2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #include <sys/socket.h>
11 #include <sys/param.h>
13 #include <netatalk/at.h>
14 #include <netatalk/endian.h>
15 #include <atalk/paths.h>
16 #include <atalk/util.h>
25 #include <sys/sockio.h>
26 #include <sys/stropts.h>
29 #include "interface.h"
30 #include "multicast.h"
35 #ifndef IFF_SLAVE /* a little backward compatibility */
39 int router(), dontroute(), seed(), phase(), net(), addr(), zone();
46 { "dontroute", dontroute },
60 static char *argv[ 128 ];
61 static char buf[ 1024 ];
68 memset(argv, 0, sizeof(argv));
73 * This parser should be made more powerful -- it should
74 * handle various escapes, e.g. \" and \031.
76 while ( *p != '\0' ) {
77 while ( *p != '\0' && *p != '"' && isspace( *p )) {
86 while ( *p != '\0' && *p != '"' ) {
91 while ( *p != '\0' && !isspace( *p )) {
98 if ( argv[ 0 ] == '\0' || *argv[ 0 ] == '#' ) {
108 char *path, *p, newpath[ MAXPATHLEN ], line[ 1024 ];
110 FILE *conf, *newconf;
111 struct interface *iface;
116 path = _PATH_ATALKDCONF;
121 /* check if old conf is writable */
122 if ( stat( path, &st ) == 0 ) {
123 if (( st.st_mode & S_IWUSR ) == 0 ) {
124 syslog( LOG_INFO, "%s not writable, won't rewrite", path );
130 if (( p = strrchr( path, '/' )) == NULL ) {
131 strcpy( newpath, _PATH_ATALKDTMP );
133 sprintf( newpath, "%.*s/%s", p - path, path, _PATH_ATALKDTMP );
135 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
136 syslog( LOG_ERR, "%s: %m", newpath );
139 if (( newconf = fdopen( fd, "w" )) == NULL ) {
140 syslog( LOG_ERR, "fdreopen %s: %m", newpath );
144 if (( conf = fopen( path, "r" )) == NULL && cf ) {
145 syslog( LOG_ERR, "%s: %m", path );
149 iface = interfaces->i_next;
151 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
152 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
153 if ( fputs( line, newconf ) == EOF ) {
154 syslog( LOG_ERR, "fputs: %m" );
160 /* write real lines */
162 fprintf( newconf, "%s", iface->i_name );
163 if ( iface->i_flags & IFACE_RSEED ) {
164 fprintf( newconf, " -router" );
165 } else if ( iface->i_flags & IFACE_SEED ) {
166 fprintf( newconf, " -seed" );
168 if ( iface->i_flags & IFACE_DONTROUTE) {
169 fprintf( newconf, " -dontroute");
172 fprintf( newconf, " -phase %d",
173 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
174 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
175 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
176 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
178 fprintf( newconf, " -addr %u.%u",
179 ntohs( iface->i_addr.sat_addr.s_net ),
180 iface->i_addr.sat_addr.s_node );
181 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
182 fprintf( newconf, " -zone \"%.*s\"",
183 ((struct ziptab *)l->l_data)->zt_len,
184 ((struct ziptab *)l->l_data)->zt_name );
186 fprintf( newconf, "\n" );
188 iface = iface->i_next;
189 if ( conf == NULL && iface == NULL ) {
194 if ( conf != NULL ) {
199 if ( rename( newpath, path ) < 0 ) {
200 syslog( LOG_ERR, "rename %s to %s: %m", newpath, path );
207 * Read our config file. If it's not there, return -1. If it is there,
208 * but has syntax errors, exit. Format of the file is as follows:
210 * interface [ -seed ] [ -phase number ] [ -net net-range ]
211 * [ -addr net.node ] [ -zone zonename ]...
213 * le0 -phase 1 -net 7938 -zone Argus
215 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
216 * le0 -phase 1 -net 7938 -zone Argus
218 * Pretty much everything is optional. Anything that is unspecified is
219 * searched for on the network. If -seed is not specified, the
220 * configuration is assumed to be soft, i.e. it can be overridden by
221 * another router. If -seed is specified, atalkd will exit if another
222 * router disagrees. If the phase is unspecified, it defaults to phase
223 * 2 (the default can be overridden on the command line). -addr can
224 * replace -net, if the network in question isn't a range. The default
225 * zone for an interface is the first zone encountered for that
232 struct interface *iface, *niface;
233 char line[ 1024 ], **argv, *p;
238 p = _PATH_ATALKDCONF;
242 if (( conf = fopen( p, "r" )) == NULL ) {
247 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
254 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
255 if (( argv = parseline( line )) == NULL ) {
261 * Check that av[ 0 ] is a valid interface.
262 * Not possible under sysV.
264 strcpy( ifr.ifr_name, argv[ 0 ] );
266 /* for devices that don't support appletalk */
267 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
272 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
277 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
278 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
283 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
284 fprintf(stderr, "%s: multicast may not work properly.\n",
288 /* configure hw multicast for this interface. */
289 if (addmulti(ifr.ifr_name, NULL) < 0) {
290 perror(ifr.ifr_name);
291 fprintf(stderr, "Can't configure multicast.\n");
296 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
297 perror( "newiface" );
301 for ( i = 1; argv[ i ]; i += cc ) {
302 if ( argv[ i ][ 0 ] == '-' ) {
305 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
306 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
307 if ( params[ j ].p_func != NULL ) {
308 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
315 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
316 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
321 for ( iface = interfaces; iface; iface = iface->i_next ) {
322 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
323 ((( niface->i_flags & iface->i_flags &
324 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
325 niface->i_flags == 0 || iface->i_flags == 0 )) {
329 if ( iface ) { /* Already have this interface and phase */
330 fprintf( stderr, "%s already configured!\n", niface->i_name );
334 if ( interfaces == NULL ) {
337 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
339 iface->i_next = niface;
341 niface->i_next = NULL;
351 * Note: we've added lo0 to the interface list previously, so we must
352 * have configured more than one interface...
354 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
356 if ( cc >= IFBASE ) {
372 struct interface *iface;
375 /* make sure "-router" and "-dontroute" aren't both on the same line. */
376 if (iface->i_flags & IFACE_DONTROUTE) {
377 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
382 * Check to be sure "-router" is before "-zone".
384 if ( iface->i_czt ) {
385 fprintf( stderr, "Must specify -router before -zone.\n");
389 /* -router also implies -seed */
390 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
395 dontroute( iface, av )
396 struct interface *iface;
399 /* make sure "-router" and "-dontroute" aren't both on the same line. */
400 if (iface->i_flags & IFACE_RSEED) {
401 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
405 iface->i_flags |= IFACE_DONTROUTE;
411 struct interface *iface;
415 * Check to be sure "-seed" is before "-zone". we keep the old
416 * semantics of just ignoring this in a routerless world.
418 if ( iface->i_czt ) {
419 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
420 iface->i_czt->zt_name);
424 iface->i_flags |= IFACE_SEED;
429 struct interface *iface;
435 if (( pnum = av[ 0 ] ) == NULL ) {
436 fprintf( stderr, "No phase.\n" );
440 switch ( n = atoi( pnum )) {
442 iface->i_flags |= IFACE_PHASE1;
446 iface->i_flags |= IFACE_PHASE2;
450 fprintf( stderr, "No phase %d.\n", n );
457 struct interface *iface;
464 if (( nrange = av[ 0 ] ) == NULL ) {
465 fprintf( stderr, "No network.\n" );
469 if (( stop = strchr( nrange, '-' )) != 0 ) {
472 net = atoi( nrange );
473 if ( net < 0 || net >= 0xffff ) {
474 fprintf( stderr, "Bad network: %d\n" );
478 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
483 if ( iface->i_flags & IFACE_PHASE1 ) {
485 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
488 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
489 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
490 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
491 net, ntohs( iface->i_caddr.sat_addr.s_net ));
494 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
495 } else if ( iface->i_flags & IFACE_PHASE2 ) {
496 iface->i_rt->rt_firstnet = htons( net );
499 if ( net < 0 || net >= 0xffff ) {
500 fprintf( stderr, "Bad network: %d\n" );
504 iface->i_rt->rt_lastnet = htons( net );
505 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
506 ( ntohs( iface->i_rt->rt_firstnet ) >
507 ntohs( iface->i_caddr.sat_addr.s_net ) ||
508 ntohs( iface->i_rt->rt_lastnet ) <
509 ntohs( iface->i_caddr.sat_addr.s_net ))) {
510 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
511 ntohs( iface->i_rt->rt_firstnet ),
512 ntohs( iface->i_rt->rt_lastnet ),
513 ntohs( iface->i_caddr.sat_addr.s_net ));
516 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
517 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
520 fprintf( stderr, "Must specify phase before networks.\n" );
527 struct interface *iface;
530 if ( av[ 0 ] == NULL ) {
531 fprintf( stderr, "No address.\n" );
534 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
535 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
540 if ( ntohs( iface->i_rt->rt_firstnet ) >
541 ntohs( iface->i_caddr.sat_addr.s_net ) ||
542 ntohs( iface->i_rt->rt_lastnet ) <
543 ntohs( iface->i_caddr.sat_addr.s_net )) {
544 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
545 ntohs( iface->i_caddr.sat_addr.s_net ),
546 ntohs( iface->i_rt->rt_firstnet ),
547 ntohs( iface->i_rt->rt_lastnet ));
551 if (( iface->i_rt = newrt(iface)) == NULL ) {
555 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
556 iface->i_caddr.sat_addr.s_net;
563 struct interface *iface;
569 if (( zname = av[ 0 ] ) == NULL ) {
570 fprintf( stderr, "No zone.\n" );
575 * Only process "-zone" if this interface has "-seed". We keep our
576 * list of configured zones in the interface structure. Then we can
577 * check that the network has given us good zones.
579 if ( iface->i_flags & IFACE_SEED ) {
580 if ( iface->i_rt == NULL ) {
581 fprintf( stderr, "Must specify net-range before zones.\n" );
584 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
588 if ( iface->i_czt == NULL ) {
591 zt->zt_next = iface->i_czt->zt_next;
592 iface->i_czt->zt_next = zt;
599 * Get the configuration from the kernel. Only called if there's no
604 struct interface *iface, *niface;
606 char **start, **list;
609 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
614 start = list = getifacelist();
615 while (list && *list) {
616 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
619 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
622 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
625 if ((ifr.ifr_flags & IFF_UP) == 0)
628 /* for devices that don't support appletalk */
629 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
632 for ( iface = interfaces; iface; iface = iface->i_next ) {
633 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
637 if ( iface ) { /* Already have this interface name */
643 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
644 fprintf(stderr, "%s: multicast may not work correctly.\n",
648 if (addmulti(ifr.ifr_name, NULL) < 0) {
649 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
653 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
654 perror( "newiface" );
656 freeifacelist(start);
660 * Could try to get the address from the kernel...
663 if ( interfaces == NULL ) {
666 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
668 iface->i_next = niface;
670 niface->i_next = NULL;
672 freeifacelist(start);
678 * Allocate a new interface structure. Centralized here so we can change
679 * the interface structure and have it updated nicely.
685 struct interface *niface;
687 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
691 strncpy( niface->i_name, name, sizeof(niface->i_name));
693 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
695 niface->i_addr.sat_family = AF_APPLETALK;
697 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
699 niface->i_caddr.sat_family = AF_APPLETALK;
707 struct interface *iface;
708 char device[ MAXPATHLEN + 1], *p;
711 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
712 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
716 strcpy( device, "/dev/" );
717 strcat( device, iface->i_name );
718 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
719 syslog( LOG_ERR, "plumb: invalid device: %s", device );
725 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
726 syslog( LOG_ERR, "%s: %m", device );
729 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
730 syslog( LOG_ERR, "I_PUSH: %m" );
734 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
735 syslog( LOG_ERR, "IF_UNITSEL: %m" );
740 /* configure multicast. */
741 if (addmulti(iface->i_name, NULL) < 0) {
742 perror(iface->i_name);
743 fprintf(stderr,"Can't configure multicast.\n");
748 syslog( LOG_INFO, "plumbed %s%d", device, ppa );