startup.c revision 11820
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.3 1995/10/11 18:57:30 jhay 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 */ 65char ether_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 66 67 68void 69quit(s) 70 char *s; 71{ 72 extern int errno; 73 int sverrno = errno; 74 75 (void) fprintf(stderr, "IPXroute: "); 76 if (s) 77 (void) fprintf(stderr, "%s: ", s); 78 (void) fprintf(stderr, "%s\n", strerror(sverrno)); 79 exit(1); 80 /* NOTREACHED */ 81} 82 83struct rt_addrinfo info; 84/* Sleazy use of local variables throughout file, warning!!!! */ 85#define netmask info.rti_info[RTAX_NETMASK] 86#define ifaaddr info.rti_info[RTAX_IFA] 87#define brdaddr info.rti_info[RTAX_BRD] 88 89#define ROUNDUP(a) \ 90 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 91#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 92 93void 94rt_xaddrs(cp, cplim, rtinfo) 95 register caddr_t cp, cplim; 96 register struct rt_addrinfo *rtinfo; 97{ 98 register struct sockaddr *sa; 99 register int i; 100 101 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 102 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 103 if ((rtinfo->rti_addrs & (1 << i)) == 0) 104 continue; 105 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 106 ADVANCE(cp, sa); 107 } 108} 109 110/* 111 * Find the network interfaces which have configured themselves. 112 * If the interface is present but not yet up (for example an 113 * ARPANET IMP), set the lookforinterfaces flag so we'll 114 * come back later and look again. 115 */ 116void 117ifinit(void) 118{ 119 struct interface ifs, *ifp; 120 size_t needed; 121 int mib[6], no_ipxaddr = 0, flags = 0; 122 char *buf, *cplim, *cp; 123 register struct if_msghdr *ifm; 124 register struct ifa_msghdr *ifam; 125 struct sockaddr_dl *sdl = 0; 126 127 mib[0] = CTL_NET; 128 mib[1] = PF_ROUTE; 129 mib[2] = 0; 130 mib[3] = AF_IPX; 131 mib[4] = NET_RT_IFLIST; 132 mib[5] = 0; 133 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 134 quit("route-sysctl-estimate"); 135 if ((buf = malloc(needed)) == NULL) 136 quit("malloc"); 137 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 138 lookforinterfaces = 0; 139 cplim = buf + needed; 140 for (cp = buf; cp < cplim; cp += ifm->ifm_msglen) { 141 ifm = (struct if_msghdr *)cp; 142 if (ifm->ifm_type == RTM_IFINFO) { 143 bzero(&ifs, sizeof(ifs)); 144 ifs.int_flags = flags = ifm->ifm_flags | IFF_INTERFACE; 145 if ((flags & IFF_UP) == 0 || no_ipxaddr) 146 lookforinterfaces = 1; 147 sdl = (struct sockaddr_dl *) (ifm + 1); 148 sdl->sdl_data[sdl->sdl_nlen] = 0; 149 no_ipxaddr = 1; 150 continue; 151 } 152 if (ifm->ifm_type != RTM_NEWADDR) 153 quit("ifinit: out of sync"); 154 if ((flags & IFF_UP) == 0) 155 continue; 156 ifam = (struct ifa_msghdr *)ifm; 157 info.rti_addrs = ifam->ifam_addrs; 158 rt_xaddrs((char *)(ifam + 1), cp + ifam->ifam_msglen, &info); 159 if (ifaaddr == 0) { 160 syslog(LOG_ERR, "%s: (get addr)", sdl->sdl_data); 161 continue; 162 } 163 ifs.int_addr = *ifaaddr; 164 if (ifs.int_addr.sa_family != AF_IPX) 165 continue; 166 no_ipxaddr = 0; 167 if (ifs.int_flags & IFF_POINTOPOINT) { 168 if (brdaddr == 0) { 169 syslog(LOG_ERR, "%s: (get dstaddr)", 170 sdl->sdl_data); 171 continue; 172 } 173 if (brdaddr->sa_family == AF_UNSPEC) { 174 lookforinterfaces = 1; 175 continue; 176 } 177 ifs.int_dstaddr = *brdaddr; 178 } 179 if (ifs.int_flags & IFF_BROADCAST) { 180 if (brdaddr == 0) { 181 syslog(LOG_ERR, "%s: (get broadaddr)", 182 sdl->sdl_data); 183 continue; 184 } 185 ifs.int_dstaddr = *brdaddr; 186 } 187 /* 188 * already known to us? 189 * what makes a POINTOPOINT if unique is its dst addr, 190 * NOT its source address 191 */ 192 if ( ((ifs.int_flags & IFF_POINTOPOINT) && 193 if_ifwithdstaddr(&ifs.int_dstaddr)) || 194 ( ((ifs.int_flags & IFF_POINTOPOINT) == 0) && 195 if_ifwithaddr(&ifs.int_addr))) 196 continue; 197 /* no one cares about software loopback interfaces */ 198 if (ifs.int_flags & IFF_LOOPBACK) 199 continue; 200 ifp = (struct interface *) 201 malloc(sdl->sdl_nlen + 1 + sizeof(ifs)); 202 if (ifp == 0) { 203 syslog(LOG_ERR, "IPXrouted: out of memory\n"); 204 lookforinterfaces = 1; 205 break; 206 } 207 *ifp = ifs; 208 /* 209 * Count the # of directly connected networks 210 * and point to point links which aren't looped 211 * back to ourself. This is used below to 212 * decide if we should be a routing ``supplier''. 213 */ 214 if ((ifs.int_flags & IFF_POINTOPOINT) == 0 || 215 if_ifwithaddr(&ifs.int_dstaddr) == 0) 216 externalinterfaces++; 217 /* 218 * If we have a point-to-point link, we want to act 219 * as a supplier even if it's our only interface, 220 * as that's the only way our peer on the other end 221 * can tell that the link is up. 222 */ 223 if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0) 224 supplier = 1; 225 ifp->int_name = (char *)(ifp + 1); 226 strcpy(ifp->int_name, sdl->sdl_data); 227 228 ifp->int_metric = ifam->ifam_metric; 229 ifp->int_next = ifnet; 230 ifnet = ifp; 231 traceinit(ifp); 232 addrouteforif(ifp); 233 } 234 if (externalinterfaces > 1 && supplier < 0) 235 supplier = 1; 236 free(buf); 237} 238 239void 240addrouteforif(ifp) 241 struct interface *ifp; 242{ 243 struct sockaddr *dst; 244 struct rt_entry *rt; 245 246 if (ifp->int_flags & IFF_POINTOPOINT) { 247 int (*match)(); 248 register struct interface *ifp2 = ifnet; 249 250 dst = &ifp->int_dstaddr; 251 252 /* Search for interfaces with the same net */ 253 ifp->int_sq.n = ifp->int_sq.p = &(ifp->int_sq); 254 match = afswitch[dst->sa_family].af_netmatch; 255 if (match) 256 for (ifp2 = ifnet; ifp2; ifp2 =ifp2->int_next) { 257 if (ifp->int_flags & IFF_POINTOPOINT == 0) 258 continue; 259 if ((*match)(&ifp2->int_dstaddr,&ifp->int_dstaddr)) { 260 insque(&ifp2->int_sq,&ifp->int_sq); 261 break; 262 } 263 } 264 } else { 265 dst = &ifp->int_broadaddr; 266 } 267 rt = rtlookup(dst); 268 if (rt) 269 rtdelete(rt); 270 if (tracing) 271 fprintf(stderr, "Adding route to interface %s\n", ifp->int_name); 272 if (ifp->int_transitions++ > 0) 273 syslog(LOG_ERR, "re-installing interface %s", ifp->int_name); 274 rtadd(dst, &ifp->int_addr, ifp->int_metric, 0, 275 ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE)); 276} 277 278