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