Home | History | Annotate | Line # | Download | only in net
route.c revision 1.89
      1 /*	$NetBSD: route.c,v 1.89 2007/04/22 13:05:21 xtraeme 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 <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.89 2007/04/22 13:05:21 xtraeme Exp $");
    102 
    103 #include <sys/param.h>
    104 #include <sys/systm.h>
    105 #include <sys/callout.h>
    106 #include <sys/proc.h>
    107 #include <sys/mbuf.h>
    108 #include <sys/socket.h>
    109 #include <sys/socketvar.h>
    110 #include <sys/domain.h>
    111 #include <sys/protosw.h>
    112 #include <sys/kernel.h>
    113 #include <sys/ioctl.h>
    114 #include <sys/pool.h>
    115 
    116 #include <net/if.h>
    117 #include <net/route.h>
    118 #include <net/raw_cb.h>
    119 
    120 #include <netinet/in.h>
    121 #include <netinet/in_var.h>
    122 
    123 
    124 struct	route_cb route_cb;
    125 struct	rtstat	rtstat;
    126 struct	radix_node_head *rt_tables[AF_MAX+1];
    127 
    128 int	rttrash;		/* routes not in table but not freed */
    129 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
    130 
    131 POOL_INIT(rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl", NULL,
    132     IPL_SOFTNET);
    133 POOL_INIT(rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", NULL,
    134     IPL_SOFTNET);
    135 
    136 struct callout rt_timer_ch; /* callout for rt_timer_timer() */
    137 
    138 static int rtdeletemsg(struct rtentry *);
    139 static int rtflushclone1(struct radix_node *, void *);
    140 static void rtflushclone(struct radix_node_head *, struct rtentry *);
    141 
    142 struct ifaddr *
    143 rt_get_ifa(struct rtentry *rt)
    144 {
    145 	struct ifaddr *ifa;
    146 
    147 	if ((ifa = rt->rt_ifa) == NULL)
    148 		return ifa;
    149 	else if (ifa->ifa_getifa == NULL)
    150 		return ifa;
    151 #if 0
    152 	else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
    153 		return ifa;
    154 #endif
    155 	else {
    156 		ifa = (*ifa->ifa_getifa)(ifa, rt_key(rt));
    157 		rt_replace_ifa(rt, ifa);
    158 		return ifa;
    159 	}
    160 }
    161 
    162 static void
    163 rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
    164 {
    165 	rt->rt_ifa = ifa;
    166 	if (ifa->ifa_seqno != NULL)
    167 		rt->rt_ifa_seqno = *ifa->ifa_seqno;
    168 }
    169 
    170 void
    171 rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
    172 {
    173 	IFAREF(ifa);
    174 	IFAFREE(rt->rt_ifa);
    175 	rt_set_ifa1(rt, ifa);
    176 }
    177 
    178 static void
    179 rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
    180 {
    181 	IFAREF(ifa);
    182 	rt_set_ifa1(rt, ifa);
    183 }
    184 
    185 void
    186 rtable_init(void **table)
    187 {
    188 	struct domain *dom;
    189 	DOMAIN_FOREACH(dom)
    190 		if (dom->dom_rtattach)
    191 			dom->dom_rtattach(&table[dom->dom_family],
    192 			    dom->dom_rtoffset);
    193 }
    194 
    195 void
    196 route_init(void)
    197 {
    198 
    199 	rn_init();	/* initialize all zeroes, all ones, mask table */
    200 	rtable_init((void **)rt_tables);
    201 }
    202 
    203 void
    204 rtflushall(int family)
    205 {
    206 	const struct domain *dom;
    207 
    208 	if ((dom = pffinddomain(family)) != NULL && dom->dom_rtflushall != NULL)
    209 		(*dom->dom_rtflushall)();
    210 }
    211 
    212 void
    213 rtflush(struct route *ro)
    214 {
    215 	const struct domain *dom;
    216 
    217 	KASSERT(ro->ro_rt != NULL);
    218 
    219 	RTFREE(ro->ro_rt);
    220 	ro->ro_rt = NULL;
    221 
    222 	if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) != NULL &&
    223 	    dom->dom_rtflush != NULL)
    224 		(*dom->dom_rtflush)(ro);
    225 }
    226 
    227 void
    228 rtcache(struct route *ro)
    229 {
    230 	const struct domain *dom;
    231 
    232 	KASSERT(ro->ro_rt != NULL);
    233 
    234 	if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) != NULL &&
    235 	    dom->dom_rtcache != NULL)
    236 		(*dom->dom_rtcache)(ro);
    237 }
    238 
    239 /*
    240  * Packet routing routines.
    241  */
    242 void
    243 rtalloc(struct route *ro)
    244 {
    245 	if (ro->ro_rt != NULL) {
    246 		if (ro->ro_rt->rt_ifp != NULL &&
    247 		    (ro->ro_rt->rt_flags & RTF_UP) != 0)
    248 			return;
    249 		rtflush(ro);
    250 	}
    251 	if ((ro->ro_rt = rtalloc1(rtcache_getdst(ro), 1)) == NULL)
    252 		return;
    253 	rtcache(ro);
    254 }
    255 
    256 struct rtentry *
    257 rtalloc1(const struct sockaddr *dst, int report)
    258 {
    259 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
    260 	struct rtentry *rt;
    261 	struct radix_node *rn;
    262 	struct rtentry *newrt = NULL;
    263 	struct rt_addrinfo info;
    264 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
    265 
    266 	if (rnh && (rn = rnh->rnh_matchaddr(dst, rnh)) &&
    267 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
    268 		newrt = rt = (struct rtentry *)rn;
    269 		if (report && (rt->rt_flags & RTF_CLONING)) {
    270 			err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
    271 			    &newrt);
    272 			if (err) {
    273 				newrt = rt;
    274 				rt->rt_refcnt++;
    275 				goto miss;
    276 			}
    277 			KASSERT(newrt != NULL);
    278 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
    279 				msgtype = RTM_RESOLVE;
    280 				goto miss;
    281 			}
    282 			/* Inform listeners of the new route */
    283 			memset(&info, 0, sizeof(info));
    284 			info.rti_info[RTAX_DST] = rt_key(rt);
    285 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    286 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    287 			if (rt->rt_ifp != NULL) {
    288 				info.rti_info[RTAX_IFP] =
    289 				    TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
    290 				info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
    291 			}
    292 			rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
    293 		} else
    294 			rt->rt_refcnt++;
    295 	} else {
    296 		rtstat.rts_unreach++;
    297 	miss:	if (report) {
    298 			memset((void *)&info, 0, sizeof(info));
    299 			info.rti_info[RTAX_DST] = dst;
    300 			rt_missmsg(msgtype, &info, 0, err);
    301 		}
    302 	}
    303 	splx(s);
    304 	return (newrt);
    305 }
    306 
    307 void
    308 rtfree(struct rtentry *rt)
    309 {
    310 	struct ifaddr *ifa;
    311 
    312 	if (rt == NULL)
    313 		panic("rtfree");
    314 	rt->rt_refcnt--;
    315 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
    316 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    317 			panic ("rtfree 2");
    318 		rttrash--;
    319 		if (rt->rt_refcnt < 0) {
    320 			printf("rtfree: %p not freed (neg refs)\n", rt);
    321 			return;
    322 		}
    323 		rt_timer_remove_all(rt, 0);
    324 		ifa = rt->rt_ifa;
    325 		rt->rt_ifa = NULL;
    326 		IFAFREE(ifa);
    327 		rt->rt_ifp = NULL;
    328 		Free(rt_key(rt));
    329 		pool_put(&rtentry_pool, rt);
    330 	}
    331 }
    332 
    333 void
    334 ifafree(struct ifaddr *ifa)
    335 {
    336 
    337 #ifdef DIAGNOSTIC
    338 	if (ifa == NULL)
    339 		panic("ifafree: null ifa");
    340 	if (ifa->ifa_refcnt != 0)
    341 		panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
    342 #endif
    343 #ifdef IFAREF_DEBUG
    344 	printf("ifafree: freeing ifaddr %p\n", ifa);
    345 #endif
    346 	free(ifa, M_IFADDR);
    347 }
    348 
    349 /*
    350  * Force a routing table entry to the specified
    351  * destination to go through the given gateway.
    352  * Normally called as a result of a routing redirect
    353  * message from the network layer.
    354  *
    355  * N.B.: must be called at splsoftnet
    356  */
    357 void
    358 rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
    359 	const struct sockaddr *netmask, int flags, const struct sockaddr *src,
    360 	struct rtentry **rtp)
    361 {
    362 	struct rtentry *rt;
    363 	int error = 0;
    364 	u_quad_t *stat = NULL;
    365 	struct rt_addrinfo info;
    366 	struct ifaddr *ifa;
    367 
    368 	/* verify the gateway is directly reachable */
    369 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
    370 		error = ENETUNREACH;
    371 		goto out;
    372 	}
    373 	rt = rtalloc1(dst, 0);
    374 	/*
    375 	 * If the redirect isn't from our current router for this dst,
    376 	 * it's either old or wrong.  If it redirects us to ourselves,
    377 	 * we have a routing loop, perhaps as a result of an interface
    378 	 * going down recently.
    379 	 */
    380 #define	equal(a1, a2) \
    381 	((a1)->sa_len == (a2)->sa_len && \
    382 	 memcmp((a1), (a2), (a1)->sa_len) == 0)
    383 	if (!(flags & RTF_DONE) && rt &&
    384 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
    385 		error = EINVAL;
    386 	else if (ifa_ifwithaddr(gateway))
    387 		error = EHOSTUNREACH;
    388 	if (error)
    389 		goto done;
    390 	/*
    391 	 * Create a new entry if we just got back a wildcard entry
    392 	 * or the lookup failed.  This is necessary for hosts
    393 	 * which use routing redirects generated by smart gateways
    394 	 * to dynamically build the routing tables.
    395 	 */
    396 	if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    397 		goto create;
    398 	/*
    399 	 * Don't listen to the redirect if it's
    400 	 * for a route to an interface.
    401 	 */
    402 	if (rt->rt_flags & RTF_GATEWAY) {
    403 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    404 			/*
    405 			 * Changing from route to net => route to host.
    406 			 * Create new route, rather than smashing route to net.
    407 			 */
    408 		create:
    409 			if (rt)
    410 				rtfree(rt);
    411 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    412 			info.rti_info[RTAX_DST] = dst;
    413 			info.rti_info[RTAX_GATEWAY] = gateway;
    414 			info.rti_info[RTAX_NETMASK] = netmask;
    415 			info.rti_ifa = ifa;
    416 			info.rti_flags = flags;
    417 			rt = NULL;
    418 			error = rtrequest1(RTM_ADD, &info, &rt);
    419 			if (rt != NULL)
    420 				flags = rt->rt_flags;
    421 			stat = &rtstat.rts_dynamic;
    422 		} else {
    423 			/*
    424 			 * Smash the current notion of the gateway to
    425 			 * this destination.  Should check about netmask!!!
    426 			 */
    427 			rt->rt_flags |= RTF_MODIFIED;
    428 			flags |= RTF_MODIFIED;
    429 			stat = &rtstat.rts_newgateway;
    430 			rt_setgate(rt, rt_key(rt), gateway);
    431 		}
    432 	} else
    433 		error = EHOSTUNREACH;
    434 done:
    435 	if (rt) {
    436 		if (rtp && !error)
    437 			*rtp = rt;
    438 		else
    439 			rtfree(rt);
    440 	}
    441 out:
    442 	if (error)
    443 		rtstat.rts_badredirect++;
    444 	else if (stat != NULL)
    445 		(*stat)++;
    446 	memset((void *)&info, 0, sizeof(info));
    447 	info.rti_info[RTAX_DST] = dst;
    448 	info.rti_info[RTAX_GATEWAY] = gateway;
    449 	info.rti_info[RTAX_NETMASK] = netmask;
    450 	info.rti_info[RTAX_AUTHOR] = src;
    451 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    452 }
    453 
    454 /*
    455  * Delete a route and generate a message
    456  */
    457 static int
    458 rtdeletemsg(struct rtentry *rt)
    459 {
    460 	int error;
    461 	struct rt_addrinfo info;
    462 
    463 	/*
    464 	 * Request the new route so that the entry is not actually
    465 	 * deleted.  That will allow the information being reported to
    466 	 * be accurate (and consistent with route_output()).
    467 	 */
    468 	memset((void *)&info, 0, sizeof(info));
    469 	info.rti_info[RTAX_DST] = rt_key(rt);
    470 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    471 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    472 	info.rti_flags = rt->rt_flags;
    473 	error = rtrequest1(RTM_DELETE, &info, &rt);
    474 
    475 	rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
    476 
    477 	/* Adjust the refcount */
    478 	if (error == 0 && rt->rt_refcnt <= 0) {
    479 		rt->rt_refcnt++;
    480 		rtfree(rt);
    481 	}
    482 	return (error);
    483 }
    484 
    485 static int
    486 rtflushclone1(struct radix_node *rn, void *arg)
    487 {
    488 	struct rtentry *rt, *parent;
    489 
    490 	rt = (struct rtentry *)rn;
    491 	parent = (struct rtentry *)arg;
    492 	if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
    493 		rtdeletemsg(rt);
    494 	return 0;
    495 }
    496 
    497 static void
    498 rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
    499 {
    500 
    501 #ifdef DIAGNOSTIC
    502 	if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
    503 		panic("rtflushclone: called with a non-cloning route");
    504 	if (!rnh->rnh_walktree)
    505 		panic("rtflushclone: no rnh_walktree");
    506 #endif
    507 	rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
    508 }
    509 
    510 /*
    511  * Routing table ioctl interface.
    512  */
    513 int
    514 rtioctl(u_long req, void *data, struct lwp *l)
    515 {
    516 	return (EOPNOTSUPP);
    517 }
    518 
    519 struct ifaddr *
    520 ifa_ifwithroute(int flags, const struct sockaddr *dst,
    521 	const struct sockaddr *gateway)
    522 {
    523 	struct ifaddr *ifa;
    524 	if ((flags & RTF_GATEWAY) == 0) {
    525 		/*
    526 		 * If we are adding a route to an interface,
    527 		 * and the interface is a pt to pt link
    528 		 * we should search for the destination
    529 		 * as our clue to the interface.  Otherwise
    530 		 * we can use the local address.
    531 		 */
    532 		ifa = NULL;
    533 		if (flags & RTF_HOST)
    534 			ifa = ifa_ifwithdstaddr(dst);
    535 		if (ifa == NULL)
    536 			ifa = ifa_ifwithaddr(gateway);
    537 	} else {
    538 		/*
    539 		 * If we are adding a route to a remote net
    540 		 * or host, the gateway may still be on the
    541 		 * other end of a pt to pt link.
    542 		 */
    543 		ifa = ifa_ifwithdstaddr(gateway);
    544 	}
    545 	if (ifa == NULL)
    546 		ifa = ifa_ifwithnet(gateway);
    547 	if (ifa == NULL) {
    548 		struct rtentry *rt = rtalloc1(dst, 0);
    549 		if (rt == NULL)
    550 			return NULL;
    551 		rt->rt_refcnt--;
    552 		if ((ifa = rt->rt_ifa) == NULL)
    553 			return NULL;
    554 	}
    555 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
    556 		struct ifaddr *oifa = ifa;
    557 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
    558 		if (ifa == 0)
    559 			ifa = oifa;
    560 	}
    561 	return (ifa);
    562 }
    563 
    564 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    565 
    566 int
    567 rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
    568 	const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
    569 {
    570 	struct rt_addrinfo info;
    571 
    572 	memset(&info, 0, sizeof(info));
    573 	info.rti_flags = flags;
    574 	info.rti_info[RTAX_DST] = dst;
    575 	info.rti_info[RTAX_GATEWAY] = gateway;
    576 	info.rti_info[RTAX_NETMASK] = netmask;
    577 	return rtrequest1(req, &info, ret_nrt);
    578 }
    579 
    580 int
    581 rt_getifa(struct rt_addrinfo *info)
    582 {
    583 	struct ifaddr *ifa;
    584 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
    585 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
    586 	const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
    587 	const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
    588 	int flags = info->rti_flags;
    589 
    590 	/*
    591 	 * ifp may be specified by sockaddr_dl when protocol address
    592 	 * is ambiguous
    593 	 */
    594 	if (info->rti_ifp == NULL && ifpaddr != NULL
    595 	    && ifpaddr->sa_family == AF_LINK &&
    596 	    (ifa = ifa_ifwithnet((const struct sockaddr *)ifpaddr)) != NULL)
    597 		info->rti_ifp = ifa->ifa_ifp;
    598 	if (info->rti_ifa == NULL && ifaaddr != NULL)
    599 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
    600 	if (info->rti_ifa == NULL) {
    601 		const struct sockaddr *sa;
    602 
    603 		sa = ifaaddr != NULL ? ifaaddr :
    604 		    (gateway != NULL ? gateway : dst);
    605 		if (sa != NULL && info->rti_ifp != NULL)
    606 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
    607 		else if (dst != NULL && gateway != NULL)
    608 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
    609 		else if (sa != NULL)
    610 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
    611 	}
    612 	if ((ifa = info->rti_ifa) == NULL)
    613 		return ENETUNREACH;
    614 	if (ifa->ifa_getifa != NULL)
    615 		info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
    616 	if (info->rti_ifp == NULL)
    617 		info->rti_ifp = ifa->ifa_ifp;
    618 	return 0;
    619 }
    620 
    621 int
    622 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
    623 {
    624 	int s = splsoftnet();
    625 	int error = 0;
    626 	struct rtentry *rt, *crt;
    627 	struct radix_node *rn;
    628 	struct radix_node_head *rnh;
    629 	struct ifaddr *ifa;
    630 	struct sockaddr_storage deldst;
    631 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
    632 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
    633 	const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
    634 	int flags = info->rti_flags;
    635 #define senderr(x) { error = x ; goto bad; }
    636 
    637 	if ((rnh = rt_tables[dst->sa_family]) == NULL)
    638 		senderr(ESRCH);
    639 	if (flags & RTF_HOST)
    640 		netmask = NULL;
    641 	switch (req) {
    642 	case RTM_DELETE:
    643 		if (netmask) {
    644 			rt_maskedcopy(dst, (struct sockaddr *)&deldst, netmask);
    645 			dst = (struct sockaddr *)&deldst;
    646 		}
    647 		if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
    648 			senderr(ESRCH);
    649 		rt = (struct rtentry *)rn;
    650 		if ((rt->rt_flags & RTF_CLONING) != 0) {
    651 			/* clean up any cloned children */
    652 			rtflushclone(rnh, rt);
    653 		}
    654 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
    655 			senderr(ESRCH);
    656 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    657 			panic ("rtrequest delete");
    658 		rt = (struct rtentry *)rn;
    659 		if (rt->rt_gwroute) {
    660 			RTFREE(rt->rt_gwroute);
    661 			rt->rt_gwroute = NULL;
    662 		}
    663 		if (rt->rt_parent) {
    664 			rt->rt_parent->rt_refcnt--;
    665 			rt->rt_parent = NULL;
    666 		}
    667 		rt->rt_flags &= ~RTF_UP;
    668 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
    669 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
    670 		rttrash++;
    671 		if (ret_nrt)
    672 			*ret_nrt = rt;
    673 		else if (rt->rt_refcnt <= 0) {
    674 			rt->rt_refcnt++;
    675 			rtfree(rt);
    676 		}
    677 		break;
    678 
    679 	case RTM_RESOLVE:
    680 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
    681 			senderr(EINVAL);
    682 		if ((rt->rt_flags & RTF_CLONING) == 0)
    683 			senderr(EINVAL);
    684 		ifa = rt->rt_ifa;
    685 		flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
    686 		flags |= RTF_CLONED;
    687 		gateway = rt->rt_gateway;
    688 		if ((netmask = rt->rt_genmask) == NULL)
    689 			flags |= RTF_HOST;
    690 		goto makeroute;
    691 
    692 	case RTM_ADD:
    693 		if (info->rti_ifa == NULL && (error = rt_getifa(info)))
    694 			senderr(error);
    695 		ifa = info->rti_ifa;
    696 	makeroute:
    697 		/* Already at splsoftnet() so pool_get/pool_put are safe */
    698 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
    699 		if (rt == NULL)
    700 			senderr(ENOBUFS);
    701 		Bzero(rt, sizeof(*rt));
    702 		rt->rt_flags = RTF_UP | flags;
    703 		LIST_INIT(&rt->rt_timer);
    704 		if (rt_setgate(rt, dst, gateway)) {
    705 			pool_put(&rtentry_pool, rt);
    706 			senderr(ENOBUFS);
    707 		}
    708 		if (netmask) {
    709 			rt_maskedcopy(dst, rt_key(rt), netmask);
    710 		} else
    711 			Bcopy(dst, rt_key(rt), dst->sa_len);
    712 		rt_set_ifa(rt, ifa);
    713 		rt->rt_ifp = ifa->ifa_ifp;
    714 		if (req == RTM_RESOLVE) {
    715 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
    716 			rt->rt_parent = *ret_nrt;
    717 			rt->rt_parent->rt_refcnt++;
    718 		}
    719 		rn = rnh->rnh_addaddr(rt_key(rt), netmask, rnh, rt->rt_nodes);
    720 		if (rn == NULL && (crt = rtalloc1(rt_key(rt), 0)) != NULL) {
    721 			/* overwrite cloned route */
    722 			if ((crt->rt_flags & RTF_CLONED) != 0) {
    723 				rtdeletemsg(crt);
    724 				rn = rnh->rnh_addaddr(rt_key(rt),
    725 				    netmask, rnh, rt->rt_nodes);
    726 			}
    727 			RTFREE(crt);
    728 		}
    729 		if (rn == NULL) {
    730 			IFAFREE(ifa);
    731 			if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
    732 				rtfree(rt->rt_parent);
    733 			if (rt->rt_gwroute)
    734 				rtfree(rt->rt_gwroute);
    735 			Free(rt_key(rt));
    736 			pool_put(&rtentry_pool, rt);
    737 			senderr(EEXIST);
    738 		}
    739 		if (ifa->ifa_rtrequest)
    740 			ifa->ifa_rtrequest(req, rt, info);
    741 		if (ret_nrt) {
    742 			*ret_nrt = rt;
    743 			rt->rt_refcnt++;
    744 		}
    745 		if ((rt->rt_flags & RTF_CLONING) != 0) {
    746 			/* clean up any cloned children */
    747 			rtflushclone(rnh, rt);
    748 		}
    749 		rtflushall(dst->sa_family);
    750 		break;
    751 	}
    752 bad:
    753 	splx(s);
    754 	return (error);
    755 }
    756 
    757 int
    758 rt_setgate( struct rtentry *rt0, const struct sockaddr *dst,
    759 	const struct sockaddr *gate)
    760 {
    761 	char *new, *old;
    762 	u_int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
    763 	struct rtentry *rt = rt0;
    764 
    765 	if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
    766 		old = (void *)rt_key(rt);
    767 		R_Malloc(new, void *, dlen + glen);
    768 		if (new == NULL)
    769 			return 1;
    770 		Bzero(new, dlen + glen);
    771 		rt->rt_nodes->rn_key = new;
    772 	} else {
    773 		new = __UNCONST(rt->rt_nodes->rn_key); /*XXXUNCONST*/
    774 		old = NULL;
    775 	}
    776 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
    777 	if (old) {
    778 		Bcopy(dst, new, dlen);
    779 		Free(old);
    780 	}
    781 	if (rt->rt_gwroute) {
    782 		RTFREE(rt->rt_gwroute);
    783 		rt->rt_gwroute = NULL;
    784 	}
    785 	if (rt->rt_flags & RTF_GATEWAY) {
    786 		rt->rt_gwroute = rtalloc1(gate, 1);
    787 		/*
    788 		 * If we switched gateways, grab the MTU from the new
    789 		 * gateway route if the current MTU, if the current MTU is
    790 		 * greater than the MTU of gateway.
    791 		 * Note that, if the MTU of gateway is 0, we will reset the
    792 		 * MTU of the route to run PMTUD again from scratch. XXX
    793 		 */
    794 		if (rt->rt_gwroute
    795 		    && !(rt->rt_rmx.rmx_locks & RTV_MTU)
    796 		    && rt->rt_rmx.rmx_mtu
    797 		    && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
    798 			rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
    799 		}
    800 	}
    801 	return 0;
    802 }
    803 
    804 void
    805 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
    806 	const struct sockaddr *netmask)
    807 {
    808 	const u_char *cp1 = (const u_char *)src;
    809 	u_char *cp2 = (u_char *)dst;
    810 	const u_char *cp3 = (const u_char *)netmask;
    811 	u_char *cplim = cp2 + *cp3;
    812 	u_char *cplim2 = cp2 + *cp1;
    813 
    814 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
    815 	cp3 += 2;
    816 	if (cplim > cplim2)
    817 		cplim = cplim2;
    818 	while (cp2 < cplim)
    819 		*cp2++ = *cp1++ & *cp3++;
    820 	if (cp2 < cplim2)
    821 		memset(cp2, 0, (unsigned)(cplim2 - cp2));
    822 }
    823 
    824 /*
    825  * Set up or tear down a routing table entry, normally
    826  * for an interface.
    827  */
    828 int
    829 rtinit(struct ifaddr *ifa, int cmd, int flags)
    830 {
    831 	struct rtentry *rt;
    832 	struct sockaddr *dst, *odst;
    833 	struct sockaddr_storage deldst;
    834 	struct rtentry *nrt = NULL;
    835 	int error;
    836 	struct rt_addrinfo info;
    837 
    838 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    839 	if (cmd == RTM_DELETE) {
    840 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    841 			/* Delete subnet route for this interface */
    842 			odst = dst;
    843 			dst = (struct sockaddr *)&deldst;
    844 			rt_maskedcopy(odst, dst, ifa->ifa_netmask);
    845 		}
    846 		if ((rt = rtalloc1(dst, 0)) != NULL) {
    847 			rt->rt_refcnt--;
    848 			if (rt->rt_ifa != ifa)
    849 				return (flags & RTF_HOST) ? EHOSTUNREACH
    850 							: ENETUNREACH;
    851 		}
    852 	}
    853 	memset(&info, 0, sizeof(info));
    854 	info.rti_ifa = ifa;
    855 	info.rti_flags = flags | ifa->ifa_flags;
    856 	info.rti_info[RTAX_DST] = dst;
    857 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
    858 	/*
    859 	 * XXX here, it seems that we are assuming that ifa_netmask is NULL
    860 	 * for RTF_HOST.  bsdi4 passes NULL explicitly (via intermediate
    861 	 * variable) when RTF_HOST is 1.  still not sure if i can safely
    862 	 * change it to meet bsdi4 behavior.
    863 	 */
    864 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    865 	error = rtrequest1(cmd, &info, &nrt);
    866 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
    867 		rt_newaddrmsg(cmd, ifa, error, nrt);
    868 		if (rt->rt_refcnt <= 0) {
    869 			rt->rt_refcnt++;
    870 			rtfree(rt);
    871 		}
    872 	}
    873 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
    874 		rt->rt_refcnt--;
    875 		if (rt->rt_ifa != ifa) {
    876 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
    877 				rt->rt_ifa);
    878 			if (rt->rt_ifa->ifa_rtrequest)
    879 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
    880 			rt_replace_ifa(rt, ifa);
    881 			rt->rt_ifp = ifa->ifa_ifp;
    882 			if (ifa->ifa_rtrequest)
    883 				ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
    884 		}
    885 		rt_newaddrmsg(cmd, ifa, error, nrt);
    886 	}
    887 	return error;
    888 }
    889 
    890 /*
    891  * Route timer routines.  These routes allow functions to be called
    892  * for various routes at any time.  This is useful in supporting
    893  * path MTU discovery and redirect route deletion.
    894  *
    895  * This is similar to some BSDI internal functions, but it provides
    896  * for multiple queues for efficiency's sake...
    897  */
    898 
    899 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
    900 static int rt_init_done = 0;
    901 
    902 #define RTTIMER_CALLOUT(r)	do {					\
    903 		if (r->rtt_func != NULL) {				\
    904 			(*r->rtt_func)(r->rtt_rt, r);			\
    905 		} else {						\
    906 			rtrequest((int) RTM_DELETE,			\
    907 				  (struct sockaddr *)rt_key(r->rtt_rt),	\
    908 				  0, 0, 0, 0);				\
    909 		}							\
    910 	} while (/*CONSTCOND*/0)
    911 
    912 /*
    913  * Some subtle order problems with domain initialization mean that
    914  * we cannot count on this being run from rt_init before various
    915  * protocol initializations are done.  Therefore, we make sure
    916  * that this is run when the first queue is added...
    917  */
    918 
    919 void
    920 rt_timer_init(void)
    921 {
    922 	assert(rt_init_done == 0);
    923 
    924 	LIST_INIT(&rttimer_queue_head);
    925 	callout_init(&rt_timer_ch);
    926 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
    927 	rt_init_done = 1;
    928 }
    929 
    930 struct rttimer_queue *
    931 rt_timer_queue_create(u_int timeout)
    932 {
    933 	struct rttimer_queue *rtq;
    934 
    935 	if (rt_init_done == 0)
    936 		rt_timer_init();
    937 
    938 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
    939 	if (rtq == NULL)
    940 		return NULL;
    941 	Bzero(rtq, sizeof *rtq);
    942 
    943 	rtq->rtq_timeout = timeout;
    944 	rtq->rtq_count = 0;
    945 	TAILQ_INIT(&rtq->rtq_head);
    946 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
    947 
    948 	return rtq;
    949 }
    950 
    951 void
    952 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
    953 {
    954 
    955 	rtq->rtq_timeout = timeout;
    956 }
    957 
    958 void
    959 rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
    960 {
    961 	struct rttimer *r;
    962 
    963 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
    964 		LIST_REMOVE(r, rtt_link);
    965 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
    966 		if (destroy)
    967 			RTTIMER_CALLOUT(r);
    968 		/* we are already at splsoftnet */
    969 		pool_put(&rttimer_pool, r);
    970 		if (rtq->rtq_count > 0)
    971 			rtq->rtq_count--;
    972 		else
    973 			printf("rt_timer_queue_remove_all: "
    974 			    "rtq_count reached 0\n");
    975 	}
    976 }
    977 
    978 void
    979 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
    980 {
    981 
    982 	rt_timer_queue_remove_all(rtq, destroy);
    983 
    984 	LIST_REMOVE(rtq, rtq_link);
    985 
    986 	/*
    987 	 * Caller is responsible for freeing the rttimer_queue structure.
    988 	 */
    989 }
    990 
    991 unsigned long
    992 rt_timer_count(struct rttimer_queue *rtq)
    993 {
    994 	return rtq->rtq_count;
    995 }
    996 
    997 void
    998 rt_timer_remove_all(struct rtentry *rt, int destroy)
    999 {
   1000 	struct rttimer *r;
   1001 
   1002 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
   1003 		LIST_REMOVE(r, rtt_link);
   1004 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1005 		if (destroy)
   1006 			RTTIMER_CALLOUT(r);
   1007 		if (r->rtt_queue->rtq_count > 0)
   1008 			r->rtt_queue->rtq_count--;
   1009 		else
   1010 			printf("rt_timer_remove_all: rtq_count reached 0\n");
   1011 		/* we are already at splsoftnet */
   1012 		pool_put(&rttimer_pool, r);
   1013 	}
   1014 }
   1015 
   1016 int
   1017 rt_timer_add(struct rtentry *rt,
   1018 	void (*func)(struct rtentry *, struct rttimer *),
   1019 	struct rttimer_queue *queue)
   1020 {
   1021 	struct rttimer *r;
   1022 	int s;
   1023 
   1024 	/*
   1025 	 * If there's already a timer with this action, destroy it before
   1026 	 * we add a new one.
   1027 	 */
   1028 	LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
   1029 		if (r->rtt_func == func)
   1030 			break;
   1031 	}
   1032 	if (r != NULL) {
   1033 		LIST_REMOVE(r, rtt_link);
   1034 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1035 		if (r->rtt_queue->rtq_count > 0)
   1036 			r->rtt_queue->rtq_count--;
   1037 		else
   1038 			printf("rt_timer_add: rtq_count reached 0\n");
   1039 	} else {
   1040 		s = splsoftnet();
   1041 		r = pool_get(&rttimer_pool, PR_NOWAIT);
   1042 		splx(s);
   1043 		if (r == NULL)
   1044 			return ENOBUFS;
   1045 	}
   1046 
   1047 	memset(r, 0, sizeof(*r));
   1048 
   1049 	r->rtt_rt = rt;
   1050 	r->rtt_time = time_uptime;
   1051 	r->rtt_func = func;
   1052 	r->rtt_queue = queue;
   1053 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
   1054 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
   1055 	r->rtt_queue->rtq_count++;
   1056 
   1057 	return (0);
   1058 }
   1059 
   1060 /* ARGSUSED */
   1061 void
   1062 rt_timer_timer(void *arg)
   1063 {
   1064 	struct rttimer_queue *rtq;
   1065 	struct rttimer *r;
   1066 	int s;
   1067 
   1068 	s = splsoftnet();
   1069 	LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
   1070 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
   1071 		    (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
   1072 			LIST_REMOVE(r, rtt_link);
   1073 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
   1074 			RTTIMER_CALLOUT(r);
   1075 			pool_put(&rttimer_pool, r);
   1076 			if (rtq->rtq_count > 0)
   1077 				rtq->rtq_count--;
   1078 			else
   1079 				printf("rt_timer_timer: rtq_count reached 0\n");
   1080 		}
   1081 	}
   1082 	splx(s);
   1083 
   1084 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
   1085 }
   1086 
   1087 #ifdef RTCACHE_DEBUG
   1088 #ifndef	RTCACHE_DEBUG_SIZE
   1089 #define	RTCACHE_DEBUG_SIZE (1024 * 1024)
   1090 #endif
   1091 static const char *cache_caller[RTCACHE_DEBUG_SIZE];
   1092 static struct route *cache_entry[RTCACHE_DEBUG_SIZE];
   1093 size_t cache_cur;
   1094 #endif
   1095 
   1096 #ifdef RTCACHE_DEBUG
   1097 static void
   1098 _rtcache_init_debug(const char *caller, struct route *ro, int flag)
   1099 #else
   1100 static void
   1101 _rtcache_init(struct route *ro, int flag)
   1102 #endif
   1103 {
   1104 #ifdef RTCACHE_DEBUG
   1105 	size_t i;
   1106 	for (i = 0; i < cache_cur; ++i) {
   1107 		if (cache_entry[i] == ro)
   1108 			panic("Reinit of route %p, initialised from %s", ro, cache_caller[i]);
   1109 	}
   1110 #endif
   1111 
   1112 	ro->ro_rt = rtalloc1(rtcache_getdst(ro), flag);
   1113 	if (ro->ro_rt != NULL) {
   1114 #ifdef RTCACHE_DEBUG
   1115 		if (cache_cur == RTCACHE_DEBUG_SIZE)
   1116 			panic("Route cache debug overflow");
   1117 		cache_caller[cache_cur] = caller;
   1118 		cache_entry[cache_cur] = ro;
   1119 		++cache_cur;
   1120 #endif
   1121 		rtcache(ro);
   1122 	}
   1123 }
   1124 
   1125 #ifdef RTCACHE_DEBUG
   1126 void
   1127 rtcache_init_debug(const char *caller, struct route *ro)
   1128 {
   1129 	_rtcache_init_debug(caller, ro, 1);
   1130 }
   1131 
   1132 void
   1133 rtcache_init_noclone_debug(const char *caller, struct route *ro)
   1134 {
   1135 	_rtcache_init_debug(caller, ro, 0);
   1136 }
   1137 
   1138 #else
   1139 void
   1140 rtcache_init(struct route *ro)
   1141 {
   1142 	_rtcache_init(ro, 1);
   1143 }
   1144 
   1145 void
   1146 rtcache_init_noclone(struct route *ro)
   1147 {
   1148 	_rtcache_init(ro, 0);
   1149 }
   1150 #endif
   1151 
   1152 #ifdef RTCACHE_DEBUG
   1153 void
   1154 rtcache_copy_debug(const char *caller, struct route *new_ro, const struct route *old_ro, size_t new_len)
   1155 #else
   1156 void
   1157 rtcache_copy(struct route *new_ro, const struct route *old_ro, size_t new_len)
   1158 #endif
   1159 {
   1160 #ifdef RTCACHE_DEBUG
   1161 	size_t i;
   1162 
   1163 	for (i = 0; i < cache_cur; ++i) {
   1164 		if (cache_entry[i] == new_ro)
   1165 			panic("Copy to initalised route %p (before %s)", new_ro, cache_caller[i]);
   1166 	}
   1167 #endif
   1168 
   1169 	memset(new_ro, 0, new_len);
   1170 #if 0
   1171 	if (old_ro->ro_sa != NULL)
   1172 		new_ro->ro_sa = sockaddr_dup(old_ro->ro_sa);
   1173 #else
   1174 	if (old_ro->ro_dst.sa_len + offsetof(struct route, ro_dst) > new_len)
   1175 		panic("rtcache_copy: dst address will overflow new route");
   1176 	memcpy(&new_ro->ro_dst, &old_ro->ro_dst, old_ro->ro_dst.sa_len);
   1177 #endif
   1178 	new_ro->ro_rt = old_ro->ro_rt;
   1179 	if (new_ro->ro_rt != NULL) {
   1180 #ifdef RTCACHE_DEBUG
   1181 		if (cache_cur == RTCACHE_DEBUG_SIZE)
   1182 			panic("Route cache debug overflow");
   1183 		cache_caller[cache_cur] = caller;
   1184 		cache_entry[cache_cur] = new_ro;
   1185 		++cache_cur;
   1186 #endif
   1187 		rtcache(new_ro);
   1188 		++new_ro->ro_rt->rt_refcnt;
   1189 	}
   1190 }
   1191 
   1192 void
   1193 rtcache_clear(struct route *ro)
   1194 {
   1195 #ifdef RTCACHE_DEBUG
   1196 	size_t j, i = cache_cur;
   1197 	for (i = j = 0; i < cache_cur; ++i, ++j) {
   1198 		if (cache_entry[i] == ro) {
   1199 			if (ro->ro_rt == NULL)
   1200 				panic("Route cache manipulated (allocated by %s)", cache_caller[i]);
   1201 			--j;
   1202 		} else {
   1203 			cache_caller[j] = cache_caller[i];
   1204 			cache_entry[j] = cache_entry[i];
   1205 		}
   1206 	}
   1207 	if (ro->ro_rt != NULL) {
   1208 		if (i != j + 1)
   1209 			panic("Wrong entries after rtcache_free: %zu (expected %zu)", j, i - 1);
   1210 		--cache_cur;
   1211 	}
   1212 #endif
   1213 
   1214 	if (ro->ro_rt != NULL)
   1215 		rtflush(ro);
   1216 	ro->ro_rt = NULL;
   1217 }
   1218 
   1219 void
   1220 rtcache_free(struct route *ro)
   1221 {
   1222 	rtcache_clear(ro);
   1223 #if 0
   1224 	if (ro->ro_sa != NULL) {
   1225 		sockaddr_free(ro->ro_sa);
   1226 		ro->ro_sa = NULL;
   1227 	}
   1228 #endif
   1229 }
   1230 
   1231 void
   1232 rtcache_update(struct route *ro)
   1233 {
   1234 	rtcache_clear(ro);
   1235 	rtcache_init(ro);
   1236 }
   1237