Home | History | Annotate | Line # | Download | only in net80211
ieee80211_ioctl.c revision 1.19
      1 /*	$NetBSD: ieee80211_ioctl.c,v 1.19 2005/06/22 06:16:02 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_ioctl.c,v 1.18 2005/01/24 19:32:09 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_ioctl.c,v 1.19 2005/06/22 06:16:02 dyoung Exp $");
     40 #endif
     41 
     42 /*
     43  * IEEE 802.11 ioctl support (FreeBSD-specific)
     44  */
     45 
     46 #include "opt_inet.h"
     47 
     48 #include <sys/endian.h>
     49 #include <sys/param.h>
     50 #include <sys/kernel.h>
     51 #include <sys/socket.h>
     52 #include <sys/sockio.h>
     53 #include <sys/systm.h>
     54 #include <sys/proc.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_arp.h>
     58 #include <net/if_media.h>
     59 #include <net/if_ether.h>
     60 
     61 #ifdef INET
     62 #include <netinet/in.h>
     63 #include <netinet/if_inarp.h>
     64 #endif
     65 
     66 #include <net80211/ieee80211_var.h>
     67 #include <net80211/ieee80211_ioctl.h>
     68 
     69 #include <dev/ic/wi_ieee.h>
     70 
     71 #define	IS_UP(_ic) \
     72 	(((_ic)->ic_ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
     73 #define	IS_UP_AUTO(_ic) \
     74 	(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
     75 
     76 /*
     77  * XXX
     78  * Wireless LAN specific configuration interface, which is compatible
     79  * with wicontrol(8).
     80  */
     81 
     82 struct wi_read_ap_args {
     83 	int	i;		/* result count */
     84 	struct wi_apinfo *ap;	/* current entry in result buffer */
     85 	caddr_t	max;		/* result buffer bound */
     86 };
     87 
     88 static void
     89 wi_read_ap_result(void *arg, struct ieee80211_node *ni)
     90 {
     91 	struct ieee80211com *ic = ni->ni_ic;
     92 	struct wi_read_ap_args *sa = arg;
     93 	struct wi_apinfo *ap = sa->ap;
     94 	struct ieee80211_rateset *rs;
     95 	int j;
     96 
     97 	if ((caddr_t)(ap + 1) > sa->max)
     98 		return;
     99 	memset(ap, 0, sizeof(struct wi_apinfo));
    100 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    101 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
    102 		ap->namelen = ic->ic_des_esslen;
    103 		if (ic->ic_des_esslen)
    104 			memcpy(ap->name, ic->ic_des_essid,
    105 			    ic->ic_des_esslen);
    106 	} else {
    107 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
    108 		ap->namelen = ni->ni_esslen;
    109 		if (ni->ni_esslen)
    110 			memcpy(ap->name, ni->ni_essid,
    111 			    ni->ni_esslen);
    112 	}
    113 	ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
    114 	ap->signal = ic->ic_node_getrssi(ni);
    115 	ap->capinfo = ni->ni_capinfo;
    116 	ap->interval = ni->ni_intval;
    117 	rs = &ni->ni_rates;
    118 	for (j = 0; j < rs->rs_nrates; j++) {
    119 		if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
    120 			ap->rate = (rs->rs_rates[j] &
    121 			    IEEE80211_RATE_VAL) * 5; /* XXX */
    122 		}
    123 	}
    124 	sa->i++;
    125 	sa->ap++;
    126 }
    127 
    128 struct wi_read_prism2_args {
    129 	int	i;		/* result count */
    130 	struct wi_scan_res *res;/* current entry in result buffer */
    131 	caddr_t	max;		/* result buffer bound */
    132 };
    133 
    134 #if 0
    135 static void
    136 wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
    137 {
    138 	struct ieee80211com *ic = ni->ni_ic;
    139 	struct wi_read_prism2_args *sa = arg;
    140 	struct wi_scan_res *res = sa->res;
    141 
    142 	if ((caddr_t)(res + 1) > sa->max)
    143 		return;
    144 	res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
    145 	res->wi_noise = 0;
    146 	res->wi_signal = ic->ic_node_getrssi(ni);
    147 	IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
    148 	res->wi_interval = ni->ni_intval;
    149 	res->wi_capinfo = ni->ni_capinfo;
    150 	res->wi_ssid_len = ni->ni_esslen;
    151 	memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
    152 	/* NB: assumes wi_srates holds <= ni->ni_rates */
    153 	memcpy(res->wi_srates, ni->ni_rates.rs_rates,
    154 		sizeof(res->wi_srates));
    155 	if (ni->ni_rates.rs_nrates < 10)
    156 		res->wi_srates[ni->ni_rates.rs_nrates] = 0;
    157 	res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
    158 	res->wi_rsvd = 0;
    159 
    160 	sa->i++;
    161 	sa->res++;
    162 }
    163 
    164 struct wi_read_sigcache_args {
    165 	int	i;		/* result count */
    166 	struct wi_sigcache *wsc;/* current entry in result buffer */
    167 	caddr_t	max;		/* result buffer bound */
    168 };
    169 
    170 static void
    171 wi_read_sigcache(void *arg, struct ieee80211_node *ni)
    172 {
    173 	struct ieee80211com *ic = ni->ni_ic;
    174 	struct wi_read_sigcache_args *sa = arg;
    175 	struct wi_sigcache *wsc = sa->wsc;
    176 
    177 	if ((caddr_t)(wsc + 1) > sa->max)
    178 		return;
    179 	memset(wsc, 0, sizeof(struct wi_sigcache));
    180 	IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
    181 	wsc->signal = ic->ic_node_getrssi(ni);
    182 
    183 	sa->wsc++;
    184 	sa->i++;
    185 }
    186 #endif
    187 
    188 int
    189 ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
    190 {
    191 	struct ifnet *ifp = ic->ic_ifp;
    192 	int i, j, error;
    193 	struct ifreq *ifr = (struct ifreq *)data;
    194 	struct wi_req wreq;
    195 	struct wi_ltv_keys *keys;
    196 
    197 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
    198 	if (error)
    199 		return error;
    200 	wreq.wi_len = 0;
    201 	switch (wreq.wi_type) {
    202 	case WI_RID_SERIALNO:
    203 	case WI_RID_STA_IDENTITY:
    204 		/* nothing appropriate */
    205 		break;
    206 	case WI_RID_NODENAME:
    207 		strlcpy((char *)&wreq.wi_val[1], hostname,
    208 		    sizeof(wreq.wi_val) - sizeof(wreq.wi_val[0]));
    209 		wreq.wi_val[0] = htole16(strlen(hostname));
    210 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
    211 		break;
    212 	case WI_RID_CURRENT_SSID:
    213 		if (ic->ic_state != IEEE80211_S_RUN) {
    214 			wreq.wi_val[0] = 0;
    215 			wreq.wi_len = 1;
    216 			break;
    217 		}
    218 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
    219 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
    220 		    ic->ic_bss->ni_esslen);
    221 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
    222 		break;
    223 	case WI_RID_OWN_SSID:
    224 	case WI_RID_DESIRED_SSID:
    225 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
    226 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
    227 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
    228 		break;
    229 	case WI_RID_CURRENT_BSSID:
    230 		if (ic->ic_state == IEEE80211_S_RUN)
    231 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
    232 		else
    233 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
    234 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    235 		break;
    236 	case WI_RID_CHANNEL_LIST:
    237 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
    238 		/*
    239 		 * Since channel 0 is not available for DS, channel 1
    240 		 * is assigned to LSB on WaveLAN.
    241 		 */
    242 		if (ic->ic_phytype == IEEE80211_T_DS)
    243 			i = 1;
    244 		else
    245 			i = 0;
    246 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
    247 			if (isset(ic->ic_chan_active, i)) {
    248 				setbit((u_int8_t *)wreq.wi_val, j);
    249 				wreq.wi_len = j / 16 + 1;
    250 			}
    251 		break;
    252 	case WI_RID_OWN_CHNL:
    253 		wreq.wi_val[0] = htole16(
    254 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
    255 		wreq.wi_len = 1;
    256 		break;
    257 	case WI_RID_CURRENT_CHAN:
    258 		wreq.wi_val[0] = htole16(
    259 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
    260 		wreq.wi_len = 1;
    261 		break;
    262 	case WI_RID_COMMS_QUALITY:
    263 		wreq.wi_val[0] = 0;				/* quality */
    264 		wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
    265 		wreq.wi_val[2] = 0;				/* noise */
    266 		wreq.wi_len = 3;
    267 		break;
    268 	case WI_RID_PROMISC:
    269 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
    270 		wreq.wi_len = 1;
    271 		break;
    272 	case WI_RID_PORTTYPE:
    273 		wreq.wi_val[0] = htole16(ic->ic_opmode);
    274 		wreq.wi_len = 1;
    275 		break;
    276 	case WI_RID_MAC_NODE:
    277 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
    278 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    279 		break;
    280 	case WI_RID_TX_RATE:
    281 		if (ic->ic_fixed_rate == -1)
    282 			wreq.wi_val[0] = 0;	/* auto */
    283 		else
    284 			wreq.wi_val[0] = htole16(
    285 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
    286 			    IEEE80211_RATE_VAL) / 2);
    287 		wreq.wi_len = 1;
    288 		break;
    289 	case WI_RID_CUR_TX_RATE:
    290 		wreq.wi_val[0] = htole16(
    291 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
    292 		    IEEE80211_RATE_VAL) / 2);
    293 		wreq.wi_len = 1;
    294 		break;
    295 	case WI_RID_FRAG_THRESH:
    296 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
    297 		wreq.wi_len = 1;
    298 		break;
    299 	case WI_RID_RTS_THRESH:
    300 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
    301 		wreq.wi_len = 1;
    302 		break;
    303 	case WI_RID_CREATE_IBSS:
    304 		wreq.wi_val[0] =
    305 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
    306 		wreq.wi_len = 1;
    307 		break;
    308 	case WI_RID_MICROWAVE_OVEN:
    309 		wreq.wi_val[0] = 0;	/* no ... not supported */
    310 		wreq.wi_len = 1;
    311 		break;
    312 	case WI_RID_ROAMING_MODE:
    313 		wreq.wi_val[0] = htole16(ic->ic_roaming);	/* XXX map */
    314 		wreq.wi_len = 1;
    315 		break;
    316 	case WI_RID_SYSTEM_SCALE:
    317 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
    318 		wreq.wi_len = 1;
    319 		break;
    320 	case WI_RID_PM_ENABLED:
    321 		wreq.wi_val[0] =
    322 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    323 		wreq.wi_len = 1;
    324 		break;
    325 	case WI_RID_MAX_SLEEP:
    326 		wreq.wi_val[0] = htole16(ic->ic_lintval);
    327 		wreq.wi_len = 1;
    328 		break;
    329 	case WI_RID_CUR_BEACON_INT:
    330 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
    331 		wreq.wi_len = 1;
    332 		break;
    333 	case WI_RID_WEP_AVAIL:
    334 		wreq.wi_val[0] = htole16(1);	/* always available */
    335 		wreq.wi_len = 1;
    336 		break;
    337 	case WI_RID_CNFAUTHMODE:
    338 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
    339 		wreq.wi_len = 1;
    340 		break;
    341 	case WI_RID_ENCRYPTION:
    342 		wreq.wi_val[0] =
    343 		    htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
    344 		wreq.wi_len = 1;
    345 		break;
    346 	case WI_RID_TX_CRYPT_KEY:
    347 		wreq.wi_val[0] = htole16(ic->ic_def_txkey);
    348 		wreq.wi_len = 1;
    349 		break;
    350 	case WI_RID_DEFLT_CRYPT_KEYS:
    351 		keys = (struct wi_ltv_keys *)&wreq;
    352 		/* do not show keys to non-root user */
    353 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    354 		if (error) {
    355 			memset(keys, 0, sizeof(*keys));
    356 			error = 0;
    357 			break;
    358 		}
    359 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    360 			keys->wi_keys[i].wi_keylen =
    361 			    htole16(ic->ic_nw_keys[i].wk_keylen);
    362 			memcpy(keys->wi_keys[i].wi_keydat,
    363 			    ic->ic_nw_keys[i].wk_key,
    364 			    ic->ic_nw_keys[i].wk_keylen);
    365 		}
    366 		wreq.wi_len = sizeof(*keys) / 2;
    367 		break;
    368 	case WI_RID_MAX_DATALEN:
    369 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
    370 		wreq.wi_len = 1;
    371 		break;
    372 	case WI_RID_DBM_ADJUST:
    373 		/* not supported, we just pass rssi value from driver. */
    374 		break;
    375 	case WI_RID_IFACE_STATS:
    376 		/* XXX: should be implemented in lower drivers */
    377 		break;
    378 	case WI_RID_READ_APS:
    379 		/*
    380 		 * Don't return results until active scan completes.
    381 		 */
    382 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
    383 			struct wi_read_ap_args args;
    384 
    385 			args.i = 0;
    386 			args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
    387 			args.max = (void *)(&wreq + 1);
    388 			ieee80211_iterate_nodes(&ic->ic_scan,
    389 				wi_read_ap_result, &args);
    390 			memcpy(wreq.wi_val, &args.i, sizeof(args.i));
    391 			wreq.wi_len = (sizeof(int) +
    392 				sizeof(struct wi_apinfo) * args.i) / 2;
    393 		} else
    394 			error = EINPROGRESS;
    395 		break;
    396 #if 0
    397 	case WI_RID_SCAN_RES:			/* compatibility interface */
    398 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
    399 			struct wi_read_prism2_args args;
    400 			struct wi_scan_p2_hdr *p2;
    401 
    402 			/* NB: use Prism2 format so we can include rate info */
    403 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
    404 			args.i = 0;
    405 			args.res = (void *)&p2[1];
    406 			args.max = (void *)(&wreq + 1);
    407 			ieee80211_iterate_nodes(&ic->ic_scan,
    408 				wi_read_prism2_result, &args);
    409 			p2->wi_rsvd = 0;
    410 			p2->wi_reason = args.i;
    411 			wreq.wi_len = (sizeof(*p2) +
    412 				sizeof(struct wi_scan_res) * args.i) / 2;
    413 		} else
    414 			error = EINPROGRESS;
    415 		break;
    416 	case WI_RID_READ_CACHE: {
    417 		struct wi_read_sigcache_args args;
    418 		args.i = 0;
    419 		args.wsc = (struct wi_sigcache *) wreq.wi_val;
    420 		args.max = (void *)(&wreq + 1);
    421 		ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
    422 		wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
    423 		break;
    424 	}
    425 #endif
    426 	default:
    427 		error = EINVAL;
    428 		break;
    429 	}
    430 	if (error == 0) {
    431 		wreq.wi_len++;
    432 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
    433 	}
    434 	return error;
    435 }
    436 
    437 static int
    438 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    439 {
    440 #define	IEEERATE(_ic,_m,_i) \
    441 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    442 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    443 	for (i = 0; i < nrates; i++)
    444 		if (IEEERATE(ic, mode, i) == rate)
    445 			return i;
    446 	return -1;
    447 #undef IEEERATE
    448 }
    449 
    450 /*
    451  * Prepare to do a user-initiated scan for AP's.  If no
    452  * current/default channel is setup or the current channel
    453  * is invalid then pick the first available channel from
    454  * the active list as the place to start the scan.
    455  */
    456 static int
    457 ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
    458 {
    459 	int i;
    460 
    461 	/*
    462 	 * XXX don't permit a scan to be started unless we
    463 	 * know the device is ready.  For the moment this means
    464 	 * the device is marked up as this is the required to
    465 	 * initialize the hardware.  It would be better to permit
    466 	 * scanning prior to being up but that'll require some
    467 	 * changes to the infrastructure.
    468 	 */
    469 	if (!IS_UP(ic))
    470 		return EINVAL;
    471 	if (ic->ic_ibss_chan == NULL ||
    472 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    473 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    474 			if (isset(chanlist, i)) {
    475 				ic->ic_ibss_chan = &ic->ic_channels[i];
    476 				goto found;
    477 			}
    478 		return EINVAL;			/* no active channels */
    479 found:
    480 		;
    481 	}
    482 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
    483 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
    484 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
    485 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
    486 	/*
    487 	 * We force the state to INIT before calling ieee80211_new_state
    488 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
    489 	 * altering the current state but that's not possible right now.
    490 	 */
    491 	/* XXX handle proberequest case */
    492 	ic->ic_state = IEEE80211_S_INIT;	/* XXX bypass state machine */
    493 	return 0;
    494 }
    495 
    496 int
    497 ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
    498 {
    499 	struct ifnet *ifp = ic->ic_ifp;
    500 	int i, j, len, error, rate;
    501 	struct ifreq *ifr = (struct ifreq *)data;
    502 	struct wi_ltv_keys *keys;
    503 	struct wi_req wreq;
    504 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
    505 
    506 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
    507 	if (error)
    508 		return error;
    509 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
    510 	switch (wreq.wi_type) {
    511 	case WI_RID_SERIALNO:
    512 	case WI_RID_NODENAME:
    513 		return EPERM;
    514 	case WI_RID_CURRENT_SSID:
    515 		return EPERM;
    516 	case WI_RID_OWN_SSID:
    517 	case WI_RID_DESIRED_SSID:
    518 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
    519 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
    520 			error = ENOSPC;
    521 			break;
    522 		}
    523 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
    524 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
    525 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
    526 		error = ENETRESET;
    527 		break;
    528 	case WI_RID_CURRENT_BSSID:
    529 		return EPERM;
    530 	case WI_RID_OWN_CHNL:
    531 		if (len != 2)
    532 			return EINVAL;
    533 		i = le16toh(wreq.wi_val[0]);
    534 		if (i < 0 ||
    535 		    i > IEEE80211_CHAN_MAX ||
    536 		    isclr(ic->ic_chan_active, i))
    537 			return EINVAL;
    538 		ic->ic_ibss_chan = &ic->ic_channels[i];
    539 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
    540 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    541 		else
    542 			error = ENETRESET;
    543 		break;
    544 	case WI_RID_CURRENT_CHAN:
    545 		return EPERM;
    546 	case WI_RID_COMMS_QUALITY:
    547 		return EPERM;
    548 	case WI_RID_PROMISC:
    549 		if (len != 2)
    550 			return EINVAL;
    551 		if (ifp->if_flags & IFF_PROMISC) {
    552 			if (wreq.wi_val[0] == 0) {
    553 				ifp->if_flags &= ~IFF_PROMISC;
    554 				error = ENETRESET;
    555 			}
    556 		} else {
    557 			if (wreq.wi_val[0] != 0) {
    558 				ifp->if_flags |= IFF_PROMISC;
    559 				error = ENETRESET;
    560 			}
    561 		}
    562 		break;
    563 	case WI_RID_PORTTYPE:
    564 		if (len != 2)
    565 			return EINVAL;
    566 		switch (le16toh(wreq.wi_val[0])) {
    567 		case IEEE80211_M_STA:
    568 			break;
    569 		case IEEE80211_M_IBSS:
    570 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
    571 				return EINVAL;
    572 			break;
    573 		case IEEE80211_M_AHDEMO:
    574 			if (ic->ic_phytype != IEEE80211_T_DS ||
    575 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
    576 				return EINVAL;
    577 			break;
    578 		case IEEE80211_M_HOSTAP:
    579 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
    580 				return EINVAL;
    581 			break;
    582 		default:
    583 			return EINVAL;
    584 		}
    585 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
    586 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
    587 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    588 		}
    589 		break;
    590 #if 0
    591 	case WI_RID_MAC_NODE:
    592 		if (len != IEEE80211_ADDR_LEN)
    593 			return EINVAL;
    594 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
    595 		/* if_init will copy lladdr into ic_myaddr */
    596 		error = ENETRESET;
    597 		break;
    598 #endif
    599 	case WI_RID_TX_RATE:
    600 		if (len != 2)
    601 			return EINVAL;
    602 		if (wreq.wi_val[0] == 0) {
    603 			/* auto */
    604 			ic->ic_fixed_rate = -1;
    605 			break;
    606 		}
    607 		rate = 2 * le16toh(wreq.wi_val[0]);
    608 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
    609 			/*
    610 			 * In autoselect mode search for the rate.  We take
    611 			 * the first instance which may not be right, but we
    612 			 * are limited by the interface.  Note that we also
    613 			 * lock the mode to insure the rate is meaningful
    614 			 * when it is used.
    615 			 */
    616 			for (j = IEEE80211_MODE_11A;
    617 			     j < IEEE80211_MODE_MAX; j++) {
    618 				if ((ic->ic_modecaps & (1<<j)) == 0)
    619 					continue;
    620 				i = findrate(ic, j, rate);
    621 				if (i != -1) {
    622 					/* lock mode too */
    623 					ic->ic_curmode = j;
    624 					goto setrate;
    625 				}
    626 			}
    627 		} else {
    628 			i = findrate(ic, ic->ic_curmode, rate);
    629 			if (i != -1)
    630 				goto setrate;
    631 		}
    632 		return EINVAL;
    633 	setrate:
    634 		ic->ic_fixed_rate = i;
    635 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    636 		break;
    637 	case WI_RID_CUR_TX_RATE:
    638 		return EPERM;
    639 	case WI_RID_FRAG_THRESH:
    640 		if (len != 2)
    641 			return EINVAL;
    642 		ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
    643 		error = ENETRESET;
    644 		break;
    645 	case WI_RID_RTS_THRESH:
    646 		if (len != 2)
    647 			return EINVAL;
    648 		ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
    649 		error = ENETRESET;
    650 		break;
    651 	case WI_RID_CREATE_IBSS:
    652 		if (len != 2)
    653 			return EINVAL;
    654 		if (wreq.wi_val[0] != 0) {
    655 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
    656 				return EINVAL;
    657 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
    658 				ic->ic_flags |= IEEE80211_F_IBSSON;
    659 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
    660 				    ic->ic_state == IEEE80211_S_SCAN)
    661 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
    662 			}
    663 		} else {
    664 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
    665 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
    666 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
    667 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
    668 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
    669 				}
    670 			}
    671 		}
    672 		break;
    673 	case WI_RID_MICROWAVE_OVEN:
    674 		if (len != 2)
    675 			return EINVAL;
    676 		if (wreq.wi_val[0] != 0)
    677 			return EINVAL;		/* not supported */
    678 		break;
    679 	case WI_RID_ROAMING_MODE:
    680 		if (len != 2)
    681 			return EINVAL;
    682 		i = le16toh(wreq.wi_val[0]);
    683 		if (i > IEEE80211_ROAMING_MANUAL)
    684 			return EINVAL;		/* not supported */
    685 		ic->ic_roaming = i;
    686 		break;
    687 	case WI_RID_SYSTEM_SCALE:
    688 		if (len != 2)
    689 			return EINVAL;
    690 		if (le16toh(wreq.wi_val[0]) != 1)
    691 			return EINVAL;		/* not supported */
    692 		break;
    693 	case WI_RID_PM_ENABLED:
    694 		if (len != 2)
    695 			return EINVAL;
    696 		if (wreq.wi_val[0] != 0) {
    697 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
    698 				return EINVAL;
    699 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
    700 				ic->ic_flags |= IEEE80211_F_PMGTON;
    701 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    702 			}
    703 		} else {
    704 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
    705 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
    706 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    707 			}
    708 		}
    709 		break;
    710 	case WI_RID_MAX_SLEEP:
    711 		if (len != 2)
    712 			return EINVAL;
    713 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
    714 		if (ic->ic_flags & IEEE80211_F_PMGTON)
    715 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    716 		break;
    717 	case WI_RID_CUR_BEACON_INT:
    718 		return EPERM;
    719 	case WI_RID_WEP_AVAIL:
    720 		return EPERM;
    721 	case WI_RID_CNFAUTHMODE:
    722 		if (len != 2)
    723 			return EINVAL;
    724 		i = le16toh(wreq.wi_val[0]);
    725 		if (i > IEEE80211_AUTH_WPA)
    726 			return EINVAL;
    727 		ic->ic_bss->ni_authmode = i;		/* XXX ENETRESET? */
    728 		error = ENETRESET;
    729 		break;
    730 	case WI_RID_ENCRYPTION:
    731 		if (len != 2)
    732 			return EINVAL;
    733 		if (wreq.wi_val[0] != 0) {
    734 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
    735 				return EINVAL;
    736 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
    737 				ic->ic_flags |= IEEE80211_F_PRIVACY;
    738 				error = ENETRESET;
    739 			}
    740 		} else {
    741 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    742 				ic->ic_flags &= ~IEEE80211_F_PRIVACY;
    743 				error = ENETRESET;
    744 			}
    745 		}
    746 		break;
    747 	case WI_RID_TX_CRYPT_KEY:
    748 		if (len != 2)
    749 			return EINVAL;
    750 		i = le16toh(wreq.wi_val[0]);
    751 		if (i >= IEEE80211_WEP_NKID)
    752 			return EINVAL;
    753 		ic->ic_def_txkey = i;
    754 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    755 		break;
    756 	case WI_RID_DEFLT_CRYPT_KEYS:
    757 		if (len != sizeof(struct wi_ltv_keys))
    758 			return EINVAL;
    759 		keys = (struct wi_ltv_keys *)&wreq;
    760 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    761 			len = le16toh(keys->wi_keys[i].wi_keylen);
    762 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
    763 				return EINVAL;
    764 			if (len > IEEE80211_KEYBUF_SIZE)
    765 				return EINVAL;
    766 		}
    767 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    768 			struct ieee80211_key *k = &ic->ic_nw_keys[i];
    769 
    770 			len = le16toh(keys->wi_keys[i].wi_keylen);
    771 			k->wk_keylen = len;
    772 			k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
    773 			memset(k->wk_key, 0, sizeof(k->wk_key));
    774 			memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
    775 #if 0
    776 			k->wk_type = IEEE80211_CIPHER_WEP;
    777 #endif
    778 		}
    779 		error = ENETRESET;
    780 		break;
    781 	case WI_RID_MAX_DATALEN:
    782 		if (len != 2)
    783 			return EINVAL;
    784 		len = le16toh(wreq.wi_val[0]);
    785 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
    786 			return EINVAL;
    787 		ic->ic_fragthreshold = len;
    788 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    789 		break;
    790 	case WI_RID_IFACE_STATS:
    791 		error = EPERM;
    792 		break;
    793 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
    794 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    795 			break;
    796 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
    797 		if (error == 0)
    798 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    799 		break;
    800 	case WI_RID_SCAN_APS:
    801 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    802 			break;
    803 		len--;			/* XXX: tx rate? */
    804 		/* FALLTHRU */
    805 	case WI_RID_CHANNEL_LIST:
    806 		memset(chanlist, 0, sizeof(chanlist));
    807 		/*
    808 		 * Since channel 0 is not available for DS, channel 1
    809 		 * is assigned to LSB on WaveLAN.
    810 		 */
    811 		if (ic->ic_phytype == IEEE80211_T_DS)
    812 			i = 1;
    813 		else
    814 			i = 0;
    815 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
    816 			if ((j / 8) >= len)
    817 				break;
    818 			if (isclr((u_int8_t *)wreq.wi_val, j))
    819 				continue;
    820 			if (isclr(ic->ic_chan_active, i)) {
    821 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
    822 					continue;
    823 				if (isclr(ic->ic_chan_avail, i))
    824 					return EPERM;
    825 			}
    826 			setbit(chanlist, i);
    827 		}
    828 		error = ieee80211_setupscan(ic, chanlist);
    829 		if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
    830 			/* NB: ignore error from ieee80211_setupscan */
    831 			error = ENETRESET;
    832 		} else if (error == 0)
    833 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    834 		break;
    835 	default:
    836 		error = EINVAL;
    837 		break;
    838 	}
    839 	if (error == ENETRESET && !IS_UP_AUTO(ic))
    840 		error = 0;
    841 	return error;
    842 }
    843 
    844 static struct ieee80211_channel *
    845 getcurchan(struct ieee80211com *ic)
    846 {
    847 	switch (ic->ic_state) {
    848 	case IEEE80211_S_INIT:
    849 	case IEEE80211_S_SCAN:
    850 		return ic->ic_des_chan;
    851 	default:
    852 		return ic->ic_ibss_chan;
    853 	}
    854 }
    855 
    856 static int
    857 cap2cipher(int flag)
    858 {
    859 	switch (flag) {
    860 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
    861 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
    862 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
    863 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
    864 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
    865 	}
    866 	return -1;
    867 }
    868 
    869 static int
    870 ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
    871 {
    872 	struct ieee80211_node *ni;
    873 	struct ieee80211req_key ik;
    874 	struct ieee80211_key *wk;
    875 	const struct ieee80211_cipher *cip;
    876 	u_int kid;
    877 	int error;
    878 
    879 	if (ireq->i_len != sizeof(ik))
    880 		return EINVAL;
    881 	error = copyin(ireq->i_data, &ik, sizeof(ik));
    882 	if (error)
    883 		return error;
    884 	kid = ik.ik_keyix;
    885 	if (kid == IEEE80211_KEYIX_NONE) {
    886 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
    887 		if (ni == NULL)
    888 			return EINVAL;		/* XXX */
    889 		wk = &ni->ni_ucastkey;
    890 	} else {
    891 		if (kid >= IEEE80211_WEP_NKID)
    892 			return EINVAL;
    893 		wk = &ic->ic_nw_keys[kid];
    894 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
    895 		ni = NULL;
    896 	}
    897 	cip = wk->wk_cipher;
    898 	ik.ik_type = cip->ic_cipher;
    899 	ik.ik_keylen = wk->wk_keylen;
    900 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
    901 	if (wk->wk_keyix == ic->ic_def_txkey)
    902 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
    903 	if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
    904 		/* NB: only root can read key data */
    905 		ik.ik_keyrsc = wk->wk_keyrsc;
    906 		ik.ik_keytsc = wk->wk_keytsc;
    907 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
    908 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
    909 			memcpy(ik.ik_keydata+wk->wk_keylen,
    910 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
    911 				IEEE80211_MICBUF_SIZE);
    912 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
    913 		}
    914 	} else {
    915 		ik.ik_keyrsc = 0;
    916 		ik.ik_keytsc = 0;
    917 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
    918 	}
    919 	if (ni != NULL)
    920 		ieee80211_free_node(ni);
    921 	return copyout(&ik, ireq->i_data, sizeof(ik));
    922 }
    923 
    924 static int
    925 ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
    926 {
    927 
    928 	if (sizeof(ic->ic_chan_active) > ireq->i_len)
    929 		ireq->i_len = sizeof(ic->ic_chan_active);
    930 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
    931 }
    932 
    933 static int
    934 ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
    935 {
    936 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
    937 	int i, space;
    938 
    939 	/*
    940 	 * Since channel 0 is not available for DS, channel 1
    941 	 * is assigned to LSB on WaveLAN.
    942 	 */
    943 	if (ic->ic_phytype == IEEE80211_T_DS)
    944 		i = 1;
    945 	else
    946 		i = 0;
    947 	memset(&chans, 0, sizeof(chans));
    948 	for (; i <= IEEE80211_CHAN_MAX; i++)
    949 		if (isset(ic->ic_chan_avail, i)) {
    950 			struct ieee80211_channel *c = &ic->ic_channels[i];
    951 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
    952 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
    953 			chans.ic_nchans++;
    954 		}
    955 	space = __offsetof(struct ieee80211req_chaninfo,
    956 			ic_chans[chans.ic_nchans]);
    957 	if (space > ireq->i_len)
    958 		space = ireq->i_len;
    959 	return copyout(&chans, ireq->i_data, space);
    960 }
    961 
    962 static int
    963 ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
    964 {
    965 	struct ieee80211_node *ni;
    966 	struct ieee80211req_wpaie wpaie;
    967 	int error;
    968 
    969 	if (ireq->i_len < IEEE80211_ADDR_LEN)
    970 		return EINVAL;
    971 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
    972 	if (error != 0)
    973 		return error;
    974 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
    975 	if (ni == NULL)
    976 		return EINVAL;		/* XXX */
    977 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
    978 	if (ni->ni_wpa_ie != NULL) {
    979 		int ielen = ni->ni_wpa_ie[1] + 2;
    980 		if (ielen > sizeof(wpaie.wpa_ie))
    981 			ielen = sizeof(wpaie.wpa_ie);
    982 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
    983 	}
    984 	ieee80211_free_node(ni);
    985 	if (ireq->i_len > sizeof(wpaie))
    986 		ireq->i_len = sizeof(wpaie);
    987 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
    988 }
    989 
    990 static int
    991 ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
    992 {
    993 	struct ieee80211_node *ni;
    994 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
    995 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
    996 	int error;
    997 
    998 	if (ireq->i_len < off)
    999 		return EINVAL;
   1000 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
   1001 	if (error != 0)
   1002 		return error;
   1003 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
   1004 	if (ni == NULL)
   1005 		return EINVAL;		/* XXX */
   1006 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
   1007 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
   1008 	/* NB: copy out only the statistics */
   1009 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
   1010 			ireq->i_len - off);
   1011 	ieee80211_free_node(ni);
   1012 	return error;
   1013 }
   1014 
   1015 static void
   1016 get_scan_result(struct ieee80211req_scan_result *sr,
   1017 	const struct ieee80211_node *ni)
   1018 {
   1019 	struct ieee80211com *ic = ni->ni_ic;
   1020 
   1021 	memset(sr, 0, sizeof(*sr));
   1022 	sr->isr_ssid_len = ni->ni_esslen;
   1023 	if (ni->ni_wpa_ie != NULL)
   1024 		sr->isr_ie_len += 2+ni->ni_wpa_ie[1];
   1025 	if (ni->ni_wme_ie != NULL)
   1026 		sr->isr_ie_len += 2+ni->ni_wme_ie[1];
   1027 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
   1028 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
   1029 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
   1030 		sr->isr_freq = ni->ni_chan->ic_freq;
   1031 		sr->isr_flags = ni->ni_chan->ic_flags;
   1032 	}
   1033 	sr->isr_rssi = ic->ic_node_getrssi(ni);
   1034 	sr->isr_intval = ni->ni_intval;
   1035 	sr->isr_capinfo = ni->ni_capinfo;
   1036 	sr->isr_erp = ni->ni_erp;
   1037 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
   1038 	sr->isr_nrates = ni->ni_rates.rs_nrates;
   1039 	if (sr->isr_nrates > 15)
   1040 		sr->isr_nrates = 15;
   1041 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
   1042 }
   1043 
   1044 static int
   1045 ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
   1046 {
   1047 	union {
   1048 		struct ieee80211req_scan_result res;
   1049 		char data[512];		/* XXX shrink? */
   1050 	} u;
   1051 	struct ieee80211req_scan_result *sr = &u.res;
   1052 	struct ieee80211_node_table *nt;
   1053 	struct ieee80211_node *ni;
   1054 	int error, space;
   1055 	u_int8_t *p, *cp;
   1056 
   1057 	p = ireq->i_data;
   1058 	space = ireq->i_len;
   1059 	error = 0;
   1060 	/* XXX locking */
   1061 	nt =  &ic->ic_scan;
   1062 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1063 		/* NB: skip pre-scan node state */
   1064 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
   1065 			continue;
   1066 		get_scan_result(sr, ni);
   1067 		if (sr->isr_len > sizeof(u))
   1068 			continue;		/* XXX */
   1069 		if (space < sr->isr_len)
   1070 			break;
   1071 		cp = (u_int8_t *)(sr+1);
   1072 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
   1073 		cp += ni->ni_esslen;
   1074 		if (ni->ni_wpa_ie != NULL) {
   1075 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1076 			cp += 2+ni->ni_wpa_ie[1];
   1077 		}
   1078 		if (ni->ni_wme_ie != NULL) {
   1079 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1080 			cp += 2+ni->ni_wme_ie[1];
   1081 		}
   1082 		error = copyout(sr, p, sr->isr_len);
   1083 		if (error)
   1084 			break;
   1085 		p += sr->isr_len;
   1086 		space -= sr->isr_len;
   1087 	}
   1088 	ireq->i_len -= space;
   1089 	return error;
   1090 }
   1091 
   1092 static void
   1093 get_sta_info(struct ieee80211req_sta_info *si, const struct ieee80211_node *ni)
   1094 {
   1095 	struct ieee80211com *ic = ni->ni_ic;
   1096 
   1097 	si->isi_ie_len = 0;
   1098 	if (ni->ni_wpa_ie != NULL)
   1099 		si->isi_ie_len += 2+ni->ni_wpa_ie[1];
   1100 	if (ni->ni_wme_ie != NULL)
   1101 		si->isi_ie_len += 2+ni->ni_wme_ie[1];
   1102 	si->isi_len = sizeof(*si) + si->isi_ie_len, sizeof(u_int32_t);
   1103 	si->isi_len = roundup(si->isi_len, sizeof(u_int32_t));
   1104 	si->isi_freq = ni->ni_chan->ic_freq;
   1105 	si->isi_flags = ni->ni_chan->ic_flags;
   1106 	si->isi_state = ni->ni_flags;
   1107 	si->isi_authmode = ni->ni_authmode;
   1108 	si->isi_rssi = ic->ic_node_getrssi(ni);
   1109 	si->isi_capinfo = ni->ni_capinfo;
   1110 	si->isi_erp = ni->ni_erp;
   1111 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
   1112 	si->isi_nrates = ni->ni_rates.rs_nrates;
   1113 	if (si->isi_nrates > 15)
   1114 		si->isi_nrates = 15;
   1115 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
   1116 	si->isi_txrate = ni->ni_txrate;
   1117 	si->isi_associd = ni->ni_associd;
   1118 	si->isi_txpower = ni->ni_txpower;
   1119 	si->isi_vlan = ni->ni_vlan;
   1120 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
   1121 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
   1122 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
   1123 	} else {
   1124 		si->isi_txseqs[0] = ni->ni_txseqs[0];
   1125 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
   1126 	}
   1127 	if (ic->ic_opmode == IEEE80211_M_IBSS || ni->ni_associd != 0)
   1128 		si->isi_inact = ic->ic_inact_run;
   1129 	else if (ieee80211_node_is_authorized(ni))
   1130 		si->isi_inact = ic->ic_inact_auth;
   1131 	else
   1132 		si->isi_inact = ic->ic_inact_init;
   1133 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
   1134 }
   1135 
   1136 static int
   1137 ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
   1138 {
   1139 	union {
   1140 		struct ieee80211req_sta_info info;
   1141 		char data[512];		/* XXX shrink? */
   1142 	} u;
   1143 	struct ieee80211req_sta_info *si = &u.info;
   1144 	struct ieee80211_node_table *nt;
   1145 	struct ieee80211_node *ni;
   1146 	int error, space;
   1147 	u_int8_t *p, *cp;
   1148 
   1149 	nt = &ic->ic_sta;
   1150 	p = ireq->i_data;
   1151 	space = ireq->i_len;
   1152 	error = 0;
   1153 	/* XXX locking */
   1154 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1155 		get_sta_info(si, ni);
   1156 		if (si->isi_len > sizeof(u))
   1157 			continue;		/* XXX */
   1158 		if (space < si->isi_len)
   1159 			break;
   1160 		cp = (u_int8_t *)(si+1);
   1161 		if (ni->ni_wpa_ie != NULL) {
   1162 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1163 			cp += 2+ni->ni_wpa_ie[1];
   1164 		}
   1165 		if (ni->ni_wme_ie != NULL) {
   1166 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1167 			cp += 2+ni->ni_wme_ie[1];
   1168 		}
   1169 		error = copyout(si, p, si->isi_len);
   1170 		if (error)
   1171 			break;
   1172 		p += si->isi_len;
   1173 		space -= si->isi_len;
   1174 	}
   1175 	ireq->i_len -= space;
   1176 	return error;
   1177 }
   1178 
   1179 static int
   1180 ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1181 {
   1182 	struct ieee80211_node *ni;
   1183 	struct ieee80211req_sta_txpow txpow;
   1184 	int error;
   1185 
   1186 	if (ireq->i_len != sizeof(txpow))
   1187 		return EINVAL;
   1188 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1189 	if (error != 0)
   1190 		return error;
   1191 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1192 	if (ni == NULL)
   1193 		return EINVAL;		/* XXX */
   1194 	txpow.it_txpow = ni->ni_txpower;
   1195 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
   1196 	ieee80211_free_node(ni);
   1197 	return error;
   1198 }
   1199 
   1200 static int
   1201 ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1202 {
   1203 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1204 	struct wmeParams *wmep;
   1205 	int ac;
   1206 
   1207 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1208 		return EINVAL;
   1209 
   1210 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1211 	if (ac >= WME_NUM_AC)
   1212 		ac = WME_AC_BE;
   1213 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
   1214 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1215 	else
   1216 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1217 	switch (ireq->i_type) {
   1218 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1219 		ireq->i_val = wmep->wmep_logcwmin;
   1220 		break;
   1221 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1222 		ireq->i_val = wmep->wmep_logcwmax;
   1223 		break;
   1224 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1225 		ireq->i_val = wmep->wmep_aifsn;
   1226 		break;
   1227 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1228 		ireq->i_val = wmep->wmep_txopLimit;
   1229 		break;
   1230 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1231 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1232 		ireq->i_val = wmep->wmep_acm;
   1233 		break;
   1234 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1235 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1236 		ireq->i_val = !wmep->wmep_noackPolicy;
   1237 		break;
   1238 	}
   1239 	return 0;
   1240 }
   1241 
   1242 /*
   1243  * When building the kernel with -O2 on the i386 architecture, gcc
   1244  * seems to want to inline this function into ieee80211_ioctl()
   1245  * (which is the only routine that calls it). When this happens,
   1246  * ieee80211_ioctl() ends up consuming an additional 2K of stack
   1247  * space. (Exactly why it needs so much is unclear.) The problem
   1248  * is that it's possible for ieee80211_ioctl() to invoke other
   1249  * routines (including driver init functions) which could then find
   1250  * themselves perilously close to exhausting the stack.
   1251  *
   1252  * To avoid this, we deliberately prevent gcc from inlining this
   1253  * routine. Another way to avoid this is to use less agressive
   1254  * optimization when compiling this file (i.e. -O instead of -O2)
   1255  * but special-casing the compilation of this one module in the
   1256  * build system would be awkward.
   1257  */
   1258 #ifdef __GNUC__
   1259 __attribute__ ((noinline))
   1260 #endif
   1261 static int
   1262 ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1263 {
   1264 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1265 	int error = 0;
   1266 	u_int kid, len, m;
   1267 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1268 	char tmpssid[IEEE80211_NWID_LEN];
   1269 
   1270 	switch (ireq->i_type) {
   1271 	case IEEE80211_IOC_SSID:
   1272 		switch (ic->ic_state) {
   1273 		case IEEE80211_S_INIT:
   1274 		case IEEE80211_S_SCAN:
   1275 			ireq->i_len = ic->ic_des_esslen;
   1276 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
   1277 			break;
   1278 		default:
   1279 			ireq->i_len = ic->ic_bss->ni_esslen;
   1280 			memcpy(tmpssid, ic->ic_bss->ni_essid,
   1281 				ireq->i_len);
   1282 			break;
   1283 		}
   1284 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
   1285 		break;
   1286 	case IEEE80211_IOC_NUMSSIDS:
   1287 		ireq->i_val = 1;
   1288 		break;
   1289 	case IEEE80211_IOC_WEP:
   1290 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
   1291 			ireq->i_val = IEEE80211_WEP_OFF;
   1292 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
   1293 			ireq->i_val = IEEE80211_WEP_ON;
   1294 		else
   1295 			ireq->i_val = IEEE80211_WEP_MIXED;
   1296 		break;
   1297 	case IEEE80211_IOC_WEPKEY:
   1298 		kid = (u_int) ireq->i_val;
   1299 		if (kid >= IEEE80211_WEP_NKID)
   1300 			return EINVAL;
   1301 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
   1302 		/* NB: only root can read WEP keys */
   1303 		if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
   1304 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
   1305 		} else {
   1306 			bzero(tmpkey, len);
   1307 		}
   1308 		ireq->i_len = len;
   1309 		error = copyout(tmpkey, ireq->i_data, len);
   1310 		break;
   1311 	case IEEE80211_IOC_NUMWEPKEYS:
   1312 		ireq->i_val = IEEE80211_WEP_NKID;
   1313 		break;
   1314 	case IEEE80211_IOC_WEPTXKEY:
   1315 		ireq->i_val = ic->ic_def_txkey;
   1316 		break;
   1317 	case IEEE80211_IOC_AUTHMODE:
   1318 		if (ic->ic_flags & IEEE80211_F_WPA)
   1319 			ireq->i_val = IEEE80211_AUTH_WPA;
   1320 		else
   1321 			ireq->i_val = ic->ic_bss->ni_authmode;
   1322 		break;
   1323 	case IEEE80211_IOC_CHANNEL:
   1324 		ireq->i_val = ieee80211_chan2ieee(ic, getcurchan(ic));
   1325 		break;
   1326 	case IEEE80211_IOC_POWERSAVE:
   1327 		if (ic->ic_flags & IEEE80211_F_PMGTON)
   1328 			ireq->i_val = IEEE80211_POWERSAVE_ON;
   1329 		else
   1330 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
   1331 		break;
   1332 	case IEEE80211_IOC_POWERSAVESLEEP:
   1333 		ireq->i_val = ic->ic_lintval;
   1334 		break;
   1335 	case IEEE80211_IOC_RTSTHRESHOLD:
   1336 		ireq->i_val = ic->ic_rtsthreshold;
   1337 		break;
   1338 	case IEEE80211_IOC_PROTMODE:
   1339 		ireq->i_val = ic->ic_protmode;
   1340 		break;
   1341 	case IEEE80211_IOC_TXPOWER:
   1342 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   1343 			return EINVAL;
   1344 		ireq->i_val = ic->ic_txpowlimit;
   1345 		break;
   1346 	case IEEE80211_IOC_MCASTCIPHER:
   1347 		ireq->i_val = rsn->rsn_mcastcipher;
   1348 		break;
   1349 	case IEEE80211_IOC_MCASTKEYLEN:
   1350 		ireq->i_val = rsn->rsn_mcastkeylen;
   1351 		break;
   1352 	case IEEE80211_IOC_UCASTCIPHERS:
   1353 		ireq->i_val = 0;
   1354 		for (m = 0x1; m != 0; m <<= 1)
   1355 			if (rsn->rsn_ucastcipherset & m)
   1356 				ireq->i_val |= 1<<cap2cipher(m);
   1357 		break;
   1358 	case IEEE80211_IOC_UCASTCIPHER:
   1359 		ireq->i_val = rsn->rsn_ucastcipher;
   1360 		break;
   1361 	case IEEE80211_IOC_UCASTKEYLEN:
   1362 		ireq->i_val = rsn->rsn_ucastkeylen;
   1363 		break;
   1364 	case IEEE80211_IOC_KEYMGTALGS:
   1365 		ireq->i_val = rsn->rsn_keymgmtset;
   1366 		break;
   1367 	case IEEE80211_IOC_RSNCAPS:
   1368 		ireq->i_val = rsn->rsn_caps;
   1369 		break;
   1370 	case IEEE80211_IOC_WPA:
   1371 		switch (ic->ic_flags & IEEE80211_F_WPA) {
   1372 		case IEEE80211_F_WPA1:
   1373 			ireq->i_val = 1;
   1374 			break;
   1375 		case IEEE80211_F_WPA2:
   1376 			ireq->i_val = 2;
   1377 			break;
   1378 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
   1379 			ireq->i_val = 3;
   1380 			break;
   1381 		default:
   1382 			ireq->i_val = 0;
   1383 			break;
   1384 		}
   1385 		break;
   1386 	case IEEE80211_IOC_CHANLIST:
   1387 		error = ieee80211_ioctl_getchanlist(ic, ireq);
   1388 		break;
   1389 	case IEEE80211_IOC_ROAMING:
   1390 		ireq->i_val = ic->ic_roaming;
   1391 		break;
   1392 	case IEEE80211_IOC_PRIVACY:
   1393 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
   1394 		break;
   1395 	case IEEE80211_IOC_DROPUNENCRYPTED:
   1396 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
   1397 		break;
   1398 	case IEEE80211_IOC_COUNTERMEASURES:
   1399 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
   1400 		break;
   1401 	case IEEE80211_IOC_DRIVER_CAPS:
   1402 		ireq->i_val = ic->ic_caps>>16;
   1403 		ireq->i_len = ic->ic_caps&0xffff;
   1404 		break;
   1405 	case IEEE80211_IOC_WME:
   1406 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
   1407 		break;
   1408 	case IEEE80211_IOC_HIDESSID:
   1409 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
   1410 		break;
   1411 	case IEEE80211_IOC_APBRIDGE:
   1412 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
   1413 		break;
   1414 	case IEEE80211_IOC_OPTIE:
   1415 		if (ic->ic_opt_ie == NULL)
   1416 			return EINVAL;
   1417 		/* NB: truncate, caller can check length */
   1418 		if (ireq->i_len > ic->ic_opt_ie_len)
   1419 			ireq->i_len = ic->ic_opt_ie_len;
   1420 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
   1421 		break;
   1422 	case IEEE80211_IOC_WPAKEY:
   1423 		error = ieee80211_ioctl_getkey(ic, ireq);
   1424 		break;
   1425 	case IEEE80211_IOC_CHANINFO:
   1426 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
   1427 		break;
   1428 	case IEEE80211_IOC_BSSID:
   1429 		if (ireq->i_len != IEEE80211_ADDR_LEN)
   1430 			return EINVAL;
   1431 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
   1432 					ic->ic_bss->ni_bssid :
   1433 					ic->ic_des_bssid,
   1434 				ireq->i_data, ireq->i_len);
   1435 		break;
   1436 	case IEEE80211_IOC_WPAIE:
   1437 		error = ieee80211_ioctl_getwpaie(ic, ireq);
   1438 		break;
   1439 	case IEEE80211_IOC_SCAN_RESULTS:
   1440 		error = ieee80211_ioctl_getscanresults(ic, ireq);
   1441 		break;
   1442 	case IEEE80211_IOC_STA_STATS:
   1443 		error = ieee80211_ioctl_getstastats(ic, ireq);
   1444 		break;
   1445 	case IEEE80211_IOC_TXPOWMAX:
   1446 		ireq->i_val = ic->ic_bss->ni_txpower;
   1447 		break;
   1448 	case IEEE80211_IOC_STA_TXPOW:
   1449 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
   1450 		break;
   1451 	case IEEE80211_IOC_STA_INFO:
   1452 		error = ieee80211_ioctl_getstainfo(ic, ireq);
   1453 		break;
   1454 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1455 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1456 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1457 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1458 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1459 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   1460 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
   1461 		break;
   1462 	case IEEE80211_IOC_DTIM_PERIOD:
   1463 		ireq->i_val = ic->ic_dtim_period;
   1464 		break;
   1465 	case IEEE80211_IOC_BEACON_INTERVAL:
   1466 		/* NB: get from ic_bss for station mode */
   1467 		ireq->i_val = ic->ic_bss->ni_intval;
   1468 		break;
   1469 	default:
   1470 		error = EINVAL;
   1471 		break;
   1472 	}
   1473 	return error;
   1474 }
   1475 
   1476 static int
   1477 ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
   1478 {
   1479 	int error;
   1480 	void *ie;
   1481 
   1482 	/*
   1483 	 * NB: Doing this for ap operation could be useful (e.g. for
   1484 	 *     WPA and/or WME) except that it typically is worthless
   1485 	 *     without being able to intervene when processing
   1486 	 *     association response frames--so disallow it for now.
   1487 	 */
   1488 	if (ic->ic_opmode != IEEE80211_M_STA)
   1489 		return EINVAL;
   1490 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
   1491 		return EINVAL;
   1492 	/* NB: data.length is validated by the wireless extensions code */
   1493 	MALLOC(ie, void *, (u_long)ireq->i_len, M_DEVBUF, M_WAITOK);
   1494 	if (ie == NULL)
   1495 		return ENOMEM;
   1496 	error = copyin(ireq->i_data, ie, ireq->i_len);
   1497 	/* XXX sanity check data? */
   1498 	if (ic->ic_opt_ie != NULL)
   1499 		FREE(ic->ic_opt_ie, M_DEVBUF);
   1500 	ic->ic_opt_ie = ie;
   1501 	ic->ic_opt_ie_len = ireq->i_len;
   1502 	return 0;
   1503 }
   1504 
   1505 static int
   1506 ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1507 {
   1508 	struct ieee80211req_key ik;
   1509 	struct ieee80211_node *ni;
   1510 	struct ieee80211_key *wk;
   1511 	u_int16_t kid;
   1512 	int error;
   1513 
   1514 	if (ireq->i_len != sizeof(ik))
   1515 		return EINVAL;
   1516 	error = copyin(ireq->i_data, &ik, sizeof(ik));
   1517 	if (error)
   1518 		return error;
   1519 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
   1520 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
   1521 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
   1522 		return E2BIG;
   1523 	kid = ik.ik_keyix;
   1524 	if (kid == IEEE80211_KEYIX_NONE) {
   1525 		/* XXX unicast keys currently must be tx/rx */
   1526 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
   1527 			return EINVAL;
   1528 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1529 			ni = ic->ic_bss;
   1530 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid))
   1531 				return EADDRNOTAVAIL;
   1532 		} else {
   1533 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
   1534 			if (ni == NULL)
   1535 				return ENOENT;
   1536 		}
   1537 		wk = &ni->ni_ucastkey;
   1538 	} else {
   1539 		if (kid >= IEEE80211_WEP_NKID)
   1540 			return EINVAL;
   1541 		wk = &ic->ic_nw_keys[kid];
   1542 		ni = NULL;
   1543 		/* XXX auto-add group key flag until applications are updated */
   1544 		if ((ik.ik_flags & IEEE80211_KEY_XMIT) == 0)	/* XXX */
   1545 			ik.ik_flags |= IEEE80211_KEY_GROUP;	/* XXX */
   1546 	}
   1547 	error = 0;
   1548 	ieee80211_key_update_begin(ic);
   1549 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
   1550 		wk->wk_keylen = ik.ik_keylen;
   1551 		/* NB: MIC presence is implied by cipher type */
   1552 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
   1553 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
   1554 		wk->wk_keyrsc = ik.ik_keyrsc;
   1555 		wk->wk_keytsc = 0;			/* new key, reset */
   1556 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
   1557 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
   1558 		if (!ieee80211_crypto_setkey(ic, wk,
   1559 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
   1560 			error = EIO;
   1561 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
   1562 			ic->ic_def_txkey = kid;
   1563 	} else
   1564 		error = ENXIO;
   1565 	ieee80211_key_update_end(ic);
   1566 	if (ni != NULL)
   1567 		ieee80211_free_node(ni);
   1568 	return error;
   1569 }
   1570 
   1571 static int
   1572 ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1573 {
   1574 	struct ieee80211req_del_key dk;
   1575 	int kid, error;
   1576 
   1577 	if (ireq->i_len != sizeof(dk))
   1578 		return EINVAL;
   1579 	error = copyin(ireq->i_data, &dk, sizeof(dk));
   1580 	if (error)
   1581 		return error;
   1582 	kid = dk.idk_keyix;
   1583 	/* XXX u_int8_t -> u_int16_t */
   1584 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
   1585 		struct ieee80211_node *ni;
   1586 
   1587 		ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
   1588 		if (ni == NULL)
   1589 			return EINVAL;		/* XXX */
   1590 		/* XXX error return */
   1591 		ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
   1592 		ieee80211_free_node(ni);
   1593 	} else {
   1594 		if (kid >= IEEE80211_WEP_NKID)
   1595 			return EINVAL;
   1596 		/* XXX error return */
   1597 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
   1598 	}
   1599 	return 0;
   1600 }
   1601 
   1602 static void
   1603 domlme(void *arg, struct ieee80211_node *ni)
   1604 {
   1605 	struct ieee80211com *ic = ni->ni_ic;
   1606 	struct ieee80211req_mlme *mlme = arg;
   1607 
   1608 	if (ni->ni_associd != 0) {
   1609 		IEEE80211_SEND_MGMT(ic, ni,
   1610 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
   1611 				IEEE80211_FC0_SUBTYPE_DEAUTH :
   1612 				IEEE80211_FC0_SUBTYPE_DISASSOC,
   1613 			mlme->im_reason);
   1614 	}
   1615 	ieee80211_node_leave(ic, ni);
   1616 }
   1617 
   1618 static int
   1619 ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
   1620 {
   1621 	struct ieee80211req_mlme mlme;
   1622 	struct ieee80211_node *ni;
   1623 	int error;
   1624 
   1625 	if (ireq->i_len != sizeof(mlme))
   1626 		return EINVAL;
   1627 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
   1628 	if (error)
   1629 		return error;
   1630 	switch (mlme.im_op) {
   1631 	case IEEE80211_MLME_ASSOC:
   1632 		if (ic->ic_opmode != IEEE80211_M_STA)
   1633 			return EINVAL;
   1634 		/* XXX must be in S_SCAN state? */
   1635 
   1636 		if (ic->ic_des_esslen != 0) {
   1637 			/*
   1638 			 * Desired ssid specified; must match both bssid and
   1639 			 * ssid to distinguish ap advertising multiple ssid's.
   1640 			 */
   1641 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
   1642 				mlme.im_macaddr,
   1643 				ic->ic_des_esslen, ic->ic_des_essid);
   1644 		} else {
   1645 			/*
   1646 			 * Normal case; just match bssid.
   1647 			 */
   1648 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
   1649 		}
   1650 		if (ni == NULL)
   1651 			return EINVAL;
   1652 		if (!ieee80211_sta_join(ic, ni)) {
   1653 			ieee80211_free_node(ni);
   1654 			return EINVAL;
   1655 		}
   1656 		break;
   1657 	case IEEE80211_MLME_DISASSOC:
   1658 	case IEEE80211_MLME_DEAUTH:
   1659 		switch (ic->ic_opmode) {
   1660 		case IEEE80211_M_STA:
   1661 			/* XXX not quite right */
   1662 			ieee80211_new_state(ic, IEEE80211_S_INIT,
   1663 				mlme.im_reason);
   1664 			break;
   1665 		case IEEE80211_M_HOSTAP:
   1666 			/* NB: the broadcast address means do 'em all */
   1667 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
   1668 				if ((ni = ieee80211_find_node(&ic->ic_sta,
   1669 						mlme.im_macaddr)) == NULL)
   1670 					return EINVAL;
   1671 				domlme(&mlme, ni);
   1672 				ieee80211_free_node(ni);
   1673 			} else {
   1674 				ieee80211_iterate_nodes(&ic->ic_sta,
   1675 						domlme, &mlme);
   1676 			}
   1677 			break;
   1678 		default:
   1679 			return EINVAL;
   1680 		}
   1681 		break;
   1682 	case IEEE80211_MLME_AUTHORIZE:
   1683 	case IEEE80211_MLME_UNAUTHORIZE:
   1684 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
   1685 			return EINVAL;
   1686 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
   1687 		if (ni == NULL)
   1688 			return EINVAL;
   1689 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
   1690 			ieee80211_node_authorize(ic, ni);
   1691 		else
   1692 			ieee80211_node_unauthorize(ic, ni);
   1693 		ieee80211_free_node(ni);
   1694 		break;
   1695 	default:
   1696 		return EINVAL;
   1697 	}
   1698 	return 0;
   1699 }
   1700 
   1701 static int
   1702 ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
   1703 {
   1704 	u_int8_t mac[IEEE80211_ADDR_LEN];
   1705 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1706 	int error;
   1707 
   1708 	if (ireq->i_len != sizeof(mac))
   1709 		return EINVAL;
   1710 	error = copyin(ireq->i_data, mac, ireq->i_len);
   1711 	if (error)
   1712 		return error;
   1713 	if (acl == NULL) {
   1714 		acl = ieee80211_aclator_get("mac");
   1715 		if (acl == NULL || !acl->iac_attach(ic))
   1716 			return EINVAL;
   1717 		ic->ic_acl = acl;
   1718 	}
   1719 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
   1720 		acl->iac_add(ic, mac);
   1721 	else
   1722 		acl->iac_remove(ic, mac);
   1723 	return 0;
   1724 }
   1725 
   1726 static int
   1727 ieee80211_ioctl_maccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
   1728 {
   1729 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1730 
   1731 	switch (ireq->i_val) {
   1732 	case IEEE80211_MACCMD_POLICY_OPEN:
   1733 	case IEEE80211_MACCMD_POLICY_ALLOW:
   1734 	case IEEE80211_MACCMD_POLICY_DENY:
   1735 		if (acl == NULL) {
   1736 			acl = ieee80211_aclator_get("mac");
   1737 			if (acl == NULL || !acl->iac_attach(ic))
   1738 				return EINVAL;
   1739 			ic->ic_acl = acl;
   1740 		}
   1741 		acl->iac_setpolicy(ic, ireq->i_val);
   1742 		break;
   1743 	case IEEE80211_MACCMD_FLUSH:
   1744 		if (acl != NULL)
   1745 			acl->iac_flush(ic);
   1746 		/* NB: silently ignore when not in use */
   1747 		break;
   1748 	case IEEE80211_MACCMD_DETACH:
   1749 		if (acl != NULL) {
   1750 			ic->ic_acl = NULL;
   1751 			acl->iac_detach(ic);
   1752 		}
   1753 		break;
   1754 	default:
   1755 		return EINVAL;
   1756 	}
   1757 	return 0;
   1758 }
   1759 
   1760 static int
   1761 ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
   1762 {
   1763 	struct ieee80211req_chanlist list;
   1764 	u_char chanlist[IEEE80211_CHAN_BYTES];
   1765 	int i, j, error;
   1766 
   1767 	if (ireq->i_len != sizeof(list))
   1768 		return EINVAL;
   1769 	error = copyin(ireq->i_data, &list, sizeof(list));
   1770 	if (error)
   1771 		return error;
   1772 	memset(chanlist, 0, sizeof(chanlist));
   1773 	/*
   1774 	 * Since channel 0 is not available for DS, channel 1
   1775 	 * is assigned to LSB on WaveLAN.
   1776 	 */
   1777 	if (ic->ic_phytype == IEEE80211_T_DS)
   1778 		i = 1;
   1779 	else
   1780 		i = 0;
   1781 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
   1782 		/*
   1783 		 * NB: silently discard unavailable channels so users
   1784 		 *     can specify 1-255 to get all available channels.
   1785 		 */
   1786 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
   1787 			setbit(chanlist, i);
   1788 	}
   1789 	if (ic->ic_ibss_chan == NULL ||
   1790 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
   1791 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
   1792 			if (isset(chanlist, i)) {
   1793 				ic->ic_ibss_chan = &ic->ic_channels[i];
   1794 				goto found;
   1795 			}
   1796 		return EINVAL;			/* no active channels */
   1797 found:
   1798 		;
   1799 	}
   1800 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
   1801 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
   1802 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
   1803 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   1804 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
   1805 }
   1806 
   1807 static int
   1808 ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1809 {
   1810 	struct ieee80211_node *ni;
   1811 	struct ieee80211req_sta_txpow txpow;
   1812 	int error;
   1813 
   1814 	if (ireq->i_len != sizeof(txpow))
   1815 		return EINVAL;
   1816 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1817 	if (error != 0)
   1818 		return error;
   1819 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1820 	if (ni == NULL)
   1821 		return EINVAL;		/* XXX */
   1822 	ni->ni_txpower = txpow.it_txpow;
   1823 	ieee80211_free_node(ni);
   1824 	return error;
   1825 }
   1826 
   1827 static int
   1828 ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1829 {
   1830 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1831 	struct wmeParams *wmep, *chanp;
   1832 	int isbss, ac;
   1833 
   1834 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1835 		return EINVAL;
   1836 
   1837 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
   1838 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1839 	if (ac >= WME_NUM_AC)
   1840 		ac = WME_AC_BE;
   1841 	if (isbss) {
   1842 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
   1843 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1844 	} else {
   1845 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
   1846 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1847 	}
   1848 	switch (ireq->i_type) {
   1849 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1850 		if (isbss) {
   1851 			wmep->wmep_logcwmin = ireq->i_val;
   1852 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1853 				chanp->wmep_logcwmin = ireq->i_val;
   1854 		} else {
   1855 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
   1856 				ireq->i_val;
   1857 		}
   1858 		break;
   1859 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1860 		if (isbss) {
   1861 			wmep->wmep_logcwmax = ireq->i_val;
   1862 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1863 				chanp->wmep_logcwmax = ireq->i_val;
   1864 		} else {
   1865 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
   1866 				ireq->i_val;
   1867 		}
   1868 		break;
   1869 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1870 		if (isbss) {
   1871 			wmep->wmep_aifsn = ireq->i_val;
   1872 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1873 				chanp->wmep_aifsn = ireq->i_val;
   1874 		} else {
   1875 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
   1876 		}
   1877 		break;
   1878 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1879 		if (isbss) {
   1880 			wmep->wmep_txopLimit = ireq->i_val;
   1881 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1882 				chanp->wmep_txopLimit = ireq->i_val;
   1883 		} else {
   1884 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
   1885 				ireq->i_val;
   1886 		}
   1887 		break;
   1888 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1889 		wmep->wmep_acm = ireq->i_val;
   1890 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1891 			chanp->wmep_acm = ireq->i_val;
   1892 		break;
   1893 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1894 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
   1895 			(ireq->i_val) == 0;
   1896 		break;
   1897 	}
   1898 	ieee80211_wme_updateparams(ic);
   1899 	return 0;
   1900 }
   1901 
   1902 static int
   1903 cipher2cap(int cipher)
   1904 {
   1905 	switch (cipher) {
   1906 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
   1907 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
   1908 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
   1909 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
   1910 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
   1911 	}
   1912 	return 0;
   1913 }
   1914 
   1915 static int
   1916 ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1917 {
   1918 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
   1919 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1920 	int error;
   1921 	const struct ieee80211_authenticator *auth;
   1922 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1923 	char tmpssid[IEEE80211_NWID_LEN];
   1924 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
   1925 	struct ieee80211_key *k;
   1926 	int j, caps;
   1927 	u_int kid;
   1928 
   1929 	error = 0;
   1930 	switch (ireq->i_type) {
   1931 	case IEEE80211_IOC_SSID:
   1932 		if (ireq->i_val != 0 ||
   1933 		    ireq->i_len > IEEE80211_NWID_LEN)
   1934 			return EINVAL;
   1935 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
   1936 		if (error)
   1937 			break;
   1938 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   1939 		ic->ic_des_esslen = ireq->i_len;
   1940 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
   1941 		error = ENETRESET;
   1942 		break;
   1943 	case IEEE80211_IOC_WEP:
   1944 		switch (ireq->i_val) {
   1945 		case IEEE80211_WEP_OFF:
   1946 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   1947 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1948 			break;
   1949 		case IEEE80211_WEP_ON:
   1950 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1951 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   1952 			break;
   1953 		case IEEE80211_WEP_MIXED:
   1954 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1955 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1956 			break;
   1957 		}
   1958 		error = ENETRESET;
   1959 		break;
   1960 	case IEEE80211_IOC_WEPKEY:
   1961 		kid = (u_int) ireq->i_val;
   1962 		if (kid >= IEEE80211_WEP_NKID)
   1963 			return EINVAL;
   1964 		k = &ic->ic_nw_keys[kid];
   1965 		if (ireq->i_len == 0) {
   1966 			/* zero-len =>'s delete any existing key */
   1967 			(void) ieee80211_crypto_delkey(ic, k);
   1968 			break;
   1969 		}
   1970 		if (ireq->i_len > sizeof(tmpkey))
   1971 			return EINVAL;
   1972 		memset(tmpkey, 0, sizeof(tmpkey));
   1973 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
   1974 		if (error)
   1975 			break;
   1976 		ieee80211_key_update_begin(ic);
   1977 		k->wk_keyix = kid;	/* NB: force fixed key id */
   1978 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   1979 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   1980 			k->wk_keylen = ireq->i_len;
   1981 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
   1982 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   1983 				error = EINVAL;
   1984 		} else
   1985 			error = EINVAL;
   1986 		ieee80211_key_update_end(ic);
   1987 		break;
   1988 	case IEEE80211_IOC_WEPTXKEY:
   1989 		kid = (u_int) ireq->i_val;
   1990 		if (kid >= IEEE80211_WEP_NKID &&
   1991 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
   1992 			return EINVAL;
   1993 		ic->ic_def_txkey = kid;
   1994 		error = ENETRESET;	/* push to hardware */
   1995 		break;
   1996 	case IEEE80211_IOC_AUTHMODE:
   1997 		switch (ireq->i_val) {
   1998 		case IEEE80211_AUTH_WPA:
   1999 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2000 		case IEEE80211_AUTH_OPEN:	/* open */
   2001 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2002 		case IEEE80211_AUTH_AUTO:	/* auto */
   2003 			auth = ieee80211_authenticator_get(ireq->i_val);
   2004 			if (auth == NULL)
   2005 				return EINVAL;
   2006 			break;
   2007 		default:
   2008 			return EINVAL;
   2009 		}
   2010 		switch (ireq->i_val) {
   2011 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
   2012 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2013 			ireq->i_val = IEEE80211_AUTH_8021X;
   2014 			break;
   2015 		case IEEE80211_AUTH_OPEN:	/* open */
   2016 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
   2017 			break;
   2018 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2019 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2020 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2021 			/* both require a key so mark the PRIVACY capability */
   2022 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2023 			break;
   2024 		case IEEE80211_AUTH_AUTO:	/* auto */
   2025 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2026 			/* XXX PRIVACY handling? */
   2027 			/* XXX what's the right way to do this? */
   2028 			break;
   2029 		}
   2030 		/* NB: authenticator attach/detach happens on state change */
   2031 		ic->ic_bss->ni_authmode = ireq->i_val;
   2032 		/* XXX mixed/mode/usage? */
   2033 		ic->ic_auth = auth;
   2034 		error = ENETRESET;
   2035 		break;
   2036 	case IEEE80211_IOC_CHANNEL:
   2037 		/* XXX 0xffff overflows 16-bit signed */
   2038 		if (ireq->i_val == 0 ||
   2039 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
   2040 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2041 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
   2042 		    isclr(ic->ic_chan_active, ireq->i_val)) {
   2043 			return EINVAL;
   2044 		} else
   2045 			ic->ic_ibss_chan = ic->ic_des_chan =
   2046 				&ic->ic_channels[ireq->i_val];
   2047 		switch (ic->ic_state) {
   2048 		case IEEE80211_S_INIT:
   2049 		case IEEE80211_S_SCAN:
   2050 			error = ENETRESET;
   2051 			break;
   2052 		default:
   2053 			/*
   2054 			 * If the desired channel has changed (to something
   2055 			 * other than any) and we're not already scanning,
   2056 			 * then kick the state machine.
   2057 			 */
   2058 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2059 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
   2060 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
   2061 				error = ENETRESET;
   2062 			break;
   2063 		}
   2064 		if (error == ENETRESET && ic->ic_opmode == IEEE80211_M_MONITOR)
   2065 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2066 		break;
   2067 	case IEEE80211_IOC_POWERSAVE:
   2068 		switch (ireq->i_val) {
   2069 		case IEEE80211_POWERSAVE_OFF:
   2070 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2071 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2072 				error = ENETRESET;
   2073 			}
   2074 			break;
   2075 		case IEEE80211_POWERSAVE_ON:
   2076 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2077 				error = EINVAL;
   2078 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2079 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2080 				error = ENETRESET;
   2081 			}
   2082 			break;
   2083 		default:
   2084 			error = EINVAL;
   2085 			break;
   2086 		}
   2087 		break;
   2088 	case IEEE80211_IOC_POWERSAVESLEEP:
   2089 		if (ireq->i_val < 0)
   2090 			return EINVAL;
   2091 		ic->ic_lintval = ireq->i_val;
   2092 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2093 		break;
   2094 	case IEEE80211_IOC_RTSTHRESHOLD:
   2095 		if (!(IEEE80211_RTS_MIN < ireq->i_val &&
   2096 		      ireq->i_val < IEEE80211_RTS_MAX))
   2097 			return EINVAL;
   2098 		ic->ic_rtsthreshold = ireq->i_val;
   2099 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2100 		break;
   2101 	case IEEE80211_IOC_PROTMODE:
   2102 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
   2103 			return EINVAL;
   2104 		ic->ic_protmode = ireq->i_val;
   2105 		/* NB: if not operating in 11g this can wait */
   2106 		if (ic->ic_curmode == IEEE80211_MODE_11G)
   2107 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2108 		break;
   2109 	case IEEE80211_IOC_TXPOWER:
   2110 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   2111 			return EINVAL;
   2112 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
   2113 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
   2114 			return EINVAL;
   2115 		ic->ic_txpowlimit = ireq->i_val;
   2116 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2117 		break;
   2118 	case IEEE80211_IOC_ROAMING:
   2119 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
   2120 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
   2121 			return EINVAL;
   2122 		ic->ic_roaming = ireq->i_val;
   2123 		/* XXXX reset? */
   2124 		break;
   2125 	case IEEE80211_IOC_PRIVACY:
   2126 		if (ireq->i_val) {
   2127 			/* XXX check for key state? */
   2128 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2129 		} else
   2130 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2131 		break;
   2132 	case IEEE80211_IOC_DROPUNENCRYPTED:
   2133 		if (ireq->i_val)
   2134 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2135 		else
   2136 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2137 		break;
   2138 	case IEEE80211_IOC_WPAKEY:
   2139 		error = ieee80211_ioctl_setkey(ic, ireq);
   2140 		break;
   2141 	case IEEE80211_IOC_DELKEY:
   2142 		error = ieee80211_ioctl_delkey(ic, ireq);
   2143 		break;
   2144 	case IEEE80211_IOC_MLME:
   2145 		error = ieee80211_ioctl_setmlme(ic, ireq);
   2146 		break;
   2147 	case IEEE80211_IOC_OPTIE:
   2148 		error = ieee80211_ioctl_setoptie(ic, ireq);
   2149 		break;
   2150 	case IEEE80211_IOC_COUNTERMEASURES:
   2151 		if (ireq->i_val) {
   2152 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
   2153 				return EINVAL;
   2154 			ic->ic_flags |= IEEE80211_F_COUNTERM;
   2155 		} else
   2156 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
   2157 		break;
   2158 	case IEEE80211_IOC_WPA:
   2159 		if (ireq->i_val > 3)
   2160 			return EINVAL;
   2161 		/* XXX verify ciphers available */
   2162 		ic->ic_flags &= ~IEEE80211_F_WPA;
   2163 		switch (ireq->i_val) {
   2164 		case 1:
   2165 			ic->ic_flags |= IEEE80211_F_WPA1;
   2166 			break;
   2167 		case 2:
   2168 			ic->ic_flags |= IEEE80211_F_WPA2;
   2169 			break;
   2170 		case 3:
   2171 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
   2172 			break;
   2173 		}
   2174 		error = ENETRESET;		/* XXX? */
   2175 		break;
   2176 	case IEEE80211_IOC_WME:
   2177 		if (ireq->i_val) {
   2178 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   2179 				return EINVAL;
   2180 			ic->ic_flags |= IEEE80211_F_WME;
   2181 		} else
   2182 			ic->ic_flags &= ~IEEE80211_F_WME;
   2183 		error = ENETRESET;		/* XXX maybe not for station? */
   2184 		break;
   2185 	case IEEE80211_IOC_HIDESSID:
   2186 		if (ireq->i_val)
   2187 			ic->ic_flags |= IEEE80211_F_HIDESSID;
   2188 		else
   2189 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
   2190 		error = ENETRESET;
   2191 		break;
   2192 	case IEEE80211_IOC_APBRIDGE:
   2193 		if (ireq->i_val == 0)
   2194 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
   2195 		else
   2196 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
   2197 		break;
   2198 	case IEEE80211_IOC_MCASTCIPHER:
   2199 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
   2200 		    !ieee80211_crypto_available(ireq->i_val))
   2201 			return EINVAL;
   2202 		rsn->rsn_mcastcipher = ireq->i_val;
   2203 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2204 		break;
   2205 	case IEEE80211_IOC_MCASTKEYLEN:
   2206 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2207 			return EINVAL;
   2208 		/* XXX no way to verify driver capability */
   2209 		rsn->rsn_mcastkeylen = ireq->i_val;
   2210 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2211 		break;
   2212 	case IEEE80211_IOC_UCASTCIPHERS:
   2213 		/*
   2214 		 * Convert user-specified cipher set to the set
   2215 		 * we can support (via hardware or software).
   2216 		 * NB: this logic intentionally ignores unknown and
   2217 		 * unsupported ciphers so folks can specify 0xff or
   2218 		 * similar and get all available ciphers.
   2219 		 */
   2220 		caps = 0;
   2221 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
   2222 			if ((ireq->i_val & (1<<j)) &&
   2223 			    ((ic->ic_caps & cipher2cap(j)) ||
   2224 			     ieee80211_crypto_available(j)))
   2225 				caps |= 1<<j;
   2226 		if (caps == 0)			/* nothing available */
   2227 			return EINVAL;
   2228 		/* XXX verify ciphers ok for unicast use? */
   2229 		/* XXX disallow if running as it'll have no effect */
   2230 		rsn->rsn_ucastcipherset = caps;
   2231 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2232 		break;
   2233 	case IEEE80211_IOC_UCASTCIPHER:
   2234 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
   2235 			return EINVAL;
   2236 		rsn->rsn_ucastcipher = ireq->i_val;
   2237 		break;
   2238 	case IEEE80211_IOC_UCASTKEYLEN:
   2239 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2240 			return EINVAL;
   2241 		/* XXX no way to verify driver capability */
   2242 		rsn->rsn_ucastkeylen = ireq->i_val;
   2243 		break;
   2244 	case IEEE80211_IOC_DRIVER_CAPS:
   2245 		/* NB: for testing */
   2246 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
   2247 			       ((u_int16_t) ireq->i_len);
   2248 		break;
   2249 	case IEEE80211_IOC_KEYMGTALGS:
   2250 		/* XXX check */
   2251 		rsn->rsn_keymgmtset = ireq->i_val;
   2252 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2253 		break;
   2254 	case IEEE80211_IOC_RSNCAPS:
   2255 		/* XXX check */
   2256 		rsn->rsn_caps = ireq->i_val;
   2257 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2258 		break;
   2259 	case IEEE80211_IOC_BSSID:
   2260 		/* NB: should only be set when in STA mode */
   2261 		if (ic->ic_opmode != IEEE80211_M_STA)
   2262 			return EINVAL;
   2263 		if (ireq->i_len != sizeof(tmpbssid))
   2264 			return EINVAL;
   2265 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
   2266 		if (error)
   2267 			break;
   2268 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
   2269 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
   2270 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2271 		else
   2272 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2273 		error = ENETRESET;
   2274 		break;
   2275 	case IEEE80211_IOC_CHANLIST:
   2276 		error = ieee80211_ioctl_setchanlist(ic, ireq);
   2277 		break;
   2278 	case IEEE80211_IOC_SCAN_REQ:
   2279 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
   2280 			break;
   2281 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
   2282 		if (error == 0)		/* XXX background scan */
   2283 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2284 		break;
   2285 	case IEEE80211_IOC_ADDMAC:
   2286 	case IEEE80211_IOC_DELMAC:
   2287 		error = ieee80211_ioctl_macmac(ic, ireq);
   2288 		break;
   2289 	case IEEE80211_IOC_MACCMD:
   2290 		error = ieee80211_ioctl_maccmd(ic, ireq);
   2291 		break;
   2292 	case IEEE80211_IOC_STA_TXPOW:
   2293 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
   2294 		break;
   2295 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   2296 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   2297 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   2298 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   2299 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   2300 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   2301 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
   2302 		break;
   2303 	case IEEE80211_IOC_DTIM_PERIOD:
   2304 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2305 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2306 			return EINVAL;
   2307 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
   2308 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
   2309 			ic->ic_dtim_period = ireq->i_val;
   2310 			error = ENETRESET;		/* requires restart */
   2311 		} else
   2312 			error = EINVAL;
   2313 		break;
   2314 	case IEEE80211_IOC_BEACON_INTERVAL:
   2315 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2316 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2317 			return EINVAL;
   2318 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
   2319 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
   2320 			ic->ic_lintval = ireq->i_val;
   2321 			error = ENETRESET;		/* requires restart */
   2322 		} else
   2323 			error = EINVAL;
   2324 		break;
   2325 	default:
   2326 		error = EINVAL;
   2327 		break;
   2328 	}
   2329 	if (error == ENETRESET && !IS_UP_AUTO(ic))
   2330 		error = 0;
   2331 	return error;
   2332 }
   2333 
   2334 #ifdef __FreeBSD__
   2335 int
   2336 ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2337 {
   2338 	struct ifnet *ifp = ic->ic_ifp;
   2339 	int error = 0;
   2340 	struct ifreq *ifr;
   2341 	struct ifaddr *ifa;			/* XXX */
   2342 
   2343 	switch (cmd) {
   2344 	case SIOCSIFMEDIA:
   2345 	case SIOCGIFMEDIA:
   2346 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
   2347 				&ic->ic_media, cmd);
   2348 		break;
   2349 	case SIOCG80211:
   2350 		error = ieee80211_ioctl_get80211(ic, cmd,
   2351 				(struct ieee80211req *) data);
   2352 		break;
   2353 	case SIOCS80211:
   2354 		error = suser(curthread);
   2355 		if (error == 0)
   2356 			error = ieee80211_ioctl_set80211(ic, cmd,
   2357 					(struct ieee80211req *) data);
   2358 		break;
   2359 	case SIOCGIFGENERIC:
   2360 		error = ieee80211_cfgget(ic, cmd, data);
   2361 		break;
   2362 	case SIOCSIFGENERIC:
   2363 		error = suser(curthread);
   2364 		if (error)
   2365 			break;
   2366 		error = ieee80211_cfgset(ic, cmd, data);
   2367 		break;
   2368 	case SIOCG80211STATS:
   2369 		ifr = (struct ifreq *)data;
   2370 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
   2371 		break;
   2372 	case SIOCSIFMTU:
   2373 		ifr = (struct ifreq *)data;
   2374 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2375 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2376 			error = EINVAL;
   2377 		else
   2378 			ifp->if_mtu = ifr->ifr_mtu;
   2379 		break;
   2380 	case SIOCSIFADDR:
   2381 		/*
   2382 		 * XXX Handle this directly so we can supress if_init calls.
   2383 		 * XXX This should be done in ether_ioctl but for the moment
   2384 		 * XXX there are too many other parts of the system that
   2385 		 * XXX set IFF_UP and so supress if_init being called when
   2386 		 * XXX it should be.
   2387 		 */
   2388 		ifa = (struct ifaddr *) data;
   2389 		switch (ifa->ifa_addr->sa_family) {
   2390 #ifdef INET
   2391 		case AF_INET:
   2392 			if ((ifp->if_flags & IFF_UP) == 0) {
   2393 				ifp->if_flags |= IFF_UP;
   2394 				ifp->if_init(ifp->if_softc);
   2395 			}
   2396 			arp_ifinit(ifp, ifa);
   2397 			break;
   2398 #endif
   2399 #ifdef IPX
   2400 		/*
   2401 		 * XXX - This code is probably wrong,
   2402 		 *	 but has been copied many times.
   2403 		 */
   2404 		case AF_IPX: {
   2405 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
   2406 			struct arpcom *ac = (struct arpcom *)ifp;
   2407 
   2408 			if (ipx_nullhost(*ina))
   2409 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
   2410 			else
   2411 				bcopy((caddr_t) ina->x_host.c_host,
   2412 				      (caddr_t) ac->ac_enaddr,
   2413 				      sizeof(ac->ac_enaddr));
   2414 			/* fall thru... */
   2415 		}
   2416 #endif
   2417 		default:
   2418 			if ((ifp->if_flags & IFF_UP) == 0) {
   2419 				ifp->if_flags |= IFF_UP;
   2420 				ifp->if_init(ifp->if_softc);
   2421 			}
   2422 			break;
   2423 		}
   2424 		break;
   2425 	default:
   2426 		error = ether_ioctl(ifp, cmd, data);
   2427 		break;
   2428 	}
   2429 	return error;
   2430 }
   2431 #endif /* __FreeBSD__ */
   2432 
   2433 #ifdef __NetBSD__
   2434 int
   2435 ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2436 {
   2437 	struct ifnet *ifp = ic->ic_ifp;
   2438 	struct ifreq *ifr = (struct ifreq *)data;
   2439 	int i, error = 0, kid, klen, s;
   2440 	struct ieee80211_key *k;
   2441 	struct ieee80211_nwid nwid;
   2442 	struct ieee80211_nwkey *nwkey;
   2443 	struct ieee80211_power *power;
   2444 	struct ieee80211_bssid *bssid;
   2445 	struct ieee80211chanreq *chanreq;
   2446 	struct ieee80211_channel *chan;
   2447 	uint32_t oflags;
   2448 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
   2449 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   2450 	};
   2451 	u_int8_t tmpkey[IEEE80211_WEP_NKID][IEEE80211_KEYBUF_SIZE];
   2452 
   2453 	switch (cmd) {
   2454 	case SIOCSIFMEDIA:
   2455 	case SIOCGIFMEDIA:
   2456 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   2457 		break;
   2458 	case SIOCG80211:
   2459 		error = ieee80211_ioctl_get80211(ic, cmd,
   2460 				(struct ieee80211req *) data);
   2461 		break;
   2462 	case SIOCS80211:
   2463 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   2464 			break;
   2465 		error = ieee80211_ioctl_set80211(ic, cmd,
   2466 				(struct ieee80211req *) data);
   2467 		break;
   2468 	case SIOCS80211NWID:
   2469 		if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
   2470 			break;
   2471 		if (nwid.i_len > IEEE80211_NWID_LEN) {
   2472 			error = EINVAL;
   2473 			break;
   2474 		}
   2475 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   2476 		ic->ic_des_esslen = nwid.i_len;
   2477 		memcpy(ic->ic_des_essid, nwid.i_nwid, nwid.i_len);
   2478 		error = ENETRESET;
   2479 		break;
   2480 	case SIOCG80211NWID:
   2481 		memset(&nwid, 0, sizeof(nwid));
   2482 		switch (ic->ic_state) {
   2483 		case IEEE80211_S_INIT:
   2484 		case IEEE80211_S_SCAN:
   2485 			nwid.i_len = ic->ic_des_esslen;
   2486 			memcpy(nwid.i_nwid, ic->ic_des_essid, nwid.i_len);
   2487 			break;
   2488 		default:
   2489 			nwid.i_len = ic->ic_bss->ni_esslen;
   2490 			memcpy(nwid.i_nwid, ic->ic_bss->ni_essid, nwid.i_len);
   2491 			break;
   2492 		}
   2493 		error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
   2494 		break;
   2495 	case SIOCS80211NWKEY:
   2496 		nwkey = (struct ieee80211_nwkey *)data;
   2497 		/* transmit key index out of range? */
   2498 		kid = nwkey->i_defkid - 1;
   2499 		if (kid < 0 || kid >= IEEE80211_WEP_NKID) {
   2500 			error = EINVAL;
   2501 			break;
   2502 		}
   2503 		/* no such transmit key is set? */
   2504 		if (nwkey->i_key[kid].i_keylen == 0 ||
   2505 		    (nwkey->i_key[kid].i_keylen == -1 &&
   2506 		     ic->ic_nw_keys[kid].wk_keylen == 0)) {
   2507 			if (nwkey->i_wepon != IEEE80211_NWKEY_OPEN) {
   2508 				error = EINVAL;
   2509 				break;
   2510 			}
   2511 		}
   2512 		/* check key lengths */
   2513 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2514 			klen = nwkey->i_key[kid].i_keylen;
   2515 			if ((klen > 0 &&
   2516 			    klen < IEEE80211_WEP_KEYLEN) ||
   2517 			    klen > sizeof(ic->ic_nw_keys[kid].wk_key)) {
   2518 				error = EINVAL;
   2519 				break;
   2520 			}
   2521 		}
   2522 
   2523 		if (error)
   2524 			break;
   2525 
   2526 		/* copy in keys */
   2527 		(void)memset(tmpkey, 0, sizeof(tmpkey));
   2528 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2529 			klen = nwkey->i_key[kid].i_keylen;
   2530 			if (klen <= 0)
   2531 				continue;
   2532 			if ((error = copyin(nwkey->i_key[kid].i_keydat,
   2533 			    tmpkey[kid], klen)) != 0)
   2534 				break;
   2535 		}
   2536 
   2537 		if (error)
   2538 			break;
   2539 
   2540 		/* set keys */
   2541 		ieee80211_key_update_begin(ic);
   2542 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2543 			klen = nwkey->i_key[kid].i_keylen;
   2544 			if (klen <= 0)
   2545 				continue;
   2546 			k = &ic->ic_nw_keys[kid];
   2547 			k->wk_keyix = kid;
   2548 			if (!ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   2549 			    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2550 				error = EINVAL;
   2551 				continue;
   2552 			}
   2553 			k->wk_keylen = nwkey->i_key[kid].i_keylen;
   2554 			(void)memcpy(k->wk_key, tmpkey[kid],
   2555 			    sizeof(tmpkey[kid]));
   2556 			if (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   2557 				error = EINVAL;
   2558 		}
   2559 		ieee80211_key_update_end(ic);
   2560 
   2561 		if (error)
   2562 			break;
   2563 
   2564 		/* delete keys */
   2565 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2566 			klen = nwkey->i_key[kid].i_keylen;
   2567 			k = &ic->ic_nw_keys[kid];
   2568 			if (klen <= 0)
   2569 				(void)ieee80211_crypto_delkey(ic, k);
   2570 		}
   2571 
   2572 		/* set transmit key */
   2573 		kid = nwkey->i_defkid - 1;
   2574 		if (ic->ic_def_txkey != kid) {
   2575 			ic->ic_def_txkey = kid;
   2576 			error = ENETRESET;
   2577 		}
   2578 		oflags = ic->ic_flags;
   2579 		if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN) {
   2580 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2581 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2582 		} else {
   2583 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2584 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2585 		}
   2586 		if (oflags != ic->ic_flags)
   2587 			error = ENETRESET;
   2588 		break;
   2589 	case SIOCG80211NWKEY:
   2590 		nwkey = (struct ieee80211_nwkey *)data;
   2591 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2592 			nwkey->i_wepon = IEEE80211_NWKEY_WEP;
   2593 		else
   2594 			nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
   2595 		nwkey->i_defkid = ic->ic_def_txkey + 1;
   2596 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2597 			if (nwkey->i_key[i].i_keydat == NULL)
   2598 				continue;
   2599 			/* do not show any keys to non-root user */
   2600 			if ((error = suser(curproc->p_ucred,
   2601 			    &curproc->p_acflag)) != 0)
   2602 				break;
   2603 			nwkey->i_key[i].i_keylen = ic->ic_nw_keys[i].wk_keylen;
   2604 			if ((error = copyout(ic->ic_nw_keys[i].wk_key,
   2605 			    nwkey->i_key[i].i_keydat,
   2606 			    ic->ic_nw_keys[i].wk_keylen)) != 0)
   2607 				break;
   2608 		}
   2609 		break;
   2610 	case SIOCS80211POWER:
   2611 		power = (struct ieee80211_power *)data;
   2612 		ic->ic_lintval = power->i_maxsleep;
   2613 		if (power->i_enabled != 0) {
   2614 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2615 				error = EINVAL;
   2616 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2617 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2618 				error = ENETRESET;
   2619 			}
   2620 		} else {
   2621 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2622 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2623 				error = ENETRESET;
   2624 			}
   2625 		}
   2626 		break;
   2627 	case SIOCG80211POWER:
   2628 		power = (struct ieee80211_power *)data;
   2629 		power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
   2630 		power->i_maxsleep = ic->ic_lintval;
   2631 		break;
   2632 	case SIOCS80211BSSID:
   2633 		bssid = (struct ieee80211_bssid *)data;
   2634 		if (IEEE80211_ADDR_EQ(bssid->i_bssid, empty_macaddr))
   2635 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2636 		else {
   2637 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2638 			IEEE80211_ADDR_COPY(ic->ic_des_bssid, bssid->i_bssid);
   2639 		}
   2640 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2641 			break;
   2642 		switch (ic->ic_state) {
   2643 		case IEEE80211_S_INIT:
   2644 		case IEEE80211_S_SCAN:
   2645 			error = ENETRESET;
   2646 			break;
   2647 		default:
   2648 			if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
   2649 			    !IEEE80211_ADDR_EQ(ic->ic_des_bssid,
   2650 			    ic->ic_bss->ni_bssid))
   2651 				error = ENETRESET;
   2652 			break;
   2653 		}
   2654 		break;
   2655 	case SIOCG80211BSSID:
   2656 		bssid = (struct ieee80211_bssid *)data;
   2657 		switch (ic->ic_state) {
   2658 		case IEEE80211_S_INIT:
   2659 		case IEEE80211_S_SCAN:
   2660 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2661 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2662 				    ic->ic_myaddr);
   2663 			else if (ic->ic_flags & IEEE80211_F_DESBSSID)
   2664 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2665 				    ic->ic_des_bssid);
   2666 			else
   2667 				memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   2668 			break;
   2669 		default:
   2670 			IEEE80211_ADDR_COPY(bssid->i_bssid,
   2671 			    ic->ic_bss->ni_bssid);
   2672 			break;
   2673 		}
   2674 		break;
   2675 	case SIOCS80211CHANNEL:
   2676 		chanreq = (struct ieee80211chanreq *)data;
   2677 		if (chanreq->i_channel == IEEE80211_CHAN_ANY)
   2678 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2679 		else if (chanreq->i_channel > IEEE80211_CHAN_MAX ||
   2680 		    isclr(ic->ic_chan_active, chanreq->i_channel)) {
   2681 			error = EINVAL;
   2682 			break;
   2683 		} else
   2684 			ic->ic_ibss_chan = ic->ic_des_chan =
   2685 			    &ic->ic_channels[chanreq->i_channel];
   2686 		switch (ic->ic_state) {
   2687 		case IEEE80211_S_INIT:
   2688 		case IEEE80211_S_SCAN:
   2689 			error = ENETRESET;
   2690 			break;
   2691 		default:
   2692 			if (ic->ic_opmode == IEEE80211_M_STA) {
   2693 				if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2694 				    ic->ic_bss->ni_chan != ic->ic_des_chan)
   2695 					error = ENETRESET;
   2696 			} else {
   2697 				if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
   2698 					error = ENETRESET;
   2699 			}
   2700 			break;
   2701 		}
   2702 		break;
   2703 	case SIOCG80211CHANNEL:
   2704 		chanreq = (struct ieee80211chanreq *)data;
   2705 		switch (ic->ic_state) {
   2706 		case IEEE80211_S_INIT:
   2707 		case IEEE80211_S_SCAN:
   2708 			if (ic->ic_opmode == IEEE80211_M_STA)
   2709 				chan = ic->ic_des_chan;
   2710 			else
   2711 				chan = ic->ic_ibss_chan;
   2712 			break;
   2713 		default:
   2714 			chan = ic->ic_bss->ni_chan;
   2715 			break;
   2716 		}
   2717 		chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
   2718 		break;
   2719 	case SIOCGIFGENERIC:
   2720 		error = ieee80211_cfgget(ic, cmd, data);
   2721 		break;
   2722 	case SIOCSIFGENERIC:
   2723 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   2724 		if (error)
   2725 			break;
   2726 		error = ieee80211_cfgset(ic, cmd, data);
   2727 		break;
   2728 	case SIOCG80211ZSTATS:
   2729 	case SIOCG80211STATS:
   2730 		ifr = (struct ifreq *)data;
   2731 		s = splnet();
   2732 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
   2733 		if (cmd == SIOCG80211ZSTATS)
   2734 			(void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
   2735 		splx(s);
   2736 		break;
   2737 	case SIOCSIFMTU:
   2738 		ifr = (struct ifreq *)data;
   2739 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2740 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2741 			error = EINVAL;
   2742 		else
   2743 			ifp->if_mtu = ifr->ifr_mtu;
   2744 		break;
   2745 	default:
   2746 		error = ether_ioctl(ifp, cmd, data);
   2747 		break;
   2748 	}
   2749 	return error;
   2750 }
   2751 #endif /* __NetBSD__ */
   2752