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