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