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