2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
10 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
15 #include <sys/param.h>
17 #include <netatalk/at.h>
18 #include <netatalk/endian.h>
19 #include <atalk/paths.h>
20 #include <atalk/util.h>
29 #include <sys/sockio.h>
30 #include <sys/stropts.h>
33 #include "interface.h"
34 #include "multicast.h"
39 #ifndef IFF_SLAVE /* a little backward compatibility */
43 int router(), dontroute(), seed(), phase(), net(), addr(), zone();
50 { "dontroute", dontroute },
64 static char *argv[ 128 ];
65 static char buf[ 1024 ];
72 memset(argv, 0, sizeof(argv));
77 * This parser should be made more powerful -- it should
78 * handle various escapes, e.g. \" and \031.
80 while ( *p != '\0' ) {
81 while ( *p != '\0' && *p != '"' && isspace( *p )) {
90 while ( *p != '\0' && *p != '"' ) {
95 while ( *p != '\0' && !isspace( *p )) {
102 if ( argv[ 0 ] == '\0' || *argv[ 0 ] == '#' ) {
112 char *path, *p, newpath[ MAXPATHLEN ], line[ 1024 ];
114 FILE *conf, *newconf;
115 struct interface *iface;
120 path = _PATH_ATALKDCONF;
125 /* check if old conf is writable */
126 if ( stat( path, &st ) == 0 ) {
127 if (( st.st_mode & S_IWUSR ) == 0 ) {
128 syslog( LOG_INFO, "%s not writable, won't rewrite", path );
134 if (( p = strrchr( path, '/' )) == NULL ) {
135 strcpy( newpath, _PATH_ATALKDTMP );
137 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
139 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
140 syslog( LOG_ERR, "%s: %m", newpath );
143 if (( newconf = fdopen( fd, "w" )) == NULL ) {
144 syslog( LOG_ERR, "fdreopen %s: %m", newpath );
148 if (( conf = fopen( path, "r" )) == NULL && cf ) {
149 syslog( LOG_ERR, "%s: %m", path );
153 iface = interfaces->i_next;
155 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
156 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
157 if ( fputs( line, newconf ) == EOF ) {
158 syslog( LOG_ERR, "fputs: %m" );
164 /* write real lines */
166 fprintf( newconf, "%s", iface->i_name );
167 if ( iface->i_flags & IFACE_RSEED ) {
168 fprintf( newconf, " -router" );
169 } else if ( iface->i_flags & IFACE_SEED ) {
170 fprintf( newconf, " -seed" );
172 if ( iface->i_flags & IFACE_DONTROUTE) {
173 fprintf( newconf, " -dontroute");
176 fprintf( newconf, " -phase %d",
177 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
178 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
179 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
180 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
182 fprintf( newconf, " -addr %u.%u",
183 ntohs( iface->i_addr.sat_addr.s_net ),
184 iface->i_addr.sat_addr.s_node );
185 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
186 fprintf( newconf, " -zone \"%.*s\"",
187 ((struct ziptab *)l->l_data)->zt_len,
188 ((struct ziptab *)l->l_data)->zt_name );
190 fprintf( newconf, "\n" );
192 iface = iface->i_next;
193 if ( conf == NULL && iface == NULL ) {
198 if ( conf != NULL ) {
203 if ( rename( newpath, path ) < 0 ) {
204 syslog( LOG_ERR, "rename %s to %s: %m", newpath, path );
211 * Read our config file. If it's not there, return -1. If it is there,
212 * but has syntax errors, exit. Format of the file is as follows:
214 * interface [ -seed ] [ -phase number ] [ -net net-range ]
215 * [ -addr net.node ] [ -zone zonename ]...
217 * le0 -phase 1 -net 7938 -zone Argus
219 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
220 * le0 -phase 1 -net 7938 -zone Argus
222 * Pretty much everything is optional. Anything that is unspecified is
223 * searched for on the network. If -seed is not specified, the
224 * configuration is assumed to be soft, i.e. it can be overridden by
225 * another router. If -seed is specified, atalkd will exit if another
226 * router disagrees. If the phase is unspecified, it defaults to phase
227 * 2 (the default can be overridden on the command line). -addr can
228 * replace -net, if the network in question isn't a range. The default
229 * zone for an interface is the first zone encountered for that
236 struct interface *iface, *niface;
237 char line[ 1024 ], **argv, *p;
242 p = _PATH_ATALKDCONF;
246 if (( conf = fopen( p, "r" )) == NULL ) {
251 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
258 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
259 if (( argv = parseline( line )) == NULL ) {
265 * Check that av[ 0 ] is a valid interface.
266 * Not possible under sysV.
268 strcpy( ifr.ifr_name, argv[ 0 ] );
270 /* for devices that don't support appletalk */
271 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
276 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
281 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
282 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
287 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
288 fprintf(stderr, "%s: multicast may not work properly.\n",
292 /* configure hw multicast for this interface. */
293 if (addmulti(ifr.ifr_name, NULL) < 0) {
294 perror(ifr.ifr_name);
295 fprintf(stderr, "Can't configure multicast.\n");
300 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
301 perror( "newiface" );
305 for ( i = 1; argv[ i ]; i += cc ) {
306 if ( argv[ i ][ 0 ] == '-' ) {
309 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
310 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
311 if ( params[ j ].p_func != NULL ) {
312 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
319 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
320 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
325 for ( iface = interfaces; iface; iface = iface->i_next ) {
326 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
327 ((( niface->i_flags & iface->i_flags &
328 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
329 niface->i_flags == 0 || iface->i_flags == 0 )) {
333 if ( iface ) { /* Already have this interface and phase */
334 fprintf( stderr, "%s already configured!\n", niface->i_name );
338 if ( interfaces == NULL ) {
341 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
343 iface->i_next = niface;
345 niface->i_next = NULL;
355 * Note: we've added lo0 to the interface list previously, so we must
356 * have configured more than one interface...
358 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
360 if ( cc >= IFBASE ) {
375 int router( iface, av )
376 struct interface *iface;
379 /* make sure "-router" and "-dontroute" aren't both on the same line. */
380 if (iface->i_flags & IFACE_DONTROUTE) {
381 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
386 * Check to be sure "-router" is before "-zone".
388 if ( iface->i_czt ) {
389 fprintf( stderr, "Must specify -router before -zone.\n");
393 /* -router also implies -seed */
394 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
399 int dontroute( iface, av )
400 struct interface *iface;
403 /* make sure "-router" and "-dontroute" aren't both on the same line. */
404 if (iface->i_flags & IFACE_RSEED) {
405 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
409 iface->i_flags |= IFACE_DONTROUTE;
414 int seed( iface, av )
415 struct interface *iface;
419 * Check to be sure "-seed" is before "-zone". we keep the old
420 * semantics of just ignoring this in a routerless world.
422 if ( iface->i_czt ) {
423 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
424 iface->i_czt->zt_name);
428 iface->i_flags |= IFACE_SEED;
432 int phase( iface, av )
433 struct interface *iface;
439 if (( pnum = av[ 0 ] ) == NULL ) {
440 fprintf( stderr, "No phase.\n" );
444 switch ( n = atoi( pnum )) {
446 iface->i_flags |= IFACE_PHASE1;
450 iface->i_flags |= IFACE_PHASE2;
454 fprintf( stderr, "No phase %d.\n", n );
461 struct interface *iface;
468 if (( nrange = av[ 0 ] ) == NULL ) {
469 fprintf( stderr, "No network.\n" );
473 if (( stop = strchr( nrange, '-' )) != 0 ) {
476 net = atoi( nrange );
477 if ( net < 0 || net >= 0xffff ) {
478 fprintf( stderr, "Bad network: %d\n", net );
482 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
487 if ( iface->i_flags & IFACE_PHASE1 ) {
489 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
492 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
493 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
494 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
495 net, ntohs( iface->i_caddr.sat_addr.s_net ));
498 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
499 } else if ( iface->i_flags & IFACE_PHASE2 ) {
500 iface->i_rt->rt_firstnet = htons( net );
503 if ( net < 0 || net >= 0xffff ) {
504 fprintf( stderr, "Bad network: %d\n", net );
508 iface->i_rt->rt_lastnet = htons( net );
509 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
510 ( ntohs( iface->i_rt->rt_firstnet ) >
511 ntohs( iface->i_caddr.sat_addr.s_net ) ||
512 ntohs( iface->i_rt->rt_lastnet ) <
513 ntohs( iface->i_caddr.sat_addr.s_net ))) {
514 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
515 ntohs( iface->i_rt->rt_firstnet ),
516 ntohs( iface->i_rt->rt_lastnet ),
517 ntohs( iface->i_caddr.sat_addr.s_net ));
520 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
521 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
524 fprintf( stderr, "Must specify phase before networks.\n" );
530 int addr( iface, av )
531 struct interface *iface;
534 if ( av[ 0 ] == NULL ) {
535 fprintf( stderr, "No address.\n" );
538 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
539 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
544 if ( ntohs( iface->i_rt->rt_firstnet ) >
545 ntohs( iface->i_caddr.sat_addr.s_net ) ||
546 ntohs( iface->i_rt->rt_lastnet ) <
547 ntohs( iface->i_caddr.sat_addr.s_net )) {
548 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
549 ntohs( iface->i_caddr.sat_addr.s_net ),
550 ntohs( iface->i_rt->rt_firstnet ),
551 ntohs( iface->i_rt->rt_lastnet ));
555 if (( iface->i_rt = newrt(iface)) == NULL ) {
559 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
560 iface->i_caddr.sat_addr.s_net;
566 int zone( iface, av )
567 struct interface *iface;
573 if (( zname = av[ 0 ] ) == NULL ) {
574 fprintf( stderr, "No zone.\n" );
579 * Only process "-zone" if this interface has "-seed". We keep our
580 * list of configured zones in the interface structure. Then we can
581 * check that the network has given us good zones.
583 if ( iface->i_flags & IFACE_SEED ) {
584 if ( iface->i_rt == NULL ) {
585 fprintf( stderr, "Must specify net-range before zones.\n" );
588 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
592 if ( iface->i_czt == NULL ) {
595 zt->zt_next = iface->i_czt->zt_next;
596 iface->i_czt->zt_next = zt;
603 * Get the configuration from the kernel. Only called if there's no
608 struct interface *iface, *niface;
610 char **start, **list;
613 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
618 start = list = getifacelist();
619 while (list && *list) {
620 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
623 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
626 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
629 if ((ifr.ifr_flags & IFF_UP) == 0)
632 /* for devices that don't support appletalk */
633 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
636 for ( iface = interfaces; iface; iface = iface->i_next ) {
637 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
641 if ( iface ) { /* Already have this interface name */
647 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
648 fprintf(stderr, "%s: multicast may not work correctly.\n",
652 if (addmulti(ifr.ifr_name, NULL) < 0) {
653 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
657 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
658 perror( "newiface" );
660 freeifacelist(start);
664 * Could try to get the address from the kernel...
667 if ( interfaces == NULL ) {
670 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
672 iface->i_next = niface;
674 niface->i_next = NULL;
676 freeifacelist(start);
682 * Allocate a new interface structure. Centralized here so we can change
683 * the interface structure and have it updated nicely.
686 struct interface *newiface( name )
689 struct interface *niface;
691 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
695 strncpy( niface->i_name, name, sizeof(niface->i_name));
697 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
699 niface->i_addr.sat_family = AF_APPLETALK;
701 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
703 niface->i_caddr.sat_family = AF_APPLETALK;
710 struct interface *iface;
711 char device[ MAXPATHLEN + 1], *p;
714 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
715 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
719 strcpy( device, "/dev/" );
720 strcat( device, iface->i_name );
721 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
722 syslog( LOG_ERR, "plumb: invalid device: %s", device );
728 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
729 syslog( LOG_ERR, "%s: %m", device );
732 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
733 syslog( LOG_ERR, "I_PUSH: %m" );
737 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
738 syslog( LOG_ERR, "IF_UNITSEL: %m" );
743 /* configure multicast. */
744 if (addmulti(iface->i_name, NULL) < 0) {
745 perror(iface->i_name);
746 fprintf(stderr,"Can't configure multicast.\n");
751 syslog( LOG_INFO, "plumbed %s%d", device, ppa );