xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * This file contains routines that manipulate Internet Routing Entries (IREs).
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
48*7c478bd9Sstevel@tonic-gate #include <net/if.h>
49*7c478bd9Sstevel@tonic-gate #include <net/route.h>
50*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
51*7c478bd9Sstevel@tonic-gate #include <net/if_dl.h>
52*7c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
53*7c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
56*7c478bd9Sstevel@tonic-gate #include <inet/mi.h>
57*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
58*7c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
59*7c478bd9Sstevel@tonic-gate #include <inet/ip_ndp.h>
60*7c478bd9Sstevel@tonic-gate #include <inet/arp.h>
61*7c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
62*7c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
63*7c478bd9Sstevel@tonic-gate #include <inet/ip_rts.h>
64*7c478bd9Sstevel@tonic-gate #include <inet/nd.h>
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
67*7c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
68*7c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
70*7c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
71*7c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * Synchronization notes:
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * The fields of the ire_t struct are protected in the following way :
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * ire_next/ire_ptpn
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  *	- bucket lock of the respective tables (cache or forwarding tables).
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * ire_fp_mp
84*7c478bd9Sstevel@tonic-gate  * ire_dlureq_mp
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  *	- ire_lock protects multiple threads updating ire_fp_mp
87*7c478bd9Sstevel@tonic-gate  *	  simultaneously. Otherwise no locks are used while accessing
88*7c478bd9Sstevel@tonic-gate  *	  (both read/write) both the fields.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  * ire_mp, ire_rfq, ire_stq, ire_u *except* ire_gateway_addr[v6], ire_mask,
91*7c478bd9Sstevel@tonic-gate  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, ire_ipif,
92*7c478bd9Sstevel@tonic-gate  * ire_ihandle, ire_phandle, ire_nce, ire_bucket, ire_in_ill, ire_in_src_addr
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
95*7c478bd9Sstevel@tonic-gate  *	  we don't need a lock whenever these fields are accessed.
96*7c478bd9Sstevel@tonic-gate  *
97*7c478bd9Sstevel@tonic-gate  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
98*7c478bd9Sstevel@tonic-gate  *        ire_add_v4/ire_add_v6 before inserting in the bucket and never
99*7c478bd9Sstevel@tonic-gate  *        changes after that. Thus we don't need a lock whenever these
100*7c478bd9Sstevel@tonic-gate  *	  fields are accessed.
101*7c478bd9Sstevel@tonic-gate  *
102*7c478bd9Sstevel@tonic-gate  * ire_gateway_addr_v4[v6]
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
105*7c478bd9Sstevel@tonic-gate  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
106*7c478bd9Sstevel@tonic-gate  *	  it assumed to be atomic and hence the other parts of the code
107*7c478bd9Sstevel@tonic-gate  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
108*7c478bd9Sstevel@tonic-gate  *	  and hence any access to it uses ire_lock to get/set the right value.
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * ire_ident, ire_refcnt
111*7c478bd9Sstevel@tonic-gate  *
112*7c478bd9Sstevel@tonic-gate  *	- Updated atomically using atomic_add_32
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
117*7c478bd9Sstevel@tonic-gate  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
118*7c478bd9Sstevel@tonic-gate  *
119*7c478bd9Sstevel@tonic-gate  * ire_max_frag, ire_frag_flag
120*7c478bd9Sstevel@tonic-gate  *
121*7c478bd9Sstevel@tonic-gate  *	- ire_lock is used to set/read both of them together.
122*7c478bd9Sstevel@tonic-gate  *
123*7c478bd9Sstevel@tonic-gate  * ire_tire_mark
124*7c478bd9Sstevel@tonic-gate  *
125*7c478bd9Sstevel@tonic-gate  *	- Set in ire_create and updated in ire_expire, which is called
126*7c478bd9Sstevel@tonic-gate  *	  by only one function namely ip_trash_timer_expire. Thus only
127*7c478bd9Sstevel@tonic-gate  *	  one function updates and examines the value.
128*7c478bd9Sstevel@tonic-gate  *
129*7c478bd9Sstevel@tonic-gate  * ire_marks
130*7c478bd9Sstevel@tonic-gate  *	- bucket lock protects this.
131*7c478bd9Sstevel@tonic-gate  *
132*7c478bd9Sstevel@tonic-gate  * ire_ipsec_overhead/ire_ll_hdr_length
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  *	- Place holder for returning the information to the upper layers
135*7c478bd9Sstevel@tonic-gate  *	  when IRE_DB_REQ comes down.
136*7c478bd9Sstevel@tonic-gate  *
137*7c478bd9Sstevel@tonic-gate  * ip_ire_default_count protected by the bucket lock of
138*7c478bd9Sstevel@tonic-gate  * ip_forwarding_table[0][0].
139*7c478bd9Sstevel@tonic-gate  *
140*7c478bd9Sstevel@tonic-gate  * ipv6_ire_default_count is protected by the bucket lock of
141*7c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6[0][0].
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  * ip_ire_default_index/ipv6_ire_default_index is not protected as it
144*7c478bd9Sstevel@tonic-gate  * is just a hint at which default gateway to use. There is nothing
145*7c478bd9Sstevel@tonic-gate  * wrong in using the same gateway for two different connections.
146*7c478bd9Sstevel@tonic-gate  *
147*7c478bd9Sstevel@tonic-gate  * As we always hold the bucket locks in all the places while accessing
148*7c478bd9Sstevel@tonic-gate  * the above values, it is natural to use them for protecting them.
149*7c478bd9Sstevel@tonic-gate  *
150*7c478bd9Sstevel@tonic-gate  * We have a separate cache table and forwarding table for IPv4 and IPv6.
151*7c478bd9Sstevel@tonic-gate  * Cache table (ip_cache_table/ip_cache_table_v6) is a pointer to an
152*7c478bd9Sstevel@tonic-gate  * array of irb_t structure and forwarding table (ip_forwarding_table/
153*7c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6) is an array of pointers to array of irb_t
154*7c478bd9Sstevel@tonic-gate  * structure. ip_forwarding_table[_v6] is allocated dynamically in
155*7c478bd9Sstevel@tonic-gate  * ire_add_v4/v6. ire_ft_init_lock is used to serialize multiple threads
156*7c478bd9Sstevel@tonic-gate  * initializing the same bucket. Once a bucket is initialized, it is never
157*7c478bd9Sstevel@tonic-gate  * de-alloacted. This assumption enables us to access ip_forwarding_table[i]
158*7c478bd9Sstevel@tonic-gate  * or ip_forwarding_table_v6[i] without any locks.
159*7c478bd9Sstevel@tonic-gate  *
160*7c478bd9Sstevel@tonic-gate  * Each irb_t - ire bucket structure has a lock to protect
161*7c478bd9Sstevel@tonic-gate  * a bucket and the ires residing in the bucket have a back pointer to
162*7c478bd9Sstevel@tonic-gate  * the bucket structure. It also has a reference count for the number
163*7c478bd9Sstevel@tonic-gate  * of threads walking the bucket - irb_refcnt which is bumped up
164*7c478bd9Sstevel@tonic-gate  * using the macro IRB_REFHOLD macro. The flags irb_flags can be
165*7c478bd9Sstevel@tonic-gate  * set to IRE_MARK_CONDEMNED indicating that there are some ires
166*7c478bd9Sstevel@tonic-gate  * in this bucket that are marked with IRE_MARK_CONDEMNED and the
167*7c478bd9Sstevel@tonic-gate  * last thread to leave the bucket should delete the ires. Usually
168*7c478bd9Sstevel@tonic-gate  * this is done by the IRB_REFRELE macro which is used to decrement
169*7c478bd9Sstevel@tonic-gate  * the reference count on a bucket.
170*7c478bd9Sstevel@tonic-gate  *
171*7c478bd9Sstevel@tonic-gate  * IRE_REFHOLD/IRE_REFRELE macros operate on the ire which increments/
172*7c478bd9Sstevel@tonic-gate  * decrements the reference count, ire_refcnt, atomically on the ire.
173*7c478bd9Sstevel@tonic-gate  * ire_refcnt is modified only using this macro. Operations on the IRE
174*7c478bd9Sstevel@tonic-gate  * could be described as follows :
175*7c478bd9Sstevel@tonic-gate  *
176*7c478bd9Sstevel@tonic-gate  * CREATE an ire with reference count initialized to 1.
177*7c478bd9Sstevel@tonic-gate  *
178*7c478bd9Sstevel@tonic-gate  * ADDITION of an ire holds the bucket lock, checks for duplicates
179*7c478bd9Sstevel@tonic-gate  * and then adds the ire. ire_add_v4/ire_add_v6 returns the ire after
180*7c478bd9Sstevel@tonic-gate  * bumping up once more i.e the reference count is 2. This is to avoid
181*7c478bd9Sstevel@tonic-gate  * an extra lookup in the functions calling ire_add which wants to
182*7c478bd9Sstevel@tonic-gate  * work with the ire after adding.
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  * LOOKUP of an ire bumps up the reference count using IRE_REFHOLD
185*7c478bd9Sstevel@tonic-gate  * macro. It is valid to bump up the referece count of the IRE,
186*7c478bd9Sstevel@tonic-gate  * after the lookup has returned an ire. Following are the lookup
187*7c478bd9Sstevel@tonic-gate  * functions that return an HELD ire :
188*7c478bd9Sstevel@tonic-gate  *
189*7c478bd9Sstevel@tonic-gate  * ire_lookup_local[_v6], ire_ctable_lookup[_v6], ire_ftable_lookup[_v6],
190*7c478bd9Sstevel@tonic-gate  * ire_cache_lookup[_v6], ire_lookup_multi[_v6], ire_route_lookup[_v6],
191*7c478bd9Sstevel@tonic-gate  * ipif_to_ire[_v6], ire_mrtun_lookup, ire_srcif_table_lookup.
192*7c478bd9Sstevel@tonic-gate  *
193*7c478bd9Sstevel@tonic-gate  * DELETION of an ire holds the bucket lock, removes it from the list
194*7c478bd9Sstevel@tonic-gate  * and then decrements the reference count for having removed from the list
195*7c478bd9Sstevel@tonic-gate  * by using the IRE_REFRELE macro. If some other thread has looked up
196*7c478bd9Sstevel@tonic-gate  * the ire, the reference count would have been bumped up and hence
197*7c478bd9Sstevel@tonic-gate  * this ire will not be freed once deleted. It will be freed once the
198*7c478bd9Sstevel@tonic-gate  * reference count drops to zero.
199*7c478bd9Sstevel@tonic-gate  *
200*7c478bd9Sstevel@tonic-gate  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
201*7c478bd9Sstevel@tonic-gate  * lookups acquire the bucket lock as RW_READER.
202*7c478bd9Sstevel@tonic-gate  *
203*7c478bd9Sstevel@tonic-gate  * NOTE : The only functions that does the IRE_REFRELE when an ire is
204*7c478bd9Sstevel@tonic-gate  *	  passed as an argument are :
205*7c478bd9Sstevel@tonic-gate  *
206*7c478bd9Sstevel@tonic-gate  *	  1) ip_wput_ire : This is because it IRE_REFHOLD/RELEs the
207*7c478bd9Sstevel@tonic-gate  *			   broadcast ires it looks up internally within
208*7c478bd9Sstevel@tonic-gate  *			   the function. Currently, for simplicity it does
209*7c478bd9Sstevel@tonic-gate  *			   not differentiate the one that is passed in and
210*7c478bd9Sstevel@tonic-gate  *			   the ones it looks up internally. It always
211*7c478bd9Sstevel@tonic-gate  *			   IRE_REFRELEs.
212*7c478bd9Sstevel@tonic-gate  *	  2) ire_send
213*7c478bd9Sstevel@tonic-gate  *	     ire_send_v6 : As ire_send calls ip_wput_ire and other functions
214*7c478bd9Sstevel@tonic-gate  *			   that take ire as an argument, it has to selectively
215*7c478bd9Sstevel@tonic-gate  *			   IRE_REFRELE the ire. To maintain symmetry,
216*7c478bd9Sstevel@tonic-gate  *			   ire_send_v6 does the same.
217*7c478bd9Sstevel@tonic-gate  *
218*7c478bd9Sstevel@tonic-gate  * Otherwise, the general rule is to do the IRE_REFRELE in the function
219*7c478bd9Sstevel@tonic-gate  * that is passing the ire as an argument.
220*7c478bd9Sstevel@tonic-gate  *
221*7c478bd9Sstevel@tonic-gate  * In trying to locate ires the following points are to be noted.
222*7c478bd9Sstevel@tonic-gate  *
223*7c478bd9Sstevel@tonic-gate  * IRE_MARK_CONDEMNED signifies that the ire has been logically deleted and is
224*7c478bd9Sstevel@tonic-gate  * to be ignored when walking the ires using ire_next.
225*7c478bd9Sstevel@tonic-gate  *
226*7c478bd9Sstevel@tonic-gate  * IRE_MARK_HIDDEN signifies that the ire is a special ire typically for the
227*7c478bd9Sstevel@tonic-gate  * benefit of in.mpathd which needs to probe interfaces for failures. Normal
228*7c478bd9Sstevel@tonic-gate  * applications should not be seeing this ire and hence this ire is ignored
229*7c478bd9Sstevel@tonic-gate  * in most cases in the search using ire_next.
230*7c478bd9Sstevel@tonic-gate  *
231*7c478bd9Sstevel@tonic-gate  * Zones note:
232*7c478bd9Sstevel@tonic-gate  *	Walking IREs within a given zone also walks certain ires in other
233*7c478bd9Sstevel@tonic-gate  *	zones.  This is done intentionally.  IRE walks with a specified
234*7c478bd9Sstevel@tonic-gate  *	zoneid are used only when doing informational reports, and
235*7c478bd9Sstevel@tonic-gate  *	zone users want to see things that they can access. See block
236*7c478bd9Sstevel@tonic-gate  *	comment in ire_walk_ill_match().
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate static irb_t *ip_forwarding_table[IP_MASK_TABLE_SIZE];
240*7c478bd9Sstevel@tonic-gate /* This is dynamically allocated in ip_ire_init */
241*7c478bd9Sstevel@tonic-gate static irb_t *ip_cache_table;
242*7c478bd9Sstevel@tonic-gate /* This is dynamically allocated in ire_add_mrtun */
243*7c478bd9Sstevel@tonic-gate irb_t	*ip_mrtun_table;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate uint32_t	ire_handle = 1;
246*7c478bd9Sstevel@tonic-gate /*
247*7c478bd9Sstevel@tonic-gate  * ire_ft_init_lock is used while initializing ip_forwarding_table
248*7c478bd9Sstevel@tonic-gate  * dynamically in ire_add.
249*7c478bd9Sstevel@tonic-gate  */
250*7c478bd9Sstevel@tonic-gate kmutex_t	ire_ft_init_lock;
251*7c478bd9Sstevel@tonic-gate kmutex_t	ire_mrtun_lock;  /* Protects creation of table and it's count */
252*7c478bd9Sstevel@tonic-gate kmutex_t	ire_srcif_table_lock; /* Same as above */
253*7c478bd9Sstevel@tonic-gate /*
254*7c478bd9Sstevel@tonic-gate  * The following counts are used to determine whether a walk is
255*7c478bd9Sstevel@tonic-gate  * needed through the reverse tunnel table or through ills
256*7c478bd9Sstevel@tonic-gate  */
257*7c478bd9Sstevel@tonic-gate kmutex_t ire_handle_lock;	/* Protects ire_handle */
258*7c478bd9Sstevel@tonic-gate uint_t	ire_mrtun_count;	/* Number of ires in reverse tun table */
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate /*
261*7c478bd9Sstevel@tonic-gate  * A per-interface routing table is created ( if not present)
262*7c478bd9Sstevel@tonic-gate  * when the first entry is added to this special routing table.
263*7c478bd9Sstevel@tonic-gate  * This special routing table is accessed through the ill data structure.
264*7c478bd9Sstevel@tonic-gate  * The routing table looks like cache table. For example, currently it
265*7c478bd9Sstevel@tonic-gate  * is used by mobile-ip foreign agent to forward data that only comes from
266*7c478bd9Sstevel@tonic-gate  * the home agent tunnel for a mobile node. Thus if the outgoing interface
267*7c478bd9Sstevel@tonic-gate  * is a RESOLVER interface, IP may need to resolve the hardware address for
268*7c478bd9Sstevel@tonic-gate  * the outgoing interface. The routing entries in this table are not updated
269*7c478bd9Sstevel@tonic-gate  * in IRE_CACHE. When MCTL msg comes back from ARP, the incoming ill informa-
270*7c478bd9Sstevel@tonic-gate  * tion is lost as the write queue is passed to ip_wput.
271*7c478bd9Sstevel@tonic-gate  * But, before sending the packet out, the hardware information must be updated
272*7c478bd9Sstevel@tonic-gate  * in the special forwarding table. ire_srcif_table_count keeps track of total
273*7c478bd9Sstevel@tonic-gate  * number of ires that are in interface based tables. Each interface based
274*7c478bd9Sstevel@tonic-gate  * table hangs off of the incoming ill and each ill_t also keeps a refcnt
275*7c478bd9Sstevel@tonic-gate  * of ires in that table.
276*7c478bd9Sstevel@tonic-gate  */
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate uint_t	ire_srcif_table_count; /* Number of ires in all srcif tables */
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /*
281*7c478bd9Sstevel@tonic-gate  * The minimum size of IRE cache table.  It will be recalcuated in
282*7c478bd9Sstevel@tonic-gate  * ip_ire_init().
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE;
285*7c478bd9Sstevel@tonic-gate uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate /*
288*7c478bd9Sstevel@tonic-gate  * The size of the forwarding table.  We will make sure that it is a
289*7c478bd9Sstevel@tonic-gate  * power of 2 in ip_ire_init().
290*7c478bd9Sstevel@tonic-gate  */
291*7c478bd9Sstevel@tonic-gate uint32_t ip_ftable_hash_size = IP_FTABLE_HASH_SIZE;
292*7c478bd9Sstevel@tonic-gate uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate struct	kmem_cache	*ire_cache;
295*7c478bd9Sstevel@tonic-gate static ire_t	ire_null;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate ire_stats_t ire_stats_v4;	/* IPv4 ire statistics */
298*7c478bd9Sstevel@tonic-gate ire_stats_t ire_stats_v6;	/* IPv6 ire statistics */
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate /*
301*7c478bd9Sstevel@tonic-gate  * The threshold number of IRE in a bucket when the IREs are
302*7c478bd9Sstevel@tonic-gate  * cleaned up.  This threshold is calculated later in ip_open()
303*7c478bd9Sstevel@tonic-gate  * based on the speed of CPU and available memory.  This default
304*7c478bd9Sstevel@tonic-gate  * value is the maximum.
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  * We have two kinds of cached IRE, temporary and
307*7c478bd9Sstevel@tonic-gate  * non-temporary.  Temporary IREs are marked with
308*7c478bd9Sstevel@tonic-gate  * IRE_MARK_TEMPORARY.  They are IREs created for non
309*7c478bd9Sstevel@tonic-gate  * TCP traffic and for forwarding purposes.  All others
310*7c478bd9Sstevel@tonic-gate  * are non-temporary IREs.  We don't mark IRE created for
311*7c478bd9Sstevel@tonic-gate  * TCP as temporary because TCP is stateful and there are
312*7c478bd9Sstevel@tonic-gate  * info stored in the IRE which can be shared by other TCP
313*7c478bd9Sstevel@tonic-gate  * connections to the same destination.  For connected
314*7c478bd9Sstevel@tonic-gate  * endpoint, we also don't want to mark the IRE used as
315*7c478bd9Sstevel@tonic-gate  * temporary because the same IRE will be used frequently,
316*7c478bd9Sstevel@tonic-gate  * otherwise, the app should not do a connect().  We change
317*7c478bd9Sstevel@tonic-gate  * the marking at ip_bind_connected_*() if necessary.
318*7c478bd9Sstevel@tonic-gate  *
319*7c478bd9Sstevel@tonic-gate  * We want to keep the cache IRE hash bucket length reasonably
320*7c478bd9Sstevel@tonic-gate  * short, otherwise IRE lookup functions will take "forever."
321*7c478bd9Sstevel@tonic-gate  * We use the "crude" function that the IRE bucket
322*7c478bd9Sstevel@tonic-gate  * length should be based on the CPU speed, which is 1 entry
323*7c478bd9Sstevel@tonic-gate  * per x MHz, depending on the shift factor ip_ire_cpu_ratio
324*7c478bd9Sstevel@tonic-gate  * (n).  This means that with a 750MHz CPU, the max bucket
325*7c478bd9Sstevel@tonic-gate  * length can be (750 >> n) entries.
326*7c478bd9Sstevel@tonic-gate  *
327*7c478bd9Sstevel@tonic-gate  * Note that this threshold is separate for temp and non-temp
328*7c478bd9Sstevel@tonic-gate  * IREs.  This means that the actual bucket length can be
329*7c478bd9Sstevel@tonic-gate  * twice as that.  And while we try to keep temporary IRE
330*7c478bd9Sstevel@tonic-gate  * length at most at the threshold value, we do not attempt to
331*7c478bd9Sstevel@tonic-gate  * make the length for non-temporary IREs fixed, for the
332*7c478bd9Sstevel@tonic-gate  * reason stated above.  Instead, we start trying to find
333*7c478bd9Sstevel@tonic-gate  * "unused" non-temporary IREs when the bucket length reaches
334*7c478bd9Sstevel@tonic-gate  * this threshold and clean them up.
335*7c478bd9Sstevel@tonic-gate  *
336*7c478bd9Sstevel@tonic-gate  * We also want to limit the amount of memory used by
337*7c478bd9Sstevel@tonic-gate  * IREs.  So if we are allowed to use ~3% of memory (M)
338*7c478bd9Sstevel@tonic-gate  * for those IREs, each bucket should not have more than
339*7c478bd9Sstevel@tonic-gate  *
340*7c478bd9Sstevel@tonic-gate  * 	M / num of cache bucket / sizeof (ire_t)
341*7c478bd9Sstevel@tonic-gate  *
342*7c478bd9Sstevel@tonic-gate  * Again the above memory uses are separate for temp and
343*7c478bd9Sstevel@tonic-gate  * non-temp cached IREs.
344*7c478bd9Sstevel@tonic-gate  *
345*7c478bd9Sstevel@tonic-gate  * We may also want the limit to be a function of the number
346*7c478bd9Sstevel@tonic-gate  * of interfaces and number of CPUs.  Doing the initialization
347*7c478bd9Sstevel@tonic-gate  * in ip_open() means that every time an interface is plumbed,
348*7c478bd9Sstevel@tonic-gate  * the max is re-calculated.  Right now, we don't do anything
349*7c478bd9Sstevel@tonic-gate  * different.  In future, when we have more experience, we
350*7c478bd9Sstevel@tonic-gate  * may want to change this behavior.
351*7c478bd9Sstevel@tonic-gate  */
352*7c478bd9Sstevel@tonic-gate uint32_t ip_ire_max_bucket_cnt = 10;
353*7c478bd9Sstevel@tonic-gate uint32_t ip6_ire_max_bucket_cnt = 10;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate /*
356*7c478bd9Sstevel@tonic-gate  * The minimum of the temporary IRE bucket count.  We do not want
357*7c478bd9Sstevel@tonic-gate  * the length of each bucket to be too short.  This may hurt
358*7c478bd9Sstevel@tonic-gate  * performance of some apps as the temporary IREs are removed too
359*7c478bd9Sstevel@tonic-gate  * often.
360*7c478bd9Sstevel@tonic-gate  */
361*7c478bd9Sstevel@tonic-gate uint32_t ip_ire_min_bucket_cnt = 3;
362*7c478bd9Sstevel@tonic-gate uint32_t ip6_ire_min_bucket_cnt = 3;
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * The ratio of memory consumed by IRE used for temporary to available
366*7c478bd9Sstevel@tonic-gate  * memory.  This is a shift factor, so 6 means the ratio 1 to 64.  This
367*7c478bd9Sstevel@tonic-gate  * value can be changed in /etc/system.  6 is a reasonable number.
368*7c478bd9Sstevel@tonic-gate  */
369*7c478bd9Sstevel@tonic-gate uint32_t ip_ire_mem_ratio = 6;
370*7c478bd9Sstevel@tonic-gate /* The shift factor for CPU speed to calculate the max IRE bucket length. */
371*7c478bd9Sstevel@tonic-gate uint32_t ip_ire_cpu_ratio = 7;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate /*
374*7c478bd9Sstevel@tonic-gate  * The maximum number of buckets in IRE cache table.  In future, we may
375*7c478bd9Sstevel@tonic-gate  * want to make it a dynamic hash table.  For the moment, we fix the
376*7c478bd9Sstevel@tonic-gate  * size and allocate the table in ip_ire_init() when IP is first loaded.
377*7c478bd9Sstevel@tonic-gate  * We take into account the amount of memory a system has.
378*7c478bd9Sstevel@tonic-gate  */
379*7c478bd9Sstevel@tonic-gate #define	IP_MAX_CACHE_TABLE_SIZE	4096
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate static uint32_t	ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
382*7c478bd9Sstevel@tonic-gate static uint32_t	ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate #define	NUM_ILLS	3	/* To build the ILL list to unlock */
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate /* Zero iulp_t for initialization. */
387*7c478bd9Sstevel@tonic-gate const iulp_t	ire_uinfo_null = { 0 };
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate static int	ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
390*7c478bd9Sstevel@tonic-gate     ipsq_func_t func);
391*7c478bd9Sstevel@tonic-gate static int	ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
392*7c478bd9Sstevel@tonic-gate     ipsq_func_t func);
393*7c478bd9Sstevel@tonic-gate static ire_t	*ire_update_srcif_v4(ire_t *ire);
394*7c478bd9Sstevel@tonic-gate static void	ire_delete_v4(ire_t *ire);
395*7c478bd9Sstevel@tonic-gate static void	ire_report_ftable(ire_t *ire, char *mp);
396*7c478bd9Sstevel@tonic-gate static void	ire_report_ctable(ire_t *ire, char *mp);
397*7c478bd9Sstevel@tonic-gate static void	ire_report_mrtun_table(ire_t *ire, char *mp);
398*7c478bd9Sstevel@tonic-gate static void	ire_report_srcif_table(ire_t *ire, char *mp);
399*7c478bd9Sstevel@tonic-gate static void	ire_walk_ipvers(pfv_t func, char *arg, uchar_t vers,
400*7c478bd9Sstevel@tonic-gate     zoneid_t zoneid);
401*7c478bd9Sstevel@tonic-gate static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
402*7c478bd9Sstevel@tonic-gate 		    pfv_t func, char *arg, uchar_t vers, ill_t *ill);
403*7c478bd9Sstevel@tonic-gate static	void	ire_walk_ill_tables(uint_t match_flags, uint_t ire_type,
404*7c478bd9Sstevel@tonic-gate 		    pfv_t func, char *arg, size_t ftbl_sz, size_t htbl_sz,
405*7c478bd9Sstevel@tonic-gate 		    irb_t **ipftbl, size_t ctbl_sz, irb_t *ipctbl, ill_t *ill,
406*7c478bd9Sstevel@tonic-gate 		    zoneid_t zoneid);
407*7c478bd9Sstevel@tonic-gate static void	ire_delete_host_redirects(ipaddr_t gateway);
408*7c478bd9Sstevel@tonic-gate static boolean_t ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask,
409*7c478bd9Sstevel@tonic-gate 		    ipaddr_t gateway, int type, ipif_t *ipif, zoneid_t zoneid,
410*7c478bd9Sstevel@tonic-gate 		    uint32_t ihandle, int match_flags);
411*7c478bd9Sstevel@tonic-gate static void	ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt);
412*7c478bd9Sstevel@tonic-gate extern void	ill_unlock_ills(ill_t **list, int cnt);
413*7c478bd9Sstevel@tonic-gate static void	ire_fastpath_list_add(ill_t *ill, ire_t *ire);
414*7c478bd9Sstevel@tonic-gate extern void	th_trace_rrecord(th_trace_t *);
415*7c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
416*7c478bd9Sstevel@tonic-gate static void	ire_trace_inactive(ire_t *);
417*7c478bd9Sstevel@tonic-gate #endif
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate /*
420*7c478bd9Sstevel@tonic-gate  * To avoid bloating the code, we call this function instead of
421*7c478bd9Sstevel@tonic-gate  * using the macro IRE_REFRELE. Use macro only in performance
422*7c478bd9Sstevel@tonic-gate  * critical paths.
423*7c478bd9Sstevel@tonic-gate  *
424*7c478bd9Sstevel@tonic-gate  * Must not be called while holding any locks. Otherwise if this is
425*7c478bd9Sstevel@tonic-gate  * the last reference to be released there is a chance of recursive mutex
426*7c478bd9Sstevel@tonic-gate  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
427*7c478bd9Sstevel@tonic-gate  * to restart an ioctl. The one exception is when the caller is sure that
428*7c478bd9Sstevel@tonic-gate  * this is not the last reference to be released. Eg. if the caller is
429*7c478bd9Sstevel@tonic-gate  * sure that the ire has not been deleted and won't be deleted.
430*7c478bd9Sstevel@tonic-gate  */
431*7c478bd9Sstevel@tonic-gate void
432*7c478bd9Sstevel@tonic-gate ire_refrele(ire_t *ire)
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	IRE_REFRELE(ire);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate void
438*7c478bd9Sstevel@tonic-gate ire_refrele_notr(ire_t *ire)
439*7c478bd9Sstevel@tonic-gate {
440*7c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate /*
444*7c478bd9Sstevel@tonic-gate  * kmem_cache_alloc constructor for IRE in kma space.
445*7c478bd9Sstevel@tonic-gate  * Note that when ire_mp is set the IRE is stored in that mblk and
446*7c478bd9Sstevel@tonic-gate  * not in this cache.
447*7c478bd9Sstevel@tonic-gate  */
448*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
449*7c478bd9Sstevel@tonic-gate static int
450*7c478bd9Sstevel@tonic-gate ip_ire_constructor(void *buf, void *cdrarg, int kmflags)
451*7c478bd9Sstevel@tonic-gate {
452*7c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	ire->ire_fp_mp = NULL;
455*7c478bd9Sstevel@tonic-gate 	ire->ire_dlureq_mp = NULL;
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	return (0);
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
461*7c478bd9Sstevel@tonic-gate static void
462*7c478bd9Sstevel@tonic-gate ip_ire_destructor(void *buf, void *cdrarg)
463*7c478bd9Sstevel@tonic-gate {
464*7c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fp_mp == NULL);
467*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_dlureq_mp == NULL);
468*7c478bd9Sstevel@tonic-gate }
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate /*
471*7c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY
472*7c478bd9Sstevel@tonic-gate  * IOCTL.  It is used by TCP (or other ULPs) to supply revised information
473*7c478bd9Sstevel@tonic-gate  * for an existing CACHED IRE.
474*7c478bd9Sstevel@tonic-gate  */
475*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
476*7c478bd9Sstevel@tonic-gate int
477*7c478bd9Sstevel@tonic-gate ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
480*7c478bd9Sstevel@tonic-gate 	ipic_t	*ipic;
481*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
482*7c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
483*7c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
484*7c478bd9Sstevel@tonic-gate 	irb_t	*irb;
485*7c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
488*7c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	/*
491*7c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
492*7c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
493*7c478bd9Sstevel@tonic-gate 	 */
494*7c478bd9Sstevel@tonic-gate 	if (ioc_cr != NULL && secpolicy_net_config(ioc_cr, B_FALSE) != 0)
495*7c478bd9Sstevel@tonic-gate 		return (EPERM);
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 	ipic = (ipic_t *)mp->b_rptr;
498*7c478bd9Sstevel@tonic-gate 	if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset,
499*7c478bd9Sstevel@tonic-gate 	    ipic->ipic_addr_length))) {
500*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
501*7c478bd9Sstevel@tonic-gate 	}
502*7c478bd9Sstevel@tonic-gate 	if (!OK_32PTR(addr_ucp))
503*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
504*7c478bd9Sstevel@tonic-gate 	switch (ipic->ipic_addr_length) {
505*7c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN: {
506*7c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
507*7c478bd9Sstevel@tonic-gate 		addr = *(ipaddr_t *)addr_ucp;
508*7c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
509*7c478bd9Sstevel@tonic-gate 		ire = ire_cache_lookup(addr, zoneid);
510*7c478bd9Sstevel@tonic-gate 		break;
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate 	case IPV6_ADDR_LEN: {
513*7c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
514*7c478bd9Sstevel@tonic-gate 		v6addr = *(in6_addr_t *)addr_ucp;
515*7c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
516*7c478bd9Sstevel@tonic-gate 		ire = ire_cache_lookup_v6(&v6addr, zoneid);
517*7c478bd9Sstevel@tonic-gate 		break;
518*7c478bd9Sstevel@tonic-gate 	}
519*7c478bd9Sstevel@tonic-gate 	default:
520*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	if (ire == NULL)
524*7c478bd9Sstevel@tonic-gate 		return (ENOENT);
525*7c478bd9Sstevel@tonic-gate 	/*
526*7c478bd9Sstevel@tonic-gate 	 * Update the round trip time estimate and/or the max frag size
527*7c478bd9Sstevel@tonic-gate 	 * and/or the slow start threshold.
528*7c478bd9Sstevel@tonic-gate 	 *
529*7c478bd9Sstevel@tonic-gate 	 * We serialize multiple advises using ire_lock.
530*7c478bd9Sstevel@tonic-gate 	 */
531*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
532*7c478bd9Sstevel@tonic-gate 	if (ipic->ipic_rtt) {
533*7c478bd9Sstevel@tonic-gate 		/*
534*7c478bd9Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
535*7c478bd9Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
536*7c478bd9Sstevel@tonic-gate 		 */
537*7c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
538*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
539*7c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt) >> 1;
540*7c478bd9Sstevel@tonic-gate 		} else {
541*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt +
542*7c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt >> 1);
543*7c478bd9Sstevel@tonic-gate 		}
544*7c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
545*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
546*7c478bd9Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
547*7c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt_sd) >> 1;
548*7c478bd9Sstevel@tonic-gate 		} else {
549*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd +
550*7c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt_sd >> 1);
551*7c478bd9Sstevel@tonic-gate 		}
552*7c478bd9Sstevel@tonic-gate 	}
553*7c478bd9Sstevel@tonic-gate 	if (ipic->ipic_max_frag)
554*7c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET);
555*7c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ssthresh != 0) {
556*7c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_ssthresh != 0)
557*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh =
558*7c478bd9Sstevel@tonic-gate 			    (ipic->ipic_ssthresh +
559*7c478bd9Sstevel@tonic-gate 			    ire->ire_uinfo.iulp_ssthresh) >> 1;
560*7c478bd9Sstevel@tonic-gate 		else
561*7c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh;
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 	/*
564*7c478bd9Sstevel@tonic-gate 	 * Don't need the ire_lock below this. ire_type does not change
565*7c478bd9Sstevel@tonic-gate 	 * after initialization. ire_marks is protected by irb_lock.
566*7c478bd9Sstevel@tonic-gate 	 */
567*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) {
570*7c478bd9Sstevel@tonic-gate 		/*
571*7c478bd9Sstevel@tonic-gate 		 * Only increment the temporary IRE count if the original
572*7c478bd9Sstevel@tonic-gate 		 * IRE is not already marked temporary.
573*7c478bd9Sstevel@tonic-gate 		 */
574*7c478bd9Sstevel@tonic-gate 		irb = ire->ire_bucket;
575*7c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_WRITER);
576*7c478bd9Sstevel@tonic-gate 		if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) &&
577*7c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_TEMPORARY)) {
578*7c478bd9Sstevel@tonic-gate 			irb->irb_tmp_ire_cnt++;
579*7c478bd9Sstevel@tonic-gate 		}
580*7c478bd9Sstevel@tonic-gate 		ire->ire_marks |= ipic->ipic_ire_marks;
581*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
585*7c478bd9Sstevel@tonic-gate 	return (0);
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate /*
589*7c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
590*7c478bd9Sstevel@tonic-gate  * IOCTL[s].  The NO_REPLY form is used by TCP to delete a route IRE
591*7c478bd9Sstevel@tonic-gate  * for a host that is not responding.  This will force an attempt to
592*7c478bd9Sstevel@tonic-gate  * establish a new route, if available.  Management processes may want
593*7c478bd9Sstevel@tonic-gate  * to use the version that generates a reply.
594*7c478bd9Sstevel@tonic-gate  *
595*7c478bd9Sstevel@tonic-gate  * This function does not support IPv6 since Neighbor Unreachability Detection
596*7c478bd9Sstevel@tonic-gate  * means that negative advise like this is useless.
597*7c478bd9Sstevel@tonic-gate  */
598*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
599*7c478bd9Sstevel@tonic-gate int
600*7c478bd9Sstevel@tonic-gate ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
601*7c478bd9Sstevel@tonic-gate {
602*7c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
603*7c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
604*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
605*7c478bd9Sstevel@tonic-gate 	ipid_t	*ipid;
606*7c478bd9Sstevel@tonic-gate 	boolean_t routing_sock_info = B_FALSE;	/* Sent info? */
607*7c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
610*7c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	/*
613*7c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
614*7c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
615*7c478bd9Sstevel@tonic-gate 	 */
616*7c478bd9Sstevel@tonic-gate 	if (ioc_cr != NULL && secpolicy_net_config(ioc_cr, B_FALSE) != 0)
617*7c478bd9Sstevel@tonic-gate 		return (EPERM);
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_rptr;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	/* Only actions on IRE_CACHEs are acceptable at present. */
622*7c478bd9Sstevel@tonic-gate 	if (ipid->ipid_ire_type != IRE_CACHE)
623*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
626*7c478bd9Sstevel@tonic-gate 		ipid->ipid_addr_length);
627*7c478bd9Sstevel@tonic-gate 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
628*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
629*7c478bd9Sstevel@tonic-gate 	switch (ipid->ipid_addr_length) {
630*7c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN:
631*7c478bd9Sstevel@tonic-gate 		/* addr_ucp points at IP addr */
632*7c478bd9Sstevel@tonic-gate 		break;
633*7c478bd9Sstevel@tonic-gate 	case sizeof (sin_t): {
634*7c478bd9Sstevel@tonic-gate 		sin_t	*sin;
635*7c478bd9Sstevel@tonic-gate 		/*
636*7c478bd9Sstevel@tonic-gate 		 * got complete (sockaddr) address - increment addr_ucp to point
637*7c478bd9Sstevel@tonic-gate 		 * at the ip_addr field.
638*7c478bd9Sstevel@tonic-gate 		 */
639*7c478bd9Sstevel@tonic-gate 		sin = (sin_t *)addr_ucp;
640*7c478bd9Sstevel@tonic-gate 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
641*7c478bd9Sstevel@tonic-gate 		break;
642*7c478bd9Sstevel@tonic-gate 	}
643*7c478bd9Sstevel@tonic-gate 	default:
644*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate 	/* Extract the destination address. */
647*7c478bd9Sstevel@tonic-gate 	bcopy(addr_ucp, &addr, IP_ADDR_LEN);
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	/* Try to find the CACHED IRE. */
650*7c478bd9Sstevel@tonic-gate 	ire = ire_cache_lookup(addr, zoneid);
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate 	/* Nail it. */
653*7c478bd9Sstevel@tonic-gate 	if (ire) {
654*7c478bd9Sstevel@tonic-gate 		/* Allow delete only on CACHE entries */
655*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type != IRE_CACHE) {
656*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
657*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
658*7c478bd9Sstevel@tonic-gate 		}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 		/*
661*7c478bd9Sstevel@tonic-gate 		 * Verify that the IRE has been around for a while.
662*7c478bd9Sstevel@tonic-gate 		 * This is to protect against transport protocols
663*7c478bd9Sstevel@tonic-gate 		 * that are too eager in sending delete messages.
664*7c478bd9Sstevel@tonic-gate 		 */
665*7c478bd9Sstevel@tonic-gate 		if (gethrestime_sec() <
666*7c478bd9Sstevel@tonic-gate 		    ire->ire_create_time + ip_ignore_delete_time) {
667*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
668*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
669*7c478bd9Sstevel@tonic-gate 		}
670*7c478bd9Sstevel@tonic-gate 		/*
671*7c478bd9Sstevel@tonic-gate 		 * Now we have a potentially dead cache entry. We need
672*7c478bd9Sstevel@tonic-gate 		 * to remove it.
673*7c478bd9Sstevel@tonic-gate 		 * If this cache entry is generated from a default route,
674*7c478bd9Sstevel@tonic-gate 		 * search the default list and mark it dead and some
675*7c478bd9Sstevel@tonic-gate 		 * background process will try to activate it.
676*7c478bd9Sstevel@tonic-gate 		 */
677*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) {
678*7c478bd9Sstevel@tonic-gate 			/*
679*7c478bd9Sstevel@tonic-gate 			 * Make sure that we pick a different
680*7c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT next time.
681*7c478bd9Sstevel@tonic-gate 			 * The ip_ire_default_count tracks the number of
682*7c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT entries. However, the
683*7c478bd9Sstevel@tonic-gate 			 * ip_forwarding_table[0] also contains
684*7c478bd9Sstevel@tonic-gate 			 * interface routes thus the count can be zero.
685*7c478bd9Sstevel@tonic-gate 			 */
686*7c478bd9Sstevel@tonic-gate 			ire_t *gw_ire;
687*7c478bd9Sstevel@tonic-gate 			irb_t *irb_ptr;
688*7c478bd9Sstevel@tonic-gate 			irb_t *irb;
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 			if (((irb_ptr = ip_forwarding_table[0]) != NULL) &&
691*7c478bd9Sstevel@tonic-gate 			    (irb = &irb_ptr[0])->irb_ire != NULL &&
692*7c478bd9Sstevel@tonic-gate 			    ip_ire_default_count != 0) {
693*7c478bd9Sstevel@tonic-gate 				uint_t index;
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 				/*
696*7c478bd9Sstevel@tonic-gate 				 * We grab it as writer just to serialize
697*7c478bd9Sstevel@tonic-gate 				 * multiple threads trying to bump up
698*7c478bd9Sstevel@tonic-gate 				 * ip_ire_default_index.
699*7c478bd9Sstevel@tonic-gate 				 */
700*7c478bd9Sstevel@tonic-gate 				rw_enter(&irb->irb_lock, RW_WRITER);
701*7c478bd9Sstevel@tonic-gate 				if ((gw_ire = irb->irb_ire) == NULL) {
702*7c478bd9Sstevel@tonic-gate 					rw_exit(&irb->irb_lock);
703*7c478bd9Sstevel@tonic-gate 					goto done;
704*7c478bd9Sstevel@tonic-gate 				}
705*7c478bd9Sstevel@tonic-gate 				index = ip_ire_default_index %
706*7c478bd9Sstevel@tonic-gate 				    ip_ire_default_count;
707*7c478bd9Sstevel@tonic-gate 				while (index-- && gw_ire->ire_next != NULL)
708*7c478bd9Sstevel@tonic-gate 					gw_ire = gw_ire->ire_next;
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 				/* Skip past the potentially bad gateway */
711*7c478bd9Sstevel@tonic-gate 				if (ire->ire_gateway_addr ==
712*7c478bd9Sstevel@tonic-gate 				    gw_ire->ire_gateway_addr)
713*7c478bd9Sstevel@tonic-gate 					ip_ire_default_index++;
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
716*7c478bd9Sstevel@tonic-gate 		    }
717*7c478bd9Sstevel@tonic-gate 		}
718*7c478bd9Sstevel@tonic-gate done:
719*7c478bd9Sstevel@tonic-gate 		/* report the bad route to routing sockets */
720*7c478bd9Sstevel@tonic-gate 		ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr,
721*7c478bd9Sstevel@tonic-gate 		    ire->ire_mask, ire->ire_src_addr, 0, 0, 0,
722*7c478bd9Sstevel@tonic-gate 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA));
723*7c478bd9Sstevel@tonic-gate 		routing_sock_info = B_TRUE;
724*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
725*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
726*7c478bd9Sstevel@tonic-gate 	}
727*7c478bd9Sstevel@tonic-gate 	/* Also look for an IRE_HOST_REDIRECT and remove it if present */
728*7c478bd9Sstevel@tonic-gate 	ire = ire_route_lookup(addr, 0, 0, IRE_HOST_REDIRECT, NULL, NULL,
729*7c478bd9Sstevel@tonic-gate 	    ALL_ZONES, MATCH_IRE_TYPE);
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	/* Nail it. */
732*7c478bd9Sstevel@tonic-gate 	if (ire) {
733*7c478bd9Sstevel@tonic-gate 		if (!routing_sock_info) {
734*7c478bd9Sstevel@tonic-gate 			ip_rts_change(RTM_LOSING, ire->ire_addr,
735*7c478bd9Sstevel@tonic-gate 			    ire->ire_gateway_addr, ire->ire_mask,
736*7c478bd9Sstevel@tonic-gate 			    ire->ire_src_addr, 0, 0, 0,
737*7c478bd9Sstevel@tonic-gate 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA));
738*7c478bd9Sstevel@tonic-gate 		}
739*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
740*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
741*7c478bd9Sstevel@tonic-gate 	}
742*7c478bd9Sstevel@tonic-gate 	return (0);
743*7c478bd9Sstevel@tonic-gate }
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate /*
746*7c478bd9Sstevel@tonic-gate  * Named Dispatch routine to produce a formatted report on all IREs.
747*7c478bd9Sstevel@tonic-gate  * This report is accessed by using the ndd utility to "get" ND variable
748*7c478bd9Sstevel@tonic-gate  * "ipv4_ire_status".
749*7c478bd9Sstevel@tonic-gate  */
750*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
751*7c478bd9Sstevel@tonic-gate int
752*7c478bd9Sstevel@tonic-gate ip_ire_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
753*7c478bd9Sstevel@tonic-gate {
754*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
757*7c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
758*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
759*7c478bd9Sstevel@tonic-gate 	    "rfq      " MI_COL_HDRPAD_STR
760*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
761*7c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
762*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
763*7c478bd9Sstevel@tonic-gate 	    " zone "
764*7c478bd9Sstevel@tonic-gate 	/*   12345 */
765*7c478bd9Sstevel@tonic-gate 	    "addr            mask            "
766*7c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 */
767*7c478bd9Sstevel@tonic-gate 	    "src             gateway         mxfrg rtt   rtt_sd ssthresh ref "
768*7c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 12345 12345 123456 12345678 123 */
769*7c478bd9Sstevel@tonic-gate 	    "rtomax tstamp_ok wscale_ok ecn_ok pmtud_ok sack sendpipe "
770*7c478bd9Sstevel@tonic-gate 	/*   123456 123456789 123456789 123456 12345678 1234 12345678 */
771*7c478bd9Sstevel@tonic-gate 	    "recvpipe in/out/forward type");
772*7c478bd9Sstevel@tonic-gate 	/*   12345678 in/out/forward xxxxxxxxxx */
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	/*
775*7c478bd9Sstevel@tonic-gate 	 * Because of the ndd constraint, at most we can have 64K buffer
776*7c478bd9Sstevel@tonic-gate 	 * to put in all IRE info.  So to be more efficient, just
777*7c478bd9Sstevel@tonic-gate 	 * allocate a 64K buffer here, assuming we need that large buffer.
778*7c478bd9Sstevel@tonic-gate 	 * This should be OK as only root can do ndd /dev/ip.
779*7c478bd9Sstevel@tonic-gate 	 */
780*7c478bd9Sstevel@tonic-gate 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
781*7c478bd9Sstevel@tonic-gate 		/* The following may work even if we cannot get a large buf. */
782*7c478bd9Sstevel@tonic-gate 		(void) mi_mpprintf(mp, "<< Out of buffer >>\n");
783*7c478bd9Sstevel@tonic-gate 		return (0);
784*7c478bd9Sstevel@tonic-gate 	}
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
787*7c478bd9Sstevel@tonic-gate 	if (zoneid == GLOBAL_ZONEID)
788*7c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	ire_walk_v4(ire_report_ftable, (char *)mp->b_cont, zoneid);
791*7c478bd9Sstevel@tonic-gate 	ire_walk_v4(ire_report_ctable, (char *)mp->b_cont, zoneid);
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate 	return (0);
794*7c478bd9Sstevel@tonic-gate }
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each IRE. */
797*7c478bd9Sstevel@tonic-gate static void
798*7c478bd9Sstevel@tonic-gate ire_report_ftable(ire_t *ire, char *mp)
799*7c478bd9Sstevel@tonic-gate {
800*7c478bd9Sstevel@tonic-gate 	char	buf1[16];
801*7c478bd9Sstevel@tonic-gate 	char	buf2[16];
802*7c478bd9Sstevel@tonic-gate 	char	buf3[16];
803*7c478bd9Sstevel@tonic-gate 	char	buf4[16];
804*7c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
805*7c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
806*7c478bd9Sstevel@tonic-gate 	int	ref;
807*7c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
808*7c478bd9Sstevel@tonic-gate 
809*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHETABLE)
810*7c478bd9Sstevel@tonic-gate 	    return;
811*7c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
812*7c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
813*7c478bd9Sstevel@tonic-gate 		return;
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
816*7c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
817*7c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
818*7c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
819*7c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
820*7c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
821*7c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
822*7c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
823*7c478bd9Sstevel@tonic-gate 	}
824*7c478bd9Sstevel@tonic-gate 	print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
825*7c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
826*7c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
827*7c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
828*7c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
829*7c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
830*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
831*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
832*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
833*7c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
834*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd,
835*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_ssthresh, ref,
836*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
837*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
838*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
839*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
840*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
841*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
842*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
843*7c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
844*7c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
845*7c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
846*7c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
847*7c478bd9Sstevel@tonic-gate 	} else {
848*7c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
849*7c478bd9Sstevel@tonic-gate 	}
850*7c478bd9Sstevel@tonic-gate }
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each cached IRE. */
853*7c478bd9Sstevel@tonic-gate static void
854*7c478bd9Sstevel@tonic-gate ire_report_ctable(ire_t *ire, char *mp)
855*7c478bd9Sstevel@tonic-gate {
856*7c478bd9Sstevel@tonic-gate 	char	buf1[16];
857*7c478bd9Sstevel@tonic-gate 	char	buf2[16];
858*7c478bd9Sstevel@tonic-gate 	char	buf3[16];
859*7c478bd9Sstevel@tonic-gate 	char	buf4[16];
860*7c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
861*7c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
862*7c478bd9Sstevel@tonic-gate 	int	ref;
863*7c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0)
866*7c478bd9Sstevel@tonic-gate 	    return;
867*7c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
868*7c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
869*7c478bd9Sstevel@tonic-gate 		return;
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
872*7c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
873*7c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
874*7c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
875*7c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
876*7c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
877*7c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
878*7c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
879*7c478bd9Sstevel@tonic-gate 	}
880*7c478bd9Sstevel@tonic-gate 	print_len =  snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
881*7c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
882*7c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
883*7c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
884*7c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
885*7c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
886*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
887*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
888*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
889*7c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
890*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd, ire->ire_uinfo.iulp_ssthresh, ref,
891*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
892*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
893*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
894*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
895*7c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
896*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
897*7c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
898*7c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
899*7c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
900*7c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
901*7c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
902*7c478bd9Sstevel@tonic-gate 	} else {
903*7c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
904*7c478bd9Sstevel@tonic-gate 	}
905*7c478bd9Sstevel@tonic-gate }
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
908*7c478bd9Sstevel@tonic-gate int
909*7c478bd9Sstevel@tonic-gate ip_ire_report_mrtun(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
910*7c478bd9Sstevel@tonic-gate {
911*7c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
912*7c478bd9Sstevel@tonic-gate 	"IRE      " MI_COL_HDRPAD_STR
913*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
914*7c478bd9Sstevel@tonic-gate 	"stq      " MI_COL_HDRPAD_STR
915*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
916*7c478bd9Sstevel@tonic-gate 	"in_ill    " MI_COL_HDRPAD_STR
917*7c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
918*7c478bd9Sstevel@tonic-gate 	"in_src_addr            "
919*7c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 */
920*7c478bd9Sstevel@tonic-gate 	"max_frag      "
921*7c478bd9Sstevel@tonic-gate 	/*   12345 */
922*7c478bd9Sstevel@tonic-gate 	"ref     ");
923*7c478bd9Sstevel@tonic-gate 	/*   123 */
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 	ire_walk_ill_mrtun(0, 0, ire_report_mrtun_table, (char *)mp, NULL);
926*7c478bd9Sstevel@tonic-gate 	return (0);
927*7c478bd9Sstevel@tonic-gate }
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate /* mrtun report table - supports ipv4_mrtun_ire_status ndd variable */
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate static void
932*7c478bd9Sstevel@tonic-gate ire_report_mrtun_table(ire_t *ire, char *mp)
933*7c478bd9Sstevel@tonic-gate {
934*7c478bd9Sstevel@tonic-gate 	char	buf1[INET_ADDRSTRLEN];
935*7c478bd9Sstevel@tonic-gate 	int	ref;
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
938*7c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
939*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_MIPRTUN);
940*7c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
941*7c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
942*7c478bd9Sstevel@tonic-gate 	    "%s          %05d             %03d",
943*7c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
944*7c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
945*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_in_src_addr, buf1),
946*7c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref);
947*7c478bd9Sstevel@tonic-gate }
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate /*
950*7c478bd9Sstevel@tonic-gate  * Dispatch routine to format ires in interface based routine
951*7c478bd9Sstevel@tonic-gate  */
952*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
953*7c478bd9Sstevel@tonic-gate int
954*7c478bd9Sstevel@tonic-gate ip_ire_report_srcif(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
955*7c478bd9Sstevel@tonic-gate {
956*7c478bd9Sstevel@tonic-gate 
957*7c478bd9Sstevel@tonic-gate 	/* Report all interface based ires */
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
960*7c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
961*7c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
962*7c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
963*7c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
964*7c478bd9Sstevel@tonic-gate 	    "in_ill    " MI_COL_HDRPAD_STR
965*7c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
966*7c478bd9Sstevel@tonic-gate 	    "addr            "
967*7c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
968*7c478bd9Sstevel@tonic-gate 	    "gateway         "
969*7c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
970*7c478bd9Sstevel@tonic-gate 	    "max_frag      "
971*7c478bd9Sstevel@tonic-gate 	    /*   12345 */
972*7c478bd9Sstevel@tonic-gate 	    "ref     "
973*7c478bd9Sstevel@tonic-gate 	    /*   123 */
974*7c478bd9Sstevel@tonic-gate 	    "type    "
975*7c478bd9Sstevel@tonic-gate 	    /* ABCDEFGH */
976*7c478bd9Sstevel@tonic-gate 	    "in/out/forward");
977*7c478bd9Sstevel@tonic-gate 	ire_walk_srcif_table_v4(ire_report_srcif_table, (char *)mp);
978*7c478bd9Sstevel@tonic-gate 	return (0);
979*7c478bd9Sstevel@tonic-gate }
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate /* Reports the interface table ires */
982*7c478bd9Sstevel@tonic-gate static void
983*7c478bd9Sstevel@tonic-gate ire_report_srcif_table(ire_t *ire, char *mp)
984*7c478bd9Sstevel@tonic-gate {
985*7c478bd9Sstevel@tonic-gate 	char    buf1[INET_ADDRSTRLEN];
986*7c478bd9Sstevel@tonic-gate 	char    buf2[INET_ADDRSTRLEN];
987*7c478bd9Sstevel@tonic-gate 	int	ref;
988*7c478bd9Sstevel@tonic-gate 
989*7c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
990*7c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
991*7c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
992*7c478bd9Sstevel@tonic-gate 	    "%s    %s      %05d       %03d      %s     %d",
993*7c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
994*7c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
995*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1),
996*7c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf2),
997*7c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref,
998*7c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type),
999*7c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count);
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate }
1002*7c478bd9Sstevel@tonic-gate /*
1003*7c478bd9Sstevel@tonic-gate  * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed
1004*7c478bd9Sstevel@tonic-gate  * down from the Upper Level Protocol to request a copy of the IRE (to check
1005*7c478bd9Sstevel@tonic-gate  * its type or to extract information like round-trip time estimates or the
1006*7c478bd9Sstevel@tonic-gate  * MTU.)
1007*7c478bd9Sstevel@tonic-gate  * The address is assumed to be in the ire_addr field. If no IRE is found
1008*7c478bd9Sstevel@tonic-gate  * an IRE is returned with ire_type being zero.
1009*7c478bd9Sstevel@tonic-gate  * Note that the upper lavel protocol has to check for broadcast
1010*7c478bd9Sstevel@tonic-gate  * (IRE_BROADCAST) and multicast (CLASSD(addr)).
1011*7c478bd9Sstevel@tonic-gate  * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the
1012*7c478bd9Sstevel@tonic-gate  * end of the returned message.
1013*7c478bd9Sstevel@tonic-gate  *
1014*7c478bd9Sstevel@tonic-gate  * TCP sends down a message of this type with a connection request packet
1015*7c478bd9Sstevel@tonic-gate  * chained on. UDP and ICMP send it down to verify that a route exists for
1016*7c478bd9Sstevel@tonic-gate  * the destination address when they get connected.
1017*7c478bd9Sstevel@tonic-gate  */
1018*7c478bd9Sstevel@tonic-gate void
1019*7c478bd9Sstevel@tonic-gate ip_ire_req(queue_t *q, mblk_t *mp)
1020*7c478bd9Sstevel@tonic-gate {
1021*7c478bd9Sstevel@tonic-gate 	ire_t	*inire;
1022*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
1023*7c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
1024*7c478bd9Sstevel@tonic-gate 	ire_t	*sire = NULL;
1025*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid;
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) ||
1028*7c478bd9Sstevel@tonic-gate 	    !OK_32PTR(mp->b_rptr)) {
1029*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1030*7c478bd9Sstevel@tonic-gate 		return;
1031*7c478bd9Sstevel@tonic-gate 	}
1032*7c478bd9Sstevel@tonic-gate 	inire = (ire_t *)mp->b_rptr;
1033*7c478bd9Sstevel@tonic-gate 	/*
1034*7c478bd9Sstevel@tonic-gate 	 * Got it, now take our best shot at an IRE.
1035*7c478bd9Sstevel@tonic-gate 	 */
1036*7c478bd9Sstevel@tonic-gate 	if (inire->ire_ipversion == IPV6_VERSION) {
1037*7c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0,
1038*7c478bd9Sstevel@tonic-gate 		    NULL, &sire, zoneid,
1039*7c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT));
1040*7c478bd9Sstevel@tonic-gate 	} else {
1041*7c478bd9Sstevel@tonic-gate 		ASSERT(inire->ire_ipversion == IPV4_VERSION);
1042*7c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(inire->ire_addr, 0, 0, 0,
1043*7c478bd9Sstevel@tonic-gate 		    NULL, &sire, zoneid,
1044*7c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT));
1045*7c478bd9Sstevel@tonic-gate 	}
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	/*
1048*7c478bd9Sstevel@tonic-gate 	 * We prevent returning IRES with source address INADDR_ANY
1049*7c478bd9Sstevel@tonic-gate 	 * as these were temporarily created for sending packets
1050*7c478bd9Sstevel@tonic-gate 	 * from endpoints that have conn_unspec_src set.
1051*7c478bd9Sstevel@tonic-gate 	 */
1052*7c478bd9Sstevel@tonic-gate 	if (ire == NULL ||
1053*7c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV4_VERSION &&
1054*7c478bd9Sstevel@tonic-gate 	    ire->ire_src_addr == INADDR_ANY) ||
1055*7c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV6_VERSION &&
1056*7c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) {
1057*7c478bd9Sstevel@tonic-gate 		inire->ire_type = 0;
1058*7c478bd9Sstevel@tonic-gate 	} else {
1059*7c478bd9Sstevel@tonic-gate 		bcopy(ire, inire, sizeof (ire_t));
1060*7c478bd9Sstevel@tonic-gate 		/* Copy the route metrics from the parent. */
1061*7c478bd9Sstevel@tonic-gate 		if (sire != NULL) {
1062*7c478bd9Sstevel@tonic-gate 			bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo),
1063*7c478bd9Sstevel@tonic-gate 			    sizeof (iulp_t));
1064*7c478bd9Sstevel@tonic-gate 		}
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate 		/*
1067*7c478bd9Sstevel@tonic-gate 		 * As we don't lookup global policy here, we may not
1068*7c478bd9Sstevel@tonic-gate 		 * pass the right size if per-socket policy is not
1069*7c478bd9Sstevel@tonic-gate 		 * present. For these cases, path mtu discovery will
1070*7c478bd9Sstevel@tonic-gate 		 * do the right thing.
1071*7c478bd9Sstevel@tonic-gate 		 */
1072*7c478bd9Sstevel@tonic-gate 		inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q));
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate 		/* Pass the latest setting of the ip_path_mtu_discovery */
1075*7c478bd9Sstevel@tonic-gate 		inire->ire_frag_flag |= (ip_path_mtu_discovery) ? IPH_DF : 0;
1076*7c478bd9Sstevel@tonic-gate 	}
1077*7c478bd9Sstevel@tonic-gate 	if (ire != NULL)
1078*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
1079*7c478bd9Sstevel@tonic-gate 	if (sire != NULL)
1080*7c478bd9Sstevel@tonic-gate 		ire_refrele(sire);
1081*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[sizeof (ire_t)];
1082*7c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
1083*7c478bd9Sstevel@tonic-gate 
1084*7c478bd9Sstevel@tonic-gate 	/* Put the IRE_DB_TYPE mblk last in the chain */
1085*7c478bd9Sstevel@tonic-gate 	mp1 = mp->b_cont;
1086*7c478bd9Sstevel@tonic-gate 	if (mp1 != NULL) {
1087*7c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
1088*7c478bd9Sstevel@tonic-gate 		linkb(mp1, mp);
1089*7c478bd9Sstevel@tonic-gate 		mp = mp1;
1090*7c478bd9Sstevel@tonic-gate 	}
1091*7c478bd9Sstevel@tonic-gate 	qreply(q, mp);
1092*7c478bd9Sstevel@tonic-gate }
1093*7c478bd9Sstevel@tonic-gate 
1094*7c478bd9Sstevel@tonic-gate /*
1095*7c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
1096*7c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
1097*7c478bd9Sstevel@tonic-gate  * the packet has been sent.
1098*7c478bd9Sstevel@tonic-gate  */
1099*7c478bd9Sstevel@tonic-gate static void
1100*7c478bd9Sstevel@tonic-gate ire_send(queue_t *q, mblk_t *pkt, ire_t *ire)
1101*7c478bd9Sstevel@tonic-gate {
1102*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1103*7c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
1104*7c478bd9Sstevel@tonic-gate 	boolean_t is_secure;
1105*7c478bd9Sstevel@tonic-gate 	uint_t ifindex;
1106*7c478bd9Sstevel@tonic-gate 	ill_t	*ill;
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1109*7c478bd9Sstevel@tonic-gate 	ipsec_mp = pkt;
1110*7c478bd9Sstevel@tonic-gate 	is_secure = (pkt->b_datap->db_type == M_CTL);
1111*7c478bd9Sstevel@tonic-gate 	if (is_secure)
1112*7c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
1115*7c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
1116*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
1117*7c478bd9Sstevel@tonic-gate 		/*
1118*7c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_noire).
1119*7c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
1120*7c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from ARP)
1121*7c478bd9Sstevel@tonic-gate 		 */
1122*7c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
1123*7c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
1124*7c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
1125*7c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
1126*7c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
1127*7c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
1128*7c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
1129*7c478bd9Sstevel@tonic-gate 			return;
1130*7c478bd9Sstevel@tonic-gate 		}
1131*7c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
1132*7c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
1133*7c478bd9Sstevel@tonic-gate 		mp = allocb(0, BPRI_HI);
1134*7c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1135*7c478bd9Sstevel@tonic-gate 			ill_refrele(ill);
1136*7c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
1137*7c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
1138*7c478bd9Sstevel@tonic-gate 			return;
1139*7c478bd9Sstevel@tonic-gate 		}
1140*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_BREAK;
1141*7c478bd9Sstevel@tonic-gate 		/*
1142*7c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
1143*7c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
1144*7c478bd9Sstevel@tonic-gate 		 * prepended.
1145*7c478bd9Sstevel@tonic-gate 		 */
1146*7c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
1147*7c478bd9Sstevel@tonic-gate 		mp->b_cont = ipsec_mp;
1148*7c478bd9Sstevel@tonic-gate 		put(q, mp);
1149*7c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
1150*7c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
1151*7c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
1152*7c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
1153*7c478bd9Sstevel@tonic-gate 		/*
1154*7c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
1155*7c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
1156*7c478bd9Sstevel@tonic-gate 		 */
1157*7c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
1158*7c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL);
1159*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
1160*7c478bd9Sstevel@tonic-gate 	} else {
1161*7c478bd9Sstevel@tonic-gate 		/* Locally originated packets */
1162*7c478bd9Sstevel@tonic-gate 		boolean_t is_inaddr_any;
1163*7c478bd9Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)pkt->b_rptr;
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 		/*
1166*7c478bd9Sstevel@tonic-gate 		 * We need to do an ire_delete below for which
1167*7c478bd9Sstevel@tonic-gate 		 * we need to make sure that the IRE will be
1168*7c478bd9Sstevel@tonic-gate 		 * around even after calling ip_wput_ire -
1169*7c478bd9Sstevel@tonic-gate 		 * which does ire_refrele. Otherwise somebody
1170*7c478bd9Sstevel@tonic-gate 		 * could potentially delete this ire and hence
1171*7c478bd9Sstevel@tonic-gate 		 * free this ire and we will be calling ire_delete
1172*7c478bd9Sstevel@tonic-gate 		 * on a freed ire below.
1173*7c478bd9Sstevel@tonic-gate 		 */
1174*7c478bd9Sstevel@tonic-gate 		is_inaddr_any = (ire->ire_src_addr == INADDR_ANY);
1175*7c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
1176*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
1177*7c478bd9Sstevel@tonic-gate 		}
1178*7c478bd9Sstevel@tonic-gate 		/*
1179*7c478bd9Sstevel@tonic-gate 		 * If we were resolving a router we can not use the
1180*7c478bd9Sstevel@tonic-gate 		 * routers IRE for sending the packet (since it would
1181*7c478bd9Sstevel@tonic-gate 		 * violate the uniqness of the IP idents) thus we
1182*7c478bd9Sstevel@tonic-gate 		 * make another pass through ip_wput to create the IRE_CACHE
1183*7c478bd9Sstevel@tonic-gate 		 * for the destination.
1184*7c478bd9Sstevel@tonic-gate 		 * When IRE_MARK_NOADD is set, ire_add() is not called.
1185*7c478bd9Sstevel@tonic-gate 		 * Thus ip_wput() will never find a ire and result in an
1186*7c478bd9Sstevel@tonic-gate 		 * infinite loop. Thus we check whether IRE_MARK_NOADD is
1187*7c478bd9Sstevel@tonic-gate 		 * is set. This also implies that IRE_MARK_NOADD can only be
1188*7c478bd9Sstevel@tonic-gate 		 * used to send packets to directly connected hosts.
1189*7c478bd9Sstevel@tonic-gate 		 */
1190*7c478bd9Sstevel@tonic-gate 		if (ipha->ipha_dst != ire->ire_addr &&
1191*7c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_NOADD)) {
1192*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
1193*7c478bd9Sstevel@tonic-gate 			(void) ip_output(Q_TO_CONN(q), ipsec_mp, q, IRE_SEND);
1194*7c478bd9Sstevel@tonic-gate 		} else {
1195*7c478bd9Sstevel@tonic-gate 			if (is_secure) {
1196*7c478bd9Sstevel@tonic-gate 				ipsec_out_t *oi;
1197*7c478bd9Sstevel@tonic-gate 				ipha_t *ipha;
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate 				oi = (ipsec_out_t *)ipsec_mp->b_rptr;
1200*7c478bd9Sstevel@tonic-gate 				ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
1201*7c478bd9Sstevel@tonic-gate 				if (oi->ipsec_out_proc_begin) {
1202*7c478bd9Sstevel@tonic-gate 					/*
1203*7c478bd9Sstevel@tonic-gate 					 * This is the case where
1204*7c478bd9Sstevel@tonic-gate 					 * ip_wput_ipsec_out could not find
1205*7c478bd9Sstevel@tonic-gate 					 * the IRE and recreated a new one.
1206*7c478bd9Sstevel@tonic-gate 					 * As ip_wput_ipsec_out does ire
1207*7c478bd9Sstevel@tonic-gate 					 * lookups, ire_refrele for the extra
1208*7c478bd9Sstevel@tonic-gate 					 * bump in ire_add.
1209*7c478bd9Sstevel@tonic-gate 					 */
1210*7c478bd9Sstevel@tonic-gate 					ire_refrele(ire);
1211*7c478bd9Sstevel@tonic-gate 					ip_wput_ipsec_out(q, ipsec_mp, ipha,
1212*7c478bd9Sstevel@tonic-gate 					    NULL, NULL);
1213*7c478bd9Sstevel@tonic-gate 				} else {
1214*7c478bd9Sstevel@tonic-gate 					/*
1215*7c478bd9Sstevel@tonic-gate 					 * IRE_REFRELE will be done in
1216*7c478bd9Sstevel@tonic-gate 					 * ip_wput_ire.
1217*7c478bd9Sstevel@tonic-gate 					 */
1218*7c478bd9Sstevel@tonic-gate 					ip_wput_ire(q, ipsec_mp, ire, NULL,
1219*7c478bd9Sstevel@tonic-gate 					    IRE_SEND);
1220*7c478bd9Sstevel@tonic-gate 				}
1221*7c478bd9Sstevel@tonic-gate 			} else {
1222*7c478bd9Sstevel@tonic-gate 				/*
1223*7c478bd9Sstevel@tonic-gate 				 * IRE_REFRELE will be done in ip_wput_ire.
1224*7c478bd9Sstevel@tonic-gate 				 */
1225*7c478bd9Sstevel@tonic-gate 				ip_wput_ire(q, ipsec_mp, ire, NULL,
1226*7c478bd9Sstevel@tonic-gate 				    IRE_SEND);
1227*7c478bd9Sstevel@tonic-gate 			}
1228*7c478bd9Sstevel@tonic-gate 		}
1229*7c478bd9Sstevel@tonic-gate 		/*
1230*7c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
1231*7c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
1232*7c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
1233*7c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
1234*7c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
1235*7c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
1236*7c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
1237*7c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected.
1238*7c478bd9Sstevel@tonic-gate 		 * Though, this does not completely prevent other threads
1239*7c478bd9Sstevel@tonic-gate 		 * from using this ire, this should not cause any problems.
1240*7c478bd9Sstevel@tonic-gate 		 *
1241*7c478bd9Sstevel@tonic-gate 		 * NOTE : We use is_inaddr_any instead of using ire_src_addr
1242*7c478bd9Sstevel@tonic-gate 		 * because for the normal case i.e !is_inaddr_any, ire_refrele
1243*7c478bd9Sstevel@tonic-gate 		 * above could have potentially freed the ire.
1244*7c478bd9Sstevel@tonic-gate 		 */
1245*7c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
1246*7c478bd9Sstevel@tonic-gate 			/*
1247*7c478bd9Sstevel@tonic-gate 			 * If this IRE has been deleted by another thread, then
1248*7c478bd9Sstevel@tonic-gate 			 * ire_bucket won't be NULL, but ire_ptpn will be NULL.
1249*7c478bd9Sstevel@tonic-gate 			 * Thus, ire_delete will do nothing.  This check
1250*7c478bd9Sstevel@tonic-gate 			 * guards against calling ire_delete when the IRE was
1251*7c478bd9Sstevel@tonic-gate 			 * never inserted in the table, which is handled by
1252*7c478bd9Sstevel@tonic-gate 			 * ire_delete as dropping another reference.
1253*7c478bd9Sstevel@tonic-gate 			 */
1254*7c478bd9Sstevel@tonic-gate 			if (ire->ire_bucket != NULL) {
1255*7c478bd9Sstevel@tonic-gate 				ip1dbg(("ire_send: delete IRE\n"));
1256*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
1257*7c478bd9Sstevel@tonic-gate 			}
1258*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held above */
1259*7c478bd9Sstevel@tonic-gate 		}
1260*7c478bd9Sstevel@tonic-gate 	}
1261*7c478bd9Sstevel@tonic-gate }
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate /*
1264*7c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
1265*7c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
1266*7c478bd9Sstevel@tonic-gate  * the packet has been sent.
1267*7c478bd9Sstevel@tonic-gate  */
1268*7c478bd9Sstevel@tonic-gate static void
1269*7c478bd9Sstevel@tonic-gate ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire)
1270*7c478bd9Sstevel@tonic-gate {
1271*7c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
1272*7c478bd9Sstevel@tonic-gate 	boolean_t secure;
1273*7c478bd9Sstevel@tonic-gate 	uint_t ifindex;
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
1276*7c478bd9Sstevel@tonic-gate 	if (pkt->b_datap->db_type == M_CTL) {
1277*7c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
1278*7c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
1279*7c478bd9Sstevel@tonic-gate 		secure = B_TRUE;
1280*7c478bd9Sstevel@tonic-gate 	} else {
1281*7c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
1282*7c478bd9Sstevel@tonic-gate 		secure = B_FALSE;
1283*7c478bd9Sstevel@tonic-gate 	}
1284*7c478bd9Sstevel@tonic-gate 
1285*7c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
1286*7c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
1287*7c478bd9Sstevel@tonic-gate 		ill_t	*ill;
1288*7c478bd9Sstevel@tonic-gate 		/*
1289*7c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_data_v6).
1290*7c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
1291*7c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from the
1292*7c478bd9Sstevel@tonic-gate 		 * resolver). Unlike IPv4 there is no need for a prepended
1293*7c478bd9Sstevel@tonic-gate 		 * M_BREAK since ip_rput_data_v6 does not process options
1294*7c478bd9Sstevel@tonic-gate 		 * before finding an IRE.
1295*7c478bd9Sstevel@tonic-gate 		 */
1296*7c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
1297*7c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
1298*7c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
1299*7c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
1300*7c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
1301*7c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
1302*7c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
1303*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
1304*7c478bd9Sstevel@tonic-gate 			return;
1305*7c478bd9Sstevel@tonic-gate 		}
1306*7c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
1307*7c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
1308*7c478bd9Sstevel@tonic-gate 		/*
1309*7c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
1310*7c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
1311*7c478bd9Sstevel@tonic-gate 		 * prepended.
1312*7c478bd9Sstevel@tonic-gate 		 */
1313*7c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
1314*7c478bd9Sstevel@tonic-gate 		put(q, pkt);
1315*7c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
1316*7c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
1317*7c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
1318*7c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
1319*7c478bd9Sstevel@tonic-gate 		/*
1320*7c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
1321*7c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
1322*7c478bd9Sstevel@tonic-gate 		 */
1323*7c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
1324*7c478bd9Sstevel@tonic-gate 		/*
1325*7c478bd9Sstevel@tonic-gate 		 * XXX TODO IPv6.
1326*7c478bd9Sstevel@tonic-gate 		 */
1327*7c478bd9Sstevel@tonic-gate 		freemsg(pkt);
1328*7c478bd9Sstevel@tonic-gate #ifdef XXX
1329*7c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL);
1330*7c478bd9Sstevel@tonic-gate #endif
1331*7c478bd9Sstevel@tonic-gate 	} else {
1332*7c478bd9Sstevel@tonic-gate 		if (secure) {
1333*7c478bd9Sstevel@tonic-gate 			ipsec_out_t *oi;
1334*7c478bd9Sstevel@tonic-gate 			ip6_t *ip6h;
1335*7c478bd9Sstevel@tonic-gate 
1336*7c478bd9Sstevel@tonic-gate 			oi = (ipsec_out_t *)ipsec_mp->b_rptr;
1337*7c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr;
1338*7c478bd9Sstevel@tonic-gate 			if (oi->ipsec_out_proc_begin) {
1339*7c478bd9Sstevel@tonic-gate 				/*
1340*7c478bd9Sstevel@tonic-gate 				 * This is the case where
1341*7c478bd9Sstevel@tonic-gate 				 * ip_wput_ipsec_out could not find
1342*7c478bd9Sstevel@tonic-gate 				 * the IRE and recreated a new one.
1343*7c478bd9Sstevel@tonic-gate 				 */
1344*7c478bd9Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h,
1345*7c478bd9Sstevel@tonic-gate 				    NULL, NULL);
1346*7c478bd9Sstevel@tonic-gate 			} else {
1347*7c478bd9Sstevel@tonic-gate 				(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp,
1348*7c478bd9Sstevel@tonic-gate 				    q, IRE_SEND);
1349*7c478bd9Sstevel@tonic-gate 			}
1350*7c478bd9Sstevel@tonic-gate 		} else {
1351*7c478bd9Sstevel@tonic-gate 			/*
1352*7c478bd9Sstevel@tonic-gate 			 * Send packets through ip_output_v6 so that any
1353*7c478bd9Sstevel@tonic-gate 			 * ip6_info header can be processed again.
1354*7c478bd9Sstevel@tonic-gate 			 */
1355*7c478bd9Sstevel@tonic-gate 			(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q,
1356*7c478bd9Sstevel@tonic-gate 			    IRE_SEND);
1357*7c478bd9Sstevel@tonic-gate 		}
1358*7c478bd9Sstevel@tonic-gate 		/*
1359*7c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
1360*7c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
1361*7c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
1362*7c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
1363*7c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
1364*7c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
1365*7c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
1366*7c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append_v6, ip_ire_req and
1367*7c478bd9Sstevel@tonic-gate 		 * ip_bind_connected_v6. Though, this does not completely
1368*7c478bd9Sstevel@tonic-gate 		 * prevent other threads from using this ire, this should
1369*7c478bd9Sstevel@tonic-gate 		 * not cause any problems.
1370*7c478bd9Sstevel@tonic-gate 		 */
1371*7c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) {
1372*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_send_v6: delete IRE\n"));
1373*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
1374*7c478bd9Sstevel@tonic-gate 		}
1375*7c478bd9Sstevel@tonic-gate 	}
1376*7c478bd9Sstevel@tonic-gate 	ire_refrele(ire);	/* Held in ire_add */
1377*7c478bd9Sstevel@tonic-gate }
1378*7c478bd9Sstevel@tonic-gate 
1379*7c478bd9Sstevel@tonic-gate /*
1380*7c478bd9Sstevel@tonic-gate  * Make sure that IRE bucket does not get too long.
1381*7c478bd9Sstevel@tonic-gate  * This can cause lock up because ire_cache_lookup()
1382*7c478bd9Sstevel@tonic-gate  * may take "forever" to finish.
1383*7c478bd9Sstevel@tonic-gate  *
1384*7c478bd9Sstevel@tonic-gate  * We just remove cnt IREs each time.  This means that
1385*7c478bd9Sstevel@tonic-gate  * the bucket length will stay approximately constant,
1386*7c478bd9Sstevel@tonic-gate  * depending on cnt.  This should be enough to defend
1387*7c478bd9Sstevel@tonic-gate  * against DoS attack based on creating temporary IREs
1388*7c478bd9Sstevel@tonic-gate  * (for forwarding and non-TCP traffic).
1389*7c478bd9Sstevel@tonic-gate  *
1390*7c478bd9Sstevel@tonic-gate  * Note that new IRE is normally added at the tail of the
1391*7c478bd9Sstevel@tonic-gate  * bucket.  This means that we are removing the "oldest"
1392*7c478bd9Sstevel@tonic-gate  * temporary IRE added.  Only if there are IREs with
1393*7c478bd9Sstevel@tonic-gate  * the same ire_addr, do we not add it at the tail.  Refer
1394*7c478bd9Sstevel@tonic-gate  * to ire_add_v*().  It should be OK for our purpose.
1395*7c478bd9Sstevel@tonic-gate  *
1396*7c478bd9Sstevel@tonic-gate  * For non-temporary cached IREs, we make sure that they
1397*7c478bd9Sstevel@tonic-gate  * have not been used for some time (defined below), they
1398*7c478bd9Sstevel@tonic-gate  * are non-local destinations, and there is no one using
1399*7c478bd9Sstevel@tonic-gate  * them at the moment (refcnt == 1).
1400*7c478bd9Sstevel@tonic-gate  *
1401*7c478bd9Sstevel@tonic-gate  * The above means that the IRE bucket length may become
1402*7c478bd9Sstevel@tonic-gate  * very long, consisting of mostly non-temporary IREs.
1403*7c478bd9Sstevel@tonic-gate  * This can happen when the hash function does a bad job
1404*7c478bd9Sstevel@tonic-gate  * so that most TCP connections cluster to a specific bucket.
1405*7c478bd9Sstevel@tonic-gate  * This "hopefully" should never happen.  It can also
1406*7c478bd9Sstevel@tonic-gate  * happen if most TCP connections have very long lives.
1407*7c478bd9Sstevel@tonic-gate  * Even with the minimal hash table size of 256, there
1408*7c478bd9Sstevel@tonic-gate  * has to be a lot of such connections to make the bucket
1409*7c478bd9Sstevel@tonic-gate  * length unreasonably long.  This should probably not
1410*7c478bd9Sstevel@tonic-gate  * happen either.  The third can when this can happen is
1411*7c478bd9Sstevel@tonic-gate  * when the machine is under attack, such as SYN flooding.
1412*7c478bd9Sstevel@tonic-gate  * TCP should already have the proper mechanism to protect
1413*7c478bd9Sstevel@tonic-gate  * that.  So we should be safe.
1414*7c478bd9Sstevel@tonic-gate  *
1415*7c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send() after
1416*7c478bd9Sstevel@tonic-gate  * a new IRE is added and the packet is sent.
1417*7c478bd9Sstevel@tonic-gate  *
1418*7c478bd9Sstevel@tonic-gate  * The idle cutoff interval is set to 60s.  It can be
1419*7c478bd9Sstevel@tonic-gate  * changed using /etc/system.
1420*7c478bd9Sstevel@tonic-gate  */
1421*7c478bd9Sstevel@tonic-gate uint32_t ire_idle_cutoff_interval = 60000;
1422*7c478bd9Sstevel@tonic-gate 
1423*7c478bd9Sstevel@tonic-gate static void
1424*7c478bd9Sstevel@tonic-gate ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt)
1425*7c478bd9Sstevel@tonic-gate {
1426*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
1427*7c478bd9Sstevel@tonic-gate 	int tmp_cnt = cnt;
1428*7c478bd9Sstevel@tonic-gate 	clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000);
1429*7c478bd9Sstevel@tonic-gate 
1430*7c478bd9Sstevel@tonic-gate 	/*
1431*7c478bd9Sstevel@tonic-gate 	 * irb is NULL if the IRE is not added to the hash.  This
1432*7c478bd9Sstevel@tonic-gate 	 * happens when IRE_MARK_NOADD is set in ire_add_then_send()
1433*7c478bd9Sstevel@tonic-gate 	 * and when ires are returned from ire_update_srcif_v4() routine.
1434*7c478bd9Sstevel@tonic-gate 	 */
1435*7c478bd9Sstevel@tonic-gate 	if (irb == NULL)
1436*7c478bd9Sstevel@tonic-gate 		return;
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
1439*7c478bd9Sstevel@tonic-gate 	if (irb->irb_tmp_ire_cnt > threshold) {
1440*7c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && tmp_cnt > 0;
1441*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
1442*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
1443*7c478bd9Sstevel@tonic-gate 				continue;
1444*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY) {
1445*7c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_type == IRE_CACHE);
1446*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
1447*7c478bd9Sstevel@tonic-gate 				tmp_cnt--;
1448*7c478bd9Sstevel@tonic-gate 			}
1449*7c478bd9Sstevel@tonic-gate 		}
1450*7c478bd9Sstevel@tonic-gate 	}
1451*7c478bd9Sstevel@tonic-gate 	if (irb->irb_ire_cnt - irb->irb_tmp_ire_cnt > threshold) {
1452*7c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && cnt > 0;
1453*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
1454*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED ||
1455*7c478bd9Sstevel@tonic-gate 			    ire->ire_gateway_addr == 0) {
1456*7c478bd9Sstevel@tonic-gate 				continue;
1457*7c478bd9Sstevel@tonic-gate 			}
1458*7c478bd9Sstevel@tonic-gate 			if ((ire->ire_type == IRE_CACHE) &&
1459*7c478bd9Sstevel@tonic-gate 			    (lbolt - ire->ire_last_used_time > cut_off) &&
1460*7c478bd9Sstevel@tonic-gate 			    (ire->ire_refcnt == 1)) {
1461*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
1462*7c478bd9Sstevel@tonic-gate 				cnt--;
1463*7c478bd9Sstevel@tonic-gate 			}
1464*7c478bd9Sstevel@tonic-gate 		}
1465*7c478bd9Sstevel@tonic-gate 	}
1466*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
1467*7c478bd9Sstevel@tonic-gate }
1468*7c478bd9Sstevel@tonic-gate 
1469*7c478bd9Sstevel@tonic-gate /*
1470*7c478bd9Sstevel@tonic-gate  * ire_add_then_send is called when a new IRE has been created in order to
1471*7c478bd9Sstevel@tonic-gate  * route an outgoing packet.  Typically, it is called from ip_wput when
1472*7c478bd9Sstevel@tonic-gate  * a response comes back down from a resolver.  We add the IRE, and then
1473*7c478bd9Sstevel@tonic-gate  * possibly run the packet through ip_wput or ip_rput, as appropriate.
1474*7c478bd9Sstevel@tonic-gate  * However, we do not add the newly created IRE in the cache when
1475*7c478bd9Sstevel@tonic-gate  * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at
1476*7c478bd9Sstevel@tonic-gate  * ip_newroute_ipif(). The ires with IRE_MARK_NOADD and ires returned
1477*7c478bd9Sstevel@tonic-gate  * by ire_update_srcif_v4() are ire_refrele'd by ip_wput_ire() and get
1478*7c478bd9Sstevel@tonic-gate  * deleted.
1479*7c478bd9Sstevel@tonic-gate  * Multirouting support: the packet is silently discarded when the new IRE
1480*7c478bd9Sstevel@tonic-gate  * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the
1481*7c478bd9Sstevel@tonic-gate  * RTF_MULTIRT flag for the same destination address.
1482*7c478bd9Sstevel@tonic-gate  * In this case, we just want to register this additional ire without
1483*7c478bd9Sstevel@tonic-gate  * sending the packet, as it has already been replicated through
1484*7c478bd9Sstevel@tonic-gate  * existing multirt routes in ip_wput().
1485*7c478bd9Sstevel@tonic-gate  */
1486*7c478bd9Sstevel@tonic-gate void
1487*7c478bd9Sstevel@tonic-gate ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp)
1488*7c478bd9Sstevel@tonic-gate {
1489*7c478bd9Sstevel@tonic-gate 	irb_t *irb;
1490*7c478bd9Sstevel@tonic-gate 	boolean_t drop = B_FALSE;
1491*7c478bd9Sstevel@tonic-gate 	/* LINTED : set but not used in function */
1492*7c478bd9Sstevel@tonic-gate 	boolean_t mctl_present;
1493*7c478bd9Sstevel@tonic-gate 	mblk_t *first_mp = NULL;
1494*7c478bd9Sstevel@tonic-gate 	mblk_t *save_mp = NULL;
1495*7c478bd9Sstevel@tonic-gate 	ire_t *dst_ire;
1496*7c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
1497*7c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
1498*7c478bd9Sstevel@tonic-gate 
1499*7c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
1500*7c478bd9Sstevel@tonic-gate 		/*
1501*7c478bd9Sstevel@tonic-gate 		 * We first have to retrieve the destination address carried
1502*7c478bd9Sstevel@tonic-gate 		 * by the packet.
1503*7c478bd9Sstevel@tonic-gate 		 * We can't rely on ire as it can be related to a gateway.
1504*7c478bd9Sstevel@tonic-gate 		 * The destination address will help in determining if
1505*7c478bd9Sstevel@tonic-gate 		 * other RTF_MULTIRT ires are already registered.
1506*7c478bd9Sstevel@tonic-gate 		 *
1507*7c478bd9Sstevel@tonic-gate 		 * We first need to know where we are going : v4 or V6.
1508*7c478bd9Sstevel@tonic-gate 		 * the ire version is enough, as there is no risk that
1509*7c478bd9Sstevel@tonic-gate 		 * we resolve an IPv6 address with an IPv4 ire
1510*7c478bd9Sstevel@tonic-gate 		 * or vice versa.
1511*7c478bd9Sstevel@tonic-gate 		 */
1512*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
1513*7c478bd9Sstevel@tonic-gate 			EXTRACT_PKT_MP(mp, first_mp, mctl_present);
1514*7c478bd9Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
1515*7c478bd9Sstevel@tonic-gate 			save_mp = mp;
1516*7c478bd9Sstevel@tonic-gate 			mp = first_mp;
1517*7c478bd9Sstevel@tonic-gate 
1518*7c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup(ipha->ipha_dst,
1519*7c478bd9Sstevel@tonic-gate 			    ire->ire_zoneid);
1520*7c478bd9Sstevel@tonic-gate 		} else {
1521*7c478bd9Sstevel@tonic-gate 			/*
1522*7c478bd9Sstevel@tonic-gate 			 * Get a pointer to the beginning of the IPv6 header.
1523*7c478bd9Sstevel@tonic-gate 			 * Ignore leading IPsec control mblks.
1524*7c478bd9Sstevel@tonic-gate 			 */
1525*7c478bd9Sstevel@tonic-gate 			first_mp = mp;
1526*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_CTL) {
1527*7c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
1528*7c478bd9Sstevel@tonic-gate 			}
1529*7c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)mp->b_rptr;
1530*7c478bd9Sstevel@tonic-gate 			save_mp = mp;
1531*7c478bd9Sstevel@tonic-gate 			mp = first_mp;
1532*7c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst,
1533*7c478bd9Sstevel@tonic-gate 			    ire->ire_zoneid);
1534*7c478bd9Sstevel@tonic-gate 		}
1535*7c478bd9Sstevel@tonic-gate 		if (dst_ire != NULL) {
1536*7c478bd9Sstevel@tonic-gate 			if (dst_ire->ire_flags & RTF_MULTIRT) {
1537*7c478bd9Sstevel@tonic-gate 				/*
1538*7c478bd9Sstevel@tonic-gate 				 * At least one resolved multirt route
1539*7c478bd9Sstevel@tonic-gate 				 * already exists for the destination,
1540*7c478bd9Sstevel@tonic-gate 				 * don't sent this packet: either drop it
1541*7c478bd9Sstevel@tonic-gate 				 * or complete the pending resolution,
1542*7c478bd9Sstevel@tonic-gate 				 * depending on the ire.
1543*7c478bd9Sstevel@tonic-gate 				 */
1544*7c478bd9Sstevel@tonic-gate 				drop = B_TRUE;
1545*7c478bd9Sstevel@tonic-gate 			}
1546*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_then_send: dst_ire %p "
1547*7c478bd9Sstevel@tonic-gate 			    "[dst %08x, gw %08x], drop %d\n",
1548*7c478bd9Sstevel@tonic-gate 			    (void *)dst_ire,
1549*7c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
1550*7c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_addr) : \
1551*7c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)),
1552*7c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
1553*7c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_gateway_addr) : \
1554*7c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(
1555*7c478bd9Sstevel@tonic-gate 				    dst_ire->ire_gateway_addr_v6)),
1556*7c478bd9Sstevel@tonic-gate 			    drop));
1557*7c478bd9Sstevel@tonic-gate 			ire_refrele(dst_ire);
1558*7c478bd9Sstevel@tonic-gate 		}
1559*7c478bd9Sstevel@tonic-gate 	}
1560*7c478bd9Sstevel@tonic-gate 
1561*7c478bd9Sstevel@tonic-gate 	if (!(ire->ire_marks & IRE_MARK_NOADD)) {
1562*7c478bd9Sstevel@tonic-gate 		/*
1563*7c478bd9Sstevel@tonic-gate 		 * Regular packets with cache bound ires and
1564*7c478bd9Sstevel@tonic-gate 		 * the packets from ARP response for ires which
1565*7c478bd9Sstevel@tonic-gate 		 * belong to the ire_srcif_v4 table, are here.
1566*7c478bd9Sstevel@tonic-gate 		 */
1567*7c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL) {
1568*7c478bd9Sstevel@tonic-gate 			/* Add the ire */
1569*7c478bd9Sstevel@tonic-gate 			(void) ire_add(&ire, NULL, NULL, NULL);
1570*7c478bd9Sstevel@tonic-gate 		} else {
1571*7c478bd9Sstevel@tonic-gate 			/*
1572*7c478bd9Sstevel@tonic-gate 			 * This must be ARP response for ire in interface based
1573*7c478bd9Sstevel@tonic-gate 			 * table. Note that we don't add them in cache table,
1574*7c478bd9Sstevel@tonic-gate 			 * instead we update the existing table with dlureq_mp
1575*7c478bd9Sstevel@tonic-gate 			 * information. The reverse tunnel ires do not come
1576*7c478bd9Sstevel@tonic-gate 			 * here, as reverse tunnel is non-resolver interface.
1577*7c478bd9Sstevel@tonic-gate 			 * XXX- another design alternative was to mark the
1578*7c478bd9Sstevel@tonic-gate 			 * ires in interface based table with a special mark to
1579*7c478bd9Sstevel@tonic-gate 			 * make absolutely sure that we operate in right ires.
1580*7c478bd9Sstevel@tonic-gate 			 * This idea was not implemented as part of code review
1581*7c478bd9Sstevel@tonic-gate 			 * suggestion, as ire_in_ill suffice to distinguish
1582*7c478bd9Sstevel@tonic-gate 			 * between the regular ires and interface based
1583*7c478bd9Sstevel@tonic-gate 			 * ires now and thus we save a bit in the ire_marks.
1584*7c478bd9Sstevel@tonic-gate 			 */
1585*7c478bd9Sstevel@tonic-gate 			ire = ire_update_srcif_v4(ire);
1586*7c478bd9Sstevel@tonic-gate 		}
1587*7c478bd9Sstevel@tonic-gate 
1588*7c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
1589*7c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
1590*7c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
1591*7c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
1592*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1593*7c478bd9Sstevel@tonic-gate 			return;
1594*7c478bd9Sstevel@tonic-gate 		}
1595*7c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1596*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add_v4/v6 */
1597*7c478bd9Sstevel@tonic-gate 			return;
1598*7c478bd9Sstevel@tonic-gate 		}
1599*7c478bd9Sstevel@tonic-gate 	}
1600*7c478bd9Sstevel@tonic-gate 	if (drop) {
1601*7c478bd9Sstevel@tonic-gate 		/*
1602*7c478bd9Sstevel@tonic-gate 		 * If we're adding an RTF_MULTIRT ire, the resolution
1603*7c478bd9Sstevel@tonic-gate 		 * is over: we just drop the packet.
1604*7c478bd9Sstevel@tonic-gate 		 */
1605*7c478bd9Sstevel@tonic-gate 		if (ire->ire_flags & RTF_MULTIRT) {
1606*7c478bd9Sstevel@tonic-gate 			if (save_mp) {
1607*7c478bd9Sstevel@tonic-gate 				save_mp->b_prev = NULL;
1608*7c478bd9Sstevel@tonic-gate 				save_mp->b_next = NULL;
1609*7c478bd9Sstevel@tonic-gate 			}
1610*7c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
1611*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1612*7c478bd9Sstevel@tonic-gate 		} else {
1613*7c478bd9Sstevel@tonic-gate 			/*
1614*7c478bd9Sstevel@tonic-gate 			 * Otherwise, we're adding the ire to a gateway
1615*7c478bd9Sstevel@tonic-gate 			 * for a multirt route.
1616*7c478bd9Sstevel@tonic-gate 			 * Invoke ip_newroute() to complete the resolution
1617*7c478bd9Sstevel@tonic-gate 			 * of the route. We will then come back here and
1618*7c478bd9Sstevel@tonic-gate 			 * finally drop this packet in the above code.
1619*7c478bd9Sstevel@tonic-gate 			 */
1620*7c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
1621*7c478bd9Sstevel@tonic-gate 				/*
1622*7c478bd9Sstevel@tonic-gate 				 * TODO: in order for CGTP to work in non-global
1623*7c478bd9Sstevel@tonic-gate 				 * zones, ip_newroute() must create the IRE
1624*7c478bd9Sstevel@tonic-gate 				 * cache in the zone indicated by
1625*7c478bd9Sstevel@tonic-gate 				 * ire->ire_zoneid.
1626*7c478bd9Sstevel@tonic-gate 				 */
1627*7c478bd9Sstevel@tonic-gate 				ip_newroute(q, mp, ipha->ipha_dst, 0,
1628*7c478bd9Sstevel@tonic-gate 				    (CONN_Q(q) ? Q_TO_CONN(q) : NULL));
1629*7c478bd9Sstevel@tonic-gate 			} else {
1630*7c478bd9Sstevel@tonic-gate 				ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL,
1631*7c478bd9Sstevel@tonic-gate 				    NULL, ire->ire_zoneid);
1632*7c478bd9Sstevel@tonic-gate 			}
1633*7c478bd9Sstevel@tonic-gate 		}
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire); /* As done by ire_send(). */
1636*7c478bd9Sstevel@tonic-gate 		return;
1637*7c478bd9Sstevel@tonic-gate 	}
1638*7c478bd9Sstevel@tonic-gate 	/*
1639*7c478bd9Sstevel@tonic-gate 	 * Need to remember ire_bucket here as ire_send*() may delete
1640*7c478bd9Sstevel@tonic-gate 	 * the ire so we cannot reference it after that.
1641*7c478bd9Sstevel@tonic-gate 	 */
1642*7c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
1643*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
1644*7c478bd9Sstevel@tonic-gate 		ire_send_v6(q, mp, ire);
1645*7c478bd9Sstevel@tonic-gate 		/*
1646*7c478bd9Sstevel@tonic-gate 		 * Clean up more than 1 IRE so that the clean up does not
1647*7c478bd9Sstevel@tonic-gate 		 * need to be done every time when a new IRE is added and
1648*7c478bd9Sstevel@tonic-gate 		 * the threshold is reached.
1649*7c478bd9Sstevel@tonic-gate 		 */
1650*7c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip6_ire_max_bucket_cnt, 2);
1651*7c478bd9Sstevel@tonic-gate 	} else {
1652*7c478bd9Sstevel@tonic-gate 		ire_send(q, mp, ire);
1653*7c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip_ire_max_bucket_cnt, 2);
1654*7c478bd9Sstevel@tonic-gate 	}
1655*7c478bd9Sstevel@tonic-gate }
1656*7c478bd9Sstevel@tonic-gate 
1657*7c478bd9Sstevel@tonic-gate /*
1658*7c478bd9Sstevel@tonic-gate  * Initialize the ire that is specific to IPv4 part and call
1659*7c478bd9Sstevel@tonic-gate  * ire_init_common to finish it.
1660*7c478bd9Sstevel@tonic-gate  */
1661*7c478bd9Sstevel@tonic-gate ire_t *
1662*7c478bd9Sstevel@tonic-gate ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr,
1663*7c478bd9Sstevel@tonic-gate     uchar_t *gateway, uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp,
1664*7c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif,
1665*7c478bd9Sstevel@tonic-gate     ill_t *in_ill, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle,
1666*7c478bd9Sstevel@tonic-gate     uint32_t flags, const iulp_t *ulp_info)
1667*7c478bd9Sstevel@tonic-gate {
1668*7c478bd9Sstevel@tonic-gate 	if (fp_mp != NULL) {
1669*7c478bd9Sstevel@tonic-gate 		/*
1670*7c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
1671*7c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
1672*7c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
1673*7c478bd9Sstevel@tonic-gate 		 */
1674*7c478bd9Sstevel@tonic-gate 		fp_mp = copyb(fp_mp);
1675*7c478bd9Sstevel@tonic-gate 		if (fp_mp == NULL)
1676*7c478bd9Sstevel@tonic-gate 			return (NULL);
1677*7c478bd9Sstevel@tonic-gate 	}
1678*7c478bd9Sstevel@tonic-gate 
1679*7c478bd9Sstevel@tonic-gate 	if (dlureq_mp != NULL) {
1680*7c478bd9Sstevel@tonic-gate 		/*
1681*7c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
1682*7c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
1683*7c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
1684*7c478bd9Sstevel@tonic-gate 		 */
1685*7c478bd9Sstevel@tonic-gate 		dlureq_mp = copyb(dlureq_mp);
1686*7c478bd9Sstevel@tonic-gate 		if (dlureq_mp == NULL) {
1687*7c478bd9Sstevel@tonic-gate 			if (fp_mp != NULL)
1688*7c478bd9Sstevel@tonic-gate 				freeb(fp_mp);
1689*7c478bd9Sstevel@tonic-gate 			return (NULL);
1690*7c478bd9Sstevel@tonic-gate 		}
1691*7c478bd9Sstevel@tonic-gate 	}
1692*7c478bd9Sstevel@tonic-gate 
1693*7c478bd9Sstevel@tonic-gate 	/*
1694*7c478bd9Sstevel@tonic-gate 	 * Check that IRE_IF_RESOLVER and IRE_IF_NORESOLVER have a
1695*7c478bd9Sstevel@tonic-gate 	 * dlureq_mp which is the ill_resolver_mp for IRE_IF_RESOLVER
1696*7c478bd9Sstevel@tonic-gate 	 * and DL_UNITDATA_REQ for IRE_IF_NORESOLVER.
1697*7c478bd9Sstevel@tonic-gate 	 */
1698*7c478bd9Sstevel@tonic-gate 	if ((type & IRE_INTERFACE) &&
1699*7c478bd9Sstevel@tonic-gate 	    dlureq_mp == NULL) {
1700*7c478bd9Sstevel@tonic-gate 		ASSERT(fp_mp == NULL);
1701*7c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_init: no dlureq_mp\n"));
1702*7c478bd9Sstevel@tonic-gate 		return (NULL);
1703*7c478bd9Sstevel@tonic-gate 	}
1704*7c478bd9Sstevel@tonic-gate 
1705*7c478bd9Sstevel@tonic-gate 	BUMP_IRE_STATS(ire_stats_v4, ire_stats_alloced);
1706*7c478bd9Sstevel@tonic-gate 
1707*7c478bd9Sstevel@tonic-gate 	if (addr != NULL)
1708*7c478bd9Sstevel@tonic-gate 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
1709*7c478bd9Sstevel@tonic-gate 	if (src_addr != NULL)
1710*7c478bd9Sstevel@tonic-gate 		bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN);
1711*7c478bd9Sstevel@tonic-gate 	if (mask != NULL) {
1712*7c478bd9Sstevel@tonic-gate 		bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
1713*7c478bd9Sstevel@tonic-gate 		ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
1714*7c478bd9Sstevel@tonic-gate 	}
1715*7c478bd9Sstevel@tonic-gate 	if (gateway != NULL) {
1716*7c478bd9Sstevel@tonic-gate 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
1717*7c478bd9Sstevel@tonic-gate 	}
1718*7c478bd9Sstevel@tonic-gate 	if (in_src_addr != NULL) {
1719*7c478bd9Sstevel@tonic-gate 		bcopy(in_src_addr, &ire->ire_in_src_addr, IP_ADDR_LEN);
1720*7c478bd9Sstevel@tonic-gate 	}
1721*7c478bd9Sstevel@tonic-gate 
1722*7c478bd9Sstevel@tonic-gate 	if (type == IRE_CACHE)
1723*7c478bd9Sstevel@tonic-gate 		ire->ire_cmask = cmask;
1724*7c478bd9Sstevel@tonic-gate 
1725*7c478bd9Sstevel@tonic-gate 	ire_init_common(ire, max_fragp, fp_mp, rfq, stq, type, dlureq_mp,
1726*7c478bd9Sstevel@tonic-gate 	    ipif, in_ill, phandle, ihandle, flags, IPV4_VERSION, ulp_info);
1727*7c478bd9Sstevel@tonic-gate 
1728*7c478bd9Sstevel@tonic-gate 	return (ire);
1729*7c478bd9Sstevel@tonic-gate }
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate /*
1732*7c478bd9Sstevel@tonic-gate  * Similar to ire_create except that it is called only when
1733*7c478bd9Sstevel@tonic-gate  * we want to allocate ire as an mblk e.g. we have an external
1734*7c478bd9Sstevel@tonic-gate  * resolver ARP.
1735*7c478bd9Sstevel@tonic-gate  */
1736*7c478bd9Sstevel@tonic-gate ire_t *
1737*7c478bd9Sstevel@tonic-gate ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1738*7c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t max_frag, mblk_t *fp_mp, queue_t *rfq,
1739*7c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
1740*7c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1741*7c478bd9Sstevel@tonic-gate     const iulp_t *ulp_info)
1742*7c478bd9Sstevel@tonic-gate {
1743*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
1744*7c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
1745*7c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate 	/* Allocate the new IRE. */
1748*7c478bd9Sstevel@tonic-gate 	mp = allocb(sizeof (ire_t), BPRI_MED);
1749*7c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
1750*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create_mp: alloc failed\n"));
1751*7c478bd9Sstevel@tonic-gate 		return (NULL);
1752*7c478bd9Sstevel@tonic-gate 	}
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate 	ire = (ire_t *)mp->b_rptr;
1755*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&ire[1];
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate 	/* Start clean. */
1758*7c478bd9Sstevel@tonic-gate 	*ire = ire_null;
1759*7c478bd9Sstevel@tonic-gate 	ire->ire_mp = mp;
1760*7c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
1761*7c478bd9Sstevel@tonic-gate 
1762*7c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
1763*7c478bd9Sstevel@tonic-gate 	    NULL, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill, cmask,
1764*7c478bd9Sstevel@tonic-gate 	    phandle, ihandle, flags, ulp_info);
1765*7c478bd9Sstevel@tonic-gate 
1766*7c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
1767*7c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
1768*7c478bd9Sstevel@tonic-gate 		return (NULL);
1769*7c478bd9Sstevel@tonic-gate 	}
1770*7c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
1771*7c478bd9Sstevel@tonic-gate 	/*
1772*7c478bd9Sstevel@tonic-gate 	 * ire_max_frag is normally zero here and is atomically set
1773*7c478bd9Sstevel@tonic-gate 	 * under the irebucket lock in ire_add_v[46] except for the
1774*7c478bd9Sstevel@tonic-gate 	 * case of IRE_MARK_NOADD. In that event the the ire_max_frag
1775*7c478bd9Sstevel@tonic-gate 	 * is non-zero here.
1776*7c478bd9Sstevel@tonic-gate 	 */
1777*7c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = max_frag;
1778*7c478bd9Sstevel@tonic-gate 	return (ire);
1779*7c478bd9Sstevel@tonic-gate }
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate /*
1782*7c478bd9Sstevel@tonic-gate  * ire_create is called to allocate and initialize a new IRE.
1783*7c478bd9Sstevel@tonic-gate  *
1784*7c478bd9Sstevel@tonic-gate  * NOTE : This is called as writer sometimes though not required
1785*7c478bd9Sstevel@tonic-gate  * by this function.
1786*7c478bd9Sstevel@tonic-gate  */
1787*7c478bd9Sstevel@tonic-gate ire_t *
1788*7c478bd9Sstevel@tonic-gate ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1789*7c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp, queue_t *rfq,
1790*7c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
1791*7c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1792*7c478bd9Sstevel@tonic-gate     const iulp_t *ulp_info)
1793*7c478bd9Sstevel@tonic-gate {
1794*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
1795*7c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
1796*7c478bd9Sstevel@tonic-gate 
1797*7c478bd9Sstevel@tonic-gate 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
1798*7c478bd9Sstevel@tonic-gate 	if (ire == NULL) {
1799*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create: alloc failed\n"));
1800*7c478bd9Sstevel@tonic-gate 		return (NULL);
1801*7c478bd9Sstevel@tonic-gate 	}
1802*7c478bd9Sstevel@tonic-gate 	*ire = ire_null;
1803*7c478bd9Sstevel@tonic-gate 
1804*7c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
1805*7c478bd9Sstevel@tonic-gate 	    max_fragp, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill,  cmask,
1806*7c478bd9Sstevel@tonic-gate 	    phandle, ihandle, flags, ulp_info);
1807*7c478bd9Sstevel@tonic-gate 
1808*7c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
1809*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
1810*7c478bd9Sstevel@tonic-gate 		return (NULL);
1811*7c478bd9Sstevel@tonic-gate 	}
1812*7c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
1813*7c478bd9Sstevel@tonic-gate 	return (ire);
1814*7c478bd9Sstevel@tonic-gate }
1815*7c478bd9Sstevel@tonic-gate 
1816*7c478bd9Sstevel@tonic-gate 
1817*7c478bd9Sstevel@tonic-gate /*
1818*7c478bd9Sstevel@tonic-gate  * Common to IPv4 and IPv6
1819*7c478bd9Sstevel@tonic-gate  */
1820*7c478bd9Sstevel@tonic-gate void
1821*7c478bd9Sstevel@tonic-gate ire_init_common(ire_t *ire, uint_t *max_fragp, mblk_t *fp_mp,
1822*7c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type,
1823*7c478bd9Sstevel@tonic-gate     mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill, uint32_t phandle,
1824*7c478bd9Sstevel@tonic-gate     uint32_t ihandle, uint32_t flags, uchar_t ipversion,
1825*7c478bd9Sstevel@tonic-gate     const iulp_t *ulp_info)
1826*7c478bd9Sstevel@tonic-gate {
1827*7c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = max_fragp;
1828*7c478bd9Sstevel@tonic-gate 	ire->ire_frag_flag |= (ip_path_mtu_discovery) ? IPH_DF : 0;
1829*7c478bd9Sstevel@tonic-gate 
1830*7c478bd9Sstevel@tonic-gate 	ASSERT(fp_mp == NULL || fp_mp->b_datap->db_type == M_DATA);
1831*7c478bd9Sstevel@tonic-gate 	if (ipif) {
1832*7c478bd9Sstevel@tonic-gate 		if (ipif->ipif_isv6)
1833*7c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV6_VERSION);
1834*7c478bd9Sstevel@tonic-gate 		else
1835*7c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV4_VERSION);
1836*7c478bd9Sstevel@tonic-gate 	}
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate 	ire->ire_fp_mp = fp_mp;
1839*7c478bd9Sstevel@tonic-gate 	ire->ire_dlureq_mp = dlureq_mp;
1840*7c478bd9Sstevel@tonic-gate 	ire->ire_stq = stq;
1841*7c478bd9Sstevel@tonic-gate 	ire->ire_rfq = rfq;
1842*7c478bd9Sstevel@tonic-gate 	ire->ire_type = type;
1843*7c478bd9Sstevel@tonic-gate 	ire->ire_flags = RTF_UP | flags;
1844*7c478bd9Sstevel@tonic-gate 	ire->ire_ident = TICK_TO_MSEC(lbolt);
1845*7c478bd9Sstevel@tonic-gate 	bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t));
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
1848*7c478bd9Sstevel@tonic-gate 	ire->ire_last_used_time = lbolt;
1849*7c478bd9Sstevel@tonic-gate 	ire->ire_create_time = (uint32_t)gethrestime_sec();
1850*7c478bd9Sstevel@tonic-gate 
1851*7c478bd9Sstevel@tonic-gate 	/*
1852*7c478bd9Sstevel@tonic-gate 	 * If this IRE is an IRE_CACHE, inherit the handles from the
1853*7c478bd9Sstevel@tonic-gate 	 * parent IREs. For others in the forwarding table, assign appropriate
1854*7c478bd9Sstevel@tonic-gate 	 * new ones.
1855*7c478bd9Sstevel@tonic-gate 	 *
1856*7c478bd9Sstevel@tonic-gate 	 * The mutex protecting ire_handle is because ire_create is not always
1857*7c478bd9Sstevel@tonic-gate 	 * called as a writer.
1858*7c478bd9Sstevel@tonic-gate 	 */
1859*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_OFFSUBNET) {
1860*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_handle_lock);
1861*7c478bd9Sstevel@tonic-gate 		ire->ire_phandle = (uint32_t)ire_handle++;
1862*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_handle_lock);
1863*7c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type & IRE_INTERFACE) {
1864*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_handle_lock);
1865*7c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = (uint32_t)ire_handle++;
1866*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_handle_lock);
1867*7c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_CACHE) {
1868*7c478bd9Sstevel@tonic-gate 		ire->ire_phandle = phandle;
1869*7c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = ihandle;
1870*7c478bd9Sstevel@tonic-gate 	}
1871*7c478bd9Sstevel@tonic-gate 	ire->ire_in_ill = in_ill;
1872*7c478bd9Sstevel@tonic-gate 	ire->ire_ipif = ipif;
1873*7c478bd9Sstevel@tonic-gate 	if (ipif != NULL) {
1874*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif_seqid = ipif->ipif_seqid;
1875*7c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = ipif->ipif_zoneid;
1876*7c478bd9Sstevel@tonic-gate 	} else {
1877*7c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = GLOBAL_ZONEID;
1878*7c478bd9Sstevel@tonic-gate 	}
1879*7c478bd9Sstevel@tonic-gate 	ire->ire_ipversion = ipversion;
1880*7c478bd9Sstevel@tonic-gate 	ire->ire_refcnt = 1;
1881*7c478bd9Sstevel@tonic-gate 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
1882*7c478bd9Sstevel@tonic-gate 
1883*7c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
1884*7c478bd9Sstevel@tonic-gate 	bzero(ire->ire_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
1885*7c478bd9Sstevel@tonic-gate #endif
1886*7c478bd9Sstevel@tonic-gate }
1887*7c478bd9Sstevel@tonic-gate 
1888*7c478bd9Sstevel@tonic-gate /*
1889*7c478bd9Sstevel@tonic-gate  * This routine is called repeatedly by ipif_up to create broadcast IREs.
1890*7c478bd9Sstevel@tonic-gate  * It is passed a pointer to a slot in an IRE pointer array into which to
1891*7c478bd9Sstevel@tonic-gate  * place the pointer to the new IRE, if indeed we create one.  If the
1892*7c478bd9Sstevel@tonic-gate  * IRE corresponding to the address passed in would be a duplicate of an
1893*7c478bd9Sstevel@tonic-gate  * existing one, we don't create the new one.  irep is incremented before
1894*7c478bd9Sstevel@tonic-gate  * return only if we do create a new IRE.  (Always called as writer.)
1895*7c478bd9Sstevel@tonic-gate  *
1896*7c478bd9Sstevel@tonic-gate  * Note that with the "match_flags" parameter, we can match on either
1897*7c478bd9Sstevel@tonic-gate  * a particular logical interface (MATCH_IRE_IPIF) or for all logical
1898*7c478bd9Sstevel@tonic-gate  * interfaces for a given physical interface (MATCH_IRE_ILL).  Currently,
1899*7c478bd9Sstevel@tonic-gate  * we only create broadcast ire's on a per physical interface basis. If
1900*7c478bd9Sstevel@tonic-gate  * someone is going to be mucking with logical interfaces, it is important
1901*7c478bd9Sstevel@tonic-gate  * to call "ipif_check_bcast_ires()" to make sure that any change to a
1902*7c478bd9Sstevel@tonic-gate  * logical interface will not cause critical broadcast IRE's to be deleted.
1903*7c478bd9Sstevel@tonic-gate  */
1904*7c478bd9Sstevel@tonic-gate ire_t **
1905*7c478bd9Sstevel@tonic-gate ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep,
1906*7c478bd9Sstevel@tonic-gate     int match_flags)
1907*7c478bd9Sstevel@tonic-gate {
1908*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
1909*7c478bd9Sstevel@tonic-gate 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
1910*7c478bd9Sstevel@tonic-gate 
1911*7c478bd9Sstevel@tonic-gate 	/*
1912*7c478bd9Sstevel@tonic-gate 	 * No broadcast IREs for the LOOPBACK interface
1913*7c478bd9Sstevel@tonic-gate 	 * or others such as point to point and IPIF_NOXMIT.
1914*7c478bd9Sstevel@tonic-gate 	 */
1915*7c478bd9Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_BROADCAST) ||
1916*7c478bd9Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_NOXMIT))
1917*7c478bd9Sstevel@tonic-gate 		return (irep);
1918*7c478bd9Sstevel@tonic-gate 
1919*7c478bd9Sstevel@tonic-gate 	/* If this would be a duplicate, don't bother. */
1920*7c478bd9Sstevel@tonic-gate 	if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif,
1921*7c478bd9Sstevel@tonic-gate 	    ipif->ipif_zoneid, match_flags)) != NULL) {
1922*7c478bd9Sstevel@tonic-gate 		/*
1923*7c478bd9Sstevel@tonic-gate 		 * We look for non-deprecated (and non-anycast, non-nolocal)
1924*7c478bd9Sstevel@tonic-gate 		 * ipifs as the best choice. ipifs with check_flags matching
1925*7c478bd9Sstevel@tonic-gate 		 * (deprecated, etc) are used only if non-deprecated ipifs
1926*7c478bd9Sstevel@tonic-gate 		 * are not available. if the existing ire's ipif is deprecated
1927*7c478bd9Sstevel@tonic-gate 		 * and the new ipif is non-deprecated, switch to the new ipif
1928*7c478bd9Sstevel@tonic-gate 		 */
1929*7c478bd9Sstevel@tonic-gate 		if ((!(ire->ire_ipif->ipif_flags & check_flags)) ||
1930*7c478bd9Sstevel@tonic-gate 		    (ipif->ipif_flags & check_flags)) {
1931*7c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
1932*7c478bd9Sstevel@tonic-gate 			return (irep);
1933*7c478bd9Sstevel@tonic-gate 		}
1934*7c478bd9Sstevel@tonic-gate 		/*
1935*7c478bd9Sstevel@tonic-gate 		 * Bcast ires exist in pairs. Both have to be deleted,
1936*7c478bd9Sstevel@tonic-gate 		 * Since we are exclusive we can make the above assertion.
1937*7c478bd9Sstevel@tonic-gate 		 * The 1st has to be refrele'd since it was ctable_lookup'd.
1938*7c478bd9Sstevel@tonic-gate 		 */
1939*7c478bd9Sstevel@tonic-gate 		ASSERT(IAM_WRITER_IPIF(ipif));
1940*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_next->ire_addr == ire->ire_addr);
1941*7c478bd9Sstevel@tonic-gate 		ire_delete(ire->ire_next);
1942*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
1943*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
1944*7c478bd9Sstevel@tonic-gate 	}
1945*7c478bd9Sstevel@tonic-gate 
1946*7c478bd9Sstevel@tonic-gate 	irep = ire_create_bcast(ipif, addr, irep);
1947*7c478bd9Sstevel@tonic-gate 
1948*7c478bd9Sstevel@tonic-gate 	return (irep);
1949*7c478bd9Sstevel@tonic-gate }
1950*7c478bd9Sstevel@tonic-gate 
1951*7c478bd9Sstevel@tonic-gate uint_t ip_loopback_mtu = IP_LOOPBACK_MTU;
1952*7c478bd9Sstevel@tonic-gate 
1953*7c478bd9Sstevel@tonic-gate /*
1954*7c478bd9Sstevel@tonic-gate  * This routine is called from ipif_check_bcast_ires and ire_check_bcast.
1955*7c478bd9Sstevel@tonic-gate  * It leaves all the verifying and deleting to those routines. So it always
1956*7c478bd9Sstevel@tonic-gate  * creates 2 bcast ires and chains them into the ire array passed in.
1957*7c478bd9Sstevel@tonic-gate  */
1958*7c478bd9Sstevel@tonic-gate ire_t **
1959*7c478bd9Sstevel@tonic-gate ire_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep)
1960*7c478bd9Sstevel@tonic-gate {
1961*7c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
1962*7c478bd9Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest addr */
1963*7c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
1964*7c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ipif->ipif_src_addr,	/* source addr */
1965*7c478bd9Sstevel@tonic-gate 	    NULL,				/* no gateway */
1966*7c478bd9Sstevel@tonic-gate 	    NULL,				/* no in_src_addr */
1967*7c478bd9Sstevel@tonic-gate 	    &ipif->ipif_mtu,			/* max frag */
1968*7c478bd9Sstevel@tonic-gate 	    NULL,				/* fast path header */
1969*7c478bd9Sstevel@tonic-gate 	    ipif->ipif_rq,			/* recv-from queue */
1970*7c478bd9Sstevel@tonic-gate 	    ipif->ipif_wq,			/* send-to queue */
1971*7c478bd9Sstevel@tonic-gate 	    IRE_BROADCAST,
1972*7c478bd9Sstevel@tonic-gate 	    ipif->ipif_bcast_mp,		/* xmit header */
1973*7c478bd9Sstevel@tonic-gate 	    ipif,
1974*7c478bd9Sstevel@tonic-gate 	    NULL,
1975*7c478bd9Sstevel@tonic-gate 	    0,
1976*7c478bd9Sstevel@tonic-gate 	    0,
1977*7c478bd9Sstevel@tonic-gate 	    0,
1978*7c478bd9Sstevel@tonic-gate 	    0,
1979*7c478bd9Sstevel@tonic-gate 	    &ire_uinfo_null);
1980*7c478bd9Sstevel@tonic-gate 
1981*7c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
1982*7c478bd9Sstevel@tonic-gate 		(uchar_t *)&addr,		 /* dest address */
1983*7c478bd9Sstevel@tonic-gate 		(uchar_t *)&ip_g_all_ones,	 /* mask */
1984*7c478bd9Sstevel@tonic-gate 		(uchar_t *)&ipif->ipif_src_addr, /* source address */
1985*7c478bd9Sstevel@tonic-gate 		NULL,				 /* no gateway */
1986*7c478bd9Sstevel@tonic-gate 		NULL,				 /* no in_src_addr */
1987*7c478bd9Sstevel@tonic-gate 		&ip_loopback_mtu,		 /* max frag size */
1988*7c478bd9Sstevel@tonic-gate 		NULL,				 /* Fast Path header */
1989*7c478bd9Sstevel@tonic-gate 		ipif->ipif_rq,			 /* recv-from queue */
1990*7c478bd9Sstevel@tonic-gate 		NULL,				 /* no send-to queue */
1991*7c478bd9Sstevel@tonic-gate 		IRE_BROADCAST,		/* Needed for fanout in wput */
1992*7c478bd9Sstevel@tonic-gate 		NULL,
1993*7c478bd9Sstevel@tonic-gate 		ipif,
1994*7c478bd9Sstevel@tonic-gate 		NULL,
1995*7c478bd9Sstevel@tonic-gate 		0,
1996*7c478bd9Sstevel@tonic-gate 		0,
1997*7c478bd9Sstevel@tonic-gate 		0,
1998*7c478bd9Sstevel@tonic-gate 		0,
1999*7c478bd9Sstevel@tonic-gate 		&ire_uinfo_null);
2000*7c478bd9Sstevel@tonic-gate 
2001*7c478bd9Sstevel@tonic-gate 	return (irep);
2002*7c478bd9Sstevel@tonic-gate }
2003*7c478bd9Sstevel@tonic-gate 
2004*7c478bd9Sstevel@tonic-gate /*
2005*7c478bd9Sstevel@tonic-gate  * ire_walk routine to delete or update any IRE_CACHE that might contain
2006*7c478bd9Sstevel@tonic-gate  * stale information.
2007*7c478bd9Sstevel@tonic-gate  * The flags state which entries to delete or update.
2008*7c478bd9Sstevel@tonic-gate  * Garbage collection is done separately using kmem alloc callbacks to
2009*7c478bd9Sstevel@tonic-gate  * ip_trash_ire_reclaim.
2010*7c478bd9Sstevel@tonic-gate  * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME
2011*7c478bd9Sstevel@tonic-gate  * since other stale information is cleaned up using NUD.
2012*7c478bd9Sstevel@tonic-gate  */
2013*7c478bd9Sstevel@tonic-gate void
2014*7c478bd9Sstevel@tonic-gate ire_expire(ire_t *ire, char *arg)
2015*7c478bd9Sstevel@tonic-gate {
2016*7c478bd9Sstevel@tonic-gate 	int flush_flags = (int)(uintptr_t)arg;
2017*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
2018*7c478bd9Sstevel@tonic-gate 
2019*7c478bd9Sstevel@tonic-gate 	if ((flush_flags & FLUSH_REDIRECT_TIME) &&
2020*7c478bd9Sstevel@tonic-gate 	    ire->ire_type == IRE_HOST_REDIRECT) {
2021*7c478bd9Sstevel@tonic-gate 		/* Make sure we delete the corresponding IRE_CACHE */
2022*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_expire: all redirects\n"));
2023*7c478bd9Sstevel@tonic-gate 		ip_rts_rtmsg(RTM_DELETE, ire, 0);
2024*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
2025*7c478bd9Sstevel@tonic-gate 		return;
2026*7c478bd9Sstevel@tonic-gate 	}
2027*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
2028*7c478bd9Sstevel@tonic-gate 		return;
2029*7c478bd9Sstevel@tonic-gate 
2030*7c478bd9Sstevel@tonic-gate 	if (flush_flags & FLUSH_ARP_TIME) {
2031*7c478bd9Sstevel@tonic-gate 		/*
2032*7c478bd9Sstevel@tonic-gate 		 * Remove all IRE_CACHE.
2033*7c478bd9Sstevel@tonic-gate 		 * Verify that create time is more than
2034*7c478bd9Sstevel@tonic-gate 		 * ip_ire_arp_interval milliseconds ago.
2035*7c478bd9Sstevel@tonic-gate 		 */
2036*7c478bd9Sstevel@tonic-gate 		if (((uint32_t)gethrestime_sec() - ire->ire_create_time) *
2037*7c478bd9Sstevel@tonic-gate 		    MILLISEC > ip_ire_arp_interval) {
2038*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_expire: all IRE_CACHE\n"));
2039*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
2040*7c478bd9Sstevel@tonic-gate 			return;
2041*7c478bd9Sstevel@tonic-gate 		}
2042*7c478bd9Sstevel@tonic-gate 	}
2043*7c478bd9Sstevel@tonic-gate 
2044*7c478bd9Sstevel@tonic-gate 	if (ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) &&
2045*7c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif != NULL)) {
2046*7c478bd9Sstevel@tonic-gate 		/* Increase pmtu if it is less than the interface mtu */
2047*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
2048*7c478bd9Sstevel@tonic-gate 		/*
2049*7c478bd9Sstevel@tonic-gate 		 * If the ipif is a vni (whose mtu is 0, since it's virtual)
2050*7c478bd9Sstevel@tonic-gate 		 * get the mtu from the sending interfaces' ipif
2051*7c478bd9Sstevel@tonic-gate 		 */
2052*7c478bd9Sstevel@tonic-gate 		if (IS_VNI(ire->ire_ipif->ipif_ill)) {
2053*7c478bd9Sstevel@tonic-gate 			stq_ill = ire->ire_stq->q_ptr;
2054*7c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu,
2055*7c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
2056*7c478bd9Sstevel@tonic-gate 		} else {
2057*7c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
2058*7c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
2059*7c478bd9Sstevel@tonic-gate 		}
2060*7c478bd9Sstevel@tonic-gate 		ire->ire_frag_flag |= IPH_DF;
2061*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2062*7c478bd9Sstevel@tonic-gate 	}
2063*7c478bd9Sstevel@tonic-gate }
2064*7c478bd9Sstevel@tonic-gate 
2065*7c478bd9Sstevel@tonic-gate /*
2066*7c478bd9Sstevel@tonic-gate  * Do fast path probing if necessary.
2067*7c478bd9Sstevel@tonic-gate  */
2068*7c478bd9Sstevel@tonic-gate static void
2069*7c478bd9Sstevel@tonic-gate ire_fastpath(ire_t *ire)
2070*7c478bd9Sstevel@tonic-gate {
2071*7c478bd9Sstevel@tonic-gate 	ill_t	*ill;
2072*7c478bd9Sstevel@tonic-gate 	int res;
2073*7c478bd9Sstevel@tonic-gate 
2074*7c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp != NULL || ire->ire_dlureq_mp == NULL ||
2075*7c478bd9Sstevel@tonic-gate 	    (ire->ire_stq == NULL)) {
2076*7c478bd9Sstevel@tonic-gate 		/*
2077*7c478bd9Sstevel@tonic-gate 		 * Already contains fastpath info or
2078*7c478bd9Sstevel@tonic-gate 		 * doesn't have DL_UNITDATA_REQ header
2079*7c478bd9Sstevel@tonic-gate 		 * or is a loopback broadcast ire i.e. no stq.
2080*7c478bd9Sstevel@tonic-gate 		 */
2081*7c478bd9Sstevel@tonic-gate 		return;
2082*7c478bd9Sstevel@tonic-gate 	}
2083*7c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
2084*7c478bd9Sstevel@tonic-gate 	if (ill == NULL)
2085*7c478bd9Sstevel@tonic-gate 		return;
2086*7c478bd9Sstevel@tonic-gate 	ire_fastpath_list_add(ill, ire);
2087*7c478bd9Sstevel@tonic-gate 	res = ill_fastpath_probe(ill, ire->ire_dlureq_mp);
2088*7c478bd9Sstevel@tonic-gate 	/*
2089*7c478bd9Sstevel@tonic-gate 	 * EAGAIN is an indication of a transient error
2090*7c478bd9Sstevel@tonic-gate 	 * i.e. allocation failure etc. leave the ire in the list it will
2091*7c478bd9Sstevel@tonic-gate 	 * be updated when another probe happens for another ire if not
2092*7c478bd9Sstevel@tonic-gate 	 * it will be taken out of the list when the ire is deleted.
2093*7c478bd9Sstevel@tonic-gate 	 */
2094*7c478bd9Sstevel@tonic-gate 	if (res != 0 && res != EAGAIN)
2095*7c478bd9Sstevel@tonic-gate 		ire_fastpath_list_delete(ill, ire);
2096*7c478bd9Sstevel@tonic-gate }
2097*7c478bd9Sstevel@tonic-gate 
2098*7c478bd9Sstevel@tonic-gate /*
2099*7c478bd9Sstevel@tonic-gate  * Update all IRE's that are not in fastpath mode and
2100*7c478bd9Sstevel@tonic-gate  * have an dlureq_mp that matches mp. mp->b_cont contains
2101*7c478bd9Sstevel@tonic-gate  * the fastpath header.
2102*7c478bd9Sstevel@tonic-gate  *
2103*7c478bd9Sstevel@tonic-gate  * Returns TRUE if entry should be dequeued, or FALSE otherwise.
2104*7c478bd9Sstevel@tonic-gate  */
2105*7c478bd9Sstevel@tonic-gate boolean_t
2106*7c478bd9Sstevel@tonic-gate ire_fastpath_update(ire_t *ire, void *arg)
2107*7c478bd9Sstevel@tonic-gate {
2108*7c478bd9Sstevel@tonic-gate 	mblk_t 	*mp, *fp_mp;
2109*7c478bd9Sstevel@tonic-gate 	uchar_t 	*up, *up2;
2110*7c478bd9Sstevel@tonic-gate 	ptrdiff_t	cmplen;
2111*7c478bd9Sstevel@tonic-gate 
2112*7c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_type & (IRE_CACHE | IRE_BROADCAST |
2113*7c478bd9Sstevel@tonic-gate 	    IRE_MIPRTUN)) != 0);
2114*7c478bd9Sstevel@tonic-gate 
2115*7c478bd9Sstevel@tonic-gate 	/*
2116*7c478bd9Sstevel@tonic-gate 	 * Already contains fastpath info or doesn't have
2117*7c478bd9Sstevel@tonic-gate 	 * DL_UNITDATA_REQ header.
2118*7c478bd9Sstevel@tonic-gate 	 */
2119*7c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp != NULL || ire->ire_dlureq_mp == NULL)
2120*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
2121*7c478bd9Sstevel@tonic-gate 
2122*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_fastpath_update: trying\n"));
2123*7c478bd9Sstevel@tonic-gate 	mp = arg;
2124*7c478bd9Sstevel@tonic-gate 	up = mp->b_rptr;
2125*7c478bd9Sstevel@tonic-gate 	cmplen = mp->b_wptr - up;
2126*7c478bd9Sstevel@tonic-gate 	/* Serialize multiple fast path updates */
2127*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
2128*7c478bd9Sstevel@tonic-gate 	up2 = ire->ire_dlureq_mp->b_rptr;
2129*7c478bd9Sstevel@tonic-gate 	ASSERT(cmplen >= 0);
2130*7c478bd9Sstevel@tonic-gate 	if (ire->ire_dlureq_mp->b_wptr - up2 != cmplen ||
2131*7c478bd9Sstevel@tonic-gate 	    bcmp(up, up2, cmplen) != 0) {
2132*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2133*7c478bd9Sstevel@tonic-gate 		/*
2134*7c478bd9Sstevel@tonic-gate 		 * Don't take the ire off the fastpath list yet,
2135*7c478bd9Sstevel@tonic-gate 		 * since the response may come later.
2136*7c478bd9Sstevel@tonic-gate 		 */
2137*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2138*7c478bd9Sstevel@tonic-gate 	}
2139*7c478bd9Sstevel@tonic-gate 	/* Matched - install mp as the ire_fp_mp */
2140*7c478bd9Sstevel@tonic-gate 	ip1dbg(("ire_fastpath_update: match\n"));
2141*7c478bd9Sstevel@tonic-gate 	fp_mp = dupb(mp->b_cont);
2142*7c478bd9Sstevel@tonic-gate 	if (fp_mp) {
2143*7c478bd9Sstevel@tonic-gate 		/*
2144*7c478bd9Sstevel@tonic-gate 		 * We checked ire_fp_mp above. Check it again with the
2145*7c478bd9Sstevel@tonic-gate 		 * lock. Update fp_mp only if it has not been done
2146*7c478bd9Sstevel@tonic-gate 		 * already.
2147*7c478bd9Sstevel@tonic-gate 		 */
2148*7c478bd9Sstevel@tonic-gate 		if (ire->ire_fp_mp == NULL) {
2149*7c478bd9Sstevel@tonic-gate 			/*
2150*7c478bd9Sstevel@tonic-gate 			 * ire_ll_hdr_length is just an optimization to
2151*7c478bd9Sstevel@tonic-gate 			 * store the length. It is used to return the
2152*7c478bd9Sstevel@tonic-gate 			 * fast path header length to the upper layers.
2153*7c478bd9Sstevel@tonic-gate 			 */
2154*7c478bd9Sstevel@tonic-gate 			ire->ire_fp_mp = fp_mp;
2155*7c478bd9Sstevel@tonic-gate 			ire->ire_ll_hdr_length =
2156*7c478bd9Sstevel@tonic-gate 			    (uint_t)(fp_mp->b_wptr - fp_mp->b_rptr);
2157*7c478bd9Sstevel@tonic-gate 		} else {
2158*7c478bd9Sstevel@tonic-gate 			freeb(fp_mp);
2159*7c478bd9Sstevel@tonic-gate 		}
2160*7c478bd9Sstevel@tonic-gate 	}
2161*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
2162*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
2163*7c478bd9Sstevel@tonic-gate }
2164*7c478bd9Sstevel@tonic-gate 
2165*7c478bd9Sstevel@tonic-gate /*
2166*7c478bd9Sstevel@tonic-gate  * This function handles the DL_NOTE_FASTPATH_FLUSH notification from the
2167*7c478bd9Sstevel@tonic-gate  * driver.
2168*7c478bd9Sstevel@tonic-gate  */
2169*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2170*7c478bd9Sstevel@tonic-gate void
2171*7c478bd9Sstevel@tonic-gate ire_fastpath_flush(ire_t *ire, void *arg)
2172*7c478bd9Sstevel@tonic-gate {
2173*7c478bd9Sstevel@tonic-gate 	ill_t	*ill;
2174*7c478bd9Sstevel@tonic-gate 	int	res;
2175*7c478bd9Sstevel@tonic-gate 
2176*7c478bd9Sstevel@tonic-gate 	/* No fastpath info? */
2177*7c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp == NULL || ire->ire_dlureq_mp == NULL)
2178*7c478bd9Sstevel@tonic-gate 		return;
2179*7c478bd9Sstevel@tonic-gate 
2180*7c478bd9Sstevel@tonic-gate 	/*
2181*7c478bd9Sstevel@tonic-gate 	 * Just remove the IRE if it is for non-broadcast dest.  Then
2182*7c478bd9Sstevel@tonic-gate 	 * we will create another one which will have the correct
2183*7c478bd9Sstevel@tonic-gate 	 * fastpath info.
2184*7c478bd9Sstevel@tonic-gate 	 */
2185*7c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
2186*7c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
2187*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
2188*7c478bd9Sstevel@tonic-gate 		break;
2189*7c478bd9Sstevel@tonic-gate 	case IRE_MIPRTUN:
2190*7c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
2191*7c478bd9Sstevel@tonic-gate 		/*
2192*7c478bd9Sstevel@tonic-gate 		 * We can't delete the ire since it is difficult to
2193*7c478bd9Sstevel@tonic-gate 		 * recreate these ire's without going through the
2194*7c478bd9Sstevel@tonic-gate 		 * ipif down/up dance. The ire_fp_mp is protected by the
2195*7c478bd9Sstevel@tonic-gate 		 * ire_lock in the case of IRE_MIPRTUN and IRE_BROADCAST.
2196*7c478bd9Sstevel@tonic-gate 		 * All access to ire_fp_mp in the case of these 2 ire types
2197*7c478bd9Sstevel@tonic-gate 		 * is protected by ire_lock.
2198*7c478bd9Sstevel@tonic-gate 		 */
2199*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
2200*7c478bd9Sstevel@tonic-gate 		if (ire->ire_fp_mp != NULL) {
2201*7c478bd9Sstevel@tonic-gate 			freeb(ire->ire_fp_mp);
2202*7c478bd9Sstevel@tonic-gate 			ire->ire_fp_mp = NULL;
2203*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
2204*7c478bd9Sstevel@tonic-gate 			/*
2205*7c478bd9Sstevel@tonic-gate 			 * No fastpath probe if there is no stq i.e.
2206*7c478bd9Sstevel@tonic-gate 			 * i.e. the case of loopback broadcast ire.
2207*7c478bd9Sstevel@tonic-gate 			 */
2208*7c478bd9Sstevel@tonic-gate 			if (ire->ire_stq == NULL)
2209*7c478bd9Sstevel@tonic-gate 				break;
2210*7c478bd9Sstevel@tonic-gate 			ill = (ill_t *)((ire->ire_stq)->q_ptr);
2211*7c478bd9Sstevel@tonic-gate 			ire_fastpath_list_add(ill, ire);
2212*7c478bd9Sstevel@tonic-gate 			res = ill_fastpath_probe(ill, ire->ire_dlureq_mp);
2213*7c478bd9Sstevel@tonic-gate 			/*
2214*7c478bd9Sstevel@tonic-gate 			 * EAGAIN is an indication of a transient error
2215*7c478bd9Sstevel@tonic-gate 			 * i.e. allocation failure etc. leave the ire in the
2216*7c478bd9Sstevel@tonic-gate 			 * list it will be updated when another probe happens
2217*7c478bd9Sstevel@tonic-gate 			 * for another ire if not it will be taken out of the
2218*7c478bd9Sstevel@tonic-gate 			 * list when the ire is deleted.
2219*7c478bd9Sstevel@tonic-gate 			 */
2220*7c478bd9Sstevel@tonic-gate 			if (res != 0 && res != EAGAIN)
2221*7c478bd9Sstevel@tonic-gate 				ire_fastpath_list_delete(ill, ire);
2222*7c478bd9Sstevel@tonic-gate 		} else {
2223*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
2224*7c478bd9Sstevel@tonic-gate 		}
2225*7c478bd9Sstevel@tonic-gate 		break;
2226*7c478bd9Sstevel@tonic-gate 	default:
2227*7c478bd9Sstevel@tonic-gate 		/* This should not happen! */
2228*7c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_fastpath_flush: Wrong ire type %s\n",
2229*7c478bd9Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type)));
2230*7c478bd9Sstevel@tonic-gate 		break;
2231*7c478bd9Sstevel@tonic-gate 	}
2232*7c478bd9Sstevel@tonic-gate }
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate /*
2235*7c478bd9Sstevel@tonic-gate  * Drain the list of ire's waiting for fastpath response.
2236*7c478bd9Sstevel@tonic-gate  */
2237*7c478bd9Sstevel@tonic-gate void
2238*7c478bd9Sstevel@tonic-gate ire_fastpath_list_dispatch(ill_t *ill, boolean_t (*func)(ire_t *, void *),
2239*7c478bd9Sstevel@tonic-gate     void *arg)
2240*7c478bd9Sstevel@tonic-gate {
2241*7c478bd9Sstevel@tonic-gate 	ire_t	 *next_ire;
2242*7c478bd9Sstevel@tonic-gate 	ire_t	 *current_ire;
2243*7c478bd9Sstevel@tonic-gate 	ire_t	 *first_ire;
2244*7c478bd9Sstevel@tonic-gate 	ire_t	 *prev_ire = NULL;
2245*7c478bd9Sstevel@tonic-gate 
2246*7c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
2247*7c478bd9Sstevel@tonic-gate 
2248*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
2249*7c478bd9Sstevel@tonic-gate 	first_ire = current_ire = (ire_t *)ill->ill_fastpath_list;
2250*7c478bd9Sstevel@tonic-gate 	while (current_ire != (ire_t *)&ill->ill_fastpath_list) {
2251*7c478bd9Sstevel@tonic-gate 		next_ire = current_ire->ire_fastpath;
2252*7c478bd9Sstevel@tonic-gate 		/*
2253*7c478bd9Sstevel@tonic-gate 		 * Take it off the list if we're flushing, or if the callback
2254*7c478bd9Sstevel@tonic-gate 		 * routine tells us to do so.  Otherwise, leave the ire in the
2255*7c478bd9Sstevel@tonic-gate 		 * fastpath list to handle any pending response from the lower
2256*7c478bd9Sstevel@tonic-gate 		 * layer.  We can't drain the list when the callback routine
2257*7c478bd9Sstevel@tonic-gate 		 * comparison failed, because the response is asynchronous in
2258*7c478bd9Sstevel@tonic-gate 		 * nature, and may not arrive in the same order as the list
2259*7c478bd9Sstevel@tonic-gate 		 * insertion.
2260*7c478bd9Sstevel@tonic-gate 		 */
2261*7c478bd9Sstevel@tonic-gate 		if (func == NULL || func(current_ire, arg)) {
2262*7c478bd9Sstevel@tonic-gate 			current_ire->ire_fastpath = NULL;
2263*7c478bd9Sstevel@tonic-gate 			if (current_ire == first_ire)
2264*7c478bd9Sstevel@tonic-gate 				ill->ill_fastpath_list = first_ire = next_ire;
2265*7c478bd9Sstevel@tonic-gate 			else
2266*7c478bd9Sstevel@tonic-gate 				prev_ire->ire_fastpath = next_ire;
2267*7c478bd9Sstevel@tonic-gate 		} else {
2268*7c478bd9Sstevel@tonic-gate 			/* previous element that is still in the list */
2269*7c478bd9Sstevel@tonic-gate 			prev_ire = current_ire;
2270*7c478bd9Sstevel@tonic-gate 		}
2271*7c478bd9Sstevel@tonic-gate 		current_ire = next_ire;
2272*7c478bd9Sstevel@tonic-gate 	}
2273*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
2274*7c478bd9Sstevel@tonic-gate }
2275*7c478bd9Sstevel@tonic-gate 
2276*7c478bd9Sstevel@tonic-gate /*
2277*7c478bd9Sstevel@tonic-gate  * Add ire to the ire fastpath list.
2278*7c478bd9Sstevel@tonic-gate  */
2279*7c478bd9Sstevel@tonic-gate static void
2280*7c478bd9Sstevel@tonic-gate ire_fastpath_list_add(ill_t *ill, ire_t *ire)
2281*7c478bd9Sstevel@tonic-gate {
2282*7c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
2283*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_stq != NULL);
2284*7c478bd9Sstevel@tonic-gate 
2285*7c478bd9Sstevel@tonic-gate 	rw_enter(&ire->ire_bucket->irb_lock, RW_READER);
2286*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
2287*7c478bd9Sstevel@tonic-gate 
2288*7c478bd9Sstevel@tonic-gate 	/*
2289*7c478bd9Sstevel@tonic-gate 	 * if ire has not been deleted and
2290*7c478bd9Sstevel@tonic-gate 	 * is not already in the list add it.
2291*7c478bd9Sstevel@tonic-gate 	 */
2292*7c478bd9Sstevel@tonic-gate 	if (((ire->ire_marks & IRE_MARK_CONDEMNED) == 0) &&
2293*7c478bd9Sstevel@tonic-gate 	    (ire->ire_fastpath == NULL)) {
2294*7c478bd9Sstevel@tonic-gate 		ire->ire_fastpath = (ire_t *)ill->ill_fastpath_list;
2295*7c478bd9Sstevel@tonic-gate 		ill->ill_fastpath_list = ire;
2296*7c478bd9Sstevel@tonic-gate 	}
2297*7c478bd9Sstevel@tonic-gate 
2298*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
2299*7c478bd9Sstevel@tonic-gate 	rw_exit(&ire->ire_bucket->irb_lock);
2300*7c478bd9Sstevel@tonic-gate }
2301*7c478bd9Sstevel@tonic-gate 
2302*7c478bd9Sstevel@tonic-gate /*
2303*7c478bd9Sstevel@tonic-gate  * remove ire from the ire fastpath list.
2304*7c478bd9Sstevel@tonic-gate  */
2305*7c478bd9Sstevel@tonic-gate void
2306*7c478bd9Sstevel@tonic-gate ire_fastpath_list_delete(ill_t *ill, ire_t *ire)
2307*7c478bd9Sstevel@tonic-gate {
2308*7c478bd9Sstevel@tonic-gate 	ire_t	*ire_ptr;
2309*7c478bd9Sstevel@tonic-gate 
2310*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_stq != NULL && ill != NULL);
2311*7c478bd9Sstevel@tonic-gate 
2312*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
2313*7c478bd9Sstevel@tonic-gate 	if (ire->ire_fastpath == NULL)
2314*7c478bd9Sstevel@tonic-gate 		goto done;
2315*7c478bd9Sstevel@tonic-gate 
2316*7c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_fastpath_list != &ill->ill_fastpath_list);
2317*7c478bd9Sstevel@tonic-gate 
2318*7c478bd9Sstevel@tonic-gate 	if (ill->ill_fastpath_list == ire) {
2319*7c478bd9Sstevel@tonic-gate 		ill->ill_fastpath_list = ire->ire_fastpath;
2320*7c478bd9Sstevel@tonic-gate 	} else {
2321*7c478bd9Sstevel@tonic-gate 		ire_ptr = ill->ill_fastpath_list;
2322*7c478bd9Sstevel@tonic-gate 		while (ire_ptr != (ire_t *)&ill->ill_fastpath_list) {
2323*7c478bd9Sstevel@tonic-gate 			if (ire_ptr->ire_fastpath == ire) {
2324*7c478bd9Sstevel@tonic-gate 				ire_ptr->ire_fastpath = ire->ire_fastpath;
2325*7c478bd9Sstevel@tonic-gate 				break;
2326*7c478bd9Sstevel@tonic-gate 			}
2327*7c478bd9Sstevel@tonic-gate 			ire_ptr = ire_ptr->ire_fastpath;
2328*7c478bd9Sstevel@tonic-gate 		}
2329*7c478bd9Sstevel@tonic-gate 	}
2330*7c478bd9Sstevel@tonic-gate 	ire->ire_fastpath = NULL;
2331*7c478bd9Sstevel@tonic-gate done:
2332*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
2333*7c478bd9Sstevel@tonic-gate }
2334*7c478bd9Sstevel@tonic-gate 
2335*7c478bd9Sstevel@tonic-gate 
2336*7c478bd9Sstevel@tonic-gate /*
2337*7c478bd9Sstevel@tonic-gate  * Find an IRE_INTERFACE for the multicast group.
2338*7c478bd9Sstevel@tonic-gate  * Allows different routes for multicast addresses
2339*7c478bd9Sstevel@tonic-gate  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
2340*7c478bd9Sstevel@tonic-gate  * which point at different interfaces. This is used when IP_MULTICAST_IF
2341*7c478bd9Sstevel@tonic-gate  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
2342*7c478bd9Sstevel@tonic-gate  * specify the interface to join on.
2343*7c478bd9Sstevel@tonic-gate  *
2344*7c478bd9Sstevel@tonic-gate  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
2345*7c478bd9Sstevel@tonic-gate  */
2346*7c478bd9Sstevel@tonic-gate ire_t *
2347*7c478bd9Sstevel@tonic-gate ire_lookup_multi(ipaddr_t group, zoneid_t zoneid)
2348*7c478bd9Sstevel@tonic-gate {
2349*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
2350*7c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
2351*7c478bd9Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
2352*7c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
2353*7c478bd9Sstevel@tonic-gate 
2354*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
2355*7c478bd9Sstevel@tonic-gate 	    0, MATCH_IRE_DEFAULT);
2356*7c478bd9Sstevel@tonic-gate 
2357*7c478bd9Sstevel@tonic-gate 	/* We search a resolvable ire in case of multirouting. */
2358*7c478bd9Sstevel@tonic-gate 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
2359*7c478bd9Sstevel@tonic-gate 		ire_t *cire = NULL;
2360*7c478bd9Sstevel@tonic-gate 		/*
2361*7c478bd9Sstevel@tonic-gate 		 * If the route is not resolvable, the looked up ire
2362*7c478bd9Sstevel@tonic-gate 		 * may be changed here. In that case, ire_multirt_lookup()
2363*7c478bd9Sstevel@tonic-gate 		 * IRE_REFRELE the original ire and change it.
2364*7c478bd9Sstevel@tonic-gate 		 */
2365*7c478bd9Sstevel@tonic-gate 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW);
2366*7c478bd9Sstevel@tonic-gate 		if (cire != NULL)
2367*7c478bd9Sstevel@tonic-gate 			ire_refrele(cire);
2368*7c478bd9Sstevel@tonic-gate 	}
2369*7c478bd9Sstevel@tonic-gate 	if (ire == NULL)
2370*7c478bd9Sstevel@tonic-gate 		return (NULL);
2371*7c478bd9Sstevel@tonic-gate 	/*
2372*7c478bd9Sstevel@tonic-gate 	 * Make sure we follow ire_ipif.
2373*7c478bd9Sstevel@tonic-gate 	 *
2374*7c478bd9Sstevel@tonic-gate 	 * We need to determine the interface route through
2375*7c478bd9Sstevel@tonic-gate 	 * which the gateway will be reached. We don't really
2376*7c478bd9Sstevel@tonic-gate 	 * care which interface is picked if the interface is
2377*7c478bd9Sstevel@tonic-gate 	 * part of a group.
2378*7c478bd9Sstevel@tonic-gate 	 */
2379*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
2380*7c478bd9Sstevel@tonic-gate 		ipif = ire->ire_ipif;
2381*7c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
2382*7c478bd9Sstevel@tonic-gate 	}
2383*7c478bd9Sstevel@tonic-gate 
2384*7c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
2385*7c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
2386*7c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
2387*7c478bd9Sstevel@tonic-gate 	case IRE_HOST:
2388*7c478bd9Sstevel@tonic-gate 		gw_addr = ire->ire_gateway_addr;
2389*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
2390*7c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(gw_addr, 0, 0,
2391*7c478bd9Sstevel@tonic-gate 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
2392*7c478bd9Sstevel@tonic-gate 		    match_flags);
2393*7c478bd9Sstevel@tonic-gate 		return (ire);
2394*7c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
2395*7c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
2396*7c478bd9Sstevel@tonic-gate 		return (ire);
2397*7c478bd9Sstevel@tonic-gate 	default:
2398*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
2399*7c478bd9Sstevel@tonic-gate 		return (NULL);
2400*7c478bd9Sstevel@tonic-gate 	}
2401*7c478bd9Sstevel@tonic-gate }
2402*7c478bd9Sstevel@tonic-gate 
2403*7c478bd9Sstevel@tonic-gate /*
2404*7c478bd9Sstevel@tonic-gate  * Return any local address.  We use this to target ourselves
2405*7c478bd9Sstevel@tonic-gate  * when the src address was specified as 'default'.
2406*7c478bd9Sstevel@tonic-gate  * Preference for IRE_LOCAL entries.
2407*7c478bd9Sstevel@tonic-gate  */
2408*7c478bd9Sstevel@tonic-gate ire_t *
2409*7c478bd9Sstevel@tonic-gate ire_lookup_local(zoneid_t zoneid)
2410*7c478bd9Sstevel@tonic-gate {
2411*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
2412*7c478bd9Sstevel@tonic-gate 	irb_t	*irb;
2413*7c478bd9Sstevel@tonic-gate 	ire_t	*maybe = NULL;
2414*7c478bd9Sstevel@tonic-gate 	int i;
2415*7c478bd9Sstevel@tonic-gate 
2416*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size;  i++) {
2417*7c478bd9Sstevel@tonic-gate 		irb = &ip_cache_table[i];
2418*7c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
2419*7c478bd9Sstevel@tonic-gate 			continue;
2420*7c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_READER);
2421*7c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2422*7c478bd9Sstevel@tonic-gate 			if ((ire->ire_marks & IRE_MARK_CONDEMNED) ||
2423*7c478bd9Sstevel@tonic-gate 			    ire->ire_zoneid != zoneid)
2424*7c478bd9Sstevel@tonic-gate 				continue;
2425*7c478bd9Sstevel@tonic-gate 			switch (ire->ire_type) {
2426*7c478bd9Sstevel@tonic-gate 			case IRE_LOOPBACK:
2427*7c478bd9Sstevel@tonic-gate 				if (maybe == NULL) {
2428*7c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
2429*7c478bd9Sstevel@tonic-gate 					maybe = ire;
2430*7c478bd9Sstevel@tonic-gate 				}
2431*7c478bd9Sstevel@tonic-gate 				break;
2432*7c478bd9Sstevel@tonic-gate 			case IRE_LOCAL:
2433*7c478bd9Sstevel@tonic-gate 				if (maybe != NULL) {
2434*7c478bd9Sstevel@tonic-gate 					ire_refrele(maybe);
2435*7c478bd9Sstevel@tonic-gate 				}
2436*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
2437*7c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
2438*7c478bd9Sstevel@tonic-gate 				return (ire);
2439*7c478bd9Sstevel@tonic-gate 			}
2440*7c478bd9Sstevel@tonic-gate 		}
2441*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
2442*7c478bd9Sstevel@tonic-gate 	}
2443*7c478bd9Sstevel@tonic-gate 	return (maybe);
2444*7c478bd9Sstevel@tonic-gate }
2445*7c478bd9Sstevel@tonic-gate 
2446*7c478bd9Sstevel@tonic-gate /*
2447*7c478bd9Sstevel@tonic-gate  * If the specified IRE is associated with a particular ILL, return
2448*7c478bd9Sstevel@tonic-gate  * that ILL pointer (May be called as writer.).
2449*7c478bd9Sstevel@tonic-gate  *
2450*7c478bd9Sstevel@tonic-gate  * NOTE : This is not a generic function that can be used always.
2451*7c478bd9Sstevel@tonic-gate  * This function always returns the ill of the outgoing packets
2452*7c478bd9Sstevel@tonic-gate  * if this ire is used.
2453*7c478bd9Sstevel@tonic-gate  */
2454*7c478bd9Sstevel@tonic-gate ill_t *
2455*7c478bd9Sstevel@tonic-gate ire_to_ill(ire_t *ire)
2456*7c478bd9Sstevel@tonic-gate {
2457*7c478bd9Sstevel@tonic-gate 	ill_t *ill = NULL;
2458*7c478bd9Sstevel@tonic-gate 
2459*7c478bd9Sstevel@tonic-gate 	/*
2460*7c478bd9Sstevel@tonic-gate 	 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained
2461*7c478bd9Sstevel@tonic-gate 	 *    the source address from. ire_stq is the one where the
2462*7c478bd9Sstevel@tonic-gate 	 *    packets will be sent out on. We return that here.
2463*7c478bd9Sstevel@tonic-gate 	 *
2464*7c478bd9Sstevel@tonic-gate 	 * 2) IRE_BROADCAST normally has a loopback and a non-loopback
2465*7c478bd9Sstevel@tonic-gate 	 *    copy and they always exist next to each other with loopback
2466*7c478bd9Sstevel@tonic-gate 	 *    copy being the first one. If we are called on the non-loopback
2467*7c478bd9Sstevel@tonic-gate 	 *    copy, return the one pointed by ire_stq. If it was called on
2468*7c478bd9Sstevel@tonic-gate 	 *    a loopback copy, we still return the one pointed by the next
2469*7c478bd9Sstevel@tonic-gate 	 *    ire's ire_stq pointer i.e the one pointed by the non-loopback
2470*7c478bd9Sstevel@tonic-gate 	 *    copy. We don't want use ire_ipif as it might represent the
2471*7c478bd9Sstevel@tonic-gate 	 *    source address (if we borrow source addresses for
2472*7c478bd9Sstevel@tonic-gate 	 *    IRE_BROADCASTS in the future).
2473*7c478bd9Sstevel@tonic-gate 	 *    However if an interface is currently coming up, the above
2474*7c478bd9Sstevel@tonic-gate 	 *    condition may not hold during that period since the ires
2475*7c478bd9Sstevel@tonic-gate 	 *    are added one at a time. Thus one of the pair could have been
2476*7c478bd9Sstevel@tonic-gate 	 *    added and the other not yet added.
2477*7c478bd9Sstevel@tonic-gate 	 * 3) For all others return the ones pointed by ire_ipif->ipif_ill.
2478*7c478bd9Sstevel@tonic-gate 	 */
2479*7c478bd9Sstevel@tonic-gate 
2480*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
2481*7c478bd9Sstevel@tonic-gate 		ill = (ill_t *)ire->ire_stq->q_ptr;
2482*7c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_BROADCAST) {
2483*7c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
2484*7c478bd9Sstevel@tonic-gate 			ill = (ill_t *)ire->ire_stq->q_ptr;
2485*7c478bd9Sstevel@tonic-gate 		} else {
2486*7c478bd9Sstevel@tonic-gate 			ire_t  *ire_next;
2487*7c478bd9Sstevel@tonic-gate 
2488*7c478bd9Sstevel@tonic-gate 			ire_next = ire->ire_next;
2489*7c478bd9Sstevel@tonic-gate 			if (ire_next != NULL &&
2490*7c478bd9Sstevel@tonic-gate 			    ire_next->ire_type == IRE_BROADCAST &&
2491*7c478bd9Sstevel@tonic-gate 			    ire_next->ire_addr == ire->ire_addr &&
2492*7c478bd9Sstevel@tonic-gate 			    ire_next->ire_ipif == ire->ire_ipif) {
2493*7c478bd9Sstevel@tonic-gate 				ill = (ill_t *)ire_next->ire_stq->q_ptr;
2494*7c478bd9Sstevel@tonic-gate 			}
2495*7c478bd9Sstevel@tonic-gate 		}
2496*7c478bd9Sstevel@tonic-gate 	} else if (ire->ire_ipif != NULL) {
2497*7c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
2498*7c478bd9Sstevel@tonic-gate 	}
2499*7c478bd9Sstevel@tonic-gate 	return (ill);
2500*7c478bd9Sstevel@tonic-gate }
2501*7c478bd9Sstevel@tonic-gate 
2502*7c478bd9Sstevel@tonic-gate /* Arrange to call the specified function for every IRE in the world. */
2503*7c478bd9Sstevel@tonic-gate void
2504*7c478bd9Sstevel@tonic-gate ire_walk(pfv_t func, char *arg)
2505*7c478bd9Sstevel@tonic-gate {
2506*7c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, 0, ALL_ZONES);
2507*7c478bd9Sstevel@tonic-gate }
2508*7c478bd9Sstevel@tonic-gate 
2509*7c478bd9Sstevel@tonic-gate void
2510*7c478bd9Sstevel@tonic-gate ire_walk_v4(pfv_t func, char *arg, zoneid_t zoneid)
2511*7c478bd9Sstevel@tonic-gate {
2512*7c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid);
2513*7c478bd9Sstevel@tonic-gate }
2514*7c478bd9Sstevel@tonic-gate 
2515*7c478bd9Sstevel@tonic-gate void
2516*7c478bd9Sstevel@tonic-gate ire_walk_v6(pfv_t func, char *arg, zoneid_t zoneid)
2517*7c478bd9Sstevel@tonic-gate {
2518*7c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid);
2519*7c478bd9Sstevel@tonic-gate }
2520*7c478bd9Sstevel@tonic-gate 
2521*7c478bd9Sstevel@tonic-gate /*
2522*7c478bd9Sstevel@tonic-gate  * Walk a particular version. version == 0 means both v4 and v6.
2523*7c478bd9Sstevel@tonic-gate  */
2524*7c478bd9Sstevel@tonic-gate static void
2525*7c478bd9Sstevel@tonic-gate ire_walk_ipvers(pfv_t func, char *arg, uchar_t vers, zoneid_t zoneid)
2526*7c478bd9Sstevel@tonic-gate {
2527*7c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
2528*7c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
2529*7c478bd9Sstevel@tonic-gate 		    ip_ftable_hash_size, ip_forwarding_table,
2530*7c478bd9Sstevel@tonic-gate 		    ip_cache_table_size, ip_cache_table, NULL, zoneid);
2531*7c478bd9Sstevel@tonic-gate 	}
2532*7c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
2533*7c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
2534*7c478bd9Sstevel@tonic-gate 		    ip6_ftable_hash_size, ip_forwarding_table_v6,
2535*7c478bd9Sstevel@tonic-gate 		    ip6_cache_table_size, ip_cache_table_v6, NULL, zoneid);
2536*7c478bd9Sstevel@tonic-gate 	}
2537*7c478bd9Sstevel@tonic-gate }
2538*7c478bd9Sstevel@tonic-gate 
2539*7c478bd9Sstevel@tonic-gate /*
2540*7c478bd9Sstevel@tonic-gate  * Arrange to call the specified
2541*7c478bd9Sstevel@tonic-gate  * function for every IRE that matches the ill.
2542*7c478bd9Sstevel@tonic-gate  */
2543*7c478bd9Sstevel@tonic-gate void
2544*7c478bd9Sstevel@tonic-gate ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, char *arg,
2545*7c478bd9Sstevel@tonic-gate     ill_t *ill)
2546*7c478bd9Sstevel@tonic-gate {
2547*7c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, 0, ill);
2548*7c478bd9Sstevel@tonic-gate }
2549*7c478bd9Sstevel@tonic-gate 
2550*7c478bd9Sstevel@tonic-gate void
2551*7c478bd9Sstevel@tonic-gate ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, char *arg,
2552*7c478bd9Sstevel@tonic-gate     ill_t *ill)
2553*7c478bd9Sstevel@tonic-gate {
2554*7c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION,
2555*7c478bd9Sstevel@tonic-gate 	    ill);
2556*7c478bd9Sstevel@tonic-gate }
2557*7c478bd9Sstevel@tonic-gate 
2558*7c478bd9Sstevel@tonic-gate void
2559*7c478bd9Sstevel@tonic-gate ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, char *arg,
2560*7c478bd9Sstevel@tonic-gate     ill_t *ill)
2561*7c478bd9Sstevel@tonic-gate {
2562*7c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION,
2563*7c478bd9Sstevel@tonic-gate 	    ill);
2564*7c478bd9Sstevel@tonic-gate }
2565*7c478bd9Sstevel@tonic-gate 
2566*7c478bd9Sstevel@tonic-gate /*
2567*7c478bd9Sstevel@tonic-gate  * Walk a particular ill and version. version == 0 means both v4 and v6.
2568*7c478bd9Sstevel@tonic-gate  */
2569*7c478bd9Sstevel@tonic-gate static void
2570*7c478bd9Sstevel@tonic-gate ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
2571*7c478bd9Sstevel@tonic-gate     char *arg, uchar_t vers, ill_t *ill)
2572*7c478bd9Sstevel@tonic-gate {
2573*7c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
2574*7c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2575*7c478bd9Sstevel@tonic-gate 		    IP_MASK_TABLE_SIZE, ip_ftable_hash_size,
2576*7c478bd9Sstevel@tonic-gate 		    ip_forwarding_table, ip_cache_table_size,
2577*7c478bd9Sstevel@tonic-gate 		    ip_cache_table, ill, ALL_ZONES);
2578*7c478bd9Sstevel@tonic-gate 	}
2579*7c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
2580*7c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2581*7c478bd9Sstevel@tonic-gate 		    IP6_MASK_TABLE_SIZE, ip6_ftable_hash_size,
2582*7c478bd9Sstevel@tonic-gate 		    ip_forwarding_table_v6, ip6_cache_table_size,
2583*7c478bd9Sstevel@tonic-gate 		    ip_cache_table_v6, ill, ALL_ZONES);
2584*7c478bd9Sstevel@tonic-gate 	}
2585*7c478bd9Sstevel@tonic-gate }
2586*7c478bd9Sstevel@tonic-gate 
2587*7c478bd9Sstevel@tonic-gate static boolean_t
2588*7c478bd9Sstevel@tonic-gate ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
2589*7c478bd9Sstevel@tonic-gate     ill_t *ill, zoneid_t zoneid)
2590*7c478bd9Sstevel@tonic-gate {
2591*7c478bd9Sstevel@tonic-gate 	ill_t *ire_stq_ill = NULL;
2592*7c478bd9Sstevel@tonic-gate 	ill_t *ire_ipif_ill = NULL;
2593*7c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
2594*7c478bd9Sstevel@tonic-gate 
2595*7c478bd9Sstevel@tonic-gate 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
2596*7c478bd9Sstevel@tonic-gate 	/*
2597*7c478bd9Sstevel@tonic-gate 	 * 1) MATCH_IRE_WQ : Used specifically to match on ire_stq.
2598*7c478bd9Sstevel@tonic-gate 	 *    The fast path update uses this to make sure it does not
2599*7c478bd9Sstevel@tonic-gate 	 *    update the fast path header of interface X with the fast
2600*7c478bd9Sstevel@tonic-gate 	 *    path updates it recieved on interface Y.  It is similar
2601*7c478bd9Sstevel@tonic-gate 	 *    in handling DL_NOTE_FASTPATH_FLUSH.
2602*7c478bd9Sstevel@tonic-gate 	 *
2603*7c478bd9Sstevel@tonic-gate 	 * 2) MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill
2604*7c478bd9Sstevel@tonic-gate 	 *    pointed by ire_stq and ire_ipif. Only in the case of
2605*7c478bd9Sstevel@tonic-gate 	 *    IRE_CACHEs can ire_stq and ire_ipif be pointing to
2606*7c478bd9Sstevel@tonic-gate 	 *    different ills. But we want to keep this function generic
2607*7c478bd9Sstevel@tonic-gate 	 *    enough for future use. So, we always try to match on both.
2608*7c478bd9Sstevel@tonic-gate 	 *    The only caller of this function ire_walk_ill_tables, will
2609*7c478bd9Sstevel@tonic-gate 	 *    call "func" after we return from this function. We expect
2610*7c478bd9Sstevel@tonic-gate 	 *    "func" to do the right filtering of ires in this case.
2611*7c478bd9Sstevel@tonic-gate 	 *
2612*7c478bd9Sstevel@tonic-gate 	 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups
2613*7c478bd9Sstevel@tonic-gate 	 * pointed by ire_stq and ire_ipif should always be the same.
2614*7c478bd9Sstevel@tonic-gate 	 * So, we just match on only one of them.
2615*7c478bd9Sstevel@tonic-gate 	 */
2616*7c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
2617*7c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL)
2618*7c478bd9Sstevel@tonic-gate 			ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
2619*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
2620*7c478bd9Sstevel@tonic-gate 			ire_ipif_ill = ire->ire_ipif->ipif_ill;
2621*7c478bd9Sstevel@tonic-gate 		if (ire_stq_ill != NULL)
2622*7c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_stq_ill->ill_group;
2623*7c478bd9Sstevel@tonic-gate 		if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL))
2624*7c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ipif_ill->ill_group;
2625*7c478bd9Sstevel@tonic-gate 	}
2626*7c478bd9Sstevel@tonic-gate 
2627*7c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES) {
2628*7c478bd9Sstevel@tonic-gate 		/*
2629*7c478bd9Sstevel@tonic-gate 		 * We're walking the IREs for a specific zone. The only relevant
2630*7c478bd9Sstevel@tonic-gate 		 * IREs are:
2631*7c478bd9Sstevel@tonic-gate 		 * - all IREs with a matching ire_zoneid
2632*7c478bd9Sstevel@tonic-gate 		 * - all IRE_OFFSUBNETs as they're shared across all zones
2633*7c478bd9Sstevel@tonic-gate 		 * - IRE_INTERFACE IREs for interfaces with a usable source addr
2634*7c478bd9Sstevel@tonic-gate 		 *   with a matching zone
2635*7c478bd9Sstevel@tonic-gate 		 * - IRE_DEFAULTs with a gateway reachable from the zone
2636*7c478bd9Sstevel@tonic-gate 		 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs
2637*7c478bd9Sstevel@tonic-gate 		 * using the same rule; but the above rules are consistent with
2638*7c478bd9Sstevel@tonic-gate 		 * the behavior of ire_ftable_lookup[_v6]() so that all the
2639*7c478bd9Sstevel@tonic-gate 		 * routes that can be matched during lookup are also matched
2640*7c478bd9Sstevel@tonic-gate 		 * here.
2641*7c478bd9Sstevel@tonic-gate 		 */
2642*7c478bd9Sstevel@tonic-gate 		if (zoneid != ire->ire_zoneid) {
2643*7c478bd9Sstevel@tonic-gate 			/*
2644*7c478bd9Sstevel@tonic-gate 			 * Note, IRE_INTERFACE can have the stq as NULL. For
2645*7c478bd9Sstevel@tonic-gate 			 * example, if the default multicast route is tied to
2646*7c478bd9Sstevel@tonic-gate 			 * the loopback address.
2647*7c478bd9Sstevel@tonic-gate 			 */
2648*7c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
2649*7c478bd9Sstevel@tonic-gate 			    (ire->ire_stq != NULL)) {
2650*7c478bd9Sstevel@tonic-gate 				ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
2651*7c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV4_VERSION) {
2652*7c478bd9Sstevel@tonic-gate 					if (!ipif_usesrc_avail(ire_stq_ill,
2653*7c478bd9Sstevel@tonic-gate 					    zoneid))
2654*7c478bd9Sstevel@tonic-gate 						/* No usable src addr in zone */
2655*7c478bd9Sstevel@tonic-gate 						return (B_FALSE);
2656*7c478bd9Sstevel@tonic-gate 				} else if (ire_stq_ill->ill_usesrc_ifindex
2657*7c478bd9Sstevel@tonic-gate 				    != 0) {
2658*7c478bd9Sstevel@tonic-gate 					/*
2659*7c478bd9Sstevel@tonic-gate 					 * For IPv6 use ipif_select_source_v6()
2660*7c478bd9Sstevel@tonic-gate 					 * so the right scope selection is done
2661*7c478bd9Sstevel@tonic-gate 					 */
2662*7c478bd9Sstevel@tonic-gate 					ipif_t *src_ipif;
2663*7c478bd9Sstevel@tonic-gate 					src_ipif =
2664*7c478bd9Sstevel@tonic-gate 					    ipif_select_source_v6(ire_stq_ill,
2665*7c478bd9Sstevel@tonic-gate 					    &ire->ire_addr_v6, B_FALSE,
2666*7c478bd9Sstevel@tonic-gate 					    IPV6_PREFER_SRC_DEFAULT,
2667*7c478bd9Sstevel@tonic-gate 					    zoneid);
2668*7c478bd9Sstevel@tonic-gate 					if (src_ipif != NULL) {
2669*7c478bd9Sstevel@tonic-gate 						ipif_refrele(src_ipif);
2670*7c478bd9Sstevel@tonic-gate 					} else {
2671*7c478bd9Sstevel@tonic-gate 						return (B_FALSE);
2672*7c478bd9Sstevel@tonic-gate 					}
2673*7c478bd9Sstevel@tonic-gate 				} else {
2674*7c478bd9Sstevel@tonic-gate 					return (B_FALSE);
2675*7c478bd9Sstevel@tonic-gate 				}
2676*7c478bd9Sstevel@tonic-gate 
2677*7c478bd9Sstevel@tonic-gate 			} else if (!(ire->ire_type & IRE_OFFSUBNET)) {
2678*7c478bd9Sstevel@tonic-gate 				return (B_FALSE);
2679*7c478bd9Sstevel@tonic-gate 			}
2680*7c478bd9Sstevel@tonic-gate 		}
2681*7c478bd9Sstevel@tonic-gate 
2682*7c478bd9Sstevel@tonic-gate 		/*
2683*7c478bd9Sstevel@tonic-gate 		 * Match all default routes from the global zone, irrespective
2684*7c478bd9Sstevel@tonic-gate 		 * of reachability.
2685*7c478bd9Sstevel@tonic-gate 		 */
2686*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) {
2687*7c478bd9Sstevel@tonic-gate 			int ire_match_flags = 0;
2688*7c478bd9Sstevel@tonic-gate 			in6_addr_t gw_addr_v6;
2689*7c478bd9Sstevel@tonic-gate 			ire_t *rire;
2690*7c478bd9Sstevel@tonic-gate 
2691*7c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif != NULL) {
2692*7c478bd9Sstevel@tonic-gate 				ire_match_flags |= MATCH_IRE_ILL_GROUP;
2693*7c478bd9Sstevel@tonic-gate 			}
2694*7c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
2695*7c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
2696*7c478bd9Sstevel@tonic-gate 				    0, 0, 0, ire->ire_ipif, NULL, zoneid,
2697*7c478bd9Sstevel@tonic-gate 				    ire_match_flags);
2698*7c478bd9Sstevel@tonic-gate 			} else {
2699*7c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
2700*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
2701*7c478bd9Sstevel@tonic-gate 				gw_addr_v6 = ire->ire_gateway_addr_v6;
2702*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire->ire_lock);
2703*7c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup_v6(&gw_addr_v6,
2704*7c478bd9Sstevel@tonic-gate 				    NULL, NULL, 0, ire->ire_ipif, NULL, zoneid,
2705*7c478bd9Sstevel@tonic-gate 				    ire_match_flags);
2706*7c478bd9Sstevel@tonic-gate 			}
2707*7c478bd9Sstevel@tonic-gate 			if (rire == NULL) {
2708*7c478bd9Sstevel@tonic-gate 				return (B_FALSE);
2709*7c478bd9Sstevel@tonic-gate 			}
2710*7c478bd9Sstevel@tonic-gate 			ire_refrele(rire);
2711*7c478bd9Sstevel@tonic-gate 		}
2712*7c478bd9Sstevel@tonic-gate 	}
2713*7c478bd9Sstevel@tonic-gate 
2714*7c478bd9Sstevel@tonic-gate 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
2715*7c478bd9Sstevel@tonic-gate 		(ire->ire_type & ire_type)) &&
2716*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_WQ)) ||
2717*7c478bd9Sstevel@tonic-gate 		(ire->ire_stq == ill->ill_wq)) &&
2718*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
2719*7c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill || ire_ipif_ill == ill)) &&
2720*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
2721*7c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill) || (ire_ipif_ill == ill) ||
2722*7c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
2723*7c478bd9Sstevel@tonic-gate 		ire_ill_group == ill->ill_group))) {
2724*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
2725*7c478bd9Sstevel@tonic-gate 	}
2726*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
2727*7c478bd9Sstevel@tonic-gate }
2728*7c478bd9Sstevel@tonic-gate 
2729*7c478bd9Sstevel@tonic-gate /*
2730*7c478bd9Sstevel@tonic-gate  * Walk the ftable and the ctable entries that match the ill.
2731*7c478bd9Sstevel@tonic-gate  */
2732*7c478bd9Sstevel@tonic-gate static void
2733*7c478bd9Sstevel@tonic-gate ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
2734*7c478bd9Sstevel@tonic-gate     char *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
2735*7c478bd9Sstevel@tonic-gate     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid)
2736*7c478bd9Sstevel@tonic-gate {
2737*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
2738*7c478bd9Sstevel@tonic-gate 	irb_t	*irb;
2739*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
2740*7c478bd9Sstevel@tonic-gate 	int i, j;
2741*7c478bd9Sstevel@tonic-gate 	boolean_t ret;
2742*7c478bd9Sstevel@tonic-gate 
2743*7c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
2744*7c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
2745*7c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
2746*7c478bd9Sstevel@tonic-gate 	/*
2747*7c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the forwarding table if there
2748*7c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE
2749*7c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
2750*7c478bd9Sstevel@tonic-gate 	 */
2751*7c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
2752*7c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_FORWARDTABLE) != 0)) {
2753*7c478bd9Sstevel@tonic-gate 		for (i = (ftbl_sz - 1);  i >= 0; i--) {
2754*7c478bd9Sstevel@tonic-gate 			if ((irb_ptr = ipftbl[i]) == NULL)
2755*7c478bd9Sstevel@tonic-gate 				continue;
2756*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < htbl_sz; j++) {
2757*7c478bd9Sstevel@tonic-gate 				irb = &irb_ptr[j];
2758*7c478bd9Sstevel@tonic-gate 				if (irb->irb_ire == NULL)
2759*7c478bd9Sstevel@tonic-gate 					continue;
2760*7c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(irb);
2761*7c478bd9Sstevel@tonic-gate 				for (ire = irb->irb_ire; ire != NULL;
2762*7c478bd9Sstevel@tonic-gate 				    ire = ire->ire_next) {
2763*7c478bd9Sstevel@tonic-gate 					if (match_flags == 0 &&
2764*7c478bd9Sstevel@tonic-gate 					    zoneid == ALL_ZONES) {
2765*7c478bd9Sstevel@tonic-gate 						ret = B_TRUE;
2766*7c478bd9Sstevel@tonic-gate 					} else {
2767*7c478bd9Sstevel@tonic-gate 						ret = ire_walk_ill_match(
2768*7c478bd9Sstevel@tonic-gate 						    match_flags, ire_type,
2769*7c478bd9Sstevel@tonic-gate 						    ire, ill, zoneid);
2770*7c478bd9Sstevel@tonic-gate 					}
2771*7c478bd9Sstevel@tonic-gate 					if (ret)
2772*7c478bd9Sstevel@tonic-gate 						(*func)(ire, arg);
2773*7c478bd9Sstevel@tonic-gate 				}
2774*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
2775*7c478bd9Sstevel@tonic-gate 			}
2776*7c478bd9Sstevel@tonic-gate 		}
2777*7c478bd9Sstevel@tonic-gate 	}
2778*7c478bd9Sstevel@tonic-gate 
2779*7c478bd9Sstevel@tonic-gate 	/*
2780*7c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the cache table if there
2781*7c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE
2782*7c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
2783*7c478bd9Sstevel@tonic-gate 	 */
2784*7c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
2785*7c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_CACHETABLE) != 0)) {
2786*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ctbl_sz;  i++) {
2787*7c478bd9Sstevel@tonic-gate 			irb = &ipctbl[i];
2788*7c478bd9Sstevel@tonic-gate 			if (irb->irb_ire == NULL)
2789*7c478bd9Sstevel@tonic-gate 				continue;
2790*7c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
2791*7c478bd9Sstevel@tonic-gate 			for (ire = irb->irb_ire; ire != NULL;
2792*7c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
2793*7c478bd9Sstevel@tonic-gate 				if (match_flags == 0 && zoneid == ALL_ZONES) {
2794*7c478bd9Sstevel@tonic-gate 					ret = B_TRUE;
2795*7c478bd9Sstevel@tonic-gate 				} else {
2796*7c478bd9Sstevel@tonic-gate 					ret = ire_walk_ill_match(
2797*7c478bd9Sstevel@tonic-gate 					    match_flags, ire_type,
2798*7c478bd9Sstevel@tonic-gate 					    ire, ill, zoneid);
2799*7c478bd9Sstevel@tonic-gate 				}
2800*7c478bd9Sstevel@tonic-gate 				if (ret)
2801*7c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
2802*7c478bd9Sstevel@tonic-gate 			}
2803*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
2804*7c478bd9Sstevel@tonic-gate 		}
2805*7c478bd9Sstevel@tonic-gate 	}
2806*7c478bd9Sstevel@tonic-gate }
2807*7c478bd9Sstevel@tonic-gate 
2808*7c478bd9Sstevel@tonic-gate /*
2809*7c478bd9Sstevel@tonic-gate  * This routine walks through the ill chain to find if there is any
2810*7c478bd9Sstevel@tonic-gate  * ire linked to the ill's interface based forwarding table
2811*7c478bd9Sstevel@tonic-gate  * The arg could be ill or mp. This routine is called when a ill goes
2812*7c478bd9Sstevel@tonic-gate  * down/deleted or the 'ipv4_ire_srcif_status' report is printed.
2813*7c478bd9Sstevel@tonic-gate  */
2814*7c478bd9Sstevel@tonic-gate void
2815*7c478bd9Sstevel@tonic-gate ire_walk_srcif_table_v4(pfv_t func, char *arg)
2816*7c478bd9Sstevel@tonic-gate {
2817*7c478bd9Sstevel@tonic-gate 	irb_t   *irb;
2818*7c478bd9Sstevel@tonic-gate 	ire_t   *ire;
2819*7c478bd9Sstevel@tonic-gate 	ill_t	*ill, *next_ill;
2820*7c478bd9Sstevel@tonic-gate 	int	i;
2821*7c478bd9Sstevel@tonic-gate 	int	total_count;
2822*7c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
2823*7c478bd9Sstevel@tonic-gate 
2824*7c478bd9Sstevel@tonic-gate 	/*
2825*7c478bd9Sstevel@tonic-gate 	 * Take care of ire's in other ill's per-interface forwarding
2826*7c478bd9Sstevel@tonic-gate 	 * table. Check if any ire in any of the ill's ill_srcif_table
2827*7c478bd9Sstevel@tonic-gate 	 * is pointing to this ill.
2828*7c478bd9Sstevel@tonic-gate 	 */
2829*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
2830*7c478bd9Sstevel@tonic-gate 	if (ire_srcif_table_count == 0) {
2831*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_srcif_table_lock);
2832*7c478bd9Sstevel@tonic-gate 		return;
2833*7c478bd9Sstevel@tonic-gate 	}
2834*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
2835*7c478bd9Sstevel@tonic-gate 
2836*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
2837*7c478bd9Sstevel@tonic-gate 	/* Keep accounting of all interface based table ires */
2838*7c478bd9Sstevel@tonic-gate 	total_count = 0;
2839*7c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
2840*7c478bd9Sstevel@tonic-gate 	ill = ILL_START_WALK_V4(&ctx);
2841*7c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
2842*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2843*7c478bd9Sstevel@tonic-gate 		total_count += ill->ill_srcif_refcnt;
2844*7c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
2845*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2846*7c478bd9Sstevel@tonic-gate 		ill = next_ill;
2847*7c478bd9Sstevel@tonic-gate 	}
2848*7c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
2849*7c478bd9Sstevel@tonic-gate 
2850*7c478bd9Sstevel@tonic-gate 	/* Hold lock here to make sure ire_srcif_table_count is stable */
2851*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
2852*7c478bd9Sstevel@tonic-gate 	i = ire_srcif_table_count;
2853*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
2854*7c478bd9Sstevel@tonic-gate 	ip1dbg(("ire_walk_srcif_v4: ire_srcif_table_count %d "
2855*7c478bd9Sstevel@tonic-gate 	    "total ill_srcif_refcnt %d\n", i, total_count));
2856*7c478bd9Sstevel@tonic-gate #endif
2857*7c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
2858*7c478bd9Sstevel@tonic-gate 	ill = ILL_START_WALK_V4(&ctx);
2859*7c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
2860*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2861*7c478bd9Sstevel@tonic-gate 		if ((ill->ill_srcif_refcnt == 0) || !ILL_CAN_LOOKUP(ill)) {
2862*7c478bd9Sstevel@tonic-gate 			next_ill = ill_next(&ctx, ill);
2863*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
2864*7c478bd9Sstevel@tonic-gate 			ill = next_ill;
2865*7c478bd9Sstevel@tonic-gate 			continue;
2866*7c478bd9Sstevel@tonic-gate 		}
2867*7c478bd9Sstevel@tonic-gate 		ill_refhold_locked(ill);
2868*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2869*7c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
2870*7c478bd9Sstevel@tonic-gate 		if (ill->ill_srcif_table != NULL) {
2871*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
2872*7c478bd9Sstevel@tonic-gate 				irb = &(ill->ill_srcif_table[i]);
2873*7c478bd9Sstevel@tonic-gate 				if (irb->irb_ire == NULL)
2874*7c478bd9Sstevel@tonic-gate 					continue;
2875*7c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(irb);
2876*7c478bd9Sstevel@tonic-gate 				for (ire = irb->irb_ire; ire != NULL;
2877*7c478bd9Sstevel@tonic-gate 				    ire = ire->ire_next) {
2878*7c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
2879*7c478bd9Sstevel@tonic-gate 				}
2880*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
2881*7c478bd9Sstevel@tonic-gate 			}
2882*7c478bd9Sstevel@tonic-gate 		}
2883*7c478bd9Sstevel@tonic-gate 		rw_enter(&ill_g_lock, RW_READER);
2884*7c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
2885*7c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
2886*7c478bd9Sstevel@tonic-gate 		ill = next_ill;
2887*7c478bd9Sstevel@tonic-gate 	}
2888*7c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
2889*7c478bd9Sstevel@tonic-gate }
2890*7c478bd9Sstevel@tonic-gate 
2891*7c478bd9Sstevel@tonic-gate /*
2892*7c478bd9Sstevel@tonic-gate  * This function takes a mask and returns
2893*7c478bd9Sstevel@tonic-gate  * number of bits set in the mask. If no
2894*7c478bd9Sstevel@tonic-gate  * bit is set it returns 0.
2895*7c478bd9Sstevel@tonic-gate  * Assumes a contiguous mask.
2896*7c478bd9Sstevel@tonic-gate  */
2897*7c478bd9Sstevel@tonic-gate int
2898*7c478bd9Sstevel@tonic-gate ip_mask_to_plen(ipaddr_t mask)
2899*7c478bd9Sstevel@tonic-gate {
2900*7c478bd9Sstevel@tonic-gate 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
2901*7c478bd9Sstevel@tonic-gate }
2902*7c478bd9Sstevel@tonic-gate 
2903*7c478bd9Sstevel@tonic-gate /*
2904*7c478bd9Sstevel@tonic-gate  * Convert length for a mask to the mask.
2905*7c478bd9Sstevel@tonic-gate  */
2906*7c478bd9Sstevel@tonic-gate ipaddr_t
2907*7c478bd9Sstevel@tonic-gate ip_plen_to_mask(uint_t masklen)
2908*7c478bd9Sstevel@tonic-gate {
2909*7c478bd9Sstevel@tonic-gate 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
2910*7c478bd9Sstevel@tonic-gate }
2911*7c478bd9Sstevel@tonic-gate 
2912*7c478bd9Sstevel@tonic-gate void
2913*7c478bd9Sstevel@tonic-gate ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
2914*7c478bd9Sstevel@tonic-gate {
2915*7c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
2916*7c478bd9Sstevel@tonic-gate 
2917*7c478bd9Sstevel@tonic-gate 	ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL;
2918*7c478bd9Sstevel@tonic-gate 	ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL;
2919*7c478bd9Sstevel@tonic-gate 	ill_list[2] = ire->ire_in_ill;
2920*7c478bd9Sstevel@tonic-gate 	ill_unlock_ills(ill_list, NUM_ILLS);
2921*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
2922*7c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_usesrc_lock);
2923*7c478bd9Sstevel@tonic-gate }
2924*7c478bd9Sstevel@tonic-gate 
2925*7c478bd9Sstevel@tonic-gate /*
2926*7c478bd9Sstevel@tonic-gate  * ire_add_v[46] atomically make sure that the ipif or ill associated
2927*7c478bd9Sstevel@tonic-gate  * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING
2928*7c478bd9Sstevel@tonic-gate  * before adding the ire to the table. This ensures that we don't create
2929*7c478bd9Sstevel@tonic-gate  * new IRE_CACHEs with stale values for parameters that are passed to
2930*7c478bd9Sstevel@tonic-gate  * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer
2931*7c478bd9Sstevel@tonic-gate  * to the ipif_mtu, and not the value. The actual value is derived from the
2932*7c478bd9Sstevel@tonic-gate  * parent ire or ipif under the bucket lock.
2933*7c478bd9Sstevel@tonic-gate  */
2934*7c478bd9Sstevel@tonic-gate int
2935*7c478bd9Sstevel@tonic-gate ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp,
2936*7c478bd9Sstevel@tonic-gate     ipsq_func_t func)
2937*7c478bd9Sstevel@tonic-gate {
2938*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
2939*7c478bd9Sstevel@tonic-gate 	ill_t	*ipif_ill;
2940*7c478bd9Sstevel@tonic-gate 	ill_t	*in_ill;
2941*7c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
2942*7c478bd9Sstevel@tonic-gate 	int	cnt = NUM_ILLS;
2943*7c478bd9Sstevel@tonic-gate 	int	error = 0;
2944*7c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
2945*7c478bd9Sstevel@tonic-gate 
2946*7c478bd9Sstevel@tonic-gate 	ill_list[0] = stq_ill = ire->ire_stq !=
2947*7c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_stq->q_ptr : NULL;
2948*7c478bd9Sstevel@tonic-gate 	ill_list[1] = ipif_ill = ire->ire_ipif !=
2949*7c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_ipif->ipif_ill : NULL;
2950*7c478bd9Sstevel@tonic-gate 	ill_list[2] = in_ill = ire->ire_in_ill;
2951*7c478bd9Sstevel@tonic-gate 
2952*7c478bd9Sstevel@tonic-gate 	ASSERT((q != NULL && mp != NULL && func != NULL) ||
2953*7c478bd9Sstevel@tonic-gate 	    (q == NULL && mp == NULL && func == NULL));
2954*7c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_usesrc_lock, RW_READER);
2955*7c478bd9Sstevel@tonic-gate 	GRAB_CONN_LOCK(q);
2956*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
2957*7c478bd9Sstevel@tonic-gate 	ill_lock_ills(ill_list, cnt);
2958*7c478bd9Sstevel@tonic-gate 
2959*7c478bd9Sstevel@tonic-gate 	/*
2960*7c478bd9Sstevel@tonic-gate 	 * While the IRE is in the process of being added, a user may have
2961*7c478bd9Sstevel@tonic-gate 	 * invoked the ifconfig usesrc option on the stq_ill to make it a
2962*7c478bd9Sstevel@tonic-gate 	 * usesrc client ILL. Check for this possibility here, if it is true
2963*7c478bd9Sstevel@tonic-gate 	 * then we fail adding the IRE_CACHE. Another check is to make sure
2964*7c478bd9Sstevel@tonic-gate 	 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc
2965*7c478bd9Sstevel@tonic-gate 	 * group. The ill_g_usesrc_lock is released in ire_atomic_end
2966*7c478bd9Sstevel@tonic-gate 	 */
2967*7c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHE) &&
2968*7c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_USESRC_CHECK)) {
2969*7c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_usesrc_ifindex != 0) {
2970*7c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_usesrc_grp_next != NULL);
2971*7c478bd9Sstevel@tonic-gate 			if ((ipif_ill->ill_phyint->phyint_ifindex !=
2972*7c478bd9Sstevel@tonic-gate 			    stq_ill->ill_usesrc_ifindex) ||
2973*7c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_grp_next == NULL) ||
2974*7c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_ifindex != 0)) {
2975*7c478bd9Sstevel@tonic-gate 				error = EINVAL;
2976*7c478bd9Sstevel@tonic-gate 				goto done;
2977*7c478bd9Sstevel@tonic-gate 			}
2978*7c478bd9Sstevel@tonic-gate 		} else if (ipif_ill->ill_usesrc_grp_next != NULL) {
2979*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
2980*7c478bd9Sstevel@tonic-gate 			goto done;
2981*7c478bd9Sstevel@tonic-gate 		}
2982*7c478bd9Sstevel@tonic-gate 	}
2983*7c478bd9Sstevel@tonic-gate 
2984*7c478bd9Sstevel@tonic-gate 	/*
2985*7c478bd9Sstevel@tonic-gate 	 * IPMP flag settings happen without taking the exclusive route
2986*7c478bd9Sstevel@tonic-gate 	 * in ip_sioctl_flags. So we need to make an atomic check here
2987*7c478bd9Sstevel@tonic-gate 	 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the
2988*7c478bd9Sstevel@tonic-gate 	 * FAILBACK=no case.
2989*7c478bd9Sstevel@tonic-gate 	 */
2990*7c478bd9Sstevel@tonic-gate 	if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) {
2991*7c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_state_flags & ILL_CHANGING) {
2992*7c478bd9Sstevel@tonic-gate 			ill = stq_ill;
2993*7c478bd9Sstevel@tonic-gate 			error = EAGAIN;
2994*7c478bd9Sstevel@tonic-gate 		} else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
2995*7c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
2996*7c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
2997*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
2998*7c478bd9Sstevel@tonic-gate 		}
2999*7c478bd9Sstevel@tonic-gate 		goto done;
3000*7c478bd9Sstevel@tonic-gate 	}
3001*7c478bd9Sstevel@tonic-gate 
3002*7c478bd9Sstevel@tonic-gate 	/*
3003*7c478bd9Sstevel@tonic-gate 	 * We don't check for OFFLINE/FAILED in this case because
3004*7c478bd9Sstevel@tonic-gate 	 * the source address selection logic (ipif_select_source)
3005*7c478bd9Sstevel@tonic-gate 	 * may still select a source address from such an ill. The
3006*7c478bd9Sstevel@tonic-gate 	 * assumption is that these addresses will be moved by in.mpathd
3007*7c478bd9Sstevel@tonic-gate 	 * soon. (i.e. this is a race). However link local addresses
3008*7c478bd9Sstevel@tonic-gate 	 * will not move and hence ipif_select_source_v6 tries to avoid
3009*7c478bd9Sstevel@tonic-gate 	 * FAILED ills. Please see ipif_select_source_v6 for more info
3010*7c478bd9Sstevel@tonic-gate 	 */
3011*7c478bd9Sstevel@tonic-gate 	if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) &&
3012*7c478bd9Sstevel@tonic-gate 	    (ipif_ill->ill_state_flags & ILL_CHANGING)) {
3013*7c478bd9Sstevel@tonic-gate 		ill = ipif_ill;
3014*7c478bd9Sstevel@tonic-gate 		error = EAGAIN;
3015*7c478bd9Sstevel@tonic-gate 		goto done;
3016*7c478bd9Sstevel@tonic-gate 	}
3017*7c478bd9Sstevel@tonic-gate 
3018*7c478bd9Sstevel@tonic-gate 	if ((in_ill != NULL) && !IAM_WRITER_ILL(in_ill) &&
3019*7c478bd9Sstevel@tonic-gate 	    (in_ill->ill_state_flags & ILL_CHANGING)) {
3020*7c478bd9Sstevel@tonic-gate 		ill = in_ill;
3021*7c478bd9Sstevel@tonic-gate 		error = EAGAIN;
3022*7c478bd9Sstevel@tonic-gate 		goto done;
3023*7c478bd9Sstevel@tonic-gate 	}
3024*7c478bd9Sstevel@tonic-gate 
3025*7c478bd9Sstevel@tonic-gate 	if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) &&
3026*7c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) {
3027*7c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
3028*7c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
3029*7c478bd9Sstevel@tonic-gate 		error = EAGAIN;
3030*7c478bd9Sstevel@tonic-gate 		goto done;
3031*7c478bd9Sstevel@tonic-gate 	}
3032*7c478bd9Sstevel@tonic-gate 
3033*7c478bd9Sstevel@tonic-gate done:
3034*7c478bd9Sstevel@tonic-gate 	if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) {
3035*7c478bd9Sstevel@tonic-gate 		ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
3036*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ipsq->ipsq_lock);
3037*7c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
3038*7c478bd9Sstevel@tonic-gate 		ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
3039*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ipsq->ipsq_lock);
3040*7c478bd9Sstevel@tonic-gate 		error = EINPROGRESS;
3041*7c478bd9Sstevel@tonic-gate 	} else if (error != 0) {
3042*7c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
3043*7c478bd9Sstevel@tonic-gate 	}
3044*7c478bd9Sstevel@tonic-gate 
3045*7c478bd9Sstevel@tonic-gate 	RELEASE_CONN_LOCK(q);
3046*7c478bd9Sstevel@tonic-gate 	return (error);
3047*7c478bd9Sstevel@tonic-gate }
3048*7c478bd9Sstevel@tonic-gate 
3049*7c478bd9Sstevel@tonic-gate /*
3050*7c478bd9Sstevel@tonic-gate  * Add a fully initialized IRE to an appropriate table based on
3051*7c478bd9Sstevel@tonic-gate  * ire_type.
3052*7c478bd9Sstevel@tonic-gate  */
3053*7c478bd9Sstevel@tonic-gate int
3054*7c478bd9Sstevel@tonic-gate ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func)
3055*7c478bd9Sstevel@tonic-gate {
3056*7c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
3057*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
3058*7c478bd9Sstevel@tonic-gate 	ill_t	*ill;
3059*7c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
3060*7c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
3061*7c478bd9Sstevel@tonic-gate 	ire_t	*ire = *irep;
3062*7c478bd9Sstevel@tonic-gate 	int	error;
3063*7c478bd9Sstevel@tonic-gate 
3064*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN);
3065*7c478bd9Sstevel@tonic-gate 
3066*7c478bd9Sstevel@tonic-gate 	/* get ready for the day when original ire is not created as mblk */
3067*7c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
3068*7c478bd9Sstevel@tonic-gate 		/* Copy the ire to a kmem_alloc'ed area */
3069*7c478bd9Sstevel@tonic-gate 		ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
3070*7c478bd9Sstevel@tonic-gate 		if (ire1 == NULL) {
3071*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add: alloc failed\n"));
3072*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3073*7c478bd9Sstevel@tonic-gate 			*irep = NULL;
3074*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
3075*7c478bd9Sstevel@tonic-gate 		}
3076*7c478bd9Sstevel@tonic-gate 		*ire1 = *ire;
3077*7c478bd9Sstevel@tonic-gate 		ire1->ire_mp = NULL;
3078*7c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
3079*7c478bd9Sstevel@tonic-gate 		ire = ire1;
3080*7c478bd9Sstevel@tonic-gate 	}
3081*7c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
3082*7c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
3083*7c478bd9Sstevel@tonic-gate 
3084*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
3085*7c478bd9Sstevel@tonic-gate 		/*
3086*7c478bd9Sstevel@tonic-gate 		 * If this interface is FAILED, or INACTIVE or has hit
3087*7c478bd9Sstevel@tonic-gate 		 * the FAILBACK=no case, we create IRE_CACHES marked
3088*7c478bd9Sstevel@tonic-gate 		 * HIDDEN for some special cases e.g. bind to
3089*7c478bd9Sstevel@tonic-gate 		 * IPIF_NOFAILOVER address etc. So, if this interface
3090*7c478bd9Sstevel@tonic-gate 		 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are
3091*7c478bd9Sstevel@tonic-gate 		 * not creating hidden ires, we should not allow that.
3092*7c478bd9Sstevel@tonic-gate 		 * This happens because the state of the interface
3093*7c478bd9Sstevel@tonic-gate 		 * changed while we were waiting in ARP. If this is the
3094*7c478bd9Sstevel@tonic-gate 		 * daemon sending probes, the next probe will create
3095*7c478bd9Sstevel@tonic-gate 		 * HIDDEN ires and we will create an ire then. This
3096*7c478bd9Sstevel@tonic-gate 		 * cannot happen with NDP currently because IRE is
3097*7c478bd9Sstevel@tonic-gate 		 * never queued in NDP. But it can happen in the
3098*7c478bd9Sstevel@tonic-gate 		 * future when we have external resolvers with IPv6.
3099*7c478bd9Sstevel@tonic-gate 		 * If the interface gets marked with OFFLINE while we
3100*7c478bd9Sstevel@tonic-gate 		 * are waiting in ARP, don't add the ire.
3101*7c478bd9Sstevel@tonic-gate 		 */
3102*7c478bd9Sstevel@tonic-gate 		if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
3103*7c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
3104*7c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
3105*7c478bd9Sstevel@tonic-gate 			/*
3106*7c478bd9Sstevel@tonic-gate 			 * We don't know whether it is a valid ipif or not.
3107*7c478bd9Sstevel@tonic-gate 			 * unless we do the check below. So, set it to NULL.
3108*7c478bd9Sstevel@tonic-gate 			 */
3109*7c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
3110*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3111*7c478bd9Sstevel@tonic-gate 			*irep = NULL;
3112*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
3113*7c478bd9Sstevel@tonic-gate 		}
3114*7c478bd9Sstevel@tonic-gate 	}
3115*7c478bd9Sstevel@tonic-gate 
3116*7c478bd9Sstevel@tonic-gate 	if (stq_ill != NULL && ire->ire_type == IRE_CACHE &&
3117*7c478bd9Sstevel@tonic-gate 	    stq_ill->ill_net_type == IRE_IF_RESOLVER) {
3118*7c478bd9Sstevel@tonic-gate 		rw_enter(&ill_g_lock, RW_READER);
3119*7c478bd9Sstevel@tonic-gate 		ill = ILL_START_WALK_ALL(&ctx);
3120*7c478bd9Sstevel@tonic-gate 		for (; ill != NULL; ill = ill_next(&ctx, ill)) {
3121*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
3122*7c478bd9Sstevel@tonic-gate 			if (ill->ill_state_flags & ILL_CONDEMNED) {
3123*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
3124*7c478bd9Sstevel@tonic-gate 				continue;
3125*7c478bd9Sstevel@tonic-gate 			}
3126*7c478bd9Sstevel@tonic-gate 			/*
3127*7c478bd9Sstevel@tonic-gate 			 * We need to make sure that the ipif is a valid one
3128*7c478bd9Sstevel@tonic-gate 			 * before adding the IRE_CACHE. This happens only
3129*7c478bd9Sstevel@tonic-gate 			 * with IRE_CACHE when there is an external resolver.
3130*7c478bd9Sstevel@tonic-gate 			 *
3131*7c478bd9Sstevel@tonic-gate 			 * We can unplumb a logical interface while the
3132*7c478bd9Sstevel@tonic-gate 			 * packet is waiting in ARP with the IRE. Then,
3133*7c478bd9Sstevel@tonic-gate 			 * later on when we feed the IRE back, the ipif
3134*7c478bd9Sstevel@tonic-gate 			 * has to be re-checked. This can't happen with
3135*7c478bd9Sstevel@tonic-gate 			 * NDP currently, as we never queue the IRE with
3136*7c478bd9Sstevel@tonic-gate 			 * the packet. We always try to recreate the IRE
3137*7c478bd9Sstevel@tonic-gate 			 * when the resolution is completed. But, we do
3138*7c478bd9Sstevel@tonic-gate 			 * it for IPv6 also here so that in future if
3139*7c478bd9Sstevel@tonic-gate 			 * we have external resolvers, it will work without
3140*7c478bd9Sstevel@tonic-gate 			 * any change.
3141*7c478bd9Sstevel@tonic-gate 			 */
3142*7c478bd9Sstevel@tonic-gate 			ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid);
3143*7c478bd9Sstevel@tonic-gate 			if (ipif != NULL) {
3144*7c478bd9Sstevel@tonic-gate 				ipif_refhold_locked(ipif);
3145*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
3146*7c478bd9Sstevel@tonic-gate 				break;
3147*7c478bd9Sstevel@tonic-gate 			}
3148*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
3149*7c478bd9Sstevel@tonic-gate 		}
3150*7c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
3151*7c478bd9Sstevel@tonic-gate 		if (ipif == NULL ||
3152*7c478bd9Sstevel@tonic-gate 		    (ipif->ipif_isv6 &&
3153*7c478bd9Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
3154*7c478bd9Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr)) ||
3155*7c478bd9Sstevel@tonic-gate 		    (!ipif->ipif_isv6 &&
3156*7c478bd9Sstevel@tonic-gate 		    ire->ire_src_addr != ipif->ipif_src_addr) ||
3157*7c478bd9Sstevel@tonic-gate 		    (ire->ire_zoneid != ipif->ipif_zoneid)) {
3158*7c478bd9Sstevel@tonic-gate 
3159*7c478bd9Sstevel@tonic-gate 			if (ipif != NULL)
3160*7c478bd9Sstevel@tonic-gate 				ipif_refrele(ipif);
3161*7c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
3162*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3163*7c478bd9Sstevel@tonic-gate 			*irep = NULL;
3164*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
3165*7c478bd9Sstevel@tonic-gate 		}
3166*7c478bd9Sstevel@tonic-gate 
3167*7c478bd9Sstevel@tonic-gate 
3168*7c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
3169*7c478bd9Sstevel@tonic-gate 		/*
3170*7c478bd9Sstevel@tonic-gate 		 * If this group was dismantled while this packets was
3171*7c478bd9Sstevel@tonic-gate 		 * queued in ARP, don't add it here.
3172*7c478bd9Sstevel@tonic-gate 		 */
3173*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) {
3174*7c478bd9Sstevel@tonic-gate 			/* We don't want ire_inactive bump stats for this */
3175*7c478bd9Sstevel@tonic-gate 			ipif_refrele(ipif);
3176*7c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
3177*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3178*7c478bd9Sstevel@tonic-gate 			*irep = NULL;
3179*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
3180*7c478bd9Sstevel@tonic-gate 		}
3181*7c478bd9Sstevel@tonic-gate 	}
3182*7c478bd9Sstevel@tonic-gate 
3183*7c478bd9Sstevel@tonic-gate 	/*
3184*7c478bd9Sstevel@tonic-gate 	 * In case ire was changed
3185*7c478bd9Sstevel@tonic-gate 	 */
3186*7c478bd9Sstevel@tonic-gate 	*irep = ire;
3187*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3188*7c478bd9Sstevel@tonic-gate 		error = ire_add_v6(irep, q, mp, func);
3189*7c478bd9Sstevel@tonic-gate 	} else {
3190*7c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL)
3191*7c478bd9Sstevel@tonic-gate 			error = ire_add_v4(irep, q, mp, func);
3192*7c478bd9Sstevel@tonic-gate 		else
3193*7c478bd9Sstevel@tonic-gate 			error = ire_add_srcif_v4(irep, q, mp, func);
3194*7c478bd9Sstevel@tonic-gate 	}
3195*7c478bd9Sstevel@tonic-gate 	if (ipif != NULL)
3196*7c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
3197*7c478bd9Sstevel@tonic-gate 	return (error);
3198*7c478bd9Sstevel@tonic-gate }
3199*7c478bd9Sstevel@tonic-gate 
3200*7c478bd9Sstevel@tonic-gate /*
3201*7c478bd9Sstevel@tonic-gate  * Add a fully initialized IRE to an appropriate
3202*7c478bd9Sstevel@tonic-gate  * table based on ire_type.
3203*7c478bd9Sstevel@tonic-gate  *
3204*7c478bd9Sstevel@tonic-gate  * The forward table contains IRE_PREFIX/IRE_HOST/IRE_HOST_REDIRECT
3205*7c478bd9Sstevel@tonic-gate  * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT.
3206*7c478bd9Sstevel@tonic-gate  *
3207*7c478bd9Sstevel@tonic-gate  * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK
3208*7c478bd9Sstevel@tonic-gate  * and IRE_CACHE.
3209*7c478bd9Sstevel@tonic-gate  *
3210*7c478bd9Sstevel@tonic-gate  * NOTE : This function is called as writer though not required
3211*7c478bd9Sstevel@tonic-gate  * by this function.
3212*7c478bd9Sstevel@tonic-gate  */
3213*7c478bd9Sstevel@tonic-gate static int
3214*7c478bd9Sstevel@tonic-gate ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
3215*7c478bd9Sstevel@tonic-gate {
3216*7c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
3217*7c478bd9Sstevel@tonic-gate 	int	mask_table_index;
3218*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
3219*7c478bd9Sstevel@tonic-gate 	ire_t	**irep;
3220*7c478bd9Sstevel@tonic-gate 	int	flags;
3221*7c478bd9Sstevel@tonic-gate 	ire_t	*pire = NULL;
3222*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
3223*7c478bd9Sstevel@tonic-gate 	ire_t	*ire = *ire_p;
3224*7c478bd9Sstevel@tonic-gate 	int	error;
3225*7c478bd9Sstevel@tonic-gate 
3226*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
3227*7c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
3228*7c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
3229*7c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(
3230*7c478bd9Sstevel@tonic-gate 		    &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock));
3231*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
3232*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */
3233*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill == NULL); /* No srcif entries */
3234*7c478bd9Sstevel@tonic-gate 
3235*7c478bd9Sstevel@tonic-gate 	/* Find the appropriate list head. */
3236*7c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
3237*7c478bd9Sstevel@tonic-gate 	case IRE_HOST:
3238*7c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
3239*7c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
3240*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3241*7c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
3242*7c478bd9Sstevel@tonic-gate 		break;
3243*7c478bd9Sstevel@tonic-gate 	case IRE_HOST_REDIRECT:
3244*7c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
3245*7c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
3246*7c478bd9Sstevel@tonic-gate 		ire->ire_src_addr = 0;
3247*7c478bd9Sstevel@tonic-gate 		break;
3248*7c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
3249*7c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
3250*7c478bd9Sstevel@tonic-gate 	case IRE_LOCAL:
3251*7c478bd9Sstevel@tonic-gate 	case IRE_LOOPBACK:
3252*7c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
3253*7c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
3254*7c478bd9Sstevel@tonic-gate 		break;
3255*7c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
3256*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3257*7c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
3258*7c478bd9Sstevel@tonic-gate 		break;
3259*7c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
3260*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3261*7c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
3262*7c478bd9Sstevel@tonic-gate 		break;
3263*7c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
3264*7c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
3265*7c478bd9Sstevel@tonic-gate 		break;
3266*7c478bd9Sstevel@tonic-gate 	default:
3267*7c478bd9Sstevel@tonic-gate 		printf("ire_add_v4: ire %p has unrecognized IRE type (%d)\n",
3268*7c478bd9Sstevel@tonic-gate 		    (void *)ire, ire->ire_type);
3269*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
3270*7c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
3271*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
3272*7c478bd9Sstevel@tonic-gate 	}
3273*7c478bd9Sstevel@tonic-gate 
3274*7c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
3275*7c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
3276*7c478bd9Sstevel@tonic-gate 
3277*7c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0) {
3278*7c478bd9Sstevel@tonic-gate 		/* IRE goes into Forward Table */
3279*7c478bd9Sstevel@tonic-gate 		mask_table_index = ire->ire_masklen;
3280*7c478bd9Sstevel@tonic-gate 		if ((ip_forwarding_table[mask_table_index]) == NULL) {
3281*7c478bd9Sstevel@tonic-gate 			irb_t *ptr;
3282*7c478bd9Sstevel@tonic-gate 			int i;
3283*7c478bd9Sstevel@tonic-gate 
3284*7c478bd9Sstevel@tonic-gate 			ptr = (irb_t *)mi_zalloc((ip_ftable_hash_size *
3285*7c478bd9Sstevel@tonic-gate 			    sizeof (irb_t)));
3286*7c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {
3287*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
3288*7c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
3289*7c478bd9Sstevel@tonic-gate 				return (ENOMEM);
3290*7c478bd9Sstevel@tonic-gate 			}
3291*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < ip_ftable_hash_size; i++) {
3292*7c478bd9Sstevel@tonic-gate 				rw_init(&ptr[i].irb_lock, NULL,
3293*7c478bd9Sstevel@tonic-gate 				    RW_DEFAULT, NULL);
3294*7c478bd9Sstevel@tonic-gate 			}
3295*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ire_ft_init_lock);
3296*7c478bd9Sstevel@tonic-gate 			if (ip_forwarding_table[mask_table_index] == NULL) {
3297*7c478bd9Sstevel@tonic-gate 				ip_forwarding_table[mask_table_index] = ptr;
3298*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_ft_init_lock);
3299*7c478bd9Sstevel@tonic-gate 			} else {
3300*7c478bd9Sstevel@tonic-gate 				/*
3301*7c478bd9Sstevel@tonic-gate 				 * Some other thread won the race in
3302*7c478bd9Sstevel@tonic-gate 				 * initializing the forwarding table at the
3303*7c478bd9Sstevel@tonic-gate 				 * same index.
3304*7c478bd9Sstevel@tonic-gate 				 */
3305*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_ft_init_lock);
3306*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < ip_ftable_hash_size; i++) {
3307*7c478bd9Sstevel@tonic-gate 					rw_destroy(&ptr[i].irb_lock);
3308*7c478bd9Sstevel@tonic-gate 				}
3309*7c478bd9Sstevel@tonic-gate 				mi_free(ptr);
3310*7c478bd9Sstevel@tonic-gate 			}
3311*7c478bd9Sstevel@tonic-gate 		}
3312*7c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[mask_table_index][
3313*7c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(ire->ire_addr, ip_ftable_hash_size)]);
3314*7c478bd9Sstevel@tonic-gate 	} else {
3315*7c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_cache_table[IRE_ADDR_HASH(ire->ire_addr,
3316*7c478bd9Sstevel@tonic-gate 		    ip_cache_table_size)]);
3317*7c478bd9Sstevel@tonic-gate 	}
3318*7c478bd9Sstevel@tonic-gate 	/*
3319*7c478bd9Sstevel@tonic-gate 	 * ip_newroute/ip_newroute_multi are unable to prevent the deletion
3320*7c478bd9Sstevel@tonic-gate 	 * of the interface route while adding an IRE_CACHE for an on-link
3321*7c478bd9Sstevel@tonic-gate 	 * destination in the IRE_IF_RESOLVER case, since the ire has to
3322*7c478bd9Sstevel@tonic-gate 	 * go to ARP and return. We can't do a REFHOLD on the
3323*7c478bd9Sstevel@tonic-gate 	 * associated interface ire for fear of ARP freeing the message.
3324*7c478bd9Sstevel@tonic-gate 	 * Here we look up the interface ire in the forwarding table and
3325*7c478bd9Sstevel@tonic-gate 	 * make sure that the interface route has not been deleted.
3326*7c478bd9Sstevel@tonic-gate 	 */
3327*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 &&
3328*7c478bd9Sstevel@tonic-gate 	    ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) {
3329*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp == NULL);
3330*7c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) {
3331*7c478bd9Sstevel@tonic-gate 			/*
3332*7c478bd9Sstevel@tonic-gate 			 * The ihandle that we used in ip_newroute_multi
3333*7c478bd9Sstevel@tonic-gate 			 * comes from the interface route corresponding
3334*7c478bd9Sstevel@tonic-gate 			 * to ire_ipif. Lookup here to see if it exists
3335*7c478bd9Sstevel@tonic-gate 			 * still.
3336*7c478bd9Sstevel@tonic-gate 			 * If the ire has a source address assigned using
3337*7c478bd9Sstevel@tonic-gate 			 * RTF_SETSRC, ire_ipif is the logical interface holding
3338*7c478bd9Sstevel@tonic-gate 			 * this source address, so we can't use it to check for
3339*7c478bd9Sstevel@tonic-gate 			 * the existence of the interface route. Instead we rely
3340*7c478bd9Sstevel@tonic-gate 			 * on the brute force ihandle search in
3341*7c478bd9Sstevel@tonic-gate 			 * ire_ihandle_lookup_onlink() below.
3342*7c478bd9Sstevel@tonic-gate 			 */
3343*7c478bd9Sstevel@tonic-gate 			pire = ipif_to_ire(ire->ire_ipif);
3344*7c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
3345*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
3346*7c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
3347*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
3348*7c478bd9Sstevel@tonic-gate 			} else if (pire->ire_ihandle != ire->ire_ihandle) {
3349*7c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
3350*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
3351*7c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
3352*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
3353*7c478bd9Sstevel@tonic-gate 			}
3354*7c478bd9Sstevel@tonic-gate 		} else {
3355*7c478bd9Sstevel@tonic-gate 			pire = ire_ihandle_lookup_onlink(ire);
3356*7c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
3357*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
3358*7c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
3359*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
3360*7c478bd9Sstevel@tonic-gate 			}
3361*7c478bd9Sstevel@tonic-gate 		}
3362*7c478bd9Sstevel@tonic-gate 		/* Prevent pire from getting deleted */
3363*7c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(pire->ire_bucket);
3364*7c478bd9Sstevel@tonic-gate 		/* Has it been removed already ? */
3365*7c478bd9Sstevel@tonic-gate 		if (pire->ire_marks & IRE_MARK_CONDEMNED) {
3366*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
3367*7c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
3368*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3369*7c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
3370*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
3371*7c478bd9Sstevel@tonic-gate 		}
3372*7c478bd9Sstevel@tonic-gate 	} else {
3373*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp != NULL);
3374*7c478bd9Sstevel@tonic-gate 	}
3375*7c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
3376*7c478bd9Sstevel@tonic-gate 
3377*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
3378*7c478bd9Sstevel@tonic-gate 		/*
3379*7c478bd9Sstevel@tonic-gate 		 * We use MATCH_IRE_IPIF while adding IRE_CACHES only
3380*7c478bd9Sstevel@tonic-gate 		 * for historic reasons and to maintain symmetry with
3381*7c478bd9Sstevel@tonic-gate 		 * IPv6 code path. Historically this was used by
3382*7c478bd9Sstevel@tonic-gate 		 * multicast code to create multiple IRE_CACHES on
3383*7c478bd9Sstevel@tonic-gate 		 * a single ill with different ipifs. This was used
3384*7c478bd9Sstevel@tonic-gate 		 * so that multicast packets leaving the node had the
3385*7c478bd9Sstevel@tonic-gate 		 * right source address. This is no longer needed as
3386*7c478bd9Sstevel@tonic-gate 		 * ip_wput initializes the address correctly.
3387*7c478bd9Sstevel@tonic-gate 		 */
3388*7c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_IPIF;
3389*7c478bd9Sstevel@tonic-gate 		/*
3390*7c478bd9Sstevel@tonic-gate 		 * If we are creating hidden ires, make sure we search on
3391*7c478bd9Sstevel@tonic-gate 		 * this ill (MATCH_IRE_ILL) and a hidden ire,
3392*7c478bd9Sstevel@tonic-gate 		 * while we are searching for duplicates below. Otherwise we
3393*7c478bd9Sstevel@tonic-gate 		 * could potentially find an IRE on some other interface
3394*7c478bd9Sstevel@tonic-gate 		 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We
3395*7c478bd9Sstevel@tonic-gate 		 * shouldn't do this as this will lead to an infinite loop
3396*7c478bd9Sstevel@tonic-gate 		 * (if we get to ip_wput again) eventually we need an hidden
3397*7c478bd9Sstevel@tonic-gate 		 * ire for this packet to go out. MATCH_IRE_ILL is explicitly
3398*7c478bd9Sstevel@tonic-gate 		 * done below.
3399*7c478bd9Sstevel@tonic-gate 		 */
3400*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_CACHE &&
3401*7c478bd9Sstevel@tonic-gate 		    (ire->ire_marks & IRE_MARK_HIDDEN))
3402*7c478bd9Sstevel@tonic-gate 			flags |= (MATCH_IRE_MARK_HIDDEN);
3403*7c478bd9Sstevel@tonic-gate 	}
3404*7c478bd9Sstevel@tonic-gate 
3405*7c478bd9Sstevel@tonic-gate 	/*
3406*7c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
3407*7c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock. Check for condemned
3408*7c478bd9Sstevel@tonic-gate 	 *
3409*7c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
3410*7c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
3411*7c478bd9Sstevel@tonic-gate 	 */
3412*7c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
3413*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
3414*7c478bd9Sstevel@tonic-gate 		/*
3415*7c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
3416*7c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
3417*7c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
3418*7c478bd9Sstevel@tonic-gate 		 */
3419*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
3420*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
3421*7c478bd9Sstevel@tonic-gate 		if (pire != NULL) {
3422*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
3423*7c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
3424*7c478bd9Sstevel@tonic-gate 		}
3425*7c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
3426*7c478bd9Sstevel@tonic-gate 		return (error);
3427*7c478bd9Sstevel@tonic-gate 	}
3428*7c478bd9Sstevel@tonic-gate 	/*
3429*7c478bd9Sstevel@tonic-gate 	 * To avoid creating ires having stale values for the ire_max_frag
3430*7c478bd9Sstevel@tonic-gate 	 * we get the latest value atomically here. For more details
3431*7c478bd9Sstevel@tonic-gate 	 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE
3432*7c478bd9Sstevel@tonic-gate 	 * in ip_rput_dlpi_writer
3433*7c478bd9Sstevel@tonic-gate 	 */
3434*7c478bd9Sstevel@tonic-gate 	if (ire->ire_max_fragp == NULL) {
3435*7c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr))
3436*7c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
3437*7c478bd9Sstevel@tonic-gate 		else
3438*7c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = pire->ire_max_frag;
3439*7c478bd9Sstevel@tonic-gate 	} else {
3440*7c478bd9Sstevel@tonic-gate 		uint_t	max_frag;
3441*7c478bd9Sstevel@tonic-gate 
3442*7c478bd9Sstevel@tonic-gate 		max_frag = *ire->ire_max_fragp;
3443*7c478bd9Sstevel@tonic-gate 		ire->ire_max_fragp = NULL;
3444*7c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = max_frag;
3445*7c478bd9Sstevel@tonic-gate 	}
3446*7c478bd9Sstevel@tonic-gate 	/*
3447*7c478bd9Sstevel@tonic-gate 	 * Atomically check for duplicate and insert in the table.
3448*7c478bd9Sstevel@tonic-gate 	 */
3449*7c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
3450*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
3451*7c478bd9Sstevel@tonic-gate 			continue;
3452*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL) {
3453*7c478bd9Sstevel@tonic-gate 			/*
3454*7c478bd9Sstevel@tonic-gate 			 * We do MATCH_IRE_ILL implicitly here for IREs
3455*7c478bd9Sstevel@tonic-gate 			 * with a non-null ire_ipif, including IRE_CACHEs.
3456*7c478bd9Sstevel@tonic-gate 			 * As ire_ipif and ire_stq could point to two
3457*7c478bd9Sstevel@tonic-gate 			 * different ills, we can't pass just ire_ipif to
3458*7c478bd9Sstevel@tonic-gate 			 * ire_match_args and get a match on both ills.
3459*7c478bd9Sstevel@tonic-gate 			 * This is just needed for duplicate checks here and
3460*7c478bd9Sstevel@tonic-gate 			 * so we don't add an extra argument to
3461*7c478bd9Sstevel@tonic-gate 			 * ire_match_args for this. Do it locally.
3462*7c478bd9Sstevel@tonic-gate 			 *
3463*7c478bd9Sstevel@tonic-gate 			 * NOTE : Currently there is no part of the code
3464*7c478bd9Sstevel@tonic-gate 			 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL
3465*7c478bd9Sstevel@tonic-gate 			 * match for IRE_CACHEs. Thus we don't want to
3466*7c478bd9Sstevel@tonic-gate 			 * extend the arguments to ire_match_args.
3467*7c478bd9Sstevel@tonic-gate 			 */
3468*7c478bd9Sstevel@tonic-gate 			if (ire1->ire_stq != ire->ire_stq)
3469*7c478bd9Sstevel@tonic-gate 				continue;
3470*7c478bd9Sstevel@tonic-gate 			/*
3471*7c478bd9Sstevel@tonic-gate 			 * Multiroute IRE_CACHEs for a given destination can
3472*7c478bd9Sstevel@tonic-gate 			 * have the same ire_ipif, typically if their source
3473*7c478bd9Sstevel@tonic-gate 			 * address is forced using RTF_SETSRC, and the same
3474*7c478bd9Sstevel@tonic-gate 			 * send-to queue. We differentiate them using the parent
3475*7c478bd9Sstevel@tonic-gate 			 * handle.
3476*7c478bd9Sstevel@tonic-gate 			 */
3477*7c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_CACHE &&
3478*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_flags & RTF_MULTIRT) &&
3479*7c478bd9Sstevel@tonic-gate 			    (ire->ire_flags & RTF_MULTIRT) &&
3480*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_phandle != ire->ire_phandle))
3481*7c478bd9Sstevel@tonic-gate 				continue;
3482*7c478bd9Sstevel@tonic-gate 		}
3483*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
3484*7c478bd9Sstevel@tonic-gate 			continue;
3485*7c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
3486*7c478bd9Sstevel@tonic-gate 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif,
3487*7c478bd9Sstevel@tonic-gate 		    ire->ire_zoneid, 0, flags)) {
3488*7c478bd9Sstevel@tonic-gate 			/*
3489*7c478bd9Sstevel@tonic-gate 			 * Return the old ire after doing a REFHOLD.
3490*7c478bd9Sstevel@tonic-gate 			 * As most of the callers continue to use the IRE
3491*7c478bd9Sstevel@tonic-gate 			 * after adding, we return a held ire. This will
3492*7c478bd9Sstevel@tonic-gate 			 * avoid a lookup in the caller again. If the callers
3493*7c478bd9Sstevel@tonic-gate 			 * don't want to use it, they need to do a REFRELE.
3494*7c478bd9Sstevel@tonic-gate 			 */
3495*7c478bd9Sstevel@tonic-gate 			ip1dbg(("found dup ire existing %p new %p",
3496*7c478bd9Sstevel@tonic-gate 			    (void *)ire1, (void *)ire));
3497*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
3498*7c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
3499*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
3500*7c478bd9Sstevel@tonic-gate 			if (pire != NULL) {
3501*7c478bd9Sstevel@tonic-gate 				/*
3502*7c478bd9Sstevel@tonic-gate 				 * Assert that it is not removed from the
3503*7c478bd9Sstevel@tonic-gate 				 * list yet.
3504*7c478bd9Sstevel@tonic-gate 				 */
3505*7c478bd9Sstevel@tonic-gate 				ASSERT(pire->ire_ptpn != NULL);
3506*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(pire->ire_bucket);
3507*7c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
3508*7c478bd9Sstevel@tonic-gate 			}
3509*7c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
3510*7c478bd9Sstevel@tonic-gate 			return (0);
3511*7c478bd9Sstevel@tonic-gate 		}
3512*7c478bd9Sstevel@tonic-gate 	}
3513*7c478bd9Sstevel@tonic-gate 
3514*7c478bd9Sstevel@tonic-gate 	/*
3515*7c478bd9Sstevel@tonic-gate 	 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by
3516*7c478bd9Sstevel@tonic-gate 	 * grouping identical addresses together on the hash chain. We also
3517*7c478bd9Sstevel@tonic-gate 	 * don't want to send multiple copies out if there are two ills part
3518*7c478bd9Sstevel@tonic-gate 	 * of the same group. Thus we group the ires with same addr and same
3519*7c478bd9Sstevel@tonic-gate 	 * ill group together so that ip_wput_ire can easily skip all the
3520*7c478bd9Sstevel@tonic-gate 	 * ires with same addr and same group after sending the first copy.
3521*7c478bd9Sstevel@tonic-gate 	 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently
3522*7c478bd9Sstevel@tonic-gate 	 * interested in such groupings only for broadcasts.
3523*7c478bd9Sstevel@tonic-gate 	 *
3524*7c478bd9Sstevel@tonic-gate 	 * NOTE : If the interfaces are brought up first and then grouped,
3525*7c478bd9Sstevel@tonic-gate 	 * illgrp_insert will handle it. We come here when the interfaces
3526*7c478bd9Sstevel@tonic-gate 	 * are already in group and we are bringing them UP.
3527*7c478bd9Sstevel@tonic-gate 	 *
3528*7c478bd9Sstevel@tonic-gate 	 * Find the first entry that matches ire_addr. *irep will be null
3529*7c478bd9Sstevel@tonic-gate 	 * if no match.
3530*7c478bd9Sstevel@tonic-gate 	 */
3531*7c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
3532*7c478bd9Sstevel@tonic-gate 	while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr)
3533*7c478bd9Sstevel@tonic-gate 		irep = &ire1->ire_next;
3534*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_BROADCAST && *irep != NULL) {
3535*7c478bd9Sstevel@tonic-gate 		/*
3536*7c478bd9Sstevel@tonic-gate 		 * We found some ire (i.e *irep) with a matching addr. We
3537*7c478bd9Sstevel@tonic-gate 		 * want to group ires with same addr and same ill group
3538*7c478bd9Sstevel@tonic-gate 		 * together.
3539*7c478bd9Sstevel@tonic-gate 		 *
3540*7c478bd9Sstevel@tonic-gate 		 * First get to the entry that matches our address and
3541*7c478bd9Sstevel@tonic-gate 		 * ill group i.e stop as soon as we find the first ire
3542*7c478bd9Sstevel@tonic-gate 		 * matching the ill group and address. If there is only
3543*7c478bd9Sstevel@tonic-gate 		 * an address match, we should walk and look for some
3544*7c478bd9Sstevel@tonic-gate 		 * group match. These are some of the possible scenarios :
3545*7c478bd9Sstevel@tonic-gate 		 *
3546*7c478bd9Sstevel@tonic-gate 		 * 1) There are no groups at all i.e all ire's ill_group
3547*7c478bd9Sstevel@tonic-gate 		 *    are NULL. In that case we will essentially group
3548*7c478bd9Sstevel@tonic-gate 		 *    all the ires with the same addr together. Same as
3549*7c478bd9Sstevel@tonic-gate 		 *    the "else" block of this "if".
3550*7c478bd9Sstevel@tonic-gate 		 *
3551*7c478bd9Sstevel@tonic-gate 		 * 2) There are some groups and this ire's ill_group is
3552*7c478bd9Sstevel@tonic-gate 		 *    NULL. In this case, we will first find the group
3553*7c478bd9Sstevel@tonic-gate 		 *    that matches the address and a NULL group. Then
3554*7c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
3555*7c478bd9Sstevel@tonic-gate 		 *
3556*7c478bd9Sstevel@tonic-gate 		 * 3) There are some groups and this ires's ill_group is
3557*7c478bd9Sstevel@tonic-gate 		 *    non-NULL. In this case we will first find the group
3558*7c478bd9Sstevel@tonic-gate 		 *    that matches the address and the ill_group. Then
3559*7c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
3560*7c478bd9Sstevel@tonic-gate 		 */
3561*7c478bd9Sstevel@tonic-gate 		/* LINTED : constant in conditional context */
3562*7c478bd9Sstevel@tonic-gate 		while (1) {
3563*7c478bd9Sstevel@tonic-gate 			ire1 = *irep;
3564*7c478bd9Sstevel@tonic-gate 			if ((ire1->ire_next == NULL) ||
3565*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_next->ire_addr != ire->ire_addr) ||
3566*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
3567*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group ==
3568*7c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
3569*7c478bd9Sstevel@tonic-gate 				break;
3570*7c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
3571*7c478bd9Sstevel@tonic-gate 		}
3572*7c478bd9Sstevel@tonic-gate 		ASSERT(*irep != NULL);
3573*7c478bd9Sstevel@tonic-gate 		irep = &((*irep)->ire_next);
3574*7c478bd9Sstevel@tonic-gate 
3575*7c478bd9Sstevel@tonic-gate 		/*
3576*7c478bd9Sstevel@tonic-gate 		 * Either we have hit the end of the list or the address
3577*7c478bd9Sstevel@tonic-gate 		 * did not match or the group *matched*. If we found
3578*7c478bd9Sstevel@tonic-gate 		 * a match on the group, skip to the end of the group.
3579*7c478bd9Sstevel@tonic-gate 		 */
3580*7c478bd9Sstevel@tonic-gate 		while (*irep != NULL) {
3581*7c478bd9Sstevel@tonic-gate 			ire1 = *irep;
3582*7c478bd9Sstevel@tonic-gate 			if ((ire1->ire_addr != ire->ire_addr) ||
3583*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
3584*7c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group !=
3585*7c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
3586*7c478bd9Sstevel@tonic-gate 				break;
3587*7c478bd9Sstevel@tonic-gate 			if (ire1->ire_ipif->ipif_ill->ill_group == NULL &&
3588*7c478bd9Sstevel@tonic-gate 			    ire1->ire_ipif == ire->ire_ipif) {
3589*7c478bd9Sstevel@tonic-gate 				irep = &ire1->ire_next;
3590*7c478bd9Sstevel@tonic-gate 				break;
3591*7c478bd9Sstevel@tonic-gate 			}
3592*7c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
3593*7c478bd9Sstevel@tonic-gate 		}
3594*7c478bd9Sstevel@tonic-gate 	} else if (*irep != NULL) {
3595*7c478bd9Sstevel@tonic-gate 		/*
3596*7c478bd9Sstevel@tonic-gate 		 * Find the last ire which matches ire_addr.
3597*7c478bd9Sstevel@tonic-gate 		 * Needed to do tail insertion among entries with the same
3598*7c478bd9Sstevel@tonic-gate 		 * ire_addr.
3599*7c478bd9Sstevel@tonic-gate 		 */
3600*7c478bd9Sstevel@tonic-gate 		while (ire->ire_addr == ire1->ire_addr) {
3601*7c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
3602*7c478bd9Sstevel@tonic-gate 			ire1 = *irep;
3603*7c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
3604*7c478bd9Sstevel@tonic-gate 				break;
3605*7c478bd9Sstevel@tonic-gate 		}
3606*7c478bd9Sstevel@tonic-gate 	}
3607*7c478bd9Sstevel@tonic-gate 
3608*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
3609*7c478bd9Sstevel@tonic-gate 		/*
3610*7c478bd9Sstevel@tonic-gate 		 * We keep a count of default gateways which is used when
3611*7c478bd9Sstevel@tonic-gate 		 * assigning them as routes.
3612*7c478bd9Sstevel@tonic-gate 		 */
3613*7c478bd9Sstevel@tonic-gate 		ip_ire_default_count++;
3614*7c478bd9Sstevel@tonic-gate 		ASSERT(ip_ire_default_count != 0); /* Wraparound */
3615*7c478bd9Sstevel@tonic-gate 	}
3616*7c478bd9Sstevel@tonic-gate 	/* Insert at *irep */
3617*7c478bd9Sstevel@tonic-gate 	ire1 = *irep;
3618*7c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
3619*7c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
3620*7c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
3621*7c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
3622*7c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
3623*7c478bd9Sstevel@tonic-gate 
3624*7c478bd9Sstevel@tonic-gate 	/*
3625*7c478bd9Sstevel@tonic-gate 	 * ire_walk routines de-reference ire_next without holding
3626*7c478bd9Sstevel@tonic-gate 	 * a lock. Before we point to the new ire, we want to make
3627*7c478bd9Sstevel@tonic-gate 	 * sure the store that sets the ire_next of the new ire
3628*7c478bd9Sstevel@tonic-gate 	 * reaches global visibility, so that ire_walk routines
3629*7c478bd9Sstevel@tonic-gate 	 * don't see a truncated list of ires i.e if the ire_next
3630*7c478bd9Sstevel@tonic-gate 	 * of the new ire gets set after we do "*irep = ire" due
3631*7c478bd9Sstevel@tonic-gate 	 * to re-ordering, the ire_walk thread will see a NULL
3632*7c478bd9Sstevel@tonic-gate 	 * once it accesses the ire_next of the new ire.
3633*7c478bd9Sstevel@tonic-gate 	 * membar_producer() makes sure that the following store
3634*7c478bd9Sstevel@tonic-gate 	 * happens *after* all of the above stores.
3635*7c478bd9Sstevel@tonic-gate 	 */
3636*7c478bd9Sstevel@tonic-gate 	membar_producer();
3637*7c478bd9Sstevel@tonic-gate 	*irep = ire;
3638*7c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
3639*7c478bd9Sstevel@tonic-gate 	/*
3640*7c478bd9Sstevel@tonic-gate 	 * We return a bumped up IRE above. Keep it symmetrical
3641*7c478bd9Sstevel@tonic-gate 	 * so that the callers will always have to release. This
3642*7c478bd9Sstevel@tonic-gate 	 * helps the callers of this function because they continue
3643*7c478bd9Sstevel@tonic-gate 	 * to use the IRE after adding and hence they don't have to
3644*7c478bd9Sstevel@tonic-gate 	 * lookup again after we return the IRE.
3645*7c478bd9Sstevel@tonic-gate 	 *
3646*7c478bd9Sstevel@tonic-gate 	 * NOTE : We don't have to use atomics as this is appearing
3647*7c478bd9Sstevel@tonic-gate 	 * in the list for the first time and no one else can bump
3648*7c478bd9Sstevel@tonic-gate 	 * up the reference count on this yet.
3649*7c478bd9Sstevel@tonic-gate 	 */
3650*7c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
3651*7c478bd9Sstevel@tonic-gate 	BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
3652*7c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
3653*7c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
3654*7c478bd9Sstevel@tonic-gate 		irb_ptr->irb_tmp_ire_cnt++;
3655*7c478bd9Sstevel@tonic-gate 
3656*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
3657*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
3658*7c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
3659*7c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
3660*7c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
3661*7c478bd9Sstevel@tonic-gate 		}
3662*7c478bd9Sstevel@tonic-gate 	} else {
3663*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
3664*7c478bd9Sstevel@tonic-gate 	}
3665*7c478bd9Sstevel@tonic-gate 
3666*7c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
3667*7c478bd9Sstevel@tonic-gate 
3668*7c478bd9Sstevel@tonic-gate 	if (pire != NULL) {
3669*7c478bd9Sstevel@tonic-gate 		/* Assert that it is not removed from the list yet */
3670*7c478bd9Sstevel@tonic-gate 		ASSERT(pire->ire_ptpn != NULL);
3671*7c478bd9Sstevel@tonic-gate 		IRB_REFRELE(pire->ire_bucket);
3672*7c478bd9Sstevel@tonic-gate 		ire_refrele(pire);
3673*7c478bd9Sstevel@tonic-gate 	}
3674*7c478bd9Sstevel@tonic-gate 
3675*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE) {
3676*7c478bd9Sstevel@tonic-gate 		/*
3677*7c478bd9Sstevel@tonic-gate 		 * For ire's with with host mask see if there is an entry
3678*7c478bd9Sstevel@tonic-gate 		 * in the cache. If there is one flush the whole cache as
3679*7c478bd9Sstevel@tonic-gate 		 * there might be multiple entries due to RTF_MULTIRT (CGTP).
3680*7c478bd9Sstevel@tonic-gate 		 * If no entry is found than there is no need to flush the
3681*7c478bd9Sstevel@tonic-gate 		 * cache.
3682*7c478bd9Sstevel@tonic-gate 		 */
3683*7c478bd9Sstevel@tonic-gate 		if (ire->ire_mask == IP_HOST_MASK) {
3684*7c478bd9Sstevel@tonic-gate 			ire_t *lire;
3685*7c478bd9Sstevel@tonic-gate 			lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE,
3686*7c478bd9Sstevel@tonic-gate 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
3687*7c478bd9Sstevel@tonic-gate 			if (lire != NULL) {
3688*7c478bd9Sstevel@tonic-gate 				ire_refrele(lire);
3689*7c478bd9Sstevel@tonic-gate 				ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
3690*7c478bd9Sstevel@tonic-gate 			}
3691*7c478bd9Sstevel@tonic-gate 		} else {
3692*7c478bd9Sstevel@tonic-gate 			ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
3693*7c478bd9Sstevel@tonic-gate 		}
3694*7c478bd9Sstevel@tonic-gate 	}
3695*7c478bd9Sstevel@tonic-gate 	/*
3696*7c478bd9Sstevel@tonic-gate 	 * We had to delay the fast path probe until the ire is inserted
3697*7c478bd9Sstevel@tonic-gate 	 * in the list. Otherwise the fast path ack won't find the ire in
3698*7c478bd9Sstevel@tonic-gate 	 * the table.
3699*7c478bd9Sstevel@tonic-gate 	 */
3700*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE || ire->ire_type == IRE_BROADCAST)
3701*7c478bd9Sstevel@tonic-gate 		ire_fastpath(ire);
3702*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
3703*7c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
3704*7c478bd9Sstevel@tonic-gate 	*ire_p = ire;
3705*7c478bd9Sstevel@tonic-gate 	return (0);
3706*7c478bd9Sstevel@tonic-gate }
3707*7c478bd9Sstevel@tonic-gate 
3708*7c478bd9Sstevel@tonic-gate /*
3709*7c478bd9Sstevel@tonic-gate  * Search for all HOST REDIRECT routes that are
3710*7c478bd9Sstevel@tonic-gate  * pointing at the specified gateway and
3711*7c478bd9Sstevel@tonic-gate  * delete them. This routine is called only
3712*7c478bd9Sstevel@tonic-gate  * when a default gateway is going away.
3713*7c478bd9Sstevel@tonic-gate  */
3714*7c478bd9Sstevel@tonic-gate static void
3715*7c478bd9Sstevel@tonic-gate ire_delete_host_redirects(ipaddr_t gateway)
3716*7c478bd9Sstevel@tonic-gate {
3717*7c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
3718*7c478bd9Sstevel@tonic-gate 	irb_t *irb;
3719*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
3720*7c478bd9Sstevel@tonic-gate 	int i;
3721*7c478bd9Sstevel@tonic-gate 
3722*7c478bd9Sstevel@tonic-gate 	/* get the hash table for HOST routes */
3723*7c478bd9Sstevel@tonic-gate 	irb_ptr = ip_forwarding_table[(IP_MASK_TABLE_SIZE - 1)];
3724*7c478bd9Sstevel@tonic-gate 	if (irb_ptr == NULL)
3725*7c478bd9Sstevel@tonic-gate 		return;
3726*7c478bd9Sstevel@tonic-gate 	for (i = 0; (i < ip_ftable_hash_size); i++) {
3727*7c478bd9Sstevel@tonic-gate 		irb = &irb_ptr[i];
3728*7c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb);
3729*7c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
3730*7c478bd9Sstevel@tonic-gate 			if (ire->ire_type != IRE_HOST_REDIRECT)
3731*7c478bd9Sstevel@tonic-gate 				continue;
3732*7c478bd9Sstevel@tonic-gate 			if (ire->ire_gateway_addr == gateway) {
3733*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
3734*7c478bd9Sstevel@tonic-gate 			}
3735*7c478bd9Sstevel@tonic-gate 		}
3736*7c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb);
3737*7c478bd9Sstevel@tonic-gate 	}
3738*7c478bd9Sstevel@tonic-gate }
3739*7c478bd9Sstevel@tonic-gate 
3740*7c478bd9Sstevel@tonic-gate /*
3741*7c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. ire_unlink calls to
3742*7c478bd9Sstevel@tonic-gate  * do the final cleanup for this ire.
3743*7c478bd9Sstevel@tonic-gate  */
3744*7c478bd9Sstevel@tonic-gate void
3745*7c478bd9Sstevel@tonic-gate ire_cleanup(ire_t *ire)
3746*7c478bd9Sstevel@tonic-gate {
3747*7c478bd9Sstevel@tonic-gate 	ire_t *ire_next;
3748*7c478bd9Sstevel@tonic-gate 
3749*7c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
3750*7c478bd9Sstevel@tonic-gate 
3751*7c478bd9Sstevel@tonic-gate 	while (ire != NULL) {
3752*7c478bd9Sstevel@tonic-gate 		ire_next = ire->ire_next;
3753*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
3754*7c478bd9Sstevel@tonic-gate 			ire_delete_v4(ire);
3755*7c478bd9Sstevel@tonic-gate 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_deleted);
3756*7c478bd9Sstevel@tonic-gate 		} else {
3757*7c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
3758*7c478bd9Sstevel@tonic-gate 			ire_delete_v6(ire);
3759*7c478bd9Sstevel@tonic-gate 			BUMP_IRE_STATS(ire_stats_v6, ire_stats_deleted);
3760*7c478bd9Sstevel@tonic-gate 		}
3761*7c478bd9Sstevel@tonic-gate 		/*
3762*7c478bd9Sstevel@tonic-gate 		 * Now it's really out of the list. Before doing the
3763*7c478bd9Sstevel@tonic-gate 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
3764*7c478bd9Sstevel@tonic-gate 		 * so.
3765*7c478bd9Sstevel@tonic-gate 		 */
3766*7c478bd9Sstevel@tonic-gate 		ire->ire_next = NULL;
3767*7c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
3768*7c478bd9Sstevel@tonic-gate 		ire = ire_next;
3769*7c478bd9Sstevel@tonic-gate 	}
3770*7c478bd9Sstevel@tonic-gate }
3771*7c478bd9Sstevel@tonic-gate 
3772*7c478bd9Sstevel@tonic-gate /*
3773*7c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. It calls to unlink
3774*7c478bd9Sstevel@tonic-gate  * all the CONDEMNED ires from this bucket.
3775*7c478bd9Sstevel@tonic-gate  */
3776*7c478bd9Sstevel@tonic-gate ire_t *
3777*7c478bd9Sstevel@tonic-gate ire_unlink(irb_t *irb)
3778*7c478bd9Sstevel@tonic-gate {
3779*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
3780*7c478bd9Sstevel@tonic-gate 	ire_t *ire1;
3781*7c478bd9Sstevel@tonic-gate 	ire_t **ptpn;
3782*7c478bd9Sstevel@tonic-gate 	ire_t *ire_list = NULL;
3783*7c478bd9Sstevel@tonic-gate 
3784*7c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
3785*7c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_refcnt == 0);
3786*7c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_marks & IRE_MARK_CONDEMNED);
3787*7c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_ire != NULL);
3788*7c478bd9Sstevel@tonic-gate 
3789*7c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
3790*7c478bd9Sstevel@tonic-gate 		ire1 = ire->ire_next;
3791*7c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
3792*7c478bd9Sstevel@tonic-gate 			ptpn = ire->ire_ptpn;
3793*7c478bd9Sstevel@tonic-gate 			ire1 = ire->ire_next;
3794*7c478bd9Sstevel@tonic-gate 			if (ire1)
3795*7c478bd9Sstevel@tonic-gate 				ire1->ire_ptpn = ptpn;
3796*7c478bd9Sstevel@tonic-gate 			*ptpn = ire1;
3797*7c478bd9Sstevel@tonic-gate 			ire->ire_ptpn = NULL;
3798*7c478bd9Sstevel@tonic-gate 			ire->ire_next = NULL;
3799*7c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_DEFAULT) {
3800*7c478bd9Sstevel@tonic-gate 				/*
3801*7c478bd9Sstevel@tonic-gate 				 * IRE is out of the list. We need to adjust
3802*7c478bd9Sstevel@tonic-gate 				 * the accounting before the caller drops
3803*7c478bd9Sstevel@tonic-gate 				 * the lock.
3804*7c478bd9Sstevel@tonic-gate 				 */
3805*7c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV6_VERSION) {
3806*7c478bd9Sstevel@tonic-gate 					ASSERT(ipv6_ire_default_count != 0);
3807*7c478bd9Sstevel@tonic-gate 					ipv6_ire_default_count--;
3808*7c478bd9Sstevel@tonic-gate 				} else {
3809*7c478bd9Sstevel@tonic-gate 					ASSERT(ip_ire_default_count != 0);
3810*7c478bd9Sstevel@tonic-gate 					ip_ire_default_count--;
3811*7c478bd9Sstevel@tonic-gate 				}
3812*7c478bd9Sstevel@tonic-gate 			}
3813*7c478bd9Sstevel@tonic-gate 			/*
3814*7c478bd9Sstevel@tonic-gate 			 * We need to call ire_delete_v4 or ire_delete_v6
3815*7c478bd9Sstevel@tonic-gate 			 * to clean up the cache or the redirects pointing at
3816*7c478bd9Sstevel@tonic-gate 			 * the default gateway. We need to drop the lock
3817*7c478bd9Sstevel@tonic-gate 			 * as ire_flush_cache/ire_delete_host_redircts require
3818*7c478bd9Sstevel@tonic-gate 			 * so. But we can't drop the lock, as ire_unlink needs
3819*7c478bd9Sstevel@tonic-gate 			 * to atomically remove the ires from the list.
3820*7c478bd9Sstevel@tonic-gate 			 * So, create a temporary list of CONDEMNED ires
3821*7c478bd9Sstevel@tonic-gate 			 * for doing ire_delete_v4/ire_delete_v6 operations
3822*7c478bd9Sstevel@tonic-gate 			 * later on.
3823*7c478bd9Sstevel@tonic-gate 			 */
3824*7c478bd9Sstevel@tonic-gate 			ire->ire_next = ire_list;
3825*7c478bd9Sstevel@tonic-gate 			ire_list = ire;
3826*7c478bd9Sstevel@tonic-gate 		}
3827*7c478bd9Sstevel@tonic-gate 	}
3828*7c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_refcnt == 0);
3829*7c478bd9Sstevel@tonic-gate 	irb->irb_marks &= ~IRE_MARK_CONDEMNED;
3830*7c478bd9Sstevel@tonic-gate 	ASSERT(ire_list != NULL);
3831*7c478bd9Sstevel@tonic-gate 	return (ire_list);
3832*7c478bd9Sstevel@tonic-gate }
3833*7c478bd9Sstevel@tonic-gate 
3834*7c478bd9Sstevel@tonic-gate /*
3835*7c478bd9Sstevel@tonic-gate  * Delete all the cache entries with this 'addr'.  When IP gets a gratuitous
3836*7c478bd9Sstevel@tonic-gate  * ARP message on any of its interface queue, it scans the cache table and
3837*7c478bd9Sstevel@tonic-gate  * deletes all the cache entries for that address. This function is called
3838*7c478bd9Sstevel@tonic-gate  * from ip_arp_news in ip.c and  also for ARP ioctl processing in ip_if.c.
3839*7c478bd9Sstevel@tonic-gate  * ip_ire_clookup_and_delete returns true if it finds at least one cache entry
3840*7c478bd9Sstevel@tonic-gate  * which is used by ip_arp_news to determine if it needs to do an ire_walk_v4.
3841*7c478bd9Sstevel@tonic-gate  * The return value is also  used for the same purpose by ARP IOCTL processing
3842*7c478bd9Sstevel@tonic-gate  * in ip_if.c when deleting ARP entries. For SIOC*IFARP ioctls in addition to
3843*7c478bd9Sstevel@tonic-gate  * the address, ip_if->ipif_ill also needs to be matched.
3844*7c478bd9Sstevel@tonic-gate  */
3845*7c478bd9Sstevel@tonic-gate boolean_t
3846*7c478bd9Sstevel@tonic-gate ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif)
3847*7c478bd9Sstevel@tonic-gate {
3848*7c478bd9Sstevel@tonic-gate 	irb_t		*irb;
3849*7c478bd9Sstevel@tonic-gate 	ire_t		*cire;
3850*7c478bd9Sstevel@tonic-gate 	ill_t		*ill;
3851*7c478bd9Sstevel@tonic-gate 	boolean_t	found = B_FALSE, loop_end = B_FALSE;
3852*7c478bd9Sstevel@tonic-gate 
3853*7c478bd9Sstevel@tonic-gate 	irb = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
3854*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
3855*7c478bd9Sstevel@tonic-gate 	for (cire = irb->irb_ire; cire != NULL; cire = cire->ire_next) {
3856*7c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & IRE_MARK_CONDEMNED)
3857*7c478bd9Sstevel@tonic-gate 			continue;
3858*7c478bd9Sstevel@tonic-gate 		if (cire->ire_addr == addr) {
3859*7c478bd9Sstevel@tonic-gate 
3860*7c478bd9Sstevel@tonic-gate 			/* This signifies start of an address match */
3861*7c478bd9Sstevel@tonic-gate 			if (!loop_end)
3862*7c478bd9Sstevel@tonic-gate 				loop_end = B_TRUE;
3863*7c478bd9Sstevel@tonic-gate 
3864*7c478bd9Sstevel@tonic-gate 			/* We are interested only in IRE_CACHEs */
3865*7c478bd9Sstevel@tonic-gate 			if (cire->ire_type == IRE_CACHE) {
3866*7c478bd9Sstevel@tonic-gate 				/* If we want a match with the ILL */
3867*7c478bd9Sstevel@tonic-gate 				if (ipif != NULL &&
3868*7c478bd9Sstevel@tonic-gate 				    ((ill = ire_to_ill(cire)) == NULL ||
3869*7c478bd9Sstevel@tonic-gate 				    ill != ipif->ipif_ill)) {
3870*7c478bd9Sstevel@tonic-gate 					continue;
3871*7c478bd9Sstevel@tonic-gate 				}
3872*7c478bd9Sstevel@tonic-gate 				if (!found)
3873*7c478bd9Sstevel@tonic-gate 					found = B_TRUE;
3874*7c478bd9Sstevel@tonic-gate 				ire_delete(cire);
3875*7c478bd9Sstevel@tonic-gate 			}
3876*7c478bd9Sstevel@tonic-gate 		/* End of the match */
3877*7c478bd9Sstevel@tonic-gate 		} else if (loop_end)
3878*7c478bd9Sstevel@tonic-gate 			break;
3879*7c478bd9Sstevel@tonic-gate 	}
3880*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
3881*7c478bd9Sstevel@tonic-gate 
3882*7c478bd9Sstevel@tonic-gate 	return (found);
3883*7c478bd9Sstevel@tonic-gate 
3884*7c478bd9Sstevel@tonic-gate }
3885*7c478bd9Sstevel@tonic-gate 
3886*7c478bd9Sstevel@tonic-gate /*
3887*7c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
3888*7c478bd9Sstevel@tonic-gate  */
3889*7c478bd9Sstevel@tonic-gate void
3890*7c478bd9Sstevel@tonic-gate ire_delete(ire_t *ire)
3891*7c478bd9Sstevel@tonic-gate {
3892*7c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
3893*7c478bd9Sstevel@tonic-gate 	ire_t	**ptpn;
3894*7c478bd9Sstevel@tonic-gate 	irb_t *irb;
3895*7c478bd9Sstevel@tonic-gate 
3896*7c478bd9Sstevel@tonic-gate 	/*
3897*7c478bd9Sstevel@tonic-gate 	 * It was never inserted in the list. Should call REFRELE
3898*7c478bd9Sstevel@tonic-gate 	 * to free this IRE.
3899*7c478bd9Sstevel@tonic-gate 	 */
3900*7c478bd9Sstevel@tonic-gate 	if ((irb = ire->ire_bucket) == NULL) {
3901*7c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
3902*7c478bd9Sstevel@tonic-gate 		return;
3903*7c478bd9Sstevel@tonic-gate 	}
3904*7c478bd9Sstevel@tonic-gate 
3905*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb->irb_lock, RW_WRITER);
3906*7c478bd9Sstevel@tonic-gate 
3907*7c478bd9Sstevel@tonic-gate 	/*
3908*7c478bd9Sstevel@tonic-gate 	 * In case of V4 we might still be waiting for fastpath ack.
3909*7c478bd9Sstevel@tonic-gate 	 */
3910*7c478bd9Sstevel@tonic-gate 	if (ire->ire_nce == NULL && ire->ire_stq != NULL) {
3911*7c478bd9Sstevel@tonic-gate 		ill_t *ill;
3912*7c478bd9Sstevel@tonic-gate 
3913*7c478bd9Sstevel@tonic-gate 		ill = ire_to_ill(ire);
3914*7c478bd9Sstevel@tonic-gate 		if (ill != NULL)
3915*7c478bd9Sstevel@tonic-gate 			ire_fastpath_list_delete(ill, ire);
3916*7c478bd9Sstevel@tonic-gate 	}
3917*7c478bd9Sstevel@tonic-gate 
3918*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ptpn == NULL) {
3919*7c478bd9Sstevel@tonic-gate 		/*
3920*7c478bd9Sstevel@tonic-gate 		 * Some other thread has removed us from the list.
3921*7c478bd9Sstevel@tonic-gate 		 * It should have done the REFRELE for us.
3922*7c478bd9Sstevel@tonic-gate 		 */
3923*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
3924*7c478bd9Sstevel@tonic-gate 		return;
3925*7c478bd9Sstevel@tonic-gate 	}
3926*7c478bd9Sstevel@tonic-gate 
3927*7c478bd9Sstevel@tonic-gate 	if (irb->irb_refcnt != 0) {
3928*7c478bd9Sstevel@tonic-gate 		/*
3929*7c478bd9Sstevel@tonic-gate 		 * The last thread to leave this bucket will
3930*7c478bd9Sstevel@tonic-gate 		 * delete this ire.
3931*7c478bd9Sstevel@tonic-gate 		 */
3932*7c478bd9Sstevel@tonic-gate 		if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
3933*7c478bd9Sstevel@tonic-gate 			irb->irb_ire_cnt--;
3934*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY)
3935*7c478bd9Sstevel@tonic-gate 				irb->irb_tmp_ire_cnt--;
3936*7c478bd9Sstevel@tonic-gate 			ire->ire_marks |= IRE_MARK_CONDEMNED;
3937*7c478bd9Sstevel@tonic-gate 		}
3938*7c478bd9Sstevel@tonic-gate 		irb->irb_marks |= IRE_MARK_CONDEMNED;
3939*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
3940*7c478bd9Sstevel@tonic-gate 		return;
3941*7c478bd9Sstevel@tonic-gate 	}
3942*7c478bd9Sstevel@tonic-gate 
3943*7c478bd9Sstevel@tonic-gate 	/*
3944*7c478bd9Sstevel@tonic-gate 	 * Normally to delete an ire, we walk the bucket. While we
3945*7c478bd9Sstevel@tonic-gate 	 * walk the bucket, we normally bump up irb_refcnt and hence
3946*7c478bd9Sstevel@tonic-gate 	 * we return from above where we mark CONDEMNED and the ire
3947*7c478bd9Sstevel@tonic-gate 	 * gets deleted from ire_unlink. This case is where somebody
3948*7c478bd9Sstevel@tonic-gate 	 * knows the ire e.g by doing a lookup, and wants to delete the
3949*7c478bd9Sstevel@tonic-gate 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
3950*7c478bd9Sstevel@tonic-gate 	 * the bucket.
3951*7c478bd9Sstevel@tonic-gate 	 */
3952*7c478bd9Sstevel@tonic-gate 	ptpn = ire->ire_ptpn;
3953*7c478bd9Sstevel@tonic-gate 	ire1 = ire->ire_next;
3954*7c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
3955*7c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = ptpn;
3956*7c478bd9Sstevel@tonic-gate 	ASSERT(ptpn != NULL);
3957*7c478bd9Sstevel@tonic-gate 	*ptpn = ire1;
3958*7c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = NULL;
3959*7c478bd9Sstevel@tonic-gate 	ire->ire_next = NULL;
3960*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3961*7c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v6, ire_stats_deleted);
3962*7c478bd9Sstevel@tonic-gate 	} else {
3963*7c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_deleted);
3964*7c478bd9Sstevel@tonic-gate 	}
3965*7c478bd9Sstevel@tonic-gate 	/*
3966*7c478bd9Sstevel@tonic-gate 	 * ip_wput/ip_wput_v6 checks this flag to see whether
3967*7c478bd9Sstevel@tonic-gate 	 * it should still use the cached ire or not.
3968*7c478bd9Sstevel@tonic-gate 	 */
3969*7c478bd9Sstevel@tonic-gate 	ire->ire_marks |= IRE_MARK_CONDEMNED;
3970*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
3971*7c478bd9Sstevel@tonic-gate 		/*
3972*7c478bd9Sstevel@tonic-gate 		 * IRE is out of the list. We need to adjust the
3973*7c478bd9Sstevel@tonic-gate 		 * accounting before we drop the lock.
3974*7c478bd9Sstevel@tonic-gate 		 */
3975*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV6_VERSION) {
3976*7c478bd9Sstevel@tonic-gate 			ASSERT(ipv6_ire_default_count != 0);
3977*7c478bd9Sstevel@tonic-gate 			ipv6_ire_default_count--;
3978*7c478bd9Sstevel@tonic-gate 		} else {
3979*7c478bd9Sstevel@tonic-gate 			ASSERT(ip_ire_default_count != 0);
3980*7c478bd9Sstevel@tonic-gate 			ip_ire_default_count--;
3981*7c478bd9Sstevel@tonic-gate 		}
3982*7c478bd9Sstevel@tonic-gate 	}
3983*7c478bd9Sstevel@tonic-gate 	irb->irb_ire_cnt--;
3984*7c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
3985*7c478bd9Sstevel@tonic-gate 		irb->irb_tmp_ire_cnt--;
3986*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb->irb_lock);
3987*7c478bd9Sstevel@tonic-gate 
3988*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3989*7c478bd9Sstevel@tonic-gate 		ire_delete_v6(ire);
3990*7c478bd9Sstevel@tonic-gate 	} else {
3991*7c478bd9Sstevel@tonic-gate 		ire_delete_v4(ire);
3992*7c478bd9Sstevel@tonic-gate 	}
3993*7c478bd9Sstevel@tonic-gate 	/*
3994*7c478bd9Sstevel@tonic-gate 	 * We removed it from the list. Decrement the
3995*7c478bd9Sstevel@tonic-gate 	 * reference count.
3996*7c478bd9Sstevel@tonic-gate 	 */
3997*7c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
3998*7c478bd9Sstevel@tonic-gate }
3999*7c478bd9Sstevel@tonic-gate 
4000*7c478bd9Sstevel@tonic-gate /*
4001*7c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
4002*7c478bd9Sstevel@tonic-gate  * All calls should use ire_delete().
4003*7c478bd9Sstevel@tonic-gate  * Sometimes called as writer though not required by this function.
4004*7c478bd9Sstevel@tonic-gate  *
4005*7c478bd9Sstevel@tonic-gate  * NOTE : This function is called only if the ire was added
4006*7c478bd9Sstevel@tonic-gate  * in the list.
4007*7c478bd9Sstevel@tonic-gate  */
4008*7c478bd9Sstevel@tonic-gate static void
4009*7c478bd9Sstevel@tonic-gate ire_delete_v4(ire_t *ire)
4010*7c478bd9Sstevel@tonic-gate {
4011*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt >= 1);
4012*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
4013*7c478bd9Sstevel@tonic-gate 
4014*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
4015*7c478bd9Sstevel@tonic-gate 		ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
4016*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
4017*7c478bd9Sstevel@tonic-gate 		/*
4018*7c478bd9Sstevel@tonic-gate 		 * when a default gateway is going away
4019*7c478bd9Sstevel@tonic-gate 		 * delete all the host redirects pointing at that
4020*7c478bd9Sstevel@tonic-gate 		 * gateway.
4021*7c478bd9Sstevel@tonic-gate 		 */
4022*7c478bd9Sstevel@tonic-gate 		ire_delete_host_redirects(ire->ire_gateway_addr);
4023*7c478bd9Sstevel@tonic-gate 	}
4024*7c478bd9Sstevel@tonic-gate }
4025*7c478bd9Sstevel@tonic-gate 
4026*7c478bd9Sstevel@tonic-gate /*
4027*7c478bd9Sstevel@tonic-gate  * IRE_REFRELE/ire_refrele are the only caller of the function. It calls
4028*7c478bd9Sstevel@tonic-gate  * to free the ire when the reference count goes to zero.
4029*7c478bd9Sstevel@tonic-gate  */
4030*7c478bd9Sstevel@tonic-gate void
4031*7c478bd9Sstevel@tonic-gate ire_inactive(ire_t *ire)
4032*7c478bd9Sstevel@tonic-gate {
4033*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4034*7c478bd9Sstevel@tonic-gate 	nce_t	*nce;
4035*7c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
4036*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
4037*7c478bd9Sstevel@tonic-gate 	ill_t	*in_ill = NULL;
4038*7c478bd9Sstevel@tonic-gate 	ipif_t	*ipif;
4039*7c478bd9Sstevel@tonic-gate 	boolean_t	need_wakeup = B_FALSE;
4040*7c478bd9Sstevel@tonic-gate 
4041*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt == 0);
4042*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ptpn == NULL);
4043*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_next == NULL);
4044*7c478bd9Sstevel@tonic-gate 
4045*7c478bd9Sstevel@tonic-gate 	if ((nce = ire->ire_nce) != NULL) {
4046*7c478bd9Sstevel@tonic-gate 		/* Only IPv6 IRE_CACHE type has an nce */
4047*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_type == IRE_CACHE);
4048*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_ipversion == IPV6_VERSION);
4049*7c478bd9Sstevel@tonic-gate 		NCE_REFRELE_NOTR(nce);
4050*7c478bd9Sstevel@tonic-gate 		ire->ire_nce = NULL;
4051*7c478bd9Sstevel@tonic-gate 	}
4052*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif == NULL)
4053*7c478bd9Sstevel@tonic-gate 		goto end;
4054*7c478bd9Sstevel@tonic-gate 
4055*7c478bd9Sstevel@tonic-gate 	ipif = ire->ire_ipif;
4056*7c478bd9Sstevel@tonic-gate 	ill = ipif->ipif_ill;
4057*7c478bd9Sstevel@tonic-gate 
4058*7c478bd9Sstevel@tonic-gate 	if (ire->ire_bucket == NULL) {
4059*7c478bd9Sstevel@tonic-gate 		/* The ire was never inserted in the table. */
4060*7c478bd9Sstevel@tonic-gate 		goto end;
4061*7c478bd9Sstevel@tonic-gate 	}
4062*7c478bd9Sstevel@tonic-gate 
4063*7c478bd9Sstevel@tonic-gate 	/*
4064*7c478bd9Sstevel@tonic-gate 	 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is
4065*7c478bd9Sstevel@tonic-gate 	 * non-null ill_ire_count also goes down by 1. If the in_ill is
4066*7c478bd9Sstevel@tonic-gate 	 * non-null either ill_mrtun_refcnt or ill_srcif_refcnt goes down by 1.
4067*7c478bd9Sstevel@tonic-gate 	 *
4068*7c478bd9Sstevel@tonic-gate 	 * The ipif that is associated with an ire is ire->ire_ipif and
4069*7c478bd9Sstevel@tonic-gate 	 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call
4070*7c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as
4071*7c478bd9Sstevel@tonic-gate 	 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only
4072*7c478bd9Sstevel@tonic-gate 	 * in the case of IRE_CACHES when IPMP is used, stq_ill can be
4073*7c478bd9Sstevel@tonic-gate 	 * different. If this is different from ire->ire_ipif->ipif_ill and
4074*7c478bd9Sstevel@tonic-gate 	 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call
4075*7c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail on the stq_ill. If mobile ip is in use
4076*7c478bd9Sstevel@tonic-gate 	 * in_ill could be non-null. If it is a reverse tunnel related ire
4077*7c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is non-zero. If it is forward tunnel related ire
4078*7c478bd9Sstevel@tonic-gate 	 * ill_srcif_refcnt is non-null.
4079*7c478bd9Sstevel@tonic-gate 	 */
4080*7c478bd9Sstevel@tonic-gate 
4081*7c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
4082*7c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
4083*7c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL)
4084*7c478bd9Sstevel@tonic-gate 		in_ill = ire->ire_in_ill;
4085*7c478bd9Sstevel@tonic-gate 
4086*7c478bd9Sstevel@tonic-gate 	if ((stq_ill == NULL || stq_ill == ill) && (in_ill == NULL)) {
4087*7c478bd9Sstevel@tonic-gate 		/* Optimize the most common case */
4088*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
4089*7c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
4090*7c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
4091*7c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0)
4092*7c478bd9Sstevel@tonic-gate 			need_wakeup = B_TRUE;
4093*7c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
4094*7c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
4095*7c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
4096*7c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)
4097*7c478bd9Sstevel@tonic-gate 				need_wakeup = B_TRUE;
4098*7c478bd9Sstevel@tonic-gate 		}
4099*7c478bd9Sstevel@tonic-gate 		if (need_wakeup) {
4100*7c478bd9Sstevel@tonic-gate 			/* Drops the ill lock */
4101*7c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
4102*7c478bd9Sstevel@tonic-gate 		} else {
4103*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
4104*7c478bd9Sstevel@tonic-gate 		}
4105*7c478bd9Sstevel@tonic-gate 	} else {
4106*7c478bd9Sstevel@tonic-gate 		/*
4107*7c478bd9Sstevel@tonic-gate 		 * We can't grab all the ill locks at the same time.
4108*7c478bd9Sstevel@tonic-gate 		 * It can lead to recursive lock enter in the call to
4109*7c478bd9Sstevel@tonic-gate 		 * ipif_ill_refrele_tail and later. Instead do it 1 at
4110*7c478bd9Sstevel@tonic-gate 		 * a time.
4111*7c478bd9Sstevel@tonic-gate 		 */
4112*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
4113*7c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
4114*7c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
4115*7c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0) {
4116*7c478bd9Sstevel@tonic-gate 			/* Drops the lock */
4117*7c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
4118*7c478bd9Sstevel@tonic-gate 		} else {
4119*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
4120*7c478bd9Sstevel@tonic-gate 		}
4121*7c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
4122*7c478bd9Sstevel@tonic-gate 			mutex_enter(&stq_ill->ill_lock);
4123*7c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
4124*7c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
4125*7c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)  {
4126*7c478bd9Sstevel@tonic-gate 				/* Drops the ill lock */
4127*7c478bd9Sstevel@tonic-gate 				ipif_ill_refrele_tail(stq_ill);
4128*7c478bd9Sstevel@tonic-gate 			} else {
4129*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stq_ill->ill_lock);
4130*7c478bd9Sstevel@tonic-gate 			}
4131*7c478bd9Sstevel@tonic-gate 		}
4132*7c478bd9Sstevel@tonic-gate 		if (in_ill != NULL) {
4133*7c478bd9Sstevel@tonic-gate 			mutex_enter(&in_ill->ill_lock);
4134*7c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_MIPRTUN) {
4135*7c478bd9Sstevel@tonic-gate 				/*
4136*7c478bd9Sstevel@tonic-gate 				 * Mobile IP reverse tunnel ire.
4137*7c478bd9Sstevel@tonic-gate 				 * Decrement table count and the
4138*7c478bd9Sstevel@tonic-gate 				 * ill reference count. This signifies
4139*7c478bd9Sstevel@tonic-gate 				 * mipagent is deleting reverse tunnel
4140*7c478bd9Sstevel@tonic-gate 				 * route for a particular mobile node.
4141*7c478bd9Sstevel@tonic-gate 				 */
4142*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ire_mrtun_lock);
4143*7c478bd9Sstevel@tonic-gate 				ire_mrtun_count--;
4144*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_mrtun_lock);
4145*7c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_mrtun_refcnt != 0);
4146*7c478bd9Sstevel@tonic-gate 				in_ill->ill_mrtun_refcnt--;
4147*7c478bd9Sstevel@tonic-gate 				if (in_ill->ill_mrtun_refcnt == 0) {
4148*7c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
4149*7c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
4150*7c478bd9Sstevel@tonic-gate 				} else {
4151*7c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
4152*7c478bd9Sstevel@tonic-gate 				}
4153*7c478bd9Sstevel@tonic-gate 			} else {
4154*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ire_srcif_table_lock);
4155*7c478bd9Sstevel@tonic-gate 				ire_srcif_table_count--;
4156*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_srcif_table_lock);
4157*7c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_srcif_refcnt != 0);
4158*7c478bd9Sstevel@tonic-gate 				in_ill->ill_srcif_refcnt--;
4159*7c478bd9Sstevel@tonic-gate 				if (in_ill->ill_srcif_refcnt == 0) {
4160*7c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
4161*7c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
4162*7c478bd9Sstevel@tonic-gate 				} else {
4163*7c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
4164*7c478bd9Sstevel@tonic-gate 				}
4165*7c478bd9Sstevel@tonic-gate 			}
4166*7c478bd9Sstevel@tonic-gate 		}
4167*7c478bd9Sstevel@tonic-gate 	}
4168*7c478bd9Sstevel@tonic-gate end:
4169*7c478bd9Sstevel@tonic-gate 	/* This should be true for both V4 and V6 */
4170*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fastpath == NULL);
4171*7c478bd9Sstevel@tonic-gate 
4172*7c478bd9Sstevel@tonic-gate 
4173*7c478bd9Sstevel@tonic-gate 	ire->ire_ipif = NULL;
4174*7c478bd9Sstevel@tonic-gate 
4175*7c478bd9Sstevel@tonic-gate 	/* Free the xmit header, and the IRE itself. */
4176*7c478bd9Sstevel@tonic-gate 	if ((mp = ire->ire_dlureq_mp) != NULL) {
4177*7c478bd9Sstevel@tonic-gate 		freeb(mp);
4178*7c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = NULL;
4179*7c478bd9Sstevel@tonic-gate 	}
4180*7c478bd9Sstevel@tonic-gate 
4181*7c478bd9Sstevel@tonic-gate 	if ((mp = ire->ire_fp_mp) != NULL) {
4182*7c478bd9Sstevel@tonic-gate 		freeb(mp);
4183*7c478bd9Sstevel@tonic-gate 		ire->ire_fp_mp = NULL;
4184*7c478bd9Sstevel@tonic-gate 	}
4185*7c478bd9Sstevel@tonic-gate 
4186*7c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL) {
4187*7c478bd9Sstevel@tonic-gate 		ire->ire_in_ill = NULL;
4188*7c478bd9Sstevel@tonic-gate 	}
4189*7c478bd9Sstevel@tonic-gate 
4190*7c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
4191*7c478bd9Sstevel@tonic-gate 	ire_trace_inactive(ire);
4192*7c478bd9Sstevel@tonic-gate #endif
4193*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire->ire_lock);
4194*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
4195*7c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v6, ire_stats_freed);
4196*7c478bd9Sstevel@tonic-gate 	} else {
4197*7c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_freed);
4198*7c478bd9Sstevel@tonic-gate 	}
4199*7c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
4200*7c478bd9Sstevel@tonic-gate 		/* Still in an mblk */
4201*7c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
4202*7c478bd9Sstevel@tonic-gate 	} else {
4203*7c478bd9Sstevel@tonic-gate 		/* Has been allocated out of the cache */
4204*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
4205*7c478bd9Sstevel@tonic-gate 	}
4206*7c478bd9Sstevel@tonic-gate }
4207*7c478bd9Sstevel@tonic-gate 
4208*7c478bd9Sstevel@tonic-gate /*
4209*7c478bd9Sstevel@tonic-gate  * ire_walk routine to delete all IRE_CACHE/IRE_HOST_REDIRECT entries
4210*7c478bd9Sstevel@tonic-gate  * that have a given gateway address.
4211*7c478bd9Sstevel@tonic-gate  */
4212*7c478bd9Sstevel@tonic-gate void
4213*7c478bd9Sstevel@tonic-gate ire_delete_cache_gw(ire_t *ire, char *cp)
4214*7c478bd9Sstevel@tonic-gate {
4215*7c478bd9Sstevel@tonic-gate 	ipaddr_t	gw_addr;
4216*7c478bd9Sstevel@tonic-gate 
4217*7c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_CACHE|IRE_HOST_REDIRECT)))
4218*7c478bd9Sstevel@tonic-gate 		return;
4219*7c478bd9Sstevel@tonic-gate 
4220*7c478bd9Sstevel@tonic-gate 	bcopy(cp, &gw_addr, sizeof (gw_addr));
4221*7c478bd9Sstevel@tonic-gate 	if (ire->ire_gateway_addr == gw_addr) {
4222*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n",
4223*7c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_addr), ire->ire_type,
4224*7c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_gateway_addr)));
4225*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
4226*7c478bd9Sstevel@tonic-gate 	}
4227*7c478bd9Sstevel@tonic-gate }
4228*7c478bd9Sstevel@tonic-gate 
4229*7c478bd9Sstevel@tonic-gate /*
4230*7c478bd9Sstevel@tonic-gate  * Remove all IRE_CACHE entries that match the ire specified.
4231*7c478bd9Sstevel@tonic-gate  *
4232*7c478bd9Sstevel@tonic-gate  * The flag argument indicates if the flush request is due to addition
4233*7c478bd9Sstevel@tonic-gate  * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE).
4234*7c478bd9Sstevel@tonic-gate  *
4235*7c478bd9Sstevel@tonic-gate  * This routine takes only the IREs from the forwarding table and flushes
4236*7c478bd9Sstevel@tonic-gate  * the corresponding entries from the cache table.
4237*7c478bd9Sstevel@tonic-gate  *
4238*7c478bd9Sstevel@tonic-gate  * When flushing due to the deletion of an old route, it
4239*7c478bd9Sstevel@tonic-gate  * just checks the cache handles (ire_phandle and ire_ihandle) and
4240*7c478bd9Sstevel@tonic-gate  * deletes the ones that match.
4241*7c478bd9Sstevel@tonic-gate  *
4242*7c478bd9Sstevel@tonic-gate  * When flushing due to the creation of a new route, it checks
4243*7c478bd9Sstevel@tonic-gate  * if a cache entry's address matches the one in the IRE and
4244*7c478bd9Sstevel@tonic-gate  * that the cache entry's parent has a less specific mask than the
4245*7c478bd9Sstevel@tonic-gate  * one in IRE. The destination of such a cache entry could be the
4246*7c478bd9Sstevel@tonic-gate  * gateway for other cache entries, so we need to flush those as
4247*7c478bd9Sstevel@tonic-gate  * well by looking for gateway addresses matching the IRE's address.
4248*7c478bd9Sstevel@tonic-gate  */
4249*7c478bd9Sstevel@tonic-gate void
4250*7c478bd9Sstevel@tonic-gate ire_flush_cache_v4(ire_t *ire, int flag)
4251*7c478bd9Sstevel@tonic-gate {
4252*7c478bd9Sstevel@tonic-gate 	int i;
4253*7c478bd9Sstevel@tonic-gate 	ire_t *cire;
4254*7c478bd9Sstevel@tonic-gate 	irb_t *irb;
4255*7c478bd9Sstevel@tonic-gate 
4256*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHE)
4257*7c478bd9Sstevel@tonic-gate 	    return;
4258*7c478bd9Sstevel@tonic-gate 
4259*7c478bd9Sstevel@tonic-gate 	/*
4260*7c478bd9Sstevel@tonic-gate 	 * If a default is just created, there is no point
4261*7c478bd9Sstevel@tonic-gate 	 * in going through the cache, as there will not be any
4262*7c478bd9Sstevel@tonic-gate 	 * cached ires.
4263*7c478bd9Sstevel@tonic-gate 	 */
4264*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD)
4265*7c478bd9Sstevel@tonic-gate 		return;
4266*7c478bd9Sstevel@tonic-gate 	if (flag == IRE_FLUSH_ADD) {
4267*7c478bd9Sstevel@tonic-gate 		/*
4268*7c478bd9Sstevel@tonic-gate 		 * This selective flush is due to the addition of
4269*7c478bd9Sstevel@tonic-gate 		 * new IRE.
4270*7c478bd9Sstevel@tonic-gate 		 */
4271*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ip_cache_table_size; i++) {
4272*7c478bd9Sstevel@tonic-gate 			irb = &ip_cache_table[i];
4273*7c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
4274*7c478bd9Sstevel@tonic-gate 				continue;
4275*7c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
4276*7c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
4277*7c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
4278*7c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
4279*7c478bd9Sstevel@tonic-gate 					continue;
4280*7c478bd9Sstevel@tonic-gate 				/*
4281*7c478bd9Sstevel@tonic-gate 				 * If 'cire' belongs to the same subnet
4282*7c478bd9Sstevel@tonic-gate 				 * as the new ire being added, and 'cire'
4283*7c478bd9Sstevel@tonic-gate 				 * is derived from a prefix that is less
4284*7c478bd9Sstevel@tonic-gate 				 * specific than the new ire being added,
4285*7c478bd9Sstevel@tonic-gate 				 * we need to flush 'cire'; for instance,
4286*7c478bd9Sstevel@tonic-gate 				 * when a new interface comes up.
4287*7c478bd9Sstevel@tonic-gate 				 */
4288*7c478bd9Sstevel@tonic-gate 				if (((cire->ire_addr & ire->ire_mask) ==
4289*7c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) &&
4290*7c478bd9Sstevel@tonic-gate 				    (ip_mask_to_plen(cire->ire_cmask) <=
4291*7c478bd9Sstevel@tonic-gate 				    ire->ire_masklen)) {
4292*7c478bd9Sstevel@tonic-gate 					ire_delete(cire);
4293*7c478bd9Sstevel@tonic-gate 					continue;
4294*7c478bd9Sstevel@tonic-gate 				}
4295*7c478bd9Sstevel@tonic-gate 				/*
4296*7c478bd9Sstevel@tonic-gate 				 * This is the case when the ire_gateway_addr
4297*7c478bd9Sstevel@tonic-gate 				 * of 'cire' belongs to the same subnet as
4298*7c478bd9Sstevel@tonic-gate 				 * the new ire being added.
4299*7c478bd9Sstevel@tonic-gate 				 * Flushing such ires is sometimes required to
4300*7c478bd9Sstevel@tonic-gate 				 * avoid misrouting: say we have a machine with
4301*7c478bd9Sstevel@tonic-gate 				 * two interfaces (I1 and I2), a default router
4302*7c478bd9Sstevel@tonic-gate 				 * R on the I1 subnet, and a host route to an
4303*7c478bd9Sstevel@tonic-gate 				 * off-link destination D with a gateway G on
4304*7c478bd9Sstevel@tonic-gate 				 * the I2 subnet.
4305*7c478bd9Sstevel@tonic-gate 				 * Under normal operation, we will have an
4306*7c478bd9Sstevel@tonic-gate 				 * on-link cache entry for G and an off-link
4307*7c478bd9Sstevel@tonic-gate 				 * cache entry for D with G as ire_gateway_addr,
4308*7c478bd9Sstevel@tonic-gate 				 * traffic to D will reach its destination
4309*7c478bd9Sstevel@tonic-gate 				 * through gateway G.
4310*7c478bd9Sstevel@tonic-gate 				 * If the administrator does 'ifconfig I2 down',
4311*7c478bd9Sstevel@tonic-gate 				 * the cache entries for D and G will be
4312*7c478bd9Sstevel@tonic-gate 				 * flushed. However, G will now be resolved as
4313*7c478bd9Sstevel@tonic-gate 				 * an off-link destination using R (the default
4314*7c478bd9Sstevel@tonic-gate 				 * router) as gateway. Then D will also be
4315*7c478bd9Sstevel@tonic-gate 				 * resolved as an off-link destination using G
4316*7c478bd9Sstevel@tonic-gate 				 * as gateway - this behavior is due to
4317*7c478bd9Sstevel@tonic-gate 				 * compatibility reasons, see comment in
4318*7c478bd9Sstevel@tonic-gate 				 * ire_ihandle_lookup_offlink(). Traffic to D
4319*7c478bd9Sstevel@tonic-gate 				 * will go to the router R and probably won't
4320*7c478bd9Sstevel@tonic-gate 				 * reach the destination.
4321*7c478bd9Sstevel@tonic-gate 				 * The administrator then does 'ifconfig I2 up'.
4322*7c478bd9Sstevel@tonic-gate 				 * Since G is on the I2 subnet, this routine
4323*7c478bd9Sstevel@tonic-gate 				 * will flush its cache entry. It must also
4324*7c478bd9Sstevel@tonic-gate 				 * flush the cache entry for D, otherwise
4325*7c478bd9Sstevel@tonic-gate 				 * traffic will stay misrouted until the IRE
4326*7c478bd9Sstevel@tonic-gate 				 * times out.
4327*7c478bd9Sstevel@tonic-gate 				 */
4328*7c478bd9Sstevel@tonic-gate 				if ((cire->ire_gateway_addr & ire->ire_mask) ==
4329*7c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) {
4330*7c478bd9Sstevel@tonic-gate 					ire_delete(cire);
4331*7c478bd9Sstevel@tonic-gate 					continue;
4332*7c478bd9Sstevel@tonic-gate 				}
4333*7c478bd9Sstevel@tonic-gate 			}
4334*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
4335*7c478bd9Sstevel@tonic-gate 		}
4336*7c478bd9Sstevel@tonic-gate 	} else {
4337*7c478bd9Sstevel@tonic-gate 		/*
4338*7c478bd9Sstevel@tonic-gate 		 * delete the cache entries based on
4339*7c478bd9Sstevel@tonic-gate 		 * handle in the IRE as this IRE is
4340*7c478bd9Sstevel@tonic-gate 		 * being deleted/changed.
4341*7c478bd9Sstevel@tonic-gate 		 */
4342*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ip_cache_table_size; i++) {
4343*7c478bd9Sstevel@tonic-gate 			irb = &ip_cache_table[i];
4344*7c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
4345*7c478bd9Sstevel@tonic-gate 				continue;
4346*7c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
4347*7c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
4348*7c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
4349*7c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
4350*7c478bd9Sstevel@tonic-gate 					continue;
4351*7c478bd9Sstevel@tonic-gate 				if ((cire->ire_phandle == 0 ||
4352*7c478bd9Sstevel@tonic-gate 				    cire->ire_phandle != ire->ire_phandle) &&
4353*7c478bd9Sstevel@tonic-gate 				    (cire->ire_ihandle == 0 ||
4354*7c478bd9Sstevel@tonic-gate 				    cire->ire_ihandle != ire->ire_ihandle))
4355*7c478bd9Sstevel@tonic-gate 					continue;
4356*7c478bd9Sstevel@tonic-gate 				ire_delete(cire);
4357*7c478bd9Sstevel@tonic-gate 			}
4358*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
4359*7c478bd9Sstevel@tonic-gate 		}
4360*7c478bd9Sstevel@tonic-gate 	}
4361*7c478bd9Sstevel@tonic-gate }
4362*7c478bd9Sstevel@tonic-gate 
4363*7c478bd9Sstevel@tonic-gate /*
4364*7c478bd9Sstevel@tonic-gate  * Matches the arguments passed with the values in the ire.
4365*7c478bd9Sstevel@tonic-gate  *
4366*7c478bd9Sstevel@tonic-gate  * Note: for match types that match using "ipif" passed in, ipif
4367*7c478bd9Sstevel@tonic-gate  * must be checked for non-NULL before calling this routine.
4368*7c478bd9Sstevel@tonic-gate  */
4369*7c478bd9Sstevel@tonic-gate static boolean_t
4370*7c478bd9Sstevel@tonic-gate ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
4371*7c478bd9Sstevel@tonic-gate     int type, ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle, int match_flags)
4372*7c478bd9Sstevel@tonic-gate {
4373*7c478bd9Sstevel@tonic-gate 	ill_t *ire_ill = NULL, *dst_ill;
4374*7c478bd9Sstevel@tonic-gate 	ill_t *ipif_ill = NULL;
4375*7c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
4376*7c478bd9Sstevel@tonic-gate 	ill_group_t *ipif_ill_group = NULL;
4377*7c478bd9Sstevel@tonic-gate 
4378*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
4379*7c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
4380*7c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) ||
4381*7c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
4382*7c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_WQ));
4383*7c478bd9Sstevel@tonic-gate 
4384*7c478bd9Sstevel@tonic-gate 	/*
4385*7c478bd9Sstevel@tonic-gate 	 * HIDDEN cache entries have to be looked up specifically with
4386*7c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set
4387*7c478bd9Sstevel@tonic-gate 	 * when the interface is FAILED or INACTIVE. In that case,
4388*7c478bd9Sstevel@tonic-gate 	 * any IRE_CACHES that exists should be marked with
4389*7c478bd9Sstevel@tonic-gate 	 * IRE_MARK_HIDDEN. So, we don't really need to match below
4390*7c478bd9Sstevel@tonic-gate 	 * for IRE_MARK_HIDDEN. But we do so for consistency.
4391*7c478bd9Sstevel@tonic-gate 	 */
4392*7c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_MARK_HIDDEN) &&
4393*7c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_HIDDEN))
4394*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
4395*7c478bd9Sstevel@tonic-gate 
4396*7c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid) {
4397*7c478bd9Sstevel@tonic-gate 		/*
4398*7c478bd9Sstevel@tonic-gate 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is
4399*7c478bd9Sstevel@tonic-gate 		 * valid and does not match that of ire_zoneid, a failure to
4400*7c478bd9Sstevel@tonic-gate 		 * match is reported at this point. Otherwise, since some IREs
4401*7c478bd9Sstevel@tonic-gate 		 * that are available in the global zone can be used in local
4402*7c478bd9Sstevel@tonic-gate 		 * zones, additional checks need to be performed:
4403*7c478bd9Sstevel@tonic-gate 		 *
4404*7c478bd9Sstevel@tonic-gate 		 *	IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK
4405*7c478bd9Sstevel@tonic-gate 		 *	entries should never be matched in this situation.
4406*7c478bd9Sstevel@tonic-gate 		 *
4407*7c478bd9Sstevel@tonic-gate 		 *	IRE entries that have an interface associated with them
4408*7c478bd9Sstevel@tonic-gate 		 *	should in general not match unless they are an IRE_LOCAL
4409*7c478bd9Sstevel@tonic-gate 		 *	or in the case when MATCH_IRE_DEFAULT has been set in
4410*7c478bd9Sstevel@tonic-gate 		 *	the caller.  In the case of the former, checking of the
4411*7c478bd9Sstevel@tonic-gate 		 *	other fields supplied should take place.
4412*7c478bd9Sstevel@tonic-gate 		 *
4413*7c478bd9Sstevel@tonic-gate 		 *	In the case where MATCH_IRE_DEFAULT has been set,
4414*7c478bd9Sstevel@tonic-gate 		 *	all of the ipif's associated with the IRE's ill are
4415*7c478bd9Sstevel@tonic-gate 		 *	checked to see if there is a matching zoneid.  If any
4416*7c478bd9Sstevel@tonic-gate 		 *	one ipif has a matching zoneid, this IRE is a
4417*7c478bd9Sstevel@tonic-gate 		 *	potential candidate so checking of the other fields
4418*7c478bd9Sstevel@tonic-gate 		 *	takes place.
4419*7c478bd9Sstevel@tonic-gate 		 *
4420*7c478bd9Sstevel@tonic-gate 		 *	In the case where the IRE_INTERFACE has a usable source
4421*7c478bd9Sstevel@tonic-gate 		 *	address (indicated by ill_usesrc_ifindex) in the
4422*7c478bd9Sstevel@tonic-gate 		 *	correct zone then it's permitted to return this IRE
4423*7c478bd9Sstevel@tonic-gate 		 */
4424*7c478bd9Sstevel@tonic-gate 		if (match_flags & MATCH_IRE_ZONEONLY)
4425*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
4426*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK))
4427*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
4428*7c478bd9Sstevel@tonic-gate 		/*
4429*7c478bd9Sstevel@tonic-gate 		 * Note, IRE_INTERFACE can have the stq as NULL. For
4430*7c478bd9Sstevel@tonic-gate 		 * example, if the default multicast route is tied to
4431*7c478bd9Sstevel@tonic-gate 		 * the loopback address.
4432*7c478bd9Sstevel@tonic-gate 		 */
4433*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_type & IRE_INTERFACE) &&
4434*7c478bd9Sstevel@tonic-gate 		    (ire->ire_stq != NULL)) {
4435*7c478bd9Sstevel@tonic-gate 			dst_ill = (ill_t *)ire->ire_stq->q_ptr;
4436*7c478bd9Sstevel@tonic-gate 			/*
4437*7c478bd9Sstevel@tonic-gate 			 * If there is a usable source address in the
4438*7c478bd9Sstevel@tonic-gate 			 * zone, then it's ok to return an
4439*7c478bd9Sstevel@tonic-gate 			 * IRE_INTERFACE
4440*7c478bd9Sstevel@tonic-gate 			 */
4441*7c478bd9Sstevel@tonic-gate 			if (ipif_usesrc_avail(dst_ill, zoneid)) {
4442*7c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: dst_ill %p match %d\n",
4443*7c478bd9Sstevel@tonic-gate 				    (void *)dst_ill,
4444*7c478bd9Sstevel@tonic-gate 				    (ire->ire_addr == (addr & mask))));
4445*7c478bd9Sstevel@tonic-gate 			} else {
4446*7c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: src_ipif NULL"
4447*7c478bd9Sstevel@tonic-gate 				    " dst_ill %p\n", (void *)dst_ill));
4448*7c478bd9Sstevel@tonic-gate 				return (B_FALSE);
4449*7c478bd9Sstevel@tonic-gate 			}
4450*7c478bd9Sstevel@tonic-gate 		}
4451*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL &&
4452*7c478bd9Sstevel@tonic-gate 		    !(ire->ire_type & IRE_INTERFACE)) {
4453*7c478bd9Sstevel@tonic-gate 			ipif_t	*tipif;
4454*7c478bd9Sstevel@tonic-gate 
4455*7c478bd9Sstevel@tonic-gate 			if ((match_flags & MATCH_IRE_DEFAULT) == 0) {
4456*7c478bd9Sstevel@tonic-gate 				return (B_FALSE);
4457*7c478bd9Sstevel@tonic-gate 			}
4458*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock);
4459*7c478bd9Sstevel@tonic-gate 			for (tipif = ire->ire_ipif->ipif_ill->ill_ipif;
4460*7c478bd9Sstevel@tonic-gate 			    tipif != NULL; tipif = tipif->ipif_next) {
4461*7c478bd9Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(tipif) &&
4462*7c478bd9Sstevel@tonic-gate 				    (tipif->ipif_flags & IPIF_UP) &&
4463*7c478bd9Sstevel@tonic-gate 				    (tipif->ipif_zoneid == zoneid))
4464*7c478bd9Sstevel@tonic-gate 					break;
4465*7c478bd9Sstevel@tonic-gate 			}
4466*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock);
4467*7c478bd9Sstevel@tonic-gate 			if (tipif == NULL) {
4468*7c478bd9Sstevel@tonic-gate 				return (B_FALSE);
4469*7c478bd9Sstevel@tonic-gate 			}
4470*7c478bd9Sstevel@tonic-gate 		}
4471*7c478bd9Sstevel@tonic-gate 	}
4472*7c478bd9Sstevel@tonic-gate 
4473*7c478bd9Sstevel@tonic-gate 	/*
4474*7c478bd9Sstevel@tonic-gate 	 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that
4475*7c478bd9Sstevel@tonic-gate 	 * somebody wants to send out on a particular interface which
4476*7c478bd9Sstevel@tonic-gate 	 * is given by ire_stq and hence use ire_stq to derive the ill
4477*7c478bd9Sstevel@tonic-gate 	 * value. ire_ipif for IRE_CACHES is just the means of getting
4478*7c478bd9Sstevel@tonic-gate 	 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr.
4479*7c478bd9Sstevel@tonic-gate 	 * ire_to_ill does the right thing for this.
4480*7c478bd9Sstevel@tonic-gate 	 */
4481*7c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
4482*7c478bd9Sstevel@tonic-gate 		ire_ill = ire_to_ill(ire);
4483*7c478bd9Sstevel@tonic-gate 		if (ire_ill != NULL)
4484*7c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ill->ill_group;
4485*7c478bd9Sstevel@tonic-gate 		ipif_ill = ipif->ipif_ill;
4486*7c478bd9Sstevel@tonic-gate 		ipif_ill_group = ipif_ill->ill_group;
4487*7c478bd9Sstevel@tonic-gate 	}
4488*7c478bd9Sstevel@tonic-gate 
4489*7c478bd9Sstevel@tonic-gate 	if ((ire->ire_addr == (addr & mask)) &&
4490*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_GW)) ||
4491*7c478bd9Sstevel@tonic-gate 		(ire->ire_gateway_addr == gateway)) &&
4492*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_TYPE)) ||
4493*7c478bd9Sstevel@tonic-gate 		(ire->ire_type & type)) &&
4494*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_SRC)) ||
4495*7c478bd9Sstevel@tonic-gate 		(ire->ire_src_addr == ipif->ipif_src_addr)) &&
4496*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IPIF)) ||
4497*7c478bd9Sstevel@tonic-gate 		(ire->ire_ipif == ipif)) &&
4498*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) ||
4499*7c478bd9Sstevel@tonic-gate 		(ire->ire_type != IRE_CACHE ||
4500*7c478bd9Sstevel@tonic-gate 		ire->ire_marks & IRE_MARK_HIDDEN)) &&
4501*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
4502*7c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill)) &&
4503*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IHANDLE)) ||
4504*7c478bd9Sstevel@tonic-gate 		(ire->ire_ihandle == ihandle)) &&
4505*7c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
4506*7c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill) ||
4507*7c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
4508*7c478bd9Sstevel@tonic-gate 		ire_ill_group == ipif_ill_group))) {
4509*7c478bd9Sstevel@tonic-gate 		/* We found the matched IRE */
4510*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
4511*7c478bd9Sstevel@tonic-gate 	}
4512*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
4513*7c478bd9Sstevel@tonic-gate }
4514*7c478bd9Sstevel@tonic-gate 
4515*7c478bd9Sstevel@tonic-gate 
4516*7c478bd9Sstevel@tonic-gate /*
4517*7c478bd9Sstevel@tonic-gate  * Lookup for a route in all the tables
4518*7c478bd9Sstevel@tonic-gate  */
4519*7c478bd9Sstevel@tonic-gate ire_t *
4520*7c478bd9Sstevel@tonic-gate ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
4521*7c478bd9Sstevel@tonic-gate     int type, ipif_t *ipif, ire_t **pire, zoneid_t zoneid, int flags)
4522*7c478bd9Sstevel@tonic-gate {
4523*7c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
4524*7c478bd9Sstevel@tonic-gate 
4525*7c478bd9Sstevel@tonic-gate 	/*
4526*7c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
4527*7c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
4528*7c478bd9Sstevel@tonic-gate 	 */
4529*7c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
4530*7c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
4531*7c478bd9Sstevel@tonic-gate 		return (NULL);
4532*7c478bd9Sstevel@tonic-gate 
4533*7c478bd9Sstevel@tonic-gate 	/*
4534*7c478bd9Sstevel@tonic-gate 	 * might be asking for a cache lookup,
4535*7c478bd9Sstevel@tonic-gate 	 * This is not best way to lookup cache,
4536*7c478bd9Sstevel@tonic-gate 	 * user should call ire_cache_lookup directly.
4537*7c478bd9Sstevel@tonic-gate 	 *
4538*7c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then
4539*7c478bd9Sstevel@tonic-gate 	 * in the forwarding table, if the applicable type flags were set.
4540*7c478bd9Sstevel@tonic-gate 	 */
4541*7c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) {
4542*7c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid,
4543*7c478bd9Sstevel@tonic-gate 		    flags);
4544*7c478bd9Sstevel@tonic-gate 		if (ire != NULL)
4545*7c478bd9Sstevel@tonic-gate 			return (ire);
4546*7c478bd9Sstevel@tonic-gate 	}
4547*7c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) {
4548*7c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire,
4549*7c478bd9Sstevel@tonic-gate 		    zoneid, 0, flags);
4550*7c478bd9Sstevel@tonic-gate 	}
4551*7c478bd9Sstevel@tonic-gate 	return (ire);
4552*7c478bd9Sstevel@tonic-gate }
4553*7c478bd9Sstevel@tonic-gate 
4554*7c478bd9Sstevel@tonic-gate /*
4555*7c478bd9Sstevel@tonic-gate  * Lookup a route in forwarding table.
4556*7c478bd9Sstevel@tonic-gate  * specific lookup is indicated by passing the
4557*7c478bd9Sstevel@tonic-gate  * required parameters and indicating the
4558*7c478bd9Sstevel@tonic-gate  * match required in flag field.
4559*7c478bd9Sstevel@tonic-gate  *
4560*7c478bd9Sstevel@tonic-gate  * Looking for default route can be done in three ways
4561*7c478bd9Sstevel@tonic-gate  * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
4562*7c478bd9Sstevel@tonic-gate  *    along with other matches.
4563*7c478bd9Sstevel@tonic-gate  * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
4564*7c478bd9Sstevel@tonic-gate  *    field along with other matches.
4565*7c478bd9Sstevel@tonic-gate  * 3) if the destination and mask are passed as zeros.
4566*7c478bd9Sstevel@tonic-gate  *
4567*7c478bd9Sstevel@tonic-gate  * A request to return a default route if no route
4568*7c478bd9Sstevel@tonic-gate  * is found, can be specified by setting MATCH_IRE_DEFAULT
4569*7c478bd9Sstevel@tonic-gate  * in flags.
4570*7c478bd9Sstevel@tonic-gate  *
4571*7c478bd9Sstevel@tonic-gate  * It does not support recursion more than one level. It
4572*7c478bd9Sstevel@tonic-gate  * will do recursive lookup only when the lookup maps to
4573*7c478bd9Sstevel@tonic-gate  * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
4574*7c478bd9Sstevel@tonic-gate  *
4575*7c478bd9Sstevel@tonic-gate  * If the routing table is setup to allow more than one level
4576*7c478bd9Sstevel@tonic-gate  * of recursion, the cleaning up cache table will not work resulting
4577*7c478bd9Sstevel@tonic-gate  * in invalid routing.
4578*7c478bd9Sstevel@tonic-gate  *
4579*7c478bd9Sstevel@tonic-gate  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
4580*7c478bd9Sstevel@tonic-gate  *
4581*7c478bd9Sstevel@tonic-gate  * NOTE : When this function returns NULL, pire has already been released.
4582*7c478bd9Sstevel@tonic-gate  *	  pire is valid only when this function successfully returns an
4583*7c478bd9Sstevel@tonic-gate  *	  ire.
4584*7c478bd9Sstevel@tonic-gate  */
4585*7c478bd9Sstevel@tonic-gate ire_t *
4586*7c478bd9Sstevel@tonic-gate ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
4587*7c478bd9Sstevel@tonic-gate     int type, ipif_t *ipif, ire_t **pire, zoneid_t zoneid, uint32_t ihandle,
4588*7c478bd9Sstevel@tonic-gate     int flags)
4589*7c478bd9Sstevel@tonic-gate {
4590*7c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
4591*7c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
4592*7c478bd9Sstevel@tonic-gate 	int i;
4593*7c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
4594*7c478bd9Sstevel@tonic-gate 
4595*7c478bd9Sstevel@tonic-gate 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4596*7c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & MATCH_IRE_WQ));
4597*7c478bd9Sstevel@tonic-gate 
4598*7c478bd9Sstevel@tonic-gate 	/*
4599*7c478bd9Sstevel@tonic-gate 	 * When we return NULL from this function, we should make
4600*7c478bd9Sstevel@tonic-gate 	 * sure that *pire is NULL so that the callers will not
4601*7c478bd9Sstevel@tonic-gate 	 * wrongly REFRELE the pire.
4602*7c478bd9Sstevel@tonic-gate 	 */
4603*7c478bd9Sstevel@tonic-gate 	if (pire != NULL)
4604*7c478bd9Sstevel@tonic-gate 		*pire = NULL;
4605*7c478bd9Sstevel@tonic-gate 	/*
4606*7c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
4607*7c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
4608*7c478bd9Sstevel@tonic-gate 	 */
4609*7c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
4610*7c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
4611*7c478bd9Sstevel@tonic-gate 		return (NULL);
4612*7c478bd9Sstevel@tonic-gate 
4613*7c478bd9Sstevel@tonic-gate 	/*
4614*7c478bd9Sstevel@tonic-gate 	 * If the mask is known, the lookup
4615*7c478bd9Sstevel@tonic-gate 	 * is simple, if the mask is not known
4616*7c478bd9Sstevel@tonic-gate 	 * we need to search.
4617*7c478bd9Sstevel@tonic-gate 	 */
4618*7c478bd9Sstevel@tonic-gate 	if (flags & MATCH_IRE_MASK) {
4619*7c478bd9Sstevel@tonic-gate 		uint_t masklen;
4620*7c478bd9Sstevel@tonic-gate 
4621*7c478bd9Sstevel@tonic-gate 		masklen = ip_mask_to_plen(mask);
4622*7c478bd9Sstevel@tonic-gate 		if (ip_forwarding_table[masklen] == NULL)
4623*7c478bd9Sstevel@tonic-gate 			return (NULL);
4624*7c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[masklen][
4625*7c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(addr & mask, ip_ftable_hash_size)]);
4626*7c478bd9Sstevel@tonic-gate 		rw_enter(&irb_ptr->irb_lock, RW_READER);
4627*7c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
4628*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
4629*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
4630*7c478bd9Sstevel@tonic-gate 				continue;
4631*7c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, mask, gateway, type, ipif,
4632*7c478bd9Sstevel@tonic-gate 			    zoneid, ihandle, flags))
4633*7c478bd9Sstevel@tonic-gate 				goto found_ire;
4634*7c478bd9Sstevel@tonic-gate 		}
4635*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb_ptr->irb_lock);
4636*7c478bd9Sstevel@tonic-gate 	} else {
4637*7c478bd9Sstevel@tonic-gate 		/*
4638*7c478bd9Sstevel@tonic-gate 		 * In this case we don't know the mask, we need to
4639*7c478bd9Sstevel@tonic-gate 		 * search the table assuming different mask sizes.
4640*7c478bd9Sstevel@tonic-gate 		 * we start with 32 bit mask, we don't allow default here.
4641*7c478bd9Sstevel@tonic-gate 		 */
4642*7c478bd9Sstevel@tonic-gate 		for (i = (IP_MASK_TABLE_SIZE - 1); i > 0; i--) {
4643*7c478bd9Sstevel@tonic-gate 			ipaddr_t tmpmask;
4644*7c478bd9Sstevel@tonic-gate 
4645*7c478bd9Sstevel@tonic-gate 			if ((ip_forwarding_table[i]) == NULL)
4646*7c478bd9Sstevel@tonic-gate 				continue;
4647*7c478bd9Sstevel@tonic-gate 			tmpmask = ip_plen_to_mask(i);
4648*7c478bd9Sstevel@tonic-gate 			irb_ptr = &ip_forwarding_table[i][
4649*7c478bd9Sstevel@tonic-gate 			    IRE_ADDR_HASH(addr & tmpmask,
4650*7c478bd9Sstevel@tonic-gate 			    ip_ftable_hash_size)];
4651*7c478bd9Sstevel@tonic-gate 			rw_enter(&irb_ptr->irb_lock, RW_READER);
4652*7c478bd9Sstevel@tonic-gate 			for (ire = irb_ptr->irb_ire; ire != NULL;
4653*7c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
4654*7c478bd9Sstevel@tonic-gate 				if (ire->ire_marks & IRE_MARK_CONDEMNED)
4655*7c478bd9Sstevel@tonic-gate 					continue;
4656*7c478bd9Sstevel@tonic-gate 				if (ire_match_args(ire, addr, ire->ire_mask,
4657*7c478bd9Sstevel@tonic-gate 				    gateway, type, ipif, zoneid, ihandle,
4658*7c478bd9Sstevel@tonic-gate 				    flags))
4659*7c478bd9Sstevel@tonic-gate 					goto found_ire;
4660*7c478bd9Sstevel@tonic-gate 			}
4661*7c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
4662*7c478bd9Sstevel@tonic-gate 		}
4663*7c478bd9Sstevel@tonic-gate 	}
4664*7c478bd9Sstevel@tonic-gate 	/*
4665*7c478bd9Sstevel@tonic-gate 	 * We come here if no route has yet been found.
4666*7c478bd9Sstevel@tonic-gate 	 *
4667*7c478bd9Sstevel@tonic-gate 	 * Handle the case where default route is
4668*7c478bd9Sstevel@tonic-gate 	 * requested by specifying type as one of the possible
4669*7c478bd9Sstevel@tonic-gate 	 * types for that can have a zero mask (IRE_DEFAULT and IRE_INTERFACE).
4670*7c478bd9Sstevel@tonic-gate 	 *
4671*7c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_MASK is specified, then the appropriate default route
4672*7c478bd9Sstevel@tonic-gate 	 * would have been found above if it exists so it isn't looked up here.
4673*7c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_DEFAULT was also specified, then a default route will be
4674*7c478bd9Sstevel@tonic-gate 	 * searched for later.
4675*7c478bd9Sstevel@tonic-gate 	 */
4676*7c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_TYPE | MATCH_IRE_MASK)) == MATCH_IRE_TYPE &&
4677*7c478bd9Sstevel@tonic-gate 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
4678*7c478bd9Sstevel@tonic-gate 		if ((ip_forwarding_table[0])) {
4679*7c478bd9Sstevel@tonic-gate 			/* addr & mask is zero for defaults */
4680*7c478bd9Sstevel@tonic-gate 			irb_ptr = &ip_forwarding_table[0][
4681*7c478bd9Sstevel@tonic-gate 			    IRE_ADDR_HASH(0, ip_ftable_hash_size)];
4682*7c478bd9Sstevel@tonic-gate 			rw_enter(&irb_ptr->irb_lock, RW_READER);
4683*7c478bd9Sstevel@tonic-gate 			for (ire = irb_ptr->irb_ire; ire != NULL;
4684*7c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
4685*7c478bd9Sstevel@tonic-gate 				if (ire->ire_marks & IRE_MARK_CONDEMNED)
4686*7c478bd9Sstevel@tonic-gate 					continue;
4687*7c478bd9Sstevel@tonic-gate 				if (ire_match_args(ire, addr, (ipaddr_t)0,
4688*7c478bd9Sstevel@tonic-gate 				    gateway, type, ipif, zoneid, ihandle,
4689*7c478bd9Sstevel@tonic-gate 				    flags))
4690*7c478bd9Sstevel@tonic-gate 					goto found_ire;
4691*7c478bd9Sstevel@tonic-gate 			}
4692*7c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
4693*7c478bd9Sstevel@tonic-gate 		}
4694*7c478bd9Sstevel@tonic-gate 	}
4695*7c478bd9Sstevel@tonic-gate 	/*
4696*7c478bd9Sstevel@tonic-gate 	 * we come here only if no route is found.
4697*7c478bd9Sstevel@tonic-gate 	 * see if the default route can be used which is allowed
4698*7c478bd9Sstevel@tonic-gate 	 * only if the default matching criteria is specified.
4699*7c478bd9Sstevel@tonic-gate 	 * The ip_ire_default_count tracks the number of IRE_DEFAULT
4700*7c478bd9Sstevel@tonic-gate 	 * entries. However, the ip_forwarding_table[0] also contains
4701*7c478bd9Sstevel@tonic-gate 	 * interface routes thus the count can be zero.
4702*7c478bd9Sstevel@tonic-gate 	 */
4703*7c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
4704*7c478bd9Sstevel@tonic-gate 	    MATCH_IRE_DEFAULT) {
4705*7c478bd9Sstevel@tonic-gate 		ire_t	*ire_origin;
4706*7c478bd9Sstevel@tonic-gate 		uint_t  g_index;
4707*7c478bd9Sstevel@tonic-gate 		uint_t	index;
4708*7c478bd9Sstevel@tonic-gate 
4709*7c478bd9Sstevel@tonic-gate 		if (ip_forwarding_table[0] == NULL)
4710*7c478bd9Sstevel@tonic-gate 			return (NULL);
4711*7c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[0])[0];
4712*7c478bd9Sstevel@tonic-gate 
4713*7c478bd9Sstevel@tonic-gate 		/*
4714*7c478bd9Sstevel@tonic-gate 		 * Keep a tab on the bucket while looking the IRE_DEFAULT
4715*7c478bd9Sstevel@tonic-gate 		 * entries. We need to keep track of a particular IRE
4716*7c478bd9Sstevel@tonic-gate 		 * (ire_origin) so this ensures that it will not be unlinked
4717*7c478bd9Sstevel@tonic-gate 		 * from the hash list during the recursive lookup below.
4718*7c478bd9Sstevel@tonic-gate 		 */
4719*7c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb_ptr);
4720*7c478bd9Sstevel@tonic-gate 		ire = irb_ptr->irb_ire;
4721*7c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
4722*7c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb_ptr);
4723*7c478bd9Sstevel@tonic-gate 			return (NULL);
4724*7c478bd9Sstevel@tonic-gate 		}
4725*7c478bd9Sstevel@tonic-gate 
4726*7c478bd9Sstevel@tonic-gate 		/*
4727*7c478bd9Sstevel@tonic-gate 		 * Get the index first, since it can be changed by other
4728*7c478bd9Sstevel@tonic-gate 		 * threads. Then get to the right default route skipping
4729*7c478bd9Sstevel@tonic-gate 		 * default interface routes if any. As we hold a reference on
4730*7c478bd9Sstevel@tonic-gate 		 * the IRE bucket, ip_ire_default_count can only increase so we
4731*7c478bd9Sstevel@tonic-gate 		 * can't reach the end of the hash list unexpectedly.
4732*7c478bd9Sstevel@tonic-gate 		 */
4733*7c478bd9Sstevel@tonic-gate 		if (ip_ire_default_count != 0) {
4734*7c478bd9Sstevel@tonic-gate 			g_index = ip_ire_default_index++;
4735*7c478bd9Sstevel@tonic-gate 			index = g_index % ip_ire_default_count;
4736*7c478bd9Sstevel@tonic-gate 			while (index != 0) {
4737*7c478bd9Sstevel@tonic-gate 				if (!(ire->ire_type & IRE_INTERFACE))
4738*7c478bd9Sstevel@tonic-gate 					index--;
4739*7c478bd9Sstevel@tonic-gate 				ire = ire->ire_next;
4740*7c478bd9Sstevel@tonic-gate 			}
4741*7c478bd9Sstevel@tonic-gate 			ASSERT(ire != NULL);
4742*7c478bd9Sstevel@tonic-gate 		} else {
4743*7c478bd9Sstevel@tonic-gate 			/*
4744*7c478bd9Sstevel@tonic-gate 			 * No default routes, so we only have default interface
4745*7c478bd9Sstevel@tonic-gate 			 * routes: don't enter the first loop.
4746*7c478bd9Sstevel@tonic-gate 			 */
4747*7c478bd9Sstevel@tonic-gate 			ire = NULL;
4748*7c478bd9Sstevel@tonic-gate 		}
4749*7c478bd9Sstevel@tonic-gate 
4750*7c478bd9Sstevel@tonic-gate 		/*
4751*7c478bd9Sstevel@tonic-gate 		 * Round-robin the default routers list looking for a route that
4752*7c478bd9Sstevel@tonic-gate 		 * matches the passed in parameters. If we can't find a default
4753*7c478bd9Sstevel@tonic-gate 		 * route (IRE_DEFAULT), look for interface default routes.
4754*7c478bd9Sstevel@tonic-gate 		 * We start with the ire we found above and we walk the hash
4755*7c478bd9Sstevel@tonic-gate 		 * list until we're back where we started, see
4756*7c478bd9Sstevel@tonic-gate 		 * ire_get_next_default_ire(). It doesn't matter if default
4757*7c478bd9Sstevel@tonic-gate 		 * routes are added or deleted by other threads - we know this
4758*7c478bd9Sstevel@tonic-gate 		 * ire will stay in the list because we hold a reference on the
4759*7c478bd9Sstevel@tonic-gate 		 * ire bucket.
4760*7c478bd9Sstevel@tonic-gate 		 * NB: if we only have interface default routes, ire is NULL so
4761*7c478bd9Sstevel@tonic-gate 		 * we don't even enter this loop (see above).
4762*7c478bd9Sstevel@tonic-gate 		 */
4763*7c478bd9Sstevel@tonic-gate 		ire_origin = ire;
4764*7c478bd9Sstevel@tonic-gate 		for (; ire != NULL;
4765*7c478bd9Sstevel@tonic-gate 		    ire = ire_get_next_default_ire(ire, ire_origin)) {
4766*7c478bd9Sstevel@tonic-gate 
4767*7c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, (ipaddr_t)0,
4768*7c478bd9Sstevel@tonic-gate 			    gateway, type, ipif, zoneid, ihandle, flags)) {
4769*7c478bd9Sstevel@tonic-gate 				int match_flags = 0;
4770*7c478bd9Sstevel@tonic-gate 				ire_t *rire;
4771*7c478bd9Sstevel@tonic-gate 
4772*7c478bd9Sstevel@tonic-gate 				/*
4773*7c478bd9Sstevel@tonic-gate 				 * The potentially expensive call to
4774*7c478bd9Sstevel@tonic-gate 				 * ire_route_lookup() is avoided when we have
4775*7c478bd9Sstevel@tonic-gate 				 * only one default route.
4776*7c478bd9Sstevel@tonic-gate 				 */
4777*7c478bd9Sstevel@tonic-gate 				if (ip_ire_default_count == 1 ||
4778*7c478bd9Sstevel@tonic-gate 				    zoneid == ALL_ZONES) {
4779*7c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
4780*7c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb_ptr);
4781*7c478bd9Sstevel@tonic-gate 					goto found_ire_held;
4782*7c478bd9Sstevel@tonic-gate 				}
4783*7c478bd9Sstevel@tonic-gate 				/*
4784*7c478bd9Sstevel@tonic-gate 				 * When we're in a local zone, we're only
4785*7c478bd9Sstevel@tonic-gate 				 * interested in default routers that are
4786*7c478bd9Sstevel@tonic-gate 				 * reachable through ipifs within our zone.
4787*7c478bd9Sstevel@tonic-gate 				 */
4788*7c478bd9Sstevel@tonic-gate 				if (ire->ire_ipif != NULL) {
4789*7c478bd9Sstevel@tonic-gate 					match_flags |= MATCH_IRE_ILL_GROUP;
4790*7c478bd9Sstevel@tonic-gate 				}
4791*7c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
4792*7c478bd9Sstevel@tonic-gate 				    0, 0, 0, ire->ire_ipif, NULL, zoneid,
4793*7c478bd9Sstevel@tonic-gate 				    match_flags);
4794*7c478bd9Sstevel@tonic-gate 				if (rire != NULL) {
4795*7c478bd9Sstevel@tonic-gate 					ire_refrele(rire);
4796*7c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
4797*7c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb_ptr);
4798*7c478bd9Sstevel@tonic-gate 					goto found_ire_held;
4799*7c478bd9Sstevel@tonic-gate 				}
4800*7c478bd9Sstevel@tonic-gate 			}
4801*7c478bd9Sstevel@tonic-gate 		}
4802*7c478bd9Sstevel@tonic-gate 		/*
4803*7c478bd9Sstevel@tonic-gate 		 * Either there are no default routes or we could not
4804*7c478bd9Sstevel@tonic-gate 		 * find a default route. Look for a interface default
4805*7c478bd9Sstevel@tonic-gate 		 * route matching the args passed in. No round robin
4806*7c478bd9Sstevel@tonic-gate 		 * here. Just pick the right one.
4807*7c478bd9Sstevel@tonic-gate 		 */
4808*7c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
4809*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
4810*7c478bd9Sstevel@tonic-gate 
4811*7c478bd9Sstevel@tonic-gate 			if (!(ire->ire_type & IRE_INTERFACE))
4812*7c478bd9Sstevel@tonic-gate 				continue;
4813*7c478bd9Sstevel@tonic-gate 
4814*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
4815*7c478bd9Sstevel@tonic-gate 				continue;
4816*7c478bd9Sstevel@tonic-gate 
4817*7c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, (ipaddr_t)0,
4818*7c478bd9Sstevel@tonic-gate 			    gateway, type, ipif, zoneid, ihandle, flags)) {
4819*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
4820*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb_ptr);
4821*7c478bd9Sstevel@tonic-gate 				goto found_ire_held;
4822*7c478bd9Sstevel@tonic-gate 			}
4823*7c478bd9Sstevel@tonic-gate 		}
4824*7c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb_ptr);
4825*7c478bd9Sstevel@tonic-gate 	}
4826*7c478bd9Sstevel@tonic-gate 	ASSERT(ire == NULL);
4827*7c478bd9Sstevel@tonic-gate 	return (NULL);
4828*7c478bd9Sstevel@tonic-gate found_ire:
4829*7c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_marks & IRE_MARK_CONDEMNED) == 0);
4830*7c478bd9Sstevel@tonic-gate 	IRE_REFHOLD(ire);
4831*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
4832*7c478bd9Sstevel@tonic-gate 
4833*7c478bd9Sstevel@tonic-gate found_ire_held:
4834*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
4835*7c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
4836*7c478bd9Sstevel@tonic-gate 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
4837*7c478bd9Sstevel@tonic-gate 		return (ire);
4838*7c478bd9Sstevel@tonic-gate 	}
4839*7c478bd9Sstevel@tonic-gate 	/*
4840*7c478bd9Sstevel@tonic-gate 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
4841*7c478bd9Sstevel@tonic-gate 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
4842*7c478bd9Sstevel@tonic-gate 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
4843*7c478bd9Sstevel@tonic-gate 	 * IRE (one of the prefix types), then it is necessary to fill in the
4844*7c478bd9Sstevel@tonic-gate 	 * parent IRE pointed to by pire, and then lookup the gateway address of
4845*7c478bd9Sstevel@tonic-gate 	 * the parent.  For backwards compatiblity, if this lookup returns an
4846*7c478bd9Sstevel@tonic-gate 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
4847*7c478bd9Sstevel@tonic-gate 	 * of lookup is done.
4848*7c478bd9Sstevel@tonic-gate 	 */
4849*7c478bd9Sstevel@tonic-gate 	if (flags & MATCH_IRE_RECURSIVE) {
4850*7c478bd9Sstevel@tonic-gate 		ipif_t	*gw_ipif;
4851*7c478bd9Sstevel@tonic-gate 		int match_flags = MATCH_IRE_DSTONLY;
4852*7c478bd9Sstevel@tonic-gate 		ire_t *save_ire;
4853*7c478bd9Sstevel@tonic-gate 
4854*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type & IRE_INTERFACE)
4855*7c478bd9Sstevel@tonic-gate 			return (ire);
4856*7c478bd9Sstevel@tonic-gate 		if (pire != NULL)
4857*7c478bd9Sstevel@tonic-gate 			*pire = ire;
4858*7c478bd9Sstevel@tonic-gate 		/*
4859*7c478bd9Sstevel@tonic-gate 		 * If we can't find an IRE_INTERFACE or the caller has not
4860*7c478bd9Sstevel@tonic-gate 		 * asked for pire, we need to REFRELE the save_ire.
4861*7c478bd9Sstevel@tonic-gate 		 */
4862*7c478bd9Sstevel@tonic-gate 		save_ire = ire;
4863*7c478bd9Sstevel@tonic-gate 
4864*7c478bd9Sstevel@tonic-gate 		/*
4865*7c478bd9Sstevel@tonic-gate 		 * Currently MATCH_IRE_ILL is never used with
4866*7c478bd9Sstevel@tonic-gate 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
4867*7c478bd9Sstevel@tonic-gate 		 * sending out packets as MATCH_IRE_ILL is used only
4868*7c478bd9Sstevel@tonic-gate 		 * for communicating with on-link hosts. We can't assert
4869*7c478bd9Sstevel@tonic-gate 		 * that here as RTM_GET calls this function with
4870*7c478bd9Sstevel@tonic-gate 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
4871*7c478bd9Sstevel@tonic-gate 		 * We have already used the MATCH_IRE_ILL in determining
4872*7c478bd9Sstevel@tonic-gate 		 * the right prefix route at this point. To match the
4873*7c478bd9Sstevel@tonic-gate 		 * behavior of how we locate routes while sending out
4874*7c478bd9Sstevel@tonic-gate 		 * packets, we don't want to use MATCH_IRE_ILL below
4875*7c478bd9Sstevel@tonic-gate 		 * while locating the interface route.
4876*7c478bd9Sstevel@tonic-gate 		 */
4877*7c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
4878*7c478bd9Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL_GROUP;
4879*7c478bd9Sstevel@tonic-gate 
4880*7c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
4881*7c478bd9Sstevel@tonic-gate 		    ire->ire_ipif, NULL, zoneid, match_flags);
4882*7c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
4883*7c478bd9Sstevel@tonic-gate 			/*
4884*7c478bd9Sstevel@tonic-gate 			 * Do not release the parent ire if MATCH_IRE_PARENT
4885*7c478bd9Sstevel@tonic-gate 			 * is set. Also return it via ire.
4886*7c478bd9Sstevel@tonic-gate 			 */
4887*7c478bd9Sstevel@tonic-gate 			if (flags & MATCH_IRE_PARENT) {
4888*7c478bd9Sstevel@tonic-gate 				if (pire != NULL) {
4889*7c478bd9Sstevel@tonic-gate 					/*
4890*7c478bd9Sstevel@tonic-gate 					 * Need an extra REFHOLD, if the parent
4891*7c478bd9Sstevel@tonic-gate 					 * ire is returned via both ire and
4892*7c478bd9Sstevel@tonic-gate 					 * pire.
4893*7c478bd9Sstevel@tonic-gate 					 */
4894*7c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(save_ire);
4895*7c478bd9Sstevel@tonic-gate 				}
4896*7c478bd9Sstevel@tonic-gate 				ire = save_ire;
4897*7c478bd9Sstevel@tonic-gate 			} else {
4898*7c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
4899*7c478bd9Sstevel@tonic-gate 				if (pire != NULL)
4900*7c478bd9Sstevel@tonic-gate 					*pire = NULL;
4901*7c478bd9Sstevel@tonic-gate 			}
4902*7c478bd9Sstevel@tonic-gate 			return (ire);
4903*7c478bd9Sstevel@tonic-gate 		}
4904*7c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
4905*7c478bd9Sstevel@tonic-gate 			/*
4906*7c478bd9Sstevel@tonic-gate 			 * If the caller did not ask for pire, release
4907*7c478bd9Sstevel@tonic-gate 			 * it now.
4908*7c478bd9Sstevel@tonic-gate 			 */
4909*7c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
4910*7c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
4911*7c478bd9Sstevel@tonic-gate 			}
4912*7c478bd9Sstevel@tonic-gate 			return (ire);
4913*7c478bd9Sstevel@tonic-gate 		}
4914*7c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_TYPE;
4915*7c478bd9Sstevel@tonic-gate 		gw_addr = ire->ire_gateway_addr;
4916*7c478bd9Sstevel@tonic-gate 		gw_ipif = ire->ire_ipif;
4917*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
4918*7c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(gw_addr, 0, 0,
4919*7c478bd9Sstevel@tonic-gate 		    (IRE_CACHETABLE | IRE_INTERFACE), gw_ipif, NULL, zoneid,
4920*7c478bd9Sstevel@tonic-gate 		    match_flags);
4921*7c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
4922*7c478bd9Sstevel@tonic-gate 			/*
4923*7c478bd9Sstevel@tonic-gate 			 * Do not release the parent ire if MATCH_IRE_PARENT
4924*7c478bd9Sstevel@tonic-gate 			 * is set. Also return it via ire.
4925*7c478bd9Sstevel@tonic-gate 			 */
4926*7c478bd9Sstevel@tonic-gate 			if (flags & MATCH_IRE_PARENT) {
4927*7c478bd9Sstevel@tonic-gate 				if (pire != NULL) {
4928*7c478bd9Sstevel@tonic-gate 					/*
4929*7c478bd9Sstevel@tonic-gate 					 * Need an extra REFHOLD, if the
4930*7c478bd9Sstevel@tonic-gate 					 * parent ire is returned via both
4931*7c478bd9Sstevel@tonic-gate 					 * ire and pire.
4932*7c478bd9Sstevel@tonic-gate 					 */
4933*7c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(save_ire);
4934*7c478bd9Sstevel@tonic-gate 				}
4935*7c478bd9Sstevel@tonic-gate 				ire = save_ire;
4936*7c478bd9Sstevel@tonic-gate 			} else {
4937*7c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
4938*7c478bd9Sstevel@tonic-gate 				if (pire != NULL)
4939*7c478bd9Sstevel@tonic-gate 					*pire = NULL;
4940*7c478bd9Sstevel@tonic-gate 			}
4941*7c478bd9Sstevel@tonic-gate 			return (ire);
4942*7c478bd9Sstevel@tonic-gate 		} else if (pire == NULL) {
4943*7c478bd9Sstevel@tonic-gate 			/*
4944*7c478bd9Sstevel@tonic-gate 			 * If the caller did not ask for pire, release
4945*7c478bd9Sstevel@tonic-gate 			 * it now.
4946*7c478bd9Sstevel@tonic-gate 			 */
4947*7c478bd9Sstevel@tonic-gate 			ire_refrele(save_ire);
4948*7c478bd9Sstevel@tonic-gate 		}
4949*7c478bd9Sstevel@tonic-gate 		return (ire);
4950*7c478bd9Sstevel@tonic-gate 	}
4951*7c478bd9Sstevel@tonic-gate 	ASSERT(pire == NULL || *pire == NULL);
4952*7c478bd9Sstevel@tonic-gate 	return (ire);
4953*7c478bd9Sstevel@tonic-gate }
4954*7c478bd9Sstevel@tonic-gate 
4955*7c478bd9Sstevel@tonic-gate /*
4956*7c478bd9Sstevel@tonic-gate  * Looks up cache table for a route.
4957*7c478bd9Sstevel@tonic-gate  * specific lookup can be indicated by
4958*7c478bd9Sstevel@tonic-gate  * passing the MATCH_* flags and the
4959*7c478bd9Sstevel@tonic-gate  * necessary parameters.
4960*7c478bd9Sstevel@tonic-gate  */
4961*7c478bd9Sstevel@tonic-gate ire_t *
4962*7c478bd9Sstevel@tonic-gate ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, ipif_t *ipif,
4963*7c478bd9Sstevel@tonic-gate     zoneid_t zoneid, int flags)
4964*7c478bd9Sstevel@tonic-gate {
4965*7c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
4966*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
4967*7c478bd9Sstevel@tonic-gate 
4968*7c478bd9Sstevel@tonic-gate 	/*
4969*7c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
4970*7c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
4971*7c478bd9Sstevel@tonic-gate 	 */
4972*7c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
4973*7c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
4974*7c478bd9Sstevel@tonic-gate 		return (NULL);
4975*7c478bd9Sstevel@tonic-gate 
4976*7c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
4977*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
4978*7c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
4979*7c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
4980*7c478bd9Sstevel@tonic-gate 			continue;
4981*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_mask == IP_HOST_MASK);
4982*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
4983*7c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, addr, ire->ire_mask, gateway, type,
4984*7c478bd9Sstevel@tonic-gate 		    ipif, zoneid, 0, flags)) {
4985*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
4986*7c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
4987*7c478bd9Sstevel@tonic-gate 			return (ire);
4988*7c478bd9Sstevel@tonic-gate 		}
4989*7c478bd9Sstevel@tonic-gate 	}
4990*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
4991*7c478bd9Sstevel@tonic-gate 	return (NULL);
4992*7c478bd9Sstevel@tonic-gate }
4993*7c478bd9Sstevel@tonic-gate 
4994*7c478bd9Sstevel@tonic-gate /*
4995*7c478bd9Sstevel@tonic-gate  * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers
4996*7c478bd9Sstevel@tonic-gate  * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get
4997*7c478bd9Sstevel@tonic-gate  * to the hidden ones.
4998*7c478bd9Sstevel@tonic-gate  */
4999*7c478bd9Sstevel@tonic-gate ire_t *
5000*7c478bd9Sstevel@tonic-gate ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid)
5001*7c478bd9Sstevel@tonic-gate {
5002*7c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
5003*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
5004*7c478bd9Sstevel@tonic-gate 
5005*7c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
5006*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
5007*7c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
5008*7c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
5009*7c478bd9Sstevel@tonic-gate 			continue;
5010*7c478bd9Sstevel@tonic-gate 		if (ire->ire_addr == addr) {
5011*7c478bd9Sstevel@tonic-gate 			if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid ||
5012*7c478bd9Sstevel@tonic-gate 			    ire->ire_type == IRE_LOCAL) {
5013*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
5014*7c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
5015*7c478bd9Sstevel@tonic-gate 				return (ire);
5016*7c478bd9Sstevel@tonic-gate 			}
5017*7c478bd9Sstevel@tonic-gate 		}
5018*7c478bd9Sstevel@tonic-gate 	}
5019*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
5020*7c478bd9Sstevel@tonic-gate 	return (NULL);
5021*7c478bd9Sstevel@tonic-gate }
5022*7c478bd9Sstevel@tonic-gate 
5023*7c478bd9Sstevel@tonic-gate /*
5024*7c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
5025*7c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
5026*7c478bd9Sstevel@tonic-gate  *
5027*7c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an offlink destn based
5028*7c478bd9Sstevel@tonic-gate  * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire
5029*7c478bd9Sstevel@tonic-gate  * as found by ip_newroute(). We are called from ip_newroute() in
5030*7c478bd9Sstevel@tonic-gate  * the IRE_CACHE case.
5031*7c478bd9Sstevel@tonic-gate  */
5032*7c478bd9Sstevel@tonic-gate ire_t *
5033*7c478bd9Sstevel@tonic-gate ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire)
5034*7c478bd9Sstevel@tonic-gate {
5035*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5036*7c478bd9Sstevel@tonic-gate 	int	match_flags;
5037*7c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
5038*7c478bd9Sstevel@tonic-gate 	ipif_t	*gw_ipif;
5039*7c478bd9Sstevel@tonic-gate 
5040*7c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL && pire != NULL);
5041*7c478bd9Sstevel@tonic-gate 
5042*7c478bd9Sstevel@tonic-gate 	/*
5043*7c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
5044*7c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
5045*7c478bd9Sstevel@tonic-gate 	 */
5046*7c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
5047*7c478bd9Sstevel@tonic-gate 	/*
5048*7c478bd9Sstevel@tonic-gate 	 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only
5049*7c478bd9Sstevel@tonic-gate 	 * for on-link hosts. We should never be here for onlink.
5050*7c478bd9Sstevel@tonic-gate 	 * Thus, use MATCH_IRE_ILL_GROUP.
5051*7c478bd9Sstevel@tonic-gate 	 */
5052*7c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
5053*7c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
5054*7c478bd9Sstevel@tonic-gate 	/*
5055*7c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
5056*7c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for the gateway it set its
5057*7c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
5058*7c478bd9Sstevel@tonic-gate 	 */
5059*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
5060*7c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle,
5061*7c478bd9Sstevel@tonic-gate 	    match_flags);
5062*7c478bd9Sstevel@tonic-gate 	if (ire != NULL)
5063*7c478bd9Sstevel@tonic-gate 		return (ire);
5064*7c478bd9Sstevel@tonic-gate 	/*
5065*7c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
5066*7c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
5067*7c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
5068*7c478bd9Sstevel@tonic-gate 	 *
5069*7c478bd9Sstevel@tonic-gate 	 * Assume we are trying to ping some offlink destn, and we have the
5070*7c478bd9Sstevel@tonic-gate 	 * routing table below.
5071*7c478bd9Sstevel@tonic-gate 	 *
5072*7c478bd9Sstevel@tonic-gate 	 * Eg.	default	- gw1		<--- pire	(line 1)
5073*7c478bd9Sstevel@tonic-gate 	 *	gw1	- gw2				(line 2)
5074*7c478bd9Sstevel@tonic-gate 	 *	gw2	- hme0				(line 3)
5075*7c478bd9Sstevel@tonic-gate 	 *
5076*7c478bd9Sstevel@tonic-gate 	 * If we already have a cache ire for gw1 in 'cire', the
5077*7c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup above would have failed, since there is no
5078*7c478bd9Sstevel@tonic-gate 	 * interface ire to reach gw1. We will fallthru below.
5079*7c478bd9Sstevel@tonic-gate 	 *
5080*7c478bd9Sstevel@tonic-gate 	 * Here we duplicate the steps that ire_ftable_lookup() did in
5081*7c478bd9Sstevel@tonic-gate 	 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case.
5082*7c478bd9Sstevel@tonic-gate 	 * The differences are the following
5083*7c478bd9Sstevel@tonic-gate 	 * i.   We want the interface ire only, so we call ire_ftable_lookup()
5084*7c478bd9Sstevel@tonic-gate 	 *	instead of ire_route_lookup()
5085*7c478bd9Sstevel@tonic-gate 	 * ii.  We look for only prefix routes in the 1st call below.
5086*7c478bd9Sstevel@tonic-gate 	 * ii.  We want to match on the ihandle in the 2nd call below.
5087*7c478bd9Sstevel@tonic-gate 	 */
5088*7c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE;
5089*7c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
5090*7c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
5091*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET,
5092*7c478bd9Sstevel@tonic-gate 	    pire->ire_ipif, NULL, ALL_ZONES, 0, match_flags);
5093*7c478bd9Sstevel@tonic-gate 	if (ire == NULL)
5094*7c478bd9Sstevel@tonic-gate 		return (NULL);
5095*7c478bd9Sstevel@tonic-gate 	/*
5096*7c478bd9Sstevel@tonic-gate 	 * At this point 'ire' corresponds to the entry shown in line 2.
5097*7c478bd9Sstevel@tonic-gate 	 * gw_addr is 'gw2' in the example above.
5098*7c478bd9Sstevel@tonic-gate 	 */
5099*7c478bd9Sstevel@tonic-gate 	gw_addr = ire->ire_gateway_addr;
5100*7c478bd9Sstevel@tonic-gate 	gw_ipif = ire->ire_ipif;
5101*7c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
5102*7c478bd9Sstevel@tonic-gate 
5103*7c478bd9Sstevel@tonic-gate 	match_flags |= MATCH_IRE_IHANDLE;
5104*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
5105*7c478bd9Sstevel@tonic-gate 	    gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, match_flags);
5106*7c478bd9Sstevel@tonic-gate 	return (ire);
5107*7c478bd9Sstevel@tonic-gate }
5108*7c478bd9Sstevel@tonic-gate 
5109*7c478bd9Sstevel@tonic-gate /*
5110*7c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
5111*7c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
5112*7c478bd9Sstevel@tonic-gate  *
5113*7c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an onlink destn. or
5114*7c478bd9Sstevel@tonic-gate  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
5115*7c478bd9Sstevel@tonic-gate  * case, after the ire has come back from ARP.
5116*7c478bd9Sstevel@tonic-gate  */
5117*7c478bd9Sstevel@tonic-gate ire_t *
5118*7c478bd9Sstevel@tonic-gate ire_ihandle_lookup_onlink(ire_t *cire)
5119*7c478bd9Sstevel@tonic-gate {
5120*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5121*7c478bd9Sstevel@tonic-gate 	int	match_flags;
5122*7c478bd9Sstevel@tonic-gate 	int	i;
5123*7c478bd9Sstevel@tonic-gate 	int	j;
5124*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
5125*7c478bd9Sstevel@tonic-gate 
5126*7c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL);
5127*7c478bd9Sstevel@tonic-gate 
5128*7c478bd9Sstevel@tonic-gate 	/*
5129*7c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
5130*7c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
5131*7c478bd9Sstevel@tonic-gate 	 */
5132*7c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
5133*7c478bd9Sstevel@tonic-gate 	/*
5134*7c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
5135*7c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
5136*7c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
5137*7c478bd9Sstevel@tonic-gate 	 */
5138*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
5139*7c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
5140*7c478bd9Sstevel@tonic-gate 	    match_flags);
5141*7c478bd9Sstevel@tonic-gate 	if (ire != NULL)
5142*7c478bd9Sstevel@tonic-gate 		return (ire);
5143*7c478bd9Sstevel@tonic-gate 	/*
5144*7c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
5145*7c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
5146*7c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
5147*7c478bd9Sstevel@tonic-gate 	 *
5148*7c478bd9Sstevel@tonic-gate 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
5149*7c478bd9Sstevel@tonic-gate 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
5150*7c478bd9Sstevel@tonic-gate 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
5151*7c478bd9Sstevel@tonic-gate 	 * doing ire_route_lookup() twice and returning an interface ire.
5152*7c478bd9Sstevel@tonic-gate 	 *
5153*7c478bd9Sstevel@tonic-gate 	 * Eg. default	-	gw1			(line 1)
5154*7c478bd9Sstevel@tonic-gate 	 *	gw1	-	gw2			(line 2)
5155*7c478bd9Sstevel@tonic-gate 	 *	gw2	-	hme0			(line 3)
5156*7c478bd9Sstevel@tonic-gate 	 *
5157*7c478bd9Sstevel@tonic-gate 	 * In the above example, ip_newroute() tried to create the cache ire
5158*7c478bd9Sstevel@tonic-gate 	 * 'cire' for gw1, based on the interface route in line 3. The
5159*7c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup() above fails, because there is no interface route
5160*7c478bd9Sstevel@tonic-gate 	 * to reach gw1. (it is gw2). We fall thru below.
5161*7c478bd9Sstevel@tonic-gate 	 *
5162*7c478bd9Sstevel@tonic-gate 	 * Do a brute force search based on the ihandle in a subset of the
5163*7c478bd9Sstevel@tonic-gate 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
5164*7c478bd9Sstevel@tonic-gate 	 * things become very complex, since we don't have 'pire' in this
5165*7c478bd9Sstevel@tonic-gate 	 * case. (Also note that this method is not possible in the offlink
5166*7c478bd9Sstevel@tonic-gate 	 * case because we don't know the mask)
5167*7c478bd9Sstevel@tonic-gate 	 */
5168*7c478bd9Sstevel@tonic-gate 	i = ip_mask_to_plen(cire->ire_cmask);
5169*7c478bd9Sstevel@tonic-gate 	if ((ip_forwarding_table[i]) == NULL)
5170*7c478bd9Sstevel@tonic-gate 		return (NULL);
5171*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < ip_ftable_hash_size; j++) {
5172*7c478bd9Sstevel@tonic-gate 		irb_ptr = &ip_forwarding_table[i][j];
5173*7c478bd9Sstevel@tonic-gate 		rw_enter(&irb_ptr->irb_lock, RW_READER);
5174*7c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
5175*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
5176*7c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
5177*7c478bd9Sstevel@tonic-gate 				continue;
5178*7c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
5179*7c478bd9Sstevel@tonic-gate 			    (ire->ire_ihandle == cire->ire_ihandle)) {
5180*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
5181*7c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
5182*7c478bd9Sstevel@tonic-gate 				return (ire);
5183*7c478bd9Sstevel@tonic-gate 			}
5184*7c478bd9Sstevel@tonic-gate 		}
5185*7c478bd9Sstevel@tonic-gate 		rw_exit(&irb_ptr->irb_lock);
5186*7c478bd9Sstevel@tonic-gate 	}
5187*7c478bd9Sstevel@tonic-gate 	return (NULL);
5188*7c478bd9Sstevel@tonic-gate }
5189*7c478bd9Sstevel@tonic-gate 
5190*7c478bd9Sstevel@tonic-gate /*
5191*7c478bd9Sstevel@tonic-gate  * ire_mrtun_lookup() is called by ip_rput() when packet is to be
5192*7c478bd9Sstevel@tonic-gate  * tunneled through reverse tunnel. This is only supported for
5193*7c478bd9Sstevel@tonic-gate  * IPv4 packets
5194*7c478bd9Sstevel@tonic-gate  */
5195*7c478bd9Sstevel@tonic-gate 
5196*7c478bd9Sstevel@tonic-gate ire_t *
5197*7c478bd9Sstevel@tonic-gate ire_mrtun_lookup(ipaddr_t srcaddr, ill_t *ill)
5198*7c478bd9Sstevel@tonic-gate {
5199*7c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
5200*7c478bd9Sstevel@tonic-gate 	ire_t *ire;
5201*7c478bd9Sstevel@tonic-gate 
5202*7c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
5203*7c478bd9Sstevel@tonic-gate 	ASSERT(!(ill->ill_isv6));
5204*7c478bd9Sstevel@tonic-gate 
5205*7c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table == NULL)
5206*7c478bd9Sstevel@tonic-gate 		return (NULL);
5207*7c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_mrtun_table[IRE_ADDR_HASH(srcaddr, IP_MRTUN_TABLE_SIZE)];
5208*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
5209*7c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
5210*7c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
5211*7c478bd9Sstevel@tonic-gate 			continue;
5212*7c478bd9Sstevel@tonic-gate 		if ((ire->ire_in_src_addr == srcaddr) &&
5213*7c478bd9Sstevel@tonic-gate 		    ire->ire_in_ill == ill) {
5214*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
5215*7c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
5216*7c478bd9Sstevel@tonic-gate 			return (ire);
5217*7c478bd9Sstevel@tonic-gate 		}
5218*7c478bd9Sstevel@tonic-gate 	}
5219*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
5220*7c478bd9Sstevel@tonic-gate 	return (NULL);
5221*7c478bd9Sstevel@tonic-gate }
5222*7c478bd9Sstevel@tonic-gate 
5223*7c478bd9Sstevel@tonic-gate /*
5224*7c478bd9Sstevel@tonic-gate  * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER
5225*7c478bd9Sstevel@tonic-gate  * ire associated with the specified ipif.
5226*7c478bd9Sstevel@tonic-gate  *
5227*7c478bd9Sstevel@tonic-gate  * This might occasionally be called when IPIF_UP is not set since
5228*7c478bd9Sstevel@tonic-gate  * the IP_MULTICAST_IF as well as creating interface routes
5229*7c478bd9Sstevel@tonic-gate  * allows specifying a down ipif (ipif_lookup* match ipifs that are down).
5230*7c478bd9Sstevel@tonic-gate  *
5231*7c478bd9Sstevel@tonic-gate  * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on
5232*7c478bd9Sstevel@tonic-gate  * the ipif, this routine might return NULL.
5233*7c478bd9Sstevel@tonic-gate  */
5234*7c478bd9Sstevel@tonic-gate ire_t *
5235*7c478bd9Sstevel@tonic-gate ipif_to_ire(ipif_t *ipif)
5236*7c478bd9Sstevel@tonic-gate {
5237*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5238*7c478bd9Sstevel@tonic-gate 
5239*7c478bd9Sstevel@tonic-gate 	ASSERT(!ipif->ipif_isv6);
5240*7c478bd9Sstevel@tonic-gate 	if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5241*7c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK,
5242*7c478bd9Sstevel@tonic-gate 		    ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
5243*7c478bd9Sstevel@tonic-gate 	} else if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5244*7c478bd9Sstevel@tonic-gate 		/* In this case we need to lookup destination address. */
5245*7c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0,
5246*7c478bd9Sstevel@tonic-gate 		    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
5247*7c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK));
5248*7c478bd9Sstevel@tonic-gate 	} else {
5249*7c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_subnet,
5250*7c478bd9Sstevel@tonic-gate 		    ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL,
5251*7c478bd9Sstevel@tonic-gate 		    ALL_ZONES, 0, (MATCH_IRE_TYPE | MATCH_IRE_IPIF |
5252*7c478bd9Sstevel@tonic-gate 		    MATCH_IRE_MASK));
5253*7c478bd9Sstevel@tonic-gate 	}
5254*7c478bd9Sstevel@tonic-gate 	return (ire);
5255*7c478bd9Sstevel@tonic-gate }
5256*7c478bd9Sstevel@tonic-gate 
5257*7c478bd9Sstevel@tonic-gate /*
5258*7c478bd9Sstevel@tonic-gate  * ire_walk function.
5259*7c478bd9Sstevel@tonic-gate  * Count the number of IRE_CACHE entries in different categories.
5260*7c478bd9Sstevel@tonic-gate  */
5261*7c478bd9Sstevel@tonic-gate void
5262*7c478bd9Sstevel@tonic-gate ire_cache_count(ire_t *ire, char *arg)
5263*7c478bd9Sstevel@tonic-gate {
5264*7c478bd9Sstevel@tonic-gate 	ire_cache_count_t *icc = (ire_cache_count_t *)arg;
5265*7c478bd9Sstevel@tonic-gate 
5266*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
5267*7c478bd9Sstevel@tonic-gate 		return;
5268*7c478bd9Sstevel@tonic-gate 
5269*7c478bd9Sstevel@tonic-gate 	icc->icc_total++;
5270*7c478bd9Sstevel@tonic-gate 
5271*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
5272*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
5273*7c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
5274*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
5275*7c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
5276*7c478bd9Sstevel@tonic-gate 			return;
5277*7c478bd9Sstevel@tonic-gate 		}
5278*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
5279*7c478bd9Sstevel@tonic-gate 	} else {
5280*7c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
5281*7c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
5282*7c478bd9Sstevel@tonic-gate 			return;
5283*7c478bd9Sstevel@tonic-gate 		}
5284*7c478bd9Sstevel@tonic-gate 	}
5285*7c478bd9Sstevel@tonic-gate 
5286*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
5287*7c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu)
5288*7c478bd9Sstevel@tonic-gate 		icc->icc_pmtu++;
5289*7c478bd9Sstevel@tonic-gate 	else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
5290*7c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count)
5291*7c478bd9Sstevel@tonic-gate 		icc->icc_offlink++;
5292*7c478bd9Sstevel@tonic-gate 	else
5293*7c478bd9Sstevel@tonic-gate 		icc->icc_unused++;
5294*7c478bd9Sstevel@tonic-gate }
5295*7c478bd9Sstevel@tonic-gate 
5296*7c478bd9Sstevel@tonic-gate /*
5297*7c478bd9Sstevel@tonic-gate  * ire_walk function called by ip_trash_ire_reclaim().
5298*7c478bd9Sstevel@tonic-gate  * Free a fraction of the IRE_CACHE cache entries. The fractions are
5299*7c478bd9Sstevel@tonic-gate  * different for different categories of IRE_CACHE entries.
5300*7c478bd9Sstevel@tonic-gate  * A fraction of zero means to not free any in that category.
5301*7c478bd9Sstevel@tonic-gate  * Use the hash bucket id plus lbolt as a random number. Thus if the fraction
5302*7c478bd9Sstevel@tonic-gate  * is N then every Nth hash bucket chain will be freed.
5303*7c478bd9Sstevel@tonic-gate  */
5304*7c478bd9Sstevel@tonic-gate void
5305*7c478bd9Sstevel@tonic-gate ire_cache_reclaim(ire_t *ire, char *arg)
5306*7c478bd9Sstevel@tonic-gate {
5307*7c478bd9Sstevel@tonic-gate 	ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg;
5308*7c478bd9Sstevel@tonic-gate 	uint_t rand;
5309*7c478bd9Sstevel@tonic-gate 
5310*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
5311*7c478bd9Sstevel@tonic-gate 		return;
5312*7c478bd9Sstevel@tonic-gate 
5313*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
5314*7c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
5315*7c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, ip6_cache_table_size);
5316*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
5317*7c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
5318*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
5319*7c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
5320*7c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
5321*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
5322*7c478bd9Sstevel@tonic-gate 				return;
5323*7c478bd9Sstevel@tonic-gate 			}
5324*7c478bd9Sstevel@tonic-gate 			goto done;
5325*7c478bd9Sstevel@tonic-gate 		}
5326*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
5327*7c478bd9Sstevel@tonic-gate 	} else {
5328*7c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
5329*7c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(ire->ire_addr, ip_cache_table_size);
5330*7c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
5331*7c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
5332*7c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
5333*7c478bd9Sstevel@tonic-gate 				ire_delete(ire);
5334*7c478bd9Sstevel@tonic-gate 				return;
5335*7c478bd9Sstevel@tonic-gate 			}
5336*7c478bd9Sstevel@tonic-gate 			goto done;
5337*7c478bd9Sstevel@tonic-gate 		}
5338*7c478bd9Sstevel@tonic-gate 	}
5339*7c478bd9Sstevel@tonic-gate 	/* Not onlink IRE */
5340*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
5341*7c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) {
5342*7c478bd9Sstevel@tonic-gate 		/* Use ptmu fraction */
5343*7c478bd9Sstevel@tonic-gate 		if (icr->icr_pmtu != 0 &&
5344*7c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) {
5345*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5346*7c478bd9Sstevel@tonic-gate 			return;
5347*7c478bd9Sstevel@tonic-gate 		}
5348*7c478bd9Sstevel@tonic-gate 	} else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
5349*7c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count) {
5350*7c478bd9Sstevel@tonic-gate 		/* Use offlink fraction */
5351*7c478bd9Sstevel@tonic-gate 		if (icr->icr_offlink != 0 &&
5352*7c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_offlink)*icr->icr_offlink == rand) {
5353*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5354*7c478bd9Sstevel@tonic-gate 			return;
5355*7c478bd9Sstevel@tonic-gate 		}
5356*7c478bd9Sstevel@tonic-gate 	} else {
5357*7c478bd9Sstevel@tonic-gate 		/* Use unused fraction */
5358*7c478bd9Sstevel@tonic-gate 		if (icr->icr_unused != 0 &&
5359*7c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_unused)*icr->icr_unused == rand) {
5360*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5361*7c478bd9Sstevel@tonic-gate 			return;
5362*7c478bd9Sstevel@tonic-gate 		}
5363*7c478bd9Sstevel@tonic-gate 	}
5364*7c478bd9Sstevel@tonic-gate done:
5365*7c478bd9Sstevel@tonic-gate 	/*
5366*7c478bd9Sstevel@tonic-gate 	 * Update tire_mark so that those that haven't been used since this
5367*7c478bd9Sstevel@tonic-gate 	 * reclaim will be considered unused next time we reclaim.
5368*7c478bd9Sstevel@tonic-gate 	 */
5369*7c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
5370*7c478bd9Sstevel@tonic-gate }
5371*7c478bd9Sstevel@tonic-gate 
5372*7c478bd9Sstevel@tonic-gate static void
5373*7c478bd9Sstevel@tonic-gate power2_roundup(uint32_t *value)
5374*7c478bd9Sstevel@tonic-gate {
5375*7c478bd9Sstevel@tonic-gate 	int i;
5376*7c478bd9Sstevel@tonic-gate 
5377*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < 31; i++) {
5378*7c478bd9Sstevel@tonic-gate 		if (*value <= (1 << i))
5379*7c478bd9Sstevel@tonic-gate 			break;
5380*7c478bd9Sstevel@tonic-gate 	}
5381*7c478bd9Sstevel@tonic-gate 	*value = (1 << i);
5382*7c478bd9Sstevel@tonic-gate }
5383*7c478bd9Sstevel@tonic-gate 
5384*7c478bd9Sstevel@tonic-gate void
5385*7c478bd9Sstevel@tonic-gate ip_ire_init()
5386*7c478bd9Sstevel@tonic-gate {
5387*7c478bd9Sstevel@tonic-gate 	int i;
5388*7c478bd9Sstevel@tonic-gate 
5389*7c478bd9Sstevel@tonic-gate 	mutex_init(&ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
5390*7c478bd9Sstevel@tonic-gate 	mutex_init(&ire_handle_lock, NULL, MUTEX_DEFAULT, NULL);
5391*7c478bd9Sstevel@tonic-gate 	mutex_init(&ire_mrtun_lock, NULL, MUTEX_DEFAULT, NULL);
5392*7c478bd9Sstevel@tonic-gate 	mutex_init(&ire_srcif_table_lock, NULL, MUTEX_DEFAULT, NULL);
5393*7c478bd9Sstevel@tonic-gate 
5394*7c478bd9Sstevel@tonic-gate 	/* Calculate the IPv4 cache table size. */
5395*7c478bd9Sstevel@tonic-gate 	ip_cache_table_size = MAX(ip_cache_table_size,
5396*7c478bd9Sstevel@tonic-gate 	    ((kmem_avail() >> ip_ire_mem_ratio) / sizeof (ire_t) /
5397*7c478bd9Sstevel@tonic-gate 	    ip_ire_max_bucket_cnt));
5398*7c478bd9Sstevel@tonic-gate 	if (ip_cache_table_size > ip_max_cache_table_size)
5399*7c478bd9Sstevel@tonic-gate 		ip_cache_table_size = ip_max_cache_table_size;
5400*7c478bd9Sstevel@tonic-gate 	/*
5401*7c478bd9Sstevel@tonic-gate 	 * Make sure that the table size is always a power of 2.  The
5402*7c478bd9Sstevel@tonic-gate 	 * hash macro IRE_ADDR_HASH() depends on that.
5403*7c478bd9Sstevel@tonic-gate 	 */
5404*7c478bd9Sstevel@tonic-gate 	power2_roundup(&ip_cache_table_size);
5405*7c478bd9Sstevel@tonic-gate 
5406*7c478bd9Sstevel@tonic-gate 	ip_cache_table = (irb_t *)kmem_zalloc(ip_cache_table_size *
5407*7c478bd9Sstevel@tonic-gate 	    sizeof (irb_t), KM_SLEEP);
5408*7c478bd9Sstevel@tonic-gate 
5409*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size; i++) {
5410*7c478bd9Sstevel@tonic-gate 		rw_init(&ip_cache_table[i].irb_lock, NULL,
5411*7c478bd9Sstevel@tonic-gate 		    RW_DEFAULT, NULL);
5412*7c478bd9Sstevel@tonic-gate 	}
5413*7c478bd9Sstevel@tonic-gate 
5414*7c478bd9Sstevel@tonic-gate 	/* Calculate the IPv6 cache table size. */
5415*7c478bd9Sstevel@tonic-gate 	ip6_cache_table_size = MAX(ip6_cache_table_size,
5416*7c478bd9Sstevel@tonic-gate 	    ((kmem_avail() >> ip_ire_mem_ratio) / sizeof (ire_t) /
5417*7c478bd9Sstevel@tonic-gate 	    ip6_ire_max_bucket_cnt));
5418*7c478bd9Sstevel@tonic-gate 	if (ip6_cache_table_size > ip6_max_cache_table_size)
5419*7c478bd9Sstevel@tonic-gate 		ip6_cache_table_size = ip6_max_cache_table_size;
5420*7c478bd9Sstevel@tonic-gate 	/*
5421*7c478bd9Sstevel@tonic-gate 	 * Make sure that the table size is always a power of 2.  The
5422*7c478bd9Sstevel@tonic-gate 	 * hash macro IRE_ADDR_HASH_V6() depends on that.
5423*7c478bd9Sstevel@tonic-gate 	 */
5424*7c478bd9Sstevel@tonic-gate 	power2_roundup(&ip6_cache_table_size);
5425*7c478bd9Sstevel@tonic-gate 
5426*7c478bd9Sstevel@tonic-gate 	ip_cache_table_v6 = (irb_t *)kmem_zalloc(ip6_cache_table_size *
5427*7c478bd9Sstevel@tonic-gate 	    sizeof (irb_t), KM_SLEEP);
5428*7c478bd9Sstevel@tonic-gate 
5429*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip6_cache_table_size; i++) {
5430*7c478bd9Sstevel@tonic-gate 		rw_init(&ip_cache_table_v6[i].irb_lock, NULL,
5431*7c478bd9Sstevel@tonic-gate 		    RW_DEFAULT, NULL);
5432*7c478bd9Sstevel@tonic-gate 	}
5433*7c478bd9Sstevel@tonic-gate 	/*
5434*7c478bd9Sstevel@tonic-gate 	 * Create ire caches, ire_reclaim()
5435*7c478bd9Sstevel@tonic-gate 	 * will give IRE_CACHE back to system when needed.
5436*7c478bd9Sstevel@tonic-gate 	 * This needs to be done here before anything else, since
5437*7c478bd9Sstevel@tonic-gate 	 * ire_add() expects the cache to be created.
5438*7c478bd9Sstevel@tonic-gate 	 */
5439*7c478bd9Sstevel@tonic-gate 	ire_cache = kmem_cache_create("ire_cache",
5440*7c478bd9Sstevel@tonic-gate 		sizeof (ire_t), 0, ip_ire_constructor,
5441*7c478bd9Sstevel@tonic-gate 		ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0);
5442*7c478bd9Sstevel@tonic-gate 
5443*7c478bd9Sstevel@tonic-gate 	/*
5444*7c478bd9Sstevel@tonic-gate 	 * Initialize ip_mrtun_table to NULL now, it will be
5445*7c478bd9Sstevel@tonic-gate 	 * populated by ip_rt_add if reverse tunnel is created
5446*7c478bd9Sstevel@tonic-gate 	 */
5447*7c478bd9Sstevel@tonic-gate 	ip_mrtun_table = NULL;
5448*7c478bd9Sstevel@tonic-gate 
5449*7c478bd9Sstevel@tonic-gate 	/*
5450*7c478bd9Sstevel@tonic-gate 	 * Make sure that the forwarding table size is a power of 2.
5451*7c478bd9Sstevel@tonic-gate 	 * The IRE*_ADDR_HASH() macroes depend on that.
5452*7c478bd9Sstevel@tonic-gate 	 */
5453*7c478bd9Sstevel@tonic-gate 	power2_roundup(&ip_ftable_hash_size);
5454*7c478bd9Sstevel@tonic-gate 	power2_roundup(&ip6_ftable_hash_size);
5455*7c478bd9Sstevel@tonic-gate }
5456*7c478bd9Sstevel@tonic-gate 
5457*7c478bd9Sstevel@tonic-gate void
5458*7c478bd9Sstevel@tonic-gate ip_ire_fini()
5459*7c478bd9Sstevel@tonic-gate {
5460*7c478bd9Sstevel@tonic-gate 	int i;
5461*7c478bd9Sstevel@tonic-gate 
5462*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire_ft_init_lock);
5463*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire_handle_lock);
5464*7c478bd9Sstevel@tonic-gate 
5465*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size; i++) {
5466*7c478bd9Sstevel@tonic-gate 		rw_destroy(&ip_cache_table[i].irb_lock);
5467*7c478bd9Sstevel@tonic-gate 	}
5468*7c478bd9Sstevel@tonic-gate 	kmem_free(ip_cache_table, ip_cache_table_size * sizeof (irb_t));
5469*7c478bd9Sstevel@tonic-gate 
5470*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip6_cache_table_size; i++) {
5471*7c478bd9Sstevel@tonic-gate 		rw_destroy(&ip_cache_table_v6[i].irb_lock);
5472*7c478bd9Sstevel@tonic-gate 	}
5473*7c478bd9Sstevel@tonic-gate 	kmem_free(ip_cache_table_v6, ip6_cache_table_size * sizeof (irb_t));
5474*7c478bd9Sstevel@tonic-gate 
5475*7c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table != NULL) {
5476*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5477*7c478bd9Sstevel@tonic-gate 			rw_destroy(&ip_mrtun_table[i].irb_lock);
5478*7c478bd9Sstevel@tonic-gate 		}
5479*7c478bd9Sstevel@tonic-gate 		kmem_free(ip_mrtun_table, IP_MRTUN_TABLE_SIZE * sizeof (irb_t));
5480*7c478bd9Sstevel@tonic-gate 	}
5481*7c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(ire_cache);
5482*7c478bd9Sstevel@tonic-gate }
5483*7c478bd9Sstevel@tonic-gate 
5484*7c478bd9Sstevel@tonic-gate int
5485*7c478bd9Sstevel@tonic-gate ire_add_mrtun(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
5486*7c478bd9Sstevel@tonic-gate {
5487*7c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
5488*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
5489*7c478bd9Sstevel@tonic-gate 	ire_t	**irep;
5490*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5491*7c478bd9Sstevel@tonic-gate 	int	i;
5492*7c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
5493*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
5494*7c478bd9Sstevel@tonic-gate 	int error;
5495*7c478bd9Sstevel@tonic-gate 
5496*7c478bd9Sstevel@tonic-gate 	ire = *ire_p;
5497*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5498*7c478bd9Sstevel@tonic-gate 	/* Is ip_mrtun_table empty ? */
5499*7c478bd9Sstevel@tonic-gate 
5500*7c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table == NULL) {
5501*7c478bd9Sstevel@tonic-gate 		/* create the mrtun table */
5502*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_mrtun_lock);
5503*7c478bd9Sstevel@tonic-gate 		if (ip_mrtun_table == NULL) {
5504*7c478bd9Sstevel@tonic-gate 			ip_mrtun_table =
5505*7c478bd9Sstevel@tonic-gate 			    (irb_t *)kmem_zalloc(IP_MRTUN_TABLE_SIZE *
5506*7c478bd9Sstevel@tonic-gate 			    sizeof (irb_t), KM_NOSLEEP);
5507*7c478bd9Sstevel@tonic-gate 
5508*7c478bd9Sstevel@tonic-gate 			if (ip_mrtun_table == NULL) {
5509*7c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_add_mrtun: allocation failure\n"));
5510*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_mrtun_lock);
5511*7c478bd9Sstevel@tonic-gate 				ire_refrele(ire);
5512*7c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
5513*7c478bd9Sstevel@tonic-gate 				return (ENOMEM);
5514*7c478bd9Sstevel@tonic-gate 			}
5515*7c478bd9Sstevel@tonic-gate 
5516*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5517*7c478bd9Sstevel@tonic-gate 			    rw_init(&ip_mrtun_table[i].irb_lock, NULL,
5518*7c478bd9Sstevel@tonic-gate 				    RW_DEFAULT, NULL);
5519*7c478bd9Sstevel@tonic-gate 			}
5520*7c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_add_mrtun: mrtun table is created\n"));
5521*7c478bd9Sstevel@tonic-gate 		}
5522*7c478bd9Sstevel@tonic-gate 		/* some other thread got it and created the table */
5523*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_mrtun_lock);
5524*7c478bd9Sstevel@tonic-gate 	}
5525*7c478bd9Sstevel@tonic-gate 
5526*7c478bd9Sstevel@tonic-gate 	/*
5527*7c478bd9Sstevel@tonic-gate 	 * Check for duplicate in the bucket and insert in the table
5528*7c478bd9Sstevel@tonic-gate 	 */
5529*7c478bd9Sstevel@tonic-gate 	irb_ptr = &(ip_mrtun_table[IRE_ADDR_HASH(ire->ire_in_src_addr,
5530*7c478bd9Sstevel@tonic-gate 	    IP_MRTUN_TABLE_SIZE)]);
5531*7c478bd9Sstevel@tonic-gate 
5532*7c478bd9Sstevel@tonic-gate 	/*
5533*7c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
5534*7c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
5535*7c478bd9Sstevel@tonic-gate 	 *
5536*7c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
5537*7c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
5538*7c478bd9Sstevel@tonic-gate 	 */
5539*7c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
5540*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
5541*7c478bd9Sstevel@tonic-gate 		/*
5542*7c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
5543*7c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
5544*7c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
5545*7c478bd9Sstevel@tonic-gate 		 */
5546*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
5547*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
5548*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_mrtun: ire_atomic_start failed\n"));
5549*7c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
5550*7c478bd9Sstevel@tonic-gate 		return (error);
5551*7c478bd9Sstevel@tonic-gate 	}
5552*7c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
5553*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
5554*7c478bd9Sstevel@tonic-gate 			continue;
5555*7c478bd9Sstevel@tonic-gate 		/* has anyone inserted the route in the meanwhile ? */
5556*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_in_ill == ire->ire_in_ill &&
5557*7c478bd9Sstevel@tonic-gate 		    ire1->ire_in_src_addr == ire->ire_in_src_addr) {
5558*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_mrtun: Duplicate entry exists\n"));
5559*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
5560*7c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
5561*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5562*7c478bd9Sstevel@tonic-gate 			/* Return the old ire */
5563*7c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
5564*7c478bd9Sstevel@tonic-gate 			return (0);
5565*7c478bd9Sstevel@tonic-gate 		}
5566*7c478bd9Sstevel@tonic-gate 	}
5567*7c478bd9Sstevel@tonic-gate 
5568*7c478bd9Sstevel@tonic-gate 	/* Atomically set the ire_max_frag */
5569*7c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
5570*7c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
5571*7c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
5572*7c478bd9Sstevel@tonic-gate 
5573*7c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
5574*7c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
5575*7c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_in_src_addr */
5576*7c478bd9Sstevel@tonic-gate 		ire1 = *irep;
5577*7c478bd9Sstevel@tonic-gate 		while (ire1->ire_in_src_addr == ire->ire_in_src_addr) {
5578*7c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
5579*7c478bd9Sstevel@tonic-gate 			ire1 = *irep;
5580*7c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
5581*7c478bd9Sstevel@tonic-gate 				break;
5582*7c478bd9Sstevel@tonic-gate 		}
5583*7c478bd9Sstevel@tonic-gate 	}
5584*7c478bd9Sstevel@tonic-gate 	ire1 = *irep;
5585*7c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
5586*7c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
5587*7c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
5588*7c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
5589*7c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
5590*7c478bd9Sstevel@tonic-gate 	membar_producer();
5591*7c478bd9Sstevel@tonic-gate 	*irep = ire;
5592*7c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
5593*7c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
5594*7c478bd9Sstevel@tonic-gate 
5595*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_add_mrtun: created and linked ire %p\n", (void *)*irep));
5596*7c478bd9Sstevel@tonic-gate 
5597*7c478bd9Sstevel@tonic-gate 	/*
5598*7c478bd9Sstevel@tonic-gate 	 * Protect ire_mrtun_count and ill_mrtun_refcnt from
5599*7c478bd9Sstevel@tonic-gate 	 * another thread trying to add ire in the table
5600*7c478bd9Sstevel@tonic-gate 	 */
5601*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_mrtun_lock);
5602*7c478bd9Sstevel@tonic-gate 	ire_mrtun_count++;
5603*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_mrtun_lock);
5604*7c478bd9Sstevel@tonic-gate 	/*
5605*7c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is protected by the ill_lock held via
5606*7c478bd9Sstevel@tonic-gate 	 * ire_atomic_start
5607*7c478bd9Sstevel@tonic-gate 	 */
5608*7c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_mrtun_refcnt++;
5609*7c478bd9Sstevel@tonic-gate 
5610*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
5611*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
5612*7c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
5613*7c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
5614*7c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
5615*7c478bd9Sstevel@tonic-gate 		}
5616*7c478bd9Sstevel@tonic-gate 	} else {
5617*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
5618*7c478bd9Sstevel@tonic-gate 	}
5619*7c478bd9Sstevel@tonic-gate 
5620*7c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
5621*7c478bd9Sstevel@tonic-gate 	ire_fastpath(ire);
5622*7c478bd9Sstevel@tonic-gate 	*ire_p = ire;
5623*7c478bd9Sstevel@tonic-gate 	return (0);
5624*7c478bd9Sstevel@tonic-gate }
5625*7c478bd9Sstevel@tonic-gate 
5626*7c478bd9Sstevel@tonic-gate 
5627*7c478bd9Sstevel@tonic-gate /* Walks down the mrtun table */
5628*7c478bd9Sstevel@tonic-gate 
5629*7c478bd9Sstevel@tonic-gate void
5630*7c478bd9Sstevel@tonic-gate ire_walk_ill_mrtun(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
5631*7c478bd9Sstevel@tonic-gate     ill_t *ill)
5632*7c478bd9Sstevel@tonic-gate {
5633*7c478bd9Sstevel@tonic-gate 	irb_t	*irb;
5634*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5635*7c478bd9Sstevel@tonic-gate 	int	i;
5636*7c478bd9Sstevel@tonic-gate 	int	ret;
5637*7c478bd9Sstevel@tonic-gate 
5638*7c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
5639*7c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
5640*7c478bd9Sstevel@tonic-gate 	ASSERT(match_flags == 0 || ire_type == IRE_MIPRTUN);
5641*7c478bd9Sstevel@tonic-gate 
5642*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_mrtun_lock);
5643*7c478bd9Sstevel@tonic-gate 	if (ire_mrtun_count == 0) {
5644*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_mrtun_lock);
5645*7c478bd9Sstevel@tonic-gate 		return;
5646*7c478bd9Sstevel@tonic-gate 	}
5647*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_mrtun_lock);
5648*7c478bd9Sstevel@tonic-gate 
5649*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_walk_ill_mrtun:walking the reverse tunnel table \n"));
5650*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5651*7c478bd9Sstevel@tonic-gate 
5652*7c478bd9Sstevel@tonic-gate 		irb = &(ip_mrtun_table[i]);
5653*7c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
5654*7c478bd9Sstevel@tonic-gate 			continue;
5655*7c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb);
5656*7c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL;
5657*7c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
5658*7c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV4_VERSION);
5659*7c478bd9Sstevel@tonic-gate 			if (match_flags != 0) {
5660*7c478bd9Sstevel@tonic-gate 				ret = ire_walk_ill_match(
5661*7c478bd9Sstevel@tonic-gate 				    match_flags, ire_type,
5662*7c478bd9Sstevel@tonic-gate 				    ire, ill, ALL_ZONES);
5663*7c478bd9Sstevel@tonic-gate 			}
5664*7c478bd9Sstevel@tonic-gate 			if (match_flags == 0 || ret)
5665*7c478bd9Sstevel@tonic-gate 				(*func)(ire, arg);
5666*7c478bd9Sstevel@tonic-gate 		}
5667*7c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb);
5668*7c478bd9Sstevel@tonic-gate 	}
5669*7c478bd9Sstevel@tonic-gate }
5670*7c478bd9Sstevel@tonic-gate 
5671*7c478bd9Sstevel@tonic-gate /*
5672*7c478bd9Sstevel@tonic-gate  * Source interface based lookup routine (IPV4 only).
5673*7c478bd9Sstevel@tonic-gate  * This routine is called only when RTA_SRCIFP bitflag is set
5674*7c478bd9Sstevel@tonic-gate  * by routing socket while adding/deleting the route and it is
5675*7c478bd9Sstevel@tonic-gate  * also called from ip_rput() when packets arrive from an interface
5676*7c478bd9Sstevel@tonic-gate  * for which ill_srcif_ref_cnt is positive. This function is useful
5677*7c478bd9Sstevel@tonic-gate  * when a packet coming from one interface must be forwarded to another
5678*7c478bd9Sstevel@tonic-gate  * designated interface to reach the correct node. This function is also
5679*7c478bd9Sstevel@tonic-gate  * called from ip_newroute when the link-layer address of an ire is resolved.
5680*7c478bd9Sstevel@tonic-gate  * We need to make sure that ip_newroute searches for IRE_IF_RESOLVER type
5681*7c478bd9Sstevel@tonic-gate  * ires--thus the ire_type parameter is needed.
5682*7c478bd9Sstevel@tonic-gate  */
5683*7c478bd9Sstevel@tonic-gate 
5684*7c478bd9Sstevel@tonic-gate ire_t *
5685*7c478bd9Sstevel@tonic-gate ire_srcif_table_lookup(ipaddr_t dst_addr, int ire_type, ipif_t *ipif,
5686*7c478bd9Sstevel@tonic-gate     ill_t *in_ill, int flags)
5687*7c478bd9Sstevel@tonic-gate {
5688*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
5689*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
5690*7c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcif_table;
5691*7c478bd9Sstevel@tonic-gate 
5692*7c478bd9Sstevel@tonic-gate 	ASSERT(in_ill != NULL && !in_ill->ill_isv6);
5693*7c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) ||
5694*7c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
5695*7c478bd9Sstevel@tonic-gate 
5696*7c478bd9Sstevel@tonic-gate 	/*
5697*7c478bd9Sstevel@tonic-gate 	 * No need to lock the ill since it is refheld by the caller of this
5698*7c478bd9Sstevel@tonic-gate 	 * function
5699*7c478bd9Sstevel@tonic-gate 	 */
5700*7c478bd9Sstevel@tonic-gate 	if (in_ill->ill_srcif_table == NULL) {
5701*7c478bd9Sstevel@tonic-gate 		return (NULL);
5702*7c478bd9Sstevel@tonic-gate 	}
5703*7c478bd9Sstevel@tonic-gate 
5704*7c478bd9Sstevel@tonic-gate 	if (!(flags & MATCH_IRE_TYPE)) {
5705*7c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_TYPE;
5706*7c478bd9Sstevel@tonic-gate 		ire_type = IRE_INTERFACE;
5707*7c478bd9Sstevel@tonic-gate 	}
5708*7c478bd9Sstevel@tonic-gate 	ire_srcif_table = in_ill->ill_srcif_table;
5709*7c478bd9Sstevel@tonic-gate 	irb_ptr = &ire_srcif_table[IRE_ADDR_HASH(dst_addr,
5710*7c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)];
5711*7c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
5712*7c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
5713*7c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
5714*7c478bd9Sstevel@tonic-gate 			continue;
5715*7c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, dst_addr, ire->ire_mask, 0,
5716*7c478bd9Sstevel@tonic-gate 		    ire_type, ipif, ire->ire_zoneid, 0, flags)) {
5717*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
5718*7c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
5719*7c478bd9Sstevel@tonic-gate 			return (ire);
5720*7c478bd9Sstevel@tonic-gate 		}
5721*7c478bd9Sstevel@tonic-gate 	}
5722*7c478bd9Sstevel@tonic-gate 	/* Not Found */
5723*7c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
5724*7c478bd9Sstevel@tonic-gate 	return (NULL);
5725*7c478bd9Sstevel@tonic-gate }
5726*7c478bd9Sstevel@tonic-gate 
5727*7c478bd9Sstevel@tonic-gate 
5728*7c478bd9Sstevel@tonic-gate /*
5729*7c478bd9Sstevel@tonic-gate  * Adds the ire into the special routing table which is hanging off of
5730*7c478bd9Sstevel@tonic-gate  * the src_ipif->ipif_ill. It also increments the refcnt in the ill.
5731*7c478bd9Sstevel@tonic-gate  * The forward table contains only IRE_IF_RESOLVER, IRE_IF_NORESOLVER
5732*7c478bd9Sstevel@tonic-gate  * i,e. IRE_INTERFACE entries. Originally the dlureq_mp field is NULL
5733*7c478bd9Sstevel@tonic-gate  * for IRE_IF_RESOLVER entry because we do not have the dst_addr's
5734*7c478bd9Sstevel@tonic-gate  * link-layer address at the time of addition.
5735*7c478bd9Sstevel@tonic-gate  * Upon resolving the address from ARP, dlureq_mp field is updated with
5736*7c478bd9Sstevel@tonic-gate  * proper information in ire_update_srcif_v4.
5737*7c478bd9Sstevel@tonic-gate  */
5738*7c478bd9Sstevel@tonic-gate static int
5739*7c478bd9Sstevel@tonic-gate ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
5740*7c478bd9Sstevel@tonic-gate {
5741*7c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
5742*7c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcifp_table = NULL;
5743*7c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr = NULL;
5744*7c478bd9Sstevel@tonic-gate 	ire_t   **irep;
5745*7c478bd9Sstevel@tonic-gate 	ire_t   *ire;
5746*7c478bd9Sstevel@tonic-gate 	int	flags;
5747*7c478bd9Sstevel@tonic-gate 	int	i;
5748*7c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
5749*7c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
5750*7c478bd9Sstevel@tonic-gate 	int error = 0;
5751*7c478bd9Sstevel@tonic-gate 
5752*7c478bd9Sstevel@tonic-gate 	ire = *ire_p;
5753*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill != NULL);
5754*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5755*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_IF_NORESOLVER ||
5756*7c478bd9Sstevel@tonic-gate 	    ire->ire_type == IRE_IF_RESOLVER);
5757*7c478bd9Sstevel@tonic-gate 
5758*7c478bd9Sstevel@tonic-gate 	ire->ire_mask = IP_HOST_MASK;
5759*7c478bd9Sstevel@tonic-gate 	/* Update ire_dlureq_mp with NULL value upon creation */
5760*7c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_IF_RESOLVER) {
5761*7c478bd9Sstevel@tonic-gate 		/*
5762*7c478bd9Sstevel@tonic-gate 		 * assign NULL now, it will be updated
5763*7c478bd9Sstevel@tonic-gate 		 * with correct value upon returning from
5764*7c478bd9Sstevel@tonic-gate 		 * ARP
5765*7c478bd9Sstevel@tonic-gate 		 */
5766*7c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = NULL;
5767*7c478bd9Sstevel@tonic-gate 	} else {
5768*7c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = ill_dlur_gen(NULL,
5769*7c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_phys_addr_length,
5770*7c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap,
5771*7c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap_length);
5772*7c478bd9Sstevel@tonic-gate 	}
5773*7c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
5774*7c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
5775*7c478bd9Sstevel@tonic-gate 
5776*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp != NULL);
5777*7c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
5778*7c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
5779*7c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
5780*7c478bd9Sstevel@tonic-gate 
5781*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_in_ill->ill_lock);
5782*7c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill->ill_srcif_table == NULL) {
5783*7c478bd9Sstevel@tonic-gate 		/* create the incoming interface based table */
5784*7c478bd9Sstevel@tonic-gate 		ire->ire_in_ill->ill_srcif_table =
5785*7c478bd9Sstevel@tonic-gate 		    (irb_t *)kmem_zalloc(IP_SRCIF_TABLE_SIZE *
5786*7c478bd9Sstevel@tonic-gate 			sizeof (irb_t), KM_NOSLEEP);
5787*7c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill->ill_srcif_table == NULL) {
5788*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4: Allocation fail\n"));
5789*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_in_ill->ill_lock);
5790*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5791*7c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
5792*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
5793*7c478bd9Sstevel@tonic-gate 		}
5794*7c478bd9Sstevel@tonic-gate 		ire_srcifp_table = ire->ire_in_ill->ill_srcif_table;
5795*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
5796*7c478bd9Sstevel@tonic-gate 			rw_init(&ire_srcifp_table[i].irb_lock, NULL,
5797*7c478bd9Sstevel@tonic-gate 			    RW_DEFAULT, NULL);
5798*7c478bd9Sstevel@tonic-gate 		}
5799*7c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_add_srcif_v4: table created for ill %p\n",
5800*7c478bd9Sstevel@tonic-gate 		    (void *)ire->ire_in_ill));
5801*7c478bd9Sstevel@tonic-gate 	}
5802*7c478bd9Sstevel@tonic-gate 	/* Check for duplicate and insert */
5803*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill->ill_srcif_table != NULL);
5804*7c478bd9Sstevel@tonic-gate 	irb_ptr =
5805*7c478bd9Sstevel@tonic-gate 	    &(ire->ire_in_ill->ill_srcif_table[IRE_ADDR_HASH(ire->ire_addr,
5806*7c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)]);
5807*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_in_ill->ill_lock);
5808*7c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
5809*7c478bd9Sstevel@tonic-gate 	flags |= MATCH_IRE_IPIF;
5810*7c478bd9Sstevel@tonic-gate 
5811*7c478bd9Sstevel@tonic-gate 	/*
5812*7c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
5813*7c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
5814*7c478bd9Sstevel@tonic-gate 	 *
5815*7c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
5816*7c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
5817*7c478bd9Sstevel@tonic-gate 	 */
5818*7c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
5819*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
5820*7c478bd9Sstevel@tonic-gate 		/*
5821*7c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
5822*7c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
5823*7c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
5824*7c478bd9Sstevel@tonic-gate 		 */
5825*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
5826*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
5827*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_srcif_v4: ire_atomic_start failed\n"));
5828*7c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
5829*7c478bd9Sstevel@tonic-gate 		return (error);
5830*7c478bd9Sstevel@tonic-gate 	}
5831*7c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
5832*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
5833*7c478bd9Sstevel@tonic-gate 			continue;
5834*7c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
5835*7c478bd9Sstevel@tonic-gate 			continue;
5836*7c478bd9Sstevel@tonic-gate 		/* Has anyone inserted route in the meanwhile ? */
5837*7c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 0,
5838*7c478bd9Sstevel@tonic-gate 		    ire->ire_type, ire->ire_ipif, ire->ire_zoneid, 0, flags)) {
5839*7c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4 : Duplicate entry exists\n"));
5840*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
5841*7c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
5842*7c478bd9Sstevel@tonic-gate 			ire_delete(ire);
5843*7c478bd9Sstevel@tonic-gate 			/* Return old ire as in ire_add_v4 */
5844*7c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
5845*7c478bd9Sstevel@tonic-gate 			return (0);
5846*7c478bd9Sstevel@tonic-gate 		}
5847*7c478bd9Sstevel@tonic-gate 	}
5848*7c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
5849*7c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
5850*7c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_addr */
5851*7c478bd9Sstevel@tonic-gate 		ire1 = *irep;
5852*7c478bd9Sstevel@tonic-gate 		while (ire1->ire_addr == ire->ire_addr) {
5853*7c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
5854*7c478bd9Sstevel@tonic-gate 			ire1 = *irep;
5855*7c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
5856*7c478bd9Sstevel@tonic-gate 				break;
5857*7c478bd9Sstevel@tonic-gate 		}
5858*7c478bd9Sstevel@tonic-gate 	}
5859*7c478bd9Sstevel@tonic-gate 	ire1 = *irep;
5860*7c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
5861*7c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
5862*7c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
5863*7c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
5864*7c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
5865*7c478bd9Sstevel@tonic-gate 	membar_producer();
5866*7c478bd9Sstevel@tonic-gate 	*irep = ire;
5867*7c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
5868*7c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
5869*7c478bd9Sstevel@tonic-gate 
5870*7c478bd9Sstevel@tonic-gate 	/*
5871*7c478bd9Sstevel@tonic-gate 	 * Protect ire_in_ill->ill_srcif_refcnt and table reference count.
5872*7c478bd9Sstevel@tonic-gate 	 * Note, ire_atomic_start already grabs the ire_in_ill->ill_lock
5873*7c478bd9Sstevel@tonic-gate 	 * so ill_srcif_refcnt is already protected.
5874*7c478bd9Sstevel@tonic-gate 	 */
5875*7c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_srcif_refcnt++;
5876*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
5877*7c478bd9Sstevel@tonic-gate 	ire_srcif_table_count++;
5878*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
5879*7c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
5880*7c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
5881*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
5882*7c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
5883*7c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
5884*7c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
5885*7c478bd9Sstevel@tonic-gate 		}
5886*7c478bd9Sstevel@tonic-gate 	} else {
5887*7c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
5888*7c478bd9Sstevel@tonic-gate 	}
5889*7c478bd9Sstevel@tonic-gate 
5890*7c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
5891*7c478bd9Sstevel@tonic-gate 	*ire_p = ire;
5892*7c478bd9Sstevel@tonic-gate 	return (0);
5893*7c478bd9Sstevel@tonic-gate }
5894*7c478bd9Sstevel@tonic-gate 
5895*7c478bd9Sstevel@tonic-gate 
5896*7c478bd9Sstevel@tonic-gate /*
5897*7c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send when ARP request comes
5898*7c478bd9Sstevel@tonic-gate  * back to ip_wput->ire_add_then_send for resolved ire in the interface
5899*7c478bd9Sstevel@tonic-gate  * based routing table. At this point, it only needs to update the resolver
5900*7c478bd9Sstevel@tonic-gate  * information for the ire. The passed ire is returned to the caller as it
5901*7c478bd9Sstevel@tonic-gate  * is the ire which is created as mblk.
5902*7c478bd9Sstevel@tonic-gate  */
5903*7c478bd9Sstevel@tonic-gate 
5904*7c478bd9Sstevel@tonic-gate static ire_t *
5905*7c478bd9Sstevel@tonic-gate ire_update_srcif_v4(ire_t *ire)
5906*7c478bd9Sstevel@tonic-gate {
5907*7c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
5908*7c478bd9Sstevel@tonic-gate 	irb_t	*irb;
5909*7c478bd9Sstevel@tonic-gate 	int	error;
5910*7c478bd9Sstevel@tonic-gate 
5911*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN &&
5912*7c478bd9Sstevel@tonic-gate 	    ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER);
5913*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5914*7c478bd9Sstevel@tonic-gate 
5915*7c478bd9Sstevel@tonic-gate 	/*
5916*7c478bd9Sstevel@tonic-gate 	 * This ire is from ARP. Update
5917*7c478bd9Sstevel@tonic-gate 	 * ire_dlureq_mp info
5918*7c478bd9Sstevel@tonic-gate 	 */
5919*7c478bd9Sstevel@tonic-gate 	ire1 = ire_srcif_table_lookup(ire->ire_addr,
5920*7c478bd9Sstevel@tonic-gate 	    IRE_IF_RESOLVER, ire->ire_ipif,
5921*7c478bd9Sstevel@tonic-gate 	    ire->ire_in_ill,
5922*7c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL | MATCH_IRE_TYPE);
5923*7c478bd9Sstevel@tonic-gate 	if (ire1 == NULL) {
5924*7c478bd9Sstevel@tonic-gate 		/* Mobile node registration expired ? */
5925*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
5926*7c478bd9Sstevel@tonic-gate 		return (NULL);
5927*7c478bd9Sstevel@tonic-gate 	}
5928*7c478bd9Sstevel@tonic-gate 	irb = ire1->ire_bucket;
5929*7c478bd9Sstevel@tonic-gate 	ASSERT(irb != NULL);
5930*7c478bd9Sstevel@tonic-gate 	/*
5931*7c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
5932*7c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
5933*7c478bd9Sstevel@tonic-gate 	 */
5934*7c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb, ire1, NULL, NULL, NULL);
5935*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
5936*7c478bd9Sstevel@tonic-gate 		/*
5937*7c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
5938*7c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
5939*7c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
5940*7c478bd9Sstevel@tonic-gate 		 */
5941*7c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
5942*7c478bd9Sstevel@tonic-gate 		ire_delete(ire);
5943*7c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_update_srcif_v4: ire_atomic_start failed\n"));
5944*7c478bd9Sstevel@tonic-gate 		return (NULL);
5945*7c478bd9Sstevel@tonic-gate 	}
5946*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp == NULL);
5947*7c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = ire1->ire_max_frag;
5948*7c478bd9Sstevel@tonic-gate 	/*
5949*7c478bd9Sstevel@tonic-gate 	 * Update resolver information and
5950*7c478bd9Sstevel@tonic-gate 	 * send-to queue.
5951*7c478bd9Sstevel@tonic-gate 	 */
5952*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_dlureq_mp != NULL);
5953*7c478bd9Sstevel@tonic-gate 	ire1->ire_dlureq_mp = copyb(ire->ire_dlureq_mp);
5954*7c478bd9Sstevel@tonic-gate 	if (ire1->ire_dlureq_mp ==  NULL) {
5955*7c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_update_srcif: copyb failed\n"));
5956*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire1);
5957*7c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
5958*7c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb, ire1);
5959*7c478bd9Sstevel@tonic-gate 		return (NULL);
5960*7c478bd9Sstevel@tonic-gate 	}
5961*7c478bd9Sstevel@tonic-gate 	ire1->ire_stq = ire->ire_stq;
5962*7c478bd9Sstevel@tonic-gate 
5963*7c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fp_mp == NULL);
5964*7c478bd9Sstevel@tonic-gate 
5965*7c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb, ire1);
5966*7c478bd9Sstevel@tonic-gate 	ire_refrele(ire1);
5967*7c478bd9Sstevel@tonic-gate 	/* Return the passed ire */
5968*7c478bd9Sstevel@tonic-gate 	return (ire);   /* Update done */
5969*7c478bd9Sstevel@tonic-gate }
5970*7c478bd9Sstevel@tonic-gate 
5971*7c478bd9Sstevel@tonic-gate 
5972*7c478bd9Sstevel@tonic-gate /*
5973*7c478bd9Sstevel@tonic-gate  * Check if another multirt route resolution is needed.
5974*7c478bd9Sstevel@tonic-gate  * B_TRUE is returned is there remain a resolvable route,
5975*7c478bd9Sstevel@tonic-gate  * or if no route for that dst is resolved yet.
5976*7c478bd9Sstevel@tonic-gate  * B_FALSE is returned if all routes for that dst are resolved
5977*7c478bd9Sstevel@tonic-gate  * or if the remaining unresolved routes are actually not
5978*7c478bd9Sstevel@tonic-gate  * resolvable.
5979*7c478bd9Sstevel@tonic-gate  * This only works in the global zone.
5980*7c478bd9Sstevel@tonic-gate  */
5981*7c478bd9Sstevel@tonic-gate boolean_t
5982*7c478bd9Sstevel@tonic-gate ire_multirt_need_resolve(ipaddr_t dst)
5983*7c478bd9Sstevel@tonic-gate {
5984*7c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
5985*7c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
5986*7c478bd9Sstevel@tonic-gate 	ire_t	*fire;
5987*7c478bd9Sstevel@tonic-gate 	ire_t	*cire;
5988*7c478bd9Sstevel@tonic-gate 	irb_t	*firb;
5989*7c478bd9Sstevel@tonic-gate 	irb_t	*cirb;
5990*7c478bd9Sstevel@tonic-gate 	int	unres_cnt = 0;
5991*7c478bd9Sstevel@tonic-gate 	boolean_t resolvable = B_FALSE;
5992*7c478bd9Sstevel@tonic-gate 
5993*7c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_HOST that matches the destination */
5994*7c478bd9Sstevel@tonic-gate 	first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL,
5995*7c478bd9Sstevel@tonic-gate 	    NULL, ALL_ZONES, 0, MATCH_IRE_MASK | MATCH_IRE_TYPE);
5996*7c478bd9Sstevel@tonic-gate 
5997*7c478bd9Sstevel@tonic-gate 	/* No route at all */
5998*7c478bd9Sstevel@tonic-gate 	if (first_fire == NULL) {
5999*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6000*7c478bd9Sstevel@tonic-gate 	}
6001*7c478bd9Sstevel@tonic-gate 
6002*7c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
6003*7c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
6004*7c478bd9Sstevel@tonic-gate 
6005*7c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_CACHE ire for that destination. */
6006*7c478bd9Sstevel@tonic-gate 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID);
6007*7c478bd9Sstevel@tonic-gate 
6008*7c478bd9Sstevel@tonic-gate 	/* No resolved route. */
6009*7c478bd9Sstevel@tonic-gate 	if (first_cire == NULL) {
6010*7c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
6011*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6012*7c478bd9Sstevel@tonic-gate 	}
6013*7c478bd9Sstevel@tonic-gate 
6014*7c478bd9Sstevel@tonic-gate 	/*
6015*7c478bd9Sstevel@tonic-gate 	 * At least one route is resolved. Here we look through the forward
6016*7c478bd9Sstevel@tonic-gate 	 * and cache tables, to compare the number of declared routes
6017*7c478bd9Sstevel@tonic-gate 	 * with the number of resolved routes. The search for a resolvable
6018*7c478bd9Sstevel@tonic-gate 	 * route is performed only if at least one route remains
6019*7c478bd9Sstevel@tonic-gate 	 * unresolved.
6020*7c478bd9Sstevel@tonic-gate 	 */
6021*7c478bd9Sstevel@tonic-gate 	cirb = first_cire->ire_bucket;
6022*7c478bd9Sstevel@tonic-gate 	ASSERT(cirb != NULL);
6023*7c478bd9Sstevel@tonic-gate 
6024*7c478bd9Sstevel@tonic-gate 	/* Count the number of routes to that dest that are declared. */
6025*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
6026*7c478bd9Sstevel@tonic-gate 	for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
6027*7c478bd9Sstevel@tonic-gate 		if (!(fire->ire_flags & RTF_MULTIRT))
6028*7c478bd9Sstevel@tonic-gate 			continue;
6029*7c478bd9Sstevel@tonic-gate 		if (fire->ire_addr != dst)
6030*7c478bd9Sstevel@tonic-gate 			continue;
6031*7c478bd9Sstevel@tonic-gate 		unres_cnt++;
6032*7c478bd9Sstevel@tonic-gate 	}
6033*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
6034*7c478bd9Sstevel@tonic-gate 
6035*7c478bd9Sstevel@tonic-gate 	/* Then subtract the number of routes to that dst that are resolved */
6036*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(cirb);
6037*7c478bd9Sstevel@tonic-gate 	for (cire = first_cire; cire != NULL; cire = cire->ire_next) {
6038*7c478bd9Sstevel@tonic-gate 		if (!(cire->ire_flags & RTF_MULTIRT))
6039*7c478bd9Sstevel@tonic-gate 			continue;
6040*7c478bd9Sstevel@tonic-gate 		if (cire->ire_addr != dst)
6041*7c478bd9Sstevel@tonic-gate 			continue;
6042*7c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
6043*7c478bd9Sstevel@tonic-gate 			continue;
6044*7c478bd9Sstevel@tonic-gate 		unres_cnt--;
6045*7c478bd9Sstevel@tonic-gate 	}
6046*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(cirb);
6047*7c478bd9Sstevel@tonic-gate 
6048*7c478bd9Sstevel@tonic-gate 	/* At least one route is unresolved; search for a resolvable route. */
6049*7c478bd9Sstevel@tonic-gate 	if (unres_cnt > 0)
6050*7c478bd9Sstevel@tonic-gate 		resolvable = ire_multirt_lookup(&first_cire, &first_fire,
6051*7c478bd9Sstevel@tonic-gate 		    MULTIRT_USESTAMP | MULTIRT_CACHEGW);
6052*7c478bd9Sstevel@tonic-gate 
6053*7c478bd9Sstevel@tonic-gate 	if (first_fire != NULL)
6054*7c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
6055*7c478bd9Sstevel@tonic-gate 
6056*7c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
6057*7c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
6058*7c478bd9Sstevel@tonic-gate 
6059*7c478bd9Sstevel@tonic-gate 	return (resolvable);
6060*7c478bd9Sstevel@tonic-gate }
6061*7c478bd9Sstevel@tonic-gate 
6062*7c478bd9Sstevel@tonic-gate 
6063*7c478bd9Sstevel@tonic-gate /*
6064*7c478bd9Sstevel@tonic-gate  * Explore a forward_table bucket, starting from fire_arg.
6065*7c478bd9Sstevel@tonic-gate  * fire_arg MUST be an IRE_HOST entry.
6066*7c478bd9Sstevel@tonic-gate  *
6067*7c478bd9Sstevel@tonic-gate  * Return B_TRUE and update *ire_arg and *fire_arg
6068*7c478bd9Sstevel@tonic-gate  * if at least one resolvable route is found. *ire_arg
6069*7c478bd9Sstevel@tonic-gate  * is the IRE entry for *fire_arg's gateway.
6070*7c478bd9Sstevel@tonic-gate  *
6071*7c478bd9Sstevel@tonic-gate  * Return B_FALSE otherwise (all routes are resolved or
6072*7c478bd9Sstevel@tonic-gate  * the remaining unresolved routes are all unresolvable).
6073*7c478bd9Sstevel@tonic-gate  *
6074*7c478bd9Sstevel@tonic-gate  * The IRE selection relies on a priority mechanism
6075*7c478bd9Sstevel@tonic-gate  * driven by the flags passed in by the caller.
6076*7c478bd9Sstevel@tonic-gate  * The caller, such as ip_newroute_ipif(), can get the most
6077*7c478bd9Sstevel@tonic-gate  * relevant ire at each stage of a multiple route resolution.
6078*7c478bd9Sstevel@tonic-gate  *
6079*7c478bd9Sstevel@tonic-gate  * The rules are:
6080*7c478bd9Sstevel@tonic-gate  *
6081*7c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE
6082*7c478bd9Sstevel@tonic-gate  *   ires are preferred for the gateway. This gives the highest
6083*7c478bd9Sstevel@tonic-gate  *   priority to routes that can be resolved without using
6084*7c478bd9Sstevel@tonic-gate  *   a resolver.
6085*7c478bd9Sstevel@tonic-gate  *
6086*7c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW
6087*7c478bd9Sstevel@tonic-gate  *   is specified but no IRE_CACHETABLE ire entry for the gateway
6088*7c478bd9Sstevel@tonic-gate  *   is found, the following rules apply.
6089*7c478bd9Sstevel@tonic-gate  *
6090*7c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE
6091*7c478bd9Sstevel@tonic-gate  *   ires for the gateway, that have not been tried since
6092*7c478bd9Sstevel@tonic-gate  *   a configurable amount of time, are preferred.
6093*7c478bd9Sstevel@tonic-gate  *   This applies when a resolver must be invoked for
6094*7c478bd9Sstevel@tonic-gate  *   a missing route, but we don't want to use the resolver
6095*7c478bd9Sstevel@tonic-gate  *   upon each packet emission. If no such resolver is found,
6096*7c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
6097*7c478bd9Sstevel@tonic-gate  *   The MULTIRT_USESTAMP flag can be combined with
6098*7c478bd9Sstevel@tonic-gate  *   MULTIRT_CACHEGW.
6099*7c478bd9Sstevel@tonic-gate  *
6100*7c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is not specified in flags, the first
6101*7c478bd9Sstevel@tonic-gate  *   unresolved but resolvable route is selected.
6102*7c478bd9Sstevel@tonic-gate  *
6103*7c478bd9Sstevel@tonic-gate  * - Otherwise, there is no resolvalble route, and
6104*7c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
6105*7c478bd9Sstevel@tonic-gate  *
6106*7c478bd9Sstevel@tonic-gate  * At last, MULTIRT_SETSTAMP can be specified in flags to
6107*7c478bd9Sstevel@tonic-gate  * request the timestamp of unresolvable routes to
6108*7c478bd9Sstevel@tonic-gate  * be refreshed. This prevents the useless exploration
6109*7c478bd9Sstevel@tonic-gate  * of those routes for a while, when MULTIRT_USESTAMP is used.
6110*7c478bd9Sstevel@tonic-gate  *
6111*7c478bd9Sstevel@tonic-gate  * This only works in the global zone.
6112*7c478bd9Sstevel@tonic-gate  */
6113*7c478bd9Sstevel@tonic-gate boolean_t
6114*7c478bd9Sstevel@tonic-gate ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags)
6115*7c478bd9Sstevel@tonic-gate {
6116*7c478bd9Sstevel@tonic-gate 	clock_t	delta;
6117*7c478bd9Sstevel@tonic-gate 	ire_t	*best_fire = NULL;
6118*7c478bd9Sstevel@tonic-gate 	ire_t	*best_cire = NULL;
6119*7c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
6120*7c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
6121*7c478bd9Sstevel@tonic-gate 	ire_t	*fire;
6122*7c478bd9Sstevel@tonic-gate 	ire_t	*cire;
6123*7c478bd9Sstevel@tonic-gate 	irb_t	*firb = NULL;
6124*7c478bd9Sstevel@tonic-gate 	irb_t	*cirb = NULL;
6125*7c478bd9Sstevel@tonic-gate 	ire_t	*gw_ire;
6126*7c478bd9Sstevel@tonic-gate 	boolean_t	already_resolved;
6127*7c478bd9Sstevel@tonic-gate 	boolean_t	res;
6128*7c478bd9Sstevel@tonic-gate 	ipaddr_t	dst;
6129*7c478bd9Sstevel@tonic-gate 	ipaddr_t	gw;
6130*7c478bd9Sstevel@tonic-gate 
6131*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n",
6132*7c478bd9Sstevel@tonic-gate 	    (void *)*ire_arg, (void *)*fire_arg, flags));
6133*7c478bd9Sstevel@tonic-gate 
6134*7c478bd9Sstevel@tonic-gate 	ASSERT(ire_arg != NULL);
6135*7c478bd9Sstevel@tonic-gate 	ASSERT(fire_arg != NULL);
6136*7c478bd9Sstevel@tonic-gate 
6137*7c478bd9Sstevel@tonic-gate 	/* Not an IRE_HOST ire; give up. */
6138*7c478bd9Sstevel@tonic-gate 	if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) {
6139*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6140*7c478bd9Sstevel@tonic-gate 	}
6141*7c478bd9Sstevel@tonic-gate 
6142*7c478bd9Sstevel@tonic-gate 	/* This is the first IRE_HOST ire for that destination. */
6143*7c478bd9Sstevel@tonic-gate 	first_fire = *fire_arg;
6144*7c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
6145*7c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
6146*7c478bd9Sstevel@tonic-gate 
6147*7c478bd9Sstevel@tonic-gate 	dst = first_fire->ire_addr;
6148*7c478bd9Sstevel@tonic-gate 
6149*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst)));
6150*7c478bd9Sstevel@tonic-gate 
6151*7c478bd9Sstevel@tonic-gate 	/*
6152*7c478bd9Sstevel@tonic-gate 	 * Retrieve the first IRE_CACHE ire for that destination;
6153*7c478bd9Sstevel@tonic-gate 	 * if we don't find one, no route for that dest is
6154*7c478bd9Sstevel@tonic-gate 	 * resolved yet.
6155*7c478bd9Sstevel@tonic-gate 	 */
6156*7c478bd9Sstevel@tonic-gate 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID);
6157*7c478bd9Sstevel@tonic-gate 	if (first_cire != NULL) {
6158*7c478bd9Sstevel@tonic-gate 		cirb = first_cire->ire_bucket;
6159*7c478bd9Sstevel@tonic-gate 	}
6160*7c478bd9Sstevel@tonic-gate 
6161*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire));
6162*7c478bd9Sstevel@tonic-gate 
6163*7c478bd9Sstevel@tonic-gate 	/*
6164*7c478bd9Sstevel@tonic-gate 	 * Search for a resolvable route, giving the top priority
6165*7c478bd9Sstevel@tonic-gate 	 * to routes that can be resolved without any call to the resolver.
6166*7c478bd9Sstevel@tonic-gate 	 */
6167*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
6168*7c478bd9Sstevel@tonic-gate 
6169*7c478bd9Sstevel@tonic-gate 	if (!CLASSD(dst)) {
6170*7c478bd9Sstevel@tonic-gate 		/*
6171*7c478bd9Sstevel@tonic-gate 		 * For all multiroute IRE_HOST ires for that destination,
6172*7c478bd9Sstevel@tonic-gate 		 * check if the route via the IRE_HOST's gateway is
6173*7c478bd9Sstevel@tonic-gate 		 * resolved yet.
6174*7c478bd9Sstevel@tonic-gate 		 */
6175*7c478bd9Sstevel@tonic-gate 		for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
6176*7c478bd9Sstevel@tonic-gate 
6177*7c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
6178*7c478bd9Sstevel@tonic-gate 				continue;
6179*7c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
6180*7c478bd9Sstevel@tonic-gate 				continue;
6181*7c478bd9Sstevel@tonic-gate 
6182*7c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
6183*7c478bd9Sstevel@tonic-gate 
6184*7c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, "
6185*7c478bd9Sstevel@tonic-gate 			    "ire_addr %08x, ire_gateway_addr %08x\n",
6186*7c478bd9Sstevel@tonic-gate 			    (void *)fire, ntohl(fire->ire_addr), ntohl(gw)));
6187*7c478bd9Sstevel@tonic-gate 
6188*7c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
6189*7c478bd9Sstevel@tonic-gate 
6190*7c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
6191*7c478bd9Sstevel@tonic-gate 				ASSERT(cirb != NULL);
6192*7c478bd9Sstevel@tonic-gate 
6193*7c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
6194*7c478bd9Sstevel@tonic-gate 				/*
6195*7c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
6196*7c478bd9Sstevel@tonic-gate 				 * destination.
6197*7c478bd9Sstevel@tonic-gate 				 */
6198*7c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
6199*7c478bd9Sstevel@tonic-gate 				    cire != NULL;
6200*7c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
6201*7c478bd9Sstevel@tonic-gate 
6202*7c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
6203*7c478bd9Sstevel@tonic-gate 						continue;
6204*7c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
6205*7c478bd9Sstevel@tonic-gate 						continue;
6206*7c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
6207*7c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
6208*7c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
6209*7c478bd9Sstevel@tonic-gate 						continue;
6210*7c478bd9Sstevel@tonic-gate 					/*
6211*7c478bd9Sstevel@tonic-gate 					 * Check if the IRE_CACHE's gateway
6212*7c478bd9Sstevel@tonic-gate 					 * matches the IRE_HOST's gateway.
6213*7c478bd9Sstevel@tonic-gate 					 */
6214*7c478bd9Sstevel@tonic-gate 					if (cire->ire_gateway_addr == gw) {
6215*7c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
6216*7c478bd9Sstevel@tonic-gate 						break;
6217*7c478bd9Sstevel@tonic-gate 					}
6218*7c478bd9Sstevel@tonic-gate 				}
6219*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
6220*7c478bd9Sstevel@tonic-gate 			}
6221*7c478bd9Sstevel@tonic-gate 
6222*7c478bd9Sstevel@tonic-gate 			/*
6223*7c478bd9Sstevel@tonic-gate 			 * This route is already resolved;
6224*7c478bd9Sstevel@tonic-gate 			 * proceed with next one.
6225*7c478bd9Sstevel@tonic-gate 			 */
6226*7c478bd9Sstevel@tonic-gate 			if (already_resolved) {
6227*7c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found cire %p, "
6228*7c478bd9Sstevel@tonic-gate 				    "already resolved\n", (void *)cire));
6229*7c478bd9Sstevel@tonic-gate 				continue;
6230*7c478bd9Sstevel@tonic-gate 			}
6231*7c478bd9Sstevel@tonic-gate 
6232*7c478bd9Sstevel@tonic-gate 			/*
6233*7c478bd9Sstevel@tonic-gate 			 * The route is unresolved; is it actually
6234*7c478bd9Sstevel@tonic-gate 			 * resolvable, i.e. is there a cache or a resolver
6235*7c478bd9Sstevel@tonic-gate 			 * for the gateway?
6236*7c478bd9Sstevel@tonic-gate 			 */
6237*7c478bd9Sstevel@tonic-gate 			gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL,
6238*7c478bd9Sstevel@tonic-gate 			    ALL_ZONES, MATCH_IRE_RECURSIVE);
6239*7c478bd9Sstevel@tonic-gate 
6240*7c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n",
6241*7c478bd9Sstevel@tonic-gate 			    (void *)gw_ire));
6242*7c478bd9Sstevel@tonic-gate 
6243*7c478bd9Sstevel@tonic-gate 			/*
6244*7c478bd9Sstevel@tonic-gate 			 * If gw_ire is typed IRE_CACHETABLE,
6245*7c478bd9Sstevel@tonic-gate 			 * this route can be resolved without any call to the
6246*7c478bd9Sstevel@tonic-gate 			 * resolver. If the MULTIRT_CACHEGW flag is set,
6247*7c478bd9Sstevel@tonic-gate 			 * give the top priority to this ire and exit the
6248*7c478bd9Sstevel@tonic-gate 			 * loop.
6249*7c478bd9Sstevel@tonic-gate 			 * This is typically the case when an ARP reply
6250*7c478bd9Sstevel@tonic-gate 			 * is processed through ip_wput_nondata().
6251*7c478bd9Sstevel@tonic-gate 			 */
6252*7c478bd9Sstevel@tonic-gate 			if ((flags & MULTIRT_CACHEGW) &&
6253*7c478bd9Sstevel@tonic-gate 			    (gw_ire != NULL) &&
6254*7c478bd9Sstevel@tonic-gate 			    (gw_ire->ire_type & IRE_CACHETABLE)) {
6255*7c478bd9Sstevel@tonic-gate 				/*
6256*7c478bd9Sstevel@tonic-gate 				 * Release the resolver associated to the
6257*7c478bd9Sstevel@tonic-gate 				 * previous candidate best ire, if any.
6258*7c478bd9Sstevel@tonic-gate 				 */
6259*7c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
6260*7c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
6261*7c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
6262*7c478bd9Sstevel@tonic-gate 				}
6263*7c478bd9Sstevel@tonic-gate 
6264*7c478bd9Sstevel@tonic-gate 				best_fire = fire;
6265*7c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
6266*7c478bd9Sstevel@tonic-gate 
6267*7c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found top prio "
6268*7c478bd9Sstevel@tonic-gate 				    "best_fire %p, best_cire %p\n",
6269*7c478bd9Sstevel@tonic-gate 				    (void *)best_fire, (void *)best_cire));
6270*7c478bd9Sstevel@tonic-gate 				break;
6271*7c478bd9Sstevel@tonic-gate 			}
6272*7c478bd9Sstevel@tonic-gate 
6273*7c478bd9Sstevel@tonic-gate 			/*
6274*7c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
6275*7c478bd9Sstevel@tonic-gate 			 * attempt to  resolve that route.
6276*7c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take that
6277*7c478bd9Sstevel@tonic-gate 			 * route into account only if this time interval
6278*7c478bd9Sstevel@tonic-gate 			 * exceeds ip_multirt_resolution_interval;
6279*7c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
6280*7c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
6281*7c478bd9Sstevel@tonic-gate 			 */
6282*7c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
6283*7c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
6284*7c478bd9Sstevel@tonic-gate 
6285*7c478bd9Sstevel@tonic-gate 			res = (boolean_t)
6286*7c478bd9Sstevel@tonic-gate 			    ((delta > ip_multirt_resolution_interval) ||
6287*7c478bd9Sstevel@tonic-gate 				(!(flags & MULTIRT_USESTAMP)));
6288*7c478bd9Sstevel@tonic-gate 
6289*7c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, "
6290*7c478bd9Sstevel@tonic-gate 			    "res %d\n",
6291*7c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, res));
6292*7c478bd9Sstevel@tonic-gate 
6293*7c478bd9Sstevel@tonic-gate 			if (res) {
6294*7c478bd9Sstevel@tonic-gate 				/*
6295*7c478bd9Sstevel@tonic-gate 				 * We are here if MULTIRT_USESTAMP flag is set
6296*7c478bd9Sstevel@tonic-gate 				 * and the resolver for fire's gateway
6297*7c478bd9Sstevel@tonic-gate 				 * has not been tried since
6298*7c478bd9Sstevel@tonic-gate 				 * ip_multirt_resolution_interval, or if
6299*7c478bd9Sstevel@tonic-gate 				 * MULTIRT_USESTAMP is not set but gw_ire did
6300*7c478bd9Sstevel@tonic-gate 				 * not fill the conditions for MULTIRT_CACHEGW,
6301*7c478bd9Sstevel@tonic-gate 				 * or if neither MULTIRT_USESTAMP nor
6302*7c478bd9Sstevel@tonic-gate 				 * MULTIRT_CACHEGW are set.
6303*7c478bd9Sstevel@tonic-gate 				 */
6304*7c478bd9Sstevel@tonic-gate 				if (gw_ire != NULL) {
6305*7c478bd9Sstevel@tonic-gate 					if (best_fire == NULL) {
6306*7c478bd9Sstevel@tonic-gate 						ASSERT(best_cire == NULL);
6307*7c478bd9Sstevel@tonic-gate 
6308*7c478bd9Sstevel@tonic-gate 						best_fire = fire;
6309*7c478bd9Sstevel@tonic-gate 						best_cire = gw_ire;
6310*7c478bd9Sstevel@tonic-gate 
6311*7c478bd9Sstevel@tonic-gate 						ip2dbg(("ire_multirt_lookup:"
6312*7c478bd9Sstevel@tonic-gate 						    "found candidate "
6313*7c478bd9Sstevel@tonic-gate 						    "best_fire %p, "
6314*7c478bd9Sstevel@tonic-gate 						    "best_cire %p\n",
6315*7c478bd9Sstevel@tonic-gate 						    (void *)best_fire,
6316*7c478bd9Sstevel@tonic-gate 						    (void *)best_cire));
6317*7c478bd9Sstevel@tonic-gate 
6318*7c478bd9Sstevel@tonic-gate 						/*
6319*7c478bd9Sstevel@tonic-gate 						 * If MULTIRT_CACHEGW is not
6320*7c478bd9Sstevel@tonic-gate 						 * set, we ignore the top
6321*7c478bd9Sstevel@tonic-gate 						 * priority ires that can
6322*7c478bd9Sstevel@tonic-gate 						 * be resolved without any
6323*7c478bd9Sstevel@tonic-gate 						 * call to the resolver;
6324*7c478bd9Sstevel@tonic-gate 						 * In that case, there is
6325*7c478bd9Sstevel@tonic-gate 						 * actually no need
6326*7c478bd9Sstevel@tonic-gate 						 * to continue the loop.
6327*7c478bd9Sstevel@tonic-gate 						 */
6328*7c478bd9Sstevel@tonic-gate 						if (!(flags &
6329*7c478bd9Sstevel@tonic-gate 						    MULTIRT_CACHEGW)) {
6330*7c478bd9Sstevel@tonic-gate 							break;
6331*7c478bd9Sstevel@tonic-gate 						}
6332*7c478bd9Sstevel@tonic-gate 						continue;
6333*7c478bd9Sstevel@tonic-gate 					}
6334*7c478bd9Sstevel@tonic-gate 				} else {
6335*7c478bd9Sstevel@tonic-gate 					/*
6336*7c478bd9Sstevel@tonic-gate 					 * No resolver for the gateway: the
6337*7c478bd9Sstevel@tonic-gate 					 * route is not resolvable.
6338*7c478bd9Sstevel@tonic-gate 					 * If the MULTIRT_SETSTAMP flag is
6339*7c478bd9Sstevel@tonic-gate 					 * set, we stamp the IRE_HOST ire,
6340*7c478bd9Sstevel@tonic-gate 					 * so we will not select it again
6341*7c478bd9Sstevel@tonic-gate 					 * during this resolution interval.
6342*7c478bd9Sstevel@tonic-gate 					 */
6343*7c478bd9Sstevel@tonic-gate 					if (flags & MULTIRT_SETSTAMP)
6344*7c478bd9Sstevel@tonic-gate 						fire->ire_last_used_time =
6345*7c478bd9Sstevel@tonic-gate 						    lbolt;
6346*7c478bd9Sstevel@tonic-gate 				}
6347*7c478bd9Sstevel@tonic-gate 			}
6348*7c478bd9Sstevel@tonic-gate 
6349*7c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL)
6350*7c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
6351*7c478bd9Sstevel@tonic-gate 		}
6352*7c478bd9Sstevel@tonic-gate 	} else { /* CLASSD(dst) */
6353*7c478bd9Sstevel@tonic-gate 
6354*7c478bd9Sstevel@tonic-gate 		for (fire = first_fire;
6355*7c478bd9Sstevel@tonic-gate 		    fire != NULL;
6356*7c478bd9Sstevel@tonic-gate 		    fire = fire->ire_next) {
6357*7c478bd9Sstevel@tonic-gate 
6358*7c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
6359*7c478bd9Sstevel@tonic-gate 				continue;
6360*7c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
6361*7c478bd9Sstevel@tonic-gate 				continue;
6362*7c478bd9Sstevel@tonic-gate 
6363*7c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
6364*7c478bd9Sstevel@tonic-gate 
6365*7c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
6366*7c478bd9Sstevel@tonic-gate 
6367*7c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE,
6368*7c478bd9Sstevel@tonic-gate 			    NULL, NULL, ALL_ZONES, 0,
6369*7c478bd9Sstevel@tonic-gate 			    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE);
6370*7c478bd9Sstevel@tonic-gate 
6371*7c478bd9Sstevel@tonic-gate 			/* No resolver for the gateway; we skip this ire. */
6372*7c478bd9Sstevel@tonic-gate 			if (gw_ire == NULL) {
6373*7c478bd9Sstevel@tonic-gate 				continue;
6374*7c478bd9Sstevel@tonic-gate 			}
6375*7c478bd9Sstevel@tonic-gate 
6376*7c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
6377*7c478bd9Sstevel@tonic-gate 
6378*7c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
6379*7c478bd9Sstevel@tonic-gate 				/*
6380*7c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
6381*7c478bd9Sstevel@tonic-gate 				 * destination.
6382*7c478bd9Sstevel@tonic-gate 				 */
6383*7c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
6384*7c478bd9Sstevel@tonic-gate 				    cire != NULL;
6385*7c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
6386*7c478bd9Sstevel@tonic-gate 
6387*7c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
6388*7c478bd9Sstevel@tonic-gate 						continue;
6389*7c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
6390*7c478bd9Sstevel@tonic-gate 						continue;
6391*7c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
6392*7c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
6393*7c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
6394*7c478bd9Sstevel@tonic-gate 						continue;
6395*7c478bd9Sstevel@tonic-gate 
6396*7c478bd9Sstevel@tonic-gate 					/*
6397*7c478bd9Sstevel@tonic-gate 					 * Cache entries are linked to the
6398*7c478bd9Sstevel@tonic-gate 					 * parent routes using the parent handle
6399*7c478bd9Sstevel@tonic-gate 					 * (ire_phandle). If no cache entry has
6400*7c478bd9Sstevel@tonic-gate 					 * the same handle as fire, fire is
6401*7c478bd9Sstevel@tonic-gate 					 * still unresolved.
6402*7c478bd9Sstevel@tonic-gate 					 */
6403*7c478bd9Sstevel@tonic-gate 					ASSERT(cire->ire_phandle != 0);
6404*7c478bd9Sstevel@tonic-gate 					if (cire->ire_phandle ==
6405*7c478bd9Sstevel@tonic-gate 					    fire->ire_phandle) {
6406*7c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
6407*7c478bd9Sstevel@tonic-gate 						break;
6408*7c478bd9Sstevel@tonic-gate 					}
6409*7c478bd9Sstevel@tonic-gate 				}
6410*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
6411*7c478bd9Sstevel@tonic-gate 			}
6412*7c478bd9Sstevel@tonic-gate 
6413*7c478bd9Sstevel@tonic-gate 			/*
6414*7c478bd9Sstevel@tonic-gate 			 * This route is already resolved; proceed with
6415*7c478bd9Sstevel@tonic-gate 			 * next one.
6416*7c478bd9Sstevel@tonic-gate 			 */
6417*7c478bd9Sstevel@tonic-gate 			if (already_resolved) {
6418*7c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
6419*7c478bd9Sstevel@tonic-gate 				continue;
6420*7c478bd9Sstevel@tonic-gate 			}
6421*7c478bd9Sstevel@tonic-gate 
6422*7c478bd9Sstevel@tonic-gate 			/*
6423*7c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
6424*7c478bd9Sstevel@tonic-gate 			 * attempt to resolve that route.
6425*7c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take
6426*7c478bd9Sstevel@tonic-gate 			 * that route into account only if this time
6427*7c478bd9Sstevel@tonic-gate 			 * interval exceeds ip_multirt_resolution_interval;
6428*7c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
6429*7c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
6430*7c478bd9Sstevel@tonic-gate 			 */
6431*7c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
6432*7c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
6433*7c478bd9Sstevel@tonic-gate 
6434*7c478bd9Sstevel@tonic-gate 			res = (boolean_t)
6435*7c478bd9Sstevel@tonic-gate 			    ((delta > ip_multirt_resolution_interval) ||
6436*7c478bd9Sstevel@tonic-gate 			    (!(flags & MULTIRT_USESTAMP)));
6437*7c478bd9Sstevel@tonic-gate 
6438*7c478bd9Sstevel@tonic-gate 			ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, "
6439*7c478bd9Sstevel@tonic-gate 			    "flags %04x, res %d\n",
6440*7c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, flags, res));
6441*7c478bd9Sstevel@tonic-gate 
6442*7c478bd9Sstevel@tonic-gate 			if (res) {
6443*7c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
6444*7c478bd9Sstevel@tonic-gate 					/*
6445*7c478bd9Sstevel@tonic-gate 					 * Release the resolver associated
6446*7c478bd9Sstevel@tonic-gate 					 * to the preceding candidate best
6447*7c478bd9Sstevel@tonic-gate 					 * ire, if any.
6448*7c478bd9Sstevel@tonic-gate 					 */
6449*7c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
6450*7c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
6451*7c478bd9Sstevel@tonic-gate 				}
6452*7c478bd9Sstevel@tonic-gate 				best_fire = fire;
6453*7c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
6454*7c478bd9Sstevel@tonic-gate 				continue;
6455*7c478bd9Sstevel@tonic-gate 			}
6456*7c478bd9Sstevel@tonic-gate 
6457*7c478bd9Sstevel@tonic-gate 			ire_refrele(gw_ire);
6458*7c478bd9Sstevel@tonic-gate 		}
6459*7c478bd9Sstevel@tonic-gate 	}
6460*7c478bd9Sstevel@tonic-gate 
6461*7c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
6462*7c478bd9Sstevel@tonic-gate 		IRE_REFHOLD(best_fire);
6463*7c478bd9Sstevel@tonic-gate 	}
6464*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
6465*7c478bd9Sstevel@tonic-gate 
6466*7c478bd9Sstevel@tonic-gate 	/* Release the first IRE_CACHE we initially looked up, if any. */
6467*7c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
6468*7c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
6469*7c478bd9Sstevel@tonic-gate 
6470*7c478bd9Sstevel@tonic-gate 	/* Found a resolvable route. */
6471*7c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
6472*7c478bd9Sstevel@tonic-gate 		ASSERT(best_cire != NULL);
6473*7c478bd9Sstevel@tonic-gate 
6474*7c478bd9Sstevel@tonic-gate 		if (*fire_arg != NULL)
6475*7c478bd9Sstevel@tonic-gate 			ire_refrele(*fire_arg);
6476*7c478bd9Sstevel@tonic-gate 		if (*ire_arg != NULL)
6477*7c478bd9Sstevel@tonic-gate 			ire_refrele(*ire_arg);
6478*7c478bd9Sstevel@tonic-gate 
6479*7c478bd9Sstevel@tonic-gate 		/*
6480*7c478bd9Sstevel@tonic-gate 		 * Update the passed-in arguments with the
6481*7c478bd9Sstevel@tonic-gate 		 * resolvable multirt route we found.
6482*7c478bd9Sstevel@tonic-gate 		 */
6483*7c478bd9Sstevel@tonic-gate 		*fire_arg = best_fire;
6484*7c478bd9Sstevel@tonic-gate 		*ire_arg = best_cire;
6485*7c478bd9Sstevel@tonic-gate 
6486*7c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_multirt_lookup: returning B_TRUE, "
6487*7c478bd9Sstevel@tonic-gate 		    "*fire_arg %p, *ire_arg %p\n",
6488*7c478bd9Sstevel@tonic-gate 		    (void *)best_fire, (void *)best_cire));
6489*7c478bd9Sstevel@tonic-gate 
6490*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6491*7c478bd9Sstevel@tonic-gate 	}
6492*7c478bd9Sstevel@tonic-gate 
6493*7c478bd9Sstevel@tonic-gate 	ASSERT(best_cire == NULL);
6494*7c478bd9Sstevel@tonic-gate 
6495*7c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, "
6496*7c478bd9Sstevel@tonic-gate 	    "*ire_arg %p\n",
6497*7c478bd9Sstevel@tonic-gate 	    (void *)*fire_arg, (void *)*ire_arg));
6498*7c478bd9Sstevel@tonic-gate 
6499*7c478bd9Sstevel@tonic-gate 	/* No resolvable route. */
6500*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
6501*7c478bd9Sstevel@tonic-gate }
6502*7c478bd9Sstevel@tonic-gate 
6503*7c478bd9Sstevel@tonic-gate /*
6504*7c478bd9Sstevel@tonic-gate  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
6505*7c478bd9Sstevel@tonic-gate  * that goes through 'ipif'. As a fallback, a route that goes through
6506*7c478bd9Sstevel@tonic-gate  * ipif->ipif_ill can be returned.
6507*7c478bd9Sstevel@tonic-gate  */
6508*7c478bd9Sstevel@tonic-gate ire_t *
6509*7c478bd9Sstevel@tonic-gate ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
6510*7c478bd9Sstevel@tonic-gate {
6511*7c478bd9Sstevel@tonic-gate 	ire_t	*ire;
6512*7c478bd9Sstevel@tonic-gate 	ire_t	*save_ire = NULL;
6513*7c478bd9Sstevel@tonic-gate 	ire_t   *gw_ire;
6514*7c478bd9Sstevel@tonic-gate 	irb_t   *irb;
6515*7c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
6516*7c478bd9Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
6517*7c478bd9Sstevel@tonic-gate 
6518*7c478bd9Sstevel@tonic-gate 	ASSERT(CLASSD(group));
6519*7c478bd9Sstevel@tonic-gate 
6520*7c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
6521*7c478bd9Sstevel@tonic-gate 	    MATCH_IRE_DEFAULT);
6522*7c478bd9Sstevel@tonic-gate 
6523*7c478bd9Sstevel@tonic-gate 	if (ire == NULL)
6524*7c478bd9Sstevel@tonic-gate 		return (NULL);
6525*7c478bd9Sstevel@tonic-gate 
6526*7c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
6527*7c478bd9Sstevel@tonic-gate 	ASSERT(irb);
6528*7c478bd9Sstevel@tonic-gate 
6529*7c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
6530*7c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
6531*7c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
6532*7c478bd9Sstevel@tonic-gate 		if (ire->ire_addr != group ||
6533*7c478bd9Sstevel@tonic-gate 		    ipif->ipif_zoneid != ire->ire_zoneid) {
6534*7c478bd9Sstevel@tonic-gate 			continue;
6535*7c478bd9Sstevel@tonic-gate 		}
6536*7c478bd9Sstevel@tonic-gate 
6537*7c478bd9Sstevel@tonic-gate 		switch (ire->ire_type) {
6538*7c478bd9Sstevel@tonic-gate 		case IRE_DEFAULT:
6539*7c478bd9Sstevel@tonic-gate 		case IRE_PREFIX:
6540*7c478bd9Sstevel@tonic-gate 		case IRE_HOST:
6541*7c478bd9Sstevel@tonic-gate 			gw_addr = ire->ire_gateway_addr;
6542*7c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
6543*7c478bd9Sstevel@tonic-gate 			    ipif, NULL, ALL_ZONES, 0, match_flags);
6544*7c478bd9Sstevel@tonic-gate 
6545*7c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL) {
6546*7c478bd9Sstevel@tonic-gate 				if (save_ire != NULL) {
6547*7c478bd9Sstevel@tonic-gate 					ire_refrele(save_ire);
6548*7c478bd9Sstevel@tonic-gate 				}
6549*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
6550*7c478bd9Sstevel@tonic-gate 				if (gw_ire->ire_ipif == ipif) {
6551*7c478bd9Sstevel@tonic-gate 					ire_refrele(gw_ire);
6552*7c478bd9Sstevel@tonic-gate 
6553*7c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb);
6554*7c478bd9Sstevel@tonic-gate 					return (ire);
6555*7c478bd9Sstevel@tonic-gate 				}
6556*7c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
6557*7c478bd9Sstevel@tonic-gate 				save_ire = ire;
6558*7c478bd9Sstevel@tonic-gate 			}
6559*7c478bd9Sstevel@tonic-gate 			break;
6560*7c478bd9Sstevel@tonic-gate 		case IRE_IF_NORESOLVER:
6561*7c478bd9Sstevel@tonic-gate 		case IRE_IF_RESOLVER:
6562*7c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif == ipif) {
6563*7c478bd9Sstevel@tonic-gate 				if (save_ire != NULL) {
6564*7c478bd9Sstevel@tonic-gate 					ire_refrele(save_ire);
6565*7c478bd9Sstevel@tonic-gate 				}
6566*7c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
6567*7c478bd9Sstevel@tonic-gate 
6568*7c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
6569*7c478bd9Sstevel@tonic-gate 				return (ire);
6570*7c478bd9Sstevel@tonic-gate 			}
6571*7c478bd9Sstevel@tonic-gate 			break;
6572*7c478bd9Sstevel@tonic-gate 		}
6573*7c478bd9Sstevel@tonic-gate 	}
6574*7c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
6575*7c478bd9Sstevel@tonic-gate 
6576*7c478bd9Sstevel@tonic-gate 	return (save_ire);
6577*7c478bd9Sstevel@tonic-gate }
6578*7c478bd9Sstevel@tonic-gate 
6579*7c478bd9Sstevel@tonic-gate /*
6580*7c478bd9Sstevel@tonic-gate  * The purpose of the next two functions is to provide some external access to
6581*7c478bd9Sstevel@tonic-gate  * routing/l2 lookup functionality while hiding the implementation of routing
6582*7c478bd9Sstevel@tonic-gate  * and interface data structures (IRE/ILL).  Thus, interfaces are passed/
6583*7c478bd9Sstevel@tonic-gate  * returned by name instead of by ILL reference.  These functions are used by
6584*7c478bd9Sstevel@tonic-gate  * IP Filter.
6585*7c478bd9Sstevel@tonic-gate  * Return a link layer header suitable for an IP packet being sent to the
6586*7c478bd9Sstevel@tonic-gate  * dst_addr IP address.  The interface associated with the route is put into
6587*7c478bd9Sstevel@tonic-gate  * ifname, which must be a buffer of LIFNAMSIZ bytes.  The dst_addr is the
6588*7c478bd9Sstevel@tonic-gate  * packet's ultimate destination address, not a router address.
6589*7c478bd9Sstevel@tonic-gate  *
6590*7c478bd9Sstevel@tonic-gate  * This function is used when the caller wants to know the outbound interface
6591*7c478bd9Sstevel@tonic-gate  * and MAC header for a packet given only the address.
6592*7c478bd9Sstevel@tonic-gate  */
6593*7c478bd9Sstevel@tonic-gate mblk_t *
6594*7c478bd9Sstevel@tonic-gate ip_nexthop_route(const struct sockaddr *target, char *ifname)
6595*7c478bd9Sstevel@tonic-gate {
6596*7c478bd9Sstevel@tonic-gate 	ire_t *dir, *gw;
6597*7c478bd9Sstevel@tonic-gate 	ill_t *ill;
6598*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6599*7c478bd9Sstevel@tonic-gate 
6600*7c478bd9Sstevel@tonic-gate 	/* parameter sanity */
6601*7c478bd9Sstevel@tonic-gate 	if (ifname == NULL || target == NULL)
6602*7c478bd9Sstevel@tonic-gate 		return (NULL);
6603*7c478bd9Sstevel@tonic-gate 
6604*7c478bd9Sstevel@tonic-gate 	gw = NULL;
6605*7c478bd9Sstevel@tonic-gate 
6606*7c478bd9Sstevel@tonic-gate 	/* Find the route entry, if it exists. */
6607*7c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
6608*7c478bd9Sstevel@tonic-gate 	case AF_INET:
6609*7c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup(
6610*7c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)target)->sin_addr.s_addr,
6611*7c478bd9Sstevel@tonic-gate 		    0xffffffff,
6612*7c478bd9Sstevel@tonic-gate 		    0, 0, NULL, &gw, ALL_ZONES,
6613*7c478bd9Sstevel@tonic-gate 		    MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE);
6614*7c478bd9Sstevel@tonic-gate 		break;
6615*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
6616*7c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup_v6(
6617*7c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in6 *)target)->sin6_addr,
6618*7c478bd9Sstevel@tonic-gate 		    NULL,
6619*7c478bd9Sstevel@tonic-gate 		    0, 0, NULL, &gw, ALL_ZONES,
6620*7c478bd9Sstevel@tonic-gate 		    MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE);
6621*7c478bd9Sstevel@tonic-gate 		break;
6622*7c478bd9Sstevel@tonic-gate 	default:
6623*7c478bd9Sstevel@tonic-gate 		dir = NULL;
6624*7c478bd9Sstevel@tonic-gate 		break;
6625*7c478bd9Sstevel@tonic-gate 	}
6626*7c478bd9Sstevel@tonic-gate 
6627*7c478bd9Sstevel@tonic-gate 
6628*7c478bd9Sstevel@tonic-gate 	if (dir == NULL)
6629*7c478bd9Sstevel@tonic-gate 		return (NULL);
6630*7c478bd9Sstevel@tonic-gate 
6631*7c478bd9Sstevel@tonic-gate 	/* Map the IRE to an ILL so we can fill in ifname. */
6632*7c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(dir);
6633*7c478bd9Sstevel@tonic-gate 	if (ill == NULL) {
6634*7c478bd9Sstevel@tonic-gate 		ire_refrele(dir);
6635*7c478bd9Sstevel@tonic-gate 		return (NULL);
6636*7c478bd9Sstevel@tonic-gate 	}
6637*7c478bd9Sstevel@tonic-gate 	(void) strncpy(ifname, ill->ill_name, LIFNAMSIZ);
6638*7c478bd9Sstevel@tonic-gate 
6639*7c478bd9Sstevel@tonic-gate 	/* Return a copy of the header to the caller. */
6640*7c478bd9Sstevel@tonic-gate 	if (dir->ire_fp_mp != NULL) {
6641*7c478bd9Sstevel@tonic-gate 		if ((mp = dupb(dir->ire_fp_mp)) == NULL)
6642*7c478bd9Sstevel@tonic-gate 			mp = copyb(dir->ire_fp_mp);
6643*7c478bd9Sstevel@tonic-gate 	} else if (dir->ire_dlureq_mp != NULL) {
6644*7c478bd9Sstevel@tonic-gate 		if ((mp = dupb(dir->ire_dlureq_mp)) == NULL)
6645*7c478bd9Sstevel@tonic-gate 			mp = copyb(dir->ire_dlureq_mp);
6646*7c478bd9Sstevel@tonic-gate 	} else {
6647*7c478bd9Sstevel@tonic-gate 		mp = NULL;
6648*7c478bd9Sstevel@tonic-gate 	}
6649*7c478bd9Sstevel@tonic-gate 
6650*7c478bd9Sstevel@tonic-gate 	ire_refrele(dir);
6651*7c478bd9Sstevel@tonic-gate 	return (mp);
6652*7c478bd9Sstevel@tonic-gate }
6653*7c478bd9Sstevel@tonic-gate 
6654*7c478bd9Sstevel@tonic-gate 
6655*7c478bd9Sstevel@tonic-gate /*
6656*7c478bd9Sstevel@tonic-gate  * Return a link layer header suitable for an IP packet being sent to the
6657*7c478bd9Sstevel@tonic-gate  * dst_addr IP address on the specified output interface.  The dst_addr
6658*7c478bd9Sstevel@tonic-gate  * may be the packet's ultimate destination or a predetermined next hop
6659*7c478bd9Sstevel@tonic-gate  * router's address.
6660*7c478bd9Sstevel@tonic-gate  * ifname must be nul-terminated.
6661*7c478bd9Sstevel@tonic-gate  *
6662*7c478bd9Sstevel@tonic-gate  * This function is used when the caller knows the outbound interface (usually
6663*7c478bd9Sstevel@tonic-gate  * because it was specified by policy) and only needs the MAC header for a
6664*7c478bd9Sstevel@tonic-gate  * packet.
6665*7c478bd9Sstevel@tonic-gate  */
6666*7c478bd9Sstevel@tonic-gate mblk_t *
6667*7c478bd9Sstevel@tonic-gate ip_nexthop(const struct sockaddr *target, const char *ifname)
6668*7c478bd9Sstevel@tonic-gate {
6669*7c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
6670*7c478bd9Sstevel@tonic-gate 	t_uscalar_t sap;
6671*7c478bd9Sstevel@tonic-gate 	ire_t *dir, *gw;
6672*7c478bd9Sstevel@tonic-gate 	ill_t *ill;
6673*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6674*7c478bd9Sstevel@tonic-gate 
6675*7c478bd9Sstevel@tonic-gate 	/* parameter sanity */
6676*7c478bd9Sstevel@tonic-gate 	if (ifname == NULL || target == NULL)
6677*7c478bd9Sstevel@tonic-gate 		return (NULL);
6678*7c478bd9Sstevel@tonic-gate 
6679*7c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
6680*7c478bd9Sstevel@tonic-gate 	case AF_INET :
6681*7c478bd9Sstevel@tonic-gate 		sap = IP_DL_SAP;
6682*7c478bd9Sstevel@tonic-gate 		break;
6683*7c478bd9Sstevel@tonic-gate 	case AF_INET6 :
6684*7c478bd9Sstevel@tonic-gate 		sap = IP6_DL_SAP;
6685*7c478bd9Sstevel@tonic-gate 		break;
6686*7c478bd9Sstevel@tonic-gate 	default:
6687*7c478bd9Sstevel@tonic-gate 		return (NULL);
6688*7c478bd9Sstevel@tonic-gate 	}
6689*7c478bd9Sstevel@tonic-gate 
6690*7c478bd9Sstevel@tonic-gate 	/* Lock ill_g_lock before walking through the list */
6691*7c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
6692*7c478bd9Sstevel@tonic-gate 	/*
6693*7c478bd9Sstevel@tonic-gate 	 * Can we find the interface name among those currently configured?
6694*7c478bd9Sstevel@tonic-gate 	 */
6695*7c478bd9Sstevel@tonic-gate 	for (ill = ILL_START_WALK_ALL(&ctx); ill != NULL;
6696*7c478bd9Sstevel@tonic-gate 	    ill = ill_next(&ctx, ill)) {
6697*7c478bd9Sstevel@tonic-gate 		if ((strcmp(ifname, ill->ill_name) == 0) &&
6698*7c478bd9Sstevel@tonic-gate 		    (ill->ill_sap == sap))
6699*7c478bd9Sstevel@tonic-gate 			break;
6700*7c478bd9Sstevel@tonic-gate 	}
6701*7c478bd9Sstevel@tonic-gate 	if (ill == NULL || ill->ill_ipif == NULL) {
6702*7c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
6703*7c478bd9Sstevel@tonic-gate 		return (NULL);
6704*7c478bd9Sstevel@tonic-gate 	}
6705*7c478bd9Sstevel@tonic-gate 
6706*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
6707*7c478bd9Sstevel@tonic-gate 	if (!ILL_CAN_LOOKUP(ill)) {
6708*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
6709*7c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
6710*7c478bd9Sstevel@tonic-gate 		return (NULL);
6711*7c478bd9Sstevel@tonic-gate 	}
6712*7c478bd9Sstevel@tonic-gate 	ill_refhold_locked(ill);
6713*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
6714*7c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
6715*7c478bd9Sstevel@tonic-gate 
6716*7c478bd9Sstevel@tonic-gate 	gw = NULL;
6717*7c478bd9Sstevel@tonic-gate 	/* Find the resolver entry, if it exists. */
6718*7c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
6719*7c478bd9Sstevel@tonic-gate 	case AF_INET:
6720*7c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup(
6721*7c478bd9Sstevel@tonic-gate 			((struct sockaddr_in *)target)->sin_addr.s_addr,
6722*7c478bd9Sstevel@tonic-gate 			0xffffffff,
6723*7c478bd9Sstevel@tonic-gate 			0, 0, ill->ill_ipif, &gw, ALL_ZONES,
6724*7c478bd9Sstevel@tonic-gate 			MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|
6725*7c478bd9Sstevel@tonic-gate 			MATCH_IRE_RECURSIVE|MATCH_IRE_IPIF);
6726*7c478bd9Sstevel@tonic-gate 		break;
6727*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
6728*7c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup_v6(
6729*7c478bd9Sstevel@tonic-gate 			&((struct sockaddr_in6 *)target)->sin6_addr, NULL,
6730*7c478bd9Sstevel@tonic-gate 			0, 0, ill->ill_ipif, &gw, ALL_ZONES,
6731*7c478bd9Sstevel@tonic-gate 			MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|
6732*7c478bd9Sstevel@tonic-gate 			MATCH_IRE_RECURSIVE|MATCH_IRE_IPIF);
6733*7c478bd9Sstevel@tonic-gate 		break;
6734*7c478bd9Sstevel@tonic-gate 	default:
6735*7c478bd9Sstevel@tonic-gate 		dir = NULL;
6736*7c478bd9Sstevel@tonic-gate 		break;
6737*7c478bd9Sstevel@tonic-gate 	}
6738*7c478bd9Sstevel@tonic-gate 
6739*7c478bd9Sstevel@tonic-gate 	ill_refrele(ill);
6740*7c478bd9Sstevel@tonic-gate 
6741*7c478bd9Sstevel@tonic-gate 	if (dir == NULL)
6742*7c478bd9Sstevel@tonic-gate 		return (NULL);
6743*7c478bd9Sstevel@tonic-gate 
6744*7c478bd9Sstevel@tonic-gate 	/* Return a copy of the header to the caller. */
6745*7c478bd9Sstevel@tonic-gate 	if (dir->ire_fp_mp != NULL) {
6746*7c478bd9Sstevel@tonic-gate 		if ((mp = dupb(dir->ire_fp_mp)) == NULL)
6747*7c478bd9Sstevel@tonic-gate 			mp = copyb(dir->ire_fp_mp);
6748*7c478bd9Sstevel@tonic-gate 	} else if (dir->ire_dlureq_mp != NULL) {
6749*7c478bd9Sstevel@tonic-gate 		if ((mp = dupb(dir->ire_dlureq_mp)) == NULL)
6750*7c478bd9Sstevel@tonic-gate 			mp = copyb(dir->ire_dlureq_mp);
6751*7c478bd9Sstevel@tonic-gate 	} else {
6752*7c478bd9Sstevel@tonic-gate 		mp = NULL;
6753*7c478bd9Sstevel@tonic-gate 	}
6754*7c478bd9Sstevel@tonic-gate 
6755*7c478bd9Sstevel@tonic-gate 
6756*7c478bd9Sstevel@tonic-gate 	ire_refrele(dir);
6757*7c478bd9Sstevel@tonic-gate 	return (mp);
6758*7c478bd9Sstevel@tonic-gate }
6759*7c478bd9Sstevel@tonic-gate 
6760*7c478bd9Sstevel@tonic-gate /*
6761*7c478bd9Sstevel@tonic-gate  * IRE iterator for inbound and loopback broadcast processing.
6762*7c478bd9Sstevel@tonic-gate  * Given an IRE_BROADCAST ire, walk the ires with the same destination
6763*7c478bd9Sstevel@tonic-gate  * address, but skip over the passed-in ire. Returns the next ire without
6764*7c478bd9Sstevel@tonic-gate  * a hold - assumes that the caller holds a reference on the IRE bucket.
6765*7c478bd9Sstevel@tonic-gate  */
6766*7c478bd9Sstevel@tonic-gate ire_t *
6767*7c478bd9Sstevel@tonic-gate ire_get_next_bcast_ire(ire_t *curr, ire_t *ire)
6768*7c478bd9Sstevel@tonic-gate {
6769*7c478bd9Sstevel@tonic-gate 	ill_t *ill;
6770*7c478bd9Sstevel@tonic-gate 
6771*7c478bd9Sstevel@tonic-gate 	if (curr == NULL) {
6772*7c478bd9Sstevel@tonic-gate 		for (curr = ire->ire_bucket->irb_ire; curr != NULL;
6773*7c478bd9Sstevel@tonic-gate 		    curr = curr->ire_next) {
6774*7c478bd9Sstevel@tonic-gate 			if (curr->ire_addr == ire->ire_addr)
6775*7c478bd9Sstevel@tonic-gate 				break;
6776*7c478bd9Sstevel@tonic-gate 		}
6777*7c478bd9Sstevel@tonic-gate 	} else {
6778*7c478bd9Sstevel@tonic-gate 		curr = curr->ire_next;
6779*7c478bd9Sstevel@tonic-gate 	}
6780*7c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
6781*7c478bd9Sstevel@tonic-gate 	for (; curr != NULL; curr = curr->ire_next) {
6782*7c478bd9Sstevel@tonic-gate 		if (curr->ire_addr != ire->ire_addr) {
6783*7c478bd9Sstevel@tonic-gate 			/*
6784*7c478bd9Sstevel@tonic-gate 			 * All the IREs to a given destination are contiguous;
6785*7c478bd9Sstevel@tonic-gate 			 * break out once the address doesn't match.
6786*7c478bd9Sstevel@tonic-gate 			 */
6787*7c478bd9Sstevel@tonic-gate 			break;
6788*7c478bd9Sstevel@tonic-gate 		}
6789*7c478bd9Sstevel@tonic-gate 		if (curr == ire) {
6790*7c478bd9Sstevel@tonic-gate 			/* skip over the passed-in ire */
6791*7c478bd9Sstevel@tonic-gate 			continue;
6792*7c478bd9Sstevel@tonic-gate 		}
6793*7c478bd9Sstevel@tonic-gate 		if ((curr->ire_stq != NULL && ire->ire_stq == NULL) ||
6794*7c478bd9Sstevel@tonic-gate 		    (curr->ire_stq == NULL && ire->ire_stq != NULL)) {
6795*7c478bd9Sstevel@tonic-gate 			/*
6796*7c478bd9Sstevel@tonic-gate 			 * If the passed-in ire is loopback, skip over
6797*7c478bd9Sstevel@tonic-gate 			 * non-loopback ires and vice versa.
6798*7c478bd9Sstevel@tonic-gate 			 */
6799*7c478bd9Sstevel@tonic-gate 			continue;
6800*7c478bd9Sstevel@tonic-gate 		}
6801*7c478bd9Sstevel@tonic-gate 		if (ire_to_ill(curr) != ill) {
6802*7c478bd9Sstevel@tonic-gate 			/* skip over IREs going through a different interface */
6803*7c478bd9Sstevel@tonic-gate 			continue;
6804*7c478bd9Sstevel@tonic-gate 		}
6805*7c478bd9Sstevel@tonic-gate 		if (curr->ire_marks & IRE_MARK_CONDEMNED) {
6806*7c478bd9Sstevel@tonic-gate 			/* skip over deleted IREs */
6807*7c478bd9Sstevel@tonic-gate 			continue;
6808*7c478bd9Sstevel@tonic-gate 		}
6809*7c478bd9Sstevel@tonic-gate 		return (curr);
6810*7c478bd9Sstevel@tonic-gate 	}
6811*7c478bd9Sstevel@tonic-gate 	return (NULL);
6812*7c478bd9Sstevel@tonic-gate }
6813*7c478bd9Sstevel@tonic-gate 
6814*7c478bd9Sstevel@tonic-gate /*
6815*7c478bd9Sstevel@tonic-gate  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
6816*7c478bd9Sstevel@tonic-gate  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
6817*7c478bd9Sstevel@tonic-gate  * in the bucket skipping default interface routes and deleted entries.
6818*7c478bd9Sstevel@tonic-gate  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
6819*7c478bd9Sstevel@tonic-gate  * Assumes that the caller holds a reference on the IRE bucket.
6820*7c478bd9Sstevel@tonic-gate  */
6821*7c478bd9Sstevel@tonic-gate ire_t *
6822*7c478bd9Sstevel@tonic-gate ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
6823*7c478bd9Sstevel@tonic-gate {
6824*7c478bd9Sstevel@tonic-gate 	ASSERT(ire_origin->ire_bucket != NULL);
6825*7c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
6826*7c478bd9Sstevel@tonic-gate 
6827*7c478bd9Sstevel@tonic-gate 	do {
6828*7c478bd9Sstevel@tonic-gate 		ire = ire->ire_next;
6829*7c478bd9Sstevel@tonic-gate 		if (ire == NULL)
6830*7c478bd9Sstevel@tonic-gate 			ire = ire_origin->ire_bucket->irb_ire;
6831*7c478bd9Sstevel@tonic-gate 		if (ire == ire_origin)
6832*7c478bd9Sstevel@tonic-gate 			return (NULL);
6833*7c478bd9Sstevel@tonic-gate 	} while ((ire->ire_type & IRE_INTERFACE) ||
6834*7c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
6835*7c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
6836*7c478bd9Sstevel@tonic-gate 	return (ire);
6837*7c478bd9Sstevel@tonic-gate }
6838*7c478bd9Sstevel@tonic-gate 
6839*7c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
6840*7c478bd9Sstevel@tonic-gate th_trace_t *
6841*7c478bd9Sstevel@tonic-gate th_trace_ire_lookup(ire_t *ire)
6842*7c478bd9Sstevel@tonic-gate {
6843*7c478bd9Sstevel@tonic-gate 	int bucket_id;
6844*7c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
6845*7c478bd9Sstevel@tonic-gate 
6846*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ire->ire_lock));
6847*7c478bd9Sstevel@tonic-gate 
6848*7c478bd9Sstevel@tonic-gate 	bucket_id = IP_TR_HASH(curthread);
6849*7c478bd9Sstevel@tonic-gate 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6850*7c478bd9Sstevel@tonic-gate 
6851*7c478bd9Sstevel@tonic-gate 	for (th_trace = ire->ire_trace[bucket_id]; th_trace != NULL;
6852*7c478bd9Sstevel@tonic-gate 	    th_trace = th_trace->th_next) {
6853*7c478bd9Sstevel@tonic-gate 		if (th_trace->th_id == curthread)
6854*7c478bd9Sstevel@tonic-gate 			return (th_trace);
6855*7c478bd9Sstevel@tonic-gate 	}
6856*7c478bd9Sstevel@tonic-gate 	return (NULL);
6857*7c478bd9Sstevel@tonic-gate }
6858*7c478bd9Sstevel@tonic-gate 
6859*7c478bd9Sstevel@tonic-gate void
6860*7c478bd9Sstevel@tonic-gate ire_trace_ref(ire_t *ire)
6861*7c478bd9Sstevel@tonic-gate {
6862*7c478bd9Sstevel@tonic-gate 	int bucket_id;
6863*7c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
6864*7c478bd9Sstevel@tonic-gate 
6865*7c478bd9Sstevel@tonic-gate 	/*
6866*7c478bd9Sstevel@tonic-gate 	 * Attempt to locate the trace buffer for the curthread.
6867*7c478bd9Sstevel@tonic-gate 	 * If it does not exist, then allocate a new trace buffer
6868*7c478bd9Sstevel@tonic-gate 	 * and link it in list of trace bufs for this ipif, at the head
6869*7c478bd9Sstevel@tonic-gate 	 */
6870*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
6871*7c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
6872*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
6873*7c478bd9Sstevel@tonic-gate 		return;
6874*7c478bd9Sstevel@tonic-gate 	}
6875*7c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
6876*7c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
6877*7c478bd9Sstevel@tonic-gate 		bucket_id = IP_TR_HASH(curthread);
6878*7c478bd9Sstevel@tonic-gate 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6879*7c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
6880*7c478bd9Sstevel@tonic-gate 		if (th_trace == NULL) {
6881*7c478bd9Sstevel@tonic-gate 			ire->ire_trace_disable = B_TRUE;
6882*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
6883*7c478bd9Sstevel@tonic-gate 			ire_trace_inactive(ire);
6884*7c478bd9Sstevel@tonic-gate 			return;
6885*7c478bd9Sstevel@tonic-gate 		}
6886*7c478bd9Sstevel@tonic-gate 
6887*7c478bd9Sstevel@tonic-gate 		th_trace->th_id = curthread;
6888*7c478bd9Sstevel@tonic-gate 		th_trace->th_next = ire->ire_trace[bucket_id];
6889*7c478bd9Sstevel@tonic-gate 		th_trace->th_prev = &ire->ire_trace[bucket_id];
6890*7c478bd9Sstevel@tonic-gate 		if (th_trace->th_next != NULL)
6891*7c478bd9Sstevel@tonic-gate 			th_trace->th_next->th_prev = &th_trace->th_next;
6892*7c478bd9Sstevel@tonic-gate 		ire->ire_trace[bucket_id] = th_trace;
6893*7c478bd9Sstevel@tonic-gate 	}
6894*7c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
6895*7c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt++;
6896*7c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
6897*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
6898*7c478bd9Sstevel@tonic-gate }
6899*7c478bd9Sstevel@tonic-gate 
6900*7c478bd9Sstevel@tonic-gate void
6901*7c478bd9Sstevel@tonic-gate ire_trace_free(th_trace_t *th_trace)
6902*7c478bd9Sstevel@tonic-gate {
6903*7c478bd9Sstevel@tonic-gate 	/* unlink th_trace and free it */
6904*7c478bd9Sstevel@tonic-gate 	*th_trace->th_prev = th_trace->th_next;
6905*7c478bd9Sstevel@tonic-gate 	if (th_trace->th_next != NULL)
6906*7c478bd9Sstevel@tonic-gate 		th_trace->th_next->th_prev = th_trace->th_prev;
6907*7c478bd9Sstevel@tonic-gate 	th_trace->th_next = NULL;
6908*7c478bd9Sstevel@tonic-gate 	th_trace->th_prev = NULL;
6909*7c478bd9Sstevel@tonic-gate 	kmem_free(th_trace, sizeof (th_trace_t));
6910*7c478bd9Sstevel@tonic-gate }
6911*7c478bd9Sstevel@tonic-gate 
6912*7c478bd9Sstevel@tonic-gate void
6913*7c478bd9Sstevel@tonic-gate ire_untrace_ref(ire_t *ire)
6914*7c478bd9Sstevel@tonic-gate {
6915*7c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
6916*7c478bd9Sstevel@tonic-gate 
6917*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
6918*7c478bd9Sstevel@tonic-gate 
6919*7c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
6920*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
6921*7c478bd9Sstevel@tonic-gate 		return;
6922*7c478bd9Sstevel@tonic-gate 	}
6923*7c478bd9Sstevel@tonic-gate 
6924*7c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
6925*7c478bd9Sstevel@tonic-gate 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
6926*7c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
6927*7c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt--;
6928*7c478bd9Sstevel@tonic-gate 
6929*7c478bd9Sstevel@tonic-gate 	if (th_trace->th_refcnt == 0)
6930*7c478bd9Sstevel@tonic-gate 		ire_trace_free(th_trace);
6931*7c478bd9Sstevel@tonic-gate 
6932*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
6933*7c478bd9Sstevel@tonic-gate }
6934*7c478bd9Sstevel@tonic-gate 
6935*7c478bd9Sstevel@tonic-gate static void
6936*7c478bd9Sstevel@tonic-gate ire_trace_inactive(ire_t *ire)
6937*7c478bd9Sstevel@tonic-gate {
6938*7c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
6939*7c478bd9Sstevel@tonic-gate 	int i;
6940*7c478bd9Sstevel@tonic-gate 
6941*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
6942*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6943*7c478bd9Sstevel@tonic-gate 		while (ire->ire_trace[i] != NULL) {
6944*7c478bd9Sstevel@tonic-gate 			th_trace = ire->ire_trace[i];
6945*7c478bd9Sstevel@tonic-gate 
6946*7c478bd9Sstevel@tonic-gate 			/* unlink th_trace and free it */
6947*7c478bd9Sstevel@tonic-gate 			ire->ire_trace[i] = th_trace->th_next;
6948*7c478bd9Sstevel@tonic-gate 			if (th_trace->th_next != NULL)
6949*7c478bd9Sstevel@tonic-gate 				th_trace->th_next->th_prev =
6950*7c478bd9Sstevel@tonic-gate 				    &ire->ire_trace[i];
6951*7c478bd9Sstevel@tonic-gate 
6952*7c478bd9Sstevel@tonic-gate 			th_trace->th_next = NULL;
6953*7c478bd9Sstevel@tonic-gate 			th_trace->th_prev = NULL;
6954*7c478bd9Sstevel@tonic-gate 			kmem_free(th_trace, sizeof (th_trace_t));
6955*7c478bd9Sstevel@tonic-gate 		}
6956*7c478bd9Sstevel@tonic-gate 	}
6957*7c478bd9Sstevel@tonic-gate 
6958*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
6959*7c478bd9Sstevel@tonic-gate }
6960*7c478bd9Sstevel@tonic-gate 
6961*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
6962*7c478bd9Sstevel@tonic-gate void
6963*7c478bd9Sstevel@tonic-gate ire_thread_exit(ire_t *ire, caddr_t arg)
6964*7c478bd9Sstevel@tonic-gate {
6965*7c478bd9Sstevel@tonic-gate 	th_trace_t	*th_trace;
6966*7c478bd9Sstevel@tonic-gate 
6967*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
6968*7c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
6969*7c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
6970*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
6971*7c478bd9Sstevel@tonic-gate 		return;
6972*7c478bd9Sstevel@tonic-gate 	}
6973*7c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt == 0);
6974*7c478bd9Sstevel@tonic-gate 
6975*7c478bd9Sstevel@tonic-gate 	ire_trace_free(th_trace);
6976*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
6977*7c478bd9Sstevel@tonic-gate }
6978*7c478bd9Sstevel@tonic-gate 
6979*7c478bd9Sstevel@tonic-gate #endif
6980