xref: /onnv-gate/usr/src/uts/common/inet/ip/ip_ftable.c (revision 2894:464fc76e8ce1)
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 /*
222535Ssangeeta  * Copyright 2006 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>
462535Ssangeeta #include <net/if.h>
472535Ssangeeta #include <net/route.h>
482535Ssangeeta #include <netinet/in.h>
492535Ssangeeta #include <net/if_dl.h>
502535Ssangeeta #include <netinet/ip6.h>
512535Ssangeeta #include <netinet/icmp6.h>
522535Ssangeeta 
532535Ssangeeta #include <inet/common.h>
542535Ssangeeta #include <inet/mi.h>
552535Ssangeeta #include <inet/mib2.h>
562535Ssangeeta #include <inet/ip.h>
572535Ssangeeta #include <inet/ip6.h>
582535Ssangeeta #include <inet/ip_ndp.h>
592535Ssangeeta #include <inet/arp.h>
602535Ssangeeta #include <inet/ip_if.h>
612535Ssangeeta #include <inet/ip_ire.h>
622535Ssangeeta #include <inet/ip_ftable.h>
632535Ssangeeta #include <inet/ip_rts.h>
642535Ssangeeta #include <inet/nd.h>
652535Ssangeeta 
662535Ssangeeta #include <net/pfkeyv2.h>
672535Ssangeeta #include <inet/ipsec_info.h>
682535Ssangeeta #include <inet/sadb.h>
692535Ssangeeta #include <sys/kmem.h>
702535Ssangeeta #include <inet/tcp.h>
712535Ssangeeta #include <inet/ipclassifier.h>
722535Ssangeeta #include <sys/zone.h>
732535Ssangeeta #include <net/radix.h>
742535Ssangeeta #include <sys/tsol/label.h>
752535Ssangeeta #include <sys/tsol/tnet.h>
762535Ssangeeta 
772535Ssangeeta #define	IS_DEFAULT_ROUTE(ire)	\
782535Ssangeeta 	(((ire)->ire_type & IRE_DEFAULT) || \
792535Ssangeeta 	    (((ire)->ire_type & IRE_INTERFACE) && ((ire)->ire_addr == 0)))
802535Ssangeeta 
812535Ssangeeta /*
822535Ssangeeta  * structure for passing args between ire_ftable_lookup and ire_find_best_route
832535Ssangeeta  */
842535Ssangeeta typedef struct ire_ftable_args_s {
852535Ssangeeta 	ipaddr_t	ift_addr;
862535Ssangeeta 	ipaddr_t	ift_mask;
872535Ssangeeta 	ipaddr_t	ift_gateway;
882535Ssangeeta 	int		ift_type;
892535Ssangeeta 	const ipif_t		*ift_ipif;
902535Ssangeeta 	zoneid_t	ift_zoneid;
912535Ssangeeta 	uint32_t	ift_ihandle;
922535Ssangeeta 	const ts_label_t	*ift_tsl;
932535Ssangeeta 	int		ift_flags;
942535Ssangeeta 	ire_t		*ift_best_ire;
952535Ssangeeta } ire_ftable_args_t;
962535Ssangeeta 
972535Ssangeeta struct	radix_node_head	*ip_ftable;
982535Ssangeeta static ire_t	*route_to_dst(const struct sockaddr *, zoneid_t);
992535Ssangeeta static ire_t   	*ire_round_robin(irb_t *, zoneid_t, ire_ftable_args_t *);
1002535Ssangeeta static void		ire_del_host_redir(ire_t *, char *);
1012535Ssangeeta static boolean_t	ire_find_best_route(struct radix_node *, void *);
1022535Ssangeeta 
1032535Ssangeeta /*
1042535Ssangeeta  * Lookup a route in forwarding table. A specific lookup is indicated by
1052535Ssangeeta  * passing the required parameters and indicating the match required in the
1062535Ssangeeta  * flag field.
1072535Ssangeeta  *
1082535Ssangeeta  * Looking for default route can be done in three ways
1092535Ssangeeta  * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
1102535Ssangeeta  *    along with other matches.
1112535Ssangeeta  * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
1122535Ssangeeta  *    field along with other matches.
1132535Ssangeeta  * 3) if the destination and mask are passed as zeros.
1142535Ssangeeta  *
1152535Ssangeeta  * A request to return a default route if no route
1162535Ssangeeta  * is found, can be specified by setting MATCH_IRE_DEFAULT
1172535Ssangeeta  * in flags.
1182535Ssangeeta  *
1192535Ssangeeta  * It does not support recursion more than one level. It
1202535Ssangeeta  * will do recursive lookup only when the lookup maps to
1212535Ssangeeta  * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
1222535Ssangeeta  *
1232535Ssangeeta  * If the routing table is setup to allow more than one level
1242535Ssangeeta  * of recursion, the cleaning up cache table will not work resulting
1252535Ssangeeta  * in invalid routing.
1262535Ssangeeta  *
1272535Ssangeeta  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
1282535Ssangeeta  *
1292535Ssangeeta  * NOTE : When this function returns NULL, pire has already been released.
1302535Ssangeeta  *	  pire is valid only when this function successfully returns an
1312535Ssangeeta  *	  ire.
1322535Ssangeeta  */
1332535Ssangeeta ire_t *
1342535Ssangeeta ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1352535Ssangeeta     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
1362535Ssangeeta     uint32_t ihandle, const ts_label_t *tsl, int flags)
1372535Ssangeeta {
1382535Ssangeeta 	ire_t *ire = NULL;
1392535Ssangeeta 	ipaddr_t gw_addr;
1402535Ssangeeta 	struct rt_sockaddr rdst, rmask;
1412535Ssangeeta 	struct rt_entry *rt;
1422535Ssangeeta 	ire_ftable_args_t margs;
1432535Ssangeeta 	boolean_t found_incomplete = B_FALSE;
1442535Ssangeeta 
1452535Ssangeeta 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
1462535Ssangeeta 	ASSERT(!(flags & MATCH_IRE_WQ));
1472535Ssangeeta 
1482535Ssangeeta 	/*
1492535Ssangeeta 	 * When we return NULL from this function, we should make
1502535Ssangeeta 	 * sure that *pire is NULL so that the callers will not
1512535Ssangeeta 	 * wrongly REFRELE the pire.
1522535Ssangeeta 	 */
1532535Ssangeeta 	if (pire != NULL)
1542535Ssangeeta 		*pire = NULL;
1552535Ssangeeta 	/*
1562535Ssangeeta 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
1572535Ssangeeta 	 * MATCH_IRE_ILL is set.
1582535Ssangeeta 	 */
1592535Ssangeeta 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
1602535Ssangeeta 	    (ipif == NULL))
1612535Ssangeeta 		return (NULL);
1622535Ssangeeta 
1632535Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
1642535Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
1652535Ssangeeta 	rdst.rt_sin_family = AF_INET;
1662535Ssangeeta 	rdst.rt_sin_addr.s_addr = addr;
1672535Ssangeeta 
1682535Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
1692535Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
1702535Ssangeeta 	rmask.rt_sin_family = AF_INET;
1712535Ssangeeta 	rmask.rt_sin_addr.s_addr = mask;
1722535Ssangeeta 
1732535Ssangeeta 	(void) memset(&margs, 0, sizeof (margs));
1742535Ssangeeta 	margs.ift_addr = addr;
1752535Ssangeeta 	margs.ift_mask = mask;
1762535Ssangeeta 	margs.ift_gateway = gateway;
1772535Ssangeeta 	margs.ift_type = type;
1782535Ssangeeta 	margs.ift_ipif = ipif;
1792535Ssangeeta 	margs.ift_zoneid = zoneid;
1802535Ssangeeta 	margs.ift_ihandle = ihandle;
1812535Ssangeeta 	margs.ift_tsl = tsl;
1822535Ssangeeta 	margs.ift_flags = flags;
1832535Ssangeeta 
1842535Ssangeeta 	/*
1852535Ssangeeta 	 * The flags argument passed to ire_ftable_lookup may cause the
1862535Ssangeeta 	 * search to return, not the longest matching prefix, but the
1872535Ssangeeta 	 * "best matching prefix", i.e., the longest prefix that also
1882535Ssangeeta 	 * satisfies constraints imposed via the permutation of flags
1892535Ssangeeta 	 * passed in. To achieve this, we invoke ire_match_args() on
1902535Ssangeeta 	 * each matching leaf in the  radix tree. ire_match_args is
1912535Ssangeeta 	 * invoked by the callback function ire_find_best_route()
1922535Ssangeeta 	 * We hold the global tree lock in read mode when calling
1932535Ssangeeta 	 * rn_match_args.Before dropping the global tree lock, ensure
1942535Ssangeeta 	 * that the radix node can't be deleted by incrementing ire_refcnt.
1952535Ssangeeta 	 */
1962535Ssangeeta 	RADIX_NODE_HEAD_RLOCK(ip_ftable);
1972535Ssangeeta 	rt = (struct rt_entry *)ip_ftable->rnh_matchaddr_args(&rdst, ip_ftable,
1982535Ssangeeta 	    ire_find_best_route, &margs);
1992535Ssangeeta 	ire = margs.ift_best_ire;
2002535Ssangeeta 	RADIX_NODE_HEAD_UNLOCK(ip_ftable);
2012535Ssangeeta 
2022535Ssangeeta 	if (rt == NULL) {
2032535Ssangeeta 		return (NULL);
2042535Ssangeeta 	} else {
2052535Ssangeeta 		ASSERT(ire != NULL);
2062535Ssangeeta 	}
2072535Ssangeeta 
2082535Ssangeeta 	DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire);
2092535Ssangeeta 
2102535Ssangeeta 	if (!IS_DEFAULT_ROUTE(ire))
2112535Ssangeeta 		goto found_ire_held;
2122535Ssangeeta 	/*
2132535Ssangeeta 	 * If default route is found, see if default matching criteria
2142535Ssangeeta 	 * are satisfied.
2152535Ssangeeta 	 */
2162535Ssangeeta 	if (flags & MATCH_IRE_MASK) {
2172535Ssangeeta 		/*
2182535Ssangeeta 		 * we were asked to match a 0 mask, and came back with
2192535Ssangeeta 		 * a default route. Ok to return it.
2202535Ssangeeta 		 */
2212535Ssangeeta 		goto found_default_ire;
2222535Ssangeeta 	}
2232535Ssangeeta 	if ((flags & MATCH_IRE_TYPE) &&
2242535Ssangeeta 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
2252535Ssangeeta 		/*
2262535Ssangeeta 		 * we were asked to match a default ire type. Ok to return it.
2272535Ssangeeta 		 */
2282535Ssangeeta 		goto found_default_ire;
2292535Ssangeeta 	}
2302535Ssangeeta 	if (flags & MATCH_IRE_DEFAULT) {
2312535Ssangeeta 		goto found_default_ire;
2322535Ssangeeta 	}
2332535Ssangeeta 	/*
2342535Ssangeeta 	 * we found a default route, but default matching criteria
2352535Ssangeeta 	 * are not specified and we are not explicitly looking for
2362535Ssangeeta 	 * default.
2372535Ssangeeta 	 */
2382535Ssangeeta 	IRE_REFRELE(ire);
2392535Ssangeeta 	return (NULL);
2402535Ssangeeta found_default_ire:
2412535Ssangeeta 	/*
2422535Ssangeeta 	 * round-robin only if we have more than one route in the bucket.
2432535Ssangeeta 	 */
2442535Ssangeeta 	if ((ire->ire_bucket->irb_ire_cnt > 1) &&
2452535Ssangeeta 	    IS_DEFAULT_ROUTE(ire) &&
2462535Ssangeeta 	    ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
2472535Ssangeeta 	    MATCH_IRE_DEFAULT)) {
2482535Ssangeeta 		ire_t *next_ire;
2492535Ssangeeta 
2502535Ssangeeta 		next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs);
2512535Ssangeeta 		IRE_REFRELE(ire);
2522535Ssangeeta 		if (next_ire != NULL) {
2532535Ssangeeta 			ire = next_ire;
2542535Ssangeeta 		} else {
2552535Ssangeeta 			/* no route */
2562535Ssangeeta 			return (NULL);
2572535Ssangeeta 		}
2582535Ssangeeta 	}
2592535Ssangeeta found_ire_held:
2602535Ssangeeta 	ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
2612535Ssangeeta 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
2622535Ssangeeta 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
2632535Ssangeeta 		return (ire);
2642535Ssangeeta 	}
2652535Ssangeeta 	/*
2662535Ssangeeta 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
2672535Ssangeeta 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
2682535Ssangeeta 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
2692535Ssangeeta 	 * IRE (one of the prefix types), then it is necessary to fill in the
2702535Ssangeeta 	 * parent IRE pointed to by pire, and then lookup the gateway address of
2712535Ssangeeta 	 * the parent.  For backwards compatiblity, if this lookup returns an
2722535Ssangeeta 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
2732535Ssangeeta 	 * of lookup is done.
2742535Ssangeeta 	 */
2752535Ssangeeta 	if (flags & MATCH_IRE_RECURSIVE) {
2762535Ssangeeta 		ipif_t	*gw_ipif;
2772535Ssangeeta 		int match_flags = MATCH_IRE_DSTONLY;
2782535Ssangeeta 		ire_t *save_ire;
2792535Ssangeeta 
2802535Ssangeeta 		if (ire->ire_type & IRE_INTERFACE)
2812535Ssangeeta 			return (ire);
2822535Ssangeeta 		if (pire != NULL)
2832535Ssangeeta 			*pire = ire;
2842535Ssangeeta 		/*
2852535Ssangeeta 		 * If we can't find an IRE_INTERFACE or the caller has not
2862535Ssangeeta 		 * asked for pire, we need to REFRELE the save_ire.
2872535Ssangeeta 		 */
2882535Ssangeeta 		save_ire = ire;
2892535Ssangeeta 
2902535Ssangeeta 		/*
2912535Ssangeeta 		 * Currently MATCH_IRE_ILL is never used with
2922535Ssangeeta 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
2932535Ssangeeta 		 * sending out packets as MATCH_IRE_ILL is used only
2942535Ssangeeta 		 * for communicating with on-link hosts. We can't assert
2952535Ssangeeta 		 * that here as RTM_GET calls this function with
2962535Ssangeeta 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
2972535Ssangeeta 		 * We have already used the MATCH_IRE_ILL in determining
2982535Ssangeeta 		 * the right prefix route at this point. To match the
2992535Ssangeeta 		 * behavior of how we locate routes while sending out
3002535Ssangeeta 		 * packets, we don't want to use MATCH_IRE_ILL below
3012535Ssangeeta 		 * while locating the interface route.
3022535Ssangeeta 		 *
3032535Ssangeeta 		 * ire_ftable_lookup may end up with an incomplete IRE_CACHE
3042535Ssangeeta 		 * entry for the gateway (i.e., one for which the
3052535Ssangeeta 		 * ire_nce->nce_state is not yet ND_REACHABLE). If the caller
3062535Ssangeeta 		 * has specified MATCH_IRE_COMPLETE, such entries will not
3072535Ssangeeta 		 * be returned; instead, we return the IF_RESOLVER ire.
3082535Ssangeeta 		 */
3092535Ssangeeta 		if (ire->ire_ipif != NULL)
3102535Ssangeeta 			match_flags |= MATCH_IRE_ILL_GROUP;
3112535Ssangeeta 
3122535Ssangeeta 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
3132535Ssangeeta 		    ire->ire_ipif, NULL, zoneid, tsl, match_flags);
3142535Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire,
3152535Ssangeeta 		    (ire_t *), save_ire);
3162535Ssangeeta 		if (ire == NULL ||
3172535Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
3182535Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
3192535Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
3202535Ssangeeta 			/*
3212535Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
3222535Ssangeeta 			 * is set. Also return it via ire.
3232535Ssangeeta 			 */
3242535Ssangeeta 			if (ire != NULL) {
3252535Ssangeeta 				ire_refrele(ire);
3262535Ssangeeta 				ire = NULL;
3272535Ssangeeta 				found_incomplete = B_TRUE;
3282535Ssangeeta 			}
3292535Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
3302535Ssangeeta 				if (pire != NULL) {
3312535Ssangeeta 					/*
3322535Ssangeeta 					 * Need an extra REFHOLD, if the parent
3332535Ssangeeta 					 * ire is returned via both ire and
3342535Ssangeeta 					 * pire.
3352535Ssangeeta 					 */
3362535Ssangeeta 					IRE_REFHOLD(save_ire);
3372535Ssangeeta 				}
3382535Ssangeeta 				ire = save_ire;
3392535Ssangeeta 			} else {
3402535Ssangeeta 				ire_refrele(save_ire);
3412535Ssangeeta 				if (pire != NULL)
3422535Ssangeeta 					*pire = NULL;
3432535Ssangeeta 			}
3442535Ssangeeta 			if (!found_incomplete)
3452535Ssangeeta 				return (ire);
3462535Ssangeeta 		}
3472535Ssangeeta 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
3482535Ssangeeta 			/*
3492535Ssangeeta 			 * If the caller did not ask for pire, release
3502535Ssangeeta 			 * it now.
3512535Ssangeeta 			 */
3522535Ssangeeta 			if (pire == NULL) {
3532535Ssangeeta 				ire_refrele(save_ire);
3542535Ssangeeta 			}
3552535Ssangeeta 			return (ire);
3562535Ssangeeta 		}
3572535Ssangeeta 		match_flags |= MATCH_IRE_TYPE;
3582535Ssangeeta 		gw_addr = ire->ire_gateway_addr;
3592535Ssangeeta 		gw_ipif = ire->ire_ipif;
3602535Ssangeeta 		ire_refrele(ire);
3612535Ssangeeta 		ire = ire_route_lookup(gw_addr, 0, 0,
3622535Ssangeeta 		    (found_incomplete? IRE_INTERFACE :
3632535Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)),
3642535Ssangeeta 		    gw_ipif, NULL, zoneid, tsl, match_flags);
3652535Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire,
3662535Ssangeeta 		    (ire_t *), save_ire);
3672535Ssangeeta 		if (ire == NULL ||
3682535Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
3692535Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
3702535Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
3712535Ssangeeta 			/*
3722535Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
3732535Ssangeeta 			 * is set. Also return it via ire.
3742535Ssangeeta 			 */
3752535Ssangeeta 			if (ire != NULL) {
3762535Ssangeeta 				ire_refrele(ire);
3772535Ssangeeta 				ire = NULL;
3782535Ssangeeta 			}
3792535Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
3802535Ssangeeta 				if (pire != NULL) {
3812535Ssangeeta 					/*
3822535Ssangeeta 					 * Need an extra REFHOLD, if the
3832535Ssangeeta 					 * parent ire is returned via both
3842535Ssangeeta 					 * ire and pire.
3852535Ssangeeta 					 */
3862535Ssangeeta 					IRE_REFHOLD(save_ire);
3872535Ssangeeta 				}
3882535Ssangeeta 				ire = save_ire;
3892535Ssangeeta 			} else {
3902535Ssangeeta 				ire_refrele(save_ire);
3912535Ssangeeta 				if (pire != NULL)
3922535Ssangeeta 					*pire = NULL;
3932535Ssangeeta 			}
3942535Ssangeeta 			return (ire);
3952535Ssangeeta 		} else if (pire == NULL) {
3962535Ssangeeta 			/*
3972535Ssangeeta 			 * If the caller did not ask for pire, release
3982535Ssangeeta 			 * it now.
3992535Ssangeeta 			 */
4002535Ssangeeta 			ire_refrele(save_ire);
4012535Ssangeeta 		}
4022535Ssangeeta 		return (ire);
4032535Ssangeeta 	}
4042535Ssangeeta 	ASSERT(pire == NULL || *pire == NULL);
4052535Ssangeeta 	return (ire);
4062535Ssangeeta }
4072535Ssangeeta 
4082535Ssangeeta 
4092535Ssangeeta /*
4102535Ssangeeta  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
4112535Ssangeeta  * that goes through 'ipif'. As a fallback, a route that goes through
4122535Ssangeeta  * ipif->ipif_ill can be returned.
4132535Ssangeeta  */
4142535Ssangeeta ire_t *
4152535Ssangeeta ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
4162535Ssangeeta {
4172535Ssangeeta 	ire_t	*ire;
4182535Ssangeeta 	ire_t	*save_ire = NULL;
4192535Ssangeeta 	ire_t   *gw_ire;
4202535Ssangeeta 	irb_t   *irb;
4212535Ssangeeta 	ipaddr_t gw_addr;
4222535Ssangeeta 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
4232535Ssangeeta 
4242535Ssangeeta 	ASSERT(CLASSD(group));
4252535Ssangeeta 
4262535Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
4272535Ssangeeta 	    NULL, MATCH_IRE_DEFAULT);
4282535Ssangeeta 
4292535Ssangeeta 	if (ire == NULL)
4302535Ssangeeta 		return (NULL);
4312535Ssangeeta 
4322535Ssangeeta 	irb = ire->ire_bucket;
4332535Ssangeeta 	ASSERT(irb);
4342535Ssangeeta 
4352535Ssangeeta 	IRB_REFHOLD(irb);
4362535Ssangeeta 	ire_refrele(ire);
4372535Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
4382535Ssangeeta 		if (ire->ire_addr != group ||
4392535Ssangeeta 		    ipif->ipif_zoneid != ire->ire_zoneid &&
4402535Ssangeeta 		    ire->ire_zoneid != ALL_ZONES) {
4412535Ssangeeta 			continue;
4422535Ssangeeta 		}
4432535Ssangeeta 
4442535Ssangeeta 		switch (ire->ire_type) {
4452535Ssangeeta 		case IRE_DEFAULT:
4462535Ssangeeta 		case IRE_PREFIX:
4472535Ssangeeta 		case IRE_HOST:
4482535Ssangeeta 			gw_addr = ire->ire_gateway_addr;
4492535Ssangeeta 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
4502535Ssangeeta 			    ipif, NULL, ALL_ZONES, 0, NULL, match_flags);
4512535Ssangeeta 
4522535Ssangeeta 			if (gw_ire != NULL) {
4532535Ssangeeta 				if (save_ire != NULL) {
4542535Ssangeeta 					ire_refrele(save_ire);
4552535Ssangeeta 				}
4562535Ssangeeta 				IRE_REFHOLD(ire);
4572535Ssangeeta 				if (gw_ire->ire_ipif == ipif) {
4582535Ssangeeta 					ire_refrele(gw_ire);
4592535Ssangeeta 
4602535Ssangeeta 					IRB_REFRELE(irb);
4612535Ssangeeta 					return (ire);
4622535Ssangeeta 				}
4632535Ssangeeta 				ire_refrele(gw_ire);
4642535Ssangeeta 				save_ire = ire;
4652535Ssangeeta 			}
4662535Ssangeeta 			break;
4672535Ssangeeta 		case IRE_IF_NORESOLVER:
4682535Ssangeeta 		case IRE_IF_RESOLVER:
4692535Ssangeeta 			if (ire->ire_ipif == ipif) {
4702535Ssangeeta 				if (save_ire != NULL) {
4712535Ssangeeta 					ire_refrele(save_ire);
4722535Ssangeeta 				}
4732535Ssangeeta 				IRE_REFHOLD(ire);
4742535Ssangeeta 
4752535Ssangeeta 				IRB_REFRELE(irb);
4762535Ssangeeta 				return (ire);
4772535Ssangeeta 			}
4782535Ssangeeta 			break;
4792535Ssangeeta 		}
4802535Ssangeeta 	}
4812535Ssangeeta 	IRB_REFRELE(irb);
4822535Ssangeeta 
4832535Ssangeeta 	return (save_ire);
4842535Ssangeeta }
4852535Ssangeeta 
4862535Ssangeeta /*
4872535Ssangeeta  * Find an IRE_INTERFACE for the multicast group.
4882535Ssangeeta  * Allows different routes for multicast addresses
4892535Ssangeeta  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
4902535Ssangeeta  * which point at different interfaces. This is used when IP_MULTICAST_IF
4912535Ssangeeta  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
4922535Ssangeeta  * specify the interface to join on.
4932535Ssangeeta  *
4942535Ssangeeta  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
4952535Ssangeeta  */
4962535Ssangeeta ire_t *
4972535Ssangeeta ire_lookup_multi(ipaddr_t group, zoneid_t zoneid)
4982535Ssangeeta {
4992535Ssangeeta 	ire_t	*ire;
5002535Ssangeeta 	ipif_t	*ipif = NULL;
5012535Ssangeeta 	int	match_flags = MATCH_IRE_TYPE;
5022535Ssangeeta 	ipaddr_t gw_addr;
5032535Ssangeeta 
5042535Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
5052535Ssangeeta 	    0, NULL, MATCH_IRE_DEFAULT);
5062535Ssangeeta 
5072535Ssangeeta 	/* We search a resolvable ire in case of multirouting. */
5082535Ssangeeta 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
5092535Ssangeeta 		ire_t *cire = NULL;
5102535Ssangeeta 		/*
5112535Ssangeeta 		 * If the route is not resolvable, the looked up ire
5122535Ssangeeta 		 * may be changed here. In that case, ire_multirt_lookup()
5132535Ssangeeta 		 * IRE_REFRELE the original ire and change it.
5142535Ssangeeta 		 */
5152535Ssangeeta 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW, NULL);
5162535Ssangeeta 		if (cire != NULL)
5172535Ssangeeta 			ire_refrele(cire);
5182535Ssangeeta 	}
5192535Ssangeeta 	if (ire == NULL)
5202535Ssangeeta 		return (NULL);
5212535Ssangeeta 	/*
5222535Ssangeeta 	 * Make sure we follow ire_ipif.
5232535Ssangeeta 	 *
5242535Ssangeeta 	 * We need to determine the interface route through
5252535Ssangeeta 	 * which the gateway will be reached. We don't really
5262535Ssangeeta 	 * care which interface is picked if the interface is
5272535Ssangeeta 	 * part of a group.
5282535Ssangeeta 	 */
5292535Ssangeeta 	if (ire->ire_ipif != NULL) {
5302535Ssangeeta 		ipif = ire->ire_ipif;
5312535Ssangeeta 		match_flags |= MATCH_IRE_ILL_GROUP;
5322535Ssangeeta 	}
5332535Ssangeeta 
5342535Ssangeeta 	switch (ire->ire_type) {
5352535Ssangeeta 	case IRE_DEFAULT:
5362535Ssangeeta 	case IRE_PREFIX:
5372535Ssangeeta 	case IRE_HOST:
5382535Ssangeeta 		gw_addr = ire->ire_gateway_addr;
5392535Ssangeeta 		ire_refrele(ire);
5402535Ssangeeta 		ire = ire_ftable_lookup(gw_addr, 0, 0,
5412535Ssangeeta 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
5422535Ssangeeta 		    NULL, match_flags);
5432535Ssangeeta 		return (ire);
5442535Ssangeeta 	case IRE_IF_NORESOLVER:
5452535Ssangeeta 	case IRE_IF_RESOLVER:
5462535Ssangeeta 		return (ire);
5472535Ssangeeta 	default:
5482535Ssangeeta 		ire_refrele(ire);
5492535Ssangeeta 		return (NULL);
5502535Ssangeeta 	}
5512535Ssangeeta }
5522535Ssangeeta 
5532535Ssangeeta /*
5542535Ssangeeta  * Delete the passed in ire if the gateway addr matches
5552535Ssangeeta  */
5562535Ssangeeta void
5572535Ssangeeta ire_del_host_redir(ire_t *ire, char *gateway)
5582535Ssangeeta {
5592535Ssangeeta 	if ((ire->ire_type & IRE_HOST_REDIRECT) &&
5602535Ssangeeta 	    (ire->ire_gateway_addr == *(ipaddr_t *)gateway))
5612535Ssangeeta 		ire_delete(ire);
5622535Ssangeeta }
5632535Ssangeeta 
5642535Ssangeeta /*
5652535Ssangeeta  * Search for all HOST REDIRECT routes that are
5662535Ssangeeta  * pointing at the specified gateway and
5672535Ssangeeta  * delete them. This routine is called only
5682535Ssangeeta  * when a default gateway is going away.
5692535Ssangeeta  */
5702535Ssangeeta void
5712535Ssangeeta ire_delete_host_redirects(ipaddr_t gateway)
5722535Ssangeeta {
5732535Ssangeeta 	struct rtfuncarg rtfarg;
5742535Ssangeeta 
5752535Ssangeeta 	(void) memset(&rtfarg, 0, sizeof (rtfarg));
5762535Ssangeeta 	rtfarg.rt_func = ire_del_host_redir;
5772535Ssangeeta 	rtfarg.rt_arg = (void *)&gateway;
5782783Ssowmini 	(void) ip_ftable->rnh_walktree_mt(ip_ftable, rtfunc, &rtfarg,
5792783Ssowmini 	    irb_refhold_rn, irb_refrele_rn);
5802535Ssangeeta }
5812535Ssangeeta 
5822535Ssangeeta struct ihandle_arg {
5832535Ssangeeta 	uint32_t ihandle;
5842535Ssangeeta 	ire_t	 *ire;
5852535Ssangeeta };
5862535Ssangeeta 
5872535Ssangeeta static int
5882535Ssangeeta ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
5892535Ssangeeta {
5902535Ssangeeta 	struct rt_entry *rt;
5912535Ssangeeta 	irb_t *irb;
5922535Ssangeeta 	ire_t *ire;
5932535Ssangeeta 	struct ihandle_arg *ih = arg;
5942535Ssangeeta 
5952535Ssangeeta 	rt = (struct rt_entry *)rn;
5962535Ssangeeta 	ASSERT(rt != NULL);
5972535Ssangeeta 	irb = &rt->rt_irb;
5982535Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
5992535Ssangeeta 		if ((ire->ire_type & IRE_INTERFACE) &&
6002535Ssangeeta 		    (ire->ire_ihandle == ih->ihandle)) {
6012535Ssangeeta 			ih->ire = ire;
6022535Ssangeeta 			IRE_REFHOLD(ire);
6032535Ssangeeta 			return (1);
6042535Ssangeeta 		}
6052535Ssangeeta 	}
6062535Ssangeeta 	return (0);
6072535Ssangeeta }
6082535Ssangeeta 
6092535Ssangeeta /*
6102535Ssangeeta  * Locate the interface ire that is tied to the cache ire 'cire' via
6112535Ssangeeta  * cire->ire_ihandle.
6122535Ssangeeta  *
6132535Ssangeeta  * We are trying to create the cache ire for an onlink destn. or
6142535Ssangeeta  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
6152535Ssangeeta  * case, after the ire has come back from ARP.
6162535Ssangeeta  */
6172535Ssangeeta ire_t *
6182535Ssangeeta ire_ihandle_lookup_onlink(ire_t *cire)
6192535Ssangeeta {
6202535Ssangeeta 	ire_t	*ire;
6212535Ssangeeta 	int	match_flags;
6222535Ssangeeta 	struct ihandle_arg ih;
6232535Ssangeeta 
6242535Ssangeeta 	ASSERT(cire != NULL);
6252535Ssangeeta 
6262535Ssangeeta 	/*
6272535Ssangeeta 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
6282535Ssangeeta 	 * because the ihandle refers to an ipif which can be in only one zone.
6292535Ssangeeta 	 */
6302535Ssangeeta 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
6312535Ssangeeta 	/*
6322535Ssangeeta 	 * We know that the mask of the interface ire equals cire->ire_cmask.
6332535Ssangeeta 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
6342535Ssangeeta 	 * cmask from the interface ire's mask)
6352535Ssangeeta 	 */
6362535Ssangeeta 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
6372535Ssangeeta 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
6382535Ssangeeta 	    NULL, match_flags);
6392535Ssangeeta 	if (ire != NULL)
6402535Ssangeeta 		return (ire);
6412535Ssangeeta 	/*
6422535Ssangeeta 	 * If we didn't find an interface ire above, we can't declare failure.
6432535Ssangeeta 	 * For backwards compatibility, we need to support prefix routes
6442535Ssangeeta 	 * pointing to next hop gateways that are not on-link.
6452535Ssangeeta 	 *
6462535Ssangeeta 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
6472535Ssangeeta 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
6482535Ssangeeta 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
6492535Ssangeeta 	 * doing ire_route_lookup() twice and returning an interface ire.
6502535Ssangeeta 	 *
6512535Ssangeeta 	 * Eg. default	-	gw1			(line 1)
6522535Ssangeeta 	 *	gw1	-	gw2			(line 2)
6532535Ssangeeta 	 *	gw2	-	hme0			(line 3)
6542535Ssangeeta 	 *
6552535Ssangeeta 	 * In the above example, ip_newroute() tried to create the cache ire
6562535Ssangeeta 	 * 'cire' for gw1, based on the interface route in line 3. The
6572535Ssangeeta 	 * ire_ftable_lookup() above fails, because there is no interface route
6582535Ssangeeta 	 * to reach gw1. (it is gw2). We fall thru below.
6592535Ssangeeta 	 *
6602535Ssangeeta 	 * Do a brute force search based on the ihandle in a subset of the
6612535Ssangeeta 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
6622535Ssangeeta 	 * things become very complex, since we don't have 'pire' in this
6632535Ssangeeta 	 * case. (Also note that this method is not possible in the offlink
6642535Ssangeeta 	 * case because we don't know the mask)
6652535Ssangeeta 	 */
6662535Ssangeeta 	(void) memset(&ih, 0, sizeof (ih));
6672535Ssangeeta 	ih.ihandle = cire->ire_ihandle;
6682783Ssowmini 	(void) ip_ftable->rnh_walktree_mt(ip_ftable, ire_ihandle_onlink_match,
6692783Ssowmini 	    &ih, irb_refhold_rn, irb_refrele_rn);
6702535Ssangeeta 	return (ih.ire);
6712535Ssangeeta }
6722535Ssangeeta 
6732535Ssangeeta /*
6742535Ssangeeta  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
6752535Ssangeeta  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
6762535Ssangeeta  * in the bucket skipping default interface routes and deleted entries.
6772535Ssangeeta  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
6782535Ssangeeta  * Assumes that the caller holds a reference on the IRE bucket.
6792535Ssangeeta  */
6802535Ssangeeta ire_t *
6812535Ssangeeta ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
6822535Ssangeeta {
6832535Ssangeeta 	ASSERT(ire_origin->ire_bucket != NULL);
6842535Ssangeeta 	ASSERT(ire != NULL);
6852535Ssangeeta 
6862535Ssangeeta 	do {
6872535Ssangeeta 		ire = ire->ire_next;
6882535Ssangeeta 		if (ire == NULL)
6892535Ssangeeta 			ire = ire_origin->ire_bucket->irb_ire;
6902535Ssangeeta 		if (ire == ire_origin)
6912535Ssangeeta 			return (NULL);
6922535Ssangeeta 	} while ((ire->ire_type & IRE_INTERFACE) ||
6932535Ssangeeta 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
6942535Ssangeeta 	ASSERT(ire != NULL);
6952535Ssangeeta 	return (ire);
6962535Ssangeeta }
6972535Ssangeeta 
6982535Ssangeeta static ipif_t *
6992535Ssangeeta ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
7002535Ssangeeta     int zoneid, ushort_t *marks)
7012535Ssangeeta {
7022535Ssangeeta 	ipif_t *src_ipif;
7032535Ssangeeta 
7042535Ssangeeta 	/*
7052535Ssangeeta 	 * Pick the best source address from dst_ill.
7062535Ssangeeta 	 *
7072535Ssangeeta 	 * 1) If it is part of a multipathing group, we would
7082535Ssangeeta 	 *    like to spread the inbound packets across different
7092535Ssangeeta 	 *    interfaces. ipif_select_source picks a random source
7102535Ssangeeta 	 *    across the different ills in the group.
7112535Ssangeeta 	 *
7122535Ssangeeta 	 * 2) If it is not part of a multipathing group, we try
7132535Ssangeeta 	 *    to pick the source address from the destination
7142535Ssangeeta 	 *    route. Clustering assumes that when we have multiple
7152535Ssangeeta 	 *    prefixes hosted on an interface, the prefix of the
7162535Ssangeeta 	 *    source address matches the prefix of the destination
7172535Ssangeeta 	 *    route. We do this only if the address is not
7182535Ssangeeta 	 *    DEPRECATED.
7192535Ssangeeta 	 *
7202535Ssangeeta 	 * 3) If the conn is in a different zone than the ire, we
7212535Ssangeeta 	 *    need to pick a source address from the right zone.
7222535Ssangeeta 	 *
7232535Ssangeeta 	 * NOTE : If we hit case (1) above, the prefix of the source
7242535Ssangeeta 	 *	  address picked may not match the prefix of the
7252535Ssangeeta 	 *	  destination routes prefix as ipif_select_source
7262535Ssangeeta 	 *	  does not look at "dst" while picking a source
7272535Ssangeeta 	 *	  address.
7282535Ssangeeta 	 *	  If we want the same behavior as (2), we will need
7292535Ssangeeta 	 *	  to change the behavior of ipif_select_source.
7302535Ssangeeta 	 */
7312535Ssangeeta 
7322535Ssangeeta 	if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
7332535Ssangeeta 		/*
7342535Ssangeeta 		 * The RTF_SETSRC flag is set in the parent ire (sire).
7352535Ssangeeta 		 * Check that the ipif matching the requested source
7362535Ssangeeta 		 * address still exists.
7372535Ssangeeta 		 */
7382535Ssangeeta 		src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
7392535Ssangeeta 		    zoneid, NULL, NULL, NULL, NULL);
7402535Ssangeeta 		return (src_ipif);
7412535Ssangeeta 	}
7422535Ssangeeta 	*marks |= IRE_MARK_USESRC_CHECK;
7432535Ssangeeta 	if ((dst_ill->ill_group != NULL) ||
7442535Ssangeeta 	    (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
7452535Ssangeeta 	    (dst_ill->ill_usesrc_ifindex != 0)) {
7462535Ssangeeta 		src_ipif = ipif_select_source(dst_ill, dst, zoneid);
7472535Ssangeeta 		if (src_ipif == NULL)
7482535Ssangeeta 			return (NULL);
7492535Ssangeeta 
7502535Ssangeeta 	} else {
7512535Ssangeeta 		src_ipif = ire->ire_ipif;
7522535Ssangeeta 		ASSERT(src_ipif != NULL);
7532535Ssangeeta 		/* hold src_ipif for uniformity */
7542535Ssangeeta 		ipif_refhold(src_ipif);
7552535Ssangeeta 	}
7562535Ssangeeta 	return (src_ipif);
7572535Ssangeeta }
7582535Ssangeeta 
7592535Ssangeeta /* Added to root cause a bug - should be removed later */
7602535Ssangeeta ire_t *ire_gw_cache = NULL;
7612535Ssangeeta 
7622535Ssangeeta /*
7632535Ssangeeta  * This function is called by ip_rput_noire() and ip_fast_forward()
7642535Ssangeeta  * to resolve the route of incoming packet that needs to be forwarded.
7652535Ssangeeta  * If the ire of the nexthop is not already in the cachetable, this
7662535Ssangeeta  * routine will insert it to the table, but won't trigger ARP resolution yet.
7672535Ssangeeta  * Thus unlike ip_newroute, this function adds incomplete ires to
7682535Ssangeeta  * the cachetable. ARP resolution for these ires are  delayed until
7692535Ssangeeta  * after all of the packet processing is completed and its ready to
7702535Ssangeeta  * be sent out on the wire, Eventually, the packet transmit routine
7712535Ssangeeta  * ip_xmit_v4() attempts to send a packet  to the driver. If it finds
7722535Ssangeeta  * that there is no link layer information, it will do the arp
7732535Ssangeeta  * resolution and queue the packet in ire->ire_nce->nce_qd_mp and
7742535Ssangeeta  * then send it out once the arp resolution is over
7752535Ssangeeta  * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
7762535Ssangeeta  * the model of BSD/SunOS 4
7772535Ssangeeta  *
7782535Ssangeeta  * In future, the insertion of incomplete ires in the cachetable should
7792535Ssangeeta  * be implemented in hostpath as well, as doing so will greatly reduce
7802535Ssangeeta  * the existing complexity for code paths that depend on the context of
7812535Ssangeeta  * the sender (such as IPsec).
7822535Ssangeeta  *
7832535Ssangeeta  * Thus this scheme of adding incomplete ires in cachetable in forwarding
7842535Ssangeeta  * path can be used as a template for simplifying the hostpath.
7852535Ssangeeta  */
7862535Ssangeeta 
7872535Ssangeeta ire_t *
7882535Ssangeeta ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
7892535Ssangeeta     ire_t *supplied_sire, const struct ts_label_s *tsl)
7902535Ssangeeta {
7912535Ssangeeta 	ipaddr_t gw = 0;
7922535Ssangeeta 	ire_t	*ire = NULL;
7932535Ssangeeta 	ire_t   *sire = NULL, *save_ire;
7942535Ssangeeta 	ill_t *dst_ill = NULL;
7952535Ssangeeta 	int error;
7962535Ssangeeta 	zoneid_t zoneid;
7972535Ssangeeta 	ipif_t *src_ipif = NULL;
7982535Ssangeeta 	mblk_t *res_mp;
7992535Ssangeeta 	ushort_t ire_marks = 0;
8002535Ssangeeta 	tsol_gcgrp_t *gcgrp = NULL;
8012535Ssangeeta 	tsol_gcgrp_addr_t ga;
8022535Ssangeeta 
8032535Ssangeeta 	zoneid = GLOBAL_ZONEID;
8042535Ssangeeta 
8052535Ssangeeta 	if (supplied_ire != NULL) {
8062535Ssangeeta 		/* We have arrived here from ipfil_sendpkt */
8072535Ssangeeta 		ire = supplied_ire;
8082535Ssangeeta 		sire = supplied_sire;
8092535Ssangeeta 		goto create_irecache;
8102535Ssangeeta 	}
8112535Ssangeeta 
8122535Ssangeeta 	ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
8132535Ssangeeta 	    tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
8142535Ssangeeta 	    MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR);
8152535Ssangeeta 
8162535Ssangeeta 	if (ire == NULL) {
8172535Ssangeeta 		ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST);
8182535Ssangeeta 		goto icmp_err_ret;
8192535Ssangeeta 	}
8202535Ssangeeta 
8212535Ssangeeta 	/*
8222535Ssangeeta 	 * If we encounter CGTP, we should  have the caller use
8232535Ssangeeta 	 * ip_newroute to resolve multirt instead of this function.
8242535Ssangeeta 	 * CGTP specs explicitly state that it can't be used with routers.
8252535Ssangeeta 	 * This essentially prevents insertion of incomplete RTF_MULTIRT
8262535Ssangeeta 	 * ires in cachetable.
8272535Ssangeeta 	 */
8282535Ssangeeta 	if (ip_cgtp_filter &&
8292535Ssangeeta 	    ((ire->ire_flags & RTF_MULTIRT) ||
8302535Ssangeeta 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
8312535Ssangeeta 		ip3dbg(("ire_forward: packet is to be multirouted- "
8322535Ssangeeta 		    "handing it to ip_newroute\n"));
8332535Ssangeeta 		if (sire != NULL)
8342535Ssangeeta 			ire_refrele(sire);
8352535Ssangeeta 		ire_refrele(ire);
8362535Ssangeeta 		/*
8372535Ssangeeta 		 * Inform caller about encountering of multirt so that
8382535Ssangeeta 		 * ip_newroute() can be called.
8392535Ssangeeta 		 */
8402535Ssangeeta 		*check_multirt = B_TRUE;
8412535Ssangeeta 		return (NULL);
8422535Ssangeeta 	}
8432535Ssangeeta 
8442535Ssangeeta 	*check_multirt = B_FALSE;
8452535Ssangeeta 
8462535Ssangeeta 	/*
8472535Ssangeeta 	 * Verify that the returned IRE does not have either
8482535Ssangeeta 	 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
8492535Ssangeeta 	 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
8502535Ssangeeta 	 */
8512535Ssangeeta 	if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
8522535Ssangeeta 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
8532535Ssangeeta 		ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
8542535Ssangeeta 		    (void *)ire));
8552535Ssangeeta 		goto icmp_err_ret;
8562535Ssangeeta 	}
8572535Ssangeeta 
8582535Ssangeeta 	/*
8592535Ssangeeta 	 * If we already have a fully resolved IRE CACHE of the
8602535Ssangeeta 	 * nexthop router, just hand over the cache entry
8612535Ssangeeta 	 * and we are done.
8622535Ssangeeta 	 */
8632535Ssangeeta 
8642535Ssangeeta 	if (ire->ire_type & IRE_CACHE) {
8652535Ssangeeta 
8662535Ssangeeta 		/*
8672535Ssangeeta 		 * If we are using this ire cache entry as a
8682535Ssangeeta 		 * gateway to forward packets, chances are we
8692535Ssangeeta 		 * will be using it again. So turn off
8702535Ssangeeta 		 * the temporary flag, thus reducing its
8712535Ssangeeta 		 * chances of getting deleted frequently.
8722535Ssangeeta 		 */
8732535Ssangeeta 		if (ire->ire_marks & IRE_MARK_TEMPORARY) {
8742535Ssangeeta 			irb_t *irb = ire->ire_bucket;
8752535Ssangeeta 			rw_enter(&irb->irb_lock, RW_WRITER);
8762535Ssangeeta 			ire->ire_marks &= ~IRE_MARK_TEMPORARY;
8772535Ssangeeta 			irb->irb_tmp_ire_cnt--;
8782535Ssangeeta 			rw_exit(&irb->irb_lock);
8792535Ssangeeta 		}
8802535Ssangeeta 
8812535Ssangeeta 		if (sire != NULL) {
8822535Ssangeeta 			UPDATE_OB_PKT_COUNT(sire);
8832535Ssangeeta 			sire->ire_last_used_time = lbolt;
8842535Ssangeeta 			ire_refrele(sire);
8852535Ssangeeta 		}
8862535Ssangeeta 		return (ire);
8872535Ssangeeta 	}
8882535Ssangeeta create_irecache:
8892535Ssangeeta 	/*
8902535Ssangeeta 	 * Increment the ire_ob_pkt_count field for ire if it is an
8912535Ssangeeta 	 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
8922535Ssangeeta 	 * increment the same for the parent IRE, sire, if it is some
8932535Ssangeeta 	 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST
8942535Ssangeeta 	 * and HOST_REDIRECT).
8952535Ssangeeta 	 */
8962535Ssangeeta 	if ((ire->ire_type & IRE_INTERFACE) != 0) {
8972535Ssangeeta 		UPDATE_OB_PKT_COUNT(ire);
8982535Ssangeeta 		ire->ire_last_used_time = lbolt;
8992535Ssangeeta 	}
9002535Ssangeeta 
9012535Ssangeeta 	/*
9022535Ssangeeta 	 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
9032535Ssangeeta 	 */
9042535Ssangeeta 	if (sire != NULL) {
9052535Ssangeeta 		gw = sire->ire_gateway_addr;
9062535Ssangeeta 		ASSERT((sire->ire_type &
9072535Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)) == 0);
9082535Ssangeeta 		UPDATE_OB_PKT_COUNT(sire);
9092535Ssangeeta 		sire->ire_last_used_time = lbolt;
9102535Ssangeeta 	}
9112535Ssangeeta 
9122535Ssangeeta 	/* Obtain dst_ill */
9132535Ssangeeta 	dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
9142535Ssangeeta 	if (dst_ill == NULL) {
9152535Ssangeeta 		ip2dbg(("ire_forward no dst ill; ire 0x%p\n",
9162535Ssangeeta 			(void *)ire));
9172535Ssangeeta 		goto icmp_err_ret;
9182535Ssangeeta 	}
9192535Ssangeeta 
9202535Ssangeeta 	ASSERT(src_ipif == NULL);
9212535Ssangeeta 	/* Now obtain the src_ipif */
9222535Ssangeeta 	src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
9232535Ssangeeta 	    zoneid, &ire_marks);
9242535Ssangeeta 	if (src_ipif == NULL)
9252535Ssangeeta 		goto icmp_err_ret;
9262535Ssangeeta 
9272535Ssangeeta 	switch (ire->ire_type) {
9282535Ssangeeta 	case IRE_IF_NORESOLVER:
9292535Ssangeeta 		/* create ire_cache for ire_addr endpoint */
9302535Ssangeeta 	case IRE_IF_RESOLVER:
9312535Ssangeeta 		/*
9322535Ssangeeta 		 * We have the IRE_IF_RESOLVER of the nexthop gateway
9332535Ssangeeta 		 * and now need to build a IRE_CACHE for it.
9342535Ssangeeta 		 * In this case, we have the following :
9352535Ssangeeta 		 *
9362535Ssangeeta 		 * 1) src_ipif - used for getting a source address.
9372535Ssangeeta 		 *
9382535Ssangeeta 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
9392535Ssangeeta 		 *    means packets using the IRE_CACHE that we will build
9402535Ssangeeta 		 *    here will go out on dst_ill.
9412535Ssangeeta 		 *
9422535Ssangeeta 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
9432535Ssangeeta 		 *    to be created will only be tied to the IRE_INTERFACE
9442535Ssangeeta 		 *    that was derived from the ire_ihandle field.
9452535Ssangeeta 		 *
9462535Ssangeeta 		 *    If sire is non-NULL, it means the destination is
9472535Ssangeeta 		 *    off-link and we will first create the IRE_CACHE for the
9482535Ssangeeta 		 *    gateway.
9492535Ssangeeta 		 */
9502535Ssangeeta 		res_mp = dst_ill->ill_resolver_mp;
9512535Ssangeeta 		if (ire->ire_type == IRE_IF_RESOLVER &&
9522535Ssangeeta 		    (!OK_RESOLVER_MP(res_mp))) {
9532535Ssangeeta 			ire_refrele(ire);
9542535Ssangeeta 			ire = NULL;
9552535Ssangeeta 			goto out;
9562535Ssangeeta 		}
9572535Ssangeeta 		/*
9582535Ssangeeta 		 * To be at this point in the code with a non-zero gw
9592535Ssangeeta 		 * means that dst is reachable through a gateway that
9602535Ssangeeta 		 * we have never resolved.  By changing dst to the gw
9612535Ssangeeta 		 * addr we resolve the gateway first.
9622535Ssangeeta 		 */
9632535Ssangeeta 		if (gw != INADDR_ANY) {
9642535Ssangeeta 			/*
9652535Ssangeeta 			 * The source ipif that was determined above was
9662535Ssangeeta 			 * relative to the destination address, not the
9672535Ssangeeta 			 * gateway's. If src_ipif was not taken out of
9682535Ssangeeta 			 * the IRE_IF_RESOLVER entry, we'll need to call
9692535Ssangeeta 			 * ipif_select_source() again.
9702535Ssangeeta 			 */
9712535Ssangeeta 			if (src_ipif != ire->ire_ipif) {
9722535Ssangeeta 				ipif_refrele(src_ipif);
9732535Ssangeeta 				src_ipif = ipif_select_source(dst_ill,
9742535Ssangeeta 				    gw, zoneid);
9752535Ssangeeta 				if (src_ipif == NULL)
9762535Ssangeeta 					goto icmp_err_ret;
9772535Ssangeeta 			}
9782535Ssangeeta 			dst = gw;
9792535Ssangeeta 			gw = INADDR_ANY;
9802535Ssangeeta 		}
9812535Ssangeeta 		/*
9822535Ssangeeta 		 * dst has been set to the address of the nexthop.
9832535Ssangeeta 		 *
9842535Ssangeeta 		 * TSol note: get security attributes of the nexthop;
9852535Ssangeeta 		 * Note that the nexthop may either be a gateway, or the
9862535Ssangeeta 		 * packet destination itself; Detailed explanation of
9872535Ssangeeta 		 * issues involved is  provided in the  IRE_IF_NORESOLVER
9882535Ssangeeta 		 * logic in ip_newroute().
9892535Ssangeeta 		 */
9902535Ssangeeta 		ga.ga_af = AF_INET;
9912535Ssangeeta 		IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
9922535Ssangeeta 		gcgrp = gcgrp_lookup(&ga, B_FALSE);
9932535Ssangeeta 
9942535Ssangeeta 		if (ire->ire_type == IRE_IF_NORESOLVER)
9952535Ssangeeta 			dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
9962535Ssangeeta 
9972535Ssangeeta 		save_ire = ire;
9982535Ssangeeta 		/*
9992535Ssangeeta 		 * create an incomplete ire-cache with a null dlureq_mp.
10002535Ssangeeta 		 * The dlureq_mp will be created in ire_arpresolve.
10012535Ssangeeta 		 */
10022535Ssangeeta 		ire = ire_create(
10032535Ssangeeta 			(uchar_t *)&dst,		/* dest address */
10042535Ssangeeta 		    (uchar_t *)&ip_g_all_ones,	/* mask */
10052535Ssangeeta 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
10062535Ssangeeta 		    (uchar_t *)&gw,		/* gateway address */
10072535Ssangeeta 		    NULL,
10082535Ssangeeta 		    (save_ire->ire_type == IRE_IF_RESOLVER ?  NULL:
10092535Ssangeeta 		    &save_ire->ire_max_frag),
10102535Ssangeeta 		    NULL,
10112535Ssangeeta 		    dst_ill->ill_rq,		/* recv-from queue */
10122535Ssangeeta 		    dst_ill->ill_wq,		/* send-to queue */
10132535Ssangeeta 		    IRE_CACHE,			/* IRE type */
10142535Ssangeeta 		    NULL,
10152535Ssangeeta 		    src_ipif,
10162535Ssangeeta 		    NULL,
10172535Ssangeeta 		    ire->ire_mask,		/* Parent mask */
10182535Ssangeeta 		    0,
10192535Ssangeeta 		    ire->ire_ihandle,	/* Interface handle */
10202535Ssangeeta 		    0,
10212535Ssangeeta 		    &(ire->ire_uinfo),
10222535Ssangeeta 		    NULL,
10232535Ssangeeta 		    gcgrp);
10242535Ssangeeta 		ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
10252535Ssangeeta 		if (ire != NULL) {
10262535Ssangeeta 			gcgrp = NULL; /* reference now held by IRE */
10272535Ssangeeta 			ire->ire_marks |= ire_marks;
10282535Ssangeeta 			/* add the incomplete ire: */
10292535Ssangeeta 			error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
10302535Ssangeeta 			if (error == 0 && ire != NULL) {
10312535Ssangeeta 				ire->ire_max_frag = save_ire->ire_max_frag;
10322535Ssangeeta 				ip1dbg(("setting max_frag to %d in ire 0x%p\n",
10332535Ssangeeta 				    ire->ire_max_frag, (void *)ire));
10342535Ssangeeta 			} else {
10352535Ssangeeta 				ire_refrele(save_ire);
10362535Ssangeeta 				goto icmp_err_ret;
10372535Ssangeeta 			}
10382535Ssangeeta 		} else {
10392535Ssangeeta 			if (gcgrp != NULL) {
10402535Ssangeeta 				GCGRP_REFRELE(gcgrp);
10412535Ssangeeta 				gcgrp = NULL;
10422535Ssangeeta 			}
10432535Ssangeeta 		}
10442535Ssangeeta 
10452535Ssangeeta 		ire_refrele(save_ire);
10462535Ssangeeta 		break;
10472535Ssangeeta 	default:
10482535Ssangeeta 		break;
10492535Ssangeeta 	}
10502535Ssangeeta 
10512535Ssangeeta out:
10522535Ssangeeta 	if (sire != NULL)
10532535Ssangeeta 		ire_refrele(sire);
10542535Ssangeeta 	if (dst_ill != NULL)
10552535Ssangeeta 		ill_refrele(dst_ill);
10562535Ssangeeta 	if (src_ipif != NULL)
10572535Ssangeeta 		ipif_refrele(src_ipif);
10582535Ssangeeta 	return (ire);
10592535Ssangeeta icmp_err_ret:
10602535Ssangeeta 	if (src_ipif != NULL)
10612535Ssangeeta 		ipif_refrele(src_ipif);
10622535Ssangeeta 	if (dst_ill != NULL)
10632535Ssangeeta 		ill_refrele(dst_ill);
10642535Ssangeeta 	if (sire != NULL)
10652535Ssangeeta 		ire_refrele(sire);
10662535Ssangeeta 	if (ire != NULL) {
10672535Ssangeeta 		ire_refrele(ire);
10682535Ssangeeta 	}
10692535Ssangeeta 	/* caller needs to send icmp error message */
10702535Ssangeeta 	return (NULL);
10712535Ssangeeta 
10722535Ssangeeta }
10732535Ssangeeta 
10742535Ssangeeta /*
10752535Ssangeeta  * Obtain the rt_entry and rt_irb for the route to be added to the ip_ftable.
10762535Ssangeeta  * First attempt to add a node to the radix tree via rn_addroute. If the
10772535Ssangeeta  * route already exists, return the bucket for the existing route.
10782535Ssangeeta  *
10792535Ssangeeta  * Locking notes: Need to hold the global radix tree lock in write mode to
10802535Ssangeeta  * add a radix node. To prevent the node from being deleted, ire_get_bucket()
10812535Ssangeeta  * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
10822535Ssangeeta  * while holding the irb_lock, but not the radix tree lock.
10832535Ssangeeta  */
10842535Ssangeeta irb_t *
10852535Ssangeeta ire_get_bucket(ire_t *ire)
10862535Ssangeeta {
10872535Ssangeeta 	struct radix_node *rn;
10882535Ssangeeta 	struct rt_entry *rt;
10892535Ssangeeta 	struct rt_sockaddr rmask, rdst;
10902535Ssangeeta 	irb_t *irb = NULL;
10912535Ssangeeta 
10922535Ssangeeta 	ASSERT(ip_ftable != NULL);
10932535Ssangeeta 
10942535Ssangeeta 	/* first try to see if route exists (based on rtalloc1) */
10952535Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
10962535Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
10972535Ssangeeta 	rdst.rt_sin_family = AF_INET;
10982535Ssangeeta 	rdst.rt_sin_addr.s_addr = ire->ire_addr;
10992535Ssangeeta 
11002535Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
11012535Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
11022535Ssangeeta 	rmask.rt_sin_family = AF_INET;
11032535Ssangeeta 	rmask.rt_sin_addr.s_addr = ire->ire_mask;
11042535Ssangeeta 
11052535Ssangeeta 	/*
11062535Ssangeeta 	 * add the route. based on BSD's rtrequest1(RTM_ADD)
11072535Ssangeeta 	 */
11082535Ssangeeta 	R_Malloc(rt, rt_entry_cache,  sizeof (*rt));
11092535Ssangeeta 	(void) memset(rt, 0, sizeof (*rt));
11102535Ssangeeta 	rt->rt_nodes->rn_key = (char *)&rt->rt_dst;
11112535Ssangeeta 	rt->rt_dst = rdst;
11122535Ssangeeta 	irb = &rt->rt_irb;
11132535Ssangeeta 	irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */
11142535Ssangeeta 	rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL);
11152535Ssangeeta 	RADIX_NODE_HEAD_WLOCK(ip_ftable);
11162535Ssangeeta 	rn = ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask, ip_ftable,
11172535Ssangeeta 	    (struct radix_node *)rt);
11182535Ssangeeta 	if (rn == NULL) {
11192535Ssangeeta 		RADIX_NODE_HEAD_UNLOCK(ip_ftable);
11202535Ssangeeta 		Free(rt, rt_entry_cache);
11212535Ssangeeta 		rt = NULL;
11222535Ssangeeta 		irb = NULL;
11232535Ssangeeta 		RADIX_NODE_HEAD_RLOCK(ip_ftable);
11242535Ssangeeta 		if ((rn = ip_ftable->rnh_lookup(&rdst, &rmask, ip_ftable)) !=
11252535Ssangeeta 		    NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
11262535Ssangeeta 			/* found a non-root match */
11272535Ssangeeta 			rt = (struct rt_entry *)rn;
11282535Ssangeeta 		}
11292535Ssangeeta 	}
11302535Ssangeeta 	if (rt != NULL) {
11312535Ssangeeta 		irb = &rt->rt_irb;
11322535Ssangeeta 		IRB_REFHOLD(irb);
11332535Ssangeeta 	}
11342535Ssangeeta 	RADIX_NODE_HEAD_UNLOCK(ip_ftable);
11352535Ssangeeta 	return (irb);
11362535Ssangeeta }
11372535Ssangeeta 
11382535Ssangeeta /*
11392535Ssangeeta  * This function is used when the caller wants to know the outbound
11402535Ssangeeta  * interface for a packet given only the address.
11412535Ssangeeta  * If this is a offlink IP address and there are multiple
11422535Ssangeeta  * routes to this destination, this routine will utilise the
11432535Ssangeeta  * first route it finds to IP address
11442535Ssangeeta  * Return values:
11452535Ssangeeta  * 	0	- FAILURE
11462535Ssangeeta  *	nonzero	- ifindex
11472535Ssangeeta  */
11482535Ssangeeta uint_t
11492535Ssangeeta ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid)
11502535Ssangeeta {
11512535Ssangeeta 	uint_t ifindex = 0;
11522535Ssangeeta 	ire_t *ire;
11532535Ssangeeta 	ill_t *ill;
11542535Ssangeeta 
11552535Ssangeeta 	/* zoneid is a placeholder for future routing table per-zone project */
11562535Ssangeeta 	ASSERT(zoneid == ALL_ZONES);
11572535Ssangeeta 
11582535Ssangeeta 	ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6);
11592535Ssangeeta 
11602535Ssangeeta 	if ((ire =  route_to_dst(ipaddr, zoneid)) != NULL) {
11612535Ssangeeta 		ill = ire_to_ill(ire);
11622535Ssangeeta 		if (ill != NULL)
11632535Ssangeeta 			ifindex = ill->ill_phyint->phyint_ifindex;
11642535Ssangeeta 		ire_refrele(ire);
11652535Ssangeeta 	}
11662535Ssangeeta 	return (ifindex);
11672535Ssangeeta }
11682535Ssangeeta 
11692535Ssangeeta /*
11702535Ssangeeta  * Routine to find the route to a destination. If a ifindex is supplied
11712535Ssangeeta  * it tries to match the the route to the corresponding ipif for the ifindex
11722535Ssangeeta  */
11732535Ssangeeta static	ire_t *
11742535Ssangeeta route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid)
11752535Ssangeeta {
11762535Ssangeeta 	ire_t *ire = NULL;
11772535Ssangeeta 	int match_flags;
11782535Ssangeeta 
11792535Ssangeeta 	match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
11802535Ssangeeta 	    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
11812535Ssangeeta 
11822535Ssangeeta 	/* XXX pass NULL tsl for now */
11832535Ssangeeta 
11842535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
11852535Ssangeeta 		ire = ire_route_lookup(
11862535Ssangeeta 		    ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr,
11872535Ssangeeta 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags);
11882535Ssangeeta 	} else {
11892535Ssangeeta 		ire = ire_route_lookup_v6(
11902535Ssangeeta 		    &((struct sockaddr_in6 *)dst_addr)->sin6_addr,
11912535Ssangeeta 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags);
11922535Ssangeeta 	}
11932535Ssangeeta 	return (ire);
11942535Ssangeeta }
11952535Ssangeeta 
11962535Ssangeeta /*
11972535Ssangeeta  * This routine is called by IP Filter to send a packet out on the wire
11982535Ssangeeta  * to a specified V4 dst (which may be onlink or offlink). The ifindex may or
11992535Ssangeeta  * may not be 0. A non-null ifindex indicates IP Filter has stipulated
12002535Ssangeeta  * an outgoing interface and requires the nexthop to be on that interface.
12012535Ssangeeta  * IP WILL NOT DO  the following to the data packet before sending it out:
12022535Ssangeeta  *	a. manipulate ttl
12032535Ssangeeta  *	b. checksuming
12042535Ssangeeta  *	c. ipsec work
12052535Ssangeeta  *	d. fragmentation
12062535Ssangeeta  *
12072535Ssangeeta  * Return values:
12082535Ssangeeta  *	0:		IP was able to send of the data pkt
12092535Ssangeeta  *	ECOMM:		Could not send packet
12102535Ssangeeta  *	ENONET		No route to dst. It is up to the caller
12112535Ssangeeta  *			to send icmp unreachable error message,
12122535Ssangeeta  *	EINPROGRESS	The macaddr of the onlink dst or that
12132535Ssangeeta  *			of the offlink dst's nexthop needs to get
12142535Ssangeeta  *			resolved before packet can be sent to dst.
12152535Ssangeeta  *			Thus transmission is not guaranteed.
12162535Ssangeeta  *
12172535Ssangeeta  */
12182535Ssangeeta 
12192535Ssangeeta int
12202535Ssangeeta ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex,
12212535Ssangeeta     zoneid_t zoneid)
12222535Ssangeeta {
12232535Ssangeeta 	ire_t *ire = NULL, *sire = NULL;
12242535Ssangeeta 	ire_t *ire_cache = NULL;
12252535Ssangeeta 	boolean_t   check_multirt = B_FALSE;
12262535Ssangeeta 	int value;
12272535Ssangeeta 	int match_flags;
12282535Ssangeeta 	ipaddr_t dst;
12292535Ssangeeta 
12302535Ssangeeta 	ASSERT(mp != NULL);
12312535Ssangeeta 
12322535Ssangeeta 	ASSERT(dst_addr->sa_family == AF_INET ||
12332535Ssangeeta 	    dst_addr->sa_family == AF_INET6);
12342535Ssangeeta 
12352535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
12362535Ssangeeta 		dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr;
12372535Ssangeeta 	} else {
12382535Ssangeeta 		/*
12392535Ssangeeta 		 * We dont have support for V6 yet. It will be provided
12402535Ssangeeta 		 * once RFE  6399103  has been delivered.
12412535Ssangeeta 		 * Until then, for V6 dsts, IP Filter will not call
12422535Ssangeeta 		 * this function. Instead, IP Filter will continue to do what
12432535Ssangeeta 		 * has been done since S10, namely it will use
12442535Ssangeeta 		 * ip_nexthop(),ip_nexthop_route() to obtain the
12452535Ssangeeta 		 * link-layer address of a V6 dst and then process the
12462535Ssangeeta 		 * packet and send it out on the wire on its own.
12472535Ssangeeta 		 */
12482535Ssangeeta 		ip1dbg(("ipfil_sendpkt: no V6 support \n"));
12492535Ssangeeta 		value = ECOMM;
12502535Ssangeeta 		freemsg(mp);
12512535Ssangeeta 		goto discard;
12522535Ssangeeta 	}
12532535Ssangeeta 
12542535Ssangeeta 	/*
12552535Ssangeeta 	 * Lets get the ire. We might get the ire cache entry,
12562535Ssangeeta 	 * or the ire,sire pair needed to create the cache entry.
12572535Ssangeeta 	 * XXX pass NULL tsl for now.
12582535Ssangeeta 	 */
12592535Ssangeeta 
12602535Ssangeeta 	if (ifindex == 0) {
12612535Ssangeeta 		/* There is no supplied index. So use the FIB info */
12622535Ssangeeta 
12632535Ssangeeta 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
12642535Ssangeeta 		    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
12652535Ssangeeta 		ire = ire_route_lookup(dst,
12662535Ssangeeta 		    0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp),
12672535Ssangeeta 		    match_flags);
12682535Ssangeeta 	} else {
12692535Ssangeeta 		ipif_t *supplied_ipif;
12702535Ssangeeta 		ill_t *ill;
12712535Ssangeeta 
12722535Ssangeeta 		/*
12732535Ssangeeta 		 * If supplied ifindex is non-null, the only valid
12742535Ssangeeta 		 * nexthop is one off of the interface corresponding
12752535Ssangeeta 		 * to the specified ifindex.
12762535Ssangeeta 		 */
12772535Ssangeeta 
12782535Ssangeeta 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
12792535Ssangeeta 		    NULL, NULL, NULL, NULL);
12802535Ssangeeta 		if (ill != NULL) {
12812535Ssangeeta 			supplied_ipif = ipif_get_next_ipif(NULL, ill);
12822535Ssangeeta 		} else {
12832535Ssangeeta 			ip1dbg(("ipfil_sendpkt: Could not find"
12842535Ssangeeta 			    " route to dst\n"));
12852535Ssangeeta 			value = ECOMM;
12862535Ssangeeta 			freemsg(mp);
12872535Ssangeeta 			goto discard;
12882535Ssangeeta 		}
12892535Ssangeeta 
12902535Ssangeeta 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
12912535Ssangeeta 		    MATCH_IRE_IPIF | MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE|
12922535Ssangeeta 		    MATCH_IRE_SECATTR);
12932535Ssangeeta 
12942535Ssangeeta 		ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif,
12952535Ssangeeta 		    &sire, zoneid, MBLK_GETLABEL(mp), match_flags);
12962535Ssangeeta 		ill_refrele(ill);
12972535Ssangeeta 	}
12982535Ssangeeta 
12992535Ssangeeta 	/*
13002535Ssangeeta 	 * Verify that the returned IRE is non-null and does
13012535Ssangeeta 	 * not have either the RTF_REJECT or RTF_BLACKHOLE
13022535Ssangeeta 	 * flags set and that the IRE is  either an IRE_CACHE,
13032535Ssangeeta 	 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
13042535Ssangeeta 	 */
13052535Ssangeeta 	if (ire == NULL ||
13062535Ssangeeta 	    ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
13072535Ssangeeta 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)) {
13082535Ssangeeta 		/*
13092535Ssangeeta 		 * Either ire could not be found or we got
13102535Ssangeeta 		 * an invalid one
13112535Ssangeeta 		 */
13122535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Could not find route to dst\n"));
13132535Ssangeeta 		value = ENONET;
13142535Ssangeeta 		freemsg(mp);
13152535Ssangeeta 		goto discard;
13162535Ssangeeta 	}
13172535Ssangeeta 
13182535Ssangeeta 	/* IP Filter and CGTP dont mix. So bail out if CGTP is on */
13192535Ssangeeta 	if (ip_cgtp_filter &&
13202535Ssangeeta 	    ((ire->ire_flags & RTF_MULTIRT) ||
13212535Ssangeeta 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
13222535Ssangeeta 		ip1dbg(("ipfil_sendpkt: IPFilter does not work with CGTP\n"));
13232535Ssangeeta 		value = ECOMM;
13242535Ssangeeta 		freemsg(mp);
13252535Ssangeeta 		goto discard;
13262535Ssangeeta 	}
13272535Ssangeeta 
13282535Ssangeeta 	ASSERT(ire->ire_nce != NULL);
13292535Ssangeeta 	/*
13302535Ssangeeta 	 * If needed, we will create the ire cache entry for the
13312535Ssangeeta 	 * nexthop, resolve its link-layer address and then send
13322535Ssangeeta 	 * the packet out without ttl, checksumming, IPSec processing.
13332535Ssangeeta 	 */
13342535Ssangeeta 
13352535Ssangeeta 	switch (ire->ire_type) {
13362535Ssangeeta 	case IRE_IF_NORESOLVER:
13372535Ssangeeta 	case IRE_CACHE:
13382535Ssangeeta 		if (sire != NULL) {
13392535Ssangeeta 			UPDATE_OB_PKT_COUNT(sire);
13402535Ssangeeta 			sire->ire_last_used_time = lbolt;
13412535Ssangeeta 			ire_refrele(sire);
13422535Ssangeeta 		}
13432535Ssangeeta 		ire_cache = ire;
13442535Ssangeeta 		break;
13452535Ssangeeta 	case IRE_IF_RESOLVER:
13462535Ssangeeta 		/*
13472535Ssangeeta 		 * Call ire_forward(). This function
13482535Ssangeeta 		 * will, create the ire cache entry of the
13492535Ssangeeta 		 * the nexthop and adds this incomplete ire
13502535Ssangeeta 		 * to the ire cache table
13512535Ssangeeta 		 */
13522535Ssangeeta 		ire_cache = ire_forward(dst, &check_multirt, ire, sire,
13532535Ssangeeta 		    MBLK_GETLABEL(mp));
13542535Ssangeeta 		if (ire_cache == NULL) {
13552535Ssangeeta 			ip1dbg(("ipfil_sendpkt: failed to create the"
13562535Ssangeeta 			    " ire cache entry \n"));
13572535Ssangeeta 			value = ENONET;
13582535Ssangeeta 			freemsg(mp);
13592535Ssangeeta 			sire = NULL;
13602535Ssangeeta 			ire = NULL;
13612535Ssangeeta 			goto discard;
13622535Ssangeeta 		}
13632535Ssangeeta 		break;
13642535Ssangeeta 	}
13652535Ssangeeta 	/*
13662535Ssangeeta 	 * Now that we have the ire cache entry of the nexthop, call
13672535Ssangeeta 	 * ip_xmit_v4() to trigger mac addr resolution
13682535Ssangeeta 	 * if necessary and send it once ready.
13692535Ssangeeta 	 */
13702535Ssangeeta 
13712535Ssangeeta 	value = ip_xmit_v4(mp, ire_cache, NULL, B_FALSE);
13722535Ssangeeta 	ire_refrele(ire_cache);
13732535Ssangeeta 	/*
13742535Ssangeeta 	 * At this point, the reference for these have already been
13752535Ssangeeta 	 * released within ire_forward() and/or ip_xmit_v4(). So we set
13762535Ssangeeta 	 * them to NULL to make sure we dont drop the references
13772535Ssangeeta 	 * again in case ip_xmit_v4() returns with either SEND_FAILED
13782535Ssangeeta 	 * or LLHDR_RESLV_FAILED
13792535Ssangeeta 	 */
13802535Ssangeeta 	sire = NULL;
13812535Ssangeeta 	ire = NULL;
13822535Ssangeeta 
13832535Ssangeeta 	switch (value) {
13842535Ssangeeta 	case SEND_FAILED:
13852535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Send failed\n"));
13862535Ssangeeta 		value = ECOMM;
13872535Ssangeeta 		break;
13882535Ssangeeta 	case LLHDR_RESLV_FAILED:
13892535Ssangeeta 		ip1dbg(("ipfil_sendpkt: Link-layer resolution"
13902535Ssangeeta 		    "  failed\n"));
13912535Ssangeeta 		value = ECOMM;
13922535Ssangeeta 		break;
13932535Ssangeeta 	case LOOKUP_IN_PROGRESS:
13942535Ssangeeta 		return (EINPROGRESS);
13952535Ssangeeta 	case SEND_PASSED:
13962535Ssangeeta 		return (0);
13972535Ssangeeta 	}
13982535Ssangeeta discard:
13992535Ssangeeta 	if (dst_addr->sa_family == AF_INET) {
14002535Ssangeeta 		BUMP_MIB(&ip_mib, ipOutDiscards);
14012535Ssangeeta 	} else {
14022535Ssangeeta 		BUMP_MIB(&ip6_mib, ipv6OutDiscards);
14032535Ssangeeta 	}
14042535Ssangeeta 	if (ire != NULL)
14052535Ssangeeta 		ire_refrele(ire);
14062535Ssangeeta 	if (sire != NULL)
14072535Ssangeeta 		ire_refrele(sire);
14082535Ssangeeta 	return (value);
14092535Ssangeeta }
14102535Ssangeeta 
14112535Ssangeeta /* ire_walk routine invoked for ip_ire_report for each IRE. */
14122535Ssangeeta void
14132535Ssangeeta ire_report_ftable(ire_t *ire, char *m)
14142535Ssangeeta {
14152535Ssangeeta 	char	buf1[16];
14162535Ssangeeta 	char	buf2[16];
14172535Ssangeeta 	char	buf3[16];
14182535Ssangeeta 	char	buf4[16];
14192535Ssangeeta 	uint_t	fo_pkt_count;
14202535Ssangeeta 	uint_t	ib_pkt_count;
14212535Ssangeeta 	int	ref;
14222535Ssangeeta 	uint_t	print_len, buf_len;
14232535Ssangeeta 	mblk_t 	*mp = (mblk_t *)m;
14242535Ssangeeta 
14252535Ssangeeta 	if (ire->ire_type & IRE_CACHETABLE)
14262535Ssangeeta 		return;
14272535Ssangeeta 	buf_len = mp->b_datap->db_lim - mp->b_wptr;
14282535Ssangeeta 	if (buf_len <= 0)
14292535Ssangeeta 		return;
14302535Ssangeeta 
14312535Ssangeeta 	/* Number of active references of this ire */
14322535Ssangeeta 	ref = ire->ire_refcnt;
14332535Ssangeeta 	/* "inbound" to a non local address is a forward */
14342535Ssangeeta 	ib_pkt_count = ire->ire_ib_pkt_count;
14352535Ssangeeta 	fo_pkt_count = 0;
14362535Ssangeeta 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
14372535Ssangeeta 		fo_pkt_count = ib_pkt_count;
14382535Ssangeeta 		ib_pkt_count = 0;
14392535Ssangeeta 	}
14402535Ssangeeta 	print_len = snprintf((char *)mp->b_wptr, buf_len,
14412535Ssangeeta 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
14422535Ssangeeta 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
14432535Ssangeeta 	    "%04d %08d %08d %d/%d/%d %s\n",
14442535Ssangeeta 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
14452535Ssangeeta 	    (int)ire->ire_zoneid,
14462535Ssangeeta 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
14472535Ssangeeta 	    ip_dot_addr(ire->ire_src_addr, buf3),
14482535Ssangeeta 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
14492535Ssangeeta 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
14502535Ssangeeta 	    ire->ire_uinfo.iulp_rtt_sd,
14512535Ssangeeta 	    ire->ire_uinfo.iulp_ssthresh, ref,
14522535Ssangeeta 	    ire->ire_uinfo.iulp_rtomax,
14532535Ssangeeta 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
14542535Ssangeeta 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
14552535Ssangeeta 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
14562535Ssangeeta 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
14572535Ssangeeta 	    ire->ire_uinfo.iulp_sack,
14582535Ssangeeta 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
14592535Ssangeeta 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
14602535Ssangeeta 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
14612535Ssangeeta 	if (print_len < buf_len) {
14622535Ssangeeta 		mp->b_wptr += print_len;
14632535Ssangeeta 	} else {
14642535Ssangeeta 		mp->b_wptr += buf_len;
14652535Ssangeeta 	}
14662535Ssangeeta }
14672535Ssangeeta 
14682535Ssangeeta /*
14692535Ssangeeta  * callback function provided by ire_ftable_lookup when calling
14702535Ssangeeta  * rn_match_args(). Invoke ire_match_args on each matching leaf node in
14712535Ssangeeta  * the radix tree.
14722535Ssangeeta  */
14732535Ssangeeta boolean_t
14742535Ssangeeta ire_find_best_route(struct radix_node *rn, void *arg)
14752535Ssangeeta {
14762535Ssangeeta 	struct rt_entry *rt = (struct rt_entry *)rn;
14772535Ssangeeta 	irb_t *irb_ptr;
14782535Ssangeeta 	ire_t *ire;
14792535Ssangeeta 	ire_ftable_args_t *margs = arg;
14802535Ssangeeta 	ipaddr_t match_mask;
14812535Ssangeeta 
14822535Ssangeeta 	ASSERT(rt != NULL);
14832535Ssangeeta 
14842535Ssangeeta 	irb_ptr = &rt->rt_irb;
14852535Ssangeeta 
14862535Ssangeeta 	if (irb_ptr->irb_ire_cnt == 0)
14872535Ssangeeta 		return (B_FALSE);
14882535Ssangeeta 
14892535Ssangeeta 	rw_enter(&irb_ptr->irb_lock, RW_READER);
14902535Ssangeeta 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
14912535Ssangeeta 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
14922535Ssangeeta 			continue;
14932535Ssangeeta 		if (margs->ift_flags & MATCH_IRE_MASK)
14942535Ssangeeta 			match_mask = margs->ift_mask;
14952535Ssangeeta 		else
14962535Ssangeeta 			match_mask = ire->ire_mask;
14972535Ssangeeta 
14982535Ssangeeta 		if (ire_match_args(ire, margs->ift_addr, match_mask,
14992535Ssangeeta 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
15002535Ssangeeta 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
15012535Ssangeeta 		    margs->ift_flags)) {
15022535Ssangeeta 			IRE_REFHOLD(ire);
15032535Ssangeeta 			rw_exit(&irb_ptr->irb_lock);
15042535Ssangeeta 			margs->ift_best_ire = ire;
15052535Ssangeeta 			return (B_TRUE);
15062535Ssangeeta 		}
15072535Ssangeeta 	}
15082535Ssangeeta 	rw_exit(&irb_ptr->irb_lock);
15092535Ssangeeta 	return (B_FALSE);
15102535Ssangeeta }
15112535Ssangeeta 
15122535Ssangeeta /*
15132535Ssangeeta  * ftable irb_t structures are dynamically allocated, and we need to
15142535Ssangeeta  * check if the irb_t (and associated ftable tree attachment) needs to
15152535Ssangeeta  * be cleaned up when the irb_refcnt goes to 0. The conditions that need
15162535Ssangeeta  * be verified are:
15172535Ssangeeta  * - no other walkers of the irebucket, i.e., quiescent irb_refcnt,
15182535Ssangeeta  * - no other threads holding references to ire's in the bucket,
15192535Ssangeeta  *   i.e., irb_nire == 0
15202535Ssangeeta  * - no active ire's in the bucket, i.e., irb_ire_cnt == 0
15212535Ssangeeta  * - need to hold the global tree lock and irb_lock in write mode.
15222535Ssangeeta  */
15232535Ssangeeta void
15242535Ssangeeta irb_refrele_ftable(irb_t *irb)
15252535Ssangeeta {
15262535Ssangeeta 	for (;;) {
15272535Ssangeeta 		rw_enter(&irb->irb_lock, RW_WRITER);
15282535Ssangeeta 		ASSERT(irb->irb_refcnt != 0);
15292535Ssangeeta 		if (irb->irb_refcnt != 1) {
15302535Ssangeeta 			/*
15312535Ssangeeta 			 * Someone has a reference to this radix node
15322535Ssangeeta 			 * or there is some bucket walker.
15332535Ssangeeta 			 */
15342535Ssangeeta 			irb->irb_refcnt--;
15352535Ssangeeta 			rw_exit(&irb->irb_lock);
15362535Ssangeeta 			return;
15372535Ssangeeta 		} else {
15382535Ssangeeta 			/*
15392535Ssangeeta 			 * There is no other walker, nor is there any
15402535Ssangeeta 			 * other thread that holds a direct ref to this
15412535Ssangeeta 			 * radix node. Do the clean up if needed. Call
15422535Ssangeeta 			 * to ire_unlink will clear the IRB_MARK_CONDEMNED flag
15432535Ssangeeta 			 */
15442535Ssangeeta 			if (irb->irb_marks & IRB_MARK_CONDEMNED)  {
15452535Ssangeeta 				ire_t *ire_list;
15462535Ssangeeta 
15472535Ssangeeta 				ire_list = ire_unlink(irb);
15482535Ssangeeta 				rw_exit(&irb->irb_lock);
15492535Ssangeeta 
15502535Ssangeeta 				if (ire_list != NULL)
15512535Ssangeeta 					ire_cleanup(ire_list);
15522535Ssangeeta 				/*
15532535Ssangeeta 				 * more CONDEMNED entries could have
15542535Ssangeeta 				 * been added while we dropped the lock,
15552535Ssangeeta 				 * so we have to re-check.
15562535Ssangeeta 				 */
15572535Ssangeeta 				continue;
15582535Ssangeeta 			}
15592535Ssangeeta 
15602535Ssangeeta 			/*
15612535Ssangeeta 			 * Now check if there are still any ires
15622535Ssangeeta 			 * associated with this radix node.
15632535Ssangeeta 			 */
15642535Ssangeeta 			if (irb->irb_nire != 0) {
15652535Ssangeeta 				/*
15662535Ssangeeta 				 * someone is still holding on
15672535Ssangeeta 				 * to ires in this bucket
15682535Ssangeeta 				 */
15692535Ssangeeta 				irb->irb_refcnt--;
15702535Ssangeeta 				rw_exit(&irb->irb_lock);
15712535Ssangeeta 				return;
15722535Ssangeeta 			} else {
15732535Ssangeeta 				/*
15742535Ssangeeta 				 * Everything is clear. Zero walkers,
15752535Ssangeeta 				 * Zero threads with a ref to this
15762535Ssangeeta 				 * radix node, Zero ires associated with
15772535Ssangeeta 				 * this radix node. Due to lock order,
15782535Ssangeeta 				 * check the above conditions again
15792535Ssangeeta 				 * after grabbing all locks in the right order
15802535Ssangeeta 				 */
15812535Ssangeeta 				rw_exit(&irb->irb_lock);
15822535Ssangeeta 				if (irb_inactive(irb))
15832535Ssangeeta 					return;
15842535Ssangeeta 				/*
15852535Ssangeeta 				 * irb_inactive could not free the irb.
15862535Ssangeeta 				 * See if there are any walkers, if not
15872535Ssangeeta 				 * try to clean up again.
15882535Ssangeeta 				 */
15892535Ssangeeta 			}
15902535Ssangeeta 		}
15912535Ssangeeta 	}
15922535Ssangeeta }
15932535Ssangeeta 
15942535Ssangeeta /*
15952535Ssangeeta  * IRE iterator used by ire_ftable_lookup() to process multiple default
15962535Ssangeeta  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
15972535Ssangeeta  * in the bucket skipping default interface routes and deleted entries.
15982535Ssangeeta  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
15992535Ssangeeta  * Assumes that the caller holds a reference on the IRE bucket.
16002535Ssangeeta  *
16012535Ssangeeta  * In the absence of good IRE_DEFAULT routes, this function will return
16022535Ssangeeta  * the first IRE_INTERFACE route found (if any).
16032535Ssangeeta  */
16042535Ssangeeta ire_t *
16052535Ssangeeta ire_round_robin(irb_t *irb_ptr, zoneid_t zoneid, ire_ftable_args_t *margs)
16062535Ssangeeta {
16072535Ssangeeta 	ire_t	*ire_origin;
16082535Ssangeeta 	ire_t	*ire, *maybe_ire = NULL;
16092535Ssangeeta 
16102535Ssangeeta 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
16112535Ssangeeta 	ire_origin = irb_ptr->irb_rr_origin;
1612*2894Ssowmini 	if (ire_origin != NULL) {
16132535Ssangeeta 		ire_origin = ire_origin->ire_next;
1614*2894Ssowmini 		IRE_FIND_NEXT_ORIGIN(ire_origin);
1615*2894Ssowmini 	}
16162535Ssangeeta 
16172535Ssangeeta 	if (ire_origin == NULL) {
16182535Ssangeeta 		/*
16192535Ssangeeta 		 * first time through routine, or we dropped off the end
16202535Ssangeeta 		 * of list.
16212535Ssangeeta 		 */
16222535Ssangeeta 		ire_origin = irb_ptr->irb_ire;
1623*2894Ssowmini 		IRE_FIND_NEXT_ORIGIN(ire_origin);
16242535Ssangeeta 	}
16252535Ssangeeta 	irb_ptr->irb_rr_origin = ire_origin;
16262535Ssangeeta 	IRB_REFHOLD_LOCKED(irb_ptr);
16272535Ssangeeta 	rw_exit(&irb_ptr->irb_lock);
16282535Ssangeeta 
1629*2894Ssowmini 	DTRACE_PROBE2(ire__rr__origin, (irb_t *), irb_ptr,
1630*2894Ssowmini 	    (ire_t *), ire_origin);
1631*2894Ssowmini 
16322535Ssangeeta 	/*
16332535Ssangeeta 	 * Round-robin the routers list looking for a route that
16342535Ssangeeta 	 * matches the passed in parameters.
16352535Ssangeeta 	 * We start with the ire we found above and we walk the hash
16362535Ssangeeta 	 * list until we're back where we started. It doesn't matter if
16372535Ssangeeta 	 * routes are added or deleted by other threads - we know this
16382535Ssangeeta 	 * ire will stay in the list because we hold a reference on the
16392535Ssangeeta 	 * ire bucket.
16402535Ssangeeta 	 */
16412535Ssangeeta 	ire = ire_origin;
16422535Ssangeeta 	while (ire != NULL) {
16432535Ssangeeta 		int match_flags = 0;
16442535Ssangeeta 		ire_t *rire;
16452535Ssangeeta 
16462535Ssangeeta 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
16472535Ssangeeta 			goto next_ire;
16482535Ssangeeta 
16492535Ssangeeta 		if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0,
16502535Ssangeeta 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
16512535Ssangeeta 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
16522535Ssangeeta 		    margs->ift_flags))
16532535Ssangeeta 			goto next_ire;
16542535Ssangeeta 
16552535Ssangeeta 		if (ire->ire_type & IRE_INTERFACE) {
16562535Ssangeeta 			/*
16572535Ssangeeta 			 * keep looking to see if there is a non-interface
16582535Ssangeeta 			 * default ire, but save this one as a last resort.
16592535Ssangeeta 			 */
16602535Ssangeeta 			if (maybe_ire == NULL)
16612535Ssangeeta 				maybe_ire = ire;
16622535Ssangeeta 			goto next_ire;
16632535Ssangeeta 		}
16642535Ssangeeta 
16652535Ssangeeta 		if (zoneid == ALL_ZONES) {
16662535Ssangeeta 			IRE_REFHOLD(ire);
16672535Ssangeeta 			IRB_REFRELE(irb_ptr);
16682535Ssangeeta 			return (ire);
16692535Ssangeeta 		}
16702535Ssangeeta 		/*
16712535Ssangeeta 		 * When we're in a local zone, we're only
16722535Ssangeeta 		 * interested in routers that are
16732535Ssangeeta 		 * reachable through ipifs within our zone.
16742535Ssangeeta 		 */
16752535Ssangeeta 		if (ire->ire_ipif != NULL) {
16762535Ssangeeta 			match_flags |= MATCH_IRE_ILL_GROUP;
16772535Ssangeeta 		}
16782535Ssangeeta 		rire = ire_route_lookup(ire->ire_gateway_addr,
16792535Ssangeeta 		    0, 0, 0, ire->ire_ipif, NULL, zoneid, margs->ift_tsl,
16802535Ssangeeta 		    match_flags);
16812535Ssangeeta 		if (rire != NULL) {
16822535Ssangeeta 			ire_refrele(rire);
16832535Ssangeeta 			IRE_REFHOLD(ire);
16842535Ssangeeta 			IRB_REFRELE(irb_ptr);
16852535Ssangeeta 			return (ire);
16862535Ssangeeta 		}
16872535Ssangeeta next_ire:
16882535Ssangeeta 		ire = (ire->ire_next ?  ire->ire_next : irb_ptr->irb_ire);
16892535Ssangeeta 		if (ire == ire_origin)
16902535Ssangeeta 			break;
16912535Ssangeeta 	}
16922535Ssangeeta 	if (maybe_ire != NULL)
16932535Ssangeeta 		IRE_REFHOLD(maybe_ire);
16942535Ssangeeta 	IRB_REFRELE(irb_ptr);
16952535Ssangeeta 	return (maybe_ire);
16962535Ssangeeta }
16972783Ssowmini 
16982783Ssowmini void
16992783Ssowmini irb_refhold_rn(struct radix_node *rn)
17002783Ssowmini {
17012783Ssowmini 	if ((rn->rn_flags & RNF_ROOT) == 0)
17022783Ssowmini 		IRB_REFHOLD(&((rt_t *)(rn))->rt_irb);
17032783Ssowmini }
17042783Ssowmini 
17052783Ssowmini void
17062783Ssowmini irb_refrele_rn(struct radix_node *rn)
17072783Ssowmini {
17082783Ssowmini 	if ((rn->rn_flags & RNF_ROOT) == 0)
17092783Ssowmini 		irb_refrele_ftable(&((rt_t *)(rn))->rt_irb);
17102783Ssowmini }
1711