Home | History | Annotate | Line # | Download | only in net
route.c revision 1.18
      1  1.18       kml /*	$NetBSD: route.c,v 1.18 1998/04/29 03:41:49 kml 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.10   mycroft  * Copyright (c) 1980, 1986, 1991, 1993
     42  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
     43   1.1       cgd  *
     44   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45   1.1       cgd  * modification, are permitted provided that the following conditions
     46   1.1       cgd  * are met:
     47   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     53   1.1       cgd  *    must display the following acknowledgement:
     54   1.1       cgd  *	This product includes software developed by the University of
     55   1.1       cgd  *	California, Berkeley and its contributors.
     56   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     57   1.1       cgd  *    may be used to endorse or promote products derived from this software
     58   1.1       cgd  *    without specific prior written permission.
     59   1.1       cgd  *
     60   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70   1.1       cgd  * SUCH DAMAGE.
     71   1.1       cgd  *
     72  1.17  christos  *	@(#)route.c	8.3 (Berkeley) 1/9/95
     73   1.1       cgd  */
     74   1.2       cgd 
     75   1.5   mycroft #include <sys/param.h>
     76   1.5   mycroft #include <sys/systm.h>
     77   1.5   mycroft #include <sys/proc.h>
     78   1.5   mycroft #include <sys/mbuf.h>
     79   1.5   mycroft #include <sys/socket.h>
     80   1.5   mycroft #include <sys/socketvar.h>
     81   1.5   mycroft #include <sys/domain.h>
     82   1.5   mycroft #include <sys/protosw.h>
     83  1.18       kml #include <sys/kernel.h>
     84   1.5   mycroft #include <sys/ioctl.h>
     85   1.1       cgd 
     86   1.5   mycroft #include <net/if.h>
     87   1.5   mycroft #include <net/route.h>
     88   1.5   mycroft #include <net/raw_cb.h>
     89   1.1       cgd 
     90   1.5   mycroft #include <netinet/in.h>
     91   1.5   mycroft #include <netinet/in_var.h>
     92   1.1       cgd 
     93   1.1       cgd #ifdef NS
     94   1.5   mycroft #include <netns/ns.h>
     95   1.1       cgd #endif
     96   1.5   mycroft 
     97   1.1       cgd #define	SA(p) ((struct sockaddr *)(p))
     98   1.1       cgd 
     99   1.1       cgd int	rttrash;		/* routes not in table but not freed */
    100   1.1       cgd struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
    101   1.1       cgd 
    102  1.10   mycroft void
    103  1.10   mycroft rtable_init(table)
    104  1.10   mycroft 	void **table;
    105  1.10   mycroft {
    106  1.10   mycroft 	struct domain *dom;
    107  1.10   mycroft 	for (dom = domains; dom; dom = dom->dom_next)
    108  1.10   mycroft 		if (dom->dom_rtattach)
    109  1.10   mycroft 			dom->dom_rtattach(&table[dom->dom_family],
    110  1.10   mycroft 			    dom->dom_rtoffset);
    111  1.10   mycroft }
    112   1.1       cgd 
    113   1.9   mycroft void
    114  1.10   mycroft route_init()
    115   1.1       cgd {
    116  1.10   mycroft 	rn_init();	/* initialize all zeroes, all ones, mask table */
    117  1.10   mycroft 	rtable_init((void **)rt_tables);
    118   1.1       cgd }
    119   1.1       cgd 
    120   1.1       cgd /*
    121   1.1       cgd  * Packet routing routines.
    122   1.1       cgd  */
    123   1.9   mycroft void
    124   1.1       cgd rtalloc(ro)
    125   1.1       cgd 	register struct route *ro;
    126   1.1       cgd {
    127   1.1       cgd 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
    128   1.1       cgd 		return;				 /* XXX */
    129   1.1       cgd 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
    130   1.1       cgd }
    131   1.1       cgd 
    132   1.1       cgd struct rtentry *
    133   1.1       cgd rtalloc1(dst, report)
    134   1.1       cgd 	register struct sockaddr *dst;
    135  1.10   mycroft 	int report;
    136   1.1       cgd {
    137  1.10   mycroft 	register struct radix_node_head *rnh = rt_tables[dst->sa_family];
    138   1.1       cgd 	register struct rtentry *rt;
    139   1.1       cgd 	register struct radix_node *rn;
    140   1.1       cgd 	struct rtentry *newrt = 0;
    141  1.10   mycroft 	struct rt_addrinfo info;
    142  1.13   mycroft 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
    143   1.1       cgd 
    144  1.10   mycroft 	if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
    145   1.1       cgd 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
    146   1.1       cgd 		newrt = rt = (struct rtentry *)rn;
    147   1.1       cgd 		if (report && (rt->rt_flags & RTF_CLONING)) {
    148   1.8       cgd 			err = rtrequest(RTM_RESOLVE, dst, SA(0),
    149  1.10   mycroft 					      SA(0), 0, &newrt);
    150   1.8       cgd 			if (err) {
    151   1.8       cgd 				newrt = rt;
    152   1.8       cgd 				rt->rt_refcnt++;
    153   1.8       cgd 				goto miss;
    154   1.8       cgd 			}
    155   1.8       cgd 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
    156   1.8       cgd 				msgtype = RTM_RESOLVE;
    157   1.8       cgd 				goto miss;
    158   1.8       cgd 			}
    159   1.1       cgd 		} else
    160   1.1       cgd 			rt->rt_refcnt++;
    161   1.1       cgd 	} else {
    162   1.1       cgd 		rtstat.rts_unreach++;
    163  1.10   mycroft 	miss:	if (report) {
    164  1.10   mycroft 			bzero((caddr_t)&info, sizeof(info));
    165  1.10   mycroft 			info.rti_info[RTAX_DST] = dst;
    166  1.10   mycroft 			rt_missmsg(msgtype, &info, 0, err);
    167  1.10   mycroft 		}
    168   1.1       cgd 	}
    169   1.1       cgd 	splx(s);
    170   1.1       cgd 	return (newrt);
    171   1.1       cgd }
    172   1.1       cgd 
    173   1.9   mycroft void
    174   1.1       cgd rtfree(rt)
    175   1.1       cgd 	register struct rtentry *rt;
    176   1.1       cgd {
    177   1.1       cgd 	register struct ifaddr *ifa;
    178  1.10   mycroft 
    179   1.1       cgd 	if (rt == 0)
    180   1.1       cgd 		panic("rtfree");
    181   1.1       cgd 	rt->rt_refcnt--;
    182   1.1       cgd 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
    183   1.1       cgd 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    184   1.1       cgd 			panic ("rtfree 2");
    185  1.10   mycroft 		rttrash--;
    186  1.10   mycroft 		if (rt->rt_refcnt < 0) {
    187  1.16  christos 			printf("rtfree: %p not freed (neg refs)\n", rt);
    188  1.10   mycroft 			return;
    189  1.10   mycroft 		}
    190  1.18       kml 		rt_timer_remove_all(rt);
    191  1.10   mycroft 		ifa = rt->rt_ifa;
    192  1.10   mycroft 		IFAFREE(ifa);
    193  1.10   mycroft 		Free(rt_key(rt));
    194  1.10   mycroft 		Free(rt);
    195   1.1       cgd 	}
    196   1.1       cgd }
    197   1.1       cgd 
    198  1.10   mycroft void
    199  1.10   mycroft ifafree(ifa)
    200  1.10   mycroft 	register struct ifaddr *ifa;
    201  1.10   mycroft {
    202  1.10   mycroft 	if (ifa == NULL)
    203  1.10   mycroft 		panic("ifafree");
    204  1.10   mycroft 	if (ifa->ifa_refcnt == 0)
    205  1.10   mycroft 		free(ifa, M_IFADDR);
    206  1.10   mycroft 	else
    207  1.10   mycroft 		ifa->ifa_refcnt--;
    208  1.10   mycroft }
    209  1.10   mycroft 
    210   1.1       cgd /*
    211   1.1       cgd  * Force a routing table entry to the specified
    212   1.1       cgd  * destination to go through the given gateway.
    213   1.1       cgd  * Normally called as a result of a routing redirect
    214   1.1       cgd  * message from the network layer.
    215   1.1       cgd  *
    216  1.13   mycroft  * N.B.: must be called at splsoftnet
    217  1.17  christos  *
    218   1.1       cgd  */
    219  1.14  christos void
    220   1.1       cgd rtredirect(dst, gateway, netmask, flags, src, rtp)
    221   1.1       cgd 	struct sockaddr *dst, *gateway, *netmask, *src;
    222   1.1       cgd 	int flags;
    223   1.1       cgd 	struct rtentry **rtp;
    224   1.1       cgd {
    225  1.10   mycroft 	register struct rtentry *rt;
    226   1.1       cgd 	int error = 0;
    227   1.1       cgd 	short *stat = 0;
    228  1.10   mycroft 	struct rt_addrinfo info;
    229  1.10   mycroft 	struct ifaddr *ifa;
    230   1.1       cgd 
    231   1.1       cgd 	/* verify the gateway is directly reachable */
    232  1.10   mycroft 	if ((ifa = ifa_ifwithnet(gateway)) == 0) {
    233   1.1       cgd 		error = ENETUNREACH;
    234   1.8       cgd 		goto out;
    235   1.1       cgd 	}
    236   1.1       cgd 	rt = rtalloc1(dst, 0);
    237   1.1       cgd 	/*
    238   1.1       cgd 	 * If the redirect isn't from our current router for this dst,
    239   1.1       cgd 	 * it's either old or wrong.  If it redirects us to ourselves,
    240   1.1       cgd 	 * we have a routing loop, perhaps as a result of an interface
    241   1.1       cgd 	 * going down recently.
    242   1.1       cgd 	 */
    243   1.1       cgd #define	equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
    244  1.10   mycroft 	if (!(flags & RTF_DONE) && rt &&
    245  1.10   mycroft 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
    246   1.1       cgd 		error = EINVAL;
    247   1.1       cgd 	else if (ifa_ifwithaddr(gateway))
    248   1.1       cgd 		error = EHOSTUNREACH;
    249   1.1       cgd 	if (error)
    250   1.1       cgd 		goto done;
    251   1.1       cgd 	/*
    252   1.1       cgd 	 * Create a new entry if we just got back a wildcard entry
    253   1.1       cgd 	 * or the the lookup failed.  This is necessary for hosts
    254   1.1       cgd 	 * which use routing redirects generated by smart gateways
    255   1.1       cgd 	 * to dynamically build the routing tables.
    256   1.1       cgd 	 */
    257   1.1       cgd 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    258   1.1       cgd 		goto create;
    259   1.1       cgd 	/*
    260   1.1       cgd 	 * Don't listen to the redirect if it's
    261   1.1       cgd 	 * for a route to an interface.
    262   1.1       cgd 	 */
    263   1.1       cgd 	if (rt->rt_flags & RTF_GATEWAY) {
    264   1.1       cgd 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    265   1.1       cgd 			/*
    266   1.1       cgd 			 * Changing from route to net => route to host.
    267   1.1       cgd 			 * Create new route, rather than smashing route to net.
    268   1.1       cgd 			 */
    269   1.1       cgd 		create:
    270   1.1       cgd 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    271   1.1       cgd 			error = rtrequest((int)RTM_ADD, dst, gateway,
    272  1.10   mycroft 				    netmask, flags,
    273   1.1       cgd 				    (struct rtentry **)0);
    274   1.1       cgd 			stat = &rtstat.rts_dynamic;
    275   1.1       cgd 		} else {
    276   1.1       cgd 			/*
    277   1.1       cgd 			 * Smash the current notion of the gateway to
    278   1.1       cgd 			 * this destination.  Should check about netmask!!!
    279   1.1       cgd 			 */
    280  1.10   mycroft 			rt->rt_flags |= RTF_MODIFIED;
    281  1.10   mycroft 			flags |= RTF_MODIFIED;
    282  1.10   mycroft 			stat = &rtstat.rts_newgateway;
    283  1.10   mycroft 			rt_setgate(rt, rt_key(rt), gateway);
    284   1.1       cgd 		}
    285   1.1       cgd 	} else
    286   1.1       cgd 		error = EHOSTUNREACH;
    287   1.1       cgd done:
    288   1.1       cgd 	if (rt) {
    289   1.1       cgd 		if (rtp && !error)
    290   1.1       cgd 			*rtp = rt;
    291   1.1       cgd 		else
    292   1.1       cgd 			rtfree(rt);
    293   1.1       cgd 	}
    294   1.8       cgd out:
    295   1.1       cgd 	if (error)
    296   1.1       cgd 		rtstat.rts_badredirect++;
    297   1.8       cgd 	else if (stat != NULL)
    298   1.8       cgd 		(*stat)++;
    299  1.10   mycroft 	bzero((caddr_t)&info, sizeof(info));
    300  1.10   mycroft 	info.rti_info[RTAX_DST] = dst;
    301  1.10   mycroft 	info.rti_info[RTAX_GATEWAY] = gateway;
    302  1.10   mycroft 	info.rti_info[RTAX_NETMASK] = netmask;
    303  1.10   mycroft 	info.rti_info[RTAX_AUTHOR] = src;
    304  1.10   mycroft 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    305   1.1       cgd }
    306   1.1       cgd 
    307   1.1       cgd /*
    308  1.10   mycroft * Routing table ioctl interface.
    309  1.10   mycroft */
    310   1.9   mycroft int
    311   1.1       cgd rtioctl(req, data, p)
    312  1.12       cgd 	u_long req;
    313   1.1       cgd 	caddr_t data;
    314   1.1       cgd 	struct proc *p;
    315   1.1       cgd {
    316   1.1       cgd 	return (EOPNOTSUPP);
    317   1.1       cgd }
    318   1.1       cgd 
    319   1.1       cgd struct ifaddr *
    320   1.1       cgd ifa_ifwithroute(flags, dst, gateway)
    321  1.10   mycroft 	int flags;
    322  1.10   mycroft 	struct sockaddr	*dst, *gateway;
    323   1.1       cgd {
    324   1.1       cgd 	register struct ifaddr *ifa;
    325   1.1       cgd 	if ((flags & RTF_GATEWAY) == 0) {
    326   1.1       cgd 		/*
    327   1.1       cgd 		 * If we are adding a route to an interface,
    328   1.1       cgd 		 * and the interface is a pt to pt link
    329   1.1       cgd 		 * we should search for the destination
    330   1.1       cgd 		 * as our clue to the interface.  Otherwise
    331   1.1       cgd 		 * we can use the local address.
    332   1.1       cgd 		 */
    333   1.1       cgd 		ifa = 0;
    334   1.1       cgd 		if (flags & RTF_HOST)
    335   1.1       cgd 			ifa = ifa_ifwithdstaddr(dst);
    336   1.1       cgd 		if (ifa == 0)
    337   1.1       cgd 			ifa = ifa_ifwithaddr(gateway);
    338   1.1       cgd 	} else {
    339   1.1       cgd 		/*
    340   1.1       cgd 		 * If we are adding a route to a remote net
    341   1.1       cgd 		 * or host, the gateway may still be on the
    342   1.1       cgd 		 * other end of a pt to pt link.
    343   1.1       cgd 		 */
    344   1.1       cgd 		ifa = ifa_ifwithdstaddr(gateway);
    345   1.1       cgd 	}
    346   1.1       cgd 	if (ifa == 0)
    347   1.1       cgd 		ifa = ifa_ifwithnet(gateway);
    348   1.1       cgd 	if (ifa == 0) {
    349   1.1       cgd 		struct rtentry *rt = rtalloc1(dst, 0);
    350   1.1       cgd 		if (rt == 0)
    351   1.1       cgd 			return (0);
    352   1.1       cgd 		rt->rt_refcnt--;
    353   1.1       cgd 		if ((ifa = rt->rt_ifa) == 0)
    354   1.1       cgd 			return (0);
    355   1.1       cgd 	}
    356   1.1       cgd 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
    357  1.10   mycroft 		struct ifaddr *oifa = ifa;
    358   1.1       cgd 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
    359   1.1       cgd 		if (ifa == 0)
    360   1.1       cgd 			ifa = oifa;
    361   1.1       cgd 	}
    362   1.1       cgd 	return (ifa);
    363   1.1       cgd }
    364   1.1       cgd 
    365   1.1       cgd #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    366   1.1       cgd 
    367   1.9   mycroft int
    368   1.1       cgd rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
    369   1.1       cgd 	int req, flags;
    370   1.1       cgd 	struct sockaddr *dst, *gateway, *netmask;
    371   1.1       cgd 	struct rtentry **ret_nrt;
    372   1.1       cgd {
    373  1.13   mycroft 	int s = splsoftnet(); int error = 0;
    374   1.1       cgd 	register struct rtentry *rt;
    375   1.1       cgd 	register struct radix_node *rn;
    376   1.1       cgd 	register struct radix_node_head *rnh;
    377  1.10   mycroft 	struct ifaddr *ifa;
    378   1.1       cgd 	struct sockaddr *ndst;
    379   1.1       cgd #define senderr(x) { error = x ; goto bad; }
    380   1.1       cgd 
    381  1.10   mycroft 	if ((rnh = rt_tables[dst->sa_family]) == 0)
    382   1.1       cgd 		senderr(ESRCH);
    383   1.1       cgd 	if (flags & RTF_HOST)
    384   1.1       cgd 		netmask = 0;
    385   1.1       cgd 	switch (req) {
    386   1.1       cgd 	case RTM_DELETE:
    387  1.10   mycroft 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
    388   1.1       cgd 			senderr(ESRCH);
    389   1.1       cgd 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    390   1.1       cgd 			panic ("rtrequest delete");
    391   1.1       cgd 		rt = (struct rtentry *)rn;
    392   1.1       cgd 		rt->rt_flags &= ~RTF_UP;
    393  1.10   mycroft 		if (rt->rt_gwroute) {
    394  1.10   mycroft 			rt = rt->rt_gwroute; RTFREE(rt);
    395  1.10   mycroft 			(rt = (struct rtentry *)rn)->rt_gwroute = 0;
    396  1.10   mycroft 		}
    397   1.1       cgd 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
    398   1.1       cgd 			ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    399   1.1       cgd 		rttrash++;
    400  1.10   mycroft 		if (ret_nrt)
    401  1.10   mycroft 			*ret_nrt = rt;
    402  1.10   mycroft 		else if (rt->rt_refcnt <= 0) {
    403  1.10   mycroft 			rt->rt_refcnt++;
    404   1.1       cgd 			rtfree(rt);
    405  1.10   mycroft 		}
    406   1.1       cgd 		break;
    407   1.1       cgd 
    408   1.1       cgd 	case RTM_RESOLVE:
    409   1.1       cgd 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
    410   1.1       cgd 			senderr(EINVAL);
    411   1.1       cgd 		ifa = rt->rt_ifa;
    412   1.1       cgd 		flags = rt->rt_flags & ~RTF_CLONING;
    413   1.1       cgd 		gateway = rt->rt_gateway;
    414   1.1       cgd 		if ((netmask = rt->rt_genmask) == 0)
    415   1.1       cgd 			flags |= RTF_HOST;
    416   1.1       cgd 		goto makeroute;
    417   1.1       cgd 
    418   1.1       cgd 	case RTM_ADD:
    419   1.1       cgd 		if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
    420   1.1       cgd 			senderr(ENETUNREACH);
    421   1.1       cgd 	makeroute:
    422  1.10   mycroft 		R_Malloc(rt, struct rtentry *, sizeof(*rt));
    423   1.1       cgd 		if (rt == 0)
    424   1.1       cgd 			senderr(ENOBUFS);
    425  1.10   mycroft 		Bzero(rt, sizeof(*rt));
    426  1.10   mycroft 		rt->rt_flags = RTF_UP | flags;
    427  1.18       kml 		LIST_INIT(&rt->rt_timer);
    428  1.10   mycroft 		if (rt_setgate(rt, dst, gateway)) {
    429  1.10   mycroft 			Free(rt);
    430  1.10   mycroft 			senderr(ENOBUFS);
    431  1.10   mycroft 		}
    432  1.10   mycroft 		ndst = rt_key(rt);
    433   1.1       cgd 		if (netmask) {
    434   1.1       cgd 			rt_maskedcopy(dst, ndst, netmask);
    435   1.1       cgd 		} else
    436   1.1       cgd 			Bcopy(dst, ndst, dst->sa_len);
    437  1.10   mycroft 		rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
    438  1.10   mycroft 					rnh, rt->rt_nodes);
    439   1.1       cgd 		if (rn == 0) {
    440  1.10   mycroft 			if (rt->rt_gwroute)
    441  1.10   mycroft 				rtfree(rt->rt_gwroute);
    442  1.10   mycroft 			Free(rt_key(rt));
    443  1.10   mycroft 			Free(rt);
    444   1.1       cgd 			senderr(EEXIST);
    445   1.1       cgd 		}
    446  1.10   mycroft 		ifa->ifa_refcnt++;
    447   1.1       cgd 		rt->rt_ifa = ifa;
    448   1.1       cgd 		rt->rt_ifp = ifa->ifa_ifp;
    449   1.1       cgd 		if (req == RTM_RESOLVE)
    450   1.1       cgd 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
    451   1.1       cgd 		if (ifa->ifa_rtrequest)
    452   1.1       cgd 			ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
    453   1.1       cgd 		if (ret_nrt) {
    454   1.1       cgd 			*ret_nrt = rt;
    455   1.1       cgd 			rt->rt_refcnt++;
    456   1.1       cgd 		}
    457   1.1       cgd 		break;
    458   1.1       cgd 	}
    459   1.1       cgd bad:
    460   1.1       cgd 	splx(s);
    461   1.1       cgd 	return (error);
    462   1.1       cgd }
    463   1.1       cgd 
    464  1.10   mycroft int
    465  1.10   mycroft rt_setgate(rt0, dst, gate)
    466  1.10   mycroft 	struct rtentry *rt0;
    467  1.10   mycroft 	struct sockaddr *dst, *gate;
    468  1.10   mycroft {
    469  1.10   mycroft 	caddr_t new, old;
    470  1.10   mycroft 	int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
    471  1.10   mycroft 	register struct rtentry *rt = rt0;
    472  1.10   mycroft 
    473  1.10   mycroft 	if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
    474  1.10   mycroft 		old = (caddr_t)rt_key(rt);
    475  1.10   mycroft 		R_Malloc(new, caddr_t, dlen + glen);
    476  1.10   mycroft 		if (new == 0)
    477  1.10   mycroft 			return 1;
    478  1.10   mycroft 		rt->rt_nodes->rn_key = new;
    479  1.10   mycroft 	} else {
    480  1.10   mycroft 		new = rt->rt_nodes->rn_key;
    481  1.10   mycroft 		old = 0;
    482  1.10   mycroft 	}
    483  1.10   mycroft 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
    484  1.10   mycroft 	if (old) {
    485  1.10   mycroft 		Bcopy(dst, new, dlen);
    486  1.10   mycroft 		Free(old);
    487  1.10   mycroft 	}
    488  1.10   mycroft 	if (rt->rt_gwroute) {
    489  1.10   mycroft 		rt = rt->rt_gwroute; RTFREE(rt);
    490  1.10   mycroft 		rt = rt0; rt->rt_gwroute = 0;
    491  1.10   mycroft 	}
    492  1.10   mycroft 	if (rt->rt_flags & RTF_GATEWAY) {
    493  1.10   mycroft 		rt->rt_gwroute = rtalloc1(gate, 1);
    494  1.10   mycroft 	}
    495  1.10   mycroft 	return 0;
    496  1.10   mycroft }
    497  1.10   mycroft 
    498   1.9   mycroft void
    499   1.1       cgd rt_maskedcopy(src, dst, netmask)
    500   1.9   mycroft 	struct sockaddr *src, *dst, *netmask;
    501   1.1       cgd {
    502   1.1       cgd 	register u_char *cp1 = (u_char *)src;
    503   1.1       cgd 	register u_char *cp2 = (u_char *)dst;
    504   1.1       cgd 	register u_char *cp3 = (u_char *)netmask;
    505   1.1       cgd 	u_char *cplim = cp2 + *cp3;
    506   1.1       cgd 	u_char *cplim2 = cp2 + *cp1;
    507   1.1       cgd 
    508   1.1       cgd 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
    509   1.1       cgd 	cp3 += 2;
    510   1.1       cgd 	if (cplim > cplim2)
    511   1.1       cgd 		cplim = cplim2;
    512   1.1       cgd 	while (cp2 < cplim)
    513   1.1       cgd 		*cp2++ = *cp1++ & *cp3++;
    514   1.1       cgd 	if (cp2 < cplim2)
    515   1.1       cgd 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
    516   1.1       cgd }
    517  1.10   mycroft 
    518   1.1       cgd /*
    519   1.1       cgd  * Set up a routing table entry, normally
    520   1.1       cgd  * for an interface.
    521   1.1       cgd  */
    522   1.9   mycroft int
    523   1.1       cgd rtinit(ifa, cmd, flags)
    524   1.1       cgd 	register struct ifaddr *ifa;
    525   1.1       cgd 	int cmd, flags;
    526   1.1       cgd {
    527   1.1       cgd 	register struct rtentry *rt;
    528   1.1       cgd 	register struct sockaddr *dst;
    529   1.1       cgd 	register struct sockaddr *deldst;
    530   1.1       cgd 	struct mbuf *m = 0;
    531  1.10   mycroft 	struct rtentry *nrt = 0;
    532   1.1       cgd 	int error;
    533   1.1       cgd 
    534   1.1       cgd 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    535   1.1       cgd 	if (cmd == RTM_DELETE) {
    536   1.1       cgd 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    537   1.1       cgd 			m = m_get(M_WAIT, MT_SONAME);
    538   1.1       cgd 			deldst = mtod(m, struct sockaddr *);
    539   1.1       cgd 			rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
    540   1.1       cgd 			dst = deldst;
    541   1.1       cgd 		}
    542  1.14  christos 		if ((rt = rtalloc1(dst, 0)) != NULL) {
    543   1.1       cgd 			rt->rt_refcnt--;
    544   1.1       cgd 			if (rt->rt_ifa != ifa) {
    545   1.1       cgd 				if (m)
    546   1.1       cgd 					(void) m_free(m);
    547   1.1       cgd 				return (flags & RTF_HOST ? EHOSTUNREACH
    548   1.1       cgd 							: ENETUNREACH);
    549   1.1       cgd 			}
    550   1.1       cgd 		}
    551   1.1       cgd 	}
    552   1.1       cgd 	error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
    553  1.10   mycroft 			flags | ifa->ifa_flags, &nrt);
    554   1.1       cgd 	if (m)
    555   1.1       cgd 		(void) m_free(m);
    556  1.10   mycroft 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
    557  1.10   mycroft 		rt_newaddrmsg(cmd, ifa, error, nrt);
    558  1.10   mycroft 		if (rt->rt_refcnt <= 0) {
    559  1.10   mycroft 			rt->rt_refcnt++;
    560  1.10   mycroft 			rtfree(rt);
    561  1.10   mycroft 		}
    562  1.10   mycroft 	}
    563  1.10   mycroft 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
    564  1.10   mycroft 		rt->rt_refcnt--;
    565  1.10   mycroft 		if (rt->rt_ifa != ifa) {
    566  1.17  christos 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
    567  1.17  christos 				rt->rt_ifa);
    568  1.10   mycroft 			if (rt->rt_ifa->ifa_rtrequest)
    569  1.10   mycroft 			    rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    570  1.10   mycroft 			IFAFREE(rt->rt_ifa);
    571  1.10   mycroft 			rt->rt_ifa = ifa;
    572  1.10   mycroft 			rt->rt_ifp = ifa->ifa_ifp;
    573  1.10   mycroft 			ifa->ifa_refcnt++;
    574  1.10   mycroft 			if (ifa->ifa_rtrequest)
    575  1.10   mycroft 			    ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
    576  1.10   mycroft 		}
    577  1.10   mycroft 		rt_newaddrmsg(cmd, ifa, error, nrt);
    578   1.1       cgd 	}
    579   1.1       cgd 	return (error);
    580  1.18       kml }
    581  1.18       kml 
    582  1.18       kml /*
    583  1.18       kml  * Route timer routines.  These routes allow functions to be called
    584  1.18       kml  * for various routes at any time.  This is useful in supporting
    585  1.18       kml  * path MTU discovery and redirect route deletion.
    586  1.18       kml  *
    587  1.18       kml  * This is similar to some BSDI internal functions, but it provides
    588  1.18       kml  * for multiple queues for efficiency's sake...
    589  1.18       kml  */
    590  1.18       kml 
    591  1.18       kml LIST_HEAD(, rttimer_queue) rttimer_queue_head;
    592  1.18       kml static int rt_init_done = 0;
    593  1.18       kml 
    594  1.18       kml #define RTTIMER_CALLOUT(r)	{				\
    595  1.18       kml 	if (r->rtt_func != NULL) {				\
    596  1.18       kml 		r->rtt_func(r->rtt_rt, r);			\
    597  1.18       kml 	} else {						\
    598  1.18       kml 		rtrequest((int) RTM_DELETE,			\
    599  1.18       kml 			  (struct sockaddr *)rt_key(r->rtt_rt),	\
    600  1.18       kml 			  0, 0, 0, 0);				\
    601  1.18       kml 	}							\
    602  1.18       kml }
    603  1.18       kml 
    604  1.18       kml /*
    605  1.18       kml  * Some subtle order problems with domain initialization mean that
    606  1.18       kml  * we cannot count on this being run from rt_init before various
    607  1.18       kml  * protocol initializations are done.  Therefore, we make sure
    608  1.18       kml  * that this is run when the first queue is added...
    609  1.18       kml  */
    610  1.18       kml 
    611  1.18       kml void
    612  1.18       kml rt_timer_init()
    613  1.18       kml {
    614  1.18       kml 	assert(rt_init_done == 0);
    615  1.18       kml 
    616  1.18       kml 	LIST_INIT(&rttimer_queue_head);
    617  1.18       kml 	timeout(rt_timer_timer, NULL, hz);  /* every second */
    618  1.18       kml 	rt_init_done = 1;
    619  1.18       kml }
    620  1.18       kml 
    621  1.18       kml 
    622  1.18       kml struct rttimer_queue *
    623  1.18       kml rt_timer_queue_create(timeout)
    624  1.18       kml 	u_int	timeout;
    625  1.18       kml {
    626  1.18       kml 	struct rttimer_queue *rtq;
    627  1.18       kml 
    628  1.18       kml 	if (rt_init_done == 0)
    629  1.18       kml 		rt_timer_init();
    630  1.18       kml 
    631  1.18       kml 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
    632  1.18       kml 	if (rtq == NULL)
    633  1.18       kml 		return NULL;
    634  1.18       kml 
    635  1.18       kml 	rtq->rtq_timeout = timeout;
    636  1.18       kml 	CIRCLEQ_INIT(&rtq->rtq_head);
    637  1.18       kml 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
    638  1.18       kml 
    639  1.18       kml 	return rtq;
    640  1.18       kml }
    641  1.18       kml 
    642  1.18       kml 
    643  1.18       kml void
    644  1.18       kml rt_timer_queue_change(rtq, timeout)
    645  1.18       kml 	struct rttimer_queue *rtq;
    646  1.18       kml 	long timeout;
    647  1.18       kml {
    648  1.18       kml 	rtq->rtq_timeout = timeout;
    649  1.18       kml }
    650  1.18       kml 
    651  1.18       kml 
    652  1.18       kml void
    653  1.18       kml rt_timer_queue_destroy(rtq, destroy)
    654  1.18       kml 	struct rttimer_queue *rtq;
    655  1.18       kml 	int destroy;
    656  1.18       kml {
    657  1.18       kml 	struct rttimer *r, *r0;
    658  1.18       kml 
    659  1.18       kml 	r = CIRCLEQ_FIRST(&rtq->rtq_head);
    660  1.18       kml 	while (r != (struct rttimer *) &rtq->rtq_head) {
    661  1.18       kml 		r0 = CIRCLEQ_NEXT(r, rtt_next);
    662  1.18       kml 		CIRCLEQ_REMOVE(&rtq->rtq_head, r, rtt_next);
    663  1.18       kml 		LIST_REMOVE(r, rtt_link);
    664  1.18       kml 		if (destroy != 0)
    665  1.18       kml 			RTTIMER_CALLOUT(r);
    666  1.18       kml 		Free(r);
    667  1.18       kml 		r = r0;
    668  1.18       kml 	}
    669  1.18       kml 
    670  1.18       kml 	LIST_REMOVE(rtq, rtq_link);
    671  1.18       kml }
    672  1.18       kml 
    673  1.18       kml 
    674  1.18       kml void
    675  1.18       kml rt_timer_remove_all(rt)
    676  1.18       kml 	struct rtentry *rt;
    677  1.18       kml {
    678  1.18       kml 	struct rttimer *r, *r0;
    679  1.18       kml 
    680  1.18       kml 	r = LIST_FIRST(&rt->rt_timer);
    681  1.18       kml 	while (r) {
    682  1.18       kml 		r0 = LIST_NEXT(r, rtt_link);
    683  1.18       kml 		LIST_REMOVE(r, rtt_link);
    684  1.18       kml 		CIRCLEQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
    685  1.18       kml 		Free(r);
    686  1.18       kml 		r = r0;
    687  1.18       kml 	}
    688  1.18       kml }
    689  1.18       kml 
    690  1.18       kml 
    691  1.18       kml int
    692  1.18       kml rt_timer_add(rt, func, queue)
    693  1.18       kml 	struct rtentry *rt;
    694  1.18       kml 	void(*func) __P((struct rtentry *, struct rttimer *));
    695  1.18       kml 	struct rttimer_queue *queue;
    696  1.18       kml {
    697  1.18       kml 	struct rttimer *r, *rttimer;
    698  1.18       kml 	int s;
    699  1.18       kml 	long current_time;
    700  1.18       kml 
    701  1.18       kml 	s = splclock();
    702  1.18       kml 	current_time = mono_time.tv_sec;
    703  1.18       kml 	splx(s);
    704  1.18       kml 
    705  1.18       kml 	for (r = LIST_FIRST(&rt->rt_timer); r; r = LIST_NEXT(r, rtt_link)) {
    706  1.18       kml 		if (r->rtt_func == func) {
    707  1.18       kml 			LIST_REMOVE(r, rtt_link);
    708  1.18       kml 			CIRCLEQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
    709  1.18       kml 			Free(r);
    710  1.18       kml 			break;  /* only one per list, so we can quit... */
    711  1.18       kml 		}
    712  1.18       kml 	}
    713  1.18       kml 
    714  1.18       kml 	R_Malloc(rttimer, struct rttimer *, sizeof *rttimer);
    715  1.18       kml 	if (rttimer == NULL)
    716  1.18       kml 		return ENOBUFS;
    717  1.18       kml 
    718  1.18       kml 	rttimer->rtt_rt = rt;
    719  1.18       kml 	rttimer->rtt_time = current_time;
    720  1.18       kml 	rttimer->rtt_func = func;
    721  1.18       kml 	rttimer->rtt_queue = queue;
    722  1.18       kml 	LIST_INSERT_HEAD(&rt->rt_timer, rttimer, rtt_link);
    723  1.18       kml 
    724  1.18       kml 	r = CIRCLEQ_LAST(&queue->rtq_head);
    725  1.18       kml 	while (r && r != (struct rttimer *) &queue->rtq_head &&
    726  1.18       kml 	       r->rtt_time > current_time)
    727  1.18       kml 		r = CIRCLEQ_PREV(r, rtt_next);
    728  1.18       kml 
    729  1.18       kml 	if (r)
    730  1.18       kml 		CIRCLEQ_INSERT_AFTER(&queue->rtq_head, r, rttimer, rtt_next);
    731  1.18       kml 	else
    732  1.18       kml 		CIRCLEQ_INSERT_HEAD(&queue->rtq_head, rttimer, rtt_next);
    733  1.18       kml 
    734  1.18       kml 	return 0;
    735  1.18       kml }
    736  1.18       kml 
    737  1.18       kml /* ARGSUSED */
    738  1.18       kml void
    739  1.18       kml rt_timer_timer(arg)
    740  1.18       kml 	void *arg;
    741  1.18       kml {
    742  1.18       kml 	struct rttimer *r, *rttimer;
    743  1.18       kml 	struct rttimer_queue	*rtq;
    744  1.18       kml 	long current_time;
    745  1.18       kml 	int s;
    746  1.18       kml 
    747  1.18       kml 	s = splclock();
    748  1.18       kml 	current_time = mono_time.tv_sec;
    749  1.18       kml 	splx(s);
    750  1.18       kml 
    751  1.18       kml 	for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
    752  1.18       kml 	     rtq = LIST_NEXT(rtq, rtq_link)) {
    753  1.18       kml 		rttimer = CIRCLEQ_FIRST(&rtq->rtq_head);
    754  1.18       kml 		while (rttimer != (struct rttimer *) &rtq->rtq_head &&
    755  1.18       kml 		       (rttimer->rtt_time + rtq->rtq_timeout) < current_time) {
    756  1.18       kml 			r = CIRCLEQ_NEXT(rttimer, rtt_next);
    757  1.18       kml 			CIRCLEQ_REMOVE(&rtq->rtq_head, rttimer, rtt_next);
    758  1.18       kml 			LIST_REMOVE(rttimer, rtt_link);
    759  1.18       kml 			RTTIMER_CALLOUT(rttimer);
    760  1.18       kml 			Free(rttimer);
    761  1.18       kml 			rttimer = r;
    762  1.18       kml 		}
    763  1.18       kml 	}
    764  1.18       kml 
    765  1.18       kml 	timeout(rt_timer_timer, NULL, hz);  /* every second */
    766   1.1       cgd }
    767