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