startup.c revision 12268
1/* 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Copyright (c) 1995 John Hay. All rights reserved. 6 * 7 * This file includes significant work done at Cornell University by 8 * Bill Nesheim. That work included by permission. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * $Id: startup.c,v 1.1 1995/10/26 21:28:26 julian Exp $ 39 */ 40 41#ifndef lint 42static char sccsid[] = "@(#)startup.c 8.1 (Berkeley) 6/5/93"; 43#endif /* not lint */ 44 45/* 46 * Routing Table Management Daemon 47 */ 48#include "defs.h" 49 50#include <sys/param.h> 51#include <sys/ioctl.h> 52#include <sys/sysctl.h> 53 54#include <net/if.h> 55#include <net/if_dl.h> 56 57#include <nlist.h> 58#include <stdlib.h> 59 60struct interface *ifnet; 61int lookforinterfaces = 1; 62int performnlist = 1; 63int gateway = 0; 64int externalinterfaces = 0; /* # of remote and local interfaces */ 65 66void 67quit(s) 68 char *s; 69{ 70 extern int errno; 71 int sverrno = errno; 72 73 (void) fprintf(stderr, "IPXroute: "); 74 if (s) 75 (void) fprintf(stderr, "%s: ", s); 76 (void) fprintf(stderr, "%s\n", strerror(sverrno)); 77 exit(1); 78 /* NOTREACHED */ 79} 80 81struct rt_addrinfo info; 82/* Sleazy use of local variables throughout file, warning!!!! */ 83#define netmask info.rti_info[RTAX_NETMASK] 84#define ifaaddr info.rti_info[RTAX_IFA] 85#define brdaddr info.rti_info[RTAX_BRD] 86 87#define ROUNDUP(a) \ 88 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 89#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 90 91void 92rt_xaddrs(cp, cplim, rtinfo) 93 register caddr_t cp, cplim; 94 register struct rt_addrinfo *rtinfo; 95{ 96 register struct sockaddr *sa; 97 register int i; 98 99 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 100 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 101 if ((rtinfo->rti_addrs & (1 << i)) == 0) 102 continue; 103 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 104 ADVANCE(cp, sa); 105 } 106} 107 108/* 109 * Find the network interfaces which have configured themselves. 110 * If the interface is present but not yet up (for example an 111 * ARPANET IMP), set the lookforinterfaces flag so we'll 112 * come back later and look again. 113 */ 114void 115ifinit(void) 116{ 117 struct interface ifs, *ifp; 118 size_t needed; 119 int mib[6], no_ipxaddr = 0, flags = 0; 120 char *buf, *cplim, *cp; 121 register struct if_msghdr *ifm; 122 register struct ifa_msghdr *ifam; 123 struct sockaddr_dl *sdl = 0; 124 125 mib[0] = CTL_NET; 126 mib[1] = PF_ROUTE; 127 mib[2] = 0; 128 mib[3] = AF_IPX; 129 mib[4] = NET_RT_IFLIST; 130 mib[5] = 0; 131 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 132 quit("route-sysctl-estimate"); 133 if ((buf = malloc(needed)) == NULL) 134 quit("malloc"); 135 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 136 lookforinterfaces = 0; 137 cplim = buf + needed; 138 for (cp = buf; cp < cplim; cp += ifm->ifm_msglen) { 139 ifm = (struct if_msghdr *)cp; 140 if (ifm->ifm_type == RTM_IFINFO) { 141 bzero(&ifs, sizeof(ifs)); 142 ifs.int_flags = flags = ifm->ifm_flags | IFF_INTERFACE; 143 if ((flags & IFF_UP) == 0 || no_ipxaddr) 144 lookforinterfaces = 1; 145 sdl = (struct sockaddr_dl *) (ifm + 1); 146 sdl->sdl_data[sdl->sdl_nlen] = 0; 147 no_ipxaddr = 1; 148 continue; 149 } 150 if (ifm->ifm_type != RTM_NEWADDR) 151 quit("ifinit: out of sync"); 152 if ((flags & IFF_UP) == 0) 153 continue; 154 ifam = (struct ifa_msghdr *)ifm; 155 info.rti_addrs = ifam->ifam_addrs; 156 rt_xaddrs((char *)(ifam + 1), cp + ifam->ifam_msglen, &info); 157 if (ifaaddr == 0) { 158 syslog(LOG_ERR, "%s: (get addr)", sdl->sdl_data); 159 continue; 160 } 161 ifs.int_addr = *ifaaddr; 162 if (ifs.int_addr.sa_family != AF_IPX) 163 continue; 164 no_ipxaddr = 0; 165 if (ifs.int_flags & IFF_POINTOPOINT) { 166 if (brdaddr == 0) { 167 syslog(LOG_ERR, "%s: (get dstaddr)", 168 sdl->sdl_data); 169 continue; 170 } 171 if (brdaddr->sa_family == AF_UNSPEC) { 172 lookforinterfaces = 1; 173 continue; 174 } 175 ifs.int_dstaddr = *brdaddr; 176 } 177 if (ifs.int_flags & IFF_BROADCAST) { 178 if (brdaddr == 0) { 179 syslog(LOG_ERR, "%s: (get broadaddr)", 180 sdl->sdl_data); 181 continue; 182 } 183 ifs.int_dstaddr = *brdaddr; 184 } 185 /* 186 * already known to us? 187 * what makes a POINTOPOINT if unique is its dst addr, 188 * NOT its source address 189 */ 190 if ( ((ifs.int_flags & IFF_POINTOPOINT) && 191 if_ifwithdstaddr(&ifs.int_dstaddr)) || 192 ( ((ifs.int_flags & IFF_POINTOPOINT) == 0) && 193 if_ifwithaddr(&ifs.int_addr))) 194 continue; 195 /* no one cares about software loopback interfaces */ 196 if (ifs.int_flags & IFF_LOOPBACK) 197 continue; 198 ifp = (struct interface *) 199 malloc(sdl->sdl_nlen + 1 + sizeof(ifs)); 200 if (ifp == 0) { 201 syslog(LOG_ERR, "IPXrouted: out of memory\n"); 202 lookforinterfaces = 1; 203 break; 204 } 205 *ifp = ifs; 206 /* 207 * Count the # of directly connected networks 208 * and point to point links which aren't looped 209 * back to ourself. This is used below to 210 * decide if we should be a routing ``supplier''. 211 */ 212 if ((ifs.int_flags & IFF_POINTOPOINT) == 0 || 213 if_ifwithaddr(&ifs.int_dstaddr) == 0) 214 externalinterfaces++; 215 /* 216 * If we have a point-to-point link, we want to act 217 * as a supplier even if it's our only interface, 218 * as that's the only way our peer on the other end 219 * can tell that the link is up. 220 */ 221 if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0) 222 supplier = 1; 223 ifp->int_name = (char *)(ifp + 1); 224 strcpy(ifp->int_name, sdl->sdl_data); 225 226 ifp->int_metric = ifam->ifam_metric; 227 ifp->int_next = ifnet; 228 ifnet = ifp; 229 traceinit(ifp); 230 addrouteforif(ifp); 231 } 232 if (externalinterfaces > 1 && supplier < 0) 233 supplier = 1; 234 free(buf); 235} 236 237void 238addrouteforif(ifp) 239 struct interface *ifp; 240{ 241 struct sockaddr *dst; 242 struct rt_entry *rt; 243 244 if (ifp->int_flags & IFF_POINTOPOINT) { 245 int (*match)(); 246 register struct interface *ifp2 = ifnet; 247 248 dst = &ifp->int_dstaddr; 249 250 /* Search for interfaces with the same net */ 251 ifp->int_sq.n = ifp->int_sq.p = &(ifp->int_sq); 252 match = afswitch[dst->sa_family].af_netmatch; 253 if (match) 254 for (ifp2 = ifnet; ifp2; ifp2 =ifp2->int_next) { 255 if (ifp->int_flags & IFF_POINTOPOINT == 0) 256 continue; 257 if ((*match)(&ifp2->int_dstaddr,&ifp->int_dstaddr)) { 258 insque(&ifp2->int_sq,&ifp->int_sq); 259 break; 260 } 261 } 262 } else { 263 dst = &ifp->int_broadaddr; 264 } 265 rt = rtlookup(dst); 266 if (rt) 267 rtdelete(rt); 268 if (tracing) 269 fprintf(stderr, "Adding route to interface %s\n", ifp->int_name); 270 if (ifp->int_transitions++ > 0) 271 syslog(LOG_ERR, "re-installing interface %s", ifp->int_name); 272 rtadd(dst, &ifp->int_addr, ifp->int_metric, 0, 273 ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE)); 274} 275 276