Home | History | Annotate | Line # | Download | only in ifconfig
af_atalk.c revision 1.12
      1  1.12    dyoung /*	$NetBSD: af_atalk.c,v 1.12 2008/05/11 23:28:40 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.12    dyoung __RCSID("$NetBSD: af_atalk.c,v 1.12 2008/05/11 23:28:40 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.12    dyoung static int
     87  1.12    dyoung setatrange_impl(prop_dictionary_t env, prop_dictionary_t xenv,
     88  1.12    dyoung     struct netrange *nr)
     89   1.1   thorpej {
     90   1.7    dyoung 	char range[24];
     91   1.1   thorpej 	u_short	first = 123, last = 123;
     92   1.1   thorpej 
     93   1.7    dyoung 	if (getargstr(env, "range", range, sizeof(range)) == -1)
     94   1.7    dyoung 		return -1;
     95   1.7    dyoung 
     96   1.7    dyoung 	if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
     97   1.7    dyoung 	    first == 0 || last == 0 || first > last)
     98   1.1   thorpej 		errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
     99   1.1   thorpej 		    first, last);
    100  1.12    dyoung 	nr->nr_firstnet = htons(first);
    101  1.12    dyoung 	nr->nr_lastnet = htons(last);
    102   1.7    dyoung 	return 0;
    103   1.1   thorpej }
    104   1.1   thorpej 
    105   1.7    dyoung int
    106  1.12    dyoung setatrange(prop_dictionary_t env, prop_dictionary_t xenv)
    107  1.12    dyoung {
    108  1.12    dyoung 	return setatrange_impl(env, xenv, &at_nr);
    109  1.12    dyoung }
    110  1.12    dyoung 
    111  1.12    dyoung void
    112  1.12    dyoung at_commit_address(prop_dictionary_t env, prop_dictionary_t xenv)
    113  1.12    dyoung {
    114  1.12    dyoung }
    115  1.12    dyoung 
    116  1.12    dyoung static int
    117  1.12    dyoung setatphase_impl(prop_dictionary_t env, prop_dictionary_t xenv,
    118  1.12    dyoung     struct netrange *nr)
    119   1.1   thorpej {
    120  1.12    dyoung 	if (!prop_dictionary_get_uint8(env, "phase", &nr->nr_phase)) {
    121   1.7    dyoung 		errno = ENOENT;
    122   1.7    dyoung 		return -1;
    123   1.7    dyoung 	}
    124   1.7    dyoung 	return 0;
    125   1.1   thorpej }
    126   1.1   thorpej 
    127  1.12    dyoung int
    128  1.12    dyoung setatphase(prop_dictionary_t env, prop_dictionary_t xenv)
    129  1.12    dyoung {
    130  1.12    dyoung 	return setatphase_impl(env, xenv, &at_nr);
    131  1.12    dyoung }
    132  1.12    dyoung 
    133   1.1   thorpej void
    134   1.1   thorpej checkatrange(struct sockaddr *sa)
    135   1.1   thorpej {
    136   1.1   thorpej 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
    137   1.1   thorpej 
    138   1.1   thorpej 	if (at_nr.nr_phase == 0)
    139   1.1   thorpej 		at_nr.nr_phase = 2;	/* Default phase 2 */
    140   1.1   thorpej 	if (at_nr.nr_firstnet == 0)
    141   1.1   thorpej 		at_nr.nr_firstnet =	/* Default range of one */
    142   1.1   thorpej 		at_nr.nr_lastnet = sat->sat_addr.s_net;
    143   1.1   thorpej 	printf("\tatalk %d.%d range %d-%d phase %d\n",
    144   1.5    dyoung 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    145   1.5    dyoung 	    ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
    146   1.5    dyoung 	if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
    147   1.5    dyoung 	    ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
    148   1.1   thorpej 		errx(EXIT_FAILURE, "AppleTalk address is not in range");
    149   1.5    dyoung 	*((struct netrange *)&sat->sat_zero) = at_nr;
    150   1.1   thorpej }
    151   1.1   thorpej 
    152   1.1   thorpej void
    153   1.9    dyoung at_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    154   1.1   thorpej {
    155  1.12    dyoung 	struct sockaddr_at *sat;
    156   1.1   thorpej 	struct netrange *nr;
    157   1.7    dyoung 	struct ifreq ifr;
    158   1.7    dyoung 	int af, s;
    159   1.7    dyoung 	const char *ifname;
    160   1.7    dyoung 	unsigned short flags;
    161   1.1   thorpej 
    162   1.7    dyoung 	if ((s = getsock(AF_APPLETALK)) == -1) {
    163   1.2      tron 		if (errno == EAFNOSUPPORT)
    164   1.1   thorpej 			return;
    165   1.8    dyoung 		err(EXIT_FAILURE, "getsock");
    166   1.1   thorpej 	}
    167   1.7    dyoung 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
    168   1.7    dyoung 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
    169   1.7    dyoung 
    170   1.7    dyoung 	if ((af = getaf(env)) == -1)
    171   1.8    dyoung 		af = AF_APPLETALK;
    172   1.8    dyoung 	else if (af != AF_APPLETALK)
    173   1.8    dyoung 		return;
    174   1.7    dyoung 
    175   1.7    dyoung 	memset(&ifr, 0, sizeof(ifr));
    176   1.7    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    177   1.7    dyoung 	ifr.ifr_addr.sa_family = af;
    178   1.1   thorpej 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
    179   1.1   thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    180   1.1   thorpej 			if (!force)
    181   1.1   thorpej 				return;
    182   1.1   thorpej 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    183   1.1   thorpej 		} else
    184   1.1   thorpej 			warn("SIOCGIFADDR");
    185   1.1   thorpej 	}
    186   1.1   thorpej 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
    187   1.1   thorpej 
    188  1.12    dyoung 	nr = (struct netrange *)&sat->sat_zero;
    189   1.1   thorpej 	printf("\tatalk %d.%d range %d-%d phase %d",
    190   1.1   thorpej 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    191   1.1   thorpej 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
    192   1.7    dyoung 
    193   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    194  1.12    dyoung 		estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    195   1.1   thorpej 		if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
    196   1.1   thorpej 			if (errno == EADDRNOTAVAIL)
    197   1.7    dyoung 				memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    198   1.1   thorpej 			else
    199   1.7    dyoung 				warn("SIOCGIFDSTADDR");
    200   1.1   thorpej 		}
    201   1.1   thorpej 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
    202   1.1   thorpej 		printf("--> %d.%d",
    203   1.1   thorpej 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    204   1.1   thorpej 	}
    205   1.1   thorpej 	if (flags & IFF_BROADCAST) {
    206   1.1   thorpej 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
    207   1.1   thorpej 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
    208  1.12    dyoung 		printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
    209  1.12    dyoung 		    sat->sat_addr.s_node);
    210   1.1   thorpej 	}
    211   1.1   thorpej 	printf("\n");
    212   1.1   thorpej }
    213   1.1   thorpej 
    214   1.1   thorpej #endif /* ! INET_ONLY */
    215