]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/startup.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgment:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)defs.h 8.1 (Berkeley) 6/5/93
61 * Routing Table Management Daemon
64 #include <sys/ioctl.h>
65 #include <sys/sysctl.h>
67 #include <net/if_dl.h>
70 #include "pathnames.h"
72 struct interface
*ifnet
;
73 struct interface
**ifnext
= &ifnet
;
74 int lookforinterfaces
= 1;
75 int externalinterfaces
= 0; /* # of remote and local interfaces */
76 int foundloopback
; /* valid flag for loopaddr */
77 struct sockaddr loopaddr
; /* our address on loopback */
87 (void) fprintf(stderr
, "route: ");
89 (void) fprintf(stderr
, "%s: ", s
);
90 (void) fprintf(stderr
, "%s\n", strerror(sverrno
));
95 struct rt_addrinfo info
;
96 /* Sleazy use of local variables throughout file, warning!!!! */
97 #define netmask info.rti_info[RTAX_NETMASK]
98 #define ifaaddr info.rti_info[RTAX_IFA]
99 #define brdaddr info.rti_info[RTAX_BRD]
102 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
103 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
106 rt_xaddrs(cp
, cplim
, rtinfo
)
107 register caddr_t cp
, cplim
;
108 register struct rt_addrinfo
*rtinfo
;
110 register struct sockaddr
*sa
;
113 memset(rtinfo
->rti_info
, 0, sizeof(rtinfo
->rti_info
));
114 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
115 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
117 rtinfo
->rti_info
[i
] = sa
= (struct sockaddr
*)cp
;
123 * Find the network interfaces which have configured themselves.
124 * If the interface is present but not yet up (for example an
125 * ARPANET IMP), set the lookforinterfaces flag so we'll
126 * come back later and look again.
130 struct interface ifs
, *ifp
;
132 int mib
[6], no_ipaddr
= 0, flags
= 0;
133 char *buf
, *cplim
, *cp
;
134 register struct if_msghdr
*ifm
;
135 register struct ifa_msghdr
*ifam
;
136 struct sockaddr_dl
*sdl
;
137 struct sockaddr_in
*sin
;
144 mib
[4] = NET_RT_IFLIST
;
146 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
147 quit("route-sysctl-estimate");
148 if ((buf
= malloc(needed
)) == NULL
)
150 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
151 quit("actual retrieval of interface table");
152 lookforinterfaces
= 0;
153 cplim
= buf
+ needed
;
154 for (cp
= buf
; cp
< cplim
; cp
+= ifm
->ifm_msglen
) {
155 ifm
= (struct if_msghdr
*)cp
;
156 if (ifm
->ifm_type
== RTM_IFINFO
) {
157 memset(&ifs
, 0, sizeof(ifs
));
158 ifs
.int_flags
= flags
= (0xffff & ifm
->ifm_flags
) | IFF_INTERFACE
;
159 if ((flags
& IFF_UP
) == 0 || no_ipaddr
)
160 lookforinterfaces
= 1;
161 sdl
= (struct sockaddr_dl
*) (ifm
+ 1);
162 sdl
->sdl_data
[sdl
->sdl_nlen
] = 0;
166 if (ifm
->ifm_type
!= RTM_NEWADDR
)
167 quit("ifinit: out of sync");
168 if ((flags
& IFF_UP
) == 0)
170 ifam
= (struct ifa_msghdr
*)ifm
;
171 info
.rti_addrs
= ifam
->ifam_addrs
;
172 rt_xaddrs((char *)(ifam
+ 1), cp
+ ifam
->ifam_msglen
, &info
);
174 syslog(LOG_ERR
, "%s: (get addr)", sdl
->sdl_data
);
177 ifs
.int_addr
= *ifaaddr
;
178 if (ifs
.int_addr
.sa_family
!= AF_INET
)
181 if (ifs
.int_flags
& IFF_POINTOPOINT
) {
183 syslog(LOG_ERR
, "%s: (get dstaddr)",
187 if (brdaddr
->sa_family
== AF_UNSPEC
) {
188 lookforinterfaces
= 1;
191 ifs
.int_dstaddr
= *brdaddr
;
194 * already known to us?
195 * This allows multiple point-to-point links
196 * to share a source address (possibly with one
197 * other link), but assumes that there will not be
198 * multiple links with the same destination address.
200 if (ifs
.int_flags
& IFF_POINTOPOINT
) {
201 if (if_ifwithdstaddr(&ifs
.int_dstaddr
))
203 } else if (if_ifwithaddr(&ifs
.int_addr
))
205 if (ifs
.int_flags
& IFF_LOOPBACK
) {
206 ifs
.int_flags
|= IFF_PASSIVE
;
208 loopaddr
= ifs
.int_addr
;
209 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
210 if (ifp
->int_flags
& IFF_POINTOPOINT
)
211 add_ptopt_localrt(ifp
);
213 if (ifs
.int_flags
& IFF_BROADCAST
) {
215 syslog(LOG_ERR
, "%s: (get broadaddr)",
219 ifs
.int_dstaddr
= *brdaddr
;
222 * Use a minimum metric of one;
223 * treat the interface metric (default 0)
224 * as an increment to the hop count of one.
226 ifs
.int_metric
= ifam
->ifam_metric
+ 1;
228 syslog(LOG_ERR
, "%s: (get netmask)",
232 sin
= (struct sockaddr_in
*)netmask
;
233 ifs
.int_subnetmask
= ntohl(sin
->sin_addr
.s_addr
);
234 sin
= (struct sockaddr_in
*)&ifs
.int_addr
;
235 i
= ntohl(sin
->sin_addr
.s_addr
);
237 ifs
.int_netmask
= IN_CLASSA_NET
;
238 else if (IN_CLASSB(i
))
239 ifs
.int_netmask
= IN_CLASSB_NET
;
241 ifs
.int_netmask
= IN_CLASSC_NET
;
242 ifs
.int_net
= i
& ifs
.int_netmask
;
243 ifs
.int_subnet
= i
& ifs
.int_subnetmask
;
244 if (ifs
.int_subnetmask
!= ifs
.int_netmask
)
245 ifs
.int_flags
|= IFF_SUBNET
;
246 ifp
= (struct interface
*)
247 malloc(sdl
->sdl_nlen
+ 1 + sizeof(ifs
));
249 printf("routed: out of memory\n");
250 lookforinterfaces
= 1;
255 * Count the # of directly connected networks
256 * and point to point links which aren't looped
257 * back to ourself. This is used below to
258 * decide if we should be a routing ``supplier''.
260 if ((ifs
.int_flags
& IFF_LOOPBACK
) == 0 &&
261 ((ifs
.int_flags
& IFF_POINTOPOINT
) == 0 ||
262 if_ifwithaddr(&ifs
.int_dstaddr
) == 0))
263 externalinterfaces
++;
265 * If we have a point-to-point link, we want to act
266 * as a supplier even if it's our only interface,
267 * as that's the only way our peer on the other end
268 * can tell that the link is up.
270 if ((ifs
.int_flags
& IFF_POINTOPOINT
) && supplier
< 0)
272 ifp
->int_name
= (char *)(ifp
+ 1);
273 strcpy(ifp
->int_name
, sdl
->sdl_data
);
275 ifnext
= &ifp
->int_next
;
279 if (externalinterfaces
> 1 && supplier
< 0)
285 * Add route for interface if not currently installed.
286 * Create route to other end if a point-to-point link,
287 * otherwise a route to this (sub)network.
291 register struct interface
*ifp
;
293 struct sockaddr_in net
;
294 struct sockaddr
*dst
;
296 register struct rt_entry
*rt
;
298 if (ifp
->int_flags
& IFF_POINTOPOINT
)
299 dst
= &ifp
->int_dstaddr
;
301 memset(&net
, 0, sizeof (net
));
302 net
.sin_family
= AF_INET
;
303 net
.sin_addr
= inet_makeaddr(ifp
->int_subnet
, INADDR_ANY
);
304 dst
= (struct sockaddr
*)&net
;
308 (rt
->rt_state
& (RTS_INTERFACE
| RTS_INTERNAL
)) == RTS_INTERFACE
)
313 * If interface on subnetted network,
314 * install route to network as well.
315 * This is meant for external viewers.
317 if ((ifp
->int_flags
& (IFF_SUBNET
|IFF_POINTOPOINT
)) == IFF_SUBNET
) {
318 struct in_addr subnet
;
320 subnet
= net
.sin_addr
;
321 net
.sin_addr
= inet_makeaddr(ifp
->int_net
, INADDR_ANY
);
324 rtadd(dst
, &ifp
->int_addr
, ifp
->int_metric
,
325 ((ifp
->int_flags
& (IFF_INTERFACE
|IFF_REMOTE
)) |
326 RTS_PASSIVE
| RTS_INTERNAL
| RTS_SUBNET
));
327 else if ((rt
->rt_state
& (RTS_INTERNAL
|RTS_SUBNET
)) ==
328 (RTS_INTERNAL
|RTS_SUBNET
) &&
329 ifp
->int_metric
< rt
->rt_metric
)
330 rtchange(rt
, &rt
->rt_router
, ifp
->int_metric
);
331 net
.sin_addr
= subnet
;
333 if (ifp
->int_transitions
++ > 0)
334 syslog(LOG_ERR
, "re-installing interface %s", ifp
->int_name
);
335 state
= ifp
->int_flags
&
336 (IFF_INTERFACE
| IFF_PASSIVE
| IFF_REMOTE
| IFF_SUBNET
);
337 if (ifp
->int_flags
& IFF_POINTOPOINT
&&
338 (ntohl(((struct sockaddr_in
*)&ifp
->int_dstaddr
)->sin_addr
.s_addr
) &
339 ifp
->int_netmask
) != ifp
->int_net
)
340 state
&= ~RTS_SUBNET
;
341 if (ifp
->int_flags
& IFF_LOOPBACK
)
342 state
|= RTS_EXTERNAL
;
343 rtadd(dst
, &ifp
->int_addr
, ifp
->int_metric
, state
);
344 if (ifp
->int_flags
& IFF_POINTOPOINT
&& foundloopback
)
345 add_ptopt_localrt(ifp
);
349 * Add route to local end of point-to-point using loopback.
350 * If a route to this network is being sent to neighbors on other nets,
351 * mark this route as subnet so we don't have to propagate it too.
353 add_ptopt_localrt(ifp
)
354 register struct interface
*ifp
;
357 struct sockaddr
*dst
;
358 struct sockaddr_in net
;
361 state
= RTS_INTERFACE
| RTS_PASSIVE
;
363 /* look for route to logical network */
364 memset(&net
, 0, sizeof (net
));
365 net
.sin_family
= AF_INET
;
366 net
.sin_addr
= inet_makeaddr(ifp
->int_net
, INADDR_ANY
);
367 dst
= (struct sockaddr
*)&net
;
369 if (rt
&& rt
->rt_state
& RTS_INTERNAL
)
372 dst
= &ifp
->int_addr
;
373 if (rt
= rtfind(dst
)) {
374 if (rt
&& rt
->rt_state
& RTS_INTERFACE
)
378 rtadd(dst
, &loopaddr
, 1, state
);
382 * As a concession to the ARPANET we read a list of gateways
383 * from /etc/gateways and add them to our tables. This file
384 * exists at each ARPANET gateway and indicates a set of ``remote''
385 * gateways (i.e. a gateway which we can't immediately determine
386 * if it's present or not as we can do for those directly connected
387 * at the hardware level). If a gateway is marked ``passive''
388 * in the file, then we assume it doesn't have a routing process
389 * of our design and simply assume it's always present. Those
390 * not marked passive are treated as if they were directly
391 * connected -- they're added into the interface list so we'll
392 * send them routing updates.
394 * PASSIVE ENTRIES AREN'T NEEDED OR USED ON GATEWAYS RUNNING EGP.
398 struct sockaddr_in dst
, gate
;
400 char *type
, *dname
, *gname
, *qual
, buf
[BUFSIZ
];
401 struct interface
*ifp
;
403 struct rt_entry route
;
405 fp
= fopen(_PATH_GATEWAYS
, "r");
410 gname
= buf
+ ((BUFSIZ
- 64) / 3);
411 type
= buf
+ (((BUFSIZ
- 64) * 2) / 3);
412 memset(&dst
, 0, sizeof (dst
));
413 memset(&gate
, 0, sizeof (gate
));
414 memset(&route
, 0, sizeof(route
));
415 /* format: {net | host} XX gateway XX metric DD [passive | external]\n */
416 #define readentry(fp) \
417 fscanf((fp), "%s %s gateway %s metric %d %s\n", \
418 type, dname, gname, &metric, qual)
420 if ((n
= readentry(fp
)) == EOF
)
422 if (!getnetorhostname(type
, dname
, &dst
))
424 if (!gethostnameornumber(gname
, &gate
))
426 if (metric
== 0) /* XXX */
428 if (strcmp(qual
, "passive") == 0) {
430 * Passive entries aren't placed in our tables,
431 * only the kernel's, so we don't copy all of the
432 * external routing information within a net.
433 * Internal machines should use the default
434 * route to a suitable gateway (like us).
436 route
.rt_dst
= *(struct sockaddr
*) &dst
;
437 route
.rt_router
= *(struct sockaddr
*) &gate
;
438 route
.rt_flags
= RTF_UP
;
439 if (strcmp(type
, "host") == 0)
440 route
.rt_flags
|= RTF_HOST
;
442 route
.rt_flags
|= RTF_GATEWAY
;
443 (void) rtioctl(ADD
, &route
.rt_rt
);
446 if (strcmp(qual
, "external") == 0) {
448 * Entries marked external are handled
449 * by other means, e.g. EGP,
450 * and are placed in our tables only
451 * to prevent overriding them
452 * with something else.
454 rtadd(&dst
, &gate
, metric
, RTS_EXTERNAL
|RTS_PASSIVE
);
457 /* assume no duplicate entries */
458 externalinterfaces
++;
459 ifp
= (struct interface
*)malloc(sizeof (*ifp
));
460 memset(ifp
, 0, sizeof (*ifp
));
461 ifp
->int_flags
= IFF_REMOTE
;
462 /* can't identify broadcast capability */
463 ifp
->int_net
= inet_netof(dst
.sin_addr
);
464 if (strcmp(type
, "host") == 0) {
465 ifp
->int_flags
|= IFF_POINTOPOINT
;
466 ifp
->int_dstaddr
= *((struct sockaddr
*)&dst
);
468 ifp
->int_addr
= *((struct sockaddr
*)&gate
);
469 ifp
->int_metric
= metric
;
470 ifp
->int_next
= ifnet
;
477 getnetorhostname(type
, name
, sin
)
479 struct sockaddr_in
*sin
;
482 if (strcmp(type
, "net") == 0) {
483 struct netent
*np
= getnetbyname(name
);
487 n
= inet_network(name
);
489 if (np
->n_addrtype
!= AF_INET
)
493 * getnetbyname returns right-adjusted value.
496 n
<<= IN_CLASSA_NSHIFT
;
498 n
<<= IN_CLASSB_NSHIFT
;
500 n
<<= IN_CLASSC_NSHIFT
;
502 sin
->sin_family
= AF_INET
;
503 sin
->sin_addr
= inet_makeaddr(n
, INADDR_ANY
);
506 if (strcmp(type
, "host") == 0) {
507 struct hostent
*hp
= gethostbyname(name
);
510 sin
->sin_addr
.s_addr
= inet_addr(name
);
512 if (hp
->h_addrtype
!= AF_INET
)
514 memmove(&sin
->sin_addr
, hp
->h_addr
, hp
->h_length
);
516 sin
->sin_family
= AF_INET
;
522 gethostnameornumber(name
, sin
)
524 struct sockaddr_in
*sin
;
528 hp
= gethostbyname(name
);
530 memmove(&sin
->sin_addr
, hp
->h_addr
, hp
->h_length
);
531 sin
->sin_family
= hp
->h_addrtype
;
534 sin
->sin_addr
.s_addr
= inet_addr(name
);
535 sin
->sin_family
= AF_INET
;
536 return (sin
->sin_addr
.s_addr
!= -1);