124322Ssklower /* 235551Sbostic * Copyright (c) 1985 The Regents of the University of California. 335551Sbostic * All rights reserved. 424322Ssklower * 535551Sbostic * This file includes significant work done at Cornell University by 635551Sbostic * Bill Nesheim. That work included by permission. 735551Sbostic * 835551Sbostic * Redistribution and use in source and binary forms are permitted 935551Sbostic * provided that the above copyright notice and this paragraph are 1035551Sbostic * duplicated in all such forms and that any documentation, 1135551Sbostic * advertising materials, and other materials related to such 1235551Sbostic * distribution and use acknowledge that the software was developed 1335551Sbostic * by the University of California, Berkeley. The name of the 1435551Sbostic * University may not be used to endorse or promote products derived 1535551Sbostic * from this software without specific prior written permission. 1635551Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1735551Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1835551Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1924322Ssklower */ 2024322Ssklower 2124312Ssklower #ifndef lint 22*38687Ssklower static char sccsid[] = "@(#)startup.c 5.8 (Berkeley) 08/21/89"; 2335551Sbostic #endif /* not lint */ 2424312Ssklower 2524312Ssklower /* 2624312Ssklower * Routing Table Management Daemon 2724312Ssklower */ 2824312Ssklower #include "defs.h" 2924312Ssklower #include <sys/ioctl.h> 3024312Ssklower #include <net/if.h> 3124312Ssklower #include <nlist.h> 3224317Ssklower #include <syslog.h> 3324312Ssklower 3424312Ssklower struct interface *ifnet; 3524312Ssklower int lookforinterfaces = 1; 3624312Ssklower int performnlist = 1; 3726170Ssklower int gateway = 0; 3824312Ssklower int externalinterfaces = 0; /* # of remote and local interfaces */ 3924312Ssklower char ether_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 4024312Ssklower 4124312Ssklower 4224312Ssklower /* 4324317Ssklower * Find the network interfaces which have configured themselves. 4424317Ssklower * If the interface is present but not yet up (for example an 4524312Ssklower * ARPANET IMP), set the lookforinterfaces flag so we'll 4624312Ssklower * come back later and look again. 4724312Ssklower */ 4824312Ssklower ifinit() 4924312Ssklower { 5024317Ssklower struct interface ifs, *ifp; 51*38687Ssklower int s; 5224317Ssklower struct ifconf ifc; 53*38687Ssklower char buf[BUFSIZ], *cp, *cplim; 5424317Ssklower struct ifreq ifreq, *ifr; 5524317Ssklower u_long i; 5624312Ssklower 5724317Ssklower if ((s = socket(AF_NS, SOCK_DGRAM, 0)) < 0) { 5824317Ssklower syslog(LOG_ERR, "socket: %m"); 5924317Ssklower exit(1); 6024312Ssklower } 6124317Ssklower ifc.ifc_len = sizeof (buf); 6224317Ssklower ifc.ifc_buf = buf; 6324317Ssklower if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 6424317Ssklower syslog(LOG_ERR, "ioctl (get interface configuration)"); 6524317Ssklower close(s); 6636437Skarels exit(1); 6724317Ssklower } 6824317Ssklower ifr = ifc.ifc_req; 6924312Ssklower lookforinterfaces = 0; 70*38687Ssklower #ifdef RTM_ADD 71*38687Ssklower #define max(a, b) (a > b ? a : b) 72*38687Ssklower #define size(p) max((p).sa_len, sizeof(p)) 73*38687Ssklower #else 74*38687Ssklower #define size(p) (sizeof (p)) 75*38687Ssklower #endif 76*38687Ssklower cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ 77*38687Ssklower for (cp = buf; cp < cplim; 78*38687Ssklower cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { 7924317Ssklower bzero((char *)&ifs, sizeof(ifs)); 8024317Ssklower ifs.int_addr = ifr->ifr_addr; 8124317Ssklower ifreq = *ifr; 8224317Ssklower if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 8324317Ssklower syslog(LOG_ERR, "ioctl (get interface flags)"); 8424317Ssklower continue; 8524317Ssklower } 8624317Ssklower ifs.int_flags = ifreq.ifr_flags | IFF_INTERFACE; 8724317Ssklower if ((ifs.int_flags & IFF_UP) == 0 || 8824317Ssklower ifr->ifr_addr.sa_family == AF_UNSPEC) { 8924312Ssklower lookforinterfaces = 1; 9024312Ssklower continue; 9124312Ssklower } 9224317Ssklower if (ifs.int_addr.sa_family != AF_NS) 9324317Ssklower continue; 9424317Ssklower if (ifs.int_flags & IFF_POINTOPOINT) { 9524317Ssklower if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 9624874Ssklower syslog(LOG_ERR, "ioctl (get dstaddr): %m"); 9724317Ssklower continue; 9824317Ssklower } 9924317Ssklower ifs.int_dstaddr = ifreq.ifr_dstaddr; 10024312Ssklower } 10124317Ssklower if (ifs.int_flags & IFF_BROADCAST) { 10224317Ssklower if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 10324874Ssklower syslog(LOG_ERR, "ioctl (get broadaddr: %m"); 10424317Ssklower continue; 10524317Ssklower } 10624317Ssklower ifs.int_broadaddr = ifreq.ifr_broadaddr; 10724317Ssklower } 10826170Ssklower /* 10926170Ssklower * already known to us? 11026170Ssklower * what makes a POINTOPOINT if unique is its dst addr, 11126170Ssklower * NOT its source address 11226170Ssklower */ 11326170Ssklower if ( ((ifs.int_flags & IFF_POINTOPOINT) && 11426170Ssklower if_ifwithdstaddr(&ifs.int_dstaddr)) || 11526170Ssklower ( ((ifs.int_flags & IFF_POINTOPOINT) == 0) && 11626170Ssklower if_ifwithaddr(&ifs.int_addr))) 11726170Ssklower continue; 11824317Ssklower /* no one cares about software loopback interfaces */ 11926170Ssklower if (strncmp(ifr->ifr_name,"lo", 2)==0) 12024317Ssklower continue; 12124312Ssklower ifp = (struct interface *)malloc(sizeof (struct interface)); 12224312Ssklower if (ifp == 0) { 12324874Ssklower syslog(LOG_ERR,"XNSrouted: out of memory\n"); 12424312Ssklower break; 12524312Ssklower } 12624317Ssklower *ifp = ifs; 12724312Ssklower /* 12824312Ssklower * Count the # of directly connected networks 12924312Ssklower * and point to point links which aren't looped 13024312Ssklower * back to ourself. This is used below to 13124312Ssklower * decide if we should be a routing ``supplier''. 13224312Ssklower */ 13324317Ssklower if ((ifs.int_flags & IFF_POINTOPOINT) == 0 || 13424317Ssklower if_ifwithaddr(&ifs.int_dstaddr) == 0) 13524312Ssklower externalinterfaces++; 13626170Ssklower /* 13726170Ssklower * If we have a point-to-point link, we want to act 13826170Ssklower * as a supplier even if it's our only interface, 13926170Ssklower * as that's the only way our peer on the other end 14026170Ssklower * can tell that the link is up. 14126170Ssklower */ 14226170Ssklower if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0) 14326170Ssklower supplier = 1; 14424317Ssklower ifp->int_name = malloc(strlen(ifr->ifr_name) + 1); 14524312Ssklower if (ifp->int_name == 0) { 14624874Ssklower syslog(LOG_ERR,"XNSrouted: out of memory\n"); 14736437Skarels exit(1); 14824312Ssklower } 14924317Ssklower strcpy(ifp->int_name, ifr->ifr_name); 15024312Ssklower ifp->int_metric = 0; 15124312Ssklower ifp->int_next = ifnet; 15224312Ssklower ifnet = ifp; 15324312Ssklower traceinit(ifp); 15424312Ssklower addrouteforif(ifp); 15524312Ssklower } 15624312Ssklower if (externalinterfaces > 1 && supplier < 0) 15724312Ssklower supplier = 1; 15824317Ssklower close(s); 15924312Ssklower } 16024312Ssklower 16124312Ssklower addrouteforif(ifp) 16224312Ssklower struct interface *ifp; 16324312Ssklower { 16424317Ssklower struct sockaddr_ns net; 16524312Ssklower struct sockaddr *dst; 16624312Ssklower int state, metric; 16724312Ssklower struct rt_entry *rt; 16824312Ssklower 16924322Ssklower if (ifp->int_flags & IFF_POINTOPOINT) { 17024322Ssklower int (*match)(); 17124322Ssklower register struct interface *ifp2 = ifnet; 17224322Ssklower register struct interface *ifprev = ifnet; 17324322Ssklower 17424312Ssklower dst = &ifp->int_dstaddr; 17524322Ssklower 17624322Ssklower /* Search for interfaces with the same net */ 17724322Ssklower ifp->int_sq.n = ifp->int_sq.p = &(ifp->int_sq); 17824322Ssklower match = afswitch[dst->sa_family].af_netmatch; 17924322Ssklower if (match) 18024322Ssklower for (ifp2 = ifnet; ifp2; ifp2 =ifp2->int_next) { 18124322Ssklower if (ifp->int_flags & IFF_POINTOPOINT == 0) 18224322Ssklower continue; 18324322Ssklower if ((*match)(&ifp2->int_dstaddr,&ifp->int_dstaddr)) { 18424322Ssklower insque(&ifp2->int_sq,&ifp->int_sq); 18524322Ssklower break; 18624322Ssklower } 18724322Ssklower } 18824322Ssklower } else { 18924317Ssklower dst = &ifp->int_broadaddr; 19024312Ssklower } 19124312Ssklower rt = rtlookup(dst); 19224317Ssklower if (rt) 19324317Ssklower rtdelete(rt); 19426170Ssklower if (tracing) 19526170Ssklower fprintf(stderr, "Adding route to interface %s\n", ifp->int_name); 19626170Ssklower if (ifp->int_transitions++ > 0) 19726170Ssklower syslog(LOG_ERR, "re-installing interface %s", ifp->int_name); 19824312Ssklower rtadd(dst, &ifp->int_addr, ifp->int_metric, 19924312Ssklower ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE)); 20024312Ssklower } 20126170Ssklower 202