Home | History | Annotate | Line # | Download | only in net
route.c revision 1.105
      1 /*	$NetBSD: route.c,v 1.105 2008/01/21 09:11:24 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the project nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1980, 1986, 1991, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)route.c	8.3 (Berkeley) 1/9/95
     98  */
     99 
    100 #include "opt_route.h"
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.105 2008/01/21 09:11:24 dyoung Exp $");
    104 
    105 #include <sys/param.h>
    106 #include <sys/sysctl.h>
    107 #include <sys/systm.h>
    108 #include <sys/callout.h>
    109 #include <sys/proc.h>
    110 #include <sys/mbuf.h>
    111 #include <sys/socket.h>
    112 #include <sys/socketvar.h>
    113 #include <sys/domain.h>
    114 #include <sys/protosw.h>
    115 #include <sys/kernel.h>
    116 #include <sys/ioctl.h>
    117 #include <sys/pool.h>
    118 
    119 #include <net/if.h>
    120 #include <net/route.h>
    121 #include <net/raw_cb.h>
    122 
    123 #include <netinet/in.h>
    124 #include <netinet/in_var.h>
    125 
    126 #ifdef RTFLUSH_DEBUG
    127 #define	rtcache_debug() __predict_false(_rtcache_debug)
    128 #else /* RTFLUSH_DEBUG */
    129 #define	rtcache_debug() 0
    130 #endif /* RTFLUSH_DEBUG */
    131 
    132 struct	route_cb route_cb;
    133 struct	rtstat	rtstat;
    134 struct	radix_node_head *rt_tables[AF_MAX+1];
    135 
    136 int	rttrash;		/* routes not in table but not freed */
    137 
    138 POOL_INIT(rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl", NULL,
    139     IPL_SOFTNET);
    140 POOL_INIT(rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", NULL,
    141     IPL_SOFTNET);
    142 
    143 struct callout rt_timer_ch; /* callout for rt_timer_timer() */
    144 
    145 #ifdef RTFLUSH_DEBUG
    146 static int _rtcache_debug = 0;
    147 #endif /* RTFLUSH_DEBUG */
    148 
    149 static int rtdeletemsg(struct rtentry *);
    150 static int rtflushclone1(struct rtentry *, void *);
    151 static void rtflushclone(sa_family_t family, struct rtentry *);
    152 
    153 #ifdef RTFLUSH_DEBUG
    154 SYSCTL_SETUP(sysctl_net_rtcache_setup, "sysctl net.rtcache.debug setup")
    155 {
    156 	const struct sysctlnode *rnode;
    157 
    158 	/* XXX do not duplicate */
    159 	if (sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
    160 	    CTLTYPE_NODE, "net", NULL, NULL, 0, NULL, 0, CTL_NET, CTL_EOL) != 0)
    161 		return;
    162 	if (sysctl_createv(clog, 0, &rnode, &rnode, CTLFLAG_PERMANENT,
    163 	    CTLTYPE_NODE,
    164 	    "rtcache", SYSCTL_DESCR("Route cache related settings"),
    165 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
    166 		return;
    167 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    168 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    169 	    "debug", SYSCTL_DESCR("Debug route caches"),
    170 	    NULL, 0, &_rtcache_debug, 0, CTL_CREATE, CTL_EOL) != 0)
    171 		return;
    172 }
    173 #endif /* RTFLUSH_DEBUG */
    174 
    175 struct ifaddr *
    176 rt_get_ifa(struct rtentry *rt)
    177 {
    178 	struct ifaddr *ifa;
    179 
    180 	if ((ifa = rt->rt_ifa) == NULL)
    181 		return ifa;
    182 	else if (ifa->ifa_getifa == NULL)
    183 		return ifa;
    184 #if 0
    185 	else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
    186 		return ifa;
    187 #endif
    188 	else {
    189 		ifa = (*ifa->ifa_getifa)(ifa, rt_getkey(rt));
    190 		rt_replace_ifa(rt, ifa);
    191 		return ifa;
    192 	}
    193 }
    194 
    195 static void
    196 rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
    197 {
    198 	rt->rt_ifa = ifa;
    199 	if (ifa->ifa_seqno != NULL)
    200 		rt->rt_ifa_seqno = *ifa->ifa_seqno;
    201 }
    202 
    203 void
    204 rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
    205 {
    206 	IFAREF(ifa);
    207 	IFAFREE(rt->rt_ifa);
    208 	rt_set_ifa1(rt, ifa);
    209 }
    210 
    211 static void
    212 rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
    213 {
    214 	IFAREF(ifa);
    215 	rt_set_ifa1(rt, ifa);
    216 }
    217 
    218 void
    219 rtable_init(void **table)
    220 {
    221 	struct domain *dom;
    222 	DOMAIN_FOREACH(dom)
    223 		if (dom->dom_rtattach)
    224 			dom->dom_rtattach(&table[dom->dom_family],
    225 			    dom->dom_rtoffset);
    226 }
    227 
    228 void
    229 route_init(void)
    230 {
    231 
    232 	rn_init();	/* initialize all zeroes, all ones, mask table */
    233 	rtable_init((void **)rt_tables);
    234 }
    235 
    236 void
    237 rtflushall(int family)
    238 {
    239 	struct domain *dom;
    240 
    241 	if (rtcache_debug())
    242 		printf("%s: enter\n", __func__);
    243 
    244 	if ((dom = pffinddomain(family)) == NULL)
    245 		return;
    246 
    247 	rtcache_invalidate(&dom->dom_rtcache);
    248 }
    249 
    250 void
    251 rtcache(struct route *ro)
    252 {
    253 	struct domain *dom;
    254 
    255 	KASSERT(ro->_ro_rt != NULL);
    256 	KASSERT(ro->ro_invalid == false);
    257 	KASSERT(rtcache_getdst(ro) != NULL);
    258 
    259 	if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) == NULL)
    260 		return;
    261 
    262 	LIST_INSERT_HEAD(&dom->dom_rtcache, ro, ro_rtcache_next);
    263 }
    264 
    265 /*
    266  * Packet routing routines.
    267  */
    268 struct rtentry *
    269 rtalloc1(const struct sockaddr *dst, int report)
    270 {
    271 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
    272 	struct rtentry *rt;
    273 	struct radix_node *rn;
    274 	struct rtentry *newrt = NULL;
    275 	struct rt_addrinfo info;
    276 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
    277 
    278 	if (rnh && (rn = rnh->rnh_matchaddr(dst, rnh)) &&
    279 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
    280 		newrt = rt = (struct rtentry *)rn;
    281 		if (report && (rt->rt_flags & RTF_CLONING)) {
    282 			err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
    283 			    &newrt);
    284 			if (err) {
    285 				newrt = rt;
    286 				rt->rt_refcnt++;
    287 				goto miss;
    288 			}
    289 			KASSERT(newrt != NULL);
    290 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
    291 				msgtype = RTM_RESOLVE;
    292 				goto miss;
    293 			}
    294 			/* Inform listeners of the new route */
    295 			memset(&info, 0, sizeof(info));
    296 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    297 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    298 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    299 			if (rt->rt_ifp != NULL) {
    300 				info.rti_info[RTAX_IFP] =
    301 				    rt->rt_ifp->if_dl->ifa_addr;
    302 				info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
    303 			}
    304 			rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
    305 		} else
    306 			rt->rt_refcnt++;
    307 	} else {
    308 		rtstat.rts_unreach++;
    309 	miss:	if (report) {
    310 			memset((void *)&info, 0, sizeof(info));
    311 			info.rti_info[RTAX_DST] = dst;
    312 			rt_missmsg(msgtype, &info, 0, err);
    313 		}
    314 	}
    315 	splx(s);
    316 	return newrt;
    317 }
    318 
    319 void
    320 rtfree(struct rtentry *rt)
    321 {
    322 	struct ifaddr *ifa;
    323 
    324 	if (rt == NULL)
    325 		panic("rtfree");
    326 	rt->rt_refcnt--;
    327 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
    328 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    329 			panic ("rtfree 2");
    330 		rttrash--;
    331 		if (rt->rt_refcnt < 0) {
    332 			printf("rtfree: %p not freed (neg refs)\n", rt);
    333 			return;
    334 		}
    335 		rt_timer_remove_all(rt, 0);
    336 		ifa = rt->rt_ifa;
    337 		rt->rt_ifa = NULL;
    338 		IFAFREE(ifa);
    339 		rt->rt_ifp = NULL;
    340 		rt_destroy(rt);
    341 		pool_put(&rtentry_pool, rt);
    342 	}
    343 }
    344 
    345 void
    346 ifafree(struct ifaddr *ifa)
    347 {
    348 
    349 #ifdef DIAGNOSTIC
    350 	if (ifa == NULL)
    351 		panic("ifafree: null ifa");
    352 	if (ifa->ifa_refcnt != 0)
    353 		panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
    354 #endif
    355 #ifdef IFAREF_DEBUG
    356 	printf("ifafree: freeing ifaddr %p\n", ifa);
    357 #endif
    358 	free(ifa, M_IFADDR);
    359 }
    360 
    361 static inline int
    362 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
    363 {
    364 	return sockaddr_cmp(sa1, sa2) == 0;
    365 }
    366 
    367 /*
    368  * Force a routing table entry to the specified
    369  * destination to go through the given gateway.
    370  * Normally called as a result of a routing redirect
    371  * message from the network layer.
    372  *
    373  * N.B.: must be called at splsoftnet
    374  */
    375 void
    376 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
    377 	const struct sockaddr *netmask, int flags, const struct sockaddr *src,
    378 	struct rtentry **rtp)
    379 {
    380 	struct rtentry *rt;
    381 	int error = 0;
    382 	u_quad_t *stat = NULL;
    383 	struct rt_addrinfo info;
    384 	struct ifaddr *ifa;
    385 
    386 	/* verify the gateway is directly reachable */
    387 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
    388 		error = ENETUNREACH;
    389 		goto out;
    390 	}
    391 	rt = rtalloc1(dst, 0);
    392 	/*
    393 	 * If the redirect isn't from our current router for this dst,
    394 	 * it's either old or wrong.  If it redirects us to ourselves,
    395 	 * we have a routing loop, perhaps as a result of an interface
    396 	 * going down recently.
    397 	 */
    398 	if (!(flags & RTF_DONE) && rt &&
    399 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
    400 		error = EINVAL;
    401 	else if (ifa_ifwithaddr(gateway))
    402 		error = EHOSTUNREACH;
    403 	if (error)
    404 		goto done;
    405 	/*
    406 	 * Create a new entry if we just got back a wildcard entry
    407 	 * or the lookup failed.  This is necessary for hosts
    408 	 * which use routing redirects generated by smart gateways
    409 	 * to dynamically build the routing tables.
    410 	 */
    411 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    412 		goto create;
    413 	/*
    414 	 * Don't listen to the redirect if it's
    415 	 * for a route to an interface.
    416 	 */
    417 	if (rt->rt_flags & RTF_GATEWAY) {
    418 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    419 			/*
    420 			 * Changing from route to net => route to host.
    421 			 * Create new route, rather than smashing route to net.
    422 			 */
    423 		create:
    424 			if (rt != NULL)
    425 				rtfree(rt);
    426 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    427 			info.rti_info[RTAX_DST] = dst;
    428 			info.rti_info[RTAX_GATEWAY] = gateway;
    429 			info.rti_info[RTAX_NETMASK] = netmask;
    430 			info.rti_ifa = ifa;
    431 			info.rti_flags = flags;
    432 			rt = NULL;
    433 			error = rtrequest1(RTM_ADD, &info, &rt);
    434 			if (rt != NULL)
    435 				flags = rt->rt_flags;
    436 			stat = &rtstat.rts_dynamic;
    437 		} else {
    438 			/*
    439 			 * Smash the current notion of the gateway to
    440 			 * this destination.  Should check about netmask!!!
    441 			 */
    442 			rt->rt_flags |= RTF_MODIFIED;
    443 			flags |= RTF_MODIFIED;
    444 			stat = &rtstat.rts_newgateway;
    445 			rt_setgate(rt, gateway);
    446 		}
    447 	} else
    448 		error = EHOSTUNREACH;
    449 done:
    450 	if (rt) {
    451 		if (rtp != NULL && !error)
    452 			*rtp = rt;
    453 		else
    454 			rtfree(rt);
    455 	}
    456 out:
    457 	if (error)
    458 		rtstat.rts_badredirect++;
    459 	else if (stat != NULL)
    460 		(*stat)++;
    461 	memset(&info, 0, sizeof(info));
    462 	info.rti_info[RTAX_DST] = dst;
    463 	info.rti_info[RTAX_GATEWAY] = gateway;
    464 	info.rti_info[RTAX_NETMASK] = netmask;
    465 	info.rti_info[RTAX_AUTHOR] = src;
    466 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    467 }
    468 
    469 /*
    470  * Delete a route and generate a message
    471  */
    472 static int
    473 rtdeletemsg(struct rtentry *rt)
    474 {
    475 	int error;
    476 	struct rt_addrinfo info;
    477 
    478 	/*
    479 	 * Request the new route so that the entry is not actually
    480 	 * deleted.  That will allow the information being reported to
    481 	 * be accurate (and consistent with route_output()).
    482 	 */
    483 	memset(&info, 0, sizeof(info));
    484 	info.rti_info[RTAX_DST] = rt_getkey(rt);
    485 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    486 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    487 	info.rti_flags = rt->rt_flags;
    488 	error = rtrequest1(RTM_DELETE, &info, &rt);
    489 
    490 	rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
    491 
    492 	/* Adjust the refcount */
    493 	if (error == 0 && rt->rt_refcnt <= 0) {
    494 		rt->rt_refcnt++;
    495 		rtfree(rt);
    496 	}
    497 	return error;
    498 }
    499 
    500 static int
    501 rtflushclone1(struct rtentry *rt, void *arg)
    502 {
    503 	struct rtentry *parent;
    504 
    505 	parent = (struct rtentry *)arg;
    506 	if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
    507 		rtdeletemsg(rt);
    508 	return 0;
    509 }
    510 
    511 static void
    512 rtflushclone(sa_family_t family, struct rtentry *parent)
    513 {
    514 
    515 #ifdef DIAGNOSTIC
    516 	if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
    517 		panic("rtflushclone: called with a non-cloning route");
    518 #endif
    519 	rt_walktree(family, rtflushclone1, (void *)parent);
    520 }
    521 
    522 /*
    523  * Routing table ioctl interface.
    524  */
    525 int
    526 rtioctl(u_long req, void *data, struct lwp *l)
    527 {
    528 	return EOPNOTSUPP;
    529 }
    530 
    531 struct ifaddr *
    532 ifa_ifwithroute(int flags, const struct sockaddr *dst,
    533 	const struct sockaddr *gateway)
    534 {
    535 	struct ifaddr *ifa;
    536 	if ((flags & RTF_GATEWAY) == 0) {
    537 		/*
    538 		 * If we are adding a route to an interface,
    539 		 * and the interface is a pt to pt link
    540 		 * we should search for the destination
    541 		 * as our clue to the interface.  Otherwise
    542 		 * we can use the local address.
    543 		 */
    544 		ifa = NULL;
    545 		if (flags & RTF_HOST)
    546 			ifa = ifa_ifwithdstaddr(dst);
    547 		if (ifa == NULL)
    548 			ifa = ifa_ifwithaddr(gateway);
    549 	} else {
    550 		/*
    551 		 * If we are adding a route to a remote net
    552 		 * or host, the gateway may still be on the
    553 		 * other end of a pt to pt link.
    554 		 */
    555 		ifa = ifa_ifwithdstaddr(gateway);
    556 	}
    557 	if (ifa == NULL)
    558 		ifa = ifa_ifwithnet(gateway);
    559 	if (ifa == NULL) {
    560 		struct rtentry *rt = rtalloc1(dst, 0);
    561 		if (rt == NULL)
    562 			return NULL;
    563 		rt->rt_refcnt--;
    564 		if ((ifa = rt->rt_ifa) == NULL)
    565 			return NULL;
    566 	}
    567 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
    568 		struct ifaddr *oifa = ifa;
    569 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
    570 		if (ifa == 0)
    571 			ifa = oifa;
    572 	}
    573 	return ifa;
    574 }
    575 
    576 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    577 
    578 int
    579 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
    580 	const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
    581 {
    582 	struct rt_addrinfo info;
    583 
    584 	memset(&info, 0, sizeof(info));
    585 	info.rti_flags = flags;
    586 	info.rti_info[RTAX_DST] = dst;
    587 	info.rti_info[RTAX_GATEWAY] = gateway;
    588 	info.rti_info[RTAX_NETMASK] = netmask;
    589 	return rtrequest1(req, &info, ret_nrt);
    590 }
    591 
    592 int
    593 rt_getifa(struct rt_addrinfo *info)
    594 {
    595 	struct ifaddr *ifa;
    596 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
    597 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
    598 	const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
    599 	const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
    600 	int flags = info->rti_flags;
    601 
    602 	/*
    603 	 * ifp may be specified by sockaddr_dl when protocol address
    604 	 * is ambiguous
    605 	 */
    606 	if (info->rti_ifp == NULL && ifpaddr != NULL
    607 	    && ifpaddr->sa_family == AF_LINK &&
    608 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
    609 		info->rti_ifp = ifa->ifa_ifp;
    610 	if (info->rti_ifa == NULL && ifaaddr != NULL)
    611 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
    612 	if (info->rti_ifa == NULL) {
    613 		const struct sockaddr *sa;
    614 
    615 		sa = ifaaddr != NULL ? ifaaddr :
    616 		    (gateway != NULL ? gateway : dst);
    617 		if (sa != NULL && info->rti_ifp != NULL)
    618 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
    619 		else if (dst != NULL && gateway != NULL)
    620 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
    621 		else if (sa != NULL)
    622 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
    623 	}
    624 	if ((ifa = info->rti_ifa) == NULL)
    625 		return ENETUNREACH;
    626 	if (ifa->ifa_getifa != NULL)
    627 		info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
    628 	if (info->rti_ifp == NULL)
    629 		info->rti_ifp = ifa->ifa_ifp;
    630 	return 0;
    631 }
    632 
    633 int
    634 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
    635 {
    636 	int s = splsoftnet();
    637 	int error = 0;
    638 	struct rtentry *rt, *crt;
    639 	struct radix_node *rn;
    640 	struct radix_node_head *rnh;
    641 	struct ifaddr *ifa;
    642 	struct sockaddr_storage maskeddst;
    643 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
    644 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
    645 	const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
    646 	int flags = info->rti_flags;
    647 #define senderr(x) { error = x ; goto bad; }
    648 
    649 	if ((rnh = rt_tables[dst->sa_family]) == NULL)
    650 		senderr(ESRCH);
    651 	if (flags & RTF_HOST)
    652 		netmask = NULL;
    653 	switch (req) {
    654 	case RTM_DELETE:
    655 		if (netmask) {
    656 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
    657 			    netmask);
    658 			dst = (struct sockaddr *)&maskeddst;
    659 		}
    660 		if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
    661 			senderr(ESRCH);
    662 		rt = (struct rtentry *)rn;
    663 		if ((rt->rt_flags & RTF_CLONING) != 0) {
    664 			/* clean up any cloned children */
    665 			rtflushclone(dst->sa_family, rt);
    666 		}
    667 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
    668 			senderr(ESRCH);
    669 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    670 			panic ("rtrequest delete");
    671 		rt = (struct rtentry *)rn;
    672 		if (rt->rt_gwroute) {
    673 			RTFREE(rt->rt_gwroute);
    674 			rt->rt_gwroute = NULL;
    675 		}
    676 		if (rt->rt_parent) {
    677 			rt->rt_parent->rt_refcnt--;
    678 			rt->rt_parent = NULL;
    679 		}
    680 		rt->rt_flags &= ~RTF_UP;
    681 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
    682 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
    683 		rttrash++;
    684 		if (ret_nrt)
    685 			*ret_nrt = rt;
    686 		else if (rt->rt_refcnt <= 0) {
    687 			rt->rt_refcnt++;
    688 			rtfree(rt);
    689 		}
    690 		break;
    691 
    692 	case RTM_RESOLVE:
    693 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
    694 			senderr(EINVAL);
    695 		if ((rt->rt_flags & RTF_CLONING) == 0)
    696 			senderr(EINVAL);
    697 		ifa = rt->rt_ifa;
    698 		flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
    699 		flags |= RTF_CLONED;
    700 		gateway = rt->rt_gateway;
    701 		flags |= RTF_HOST;
    702 		goto makeroute;
    703 
    704 	case RTM_ADD:
    705 		if (info->rti_ifa == NULL && (error = rt_getifa(info)))
    706 			senderr(error);
    707 		ifa = info->rti_ifa;
    708 	makeroute:
    709 		/* Already at splsoftnet() so pool_get/pool_put are safe */
    710 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
    711 		if (rt == NULL)
    712 			senderr(ENOBUFS);
    713 		Bzero(rt, sizeof(*rt));
    714 		rt->rt_flags = RTF_UP | flags;
    715 		LIST_INIT(&rt->rt_timer);
    716 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__, __LINE__,
    717 		    (void *)rt->_rt_key);
    718 		if (rt_setkey(rt, dst, M_NOWAIT) == NULL ||
    719 		    rt_setgate(rt, gateway) != 0) {
    720 			pool_put(&rtentry_pool, rt);
    721 			senderr(ENOBUFS);
    722 		}
    723 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__, __LINE__,
    724 		    (void *)rt->_rt_key);
    725 		if (netmask) {
    726 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
    727 			    netmask);
    728 			rt_setkey(rt, (struct sockaddr *)&maskeddst, M_NOWAIT);
    729 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    730 			    __LINE__, (void *)rt->_rt_key);
    731 		} else {
    732 			rt_setkey(rt, dst, M_NOWAIT);
    733 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    734 			    __LINE__, (void *)rt->_rt_key);
    735 		}
    736 		rt_set_ifa(rt, ifa);
    737 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    738 		    __LINE__, (void *)rt->_rt_key);
    739 		rt->rt_ifp = ifa->ifa_ifp;
    740 		if (req == RTM_RESOLVE) {
    741 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
    742 			rt->rt_parent = *ret_nrt;
    743 			rt->rt_parent->rt_refcnt++;
    744 		}
    745 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    746 		    __LINE__, (void *)rt->_rt_key);
    747 		rn = rnh->rnh_addaddr(rt_getkey(rt), netmask, rnh,
    748 		    rt->rt_nodes);
    749 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    750 		    __LINE__, (void *)rt->_rt_key);
    751 		if (rn == NULL && (crt = rtalloc1(rt_getkey(rt), 0)) != NULL) {
    752 			/* overwrite cloned route */
    753 			if ((crt->rt_flags & RTF_CLONED) != 0) {
    754 				rtdeletemsg(crt);
    755 				rn = rnh->rnh_addaddr(rt_getkey(rt),
    756 				    netmask, rnh, rt->rt_nodes);
    757 			}
    758 			RTFREE(crt);
    759 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    760 			    __LINE__, (void *)rt->_rt_key);
    761 		}
    762 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    763 		    __LINE__, (void *)rt->_rt_key);
    764 		if (rn == NULL) {
    765 			IFAFREE(ifa);
    766 			if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
    767 				rtfree(rt->rt_parent);
    768 			if (rt->rt_gwroute)
    769 				rtfree(rt->rt_gwroute);
    770 			rt_destroy(rt);
    771 			pool_put(&rtentry_pool, rt);
    772 			senderr(EEXIST);
    773 		}
    774 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    775 		    __LINE__, (void *)rt->_rt_key);
    776 		if (ifa->ifa_rtrequest)
    777 			ifa->ifa_rtrequest(req, rt, info);
    778 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    779 		    __LINE__, (void *)rt->_rt_key);
    780 		if (ret_nrt) {
    781 			*ret_nrt = rt;
    782 			rt->rt_refcnt++;
    783 		}
    784 		if ((rt->rt_flags & RTF_CLONING) != 0) {
    785 			/* clean up any cloned children */
    786 			rtflushclone(dst->sa_family, rt);
    787 		}
    788 		rtflushall(dst->sa_family);
    789 		break;
    790 	case RTM_GET:
    791 		if (netmask != NULL) {
    792 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
    793 			    netmask);
    794 			dst = (struct sockaddr *)&maskeddst;
    795 		}
    796 		rn = rnh->rnh_lookup(dst, netmask, rnh);
    797 		if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0)
    798 			senderr(ESRCH);
    799 		if (ret_nrt != NULL) {
    800 			rt = (struct rtentry *)rn;
    801 			*ret_nrt = rt;
    802 			rt->rt_refcnt++;
    803 		}
    804 		break;
    805 	}
    806 bad:
    807 	splx(s);
    808 	return error;
    809 }
    810 
    811 int
    812 rt_setgate(struct rtentry *rt, const struct sockaddr *gate)
    813 {
    814 	KASSERT(rt != rt->rt_gwroute);
    815 
    816 	KASSERT(rt->_rt_key != NULL);
    817 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    818 	    __LINE__, (void *)rt->_rt_key);
    819 
    820 	if (rt->rt_gwroute) {
    821 		RTFREE(rt->rt_gwroute);
    822 		rt->rt_gwroute = NULL;
    823 	}
    824 	KASSERT(rt->_rt_key != NULL);
    825 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    826 	    __LINE__, (void *)rt->_rt_key);
    827 	if (rt->rt_gateway != NULL)
    828 		sockaddr_free(rt->rt_gateway);
    829 	KASSERT(rt->_rt_key != NULL);
    830 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    831 	    __LINE__, (void *)rt->_rt_key);
    832 	if ((rt->rt_gateway = sockaddr_dup(gate, M_NOWAIT)) == NULL)
    833 		return ENOMEM;
    834 	KASSERT(rt->_rt_key != NULL);
    835 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    836 	    __LINE__, (void *)rt->_rt_key);
    837 
    838 	if (rt->rt_flags & RTF_GATEWAY) {
    839 		KASSERT(rt->_rt_key != NULL);
    840 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    841 		    __LINE__, (void *)rt->_rt_key);
    842 		rt->rt_gwroute = rtalloc1(gate, 1);
    843 		/*
    844 		 * If we switched gateways, grab the MTU from the new
    845 		 * gateway route if the current MTU, if the current MTU is
    846 		 * greater than the MTU of gateway.
    847 		 * Note that, if the MTU of gateway is 0, we will reset the
    848 		 * MTU of the route to run PMTUD again from scratch. XXX
    849 		 */
    850 		KASSERT(rt->_rt_key != NULL);
    851 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    852 		    __LINE__, (void *)rt->_rt_key);
    853 		if (rt->rt_gwroute
    854 		    && !(rt->rt_rmx.rmx_locks & RTV_MTU)
    855 		    && rt->rt_rmx.rmx_mtu
    856 		    && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
    857 			rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
    858 		}
    859 	}
    860 	KASSERT(rt->_rt_key != NULL);
    861 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
    862 	    __LINE__, (void *)rt->_rt_key);
    863 	return 0;
    864 }
    865 
    866 void
    867 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
    868 	const struct sockaddr *netmask)
    869 {
    870 	const char *netmaskp = &netmask->sa_data[0],
    871 	           *srcp = &src->sa_data[0];
    872 	char *dstp = &dst->sa_data[0];
    873 	const char *maskend = dstp + MIN(netmask->sa_len, src->sa_len);
    874 	const char *srcend = dstp + src->sa_len;
    875 
    876 	dst->sa_len = src->sa_len;
    877 	dst->sa_family = src->sa_family;
    878 
    879 	while (dstp < maskend)
    880 		*dstp++ = *srcp++ & *netmaskp++;
    881 	if (dstp < srcend)
    882 		memset(dstp, 0, (size_t)(srcend - dstp));
    883 }
    884 
    885 /*
    886  * Set up or tear down a routing table entry, normally
    887  * for an interface.
    888  */
    889 int
    890 rtinit(struct ifaddr *ifa, int cmd, int flags)
    891 {
    892 	struct rtentry *rt;
    893 	struct sockaddr *dst, *odst;
    894 	struct sockaddr_storage maskeddst;
    895 	struct rtentry *nrt = NULL;
    896 	int error;
    897 	struct rt_addrinfo info;
    898 
    899 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    900 	if (cmd == RTM_DELETE) {
    901 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    902 			/* Delete subnet route for this interface */
    903 			odst = dst;
    904 			dst = (struct sockaddr *)&maskeddst;
    905 			rt_maskedcopy(odst, dst, ifa->ifa_netmask);
    906 		}
    907 		if ((rt = rtalloc1(dst, 0)) != NULL) {
    908 			rt->rt_refcnt--;
    909 			if (rt->rt_ifa != ifa)
    910 				return (flags & RTF_HOST) ? EHOSTUNREACH
    911 							: ENETUNREACH;
    912 		}
    913 	}
    914 	memset(&info, 0, sizeof(info));
    915 	info.rti_ifa = ifa;
    916 	info.rti_flags = flags | ifa->ifa_flags;
    917 	info.rti_info[RTAX_DST] = dst;
    918 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
    919 	/*
    920 	 * XXX here, it seems that we are assuming that ifa_netmask is NULL
    921 	 * for RTF_HOST.  bsdi4 passes NULL explicitly (via intermediate
    922 	 * variable) when RTF_HOST is 1.  still not sure if i can safely
    923 	 * change it to meet bsdi4 behavior.
    924 	 */
    925 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    926 	error = rtrequest1(cmd, &info, &nrt);
    927 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
    928 		rt_newaddrmsg(cmd, ifa, error, nrt);
    929 		if (rt->rt_refcnt <= 0) {
    930 			rt->rt_refcnt++;
    931 			rtfree(rt);
    932 		}
    933 	}
    934 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
    935 		rt->rt_refcnt--;
    936 		if (rt->rt_ifa != ifa) {
    937 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
    938 				rt->rt_ifa);
    939 			if (rt->rt_ifa->ifa_rtrequest)
    940 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
    941 			rt_replace_ifa(rt, ifa);
    942 			rt->rt_ifp = ifa->ifa_ifp;
    943 			if (ifa->ifa_rtrequest)
    944 				ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
    945 		}
    946 		rt_newaddrmsg(cmd, ifa, error, nrt);
    947 	}
    948 	return error;
    949 }
    950 
    951 /*
    952  * Route timer routines.  These routes allow functions to be called
    953  * for various routes at any time.  This is useful in supporting
    954  * path MTU discovery and redirect route deletion.
    955  *
    956  * This is similar to some BSDI internal functions, but it provides
    957  * for multiple queues for efficiency's sake...
    958  */
    959 
    960 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
    961 static int rt_init_done = 0;
    962 
    963 #define RTTIMER_CALLOUT(r)	do {					\
    964 		if (r->rtt_func != NULL) {				\
    965 			(*r->rtt_func)(r->rtt_rt, r);			\
    966 		} else {						\
    967 			rtrequest((int) RTM_DELETE,			\
    968 				  rt_getkey(r->rtt_rt),			\
    969 				  0, 0, 0, 0);				\
    970 		}							\
    971 	} while (/*CONSTCOND*/0)
    972 
    973 /*
    974  * Some subtle order problems with domain initialization mean that
    975  * we cannot count on this being run from rt_init before various
    976  * protocol initializations are done.  Therefore, we make sure
    977  * that this is run when the first queue is added...
    978  */
    979 
    980 void
    981 rt_timer_init(void)
    982 {
    983 	assert(rt_init_done == 0);
    984 
    985 	LIST_INIT(&rttimer_queue_head);
    986 	callout_init(&rt_timer_ch, 0);
    987 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
    988 	rt_init_done = 1;
    989 }
    990 
    991 struct rttimer_queue *
    992 rt_timer_queue_create(u_int timeout)
    993 {
    994 	struct rttimer_queue *rtq;
    995 
    996 	if (rt_init_done == 0)
    997 		rt_timer_init();
    998 
    999 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
   1000 	if (rtq == NULL)
   1001 		return NULL;
   1002 	Bzero(rtq, sizeof *rtq);
   1003 
   1004 	rtq->rtq_timeout = timeout;
   1005 	rtq->rtq_count = 0;
   1006 	TAILQ_INIT(&rtq->rtq_head);
   1007 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
   1008 
   1009 	return rtq;
   1010 }
   1011 
   1012 void
   1013 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
   1014 {
   1015 
   1016 	rtq->rtq_timeout = timeout;
   1017 }
   1018 
   1019 void
   1020 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
   1021 {
   1022 	struct rttimer *r;
   1023 
   1024 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
   1025 		LIST_REMOVE(r, rtt_link);
   1026 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
   1027 		if (destroy)
   1028 			RTTIMER_CALLOUT(r);
   1029 		/* we are already at splsoftnet */
   1030 		pool_put(&rttimer_pool, r);
   1031 		if (rtq->rtq_count > 0)
   1032 			rtq->rtq_count--;
   1033 		else
   1034 			printf("rt_timer_queue_remove_all: "
   1035 			    "rtq_count reached 0\n");
   1036 	}
   1037 }
   1038 
   1039 void
   1040 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
   1041 {
   1042 
   1043 	rt_timer_queue_remove_all(rtq, destroy);
   1044 
   1045 	LIST_REMOVE(rtq, rtq_link);
   1046 
   1047 	/*
   1048 	 * Caller is responsible for freeing the rttimer_queue structure.
   1049 	 */
   1050 }
   1051 
   1052 unsigned long
   1053 rt_timer_count(struct rttimer_queue *rtq)
   1054 {
   1055 	return rtq->rtq_count;
   1056 }
   1057 
   1058 void
   1059 rt_timer_remove_all(struct rtentry *rt, int destroy)
   1060 {
   1061 	struct rttimer *r;
   1062 
   1063 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
   1064 		LIST_REMOVE(r, rtt_link);
   1065 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1066 		if (destroy)
   1067 			RTTIMER_CALLOUT(r);
   1068 		if (r->rtt_queue->rtq_count > 0)
   1069 			r->rtt_queue->rtq_count--;
   1070 		else
   1071 			printf("rt_timer_remove_all: rtq_count reached 0\n");
   1072 		/* we are already at splsoftnet */
   1073 		pool_put(&rttimer_pool, r);
   1074 	}
   1075 }
   1076 
   1077 int
   1078 rt_timer_add(struct rtentry *rt,
   1079 	void (*func)(struct rtentry *, struct rttimer *),
   1080 	struct rttimer_queue *queue)
   1081 {
   1082 	struct rttimer *r;
   1083 	int s;
   1084 
   1085 	/*
   1086 	 * If there's already a timer with this action, destroy it before
   1087 	 * we add a new one.
   1088 	 */
   1089 	LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
   1090 		if (r->rtt_func == func)
   1091 			break;
   1092 	}
   1093 	if (r != NULL) {
   1094 		LIST_REMOVE(r, rtt_link);
   1095 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1096 		if (r->rtt_queue->rtq_count > 0)
   1097 			r->rtt_queue->rtq_count--;
   1098 		else
   1099 			printf("rt_timer_add: rtq_count reached 0\n");
   1100 	} else {
   1101 		s = splsoftnet();
   1102 		r = pool_get(&rttimer_pool, PR_NOWAIT);
   1103 		splx(s);
   1104 		if (r == NULL)
   1105 			return ENOBUFS;
   1106 	}
   1107 
   1108 	memset(r, 0, sizeof(*r));
   1109 
   1110 	r->rtt_rt = rt;
   1111 	r->rtt_time = time_uptime;
   1112 	r->rtt_func = func;
   1113 	r->rtt_queue = queue;
   1114 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
   1115 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
   1116 	r->rtt_queue->rtq_count++;
   1117 
   1118 	return 0;
   1119 }
   1120 
   1121 /* ARGSUSED */
   1122 void
   1123 rt_timer_timer(void *arg)
   1124 {
   1125 	struct rttimer_queue *rtq;
   1126 	struct rttimer *r;
   1127 	int s;
   1128 
   1129 	s = splsoftnet();
   1130 	LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
   1131 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
   1132 		    (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
   1133 			LIST_REMOVE(r, rtt_link);
   1134 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
   1135 			RTTIMER_CALLOUT(r);
   1136 			pool_put(&rttimer_pool, r);
   1137 			if (rtq->rtq_count > 0)
   1138 				rtq->rtq_count--;
   1139 			else
   1140 				printf("rt_timer_timer: rtq_count reached 0\n");
   1141 		}
   1142 	}
   1143 	splx(s);
   1144 
   1145 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
   1146 }
   1147 
   1148 static struct rtentry *
   1149 _rtcache_init(struct route *ro, int flag)
   1150 {
   1151 	KASSERT(ro->_ro_rt == NULL);
   1152 
   1153 	if (rtcache_getdst(ro) == NULL)
   1154 		return NULL;
   1155 	ro->ro_invalid = false;
   1156 	if ((ro->_ro_rt = rtalloc1(rtcache_getdst(ro), flag)) != NULL)
   1157 		rtcache(ro);
   1158 
   1159 	return ro->_ro_rt;
   1160 }
   1161 
   1162 struct rtentry *
   1163 rtcache_init(struct route *ro)
   1164 {
   1165 	return _rtcache_init(ro, 1);
   1166 }
   1167 
   1168 struct rtentry *
   1169 rtcache_init_noclone(struct route *ro)
   1170 {
   1171 	return _rtcache_init(ro, 0);
   1172 }
   1173 
   1174 struct rtentry *
   1175 rtcache_update(struct route *ro, int clone)
   1176 {
   1177 	rtcache_clear(ro);
   1178 	return _rtcache_init(ro, clone);
   1179 }
   1180 
   1181 void
   1182 rtcache_copy(struct route *new_ro, const struct route *old_ro)
   1183 {
   1184 	struct rtentry *rt;
   1185 
   1186 	KASSERT(new_ro != old_ro);
   1187 
   1188 	if ((rt = rtcache_validate(old_ro)) != NULL)
   1189 		rt->rt_refcnt++;
   1190 
   1191 	if (rtcache_getdst(old_ro) == NULL ||
   1192 	    rtcache_setdst(new_ro, rtcache_getdst(old_ro)) != 0)
   1193 		return;
   1194 
   1195 	new_ro->ro_invalid = false;
   1196 	if ((new_ro->_ro_rt = rt) != NULL)
   1197 		rtcache(new_ro);
   1198 }
   1199 
   1200 static struct dom_rtlist invalid_routes = LIST_HEAD_INITIALIZER(dom_rtlist);
   1201 
   1202 void
   1203 rtcache_invalidate(struct dom_rtlist *rtlist)
   1204 {
   1205 	struct route *ro;
   1206 
   1207 	while ((ro = LIST_FIRST(rtlist)) != NULL) {
   1208 		KASSERT(ro->_ro_rt != NULL);
   1209 		ro->ro_invalid = true;
   1210 		LIST_REMOVE(ro, ro_rtcache_next);
   1211 		LIST_INSERT_HEAD(&invalid_routes, ro, ro_rtcache_next);
   1212 	}
   1213 }
   1214 
   1215 void
   1216 rtcache_clear(struct route *ro)
   1217 {
   1218 	if (ro->_ro_rt == NULL)
   1219 		return;
   1220 
   1221 	KASSERT(rtcache_getdst(ro) != NULL);
   1222 
   1223 	LIST_REMOVE(ro, ro_rtcache_next);
   1224 
   1225 	RTFREE(ro->_ro_rt);
   1226 	ro->_ro_rt = NULL;
   1227 }
   1228 
   1229 struct rtentry *
   1230 rtcache_lookup2(struct route *ro, const struct sockaddr *dst, int clone,
   1231     int *hitp)
   1232 {
   1233 	const struct sockaddr *odst;
   1234 	struct rtentry *rt = NULL;
   1235 
   1236 	odst = rtcache_getdst(ro);
   1237 
   1238 	if (odst == NULL)
   1239 		;
   1240 	else if (sockaddr_cmp(odst, dst) != 0)
   1241 		rtcache_free(ro);
   1242 	else if ((rt = rtcache_validate(ro)) == NULL)
   1243 		rtcache_clear(ro);
   1244 
   1245 	if (rt == NULL) {
   1246 		*hitp = 0;
   1247 		if (rtcache_setdst(ro, dst) == 0)
   1248 			rt = _rtcache_init(ro, clone);
   1249 	} else
   1250 		*hitp = 1;
   1251 
   1252 	return rt;
   1253 }
   1254 
   1255 void
   1256 rtcache_free(struct route *ro)
   1257 {
   1258 	rtcache_clear(ro);
   1259 	if (ro->ro_sa != NULL) {
   1260 		sockaddr_free(ro->ro_sa);
   1261 		ro->ro_sa = NULL;
   1262 	}
   1263 }
   1264 
   1265 int
   1266 rtcache_setdst(struct route *ro, const struct sockaddr *sa)
   1267 {
   1268 	KASSERT(sa != NULL);
   1269 
   1270 	if (ro->ro_sa != NULL && ro->ro_sa->sa_family == sa->sa_family) {
   1271 		rtcache_clear(ro);
   1272 		if (sockaddr_copy(ro->ro_sa, ro->ro_sa->sa_len, sa) != NULL)
   1273 			return 0;
   1274 		sockaddr_free(ro->ro_sa);
   1275 	} else if (ro->ro_sa != NULL)
   1276 		rtcache_free(ro);	/* free ro_sa, wrong family */
   1277 
   1278 	if ((ro->ro_sa = sockaddr_dup(sa, M_NOWAIT)) == NULL)
   1279 		return ENOMEM;
   1280 	return 0;
   1281 }
   1282 
   1283 static int
   1284 rt_walktree_visitor(struct radix_node *rn, void *v)
   1285 {
   1286 	struct rtwalk *rw = (struct rtwalk *)v;
   1287 
   1288 	return (*rw->rw_f)((struct rtentry *)rn, rw->rw_v);
   1289 }
   1290 
   1291 int
   1292 rt_walktree(sa_family_t family, int (*f)(struct rtentry *, void *), void *v)
   1293 {
   1294 	struct radix_node_head *rnh = rt_tables[family];
   1295 	struct rtwalk rw;
   1296 
   1297 	if (rnh == NULL)
   1298 		return 0;
   1299 
   1300 	rw.rw_f = f;
   1301 	rw.rw_v = v;
   1302 
   1303 	return rn_walktree(rnh, rt_walktree_visitor, &rw);
   1304 }
   1305