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