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