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