2 * $Id: main.c,v 1.17.8.1 2004-01-10 08:09:12 bfernhomberg 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 */
35 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
41 #include <net/route.h>
44 #include <net/route.h>
46 #include <netinet/in.h>
49 #include <atalk/logger.h>
57 #include <netatalk/endian.h>
58 #include <netatalk/at.h>
59 #include <atalk/compat.h>
60 #include <atalk/zip.h>
61 #include <atalk/rtmp.h>
62 #include <atalk/ddp.h>
63 #include <atalk/atp.h>
64 #include <atalk/paths.h>
65 #include <atalk/util.h>
68 #include <sys/sockio.h>
72 #include "interface.h"
80 /* Forward Declarations */
81 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
83 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
85 #define SOCKLEN_T unsigned int
86 #endif /* SOCKLEN_T */
89 #define WEXITSTATUS(x) ((x).w_retcode)
90 #endif /* WEXITSTATUS */
92 /* linux has a special ioctl for appletalk device destruction. as of
93 * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
94 * fact that SIOCDIFADDR may be defined on linux despite the fact that
96 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
97 #define SIOCDIFADDR SIOCATALKDIFADDR
100 #define elements(a) (sizeof(a)/sizeof((a)[0]))
104 extern int rtmp_packet();
105 extern int nbp_packet();
106 extern int aep_packet();
107 extern int zip_packet();
111 struct atserv atserv[] = {
112 { "rtmp", 1, rtmp_packet }, /* 0 */
113 { "nbp", 2, nbp_packet }, /* 1 */
114 { "echo", 4, aep_packet }, /* 2 */
115 { "zip", 6, zip_packet }, /* 3 */
117 int atservNATSERV = elements( atserv );
119 struct interface *interfaces = NULL, *ciface = NULL;
121 int debug = 0, quiet = 0, chatty = 0;
122 char *configfile = NULL;
123 int ziptimeout = 0, transition = 0;
124 int stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
125 static int ninterfaces;
126 int defphase = IFACE_PHASE2;
129 char Packet[ PKTSZ ];
130 char *version = VERSION;
131 static char *pidfile = _PATH_ATALKDLOCK;
136 int readconf( char * );
137 int getifconf( void );
138 int writeconf( char * );
140 /* this is the messiest of the bunch as atalkd can exit pretty much
141 * everywhere. we delete interfaces here instead of in as_down. */
142 static void atalkd_exit(const int i)
145 struct interface *iface;
147 for (iface = interfaces; iface; iface = iface->i_next) {
148 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
149 #ifdef SIOCATALKDIFADDR
150 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
151 if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr))
153 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
154 #endif /* SIOCATALKIFADDR */
155 LOG(log_error, logtype_atalkd, "difaddr(%u.%u): %s",
156 ntohs(iface->i_addr.sat_addr.s_net),
157 iface->i_addr.sat_addr.s_node, strerror(errno));
160 #endif /* SOPCDOFADDR */
162 server_unlock(pidfile);
167 static void as_timer(int sig)
169 struct sockaddr_at sat;
172 struct rtmp_tuple rt;
173 struct atport *ap, *zap, *rap;
174 struct interface *iface, *iface2;
175 struct gate *gate, *fgate = NULL;
176 struct rtmptab *rtmp, *frtmp;
178 char *data, *end, packet[ ATP_BUFSIZ ];
184 memset(&sat, 0, sizeof( struct sockaddr_at ));
185 for ( iface = interfaces; iface; iface = iface->i_next ) {
186 if ( iface->i_flags & IFACE_LOOPBACK ) {
189 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
190 if ( ap->ap_packet == zip_packet ) {
193 if ( ap->ap_packet == rtmp_packet ) {
198 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
200 if ( iface->i_time < 3 ) {
201 if ( iface->i_flags & IFACE_PHASE1 ) {
202 if (rtmp_request( iface ) < 0) {
203 LOG(log_error, logtype_atalkd, "rtmp_request: %s", strerror(errno));
208 if (zip_getnetinfo( iface ) < 0) {
209 LOG(log_error, logtype_atalkd, "zip_getnetinfo: %s", strerror(errno));
216 iface->i_flags |= IFACE_NOROUTER;
217 if ((iface->i_flags & IFACE_ISROUTER)) {
218 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
220 * No seed info, and we've got multiple interfaces.
223 LOG(log_info, logtype_atalkd,
224 "as_timer multiple interfaces, no seed" );
225 LOG(log_info, logtype_atalkd, "as_timer can't configure %s",
227 LOG(log_info, logtype_atalkd, "as_timer waiting for router" );
232 * Complete configuration for iface, and boot next
235 iface->i_flags |= IFACE_CONFIG;
236 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
237 if (addzone( iface->i_rt, zt->zt_len,
239 LOG(log_error, logtype_atalkd, "addzone: %s", strerror(errno));
243 if ( iface->i_rt->rt_zt ) {
244 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
245 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
247 if ( iface->i_flags & IFACE_PHASE1 ) {
248 LOG(log_info, logtype_atalkd,
249 "as_timer configured %s phase 1 from seed",
251 setaddr( iface, IFACE_PHASE1,
252 iface->i_caddr.sat_addr.s_net,
253 iface->i_addr.sat_addr.s_node,
254 iface->i_caddr.sat_addr.s_net,
255 iface->i_caddr.sat_addr.s_net );
257 LOG(log_info, logtype_atalkd,
258 "as_timer configured %s phase 2 from seed",
262 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
263 LOG(log_error, logtype_atalkd,
264 "as_timer: can't route %u.%u to loop: %s",
265 ntohs( iface->i_addr.sat_addr.s_net ),
266 iface->i_addr.sat_addr.s_node,
270 if ( iface == ciface ) {
271 ciface = ciface->i_next;
277 * Configure for no router operation. Wait for a route
278 * to become available in rtmp_packet().
280 LOG(log_info, logtype_atalkd, "config for no router" );
282 if ( iface->i_flags & IFACE_PHASE2 ) {
283 iface->i_rt->rt_firstnet = 0;
284 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
285 setaddr( iface, IFACE_PHASE2,
286 iface->i_addr.sat_addr.s_net,
287 iface->i_addr.sat_addr.s_node,
288 0, htons( STARTUP_LASTNET ));
290 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
291 LOG(log_error, logtype_atalkd,
292 "as_timer: can't route %u.%u to loopback: %s",
293 ntohs( iface->i_addr.sat_addr.s_net ),
294 iface->i_addr.sat_addr.s_node,
299 if ( iface == ciface ) {
300 ciface = ciface->i_next;
307 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
309 free( (caddr_t)fgate );
314 data = packet + 1 + sizeof( struct ziphdr );
315 end = packet + sizeof( packet );
318 sat.sat_port = zap->ap_port;
321 * Perform timeouts on routers. If we've only got one
322 * interface, we'll use these timeouts to decide that
323 * our zone has gone away.
325 if ( ++gate->g_state >= RTMPTAB_BAD ) {
326 LOG(log_info, logtype_atalkd, "as_timer gateway %u.%u down",
327 ntohs( gate->g_sat.sat_addr.s_net ),
328 gate->g_sat.sat_addr.s_node );
331 frtmp = rtmp->rt_next;
332 if ( rtmp->rt_hops == RTMPHOPS_POISON ||
333 rtmp->rt_iprev == 0 ) {
336 rtmp->rt_hops = RTMPHOPS_POISON;
337 if ((cc = rtmp_replace( rtmp )) < 0) {
338 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
342 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
347 if ( gate->g_rt == 0 ) {
348 if ( gate->g_prev == 0 ) {
349 gate->g_iface->i_gate = gate->g_next;
351 gate->g_prev->g_next = gate->g_next;
353 if ( gate->g_next != 0 ) {
354 gate->g_next->g_prev = gate->g_prev;
356 fgate = gate; /* can't free here, just mark it */
359 * If this is the last router on the only interface,
360 * reconfigure our netrange. By marking the interface
361 * as having no router, we will notice when a router
364 * XXX: actually, we always reconfigure an interface
365 * if we're not a seed router.
368 if ( gate->g_iface->i_gate == 0 &&
369 ((iface->i_flags & IFACE_SEED) == 0)) {
370 gate->g_iface->i_flags |= IFACE_NOROUTER;
371 gate->g_iface->i_flags &= ~IFACE_CONFIG;
373 /* get rid of any zones associated with this iface */
374 if (gate->g_iface->i_rt->rt_zt) {
375 rtmp_delzonemap(gate->g_iface->i_rt);
376 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
379 LOG(log_info, logtype_atalkd, "as_timer last gateway down" );
381 /* Set netrange to 0-fffe. */
382 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
383 gate->g_iface->i_rt->rt_firstnet = 0;
384 gate->g_iface->i_rt->rt_lastnet =
385 htons( STARTUP_LASTNET );
386 setaddr( iface, IFACE_PHASE2,
387 iface->i_addr.sat_addr.s_net,
388 iface->i_addr.sat_addr.s_node,
389 0, htons( STARTUP_LASTNET ));
396 * If we don't have a zone for our interface yet, ask for
397 * it from any router (all routers) on the interface.
399 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
400 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
401 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
402 data += sizeof( u_short );
409 * Delete old routing tuples.
411 if ( rtmp->rt_state != RTMPTAB_PERM ) {
416 * We've not been updated for this route in a while. If
417 * it's not in use, go ahead and remove it. If it is in
418 * use, mark the route as down (POISON), and look for a
419 * better route. If one is found, delete this route and use
420 * the new one. If it's not found, mark the route as GOOD
421 * (so we'll propogate our poison) and delete it the next
422 * time it becomes BAD.
424 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
425 frtmp = rtmp->rt_next;
426 if ( rtmp->rt_iprev == 0 ) { /* not in use */
428 } else { /* in use */
429 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
432 rtmp->rt_hops = RTMPHOPS_POISON;
433 if ((cc = rtmp_replace( rtmp )) < 0) {
434 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
438 rtmp->rt_state = RTMPTAB_GOOD;
448 if ( rtmp->rt_iprev &&
449 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
450 if ( data + sizeof( u_short ) > end || n == 255 ) {
451 /* send what we've got */
452 zh.zh_op = ZIPOP_QUERY;
456 *data++ = DDPTYPE_ZIP;
457 memcpy( data, &zh, sizeof( struct ziphdr ));
459 if ( sendto( zap->ap_fd, packet, cc, 0,
460 (struct sockaddr *)&sat,
461 sizeof( struct sockaddr_at )) < 0 ) {
462 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
467 data = packet + 1 + sizeof( struct ziphdr );
468 end = packet + sizeof( packet );
472 * rt_nzq is number of ZIP Queries we've issued for a
473 * given netrange. If we've got ziptimeout on, we
474 * will only ask 3 times for any given netrange.
475 * Interestingly enough, since rt_nzq is a u_char,
476 * it will overflow after a while. This means we will
477 * periodically ask for nets that we've decided not to
478 * ask about, and warn that we can't get it's zone.
480 if ( rtmp->rt_nzq++ == 3 ) {
481 LOG(log_info, logtype_atalkd, "as_timer can't get zone for %u",
482 ntohs( rtmp->rt_firstnet ));
484 if ( rtmp->rt_nzq > 3 ) {
486 rtmp = rtmp->rt_next;
492 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
493 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
494 data += sizeof( u_short );
497 rtmp = rtmp->rt_next;
500 /* send what we've got */
502 zh.zh_op = ZIPOP_QUERY;
506 *data++ = DDPTYPE_ZIP;
507 memcpy( data, &zh, sizeof( struct ziphdr ));
509 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
510 sizeof( struct sockaddr_at )) < 0 ) {
511 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
516 free( (caddr_t)fgate );
521 * Send RTMP broadcasts if we have multiple interfaces or our
522 * interface is configured as a router.
524 if ((iface->i_flags & IFACE_ISROUTER)) {
526 sat.sat_len = sizeof( struct sockaddr_at );
528 sat.sat_family = AF_APPLETALK;
529 sat.sat_addr.s_net = ATADDR_ANYNET;
530 sat.sat_addr.s_node = ATADDR_BCAST;
531 sat.sat_port = rap->ap_port;
534 end = data + sizeof( packet );
535 *data++ = DDPTYPE_RTMPRD;
536 rh.rh_net = iface->i_addr.sat_addr.s_net;
538 rh.rh_node = iface->i_addr.sat_addr.s_node;
539 memcpy( data, &rh, sizeof( struct rtmp_head ));
540 data += sizeof( struct rtmp_head );
544 if ( iface->i_flags & IFACE_PHASE1 ) {
547 memcpy( data, &rt, SZ_RTMPTUPLE );
548 data += SZ_RTMPTUPLE;
550 rt.rt_net = iface->i_rt->rt_firstnet;
552 memcpy( data, &rt, SZ_RTMPTUPLE );
553 data += SZ_RTMPTUPLE;
555 rt.rt_net = iface->i_rt->rt_lastnet;
557 memcpy( data, &rt, SZ_RTMPTUPLE );
558 data += SZ_RTMPTUPLE;
561 for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
563 /* XXX: there used to be a bit checking against iface ==
564 iface2. also, we don't want to send an rtmp broadcast
565 to an interface that doesn't want it. */
566 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
567 ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
571 * Fill in tuples. Always send the same thing, regardless
572 * of the phase of the destination. Routers who don't
573 * understand extended rtmp packets will toss extended
574 * tuples because their distance will have the high bit set.
576 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
577 /* don't broadcast routes we have no zone for */
578 if ( rtmp->rt_zt == 0 ||
579 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
580 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
584 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
585 data + 2 * SZ_RTMPTUPLE > end ) ||
586 data + SZ_RTMPTUPLE > end ) {
587 if ( sendto( rap->ap_fd, packet, data - packet, 0,
588 (struct sockaddr *)&sat,
589 sizeof( struct sockaddr_at )) < 0 ) {
590 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
591 ntohs( sat.sat_addr.s_net ),
593 ntohs( iface->i_rt->rt_firstnet ),
597 if ( iface->i_flags & IFACE_PHASE2 ) {
598 data = packet + 1 + sizeof( struct rtmp_head ) +
601 data = packet + 1 + sizeof( struct rtmp_head ) +
607 rt.rt_net = rtmp->rt_firstnet;
608 rt.rt_dist = rtmp->rt_hops;
609 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
612 memcpy( data, &rt, SZ_RTMPTUPLE );
613 data += SZ_RTMPTUPLE;
615 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
616 rt.rt_net = rtmp->rt_lastnet;
618 memcpy( data, &rt, SZ_RTMPTUPLE );
619 data += SZ_RTMPTUPLE;
627 if ( sendto( rap->ap_fd, packet, data - packet, 0,
628 (struct sockaddr *)&sat,
629 sizeof( struct sockaddr_at )) < 0 ) {
630 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
631 ntohs( sat.sat_addr.s_net ),
633 ntohs( iface->i_rt->rt_firstnet ),
641 * Check if we're stable. Each time we configure an interface, we
642 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
643 * STABLEANYWAY, we give up and decide to "be" stable anyway.
644 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
645 * data and all zip data complete.
648 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
649 /* write out config file */
651 writeconf( configfile );
653 if ( stabletimer-- <= STABLEANYWAY ) {
659 if ( stable && !noparent ) {
661 LOG(log_info, logtype_atalkd, "ready %d/%d/%d", stabletimer, newrtmpdata,
665 * Seems like we could get here more than once...
667 if ( kill( getpid(), SIGSTOP ) < 0 ) {
668 LOG(log_error, logtype_atalkd, "as_timer: kill-self failed!" );
682 * Consistency check...
686 struct rtmptab *rtmp;
687 struct list *lr, *lz;
690 for ( zt = ziptab; zt; zt = zt->zt_next ) {
691 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
692 rtmp = (struct rtmptab *)lr->l_data;
693 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
694 LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)\n",
695 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
696 ntohs( rtmp->rt_lastnet ));
699 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
700 if ( zt == (struct ziptab *)lz->l_data ) {
705 LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s\n",
706 ntohs( rtmp->rt_firstnet ),
707 ntohs( rtmp->rt_lastnet ),
708 zt->zt_len, zt->zt_name );
716 #if !defined( ibm032 ) && !defined( _IBMR2 )
718 #endif /* ! ibm032 && ! _IBMR2 */
721 struct interface *iface;
728 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
729 LOG(log_error, logtype_atalkd, "rtmp: %s", strerror(errno) );
732 for ( iface = interfaces; iface; iface = iface->i_next ) {
733 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
734 ntohs( iface->i_addr.sat_addr.s_net ),
735 iface->i_addr.sat_addr.s_node );
736 if ( iface->i_flags & IFACE_PHASE1 ) {
737 putc( '1', rtmpdebug );
739 if ( iface->i_flags & IFACE_PHASE2 ) {
740 putc( '2', rtmpdebug );
742 if ( iface->i_flags & IFACE_RSEED ) {
743 putc( 'R', rtmpdebug );
745 if ( iface->i_flags & IFACE_SEED ) {
746 putc( 'S', rtmpdebug );
748 if ( iface->i_flags & IFACE_DONTROUTE ) {
749 putc( 'D', rtmpdebug );
751 if ( iface->i_flags & IFACE_ADDR ) {
752 putc( 'A', rtmpdebug );
754 if ( iface->i_flags & IFACE_CONFIG ) {
755 putc( 'C', rtmpdebug );
757 if ( iface->i_flags & IFACE_NOROUTER ) {
758 putc( 'N', rtmpdebug );
760 if ( iface->i_flags & IFACE_LOOP ) {
761 putc( 'L', rtmpdebug );
763 putc( '\n', rtmpdebug );
766 fprintf( rtmpdebug, "\t%u-%u ",
767 ntohs( iface->i_rt->rt_firstnet ),
768 ntohs( iface->i_rt->rt_lastnet ));
769 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
770 putc( 'q', rtmpdebug );
772 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
773 putc( 'z', rtmpdebug );
775 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
776 putc( 'x', rtmpdebug );
778 putc( 'i', rtmpdebug );
779 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
780 zt = (struct ziptab *)l->l_data;
781 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
783 fprintf( rtmpdebug, "\n" );
786 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
787 fprintf( rtmpdebug, "gate %u.%u %X\n",
788 ntohs( gate->g_sat.sat_addr.s_net ),
789 gate->g_sat.sat_addr.s_node, gate->g_state );
790 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
791 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
792 ntohs( rt->rt_lastnet ));
793 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
794 putc( 'q', rtmpdebug );
796 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
797 putc( 'z', rtmpdebug );
799 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
800 putc( 'x', rtmpdebug );
802 if ( rt->rt_iprev ) {
803 putc( 'i', rtmpdebug );
805 for ( l = rt->rt_zt; l; l = l->l_next ) {
806 zt = (struct ziptab *)l->l_data;
807 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
809 fprintf( rtmpdebug, "\n" );
818 * Called when SIGTERM is recieved. Remove all routes and then exit.
820 #if !defined( ibm032 ) && !defined( _IBMR2 )
822 #endif /* ! ibm032 && ! _IBMR2 */
825 struct interface *iface;
829 for ( iface = interfaces; iface; iface = iface->i_next ) {
830 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
831 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
832 if ( rt->rt_iprev ) {
833 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
834 LOG(log_error, logtype_atalkd, "as_down remove %u-%u failed: %s",
835 ntohs( rt->rt_firstnet ),
836 ntohs( rt->rt_lastnet ),
842 if ( iface->i_flags & IFACE_LOOP ) {
843 if (looproute( iface, RTMP_DEL )) {
844 LOG(log_error, logtype_atalkd, "as_down remove %s %u.%u failed: %s",
845 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
846 iface->i_addr.sat_addr.s_node,
852 LOG(log_info, logtype_atalkd, "done" );
863 struct sockaddr_at sat;
866 struct interface *iface;
875 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
878 defphase = IFACE_PHASE1;
882 defphase = IFACE_PHASE2;
893 case 'q' : /* don't seed */
897 case 's' : /* seed */
901 case 't' : /* transition */
905 case 'P' : /* pid file */
909 case 'v' : /* version */
910 printf( "atalkd (version %s)\n", version );
915 fprintf( stderr, "Unknown option -- '%c'\n", c );
919 if ( optind != ac ) {
920 fprintf( stderr, "Too many arguments.\n" );
924 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
931 * Configure loop back address first, so appearances of "lo0" in
932 * the config file fail. Also insures that lo0 gets configured,
933 * even if there's some hangup during configuration of some
936 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
937 perror( "newiface" );
940 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
943 * Check our initial configuration before we fork. This way we can
944 * complain about syntax errors on stdout.
946 * Basically, if we're going to read our config file, we should read
947 * it and initialize our data structures. If we're not going to read
948 * our config file, use GIFCONF to initialize our data structures.
950 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
951 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
955 /* we need to count up our interfaces so that we can simplify things
956 * later. we also need to figure out if we have more than one interface
957 * that is routing. */
958 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
959 iface=iface->i_next) {
960 if (iface->i_flags & IFACE_DONTROUTE)
964 i = ninterfaces - i; /* number of routable interfaces */
967 * At this point, we have (at least partially) initialized data
968 * structures. Fill in what we can and verify that nothing is obviously
971 for (iface = interfaces; iface; iface = iface->i_next) {
972 /* Apply the default phase */
973 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
974 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
975 iface->i_flags |= defphase;
978 /* set up router flag information. if we have multiple interfaces
979 * and DONTROUTE isn't set, set up ROUTER. i is the number of
980 * interfaces that don't have the DONTROUTE flag set. */
981 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
982 iface->i_flags |= IFACE_ISROUTER;
985 /* Set default addresses */
986 if ( iface->i_rt == NULL ) {
987 if (( iface->i_rt = newrt(iface)) == NULL ) {
992 if ( iface->i_flags & IFACE_PHASE1 ) {
993 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
994 iface->i_caddr.sat_addr.s_net;
996 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
997 ( iface->i_flags & IFACE_LOOPBACK )) {
998 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
999 iface->i_caddr.sat_addr.s_net;
1001 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
1002 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
1007 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
1008 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
1011 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1012 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1016 dumpconfig( iface ); /* probably needs args */
1021 * A little consistency check...
1023 if ( ninterfaces < IFBASE ) {
1024 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1028 /* do this here so that we can use ifconfig */
1030 if ( plumb() < 0 ) {
1031 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1034 #endif /* __svr4__ */
1036 /* delete pre-existing interface addresses. */
1038 for (iface = interfaces; iface; iface = iface->i_next) {
1039 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1040 #ifdef SIOCATALKDIFADDR
1041 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1042 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1043 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1044 #endif /* SIOCATALKDIFADDR */
1047 #endif /* SIOCDIFADDR */
1050 * Disassociate. The child will send itself a signal when it is
1051 * stable. This indicates that other processes may begin using
1054 switch (i = server_lock("atalkd", pidfile, debug)) {
1059 default: /* parent */
1061 * Wait for the child to send itself a SIGSTOP, after which
1062 * we send it a SIGCONT and exit ourself.
1064 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1065 perror( "wait3" ); /* Child died? */
1068 if ( !WIFSTOPPED( status )) {
1069 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1070 if ( WIFEXITED( status )) {
1071 fprintf( stderr, " Child exited with %d.\n",
1072 WEXITSTATUS( status ));
1074 fprintf( stderr, " Child died.\n" );
1078 if ( kill(i, SIGCONT ) < 0 ) {
1086 openlog( prog, LOG_PID );
1088 set_processname(prog);
1089 syslog_setup(log_debug, logtype_default, logoption_pid, logfacility_daemon );
1092 LOG(log_info, logtype_atalkd, "restart (%s)", version );
1095 * Socket for use in routing ioctl()s. Can't add routes to our
1096 * interfaces until we have our routing socket.
1099 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1100 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1103 if ( shutdown( rtfd, 0 ) < 0 ) {
1104 LOG(log_error, logtype_atalkd, "route shutdown: %s", strerror(errno) );
1108 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1109 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1114 ciface = interfaces;
1117 memset(&sv, 0, sizeof(sv));
1118 sv.sa_handler = as_down;
1119 sigemptyset( &sv.sa_mask );
1120 sigaddset( &sv.sa_mask, SIGUSR1 );
1121 sigaddset( &sv.sa_mask, SIGALRM );
1122 sigaddset( &sv.sa_mask, SIGTERM );
1123 sv.sa_flags = SA_RESTART;
1124 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1125 LOG(log_error, logtype_atalkd, "sigterm: %s", strerror(errno) );
1129 sv.sa_handler = as_debug;
1130 sigemptyset( &sv.sa_mask );
1131 sigaddset( &sv.sa_mask, SIGUSR1 );
1132 sigaddset( &sv.sa_mask, SIGALRM );
1133 sigaddset( &sv.sa_mask, SIGTERM );
1134 sv.sa_flags = SA_RESTART;
1135 if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1136 LOG(log_error, logtype_atalkd, "sigusr1: %s", strerror(errno) );
1140 sv.sa_handler = as_timer;
1141 sigemptyset( &sv.sa_mask );
1142 sigaddset( &sv.sa_mask, SIGUSR1 );
1143 sigaddset( &sv.sa_mask, SIGALRM );
1144 sigaddset( &sv.sa_mask, SIGTERM );
1145 sv.sa_flags = SA_RESTART;
1146 if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1147 LOG(log_error, logtype_atalkd, "sigalrm: %s", strerror(errno) );
1151 it.it_interval.tv_sec = 10L;
1152 it.it_interval.tv_usec = 0L;
1153 it.it_value.tv_sec = 10L;
1154 it.it_value.tv_usec = 0L;
1155 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1156 LOG(log_error, logtype_atalkd, "setitimer: %s", strerror(errno) );
1162 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1163 if ( errno == EINTR ) {
1167 LOG(log_error, logtype_atalkd, "select: %s", strerror(errno) );
1172 for ( iface = interfaces; iface; iface = iface->i_next ) {
1173 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1174 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1175 if ( ap->ap_packet ) {
1176 fromlen = sizeof( struct sockaddr_at );
1177 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1178 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1179 LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
1184 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1185 ntohs( sat.sat_addr.s_net ),
1186 sat.sat_addr.s_node, iface->i_name,
1187 iface->i_flags, ap->ap_port, ap->ap_fd );
1188 bprint( Packet, c );
1192 if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
1193 LOG(log_error, logtype_atalkd, "sighold: %s", strerror(errno) );
1196 #else /* __svr4__ */
1197 mask = sigsetmask( sigmask( SIGALRM ) |
1198 sigmask( SIGUSR1 ));
1199 #endif /* __svr4__ */
1200 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1201 LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
1209 if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
1210 LOG(log_error, logtype_atalkd, "sigrelse: %s", strerror(errno) );
1213 #else /* __svr4__ */
1215 #endif /* __svr4__ */
1224 * This code is called (from main(), as_timer(), zip_packet(),
1225 * and rtmp_packet()) to set the initial "bootstrapping" address
1228 void bootaddr( iface )
1229 struct interface *iface;
1236 if ( iface->i_flags & IFACE_ADDR ) {
1237 LOG(log_error, logtype_atalkd, "bootaddr OOPS!" );
1241 if ( iface->i_flags & IFACE_PHASE1 ) {
1242 setaddr( iface, IFACE_PHASE1, 0,
1243 iface->i_caddr.sat_addr.s_node, 0, 0 );
1245 if ( iface->i_flags & IFACE_LOOPBACK ) {
1246 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1247 if ( ciface == iface ) {
1248 ciface = ciface->i_next;
1252 } else if (rtmp_request( iface ) < 0) {
1253 LOG(log_error, logtype_atalkd, "bootaddr (rtmp_request): %s", strerror(errno));
1258 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1259 iface->i_caddr.sat_addr.s_node,
1260 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1262 if ( iface->i_flags & IFACE_LOOPBACK ) {
1263 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1264 if ( ciface == iface ) {
1265 ciface = ciface->i_next;
1269 } else if (zip_getnetinfo( iface ) < 0) {
1270 LOG(log_error, logtype_atalkd, "bootaddr (zip_getnetinfo): %s", strerror(errno));
1275 iface->i_flags |= IFACE_ADDR;
1276 stabletimer = UNSTABLE;
1282 * to manage the i_ports field and the fds for select().
1284 void setaddr(struct interface *iface,
1285 u_int8_t phase, u_int16_t net, u_int8_t node,
1286 u_int16_t first, u_int16_t last)
1292 struct sockaddr_at sat;
1295 if ( iface->i_ports == NULL ) { /* allocate port structures */
1296 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1297 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1298 LOG(log_info, logtype_atalkd, "%s: service unknown", as->as_name );
1300 as->as_port = ntohs( se->s_port );
1302 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1304 LOG(log_error, logtype_atalkd, "malloc: %s", strerror(errno) );
1308 ap->ap_next = iface->i_ports;
1309 ap->ap_iface = iface;
1310 ap->ap_port = as->as_port;
1311 ap->ap_packet = as->as_packet;
1313 iface->i_ports = ap;
1315 } else { /* close ports */
1316 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1317 (void)close( ap->ap_fd );
1322 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1324 iface->i_addr.sat_family = AF_APPLETALK;
1325 iface->i_addr.sat_addr.s_net = net;
1326 iface->i_addr.sat_addr.s_node = node;
1328 nr.nr_phase = phase;
1329 nr.nr_firstnet = first;
1330 nr.nr_lastnet = last;
1331 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1333 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1334 LOG(log_error, logtype_atalkd, "setifaddr: %s (%u-%u): %s. try specifying a \
1335 smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
1338 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1339 LOG(log_error, logtype_atalkd, "getifaddr: %s: %s", iface->i_name, strerror(errno) );
1344 i = 1; /* enable broadcasts */
1346 LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
1347 #endif /* __svr4__ */
1348 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1349 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1350 LOG(log_error, logtype_atalkd, "socket: %s", strerror(errno) );
1354 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1355 #endif /* ! __svr4 */
1357 memset( &sat, 0, sizeof( struct sockaddr_at ));
1359 sat.sat_len = sizeof( struct sockaddr_at );
1361 sat.sat_family = AF_APPLETALK;
1362 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1363 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1364 sat.sat_port = ap->ap_port;
1366 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1367 sizeof( struct sockaddr_at )) < 0 ) {
1368 LOG(log_error, logtype_atalkd, "bind %u.%u:%u: %s",
1369 ntohs( sat.sat_addr.s_net ),
1370 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1372 /* remove all interfaces if we have a problem with bind */
1373 for (iface = interfaces; iface; iface = iface->i_next) {
1374 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1375 #ifdef SIOCATALKDIFADDR
1376 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1377 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1378 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1379 #endif /* SIOCATALKDIFADDR */
1382 #endif /* SIOCDIFADDR */
1387 /* recalculate nfds and fds */
1389 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1390 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1391 FD_SET( ap->ap_fd, &fds );
1392 if ( ap->ap_fd > nfds ) {
1400 int ifconfig( iname, cmd, sa )
1403 struct sockaddr_at *sa;
1408 memset(&ifr, 0, sizeof(ifr));
1409 strcpy( ifr.ifr_name, iname );
1410 ifr.ifr_addr = *(struct sockaddr *)sa;
1412 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1415 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1420 if ( cmd == SIOCGIFADDR ) {
1421 *(struct sockaddr *)sa = ifr.ifr_addr;
1426 void dumpconfig( iface )
1427 struct interface *iface;
1431 printf( "%s", iface->i_name );
1432 if ( iface->i_flags & IFACE_RSEED ) {
1433 printf( " -router" );
1434 } else if ( iface->i_flags & IFACE_SEED ) {
1438 if ( iface->i_flags & IFACE_DONTROUTE)
1439 printf( " -dontroute");
1441 printf( " -phase" );
1442 if ( iface->i_flags & IFACE_PHASE1 ) {
1447 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1448 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1449 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1451 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1452 iface->i_addr.sat_addr.s_node );
1453 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1454 iface->i_caddr.sat_addr.s_node );
1455 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1456 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1457 ((struct ziptab *)l->l_data)->zt_name );
1465 struct interface *iface;
1466 struct rtmptab *rtmp;
1470 for ( iface = interfaces; iface; iface = iface->i_next ) {
1471 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1472 if ( rtmp->rt_gate == 0 ) {
1473 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1474 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1475 ntohs( rtmp->rt_lastnet ));
1477 printf( "%u", ntohs( rtmp->rt_firstnet ));
1480 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1481 printf( "%u.%u for %u-%u",
1482 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1483 rtmp->rt_gate->g_sat.sat_addr.s_node,
1484 ntohs( rtmp->rt_firstnet ),
1485 ntohs( rtmp->rt_lastnet ));
1487 printf( "%u.%u for %u",
1488 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1489 rtmp->rt_gate->g_sat.sat_addr.s_node,
1490 ntohs( rtmp->rt_firstnet ));
1494 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1498 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1499 zt = (struct ziptab *)l->l_data;
1500 printf( " %.*s", zt->zt_len, zt->zt_name );
1513 struct interface *iface;
1514 struct rtmptab *rtmp;
1518 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1519 printf( "%.*s", zt->zt_len, zt->zt_name );
1520 for ( l = zt->zt_rt; l; l = l->l_next ) {
1521 rtmp = (struct rtmptab *)l->l_data;
1522 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1523 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1524 ntohs( rtmp->rt_lastnet ));
1526 printf( " %u", ntohs( rtmp->rt_firstnet ));
1528 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {