2 * $Id: main.c,v 1.25 2009-12-13 02:21:47 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);
167 /* XXX need better error handling for gone interfaces, delete routes and so on
168 * moreover there's no way to put an interface back short of restarting atalkd
169 * thus after the first time, silently fail
171 static ssize_t sendto_iface(struct interface *iface, int sockfd, const void *buf, size_t len,
172 const struct sockaddr_at *dest_addr)
174 ssize_t ret = sendto( sockfd, buf, len, 0, (struct sockaddr *)dest_addr, sizeof( struct sockaddr_at ));
177 if (!(iface->i_flags & IFACE_ERROR)) {
178 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
179 ntohs( dest_addr->sat_addr.s_net ),
180 dest_addr->sat_addr.s_node,
181 ntohs( iface->i_rt->rt_firstnet ),
184 iface->i_flags |= IFACE_ERROR;
187 iface->i_flags &= ~IFACE_ERROR;
192 static void as_timer(int sig _U_)
194 struct sockaddr_at sat;
197 struct rtmp_tuple rt;
198 struct atport *ap, *zap, *rap;
199 struct interface *iface, *iface2;
200 struct gate *gate, *fgate = NULL;
201 struct rtmptab *rtmp, *frtmp;
203 char *data, *end, packet[ ATP_BUFSIZ ];
209 memset(&sat, 0, sizeof( struct sockaddr_at ));
211 for ( iface = interfaces; iface; iface = iface->i_next ) {
212 if ( iface->i_flags & IFACE_LOOPBACK ) {
215 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
216 if ( ap->ap_packet == zip_packet ) {
219 if ( ap->ap_packet == rtmp_packet ) {
224 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) == IFACE_ADDR ) {
225 if ( iface->i_time < 3 ) {
226 if ( iface->i_flags & IFACE_PHASE1 ) {
227 if (rtmp_request( iface ) < 0) {
228 LOG(log_error, logtype_atalkd, "rtmp_request: %s", strerror(errno));
233 if (zip_getnetinfo( iface ) < 0) {
234 LOG(log_error, logtype_atalkd, "zip_getnetinfo: %s", strerror(errno));
241 iface->i_flags |= IFACE_NOROUTER;
242 if ((iface->i_flags & IFACE_ISROUTER)) {
243 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
245 * No seed info, and we've got multiple interfaces.
248 LOG(log_info, logtype_atalkd, "as_timer multiple interfaces, no seed" );
249 LOG(log_info, logtype_atalkd, "as_timer can't configure %s", iface->i_name );
250 LOG(log_info, logtype_atalkd, "as_timer waiting for router" );
255 * Complete configuration for iface, and boot next
258 iface->i_flags |= IFACE_CONFIG;
259 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
260 if (addzone( iface->i_rt, zt->zt_len, zt->zt_name) < 0) {
261 LOG(log_error, logtype_atalkd, "addzone: %s", strerror(errno));
265 if ( iface->i_rt->rt_zt ) {
266 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
267 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
269 if ( iface->i_flags & IFACE_PHASE1 ) {
270 LOG(log_info, logtype_atalkd, "as_timer configured %s phase 1 from seed", iface->i_name );
271 setaddr( iface, IFACE_PHASE1,
272 iface->i_caddr.sat_addr.s_net,
273 iface->i_addr.sat_addr.s_node,
274 iface->i_caddr.sat_addr.s_net,
275 iface->i_caddr.sat_addr.s_net );
277 LOG(log_info, logtype_atalkd, "as_timer configured %s phase 2 from seed", iface->i_name );
280 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
281 LOG(log_error, logtype_atalkd, "as_timer: can't route %u.%u to loop: %s",
282 ntohs( iface->i_addr.sat_addr.s_net ),
283 iface->i_addr.sat_addr.s_node, strerror(errno) );
286 if ( iface == ciface ) {
287 ciface = ciface->i_next;
293 * Configure for no router operation. Wait for a route
294 * to become available in rtmp_packet().
296 LOG(log_info, logtype_atalkd, "config for no router" );
298 if ( iface->i_flags & IFACE_PHASE2 ) {
299 iface->i_rt->rt_firstnet = 0;
300 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
301 setaddr( iface, IFACE_PHASE2, iface->i_addr.sat_addr.s_net, iface->i_addr.sat_addr.s_node,
302 0, htons( STARTUP_LASTNET ));
304 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
305 LOG(log_error, logtype_atalkd, "as_timer: can't route %u.%u to loopback: %s",
306 ntohs( iface->i_addr.sat_addr.s_net ), iface->i_addr.sat_addr.s_node,
311 if ( iface == ciface ) {
312 ciface = ciface->i_next;
319 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
321 free( (caddr_t)fgate );
326 data = packet + 1 + sizeof( struct ziphdr );
327 end = packet + sizeof( packet );
330 sat.sat_port = zap->ap_port;
333 * Perform timeouts on routers. If we've only got one
334 * interface, we'll use these timeouts to decide that
335 * our zone has gone away.
337 if ( ++gate->g_state >= RTMPTAB_BAD ) {
338 LOG(log_info, logtype_atalkd, "as_timer gateway %u.%u down", ntohs( gate->g_sat.sat_addr.s_net ),
339 gate->g_sat.sat_addr.s_node );
342 frtmp = rtmp->rt_next;
343 if ( rtmp->rt_hops == RTMPHOPS_POISON || rtmp->rt_iprev == NULL ) {
346 rtmp->rt_hops = RTMPHOPS_POISON;
347 if ((cc = rtmp_replace( rtmp )) < 0) {
348 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
352 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
357 if ( gate->g_rt == NULL ) {
358 if ( gate->g_prev == NULL ) {
359 gate->g_iface->i_gate = gate->g_next;
361 gate->g_prev->g_next = gate->g_next;
363 if ( gate->g_next != NULL ) {
364 gate->g_next->g_prev = gate->g_prev;
366 fgate = gate; /* can't free here, just mark it */
369 * If this is the last router on the only interface,
370 * reconfigure our netrange. By marking the interface
371 * as having no router, we will notice when a router
374 * XXX: actually, we always reconfigure an interface
375 * if we're not a seed router.
378 if ( gate->g_iface->i_gate == NULL && ((iface->i_flags & IFACE_SEED) == 0)) {
379 gate->g_iface->i_flags |= IFACE_NOROUTER;
380 gate->g_iface->i_flags &= ~IFACE_CONFIG;
382 /* get rid of any zones associated with this iface */
383 if (gate->g_iface->i_rt->rt_zt) {
384 rtmp_delzonemap(gate->g_iface->i_rt);
385 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
388 LOG(log_info, logtype_atalkd, "as_timer last gateway down" );
390 /* Set netrange to 0-fffe. */
391 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
392 gate->g_iface->i_rt->rt_firstnet = 0;
393 gate->g_iface->i_rt->rt_lastnet =
394 htons( STARTUP_LASTNET );
395 setaddr( iface, IFACE_PHASE2,
396 iface->i_addr.sat_addr.s_net,
397 iface->i_addr.sat_addr.s_node,
398 0, htons( STARTUP_LASTNET ));
405 * If we don't have a zone for our interface yet, ask for
406 * it from any router (all routers) on the interface.
408 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
409 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
410 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
411 data += sizeof( u_short );
418 * Delete old routing tuples.
420 if ( rtmp->rt_state != RTMPTAB_PERM ) {
425 * We've not been updated for this route in a while. If
426 * it's not in use, go ahead and remove it. If it is in
427 * use, mark the route as down (POISON), and look for a
428 * better route. If one is found, delete this route and use
429 * the new one. If it's not found, mark the route as GOOD
430 * (so we'll propogate our poison) and delete it the next
431 * time it becomes BAD.
433 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
434 frtmp = rtmp->rt_next;
435 if ( rtmp->rt_iprev == NULL ) { /* not in use */
437 } else { /* in use */
438 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
441 rtmp->rt_hops = RTMPHOPS_POISON;
442 if ((cc = rtmp_replace( rtmp )) < 0) {
443 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
447 rtmp->rt_state = RTMPTAB_GOOD;
457 if ( rtmp->rt_iprev && ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
458 if ( data + sizeof( u_short ) > end || n == 255 ) {
459 /* send what we've got */
460 zh.zh_op = ZIPOP_QUERY;
464 *data++ = DDPTYPE_ZIP;
465 memcpy( data, &zh, sizeof( struct ziphdr ));
467 sendto_iface(iface, zap->ap_fd, packet, cc, &sat);
471 data = packet + 1 + sizeof( struct ziphdr );
472 end = packet + sizeof( packet );
476 * rt_nzq is number of ZIP Queries we've issued for a
477 * given netrange. If we've got ziptimeout on, we
478 * will only ask 3 times for any given netrange.
479 * Interestingly enough, since rt_nzq is a u_char,
480 * it will overflow after a while. This means we will
481 * periodically ask for nets that we've decided not to
482 * ask about, and warn that we can't get it's zone.
484 if ( rtmp->rt_nzq++ == 3 ) {
485 LOG(log_info, logtype_atalkd, "as_timer can't get zone for %u", ntohs( rtmp->rt_firstnet ));
487 if ( rtmp->rt_nzq > 3 ) {
489 rtmp = rtmp->rt_next;
495 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
496 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
497 data += sizeof( u_short );
500 rtmp = rtmp->rt_next;
503 /* send what we've got */
505 zh.zh_op = ZIPOP_QUERY;
509 *data++ = DDPTYPE_ZIP;
510 memcpy( data, &zh, sizeof( struct ziphdr ));
512 sendto_iface( iface, zap->ap_fd, packet, cc, &sat);
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 == NULL ||
579 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
580 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
585 if (rtmp->rt_iface == iface) {
589 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
590 data + 2 * SZ_RTMPTUPLE > end ) ||
591 data + SZ_RTMPTUPLE > end ) {
593 sendto_iface(iface,rap->ap_fd, packet, data - packet, &sat);
595 if ( iface->i_flags & IFACE_PHASE2 ) {
596 data = packet + 1 + sizeof( struct rtmp_head ) + 2 * SZ_RTMPTUPLE;
598 data = packet + 1 + sizeof( struct rtmp_head ) + SZ_RTMPTUPLE;
603 rt.rt_net = rtmp->rt_firstnet;
604 rt.rt_dist = rtmp->rt_hops;
605 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
608 memcpy( data, &rt, SZ_RTMPTUPLE );
609 data += SZ_RTMPTUPLE;
611 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
612 rt.rt_net = rtmp->rt_lastnet;
614 memcpy( data, &rt, SZ_RTMPTUPLE );
615 data += SZ_RTMPTUPLE;
623 sendto_iface(iface, rap->ap_fd, packet, data - packet, &sat);
629 * Check if we're stable. Each time we configure an interface, we
630 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
631 * STABLEANYWAY, we give up and decide to "be" stable anyway.
632 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
633 * data and all zip data complete.
636 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
637 /* write out config file */
639 writeconf( configfile );
641 if ( stabletimer-- <= STABLEANYWAY ) {
647 if ( stable && !noparent ) {
649 LOG(log_info, logtype_atalkd, "ready %d/%d/%d", stabletimer, newrtmpdata,
653 * Seems like we could get here more than once...
655 if ( kill( getpid(), SIGSTOP ) < 0 ) {
656 LOG(log_error, logtype_atalkd, "as_timer: kill-self failed!" );
670 * Consistency check...
674 struct rtmptab *rtmp;
675 struct list *lr, *lz;
678 for ( zt = ziptab; zt; zt = zt->zt_next ) {
679 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
680 rtmp = (struct rtmptab *)lr->l_data;
681 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
682 LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)",
683 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
684 ntohs( rtmp->rt_lastnet ));
687 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
688 if ( zt == (struct ziptab *)lz->l_data ) {
693 LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s",
694 ntohs( rtmp->rt_firstnet ),
695 ntohs( rtmp->rt_lastnet ),
696 zt->zt_len, zt->zt_name );
705 as_debug(int sig _U_)
707 struct interface *iface;
714 if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
715 LOG(log_error, logtype_atalkd, "rtmp: %s", strerror(errno) );
718 for ( iface = interfaces; iface; iface = iface->i_next ) {
719 fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
720 ntohs( iface->i_addr.sat_addr.s_net ),
721 iface->i_addr.sat_addr.s_node );
722 if ( iface->i_flags & IFACE_PHASE1 ) {
723 putc( '1', rtmpdebug );
725 if ( iface->i_flags & IFACE_PHASE2 ) {
726 putc( '2', rtmpdebug );
728 if ( iface->i_flags & IFACE_RSEED ) {
729 putc( 'R', rtmpdebug );
731 if ( iface->i_flags & IFACE_SEED ) {
732 putc( 'S', rtmpdebug );
734 if ( iface->i_flags & IFACE_DONTROUTE ) {
735 putc( 'D', rtmpdebug );
737 if ( iface->i_flags & IFACE_ADDR ) {
738 putc( 'A', rtmpdebug );
740 if ( iface->i_flags & IFACE_CONFIG ) {
741 putc( 'C', rtmpdebug );
743 if ( iface->i_flags & IFACE_NOROUTER ) {
744 putc( 'N', rtmpdebug );
746 if ( iface->i_flags & IFACE_LOOP ) {
747 putc( 'L', rtmpdebug );
749 putc( '\n', rtmpdebug );
752 fprintf( rtmpdebug, "\t%u-%u ",
753 ntohs( iface->i_rt->rt_firstnet ),
754 ntohs( iface->i_rt->rt_lastnet ));
755 if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
756 putc( 'q', rtmpdebug );
758 if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
759 putc( 'z', rtmpdebug );
761 if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
762 putc( 'x', rtmpdebug );
764 putc( 'i', rtmpdebug );
765 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
766 zt = (struct ziptab *)l->l_data;
767 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
769 fprintf( rtmpdebug, "\n" );
772 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
773 fprintf( rtmpdebug, "gate %u.%u %X\n",
774 ntohs( gate->g_sat.sat_addr.s_net ),
775 gate->g_sat.sat_addr.s_node, gate->g_state );
776 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
777 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
778 ntohs( rt->rt_lastnet ));
779 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
780 putc( 'q', rtmpdebug );
782 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
783 putc( 'z', rtmpdebug );
785 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
786 putc( 'x', rtmpdebug );
788 if ( rt->rt_iprev ) {
789 putc( 'i', rtmpdebug );
791 for ( l = rt->rt_zt; l; l = l->l_next ) {
792 zt = (struct ziptab *)l->l_data;
793 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
795 fprintf( rtmpdebug, "\n" );
804 * Called when SIGTERM is recieved. Remove all routes and then exit.
809 struct interface *iface;
813 for ( iface = interfaces; iface; iface = iface->i_next ) {
814 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
815 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
816 if ( rt->rt_iprev ) {
817 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
818 LOG(log_error, logtype_atalkd, "as_down remove %u-%u failed: %s",
819 ntohs( rt->rt_firstnet ),
820 ntohs( rt->rt_lastnet ),
826 if ( iface->i_flags & IFACE_LOOP ) {
827 if (looproute( iface, RTMP_DEL )) {
828 LOG(log_error, logtype_atalkd, "as_down remove %s %u.%u failed: %s",
829 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
830 iface->i_addr.sat_addr.s_node,
836 LOG(log_info, logtype_atalkd, "done" );
840 int main( int ac, char **av)
845 struct sockaddr_at sat;
848 sigset_t signal_set, old_set;
850 struct interface *iface;
858 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
861 defphase = IFACE_PHASE1;
865 defphase = IFACE_PHASE2;
876 case 'q' : /* don't seed */
880 case 's' : /* seed */
884 case 't' : /* transition */
888 case 'P' : /* pid file */
892 case 'v' : /* version */
893 printf( "atalkd (version %s)\n", version );
898 fprintf( stderr, "Unknown option -- '%c'\n", c );
902 if ( optind != ac ) {
903 fprintf( stderr, "Too many arguments.\n" );
907 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
914 * Configure loop back address first, so appearances of "lo0" in
915 * the config file fail. Also insures that lo0 gets configured,
916 * even if there's some hangup during configuration of some
919 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
920 perror( "newiface" );
923 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
926 * Check our initial configuration before we fork. This way we can
927 * complain about syntax errors on stdout.
929 * Basically, if we're going to read our config file, we should read
930 * it and initialize our data structures. If we're not going to read
931 * our config file, use GIFCONF to initialize our data structures.
933 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
934 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
938 /* we need to count up our interfaces so that we can simplify things
939 * later. we also need to figure out if we have more than one interface
940 * that is routing. */
941 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
942 iface=iface->i_next) {
943 if (iface->i_flags & IFACE_DONTROUTE)
947 i = ninterfaces - i; /* number of routable interfaces */
950 * At this point, we have (at least partially) initialized data
951 * structures. Fill in what we can and verify that nothing is obviously
954 for (iface = interfaces; iface; iface = iface->i_next) {
955 /* Apply the default phase */
956 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
957 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
958 iface->i_flags |= defphase;
961 /* set up router flag information. if we have multiple interfaces
962 * and DONTROUTE isn't set, set up ROUTER. i is the number of
963 * interfaces that don't have the DONTROUTE flag set. */
964 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
965 iface->i_flags |= IFACE_ISROUTER;
968 /* Set default addresses */
969 if ( iface->i_rt == NULL ) {
970 if (( iface->i_rt = newrt(iface)) == NULL ) {
975 if ( iface->i_flags & IFACE_PHASE1 ) {
976 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
977 iface->i_caddr.sat_addr.s_net;
979 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
980 ( iface->i_flags & IFACE_LOOPBACK )) {
981 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
982 iface->i_caddr.sat_addr.s_net;
984 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
985 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
990 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
991 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
994 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
995 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
999 dumpconfig( iface ); /* probably needs args */
1004 * A little consistency check...
1006 if ( ninterfaces < IFBASE ) {
1007 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1012 * Set process name for logging
1015 set_processname("atalkd");
1017 /* do this here so that we can use ifconfig */
1019 if ( plumb() < 0 ) {
1020 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1023 #endif /* __svr4__ */
1025 /* delete pre-existing interface addresses. */
1027 for (iface = interfaces; iface; iface = iface->i_next) {
1028 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1029 #ifdef SIOCATALKDIFADDR
1030 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1031 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1032 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1033 #endif /* SIOCATALKDIFADDR */
1036 #endif /* SIOCDIFADDR */
1039 * Disassociate. The child will send itself a signal when it is
1040 * stable. This indicates that other processes may begin using
1043 switch (i = server_lock("atalkd", pidfile, debug)) {
1048 default: /* parent */
1050 * Wait for the child to send itself a SIGSTOP, after which
1051 * we send it a SIGCONT and exit ourself.
1053 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1054 perror( "wait3" ); /* Child died? */
1057 if ( !WIFSTOPPED( status )) {
1058 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1059 if ( WIFEXITED( status )) {
1060 fprintf( stderr, " Child exited with %d.\n",
1061 WEXITSTATUS( status ));
1063 fprintf( stderr, " Child died.\n" );
1067 if ( kill(i, SIGCONT ) < 0 ) {
1075 openlog( prog, LOG_PID );
1077 set_processname(prog);
1078 syslog_setup(log_debug, logtype_default, logoption_pid, logfacility_daemon );
1081 LOG(log_info, logtype_atalkd, "restart (%s)", version );
1084 * Socket for use in routing ioctl()s. Can't add routes to our
1085 * interfaces until we have our routing socket.
1088 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1089 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1092 if ( shutdown( rtfd, 0 ) < 0 ) {
1093 LOG(log_error, logtype_atalkd, "route shutdown: %s", strerror(errno) );
1097 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1098 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1103 ciface = interfaces;
1106 memset(&sv, 0, sizeof(sv));
1107 sv.sa_handler = as_down;
1108 sigemptyset( &sv.sa_mask );
1109 sigaddset( &sv.sa_mask, SIGUSR1 );
1110 sigaddset( &sv.sa_mask, SIGALRM );
1111 sigaddset( &sv.sa_mask, SIGTERM );
1112 sv.sa_flags = SA_RESTART;
1113 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1114 LOG(log_error, logtype_atalkd, "sigterm: %s", strerror(errno) );
1118 sv.sa_handler = as_debug;
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( SIGUSR1, &sv, NULL) < 0 ) {
1125 LOG(log_error, logtype_atalkd, "sigusr1: %s", strerror(errno) );
1129 sv.sa_handler = as_timer;
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( SIGALRM, &sv, NULL) < 0 ) {
1136 LOG(log_error, logtype_atalkd, "sigalrm: %s", strerror(errno) );
1140 it.it_interval.tv_sec = 10L;
1141 it.it_interval.tv_usec = 0L;
1142 it.it_value.tv_sec = 10L;
1143 it.it_value.tv_usec = 0L;
1144 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1145 LOG(log_error, logtype_atalkd, "setitimer: %s", strerror(errno) );
1149 sigemptyset( &signal_set );
1150 sigaddset(&signal_set, SIGALRM);
1152 /* don't block SIGTERM */
1153 sigaddset(&signal_set, SIGTERM);
1155 sigaddset(&signal_set, SIGUSR1);
1159 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1160 if ( errno == EINTR ) {
1164 LOG(log_error, logtype_atalkd, "select: %s", strerror(errno) );
1169 for ( iface = interfaces; iface; iface = iface->i_next ) {
1170 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1171 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1172 if ( ap->ap_packet ) {
1173 fromlen = sizeof( struct sockaddr_at );
1174 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1175 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1176 LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
1181 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1182 ntohs( sat.sat_addr.s_net ),
1183 sat.sat_addr.s_node, iface->i_name,
1184 iface->i_flags, ap->ap_port, ap->ap_fd );
1185 bprint( Packet, c );
1188 if (sigprocmask(SIG_BLOCK, &signal_set, &old_set) < 0) {
1189 LOG(log_error, logtype_atalkd, "sigprocmask: %s", strerror(errno) );
1193 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1194 LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
1201 if (sigprocmask(SIG_SETMASK, &old_set, NULL) < 0) {
1202 LOG(log_error, logtype_atalkd, "sigprocmask old set: %s", strerror(errno) );
1214 * This code is called (from main(), as_timer(), zip_packet(),
1215 * and rtmp_packet()) to set the initial "bootstrapping" address
1218 void bootaddr(struct interface *iface)
1220 if ( iface == NULL ) {
1225 if ( iface->i_flags & IFACE_ADDR ) {
1226 LOG(log_error, logtype_atalkd, "bootaddr OOPS!" );
1230 if ( iface->i_flags & IFACE_PHASE1 ) {
1231 setaddr( iface, IFACE_PHASE1, 0,
1232 iface->i_caddr.sat_addr.s_node, 0, 0 );
1234 if ( iface->i_flags & IFACE_LOOPBACK ) {
1235 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1236 if ( ciface == iface ) {
1237 ciface = ciface->i_next;
1241 } else if (rtmp_request( iface ) < 0) {
1242 LOG(log_error, logtype_atalkd, "bootaddr (rtmp_request): %s", strerror(errno));
1247 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1248 iface->i_caddr.sat_addr.s_node,
1249 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1251 if ( iface->i_flags & IFACE_LOOPBACK ) {
1252 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1253 if ( ciface == iface ) {
1254 ciface = ciface->i_next;
1258 } else if (zip_getnetinfo( iface ) < 0) {
1259 LOG(log_error, logtype_atalkd, "bootaddr (zip_getnetinfo): %s", strerror(errno));
1264 iface->i_flags |= IFACE_ADDR;
1265 stabletimer = UNSTABLE;
1271 * to manage the i_ports field and the fds for select().
1273 void setaddr(struct interface *iface,
1274 u_int8_t phase, u_int16_t net, u_int8_t node,
1275 u_int16_t first, u_int16_t last)
1281 struct sockaddr_at sat;
1284 if ( iface->i_ports == NULL ) { /* allocate port structures */
1285 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1286 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1287 LOG(log_info, logtype_atalkd, "%s: service unknown", as->as_name );
1289 as->as_port = ntohs( se->s_port );
1291 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1293 LOG(log_error, logtype_atalkd, "malloc: %s", strerror(errno) );
1297 ap->ap_next = iface->i_ports;
1298 ap->ap_iface = iface;
1299 ap->ap_port = as->as_port;
1300 ap->ap_packet = as->as_packet;
1302 iface->i_ports = ap;
1304 } else { /* close ports */
1305 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1306 (void)close( ap->ap_fd );
1311 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1313 iface->i_addr.sat_family = AF_APPLETALK;
1314 iface->i_addr.sat_addr.s_net = net;
1315 iface->i_addr.sat_addr.s_node = node;
1317 nr.nr_phase = phase;
1318 nr.nr_firstnet = first;
1319 nr.nr_lastnet = last;
1320 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1322 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1323 LOG(log_error, logtype_atalkd, "setifaddr: %s (%u-%u): %s. try specifying a \
1324 smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
1327 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1328 LOG(log_error, logtype_atalkd, "getifaddr: %s: %s", iface->i_name, strerror(errno) );
1333 i = 1; /* enable broadcasts */
1335 /* useless message, no? */
1336 LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
1337 #endif /* __svr4__ */
1338 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1339 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1340 LOG(log_error, logtype_atalkd, "socket: %s", strerror(errno) );
1344 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1345 #endif /* ! __svr4 */
1347 memset( &sat, 0, sizeof( struct sockaddr_at ));
1349 sat.sat_len = sizeof( struct sockaddr_at );
1351 sat.sat_family = AF_APPLETALK;
1352 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1353 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1354 sat.sat_port = ap->ap_port;
1356 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1357 sizeof( struct sockaddr_at )) < 0 ) {
1358 LOG(log_error, logtype_atalkd, "bind %u.%u:%u: %s",
1359 ntohs( sat.sat_addr.s_net ),
1360 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1362 /* remove all interfaces if we have a problem with bind */
1363 for (iface = interfaces; iface; iface = iface->i_next) {
1364 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1365 #ifdef SIOCATALKDIFADDR
1366 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1367 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1368 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1369 #endif /* SIOCATALKDIFADDR */
1372 #endif /* SIOCDIFADDR */
1377 /* recalculate nfds and fds */
1379 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1380 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1381 FD_SET( ap->ap_fd, &fds );
1382 if ( ap->ap_fd > nfds ) {
1390 int ifsetallmulti (const char *iname, int set)
1395 memset(&ifr, 0, sizeof(ifr));
1397 if (( sock = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1401 /* get interface config */
1402 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1403 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
1408 /* should we set or unset IFF_ALLMULTI */
1410 ifr.ifr_flags |= IFF_ALLMULTI;
1412 ifr.ifr_flags &= ~IFF_ALLMULTI;
1414 /* set interface config */
1415 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1416 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
1425 int ifconfig( const char *iname, unsigned long cmd, struct sockaddr_at *sa)
1430 memset(&ifr, 0, sizeof(ifr));
1431 strcpy( ifr.ifr_name, iname );
1432 ifr.ifr_addr = *(struct sockaddr *)sa;
1434 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1437 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1442 if ( cmd == SIOCGIFADDR ) {
1443 *(struct sockaddr *)sa = ifr.ifr_addr;
1448 void dumpconfig( struct interface *iface)
1452 printf( "%s", iface->i_name );
1453 if ( iface->i_flags & IFACE_RSEED ) {
1454 printf( " -router" );
1455 } else if ( iface->i_flags & IFACE_SEED ) {
1459 if ( iface->i_flags & IFACE_DONTROUTE)
1460 printf( " -dontroute");
1462 printf( " -phase" );
1463 if ( iface->i_flags & IFACE_PHASE1 ) {
1468 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1469 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1470 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1472 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1473 iface->i_addr.sat_addr.s_node );
1474 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1475 iface->i_caddr.sat_addr.s_node );
1476 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1477 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1478 ((struct ziptab *)l->l_data)->zt_name );
1484 void dumproutes(void)
1486 struct interface *iface;
1487 struct rtmptab *rtmp;
1491 for ( iface = interfaces; iface; iface = iface->i_next ) {
1492 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1493 if ( rtmp->rt_gate == 0 ) {
1494 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1495 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1496 ntohs( rtmp->rt_lastnet ));
1498 printf( "%u", ntohs( rtmp->rt_firstnet ));
1501 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1502 printf( "%u.%u for %u-%u",
1503 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1504 rtmp->rt_gate->g_sat.sat_addr.s_node,
1505 ntohs( rtmp->rt_firstnet ),
1506 ntohs( rtmp->rt_lastnet ));
1508 printf( "%u.%u for %u",
1509 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1510 rtmp->rt_gate->g_sat.sat_addr.s_node,
1511 ntohs( rtmp->rt_firstnet ));
1515 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1519 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1520 zt = (struct ziptab *)l->l_data;
1521 printf( " %.*s", zt->zt_len, zt->zt_name );
1532 void dumpzones(void)
1534 struct interface *iface;
1535 struct rtmptab *rtmp;
1539 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1540 printf( "%.*s", zt->zt_len, zt->zt_name );
1541 for ( l = zt->zt_rt; l; l = l->l_next ) {
1542 rtmp = (struct rtmptab *)l->l_data;
1543 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1544 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1545 ntohs( rtmp->rt_lastnet ));
1547 printf( " %u", ntohs( rtmp->rt_firstnet ));
1549 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {