home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / net / route.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  17.1 KB  |  638 lines

  1. RCS_ID_C="$Id: route.c,v 1.14 1993/06/04 11:16:15 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * route.c --- Generic Routing Routines
  8.  *
  9.  * Last modified: Fri Jun  4 00:39:33 1993 jraja
  10.  *
  11.  * HISTORY
  12.  * $Log: route.c,v $
  13.  * Revision 1.14  1993/06/04  11:16:15  jraja
  14.  * Fixes for first public release.
  15.  *
  16.  * Revision 1.13  1993/05/16  21:09:43  ppessi
  17.  * RCS version changed.
  18.  *
  19.  * Revision 1.12  1993/05/04  12:53:55  jraja
  20.  * Enabled rtioctl() with AMITCP.
  21.  *
  22.  * Revision 1.11  93/05/02  17:31:25  17:31:25  jraja (Jarno Tapio Rajahalme)
  23.  * Updated rtioctl().
  24.  * 
  25.  * Revision 1.10  93/04/05  17:46:20  17:46:20  jraja (Jarno Tapio Rajahalme)
  26.  * Changed spl storage variables to spl_t.
  27.  * Changed every .c file to use conf.h.
  28.  * 
  29.  * Revision 1.9  93/03/13  17:13:42  17:13:42  ppessi (Pekka Pessi)
  30.  * Fixed variable initialization problems.
  31.  * 
  32.  * Revision 1.8  93/03/05  19:51:09  19:51:09  jraja (Jarno Tapio Rajahalme)
  33.  * Fixed includes (again).
  34.  * 
  35.  * Revision 1.7  93/03/05  03:12:52  03:12:52  ppessi (Pekka Pessi)
  36.  * Compiles with SASC. Initial test version
  37.  * 
  38.  * Revision 1.6  93/02/25  19:52:45  19:52:45  ppessi (Pekka Pessi)
  39.  *  Added prototypes
  40.  * 
  41.  * Revision 1.5  93/02/25  16:33:05  16:33:05  jraja (Jarno Tapio Rajahalme)
  42.  * Removed SocketBase argument from rtioctl.
  43.  * 
  44.  * Revision 1.4  93/02/19  15:39:37  15:39:37  jraja (Jarno Tapio Rajahalme)
  45.  * Changed rtioctl's proc argument to SocketBase.
  46.  * 
  47.  * Revision 1.3  93/02/04  19:03:08  19:03:08  jraja (Jarno Tapio Rajahalme)
  48.  * Fixed bsd_free() and free() to Free().
  49.  * 
  50.  * Revision 1.2  92/11/20  14:43:36  14:43:36  jraja (Jarno Tapio Rajahalme)
  51.  * added #ifndef AMITCP's to get this compile somehow
  52.  * 
  53.  * Revision 1.1  92/11/20  13:32:42  13:32:42  jraja (Jarno Tapio Rajahalme)
  54.  * Initial revision
  55.  * 
  56.  *
  57.  */
  58.  
  59. /* 
  60.  * Mach Operating System
  61.  * Copyright (c) 1992 Carnegie Mellon University
  62.  * All Rights Reserved.
  63.  * 
  64.  * Permission to use, copy, modify and distribute this software and its
  65.  * documentation is hereby granted, provided that both the copyright
  66.  * notice and this permission notice appear in all copies of the
  67.  * software, derivative works or modified versions, and any portions
  68.  * thereof, and that both notices appear in supporting documentation.
  69.  * 
  70.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  71.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  72.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  73.  * 
  74.  * Carnegie Mellon requests users of this software to return to
  75.  * 
  76.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  77.  *  School of Computer Science
  78.  *  Carnegie Mellon University
  79.  *  Pittsburgh PA 15213-3890
  80.  * 
  81.  * any improvements or extensions that they make and grant Carnegie Mellon 
  82.  * the rights to redistribute these changes.
  83.  */
  84. /*
  85.  * HISTORY
  86.  * Log:    route.c,v
  87.  * Revision 2.1  92/04/21  17:13:54  rwd
  88.  * BSDSS
  89.  * 
  90.  *
  91.  */
  92.  
  93. /*
  94.  * Copyright (c) 1980, 1986, 1991 Regents of the University of California.
  95.  * All rights reserved.
  96.  *
  97.  * Redistribution and use in source and binary forms, with or without
  98.  * modification, are permitted provided that the following conditions
  99.  * are met:
  100.  * 1. Redistributions of source code must retain the above copyright
  101.  *    notice, this list of conditions and the following disclaimer.
  102.  * 2. Redistributions in binary form must reproduce the above copyright
  103.  *    notice, this list of conditions and the following disclaimer in the
  104.  *    documentation and/or other materials provided with the distribution.
  105.  * 3. All advertising materials mentioning features or use of this software
  106.  *    must display the following acknowledgement:
  107.  *    This product includes software developed by the University of
  108.  *    California, Berkeley and its contributors.
  109.  * 4. Neither the name of the University nor the names of its contributors
  110.  *    may be used to endorse or promote products derived from this software
  111.  *    without specific prior written permission.
  112.  *
  113.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  114.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  115.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  116.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  117.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  118.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  119.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  120.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  121.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  122.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  123.  * SUCH DAMAGE.
  124.  *
  125.  *    @(#)route.c    7.22 (Berkeley) 6/27/91
  126.  */
  127.  
  128. #include <conf.h>
  129.  
  130. #include <sys/param.h>
  131. #include <sys/systm.h>
  132. #include <sys/malloc.h>
  133. #include <sys/mbuf.h>
  134. #include <sys/socket.h>
  135. #include <sys/socketvar.h>
  136. #include <sys/domain.h>
  137. #include <sys/protosw.h>
  138. #include <sys/ioctl.h>
  139. #include <sys/synch.h>
  140.  
  141. #include <net/if.h>
  142. #include <net/route.h>
  143. #include <net/raw_cb.h>
  144.  
  145. #include <netinet/in.h>
  146. #include <netinet/in_var.h>
  147.  
  148. #if NS
  149. #include <netns/ns.h>
  150. #endif
  151. #include <net/netisr.h>
  152.  
  153. #include <net/rtsock_protos.h>
  154. #include <net/radix_protos.h>
  155. #include <netinet/in_protos.h>
  156.  
  157. #define    SA(p) ((struct sockaddr *)(p))
  158.  
  159. /* --- commons from route.h --- */
  160. struct route_cb route_cb = {0};
  161. #if 0 /* obsolete */
  162. struct    mbuf *rthost[RTHASHSIZ] = {NULL};
  163. struct    mbuf *rtnet[RTHASHSIZ] = {NULL};
  164. #endif
  165. struct    rtstat    rtstat = {0};
  166. /* --- commons from route.h --- */
  167.  
  168. int    rttrash = 0;        /* routes not in table but not freed */
  169. struct sockaddr wildcard = {0};    /* zero valued cookie for wildcard searches */
  170. #if 0 /* obsolete */
  171. int    rthashsize = RTHASHSIZ;    /* for netstat, etc. */
  172. #endif
  173.  
  174. static int rtinits_done = 0;
  175. struct radix_node_head *ns_rnhead = 0, *in_rnhead = 0;
  176. struct radix_node *rn_match(), *rn_delete(), *rn_addroute();
  177.  
  178. void
  179. rtinitheads()
  180. {
  181.     if (rtinits_done == 0 &&
  182. #if NS
  183.         rn_inithead(&ns_rnhead, 16, AF_NS) &&
  184. #endif
  185.         rn_inithead(&in_rnhead, 32, AF_INET))
  186.         rtinits_done = 1;
  187. }
  188.  
  189. /*
  190.  * Packet routing routines.
  191.  */
  192. void
  193. rtalloc(ro)
  194.     register struct route *ro;
  195. {
  196.     if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
  197.         return;                 /* XXX */
  198.     ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
  199. }
  200.  
  201. struct rtentry *
  202. rtalloc1(dst, report)
  203.     register struct sockaddr *dst;
  204.     int  report;
  205. {
  206.     register struct radix_node_head *rnh;
  207.     register struct rtentry *rt;
  208.     register struct radix_node *rn;
  209.     struct rtentry *newrt = 0;
  210.     int err = 0, msgtype = RTM_MISS;
  211.     spl_t s = splnet();
  212.  
  213.     for (rnh = radix_node_head; rnh && (dst->sa_family != rnh->rnh_af); )
  214.         rnh = rnh->rnh_next;
  215.     if (rnh && rnh->rnh_treetop &&
  216.         (rn = rn_match((caddr_t)dst, rnh->rnh_treetop)) &&
  217.         ((rn->rn_flags & RNF_ROOT) == 0)) {
  218.         newrt = rt = (struct rtentry *)rn;
  219.         if (report && (rt->rt_flags & RTF_CLONING)) {
  220.             if ((err = rtrequest(RTM_RESOLVE, dst, SA(0),
  221.                           SA(0), 0, &newrt)) ||
  222.                 ((rt->rt_flags & RTF_XRESOLVE)
  223.                   && (msgtype = RTM_RESOLVE))) /* intended! */
  224.                 goto miss;
  225.         } else
  226.             rt->rt_refcnt++;
  227.     } else {
  228.         rtstat.rts_unreach++;
  229.     miss:    if (report)
  230.             rt_missmsg(msgtype, dst, SA(0), SA(0), SA(0), 0, err);
  231.     }
  232.     splx(s);
  233.     return (newrt);
  234. }
  235.  
  236. void
  237. rtfree(rt)
  238.     register struct rtentry *rt;
  239. {
  240.     if (rt == 0)
  241.         panic("rtfree");
  242.     rt->rt_refcnt--;
  243.     if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
  244.         rttrash--;
  245.         if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
  246.             panic ("rtfree 2");
  247.         Free(rt);
  248.     }
  249. }
  250.  
  251. /*
  252.  * Force a routing table entry to the specified
  253.  * destination to go through the given gateway.
  254.  * Normally called as a result of a routing redirect
  255.  * message from the network layer.
  256.  *
  257.  * N.B.: must be called at splnet
  258.  *
  259.  */
  260. void
  261. rtredirect(dst, gateway, netmask, flags, src, rtp)
  262.     struct sockaddr *dst, *gateway, *netmask, *src;
  263.     int flags;
  264.     struct rtentry **rtp;
  265. {
  266.     register struct rtentry *rt = NULL;
  267.     int error = 0;
  268.     short *stat = 0;
  269.  
  270.     /* verify the gateway is directly reachable */
  271.     if (ifa_ifwithnet(gateway) == 0) {
  272.         error = ENETUNREACH;
  273.         goto done;
  274.     }
  275.     rt = rtalloc1(dst, 0);
  276.     /*
  277.      * If the redirect isn't from our current router for this dst,
  278.      * it's either old or wrong.  If it redirects us to ourselves,
  279.      * we have a routing loop, perhaps as a result of an interface
  280.      * going down recently.
  281.      */
  282. #define    equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
  283.     if (!(flags & RTF_DONE) && rt && !equal(src, rt->rt_gateway))
  284.         error = EINVAL;
  285.     else if (ifa_ifwithaddr(gateway))
  286.         error = EHOSTUNREACH;
  287.     if (error)
  288.         goto done;
  289.     /*
  290.      * Create a new entry if we just got back a wildcard entry
  291.      * or the the lookup failed.  This is necessary for hosts
  292.      * which use routing redirects generated by smart gateways
  293.      * to dynamically build the routing tables.
  294.      */
  295.     if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
  296.         goto create;
  297.     /*
  298.      * Don't listen to the redirect if it's
  299.      * for a route to an interface. 
  300.      */
  301.     if (rt->rt_flags & RTF_GATEWAY) {
  302.         if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
  303.             /*
  304.              * Changing from route to net => route to host.
  305.              * Create new route, rather than smashing route to net.
  306.              */
  307.         create:
  308.             flags |=  RTF_GATEWAY | RTF_DYNAMIC;
  309.             error = rtrequest((int)RTM_ADD, dst, gateway,
  310.                     SA(0), flags,
  311.                     (struct rtentry **)0);
  312.             stat = &rtstat.rts_dynamic;
  313.         } else {
  314.             /*
  315.              * Smash the current notion of the gateway to
  316.              * this destination.  Should check about netmask!!!
  317.              */
  318.             if (gateway->sa_len <= rt->rt_gateway->sa_len) {
  319.                 Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
  320.                 rt->rt_flags |= RTF_MODIFIED;
  321.                 flags |= RTF_MODIFIED;
  322.                 stat = &rtstat.rts_newgateway;
  323.             } else
  324.                 error = ENOSPC;
  325.         }
  326.     } else
  327.         error = EHOSTUNREACH;
  328. done:
  329.     if (rt) {
  330.         if (rtp && !error)
  331.             *rtp = rt;
  332.         else
  333.             rtfree(rt);
  334.     }
  335.     if (error)
  336.         rtstat.rts_badredirect++;
  337.     else
  338.         (stat && (*stat)++);
  339.     rt_missmsg(RTM_REDIRECT, dst, gateway, netmask, src, flags, error);
  340. }
  341.  
  342. /*
  343. * Routing table ioctl interface.
  344. */
  345. int
  346. rtioctl(req, data)
  347.     int req;
  348.     caddr_t data;
  349. {
  350. #if !defined(COMPAT_43) && !defined(AMITCP)
  351.     return (EOPNOTSUPP);
  352. #else
  353.     register struct ortentry *entry = (struct ortentry *)data;
  354.     int error;
  355.     struct sockaddr netmask;
  356.  
  357.     if (req == SIOCADDRT)
  358.         req = RTM_ADD;
  359.     else if (req == SIOCDELRT)
  360.         req = RTM_DELETE;
  361.     else
  362.         return (EINVAL);
  363.  
  364. #ifndef AMITCP /* no protection on AmigaOS */
  365.     if (error = suser(p->p_ucred, &p->p_acflag))
  366.         return (error);
  367. #endif /* AMITCP */
  368.  
  369. #if BYTE_ORDER != BIG_ENDIAN
  370.     if (entry->rt_dst.sa_family == 0 && entry->rt_dst.sa_len < 16) {
  371.         entry->rt_dst.sa_family = entry->rt_dst.sa_len;
  372.         entry->rt_dst.sa_len = 16;
  373.     }
  374.     if (entry->rt_gateway.sa_family == 0 && entry->rt_gateway.sa_len < 16) {
  375.         entry->rt_gateway.sa_family = entry->rt_gateway.sa_len;
  376.         entry->rt_gateway.sa_len = 16;
  377.     }
  378. #else
  379.     if (entry->rt_dst.sa_len == 0)
  380.         entry->rt_dst.sa_len = 16;
  381.     if (entry->rt_gateway.sa_len == 0)
  382.         entry->rt_gateway.sa_len = 16;
  383. #endif
  384.     if ((entry->rt_flags & RTF_HOST) == 0)
  385.         switch (entry->rt_dst.sa_family) {
  386. #if INET
  387.         case AF_INET:
  388.             {
  389. #if 0
  390.               /*
  391.                * Should not use icmpmask, since it is accessed from
  392.                * different tasks!
  393.                */
  394.                 extern struct sockaddr_in icmpmask;
  395.                 struct sockaddr_in *dst_in = 
  396.                     (struct sockaddr_in *)&entry->rt_dst;
  397.  
  398.                 in_sockmaskof(dst_in->sin_addr, &icmpmask);
  399.                 netmask = (struct sockaddr *)&icmpmask;
  400. #else
  401.                 in_sockmaskof(((struct sockaddr_in *)&entry->rt_dst)->sin_addr, (struct sockaddr_in *)&netmask);
  402. #endif
  403.             }
  404.             break;
  405. #endif
  406. #if NS
  407.         case AF_NS:
  408.             {
  409.                 extern struct sockaddr_ns ns_netmask;
  410.                 netmask = (struct sockaddr)ns_netmask;
  411.             }
  412. #endif
  413.         }
  414.     error = rtrequest(req, &(entry->rt_dst), &(entry->rt_gateway),
  415.               (entry->rt_flags & RTF_HOST) ? NULL : &netmask,
  416.               entry->rt_flags, (struct rtentry **)0);
  417.     rt_missmsg((req == RTM_ADD ? RTM_OLDADD : RTM_OLDDEL),
  418.            &(entry->rt_dst), &(entry->rt_gateway),
  419.            (entry->rt_flags & RTF_HOST) ? NULL : &netmask,
  420.            SA(0), entry->rt_flags, error);
  421.     return (error);
  422. #endif
  423. }
  424.  
  425. struct ifaddr *
  426. ifa_ifwithroute(flags, dst, gateway)
  427. int    flags;
  428. struct sockaddr    *dst, *gateway;
  429. {
  430.     register struct ifaddr *ifa;
  431.     if ((flags & RTF_GATEWAY) == 0) {
  432.         /*
  433.          * If we are adding a route to an interface,
  434.          * and the interface is a pt to pt link
  435.          * we should search for the destination
  436.          * as our clue to the interface.  Otherwise
  437.          * we can use the local address.
  438.          */
  439.         ifa = 0;
  440.         if (flags & RTF_HOST) 
  441.             ifa = ifa_ifwithdstaddr(dst);
  442.         if (ifa == 0)
  443.             ifa = ifa_ifwithaddr(gateway);
  444.     } else {
  445.         /*
  446.          * If we are adding a route to a remote net
  447.          * or host, the gateway may still be on the
  448.          * other end of a pt to pt link.
  449.          */
  450.         ifa = ifa_ifwithdstaddr(gateway);
  451.     }
  452.     if (ifa == 0)
  453.         ifa = ifa_ifwithnet(gateway);
  454.     if (ifa == 0) {
  455.         struct rtentry *rt = rtalloc1(dst, 0);
  456.         if (rt == 0)
  457.             return (0);
  458.         rt->rt_refcnt--;
  459.         if ((ifa = rt->rt_ifa) == 0)
  460.             return (0);
  461.     }
  462.     if (ifa->ifa_addr->sa_family != dst->sa_family) {
  463.         struct ifaddr *oifa = ifa, *ifaof_ifpforaddr();
  464.         ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
  465.         if (ifa == 0)
  466.             ifa = oifa;
  467.     }
  468.     return (ifa);
  469. }
  470.  
  471. #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  472.  
  473. int
  474. rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
  475.     int req, flags;
  476.     struct sockaddr *dst, *gateway, *netmask;
  477.     struct rtentry **ret_nrt;
  478. {
  479.     int len, error = 0;
  480.     spl_t s = splnet();
  481.     register struct rtentry *rt;
  482.     register struct radix_node *rn;
  483.     register struct radix_node_head *rnh;
  484.     struct ifaddr *ifa, *ifa_ifwithdstaddr();
  485.     struct sockaddr *ndst;
  486.     u_char af = dst->sa_family;
  487. #define senderr(x) { error = x ; goto bad; }
  488.  
  489.     if (rtinits_done == 0)
  490.         rtinitheads();
  491.     for (rnh = radix_node_head; rnh && (af != rnh->rnh_af); )
  492.         rnh = rnh->rnh_next;
  493.     if (rnh == 0)
  494.         senderr(ESRCH);
  495.     if (flags & RTF_HOST)
  496.         netmask = 0;
  497.     switch (req) {
  498.     case RTM_DELETE:
  499.         if (ret_nrt && (rt = *ret_nrt)) {
  500.             RTFREE(rt);
  501.             *ret_nrt = 0;
  502.         }
  503.         if ((rn = rn_delete((caddr_t)dst, (caddr_t)netmask, 
  504.                     rnh->rnh_treetop)) == 0)
  505.             senderr(ESRCH);
  506.         if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
  507.             panic ("rtrequest delete");
  508.         rt = (struct rtentry *)rn;
  509.         rt->rt_flags &= ~RTF_UP;
  510.         if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
  511.             ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
  512.         rttrash++;
  513.         if (rt->rt_refcnt <= 0)
  514.             rtfree(rt);
  515.         break;
  516.  
  517.     case RTM_RESOLVE:
  518.         if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
  519.             senderr(EINVAL);
  520.         ifa = rt->rt_ifa;
  521.         flags = rt->rt_flags & ~RTF_CLONING;
  522.         gateway = rt->rt_gateway;
  523.         if ((netmask = rt->rt_genmask) == 0)
  524.             flags |= RTF_HOST;
  525.         goto makeroute;
  526.  
  527.     case RTM_ADD:
  528.         if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
  529.             senderr(ENETUNREACH);
  530.     makeroute:
  531.         len = sizeof (*rt) + ROUNDUP(gateway->sa_len)
  532.             + ROUNDUP(dst->sa_len);
  533.         R_Malloc(rt, struct rtentry *, len);
  534.         if (rt == 0)
  535.             senderr(ENOBUFS);
  536.         Bzero(rt, len);
  537.         ndst = (struct sockaddr *)(rt + 1);
  538.         if (netmask) {
  539.             rt_maskedcopy(dst, ndst, netmask);
  540.         } else
  541.             Bcopy(dst, ndst, dst->sa_len);
  542.         rn = rn_addroute((caddr_t)ndst, (caddr_t)netmask,
  543.                     rnh->rnh_treetop, rt->rt_nodes);
  544.         if (rn == 0) {
  545.             Free(rt);
  546.             senderr(EEXIST);
  547.         }
  548.         rt->rt_ifa = ifa;
  549.         rt->rt_ifp = ifa->ifa_ifp;
  550.         rt->rt_flags = RTF_UP | flags;
  551.         rt->rt_gateway = (struct sockaddr *)
  552.                     (rn->rn_key + ROUNDUP(dst->sa_len));
  553.         Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
  554.         if (req == RTM_RESOLVE)
  555.             rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
  556.         if (ifa->ifa_rtrequest)
  557.             ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
  558.         if (ret_nrt) {
  559.             *ret_nrt = rt;
  560.             rt->rt_refcnt++;
  561.         }
  562.         break;
  563.     }
  564. bad:
  565.     splx(s);
  566.     return (error);
  567. }
  568.  
  569. void
  570. rt_maskedcopy(src, dst, netmask)
  571. struct sockaddr *src, *dst, *netmask;
  572. {
  573.     register u_char *cp1 = (u_char *)src;
  574.     register u_char *cp2 = (u_char *)dst;
  575.     register u_char *cp3 = (u_char *)netmask;
  576.     u_char *cplim = cp2 + *cp3;
  577.     u_char *cplim2 = cp2 + *cp1;
  578.  
  579.     *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
  580.     cp3 += 2;
  581.     if (cplim > cplim2)
  582.         cplim = cplim2;
  583.     while (cp2 < cplim)
  584.         *cp2++ = *cp1++ & *cp3++;
  585.     if (cp2 < cplim2)
  586.         bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
  587. }
  588. /*
  589.  * Set up a routing table entry, normally
  590.  * for an interface.
  591.  */
  592. int
  593. rtinit(ifa, cmd, flags)
  594.     register struct ifaddr *ifa;
  595.     int cmd, flags;
  596. {
  597.     register struct rtentry *rt;
  598.     register struct sockaddr *dst;
  599.     register struct sockaddr *deldst;
  600.     struct mbuf *m = 0;
  601.     int error;
  602.  
  603.     dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
  604.     if (ifa->ifa_flags & IFA_ROUTE) {
  605.         if ((rt = ifa->ifa_rt) && (rt->rt_flags & RTF_UP) == 0) {
  606.             RTFREE(rt);
  607.             ifa->ifa_rt = 0;
  608.         }
  609.     }
  610.     if (cmd == RTM_DELETE) {
  611.         if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
  612.             m = m_get(M_WAIT, MT_SONAME);
  613.             deldst = mtod(m, struct sockaddr *);
  614.             rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
  615.             dst = deldst;
  616.         }
  617.         if (rt = rtalloc1(dst, 0)) {
  618.             rt->rt_refcnt--;
  619.             if (rt->rt_ifa != ifa) {
  620.                 if (m)
  621.                     (void) m_free(m);
  622.                 return (flags & RTF_HOST ? EHOSTUNREACH
  623.                             : ENETUNREACH);
  624.             }
  625.         }
  626.     }
  627.     error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
  628.             flags | ifa->ifa_flags, &ifa->ifa_rt);
  629.     if (m)
  630.         (void) m_free(m);
  631.     if (cmd == RTM_ADD && error == 0 && (rt = ifa->ifa_rt)
  632.                         && rt->rt_ifa != ifa) {
  633.         rt->rt_ifa = ifa;
  634.         rt->rt_ifp = ifa->ifa_ifp;
  635.     }
  636.     return (error);
  637. }
  638.