2 * $Id: main.c,v 1.10.2.2 2002-03-12 15:36:10 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #if defined( sun ) && defined( __svr4__ )
15 #include </usr/ucbinclude/sys/file.h>
16 #else /* sun __svr4__ */
18 #endif /* sun __svr4__ */
20 #include <sys/resource.h>
21 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_WAIT_H
27 #endif /* HAVE_SYS_WAIT_H */
29 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
30 #endif /* ! WEXITSTATUS */
32 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
33 #endif /* ! WIFEXITED */
38 #include <net/route.h>
41 #include <net/route.h>
44 #include <netinet/in.h>
53 #include <netatalk/endian.h>
54 #include <netatalk/at.h>
55 #include <atalk/compat.h>
56 #include <atalk/zip.h>
57 #include <atalk/rtmp.h>
58 #include <atalk/ddp.h>
59 #include <atalk/atp.h>
60 #include <atalk/paths.h>
61 #include <atalk/util.h>
64 #include <sys/sockio.h>
68 #include "interface.h"
76 /* Forward Declarations */
77 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
79 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
81 #define SOCKLEN_T unsigned int
82 #endif /* SOCKLEN_T */
85 #define WEXITSTATUS(x) ((x).w_retcode)
86 #endif /* WEXITSTATUS */
88 /* linux has a special ioctl for appletalk device destruction. as of
89 * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
90 * fact that SIOCDIFADDR may be defined on linux despite the fact that
92 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
93 #define SIOCDIFADDR SIOCATALKDIFADDR
96 #define elements(a) (sizeof(a)/sizeof((a)[0]))
100 extern int rtmp_packet();
101 extern int nbp_packet();
102 extern int aep_packet();
103 extern int zip_packet();
107 struct atserv atserv[] = {
108 { "rtmp", 1, rtmp_packet }, /* 0 */
109 { "nbp", 2, nbp_packet }, /* 1 */
110 { "echo", 4, aep_packet }, /* 2 */
111 { "zip", 6, zip_packet }, /* 3 */
113 int atservNATSERV = elements( atserv );
115 struct interface *interfaces = NULL, *ciface = NULL;
117 int debug = 0, quiet = 0, chatty = 0;
118 char *configfile = NULL;
119 int ziptimeout = 0, transition = 0;
120 int stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
121 static int ninterfaces;
122 int defphase = IFACE_PHASE2;
125 char Packet[ PKTSZ ];
126 char *version = VERSION;
127 static char *pidfile = _PATH_ATALKDLOCK;
132 int readconf( char * );
133 int getifconf( void );
134 int writeconf( char * );
136 /* this is the messiest of the bunch as atalkd can exit pretty much
137 * everywhere. we delete interfaces here instead of in as_down. */
138 static void atalkd_exit(const int i)
141 struct interface *iface;
143 for (iface = interfaces; iface; iface = iface->i_next) {
144 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
145 #ifdef SIOCATALKDIFADDR
146 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
147 if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr))
149 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
150 #endif /* SIOCATALKIFADDR */
151 syslog( LOG_ERR, "difaddr(%u.%u): %s",
152 ntohs(iface->i_addr.sat_addr.s_net),
153 iface->i_addr.sat_addr.s_node, strerror(errno));
156 #endif /* SOPCDOFADDR */
158 server_unlock(pidfile);
165 struct sockaddr_at sat;
168 struct rtmp_tuple rt;
169 struct atport *ap, *zap, *rap;
170 struct interface *iface, *iface2;
171 struct gate *gate, *fgate = NULL;
172 struct rtmptab *rtmp, *frtmp;
174 char *data, *end, packet[ ATP_BUFSIZ ];
178 memset(&sat, 0, sizeof( struct sockaddr_at ));
179 for ( iface = interfaces; iface; iface = iface->i_next ) {
180 if ( iface->i_flags & IFACE_LOOPBACK ) {
183 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
184 if ( ap->ap_packet == zip_packet ) {
187 if ( ap->ap_packet == rtmp_packet ) {
192 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
194 if ( iface->i_time < 3 ) {
195 if ( iface->i_flags & IFACE_PHASE1 ) {
196 if (rtmp_request( iface ) < 0) {
197 syslog(LOG_ERR, "rtmp_request: %s", strerror(errno));
202 if (zip_getnetinfo( iface ) < 0) {
203 syslog(LOG_ERR, "zip_getnetinfo: %s", strerror(errno));
210 iface->i_flags |= IFACE_NOROUTER;
211 if ((iface->i_flags & IFACE_ISROUTER)) {
212 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
214 * No seed info, and we've got multiple interfaces.
218 "as_timer multiple interfaces, no seed" );
219 syslog( LOG_INFO, "as_timer can't configure %s",
221 syslog( LOG_INFO, "as_timer waiting for router" );
226 * Complete configuration for iface, and boot next
229 iface->i_flags |= IFACE_CONFIG;
230 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
231 if (addzone( iface->i_rt, zt->zt_len,
233 syslog(LOG_ERR, "addzone: %s", strerror(errno));
237 if ( iface->i_rt->rt_zt ) {
238 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
239 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
241 if ( iface->i_flags & IFACE_PHASE1 ) {
243 "as_timer configured %s phase 1 from seed",
245 setaddr( iface, IFACE_PHASE1,
246 iface->i_caddr.sat_addr.s_net,
247 iface->i_addr.sat_addr.s_node,
248 iface->i_caddr.sat_addr.s_net,
249 iface->i_caddr.sat_addr.s_net );
252 "as_timer configured %s phase 2 from seed",
256 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
258 "as_timer: can't route %u.%u to loop: %s",
259 ntohs( iface->i_addr.sat_addr.s_net ),
260 iface->i_addr.sat_addr.s_node,
264 if ( iface == ciface ) {
265 ciface = ciface->i_next;
271 * Configure for no router operation. Wait for a route
272 * to become available in rtmp_packet().
274 syslog( LOG_INFO, "config for no router" );
276 if ( iface->i_flags & IFACE_PHASE2 ) {
277 iface->i_rt->rt_firstnet = 0;
278 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
279 setaddr( iface, IFACE_PHASE2,
280 iface->i_addr.sat_addr.s_net,
281 iface->i_addr.sat_addr.s_node,
282 0, htons( STARTUP_LASTNET ));
284 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
286 "as_timer: can't route %u.%u to loopback: %s",
287 ntohs( iface->i_addr.sat_addr.s_net ),
288 iface->i_addr.sat_addr.s_node,
293 if ( iface == ciface ) {
294 ciface = ciface->i_next;
301 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
303 free( (caddr_t)fgate );
308 data = packet + 1 + sizeof( struct ziphdr );
309 end = packet + sizeof( packet );
312 sat.sat_port = zap->ap_port;
315 * Perform timeouts on routers. If we've only got one
316 * interface, we'll use these timeouts to decide that
317 * our zone has gone away.
319 if ( ++gate->g_state >= RTMPTAB_BAD ) {
320 syslog( LOG_INFO, "as_timer gateway %u.%u down",
321 ntohs( gate->g_sat.sat_addr.s_net ),
322 gate->g_sat.sat_addr.s_node );
325 frtmp = rtmp->rt_next;
326 if ( rtmp->rt_hops == RTMPHOPS_POISON ||
327 rtmp->rt_iprev == 0 ) {
330 rtmp->rt_hops = RTMPHOPS_POISON;
331 if ((cc = rtmp_replace( rtmp )) < 0) {
332 syslog(LOG_ERR, "rtmp_replace: %s", strerror(errno));
336 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
341 if ( gate->g_rt == 0 ) {
342 if ( gate->g_prev == 0 ) {
343 gate->g_iface->i_gate = gate->g_next;
345 gate->g_prev->g_next = gate->g_next;
347 if ( gate->g_next != 0 ) {
348 gate->g_next->g_prev = gate->g_prev;
350 fgate = gate; /* can't free here, just mark it */
353 * If this is the last router on the only interface,
354 * reconfigure our netrange. By marking the interface
355 * as having no router, we will notice when a router
358 * XXX: actually, we always reconfigure an interface
359 * if we're not a seed router.
362 if ( gate->g_iface->i_gate == 0 &&
363 ((iface->i_flags & IFACE_SEED) == 0)) {
364 gate->g_iface->i_flags |= IFACE_NOROUTER;
365 gate->g_iface->i_flags &= ~IFACE_CONFIG;
367 /* get rid of any zones associated with this iface */
368 if (gate->g_iface->i_rt->rt_zt) {
369 rtmp_delzonemap(gate->g_iface->i_rt);
370 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
373 syslog( LOG_INFO, "as_timer last gateway down" );
375 /* Set netrange to 0-fffe. */
376 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
377 gate->g_iface->i_rt->rt_firstnet = 0;
378 gate->g_iface->i_rt->rt_lastnet =
379 htons( STARTUP_LASTNET );
380 setaddr( iface, IFACE_PHASE2,
381 iface->i_addr.sat_addr.s_net,
382 iface->i_addr.sat_addr.s_node,
383 0, htons( STARTUP_LASTNET ));
390 * If we don't have a zone for our interface yet, ask for
391 * it from any router (all routers) on the interface.
393 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
394 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
395 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
396 data += sizeof( u_short );
403 * Delete old routing tuples.
405 if ( rtmp->rt_state != RTMPTAB_PERM ) {
410 * We've not been updated for this route in a while. If
411 * it's not in use, go ahead and remove it. If it is in
412 * use, mark the route as down (POISON), and look for a
413 * better route. If one is found, delete this route and use
414 * the new one. If it's not found, mark the route as GOOD
415 * (so we'll propogate our poison) and delete it the next
416 * time it becomes BAD.
418 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
419 frtmp = rtmp->rt_next;
420 if ( rtmp->rt_iprev == 0 ) { /* not in use */
422 } else { /* in use */
423 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
426 rtmp->rt_hops = RTMPHOPS_POISON;
427 if ((cc = rtmp_replace( rtmp )) < 0) {
428 syslog(LOG_ERR, "rtmp_replace: %s", strerror(errno));
432 rtmp->rt_state = RTMPTAB_GOOD;
442 if ( rtmp->rt_iprev &&
443 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
444 if ( data + sizeof( u_short ) > end || n == 255 ) {
445 /* send what we've got */
446 zh.zh_op = ZIPOP_QUERY;
450 *data++ = DDPTYPE_ZIP;
451 memcpy( data, &zh, sizeof( struct ziphdr ));
453 if ( sendto( zap->ap_fd, packet, cc, 0,
454 (struct sockaddr *)&sat,
455 sizeof( struct sockaddr_at )) < 0 ) {
456 syslog( LOG_ERR, "as_timer sendto: %s", strerror(errno) );
461 data = packet + 1 + sizeof( struct ziphdr );
462 end = packet + sizeof( packet );
466 * rt_nzq is number of ZIP Queries we've issued for a
467 * given netrange. If we've got ziptimeout on, we
468 * will only ask 3 times for any given netrange.
469 * Interestingly enough, since rt_nzq is a u_char,
470 * it will overflow after a while. This means we will
471 * periodically ask for nets that we've decided not to
472 * ask about, and warn that we can't get it's zone.
474 if ( rtmp->rt_nzq++ == 3 ) {
475 syslog( LOG_INFO, "as_timer can't get zone for %u",
476 ntohs( rtmp->rt_firstnet ));
478 if ( rtmp->rt_nzq > 3 ) {
480 rtmp = rtmp->rt_next;
486 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
487 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
488 data += sizeof( u_short );
491 rtmp = rtmp->rt_next;
494 /* send what we've got */
496 zh.zh_op = ZIPOP_QUERY;
500 *data++ = DDPTYPE_ZIP;
501 memcpy( data, &zh, sizeof( struct ziphdr ));
503 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
504 sizeof( struct sockaddr_at )) < 0 ) {
505 syslog( LOG_ERR, "as_timer sendto: %s", strerror(errno) );
510 free( (caddr_t)fgate );
515 * Send RTMP broadcasts if we have multiple interfaces or our
516 * interface is configured as a router.
518 if ((iface->i_flags & IFACE_ISROUTER)) {
520 sat.sat_len = sizeof( struct sockaddr_at );
522 sat.sat_family = AF_APPLETALK;
523 sat.sat_addr.s_net = ATADDR_ANYNET;
524 sat.sat_addr.s_node = ATADDR_BCAST;
525 sat.sat_port = rap->ap_port;
528 end = data + sizeof( packet );
529 *data++ = DDPTYPE_RTMPRD;
530 rh.rh_net = iface->i_addr.sat_addr.s_net;
532 rh.rh_node = iface->i_addr.sat_addr.s_node;
533 memcpy( data, &rh, sizeof( struct rtmp_head ));
534 data += sizeof( struct rtmp_head );
538 if ( iface->i_flags & IFACE_PHASE1 ) {
541 memcpy( data, &rt, SZ_RTMPTUPLE );
542 data += SZ_RTMPTUPLE;
544 rt.rt_net = iface->i_rt->rt_firstnet;
546 memcpy( data, &rt, SZ_RTMPTUPLE );
547 data += SZ_RTMPTUPLE;
549 rt.rt_net = iface->i_rt->rt_lastnet;
551 memcpy( data, &rt, SZ_RTMPTUPLE );
552 data += SZ_RTMPTUPLE;
555 for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
557 /* XXX: there used to be a bit checking against iface ==
558 iface2. also, we don't want to send an rtmp broadcast
559 to an interface that doesn't want it. */
560 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
561 ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
565 * Fill in tuples. Always send the same thing, regardless
566 * of the phase of the destination. Routers who don't
567 * understand extended rtmp packets will toss extended
568 * tuples because their distance will have the high bit set.
570 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
571 /* don't broadcast routes we have no zone for */
572 if ( rtmp->rt_zt == 0 ||
573 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
574 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
578 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
579 data + 2 * SZ_RTMPTUPLE > end ) ||
580 data + SZ_RTMPTUPLE > end ) {
581 if ( sendto( rap->ap_fd, packet, data - packet, 0,
582 (struct sockaddr *)&sat,
583 sizeof( struct sockaddr_at )) < 0 ) {
584 syslog( LOG_ERR, "as_timer sendto %u.%u (%u): %s",
585 ntohs( sat.sat_addr.s_net ),
587 ntohs( iface->i_rt->rt_firstnet ),
591 if ( iface->i_flags & IFACE_PHASE2 ) {
592 data = packet + 1 + sizeof( struct rtmp_head ) +
595 data = packet + 1 + sizeof( struct rtmp_head ) +
601 rt.rt_net = rtmp->rt_firstnet;
602 rt.rt_dist = rtmp->rt_hops;
603 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
606 memcpy( data, &rt, SZ_RTMPTUPLE );
607 data += SZ_RTMPTUPLE;
609 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
610 rt.rt_net = rtmp->rt_lastnet;
612 memcpy( data, &rt, SZ_RTMPTUPLE );
613 data += SZ_RTMPTUPLE;
621 if ( sendto( rap->ap_fd, packet, data - packet, 0,
622 (struct sockaddr *)&sat,
623 sizeof( struct sockaddr_at )) < 0 ) {
624 syslog( LOG_ERR, "as_timer sendto %u.%u (%u): %s",
625 ntohs( sat.sat_addr.s_net ),
627 ntohs( iface->i_rt->rt_firstnet ),
635 * Check if we're stable. Each time we configure an interface, we
636 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
637 * STABLEANYWAY, we give up and decide to "be" stable anyway.
638 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
639 * data and all zip data complete.
642 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
643 /* write out config file */
645 writeconf( configfile );
647 if ( stabletimer-- <= STABLEANYWAY ) {
653 if ( stable && !noparent ) {
655 syslog( LOG_INFO, "ready %d/%d/%d", stabletimer, newrtmpdata,
659 * Seems like we could get here more than once...
661 if ( kill( getpid(), SIGSTOP ) < 0 ) {
662 syslog( LOG_ERR, "as_timer: kill-self failed!" );
676 * Consistency check...
680 struct rtmptab *rtmp;
681 struct list *lr, *lz;
684 for ( zt = ziptab; zt; zt = zt->zt_next ) {
685 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
686 rtmp = (struct rtmptab *)lr->l_data;
687 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
688 syslog( LOG_ERR, "%.*s has %u-%u (unused)\n",
689 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
690 ntohs( rtmp->rt_lastnet ));
693 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
694 if ( zt == (struct ziptab *)lz->l_data ) {
699 syslog( LOG_ERR, "no map from %u-%u to %.*s\n",
700 ntohs( rtmp->rt_firstnet ),
701 ntohs( rtmp->rt_lastnet ),
702 zt->zt_len, zt->zt_name );
710 #if !defined( ibm032 ) && !defined( _IBMR2 )
712 #endif /* ! ibm032 && ! _IBMR2 */
715 struct interface *iface;
722 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
723 syslog( LOG_ERR, "rtmp: %s", strerror(errno) );
726 for ( iface = interfaces; iface; iface = iface->i_next ) {
727 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
728 ntohs( iface->i_addr.sat_addr.s_net ),
729 iface->i_addr.sat_addr.s_node );
730 if ( iface->i_flags & IFACE_PHASE1 ) {
731 putc( '1', rtmpdebug );
733 if ( iface->i_flags & IFACE_PHASE2 ) {
734 putc( '2', rtmpdebug );
736 if ( iface->i_flags & IFACE_RSEED ) {
737 putc( 'R', rtmpdebug );
739 if ( iface->i_flags & IFACE_SEED ) {
740 putc( 'S', rtmpdebug );
742 if ( iface->i_flags & IFACE_DONTROUTE ) {
743 putc( 'D', rtmpdebug );
745 if ( iface->i_flags & IFACE_ADDR ) {
746 putc( 'A', rtmpdebug );
748 if ( iface->i_flags & IFACE_CONFIG ) {
749 putc( 'C', rtmpdebug );
751 if ( iface->i_flags & IFACE_NOROUTER ) {
752 putc( 'N', rtmpdebug );
754 if ( iface->i_flags & IFACE_LOOP ) {
755 putc( 'L', rtmpdebug );
757 putc( '\n', rtmpdebug );
760 fprintf( rtmpdebug, "\t%u-%u ",
761 ntohs( iface->i_rt->rt_firstnet ),
762 ntohs( iface->i_rt->rt_lastnet ));
763 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
764 putc( 'q', rtmpdebug );
766 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
767 putc( 'z', rtmpdebug );
769 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
770 putc( 'x', rtmpdebug );
772 putc( 'i', rtmpdebug );
773 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
774 zt = (struct ziptab *)l->l_data;
775 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
777 fprintf( rtmpdebug, "\n" );
780 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
781 fprintf( rtmpdebug, "gate %u.%u %X\n",
782 ntohs( gate->g_sat.sat_addr.s_net ),
783 gate->g_sat.sat_addr.s_node, gate->g_state );
784 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
785 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
786 ntohs( rt->rt_lastnet ));
787 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
788 putc( 'q', rtmpdebug );
790 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
791 putc( 'z', rtmpdebug );
793 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
794 putc( 'x', rtmpdebug );
796 if ( rt->rt_iprev ) {
797 putc( 'i', rtmpdebug );
799 for ( l = rt->rt_zt; l; l = l->l_next ) {
800 zt = (struct ziptab *)l->l_data;
801 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
803 fprintf( rtmpdebug, "\n" );
812 * Called when SIGTERM is recieved. Remove all routes and then exit.
814 #if !defined( ibm032 ) && !defined( _IBMR2 )
816 #endif /* ! ibm032 && ! _IBMR2 */
819 struct interface *iface;
823 for ( iface = interfaces; iface; iface = iface->i_next ) {
824 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
825 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
826 if ( rt->rt_iprev ) {
827 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
828 syslog( LOG_ERR, "as_down remove %u-%u failed: %s",
829 ntohs( rt->rt_firstnet ),
830 ntohs( rt->rt_lastnet ),
836 if ( iface->i_flags & IFACE_LOOP ) {
837 if (looproute( iface, RTMP_DEL )) {
838 syslog( LOG_ERR, "as_down remove %s %u.%u failed: %s",
839 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
840 iface->i_addr.sat_addr.s_node,
846 syslog( LOG_INFO, "done" );
857 struct sockaddr_at sat;
860 struct interface *iface;
869 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
872 defphase = IFACE_PHASE1;
876 defphase = IFACE_PHASE2;
887 case 'q' : /* don't seed */
891 case 's' : /* seed */
895 case 't' : /* transition */
899 case 'P' : /* pid file */
903 case 'v' : /* version */
904 printf( "atalkd (version %s)\n", version );
909 fprintf( stderr, "Unknown option -- '%c'\n", c );
913 if ( optind != ac ) {
914 fprintf( stderr, "Too many arguments.\n" );
918 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
925 * Configure loop back address first, so appearances of "lo0" in
926 * the config file fail. Also insures that lo0 gets configured,
927 * even if there's some hangup during configuration of some
930 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
931 perror( "newiface" );
934 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
937 * Check our initial configuration before we fork. This way we can
938 * complain about syntax errors on stdout.
940 * Basically, if we're going to read our config file, we should read
941 * it and initialize our data structures. If we're not going to read
942 * our config file, use GIFCONF to initialize our data structures.
944 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
945 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
949 /* we need to count up our interfaces so that we can simplify things
950 * later. we also need to figure out if we have more than one interface
951 * that is routing. */
952 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
953 iface=iface->i_next) {
954 if (iface->i_flags & IFACE_DONTROUTE)
958 i = ninterfaces - i; /* number of routable interfaces */
961 * At this point, we have (at least partially) initialized data
962 * structures. Fill in what we can and verify that nothing is obviously
965 for (iface = interfaces; iface; iface = iface->i_next) {
966 /* Apply the default phase */
967 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
968 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
969 iface->i_flags |= defphase;
972 /* set up router flag information. if we have multiple interfaces
973 * and DONTROUTE isn't set, set up ROUTER. i is the number of
974 * interfaces that don't have the DONTROUTE flag set. */
975 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
976 iface->i_flags |= IFACE_ISROUTER;
979 /* Set default addresses */
980 if ( iface->i_rt == NULL ) {
981 if (( iface->i_rt = newrt(iface)) == NULL ) {
986 if ( iface->i_flags & IFACE_PHASE1 ) {
987 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
988 iface->i_caddr.sat_addr.s_net;
990 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
991 ( iface->i_flags & IFACE_LOOPBACK )) {
992 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
993 iface->i_caddr.sat_addr.s_net;
995 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
996 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
1001 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
1002 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
1005 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1006 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1010 dumpconfig( iface ); /* probably needs args */
1015 * A little consistency check...
1017 if ( ninterfaces < IFBASE ) {
1018 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1022 /* do this here so that we can use ifconfig */
1024 if ( plumb() < 0 ) {
1025 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1028 #endif /* __svr4__ */
1030 /* delete pre-existing interface addresses. */
1032 for (iface = interfaces; iface; iface = iface->i_next) {
1033 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1034 #ifdef SIOCATALKDIFADDR
1035 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1036 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1037 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1038 #endif /* SIOCATALKDIFADDR */
1041 #endif /* SIOCDIFADDR */
1044 * Disassociate. The child will send itself a signal when it is
1045 * stable. This indicates that other processes may begin using
1048 switch (i = server_lock("atalkd", pidfile, debug)) {
1053 default: /* parent */
1055 * Wait for the child to send itself a SIGSTOP, after which
1056 * we send it a SIGCONT and exit ourself.
1058 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1059 perror( "wait3" ); /* Child died? */
1062 if ( !WIFSTOPPED( status )) {
1063 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1064 if ( WIFEXITED( status )) {
1065 fprintf( stderr, " Child exited with %d.\n",
1066 WEXITSTATUS( status ));
1068 fprintf( stderr, " Child died.\n" );
1072 if ( kill(i, SIGCONT ) < 0 ) {
1080 openlog( prog, LOG_PID );
1082 openlog( prog, LOG_PID, LOG_DAEMON );
1085 syslog( LOG_INFO, "restart (%s)", version );
1088 * Socket for use in routing ioctl()s. Can't add routes to our
1089 * interfaces until we have our routing socket.
1092 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1093 syslog( LOG_ERR, "route socket: %s", strerror(errno) );
1096 if ( shutdown( rtfd, 0 ) < 0 ) {
1097 syslog( LOG_ERR, "route shutdown: %s", strerror(errno) );
1101 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1102 syslog( LOG_ERR, "route socket: %s", strerror(errno) );
1107 memset(&sv, 0, sizeof(sv));
1108 sv.sa_handler = as_down;
1109 sigemptyset( &sv.sa_mask );
1110 sigaddset( &sv.sa_mask, SIGUSR1 );
1111 sigaddset( &sv.sa_mask, SIGALRM );
1112 sigaddset( &sv.sa_mask, SIGTERM );
1113 sv.sa_flags = SA_RESTART;
1114 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1115 syslog( LOG_ERR, "sigterm: %s", strerror(errno) );
1119 sv.sa_handler = as_debug;
1120 sigemptyset( &sv.sa_mask );
1121 sigaddset( &sv.sa_mask, SIGUSR1 );
1122 sigaddset( &sv.sa_mask, SIGALRM );
1123 sigaddset( &sv.sa_mask, SIGTERM );
1124 sv.sa_flags = SA_RESTART;
1125 if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1126 syslog( LOG_ERR, "sigusr1: %s", strerror(errno) );
1130 sv.sa_handler = as_timer;
1131 sigemptyset( &sv.sa_mask );
1132 sigaddset( &sv.sa_mask, SIGUSR1 );
1133 sigaddset( &sv.sa_mask, SIGALRM );
1134 sigaddset( &sv.sa_mask, SIGTERM );
1135 sv.sa_flags = SA_RESTART;
1136 if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1137 syslog( LOG_ERR, "sigalrm: %s", strerror(errno) );
1141 it.it_interval.tv_sec = 10L;
1142 it.it_interval.tv_usec = 0L;
1143 it.it_value.tv_sec = 10L;
1144 it.it_value.tv_usec = 0L;
1145 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1146 syslog( LOG_ERR, "setitimer: %s", strerror(errno) );
1150 ciface = interfaces;
1154 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1155 if ( errno == EINTR ) {
1159 syslog( LOG_ERR, "select: %s", strerror(errno) );
1164 for ( iface = interfaces; iface; iface = iface->i_next ) {
1165 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1166 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1167 if ( ap->ap_packet ) {
1168 fromlen = sizeof( struct sockaddr_at );
1169 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1170 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1171 syslog( LOG_ERR, "recvfrom: %s", strerror(errno) );
1176 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1177 ntohs( sat.sat_addr.s_net ),
1178 sat.sat_addr.s_node, iface->i_name,
1179 iface->i_flags, ap->ap_port, ap->ap_fd );
1180 bprint( Packet, c );
1184 if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
1185 syslog( LOG_ERR, "sighold: %s", strerror(errno) );
1188 #else /* __svr4__ */
1189 mask = sigsetmask( sigmask( SIGALRM ) |
1190 sigmask( SIGUSR1 ));
1191 #endif /* __svr4__ */
1192 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1193 syslog(LOG_ERR, "ap->ap_packet: %s", strerror(errno));
1201 if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
1202 syslog( LOG_ERR, "sigrelse: %s", strerror(errno) );
1205 #else /* __svr4__ */
1207 #endif /* __svr4__ */
1216 * This code is called (from main(), as_timer(), zip_packet(),
1217 * and rtmp_packet()) to set the initial "bootstrapping" address
1220 void bootaddr( iface )
1221 struct interface *iface;
1228 if ( iface->i_flags & IFACE_ADDR ) {
1229 syslog( LOG_ERR, "bootaddr OOPS!" );
1233 if ( iface->i_flags & IFACE_PHASE1 ) {
1234 setaddr( iface, IFACE_PHASE1, 0,
1235 iface->i_caddr.sat_addr.s_node, 0, 0 );
1237 if ( iface->i_flags & IFACE_LOOPBACK ) {
1238 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1239 if ( ciface == iface ) {
1240 ciface = ciface->i_next;
1244 } else if (rtmp_request( iface ) < 0) {
1245 syslog(LOG_ERR, "bootaddr (rtmp_request): %s", strerror(errno));
1250 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1251 iface->i_caddr.sat_addr.s_node,
1252 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1254 if ( iface->i_flags & IFACE_LOOPBACK ) {
1255 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1256 if ( ciface == iface ) {
1257 ciface = ciface->i_next;
1261 } else if (zip_getnetinfo( iface ) < 0) {
1262 syslog(LOG_ERR, "bootaddr (zip_getnetinfo): %s", strerror(errno));
1267 iface->i_flags |= IFACE_ADDR;
1268 stabletimer = UNSTABLE;
1274 * to manage the i_ports field and the fds for select().
1276 void setaddr(struct interface *iface,
1277 u_int8_t phase, u_int16_t net, u_int8_t node,
1278 u_int16_t first, u_int16_t last)
1284 struct sockaddr_at sat;
1287 if ( iface->i_ports == NULL ) { /* allocate port structures */
1288 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1289 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1290 syslog( LOG_INFO, "%s: service unknown", as->as_name );
1292 as->as_port = ntohs( se->s_port );
1294 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1296 syslog( LOG_ERR, "malloc: %s", strerror(errno) );
1300 ap->ap_next = iface->i_ports;
1301 ap->ap_iface = iface;
1302 ap->ap_port = as->as_port;
1303 ap->ap_packet = as->as_packet;
1305 iface->i_ports = ap;
1307 } else { /* close ports */
1308 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1309 (void)close( ap->ap_fd );
1314 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1316 iface->i_addr.sat_family = AF_APPLETALK;
1317 iface->i_addr.sat_addr.s_net = net;
1318 iface->i_addr.sat_addr.s_node = node;
1320 nr.nr_phase = phase;
1321 nr.nr_firstnet = first;
1322 nr.nr_lastnet = last;
1323 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1325 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1326 syslog( LOG_ERR, "setifaddr: %s (%u-%u): %s. try specifying a \
1327 smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
1330 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1331 syslog( LOG_ERR, "getifaddr: %s: %s", iface->i_name, strerror(errno) );
1336 i = 1; /* enable broadcasts */
1338 syslog(LOG_INFO, "setsockopt incompatible w/ Solaris STREAMS module.");
1339 #endif /* __svr4__ */
1340 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1341 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1342 syslog( LOG_ERR, "socket: %s", strerror(errno) );
1346 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1347 #endif /* ! __svr4 */
1349 memset( &sat, 0, sizeof( struct sockaddr_at ));
1351 sat.sat_len = sizeof( struct sockaddr_at );
1353 sat.sat_family = AF_APPLETALK;
1354 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1355 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1356 sat.sat_port = ap->ap_port;
1358 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1359 sizeof( struct sockaddr_at )) < 0 ) {
1360 syslog( LOG_ERR, "bind %u.%u:%u: %s",
1361 ntohs( sat.sat_addr.s_net ),
1362 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1364 /* remove all interfaces if we have a problem with bind */
1365 for (iface = interfaces; iface; iface = iface->i_next) {
1366 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1367 #ifdef SIOCATALKDIFADDR
1368 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1369 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1370 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1371 #endif /* SIOCATALKDIFADDR */
1374 #endif /* SIOCDIFADDR */
1379 /* recalculate nfds and fds */
1381 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1382 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1383 FD_SET( ap->ap_fd, &fds );
1384 if ( ap->ap_fd > nfds ) {
1392 int ifconfig( iname, cmd, sa )
1395 struct sockaddr_at *sa;
1400 memset(&ifr, 0, sizeof(ifr));
1401 strcpy( ifr.ifr_name, iname );
1402 ifr.ifr_addr = *(struct sockaddr *)sa;
1404 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1407 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1412 if ( cmd == SIOCGIFADDR ) {
1413 *(struct sockaddr *)sa = ifr.ifr_addr;
1418 void dumpconfig( iface )
1419 struct interface *iface;
1423 printf( "%s", iface->i_name );
1424 if ( iface->i_flags & IFACE_RSEED ) {
1425 printf( " -router" );
1426 } else if ( iface->i_flags & IFACE_SEED ) {
1430 if ( iface->i_flags & IFACE_DONTROUTE)
1431 printf( " -dontroute");
1433 printf( " -phase" );
1434 if ( iface->i_flags & IFACE_PHASE1 ) {
1439 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1440 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1441 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1443 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1444 iface->i_addr.sat_addr.s_node );
1445 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1446 iface->i_caddr.sat_addr.s_node );
1447 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1448 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1449 ((struct ziptab *)l->l_data)->zt_name );
1457 struct interface *iface;
1458 struct rtmptab *rtmp;
1462 for ( iface = interfaces; iface; iface = iface->i_next ) {
1463 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1464 if ( rtmp->rt_gate == 0 ) {
1465 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1466 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1467 ntohs( rtmp->rt_lastnet ));
1469 printf( "%u", ntohs( rtmp->rt_firstnet ));
1472 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1473 printf( "%u.%u for %u-%u",
1474 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1475 rtmp->rt_gate->g_sat.sat_addr.s_node,
1476 ntohs( rtmp->rt_firstnet ),
1477 ntohs( rtmp->rt_lastnet ));
1479 printf( "%u.%u for %u",
1480 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1481 rtmp->rt_gate->g_sat.sat_addr.s_node,
1482 ntohs( rtmp->rt_firstnet ));
1486 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1490 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1491 zt = (struct ziptab *)l->l_data;
1492 printf( " %.*s", zt->zt_len, zt->zt_name );
1505 struct interface *iface;
1506 struct rtmptab *rtmp;
1510 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1511 printf( "%.*s", zt->zt_len, zt->zt_name );
1512 for ( l = zt->zt_rt; l; l = l->l_next ) {
1513 rtmp = (struct rtmptab *)l->l_data;
1514 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1515 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1516 ntohs( rtmp->rt_lastnet ));
1518 printf( " %u", ntohs( rtmp->rt_firstnet ));
1520 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {