ieee80211.c revision 1.1 1 1.1 thorpej /* $NetBSD: ieee80211.c,v 1.1 2005/03/19 23:13:42 thorpej 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.1 thorpej __RCSID("$NetBSD: ieee80211.c,v 1.1 2005/03/19 23:13:42 thorpej 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 #include <net/if_ether.h>
43 1.1 thorpej #include <net80211/ieee80211.h>
44 1.1 thorpej #include <net80211/ieee80211_ioctl.h>
45 1.1 thorpej
46 1.1 thorpej #include <ctype.h>
47 1.1 thorpej #include <err.h>
48 1.1 thorpej #include <netdb.h>
49 1.1 thorpej #include <string.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <stdio.h>
52 1.1 thorpej
53 1.1 thorpej #include "extern.h"
54 1.1 thorpej #include "ieee80211.h"
55 1.1 thorpej
56 1.1 thorpej void
57 1.1 thorpej setifnwid(const char *val, int d)
58 1.1 thorpej {
59 1.1 thorpej struct ieee80211_nwid nwid;
60 1.1 thorpej int len;
61 1.1 thorpej
62 1.1 thorpej len = sizeof(nwid.i_nwid);
63 1.1 thorpej if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
64 1.1 thorpej return;
65 1.1 thorpej nwid.i_len = len;
66 1.1 thorpej (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
67 1.1 thorpej ifr.ifr_data = (void *)&nwid;
68 1.1 thorpej if (ioctl(s, SIOCS80211NWID, &ifr) == -1)
69 1.1 thorpej warn("SIOCS80211NWID");
70 1.1 thorpej }
71 1.1 thorpej
72 1.1 thorpej void
73 1.1 thorpej setifbssid(const char *val, int d)
74 1.1 thorpej {
75 1.1 thorpej struct ieee80211_bssid bssid;
76 1.1 thorpej struct ether_addr *ea;
77 1.1 thorpej
78 1.1 thorpej if (d != 0) {
79 1.1 thorpej /* no BSSID is especially desired */
80 1.1 thorpej memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));
81 1.1 thorpej } else {
82 1.1 thorpej ea = ether_aton(val);
83 1.1 thorpej if (ea == NULL) {
84 1.1 thorpej warnx("malformed BSSID: %s", val);
85 1.1 thorpej return;
86 1.1 thorpej }
87 1.1 thorpej memcpy(&bssid.i_bssid, ea->ether_addr_octet,
88 1.1 thorpej sizeof(bssid.i_bssid));
89 1.1 thorpej }
90 1.1 thorpej (void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
91 1.1 thorpej if (ioctl(s, SIOCS80211BSSID, &bssid) == -1)
92 1.1 thorpej warn("SIOCS80211BSSID");
93 1.1 thorpej }
94 1.1 thorpej
95 1.1 thorpej void
96 1.1 thorpej setifchan(const char *val, int d)
97 1.1 thorpej {
98 1.1 thorpej struct ieee80211chanreq channel;
99 1.1 thorpej int chan;
100 1.1 thorpej
101 1.1 thorpej if (d != 0)
102 1.1 thorpej chan = IEEE80211_CHAN_ANY;
103 1.1 thorpej else {
104 1.1 thorpej chan = atoi(val);
105 1.1 thorpej if (chan < 0 || chan > 0xffff) {
106 1.1 thorpej warnx("invalid channel: %s", val);
107 1.1 thorpej return;
108 1.1 thorpej }
109 1.1 thorpej }
110 1.1 thorpej
111 1.1 thorpej (void)strncpy(channel.i_name, name, sizeof(channel.i_name));
112 1.1 thorpej channel.i_channel = (u_int16_t) chan;
113 1.1 thorpej if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
114 1.1 thorpej warn("SIOCS80211CHANNEL");
115 1.1 thorpej }
116 1.1 thorpej
117 1.1 thorpej void
118 1.1 thorpej setifnwkey(const char *val, int d)
119 1.1 thorpej {
120 1.1 thorpej struct ieee80211_nwkey nwkey;
121 1.1 thorpej int i;
122 1.1 thorpej u_int8_t keybuf[IEEE80211_WEP_NKID][16];
123 1.1 thorpej
124 1.1 thorpej nwkey.i_wepon = IEEE80211_NWKEY_WEP;
125 1.1 thorpej nwkey.i_defkid = 1;
126 1.1 thorpej for (i = 0; i < IEEE80211_WEP_NKID; i++) {
127 1.1 thorpej nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
128 1.1 thorpej nwkey.i_key[i].i_keydat = keybuf[i];
129 1.1 thorpej }
130 1.1 thorpej if (d != 0) {
131 1.1 thorpej /* disable WEP encryption */
132 1.1 thorpej nwkey.i_wepon = 0;
133 1.1 thorpej i = 0;
134 1.1 thorpej } else if (strcasecmp("persist", val) == 0) {
135 1.1 thorpej /* use all values from persistent memory */
136 1.1 thorpej nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
137 1.1 thorpej nwkey.i_defkid = 0;
138 1.1 thorpej for (i = 0; i < IEEE80211_WEP_NKID; i++)
139 1.1 thorpej nwkey.i_key[i].i_keylen = -1;
140 1.1 thorpej } else if (strncasecmp("persist:", val, 8) == 0) {
141 1.1 thorpej val += 8;
142 1.1 thorpej /* program keys in persistent memory */
143 1.1 thorpej nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
144 1.1 thorpej goto set_nwkey;
145 1.1 thorpej } else {
146 1.1 thorpej set_nwkey:
147 1.1 thorpej if (isdigit((unsigned char)val[0]) && val[1] == ':') {
148 1.1 thorpej /* specifying a full set of four keys */
149 1.1 thorpej nwkey.i_defkid = val[0] - '0';
150 1.1 thorpej val += 2;
151 1.1 thorpej for (i = 0; i < IEEE80211_WEP_NKID; i++) {
152 1.1 thorpej val = get_string(val, ",", keybuf[i],
153 1.1 thorpej &nwkey.i_key[i].i_keylen);
154 1.1 thorpej if (val == NULL)
155 1.1 thorpej return;
156 1.1 thorpej }
157 1.1 thorpej if (*val != '\0') {
158 1.1 thorpej warnx("SIOCS80211NWKEY: too many keys.");
159 1.1 thorpej return;
160 1.1 thorpej }
161 1.1 thorpej } else {
162 1.1 thorpej val = get_string(val, NULL, keybuf[0],
163 1.1 thorpej &nwkey.i_key[0].i_keylen);
164 1.1 thorpej if (val == NULL)
165 1.1 thorpej return;
166 1.1 thorpej i = 1;
167 1.1 thorpej }
168 1.1 thorpej }
169 1.1 thorpej for (; i < IEEE80211_WEP_NKID; i++)
170 1.1 thorpej nwkey.i_key[i].i_keylen = 0;
171 1.1 thorpej (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
172 1.1 thorpej if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
173 1.1 thorpej warn("SIOCS80211NWKEY");
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej void
177 1.1 thorpej setifpowersave(const char *val, int d)
178 1.1 thorpej {
179 1.1 thorpej struct ieee80211_power power;
180 1.1 thorpej
181 1.1 thorpej (void)strncpy(power.i_name, name, sizeof(power.i_name));
182 1.1 thorpej if (ioctl(s, SIOCG80211POWER, &power) == -1) {
183 1.1 thorpej warn("SIOCG80211POWER");
184 1.1 thorpej return;
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej power.i_enabled = d;
188 1.1 thorpej if (ioctl(s, SIOCS80211POWER, &power) == -1)
189 1.1 thorpej warn("SIOCS80211POWER");
190 1.1 thorpej }
191 1.1 thorpej
192 1.1 thorpej void
193 1.1 thorpej setifpowersavesleep(const char *val, int d)
194 1.1 thorpej {
195 1.1 thorpej struct ieee80211_power power;
196 1.1 thorpej
197 1.1 thorpej (void)strncpy(power.i_name, name, sizeof(power.i_name));
198 1.1 thorpej if (ioctl(s, SIOCG80211POWER, &power) == -1) {
199 1.1 thorpej warn("SIOCG80211POWER");
200 1.1 thorpej return;
201 1.1 thorpej }
202 1.1 thorpej
203 1.1 thorpej power.i_maxsleep = atoi(val);
204 1.1 thorpej if (ioctl(s, SIOCS80211POWER, &power) == -1)
205 1.1 thorpej warn("SIOCS80211POWER");
206 1.1 thorpej }
207 1.1 thorpej
208 1.1 thorpej void
209 1.1 thorpej ieee80211_statistics(void)
210 1.1 thorpej {
211 1.1 thorpej struct ieee80211_stats stats;
212 1.1 thorpej
213 1.1 thorpej memset(&ifr, 0, sizeof(ifr));
214 1.1 thorpej ifr.ifr_data = (caddr_t)&stats;
215 1.1 thorpej (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
216 1.1 thorpej if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
217 1.1 thorpej (caddr_t)&ifr) == -1)
218 1.1 thorpej return;
219 1.1 thorpej #define RX_PRINT(desc, member) printf("\trx " desc ": %u\n", stats.member)
220 1.1 thorpej #define TX_PRINT(desc, member) printf("\ttx " desc ": %u\n", stats.member)
221 1.1 thorpej
222 1.1 thorpej RX_PRINT("too short", is_rx_tooshort);
223 1.1 thorpej RX_PRINT("bad version", is_rx_badversion);
224 1.1 thorpej RX_PRINT("wrong bss", is_rx_wrongbss);
225 1.1 thorpej RX_PRINT("duplicate", is_rx_dup);
226 1.1 thorpej RX_PRINT("wrong direction", is_rx_wrongdir);
227 1.1 thorpej RX_PRINT("multicast echo", is_rx_mcastecho);
228 1.1 thorpej RX_PRINT("STA not associated", is_rx_notassoc);
229 1.1 thorpej RX_PRINT("WEP-encrypted but WEP not configured", is_rx_nowep);
230 1.1 thorpej RX_PRINT("WEP processing failed", is_rx_wepfail);
231 1.1 thorpej #if 0
232 1.1 thorpej RX_PRINT("single (M)MSDU, both WEP/non-WEP fragments", is_rx_wepmix);
233 1.1 thorpej RX_PRINT("non-consecutive fragments", is_rx_fragorder);
234 1.1 thorpej #endif
235 1.1 thorpej RX_PRINT("decapsulation failed", is_rx_decap);
236 1.1 thorpej RX_PRINT("management-type discarded", is_rx_mgtdiscard);
237 1.1 thorpej RX_PRINT("control-type discarded", is_rx_ctl);
238 1.1 thorpej RX_PRINT("truncated rate set", is_rx_rstoobig);
239 1.1 thorpej RX_PRINT("beacon/prresp element missing", is_rx_elem_missing);
240 1.1 thorpej RX_PRINT("beacon/prresp element too big", is_rx_elem_toobig);
241 1.1 thorpej RX_PRINT("beacon/prresp element too small", is_rx_elem_toosmall);
242 1.1 thorpej RX_PRINT("beacon/prresp element unknown", is_rx_elem_unknown);
243 1.1 thorpej RX_PRINT("invalid channel", is_rx_badchan);
244 1.1 thorpej RX_PRINT("channel mismatch", is_rx_chanmismatch);
245 1.1 thorpej RX_PRINT("failed node allocation", is_rx_nodealloc);
246 1.1 thorpej RX_PRINT("SSID mismatch", is_rx_ssidmismatch);
247 1.1 thorpej RX_PRINT("unsupported authentication algor.", is_rx_auth_unsupported);
248 1.1 thorpej RX_PRINT("STA authentication failure", is_rx_auth_fail);
249 1.1 thorpej RX_PRINT("association for wrong bss", is_rx_assoc_bss);
250 1.1 thorpej RX_PRINT("association without authenication", is_rx_assoc_notauth);
251 1.1 thorpej RX_PRINT("association capability mismatch", is_rx_assoc_capmismatch);
252 1.1 thorpej RX_PRINT("association without rate match", is_rx_assoc_norate);
253 1.1 thorpej RX_PRINT("deauthentication", is_rx_deauth);
254 1.1 thorpej RX_PRINT("disassocation", is_rx_disassoc);
255 1.1 thorpej RX_PRINT("unknown subtype", is_rx_badsubtype);
256 1.1 thorpej RX_PRINT("failed, mbuf unavailable", is_rx_nombuf);
257 1.1 thorpej RX_PRINT("failed, bad ICV", is_rx_decryptcrc);
258 1.1 thorpej RX_PRINT("discard mgmt frame in ad-hoc demo mode", is_rx_ahdemo_mgt);
259 1.1 thorpej RX_PRINT("bad authentication", is_rx_bad_auth);
260 1.1 thorpej TX_PRINT("failed, mbuf unavailable", is_tx_nombuf);
261 1.1 thorpej TX_PRINT("failed, no node", is_tx_nonode);
262 1.1 thorpej TX_PRINT("unknown mgmt frame", is_tx_unknownmgt);
263 1.1 thorpej printf("\tactive scans: %u\n", stats.is_scan_active);
264 1.1 thorpej printf("\tpassive scans: %u\n", stats.is_scan_passive);
265 1.1 thorpej printf("\tnodes timed-out for inactivity: %u\n",
266 1.1 thorpej stats.is_node_timeout);
267 1.1 thorpej printf("\tcrypto context memory unavailable: %u\n",
268 1.1 thorpej stats.is_crypto_nomem);
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej void
272 1.1 thorpej ieee80211_status(void)
273 1.1 thorpej {
274 1.1 thorpej int i, nwkey_verbose;
275 1.1 thorpej struct ieee80211_nwid nwid;
276 1.1 thorpej struct ieee80211_nwkey nwkey;
277 1.1 thorpej struct ieee80211_power power;
278 1.1 thorpej u_int8_t keybuf[IEEE80211_WEP_NKID][16];
279 1.1 thorpej struct ieee80211_bssid bssid;
280 1.1 thorpej struct ieee80211chanreq channel;
281 1.1 thorpej struct ether_addr ea;
282 1.1 thorpej static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
283 1.1 thorpej
284 1.1 thorpej memset(&ifr, 0, sizeof(ifr));
285 1.1 thorpej ifr.ifr_data = (void *)&nwid;
286 1.1 thorpej (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
287 1.1 thorpej if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
288 1.1 thorpej return;
289 1.1 thorpej if (nwid.i_len > IEEE80211_NWID_LEN) {
290 1.1 thorpej warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
291 1.1 thorpej return;
292 1.1 thorpej }
293 1.1 thorpej printf("\tssid ");
294 1.1 thorpej print_string(nwid.i_nwid, nwid.i_len);
295 1.1 thorpej memset(&nwkey, 0, sizeof(nwkey));
296 1.1 thorpej (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
297 1.1 thorpej /* show nwkey only when WEP is enabled */
298 1.1 thorpej if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
299 1.1 thorpej nwkey.i_wepon == 0) {
300 1.1 thorpej printf("\n");
301 1.1 thorpej goto skip_wep;
302 1.1 thorpej }
303 1.1 thorpej
304 1.1 thorpej printf(" nwkey ");
305 1.1 thorpej /* try to retrieve WEP keys */
306 1.1 thorpej for (i = 0; i < IEEE80211_WEP_NKID; i++) {
307 1.1 thorpej nwkey.i_key[i].i_keydat = keybuf[i];
308 1.1 thorpej nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
309 1.1 thorpej }
310 1.1 thorpej if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
311 1.1 thorpej printf("*****");
312 1.1 thorpej } else {
313 1.1 thorpej nwkey_verbose = 0;
314 1.1 thorpej /* check to see non default key or multiple keys defined */
315 1.1 thorpej if (nwkey.i_defkid != 1) {
316 1.1 thorpej nwkey_verbose = 1;
317 1.1 thorpej } else {
318 1.1 thorpej for (i = 1; i < IEEE80211_WEP_NKID; i++) {
319 1.1 thorpej if (nwkey.i_key[i].i_keylen != 0) {
320 1.1 thorpej nwkey_verbose = 1;
321 1.1 thorpej break;
322 1.1 thorpej }
323 1.1 thorpej }
324 1.1 thorpej }
325 1.1 thorpej /* check extra ambiguity with keywords */
326 1.1 thorpej if (!nwkey_verbose) {
327 1.1 thorpej if (nwkey.i_key[0].i_keylen >= 2 &&
328 1.1 thorpej isdigit(nwkey.i_key[0].i_keydat[0]) &&
329 1.1 thorpej nwkey.i_key[0].i_keydat[1] == ':')
330 1.1 thorpej nwkey_verbose = 1;
331 1.1 thorpej else if (nwkey.i_key[0].i_keylen >= 7 &&
332 1.1 thorpej strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
333 1.1 thorpej == 0)
334 1.1 thorpej nwkey_verbose = 1;
335 1.1 thorpej }
336 1.1 thorpej if (nwkey_verbose)
337 1.1 thorpej printf("%d:", nwkey.i_defkid);
338 1.1 thorpej for (i = 0; i < IEEE80211_WEP_NKID; i++) {
339 1.1 thorpej if (i > 0)
340 1.1 thorpej printf(",");
341 1.1 thorpej if (nwkey.i_key[i].i_keylen < 0)
342 1.1 thorpej printf("persist");
343 1.1 thorpej else
344 1.1 thorpej print_string(nwkey.i_key[i].i_keydat,
345 1.1 thorpej nwkey.i_key[i].i_keylen);
346 1.1 thorpej if (!nwkey_verbose)
347 1.1 thorpej break;
348 1.1 thorpej }
349 1.1 thorpej }
350 1.1 thorpej printf("\n");
351 1.1 thorpej
352 1.1 thorpej skip_wep:
353 1.1 thorpej (void)strncpy(power.i_name, name, sizeof(power.i_name));
354 1.1 thorpej if (ioctl(s, SIOCG80211POWER, &power) == -1)
355 1.1 thorpej goto skip_power;
356 1.1 thorpej printf("\tpowersave ");
357 1.1 thorpej if (power.i_enabled)
358 1.1 thorpej printf("on (%dms sleep)", power.i_maxsleep);
359 1.1 thorpej else
360 1.1 thorpej printf("off");
361 1.1 thorpej printf("\n");
362 1.1 thorpej
363 1.1 thorpej skip_power:
364 1.1 thorpej (void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
365 1.1 thorpej if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
366 1.1 thorpej return;
367 1.1 thorpej (void)strncpy(channel.i_name, name, sizeof(channel.i_name));
368 1.1 thorpej if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
369 1.1 thorpej return;
370 1.1 thorpej if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
371 1.1 thorpej if (channel.i_channel != (u_int16_t)-1)
372 1.1 thorpej printf("\tchan %d\n", channel.i_channel);
373 1.1 thorpej } else {
374 1.1 thorpej memcpy(ea.ether_addr_octet, bssid.i_bssid,
375 1.1 thorpej sizeof(ea.ether_addr_octet));
376 1.1 thorpej printf("\tbssid %s", ether_ntoa(&ea));
377 1.1 thorpej if (channel.i_channel != IEEE80211_CHAN_ANY)
378 1.1 thorpej printf(" chan %d", channel.i_channel);
379 1.1 thorpej printf("\n");
380 1.1 thorpej }
381 1.1 thorpej }
382