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