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