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