ieee80211.c revision 1.8 1 /* $NetBSD: ieee80211.c,v 1.8 2007/01/09 09:19:02 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: ieee80211.c,v 1.8 2007/01/09 09:19:02 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 #include <net/if_ether.h>
43 #include <net/if_media.h>
44 #include <net80211/ieee80211.h>
45 #include <net80211/ieee80211_ioctl.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <util.h>
54
55 #include "extern.h"
56 #include "ieee80211.h"
57
58 static void set80211(int, int, int, uint8_t *);
59
60 static void
61 set80211(int type, int val, int len, u_int8_t *data)
62 {
63 struct ieee80211req ireq;
64
65 (void) memset(&ireq, 0, sizeof(ireq));
66 estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
67 ireq.i_type = type;
68 ireq.i_val = val;
69 ireq.i_len = len;
70 ireq.i_data = data;
71 if (ioctl(s, SIOCS80211, &ireq) < 0)
72 err(1, "SIOCS80211");
73 }
74
75 void
76 sethidessid(const char *val, int d)
77 {
78 set80211(IEEE80211_IOC_HIDESSID, d, 0, NULL);
79 }
80
81 void
82 setapbridge(const char *val, int d)
83 {
84 set80211(IEEE80211_IOC_APBRIDGE, d, 0, NULL);
85 }
86
87 static enum ieee80211_opmode
88 get80211opmode(void)
89 {
90 struct ifmediareq ifmr;
91
92 (void) memset(&ifmr, 0, sizeof(ifmr));
93 estrlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
94 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
95 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
96 return IEEE80211_M_IBSS; /* XXX ahdemo */
97 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
98 return IEEE80211_M_HOSTAP;
99 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
100 return IEEE80211_M_MONITOR;
101 }
102
103 return IEEE80211_M_STA;
104 }
105
106 void
107 setifnwid(const char *val, int d)
108 {
109 struct ieee80211_nwid nwid;
110 int len;
111
112 len = sizeof(nwid.i_nwid);
113 if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
114 return;
115 nwid.i_len = len;
116 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
117 ifr.ifr_data = (void *)&nwid;
118 if (ioctl(s, SIOCS80211NWID, &ifr) == -1)
119 warn("SIOCS80211NWID");
120 }
121
122 void
123 setifbssid(const char *val, int d)
124 {
125 struct ieee80211_bssid bssid;
126 struct ether_addr *ea;
127
128 if (d != 0) {
129 /* no BSSID is especially desired */
130 memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));
131 } else {
132 ea = ether_aton(val);
133 if (ea == NULL) {
134 warnx("malformed BSSID: %s", val);
135 return;
136 }
137 memcpy(&bssid.i_bssid, ea->ether_addr_octet,
138 sizeof(bssid.i_bssid));
139 }
140 estrlcpy(bssid.i_name, name, sizeof(bssid.i_name));
141 if (ioctl(s, SIOCS80211BSSID, &bssid) == -1)
142 warn("SIOCS80211BSSID");
143 }
144
145 void
146 setiffrag(const char *val, int d)
147 {
148 struct ieee80211req ireq;
149 int thr;
150
151 if (d != 0)
152 thr = IEEE80211_FRAG_MAX;
153 else {
154 thr = atoi(val);
155 if (thr < IEEE80211_FRAG_MIN || thr > IEEE80211_FRAG_MAX) {
156 errx(EXIT_FAILURE, "invalid fragmentation threshold: %s", val);
157 return;
158 }
159 }
160
161 (void)strncpy(ireq.i_name, name, sizeof(ireq.i_name));
162 ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
163 ireq.i_val = thr;
164 if (ioctl(s, SIOCS80211, &ireq) == -1)
165 err(EXIT_FAILURE, "IEEE80211_IOC_FRAGTHRESHOLD");
166 }
167
168 void
169 setifchan(const char *val, int d)
170 {
171 struct ieee80211chanreq channel;
172 int chan;
173
174 if (d != 0)
175 chan = IEEE80211_CHAN_ANY;
176 else {
177 chan = atoi(val);
178 if (chan < 0 || chan > 0xffff) {
179 warnx("invalid channel: %s", val);
180 return;
181 }
182 }
183
184 estrlcpy(channel.i_name, name, sizeof(channel.i_name));
185 channel.i_channel = (u_int16_t) chan;
186 if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
187 warn("SIOCS80211CHANNEL");
188 }
189
190 void
191 setifnwkey(const char *val, int d)
192 {
193 struct ieee80211_nwkey nwkey;
194 int i;
195 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
196
197 nwkey.i_wepon = IEEE80211_NWKEY_WEP;
198 nwkey.i_defkid = 1;
199 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
200 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
201 nwkey.i_key[i].i_keydat = keybuf[i];
202 }
203 if (d != 0) {
204 /* disable WEP encryption */
205 nwkey.i_wepon = 0;
206 i = 0;
207 } else if (strcasecmp("persist", val) == 0) {
208 /* use all values from persistent memory */
209 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
210 nwkey.i_defkid = 0;
211 for (i = 0; i < IEEE80211_WEP_NKID; i++)
212 nwkey.i_key[i].i_keylen = -1;
213 } else if (strncasecmp("persist:", val, 8) == 0) {
214 val += 8;
215 /* program keys in persistent memory */
216 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
217 goto set_nwkey;
218 } else {
219 set_nwkey:
220 if (isdigit((unsigned char)val[0]) && val[1] == ':') {
221 /* specifying a full set of four keys */
222 nwkey.i_defkid = val[0] - '0';
223 val += 2;
224 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
225 val = get_string(val, ",", keybuf[i],
226 &nwkey.i_key[i].i_keylen);
227 if (val == NULL)
228 return;
229 }
230 if (*val != '\0') {
231 warnx("SIOCS80211NWKEY: too many keys.");
232 return;
233 }
234 } else {
235 val = get_string(val, NULL, keybuf[0],
236 &nwkey.i_key[0].i_keylen);
237 if (val == NULL)
238 return;
239 i = 1;
240 }
241 }
242 for (; i < IEEE80211_WEP_NKID; i++)
243 nwkey.i_key[i].i_keylen = 0;
244 estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
245 if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
246 warn("SIOCS80211NWKEY");
247 }
248
249 void
250 setifpowersave(const char *val, int d)
251 {
252 struct ieee80211_power power;
253
254 estrlcpy(power.i_name, name, sizeof(power.i_name));
255 if (ioctl(s, SIOCG80211POWER, &power) == -1) {
256 warn("SIOCG80211POWER");
257 return;
258 }
259
260 power.i_enabled = d;
261 if (ioctl(s, SIOCS80211POWER, &power) == -1)
262 warn("SIOCS80211POWER");
263 }
264
265 void
266 setifpowersavesleep(const char *val, int d)
267 {
268 struct ieee80211_power power;
269
270 estrlcpy(power.i_name, name, sizeof(power.i_name));
271 if (ioctl(s, SIOCG80211POWER, &power) == -1) {
272 warn("SIOCG80211POWER");
273 return;
274 }
275
276 power.i_maxsleep = atoi(val);
277 if (ioctl(s, SIOCS80211POWER, &power) == -1)
278 warn("SIOCS80211POWER");
279 }
280
281 void
282 ieee80211_statistics(void)
283 {
284 struct ieee80211_stats stats;
285
286 memset(&ifr, 0, sizeof(ifr));
287 ifr.ifr_buflen = sizeof(stats);
288 ifr.ifr_buf = (caddr_t)&stats;
289 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
290 if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
291 (caddr_t)&ifr) == -1)
292 return;
293 #define STAT_PRINT(_member, _desc) \
294 printf("\t" _desc ": %" PRIu32 "\n", stats._member)
295
296 STAT_PRINT(is_rx_badversion, "rx frame with bad version");
297 STAT_PRINT(is_rx_tooshort, "rx frame too short");
298 STAT_PRINT(is_rx_wrongbss, "rx from wrong bssid");
299 STAT_PRINT(is_rx_dup, "rx discard 'cuz dup");
300 STAT_PRINT(is_rx_wrongdir, "rx w/ wrong direction");
301 STAT_PRINT(is_rx_mcastecho, "rx discard 'cuz mcast echo");
302 STAT_PRINT(is_rx_notassoc, "rx discard 'cuz sta !assoc");
303 STAT_PRINT(is_rx_noprivacy, "rx w/ wep but privacy off");
304 STAT_PRINT(is_rx_unencrypted, "rx w/o wep and privacy on");
305 STAT_PRINT(is_rx_wepfail, "rx wep processing failed");
306 STAT_PRINT(is_rx_decap, "rx decapsulation failed");
307 STAT_PRINT(is_rx_mgtdiscard, "rx discard mgt frames");
308 STAT_PRINT(is_rx_ctl, "rx discard ctrl frames");
309 STAT_PRINT(is_rx_beacon, "rx beacon frames");
310 STAT_PRINT(is_rx_rstoobig, "rx rate set truncated");
311 STAT_PRINT(is_rx_elem_missing, "rx required element missin");
312 STAT_PRINT(is_rx_elem_toobig, "rx element too big");
313 STAT_PRINT(is_rx_elem_toosmall, "rx element too small");
314 STAT_PRINT(is_rx_elem_unknown, "rx element unknown");
315 STAT_PRINT(is_rx_badchan, "rx frame w/ invalid chan");
316 STAT_PRINT(is_rx_chanmismatch, "rx frame chan mismatch");
317 STAT_PRINT(is_rx_nodealloc, "rx frame dropped");
318 STAT_PRINT(is_rx_ssidmismatch, "rx frame ssid mismatch ");
319 STAT_PRINT(is_rx_auth_unsupported, "rx w/ unsupported auth alg");
320 STAT_PRINT(is_rx_auth_fail, "rx sta auth failure");
321 STAT_PRINT(is_rx_auth_countermeasures, "rx auth discard 'cuz CM");
322 STAT_PRINT(is_rx_assoc_bss, "rx assoc from wrong bssid");
323 STAT_PRINT(is_rx_assoc_notauth, "rx assoc w/o auth");
324 STAT_PRINT(is_rx_assoc_capmismatch, "rx assoc w/ cap mismatch");
325 STAT_PRINT(is_rx_assoc_norate, "rx assoc w/ no rate match");
326 STAT_PRINT(is_rx_assoc_badwpaie, "rx assoc w/ bad WPA IE");
327 STAT_PRINT(is_rx_deauth, "rx deauthentication");
328 STAT_PRINT(is_rx_disassoc, "rx disassociation");
329 STAT_PRINT(is_rx_badsubtype, "rx frame w/ unknown subtyp");
330 STAT_PRINT(is_rx_nobuf, "rx failed for lack of buf");
331 STAT_PRINT(is_rx_decryptcrc, "rx decrypt failed on crc");
332 STAT_PRINT(is_rx_ahdemo_mgt, "rx discard ahdemo mgt fram");
333 STAT_PRINT(is_rx_bad_auth, "rx bad auth request");
334 STAT_PRINT(is_rx_unauth, "rx on unauthorized port");
335 STAT_PRINT(is_rx_badkeyid, "rx w/ incorrect keyid");
336 STAT_PRINT(is_rx_ccmpreplay, "rx seq# violation (CCMP)");
337 STAT_PRINT(is_rx_ccmpformat, "rx format bad (CCMP)");
338 STAT_PRINT(is_rx_ccmpmic, "rx MIC check failed (CCMP)");
339 STAT_PRINT(is_rx_tkipreplay, "rx seq# violation (TKIP)");
340 STAT_PRINT(is_rx_tkipformat, "rx format bad (TKIP)");
341 STAT_PRINT(is_rx_tkipmic, "rx MIC check failed (TKIP)");
342 STAT_PRINT(is_rx_tkipicv, "rx ICV check failed (TKIP)");
343 STAT_PRINT(is_rx_badcipher, "rx failed 'cuz key type");
344 STAT_PRINT(is_rx_nocipherctx, "rx failed 'cuz key !setup");
345 STAT_PRINT(is_rx_acl, "rx discard 'cuz acl policy");
346
347 STAT_PRINT(is_tx_nobuf, "tx failed for lack of buf");
348 STAT_PRINT(is_tx_nonode, "tx failed for no node");
349 STAT_PRINT(is_tx_unknownmgt, "tx of unknown mgt frame");
350 STAT_PRINT(is_tx_badcipher, "tx failed 'cuz key type");
351 STAT_PRINT(is_tx_nodefkey, "tx failed 'cuz no defkey");
352 STAT_PRINT(is_tx_noheadroom, "tx failed 'cuz no space");
353
354 STAT_PRINT(is_scan_active, "active scans started");
355 STAT_PRINT(is_scan_passive, "passive scans started");
356 STAT_PRINT(is_node_timeout, "nodes timed out inactivity");
357 STAT_PRINT(is_crypto_nomem, "no memory for crypto ctx");
358 STAT_PRINT(is_crypto_tkip, "tkip crypto done in s/w");
359 STAT_PRINT(is_crypto_tkipenmic, "tkip en-MIC done in s/w");
360 STAT_PRINT(is_crypto_tkipdemic, "tkip de-MIC done in s/w");
361 STAT_PRINT(is_crypto_tkipcm, "tkip counter measures");
362 STAT_PRINT(is_crypto_ccmp, "ccmp crypto done in s/w");
363 STAT_PRINT(is_crypto_wep, "wep crypto done in s/w");
364 STAT_PRINT(is_crypto_setkey_cipher, "cipher rejected key");
365 STAT_PRINT(is_crypto_setkey_nokey, "no key index for setkey");
366 STAT_PRINT(is_crypto_delkey, "driver key delete failed");
367 STAT_PRINT(is_crypto_badcipher, "unknown cipher");
368 STAT_PRINT(is_crypto_nocipher, "cipher not available");
369 STAT_PRINT(is_crypto_attachfail, "cipher attach failed");
370 STAT_PRINT(is_crypto_swfallback, "cipher fallback to s/w");
371 STAT_PRINT(is_crypto_keyfail, "driver key alloc failed");
372 STAT_PRINT(is_crypto_enmicfail, "en-MIC failed");
373 STAT_PRINT(is_ibss_capmismatch, "merge failed-cap mismatch");
374 STAT_PRINT(is_ibss_norate, "merge failed-rate mismatch");
375 STAT_PRINT(is_ps_unassoc, "ps-poll for unassoc. sta");
376 STAT_PRINT(is_ps_badaid, "ps-poll w/ incorrect aid");
377 STAT_PRINT(is_ps_qempty, "ps-poll w/ nothing to send");
378 }
379
380 void
381 ieee80211_status(void)
382 {
383 int i, nwkey_verbose;
384 struct ieee80211_nwid nwid;
385 struct ieee80211_nwkey nwkey;
386 struct ieee80211_power power;
387 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
388 struct ieee80211_bssid bssid;
389 struct ieee80211chanreq channel;
390 struct ieee80211req ireq;
391 struct ether_addr ea;
392 static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
393 enum ieee80211_opmode opmode = get80211opmode();
394 extern int vflag;
395
396 memset(&ifr, 0, sizeof(ifr));
397 ifr.ifr_data = (void *)&nwid;
398 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
399 if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
400 return;
401 if (nwid.i_len > IEEE80211_NWID_LEN) {
402 warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
403 return;
404 }
405 printf("\tssid ");
406 print_string(nwid.i_nwid, nwid.i_len);
407
408 if (opmode == IEEE80211_M_HOSTAP) {
409 estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
410 ireq.i_type = IEEE80211_IOC_HIDESSID;
411 if (ioctl(s, SIOCG80211, &ireq) != -1) {
412 if (ireq.i_val)
413 printf(" [hidden]");
414 else if (vflag)
415 printf(" [shown]");
416 }
417
418 ireq.i_type = IEEE80211_IOC_APBRIDGE;
419 if (ioctl(s, SIOCG80211, &ireq) != -1) {
420 if (ireq.i_val)
421 printf(" apbridge");
422 else if (vflag)
423 printf(" -apbridge");
424 }
425 }
426
427 estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
428 ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
429 if (ioctl(s, SIOCG80211, &ireq) == -1)
430 ;
431 else if (ireq.i_val < IEEE80211_FRAG_MAX)
432 printf(" frag %d", ireq.i_val);
433 else if (vflag)
434 printf(" -frag");
435
436 memset(&nwkey, 0, sizeof(nwkey));
437 estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
438 /* show nwkey only when WEP is enabled */
439 if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
440 nwkey.i_wepon == 0) {
441 printf("\n");
442 goto skip_wep;
443 }
444
445 printf(" nwkey ");
446 /* try to retrieve WEP keys */
447 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
448 nwkey.i_key[i].i_keydat = keybuf[i];
449 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
450 }
451 if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
452 printf("*****");
453 } else {
454 nwkey_verbose = 0;
455 /* check to see non default key or multiple keys defined */
456 if (nwkey.i_defkid != 1) {
457 nwkey_verbose = 1;
458 } else {
459 for (i = 1; i < IEEE80211_WEP_NKID; i++) {
460 if (nwkey.i_key[i].i_keylen != 0) {
461 nwkey_verbose = 1;
462 break;
463 }
464 }
465 }
466 /* check extra ambiguity with keywords */
467 if (!nwkey_verbose) {
468 if (nwkey.i_key[0].i_keylen >= 2 &&
469 isdigit(nwkey.i_key[0].i_keydat[0]) &&
470 nwkey.i_key[0].i_keydat[1] == ':')
471 nwkey_verbose = 1;
472 else if (nwkey.i_key[0].i_keylen >= 7 &&
473 strncasecmp("persist",
474 (const char *)nwkey.i_key[0].i_keydat, 7) == 0)
475 nwkey_verbose = 1;
476 }
477 if (nwkey_verbose)
478 printf("%d:", nwkey.i_defkid);
479 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
480 if (i > 0)
481 printf(",");
482 if (nwkey.i_key[i].i_keylen < 0)
483 printf("persist");
484 else
485 print_string(nwkey.i_key[i].i_keydat,
486 nwkey.i_key[i].i_keylen);
487 if (!nwkey_verbose)
488 break;
489 }
490 }
491 printf("\n");
492
493 skip_wep:
494 estrlcpy(power.i_name, name, sizeof(power.i_name));
495 if (ioctl(s, SIOCG80211POWER, &power) == -1)
496 goto skip_power;
497 printf("\tpowersave ");
498 if (power.i_enabled)
499 printf("on (%dms sleep)", power.i_maxsleep);
500 else
501 printf("off");
502 printf("\n");
503
504 skip_power:
505 estrlcpy(bssid.i_name, name, sizeof(bssid.i_name));
506 if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
507 return;
508 estrlcpy(channel.i_name, name, sizeof(channel.i_name));
509 if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
510 return;
511 if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
512 if (channel.i_channel != (u_int16_t)-1)
513 printf("\tchan %d\n", channel.i_channel);
514 } else {
515 memcpy(ea.ether_addr_octet, bssid.i_bssid,
516 sizeof(ea.ether_addr_octet));
517 printf("\tbssid %s", ether_ntoa(&ea));
518 if (channel.i_channel != IEEE80211_CHAN_ANY)
519 printf(" chan %d", channel.i_channel);
520 printf("\n");
521 }
522 }
523