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