xref: /titanic_44/usr/src/uts/common/inet/ip/ip_ftable.c (revision caa9584eae2ea3abc85638640265d49a0ffea363)
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_type & IRE_HOST_REDIRECT) &&
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(ip_ftable, rtfunc, &rtfarg);
579 }
580 
581 struct ihandle_arg {
582 	uint32_t ihandle;
583 	ire_t	 *ire;
584 };
585 
586 static int
587 ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
588 {
589 	struct rt_entry *rt;
590 	irb_t *irb;
591 	ire_t *ire;
592 	struct ihandle_arg *ih = arg;
593 
594 	rt = (struct rt_entry *)rn;
595 	ASSERT(rt != NULL);
596 	irb = &rt->rt_irb;
597 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
598 		if ((ire->ire_type & IRE_INTERFACE) &&
599 		    (ire->ire_ihandle == ih->ihandle)) {
600 			ih->ire = ire;
601 			IRE_REFHOLD(ire);
602 			return (1);
603 		}
604 	}
605 	return (0);
606 }
607 
608 /*
609  * Locate the interface ire that is tied to the cache ire 'cire' via
610  * cire->ire_ihandle.
611  *
612  * We are trying to create the cache ire for an onlink destn. or
613  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
614  * case, after the ire has come back from ARP.
615  */
616 ire_t *
617 ire_ihandle_lookup_onlink(ire_t *cire)
618 {
619 	ire_t	*ire;
620 	int	match_flags;
621 	struct ihandle_arg ih;
622 
623 	ASSERT(cire != NULL);
624 
625 	/*
626 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
627 	 * because the ihandle refers to an ipif which can be in only one zone.
628 	 */
629 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
630 	/*
631 	 * We know that the mask of the interface ire equals cire->ire_cmask.
632 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
633 	 * cmask from the interface ire's mask)
634 	 */
635 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
636 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
637 	    NULL, match_flags);
638 	if (ire != NULL)
639 		return (ire);
640 	/*
641 	 * If we didn't find an interface ire above, we can't declare failure.
642 	 * For backwards compatibility, we need to support prefix routes
643 	 * pointing to next hop gateways that are not on-link.
644 	 *
645 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
646 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
647 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
648 	 * doing ire_route_lookup() twice and returning an interface ire.
649 	 *
650 	 * Eg. default	-	gw1			(line 1)
651 	 *	gw1	-	gw2			(line 2)
652 	 *	gw2	-	hme0			(line 3)
653 	 *
654 	 * In the above example, ip_newroute() tried to create the cache ire
655 	 * 'cire' for gw1, based on the interface route in line 3. The
656 	 * ire_ftable_lookup() above fails, because there is no interface route
657 	 * to reach gw1. (it is gw2). We fall thru below.
658 	 *
659 	 * Do a brute force search based on the ihandle in a subset of the
660 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
661 	 * things become very complex, since we don't have 'pire' in this
662 	 * case. (Also note that this method is not possible in the offlink
663 	 * case because we don't know the mask)
664 	 */
665 	(void) memset(&ih, 0, sizeof (ih));
666 	ih.ihandle = cire->ire_ihandle;
667 	(void) ip_ftable->rnh_walktree(ip_ftable,
668 	    ire_ihandle_onlink_match, &ih);
669 	return (ih.ire);
670 }
671 
672 /*
673  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
674  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
675  * in the bucket skipping default interface routes and deleted entries.
676  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
677  * Assumes that the caller holds a reference on the IRE bucket.
678  */
679 ire_t *
680 ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
681 {
682 	ASSERT(ire_origin->ire_bucket != NULL);
683 	ASSERT(ire != NULL);
684 
685 	do {
686 		ire = ire->ire_next;
687 		if (ire == NULL)
688 			ire = ire_origin->ire_bucket->irb_ire;
689 		if (ire == ire_origin)
690 			return (NULL);
691 	} while ((ire->ire_type & IRE_INTERFACE) ||
692 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
693 	ASSERT(ire != NULL);
694 	return (ire);
695 }
696 
697 static ipif_t *
698 ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
699     int zoneid, ushort_t *marks)
700 {
701 	ipif_t *src_ipif;
702 
703 	/*
704 	 * Pick the best source address from dst_ill.
705 	 *
706 	 * 1) If it is part of a multipathing group, we would
707 	 *    like to spread the inbound packets across different
708 	 *    interfaces. ipif_select_source picks a random source
709 	 *    across the different ills in the group.
710 	 *
711 	 * 2) If it is not part of a multipathing group, we try
712 	 *    to pick the source address from the destination
713 	 *    route. Clustering assumes that when we have multiple
714 	 *    prefixes hosted on an interface, the prefix of the
715 	 *    source address matches the prefix of the destination
716 	 *    route. We do this only if the address is not
717 	 *    DEPRECATED.
718 	 *
719 	 * 3) If the conn is in a different zone than the ire, we
720 	 *    need to pick a source address from the right zone.
721 	 *
722 	 * NOTE : If we hit case (1) above, the prefix of the source
723 	 *	  address picked may not match the prefix of the
724 	 *	  destination routes prefix as ipif_select_source
725 	 *	  does not look at "dst" while picking a source
726 	 *	  address.
727 	 *	  If we want the same behavior as (2), we will need
728 	 *	  to change the behavior of ipif_select_source.
729 	 */
730 
731 	if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
732 		/*
733 		 * The RTF_SETSRC flag is set in the parent ire (sire).
734 		 * Check that the ipif matching the requested source
735 		 * address still exists.
736 		 */
737 		src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
738 		    zoneid, NULL, NULL, NULL, NULL);
739 		return (src_ipif);
740 	}
741 	*marks |= IRE_MARK_USESRC_CHECK;
742 	if ((dst_ill->ill_group != NULL) ||
743 	    (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
744 	    (dst_ill->ill_usesrc_ifindex != 0)) {
745 		src_ipif = ipif_select_source(dst_ill, dst, zoneid);
746 		if (src_ipif == NULL)
747 			return (NULL);
748 
749 	} else {
750 		src_ipif = ire->ire_ipif;
751 		ASSERT(src_ipif != NULL);
752 		/* hold src_ipif for uniformity */
753 		ipif_refhold(src_ipif);
754 	}
755 	return (src_ipif);
756 }
757 
758 /* Added to root cause a bug - should be removed later */
759 ire_t *ire_gw_cache = NULL;
760 
761 /*
762  * This function is called by ip_rput_noire() and ip_fast_forward()
763  * to resolve the route of incoming packet that needs to be forwarded.
764  * If the ire of the nexthop is not already in the cachetable, this
765  * routine will insert it to the table, but won't trigger ARP resolution yet.
766  * Thus unlike ip_newroute, this function adds incomplete ires to
767  * the cachetable. ARP resolution for these ires are  delayed until
768  * after all of the packet processing is completed and its ready to
769  * be sent out on the wire, Eventually, the packet transmit routine
770  * ip_xmit_v4() attempts to send a packet  to the driver. If it finds
771  * that there is no link layer information, it will do the arp
772  * resolution and queue the packet in ire->ire_nce->nce_qd_mp and
773  * then send it out once the arp resolution is over
774  * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
775  * the model of BSD/SunOS 4
776  *
777  * In future, the insertion of incomplete ires in the cachetable should
778  * be implemented in hostpath as well, as doing so will greatly reduce
779  * the existing complexity for code paths that depend on the context of
780  * the sender (such as IPsec).
781  *
782  * Thus this scheme of adding incomplete ires in cachetable in forwarding
783  * path can be used as a template for simplifying the hostpath.
784  */
785 
786 ire_t *
787 ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
788     ire_t *supplied_sire, const struct ts_label_s *tsl)
789 {
790 	ipaddr_t gw = 0;
791 	ire_t	*ire = NULL;
792 	ire_t   *sire = NULL, *save_ire;
793 	ill_t *dst_ill = NULL;
794 	int error;
795 	zoneid_t zoneid;
796 	ipif_t *src_ipif = NULL;
797 	mblk_t *res_mp;
798 	ushort_t ire_marks = 0;
799 	tsol_gcgrp_t *gcgrp = NULL;
800 	tsol_gcgrp_addr_t ga;
801 
802 	zoneid = GLOBAL_ZONEID;
803 
804 	if (supplied_ire != NULL) {
805 		/* We have arrived here from ipfil_sendpkt */
806 		ire = supplied_ire;
807 		sire = supplied_sire;
808 		goto create_irecache;
809 	}
810 
811 	ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
812 	    tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
813 	    MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR);
814 
815 	if (ire == NULL) {
816 		ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST);
817 		goto icmp_err_ret;
818 	}
819 
820 	/*
821 	 * If we encounter CGTP, we should  have the caller use
822 	 * ip_newroute to resolve multirt instead of this function.
823 	 * CGTP specs explicitly state that it can't be used with routers.
824 	 * This essentially prevents insertion of incomplete RTF_MULTIRT
825 	 * ires in cachetable.
826 	 */
827 	if (ip_cgtp_filter &&
828 	    ((ire->ire_flags & RTF_MULTIRT) ||
829 	    ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
830 		ip3dbg(("ire_forward: packet is to be multirouted- "
831 		    "handing it to ip_newroute\n"));
832 		if (sire != NULL)
833 			ire_refrele(sire);
834 		ire_refrele(ire);
835 		/*
836 		 * Inform caller about encountering of multirt so that
837 		 * ip_newroute() can be called.
838 		 */
839 		*check_multirt = B_TRUE;
840 		return (NULL);
841 	}
842 
843 	*check_multirt = B_FALSE;
844 
845 	/*
846 	 * Verify that the returned IRE does not have either
847 	 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
848 	 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
849 	 */
850 	if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
851 	    (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
852 		ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
853 		    (void *)ire));
854 		goto icmp_err_ret;
855 	}
856 
857 	/*
858 	 * If we already have a fully resolved IRE CACHE of the
859 	 * nexthop router, just hand over the cache entry
860 	 * and we are done.
861 	 */
862 
863 	if (ire->ire_type & IRE_CACHE) {
864 
865 		/*
866 		 * If we are using this ire cache entry as a
867 		 * gateway to forward packets, chances are we
868 		 * will be using it again. So turn off
869 		 * the temporary flag, thus reducing its
870 		 * chances of getting deleted frequently.
871 		 */
872 		if (ire->ire_marks & IRE_MARK_TEMPORARY) {
873 			irb_t *irb = ire->ire_bucket;
874 			rw_enter(&irb->irb_lock, RW_WRITER);
875 			ire->ire_marks &= ~IRE_MARK_TEMPORARY;
876 			irb->irb_tmp_ire_cnt--;
877 			rw_exit(&irb->irb_lock);
878 		}
879 
880 		if (sire != NULL) {
881 			UPDATE_OB_PKT_COUNT(sire);
882 			sire->ire_last_used_time = lbolt;
883 			ire_refrele(sire);
884 		}
885 		return (ire);
886 	}
887 create_irecache:
888 	/*
889 	 * Increment the ire_ob_pkt_count field for ire if it is an
890 	 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
891 	 * increment the same for the parent IRE, sire, if it is some
892 	 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST
893 	 * and HOST_REDIRECT).
894 	 */
895 	if ((ire->ire_type & IRE_INTERFACE) != 0) {
896 		UPDATE_OB_PKT_COUNT(ire);
897 		ire->ire_last_used_time = lbolt;
898 	}
899 
900 	/*
901 	 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
902 	 */
903 	if (sire != NULL) {
904 		gw = sire->ire_gateway_addr;
905 		ASSERT((sire->ire_type &
906 		    (IRE_CACHETABLE | IRE_INTERFACE)) == 0);
907 		UPDATE_OB_PKT_COUNT(sire);
908 		sire->ire_last_used_time = lbolt;
909 	}
910 
911 	/* Obtain dst_ill */
912 	dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
913 	if (dst_ill == NULL) {
914 		ip2dbg(("ire_forward no dst ill; ire 0x%p\n",
915 			(void *)ire));
916 		goto icmp_err_ret;
917 	}
918 
919 	ASSERT(src_ipif == NULL);
920 	/* Now obtain the src_ipif */
921 	src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
922 	    zoneid, &ire_marks);
923 	if (src_ipif == NULL)
924 		goto icmp_err_ret;
925 
926 	switch (ire->ire_type) {
927 	case IRE_IF_NORESOLVER:
928 		/* create ire_cache for ire_addr endpoint */
929 	case IRE_IF_RESOLVER:
930 		/*
931 		 * We have the IRE_IF_RESOLVER of the nexthop gateway
932 		 * and now need to build a IRE_CACHE for it.
933 		 * In this case, we have the following :
934 		 *
935 		 * 1) src_ipif - used for getting a source address.
936 		 *
937 		 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
938 		 *    means packets using the IRE_CACHE that we will build
939 		 *    here will go out on dst_ill.
940 		 *
941 		 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
942 		 *    to be created will only be tied to the IRE_INTERFACE
943 		 *    that was derived from the ire_ihandle field.
944 		 *
945 		 *    If sire is non-NULL, it means the destination is
946 		 *    off-link and we will first create the IRE_CACHE for the
947 		 *    gateway.
948 		 */
949 		res_mp = dst_ill->ill_resolver_mp;
950 		if (ire->ire_type == IRE_IF_RESOLVER &&
951 		    (!OK_RESOLVER_MP(res_mp))) {
952 			ire_refrele(ire);
953 			ire = NULL;
954 			goto out;
955 		}
956 		/*
957 		 * To be at this point in the code with a non-zero gw
958 		 * means that dst is reachable through a gateway that
959 		 * we have never resolved.  By changing dst to the gw
960 		 * addr we resolve the gateway first.
961 		 */
962 		if (gw != INADDR_ANY) {
963 			/*
964 			 * The source ipif that was determined above was
965 			 * relative to the destination address, not the
966 			 * gateway's. If src_ipif was not taken out of
967 			 * the IRE_IF_RESOLVER entry, we'll need to call
968 			 * ipif_select_source() again.
969 			 */
970 			if (src_ipif != ire->ire_ipif) {
971 				ipif_refrele(src_ipif);
972 				src_ipif = ipif_select_source(dst_ill,
973 				    gw, zoneid);
974 				if (src_ipif == NULL)
975 					goto icmp_err_ret;
976 			}
977 			dst = gw;
978 			gw = INADDR_ANY;
979 		}
980 		/*
981 		 * dst has been set to the address of the nexthop.
982 		 *
983 		 * TSol note: get security attributes of the nexthop;
984 		 * Note that the nexthop may either be a gateway, or the
985 		 * packet destination itself; Detailed explanation of
986 		 * issues involved is  provided in the  IRE_IF_NORESOLVER
987 		 * logic in ip_newroute().
988 		 */
989 		ga.ga_af = AF_INET;
990 		IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
991 		gcgrp = gcgrp_lookup(&ga, B_FALSE);
992 
993 		if (ire->ire_type == IRE_IF_NORESOLVER)
994 			dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
995 
996 		save_ire = ire;
997 		/*
998 		 * create an incomplete ire-cache with a null dlureq_mp.
999 		 * The dlureq_mp will be created in ire_arpresolve.
1000 		 */
1001 		ire = ire_create(
1002 			(uchar_t *)&dst,		/* dest address */
1003 		    (uchar_t *)&ip_g_all_ones,	/* mask */
1004 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
1005 		    (uchar_t *)&gw,		/* gateway address */
1006 		    NULL,
1007 		    (save_ire->ire_type == IRE_IF_RESOLVER ?  NULL:
1008 		    &save_ire->ire_max_frag),
1009 		    NULL,
1010 		    dst_ill->ill_rq,		/* recv-from queue */
1011 		    dst_ill->ill_wq,		/* send-to queue */
1012 		    IRE_CACHE,			/* IRE type */
1013 		    NULL,
1014 		    src_ipif,
1015 		    NULL,
1016 		    ire->ire_mask,		/* Parent mask */
1017 		    0,
1018 		    ire->ire_ihandle,	/* Interface handle */
1019 		    0,
1020 		    &(ire->ire_uinfo),
1021 		    NULL,
1022 		    gcgrp);
1023 		ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
1024 		if (ire != NULL) {
1025 			gcgrp = NULL; /* reference now held by IRE */
1026 			ire->ire_marks |= ire_marks;
1027 			/* add the incomplete ire: */
1028 			error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
1029 			if (error == 0 && ire != NULL) {
1030 				ire->ire_max_frag = save_ire->ire_max_frag;
1031 				ip1dbg(("setting max_frag to %d in ire 0x%p\n",
1032 				    ire->ire_max_frag, (void *)ire));
1033 			} else {
1034 				ire_refrele(save_ire);
1035 				goto icmp_err_ret;
1036 			}
1037 		} else {
1038 			if (gcgrp != NULL) {
1039 				GCGRP_REFRELE(gcgrp);
1040 				gcgrp = NULL;
1041 			}
1042 		}
1043 
1044 		ire_refrele(save_ire);
1045 		break;
1046 	default:
1047 		break;
1048 	}
1049 
1050 out:
1051 	if (sire != NULL)
1052 		ire_refrele(sire);
1053 	if (dst_ill != NULL)
1054 		ill_refrele(dst_ill);
1055 	if (src_ipif != NULL)
1056 		ipif_refrele(src_ipif);
1057 	return (ire);
1058 icmp_err_ret:
1059 	if (src_ipif != NULL)
1060 		ipif_refrele(src_ipif);
1061 	if (dst_ill != NULL)
1062 		ill_refrele(dst_ill);
1063 	if (sire != NULL)
1064 		ire_refrele(sire);
1065 	if (ire != NULL) {
1066 		ire_refrele(ire);
1067 	}
1068 	/* caller needs to send icmp error message */
1069 	return (NULL);
1070 
1071 }
1072 
1073 /*
1074  * Obtain the rt_entry and rt_irb for the route to be added to the ip_ftable.
1075  * First attempt to add a node to the radix tree via rn_addroute. If the
1076  * route already exists, return the bucket for the existing route.
1077  *
1078  * Locking notes: Need to hold the global radix tree lock in write mode to
1079  * add a radix node. To prevent the node from being deleted, ire_get_bucket()
1080  * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
1081  * while holding the irb_lock, but not the radix tree lock.
1082  */
1083 irb_t *
1084 ire_get_bucket(ire_t *ire)
1085 {
1086 	struct radix_node *rn;
1087 	struct rt_entry *rt;
1088 	struct rt_sockaddr rmask, rdst;
1089 	irb_t *irb = NULL;
1090 
1091 	ASSERT(ip_ftable != NULL);
1092 
1093 	/* first try to see if route exists (based on rtalloc1) */
1094 	(void) memset(&rdst, 0, sizeof (rdst));
1095 	rdst.rt_sin_len = sizeof (rdst);
1096 	rdst.rt_sin_family = AF_INET;
1097 	rdst.rt_sin_addr.s_addr = ire->ire_addr;
1098 
1099 	(void) memset(&rmask, 0, sizeof (rmask));
1100 	rmask.rt_sin_len = sizeof (rmask);
1101 	rmask.rt_sin_family = AF_INET;
1102 	rmask.rt_sin_addr.s_addr = ire->ire_mask;
1103 
1104 	/*
1105 	 * add the route. based on BSD's rtrequest1(RTM_ADD)
1106 	 */
1107 	R_Malloc(rt, rt_entry_cache,  sizeof (*rt));
1108 	(void) memset(rt, 0, sizeof (*rt));
1109 	rt->rt_nodes->rn_key = (char *)&rt->rt_dst;
1110 	rt->rt_dst = rdst;
1111 	irb = &rt->rt_irb;
1112 	irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */
1113 	rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL);
1114 	RADIX_NODE_HEAD_WLOCK(ip_ftable);
1115 	rn = ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask, ip_ftable,
1116 	    (struct radix_node *)rt);
1117 	if (rn == NULL) {
1118 		RADIX_NODE_HEAD_UNLOCK(ip_ftable);
1119 		Free(rt, rt_entry_cache);
1120 		rt = NULL;
1121 		irb = NULL;
1122 		RADIX_NODE_HEAD_RLOCK(ip_ftable);
1123 		if ((rn = ip_ftable->rnh_lookup(&rdst, &rmask, ip_ftable)) !=
1124 		    NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
1125 			/* found a non-root match */
1126 			rt = (struct rt_entry *)rn;
1127 		}
1128 	}
1129 	if (rt != NULL) {
1130 		irb = &rt->rt_irb;
1131 		IRB_REFHOLD(irb);
1132 	}
1133 	RADIX_NODE_HEAD_UNLOCK(ip_ftable);
1134 	return (irb);
1135 }
1136 
1137 /*
1138  * This function is used when the caller wants to know the outbound
1139  * interface for a packet given only the address.
1140  * If this is a offlink IP address and there are multiple
1141  * routes to this destination, this routine will utilise the
1142  * first route it finds to IP address
1143  * Return values:
1144  * 	0	- FAILURE
1145  *	nonzero	- ifindex
1146  */
1147 uint_t
1148 ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid)
1149 {
1150 	uint_t ifindex = 0;
1151 	ire_t *ire;
1152 	ill_t *ill;
1153 
1154 	/* zoneid is a placeholder for future routing table per-zone project */
1155 	ASSERT(zoneid == ALL_ZONES);
1156 
1157 	ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6);
1158 
1159 	if ((ire =  route_to_dst(ipaddr, zoneid)) != NULL) {
1160 		ill = ire_to_ill(ire);
1161 		if (ill != NULL)
1162 			ifindex = ill->ill_phyint->phyint_ifindex;
1163 		ire_refrele(ire);
1164 	}
1165 	return (ifindex);
1166 }
1167 
1168 /*
1169  * Routine to find the route to a destination. If a ifindex is supplied
1170  * it tries to match the the route to the corresponding ipif for the ifindex
1171  */
1172 static	ire_t *
1173 route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid)
1174 {
1175 	ire_t *ire = NULL;
1176 	int match_flags;
1177 
1178 	match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1179 	    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
1180 
1181 	/* XXX pass NULL tsl for now */
1182 
1183 	if (dst_addr->sa_family == AF_INET) {
1184 		ire = ire_route_lookup(
1185 		    ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr,
1186 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags);
1187 	} else {
1188 		ire = ire_route_lookup_v6(
1189 		    &((struct sockaddr_in6 *)dst_addr)->sin6_addr,
1190 		    0, 0, 0, NULL, NULL, zoneid, NULL, match_flags);
1191 	}
1192 	return (ire);
1193 }
1194 
1195 /*
1196  * This routine is called by IP Filter to send a packet out on the wire
1197  * to a specified V4 dst (which may be onlink or offlink). The ifindex may or
1198  * may not be 0. A non-null ifindex indicates IP Filter has stipulated
1199  * an outgoing interface and requires the nexthop to be on that interface.
1200  * IP WILL NOT DO  the following to the data packet before sending it out:
1201  *	a. manipulate ttl
1202  *	b. checksuming
1203  *	c. ipsec work
1204  *	d. fragmentation
1205  *
1206  * Return values:
1207  *	0:		IP was able to send of the data pkt
1208  *	ECOMM:		Could not send packet
1209  *	ENONET		No route to dst. It is up to the caller
1210  *			to send icmp unreachable error message,
1211  *	EINPROGRESS	The macaddr of the onlink dst or that
1212  *			of the offlink dst's nexthop needs to get
1213  *			resolved before packet can be sent to dst.
1214  *			Thus transmission is not guaranteed.
1215  *
1216  */
1217 
1218 int
1219 ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex,
1220     zoneid_t zoneid)
1221 {
1222 	ire_t *ire = NULL, *sire = NULL;
1223 	ire_t *ire_cache = NULL;
1224 	boolean_t   check_multirt = B_FALSE;
1225 	int value;
1226 	int match_flags;
1227 	ipaddr_t dst;
1228 
1229 	ASSERT(mp != NULL);
1230 
1231 	ASSERT(dst_addr->sa_family == AF_INET ||
1232 	    dst_addr->sa_family == AF_INET6);
1233 
1234 	if (dst_addr->sa_family == AF_INET) {
1235 		dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr;
1236 	} else {
1237 		/*
1238 		 * We dont have support for V6 yet. It will be provided
1239 		 * once RFE  6399103  has been delivered.
1240 		 * Until then, for V6 dsts, IP Filter will not call
1241 		 * this function. Instead, IP Filter will continue to do what
1242 		 * has been done since S10, namely it will use
1243 		 * ip_nexthop(),ip_nexthop_route() to obtain the
1244 		 * link-layer address of a V6 dst and then process the
1245 		 * packet and send it out on the wire on its own.
1246 		 */
1247 		ip1dbg(("ipfil_sendpkt: no V6 support \n"));
1248 		value = ECOMM;
1249 		freemsg(mp);
1250 		goto discard;
1251 	}
1252 
1253 	/*
1254 	 * Lets get the ire. We might get the ire cache entry,
1255 	 * or the ire,sire pair needed to create the cache entry.
1256 	 * XXX pass NULL tsl for now.
1257 	 */
1258 
1259 	if (ifindex == 0) {
1260 		/* There is no supplied index. So use the FIB info */
1261 
1262 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1263 		    MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
1264 		ire = ire_route_lookup(dst,
1265 		    0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp),
1266 		    match_flags);
1267 	} else {
1268 		ipif_t *supplied_ipif;
1269 		ill_t *ill;
1270 
1271 		/*
1272 		 * If supplied ifindex is non-null, the only valid
1273 		 * nexthop is one off of the interface corresponding
1274 		 * to the specified ifindex.
1275 		 */
1276 
1277 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
1278 		    NULL, NULL, NULL, NULL);
1279 		if (ill != NULL) {
1280 			supplied_ipif = ipif_get_next_ipif(NULL, ill);
1281 		} else {
1282 			ip1dbg(("ipfil_sendpkt: Could not find"
1283 			    " route to dst\n"));
1284 			value = ECOMM;
1285 			freemsg(mp);
1286 			goto discard;
1287 		}
1288 
1289 		match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
1290 		    MATCH_IRE_IPIF | MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE|
1291 		    MATCH_IRE_SECATTR);
1292 
1293 		ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif,
1294 		    &sire, zoneid, MBLK_GETLABEL(mp), match_flags);
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_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 
1614 	if (ire_origin == NULL) {
1615 		/*
1616 		 * first time through routine, or we dropped off the end
1617 		 * of list.
1618 		 */
1619 		ire_origin = irb_ptr->irb_ire;
1620 	}
1621 	irb_ptr->irb_rr_origin = ire_origin;
1622 	IRB_REFHOLD_LOCKED(irb_ptr);
1623 	rw_exit(&irb_ptr->irb_lock);
1624 
1625 	/*
1626 	 * Round-robin the routers list looking for a route that
1627 	 * matches the passed in parameters.
1628 	 * We start with the ire we found above and we walk the hash
1629 	 * list until we're back where we started. It doesn't matter if
1630 	 * routes are added or deleted by other threads - we know this
1631 	 * ire will stay in the list because we hold a reference on the
1632 	 * ire bucket.
1633 	 */
1634 	ire = ire_origin;
1635 	while (ire != NULL) {
1636 		int match_flags = 0;
1637 		ire_t *rire;
1638 
1639 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
1640 			goto next_ire;
1641 
1642 		if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0,
1643 		    margs->ift_gateway, margs->ift_type, margs->ift_ipif,
1644 		    margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
1645 		    margs->ift_flags))
1646 			goto next_ire;
1647 
1648 		if (ire->ire_type & IRE_INTERFACE) {
1649 			/*
1650 			 * keep looking to see if there is a non-interface
1651 			 * default ire, but save this one as a last resort.
1652 			 */
1653 			if (maybe_ire == NULL)
1654 				maybe_ire = ire;
1655 			goto next_ire;
1656 		}
1657 
1658 		if (zoneid == ALL_ZONES) {
1659 			IRE_REFHOLD(ire);
1660 			IRB_REFRELE(irb_ptr);
1661 			return (ire);
1662 		}
1663 		/*
1664 		 * When we're in a local zone, we're only
1665 		 * interested in routers that are
1666 		 * reachable through ipifs within our zone.
1667 		 */
1668 		if (ire->ire_ipif != NULL) {
1669 			match_flags |= MATCH_IRE_ILL_GROUP;
1670 		}
1671 		rire = ire_route_lookup(ire->ire_gateway_addr,
1672 		    0, 0, 0, ire->ire_ipif, NULL, zoneid, margs->ift_tsl,
1673 		    match_flags);
1674 		if (rire != NULL) {
1675 			ire_refrele(rire);
1676 			IRE_REFHOLD(ire);
1677 			IRB_REFRELE(irb_ptr);
1678 			return (ire);
1679 		}
1680 next_ire:
1681 		ire = (ire->ire_next ?  ire->ire_next : irb_ptr->irb_ire);
1682 		if (ire == ire_origin)
1683 			break;
1684 	}
1685 	if (maybe_ire != NULL)
1686 		IRE_REFHOLD(maybe_ire);
1687 	IRB_REFRELE(irb_ptr);
1688 	return (maybe_ire);
1689 }
1690