2 * $Id: main.c,v 1.23 2009-12-13 00:33:58 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 int stabletimer, newrtmpdata = 0;
108 static struct atserv atserv[] = {
109 { "rtmp", 1, rtmp_packet }, /* 0 */
110 { "nbp", 2, nbp_packet }, /* 1 */
111 { "echo", 4, aep_packet }, /* 2 */
112 { "zip", 6, zip_packet }, /* 3 */
114 static int atservNATSERV = elements( atserv );
116 struct interface *interfaces = NULL, *ciface = NULL;
118 static int debug = 0, quiet = 0, chatty = 0;
119 static char *configfile = NULL;
120 static int ziptimeout = 0;
121 static int stable = 0, noparent = 0;
122 static int ninterfaces;
123 static int defphase = IFACE_PHASE2;
126 static char Packet[ PKTSZ ];
127 static char *version = VERSION;
128 static char *pidfile = _PATH_ATALKDLOCK;
133 int readconf( char * );
134 int getifconf( void );
135 int writeconf( char * );
137 /* this is the messiest of the bunch as atalkd can exit pretty much
138 * everywhere. we delete interfaces here instead of in as_down. */
139 static void atalkd_exit(const int i)
142 struct interface *iface;
144 for (iface = interfaces; iface; iface = iface->i_next) {
145 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
146 #ifdef SIOCATALKDIFADDR
147 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
148 if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr))
150 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
151 #endif /* SIOCATALKIFADDR */
152 LOG(log_error, logtype_atalkd, "difaddr(%u.%u): %s",
153 ntohs(iface->i_addr.sat_addr.s_net),
154 iface->i_addr.sat_addr.s_node, strerror(errno));
157 if (!(iface->i_flags & IFACE_WASALLMULTI) && (iface->i_flags & IFACE_ALLMULTI))
158 ifsetallmulti(iface->i_name, 0);
161 #endif /* SOPCDOFADDR */
163 server_unlock(pidfile);
168 static void as_timer(int sig _U_)
170 struct sockaddr_at sat;
173 struct rtmp_tuple rt;
174 struct atport *ap, *zap, *rap;
175 struct interface *iface, *iface2;
176 struct gate *gate, *fgate = NULL;
177 struct rtmptab *rtmp, *frtmp;
179 char *data, *end, packet[ ATP_BUFSIZ ];
185 memset(&sat, 0, sizeof( struct sockaddr_at ));
186 for ( iface = interfaces; iface; iface = iface->i_next ) {
187 if ( iface->i_flags & IFACE_LOOPBACK ) {
190 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
191 if ( ap->ap_packet == zip_packet ) {
194 if ( ap->ap_packet == rtmp_packet ) {
199 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
201 if ( iface->i_time < 3 ) {
202 if ( iface->i_flags & IFACE_PHASE1 ) {
203 if (rtmp_request( iface ) < 0) {
204 LOG(log_error, logtype_atalkd, "rtmp_request: %s", strerror(errno));
209 if (zip_getnetinfo( iface ) < 0) {
210 LOG(log_error, logtype_atalkd, "zip_getnetinfo: %s", strerror(errno));
217 iface->i_flags |= IFACE_NOROUTER;
218 if ((iface->i_flags & IFACE_ISROUTER)) {
219 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
221 * No seed info, and we've got multiple interfaces.
224 LOG(log_info, logtype_atalkd,
225 "as_timer multiple interfaces, no seed" );
226 LOG(log_info, logtype_atalkd, "as_timer can't configure %s",
228 LOG(log_info, logtype_atalkd, "as_timer waiting for router" );
233 * Complete configuration for iface, and boot next
236 iface->i_flags |= IFACE_CONFIG;
237 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
238 if (addzone( iface->i_rt, zt->zt_len,
240 LOG(log_error, logtype_atalkd, "addzone: %s", strerror(errno));
244 if ( iface->i_rt->rt_zt ) {
245 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
246 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
248 if ( iface->i_flags & IFACE_PHASE1 ) {
249 LOG(log_info, logtype_atalkd,
250 "as_timer configured %s phase 1 from seed",
252 setaddr( iface, IFACE_PHASE1,
253 iface->i_caddr.sat_addr.s_net,
254 iface->i_addr.sat_addr.s_node,
255 iface->i_caddr.sat_addr.s_net,
256 iface->i_caddr.sat_addr.s_net );
258 LOG(log_info, logtype_atalkd,
259 "as_timer configured %s phase 2 from seed",
263 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
264 LOG(log_error, logtype_atalkd,
265 "as_timer: can't route %u.%u to loop: %s",
266 ntohs( iface->i_addr.sat_addr.s_net ),
267 iface->i_addr.sat_addr.s_node,
271 if ( iface == ciface ) {
272 ciface = ciface->i_next;
278 * Configure for no router operation. Wait for a route
279 * to become available in rtmp_packet().
281 LOG(log_info, logtype_atalkd, "config for no router" );
283 if ( iface->i_flags & IFACE_PHASE2 ) {
284 iface->i_rt->rt_firstnet = 0;
285 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
286 setaddr( iface, IFACE_PHASE2,
287 iface->i_addr.sat_addr.s_net,
288 iface->i_addr.sat_addr.s_node,
289 0, htons( STARTUP_LASTNET ));
291 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
292 LOG(log_error, logtype_atalkd,
293 "as_timer: can't route %u.%u to loopback: %s",
294 ntohs( iface->i_addr.sat_addr.s_net ),
295 iface->i_addr.sat_addr.s_node,
300 if ( iface == ciface ) {
301 ciface = ciface->i_next;
308 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
310 free( (caddr_t)fgate );
315 data = packet + 1 + sizeof( struct ziphdr );
316 end = packet + sizeof( packet );
319 sat.sat_port = zap->ap_port;
322 * Perform timeouts on routers. If we've only got one
323 * interface, we'll use these timeouts to decide that
324 * our zone has gone away.
326 if ( ++gate->g_state >= RTMPTAB_BAD ) {
327 LOG(log_info, logtype_atalkd, "as_timer gateway %u.%u down",
328 ntohs( gate->g_sat.sat_addr.s_net ),
329 gate->g_sat.sat_addr.s_node );
332 frtmp = rtmp->rt_next;
333 if ( rtmp->rt_hops == RTMPHOPS_POISON ||
334 rtmp->rt_iprev == NULL ) {
337 rtmp->rt_hops = RTMPHOPS_POISON;
338 if ((cc = rtmp_replace( rtmp )) < 0) {
339 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
343 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
348 if ( gate->g_rt == NULL ) {
349 if ( gate->g_prev == NULL ) {
350 gate->g_iface->i_gate = gate->g_next;
352 gate->g_prev->g_next = gate->g_next;
354 if ( gate->g_next != NULL ) {
355 gate->g_next->g_prev = gate->g_prev;
357 fgate = gate; /* can't free here, just mark it */
360 * If this is the last router on the only interface,
361 * reconfigure our netrange. By marking the interface
362 * as having no router, we will notice when a router
365 * XXX: actually, we always reconfigure an interface
366 * if we're not a seed router.
369 if ( gate->g_iface->i_gate == NULL &&
370 ((iface->i_flags & IFACE_SEED) == 0)) {
371 gate->g_iface->i_flags |= IFACE_NOROUTER;
372 gate->g_iface->i_flags &= ~IFACE_CONFIG;
374 /* get rid of any zones associated with this iface */
375 if (gate->g_iface->i_rt->rt_zt) {
376 rtmp_delzonemap(gate->g_iface->i_rt);
377 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
380 LOG(log_info, logtype_atalkd, "as_timer last gateway down" );
382 /* Set netrange to 0-fffe. */
383 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
384 gate->g_iface->i_rt->rt_firstnet = 0;
385 gate->g_iface->i_rt->rt_lastnet =
386 htons( STARTUP_LASTNET );
387 setaddr( iface, IFACE_PHASE2,
388 iface->i_addr.sat_addr.s_net,
389 iface->i_addr.sat_addr.s_node,
390 0, htons( STARTUP_LASTNET ));
397 * If we don't have a zone for our interface yet, ask for
398 * it from any router (all routers) on the interface.
400 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
401 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
402 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
403 data += sizeof( u_short );
410 * Delete old routing tuples.
412 if ( rtmp->rt_state != RTMPTAB_PERM ) {
417 * We've not been updated for this route in a while. If
418 * it's not in use, go ahead and remove it. If it is in
419 * use, mark the route as down (POISON), and look for a
420 * better route. If one is found, delete this route and use
421 * the new one. If it's not found, mark the route as GOOD
422 * (so we'll propogate our poison) and delete it the next
423 * time it becomes BAD.
425 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
426 frtmp = rtmp->rt_next;
427 if ( rtmp->rt_iprev == NULL ) { /* not in use */
429 } else { /* in use */
430 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
433 rtmp->rt_hops = RTMPHOPS_POISON;
434 if ((cc = rtmp_replace( rtmp )) < 0) {
435 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
439 rtmp->rt_state = RTMPTAB_GOOD;
449 if ( rtmp->rt_iprev &&
450 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
451 if ( data + sizeof( u_short ) > end || n == 255 ) {
452 /* send what we've got */
453 zh.zh_op = ZIPOP_QUERY;
457 *data++ = DDPTYPE_ZIP;
458 memcpy( data, &zh, sizeof( struct ziphdr ));
460 if ( sendto( zap->ap_fd, packet, cc, 0,
461 (struct sockaddr *)&sat,
462 sizeof( struct sockaddr_at )) < 0 ) {
463 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
468 data = packet + 1 + sizeof( struct ziphdr );
469 end = packet + sizeof( packet );
473 * rt_nzq is number of ZIP Queries we've issued for a
474 * given netrange. If we've got ziptimeout on, we
475 * will only ask 3 times for any given netrange.
476 * Interestingly enough, since rt_nzq is a u_char,
477 * it will overflow after a while. This means we will
478 * periodically ask for nets that we've decided not to
479 * ask about, and warn that we can't get it's zone.
481 if ( rtmp->rt_nzq++ == 3 ) {
482 LOG(log_info, logtype_atalkd, "as_timer can't get zone for %u",
483 ntohs( rtmp->rt_firstnet ));
485 if ( rtmp->rt_nzq > 3 ) {
487 rtmp = rtmp->rt_next;
493 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
494 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
495 data += sizeof( u_short );
498 rtmp = rtmp->rt_next;
501 /* send what we've got */
503 zh.zh_op = ZIPOP_QUERY;
507 *data++ = DDPTYPE_ZIP;
508 memcpy( data, &zh, sizeof( struct ziphdr ));
510 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
511 sizeof( struct sockaddr_at )) < 0 ) {
512 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
517 free( (caddr_t)fgate );
522 * Send RTMP broadcasts if we have multiple interfaces or our
523 * interface is configured as a router.
525 if ((iface->i_flags & IFACE_ISROUTER)) {
527 sat.sat_len = sizeof( struct sockaddr_at );
529 sat.sat_family = AF_APPLETALK;
530 sat.sat_addr.s_net = ATADDR_ANYNET;
531 sat.sat_addr.s_node = ATADDR_BCAST;
532 sat.sat_port = rap->ap_port;
535 end = data + sizeof( packet );
536 *data++ = DDPTYPE_RTMPRD;
537 rh.rh_net = iface->i_addr.sat_addr.s_net;
539 rh.rh_node = iface->i_addr.sat_addr.s_node;
540 memcpy( data, &rh, sizeof( struct rtmp_head ));
541 data += sizeof( struct rtmp_head );
545 if ( iface->i_flags & IFACE_PHASE1 ) {
548 memcpy( data, &rt, SZ_RTMPTUPLE );
549 data += SZ_RTMPTUPLE;
551 rt.rt_net = iface->i_rt->rt_firstnet;
553 memcpy( data, &rt, SZ_RTMPTUPLE );
554 data += SZ_RTMPTUPLE;
556 rt.rt_net = iface->i_rt->rt_lastnet;
558 memcpy( data, &rt, SZ_RTMPTUPLE );
559 data += SZ_RTMPTUPLE;
562 for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
564 /* XXX: there used to be a bit checking against iface ==
565 iface2. also, we don't want to send an rtmp broadcast
566 to an interface that doesn't want it. */
567 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
568 ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
572 * Fill in tuples. Always send the same thing, regardless
573 * of the phase of the destination. Routers who don't
574 * understand extended rtmp packets will toss extended
575 * tuples because their distance will have the high bit set.
577 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
578 /* don't broadcast routes we have no zone for */
579 if ( rtmp->rt_zt == NULL ||
580 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
581 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
586 if (rtmp->rt_iface == iface) {
590 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
591 data + 2 * SZ_RTMPTUPLE > end ) ||
592 data + SZ_RTMPTUPLE > end ) {
593 if ( sendto( rap->ap_fd, packet, data - packet, 0,
594 (struct sockaddr *)&sat,
595 sizeof( struct sockaddr_at )) < 0 ) {
596 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
597 ntohs( sat.sat_addr.s_net ),
599 ntohs( iface->i_rt->rt_firstnet ),
603 if ( iface->i_flags & IFACE_PHASE2 ) {
604 data = packet + 1 + sizeof( struct rtmp_head ) +
607 data = packet + 1 + sizeof( struct rtmp_head ) +
613 rt.rt_net = rtmp->rt_firstnet;
614 rt.rt_dist = rtmp->rt_hops;
615 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
618 memcpy( data, &rt, SZ_RTMPTUPLE );
619 data += SZ_RTMPTUPLE;
621 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
622 rt.rt_net = rtmp->rt_lastnet;
624 memcpy( data, &rt, SZ_RTMPTUPLE );
625 data += SZ_RTMPTUPLE;
633 if ( sendto( rap->ap_fd, packet, data - packet, 0,
634 (struct sockaddr *)&sat,
635 sizeof( struct sockaddr_at )) < 0 ) {
636 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
637 ntohs( sat.sat_addr.s_net ),
639 ntohs( iface->i_rt->rt_firstnet ),
647 * Check if we're stable. Each time we configure an interface, we
648 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
649 * STABLEANYWAY, we give up and decide to "be" stable anyway.
650 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
651 * data and all zip data complete.
654 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
655 /* write out config file */
657 writeconf( configfile );
659 if ( stabletimer-- <= STABLEANYWAY ) {
665 if ( stable && !noparent ) {
667 LOG(log_info, logtype_atalkd, "ready %d/%d/%d", stabletimer, newrtmpdata,
671 * Seems like we could get here more than once...
673 if ( kill( getpid(), SIGSTOP ) < 0 ) {
674 LOG(log_error, logtype_atalkd, "as_timer: kill-self failed!" );
688 * Consistency check...
692 struct rtmptab *rtmp;
693 struct list *lr, *lz;
696 for ( zt = ziptab; zt; zt = zt->zt_next ) {
697 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
698 rtmp = (struct rtmptab *)lr->l_data;
699 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
700 LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)",
701 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
702 ntohs( rtmp->rt_lastnet ));
705 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
706 if ( zt == (struct ziptab *)lz->l_data ) {
711 LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s",
712 ntohs( rtmp->rt_firstnet ),
713 ntohs( rtmp->rt_lastnet ),
714 zt->zt_len, zt->zt_name );
723 as_debug(int sig _U_)
725 struct interface *iface;
732 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
733 LOG(log_error, logtype_atalkd, "rtmp: %s", strerror(errno) );
736 for ( iface = interfaces; iface; iface = iface->i_next ) {
737 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
738 ntohs( iface->i_addr.sat_addr.s_net ),
739 iface->i_addr.sat_addr.s_node );
740 if ( iface->i_flags & IFACE_PHASE1 ) {
741 putc( '1', rtmpdebug );
743 if ( iface->i_flags & IFACE_PHASE2 ) {
744 putc( '2', rtmpdebug );
746 if ( iface->i_flags & IFACE_RSEED ) {
747 putc( 'R', rtmpdebug );
749 if ( iface->i_flags & IFACE_SEED ) {
750 putc( 'S', rtmpdebug );
752 if ( iface->i_flags & IFACE_DONTROUTE ) {
753 putc( 'D', rtmpdebug );
755 if ( iface->i_flags & IFACE_ADDR ) {
756 putc( 'A', rtmpdebug );
758 if ( iface->i_flags & IFACE_CONFIG ) {
759 putc( 'C', rtmpdebug );
761 if ( iface->i_flags & IFACE_NOROUTER ) {
762 putc( 'N', rtmpdebug );
764 if ( iface->i_flags & IFACE_LOOP ) {
765 putc( 'L', rtmpdebug );
767 putc( '\n', rtmpdebug );
770 fprintf( rtmpdebug, "\t%u-%u ",
771 ntohs( iface->i_rt->rt_firstnet ),
772 ntohs( iface->i_rt->rt_lastnet ));
773 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
774 putc( 'q', rtmpdebug );
776 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
777 putc( 'z', rtmpdebug );
779 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
780 putc( 'x', rtmpdebug );
782 putc( 'i', rtmpdebug );
783 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
784 zt = (struct ziptab *)l->l_data;
785 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
787 fprintf( rtmpdebug, "\n" );
790 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
791 fprintf( rtmpdebug, "gate %u.%u %X\n",
792 ntohs( gate->g_sat.sat_addr.s_net ),
793 gate->g_sat.sat_addr.s_node, gate->g_state );
794 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
795 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
796 ntohs( rt->rt_lastnet ));
797 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
798 putc( 'q', rtmpdebug );
800 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
801 putc( 'z', rtmpdebug );
803 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
804 putc( 'x', rtmpdebug );
806 if ( rt->rt_iprev ) {
807 putc( 'i', rtmpdebug );
809 for ( l = rt->rt_zt; l; l = l->l_next ) {
810 zt = (struct ziptab *)l->l_data;
811 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
813 fprintf( rtmpdebug, "\n" );
822 * Called when SIGTERM is recieved. Remove all routes and then exit.
827 struct interface *iface;
831 for ( iface = interfaces; iface; iface = iface->i_next ) {
832 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
833 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
834 if ( rt->rt_iprev ) {
835 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
836 LOG(log_error, logtype_atalkd, "as_down remove %u-%u failed: %s",
837 ntohs( rt->rt_firstnet ),
838 ntohs( rt->rt_lastnet ),
844 if ( iface->i_flags & IFACE_LOOP ) {
845 if (looproute( iface, RTMP_DEL )) {
846 LOG(log_error, logtype_atalkd, "as_down remove %s %u.%u failed: %s",
847 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
848 iface->i_addr.sat_addr.s_node,
854 LOG(log_info, logtype_atalkd, "done" );
858 int main( int ac, char **av)
863 struct sockaddr_at sat;
866 sigset_t signal_set, old_set;
868 struct interface *iface;
876 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
879 defphase = IFACE_PHASE1;
883 defphase = IFACE_PHASE2;
894 case 'q' : /* don't seed */
898 case 's' : /* seed */
902 case 't' : /* transition */
906 case 'P' : /* pid file */
910 case 'v' : /* version */
911 printf( "atalkd (version %s)\n", version );
916 fprintf( stderr, "Unknown option -- '%c'\n", c );
920 if ( optind != ac ) {
921 fprintf( stderr, "Too many arguments.\n" );
925 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
932 * Configure loop back address first, so appearances of "lo0" in
933 * the config file fail. Also insures that lo0 gets configured,
934 * even if there's some hangup during configuration of some
937 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
938 perror( "newiface" );
941 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
944 * Check our initial configuration before we fork. This way we can
945 * complain about syntax errors on stdout.
947 * Basically, if we're going to read our config file, we should read
948 * it and initialize our data structures. If we're not going to read
949 * our config file, use GIFCONF to initialize our data structures.
951 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
952 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
956 /* we need to count up our interfaces so that we can simplify things
957 * later. we also need to figure out if we have more than one interface
958 * that is routing. */
959 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
960 iface=iface->i_next) {
961 if (iface->i_flags & IFACE_DONTROUTE)
965 i = ninterfaces - i; /* number of routable interfaces */
968 * At this point, we have (at least partially) initialized data
969 * structures. Fill in what we can and verify that nothing is obviously
972 for (iface = interfaces; iface; iface = iface->i_next) {
973 /* Apply the default phase */
974 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
975 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
976 iface->i_flags |= defphase;
979 /* set up router flag information. if we have multiple interfaces
980 * and DONTROUTE isn't set, set up ROUTER. i is the number of
981 * interfaces that don't have the DONTROUTE flag set. */
982 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
983 iface->i_flags |= IFACE_ISROUTER;
986 /* Set default addresses */
987 if ( iface->i_rt == NULL ) {
988 if (( iface->i_rt = newrt(iface)) == NULL ) {
993 if ( iface->i_flags & IFACE_PHASE1 ) {
994 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
995 iface->i_caddr.sat_addr.s_net;
997 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
998 ( iface->i_flags & IFACE_LOOPBACK )) {
999 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
1000 iface->i_caddr.sat_addr.s_net;
1002 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
1003 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
1008 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
1009 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
1012 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1013 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1017 dumpconfig( iface ); /* probably needs args */
1022 * A little consistency check...
1024 if ( ninterfaces < IFBASE ) {
1025 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1030 * Set process name for logging
1033 set_processname("atalkd");
1035 /* do this here so that we can use ifconfig */
1037 if ( plumb() < 0 ) {
1038 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1041 #endif /* __svr4__ */
1043 /* delete pre-existing interface addresses. */
1045 for (iface = interfaces; iface; iface = iface->i_next) {
1046 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1047 #ifdef SIOCATALKDIFADDR
1048 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1049 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1050 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1051 #endif /* SIOCATALKDIFADDR */
1054 #endif /* SIOCDIFADDR */
1057 * Disassociate. The child will send itself a signal when it is
1058 * stable. This indicates that other processes may begin using
1061 switch (i = server_lock("atalkd", pidfile, debug)) {
1066 default: /* parent */
1068 * Wait for the child to send itself a SIGSTOP, after which
1069 * we send it a SIGCONT and exit ourself.
1071 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1072 perror( "wait3" ); /* Child died? */
1075 if ( !WIFSTOPPED( status )) {
1076 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1077 if ( WIFEXITED( status )) {
1078 fprintf( stderr, " Child exited with %d.\n",
1079 WEXITSTATUS( status ));
1081 fprintf( stderr, " Child died.\n" );
1085 if ( kill(i, SIGCONT ) < 0 ) {
1093 openlog( prog, LOG_PID );
1095 set_processname(prog);
1096 syslog_setup(log_debug, logtype_default, logoption_pid, logfacility_daemon );
1099 LOG(log_info, logtype_atalkd, "restart (%s)", version );
1102 * Socket for use in routing ioctl()s. Can't add routes to our
1103 * interfaces until we have our routing socket.
1106 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1107 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1110 if ( shutdown( rtfd, 0 ) < 0 ) {
1111 LOG(log_error, logtype_atalkd, "route shutdown: %s", strerror(errno) );
1115 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1116 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1121 ciface = interfaces;
1124 memset(&sv, 0, sizeof(sv));
1125 sv.sa_handler = as_down;
1126 sigemptyset( &sv.sa_mask );
1127 sigaddset( &sv.sa_mask, SIGUSR1 );
1128 sigaddset( &sv.sa_mask, SIGALRM );
1129 sigaddset( &sv.sa_mask, SIGTERM );
1130 sv.sa_flags = SA_RESTART;
1131 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1132 LOG(log_error, logtype_atalkd, "sigterm: %s", strerror(errno) );
1136 sv.sa_handler = as_debug;
1137 sigemptyset( &sv.sa_mask );
1138 sigaddset( &sv.sa_mask, SIGUSR1 );
1139 sigaddset( &sv.sa_mask, SIGALRM );
1140 sigaddset( &sv.sa_mask, SIGTERM );
1141 sv.sa_flags = SA_RESTART;
1142 if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1143 LOG(log_error, logtype_atalkd, "sigusr1: %s", strerror(errno) );
1147 sv.sa_handler = as_timer;
1148 sigemptyset( &sv.sa_mask );
1149 sigaddset( &sv.sa_mask, SIGUSR1 );
1150 sigaddset( &sv.sa_mask, SIGALRM );
1151 sigaddset( &sv.sa_mask, SIGTERM );
1152 sv.sa_flags = SA_RESTART;
1153 if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1154 LOG(log_error, logtype_atalkd, "sigalrm: %s", strerror(errno) );
1158 it.it_interval.tv_sec = 10L;
1159 it.it_interval.tv_usec = 0L;
1160 it.it_value.tv_sec = 10L;
1161 it.it_value.tv_usec = 0L;
1162 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1163 LOG(log_error, logtype_atalkd, "setitimer: %s", strerror(errno) );
1167 sigemptyset( &signal_set );
1168 sigaddset(&signal_set, SIGALRM);
1170 /* don't block SIGTERM */
1171 sigaddset(&signal_set, SIGTERM);
1173 sigaddset(&signal_set, SIGUSR1);
1177 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1178 if ( errno == EINTR ) {
1182 LOG(log_error, logtype_atalkd, "select: %s", strerror(errno) );
1187 for ( iface = interfaces; iface; iface = iface->i_next ) {
1188 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1189 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1190 if ( ap->ap_packet ) {
1191 fromlen = sizeof( struct sockaddr_at );
1192 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1193 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1194 LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
1199 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1200 ntohs( sat.sat_addr.s_net ),
1201 sat.sat_addr.s_node, iface->i_name,
1202 iface->i_flags, ap->ap_port, ap->ap_fd );
1203 bprint( Packet, c );
1206 if (sigprocmask(SIG_BLOCK, &signal_set, &old_set) < 0) {
1207 LOG(log_error, logtype_atalkd, "sigprocmask: %s", strerror(errno) );
1211 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1212 LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
1219 if (sigprocmask(SIG_SETMASK, &old_set, NULL) < 0) {
1220 LOG(log_error, logtype_atalkd, "sigprocmask old set: %s", strerror(errno) );
1232 * This code is called (from main(), as_timer(), zip_packet(),
1233 * and rtmp_packet()) to set the initial "bootstrapping" address
1236 void bootaddr(struct interface *iface)
1238 if ( iface == NULL ) {
1243 if ( iface->i_flags & IFACE_ADDR ) {
1244 LOG(log_error, logtype_atalkd, "bootaddr OOPS!" );
1248 if ( iface->i_flags & IFACE_PHASE1 ) {
1249 setaddr( iface, IFACE_PHASE1, 0,
1250 iface->i_caddr.sat_addr.s_node, 0, 0 );
1252 if ( iface->i_flags & IFACE_LOOPBACK ) {
1253 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1254 if ( ciface == iface ) {
1255 ciface = ciface->i_next;
1259 } else if (rtmp_request( iface ) < 0) {
1260 LOG(log_error, logtype_atalkd, "bootaddr (rtmp_request): %s", strerror(errno));
1265 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1266 iface->i_caddr.sat_addr.s_node,
1267 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1269 if ( iface->i_flags & IFACE_LOOPBACK ) {
1270 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1271 if ( ciface == iface ) {
1272 ciface = ciface->i_next;
1276 } else if (zip_getnetinfo( iface ) < 0) {
1277 LOG(log_error, logtype_atalkd, "bootaddr (zip_getnetinfo): %s", strerror(errno));
1282 iface->i_flags |= IFACE_ADDR;
1283 stabletimer = UNSTABLE;
1289 * to manage the i_ports field and the fds for select().
1291 void setaddr(struct interface *iface,
1292 u_int8_t phase, u_int16_t net, u_int8_t node,
1293 u_int16_t first, u_int16_t last)
1299 struct sockaddr_at sat;
1302 if ( iface->i_ports == NULL ) { /* allocate port structures */
1303 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1304 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1305 LOG(log_info, logtype_atalkd, "%s: service unknown", as->as_name );
1307 as->as_port = ntohs( se->s_port );
1309 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1311 LOG(log_error, logtype_atalkd, "malloc: %s", strerror(errno) );
1315 ap->ap_next = iface->i_ports;
1316 ap->ap_iface = iface;
1317 ap->ap_port = as->as_port;
1318 ap->ap_packet = as->as_packet;
1320 iface->i_ports = ap;
1322 } else { /* close ports */
1323 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1324 (void)close( ap->ap_fd );
1329 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1331 iface->i_addr.sat_family = AF_APPLETALK;
1332 iface->i_addr.sat_addr.s_net = net;
1333 iface->i_addr.sat_addr.s_node = node;
1335 nr.nr_phase = phase;
1336 nr.nr_firstnet = first;
1337 nr.nr_lastnet = last;
1338 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1340 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1341 LOG(log_error, logtype_atalkd, "setifaddr: %s (%u-%u): %s. try specifying a \
1342 smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
1345 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1346 LOG(log_error, logtype_atalkd, "getifaddr: %s: %s", iface->i_name, strerror(errno) );
1351 i = 1; /* enable broadcasts */
1353 /* useless message, no? */
1354 LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
1355 #endif /* __svr4__ */
1356 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1357 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1358 LOG(log_error, logtype_atalkd, "socket: %s", strerror(errno) );
1362 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1363 #endif /* ! __svr4 */
1365 memset( &sat, 0, sizeof( struct sockaddr_at ));
1367 sat.sat_len = sizeof( struct sockaddr_at );
1369 sat.sat_family = AF_APPLETALK;
1370 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1371 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1372 sat.sat_port = ap->ap_port;
1374 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1375 sizeof( struct sockaddr_at )) < 0 ) {
1376 LOG(log_error, logtype_atalkd, "bind %u.%u:%u: %s",
1377 ntohs( sat.sat_addr.s_net ),
1378 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1380 /* remove all interfaces if we have a problem with bind */
1381 for (iface = interfaces; iface; iface = iface->i_next) {
1382 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1383 #ifdef SIOCATALKDIFADDR
1384 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1385 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1386 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1387 #endif /* SIOCATALKDIFADDR */
1390 #endif /* SIOCDIFADDR */
1395 /* recalculate nfds and fds */
1397 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1398 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1399 FD_SET( ap->ap_fd, &fds );
1400 if ( ap->ap_fd > nfds ) {
1408 int ifsetallmulti (const char *iname, int set)
1413 memset(&ifr, 0, sizeof(ifr));
1415 if (( sock = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1419 /* get interface config */
1420 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1421 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
1426 /* should we set or unset IFF_ALLMULTI */
1428 ifr.ifr_flags |= IFF_ALLMULTI;
1430 ifr.ifr_flags &= ~IFF_ALLMULTI;
1432 /* set interface config */
1433 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1434 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
1443 int ifconfig( const char *iname, unsigned long cmd, struct sockaddr_at *sa)
1448 memset(&ifr, 0, sizeof(ifr));
1449 strcpy( ifr.ifr_name, iname );
1450 ifr.ifr_addr = *(struct sockaddr *)sa;
1452 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1455 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1460 if ( cmd == SIOCGIFADDR ) {
1461 *(struct sockaddr *)sa = ifr.ifr_addr;
1466 void dumpconfig( struct interface *iface)
1470 printf( "%s", iface->i_name );
1471 if ( iface->i_flags & IFACE_RSEED ) {
1472 printf( " -router" );
1473 } else if ( iface->i_flags & IFACE_SEED ) {
1477 if ( iface->i_flags & IFACE_DONTROUTE)
1478 printf( " -dontroute");
1480 printf( " -phase" );
1481 if ( iface->i_flags & IFACE_PHASE1 ) {
1486 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1487 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1488 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1490 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1491 iface->i_addr.sat_addr.s_node );
1492 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1493 iface->i_caddr.sat_addr.s_node );
1494 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1495 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1496 ((struct ziptab *)l->l_data)->zt_name );
1502 void dumproutes(void)
1504 struct interface *iface;
1505 struct rtmptab *rtmp;
1509 for ( iface = interfaces; iface; iface = iface->i_next ) {
1510 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1511 if ( rtmp->rt_gate == 0 ) {
1512 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1513 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1514 ntohs( rtmp->rt_lastnet ));
1516 printf( "%u", ntohs( rtmp->rt_firstnet ));
1519 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1520 printf( "%u.%u for %u-%u",
1521 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1522 rtmp->rt_gate->g_sat.sat_addr.s_node,
1523 ntohs( rtmp->rt_firstnet ),
1524 ntohs( rtmp->rt_lastnet ));
1526 printf( "%u.%u for %u",
1527 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1528 rtmp->rt_gate->g_sat.sat_addr.s_node,
1529 ntohs( rtmp->rt_firstnet ));
1533 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1537 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1538 zt = (struct ziptab *)l->l_data;
1539 printf( " %.*s", zt->zt_len, zt->zt_name );
1550 void dumpzones(void)
1552 struct interface *iface;
1553 struct rtmptab *rtmp;
1557 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1558 printf( "%.*s", zt->zt_len, zt->zt_name );
1559 for ( l = zt->zt_rt; l; l = l->l_next ) {
1560 rtmp = (struct rtmptab *)l->l_data;
1561 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1562 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1563 ntohs( rtmp->rt_lastnet ));
1565 printf( " %u", ntohs( rtmp->rt_firstnet ));
1567 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {