2 * $Id: main.c,v 1.10 2001-09-12 19:13:17 uhees 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>
52 #include <netatalk/endian.h>
53 #include <netatalk/at.h>
54 #include <atalk/compat.h>
55 #include <atalk/zip.h>
56 #include <atalk/rtmp.h>
57 #include <atalk/ddp.h>
58 #include <atalk/atp.h>
59 #include <atalk/paths.h>
60 #include <atalk/util.h>
63 #include <sys/sockio.h>
67 #include "interface.h"
75 /* Forward Declarations */
76 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
78 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
80 #define SOCKLEN_T unsigned int
81 #endif /* SOCKLEN_T */
84 #define WEXITSTATUS(x) ((x).w_retcode)
85 #endif /* WEXITSTATUS */
87 /* linux has a special ioctl for appletalk device destruction. as of
88 * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
89 * fact that SIOCDIFADDR may be defined on linux despite the fact that
91 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
92 #define SIOCDIFADDR SIOCATALKDIFADDR
95 #define elements(a) (sizeof(a)/sizeof((a)[0]))
99 extern int rtmp_packet();
100 extern int nbp_packet();
101 extern int aep_packet();
102 extern int zip_packet();
106 struct atserv atserv[] = {
107 { "rtmp", 1, rtmp_packet }, /* 0 */
108 { "nbp", 2, nbp_packet }, /* 1 */
109 { "echo", 4, aep_packet }, /* 2 */
110 { "zip", 6, zip_packet }, /* 3 */
112 int atservNATSERV = elements( atserv );
114 struct interface *interfaces = NULL, *ciface = NULL;
116 int debug = 0, quiet = 0, chatty = 0;
117 char *configfile = NULL;
118 int ziptimeout = 0, transition = 0;
119 int stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
120 static int ninterfaces;
121 int defphase = IFACE_PHASE2;
124 char Packet[ PKTSZ ];
125 char *version = VERSION;
126 static char *pidfile = _PATH_ATALKDLOCK;
131 int readconf( char * );
132 int getifconf( void );
133 int writeconf( char * );
135 /* this is the messiest of the bunch as atalkd can exit pretty much
136 * everywhere. we delete interfaces here instead of in as_down. */
137 static void atalkd_exit(const int i)
140 struct interface *iface;
142 for (iface = interfaces; iface; iface = iface->i_next) {
143 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
144 #ifdef SIOCATALKDIFADDR
145 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
146 if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr))
148 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
149 #endif /* SIOCATALKIFADDR */
150 syslog( LOG_ERR, "difaddr(%u.%u): %m",
151 ntohs(iface->i_addr.sat_addr.s_net),
152 iface->i_addr.sat_addr.s_node);
155 #endif /* SOPCDOFADDR */
157 server_unlock(pidfile);
162 #if !defined( ibm032 ) && !defined( _IBMR2 )
164 #endif /* ibm032 _IBMR2 */
167 struct sockaddr_at sat;
170 struct rtmp_tuple rt;
171 struct atport *ap, *zap, *rap;
172 struct interface *iface, *iface2;
173 struct gate *gate, *fgate = NULL;
174 struct rtmptab *rtmp, *frtmp;
176 char *data, *end, packet[ ATP_BUFSIZ ];
180 memset(&sat, 0, sizeof( struct sockaddr_at ));
181 for ( iface = interfaces; iface; iface = iface->i_next ) {
182 if ( iface->i_flags & IFACE_LOOPBACK ) {
185 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
186 if ( ap->ap_packet == zip_packet ) {
189 if ( ap->ap_packet == rtmp_packet ) {
194 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
196 if ( iface->i_time < 3 ) {
197 if ( iface->i_flags & IFACE_PHASE1 ) {
198 if (rtmp_request( iface ) < 0) {
199 syslog(LOG_ERR, "rtmp_request: %m");
204 if (zip_getnetinfo( iface ) < 0) {
205 syslog(LOG_ERR, "zip_getnetinfo: %m");
212 iface->i_flags |= IFACE_NOROUTER;
213 if ((iface->i_flags & IFACE_ISROUTER)) {
214 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
216 * No seed info, and we've got multiple interfaces.
220 "as_timer multiple interfaces, no seed" );
221 syslog( LOG_INFO, "as_timer can't configure %s",
223 syslog( LOG_INFO, "as_timer waiting for router" );
228 * Complete configuration for iface, and boot next
231 iface->i_flags |= IFACE_CONFIG;
232 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
233 if (addzone( iface->i_rt, zt->zt_len,
235 syslog(LOG_ERR, "addzone: %m");
239 if ( iface->i_rt->rt_zt ) {
240 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
241 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
243 if ( iface->i_flags & IFACE_PHASE1 ) {
245 "as_timer configured %s phase 1 from seed",
247 setaddr( iface, IFACE_PHASE1,
248 iface->i_caddr.sat_addr.s_net,
249 iface->i_addr.sat_addr.s_node,
250 iface->i_caddr.sat_addr.s_net,
251 iface->i_caddr.sat_addr.s_net );
254 "as_timer configured %s phase 2 from seed",
258 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
260 "as_timer: can't route %u.%u to loop: %m",
261 ntohs( iface->i_addr.sat_addr.s_net ),
262 iface->i_addr.sat_addr.s_node );
265 if ( iface == ciface ) {
266 ciface = ciface->i_next;
272 * Configure for no router operation. Wait for a route
273 * to become available in rtmp_packet().
275 syslog( LOG_INFO, "config for no router" );
277 if ( iface->i_flags & IFACE_PHASE2 ) {
278 iface->i_rt->rt_firstnet = 0;
279 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
280 setaddr( iface, IFACE_PHASE2,
281 iface->i_addr.sat_addr.s_net,
282 iface->i_addr.sat_addr.s_node,
283 0, htons( STARTUP_LASTNET ));
285 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
287 "as_timer: can't route %u.%u to loopback: %m",
288 ntohs( iface->i_addr.sat_addr.s_net ),
289 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: %m");
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: %m");
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: %m" );
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: %m" );
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): %m",
585 ntohs( sat.sat_addr.s_net ),
587 ntohs( iface->i_rt->rt_firstnet ));
590 if ( iface->i_flags & IFACE_PHASE2 ) {
591 data = packet + 1 + sizeof( struct rtmp_head ) +
594 data = packet + 1 + sizeof( struct rtmp_head ) +
600 rt.rt_net = rtmp->rt_firstnet;
601 rt.rt_dist = rtmp->rt_hops;
602 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
605 memcpy( data, &rt, SZ_RTMPTUPLE );
606 data += SZ_RTMPTUPLE;
608 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
609 rt.rt_net = rtmp->rt_lastnet;
611 memcpy( data, &rt, SZ_RTMPTUPLE );
612 data += SZ_RTMPTUPLE;
620 if ( sendto( rap->ap_fd, packet, data - packet, 0,
621 (struct sockaddr *)&sat,
622 sizeof( struct sockaddr_at )) < 0 ) {
623 syslog( LOG_ERR, "as_timer sendto %u.%u (%u): %m",
624 ntohs( sat.sat_addr.s_net ),
626 ntohs( iface->i_rt->rt_firstnet ));
633 * Check if we're stable. Each time we configure an interface, we
634 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
635 * STABLEANYWAY, we give up and decide to "be" stable anyway.
636 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
637 * data and all zip data complete.
640 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
641 /* write out config file */
643 writeconf( configfile );
645 if ( stabletimer-- <= STABLEANYWAY ) {
651 if ( stable && !noparent ) {
653 syslog( LOG_INFO, "ready %d/%d/%d", stabletimer, newrtmpdata,
657 * Seems like we could get here more than once...
659 if ( kill( getpid(), SIGSTOP ) < 0 ) {
660 syslog( LOG_ERR, "as_timer: kill-self failed!" );
674 * Consistency check...
678 struct rtmptab *rtmp;
679 struct list *lr, *lz;
682 for ( zt = ziptab; zt; zt = zt->zt_next ) {
683 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
684 rtmp = (struct rtmptab *)lr->l_data;
685 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
686 syslog( LOG_ERR, "%.*s has %u-%u (unused)\n",
687 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
688 ntohs( rtmp->rt_lastnet ));
691 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
692 if ( zt == (struct ziptab *)lz->l_data ) {
697 syslog( LOG_ERR, "no map from %u-%u to %.*s\n",
698 ntohs( rtmp->rt_firstnet ),
699 ntohs( rtmp->rt_lastnet ),
700 zt->zt_len, zt->zt_name );
708 #if !defined( ibm032 ) && !defined( _IBMR2 )
710 #endif /* ! ibm032 && ! _IBMR2 */
713 struct interface *iface;
720 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
721 syslog( LOG_ERR, "rtmp: %m" );
724 for ( iface = interfaces; iface; iface = iface->i_next ) {
725 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
726 ntohs( iface->i_addr.sat_addr.s_net ),
727 iface->i_addr.sat_addr.s_node );
728 if ( iface->i_flags & IFACE_PHASE1 ) {
729 putc( '1', rtmpdebug );
731 if ( iface->i_flags & IFACE_PHASE2 ) {
732 putc( '2', rtmpdebug );
734 if ( iface->i_flags & IFACE_RSEED ) {
735 putc( 'R', rtmpdebug );
737 if ( iface->i_flags & IFACE_SEED ) {
738 putc( 'S', rtmpdebug );
740 if ( iface->i_flags & IFACE_DONTROUTE ) {
741 putc( 'D', rtmpdebug );
743 if ( iface->i_flags & IFACE_ADDR ) {
744 putc( 'A', rtmpdebug );
746 if ( iface->i_flags & IFACE_CONFIG ) {
747 putc( 'C', rtmpdebug );
749 if ( iface->i_flags & IFACE_NOROUTER ) {
750 putc( 'N', rtmpdebug );
752 if ( iface->i_flags & IFACE_LOOP ) {
753 putc( 'L', rtmpdebug );
755 putc( '\n', rtmpdebug );
758 fprintf( rtmpdebug, "\t%u-%u ",
759 ntohs( iface->i_rt->rt_firstnet ),
760 ntohs( iface->i_rt->rt_lastnet ));
761 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
762 putc( 'q', rtmpdebug );
764 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
765 putc( 'z', rtmpdebug );
767 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
768 putc( 'x', rtmpdebug );
770 putc( 'i', rtmpdebug );
771 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
772 zt = (struct ziptab *)l->l_data;
773 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
775 fprintf( rtmpdebug, "\n" );
778 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
779 fprintf( rtmpdebug, "gate %u.%u %X\n",
780 ntohs( gate->g_sat.sat_addr.s_net ),
781 gate->g_sat.sat_addr.s_node, gate->g_state );
782 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
783 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
784 ntohs( rt->rt_lastnet ));
785 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
786 putc( 'q', rtmpdebug );
788 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
789 putc( 'z', rtmpdebug );
791 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
792 putc( 'x', rtmpdebug );
794 if ( rt->rt_iprev ) {
795 putc( 'i', rtmpdebug );
797 for ( l = rt->rt_zt; l; l = l->l_next ) {
798 zt = (struct ziptab *)l->l_data;
799 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
801 fprintf( rtmpdebug, "\n" );
810 * Called when SIGTERM is recieved. Remove all routes and then exit.
812 #if !defined( ibm032 ) && !defined( _IBMR2 )
814 #endif /* ! ibm032 && ! _IBMR2 */
817 struct interface *iface;
821 for ( iface = interfaces; iface; iface = iface->i_next ) {
822 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
823 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
824 if ( rt->rt_iprev ) {
825 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
826 syslog( LOG_ERR, "as_down remove %u-%u failed: %m",
827 ntohs( rt->rt_firstnet ),
828 ntohs( rt->rt_lastnet ));
833 if ( iface->i_flags & IFACE_LOOP ) {
834 if (looproute( iface, RTMP_DEL )) {
835 syslog( LOG_ERR, "as_down remove %s %u.%u failed: %m",
836 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
837 iface->i_addr.sat_addr.s_node );
842 syslog( LOG_INFO, "done" );
853 struct sockaddr_at sat;
856 struct interface *iface;
865 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
868 defphase = IFACE_PHASE1;
872 defphase = IFACE_PHASE2;
883 case 'q' : /* don't seed */
887 case 's' : /* seed */
891 case 't' : /* transition */
895 case 'P' : /* pid file */
899 case 'v' : /* version */
900 printf( "atalkd (version %s)\n", version );
905 fprintf( stderr, "Unknown option -- '%c'\n", c );
909 if ( optind != ac ) {
910 fprintf( stderr, "Too many arguments.\n" );
914 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
921 * Configure loop back address first, so appearances of "lo0" in
922 * the config file fail. Also insures that lo0 gets configured,
923 * even if there's some hangup during configuration of some
926 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
927 perror( "newiface" );
930 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
933 * Check our initial configuration before we fork. This way we can
934 * complain about syntax errors on stdout.
936 * Basically, if we're going to read our config file, we should read
937 * it and initialize our data structures. If we're not going to read
938 * our config file, use GIFCONF to initialize our data structures.
940 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
941 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
945 /* we need to count up our interfaces so that we can simplify things
946 * later. we also need to figure out if we have more than one interface
947 * that is routing. */
948 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
949 iface=iface->i_next) {
950 if (iface->i_flags & IFACE_DONTROUTE)
954 i = ninterfaces - i; /* number of routable interfaces */
957 * At this point, we have (at least partially) initialized data
958 * structures. Fill in what we can and verify that nothing is obviously
961 for (iface = interfaces; iface; iface = iface->i_next) {
962 /* Apply the default phase */
963 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
964 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
965 iface->i_flags |= defphase;
968 /* set up router flag information. if we have multiple interfaces
969 * and DONTROUTE isn't set, set up ROUTER. i is the number of
970 * interfaces that don't have the DONTROUTE flag set. */
971 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
972 iface->i_flags |= IFACE_ISROUTER;
975 /* Set default addresses */
976 if ( iface->i_rt == NULL ) {
977 if (( iface->i_rt = newrt(iface)) == NULL ) {
982 if ( iface->i_flags & IFACE_PHASE1 ) {
983 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
984 iface->i_caddr.sat_addr.s_net;
986 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
987 ( iface->i_flags & IFACE_LOOPBACK )) {
988 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
989 iface->i_caddr.sat_addr.s_net;
991 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
992 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
997 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
998 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
1001 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1002 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1006 dumpconfig( iface ); /* probably needs args */
1011 * A little consistency check...
1013 if ( ninterfaces < IFBASE ) {
1014 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1018 /* do this here so that we can use ifconfig */
1020 if ( plumb() < 0 ) {
1021 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1024 #endif /* __svr4__ */
1026 /* delete pre-existing interface addresses. */
1028 for (iface = interfaces; iface; iface = iface->i_next) {
1029 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1030 #ifdef SIOCATALKDIFADDR
1031 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1032 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1033 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1034 #endif /* SIOCATALKDIFADDR */
1037 #endif /* SIOCDIFADDR */
1040 * Disassociate. The child will send itself a signal when it is
1041 * stable. This indicates that other processes may begin using
1044 switch (i = server_lock("atalkd", pidfile, debug)) {
1049 default: /* parent */
1051 * Wait for the child to send itself a SIGSTOP, after which
1052 * we send it a SIGCONT and exit ourself.
1054 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1055 perror( "wait3" ); /* Child died? */
1058 if ( !WIFSTOPPED( status )) {
1059 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1060 if ( WIFEXITED( status )) {
1061 fprintf( stderr, " Child exited with %d.\n",
1062 WEXITSTATUS( status ));
1064 fprintf( stderr, " Child died.\n" );
1068 if ( kill(i, SIGCONT ) < 0 ) {
1076 openlog( prog, LOG_PID );
1078 openlog( prog, LOG_PID, LOG_DAEMON );
1081 syslog( LOG_INFO, "restart (%s)", version );
1084 * Socket for use in routing ioctl()s. Can't add routes to our
1085 * interfaces until we have our routing socket.
1088 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1089 syslog( LOG_ERR, "route socket: %m" );
1092 if ( shutdown( rtfd, 0 ) < 0 ) {
1093 syslog( LOG_ERR, "route shutdown: %m" );
1097 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1098 syslog( LOG_ERR, "route socket: %m" );
1103 memset(&sv, 0, sizeof(sv));
1104 sv.sa_handler = as_down;
1105 sigemptyset( &sv.sa_mask );
1106 sigaddset( &sv.sa_mask, SIGUSR1 );
1107 sigaddset( &sv.sa_mask, SIGALRM );
1108 sigaddset( &sv.sa_mask, SIGTERM );
1109 sv.sa_flags = SA_RESTART;
1110 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1111 syslog( LOG_ERR, "sigterm: %m" );
1115 sv.sa_handler = as_debug;
1116 sigemptyset( &sv.sa_mask );
1117 sigaddset( &sv.sa_mask, SIGUSR1 );
1118 sigaddset( &sv.sa_mask, SIGALRM );
1119 sigaddset( &sv.sa_mask, SIGTERM );
1120 sv.sa_flags = SA_RESTART;
1121 if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1122 syslog( LOG_ERR, "sigusr1: %m" );
1126 sv.sa_handler = as_timer;
1127 sigemptyset( &sv.sa_mask );
1128 sigaddset( &sv.sa_mask, SIGUSR1 );
1129 sigaddset( &sv.sa_mask, SIGALRM );
1130 sigaddset( &sv.sa_mask, SIGTERM );
1131 sv.sa_flags = SA_RESTART;
1132 if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1133 syslog( LOG_ERR, "sigalrm: %m" );
1137 it.it_interval.tv_sec = 10L;
1138 it.it_interval.tv_usec = 0L;
1139 it.it_value.tv_sec = 10L;
1140 it.it_value.tv_usec = 0L;
1141 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1142 syslog( LOG_ERR, "setitimer: %m" );
1146 ciface = interfaces;
1150 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1151 if ( errno == EINTR ) {
1155 syslog( LOG_ERR, "select: %m" );
1160 for ( iface = interfaces; iface; iface = iface->i_next ) {
1161 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1162 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1163 if ( ap->ap_packet ) {
1164 fromlen = sizeof( struct sockaddr_at );
1165 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1166 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1167 syslog( LOG_ERR, "recvfrom: %m" );
1172 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1173 ntohs( sat.sat_addr.s_net ),
1174 sat.sat_addr.s_node, iface->i_name,
1175 iface->i_flags, ap->ap_port, ap->ap_fd );
1176 bprint( Packet, c );
1180 if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
1181 syslog( LOG_ERR, "sighold: %m" );
1184 #else /* __svr4__ */
1185 mask = sigsetmask( sigmask( SIGALRM ) |
1186 sigmask( SIGUSR1 ));
1187 #endif /* __svr4__ */
1188 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1189 syslog(LOG_ERR, "ap->ap_packet: %m");
1197 if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
1198 syslog( LOG_ERR, "sigrelse: %m" );
1201 #else /* __svr4__ */
1203 #endif /* __svr4__ */
1212 * This code is called (from main(), as_timer(), zip_packet(),
1213 * and rtmp_packet()) to set the initial "bootstrapping" address
1216 void bootaddr( iface )
1217 struct interface *iface;
1224 if ( iface->i_flags & IFACE_ADDR ) {
1225 syslog( LOG_ERR, "bootaddr OOPS!" );
1229 if ( iface->i_flags & IFACE_PHASE1 ) {
1230 setaddr( iface, IFACE_PHASE1, 0,
1231 iface->i_caddr.sat_addr.s_node, 0, 0 );
1233 if ( iface->i_flags & IFACE_LOOPBACK ) {
1234 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1235 if ( ciface == iface ) {
1236 ciface = ciface->i_next;
1240 } else if (rtmp_request( iface ) < 0) {
1241 syslog(LOG_ERR, "bootaddr (rtmp_request): %m");
1246 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1247 iface->i_caddr.sat_addr.s_node,
1248 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1250 if ( iface->i_flags & IFACE_LOOPBACK ) {
1251 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1252 if ( ciface == iface ) {
1253 ciface = ciface->i_next;
1257 } else if (zip_getnetinfo( iface ) < 0) {
1258 syslog(LOG_ERR, "bootaddr (zip_getnetinfo): %m");
1263 iface->i_flags |= IFACE_ADDR;
1264 stabletimer = UNSTABLE;
1270 * to manage the i_ports field and the fds for select().
1272 void setaddr( iface, phase, net, node, first, last )
1273 struct interface *iface;
1277 u_int16_t first, last;
1283 struct sockaddr_at sat;
1286 if ( iface->i_ports == NULL ) { /* allocate port structures */
1287 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1288 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1289 syslog( LOG_INFO, "%s: service unknown", as->as_name );
1291 as->as_port = ntohs( se->s_port );
1293 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1295 syslog( LOG_ERR, "malloc: %m" );
1299 ap->ap_next = iface->i_ports;
1300 ap->ap_iface = iface;
1301 ap->ap_port = as->as_port;
1302 ap->ap_packet = as->as_packet;
1304 iface->i_ports = ap;
1306 } else { /* close ports */
1307 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1308 (void)close( ap->ap_fd );
1313 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1315 iface->i_addr.sat_family = AF_APPLETALK;
1316 iface->i_addr.sat_addr.s_net = net;
1317 iface->i_addr.sat_addr.s_node = node;
1319 nr.nr_phase = phase;
1320 nr.nr_firstnet = first;
1321 nr.nr_lastnet = last;
1322 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1324 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1325 syslog( LOG_ERR, "setifaddr: %s (%u-%u): %m. try specifying a \
1326 smaller net range.", iface->i_name, ntohs(first), ntohs(last));
1329 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1330 syslog( LOG_ERR, "getifaddr: %s: %m", iface->i_name );
1335 i = 1; /* enable broadcasts */
1337 syslog(LOG_INFO, "setsockopt incompatible w/ Solaris STREAMS module.");
1338 #endif /* __svr4__ */
1339 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1340 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1341 syslog( LOG_ERR, "socket: %m" );
1345 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1346 #endif /* ! __svr4 */
1348 memset( &sat, 0, sizeof( struct sockaddr_at ));
1350 sat.sat_len = sizeof( struct sockaddr_at );
1352 sat.sat_family = AF_APPLETALK;
1353 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1354 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1355 sat.sat_port = ap->ap_port;
1357 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1358 sizeof( struct sockaddr_at )) < 0 ) {
1359 syslog( LOG_ERR, "bind %u.%u:%u: %m",
1360 ntohs( sat.sat_addr.s_net ),
1361 sat.sat_addr.s_node, sat.sat_port );
1363 /* remove all interfaces if we have a problem with bind */
1364 for (iface = interfaces; iface; iface = iface->i_next) {
1365 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1366 #ifdef SIOCATALKDIFADDR
1367 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1368 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1369 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1370 #endif /* SIOCATALKDIFADDR */
1373 #endif /* SIOCDIFADDR */
1378 /* recalculate nfds and fds */
1380 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1381 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1382 FD_SET( ap->ap_fd, &fds );
1383 if ( ap->ap_fd > nfds ) {
1391 int ifconfig( iname, cmd, sa )
1394 struct sockaddr_at *sa;
1399 memset(&ifr, 0, sizeof(ifr));
1400 strcpy( ifr.ifr_name, iname );
1401 ifr.ifr_addr = *(struct sockaddr *)sa;
1403 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1406 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1411 if ( cmd == SIOCGIFADDR ) {
1412 *(struct sockaddr *)sa = ifr.ifr_addr;
1417 void dumpconfig( iface )
1418 struct interface *iface;
1422 printf( "%s", iface->i_name );
1423 if ( iface->i_flags & IFACE_RSEED ) {
1424 printf( " -router" );
1425 } else if ( iface->i_flags & IFACE_SEED ) {
1429 if ( iface->i_flags & IFACE_DONTROUTE)
1430 printf( " -dontroute");
1432 printf( " -phase" );
1433 if ( iface->i_flags & IFACE_PHASE1 ) {
1438 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1439 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1440 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1442 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1443 iface->i_addr.sat_addr.s_node );
1444 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1445 iface->i_caddr.sat_addr.s_node );
1446 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1447 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1448 ((struct ziptab *)l->l_data)->zt_name );
1456 struct interface *iface;
1457 struct rtmptab *rtmp;
1461 for ( iface = interfaces; iface; iface = iface->i_next ) {
1462 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1463 if ( rtmp->rt_gate == 0 ) {
1464 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1465 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1466 ntohs( rtmp->rt_lastnet ));
1468 printf( "%u", ntohs( rtmp->rt_firstnet ));
1471 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1472 printf( "%u.%u for %u-%u",
1473 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1474 rtmp->rt_gate->g_sat.sat_addr.s_node,
1475 ntohs( rtmp->rt_firstnet ),
1476 ntohs( rtmp->rt_lastnet ));
1478 printf( "%u.%u for %u",
1479 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1480 rtmp->rt_gate->g_sat.sat_addr.s_node,
1481 ntohs( rtmp->rt_firstnet ));
1485 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1489 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1490 zt = (struct ziptab *)l->l_data;
1491 printf( " %.*s", zt->zt_len, zt->zt_name );
1504 struct interface *iface;
1505 struct rtmptab *rtmp;
1509 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1510 printf( "%.*s", zt->zt_len, zt->zt_name );
1511 for ( l = zt->zt_rt; l; l = l->l_next ) {
1512 rtmp = (struct rtmptab *)l->l_data;
1513 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1514 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1515 ntohs( rtmp->rt_lastnet ));
1517 printf( " %u", ntohs( rtmp->rt_firstnet ));
1519 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {