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