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