Home | History | Annotate | Line # | Download | only in ifconfig
ieee80211.c revision 1.10
      1  1.10    dyoung /*	$NetBSD: ieee80211.c,v 1.10 2007/01/09 09:25:56 dyoung Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1983, 1993
      5   1.1   thorpej  *      The Regents of the University of California.  All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   thorpej  *    may be used to endorse or promote products derived from this software
     17   1.1   thorpej  *    without specific prior written permission.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   thorpej  * SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej #include <sys/cdefs.h>
     33   1.1   thorpej #ifndef lint
     34  1.10    dyoung __RCSID("$NetBSD: ieee80211.c,v 1.10 2007/01/09 09:25:56 dyoung Exp $");
     35   1.1   thorpej #endif /* not lint */
     36   1.1   thorpej 
     37   1.1   thorpej #include <sys/param.h>
     38   1.1   thorpej #include <sys/ioctl.h>
     39   1.1   thorpej #include <sys/socket.h>
     40   1.1   thorpej 
     41   1.1   thorpej #include <net/if.h>
     42   1.1   thorpej #include <net/if_ether.h>
     43   1.5    rpaulo #include <net/if_media.h>
     44   1.1   thorpej #include <net80211/ieee80211.h>
     45   1.1   thorpej #include <net80211/ieee80211_ioctl.h>
     46   1.1   thorpej 
     47   1.1   thorpej #include <ctype.h>
     48   1.1   thorpej #include <err.h>
     49   1.1   thorpej #include <netdb.h>
     50   1.1   thorpej #include <string.h>
     51   1.1   thorpej #include <stdlib.h>
     52   1.1   thorpej #include <stdio.h>
     53   1.7  christos #include <util.h>
     54   1.1   thorpej 
     55   1.1   thorpej #include "extern.h"
     56   1.1   thorpej #include "ieee80211.h"
     57   1.1   thorpej 
     58   1.5    rpaulo static void set80211(int, int, int, uint8_t *);
     59   1.5    rpaulo 
     60   1.5    rpaulo static void
     61   1.5    rpaulo set80211(int type, int val, int len, u_int8_t *data)
     62   1.5    rpaulo {
     63   1.5    rpaulo 	struct ieee80211req	ireq;
     64   1.5    rpaulo 
     65   1.5    rpaulo 	(void) memset(&ireq, 0, sizeof(ireq));
     66   1.6      elad 	estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
     67   1.5    rpaulo 	ireq.i_type = type;
     68   1.5    rpaulo 	ireq.i_val = val;
     69   1.5    rpaulo 	ireq.i_len = len;
     70   1.5    rpaulo 	ireq.i_data = data;
     71   1.5    rpaulo 	if (ioctl(s, SIOCS80211, &ireq) < 0)
     72   1.5    rpaulo 		err(1, "SIOCS80211");
     73   1.5    rpaulo }
     74   1.5    rpaulo 
     75   1.5    rpaulo void
     76   1.5    rpaulo sethidessid(const char *val, int d)
     77   1.5    rpaulo {
     78   1.5    rpaulo 	set80211(IEEE80211_IOC_HIDESSID, d, 0, NULL);
     79   1.5    rpaulo }
     80   1.5    rpaulo 
     81   1.5    rpaulo void
     82   1.5    rpaulo setapbridge(const char *val, int d)
     83   1.5    rpaulo {
     84   1.5    rpaulo 	set80211(IEEE80211_IOC_APBRIDGE, d, 0, NULL);
     85   1.5    rpaulo }
     86   1.5    rpaulo 
     87   1.5    rpaulo static enum ieee80211_opmode
     88   1.5    rpaulo get80211opmode(void)
     89   1.5    rpaulo {
     90   1.5    rpaulo 	struct ifmediareq ifmr;
     91   1.5    rpaulo 
     92   1.5    rpaulo 	(void) memset(&ifmr, 0, sizeof(ifmr));
     93   1.6      elad 	estrlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
     94   1.5    rpaulo 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
     95   1.5    rpaulo 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
     96   1.5    rpaulo 			return IEEE80211_M_IBSS;        /* XXX ahdemo */
     97   1.5    rpaulo 		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
     98   1.5    rpaulo 			return IEEE80211_M_HOSTAP;
     99   1.5    rpaulo 		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
    100   1.5    rpaulo 			return IEEE80211_M_MONITOR;
    101   1.5    rpaulo 	}
    102   1.5    rpaulo 
    103   1.5    rpaulo 	return IEEE80211_M_STA;
    104   1.5    rpaulo }
    105   1.5    rpaulo 
    106   1.1   thorpej void
    107   1.1   thorpej setifnwid(const char *val, int d)
    108   1.1   thorpej {
    109   1.1   thorpej 	struct ieee80211_nwid nwid;
    110   1.1   thorpej 	int len;
    111   1.1   thorpej 
    112   1.1   thorpej 	len = sizeof(nwid.i_nwid);
    113   1.1   thorpej 	if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
    114   1.1   thorpej 		return;
    115   1.1   thorpej 	nwid.i_len = len;
    116   1.6      elad 	estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    117   1.1   thorpej 	ifr.ifr_data = (void *)&nwid;
    118   1.1   thorpej 	if (ioctl(s, SIOCS80211NWID, &ifr) == -1)
    119   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211NWID");
    120   1.1   thorpej }
    121   1.1   thorpej 
    122   1.1   thorpej void
    123   1.1   thorpej setifbssid(const char *val, int d)
    124   1.1   thorpej {
    125   1.1   thorpej 	struct ieee80211_bssid bssid;
    126   1.1   thorpej 	struct ether_addr *ea;
    127   1.1   thorpej 
    128   1.1   thorpej 	if (d != 0) {
    129   1.1   thorpej 		/* no BSSID is especially desired */
    130   1.1   thorpej 		memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));
    131   1.1   thorpej 	} else {
    132   1.1   thorpej 		ea = ether_aton(val);
    133   1.1   thorpej 		if (ea == NULL) {
    134   1.9    dyoung 			errx(EXIT_FAILURE, "malformed BSSID: %s", val);
    135   1.1   thorpej 			return;
    136   1.1   thorpej 		}
    137   1.1   thorpej 		memcpy(&bssid.i_bssid, ea->ether_addr_octet,
    138   1.1   thorpej 		    sizeof(bssid.i_bssid));
    139   1.1   thorpej 	}
    140   1.6      elad 	estrlcpy(bssid.i_name, name, sizeof(bssid.i_name));
    141   1.1   thorpej 	if (ioctl(s, SIOCS80211BSSID, &bssid) == -1)
    142   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211BSSID");
    143   1.1   thorpej }
    144   1.1   thorpej 
    145   1.1   thorpej void
    146   1.8    dyoung setiffrag(const char *val, int d)
    147   1.8    dyoung {
    148   1.8    dyoung 	struct ieee80211req ireq;
    149   1.8    dyoung 	int thr;
    150   1.8    dyoung 
    151   1.8    dyoung 	if (d != 0)
    152   1.8    dyoung 		thr = IEEE80211_FRAG_MAX;
    153   1.8    dyoung 	else {
    154   1.8    dyoung 		thr = atoi(val);
    155   1.8    dyoung 		if (thr < IEEE80211_FRAG_MIN || thr > IEEE80211_FRAG_MAX) {
    156   1.8    dyoung 			errx(EXIT_FAILURE, "invalid fragmentation threshold: %s", val);
    157   1.8    dyoung 			return;
    158   1.8    dyoung 		}
    159   1.8    dyoung 	}
    160   1.8    dyoung 
    161   1.8    dyoung 	(void)strncpy(ireq.i_name, name, sizeof(ireq.i_name));
    162   1.8    dyoung 	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
    163   1.8    dyoung 	ireq.i_val = thr;
    164   1.8    dyoung 	if (ioctl(s, SIOCS80211, &ireq) == -1)
    165   1.8    dyoung 		err(EXIT_FAILURE, "IEEE80211_IOC_FRAGTHRESHOLD");
    166   1.8    dyoung }
    167   1.8    dyoung 
    168   1.8    dyoung void
    169   1.1   thorpej setifchan(const char *val, int d)
    170   1.1   thorpej {
    171   1.1   thorpej 	struct ieee80211chanreq channel;
    172   1.1   thorpej 	int chan;
    173   1.1   thorpej 
    174   1.1   thorpej 	if (d != 0)
    175   1.1   thorpej 		chan = IEEE80211_CHAN_ANY;
    176   1.1   thorpej 	else {
    177   1.1   thorpej 		chan = atoi(val);
    178   1.1   thorpej 		if (chan < 0 || chan > 0xffff) {
    179   1.9    dyoung 			errx(EXIT_FAILURE, "invalid channel: %s", val);
    180   1.1   thorpej 		}
    181   1.1   thorpej 	}
    182   1.1   thorpej 
    183   1.6      elad 	estrlcpy(channel.i_name, name, sizeof(channel.i_name));
    184   1.1   thorpej 	channel.i_channel = (u_int16_t) chan;
    185   1.1   thorpej 	if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
    186   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211CHANNEL");
    187   1.1   thorpej }
    188   1.1   thorpej 
    189   1.1   thorpej void
    190   1.1   thorpej setifnwkey(const char *val, int d)
    191   1.1   thorpej {
    192   1.1   thorpej 	struct ieee80211_nwkey nwkey;
    193   1.1   thorpej 	int i;
    194   1.1   thorpej 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
    195   1.1   thorpej 
    196   1.1   thorpej 	nwkey.i_wepon = IEEE80211_NWKEY_WEP;
    197   1.1   thorpej 	nwkey.i_defkid = 1;
    198   1.1   thorpej 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    199   1.1   thorpej 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
    200   1.1   thorpej 		nwkey.i_key[i].i_keydat = keybuf[i];
    201   1.1   thorpej 	}
    202   1.1   thorpej 	if (d != 0) {
    203   1.1   thorpej 		/* disable WEP encryption */
    204   1.1   thorpej 		nwkey.i_wepon = 0;
    205   1.1   thorpej 		i = 0;
    206   1.1   thorpej 	} else if (strcasecmp("persist", val) == 0) {
    207   1.1   thorpej 		/* use all values from persistent memory */
    208   1.1   thorpej 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
    209   1.1   thorpej 		nwkey.i_defkid = 0;
    210   1.1   thorpej 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
    211   1.1   thorpej 			nwkey.i_key[i].i_keylen = -1;
    212   1.1   thorpej 	} else if (strncasecmp("persist:", val, 8) == 0) {
    213   1.1   thorpej 		val += 8;
    214   1.1   thorpej 		/* program keys in persistent memory */
    215   1.1   thorpej 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
    216   1.1   thorpej 		goto set_nwkey;
    217   1.1   thorpej 	} else {
    218   1.1   thorpej   set_nwkey:
    219   1.1   thorpej 		if (isdigit((unsigned char)val[0]) && val[1] == ':') {
    220   1.1   thorpej 			/* specifying a full set of four keys */
    221   1.1   thorpej 			nwkey.i_defkid = val[0] - '0';
    222   1.1   thorpej 			val += 2;
    223   1.1   thorpej 			for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    224   1.1   thorpej 				val = get_string(val, ",", keybuf[i],
    225   1.1   thorpej 				    &nwkey.i_key[i].i_keylen);
    226   1.1   thorpej 				if (val == NULL)
    227   1.1   thorpej 					return;
    228   1.1   thorpej 			}
    229   1.1   thorpej 			if (*val != '\0') {
    230   1.9    dyoung 				errx(EXIT_FAILURE, "SIOCS80211NWKEY: too many keys.");
    231   1.1   thorpej 			}
    232   1.1   thorpej 		} else {
    233   1.1   thorpej 			val = get_string(val, NULL, keybuf[0],
    234   1.1   thorpej 			    &nwkey.i_key[0].i_keylen);
    235   1.1   thorpej 			if (val == NULL)
    236   1.1   thorpej 				return;
    237   1.1   thorpej 			i = 1;
    238   1.1   thorpej 		}
    239   1.1   thorpej 	}
    240   1.1   thorpej 	for (; i < IEEE80211_WEP_NKID; i++)
    241   1.1   thorpej 		nwkey.i_key[i].i_keylen = 0;
    242   1.6      elad 	estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
    243   1.1   thorpej 	if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
    244   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211NWKEY");
    245   1.1   thorpej }
    246   1.1   thorpej 
    247   1.1   thorpej void
    248   1.1   thorpej setifpowersave(const char *val, int d)
    249   1.1   thorpej {
    250   1.1   thorpej 	struct ieee80211_power power;
    251   1.1   thorpej 
    252   1.6      elad 	estrlcpy(power.i_name, name, sizeof(power.i_name));
    253   1.1   thorpej 	if (ioctl(s, SIOCG80211POWER, &power) == -1) {
    254   1.9    dyoung 		err(EXIT_FAILURE, "SIOCG80211POWER");
    255   1.1   thorpej 	}
    256   1.1   thorpej 
    257   1.1   thorpej 	power.i_enabled = d;
    258   1.1   thorpej 	if (ioctl(s, SIOCS80211POWER, &power) == -1)
    259   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211POWER");
    260   1.1   thorpej }
    261   1.1   thorpej 
    262   1.1   thorpej void
    263   1.1   thorpej setifpowersavesleep(const char *val, int d)
    264   1.1   thorpej {
    265   1.1   thorpej 	struct ieee80211_power power;
    266   1.1   thorpej 
    267   1.6      elad 	estrlcpy(power.i_name, name, sizeof(power.i_name));
    268   1.1   thorpej 	if (ioctl(s, SIOCG80211POWER, &power) == -1) {
    269   1.9    dyoung 		err(EXIT_FAILURE, "SIOCG80211POWER");
    270   1.1   thorpej 	}
    271   1.1   thorpej 
    272   1.1   thorpej 	power.i_maxsleep = atoi(val);
    273   1.1   thorpej 	if (ioctl(s, SIOCS80211POWER, &power) == -1)
    274   1.9    dyoung 		err(EXIT_FAILURE, "SIOCS80211POWER");
    275   1.1   thorpej }
    276   1.1   thorpej 
    277   1.1   thorpej void
    278   1.1   thorpej ieee80211_statistics(void)
    279   1.1   thorpej {
    280   1.1   thorpej 	struct ieee80211_stats stats;
    281   1.1   thorpej 
    282   1.1   thorpej 	memset(&ifr, 0, sizeof(ifr));
    283   1.3    dyoung 	ifr.ifr_buflen = sizeof(stats);
    284   1.3    dyoung 	ifr.ifr_buf = (caddr_t)&stats;
    285   1.6      elad 	estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    286   1.1   thorpej 	if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
    287   1.1   thorpej 	    (caddr_t)&ifr) == -1)
    288   1.1   thorpej 		return;
    289   1.2    dyoung #define	STAT_PRINT(_member, _desc)	\
    290   1.2    dyoung 	printf("\t" _desc ": %" PRIu32 "\n", stats._member)
    291   1.1   thorpej 
    292   1.2    dyoung 	STAT_PRINT(is_rx_badversion, "rx frame with bad version");
    293   1.2    dyoung 	STAT_PRINT(is_rx_tooshort, "rx frame too short");
    294   1.2    dyoung 	STAT_PRINT(is_rx_wrongbss, "rx from wrong bssid");
    295   1.2    dyoung 	STAT_PRINT(is_rx_dup, "rx discard 'cuz dup");
    296   1.2    dyoung 	STAT_PRINT(is_rx_wrongdir, "rx w/ wrong direction");
    297   1.2    dyoung 	STAT_PRINT(is_rx_mcastecho, "rx discard 'cuz mcast echo");
    298   1.2    dyoung 	STAT_PRINT(is_rx_notassoc, "rx discard 'cuz sta !assoc");
    299   1.2    dyoung 	STAT_PRINT(is_rx_noprivacy, "rx w/ wep but privacy off");
    300   1.2    dyoung 	STAT_PRINT(is_rx_unencrypted, "rx w/o wep and privacy on");
    301   1.2    dyoung 	STAT_PRINT(is_rx_wepfail, "rx wep processing failed");
    302   1.2    dyoung 	STAT_PRINT(is_rx_decap, "rx decapsulation failed");
    303   1.2    dyoung 	STAT_PRINT(is_rx_mgtdiscard, "rx discard mgt frames");
    304   1.2    dyoung 	STAT_PRINT(is_rx_ctl, "rx discard ctrl frames");
    305   1.2    dyoung 	STAT_PRINT(is_rx_beacon, "rx beacon frames");
    306   1.2    dyoung 	STAT_PRINT(is_rx_rstoobig, "rx rate set truncated");
    307  1.10    dyoung 	STAT_PRINT(is_rx_elem_missing, "rx required element missing");
    308   1.2    dyoung 	STAT_PRINT(is_rx_elem_toobig, "rx element too big");
    309   1.2    dyoung 	STAT_PRINT(is_rx_elem_toosmall, "rx element too small");
    310   1.2    dyoung 	STAT_PRINT(is_rx_elem_unknown, "rx element unknown");
    311   1.2    dyoung 	STAT_PRINT(is_rx_badchan, "rx frame w/ invalid chan");
    312   1.2    dyoung 	STAT_PRINT(is_rx_chanmismatch, "rx frame chan mismatch");
    313   1.2    dyoung 	STAT_PRINT(is_rx_nodealloc, "rx frame dropped");
    314   1.2    dyoung 	STAT_PRINT(is_rx_ssidmismatch, "rx frame ssid mismatch ");
    315   1.2    dyoung 	STAT_PRINT(is_rx_auth_unsupported, "rx w/ unsupported auth alg");
    316   1.2    dyoung 	STAT_PRINT(is_rx_auth_fail, "rx sta auth failure");
    317   1.2    dyoung 	STAT_PRINT(is_rx_auth_countermeasures, "rx auth discard 'cuz CM");
    318   1.2    dyoung 	STAT_PRINT(is_rx_assoc_bss, "rx assoc from wrong bssid");
    319   1.2    dyoung 	STAT_PRINT(is_rx_assoc_notauth, "rx assoc w/o auth");
    320   1.2    dyoung 	STAT_PRINT(is_rx_assoc_capmismatch, "rx assoc w/ cap mismatch");
    321   1.2    dyoung 	STAT_PRINT(is_rx_assoc_norate, "rx assoc w/ no rate match");
    322   1.2    dyoung 	STAT_PRINT(is_rx_assoc_badwpaie, "rx assoc w/ bad WPA IE");
    323   1.2    dyoung 	STAT_PRINT(is_rx_deauth, "rx deauthentication");
    324   1.2    dyoung 	STAT_PRINT(is_rx_disassoc, "rx disassociation");
    325   1.2    dyoung 	STAT_PRINT(is_rx_badsubtype, "rx frame w/ unknown subtyp");
    326   1.2    dyoung 	STAT_PRINT(is_rx_nobuf, "rx failed for lack of buf");
    327   1.2    dyoung 	STAT_PRINT(is_rx_decryptcrc, "rx decrypt failed on crc");
    328   1.2    dyoung 	STAT_PRINT(is_rx_ahdemo_mgt, "rx discard ahdemo mgt fram");
    329   1.2    dyoung 	STAT_PRINT(is_rx_bad_auth, "rx bad auth request");
    330   1.2    dyoung 	STAT_PRINT(is_rx_unauth, "rx on unauthorized port");
    331   1.2    dyoung 	STAT_PRINT(is_rx_badkeyid, "rx w/ incorrect keyid");
    332   1.2    dyoung 	STAT_PRINT(is_rx_ccmpreplay, "rx seq# violation (CCMP)");
    333   1.2    dyoung 	STAT_PRINT(is_rx_ccmpformat, "rx format bad (CCMP)");
    334   1.2    dyoung 	STAT_PRINT(is_rx_ccmpmic, "rx MIC check failed (CCMP)");
    335   1.2    dyoung 	STAT_PRINT(is_rx_tkipreplay, "rx seq# violation (TKIP)");
    336   1.2    dyoung 	STAT_PRINT(is_rx_tkipformat, "rx format bad (TKIP)");
    337   1.2    dyoung 	STAT_PRINT(is_rx_tkipmic, "rx MIC check failed (TKIP)");
    338   1.2    dyoung 	STAT_PRINT(is_rx_tkipicv, "rx ICV check failed (TKIP)");
    339   1.2    dyoung 	STAT_PRINT(is_rx_badcipher, "rx failed 'cuz key type");
    340   1.2    dyoung 	STAT_PRINT(is_rx_nocipherctx, "rx failed 'cuz key !setup");
    341   1.2    dyoung 	STAT_PRINT(is_rx_acl, "rx discard 'cuz acl policy");
    342   1.2    dyoung 
    343   1.2    dyoung 	STAT_PRINT(is_tx_nobuf, "tx failed for lack of buf");
    344   1.2    dyoung 	STAT_PRINT(is_tx_nonode, "tx failed for no node");
    345   1.2    dyoung 	STAT_PRINT(is_tx_unknownmgt, "tx of unknown mgt frame");
    346   1.2    dyoung 	STAT_PRINT(is_tx_badcipher, "tx failed 'cuz key type");
    347   1.2    dyoung 	STAT_PRINT(is_tx_nodefkey, "tx failed 'cuz no defkey");
    348   1.2    dyoung 	STAT_PRINT(is_tx_noheadroom, "tx failed 'cuz no space");
    349  1.10    dyoung 	STAT_PRINT(is_tx_fragframes, "tx frames fragmented");
    350  1.10    dyoung 	STAT_PRINT(is_tx_frags, "tx fragments created");
    351   1.2    dyoung 
    352   1.2    dyoung 	STAT_PRINT(is_scan_active, "active scans started");
    353   1.2    dyoung 	STAT_PRINT(is_scan_passive, "passive scans started");
    354   1.2    dyoung 	STAT_PRINT(is_node_timeout, "nodes timed out inactivity");
    355   1.2    dyoung 	STAT_PRINT(is_crypto_nomem, "no memory for crypto ctx");
    356   1.2    dyoung 	STAT_PRINT(is_crypto_tkip, "tkip crypto done in s/w");
    357   1.2    dyoung 	STAT_PRINT(is_crypto_tkipenmic, "tkip en-MIC done in s/w");
    358   1.2    dyoung 	STAT_PRINT(is_crypto_tkipdemic, "tkip de-MIC done in s/w");
    359   1.2    dyoung 	STAT_PRINT(is_crypto_tkipcm, "tkip counter measures");
    360   1.2    dyoung 	STAT_PRINT(is_crypto_ccmp, "ccmp crypto done in s/w");
    361   1.2    dyoung 	STAT_PRINT(is_crypto_wep, "wep crypto done in s/w");
    362   1.2    dyoung 	STAT_PRINT(is_crypto_setkey_cipher, "cipher rejected key");
    363   1.2    dyoung 	STAT_PRINT(is_crypto_setkey_nokey, "no key index for setkey");
    364   1.2    dyoung 	STAT_PRINT(is_crypto_delkey, "driver key delete failed");
    365   1.2    dyoung 	STAT_PRINT(is_crypto_badcipher, "unknown cipher");
    366   1.2    dyoung 	STAT_PRINT(is_crypto_nocipher, "cipher not available");
    367   1.2    dyoung 	STAT_PRINT(is_crypto_attachfail, "cipher attach failed");
    368   1.2    dyoung 	STAT_PRINT(is_crypto_swfallback, "cipher fallback to s/w");
    369   1.2    dyoung 	STAT_PRINT(is_crypto_keyfail, "driver key alloc failed");
    370   1.2    dyoung 	STAT_PRINT(is_crypto_enmicfail, "en-MIC failed");
    371   1.2    dyoung 	STAT_PRINT(is_ibss_capmismatch, "merge failed-cap mismatch");
    372   1.2    dyoung 	STAT_PRINT(is_ibss_norate, "merge failed-rate mismatch");
    373   1.2    dyoung 	STAT_PRINT(is_ps_unassoc, "ps-poll for unassoc. sta");
    374   1.2    dyoung 	STAT_PRINT(is_ps_badaid, "ps-poll w/ incorrect aid");
    375   1.2    dyoung 	STAT_PRINT(is_ps_qempty, "ps-poll w/ nothing to send");
    376  1.10    dyoung 	STAT_PRINT(is_ff_badhdr, "fast frame rx'd w/ bad hdr");
    377  1.10    dyoung 	STAT_PRINT(is_ff_tooshort, "fast frame rx decap error");
    378  1.10    dyoung 	STAT_PRINT(is_ff_split, "fast frame rx split error");
    379  1.10    dyoung 	STAT_PRINT(is_ff_decap, "fast frames decap'd");
    380  1.10    dyoung 	STAT_PRINT(is_ff_encap, "fast frames encap'd for tx");
    381  1.10    dyoung 	STAT_PRINT(is_rx_badbintval, "rx frame w/ bogus bintval");
    382   1.1   thorpej }
    383   1.1   thorpej 
    384   1.1   thorpej void
    385   1.1   thorpej ieee80211_status(void)
    386   1.1   thorpej {
    387   1.1   thorpej 	int i, nwkey_verbose;
    388   1.1   thorpej 	struct ieee80211_nwid nwid;
    389   1.1   thorpej 	struct ieee80211_nwkey nwkey;
    390   1.1   thorpej 	struct ieee80211_power power;
    391   1.1   thorpej 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
    392   1.1   thorpej 	struct ieee80211_bssid bssid;
    393   1.1   thorpej 	struct ieee80211chanreq channel;
    394   1.5    rpaulo 	struct ieee80211req ireq;
    395   1.1   thorpej 	struct ether_addr ea;
    396   1.1   thorpej 	static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
    397   1.5    rpaulo 	enum ieee80211_opmode opmode = get80211opmode();
    398   1.5    rpaulo 	extern int vflag;
    399   1.1   thorpej 
    400   1.1   thorpej 	memset(&ifr, 0, sizeof(ifr));
    401   1.1   thorpej 	ifr.ifr_data = (void *)&nwid;
    402   1.6      elad 	estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    403   1.1   thorpej 	if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
    404   1.1   thorpej 		return;
    405   1.1   thorpej 	if (nwid.i_len > IEEE80211_NWID_LEN) {
    406   1.9    dyoung 		errx(EXIT_FAILURE, "SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
    407   1.1   thorpej 	}
    408   1.1   thorpej 	printf("\tssid ");
    409   1.1   thorpej 	print_string(nwid.i_nwid, nwid.i_len);
    410   1.5    rpaulo 
    411   1.5    rpaulo 	if (opmode == IEEE80211_M_HOSTAP) {
    412   1.6      elad 		estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
    413   1.5    rpaulo 		ireq.i_type = IEEE80211_IOC_HIDESSID;
    414   1.5    rpaulo 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
    415   1.5    rpaulo                         if (ireq.i_val)
    416   1.5    rpaulo                                 printf(" [hidden]");
    417   1.5    rpaulo                         else if (vflag)
    418   1.5    rpaulo                                 printf(" [shown]");
    419   1.5    rpaulo                 }
    420   1.5    rpaulo 
    421   1.5    rpaulo 		ireq.i_type = IEEE80211_IOC_APBRIDGE;
    422   1.5    rpaulo 		if (ioctl(s, SIOCG80211, &ireq) != -1) {
    423   1.5    rpaulo 			if (ireq.i_val)
    424   1.5    rpaulo 				printf(" apbridge");
    425   1.5    rpaulo 			else if (vflag)
    426   1.5    rpaulo 				printf(" -apbridge");
    427   1.5    rpaulo 		}
    428   1.5    rpaulo         }
    429   1.5    rpaulo 
    430   1.8    dyoung 	estrlcpy(ireq.i_name, name, sizeof(ireq.i_name));
    431   1.8    dyoung 	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
    432   1.8    dyoung 	if (ioctl(s, SIOCG80211, &ireq) == -1)
    433   1.8    dyoung 		;
    434   1.8    dyoung 	else if (ireq.i_val < IEEE80211_FRAG_MAX)
    435   1.8    dyoung 		printf(" frag %d", ireq.i_val);
    436   1.8    dyoung 	else if (vflag)
    437   1.8    dyoung 		printf(" -frag");
    438   1.8    dyoung 
    439   1.1   thorpej 	memset(&nwkey, 0, sizeof(nwkey));
    440   1.6      elad 	estrlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
    441   1.1   thorpej 	/* show nwkey only when WEP is enabled */
    442   1.1   thorpej 	if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
    443   1.1   thorpej 	    nwkey.i_wepon == 0) {
    444   1.1   thorpej 		printf("\n");
    445   1.1   thorpej 		goto skip_wep;
    446   1.1   thorpej 	}
    447   1.1   thorpej 
    448   1.1   thorpej 	printf(" nwkey ");
    449   1.1   thorpej 	/* try to retrieve WEP keys */
    450   1.1   thorpej 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    451   1.1   thorpej 		nwkey.i_key[i].i_keydat = keybuf[i];
    452   1.1   thorpej 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
    453   1.1   thorpej 	}
    454   1.1   thorpej 	if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
    455   1.1   thorpej 		printf("*****");
    456   1.1   thorpej 	} else {
    457   1.1   thorpej 		nwkey_verbose = 0;
    458   1.1   thorpej 		/* check to see non default key or multiple keys defined */
    459   1.1   thorpej 		if (nwkey.i_defkid != 1) {
    460   1.1   thorpej 			nwkey_verbose = 1;
    461   1.1   thorpej 		} else {
    462   1.1   thorpej 			for (i = 1; i < IEEE80211_WEP_NKID; i++) {
    463   1.1   thorpej 				if (nwkey.i_key[i].i_keylen != 0) {
    464   1.1   thorpej 					nwkey_verbose = 1;
    465   1.1   thorpej 					break;
    466   1.1   thorpej 				}
    467   1.1   thorpej 			}
    468   1.1   thorpej 		}
    469   1.1   thorpej 		/* check extra ambiguity with keywords */
    470   1.1   thorpej 		if (!nwkey_verbose) {
    471   1.1   thorpej 			if (nwkey.i_key[0].i_keylen >= 2 &&
    472   1.1   thorpej 			    isdigit(nwkey.i_key[0].i_keydat[0]) &&
    473   1.1   thorpej 			    nwkey.i_key[0].i_keydat[1] == ':')
    474   1.1   thorpej 				nwkey_verbose = 1;
    475   1.1   thorpej 			else if (nwkey.i_key[0].i_keylen >= 7 &&
    476   1.4  christos 			    strncasecmp("persist",
    477   1.4  christos 			    (const char *)nwkey.i_key[0].i_keydat, 7) == 0)
    478   1.1   thorpej 				nwkey_verbose = 1;
    479   1.1   thorpej 		}
    480   1.1   thorpej 		if (nwkey_verbose)
    481   1.1   thorpej 			printf("%d:", nwkey.i_defkid);
    482   1.1   thorpej 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    483   1.1   thorpej 			if (i > 0)
    484   1.1   thorpej 				printf(",");
    485   1.1   thorpej 			if (nwkey.i_key[i].i_keylen < 0)
    486   1.1   thorpej 				printf("persist");
    487   1.1   thorpej 			else
    488   1.1   thorpej 				print_string(nwkey.i_key[i].i_keydat,
    489   1.1   thorpej 				    nwkey.i_key[i].i_keylen);
    490   1.1   thorpej 			if (!nwkey_verbose)
    491   1.1   thorpej 				break;
    492   1.1   thorpej 		}
    493   1.1   thorpej 	}
    494   1.1   thorpej 	printf("\n");
    495   1.1   thorpej 
    496   1.1   thorpej  skip_wep:
    497   1.6      elad 	estrlcpy(power.i_name, name, sizeof(power.i_name));
    498   1.1   thorpej 	if (ioctl(s, SIOCG80211POWER, &power) == -1)
    499   1.1   thorpej 		goto skip_power;
    500   1.1   thorpej 	printf("\tpowersave ");
    501   1.1   thorpej 	if (power.i_enabled)
    502   1.1   thorpej 		printf("on (%dms sleep)", power.i_maxsleep);
    503   1.1   thorpej 	else
    504   1.1   thorpej 		printf("off");
    505   1.1   thorpej 	printf("\n");
    506   1.1   thorpej 
    507   1.1   thorpej  skip_power:
    508   1.6      elad 	estrlcpy(bssid.i_name, name, sizeof(bssid.i_name));
    509   1.1   thorpej 	if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
    510   1.1   thorpej 		return;
    511   1.6      elad 	estrlcpy(channel.i_name, name, sizeof(channel.i_name));
    512   1.1   thorpej 	if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
    513   1.1   thorpej 		return;
    514   1.1   thorpej 	if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
    515   1.1   thorpej 		if (channel.i_channel != (u_int16_t)-1)
    516   1.1   thorpej 			printf("\tchan %d\n", channel.i_channel);
    517   1.1   thorpej 	} else {
    518   1.1   thorpej 		memcpy(ea.ether_addr_octet, bssid.i_bssid,
    519   1.1   thorpej 		    sizeof(ea.ether_addr_octet));
    520   1.1   thorpej 		printf("\tbssid %s", ether_ntoa(&ea));
    521   1.1   thorpej 		if (channel.i_channel != IEEE80211_CHAN_ANY)
    522   1.1   thorpej 			printf(" chan %d", channel.i_channel);
    523   1.1   thorpej 		printf("\n");
    524   1.1   thorpej 	}
    525   1.1   thorpej }
    526