Home | History | Annotate | Line # | Download | only in ifconfig
af_atalk.c revision 1.15
      1  1.15    dyoung /*	$NetBSD: af_atalk.c,v 1.15 2008/08/01 22:44:17 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.15    dyoung __RCSID("$NetBSD: af_atalk.c,v 1.15 2008/08/01 22:44:17 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.15    dyoung #include <stddef.h>
     49   1.1   thorpej #include <stdlib.h>
     50   1.1   thorpej #include <stdio.h>
     51   1.4  christos #include <util.h>
     52   1.1   thorpej 
     53   1.7    dyoung #include "env.h"
     54  1.13    dyoung #include "af_inetany.h"
     55   1.7    dyoung #include "parse.h"
     56   1.1   thorpej #include "extern.h"
     57   1.1   thorpej 
     58  1.13    dyoung #ifndef satocsat
     59  1.13    dyoung #define	satocsat(__sa) ((const struct sockaddr_at *)(__sa))
     60  1.13    dyoung #endif
     61  1.13    dyoung 
     62  1.13    dyoung static void at_status(prop_dictionary_t, prop_dictionary_t, bool);
     63  1.13    dyoung static void at_commit_address(prop_dictionary_t, prop_dictionary_t);
     64  1.13    dyoung 
     65  1.13    dyoung static void at_constructor(void) __attribute__((constructor));
     66  1.13    dyoung 
     67  1.13    dyoung static struct afswtch ataf = {
     68  1.13    dyoung 	.af_name = "atalk", .af_af = AF_APPLETALK, .af_status = at_status,
     69  1.13    dyoung 	.af_addr_commit = at_commit_address
     70  1.13    dyoung };
     71   1.7    dyoung struct pinteger phase = PINTEGER_INITIALIZER1(&phase, "phase",
     72  1.13    dyoung     1, 2, 10, NULL, "phase", &command_root.pb_parser);
     73   1.7    dyoung 
     74  1.13    dyoung struct pstr parse_range = PSTR_INITIALIZER(&range, "range", NULL, "range",
     75   1.7    dyoung     &command_root.pb_parser);
     76   1.7    dyoung 
     77  1.11    dyoung static const struct kwinst atalkkw[] = {
     78  1.11    dyoung 	  {.k_word = "phase", .k_nextparser = &phase.pi_parser}
     79  1.11    dyoung 	, {.k_word = "range", .k_nextparser = &parse_range.ps_parser}
     80  1.11    dyoung };
     81  1.11    dyoung 
     82  1.11    dyoung struct pkw atalk = PKW_INITIALIZER(&atalk, "AppleTalk", NULL, NULL,
     83  1.11    dyoung     atalkkw, __arraycount(atalkkw), NULL);
     84  1.11    dyoung 
     85  1.13    dyoung static cmdloop_branch_t branch;
     86   1.1   thorpej 
     87  1.13    dyoung static void
     88  1.14    dyoung setatrange_impl(prop_dictionary_t env, prop_dictionary_t oenv,
     89  1.12    dyoung     struct netrange *nr)
     90   1.1   thorpej {
     91   1.7    dyoung 	char range[24];
     92   1.1   thorpej 	u_short	first = 123, last = 123;
     93   1.1   thorpej 
     94   1.7    dyoung 	if (getargstr(env, "range", range, sizeof(range)) == -1)
     95  1.13    dyoung 		return;
     96   1.7    dyoung 
     97   1.7    dyoung 	if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
     98   1.7    dyoung 	    first == 0 || last == 0 || first > last)
     99   1.1   thorpej 		errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
    100   1.1   thorpej 		    first, last);
    101  1.12    dyoung 	nr->nr_firstnet = htons(first);
    102  1.12    dyoung 	nr->nr_lastnet = htons(last);
    103   1.1   thorpej }
    104   1.1   thorpej 
    105  1.13    dyoung static void
    106  1.13    dyoung at_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    107  1.12    dyoung {
    108  1.13    dyoung 	struct ifreq ifr;
    109  1.13    dyoung 	struct ifaliasreq ifra __attribute__((aligned(4)));
    110  1.13    dyoung 	struct afparam atparam = {
    111  1.13    dyoung 		  .req = BUFPARAM(ifra)
    112  1.13    dyoung 		, .dgreq = BUFPARAM(ifr)
    113  1.13    dyoung 		, .name = {
    114  1.13    dyoung 			  {.buf = ifr.ifr_name,
    115  1.13    dyoung 			   .buflen = sizeof(ifr.ifr_name)}
    116  1.13    dyoung 			, {.buf = ifra.ifra_name,
    117  1.13    dyoung 			   .buflen = sizeof(ifra.ifra_name)}
    118  1.13    dyoung 		  }
    119  1.13    dyoung 		, .dgaddr = BUFPARAM(ifr.ifr_addr)
    120  1.13    dyoung 		, .addr = BUFPARAM(ifra.ifra_addr)
    121  1.13    dyoung 		, .dst = BUFPARAM(ifra.ifra_dstaddr)
    122  1.13    dyoung 		, .brd = BUFPARAM(ifra.ifra_broadaddr)
    123  1.13    dyoung 		, .mask = BUFPARAM(ifra.ifra_mask)
    124  1.13    dyoung 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
    125  1.13    dyoung 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR)
    126  1.13    dyoung 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
    127  1.13    dyoung 		, .defmask = {.buf = NULL, .buflen = 0}
    128  1.13    dyoung 	};
    129  1.13    dyoung 	struct netrange nr = {.nr_phase = 2};	/* AppleTalk net range */
    130  1.15    dyoung 	prop_data_t d, d0;
    131  1.15    dyoung 	prop_dictionary_t ienv;
    132  1.15    dyoung 	struct paddr_prefix *addr;
    133  1.15    dyoung 	struct sockaddr_at *sat;
    134  1.13    dyoung 
    135  1.15    dyoung 	if ((d0 = (prop_data_t)prop_dictionary_get(env, "address")) == NULL)
    136  1.13    dyoung 		return;
    137  1.13    dyoung 
    138  1.15    dyoung 	addr = prop_data_data(d0);
    139  1.13    dyoung 
    140  1.15    dyoung 	sat = (struct sockaddr_at *)&addr->pfx_addr;
    141  1.12    dyoung 
    142  1.13    dyoung 	(void)prop_dictionary_get_uint8(env, "phase", &nr.nr_phase);
    143  1.13    dyoung 	/* Default range of one */
    144  1.15    dyoung 	nr.nr_firstnet = nr.nr_lastnet = sat->sat_addr.s_net;
    145  1.13    dyoung 	setatrange_impl(env, oenv, &nr);
    146  1.12    dyoung 
    147  1.15    dyoung 	if (ntohs(nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
    148  1.15    dyoung 	    ntohs(nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
    149  1.13    dyoung 		errx(EXIT_FAILURE, "AppleTalk address is not in range");
    150  1.15    dyoung 	*((struct netrange *)&sat->sat_zero) = nr;
    151  1.15    dyoung 
    152  1.15    dyoung 	/* Copy the new address to a temporary input environment */
    153  1.15    dyoung 
    154  1.15    dyoung 	d = prop_data_create_data_nocopy(addr, paddr_prefix_size(addr));
    155  1.15    dyoung 	ienv = prop_dictionary_copy_mutable(env);
    156  1.15    dyoung 
    157  1.15    dyoung 	if (d == NULL)
    158  1.15    dyoung 		err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
    159  1.15    dyoung 	if (ienv == NULL)
    160  1.15    dyoung 		err(EXIT_FAILURE, "%s: prop_dictionary_copy_mutable", __func__);
    161  1.15    dyoung 
    162  1.15    dyoung 	if (!prop_dictionary_set(ienv, "address", (prop_object_t)d))
    163  1.15    dyoung 		err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
    164  1.15    dyoung 
    165  1.15    dyoung 	/* copy to output environment for good measure */
    166  1.15    dyoung 	if (!prop_dictionary_set(oenv, "address", (prop_object_t)d))
    167  1.15    dyoung 		err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
    168  1.15    dyoung 
    169  1.15    dyoung 	prop_object_release((prop_object_t)d);
    170   1.1   thorpej 
    171  1.13    dyoung 	memset(&ifr, 0, sizeof(ifr));
    172  1.13    dyoung 	memset(&ifra, 0, sizeof(ifra));
    173  1.15    dyoung 	commit_address(ienv, oenv, &atparam);
    174  1.15    dyoung 
    175  1.15    dyoung 	/* release temporary input environment */
    176  1.15    dyoung 	prop_object_release((prop_object_t)ienv);
    177  1.12    dyoung }
    178  1.12    dyoung 
    179  1.13    dyoung static void
    180  1.13    dyoung sat_print(const char *prefix, const struct sockaddr *sa)
    181   1.1   thorpej {
    182  1.13    dyoung 	const struct sockaddr_at *sat = satocsat(sa);
    183   1.1   thorpej 
    184  1.13    dyoung 	printf("%s%d.%d", prefix, ntohs(sat->sat_addr.s_net),
    185  1.13    dyoung 	    sat->sat_addr.s_node);
    186   1.1   thorpej }
    187   1.1   thorpej 
    188  1.13    dyoung static void
    189   1.9    dyoung at_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    190   1.1   thorpej {
    191  1.12    dyoung 	struct sockaddr_at *sat;
    192   1.1   thorpej 	struct netrange *nr;
    193   1.7    dyoung 	struct ifreq ifr;
    194  1.13    dyoung 	int s;
    195   1.7    dyoung 	const char *ifname;
    196   1.7    dyoung 	unsigned short flags;
    197   1.1   thorpej 
    198   1.7    dyoung 	if ((s = getsock(AF_APPLETALK)) == -1) {
    199   1.2      tron 		if (errno == EAFNOSUPPORT)
    200   1.1   thorpej 			return;
    201   1.8    dyoung 		err(EXIT_FAILURE, "getsock");
    202   1.1   thorpej 	}
    203   1.7    dyoung 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
    204   1.7    dyoung 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
    205   1.7    dyoung 
    206   1.7    dyoung 	memset(&ifr, 0, sizeof(ifr));
    207   1.7    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    208  1.13    dyoung 	ifr.ifr_addr.sa_family = AF_APPLETALK;
    209  1.13    dyoung 	if (ioctl(s, SIOCGIFADDR, &ifr) != -1)
    210  1.13    dyoung 		;
    211  1.13    dyoung 	else if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    212  1.13    dyoung 		if (!force)
    213  1.13    dyoung 			return;
    214  1.13    dyoung 		memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    215  1.13    dyoung 	} else
    216  1.13    dyoung 		warn("SIOCGIFADDR");
    217   1.1   thorpej 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
    218   1.1   thorpej 
    219  1.12    dyoung 	nr = (struct netrange *)&sat->sat_zero;
    220  1.13    dyoung 	sat_print("\tatalk ", &ifr.ifr_addr);
    221  1.13    dyoung 	printf(" range %d-%d phase %d",
    222   1.1   thorpej 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
    223   1.7    dyoung 
    224   1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    225  1.12    dyoung 		estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    226   1.1   thorpej 		if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
    227   1.1   thorpej 			if (errno == EADDRNOTAVAIL)
    228   1.7    dyoung 				memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    229   1.1   thorpej 			else
    230   1.7    dyoung 				warn("SIOCGIFDSTADDR");
    231   1.1   thorpej 		}
    232  1.13    dyoung 		sat_print(" --> ", &ifr.ifr_dstaddr);
    233   1.1   thorpej 	}
    234   1.1   thorpej 	if (flags & IFF_BROADCAST) {
    235   1.1   thorpej 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
    236  1.13    dyoung 		sat_print(" broadcast ", &ifr.ifr_broadaddr);
    237   1.1   thorpej 	}
    238   1.1   thorpej 	printf("\n");
    239   1.1   thorpej }
    240   1.1   thorpej 
    241  1.13    dyoung static void
    242  1.13    dyoung at_constructor(void)
    243  1.13    dyoung {
    244  1.13    dyoung 	register_family(&ataf);
    245  1.13    dyoung 	cmdloop_branch_init(&branch, &atalk.pk_parser);
    246  1.13    dyoung 	register_cmdloop_branch(&branch);
    247  1.13    dyoung }
    248