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