xref: /netbsd-src/sys/net/route.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: route.c,v 1.167 2016/04/28 00:16:56 ozaki-r Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Kevin M. Lahey of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the project nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 /*
63  * Copyright (c) 1980, 1986, 1991, 1993
64  *	The Regents of the University of California.  All rights reserved.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
69  * 1. Redistributions of source code must retain the above copyright
70  *    notice, this list of conditions and the following disclaimer.
71  * 2. Redistributions in binary form must reproduce the above copyright
72  *    notice, this list of conditions and the following disclaimer in the
73  *    documentation and/or other materials provided with the distribution.
74  * 3. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)route.c	8.3 (Berkeley) 1/9/95
91  */
92 
93 #ifdef _KERNEL_OPT
94 #include "opt_inet.h"
95 #include "opt_route.h"
96 #endif
97 
98 #include <sys/cdefs.h>
99 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.167 2016/04/28 00:16:56 ozaki-r Exp $");
100 
101 #include <sys/param.h>
102 #ifdef RTFLUSH_DEBUG
103 #include <sys/sysctl.h>
104 #endif
105 #include <sys/systm.h>
106 #include <sys/callout.h>
107 #include <sys/proc.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/domain.h>
112 #include <sys/protosw.h>
113 #include <sys/kernel.h>
114 #include <sys/ioctl.h>
115 #include <sys/pool.h>
116 #include <sys/kauth.h>
117 
118 #include <net/if.h>
119 #include <net/if_dl.h>
120 #include <net/route.h>
121 
122 #include <netinet/in.h>
123 #include <netinet/in_var.h>
124 
125 #ifdef RTFLUSH_DEBUG
126 #define	rtcache_debug() __predict_false(_rtcache_debug)
127 #else /* RTFLUSH_DEBUG */
128 #define	rtcache_debug() 0
129 #endif /* RTFLUSH_DEBUG */
130 
131 struct rtstat		rtstat;
132 
133 static int		rttrash;	/* routes not in table but not freed */
134 
135 static struct pool	rtentry_pool;
136 static struct pool	rttimer_pool;
137 
138 static struct callout	rt_timer_ch; /* callout for rt_timer_timer() */
139 
140 #ifdef RTFLUSH_DEBUG
141 static int _rtcache_debug = 0;
142 #endif /* RTFLUSH_DEBUG */
143 
144 static kauth_listener_t route_listener;
145 
146 static int rtdeletemsg(struct rtentry *);
147 static void rtflushall(int);
148 
149 static void rt_maskedcopy(const struct sockaddr *,
150     struct sockaddr *, const struct sockaddr *);
151 
152 static void rtcache_clear(struct route *);
153 static void rtcache_invalidate(struct dom_rtlist *);
154 
155 #ifdef DDB
156 static void db_print_sa(const struct sockaddr *);
157 static void db_print_ifa(struct ifaddr *);
158 static int db_show_rtentry(struct rtentry *, void *);
159 #endif
160 
161 #ifdef RTFLUSH_DEBUG
162 static void sysctl_net_rtcache_setup(struct sysctllog **);
163 static void
164 sysctl_net_rtcache_setup(struct sysctllog **clog)
165 {
166 	const struct sysctlnode *rnode;
167 
168 	if (sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
169 	    CTLTYPE_NODE,
170 	    "rtcache", SYSCTL_DESCR("Route cache related settings"),
171 	    NULL, 0, NULL, 0, CTL_NET, CTL_CREATE, CTL_EOL) != 0)
172 		return;
173 	if (sysctl_createv(clog, 0, &rnode, &rnode,
174 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
175 	    "debug", SYSCTL_DESCR("Debug route caches"),
176 	    NULL, 0, &_rtcache_debug, 0, CTL_CREATE, CTL_EOL) != 0)
177 		return;
178 }
179 #endif /* RTFLUSH_DEBUG */
180 
181 static inline void
182 rt_destroy(struct rtentry *rt)
183 {
184 	if (rt->_rt_key != NULL)
185 		sockaddr_free(rt->_rt_key);
186 	if (rt->rt_gateway != NULL)
187 		sockaddr_free(rt->rt_gateway);
188 	if (rt_gettag(rt) != NULL)
189 		sockaddr_free(rt_gettag(rt));
190 	rt->_rt_key = rt->rt_gateway = rt->rt_tag = NULL;
191 }
192 
193 static inline const struct sockaddr *
194 rt_setkey(struct rtentry *rt, const struct sockaddr *key, int flags)
195 {
196 	if (rt->_rt_key == key)
197 		goto out;
198 
199 	if (rt->_rt_key != NULL)
200 		sockaddr_free(rt->_rt_key);
201 	rt->_rt_key = sockaddr_dup(key, flags);
202 out:
203 	rt->rt_nodes->rn_key = (const char *)rt->_rt_key;
204 	return rt->_rt_key;
205 }
206 
207 struct ifaddr *
208 rt_get_ifa(struct rtentry *rt)
209 {
210 	struct ifaddr *ifa;
211 
212 	if ((ifa = rt->rt_ifa) == NULL)
213 		return ifa;
214 	else if (ifa->ifa_getifa == NULL)
215 		return ifa;
216 #if 0
217 	else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
218 		return ifa;
219 #endif
220 	else {
221 		ifa = (*ifa->ifa_getifa)(ifa, rt_getkey(rt));
222 		if (ifa == NULL)
223 			return NULL;
224 		rt_replace_ifa(rt, ifa);
225 		return ifa;
226 	}
227 }
228 
229 static void
230 rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
231 {
232 	rt->rt_ifa = ifa;
233 	if (ifa->ifa_seqno != NULL)
234 		rt->rt_ifa_seqno = *ifa->ifa_seqno;
235 }
236 
237 /*
238  * Is this route the connected route for the ifa?
239  */
240 static int
241 rt_ifa_connected(const struct rtentry *rt, const struct ifaddr *ifa)
242 {
243 	const struct sockaddr *key, *dst, *odst;
244 	struct sockaddr_storage maskeddst;
245 
246 	key = rt_getkey(rt);
247 	dst = rt->rt_flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
248 	if (dst == NULL ||
249 	    dst->sa_family != key->sa_family ||
250 	    dst->sa_len != key->sa_len)
251 		return 0;
252 	if ((rt->rt_flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
253 		odst = dst;
254 		dst = (struct sockaddr *)&maskeddst;
255 		rt_maskedcopy(odst, (struct sockaddr *)&maskeddst,
256 		    ifa->ifa_netmask);
257 	}
258 	return (memcmp(dst, key, dst->sa_len) == 0);
259 }
260 
261 void
262 rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
263 {
264 	if (rt->rt_ifa &&
265 	    rt->rt_ifa != ifa &&
266 	    rt->rt_ifa->ifa_flags & IFA_ROUTE &&
267 	    rt_ifa_connected(rt, rt->rt_ifa))
268 	{
269 		RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
270 		    "replace deleted IFA_ROUTE\n",
271 		    (void *)rt->_rt_key, (void *)rt->rt_ifa);
272 		rt->rt_ifa->ifa_flags &= ~IFA_ROUTE;
273 		if (rt_ifa_connected(rt, ifa)) {
274 			RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
275 			    "replace added IFA_ROUTE\n",
276 			    (void *)rt->_rt_key, (void *)ifa);
277 			ifa->ifa_flags |= IFA_ROUTE;
278 		}
279 	}
280 
281 	ifaref(ifa);
282 	ifafree(rt->rt_ifa);
283 	rt_set_ifa1(rt, ifa);
284 }
285 
286 static void
287 rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
288 {
289 	ifaref(ifa);
290 	rt_set_ifa1(rt, ifa);
291 }
292 
293 static int
294 route_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
295     void *arg0, void *arg1, void *arg2, void *arg3)
296 {
297 	struct rt_msghdr *rtm;
298 	int result;
299 
300 	result = KAUTH_RESULT_DEFER;
301 	rtm = arg1;
302 
303 	if (action != KAUTH_NETWORK_ROUTE)
304 		return result;
305 
306 	if (rtm->rtm_type == RTM_GET)
307 		result = KAUTH_RESULT_ALLOW;
308 
309 	return result;
310 }
311 
312 void
313 rt_init(void)
314 {
315 
316 #ifdef RTFLUSH_DEBUG
317 	sysctl_net_rtcache_setup(NULL);
318 #endif
319 
320 	pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
321 	    NULL, IPL_SOFTNET);
322 	pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
323 	    NULL, IPL_SOFTNET);
324 
325 	rn_init();	/* initialize all zeroes, all ones, mask table */
326 	rtbl_init();
327 
328 	route_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
329 	    route_listener_cb, NULL);
330 }
331 
332 static void
333 rtflushall(int family)
334 {
335 	struct domain *dom;
336 
337 	if (rtcache_debug())
338 		printf("%s: enter\n", __func__);
339 
340 	if ((dom = pffinddomain(family)) == NULL)
341 		return;
342 
343 	rtcache_invalidate(&dom->dom_rtcache);
344 }
345 
346 static void
347 rtcache(struct route *ro)
348 {
349 	struct domain *dom;
350 
351 	rtcache_invariants(ro);
352 	KASSERT(ro->_ro_rt != NULL);
353 	KASSERT(ro->ro_invalid == false);
354 	KASSERT(rtcache_getdst(ro) != NULL);
355 
356 	if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) == NULL)
357 		return;
358 
359 	LIST_INSERT_HEAD(&dom->dom_rtcache, ro, ro_rtcache_next);
360 	rtcache_invariants(ro);
361 }
362 
363 #ifdef RT_DEBUG
364 static void
365 dump_rt(const struct rtentry *rt)
366 {
367 	char buf[512];
368 
369 	aprint_normal("rt: ");
370 	aprint_normal("p=%p ", rt);
371 	if (rt->_rt_key == NULL) {
372 		aprint_normal("dst=(NULL) ");
373 	} else {
374 		sockaddr_format(rt->_rt_key, buf, sizeof(buf));
375 		aprint_normal("dst=%s ", buf);
376 	}
377 	if (rt->rt_gateway == NULL) {
378 		aprint_normal("gw=(NULL) ");
379 	} else {
380 		sockaddr_format(rt->_rt_key, buf, sizeof(buf));
381 		aprint_normal("gw=%s ", buf);
382 	}
383 	aprint_normal("flags=%x ", rt->rt_flags);
384 	if (rt->rt_ifp == NULL) {
385 		aprint_normal("if=(NULL) ");
386 	} else {
387 		aprint_normal("if=%s ", rt->rt_ifp->if_xname);
388 	}
389 	aprint_normal("\n");
390 }
391 #endif /* RT_DEBUG */
392 
393 /*
394  * Packet routing routines. If success, refcnt of a returned rtentry
395  * will be incremented. The caller has to rtfree it by itself.
396  */
397 struct rtentry *
398 rtalloc1(const struct sockaddr *dst, int report)
399 {
400 	rtbl_t *rtbl;
401 	struct rtentry *rt;
402 	int s;
403 
404 	s = splsoftnet();
405 	rtbl = rt_gettable(dst->sa_family);
406 	if (rtbl == NULL)
407 		goto miss;
408 
409 	rt = rt_matchaddr(rtbl, dst);
410 	if (rt == NULL)
411 		goto miss;
412 
413 	rt->rt_refcnt++;
414 
415 	splx(s);
416 	return rt;
417 miss:
418 	rtstat.rts_unreach++;
419 	if (report) {
420 		struct rt_addrinfo info;
421 
422 		memset(&info, 0, sizeof(info));
423 		info.rti_info[RTAX_DST] = dst;
424 		rt_missmsg(RTM_MISS, &info, 0, 0);
425 	}
426 	splx(s);
427 	return NULL;
428 }
429 
430 #ifdef DEBUG
431 /*
432  * Check the following constraint for each rtcache:
433  *   if a rtcache holds a rtentry, the rtentry's refcnt is more than zero,
434  *   i.e., the rtentry should be referenced at least by the rtcache.
435  */
436 static void
437 rtcache_check_rtrefcnt(int family)
438 {
439 	struct domain *dom = pffinddomain(family);
440 	struct route *ro;
441 
442 	if (dom == NULL)
443 		return;
444 
445 	LIST_FOREACH(ro, &dom->dom_rtcache, ro_rtcache_next)
446 		KDASSERT(ro->_ro_rt == NULL || ro->_ro_rt->rt_refcnt > 0);
447 }
448 #endif
449 
450 void
451 rtfree(struct rtentry *rt)
452 {
453 	struct ifaddr *ifa;
454 
455 	KASSERT(rt != NULL);
456 	KASSERT(rt->rt_refcnt > 0);
457 
458 	rt->rt_refcnt--;
459 #ifdef DEBUG
460 	if (rt_getkey(rt) != NULL)
461 		rtcache_check_rtrefcnt(rt_getkey(rt)->sa_family);
462 #endif
463 	if (rt->rt_refcnt == 0 && (rt->rt_flags & RTF_UP) == 0) {
464 		rt_assert_inactive(rt);
465 		rttrash--;
466 		rt_timer_remove_all(rt, 0);
467 		ifa = rt->rt_ifa;
468 		rt->rt_ifa = NULL;
469 		ifafree(ifa);
470 		rt->rt_ifp = NULL;
471 		rt_destroy(rt);
472 		pool_put(&rtentry_pool, rt);
473 	}
474 }
475 
476 /*
477  * Force a routing table entry to the specified
478  * destination to go through the given gateway.
479  * Normally called as a result of a routing redirect
480  * message from the network layer.
481  *
482  * N.B.: must be called at splsoftnet
483  */
484 void
485 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
486 	const struct sockaddr *netmask, int flags, const struct sockaddr *src,
487 	struct rtentry **rtp)
488 {
489 	struct rtentry *rt;
490 	int error = 0;
491 	uint64_t *stat = NULL;
492 	struct rt_addrinfo info;
493 	struct ifaddr *ifa;
494 
495 	/* verify the gateway is directly reachable */
496 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
497 		error = ENETUNREACH;
498 		goto out;
499 	}
500 	rt = rtalloc1(dst, 0);
501 	/*
502 	 * If the redirect isn't from our current router for this dst,
503 	 * it's either old or wrong.  If it redirects us to ourselves,
504 	 * we have a routing loop, perhaps as a result of an interface
505 	 * going down recently.
506 	 */
507 	if (!(flags & RTF_DONE) && rt &&
508 	     (sockaddr_cmp(src, rt->rt_gateway) != 0 || rt->rt_ifa != ifa))
509 		error = EINVAL;
510 	else if (ifa_ifwithaddr(gateway))
511 		error = EHOSTUNREACH;
512 	if (error)
513 		goto done;
514 	/*
515 	 * Create a new entry if we just got back a wildcard entry
516 	 * or the lookup failed.  This is necessary for hosts
517 	 * which use routing redirects generated by smart gateways
518 	 * to dynamically build the routing tables.
519 	 */
520 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
521 		goto create;
522 	/*
523 	 * Don't listen to the redirect if it's
524 	 * for a route to an interface.
525 	 */
526 	if (rt->rt_flags & RTF_GATEWAY) {
527 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
528 			/*
529 			 * Changing from route to net => route to host.
530 			 * Create new route, rather than smashing route to net.
531 			 */
532 		create:
533 			if (rt != NULL)
534 				rtfree(rt);
535 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
536 			memset(&info, 0, sizeof(info));
537 			info.rti_info[RTAX_DST] = dst;
538 			info.rti_info[RTAX_GATEWAY] = gateway;
539 			info.rti_info[RTAX_NETMASK] = netmask;
540 			info.rti_ifa = ifa;
541 			info.rti_flags = flags;
542 			rt = NULL;
543 			error = rtrequest1(RTM_ADD, &info, &rt);
544 			if (rt != NULL)
545 				flags = rt->rt_flags;
546 			stat = &rtstat.rts_dynamic;
547 		} else {
548 			/*
549 			 * Smash the current notion of the gateway to
550 			 * this destination.  Should check about netmask!!!
551 			 */
552 			error = rt_setgate(rt, gateway);
553 			if (error == 0) {
554 				rt->rt_flags |= RTF_MODIFIED;
555 				flags |= RTF_MODIFIED;
556 			}
557 			stat = &rtstat.rts_newgateway;
558 		}
559 	} else
560 		error = EHOSTUNREACH;
561 done:
562 	if (rt) {
563 		if (rtp != NULL && !error)
564 			*rtp = rt;
565 		else
566 			rtfree(rt);
567 	}
568 out:
569 	if (error)
570 		rtstat.rts_badredirect++;
571 	else if (stat != NULL)
572 		(*stat)++;
573 	memset(&info, 0, sizeof(info));
574 	info.rti_info[RTAX_DST] = dst;
575 	info.rti_info[RTAX_GATEWAY] = gateway;
576 	info.rti_info[RTAX_NETMASK] = netmask;
577 	info.rti_info[RTAX_AUTHOR] = src;
578 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
579 }
580 
581 /*
582  * Delete a route and generate a message.
583  * It doesn't free a passed rt.
584  */
585 static int
586 rtdeletemsg(struct rtentry *rt)
587 {
588 	int error;
589 	struct rt_addrinfo info;
590 
591 	/*
592 	 * Request the new route so that the entry is not actually
593 	 * deleted.  That will allow the information being reported to
594 	 * be accurate (and consistent with route_output()).
595 	 */
596 	memset(&info, 0, sizeof(info));
597 	info.rti_info[RTAX_DST] = rt_getkey(rt);
598 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
599 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
600 	info.rti_flags = rt->rt_flags;
601 	error = rtrequest1(RTM_DELETE, &info, NULL);
602 
603 	rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
604 
605 	return error;
606 }
607 
608 struct ifaddr *
609 ifa_ifwithroute(int flags, const struct sockaddr *dst,
610 	const struct sockaddr *gateway)
611 {
612 	struct ifaddr *ifa;
613 	if ((flags & RTF_GATEWAY) == 0) {
614 		/*
615 		 * If we are adding a route to an interface,
616 		 * and the interface is a pt to pt link
617 		 * we should search for the destination
618 		 * as our clue to the interface.  Otherwise
619 		 * we can use the local address.
620 		 */
621 		ifa = NULL;
622 		if ((flags & RTF_HOST) && gateway->sa_family != AF_LINK)
623 			ifa = ifa_ifwithdstaddr(dst);
624 		if (ifa == NULL)
625 			ifa = ifa_ifwithaddr(gateway);
626 	} else {
627 		/*
628 		 * If we are adding a route to a remote net
629 		 * or host, the gateway may still be on the
630 		 * other end of a pt to pt link.
631 		 */
632 		ifa = ifa_ifwithdstaddr(gateway);
633 	}
634 	if (ifa == NULL)
635 		ifa = ifa_ifwithnet(gateway);
636 	if (ifa == NULL) {
637 		struct rtentry *rt = rtalloc1(dst, 0);
638 		if (rt == NULL)
639 			return NULL;
640 		ifa = rt->rt_ifa;
641 		rtfree(rt);
642 		if (ifa == NULL)
643 			return NULL;
644 	}
645 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
646 		struct ifaddr *oifa = ifa;
647 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
648 		if (ifa == NULL)
649 			ifa = oifa;
650 	}
651 	return ifa;
652 }
653 
654 /*
655  * If it suceeds and ret_nrt isn't NULL, refcnt of ret_nrt is incremented.
656  * The caller has to rtfree it by itself.
657  */
658 int
659 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
660 	const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
661 {
662 	struct rt_addrinfo info;
663 
664 	memset(&info, 0, sizeof(info));
665 	info.rti_flags = flags;
666 	info.rti_info[RTAX_DST] = dst;
667 	info.rti_info[RTAX_GATEWAY] = gateway;
668 	info.rti_info[RTAX_NETMASK] = netmask;
669 	return rtrequest1(req, &info, ret_nrt);
670 }
671 
672 /*
673  * It's a utility function to add/remove a route to/from the routing table
674  * and tell user processes the addition/removal on success.
675  */
676 int
677 rtrequest_newmsg(const int req, const struct sockaddr *dst,
678 	const struct sockaddr *gateway, const struct sockaddr *netmask,
679 	const int flags)
680 {
681 	int error;
682 	struct rtentry *ret_nrt = NULL;
683 
684 	KASSERT(req == RTM_ADD || req == RTM_DELETE);
685 
686 	error = rtrequest(req, dst, gateway, netmask, flags, &ret_nrt);
687 	if (error != 0)
688 		return error;
689 
690 	KASSERT(ret_nrt != NULL);
691 
692 	rt_newmsg(req, ret_nrt); /* tell user process */
693 	rtfree(ret_nrt);
694 
695 	return 0;
696 }
697 
698 int
699 rt_getifa(struct rt_addrinfo *info)
700 {
701 	struct ifaddr *ifa;
702 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
703 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
704 	const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
705 	const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
706 	int flags = info->rti_flags;
707 
708 	/*
709 	 * ifp may be specified by sockaddr_dl when protocol address
710 	 * is ambiguous
711 	 */
712 	if (info->rti_ifp == NULL && ifpaddr != NULL
713 	    && ifpaddr->sa_family == AF_LINK &&
714 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
715 		info->rti_ifp = ifa->ifa_ifp;
716 	if (info->rti_ifa == NULL && ifaaddr != NULL)
717 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
718 	if (info->rti_ifa == NULL) {
719 		const struct sockaddr *sa;
720 
721 		sa = ifaaddr != NULL ? ifaaddr :
722 		    (gateway != NULL ? gateway : dst);
723 		if (sa != NULL && info->rti_ifp != NULL)
724 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
725 		else if (dst != NULL && gateway != NULL)
726 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
727 		else if (sa != NULL)
728 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
729 	}
730 	if ((ifa = info->rti_ifa) == NULL)
731 		return ENETUNREACH;
732 	if (ifa->ifa_getifa != NULL) {
733 		info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
734 		if (ifa == NULL)
735 			return ENETUNREACH;
736 	}
737 	if (info->rti_ifp == NULL)
738 		info->rti_ifp = ifa->ifa_ifp;
739 	return 0;
740 }
741 
742 /*
743  * If it suceeds and ret_nrt isn't NULL, refcnt of ret_nrt is incremented.
744  * The caller has to rtfree it by itself.
745  */
746 int
747 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
748 {
749 	int s = splsoftnet();
750 	int error = 0, rc;
751 	struct rtentry *rt;
752 	rtbl_t *rtbl;
753 	struct ifaddr *ifa, *ifa2;
754 	struct sockaddr_storage maskeddst;
755 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
756 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
757 	const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
758 	int flags = info->rti_flags;
759 #define senderr(x) { error = x ; goto bad; }
760 
761 	if ((rtbl = rt_gettable(dst->sa_family)) == NULL)
762 		senderr(ESRCH);
763 	if (flags & RTF_HOST)
764 		netmask = NULL;
765 	switch (req) {
766 	case RTM_DELETE:
767 		if (netmask) {
768 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
769 			    netmask);
770 			dst = (struct sockaddr *)&maskeddst;
771 		}
772 		if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
773 			senderr(ESRCH);
774 		if ((rt = rt_deladdr(rtbl, dst, netmask)) == NULL)
775 			senderr(ESRCH);
776 		rt->rt_flags &= ~RTF_UP;
777 		if ((ifa = rt->rt_ifa)) {
778 			if (ifa->ifa_flags & IFA_ROUTE &&
779 			    rt_ifa_connected(rt, ifa)) {
780 				RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
781 				    "deleted IFA_ROUTE\n",
782 				    (void *)rt->_rt_key, (void *)ifa);
783 				ifa->ifa_flags &= ~IFA_ROUTE;
784 			}
785 			if (ifa->ifa_rtrequest)
786 				ifa->ifa_rtrequest(RTM_DELETE, rt, info);
787 		}
788 		rttrash++;
789 		if (ret_nrt) {
790 			*ret_nrt = rt;
791 			rt->rt_refcnt++;
792 		} else if (rt->rt_refcnt <= 0) {
793 			/* Adjust the refcount */
794 			rt->rt_refcnt++;
795 			rtfree(rt);
796 		}
797 		break;
798 
799 	case RTM_ADD:
800 		if (info->rti_ifa == NULL && (error = rt_getifa(info)))
801 			senderr(error);
802 		ifa = info->rti_ifa;
803 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
804 		if (rt == NULL)
805 			senderr(ENOBUFS);
806 		memset(rt, 0, sizeof(*rt));
807 		rt->rt_flags = RTF_UP | flags;
808 		LIST_INIT(&rt->rt_timer);
809 
810 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
811 		if (netmask) {
812 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
813 			    netmask);
814 			rt_setkey(rt, (struct sockaddr *)&maskeddst, M_NOWAIT);
815 		} else {
816 			rt_setkey(rt, dst, M_NOWAIT);
817 		}
818 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
819 		if (rt_getkey(rt) == NULL ||
820 		    rt_setgate(rt, gateway) != 0) {
821 			pool_put(&rtentry_pool, rt);
822 			senderr(ENOBUFS);
823 		}
824 
825 		rt_set_ifa(rt, ifa);
826 		if (info->rti_info[RTAX_TAG] != NULL) {
827 			const struct sockaddr *tag;
828 			tag = rt_settag(rt, info->rti_info[RTAX_TAG]);
829 			if (tag == NULL)
830 				senderr(ENOBUFS);
831 		}
832 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
833 		if (info->rti_info[RTAX_IFP] != NULL &&
834 		    (ifa2 = ifa_ifwithnet(info->rti_info[RTAX_IFP])) != NULL &&
835 		    ifa2->ifa_ifp != NULL)
836 			rt->rt_ifp = ifa2->ifa_ifp;
837 		else
838 			rt->rt_ifp = ifa->ifa_ifp;
839 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
840 		rc = rt_addaddr(rtbl, rt, netmask);
841 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
842 		if (rc != 0) {
843 			ifafree(ifa);
844 			rt_destroy(rt);
845 			pool_put(&rtentry_pool, rt);
846 			senderr(rc);
847 		}
848 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
849 		if (ifa->ifa_rtrequest)
850 			ifa->ifa_rtrequest(req, rt, info);
851 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
852 		if (ret_nrt) {
853 			*ret_nrt = rt;
854 			rt->rt_refcnt++;
855 		}
856 		rtflushall(dst->sa_family);
857 		break;
858 	case RTM_GET:
859 		if (netmask != NULL) {
860 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
861 			    netmask);
862 			dst = (struct sockaddr *)&maskeddst;
863 		}
864 		if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
865 			senderr(ESRCH);
866 		if (ret_nrt != NULL) {
867 			*ret_nrt = rt;
868 			rt->rt_refcnt++;
869 		}
870 		break;
871 	}
872 bad:
873 	splx(s);
874 	return error;
875 }
876 
877 int
878 rt_setgate(struct rtentry *rt, const struct sockaddr *gate)
879 {
880 
881 	KASSERT(rt->_rt_key != NULL);
882 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
883 
884 	if (rt->rt_gateway != NULL)
885 		sockaddr_free(rt->rt_gateway);
886 	KASSERT(rt->_rt_key != NULL);
887 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
888 	if ((rt->rt_gateway = sockaddr_dup(gate, M_ZERO | M_NOWAIT)) == NULL)
889 		return ENOMEM;
890 	KASSERT(rt->_rt_key != NULL);
891 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
892 
893 	if (rt->rt_flags & RTF_GATEWAY) {
894 		struct rtentry *gwrt = rtalloc1(gate, 1);
895 		/*
896 		 * If we switched gateways, grab the MTU from the new
897 		 * gateway route if the current MTU, if the current MTU is
898 		 * greater than the MTU of gateway.
899 		 * Note that, if the MTU of gateway is 0, we will reset the
900 		 * MTU of the route to run PMTUD again from scratch. XXX
901 		 */
902 		if (gwrt != NULL) {
903 			KASSERT(gwrt->_rt_key != NULL);
904 			RT_DPRINTF("gwrt->_rt_key = %p\n", gwrt->_rt_key);
905 			if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
906 			    rt->rt_rmx.rmx_mtu &&
907 			    rt->rt_rmx.rmx_mtu > gwrt->rt_rmx.rmx_mtu) {
908 				rt->rt_rmx.rmx_mtu = gwrt->rt_rmx.rmx_mtu;
909 			}
910 			rtfree(gwrt);
911 		}
912 	}
913 	KASSERT(rt->_rt_key != NULL);
914 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
915 	return 0;
916 }
917 
918 static void
919 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
920 	const struct sockaddr *netmask)
921 {
922 	const char *netmaskp = &netmask->sa_data[0],
923 	           *srcp = &src->sa_data[0];
924 	char *dstp = &dst->sa_data[0];
925 	const char *maskend = (char *)dst + MIN(netmask->sa_len, src->sa_len);
926 	const char *srcend = (char *)dst + src->sa_len;
927 
928 	dst->sa_len = src->sa_len;
929 	dst->sa_family = src->sa_family;
930 
931 	while (dstp < maskend)
932 		*dstp++ = *srcp++ & *netmaskp++;
933 	if (dstp < srcend)
934 		memset(dstp, 0, (size_t)(srcend - dstp));
935 }
936 
937 /*
938  * Inform the routing socket of a route change.
939  */
940 void
941 rt_newmsg(const int cmd, const struct rtentry *rt)
942 {
943 	struct rt_addrinfo info;
944 
945 	memset((void *)&info, 0, sizeof(info));
946 	info.rti_info[RTAX_DST] = rt_getkey(rt);
947 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
948 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
949 	if (rt->rt_ifp) {
950 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
951 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
952 	}
953 
954 	rt_missmsg(cmd, &info, rt->rt_flags, 0);
955 }
956 
957 /*
958  * Set up or tear down a routing table entry, normally
959  * for an interface.
960  */
961 int
962 rtinit(struct ifaddr *ifa, int cmd, int flags)
963 {
964 	struct rtentry *rt;
965 	struct sockaddr *dst, *odst;
966 	struct sockaddr_storage maskeddst;
967 	struct rtentry *nrt = NULL;
968 	int error;
969 	struct rt_addrinfo info;
970 
971 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
972 	if (cmd == RTM_DELETE) {
973 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
974 			/* Delete subnet route for this interface */
975 			odst = dst;
976 			dst = (struct sockaddr *)&maskeddst;
977 			rt_maskedcopy(odst, dst, ifa->ifa_netmask);
978 		}
979 		if ((rt = rtalloc1(dst, 0)) != NULL) {
980 			if (rt->rt_ifa != ifa) {
981 				rtfree(rt);
982 				return (flags & RTF_HOST) ? EHOSTUNREACH
983 							: ENETUNREACH;
984 			}
985 			rtfree(rt);
986 		}
987 	}
988 	memset(&info, 0, sizeof(info));
989 	info.rti_ifa = ifa;
990 	info.rti_flags = flags | ifa->ifa_flags;
991 	info.rti_info[RTAX_DST] = dst;
992 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
993 
994 	/*
995 	 * XXX here, it seems that we are assuming that ifa_netmask is NULL
996 	 * for RTF_HOST.  bsdi4 passes NULL explicitly (via intermediate
997 	 * variable) when RTF_HOST is 1.  still not sure if i can safely
998 	 * change it to meet bsdi4 behavior.
999 	 */
1000 	if (cmd != RTM_LLINFO_UPD)
1001 		info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1002 	error = rtrequest1((cmd == RTM_LLINFO_UPD) ? RTM_GET : cmd, &info,
1003 	    &nrt);
1004 	if (error != 0)
1005 		return error;
1006 
1007 	rt = nrt;
1008 	switch (cmd) {
1009 	case RTM_DELETE:
1010 		rt_newmsg(cmd, rt);
1011 		break;
1012 	case RTM_LLINFO_UPD:
1013 		if (cmd == RTM_LLINFO_UPD && ifa->ifa_rtrequest != NULL)
1014 			ifa->ifa_rtrequest(RTM_LLINFO_UPD, rt, &info);
1015 		rt_newmsg(RTM_CHANGE, rt);
1016 		break;
1017 	case RTM_ADD:
1018 		if (rt->rt_ifa != ifa) {
1019 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
1020 				rt->rt_ifa);
1021 			if (rt->rt_ifa->ifa_rtrequest != NULL) {
1022 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
1023 				    &info);
1024 			}
1025 			rt_replace_ifa(rt, ifa);
1026 			rt->rt_ifp = ifa->ifa_ifp;
1027 			if (ifa->ifa_rtrequest != NULL)
1028 				ifa->ifa_rtrequest(RTM_ADD, rt, &info);
1029 		}
1030 		rt_newmsg(cmd, rt);
1031 		break;
1032 	}
1033 	rtfree(rt);
1034 	return error;
1035 }
1036 
1037 /*
1038  * Create a local route entry for the address.
1039  * Announce the addition of the address and the route to the routing socket.
1040  */
1041 int
1042 rt_ifa_addlocal(struct ifaddr *ifa)
1043 {
1044 	struct rtentry *rt;
1045 	int e;
1046 
1047 	/* If there is no loopback entry, allocate one. */
1048 	rt = rtalloc1(ifa->ifa_addr, 0);
1049 #ifdef RT_DEBUG
1050 	if (rt != NULL)
1051 		dump_rt(rt);
1052 #endif
1053 	if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
1054 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1055 	{
1056 		struct rt_addrinfo info;
1057 		struct rtentry *nrt;
1058 
1059 		memset(&info, 0, sizeof(info));
1060 		info.rti_flags = RTF_HOST | RTF_LOCAL;
1061 		if (!(ifa->ifa_ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)))
1062 			info.rti_flags |= RTF_LLDATA;
1063 		info.rti_info[RTAX_DST] = ifa->ifa_addr;
1064 		info.rti_info[RTAX_GATEWAY] =
1065 		    (const struct sockaddr *)ifa->ifa_ifp->if_sadl;
1066 		info.rti_ifa = ifa;
1067 		nrt = NULL;
1068 		e = rtrequest1(RTM_ADD, &info, &nrt);
1069 		if (nrt && ifa != nrt->rt_ifa)
1070 			rt_replace_ifa(nrt, ifa);
1071 		rt_newaddrmsg(RTM_ADD, ifa, e, nrt);
1072 		if (nrt != NULL) {
1073 #ifdef RT_DEBUG
1074 			dump_rt(nrt);
1075 #endif
1076 			rtfree(nrt);
1077 		}
1078 	} else {
1079 		e = 0;
1080 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1081 	}
1082 	if (rt != NULL)
1083 		rtfree(rt);
1084 	return e;
1085 }
1086 
1087 /*
1088  * Remove the local route entry for the address.
1089  * Announce the removal of the address and the route to the routing socket.
1090  */
1091 int
1092 rt_ifa_remlocal(struct ifaddr *ifa, struct ifaddr *alt_ifa)
1093 {
1094 	struct rtentry *rt;
1095 	int e = 0;
1096 
1097 	rt = rtalloc1(ifa->ifa_addr, 0);
1098 
1099 	/*
1100 	 * Before deleting, check if a corresponding loopbacked
1101 	 * host route surely exists.  With this check, we can avoid
1102 	 * deleting an interface direct route whose destination is
1103 	 * the same as the address being removed.  This can happen
1104 	 * when removing a subnet-router anycast address on an
1105 	 * interface attached to a shared medium.
1106 	 */
1107 	if (rt != NULL &&
1108 	    (rt->rt_flags & RTF_HOST) &&
1109 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK))
1110 	{
1111 		/* If we cannot replace the route's ifaddr with the equivalent
1112 		 * ifaddr of another interface, I believe it is safest to
1113 		 * delete the route.
1114 		 */
1115 		if (alt_ifa == NULL) {
1116 			e = rtdeletemsg(rt);
1117 			rt_newaddrmsg(RTM_DELADDR, ifa, 0, NULL);
1118 		} else {
1119 			rt_replace_ifa(rt, alt_ifa);
1120 			rt_newmsg(RTM_CHANGE, rt);
1121 		}
1122 	} else
1123 		rt_newaddrmsg(RTM_DELADDR, ifa, 0, NULL);
1124 	if (rt != NULL)
1125 		rtfree(rt);
1126 	return e;
1127 }
1128 
1129 /*
1130  * Route timer routines.  These routes allow functions to be called
1131  * for various routes at any time.  This is useful in supporting
1132  * path MTU discovery and redirect route deletion.
1133  *
1134  * This is similar to some BSDI internal functions, but it provides
1135  * for multiple queues for efficiency's sake...
1136  */
1137 
1138 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
1139 static int rt_init_done = 0;
1140 
1141 /*
1142  * Some subtle order problems with domain initialization mean that
1143  * we cannot count on this being run from rt_init before various
1144  * protocol initializations are done.  Therefore, we make sure
1145  * that this is run when the first queue is added...
1146  */
1147 
1148 void
1149 rt_timer_init(void)
1150 {
1151 	assert(rt_init_done == 0);
1152 
1153 	LIST_INIT(&rttimer_queue_head);
1154 	callout_init(&rt_timer_ch, 0);
1155 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1156 	rt_init_done = 1;
1157 }
1158 
1159 struct rttimer_queue *
1160 rt_timer_queue_create(u_int timeout)
1161 {
1162 	struct rttimer_queue *rtq;
1163 
1164 	if (rt_init_done == 0)
1165 		rt_timer_init();
1166 
1167 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
1168 	if (rtq == NULL)
1169 		return NULL;
1170 	memset(rtq, 0, sizeof(*rtq));
1171 
1172 	rtq->rtq_timeout = timeout;
1173 	TAILQ_INIT(&rtq->rtq_head);
1174 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
1175 
1176 	return rtq;
1177 }
1178 
1179 void
1180 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
1181 {
1182 
1183 	rtq->rtq_timeout = timeout;
1184 }
1185 
1186 void
1187 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
1188 {
1189 	struct rttimer *r;
1190 
1191 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
1192 		LIST_REMOVE(r, rtt_link);
1193 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1194 		if (destroy)
1195 			(*r->rtt_func)(r->rtt_rt, r);
1196 		rtfree(r->rtt_rt);
1197 		pool_put(&rttimer_pool, r);
1198 		if (rtq->rtq_count > 0)
1199 			rtq->rtq_count--;
1200 		else
1201 			printf("rt_timer_queue_remove_all: "
1202 			    "rtq_count reached 0\n");
1203 	}
1204 }
1205 
1206 void
1207 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
1208 {
1209 
1210 	rt_timer_queue_remove_all(rtq, destroy);
1211 
1212 	LIST_REMOVE(rtq, rtq_link);
1213 
1214 	/*
1215 	 * Caller is responsible for freeing the rttimer_queue structure.
1216 	 */
1217 }
1218 
1219 unsigned long
1220 rt_timer_count(struct rttimer_queue *rtq)
1221 {
1222 	return rtq->rtq_count;
1223 }
1224 
1225 void
1226 rt_timer_remove_all(struct rtentry *rt, int destroy)
1227 {
1228 	struct rttimer *r;
1229 
1230 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1231 		LIST_REMOVE(r, rtt_link);
1232 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1233 		if (destroy)
1234 			(*r->rtt_func)(r->rtt_rt, r);
1235 		if (r->rtt_queue->rtq_count > 0)
1236 			r->rtt_queue->rtq_count--;
1237 		else
1238 			printf("rt_timer_remove_all: rtq_count reached 0\n");
1239 		rtfree(r->rtt_rt);
1240 		pool_put(&rttimer_pool, r);
1241 	}
1242 }
1243 
1244 int
1245 rt_timer_add(struct rtentry *rt,
1246 	void (*func)(struct rtentry *, struct rttimer *),
1247 	struct rttimer_queue *queue)
1248 {
1249 	struct rttimer *r;
1250 
1251 	KASSERT(func != NULL);
1252 	/*
1253 	 * If there's already a timer with this action, destroy it before
1254 	 * we add a new one.
1255 	 */
1256 	LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
1257 		if (r->rtt_func == func)
1258 			break;
1259 	}
1260 	if (r != NULL) {
1261 		LIST_REMOVE(r, rtt_link);
1262 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1263 		if (r->rtt_queue->rtq_count > 0)
1264 			r->rtt_queue->rtq_count--;
1265 		else
1266 			printf("rt_timer_add: rtq_count reached 0\n");
1267 		rtfree(r->rtt_rt);
1268 	} else {
1269 		r = pool_get(&rttimer_pool, PR_NOWAIT);
1270 		if (r == NULL)
1271 			return ENOBUFS;
1272 	}
1273 
1274 	memset(r, 0, sizeof(*r));
1275 
1276 	rt->rt_refcnt++;
1277 	r->rtt_rt = rt;
1278 	r->rtt_time = time_uptime;
1279 	r->rtt_func = func;
1280 	r->rtt_queue = queue;
1281 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1282 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1283 	r->rtt_queue->rtq_count++;
1284 
1285 	return 0;
1286 }
1287 
1288 /* ARGSUSED */
1289 void
1290 rt_timer_timer(void *arg)
1291 {
1292 	struct rttimer_queue *rtq;
1293 	struct rttimer *r;
1294 	int s;
1295 
1296 	s = splsoftnet();
1297 	LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
1298 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1299 		    (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
1300 			LIST_REMOVE(r, rtt_link);
1301 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1302 			(*r->rtt_func)(r->rtt_rt, r);
1303 			rtfree(r->rtt_rt);
1304 			pool_put(&rttimer_pool, r);
1305 			if (rtq->rtq_count > 0)
1306 				rtq->rtq_count--;
1307 			else
1308 				printf("rt_timer_timer: rtq_count reached 0\n");
1309 		}
1310 	}
1311 	splx(s);
1312 
1313 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1314 }
1315 
1316 static struct rtentry *
1317 _rtcache_init(struct route *ro, int flag)
1318 {
1319 	rtcache_invariants(ro);
1320 	KASSERT(ro->_ro_rt == NULL);
1321 
1322 	if (rtcache_getdst(ro) == NULL)
1323 		return NULL;
1324 	ro->ro_invalid = false;
1325 	if ((ro->_ro_rt = rtalloc1(rtcache_getdst(ro), flag)) != NULL)
1326 		rtcache(ro);
1327 
1328 	rtcache_invariants(ro);
1329 	return ro->_ro_rt;
1330 }
1331 
1332 struct rtentry *
1333 rtcache_init(struct route *ro)
1334 {
1335 	return _rtcache_init(ro, 1);
1336 }
1337 
1338 struct rtentry *
1339 rtcache_init_noclone(struct route *ro)
1340 {
1341 	return _rtcache_init(ro, 0);
1342 }
1343 
1344 struct rtentry *
1345 rtcache_update(struct route *ro, int clone)
1346 {
1347 	rtcache_clear(ro);
1348 	return _rtcache_init(ro, clone);
1349 }
1350 
1351 void
1352 rtcache_copy(struct route *new_ro, const struct route *old_ro)
1353 {
1354 	struct rtentry *rt;
1355 
1356 	KASSERT(new_ro != old_ro);
1357 	rtcache_invariants(new_ro);
1358 	rtcache_invariants(old_ro);
1359 
1360 	if ((rt = rtcache_validate(old_ro)) != NULL)
1361 		rt->rt_refcnt++;
1362 
1363 	if (rtcache_getdst(old_ro) == NULL ||
1364 	    rtcache_setdst(new_ro, rtcache_getdst(old_ro)) != 0)
1365 		return;
1366 
1367 	new_ro->ro_invalid = false;
1368 	if ((new_ro->_ro_rt = rt) != NULL)
1369 		rtcache(new_ro);
1370 	rtcache_invariants(new_ro);
1371 }
1372 
1373 static struct dom_rtlist invalid_routes = LIST_HEAD_INITIALIZER(dom_rtlist);
1374 
1375 static void
1376 rtcache_invalidate(struct dom_rtlist *rtlist)
1377 {
1378 	struct route *ro;
1379 
1380 	while ((ro = LIST_FIRST(rtlist)) != NULL) {
1381 		rtcache_invariants(ro);
1382 		KASSERT(ro->_ro_rt != NULL);
1383 		ro->ro_invalid = true;
1384 		LIST_REMOVE(ro, ro_rtcache_next);
1385 		LIST_INSERT_HEAD(&invalid_routes, ro, ro_rtcache_next);
1386 		rtcache_invariants(ro);
1387 	}
1388 }
1389 
1390 static void
1391 rtcache_clear(struct route *ro)
1392 {
1393 	rtcache_invariants(ro);
1394 	if (ro->_ro_rt == NULL)
1395 		return;
1396 
1397 	LIST_REMOVE(ro, ro_rtcache_next);
1398 
1399 	rtfree(ro->_ro_rt);
1400 	ro->_ro_rt = NULL;
1401 	ro->ro_invalid = false;
1402 	rtcache_invariants(ro);
1403 }
1404 
1405 struct rtentry *
1406 rtcache_lookup2(struct route *ro, const struct sockaddr *dst, int clone,
1407     int *hitp)
1408 {
1409 	const struct sockaddr *odst;
1410 	struct rtentry *rt = NULL;
1411 
1412 	odst = rtcache_getdst(ro);
1413 	if (odst == NULL)
1414 		goto miss;
1415 
1416 	if (sockaddr_cmp(odst, dst) != 0) {
1417 		rtcache_free(ro);
1418 		goto miss;
1419 	}
1420 
1421 	rt = rtcache_validate(ro);
1422 	if (rt == NULL) {
1423 		rtcache_clear(ro);
1424 		goto miss;
1425 	}
1426 
1427 	*hitp = 1;
1428 	rtcache_invariants(ro);
1429 
1430 	return rt;
1431 miss:
1432 	*hitp = 0;
1433 	if (rtcache_setdst(ro, dst) == 0)
1434 		rt = _rtcache_init(ro, clone);
1435 
1436 	rtcache_invariants(ro);
1437 
1438 	return rt;
1439 }
1440 
1441 void
1442 rtcache_free(struct route *ro)
1443 {
1444 	rtcache_clear(ro);
1445 	if (ro->ro_sa != NULL) {
1446 		sockaddr_free(ro->ro_sa);
1447 		ro->ro_sa = NULL;
1448 	}
1449 	rtcache_invariants(ro);
1450 }
1451 
1452 int
1453 rtcache_setdst(struct route *ro, const struct sockaddr *sa)
1454 {
1455 	KASSERT(sa != NULL);
1456 
1457 	rtcache_invariants(ro);
1458 	if (ro->ro_sa != NULL) {
1459 		if (ro->ro_sa->sa_family == sa->sa_family) {
1460 			rtcache_clear(ro);
1461 			sockaddr_copy(ro->ro_sa, ro->ro_sa->sa_len, sa);
1462 			rtcache_invariants(ro);
1463 			return 0;
1464 		}
1465 		/* free ro_sa, wrong family */
1466 		rtcache_free(ro);
1467 	}
1468 
1469 	KASSERT(ro->_ro_rt == NULL);
1470 
1471 	if ((ro->ro_sa = sockaddr_dup(sa, M_ZERO | M_NOWAIT)) == NULL) {
1472 		rtcache_invariants(ro);
1473 		return ENOMEM;
1474 	}
1475 	rtcache_invariants(ro);
1476 	return 0;
1477 }
1478 
1479 const struct sockaddr *
1480 rt_settag(struct rtentry *rt, const struct sockaddr *tag)
1481 {
1482 	if (rt->rt_tag != tag) {
1483 		if (rt->rt_tag != NULL)
1484 			sockaddr_free(rt->rt_tag);
1485 		rt->rt_tag = sockaddr_dup(tag, M_ZERO | M_NOWAIT);
1486 	}
1487 	return rt->rt_tag;
1488 }
1489 
1490 struct sockaddr *
1491 rt_gettag(const struct rtentry *rt)
1492 {
1493 	return rt->rt_tag;
1494 }
1495 
1496 int
1497 rt_check_reject_route(const struct rtentry *rt, const struct ifnet *ifp)
1498 {
1499 
1500 	if ((rt->rt_flags & RTF_REJECT) != 0) {
1501 		/* Mimic looutput */
1502 		if (ifp->if_flags & IFF_LOOPBACK)
1503 			return (rt->rt_flags & RTF_HOST) ?
1504 			    EHOSTUNREACH : ENETUNREACH;
1505 		else if (rt->rt_rmx.rmx_expire == 0 ||
1506 		    time_uptime < rt->rt_rmx.rmx_expire)
1507 			return (rt->rt_flags & RTF_GATEWAY) ?
1508 			    EHOSTUNREACH : EHOSTDOWN;
1509 	}
1510 
1511 	return 0;
1512 }
1513 
1514 #ifdef DDB
1515 
1516 #include <machine/db_machdep.h>
1517 #include <ddb/db_interface.h>
1518 #include <ddb/db_output.h>
1519 
1520 #define	rt_expire rt_rmx.rmx_expire
1521 
1522 static void
1523 db_print_sa(const struct sockaddr *sa)
1524 {
1525 	int len;
1526 	const u_char *p;
1527 
1528 	if (sa == NULL) {
1529 		db_printf("[NULL]");
1530 		return;
1531 	}
1532 
1533 	p = (const u_char *)sa;
1534 	len = sa->sa_len;
1535 	db_printf("[");
1536 	while (len > 0) {
1537 		db_printf("%d", *p);
1538 		p++; len--;
1539 		if (len) db_printf(",");
1540 	}
1541 	db_printf("]\n");
1542 }
1543 
1544 static void
1545 db_print_ifa(struct ifaddr *ifa)
1546 {
1547 	if (ifa == NULL)
1548 		return;
1549 	db_printf("  ifa_addr=");
1550 	db_print_sa(ifa->ifa_addr);
1551 	db_printf("  ifa_dsta=");
1552 	db_print_sa(ifa->ifa_dstaddr);
1553 	db_printf("  ifa_mask=");
1554 	db_print_sa(ifa->ifa_netmask);
1555 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
1556 			  ifa->ifa_flags,
1557 			  ifa->ifa_refcnt,
1558 			  ifa->ifa_metric);
1559 }
1560 
1561 /*
1562  * Function to pass to rt_walktree().
1563  * Return non-zero error to abort walk.
1564  */
1565 static int
1566 db_show_rtentry(struct rtentry *rt, void *w)
1567 {
1568 	db_printf("rtentry=%p", rt);
1569 
1570 	db_printf(" flags=0x%x refcnt=%d use=%"PRId64" expire=%"PRId64"\n",
1571 			  rt->rt_flags, rt->rt_refcnt,
1572 			  rt->rt_use, (uint64_t)rt->rt_expire);
1573 
1574 	db_printf(" key="); db_print_sa(rt_getkey(rt));
1575 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1576 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1577 
1578 	db_printf(" ifp=%p ", rt->rt_ifp);
1579 	if (rt->rt_ifp)
1580 		db_printf("(%s)", rt->rt_ifp->if_xname);
1581 	else
1582 		db_printf("(NULL)");
1583 
1584 	db_printf(" ifa=%p\n", rt->rt_ifa);
1585 	db_print_ifa(rt->rt_ifa);
1586 
1587 	db_printf(" gwroute=%p llinfo=%p\n",
1588 			  rt->rt_gwroute, rt->rt_llinfo);
1589 
1590 	return 0;
1591 }
1592 
1593 /*
1594  * Function to print all the route trees.
1595  * Use this from ddb:  "show routes"
1596  */
1597 void
1598 db_show_routes(db_expr_t addr, bool have_addr,
1599     db_expr_t count, const char *modif)
1600 {
1601 	rt_walktree(AF_INET, db_show_rtentry, NULL);
1602 }
1603 #endif
1604