ieee80211.c revision 1.18 1 /* $NetBSD: ieee80211.c,v 1.18 2008/05/07 23:55:06 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.18 2008/05/07 23:55:06 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 <net/route.h>
45 #include <net80211/ieee80211.h>
46 #include <net80211/ieee80211_ioctl.h>
47 #include <net80211/ieee80211_netbsd.h>
48
49 #include <assert.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <netdb.h>
54 #include <string.h>
55 #include <stddef.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 #include <util.h>
60
61 #include "extern.h"
62 #include "ieee80211.h"
63 #include "parse.h"
64 #include "env.h"
65 #include "util.h"
66
67 static int set80211(prop_dictionary_t env, uint16_t, int16_t, int16_t,
68 u_int8_t *);
69 static u_int ieee80211_mhz2ieee(u_int, u_int);
70 static int getmaxrate(const uint8_t [15], u_int8_t);
71 static const char * getcaps(int);
72 static void printie(const char*, const uint8_t *, size_t, int);
73 static int copy_essid(char [], size_t, const u_int8_t *, size_t);
74 static void scan_and_wait(prop_dictionary_t);
75 static void list_scan(prop_dictionary_t);
76 static int mappsb(u_int , u_int);
77 static int mapgsm(u_int , u_int);
78
79 static void printies(const u_int8_t *, int, int);
80 static void printie(const char* , const uint8_t *, size_t , int);
81 static void printwmeparam(const char *, const u_int8_t *, size_t , int);
82 static void printwmeinfo(const char *, const u_int8_t *, size_t , int);
83 static const char * wpa_cipher(const u_int8_t *);
84 static const char * wpa_keymgmt(const u_int8_t *);
85 static void printwpaie(const char *, const u_int8_t *, size_t , int);
86 static const char * rsn_cipher(const u_int8_t *);
87 static const char * rsn_keymgmt(const u_int8_t *);
88 static void printrsnie(const char *, const u_int8_t *, size_t , int);
89 static void printssid(const char *, const u_int8_t *, size_t , int);
90 static void printrates(const char *, const u_int8_t *, size_t , int);
91 static void printcountry(const char *, const u_int8_t *, size_t , int);
92 static int iswpaoui(const u_int8_t *);
93 static int iswmeinfo(const u_int8_t *);
94 static int iswmeparam(const u_int8_t *);
95 static const char * iename(int);
96
97 extern int vflag;
98
99 static const struct kwinst ieee80211boolkw[] = {
100 {.k_word = "hidessid", .k_key = "hidessid", .k_neg = true,
101 .k_type = KW_T_BOOL, .k_bool = true, .k_negbool = false,
102 .k_exec = sethidessid}
103 , {.k_word = "apbridge", .k_key = "apbridge", .k_neg = true,
104 .k_type = KW_T_BOOL, .k_bool = true, .k_negbool = false,
105 .k_exec = setapbridge}
106 , {.k_word = "powersave", .k_key = "powersave", .k_neg = true,
107 .k_type = KW_T_BOOL, .k_bool = true, .k_negbool = false,
108 .k_exec = setifpowersave}
109 };
110
111 static const struct kwinst listskw[] = {
112 {.k_word = "scan", .k_exec = scan_exec}
113 };
114
115 static struct pkw lists = PKW_INITIALIZER(&lists, "ieee80211 lists", NULL,
116 "list", listskw, __arraycount(listskw), &command_root.pb_parser);
117
118 static const struct kwinst kw80211kw[] = {
119 {.k_word = "bssid", .k_nextparser = &parse_bssid.ps_parser}
120 , {.k_word = "-bssid", .k_exec = unsetifbssid,
121 .k_nextparser = &command_root.pb_parser}
122 , {.k_word = "chan", .k_nextparser = &parse_chan.pi_parser}
123 , {.k_word = "-chan", .k_key = "chan", .k_type = KW_T_NUM,
124 .k_num = IEEE80211_CHAN_ANY, .k_exec = setifchan,
125 .k_nextparser = &command_root.pb_parser}
126 , {.k_word = "frag", .k_nextparser = &parse_frag.pi_parser}
127 , {.k_word = "-frag", .k_key = "frag", .k_type = KW_T_NUM,
128 .k_num = IEEE80211_FRAG_MAX, .k_exec = setiffrag,
129 .k_nextparser = &command_root.pb_parser}
130 , {.k_word = "list", .k_nextparser = &lists.pk_parser}
131 , {.k_word = "nwid", .k_nextparser = &parse_ssid.ps_parser}
132 , {.k_word = "nwkey", .k_nextparser = &parse_nwkey.ps_parser}
133 , {.k_word = "-nwkey", .k_exec = unsetifnwkey,
134 .k_nextparser = &command_root.pb_parser}
135 , {.k_word = "ssid", .k_nextparser = &parse_ssid.ps_parser}
136 , {.k_word = "powersavesleep",
137 .k_nextparser = &parse_powersavesleep.pi_parser}
138 };
139
140 struct pkw kw80211 = PKW_INITIALIZER(&kw80211, "802.11 keywords", NULL, NULL,
141 kw80211kw, __arraycount(kw80211kw), NULL);
142
143 struct pkw ieee80211bool = PKW_INITIALIZER(&ieee80211bool, "ieee80211 boolean",
144 NULL, NULL, ieee80211boolkw, __arraycount(ieee80211boolkw),
145 &command_root.pb_parser);
146
147 struct pinteger parse_chan = PINTEGER_INITIALIZER1(&parse_chan, "chan",
148 0, UINT16_MAX, 10, setifchan, "chan", &command_root.pb_parser);
149
150 struct pinteger parse_frag = PINTEGER_INITIALIZER1(&parse_frag, "frag",
151 IEEE80211_FRAG_MIN, IEEE80211_FRAG_MAX, 10,
152 setiffrag, "frag", &command_root.pb_parser);
153
154 struct pstr parse_ssid = PSTR_INITIALIZER(&parse_pass, "ssid", setifssid,
155 "ssid", &command_root.pb_parser);
156
157 struct pinteger parse_powersavesleep =
158 PINTEGER_INITIALIZER1(&parse_powersavesleep, "powersavesleep",
159 0, INT_MAX, 10, setifpowersavesleep, "powersavesleep",
160 &command_root.pb_parser);
161
162 struct pstr parse_nwkey = PSTR_INITIALIZER(&parse_nwkey, "nwkey", setifnwkey,
163 "nwkey", &command_root.pb_parser);
164
165 struct pstr parse_bssid = PSTR_INITIALIZER(&parse_bssid, "bssid", setifbssid,
166 "bssid", &command_root.pb_parser);
167
168 static int
169 set80211(prop_dictionary_t env, uint16_t type, int16_t val, int16_t len,
170 u_int8_t *data)
171 {
172 struct ieee80211req ireq;
173
174 memset(&ireq, 0, sizeof(ireq));
175 ireq.i_type = type;
176 ireq.i_val = val;
177 ireq.i_len = len;
178 ireq.i_data = data;
179 if (direct_ioctl(env, SIOCS80211, &ireq) == -1) {
180 warn("SIOCS80211");
181 return -1;
182 }
183 return 0;
184 }
185
186 int
187 sethidessid(prop_dictionary_t env, prop_dictionary_t xenv)
188 {
189 bool on, rc;
190
191 rc = prop_dictionary_get_bool(env, "hidessid", &on);
192 assert(rc);
193 return set80211(env, IEEE80211_IOC_HIDESSID, on ? 1 : 0, 0, NULL);
194 }
195
196 int
197 setapbridge(prop_dictionary_t env, prop_dictionary_t xenv)
198 {
199 bool on, rc;
200
201 rc = prop_dictionary_get_bool(env, "apbridge", &on);
202 assert(rc);
203 return set80211(env, IEEE80211_IOC_APBRIDGE, on ? 1 : 0, 0, NULL);
204 }
205
206 static enum ieee80211_opmode
207 get80211opmode(prop_dictionary_t env)
208 {
209 struct ifmediareq ifmr;
210
211 memset(&ifmr, 0, sizeof(ifmr));
212 if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1)
213 ;
214 else if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
215 return IEEE80211_M_IBSS; /* XXX ahdemo */
216 else if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
217 return IEEE80211_M_HOSTAP;
218 else if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
219 return IEEE80211_M_MONITOR;
220
221 return IEEE80211_M_STA;
222 }
223
224 int
225 setifssid(prop_dictionary_t env, prop_dictionary_t xenv)
226 {
227 struct ieee80211_nwid nwid;
228 ssize_t len;
229
230 memset(&nwid, 0, sizeof(nwid));
231 if ((len = getargdata(env, "ssid", nwid.i_nwid,
232 sizeof(nwid.i_nwid))) == -1)
233 errx(EXIT_FAILURE, "%s: SSID too long", __func__);
234 nwid.i_len = (uint8_t)len;
235 if (indirect_ioctl(env, SIOCS80211NWID, &nwid) == -1)
236 err(EXIT_FAILURE, "SIOCS80211NWID");
237 return 0;
238 }
239
240 int
241 unsetifbssid(prop_dictionary_t env, prop_dictionary_t xenv)
242 {
243 struct ieee80211_bssid bssid;
244
245 memset(&bssid, 0, sizeof(bssid));
246
247 if (direct_ioctl(env, SIOCS80211BSSID, &bssid) == -1)
248 err(EXIT_FAILURE, "SIOCS80211BSSID");
249 return 0;
250 }
251
252 int
253 setifbssid(prop_dictionary_t env, prop_dictionary_t xenv)
254 {
255 char buf[24];
256 struct ieee80211_bssid bssid;
257 struct ether_addr *ea;
258
259 if (getargstr(env, "bssid", buf, sizeof(buf)) == -1)
260 errx(EXIT_FAILURE, "%s: BSSID too long", __func__);
261
262 ea = ether_aton(buf);
263 if (ea == NULL) {
264 errx(EXIT_FAILURE, "malformed BSSID: %s", buf);
265 return -1;
266 }
267 memcpy(&bssid.i_bssid, ea->ether_addr_octet,
268 sizeof(bssid.i_bssid));
269
270 if (direct_ioctl(env, SIOCS80211BSSID, &bssid) == -1)
271 err(EXIT_FAILURE, "SIOCS80211BSSID");
272 return 0;
273 }
274
275 int
276 setiffrag(prop_dictionary_t env, prop_dictionary_t xenv)
277 {
278 bool rc;
279 int16_t val;
280
281 rc = prop_dictionary_get_int16(env, "frag", &val);
282 assert(rc);
283 if (set80211(env, IEEE80211_IOC_FRAGTHRESHOLD, val, 0, NULL) == -1)
284 err(EXIT_FAILURE, "IEEE80211_IOC_FRAGTHRESHOLD");
285 return 0;
286 }
287
288 int
289 setifchan(prop_dictionary_t env, prop_dictionary_t xenv)
290 {
291 bool rc;
292 struct ieee80211chanreq channel;
293
294 rc = prop_dictionary_get_uint16(env, "chan", &channel.i_channel);
295 assert(rc);
296 if (direct_ioctl(env, SIOCS80211CHANNEL, &channel) == -1)
297 err(EXIT_FAILURE, "SIOCS80211CHANNEL");
298 return 0;
299 }
300
301 int
302 setifnwkey(prop_dictionary_t env, prop_dictionary_t xenv)
303 {
304 const char *val;
305 char buf[256];
306 struct ieee80211_nwkey nwkey;
307 int i;
308 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
309
310 if (getargstr(env, "nwkey", buf, sizeof(buf)) == -1)
311 errx(EXIT_FAILURE, "%s: nwkey too long", __func__);
312
313 val = buf;
314
315 nwkey.i_wepon = IEEE80211_NWKEY_WEP;
316 nwkey.i_defkid = 1;
317 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
318 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
319 nwkey.i_key[i].i_keydat = keybuf[i];
320 }
321 if (strcasecmp("persist", val) == 0) {
322 /* use all values from persistent memory */
323 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
324 nwkey.i_defkid = 0;
325 for (i = 0; i < IEEE80211_WEP_NKID; i++)
326 nwkey.i_key[i].i_keylen = -1;
327 } else if (strncasecmp("persist:", val, 8) == 0) {
328 val += 8;
329 /* program keys in persistent memory */
330 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
331 goto set_nwkey;
332 } else {
333 set_nwkey:
334 if (isdigit((unsigned char)val[0]) && val[1] == ':') {
335 /* specifying a full set of four keys */
336 nwkey.i_defkid = val[0] - '0';
337 val += 2;
338 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
339 val = get_string(val, ",", keybuf[i],
340 &nwkey.i_key[i].i_keylen);
341 if (val == NULL) {
342 errno = EINVAL;
343 return -1;
344 }
345 }
346 if (*val != '\0') {
347 errx(EXIT_FAILURE, "SIOCS80211NWKEY: too many keys.");
348 }
349 } else {
350 val = get_string(val, NULL, keybuf[0],
351 &nwkey.i_key[0].i_keylen);
352 if (val == NULL) {
353 errno = EINVAL;
354 return -1;
355 }
356 i = 1;
357 }
358 }
359 for (; i < IEEE80211_WEP_NKID; i++)
360 nwkey.i_key[i].i_keylen = 0;
361
362 if (direct_ioctl(env, SIOCS80211NWKEY, &nwkey) == -1)
363 err(EXIT_FAILURE, "SIOCS80211NWKEY");
364 return 0;
365 }
366
367 int
368 unsetifnwkey(prop_dictionary_t env, prop_dictionary_t xenv)
369 {
370 struct ieee80211_nwkey nwkey;
371 int i;
372
373 nwkey.i_wepon = 0;
374 nwkey.i_defkid = 1;
375 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
376 nwkey.i_key[i].i_keylen = 0;
377 nwkey.i_key[i].i_keydat = NULL;
378 }
379
380 if (direct_ioctl(env, SIOCS80211NWKEY, &nwkey) == -1)
381 err(EXIT_FAILURE, "SIOCS80211NWKEY");
382 return 0;
383 }
384
385 int
386 setifpowersave(prop_dictionary_t env, prop_dictionary_t xenv)
387 {
388 struct ieee80211_power power;
389 bool on, rc;
390
391 if (direct_ioctl(env, SIOCG80211POWER, &power) == -1)
392 err(EXIT_FAILURE, "SIOCG80211POWER");
393
394 rc = prop_dictionary_get_bool(env, "powersave", &on);
395 assert(rc);
396
397 power.i_enabled = on ? 1 : 0;
398 if (direct_ioctl(env, SIOCS80211POWER, &power) == -1) {
399 warn("SIOCS80211POWER");
400 return -1;
401 }
402 return 0;
403 }
404
405 int
406 setifpowersavesleep(prop_dictionary_t env, prop_dictionary_t xenv)
407 {
408 struct ieee80211_power power;
409 int64_t maxsleep;
410 bool rc;
411
412 rc = prop_dictionary_get_int64(env, "powersavesleep", &maxsleep);
413 assert(rc);
414
415 if (direct_ioctl(env, SIOCG80211POWER, &power) == -1)
416 err(EXIT_FAILURE, "SIOCG80211POWER");
417
418 power.i_maxsleep = maxsleep;
419 if (direct_ioctl(env, SIOCS80211POWER, &power) == -1)
420 err(EXIT_FAILURE, "SIOCS80211POWER");
421 return 0;
422 }
423
424 int
425 scan_exec(prop_dictionary_t env, prop_dictionary_t xenv)
426 {
427 scan_and_wait(env);
428 list_scan(env);
429 return 0;
430 }
431
432 void
433 ieee80211_statistics(prop_dictionary_t env)
434 {
435 struct ieee80211_stats stats;
436 struct ifreq ifr;
437
438 memset(&ifr, 0, sizeof(ifr));
439 ifr.ifr_buflen = sizeof(stats);
440 ifr.ifr_buf = (caddr_t)&stats;
441 if (direct_ioctl(env, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
442 &ifr) == -1)
443 return;
444 #define STAT_PRINT(_member, _desc) \
445 printf("\t" _desc ": %" PRIu32 "\n", stats._member)
446
447 STAT_PRINT(is_rx_badversion, "rx frame with bad version");
448 STAT_PRINT(is_rx_tooshort, "rx frame too short");
449 STAT_PRINT(is_rx_wrongbss, "rx from wrong bssid");
450 STAT_PRINT(is_rx_dup, "rx discard 'cuz dup");
451 STAT_PRINT(is_rx_wrongdir, "rx w/ wrong direction");
452 STAT_PRINT(is_rx_mcastecho, "rx discard 'cuz mcast echo");
453 STAT_PRINT(is_rx_notassoc, "rx discard 'cuz sta !assoc");
454 STAT_PRINT(is_rx_noprivacy, "rx w/ wep but privacy off");
455 STAT_PRINT(is_rx_unencrypted, "rx w/o wep and privacy on");
456 STAT_PRINT(is_rx_wepfail, "rx wep processing failed");
457 STAT_PRINT(is_rx_decap, "rx decapsulation failed");
458 STAT_PRINT(is_rx_mgtdiscard, "rx discard mgt frames");
459 STAT_PRINT(is_rx_ctl, "rx discard ctrl frames");
460 STAT_PRINT(is_rx_beacon, "rx beacon frames");
461 STAT_PRINT(is_rx_rstoobig, "rx rate set truncated");
462 STAT_PRINT(is_rx_elem_missing, "rx required element missing");
463 STAT_PRINT(is_rx_elem_toobig, "rx element too big");
464 STAT_PRINT(is_rx_elem_toosmall, "rx element too small");
465 STAT_PRINT(is_rx_elem_unknown, "rx element unknown");
466 STAT_PRINT(is_rx_badchan, "rx frame w/ invalid chan");
467 STAT_PRINT(is_rx_chanmismatch, "rx frame chan mismatch");
468 STAT_PRINT(is_rx_nodealloc, "rx frame dropped");
469 STAT_PRINT(is_rx_ssidmismatch, "rx frame ssid mismatch ");
470 STAT_PRINT(is_rx_auth_unsupported, "rx w/ unsupported auth alg");
471 STAT_PRINT(is_rx_auth_fail, "rx sta auth failure");
472 STAT_PRINT(is_rx_auth_countermeasures, "rx auth discard 'cuz CM");
473 STAT_PRINT(is_rx_assoc_bss, "rx assoc from wrong bssid");
474 STAT_PRINT(is_rx_assoc_notauth, "rx assoc w/o auth");
475 STAT_PRINT(is_rx_assoc_capmismatch, "rx assoc w/ cap mismatch");
476 STAT_PRINT(is_rx_assoc_norate, "rx assoc w/ no rate match");
477 STAT_PRINT(is_rx_assoc_badwpaie, "rx assoc w/ bad WPA IE");
478 STAT_PRINT(is_rx_deauth, "rx deauthentication");
479 STAT_PRINT(is_rx_disassoc, "rx disassociation");
480 STAT_PRINT(is_rx_badsubtype, "rx frame w/ unknown subtyp");
481 STAT_PRINT(is_rx_nobuf, "rx failed for lack of buf");
482 STAT_PRINT(is_rx_decryptcrc, "rx decrypt failed on crc");
483 STAT_PRINT(is_rx_ahdemo_mgt, "rx discard ahdemo mgt fram");
484 STAT_PRINT(is_rx_bad_auth, "rx bad auth request");
485 STAT_PRINT(is_rx_unauth, "rx on unauthorized port");
486 STAT_PRINT(is_rx_badkeyid, "rx w/ incorrect keyid");
487 STAT_PRINT(is_rx_ccmpreplay, "rx seq# violation (CCMP)");
488 STAT_PRINT(is_rx_ccmpformat, "rx format bad (CCMP)");
489 STAT_PRINT(is_rx_ccmpmic, "rx MIC check failed (CCMP)");
490 STAT_PRINT(is_rx_tkipreplay, "rx seq# violation (TKIP)");
491 STAT_PRINT(is_rx_tkipformat, "rx format bad (TKIP)");
492 STAT_PRINT(is_rx_tkipmic, "rx MIC check failed (TKIP)");
493 STAT_PRINT(is_rx_tkipicv, "rx ICV check failed (TKIP)");
494 STAT_PRINT(is_rx_badcipher, "rx failed 'cuz key type");
495 STAT_PRINT(is_rx_nocipherctx, "rx failed 'cuz key !setup");
496 STAT_PRINT(is_rx_acl, "rx discard 'cuz acl policy");
497
498 STAT_PRINT(is_tx_nobuf, "tx failed for lack of buf");
499 STAT_PRINT(is_tx_nonode, "tx failed for no node");
500 STAT_PRINT(is_tx_unknownmgt, "tx of unknown mgt frame");
501 STAT_PRINT(is_tx_badcipher, "tx failed 'cuz key type");
502 STAT_PRINT(is_tx_nodefkey, "tx failed 'cuz no defkey");
503 STAT_PRINT(is_tx_noheadroom, "tx failed 'cuz no space");
504 STAT_PRINT(is_tx_fragframes, "tx frames fragmented");
505 STAT_PRINT(is_tx_frags, "tx fragments created");
506
507 STAT_PRINT(is_scan_active, "active scans started");
508 STAT_PRINT(is_scan_passive, "passive scans started");
509 STAT_PRINT(is_node_timeout, "nodes timed out inactivity");
510 STAT_PRINT(is_crypto_nomem, "no memory for crypto ctx");
511 STAT_PRINT(is_crypto_tkip, "tkip crypto done in s/w");
512 STAT_PRINT(is_crypto_tkipenmic, "tkip en-MIC done in s/w");
513 STAT_PRINT(is_crypto_tkipdemic, "tkip de-MIC done in s/w");
514 STAT_PRINT(is_crypto_tkipcm, "tkip counter measures");
515 STAT_PRINT(is_crypto_ccmp, "ccmp crypto done in s/w");
516 STAT_PRINT(is_crypto_wep, "wep crypto done in s/w");
517 STAT_PRINT(is_crypto_setkey_cipher, "cipher rejected key");
518 STAT_PRINT(is_crypto_setkey_nokey, "no key index for setkey");
519 STAT_PRINT(is_crypto_delkey, "driver key delete failed");
520 STAT_PRINT(is_crypto_badcipher, "unknown cipher");
521 STAT_PRINT(is_crypto_nocipher, "cipher not available");
522 STAT_PRINT(is_crypto_attachfail, "cipher attach failed");
523 STAT_PRINT(is_crypto_swfallback, "cipher fallback to s/w");
524 STAT_PRINT(is_crypto_keyfail, "driver key alloc failed");
525 STAT_PRINT(is_crypto_enmicfail, "en-MIC failed");
526 STAT_PRINT(is_ibss_capmismatch, "merge failed-cap mismatch");
527 STAT_PRINT(is_ibss_norate, "merge failed-rate mismatch");
528 STAT_PRINT(is_ps_unassoc, "ps-poll for unassoc. sta");
529 STAT_PRINT(is_ps_badaid, "ps-poll w/ incorrect aid");
530 STAT_PRINT(is_ps_qempty, "ps-poll w/ nothing to send");
531 STAT_PRINT(is_ff_badhdr, "fast frame rx'd w/ bad hdr");
532 STAT_PRINT(is_ff_tooshort, "fast frame rx decap error");
533 STAT_PRINT(is_ff_split, "fast frame rx split error");
534 STAT_PRINT(is_ff_decap, "fast frames decap'd");
535 STAT_PRINT(is_ff_encap, "fast frames encap'd for tx");
536 STAT_PRINT(is_rx_badbintval, "rx frame w/ bogus bintval");
537 }
538
539 void
540 ieee80211_status(prop_dictionary_t env, prop_dictionary_t oenv)
541 {
542 int i, nwkey_verbose;
543 struct ieee80211_nwid nwid;
544 struct ieee80211_nwkey nwkey;
545 struct ieee80211_power power;
546 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
547 struct ieee80211_bssid bssid;
548 struct ieee80211chanreq channel;
549 struct ieee80211req ireq;
550 struct ether_addr ea;
551 static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
552 enum ieee80211_opmode opmode = get80211opmode(env);
553
554 memset(&bssid, 0, sizeof(bssid));
555 memset(&nwkey, 0, sizeof(nwkey));
556 memset(&nwid, 0, sizeof(nwid));
557 memset(&nwid, 0, sizeof(nwid));
558
559 if (indirect_ioctl(env, SIOCG80211NWID, &nwid) == -1)
560 return;
561 if (nwid.i_len > IEEE80211_NWID_LEN) {
562 errx(EXIT_FAILURE, "SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
563 }
564 printf("\tssid ");
565 print_string(nwid.i_nwid, nwid.i_len);
566
567 if (opmode == IEEE80211_M_HOSTAP) {
568 ireq.i_type = IEEE80211_IOC_HIDESSID;
569 if (direct_ioctl(env, SIOCG80211, &ireq) != -1) {
570 if (ireq.i_val)
571 printf(" [hidden]");
572 else if (vflag)
573 printf(" [shown]");
574 }
575
576 ireq.i_type = IEEE80211_IOC_APBRIDGE;
577 if (direct_ioctl(env, SIOCG80211, &ireq) != -1) {
578 if (ireq.i_val)
579 printf(" apbridge");
580 else if (vflag)
581 printf(" -apbridge");
582 }
583 }
584
585 ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
586 if (direct_ioctl(env, SIOCG80211, &ireq) == -1)
587 ;
588 else if (ireq.i_val < IEEE80211_FRAG_MAX)
589 printf(" frag %d", ireq.i_val);
590 else if (vflag)
591 printf(" -frag");
592
593 memset(&nwkey, 0, sizeof(nwkey));
594 /* show nwkey only when WEP is enabled */
595 if (direct_ioctl(env, SIOCG80211NWKEY, &nwkey) == -1 ||
596 nwkey.i_wepon == 0) {
597 printf("\n");
598 goto skip_wep;
599 }
600
601 printf(" nwkey ");
602 /* try to retrieve WEP keys */
603 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
604 nwkey.i_key[i].i_keydat = keybuf[i];
605 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
606 }
607 if (direct_ioctl(env, SIOCG80211NWKEY, &nwkey) == -1) {
608 printf("*****");
609 } else {
610 nwkey_verbose = 0;
611 /* check to see non default key or multiple keys defined */
612 if (nwkey.i_defkid != 1) {
613 nwkey_verbose = 1;
614 } else {
615 for (i = 1; i < IEEE80211_WEP_NKID; i++) {
616 if (nwkey.i_key[i].i_keylen != 0) {
617 nwkey_verbose = 1;
618 break;
619 }
620 }
621 }
622 /* check extra ambiguity with keywords */
623 if (!nwkey_verbose) {
624 if (nwkey.i_key[0].i_keylen >= 2 &&
625 isdigit(nwkey.i_key[0].i_keydat[0]) &&
626 nwkey.i_key[0].i_keydat[1] == ':')
627 nwkey_verbose = 1;
628 else if (nwkey.i_key[0].i_keylen >= 7 &&
629 strncasecmp("persist",
630 (const char *)nwkey.i_key[0].i_keydat, 7) == 0)
631 nwkey_verbose = 1;
632 }
633 if (nwkey_verbose)
634 printf("%d:", nwkey.i_defkid);
635 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
636 if (i > 0)
637 printf(",");
638 if (nwkey.i_key[i].i_keylen < 0)
639 printf("persist");
640 else
641 print_string(nwkey.i_key[i].i_keydat,
642 nwkey.i_key[i].i_keylen);
643 if (!nwkey_verbose)
644 break;
645 }
646 }
647 printf("\n");
648
649 skip_wep:
650 if (direct_ioctl(env, SIOCG80211POWER, &power) == -1)
651 goto skip_power;
652 printf("\tpowersave ");
653 if (power.i_enabled)
654 printf("on (%dms sleep)", power.i_maxsleep);
655 else
656 printf("off");
657 printf("\n");
658
659 skip_power:
660 if (direct_ioctl(env, SIOCG80211BSSID, &bssid) == -1)
661 return;
662 if (direct_ioctl(env, SIOCG80211CHANNEL, &channel) == -1)
663 return;
664 if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
665 if (channel.i_channel != (u_int16_t)-1)
666 printf("\tchan %d\n", channel.i_channel);
667 } else {
668 memcpy(ea.ether_addr_octet, bssid.i_bssid,
669 sizeof(ea.ether_addr_octet));
670 printf("\tbssid %s", ether_ntoa(&ea));
671 if (channel.i_channel != IEEE80211_CHAN_ANY)
672 printf(" chan %d", channel.i_channel);
673 printf("\n");
674 }
675 }
676
677 static void
678 scan_and_wait(prop_dictionary_t env)
679 {
680 int sroute;
681
682 sroute = socket(PF_ROUTE, SOCK_RAW, 0);
683 if (sroute < 0) {
684 perror("socket(PF_ROUTE,SOCK_RAW)");
685 return;
686 }
687 /* NB: only root can trigger a scan so ignore errors */
688 if (set80211(env, IEEE80211_IOC_SCAN_REQ, 0, 0, NULL) >= 0) {
689 char buf[2048];
690 struct if_announcemsghdr *ifan;
691 struct rt_msghdr *rtm;
692
693 do {
694 if (read(sroute, buf, sizeof(buf)) < 0) {
695 perror("read(PF_ROUTE)");
696 break;
697 }
698 rtm = (struct rt_msghdr *) buf;
699 if (rtm->rtm_version != RTM_VERSION)
700 break;
701 ifan = (struct if_announcemsghdr *) rtm;
702 } while (rtm->rtm_type != RTM_IEEE80211 ||
703 ifan->ifan_what != RTM_IEEE80211_SCAN);
704 }
705 close(sroute);
706 }
707
708 static void
709 list_scan(prop_dictionary_t env)
710 {
711 u_int8_t buf[24*1024];
712 struct ieee80211req ireq;
713 char ssid[IEEE80211_NWID_LEN+1];
714 const u_int8_t *cp;
715 int len, ssidmax;
716
717 memset(&ireq, 0, sizeof(ireq));
718 ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
719 ireq.i_data = buf;
720 ireq.i_len = sizeof(buf);
721 if (direct_ioctl(env, SIOCG80211, &ireq) < 0)
722 errx(EXIT_FAILURE, "unable to get scan results");
723 len = ireq.i_len;
724 if (len < sizeof(struct ieee80211req_scan_result))
725 return;
726
727 ssidmax = IEEE80211_NWID_LEN;
728 printf("%-*.*s %-17.17s %4s %4s %-7s %3s %4s\n"
729 , ssidmax, ssidmax, "SSID"
730 , "BSSID"
731 , "CHAN"
732 , "RATE"
733 , "S:N"
734 , "INT"
735 , "CAPS"
736 );
737 cp = buf;
738 do {
739 const struct ieee80211req_scan_result *sr;
740 const uint8_t *vp;
741
742 sr = (const struct ieee80211req_scan_result *) cp;
743 vp = (const u_int8_t *)(sr+1);
744 printf("%-*.*s %s %3d %3dM %3d:%-3d %3d %-4.4s"
745 , ssidmax
746 , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
747 , ssid
748 , ether_ntoa((const struct ether_addr *) sr->isr_bssid)
749 , ieee80211_mhz2ieee(sr->isr_freq, sr->isr_flags)
750 , getmaxrate(sr->isr_rates, sr->isr_nrates)
751 , sr->isr_rssi, sr->isr_noise
752 , sr->isr_intval
753 , getcaps(sr->isr_capinfo)
754 );
755 printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);;
756 printf("\n");
757 cp += sr->isr_len, len -= sr->isr_len;
758 } while (len >= sizeof(struct ieee80211req_scan_result));
759 }
760 /*
761 * Convert MHz frequency to IEEE channel number.
762 */
763 static u_int
764 ieee80211_mhz2ieee(u_int isrfreq, u_int isrflags)
765 {
766 if ((isrflags & IEEE80211_CHAN_GSM) || (907 <= isrfreq && isrfreq <= 922))
767 return mapgsm(isrfreq, isrflags);
768 if (isrfreq == 2484)
769 return 14;
770 if (isrfreq < 2484)
771 return (isrfreq - 2407) / 5;
772 if (isrfreq < 5000) {
773 if (isrflags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER))
774 return mappsb(isrfreq, isrflags);
775 else if (isrfreq > 4900)
776 return (isrfreq - 4000) / 5;
777 else
778 return 15 + ((isrfreq - 2512) / 20);
779 }
780 return (isrfreq - 5000) / 5;
781 }
782
783 static int
784 getmaxrate(const u_int8_t rates[15], u_int8_t nrates)
785 {
786 int i, maxrate = -1;
787
788 for (i = 0; i < nrates; i++) {
789 int rate = rates[i] & IEEE80211_RATE_VAL;
790 if (rate > maxrate)
791 maxrate = rate;
792 }
793 return maxrate / 2;
794 }
795
796 static const char *
797 getcaps(int capinfo)
798 {
799 static char capstring[32];
800 char *cp = capstring;
801
802 if (capinfo & IEEE80211_CAPINFO_ESS)
803 *cp++ = 'E';
804 if (capinfo & IEEE80211_CAPINFO_IBSS)
805 *cp++ = 'I';
806 if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
807 *cp++ = 'c';
808 if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
809 *cp++ = 'C';
810 if (capinfo & IEEE80211_CAPINFO_PRIVACY)
811 *cp++ = 'P';
812 if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
813 *cp++ = 'S';
814 if (capinfo & IEEE80211_CAPINFO_PBCC)
815 *cp++ = 'B';
816 if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
817 *cp++ = 'A';
818 if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
819 *cp++ = 's';
820 if (capinfo & IEEE80211_CAPINFO_RSN)
821 *cp++ = 'R';
822 if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
823 *cp++ = 'D';
824 *cp = '\0';
825 return capstring;
826 }
827
828 static void
829 printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
830 {
831 printf("%s", tag);
832
833 maxlen -= strlen(tag)+2;
834 if (2*ielen > maxlen)
835 maxlen--;
836 printf("<");
837 for (; ielen > 0; ie++, ielen--) {
838 if (maxlen-- <= 0)
839 break;
840 printf("%02x", *ie);
841 }
842 if (ielen != 0)
843 printf("-");
844 printf(">");
845 }
846
847 #define LE_READ_2(p) \
848 ((u_int16_t) \
849 ((((const u_int8_t *)(p))[0] ) | \
850 (((const u_int8_t *)(p))[1] << 8)))
851 #define LE_READ_4(p) \
852 ((u_int32_t) \
853 ((((const u_int8_t *)(p))[0] ) | \
854 (((const u_int8_t *)(p))[1] << 8) | \
855 (((const u_int8_t *)(p))[2] << 16) | \
856 (((const u_int8_t *)(p))[3] << 24)))
857
858 /*
859 * NB: The decoding routines assume a properly formatted ie
860 * which should be safe as the kernel only retains them
861 * if they parse ok.
862 */
863
864 static void
865 printwmeparam(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
866 {
867 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
868 static const char *acnames[] = { "BE", "BK", "VO", "VI" };
869 const struct ieee80211_wme_param *wme =
870 (const struct ieee80211_wme_param *) ie;
871 int i;
872
873 printf("%s", tag);
874 if (!vflag)
875 return;
876 printf("<qosinfo 0x%x", wme->param_qosInfo);
877 ie += offsetof(struct ieee80211_wme_param, params_acParams);
878 for (i = 0; i < WME_NUM_AC; i++) {
879 const struct ieee80211_wme_acparams *ac =
880 &wme->params_acParams[i];
881
882 printf(" %s[%saifsn %u cwmin %u cwmax %u txop %u]"
883 , acnames[i]
884 , MS(ac->acp_aci_aifsn, WME_PARAM_ACM) ? "acm " : ""
885 , MS(ac->acp_aci_aifsn, WME_PARAM_AIFSN)
886 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMIN)
887 , MS(ac->acp_logcwminmax, WME_PARAM_LOGCWMAX)
888 , LE_READ_2(&ac->acp_txop)
889 );
890 }
891 printf(">");
892 #undef MS
893 }
894
895 static void
896 printwmeinfo(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
897 {
898 printf("%s", tag);
899 if (vflag) {
900 const struct ieee80211_wme_info *wme =
901 (const struct ieee80211_wme_info *) ie;
902 printf("<version 0x%x info 0x%x>",
903 wme->wme_version, wme->wme_info);
904 }
905 }
906
907 static const char *
908 wpa_cipher(const u_int8_t *sel)
909 {
910 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
911 u_int32_t w = LE_READ_4(sel);
912
913 switch (w) {
914 case WPA_SEL(WPA_CSE_NULL):
915 return "NONE";
916 case WPA_SEL(WPA_CSE_WEP40):
917 return "WEP40";
918 case WPA_SEL(WPA_CSE_WEP104):
919 return "WEP104";
920 case WPA_SEL(WPA_CSE_TKIP):
921 return "TKIP";
922 case WPA_SEL(WPA_CSE_CCMP):
923 return "AES-CCMP";
924 }
925 return "?"; /* NB: so 1<< is discarded */
926 #undef WPA_SEL
927 }
928
929 static const char *
930 wpa_keymgmt(const u_int8_t *sel)
931 {
932 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
933 u_int32_t w = LE_READ_4(sel);
934
935 switch (w) {
936 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
937 return "8021X-UNSPEC";
938 case WPA_SEL(WPA_ASE_8021X_PSK):
939 return "8021X-PSK";
940 case WPA_SEL(WPA_ASE_NONE):
941 return "NONE";
942 }
943 return "?";
944 #undef WPA_SEL
945 }
946
947 static void
948 printwpaie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
949 {
950 u_int8_t len = ie[1];
951
952 printf("%s", tag);
953 if (vflag) {
954 const char *sep;
955 int n;
956
957 ie += 6, len -= 4; /* NB: len is payload only */
958
959 printf("<v%u", LE_READ_2(ie));
960 ie += 2, len -= 2;
961
962 printf(" mc:%s", wpa_cipher(ie));
963 ie += 4, len -= 4;
964
965 /* unicast ciphers */
966 n = LE_READ_2(ie);
967 ie += 2, len -= 2;
968 sep = " uc:";
969 for (; n > 0; n--) {
970 printf("%s%s", sep, wpa_cipher(ie));
971 ie += 4, len -= 4;
972 sep = "+";
973 }
974
975 /* key management algorithms */
976 n = LE_READ_2(ie);
977 ie += 2, len -= 2;
978 sep = " km:";
979 for (; n > 0; n--) {
980 printf("%s%s", sep, wpa_keymgmt(ie));
981 ie += 4, len -= 4;
982 sep = "+";
983 }
984
985 if (len > 2) /* optional capabilities */
986 printf(", caps 0x%x", LE_READ_2(ie));
987 printf(">");
988 }
989 }
990
991 static const char *
992 rsn_cipher(const u_int8_t *sel)
993 {
994 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
995 u_int32_t w = LE_READ_4(sel);
996
997 switch (w) {
998 case RSN_SEL(RSN_CSE_NULL):
999 return "NONE";
1000 case RSN_SEL(RSN_CSE_WEP40):
1001 return "WEP40";
1002 case RSN_SEL(RSN_CSE_WEP104):
1003 return "WEP104";
1004 case RSN_SEL(RSN_CSE_TKIP):
1005 return "TKIP";
1006 case RSN_SEL(RSN_CSE_CCMP):
1007 return "AES-CCMP";
1008 case RSN_SEL(RSN_CSE_WRAP):
1009 return "AES-OCB";
1010 }
1011 return "?";
1012 #undef WPA_SEL
1013 }
1014
1015 static const char *
1016 rsn_keymgmt(const u_int8_t *sel)
1017 {
1018 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1019 u_int32_t w = LE_READ_4(sel);
1020
1021 switch (w) {
1022 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1023 return "8021X-UNSPEC";
1024 case RSN_SEL(RSN_ASE_8021X_PSK):
1025 return "8021X-PSK";
1026 case RSN_SEL(RSN_ASE_NONE):
1027 return "NONE";
1028 }
1029 return "?";
1030 #undef RSN_SEL
1031 }
1032
1033 static void
1034 printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1035 {
1036 printf("%s", tag);
1037 if (vflag) {
1038 const char *sep;
1039 int n;
1040
1041 ie += 2, ielen -= 2;
1042
1043 printf("<v%u", LE_READ_2(ie));
1044 ie += 2, ielen -= 2;
1045
1046 printf(" mc:%s", rsn_cipher(ie));
1047 ie += 4, ielen -= 4;
1048
1049 /* unicast ciphers */
1050 n = LE_READ_2(ie);
1051 ie += 2, ielen -= 2;
1052 sep = " uc:";
1053 for (; n > 0; n--) {
1054 printf("%s%s", sep, rsn_cipher(ie));
1055 ie += 4, ielen -= 4;
1056 sep = "+";
1057 }
1058
1059 /* key management algorithms */
1060 n = LE_READ_2(ie);
1061 ie += 2, ielen -= 2;
1062 sep = " km:";
1063 for (; n > 0; n--) {
1064 printf("%s%s", sep, rsn_keymgmt(ie));
1065 ie += 4, ielen -= 4;
1066 sep = "+";
1067 }
1068
1069 if (ielen > 2) /* optional capabilities */
1070 printf(", caps 0x%x", LE_READ_2(ie));
1071 /* XXXPMKID */
1072 printf(">");
1073 }
1074 }
1075
1076 /*
1077 * Copy the ssid string contents into buf, truncating to fit. If the
1078 * ssid is entirely printable then just copy intact. Otherwise convert
1079 * to hexadecimal. If the result is truncated then replace the last
1080 * three characters with "...".
1081 */
1082 static int
1083 copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
1084 {
1085 const u_int8_t *p;
1086 size_t maxlen;
1087 int i;
1088
1089 if (essid_len > bufsize)
1090 maxlen = bufsize;
1091 else
1092 maxlen = essid_len;
1093 /* determine printable or not */
1094 for (i = 0, p = essid; i < maxlen; i++, p++) {
1095 if (*p < ' ' || *p > 0x7e)
1096 break;
1097 }
1098 if (i != maxlen) { /* not printable, print as hex */
1099 if (bufsize < 3)
1100 return 0;
1101 strlcpy(buf, "0x", bufsize);
1102 bufsize -= 2;
1103 p = essid;
1104 for (i = 0; i < maxlen && bufsize >= 2; i++) {
1105 sprintf(&buf[2+2*i], "%02x", p[i]);
1106 bufsize -= 2;
1107 }
1108 if (i != essid_len)
1109 memcpy(&buf[2+2*i-3], "...", 3);
1110 } else { /* printable, truncate as needed */
1111 memcpy(buf, essid, maxlen);
1112 if (maxlen != essid_len)
1113 memcpy(&buf[maxlen-3], "...", 3);
1114 }
1115 return maxlen;
1116 }
1117
1118 static void
1119 printssid(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1120 {
1121 char ssid[2*IEEE80211_NWID_LEN+1];
1122
1123 printf("%s<%.*s>", tag, copy_essid(ssid, maxlen, ie+2, ie[1]), ssid);
1124 }
1125
1126 static void
1127 printrates(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1128 {
1129 const char *sep;
1130 int i;
1131
1132 printf("%s", tag);
1133 sep = "<";
1134 for (i = 2; i < ielen; i++) {
1135 printf("%s%s%d", sep,
1136 ie[i] & IEEE80211_RATE_BASIC ? "B" : "",
1137 ie[i] & IEEE80211_RATE_VAL);
1138 sep = ",";
1139 }
1140 printf(">");
1141 }
1142
1143 static void
1144 printcountry(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen)
1145 {
1146 const struct ieee80211_country_ie *cie =
1147 (const struct ieee80211_country_ie *) ie;
1148 int i, nbands, schan, nchan;
1149
1150 printf("%s<%c%c%c", tag, cie->cc[0], cie->cc[1], cie->cc[2]);
1151 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1152 for (i = 0; i < nbands; i++) {
1153 schan = cie->band[i].schan;
1154 nchan = cie->band[i].nchan;
1155 if (nchan != 1)
1156 printf(" %u-%u,%u", schan, schan + nchan-1,
1157 cie->band[i].maxtxpwr);
1158 else
1159 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1160 }
1161 printf(">");
1162 }
1163
1164 /* unaligned little endian access */
1165 #define LE_READ_4(p) \
1166 ((u_int32_t) \
1167 ((((const u_int8_t *)(p))[0] ) | \
1168 (((const u_int8_t *)(p))[1] << 8) | \
1169 (((const u_int8_t *)(p))[2] << 16) | \
1170 (((const u_int8_t *)(p))[3] << 24)))
1171
1172 static int
1173 iswpaoui(const u_int8_t *frm)
1174 {
1175 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
1176 }
1177
1178 static int
1179 iswmeinfo(const u_int8_t *frm)
1180 {
1181 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1182 frm[6] == WME_INFO_OUI_SUBTYPE;
1183 }
1184
1185 static int
1186 iswmeparam(const u_int8_t *frm)
1187 {
1188 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1189 frm[6] == WME_PARAM_OUI_SUBTYPE;
1190 }
1191
1192 static const char *
1193 iename(int elemid)
1194 {
1195 switch (elemid) {
1196 case IEEE80211_ELEMID_FHPARMS: return " FHPARMS";
1197 case IEEE80211_ELEMID_CFPARMS: return " CFPARMS";
1198 case IEEE80211_ELEMID_TIM: return " TIM";
1199 case IEEE80211_ELEMID_IBSSPARMS:return " IBSSPARMS";
1200 case IEEE80211_ELEMID_CHALLENGE:return " CHALLENGE";
1201 case IEEE80211_ELEMID_PWRCNSTR: return " PWRCNSTR";
1202 case IEEE80211_ELEMID_PWRCAP: return " PWRCAP";
1203 case IEEE80211_ELEMID_TPCREQ: return " TPCREQ";
1204 case IEEE80211_ELEMID_TPCREP: return " TPCREP";
1205 case IEEE80211_ELEMID_SUPPCHAN: return " SUPPCHAN";
1206 case IEEE80211_ELEMID_CHANSWITCHANN:return " CSA";
1207 case IEEE80211_ELEMID_MEASREQ: return " MEASREQ";
1208 case IEEE80211_ELEMID_MEASREP: return " MEASREP";
1209 case IEEE80211_ELEMID_QUIET: return " QUIET";
1210 case IEEE80211_ELEMID_IBSSDFS: return " IBSSDFS";
1211 case IEEE80211_ELEMID_TPC: return " TPC";
1212 case IEEE80211_ELEMID_CCKM: return " CCKM";
1213 }
1214 return " ???";
1215 }
1216
1217 static void
1218 printies(const u_int8_t *vp, int ielen, int maxcols)
1219 {
1220 while (ielen > 0) {
1221 switch (vp[0]) {
1222 case IEEE80211_ELEMID_SSID:
1223 if (vflag)
1224 printssid(" SSID", vp, 2+vp[1], maxcols);
1225 break;
1226 case IEEE80211_ELEMID_RATES:
1227 case IEEE80211_ELEMID_XRATES:
1228 if (vflag)
1229 printrates(vp[0] == IEEE80211_ELEMID_RATES ?
1230 " RATES" : " XRATES", vp, 2+vp[1], maxcols);
1231 break;
1232 case IEEE80211_ELEMID_DSPARMS:
1233 if (vflag)
1234 printf(" DSPARMS<%u>", vp[2]);
1235 break;
1236 case IEEE80211_ELEMID_COUNTRY:
1237 if (vflag)
1238 printcountry(" COUNTRY", vp, 2+vp[1], maxcols);
1239 break;
1240 case IEEE80211_ELEMID_ERP:
1241 if (vflag)
1242 printf(" ERP<0x%x>", vp[2]);
1243 break;
1244 case IEEE80211_ELEMID_VENDOR:
1245 if (iswpaoui(vp))
1246 printwpaie(" WPA", vp, 2+vp[1], maxcols);
1247 else if (iswmeinfo(vp))
1248 printwmeinfo(" WME", vp, 2+vp[1], maxcols);
1249 else if (iswmeparam(vp))
1250 printwmeparam(" WME", vp, 2+vp[1], maxcols);
1251 else if (vflag)
1252 printie(" VEN", vp, 2+vp[1], maxcols);
1253 break;
1254 case IEEE80211_ELEMID_RSN:
1255 printrsnie(" RSN", vp, 2+vp[1], maxcols);
1256 break;
1257 default:
1258 if (vflag)
1259 printie(iename(vp[0]), vp, 2+vp[1], maxcols);
1260 break;
1261 }
1262 ielen -= 2+vp[1];
1263 vp += 2+vp[1];
1264 }
1265 }
1266
1267 static int
1268 mapgsm(u_int isrfreq, u_int isrflags)
1269 {
1270 isrfreq *= 10;
1271 if (isrflags & IEEE80211_CHAN_QUARTER)
1272 isrfreq += 5;
1273 else if (isrflags & IEEE80211_CHAN_HALF)
1274 isrfreq += 10;
1275 else
1276 isrfreq += 20;
1277 /* NB: there is no 907/20 wide but leave room */
1278 return (isrfreq - 906*10) / 5;
1279 }
1280
1281 static int
1282 mappsb(u_int isrfreq, u_int isrflags)
1283 {
1284 return 37 + ((isrfreq * 10) + ((isrfreq % 5) == 2 ? 5 : 0) - 49400) / 5;
1285 }
1286