Home | History | Annotate | Line # | Download | only in ifconfig
af_atalk.c revision 1.14
      1  1.14    dyoung /*	$NetBSD: af_atalk.c,v 1.14 2008/07/15 21:27:58 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 #include <sys/cdefs.h>
     33   1.1   thorpej #ifndef lint
     34  1.14    dyoung __RCSID("$NetBSD: af_atalk.c,v 1.14 2008/07/15 21:27:58 dyoung Exp $");
     35   1.1   thorpej #endif /* not lint */
     36   1.1   thorpej 
     37   1.1   thorpej #include <sys/param.h>
     38   1.1   thorpej #include <sys/ioctl.h>
     39   1.1   thorpej #include <sys/socket.h>
     40   1.1   thorpej 
     41   1.1   thorpej #include <net/if.h>
     42   1.1   thorpej 
     43   1.1   thorpej #include <netatalk/at.h>
     44   1.1   thorpej 
     45   1.1   thorpej #include <err.h>
     46   1.1   thorpej #include <errno.h>
     47   1.1   thorpej #include <string.h>
     48   1.1   thorpej #include <stdlib.h>
     49   1.1   thorpej #include <stdio.h>
     50   1.4  christos #include <util.h>
     51   1.1   thorpej 
     52   1.7    dyoung #include "env.h"
     53  1.13    dyoung #include "af_inetany.h"
     54   1.7    dyoung #include "parse.h"
     55   1.1   thorpej #include "extern.h"
     56   1.1   thorpej 
     57  1.13    dyoung #ifndef satocsat
     58  1.13    dyoung #define	satocsat(__sa) ((const struct sockaddr_at *)(__sa))
     59  1.13    dyoung #endif
     60  1.13    dyoung 
     61  1.13    dyoung static void at_status(prop_dictionary_t, prop_dictionary_t, bool);
     62  1.13    dyoung static void at_commit_address(prop_dictionary_t, prop_dictionary_t);
     63  1.13    dyoung 
     64  1.13    dyoung static void at_constructor(void) __attribute__((constructor));
     65  1.13    dyoung 
     66  1.13    dyoung static struct afswtch ataf = {
     67  1.13    dyoung 	.af_name = "atalk", .af_af = AF_APPLETALK, .af_status = at_status,
     68  1.13    dyoung 	.af_addr_commit = at_commit_address
     69  1.13    dyoung };
     70   1.7    dyoung struct pinteger phase = PINTEGER_INITIALIZER1(&phase, "phase",
     71  1.13    dyoung     1, 2, 10, NULL, "phase", &command_root.pb_parser);
     72   1.7    dyoung 
     73  1.13    dyoung struct pstr parse_range = PSTR_INITIALIZER(&range, "range", NULL, "range",
     74   1.7    dyoung     &command_root.pb_parser);
     75   1.7    dyoung 
     76  1.11    dyoung static const struct kwinst atalkkw[] = {
     77  1.11    dyoung 	  {.k_word = "phase", .k_nextparser = &phase.pi_parser}
     78  1.11    dyoung 	, {.k_word = "range", .k_nextparser = &parse_range.ps_parser}
     79  1.11    dyoung };
     80  1.11    dyoung 
     81  1.11    dyoung struct pkw atalk = PKW_INITIALIZER(&atalk, "AppleTalk", NULL, NULL,
     82  1.11    dyoung     atalkkw, __arraycount(atalkkw), NULL);
     83  1.11    dyoung 
     84  1.13    dyoung static cmdloop_branch_t branch;
     85   1.1   thorpej 
     86  1.13    dyoung static void
     87  1.14    dyoung setatrange_impl(prop_dictionary_t env, prop_dictionary_t oenv,
     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.13    dyoung 		return;
     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.1   thorpej }
    103   1.1   thorpej 
    104  1.13    dyoung static void
    105  1.13    dyoung at_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    106  1.12    dyoung {
    107  1.13    dyoung 	struct ifreq ifr;
    108  1.13    dyoung 	struct ifaliasreq ifra __attribute__((aligned(4)));
    109  1.13    dyoung 	struct afparam atparam = {
    110  1.13    dyoung 		  .req = BUFPARAM(ifra)
    111  1.13    dyoung 		, .dgreq = BUFPARAM(ifr)
    112  1.13    dyoung 		, .name = {
    113  1.13    dyoung 			  {.buf = ifr.ifr_name,
    114  1.13    dyoung 			   .buflen = sizeof(ifr.ifr_name)}
    115  1.13    dyoung 			, {.buf = ifra.ifra_name,
    116  1.13    dyoung 			   .buflen = sizeof(ifra.ifra_name)}
    117  1.13    dyoung 		  }
    118  1.13    dyoung 		, .dgaddr = BUFPARAM(ifr.ifr_addr)
    119  1.13    dyoung 		, .addr = BUFPARAM(ifra.ifra_addr)
    120  1.13    dyoung 		, .dst = BUFPARAM(ifra.ifra_dstaddr)
    121  1.13    dyoung 		, .brd = BUFPARAM(ifra.ifra_broadaddr)
    122  1.13    dyoung 		, .mask = BUFPARAM(ifra.ifra_mask)
    123  1.13    dyoung 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
    124  1.13    dyoung 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR)
    125  1.13    dyoung 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
    126  1.13    dyoung 		, .defmask = {.buf = NULL, .buflen = 0}
    127  1.13    dyoung 	};
    128  1.13    dyoung 	struct netrange nr = {.nr_phase = 2};	/* AppleTalk net range */
    129  1.13    dyoung 	prop_data_t d;
    130  1.13    dyoung 	const struct paddr_prefix *addr;
    131  1.13    dyoung 	struct sockaddr_at sat;
    132  1.13    dyoung 
    133  1.13    dyoung 	if ((d = (prop_data_t)prop_dictionary_get(env, "address")) == NULL)
    134  1.13    dyoung 		return;
    135  1.13    dyoung 
    136  1.13    dyoung 	addr = prop_data_data_nocopy(d);
    137  1.13    dyoung 
    138  1.13    dyoung 	memcpy(&sat, &addr->pfx_addr, MIN(sizeof(sat), addr->pfx_addr.sa_len));
    139  1.12    dyoung 
    140  1.13    dyoung 	(void)prop_dictionary_get_uint8(env, "phase", &nr.nr_phase);
    141  1.13    dyoung 	/* Default range of one */
    142  1.13    dyoung 	nr.nr_firstnet = nr.nr_lastnet = sat.sat_addr.s_net;
    143  1.13    dyoung 	setatrange_impl(env, oenv, &nr);
    144  1.12    dyoung 
    145  1.13    dyoung 	if (ntohs(nr.nr_firstnet) > ntohs(sat.sat_addr.s_net) ||
    146  1.13    dyoung 	    ntohs(nr.nr_lastnet) < ntohs(sat.sat_addr.s_net))
    147  1.13    dyoung 		errx(EXIT_FAILURE, "AppleTalk address is not in range");
    148  1.13    dyoung 	*((struct netrange *)&sat.sat_zero) = nr;
    149   1.1   thorpej 
    150  1.13    dyoung 	memset(&ifr, 0, sizeof(ifr));
    151  1.13    dyoung 	memset(&ifra, 0, sizeof(ifra));
    152  1.13    dyoung 	commit_address(env, oenv, &atparam);
    153  1.12    dyoung }
    154  1.12    dyoung 
    155  1.13    dyoung static void
    156  1.13    dyoung sat_print(const char *prefix, const struct sockaddr *sa)
    157   1.1   thorpej {
    158  1.13    dyoung 	const struct sockaddr_at *sat = satocsat(sa);
    159   1.1   thorpej 
    160  1.13    dyoung 	printf("%s%d.%d", prefix, ntohs(sat->sat_addr.s_net),
    161  1.13    dyoung 	    sat->sat_addr.s_node);
    162   1.1   thorpej }
    163   1.1   thorpej 
    164  1.13    dyoung static void
    165   1.9    dyoung at_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    166   1.1   thorpej {
    167  1.12    dyoung 	struct sockaddr_at *sat;
    168   1.1   thorpej 	struct netrange *nr;
    169   1.7    dyoung 	struct ifreq ifr;
    170  1.13    dyoung 	int s;
    171   1.7    dyoung 	const char *ifname;
    172   1.7    dyoung 	unsigned short flags;
    173   1.1   thorpej 
    174   1.7    dyoung 	if ((s = getsock(AF_APPLETALK)) == -1) {
    175   1.2      tron 		if (errno == EAFNOSUPPORT)
    176   1.1   thorpej 			return;
    177   1.8    dyoung 		err(EXIT_FAILURE, "getsock");
    178   1.1   thorpej 	}
    179   1.7    dyoung 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
    180   1.7    dyoung 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
    181   1.7    dyoung 
    182   1.7    dyoung 	memset(&ifr, 0, sizeof(ifr));
    183   1.7    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    184  1.13    dyoung 	ifr.ifr_addr.sa_family = AF_APPLETALK;
    185  1.13    dyoung 	if (ioctl(s, SIOCGIFADDR, &ifr) != -1)
    186  1.13    dyoung 		;
    187  1.13    dyoung 	else if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    188  1.13    dyoung 		if (!force)
    189  1.13    dyoung 			return;
    190  1.13    dyoung 		memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    191  1.13    dyoung 	} else
    192  1.13    dyoung 		warn("SIOCGIFADDR");
    193   1.1   thorpej 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
    194   1.1   thorpej 
    195  1.12    dyoung 	nr = (struct netrange *)&sat->sat_zero;
    196  1.13    dyoung 	sat_print("\tatalk ", &ifr.ifr_addr);
    197  1.13    dyoung 	printf(" range %d-%d phase %d",
    198   1.1   thorpej 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
    199   1.7    dyoung 
    200   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    201  1.12    dyoung 		estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    202   1.1   thorpej 		if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
    203   1.1   thorpej 			if (errno == EADDRNOTAVAIL)
    204   1.7    dyoung 				memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    205   1.1   thorpej 			else
    206   1.7    dyoung 				warn("SIOCGIFDSTADDR");
    207   1.1   thorpej 		}
    208  1.13    dyoung 		sat_print(" --> ", &ifr.ifr_dstaddr);
    209   1.1   thorpej 	}
    210   1.1   thorpej 	if (flags & IFF_BROADCAST) {
    211   1.1   thorpej 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
    212  1.13    dyoung 		sat_print(" broadcast ", &ifr.ifr_broadaddr);
    213   1.1   thorpej 	}
    214   1.1   thorpej 	printf("\n");
    215   1.1   thorpej }
    216   1.1   thorpej 
    217  1.13    dyoung static void
    218  1.13    dyoung at_constructor(void)
    219  1.13    dyoung {
    220  1.13    dyoung 	register_family(&ataf);
    221  1.13    dyoung 	cmdloop_branch_init(&branch, &atalk.pk_parser);
    222  1.13    dyoung 	register_cmdloop_branch(&branch);
    223  1.13    dyoung }
    224