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