Home | History | Annotate | Line # | Download | only in net
route.c revision 1.25
      1 /*	$NetBSD: route.c,v 1.25 1999/07/01 08:12:49 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/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 #define	SA(p) ((struct sockaddr *)(p))
    130 
    131 int	rttrash;		/* routes not in table but not freed */
    132 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
    133 
    134 struct pool rtentry_pool;	/* pool for rtentry structures */
    135 struct pool rttimer_pool;	/* pool for rttimer structures */
    136 
    137 void
    138 rtable_init(table)
    139 	void **table;
    140 {
    141 	struct domain *dom;
    142 	for (dom = domains; dom; dom = dom->dom_next)
    143 		if (dom->dom_rtattach)
    144 			dom->dom_rtattach(&table[dom->dom_family],
    145 			    dom->dom_rtoffset);
    146 }
    147 
    148 void
    149 route_init()
    150 {
    151 
    152 	pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
    153 	    0, NULL, NULL, M_RTABLE);
    154 
    155 	rn_init();	/* initialize all zeroes, all ones, mask table */
    156 	rtable_init((void **)rt_tables);
    157 }
    158 
    159 /*
    160  * Packet routing routines.
    161  */
    162 void
    163 rtalloc(ro)
    164 	register struct route *ro;
    165 {
    166 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
    167 		return;				 /* XXX */
    168 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
    169 }
    170 
    171 #if 1
    172 /* for INET6 */
    173 void
    174 rtcalloc(ro)
    175 	register struct route *ro;
    176 {
    177 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
    178 		return;				 /* XXX */
    179 	ro->ro_rt = rtalloc1(&ro->ro_dst, 0);
    180 }
    181 #endif
    182 
    183 struct rtentry *
    184 rtalloc1(dst, report)
    185 	register struct sockaddr *dst;
    186 	int report;
    187 {
    188 	register struct radix_node_head *rnh = rt_tables[dst->sa_family];
    189 	register struct rtentry *rt;
    190 	register struct radix_node *rn;
    191 	struct rtentry *newrt = 0;
    192 	struct rt_addrinfo info;
    193 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
    194 
    195 	if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
    196 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
    197 		newrt = rt = (struct rtentry *)rn;
    198 		if (report && (rt->rt_flags & RTF_CLONING)) {
    199 			err = rtrequest(RTM_RESOLVE, dst, SA(0),
    200 					      SA(0), 0, &newrt);
    201 			if (err) {
    202 				newrt = rt;
    203 				rt->rt_refcnt++;
    204 				goto miss;
    205 			}
    206 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
    207 				msgtype = RTM_RESOLVE;
    208 				goto miss;
    209 			}
    210 		} else
    211 			rt->rt_refcnt++;
    212 	} else {
    213 		rtstat.rts_unreach++;
    214 	miss:	if (report) {
    215 			bzero((caddr_t)&info, 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(rt)
    226 	register struct rtentry *rt;
    227 {
    228 	register struct ifaddr *ifa;
    229 
    230 	if (rt == 0)
    231 		panic("rtfree");
    232 	rt->rt_refcnt--;
    233 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
    234 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    235 			panic ("rtfree 2");
    236 		rttrash--;
    237 		if (rt->rt_refcnt < 0) {
    238 			printf("rtfree: %p not freed (neg refs)\n", rt);
    239 			return;
    240 		}
    241 		rt_timer_remove_all(rt);
    242 		ifa = rt->rt_ifa;
    243 		IFAFREE(ifa);
    244 		Free(rt_key(rt));
    245 		pool_put(&rtentry_pool, rt);
    246 	}
    247 }
    248 
    249 void
    250 ifafree(ifa)
    251 	register struct ifaddr *ifa;
    252 {
    253 	if (ifa == NULL)
    254 		panic("ifafree");
    255 	if (ifa->ifa_refcnt == 0)
    256 		free(ifa, M_IFADDR);
    257 	else
    258 		ifa->ifa_refcnt--;
    259 }
    260 
    261 /*
    262  * Force a routing table entry to the specified
    263  * destination to go through the given gateway.
    264  * Normally called as a result of a routing redirect
    265  * message from the network layer.
    266  *
    267  * N.B.: must be called at splsoftnet
    268  */
    269 void
    270 rtredirect(dst, gateway, netmask, flags, src, rtp)
    271 	struct sockaddr *dst, *gateway, *netmask, *src;
    272 	int flags;
    273 	struct rtentry **rtp;
    274 {
    275 	register struct rtentry *rt;
    276 	int error = 0;
    277 	short *stat = 0;
    278 	struct rt_addrinfo info;
    279 	struct ifaddr *ifa;
    280 
    281 	/* verify the gateway is directly reachable */
    282 	if ((ifa = ifa_ifwithnet(gateway)) == 0) {
    283 		error = ENETUNREACH;
    284 		goto out;
    285 	}
    286 	rt = rtalloc1(dst, 0);
    287 	/*
    288 	 * If the redirect isn't from our current router for this dst,
    289 	 * it's either old or wrong.  If it redirects us to ourselves,
    290 	 * we have a routing loop, perhaps as a result of an interface
    291 	 * going down recently.
    292 	 */
    293 #define	equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
    294 	if (!(flags & RTF_DONE) && rt &&
    295 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
    296 		error = EINVAL;
    297 	else if (ifa_ifwithaddr(gateway))
    298 		error = EHOSTUNREACH;
    299 	if (error)
    300 		goto done;
    301 	/*
    302 	 * Create a new entry if we just got back a wildcard entry
    303 	 * or the the lookup failed.  This is necessary for hosts
    304 	 * which use routing redirects generated by smart gateways
    305 	 * to dynamically build the routing tables.
    306 	 */
    307 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    308 		goto create;
    309 	/*
    310 	 * Don't listen to the redirect if it's
    311 	 * for a route to an interface.
    312 	 */
    313 	if (rt->rt_flags & RTF_GATEWAY) {
    314 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    315 			/*
    316 			 * Changing from route to net => route to host.
    317 			 * Create new route, rather than smashing route to net.
    318 			 */
    319 		create:
    320 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    321 			error = rtrequest((int)RTM_ADD, dst, gateway,
    322 				    netmask, flags,
    323 				    (struct rtentry **)0);
    324 			stat = &rtstat.rts_dynamic;
    325 		} else {
    326 			/*
    327 			 * Smash the current notion of the gateway to
    328 			 * this destination.  Should check about netmask!!!
    329 			 */
    330 			rt->rt_flags |= RTF_MODIFIED;
    331 			flags |= RTF_MODIFIED;
    332 			stat = &rtstat.rts_newgateway;
    333 			rt_setgate(rt, rt_key(rt), gateway);
    334 		}
    335 	} else
    336 		error = EHOSTUNREACH;
    337 done:
    338 	if (rt) {
    339 		if (rtp && !error)
    340 			*rtp = rt;
    341 		else
    342 			rtfree(rt);
    343 	}
    344 out:
    345 	if (error)
    346 		rtstat.rts_badredirect++;
    347 	else if (stat != NULL)
    348 		(*stat)++;
    349 	bzero((caddr_t)&info, sizeof(info));
    350 	info.rti_info[RTAX_DST] = dst;
    351 	info.rti_info[RTAX_GATEWAY] = gateway;
    352 	info.rti_info[RTAX_NETMASK] = netmask;
    353 	info.rti_info[RTAX_AUTHOR] = src;
    354 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    355 }
    356 
    357 /*
    358  * Routing table ioctl interface.
    359  */
    360 int
    361 rtioctl(req, data, p)
    362 	u_long req;
    363 	caddr_t data;
    364 	struct proc *p;
    365 {
    366 	return (EOPNOTSUPP);
    367 }
    368 
    369 struct ifaddr *
    370 ifa_ifwithroute(flags, dst, gateway)
    371 	int flags;
    372 	struct sockaddr	*dst, *gateway;
    373 {
    374 	register struct ifaddr *ifa;
    375 	if ((flags & RTF_GATEWAY) == 0) {
    376 		/*
    377 		 * If we are adding a route to an interface,
    378 		 * and the interface is a pt to pt link
    379 		 * we should search for the destination
    380 		 * as our clue to the interface.  Otherwise
    381 		 * we can use the local address.
    382 		 */
    383 		ifa = 0;
    384 		if (flags & RTF_HOST)
    385 			ifa = ifa_ifwithdstaddr(dst);
    386 		if (ifa == 0)
    387 			ifa = ifa_ifwithaddr(gateway);
    388 	} else {
    389 		/*
    390 		 * If we are adding a route to a remote net
    391 		 * or host, the gateway may still be on the
    392 		 * other end of a pt to pt link.
    393 		 */
    394 		ifa = ifa_ifwithdstaddr(gateway);
    395 	}
    396 	if (ifa == 0)
    397 		ifa = ifa_ifwithnet(gateway);
    398 	if (ifa == 0) {
    399 		struct rtentry *rt = rtalloc1(dst, 0);
    400 		if (rt == 0)
    401 			return (0);
    402 		rt->rt_refcnt--;
    403 		if ((ifa = rt->rt_ifa) == 0)
    404 			return (0);
    405 	}
    406 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
    407 		struct ifaddr *oifa = ifa;
    408 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
    409 		if (ifa == 0)
    410 			ifa = oifa;
    411 	}
    412 	return (ifa);
    413 }
    414 
    415 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    416 
    417 int
    418 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
    419 	int req, flags;
    420 	struct sockaddr *dst, *gateway, *netmask;
    421 	struct rtentry **ret_nrt;
    422 {
    423 	int s = splsoftnet(); int error = 0;
    424 	register struct rtentry *rt;
    425 	register struct radix_node *rn;
    426 	register struct radix_node_head *rnh;
    427 	struct ifaddr *ifa;
    428 	struct sockaddr *ndst;
    429 #define senderr(x) { error = x ; goto bad; }
    430 
    431 	if ((rnh = rt_tables[dst->sa_family]) == 0)
    432 		senderr(ESRCH);
    433 	if (flags & RTF_HOST)
    434 		netmask = 0;
    435 	switch (req) {
    436 	case RTM_DELETE:
    437 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
    438 			senderr(ESRCH);
    439 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    440 			panic ("rtrequest delete");
    441 		rt = (struct rtentry *)rn;
    442 		rt->rt_flags &= ~RTF_UP;
    443 		if (rt->rt_gwroute) {
    444 			rt = rt->rt_gwroute; RTFREE(rt);
    445 			(rt = (struct rtentry *)rn)->rt_gwroute = 0;
    446 		}
    447 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
    448 			ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    449 		rttrash++;
    450 		if (ret_nrt)
    451 			*ret_nrt = rt;
    452 		else if (rt->rt_refcnt <= 0) {
    453 			rt->rt_refcnt++;
    454 			rtfree(rt);
    455 		}
    456 		break;
    457 
    458 	case RTM_RESOLVE:
    459 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
    460 			senderr(EINVAL);
    461 		ifa = rt->rt_ifa;
    462 		flags = rt->rt_flags & ~RTF_CLONING;
    463 		gateway = rt->rt_gateway;
    464 		if ((netmask = rt->rt_genmask) == 0)
    465 			flags |= RTF_HOST;
    466 		goto makeroute;
    467 
    468 	case RTM_ADD:
    469 		if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
    470 			senderr(ENETUNREACH);
    471 
    472 		/* The interface found in the previous statement may
    473 		 * be overridden later by rt_setif.  See the code
    474 		 * for case RTM_ADD in rtsock.c:route_output.
    475 		 */
    476 	makeroute:
    477 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
    478 		if (rt == 0)
    479 			senderr(ENOBUFS);
    480 		Bzero(rt, sizeof(*rt));
    481 		rt->rt_flags = RTF_UP | flags;
    482 		LIST_INIT(&rt->rt_timer);
    483 		if (rt_setgate(rt, dst, gateway)) {
    484 			pool_put(&rtentry_pool, rt);
    485 			senderr(ENOBUFS);
    486 		}
    487 		ndst = rt_key(rt);
    488 		if (netmask) {
    489 			rt_maskedcopy(dst, ndst, netmask);
    490 		} else
    491 			Bcopy(dst, ndst, dst->sa_len);
    492 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { /* XXX */
    493   rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu;
    494 }
    495 		rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
    496 					rnh, rt->rt_nodes);
    497 		if (rn == 0) {
    498 			if (rt->rt_gwroute)
    499 				rtfree(rt->rt_gwroute);
    500 			Free(rt_key(rt));
    501 			pool_put(&rtentry_pool, rt);
    502 			senderr(EEXIST);
    503 		}
    504 		ifa->ifa_refcnt++;
    505 		rt->rt_ifa = ifa;
    506 		rt->rt_ifp = ifa->ifa_ifp;
    507 		if (req == RTM_RESOLVE)
    508 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
    509 		if (ifa->ifa_rtrequest)
    510 			ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
    511 		if (ret_nrt) {
    512 			*ret_nrt = rt;
    513 			rt->rt_refcnt++;
    514 		}
    515 		break;
    516 	}
    517 bad:
    518 	splx(s);
    519 	return (error);
    520 }
    521 
    522 int
    523 rt_setgate(rt0, dst, gate)
    524 	struct rtentry *rt0;
    525 	struct sockaddr *dst, *gate;
    526 {
    527 	caddr_t new, old;
    528 	int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
    529 	register struct rtentry *rt = rt0;
    530 
    531 	if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
    532 		old = (caddr_t)rt_key(rt);
    533 		R_Malloc(new, caddr_t, dlen + glen);
    534 		if (new == 0)
    535 			return 1;
    536 		rt->rt_nodes->rn_key = new;
    537 	} else {
    538 		new = rt->rt_nodes->rn_key;
    539 		old = 0;
    540 	}
    541 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
    542 	if (old) {
    543 		Bcopy(dst, new, dlen);
    544 		Free(old);
    545 	}
    546 	if (rt->rt_gwroute) {
    547 		rt = rt->rt_gwroute; RTFREE(rt);
    548 		rt = rt0; rt->rt_gwroute = 0;
    549 	}
    550 	if (rt->rt_flags & RTF_GATEWAY) {
    551 		rt->rt_gwroute = rtalloc1(gate, 1);
    552 	}
    553 	return 0;
    554 }
    555 
    556 void
    557 rt_maskedcopy(src, dst, netmask)
    558 	struct sockaddr *src, *dst, *netmask;
    559 {
    560 	register u_char *cp1 = (u_char *)src;
    561 	register u_char *cp2 = (u_char *)dst;
    562 	register u_char *cp3 = (u_char *)netmask;
    563 	u_char *cplim = cp2 + *cp3;
    564 	u_char *cplim2 = cp2 + *cp1;
    565 
    566 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
    567 	cp3 += 2;
    568 	if (cplim > cplim2)
    569 		cplim = cplim2;
    570 	while (cp2 < cplim)
    571 		*cp2++ = *cp1++ & *cp3++;
    572 	if (cp2 < cplim2)
    573 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
    574 }
    575 
    576 /*
    577  * Set up a routing table entry, normally
    578  * for an interface.
    579  */
    580 int
    581 rtinit(ifa, cmd, flags)
    582 	register struct ifaddr *ifa;
    583 	int cmd, flags;
    584 {
    585 	register struct rtentry *rt;
    586 	register struct sockaddr *dst;
    587 	register struct sockaddr *deldst;
    588 	struct mbuf *m = 0;
    589 	struct rtentry *nrt = 0;
    590 	int error;
    591 
    592 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    593 	if (cmd == RTM_DELETE) {
    594 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    595 			m = m_get(M_WAIT, MT_SONAME);
    596 			deldst = mtod(m, struct sockaddr *);
    597 			rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
    598 			dst = deldst;
    599 		}
    600 		if ((rt = rtalloc1(dst, 0)) != NULL) {
    601 			rt->rt_refcnt--;
    602 			if (rt->rt_ifa != ifa) {
    603 				if (m)
    604 					(void) m_free(m);
    605 				return (flags & RTF_HOST ? EHOSTUNREACH
    606 							: ENETUNREACH);
    607 			}
    608 		}
    609 	}
    610 	error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
    611 			flags | ifa->ifa_flags, &nrt);
    612 	if (m)
    613 		(void) m_free(m);
    614 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
    615 		rt_newaddrmsg(cmd, ifa, error, nrt);
    616 		if (rt->rt_refcnt <= 0) {
    617 			rt->rt_refcnt++;
    618 			rtfree(rt);
    619 		}
    620 	}
    621 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
    622 		rt->rt_refcnt--;
    623 		if (rt->rt_ifa != ifa) {
    624 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
    625 				rt->rt_ifa);
    626 			if (rt->rt_ifa->ifa_rtrequest)
    627 			    rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    628 			IFAFREE(rt->rt_ifa);
    629 			rt->rt_ifa = ifa;
    630 			rt->rt_ifp = ifa->ifa_ifp;
    631 			rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu;	/*XXX*/
    632 			ifa->ifa_refcnt++;
    633 			if (ifa->ifa_rtrequest)
    634 			    ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
    635 		}
    636 		rt_newaddrmsg(cmd, ifa, error, nrt);
    637 	}
    638 	return (error);
    639 }
    640 
    641 /*
    642  * Route timer routines.  These routes allow functions to be called
    643  * for various routes at any time.  This is useful in supporting
    644  * path MTU discovery and redirect route deletion.
    645  *
    646  * This is similar to some BSDI internal functions, but it provides
    647  * for multiple queues for efficiency's sake...
    648  */
    649 
    650 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
    651 static int rt_init_done = 0;
    652 
    653 #define RTTIMER_CALLOUT(r)	{				\
    654 	if (r->rtt_func != NULL) {				\
    655 		(*r->rtt_func)(r->rtt_rt, r);			\
    656 	} else {						\
    657 		rtrequest((int) RTM_DELETE,			\
    658 			  (struct sockaddr *)rt_key(r->rtt_rt),	\
    659 			  0, 0, 0, 0);				\
    660 	}							\
    661 }
    662 
    663 /*
    664  * Some subtle order problems with domain initialization mean that
    665  * we cannot count on this being run from rt_init before various
    666  * protocol initializations are done.  Therefore, we make sure
    667  * that this is run when the first queue is added...
    668  */
    669 
    670 void
    671 rt_timer_init()
    672 {
    673 	assert(rt_init_done == 0);
    674 
    675 	pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
    676 	    0, NULL, NULL, M_RTABLE);
    677 
    678 	LIST_INIT(&rttimer_queue_head);
    679 	timeout(rt_timer_timer, NULL, hz);  /* every second */
    680 	rt_init_done = 1;
    681 }
    682 
    683 struct rttimer_queue *
    684 rt_timer_queue_create(timeout)
    685 	u_int	timeout;
    686 {
    687 	struct rttimer_queue *rtq;
    688 
    689 	if (rt_init_done == 0)
    690 		rt_timer_init();
    691 
    692 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
    693 	if (rtq == NULL)
    694 		return (NULL);
    695 
    696 	rtq->rtq_timeout = timeout;
    697 	TAILQ_INIT(&rtq->rtq_head);
    698 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
    699 
    700 	return (rtq);
    701 }
    702 
    703 void
    704 rt_timer_queue_change(rtq, timeout)
    705 	struct rttimer_queue *rtq;
    706 	long timeout;
    707 {
    708 
    709 	rtq->rtq_timeout = timeout;
    710 }
    711 
    712 
    713 void
    714 rt_timer_queue_destroy(rtq, destroy)
    715 	struct rttimer_queue *rtq;
    716 	int destroy;
    717 {
    718 	struct rttimer *r;
    719 
    720 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
    721 		LIST_REMOVE(r, rtt_link);
    722 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
    723 		if (destroy)
    724 			RTTIMER_CALLOUT(r);
    725 		pool_put(&rttimer_pool, r);
    726 	}
    727 
    728 	LIST_REMOVE(rtq, rtq_link);
    729 
    730 	/*
    731 	 * Caller is responsible for freeing the rttimer_queue structure.
    732 	 */
    733 }
    734 
    735 void
    736 rt_timer_remove_all(rt)
    737 	struct rtentry *rt;
    738 {
    739 	struct rttimer *r;
    740 
    741 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
    742 		LIST_REMOVE(r, rtt_link);
    743 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
    744 		pool_put(&rttimer_pool, r);
    745 	}
    746 }
    747 
    748 int
    749 rt_timer_add(rt, func, queue)
    750 	struct rtentry *rt;
    751 	void(*func) __P((struct rtentry *, struct rttimer *));
    752 	struct rttimer_queue *queue;
    753 {
    754 	struct rttimer *r;
    755 	long current_time;
    756 	int s;
    757 
    758 	s = splclock();
    759 	current_time = mono_time.tv_sec;
    760 	splx(s);
    761 
    762 	/*
    763 	 * If there's already a timer with this action, destroy it before
    764 	 * we add a new one.
    765 	 */
    766 	for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
    767 	     r = LIST_NEXT(r, rtt_link)) {
    768 		if (r->rtt_func == func) {
    769 			LIST_REMOVE(r, rtt_link);
    770 			TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
    771 			pool_put(&rttimer_pool, r);
    772 			break;  /* only one per list, so we can quit... */
    773 		}
    774 	}
    775 
    776 	r = pool_get(&rttimer_pool, PR_NOWAIT);
    777 	if (r == NULL)
    778 		return (ENOBUFS);
    779 
    780 	r->rtt_rt = rt;
    781 	r->rtt_time = current_time;
    782 	r->rtt_func = func;
    783 	r->rtt_queue = queue;
    784 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
    785 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
    786 
    787 	return (0);
    788 }
    789 
    790 /* ARGSUSED */
    791 void
    792 rt_timer_timer(arg)
    793 	void *arg;
    794 {
    795 	struct rttimer_queue *rtq;
    796 	struct rttimer *r;
    797 	long current_time;
    798 	int s;
    799 
    800 	s = splclock();
    801 	current_time = mono_time.tv_sec;
    802 	splx(s);
    803 
    804 	s = splsoftnet();
    805 	for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
    806 	     rtq = LIST_NEXT(rtq, rtq_link)) {
    807 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
    808 		    (r->rtt_time + rtq->rtq_timeout) < current_time) {
    809 			LIST_REMOVE(r, rtt_link);
    810 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
    811 			RTTIMER_CALLOUT(r);
    812 			pool_put(&rttimer_pool, r);
    813 		}
    814 	}
    815 	splx(s);
    816 
    817 	timeout(rt_timer_timer, NULL, hz);  /* every second */
    818 }
    819