Home | History | Annotate | Line # | Download | only in ifconfig
af_atalk.c revision 1.11
      1  1.11    dyoung /*	$NetBSD: af_atalk.c,v 1.11 2008/05/07 20:45:01 dyoung Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1983, 1993
      5   1.1   thorpej  *      The Regents of the University of California.  All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   thorpej  *    may be used to endorse or promote products derived from this software
     17   1.1   thorpej  *    without specific prior written permission.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   thorpej  * SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej #ifndef INET_ONLY
     33   1.1   thorpej 
     34   1.1   thorpej #include <sys/cdefs.h>
     35   1.1   thorpej #ifndef lint
     36  1.11    dyoung __RCSID("$NetBSD: af_atalk.c,v 1.11 2008/05/07 20:45:01 dyoung Exp $");
     37   1.1   thorpej #endif /* not lint */
     38   1.1   thorpej 
     39   1.1   thorpej #include <sys/param.h>
     40   1.1   thorpej #include <sys/ioctl.h>
     41   1.1   thorpej #include <sys/socket.h>
     42   1.1   thorpej 
     43   1.1   thorpej #include <net/if.h>
     44   1.1   thorpej 
     45   1.1   thorpej #include <netatalk/at.h>
     46   1.1   thorpej 
     47   1.1   thorpej #include <err.h>
     48   1.1   thorpej #include <errno.h>
     49   1.1   thorpej #include <string.h>
     50   1.1   thorpej #include <stdlib.h>
     51   1.1   thorpej #include <stdio.h>
     52   1.4  christos #include <util.h>
     53   1.1   thorpej 
     54   1.7    dyoung #include "env.h"
     55   1.7    dyoung #include "parse.h"
     56   1.1   thorpej #include "extern.h"
     57   1.1   thorpej #include "af_atalk.h"
     58   1.1   thorpej 
     59   1.7    dyoung struct ifaliasreq at_addreq __attribute__((aligned(4)));
     60   1.1   thorpej static struct netrange at_nr;	/* AppleTalk net range */
     61   1.1   thorpej 
     62   1.7    dyoung struct pinteger phase = PINTEGER_INITIALIZER1(&phase, "phase",
     63   1.7    dyoung     1, 2, 10, setatphase, "phase", &command_root.pb_parser);
     64   1.7    dyoung 
     65   1.7    dyoung struct pstr parse_range = PSTR_INITIALIZER(&range, "range", setatrange, "range",
     66   1.7    dyoung     &command_root.pb_parser);
     67   1.7    dyoung 
     68  1.11    dyoung static const struct kwinst atalkkw[] = {
     69  1.11    dyoung 	  {.k_word = "phase", .k_nextparser = &phase.pi_parser}
     70  1.11    dyoung 	, {.k_word = "range", .k_nextparser = &parse_range.ps_parser}
     71  1.11    dyoung };
     72  1.11    dyoung 
     73  1.11    dyoung struct pkw atalk = PKW_INITIALIZER(&atalk, "AppleTalk", NULL, NULL,
     74  1.11    dyoung     atalkkw, __arraycount(atalkkw), NULL);
     75  1.11    dyoung 
     76   1.1   thorpej void
     77   1.7    dyoung at_getaddr(const struct paddr_prefix *pfx, int which)
     78   1.1   thorpej {
     79   1.1   thorpej 	if (which == MASK)
     80   1.1   thorpej 		errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
     81   1.7    dyoung 
     82   1.7    dyoung 	memcpy(&at_addreq.ifra_addr, &pfx->pfx_addr,
     83   1.7    dyoung 	    MIN(sizeof(at_addreq.ifra_addr), pfx->pfx_addr.sa_len));
     84   1.1   thorpej }
     85   1.1   thorpej 
     86   1.7    dyoung int
     87   1.7    dyoung setatrange(prop_dictionary_t env, prop_dictionary_t xenv)
     88   1.1   thorpej {
     89   1.7    dyoung 	char range[24];
     90   1.1   thorpej 	u_short	first = 123, last = 123;
     91   1.1   thorpej 
     92   1.7    dyoung 	if (getargstr(env, "range", range, sizeof(range)) == -1)
     93   1.7    dyoung 		return -1;
     94   1.7    dyoung 
     95   1.7    dyoung 	if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
     96   1.7    dyoung 	    first == 0 || last == 0 || first > last)
     97   1.1   thorpej 		errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
     98   1.1   thorpej 		    first, last);
     99   1.1   thorpej 	at_nr.nr_firstnet = htons(first);
    100   1.1   thorpej 	at_nr.nr_lastnet = htons(last);
    101   1.7    dyoung 	return 0;
    102   1.1   thorpej }
    103   1.1   thorpej 
    104   1.7    dyoung int
    105   1.7    dyoung setatphase(prop_dictionary_t env, prop_dictionary_t xenv)
    106   1.1   thorpej {
    107  1.10    dyoung 	if (!prop_dictionary_get_uint8(env, "phase", &at_nr.nr_phase)) {
    108   1.7    dyoung 		errno = ENOENT;
    109   1.7    dyoung 		return -1;
    110   1.7    dyoung 	}
    111   1.7    dyoung 	return 0;
    112   1.1   thorpej }
    113   1.1   thorpej 
    114   1.1   thorpej void
    115   1.1   thorpej checkatrange(struct sockaddr *sa)
    116   1.1   thorpej {
    117   1.1   thorpej 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
    118   1.1   thorpej 
    119   1.1   thorpej 	if (at_nr.nr_phase == 0)
    120   1.1   thorpej 		at_nr.nr_phase = 2;	/* Default phase 2 */
    121   1.1   thorpej 	if (at_nr.nr_firstnet == 0)
    122   1.1   thorpej 		at_nr.nr_firstnet =	/* Default range of one */
    123   1.1   thorpej 		at_nr.nr_lastnet = sat->sat_addr.s_net;
    124   1.1   thorpej 	printf("\tatalk %d.%d range %d-%d phase %d\n",
    125   1.5    dyoung 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    126   1.5    dyoung 	    ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
    127   1.5    dyoung 	if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
    128   1.5    dyoung 	    ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
    129   1.1   thorpej 		errx(EXIT_FAILURE, "AppleTalk address is not in range");
    130   1.5    dyoung 	*((struct netrange *)&sat->sat_zero) = at_nr;
    131   1.1   thorpej }
    132   1.1   thorpej 
    133   1.1   thorpej void
    134   1.9    dyoung at_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    135   1.1   thorpej {
    136   1.1   thorpej 	struct sockaddr_at *sat, null_sat;
    137   1.1   thorpej 	struct netrange *nr;
    138   1.7    dyoung 	struct ifreq ifr;
    139   1.7    dyoung 	int af, s;
    140   1.7    dyoung 	const char *ifname;
    141   1.7    dyoung 	unsigned short flags;
    142   1.1   thorpej 
    143   1.7    dyoung 	if ((s = getsock(AF_APPLETALK)) == -1) {
    144   1.2      tron 		if (errno == EAFNOSUPPORT)
    145   1.1   thorpej 			return;
    146   1.8    dyoung 		err(EXIT_FAILURE, "getsock");
    147   1.1   thorpej 	}
    148   1.7    dyoung 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
    149   1.7    dyoung 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
    150   1.7    dyoung 
    151   1.7    dyoung 	if ((af = getaf(env)) == -1)
    152   1.8    dyoung 		af = AF_APPLETALK;
    153   1.8    dyoung 	else if (af != AF_APPLETALK)
    154   1.8    dyoung 		return;
    155   1.7    dyoung 
    156   1.7    dyoung 	memset(&ifr, 0, sizeof(ifr));
    157   1.7    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    158   1.7    dyoung 	ifr.ifr_addr.sa_family = af;
    159   1.1   thorpej 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
    160   1.1   thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    161   1.1   thorpej 			if (!force)
    162   1.1   thorpej 				return;
    163   1.1   thorpej 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    164   1.1   thorpej 		} else
    165   1.1   thorpej 			warn("SIOCGIFADDR");
    166   1.1   thorpej 	}
    167   1.7    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    168   1.1   thorpej 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
    169   1.1   thorpej 
    170   1.1   thorpej 	(void) memset(&null_sat, 0, sizeof(null_sat));
    171   1.1   thorpej 
    172   1.1   thorpej 	nr = (struct netrange *) &sat->sat_zero;
    173   1.1   thorpej 	printf("\tatalk %d.%d range %d-%d phase %d",
    174   1.1   thorpej 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    175   1.1   thorpej 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
    176   1.7    dyoung 
    177   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    178   1.1   thorpej 		if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
    179   1.1   thorpej 			if (errno == EADDRNOTAVAIL)
    180   1.7    dyoung 				memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    181   1.1   thorpej 			else
    182   1.7    dyoung 				warn("SIOCGIFDSTADDR");
    183   1.1   thorpej 		}
    184   1.7    dyoung 		estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    185   1.1   thorpej 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
    186   1.1   thorpej 		if (!sat)
    187   1.1   thorpej 			sat = &null_sat;
    188   1.1   thorpej 		printf("--> %d.%d",
    189   1.1   thorpej 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    190   1.1   thorpej 	}
    191   1.1   thorpej 	if (flags & IFF_BROADCAST) {
    192   1.1   thorpej 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
    193   1.1   thorpej 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
    194   1.1   thorpej 		if (sat)
    195   1.1   thorpej 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
    196   1.1   thorpej 			    sat->sat_addr.s_node);
    197   1.1   thorpej 	}
    198   1.1   thorpej 	printf("\n");
    199   1.1   thorpej }
    200   1.1   thorpej 
    201   1.1   thorpej #endif /* ! INET_ONLY */
    202