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