2 * $Id: main.c,v 1.20 2009-10-13 22:55:37 didg 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>
16 #include <sys/resource.h>
17 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_WAIT_H
23 #endif /* HAVE_SYS_WAIT_H */
25 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
26 #endif /* ! WEXITSTATUS */
28 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
29 #endif /* ! WIFEXITED */
31 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
37 #include <net/route.h>
40 #include <net/route.h>
42 #include <netinet/in.h>
45 #include <atalk/logger.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/nbp.h>
59 #include <atalk/ddp.h>
60 #include <atalk/atp.h>
61 #include <atalk/paths.h>
62 #include <atalk/util.h>
65 #include <sys/sockio.h>
69 #include "interface.h"
78 /* Forward Declarations */
79 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
81 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
83 #define SOCKLEN_T unsigned int
84 #endif /* SOCKLEN_T */
87 #define WEXITSTATUS(x) ((x).w_retcode)
88 #endif /* WEXITSTATUS */
90 /* linux has a special ioctl for appletalk device destruction. as of
91 * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
92 * fact that SIOCDIFADDR may be defined on linux despite the fact that
94 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
95 #define SIOCDIFADDR SIOCATALKDIFADDR
98 #define elements(a) (sizeof(a)/sizeof((a)[0]))
102 extern int aep_packet(struct atport *ap, struct sockaddr_at *from, char *data, int len);
106 static 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 static 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 LOG(log_error, logtype_atalkd, "difaddr(%u.%u): %s",
151 ntohs(iface->i_addr.sat_addr.s_net),
152 iface->i_addr.sat_addr.s_node, strerror(errno));
155 if (!(iface->i_flags & IFACE_WASALLMULTI) && (iface->i_flags & IFACE_ALLMULTI))
156 ifsetallmulti(iface->i_name, 0);
159 #endif /* SOPCDOFADDR */
161 server_unlock(pidfile);
166 static void as_timer(int sig _U_)
168 struct sockaddr_at sat;
171 struct rtmp_tuple rt;
172 struct atport *ap, *zap, *rap;
173 struct interface *iface, *iface2;
174 struct gate *gate, *fgate = NULL;
175 struct rtmptab *rtmp, *frtmp;
177 char *data, *end, packet[ ATP_BUFSIZ ];
183 memset(&sat, 0, sizeof( struct sockaddr_at ));
184 for ( iface = interfaces; iface; iface = iface->i_next ) {
185 if ( iface->i_flags & IFACE_LOOPBACK ) {
188 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
189 if ( ap->ap_packet == zip_packet ) {
192 if ( ap->ap_packet == rtmp_packet ) {
197 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
199 if ( iface->i_time < 3 ) {
200 if ( iface->i_flags & IFACE_PHASE1 ) {
201 if (rtmp_request( iface ) < 0) {
202 LOG(log_error, logtype_atalkd, "rtmp_request: %s", strerror(errno));
207 if (zip_getnetinfo( iface ) < 0) {
208 LOG(log_error, logtype_atalkd, "zip_getnetinfo: %s", strerror(errno));
215 iface->i_flags |= IFACE_NOROUTER;
216 if ((iface->i_flags & IFACE_ISROUTER)) {
217 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
219 * No seed info, and we've got multiple interfaces.
222 LOG(log_info, logtype_atalkd,
223 "as_timer multiple interfaces, no seed" );
224 LOG(log_info, logtype_atalkd, "as_timer can't configure %s",
226 LOG(log_info, logtype_atalkd, "as_timer waiting for router" );
231 * Complete configuration for iface, and boot next
234 iface->i_flags |= IFACE_CONFIG;
235 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
236 if (addzone( iface->i_rt, zt->zt_len,
238 LOG(log_error, logtype_atalkd, "addzone: %s", strerror(errno));
242 if ( iface->i_rt->rt_zt ) {
243 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
244 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
246 if ( iface->i_flags & IFACE_PHASE1 ) {
247 LOG(log_info, logtype_atalkd,
248 "as_timer configured %s phase 1 from seed",
250 setaddr( iface, IFACE_PHASE1,
251 iface->i_caddr.sat_addr.s_net,
252 iface->i_addr.sat_addr.s_node,
253 iface->i_caddr.sat_addr.s_net,
254 iface->i_caddr.sat_addr.s_net );
256 LOG(log_info, logtype_atalkd,
257 "as_timer configured %s phase 2 from seed",
261 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
262 LOG(log_error, logtype_atalkd,
263 "as_timer: can't route %u.%u to loop: %s",
264 ntohs( iface->i_addr.sat_addr.s_net ),
265 iface->i_addr.sat_addr.s_node,
269 if ( iface == ciface ) {
270 ciface = ciface->i_next;
276 * Configure for no router operation. Wait for a route
277 * to become available in rtmp_packet().
279 LOG(log_info, logtype_atalkd, "config for no router" );
281 if ( iface->i_flags & IFACE_PHASE2 ) {
282 iface->i_rt->rt_firstnet = 0;
283 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
284 setaddr( iface, IFACE_PHASE2,
285 iface->i_addr.sat_addr.s_net,
286 iface->i_addr.sat_addr.s_node,
287 0, htons( STARTUP_LASTNET ));
289 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
290 LOG(log_error, logtype_atalkd,
291 "as_timer: can't route %u.%u to loopback: %s",
292 ntohs( iface->i_addr.sat_addr.s_net ),
293 iface->i_addr.sat_addr.s_node,
298 if ( iface == ciface ) {
299 ciface = ciface->i_next;
306 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
308 free( (caddr_t)fgate );
313 data = packet + 1 + sizeof( struct ziphdr );
314 end = packet + sizeof( packet );
317 sat.sat_port = zap->ap_port;
320 * Perform timeouts on routers. If we've only got one
321 * interface, we'll use these timeouts to decide that
322 * our zone has gone away.
324 if ( ++gate->g_state >= RTMPTAB_BAD ) {
325 LOG(log_info, logtype_atalkd, "as_timer gateway %u.%u down",
326 ntohs( gate->g_sat.sat_addr.s_net ),
327 gate->g_sat.sat_addr.s_node );
330 frtmp = rtmp->rt_next;
331 if ( rtmp->rt_hops == RTMPHOPS_POISON ||
332 rtmp->rt_iprev == 0 ) {
335 rtmp->rt_hops = RTMPHOPS_POISON;
336 if ((cc = rtmp_replace( rtmp )) < 0) {
337 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
341 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
346 if ( gate->g_rt == 0 ) {
347 if ( gate->g_prev == 0 ) {
348 gate->g_iface->i_gate = gate->g_next;
350 gate->g_prev->g_next = gate->g_next;
352 if ( gate->g_next != 0 ) {
353 gate->g_next->g_prev = gate->g_prev;
355 fgate = gate; /* can't free here, just mark it */
358 * If this is the last router on the only interface,
359 * reconfigure our netrange. By marking the interface
360 * as having no router, we will notice when a router
363 * XXX: actually, we always reconfigure an interface
364 * if we're not a seed router.
367 if ( gate->g_iface->i_gate == 0 &&
368 ((iface->i_flags & IFACE_SEED) == 0)) {
369 gate->g_iface->i_flags |= IFACE_NOROUTER;
370 gate->g_iface->i_flags &= ~IFACE_CONFIG;
372 /* get rid of any zones associated with this iface */
373 if (gate->g_iface->i_rt->rt_zt) {
374 rtmp_delzonemap(gate->g_iface->i_rt);
375 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
378 LOG(log_info, logtype_atalkd, "as_timer last gateway down" );
380 /* Set netrange to 0-fffe. */
381 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
382 gate->g_iface->i_rt->rt_firstnet = 0;
383 gate->g_iface->i_rt->rt_lastnet =
384 htons( STARTUP_LASTNET );
385 setaddr( iface, IFACE_PHASE2,
386 iface->i_addr.sat_addr.s_net,
387 iface->i_addr.sat_addr.s_node,
388 0, htons( STARTUP_LASTNET ));
395 * If we don't have a zone for our interface yet, ask for
396 * it from any router (all routers) on the interface.
398 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
399 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
400 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
401 data += sizeof( u_short );
408 * Delete old routing tuples.
410 if ( rtmp->rt_state != RTMPTAB_PERM ) {
415 * We've not been updated for this route in a while. If
416 * it's not in use, go ahead and remove it. If it is in
417 * use, mark the route as down (POISON), and look for a
418 * better route. If one is found, delete this route and use
419 * the new one. If it's not found, mark the route as GOOD
420 * (so we'll propogate our poison) and delete it the next
421 * time it becomes BAD.
423 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
424 frtmp = rtmp->rt_next;
425 if ( rtmp->rt_iprev == 0 ) { /* not in use */
427 } else { /* in use */
428 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
431 rtmp->rt_hops = RTMPHOPS_POISON;
432 if ((cc = rtmp_replace( rtmp )) < 0) {
433 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
437 rtmp->rt_state = RTMPTAB_GOOD;
447 if ( rtmp->rt_iprev &&
448 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
449 if ( data + sizeof( u_short ) > end || n == 255 ) {
450 /* send what we've got */
451 zh.zh_op = ZIPOP_QUERY;
455 *data++ = DDPTYPE_ZIP;
456 memcpy( data, &zh, sizeof( struct ziphdr ));
458 if ( sendto( zap->ap_fd, packet, cc, 0,
459 (struct sockaddr *)&sat,
460 sizeof( struct sockaddr_at )) < 0 ) {
461 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
466 data = packet + 1 + sizeof( struct ziphdr );
467 end = packet + sizeof( packet );
471 * rt_nzq is number of ZIP Queries we've issued for a
472 * given netrange. If we've got ziptimeout on, we
473 * will only ask 3 times for any given netrange.
474 * Interestingly enough, since rt_nzq is a u_char,
475 * it will overflow after a while. This means we will
476 * periodically ask for nets that we've decided not to
477 * ask about, and warn that we can't get it's zone.
479 if ( rtmp->rt_nzq++ == 3 ) {
480 LOG(log_info, logtype_atalkd, "as_timer can't get zone for %u",
481 ntohs( rtmp->rt_firstnet ));
483 if ( rtmp->rt_nzq > 3 ) {
485 rtmp = rtmp->rt_next;
491 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
492 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
493 data += sizeof( u_short );
496 rtmp = rtmp->rt_next;
499 /* send what we've got */
501 zh.zh_op = ZIPOP_QUERY;
505 *data++ = DDPTYPE_ZIP;
506 memcpy( data, &zh, sizeof( struct ziphdr ));
508 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
509 sizeof( struct sockaddr_at )) < 0 ) {
510 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
515 free( (caddr_t)fgate );
520 * Send RTMP broadcasts if we have multiple interfaces or our
521 * interface is configured as a router.
523 if ((iface->i_flags & IFACE_ISROUTER)) {
525 sat.sat_len = sizeof( struct sockaddr_at );
527 sat.sat_family = AF_APPLETALK;
528 sat.sat_addr.s_net = ATADDR_ANYNET;
529 sat.sat_addr.s_node = ATADDR_BCAST;
530 sat.sat_port = rap->ap_port;
533 end = data + sizeof( packet );
534 *data++ = DDPTYPE_RTMPRD;
535 rh.rh_net = iface->i_addr.sat_addr.s_net;
537 rh.rh_node = iface->i_addr.sat_addr.s_node;
538 memcpy( data, &rh, sizeof( struct rtmp_head ));
539 data += sizeof( struct rtmp_head );
543 if ( iface->i_flags & IFACE_PHASE1 ) {
546 memcpy( data, &rt, SZ_RTMPTUPLE );
547 data += SZ_RTMPTUPLE;
549 rt.rt_net = iface->i_rt->rt_firstnet;
551 memcpy( data, &rt, SZ_RTMPTUPLE );
552 data += SZ_RTMPTUPLE;
554 rt.rt_net = iface->i_rt->rt_lastnet;
556 memcpy( data, &rt, SZ_RTMPTUPLE );
557 data += SZ_RTMPTUPLE;
560 for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
562 /* XXX: there used to be a bit checking against iface ==
563 iface2. also, we don't want to send an rtmp broadcast
564 to an interface that doesn't want it. */
565 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
566 ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
570 * Fill in tuples. Always send the same thing, regardless
571 * of the phase of the destination. Routers who don't
572 * understand extended rtmp packets will toss extended
573 * tuples because their distance will have the high bit set.
575 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
576 /* don't broadcast routes we have no zone for */
577 if ( rtmp->rt_zt == 0 ||
578 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
579 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
583 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
584 data + 2 * SZ_RTMPTUPLE > end ) ||
585 data + SZ_RTMPTUPLE > end ) {
586 if ( sendto( rap->ap_fd, packet, data - packet, 0,
587 (struct sockaddr *)&sat,
588 sizeof( struct sockaddr_at )) < 0 ) {
589 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
590 ntohs( sat.sat_addr.s_net ),
592 ntohs( iface->i_rt->rt_firstnet ),
596 if ( iface->i_flags & IFACE_PHASE2 ) {
597 data = packet + 1 + sizeof( struct rtmp_head ) +
600 data = packet + 1 + sizeof( struct rtmp_head ) +
606 rt.rt_net = rtmp->rt_firstnet;
607 rt.rt_dist = rtmp->rt_hops;
608 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
611 memcpy( data, &rt, SZ_RTMPTUPLE );
612 data += SZ_RTMPTUPLE;
614 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
615 rt.rt_net = rtmp->rt_lastnet;
617 memcpy( data, &rt, SZ_RTMPTUPLE );
618 data += SZ_RTMPTUPLE;
626 if ( sendto( rap->ap_fd, packet, data - packet, 0,
627 (struct sockaddr *)&sat,
628 sizeof( struct sockaddr_at )) < 0 ) {
629 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
630 ntohs( sat.sat_addr.s_net ),
632 ntohs( iface->i_rt->rt_firstnet ),
640 * Check if we're stable. Each time we configure an interface, we
641 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
642 * STABLEANYWAY, we give up and decide to "be" stable anyway.
643 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
644 * data and all zip data complete.
647 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
648 /* write out config file */
650 writeconf( configfile );
652 if ( stabletimer-- <= STABLEANYWAY ) {
658 if ( stable && !noparent ) {
660 LOG(log_info, logtype_atalkd, "ready %d/%d/%d", stabletimer, newrtmpdata,
664 * Seems like we could get here more than once...
666 if ( kill( getpid(), SIGSTOP ) < 0 ) {
667 LOG(log_error, logtype_atalkd, "as_timer: kill-self failed!" );
681 * Consistency check...
685 struct rtmptab *rtmp;
686 struct list *lr, *lz;
689 for ( zt = ziptab; zt; zt = zt->zt_next ) {
690 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
691 rtmp = (struct rtmptab *)lr->l_data;
692 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
693 LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)",
694 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
695 ntohs( rtmp->rt_lastnet ));
698 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
699 if ( zt == (struct ziptab *)lz->l_data ) {
704 LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s",
705 ntohs( rtmp->rt_firstnet ),
706 ntohs( rtmp->rt_lastnet ),
707 zt->zt_len, zt->zt_name );
716 as_debug(int sig _U_)
718 struct interface *iface;
725 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
726 LOG(log_error, logtype_atalkd, "rtmp: %s", strerror(errno) );
729 for ( iface = interfaces; iface; iface = iface->i_next ) {
730 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
731 ntohs( iface->i_addr.sat_addr.s_net ),
732 iface->i_addr.sat_addr.s_node );
733 if ( iface->i_flags & IFACE_PHASE1 ) {
734 putc( '1', rtmpdebug );
736 if ( iface->i_flags & IFACE_PHASE2 ) {
737 putc( '2', rtmpdebug );
739 if ( iface->i_flags & IFACE_RSEED ) {
740 putc( 'R', rtmpdebug );
742 if ( iface->i_flags & IFACE_SEED ) {
743 putc( 'S', rtmpdebug );
745 if ( iface->i_flags & IFACE_DONTROUTE ) {
746 putc( 'D', rtmpdebug );
748 if ( iface->i_flags & IFACE_ADDR ) {
749 putc( 'A', rtmpdebug );
751 if ( iface->i_flags & IFACE_CONFIG ) {
752 putc( 'C', rtmpdebug );
754 if ( iface->i_flags & IFACE_NOROUTER ) {
755 putc( 'N', rtmpdebug );
757 if ( iface->i_flags & IFACE_LOOP ) {
758 putc( 'L', rtmpdebug );
760 putc( '\n', rtmpdebug );
763 fprintf( rtmpdebug, "\t%u-%u ",
764 ntohs( iface->i_rt->rt_firstnet ),
765 ntohs( iface->i_rt->rt_lastnet ));
766 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
767 putc( 'q', rtmpdebug );
769 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
770 putc( 'z', rtmpdebug );
772 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
773 putc( 'x', rtmpdebug );
775 putc( 'i', rtmpdebug );
776 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
777 zt = (struct ziptab *)l->l_data;
778 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
780 fprintf( rtmpdebug, "\n" );
783 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
784 fprintf( rtmpdebug, "gate %u.%u %X\n",
785 ntohs( gate->g_sat.sat_addr.s_net ),
786 gate->g_sat.sat_addr.s_node, gate->g_state );
787 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
788 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
789 ntohs( rt->rt_lastnet ));
790 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
791 putc( 'q', rtmpdebug );
793 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
794 putc( 'z', rtmpdebug );
796 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
797 putc( 'x', rtmpdebug );
799 if ( rt->rt_iprev ) {
800 putc( 'i', rtmpdebug );
802 for ( l = rt->rt_zt; l; l = l->l_next ) {
803 zt = (struct ziptab *)l->l_data;
804 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
806 fprintf( rtmpdebug, "\n" );
815 * Called when SIGTERM is recieved. Remove all routes and then exit.
820 struct interface *iface;
824 for ( iface = interfaces; iface; iface = iface->i_next ) {
825 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
826 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
827 if ( rt->rt_iprev ) {
828 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
829 LOG(log_error, logtype_atalkd, "as_down remove %u-%u failed: %s",
830 ntohs( rt->rt_firstnet ),
831 ntohs( rt->rt_lastnet ),
837 if ( iface->i_flags & IFACE_LOOP ) {
838 if (looproute( iface, RTMP_DEL )) {
839 LOG(log_error, logtype_atalkd, "as_down remove %s %u.%u failed: %s",
840 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
841 iface->i_addr.sat_addr.s_node,
847 LOG(log_info, logtype_atalkd, "done" );
851 int main( int ac, char **av)
856 struct sockaddr_at sat;
859 sigset_t signal_set, old_set;
861 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 );
1023 * Set process name for logging
1026 set_processname("atalkd");
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) );
1160 sigemptyset( &signal_set );
1161 sigaddset(&signal_set, SIGALRM);
1163 /* don't block SIGTERM */
1164 sigaddset(&signal_set, SIGTERM);
1166 sigaddset(&signal_set, SIGUSR1);
1170 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1171 if ( errno == EINTR ) {
1175 LOG(log_error, logtype_atalkd, "select: %s", strerror(errno) );
1180 for ( iface = interfaces; iface; iface = iface->i_next ) {
1181 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1182 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1183 if ( ap->ap_packet ) {
1184 fromlen = sizeof( struct sockaddr_at );
1185 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1186 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1187 LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
1192 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1193 ntohs( sat.sat_addr.s_net ),
1194 sat.sat_addr.s_node, iface->i_name,
1195 iface->i_flags, ap->ap_port, ap->ap_fd );
1196 bprint( Packet, c );
1199 if (sigprocmask(SIG_BLOCK, &signal_set, &old_set) < 0) {
1200 LOG(log_error, logtype_atalkd, "sigprocmask: %s", strerror(errno) );
1204 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1205 LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
1212 if (sigprocmask(SIG_SETMASK, &old_set, NULL) < 0) {
1213 LOG(log_error, logtype_atalkd, "sigprocmask old set: %s", strerror(errno) );
1225 * This code is called (from main(), as_timer(), zip_packet(),
1226 * and rtmp_packet()) to set the initial "bootstrapping" address
1229 void bootaddr(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 /* useless message, no? */
1347 LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
1348 #endif /* __svr4__ */
1349 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1350 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1351 LOG(log_error, logtype_atalkd, "socket: %s", strerror(errno) );
1355 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1356 #endif /* ! __svr4 */
1358 memset( &sat, 0, sizeof( struct sockaddr_at ));
1360 sat.sat_len = sizeof( struct sockaddr_at );
1362 sat.sat_family = AF_APPLETALK;
1363 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1364 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1365 sat.sat_port = ap->ap_port;
1367 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1368 sizeof( struct sockaddr_at )) < 0 ) {
1369 LOG(log_error, logtype_atalkd, "bind %u.%u:%u: %s",
1370 ntohs( sat.sat_addr.s_net ),
1371 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1373 /* remove all interfaces if we have a problem with bind */
1374 for (iface = interfaces; iface; iface = iface->i_next) {
1375 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1376 #ifdef SIOCATALKDIFADDR
1377 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1378 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1379 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1380 #endif /* SIOCATALKDIFADDR */
1383 #endif /* SIOCDIFADDR */
1388 /* recalculate nfds and fds */
1390 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1391 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1392 FD_SET( ap->ap_fd, &fds );
1393 if ( ap->ap_fd > nfds ) {
1401 int ifsetallmulti (const char *iname, int set)
1406 memset(&ifr, 0, sizeof(ifr));
1408 if (( sock = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1412 /* get interface config */
1413 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1414 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
1419 /* should we set or unset IFF_ALLMULTI */
1421 ifr.ifr_flags |= IFF_ALLMULTI;
1423 ifr.ifr_flags &= ~IFF_ALLMULTI;
1425 /* set interface config */
1426 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1427 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
1436 int ifconfig( const char *iname, unsigned long cmd, struct sockaddr_at *sa)
1441 memset(&ifr, 0, sizeof(ifr));
1442 strcpy( ifr.ifr_name, iname );
1443 ifr.ifr_addr = *(struct sockaddr *)sa;
1445 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1448 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1453 if ( cmd == SIOCGIFADDR ) {
1454 *(struct sockaddr *)sa = ifr.ifr_addr;
1459 void dumpconfig( struct interface *iface)
1463 printf( "%s", iface->i_name );
1464 if ( iface->i_flags & IFACE_RSEED ) {
1465 printf( " -router" );
1466 } else if ( iface->i_flags & IFACE_SEED ) {
1470 if ( iface->i_flags & IFACE_DONTROUTE)
1471 printf( " -dontroute");
1473 printf( " -phase" );
1474 if ( iface->i_flags & IFACE_PHASE1 ) {
1479 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1480 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1481 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1483 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1484 iface->i_addr.sat_addr.s_node );
1485 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1486 iface->i_caddr.sat_addr.s_node );
1487 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1488 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1489 ((struct ziptab *)l->l_data)->zt_name );
1495 void dumproutes(void)
1497 struct interface *iface;
1498 struct rtmptab *rtmp;
1502 for ( iface = interfaces; iface; iface = iface->i_next ) {
1503 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1504 if ( rtmp->rt_gate == 0 ) {
1505 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1506 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1507 ntohs( rtmp->rt_lastnet ));
1509 printf( "%u", ntohs( rtmp->rt_firstnet ));
1512 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1513 printf( "%u.%u for %u-%u",
1514 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1515 rtmp->rt_gate->g_sat.sat_addr.s_node,
1516 ntohs( rtmp->rt_firstnet ),
1517 ntohs( rtmp->rt_lastnet ));
1519 printf( "%u.%u for %u",
1520 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1521 rtmp->rt_gate->g_sat.sat_addr.s_node,
1522 ntohs( rtmp->rt_firstnet ));
1526 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1530 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1531 zt = (struct ziptab *)l->l_data;
1532 printf( " %.*s", zt->zt_len, zt->zt_name );
1543 void dumpzones(void)
1545 struct interface *iface;
1546 struct rtmptab *rtmp;
1550 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1551 printf( "%.*s", zt->zt_len, zt->zt_name );
1552 for ( l = zt->zt_rt; l; l = l->l_next ) {
1553 rtmp = (struct rtmptab *)l->l_data;
1554 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1555 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1556 ntohs( rtmp->rt_lastnet ));
1558 printf( " %u", ntohs( rtmp->rt_firstnet ));
1560 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {