ieee80211.c revision 1.7 1 /* $NetBSD: ieee80211.c,v 1.7 2006/08/26 18:14:28 christos 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.7 2006/08/26 18:14:28 christos 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 setifchan(const char *val, int d)
147 {
148 struct ieee80211chanreq channel;
149 int chan;
150
151 if (d != 0)
152 chan = IEEE80211_CHAN_ANY;
153 else {
154 chan = atoi(val);
155 if (chan < 0 || chan > 0xffff) {
156 warnx("invalid channel: %s", val);
157 return;
158 }
159 }
160
161 estrlcpy(channel.i_name, name, sizeof(channel.i_name));
162 channel.i_channel = (u_int16_t) chan;
163 if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
164 warn("SIOCS80211CHANNEL");
165 }
166
167 void
168 setifnwkey(const char *val, int d)
169 {
170 struct ieee80211_nwkey nwkey;
171 int i;
172 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
173
174 nwkey.i_wepon = IEEE80211_NWKEY_WEP;
175 nwkey.i_defkid = 1;
176 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
177 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
178 nwkey.i_key[i].i_keydat = keybuf[i];
179 }
180 if (d != 0) {
181 /* disable WEP encryption */
182 nwkey.i_wepon = 0;
183 i = 0;
184 } else if (strcasecmp("persist", val) == 0) {
185 /* use all values from persistent memory */
186 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
187 nwkey.i_defkid = 0;
188 for (i = 0; i < IEEE80211_WEP_NKID; i++)
189 nwkey.i_key[i].i_keylen = -1;
190 } else if (strncasecmp("persist:", val, 8) == 0) {
191 val += 8;
192 /* program keys in persistent memory */
193 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
194 goto set_nwkey;
195 } else {
196 set_nwkey:
197 if (isdigit((unsigned char)val[0]) && val[1] == ':') {
198 /* specifying a full set of four keys */
199 nwkey.i_defkid = val[0] - '0';
200 val += 2;
201 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
202 val = get_string(val, ",", keybuf[i],
203 &nwkey.i_key[i].i_keylen);
204 if (val == NULL)
205 return;
206 }
207 if (*val != '\0') {
208 warnx("SIOCS80211NWKEY: too many keys.");
209 return;
210 }
211 } else {
212 val = get_string(val, NULL, keybuf[0],
213 &nwkey.i_key[0].i_keylen);
214 if (val == NULL)
215 return;
216 i = 1;
217 }
218 }
219 for (; i < IEEE80211_WEP_NKID; i++)
220 nwkey.i_key[i].i_keylen = 0;
221 estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
222 if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
223 warn("SIOCS80211NWKEY");
224 }
225
226 void
227 setifpowersave(const char *val, int d)
228 {
229 struct ieee80211_power power;
230
231 estrlcpy(power.i_name, name, sizeof(power.i_name));
232 if (ioctl(s, SIOCG80211POWER, &power) == -1) {
233 warn("SIOCG80211POWER");
234 return;
235 }
236
237 power.i_enabled = d;
238 if (ioctl(s, SIOCS80211POWER, &power) == -1)
239 warn("SIOCS80211POWER");
240 }
241
242 void
243 setifpowersavesleep(const char *val, int d)
244 {
245 struct ieee80211_power power;
246
247 estrlcpy(power.i_name, name, sizeof(power.i_name));
248 if (ioctl(s, SIOCG80211POWER, &power) == -1) {
249 warn("SIOCG80211POWER");
250 return;
251 }
252
253 power.i_maxsleep = atoi(val);
254 if (ioctl(s, SIOCS80211POWER, &power) == -1)
255 warn("SIOCS80211POWER");
256 }
257
258 void
259 ieee80211_statistics(void)
260 {
261 struct ieee80211_stats stats;
262
263 memset(&ifr, 0, sizeof(ifr));
264 ifr.ifr_buflen = sizeof(stats);
265 ifr.ifr_buf = (caddr_t)&stats;
266 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
267 if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
268 (caddr_t)&ifr) == -1)
269 return;
270 #define STAT_PRINT(_member, _desc) \
271 printf("\t" _desc ": %" PRIu32 "\n", stats._member)
272
273 STAT_PRINT(is_rx_badversion, "rx frame with bad version");
274 STAT_PRINT(is_rx_tooshort, "rx frame too short");
275 STAT_PRINT(is_rx_wrongbss, "rx from wrong bssid");
276 STAT_PRINT(is_rx_dup, "rx discard 'cuz dup");
277 STAT_PRINT(is_rx_wrongdir, "rx w/ wrong direction");
278 STAT_PRINT(is_rx_mcastecho, "rx discard 'cuz mcast echo");
279 STAT_PRINT(is_rx_notassoc, "rx discard 'cuz sta !assoc");
280 STAT_PRINT(is_rx_noprivacy, "rx w/ wep but privacy off");
281 STAT_PRINT(is_rx_unencrypted, "rx w/o wep and privacy on");
282 STAT_PRINT(is_rx_wepfail, "rx wep processing failed");
283 STAT_PRINT(is_rx_decap, "rx decapsulation failed");
284 STAT_PRINT(is_rx_mgtdiscard, "rx discard mgt frames");
285 STAT_PRINT(is_rx_ctl, "rx discard ctrl frames");
286 STAT_PRINT(is_rx_beacon, "rx beacon frames");
287 STAT_PRINT(is_rx_rstoobig, "rx rate set truncated");
288 STAT_PRINT(is_rx_elem_missing, "rx required element missin");
289 STAT_PRINT(is_rx_elem_toobig, "rx element too big");
290 STAT_PRINT(is_rx_elem_toosmall, "rx element too small");
291 STAT_PRINT(is_rx_elem_unknown, "rx element unknown");
292 STAT_PRINT(is_rx_badchan, "rx frame w/ invalid chan");
293 STAT_PRINT(is_rx_chanmismatch, "rx frame chan mismatch");
294 STAT_PRINT(is_rx_nodealloc, "rx frame dropped");
295 STAT_PRINT(is_rx_ssidmismatch, "rx frame ssid mismatch ");
296 STAT_PRINT(is_rx_auth_unsupported, "rx w/ unsupported auth alg");
297 STAT_PRINT(is_rx_auth_fail, "rx sta auth failure");
298 STAT_PRINT(is_rx_auth_countermeasures, "rx auth discard 'cuz CM");
299 STAT_PRINT(is_rx_assoc_bss, "rx assoc from wrong bssid");
300 STAT_PRINT(is_rx_assoc_notauth, "rx assoc w/o auth");
301 STAT_PRINT(is_rx_assoc_capmismatch, "rx assoc w/ cap mismatch");
302 STAT_PRINT(is_rx_assoc_norate, "rx assoc w/ no rate match");
303 STAT_PRINT(is_rx_assoc_badwpaie, "rx assoc w/ bad WPA IE");
304 STAT_PRINT(is_rx_deauth, "rx deauthentication");
305 STAT_PRINT(is_rx_disassoc, "rx disassociation");
306 STAT_PRINT(is_rx_badsubtype, "rx frame w/ unknown subtyp");
307 STAT_PRINT(is_rx_nobuf, "rx failed for lack of buf");
308 STAT_PRINT(is_rx_decryptcrc, "rx decrypt failed on crc");
309 STAT_PRINT(is_rx_ahdemo_mgt, "rx discard ahdemo mgt fram");
310 STAT_PRINT(is_rx_bad_auth, "rx bad auth request");
311 STAT_PRINT(is_rx_unauth, "rx on unauthorized port");
312 STAT_PRINT(is_rx_badkeyid, "rx w/ incorrect keyid");
313 STAT_PRINT(is_rx_ccmpreplay, "rx seq# violation (CCMP)");
314 STAT_PRINT(is_rx_ccmpformat, "rx format bad (CCMP)");
315 STAT_PRINT(is_rx_ccmpmic, "rx MIC check failed (CCMP)");
316 STAT_PRINT(is_rx_tkipreplay, "rx seq# violation (TKIP)");
317 STAT_PRINT(is_rx_tkipformat, "rx format bad (TKIP)");
318 STAT_PRINT(is_rx_tkipmic, "rx MIC check failed (TKIP)");
319 STAT_PRINT(is_rx_tkipicv, "rx ICV check failed (TKIP)");
320 STAT_PRINT(is_rx_badcipher, "rx failed 'cuz key type");
321 STAT_PRINT(is_rx_nocipherctx, "rx failed 'cuz key !setup");
322 STAT_PRINT(is_rx_acl, "rx discard 'cuz acl policy");
323
324 STAT_PRINT(is_tx_nobuf, "tx failed for lack of buf");
325 STAT_PRINT(is_tx_nonode, "tx failed for no node");
326 STAT_PRINT(is_tx_unknownmgt, "tx of unknown mgt frame");
327 STAT_PRINT(is_tx_badcipher, "tx failed 'cuz key type");
328 STAT_PRINT(is_tx_nodefkey, "tx failed 'cuz no defkey");
329 STAT_PRINT(is_tx_noheadroom, "tx failed 'cuz no space");
330
331 STAT_PRINT(is_scan_active, "active scans started");
332 STAT_PRINT(is_scan_passive, "passive scans started");
333 STAT_PRINT(is_node_timeout, "nodes timed out inactivity");
334 STAT_PRINT(is_crypto_nomem, "no memory for crypto ctx");
335 STAT_PRINT(is_crypto_tkip, "tkip crypto done in s/w");
336 STAT_PRINT(is_crypto_tkipenmic, "tkip en-MIC done in s/w");
337 STAT_PRINT(is_crypto_tkipdemic, "tkip de-MIC done in s/w");
338 STAT_PRINT(is_crypto_tkipcm, "tkip counter measures");
339 STAT_PRINT(is_crypto_ccmp, "ccmp crypto done in s/w");
340 STAT_PRINT(is_crypto_wep, "wep crypto done in s/w");
341 STAT_PRINT(is_crypto_setkey_cipher, "cipher rejected key");
342 STAT_PRINT(is_crypto_setkey_nokey, "no key index for setkey");
343 STAT_PRINT(is_crypto_delkey, "driver key delete failed");
344 STAT_PRINT(is_crypto_badcipher, "unknown cipher");
345 STAT_PRINT(is_crypto_nocipher, "cipher not available");
346 STAT_PRINT(is_crypto_attachfail, "cipher attach failed");
347 STAT_PRINT(is_crypto_swfallback, "cipher fallback to s/w");
348 STAT_PRINT(is_crypto_keyfail, "driver key alloc failed");
349 STAT_PRINT(is_crypto_enmicfail, "en-MIC failed");
350 STAT_PRINT(is_ibss_capmismatch, "merge failed-cap mismatch");
351 STAT_PRINT(is_ibss_norate, "merge failed-rate mismatch");
352 STAT_PRINT(is_ps_unassoc, "ps-poll for unassoc. sta");
353 STAT_PRINT(is_ps_badaid, "ps-poll w/ incorrect aid");
354 STAT_PRINT(is_ps_qempty, "ps-poll w/ nothing to send");
355 }
356
357 void
358 ieee80211_status(void)
359 {
360 int i, nwkey_verbose;
361 struct ieee80211_nwid nwid;
362 struct ieee80211_nwkey nwkey;
363 struct ieee80211_power power;
364 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
365 struct ieee80211_bssid bssid;
366 struct ieee80211chanreq channel;
367 struct ieee80211req ireq;
368 struct ether_addr ea;
369 static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
370 enum ieee80211_opmode opmode = get80211opmode();
371 extern int vflag;
372
373 memset(&ifr, 0, sizeof(ifr));
374 ifr.ifr_data = (void *)&nwid;
375 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
376 if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
377 return;
378 if (nwid.i_len > IEEE80211_NWID_LEN) {
379 warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
380 return;
381 }
382 printf("\tssid ");
383 print_string(nwid.i_nwid, nwid.i_len);
384
385 if (opmode == IEEE80211_M_HOSTAP) {
386 estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
387 ireq.i_type = IEEE80211_IOC_HIDESSID;
388 if (ioctl(s, SIOCG80211, &ireq) != -1) {
389 if (ireq.i_val)
390 printf(" [hidden]");
391 else if (vflag)
392 printf(" [shown]");
393 }
394
395 ireq.i_type = IEEE80211_IOC_APBRIDGE;
396 if (ioctl(s, SIOCG80211, &ireq) != -1) {
397 if (ireq.i_val)
398 printf(" apbridge");
399 else if (vflag)
400 printf(" -apbridge");
401 }
402 }
403
404 memset(&nwkey, 0, sizeof(nwkey));
405 estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
406 /* show nwkey only when WEP is enabled */
407 if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
408 nwkey.i_wepon == 0) {
409 printf("\n");
410 goto skip_wep;
411 }
412
413 printf(" nwkey ");
414 /* try to retrieve WEP keys */
415 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
416 nwkey.i_key[i].i_keydat = keybuf[i];
417 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
418 }
419 if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
420 printf("*****");
421 } else {
422 nwkey_verbose = 0;
423 /* check to see non default key or multiple keys defined */
424 if (nwkey.i_defkid != 1) {
425 nwkey_verbose = 1;
426 } else {
427 for (i = 1; i < IEEE80211_WEP_NKID; i++) {
428 if (nwkey.i_key[i].i_keylen != 0) {
429 nwkey_verbose = 1;
430 break;
431 }
432 }
433 }
434 /* check extra ambiguity with keywords */
435 if (!nwkey_verbose) {
436 if (nwkey.i_key[0].i_keylen >= 2 &&
437 isdigit(nwkey.i_key[0].i_keydat[0]) &&
438 nwkey.i_key[0].i_keydat[1] == ':')
439 nwkey_verbose = 1;
440 else if (nwkey.i_key[0].i_keylen >= 7 &&
441 strncasecmp("persist",
442 (const char *)nwkey.i_key[0].i_keydat, 7) == 0)
443 nwkey_verbose = 1;
444 }
445 if (nwkey_verbose)
446 printf("%d:", nwkey.i_defkid);
447 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
448 if (i > 0)
449 printf(",");
450 if (nwkey.i_key[i].i_keylen < 0)
451 printf("persist");
452 else
453 print_string(nwkey.i_key[i].i_keydat,
454 nwkey.i_key[i].i_keylen);
455 if (!nwkey_verbose)
456 break;
457 }
458 }
459 printf("\n");
460
461 skip_wep:
462 estrlcpy(power.i_name, name, sizeof(power.i_name));
463 if (ioctl(s, SIOCG80211POWER, &power) == -1)
464 goto skip_power;
465 printf("\tpowersave ");
466 if (power.i_enabled)
467 printf("on (%dms sleep)", power.i_maxsleep);
468 else
469 printf("off");
470 printf("\n");
471
472 skip_power:
473 estrlcpy(bssid.i_name, name, sizeof(bssid.i_name));
474 if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
475 return;
476 estrlcpy(channel.i_name, name, sizeof(channel.i_name));
477 if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
478 return;
479 if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
480 if (channel.i_channel != (u_int16_t)-1)
481 printf("\tchan %d\n", channel.i_channel);
482 } else {
483 memcpy(ea.ether_addr_octet, bssid.i_bssid,
484 sizeof(ea.ether_addr_octet));
485 printf("\tbssid %s", ether_ntoa(&ea));
486 if (channel.i_channel != IEEE80211_CHAN_ANY)
487 printf(" chan %d", channel.i_channel);
488 printf("\n");
489 }
490 }
491