xref: /titanic_51/usr/src/uts/common/inet/ip/ip_ftable.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
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 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This file contains consumer routines of the IPv4 forwarding engine
30  */
31 
32 #include <sys/types.h>
33 #include <sys/stream.h>
34 #include <sys/stropts.h>
35 #include <sys/strlog.h>
36 #include <sys/dlpi.h>
37 #include <sys/ddi.h>
38 #include <sys/cmn_err.h>
39 #include <sys/policy.h>
40 
41 #include <sys/systm.h>
42 #include <sys/strsun.h>
43 #include <sys/kmem.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/strsubr.h>
47 #include <sys/pattr.h>
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <net/if_dl.h>
52 #include <netinet/ip6.h>
53 #include <netinet/icmp6.h>
54 
55 #include <inet/common.h>
56 #include <inet/mi.h>
57 #include <inet/mib2.h>
58 #include <inet/ip.h>
59 #include <inet/ip_impl.h>
60 #include <inet/ip6.h>
61 #include <inet/ip_ndp.h>
62 #include <inet/arp.h>
63 #include <inet/ip_if.h>
64 #include <inet/ip_ire.h>
65 #include <inet/ip_ftable.h>
66 #include <inet/ip_rts.h>
67 #include <inet/nd.h>
68 
69 #include <net/pfkeyv2.h>
70 #include <inet/ipsec_info.h>
71 #include <inet/sadb.h>
72 #include <sys/kmem.h>
73 #include <inet/tcp.h>
74 #include <inet/ipclassifier.h>
75 #include <sys/zone.h>
76 #include <net/radix.h>
77 #include <sys/tsol/label.h>
78 #include <sys/tsol/tnet.h>
79 
80 #define	IS_DEFAULT_ROUTE(ire)	\
81 	(((ire)->ire_type & IRE_DEFAULT) || \
82 	    (((ire)->ire_type & IRE_INTERFACE) && ((ire)->ire_addr == 0)))
83 
84 /*
85  * structure for passing args between ire_ftable_lookup and ire_find_best_route
86  */
87 typedef struct ire_ftable_args_s {
88 	ipaddr_t	ift_addr;
89 	ipaddr_t	ift_mask;
90 	ipaddr_t	ift_gateway;
91 	int		ift_type;
92 	const ipif_t		*ift_ipif;
93 	zoneid_t	ift_zoneid;
94 	uint32_t	ift_ihandle;
95 	const ts_label_t	*ift_tsl;
96 	int		ift_flags;
97 	ire_t		*ift_best_ire;
98 } ire_ftable_args_t;
99 
100 static ire_t	*route_to_dst(const struct sockaddr *, zoneid_t, ip_stack_t *);
101 static ire_t   	*ire_round_robin(irb_t *, zoneid_t, ire_ftable_args_t *,
102     ip_stack_t *);
103 static void		ire_del_host_redir(ire_t *, char *);
104 static boolean_t	ire_find_best_route(struct radix_node *, void *);
105 static int	ip_send_align_hcksum_flags(mblk_t *, ill_t *);
106 
107 /*
108  * Lookup a route in forwarding table. A specific lookup is indicated by
109  * passing the required parameters and indicating the match required in the
110  * flag field.
111  *
112  * Looking for default route can be done in three ways
113  * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
114  *    along with other matches.
115  * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
116  *    field along with other matches.
117  * 3) if the destination and mask are passed as zeros.
118  *
119  * A request to return a default route if no route
120  * is found, can be specified by setting MATCH_IRE_DEFAULT
121  * in flags.
122  *
123  * It does not support recursion more than one level. It
124  * will do recursive lookup only when the lookup maps to
125  * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
126  *
127  * If the routing table is setup to allow more than one level
128  * of recursion, the cleaning up cache table will not work resulting
129  * in invalid routing.
130  *
131  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
132  *
133  * NOTE : When this function returns NULL, pire has already been released.
134  *	  pire is valid only when this function successfully returns an
135  *	  ire.
136  */
137 ire_t *
138 ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
139     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
140     uint32_t ihandle, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
141 {
142 	ire_t *ire = NULL;
143 	ipaddr_t gw_addr;
144 	struct rt_sockaddr rdst, rmask;
145 	struct rt_entry *rt;
146 	ire_ftable_args_t margs;
147 	boolean_t found_incomplete = B_FALSE;
148 
149 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
150 
151 	/*
152 	 * When we return NULL from this function, we should make
153 	 * sure that *pire is NULL so that the callers will not
154 	 * wrongly REFRELE the pire.
155 	 */
156 	if (pire != NULL)
157 		*pire = NULL;
158 	/*
159 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
160 	 * MATCH_IRE_ILL is set.
161 	 */
162 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
163 	    (ipif == NULL))
164 		return (NULL);
165 
166 	(void) memset(&rdst, 0, sizeof (rdst));
167 	rdst.rt_sin_len = sizeof (rdst);
168 	rdst.rt_sin_family = AF_INET;
169 	rdst.rt_sin_addr.s_addr = addr;
170 
171 	(void) memset(&rmask, 0, sizeof (rmask));
172 	rmask.rt_sin_len = sizeof (rmask);
173 	rmask.rt_sin_family = AF_INET;
174 	rmask.rt_sin_addr.s_addr = mask;
175 
176 	(void) memset(&margs, 0, sizeof (margs));
177 	margs.ift_addr = addr;
178 	margs.ift_mask = mask;
179 	margs.ift_gateway = gateway;
180 	margs.ift_type = type;
181 	margs.ift_ipif = ipif;
182 	margs.ift_zoneid = zoneid;
183 	margs.ift_ihandle = ihandle;
184 	margs.ift_tsl = tsl;
185 	margs.ift_flags = flags;
186 
187 	/*
188 	 * The flags argument passed to ire_ftable_lookup may cause the
189 	 * search to return, not the longest matching prefix, but the
190 	 * "best matching prefix", i.e., the longest prefix that also
191 	 * satisfies constraints imposed via the permutation of flags
192 	 * passed in. To achieve this, we invoke ire_match_args() on
193 	 * each matching leaf in the  radix tree. ire_match_args is
194 	 * invoked by the callback function ire_find_best_route()
195 	 * We hold the global tree lock in read mode when calling
196 	 * rn_match_args.Before dropping the global tree lock, ensure
197 	 * that the radix node can't be deleted by incrementing ire_refcnt.
198 	 */
199 	RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
200 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
201 	    ipst->ips_ip_ftable, ire_find_best_route, &margs);
202 	ire = margs.ift_best_ire;
203 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
204 
205 	if (rt == NULL) {
206 		return (NULL);
207 	} else {
208 		ASSERT(ire != NULL);
209 	}
210 
211 	DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire);
212 
213 	if (!IS_DEFAULT_ROUTE(ire))
214 		goto found_ire_held;
215 	/*
216 	 * If default route is found, see if default matching criteria
217 	 * are satisfied.
218 	 */
219 	if (flags & MATCH_IRE_MASK) {
220 		/*
221 		 * we were asked to match a 0 mask, and came back with
222 		 * a default route. Ok to return it.
223 		 */
224 		goto found_default_ire;
225 	}
226 	if ((flags & MATCH_IRE_TYPE) &&
227 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
228 		/*
229 		 * we were asked to match a default ire type. Ok to return it.
230 		 */
231 		goto found_default_ire;
232 	}
233 	if (flags & MATCH_IRE_DEFAULT) {
234 		goto found_default_ire;
235 	}
236 	/*
237 	 * we found a default route, but default matching criteria
238 	 * are not specified and we are not explicitly looking for
239 	 * default.
240 	 */
241 	IRE_REFRELE(ire);
242 	return (NULL);
243 found_default_ire:
244 	/*
245 	 * round-robin only if we have more than one route in the bucket.
246 	 */
247 	if ((ire->ire_bucket->irb_ire_cnt > 1) &&
248 	    IS_DEFAULT_ROUTE(ire) &&
249 	    ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
250 	    MATCH_IRE_DEFAULT)) {
251 		ire_t *next_ire;
252 
253 		next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs,
254 		    ipst);
255 		IRE_REFRELE(ire);
256 		if (next_ire != NULL) {
257 			ire = next_ire;
258 		} else {
259 			/* no route */
260 			return (NULL);
261 		}
262 	}
263 found_ire_held:
264 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
265 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
266 		return (ire);
267 	}
268 	/*
269 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
270 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
271 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
272 	 * IRE (one of the prefix types), then it is necessary to fill in the
273 	 * parent IRE pointed to by pire, and then lookup the gateway address of
274 	 * the parent.  For backwards compatiblity, if this lookup returns an
275 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
276 	 * of lookup is done.
277 	 */
278 	if (flags & MATCH_IRE_RECURSIVE) {
279 		ipif_t	*gw_ipif;
280 		int match_flags = MATCH_IRE_DSTONLY;
281 		ire_t *save_ire;
282 
283 		if (ire->ire_type & IRE_INTERFACE)
284 			return (ire);
285 		if (pire != NULL)
286 			*pire = ire;
287 		/*
288 		 * If we can't find an IRE_INTERFACE or the caller has not
289 		 * asked for pire, we need to REFRELE the save_ire.
290 		 */
291 		save_ire = ire;
292 
293 		/*
294 		 * Currently MATCH_IRE_ILL is never used with
295 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
296 		 * sending out packets as MATCH_IRE_ILL is used only
297 		 * for communicating with on-link hosts. We can't assert
298 		 * that here as RTM_GET calls this function with
299 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
300 		 * We have already used the MATCH_IRE_ILL in determining
301 		 * the right prefix route at this point. To match the
302 		 * behavior of how we locate routes while sending out
303 		 * packets, we don't want to use MATCH_IRE_ILL below
304 		 * while locating the interface route.
305 		 *
306 		 * ire_ftable_lookup may end up with an incomplete IRE_CACHE
307 		 * entry for the gateway (i.e., one for which the
308 		 * ire_nce->nce_state is not yet ND_REACHABLE). If the caller
309 		 * has specified MATCH_IRE_COMPLETE, such entries will not
310 		 * be returned; instead, we return the IF_RESOLVER ire.
311 		 */
312 		if (ire->ire_ipif != NULL)
313 			match_flags |= MATCH_IRE_ILL_GROUP;
314 
315 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
316 		    ire->ire_ipif, NULL, zoneid, tsl, match_flags, ipst);
317 		DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire,
318 		    (ire_t *), save_ire);
319 		if (ire == NULL ||
320 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
321 		    ire->ire_nce->nce_state != ND_REACHABLE &&
322 		    (flags & MATCH_IRE_COMPLETE))) {
323 			/*
324 			 * Do not release the parent ire if MATCH_IRE_PARENT
325 			 * is set. Also return it via ire.
326 			 */
327 			if (ire != NULL) {
328 				ire_refrele(ire);
329 				ire = NULL;
330 				found_incomplete = B_TRUE;
331 			}
332 			if (flags & MATCH_IRE_PARENT) {
333 				if (pire != NULL) {
334 					/*
335 					 * Need an extra REFHOLD, if the parent
336 					 * ire is returned via both ire and
337 					 * pire.
338 					 */
339 					IRE_REFHOLD(save_ire);
340 				}
341 				ire = save_ire;
342 			} else {
343 				ire_refrele(save_ire);
344 				if (pire != NULL)
345 					*pire = NULL;
346 			}
347 			if (!found_incomplete)
348 				return (ire);
349 		}
350 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
351 			/*
352 			 * If the caller did not ask for pire, release
353 			 * it now.
354 			 */
355 			if (pire == NULL) {
356 				ire_refrele(save_ire);
357 			}
358 			return (ire);
359 		}
360 		match_flags |= MATCH_IRE_TYPE;
361 		gw_addr = ire->ire_gateway_addr;
362 		gw_ipif = ire->ire_ipif;
363 		ire_refrele(ire);
364 		ire = ire_route_lookup(gw_addr, 0, 0,
365 		    (found_incomplete? IRE_INTERFACE :
366 		    (IRE_CACHETABLE | IRE_INTERFACE)),
367 		    gw_ipif, NULL, zoneid, tsl, match_flags, ipst);
368 		DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire,
369 		    (ire_t *), save_ire);
370 		if (ire == NULL ||
371 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
372 		    ire->ire_nce->nce_state != ND_REACHABLE &&
373 		    (flags & MATCH_IRE_COMPLETE))) {
374 			/*
375 			 * Do not release the parent ire if MATCH_IRE_PARENT
376 			 * is set. Also return it via ire.
377 			 */
378 			if (ire != NULL) {
379 				ire_refrele(ire);
380 				ire = NULL;
381 			}
382 			if (flags & MATCH_IRE_PARENT) {
383 				if (pire != NULL) {
384 					/*
385 					 * Need an extra REFHOLD, if the
386 					 * parent ire is returned via both
387 					 * ire and pire.
388 					 */
389 					IRE_REFHOLD(save_ire);
390 				}
391 				ire = save_ire;
392 			} else {
393 				ire_refrele(save_ire);
394 				if (pire != NULL)
395 					*pire = NULL;
396 			}
397 			return (ire);
398 		} else if (pire == NULL) {
399 			/*
400 			 * If the caller did not ask for pire, release
401 			 * it now.
402 			 */
403 			ire_refrele(save_ire);
404 		}
405 		return (ire);
406 	}
407 	ASSERT(pire == NULL || *pire == NULL);
408 	return (ire);
409 }
410 
411 
412 /*
413  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
414  * that goes through 'ipif'. As a fallback, a route that goes through
415  * ipif->ipif_ill can be returned.
416  */
417 ire_t *
418 ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
419 {
420 	ire_t	*ire;
421 	ire_t	*save_ire = NULL;
422 	ire_t   *gw_ire;
423 	irb_t   *irb;
424 	ipaddr_t gw_addr;
425 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
426 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
427 
428 	ASSERT(CLASSD(group));
429 
430 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
431 	    NULL, MATCH_IRE_DEFAULT, ipst);
432 
433 	if (ire == NULL)
434 		return (NULL);
435 
436 	irb = ire->ire_bucket;
437 	ASSERT(irb);
438 
439 	IRB_REFHOLD(irb);
440 	ire_refrele(ire);
441 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
442 		if (ire->ire_addr != group ||
443 		    ipif->ipif_zoneid != ire->ire_zoneid &&
444 		    ire->ire_zoneid != ALL_ZONES) {
445 			continue;
446 		}
447 
448 		switch (ire->ire_type) {
449 		case IRE_DEFAULT:
450 		case IRE_PREFIX:
451 		case IRE_HOST:
452 			gw_addr = ire->ire_gateway_addr;
453 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
454 			    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
455 
456 			if (gw_ire != NULL) {
457 				if (save_ire != NULL) {
458 					ire_refrele(save_ire);
459 				}
460 				IRE_REFHOLD(ire);
461 				if (gw_ire->ire_ipif == ipif) {
462 					ire_refrele(gw_ire);
463 
464 					IRB_REFRELE(irb);
465 					return (ire);
466 				}
467 				ire_refrele(gw_ire);
468 				save_ire = ire;
469 			}
470 			break;
471 		case IRE_IF_NORESOLVER:
472 		case IRE_IF_RESOLVER:
473 			if (ire->ire_ipif == ipif) {
474 				if (save_ire != NULL) {
475 					ire_refrele(save_ire);
476 				}
477 				IRE_REFHOLD(ire);
478 
479 				IRB_REFRELE(irb);
480 				return (ire);
481 			}
482 			break;
483 		}
484 	}
485 	IRB_REFRELE(irb);
486 
487 	return (save_ire);
488 }
489 
490 /*
491  * Find an IRE_INTERFACE for the multicast group.
492  * Allows different routes for multicast addresses
493  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
494  * which point at different interfaces. This is used when IP_MULTICAST_IF
495  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
496  * specify the interface to join on.
497  *
498  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
499  */
500 ire_t *
501 ire_lookup_multi(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
502 {
503 	ire_t	*ire;
504 	ipif_t	*ipif = NULL;
505 	int	match_flags = MATCH_IRE_TYPE;
506 	ipaddr_t gw_addr;
507 
508 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
509 	    0, NULL, MATCH_IRE_DEFAULT, ipst);
510 
511 	/* We search a resolvable ire in case of multirouting. */
512 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
513 		ire_t *cire = NULL;
514 		/*
515 		 * If the route is not resolvable, the looked up ire
516 		 * may be changed here. In that case, ire_multirt_lookup()
517 		 * IRE_REFRELE the original ire and change it.
518 		 */
519 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW,
520 		    NULL, ipst);
521 		if (cire != NULL)
522 			ire_refrele(cire);
523 	}
524 	if (ire == NULL)
525 		return (NULL);
526 	/*
527 	 * Make sure we follow ire_ipif.
528 	 *
529 	 * We need to determine the interface route through
530 	 * which the gateway will be reached. We don't really
531 	 * care which interface is picked if the interface is
532 	 * part of a group.
533 	 */
534 	if (ire->ire_ipif != NULL) {
535 		ipif = ire->ire_ipif;
536 		match_flags |= MATCH_IRE_ILL_GROUP;
537 	}
538 
539 	switch (ire->ire_type) {
540 	case IRE_DEFAULT:
541 	case IRE_PREFIX:
542 	case IRE_HOST:
543 		gw_addr = ire->ire_gateway_addr;
544 		ire_refrele(ire);
545 		ire = ire_ftable_lookup(gw_addr, 0, 0,
546 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
547 		    NULL, match_flags, ipst);
548 		return (ire);
549 	case IRE_IF_NORESOLVER:
550 	case IRE_IF_RESOLVER:
551 		return (ire);
552 	default:
553 		ire_refrele(ire);
554 		return (NULL);
555 	}
556 }
557 
558 /*
559  * Delete the passed in ire if the gateway addr matches
560  */
561 void
562 ire_del_host_redir(ire_t *ire, char *gateway)
563 {
564 	if ((ire->ire_flags & RTF_DYNAMIC) &&
565 	    (ire->ire_gateway_addr == *(ipaddr_t *)gateway))
566 		ire_delete(ire);
567 }
568 
569 /*
570  * Search for all HOST REDIRECT routes that are
571  * pointing at the specified gateway and
572  * delete them. This routine is called only
573  * when a default gateway is going away.
574  */
575 void
576 ire_delete_host_redirects(ipaddr_t gateway, ip_stack_t *ipst)
577 {
578 	struct rtfuncarg rtfarg;
579 
580 	(void) memset(&rtfarg, 0, sizeof (rtfarg));
581 	rtfarg.rt_func = ire_del_host_redir;
582 	rtfarg.rt_arg = (void *)&gateway;
583 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
584 	    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
585 }
586 
587 struct ihandle_arg {
588 	uint32_t ihandle;
589 	ire_t	 *ire;
590 };
591 
592 static int
593 ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
594 {
595 	struct rt_entry *rt;
596 	irb_t *irb;
597 	ire_t *ire;
598 	struct ihandle_arg *ih = arg;
599 
600 	rt = (struct rt_entry *)rn;
601 	ASSERT(rt != NULL);
602 	irb = &rt->rt_irb;
603 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
604 		if ((ire->ire_type & IRE_INTERFACE) &&
605 		    (ire->ire_ihandle == ih->ihandle)) {
606 			ih->ire = ire;
607 			IRE_REFHOLD(ire);
608 			return (1);
609 		}
610 	}
611 	return (0);
612 }
613 
614 /*
615  * Locate the interface ire that is tied to the cache ire 'cire' via
616  * cire->ire_ihandle.
617  *
618  * We are trying to create the cache ire for an onlink destn. or
619  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
620  * case, after the ire has come back from ARP.
621  */
622 ire_t *
623 ire_ihandle_lookup_onlink(ire_t *cire)
624 {
625 	ire_t	*ire;
626 	int	match_flags;
627 	struct ihandle_arg ih;
628 	ip_stack_t *ipst;
629 
630 	ASSERT(cire != NULL);
631 	ipst = cire->ire_ipst;
632 
633 	/*
634 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
635 	 * because the ihandle refers to an ipif which can be in only one zone.
636 	 */
637 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
638 	/*
639 	 * We know that the mask of the interface ire equals cire->ire_cmask.
640 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
641 	 * cmask from the interface ire's mask)
642 	 */
643 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
644 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
645 	    NULL, match_flags, ipst);
646 	if (ire != NULL)
647 		return (ire);
648 	/*
649 	 * If we didn't find an interface ire above, we can't declare failure.
650 	 * For backwards compatibility, we need to support prefix routes
651 	 * pointing to next hop gateways that are not on-link.
652 	 *
653 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
654 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
655 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
656 	 * doing ire_route_lookup() twice and returning an interface ire.
657 	 *
658 	 * Eg. default	-	gw1			(line 1)
659 	 *	gw1	-	gw2			(line 2)
660 	 *	gw2	-	hme0			(line 3)
661 	 *
662 	 * In the above example, ip_newroute() tried to create the cache ire
663 	 * 'cire' for gw1, based on the interface route in line 3. The
664 	 * ire_ftable_lookup() above fails, because there is no interface route
665 	 * to reach gw1. (it is gw2). We fall thru below.
666 	 *
667 	 * Do a brute force search based on the ihandle in a subset of the
668 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
669 	 * things become very complex, since we don't have 'pire' in this
670 	 * case. (Also note that this method is not possible in the offlink
671 	 * case because we don't know the mask)
672 	 */
673 	(void) memset(&ih, 0, sizeof (ih));
674 	ih.ihandle = cire->ire_ihandle;
675 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
676 	    ire_ihandle_onlink_match, &ih, irb_refhold_rn, irb_refrele_rn);
677 	return (ih.ire);
678 }
679 
680 /*
681  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
682  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
683  * in the bucket skipping default interface routes and deleted entries.
684  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
685  * Assumes that the caller holds a reference on the IRE bucket.
686  */
687 ire_t *
688 ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
689 {
690 	ASSERT(ire_origin->ire_bucket != NULL);
691 	ASSERT(ire != NULL);
692 
693 	do {
694 		ire = ire->ire_next;
695 		if (ire == NULL)
696 			ire = ire_origin->ire_bucket->irb_ire;
697 		if (ire == ire_origin)
698 			return (NULL);
699 	} while ((ire->ire_type & IRE_INTERFACE) ||
700 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
701 	ASSERT(ire != NULL);
702 	return (ire);
703 }
704 
705 static ipif_t *
706 ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
707     int zoneid, ushort_t *marks)
708 {
709 	ipif_t *src_ipif;
710 	ip_stack_t *ipst = dst_ill->ill_ipst;
711 
712 	/*
713 	 * Pick the best source address from dst_ill.
714 	 *
715 	 * 1) If it is part of a multipathing group, we would
716 	 *    like to spread the inbound packets across different
717 	 *    interfaces. ipif_select_source picks a random source
718 	 *    across the different ills in the group.
719 	 *
720 	 * 2) If it is not part of a multipathing group, we try
721 	 *    to pick the source address from the destination
722 	 *    route. Clustering assumes that when we have multiple
723 	 *    prefixes hosted on an interface, the prefix of the
724 	 *    source address matches the prefix of the destination
725 	 *    route. We do this only if the address is not
726 	 *    DEPRECATED.
727 	 *
728 	 * 3) If the conn is in a different zone than the ire, we
729 	 *    need to pick a source address from the right zone.
730 	 *
731 	 * NOTE : If we hit case (1) above, the prefix of the source
732 	 *	  address picked may not match the prefix of the
733 	 *	  destination routes prefix as ipif_select_source
734 	 *	  does not look at "dst" while picking a source
735 	 *	  address.
736 	 *	  If we want the same behavior as (2), we will need
737 	 *	  to change the behavior of ipif_select_source.
738 	 */
739 
740 	if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
741 		/*
742 		 * The RTF_SETSRC flag is set in the parent ire (sire).
743 		 * Check that the ipif matching the requested source
744 		 * address still exists.
745 		 */
746 		src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
747 		    zoneid, NULL, NULL, NULL, NULL, ipst);
748 		return (src_ipif);
749 	}
750 	*marks |= IRE_MARK_USESRC_CHECK;
751 	if ((dst_ill->ill_group != NULL) ||
752 	    (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
753 	    (dst_ill->ill_usesrc_ifindex != 0)) {
754 		src_ipif = ipif_select_source(dst_ill, dst, zoneid);
755 		if (src_ipif == NULL)
756 			return (NULL);
757 
758 	} else {
759 		src_ipif = ire->ire_ipif;
760 		ASSERT(src_ipif != NULL);
761 		/* hold src_ipif for uniformity */
762 		ipif_refhold(src_ipif);
763 	}
764 	return (src_ipif);
765 }
766 
767 /*
768  * This function is called by ip_rput_noire() and ip_fast_forward()
769  * to resolve the route of incoming packet that needs to be forwarded.
770  * If the ire of the nexthop is not already in the cachetable, this
771  * routine will insert it to the table, but won't trigger ARP resolution yet.
772  * Thus unlike ip_newroute, this function adds incomplete ires to
773  * the cachetable. ARP resolution for these ires are  delayed until
774  * after all of the packet processing is completed and its ready to
775  * be sent out on the wire, Eventually, the packet transmit routine
776  * ip_xmit_v4() attempts to send a packet  to the driver. If it finds
777  * that there is no link layer information, it will do the arp
778  * resolution and queue the packet in ire->ire_nce->nce_qd_mp and
779  * then send it out once the arp resolution is over
780  * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
781  * the model of BSD/SunOS 4
782  *
783  * In future, the insertion of incomplete ires in the cachetable should
784  * be implemented in hostpath as well, as doing so will greatly reduce
785  * the existing complexity for code paths that depend on the context of
786  * the sender (such as IPsec).
787  *
788  * Thus this scheme of adding incomplete ires in cachetable in forwarding
789  * path can be used as a template for simplifying the hostpath.
790  */
791 
792 ire_t *
793 ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
794     ire_t *supplied_sire, const struct ts_label_s *tsl, ip_stack_t *ipst)
795 {
796 	ipaddr_t gw = 0;
797 	ire_t	*ire = NULL;
798 	ire_t   *sire = NULL, *save_ire;
799 	ill_t *dst_ill = NULL;
800 	int error;
801 	zoneid_t zoneid;
802 	ipif_t *src_ipif = NULL;
803 	mblk_t *res_mp;
804 	ushort_t ire_marks = 0;
805 	tsol_gcgrp_t *gcgrp = NULL;
806 	tsol_gcgrp_addr_t ga;
807 
808 	zoneid = GLOBAL_ZONEID;
809 
810 	if (supplied_ire != NULL) {
811 		/* We have arrived here from ipfil_sendpkt */
812 		ire = supplied_ire;
813 		sire = supplied_sire;
814 		goto create_irecache;
815 	}
816 
817 	ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
818 	    tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
819 	    MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR, ipst);
820 
821 	if (ire == NULL) {
822 		ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst);
823 		goto icmp_err_ret;
824 	}
825 
826 	/*
827 	 * If we encounter CGTP, we should  have the caller use
828 	 * ip_newroute to resolve multirt instead of this function.
829 	 * CGTP specs explicitly state that it can't be used with routers.
830 	 * This essentially prevents insertion of incomplete RTF_MULTIRT
831 	 * ires in cachetable.
832 	 */
833 	if (ipst->ips_ip_cgtp_filter &&
834 	    ((ire->ire_flags & RTF_MULTIRT) ||
835 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
836 		ip3dbg(("ire_forward: packet is to be multirouted- "
837 		    "handing it to ip_newroute\n"));
838 		if (sire != NULL)
839 			ire_refrele(sire);
840 		ire_refrele(ire);
841 		/*
842 		 * Inform caller about encountering of multirt so that
843 		 * ip_newroute() can be called.
844 		 */
845 		*check_multirt = B_TRUE;
846 		return (NULL);
847 	}
848 
849 	*check_multirt = B_FALSE;
850 
851 	/*
852 	 * Verify that the returned IRE does not have either
853 	 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
854 	 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
855 	 */
856 	if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
857 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
858 		ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
859 		    (void *)ire));
860 		goto icmp_err_ret;
861 	}
862 
863 	/*
864 	 * If we already have a fully resolved IRE CACHE of the
865 	 * nexthop router, just hand over the cache entry
866 	 * and we are done.
867 	 */
868 
869 	if (ire->ire_type & IRE_CACHE) {
870 
871 		/*
872 		 * If we are using this ire cache entry as a
873 		 * gateway to forward packets, chances are we
874 		 * will be using it again. So turn off
875 		 * the temporary flag, thus reducing its
876 		 * chances of getting deleted frequently.
877 		 */
878 		if (ire->ire_marks & IRE_MARK_TEMPORARY) {
879 			irb_t *irb = ire->ire_bucket;
880 			rw_enter(&irb->irb_lock, RW_WRITER);
881 			ire->ire_marks &= ~IRE_MARK_TEMPORARY;
882 			irb->irb_tmp_ire_cnt--;
883 			rw_exit(&irb->irb_lock);
884 		}
885 
886 		if (sire != NULL) {
887 			UPDATE_OB_PKT_COUNT(sire);
888 			sire->ire_last_used_time = lbolt;
889 			ire_refrele(sire);
890 		}
891 		return (ire);
892 	}
893 create_irecache:
894 	/*
895 	 * Increment the ire_ob_pkt_count field for ire if it is an
896 	 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
897 	 * increment the same for the parent IRE, sire, if it is some
898 	 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST).
899 	 */
900 	if ((ire->ire_type & IRE_INTERFACE) != 0) {
901 		UPDATE_OB_PKT_COUNT(ire);
902 		ire->ire_last_used_time = lbolt;
903 	}
904 
905 	/*
906 	 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
907 	 */
908 	if (sire != NULL) {
909 		gw = sire->ire_gateway_addr;
910 		ASSERT((sire->ire_type &
911 		    (IRE_CACHETABLE | IRE_INTERFACE)) == 0);
912 		UPDATE_OB_PKT_COUNT(sire);
913 		sire->ire_last_used_time = lbolt;
914 	}
915 
916 	/* Obtain dst_ill */
917 	dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
918 	if (dst_ill == NULL) {
919 		ip2dbg(("ire_forward no dst ill; ire 0x%p\n", (void *)ire));
920 		goto icmp_err_ret;
921 	}
922 
923 	ASSERT(src_ipif == NULL);
924 	/* Now obtain the src_ipif */
925 	src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
926 	    zoneid, &ire_marks);
927 	if (src_ipif == NULL)
928 		goto icmp_err_ret;
929 
930 	switch (ire->ire_type) {
931 	case IRE_IF_NORESOLVER:
932 		/* create ire_cache for ire_addr endpoint */
933 	case IRE_IF_RESOLVER:
934 		/*
935 		 * We have the IRE_IF_RESOLVER of the nexthop gateway
936 		 * and now need to build a IRE_CACHE for it.
937 		 * In this case, we have the following :
938 		 *
939 		 * 1) src_ipif - used for getting a source address.
940 		 *
941 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
942 		 *    means packets using the IRE_CACHE that we will build
943 		 *    here will go out on dst_ill.
944 		 *
945 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
946 		 *    to be created will only be tied to the IRE_INTERFACE
947 		 *    that was derived from the ire_ihandle field.
948 		 *
949 		 *    If sire is non-NULL, it means the destination is
950 		 *    off-link and we will first create the IRE_CACHE for the
951 		 *    gateway.
952 		 */
953 		res_mp = dst_ill->ill_resolver_mp;
954 		if (ire->ire_type == IRE_IF_RESOLVER &&
955 		    (!OK_RESOLVER_MP(res_mp))) {
956 			ire_refrele(ire);
957 			ire = NULL;
958 			goto out;
959 		}
960 		/*
961 		 * To be at this point in the code with a non-zero gw
962 		 * means that dst is reachable through a gateway that
963 		 * we have never resolved.  By changing dst to the gw
964 		 * addr we resolve the gateway first.
965 		 */
966 		if (gw != INADDR_ANY) {
967 			/*
968 			 * The source ipif that was determined above was
969 			 * relative to the destination address, not the
970 			 * gateway's. If src_ipif was not taken out of
971 			 * the IRE_IF_RESOLVER entry, we'll need to call
972 			 * ipif_select_source() again.
973 			 */
974 			if (src_ipif != ire->ire_ipif) {
975 				ipif_refrele(src_ipif);
976 				src_ipif = ipif_select_source(dst_ill,
977 				    gw, zoneid);
978 				if (src_ipif == NULL)
979 					goto icmp_err_ret;
980 			}
981 			dst = gw;
982 			gw = INADDR_ANY;
983 		}
984 		/*
985 		 * dst has been set to the address of the nexthop.
986 		 *
987 		 * TSol note: get security attributes of the nexthop;
988 		 * Note that the nexthop may either be a gateway, or the
989 		 * packet destination itself; Detailed explanation of
990 		 * issues involved is  provided in the  IRE_IF_NORESOLVER
991 		 * logic in ip_newroute().
992 		 */
993 		ga.ga_af = AF_INET;
994 		IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
995 		gcgrp = gcgrp_lookup(&ga, B_FALSE);
996 
997 		if (ire->ire_type == IRE_IF_NORESOLVER)
998 			dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
999 
1000 		save_ire = ire;
1001 		/*
1002 		 * create an incomplete IRE_CACHE.
1003 		 * An areq_mp will be generated in ire_arpresolve() for
1004 		 * RESOLVER interfaces.
1005 		 */
1006 		ire = ire_create(
1007 		    (uchar_t *)&dst,		/* dest address */
1008 		    (uchar_t *)&ip_g_all_ones,	/* mask */
1009 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
1010 		    (uchar_t *)&gw,		/* gateway address */
1011 		    (save_ire->ire_type == IRE_IF_RESOLVER ?  NULL:
1012 		    &save_ire->ire_max_frag),
1013 		    NULL,
1014 		    dst_ill->ill_rq,		/* recv-from queue */
1015 		    dst_ill->ill_wq,		/* send-to queue */
1016 		    IRE_CACHE,			/* IRE type */
1017 		    src_ipif,
1018 		    ire->ire_mask,		/* Parent mask */
1019 		    0,
1020 		    ire->ire_ihandle,	/* Interface handle */
1021 		    0,
1022 		    &(ire->ire_uinfo),
1023 		    NULL,
1024 		    gcgrp,
1025 		    ipst);
1026 		ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
1027 		if (ire != NULL) {
1028 			gcgrp = NULL; /* reference now held by IRE */
1029 			ire->ire_marks |= ire_marks;
1030 			/* add the incomplete ire: */
1031 			error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
1032 			if (error == 0 && ire != NULL) {
1033 				ire->ire_max_frag = save_ire->ire_max_frag;
1034 				ip1dbg(("setting max_frag to %d in ire 0x%p\n",
1035 				    ire->ire_max_frag, (void *)ire));
1036 			} else {
1037 				ire_refrele(save_ire);
1038 				goto icmp_err_ret;
1039 			}
1040 		} else {
1041 			if (gcgrp != NULL) {
1042 				GCGRP_REFRELE(gcgrp);
1043 				gcgrp = NULL;
1044 			}
1045 		}
1046 
1047 		ire_refrele(save_ire);
1048 		break;
1049 	default:
1050 		break;
1051 	}
1052 
1053 out:
1054 	if (sire != NULL)
1055 		ire_refrele(sire);
1056 	if (dst_ill != NULL)
1057 		ill_refrele(dst_ill);
1058 	if (src_ipif != NULL)
1059 		ipif_refrele(src_ipif);
1060 	return (ire);
1061 icmp_err_ret:
1062 	if (src_ipif != NULL)
1063 		ipif_refrele(src_ipif);
1064 	if (dst_ill != NULL)
1065 		ill_refrele(dst_ill);
1066 	if (sire != NULL)
1067 		ire_refrele(sire);
1068 	if (ire != NULL) {
1069 		ire_refrele(ire);
1070 	}
1071 	/* caller needs to send icmp error message */
1072 	return (NULL);
1073 
1074 }
1075 
1076 /*
1077  * Obtain the rt_entry and rt_irb for the route to be added to
1078  * the ips_ip_ftable.
1079  * First attempt to add a node to the radix tree via rn_addroute. If the
1080  * route already exists, return the bucket for the existing route.
1081  *
1082  * Locking notes: Need to hold the global radix tree lock in write mode to
1083  * add a radix node. To prevent the node from being deleted, ire_get_bucket()
1084  * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
1085  * while holding the irb_lock, but not the radix tree lock.
1086  */
1087 irb_t *
1088 ire_get_bucket(ire_t *ire)
1089 {
1090 	struct radix_node *rn;
1091 	struct rt_entry *rt;
1092 	struct rt_sockaddr rmask, rdst;
1093 	irb_t *irb = NULL;
1094 	ip_stack_t *ipst = ire->ire_ipst;
1095 
1096 	ASSERT(ipst->ips_ip_ftable != NULL);
1097 
1098 	/* first try to see if route exists (based on rtalloc1) */
1099 	(void) memset(&rdst, 0, sizeof (rdst));
1100 	rdst.rt_sin_len = sizeof (rdst);
1101 	rdst.rt_sin_family = AF_INET;
1102 	rdst.rt_sin_addr.s_addr = ire->ire_addr;
1103 
1104 	(void) memset(&rmask, 0, sizeof (rmask));
1105 	rmask.rt_sin_len = sizeof (rmask);
1106 	rmask.rt_sin_family = AF_INET;
1107 	rmask.rt_sin_addr.s_addr = ire->ire_mask;
1108 
1109 	/*
1110 	 * add the route. based on BSD's rtrequest1(RTM_ADD)
1111 	 */
1112 	R_Malloc(rt, rt_entry_cache,  sizeof (*rt));
1113 	(void) memset(rt, 0, sizeof (*rt));
1114 	rt->rt_nodes->rn_key = (char *)&rt->rt_dst;
1115 	rt->rt_dst = rdst;
1116 	irb = &rt->rt_irb;
1117 	irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */
1118 	irb->irb_ipst = ipst;
1119 	rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL);
1120 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1121 	rn = ipst->ips_ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask,
1122 	    ipst->ips_ip_ftable, (struct radix_node *)rt);
1123 	if (rn == NULL) {
1124 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1125 		Free(rt, rt_entry_cache);
1126 		rt = NULL;
1127 		irb = NULL;
1128 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
1129 		rn = ipst->ips_ip_ftable->rnh_lookup(&rdst, &rmask,
1130 		    ipst->ips_ip_ftable);
1131 		if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
1132 			/* found a non-root match */
1133 			rt = (struct rt_entry *)rn;
1134 		}
1135 	}
1136 	if (rt != NULL) {
1137 		irb = &rt->rt_irb;
1138 		IRB_REFHOLD(irb);
1139 	}
1140 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1141 	return (irb);
1142 }
1143 
1144 /*
1145  * This function is used when the caller wants to know the outbound
1146  * interface for a packet given only the address.
1147  * If this is a offlink IP address and there are multiple
1148  * routes to this destination, this routine will utilise the
1149  * first route it finds to IP address
1150  * Return values:
1151  * 	0	- FAILURE
1152  *	nonzero	- ifindex
1153  */
1154 uint_t
1155 ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid)
1156 {
1157 	uint_t ifindex = 0;
1158 	ire_t *ire;
1159 	ill_t *ill;
1160 	netstack_t *ns;
1161 	ip_stack_t *ipst;
1162 
1163 	if (zoneid == ALL_ZONES)
1164 		ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
1165 	else
1166 		ns = netstack_find_by_zoneid(zoneid);
1167 	ASSERT(ns != NULL);
1168 
1169 	/*
1170 	 * For exclusive stacks we set the zoneid to zero
1171 	 * since IP uses the global zoneid in the exclusive stacks.
1172 	 */
1173 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
1174 		zoneid = GLOBAL_ZONEID;
1175 	ipst = ns->netstack_ip;
1176 
1177 	ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6);
1178 
1179 	if ((ire =  route_to_dst(ipaddr, zoneid, ipst)) != NULL) {
1180 		ill = ire_to_ill(ire);
1181 		if (ill != NULL)
1182 			ifindex = ill->ill_phyint->phyint_ifindex;
1183 		ire_refrele(ire);
1184 	}
1185 	netstack_rele(ns);
1186 	return (ifindex);
1187 }
1188 
1189 /*
1190  * Routine to find the route to a destination. If a ifindex is supplied
1191  * it tries to match the the route to the corresponding ipif for the ifindex
1192  */
1193 static	ire_t *
1194 route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid, ip_stack_t *ipst)
1195 {
1196 	ire_t *ire = NULL;
1197 	int match_flags;
1198 
1199 	match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1200 	    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
1201 
1202 	/* XXX pass NULL tsl for now */
1203 
1204 	if (dst_addr->sa_family == AF_INET) {
1205 		ire = ire_route_lookup(
1206 		    ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr,
1207 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
1208 	} else {
1209 		ire = ire_route_lookup_v6(
1210 		    &((struct sockaddr_in6 *)dst_addr)->sin6_addr,
1211 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
1212 	}
1213 	return (ire);
1214 }
1215 
1216 /*
1217  * This routine is called by IP Filter to send a packet out on the wire
1218  * to a specified V4 dst (which may be onlink or offlink). The ifindex may or
1219  * may not be 0. A non-null ifindex indicates IP Filter has stipulated
1220  * an outgoing interface and requires the nexthop to be on that interface.
1221  * IP WILL NOT DO the following to the data packet before sending it out:
1222  *	a. manipulate ttl
1223  *	b. ipsec work
1224  *	c. fragmentation
1225  *
1226  * If the packet has been prepared for hardware checksum then it will be
1227  * passed off to ip_send_align_cksum() to check that the flags set on the
1228  * packet are in alignment with the capabilities of the new outgoing NIC.
1229  *
1230  * Return values:
1231  *	0:		IP was able to send of the data pkt
1232  *	ECOMM:		Could not send packet
1233  *	ENONET		No route to dst. It is up to the caller
1234  *			to send icmp unreachable error message,
1235  *	EINPROGRESS	The macaddr of the onlink dst or that
1236  *			of the offlink dst's nexthop needs to get
1237  *			resolved before packet can be sent to dst.
1238  *			Thus transmission is not guaranteed.
1239  *
1240  */
1241 
1242 int
1243 ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex,
1244     zoneid_t zoneid)
1245 {
1246 	ire_t *ire = NULL, *sire = NULL;
1247 	ire_t *ire_cache = NULL;
1248 	boolean_t   check_multirt = B_FALSE;
1249 	int value;
1250 	int match_flags;
1251 	ipaddr_t dst;
1252 	netstack_t *ns;
1253 	ip_stack_t *ipst;
1254 
1255 	ASSERT(mp != NULL);
1256 
1257 	if (zoneid == ALL_ZONES)
1258 		ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
1259 	else
1260 		ns = netstack_find_by_zoneid(zoneid);
1261 	ASSERT(ns != NULL);
1262 
1263 	/*
1264 	 * For exclusive stacks we set the zoneid to zero
1265 	 * since IP uses the global zoneid in the exclusive stacks.
1266 	 */
1267 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
1268 		zoneid = GLOBAL_ZONEID;
1269 	ipst = ns->netstack_ip;
1270 
1271 	ASSERT(dst_addr->sa_family == AF_INET ||
1272 	    dst_addr->sa_family == AF_INET6);
1273 
1274 	if (dst_addr->sa_family == AF_INET) {
1275 		dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr;
1276 	} else {
1277 		/*
1278 		 * We dont have support for V6 yet. It will be provided
1279 		 * once RFE  6399103  has been delivered.
1280 		 * Until then, for V6 dsts, IP Filter will not call
1281 		 * this function. Instead the netinfo framework provides
1282 		 * its own code path, in ip_inject_impl(), to achieve
1283 		 * what it needs to do, for the time being.
1284 		 */
1285 		ip1dbg(("ipfil_sendpkt: no V6 support \n"));
1286 		value = ECOMM;
1287 		freemsg(mp);
1288 		goto discard;
1289 	}
1290 
1291 	/*
1292 	 * Lets get the ire. We might get the ire cache entry,
1293 	 * or the ire,sire pair needed to create the cache entry.
1294 	 * XXX pass NULL tsl for now.
1295 	 */
1296 
1297 	if (ifindex == 0) {
1298 		/* There is no supplied index. So use the FIB info */
1299 
1300 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1301 		    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
1302 		ire = ire_route_lookup(dst,
1303 		    0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp),
1304 		    match_flags, ipst);
1305 	} else {
1306 		ipif_t *supplied_ipif;
1307 		ill_t *ill;
1308 
1309 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1310 		    MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE|
1311 		    MATCH_IRE_SECATTR);
1312 
1313 		/*
1314 		 * If supplied ifindex is non-null, the only valid
1315 		 * nexthop is one off of the interface or group corresponding
1316 		 * to the specified ifindex.
1317 		 */
1318 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
1319 		    NULL, NULL, NULL, NULL, ipst);
1320 		if (ill != NULL) {
1321 			match_flags |= MATCH_IRE_ILL;
1322 		} else {
1323 			/* Fallback to group names if hook_emulation set */
1324 			if (ipst->ips_ipmp_hook_emulation) {
1325 				ill = ill_group_lookup_on_ifindex(ifindex,
1326 				    B_FALSE, ipst);
1327 			}
1328 			if (ill == NULL) {
1329 				ip1dbg(("ipfil_sendpkt: Could not find"
1330 				    " route to dst\n"));
1331 				value = ECOMM;
1332 				freemsg(mp);
1333 				goto discard;
1334 			}
1335 			match_flags |= MATCH_IRE_ILL_GROUP;
1336 		}
1337 		supplied_ipif = ipif_get_next_ipif(NULL, ill);
1338 
1339 		ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif,
1340 		    &sire, zoneid, MBLK_GETLABEL(mp), match_flags, ipst);
1341 		ipif_refrele(supplied_ipif);
1342 		ill_refrele(ill);
1343 	}
1344 
1345 	/*
1346 	 * Verify that the returned IRE is non-null and does
1347 	 * not have either the RTF_REJECT or RTF_BLACKHOLE
1348 	 * flags set and that the IRE is  either an IRE_CACHE,
1349 	 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
1350 	 */
1351 	if (ire == NULL ||
1352 	    ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
1353 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)) {
1354 		/*
1355 		 * Either ire could not be found or we got
1356 		 * an invalid one
1357 		 */
1358 		ip1dbg(("ipfil_sendpkt: Could not find route to dst\n"));
1359 		value = ENONET;
1360 		freemsg(mp);
1361 		goto discard;
1362 	}
1363 
1364 	/* IP Filter and CGTP dont mix. So bail out if CGTP is on */
1365 	if (ipst->ips_ip_cgtp_filter &&
1366 	    ((ire->ire_flags & RTF_MULTIRT) ||
1367 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
1368 		ip1dbg(("ipfil_sendpkt: IPFilter does not work with CGTP\n"));
1369 		value = ECOMM;
1370 		freemsg(mp);
1371 		goto discard;
1372 	}
1373 
1374 	ASSERT(ire->ire_type != IRE_CACHE || ire->ire_nce != NULL);
1375 
1376 	/*
1377 	 * If needed, we will create the ire cache entry for the
1378 	 * nexthop, resolve its link-layer address and then send
1379 	 * the packet out without ttl or IPSec processing.
1380 	 */
1381 	switch (ire->ire_type) {
1382 	case IRE_IF_NORESOLVER:
1383 	case IRE_CACHE:
1384 		if (sire != NULL) {
1385 			UPDATE_OB_PKT_COUNT(sire);
1386 			sire->ire_last_used_time = lbolt;
1387 			ire_refrele(sire);
1388 		}
1389 		ire_cache = ire;
1390 		break;
1391 	case IRE_IF_RESOLVER:
1392 		/*
1393 		 * Call ire_forward(). This function
1394 		 * will, create the ire cache entry of the
1395 		 * the nexthop and adds this incomplete ire
1396 		 * to the ire cache table
1397 		 */
1398 		ire_cache = ire_forward(dst, &check_multirt, ire, sire,
1399 		    MBLK_GETLABEL(mp), ipst);
1400 		if (ire_cache == NULL) {
1401 			ip1dbg(("ipfil_sendpkt: failed to create the"
1402 			    " ire cache entry \n"));
1403 			value = ENONET;
1404 			freemsg(mp);
1405 			sire = NULL;
1406 			ire = NULL;
1407 			goto discard;
1408 		}
1409 		break;
1410 	}
1411 
1412 	if (DB_CKSUMFLAGS(mp)) {
1413 		if (ip_send_align_hcksum_flags(mp, ire_to_ill(ire_cache)))
1414 			goto cleanup;
1415 	}
1416 
1417 	/*
1418 	 * Now that we have the ire cache entry of the nexthop, call
1419 	 * ip_xmit_v4() to trigger mac addr resolution
1420 	 * if necessary and send it once ready.
1421 	 */
1422 
1423 	value = ip_xmit_v4(mp, ire_cache, NULL, B_FALSE);
1424 cleanup:
1425 	ire_refrele(ire_cache);
1426 	/*
1427 	 * At this point, the reference for these have already been
1428 	 * released within ire_forward() and/or ip_xmit_v4(). So we set
1429 	 * them to NULL to make sure we dont drop the references
1430 	 * again in case ip_xmit_v4() returns with either SEND_FAILED
1431 	 * or LLHDR_RESLV_FAILED
1432 	 */
1433 	sire = NULL;
1434 	ire = NULL;
1435 
1436 	switch (value) {
1437 	case SEND_FAILED:
1438 		ip1dbg(("ipfil_sendpkt: Send failed\n"));
1439 		value = ECOMM;
1440 		break;
1441 	case LLHDR_RESLV_FAILED:
1442 		ip1dbg(("ipfil_sendpkt: Link-layer resolution"
1443 		    "  failed\n"));
1444 		value = ECOMM;
1445 		break;
1446 	case LOOKUP_IN_PROGRESS:
1447 		netstack_rele(ns);
1448 		return (EINPROGRESS);
1449 	case SEND_PASSED:
1450 		netstack_rele(ns);
1451 		return (0);
1452 	}
1453 discard:
1454 	if (dst_addr->sa_family == AF_INET) {
1455 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
1456 	} else {
1457 		BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards);
1458 	}
1459 	if (ire != NULL)
1460 		ire_refrele(ire);
1461 	if (sire != NULL)
1462 		ire_refrele(sire);
1463 	netstack_rele(ns);
1464 	return (value);
1465 }
1466 
1467 
1468 /*
1469  * We don't check for dohwcksum in here because it should be being used
1470  * elsewhere to control what flags are being set on the mblk.  That is,
1471  * if DB_CKSUMFLAGS() is non-zero then we assume dohwcksum to be true
1472  * for this packet.
1473  *
1474  * This function assumes that it is *only* being called for TCP or UDP
1475  * packets and nothing else.
1476  */
1477 static int
1478 ip_send_align_hcksum_flags(mblk_t *mp, ill_t *ill)
1479 {
1480 	int illhckflags;
1481 	int mbhckflags;
1482 	uint16_t *up;
1483 	uint32_t cksum;
1484 	ipha_t *ipha;
1485 	ip6_t *ip6;
1486 	int proto;
1487 	int ipversion;
1488 	int length;
1489 	int start;
1490 	ip6_pkt_t ipp;
1491 
1492 	mbhckflags = DB_CKSUMFLAGS(mp);
1493 	ASSERT(mbhckflags != 0);
1494 	ASSERT(mp->b_datap->db_type == M_DATA);
1495 	/*
1496 	 * Since this function only knows how to manage the hardware checksum
1497 	 * issue, reject and packets that have flags set on the aside from
1498 	 * checksum related attributes as we cannot necessarily safely map
1499 	 * that packet onto the new NIC.  Packets that can be potentially
1500 	 * dropped here include those marked for LSO.
1501 	 */
1502 	if ((mbhckflags &
1503 	    ~(HCK_FULLCKSUM|HCK_PARTIALCKSUM|HCK_IPV4_HDRCKSUM)) != 0) {
1504 		DTRACE_PROBE2(pbr__incapable, (mblk_t *), mp, (ill_t *), ill);
1505 		freemsg(mp);
1506 		return (-1);
1507 	}
1508 
1509 	ipha = (ipha_t *)mp->b_rptr;
1510 
1511 	/*
1512 	 * Find out what the new NIC is capable of, if anything, and
1513 	 * only allow it to be used with M_DATA mblks being sent out.
1514 	 */
1515 	if (ILL_HCKSUM_CAPABLE(ill)) {
1516 		illhckflags = ill->ill_hcksum_capab->ill_hcksum_txflags;
1517 	} else {
1518 		/*
1519 		 * No capabilities, so turn off everything.
1520 		 */
1521 		illhckflags = 0;
1522 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0, 0, 0);
1523 		mp->b_datap->db_struioflag &= ~STRUIO_IP;
1524 	}
1525 
1526 	DTRACE_PROBE4(pbr__info__a, (mblk_t *), mp, (ill_t *), ill,
1527 	    uint32_t, illhckflags, uint32_t, mbhckflags);
1528 	/*
1529 	 * This block of code that looks for the position of the TCP/UDP
1530 	 * checksum is early in this function because we need to know
1531 	 * what needs to be blanked out for the hardware checksum case.
1532 	 *
1533 	 * That we're in this function implies that the packet is either
1534 	 * TCP or UDP on Solaris, so checks are made for one protocol and
1535 	 * if that fails, the other is therefore implied.
1536 	 */
1537 	ipversion = IPH_HDR_VERSION(ipha);
1538 
1539 	if (ipversion == IPV4_VERSION) {
1540 		proto = ipha->ipha_protocol;
1541 		if (proto == IPPROTO_TCP) {
1542 			up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1543 		} else {
1544 			up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1545 		}
1546 	} else {
1547 		uint8_t lasthdr;
1548 
1549 		/*
1550 		 * Nothing I've seen indicates that IPv6 checksum'ing
1551 		 * precludes the presence of extension headers, so we
1552 		 * can't just look at the next header value in the IPv6
1553 		 * packet header to see if it is TCP/UDP.
1554 		 */
1555 		ip6 = (ip6_t *)ipha;
1556 		(void) memset(&ipp, 0, sizeof (ipp));
1557 		start = ip_find_hdr_v6(mp, ip6, &ipp, &lasthdr);
1558 		proto = lasthdr;
1559 
1560 		if (proto == IPPROTO_TCP) {
1561 			up = IPH_TCPH_CHECKSUMP(ipha, start);
1562 		} else {
1563 			up = IPH_UDPH_CHECKSUMP(ipha, start);
1564 		}
1565 	}
1566 
1567 	/*
1568 	 * The first case here is easiest:
1569 	 * mblk hasn't asked for full checksum, but the card supports it.
1570 	 *
1571 	 * In addition, check for IPv4 header capability.  Note that only
1572 	 * the mblk flag is checked and not ipversion.
1573 	 */
1574 	if ((((illhckflags & HCKSUM_INET_FULL_V4) && (ipversion == 4)) ||
1575 	    (((illhckflags & HCKSUM_INET_FULL_V6) && (ipversion == 6)))) &&
1576 	    ((mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) != 0)) {
1577 		int newflags = HCK_FULLCKSUM;
1578 
1579 		if ((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) {
1580 			if ((illhckflags & HCKSUM_IPHDRCKSUM) != 0) {
1581 				newflags |= HCK_IPV4_HDRCKSUM;
1582 			} else {
1583 				/*
1584 				 * Rather than call a function, just inline
1585 				 * the computation of the basic IPv4 header.
1586 				 */
1587 				cksum = (ipha->ipha_dst >> 16) +
1588 				    (ipha->ipha_dst & 0xFFFF) +
1589 				    (ipha->ipha_src >> 16) +
1590 				    (ipha->ipha_src & 0xFFFF);
1591 				IP_HDR_CKSUM(ipha, cksum,
1592 				    ((uint32_t *)ipha)[0],
1593 				    ((uint16_t *)ipha)[4]);
1594 			}
1595 		}
1596 
1597 		*up = 0;
1598 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0,
1599 		    newflags, 0);
1600 		return (0);
1601 	}
1602 
1603 	DTRACE_PROBE2(pbr__info__b, int, ipversion, int, proto);
1604 
1605 	/*
1606 	 * Start calculating the pseudo checksum over the IP packet header.
1607 	 * Although the final pseudo checksum used by TCP/UDP consists of
1608 	 * more than just the address fields, we can use the result of
1609 	 * adding those together a little bit further down for IPv4.
1610 	 */
1611 	if (ipversion == IPV4_VERSION) {
1612 		cksum = (ipha->ipha_dst >> 16) + (ipha->ipha_dst & 0xFFFF) +
1613 		    (ipha->ipha_src >> 16) + (ipha->ipha_src & 0xFFFF);
1614 		start = IP_SIMPLE_HDR_LENGTH;
1615 		length = ntohs(ipha->ipha_length);
1616 		DTRACE_PROBE3(pbr__info__e, uint32_t, ipha->ipha_src,
1617 		    uint32_t, ipha->ipha_dst, int, cksum);
1618 	} else {
1619 		uint16_t *pseudo;
1620 
1621 		pseudo = (uint16_t *)&ip6->ip6_src;
1622 
1623 		/* calculate pseudo-header checksum */
1624 		cksum = pseudo[0] + pseudo[1] + pseudo[2] + pseudo[3] +
1625 		    pseudo[4] + pseudo[5] + pseudo[6] + pseudo[7] +
1626 		    pseudo[8] + pseudo[9] + pseudo[10] + pseudo[11] +
1627 		    pseudo[12] + pseudo[13] + pseudo[14] + pseudo[15];
1628 
1629 		length = ntohs(ip6->ip6_plen) + sizeof (ip6_t);
1630 	}
1631 
1632 	/* Fold the initial sum */
1633 	cksum = (cksum & 0xffff) + (cksum >> 16);
1634 
1635 	/*
1636 	 * If the packet was asking for an IPv4 header checksum to be
1637 	 * calculated but the interface doesn't support that, fill it in
1638 	 * using our pseudo checksum as a starting point.
1639 	 */
1640 	if (((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) &&
1641 	    ((illhckflags & HCKSUM_IPHDRCKSUM) == 0)) {
1642 		/*
1643 		 * IP_HDR_CKSUM uses the 2rd arg to the macro in a destructive
1644 		 * way so pass in a copy of the checksum calculated thus far.
1645 		 */
1646 		uint32_t ipsum = cksum;
1647 
1648 		DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM;
1649 
1650 		IP_HDR_CKSUM(ipha, ipsum, ((uint32_t *)ipha)[0],
1651 		    ((uint16_t *)ipha)[4]);
1652 	}
1653 
1654 	DTRACE_PROBE3(pbr__info__c, int, start, int, length, int, cksum);
1655 
1656 	if (proto == IPPROTO_TCP) {
1657 		cksum += IP_TCP_CSUM_COMP;
1658 	} else {
1659 		cksum += IP_UDP_CSUM_COMP;
1660 	}
1661 	cksum += htons(length - start);
1662 	cksum = (cksum & 0xffff) + (cksum >> 16);
1663 
1664 	/*
1665 	 * For TCP/UDP, we either want to setup the packet for partial
1666 	 * checksum or we want to do it all ourselves because the NIC
1667 	 * offers no support for either partial or full checksum.
1668 	 */
1669 	if ((illhckflags & HCKSUM_INET_PARTIAL) != 0) {
1670 		/*
1671 		 * The only case we care about here is if the mblk was
1672 		 * previously set for full checksum offload.  If it was
1673 		 * marked for partial (and the NIC does partial), then
1674 		 * we have nothing to do.  Similarly if the packet was
1675 		 * not set for partial or full, we do nothing as this
1676 		 * is cheaper than more work to set something up.
1677 		 */
1678 		if ((mbhckflags & HCK_FULLCKSUM) != 0) {
1679 			uint32_t offset;
1680 
1681 			if (proto == IPPROTO_TCP) {
1682 				offset = TCP_CHECKSUM_OFFSET;
1683 			} else {
1684 				offset = UDP_CHECKSUM_OFFSET;
1685 			}
1686 			*up = cksum;
1687 
1688 			DTRACE_PROBE3(pbr__info__f, int, length - start, int,
1689 			    cksum, int, offset);
1690 
1691 			(void) hcksum_assoc(mp, NULL, NULL, start,
1692 			    start + offset, length, 0,
1693 			    DB_CKSUMFLAGS(mp) | HCK_PARTIALCKSUM, 0);
1694 		}
1695 
1696 	} else if (mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) {
1697 		DB_CKSUMFLAGS(mp) &= ~(HCK_PARTIALCKSUM|HCK_FULLCKSUM);
1698 
1699 		*up = 0;
1700 		*up = IP_CSUM(mp, start, cksum);
1701 	}
1702 
1703 	DTRACE_PROBE4(pbr__info__d, (mblk_t *), mp, (ipha_t *), ipha,
1704 	    (uint16_t *), up, int, cksum);
1705 	return (0);
1706 }
1707 
1708 
1709 /* ire_walk routine invoked for ip_ire_report for each IRE. */
1710 void
1711 ire_report_ftable(ire_t *ire, char *m)
1712 {
1713 	char	buf1[16];
1714 	char	buf2[16];
1715 	char	buf3[16];
1716 	char	buf4[16];
1717 	uint_t	fo_pkt_count;
1718 	uint_t	ib_pkt_count;
1719 	int	ref;
1720 	uint_t	print_len, buf_len;
1721 	mblk_t 	*mp = (mblk_t *)m;
1722 
1723 	if (ire->ire_type & IRE_CACHETABLE)
1724 		return;
1725 	buf_len = mp->b_datap->db_lim - mp->b_wptr;
1726 	if (buf_len <= 0)
1727 		return;
1728 
1729 	/* Number of active references of this ire */
1730 	ref = ire->ire_refcnt;
1731 	/* "inbound" to a non local address is a forward */
1732 	ib_pkt_count = ire->ire_ib_pkt_count;
1733 	fo_pkt_count = 0;
1734 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
1735 		fo_pkt_count = ib_pkt_count;
1736 		ib_pkt_count = 0;
1737 	}
1738 	print_len = snprintf((char *)mp->b_wptr, buf_len,
1739 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
1740 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
1741 	    "%04d %08d %08d %d/%d/%d %s\n",
1742 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
1743 	    (int)ire->ire_zoneid,
1744 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
1745 	    ip_dot_addr(ire->ire_src_addr, buf3),
1746 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
1747 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
1748 	    ire->ire_uinfo.iulp_rtt_sd,
1749 	    ire->ire_uinfo.iulp_ssthresh, ref,
1750 	    ire->ire_uinfo.iulp_rtomax,
1751 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
1752 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
1753 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
1754 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
1755 	    ire->ire_uinfo.iulp_sack,
1756 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
1757 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
1758 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
1759 	if (print_len < buf_len) {
1760 		mp->b_wptr += print_len;
1761 	} else {
1762 		mp->b_wptr += buf_len;
1763 	}
1764 }
1765 
1766 /*
1767  * callback function provided by ire_ftable_lookup when calling
1768  * rn_match_args(). Invoke ire_match_args on each matching leaf node in
1769  * the radix tree.
1770  */
1771 boolean_t
1772 ire_find_best_route(struct radix_node *rn, void *arg)
1773 {
1774 	struct rt_entry *rt = (struct rt_entry *)rn;
1775 	irb_t *irb_ptr;
1776 	ire_t *ire;
1777 	ire_ftable_args_t *margs = arg;
1778 	ipaddr_t match_mask;
1779 
1780 	ASSERT(rt != NULL);
1781 
1782 	irb_ptr = &rt->rt_irb;
1783 
1784 	if (irb_ptr->irb_ire_cnt == 0)
1785 		return (B_FALSE);
1786 
1787 	rw_enter(&irb_ptr->irb_lock, RW_READER);
1788 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
1789 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
1790 			continue;
1791 		if (margs->ift_flags & MATCH_IRE_MASK)
1792 			match_mask = margs->ift_mask;
1793 		else
1794 			match_mask = ire->ire_mask;
1795 
1796 		if (ire_match_args(ire, margs->ift_addr, match_mask,
1797 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
1798 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
1799 		    margs->ift_flags)) {
1800 			IRE_REFHOLD(ire);
1801 			rw_exit(&irb_ptr->irb_lock);
1802 			margs->ift_best_ire = ire;
1803 			return (B_TRUE);
1804 		}
1805 	}
1806 	rw_exit(&irb_ptr->irb_lock);
1807 	return (B_FALSE);
1808 }
1809 
1810 /*
1811  * ftable irb_t structures are dynamically allocated, and we need to
1812  * check if the irb_t (and associated ftable tree attachment) needs to
1813  * be cleaned up when the irb_refcnt goes to 0. The conditions that need
1814  * be verified are:
1815  * - no other walkers of the irebucket, i.e., quiescent irb_refcnt,
1816  * - no other threads holding references to ire's in the bucket,
1817  *   i.e., irb_nire == 0
1818  * - no active ire's in the bucket, i.e., irb_ire_cnt == 0
1819  * - need to hold the global tree lock and irb_lock in write mode.
1820  */
1821 void
1822 irb_refrele_ftable(irb_t *irb)
1823 {
1824 	for (;;) {
1825 		rw_enter(&irb->irb_lock, RW_WRITER);
1826 		ASSERT(irb->irb_refcnt != 0);
1827 		if (irb->irb_refcnt != 1) {
1828 			/*
1829 			 * Someone has a reference to this radix node
1830 			 * or there is some bucket walker.
1831 			 */
1832 			irb->irb_refcnt--;
1833 			rw_exit(&irb->irb_lock);
1834 			return;
1835 		} else {
1836 			/*
1837 			 * There is no other walker, nor is there any
1838 			 * other thread that holds a direct ref to this
1839 			 * radix node. Do the clean up if needed. Call
1840 			 * to ire_unlink will clear the IRB_MARK_CONDEMNED flag
1841 			 */
1842 			if (irb->irb_marks & IRB_MARK_CONDEMNED)  {
1843 				ire_t *ire_list;
1844 
1845 				ire_list = ire_unlink(irb);
1846 				rw_exit(&irb->irb_lock);
1847 
1848 				if (ire_list != NULL)
1849 					ire_cleanup(ire_list);
1850 				/*
1851 				 * more CONDEMNED entries could have
1852 				 * been added while we dropped the lock,
1853 				 * so we have to re-check.
1854 				 */
1855 				continue;
1856 			}
1857 
1858 			/*
1859 			 * Now check if there are still any ires
1860 			 * associated with this radix node.
1861 			 */
1862 			if (irb->irb_nire != 0) {
1863 				/*
1864 				 * someone is still holding on
1865 				 * to ires in this bucket
1866 				 */
1867 				irb->irb_refcnt--;
1868 				rw_exit(&irb->irb_lock);
1869 				return;
1870 			} else {
1871 				/*
1872 				 * Everything is clear. Zero walkers,
1873 				 * Zero threads with a ref to this
1874 				 * radix node, Zero ires associated with
1875 				 * this radix node. Due to lock order,
1876 				 * check the above conditions again
1877 				 * after grabbing all locks in the right order
1878 				 */
1879 				rw_exit(&irb->irb_lock);
1880 				if (irb_inactive(irb))
1881 					return;
1882 				/*
1883 				 * irb_inactive could not free the irb.
1884 				 * See if there are any walkers, if not
1885 				 * try to clean up again.
1886 				 */
1887 			}
1888 		}
1889 	}
1890 }
1891 
1892 /*
1893  * IRE iterator used by ire_ftable_lookup() to process multiple default
1894  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
1895  * in the bucket skipping default interface routes and deleted entries.
1896  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
1897  * Assumes that the caller holds a reference on the IRE bucket.
1898  *
1899  * In the absence of good IRE_DEFAULT routes, this function will return
1900  * the first IRE_INTERFACE route found (if any).
1901  */
1902 ire_t *
1903 ire_round_robin(irb_t *irb_ptr, zoneid_t zoneid, ire_ftable_args_t *margs,
1904 	ip_stack_t *ipst)
1905 {
1906 	ire_t	*ire_origin;
1907 	ire_t	*ire, *maybe_ire = NULL;
1908 
1909 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
1910 	ire_origin = irb_ptr->irb_rr_origin;
1911 	if (ire_origin != NULL) {
1912 		ire_origin = ire_origin->ire_next;
1913 		IRE_FIND_NEXT_ORIGIN(ire_origin);
1914 	}
1915 
1916 	if (ire_origin == NULL) {
1917 		/*
1918 		 * first time through routine, or we dropped off the end
1919 		 * of list.
1920 		 */
1921 		ire_origin = irb_ptr->irb_ire;
1922 		IRE_FIND_NEXT_ORIGIN(ire_origin);
1923 	}
1924 	irb_ptr->irb_rr_origin = ire_origin;
1925 	IRB_REFHOLD_LOCKED(irb_ptr);
1926 	rw_exit(&irb_ptr->irb_lock);
1927 
1928 	DTRACE_PROBE2(ire__rr__origin, (irb_t *), irb_ptr,
1929 	    (ire_t *), ire_origin);
1930 
1931 	/*
1932 	 * Round-robin the routers list looking for a route that
1933 	 * matches the passed in parameters.
1934 	 * We start with the ire we found above and we walk the hash
1935 	 * list until we're back where we started. It doesn't matter if
1936 	 * routes are added or deleted by other threads - we know this
1937 	 * ire will stay in the list because we hold a reference on the
1938 	 * ire bucket.
1939 	 */
1940 	ire = ire_origin;
1941 	while (ire != NULL) {
1942 		int match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
1943 		ire_t *rire;
1944 
1945 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
1946 			goto next_ire;
1947 
1948 		if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0,
1949 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
1950 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
1951 		    margs->ift_flags))
1952 			goto next_ire;
1953 
1954 		if (ire->ire_type & IRE_INTERFACE) {
1955 			/*
1956 			 * keep looking to see if there is a non-interface
1957 			 * default ire, but save this one as a last resort.
1958 			 */
1959 			if (maybe_ire == NULL)
1960 				maybe_ire = ire;
1961 			goto next_ire;
1962 		}
1963 
1964 		if (zoneid == ALL_ZONES) {
1965 			IRE_REFHOLD(ire);
1966 			IRB_REFRELE(irb_ptr);
1967 			return (ire);
1968 		}
1969 		/*
1970 		 * When we're in a non-global zone, we're only
1971 		 * interested in routers that are
1972 		 * reachable through ipifs within our zone.
1973 		 */
1974 		if (ire->ire_ipif != NULL) {
1975 			match_flags |= MATCH_IRE_ILL_GROUP;
1976 		}
1977 		rire = ire_route_lookup(ire->ire_gateway_addr, 0, 0,
1978 		    IRE_INTERFACE, ire->ire_ipif, NULL, zoneid, margs->ift_tsl,
1979 		    match_flags, ipst);
1980 		if (rire != NULL) {
1981 			ire_refrele(rire);
1982 			IRE_REFHOLD(ire);
1983 			IRB_REFRELE(irb_ptr);
1984 			return (ire);
1985 		}
1986 next_ire:
1987 		ire = (ire->ire_next ?  ire->ire_next : irb_ptr->irb_ire);
1988 		if (ire == ire_origin)
1989 			break;
1990 	}
1991 	if (maybe_ire != NULL)
1992 		IRE_REFHOLD(maybe_ire);
1993 	IRB_REFRELE(irb_ptr);
1994 	return (maybe_ire);
1995 }
1996 
1997 void
1998 irb_refhold_rn(struct radix_node *rn)
1999 {
2000 	if ((rn->rn_flags & RNF_ROOT) == 0)
2001 		IRB_REFHOLD(&((rt_t *)(rn))->rt_irb);
2002 }
2003 
2004 void
2005 irb_refrele_rn(struct radix_node *rn)
2006 {
2007 	if ((rn->rn_flags & RNF_ROOT) == 0)
2008 		irb_refrele_ftable(&((rt_t *)(rn))->rt_irb);
2009 }
2010