xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ftable.c (revision edd26dc5eeb3b093945c371e4b6dd8286348d53f)
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>
46*edd26dc5Sdr146992 #include <sys/strsubr.h>
47*edd26dc5Sdr146992 #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>
59*edd26dc5Sdr146992 #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 *);
105*edd26dc5Sdr146992 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 	ASSERT(!(flags & MATCH_IRE_WQ));
151c793af95Ssangeeta 
152c793af95Ssangeeta 	/*
153c793af95Ssangeeta 	 * When we return NULL from this function, we should make
154c793af95Ssangeeta 	 * sure that *pire is NULL so that the callers will not
155c793af95Ssangeeta 	 * wrongly REFRELE the pire.
156c793af95Ssangeeta 	 */
157c793af95Ssangeeta 	if (pire != NULL)
158c793af95Ssangeeta 		*pire = NULL;
159c793af95Ssangeeta 	/*
160c793af95Ssangeeta 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
161c793af95Ssangeeta 	 * MATCH_IRE_ILL is set.
162c793af95Ssangeeta 	 */
163c793af95Ssangeeta 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
164c793af95Ssangeeta 	    (ipif == NULL))
165c793af95Ssangeeta 		return (NULL);
166c793af95Ssangeeta 
167c793af95Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
168c793af95Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
169c793af95Ssangeeta 	rdst.rt_sin_family = AF_INET;
170c793af95Ssangeeta 	rdst.rt_sin_addr.s_addr = addr;
171c793af95Ssangeeta 
172c793af95Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
173c793af95Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
174c793af95Ssangeeta 	rmask.rt_sin_family = AF_INET;
175c793af95Ssangeeta 	rmask.rt_sin_addr.s_addr = mask;
176c793af95Ssangeeta 
177c793af95Ssangeeta 	(void) memset(&margs, 0, sizeof (margs));
178c793af95Ssangeeta 	margs.ift_addr = addr;
179c793af95Ssangeeta 	margs.ift_mask = mask;
180c793af95Ssangeeta 	margs.ift_gateway = gateway;
181c793af95Ssangeeta 	margs.ift_type = type;
182c793af95Ssangeeta 	margs.ift_ipif = ipif;
183c793af95Ssangeeta 	margs.ift_zoneid = zoneid;
184c793af95Ssangeeta 	margs.ift_ihandle = ihandle;
185c793af95Ssangeeta 	margs.ift_tsl = tsl;
186c793af95Ssangeeta 	margs.ift_flags = flags;
187c793af95Ssangeeta 
188c793af95Ssangeeta 	/*
189c793af95Ssangeeta 	 * The flags argument passed to ire_ftable_lookup may cause the
190c793af95Ssangeeta 	 * search to return, not the longest matching prefix, but the
191c793af95Ssangeeta 	 * "best matching prefix", i.e., the longest prefix that also
192c793af95Ssangeeta 	 * satisfies constraints imposed via the permutation of flags
193c793af95Ssangeeta 	 * passed in. To achieve this, we invoke ire_match_args() on
194c793af95Ssangeeta 	 * each matching leaf in the  radix tree. ire_match_args is
195c793af95Ssangeeta 	 * invoked by the callback function ire_find_best_route()
196c793af95Ssangeeta 	 * We hold the global tree lock in read mode when calling
197c793af95Ssangeeta 	 * rn_match_args.Before dropping the global tree lock, ensure
198c793af95Ssangeeta 	 * that the radix node can't be deleted by incrementing ire_refcnt.
199c793af95Ssangeeta 	 */
200f4b3ec61Sdh155122 	RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
201f4b3ec61Sdh155122 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
202f4b3ec61Sdh155122 	    ipst->ips_ip_ftable, ire_find_best_route, &margs);
203c793af95Ssangeeta 	ire = margs.ift_best_ire;
204f4b3ec61Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
205c793af95Ssangeeta 
206c793af95Ssangeeta 	if (rt == NULL) {
207c793af95Ssangeeta 		return (NULL);
208c793af95Ssangeeta 	} else {
209c793af95Ssangeeta 		ASSERT(ire != NULL);
210c793af95Ssangeeta 	}
211c793af95Ssangeeta 
212c793af95Ssangeeta 	DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire);
213c793af95Ssangeeta 
214c793af95Ssangeeta 	if (!IS_DEFAULT_ROUTE(ire))
215c793af95Ssangeeta 		goto found_ire_held;
216c793af95Ssangeeta 	/*
217c793af95Ssangeeta 	 * If default route is found, see if default matching criteria
218c793af95Ssangeeta 	 * are satisfied.
219c793af95Ssangeeta 	 */
220c793af95Ssangeeta 	if (flags & MATCH_IRE_MASK) {
221c793af95Ssangeeta 		/*
222c793af95Ssangeeta 		 * we were asked to match a 0 mask, and came back with
223c793af95Ssangeeta 		 * a default route. Ok to return it.
224c793af95Ssangeeta 		 */
225c793af95Ssangeeta 		goto found_default_ire;
226c793af95Ssangeeta 	}
227c793af95Ssangeeta 	if ((flags & MATCH_IRE_TYPE) &&
228c793af95Ssangeeta 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
229c793af95Ssangeeta 		/*
230c793af95Ssangeeta 		 * we were asked to match a default ire type. Ok to return it.
231c793af95Ssangeeta 		 */
232c793af95Ssangeeta 		goto found_default_ire;
233c793af95Ssangeeta 	}
234c793af95Ssangeeta 	if (flags & MATCH_IRE_DEFAULT) {
235c793af95Ssangeeta 		goto found_default_ire;
236c793af95Ssangeeta 	}
237c793af95Ssangeeta 	/*
238c793af95Ssangeeta 	 * we found a default route, but default matching criteria
239c793af95Ssangeeta 	 * are not specified and we are not explicitly looking for
240c793af95Ssangeeta 	 * default.
241c793af95Ssangeeta 	 */
242c793af95Ssangeeta 	IRE_REFRELE(ire);
243c793af95Ssangeeta 	return (NULL);
244c793af95Ssangeeta found_default_ire:
245c793af95Ssangeeta 	/*
246c793af95Ssangeeta 	 * round-robin only if we have more than one route in the bucket.
247c793af95Ssangeeta 	 */
248c793af95Ssangeeta 	if ((ire->ire_bucket->irb_ire_cnt > 1) &&
249c793af95Ssangeeta 	    IS_DEFAULT_ROUTE(ire) &&
250c793af95Ssangeeta 	    ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
251c793af95Ssangeeta 	    MATCH_IRE_DEFAULT)) {
252c793af95Ssangeeta 		ire_t *next_ire;
253c793af95Ssangeeta 
254f4b3ec61Sdh155122 		next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs,
255f4b3ec61Sdh155122 		    ipst);
256c793af95Ssangeeta 		IRE_REFRELE(ire);
257c793af95Ssangeeta 		if (next_ire != NULL) {
258c793af95Ssangeeta 			ire = next_ire;
259c793af95Ssangeeta 		} else {
260c793af95Ssangeeta 			/* no route */
261c793af95Ssangeeta 			return (NULL);
262c793af95Ssangeeta 		}
263c793af95Ssangeeta 	}
264c793af95Ssangeeta found_ire_held:
265c793af95Ssangeeta 	ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
266c793af95Ssangeeta 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
267c793af95Ssangeeta 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
268c793af95Ssangeeta 		return (ire);
269c793af95Ssangeeta 	}
270c793af95Ssangeeta 	/*
271c793af95Ssangeeta 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
272c793af95Ssangeeta 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
273c793af95Ssangeeta 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
274c793af95Ssangeeta 	 * IRE (one of the prefix types), then it is necessary to fill in the
275c793af95Ssangeeta 	 * parent IRE pointed to by pire, and then lookup the gateway address of
276c793af95Ssangeeta 	 * the parent.  For backwards compatiblity, if this lookup returns an
277c793af95Ssangeeta 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
278c793af95Ssangeeta 	 * of lookup is done.
279c793af95Ssangeeta 	 */
280c793af95Ssangeeta 	if (flags & MATCH_IRE_RECURSIVE) {
281c793af95Ssangeeta 		ipif_t	*gw_ipif;
282c793af95Ssangeeta 		int match_flags = MATCH_IRE_DSTONLY;
283c793af95Ssangeeta 		ire_t *save_ire;
284c793af95Ssangeeta 
285c793af95Ssangeeta 		if (ire->ire_type & IRE_INTERFACE)
286c793af95Ssangeeta 			return (ire);
287c793af95Ssangeeta 		if (pire != NULL)
288c793af95Ssangeeta 			*pire = ire;
289c793af95Ssangeeta 		/*
290c793af95Ssangeeta 		 * If we can't find an IRE_INTERFACE or the caller has not
291c793af95Ssangeeta 		 * asked for pire, we need to REFRELE the save_ire.
292c793af95Ssangeeta 		 */
293c793af95Ssangeeta 		save_ire = ire;
294c793af95Ssangeeta 
295c793af95Ssangeeta 		/*
296c793af95Ssangeeta 		 * Currently MATCH_IRE_ILL is never used with
297c793af95Ssangeeta 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
298c793af95Ssangeeta 		 * sending out packets as MATCH_IRE_ILL is used only
299c793af95Ssangeeta 		 * for communicating with on-link hosts. We can't assert
300c793af95Ssangeeta 		 * that here as RTM_GET calls this function with
301c793af95Ssangeeta 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
302c793af95Ssangeeta 		 * We have already used the MATCH_IRE_ILL in determining
303c793af95Ssangeeta 		 * the right prefix route at this point. To match the
304c793af95Ssangeeta 		 * behavior of how we locate routes while sending out
305c793af95Ssangeeta 		 * packets, we don't want to use MATCH_IRE_ILL below
306c793af95Ssangeeta 		 * while locating the interface route.
307c793af95Ssangeeta 		 *
308c793af95Ssangeeta 		 * ire_ftable_lookup may end up with an incomplete IRE_CACHE
309c793af95Ssangeeta 		 * entry for the gateway (i.e., one for which the
310c793af95Ssangeeta 		 * ire_nce->nce_state is not yet ND_REACHABLE). If the caller
311c793af95Ssangeeta 		 * has specified MATCH_IRE_COMPLETE, such entries will not
312c793af95Ssangeeta 		 * be returned; instead, we return the IF_RESOLVER ire.
313c793af95Ssangeeta 		 */
314c793af95Ssangeeta 		if (ire->ire_ipif != NULL)
315c793af95Ssangeeta 			match_flags |= MATCH_IRE_ILL_GROUP;
316c793af95Ssangeeta 
317c793af95Ssangeeta 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
318f4b3ec61Sdh155122 		    ire->ire_ipif, NULL, zoneid, tsl, match_flags, ipst);
319c793af95Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire,
320c793af95Ssangeeta 		    (ire_t *), save_ire);
321c793af95Ssangeeta 		if (ire == NULL ||
322c793af95Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
323c793af95Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
324c793af95Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
325c793af95Ssangeeta 			/*
326c793af95Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
327c793af95Ssangeeta 			 * is set. Also return it via ire.
328c793af95Ssangeeta 			 */
329c793af95Ssangeeta 			if (ire != NULL) {
330c793af95Ssangeeta 				ire_refrele(ire);
331c793af95Ssangeeta 				ire = NULL;
332c793af95Ssangeeta 				found_incomplete = B_TRUE;
333c793af95Ssangeeta 			}
334c793af95Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
335c793af95Ssangeeta 				if (pire != NULL) {
336c793af95Ssangeeta 					/*
337c793af95Ssangeeta 					 * Need an extra REFHOLD, if the parent
338c793af95Ssangeeta 					 * ire is returned via both ire and
339c793af95Ssangeeta 					 * pire.
340c793af95Ssangeeta 					 */
341c793af95Ssangeeta 					IRE_REFHOLD(save_ire);
342c793af95Ssangeeta 				}
343c793af95Ssangeeta 				ire = save_ire;
344c793af95Ssangeeta 			} else {
345c793af95Ssangeeta 				ire_refrele(save_ire);
346c793af95Ssangeeta 				if (pire != NULL)
347c793af95Ssangeeta 					*pire = NULL;
348c793af95Ssangeeta 			}
349c793af95Ssangeeta 			if (!found_incomplete)
350c793af95Ssangeeta 				return (ire);
351c793af95Ssangeeta 		}
352c793af95Ssangeeta 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
353c793af95Ssangeeta 			/*
354c793af95Ssangeeta 			 * If the caller did not ask for pire, release
355c793af95Ssangeeta 			 * it now.
356c793af95Ssangeeta 			 */
357c793af95Ssangeeta 			if (pire == NULL) {
358c793af95Ssangeeta 				ire_refrele(save_ire);
359c793af95Ssangeeta 			}
360c793af95Ssangeeta 			return (ire);
361c793af95Ssangeeta 		}
362c793af95Ssangeeta 		match_flags |= MATCH_IRE_TYPE;
363c793af95Ssangeeta 		gw_addr = ire->ire_gateway_addr;
364c793af95Ssangeeta 		gw_ipif = ire->ire_ipif;
365c793af95Ssangeeta 		ire_refrele(ire);
366c793af95Ssangeeta 		ire = ire_route_lookup(gw_addr, 0, 0,
367c793af95Ssangeeta 		    (found_incomplete? IRE_INTERFACE :
368c793af95Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)),
369f4b3ec61Sdh155122 		    gw_ipif, NULL, zoneid, tsl, match_flags, ipst);
370c793af95Ssangeeta 		DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire,
371c793af95Ssangeeta 		    (ire_t *), save_ire);
372c793af95Ssangeeta 		if (ire == NULL ||
373c793af95Ssangeeta 		    ((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
374c793af95Ssangeeta 		    ire->ire_nce->nce_state != ND_REACHABLE &&
375c793af95Ssangeeta 		    (flags & MATCH_IRE_COMPLETE))) {
376c793af95Ssangeeta 			/*
377c793af95Ssangeeta 			 * Do not release the parent ire if MATCH_IRE_PARENT
378c793af95Ssangeeta 			 * is set. Also return it via ire.
379c793af95Ssangeeta 			 */
380c793af95Ssangeeta 			if (ire != NULL) {
381c793af95Ssangeeta 				ire_refrele(ire);
382c793af95Ssangeeta 				ire = NULL;
383c793af95Ssangeeta 			}
384c793af95Ssangeeta 			if (flags & MATCH_IRE_PARENT) {
385c793af95Ssangeeta 				if (pire != NULL) {
386c793af95Ssangeeta 					/*
387c793af95Ssangeeta 					 * Need an extra REFHOLD, if the
388c793af95Ssangeeta 					 * parent ire is returned via both
389c793af95Ssangeeta 					 * ire and pire.
390c793af95Ssangeeta 					 */
391c793af95Ssangeeta 					IRE_REFHOLD(save_ire);
392c793af95Ssangeeta 				}
393c793af95Ssangeeta 				ire = save_ire;
394c793af95Ssangeeta 			} else {
395c793af95Ssangeeta 				ire_refrele(save_ire);
396c793af95Ssangeeta 				if (pire != NULL)
397c793af95Ssangeeta 					*pire = NULL;
398c793af95Ssangeeta 			}
399c793af95Ssangeeta 			return (ire);
400c793af95Ssangeeta 		} else if (pire == NULL) {
401c793af95Ssangeeta 			/*
402c793af95Ssangeeta 			 * If the caller did not ask for pire, release
403c793af95Ssangeeta 			 * it now.
404c793af95Ssangeeta 			 */
405c793af95Ssangeeta 			ire_refrele(save_ire);
406c793af95Ssangeeta 		}
407c793af95Ssangeeta 		return (ire);
408c793af95Ssangeeta 	}
409c793af95Ssangeeta 	ASSERT(pire == NULL || *pire == NULL);
410c793af95Ssangeeta 	return (ire);
411c793af95Ssangeeta }
412c793af95Ssangeeta 
413c793af95Ssangeeta 
414c793af95Ssangeeta /*
415c793af95Ssangeeta  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
416c793af95Ssangeeta  * that goes through 'ipif'. As a fallback, a route that goes through
417c793af95Ssangeeta  * ipif->ipif_ill can be returned.
418c793af95Ssangeeta  */
419c793af95Ssangeeta ire_t *
420c793af95Ssangeeta ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
421c793af95Ssangeeta {
422c793af95Ssangeeta 	ire_t	*ire;
423c793af95Ssangeeta 	ire_t	*save_ire = NULL;
424c793af95Ssangeeta 	ire_t   *gw_ire;
425c793af95Ssangeeta 	irb_t   *irb;
426c793af95Ssangeeta 	ipaddr_t gw_addr;
427c793af95Ssangeeta 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
428f4b3ec61Sdh155122 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
429c793af95Ssangeeta 
430c793af95Ssangeeta 	ASSERT(CLASSD(group));
431c793af95Ssangeeta 
432c793af95Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
433f4b3ec61Sdh155122 	    NULL, MATCH_IRE_DEFAULT, ipst);
434c793af95Ssangeeta 
435c793af95Ssangeeta 	if (ire == NULL)
436c793af95Ssangeeta 		return (NULL);
437c793af95Ssangeeta 
438c793af95Ssangeeta 	irb = ire->ire_bucket;
439c793af95Ssangeeta 	ASSERT(irb);
440c793af95Ssangeeta 
441c793af95Ssangeeta 	IRB_REFHOLD(irb);
442c793af95Ssangeeta 	ire_refrele(ire);
443c793af95Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
444c793af95Ssangeeta 		if (ire->ire_addr != group ||
445c793af95Ssangeeta 		    ipif->ipif_zoneid != ire->ire_zoneid &&
446c793af95Ssangeeta 		    ire->ire_zoneid != ALL_ZONES) {
447c793af95Ssangeeta 			continue;
448c793af95Ssangeeta 		}
449c793af95Ssangeeta 
450c793af95Ssangeeta 		switch (ire->ire_type) {
451c793af95Ssangeeta 		case IRE_DEFAULT:
452c793af95Ssangeeta 		case IRE_PREFIX:
453c793af95Ssangeeta 		case IRE_HOST:
454c793af95Ssangeeta 			gw_addr = ire->ire_gateway_addr;
455c793af95Ssangeeta 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
456f4b3ec61Sdh155122 			    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
457c793af95Ssangeeta 
458c793af95Ssangeeta 			if (gw_ire != NULL) {
459c793af95Ssangeeta 				if (save_ire != NULL) {
460c793af95Ssangeeta 					ire_refrele(save_ire);
461c793af95Ssangeeta 				}
462c793af95Ssangeeta 				IRE_REFHOLD(ire);
463c793af95Ssangeeta 				if (gw_ire->ire_ipif == ipif) {
464c793af95Ssangeeta 					ire_refrele(gw_ire);
465c793af95Ssangeeta 
466c793af95Ssangeeta 					IRB_REFRELE(irb);
467c793af95Ssangeeta 					return (ire);
468c793af95Ssangeeta 				}
469c793af95Ssangeeta 				ire_refrele(gw_ire);
470c793af95Ssangeeta 				save_ire = ire;
471c793af95Ssangeeta 			}
472c793af95Ssangeeta 			break;
473c793af95Ssangeeta 		case IRE_IF_NORESOLVER:
474c793af95Ssangeeta 		case IRE_IF_RESOLVER:
475c793af95Ssangeeta 			if (ire->ire_ipif == ipif) {
476c793af95Ssangeeta 				if (save_ire != NULL) {
477c793af95Ssangeeta 					ire_refrele(save_ire);
478c793af95Ssangeeta 				}
479c793af95Ssangeeta 				IRE_REFHOLD(ire);
480c793af95Ssangeeta 
481c793af95Ssangeeta 				IRB_REFRELE(irb);
482c793af95Ssangeeta 				return (ire);
483c793af95Ssangeeta 			}
484c793af95Ssangeeta 			break;
485c793af95Ssangeeta 		}
486c793af95Ssangeeta 	}
487c793af95Ssangeeta 	IRB_REFRELE(irb);
488c793af95Ssangeeta 
489c793af95Ssangeeta 	return (save_ire);
490c793af95Ssangeeta }
491c793af95Ssangeeta 
492c793af95Ssangeeta /*
493c793af95Ssangeeta  * Find an IRE_INTERFACE for the multicast group.
494c793af95Ssangeeta  * Allows different routes for multicast addresses
495c793af95Ssangeeta  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
496c793af95Ssangeeta  * which point at different interfaces. This is used when IP_MULTICAST_IF
497c793af95Ssangeeta  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
498c793af95Ssangeeta  * specify the interface to join on.
499c793af95Ssangeeta  *
500c793af95Ssangeeta  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
501c793af95Ssangeeta  */
502c793af95Ssangeeta ire_t *
503f4b3ec61Sdh155122 ire_lookup_multi(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
504c793af95Ssangeeta {
505c793af95Ssangeeta 	ire_t	*ire;
506c793af95Ssangeeta 	ipif_t	*ipif = NULL;
507c793af95Ssangeeta 	int	match_flags = MATCH_IRE_TYPE;
508c793af95Ssangeeta 	ipaddr_t gw_addr;
509c793af95Ssangeeta 
510c793af95Ssangeeta 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
511f4b3ec61Sdh155122 	    0, NULL, MATCH_IRE_DEFAULT, ipst);
512c793af95Ssangeeta 
513c793af95Ssangeeta 	/* We search a resolvable ire in case of multirouting. */
514c793af95Ssangeeta 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
515c793af95Ssangeeta 		ire_t *cire = NULL;
516c793af95Ssangeeta 		/*
517c793af95Ssangeeta 		 * If the route is not resolvable, the looked up ire
518c793af95Ssangeeta 		 * may be changed here. In that case, ire_multirt_lookup()
519c793af95Ssangeeta 		 * IRE_REFRELE the original ire and change it.
520c793af95Ssangeeta 		 */
521f4b3ec61Sdh155122 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW,
522f4b3ec61Sdh155122 		    NULL, ipst);
523c793af95Ssangeeta 		if (cire != NULL)
524c793af95Ssangeeta 			ire_refrele(cire);
525c793af95Ssangeeta 	}
526c793af95Ssangeeta 	if (ire == NULL)
527c793af95Ssangeeta 		return (NULL);
528c793af95Ssangeeta 	/*
529c793af95Ssangeeta 	 * Make sure we follow ire_ipif.
530c793af95Ssangeeta 	 *
531c793af95Ssangeeta 	 * We need to determine the interface route through
532c793af95Ssangeeta 	 * which the gateway will be reached. We don't really
533c793af95Ssangeeta 	 * care which interface is picked if the interface is
534c793af95Ssangeeta 	 * part of a group.
535c793af95Ssangeeta 	 */
536c793af95Ssangeeta 	if (ire->ire_ipif != NULL) {
537c793af95Ssangeeta 		ipif = ire->ire_ipif;
538c793af95Ssangeeta 		match_flags |= MATCH_IRE_ILL_GROUP;
539c793af95Ssangeeta 	}
540c793af95Ssangeeta 
541c793af95Ssangeeta 	switch (ire->ire_type) {
542c793af95Ssangeeta 	case IRE_DEFAULT:
543c793af95Ssangeeta 	case IRE_PREFIX:
544c793af95Ssangeeta 	case IRE_HOST:
545c793af95Ssangeeta 		gw_addr = ire->ire_gateway_addr;
546c793af95Ssangeeta 		ire_refrele(ire);
547c793af95Ssangeeta 		ire = ire_ftable_lookup(gw_addr, 0, 0,
548c793af95Ssangeeta 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
549f4b3ec61Sdh155122 		    NULL, match_flags, ipst);
550c793af95Ssangeeta 		return (ire);
551c793af95Ssangeeta 	case IRE_IF_NORESOLVER:
552c793af95Ssangeeta 	case IRE_IF_RESOLVER:
553c793af95Ssangeeta 		return (ire);
554c793af95Ssangeeta 	default:
555c793af95Ssangeeta 		ire_refrele(ire);
556c793af95Ssangeeta 		return (NULL);
557c793af95Ssangeeta 	}
558c793af95Ssangeeta }
559c793af95Ssangeeta 
560c793af95Ssangeeta /*
561c793af95Ssangeeta  * Delete the passed in ire if the gateway addr matches
562c793af95Ssangeeta  */
563c793af95Ssangeeta void
564c793af95Ssangeeta ire_del_host_redir(ire_t *ire, char *gateway)
565c793af95Ssangeeta {
5666bdb8e66Sdd193516 	if ((ire->ire_flags & RTF_DYNAMIC) &&
567c793af95Ssangeeta 	    (ire->ire_gateway_addr == *(ipaddr_t *)gateway))
568c793af95Ssangeeta 		ire_delete(ire);
569c793af95Ssangeeta }
570c793af95Ssangeeta 
571c793af95Ssangeeta /*
572c793af95Ssangeeta  * Search for all HOST REDIRECT routes that are
573c793af95Ssangeeta  * pointing at the specified gateway and
574c793af95Ssangeeta  * delete them. This routine is called only
575c793af95Ssangeeta  * when a default gateway is going away.
576c793af95Ssangeeta  */
577c793af95Ssangeeta void
578f4b3ec61Sdh155122 ire_delete_host_redirects(ipaddr_t gateway, ip_stack_t *ipst)
579c793af95Ssangeeta {
580c793af95Ssangeeta 	struct rtfuncarg rtfarg;
581c793af95Ssangeeta 
582c793af95Ssangeeta 	(void) memset(&rtfarg, 0, sizeof (rtfarg));
583c793af95Ssangeeta 	rtfarg.rt_func = ire_del_host_redir;
584c793af95Ssangeeta 	rtfarg.rt_arg = (void *)&gateway;
585f4b3ec61Sdh155122 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
586f4b3ec61Sdh155122 	    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
587c793af95Ssangeeta }
588c793af95Ssangeeta 
589c793af95Ssangeeta struct ihandle_arg {
590c793af95Ssangeeta 	uint32_t ihandle;
591c793af95Ssangeeta 	ire_t	 *ire;
592c793af95Ssangeeta };
593c793af95Ssangeeta 
594c793af95Ssangeeta static int
595c793af95Ssangeeta ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
596c793af95Ssangeeta {
597c793af95Ssangeeta 	struct rt_entry *rt;
598c793af95Ssangeeta 	irb_t *irb;
599c793af95Ssangeeta 	ire_t *ire;
600c793af95Ssangeeta 	struct ihandle_arg *ih = arg;
601c793af95Ssangeeta 
602c793af95Ssangeeta 	rt = (struct rt_entry *)rn;
603c793af95Ssangeeta 	ASSERT(rt != NULL);
604c793af95Ssangeeta 	irb = &rt->rt_irb;
605c793af95Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
606c793af95Ssangeeta 		if ((ire->ire_type & IRE_INTERFACE) &&
607c793af95Ssangeeta 		    (ire->ire_ihandle == ih->ihandle)) {
608c793af95Ssangeeta 			ih->ire = ire;
609c793af95Ssangeeta 			IRE_REFHOLD(ire);
610c793af95Ssangeeta 			return (1);
611c793af95Ssangeeta 		}
612c793af95Ssangeeta 	}
613c793af95Ssangeeta 	return (0);
614c793af95Ssangeeta }
615c793af95Ssangeeta 
616c793af95Ssangeeta /*
617c793af95Ssangeeta  * Locate the interface ire that is tied to the cache ire 'cire' via
618c793af95Ssangeeta  * cire->ire_ihandle.
619c793af95Ssangeeta  *
620c793af95Ssangeeta  * We are trying to create the cache ire for an onlink destn. or
621c793af95Ssangeeta  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
622c793af95Ssangeeta  * case, after the ire has come back from ARP.
623c793af95Ssangeeta  */
624c793af95Ssangeeta ire_t *
625c793af95Ssangeeta ire_ihandle_lookup_onlink(ire_t *cire)
626c793af95Ssangeeta {
627c793af95Ssangeeta 	ire_t	*ire;
628c793af95Ssangeeta 	int	match_flags;
629c793af95Ssangeeta 	struct ihandle_arg ih;
630f4b3ec61Sdh155122 	ip_stack_t *ipst;
631c793af95Ssangeeta 
632c793af95Ssangeeta 	ASSERT(cire != NULL);
633f4b3ec61Sdh155122 	ipst = cire->ire_ipst;
634c793af95Ssangeeta 
635c793af95Ssangeeta 	/*
636c793af95Ssangeeta 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
637c793af95Ssangeeta 	 * because the ihandle refers to an ipif which can be in only one zone.
638c793af95Ssangeeta 	 */
639c793af95Ssangeeta 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
640c793af95Ssangeeta 	/*
641c793af95Ssangeeta 	 * We know that the mask of the interface ire equals cire->ire_cmask.
642c793af95Ssangeeta 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
643c793af95Ssangeeta 	 * cmask from the interface ire's mask)
644c793af95Ssangeeta 	 */
645c793af95Ssangeeta 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
646c793af95Ssangeeta 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
647f4b3ec61Sdh155122 	    NULL, match_flags, ipst);
648c793af95Ssangeeta 	if (ire != NULL)
649c793af95Ssangeeta 		return (ire);
650c793af95Ssangeeta 	/*
651c793af95Ssangeeta 	 * If we didn't find an interface ire above, we can't declare failure.
652c793af95Ssangeeta 	 * For backwards compatibility, we need to support prefix routes
653c793af95Ssangeeta 	 * pointing to next hop gateways that are not on-link.
654c793af95Ssangeeta 	 *
655c793af95Ssangeeta 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
656c793af95Ssangeeta 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
657c793af95Ssangeeta 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
658c793af95Ssangeeta 	 * doing ire_route_lookup() twice and returning an interface ire.
659c793af95Ssangeeta 	 *
660c793af95Ssangeeta 	 * Eg. default	-	gw1			(line 1)
661c793af95Ssangeeta 	 *	gw1	-	gw2			(line 2)
662c793af95Ssangeeta 	 *	gw2	-	hme0			(line 3)
663c793af95Ssangeeta 	 *
664c793af95Ssangeeta 	 * In the above example, ip_newroute() tried to create the cache ire
665c793af95Ssangeeta 	 * 'cire' for gw1, based on the interface route in line 3. The
666c793af95Ssangeeta 	 * ire_ftable_lookup() above fails, because there is no interface route
667c793af95Ssangeeta 	 * to reach gw1. (it is gw2). We fall thru below.
668c793af95Ssangeeta 	 *
669c793af95Ssangeeta 	 * Do a brute force search based on the ihandle in a subset of the
670c793af95Ssangeeta 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
671c793af95Ssangeeta 	 * things become very complex, since we don't have 'pire' in this
672c793af95Ssangeeta 	 * case. (Also note that this method is not possible in the offlink
673c793af95Ssangeeta 	 * case because we don't know the mask)
674c793af95Ssangeeta 	 */
675c793af95Ssangeeta 	(void) memset(&ih, 0, sizeof (ih));
676c793af95Ssangeeta 	ih.ihandle = cire->ire_ihandle;
677f4b3ec61Sdh155122 	(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
678f4b3ec61Sdh155122 	    ire_ihandle_onlink_match, &ih, irb_refhold_rn, irb_refrele_rn);
679c793af95Ssangeeta 	return (ih.ire);
680c793af95Ssangeeta }
681c793af95Ssangeeta 
682c793af95Ssangeeta /*
683c793af95Ssangeeta  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
684c793af95Ssangeeta  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
685c793af95Ssangeeta  * in the bucket skipping default interface routes and deleted entries.
686c793af95Ssangeeta  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
687c793af95Ssangeeta  * Assumes that the caller holds a reference on the IRE bucket.
688c793af95Ssangeeta  */
689c793af95Ssangeeta ire_t *
690c793af95Ssangeeta ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
691c793af95Ssangeeta {
692c793af95Ssangeeta 	ASSERT(ire_origin->ire_bucket != NULL);
693c793af95Ssangeeta 	ASSERT(ire != NULL);
694c793af95Ssangeeta 
695c793af95Ssangeeta 	do {
696c793af95Ssangeeta 		ire = ire->ire_next;
697c793af95Ssangeeta 		if (ire == NULL)
698c793af95Ssangeeta 			ire = ire_origin->ire_bucket->irb_ire;
699c793af95Ssangeeta 		if (ire == ire_origin)
700c793af95Ssangeeta 			return (NULL);
701c793af95Ssangeeta 	} while ((ire->ire_type & IRE_INTERFACE) ||
702c793af95Ssangeeta 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
703c793af95Ssangeeta 	ASSERT(ire != NULL);
704c793af95Ssangeeta 	return (ire);
705c793af95Ssangeeta }
706c793af95Ssangeeta 
707c793af95Ssangeeta static ipif_t *
708c793af95Ssangeeta ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
709c793af95Ssangeeta     int zoneid, ushort_t *marks)
710c793af95Ssangeeta {
711c793af95Ssangeeta 	ipif_t *src_ipif;
712f4b3ec61Sdh155122 	ip_stack_t *ipst = dst_ill->ill_ipst;
713c793af95Ssangeeta 
714c793af95Ssangeeta 	/*
715c793af95Ssangeeta 	 * Pick the best source address from dst_ill.
716c793af95Ssangeeta 	 *
717c793af95Ssangeeta 	 * 1) If it is part of a multipathing group, we would
718c793af95Ssangeeta 	 *    like to spread the inbound packets across different
719c793af95Ssangeeta 	 *    interfaces. ipif_select_source picks a random source
720c793af95Ssangeeta 	 *    across the different ills in the group.
721c793af95Ssangeeta 	 *
722c793af95Ssangeeta 	 * 2) If it is not part of a multipathing group, we try
723c793af95Ssangeeta 	 *    to pick the source address from the destination
724c793af95Ssangeeta 	 *    route. Clustering assumes that when we have multiple
725c793af95Ssangeeta 	 *    prefixes hosted on an interface, the prefix of the
726c793af95Ssangeeta 	 *    source address matches the prefix of the destination
727c793af95Ssangeeta 	 *    route. We do this only if the address is not
728c793af95Ssangeeta 	 *    DEPRECATED.
729c793af95Ssangeeta 	 *
730c793af95Ssangeeta 	 * 3) If the conn is in a different zone than the ire, we
731c793af95Ssangeeta 	 *    need to pick a source address from the right zone.
732c793af95Ssangeeta 	 *
733c793af95Ssangeeta 	 * NOTE : If we hit case (1) above, the prefix of the source
734c793af95Ssangeeta 	 *	  address picked may not match the prefix of the
735c793af95Ssangeeta 	 *	  destination routes prefix as ipif_select_source
736c793af95Ssangeeta 	 *	  does not look at "dst" while picking a source
737c793af95Ssangeeta 	 *	  address.
738c793af95Ssangeeta 	 *	  If we want the same behavior as (2), we will need
739c793af95Ssangeeta 	 *	  to change the behavior of ipif_select_source.
740c793af95Ssangeeta 	 */
741c793af95Ssangeeta 
742c793af95Ssangeeta 	if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
743c793af95Ssangeeta 		/*
744c793af95Ssangeeta 		 * The RTF_SETSRC flag is set in the parent ire (sire).
745c793af95Ssangeeta 		 * Check that the ipif matching the requested source
746c793af95Ssangeeta 		 * address still exists.
747c793af95Ssangeeta 		 */
748c793af95Ssangeeta 		src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
749f4b3ec61Sdh155122 		    zoneid, NULL, NULL, NULL, NULL, ipst);
750c793af95Ssangeeta 		return (src_ipif);
751c793af95Ssangeeta 	}
752c793af95Ssangeeta 	*marks |= IRE_MARK_USESRC_CHECK;
753c793af95Ssangeeta 	if ((dst_ill->ill_group != NULL) ||
754c793af95Ssangeeta 	    (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
755c793af95Ssangeeta 	    (dst_ill->ill_usesrc_ifindex != 0)) {
756c793af95Ssangeeta 		src_ipif = ipif_select_source(dst_ill, dst, zoneid);
757c793af95Ssangeeta 		if (src_ipif == NULL)
758c793af95Ssangeeta 			return (NULL);
759c793af95Ssangeeta 
760c793af95Ssangeeta 	} else {
761c793af95Ssangeeta 		src_ipif = ire->ire_ipif;
762c793af95Ssangeeta 		ASSERT(src_ipif != NULL);
763c793af95Ssangeeta 		/* hold src_ipif for uniformity */
764c793af95Ssangeeta 		ipif_refhold(src_ipif);
765c793af95Ssangeeta 	}
766c793af95Ssangeeta 	return (src_ipif);
767c793af95Ssangeeta }
768c793af95Ssangeeta 
769c793af95Ssangeeta /*
770c793af95Ssangeeta  * This function is called by ip_rput_noire() and ip_fast_forward()
771c793af95Ssangeeta  * to resolve the route of incoming packet that needs to be forwarded.
772c793af95Ssangeeta  * If the ire of the nexthop is not already in the cachetable, this
773c793af95Ssangeeta  * routine will insert it to the table, but won't trigger ARP resolution yet.
774c793af95Ssangeeta  * Thus unlike ip_newroute, this function adds incomplete ires to
775c793af95Ssangeeta  * the cachetable. ARP resolution for these ires are  delayed until
776c793af95Ssangeeta  * after all of the packet processing is completed and its ready to
777c793af95Ssangeeta  * be sent out on the wire, Eventually, the packet transmit routine
778c793af95Ssangeeta  * ip_xmit_v4() attempts to send a packet  to the driver. If it finds
779c793af95Ssangeeta  * that there is no link layer information, it will do the arp
780c793af95Ssangeeta  * resolution and queue the packet in ire->ire_nce->nce_qd_mp and
781c793af95Ssangeeta  * then send it out once the arp resolution is over
782c793af95Ssangeeta  * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
783c793af95Ssangeeta  * the model of BSD/SunOS 4
784c793af95Ssangeeta  *
785c793af95Ssangeeta  * In future, the insertion of incomplete ires in the cachetable should
786c793af95Ssangeeta  * be implemented in hostpath as well, as doing so will greatly reduce
787c793af95Ssangeeta  * the existing complexity for code paths that depend on the context of
788c793af95Ssangeeta  * the sender (such as IPsec).
789c793af95Ssangeeta  *
790c793af95Ssangeeta  * Thus this scheme of adding incomplete ires in cachetable in forwarding
791c793af95Ssangeeta  * path can be used as a template for simplifying the hostpath.
792c793af95Ssangeeta  */
793c793af95Ssangeeta 
794c793af95Ssangeeta ire_t *
795c793af95Ssangeeta ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
796f4b3ec61Sdh155122     ire_t *supplied_sire, const struct ts_label_s *tsl, ip_stack_t *ipst)
797c793af95Ssangeeta {
798c793af95Ssangeeta 	ipaddr_t gw = 0;
799c793af95Ssangeeta 	ire_t	*ire = NULL;
800c793af95Ssangeeta 	ire_t   *sire = NULL, *save_ire;
801c793af95Ssangeeta 	ill_t *dst_ill = NULL;
802c793af95Ssangeeta 	int error;
803c793af95Ssangeeta 	zoneid_t zoneid;
804c793af95Ssangeeta 	ipif_t *src_ipif = NULL;
805c793af95Ssangeeta 	mblk_t *res_mp;
806c793af95Ssangeeta 	ushort_t ire_marks = 0;
807c793af95Ssangeeta 	tsol_gcgrp_t *gcgrp = NULL;
808c793af95Ssangeeta 	tsol_gcgrp_addr_t ga;
809c793af95Ssangeeta 
810c793af95Ssangeeta 	zoneid = GLOBAL_ZONEID;
811c793af95Ssangeeta 
812c793af95Ssangeeta 	if (supplied_ire != NULL) {
813c793af95Ssangeeta 		/* We have arrived here from ipfil_sendpkt */
814c793af95Ssangeeta 		ire = supplied_ire;
815c793af95Ssangeeta 		sire = supplied_sire;
816c793af95Ssangeeta 		goto create_irecache;
817c793af95Ssangeeta 	}
818c793af95Ssangeeta 
819c793af95Ssangeeta 	ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
820c793af95Ssangeeta 	    tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
821f4b3ec61Sdh155122 	    MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR, ipst);
822c793af95Ssangeeta 
823c793af95Ssangeeta 	if (ire == NULL) {
824f4b3ec61Sdh155122 		ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst);
825c793af95Ssangeeta 		goto icmp_err_ret;
826c793af95Ssangeeta 	}
827c793af95Ssangeeta 
828c793af95Ssangeeta 	/*
829c793af95Ssangeeta 	 * If we encounter CGTP, we should  have the caller use
830c793af95Ssangeeta 	 * ip_newroute to resolve multirt instead of this function.
831c793af95Ssangeeta 	 * CGTP specs explicitly state that it can't be used with routers.
832c793af95Ssangeeta 	 * This essentially prevents insertion of incomplete RTF_MULTIRT
833c793af95Ssangeeta 	 * ires in cachetable.
834c793af95Ssangeeta 	 */
835c793af95Ssangeeta 	if (ip_cgtp_filter &&
836c793af95Ssangeeta 	    ((ire->ire_flags & RTF_MULTIRT) ||
837c793af95Ssangeeta 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
838c793af95Ssangeeta 		ip3dbg(("ire_forward: packet is to be multirouted- "
839c793af95Ssangeeta 		    "handing it to ip_newroute\n"));
840c793af95Ssangeeta 		if (sire != NULL)
841c793af95Ssangeeta 			ire_refrele(sire);
842c793af95Ssangeeta 		ire_refrele(ire);
843c793af95Ssangeeta 		/*
844c793af95Ssangeeta 		 * Inform caller about encountering of multirt so that
845c793af95Ssangeeta 		 * ip_newroute() can be called.
846c793af95Ssangeeta 		 */
847c793af95Ssangeeta 		*check_multirt = B_TRUE;
848c793af95Ssangeeta 		return (NULL);
849c793af95Ssangeeta 	}
850c793af95Ssangeeta 
851c793af95Ssangeeta 	*check_multirt = B_FALSE;
852c793af95Ssangeeta 
853c793af95Ssangeeta 	/*
854c793af95Ssangeeta 	 * Verify that the returned IRE does not have either
855c793af95Ssangeeta 	 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
856c793af95Ssangeeta 	 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
857c793af95Ssangeeta 	 */
858c793af95Ssangeeta 	if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
859c793af95Ssangeeta 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
860c793af95Ssangeeta 		ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
861c793af95Ssangeeta 		    (void *)ire));
862c793af95Ssangeeta 		goto icmp_err_ret;
863c793af95Ssangeeta 	}
864c793af95Ssangeeta 
865c793af95Ssangeeta 	/*
866c793af95Ssangeeta 	 * If we already have a fully resolved IRE CACHE of the
867c793af95Ssangeeta 	 * nexthop router, just hand over the cache entry
868c793af95Ssangeeta 	 * and we are done.
869c793af95Ssangeeta 	 */
870c793af95Ssangeeta 
871c793af95Ssangeeta 	if (ire->ire_type & IRE_CACHE) {
872c793af95Ssangeeta 
873c793af95Ssangeeta 		/*
874c793af95Ssangeeta 		 * If we are using this ire cache entry as a
875c793af95Ssangeeta 		 * gateway to forward packets, chances are we
876c793af95Ssangeeta 		 * will be using it again. So turn off
877c793af95Ssangeeta 		 * the temporary flag, thus reducing its
878c793af95Ssangeeta 		 * chances of getting deleted frequently.
879c793af95Ssangeeta 		 */
880c793af95Ssangeeta 		if (ire->ire_marks & IRE_MARK_TEMPORARY) {
881c793af95Ssangeeta 			irb_t *irb = ire->ire_bucket;
882c793af95Ssangeeta 			rw_enter(&irb->irb_lock, RW_WRITER);
883c793af95Ssangeeta 			ire->ire_marks &= ~IRE_MARK_TEMPORARY;
884c793af95Ssangeeta 			irb->irb_tmp_ire_cnt--;
885c793af95Ssangeeta 			rw_exit(&irb->irb_lock);
886c793af95Ssangeeta 		}
887c793af95Ssangeeta 
888c793af95Ssangeeta 		if (sire != NULL) {
889c793af95Ssangeeta 			UPDATE_OB_PKT_COUNT(sire);
890c793af95Ssangeeta 			sire->ire_last_used_time = lbolt;
891c793af95Ssangeeta 			ire_refrele(sire);
892c793af95Ssangeeta 		}
893c793af95Ssangeeta 		return (ire);
894c793af95Ssangeeta 	}
895c793af95Ssangeeta create_irecache:
896c793af95Ssangeeta 	/*
897c793af95Ssangeeta 	 * Increment the ire_ob_pkt_count field for ire if it is an
898c793af95Ssangeeta 	 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
899c793af95Ssangeeta 	 * increment the same for the parent IRE, sire, if it is some
90027c48ed9Snordmark 	 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST).
901c793af95Ssangeeta 	 */
902c793af95Ssangeeta 	if ((ire->ire_type & IRE_INTERFACE) != 0) {
903c793af95Ssangeeta 		UPDATE_OB_PKT_COUNT(ire);
904c793af95Ssangeeta 		ire->ire_last_used_time = lbolt;
905c793af95Ssangeeta 	}
906c793af95Ssangeeta 
907c793af95Ssangeeta 	/*
908c793af95Ssangeeta 	 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
909c793af95Ssangeeta 	 */
910c793af95Ssangeeta 	if (sire != NULL) {
911c793af95Ssangeeta 		gw = sire->ire_gateway_addr;
912c793af95Ssangeeta 		ASSERT((sire->ire_type &
913c793af95Ssangeeta 		    (IRE_CACHETABLE | IRE_INTERFACE)) == 0);
914c793af95Ssangeeta 		UPDATE_OB_PKT_COUNT(sire);
915c793af95Ssangeeta 		sire->ire_last_used_time = lbolt;
916c793af95Ssangeeta 	}
917c793af95Ssangeeta 
918c793af95Ssangeeta 	/* Obtain dst_ill */
919c793af95Ssangeeta 	dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
920c793af95Ssangeeta 	if (dst_ill == NULL) {
921c793af95Ssangeeta 		ip2dbg(("ire_forward no dst ill; ire 0x%p\n",
922c793af95Ssangeeta 			(void *)ire));
923c793af95Ssangeeta 		goto icmp_err_ret;
924c793af95Ssangeeta 	}
925c793af95Ssangeeta 
926c793af95Ssangeeta 	ASSERT(src_ipif == NULL);
927c793af95Ssangeeta 	/* Now obtain the src_ipif */
928c793af95Ssangeeta 	src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
929c793af95Ssangeeta 	    zoneid, &ire_marks);
930c793af95Ssangeeta 	if (src_ipif == NULL)
931c793af95Ssangeeta 		goto icmp_err_ret;
932c793af95Ssangeeta 
933c793af95Ssangeeta 	switch (ire->ire_type) {
934c793af95Ssangeeta 	case IRE_IF_NORESOLVER:
935c793af95Ssangeeta 		/* create ire_cache for ire_addr endpoint */
936c793af95Ssangeeta 	case IRE_IF_RESOLVER:
937c793af95Ssangeeta 		/*
938c793af95Ssangeeta 		 * We have the IRE_IF_RESOLVER of the nexthop gateway
939c793af95Ssangeeta 		 * and now need to build a IRE_CACHE for it.
940c793af95Ssangeeta 		 * In this case, we have the following :
941c793af95Ssangeeta 		 *
942c793af95Ssangeeta 		 * 1) src_ipif - used for getting a source address.
943c793af95Ssangeeta 		 *
944c793af95Ssangeeta 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
945c793af95Ssangeeta 		 *    means packets using the IRE_CACHE that we will build
946c793af95Ssangeeta 		 *    here will go out on dst_ill.
947c793af95Ssangeeta 		 *
948c793af95Ssangeeta 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
949c793af95Ssangeeta 		 *    to be created will only be tied to the IRE_INTERFACE
950c793af95Ssangeeta 		 *    that was derived from the ire_ihandle field.
951c793af95Ssangeeta 		 *
952c793af95Ssangeeta 		 *    If sire is non-NULL, it means the destination is
953c793af95Ssangeeta 		 *    off-link and we will first create the IRE_CACHE for the
954c793af95Ssangeeta 		 *    gateway.
955c793af95Ssangeeta 		 */
956c793af95Ssangeeta 		res_mp = dst_ill->ill_resolver_mp;
957c793af95Ssangeeta 		if (ire->ire_type == IRE_IF_RESOLVER &&
958c793af95Ssangeeta 		    (!OK_RESOLVER_MP(res_mp))) {
959c793af95Ssangeeta 			ire_refrele(ire);
960c793af95Ssangeeta 			ire = NULL;
961c793af95Ssangeeta 			goto out;
962c793af95Ssangeeta 		}
963c793af95Ssangeeta 		/*
964c793af95Ssangeeta 		 * To be at this point in the code with a non-zero gw
965c793af95Ssangeeta 		 * means that dst is reachable through a gateway that
966c793af95Ssangeeta 		 * we have never resolved.  By changing dst to the gw
967c793af95Ssangeeta 		 * addr we resolve the gateway first.
968c793af95Ssangeeta 		 */
969c793af95Ssangeeta 		if (gw != INADDR_ANY) {
970c793af95Ssangeeta 			/*
971c793af95Ssangeeta 			 * The source ipif that was determined above was
972c793af95Ssangeeta 			 * relative to the destination address, not the
973c793af95Ssangeeta 			 * gateway's. If src_ipif was not taken out of
974c793af95Ssangeeta 			 * the IRE_IF_RESOLVER entry, we'll need to call
975c793af95Ssangeeta 			 * ipif_select_source() again.
976c793af95Ssangeeta 			 */
977c793af95Ssangeeta 			if (src_ipif != ire->ire_ipif) {
978c793af95Ssangeeta 				ipif_refrele(src_ipif);
979c793af95Ssangeeta 				src_ipif = ipif_select_source(dst_ill,
980c793af95Ssangeeta 				    gw, zoneid);
981c793af95Ssangeeta 				if (src_ipif == NULL)
982c793af95Ssangeeta 					goto icmp_err_ret;
983c793af95Ssangeeta 			}
984c793af95Ssangeeta 			dst = gw;
985c793af95Ssangeeta 			gw = INADDR_ANY;
986c793af95Ssangeeta 		}
987c793af95Ssangeeta 		/*
988c793af95Ssangeeta 		 * dst has been set to the address of the nexthop.
989c793af95Ssangeeta 		 *
990c793af95Ssangeeta 		 * TSol note: get security attributes of the nexthop;
991c793af95Ssangeeta 		 * Note that the nexthop may either be a gateway, or the
992c793af95Ssangeeta 		 * packet destination itself; Detailed explanation of
993c793af95Ssangeeta 		 * issues involved is  provided in the  IRE_IF_NORESOLVER
994c793af95Ssangeeta 		 * logic in ip_newroute().
995c793af95Ssangeeta 		 */
996c793af95Ssangeeta 		ga.ga_af = AF_INET;
997c793af95Ssangeeta 		IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
998c793af95Ssangeeta 		gcgrp = gcgrp_lookup(&ga, B_FALSE);
999c793af95Ssangeeta 
1000c793af95Ssangeeta 		if (ire->ire_type == IRE_IF_NORESOLVER)
1001c793af95Ssangeeta 			dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
1002c793af95Ssangeeta 
1003c793af95Ssangeeta 		save_ire = ire;
1004c793af95Ssangeeta 		/*
1005c793af95Ssangeeta 		 * create an incomplete ire-cache with a null dlureq_mp.
1006c793af95Ssangeeta 		 * The dlureq_mp will be created in ire_arpresolve.
1007c793af95Ssangeeta 		 */
1008c793af95Ssangeeta 		ire = ire_create(
1009c793af95Ssangeeta 			(uchar_t *)&dst,		/* dest address */
1010c793af95Ssangeeta 		    (uchar_t *)&ip_g_all_ones,	/* mask */
1011c793af95Ssangeeta 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
1012c793af95Ssangeeta 		    (uchar_t *)&gw,		/* gateway address */
1013c793af95Ssangeeta 		    NULL,
1014c793af95Ssangeeta 		    (save_ire->ire_type == IRE_IF_RESOLVER ?  NULL:
1015c793af95Ssangeeta 		    &save_ire->ire_max_frag),
1016c793af95Ssangeeta 		    NULL,
1017c793af95Ssangeeta 		    dst_ill->ill_rq,		/* recv-from queue */
1018c793af95Ssangeeta 		    dst_ill->ill_wq,		/* send-to queue */
1019c793af95Ssangeeta 		    IRE_CACHE,			/* IRE type */
1020c793af95Ssangeeta 		    NULL,
1021c793af95Ssangeeta 		    src_ipif,
1022c793af95Ssangeeta 		    NULL,
1023c793af95Ssangeeta 		    ire->ire_mask,		/* Parent mask */
1024c793af95Ssangeeta 		    0,
1025c793af95Ssangeeta 		    ire->ire_ihandle,	/* Interface handle */
1026c793af95Ssangeeta 		    0,
1027c793af95Ssangeeta 		    &(ire->ire_uinfo),
1028c793af95Ssangeeta 		    NULL,
1029f4b3ec61Sdh155122 		    gcgrp,
1030f4b3ec61Sdh155122 		    ipst);
1031c793af95Ssangeeta 		ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
1032c793af95Ssangeeta 		if (ire != NULL) {
1033c793af95Ssangeeta 			gcgrp = NULL; /* reference now held by IRE */
1034c793af95Ssangeeta 			ire->ire_marks |= ire_marks;
1035c793af95Ssangeeta 			/* add the incomplete ire: */
1036c793af95Ssangeeta 			error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
1037c793af95Ssangeeta 			if (error == 0 && ire != NULL) {
1038c793af95Ssangeeta 				ire->ire_max_frag = save_ire->ire_max_frag;
1039c793af95Ssangeeta 				ip1dbg(("setting max_frag to %d in ire 0x%p\n",
1040c793af95Ssangeeta 				    ire->ire_max_frag, (void *)ire));
1041c793af95Ssangeeta 			} else {
1042c793af95Ssangeeta 				ire_refrele(save_ire);
1043c793af95Ssangeeta 				goto icmp_err_ret;
1044c793af95Ssangeeta 			}
1045c793af95Ssangeeta 		} else {
1046c793af95Ssangeeta 			if (gcgrp != NULL) {
1047c793af95Ssangeeta 				GCGRP_REFRELE(gcgrp);
1048c793af95Ssangeeta 				gcgrp = NULL;
1049c793af95Ssangeeta 			}
1050c793af95Ssangeeta 		}
1051c793af95Ssangeeta 
1052c793af95Ssangeeta 		ire_refrele(save_ire);
1053c793af95Ssangeeta 		break;
1054c793af95Ssangeeta 	default:
1055c793af95Ssangeeta 		break;
1056c793af95Ssangeeta 	}
1057c793af95Ssangeeta 
1058c793af95Ssangeeta out:
1059c793af95Ssangeeta 	if (sire != NULL)
1060c793af95Ssangeeta 		ire_refrele(sire);
1061c793af95Ssangeeta 	if (dst_ill != NULL)
1062c793af95Ssangeeta 		ill_refrele(dst_ill);
1063c793af95Ssangeeta 	if (src_ipif != NULL)
1064c793af95Ssangeeta 		ipif_refrele(src_ipif);
1065c793af95Ssangeeta 	return (ire);
1066c793af95Ssangeeta icmp_err_ret:
1067c793af95Ssangeeta 	if (src_ipif != NULL)
1068c793af95Ssangeeta 		ipif_refrele(src_ipif);
1069c793af95Ssangeeta 	if (dst_ill != NULL)
1070c793af95Ssangeeta 		ill_refrele(dst_ill);
1071c793af95Ssangeeta 	if (sire != NULL)
1072c793af95Ssangeeta 		ire_refrele(sire);
1073c793af95Ssangeeta 	if (ire != NULL) {
1074c793af95Ssangeeta 		ire_refrele(ire);
1075c793af95Ssangeeta 	}
1076c793af95Ssangeeta 	/* caller needs to send icmp error message */
1077c793af95Ssangeeta 	return (NULL);
1078c793af95Ssangeeta 
1079c793af95Ssangeeta }
1080c793af95Ssangeeta 
1081c793af95Ssangeeta /*
1082f4b3ec61Sdh155122  * Obtain the rt_entry and rt_irb for the route to be added to
1083f4b3ec61Sdh155122  * the ips_ip_ftable.
1084c793af95Ssangeeta  * First attempt to add a node to the radix tree via rn_addroute. If the
1085c793af95Ssangeeta  * route already exists, return the bucket for the existing route.
1086c793af95Ssangeeta  *
1087c793af95Ssangeeta  * Locking notes: Need to hold the global radix tree lock in write mode to
1088c793af95Ssangeeta  * add a radix node. To prevent the node from being deleted, ire_get_bucket()
1089c793af95Ssangeeta  * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
1090c793af95Ssangeeta  * while holding the irb_lock, but not the radix tree lock.
1091c793af95Ssangeeta  */
1092c793af95Ssangeeta irb_t *
1093c793af95Ssangeeta ire_get_bucket(ire_t *ire)
1094c793af95Ssangeeta {
1095c793af95Ssangeeta 	struct radix_node *rn;
1096c793af95Ssangeeta 	struct rt_entry *rt;
1097c793af95Ssangeeta 	struct rt_sockaddr rmask, rdst;
1098c793af95Ssangeeta 	irb_t *irb = NULL;
1099f4b3ec61Sdh155122 	ip_stack_t *ipst = ire->ire_ipst;
1100c793af95Ssangeeta 
1101f4b3ec61Sdh155122 	ASSERT(ipst->ips_ip_ftable != NULL);
1102c793af95Ssangeeta 
1103c793af95Ssangeeta 	/* first try to see if route exists (based on rtalloc1) */
1104c793af95Ssangeeta 	(void) memset(&rdst, 0, sizeof (rdst));
1105c793af95Ssangeeta 	rdst.rt_sin_len = sizeof (rdst);
1106c793af95Ssangeeta 	rdst.rt_sin_family = AF_INET;
1107c793af95Ssangeeta 	rdst.rt_sin_addr.s_addr = ire->ire_addr;
1108c793af95Ssangeeta 
1109c793af95Ssangeeta 	(void) memset(&rmask, 0, sizeof (rmask));
1110c793af95Ssangeeta 	rmask.rt_sin_len = sizeof (rmask);
1111c793af95Ssangeeta 	rmask.rt_sin_family = AF_INET;
1112c793af95Ssangeeta 	rmask.rt_sin_addr.s_addr = ire->ire_mask;
1113c793af95Ssangeeta 
1114c793af95Ssangeeta 	/*
1115c793af95Ssangeeta 	 * add the route. based on BSD's rtrequest1(RTM_ADD)
1116c793af95Ssangeeta 	 */
1117c793af95Ssangeeta 	R_Malloc(rt, rt_entry_cache,  sizeof (*rt));
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
1228*edd26dc5Sdr146992  *	b. ipsec work
1229*edd26dc5Sdr146992  *	c. fragmentation
1230*edd26dc5Sdr146992  *
1231*edd26dc5Sdr146992  * If the packet has been prepared for hardware checksum then it will be
1232*edd26dc5Sdr146992  * passed off to ip_send_align_cksum() to check that the flags set on the
1233*edd26dc5Sdr146992  * 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 */
1370c793af95Ssangeeta 	if (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);
1380*edd26dc5Sdr146992 
1381c793af95Ssangeeta 	/*
1382c793af95Ssangeeta 	 * If needed, we will create the ire cache entry for the
1383c793af95Ssangeeta 	 * nexthop, resolve its link-layer address and then send
1384*edd26dc5Sdr146992 	 * 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 	}
1416*edd26dc5Sdr146992 
1417*edd26dc5Sdr146992 	if (DB_CKSUMFLAGS(mp)) {
1418*edd26dc5Sdr146992 		if (ip_send_align_hcksum_flags(mp, ire_to_ill(ire_cache)))
1419*edd26dc5Sdr146992 			goto cleanup;
1420*edd26dc5Sdr146992 	}
1421*edd26dc5Sdr146992 
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);
1429*edd26dc5Sdr146992 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 
1472*edd26dc5Sdr146992 
1473*edd26dc5Sdr146992 /*
1474*edd26dc5Sdr146992  * We don't check for dohwcksum in here because it should be being used
1475*edd26dc5Sdr146992  * elsewhere to control what flags are being set on the mblk.  That is,
1476*edd26dc5Sdr146992  * if DB_CKSUMFLAGS() is non-zero then we assume dohwcksum to be true
1477*edd26dc5Sdr146992  * for this packet.
1478*edd26dc5Sdr146992  *
1479*edd26dc5Sdr146992  * This function assumes that it is *only* being called for TCP or UDP
1480*edd26dc5Sdr146992  * packets and nothing else.
1481*edd26dc5Sdr146992  */
1482*edd26dc5Sdr146992 static int
1483*edd26dc5Sdr146992 ip_send_align_hcksum_flags(mblk_t *mp, ill_t *ill)
1484*edd26dc5Sdr146992 {
1485*edd26dc5Sdr146992 	int illhckflags;
1486*edd26dc5Sdr146992 	int mbhckflags;
1487*edd26dc5Sdr146992 	uint16_t *up;
1488*edd26dc5Sdr146992 	uint32_t cksum;
1489*edd26dc5Sdr146992 	ipha_t *ipha;
1490*edd26dc5Sdr146992 	ip6_t *ip6;
1491*edd26dc5Sdr146992 	int proto;
1492*edd26dc5Sdr146992 	int ipversion;
1493*edd26dc5Sdr146992 	int length;
1494*edd26dc5Sdr146992 	int start;
1495*edd26dc5Sdr146992 	ip6_pkt_t ipp;
1496*edd26dc5Sdr146992 
1497*edd26dc5Sdr146992 	mbhckflags = DB_CKSUMFLAGS(mp);
1498*edd26dc5Sdr146992 	ASSERT(mbhckflags != 0);
1499*edd26dc5Sdr146992 	ASSERT(mp->b_datap->db_type == M_DATA);
1500*edd26dc5Sdr146992 	/*
1501*edd26dc5Sdr146992 	 * Since this function only knows how to manage the hardware checksum
1502*edd26dc5Sdr146992 	 * issue, reject and packets that have flags set on the aside from
1503*edd26dc5Sdr146992 	 * checksum related attributes as we cannot necessarily safely map
1504*edd26dc5Sdr146992 	 * that packet onto the new NIC.  Packets that can be potentially
1505*edd26dc5Sdr146992 	 * dropped here include those marked for LSO.
1506*edd26dc5Sdr146992 	 */
1507*edd26dc5Sdr146992 	if ((mbhckflags &
1508*edd26dc5Sdr146992 	    ~(HCK_FULLCKSUM|HCK_PARTIALCKSUM|HCK_IPV4_HDRCKSUM)) != 0) {
1509*edd26dc5Sdr146992 		DTRACE_PROBE2(pbr__incapable, (mblk_t *), mp, (ill_t *), ill);
1510*edd26dc5Sdr146992 		freemsg(mp);
1511*edd26dc5Sdr146992 		return (-1);
1512*edd26dc5Sdr146992 	}
1513*edd26dc5Sdr146992 
1514*edd26dc5Sdr146992 	ipha = (ipha_t *)mp->b_rptr;
1515*edd26dc5Sdr146992 
1516*edd26dc5Sdr146992 	/*
1517*edd26dc5Sdr146992 	 * Find out what the new NIC is capable of, if anything, and
1518*edd26dc5Sdr146992 	 * only allow it to be used with M_DATA mblks being sent out.
1519*edd26dc5Sdr146992 	 */
1520*edd26dc5Sdr146992 	if (ILL_HCKSUM_CAPABLE(ill)) {
1521*edd26dc5Sdr146992 		illhckflags = ill->ill_hcksum_capab->ill_hcksum_txflags;
1522*edd26dc5Sdr146992 	} else {
1523*edd26dc5Sdr146992 		/*
1524*edd26dc5Sdr146992 		 * No capabilities, so turn off everything.
1525*edd26dc5Sdr146992 		 */
1526*edd26dc5Sdr146992 		illhckflags = 0;
1527*edd26dc5Sdr146992 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0, 0, 0);
1528*edd26dc5Sdr146992 		mp->b_datap->db_struioflag &= ~STRUIO_IP;
1529*edd26dc5Sdr146992 	}
1530*edd26dc5Sdr146992 
1531*edd26dc5Sdr146992 	DTRACE_PROBE4(pbr__info__a, (mblk_t *), mp, (ill_t *), ill,
1532*edd26dc5Sdr146992 	    uint32_t, illhckflags, uint32_t, mbhckflags);
1533*edd26dc5Sdr146992 	/*
1534*edd26dc5Sdr146992 	 * This block of code that looks for the position of the TCP/UDP
1535*edd26dc5Sdr146992 	 * checksum is early in this function because we need to know
1536*edd26dc5Sdr146992 	 * what needs to be blanked out for the hardware checksum case.
1537*edd26dc5Sdr146992 	 *
1538*edd26dc5Sdr146992 	 * That we're in this function implies that the packet is either
1539*edd26dc5Sdr146992 	 * TCP or UDP on Solaris, so checks are made for one protocol and
1540*edd26dc5Sdr146992 	 * if that fails, the other is therefore implied.
1541*edd26dc5Sdr146992 	 */
1542*edd26dc5Sdr146992 	ipversion = IPH_HDR_VERSION(ipha);
1543*edd26dc5Sdr146992 
1544*edd26dc5Sdr146992 	if (ipversion == IPV4_VERSION) {
1545*edd26dc5Sdr146992 		proto = ipha->ipha_protocol;
1546*edd26dc5Sdr146992 		if (proto == IPPROTO_TCP) {
1547*edd26dc5Sdr146992 			up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1548*edd26dc5Sdr146992 		} else {
1549*edd26dc5Sdr146992 			up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
1550*edd26dc5Sdr146992 		}
1551*edd26dc5Sdr146992 	} else {
1552*edd26dc5Sdr146992 		uint8_t lasthdr;
1553*edd26dc5Sdr146992 
1554*edd26dc5Sdr146992 		/*
1555*edd26dc5Sdr146992 		 * Nothing I've seen indicates that IPv6 checksum'ing
1556*edd26dc5Sdr146992 		 * precludes the presence of extension headers, so we
1557*edd26dc5Sdr146992 		 * can't just look at the next header value in the IPv6
1558*edd26dc5Sdr146992 		 * packet header to see if it is TCP/UDP.
1559*edd26dc5Sdr146992 		 */
1560*edd26dc5Sdr146992 		ip6 = (ip6_t *)ipha;
1561*edd26dc5Sdr146992 		(void) memset(&ipp, 0, sizeof (ipp));
1562*edd26dc5Sdr146992 		start = ip_find_hdr_v6(mp, ip6, &ipp, &lasthdr);
1563*edd26dc5Sdr146992 		proto = lasthdr;
1564*edd26dc5Sdr146992 
1565*edd26dc5Sdr146992 		if (proto == IPPROTO_TCP) {
1566*edd26dc5Sdr146992 			up = IPH_TCPH_CHECKSUMP(ipha, start);
1567*edd26dc5Sdr146992 		} else {
1568*edd26dc5Sdr146992 			up = IPH_UDPH_CHECKSUMP(ipha, start);
1569*edd26dc5Sdr146992 		}
1570*edd26dc5Sdr146992 	}
1571*edd26dc5Sdr146992 
1572*edd26dc5Sdr146992 	/*
1573*edd26dc5Sdr146992 	 * The first case here is easiest:
1574*edd26dc5Sdr146992 	 * mblk hasn't asked for full checksum, but the card supports it.
1575*edd26dc5Sdr146992 	 *
1576*edd26dc5Sdr146992 	 * In addition, check for IPv4 header capability.  Note that only
1577*edd26dc5Sdr146992 	 * the mblk flag is checked and not ipversion.
1578*edd26dc5Sdr146992 	 */
1579*edd26dc5Sdr146992 	if ((((illhckflags & HCKSUM_INET_FULL_V4) && (ipversion == 4)) ||
1580*edd26dc5Sdr146992 	    (((illhckflags & HCKSUM_INET_FULL_V6) && (ipversion == 6)))) &&
1581*edd26dc5Sdr146992 	    ((mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) != 0)) {
1582*edd26dc5Sdr146992 		int newflags = HCK_FULLCKSUM;
1583*edd26dc5Sdr146992 
1584*edd26dc5Sdr146992 		if ((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) {
1585*edd26dc5Sdr146992 			if ((illhckflags & HCKSUM_IPHDRCKSUM) != 0) {
1586*edd26dc5Sdr146992 				newflags |= HCK_IPV4_HDRCKSUM;
1587*edd26dc5Sdr146992 			} else {
1588*edd26dc5Sdr146992 				/*
1589*edd26dc5Sdr146992 				 * Rather than call a function, just inline
1590*edd26dc5Sdr146992 				 * the computation of the basic IPv4 header.
1591*edd26dc5Sdr146992 				 */
1592*edd26dc5Sdr146992 				cksum = (ipha->ipha_dst >> 16) +
1593*edd26dc5Sdr146992 				    (ipha->ipha_dst & 0xFFFF) +
1594*edd26dc5Sdr146992 				    (ipha->ipha_src >> 16) +
1595*edd26dc5Sdr146992 				    (ipha->ipha_src & 0xFFFF);
1596*edd26dc5Sdr146992 				IP_HDR_CKSUM(ipha, cksum,
1597*edd26dc5Sdr146992 				    ((uint32_t *)ipha)[0],
1598*edd26dc5Sdr146992 				    ((uint16_t *)ipha)[4]);
1599*edd26dc5Sdr146992 			}
1600*edd26dc5Sdr146992 		}
1601*edd26dc5Sdr146992 
1602*edd26dc5Sdr146992 		*up = 0;
1603*edd26dc5Sdr146992 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0,
1604*edd26dc5Sdr146992 		    newflags, 0);
1605*edd26dc5Sdr146992 		return (0);
1606*edd26dc5Sdr146992 	}
1607*edd26dc5Sdr146992 
1608*edd26dc5Sdr146992 	DTRACE_PROBE2(pbr__info__b, int, ipversion, int, proto);
1609*edd26dc5Sdr146992 
1610*edd26dc5Sdr146992 	/*
1611*edd26dc5Sdr146992 	 * Start calculating the pseudo checksum over the IP packet header.
1612*edd26dc5Sdr146992 	 * Although the final pseudo checksum used by TCP/UDP consists of
1613*edd26dc5Sdr146992 	 * more than just the address fields, we can use the result of
1614*edd26dc5Sdr146992 	 * adding those together a little bit further down for IPv4.
1615*edd26dc5Sdr146992 	 */
1616*edd26dc5Sdr146992 	if (ipversion == IPV4_VERSION) {
1617*edd26dc5Sdr146992 		cksum = (ipha->ipha_dst >> 16) + (ipha->ipha_dst & 0xFFFF) +
1618*edd26dc5Sdr146992 		    (ipha->ipha_src >> 16) + (ipha->ipha_src & 0xFFFF);
1619*edd26dc5Sdr146992 		start = IP_SIMPLE_HDR_LENGTH;
1620*edd26dc5Sdr146992 		length = ntohs(ipha->ipha_length);
1621*edd26dc5Sdr146992 		DTRACE_PROBE3(pbr__info__e, uint32_t, ipha->ipha_src,
1622*edd26dc5Sdr146992 		    uint32_t, ipha->ipha_dst, int, cksum);
1623*edd26dc5Sdr146992 	} else {
1624*edd26dc5Sdr146992 		uint16_t *pseudo;
1625*edd26dc5Sdr146992 
1626*edd26dc5Sdr146992 		pseudo = (uint16_t *)&ip6->ip6_src;
1627*edd26dc5Sdr146992 
1628*edd26dc5Sdr146992 		/* calculate pseudo-header checksum */
1629*edd26dc5Sdr146992 		cksum = pseudo[0] + pseudo[1] + pseudo[2] + pseudo[3] +
1630*edd26dc5Sdr146992 		    pseudo[4] + pseudo[5] + pseudo[6] + pseudo[7] +
1631*edd26dc5Sdr146992 		    pseudo[8] + pseudo[9] + pseudo[10] + pseudo[11] +
1632*edd26dc5Sdr146992 		    pseudo[12] + pseudo[13] + pseudo[14] + pseudo[15];
1633*edd26dc5Sdr146992 
1634*edd26dc5Sdr146992 		length = ntohs(ip6->ip6_plen) + sizeof (ip6_t);
1635*edd26dc5Sdr146992 	}
1636*edd26dc5Sdr146992 
1637*edd26dc5Sdr146992 	/* Fold the initial sum */
1638*edd26dc5Sdr146992 	cksum = (cksum & 0xffff) + (cksum >> 16);
1639*edd26dc5Sdr146992 
1640*edd26dc5Sdr146992 	/*
1641*edd26dc5Sdr146992 	 * If the packet was asking for an IPv4 header checksum to be
1642*edd26dc5Sdr146992 	 * calculated but the interface doesn't support that, fill it in
1643*edd26dc5Sdr146992 	 * using our pseudo checksum as a starting point.
1644*edd26dc5Sdr146992 	 */
1645*edd26dc5Sdr146992 	if (((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) &&
1646*edd26dc5Sdr146992 	    ((illhckflags & HCKSUM_IPHDRCKSUM) == 0)) {
1647*edd26dc5Sdr146992 		/*
1648*edd26dc5Sdr146992 		 * IP_HDR_CKSUM uses the 2rd arg to the macro in a destructive
1649*edd26dc5Sdr146992 		 * way so pass in a copy of the checksum calculated thus far.
1650*edd26dc5Sdr146992 		 */
1651*edd26dc5Sdr146992 		uint32_t ipsum = cksum;
1652*edd26dc5Sdr146992 
1653*edd26dc5Sdr146992 		DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM;
1654*edd26dc5Sdr146992 
1655*edd26dc5Sdr146992 		IP_HDR_CKSUM(ipha, ipsum, ((uint32_t *)ipha)[0],
1656*edd26dc5Sdr146992 		    ((uint16_t *)ipha)[4]);
1657*edd26dc5Sdr146992 	}
1658*edd26dc5Sdr146992 
1659*edd26dc5Sdr146992 	DTRACE_PROBE3(pbr__info__c, int, start, int, length, int, cksum);
1660*edd26dc5Sdr146992 
1661*edd26dc5Sdr146992 	if (proto == IPPROTO_TCP) {
1662*edd26dc5Sdr146992 		cksum += IP_TCP_CSUM_COMP;
1663*edd26dc5Sdr146992 	} else {
1664*edd26dc5Sdr146992 		cksum += IP_UDP_CSUM_COMP;
1665*edd26dc5Sdr146992 	}
1666*edd26dc5Sdr146992 	cksum += htons(length - start);
1667*edd26dc5Sdr146992 	cksum = (cksum & 0xffff) + (cksum >> 16);
1668*edd26dc5Sdr146992 
1669*edd26dc5Sdr146992 	/*
1670*edd26dc5Sdr146992 	 * For TCP/UDP, we either want to setup the packet for partial
1671*edd26dc5Sdr146992 	 * checksum or we want to do it all ourselves because the NIC
1672*edd26dc5Sdr146992 	 * offers no support for either partial or full checksum.
1673*edd26dc5Sdr146992 	 */
1674*edd26dc5Sdr146992 	if ((illhckflags & HCKSUM_INET_PARTIAL) != 0) {
1675*edd26dc5Sdr146992 		/*
1676*edd26dc5Sdr146992 		 * The only case we care about here is if the mblk was
1677*edd26dc5Sdr146992 		 * previously set for full checksum offload.  If it was
1678*edd26dc5Sdr146992 		 * marked for partial (and the NIC does partial), then
1679*edd26dc5Sdr146992 		 * we have nothing to do.  Similarly if the packet was
1680*edd26dc5Sdr146992 		 * not set for partial or full, we do nothing as this
1681*edd26dc5Sdr146992 		 * is cheaper than more work to set something up.
1682*edd26dc5Sdr146992 		 */
1683*edd26dc5Sdr146992 		if ((mbhckflags & HCK_FULLCKSUM) != 0) {
1684*edd26dc5Sdr146992 			uint32_t offset;
1685*edd26dc5Sdr146992 
1686*edd26dc5Sdr146992 			if (proto == IPPROTO_TCP) {
1687*edd26dc5Sdr146992 				offset = TCP_CHECKSUM_OFFSET;
1688*edd26dc5Sdr146992 			} else {
1689*edd26dc5Sdr146992 				offset = UDP_CHECKSUM_OFFSET;
1690*edd26dc5Sdr146992 			}
1691*edd26dc5Sdr146992 			*up = cksum;
1692*edd26dc5Sdr146992 
1693*edd26dc5Sdr146992 			DTRACE_PROBE3(pbr__info__f, int, length - start, int,
1694*edd26dc5Sdr146992 			    cksum, int, offset);
1695*edd26dc5Sdr146992 
1696*edd26dc5Sdr146992 			(void) hcksum_assoc(mp, NULL, NULL, start,
1697*edd26dc5Sdr146992 			    start + offset, length, 0,
1698*edd26dc5Sdr146992 			    DB_CKSUMFLAGS(mp) | HCK_PARTIALCKSUM, 0);
1699*edd26dc5Sdr146992 		}
1700*edd26dc5Sdr146992 
1701*edd26dc5Sdr146992 	} else if (mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) {
1702*edd26dc5Sdr146992 		DB_CKSUMFLAGS(mp) &= ~(HCK_PARTIALCKSUM|HCK_FULLCKSUM);
1703*edd26dc5Sdr146992 
1704*edd26dc5Sdr146992 		*up = 0;
1705*edd26dc5Sdr146992 		*up = IP_CSUM(mp, start, cksum);
1706*edd26dc5Sdr146992 	}
1707*edd26dc5Sdr146992 
1708*edd26dc5Sdr146992 	DTRACE_PROBE4(pbr__info__d, (mblk_t *), mp, (ipha_t *), ipha,
1709*edd26dc5Sdr146992 	    (uint16_t *), up, int, cksum);
1710*edd26dc5Sdr146992 	return (0);
1711*edd26dc5Sdr146992 }
1712*edd26dc5Sdr146992 
1713*edd26dc5Sdr146992 
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