af_atalk.c revision 1.8 1 1.8 dyoung /* $NetBSD: af_atalk.c,v 1.8 2008/05/06 15:02:49 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.8 dyoung __RCSID("$NetBSD: af_atalk.c,v 1.8 2008/05/06 15:02:49 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.1 thorpej void
69 1.7 dyoung at_getaddr(const struct paddr_prefix *pfx, int which)
70 1.1 thorpej {
71 1.1 thorpej if (which == MASK)
72 1.1 thorpej errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
73 1.7 dyoung
74 1.7 dyoung memcpy(&at_addreq.ifra_addr, &pfx->pfx_addr,
75 1.7 dyoung MIN(sizeof(at_addreq.ifra_addr), pfx->pfx_addr.sa_len));
76 1.1 thorpej }
77 1.1 thorpej
78 1.7 dyoung int
79 1.7 dyoung setatrange(prop_dictionary_t env, prop_dictionary_t xenv)
80 1.1 thorpej {
81 1.7 dyoung char range[24];
82 1.1 thorpej u_short first = 123, last = 123;
83 1.1 thorpej
84 1.7 dyoung if (getargstr(env, "range", range, sizeof(range)) == -1)
85 1.7 dyoung return -1;
86 1.7 dyoung
87 1.7 dyoung if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
88 1.7 dyoung first == 0 || last == 0 || first > last)
89 1.1 thorpej errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
90 1.1 thorpej first, last);
91 1.1 thorpej at_nr.nr_firstnet = htons(first);
92 1.1 thorpej at_nr.nr_lastnet = htons(last);
93 1.7 dyoung return 0;
94 1.1 thorpej }
95 1.1 thorpej
96 1.7 dyoung int
97 1.7 dyoung setatphase(prop_dictionary_t env, prop_dictionary_t xenv)
98 1.1 thorpej {
99 1.7 dyoung prop_number_t num;
100 1.7 dyoung
101 1.7 dyoung num = (prop_number_t)prop_dictionary_get(env, "phase");
102 1.7 dyoung if (num == NULL) {
103 1.7 dyoung errno = ENOENT;
104 1.7 dyoung return -1;
105 1.7 dyoung }
106 1.7 dyoung at_nr.nr_phase = (uint8_t)prop_number_integer_value(num);
107 1.7 dyoung return 0;
108 1.1 thorpej }
109 1.1 thorpej
110 1.1 thorpej void
111 1.1 thorpej checkatrange(struct sockaddr *sa)
112 1.1 thorpej {
113 1.1 thorpej struct sockaddr_at *sat = (struct sockaddr_at *) sa;
114 1.1 thorpej
115 1.1 thorpej if (at_nr.nr_phase == 0)
116 1.1 thorpej at_nr.nr_phase = 2; /* Default phase 2 */
117 1.1 thorpej if (at_nr.nr_firstnet == 0)
118 1.1 thorpej at_nr.nr_firstnet = /* Default range of one */
119 1.1 thorpej at_nr.nr_lastnet = sat->sat_addr.s_net;
120 1.1 thorpej printf("\tatalk %d.%d range %d-%d phase %d\n",
121 1.5 dyoung ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
122 1.5 dyoung ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
123 1.5 dyoung if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
124 1.5 dyoung ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
125 1.1 thorpej errx(EXIT_FAILURE, "AppleTalk address is not in range");
126 1.5 dyoung *((struct netrange *)&sat->sat_zero) = at_nr;
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej void
130 1.7 dyoung at_status(prop_dictionary_t env, prop_dictionary_t oenv, int force)
131 1.1 thorpej {
132 1.1 thorpej struct sockaddr_at *sat, null_sat;
133 1.1 thorpej struct netrange *nr;
134 1.7 dyoung struct ifreq ifr;
135 1.7 dyoung int af, s;
136 1.7 dyoung const char *ifname;
137 1.7 dyoung unsigned short flags;
138 1.1 thorpej
139 1.7 dyoung if ((s = getsock(AF_APPLETALK)) == -1) {
140 1.2 tron if (errno == EAFNOSUPPORT)
141 1.1 thorpej return;
142 1.8 dyoung err(EXIT_FAILURE, "getsock");
143 1.1 thorpej }
144 1.7 dyoung if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
145 1.7 dyoung err(EXIT_FAILURE, "%s: getifinfo", __func__);
146 1.7 dyoung
147 1.7 dyoung if ((af = getaf(env)) == -1)
148 1.8 dyoung af = AF_APPLETALK;
149 1.8 dyoung else if (af != AF_APPLETALK)
150 1.8 dyoung return;
151 1.7 dyoung
152 1.7 dyoung memset(&ifr, 0, sizeof(ifr));
153 1.7 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
154 1.7 dyoung ifr.ifr_addr.sa_family = af;
155 1.1 thorpej if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
156 1.1 thorpej if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
157 1.1 thorpej if (!force)
158 1.1 thorpej return;
159 1.1 thorpej (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
160 1.1 thorpej } else
161 1.1 thorpej warn("SIOCGIFADDR");
162 1.1 thorpej }
163 1.7 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
164 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_addr;
165 1.1 thorpej
166 1.1 thorpej (void) memset(&null_sat, 0, sizeof(null_sat));
167 1.1 thorpej
168 1.1 thorpej nr = (struct netrange *) &sat->sat_zero;
169 1.1 thorpej printf("\tatalk %d.%d range %d-%d phase %d",
170 1.1 thorpej ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
171 1.1 thorpej ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
172 1.7 dyoung
173 1.1 thorpej if (flags & IFF_POINTOPOINT) {
174 1.1 thorpej if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
175 1.1 thorpej if (errno == EADDRNOTAVAIL)
176 1.7 dyoung memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
177 1.1 thorpej else
178 1.7 dyoung warn("SIOCGIFDSTADDR");
179 1.1 thorpej }
180 1.7 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
181 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
182 1.1 thorpej if (!sat)
183 1.1 thorpej sat = &null_sat;
184 1.1 thorpej printf("--> %d.%d",
185 1.1 thorpej ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
186 1.1 thorpej }
187 1.1 thorpej if (flags & IFF_BROADCAST) {
188 1.1 thorpej /* note RTAX_BRD overlap with IFF_POINTOPOINT */
189 1.1 thorpej sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
190 1.1 thorpej if (sat)
191 1.1 thorpej printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
192 1.1 thorpej sat->sat_addr.s_node);
193 1.1 thorpej }
194 1.1 thorpej printf("\n");
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej #endif /* ! INET_ONLY */
198