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