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