Home | History | Annotate | Line # | Download | only in net
route.c revision 1.13
      1  1.13  mycroft /*	$NetBSD: route.c,v 1.13 1995/08/12 23:59:25 mycroft Exp $	*/
      2  1.11      cgd 
      3   1.1      cgd /*
      4  1.10  mycroft  * Copyright (c) 1980, 1986, 1991, 1993
      5  1.10  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  *
     35  1.11      cgd  *	@(#)route.c	8.2 (Berkeley) 11/15/93
     36   1.1      cgd  */
     37   1.2      cgd 
     38   1.5  mycroft #include <sys/param.h>
     39   1.5  mycroft #include <sys/systm.h>
     40   1.5  mycroft #include <sys/proc.h>
     41   1.5  mycroft #include <sys/mbuf.h>
     42   1.5  mycroft #include <sys/socket.h>
     43   1.5  mycroft #include <sys/socketvar.h>
     44   1.5  mycroft #include <sys/domain.h>
     45   1.5  mycroft #include <sys/protosw.h>
     46   1.5  mycroft #include <sys/ioctl.h>
     47   1.1      cgd 
     48   1.5  mycroft #include <net/if.h>
     49   1.5  mycroft #include <net/route.h>
     50   1.5  mycroft #include <net/raw_cb.h>
     51   1.1      cgd 
     52   1.5  mycroft #include <netinet/in.h>
     53   1.5  mycroft #include <netinet/in_var.h>
     54   1.1      cgd 
     55   1.1      cgd #ifdef NS
     56   1.5  mycroft #include <netns/ns.h>
     57   1.1      cgd #endif
     58   1.5  mycroft 
     59   1.1      cgd #define	SA(p) ((struct sockaddr *)(p))
     60   1.1      cgd 
     61   1.1      cgd int	rttrash;		/* routes not in table but not freed */
     62   1.1      cgd struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
     63   1.1      cgd 
     64  1.10  mycroft void
     65  1.10  mycroft rtable_init(table)
     66  1.10  mycroft 	void **table;
     67  1.10  mycroft {
     68  1.10  mycroft 	struct domain *dom;
     69  1.10  mycroft 	for (dom = domains; dom; dom = dom->dom_next)
     70  1.10  mycroft 		if (dom->dom_rtattach)
     71  1.10  mycroft 			dom->dom_rtattach(&table[dom->dom_family],
     72  1.10  mycroft 			    dom->dom_rtoffset);
     73  1.10  mycroft }
     74   1.1      cgd 
     75   1.9  mycroft void
     76  1.10  mycroft route_init()
     77   1.1      cgd {
     78  1.10  mycroft 	rn_init();	/* initialize all zeroes, all ones, mask table */
     79  1.10  mycroft 	rtable_init((void **)rt_tables);
     80   1.1      cgd }
     81   1.1      cgd 
     82   1.1      cgd /*
     83   1.1      cgd  * Packet routing routines.
     84   1.1      cgd  */
     85   1.9  mycroft void
     86   1.1      cgd rtalloc(ro)
     87   1.1      cgd 	register struct route *ro;
     88   1.1      cgd {
     89   1.1      cgd 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
     90   1.1      cgd 		return;				 /* XXX */
     91   1.1      cgd 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
     92   1.1      cgd }
     93   1.1      cgd 
     94   1.1      cgd struct rtentry *
     95   1.1      cgd rtalloc1(dst, report)
     96   1.1      cgd 	register struct sockaddr *dst;
     97  1.10  mycroft 	int report;
     98   1.1      cgd {
     99  1.10  mycroft 	register struct radix_node_head *rnh = rt_tables[dst->sa_family];
    100   1.1      cgd 	register struct rtentry *rt;
    101   1.1      cgd 	register struct radix_node *rn;
    102   1.1      cgd 	struct rtentry *newrt = 0;
    103  1.10  mycroft 	struct rt_addrinfo info;
    104  1.13  mycroft 	int  s = splsoftnet(), err = 0, msgtype = RTM_MISS;
    105   1.1      cgd 
    106  1.10  mycroft 	if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
    107   1.1      cgd 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
    108   1.1      cgd 		newrt = rt = (struct rtentry *)rn;
    109   1.1      cgd 		if (report && (rt->rt_flags & RTF_CLONING)) {
    110   1.8      cgd 			err = rtrequest(RTM_RESOLVE, dst, SA(0),
    111  1.10  mycroft 					      SA(0), 0, &newrt);
    112   1.8      cgd 			if (err) {
    113   1.8      cgd 				newrt = rt;
    114   1.8      cgd 				rt->rt_refcnt++;
    115   1.8      cgd 				goto miss;
    116   1.8      cgd 			}
    117   1.8      cgd 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
    118   1.8      cgd 				msgtype = RTM_RESOLVE;
    119   1.8      cgd 				goto miss;
    120   1.8      cgd 			}
    121   1.1      cgd 		} else
    122   1.1      cgd 			rt->rt_refcnt++;
    123   1.1      cgd 	} else {
    124   1.1      cgd 		rtstat.rts_unreach++;
    125  1.10  mycroft 	miss:	if (report) {
    126  1.10  mycroft 			bzero((caddr_t)&info, sizeof(info));
    127  1.10  mycroft 			info.rti_info[RTAX_DST] = dst;
    128  1.10  mycroft 			rt_missmsg(msgtype, &info, 0, err);
    129  1.10  mycroft 		}
    130   1.1      cgd 	}
    131   1.1      cgd 	splx(s);
    132   1.1      cgd 	return (newrt);
    133   1.1      cgd }
    134   1.1      cgd 
    135   1.9  mycroft void
    136   1.1      cgd rtfree(rt)
    137   1.1      cgd 	register struct rtentry *rt;
    138   1.1      cgd {
    139   1.1      cgd 	register struct ifaddr *ifa;
    140  1.10  mycroft 
    141   1.1      cgd 	if (rt == 0)
    142   1.1      cgd 		panic("rtfree");
    143   1.1      cgd 	rt->rt_refcnt--;
    144   1.1      cgd 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
    145   1.1      cgd 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    146   1.1      cgd 			panic ("rtfree 2");
    147  1.10  mycroft 		rttrash--;
    148  1.10  mycroft 		if (rt->rt_refcnt < 0) {
    149  1.10  mycroft 			printf("rtfree: %x not freed (neg refs)\n", rt);
    150  1.10  mycroft 			return;
    151  1.10  mycroft 		}
    152  1.10  mycroft 		ifa = rt->rt_ifa;
    153  1.10  mycroft 		IFAFREE(ifa);
    154  1.10  mycroft 		Free(rt_key(rt));
    155  1.10  mycroft 		Free(rt);
    156   1.1      cgd 	}
    157   1.1      cgd }
    158   1.1      cgd 
    159  1.10  mycroft void
    160  1.10  mycroft ifafree(ifa)
    161  1.10  mycroft 	register struct ifaddr *ifa;
    162  1.10  mycroft {
    163  1.10  mycroft 	if (ifa == NULL)
    164  1.10  mycroft 		panic("ifafree");
    165  1.10  mycroft 	if (ifa->ifa_refcnt == 0)
    166  1.10  mycroft 		free(ifa, M_IFADDR);
    167  1.10  mycroft 	else
    168  1.10  mycroft 		ifa->ifa_refcnt--;
    169  1.10  mycroft }
    170  1.10  mycroft 
    171   1.1      cgd /*
    172   1.1      cgd  * Force a routing table entry to the specified
    173   1.1      cgd  * destination to go through the given gateway.
    174   1.1      cgd  * Normally called as a result of a routing redirect
    175   1.1      cgd  * message from the network layer.
    176   1.1      cgd  *
    177  1.13  mycroft  * N.B.: must be called at splsoftnet
    178   1.1      cgd  */
    179   1.9  mycroft int
    180   1.1      cgd rtredirect(dst, gateway, netmask, flags, src, rtp)
    181   1.1      cgd 	struct sockaddr *dst, *gateway, *netmask, *src;
    182   1.1      cgd 	int flags;
    183   1.1      cgd 	struct rtentry **rtp;
    184   1.1      cgd {
    185  1.10  mycroft 	register struct rtentry *rt;
    186   1.1      cgd 	int error = 0;
    187   1.1      cgd 	short *stat = 0;
    188  1.10  mycroft 	struct rt_addrinfo info;
    189  1.10  mycroft 	struct ifaddr *ifa;
    190   1.1      cgd 
    191   1.1      cgd 	/* verify the gateway is directly reachable */
    192  1.10  mycroft 	if ((ifa = ifa_ifwithnet(gateway)) == 0) {
    193   1.1      cgd 		error = ENETUNREACH;
    194   1.8      cgd 		goto out;
    195   1.1      cgd 	}
    196   1.1      cgd 	rt = rtalloc1(dst, 0);
    197   1.1      cgd 	/*
    198   1.1      cgd 	 * If the redirect isn't from our current router for this dst,
    199   1.1      cgd 	 * it's either old or wrong.  If it redirects us to ourselves,
    200   1.1      cgd 	 * we have a routing loop, perhaps as a result of an interface
    201   1.1      cgd 	 * going down recently.
    202   1.1      cgd 	 */
    203   1.1      cgd #define	equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
    204  1.10  mycroft 	if (!(flags & RTF_DONE) && rt &&
    205  1.10  mycroft 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
    206   1.1      cgd 		error = EINVAL;
    207   1.1      cgd 	else if (ifa_ifwithaddr(gateway))
    208   1.1      cgd 		error = EHOSTUNREACH;
    209   1.1      cgd 	if (error)
    210   1.1      cgd 		goto done;
    211   1.1      cgd 	/*
    212   1.1      cgd 	 * Create a new entry if we just got back a wildcard entry
    213   1.1      cgd 	 * or the the lookup failed.  This is necessary for hosts
    214   1.1      cgd 	 * which use routing redirects generated by smart gateways
    215   1.1      cgd 	 * to dynamically build the routing tables.
    216   1.1      cgd 	 */
    217   1.1      cgd 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    218   1.1      cgd 		goto create;
    219   1.1      cgd 	/*
    220   1.1      cgd 	 * Don't listen to the redirect if it's
    221   1.1      cgd 	 * for a route to an interface.
    222   1.1      cgd 	 */
    223   1.1      cgd 	if (rt->rt_flags & RTF_GATEWAY) {
    224   1.1      cgd 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    225   1.1      cgd 			/*
    226   1.1      cgd 			 * Changing from route to net => route to host.
    227   1.1      cgd 			 * Create new route, rather than smashing route to net.
    228   1.1      cgd 			 */
    229   1.1      cgd 		create:
    230   1.1      cgd 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    231   1.1      cgd 			error = rtrequest((int)RTM_ADD, dst, gateway,
    232  1.10  mycroft 				    netmask, flags,
    233   1.1      cgd 				    (struct rtentry **)0);
    234   1.1      cgd 			stat = &rtstat.rts_dynamic;
    235   1.1      cgd 		} else {
    236   1.1      cgd 			/*
    237   1.1      cgd 			 * Smash the current notion of the gateway to
    238   1.1      cgd 			 * this destination.  Should check about netmask!!!
    239   1.1      cgd 			 */
    240  1.10  mycroft 			rt->rt_flags |= RTF_MODIFIED;
    241  1.10  mycroft 			flags |= RTF_MODIFIED;
    242  1.10  mycroft 			stat = &rtstat.rts_newgateway;
    243  1.10  mycroft 			rt_setgate(rt, rt_key(rt), gateway);
    244   1.1      cgd 		}
    245   1.1      cgd 	} else
    246   1.1      cgd 		error = EHOSTUNREACH;
    247   1.1      cgd done:
    248   1.1      cgd 	if (rt) {
    249   1.1      cgd 		if (rtp && !error)
    250   1.1      cgd 			*rtp = rt;
    251   1.1      cgd 		else
    252   1.1      cgd 			rtfree(rt);
    253   1.1      cgd 	}
    254   1.8      cgd out:
    255   1.1      cgd 	if (error)
    256   1.1      cgd 		rtstat.rts_badredirect++;
    257   1.8      cgd 	else if (stat != NULL)
    258   1.8      cgd 		(*stat)++;
    259  1.10  mycroft 	bzero((caddr_t)&info, sizeof(info));
    260  1.10  mycroft 	info.rti_info[RTAX_DST] = dst;
    261  1.10  mycroft 	info.rti_info[RTAX_GATEWAY] = gateway;
    262  1.10  mycroft 	info.rti_info[RTAX_NETMASK] = netmask;
    263  1.10  mycroft 	info.rti_info[RTAX_AUTHOR] = src;
    264  1.10  mycroft 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    265   1.1      cgd }
    266   1.1      cgd 
    267   1.1      cgd /*
    268  1.10  mycroft * Routing table ioctl interface.
    269  1.10  mycroft */
    270   1.9  mycroft int
    271   1.1      cgd rtioctl(req, data, p)
    272  1.12      cgd 	u_long req;
    273   1.1      cgd 	caddr_t data;
    274   1.1      cgd 	struct proc *p;
    275   1.1      cgd {
    276   1.1      cgd 	return (EOPNOTSUPP);
    277   1.1      cgd }
    278   1.1      cgd 
    279   1.1      cgd struct ifaddr *
    280   1.1      cgd ifa_ifwithroute(flags, dst, gateway)
    281  1.10  mycroft 	int flags;
    282  1.10  mycroft 	struct sockaddr	*dst, *gateway;
    283   1.1      cgd {
    284   1.1      cgd 	register struct ifaddr *ifa;
    285   1.1      cgd 	if ((flags & RTF_GATEWAY) == 0) {
    286   1.1      cgd 		/*
    287   1.1      cgd 		 * If we are adding a route to an interface,
    288   1.1      cgd 		 * and the interface is a pt to pt link
    289   1.1      cgd 		 * we should search for the destination
    290   1.1      cgd 		 * as our clue to the interface.  Otherwise
    291   1.1      cgd 		 * we can use the local address.
    292   1.1      cgd 		 */
    293   1.1      cgd 		ifa = 0;
    294   1.1      cgd 		if (flags & RTF_HOST)
    295   1.1      cgd 			ifa = ifa_ifwithdstaddr(dst);
    296   1.1      cgd 		if (ifa == 0)
    297   1.1      cgd 			ifa = ifa_ifwithaddr(gateway);
    298   1.1      cgd 	} else {
    299   1.1      cgd 		/*
    300   1.1      cgd 		 * If we are adding a route to a remote net
    301   1.1      cgd 		 * or host, the gateway may still be on the
    302   1.1      cgd 		 * other end of a pt to pt link.
    303   1.1      cgd 		 */
    304   1.1      cgd 		ifa = ifa_ifwithdstaddr(gateway);
    305   1.1      cgd 	}
    306   1.1      cgd 	if (ifa == 0)
    307   1.1      cgd 		ifa = ifa_ifwithnet(gateway);
    308   1.1      cgd 	if (ifa == 0) {
    309   1.1      cgd 		struct rtentry *rt = rtalloc1(dst, 0);
    310   1.1      cgd 		if (rt == 0)
    311   1.1      cgd 			return (0);
    312   1.1      cgd 		rt->rt_refcnt--;
    313   1.1      cgd 		if ((ifa = rt->rt_ifa) == 0)
    314   1.1      cgd 			return (0);
    315   1.1      cgd 	}
    316   1.1      cgd 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
    317  1.10  mycroft 		struct ifaddr *oifa = ifa;
    318   1.1      cgd 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
    319   1.1      cgd 		if (ifa == 0)
    320   1.1      cgd 			ifa = oifa;
    321   1.1      cgd 	}
    322   1.1      cgd 	return (ifa);
    323   1.1      cgd }
    324   1.1      cgd 
    325   1.1      cgd #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    326   1.1      cgd 
    327   1.9  mycroft int
    328   1.1      cgd rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
    329   1.1      cgd 	int req, flags;
    330   1.1      cgd 	struct sockaddr *dst, *gateway, *netmask;
    331   1.1      cgd 	struct rtentry **ret_nrt;
    332   1.1      cgd {
    333  1.13  mycroft 	int s = splsoftnet(); int error = 0;
    334   1.1      cgd 	register struct rtentry *rt;
    335   1.1      cgd 	register struct radix_node *rn;
    336   1.1      cgd 	register struct radix_node_head *rnh;
    337  1.10  mycroft 	struct ifaddr *ifa;
    338   1.1      cgd 	struct sockaddr *ndst;
    339   1.1      cgd #define senderr(x) { error = x ; goto bad; }
    340   1.1      cgd 
    341  1.10  mycroft 	if ((rnh = rt_tables[dst->sa_family]) == 0)
    342   1.1      cgd 		senderr(ESRCH);
    343   1.1      cgd 	if (flags & RTF_HOST)
    344   1.1      cgd 		netmask = 0;
    345   1.1      cgd 	switch (req) {
    346   1.1      cgd 	case RTM_DELETE:
    347  1.10  mycroft 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
    348   1.1      cgd 			senderr(ESRCH);
    349   1.1      cgd 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
    350   1.1      cgd 			panic ("rtrequest delete");
    351   1.1      cgd 		rt = (struct rtentry *)rn;
    352   1.1      cgd 		rt->rt_flags &= ~RTF_UP;
    353  1.10  mycroft 		if (rt->rt_gwroute) {
    354  1.10  mycroft 			rt = rt->rt_gwroute; RTFREE(rt);
    355  1.10  mycroft 			(rt = (struct rtentry *)rn)->rt_gwroute = 0;
    356  1.10  mycroft 		}
    357   1.1      cgd 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
    358   1.1      cgd 			ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    359   1.1      cgd 		rttrash++;
    360  1.10  mycroft 		if (ret_nrt)
    361  1.10  mycroft 			*ret_nrt = rt;
    362  1.10  mycroft 		else if (rt->rt_refcnt <= 0) {
    363  1.10  mycroft 			rt->rt_refcnt++;
    364   1.1      cgd 			rtfree(rt);
    365  1.10  mycroft 		}
    366   1.1      cgd 		break;
    367   1.1      cgd 
    368   1.1      cgd 	case RTM_RESOLVE:
    369   1.1      cgd 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
    370   1.1      cgd 			senderr(EINVAL);
    371   1.1      cgd 		ifa = rt->rt_ifa;
    372   1.1      cgd 		flags = rt->rt_flags & ~RTF_CLONING;
    373   1.1      cgd 		gateway = rt->rt_gateway;
    374   1.1      cgd 		if ((netmask = rt->rt_genmask) == 0)
    375   1.1      cgd 			flags |= RTF_HOST;
    376   1.1      cgd 		goto makeroute;
    377   1.1      cgd 
    378   1.1      cgd 	case RTM_ADD:
    379   1.1      cgd 		if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
    380   1.1      cgd 			senderr(ENETUNREACH);
    381   1.1      cgd 	makeroute:
    382  1.10  mycroft 		R_Malloc(rt, struct rtentry *, sizeof(*rt));
    383   1.1      cgd 		if (rt == 0)
    384   1.1      cgd 			senderr(ENOBUFS);
    385  1.10  mycroft 		Bzero(rt, sizeof(*rt));
    386  1.10  mycroft 		rt->rt_flags = RTF_UP | flags;
    387  1.10  mycroft 		if (rt_setgate(rt, dst, gateway)) {
    388  1.10  mycroft 			Free(rt);
    389  1.10  mycroft 			senderr(ENOBUFS);
    390  1.10  mycroft 		}
    391  1.10  mycroft 		ndst = rt_key(rt);
    392   1.1      cgd 		if (netmask) {
    393   1.1      cgd 			rt_maskedcopy(dst, ndst, netmask);
    394   1.1      cgd 		} else
    395   1.1      cgd 			Bcopy(dst, ndst, dst->sa_len);
    396  1.10  mycroft 		rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
    397  1.10  mycroft 					rnh, rt->rt_nodes);
    398   1.1      cgd 		if (rn == 0) {
    399  1.10  mycroft 			if (rt->rt_gwroute)
    400  1.10  mycroft 				rtfree(rt->rt_gwroute);
    401  1.10  mycroft 			Free(rt_key(rt));
    402  1.10  mycroft 			Free(rt);
    403   1.1      cgd 			senderr(EEXIST);
    404   1.1      cgd 		}
    405  1.10  mycroft 		ifa->ifa_refcnt++;
    406   1.1      cgd 		rt->rt_ifa = ifa;
    407   1.1      cgd 		rt->rt_ifp = ifa->ifa_ifp;
    408   1.1      cgd 		if (req == RTM_RESOLVE)
    409   1.1      cgd 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
    410   1.1      cgd 		if (ifa->ifa_rtrequest)
    411   1.1      cgd 			ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
    412   1.1      cgd 		if (ret_nrt) {
    413   1.1      cgd 			*ret_nrt = rt;
    414   1.1      cgd 			rt->rt_refcnt++;
    415   1.1      cgd 		}
    416   1.1      cgd 		break;
    417   1.1      cgd 	}
    418   1.1      cgd bad:
    419   1.1      cgd 	splx(s);
    420   1.1      cgd 	return (error);
    421   1.1      cgd }
    422   1.1      cgd 
    423  1.10  mycroft int
    424  1.10  mycroft rt_setgate(rt0, dst, gate)
    425  1.10  mycroft 	struct rtentry *rt0;
    426  1.10  mycroft 	struct sockaddr *dst, *gate;
    427  1.10  mycroft {
    428  1.10  mycroft 	caddr_t new, old;
    429  1.10  mycroft 	int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
    430  1.10  mycroft 	register struct rtentry *rt = rt0;
    431  1.10  mycroft 
    432  1.10  mycroft 	if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
    433  1.10  mycroft 		old = (caddr_t)rt_key(rt);
    434  1.10  mycroft 		R_Malloc(new, caddr_t, dlen + glen);
    435  1.10  mycroft 		if (new == 0)
    436  1.10  mycroft 			return 1;
    437  1.10  mycroft 		rt->rt_nodes->rn_key = new;
    438  1.10  mycroft 	} else {
    439  1.10  mycroft 		new = rt->rt_nodes->rn_key;
    440  1.10  mycroft 		old = 0;
    441  1.10  mycroft 	}
    442  1.10  mycroft 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
    443  1.10  mycroft 	if (old) {
    444  1.10  mycroft 		Bcopy(dst, new, dlen);
    445  1.10  mycroft 		Free(old);
    446  1.10  mycroft 	}
    447  1.10  mycroft 	if (rt->rt_gwroute) {
    448  1.10  mycroft 		rt = rt->rt_gwroute; RTFREE(rt);
    449  1.10  mycroft 		rt = rt0; rt->rt_gwroute = 0;
    450  1.10  mycroft 	}
    451  1.10  mycroft 	if (rt->rt_flags & RTF_GATEWAY) {
    452  1.10  mycroft 		rt->rt_gwroute = rtalloc1(gate, 1);
    453  1.10  mycroft 	}
    454  1.10  mycroft 	return 0;
    455  1.10  mycroft }
    456  1.10  mycroft 
    457   1.9  mycroft void
    458   1.1      cgd rt_maskedcopy(src, dst, netmask)
    459   1.9  mycroft 	struct sockaddr *src, *dst, *netmask;
    460   1.1      cgd {
    461   1.1      cgd 	register u_char *cp1 = (u_char *)src;
    462   1.1      cgd 	register u_char *cp2 = (u_char *)dst;
    463   1.1      cgd 	register u_char *cp3 = (u_char *)netmask;
    464   1.1      cgd 	u_char *cplim = cp2 + *cp3;
    465   1.1      cgd 	u_char *cplim2 = cp2 + *cp1;
    466   1.1      cgd 
    467   1.1      cgd 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
    468   1.1      cgd 	cp3 += 2;
    469   1.1      cgd 	if (cplim > cplim2)
    470   1.1      cgd 		cplim = cplim2;
    471   1.1      cgd 	while (cp2 < cplim)
    472   1.1      cgd 		*cp2++ = *cp1++ & *cp3++;
    473   1.1      cgd 	if (cp2 < cplim2)
    474   1.1      cgd 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
    475   1.1      cgd }
    476  1.10  mycroft 
    477   1.1      cgd /*
    478   1.1      cgd  * Set up a routing table entry, normally
    479   1.1      cgd  * for an interface.
    480   1.1      cgd  */
    481   1.9  mycroft int
    482   1.1      cgd rtinit(ifa, cmd, flags)
    483   1.1      cgd 	register struct ifaddr *ifa;
    484   1.1      cgd 	int cmd, flags;
    485   1.1      cgd {
    486   1.1      cgd 	register struct rtentry *rt;
    487   1.1      cgd 	register struct sockaddr *dst;
    488   1.1      cgd 	register struct sockaddr *deldst;
    489   1.1      cgd 	struct mbuf *m = 0;
    490  1.10  mycroft 	struct rtentry *nrt = 0;
    491   1.1      cgd 	int error;
    492   1.1      cgd 
    493   1.1      cgd 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    494   1.1      cgd 	if (cmd == RTM_DELETE) {
    495   1.1      cgd 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    496   1.1      cgd 			m = m_get(M_WAIT, MT_SONAME);
    497   1.1      cgd 			deldst = mtod(m, struct sockaddr *);
    498   1.1      cgd 			rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
    499   1.1      cgd 			dst = deldst;
    500   1.1      cgd 		}
    501   1.1      cgd 		if (rt = rtalloc1(dst, 0)) {
    502   1.1      cgd 			rt->rt_refcnt--;
    503   1.1      cgd 			if (rt->rt_ifa != ifa) {
    504   1.1      cgd 				if (m)
    505   1.1      cgd 					(void) m_free(m);
    506   1.1      cgd 				return (flags & RTF_HOST ? EHOSTUNREACH
    507   1.1      cgd 							: ENETUNREACH);
    508   1.1      cgd 			}
    509   1.1      cgd 		}
    510   1.1      cgd 	}
    511   1.1      cgd 	error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
    512  1.10  mycroft 			flags | ifa->ifa_flags, &nrt);
    513   1.1      cgd 	if (m)
    514   1.1      cgd 		(void) m_free(m);
    515  1.10  mycroft 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
    516  1.10  mycroft 		rt_newaddrmsg(cmd, ifa, error, nrt);
    517  1.10  mycroft 		if (rt->rt_refcnt <= 0) {
    518  1.10  mycroft 			rt->rt_refcnt++;
    519  1.10  mycroft 			rtfree(rt);
    520  1.10  mycroft 		}
    521  1.10  mycroft 	}
    522  1.10  mycroft 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
    523  1.10  mycroft 		rt->rt_refcnt--;
    524  1.10  mycroft 		if (rt->rt_ifa != ifa) {
    525  1.10  mycroft 			printf("rtinit: wrong ifa (%x) was (%x)\n", ifa,
    526  1.10  mycroft 				rt->rt_ifa);
    527  1.10  mycroft 			if (rt->rt_ifa->ifa_rtrequest)
    528  1.10  mycroft 			    rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
    529  1.10  mycroft 			IFAFREE(rt->rt_ifa);
    530  1.10  mycroft 			rt->rt_ifa = ifa;
    531  1.10  mycroft 			rt->rt_ifp = ifa->ifa_ifp;
    532  1.10  mycroft 			ifa->ifa_refcnt++;
    533  1.10  mycroft 			if (ifa->ifa_rtrequest)
    534  1.10  mycroft 			    ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
    535  1.10  mycroft 		}
    536  1.10  mycroft 		rt_newaddrmsg(cmd, ifa, error, nrt);
    537   1.1      cgd 	}
    538   1.1      cgd 	return (error);
    539   1.1      cgd }
    540