home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / routed / startup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  14.6 KB  |  489 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. /*static char sccsid[] = "from: @(#)startup.c    5.19 (Berkeley) 2/28/91";*/
  36. static char rcsid[] = "$Id: startup.c,v 1.1 1994/05/23 09:08:12 rzsfl Exp rzsfl $";
  37. #endif /* not lint */
  38.  
  39. /*
  40.  * Routing Table Management Daemon
  41.  */
  42. #include "defs.h"
  43. #include <sys/ioctl.h>
  44. #include <net/if.h>
  45. #include <syslog.h>
  46. #include <stdlib.h>
  47. #include "pathnames.h"
  48.  
  49. struct    interface *ifnet;
  50. struct    interface **ifnext = &ifnet;
  51. int    lookforinterfaces = 1;
  52. int    externalinterfaces = 0;        /* # of remote and local interfaces */
  53. int    foundloopback;            /* valid flag for loopaddr */
  54. struct    sockaddr loopaddr;        /* our address on loopback */
  55.  
  56. /*
  57.  * Find the network interfaces which have configured themselves.
  58.  * If the interface is present but not yet up (for example an
  59.  * ARPANET IMP), set the lookforinterfaces flag so we'll
  60.  * come back later and look again.
  61.  */
  62. ifinit()
  63. {
  64.     struct interface ifs, *ifp;
  65.     int s;
  66.     char buf[BUFSIZ], *cp, *cplim;
  67.         struct ifconf ifc;
  68.         struct ifreq ifreq, *ifr;
  69.         struct sockaddr_in *sin;
  70.     u_long i;
  71.  
  72.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  73.         syslog(LOG_ERR, "socket: %m");
  74.         close(s);
  75.                 return;
  76.     }
  77.         ifc.ifc_len = sizeof (buf);
  78.         ifc.ifc_buf = buf;
  79.         if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  80.                 syslog(LOG_ERR, "ioctl (get interface configuration)");
  81.         close(s);
  82.                 return;
  83.         }
  84.         ifr = ifc.ifc_req;
  85.     lookforinterfaces = 0;
  86. #ifdef RTM_ADD
  87. #define max(a, b) (a > b ? a : b)
  88. #define size(p)    max((p).sa_len, sizeof(p))
  89. #else
  90. #define size(p) (sizeof (p))
  91. #endif
  92.     cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
  93.     for (cp = buf; cp < cplim;
  94.             cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
  95.         ifr = (struct ifreq *)cp;
  96.         bzero((char *)&ifs, sizeof(ifs));
  97.         ifs.int_addr = ifr->ifr_addr;
  98.         ifreq = *ifr;
  99.                 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  100.                         syslog(LOG_ERR, "%s: ioctl (get interface flags)",
  101.                 ifr->ifr_name);
  102.                         continue;
  103.                 }
  104.         ifs.int_flags =
  105.             ifreq.ifr_flags | IFF_INTERFACE;
  106.         if ((ifs.int_flags & IFF_UP) == 0 ||
  107.             ifr->ifr_addr.sa_family == AF_UNSPEC) {
  108.             lookforinterfaces = 1;
  109.             continue;
  110.         }
  111.         /* argh, this'll have to change sometime */
  112.         if (ifs.int_addr.sa_family != AF_INET)
  113.             continue;
  114.                 if (ifs.int_flags & IFF_POINTOPOINT) {
  115.                         if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
  116.                                 syslog(LOG_ERR, "%s: ioctl (get dstaddr)",
  117.                     ifr->ifr_name);
  118.                                 continue;
  119.             }
  120.             if (ifr->ifr_addr.sa_family == AF_UNSPEC) {
  121.                 lookforinterfaces = 1;
  122.                 continue;
  123.             }
  124.             ifs.int_dstaddr = ifreq.ifr_dstaddr;
  125.         }
  126.         /*
  127.          * already known to us?
  128.          * This allows multiple point-to-point links
  129.          * to share a source address (possibly with one
  130.          * other link), but assumes that there will not be
  131.          * multiple links with the same destination address.
  132.          */
  133.         if (ifs.int_flags & IFF_POINTOPOINT) {
  134.             if (if_ifwithdstaddr(&ifs.int_dstaddr))
  135.                 continue;
  136.         } else if (if_ifwithaddr(&ifs.int_addr))
  137.             continue;
  138.         if (ifs.int_flags & IFF_LOOPBACK) {
  139.             ifs.int_flags |= IFF_PASSIVE;
  140.             foundloopback = 1;
  141.             loopaddr = ifs.int_addr;
  142.             for (ifp = ifnet; ifp; ifp = ifp->int_next)
  143.                 if (ifp->int_flags & IFF_POINTOPOINT)
  144.                 add_ptopt_localrt(ifp);
  145.         }
  146.                 if (ifs.int_flags & IFF_BROADCAST) {
  147.                         if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
  148.                                 syslog(LOG_ERR, "%s: ioctl (get broadaddr)",
  149.                     ifr->ifr_name);
  150.                                 continue;
  151.                         }
  152. #ifndef sun
  153.             ifs.int_broadaddr = ifreq.ifr_broadaddr;
  154. #else
  155.             ifs.int_broadaddr = ifreq.ifr_addr;
  156. #endif
  157.         }
  158. #ifdef SIOCGIFMETRIC
  159.         if (ioctl(s, SIOCGIFMETRIC, (char *)&ifreq) < 0) {
  160.             syslog(LOG_ERR, "%s: ioctl (get metric)",
  161.                 ifr->ifr_name);
  162.             ifs.int_metric = 0;
  163.         } else
  164.             ifs.int_metric = ifreq.ifr_metric;
  165. #else
  166.         ifs.int_metric = 0;
  167. #endif
  168.         /*
  169.          * Use a minimum metric of one;
  170.          * treat the interface metric (default 0)
  171.          * as an increment to the hop count of one.
  172.          */
  173.         ifs.int_metric++;
  174.         if (ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
  175.             syslog(LOG_ERR, "%s: ioctl (get netmask)",
  176.                 ifr->ifr_name);
  177.             continue;
  178.         }
  179.         sin = (struct sockaddr_in *)&ifreq.ifr_addr;
  180.         ifs.int_subnetmask = ntohl(sin->sin_addr.s_addr);
  181.         sin = (struct sockaddr_in *)&ifs.int_addr;
  182.         i = ntohl(sin->sin_addr.s_addr);
  183.         if (IN_CLASSA(i))
  184.             ifs.int_netmask = IN_CLASSA_NET;
  185.         else if (IN_CLASSB(i))
  186.             ifs.int_netmask = IN_CLASSB_NET;
  187.         else
  188.             ifs.int_netmask = IN_CLASSC_NET;
  189.         ifs.int_net = i & ifs.int_netmask;
  190.         ifs.int_subnet = i & ifs.int_subnetmask;
  191.         if (ifs.int_subnetmask != ifs.int_netmask)
  192.             ifs.int_flags |= IFF_SUBNET;
  193.         ifp = (struct interface *)malloc(sizeof (struct interface));
  194.         if (ifp == 0) {
  195.             printf("routed: out of memory\n");
  196.             break;
  197.         }
  198.         *ifp = ifs;
  199.         /*
  200.          * Count the # of directly connected networks
  201.          * and point to point links which aren't looped
  202.          * back to ourself.  This is used below to
  203.          * decide if we should be a routing ``supplier''.
  204.          */
  205.         if ((ifs.int_flags & IFF_LOOPBACK) == 0 &&
  206.             ((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
  207.             if_ifwithaddr(&ifs.int_dstaddr) == 0))
  208.             externalinterfaces++;
  209.         /*
  210.          * If we have a point-to-point link, we want to act
  211.          * as a supplier even if it's our only interface,
  212.          * as that's the only way our peer on the other end
  213.          * can tell that the link is up.
  214.          */
  215.         if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0)
  216.             supplier = 1;
  217.         ifp->int_name = malloc(strlen(ifr->ifr_name) + 1);
  218.         if (ifp->int_name == 0) {
  219.             fprintf(stderr, "routed: ifinit: out of memory\n");
  220.             syslog(LOG_ERR, "routed: ifinit: out of memory\n");
  221.             close(s);
  222.             return;
  223.         }
  224.         strcpy(ifp->int_name, ifr->ifr_name);
  225.         *ifnext = ifp;
  226.         ifnext = &ifp->int_next;
  227.         traceinit(ifp);
  228.         addrouteforif(ifp);
  229.     }
  230.     if (externalinterfaces > 1 && supplier < 0)
  231.         supplier = 1;
  232.     close(s);
  233. }
  234.  
  235. /*
  236.  * Add route for interface if not currently installed.
  237.  * Create route to other end if a point-to-point link,
  238.  * otherwise a route to this (sub)network.
  239.  * INTERNET SPECIFIC.
  240.  */
  241. addrouteforif(ifp)
  242.     register struct interface *ifp;
  243. {
  244.     struct sockaddr_in net;
  245.     struct sockaddr *dst;
  246.     int state;
  247.     register struct rt_entry *rt;
  248.  
  249.     if (ifp->int_flags & IFF_POINTOPOINT)
  250.         dst = &ifp->int_dstaddr;
  251.     else {
  252.         bzero((char *)&net, sizeof (net));
  253.         net.sin_family = AF_INET;
  254.         net.sin_addr = inet_makeaddr(ifp->int_subnet, INADDR_ANY);
  255.         dst = (struct sockaddr *)&net;
  256.     }
  257.     rt = rtfind(dst);
  258.     if (rt &&
  259.         (rt->rt_state & (RTS_INTERFACE | RTS_INTERNAL)) == RTS_INTERFACE)
  260.         return;
  261.     if (rt)
  262.         rtdelete(rt);
  263.     /*
  264.      * If interface on subnetted network,
  265.      * install route to network as well.
  266.      * This is meant for external viewers.
  267.      */
  268.     if ((ifp->int_flags & (IFF_SUBNET|IFF_POINTOPOINT)) == IFF_SUBNET) {
  269.         struct in_addr subnet;
  270.  
  271.         subnet = net.sin_addr;
  272.         net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
  273.         rt = rtfind(dst);
  274.         if (rt == 0)
  275.             rtadd(dst, &ifp->int_addr, ifp->int_metric,
  276.                 ((ifp->int_flags & (IFF_INTERFACE|IFF_REMOTE)) |
  277.                 RTS_PASSIVE | RTS_INTERNAL | RTS_SUBNET));
  278.         else if ((rt->rt_state & (RTS_INTERNAL|RTS_SUBNET)) == 
  279.             (RTS_INTERNAL|RTS_SUBNET) &&
  280.             ifp->int_metric < rt->rt_metric)
  281.             rtchange(rt, &rt->rt_router, ifp->int_metric);
  282.         net.sin_addr = subnet;
  283.     }
  284.     if (ifp->int_transitions++ > 0)
  285.         syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
  286.     state = ifp->int_flags &
  287.         (IFF_INTERFACE | IFF_PASSIVE | IFF_REMOTE | IFF_SUBNET);
  288.     if (ifp->int_flags & IFF_POINTOPOINT &&
  289.         (ntohl(((struct sockaddr_in *)&ifp->int_dstaddr)->sin_addr.s_addr) &
  290.         ifp->int_netmask) != ifp->int_net)
  291.         state &= ~RTS_SUBNET;
  292.     if (ifp->int_flags & IFF_LOOPBACK)
  293.         state |= RTS_EXTERNAL;
  294.     rtadd(dst, &ifp->int_addr, ifp->int_metric, state);
  295.     if (ifp->int_flags & IFF_POINTOPOINT && foundloopback)
  296.         add_ptopt_localrt(ifp);
  297. }
  298.  
  299. /*
  300.  * Add route to local end of point-to-point using loopback.
  301.  * If a route to this network is being sent to neighbors on other nets,
  302.  * mark this route as subnet so we don't have to propagate it too.
  303.  */
  304. add_ptopt_localrt(ifp)
  305.     register struct interface *ifp;
  306. {
  307.     struct rt_entry *rt;
  308.     struct sockaddr *dst;
  309.     struct sockaddr_in net;
  310.     int state;
  311.  
  312.     state = RTS_INTERFACE | RTS_PASSIVE;
  313.  
  314.     /* look for route to logical network */
  315.     bzero((char *)&net, sizeof (net));
  316.     net.sin_family = AF_INET;
  317.     net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
  318.     dst = (struct sockaddr *)&net;
  319.     rt = rtfind(dst);
  320.     if (rt && rt->rt_state & RTS_INTERNAL)
  321.         state |= RTS_SUBNET;
  322.  
  323.     dst = &ifp->int_addr;
  324.     if (rt = rtfind(dst)) {
  325.         if (rt && rt->rt_state & RTS_INTERFACE)
  326.             return;
  327.         rtdelete(rt);
  328.     }
  329.     rtadd(dst, &loopaddr, 1, state);
  330. }
  331.  
  332. /*
  333.  * As a concession to the ARPANET we read a list of gateways
  334.  * from /etc/gateways and add them to our tables.  This file
  335.  * exists at each ARPANET gateway and indicates a set of ``remote''
  336.  * gateways (i.e. a gateway which we can't immediately determine
  337.  * if it's present or not as we can do for those directly connected
  338.  * at the hardware level).  If a gateway is marked ``passive''
  339.  * in the file, then we assume it doesn't have a routing process
  340.  * of our design and simply assume it's always present.  Those
  341.  * not marked passive are treated as if they were directly
  342.  * connected -- they're added into the interface list so we'll
  343.  * send them routing updates.
  344.  *
  345.  * PASSIVE ENTRIES AREN'T NEEDED OR USED ON GATEWAYS RUNNING EGP.
  346.  */
  347. gwkludge()
  348. {
  349.     struct sockaddr_in dst, gate;
  350.     FILE *fp;
  351.     char *type, *dname, *gname, *qual, buf[BUFSIZ];
  352.     struct interface *ifp;
  353.     int metric, n;
  354.     struct rt_entry route;
  355.  
  356.     fp = fopen(_PATH_GATEWAYS, "r");
  357.     if (fp == NULL)
  358.         return;
  359.     qual = buf;
  360.     dname = buf + 64;
  361.     gname = buf + ((BUFSIZ - 64) / 3);
  362.     type = buf + (((BUFSIZ - 64) * 2) / 3);
  363.     bzero((char *)&dst, sizeof (dst));
  364.     bzero((char *)&gate, sizeof (gate));
  365.     bzero((char *)&route, sizeof(route));
  366. /* format: {net | host} XX gateway XX metric DD [passive | external]\n */
  367. #define    readentry(fp) \
  368.     fscanf((fp), "%s %s gateway %s metric %d %s\n", \
  369.         type, dname, gname, &metric, qual)
  370.     for (;;) {
  371.         if ((n = readentry(fp)) == EOF)
  372.             break;
  373.         if (!getnetorhostname(type, dname, &dst))
  374.             continue;
  375.         if (!gethostnameornumber(gname, &gate))
  376.             continue;
  377.         if (metric == 0)            /* XXX */
  378.             metric = 1;
  379.         if (strcmp(qual, "passive") == 0) {
  380.             /*
  381.              * Passive entries aren't placed in our tables,
  382.              * only the kernel's, so we don't copy all of the
  383.              * external routing information within a net.
  384.              * Internal machines should use the default
  385.              * route to a suitable gateway (like us).
  386.              */
  387.             route.rt_dst = *(struct sockaddr *) &dst;
  388.             route.rt_router = *(struct sockaddr *) &gate;
  389.             route.rt_flags = RTF_UP;
  390.             if (strcmp(type, "host") == 0)
  391.                 route.rt_flags |= RTF_HOST;
  392.             if (metric)
  393.                 route.rt_flags |= RTF_GATEWAY;
  394.             (void) ioctl(s, SIOCADDRT, (char *)&route.rt_rt);
  395.             continue;
  396.         }
  397.         if (strcmp(qual, "external") == 0) {
  398.             /*
  399.              * Entries marked external are handled
  400.              * by other means, e.g. EGP,
  401.              * and are placed in our tables only
  402.              * to prevent overriding them
  403.              * with something else.
  404.              */
  405.             rtadd(&dst, &gate, metric, RTS_EXTERNAL|RTS_PASSIVE);
  406.             continue;
  407.         }
  408.         /* assume no duplicate entries */
  409.         externalinterfaces++;
  410.         ifp = (struct interface *)malloc(sizeof (*ifp));
  411.         bzero((char *)ifp, sizeof (*ifp));
  412.         ifp->int_flags = IFF_REMOTE;
  413.         /* can't identify broadcast capability */
  414.         ifp->int_net = inet_netof(dst.sin_addr);
  415.         if (strcmp(type, "host") == 0) {
  416.             ifp->int_flags |= IFF_POINTOPOINT;
  417.             ifp->int_dstaddr = *((struct sockaddr *)&dst);
  418.         }
  419.         ifp->int_addr = *((struct sockaddr *)&gate);
  420.         ifp->int_metric = metric;
  421.         ifp->int_next = ifnet;
  422.         ifnet = ifp;
  423.         addrouteforif(ifp);
  424.     }
  425.     fclose(fp);
  426. }
  427.  
  428. getnetorhostname(type, name, sin)
  429.     char *type, *name;
  430.     struct sockaddr_in *sin;
  431. {
  432.  
  433.     if (strcmp(type, "net") == 0) {
  434.         struct netent *np = getnetbyname(name);
  435.         int n;
  436.  
  437.         if (np == 0)
  438.             n = inet_network(name);
  439.         else {
  440.             if (np->n_addrtype != AF_INET)
  441.                 return (0);
  442.             n = np->n_net;
  443.             /*
  444.              * getnetbyname returns right-adjusted value.
  445.              */
  446.             if (n < 128)
  447.                 n <<= IN_CLASSA_NSHIFT;
  448.             else if (n < 65536)
  449.                 n <<= IN_CLASSB_NSHIFT;
  450.             else
  451.                 n <<= IN_CLASSC_NSHIFT;
  452.         }
  453.         sin->sin_family = AF_INET;
  454.         sin->sin_addr = inet_makeaddr(n, INADDR_ANY);
  455.         return (1);
  456.     }
  457.     if (strcmp(type, "host") == 0) {
  458.         struct hostent *hp = gethostbyname(name);
  459.  
  460.         if (hp == 0)
  461.             sin->sin_addr.s_addr = inet_addr(name);
  462.         else {
  463.             if (hp->h_addrtype != AF_INET)
  464.                 return (0);
  465.             bcopy(hp->h_addr, &sin->sin_addr, hp->h_length);
  466.         }
  467.         sin->sin_family = AF_INET;
  468.         return (1);
  469.     }
  470.     return (0);
  471. }
  472.  
  473. gethostnameornumber(name, sin)
  474.     char *name;
  475.     struct sockaddr_in *sin;
  476. {
  477.     struct hostent *hp;
  478.  
  479.     hp = gethostbyname(name);
  480.     if (hp) {
  481.         bcopy(hp->h_addr, &sin->sin_addr, hp->h_length);
  482.         sin->sin_family = hp->h_addrtype;
  483.         return (1);
  484.     }
  485.     sin->sin_addr.s_addr = inet_addr(name);
  486.     sin->sin_family = AF_INET;
  487.     return (sin->sin_addr.s_addr != -1);
  488. }
  489.