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