xref: /titanic_41/usr/src/uts/common/inet/ip/ip_ire.c (revision cc52cd5b16ec3b8334f7db84aa4d148f1e4391d3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 /*
31  * This file contains routines that manipulate Internet Routing Entries (IREs).
32  */
33 
34 #include <sys/types.h>
35 #include <sys/stream.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/ddi.h>
39 #include <sys/cmn_err.h>
40 #include <sys/policy.h>
41 
42 #include <sys/systm.h>
43 #include <sys/kmem.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <net/if_dl.h>
50 #include <netinet/ip6.h>
51 #include <netinet/icmp6.h>
52 
53 #include <inet/common.h>
54 #include <inet/mi.h>
55 #include <inet/ip.h>
56 #include <inet/ip6.h>
57 #include <inet/ip_ndp.h>
58 #include <inet/arp.h>
59 #include <inet/ip_if.h>
60 #include <inet/ip_ire.h>
61 #include <inet/ip_ftable.h>
62 #include <inet/ip_rts.h>
63 #include <inet/nd.h>
64 
65 #include <net/pfkeyv2.h>
66 #include <inet/ipsec_info.h>
67 #include <inet/sadb.h>
68 #include <sys/kmem.h>
69 #include <inet/tcp.h>
70 #include <inet/ipclassifier.h>
71 #include <sys/zone.h>
72 #include <sys/cpuvar.h>
73 
74 #include <sys/tsol/label.h>
75 #include <sys/tsol/tnet.h>
76 
77 struct kmem_cache *rt_entry_cache;
78 
79 
80 /*
81  * Synchronization notes:
82  *
83  * The fields of the ire_t struct are protected in the following way :
84  *
85  * ire_next/ire_ptpn
86  *
87  *	- bucket lock of the respective tables (cache or forwarding tables).
88  *
89  * ire_mp, ire_rfq, ire_stq, ire_u *except* ire_gateway_addr[v6], ire_mask,
90  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, ire_ipif,
91  * ire_ihandle, ire_phandle, ire_nce, ire_bucket, ire_in_ill, ire_in_src_addr
92  *
93  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
94  *	  we don't need a lock whenever these fields are accessed.
95  *
96  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
97  *        ire_add_v4/ire_add_v6 before inserting in the bucket and never
98  *        changes after that. Thus we don't need a lock whenever these
99  *	  fields are accessed.
100  *
101  * ire_gateway_addr_v4[v6]
102  *
103  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
104  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
105  *	  it assumed to be atomic and hence the other parts of the code
106  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
107  *	  and hence any access to it uses ire_lock to get/set the right value.
108  *
109  * ire_ident, ire_refcnt
110  *
111  *	- Updated atomically using atomic_add_32
112  *
113  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
114  *
115  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
116  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
117  *
118  * ire_max_frag, ire_frag_flag
119  *
120  *	- ire_lock is used to set/read both of them together.
121  *
122  * ire_tire_mark
123  *
124  *	- Set in ire_create and updated in ire_expire, which is called
125  *	  by only one function namely ip_trash_timer_expire. Thus only
126  *	  one function updates and examines the value.
127  *
128  * ire_marks
129  *	- bucket lock protects this.
130  *
131  * ire_ipsec_overhead/ire_ll_hdr_length
132  *
133  *	- Place holder for returning the information to the upper layers
134  *	  when IRE_DB_REQ comes down.
135  *
136  *
137  * ipv6_ire_default_count is protected by the bucket lock of
138  * ip_forwarding_table_v6[0][0].
139  *
140  * ipv6_ire_default_index is not protected as it  is just a hint
141  * at which default gateway to use. There is nothing
142  * wrong in using the same gateway for two different connections.
143  *
144  * As we always hold the bucket locks in all the places while accessing
145  * the above values, it is natural to use them for protecting them.
146  *
147  * We have a separate cache table and forwarding table for IPv4 and IPv6.
148  * Cache table (ip_cache_table/ip_cache_table_v6) is a pointer to an
149  * array of irb_t structure and forwarding table (ip_forwarding_table/
150  * ip_forwarding_table_v6) is an array of pointers to array of irb_t
151  * structure. ip_forwarding_table_v6 is allocated dynamically in
152  * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
153  * initializing the same bucket. Once a bucket is initialized, it is never
154  * de-alloacted. This assumption enables us to access
155  * ip_forwarding_table_v6[i] without any locks.
156  *
157  * Each irb_t - ire bucket structure has a lock to protect
158  * a bucket and the ires residing in the bucket have a back pointer to
159  * the bucket structure. It also has a reference count for the number
160  * of threads walking the bucket - irb_refcnt which is bumped up
161  * using the macro IRB_REFHOLD macro. The flags irb_flags can be
162  * set to IRE_MARK_CONDEMNED indicating that there are some ires
163  * in this bucket that are marked with IRE_MARK_CONDEMNED and the
164  * last thread to leave the bucket should delete the ires. Usually
165  * this is done by the IRB_REFRELE macro which is used to decrement
166  * the reference count on a bucket.
167  *
168  * IRE_REFHOLD/IRE_REFRELE macros operate on the ire which increments/
169  * decrements the reference count, ire_refcnt, atomically on the ire.
170  * ire_refcnt is modified only using this macro. Operations on the IRE
171  * could be described as follows :
172  *
173  * CREATE an ire with reference count initialized to 1.
174  *
175  * ADDITION of an ire holds the bucket lock, checks for duplicates
176  * and then adds the ire. ire_add_v4/ire_add_v6 returns the ire after
177  * bumping up once more i.e the reference count is 2. This is to avoid
178  * an extra lookup in the functions calling ire_add which wants to
179  * work with the ire after adding.
180  *
181  * LOOKUP of an ire bumps up the reference count using IRE_REFHOLD
182  * macro. It is valid to bump up the referece count of the IRE,
183  * after the lookup has returned an ire. Following are the lookup
184  * functions that return an HELD ire :
185  *
186  * ire_lookup_local[_v6], ire_ctable_lookup[_v6], ire_ftable_lookup[_v6],
187  * ire_cache_lookup[_v6], ire_lookup_multi[_v6], ire_route_lookup[_v6],
188  * ipif_to_ire[_v6], ire_mrtun_lookup, ire_srcif_table_lookup.
189  *
190  * DELETION of an ire holds the bucket lock, removes it from the list
191  * and then decrements the reference count for having removed from the list
192  * by using the IRE_REFRELE macro. If some other thread has looked up
193  * the ire, the reference count would have been bumped up and hence
194  * this ire will not be freed once deleted. It will be freed once the
195  * reference count drops to zero.
196  *
197  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
198  * lookups acquire the bucket lock as RW_READER.
199  *
200  * NOTE : The only functions that does the IRE_REFRELE when an ire is
201  *	  passed as an argument are :
202  *
203  *	  1) ip_wput_ire : This is because it IRE_REFHOLD/RELEs the
204  *			   broadcast ires it looks up internally within
205  *			   the function. Currently, for simplicity it does
206  *			   not differentiate the one that is passed in and
207  *			   the ones it looks up internally. It always
208  *			   IRE_REFRELEs.
209  *	  2) ire_send
210  *	     ire_send_v6 : As ire_send calls ip_wput_ire and other functions
211  *			   that take ire as an argument, it has to selectively
212  *			   IRE_REFRELE the ire. To maintain symmetry,
213  *			   ire_send_v6 does the same.
214  *
215  * Otherwise, the general rule is to do the IRE_REFRELE in the function
216  * that is passing the ire as an argument.
217  *
218  * In trying to locate ires the following points are to be noted.
219  *
220  * IRE_MARK_CONDEMNED signifies that the ire has been logically deleted and is
221  * to be ignored when walking the ires using ire_next.
222  *
223  * IRE_MARK_HIDDEN signifies that the ire is a special ire typically for the
224  * benefit of in.mpathd which needs to probe interfaces for failures. Normal
225  * applications should not be seeing this ire and hence this ire is ignored
226  * in most cases in the search using ire_next.
227  *
228  * Zones note:
229  *	Walking IREs within a given zone also walks certain ires in other
230  *	zones.  This is done intentionally.  IRE walks with a specified
231  *	zoneid are used only when doing informational reports, and
232  *	zone users want to see things that they can access. See block
233  *	comment in ire_walk_ill_match().
234  */
235 
236 /*
237  * A per-interface routing table is created ( if not present)
238  * when the first entry is added to this special routing table.
239  * This special routing table is accessed through the ill data structure.
240  * The routing table looks like cache table. For example, currently it
241  * is used by mobile-ip foreign agent to forward data that only comes from
242  * the home agent tunnel for a mobile node. Thus if the outgoing interface
243  * is a RESOLVER interface, IP may need to resolve the hardware address for
244  * the outgoing interface. The routing entries in this table are not updated
245  * in IRE_CACHE. When MCTL msg comes back from ARP, the incoming ill informa-
246  * tion is lost as the write queue is passed to ip_wput.
247  * But, before sending the packet out, the hardware information must be updated
248  * in the special forwarding table. ire_srcif_table_count keeps track of total
249  * number of ires that are in interface based tables. Each interface based
250  * table hangs off of the incoming ill and each ill_t also keeps a refcnt
251  * of ires in that table.
252  */
253 
254 /*
255  * The minimum size of IRE cache table.  It will be recalcuated in
256  * ip_ire_init().
257  * Setable in /etc/system
258  */
259 uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE;
260 uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE;
261 
262 /*
263  * The size of the forwarding table.  We will make sure that it is a
264  * power of 2 in ip_ire_init().
265  * Setable in /etc/system
266  */
267 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
268 
269 struct	kmem_cache	*ire_cache;
270 static ire_t	ire_null;
271 
272 /*
273  * The threshold number of IRE in a bucket when the IREs are
274  * cleaned up.  This threshold is calculated later in ip_open()
275  * based on the speed of CPU and available memory.  This default
276  * value is the maximum.
277  *
278  * We have two kinds of cached IRE, temporary and
279  * non-temporary.  Temporary IREs are marked with
280  * IRE_MARK_TEMPORARY.  They are IREs created for non
281  * TCP traffic and for forwarding purposes.  All others
282  * are non-temporary IREs.  We don't mark IRE created for
283  * TCP as temporary because TCP is stateful and there are
284  * info stored in the IRE which can be shared by other TCP
285  * connections to the same destination.  For connected
286  * endpoint, we also don't want to mark the IRE used as
287  * temporary because the same IRE will be used frequently,
288  * otherwise, the app should not do a connect().  We change
289  * the marking at ip_bind_connected_*() if necessary.
290  *
291  * We want to keep the cache IRE hash bucket length reasonably
292  * short, otherwise IRE lookup functions will take "forever."
293  * We use the "crude" function that the IRE bucket
294  * length should be based on the CPU speed, which is 1 entry
295  * per x MHz, depending on the shift factor ip_ire_cpu_ratio
296  * (n).  This means that with a 750MHz CPU, the max bucket
297  * length can be (750 >> n) entries.
298  *
299  * Note that this threshold is separate for temp and non-temp
300  * IREs.  This means that the actual bucket length can be
301  * twice as that.  And while we try to keep temporary IRE
302  * length at most at the threshold value, we do not attempt to
303  * make the length for non-temporary IREs fixed, for the
304  * reason stated above.  Instead, we start trying to find
305  * "unused" non-temporary IREs when the bucket length reaches
306  * this threshold and clean them up.
307  *
308  * We also want to limit the amount of memory used by
309  * IREs.  So if we are allowed to use ~3% of memory (M)
310  * for those IREs, each bucket should not have more than
311  *
312  * 	M / num of cache bucket / sizeof (ire_t)
313  *
314  * Again the above memory uses are separate for temp and
315  * non-temp cached IREs.
316  *
317  * We may also want the limit to be a function of the number
318  * of interfaces and number of CPUs.  Doing the initialization
319  * in ip_open() means that every time an interface is plumbed,
320  * the max is re-calculated.  Right now, we don't do anything
321  * different.  In future, when we have more experience, we
322  * may want to change this behavior.
323  */
324 uint32_t ip_ire_max_bucket_cnt = 10;	/* Setable in /etc/system */
325 uint32_t ip6_ire_max_bucket_cnt = 10;
326 
327 /*
328  * The minimum of the temporary IRE bucket count.  We do not want
329  * the length of each bucket to be too short.  This may hurt
330  * performance of some apps as the temporary IREs are removed too
331  * often.
332  */
333 uint32_t ip_ire_min_bucket_cnt = 3;	/* /etc/system - not used */
334 uint32_t ip6_ire_min_bucket_cnt = 3;
335 
336 /*
337  * The ratio of memory consumed by IRE used for temporary to available
338  * memory.  This is a shift factor, so 6 means the ratio 1 to 64.  This
339  * value can be changed in /etc/system.  6 is a reasonable number.
340  */
341 uint32_t ip_ire_mem_ratio = 6;	/* /etc/system */
342 /* The shift factor for CPU speed to calculate the max IRE bucket length. */
343 uint32_t ip_ire_cpu_ratio = 7;	/* /etc/system */
344 
345 typedef struct nce_clookup_s {
346 	ipaddr_t ncecl_addr;
347 	boolean_t ncecl_found;
348 } nce_clookup_t;
349 
350 /*
351  * The maximum number of buckets in IRE cache table.  In future, we may
352  * want to make it a dynamic hash table.  For the moment, we fix the
353  * size and allocate the table in ip_ire_init() when IP is first loaded.
354  * We take into account the amount of memory a system has.
355  */
356 #define	IP_MAX_CACHE_TABLE_SIZE	4096
357 
358 /* Setable in /etc/system */
359 static uint32_t	ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
360 static uint32_t	ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
361 
362 #define	NUM_ILLS	3	/* To build the ILL list to unlock */
363 
364 /* Zero iulp_t for initialization. */
365 const iulp_t	ire_uinfo_null = { 0 };
366 
367 static int	ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
368     ipsq_func_t func, boolean_t);
369 static int	ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
370     ipsq_func_t func);
371 static ire_t	*ire_update_srcif_v4(ire_t *ire);
372 static void	ire_delete_v4(ire_t *ire);
373 static void	ire_report_ctable(ire_t *ire, char *mp);
374 static void	ire_report_mrtun_table(ire_t *ire, char *mp);
375 static void	ire_report_srcif_table(ire_t *ire, char *mp, ip_stack_t *ipst);
376 static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
377     zoneid_t zoneid, ip_stack_t *);
378 static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
379     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
380 static void	ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt);
381 extern void	ill_unlock_ills(ill_t **list, int cnt);
382 static	void	ip_nce_clookup_and_delete(nce_t *nce, void *arg);
383 extern void	th_trace_rrecord(th_trace_t *);
384 #ifdef IRE_DEBUG
385 static void	ire_trace_inactive(ire_t *);
386 #endif
387 
388 /*
389  * To avoid bloating the code, we call this function instead of
390  * using the macro IRE_REFRELE. Use macro only in performance
391  * critical paths.
392  *
393  * Must not be called while holding any locks. Otherwise if this is
394  * the last reference to be released there is a chance of recursive mutex
395  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
396  * to restart an ioctl. The one exception is when the caller is sure that
397  * this is not the last reference to be released. Eg. if the caller is
398  * sure that the ire has not been deleted and won't be deleted.
399  */
400 void
401 ire_refrele(ire_t *ire)
402 {
403 	IRE_REFRELE(ire);
404 }
405 
406 void
407 ire_refrele_notr(ire_t *ire)
408 {
409 	IRE_REFRELE_NOTR(ire);
410 }
411 
412 /*
413  * kmem_cache_alloc constructor for IRE in kma space.
414  * Note that when ire_mp is set the IRE is stored in that mblk and
415  * not in this cache.
416  */
417 /* ARGSUSED */
418 static int
419 ip_ire_constructor(void *buf, void *cdrarg, int kmflags)
420 {
421 	ire_t	*ire = buf;
422 
423 	ire->ire_nce = NULL;
424 
425 	return (0);
426 }
427 
428 /* ARGSUSED1 */
429 static void
430 ip_ire_destructor(void *buf, void *cdrarg)
431 {
432 	ire_t	*ire = buf;
433 
434 	ASSERT(ire->ire_nce == NULL);
435 }
436 
437 /*
438  * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY
439  * IOCTL.  It is used by TCP (or other ULPs) to supply revised information
440  * for an existing CACHED IRE.
441  */
442 /* ARGSUSED */
443 int
444 ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
445 {
446 	uchar_t	*addr_ucp;
447 	ipic_t	*ipic;
448 	ire_t	*ire;
449 	ipaddr_t	addr;
450 	in6_addr_t	v6addr;
451 	irb_t	*irb;
452 	zoneid_t	zoneid;
453 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
454 
455 	ASSERT(q->q_next == NULL);
456 	zoneid = Q_TO_CONN(q)->conn_zoneid;
457 
458 	/*
459 	 * Check privilege using the ioctl credential; if it is NULL
460 	 * then this is a kernel message and therefor privileged.
461 	 */
462 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
463 		return (EPERM);
464 
465 	ipic = (ipic_t *)mp->b_rptr;
466 	if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset,
467 	    ipic->ipic_addr_length))) {
468 		return (EINVAL);
469 	}
470 	if (!OK_32PTR(addr_ucp))
471 		return (EINVAL);
472 	switch (ipic->ipic_addr_length) {
473 	case IP_ADDR_LEN: {
474 		/* Extract the destination address. */
475 		addr = *(ipaddr_t *)addr_ucp;
476 		/* Find the corresponding IRE. */
477 		ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
478 		break;
479 	}
480 	case IPV6_ADDR_LEN: {
481 		/* Extract the destination address. */
482 		v6addr = *(in6_addr_t *)addr_ucp;
483 		/* Find the corresponding IRE. */
484 		ire = ire_cache_lookup_v6(&v6addr, zoneid, NULL, ipst);
485 		break;
486 	}
487 	default:
488 		return (EINVAL);
489 	}
490 
491 	if (ire == NULL)
492 		return (ENOENT);
493 	/*
494 	 * Update the round trip time estimate and/or the max frag size
495 	 * and/or the slow start threshold.
496 	 *
497 	 * We serialize multiple advises using ire_lock.
498 	 */
499 	mutex_enter(&ire->ire_lock);
500 	if (ipic->ipic_rtt) {
501 		/*
502 		 * If there is no old cached values, initialize them
503 		 * conservatively.  Set them to be (1.5 * new value).
504 		 */
505 		if (ire->ire_uinfo.iulp_rtt != 0) {
506 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
507 			    ipic->ipic_rtt) >> 1;
508 		} else {
509 			ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt +
510 			    (ipic->ipic_rtt >> 1);
511 		}
512 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
513 			ire->ire_uinfo.iulp_rtt_sd =
514 			    (ire->ire_uinfo.iulp_rtt_sd +
515 			    ipic->ipic_rtt_sd) >> 1;
516 		} else {
517 			ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd +
518 			    (ipic->ipic_rtt_sd >> 1);
519 		}
520 	}
521 	if (ipic->ipic_max_frag)
522 		ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET);
523 	if (ipic->ipic_ssthresh != 0) {
524 		if (ire->ire_uinfo.iulp_ssthresh != 0)
525 			ire->ire_uinfo.iulp_ssthresh =
526 			    (ipic->ipic_ssthresh +
527 			    ire->ire_uinfo.iulp_ssthresh) >> 1;
528 		else
529 			ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh;
530 	}
531 	/*
532 	 * Don't need the ire_lock below this. ire_type does not change
533 	 * after initialization. ire_marks is protected by irb_lock.
534 	 */
535 	mutex_exit(&ire->ire_lock);
536 
537 	if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) {
538 		/*
539 		 * Only increment the temporary IRE count if the original
540 		 * IRE is not already marked temporary.
541 		 */
542 		irb = ire->ire_bucket;
543 		rw_enter(&irb->irb_lock, RW_WRITER);
544 		if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) &&
545 		    !(ire->ire_marks & IRE_MARK_TEMPORARY)) {
546 			irb->irb_tmp_ire_cnt++;
547 		}
548 		ire->ire_marks |= ipic->ipic_ire_marks;
549 		rw_exit(&irb->irb_lock);
550 	}
551 
552 	ire_refrele(ire);
553 	return (0);
554 }
555 
556 /*
557  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
558  * IOCTL[s].  The NO_REPLY form is used by TCP to delete a route IRE
559  * for a host that is not responding.  This will force an attempt to
560  * establish a new route, if available, and flush out the ARP entry so
561  * it will re-resolve.  Management processes may want to use the
562  * version that generates a reply.
563  *
564  * This function does not support IPv6 since Neighbor Unreachability Detection
565  * means that negative advise like this is useless.
566  */
567 /* ARGSUSED */
568 int
569 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
570 {
571 	uchar_t		*addr_ucp;
572 	ipaddr_t	addr;
573 	ire_t		*ire;
574 	ipid_t		*ipid;
575 	boolean_t	routing_sock_info = B_FALSE;	/* Sent info? */
576 	zoneid_t	zoneid;
577 	ire_t		*gire = NULL;
578 	ill_t		*ill;
579 	mblk_t		*arp_mp;
580 	ip_stack_t	*ipst;
581 
582 	ASSERT(q->q_next == NULL);
583 	zoneid = Q_TO_CONN(q)->conn_zoneid;
584 	ipst = CONNQ_TO_IPST(q);
585 
586 	/*
587 	 * Check privilege using the ioctl credential; if it is NULL
588 	 * then this is a kernel message and therefor privileged.
589 	 */
590 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
591 		return (EPERM);
592 
593 	ipid = (ipid_t *)mp->b_rptr;
594 
595 	/* Only actions on IRE_CACHEs are acceptable at present. */
596 	if (ipid->ipid_ire_type != IRE_CACHE)
597 		return (EINVAL);
598 
599 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
600 		ipid->ipid_addr_length);
601 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
602 		return (EINVAL);
603 	switch (ipid->ipid_addr_length) {
604 	case IP_ADDR_LEN:
605 		/* addr_ucp points at IP addr */
606 		break;
607 	case sizeof (sin_t): {
608 		sin_t	*sin;
609 		/*
610 		 * got complete (sockaddr) address - increment addr_ucp to point
611 		 * at the ip_addr field.
612 		 */
613 		sin = (sin_t *)addr_ucp;
614 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
615 		break;
616 	}
617 	default:
618 		return (EINVAL);
619 	}
620 	/* Extract the destination address. */
621 	bcopy(addr_ucp, &addr, IP_ADDR_LEN);
622 
623 	/* Try to find the CACHED IRE. */
624 	ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
625 
626 	/* Nail it. */
627 	if (ire) {
628 		/* Allow delete only on CACHE entries */
629 		if (ire->ire_type != IRE_CACHE) {
630 			ire_refrele(ire);
631 			return (EINVAL);
632 		}
633 
634 		/*
635 		 * Verify that the IRE has been around for a while.
636 		 * This is to protect against transport protocols
637 		 * that are too eager in sending delete messages.
638 		 */
639 		if (gethrestime_sec() <
640 		    ire->ire_create_time + ipst->ips_ip_ignore_delete_time) {
641 			ire_refrele(ire);
642 			return (EINVAL);
643 		}
644 		/*
645 		 * Now we have a potentially dead cache entry. We need
646 		 * to remove it.
647 		 * If this cache entry is generated from a
648 		 * default route (i.e., ire_cmask == 0),
649 		 * search the default list and mark it dead and some
650 		 * background process will try to activate it.
651 		 */
652 		if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) {
653 			/*
654 			 * Make sure that we pick a different
655 			 * IRE_DEFAULT next time.
656 			 */
657 			ire_t *gw_ire;
658 			irb_t *irb = NULL;
659 			uint_t match_flags;
660 
661 			match_flags = (MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE);
662 
663 			gire = ire_ftable_lookup(ire->ire_addr,
664 			    ire->ire_cmask, 0, 0,
665 			    ire->ire_ipif, NULL, zoneid, 0, NULL, match_flags,
666 			    ipst);
667 
668 			ip3dbg(("ire_ftable_lookup() returned gire %p\n",
669 			    (void *)gire));
670 
671 			if (gire != NULL) {
672 				irb = gire->ire_bucket;
673 
674 				/*
675 				 * We grab it as writer just to serialize
676 				 * multiple threads trying to bump up
677 				 * irb_rr_origin
678 				 */
679 				rw_enter(&irb->irb_lock, RW_WRITER);
680 				if ((gw_ire = irb->irb_rr_origin) == NULL) {
681 					rw_exit(&irb->irb_lock);
682 					goto done;
683 				}
684 
685 				DTRACE_PROBE1(ip__ire__del__origin,
686 				    (ire_t *), gw_ire);
687 
688 				/* Skip past the potentially bad gateway */
689 				if (ire->ire_gateway_addr ==
690 				    gw_ire->ire_gateway_addr) {
691 					ire_t *next = gw_ire->ire_next;
692 
693 					DTRACE_PROBE2(ip__ire__del,
694 					    (ire_t *), gw_ire, (irb_t *), irb);
695 					IRE_FIND_NEXT_ORIGIN(next);
696 					irb->irb_rr_origin = next;
697 				}
698 				rw_exit(&irb->irb_lock);
699 			}
700 		}
701 done:
702 		if (gire != NULL)
703 			IRE_REFRELE(gire);
704 		/* report the bad route to routing sockets */
705 		ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr,
706 		    ire->ire_mask, ire->ire_src_addr, 0, 0, 0,
707 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA), ipst);
708 		routing_sock_info = B_TRUE;
709 
710 		/*
711 		 * TCP is really telling us to start over completely, and it
712 		 * expects that we'll resend the ARP query.  Tell ARP to
713 		 * discard the entry, if this is a local destination.
714 		 */
715 		ill = ire->ire_stq->q_ptr;
716 		if (ire->ire_gateway_addr == 0 &&
717 		    (arp_mp = ill_ared_alloc(ill, addr)) != NULL) {
718 			putnext(ill->ill_rq, arp_mp);
719 		}
720 
721 		ire_delete(ire);
722 		ire_refrele(ire);
723 	}
724 	/*
725 	 * Also look for an IRE_HOST type redirect ire and
726 	 * remove it if present.
727 	 */
728 	ire = ire_route_lookup(addr, 0, 0, IRE_HOST, NULL, NULL,
729 	    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
730 
731 	/* Nail it. */
732 	if (ire != NULL) {
733 	    if (ire->ire_flags & RTF_DYNAMIC) {
734 		if (!routing_sock_info) {
735 			ip_rts_change(RTM_LOSING, ire->ire_addr,
736 			    ire->ire_gateway_addr, ire->ire_mask,
737 			    ire->ire_src_addr, 0, 0, 0,
738 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
739 			    ipst);
740 		}
741 		ire_delete(ire);
742 	    }
743 	    ire_refrele(ire);
744 	}
745 	return (0);
746 }
747 
748 /*
749  * Named Dispatch routine to produce a formatted report on all IREs.
750  * This report is accessed by using the ndd utility to "get" ND variable
751  * "ipv4_ire_status".
752  */
753 /* ARGSUSED */
754 int
755 ip_ire_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
756 {
757 	zoneid_t zoneid;
758 	ip_stack_t	*ipst;
759 
760 	if (CONN_Q(q))
761 		ipst = CONNQ_TO_IPST(q);
762 	else
763 		ipst = ILLQ_TO_IPST(q);
764 
765 	(void) mi_mpprintf(mp,
766 	    "IRE      " MI_COL_HDRPAD_STR
767 	/*   01234567[89ABCDEF] */
768 	    "rfq      " MI_COL_HDRPAD_STR
769 	/*   01234567[89ABCDEF] */
770 	    "stq      " MI_COL_HDRPAD_STR
771 	/*   01234567[89ABCDEF] */
772 	    " zone "
773 	/*   12345 */
774 	    "addr            mask            "
775 	/*   123.123.123.123 123.123.123.123 */
776 	    "src             gateway         mxfrg rtt   rtt_sd ssthresh ref "
777 	/*   123.123.123.123 123.123.123.123 12345 12345 123456 12345678 123 */
778 	    "rtomax tstamp_ok wscale_ok ecn_ok pmtud_ok sack sendpipe "
779 	/*   123456 123456789 123456789 123456 12345678 1234 12345678 */
780 	    "recvpipe in/out/forward type");
781 	/*   12345678 in/out/forward xxxxxxxxxx */
782 
783 	/*
784 	 * Because of the ndd constraint, at most we can have 64K buffer
785 	 * to put in all IRE info.  So to be more efficient, just
786 	 * allocate a 64K buffer here, assuming we need that large buffer.
787 	 * This should be OK as only root can do ndd /dev/ip.
788 	 */
789 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
790 		/* The following may work even if we cannot get a large buf. */
791 		(void) mi_mpprintf(mp, "<< Out of buffer >>\n");
792 		return (0);
793 	}
794 
795 	zoneid = Q_TO_CONN(q)->conn_zoneid;
796 	if (zoneid == GLOBAL_ZONEID)
797 		zoneid = ALL_ZONES;
798 
799 	ire_walk_v4(ire_report_ftable, mp->b_cont, zoneid, ipst);
800 	ire_walk_v4(ire_report_ctable, mp->b_cont, zoneid, ipst);
801 
802 	return (0);
803 }
804 
805 
806 /* ire_walk routine invoked for ip_ire_report for each cached IRE. */
807 static void
808 ire_report_ctable(ire_t *ire, char *mp)
809 {
810 	char	buf1[16];
811 	char	buf2[16];
812 	char	buf3[16];
813 	char	buf4[16];
814 	uint_t	fo_pkt_count;
815 	uint_t	ib_pkt_count;
816 	int	ref;
817 	uint_t	print_len, buf_len;
818 
819 	if ((ire->ire_type & IRE_CACHETABLE) == 0)
820 	    return;
821 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
822 	if (buf_len <= 0)
823 		return;
824 
825 	/* Number of active references of this ire */
826 	ref = ire->ire_refcnt;
827 	/* "inbound" to a non local address is a forward */
828 	ib_pkt_count = ire->ire_ib_pkt_count;
829 	fo_pkt_count = 0;
830 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
831 		fo_pkt_count = ib_pkt_count;
832 		ib_pkt_count = 0;
833 	}
834 	print_len =  snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
835 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
836 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
837 	    "%04d %08d %08d %d/%d/%d %s\n",
838 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
839 	    (int)ire->ire_zoneid,
840 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
841 	    ip_dot_addr(ire->ire_src_addr, buf3),
842 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
843 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
844 	    ire->ire_uinfo.iulp_rtt_sd, ire->ire_uinfo.iulp_ssthresh, ref,
845 	    ire->ire_uinfo.iulp_rtomax,
846 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
847 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
848 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
849 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
850 	    ire->ire_uinfo.iulp_sack,
851 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
852 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
853 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
854 	if (print_len < buf_len) {
855 		((mblk_t *)mp)->b_wptr += print_len;
856 	} else {
857 		((mblk_t *)mp)->b_wptr += buf_len;
858 	}
859 }
860 
861 /* ARGSUSED */
862 int
863 ip_ire_report_mrtun(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
864 {
865 	ip_stack_t	*ipst;
866 
867 	if (CONN_Q(q))
868 		ipst = CONNQ_TO_IPST(q);
869 	else
870 		ipst = ILLQ_TO_IPST(q);
871 
872 	(void) mi_mpprintf(mp,
873 	"IRE      " MI_COL_HDRPAD_STR
874 	/*   01234567[89ABCDEF] */
875 	"stq      " MI_COL_HDRPAD_STR
876 	/*   01234567[89ABCDEF] */
877 	"in_ill    " MI_COL_HDRPAD_STR
878 	/*   01234567[89ABCDEF] */
879 	"in_src_addr            "
880 	/*   123.123.123.123 */
881 	"max_frag      "
882 	/*   12345 */
883 	"ref     ");
884 	/*   123 */
885 
886 	ire_walk_ill_mrtun(0, 0, ire_report_mrtun_table, mp, NULL,
887 	    ipst);
888 	return (0);
889 }
890 
891 /* mrtun report table - supports ipv4_mrtun_ire_status ndd variable */
892 
893 static void
894 ire_report_mrtun_table(ire_t *ire, char *mp)
895 {
896 	char	buf1[INET_ADDRSTRLEN];
897 	int	ref;
898 
899 	/* Number of active references of this ire */
900 	ref = ire->ire_refcnt;
901 	ASSERT(ire->ire_type == IRE_MIPRTUN);
902 	(void) mi_mpprintf((mblk_t *)mp,
903 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
904 	    "%s          %05d             %03d",
905 	    (void *)ire, (void *)ire->ire_stq,
906 	    (void *)ire->ire_in_ill,
907 	    ip_dot_addr(ire->ire_in_src_addr, buf1),
908 	    ire->ire_max_frag, ref);
909 }
910 
911 /*
912  * Dispatch routine to format ires in interface based routine
913  */
914 /* ARGSUSED */
915 int
916 ip_ire_report_srcif(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
917 {
918 	ip_stack_t	*ipst;
919 
920 	if (CONN_Q(q))
921 		ipst = CONNQ_TO_IPST(q);
922 	else
923 		ipst = ILLQ_TO_IPST(q);
924 
925 	/* Report all interface based ires */
926 
927 	(void) mi_mpprintf(mp,
928 	    "IRE      " MI_COL_HDRPAD_STR
929 	    /*   01234567[89ABCDEF] */
930 	    "stq      " MI_COL_HDRPAD_STR
931 	    /*   01234567[89ABCDEF] */
932 	    "in_ill    " MI_COL_HDRPAD_STR
933 	    /*   01234567[89ABCDEF] */
934 	    "addr            "
935 	    /*   123.123.123.123 */
936 	    "gateway         "
937 	    /*   123.123.123.123 */
938 	    "max_frag      "
939 	    /*   12345 */
940 	    "ref     "
941 	    /*   123 */
942 	    "type    "
943 	    /* ABCDEFGH */
944 	    "in/out/forward");
945 	ire_walk_srcif_table_v4(ire_report_srcif_table, mp, ipst);
946 	return (0);
947 }
948 
949 /* Reports the interface table ires */
950 /* ARGSUSED2 */
951 static void
952 ire_report_srcif_table(ire_t *ire, char *mp, ip_stack_t *ipst)
953 {
954 	char    buf1[INET_ADDRSTRLEN];
955 	char    buf2[INET_ADDRSTRLEN];
956 	int	ref;
957 
958 	ref = ire->ire_refcnt;
959 	(void) mi_mpprintf((mblk_t *)mp,
960 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
961 	    "%s    %s      %05d       %03d      %s     %d",
962 	    (void *)ire, (void *)ire->ire_stq,
963 	    (void *)ire->ire_in_ill,
964 	    ip_dot_addr(ire->ire_addr, buf1),
965 	    ip_dot_addr(ire->ire_gateway_addr, buf2),
966 	    ire->ire_max_frag, ref,
967 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type),
968 	    ire->ire_ib_pkt_count);
969 
970 }
971 /*
972  * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed
973  * down from the Upper Level Protocol to request a copy of the IRE (to check
974  * its type or to extract information like round-trip time estimates or the
975  * MTU.)
976  * The address is assumed to be in the ire_addr field. If no IRE is found
977  * an IRE is returned with ire_type being zero.
978  * Note that the upper lavel protocol has to check for broadcast
979  * (IRE_BROADCAST) and multicast (CLASSD(addr)).
980  * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the
981  * end of the returned message.
982  *
983  * TCP sends down a message of this type with a connection request packet
984  * chained on. UDP and ICMP send it down to verify that a route exists for
985  * the destination address when they get connected.
986  */
987 void
988 ip_ire_req(queue_t *q, mblk_t *mp)
989 {
990 	ire_t	*inire;
991 	ire_t	*ire;
992 	mblk_t	*mp1;
993 	ire_t	*sire = NULL;
994 	zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid;
995 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
996 
997 	ASSERT(q->q_next == NULL);
998 
999 	if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) ||
1000 	    !OK_32PTR(mp->b_rptr)) {
1001 		freemsg(mp);
1002 		return;
1003 	}
1004 	inire = (ire_t *)mp->b_rptr;
1005 	/*
1006 	 * Got it, now take our best shot at an IRE.
1007 	 */
1008 	if (inire->ire_ipversion == IPV6_VERSION) {
1009 		ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0,
1010 		    NULL, &sire, zoneid, NULL,
1011 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
1012 	} else {
1013 		ASSERT(inire->ire_ipversion == IPV4_VERSION);
1014 		ire = ire_route_lookup(inire->ire_addr, 0, 0, 0,
1015 		    NULL, &sire, zoneid, NULL,
1016 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
1017 	}
1018 
1019 	/*
1020 	 * We prevent returning IRES with source address INADDR_ANY
1021 	 * as these were temporarily created for sending packets
1022 	 * from endpoints that have conn_unspec_src set.
1023 	 */
1024 	if (ire == NULL ||
1025 	    (ire->ire_ipversion == IPV4_VERSION &&
1026 	    ire->ire_src_addr == INADDR_ANY) ||
1027 	    (ire->ire_ipversion == IPV6_VERSION &&
1028 	    IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) {
1029 		inire->ire_type = 0;
1030 	} else {
1031 		bcopy(ire, inire, sizeof (ire_t));
1032 		/* Copy the route metrics from the parent. */
1033 		if (sire != NULL) {
1034 			bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo),
1035 			    sizeof (iulp_t));
1036 		}
1037 
1038 		/*
1039 		 * As we don't lookup global policy here, we may not
1040 		 * pass the right size if per-socket policy is not
1041 		 * present. For these cases, path mtu discovery will
1042 		 * do the right thing.
1043 		 */
1044 		inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q));
1045 
1046 		/* Pass the latest setting of the ip_path_mtu_discovery */
1047 		inire->ire_frag_flag |=
1048 		    (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
1049 	}
1050 	if (ire != NULL)
1051 		ire_refrele(ire);
1052 	if (sire != NULL)
1053 		ire_refrele(sire);
1054 	mp->b_wptr = &mp->b_rptr[sizeof (ire_t)];
1055 	mp->b_datap->db_type = IRE_DB_TYPE;
1056 
1057 	/* Put the IRE_DB_TYPE mblk last in the chain */
1058 	mp1 = mp->b_cont;
1059 	if (mp1 != NULL) {
1060 		mp->b_cont = NULL;
1061 		linkb(mp1, mp);
1062 		mp = mp1;
1063 	}
1064 	qreply(q, mp);
1065 }
1066 
1067 /*
1068  * Send a packet using the specified IRE.
1069  * If ire_src_addr_v6 is all zero then discard the IRE after
1070  * the packet has been sent.
1071  */
1072 static void
1073 ire_send(queue_t *q, mblk_t *pkt, ire_t *ire)
1074 {
1075 	mblk_t *ipsec_mp;
1076 	boolean_t is_secure;
1077 	uint_t ifindex;
1078 	ill_t	*ill;
1079 	zoneid_t zoneid = ire->ire_zoneid;
1080 	ip_stack_t	*ipst = ire->ire_ipst;
1081 
1082 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1083 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
1084 	ipsec_mp = pkt;
1085 	is_secure = (pkt->b_datap->db_type == M_CTL);
1086 	if (is_secure) {
1087 		ipsec_out_t *io;
1088 
1089 		pkt = pkt->b_cont;
1090 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
1091 		if (io->ipsec_out_type == IPSEC_OUT)
1092 			zoneid = io->ipsec_out_zoneid;
1093 	}
1094 
1095 	/* If the packet originated externally then */
1096 	if (pkt->b_prev) {
1097 		ire_refrele(ire);
1098 		/*
1099 		 * Extract the ifindex from b_prev (set in ip_rput_noire).
1100 		 * Look up interface to see if it still exists (it could have
1101 		 * been unplumbed by the time the reply came back from ARP)
1102 		 */
1103 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
1104 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
1105 		    NULL, NULL, NULL, NULL, ipst);
1106 		if (ill == NULL) {
1107 			pkt->b_prev = NULL;
1108 			pkt->b_next = NULL;
1109 			freemsg(ipsec_mp);
1110 			return;
1111 		}
1112 		q = ill->ill_rq;
1113 		pkt->b_prev = NULL;
1114 		/*
1115 		 * This packet has not gone through IPSEC processing
1116 		 * and hence we should not have any IPSEC message
1117 		 * prepended.
1118 		 */
1119 		ASSERT(ipsec_mp == pkt);
1120 		put(q, pkt);
1121 		ill_refrele(ill);
1122 	} else if (pkt->b_next) {
1123 		/* Packets from multicast router */
1124 		pkt->b_next = NULL;
1125 		/*
1126 		 * We never get the IPSEC_OUT while forwarding the
1127 		 * packet for multicast router.
1128 		 */
1129 		ASSERT(ipsec_mp == pkt);
1130 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL);
1131 		ire_refrele(ire);
1132 	} else {
1133 		/* Locally originated packets */
1134 		boolean_t is_inaddr_any;
1135 		ipha_t *ipha = (ipha_t *)pkt->b_rptr;
1136 
1137 		/*
1138 		 * We need to do an ire_delete below for which
1139 		 * we need to make sure that the IRE will be
1140 		 * around even after calling ip_wput_ire -
1141 		 * which does ire_refrele. Otherwise somebody
1142 		 * could potentially delete this ire and hence
1143 		 * free this ire and we will be calling ire_delete
1144 		 * on a freed ire below.
1145 		 */
1146 		is_inaddr_any = (ire->ire_src_addr == INADDR_ANY);
1147 		if (is_inaddr_any) {
1148 			IRE_REFHOLD(ire);
1149 		}
1150 		/*
1151 		 * If we were resolving a router we can not use the
1152 		 * routers IRE for sending the packet (since it would
1153 		 * violate the uniqness of the IP idents) thus we
1154 		 * make another pass through ip_wput to create the IRE_CACHE
1155 		 * for the destination.
1156 		 * When IRE_MARK_NOADD is set, ire_add() is not called.
1157 		 * Thus ip_wput() will never find a ire and result in an
1158 		 * infinite loop. Thus we check whether IRE_MARK_NOADD is
1159 		 * is set. This also implies that IRE_MARK_NOADD can only be
1160 		 * used to send packets to directly connected hosts.
1161 		 */
1162 		if (ipha->ipha_dst != ire->ire_addr &&
1163 		    !(ire->ire_marks & IRE_MARK_NOADD)) {
1164 			ire_refrele(ire);	/* Held in ire_add */
1165 			if (CONN_Q(q)) {
1166 				(void) ip_output(Q_TO_CONN(q), ipsec_mp, q,
1167 				    IRE_SEND);
1168 			} else {
1169 				(void) ip_output((void *)(uintptr_t)zoneid,
1170 				    ipsec_mp, q, IRE_SEND);
1171 			}
1172 		} else {
1173 			if (is_secure) {
1174 				ipsec_out_t *oi;
1175 				ipha_t *ipha;
1176 
1177 				oi = (ipsec_out_t *)ipsec_mp->b_rptr;
1178 				ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
1179 				if (oi->ipsec_out_proc_begin) {
1180 					/*
1181 					 * This is the case where
1182 					 * ip_wput_ipsec_out could not find
1183 					 * the IRE and recreated a new one.
1184 					 * As ip_wput_ipsec_out does ire
1185 					 * lookups, ire_refrele for the extra
1186 					 * bump in ire_add.
1187 					 */
1188 					ire_refrele(ire);
1189 					ip_wput_ipsec_out(q, ipsec_mp, ipha,
1190 					    NULL, NULL);
1191 				} else {
1192 					/*
1193 					 * IRE_REFRELE will be done in
1194 					 * ip_wput_ire.
1195 					 */
1196 					ip_wput_ire(q, ipsec_mp, ire, NULL,
1197 					    IRE_SEND, zoneid);
1198 				}
1199 			} else {
1200 				/*
1201 				 * IRE_REFRELE will be done in ip_wput_ire.
1202 				 */
1203 				ip_wput_ire(q, ipsec_mp, ire, NULL,
1204 				    IRE_SEND, zoneid);
1205 			}
1206 		}
1207 		/*
1208 		 * Special code to support sending a single packet with
1209 		 * conn_unspec_src using an IRE which has no source address.
1210 		 * The IRE is deleted here after sending the packet to avoid
1211 		 * having other code trip on it. But before we delete the
1212 		 * ire, somebody could have looked up this ire.
1213 		 * We prevent returning/using this IRE by the upper layers
1214 		 * by making checks to NULL source address in other places
1215 		 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected.
1216 		 * Though, this does not completely prevent other threads
1217 		 * from using this ire, this should not cause any problems.
1218 		 *
1219 		 * NOTE : We use is_inaddr_any instead of using ire_src_addr
1220 		 * because for the normal case i.e !is_inaddr_any, ire_refrele
1221 		 * above could have potentially freed the ire.
1222 		 */
1223 		if (is_inaddr_any) {
1224 			/*
1225 			 * If this IRE has been deleted by another thread, then
1226 			 * ire_bucket won't be NULL, but ire_ptpn will be NULL.
1227 			 * Thus, ire_delete will do nothing.  This check
1228 			 * guards against calling ire_delete when the IRE was
1229 			 * never inserted in the table, which is handled by
1230 			 * ire_delete as dropping another reference.
1231 			 */
1232 			if (ire->ire_bucket != NULL) {
1233 				ip1dbg(("ire_send: delete IRE\n"));
1234 				ire_delete(ire);
1235 			}
1236 			ire_refrele(ire);	/* Held above */
1237 		}
1238 	}
1239 }
1240 
1241 /*
1242  * Send a packet using the specified IRE.
1243  * If ire_src_addr_v6 is all zero then discard the IRE after
1244  * the packet has been sent.
1245  */
1246 static void
1247 ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire)
1248 {
1249 	mblk_t *ipsec_mp;
1250 	boolean_t secure;
1251 	uint_t ifindex;
1252 	zoneid_t zoneid = ire->ire_zoneid;
1253 	ip_stack_t	*ipst = ire->ire_ipst;
1254 
1255 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
1256 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
1257 	if (pkt->b_datap->db_type == M_CTL) {
1258 		ipsec_out_t *io;
1259 
1260 		ipsec_mp = pkt;
1261 		pkt = pkt->b_cont;
1262 		secure = B_TRUE;
1263 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
1264 		if (io->ipsec_out_type == IPSEC_OUT)
1265 			zoneid = io->ipsec_out_zoneid;
1266 	} else {
1267 		ipsec_mp = pkt;
1268 		secure = B_FALSE;
1269 	}
1270 
1271 	/* If the packet originated externally then */
1272 	if (pkt->b_prev) {
1273 		ill_t	*ill;
1274 		/*
1275 		 * Extract the ifindex from b_prev (set in ip_rput_data_v6).
1276 		 * Look up interface to see if it still exists (it could have
1277 		 * been unplumbed by the time the reply came back from the
1278 		 * resolver).
1279 		 */
1280 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
1281 		ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
1282 		    NULL, NULL, NULL, NULL, ipst);
1283 		if (ill == NULL) {
1284 			pkt->b_prev = NULL;
1285 			pkt->b_next = NULL;
1286 			freemsg(ipsec_mp);
1287 			ire_refrele(ire);	/* Held in ire_add */
1288 			return;
1289 		}
1290 		q = ill->ill_rq;
1291 		pkt->b_prev = NULL;
1292 		/*
1293 		 * This packet has not gone through IPSEC processing
1294 		 * and hence we should not have any IPSEC message
1295 		 * prepended.
1296 		 */
1297 		ASSERT(ipsec_mp == pkt);
1298 		put(q, pkt);
1299 		ill_refrele(ill);
1300 	} else if (pkt->b_next) {
1301 		/* Packets from multicast router */
1302 		pkt->b_next = NULL;
1303 		/*
1304 		 * We never get the IPSEC_OUT while forwarding the
1305 		 * packet for multicast router.
1306 		 */
1307 		ASSERT(ipsec_mp == pkt);
1308 		/*
1309 		 * XXX TODO IPv6.
1310 		 */
1311 		freemsg(pkt);
1312 #ifdef XXX
1313 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL);
1314 #endif
1315 	} else {
1316 		if (secure) {
1317 			ipsec_out_t *oi;
1318 			ip6_t *ip6h;
1319 
1320 			oi = (ipsec_out_t *)ipsec_mp->b_rptr;
1321 			ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr;
1322 			if (oi->ipsec_out_proc_begin) {
1323 				/*
1324 				 * This is the case where
1325 				 * ip_wput_ipsec_out could not find
1326 				 * the IRE and recreated a new one.
1327 				 */
1328 				ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h,
1329 				    NULL, NULL);
1330 			} else {
1331 				if (CONN_Q(q)) {
1332 					(void) ip_output_v6(Q_TO_CONN(q),
1333 					    ipsec_mp, q, IRE_SEND);
1334 				} else {
1335 					(void) ip_output_v6(
1336 					    (void *)(uintptr_t)zoneid,
1337 					    ipsec_mp, q, IRE_SEND);
1338 				}
1339 			}
1340 		} else {
1341 			/*
1342 			 * Send packets through ip_output_v6 so that any
1343 			 * ip6_info header can be processed again.
1344 			 */
1345 			if (CONN_Q(q)) {
1346 				(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q,
1347 				    IRE_SEND);
1348 			} else {
1349 				(void) ip_output_v6((void *)(uintptr_t)zoneid,
1350 				    ipsec_mp, q, IRE_SEND);
1351 			}
1352 		}
1353 		/*
1354 		 * Special code to support sending a single packet with
1355 		 * conn_unspec_src using an IRE which has no source address.
1356 		 * The IRE is deleted here after sending the packet to avoid
1357 		 * having other code trip on it. But before we delete the
1358 		 * ire, somebody could have looked up this ire.
1359 		 * We prevent returning/using this IRE by the upper layers
1360 		 * by making checks to NULL source address in other places
1361 		 * like e.g ip_ire_append_v6, ip_ire_req and
1362 		 * ip_bind_connected_v6. Though, this does not completely
1363 		 * prevent other threads from using this ire, this should
1364 		 * not cause any problems.
1365 		 */
1366 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) {
1367 			ip1dbg(("ire_send_v6: delete IRE\n"));
1368 			ire_delete(ire);
1369 		}
1370 	}
1371 	ire_refrele(ire);	/* Held in ire_add */
1372 }
1373 
1374 /*
1375  * Make sure that IRE bucket does not get too long.
1376  * This can cause lock up because ire_cache_lookup()
1377  * may take "forever" to finish.
1378  *
1379  * We just remove cnt IREs each time.  This means that
1380  * the bucket length will stay approximately constant,
1381  * depending on cnt.  This should be enough to defend
1382  * against DoS attack based on creating temporary IREs
1383  * (for forwarding and non-TCP traffic).
1384  *
1385  * Note that new IRE is normally added at the tail of the
1386  * bucket.  This means that we are removing the "oldest"
1387  * temporary IRE added.  Only if there are IREs with
1388  * the same ire_addr, do we not add it at the tail.  Refer
1389  * to ire_add_v*().  It should be OK for our purpose.
1390  *
1391  * For non-temporary cached IREs, we make sure that they
1392  * have not been used for some time (defined below), they
1393  * are non-local destinations, and there is no one using
1394  * them at the moment (refcnt == 1).
1395  *
1396  * The above means that the IRE bucket length may become
1397  * very long, consisting of mostly non-temporary IREs.
1398  * This can happen when the hash function does a bad job
1399  * so that most TCP connections cluster to a specific bucket.
1400  * This "hopefully" should never happen.  It can also
1401  * happen if most TCP connections have very long lives.
1402  * Even with the minimal hash table size of 256, there
1403  * has to be a lot of such connections to make the bucket
1404  * length unreasonably long.  This should probably not
1405  * happen either.  The third can when this can happen is
1406  * when the machine is under attack, such as SYN flooding.
1407  * TCP should already have the proper mechanism to protect
1408  * that.  So we should be safe.
1409  *
1410  * This function is called by ire_add_then_send() after
1411  * a new IRE is added and the packet is sent.
1412  *
1413  * The idle cutoff interval is set to 60s.  It can be
1414  * changed using /etc/system.
1415  */
1416 uint32_t ire_idle_cutoff_interval = 60000;
1417 
1418 static void
1419 ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt)
1420 {
1421 	ire_t *ire;
1422 	int tmp_cnt = cnt;
1423 	clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000);
1424 
1425 	/*
1426 	 * irb is NULL if the IRE is not added to the hash.  This
1427 	 * happens when IRE_MARK_NOADD is set in ire_add_then_send()
1428 	 * and when ires are returned from ire_update_srcif_v4() routine.
1429 	 */
1430 	if (irb == NULL)
1431 		return;
1432 
1433 	IRB_REFHOLD(irb);
1434 	if (irb->irb_tmp_ire_cnt > threshold) {
1435 		for (ire = irb->irb_ire; ire != NULL && tmp_cnt > 0;
1436 		    ire = ire->ire_next) {
1437 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
1438 				continue;
1439 			if (ire->ire_marks & IRE_MARK_TEMPORARY) {
1440 				ASSERT(ire->ire_type == IRE_CACHE);
1441 				ire_delete(ire);
1442 				tmp_cnt--;
1443 			}
1444 		}
1445 	}
1446 	if (irb->irb_ire_cnt - irb->irb_tmp_ire_cnt > threshold) {
1447 		for (ire = irb->irb_ire; ire != NULL && cnt > 0;
1448 		    ire = ire->ire_next) {
1449 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
1450 				continue;
1451 			if (ire->ire_ipversion == IPV4_VERSION) {
1452 				if (ire->ire_gateway_addr == 0)
1453 					continue;
1454 			} else {
1455 				if (IN6_IS_ADDR_UNSPECIFIED(
1456 				    &ire->ire_gateway_addr_v6))
1457 					continue;
1458 			}
1459 			if ((ire->ire_type == IRE_CACHE) &&
1460 			    (lbolt - ire->ire_last_used_time > cut_off) &&
1461 			    (ire->ire_refcnt == 1)) {
1462 				ire_delete(ire);
1463 				cnt--;
1464 			}
1465 		}
1466 	}
1467 	IRB_REFRELE(irb);
1468 }
1469 
1470 /*
1471  * ire_add_then_send is called when a new IRE has been created in order to
1472  * route an outgoing packet.  Typically, it is called from ip_wput when
1473  * a response comes back down from a resolver.  We add the IRE, and then
1474  * possibly run the packet through ip_wput or ip_rput, as appropriate.
1475  * However, we do not add the newly created IRE in the cache when
1476  * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at
1477  * ip_newroute_ipif(). The ires with IRE_MARK_NOADD and ires returned
1478  * by ire_update_srcif_v4() are ire_refrele'd by ip_wput_ire() and get
1479  * deleted.
1480  * Multirouting support: the packet is silently discarded when the new IRE
1481  * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the
1482  * RTF_MULTIRT flag for the same destination address.
1483  * In this case, we just want to register this additional ire without
1484  * sending the packet, as it has already been replicated through
1485  * existing multirt routes in ip_wput().
1486  */
1487 void
1488 ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp)
1489 {
1490 	irb_t *irb;
1491 	boolean_t drop = B_FALSE;
1492 	/* LINTED : set but not used in function */
1493 	boolean_t mctl_present;
1494 	mblk_t *first_mp = NULL;
1495 	mblk_t *save_mp = NULL;
1496 	ire_t *dst_ire;
1497 	ipha_t *ipha;
1498 	ip6_t *ip6h;
1499 	ip_stack_t	*ipst = ire->ire_ipst;
1500 
1501 	if (mp != NULL) {
1502 		/*
1503 		 * We first have to retrieve the destination address carried
1504 		 * by the packet.
1505 		 * We can't rely on ire as it can be related to a gateway.
1506 		 * The destination address will help in determining if
1507 		 * other RTF_MULTIRT ires are already registered.
1508 		 *
1509 		 * We first need to know where we are going : v4 or V6.
1510 		 * the ire version is enough, as there is no risk that
1511 		 * we resolve an IPv6 address with an IPv4 ire
1512 		 * or vice versa.
1513 		 */
1514 		if (ire->ire_ipversion == IPV4_VERSION) {
1515 			EXTRACT_PKT_MP(mp, first_mp, mctl_present);
1516 			ipha = (ipha_t *)mp->b_rptr;
1517 			save_mp = mp;
1518 			mp = first_mp;
1519 
1520 			dst_ire = ire_cache_lookup(ipha->ipha_dst,
1521 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
1522 		} else {
1523 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
1524 			/*
1525 			 * Get a pointer to the beginning of the IPv6 header.
1526 			 * Ignore leading IPsec control mblks.
1527 			 */
1528 			first_mp = mp;
1529 			if (mp->b_datap->db_type == M_CTL) {
1530 				mp = mp->b_cont;
1531 			}
1532 			ip6h = (ip6_t *)mp->b_rptr;
1533 			save_mp = mp;
1534 			mp = first_mp;
1535 			dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst,
1536 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
1537 		}
1538 		if (dst_ire != NULL) {
1539 			if (dst_ire->ire_flags & RTF_MULTIRT) {
1540 				/*
1541 				 * At least one resolved multirt route
1542 				 * already exists for the destination,
1543 				 * don't sent this packet: either drop it
1544 				 * or complete the pending resolution,
1545 				 * depending on the ire.
1546 				 */
1547 				drop = B_TRUE;
1548 			}
1549 			ip1dbg(("ire_add_then_send: dst_ire %p "
1550 			    "[dst %08x, gw %08x], drop %d\n",
1551 			    (void *)dst_ire,
1552 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
1553 				ntohl(dst_ire->ire_addr) : \
1554 				ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)),
1555 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
1556 				ntohl(dst_ire->ire_gateway_addr) : \
1557 				ntohl(V4_PART_OF_V6(
1558 				    dst_ire->ire_gateway_addr_v6)),
1559 			    drop));
1560 			ire_refrele(dst_ire);
1561 		}
1562 	}
1563 
1564 	if (!(ire->ire_marks & IRE_MARK_NOADD)) {
1565 		/*
1566 		 * Regular packets with cache bound ires and
1567 		 * the packets from ARP response for ires which
1568 		 * belong to the ire_srcif_v4 table, are here.
1569 		 */
1570 		if (ire->ire_in_ill == NULL) {
1571 			/* Add the ire */
1572 			(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
1573 		} else {
1574 			/*
1575 			 * This must be ARP response for ire in interface based
1576 			 * table. Note that we don't add them in cache table,
1577 			 * instead we update the existing table with dlureq_mp
1578 			 * information. The reverse tunnel ires do not come
1579 			 * here, as reverse tunnel is non-resolver interface.
1580 			 * XXX- another design alternative was to mark the
1581 			 * ires in interface based table with a special mark to
1582 			 * make absolutely sure that we operate in right ires.
1583 			 * This idea was not implemented as part of code review
1584 			 * suggestion, as ire_in_ill suffice to distinguish
1585 			 * between the regular ires and interface based
1586 			 * ires now and thus we save a bit in the ire_marks.
1587 			 */
1588 			ire = ire_update_srcif_v4(ire);
1589 		}
1590 
1591 		if (ire == NULL) {
1592 			mp->b_prev = NULL;
1593 			mp->b_next = NULL;
1594 			MULTIRT_DEBUG_UNTAG(mp);
1595 			freemsg(mp);
1596 			return;
1597 		}
1598 		if (mp == NULL) {
1599 			ire_refrele(ire);	/* Held in ire_add_v4/v6 */
1600 			return;
1601 		}
1602 	}
1603 	if (drop) {
1604 		/*
1605 		 * If we're adding an RTF_MULTIRT ire, the resolution
1606 		 * is over: we just drop the packet.
1607 		 */
1608 		if (ire->ire_flags & RTF_MULTIRT) {
1609 			if (save_mp) {
1610 				save_mp->b_prev = NULL;
1611 				save_mp->b_next = NULL;
1612 			}
1613 			MULTIRT_DEBUG_UNTAG(mp);
1614 			freemsg(mp);
1615 		} else {
1616 			/*
1617 			 * Otherwise, we're adding the ire to a gateway
1618 			 * for a multirt route.
1619 			 * Invoke ip_newroute() to complete the resolution
1620 			 * of the route. We will then come back here and
1621 			 * finally drop this packet in the above code.
1622 			 */
1623 			if (ire->ire_ipversion == IPV4_VERSION) {
1624 				/*
1625 				 * TODO: in order for CGTP to work in non-global
1626 				 * zones, ip_newroute() must create the IRE
1627 				 * cache in the zone indicated by
1628 				 * ire->ire_zoneid.
1629 				 */
1630 				ip_newroute(q, mp, ipha->ipha_dst, 0,
1631 				    (CONN_Q(q) ? Q_TO_CONN(q) : NULL),
1632 				    ire->ire_zoneid, ipst);
1633 			} else {
1634 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
1635 				ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL,
1636 				    NULL, ire->ire_zoneid, ipst);
1637 			}
1638 		}
1639 
1640 		ire_refrele(ire); /* As done by ire_send(). */
1641 		return;
1642 	}
1643 	/*
1644 	 * Need to remember ire_bucket here as ire_send*() may delete
1645 	 * the ire so we cannot reference it after that.
1646 	 */
1647 	irb = ire->ire_bucket;
1648 	if (ire->ire_ipversion == IPV6_VERSION) {
1649 		ire_send_v6(q, mp, ire);
1650 		/*
1651 		 * Clean up more than 1 IRE so that the clean up does not
1652 		 * need to be done every time when a new IRE is added and
1653 		 * the threshold is reached.
1654 		 */
1655 		ire_cache_cleanup(irb, ip6_ire_max_bucket_cnt, 2);
1656 	} else {
1657 		ire_send(q, mp, ire);
1658 		ire_cache_cleanup(irb, ip_ire_max_bucket_cnt, 2);
1659 	}
1660 }
1661 
1662 /*
1663  * Initialize the ire that is specific to IPv4 part and call
1664  * ire_init_common to finish it.
1665  */
1666 ire_t *
1667 ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr,
1668     uchar_t *gateway, uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp,
1669     queue_t *rfq, queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif,
1670     ill_t *in_ill, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle,
1671     uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1672     ip_stack_t *ipst)
1673 {
1674 	/*
1675 	 * Reject IRE security attribute creation/initialization
1676 	 * if system is not running in Trusted mode.
1677 	 */
1678 	if ((gc != NULL || gcgrp != NULL) && !is_system_labeled())
1679 		return (NULL);
1680 
1681 	if (fp_mp != NULL) {
1682 		/*
1683 		 * We can't dupb() here as multiple threads could be
1684 		 * calling dupb on the same mp which is incorrect.
1685 		 * First dupb() should be called only by one thread.
1686 		 */
1687 		fp_mp = copyb(fp_mp);
1688 		if (fp_mp == NULL)
1689 			return (NULL);
1690 	}
1691 
1692 	if (dlureq_mp != NULL) {
1693 		/*
1694 		 * We can't dupb() here as multiple threads could be
1695 		 * calling dupb on the same mp which is incorrect.
1696 		 * First dupb() should be called only by one thread.
1697 		 */
1698 		dlureq_mp = copyb(dlureq_mp);
1699 		if (dlureq_mp == NULL) {
1700 			if (fp_mp != NULL)
1701 				freeb(fp_mp);
1702 			return (NULL);
1703 		}
1704 	}
1705 
1706 	/*
1707 	 * Check that IRE_IF_RESOLVER and IRE_IF_NORESOLVER have a
1708 	 * dlureq_mp which is the ill_resolver_mp for IRE_IF_RESOLVER
1709 	 * and DL_UNITDATA_REQ for IRE_IF_NORESOLVER.
1710 	 */
1711 	if ((type & IRE_INTERFACE) &&
1712 	    dlureq_mp == NULL) {
1713 		ASSERT(fp_mp == NULL);
1714 		ip0dbg(("ire_init: no dlureq_mp\n"));
1715 		return (NULL);
1716 	}
1717 
1718 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
1719 
1720 	if (addr != NULL)
1721 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
1722 	if (src_addr != NULL)
1723 		bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN);
1724 	if (mask != NULL) {
1725 		bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
1726 		ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
1727 	}
1728 	if (gateway != NULL) {
1729 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
1730 	}
1731 	if (in_src_addr != NULL) {
1732 		bcopy(in_src_addr, &ire->ire_in_src_addr, IP_ADDR_LEN);
1733 	}
1734 
1735 	if (type == IRE_CACHE)
1736 		ire->ire_cmask = cmask;
1737 
1738 	/* ire_init_common will free the mblks upon encountering any failure */
1739 	if (!ire_init_common(ire, max_fragp, fp_mp, rfq, stq, type, dlureq_mp,
1740 	    ipif, in_ill, phandle, ihandle, flags, IPV4_VERSION, ulp_info,
1741 	    gc, gcgrp, ipst))
1742 		return (NULL);
1743 
1744 	return (ire);
1745 }
1746 
1747 /*
1748  * Similar to ire_create except that it is called only when
1749  * we want to allocate ire as an mblk e.g. we have an external
1750  * resolver ARP.
1751  */
1752 ire_t *
1753 ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1754     uchar_t *in_src_addr, uint_t max_frag, mblk_t *fp_mp, queue_t *rfq,
1755     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
1756     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1757     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1758     ip_stack_t *ipst)
1759 {
1760 	ire_t	*ire, *buf;
1761 	ire_t	*ret_ire;
1762 	mblk_t	*mp;
1763 	size_t	bufsize;
1764 	frtn_t	*frtnp;
1765 	ill_t	*ill;
1766 
1767 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
1768 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
1769 	if (buf == NULL) {
1770 		ip1dbg(("ire_create_mp: alloc failed\n"));
1771 		return (NULL);
1772 	}
1773 	frtnp = (frtn_t *)(buf + 1);
1774 	frtnp->free_arg = (caddr_t)buf;
1775 	frtnp->free_func = ire_freemblk;
1776 
1777 	/*
1778 	 * Allocate the new IRE. The ire created will hold a ref on
1779 	 * an nce_t after ire_nce_init, and this ref must either be
1780 	 * (a)  transferred to the ire_cache entry created when ire_add_v4
1781 	 *	is called after successful arp resolution, or,
1782 	 * (b)  released, when arp resolution fails
1783 	 * Case (b) is handled in ire_freemblk() which will be called
1784 	 * when mp is freed as a result of failed arp.
1785 	 */
1786 	mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
1787 	if (mp == NULL) {
1788 		ip1dbg(("ire_create_mp: alloc failed\n"));
1789 		kmem_free(buf, bufsize);
1790 		return (NULL);
1791 	}
1792 	ire = (ire_t *)mp->b_rptr;
1793 	mp->b_wptr = (uchar_t *)&ire[1];
1794 
1795 	/* Start clean. */
1796 	*ire = ire_null;
1797 	ire->ire_mp = mp;
1798 	mp->b_datap->db_type = IRE_DB_TYPE;
1799 	ire->ire_marks |= IRE_MARK_UNCACHED;
1800 
1801 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
1802 	    NULL, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill, cmask,
1803 	    phandle, ihandle, flags, ulp_info, gc, gcgrp, ipst);
1804 
1805 	ill = (ill_t *)(stq->q_ptr);
1806 	if (ret_ire == NULL) {
1807 		/* ire_freemblk needs these set */
1808 		ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
1809 		ire->ire_ipst = ipst;
1810 		freeb(ire->ire_mp);
1811 		return (NULL);
1812 	}
1813 	ret_ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
1814 	ASSERT(ret_ire == ire);
1815 	/*
1816 	 * ire_max_frag is normally zero here and is atomically set
1817 	 * under the irebucket lock in ire_add_v[46] except for the
1818 	 * case of IRE_MARK_NOADD. In that event the the ire_max_frag
1819 	 * is non-zero here.
1820 	 */
1821 	ire->ire_max_frag = max_frag;
1822 	return (ire);
1823 }
1824 
1825 /*
1826  * ire_create is called to allocate and initialize a new IRE.
1827  *
1828  * NOTE : This is called as writer sometimes though not required
1829  * by this function.
1830  */
1831 ire_t *
1832 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1833     uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp, queue_t *rfq,
1834     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
1835     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1836     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1837     ip_stack_t *ipst)
1838 {
1839 	ire_t	*ire;
1840 	ire_t	*ret_ire;
1841 
1842 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
1843 	if (ire == NULL) {
1844 		ip1dbg(("ire_create: alloc failed\n"));
1845 		return (NULL);
1846 	}
1847 	*ire = ire_null;
1848 
1849 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
1850 	    max_fragp, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill,  cmask,
1851 	    phandle, ihandle, flags, ulp_info, gc, gcgrp, ipst);
1852 
1853 	if (ret_ire == NULL) {
1854 		kmem_cache_free(ire_cache, ire);
1855 		return (NULL);
1856 	}
1857 	ASSERT(ret_ire == ire);
1858 	return (ire);
1859 }
1860 
1861 
1862 /*
1863  * Common to IPv4 and IPv6
1864  */
1865 boolean_t
1866 ire_init_common(ire_t *ire, uint_t *max_fragp, mblk_t *fp_mp,
1867     queue_t *rfq, queue_t *stq, ushort_t type,
1868     mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill, uint32_t phandle,
1869     uint32_t ihandle, uint32_t flags, uchar_t ipversion,
1870     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1871     ip_stack_t *ipst)
1872 {
1873 	ire->ire_max_fragp = max_fragp;
1874 	ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
1875 
1876 	ASSERT(fp_mp == NULL || fp_mp->b_datap->db_type == M_DATA);
1877 #ifdef DEBUG
1878 	if (ipif != NULL) {
1879 		if (ipif->ipif_isv6)
1880 			ASSERT(ipversion == IPV6_VERSION);
1881 		else
1882 			ASSERT(ipversion == IPV4_VERSION);
1883 	}
1884 #endif /* DEBUG */
1885 
1886 	/*
1887 	 * Create/initialize IRE security attribute only in Trusted mode;
1888 	 * if the passed in gc/gcgrp is non-NULL, we expect that the caller
1889 	 * has held a reference to it and will release it when this routine
1890 	 * returns a failure, otherwise we own the reference.  We do this
1891 	 * prior to initializing the rest IRE fields.
1892 	 *
1893 	 * Don't allocate ire_gw_secattr for the resolver case to prevent
1894 	 * memory leak (in case of external resolution failure). We'll
1895 	 * allocate it after a successful external resolution, in ire_add().
1896 	 * Note that ire->ire_mp != NULL here means this ire is headed
1897 	 * to an external resolver.
1898 	 */
1899 	if (is_system_labeled()) {
1900 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
1901 		    IRE_INTERFACE)) != 0) {
1902 			/* release references on behalf of caller */
1903 			if (gc != NULL)
1904 				GC_REFRELE(gc);
1905 			if (gcgrp != NULL)
1906 				GCGRP_REFRELE(gcgrp);
1907 		} else if ((ire->ire_mp == NULL) &&
1908 		    tsol_ire_init_gwattr(ire, ipversion, gc, gcgrp) != 0) {
1909 			/* free any caller-allocated mblks upon failure */
1910 			if (fp_mp != NULL)
1911 				freeb(fp_mp);
1912 			if (dlureq_mp != NULL)
1913 				freeb(dlureq_mp);
1914 			return (B_FALSE);
1915 		}
1916 	}
1917 
1918 	ire->ire_stq = stq;
1919 	ire->ire_rfq = rfq;
1920 	ire->ire_type = type;
1921 	ire->ire_flags = RTF_UP | flags;
1922 	ire->ire_ident = TICK_TO_MSEC(lbolt);
1923 	bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t));
1924 
1925 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
1926 	ire->ire_last_used_time = lbolt;
1927 	ire->ire_create_time = (uint32_t)gethrestime_sec();
1928 
1929 	/*
1930 	 * If this IRE is an IRE_CACHE, inherit the handles from the
1931 	 * parent IREs. For others in the forwarding table, assign appropriate
1932 	 * new ones.
1933 	 *
1934 	 * The mutex protecting ire_handle is because ire_create is not always
1935 	 * called as a writer.
1936 	 */
1937 	if (ire->ire_type & IRE_OFFSUBNET) {
1938 		mutex_enter(&ipst->ips_ire_handle_lock);
1939 		ire->ire_phandle = (uint32_t)ipst->ips_ire_handle++;
1940 		mutex_exit(&ipst->ips_ire_handle_lock);
1941 	} else if (ire->ire_type & IRE_INTERFACE) {
1942 		mutex_enter(&ipst->ips_ire_handle_lock);
1943 		ire->ire_ihandle = (uint32_t)ipst->ips_ire_handle++;
1944 		mutex_exit(&ipst->ips_ire_handle_lock);
1945 	} else if (ire->ire_type == IRE_CACHE) {
1946 		ire->ire_phandle = phandle;
1947 		ire->ire_ihandle = ihandle;
1948 	}
1949 	ire->ire_in_ill = in_ill;
1950 	ire->ire_ipif = ipif;
1951 	if (ipif != NULL) {
1952 		ire->ire_ipif_seqid = ipif->ipif_seqid;
1953 		ire->ire_zoneid = ipif->ipif_zoneid;
1954 	} else {
1955 		ire->ire_zoneid = GLOBAL_ZONEID;
1956 	}
1957 	ire->ire_ipversion = ipversion;
1958 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
1959 	if (ipversion == IPV4_VERSION) {
1960 		if (ire_nce_init(ire, fp_mp, dlureq_mp) != 0) {
1961 			/* some failure occurred. propagate error back */
1962 			return (B_FALSE);
1963 		}
1964 	} else {
1965 		ASSERT(ipversion == IPV6_VERSION);
1966 		/*
1967 		 * IPv6 initializes the ire_nce in ire_add_v6,
1968 		 * which expects to find the ire_nce to be null when
1969 		 * when it is called.
1970 		 */
1971 		if (dlureq_mp)
1972 			freemsg(dlureq_mp);
1973 		if (fp_mp)
1974 			freemsg(fp_mp);
1975 	}
1976 	ire->ire_refcnt = 1;
1977 	ire->ire_ipst = ipst;	/* No netstack_hold */
1978 
1979 #ifdef IRE_DEBUG
1980 	bzero(ire->ire_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
1981 #endif
1982 
1983 	return (B_TRUE);
1984 }
1985 
1986 /*
1987  * This routine is called repeatedly by ipif_up to create broadcast IREs.
1988  * It is passed a pointer to a slot in an IRE pointer array into which to
1989  * place the pointer to the new IRE, if indeed we create one.  If the
1990  * IRE corresponding to the address passed in would be a duplicate of an
1991  * existing one, we don't create the new one.  irep is incremented before
1992  * return only if we do create a new IRE.  (Always called as writer.)
1993  *
1994  * Note that with the "match_flags" parameter, we can match on either
1995  * a particular logical interface (MATCH_IRE_IPIF) or for all logical
1996  * interfaces for a given physical interface (MATCH_IRE_ILL).  Currently,
1997  * we only create broadcast ire's on a per physical interface basis. If
1998  * someone is going to be mucking with logical interfaces, it is important
1999  * to call "ipif_check_bcast_ires()" to make sure that any change to a
2000  * logical interface will not cause critical broadcast IRE's to be deleted.
2001  */
2002 ire_t **
2003 ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep,
2004     int match_flags)
2005 {
2006 	ire_t *ire;
2007 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
2008 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
2009 
2010 	/*
2011 	 * No broadcast IREs for the LOOPBACK interface
2012 	 * or others such as point to point and IPIF_NOXMIT.
2013 	 */
2014 	if (!(ipif->ipif_flags & IPIF_BROADCAST) ||
2015 	    (ipif->ipif_flags & IPIF_NOXMIT))
2016 		return (irep);
2017 
2018 	/* If this would be a duplicate, don't bother. */
2019 	if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif,
2020 	    ipif->ipif_zoneid, NULL, match_flags, ipst)) != NULL) {
2021 		/*
2022 		 * We look for non-deprecated (and non-anycast, non-nolocal)
2023 		 * ipifs as the best choice. ipifs with check_flags matching
2024 		 * (deprecated, etc) are used only if non-deprecated ipifs
2025 		 * are not available. if the existing ire's ipif is deprecated
2026 		 * and the new ipif is non-deprecated, switch to the new ipif
2027 		 */
2028 		if ((!(ire->ire_ipif->ipif_flags & check_flags)) ||
2029 		    (ipif->ipif_flags & check_flags)) {
2030 			ire_refrele(ire);
2031 			return (irep);
2032 		}
2033 		/*
2034 		 * Bcast ires exist in pairs. Both have to be deleted,
2035 		 * Since we are exclusive we can make the above assertion.
2036 		 * The 1st has to be refrele'd since it was ctable_lookup'd.
2037 		 */
2038 		ASSERT(IAM_WRITER_IPIF(ipif));
2039 		ASSERT(ire->ire_next->ire_addr == ire->ire_addr);
2040 		ire_delete(ire->ire_next);
2041 		ire_delete(ire);
2042 		ire_refrele(ire);
2043 	}
2044 
2045 	irep = ire_create_bcast(ipif, addr, irep);
2046 
2047 	return (irep);
2048 }
2049 
2050 uint_t ip_loopback_mtu = IP_LOOPBACK_MTU;
2051 
2052 /*
2053  * This routine is called from ipif_check_bcast_ires and ire_check_bcast.
2054  * It leaves all the verifying and deleting to those routines. So it always
2055  * creates 2 bcast ires and chains them into the ire array passed in.
2056  */
2057 ire_t **
2058 ire_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep)
2059 {
2060 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
2061 
2062 	*irep++ = ire_create(
2063 	    (uchar_t *)&addr,			/* dest addr */
2064 	    (uchar_t *)&ip_g_all_ones,		/* mask */
2065 	    (uchar_t *)&ipif->ipif_src_addr,	/* source addr */
2066 	    NULL,				/* no gateway */
2067 	    NULL,				/* no in_src_addr */
2068 	    &ipif->ipif_mtu,			/* max frag */
2069 	    NULL,				/* fast path header */
2070 	    ipif->ipif_rq,			/* recv-from queue */
2071 	    ipif->ipif_wq,			/* send-to queue */
2072 	    IRE_BROADCAST,
2073 	    ipif->ipif_bcast_mp,		/* xmit header */
2074 	    ipif,
2075 	    NULL,
2076 	    0,
2077 	    0,
2078 	    0,
2079 	    0,
2080 	    &ire_uinfo_null,
2081 	    NULL,
2082 	    NULL,
2083 	    ipst);
2084 
2085 	*irep++ = ire_create(
2086 		(uchar_t *)&addr,		 /* dest address */
2087 		(uchar_t *)&ip_g_all_ones,	 /* mask */
2088 		(uchar_t *)&ipif->ipif_src_addr, /* source address */
2089 		NULL,				 /* no gateway */
2090 		NULL,				 /* no in_src_addr */
2091 		&ip_loopback_mtu,		 /* max frag size */
2092 		NULL,				 /* Fast Path header */
2093 		ipif->ipif_rq,			 /* recv-from queue */
2094 		NULL,				 /* no send-to queue */
2095 		IRE_BROADCAST,		/* Needed for fanout in wput */
2096 		NULL,
2097 		ipif,
2098 		NULL,
2099 		0,
2100 		0,
2101 		0,
2102 		0,
2103 		&ire_uinfo_null,
2104 		NULL,
2105 		NULL,
2106 		ipst);
2107 
2108 	return (irep);
2109 }
2110 
2111 /*
2112  * ire_walk routine to delete or update any IRE_CACHE that might contain
2113  * stale information.
2114  * The flags state which entries to delete or update.
2115  * Garbage collection is done separately using kmem alloc callbacks to
2116  * ip_trash_ire_reclaim.
2117  * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME
2118  * since other stale information is cleaned up using NUD.
2119  */
2120 void
2121 ire_expire(ire_t *ire, char *arg)
2122 {
2123 	ire_expire_arg_t	*ieap = (ire_expire_arg_t *)(uintptr_t)arg;
2124 	ill_t			*stq_ill;
2125 	int			flush_flags = ieap->iea_flush_flag;
2126 	ip_stack_t		*ipst = ieap->iea_ipst;
2127 
2128 	if ((flush_flags & FLUSH_REDIRECT_TIME) &&
2129 	    (ire->ire_flags & RTF_DYNAMIC)) {
2130 		/* Make sure we delete the corresponding IRE_CACHE */
2131 		ip1dbg(("ire_expire: all redirects\n"));
2132 		ip_rts_rtmsg(RTM_DELETE, ire, 0, ipst);
2133 		ire_delete(ire);
2134 		atomic_dec_32(&ipst->ips_ip_redirect_cnt);
2135 		return;
2136 	}
2137 	if (ire->ire_type != IRE_CACHE)
2138 		return;
2139 
2140 	if (flush_flags & FLUSH_ARP_TIME) {
2141 		/*
2142 		 * Remove all IRE_CACHE.
2143 		 * Verify that create time is more than
2144 		 * ip_ire_arp_interval milliseconds ago.
2145 		 */
2146 		if (NCE_EXPIRED(ire->ire_nce, ipst)) {
2147 			ire_delete(ire);
2148 			return;
2149 		}
2150 	}
2151 
2152 	if (ipst->ips_ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) &&
2153 	    (ire->ire_ipif != NULL)) {
2154 		/* Increase pmtu if it is less than the interface mtu */
2155 		mutex_enter(&ire->ire_lock);
2156 		/*
2157 		 * If the ipif is a vni (whose mtu is 0, since it's virtual)
2158 		 * get the mtu from the sending interfaces' ipif
2159 		 */
2160 		if (IS_VNI(ire->ire_ipif->ipif_ill)) {
2161 			stq_ill = ire->ire_stq->q_ptr;
2162 			ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu,
2163 			    IP_MAXPACKET);
2164 		} else {
2165 			ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
2166 			    IP_MAXPACKET);
2167 		}
2168 		ire->ire_frag_flag |= IPH_DF;
2169 		mutex_exit(&ire->ire_lock);
2170 	}
2171 }
2172 
2173 /*
2174  * Return any local address.  We use this to target ourselves
2175  * when the src address was specified as 'default'.
2176  * Preference for IRE_LOCAL entries.
2177  */
2178 ire_t *
2179 ire_lookup_local(zoneid_t zoneid, ip_stack_t *ipst)
2180 {
2181 	ire_t	*ire;
2182 	irb_t	*irb;
2183 	ire_t	*maybe = NULL;
2184 	int i;
2185 
2186 	for (i = 0; i < ipst->ips_ip_cache_table_size;  i++) {
2187 		irb = &ipst->ips_ip_cache_table[i];
2188 		if (irb->irb_ire == NULL)
2189 			continue;
2190 		rw_enter(&irb->irb_lock, RW_READER);
2191 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2192 			if ((ire->ire_marks & IRE_MARK_CONDEMNED) ||
2193 			    (ire->ire_zoneid != zoneid &&
2194 			    ire->ire_zoneid != ALL_ZONES))
2195 				continue;
2196 			switch (ire->ire_type) {
2197 			case IRE_LOOPBACK:
2198 				if (maybe == NULL) {
2199 					IRE_REFHOLD(ire);
2200 					maybe = ire;
2201 				}
2202 				break;
2203 			case IRE_LOCAL:
2204 				if (maybe != NULL) {
2205 					ire_refrele(maybe);
2206 				}
2207 				IRE_REFHOLD(ire);
2208 				rw_exit(&irb->irb_lock);
2209 				return (ire);
2210 			}
2211 		}
2212 		rw_exit(&irb->irb_lock);
2213 	}
2214 	return (maybe);
2215 }
2216 
2217 /*
2218  * If the specified IRE is associated with a particular ILL, return
2219  * that ILL pointer (May be called as writer.).
2220  *
2221  * NOTE : This is not a generic function that can be used always.
2222  * This function always returns the ill of the outgoing packets
2223  * if this ire is used.
2224  */
2225 ill_t *
2226 ire_to_ill(const ire_t *ire)
2227 {
2228 	ill_t *ill = NULL;
2229 
2230 	/*
2231 	 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained
2232 	 *    the source address from. ire_stq is the one where the
2233 	 *    packets will be sent out on. We return that here.
2234 	 *
2235 	 * 2) IRE_BROADCAST normally has a loopback and a non-loopback
2236 	 *    copy and they always exist next to each other with loopback
2237 	 *    copy being the first one. If we are called on the non-loopback
2238 	 *    copy, return the one pointed by ire_stq. If it was called on
2239 	 *    a loopback copy, we still return the one pointed by the next
2240 	 *    ire's ire_stq pointer i.e the one pointed by the non-loopback
2241 	 *    copy. We don't want use ire_ipif as it might represent the
2242 	 *    source address (if we borrow source addresses for
2243 	 *    IRE_BROADCASTS in the future).
2244 	 *    However if an interface is currently coming up, the above
2245 	 *    condition may not hold during that period since the ires
2246 	 *    are added one at a time. Thus one of the pair could have been
2247 	 *    added and the other not yet added.
2248 	 * 3) For many other IREs (e.g., IRE_LOCAL), ire_rfq indicates the ill.
2249 	 * 4) For all others return the ones pointed by ire_ipif->ipif_ill.
2250 	 *    That handles IRE_LOOPBACK.
2251 	 */
2252 
2253 	if (ire->ire_type == IRE_CACHE) {
2254 		ill = (ill_t *)ire->ire_stq->q_ptr;
2255 	} else if (ire->ire_type == IRE_BROADCAST) {
2256 		if (ire->ire_stq != NULL) {
2257 			ill = (ill_t *)ire->ire_stq->q_ptr;
2258 		} else {
2259 			ire_t  *ire_next;
2260 
2261 			ire_next = ire->ire_next;
2262 			if (ire_next != NULL &&
2263 			    ire_next->ire_type == IRE_BROADCAST &&
2264 			    ire_next->ire_addr == ire->ire_addr &&
2265 			    ire_next->ire_ipif == ire->ire_ipif) {
2266 				ill = (ill_t *)ire_next->ire_stq->q_ptr;
2267 			}
2268 		}
2269 	} else if (ire->ire_rfq != NULL) {
2270 		ill = ire->ire_rfq->q_ptr;
2271 	} else if (ire->ire_ipif != NULL) {
2272 		ill = ire->ire_ipif->ipif_ill;
2273 	}
2274 	return (ill);
2275 }
2276 
2277 /* Arrange to call the specified function for every IRE in the world. */
2278 void
2279 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
2280 {
2281 	ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
2282 }
2283 
2284 void
2285 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
2286 {
2287 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
2288 }
2289 
2290 void
2291 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
2292 {
2293 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
2294 }
2295 
2296 /*
2297  * Walk a particular version. version == 0 means both v4 and v6.
2298  */
2299 static void
2300 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
2301     ip_stack_t *ipst)
2302 {
2303 	if (vers != IPV6_VERSION) {
2304 		/*
2305 		 * ip_forwarding_table variable doesn't matter for IPv4 since
2306 		 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
2307 		 */
2308 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
2309 		    0, NULL,
2310 		    ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table,
2311 		    NULL, zoneid, ipst);
2312 	}
2313 	if (vers != IPV4_VERSION) {
2314 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
2315 		    ipst->ips_ip6_ftable_hash_size,
2316 		    ipst->ips_ip_forwarding_table_v6,
2317 		    ipst->ips_ip6_cache_table_size,
2318 		    ipst->ips_ip_cache_table_v6, NULL, zoneid, ipst);
2319 	}
2320 }
2321 
2322 /*
2323  * Arrange to call the specified
2324  * function for every IRE that matches the ill.
2325  */
2326 void
2327 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
2328     ill_t *ill)
2329 {
2330 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, 0, ill);
2331 }
2332 
2333 void
2334 ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
2335     ill_t *ill)
2336 {
2337 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION,
2338 	    ill);
2339 }
2340 
2341 void
2342 ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
2343     ill_t *ill)
2344 {
2345 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION,
2346 	    ill);
2347 }
2348 
2349 /*
2350  * Walk a particular ill and version. version == 0 means both v4 and v6.
2351  */
2352 static void
2353 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
2354     void *arg, uchar_t vers, ill_t *ill)
2355 {
2356 	ip_stack_t	*ipst = ill->ill_ipst;
2357 
2358 	if (vers != IPV6_VERSION) {
2359 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2360 		    IP_MASK_TABLE_SIZE, 0,
2361 		    NULL, ipst->ips_ip_cache_table_size,
2362 		    ipst->ips_ip_cache_table, ill, ALL_ZONES, ipst);
2363 	}
2364 	if (vers != IPV4_VERSION) {
2365 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2366 		    IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
2367 		    ipst->ips_ip_forwarding_table_v6,
2368 		    ipst->ips_ip6_cache_table_size,
2369 		    ipst->ips_ip_cache_table_v6, ill, ALL_ZONES, ipst);
2370 	}
2371 }
2372 
2373 boolean_t
2374 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
2375     ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
2376 {
2377 	ill_t *ire_stq_ill = NULL;
2378 	ill_t *ire_ipif_ill = NULL;
2379 	ill_group_t *ire_ill_group = NULL;
2380 
2381 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
2382 	/*
2383 	 * 1) MATCH_IRE_WQ : Used specifically to match on ire_stq.
2384 	 *    The fast path update uses this to make sure it does not
2385 	 *    update the fast path header of interface X with the fast
2386 	 *    path updates it recieved on interface Y.  It is similar
2387 	 *    in handling DL_NOTE_FASTPATH_FLUSH.
2388 	 *
2389 	 * 2) MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill
2390 	 *    pointed by ire_stq and ire_ipif. Only in the case of
2391 	 *    IRE_CACHEs can ire_stq and ire_ipif be pointing to
2392 	 *    different ills. But we want to keep this function generic
2393 	 *    enough for future use. So, we always try to match on both.
2394 	 *    The only caller of this function ire_walk_ill_tables, will
2395 	 *    call "func" after we return from this function. We expect
2396 	 *    "func" to do the right filtering of ires in this case.
2397 	 *
2398 	 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups
2399 	 * pointed by ire_stq and ire_ipif should always be the same.
2400 	 * So, we just match on only one of them.
2401 	 */
2402 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
2403 		if (ire->ire_stq != NULL)
2404 			ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
2405 		if (ire->ire_ipif != NULL)
2406 			ire_ipif_ill = ire->ire_ipif->ipif_ill;
2407 		if (ire_stq_ill != NULL)
2408 			ire_ill_group = ire_stq_ill->ill_group;
2409 		if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL))
2410 			ire_ill_group = ire_ipif_ill->ill_group;
2411 	}
2412 
2413 	if (zoneid != ALL_ZONES) {
2414 		/*
2415 		 * We're walking the IREs for a specific zone. The only relevant
2416 		 * IREs are:
2417 		 * - all IREs with a matching ire_zoneid
2418 		 * - all IRE_OFFSUBNETs as they're shared across all zones
2419 		 * - IRE_INTERFACE IREs for interfaces with a usable source addr
2420 		 *   with a matching zone
2421 		 * - IRE_DEFAULTs with a gateway reachable from the zone
2422 		 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs
2423 		 * using the same rule; but the above rules are consistent with
2424 		 * the behavior of ire_ftable_lookup[_v6]() so that all the
2425 		 * routes that can be matched during lookup are also matched
2426 		 * here.
2427 		 */
2428 		if (zoneid != ire->ire_zoneid && ire->ire_zoneid != ALL_ZONES) {
2429 			/*
2430 			 * Note, IRE_INTERFACE can have the stq as NULL. For
2431 			 * example, if the default multicast route is tied to
2432 			 * the loopback address.
2433 			 */
2434 			if ((ire->ire_type & IRE_INTERFACE) &&
2435 			    (ire->ire_stq != NULL)) {
2436 				ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
2437 				if (ire->ire_ipversion == IPV4_VERSION) {
2438 					if (!ipif_usesrc_avail(ire_stq_ill,
2439 					    zoneid))
2440 						/* No usable src addr in zone */
2441 						return (B_FALSE);
2442 				} else if (ire_stq_ill->ill_usesrc_ifindex
2443 				    != 0) {
2444 					/*
2445 					 * For IPv6 use ipif_select_source_v6()
2446 					 * so the right scope selection is done
2447 					 */
2448 					ipif_t *src_ipif;
2449 					src_ipif =
2450 					    ipif_select_source_v6(ire_stq_ill,
2451 					    &ire->ire_addr_v6, RESTRICT_TO_NONE,
2452 					    IPV6_PREFER_SRC_DEFAULT,
2453 					    zoneid);
2454 					if (src_ipif != NULL) {
2455 						ipif_refrele(src_ipif);
2456 					} else {
2457 						return (B_FALSE);
2458 					}
2459 				} else {
2460 					return (B_FALSE);
2461 				}
2462 
2463 			} else if (!(ire->ire_type & IRE_OFFSUBNET)) {
2464 				return (B_FALSE);
2465 			}
2466 		}
2467 
2468 		/*
2469 		 * Match all default routes from the global zone, irrespective
2470 		 * of reachability. For a non-global zone only match those
2471 		 * where ire_gateway_addr has a IRE_INTERFACE for the zoneid.
2472 		 */
2473 		if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) {
2474 			int ire_match_flags = 0;
2475 			in6_addr_t gw_addr_v6;
2476 			ire_t *rire;
2477 
2478 			ire_match_flags |= MATCH_IRE_TYPE;
2479 			if (ire->ire_ipif != NULL) {
2480 				ire_match_flags |= MATCH_IRE_ILL_GROUP;
2481 			}
2482 			if (ire->ire_ipversion == IPV4_VERSION) {
2483 				rire = ire_route_lookup(ire->ire_gateway_addr,
2484 				    0, 0, IRE_INTERFACE, ire->ire_ipif, NULL,
2485 				    zoneid, NULL, ire_match_flags, ipst);
2486 			} else {
2487 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
2488 				mutex_enter(&ire->ire_lock);
2489 				gw_addr_v6 = ire->ire_gateway_addr_v6;
2490 				mutex_exit(&ire->ire_lock);
2491 				rire = ire_route_lookup_v6(&gw_addr_v6,
2492 				    NULL, NULL, IRE_INTERFACE, ire->ire_ipif,
2493 				    NULL, zoneid, NULL, ire_match_flags, ipst);
2494 			}
2495 			if (rire == NULL) {
2496 				return (B_FALSE);
2497 			}
2498 			ire_refrele(rire);
2499 		}
2500 	}
2501 
2502 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
2503 		(ire->ire_type & ire_type)) &&
2504 	    ((!(match_flags & MATCH_IRE_WQ)) ||
2505 		(ire->ire_stq == ill->ill_wq)) &&
2506 	    ((!(match_flags & MATCH_IRE_ILL)) ||
2507 		(ire_stq_ill == ill || ire_ipif_ill == ill)) &&
2508 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
2509 		(ire_stq_ill == ill) || (ire_ipif_ill == ill) ||
2510 		(ire_ill_group != NULL &&
2511 		ire_ill_group == ill->ill_group))) {
2512 		return (B_TRUE);
2513 	}
2514 	return (B_FALSE);
2515 }
2516 
2517 int
2518 rtfunc(struct radix_node *rn, void *arg)
2519 {
2520 	struct rtfuncarg *rtf = arg;
2521 	struct rt_entry *rt;
2522 	irb_t *irb;
2523 	ire_t *ire;
2524 	boolean_t ret;
2525 
2526 	rt = (struct rt_entry *)rn;
2527 	ASSERT(rt != NULL);
2528 	irb = &rt->rt_irb;
2529 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2530 		if ((rtf->rt_match_flags != 0) ||
2531 		    (rtf->rt_zoneid != ALL_ZONES)) {
2532 			ret = ire_walk_ill_match(rtf->rt_match_flags,
2533 			    rtf->rt_ire_type, ire,
2534 			    rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
2535 		} else
2536 			ret = B_TRUE;
2537 		if (ret)
2538 			(*rtf->rt_func)(ire, rtf->rt_arg);
2539 	}
2540 	return (0);
2541 }
2542 
2543 /*
2544  * Walk the ftable and the ctable entries that match the ill.
2545  */
2546 void
2547 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
2548     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
2549     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid,
2550     ip_stack_t *ipst)
2551 {
2552 	irb_t	*irb_ptr;
2553 	irb_t	*irb;
2554 	ire_t	*ire;
2555 	int i, j;
2556 	boolean_t ret;
2557 	struct rtfuncarg rtfarg;
2558 
2559 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
2560 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
2561 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
2562 	/*
2563 	 * Optimize by not looking at the forwarding table if there
2564 	 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE
2565 	 * specified in ire_type.
2566 	 */
2567 	if (!(match_flags & MATCH_IRE_TYPE) ||
2568 	    ((ire_type & IRE_FORWARDTABLE) != 0)) {
2569 		/* knobs such that routine is called only for v6 case */
2570 		if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
2571 			for (i = (ftbl_sz - 1);  i >= 0; i--) {
2572 				if ((irb_ptr = ipftbl[i]) == NULL)
2573 					continue;
2574 				for (j = 0; j < htbl_sz; j++) {
2575 					irb = &irb_ptr[j];
2576 					if (irb->irb_ire == NULL)
2577 						continue;
2578 
2579 					IRB_REFHOLD(irb);
2580 					for (ire = irb->irb_ire; ire != NULL;
2581 						ire = ire->ire_next) {
2582 						if (match_flags == 0 &&
2583 						    zoneid == ALL_ZONES) {
2584 							ret = B_TRUE;
2585 						} else {
2586 							ret =
2587 							    ire_walk_ill_match(
2588 							    match_flags,
2589 							    ire_type, ire, ill,
2590 							    zoneid, ipst);
2591 						}
2592 						if (ret)
2593 							(*func)(ire, arg);
2594 					}
2595 					IRB_REFRELE(irb);
2596 				}
2597 			}
2598 		} else {
2599 			(void) memset(&rtfarg, 0, sizeof (rtfarg));
2600 			rtfarg.rt_func = func;
2601 			rtfarg.rt_arg = arg;
2602 			if (match_flags != 0) {
2603 				rtfarg.rt_match_flags = match_flags;
2604 			}
2605 			rtfarg.rt_ire_type = ire_type;
2606 			rtfarg.rt_ill = ill;
2607 			rtfarg.rt_zoneid = zoneid;
2608 			rtfarg.rt_ipst = ipst;	/* No netstack_hold */
2609 			(void) ipst->ips_ip_ftable->rnh_walktree_mt(
2610 			    ipst->ips_ip_ftable,
2611 			    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
2612 		}
2613 	}
2614 
2615 	/*
2616 	 * Optimize by not looking at the cache table if there
2617 	 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE
2618 	 * specified in ire_type.
2619 	 */
2620 	if (!(match_flags & MATCH_IRE_TYPE) ||
2621 	    ((ire_type & IRE_CACHETABLE) != 0)) {
2622 		for (i = 0; i < ctbl_sz;  i++) {
2623 			irb = &ipctbl[i];
2624 			if (irb->irb_ire == NULL)
2625 				continue;
2626 			IRB_REFHOLD(irb);
2627 			for (ire = irb->irb_ire; ire != NULL;
2628 			    ire = ire->ire_next) {
2629 				if (match_flags == 0 && zoneid == ALL_ZONES) {
2630 					ret = B_TRUE;
2631 				} else {
2632 					ret = ire_walk_ill_match(
2633 					    match_flags, ire_type,
2634 					    ire, ill, zoneid, ipst);
2635 				}
2636 				if (ret)
2637 					(*func)(ire, arg);
2638 			}
2639 			IRB_REFRELE(irb);
2640 		}
2641 	}
2642 }
2643 
2644 /*
2645  * This routine walks through the ill chain to find if there is any
2646  * ire linked to the ill's interface based forwarding table
2647  * The arg could be ill or mp. This routine is called when a ill goes
2648  * down/deleted or the 'ipv4_ire_srcif_status' report is printed.
2649  */
2650 void
2651 ire_walk_srcif_table_v4(pfv_t func, void *arg, ip_stack_t *ipst)
2652 {
2653 	irb_t   *irb;
2654 	ire_t   *ire;
2655 	ill_t	*ill, *next_ill;
2656 	int	i;
2657 	int	total_count;
2658 	ill_walk_context_t ctx;
2659 
2660 	/*
2661 	 * Take care of ire's in other ill's per-interface forwarding
2662 	 * table. Check if any ire in any of the ill's ill_srcif_table
2663 	 * is pointing to this ill.
2664 	 */
2665 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
2666 	if (ipst->ips_ire_srcif_table_count == 0) {
2667 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
2668 		return;
2669 	}
2670 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
2671 
2672 #ifdef DEBUG
2673 	/* Keep accounting of all interface based table ires */
2674 	total_count = 0;
2675 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2676 	ill = ILL_START_WALK_V4(&ctx, ipst);
2677 	while (ill != NULL) {
2678 		mutex_enter(&ill->ill_lock);
2679 		total_count += ill->ill_srcif_refcnt;
2680 		next_ill = ill_next(&ctx, ill);
2681 		mutex_exit(&ill->ill_lock);
2682 		ill = next_ill;
2683 	}
2684 	rw_exit(&ipst->ips_ill_g_lock);
2685 
2686 	/* Hold lock here to make sure ire_srcif_table_count is stable */
2687 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
2688 	i = ipst->ips_ire_srcif_table_count;
2689 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
2690 	ip1dbg(("ire_walk_srcif_v4: ire_srcif_table_count %d "
2691 	    "total ill_srcif_refcnt %d\n", i, total_count));
2692 #endif
2693 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2694 	ill = ILL_START_WALK_V4(&ctx, ipst);
2695 	while (ill != NULL) {
2696 		mutex_enter(&ill->ill_lock);
2697 		if ((ill->ill_srcif_refcnt == 0) || !ILL_CAN_LOOKUP(ill)) {
2698 			next_ill = ill_next(&ctx, ill);
2699 			mutex_exit(&ill->ill_lock);
2700 			ill = next_ill;
2701 			continue;
2702 		}
2703 		ill_refhold_locked(ill);
2704 		mutex_exit(&ill->ill_lock);
2705 		rw_exit(&ipst->ips_ill_g_lock);
2706 		if (ill->ill_srcif_table != NULL) {
2707 			for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
2708 				irb = &(ill->ill_srcif_table[i]);
2709 				if (irb->irb_ire == NULL)
2710 					continue;
2711 				IRB_REFHOLD(irb);
2712 				for (ire = irb->irb_ire; ire != NULL;
2713 				    ire = ire->ire_next) {
2714 					(*func)(ire, arg);
2715 				}
2716 				IRB_REFRELE(irb);
2717 			}
2718 		}
2719 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2720 		next_ill = ill_next(&ctx, ill);
2721 		ill_refrele(ill);
2722 		ill = next_ill;
2723 	}
2724 	rw_exit(&ipst->ips_ill_g_lock);
2725 }
2726 
2727 /*
2728  * This function takes a mask and returns
2729  * number of bits set in the mask. If no
2730  * bit is set it returns 0.
2731  * Assumes a contiguous mask.
2732  */
2733 int
2734 ip_mask_to_plen(ipaddr_t mask)
2735 {
2736 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
2737 }
2738 
2739 /*
2740  * Convert length for a mask to the mask.
2741  */
2742 ipaddr_t
2743 ip_plen_to_mask(uint_t masklen)
2744 {
2745 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
2746 }
2747 
2748 void
2749 ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
2750 {
2751 	ill_t	*ill_list[NUM_ILLS];
2752 	ip_stack_t	*ipst = ire->ire_ipst;
2753 
2754 	ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL;
2755 	ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL;
2756 	ill_list[2] = ire->ire_in_ill;
2757 	ill_unlock_ills(ill_list, NUM_ILLS);
2758 	rw_exit(&irb_ptr->irb_lock);
2759 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
2760 }
2761 
2762 /*
2763  * ire_add_v[46] atomically make sure that the ipif or ill associated
2764  * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING
2765  * before adding the ire to the table. This ensures that we don't create
2766  * new IRE_CACHEs with stale values for parameters that are passed to
2767  * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer
2768  * to the ipif_mtu, and not the value. The actual value is derived from the
2769  * parent ire or ipif under the bucket lock.
2770  */
2771 int
2772 ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp,
2773     ipsq_func_t func)
2774 {
2775 	ill_t	*stq_ill;
2776 	ill_t	*ipif_ill;
2777 	ill_t	*in_ill;
2778 	ill_t	*ill_list[NUM_ILLS];
2779 	int	cnt = NUM_ILLS;
2780 	int	error = 0;
2781 	ill_t	*ill = NULL;
2782 	ip_stack_t	*ipst = ire->ire_ipst;
2783 
2784 	ill_list[0] = stq_ill = ire->ire_stq !=
2785 		NULL ? ire->ire_stq->q_ptr : NULL;
2786 	ill_list[1] = ipif_ill = ire->ire_ipif !=
2787 		NULL ? ire->ire_ipif->ipif_ill : NULL;
2788 	ill_list[2] = in_ill = ire->ire_in_ill;
2789 
2790 	ASSERT((q != NULL && mp != NULL && func != NULL) ||
2791 	    (q == NULL && mp == NULL && func == NULL));
2792 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
2793 	GRAB_CONN_LOCK(q);
2794 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
2795 	ill_lock_ills(ill_list, cnt);
2796 
2797 	/*
2798 	 * While the IRE is in the process of being added, a user may have
2799 	 * invoked the ifconfig usesrc option on the stq_ill to make it a
2800 	 * usesrc client ILL. Check for this possibility here, if it is true
2801 	 * then we fail adding the IRE_CACHE. Another check is to make sure
2802 	 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc
2803 	 * group. The ill_g_usesrc_lock is released in ire_atomic_end
2804 	 */
2805 	if ((ire->ire_type & IRE_CACHE) &&
2806 	    (ire->ire_marks & IRE_MARK_USESRC_CHECK)) {
2807 		if (stq_ill->ill_usesrc_ifindex != 0) {
2808 			ASSERT(stq_ill->ill_usesrc_grp_next != NULL);
2809 			if ((ipif_ill->ill_phyint->phyint_ifindex !=
2810 			    stq_ill->ill_usesrc_ifindex) ||
2811 			    (ipif_ill->ill_usesrc_grp_next == NULL) ||
2812 			    (ipif_ill->ill_usesrc_ifindex != 0)) {
2813 				error = EINVAL;
2814 				goto done;
2815 			}
2816 		} else if (ipif_ill->ill_usesrc_grp_next != NULL) {
2817 			error = EINVAL;
2818 			goto done;
2819 		}
2820 	}
2821 
2822 	/*
2823 	 * IPMP flag settings happen without taking the exclusive route
2824 	 * in ip_sioctl_flags. So we need to make an atomic check here
2825 	 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the
2826 	 * FAILBACK=no case.
2827 	 */
2828 	if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) {
2829 		if (stq_ill->ill_state_flags & ILL_CHANGING) {
2830 			ill = stq_ill;
2831 			error = EAGAIN;
2832 		} else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
2833 		    (ill_is_probeonly(stq_ill) &&
2834 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
2835 			error = EINVAL;
2836 		}
2837 		goto done;
2838 	}
2839 
2840 	/*
2841 	 * We don't check for OFFLINE/FAILED in this case because
2842 	 * the source address selection logic (ipif_select_source)
2843 	 * may still select a source address from such an ill. The
2844 	 * assumption is that these addresses will be moved by in.mpathd
2845 	 * soon. (i.e. this is a race). However link local addresses
2846 	 * will not move and hence ipif_select_source_v6 tries to avoid
2847 	 * FAILED ills. Please see ipif_select_source_v6 for more info
2848 	 */
2849 	if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) &&
2850 	    (ipif_ill->ill_state_flags & ILL_CHANGING)) {
2851 		ill = ipif_ill;
2852 		error = EAGAIN;
2853 		goto done;
2854 	}
2855 
2856 	if ((in_ill != NULL) && !IAM_WRITER_ILL(in_ill) &&
2857 	    (in_ill->ill_state_flags & ILL_CHANGING)) {
2858 		ill = in_ill;
2859 		error = EAGAIN;
2860 		goto done;
2861 	}
2862 
2863 	if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) &&
2864 	    (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) {
2865 		ill = ire->ire_ipif->ipif_ill;
2866 		ASSERT(ill != NULL);
2867 		error = EAGAIN;
2868 		goto done;
2869 	}
2870 
2871 done:
2872 	if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) {
2873 		ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
2874 		mutex_enter(&ipsq->ipsq_lock);
2875 		ire_atomic_end(irb_ptr, ire);
2876 		ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
2877 		mutex_exit(&ipsq->ipsq_lock);
2878 		error = EINPROGRESS;
2879 	} else if (error != 0) {
2880 		ire_atomic_end(irb_ptr, ire);
2881 	}
2882 
2883 	RELEASE_CONN_LOCK(q);
2884 	return (error);
2885 }
2886 
2887 /*
2888  * Add a fully initialized IRE to an appropriate table based on
2889  * ire_type.
2890  *
2891  * allow_unresolved == B_FALSE indicates a legacy code-path call
2892  * that has prohibited the addition of incomplete ire's. If this
2893  * parameter is set, and we find an nce that is in a state other
2894  * than ND_REACHABLE, we fail the add. Note that nce_state could be
2895  * something other than ND_REACHABLE if nce_reinit has just
2896  * kicked in and reset the nce.
2897  */
2898 int
2899 ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func,
2900     boolean_t allow_unresolved)
2901 {
2902 	ire_t	*ire1;
2903 	ill_t	*stq_ill = NULL;
2904 	ill_t	*ill;
2905 	ipif_t	*ipif = NULL;
2906 	ill_walk_context_t ctx;
2907 	ire_t	*ire = *irep;
2908 	int	error;
2909 	boolean_t ire_is_mblk = B_FALSE;
2910 	tsol_gcgrp_t *gcgrp = NULL;
2911 	tsol_gcgrp_addr_t ga;
2912 	ip_stack_t	*ipst = ire->ire_ipst;
2913 
2914 	ASSERT(ire->ire_type != IRE_MIPRTUN);
2915 
2916 	/* get ready for the day when original ire is not created as mblk */
2917 	if (ire->ire_mp != NULL) {
2918 		ire_is_mblk = B_TRUE;
2919 		/* Copy the ire to a kmem_alloc'ed area */
2920 		ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
2921 		if (ire1 == NULL) {
2922 			ip1dbg(("ire_add: alloc failed\n"));
2923 			ire_delete(ire);
2924 			*irep = NULL;
2925 			return (ENOMEM);
2926 		}
2927 		ire->ire_marks &= ~IRE_MARK_UNCACHED;
2928 		*ire1 = *ire;
2929 		ire1->ire_mp = NULL;
2930 		ire1->ire_stq_ifindex = 0;
2931 		freeb(ire->ire_mp);
2932 		ire = ire1;
2933 	}
2934 	if (ire->ire_stq != NULL)
2935 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
2936 
2937 	if (ire->ire_type == IRE_CACHE) {
2938 		/*
2939 		 * If this interface is FAILED, or INACTIVE or has hit
2940 		 * the FAILBACK=no case, we create IRE_CACHES marked
2941 		 * HIDDEN for some special cases e.g. bind to
2942 		 * IPIF_NOFAILOVER address etc. So, if this interface
2943 		 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are
2944 		 * not creating hidden ires, we should not allow that.
2945 		 * This happens because the state of the interface
2946 		 * changed while we were waiting in ARP. If this is the
2947 		 * daemon sending probes, the next probe will create
2948 		 * HIDDEN ires and we will create an ire then. This
2949 		 * cannot happen with NDP currently because IRE is
2950 		 * never queued in NDP. But it can happen in the
2951 		 * future when we have external resolvers with IPv6.
2952 		 * If the interface gets marked with OFFLINE while we
2953 		 * are waiting in ARP, don't add the ire.
2954 		 */
2955 		if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
2956 		    (ill_is_probeonly(stq_ill) &&
2957 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
2958 			/*
2959 			 * We don't know whether it is a valid ipif or not.
2960 			 * unless we do the check below. So, set it to NULL.
2961 			 */
2962 			ire->ire_ipif = NULL;
2963 			ire_delete(ire);
2964 			*irep = NULL;
2965 			return (EINVAL);
2966 		}
2967 	}
2968 
2969 	if (stq_ill != NULL && ire->ire_type == IRE_CACHE &&
2970 	    stq_ill->ill_net_type == IRE_IF_RESOLVER) {
2971 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2972 		ill = ILL_START_WALK_ALL(&ctx, ipst);
2973 		for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2974 			mutex_enter(&ill->ill_lock);
2975 			if (ill->ill_state_flags & ILL_CONDEMNED) {
2976 				mutex_exit(&ill->ill_lock);
2977 				continue;
2978 			}
2979 			/*
2980 			 * We need to make sure that the ipif is a valid one
2981 			 * before adding the IRE_CACHE. This happens only
2982 			 * with IRE_CACHE when there is an external resolver.
2983 			 *
2984 			 * We can unplumb a logical interface while the
2985 			 * packet is waiting in ARP with the IRE. Then,
2986 			 * later on when we feed the IRE back, the ipif
2987 			 * has to be re-checked. This can't happen with
2988 			 * NDP currently, as we never queue the IRE with
2989 			 * the packet. We always try to recreate the IRE
2990 			 * when the resolution is completed. But, we do
2991 			 * it for IPv6 also here so that in future if
2992 			 * we have external resolvers, it will work without
2993 			 * any change.
2994 			 */
2995 			ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid);
2996 			if (ipif != NULL) {
2997 				ipif_refhold_locked(ipif);
2998 				mutex_exit(&ill->ill_lock);
2999 				break;
3000 			}
3001 			mutex_exit(&ill->ill_lock);
3002 		}
3003 		rw_exit(&ipst->ips_ill_g_lock);
3004 		if (ipif == NULL ||
3005 		    (ipif->ipif_isv6 &&
3006 		    !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
3007 		    &ipif->ipif_v6src_addr)) ||
3008 		    (!ipif->ipif_isv6 &&
3009 		    ire->ire_src_addr != ipif->ipif_src_addr) ||
3010 		    ire->ire_zoneid != ipif->ipif_zoneid) {
3011 
3012 			if (ipif != NULL)
3013 				ipif_refrele(ipif);
3014 			ire->ire_ipif = NULL;
3015 			ire_delete(ire);
3016 			*irep = NULL;
3017 			return (EINVAL);
3018 		}
3019 
3020 
3021 		ASSERT(ill != NULL);
3022 		/*
3023 		 * If this group was dismantled while this packets was
3024 		 * queued in ARP, don't add it here.
3025 		 */
3026 		if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) {
3027 			/* We don't want ire_inactive bump stats for this */
3028 			ipif_refrele(ipif);
3029 			ire->ire_ipif = NULL;
3030 			ire_delete(ire);
3031 			*irep = NULL;
3032 			return (EINVAL);
3033 		}
3034 
3035 		/*
3036 		 * Since we didn't attach label security attributes to the
3037 		 * ire for the resolver case, we need to add it now. (only
3038 		 * for v4 resolver and v6 xresolv case).
3039 		 */
3040 		if (is_system_labeled() && ire_is_mblk) {
3041 			if (ire->ire_ipversion == IPV4_VERSION) {
3042 				ga.ga_af = AF_INET;
3043 				IN6_IPADDR_TO_V4MAPPED(ire->ire_gateway_addr !=
3044 				    INADDR_ANY ? ire->ire_gateway_addr :
3045 				    ire->ire_addr, &ga.ga_addr);
3046 			} else {
3047 				ga.ga_af = AF_INET6;
3048 				ga.ga_addr = IN6_IS_ADDR_UNSPECIFIED(
3049 				    &ire->ire_gateway_addr_v6) ?
3050 				    ire->ire_addr_v6 :
3051 				    ire->ire_gateway_addr_v6;
3052 			}
3053 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
3054 			error = tsol_ire_init_gwattr(ire, ire->ire_ipversion,
3055 			    NULL, gcgrp);
3056 			if (error != 0) {
3057 				if (gcgrp != NULL) {
3058 					GCGRP_REFRELE(gcgrp);
3059 					gcgrp = NULL;
3060 				}
3061 				ipif_refrele(ipif);
3062 				ire->ire_ipif = NULL;
3063 				ire_delete(ire);
3064 				*irep = NULL;
3065 				return (error);
3066 			}
3067 		}
3068 	}
3069 
3070 	/*
3071 	 * In case ire was changed
3072 	 */
3073 	*irep = ire;
3074 	if (ire->ire_ipversion == IPV6_VERSION) {
3075 		error = ire_add_v6(irep, q, mp, func);
3076 	} else {
3077 		if (ire->ire_in_ill == NULL)
3078 			error = ire_add_v4(irep, q, mp, func, allow_unresolved);
3079 		else
3080 			error = ire_add_srcif_v4(irep, q, mp, func);
3081 	}
3082 	if (ipif != NULL)
3083 		ipif_refrele(ipif);
3084 	return (error);
3085 }
3086 
3087 /*
3088  * Add an initialized IRE to an appropriate table based on ire_type.
3089  *
3090  * The forward table contains IRE_PREFIX/IRE_HOST and
3091  * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT.
3092  *
3093  * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK
3094  * and IRE_CACHE.
3095  *
3096  * NOTE : This function is called as writer though not required
3097  * by this function.
3098  */
3099 static int
3100 ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func,
3101     boolean_t allow_unresolved)
3102 {
3103 	ire_t	*ire1;
3104 	irb_t	*irb_ptr;
3105 	ire_t	**irep;
3106 	int	flags;
3107 	ire_t	*pire = NULL;
3108 	ill_t	*stq_ill;
3109 	ire_t	*ire = *ire_p;
3110 	int	error;
3111 	boolean_t need_refrele = B_FALSE;
3112 	nce_t	*nce;
3113 	ip_stack_t	*ipst = ire->ire_ipst;
3114 
3115 	if (ire->ire_ipif != NULL)
3116 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
3117 	if (ire->ire_stq != NULL)
3118 		ASSERT(!MUTEX_HELD(
3119 		    &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock));
3120 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
3121 	ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */
3122 	ASSERT(ire->ire_in_ill == NULL); /* No srcif entries */
3123 
3124 	/* Find the appropriate list head. */
3125 	switch (ire->ire_type) {
3126 	case IRE_HOST:
3127 		ire->ire_mask = IP_HOST_MASK;
3128 		ire->ire_masklen = IP_ABITS;
3129 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3130 			ire->ire_src_addr = 0;
3131 		break;
3132 	case IRE_CACHE:
3133 	case IRE_BROADCAST:
3134 	case IRE_LOCAL:
3135 	case IRE_LOOPBACK:
3136 		ire->ire_mask = IP_HOST_MASK;
3137 		ire->ire_masklen = IP_ABITS;
3138 		break;
3139 	case IRE_PREFIX:
3140 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3141 			ire->ire_src_addr = 0;
3142 		break;
3143 	case IRE_DEFAULT:
3144 		if ((ire->ire_flags & RTF_SETSRC) == 0)
3145 			ire->ire_src_addr = 0;
3146 		break;
3147 	case IRE_IF_RESOLVER:
3148 	case IRE_IF_NORESOLVER:
3149 		break;
3150 	default:
3151 		ip0dbg(("ire_add_v4: ire %p has unrecognized IRE type (%d)\n",
3152 		    (void *)ire, ire->ire_type));
3153 		ire_delete(ire);
3154 		*ire_p = NULL;
3155 		return (EINVAL);
3156 	}
3157 
3158 	/* Make sure the address is properly masked. */
3159 	ire->ire_addr &= ire->ire_mask;
3160 
3161 	/*
3162 	 * ip_newroute/ip_newroute_multi are unable to prevent the deletion
3163 	 * of the interface route while adding an IRE_CACHE for an on-link
3164 	 * destination in the IRE_IF_RESOLVER case, since the ire has to
3165 	 * go to ARP and return. We can't do a REFHOLD on the
3166 	 * associated interface ire for fear of ARP freeing the message.
3167 	 * Here we look up the interface ire in the forwarding table and
3168 	 * make sure that the interface route has not been deleted.
3169 	 */
3170 	if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 &&
3171 	    ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) {
3172 
3173 		ASSERT(ire->ire_max_fragp == NULL);
3174 		if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) {
3175 			/*
3176 			 * The ihandle that we used in ip_newroute_multi
3177 			 * comes from the interface route corresponding
3178 			 * to ire_ipif. Lookup here to see if it exists
3179 			 * still.
3180 			 * If the ire has a source address assigned using
3181 			 * RTF_SETSRC, ire_ipif is the logical interface holding
3182 			 * this source address, so we can't use it to check for
3183 			 * the existence of the interface route. Instead we rely
3184 			 * on the brute force ihandle search in
3185 			 * ire_ihandle_lookup_onlink() below.
3186 			 */
3187 			pire = ipif_to_ire(ire->ire_ipif);
3188 			if (pire == NULL) {
3189 				ire_delete(ire);
3190 				*ire_p = NULL;
3191 				return (EINVAL);
3192 			} else if (pire->ire_ihandle != ire->ire_ihandle) {
3193 				ire_refrele(pire);
3194 				ire_delete(ire);
3195 				*ire_p = NULL;
3196 				return (EINVAL);
3197 			}
3198 		} else {
3199 			pire = ire_ihandle_lookup_onlink(ire);
3200 			if (pire == NULL) {
3201 				ire_delete(ire);
3202 				*ire_p = NULL;
3203 				return (EINVAL);
3204 			}
3205 		}
3206 		/* Prevent pire from getting deleted */
3207 		IRB_REFHOLD(pire->ire_bucket);
3208 		/* Has it been removed already ? */
3209 		if (pire->ire_marks & IRE_MARK_CONDEMNED) {
3210 			IRB_REFRELE(pire->ire_bucket);
3211 			ire_refrele(pire);
3212 			ire_delete(ire);
3213 			*ire_p = NULL;
3214 			return (EINVAL);
3215 		}
3216 	} else {
3217 		ASSERT(ire->ire_max_fragp != NULL);
3218 	}
3219 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
3220 
3221 	if (ire->ire_ipif != NULL) {
3222 		/*
3223 		 * We use MATCH_IRE_IPIF while adding IRE_CACHES only
3224 		 * for historic reasons and to maintain symmetry with
3225 		 * IPv6 code path. Historically this was used by
3226 		 * multicast code to create multiple IRE_CACHES on
3227 		 * a single ill with different ipifs. This was used
3228 		 * so that multicast packets leaving the node had the
3229 		 * right source address. This is no longer needed as
3230 		 * ip_wput initializes the address correctly.
3231 		 */
3232 		flags |= MATCH_IRE_IPIF;
3233 		/*
3234 		 * If we are creating hidden ires, make sure we search on
3235 		 * this ill (MATCH_IRE_ILL) and a hidden ire,
3236 		 * while we are searching for duplicates below. Otherwise we
3237 		 * could potentially find an IRE on some other interface
3238 		 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We
3239 		 * shouldn't do this as this will lead to an infinite loop
3240 		 * (if we get to ip_wput again) eventually we need an hidden
3241 		 * ire for this packet to go out. MATCH_IRE_ILL is explicitly
3242 		 * done below.
3243 		 */
3244 		if (ire->ire_type == IRE_CACHE &&
3245 		    (ire->ire_marks & IRE_MARK_HIDDEN))
3246 			flags |= (MATCH_IRE_MARK_HIDDEN);
3247 	}
3248 	if ((ire->ire_type & IRE_CACHETABLE) == 0) {
3249 		irb_ptr = ire_get_bucket(ire);
3250 		need_refrele = B_TRUE;
3251 		if (irb_ptr == NULL) {
3252 			/*
3253 			 * This assumes that the ire has not added
3254 			 * a reference to the ipif.
3255 			 */
3256 			ire->ire_ipif = NULL;
3257 			ire_delete(ire);
3258 			if (pire != NULL) {
3259 				IRB_REFRELE(pire->ire_bucket);
3260 				ire_refrele(pire);
3261 			}
3262 			*ire_p = NULL;
3263 			return (EINVAL);
3264 		}
3265 	} else {
3266 		irb_ptr = &(ipst->ips_ip_cache_table[IRE_ADDR_HASH(
3267 		    ire->ire_addr, ipst->ips_ip_cache_table_size)]);
3268 	}
3269 
3270 	/*
3271 	 * Start the atomic add of the ire. Grab the ill locks,
3272 	 * ill_g_usesrc_lock and the bucket lock. Check for condemned
3273 	 *
3274 	 * If ipif or ill is changing ire_atomic_start() may queue the
3275 	 * request and return EINPROGRESS.
3276 	 * To avoid lock order problems, get the ndp4->ndp_g_lock.
3277 	 */
3278 	mutex_enter(&ipst->ips_ndp4->ndp_g_lock);
3279 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
3280 	if (error != 0) {
3281 		mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3282 		/*
3283 		 * We don't know whether it is a valid ipif or not.
3284 		 * So, set it to NULL. This assumes that the ire has not added
3285 		 * a reference to the ipif.
3286 		 */
3287 		ire->ire_ipif = NULL;
3288 		ire_delete(ire);
3289 		if (pire != NULL) {
3290 			IRB_REFRELE(pire->ire_bucket);
3291 			ire_refrele(pire);
3292 		}
3293 		*ire_p = NULL;
3294 		if (need_refrele)
3295 			IRB_REFRELE(irb_ptr);
3296 		return (error);
3297 	}
3298 	/*
3299 	 * To avoid creating ires having stale values for the ire_max_frag
3300 	 * we get the latest value atomically here. For more details
3301 	 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE
3302 	 * in ip_rput_dlpi_writer
3303 	 */
3304 	if (ire->ire_max_fragp == NULL) {
3305 		if (CLASSD(ire->ire_addr))
3306 			ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
3307 		else
3308 			ire->ire_max_frag = pire->ire_max_frag;
3309 	} else {
3310 		uint_t	max_frag;
3311 
3312 		max_frag = *ire->ire_max_fragp;
3313 		ire->ire_max_fragp = NULL;
3314 		ire->ire_max_frag = max_frag;
3315 	}
3316 	/*
3317 	 * Atomically check for duplicate and insert in the table.
3318 	 */
3319 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
3320 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
3321 			continue;
3322 		if (ire->ire_ipif != NULL) {
3323 			/*
3324 			 * We do MATCH_IRE_ILL implicitly here for IREs
3325 			 * with a non-null ire_ipif, including IRE_CACHEs.
3326 			 * As ire_ipif and ire_stq could point to two
3327 			 * different ills, we can't pass just ire_ipif to
3328 			 * ire_match_args and get a match on both ills.
3329 			 * This is just needed for duplicate checks here and
3330 			 * so we don't add an extra argument to
3331 			 * ire_match_args for this. Do it locally.
3332 			 *
3333 			 * NOTE : Currently there is no part of the code
3334 			 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL
3335 			 * match for IRE_CACHEs. Thus we don't want to
3336 			 * extend the arguments to ire_match_args.
3337 			 */
3338 			if (ire1->ire_stq != ire->ire_stq)
3339 				continue;
3340 			/*
3341 			 * Multiroute IRE_CACHEs for a given destination can
3342 			 * have the same ire_ipif, typically if their source
3343 			 * address is forced using RTF_SETSRC, and the same
3344 			 * send-to queue. We differentiate them using the parent
3345 			 * handle.
3346 			 */
3347 			if (ire->ire_type == IRE_CACHE &&
3348 			    (ire1->ire_flags & RTF_MULTIRT) &&
3349 			    (ire->ire_flags & RTF_MULTIRT) &&
3350 			    (ire1->ire_phandle != ire->ire_phandle))
3351 				continue;
3352 		}
3353 		if (ire1->ire_zoneid != ire->ire_zoneid)
3354 			continue;
3355 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
3356 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif,
3357 		    ire->ire_zoneid, 0, NULL, flags)) {
3358 			/*
3359 			 * Return the old ire after doing a REFHOLD.
3360 			 * As most of the callers continue to use the IRE
3361 			 * after adding, we return a held ire. This will
3362 			 * avoid a lookup in the caller again. If the callers
3363 			 * don't want to use it, they need to do a REFRELE.
3364 			 */
3365 			ip1dbg(("found dup ire existing %p new %p",
3366 			    (void *)ire1, (void *)ire));
3367 			IRE_REFHOLD(ire1);
3368 			ire_atomic_end(irb_ptr, ire);
3369 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3370 			ire_delete(ire);
3371 			if (pire != NULL) {
3372 				/*
3373 				 * Assert that it is not removed from the
3374 				 * list yet.
3375 				 */
3376 				ASSERT(pire->ire_ptpn != NULL);
3377 				IRB_REFRELE(pire->ire_bucket);
3378 				ire_refrele(pire);
3379 			}
3380 			*ire_p = ire1;
3381 			if (need_refrele)
3382 				IRB_REFRELE(irb_ptr);
3383 			return (0);
3384 		}
3385 	}
3386 	if (ire->ire_type & IRE_CACHE) {
3387 		ASSERT(ire->ire_stq != NULL);
3388 		nce = ndp_lookup_v4(ire_to_ill(ire),
3389 		    ((ire->ire_gateway_addr != INADDR_ANY) ?
3390 		    &ire->ire_gateway_addr : &ire->ire_addr),
3391 		    B_TRUE);
3392 		if (nce != NULL)
3393 			mutex_enter(&nce->nce_lock);
3394 		/*
3395 		 * if the nce is NCE_F_CONDEMNED, or if it is not ND_REACHABLE
3396 		 * and the caller has prohibited the addition of incomplete
3397 		 * ire's, we fail the add. Note that nce_state could be
3398 		 * something other than ND_REACHABLE if nce_reinit has just
3399 		 * kicked in and reset the nce.
3400 		 */
3401 		if ((nce == NULL) ||
3402 		    (nce->nce_flags & NCE_F_CONDEMNED) ||
3403 		    (!allow_unresolved &&
3404 		    (nce->nce_state != ND_REACHABLE))) {
3405 			if (nce != NULL)
3406 				mutex_exit(&nce->nce_lock);
3407 			ire_atomic_end(irb_ptr, ire);
3408 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3409 			if (nce != NULL)
3410 				NCE_REFRELE(nce);
3411 			DTRACE_PROBE1(ire__no__nce, ire_t *, ire);
3412 			ire_delete(ire);
3413 			if (pire != NULL) {
3414 				IRB_REFRELE(pire->ire_bucket);
3415 				ire_refrele(pire);
3416 			}
3417 			*ire_p = NULL;
3418 			if (need_refrele)
3419 				IRB_REFRELE(irb_ptr);
3420 			return (EINVAL);
3421 		} else {
3422 			ire->ire_nce = nce;
3423 			mutex_exit(&nce->nce_lock);
3424 			/*
3425 			 * We are associating this nce to the ire, so
3426 			 * change the nce ref taken in ndp_lookup_v4() from
3427 			 * NCE_REFHOLD to NCE_REFHOLD_NOTR
3428 			 */
3429 			NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
3430 		}
3431 	}
3432 	/*
3433 	 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by
3434 	 * grouping identical addresses together on the hash chain. We also
3435 	 * don't want to send multiple copies out if there are two ills part
3436 	 * of the same group. Thus we group the ires with same addr and same
3437 	 * ill group together so that ip_wput_ire can easily skip all the
3438 	 * ires with same addr and same group after sending the first copy.
3439 	 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently
3440 	 * interested in such groupings only for broadcasts.
3441 	 *
3442 	 * NOTE : If the interfaces are brought up first and then grouped,
3443 	 * illgrp_insert will handle it. We come here when the interfaces
3444 	 * are already in group and we are bringing them UP.
3445 	 *
3446 	 * Find the first entry that matches ire_addr. *irep will be null
3447 	 * if no match.
3448 	 */
3449 	irep = (ire_t **)irb_ptr;
3450 	while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr)
3451 		irep = &ire1->ire_next;
3452 	if (ire->ire_type == IRE_BROADCAST && *irep != NULL) {
3453 		/*
3454 		 * We found some ire (i.e *irep) with a matching addr. We
3455 		 * want to group ires with same addr and same ill group
3456 		 * together.
3457 		 *
3458 		 * First get to the entry that matches our address and
3459 		 * ill group i.e stop as soon as we find the first ire
3460 		 * matching the ill group and address. If there is only
3461 		 * an address match, we should walk and look for some
3462 		 * group match. These are some of the possible scenarios :
3463 		 *
3464 		 * 1) There are no groups at all i.e all ire's ill_group
3465 		 *    are NULL. In that case we will essentially group
3466 		 *    all the ires with the same addr together. Same as
3467 		 *    the "else" block of this "if".
3468 		 *
3469 		 * 2) There are some groups and this ire's ill_group is
3470 		 *    NULL. In this case, we will first find the group
3471 		 *    that matches the address and a NULL group. Then
3472 		 *    we will insert the ire at the end of that group.
3473 		 *
3474 		 * 3) There are some groups and this ires's ill_group is
3475 		 *    non-NULL. In this case we will first find the group
3476 		 *    that matches the address and the ill_group. Then
3477 		 *    we will insert the ire at the end of that group.
3478 		 */
3479 		/* LINTED : constant in conditional context */
3480 		while (1) {
3481 			ire1 = *irep;
3482 			if ((ire1->ire_next == NULL) ||
3483 			    (ire1->ire_next->ire_addr != ire->ire_addr) ||
3484 			    (ire1->ire_type != IRE_BROADCAST) ||
3485 			    (ire1->ire_ipif->ipif_ill->ill_group ==
3486 			    ire->ire_ipif->ipif_ill->ill_group))
3487 				break;
3488 			irep = &ire1->ire_next;
3489 		}
3490 		ASSERT(*irep != NULL);
3491 		irep = &((*irep)->ire_next);
3492 
3493 		/*
3494 		 * Either we have hit the end of the list or the address
3495 		 * did not match or the group *matched*. If we found
3496 		 * a match on the group, skip to the end of the group.
3497 		 */
3498 		while (*irep != NULL) {
3499 			ire1 = *irep;
3500 			if ((ire1->ire_addr != ire->ire_addr) ||
3501 			    (ire1->ire_type != IRE_BROADCAST) ||
3502 			    (ire1->ire_ipif->ipif_ill->ill_group !=
3503 			    ire->ire_ipif->ipif_ill->ill_group))
3504 				break;
3505 			if (ire1->ire_ipif->ipif_ill->ill_group == NULL &&
3506 			    ire1->ire_ipif == ire->ire_ipif) {
3507 				irep = &ire1->ire_next;
3508 				break;
3509 			}
3510 			irep = &ire1->ire_next;
3511 		}
3512 	} else if (*irep != NULL) {
3513 		/*
3514 		 * Find the last ire which matches ire_addr.
3515 		 * Needed to do tail insertion among entries with the same
3516 		 * ire_addr.
3517 		 */
3518 		while (ire->ire_addr == ire1->ire_addr) {
3519 			irep = &ire1->ire_next;
3520 			ire1 = *irep;
3521 			if (ire1 == NULL)
3522 				break;
3523 		}
3524 	}
3525 
3526 	/* Insert at *irep */
3527 	ire1 = *irep;
3528 	if (ire1 != NULL)
3529 		ire1->ire_ptpn = &ire->ire_next;
3530 	ire->ire_next = ire1;
3531 	/* Link the new one in. */
3532 	ire->ire_ptpn = irep;
3533 
3534 	/*
3535 	 * ire_walk routines de-reference ire_next without holding
3536 	 * a lock. Before we point to the new ire, we want to make
3537 	 * sure the store that sets the ire_next of the new ire
3538 	 * reaches global visibility, so that ire_walk routines
3539 	 * don't see a truncated list of ires i.e if the ire_next
3540 	 * of the new ire gets set after we do "*irep = ire" due
3541 	 * to re-ordering, the ire_walk thread will see a NULL
3542 	 * once it accesses the ire_next of the new ire.
3543 	 * membar_producer() makes sure that the following store
3544 	 * happens *after* all of the above stores.
3545 	 */
3546 	membar_producer();
3547 	*irep = ire;
3548 	ire->ire_bucket = irb_ptr;
3549 	/*
3550 	 * We return a bumped up IRE above. Keep it symmetrical
3551 	 * so that the callers will always have to release. This
3552 	 * helps the callers of this function because they continue
3553 	 * to use the IRE after adding and hence they don't have to
3554 	 * lookup again after we return the IRE.
3555 	 *
3556 	 * NOTE : We don't have to use atomics as this is appearing
3557 	 * in the list for the first time and no one else can bump
3558 	 * up the reference count on this yet.
3559 	 */
3560 	IRE_REFHOLD_LOCKED(ire);
3561 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
3562 
3563 	irb_ptr->irb_ire_cnt++;
3564 	if (irb_ptr->irb_marks & IRB_MARK_FTABLE)
3565 		irb_ptr->irb_nire++;
3566 
3567 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
3568 		irb_ptr->irb_tmp_ire_cnt++;
3569 
3570 	if (ire->ire_ipif != NULL) {
3571 		ire->ire_ipif->ipif_ire_cnt++;
3572 		if (ire->ire_stq != NULL) {
3573 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
3574 			stq_ill->ill_ire_cnt++;
3575 		}
3576 	} else {
3577 		ASSERT(ire->ire_stq == NULL);
3578 	}
3579 
3580 	ire_atomic_end(irb_ptr, ire);
3581 	mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3582 
3583 	if (pire != NULL) {
3584 		/* Assert that it is not removed from the list yet */
3585 		ASSERT(pire->ire_ptpn != NULL);
3586 		IRB_REFRELE(pire->ire_bucket);
3587 		ire_refrele(pire);
3588 	}
3589 
3590 	if (ire->ire_type != IRE_CACHE) {
3591 		/*
3592 		 * For ire's with host mask see if there is an entry
3593 		 * in the cache. If there is one flush the whole cache as
3594 		 * there might be multiple entries due to RTF_MULTIRT (CGTP).
3595 		 * If no entry is found than there is no need to flush the
3596 		 * cache.
3597 		 */
3598 		if (ire->ire_mask == IP_HOST_MASK) {
3599 			ire_t *lire;
3600 			lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE,
3601 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3602 			if (lire != NULL) {
3603 				ire_refrele(lire);
3604 				ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
3605 			}
3606 		} else {
3607 			ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
3608 		}
3609 	}
3610 	/*
3611 	 * We had to delay the fast path probe until the ire is inserted
3612 	 * in the list. Otherwise the fast path ack won't find the ire in
3613 	 * the table.
3614 	 */
3615 	if (ire->ire_type == IRE_CACHE ||
3616 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL)) {
3617 		ASSERT(ire->ire_nce != NULL);
3618 		nce_fastpath(ire->ire_nce);
3619 	}
3620 	if (ire->ire_ipif != NULL)
3621 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
3622 	*ire_p = ire;
3623 	if (need_refrele) {
3624 		IRB_REFRELE(irb_ptr);
3625 	}
3626 	return (0);
3627 }
3628 
3629 /*
3630  * IRB_REFRELE is the only caller of the function. ire_unlink calls to
3631  * do the final cleanup for this ire.
3632  */
3633 void
3634 ire_cleanup(ire_t *ire)
3635 {
3636 	ire_t *ire_next;
3637 	ip_stack_t *ipst = ire->ire_ipst;
3638 
3639 	ASSERT(ire != NULL);
3640 
3641 	while (ire != NULL) {
3642 		ire_next = ire->ire_next;
3643 		if (ire->ire_ipversion == IPV4_VERSION) {
3644 			ire_delete_v4(ire);
3645 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
3646 			    ire_stats_deleted);
3647 		} else {
3648 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
3649 			ire_delete_v6(ire);
3650 			BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
3651 			    ire_stats_deleted);
3652 		}
3653 		/*
3654 		 * Now it's really out of the list. Before doing the
3655 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
3656 		 * so.
3657 		 */
3658 		ire->ire_next = NULL;
3659 		IRE_REFRELE_NOTR(ire);
3660 		ire = ire_next;
3661 	}
3662 }
3663 
3664 /*
3665  * IRB_REFRELE is the only caller of the function. It calls to unlink
3666  * all the CONDEMNED ires from this bucket.
3667  */
3668 ire_t *
3669 ire_unlink(irb_t *irb)
3670 {
3671 	ire_t *ire;
3672 	ire_t *ire1;
3673 	ire_t **ptpn;
3674 	ire_t *ire_list = NULL;
3675 
3676 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
3677 	ASSERT(((irb->irb_marks & IRB_MARK_FTABLE) && irb->irb_refcnt == 1) ||
3678 	    (irb->irb_refcnt == 0));
3679 	ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
3680 	ASSERT(irb->irb_ire != NULL);
3681 
3682 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
3683 		ip_stack_t	*ipst = ire->ire_ipst;
3684 
3685 		ire1 = ire->ire_next;
3686 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
3687 			ptpn = ire->ire_ptpn;
3688 			ire1 = ire->ire_next;
3689 			if (ire1)
3690 				ire1->ire_ptpn = ptpn;
3691 			*ptpn = ire1;
3692 			ire->ire_ptpn = NULL;
3693 			ire->ire_next = NULL;
3694 			if (ire->ire_type == IRE_DEFAULT) {
3695 				/*
3696 				 * IRE is out of the list. We need to adjust
3697 				 * the accounting before the caller drops
3698 				 * the lock.
3699 				 */
3700 				if (ire->ire_ipversion == IPV6_VERSION) {
3701 					ASSERT(ipst->
3702 					    ips_ipv6_ire_default_count !=
3703 					    0);
3704 					ipst->ips_ipv6_ire_default_count--;
3705 				}
3706 			}
3707 			/*
3708 			 * We need to call ire_delete_v4 or ire_delete_v6
3709 			 * to clean up the cache or the redirects pointing at
3710 			 * the default gateway. We need to drop the lock
3711 			 * as ire_flush_cache/ire_delete_host_redircts require
3712 			 * so. But we can't drop the lock, as ire_unlink needs
3713 			 * to atomically remove the ires from the list.
3714 			 * So, create a temporary list of CONDEMNED ires
3715 			 * for doing ire_delete_v4/ire_delete_v6 operations
3716 			 * later on.
3717 			 */
3718 			ire->ire_next = ire_list;
3719 			ire_list = ire;
3720 		}
3721 	}
3722 	irb->irb_marks &= ~IRB_MARK_CONDEMNED;
3723 	return (ire_list);
3724 }
3725 
3726 /*
3727  * Delete all the cache entries with this 'addr'.  When IP gets a gratuitous
3728  * ARP message on any of its interface queue, it scans the nce table and
3729  * deletes and calls ndp_delete() for the appropriate nce. This action
3730  * also deletes all the neighbor/ire cache entries for that address.
3731  * This function is called from ip_arp_news in ip.c and also for
3732  * ARP ioctl processing in ip_if.c. ip_ire_clookup_and_delete returns
3733  * true if it finds a nce entry which is used by ip_arp_news to determine if
3734  * it needs to do an ire_walk_v4. The return value is also  used for the
3735  * same purpose by ARP IOCTL processing * in ip_if.c when deleting
3736  * ARP entries. For SIOC*IFARP ioctls in addition to the address,
3737  * ip_if->ipif_ill also needs to be matched.
3738  */
3739 boolean_t
3740 ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif, ip_stack_t *ipst)
3741 {
3742 	ill_t	*ill;
3743 	nce_t	*nce;
3744 
3745 	ill = (ipif ? ipif->ipif_ill : NULL);
3746 
3747 	if (ill != NULL) {
3748 		/*
3749 		 * clean up the nce (and any relevant ire's) that matches
3750 		 * on addr and ill.
3751 		 */
3752 		nce = ndp_lookup_v4(ill, &addr, B_FALSE);
3753 		if (nce != NULL) {
3754 			ndp_delete(nce);
3755 			return (B_TRUE);
3756 		}
3757 	} else {
3758 		/*
3759 		 * ill is wildcard. clean up all nce's and
3760 		 * ire's that match on addr
3761 		 */
3762 		nce_clookup_t cl;
3763 
3764 		cl.ncecl_addr = addr;
3765 		cl.ncecl_found = B_FALSE;
3766 
3767 		ndp_walk_common(ipst->ips_ndp4, NULL,
3768 		    (pfi_t)ip_nce_clookup_and_delete, (uchar_t *)&cl, B_TRUE);
3769 
3770 		/*
3771 		 *  ncecl_found would be set by ip_nce_clookup_and_delete if
3772 		 *  we found a matching nce.
3773 		 */
3774 		return (cl.ncecl_found);
3775 	}
3776 	return (B_FALSE);
3777 
3778 }
3779 
3780 /* Delete the supplied nce if its nce_addr matches the supplied address */
3781 static void
3782 ip_nce_clookup_and_delete(nce_t *nce, void *arg)
3783 {
3784 	nce_clookup_t *cl = (nce_clookup_t *)arg;
3785 	ipaddr_t nce_addr;
3786 
3787 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr);
3788 	if (nce_addr == cl->ncecl_addr) {
3789 		cl->ncecl_found = B_TRUE;
3790 		/* clean up the nce (and any relevant ire's) */
3791 		ndp_delete(nce);
3792 	}
3793 }
3794 
3795 /*
3796  * Clean up the radix node for this ire. Must be called by IRB_REFRELE
3797  * when there are no ire's left in the bucket. Returns TRUE if the bucket
3798  * is deleted and freed.
3799  */
3800 boolean_t
3801 irb_inactive(irb_t *irb)
3802 {
3803 	struct rt_entry *rt;
3804 	struct radix_node *rn;
3805 	ip_stack_t *ipst = irb->irb_ipst;
3806 
3807 	ASSERT(irb->irb_ipst != NULL);
3808 
3809 	rt = IRB2RT(irb);
3810 	rn = (struct radix_node *)rt;
3811 
3812 	/* first remove it from the radix tree. */
3813 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
3814 	rw_enter(&irb->irb_lock, RW_WRITER);
3815 	if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
3816 		rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
3817 		    ipst->ips_ip_ftable);
3818 		DTRACE_PROBE1(irb__free, rt_t *,  rt);
3819 		ASSERT((void *)rn == (void *)rt);
3820 		Free(rt, rt_entry_cache);
3821 		/* irb_lock is freed */
3822 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3823 		return (B_TRUE);
3824 	}
3825 	rw_exit(&irb->irb_lock);
3826 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3827 	return (B_FALSE);
3828 }
3829 
3830 /*
3831  * Delete the specified IRE.
3832  */
3833 void
3834 ire_delete(ire_t *ire)
3835 {
3836 	ire_t	*ire1;
3837 	ire_t	**ptpn;
3838 	irb_t *irb;
3839 	ip_stack_t	*ipst = ire->ire_ipst;
3840 
3841 	if ((irb = ire->ire_bucket) == NULL) {
3842 		/*
3843 		 * It was never inserted in the list. Should call REFRELE
3844 		 * to free this IRE.
3845 		 */
3846 		IRE_REFRELE_NOTR(ire);
3847 		return;
3848 	}
3849 
3850 	rw_enter(&irb->irb_lock, RW_WRITER);
3851 
3852 	if (irb->irb_rr_origin == ire) {
3853 		irb->irb_rr_origin = NULL;
3854 	}
3855 
3856 	/*
3857 	 * In case of V4 we might still be waiting for fastpath ack.
3858 	 */
3859 	if (ire->ire_ipversion == IPV4_VERSION &&
3860 	    (ire->ire_type == IRE_CACHE ||
3861 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL))) {
3862 		ASSERT(ire->ire_nce != NULL);
3863 		nce_fastpath_list_delete(ire->ire_nce);
3864 	}
3865 
3866 	if (ire->ire_ptpn == NULL) {
3867 		/*
3868 		 * Some other thread has removed us from the list.
3869 		 * It should have done the REFRELE for us.
3870 		 */
3871 		rw_exit(&irb->irb_lock);
3872 		return;
3873 	}
3874 
3875 	if (irb->irb_refcnt != 0) {
3876 		/*
3877 		 * The last thread to leave this bucket will
3878 		 * delete this ire.
3879 		 */
3880 		if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
3881 			irb->irb_ire_cnt--;
3882 			if (ire->ire_marks & IRE_MARK_TEMPORARY)
3883 				irb->irb_tmp_ire_cnt--;
3884 			ire->ire_marks |= IRE_MARK_CONDEMNED;
3885 		}
3886 		irb->irb_marks |= IRB_MARK_CONDEMNED;
3887 		rw_exit(&irb->irb_lock);
3888 		return;
3889 	}
3890 
3891 	/*
3892 	 * Normally to delete an ire, we walk the bucket. While we
3893 	 * walk the bucket, we normally bump up irb_refcnt and hence
3894 	 * we return from above where we mark CONDEMNED and the ire
3895 	 * gets deleted from ire_unlink. This case is where somebody
3896 	 * knows the ire e.g by doing a lookup, and wants to delete the
3897 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
3898 	 * the bucket.
3899 	 */
3900 	ptpn = ire->ire_ptpn;
3901 	ire1 = ire->ire_next;
3902 	if (ire1 != NULL)
3903 		ire1->ire_ptpn = ptpn;
3904 	ASSERT(ptpn != NULL);
3905 	*ptpn = ire1;
3906 	ire->ire_ptpn = NULL;
3907 	ire->ire_next = NULL;
3908 	if (ire->ire_ipversion == IPV6_VERSION) {
3909 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
3910 	} else {
3911 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
3912 	}
3913 	/*
3914 	 * ip_wput/ip_wput_v6 checks this flag to see whether
3915 	 * it should still use the cached ire or not.
3916 	 */
3917 	ire->ire_marks |= IRE_MARK_CONDEMNED;
3918 	if (ire->ire_type == IRE_DEFAULT) {
3919 		/*
3920 		 * IRE is out of the list. We need to adjust the
3921 		 * accounting before we drop the lock.
3922 		 */
3923 		if (ire->ire_ipversion == IPV6_VERSION) {
3924 			ASSERT(ipst->ips_ipv6_ire_default_count != 0);
3925 			ipst->ips_ipv6_ire_default_count--;
3926 		}
3927 	}
3928 	irb->irb_ire_cnt--;
3929 
3930 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
3931 		irb->irb_tmp_ire_cnt--;
3932 	rw_exit(&irb->irb_lock);
3933 
3934 	if (ire->ire_ipversion == IPV6_VERSION) {
3935 		ire_delete_v6(ire);
3936 	} else {
3937 		ire_delete_v4(ire);
3938 	}
3939 	/*
3940 	 * We removed it from the list. Decrement the
3941 	 * reference count.
3942 	 */
3943 	IRE_REFRELE_NOTR(ire);
3944 }
3945 
3946 /*
3947  * Delete the specified IRE.
3948  * All calls should use ire_delete().
3949  * Sometimes called as writer though not required by this function.
3950  *
3951  * NOTE : This function is called only if the ire was added
3952  * in the list.
3953  */
3954 static void
3955 ire_delete_v4(ire_t *ire)
3956 {
3957 	ip_stack_t	*ipst = ire->ire_ipst;
3958 
3959 	ASSERT(ire->ire_refcnt >= 1);
3960 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
3961 
3962 	if (ire->ire_type != IRE_CACHE)
3963 		ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
3964 	if (ire->ire_type == IRE_DEFAULT) {
3965 		/*
3966 		 * when a default gateway is going away
3967 		 * delete all the host redirects pointing at that
3968 		 * gateway.
3969 		 */
3970 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
3971 	}
3972 }
3973 
3974 /*
3975  * IRE_REFRELE/ire_refrele are the only caller of the function. It calls
3976  * to free the ire when the reference count goes to zero.
3977  */
3978 void
3979 ire_inactive(ire_t *ire)
3980 {
3981 	nce_t	*nce;
3982 	ill_t	*ill = NULL;
3983 	ill_t	*stq_ill = NULL;
3984 	ill_t	*in_ill = NULL;
3985 	ipif_t	*ipif;
3986 	boolean_t	need_wakeup = B_FALSE;
3987 	irb_t 	*irb;
3988 	ip_stack_t	*ipst = ire->ire_ipst;
3989 
3990 	ASSERT(ire->ire_refcnt == 0);
3991 	ASSERT(ire->ire_ptpn == NULL);
3992 	ASSERT(ire->ire_next == NULL);
3993 
3994 	if (ire->ire_gw_secattr != NULL) {
3995 		ire_gw_secattr_free(ire->ire_gw_secattr);
3996 		ire->ire_gw_secattr = NULL;
3997 	}
3998 
3999 	if (ire->ire_mp != NULL) {
4000 		ASSERT(ire->ire_bucket == NULL);
4001 		mutex_destroy(&ire->ire_lock);
4002 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
4003 		if (ire->ire_nce != NULL)
4004 			NCE_REFRELE_NOTR(ire->ire_nce);
4005 		freeb(ire->ire_mp);
4006 		return;
4007 	}
4008 
4009 	if ((nce = ire->ire_nce) != NULL) {
4010 		NCE_REFRELE_NOTR(nce);
4011 		ire->ire_nce = NULL;
4012 	}
4013 
4014 	if (ire->ire_ipif == NULL)
4015 		goto end;
4016 
4017 	ipif = ire->ire_ipif;
4018 	ill = ipif->ipif_ill;
4019 
4020 	if (ire->ire_bucket == NULL) {
4021 		/* The ire was never inserted in the table. */
4022 		goto end;
4023 	}
4024 
4025 	/*
4026 	 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is
4027 	 * non-null ill_ire_count also goes down by 1. If the in_ill is
4028 	 * non-null either ill_mrtun_refcnt or ill_srcif_refcnt goes down by 1.
4029 	 *
4030 	 * The ipif that is associated with an ire is ire->ire_ipif and
4031 	 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call
4032 	 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as
4033 	 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only
4034 	 * in the case of IRE_CACHES when IPMP is used, stq_ill can be
4035 	 * different. If this is different from ire->ire_ipif->ipif_ill and
4036 	 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call
4037 	 * ipif_ill_refrele_tail on the stq_ill. If mobile ip is in use
4038 	 * in_ill could be non-null. If it is a reverse tunnel related ire
4039 	 * ill_mrtun_refcnt is non-zero. If it is forward tunnel related ire
4040 	 * ill_srcif_refcnt is non-null.
4041 	 */
4042 
4043 	if (ire->ire_stq != NULL)
4044 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
4045 	if (ire->ire_in_ill != NULL)
4046 		in_ill = ire->ire_in_ill;
4047 
4048 	if ((stq_ill == NULL || stq_ill == ill) && (in_ill == NULL)) {
4049 		/* Optimize the most common case */
4050 		mutex_enter(&ill->ill_lock);
4051 		ASSERT(ipif->ipif_ire_cnt != 0);
4052 		ipif->ipif_ire_cnt--;
4053 		if (ipif->ipif_ire_cnt == 0)
4054 			need_wakeup = B_TRUE;
4055 		if (stq_ill != NULL) {
4056 			ASSERT(stq_ill->ill_ire_cnt != 0);
4057 			stq_ill->ill_ire_cnt--;
4058 			if (stq_ill->ill_ire_cnt == 0)
4059 				need_wakeup = B_TRUE;
4060 		}
4061 		if (need_wakeup) {
4062 			/* Drops the ill lock */
4063 			ipif_ill_refrele_tail(ill);
4064 		} else {
4065 			mutex_exit(&ill->ill_lock);
4066 		}
4067 	} else {
4068 		/*
4069 		 * We can't grab all the ill locks at the same time.
4070 		 * It can lead to recursive lock enter in the call to
4071 		 * ipif_ill_refrele_tail and later. Instead do it 1 at
4072 		 * a time.
4073 		 */
4074 		mutex_enter(&ill->ill_lock);
4075 		ASSERT(ipif->ipif_ire_cnt != 0);
4076 		ipif->ipif_ire_cnt--;
4077 		if (ipif->ipif_ire_cnt == 0) {
4078 			/* Drops the lock */
4079 			ipif_ill_refrele_tail(ill);
4080 		} else {
4081 			mutex_exit(&ill->ill_lock);
4082 		}
4083 		if (stq_ill != NULL) {
4084 			mutex_enter(&stq_ill->ill_lock);
4085 			ASSERT(stq_ill->ill_ire_cnt != 0);
4086 			stq_ill->ill_ire_cnt--;
4087 			if (stq_ill->ill_ire_cnt == 0)  {
4088 				/* Drops the ill lock */
4089 				ipif_ill_refrele_tail(stq_ill);
4090 			} else {
4091 				mutex_exit(&stq_ill->ill_lock);
4092 			}
4093 		}
4094 		if (in_ill != NULL) {
4095 			mutex_enter(&in_ill->ill_lock);
4096 			if (ire->ire_type == IRE_MIPRTUN) {
4097 				/*
4098 				 * Mobile IP reverse tunnel ire.
4099 				 * Decrement table count and the
4100 				 * ill reference count. This signifies
4101 				 * mipagent is deleting reverse tunnel
4102 				 * route for a particular mobile node.
4103 				 */
4104 				mutex_enter(&ipst->ips_ire_mrtun_lock);
4105 				ipst->ips_ire_mrtun_count--;
4106 				mutex_exit(&ipst->ips_ire_mrtun_lock);
4107 				ASSERT(in_ill->ill_mrtun_refcnt != 0);
4108 				in_ill->ill_mrtun_refcnt--;
4109 				if (in_ill->ill_mrtun_refcnt == 0) {
4110 					/* Drops the ill lock */
4111 					ipif_ill_refrele_tail(in_ill);
4112 				} else {
4113 					mutex_exit(&in_ill->ill_lock);
4114 				}
4115 			} else {
4116 				mutex_enter(&ipst->ips_ire_srcif_table_lock);
4117 				ipst->ips_ire_srcif_table_count--;
4118 				mutex_exit(&ipst->ips_ire_srcif_table_lock);
4119 				ASSERT(in_ill->ill_srcif_refcnt != 0);
4120 				in_ill->ill_srcif_refcnt--;
4121 				if (in_ill->ill_srcif_refcnt == 0) {
4122 					/* Drops the ill lock */
4123 					ipif_ill_refrele_tail(in_ill);
4124 				} else {
4125 					mutex_exit(&in_ill->ill_lock);
4126 				}
4127 			}
4128 		}
4129 	}
4130 end:
4131 	/* This should be true for both V4 and V6 */
4132 
4133 	if ((ire->ire_type & IRE_FORWARDTABLE) &&
4134 	    (ire->ire_ipversion == IPV4_VERSION) &&
4135 	    ((irb = ire->ire_bucket) != NULL)) {
4136 		rw_enter(&irb->irb_lock, RW_WRITER);
4137 		irb->irb_nire--;
4138 		/*
4139 		 * Instead of examining the conditions for freeing
4140 		 * the radix node here, we do it by calling
4141 		 * IRB_REFRELE which is a single point in the code
4142 		 * that embeds that logic. Bump up the refcnt to
4143 		 * be able to call IRB_REFRELE
4144 		 */
4145 		IRB_REFHOLD_LOCKED(irb);
4146 		rw_exit(&irb->irb_lock);
4147 		IRB_REFRELE(irb);
4148 	}
4149 	ire->ire_ipif = NULL;
4150 
4151 	if (ire->ire_in_ill != NULL) {
4152 		ire->ire_in_ill = NULL;
4153 	}
4154 
4155 #ifdef IRE_DEBUG
4156 	ire_trace_inactive(ire);
4157 #endif
4158 	mutex_destroy(&ire->ire_lock);
4159 	if (ire->ire_ipversion == IPV6_VERSION) {
4160 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
4161 	} else {
4162 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
4163 	}
4164 	ASSERT(ire->ire_mp == NULL);
4165 	/* Has been allocated out of the cache */
4166 	kmem_cache_free(ire_cache, ire);
4167 }
4168 
4169 /*
4170  * ire_walk routine to delete all IRE_CACHE/IRE_HOST types redirect
4171  * entries that have a given gateway address.
4172  */
4173 void
4174 ire_delete_cache_gw(ire_t *ire, char *cp)
4175 {
4176 	ipaddr_t	gw_addr;
4177 
4178 	if (!(ire->ire_type & IRE_CACHE) &&
4179 	    !(ire->ire_flags & RTF_DYNAMIC))
4180 		return;
4181 
4182 	bcopy(cp, &gw_addr, sizeof (gw_addr));
4183 	if (ire->ire_gateway_addr == gw_addr) {
4184 		ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n",
4185 			(int)ntohl(ire->ire_addr), ire->ire_type,
4186 			(int)ntohl(ire->ire_gateway_addr)));
4187 		ire_delete(ire);
4188 	}
4189 }
4190 
4191 /*
4192  * Remove all IRE_CACHE entries that match the ire specified.
4193  *
4194  * The flag argument indicates if the flush request is due to addition
4195  * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE).
4196  *
4197  * This routine takes only the IREs from the forwarding table and flushes
4198  * the corresponding entries from the cache table.
4199  *
4200  * When flushing due to the deletion of an old route, it
4201  * just checks the cache handles (ire_phandle and ire_ihandle) and
4202  * deletes the ones that match.
4203  *
4204  * When flushing due to the creation of a new route, it checks
4205  * if a cache entry's address matches the one in the IRE and
4206  * that the cache entry's parent has a less specific mask than the
4207  * one in IRE. The destination of such a cache entry could be the
4208  * gateway for other cache entries, so we need to flush those as
4209  * well by looking for gateway addresses matching the IRE's address.
4210  */
4211 void
4212 ire_flush_cache_v4(ire_t *ire, int flag)
4213 {
4214 	int i;
4215 	ire_t *cire;
4216 	irb_t *irb;
4217 	ip_stack_t	*ipst = ire->ire_ipst;
4218 
4219 	if (ire->ire_type & IRE_CACHE)
4220 	    return;
4221 
4222 	/*
4223 	 * If a default is just created, there is no point
4224 	 * in going through the cache, as there will not be any
4225 	 * cached ires.
4226 	 */
4227 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD)
4228 		return;
4229 	if (flag == IRE_FLUSH_ADD) {
4230 		/*
4231 		 * This selective flush is due to the addition of
4232 		 * new IRE.
4233 		 */
4234 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4235 			irb = &ipst->ips_ip_cache_table[i];
4236 			if ((cire = irb->irb_ire) == NULL)
4237 				continue;
4238 			IRB_REFHOLD(irb);
4239 			for (cire = irb->irb_ire; cire != NULL;
4240 			    cire = cire->ire_next) {
4241 				if (cire->ire_type != IRE_CACHE)
4242 					continue;
4243 				/*
4244 				 * If 'cire' belongs to the same subnet
4245 				 * as the new ire being added, and 'cire'
4246 				 * is derived from a prefix that is less
4247 				 * specific than the new ire being added,
4248 				 * we need to flush 'cire'; for instance,
4249 				 * when a new interface comes up.
4250 				 */
4251 				if (((cire->ire_addr & ire->ire_mask) ==
4252 				    (ire->ire_addr & ire->ire_mask)) &&
4253 				    (ip_mask_to_plen(cire->ire_cmask) <=
4254 				    ire->ire_masklen)) {
4255 					ire_delete(cire);
4256 					continue;
4257 				}
4258 				/*
4259 				 * This is the case when the ire_gateway_addr
4260 				 * of 'cire' belongs to the same subnet as
4261 				 * the new ire being added.
4262 				 * Flushing such ires is sometimes required to
4263 				 * avoid misrouting: say we have a machine with
4264 				 * two interfaces (I1 and I2), a default router
4265 				 * R on the I1 subnet, and a host route to an
4266 				 * off-link destination D with a gateway G on
4267 				 * the I2 subnet.
4268 				 * Under normal operation, we will have an
4269 				 * on-link cache entry for G and an off-link
4270 				 * cache entry for D with G as ire_gateway_addr,
4271 				 * traffic to D will reach its destination
4272 				 * through gateway G.
4273 				 * If the administrator does 'ifconfig I2 down',
4274 				 * the cache entries for D and G will be
4275 				 * flushed. However, G will now be resolved as
4276 				 * an off-link destination using R (the default
4277 				 * router) as gateway. Then D will also be
4278 				 * resolved as an off-link destination using G
4279 				 * as gateway - this behavior is due to
4280 				 * compatibility reasons, see comment in
4281 				 * ire_ihandle_lookup_offlink(). Traffic to D
4282 				 * will go to the router R and probably won't
4283 				 * reach the destination.
4284 				 * The administrator then does 'ifconfig I2 up'.
4285 				 * Since G is on the I2 subnet, this routine
4286 				 * will flush its cache entry. It must also
4287 				 * flush the cache entry for D, otherwise
4288 				 * traffic will stay misrouted until the IRE
4289 				 * times out.
4290 				 */
4291 				if ((cire->ire_gateway_addr & ire->ire_mask) ==
4292 				    (ire->ire_addr & ire->ire_mask)) {
4293 					ire_delete(cire);
4294 					continue;
4295 				}
4296 			}
4297 			IRB_REFRELE(irb);
4298 		}
4299 	} else {
4300 		/*
4301 		 * delete the cache entries based on
4302 		 * handle in the IRE as this IRE is
4303 		 * being deleted/changed.
4304 		 */
4305 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4306 			irb = &ipst->ips_ip_cache_table[i];
4307 			if ((cire = irb->irb_ire) == NULL)
4308 				continue;
4309 			IRB_REFHOLD(irb);
4310 			for (cire = irb->irb_ire; cire != NULL;
4311 			    cire = cire->ire_next) {
4312 				if (cire->ire_type != IRE_CACHE)
4313 					continue;
4314 				if ((cire->ire_phandle == 0 ||
4315 				    cire->ire_phandle != ire->ire_phandle) &&
4316 				    (cire->ire_ihandle == 0 ||
4317 				    cire->ire_ihandle != ire->ire_ihandle))
4318 					continue;
4319 				ire_delete(cire);
4320 			}
4321 			IRB_REFRELE(irb);
4322 		}
4323 	}
4324 }
4325 
4326 /*
4327  * Matches the arguments passed with the values in the ire.
4328  *
4329  * Note: for match types that match using "ipif" passed in, ipif
4330  * must be checked for non-NULL before calling this routine.
4331  */
4332 boolean_t
4333 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
4334     int type, const ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle,
4335     const ts_label_t *tsl, int match_flags)
4336 {
4337 	ill_t *ire_ill = NULL, *dst_ill;
4338 	ill_t *ipif_ill = NULL;
4339 	ill_group_t *ire_ill_group = NULL;
4340 	ill_group_t *ipif_ill_group = NULL;
4341 
4342 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
4343 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
4344 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) ||
4345 	    (ipif != NULL && !ipif->ipif_isv6));
4346 	ASSERT(!(match_flags & MATCH_IRE_WQ));
4347 
4348 	/*
4349 	 * HIDDEN cache entries have to be looked up specifically with
4350 	 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set
4351 	 * when the interface is FAILED or INACTIVE. In that case,
4352 	 * any IRE_CACHES that exists should be marked with
4353 	 * IRE_MARK_HIDDEN. So, we don't really need to match below
4354 	 * for IRE_MARK_HIDDEN. But we do so for consistency.
4355 	 */
4356 	if (!(match_flags & MATCH_IRE_MARK_HIDDEN) &&
4357 	    (ire->ire_marks & IRE_MARK_HIDDEN))
4358 		return (B_FALSE);
4359 
4360 	/*
4361 	 * MATCH_IRE_MARK_PRIVATE_ADDR is set when IP_NEXTHOP option
4362 	 * is used. In that case the routing table is bypassed and the
4363 	 * packets are sent directly to the specified nexthop. The
4364 	 * IRE_CACHE entry representing this route should be marked
4365 	 * with IRE_MARK_PRIVATE_ADDR.
4366 	 */
4367 
4368 	if (!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR) &&
4369 	    (ire->ire_marks & IRE_MARK_PRIVATE_ADDR))
4370 		return (B_FALSE);
4371 
4372 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
4373 	    ire->ire_zoneid != ALL_ZONES) {
4374 		/*
4375 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is
4376 		 * valid and does not match that of ire_zoneid, a failure to
4377 		 * match is reported at this point. Otherwise, since some IREs
4378 		 * that are available in the global zone can be used in local
4379 		 * zones, additional checks need to be performed:
4380 		 *
4381 		 *	IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK
4382 		 *	entries should never be matched in this situation.
4383 		 *
4384 		 *	IRE entries that have an interface associated with them
4385 		 *	should in general not match unless they are an IRE_LOCAL
4386 		 *	or in the case when MATCH_IRE_DEFAULT has been set in
4387 		 *	the caller.  In the case of the former, checking of the
4388 		 *	other fields supplied should take place.
4389 		 *
4390 		 *	In the case where MATCH_IRE_DEFAULT has been set,
4391 		 *	all of the ipif's associated with the IRE's ill are
4392 		 *	checked to see if there is a matching zoneid.  If any
4393 		 *	one ipif has a matching zoneid, this IRE is a
4394 		 *	potential candidate so checking of the other fields
4395 		 *	takes place.
4396 		 *
4397 		 *	In the case where the IRE_INTERFACE has a usable source
4398 		 *	address (indicated by ill_usesrc_ifindex) in the
4399 		 *	correct zone then it's permitted to return this IRE
4400 		 */
4401 		if (match_flags & MATCH_IRE_ZONEONLY)
4402 			return (B_FALSE);
4403 		if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK))
4404 			return (B_FALSE);
4405 		/*
4406 		 * Note, IRE_INTERFACE can have the stq as NULL. For
4407 		 * example, if the default multicast route is tied to
4408 		 * the loopback address.
4409 		 */
4410 		if ((ire->ire_type & IRE_INTERFACE) &&
4411 		    (ire->ire_stq != NULL)) {
4412 			dst_ill = (ill_t *)ire->ire_stq->q_ptr;
4413 			/*
4414 			 * If there is a usable source address in the
4415 			 * zone, then it's ok to return an
4416 			 * IRE_INTERFACE
4417 			 */
4418 			if (ipif_usesrc_avail(dst_ill, zoneid)) {
4419 				ip3dbg(("ire_match_args: dst_ill %p match %d\n",
4420 				    (void *)dst_ill,
4421 				    (ire->ire_addr == (addr & mask))));
4422 			} else {
4423 				ip3dbg(("ire_match_args: src_ipif NULL"
4424 				    " dst_ill %p\n", (void *)dst_ill));
4425 				return (B_FALSE);
4426 			}
4427 		}
4428 		if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL &&
4429 		    !(ire->ire_type & IRE_INTERFACE)) {
4430 			ipif_t	*tipif;
4431 
4432 			if ((match_flags & MATCH_IRE_DEFAULT) == 0) {
4433 				return (B_FALSE);
4434 			}
4435 			mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock);
4436 			for (tipif = ire->ire_ipif->ipif_ill->ill_ipif;
4437 			    tipif != NULL; tipif = tipif->ipif_next) {
4438 				if (IPIF_CAN_LOOKUP(tipif) &&
4439 				    (tipif->ipif_flags & IPIF_UP) &&
4440 				    (tipif->ipif_zoneid == zoneid ||
4441 				    tipif->ipif_zoneid == ALL_ZONES))
4442 					break;
4443 			}
4444 			mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock);
4445 			if (tipif == NULL) {
4446 				return (B_FALSE);
4447 			}
4448 		}
4449 	}
4450 
4451 	/*
4452 	 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that
4453 	 * somebody wants to send out on a particular interface which
4454 	 * is given by ire_stq and hence use ire_stq to derive the ill
4455 	 * value. ire_ipif for IRE_CACHES is just the means of getting
4456 	 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr.
4457 	 * ire_to_ill does the right thing for this.
4458 	 */
4459 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
4460 		ire_ill = ire_to_ill(ire);
4461 		if (ire_ill != NULL)
4462 			ire_ill_group = ire_ill->ill_group;
4463 		ipif_ill = ipif->ipif_ill;
4464 		ipif_ill_group = ipif_ill->ill_group;
4465 	}
4466 
4467 	if ((ire->ire_addr == (addr & mask)) &&
4468 	    ((!(match_flags & MATCH_IRE_GW)) ||
4469 		(ire->ire_gateway_addr == gateway)) &&
4470 	    ((!(match_flags & MATCH_IRE_TYPE)) ||
4471 		(ire->ire_type & type)) &&
4472 	    ((!(match_flags & MATCH_IRE_SRC)) ||
4473 		(ire->ire_src_addr == ipif->ipif_src_addr)) &&
4474 	    ((!(match_flags & MATCH_IRE_IPIF)) ||
4475 		(ire->ire_ipif == ipif)) &&
4476 	    ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) ||
4477 		(ire->ire_type != IRE_CACHE ||
4478 		ire->ire_marks & IRE_MARK_HIDDEN)) &&
4479 	    ((!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR)) ||
4480 		(ire->ire_type != IRE_CACHE ||
4481 		ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) &&
4482 	    ((!(match_flags & MATCH_IRE_ILL)) ||
4483 		(ire_ill == ipif_ill)) &&
4484 	    ((!(match_flags & MATCH_IRE_IHANDLE)) ||
4485 		(ire->ire_ihandle == ihandle)) &&
4486 	    ((!(match_flags & MATCH_IRE_MASK)) ||
4487 		(ire->ire_mask == mask)) &&
4488 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
4489 		(ire_ill == ipif_ill) ||
4490 		(ire_ill_group != NULL &&
4491 		ire_ill_group == ipif_ill_group)) &&
4492 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
4493 		(!is_system_labeled()) ||
4494 		(tsol_ire_match_gwattr(ire, tsl) == 0))) {
4495 		/* We found the matched IRE */
4496 		return (B_TRUE);
4497 	}
4498 	return (B_FALSE);
4499 }
4500 
4501 
4502 /*
4503  * Lookup for a route in all the tables
4504  */
4505 ire_t *
4506 ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
4507     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
4508     const ts_label_t *tsl, int flags, ip_stack_t *ipst)
4509 {
4510 	ire_t *ire = NULL;
4511 
4512 	/*
4513 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
4514 	 * MATCH_IRE_ILL is set.
4515 	 */
4516 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
4517 	    (ipif == NULL))
4518 		return (NULL);
4519 
4520 	/*
4521 	 * might be asking for a cache lookup,
4522 	 * This is not best way to lookup cache,
4523 	 * user should call ire_cache_lookup directly.
4524 	 *
4525 	 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then
4526 	 * in the forwarding table, if the applicable type flags were set.
4527 	 */
4528 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) {
4529 		ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid,
4530 		    tsl, flags, ipst);
4531 		if (ire != NULL)
4532 			return (ire);
4533 	}
4534 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) {
4535 		ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire,
4536 		    zoneid, 0, tsl, flags, ipst);
4537 	}
4538 	return (ire);
4539 }
4540 
4541 
4542 /*
4543  * Delete the IRE cache for the gateway and all IRE caches whose
4544  * ire_gateway_addr points to this gateway, and allow them to
4545  * be created on demand by ip_newroute.
4546  */
4547 void
4548 ire_clookup_delete_cache_gw(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
4549 {
4550 	irb_t *irb;
4551 	ire_t *ire;
4552 
4553 	irb = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4554 	    ipst->ips_ip_cache_table_size)];
4555 	IRB_REFHOLD(irb);
4556 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
4557 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
4558 			continue;
4559 
4560 		ASSERT(ire->ire_mask == IP_HOST_MASK);
4561 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
4562 		if (ire_match_args(ire, addr, ire->ire_mask, 0, IRE_CACHE,
4563 		    NULL, zoneid, 0, NULL, MATCH_IRE_TYPE)) {
4564 			ire_delete(ire);
4565 		}
4566 	}
4567 	IRB_REFRELE(irb);
4568 
4569 	ire_walk_v4(ire_delete_cache_gw, &addr, zoneid, ipst);
4570 }
4571 
4572 /*
4573  * Looks up cache table for a route.
4574  * specific lookup can be indicated by
4575  * passing the MATCH_* flags and the
4576  * necessary parameters.
4577  */
4578 ire_t *
4579 ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, const ipif_t *ipif,
4580     zoneid_t zoneid, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
4581 {
4582 	irb_t *irb_ptr;
4583 	ire_t *ire;
4584 
4585 	/*
4586 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
4587 	 * MATCH_IRE_ILL is set.
4588 	 */
4589 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
4590 	    (ipif == NULL))
4591 		return (NULL);
4592 
4593 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4594 					    ipst->ips_ip_cache_table_size)];
4595 	rw_enter(&irb_ptr->irb_lock, RW_READER);
4596 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
4597 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
4598 			continue;
4599 		ASSERT(ire->ire_mask == IP_HOST_MASK);
4600 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
4601 		if (ire_match_args(ire, addr, ire->ire_mask, gateway, type,
4602 		    ipif, zoneid, 0, tsl, flags)) {
4603 			IRE_REFHOLD(ire);
4604 			rw_exit(&irb_ptr->irb_lock);
4605 			return (ire);
4606 		}
4607 	}
4608 	rw_exit(&irb_ptr->irb_lock);
4609 	return (NULL);
4610 }
4611 
4612 /*
4613  * Check whether the IRE_LOCAL and the IRE potentially used to transmit
4614  * (could be an IRE_CACHE, IRE_BROADCAST, or IRE_INTERFACE) are part of
4615  * the same ill group.
4616  */
4617 boolean_t
4618 ire_local_same_ill_group(ire_t *ire_local, ire_t *xmit_ire)
4619 {
4620 	ill_t		*recv_ill, *xmit_ill;
4621 	ill_group_t	*recv_group, *xmit_group;
4622 
4623 	ASSERT(ire_local->ire_type & (IRE_LOCAL|IRE_LOOPBACK));
4624 	ASSERT(xmit_ire->ire_type & (IRE_CACHETABLE|IRE_INTERFACE));
4625 
4626 	recv_ill = ire_to_ill(ire_local);
4627 	xmit_ill = ire_to_ill(xmit_ire);
4628 
4629 	ASSERT(recv_ill != NULL);
4630 	ASSERT(xmit_ill != NULL);
4631 
4632 	if (recv_ill == xmit_ill)
4633 		return (B_TRUE);
4634 
4635 	recv_group = recv_ill->ill_group;
4636 	xmit_group = xmit_ill->ill_group;
4637 
4638 	if (recv_group != NULL && recv_group == xmit_group)
4639 		return (B_TRUE);
4640 
4641 	return (B_FALSE);
4642 }
4643 
4644 /*
4645  * Check if the IRE_LOCAL uses the same ill (group) as another route would use.
4646  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
4647  * then we don't allow this IRE_LOCAL to be used.
4648  */
4649 boolean_t
4650 ire_local_ok_across_zones(ire_t *ire_local, zoneid_t zoneid, void *addr,
4651     const ts_label_t *tsl, ip_stack_t *ipst)
4652 {
4653 	ire_t		*alt_ire;
4654 	boolean_t	rval;
4655 
4656 	if (ire_local->ire_ipversion == IPV4_VERSION) {
4657 		alt_ire = ire_ftable_lookup(*((ipaddr_t *)addr), 0, 0, 0, NULL,
4658 		    NULL, zoneid, 0, tsl,
4659 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4660 		    MATCH_IRE_RJ_BHOLE, ipst);
4661 	} else {
4662 		alt_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
4663 		    0, NULL, NULL, zoneid, 0, tsl,
4664 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4665 		    MATCH_IRE_RJ_BHOLE, ipst);
4666 	}
4667 
4668 	if (alt_ire == NULL)
4669 		return (B_FALSE);
4670 
4671 	if (alt_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
4672 		ire_refrele(alt_ire);
4673 		return (B_FALSE);
4674 	}
4675 	rval = ire_local_same_ill_group(ire_local, alt_ire);
4676 
4677 	ire_refrele(alt_ire);
4678 	return (rval);
4679 }
4680 
4681 /*
4682  * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers
4683  * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get
4684  * to the hidden ones.
4685  *
4686  * In general the zoneid has to match (where ALL_ZONES match all of them).
4687  * But for IRE_LOCAL we also need to handle the case where L2 should
4688  * conceptually loop back the packet. This is necessary since neither
4689  * Ethernet drivers nor Ethernet hardware loops back packets sent to their
4690  * own MAC address. This loopback is needed when the normal
4691  * routes (ignoring IREs with different zoneids) would send out the packet on
4692  * the same ill (or ill group) as the ill with which this IRE_LOCAL is
4693  * associated.
4694  *
4695  * Earlier versions of this code always matched an IRE_LOCAL independently of
4696  * the zoneid. We preserve that earlier behavior when
4697  * ip_restrict_interzone_loopback is turned off.
4698  */
4699 ire_t *
4700 ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid, const ts_label_t *tsl,
4701     ip_stack_t *ipst)
4702 {
4703 	irb_t *irb_ptr;
4704 	ire_t *ire;
4705 
4706 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4707 					    ipst->ips_ip_cache_table_size)];
4708 	rw_enter(&irb_ptr->irb_lock, RW_READER);
4709 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
4710 		if (ire->ire_marks & (IRE_MARK_CONDEMNED |
4711 		    IRE_MARK_HIDDEN | IRE_MARK_PRIVATE_ADDR)) {
4712 			continue;
4713 		}
4714 		if (ire->ire_addr == addr) {
4715 			/*
4716 			 * Finally, check if the security policy has any
4717 			 * restriction on using this route for the specified
4718 			 * message.
4719 			 */
4720 			if (tsl != NULL &&
4721 			    ire->ire_gw_secattr != NULL &&
4722 			    tsol_ire_match_gwattr(ire, tsl) != 0) {
4723 				continue;
4724 			}
4725 
4726 			if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid ||
4727 			    ire->ire_zoneid == ALL_ZONES) {
4728 				IRE_REFHOLD(ire);
4729 				rw_exit(&irb_ptr->irb_lock);
4730 				return (ire);
4731 			}
4732 
4733 			if (ire->ire_type == IRE_LOCAL) {
4734 				if (ipst->ips_ip_restrict_interzone_loopback &&
4735 				    !ire_local_ok_across_zones(ire, zoneid,
4736 				    &addr, tsl, ipst))
4737 					continue;
4738 
4739 				IRE_REFHOLD(ire);
4740 				rw_exit(&irb_ptr->irb_lock);
4741 				return (ire);
4742 			}
4743 		}
4744 	}
4745 	rw_exit(&irb_ptr->irb_lock);
4746 	return (NULL);
4747 }
4748 
4749 /*
4750  * Locate the interface ire that is tied to the cache ire 'cire' via
4751  * cire->ire_ihandle.
4752  *
4753  * We are trying to create the cache ire for an offlink destn based
4754  * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire
4755  * as found by ip_newroute(). We are called from ip_newroute() in
4756  * the IRE_CACHE case.
4757  */
4758 ire_t *
4759 ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire)
4760 {
4761 	ire_t	*ire;
4762 	int	match_flags;
4763 	ipaddr_t gw_addr;
4764 	ipif_t	*gw_ipif;
4765 	ip_stack_t	*ipst = cire->ire_ipst;
4766 
4767 	ASSERT(cire != NULL && pire != NULL);
4768 
4769 	/*
4770 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
4771 	 * because the ihandle refers to an ipif which can be in only one zone.
4772 	 */
4773 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
4774 	/*
4775 	 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only
4776 	 * for on-link hosts. We should never be here for onlink.
4777 	 * Thus, use MATCH_IRE_ILL_GROUP.
4778 	 */
4779 	if (pire->ire_ipif != NULL)
4780 		match_flags |= MATCH_IRE_ILL_GROUP;
4781 	/*
4782 	 * We know that the mask of the interface ire equals cire->ire_cmask.
4783 	 * (When ip_newroute() created 'cire' for the gateway it set its
4784 	 * cmask from the interface ire's mask)
4785 	 */
4786 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
4787 	    IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle,
4788 	    NULL, match_flags, ipst);
4789 	if (ire != NULL)
4790 		return (ire);
4791 	/*
4792 	 * If we didn't find an interface ire above, we can't declare failure.
4793 	 * For backwards compatibility, we need to support prefix routes
4794 	 * pointing to next hop gateways that are not on-link.
4795 	 *
4796 	 * Assume we are trying to ping some offlink destn, and we have the
4797 	 * routing table below.
4798 	 *
4799 	 * Eg.	default	- gw1		<--- pire	(line 1)
4800 	 *	gw1	- gw2				(line 2)
4801 	 *	gw2	- hme0				(line 3)
4802 	 *
4803 	 * If we already have a cache ire for gw1 in 'cire', the
4804 	 * ire_ftable_lookup above would have failed, since there is no
4805 	 * interface ire to reach gw1. We will fallthru below.
4806 	 *
4807 	 * Here we duplicate the steps that ire_ftable_lookup() did in
4808 	 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case.
4809 	 * The differences are the following
4810 	 * i.   We want the interface ire only, so we call ire_ftable_lookup()
4811 	 *	instead of ire_route_lookup()
4812 	 * ii.  We look for only prefix routes in the 1st call below.
4813 	 * ii.  We want to match on the ihandle in the 2nd call below.
4814 	 */
4815 	match_flags =  MATCH_IRE_TYPE;
4816 	if (pire->ire_ipif != NULL)
4817 		match_flags |= MATCH_IRE_ILL_GROUP;
4818 	ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET,
4819 	    pire->ire_ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
4820 	if (ire == NULL)
4821 		return (NULL);
4822 	/*
4823 	 * At this point 'ire' corresponds to the entry shown in line 2.
4824 	 * gw_addr is 'gw2' in the example above.
4825 	 */
4826 	gw_addr = ire->ire_gateway_addr;
4827 	gw_ipif = ire->ire_ipif;
4828 	ire_refrele(ire);
4829 
4830 	match_flags |= MATCH_IRE_IHANDLE;
4831 	ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
4832 	    gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, NULL, match_flags,
4833 	    ipst);
4834 	return (ire);
4835 }
4836 
4837 /*
4838  * ire_mrtun_lookup() is called by ip_rput() when packet is to be
4839  * tunneled through reverse tunnel. This is only supported for
4840  * IPv4 packets
4841  */
4842 
4843 ire_t *
4844 ire_mrtun_lookup(ipaddr_t srcaddr, ill_t *ill)
4845 {
4846 	irb_t *irb_ptr;
4847 	ire_t *ire;
4848 	ip_stack_t	*ipst = ill->ill_ipst;
4849 
4850 	ASSERT(ill != NULL);
4851 	ASSERT(!(ill->ill_isv6));
4852 
4853 	if (ipst->ips_ip_mrtun_table == NULL)
4854 		return (NULL);
4855 	irb_ptr = &ipst->ips_ip_mrtun_table[IRE_ADDR_HASH(srcaddr,
4856 					    IP_MRTUN_TABLE_SIZE)];
4857 	rw_enter(&irb_ptr->irb_lock, RW_READER);
4858 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
4859 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
4860 			continue;
4861 		if ((ire->ire_in_src_addr == srcaddr) &&
4862 		    ire->ire_in_ill == ill) {
4863 			IRE_REFHOLD(ire);
4864 			rw_exit(&irb_ptr->irb_lock);
4865 			return (ire);
4866 		}
4867 	}
4868 	rw_exit(&irb_ptr->irb_lock);
4869 	return (NULL);
4870 }
4871 
4872 /*
4873  * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER
4874  * ire associated with the specified ipif.
4875  *
4876  * This might occasionally be called when IPIF_UP is not set since
4877  * the IP_MULTICAST_IF as well as creating interface routes
4878  * allows specifying a down ipif (ipif_lookup* match ipifs that are down).
4879  *
4880  * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on
4881  * the ipif, this routine might return NULL.
4882  */
4883 ire_t *
4884 ipif_to_ire(const ipif_t *ipif)
4885 {
4886 	ire_t	*ire;
4887 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
4888 
4889 	ASSERT(!ipif->ipif_isv6);
4890 	if (ipif->ipif_ire_type == IRE_LOOPBACK) {
4891 		ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK,
4892 		    ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
4893 		    ipst);
4894 	} else if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4895 		/* In this case we need to lookup destination address. */
4896 		ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0,
4897 		    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
4898 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK), ipst);
4899 	} else {
4900 		ire = ire_ftable_lookup(ipif->ipif_subnet,
4901 		    ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL,
4902 		    ALL_ZONES, 0, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF |
4903 		    MATCH_IRE_MASK), ipst);
4904 	}
4905 	return (ire);
4906 }
4907 
4908 /*
4909  * ire_walk function.
4910  * Count the number of IRE_CACHE entries in different categories.
4911  */
4912 void
4913 ire_cache_count(ire_t *ire, char *arg)
4914 {
4915 	ire_cache_count_t *icc = (ire_cache_count_t *)arg;
4916 
4917 	if (ire->ire_type != IRE_CACHE)
4918 		return;
4919 
4920 	icc->icc_total++;
4921 
4922 	if (ire->ire_ipversion == IPV6_VERSION) {
4923 		mutex_enter(&ire->ire_lock);
4924 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
4925 			mutex_exit(&ire->ire_lock);
4926 			icc->icc_onlink++;
4927 			return;
4928 		}
4929 		mutex_exit(&ire->ire_lock);
4930 	} else {
4931 		if (ire->ire_gateway_addr == 0) {
4932 			icc->icc_onlink++;
4933 			return;
4934 		}
4935 	}
4936 
4937 	ASSERT(ire->ire_ipif != NULL);
4938 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu)
4939 		icc->icc_pmtu++;
4940 	else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
4941 	    ire->ire_ib_pkt_count)
4942 		icc->icc_offlink++;
4943 	else
4944 		icc->icc_unused++;
4945 }
4946 
4947 /*
4948  * ire_walk function called by ip_trash_ire_reclaim().
4949  * Free a fraction of the IRE_CACHE cache entries. The fractions are
4950  * different for different categories of IRE_CACHE entries.
4951  * A fraction of zero means to not free any in that category.
4952  * Use the hash bucket id plus lbolt as a random number. Thus if the fraction
4953  * is N then every Nth hash bucket chain will be freed.
4954  */
4955 void
4956 ire_cache_reclaim(ire_t *ire, char *arg)
4957 {
4958 	ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg;
4959 	uint_t rand;
4960 	ip_stack_t	*ipst = icr->icr_ipst;
4961 
4962 	if (ire->ire_type != IRE_CACHE)
4963 		return;
4964 
4965 	if (ire->ire_ipversion == IPV6_VERSION) {
4966 		rand = (uint_t)lbolt +
4967 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6,
4968 			ipst->ips_ip6_cache_table_size);
4969 		mutex_enter(&ire->ire_lock);
4970 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
4971 			mutex_exit(&ire->ire_lock);
4972 			if (icr->icr_onlink != 0 &&
4973 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
4974 				ire_delete(ire);
4975 				return;
4976 			}
4977 			goto done;
4978 		}
4979 		mutex_exit(&ire->ire_lock);
4980 	} else {
4981 		rand = (uint_t)lbolt +
4982 		    IRE_ADDR_HASH(ire->ire_addr, ipst->ips_ip_cache_table_size);
4983 		if (ire->ire_gateway_addr == 0) {
4984 			if (icr->icr_onlink != 0 &&
4985 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
4986 				ire_delete(ire);
4987 				return;
4988 			}
4989 			goto done;
4990 		}
4991 	}
4992 	/* Not onlink IRE */
4993 	ASSERT(ire->ire_ipif != NULL);
4994 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) {
4995 		/* Use ptmu fraction */
4996 		if (icr->icr_pmtu != 0 &&
4997 		    (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) {
4998 			ire_delete(ire);
4999 			return;
5000 		}
5001 	} else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
5002 	    ire->ire_ib_pkt_count) {
5003 		/* Use offlink fraction */
5004 		if (icr->icr_offlink != 0 &&
5005 		    (rand/icr->icr_offlink)*icr->icr_offlink == rand) {
5006 			ire_delete(ire);
5007 			return;
5008 		}
5009 	} else {
5010 		/* Use unused fraction */
5011 		if (icr->icr_unused != 0 &&
5012 		    (rand/icr->icr_unused)*icr->icr_unused == rand) {
5013 			ire_delete(ire);
5014 			return;
5015 		}
5016 	}
5017 done:
5018 	/*
5019 	 * Update tire_mark so that those that haven't been used since this
5020 	 * reclaim will be considered unused next time we reclaim.
5021 	 */
5022 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
5023 }
5024 
5025 static void
5026 power2_roundup(uint32_t *value)
5027 {
5028 	int i;
5029 
5030 	for (i = 1; i < 31; i++) {
5031 		if (*value <= (1 << i))
5032 			break;
5033 	}
5034 	*value = (1 << i);
5035 }
5036 
5037 /* Global init for all zones */
5038 void
5039 ip_ire_g_init()
5040 {
5041 	/*
5042 	 * Create ire caches, ire_reclaim()
5043 	 * will give IRE_CACHE back to system when needed.
5044 	 * This needs to be done here before anything else, since
5045 	 * ire_add() expects the cache to be created.
5046 	 */
5047 	ire_cache = kmem_cache_create("ire_cache",
5048 		sizeof (ire_t), 0, ip_ire_constructor,
5049 		ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0);
5050 
5051 	rt_entry_cache = kmem_cache_create("rt_entry",
5052 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
5053 
5054 	/*
5055 	 * Have radix code setup kmem caches etc.
5056 	 */
5057 	rn_init();
5058 }
5059 
5060 void
5061 ip_ire_init(ip_stack_t *ipst)
5062 {
5063 	int i;
5064 	uint32_t mem_cnt;
5065 	uint32_t cpu_cnt;
5066 	uint32_t min_cnt;
5067 	pgcnt_t mem_avail;
5068 
5069 	/*
5070 	 * ip_ire_max_bucket_cnt is sized below based on the memory
5071 	 * size and the cpu speed of the machine. This is upper
5072 	 * bounded by the compile time value of ip_ire_max_bucket_cnt
5073 	 * and is lower bounded by the compile time value of
5074 	 * ip_ire_min_bucket_cnt.  Similar logic applies to
5075 	 * ip6_ire_max_bucket_cnt.
5076 	 *
5077 	 * We calculate this for each IP Instances in order to use
5078 	 * the kmem_avail and ip_ire_{min,max}_bucket_cnt that are
5079 	 * in effect when the zone is booted.
5080 	 */
5081 	mem_avail = kmem_avail();
5082 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
5083 	    ip_cache_table_size / sizeof (ire_t);
5084 	cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio;
5085 
5086 	min_cnt = MIN(cpu_cnt, mem_cnt);
5087 	if (min_cnt < ip_ire_min_bucket_cnt)
5088 		min_cnt = ip_ire_min_bucket_cnt;
5089 	if (ip_ire_max_bucket_cnt > min_cnt) {
5090 		ip_ire_max_bucket_cnt = min_cnt;
5091 	}
5092 
5093 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
5094 	    ip6_cache_table_size / sizeof (ire_t);
5095 	min_cnt = MIN(cpu_cnt, mem_cnt);
5096 	if (min_cnt < ip6_ire_min_bucket_cnt)
5097 		min_cnt = ip6_ire_min_bucket_cnt;
5098 	if (ip6_ire_max_bucket_cnt > min_cnt) {
5099 		ip6_ire_max_bucket_cnt = min_cnt;
5100 	}
5101 
5102 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
5103 	mutex_init(&ipst->ips_ire_handle_lock, NULL, MUTEX_DEFAULT, NULL);
5104 	mutex_init(&ipst->ips_ire_mrtun_lock, NULL, MUTEX_DEFAULT, NULL);
5105 	mutex_init(&ipst->ips_ire_srcif_table_lock, NULL, MUTEX_DEFAULT, NULL);
5106 
5107 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
5108 
5109 
5110 	/* Calculate the IPv4 cache table size. */
5111 	ipst->ips_ip_cache_table_size = MAX(ip_cache_table_size,
5112 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
5113 	    ip_ire_max_bucket_cnt));
5114 	if (ipst->ips_ip_cache_table_size > ip_max_cache_table_size)
5115 		ipst->ips_ip_cache_table_size = ip_max_cache_table_size;
5116 	/*
5117 	 * Make sure that the table size is always a power of 2.  The
5118 	 * hash macro IRE_ADDR_HASH() depends on that.
5119 	 */
5120 	power2_roundup(&ipst->ips_ip_cache_table_size);
5121 
5122 	ipst->ips_ip_cache_table = kmem_zalloc(ipst->ips_ip_cache_table_size *
5123 	    sizeof (irb_t), KM_SLEEP);
5124 
5125 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
5126 		rw_init(&ipst->ips_ip_cache_table[i].irb_lock, NULL,
5127 		    RW_DEFAULT, NULL);
5128 	}
5129 
5130 	/* Calculate the IPv6 cache table size. */
5131 	ipst->ips_ip6_cache_table_size = MAX(ip6_cache_table_size,
5132 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
5133 	    ip6_ire_max_bucket_cnt));
5134 	if (ipst->ips_ip6_cache_table_size > ip6_max_cache_table_size)
5135 		ipst->ips_ip6_cache_table_size = ip6_max_cache_table_size;
5136 	/*
5137 	 * Make sure that the table size is always a power of 2.  The
5138 	 * hash macro IRE_ADDR_HASH_V6() depends on that.
5139 	 */
5140 	power2_roundup(&ipst->ips_ip6_cache_table_size);
5141 
5142 	ipst->ips_ip_cache_table_v6 = kmem_zalloc(
5143 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t), KM_SLEEP);
5144 
5145 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
5146 		rw_init(&ipst->ips_ip_cache_table_v6[i].irb_lock, NULL,
5147 		    RW_DEFAULT, NULL);
5148 	}
5149 
5150 	/*
5151 	 * Initialize ip_mrtun_table to NULL now, it will be
5152 	 * populated by ip_rt_add if reverse tunnel is created
5153 	 */
5154 	ipst->ips_ip_mrtun_table = NULL;
5155 
5156 	/*
5157 	 * Make sure that the forwarding table size is a power of 2.
5158 	 * The IRE*_ADDR_HASH() macroes depend on that.
5159 	 */
5160 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
5161 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
5162 
5163 	ipst->ips_ire_handle = 1;
5164 }
5165 
5166 void
5167 ip_ire_g_fini(void)
5168 {
5169 	kmem_cache_destroy(ire_cache);
5170 	kmem_cache_destroy(rt_entry_cache);
5171 
5172 	rn_fini();
5173 }
5174 
5175 void
5176 ip_ire_fini(ip_stack_t *ipst)
5177 {
5178 	int i;
5179 
5180 	/*
5181 	 * Delete all IREs - assumes that the ill/ipifs have
5182 	 * been removed so what remains are just the ftable and IRE_CACHE.
5183 	 */
5184 	ire_walk_ill_mrtun(0, 0, ire_delete, NULL, NULL, ipst);
5185 	ire_walk(ire_delete, NULL, ipst);
5186 
5187 	rn_freehead(ipst->ips_ip_ftable);
5188 	ipst->ips_ip_ftable = NULL;
5189 
5190 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
5191 	mutex_destroy(&ipst->ips_ire_handle_lock);
5192 	mutex_destroy(&ipst->ips_ire_mrtun_lock);
5193 	mutex_destroy(&ipst->ips_ire_srcif_table_lock);
5194 
5195 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
5196 		ASSERT(ipst->ips_ip_cache_table[i].irb_ire == NULL);
5197 		rw_destroy(&ipst->ips_ip_cache_table[i].irb_lock);
5198 	}
5199 	kmem_free(ipst->ips_ip_cache_table,
5200 	    ipst->ips_ip_cache_table_size * sizeof (irb_t));
5201 	ipst->ips_ip_cache_table = NULL;
5202 
5203 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
5204 		ASSERT(ipst->ips_ip_cache_table_v6[i].irb_ire == NULL);
5205 		rw_destroy(&ipst->ips_ip_cache_table_v6[i].irb_lock);
5206 	}
5207 	kmem_free(ipst->ips_ip_cache_table_v6,
5208 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t));
5209 	ipst->ips_ip_cache_table_v6 = NULL;
5210 
5211 	if (ipst->ips_ip_mrtun_table != NULL) {
5212 		for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5213 			ASSERT(ipst->ips_ip_mrtun_table[i].irb_ire == NULL);
5214 			rw_destroy(&ipst->ips_ip_mrtun_table[i].irb_lock);
5215 		}
5216 		kmem_free(ipst->ips_ip_mrtun_table,
5217 		    IP_MRTUN_TABLE_SIZE * sizeof (irb_t));
5218 		ipst->ips_ip_mrtun_table = NULL;
5219 	}
5220 
5221 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
5222 		irb_t *ptr;
5223 		int j;
5224 
5225 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
5226 			continue;
5227 
5228 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
5229 			ASSERT(ptr[j].irb_ire == NULL);
5230 			rw_destroy(&ptr[j].irb_lock);
5231 		}
5232 		mi_free(ptr);
5233 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
5234 	}
5235 }
5236 
5237 int
5238 ire_add_mrtun(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
5239 {
5240 	ire_t   *ire1;
5241 	irb_t	*irb_ptr;
5242 	ire_t	**irep;
5243 	ire_t	*ire = *ire_p;
5244 	int	i;
5245 	uint_t	max_frag;
5246 	ill_t	*stq_ill;
5247 	int error;
5248 	ip_stack_t	*ipst = ire->ire_ipst;
5249 
5250 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5251 	/* Is ip_mrtun_table empty ? */
5252 
5253 	if (ipst->ips_ip_mrtun_table == NULL) {
5254 		/* create the mrtun table */
5255 		mutex_enter(&ipst->ips_ire_mrtun_lock);
5256 		if (ipst->ips_ip_mrtun_table == NULL) {
5257 			ipst->ips_ip_mrtun_table = kmem_zalloc(
5258 			    IP_MRTUN_TABLE_SIZE * sizeof (irb_t), KM_NOSLEEP);
5259 
5260 			if (ipst->ips_ip_mrtun_table == NULL) {
5261 				ip2dbg(("ire_add_mrtun: allocation failure\n"));
5262 				mutex_exit(&ipst->ips_ire_mrtun_lock);
5263 				ire_refrele(ire);
5264 				*ire_p = NULL;
5265 				return (ENOMEM);
5266 			}
5267 
5268 			for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5269 			    rw_init(&ipst->ips_ip_mrtun_table[i].irb_lock, NULL,
5270 				    RW_DEFAULT, NULL);
5271 			}
5272 			ip2dbg(("ire_add_mrtun: mrtun table is created\n"));
5273 		}
5274 		/* some other thread got it and created the table */
5275 		mutex_exit(&ipst->ips_ire_mrtun_lock);
5276 	}
5277 
5278 	/*
5279 	 * Check for duplicate in the bucket and insert in the table
5280 	 */
5281 	irb_ptr = &(ipst->ips_ip_mrtun_table[IRE_ADDR_HASH(ire->ire_in_src_addr,
5282 	    IP_MRTUN_TABLE_SIZE)]);
5283 
5284 	/*
5285 	 * Start the atomic add of the ire. Grab the ill locks,
5286 	 * ill_g_usesrc_lock and the bucket lock.
5287 	 *
5288 	 * If ipif or ill is changing ire_atomic_start() may queue the
5289 	 * request and return EINPROGRESS.
5290 	 */
5291 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
5292 	if (error != 0) {
5293 		/*
5294 		 * We don't know whether it is a valid ipif or not.
5295 		 * So, set it to NULL. This assumes that the ire has not added
5296 		 * a reference to the ipif.
5297 		 */
5298 		ire->ire_ipif = NULL;
5299 		ire_delete(ire);
5300 		ip1dbg(("ire_add_mrtun: ire_atomic_start failed\n"));
5301 		*ire_p = NULL;
5302 		return (error);
5303 	}
5304 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
5305 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
5306 			continue;
5307 		/* has anyone inserted the route in the meanwhile ? */
5308 		if (ire1->ire_in_ill == ire->ire_in_ill &&
5309 		    ire1->ire_in_src_addr == ire->ire_in_src_addr) {
5310 			ip1dbg(("ire_add_mrtun: Duplicate entry exists\n"));
5311 			IRE_REFHOLD(ire1);
5312 			ire_atomic_end(irb_ptr, ire);
5313 			ire_delete(ire);
5314 			/* Return the old ire */
5315 			*ire_p = ire1;
5316 			return (0);
5317 		}
5318 	}
5319 
5320 	/* Atomically set the ire_max_frag */
5321 	max_frag = *ire->ire_max_fragp;
5322 	ire->ire_max_fragp = NULL;
5323 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
5324 	ASSERT(ire->ire_type != IRE_CACHE);
5325 	irep = (ire_t **)irb_ptr;
5326 	if (*irep != NULL) {
5327 		/* Find the last ire which matches ire_in_src_addr */
5328 		ire1 = *irep;
5329 		while (ire1->ire_in_src_addr == ire->ire_in_src_addr) {
5330 			irep = &ire1->ire_next;
5331 			ire1 = *irep;
5332 			if (ire1 == NULL)
5333 				break;
5334 		}
5335 	}
5336 	ire1 = *irep;
5337 	if (ire1 != NULL)
5338 		ire1->ire_ptpn = &ire->ire_next;
5339 	ire->ire_next = ire1;
5340 	/* Link the new one in. */
5341 	ire->ire_ptpn = irep;
5342 	membar_producer();
5343 	*irep = ire;
5344 	ire->ire_bucket = irb_ptr;
5345 	IRE_REFHOLD_LOCKED(ire);
5346 
5347 	ip2dbg(("ire_add_mrtun: created and linked ire %p\n", (void *)*irep));
5348 
5349 	/*
5350 	 * Protect ire_mrtun_count and ill_mrtun_refcnt from
5351 	 * another thread trying to add ire in the table
5352 	 */
5353 	mutex_enter(&ipst->ips_ire_mrtun_lock);
5354 	ipst->ips_ire_mrtun_count++;
5355 	mutex_exit(&ipst->ips_ire_mrtun_lock);
5356 	/*
5357 	 * ill_mrtun_refcnt is protected by the ill_lock held via
5358 	 * ire_atomic_start
5359 	 */
5360 	ire->ire_in_ill->ill_mrtun_refcnt++;
5361 
5362 	if (ire->ire_ipif != NULL) {
5363 		ire->ire_ipif->ipif_ire_cnt++;
5364 		if (ire->ire_stq != NULL) {
5365 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
5366 			stq_ill->ill_ire_cnt++;
5367 		}
5368 	} else {
5369 		ASSERT(ire->ire_stq == NULL);
5370 	}
5371 
5372 	ire_atomic_end(irb_ptr, ire);
5373 	nce_fastpath(ire->ire_nce);
5374 	*ire_p = ire;
5375 	return (0);
5376 }
5377 
5378 
5379 /* Walks down the mrtun table */
5380 
5381 void
5382 ire_walk_ill_mrtun(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
5383     ill_t *ill, ip_stack_t *ipst)
5384 {
5385 	irb_t	*irb;
5386 	ire_t	*ire;
5387 	int	i;
5388 	int	ret;
5389 
5390 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
5391 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
5392 	ASSERT(match_flags == 0 || ire_type == IRE_MIPRTUN);
5393 
5394 	mutex_enter(&ipst->ips_ire_mrtun_lock);
5395 	if (ipst->ips_ire_mrtun_count == 0) {
5396 		mutex_exit(&ipst->ips_ire_mrtun_lock);
5397 		return;
5398 	}
5399 	mutex_exit(&ipst->ips_ire_mrtun_lock);
5400 
5401 	ip2dbg(("ire_walk_ill_mrtun:walking the reverse tunnel table \n"));
5402 	for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5403 
5404 		irb = &(ipst->ips_ip_mrtun_table[i]);
5405 		if (irb->irb_ire == NULL)
5406 			continue;
5407 		IRB_REFHOLD(irb);
5408 		for (ire = irb->irb_ire; ire != NULL;
5409 		    ire = ire->ire_next) {
5410 			ASSERT(ire->ire_ipversion == IPV4_VERSION);
5411 			if (match_flags != 0) {
5412 				ret = ire_walk_ill_match(
5413 				    match_flags, ire_type,
5414 				    ire, ill, ALL_ZONES, ipst);
5415 			}
5416 			if (match_flags == 0 || ret)
5417 				(*func)(ire, arg);
5418 		}
5419 		IRB_REFRELE(irb);
5420 	}
5421 }
5422 
5423 /*
5424  * Source interface based lookup routine (IPV4 only).
5425  * This routine is called only when RTA_SRCIFP bitflag is set
5426  * by routing socket while adding/deleting the route and it is
5427  * also called from ip_rput() when packets arrive from an interface
5428  * for which ill_srcif_ref_cnt is positive. This function is useful
5429  * when a packet coming from one interface must be forwarded to another
5430  * designated interface to reach the correct node. This function is also
5431  * called from ip_newroute when the link-layer address of an ire is resolved.
5432  * We need to make sure that ip_newroute searches for IRE_IF_RESOLVER type
5433  * ires--thus the ire_type parameter is needed.
5434  */
5435 
5436 ire_t *
5437 ire_srcif_table_lookup(ipaddr_t dst_addr, int ire_type, ipif_t *ipif,
5438     ill_t *in_ill, int flags)
5439 {
5440 	irb_t	*irb_ptr;
5441 	ire_t	*ire;
5442 	irb_t	*ire_srcif_table;
5443 
5444 	ASSERT(in_ill != NULL && !in_ill->ill_isv6);
5445 	ASSERT(!(flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) ||
5446 	    (ipif != NULL && !ipif->ipif_isv6));
5447 
5448 	/*
5449 	 * No need to lock the ill since it is refheld by the caller of this
5450 	 * function
5451 	 */
5452 	if (in_ill->ill_srcif_table == NULL) {
5453 		return (NULL);
5454 	}
5455 
5456 	if (!(flags & MATCH_IRE_TYPE)) {
5457 		flags |= MATCH_IRE_TYPE;
5458 		ire_type = IRE_INTERFACE;
5459 	}
5460 	ire_srcif_table = in_ill->ill_srcif_table;
5461 	irb_ptr = &ire_srcif_table[IRE_ADDR_HASH(dst_addr,
5462 	    IP_SRCIF_TABLE_SIZE)];
5463 	rw_enter(&irb_ptr->irb_lock, RW_READER);
5464 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
5465 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
5466 			continue;
5467 		if (ire_match_args(ire, dst_addr, ire->ire_mask, 0,
5468 		    ire_type, ipif, ire->ire_zoneid, 0, NULL, flags)) {
5469 			IRE_REFHOLD(ire);
5470 			rw_exit(&irb_ptr->irb_lock);
5471 			return (ire);
5472 		}
5473 	}
5474 	/* Not Found */
5475 	rw_exit(&irb_ptr->irb_lock);
5476 	return (NULL);
5477 }
5478 
5479 
5480 /*
5481  * Adds the ire into the special routing table which is hanging off of
5482  * the src_ipif->ipif_ill. It also increments the refcnt in the ill.
5483  * The forward table contains only IRE_IF_RESOLVER, IRE_IF_NORESOLVER
5484  * i,e. IRE_INTERFACE entries. Originally the dlureq_mp field is NULL
5485  * for IRE_IF_RESOLVER entry because we do not have the dst_addr's
5486  * link-layer address at the time of addition.
5487  * Upon resolving the address from ARP, dlureq_mp field is updated with
5488  * proper information in ire_update_srcif_v4.
5489  */
5490 static int
5491 ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
5492 {
5493 	ire_t	*ire1;
5494 	irb_t	*ire_srcifp_table = NULL;
5495 	irb_t	*irb_ptr = NULL;
5496 	ire_t   **irep;
5497 	ire_t   *ire;
5498 	int	flags;
5499 	int	i;
5500 	ill_t	*stq_ill;
5501 	uint_t	max_frag;
5502 	int error = 0;
5503 	ip_stack_t	*ipst;
5504 
5505 	ire = *ire_p;
5506 	ipst = ire->ire_ipst;
5507 	ASSERT(ire->ire_in_ill != NULL);
5508 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5509 	ASSERT(ire->ire_type == IRE_IF_NORESOLVER ||
5510 	    ire->ire_type == IRE_IF_RESOLVER);
5511 
5512 	ire->ire_mask = IP_HOST_MASK;
5513 	/*
5514 	 * Update ire_nce->nce_res_mp with NULL value upon creation;
5515 	 * first free the default res_mp created by ire_nce_init.
5516 	 */
5517 	freeb(ire->ire_nce->nce_res_mp);
5518 	if (ire->ire_type == IRE_IF_RESOLVER) {
5519 		/*
5520 		 * assign NULL now, it will be updated
5521 		 * with correct value upon returning from
5522 		 * ARP
5523 		 */
5524 		ire->ire_nce->nce_res_mp = NULL;
5525 	} else {
5526 		ire->ire_nce->nce_res_mp = ill_dlur_gen(NULL,
5527 		    ire->ire_ipif->ipif_ill->ill_phys_addr_length,
5528 		    ire->ire_ipif->ipif_ill->ill_sap,
5529 		    ire->ire_ipif->ipif_ill->ill_sap_length);
5530 	}
5531 	/* Make sure the address is properly masked. */
5532 	ire->ire_addr &= ire->ire_mask;
5533 
5534 	ASSERT(ire->ire_max_fragp != NULL);
5535 	max_frag = *ire->ire_max_fragp;
5536 	ire->ire_max_fragp = NULL;
5537 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
5538 
5539 	mutex_enter(&ire->ire_in_ill->ill_lock);
5540 	if (ire->ire_in_ill->ill_srcif_table == NULL) {
5541 		/* create the incoming interface based table */
5542 		ire->ire_in_ill->ill_srcif_table = kmem_zalloc(
5543 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t), KM_NOSLEEP);
5544 		if (ire->ire_in_ill->ill_srcif_table == NULL) {
5545 			ip1dbg(("ire_add_srcif_v4: Allocation fail\n"));
5546 			mutex_exit(&ire->ire_in_ill->ill_lock);
5547 			ire_delete(ire);
5548 			*ire_p = NULL;
5549 			return (ENOMEM);
5550 		}
5551 		ire_srcifp_table = ire->ire_in_ill->ill_srcif_table;
5552 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
5553 			rw_init(&ire_srcifp_table[i].irb_lock, NULL,
5554 			    RW_DEFAULT, NULL);
5555 		}
5556 		ip2dbg(("ire_add_srcif_v4: table created for ill %p\n",
5557 		    (void *)ire->ire_in_ill));
5558 	}
5559 	/* Check for duplicate and insert */
5560 	ASSERT(ire->ire_in_ill->ill_srcif_table != NULL);
5561 	irb_ptr =
5562 	    &(ire->ire_in_ill->ill_srcif_table[IRE_ADDR_HASH(ire->ire_addr,
5563 	    IP_SRCIF_TABLE_SIZE)]);
5564 	mutex_exit(&ire->ire_in_ill->ill_lock);
5565 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
5566 	flags |= MATCH_IRE_IPIF;
5567 
5568 	/*
5569 	 * Start the atomic add of the ire. Grab the ill locks,
5570 	 * ill_g_usesrc_lock and the bucket lock.
5571 	 *
5572 	 * If ipif or ill is changing ire_atomic_start() may queue the
5573 	 * request and return EINPROGRESS.
5574 	 */
5575 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
5576 	if (error != 0) {
5577 		/*
5578 		 * We don't know whether it is a valid ipif or not.
5579 		 * So, set it to NULL. This assumes that the ire has not added
5580 		 * a reference to the ipif.
5581 		 */
5582 		ire->ire_ipif = NULL;
5583 		ire_delete(ire);
5584 		ip1dbg(("ire_add_srcif_v4: ire_atomic_start failed\n"));
5585 		*ire_p = NULL;
5586 		return (error);
5587 	}
5588 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
5589 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
5590 			continue;
5591 		if (ire1->ire_zoneid != ire->ire_zoneid)
5592 			continue;
5593 		/* Has anyone inserted route in the meanwhile ? */
5594 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 0,
5595 		    ire->ire_type, ire->ire_ipif, ire->ire_zoneid, 0, NULL,
5596 		    flags)) {
5597 			ip1dbg(("ire_add_srcif_v4 : Duplicate entry exists\n"));
5598 			IRE_REFHOLD(ire1);
5599 			ire_atomic_end(irb_ptr, ire);
5600 			ire_delete(ire);
5601 			/* Return old ire as in ire_add_v4 */
5602 			*ire_p = ire1;
5603 			return (0);
5604 		}
5605 	}
5606 	irep = (ire_t **)irb_ptr;
5607 	if (*irep != NULL) {
5608 		/* Find the last ire which matches ire_addr */
5609 		ire1 = *irep;
5610 		while (ire1->ire_addr == ire->ire_addr) {
5611 			irep = &ire1->ire_next;
5612 			ire1 = *irep;
5613 			if (ire1 == NULL)
5614 				break;
5615 		}
5616 	}
5617 	ire1 = *irep;
5618 	if (ire1 != NULL)
5619 		ire1->ire_ptpn = &ire->ire_next;
5620 	ire->ire_next = ire1;
5621 	/* Link the new one in. */
5622 	ire->ire_ptpn = irep;
5623 	membar_producer();
5624 	*irep = ire;
5625 	ire->ire_bucket = irb_ptr;
5626 	IRE_REFHOLD_LOCKED(ire);
5627 
5628 	/*
5629 	 * Protect ire_in_ill->ill_srcif_refcnt and table reference count.
5630 	 * Note, ire_atomic_start already grabs the ire_in_ill->ill_lock
5631 	 * so ill_srcif_refcnt is already protected.
5632 	 */
5633 	ire->ire_in_ill->ill_srcif_refcnt++;
5634 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
5635 	ipst->ips_ire_srcif_table_count++;
5636 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
5637 	irb_ptr->irb_ire_cnt++;
5638 	if (ire->ire_ipif != NULL) {
5639 		ire->ire_ipif->ipif_ire_cnt++;
5640 		if (ire->ire_stq != NULL) {
5641 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
5642 			stq_ill->ill_ire_cnt++;
5643 		}
5644 	} else {
5645 		ASSERT(ire->ire_stq == NULL);
5646 	}
5647 
5648 	ire_atomic_end(irb_ptr, ire);
5649 	*ire_p = ire;
5650 	return (0);
5651 }
5652 
5653 
5654 /*
5655  * This function is called by ire_add_then_send when ARP request comes
5656  * back to ip_wput->ire_add_then_send for resolved ire in the interface
5657  * based routing table. At this point, it only needs to update the resolver
5658  * information for the ire. The passed ire is returned to the caller as it
5659  * is the ire which is created as mblk.
5660  */
5661 static ire_t *
5662 ire_update_srcif_v4(ire_t *ire)
5663 {
5664 	ire_t   *ire1;
5665 	irb_t	*irb;
5666 	int	error;
5667 
5668 	ASSERT(ire->ire_type != IRE_MIPRTUN &&
5669 	    ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER);
5670 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
5671 
5672 	/*
5673 	 * This ire is from ARP. Update
5674 	 * ire_nce->nce_res_mp info
5675 	 */
5676 	ire1 = ire_srcif_table_lookup(ire->ire_addr,
5677 	    IRE_IF_RESOLVER, ire->ire_ipif,
5678 	    ire->ire_in_ill,
5679 	    MATCH_IRE_ILL | MATCH_IRE_TYPE);
5680 	if (ire1 == NULL) {
5681 		/* Mobile node registration expired ? */
5682 		ire_delete(ire);
5683 		return (NULL);
5684 	}
5685 	irb = ire1->ire_bucket;
5686 	ASSERT(irb != NULL);
5687 	/*
5688 	 * Start the atomic add of the ire. Grab the ill locks,
5689 	 * ill_g_usesrc_lock and the bucket lock.
5690 	 */
5691 	error = ire_atomic_start(irb, ire1, NULL, NULL, NULL);
5692 	if (error != 0) {
5693 		/*
5694 		 * We don't know whether it is a valid ipif or not.
5695 		 * So, set it to NULL. This assumes that the ire has not added
5696 		 * a reference to the ipif.
5697 		 */
5698 		ire->ire_ipif = NULL;
5699 		ire_delete(ire);
5700 		ip1dbg(("ire_update_srcif_v4: ire_atomic_start failed\n"));
5701 		return (NULL);
5702 	}
5703 	ASSERT(ire->ire_max_fragp == NULL);
5704 	ire->ire_max_frag = ire1->ire_max_frag;
5705 	/*
5706 	 * Update resolver information and
5707 	 * send-to queue.
5708 	 */
5709 	ASSERT(ire->ire_nce->nce_res_mp != NULL);
5710 	ire1->ire_nce->nce_res_mp = copyb(ire->ire_nce->nce_res_mp);
5711 	if (ire1->ire_nce->nce_res_mp ==  NULL) {
5712 		ip0dbg(("ire_update_srcif: copyb failed\n"));
5713 		ire_refrele(ire1);
5714 		ire_refrele(ire);
5715 		ire_atomic_end(irb, ire1);
5716 		return (NULL);
5717 	}
5718 	ire1->ire_stq = ire->ire_stq;
5719 
5720 	ASSERT(ire->ire_nce->nce_fp_mp == NULL);
5721 
5722 	ire_atomic_end(irb, ire1);
5723 	ire_refrele(ire1);
5724 	/* Return the passed ire */
5725 	return (ire);   /* Update done */
5726 }
5727 
5728 
5729 /*
5730  * Check if another multirt route resolution is needed.
5731  * B_TRUE is returned is there remain a resolvable route,
5732  * or if no route for that dst is resolved yet.
5733  * B_FALSE is returned if all routes for that dst are resolved
5734  * or if the remaining unresolved routes are actually not
5735  * resolvable.
5736  * This only works in the global zone.
5737  */
5738 boolean_t
5739 ire_multirt_need_resolve(ipaddr_t dst, const ts_label_t *tsl, ip_stack_t *ipst)
5740 {
5741 	ire_t	*first_fire;
5742 	ire_t	*first_cire;
5743 	ire_t	*fire;
5744 	ire_t	*cire;
5745 	irb_t	*firb;
5746 	irb_t	*cirb;
5747 	int	unres_cnt = 0;
5748 	boolean_t resolvable = B_FALSE;
5749 
5750 	/* Retrieve the first IRE_HOST that matches the destination */
5751 	first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL,
5752 	    NULL, ALL_ZONES, 0, tsl,
5753 	    MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst);
5754 
5755 	/* No route at all */
5756 	if (first_fire == NULL) {
5757 		return (B_TRUE);
5758 	}
5759 
5760 	firb = first_fire->ire_bucket;
5761 	ASSERT(firb != NULL);
5762 
5763 	/* Retrieve the first IRE_CACHE ire for that destination. */
5764 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
5765 
5766 	/* No resolved route. */
5767 	if (first_cire == NULL) {
5768 		ire_refrele(first_fire);
5769 		return (B_TRUE);
5770 	}
5771 
5772 	/*
5773 	 * At least one route is resolved. Here we look through the forward
5774 	 * and cache tables, to compare the number of declared routes
5775 	 * with the number of resolved routes. The search for a resolvable
5776 	 * route is performed only if at least one route remains
5777 	 * unresolved.
5778 	 */
5779 	cirb = first_cire->ire_bucket;
5780 	ASSERT(cirb != NULL);
5781 
5782 	/* Count the number of routes to that dest that are declared. */
5783 	IRB_REFHOLD(firb);
5784 	for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
5785 		if (!(fire->ire_flags & RTF_MULTIRT))
5786 			continue;
5787 		if (fire->ire_addr != dst)
5788 			continue;
5789 		unres_cnt++;
5790 	}
5791 	IRB_REFRELE(firb);
5792 
5793 	/* Then subtract the number of routes to that dst that are resolved */
5794 	IRB_REFHOLD(cirb);
5795 	for (cire = first_cire; cire != NULL; cire = cire->ire_next) {
5796 		if (!(cire->ire_flags & RTF_MULTIRT))
5797 			continue;
5798 		if (cire->ire_addr != dst)
5799 			continue;
5800 		if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
5801 			continue;
5802 		unres_cnt--;
5803 	}
5804 	IRB_REFRELE(cirb);
5805 
5806 	/* At least one route is unresolved; search for a resolvable route. */
5807 	if (unres_cnt > 0)
5808 		resolvable = ire_multirt_lookup(&first_cire, &first_fire,
5809 		    MULTIRT_USESTAMP | MULTIRT_CACHEGW, tsl, ipst);
5810 
5811 	if (first_fire != NULL)
5812 		ire_refrele(first_fire);
5813 
5814 	if (first_cire != NULL)
5815 		ire_refrele(first_cire);
5816 
5817 	return (resolvable);
5818 }
5819 
5820 
5821 /*
5822  * Explore a forward_table bucket, starting from fire_arg.
5823  * fire_arg MUST be an IRE_HOST entry.
5824  *
5825  * Return B_TRUE and update *ire_arg and *fire_arg
5826  * if at least one resolvable route is found. *ire_arg
5827  * is the IRE entry for *fire_arg's gateway.
5828  *
5829  * Return B_FALSE otherwise (all routes are resolved or
5830  * the remaining unresolved routes are all unresolvable).
5831  *
5832  * The IRE selection relies on a priority mechanism
5833  * driven by the flags passed in by the caller.
5834  * The caller, such as ip_newroute_ipif(), can get the most
5835  * relevant ire at each stage of a multiple route resolution.
5836  *
5837  * The rules are:
5838  *
5839  * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE
5840  *   ires are preferred for the gateway. This gives the highest
5841  *   priority to routes that can be resolved without using
5842  *   a resolver.
5843  *
5844  * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW
5845  *   is specified but no IRE_CACHETABLE ire entry for the gateway
5846  *   is found, the following rules apply.
5847  *
5848  * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE
5849  *   ires for the gateway, that have not been tried since
5850  *   a configurable amount of time, are preferred.
5851  *   This applies when a resolver must be invoked for
5852  *   a missing route, but we don't want to use the resolver
5853  *   upon each packet emission. If no such resolver is found,
5854  *   B_FALSE is returned.
5855  *   The MULTIRT_USESTAMP flag can be combined with
5856  *   MULTIRT_CACHEGW.
5857  *
5858  * - if MULTIRT_USESTAMP is not specified in flags, the first
5859  *   unresolved but resolvable route is selected.
5860  *
5861  * - Otherwise, there is no resolvalble route, and
5862  *   B_FALSE is returned.
5863  *
5864  * At last, MULTIRT_SETSTAMP can be specified in flags to
5865  * request the timestamp of unresolvable routes to
5866  * be refreshed. This prevents the useless exploration
5867  * of those routes for a while, when MULTIRT_USESTAMP is used.
5868  *
5869  * This only works in the global zone.
5870  */
5871 boolean_t
5872 ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags,
5873     const ts_label_t *tsl, ip_stack_t *ipst)
5874 {
5875 	clock_t	delta;
5876 	ire_t	*best_fire = NULL;
5877 	ire_t	*best_cire = NULL;
5878 	ire_t	*first_fire;
5879 	ire_t	*first_cire;
5880 	ire_t	*fire;
5881 	ire_t	*cire;
5882 	irb_t	*firb = NULL;
5883 	irb_t	*cirb = NULL;
5884 	ire_t	*gw_ire;
5885 	boolean_t	already_resolved;
5886 	boolean_t	res;
5887 	ipaddr_t	dst;
5888 	ipaddr_t	gw;
5889 
5890 	ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n",
5891 	    (void *)*ire_arg, (void *)*fire_arg, flags));
5892 
5893 	ASSERT(ire_arg != NULL);
5894 	ASSERT(fire_arg != NULL);
5895 
5896 	/* Not an IRE_HOST ire; give up. */
5897 	if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) {
5898 		return (B_FALSE);
5899 	}
5900 
5901 	/* This is the first IRE_HOST ire for that destination. */
5902 	first_fire = *fire_arg;
5903 	firb = first_fire->ire_bucket;
5904 	ASSERT(firb != NULL);
5905 
5906 	dst = first_fire->ire_addr;
5907 
5908 	ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst)));
5909 
5910 	/*
5911 	 * Retrieve the first IRE_CACHE ire for that destination;
5912 	 * if we don't find one, no route for that dest is
5913 	 * resolved yet.
5914 	 */
5915 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
5916 	if (first_cire != NULL) {
5917 		cirb = first_cire->ire_bucket;
5918 	}
5919 
5920 	ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire));
5921 
5922 	/*
5923 	 * Search for a resolvable route, giving the top priority
5924 	 * to routes that can be resolved without any call to the resolver.
5925 	 */
5926 	IRB_REFHOLD(firb);
5927 
5928 	if (!CLASSD(dst)) {
5929 		/*
5930 		 * For all multiroute IRE_HOST ires for that destination,
5931 		 * check if the route via the IRE_HOST's gateway is
5932 		 * resolved yet.
5933 		 */
5934 		for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
5935 
5936 			if (!(fire->ire_flags & RTF_MULTIRT))
5937 				continue;
5938 			if (fire->ire_addr != dst)
5939 				continue;
5940 
5941 			if (fire->ire_gw_secattr != NULL &&
5942 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
5943 				continue;
5944 			}
5945 
5946 			gw = fire->ire_gateway_addr;
5947 
5948 			ip2dbg(("ire_multirt_lookup: fire %p, "
5949 			    "ire_addr %08x, ire_gateway_addr %08x\n",
5950 			    (void *)fire, ntohl(fire->ire_addr), ntohl(gw)));
5951 
5952 			already_resolved = B_FALSE;
5953 
5954 			if (first_cire != NULL) {
5955 				ASSERT(cirb != NULL);
5956 
5957 				IRB_REFHOLD(cirb);
5958 				/*
5959 				 * For all IRE_CACHE ires for that
5960 				 * destination.
5961 				 */
5962 				for (cire = first_cire;
5963 				    cire != NULL;
5964 				    cire = cire->ire_next) {
5965 
5966 					if (!(cire->ire_flags & RTF_MULTIRT))
5967 						continue;
5968 					if (cire->ire_addr != dst)
5969 						continue;
5970 					if (cire->ire_marks &
5971 					    (IRE_MARK_CONDEMNED |
5972 						IRE_MARK_HIDDEN))
5973 						continue;
5974 
5975 					if (cire->ire_gw_secattr != NULL &&
5976 					    tsol_ire_match_gwattr(cire,
5977 					    tsl) != 0) {
5978 						continue;
5979 					}
5980 
5981 					/*
5982 					 * Check if the IRE_CACHE's gateway
5983 					 * matches the IRE_HOST's gateway.
5984 					 */
5985 					if (cire->ire_gateway_addr == gw) {
5986 						already_resolved = B_TRUE;
5987 						break;
5988 					}
5989 				}
5990 				IRB_REFRELE(cirb);
5991 			}
5992 
5993 			/*
5994 			 * This route is already resolved;
5995 			 * proceed with next one.
5996 			 */
5997 			if (already_resolved) {
5998 				ip2dbg(("ire_multirt_lookup: found cire %p, "
5999 				    "already resolved\n", (void *)cire));
6000 				continue;
6001 			}
6002 
6003 			/*
6004 			 * The route is unresolved; is it actually
6005 			 * resolvable, i.e. is there a cache or a resolver
6006 			 * for the gateway?
6007 			 */
6008 			gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL,
6009 			    ALL_ZONES, tsl,
6010 			    MATCH_IRE_RECURSIVE | MATCH_IRE_SECATTR, ipst);
6011 
6012 			ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n",
6013 			    (void *)gw_ire));
6014 
6015 			/*
6016 			 * If gw_ire is typed IRE_CACHETABLE,
6017 			 * this route can be resolved without any call to the
6018 			 * resolver. If the MULTIRT_CACHEGW flag is set,
6019 			 * give the top priority to this ire and exit the
6020 			 * loop.
6021 			 * This is typically the case when an ARP reply
6022 			 * is processed through ip_wput_nondata().
6023 			 */
6024 			if ((flags & MULTIRT_CACHEGW) &&
6025 			    (gw_ire != NULL) &&
6026 			    (gw_ire->ire_type & IRE_CACHETABLE)) {
6027 				ASSERT(gw_ire->ire_nce == NULL ||
6028 				    gw_ire->ire_nce->nce_state == ND_REACHABLE);
6029 				/*
6030 				 * Release the resolver associated to the
6031 				 * previous candidate best ire, if any.
6032 				 */
6033 				if (best_cire != NULL) {
6034 					ire_refrele(best_cire);
6035 					ASSERT(best_fire != NULL);
6036 				}
6037 
6038 				best_fire = fire;
6039 				best_cire = gw_ire;
6040 
6041 				ip2dbg(("ire_multirt_lookup: found top prio "
6042 				    "best_fire %p, best_cire %p\n",
6043 				    (void *)best_fire, (void *)best_cire));
6044 				break;
6045 			}
6046 
6047 			/*
6048 			 * Compute the time elapsed since our preceding
6049 			 * attempt to  resolve that route.
6050 			 * If the MULTIRT_USESTAMP flag is set, we take that
6051 			 * route into account only if this time interval
6052 			 * exceeds ip_multirt_resolution_interval;
6053 			 * this prevents us from attempting to resolve a
6054 			 * broken route upon each sending of a packet.
6055 			 */
6056 			delta = lbolt - fire->ire_last_used_time;
6057 			delta = TICK_TO_MSEC(delta);
6058 
6059 			res = (boolean_t)((delta >
6060 				ipst->ips_ip_multirt_resolution_interval) ||
6061 				(!(flags & MULTIRT_USESTAMP)));
6062 
6063 			ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, "
6064 			    "res %d\n",
6065 			    (void *)fire, delta, res));
6066 
6067 			if (res) {
6068 				/*
6069 				 * We are here if MULTIRT_USESTAMP flag is set
6070 				 * and the resolver for fire's gateway
6071 				 * has not been tried since
6072 				 * ip_multirt_resolution_interval, or if
6073 				 * MULTIRT_USESTAMP is not set but gw_ire did
6074 				 * not fill the conditions for MULTIRT_CACHEGW,
6075 				 * or if neither MULTIRT_USESTAMP nor
6076 				 * MULTIRT_CACHEGW are set.
6077 				 */
6078 				if (gw_ire != NULL) {
6079 					if (best_fire == NULL) {
6080 						ASSERT(best_cire == NULL);
6081 
6082 						best_fire = fire;
6083 						best_cire = gw_ire;
6084 
6085 						ip2dbg(("ire_multirt_lookup:"
6086 						    "found candidate "
6087 						    "best_fire %p, "
6088 						    "best_cire %p\n",
6089 						    (void *)best_fire,
6090 						    (void *)best_cire));
6091 
6092 						/*
6093 						 * If MULTIRT_CACHEGW is not
6094 						 * set, we ignore the top
6095 						 * priority ires that can
6096 						 * be resolved without any
6097 						 * call to the resolver;
6098 						 * In that case, there is
6099 						 * actually no need
6100 						 * to continue the loop.
6101 						 */
6102 						if (!(flags &
6103 						    MULTIRT_CACHEGW)) {
6104 							break;
6105 						}
6106 						continue;
6107 					}
6108 				} else {
6109 					/*
6110 					 * No resolver for the gateway: the
6111 					 * route is not resolvable.
6112 					 * If the MULTIRT_SETSTAMP flag is
6113 					 * set, we stamp the IRE_HOST ire,
6114 					 * so we will not select it again
6115 					 * during this resolution interval.
6116 					 */
6117 					if (flags & MULTIRT_SETSTAMP)
6118 						fire->ire_last_used_time =
6119 						    lbolt;
6120 				}
6121 			}
6122 
6123 			if (gw_ire != NULL)
6124 				ire_refrele(gw_ire);
6125 		}
6126 	} else { /* CLASSD(dst) */
6127 
6128 		for (fire = first_fire;
6129 		    fire != NULL;
6130 		    fire = fire->ire_next) {
6131 
6132 			if (!(fire->ire_flags & RTF_MULTIRT))
6133 				continue;
6134 			if (fire->ire_addr != dst)
6135 				continue;
6136 
6137 			if (fire->ire_gw_secattr != NULL &&
6138 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
6139 				continue;
6140 			}
6141 
6142 			already_resolved = B_FALSE;
6143 
6144 			gw = fire->ire_gateway_addr;
6145 
6146 			gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE,
6147 			    NULL, NULL, ALL_ZONES, 0, tsl,
6148 			    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE |
6149 			    MATCH_IRE_SECATTR, ipst);
6150 
6151 			/* No resolver for the gateway; we skip this ire. */
6152 			if (gw_ire == NULL) {
6153 				continue;
6154 			}
6155 			ASSERT(gw_ire->ire_nce == NULL ||
6156 			    gw_ire->ire_nce->nce_state == ND_REACHABLE);
6157 
6158 			if (first_cire != NULL) {
6159 
6160 				IRB_REFHOLD(cirb);
6161 				/*
6162 				 * For all IRE_CACHE ires for that
6163 				 * destination.
6164 				 */
6165 				for (cire = first_cire;
6166 				    cire != NULL;
6167 				    cire = cire->ire_next) {
6168 
6169 					if (!(cire->ire_flags & RTF_MULTIRT))
6170 						continue;
6171 					if (cire->ire_addr != dst)
6172 						continue;
6173 					if (cire->ire_marks &
6174 					    (IRE_MARK_CONDEMNED |
6175 						IRE_MARK_HIDDEN))
6176 						continue;
6177 
6178 					if (cire->ire_gw_secattr != NULL &&
6179 					    tsol_ire_match_gwattr(cire,
6180 					    tsl) != 0) {
6181 						continue;
6182 					}
6183 
6184 					/*
6185 					 * Cache entries are linked to the
6186 					 * parent routes using the parent handle
6187 					 * (ire_phandle). If no cache entry has
6188 					 * the same handle as fire, fire is
6189 					 * still unresolved.
6190 					 */
6191 					ASSERT(cire->ire_phandle != 0);
6192 					if (cire->ire_phandle ==
6193 					    fire->ire_phandle) {
6194 						already_resolved = B_TRUE;
6195 						break;
6196 					}
6197 				}
6198 				IRB_REFRELE(cirb);
6199 			}
6200 
6201 			/*
6202 			 * This route is already resolved; proceed with
6203 			 * next one.
6204 			 */
6205 			if (already_resolved) {
6206 				ire_refrele(gw_ire);
6207 				continue;
6208 			}
6209 
6210 			/*
6211 			 * Compute the time elapsed since our preceding
6212 			 * attempt to resolve that route.
6213 			 * If the MULTIRT_USESTAMP flag is set, we take
6214 			 * that route into account only if this time
6215 			 * interval exceeds ip_multirt_resolution_interval;
6216 			 * this prevents us from attempting to resolve a
6217 			 * broken route upon each sending of a packet.
6218 			 */
6219 			delta = lbolt - fire->ire_last_used_time;
6220 			delta = TICK_TO_MSEC(delta);
6221 
6222 			res = (boolean_t)((delta >
6223 				ipst->ips_ip_multirt_resolution_interval) ||
6224 				(!(flags & MULTIRT_USESTAMP)));
6225 
6226 			ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, "
6227 			    "flags %04x, res %d\n",
6228 			    (void *)fire, delta, flags, res));
6229 
6230 			if (res) {
6231 				if (best_cire != NULL) {
6232 					/*
6233 					 * Release the resolver associated
6234 					 * to the preceding candidate best
6235 					 * ire, if any.
6236 					 */
6237 					ire_refrele(best_cire);
6238 					ASSERT(best_fire != NULL);
6239 				}
6240 				best_fire = fire;
6241 				best_cire = gw_ire;
6242 				continue;
6243 			}
6244 
6245 			ire_refrele(gw_ire);
6246 		}
6247 	}
6248 
6249 	if (best_fire != NULL) {
6250 		IRE_REFHOLD(best_fire);
6251 	}
6252 	IRB_REFRELE(firb);
6253 
6254 	/* Release the first IRE_CACHE we initially looked up, if any. */
6255 	if (first_cire != NULL)
6256 		ire_refrele(first_cire);
6257 
6258 	/* Found a resolvable route. */
6259 	if (best_fire != NULL) {
6260 		ASSERT(best_cire != NULL);
6261 
6262 		if (*fire_arg != NULL)
6263 			ire_refrele(*fire_arg);
6264 		if (*ire_arg != NULL)
6265 			ire_refrele(*ire_arg);
6266 
6267 		/*
6268 		 * Update the passed-in arguments with the
6269 		 * resolvable multirt route we found.
6270 		 */
6271 		*fire_arg = best_fire;
6272 		*ire_arg = best_cire;
6273 
6274 		ip2dbg(("ire_multirt_lookup: returning B_TRUE, "
6275 		    "*fire_arg %p, *ire_arg %p\n",
6276 		    (void *)best_fire, (void *)best_cire));
6277 
6278 		return (B_TRUE);
6279 	}
6280 
6281 	ASSERT(best_cire == NULL);
6282 
6283 	ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, "
6284 	    "*ire_arg %p\n",
6285 	    (void *)*fire_arg, (void *)*ire_arg));
6286 
6287 	/* No resolvable route. */
6288 	return (B_FALSE);
6289 }
6290 
6291 /*
6292  * IRE iterator for inbound and loopback broadcast processing.
6293  * Given an IRE_BROADCAST ire, walk the ires with the same destination
6294  * address, but skip over the passed-in ire. Returns the next ire without
6295  * a hold - assumes that the caller holds a reference on the IRE bucket.
6296  */
6297 ire_t *
6298 ire_get_next_bcast_ire(ire_t *curr, ire_t *ire)
6299 {
6300 	ill_t *ill;
6301 
6302 	if (curr == NULL) {
6303 		for (curr = ire->ire_bucket->irb_ire; curr != NULL;
6304 		    curr = curr->ire_next) {
6305 			if (curr->ire_addr == ire->ire_addr)
6306 				break;
6307 		}
6308 	} else {
6309 		curr = curr->ire_next;
6310 	}
6311 	ill = ire_to_ill(ire);
6312 	for (; curr != NULL; curr = curr->ire_next) {
6313 		if (curr->ire_addr != ire->ire_addr) {
6314 			/*
6315 			 * All the IREs to a given destination are contiguous;
6316 			 * break out once the address doesn't match.
6317 			 */
6318 			break;
6319 		}
6320 		if (curr == ire) {
6321 			/* skip over the passed-in ire */
6322 			continue;
6323 		}
6324 		if ((curr->ire_stq != NULL && ire->ire_stq == NULL) ||
6325 		    (curr->ire_stq == NULL && ire->ire_stq != NULL)) {
6326 			/*
6327 			 * If the passed-in ire is loopback, skip over
6328 			 * non-loopback ires and vice versa.
6329 			 */
6330 			continue;
6331 		}
6332 		if (ire_to_ill(curr) != ill) {
6333 			/* skip over IREs going through a different interface */
6334 			continue;
6335 		}
6336 		if (curr->ire_marks & IRE_MARK_CONDEMNED) {
6337 			/* skip over deleted IREs */
6338 			continue;
6339 		}
6340 		return (curr);
6341 	}
6342 	return (NULL);
6343 }
6344 
6345 #ifdef IRE_DEBUG
6346 th_trace_t *
6347 th_trace_ire_lookup(ire_t *ire)
6348 {
6349 	int bucket_id;
6350 	th_trace_t *th_trace;
6351 
6352 	ASSERT(MUTEX_HELD(&ire->ire_lock));
6353 
6354 	bucket_id = IP_TR_HASH(curthread);
6355 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6356 
6357 	for (th_trace = ire->ire_trace[bucket_id]; th_trace != NULL;
6358 	    th_trace = th_trace->th_next) {
6359 		if (th_trace->th_id == curthread)
6360 			return (th_trace);
6361 	}
6362 	return (NULL);
6363 }
6364 
6365 void
6366 ire_trace_ref(ire_t *ire)
6367 {
6368 	int bucket_id;
6369 	th_trace_t *th_trace;
6370 
6371 	/*
6372 	 * Attempt to locate the trace buffer for the curthread.
6373 	 * If it does not exist, then allocate a new trace buffer
6374 	 * and link it in list of trace bufs for this ipif, at the head
6375 	 */
6376 	mutex_enter(&ire->ire_lock);
6377 	if (ire->ire_trace_disable == B_TRUE) {
6378 		mutex_exit(&ire->ire_lock);
6379 		return;
6380 	}
6381 	th_trace = th_trace_ire_lookup(ire);
6382 	if (th_trace == NULL) {
6383 		bucket_id = IP_TR_HASH(curthread);
6384 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6385 		    KM_NOSLEEP);
6386 		if (th_trace == NULL) {
6387 			ire->ire_trace_disable = B_TRUE;
6388 			mutex_exit(&ire->ire_lock);
6389 			ire_trace_inactive(ire);
6390 			return;
6391 		}
6392 
6393 		th_trace->th_id = curthread;
6394 		th_trace->th_next = ire->ire_trace[bucket_id];
6395 		th_trace->th_prev = &ire->ire_trace[bucket_id];
6396 		if (th_trace->th_next != NULL)
6397 			th_trace->th_next->th_prev = &th_trace->th_next;
6398 		ire->ire_trace[bucket_id] = th_trace;
6399 	}
6400 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
6401 	th_trace->th_refcnt++;
6402 	th_trace_rrecord(th_trace);
6403 	mutex_exit(&ire->ire_lock);
6404 }
6405 
6406 void
6407 ire_trace_free(th_trace_t *th_trace)
6408 {
6409 	/* unlink th_trace and free it */
6410 	*th_trace->th_prev = th_trace->th_next;
6411 	if (th_trace->th_next != NULL)
6412 		th_trace->th_next->th_prev = th_trace->th_prev;
6413 	th_trace->th_next = NULL;
6414 	th_trace->th_prev = NULL;
6415 	kmem_free(th_trace, sizeof (th_trace_t));
6416 }
6417 
6418 void
6419 ire_untrace_ref(ire_t *ire)
6420 {
6421 	th_trace_t *th_trace;
6422 
6423 	mutex_enter(&ire->ire_lock);
6424 
6425 	if (ire->ire_trace_disable == B_TRUE) {
6426 		mutex_exit(&ire->ire_lock);
6427 		return;
6428 	}
6429 
6430 	th_trace = th_trace_ire_lookup(ire);
6431 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
6432 	th_trace_rrecord(th_trace);
6433 	th_trace->th_refcnt--;
6434 
6435 	if (th_trace->th_refcnt == 0)
6436 		ire_trace_free(th_trace);
6437 
6438 	mutex_exit(&ire->ire_lock);
6439 }
6440 
6441 static void
6442 ire_trace_inactive(ire_t *ire)
6443 {
6444 	th_trace_t *th_trace;
6445 	int i;
6446 
6447 	mutex_enter(&ire->ire_lock);
6448 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6449 		while (ire->ire_trace[i] != NULL) {
6450 			th_trace = ire->ire_trace[i];
6451 
6452 			/* unlink th_trace and free it */
6453 			ire->ire_trace[i] = th_trace->th_next;
6454 			if (th_trace->th_next != NULL)
6455 				th_trace->th_next->th_prev =
6456 				    &ire->ire_trace[i];
6457 
6458 			th_trace->th_next = NULL;
6459 			th_trace->th_prev = NULL;
6460 			kmem_free(th_trace, sizeof (th_trace_t));
6461 		}
6462 	}
6463 
6464 	mutex_exit(&ire->ire_lock);
6465 }
6466 
6467 /* ARGSUSED */
6468 void
6469 ire_thread_exit(ire_t *ire, caddr_t arg)
6470 {
6471 	th_trace_t	*th_trace;
6472 
6473 	mutex_enter(&ire->ire_lock);
6474 	th_trace = th_trace_ire_lookup(ire);
6475 	if (th_trace == NULL) {
6476 		mutex_exit(&ire->ire_lock);
6477 		return;
6478 	}
6479 	ASSERT(th_trace->th_refcnt == 0);
6480 
6481 	ire_trace_free(th_trace);
6482 	mutex_exit(&ire->ire_lock);
6483 }
6484 
6485 #endif
6486 
6487 /*
6488  * Generate a message chain with an arp request to resolve the in_ire.
6489  * It is assumed that in_ire itself is currently in the ire cache table,
6490  * so we create a fake_ire filled with enough information about ire_addr etc.
6491  * to retrieve in_ire when the DL_UNITDATA response from the resolver
6492  * comes back. The fake_ire itself is created by calling esballoc with
6493  * the fr_rtnp (free routine) set to ire_freemblk. This routine will be
6494  * invoked when the mblk containing fake_ire is freed.
6495  */
6496 void
6497 ire_arpresolve(ire_t *in_ire, ill_t *dst_ill)
6498 {
6499 	areq_t		*areq;
6500 	ipaddr_t	*addrp;
6501 	mblk_t 		*ire_mp, *dlureq_mp;
6502 	ire_t 		*ire, *buf;
6503 	size_t		bufsize;
6504 	frtn_t		*frtnp;
6505 	ill_t		*ill;
6506 	ip_stack_t	*ipst = dst_ill->ill_ipst;
6507 
6508 	/*
6509 	 * Construct message chain for the resolver
6510 	 * of the form:
6511 	 *	ARP_REQ_MBLK-->IRE_MBLK
6512 	 *
6513 	 * NOTE : If the response does not
6514 	 * come back, ARP frees the packet. For this reason,
6515 	 * we can't REFHOLD the bucket of save_ire to prevent
6516 	 * deletions. We may not be able to REFRELE the bucket
6517 	 * if the response never comes back. Thus, before
6518 	 * adding the ire, ire_add_v4 will make sure that the
6519 	 * interface route does not get deleted. This is the
6520 	 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
6521 	 * where we can always prevent deletions because of
6522 	 * the synchronous nature of adding IRES i.e
6523 	 * ire_add_then_send is called after creating the IRE.
6524 	 */
6525 
6526 	/*
6527 	 * We use esballoc to allocate the second part(the ire_t size mblk)
6528 	 * of the message chain depicted above. THis mblk will be freed
6529 	 * by arp when there is a  timeout, and otherwise passed to IP
6530 	 * and IP will * free it after processing the ARP response.
6531 	 */
6532 
6533 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
6534 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
6535 	if (buf == NULL) {
6536 		ip1dbg(("ire_arpresolver:alloc buffer failed\n "));
6537 		return;
6538 	}
6539 	frtnp = (frtn_t *)(buf + 1);
6540 	frtnp->free_arg = (caddr_t)buf;
6541 	frtnp->free_func = ire_freemblk;
6542 
6543 	ire_mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
6544 
6545 	if (ire_mp == NULL) {
6546 		ip1dbg(("ire_arpresolve: esballoc failed\n"));
6547 		kmem_free(buf, bufsize);
6548 		return;
6549 	}
6550 	ASSERT(in_ire->ire_nce != NULL);
6551 	dlureq_mp = copyb(dst_ill->ill_resolver_mp);
6552 	if (dlureq_mp == NULL) {
6553 		kmem_free(buf, bufsize);
6554 		return;
6555 	}
6556 
6557 	ire_mp->b_datap->db_type = IRE_ARPRESOLVE_TYPE;
6558 	ire = (ire_t *)buf;
6559 	/*
6560 	 * keep enough info in the fake ire so that we can pull up
6561 	 * the incomplete ire (in_ire) after result comes back from
6562 	 * arp and make it complete.
6563 	 */
6564 	*ire = ire_null;
6565 	ire->ire_u = in_ire->ire_u;
6566 	ire->ire_ipif_seqid = in_ire->ire_ipif_seqid;
6567 	ire->ire_ipif = in_ire->ire_ipif;
6568 	ire->ire_stq = in_ire->ire_stq;
6569 	ill = ire_to_ill(ire);
6570 	ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
6571 	ire->ire_zoneid = in_ire->ire_zoneid;
6572 	ire->ire_ipst = ipst;
6573 
6574 	/*
6575 	 * ire_freemblk will be called when ire_mp is freed, both for
6576 	 * successful and failed arp resolution. IRE_MARK_UNCACHED will be set
6577 	 * when the arp resolution failed.
6578 	 */
6579 	ire->ire_marks |= IRE_MARK_UNCACHED;
6580 	ire->ire_mp = ire_mp;
6581 	ire_mp->b_wptr = (uchar_t *)&ire[1];
6582 	ire_mp->b_cont = NULL;
6583 	ASSERT(dlureq_mp != NULL);
6584 	linkb(dlureq_mp, ire_mp);
6585 
6586 	/*
6587 	 * Fill in the source and dest addrs for the resolver.
6588 	 * NOTE: this depends on memory layouts imposed by
6589 	 * ill_init().
6590 	 */
6591 	areq = (areq_t *)dlureq_mp->b_rptr;
6592 	addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset);
6593 	*addrp = ire->ire_src_addr;
6594 
6595 	addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset);
6596 	if (ire->ire_gateway_addr != INADDR_ANY) {
6597 		*addrp = ire->ire_gateway_addr;
6598 	} else {
6599 		*addrp = ire->ire_addr;
6600 	}
6601 
6602 	/* Up to the resolver. */
6603 	if (canputnext(dst_ill->ill_rq)) {
6604 		putnext(dst_ill->ill_rq, dlureq_mp);
6605 	} else {
6606 		/* Prepare for cleanup */
6607 		freemsg(dlureq_mp);
6608 	}
6609 }
6610 
6611 /*
6612  * Esballoc free function for AR_ENTRY_QUERY request to clean up any
6613  * unresolved ire_t and/or nce_t structures when ARP resolution fails.
6614  *
6615  * This function can be called by ARP via free routine for ire_mp or
6616  * by IPv4(both host and forwarding path) via ire_delete
6617  * in case ARP resolution fails.
6618  * NOTE: Since IP is MT, ARP can call into IP but not vice versa
6619  * (for IP to talk to ARP, it still has to send AR* messages).
6620  *
6621  * Note that the ARP/IP merge should replace the functioanlity by providing
6622  * direct function calls to clean up unresolved entries in ire/nce lists.
6623  */
6624 void
6625 ire_freemblk(ire_t *ire_mp)
6626 {
6627 	nce_t		*nce = NULL;
6628 	ill_t		*ill;
6629 	ip_stack_t	*ipst;
6630 
6631 	ASSERT(ire_mp != NULL);
6632 
6633 	if ((ire_mp->ire_addr == NULL) && (ire_mp->ire_gateway_addr == NULL)) {
6634 		ip1dbg(("ire_freemblk(0x%p) ire_addr is NULL\n",
6635 		    (void *)ire_mp));
6636 		goto cleanup;
6637 	}
6638 	if ((ire_mp->ire_marks & IRE_MARK_UNCACHED) == 0) {
6639 		goto cleanup; /* everything succeeded. just free and return */
6640 	}
6641 
6642 	/*
6643 	 * the arp information corresponding to this ire_mp was not
6644 	 * transferred to  a ire_cache entry. Need
6645 	 * to clean up incomplete ire's and nce, if necessary.
6646 	 */
6647 	ASSERT(ire_mp->ire_stq != NULL);
6648 	ASSERT(ire_mp->ire_stq_ifindex != 0);
6649 	ASSERT(ire_mp->ire_ipst != NULL);
6650 
6651 	ipst = ire_mp->ire_ipst;
6652 
6653 	/*
6654 	 * Get any nce's corresponding to this ire_mp. We first have to
6655 	 * make sure that the ill is still around.
6656 	 */
6657 	ill = ill_lookup_on_ifindex(ire_mp->ire_stq_ifindex,
6658 	    B_FALSE, NULL, NULL, NULL, NULL, ipst);
6659 	if (ill == NULL || (ire_mp->ire_stq != ill->ill_wq) ||
6660 	    (ill->ill_state_flags & ILL_CONDEMNED)) {
6661 		/*
6662 		 * ill went away. no nce to clean up.
6663 		 * Note that the ill_state_flags could be set to
6664 		 * ILL_CONDEMNED after this point, but if we know
6665 		 * that it is CONDEMNED now, we just bail out quickly.
6666 		 */
6667 		if (ill != NULL)
6668 			ill_refrele(ill);
6669 		goto cleanup;
6670 	}
6671 	nce = ndp_lookup_v4(ill,
6672 	    ((ire_mp->ire_gateway_addr != INADDR_ANY) ?
6673 	    &ire_mp->ire_gateway_addr : &ire_mp->ire_addr),
6674 	    B_FALSE);
6675 	ill_refrele(ill);
6676 
6677 	if ((nce != NULL) && (nce->nce_state != ND_REACHABLE)) {
6678 		/*
6679 		 * some incomplete nce was found.
6680 		 */
6681 		DTRACE_PROBE2(ire__freemblk__arp__resolv__fail,
6682 		    nce_t *, nce, ire_t *, ire_mp);
6683 		/*
6684 		 * Send the icmp_unreachable messages for the queued mblks in
6685 		 * ire->ire_nce->nce_qd_mp, since ARP resolution failed
6686 		 * for this ire
6687 		 */
6688 		arp_resolv_failed(nce);
6689 		/*
6690 		 * Delete the nce and clean up all ire's pointing at this nce
6691 		 * in the cachetable
6692 		 */
6693 		ndp_delete(nce);
6694 	}
6695 	if (nce != NULL)
6696 		NCE_REFRELE(nce); /* release the ref taken by ndp_lookup_v4 */
6697 
6698 cleanup:
6699 	/*
6700 	 * Get rid of the ire buffer
6701 	 * We call kmem_free here(instead of ire_delete()), since
6702 	 * this is the freeb's callback.
6703 	 */
6704 	kmem_free(ire_mp, sizeof (ire_t) + sizeof (frtn_t));
6705 }
6706 
6707 
6708 /*
6709  * create the neighbor cache entry  nce_t for  IRE_CACHE and
6710  * non-loopback IRE_BROADCAST ire's. Note that IRE_BROADCAST
6711  * (non-loopback) entries  have the nce_res_mp set to the
6712  * template passed in (generated from ill_bcast_mp); IRE_CACHE ire's
6713  * contain the information for  the nexthop (ire_gateway_addr) in the
6714  * case of indirect routes, and for the dst itself (ire_addr) in the
6715  * case of direct routes, with the nce_res_mp containing a template
6716  * DL_UNITDATA request.
6717  *
6718  * This function always consumes res_mp and fp_mp.
6719  *
6720  * The actual association of the ire_nce to the nce created here is
6721  * typically done in ire_add_v4 for IRE_CACHE entries. Exceptions
6722  * to this rule are SO_DONTROUTE ire's (IRE_MARK_NO_ADD), for which
6723  * the ire_nce assignment is done in ire_add_then_send, and mobile-ip
6724  * where the assignment is done in ire_add_mrtun().
6725  */
6726 int
6727 ire_nce_init(ire_t *ire, mblk_t *fp_mp, mblk_t *res_mp)
6728 {
6729 	in_addr_t	addr4, mask4;
6730 	int		err;
6731 	nce_t		*arpce = NULL;
6732 	ill_t		*ire_ill;
6733 	uint16_t	nce_state, nce_flags;
6734 	ip_stack_t	*ipst;
6735 
6736 	if (ire->ire_stq == NULL) {
6737 		if (res_mp)
6738 			freemsg(res_mp);
6739 		if (fp_mp)
6740 			freemsg(fp_mp);
6741 		return (0); /* no need to create nce for local/loopback */
6742 	}
6743 
6744 	mask4 = IP_HOST_MASK;
6745 	switch (ire->ire_type) {
6746 	case IRE_CACHE:
6747 		if (ire->ire_gateway_addr != INADDR_ANY)
6748 			addr4 = ire->ire_gateway_addr; /* 'G' route */
6749 		else
6750 			addr4 = ire->ire_addr; /* direct route */
6751 		break;
6752 	case IRE_BROADCAST:
6753 		addr4 = ire->ire_addr;
6754 		break;
6755 	default:
6756 		if (res_mp)
6757 			freemsg(res_mp);
6758 		if (fp_mp)
6759 			freemsg(fp_mp);
6760 		return (0);
6761 	}
6762 
6763 	/*
6764 	 * ire_ipif is picked based on RTF_SETSRC, usesrc etc.
6765 	 * rules in ire_forward_src_ipif. We want the dlureq_mp
6766 	 * for the outgoing interface, which we get from the ire_stq.
6767 	 */
6768 	ire_ill = ire_to_ill(ire);
6769 	ipst = ire_ill->ill_ipst;
6770 
6771 	/*
6772 	 * if we are creating an nce for the first time, and this is
6773 	 * a NORESOLVER interface, atomically create the nce in the
6774 	 * REACHABLE state; else create it in the ND_INITIAL state.
6775 	 */
6776 	if (ire_ill->ill_net_type == IRE_IF_NORESOLVER)  {
6777 		nce_state = ND_REACHABLE;
6778 		nce_flags = NCE_F_PERMANENT;
6779 	} else {
6780 		if (fp_mp != NULL)
6781 			nce_state = ND_REACHABLE;
6782 		else
6783 			nce_state = ND_INITIAL;
6784 		nce_flags = 0;
6785 	}
6786 
6787 	err = ndp_lookup_then_add(ire_ill, NULL,
6788 	    &addr4, &mask4, NULL, 0, nce_flags, nce_state, &arpce,
6789 	    fp_mp, res_mp);
6790 
6791 	ip1dbg(("ire 0x%p addr 0x%lx mask 0x%lx type 0x%x; "
6792 	    "found nce 0x%p err %d\n", (void *)ire, (ulong_t)addr4,
6793 	    (ulong_t)mask4, ire->ire_type, (void *)arpce, err));
6794 
6795 	switch (err) {
6796 	case 0:
6797 		break;
6798 	case EEXIST:
6799 		/*
6800 		 * return a pointer to an existing nce_t;
6801 		 * note that the ire-nce mapping is many-one, i.e.,
6802 		 * multiple ire's could point to the same nce_t;
6803 		 */
6804 		if (fp_mp != NULL) {
6805 			freemsg(fp_mp);
6806 		}
6807 		if (res_mp != NULL) {
6808 			freemsg(res_mp);
6809 		}
6810 		break;
6811 	default:
6812 		DTRACE_PROBE2(nce__init__fail, ill_t *, ire_ill, int, err);
6813 		if (res_mp)
6814 			freemsg(res_mp);
6815 		if (fp_mp)
6816 			freemsg(fp_mp);
6817 		return (EINVAL);
6818 	}
6819 #if DEBUG
6820 	/*
6821 	 * if an nce_fp_mp was passed in, we should be picking up an
6822 	 * existing nce_t in the ND_REACHABLE state.
6823 	 */
6824 	mutex_enter(&arpce->nce_lock);
6825 	ASSERT(arpce->nce_fp_mp == NULL || arpce->nce_state == ND_REACHABLE);
6826 	mutex_exit(&arpce->nce_lock);
6827 #endif
6828 	if (ire->ire_type == IRE_BROADCAST) {
6829 		/*
6830 		 * Two bcast ires are created for each interface;
6831 		 * 1. loopback copy (which does not  have an
6832 		 *    ire_stq, and therefore has no ire_nce), and,
6833 		 * 2. the non-loopback copy, which has the nce_res_mp
6834 		 *    initialized to a copy of the ill_bcast_mp, and
6835 		 *    is marked as ND_REACHABLE at this point.
6836 		 *    This nce does not undergo any further state changes,
6837 		 *    and exists as long as the interface is plumbed.
6838 		 * Note: we do the ire_nce assignment here for IRE_BROADCAST
6839 		 * because some functions like ill_mark_bcast() inline the
6840 		 * ire_add functionality;
6841 		 */
6842 		mutex_enter(&arpce->nce_lock);
6843 		arpce->nce_state = ND_REACHABLE;
6844 		arpce->nce_flags |= (NCE_F_PERMANENT | NCE_F_BCAST);
6845 		arpce->nce_last = TICK_TO_MSEC(lbolt64);
6846 		ire->ire_nce = arpce;
6847 		mutex_exit(&arpce->nce_lock);
6848 		/*
6849 		 * We are associating this nce to the ire,
6850 		 * so change the nce ref taken in
6851 		 * ndp_lookup_then_add_v4() from
6852 		 * NCE_REFHOLD to NCE_REFHOLD_NOTR
6853 		 */
6854 		NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
6855 	} else {
6856 		if (NCE_EXPIRED(arpce, ipst))
6857 			arpce = nce_reinit(arpce);
6858 		if (arpce != NULL) {
6859 			/*
6860 			 * We are not using this nce_t just yet so release
6861 			 * the ref taken in ndp_lookup_then_add_v4()
6862 			 */
6863 			NCE_REFRELE(arpce);
6864 		} else {
6865 			ip0dbg(("can't reinit arpce for ill 0x%p;\n",
6866 			    (void *)ire_ill));
6867 		}
6868 	}
6869 	return (0);
6870 }
6871