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