xref: /freebsd/sys/net/route.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
1 /*-
2  * Copyright (c) 1980, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)route.c	8.3.1.1 (Berkeley) 2/23/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet.h"
34 #include "opt_mrouting.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/domain.h>
42 #include <sys/kernel.h>
43 
44 #include <net/if.h>
45 #include <net/route.h>
46 
47 #include <netinet/in.h>
48 #include <netinet/ip_mroute.h>
49 
50 #include <vm/uma.h>
51 
52 static struct rtstat rtstat;
53 struct radix_node_head *rt_tables[AF_MAX+1];
54 
55 static int	rttrash;		/* routes not in table but not freed */
56 
57 static void rt_maskedcopy(struct sockaddr *,
58 	    struct sockaddr *, struct sockaddr *);
59 static void rtable_init(void **);
60 
61 /* compare two sockaddr structures */
62 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
63 
64 /*
65  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
66  * The operation can be done safely (in this code) because a
67  * 'struct rtentry' starts with two 'struct radix_node''s, the first
68  * one representing leaf nodes in the routing tree, which is
69  * what the code in radix.c passes us as a 'struct radix_node'.
70  *
71  * But because there are a lot of assumptions in this conversion,
72  * do not cast explicitly, but always use the macro below.
73  */
74 #define RNTORT(p)	((struct rtentry *)(p))
75 
76 static void
77 rtable_init(void **table)
78 {
79 	struct domain *dom;
80 	for (dom = domains; dom; dom = dom->dom_next)
81 		if (dom->dom_rtattach)
82 			dom->dom_rtattach(&table[dom->dom_family],
83 			    dom->dom_rtoffset);
84 }
85 
86 static uma_zone_t rtzone;		/* Routing table UMA zone. */
87 
88 static void
89 route_init(void)
90 {
91 	rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
92 	    NULL, NULL, UMA_ALIGN_PTR, 0);
93 	rn_init();	/* initialize all zeroes, all ones, mask table */
94 	rtable_init((void **)rt_tables);
95 }
96 
97 /*
98  * Packet routing routines.
99  */
100 void
101 rtalloc(struct route *ro)
102 {
103 	rtalloc_ign(ro, 0UL);
104 }
105 
106 void
107 rtalloc_ign(struct route *ro, u_long ignore)
108 {
109 	struct rtentry *rt;
110 
111 	if ((rt = ro->ro_rt) != NULL) {
112 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
113 			return;
114 		RTFREE(rt);
115 		ro->ro_rt = NULL;
116 	}
117 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
118 	if (ro->ro_rt)
119 		RT_UNLOCK(ro->ro_rt);
120 }
121 
122 /*
123  * Look up the route that matches the address given
124  * Or, at least try.. Create a cloned route if needed.
125  *
126  * The returned route, if any, is locked.
127  */
128 struct rtentry *
129 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
130 {
131 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
132 	struct rtentry *rt;
133 	struct radix_node *rn;
134 	struct rtentry *newrt;
135 	struct rt_addrinfo info;
136 	u_long nflags;
137 	int err = 0, msgtype = RTM_MISS;
138 
139 	newrt = NULL;
140 	bzero(&info, sizeof(info));
141 	/*
142 	 * Look up the address in the table for that Address Family
143 	 */
144 	if (rnh == NULL) {
145 		rtstat.rts_unreach++;
146 		goto miss2;
147 	}
148 	RADIX_NODE_HEAD_LOCK(rnh);
149 	if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
150 	    (rn->rn_flags & RNF_ROOT) == 0) {
151 		/*
152 		 * If we find it and it's not the root node, then
153 		 * get a refernce on the rtentry associated.
154 		 */
155 		newrt = rt = RNTORT(rn);
156 		nflags = rt->rt_flags & ~ignflags;
157 		if (report && (nflags & RTF_CLONING)) {
158 			/*
159 			 * We are apparently adding (report = 0 in delete).
160 			 * If it requires that it be cloned, do so.
161 			 * (This implies it wasn't a HOST route.)
162 			 */
163 			err = rtrequest(RTM_RESOLVE, dst, NULL,
164 					      NULL, 0, &newrt);
165 			if (err) {
166 				/*
167 				 * If the cloning didn't succeed, maybe
168 				 * what we have will do. Return that.
169 				 */
170 				newrt = rt;		/* existing route */
171 				RT_LOCK(newrt);
172 				RT_ADDREF(newrt);
173 				goto miss;
174 			}
175 			KASSERT(newrt, ("no route and no error"));
176 			RT_LOCK(newrt);
177 			if (newrt->rt_flags & RTF_XRESOLVE) {
178 				/*
179 				 * If the new route specifies it be
180 				 * externally resolved, then go do that.
181 				 */
182 				msgtype = RTM_RESOLVE;
183 				goto miss;
184 			}
185 			/* Inform listeners of the new route. */
186 			info.rti_info[RTAX_DST] = rt_key(newrt);
187 			info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
188 			info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
189 			if (newrt->rt_ifp != NULL) {
190 				info.rti_info[RTAX_IFP] =
191 				    newrt->rt_ifp->if_addr->ifa_addr;
192 				info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
193 			}
194 			rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
195 		} else {
196 			KASSERT(rt == newrt, ("locking wrong route"));
197 			RT_LOCK(newrt);
198 			RT_ADDREF(newrt);
199 		}
200 		RADIX_NODE_HEAD_UNLOCK(rnh);
201 	} else {
202 		/*
203 		 * Either we hit the root or couldn't find any match,
204 		 * Which basically means
205 		 * "caint get there frm here"
206 		 */
207 		rtstat.rts_unreach++;
208 	miss:
209 		RADIX_NODE_HEAD_UNLOCK(rnh);
210 	miss2:	if (report) {
211 			/*
212 			 * If required, report the failure to the supervising
213 			 * Authorities.
214 			 * For a delete, this is not an error. (report == 0)
215 			 */
216 			info.rti_info[RTAX_DST] = dst;
217 			rt_missmsg(msgtype, &info, 0, err);
218 		}
219 	}
220 	if (newrt)
221 		RT_LOCK_ASSERT(newrt);
222 	return (newrt);
223 }
224 
225 /*
226  * Remove a reference count from an rtentry.
227  * If the count gets low enough, take it out of the routing table
228  */
229 void
230 rtfree(struct rtentry *rt)
231 {
232 	struct radix_node_head *rnh;
233 
234 	/* XXX the NULL checks are probably useless */
235 	if (rt == NULL)
236 		panic("rtfree: NULL rt");
237 	rnh = rt_tables[rt_key(rt)->sa_family];
238 	if (rnh == NULL)
239 		panic("rtfree: NULL rnh");
240 
241 	RT_LOCK_ASSERT(rt);
242 
243 	/*
244 	 * decrement the reference count by one and if it reaches 0,
245 	 * and there is a close function defined, call the close function
246 	 */
247 	RT_REMREF(rt);
248 	if (rt->rt_refcnt > 0)
249 		goto done;
250 
251 	/*
252 	 * On last reference give the "close method" a chance
253 	 * to cleanup private state.  This also permits (for
254 	 * IPv4 and IPv6) a chance to decide if the routing table
255 	 * entry should be purged immediately or at a later time.
256 	 * When an immediate purge is to happen the close routine
257 	 * typically calls rtexpunge which clears the RTF_UP flag
258 	 * on the entry so that the code below reclaims the storage.
259 	 */
260 	if (rt->rt_refcnt == 0 && rnh->rnh_close)
261 		rnh->rnh_close((struct radix_node *)rt, rnh);
262 
263 	/*
264 	 * If we are no longer "up" (and ref == 0)
265 	 * then we can free the resources associated
266 	 * with the route.
267 	 */
268 	if ((rt->rt_flags & RTF_UP) == 0) {
269 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
270 			panic ("rtfree 2");
271 		/*
272 		 * the rtentry must have been removed from the routing table
273 		 * so it is represented in rttrash.. remove that now.
274 		 */
275 		rttrash--;
276 #ifdef	DIAGNOSTIC
277 		if (rt->rt_refcnt < 0) {
278 			printf("rtfree: %p not freed (neg refs)\n", rt);
279 			goto done;
280 		}
281 #endif
282 		/*
283 		 * release references on items we hold them on..
284 		 * e.g other routes and ifaddrs.
285 		 */
286 		if (rt->rt_ifa)
287 			IFAFREE(rt->rt_ifa);
288 		rt->rt_parent = NULL;		/* NB: no refcnt on parent */
289 
290 		/*
291 		 * The key is separatly alloc'd so free it (see rt_setgate()).
292 		 * This also frees the gateway, as they are always malloc'd
293 		 * together.
294 		 */
295 		Free(rt_key(rt));
296 
297 		/*
298 		 * and the rtentry itself of course
299 		 */
300 		RT_LOCK_DESTROY(rt);
301 		uma_zfree(rtzone, rt);
302 		return;
303 	}
304 done:
305 	RT_UNLOCK(rt);
306 }
307 
308 
309 /*
310  * Force a routing table entry to the specified
311  * destination to go through the given gateway.
312  * Normally called as a result of a routing redirect
313  * message from the network layer.
314  */
315 void
316 rtredirect(struct sockaddr *dst,
317 	struct sockaddr *gateway,
318 	struct sockaddr *netmask,
319 	int flags,
320 	struct sockaddr *src)
321 {
322 	struct rtentry *rt;
323 	int error = 0;
324 	short *stat = NULL;
325 	struct rt_addrinfo info;
326 	struct ifaddr *ifa;
327 
328 	/* verify the gateway is directly reachable */
329 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
330 		error = ENETUNREACH;
331 		goto out;
332 	}
333 	rt = rtalloc1(dst, 0, 0UL);	/* NB: rt is locked */
334 	/*
335 	 * If the redirect isn't from our current router for this dst,
336 	 * it's either old or wrong.  If it redirects us to ourselves,
337 	 * we have a routing loop, perhaps as a result of an interface
338 	 * going down recently.
339 	 */
340 	if (!(flags & RTF_DONE) && rt &&
341 	     (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
342 		error = EINVAL;
343 	else if (ifa_ifwithaddr(gateway))
344 		error = EHOSTUNREACH;
345 	if (error)
346 		goto done;
347 	/*
348 	 * Create a new entry if we just got back a wildcard entry
349 	 * or the the lookup failed.  This is necessary for hosts
350 	 * which use routing redirects generated by smart gateways
351 	 * to dynamically build the routing tables.
352 	 */
353 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
354 		goto create;
355 	/*
356 	 * Don't listen to the redirect if it's
357 	 * for a route to an interface.
358 	 */
359 	if (rt->rt_flags & RTF_GATEWAY) {
360 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
361 			/*
362 			 * Changing from route to net => route to host.
363 			 * Create new route, rather than smashing route to net.
364 			 */
365 		create:
366 			if (rt)
367 				rtfree(rt);
368 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
369 			bzero((caddr_t)&info, sizeof(info));
370 			info.rti_info[RTAX_DST] = dst;
371 			info.rti_info[RTAX_GATEWAY] = gateway;
372 			info.rti_info[RTAX_NETMASK] = netmask;
373 			info.rti_ifa = ifa;
374 			info.rti_flags = flags;
375 			rt = NULL;
376 			error = rtrequest1(RTM_ADD, &info, &rt);
377 			if (rt != NULL) {
378 				RT_LOCK(rt);
379 				flags = rt->rt_flags;
380 			}
381 			stat = &rtstat.rts_dynamic;
382 		} else {
383 			/*
384 			 * Smash the current notion of the gateway to
385 			 * this destination.  Should check about netmask!!!
386 			 */
387 			rt->rt_flags |= RTF_MODIFIED;
388 			flags |= RTF_MODIFIED;
389 			stat = &rtstat.rts_newgateway;
390 			/*
391 			 * add the key and gateway (in one malloc'd chunk).
392 			 */
393 			rt_setgate(rt, rt_key(rt), gateway);
394 		}
395 	} else
396 		error = EHOSTUNREACH;
397 done:
398 	if (rt)
399 		rtfree(rt);
400 out:
401 	if (error)
402 		rtstat.rts_badredirect++;
403 	else if (stat != NULL)
404 		(*stat)++;
405 	bzero((caddr_t)&info, sizeof(info));
406 	info.rti_info[RTAX_DST] = dst;
407 	info.rti_info[RTAX_GATEWAY] = gateway;
408 	info.rti_info[RTAX_NETMASK] = netmask;
409 	info.rti_info[RTAX_AUTHOR] = src;
410 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
411 }
412 
413 /*
414  * Routing table ioctl interface.
415  */
416 int
417 rtioctl(u_long req, caddr_t data)
418 {
419 
420 	/*
421 	 * If more ioctl commands are added here, make sure the proper
422 	 * super-user checks are being performed because it is possible for
423 	 * prison-root to make it this far if raw sockets have been enabled
424 	 * in jails.
425 	 */
426 #ifdef INET
427 	/* Multicast goop, grrr... */
428 	return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
429 #else /* INET */
430 	return ENXIO;
431 #endif /* INET */
432 }
433 
434 struct ifaddr *
435 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
436 {
437 	register struct ifaddr *ifa;
438 
439 	if ((flags & RTF_GATEWAY) == 0) {
440 		/*
441 		 * If we are adding a route to an interface,
442 		 * and the interface is a pt to pt link
443 		 * we should search for the destination
444 		 * as our clue to the interface.  Otherwise
445 		 * we can use the local address.
446 		 */
447 		ifa = NULL;
448 		if (flags & RTF_HOST)
449 			ifa = ifa_ifwithdstaddr(dst);
450 		if (ifa == NULL)
451 			ifa = ifa_ifwithaddr(gateway);
452 	} else {
453 		/*
454 		 * If we are adding a route to a remote net
455 		 * or host, the gateway may still be on the
456 		 * other end of a pt to pt link.
457 		 */
458 		ifa = ifa_ifwithdstaddr(gateway);
459 	}
460 	if (ifa == NULL)
461 		ifa = ifa_ifwithnet(gateway);
462 	if (ifa == NULL) {
463 		struct rtentry *rt = rtalloc1(gateway, 0, 0UL);
464 		if (rt == NULL)
465 			return (NULL);
466 		RT_REMREF(rt);
467 		RT_UNLOCK(rt);
468 		if ((ifa = rt->rt_ifa) == NULL)
469 			return (NULL);
470 	}
471 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
472 		struct ifaddr *oifa = ifa;
473 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
474 		if (ifa == NULL)
475 			ifa = oifa;
476 	}
477 	return (ifa);
478 }
479 
480 static walktree_f_t rt_fixdelete;
481 static walktree_f_t rt_fixchange;
482 
483 struct rtfc_arg {
484 	struct rtentry *rt0;
485 	struct radix_node_head *rnh;
486 };
487 
488 /*
489  * Do appropriate manipulations of a routing tree given
490  * all the bits of info needed
491  */
492 int
493 rtrequest(int req,
494 	struct sockaddr *dst,
495 	struct sockaddr *gateway,
496 	struct sockaddr *netmask,
497 	int flags,
498 	struct rtentry **ret_nrt)
499 {
500 	struct rt_addrinfo info;
501 
502 	if (dst->sa_len == 0)
503 		return(EINVAL);
504 
505 	bzero((caddr_t)&info, sizeof(info));
506 	info.rti_flags = flags;
507 	info.rti_info[RTAX_DST] = dst;
508 	info.rti_info[RTAX_GATEWAY] = gateway;
509 	info.rti_info[RTAX_NETMASK] = netmask;
510 	return rtrequest1(req, &info, ret_nrt);
511 }
512 
513 /*
514  * These (questionable) definitions of apparent local variables apply
515  * to the next two functions.  XXXXXX!!!
516  */
517 #define	dst	info->rti_info[RTAX_DST]
518 #define	gateway	info->rti_info[RTAX_GATEWAY]
519 #define	netmask	info->rti_info[RTAX_NETMASK]
520 #define	ifaaddr	info->rti_info[RTAX_IFA]
521 #define	ifpaddr	info->rti_info[RTAX_IFP]
522 #define	flags	info->rti_flags
523 
524 int
525 rt_getifa(struct rt_addrinfo *info)
526 {
527 	struct ifaddr *ifa;
528 	int error = 0;
529 
530 	/*
531 	 * ifp may be specified by sockaddr_dl
532 	 * when protocol address is ambiguous.
533 	 */
534 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
535 	    ifpaddr->sa_family == AF_LINK &&
536 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
537 		info->rti_ifp = ifa->ifa_ifp;
538 	if (info->rti_ifa == NULL && ifaaddr != NULL)
539 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
540 	if (info->rti_ifa == NULL) {
541 		struct sockaddr *sa;
542 
543 		sa = ifaaddr != NULL ? ifaaddr :
544 		    (gateway != NULL ? gateway : dst);
545 		if (sa != NULL && info->rti_ifp != NULL)
546 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
547 		else if (dst != NULL && gateway != NULL)
548 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
549 		else if (sa != NULL)
550 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
551 	}
552 	if ((ifa = info->rti_ifa) != NULL) {
553 		if (info->rti_ifp == NULL)
554 			info->rti_ifp = ifa->ifa_ifp;
555 	} else
556 		error = ENETUNREACH;
557 	return (error);
558 }
559 
560 /*
561  * Expunges references to a route that's about to be reclaimed.
562  * The route must be locked.
563  */
564 int
565 rtexpunge(struct rtentry *rt)
566 {
567 	struct radix_node *rn;
568 	struct radix_node_head *rnh;
569 	struct ifaddr *ifa;
570 	int error = 0;
571 
572 	RT_LOCK_ASSERT(rt);
573 #if 0
574 	/*
575 	 * We cannot assume anything about the reference count
576 	 * because protocols call us in many situations; often
577 	 * before unwinding references to the table entry.
578 	 */
579 	KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
580 #endif
581 	/*
582 	 * Find the correct routing tree to use for this Address Family
583 	 */
584 	rnh = rt_tables[rt_key(rt)->sa_family];
585 	if (rnh == NULL)
586 		return (EAFNOSUPPORT);
587 
588 	RADIX_NODE_HEAD_LOCK(rnh);
589 
590 	/*
591 	 * Remove the item from the tree; it should be there,
592 	 * but when callers invoke us blindly it may not (sigh).
593 	 */
594 	rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
595 	if (rn == NULL) {
596 		error = ESRCH;
597 		goto bad;
598 	}
599 	KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
600 		("unexpected flags 0x%x", rn->rn_flags));
601 	KASSERT(rt == RNTORT(rn),
602 		("lookup mismatch, rt %p rn %p", rt, rn));
603 
604 	rt->rt_flags &= ~RTF_UP;
605 
606 	/*
607 	 * Now search what's left of the subtree for any cloned
608 	 * routes which might have been formed from this node.
609 	 */
610 	if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
611 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
612 				       rt_fixdelete, rt);
613 
614 	/*
615 	 * Remove any external references we may have.
616 	 * This might result in another rtentry being freed if
617 	 * we held its last reference.
618 	 */
619 	if (rt->rt_gwroute) {
620 		RTFREE(rt->rt_gwroute);
621 		rt->rt_gwroute = NULL;
622 	}
623 
624 	/*
625 	 * Give the protocol a chance to keep things in sync.
626 	 */
627 	if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
628 		struct rt_addrinfo info;
629 
630 		bzero((caddr_t)&info, sizeof(info));
631 		info.rti_flags = rt->rt_flags;
632 		info.rti_info[RTAX_DST] = rt_key(rt);
633 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
634 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
635 		ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
636 	}
637 
638 	/*
639 	 * one more rtentry floating around that is not
640 	 * linked to the routing table.
641 	 */
642 	rttrash++;
643 bad:
644 	RADIX_NODE_HEAD_UNLOCK(rnh);
645 	return (error);
646 }
647 
648 int
649 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
650 {
651 	int error = 0;
652 	register struct rtentry *rt;
653 	register struct radix_node *rn;
654 	register struct radix_node_head *rnh;
655 	struct ifaddr *ifa;
656 	struct sockaddr *ndst;
657 #define senderr(x) { error = x ; goto bad; }
658 
659 	/*
660 	 * Find the correct routing tree to use for this Address Family
661 	 */
662 	rnh = rt_tables[dst->sa_family];
663 	if (rnh == NULL)
664 		return (EAFNOSUPPORT);
665 	RADIX_NODE_HEAD_LOCK(rnh);
666 	/*
667 	 * If we are adding a host route then we don't want to put
668 	 * a netmask in the tree, nor do we want to clone it.
669 	 */
670 	if (flags & RTF_HOST) {
671 		netmask = NULL;
672 		flags &= ~RTF_CLONING;
673 	}
674 	switch (req) {
675 	case RTM_DELETE:
676 		/*
677 		 * Remove the item from the tree and return it.
678 		 * Complain if it is not there and do no more processing.
679 		 */
680 		rn = rnh->rnh_deladdr(dst, netmask, rnh);
681 		if (rn == NULL)
682 			senderr(ESRCH);
683 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
684 			panic ("rtrequest delete");
685 		rt = RNTORT(rn);
686 		RT_LOCK(rt);
687 		RT_ADDREF(rt);
688 		rt->rt_flags &= ~RTF_UP;
689 
690 		/*
691 		 * Now search what's left of the subtree for any cloned
692 		 * routes which might have been formed from this node.
693 		 */
694 		if ((rt->rt_flags & RTF_CLONING) &&
695 		    rt_mask(rt)) {
696 			rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
697 					       rt_fixdelete, rt);
698 		}
699 
700 		/*
701 		 * Remove any external references we may have.
702 		 * This might result in another rtentry being freed if
703 		 * we held its last reference.
704 		 */
705 		if (rt->rt_gwroute) {
706 			RTFREE(rt->rt_gwroute);
707 			rt->rt_gwroute = NULL;
708 		}
709 
710 		/*
711 		 * give the protocol a chance to keep things in sync.
712 		 */
713 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
714 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
715 
716 		/*
717 		 * One more rtentry floating around that is not
718 		 * linked to the routing table. rttrash will be decremented
719 		 * when RTFREE(rt) is eventually called.
720 		 */
721 		rttrash++;
722 
723 		/*
724 		 * If the caller wants it, then it can have it,
725 		 * but it's up to it to free the rtentry as we won't be
726 		 * doing it.
727 		 */
728 		if (ret_nrt) {
729 			*ret_nrt = rt;
730 			RT_UNLOCK(rt);
731 		} else
732 			RTFREE_LOCKED(rt);
733 		break;
734 
735 	case RTM_RESOLVE:
736 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
737 			senderr(EINVAL);
738 		ifa = rt->rt_ifa;
739 		/* XXX locking? */
740 		flags = rt->rt_flags &
741 		    ~(RTF_CLONING | RTF_STATIC);
742 		flags |= RTF_WASCLONED;
743 		gateway = rt->rt_gateway;
744 		if ((netmask = rt->rt_genmask) == NULL)
745 			flags |= RTF_HOST;
746 		goto makeroute;
747 
748 	case RTM_ADD:
749 		if ((flags & RTF_GATEWAY) && !gateway)
750 			senderr(EINVAL);
751 		if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
752 		    (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
753 			senderr(EINVAL);
754 
755 		if (info->rti_ifa == NULL && (error = rt_getifa(info)))
756 			senderr(error);
757 		ifa = info->rti_ifa;
758 
759 	makeroute:
760 		rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
761 		if (rt == NULL)
762 			senderr(ENOBUFS);
763 		RT_LOCK_INIT(rt);
764 		rt->rt_flags = RTF_UP | flags;
765 		/*
766 		 * Add the gateway. Possibly re-malloc-ing the storage for it
767 		 * also add the rt_gwroute if possible.
768 		 */
769 		RT_LOCK(rt);
770 		if ((error = rt_setgate(rt, dst, gateway)) != 0) {
771 			RT_LOCK_DESTROY(rt);
772 			uma_zfree(rtzone, rt);
773 			senderr(error);
774 		}
775 
776 		/*
777 		 * point to the (possibly newly malloc'd) dest address.
778 		 */
779 		ndst = (struct sockaddr *)rt_key(rt);
780 
781 		/*
782 		 * make sure it contains the value we want (masked if needed).
783 		 */
784 		if (netmask) {
785 			rt_maskedcopy(dst, ndst, netmask);
786 		} else
787 			bcopy(dst, ndst, dst->sa_len);
788 
789 		/*
790 		 * Note that we now have a reference to the ifa.
791 		 * This moved from below so that rnh->rnh_addaddr() can
792 		 * examine the ifa and  ifa->ifa_ifp if it so desires.
793 		 */
794 		IFAREF(ifa);
795 		rt->rt_ifa = ifa;
796 		rt->rt_ifp = ifa->ifa_ifp;
797 
798 		/* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
799 		rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
800 		if (rn == NULL) {
801 			struct rtentry *rt2;
802 			/*
803 			 * Uh-oh, we already have one of these in the tree.
804 			 * We do a special hack: if the route that's already
805 			 * there was generated by the cloning mechanism
806 			 * then we just blow it away and retry the insertion
807 			 * of the new one.
808 			 */
809 			rt2 = rtalloc1(dst, 0, 0);
810 			if (rt2 && rt2->rt_parent) {
811 				rtexpunge(rt2);
812 				RT_UNLOCK(rt2);
813 				rn = rnh->rnh_addaddr(ndst, netmask,
814 						      rnh, rt->rt_nodes);
815 			} else if (rt2) {
816 				/* undo the extra ref we got */
817 				RTFREE_LOCKED(rt2);
818 			}
819 		}
820 
821 		/*
822 		 * If it still failed to go into the tree,
823 		 * then un-make it (this should be a function)
824 		 */
825 		if (rn == NULL) {
826 			if (rt->rt_gwroute)
827 				RTFREE(rt->rt_gwroute);
828 			if (rt->rt_ifa)
829 				IFAFREE(rt->rt_ifa);
830 			Free(rt_key(rt));
831 			RT_LOCK_DESTROY(rt);
832 			uma_zfree(rtzone, rt);
833 			senderr(EEXIST);
834 		}
835 
836 		rt->rt_parent = NULL;
837 
838 		/*
839 		 * If we got here from RESOLVE, then we are cloning
840 		 * so clone the rest, and note that we
841 		 * are a clone (and increment the parent's references)
842 		 */
843 		if (req == RTM_RESOLVE) {
844 			KASSERT(ret_nrt && *ret_nrt,
845 				("no route to clone from"));
846 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
847 			rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
848 			if ((*ret_nrt)->rt_flags & RTF_CLONING) {
849 				/*
850 				 * NB: We do not bump the refcnt on the parent
851 				 * entry under the assumption that it will
852 				 * remain so long as we do.  This is
853 				 * important when deleting the parent route
854 				 * as this operation requires traversing
855 				 * the tree to delete all clones and futzing
856 				 * with refcnts requires us to double-lock
857 				 * parent through this back reference.
858 				 */
859 				rt->rt_parent = *ret_nrt;
860 			}
861 		}
862 
863 		/*
864 		 * if this protocol has something to add to this then
865 		 * allow it to do that as well.
866 		 */
867 		if (ifa->ifa_rtrequest)
868 			ifa->ifa_rtrequest(req, rt, info);
869 
870 		/*
871 		 * We repeat the same procedure from rt_setgate() here because
872 		 * it doesn't fire when we call it there because the node
873 		 * hasn't been added to the tree yet.
874 		 */
875 		if (req == RTM_ADD &&
876 		    !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
877 			struct rtfc_arg arg;
878 			arg.rnh = rnh;
879 			arg.rt0 = rt;
880 			rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
881 					       rt_fixchange, &arg);
882 		}
883 
884 		/*
885 		 * actually return a resultant rtentry and
886 		 * give the caller a single reference.
887 		 */
888 		if (ret_nrt) {
889 			*ret_nrt = rt;
890 			RT_ADDREF(rt);
891 		}
892 		RT_UNLOCK(rt);
893 		break;
894 	default:
895 		error = EOPNOTSUPP;
896 	}
897 bad:
898 	RADIX_NODE_HEAD_UNLOCK(rnh);
899 	return (error);
900 #undef senderr
901 }
902 
903 #undef dst
904 #undef gateway
905 #undef netmask
906 #undef ifaaddr
907 #undef ifpaddr
908 #undef flags
909 
910 /*
911  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
912  * (i.e., the routes related to it by the operation of cloning).  This
913  * routine is iterated over all potential former-child-routes by way of
914  * rnh->rnh_walktree_from() above, and those that actually are children of
915  * the late parent (passed in as VP here) are themselves deleted.
916  */
917 static int
918 rt_fixdelete(struct radix_node *rn, void *vp)
919 {
920 	struct rtentry *rt = RNTORT(rn);
921 	struct rtentry *rt0 = vp;
922 
923 	if (rt->rt_parent == rt0 &&
924 	    !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
925 		return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
926 				 rt->rt_flags, NULL);
927 	}
928 	return 0;
929 }
930 
931 /*
932  * This routine is called from rt_setgate() to do the analogous thing for
933  * adds and changes.  There is the added complication in this case of a
934  * middle insert; i.e., insertion of a new network route between an older
935  * network route and (cloned) host routes.  For this reason, a simple check
936  * of rt->rt_parent is insufficient; each candidate route must be tested
937  * against the (mask, value) of the new route (passed as before in vp)
938  * to see if the new route matches it.
939  *
940  * XXX - it may be possible to do fixdelete() for changes and reserve this
941  * routine just for adds.  I'm not sure why I thought it was necessary to do
942  * changes this way.
943  */
944 
945 static int
946 rt_fixchange(struct radix_node *rn, void *vp)
947 {
948 	struct rtentry *rt = RNTORT(rn);
949 	struct rtfc_arg *ap = vp;
950 	struct rtentry *rt0 = ap->rt0;
951 	struct radix_node_head *rnh = ap->rnh;
952 	u_char *xk1, *xm1, *xk2, *xmp;
953 	int i, len, mlen;
954 
955 	/* make sure we have a parent, and route is not pinned or cloning */
956 	if (!rt->rt_parent ||
957 	    (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
958 		return 0;
959 
960 	if (rt->rt_parent == rt0)	/* parent match */
961 		goto delete_rt;
962 	/*
963 	 * There probably is a function somewhere which does this...
964 	 * if not, there should be.
965 	 */
966 	len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
967 
968 	xk1 = (u_char *)rt_key(rt0);
969 	xm1 = (u_char *)rt_mask(rt0);
970 	xk2 = (u_char *)rt_key(rt);
971 
972 	/* avoid applying a less specific route */
973 	xmp = (u_char *)rt_mask(rt->rt_parent);
974 	mlen = rt_key(rt->rt_parent)->sa_len;
975 	if (mlen > rt_key(rt0)->sa_len)		/* less specific route */
976 		return 0;
977 	for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
978 		if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
979 			return 0;	/* less specific route */
980 
981 	for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
982 		if ((xk2[i] & xm1[i]) != xk1[i])
983 			return 0;	/* no match */
984 
985 	/*
986 	 * OK, this node is a clone, and matches the node currently being
987 	 * changed/added under the node's mask.  So, get rid of it.
988 	 */
989 delete_rt:
990 	return rtrequest(RTM_DELETE, rt_key(rt), NULL,
991 			 rt_mask(rt), rt->rt_flags, NULL);
992 }
993 
994 int
995 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
996 {
997 	/* XXX dst may be overwritten, can we move this to below */
998 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
999 	int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1000 
1001 	RT_LOCK_ASSERT(rt);
1002 
1003 	/*
1004 	 * A host route with the destination equal to the gateway
1005 	 * will interfere with keeping LLINFO in the routing
1006 	 * table, so disallow it.
1007 	 */
1008 	if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1009 					(RTF_HOST|RTF_GATEWAY)) &&
1010 	    dst->sa_len == gate->sa_len &&
1011 	    bcmp(dst, gate, dst->sa_len) == 0) {
1012 		/*
1013 		 * The route might already exist if this is an RTM_CHANGE
1014 		 * or a routing redirect, so try to delete it.
1015 		 */
1016 		if (rt_key(rt))
1017 			rtexpunge(rt);
1018 		return EADDRNOTAVAIL;
1019 	}
1020 
1021 	/*
1022 	 * Cloning loop avoidance in case of bad configuration.
1023 	 */
1024 	if (rt->rt_flags & RTF_GATEWAY) {
1025 		struct rtentry *gwrt;
1026 
1027 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1028 		gwrt = rtalloc1(gate, 1, 0);
1029 		if (gwrt == rt) {
1030 			RT_LOCK_ASSERT(rt);
1031 			RT_REMREF(rt);
1032 			return (EADDRINUSE); /* failure */
1033 		}
1034 		RT_LOCK(rt);
1035 		/*
1036 		 * If there is already a gwroute, then drop it. If we
1037 		 * are asked to replace route with itself, then do
1038 		 * not leak its refcounter.
1039 		 */
1040 		if (rt->rt_gwroute != NULL) {
1041 			if (rt->rt_gwroute == gwrt) {
1042 				RT_REMREF(rt->rt_gwroute);
1043 			} else
1044 				RTFREE(rt->rt_gwroute);
1045 		}
1046 
1047 		if ((rt->rt_gwroute = gwrt) != NULL)
1048 			RT_UNLOCK(rt->rt_gwroute);
1049 	}
1050 
1051 	/*
1052 	 * Prepare to store the gateway in rt->rt_gateway.
1053 	 * Both dst and gateway are stored one after the other in the same
1054 	 * malloc'd chunk. If we have room, we can reuse the old buffer,
1055 	 * rt_gateway already points to the right place.
1056 	 * Otherwise, malloc a new block and update the 'dst' address.
1057 	 */
1058 	if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1059 		caddr_t new;
1060 
1061 		R_Malloc(new, caddr_t, dlen + glen);
1062 		if (new == NULL)
1063 			return ENOBUFS;
1064 		/*
1065 		 * XXX note, we copy from *dst and not *rt_key(rt) because
1066 		 * rt_setgate() can be called to initialize a newly
1067 		 * allocated route entry, in which case rt_key(rt) == NULL
1068 		 * (and also rt->rt_gateway == NULL).
1069 		 * Free()/free() handle a NULL argument just fine.
1070 		 */
1071 		bcopy(dst, new, dlen);
1072 		Free(rt_key(rt));	/* free old block, if any */
1073 		rt_key(rt) = (struct sockaddr *)new;
1074 		rt->rt_gateway = (struct sockaddr *)(new + dlen);
1075 	}
1076 
1077 	/*
1078 	 * Copy the new gateway value into the memory chunk.
1079 	 */
1080 	bcopy(gate, rt->rt_gateway, glen);
1081 
1082 	/*
1083 	 * This isn't going to do anything useful for host routes, so
1084 	 * don't bother.  Also make sure we have a reasonable mask
1085 	 * (we don't yet have one during adds).
1086 	 */
1087 	if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1088 		struct rtfc_arg arg;
1089 
1090 		arg.rnh = rnh;
1091 		arg.rt0 = rt;
1092 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1093 		RADIX_NODE_HEAD_LOCK(rnh);
1094 		RT_LOCK(rt);
1095 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1096 				       rt_fixchange, &arg);
1097 		RADIX_NODE_HEAD_UNLOCK(rnh);
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 static void
1104 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1105 {
1106 	register u_char *cp1 = (u_char *)src;
1107 	register u_char *cp2 = (u_char *)dst;
1108 	register u_char *cp3 = (u_char *)netmask;
1109 	u_char *cplim = cp2 + *cp3;
1110 	u_char *cplim2 = cp2 + *cp1;
1111 
1112 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1113 	cp3 += 2;
1114 	if (cplim > cplim2)
1115 		cplim = cplim2;
1116 	while (cp2 < cplim)
1117 		*cp2++ = *cp1++ & *cp3++;
1118 	if (cp2 < cplim2)
1119 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1120 }
1121 
1122 /*
1123  * Set up a routing table entry, normally
1124  * for an interface.
1125  */
1126 int
1127 rtinit(struct ifaddr *ifa, int cmd, int flags)
1128 {
1129 	struct sockaddr *dst;
1130 	struct sockaddr *netmask;
1131 	struct mbuf *m = NULL;
1132 	struct rtentry *rt = NULL;
1133 	struct rt_addrinfo info;
1134 	int error;
1135 
1136 	if (flags & RTF_HOST) {
1137 		dst = ifa->ifa_dstaddr;
1138 		netmask = NULL;
1139 	} else {
1140 		dst = ifa->ifa_addr;
1141 		netmask = ifa->ifa_netmask;
1142 	}
1143 	if (dst->sa_len == 0)
1144 		return(EINVAL);
1145 
1146 	/*
1147 	 * If it's a delete, check that if it exists, it's on the correct
1148 	 * interface or we might scrub a route to another ifa which would
1149 	 * be confusing at best and possibly worse.
1150 	 */
1151 	if (cmd == RTM_DELETE) {
1152 		struct sockaddr *deldst;
1153 		struct radix_node_head *rnh;
1154 		struct radix_node *rn;
1155 
1156 		/*
1157 		 * It's a delete, so it should already exist..
1158 		 * If it's a net, mask off the host bits
1159 		 * (Assuming we have a mask)
1160 		 */
1161 		if (netmask != NULL) {
1162 			m = m_get(M_DONTWAIT, MT_SONAME);
1163 			if (m == NULL)
1164 				return(ENOBUFS);
1165 			deldst = mtod(m, struct sockaddr *);
1166 			rt_maskedcopy(dst, deldst, netmask);
1167 			dst = deldst;
1168 		}
1169 		/*
1170 		 * Look up an rtentry that is in the routing tree and
1171 		 * contains the correct info.
1172 		 */
1173 		if ((rnh = rt_tables[dst->sa_family]) == NULL)
1174 			goto bad;
1175 		RADIX_NODE_HEAD_LOCK(rnh);
1176 		error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
1177 		    (rn->rn_flags & RNF_ROOT) ||
1178 		    RNTORT(rn)->rt_ifa != ifa ||
1179 		    !sa_equal((struct sockaddr *)rn->rn_key, dst));
1180 		RADIX_NODE_HEAD_UNLOCK(rnh);
1181 		if (error) {
1182 bad:
1183 			if (m)
1184 				(void) m_free(m);
1185 			return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1186 		}
1187 	}
1188 	/*
1189 	 * Do the actual request
1190 	 */
1191 	bzero((caddr_t)&info, sizeof(info));
1192 	info.rti_ifa = ifa;
1193 	info.rti_flags = flags | ifa->ifa_flags;
1194 	info.rti_info[RTAX_DST] = dst;
1195 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1196 	info.rti_info[RTAX_NETMASK] = netmask;
1197 	error = rtrequest1(cmd, &info, &rt);
1198 	if (error == 0 && rt != NULL) {
1199 		/*
1200 		 * notify any listening routing agents of the change
1201 		 */
1202 		RT_LOCK(rt);
1203 		rt_newaddrmsg(cmd, ifa, error, rt);
1204 		if (cmd == RTM_DELETE) {
1205 			/*
1206 			 * If we are deleting, and we found an entry, then
1207 			 * it's been removed from the tree.. now throw it away.
1208 			 */
1209 			RTFREE_LOCKED(rt);
1210 		} else {
1211 			if (cmd == RTM_ADD) {
1212 				/*
1213 				 * We just wanted to add it.. we don't actually
1214 				 * need a reference.
1215 				 */
1216 				RT_REMREF(rt);
1217 			}
1218 			RT_UNLOCK(rt);
1219 		}
1220 	}
1221 	if (m)
1222 		(void) m_free(m);
1223 	return (error);
1224 }
1225 
1226 /*
1227  * rt_check() is invoked on each layer 2 output path, prior to
1228  * encapsulating outbound packets.
1229  *
1230  * The function is mostly used to find a routing entry for the gateway,
1231  * which in some protocol families could also point to the link-level
1232  * address for the gateway itself (the side effect of revalidating the
1233  * route to the destination is rather pointless at this stage, we did it
1234  * already a moment before in the pr_output() routine to locate the ifp
1235  * and gateway to use).
1236  *
1237  * When we remove the layer-3 to layer-2 mapping tables from the
1238  * routing table, this function can be removed.
1239  *
1240  * === On input ===
1241  *   *dst is the address of the NEXT HOP (which coincides with the
1242  *	final destination if directly reachable);
1243  *   *lrt0 points to the cached route to the final destination;
1244  *   *lrt is not meaningful;
1245  *
1246  * === Operation ===
1247  * If the route is marked down try to find a new route.  If the route
1248  * to the gateway is gone, try to setup a new route.  Otherwise,
1249  * if the route is marked for packets to be rejected, enforce that.
1250  *
1251  * === On return ===
1252  *   *dst is unchanged;
1253  *   *lrt0 points to the (possibly new) route to the final destination
1254  *   *lrt points to the route to the next hop
1255  *
1256  * Their values are meaningful ONLY if no error is returned.
1257  */
1258 int
1259 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1260 {
1261 #define senderr(x) { error = x ; goto bad; }
1262 	struct rtentry *rt;
1263 	struct rtentry *rt0;
1264 	int error;
1265 
1266 	KASSERT(*lrt0 != NULL, ("rt_check"));
1267 	rt = rt0 = *lrt0;
1268 
1269 	/* NB: the locking here is tortuous... */
1270 	RT_LOCK(rt);
1271 	if ((rt->rt_flags & RTF_UP) == 0) {
1272 		RT_UNLOCK(rt);
1273 		rt = rtalloc1(dst, 1, 0UL);
1274 		if (rt != NULL) {
1275 			RT_REMREF(rt);
1276 			/* XXX what about if change? */
1277 		} else
1278 			senderr(EHOSTUNREACH);
1279 		rt0 = rt;
1280 	}
1281 	/* XXX BSD/OS checks dst->sa_family != AF_NS */
1282 	if (rt->rt_flags & RTF_GATEWAY) {
1283 		if (rt->rt_gwroute == NULL)
1284 			goto lookup;
1285 		rt = rt->rt_gwroute;
1286 		RT_LOCK(rt);		/* NB: gwroute */
1287 		if ((rt->rt_flags & RTF_UP) == 0) {
1288 			rtfree(rt);	/* unlock gwroute */
1289 			rt = rt0;
1290 		lookup:
1291 			RT_UNLOCK(rt0);
1292 			rt = rtalloc1(rt->rt_gateway, 1, 0UL);
1293 			RT_LOCK(rt0);
1294 			rt0->rt_gwroute = rt;
1295 			if (rt == NULL) {
1296 				RT_UNLOCK(rt0);
1297 				senderr(EHOSTUNREACH);
1298 			}
1299 		}
1300 		RT_UNLOCK(rt0);
1301 	}
1302 	/* XXX why are we inspecting rmx_expire? */
1303 	error = (rt->rt_flags & RTF_REJECT) &&
1304 		(rt->rt_rmx.rmx_expire == 0 ||
1305 			time_uptime < rt->rt_rmx.rmx_expire);
1306 	if (error) {
1307 		RT_UNLOCK(rt);
1308 		senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1309 	}
1310 
1311 	*lrt = rt;
1312 	*lrt0 = rt0;
1313 	return (0);
1314 bad:
1315 	/* NB: lrt and lrt0 should not be interpreted if error is non-zero */
1316 	return (error);
1317 #undef senderr
1318 }
1319 
1320 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1321 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
1322