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