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