xref: /onnv-gate/usr/src/uts/common/inet/ip/ip_ftable.c (revision 4482:a8d24a20d182)
12535Ssangeeta /*
22535Ssangeeta  * CDDL HEADER START
32535Ssangeeta  *
42535Ssangeeta  * The contents of this file are subject to the terms of the
52535Ssangeeta  * Common Development and Distribution License (the "License").
62535Ssangeeta  * You may not use this file except in compliance with the License.
72535Ssangeeta  *
82535Ssangeeta  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92535Ssangeeta  * or http://www.opensolaris.org/os/licensing.
102535Ssangeeta  * See the License for the specific language governing permissions
112535Ssangeeta  * and limitations under the License.
122535Ssangeeta  *
132535Ssangeeta  * When distributing Covered Code, include this CDDL HEADER in each
142535Ssangeeta  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152535Ssangeeta  * If applicable, add the following below this CDDL HEADER, with the
162535Ssangeeta  * fields enclosed by brackets "[]" replaced with your own identifying
172535Ssangeeta  * information: Portions Copyright [yyyy] [name of copyright owner]
182535Ssangeeta  *
192535Ssangeeta  * CDDL HEADER END
202535Ssangeeta  */
212535Ssangeeta /*
223448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
232535Ssangeeta  * Use is subject to license terms.
242535Ssangeeta  */
252535Ssangeeta 
262535Ssangeeta #pragma ident	"%Z%%M%	%I%	%E% SMI"
272535Ssangeeta 
282535Ssangeeta /*
292535Ssangeeta  * This file contains consumer routines of the IPv4 forwarding engine
302535Ssangeeta  */
312535Ssangeeta 
322535Ssangeeta #include <sys/types.h>
332535Ssangeeta #include <sys/stream.h>
342535Ssangeeta #include <sys/stropts.h>
352535Ssangeeta #include <sys/strlog.h>
362535Ssangeeta #include <sys/dlpi.h>
372535Ssangeeta #include <sys/ddi.h>
382535Ssangeeta #include <sys/cmn_err.h>
392535Ssangeeta #include <sys/policy.h>
402535Ssangeeta 
412535Ssangeeta #include <sys/systm.h>
422535Ssangeeta #include <sys/strsun.h>
432535Ssangeeta #include <sys/kmem.h>
442535Ssangeeta #include <sys/param.h>
452535Ssangeeta #include <sys/socket.h>
46*4482Sdr146992 #include <sys/strsubr.h>
47*4482Sdr146992 #include <sys/pattr.h>
482535Ssangeeta #include <net/if.h>
492535Ssangeeta #include <net/route.h>
502535Ssangeeta #include <netinet/in.h>
512535Ssangeeta #include <net/if_dl.h>
522535Ssangeeta #include <netinet/ip6.h>
532535Ssangeeta #include <netinet/icmp6.h>
542535Ssangeeta 
552535Ssangeeta #include <inet/common.h>
562535Ssangeeta #include <inet/mi.h>
572535Ssangeeta #include <inet/mib2.h>
582535Ssangeeta #include <inet/ip.h>
59*4482Sdr146992 #include <inet/ip_impl.h>
602535Ssangeeta #include <inet/ip6.h>
612535Ssangeeta #include <inet/ip_ndp.h>
622535Ssangeeta #include <inet/arp.h>
632535Ssangeeta #include <inet/ip_if.h>
642535Ssangeeta #include <inet/ip_ire.h>
652535Ssangeeta #include <inet/ip_ftable.h>
662535Ssangeeta #include <inet/ip_rts.h>
672535Ssangeeta #include <inet/nd.h>
682535Ssangeeta 
692535Ssangeeta #include <net/pfkeyv2.h>
702535Ssangeeta #include <inet/ipsec_info.h>
712535Ssangeeta #include <inet/sadb.h>
722535Ssangeeta #include <sys/kmem.h>
732535Ssangeeta #include <inet/tcp.h>
742535Ssangeeta #include <inet/ipclassifier.h>
752535Ssangeeta #include <sys/zone.h>
762535Ssangeeta #include <net/radix.h>
772535Ssangeeta #include <sys/tsol/label.h>
782535Ssangeeta #include <sys/tsol/tnet.h>
792535Ssangeeta 
802535Ssangeeta #define	IS_DEFAULT_ROUTE(ire)	\
812535Ssangeeta 	(((ire)->ire_type & IRE_DEFAULT) || \
822535Ssangeeta 	    (((ire)->ire_type & IRE_INTERFACE) && ((ire)->ire_addr == 0)))
832535Ssangeeta 
842535Ssangeeta /*
852535Ssangeeta  * structure for passing args between ire_ftable_lookup and ire_find_best_route
862535Ssangeeta  */
872535Ssangeeta typedef struct ire_ftable_args_s {
882535Ssangeeta 	ipaddr_t	ift_addr;
892535Ssangeeta 	ipaddr_t	ift_mask;
902535Ssangeeta 	ipaddr_t	ift_gateway;
912535Ssangeeta 	int		ift_type;
922535Ssangeeta 	const ipif_t		*ift_ipif;
932535Ssangeeta 	zoneid_t	ift_zoneid;
942535Ssangeeta 	uint32_t	ift_ihandle;
952535Ssangeeta 	const ts_label_t	*ift_tsl;
962535Ssangeeta 	int		ift_flags;
972535Ssangeeta 	ire_t		*ift_best_ire;
982535Ssangeeta } ire_ftable_args_t;
992535Ssangeeta 
1003448Sdh155122 static ire_t	*route_to_dst(const struct sockaddr *, zoneid_t, ip_stack_t *);
1013448Sdh155122 static ire_t   	*ire_round_robin(irb_t *, zoneid_t, ire_ftable_args_t *,
1023448Sdh155122     ip_stack_t *);
1032535Ssangeeta static void		ire_del_host_redir(ire_t *, char *);
1042535Ssangeeta static boolean_t	ire_find_best_route(struct radix_node *, void *);
105*4482Sdr146992 static int	ip_send_align_hcksum_flags(mblk_t *, ill_t *);
1062535Ssangeeta 
1072535Ssangeeta /*
1082535Ssangeeta  * Lookup a route in forwarding table. A specific lookup is indicated by
1092535Ssangeeta  * passing the required parameters and indicating the match required in the
1102535Ssangeeta  * flag field.
1112535Ssangeeta  *
1122535Ssangeeta  * Looking for default route can be done in three ways
1132535Ssangeeta  * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
1142535Ssangeeta  *    along with other matches.
1152535Ssangeeta  * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
1162535Ssangeeta  *    field along with other matches.
1172535Ssangeeta  * 3) if the destination and mask are passed as zeros.
1182535Ssangeeta  *
1192535Ssangeeta  * A request to return a default route if no route
1202535Ssangeeta  * is found, can be specified by setting MATCH_IRE_DEFAULT
1212535Ssangeeta  * in flags.
1222535Ssangeeta  *
1232535Ssangeeta  * It does not support recursion more than one level. It
1242535Ssangeeta  * will do recursive lookup only when the lookup maps to
1252535Ssangeeta  * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
1262535Ssangeeta  *
1272535Ssangeeta  * If the routing table is setup to allow more than one level
1282535Ssangeeta  * of recursion, the cleaning up cache table will not work resulting
1292535Ssangeeta  * in invalid routing.
1302535Ssangeeta  *
1312535Ssangeeta  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
1322535Ssangeeta  *
1332535Ssangeeta  * NOTE : When this function returns NULL, pire has already been released.
1342535Ssangeeta  *	  pire is valid only when this function successfully returns an
1352535Ssangeeta  *	  ire.
1362535Ssangeeta  */
1372535Ssangeeta ire_t *
1382535Ssangeeta ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1392535Ssangeeta     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
1403448Sdh155122     uint32_t ihandle, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
1412535Ssangeeta {
1422535Ssangeeta 	ire_t *ire = NULL;
1432535Ssangeeta 	ipaddr_t gw_addr;
1442535Ssangeeta 	struct rt_sockaddr rdst, rmask;
1452535Ssangeeta 	struct rt_entry *rt;
1462535Ssangeeta 	ire_ftable_args_t margs;
1472535Ssangeeta 	boolean_t found_incomplete = B_FALSE;
1482535Ssangeeta 
1492535Ssangeeta 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
1502535Ssangeeta 	ASSERT(!(flags & MATCH_IRE_WQ));
1512535Ssangeeta 
1522535Ssangeeta 	/*
1532535Ssangeeta 	 * When we return NULL from this function, we should make
1542535Ssangeeta 	 * sure that *pire is NULL so that the callers will not
1552535Ssangeeta 	 * wrongly REFRELE the pire.
1562535Ssangeeta 	 */
1572535Ssangeeta 	if (pire != NULL)
1582535Ssangeeta 		*pire = NULL;
1592535Ssangeeta 	/*
1602535Ssangeeta 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
1612535Ssangeeta 	 * MATCH_IRE_ILL is set.
1622535Ssangeeta 	 */
1632535Ssangeeta 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
1642535Ssangeeta 	    (ipif == NULL))
1652535Ssangeeta 		return (NULL);
1662535Ssangeeta 
1672535Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
1682535Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
1692535Ssangeeta 	rdst.rt_sin_family = AF_INET;
1702535Ssangeeta 	rdst.rt_sin_addr.s_addr = addr;
1712535Ssangeeta 
1722535Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
1732535Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
1742535Ssangeeta 	rmask.rt_sin_family = AF_INET;
1752535Ssangeeta 	rmask.rt_sin_addr.s_addr = mask;
1762535Ssangeeta 
1772535Ssangeeta 	(void) memset(&margs, 0, sizeof (margs));
1782535Ssangeeta 	margs.ift_addr = addr;
1792535Ssangeeta 	margs.ift_mask = mask;
1802535Ssangeeta 	margs.ift_gateway = gateway;
1812535Ssangeeta 	margs.ift_type = type;
1822535Ssangeeta 	margs.ift_ipif = ipif;
1832535Ssangeeta 	margs.ift_zoneid = zoneid;
1842535Ssangeeta 	margs.ift_ihandle = ihandle;
1852535Ssangeeta 	margs.ift_tsl = tsl;
1862535Ssangeeta 	margs.ift_flags = flags;
1872535Ssangeeta 
1882535Ssangeeta 	/*
1892535Ssangeeta 	 * The flags argument passed to ire_ftable_lookup may cause the
1902535Ssangeeta 	 * search to return, not the longest matching prefix, but the
1912535Ssangeeta 	 * "best matching prefix", i.e., the longest prefix that also
1922535Ssangeeta 	 * satisfies constraints imposed via the permutation of flags
1932535Ssangeeta 	 * passed in. To achieve this, we invoke ire_match_args() on
1942535Ssangeeta 	 * each matching leaf in the  radix tree. ire_match_args is
1952535Ssangeeta 	 * invoked by the callback function ire_find_best_route()
1962535Ssangeeta 	 * We hold the global tree lock in read mode when calling
1972535Ssangeeta 	 * rn_match_args.Before dropping the global tree lock, ensure
1982535Ssangeeta 	 * that the radix node can't be deleted by incrementing ire_refcnt.
1992535Ssangeeta 	 */
2003448Sdh155122 	RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2013448Sdh155122 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2023448Sdh155122 	    ipst->ips_ip_ftable, ire_find_best_route, &margs);
2032535Ssangeeta 	ire = margs.ift_best_ire;
2043448Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2052535Ssangeeta 
2062535Ssangeeta 	if (rt == NULL) {
2072535Ssangeeta 		return (NULL);
2082535Ssangeeta 	} else {
2092535Ssangeeta 		ASSERT(ire != NULL);
2102535Ssangeeta 	}
2112535Ssangeeta 
2122535Ssangeeta 	DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire);
2132535Ssangeeta 
2142535Ssangeeta 	if (!IS_DEFAULT_ROUTE(ire))
2152535Ssangeeta 		goto found_ire_held;
2162535Ssangeeta 	/*
2172535Ssangeeta 	 * If default route is found, see if default matching criteria
2182535Ssangeeta 	 * are satisfied.
2192535Ssangeeta 	 */
2202535Ssangeeta 	if (flags & MATCH_IRE_MASK) {
2212535Ssangeeta 		/*
2222535Ssangeeta 		 * we were asked to match a 0 mask, and came back with
2232535Ssangeeta 		 * a default route. Ok to return it.
2242535Ssangeeta 		 */
2252535Ssangeeta 		goto found_default_ire;
2262535Ssangeeta 	}
2272535Ssangeeta 	if ((flags & MATCH_IRE_TYPE) &&
2282535Ssangeeta 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
2292535Ssangeeta 		/*
2302535Ssangeeta 		 * we were asked to match a default ire type. Ok to return it.
2312535Ssangeeta 		 */
2322535Ssangeeta 		goto found_default_ire;
2332535Ssangeeta 	}
2342535Ssangeeta 	if (flags & MATCH_IRE_DEFAULT) {
2352535Ssangeeta 		goto found_default_ire;
2362535Ssangeeta 	}
2372535Ssangeeta 	/*
2382535Ssangeeta 	 * we found a default route, but default matching criteria
2392535Ssangeeta 	 * are not specified and we are not explicitly looking for
2402535Ssangeeta 	 * default.
2412535Ssangeeta 	 */
2422535Ssangeeta 	IRE_REFRELE(ire);
2432535Ssangeeta 	return (NULL);
2442535Ssangeeta found_default_ire:
2452535Ssangeeta 	/*
2462535Ssangeeta 	 * round-robin only if we have more than one route in the bucket.
2472535Ssangeeta 	 */
2482535Ssangeeta 	if ((ire->ire_bucket->irb_ire_cnt > 1) &&
2492535Ssangeeta 	    IS_DEFAULT_ROUTE(ire) &&
2502535Ssangeeta 	    ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
2512535Ssangeeta 	    MATCH_IRE_DEFAULT)) {
2522535Ssangeeta 		ire_t *next_ire;
2532535Ssangeeta 
2543448Sdh155122 		next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs,
2553448Sdh155122 		    ipst);
2562535Ssangeeta 		IRE_REFRELE(ire);
2572535Ssangeeta 		if (next_ire != NULL) {
2582535Ssangeeta 			ire = next_ire;
2592535Ssangeeta 		} else {
2602535Ssangeeta 			/* no route */
2612535Ssangeeta 			return (NULL);
2622535Ssangeeta 		}
2632535Ssangeeta 	}
2642535Ssangeeta found_ire_held:
2652535Ssangeeta 	ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
2662535Ssangeeta 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
2672535Ssangeeta 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
2682535Ssangeeta 		return (ire);
2692535Ssangeeta 	}
2702535Ssangeeta 	/*
2712535Ssangeeta 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
2722535Ssangeeta 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
2732535Ssangeeta 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
2742535Ssangeeta 	 * IRE (one of the prefix types), then it is necessary to fill in the
2752535Ssangeeta 	 * parent IRE pointed to by pire, and then lookup the gateway address of
2762535Ssangeeta 	 * the parent.  For backwards compatiblity, if this lookup returns an
2772535Ssangeeta 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
2782535Ssangeeta 	 * of lookup is done.
2792535Ssangeeta 	 */
2802535Ssangeeta 	if (flags & MATCH_IRE_RECURSIVE) {
2812535Ssangeeta 		ipif_t	*gw_ipif;
2822535Ssangeeta 		int match_flags = MATCH_IRE_DSTONLY;
2832535Ssangeeta 		ire_t *save_ire;
2842535Ssangeeta 
2852535Ssangeeta 		if (ire->ire_type & IRE_INTERFACE)
2862535Ssangeeta 			return (ire);
2872535Ssangeeta 		if (pire != NULL)
2882535Ssangeeta 			*pire = ire;
2892535Ssangeeta 		/*
2902535Ssangeeta 		 * If we can't find an IRE_INTERFACE or the caller has not
2912535Ssangeeta 		 * asked for pire, we need to REFRELE the save_ire.
2922535Ssangeeta 		 */
2932535Ssangeeta 		save_ire = ire;
2942535Ssangeeta 
2952535Ssangeeta 		/*
2962535Ssangeeta 		 * Currently MATCH_IRE_ILL is never used with
2972535Ssangeeta 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
2982535Ssangeeta 		 * sending out packets as MATCH_IRE_ILL is used only
2992535Ssangeeta 		 * for communicating with on-link hosts. We can't assert
3002535Ssangeeta 		 * that here as RTM_GET calls this function with
3012535Ssangeeta 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
3022535Ssangeeta 		 * We have already used the MATCH_IRE_ILL in determining
3032535Ssangeeta 		 * the right prefix route at this point. To match the
3042535Ssangeeta 		 * behavior of how we locate routes while sending out
3052535Ssangeeta 		 * packets, we don't want to use MATCH_IRE_ILL below
3062535Ssangeeta 		 * while locating the interface route.
3072535Ssangeeta 		 *
3082535Ssangeeta 		 * ire_ftable_lookup may end up with an incomplete IRE_CACHE
3092535Ssangeeta 		 * entry for the gateway (i.e., one for which the
3102535Ssangeeta 		 * ire_nce->nce_state is not yet ND_REACHABLE). If the caller
3112535Ssangeeta 		 * has specified MATCH_IRE_COMPLETE, such entries will not
3122535Ssangeeta 		 * be returned; instead, we return the IF_RESOLVER ire.
3132535Ssangeeta 		 */
3142535Ssangeeta 		if (ire->ire_ipif != NULL)
3152535Ssangeeta 			match_flags |= MATCH_IRE_ILL_GROUP;
3162535Ssangeeta 
3172535Ssangeeta 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
3183448Sdh155122 		    ire->ire_ipif, NULL, zoneid, tsl, match_flags, ipst);
3192535Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire,
3202535Ssangeeta 		    (ire_t *), save_ire);
3212535Ssangeeta 		if (ire == NULL ||
3222535Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
3232535Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
3242535Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
3252535Ssangeeta 			/*
3262535Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
3272535Ssangeeta 			 * is set. Also return it via ire.
3282535Ssangeeta 			 */
3292535Ssangeeta 			if (ire != NULL) {
3302535Ssangeeta 				ire_refrele(ire);
3312535Ssangeeta 				ire = NULL;
3322535Ssangeeta 				found_incomplete = B_TRUE;
3332535Ssangeeta 			}
3342535Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
3352535Ssangeeta 				if (pire != NULL) {
3362535Ssangeeta 					/*
3372535Ssangeeta 					 * Need an extra REFHOLD, if the parent
3382535Ssangeeta 					 * ire is returned via both ire and
3392535Ssangeeta 					 * pire.
3402535Ssangeeta 					 */
3412535Ssangeeta 					IRE_REFHOLD(save_ire);
3422535Ssangeeta 				}
3432535Ssangeeta 				ire = save_ire;
3442535Ssangeeta 			} else {
3452535Ssangeeta 				ire_refrele(save_ire);
3462535Ssangeeta 				if (pire != NULL)
3472535Ssangeeta 					*pire = NULL;
3482535Ssangeeta 			}
3492535Ssangeeta 			if (!found_incomplete)
3502535Ssangeeta 				return (ire);
3512535Ssangeeta 		}
3522535Ssangeeta 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
3532535Ssangeeta 			/*
3542535Ssangeeta 			 * If the caller did not ask for pire, release
3552535Ssangeeta 			 * it now.
3562535Ssangeeta 			 */
3572535Ssangeeta 			if (pire == NULL) {
3582535Ssangeeta 				ire_refrele(save_ire);
3592535Ssangeeta 			}
3602535Ssangeeta 			return (ire);
3612535Ssangeeta 		}
3622535Ssangeeta 		match_flags |= MATCH_IRE_TYPE;
3632535Ssangeeta 		gw_addr = ire->ire_gateway_addr;
3642535Ssangeeta 		gw_ipif = ire->ire_ipif;
3652535Ssangeeta 		ire_refrele(ire);
3662535Ssangeeta 		ire = ire_route_lookup(gw_addr, 0, 0,
3672535Ssangeeta 		    (found_incomplete? IRE_INTERFACE :
3682535Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)),
3693448Sdh155122 		    gw_ipif, NULL, zoneid, tsl, match_flags, ipst);
3702535Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire,
3712535Ssangeeta 		    (ire_t *), save_ire);
3722535Ssangeeta 		if (ire == NULL ||
3732535Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
3742535Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
3752535Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
3762535Ssangeeta 			/*
3772535Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
3782535Ssangeeta 			 * is set. Also return it via ire.
3792535Ssangeeta 			 */
3802535Ssangeeta 			if (ire != NULL) {
3812535Ssangeeta 				ire_refrele(ire);
3822535Ssangeeta 				ire = NULL;
3832535Ssangeeta 			}
3842535Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
3852535Ssangeeta 				if (pire != NULL) {
3862535Ssangeeta 					/*
3872535Ssangeeta 					 * Need an extra REFHOLD, if the
3882535Ssangeeta 					 * parent ire is returned via both
3892535Ssangeeta 					 * ire and pire.
3902535Ssangeeta 					 */
3912535Ssangeeta 					IRE_REFHOLD(save_ire);
3922535Ssangeeta 				}
3932535Ssangeeta 				ire = save_ire;
3942535Ssangeeta 			} else {
3952535Ssangeeta 				ire_refrele(save_ire);
3962535Ssangeeta 				if (pire != NULL)
3972535Ssangeeta 					*pire = NULL;
3982535Ssangeeta 			}
3992535Ssangeeta 			return (ire);
4002535Ssangeeta 		} else if (pire == NULL) {
4012535Ssangeeta 			/*
4022535Ssangeeta 			 * If the caller did not ask for pire, release
4032535Ssangeeta 			 * it now.
4042535Ssangeeta 			 */
4052535Ssangeeta 			ire_refrele(save_ire);
4062535Ssangeeta 		}
4072535Ssangeeta 		return (ire);
4082535Ssangeeta 	}
4092535Ssangeeta 	ASSERT(pire == NULL || *pire == NULL);
4102535Ssangeeta 	return (ire);
4112535Ssangeeta }
4122535Ssangeeta 
4132535Ssangeeta 
4142535Ssangeeta /*
4152535Ssangeeta  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
4162535Ssangeeta  * that goes through 'ipif'. As a fallback, a route that goes through
4172535Ssangeeta  * ipif->ipif_ill can be returned.
4182535Ssangeeta  */
4192535Ssangeeta ire_t *
4202535Ssangeeta ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
4212535Ssangeeta {
4222535Ssangeeta 	ire_t	*ire;
4232535Ssangeeta 	ire_t	*save_ire = NULL;
4242535Ssangeeta 	ire_t   *gw_ire;
4252535Ssangeeta 	irb_t   *irb;
4262535Ssangeeta 	ipaddr_t gw_addr;
4272535Ssangeeta 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
4283448Sdh155122 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
4292535Ssangeeta 
4302535Ssangeeta 	ASSERT(CLASSD(group));
4312535Ssangeeta 
4322535Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
4333448Sdh155122 	    NULL, MATCH_IRE_DEFAULT, ipst);
4342535Ssangeeta 
4352535Ssangeeta 	if (ire == NULL)
4362535Ssangeeta 		return (NULL);
4372535Ssangeeta 
4382535Ssangeeta 	irb = ire->ire_bucket;
4392535Ssangeeta 	ASSERT(irb);
4402535Ssangeeta 
4412535Ssangeeta 	IRB_REFHOLD(irb);
4422535Ssangeeta 	ire_refrele(ire);
4432535Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
4442535Ssangeeta 		if (ire->ire_addr != group ||
4452535Ssangeeta 		    ipif->ipif_zoneid != ire->ire_zoneid &&
4462535Ssangeeta 		    ire->ire_zoneid != ALL_ZONES) {
4472535Ssangeeta 			continue;
4482535Ssangeeta 		}
4492535Ssangeeta 
4502535Ssangeeta 		switch (ire->ire_type) {
4512535Ssangeeta 		case IRE_DEFAULT:
4522535Ssangeeta 		case IRE_PREFIX:
4532535Ssangeeta 		case IRE_HOST:
4542535Ssangeeta 			gw_addr = ire->ire_gateway_addr;
4552535Ssangeeta 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
4563448Sdh155122 			    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
4572535Ssangeeta 
4582535Ssangeeta 			if (gw_ire != NULL) {
4592535Ssangeeta 				if (save_ire != NULL) {
4602535Ssangeeta 					ire_refrele(save_ire);
4612535Ssangeeta 				}
4622535Ssangeeta 				IRE_REFHOLD(ire);
4632535Ssangeeta 				if (gw_ire->ire_ipif == ipif) {
4642535Ssangeeta 					ire_refrele(gw_ire);
4652535Ssangeeta 
4662535Ssangeeta 					IRB_REFRELE(irb);
4672535Ssangeeta 					return (ire);
4682535Ssangeeta 				}
4692535Ssangeeta 				ire_refrele(gw_ire);
4702535Ssangeeta 				save_ire = ire;
4712535Ssangeeta 			}
4722535Ssangeeta 			break;
4732535Ssangeeta 		case IRE_IF_NORESOLVER:
4742535Ssangeeta 		case IRE_IF_RESOLVER:
4752535Ssangeeta 			if (ire->ire_ipif == ipif) {
4762535Ssangeeta 				if (save_ire != NULL) {
4772535Ssangeeta 					ire_refrele(save_ire);
4782535Ssangeeta 				}
4792535Ssangeeta 				IRE_REFHOLD(ire);
4802535Ssangeeta 
4812535Ssangeeta 				IRB_REFRELE(irb);
4822535Ssangeeta 				return (ire);
4832535Ssangeeta 			}
4842535Ssangeeta 			break;
4852535Ssangeeta 		}
4862535Ssangeeta 	}
4872535Ssangeeta 	IRB_REFRELE(irb);
4882535Ssangeeta 
4892535Ssangeeta 	return (save_ire);
4902535Ssangeeta }
4912535Ssangeeta 
4922535Ssangeeta /*
4932535Ssangeeta  * Find an IRE_INTERFACE for the multicast group.
4942535Ssangeeta  * Allows different routes for multicast addresses
4952535Ssangeeta  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
4962535Ssangeeta  * which point at different interfaces. This is used when IP_MULTICAST_IF
4972535Ssangeeta  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
4982535Ssangeeta  * specify the interface to join on.
4992535Ssangeeta  *
5002535Ssangeeta  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
5012535Ssangeeta  */
5022535Ssangeeta ire_t *
5033448Sdh155122 ire_lookup_multi(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5042535Ssangeeta {
5052535Ssangeeta 	ire_t	*ire;
5062535Ssangeeta 	ipif_t	*ipif = NULL;
5072535Ssangeeta 	int	match_flags = MATCH_IRE_TYPE;
5082535Ssangeeta 	ipaddr_t gw_addr;
5092535Ssangeeta 
5102535Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
5113448Sdh155122 	    0, NULL, MATCH_IRE_DEFAULT, ipst);
5122535Ssangeeta 
5132535Ssangeeta 	/* We search a resolvable ire in case of multirouting. */
5142535Ssangeeta 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
5152535Ssangeeta 		ire_t *cire = NULL;
5162535Ssangeeta 		/*
5172535Ssangeeta 		 * If the route is not resolvable, the looked up ire
5182535Ssangeeta 		 * may be changed here. In that case, ire_multirt_lookup()
5192535Ssangeeta 		 * IRE_REFRELE the original ire and change it.
5202535Ssangeeta 		 */
5213448Sdh155122 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW,
5223448Sdh155122 		    NULL, ipst);
5232535Ssangeeta 		if (cire != NULL)
5242535Ssangeeta 			ire_refrele(cire);
5252535Ssangeeta 	}
5262535Ssangeeta 	if (ire == NULL)
5272535Ssangeeta 		return (NULL);
5282535Ssangeeta 	/*
5292535Ssangeeta 	 * Make sure we follow ire_ipif.
5302535Ssangeeta 	 *
5312535Ssangeeta 	 * We need to determine the interface route through
5322535Ssangeeta 	 * which the gateway will be reached. We don't really
5332535Ssangeeta 	 * care which interface is picked if the interface is
5342535Ssangeeta 	 * part of a group.
5352535Ssangeeta 	 */
5362535Ssangeeta 	if (ire->ire_ipif != NULL) {
5372535Ssangeeta 		ipif = ire->ire_ipif;
5382535Ssangeeta 		match_flags |= MATCH_IRE_ILL_GROUP;
5392535Ssangeeta 	}
5402535Ssangeeta 
5412535Ssangeeta 	switch (ire->ire_type) {
5422535Ssangeeta 	case IRE_DEFAULT:
5432535Ssangeeta 	case IRE_PREFIX:
5442535Ssangeeta 	case IRE_HOST:
5452535Ssangeeta 		gw_addr = ire->ire_gateway_addr;
5462535Ssangeeta 		ire_refrele(ire);
5472535Ssangeeta 		ire = ire_ftable_lookup(gw_addr, 0, 0,
5482535Ssangeeta 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
5493448Sdh155122 		    NULL, match_flags, ipst);
5502535Ssangeeta 		return (ire);
5512535Ssangeeta 	case IRE_IF_NORESOLVER:
5522535Ssangeeta 	case IRE_IF_RESOLVER:
5532535Ssangeeta 		return (ire);
5542535Ssangeeta 	default:
5552535Ssangeeta 		ire_refrele(ire);
5562535Ssangeeta 		return (NULL);
5572535Ssangeeta 	}
5582535Ssangeeta }
5592535Ssangeeta 
5602535Ssangeeta /*
5612535Ssangeeta  * Delete the passed in ire if the gateway addr matches
5622535Ssangeeta  */
5632535Ssangeeta void
5642535Ssangeeta ire_del_host_redir(ire_t *ire, char *gateway)
5652535Ssangeeta {
5663004Sdd193516 	if ((ire->ire_flags & RTF_DYNAMIC) &&
5672535Ssangeeta 	    (ire->ire_gateway_addr == *(ipaddr_t *)gateway))
5682535Ssangeeta 		ire_delete(ire);
5692535Ssangeeta }
5702535Ssangeeta 
5712535Ssangeeta /*
5722535Ssangeeta  * Search for all HOST REDIRECT routes that are
5732535Ssangeeta  * pointing at the specified gateway and
5742535Ssangeeta  * delete them. This routine is called only
5752535Ssangeeta  * when a default gateway is going away.
5762535Ssangeeta  */
5772535Ssangeeta void
5783448Sdh155122 ire_delete_host_redirects(ipaddr_t gateway, ip_stack_t *ipst)
5792535Ssangeeta {
5802535Ssangeeta 	struct rtfuncarg rtfarg;
5812535Ssangeeta 
5822535Ssangeeta 	(void) memset(&rtfarg, 0, sizeof (rtfarg));
5832535Ssangeeta 	rtfarg.rt_func = ire_del_host_redir;
5842535Ssangeeta 	rtfarg.rt_arg = (void *)&gateway;
5853448Sdh155122 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
5863448Sdh155122 	    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
5872535Ssangeeta }
5882535Ssangeeta 
5892535Ssangeeta struct ihandle_arg {
5902535Ssangeeta 	uint32_t ihandle;
5912535Ssangeeta 	ire_t	 *ire;
5922535Ssangeeta };
5932535Ssangeeta 
5942535Ssangeeta static int
5952535Ssangeeta ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
5962535Ssangeeta {
5972535Ssangeeta 	struct rt_entry *rt;
5982535Ssangeeta 	irb_t *irb;
5992535Ssangeeta 	ire_t *ire;
6002535Ssangeeta 	struct ihandle_arg *ih = arg;
6012535Ssangeeta 
6022535Ssangeeta 	rt = (struct rt_entry *)rn;
6032535Ssangeeta 	ASSERT(rt != NULL);
6042535Ssangeeta 	irb = &rt->rt_irb;
6052535Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
6062535Ssangeeta 		if ((ire->ire_type & IRE_INTERFACE) &&
6072535Ssangeeta 		    (ire->ire_ihandle == ih->ihandle)) {
6082535Ssangeeta 			ih->ire = ire;
6092535Ssangeeta 			IRE_REFHOLD(ire);
6102535Ssangeeta 			return (1);
6112535Ssangeeta 		}
6122535Ssangeeta 	}
6132535Ssangeeta 	return (0);
6142535Ssangeeta }
6152535Ssangeeta 
6162535Ssangeeta /*
6172535Ssangeeta  * Locate the interface ire that is tied to the cache ire 'cire' via
6182535Ssangeeta  * cire->ire_ihandle.
6192535Ssangeeta  *
6202535Ssangeeta  * We are trying to create the cache ire for an onlink destn. or
6212535Ssangeeta  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
6222535Ssangeeta  * case, after the ire has come back from ARP.
6232535Ssangeeta  */
6242535Ssangeeta ire_t *
6252535Ssangeeta ire_ihandle_lookup_onlink(ire_t *cire)
6262535Ssangeeta {
6272535Ssangeeta 	ire_t	*ire;
6282535Ssangeeta 	int	match_flags;
6292535Ssangeeta 	struct ihandle_arg ih;
6303448Sdh155122 	ip_stack_t *ipst;
6312535Ssangeeta 
6322535Ssangeeta 	ASSERT(cire != NULL);
6333448Sdh155122 	ipst = cire->ire_ipst;
6342535Ssangeeta 
6352535Ssangeeta 	/*
6362535Ssangeeta 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
6372535Ssangeeta 	 * because the ihandle refers to an ipif which can be in only one zone.
6382535Ssangeeta 	 */
6392535Ssangeeta 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
6402535Ssangeeta 	/*
6412535Ssangeeta 	 * We know that the mask of the interface ire equals cire->ire_cmask.
6422535Ssangeeta 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
6432535Ssangeeta 	 * cmask from the interface ire's mask)
6442535Ssangeeta 	 */
6452535Ssangeeta 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
6462535Ssangeeta 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
6473448Sdh155122 	    NULL, match_flags, ipst);
6482535Ssangeeta 	if (ire != NULL)
6492535Ssangeeta 		return (ire);
6502535Ssangeeta 	/*
6512535Ssangeeta 	 * If we didn't find an interface ire above, we can't declare failure.
6522535Ssangeeta 	 * For backwards compatibility, we need to support prefix routes
6532535Ssangeeta 	 * pointing to next hop gateways that are not on-link.
6542535Ssangeeta 	 *
6552535Ssangeeta 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
6562535Ssangeeta 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
6572535Ssangeeta 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
6582535Ssangeeta 	 * doing ire_route_lookup() twice and returning an interface ire.
6592535Ssangeeta 	 *
6602535Ssangeeta 	 * Eg. default	-	gw1			(line 1)
6612535Ssangeeta 	 *	gw1	-	gw2			(line 2)
6622535Ssangeeta 	 *	gw2	-	hme0			(line 3)
6632535Ssangeeta 	 *
6642535Ssangeeta 	 * In the above example, ip_newroute() tried to create the cache ire
6652535Ssangeeta 	 * 'cire' for gw1, based on the interface route in line 3. The
6662535Ssangeeta 	 * ire_ftable_lookup() above fails, because there is no interface route
6672535Ssangeeta 	 * to reach gw1. (it is gw2). We fall thru below.
6682535Ssangeeta 	 *
6692535Ssangeeta 	 * Do a brute force search based on the ihandle in a subset of the
6702535Ssangeeta 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
6712535Ssangeeta 	 * things become very complex, since we don't have 'pire' in this
6722535Ssangeeta 	 * case. (Also note that this method is not possible in the offlink
6732535Ssangeeta 	 * case because we don't know the mask)
6742535Ssangeeta 	 */
6752535Ssangeeta 	(void) memset(&ih, 0, sizeof (ih));
6762535Ssangeeta 	ih.ihandle = cire->ire_ihandle;
6773448Sdh155122 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
6783448Sdh155122 	    ire_ihandle_onlink_match, &ih, irb_refhold_rn, irb_refrele_rn);
6792535Ssangeeta 	return (ih.ire);
6802535Ssangeeta }
6812535Ssangeeta 
6822535Ssangeeta /*
6832535Ssangeeta  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
6842535Ssangeeta  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
6852535Ssangeeta  * in the bucket skipping default interface routes and deleted entries.
6862535Ssangeeta  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
6872535Ssangeeta  * Assumes that the caller holds a reference on the IRE bucket.
6882535Ssangeeta  */
6892535Ssangeeta ire_t *
6902535Ssangeeta ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
6912535Ssangeeta {
6922535Ssangeeta 	ASSERT(ire_origin->ire_bucket != NULL);
6932535Ssangeeta 	ASSERT(ire != NULL);
6942535Ssangeeta 
6952535Ssangeeta 	do {
6962535Ssangeeta 		ire = ire->ire_next;
6972535Ssangeeta 		if (ire == NULL)
6982535Ssangeeta 			ire = ire_origin->ire_bucket->irb_ire;
6992535Ssangeeta 		if (ire == ire_origin)
7002535Ssangeeta 			return (NULL);
7012535Ssangeeta 	} while ((ire->ire_type & IRE_INTERFACE) ||
7022535Ssangeeta 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
7032535Ssangeeta 	ASSERT(ire != NULL);
7042535Ssangeeta 	return (ire);
7052535Ssangeeta }
7062535Ssangeeta 
7072535Ssangeeta static ipif_t *
7082535Ssangeeta ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
7092535Ssangeeta     int zoneid, ushort_t *marks)
7102535Ssangeeta {
7112535Ssangeeta 	ipif_t *src_ipif;
7123448Sdh155122 	ip_stack_t *ipst = dst_ill->ill_ipst;
7132535Ssangeeta 
7142535Ssangeeta 	/*
7152535Ssangeeta 	 * Pick the best source address from dst_ill.
7162535Ssangeeta 	 *
7172535Ssangeeta 	 * 1) If it is part of a multipathing group, we would
7182535Ssangeeta 	 *    like to spread the inbound packets across different
7192535Ssangeeta 	 *    interfaces. ipif_select_source picks a random source
7202535Ssangeeta 	 *    across the different ills in the group.
7212535Ssangeeta 	 *
7222535Ssangeeta 	 * 2) If it is not part of a multipathing group, we try
7232535Ssangeeta 	 *    to pick the source address from the destination
7242535Ssangeeta 	 *    route. Clustering assumes that when we have multiple
7252535Ssangeeta 	 *    prefixes hosted on an interface, the prefix of the
7262535Ssangeeta 	 *    source address matches the prefix of the destination
7272535Ssangeeta 	 *    route. We do this only if the address is not
7282535Ssangeeta 	 *    DEPRECATED.
7292535Ssangeeta 	 *
7302535Ssangeeta 	 * 3) If the conn is in a different zone than the ire, we
7312535Ssangeeta 	 *    need to pick a source address from the right zone.
7322535Ssangeeta 	 *
7332535Ssangeeta 	 * NOTE : If we hit case (1) above, the prefix of the source
7342535Ssangeeta 	 *	  address picked may not match the prefix of the
7352535Ssangeeta 	 *	  destination routes prefix as ipif_select_source
7362535Ssangeeta 	 *	  does not look at "dst" while picking a source
7372535Ssangeeta 	 *	  address.
7382535Ssangeeta 	 *	  If we want the same behavior as (2), we will need
7392535Ssangeeta 	 *	  to change the behavior of ipif_select_source.
7402535Ssangeeta 	 */
7412535Ssangeeta 
7422535Ssangeeta 	if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
7432535Ssangeeta 		/*
7442535Ssangeeta 		 * The RTF_SETSRC flag is set in the parent ire (sire).
7452535Ssangeeta 		 * Check that the ipif matching the requested source
7462535Ssangeeta 		 * address still exists.
7472535Ssangeeta 		 */
7482535Ssangeeta 		src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
7493448Sdh155122 		    zoneid, NULL, NULL, NULL, NULL, ipst);
7502535Ssangeeta 		return (src_ipif);
7512535Ssangeeta 	}
7522535Ssangeeta 	*marks |= IRE_MARK_USESRC_CHECK;
7532535Ssangeeta 	if ((dst_ill->ill_group != NULL) ||
7542535Ssangeeta 	    (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
7552535Ssangeeta 	    (dst_ill->ill_usesrc_ifindex != 0)) {
7562535Ssangeeta 		src_ipif = ipif_select_source(dst_ill, dst, zoneid);
7572535Ssangeeta 		if (src_ipif == NULL)
7582535Ssangeeta 			return (NULL);
7592535Ssangeeta 
7602535Ssangeeta 	} else {
7612535Ssangeeta 		src_ipif = ire->ire_ipif;
7622535Ssangeeta 		ASSERT(src_ipif != NULL);
7632535Ssangeeta 		/* hold src_ipif for uniformity */
7642535Ssangeeta 		ipif_refhold(src_ipif);
7652535Ssangeeta 	}
7662535Ssangeeta 	return (src_ipif);
7672535Ssangeeta }
7682535Ssangeeta 
7692535Ssangeeta /*
7702535Ssangeeta  * This function is called by ip_rput_noire() and ip_fast_forward()
7712535Ssangeeta  * to resolve the route of incoming packet that needs to be forwarded.
7722535Ssangeeta  * If the ire of the nexthop is not already in the cachetable, this
7732535Ssangeeta  * routine will insert it to the table, but won't trigger ARP resolution yet.
7742535Ssangeeta  * Thus unlike ip_newroute, this function adds incomplete ires to
7752535Ssangeeta  * the cachetable. ARP resolution for these ires are  delayed until
7762535Ssangeeta  * after all of the packet processing is completed and its ready to
7772535Ssangeeta  * be sent out on the wire, Eventually, the packet transmit routine
7782535Ssangeeta  * ip_xmit_v4() attempts to send a packet  to the driver. If it finds
7792535Ssangeeta  * that there is no link layer information, it will do the arp
7802535Ssangeeta  * resolution and queue the packet in ire->ire_nce->nce_qd_mp and
7812535Ssangeeta  * then send it out once the arp resolution is over
7822535Ssangeeta  * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
7832535Ssangeeta  * the model of BSD/SunOS 4
7842535Ssangeeta  *
7852535Ssangeeta  * In future, the insertion of incomplete ires in the cachetable should
7862535Ssangeeta  * be implemented in hostpath as well, as doing so will greatly reduce
7872535Ssangeeta  * the existing complexity for code paths that depend on the context of
7882535Ssangeeta  * the sender (such as IPsec).
7892535Ssangeeta  *
7902535Ssangeeta  * Thus this scheme of adding incomplete ires in cachetable in forwarding
7912535Ssangeeta  * path can be used as a template for simplifying the hostpath.
7922535Ssangeeta  */
7932535Ssangeeta 
7942535Ssangeeta ire_t *
7952535Ssangeeta ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
7963448Sdh155122     ire_t *supplied_sire, const struct ts_label_s *tsl, ip_stack_t *ipst)
7972535Ssangeeta {
7982535Ssangeeta 	ipaddr_t gw = 0;
7992535Ssangeeta 	ire_t	*ire = NULL;
8002535Ssangeeta 	ire_t   *sire = NULL, *save_ire;
8012535Ssangeeta 	ill_t *dst_ill = NULL;
8022535Ssangeeta 	int error;
8032535Ssangeeta 	zoneid_t zoneid;
8042535Ssangeeta 	ipif_t *src_ipif = NULL;
8052535Ssangeeta 	mblk_t *res_mp;
8062535Ssangeeta 	ushort_t ire_marks = 0;
8072535Ssangeeta 	tsol_gcgrp_t *gcgrp = NULL;
8082535Ssangeeta 	tsol_gcgrp_addr_t ga;
8092535Ssangeeta 
8102535Ssangeeta 	zoneid = GLOBAL_ZONEID;
8112535Ssangeeta 
8122535Ssangeeta 	if (supplied_ire != NULL) {
8132535Ssangeeta 		/* We have arrived here from ipfil_sendpkt */
8142535Ssangeeta 		ire = supplied_ire;
8152535Ssangeeta 		sire = supplied_sire;
8162535Ssangeeta 		goto create_irecache;
8172535Ssangeeta 	}
8182535Ssangeeta 
8192535Ssangeeta 	ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
8202535Ssangeeta 	    tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
8213448Sdh155122 	    MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR, ipst);
8222535Ssangeeta 
8232535Ssangeeta 	if (ire == NULL) {
8243448Sdh155122 		ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst);
8252535Ssangeeta 		goto icmp_err_ret;
8262535Ssangeeta 	}
8272535Ssangeeta 
8282535Ssangeeta 	/*
8292535Ssangeeta 	 * If we encounter CGTP, we should  have the caller use
8302535Ssangeeta 	 * ip_newroute to resolve multirt instead of this function.
8312535Ssangeeta 	 * CGTP specs explicitly state that it can't be used with routers.
8322535Ssangeeta 	 * This essentially prevents insertion of incomplete RTF_MULTIRT
8332535Ssangeeta 	 * ires in cachetable.
8342535Ssangeeta 	 */
8352535Ssangeeta 	if (ip_cgtp_filter &&
8362535Ssangeeta 	    ((ire->ire_flags & RTF_MULTIRT) ||
8372535Ssangeeta 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
8382535Ssangeeta 		ip3dbg(("ire_forward: packet is to be multirouted- "
8392535Ssangeeta 		    "handing it to ip_newroute\n"));
8402535Ssangeeta 		if (sire != NULL)
8412535Ssangeeta 			ire_refrele(sire);
8422535Ssangeeta 		ire_refrele(ire);
8432535Ssangeeta 		/*
8442535Ssangeeta 		 * Inform caller about encountering of multirt so that
8452535Ssangeeta 		 * ip_newroute() can be called.
8462535Ssangeeta 		 */
8472535Ssangeeta 		*check_multirt = B_TRUE;
8482535Ssangeeta 		return (NULL);
8492535Ssangeeta 	}
8502535Ssangeeta 
8512535Ssangeeta 	*check_multirt = B_FALSE;
8522535Ssangeeta 
8532535Ssangeeta 	/*
8542535Ssangeeta 	 * Verify that the returned IRE does not have either
8552535Ssangeeta 	 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
8562535Ssangeeta 	 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
8572535Ssangeeta 	 */
8582535Ssangeeta 	if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
8592535Ssangeeta 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
8602535Ssangeeta 		ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
8612535Ssangeeta 		    (void *)ire));
8622535Ssangeeta 		goto icmp_err_ret;
8632535Ssangeeta 	}
8642535Ssangeeta 
8652535Ssangeeta 	/*
8662535Ssangeeta 	 * If we already have a fully resolved IRE CACHE of the
8672535Ssangeeta 	 * nexthop router, just hand over the cache entry
8682535Ssangeeta 	 * and we are done.
8692535Ssangeeta 	 */
8702535Ssangeeta 
8712535Ssangeeta 	if (ire->ire_type & IRE_CACHE) {
8722535Ssangeeta 
8732535Ssangeeta 		/*
8742535Ssangeeta 		 * If we are using this ire cache entry as a
8752535Ssangeeta 		 * gateway to forward packets, chances are we
8762535Ssangeeta 		 * will be using it again. So turn off
8772535Ssangeeta 		 * the temporary flag, thus reducing its
8782535Ssangeeta 		 * chances of getting deleted frequently.
8792535Ssangeeta 		 */
8802535Ssangeeta 		if (ire->ire_marks & IRE_MARK_TEMPORARY) {
8812535Ssangeeta 			irb_t *irb = ire->ire_bucket;
8822535Ssangeeta 			rw_enter(&irb->irb_lock, RW_WRITER);
8832535Ssangeeta 			ire->ire_marks &= ~IRE_MARK_TEMPORARY;
8842535Ssangeeta 			irb->irb_tmp_ire_cnt--;
8852535Ssangeeta 			rw_exit(&irb->irb_lock);
8862535Ssangeeta 		}
8872535Ssangeeta 
8882535Ssangeeta 		if (sire != NULL) {
8892535Ssangeeta 			UPDATE_OB_PKT_COUNT(sire);
8902535Ssangeeta 			sire->ire_last_used_time = lbolt;
8912535Ssangeeta 			ire_refrele(sire);
8922535Ssangeeta 		}
8932535Ssangeeta 		return (ire);
8942535Ssangeeta 	}
8952535Ssangeeta create_irecache:
8962535Ssangeeta 	/*
8972535Ssangeeta 	 * Increment the ire_ob_pkt_count field for ire if it is an
8982535Ssangeeta 	 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
8992535Ssangeeta 	 * increment the same for the parent IRE, sire, if it is some
9004365Snordmark 	 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST).
9012535Ssangeeta 	 */
9022535Ssangeeta 	if ((ire->ire_type & IRE_INTERFACE) != 0) {
9032535Ssangeeta 		UPDATE_OB_PKT_COUNT(ire);
9042535Ssangeeta 		ire->ire_last_used_time = lbolt;
9052535Ssangeeta 	}
9062535Ssangeeta 
9072535Ssangeeta 	/*
9082535Ssangeeta 	 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
9092535Ssangeeta 	 */
9102535Ssangeeta 	if (sire != NULL) {
9112535Ssangeeta 		gw = sire->ire_gateway_addr;
9122535Ssangeeta 		ASSERT((sire->ire_type &
9132535Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)) == 0);
9142535Ssangeeta 		UPDATE_OB_PKT_COUNT(sire);
9152535Ssangeeta 		sire->ire_last_used_time = lbolt;
9162535Ssangeeta 	}
9172535Ssangeeta 
9182535Ssangeeta 	/* Obtain dst_ill */
9192535Ssangeeta 	dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
9202535Ssangeeta 	if (dst_ill == NULL) {
9212535Ssangeeta 		ip2dbg(("ire_forward no dst ill; ire 0x%p\n",
9222535Ssangeeta 			(void *)ire));
9232535Ssangeeta 		goto icmp_err_ret;
9242535Ssangeeta 	}
9252535Ssangeeta 
9262535Ssangeeta 	ASSERT(src_ipif == NULL);
9272535Ssangeeta 	/* Now obtain the src_ipif */
9282535Ssangeeta 	src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
9292535Ssangeeta 	    zoneid, &ire_marks);
9302535Ssangeeta 	if (src_ipif == NULL)
9312535Ssangeeta 		goto icmp_err_ret;
9322535Ssangeeta 
9332535Ssangeeta 	switch (ire->ire_type) {
9342535Ssangeeta 	case IRE_IF_NORESOLVER:
9352535Ssangeeta 		/* create ire_cache for ire_addr endpoint */
9362535Ssangeeta 	case IRE_IF_RESOLVER:
9372535Ssangeeta 		/*
9382535Ssangeeta 		 * We have the IRE_IF_RESOLVER of the nexthop gateway
9392535Ssangeeta 		 * and now need to build a IRE_CACHE for it.
9402535Ssangeeta 		 * In this case, we have the following :
9412535Ssangeeta 		 *
9422535Ssangeeta 		 * 1) src_ipif - used for getting a source address.
9432535Ssangeeta 		 *
9442535Ssangeeta 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
9452535Ssangeeta 		 *    means packets using the IRE_CACHE that we will build
9462535Ssangeeta 		 *    here will go out on dst_ill.
9472535Ssangeeta 		 *
9482535Ssangeeta 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
9492535Ssangeeta 		 *    to be created will only be tied to the IRE_INTERFACE
9502535Ssangeeta 		 *    that was derived from the ire_ihandle field.
9512535Ssangeeta 		 *
9522535Ssangeeta 		 *    If sire is non-NULL, it means the destination is
9532535Ssangeeta 		 *    off-link and we will first create the IRE_CACHE for the
9542535Ssangeeta 		 *    gateway.
9552535Ssangeeta 		 */
9562535Ssangeeta 		res_mp = dst_ill->ill_resolver_mp;
9572535Ssangeeta 		if (ire->ire_type == IRE_IF_RESOLVER &&
9582535Ssangeeta 		    (!OK_RESOLVER_MP(res_mp))) {
9592535Ssangeeta 			ire_refrele(ire);
9602535Ssangeeta 			ire = NULL;
9612535Ssangeeta 			goto out;
9622535Ssangeeta 		}
9632535Ssangeeta 		/*
9642535Ssangeeta 		 * To be at this point in the code with a non-zero gw
9652535Ssangeeta 		 * means that dst is reachable through a gateway that
9662535Ssangeeta 		 * we have never resolved.  By changing dst to the gw
9672535Ssangeeta 		 * addr we resolve the gateway first.
9682535Ssangeeta 		 */
9692535Ssangeeta 		if (gw != INADDR_ANY) {
9702535Ssangeeta 			/*
9712535Ssangeeta 			 * The source ipif that was determined above was
9722535Ssangeeta 			 * relative to the destination address, not the
9732535Ssangeeta 			 * gateway's. If src_ipif was not taken out of
9742535Ssangeeta 			 * the IRE_IF_RESOLVER entry, we'll need to call
9752535Ssangeeta 			 * ipif_select_source() again.
9762535Ssangeeta 			 */
9772535Ssangeeta 			if (src_ipif != ire->ire_ipif) {
9782535Ssangeeta 				ipif_refrele(src_ipif);
9792535Ssangeeta 				src_ipif = ipif_select_source(dst_ill,
9802535Ssangeeta 				    gw, zoneid);
9812535Ssangeeta 				if (src_ipif == NULL)
9822535Ssangeeta 					goto icmp_err_ret;
9832535Ssangeeta 			}
9842535Ssangeeta 			dst = gw;
9852535Ssangeeta 			gw = INADDR_ANY;
9862535Ssangeeta 		}
9872535Ssangeeta 		/*
9882535Ssangeeta 		 * dst has been set to the address of the nexthop.
9892535Ssangeeta 		 *
9902535Ssangeeta 		 * TSol note: get security attributes of the nexthop;
9912535Ssangeeta 		 * Note that the nexthop may either be a gateway, or the
9922535Ssangeeta 		 * packet destination itself; Detailed explanation of
9932535Ssangeeta 		 * issues involved is  provided in the  IRE_IF_NORESOLVER
9942535Ssangeeta 		 * logic in ip_newroute().
9952535Ssangeeta 		 */
9962535Ssangeeta 		ga.ga_af = AF_INET;
9972535Ssangeeta 		IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
9982535Ssangeeta 		gcgrp = gcgrp_lookup(&ga, B_FALSE);
9992535Ssangeeta 
10002535Ssangeeta 		if (ire->ire_type == IRE_IF_NORESOLVER)
10012535Ssangeeta 			dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
10022535Ssangeeta 
10032535Ssangeeta 		save_ire = ire;
10042535Ssangeeta 		/*
10052535Ssangeeta 		 * create an incomplete ire-cache with a null dlureq_mp.
10062535Ssangeeta 		 * The dlureq_mp will be created in ire_arpresolve.
10072535Ssangeeta 		 */
10082535Ssangeeta 		ire = ire_create(
10092535Ssangeeta 			(uchar_t *)&dst,		/* dest address */
10102535Ssangeeta 		    (uchar_t *)&ip_g_all_ones,	/* mask */
10112535Ssangeeta 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
10122535Ssangeeta 		    (uchar_t *)&gw,		/* gateway address */
10132535Ssangeeta 		    NULL,
10142535Ssangeeta 		    (save_ire->ire_type == IRE_IF_RESOLVER ?  NULL:
10152535Ssangeeta 		    &save_ire->ire_max_frag),
10162535Ssangeeta 		    NULL,
10172535Ssangeeta 		    dst_ill->ill_rq,		/* recv-from queue */
10182535Ssangeeta 		    dst_ill->ill_wq,		/* send-to queue */
10192535Ssangeeta 		    IRE_CACHE,			/* IRE type */
10202535Ssangeeta 		    NULL,
10212535Ssangeeta 		    src_ipif,
10222535Ssangeeta 		    NULL,
10232535Ssangeeta 		    ire->ire_mask,		/* Parent mask */
10242535Ssangeeta 		    0,
10252535Ssangeeta 		    ire->ire_ihandle,	/* Interface handle */
10262535Ssangeeta 		    0,
10272535Ssangeeta 		    &(ire->ire_uinfo),
10282535Ssangeeta 		    NULL,
10293448Sdh155122 		    gcgrp,
10303448Sdh155122 		    ipst);
10312535Ssangeeta 		ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
10322535Ssangeeta 		if (ire != NULL) {
10332535Ssangeeta 			gcgrp = NULL; /* reference now held by IRE */
10342535Ssangeeta 			ire->ire_marks |= ire_marks;
10352535Ssangeeta 			/* add the incomplete ire: */
10362535Ssangeeta 			error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
10372535Ssangeeta 			if (error == 0 && ire != NULL) {
10382535Ssangeeta 				ire->ire_max_frag = save_ire->ire_max_frag;
10392535Ssangeeta 				ip1dbg(("setting max_frag to %d in ire 0x%p\n",
10402535Ssangeeta 				    ire->ire_max_frag, (void *)ire));
10412535Ssangeeta 			} else {
10422535Ssangeeta 				ire_refrele(save_ire);
10432535Ssangeeta 				goto icmp_err_ret;
10442535Ssangeeta 			}
10452535Ssangeeta 		} else {
10462535Ssangeeta 			if (gcgrp != NULL) {
10472535Ssangeeta 				GCGRP_REFRELE(gcgrp);
10482535Ssangeeta 				gcgrp = NULL;
10492535Ssangeeta 			}
10502535Ssangeeta 		}
10512535Ssangeeta 
10522535Ssangeeta 		ire_refrele(save_ire);
10532535Ssangeeta 		break;
10542535Ssangeeta 	default:
10552535Ssangeeta 		break;
10562535Ssangeeta 	}
10572535Ssangeeta 
10582535Ssangeeta out:
10592535Ssangeeta 	if (sire != NULL)
10602535Ssangeeta 		ire_refrele(sire);
10612535Ssangeeta 	if (dst_ill != NULL)
10622535Ssangeeta 		ill_refrele(dst_ill);
10632535Ssangeeta 	if (src_ipif != NULL)
10642535Ssangeeta 		ipif_refrele(src_ipif);
10652535Ssangeeta 	return (ire);
10662535Ssangeeta icmp_err_ret:
10672535Ssangeeta 	if (src_ipif != NULL)
10682535Ssangeeta 		ipif_refrele(src_ipif);
10692535Ssangeeta 	if (dst_ill != NULL)
10702535Ssangeeta 		ill_refrele(dst_ill);
10712535Ssangeeta 	if (sire != NULL)
10722535Ssangeeta 		ire_refrele(sire);
10732535Ssangeeta 	if (ire != NULL) {
10742535Ssangeeta 		ire_refrele(ire);
10752535Ssangeeta 	}
10762535Ssangeeta 	/* caller needs to send icmp error message */
10772535Ssangeeta 	return (NULL);
10782535Ssangeeta 
10792535Ssangeeta }
10802535Ssangeeta 
10812535Ssangeeta /*
10823448Sdh155122  * Obtain the rt_entry and rt_irb for the route to be added to
10833448Sdh155122  * the ips_ip_ftable.
10842535Ssangeeta  * First attempt to add a node to the radix tree via rn_addroute. If the
10852535Ssangeeta  * route already exists, return the bucket for the existing route.
10862535Ssangeeta  *
10872535Ssangeeta  * Locking notes: Need to hold the global radix tree lock in write mode to
10882535Ssangeeta  * add a radix node. To prevent the node from being deleted, ire_get_bucket()
10892535Ssangeeta  * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
10902535Ssangeeta  * while holding the irb_lock, but not the radix tree lock.
10912535Ssangeeta  */
10922535Ssangeeta irb_t *
10932535Ssangeeta ire_get_bucket(ire_t *ire)
10942535Ssangeeta {
10952535Ssangeeta 	struct radix_node *rn;
10962535Ssangeeta 	struct rt_entry *rt;
10972535Ssangeeta 	struct rt_sockaddr rmask, rdst;
10982535Ssangeeta 	irb_t *irb = NULL;
10993448Sdh155122 	ip_stack_t *ipst = ire->ire_ipst;
11002535Ssangeeta 
11013448Sdh155122 	ASSERT(ipst->ips_ip_ftable != NULL);
11022535Ssangeeta 
11032535Ssangeeta 	/* first try to see if route exists (based on rtalloc1) */
11042535Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
11052535Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
11062535Ssangeeta 	rdst.rt_sin_family = AF_INET;
11072535Ssangeeta 	rdst.rt_sin_addr.s_addr = ire->ire_addr;
11082535Ssangeeta 
11092535Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
11102535Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
11112535Ssangeeta 	rmask.rt_sin_family = AF_INET;
11122535Ssangeeta 	rmask.rt_sin_addr.s_addr = ire->ire_mask;
11132535Ssangeeta 
11142535Ssangeeta 	/*
11152535Ssangeeta 	 * add the route. based on BSD's rtrequest1(RTM_ADD)
11162535Ssangeeta 	 */
11172535Ssangeeta 	R_Malloc(rt, rt_entry_cache,  sizeof (*rt));
11182535Ssangeeta 	(void) memset(rt, 0, sizeof (*rt));
11192535Ssangeeta 	rt->rt_nodes->rn_key = (char *)&rt->rt_dst;
11202535Ssangeeta 	rt->rt_dst = rdst;
11212535Ssangeeta 	irb = &rt->rt_irb;
11222535Ssangeeta 	irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */
11233448Sdh155122 	irb->irb_ipst = ipst;
11242535Ssangeeta 	rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL);
11253448Sdh155122 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
11263448Sdh155122 	rn = ipst->ips_ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask,
11273448Sdh155122 	    ipst->ips_ip_ftable, (struct radix_node *)rt);
11282535Ssangeeta 	if (rn == NULL) {
11293448Sdh155122 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
11302535Ssangeeta 		Free(rt, rt_entry_cache);
11312535Ssangeeta 		rt = NULL;
11322535Ssangeeta 		irb = NULL;
11333448Sdh155122 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
11343448Sdh155122 		rn = ipst->ips_ip_ftable->rnh_lookup(&rdst, &rmask,
11353448Sdh155122 		    ipst->ips_ip_ftable);
11363448Sdh155122 		if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
11372535Ssangeeta 			/* found a non-root match */
11382535Ssangeeta 			rt = (struct rt_entry *)rn;
11392535Ssangeeta 		}
11402535Ssangeeta 	}
11412535Ssangeeta 	if (rt != NULL) {
11422535Ssangeeta 		irb = &rt->rt_irb;
11432535Ssangeeta 		IRB_REFHOLD(irb);
11442535Ssangeeta 	}
11453448Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
11462535Ssangeeta 	return (irb);
11472535Ssangeeta }
11482535Ssangeeta 
11492535Ssangeeta /*
11502535Ssangeeta  * This function is used when the caller wants to know the outbound
11512535Ssangeeta  * interface for a packet given only the address.
11522535Ssangeeta  * If this is a offlink IP address and there are multiple
11532535Ssangeeta  * routes to this destination, this routine will utilise the
11542535Ssangeeta  * first route it finds to IP address
11552535Ssangeeta  * Return values:
11562535Ssangeeta  * 	0	- FAILURE
11572535Ssangeeta  *	nonzero	- ifindex
11582535Ssangeeta  */
11592535Ssangeeta uint_t
11602535Ssangeeta ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid)
11612535Ssangeeta {
11622535Ssangeeta 	uint_t ifindex = 0;
11632535Ssangeeta 	ire_t *ire;
11642535Ssangeeta 	ill_t *ill;
11653448Sdh155122 	netstack_t *ns;
11663448Sdh155122 	ip_stack_t *ipst;
11672535Ssangeeta 
11683448Sdh155122 	if (zoneid == ALL_ZONES)
11693448Sdh155122 		ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
11703448Sdh155122 	else
11713448Sdh155122 		ns = netstack_find_by_zoneid(zoneid);
11723448Sdh155122 	ASSERT(ns != NULL);
11733448Sdh155122 
11743448Sdh155122 	/*
11753448Sdh155122 	 * For exclusive stacks we set the zoneid to zero
11763448Sdh155122 	 * since IP uses the global zoneid in the exclusive stacks.
11773448Sdh155122 	 */
11783448Sdh155122 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
11793448Sdh155122 		zoneid = GLOBAL_ZONEID;
11803448Sdh155122 	ipst = ns->netstack_ip;
11812535Ssangeeta 
11822535Ssangeeta 	ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6);
11832535Ssangeeta 
11843448Sdh155122 	if ((ire =  route_to_dst(ipaddr, zoneid, ipst)) != NULL) {
11852535Ssangeeta 		ill = ire_to_ill(ire);
11862535Ssangeeta 		if (ill != NULL)
11872535Ssangeeta 			ifindex = ill->ill_phyint->phyint_ifindex;
11882535Ssangeeta 		ire_refrele(ire);
11892535Ssangeeta 	}
11903448Sdh155122 	netstack_rele(ns);
11912535Ssangeeta 	return (ifindex);
11922535Ssangeeta }
11932535Ssangeeta 
11942535Ssangeeta /*
11952535Ssangeeta  * Routine to find the route to a destination. If a ifindex is supplied
11962535Ssangeeta  * it tries to match the the route to the corresponding ipif for the ifindex
11972535Ssangeeta  */
11982535Ssangeeta static	ire_t *
11993448Sdh155122 route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid, ip_stack_t *ipst)
12002535Ssangeeta {
12012535Ssangeeta 	ire_t *ire = NULL;
12022535Ssangeeta 	int match_flags;
12032535Ssangeeta 
12042535Ssangeeta 	match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
12052535Ssangeeta 	    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
12062535Ssangeeta 
12072535Ssangeeta 	/* XXX pass NULL tsl for now */
12082535Ssangeeta 
12092535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
12102535Ssangeeta 		ire = ire_route_lookup(
12112535Ssangeeta 		    ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr,
12123448Sdh155122 			0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
12132535Ssangeeta 	} else {
12142535Ssangeeta 		ire = ire_route_lookup_v6(
12152535Ssangeeta 		    &((struct sockaddr_in6 *)dst_addr)->sin6_addr,
12163448Sdh155122 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
12172535Ssangeeta 	}
12182535Ssangeeta 	return (ire);
12192535Ssangeeta }
12202535Ssangeeta 
12212535Ssangeeta /*
12222535Ssangeeta  * This routine is called by IP Filter to send a packet out on the wire
12232535Ssangeeta  * to a specified V4 dst (which may be onlink or offlink). The ifindex may or
12242535Ssangeeta  * may not be 0. A non-null ifindex indicates IP Filter has stipulated
12252535Ssangeeta  * an outgoing interface and requires the nexthop to be on that interface.
1226*4482Sdr146992  * IP WILL NOT DO the following to the data packet before sending it out:
12272535Ssangeeta  *	a. manipulate ttl
1228*4482Sdr146992  *	b. ipsec work
1229*4482Sdr146992  *	c. fragmentation
1230*4482Sdr146992  *
1231*4482Sdr146992  * If the packet has been prepared for hardware checksum then it will be
1232*4482Sdr146992  * passed off to ip_send_align_cksum() to check that the flags set on the
1233*4482Sdr146992  * packet are in alignment with the capabilities of the new outgoing NIC.
12342535Ssangeeta  *
12352535Ssangeeta  * Return values:
12362535Ssangeeta  *	0:		IP was able to send of the data pkt
12372535Ssangeeta  *	ECOMM:		Could not send packet
12382535Ssangeeta  *	ENONET		No route to dst. It is up to the caller
12392535Ssangeeta  *			to send icmp unreachable error message,
12402535Ssangeeta  *	EINPROGRESS	The macaddr of the onlink dst or that
12412535Ssangeeta  *			of the offlink dst's nexthop needs to get
12422535Ssangeeta  *			resolved before packet can be sent to dst.
12432535Ssangeeta  *			Thus transmission is not guaranteed.
12442535Ssangeeta  *
12452535Ssangeeta  */
12462535Ssangeeta 
12472535Ssangeeta int
12482535Ssangeeta ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex,
12492535Ssangeeta     zoneid_t zoneid)
12502535Ssangeeta {
12512535Ssangeeta 	ire_t *ire = NULL, *sire = NULL;
12522535Ssangeeta 	ire_t *ire_cache = NULL;
12532535Ssangeeta 	boolean_t   check_multirt = B_FALSE;
12542535Ssangeeta 	int value;
12552535Ssangeeta 	int match_flags;
12562535Ssangeeta 	ipaddr_t dst;
12573448Sdh155122 	netstack_t *ns;
12583448Sdh155122 	ip_stack_t *ipst;
12592535Ssangeeta 
12602535Ssangeeta 	ASSERT(mp != NULL);
12612535Ssangeeta 
12623448Sdh155122 	if (zoneid == ALL_ZONES)
12633448Sdh155122 		ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
12643448Sdh155122 	else
12653448Sdh155122 		ns = netstack_find_by_zoneid(zoneid);
12663448Sdh155122 	ASSERT(ns != NULL);
12673448Sdh155122 
12683448Sdh155122 	/*
12693448Sdh155122 	 * For exclusive stacks we set the zoneid to zero
12703448Sdh155122 	 * since IP uses the global zoneid in the exclusive stacks.
12713448Sdh155122 	 */
12723448Sdh155122 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
12733448Sdh155122 		zoneid = GLOBAL_ZONEID;
12743448Sdh155122 	ipst = ns->netstack_ip;
12753448Sdh155122 
12762535Ssangeeta 	ASSERT(dst_addr->sa_family == AF_INET ||
12772535Ssangeeta 	    dst_addr->sa_family == AF_INET6);
12782535Ssangeeta 
12792535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
12802535Ssangeeta 		dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr;
12812535Ssangeeta 	} else {
12822535Ssangeeta 		/*
12832535Ssangeeta 		 * We dont have support for V6 yet. It will be provided
12842535Ssangeeta 		 * once RFE  6399103  has been delivered.
12852535Ssangeeta 		 * Until then, for V6 dsts, IP Filter will not call
12863187Sdr146992 		 * this function. Instead the netinfo framework provides
12873187Sdr146992 		 * its own code path, in ip_inject_impl(), to achieve
12883187Sdr146992 		 * what it needs to do, for the time being.
12892535Ssangeeta 		 */
12902535Ssangeeta 		ip1dbg(("ipfil_sendpkt: no V6 support \n"));
12912535Ssangeeta 		value = ECOMM;
12922535Ssangeeta 		freemsg(mp);
12932535Ssangeeta 		goto discard;
12942535Ssangeeta 	}
12952535Ssangeeta 
12962535Ssangeeta 	/*
12972535Ssangeeta 	 * Lets get the ire. We might get the ire cache entry,
12982535Ssangeeta 	 * or the ire,sire pair needed to create the cache entry.
12992535Ssangeeta 	 * XXX pass NULL tsl for now.
13002535Ssangeeta 	 */
13012535Ssangeeta 
13022535Ssangeeta 	if (ifindex == 0) {
13032535Ssangeeta 		/* There is no supplied index. So use the FIB info */
13042535Ssangeeta 
13052535Ssangeeta 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
13062535Ssangeeta 		    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
13072535Ssangeeta 		ire = ire_route_lookup(dst,
13082535Ssangeeta 		    0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp),
13093448Sdh155122 		    match_flags, ipst);
13102535Ssangeeta 	} else {
13112535Ssangeeta 		ipif_t *supplied_ipif;
13122535Ssangeeta 		ill_t *ill;
13132535Ssangeeta 
13144041Snordmark 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
13154041Snordmark 		    MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE|
13164041Snordmark 		    MATCH_IRE_SECATTR);
13174041Snordmark 
13182535Ssangeeta 		/*
13192535Ssangeeta 		 * If supplied ifindex is non-null, the only valid
13204041Snordmark 		 * nexthop is one off of the interface or group corresponding
13212535Ssangeeta 		 * to the specified ifindex.
13222535Ssangeeta 		 */
13232535Ssangeeta 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
13243448Sdh155122 		    NULL, NULL, NULL, NULL, ipst);
13252535Ssangeeta 		if (ill != NULL) {
13264041Snordmark 			match_flags |= MATCH_IRE_ILL;
13272535Ssangeeta 		} else {
13284041Snordmark 			/* Fallback to group names if hook_emulation set */
13294041Snordmark 			if (ipst->ips_ipmp_hook_emulation) {
13304041Snordmark 				ill = ill_group_lookup_on_ifindex(ifindex,
13314041Snordmark 				    B_FALSE, ipst);
13324041Snordmark 			}
13334041Snordmark 			if (ill == NULL) {
13344041Snordmark 				ip1dbg(("ipfil_sendpkt: Could not find"
13354041Snordmark 				    " route to dst\n"));
13364041Snordmark 				value = ECOMM;
13374041Snordmark 				freemsg(mp);
13384041Snordmark 				goto discard;
13394041Snordmark 			}
13404041Snordmark 			match_flags |= MATCH_IRE_ILL_GROUP;
13412535Ssangeeta 		}
13424041Snordmark 		supplied_ipif = ipif_get_next_ipif(NULL, ill);
13432535Ssangeeta 
13442535Ssangeeta 		ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif,
13453448Sdh155122 		    &sire, zoneid, MBLK_GETLABEL(mp), match_flags, ipst);
13462958Sdr146992 		ipif_refrele(supplied_ipif);
13472535Ssangeeta 		ill_refrele(ill);
13482535Ssangeeta 	}
13492535Ssangeeta 
13502535Ssangeeta 	/*
13512535Ssangeeta 	 * Verify that the returned IRE is non-null and does
13522535Ssangeeta 	 * not have either the RTF_REJECT or RTF_BLACKHOLE
13532535Ssangeeta 	 * flags set and that the IRE is  either an IRE_CACHE,
13542535Ssangeeta 	 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
13552535Ssangeeta 	 */
13562535Ssangeeta 	if (ire == NULL ||
13572535Ssangeeta 	    ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
13582535Ssangeeta 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)) {
13592535Ssangeeta 		/*
13602535Ssangeeta 		 * Either ire could not be found or we got
13612535Ssangeeta 		 * an invalid one
13622535Ssangeeta 		 */
13632535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Could not find route to dst\n"));
13642535Ssangeeta 		value = ENONET;
13652535Ssangeeta 		freemsg(mp);
13662535Ssangeeta 		goto discard;
13672535Ssangeeta 	}
13682535Ssangeeta 
13692535Ssangeeta 	/* IP Filter and CGTP dont mix. So bail out if CGTP is on */
13702535Ssangeeta 	if (ip_cgtp_filter &&
13712535Ssangeeta 	    ((ire->ire_flags & RTF_MULTIRT) ||
13722535Ssangeeta 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
13732535Ssangeeta 		ip1dbg(("ipfil_sendpkt: IPFilter does not work with CGTP\n"));
13742535Ssangeeta 		value = ECOMM;
13752535Ssangeeta 		freemsg(mp);
13762535Ssangeeta 		goto discard;
13772535Ssangeeta 	}
13782535Ssangeeta 
13792958Sdr146992 	ASSERT(ire->ire_type != IRE_CACHE || ire->ire_nce != NULL);
1380*4482Sdr146992 
13812535Ssangeeta 	/*
13822535Ssangeeta 	 * If needed, we will create the ire cache entry for the
13832535Ssangeeta 	 * nexthop, resolve its link-layer address and then send
1384*4482Sdr146992 	 * the packet out without ttl or IPSec processing.
13852535Ssangeeta 	 */
13862535Ssangeeta 	switch (ire->ire_type) {
13872535Ssangeeta 	case IRE_IF_NORESOLVER:
13882535Ssangeeta 	case IRE_CACHE:
13892535Ssangeeta 		if (sire != NULL) {
13902535Ssangeeta 			UPDATE_OB_PKT_COUNT(sire);
13912535Ssangeeta 			sire->ire_last_used_time = lbolt;
13922535Ssangeeta 			ire_refrele(sire);
13932535Ssangeeta 		}
13942535Ssangeeta 		ire_cache = ire;
13952535Ssangeeta 		break;
13962535Ssangeeta 	case IRE_IF_RESOLVER:
13972535Ssangeeta 		/*
13982535Ssangeeta 		 * Call ire_forward(). This function
13992535Ssangeeta 		 * will, create the ire cache entry of the
14002535Ssangeeta 		 * the nexthop and adds this incomplete ire
14012535Ssangeeta 		 * to the ire cache table
14022535Ssangeeta 		 */
14032535Ssangeeta 		ire_cache = ire_forward(dst, &check_multirt, ire, sire,
14043448Sdh155122 		    MBLK_GETLABEL(mp), ipst);
14052535Ssangeeta 		if (ire_cache == NULL) {
14062535Ssangeeta 			ip1dbg(("ipfil_sendpkt: failed to create the"
14072535Ssangeeta 			    " ire cache entry \n"));
14082535Ssangeeta 			value = ENONET;
14092535Ssangeeta 			freemsg(mp);
14102535Ssangeeta 			sire = NULL;
14112535Ssangeeta 			ire = NULL;
14122535Ssangeeta 			goto discard;
14132535Ssangeeta 		}
14142535Ssangeeta 		break;
14152535Ssangeeta 	}
1416*4482Sdr146992 
1417*4482Sdr146992 	if (DB_CKSUMFLAGS(mp)) {
1418*4482Sdr146992 		if (ip_send_align_hcksum_flags(mp, ire_to_ill(ire_cache)))
1419*4482Sdr146992 			goto cleanup;
1420*4482Sdr146992 	}
1421*4482Sdr146992 
14222535Ssangeeta 	/*
14232535Ssangeeta 	 * Now that we have the ire cache entry of the nexthop, call
14242535Ssangeeta 	 * ip_xmit_v4() to trigger mac addr resolution
14252535Ssangeeta 	 * if necessary and send it once ready.
14262535Ssangeeta 	 */
14272535Ssangeeta 
14282535Ssangeeta 	value = ip_xmit_v4(mp, ire_cache, NULL, B_FALSE);
1429*4482Sdr146992 cleanup:
14302535Ssangeeta 	ire_refrele(ire_cache);
14312535Ssangeeta 	/*
14322535Ssangeeta 	 * At this point, the reference for these have already been
14332535Ssangeeta 	 * released within ire_forward() and/or ip_xmit_v4(). So we set
14342535Ssangeeta 	 * them to NULL to make sure we dont drop the references
14352535Ssangeeta 	 * again in case ip_xmit_v4() returns with either SEND_FAILED
14362535Ssangeeta 	 * or LLHDR_RESLV_FAILED
14372535Ssangeeta 	 */
14382535Ssangeeta 	sire = NULL;
14392535Ssangeeta 	ire = NULL;
14402535Ssangeeta 
14412535Ssangeeta 	switch (value) {
14422535Ssangeeta 	case SEND_FAILED:
14432535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Send failed\n"));
14442535Ssangeeta 		value = ECOMM;
14452535Ssangeeta 		break;
14462535Ssangeeta 	case LLHDR_RESLV_FAILED:
14472535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Link-layer resolution"
14482535Ssangeeta 		    "  failed\n"));
14492535Ssangeeta 		value = ECOMM;
14502535Ssangeeta 		break;
14512535Ssangeeta 	case LOOKUP_IN_PROGRESS:
14523448Sdh155122 		netstack_rele(ns);
14532535Ssangeeta 		return (EINPROGRESS);
14542535Ssangeeta 	case SEND_PASSED:
14553448Sdh155122 		netstack_rele(ns);
14562535Ssangeeta 		return (0);
14572535Ssangeeta 	}
14582535Ssangeeta discard:
14592535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
14603448Sdh155122 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
14612535Ssangeeta 	} else {
14623448Sdh155122 		BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards);
14632535Ssangeeta 	}
14642535Ssangeeta 	if (ire != NULL)
14652535Ssangeeta 		ire_refrele(ire);
14662535Ssangeeta 	if (sire != NULL)
14672535Ssangeeta 		ire_refrele(sire);
14683448Sdh155122 	netstack_rele(ns);
14692535Ssangeeta 	return (value);
14702535Ssangeeta }
14712535Ssangeeta 
1472*4482Sdr146992 
1473*4482Sdr146992 /*
1474*4482Sdr146992  * We don't check for dohwcksum in here because it should be being used
1475*4482Sdr146992  * elsewhere to control what flags are being set on the mblk.  That is,
1476*4482Sdr146992  * if DB_CKSUMFLAGS() is non-zero then we assume dohwcksum to be true
1477*4482Sdr146992  * for this packet.
1478*4482Sdr146992  *
1479*4482Sdr146992  * This function assumes that it is *only* being called for TCP or UDP
1480*4482Sdr146992  * packets and nothing else.
1481*4482Sdr146992  */
1482*4482Sdr146992 static int
1483*4482Sdr146992 ip_send_align_hcksum_flags(mblk_t *mp, ill_t *ill)
1484*4482Sdr146992 {
1485*4482Sdr146992 	int illhckflags;
1486*4482Sdr146992 	int mbhckflags;
1487*4482Sdr146992 	uint16_t *up;
1488*4482Sdr146992 	uint32_t cksum;
1489*4482Sdr146992 	ipha_t *ipha;
1490*4482Sdr146992 	ip6_t *ip6;
1491*4482Sdr146992 	int proto;
1492*4482Sdr146992 	int ipversion;
1493*4482Sdr146992 	int length;
1494*4482Sdr146992 	int start;
1495*4482Sdr146992 	ip6_pkt_t ipp;
1496*4482Sdr146992 
1497*4482Sdr146992 	mbhckflags = DB_CKSUMFLAGS(mp);
1498*4482Sdr146992 	ASSERT(mbhckflags != 0);
1499*4482Sdr146992 	ASSERT(mp->b_datap->db_type == M_DATA);
1500*4482Sdr146992 	/*
1501*4482Sdr146992 	 * Since this function only knows how to manage the hardware checksum
1502*4482Sdr146992 	 * issue, reject and packets that have flags set on the aside from
1503*4482Sdr146992 	 * checksum related attributes as we cannot necessarily safely map
1504*4482Sdr146992 	 * that packet onto the new NIC.  Packets that can be potentially
1505*4482Sdr146992 	 * dropped here include those marked for LSO.
1506*4482Sdr146992 	 */
1507*4482Sdr146992 	if ((mbhckflags &
1508*4482Sdr146992 	    ~(HCK_FULLCKSUM|HCK_PARTIALCKSUM|HCK_IPV4_HDRCKSUM)) != 0) {
1509*4482Sdr146992 		DTRACE_PROBE2(pbr__incapable, (mblk_t *), mp, (ill_t *), ill);
1510*4482Sdr146992 		freemsg(mp);
1511*4482Sdr146992 		return (-1);
1512*4482Sdr146992 	}
1513*4482Sdr146992 
1514*4482Sdr146992 	ipha = (ipha_t *)mp->b_rptr;
1515*4482Sdr146992 
1516*4482Sdr146992 	/*
1517*4482Sdr146992 	 * Find out what the new NIC is capable of, if anything, and
1518*4482Sdr146992 	 * only allow it to be used with M_DATA mblks being sent out.
1519*4482Sdr146992 	 */
1520*4482Sdr146992 	if (ILL_HCKSUM_CAPABLE(ill)) {
1521*4482Sdr146992 		illhckflags = ill->ill_hcksum_capab->ill_hcksum_txflags;
1522*4482Sdr146992 	} else {
1523*4482Sdr146992 		/*
1524*4482Sdr146992 		 * No capabilities, so turn off everything.
1525*4482Sdr146992 		 */
1526*4482Sdr146992 		illhckflags = 0;
1527*4482Sdr146992 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0, 0, 0);
1528*4482Sdr146992 		mp->b_datap->db_struioflag &= ~STRUIO_IP;
1529*4482Sdr146992 	}
1530*4482Sdr146992 
1531*4482Sdr146992 	DTRACE_PROBE4(pbr__info__a, (mblk_t *), mp, (ill_t *), ill,
1532*4482Sdr146992 	    uint32_t, illhckflags, uint32_t, mbhckflags);
1533*4482Sdr146992 	/*
1534*4482Sdr146992 	 * This block of code that looks for the position of the TCP/UDP
1535*4482Sdr146992 	 * checksum is early in this function because we need to know
1536*4482Sdr146992 	 * what needs to be blanked out for the hardware checksum case.
1537*4482Sdr146992 	 *
1538*4482Sdr146992 	 * That we're in this function implies that the packet is either
1539*4482Sdr146992 	 * TCP or UDP on Solaris, so checks are made for one protocol and
1540*4482Sdr146992 	 * if that fails, the other is therefore implied.
1541*4482Sdr146992 	 */
1542*4482Sdr146992 	ipversion = IPH_HDR_VERSION(ipha);
1543*4482Sdr146992 
1544*4482Sdr146992 	if (ipversion == IPV4_VERSION) {
1545*4482Sdr146992 		proto = ipha->ipha_protocol;
1546*4482Sdr146992 		if (proto == IPPROTO_TCP) {
1547*4482Sdr146992 			up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1548*4482Sdr146992 		} else {
1549*4482Sdr146992 			up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1550*4482Sdr146992 		}
1551*4482Sdr146992 	} else {
1552*4482Sdr146992 		uint8_t lasthdr;
1553*4482Sdr146992 
1554*4482Sdr146992 		/*
1555*4482Sdr146992 		 * Nothing I've seen indicates that IPv6 checksum'ing
1556*4482Sdr146992 		 * precludes the presence of extension headers, so we
1557*4482Sdr146992 		 * can't just look at the next header value in the IPv6
1558*4482Sdr146992 		 * packet header to see if it is TCP/UDP.
1559*4482Sdr146992 		 */
1560*4482Sdr146992 		ip6 = (ip6_t *)ipha;
1561*4482Sdr146992 		(void) memset(&ipp, 0, sizeof (ipp));
1562*4482Sdr146992 		start = ip_find_hdr_v6(mp, ip6, &ipp, &lasthdr);
1563*4482Sdr146992 		proto = lasthdr;
1564*4482Sdr146992 
1565*4482Sdr146992 		if (proto == IPPROTO_TCP) {
1566*4482Sdr146992 			up = IPH_TCPH_CHECKSUMP(ipha, start);
1567*4482Sdr146992 		} else {
1568*4482Sdr146992 			up = IPH_UDPH_CHECKSUMP(ipha, start);
1569*4482Sdr146992 		}
1570*4482Sdr146992 	}
1571*4482Sdr146992 
1572*4482Sdr146992 	/*
1573*4482Sdr146992 	 * The first case here is easiest:
1574*4482Sdr146992 	 * mblk hasn't asked for full checksum, but the card supports it.
1575*4482Sdr146992 	 *
1576*4482Sdr146992 	 * In addition, check for IPv4 header capability.  Note that only
1577*4482Sdr146992 	 * the mblk flag is checked and not ipversion.
1578*4482Sdr146992 	 */
1579*4482Sdr146992 	if ((((illhckflags & HCKSUM_INET_FULL_V4) && (ipversion == 4)) ||
1580*4482Sdr146992 	    (((illhckflags & HCKSUM_INET_FULL_V6) && (ipversion == 6)))) &&
1581*4482Sdr146992 	    ((mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) != 0)) {
1582*4482Sdr146992 		int newflags = HCK_FULLCKSUM;
1583*4482Sdr146992 
1584*4482Sdr146992 		if ((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) {
1585*4482Sdr146992 			if ((illhckflags & HCKSUM_IPHDRCKSUM) != 0) {
1586*4482Sdr146992 				newflags |= HCK_IPV4_HDRCKSUM;
1587*4482Sdr146992 			} else {
1588*4482Sdr146992 				/*
1589*4482Sdr146992 				 * Rather than call a function, just inline
1590*4482Sdr146992 				 * the computation of the basic IPv4 header.
1591*4482Sdr146992 				 */
1592*4482Sdr146992 				cksum = (ipha->ipha_dst >> 16) +
1593*4482Sdr146992 				    (ipha->ipha_dst & 0xFFFF) +
1594*4482Sdr146992 				    (ipha->ipha_src >> 16) +
1595*4482Sdr146992 				    (ipha->ipha_src & 0xFFFF);
1596*4482Sdr146992 				IP_HDR_CKSUM(ipha, cksum,
1597*4482Sdr146992 				    ((uint32_t *)ipha)[0],
1598*4482Sdr146992 				    ((uint16_t *)ipha)[4]);
1599*4482Sdr146992 			}
1600*4482Sdr146992 		}
1601*4482Sdr146992 
1602*4482Sdr146992 		*up = 0;
1603*4482Sdr146992 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0,
1604*4482Sdr146992 		    newflags, 0);
1605*4482Sdr146992 		return (0);
1606*4482Sdr146992 	}
1607*4482Sdr146992 
1608*4482Sdr146992 	DTRACE_PROBE2(pbr__info__b, int, ipversion, int, proto);
1609*4482Sdr146992 
1610*4482Sdr146992 	/*
1611*4482Sdr146992 	 * Start calculating the pseudo checksum over the IP packet header.
1612*4482Sdr146992 	 * Although the final pseudo checksum used by TCP/UDP consists of
1613*4482Sdr146992 	 * more than just the address fields, we can use the result of
1614*4482Sdr146992 	 * adding those together a little bit further down for IPv4.
1615*4482Sdr146992 	 */
1616*4482Sdr146992 	if (ipversion == IPV4_VERSION) {
1617*4482Sdr146992 		cksum = (ipha->ipha_dst >> 16) + (ipha->ipha_dst & 0xFFFF) +
1618*4482Sdr146992 		    (ipha->ipha_src >> 16) + (ipha->ipha_src & 0xFFFF);
1619*4482Sdr146992 		start = IP_SIMPLE_HDR_LENGTH;
1620*4482Sdr146992 		length = ntohs(ipha->ipha_length);
1621*4482Sdr146992 		DTRACE_PROBE3(pbr__info__e, uint32_t, ipha->ipha_src,
1622*4482Sdr146992 		    uint32_t, ipha->ipha_dst, int, cksum);
1623*4482Sdr146992 	} else {
1624*4482Sdr146992 		uint16_t *pseudo;
1625*4482Sdr146992 
1626*4482Sdr146992 		pseudo = (uint16_t *)&ip6->ip6_src;
1627*4482Sdr146992 
1628*4482Sdr146992 		/* calculate pseudo-header checksum */
1629*4482Sdr146992 		cksum = pseudo[0] + pseudo[1] + pseudo[2] + pseudo[3] +
1630*4482Sdr146992 		    pseudo[4] + pseudo[5] + pseudo[6] + pseudo[7] +
1631*4482Sdr146992 		    pseudo[8] + pseudo[9] + pseudo[10] + pseudo[11] +
1632*4482Sdr146992 		    pseudo[12] + pseudo[13] + pseudo[14] + pseudo[15];
1633*4482Sdr146992 
1634*4482Sdr146992 		length = ntohs(ip6->ip6_plen) + sizeof (ip6_t);
1635*4482Sdr146992 	}
1636*4482Sdr146992 
1637*4482Sdr146992 	/* Fold the initial sum */
1638*4482Sdr146992 	cksum = (cksum & 0xffff) + (cksum >> 16);
1639*4482Sdr146992 
1640*4482Sdr146992 	/*
1641*4482Sdr146992 	 * If the packet was asking for an IPv4 header checksum to be
1642*4482Sdr146992 	 * calculated but the interface doesn't support that, fill it in
1643*4482Sdr146992 	 * using our pseudo checksum as a starting point.
1644*4482Sdr146992 	 */
1645*4482Sdr146992 	if (((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) &&
1646*4482Sdr146992 	    ((illhckflags & HCKSUM_IPHDRCKSUM) == 0)) {
1647*4482Sdr146992 		/*
1648*4482Sdr146992 		 * IP_HDR_CKSUM uses the 2rd arg to the macro in a destructive
1649*4482Sdr146992 		 * way so pass in a copy of the checksum calculated thus far.
1650*4482Sdr146992 		 */
1651*4482Sdr146992 		uint32_t ipsum = cksum;
1652*4482Sdr146992 
1653*4482Sdr146992 		DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM;
1654*4482Sdr146992 
1655*4482Sdr146992 		IP_HDR_CKSUM(ipha, ipsum, ((uint32_t *)ipha)[0],
1656*4482Sdr146992 		    ((uint16_t *)ipha)[4]);
1657*4482Sdr146992 	}
1658*4482Sdr146992 
1659*4482Sdr146992 	DTRACE_PROBE3(pbr__info__c, int, start, int, length, int, cksum);
1660*4482Sdr146992 
1661*4482Sdr146992 	if (proto == IPPROTO_TCP) {
1662*4482Sdr146992 		cksum += IP_TCP_CSUM_COMP;
1663*4482Sdr146992 	} else {
1664*4482Sdr146992 		cksum += IP_UDP_CSUM_COMP;
1665*4482Sdr146992 	}
1666*4482Sdr146992 	cksum += htons(length - start);
1667*4482Sdr146992 	cksum = (cksum & 0xffff) + (cksum >> 16);
1668*4482Sdr146992 
1669*4482Sdr146992 	/*
1670*4482Sdr146992 	 * For TCP/UDP, we either want to setup the packet for partial
1671*4482Sdr146992 	 * checksum or we want to do it all ourselves because the NIC
1672*4482Sdr146992 	 * offers no support for either partial or full checksum.
1673*4482Sdr146992 	 */
1674*4482Sdr146992 	if ((illhckflags & HCKSUM_INET_PARTIAL) != 0) {
1675*4482Sdr146992 		/*
1676*4482Sdr146992 		 * The only case we care about here is if the mblk was
1677*4482Sdr146992 		 * previously set for full checksum offload.  If it was
1678*4482Sdr146992 		 * marked for partial (and the NIC does partial), then
1679*4482Sdr146992 		 * we have nothing to do.  Similarly if the packet was
1680*4482Sdr146992 		 * not set for partial or full, we do nothing as this
1681*4482Sdr146992 		 * is cheaper than more work to set something up.
1682*4482Sdr146992 		 */
1683*4482Sdr146992 		if ((mbhckflags & HCK_FULLCKSUM) != 0) {
1684*4482Sdr146992 			uint32_t offset;
1685*4482Sdr146992 
1686*4482Sdr146992 			if (proto == IPPROTO_TCP) {
1687*4482Sdr146992 				offset = TCP_CHECKSUM_OFFSET;
1688*4482Sdr146992 			} else {
1689*4482Sdr146992 				offset = UDP_CHECKSUM_OFFSET;
1690*4482Sdr146992 			}
1691*4482Sdr146992 			*up = cksum;
1692*4482Sdr146992 
1693*4482Sdr146992 			DTRACE_PROBE3(pbr__info__f, int, length - start, int,
1694*4482Sdr146992 			    cksum, int, offset);
1695*4482Sdr146992 
1696*4482Sdr146992 			(void) hcksum_assoc(mp, NULL, NULL, start,
1697*4482Sdr146992 			    start + offset, length, 0,
1698*4482Sdr146992 			    DB_CKSUMFLAGS(mp) | HCK_PARTIALCKSUM, 0);
1699*4482Sdr146992 		}
1700*4482Sdr146992 
1701*4482Sdr146992 	} else if (mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) {
1702*4482Sdr146992 		DB_CKSUMFLAGS(mp) &= ~(HCK_PARTIALCKSUM|HCK_FULLCKSUM);
1703*4482Sdr146992 
1704*4482Sdr146992 		*up = 0;
1705*4482Sdr146992 		*up = IP_CSUM(mp, start, cksum);
1706*4482Sdr146992 	}
1707*4482Sdr146992 
1708*4482Sdr146992 	DTRACE_PROBE4(pbr__info__d, (mblk_t *), mp, (ipha_t *), ipha,
1709*4482Sdr146992 	    (uint16_t *), up, int, cksum);
1710*4482Sdr146992 	return (0);
1711*4482Sdr146992 }
1712*4482Sdr146992 
1713*4482Sdr146992 
17142535Ssangeeta /* ire_walk routine invoked for ip_ire_report for each IRE. */
17152535Ssangeeta void
17162535Ssangeeta ire_report_ftable(ire_t *ire, char *m)
17172535Ssangeeta {
17182535Ssangeeta 	char	buf1[16];
17192535Ssangeeta 	char	buf2[16];
17202535Ssangeeta 	char	buf3[16];
17212535Ssangeeta 	char	buf4[16];
17222535Ssangeeta 	uint_t	fo_pkt_count;
17232535Ssangeeta 	uint_t	ib_pkt_count;
17242535Ssangeeta 	int	ref;
17252535Ssangeeta 	uint_t	print_len, buf_len;
17262535Ssangeeta 	mblk_t 	*mp = (mblk_t *)m;
17272535Ssangeeta 
17282535Ssangeeta 	if (ire->ire_type & IRE_CACHETABLE)
17292535Ssangeeta 		return;
17302535Ssangeeta 	buf_len = mp->b_datap->db_lim - mp->b_wptr;
17312535Ssangeeta 	if (buf_len <= 0)
17322535Ssangeeta 		return;
17332535Ssangeeta 
17342535Ssangeeta 	/* Number of active references of this ire */
17352535Ssangeeta 	ref = ire->ire_refcnt;
17362535Ssangeeta 	/* "inbound" to a non local address is a forward */
17372535Ssangeeta 	ib_pkt_count = ire->ire_ib_pkt_count;
17382535Ssangeeta 	fo_pkt_count = 0;
17392535Ssangeeta 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
17402535Ssangeeta 		fo_pkt_count = ib_pkt_count;
17412535Ssangeeta 		ib_pkt_count = 0;
17422535Ssangeeta 	}
17432535Ssangeeta 	print_len = snprintf((char *)mp->b_wptr, buf_len,
17442535Ssangeeta 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
17452535Ssangeeta 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
17462535Ssangeeta 	    "%04d %08d %08d %d/%d/%d %s\n",
17472535Ssangeeta 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
17482535Ssangeeta 	    (int)ire->ire_zoneid,
17492535Ssangeeta 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
17502535Ssangeeta 	    ip_dot_addr(ire->ire_src_addr, buf3),
17512535Ssangeeta 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
17522535Ssangeeta 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
17532535Ssangeeta 	    ire->ire_uinfo.iulp_rtt_sd,
17542535Ssangeeta 	    ire->ire_uinfo.iulp_ssthresh, ref,
17552535Ssangeeta 	    ire->ire_uinfo.iulp_rtomax,
17562535Ssangeeta 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
17572535Ssangeeta 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
17582535Ssangeeta 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
17592535Ssangeeta 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
17602535Ssangeeta 	    ire->ire_uinfo.iulp_sack,
17612535Ssangeeta 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
17622535Ssangeeta 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
17632535Ssangeeta 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
17642535Ssangeeta 	if (print_len < buf_len) {
17652535Ssangeeta 		mp->b_wptr += print_len;
17662535Ssangeeta 	} else {
17672535Ssangeeta 		mp->b_wptr += buf_len;
17682535Ssangeeta 	}
17692535Ssangeeta }
17702535Ssangeeta 
17712535Ssangeeta /*
17722535Ssangeeta  * callback function provided by ire_ftable_lookup when calling
17732535Ssangeeta  * rn_match_args(). Invoke ire_match_args on each matching leaf node in
17742535Ssangeeta  * the radix tree.
17752535Ssangeeta  */
17762535Ssangeeta boolean_t
17772535Ssangeeta ire_find_best_route(struct radix_node *rn, void *arg)
17782535Ssangeeta {
17792535Ssangeeta 	struct rt_entry *rt = (struct rt_entry *)rn;
17802535Ssangeeta 	irb_t *irb_ptr;
17812535Ssangeeta 	ire_t *ire;
17822535Ssangeeta 	ire_ftable_args_t *margs = arg;
17832535Ssangeeta 	ipaddr_t match_mask;
17842535Ssangeeta 
17852535Ssangeeta 	ASSERT(rt != NULL);
17862535Ssangeeta 
17872535Ssangeeta 	irb_ptr = &rt->rt_irb;
17882535Ssangeeta 
17892535Ssangeeta 	if (irb_ptr->irb_ire_cnt == 0)
17902535Ssangeeta 		return (B_FALSE);
17912535Ssangeeta 
17922535Ssangeeta 	rw_enter(&irb_ptr->irb_lock, RW_READER);
17932535Ssangeeta 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
17942535Ssangeeta 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
17952535Ssangeeta 			continue;
17962535Ssangeeta 		if (margs->ift_flags & MATCH_IRE_MASK)
17972535Ssangeeta 			match_mask = margs->ift_mask;
17982535Ssangeeta 		else
17992535Ssangeeta 			match_mask = ire->ire_mask;
18002535Ssangeeta 
18012535Ssangeeta 		if (ire_match_args(ire, margs->ift_addr, match_mask,
18022535Ssangeeta 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
18032535Ssangeeta 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
18042535Ssangeeta 		    margs->ift_flags)) {
18052535Ssangeeta 			IRE_REFHOLD(ire);
18062535Ssangeeta 			rw_exit(&irb_ptr->irb_lock);
18072535Ssangeeta 			margs->ift_best_ire = ire;
18082535Ssangeeta 			return (B_TRUE);
18092535Ssangeeta 		}
18102535Ssangeeta 	}
18112535Ssangeeta 	rw_exit(&irb_ptr->irb_lock);
18122535Ssangeeta 	return (B_FALSE);
18132535Ssangeeta }
18142535Ssangeeta 
18152535Ssangeeta /*
18162535Ssangeeta  * ftable irb_t structures are dynamically allocated, and we need to
18172535Ssangeeta  * check if the irb_t (and associated ftable tree attachment) needs to
18182535Ssangeeta  * be cleaned up when the irb_refcnt goes to 0. The conditions that need
18192535Ssangeeta  * be verified are:
18202535Ssangeeta  * - no other walkers of the irebucket, i.e., quiescent irb_refcnt,
18212535Ssangeeta  * - no other threads holding references to ire's in the bucket,
18222535Ssangeeta  *   i.e., irb_nire == 0
18232535Ssangeeta  * - no active ire's in the bucket, i.e., irb_ire_cnt == 0
18242535Ssangeeta  * - need to hold the global tree lock and irb_lock in write mode.
18252535Ssangeeta  */
18262535Ssangeeta void
18272535Ssangeeta irb_refrele_ftable(irb_t *irb)
18282535Ssangeeta {
18292535Ssangeeta 	for (;;) {
18302535Ssangeeta 		rw_enter(&irb->irb_lock, RW_WRITER);
18312535Ssangeeta 		ASSERT(irb->irb_refcnt != 0);
18322535Ssangeeta 		if (irb->irb_refcnt != 1) {
18332535Ssangeeta 			/*
18342535Ssangeeta 			 * Someone has a reference to this radix node
18352535Ssangeeta 			 * or there is some bucket walker.
18362535Ssangeeta 			 */
18372535Ssangeeta 			irb->irb_refcnt--;
18382535Ssangeeta 			rw_exit(&irb->irb_lock);
18392535Ssangeeta 			return;
18402535Ssangeeta 		} else {
18412535Ssangeeta 			/*
18422535Ssangeeta 			 * There is no other walker, nor is there any
18432535Ssangeeta 			 * other thread that holds a direct ref to this
18442535Ssangeeta 			 * radix node. Do the clean up if needed. Call
18452535Ssangeeta 			 * to ire_unlink will clear the IRB_MARK_CONDEMNED flag
18462535Ssangeeta 			 */
18472535Ssangeeta 			if (irb->irb_marks & IRB_MARK_CONDEMNED)  {
18482535Ssangeeta 				ire_t *ire_list;
18492535Ssangeeta 
18502535Ssangeeta 				ire_list = ire_unlink(irb);
18512535Ssangeeta 				rw_exit(&irb->irb_lock);
18522535Ssangeeta 
18532535Ssangeeta 				if (ire_list != NULL)
18542535Ssangeeta 					ire_cleanup(ire_list);
18552535Ssangeeta 				/*
18562535Ssangeeta 				 * more CONDEMNED entries could have
18572535Ssangeeta 				 * been added while we dropped the lock,
18582535Ssangeeta 				 * so we have to re-check.
18592535Ssangeeta 				 */
18602535Ssangeeta 				continue;
18612535Ssangeeta 			}
18622535Ssangeeta 
18632535Ssangeeta 			/*
18642535Ssangeeta 			 * Now check if there are still any ires
18652535Ssangeeta 			 * associated with this radix node.
18662535Ssangeeta 			 */
18672535Ssangeeta 			if (irb->irb_nire != 0) {
18682535Ssangeeta 				/*
18692535Ssangeeta 				 * someone is still holding on
18702535Ssangeeta 				 * to ires in this bucket
18712535Ssangeeta 				 */
18722535Ssangeeta 				irb->irb_refcnt--;
18732535Ssangeeta 				rw_exit(&irb->irb_lock);
18742535Ssangeeta 				return;
18752535Ssangeeta 			} else {
18762535Ssangeeta 				/*
18772535Ssangeeta 				 * Everything is clear. Zero walkers,
18782535Ssangeeta 				 * Zero threads with a ref to this
18792535Ssangeeta 				 * radix node, Zero ires associated with
18802535Ssangeeta 				 * this radix node. Due to lock order,
18812535Ssangeeta 				 * check the above conditions again
18822535Ssangeeta 				 * after grabbing all locks in the right order
18832535Ssangeeta 				 */
18842535Ssangeeta 				rw_exit(&irb->irb_lock);
18852535Ssangeeta 				if (irb_inactive(irb))
18862535Ssangeeta 					return;
18872535Ssangeeta 				/*
18882535Ssangeeta 				 * irb_inactive could not free the irb.
18892535Ssangeeta 				 * See if there are any walkers, if not
18902535Ssangeeta 				 * try to clean up again.
18912535Ssangeeta 				 */
18922535Ssangeeta 			}
18932535Ssangeeta 		}
18942535Ssangeeta 	}
18952535Ssangeeta }
18962535Ssangeeta 
18972535Ssangeeta /*
18982535Ssangeeta  * IRE iterator used by ire_ftable_lookup() to process multiple default
18992535Ssangeeta  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
19002535Ssangeeta  * in the bucket skipping default interface routes and deleted entries.
19012535Ssangeeta  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
19022535Ssangeeta  * Assumes that the caller holds a reference on the IRE bucket.
19032535Ssangeeta  *
19042535Ssangeeta  * In the absence of good IRE_DEFAULT routes, this function will return
19052535Ssangeeta  * the first IRE_INTERFACE route found (if any).
19062535Ssangeeta  */
19072535Ssangeeta ire_t *
19083448Sdh155122 ire_round_robin(irb_t *irb_ptr, zoneid_t zoneid, ire_ftable_args_t *margs,
19093448Sdh155122 	ip_stack_t *ipst)
19102535Ssangeeta {
19112535Ssangeeta 	ire_t	*ire_origin;
19122535Ssangeeta 	ire_t	*ire, *maybe_ire = NULL;
19132535Ssangeeta 
19142535Ssangeeta 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
19152535Ssangeeta 	ire_origin = irb_ptr->irb_rr_origin;
19162894Ssowmini 	if (ire_origin != NULL) {
19172535Ssangeeta 		ire_origin = ire_origin->ire_next;
19182894Ssowmini 		IRE_FIND_NEXT_ORIGIN(ire_origin);
19192894Ssowmini 	}
19202535Ssangeeta 
19212535Ssangeeta 	if (ire_origin == NULL) {
19222535Ssangeeta 		/*
19232535Ssangeeta 		 * first time through routine, or we dropped off the end
19242535Ssangeeta 		 * of list.
19252535Ssangeeta 		 */
19262535Ssangeeta 		ire_origin = irb_ptr->irb_ire;
19272894Ssowmini 		IRE_FIND_NEXT_ORIGIN(ire_origin);
19282535Ssangeeta 	}
19292535Ssangeeta 	irb_ptr->irb_rr_origin = ire_origin;
19302535Ssangeeta 	IRB_REFHOLD_LOCKED(irb_ptr);
19312535Ssangeeta 	rw_exit(&irb_ptr->irb_lock);
19322535Ssangeeta 
19332894Ssowmini 	DTRACE_PROBE2(ire__rr__origin, (irb_t *), irb_ptr,
19342894Ssowmini 	    (ire_t *), ire_origin);
19352894Ssowmini 
19362535Ssangeeta 	/*
19372535Ssangeeta 	 * Round-robin the routers list looking for a route that
19382535Ssangeeta 	 * matches the passed in parameters.
19392535Ssangeeta 	 * We start with the ire we found above and we walk the hash
19402535Ssangeeta 	 * list until we're back where we started. It doesn't matter if
19412535Ssangeeta 	 * routes are added or deleted by other threads - we know this
19422535Ssangeeta 	 * ire will stay in the list because we hold a reference on the
19432535Ssangeeta 	 * ire bucket.
19442535Ssangeeta 	 */
19452535Ssangeeta 	ire = ire_origin;
19462535Ssangeeta 	while (ire != NULL) {
19474365Snordmark 		int match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
19482535Ssangeeta 		ire_t *rire;
19492535Ssangeeta 
19502535Ssangeeta 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
19512535Ssangeeta 			goto next_ire;
19522535Ssangeeta 
19532535Ssangeeta 		if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0,
19542535Ssangeeta 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
19552535Ssangeeta 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
19562535Ssangeeta 		    margs->ift_flags))
19572535Ssangeeta 			goto next_ire;
19582535Ssangeeta 
19592535Ssangeeta 		if (ire->ire_type & IRE_INTERFACE) {
19602535Ssangeeta 			/*
19612535Ssangeeta 			 * keep looking to see if there is a non-interface
19622535Ssangeeta 			 * default ire, but save this one as a last resort.
19632535Ssangeeta 			 */
19642535Ssangeeta 			if (maybe_ire == NULL)
19652535Ssangeeta 				maybe_ire = ire;
19662535Ssangeeta 			goto next_ire;
19672535Ssangeeta 		}
19682535Ssangeeta 
19692535Ssangeeta 		if (zoneid == ALL_ZONES) {
19702535Ssangeeta 			IRE_REFHOLD(ire);
19712535Ssangeeta 			IRB_REFRELE(irb_ptr);
19722535Ssangeeta 			return (ire);
19732535Ssangeeta 		}
19742535Ssangeeta 		/*
19754365Snordmark 		 * When we're in a non-global zone, we're only
19762535Ssangeeta 		 * interested in routers that are
19772535Ssangeeta 		 * reachable through ipifs within our zone.
19782535Ssangeeta 		 */
19792535Ssangeeta 		if (ire->ire_ipif != NULL) {
19802535Ssangeeta 			match_flags |= MATCH_IRE_ILL_GROUP;
19812535Ssangeeta 		}
19824365Snordmark 		rire = ire_route_lookup(ire->ire_gateway_addr, 0, 0,
19834365Snordmark 		    IRE_INTERFACE, ire->ire_ipif, NULL, zoneid, margs->ift_tsl,
19843448Sdh155122 		    match_flags, ipst);
19852535Ssangeeta 		if (rire != NULL) {
19862535Ssangeeta 			ire_refrele(rire);
19872535Ssangeeta 			IRE_REFHOLD(ire);
19882535Ssangeeta 			IRB_REFRELE(irb_ptr);
19892535Ssangeeta 			return (ire);
19902535Ssangeeta 		}
19912535Ssangeeta next_ire:
19922535Ssangeeta 		ire = (ire->ire_next ?  ire->ire_next : irb_ptr->irb_ire);
19932535Ssangeeta 		if (ire == ire_origin)
19942535Ssangeeta 			break;
19952535Ssangeeta 	}
19962535Ssangeeta 	if (maybe_ire != NULL)
19972535Ssangeeta 		IRE_REFHOLD(maybe_ire);
19982535Ssangeeta 	IRB_REFRELE(irb_ptr);
19992535Ssangeeta 	return (maybe_ire);
20002535Ssangeeta }
20012783Ssowmini 
20022783Ssowmini void
20032783Ssowmini irb_refhold_rn(struct radix_node *rn)
20042783Ssowmini {
20052783Ssowmini 	if ((rn->rn_flags & RNF_ROOT) == 0)
20062783Ssowmini 		IRB_REFHOLD(&((rt_t *)(rn))->rt_irb);
20072783Ssowmini }
20082783Ssowmini 
20092783Ssowmini void
20102783Ssowmini irb_refrele_rn(struct radix_node *rn)
20112783Ssowmini {
20122783Ssowmini 	if ((rn->rn_flags & RNF_ROOT) == 0)
20132783Ssowmini 		irb_refrele_ftable(&((rt_t *)(rn))->rt_irb);
20142783Ssowmini }
2015