2 * $Id: main.c,v 1.17.8.5 2004-02-28 10:56:26 bfernhomberg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #if defined( sun ) && defined( __svr4__ )
15 #include </usr/ucbinclude/sys/file.h>
16 #else /* sun __svr4__ */
18 #endif /* sun __svr4__ */
20 #include <sys/resource.h>
21 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_WAIT_H
27 #endif /* HAVE_SYS_WAIT_H */
29 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
30 #endif /* ! WEXITSTATUS */
32 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
33 #endif /* ! WIFEXITED */
35 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
41 #include <net/route.h>
44 #include <net/route.h>
46 #include <netinet/in.h>
49 #include <atalk/logger.h>
57 #include <netatalk/endian.h>
58 #include <netatalk/at.h>
59 #include <atalk/compat.h>
60 #include <atalk/zip.h>
61 #include <atalk/rtmp.h>
62 #include <atalk/ddp.h>
63 #include <atalk/atp.h>
64 #include <atalk/paths.h>
65 #include <atalk/util.h>
68 #include <sys/sockio.h>
72 #include "interface.h"
80 /* Forward Declarations */
81 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
83 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
85 #define SOCKLEN_T unsigned int
86 #endif /* SOCKLEN_T */
89 #define WEXITSTATUS(x) ((x).w_retcode)
90 #endif /* WEXITSTATUS */
92 /* linux has a special ioctl for appletalk device destruction. as of
93 * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
94 * fact that SIOCDIFADDR may be defined on linux despite the fact that
96 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
97 #define SIOCDIFADDR SIOCATALKDIFADDR
100 #define elements(a) (sizeof(a)/sizeof((a)[0]))
104 extern int rtmp_packet();
105 extern int nbp_packet();
106 extern int aep_packet();
107 extern int zip_packet();
111 struct atserv atserv[] = {
112 { "rtmp", 1, rtmp_packet }, /* 0 */
113 { "nbp", 2, nbp_packet }, /* 1 */
114 { "echo", 4, aep_packet }, /* 2 */
115 { "zip", 6, zip_packet }, /* 3 */
117 int atservNATSERV = elements( atserv );
119 struct interface *interfaces = NULL, *ciface = NULL;
121 int debug = 0, quiet = 0, chatty = 0;
122 char *configfile = NULL;
123 int ziptimeout = 0, transition = 0;
124 int stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
125 static int ninterfaces;
126 int defphase = IFACE_PHASE2;
129 char Packet[ PKTSZ ];
130 char *version = VERSION;
131 static char *pidfile = _PATH_ATALKDLOCK;
136 int readconf( char * );
137 int getifconf( void );
138 int writeconf( char * );
140 /* this is the messiest of the bunch as atalkd can exit pretty much
141 * everywhere. we delete interfaces here instead of in as_down. */
142 static void atalkd_exit(const int i)
145 struct interface *iface;
147 for (iface = interfaces; iface; iface = iface->i_next) {
148 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
149 #ifdef SIOCATALKDIFADDR
150 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
151 if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr))
153 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
154 #endif /* SIOCATALKIFADDR */
155 LOG(log_error, logtype_atalkd, "difaddr(%u.%u): %s",
156 ntohs(iface->i_addr.sat_addr.s_net),
157 iface->i_addr.sat_addr.s_node, strerror(errno));
160 if (!(iface->i_flags & IFACE_WASALLMULTI) && (iface->i_flags & IFACE_ALLMULTI))
161 ifsetallmulti(iface->i_name, 0);
164 #endif /* SOPCDOFADDR */
166 server_unlock(pidfile);
171 static void as_timer(int sig)
173 struct sockaddr_at sat;
176 struct rtmp_tuple rt;
177 struct atport *ap, *zap, *rap;
178 struct interface *iface, *iface2;
179 struct gate *gate, *fgate = NULL;
180 struct rtmptab *rtmp, *frtmp;
182 char *data, *end, packet[ ATP_BUFSIZ ];
188 memset(&sat, 0, sizeof( struct sockaddr_at ));
189 for ( iface = interfaces; iface; iface = iface->i_next ) {
190 if ( iface->i_flags & IFACE_LOOPBACK ) {
193 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
194 if ( ap->ap_packet == zip_packet ) {
197 if ( ap->ap_packet == rtmp_packet ) {
202 if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
204 if ( iface->i_time < 3 ) {
205 if ( iface->i_flags & IFACE_PHASE1 ) {
206 if (rtmp_request( iface ) < 0) {
207 LOG(log_error, logtype_atalkd, "rtmp_request: %s", strerror(errno));
212 if (zip_getnetinfo( iface ) < 0) {
213 LOG(log_error, logtype_atalkd, "zip_getnetinfo: %s", strerror(errno));
220 iface->i_flags |= IFACE_NOROUTER;
221 if ((iface->i_flags & IFACE_ISROUTER)) {
222 if (( iface->i_flags & IFACE_SEED ) == 0 ) {
224 * No seed info, and we've got multiple interfaces.
227 LOG(log_info, logtype_atalkd,
228 "as_timer multiple interfaces, no seed" );
229 LOG(log_info, logtype_atalkd, "as_timer can't configure %s",
231 LOG(log_info, logtype_atalkd, "as_timer waiting for router" );
236 * Complete configuration for iface, and boot next
239 iface->i_flags |= IFACE_CONFIG;
240 for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
241 if (addzone( iface->i_rt, zt->zt_len,
243 LOG(log_error, logtype_atalkd, "addzone: %s", strerror(errno));
247 if ( iface->i_rt->rt_zt ) {
248 iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
249 iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
251 if ( iface->i_flags & IFACE_PHASE1 ) {
252 LOG(log_info, logtype_atalkd,
253 "as_timer configured %s phase 1 from seed",
255 setaddr( iface, IFACE_PHASE1,
256 iface->i_caddr.sat_addr.s_net,
257 iface->i_addr.sat_addr.s_node,
258 iface->i_caddr.sat_addr.s_net,
259 iface->i_caddr.sat_addr.s_net );
261 LOG(log_info, logtype_atalkd,
262 "as_timer configured %s phase 2 from seed",
266 if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
267 LOG(log_error, logtype_atalkd,
268 "as_timer: can't route %u.%u to loop: %s",
269 ntohs( iface->i_addr.sat_addr.s_net ),
270 iface->i_addr.sat_addr.s_node,
274 if ( iface == ciface ) {
275 ciface = ciface->i_next;
281 * Configure for no router operation. Wait for a route
282 * to become available in rtmp_packet().
284 LOG(log_info, logtype_atalkd, "config for no router" );
286 if ( iface->i_flags & IFACE_PHASE2 ) {
287 iface->i_rt->rt_firstnet = 0;
288 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
289 setaddr( iface, IFACE_PHASE2,
290 iface->i_addr.sat_addr.s_net,
291 iface->i_addr.sat_addr.s_node,
292 0, htons( STARTUP_LASTNET ));
294 if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
295 LOG(log_error, logtype_atalkd,
296 "as_timer: can't route %u.%u to loopback: %s",
297 ntohs( iface->i_addr.sat_addr.s_net ),
298 iface->i_addr.sat_addr.s_node,
303 if ( iface == ciface ) {
304 ciface = ciface->i_next;
311 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
313 free( (caddr_t)fgate );
318 data = packet + 1 + sizeof( struct ziphdr );
319 end = packet + sizeof( packet );
322 sat.sat_port = zap->ap_port;
325 * Perform timeouts on routers. If we've only got one
326 * interface, we'll use these timeouts to decide that
327 * our zone has gone away.
329 if ( ++gate->g_state >= RTMPTAB_BAD ) {
330 LOG(log_info, logtype_atalkd, "as_timer gateway %u.%u down",
331 ntohs( gate->g_sat.sat_addr.s_net ),
332 gate->g_sat.sat_addr.s_node );
335 frtmp = rtmp->rt_next;
336 if ( rtmp->rt_hops == RTMPHOPS_POISON ||
337 rtmp->rt_iprev == 0 ) {
340 rtmp->rt_hops = RTMPHOPS_POISON;
341 if ((cc = rtmp_replace( rtmp )) < 0) {
342 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
346 gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
351 if ( gate->g_rt == 0 ) {
352 if ( gate->g_prev == 0 ) {
353 gate->g_iface->i_gate = gate->g_next;
355 gate->g_prev->g_next = gate->g_next;
357 if ( gate->g_next != 0 ) {
358 gate->g_next->g_prev = gate->g_prev;
360 fgate = gate; /* can't free here, just mark it */
363 * If this is the last router on the only interface,
364 * reconfigure our netrange. By marking the interface
365 * as having no router, we will notice when a router
368 * XXX: actually, we always reconfigure an interface
369 * if we're not a seed router.
372 if ( gate->g_iface->i_gate == 0 &&
373 ((iface->i_flags & IFACE_SEED) == 0)) {
374 gate->g_iface->i_flags |= IFACE_NOROUTER;
375 gate->g_iface->i_flags &= ~IFACE_CONFIG;
377 /* get rid of any zones associated with this iface */
378 if (gate->g_iface->i_rt->rt_zt) {
379 rtmp_delzonemap(gate->g_iface->i_rt);
380 gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
383 LOG(log_info, logtype_atalkd, "as_timer last gateway down" );
385 /* Set netrange to 0-fffe. */
386 if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
387 gate->g_iface->i_rt->rt_firstnet = 0;
388 gate->g_iface->i_rt->rt_lastnet =
389 htons( STARTUP_LASTNET );
390 setaddr( iface, IFACE_PHASE2,
391 iface->i_addr.sat_addr.s_net,
392 iface->i_addr.sat_addr.s_node,
393 0, htons( STARTUP_LASTNET ));
400 * If we don't have a zone for our interface yet, ask for
401 * it from any router (all routers) on the interface.
403 if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
404 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
405 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
406 data += sizeof( u_short );
413 * Delete old routing tuples.
415 if ( rtmp->rt_state != RTMPTAB_PERM ) {
420 * We've not been updated for this route in a while. If
421 * it's not in use, go ahead and remove it. If it is in
422 * use, mark the route as down (POISON), and look for a
423 * better route. If one is found, delete this route and use
424 * the new one. If it's not found, mark the route as GOOD
425 * (so we'll propogate our poison) and delete it the next
426 * time it becomes BAD.
428 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
429 frtmp = rtmp->rt_next;
430 if ( rtmp->rt_iprev == 0 ) { /* not in use */
432 } else { /* in use */
433 if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
436 rtmp->rt_hops = RTMPHOPS_POISON;
437 if ((cc = rtmp_replace( rtmp )) < 0) {
438 LOG(log_error, logtype_atalkd, "rtmp_replace: %s", strerror(errno));
442 rtmp->rt_state = RTMPTAB_GOOD;
452 if ( rtmp->rt_iprev &&
453 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
454 if ( data + sizeof( u_short ) > end || n == 255 ) {
455 /* send what we've got */
456 zh.zh_op = ZIPOP_QUERY;
460 *data++ = DDPTYPE_ZIP;
461 memcpy( data, &zh, sizeof( struct ziphdr ));
463 if ( sendto( zap->ap_fd, packet, cc, 0,
464 (struct sockaddr *)&sat,
465 sizeof( struct sockaddr_at )) < 0 ) {
466 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
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",
486 ntohs( rtmp->rt_firstnet ));
488 if ( rtmp->rt_nzq > 3 ) {
490 rtmp = rtmp->rt_next;
496 rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
497 memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
498 data += sizeof( u_short );
501 rtmp = rtmp->rt_next;
504 /* send what we've got */
506 zh.zh_op = ZIPOP_QUERY;
510 *data++ = DDPTYPE_ZIP;
511 memcpy( data, &zh, sizeof( struct ziphdr ));
513 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
514 sizeof( struct sockaddr_at )) < 0 ) {
515 LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
520 free( (caddr_t)fgate );
525 * Send RTMP broadcasts if we have multiple interfaces or our
526 * interface is configured as a router.
528 if ((iface->i_flags & IFACE_ISROUTER)) {
530 sat.sat_len = sizeof( struct sockaddr_at );
532 sat.sat_family = AF_APPLETALK;
533 sat.sat_addr.s_net = ATADDR_ANYNET;
534 sat.sat_addr.s_node = ATADDR_BCAST;
535 sat.sat_port = rap->ap_port;
538 end = data + sizeof( packet );
539 *data++ = DDPTYPE_RTMPRD;
540 rh.rh_net = iface->i_addr.sat_addr.s_net;
542 rh.rh_node = iface->i_addr.sat_addr.s_node;
543 memcpy( data, &rh, sizeof( struct rtmp_head ));
544 data += sizeof( struct rtmp_head );
548 if ( iface->i_flags & IFACE_PHASE1 ) {
551 memcpy( data, &rt, SZ_RTMPTUPLE );
552 data += SZ_RTMPTUPLE;
554 rt.rt_net = iface->i_rt->rt_firstnet;
556 memcpy( data, &rt, SZ_RTMPTUPLE );
557 data += SZ_RTMPTUPLE;
559 rt.rt_net = iface->i_rt->rt_lastnet;
561 memcpy( data, &rt, SZ_RTMPTUPLE );
562 data += SZ_RTMPTUPLE;
565 for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
567 /* XXX: there used to be a bit checking against iface ==
568 iface2. also, we don't want to send an rtmp broadcast
569 to an interface that doesn't want it. */
570 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
571 ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
575 * Fill in tuples. Always send the same thing, regardless
576 * of the phase of the destination. Routers who don't
577 * understand extended rtmp packets will toss extended
578 * tuples because their distance will have the high bit set.
580 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
581 /* don't broadcast routes we have no zone for */
582 if ( rtmp->rt_zt == 0 ||
583 ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
584 ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
588 if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
589 data + 2 * SZ_RTMPTUPLE > end ) ||
590 data + SZ_RTMPTUPLE > end ) {
591 if ( sendto( rap->ap_fd, packet, data - packet, 0,
592 (struct sockaddr *)&sat,
593 sizeof( struct sockaddr_at )) < 0 ) {
594 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
595 ntohs( sat.sat_addr.s_net ),
597 ntohs( iface->i_rt->rt_firstnet ),
601 if ( iface->i_flags & IFACE_PHASE2 ) {
602 data = packet + 1 + sizeof( struct rtmp_head ) +
605 data = packet + 1 + sizeof( struct rtmp_head ) +
611 rt.rt_net = rtmp->rt_firstnet;
612 rt.rt_dist = rtmp->rt_hops;
613 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
616 memcpy( data, &rt, SZ_RTMPTUPLE );
617 data += SZ_RTMPTUPLE;
619 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
620 rt.rt_net = rtmp->rt_lastnet;
622 memcpy( data, &rt, SZ_RTMPTUPLE );
623 data += SZ_RTMPTUPLE;
631 if ( sendto( rap->ap_fd, packet, data - packet, 0,
632 (struct sockaddr *)&sat,
633 sizeof( struct sockaddr_at )) < 0 ) {
634 LOG(log_error, logtype_atalkd, "as_timer sendto %u.%u (%u): %s",
635 ntohs( sat.sat_addr.s_net ),
637 ntohs( iface->i_rt->rt_firstnet ),
645 * Check if we're stable. Each time we configure an interface, we
646 * sent stabletimer to UNSTABLE. If stabletimer ever gets to
647 * STABLEANYWAY, we give up and decide to "be" stable anyway.
648 * Normally, we wait for stabletimer get <= STABLE with no new rtmp
649 * data and all zip data complete.
652 if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
653 /* write out config file */
655 writeconf( configfile );
657 if ( stabletimer-- <= STABLEANYWAY ) {
663 if ( stable && !noparent ) {
665 LOG(log_info, logtype_atalkd, "ready %d/%d/%d", stabletimer, newrtmpdata,
669 * Seems like we could get here more than once...
671 if ( kill( getpid(), SIGSTOP ) < 0 ) {
672 LOG(log_error, logtype_atalkd, "as_timer: kill-self failed!" );
686 * Consistency check...
690 struct rtmptab *rtmp;
691 struct list *lr, *lz;
694 for ( zt = ziptab; zt; zt = zt->zt_next ) {
695 for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
696 rtmp = (struct rtmptab *)lr->l_data;
697 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
698 LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)\n",
699 zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
700 ntohs( rtmp->rt_lastnet ));
703 for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
704 if ( zt == (struct ziptab *)lz->l_data ) {
709 LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s\n",
710 ntohs( rtmp->rt_firstnet ),
711 ntohs( rtmp->rt_lastnet ),
712 zt->zt_len, zt->zt_name );
720 #if !defined( ibm032 ) && !defined( _IBMR2 )
722 #endif /* ! ibm032 && ! _IBMR2 */
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.
824 #if !defined( ibm032 ) && !defined( _IBMR2 )
826 #endif /* ! ibm032 && ! _IBMR2 */
829 struct interface *iface;
833 for ( iface = interfaces; iface; iface = iface->i_next ) {
834 for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
835 for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
836 if ( rt->rt_iprev ) {
837 if ( gateroute( RTMP_DEL, rt ) < 0 ) {
838 LOG(log_error, logtype_atalkd, "as_down remove %u-%u failed: %s",
839 ntohs( rt->rt_firstnet ),
840 ntohs( rt->rt_lastnet ),
846 if ( iface->i_flags & IFACE_LOOP ) {
847 if (looproute( iface, RTMP_DEL )) {
848 LOG(log_error, logtype_atalkd, "as_down remove %s %u.%u failed: %s",
849 iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
850 iface->i_addr.sat_addr.s_node,
856 LOG(log_info, logtype_atalkd, "done" );
867 struct sockaddr_at sat;
870 struct interface *iface;
879 while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
882 defphase = IFACE_PHASE1;
886 defphase = IFACE_PHASE2;
897 case 'q' : /* don't seed */
901 case 's' : /* seed */
905 case 't' : /* transition */
909 case 'P' : /* pid file */
913 case 'v' : /* version */
914 printf( "atalkd (version %s)\n", version );
919 fprintf( stderr, "Unknown option -- '%c'\n", c );
923 if ( optind != ac ) {
924 fprintf( stderr, "Too many arguments.\n" );
928 if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
935 * Configure loop back address first, so appearances of "lo0" in
936 * the config file fail. Also insures that lo0 gets configured,
937 * even if there's some hangup during configuration of some
940 if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
941 perror( "newiface" );
944 interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
947 * Check our initial configuration before we fork. This way we can
948 * complain about syntax errors on stdout.
950 * Basically, if we're going to read our config file, we should read
951 * it and initialize our data structures. If we're not going to read
952 * our config file, use GIFCONF to initialize our data structures.
954 if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
955 fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
959 /* we need to count up our interfaces so that we can simplify things
960 * later. we also need to figure out if we have more than one interface
961 * that is routing. */
962 for (i = 0, ninterfaces = 0, iface = interfaces; iface;
963 iface=iface->i_next) {
964 if (iface->i_flags & IFACE_DONTROUTE)
968 i = ninterfaces - i; /* number of routable interfaces */
971 * At this point, we have (at least partially) initialized data
972 * structures. Fill in what we can and verify that nothing is obviously
975 for (iface = interfaces; iface; iface = iface->i_next) {
976 /* Apply the default phase */
977 if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
978 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
979 iface->i_flags |= defphase;
982 /* set up router flag information. if we have multiple interfaces
983 * and DONTROUTE isn't set, set up ROUTER. i is the number of
984 * interfaces that don't have the DONTROUTE flag set. */
985 if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
986 iface->i_flags |= IFACE_ISROUTER;
989 /* Set default addresses */
990 if ( iface->i_rt == NULL ) {
991 if (( iface->i_rt = newrt(iface)) == NULL ) {
996 if ( iface->i_flags & IFACE_PHASE1 ) {
997 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
998 iface->i_caddr.sat_addr.s_net;
1000 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
1001 ( iface->i_flags & IFACE_LOOPBACK )) {
1002 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
1003 iface->i_caddr.sat_addr.s_net;
1005 iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
1006 iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
1011 if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
1012 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
1015 if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1016 iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1020 dumpconfig( iface ); /* probably needs args */
1025 * A little consistency check...
1027 if ( ninterfaces < IFBASE ) {
1028 fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1033 * Set process name for logging
1036 set_processname("atalkd");
1038 /* do this here so that we can use ifconfig */
1040 if ( plumb() < 0 ) {
1041 fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1044 #endif /* __svr4__ */
1046 /* delete pre-existing interface addresses. */
1048 for (iface = interfaces; iface; iface = iface->i_next) {
1049 if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1050 #ifdef SIOCATALKDIFADDR
1051 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1052 ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1053 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1054 #endif /* SIOCATALKDIFADDR */
1057 #endif /* SIOCDIFADDR */
1060 * Disassociate. The child will send itself a signal when it is
1061 * stable. This indicates that other processes may begin using
1064 switch (i = server_lock("atalkd", pidfile, debug)) {
1069 default: /* parent */
1071 * Wait for the child to send itself a SIGSTOP, after which
1072 * we send it a SIGCONT and exit ourself.
1074 if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1075 perror( "wait3" ); /* Child died? */
1078 if ( !WIFSTOPPED( status )) {
1079 fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1080 if ( WIFEXITED( status )) {
1081 fprintf( stderr, " Child exited with %d.\n",
1082 WEXITSTATUS( status ));
1084 fprintf( stderr, " Child died.\n" );
1088 if ( kill(i, SIGCONT ) < 0 ) {
1096 openlog( prog, LOG_PID );
1098 set_processname(prog);
1099 syslog_setup(log_debug, logtype_default, logoption_pid, logfacility_daemon );
1102 LOG(log_info, logtype_atalkd, "restart (%s)", version );
1105 * Socket for use in routing ioctl()s. Can't add routes to our
1106 * interfaces until we have our routing socket.
1109 if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1110 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1113 if ( shutdown( rtfd, 0 ) < 0 ) {
1114 LOG(log_error, logtype_atalkd, "route shutdown: %s", strerror(errno) );
1118 if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1119 LOG(log_error, logtype_atalkd, "route socket: %s", strerror(errno) );
1124 ciface = interfaces;
1127 memset(&sv, 0, sizeof(sv));
1128 sv.sa_handler = as_down;
1129 sigemptyset( &sv.sa_mask );
1130 sigaddset( &sv.sa_mask, SIGUSR1 );
1131 sigaddset( &sv.sa_mask, SIGALRM );
1132 sigaddset( &sv.sa_mask, SIGTERM );
1133 sv.sa_flags = SA_RESTART;
1134 if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1135 LOG(log_error, logtype_atalkd, "sigterm: %s", strerror(errno) );
1139 sv.sa_handler = as_debug;
1140 sigemptyset( &sv.sa_mask );
1141 sigaddset( &sv.sa_mask, SIGUSR1 );
1142 sigaddset( &sv.sa_mask, SIGALRM );
1143 sigaddset( &sv.sa_mask, SIGTERM );
1144 sv.sa_flags = SA_RESTART;
1145 if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1146 LOG(log_error, logtype_atalkd, "sigusr1: %s", strerror(errno) );
1150 sv.sa_handler = as_timer;
1151 sigemptyset( &sv.sa_mask );
1152 sigaddset( &sv.sa_mask, SIGUSR1 );
1153 sigaddset( &sv.sa_mask, SIGALRM );
1154 sigaddset( &sv.sa_mask, SIGTERM );
1155 sv.sa_flags = SA_RESTART;
1156 if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1157 LOG(log_error, logtype_atalkd, "sigalrm: %s", strerror(errno) );
1161 it.it_interval.tv_sec = 10L;
1162 it.it_interval.tv_usec = 0L;
1163 it.it_value.tv_sec = 10L;
1164 it.it_value.tv_usec = 0L;
1165 if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1166 LOG(log_error, logtype_atalkd, "setitimer: %s", strerror(errno) );
1172 if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1173 if ( errno == EINTR ) {
1177 LOG(log_error, logtype_atalkd, "select: %s", strerror(errno) );
1182 for ( iface = interfaces; iface; iface = iface->i_next ) {
1183 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1184 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1185 if ( ap->ap_packet ) {
1186 fromlen = sizeof( struct sockaddr_at );
1187 if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1188 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1189 LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
1194 printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1195 ntohs( sat.sat_addr.s_net ),
1196 sat.sat_addr.s_node, iface->i_name,
1197 iface->i_flags, ap->ap_port, ap->ap_fd );
1198 bprint( Packet, c );
1202 if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
1203 LOG(log_error, logtype_atalkd, "sighold: %s", strerror(errno) );
1206 #else /* __svr4__ */
1207 mask = sigsetmask( sigmask( SIGALRM ) |
1208 sigmask( SIGUSR1 ));
1209 #endif /* __svr4__ */
1210 if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1211 LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
1219 if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
1220 LOG(log_error, logtype_atalkd, "sigrelse: %s", strerror(errno) );
1223 #else /* __svr4__ */
1225 #endif /* __svr4__ */
1234 * This code is called (from main(), as_timer(), zip_packet(),
1235 * and rtmp_packet()) to set the initial "bootstrapping" address
1238 void bootaddr( iface )
1239 struct interface *iface;
1246 if ( iface->i_flags & IFACE_ADDR ) {
1247 LOG(log_error, logtype_atalkd, "bootaddr OOPS!" );
1251 if ( iface->i_flags & IFACE_PHASE1 ) {
1252 setaddr( iface, IFACE_PHASE1, 0,
1253 iface->i_caddr.sat_addr.s_node, 0, 0 );
1255 if ( iface->i_flags & IFACE_LOOPBACK ) {
1256 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1257 if ( ciface == iface ) {
1258 ciface = ciface->i_next;
1262 } else if (rtmp_request( iface ) < 0) {
1263 LOG(log_error, logtype_atalkd, "bootaddr (rtmp_request): %s", strerror(errno));
1268 setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1269 iface->i_caddr.sat_addr.s_node,
1270 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1272 if ( iface->i_flags & IFACE_LOOPBACK ) {
1273 iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1274 if ( ciface == iface ) {
1275 ciface = ciface->i_next;
1279 } else if (zip_getnetinfo( iface ) < 0) {
1280 LOG(log_error, logtype_atalkd, "bootaddr (zip_getnetinfo): %s", strerror(errno));
1285 iface->i_flags |= IFACE_ADDR;
1286 stabletimer = UNSTABLE;
1292 * to manage the i_ports field and the fds for select().
1294 void setaddr(struct interface *iface,
1295 u_int8_t phase, u_int16_t net, u_int8_t node,
1296 u_int16_t first, u_int16_t last)
1302 struct sockaddr_at sat;
1305 if ( iface->i_ports == NULL ) { /* allocate port structures */
1306 for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1307 if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1308 LOG(log_info, logtype_atalkd, "%s: service unknown", as->as_name );
1310 as->as_port = ntohs( se->s_port );
1312 if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1314 LOG(log_error, logtype_atalkd, "malloc: %s", strerror(errno) );
1318 ap->ap_next = iface->i_ports;
1319 ap->ap_iface = iface;
1320 ap->ap_port = as->as_port;
1321 ap->ap_packet = as->as_packet;
1323 iface->i_ports = ap;
1325 } else { /* close ports */
1326 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1327 (void)close( ap->ap_fd );
1332 iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1334 iface->i_addr.sat_family = AF_APPLETALK;
1335 iface->i_addr.sat_addr.s_net = net;
1336 iface->i_addr.sat_addr.s_node = node;
1338 nr.nr_phase = phase;
1339 nr.nr_firstnet = first;
1340 nr.nr_lastnet = last;
1341 memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1343 if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1344 LOG(log_error, logtype_atalkd, "setifaddr: %s (%u-%u): %s. try specifying a \
1345 smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
1348 if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1349 LOG(log_error, logtype_atalkd, "getifaddr: %s: %s", iface->i_name, strerror(errno) );
1354 i = 1; /* enable broadcasts */
1356 /* useless message, no? */
1357 LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
1358 #endif /* __svr4__ */
1359 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1360 if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1361 LOG(log_error, logtype_atalkd, "socket: %s", strerror(errno) );
1365 setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1366 #endif /* ! __svr4 */
1368 memset( &sat, 0, sizeof( struct sockaddr_at ));
1370 sat.sat_len = sizeof( struct sockaddr_at );
1372 sat.sat_family = AF_APPLETALK;
1373 sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1374 sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1375 sat.sat_port = ap->ap_port;
1377 if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1378 sizeof( struct sockaddr_at )) < 0 ) {
1379 LOG(log_error, logtype_atalkd, "bind %u.%u:%u: %s",
1380 ntohs( sat.sat_addr.s_net ),
1381 sat.sat_addr.s_node, sat.sat_port, strerror(errno) );
1383 /* remove all interfaces if we have a problem with bind */
1384 for (iface = interfaces; iface; iface = iface->i_next) {
1385 if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1386 #ifdef SIOCATALKDIFADDR
1387 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1388 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1389 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1390 #endif /* SIOCATALKDIFADDR */
1393 #endif /* SIOCDIFADDR */
1398 /* recalculate nfds and fds */
1400 for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1401 for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1402 FD_SET( ap->ap_fd, &fds );
1403 if ( ap->ap_fd > nfds ) {
1411 int ifsetallmulti ( iname, set )
1418 memset(&ifr, 0, sizeof(ifr));
1420 if (( sock = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1424 /* get interface config */
1425 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1426 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
1431 /* should we set or unset IFF_ALLMULTI */
1433 ifr.ifr_flags |= IFF_ALLMULTI;
1435 ifr.ifr_flags &= ~IFF_ALLMULTI;
1437 /* set interface config */
1438 strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
1439 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
1448 int ifconfig( iname, cmd, sa )
1451 struct sockaddr_at *sa;
1456 memset(&ifr, 0, sizeof(ifr));
1457 strcpy( ifr.ifr_name, iname );
1458 ifr.ifr_addr = *(struct sockaddr *)sa;
1460 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1463 if ( ioctl( s, cmd, &ifr ) < 0 ) {
1468 if ( cmd == SIOCGIFADDR ) {
1469 *(struct sockaddr *)sa = ifr.ifr_addr;
1474 void dumpconfig( iface )
1475 struct interface *iface;
1479 printf( "%s", iface->i_name );
1480 if ( iface->i_flags & IFACE_RSEED ) {
1481 printf( " -router" );
1482 } else if ( iface->i_flags & IFACE_SEED ) {
1486 if ( iface->i_flags & IFACE_DONTROUTE)
1487 printf( " -dontroute");
1489 printf( " -phase" );
1490 if ( iface->i_flags & IFACE_PHASE1 ) {
1495 printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1496 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1497 printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1499 printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1500 iface->i_addr.sat_addr.s_node );
1501 printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1502 iface->i_caddr.sat_addr.s_node );
1503 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1504 printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1505 ((struct ziptab *)l->l_data)->zt_name );
1513 struct interface *iface;
1514 struct rtmptab *rtmp;
1518 for ( iface = interfaces; iface; iface = iface->i_next ) {
1519 for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1520 if ( rtmp->rt_gate == 0 ) {
1521 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1522 printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1523 ntohs( rtmp->rt_lastnet ));
1525 printf( "%u", ntohs( rtmp->rt_firstnet ));
1528 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1529 printf( "%u.%u for %u-%u",
1530 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1531 rtmp->rt_gate->g_sat.sat_addr.s_node,
1532 ntohs( rtmp->rt_firstnet ),
1533 ntohs( rtmp->rt_lastnet ));
1535 printf( "%u.%u for %u",
1536 ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1537 rtmp->rt_gate->g_sat.sat_addr.s_node,
1538 ntohs( rtmp->rt_firstnet ));
1542 if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1546 for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1547 zt = (struct ziptab *)l->l_data;
1548 printf( " %.*s", zt->zt_len, zt->zt_name );
1561 struct interface *iface;
1562 struct rtmptab *rtmp;
1566 for ( zt = ziptab; zt; zt = zt->zt_next ) {
1567 printf( "%.*s", zt->zt_len, zt->zt_name );
1568 for ( l = zt->zt_rt; l; l = l->l_next ) {
1569 rtmp = (struct rtmptab *)l->l_data;
1570 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1571 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1572 ntohs( rtmp->rt_lastnet ));
1574 printf( " %u", ntohs( rtmp->rt_firstnet ));
1576 if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {