Home | History | Annotate | Line # | Download | only in net80211
ieee80211_ioctl.c revision 1.23
      1 /*	$NetBSD: ieee80211_ioctl.c,v 1.23 2005/07/27 07:01:25 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.25 2005/07/06 15:38:27 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_ioctl.c,v 1.23 2005/07/27 07:01:25 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 #ifdef COMPAT_FREEBSD
    845 static struct ieee80211_channel *
    846 getcurchan(struct ieee80211com *ic)
    847 {
    848 	switch (ic->ic_state) {
    849 	case IEEE80211_S_INIT:
    850 	case IEEE80211_S_SCAN:
    851 		return ic->ic_des_chan;
    852 	default:
    853 		return ic->ic_ibss_chan;
    854 	}
    855 }
    856 #endif /* COMPAT_FREEBSD */
    857 
    858 static int
    859 cap2cipher(int flag)
    860 {
    861 	switch (flag) {
    862 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
    863 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
    864 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
    865 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
    866 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
    867 	}
    868 	return -1;
    869 }
    870 
    871 static int
    872 ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
    873 {
    874 	struct ieee80211_node *ni;
    875 	struct ieee80211req_key ik;
    876 	struct ieee80211_key *wk;
    877 	const struct ieee80211_cipher *cip;
    878 	u_int kid;
    879 	int error;
    880 
    881 	if (ireq->i_len != sizeof(ik))
    882 		return EINVAL;
    883 	error = copyin(ireq->i_data, &ik, sizeof(ik));
    884 	if (error)
    885 		return error;
    886 	kid = ik.ik_keyix;
    887 	if (kid == IEEE80211_KEYIX_NONE) {
    888 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
    889 		if (ni == NULL)
    890 			return EINVAL;		/* XXX */
    891 		wk = &ni->ni_ucastkey;
    892 	} else {
    893 		if (kid >= IEEE80211_WEP_NKID)
    894 			return EINVAL;
    895 		wk = &ic->ic_nw_keys[kid];
    896 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
    897 		ni = NULL;
    898 	}
    899 	cip = wk->wk_cipher;
    900 	ik.ik_type = cip->ic_cipher;
    901 	ik.ik_keylen = wk->wk_keylen;
    902 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
    903 	if (wk->wk_keyix == ic->ic_def_txkey)
    904 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
    905 	if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
    906 		/* NB: only root can read key data */
    907 		ik.ik_keyrsc = wk->wk_keyrsc;
    908 		ik.ik_keytsc = wk->wk_keytsc;
    909 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
    910 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
    911 			memcpy(ik.ik_keydata+wk->wk_keylen,
    912 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
    913 				IEEE80211_MICBUF_SIZE);
    914 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
    915 		}
    916 	} else {
    917 		ik.ik_keyrsc = 0;
    918 		ik.ik_keytsc = 0;
    919 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
    920 	}
    921 	if (ni != NULL)
    922 		ieee80211_free_node(ni);
    923 	return copyout(&ik, ireq->i_data, sizeof(ik));
    924 }
    925 
    926 static int
    927 ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
    928 {
    929 
    930 	if (sizeof(ic->ic_chan_active) > ireq->i_len)
    931 		ireq->i_len = sizeof(ic->ic_chan_active);
    932 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
    933 }
    934 
    935 static int
    936 ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
    937 {
    938 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
    939 	int i, space;
    940 
    941 	/*
    942 	 * Since channel 0 is not available for DS, channel 1
    943 	 * is assigned to LSB on WaveLAN.
    944 	 */
    945 	if (ic->ic_phytype == IEEE80211_T_DS)
    946 		i = 1;
    947 	else
    948 		i = 0;
    949 	memset(&chans, 0, sizeof(chans));
    950 	for (; i <= IEEE80211_CHAN_MAX; i++)
    951 		if (isset(ic->ic_chan_avail, i)) {
    952 			struct ieee80211_channel *c = &ic->ic_channels[i];
    953 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
    954 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
    955 			chans.ic_nchans++;
    956 		}
    957 	space = __offsetof(struct ieee80211req_chaninfo,
    958 			ic_chans[chans.ic_nchans]);
    959 	if (space > ireq->i_len)
    960 		space = ireq->i_len;
    961 	return copyout(&chans, ireq->i_data, space);
    962 }
    963 
    964 static int
    965 ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
    966 {
    967 	struct ieee80211_node *ni;
    968 	struct ieee80211req_wpaie wpaie;
    969 	int error;
    970 
    971 	if (ireq->i_len < IEEE80211_ADDR_LEN)
    972 		return EINVAL;
    973 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
    974 	if (error != 0)
    975 		return error;
    976 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
    977 	if (ni == NULL)
    978 		return EINVAL;		/* XXX */
    979 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
    980 	if (ni->ni_wpa_ie != NULL) {
    981 		int ielen = ni->ni_wpa_ie[1] + 2;
    982 		if (ielen > sizeof(wpaie.wpa_ie))
    983 			ielen = sizeof(wpaie.wpa_ie);
    984 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
    985 	}
    986 	ieee80211_free_node(ni);
    987 	if (ireq->i_len > sizeof(wpaie))
    988 		ireq->i_len = sizeof(wpaie);
    989 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
    990 }
    991 
    992 static int
    993 ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
    994 {
    995 	struct ieee80211_node *ni;
    996 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
    997 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
    998 	int error;
    999 
   1000 	if (ireq->i_len < off)
   1001 		return EINVAL;
   1002 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
   1003 	if (error != 0)
   1004 		return error;
   1005 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
   1006 	if (ni == NULL)
   1007 		return EINVAL;		/* XXX */
   1008 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
   1009 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
   1010 	/* NB: copy out only the statistics */
   1011 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
   1012 			ireq->i_len - off);
   1013 	ieee80211_free_node(ni);
   1014 	return error;
   1015 }
   1016 
   1017 static void
   1018 get_scan_result(struct ieee80211req_scan_result *sr,
   1019 	const struct ieee80211_node *ni)
   1020 {
   1021 	struct ieee80211com *ic = ni->ni_ic;
   1022 
   1023 	memset(sr, 0, sizeof(*sr));
   1024 	sr->isr_ssid_len = ni->ni_esslen;
   1025 	if (ni->ni_wpa_ie != NULL)
   1026 		sr->isr_ie_len += 2+ni->ni_wpa_ie[1];
   1027 	if (ni->ni_wme_ie != NULL)
   1028 		sr->isr_ie_len += 2+ni->ni_wme_ie[1];
   1029 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
   1030 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
   1031 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
   1032 		sr->isr_freq = ni->ni_chan->ic_freq;
   1033 		sr->isr_flags = ni->ni_chan->ic_flags;
   1034 	}
   1035 	sr->isr_rssi = ic->ic_node_getrssi(ni);
   1036 	sr->isr_intval = ni->ni_intval;
   1037 	sr->isr_capinfo = ni->ni_capinfo;
   1038 	sr->isr_erp = ni->ni_erp;
   1039 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
   1040 	sr->isr_nrates = ni->ni_rates.rs_nrates;
   1041 	if (sr->isr_nrates > 15)
   1042 		sr->isr_nrates = 15;
   1043 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
   1044 }
   1045 
   1046 static int
   1047 ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
   1048 {
   1049 	union {
   1050 		struct ieee80211req_scan_result res;
   1051 		char data[512];		/* XXX shrink? */
   1052 	} u;
   1053 	struct ieee80211req_scan_result *sr = &u.res;
   1054 	struct ieee80211_node_table *nt;
   1055 	struct ieee80211_node *ni;
   1056 	int error, space;
   1057 	u_int8_t *p, *cp;
   1058 
   1059 	p = ireq->i_data;
   1060 	space = ireq->i_len;
   1061 	error = 0;
   1062 	/* XXX locking */
   1063 	nt =  &ic->ic_scan;
   1064 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1065 		/* NB: skip pre-scan node state */
   1066 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
   1067 			continue;
   1068 		get_scan_result(sr, ni);
   1069 		if (sr->isr_len > sizeof(u))
   1070 			continue;		/* XXX */
   1071 		if (space < sr->isr_len)
   1072 			break;
   1073 		cp = (u_int8_t *)(sr+1);
   1074 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
   1075 		cp += ni->ni_esslen;
   1076 		if (ni->ni_wpa_ie != NULL) {
   1077 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1078 			cp += 2+ni->ni_wpa_ie[1];
   1079 		}
   1080 		if (ni->ni_wme_ie != NULL) {
   1081 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1082 			cp += 2+ni->ni_wme_ie[1];
   1083 		}
   1084 		error = copyout(sr, p, sr->isr_len);
   1085 		if (error)
   1086 			break;
   1087 		p += sr->isr_len;
   1088 		space -= sr->isr_len;
   1089 	}
   1090 	ireq->i_len -= space;
   1091 	return error;
   1092 }
   1093 
   1094 static void
   1095 get_sta_info(struct ieee80211req_sta_info *si, const struct ieee80211_node *ni)
   1096 {
   1097 	struct ieee80211com *ic = ni->ni_ic;
   1098 
   1099 	si->isi_ie_len = 0;
   1100 	if (ni->ni_wpa_ie != NULL)
   1101 		si->isi_ie_len += 2+ni->ni_wpa_ie[1];
   1102 	if (ni->ni_wme_ie != NULL)
   1103 		si->isi_ie_len += 2+ni->ni_wme_ie[1];
   1104 	si->isi_len = sizeof(*si) + si->isi_ie_len, sizeof(u_int32_t);
   1105 	si->isi_len = roundup(si->isi_len, sizeof(u_int32_t));
   1106 	si->isi_freq = ni->ni_chan->ic_freq;
   1107 	si->isi_flags = ni->ni_chan->ic_flags;
   1108 	si->isi_state = ni->ni_flags;
   1109 	si->isi_authmode = ni->ni_authmode;
   1110 	si->isi_rssi = ic->ic_node_getrssi(ni);
   1111 	si->isi_capinfo = ni->ni_capinfo;
   1112 	si->isi_erp = ni->ni_erp;
   1113 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
   1114 	si->isi_nrates = ni->ni_rates.rs_nrates;
   1115 	if (si->isi_nrates > 15)
   1116 		si->isi_nrates = 15;
   1117 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
   1118 	si->isi_txrate = ni->ni_txrate;
   1119 	si->isi_associd = ni->ni_associd;
   1120 	si->isi_txpower = ni->ni_txpower;
   1121 	si->isi_vlan = ni->ni_vlan;
   1122 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
   1123 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
   1124 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
   1125 	} else {
   1126 		si->isi_txseqs[0] = ni->ni_txseqs[0];
   1127 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
   1128 	}
   1129 	if (ic->ic_opmode == IEEE80211_M_IBSS || ni->ni_associd != 0)
   1130 		si->isi_inact = ic->ic_inact_run;
   1131 	else if (ieee80211_node_is_authorized(ni))
   1132 		si->isi_inact = ic->ic_inact_auth;
   1133 	else
   1134 		si->isi_inact = ic->ic_inact_init;
   1135 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
   1136 }
   1137 
   1138 static int
   1139 ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
   1140 {
   1141 	union {
   1142 		struct ieee80211req_sta_info info;
   1143 		char data[512];		/* XXX shrink? */
   1144 	} u;
   1145 	struct ieee80211req_sta_info *si = &u.info;
   1146 	struct ieee80211_node_table *nt;
   1147 	struct ieee80211_node *ni;
   1148 	int error, space;
   1149 	u_int8_t *p, *cp;
   1150 
   1151 	nt = &ic->ic_sta;
   1152 	p = ireq->i_data;
   1153 	space = ireq->i_len;
   1154 	error = 0;
   1155 	/* XXX locking */
   1156 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1157 		get_sta_info(si, ni);
   1158 		if (si->isi_len > sizeof(u))
   1159 			continue;		/* XXX */
   1160 		if (space < si->isi_len)
   1161 			break;
   1162 		cp = (u_int8_t *)(si+1);
   1163 		if (ni->ni_wpa_ie != NULL) {
   1164 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1165 			cp += 2+ni->ni_wpa_ie[1];
   1166 		}
   1167 		if (ni->ni_wme_ie != NULL) {
   1168 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1169 			cp += 2+ni->ni_wme_ie[1];
   1170 		}
   1171 		error = copyout(si, p, si->isi_len);
   1172 		if (error)
   1173 			break;
   1174 		p += si->isi_len;
   1175 		space -= si->isi_len;
   1176 	}
   1177 	ireq->i_len -= space;
   1178 	return error;
   1179 }
   1180 
   1181 static int
   1182 ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1183 {
   1184 	struct ieee80211_node *ni;
   1185 	struct ieee80211req_sta_txpow txpow;
   1186 	int error;
   1187 
   1188 	if (ireq->i_len != sizeof(txpow))
   1189 		return EINVAL;
   1190 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1191 	if (error != 0)
   1192 		return error;
   1193 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1194 	if (ni == NULL)
   1195 		return EINVAL;		/* XXX */
   1196 	txpow.it_txpow = ni->ni_txpower;
   1197 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
   1198 	ieee80211_free_node(ni);
   1199 	return error;
   1200 }
   1201 
   1202 static int
   1203 ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1204 {
   1205 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1206 	struct wmeParams *wmep;
   1207 	int ac;
   1208 
   1209 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1210 		return EINVAL;
   1211 
   1212 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1213 	if (ac >= WME_NUM_AC)
   1214 		ac = WME_AC_BE;
   1215 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
   1216 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1217 	else
   1218 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1219 	switch (ireq->i_type) {
   1220 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1221 		ireq->i_val = wmep->wmep_logcwmin;
   1222 		break;
   1223 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1224 		ireq->i_val = wmep->wmep_logcwmax;
   1225 		break;
   1226 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1227 		ireq->i_val = wmep->wmep_aifsn;
   1228 		break;
   1229 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1230 		ireq->i_val = wmep->wmep_txopLimit;
   1231 		break;
   1232 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1233 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1234 		ireq->i_val = wmep->wmep_acm;
   1235 		break;
   1236 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1237 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1238 		ireq->i_val = !wmep->wmep_noackPolicy;
   1239 		break;
   1240 	}
   1241 	return 0;
   1242 }
   1243 
   1244 /*
   1245  * When building the kernel with -O2 on the i386 architecture, gcc
   1246  * seems to want to inline this function into ieee80211_ioctl()
   1247  * (which is the only routine that calls it). When this happens,
   1248  * ieee80211_ioctl() ends up consuming an additional 2K of stack
   1249  * space. (Exactly why it needs so much is unclear.) The problem
   1250  * is that it's possible for ieee80211_ioctl() to invoke other
   1251  * routines (including driver init functions) which could then find
   1252  * themselves perilously close to exhausting the stack.
   1253  *
   1254  * To avoid this, we deliberately prevent gcc from inlining this
   1255  * routine. Another way to avoid this is to use less agressive
   1256  * optimization when compiling this file (i.e. -O instead of -O2)
   1257  * but special-casing the compilation of this one module in the
   1258  * build system would be awkward.
   1259  */
   1260 #ifdef __GNUC__
   1261 __attribute__ ((noinline))
   1262 #endif
   1263 static int
   1264 ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1265 {
   1266 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1267 	int error = 0;
   1268 #ifdef COMPAT_FREEBSD
   1269 	u_int kid, len;
   1270 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1271 	char tmpssid[IEEE80211_NWID_LEN];
   1272 #endif /* COMPAT_FREEBSD */
   1273 	u_int m;
   1274 
   1275 	switch (ireq->i_type) {
   1276 #ifdef COMPAT_FREEBSD
   1277 	case IEEE80211_IOC_SSID:
   1278 		switch (ic->ic_state) {
   1279 		case IEEE80211_S_INIT:
   1280 		case IEEE80211_S_SCAN:
   1281 			ireq->i_len = ic->ic_des_esslen;
   1282 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
   1283 			break;
   1284 		default:
   1285 			ireq->i_len = ic->ic_bss->ni_esslen;
   1286 			memcpy(tmpssid, ic->ic_bss->ni_essid,
   1287 				ireq->i_len);
   1288 			break;
   1289 		}
   1290 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
   1291 		break;
   1292 	case IEEE80211_IOC_NUMSSIDS:
   1293 		ireq->i_val = 1;
   1294 		break;
   1295 	case IEEE80211_IOC_WEP:
   1296 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
   1297 			ireq->i_val = IEEE80211_WEP_OFF;
   1298 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
   1299 			ireq->i_val = IEEE80211_WEP_ON;
   1300 		else
   1301 			ireq->i_val = IEEE80211_WEP_MIXED;
   1302 		break;
   1303 	case IEEE80211_IOC_WEPKEY:
   1304 		kid = (u_int) ireq->i_val;
   1305 		if (kid >= IEEE80211_WEP_NKID)
   1306 			return EINVAL;
   1307 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
   1308 		/* NB: only root can read WEP keys */
   1309 		if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
   1310 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
   1311 		} else {
   1312 			bzero(tmpkey, len);
   1313 		}
   1314 		ireq->i_len = len;
   1315 		error = copyout(tmpkey, ireq->i_data, len);
   1316 		break;
   1317 	case IEEE80211_IOC_NUMWEPKEYS:
   1318 		ireq->i_val = IEEE80211_WEP_NKID;
   1319 		break;
   1320 	case IEEE80211_IOC_WEPTXKEY:
   1321 		ireq->i_val = ic->ic_def_txkey;
   1322 		break;
   1323 #endif /* COMPAT_FREEBSD */
   1324 	case IEEE80211_IOC_AUTHMODE:
   1325 		if (ic->ic_flags & IEEE80211_F_WPA)
   1326 			ireq->i_val = IEEE80211_AUTH_WPA;
   1327 		else
   1328 			ireq->i_val = ic->ic_bss->ni_authmode;
   1329 		break;
   1330 #ifdef COMPAT_FREEBSD
   1331 	case IEEE80211_IOC_CHANNEL:
   1332 		ireq->i_val = ieee80211_chan2ieee(ic, getcurchan(ic));
   1333 		break;
   1334 	case IEEE80211_IOC_POWERSAVE:
   1335 		if (ic->ic_flags & IEEE80211_F_PMGTON)
   1336 			ireq->i_val = IEEE80211_POWERSAVE_ON;
   1337 		else
   1338 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
   1339 		break;
   1340 	case IEEE80211_IOC_POWERSAVESLEEP:
   1341 		ireq->i_val = ic->ic_lintval;
   1342 		break;
   1343 #endif /* COMPAT_FREEBSD */
   1344 	case IEEE80211_IOC_RTSTHRESHOLD:
   1345 		ireq->i_val = ic->ic_rtsthreshold;
   1346 		break;
   1347 	case IEEE80211_IOC_PROTMODE:
   1348 		ireq->i_val = ic->ic_protmode;
   1349 		break;
   1350 	case IEEE80211_IOC_TXPOWER:
   1351 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   1352 			return EINVAL;
   1353 		ireq->i_val = ic->ic_txpowlimit;
   1354 		break;
   1355 	case IEEE80211_IOC_MCASTCIPHER:
   1356 		ireq->i_val = rsn->rsn_mcastcipher;
   1357 		break;
   1358 	case IEEE80211_IOC_MCASTKEYLEN:
   1359 		ireq->i_val = rsn->rsn_mcastkeylen;
   1360 		break;
   1361 	case IEEE80211_IOC_UCASTCIPHERS:
   1362 		ireq->i_val = 0;
   1363 		for (m = 0x1; m != 0; m <<= 1)
   1364 			if (rsn->rsn_ucastcipherset & m)
   1365 				ireq->i_val |= 1<<cap2cipher(m);
   1366 		break;
   1367 	case IEEE80211_IOC_UCASTCIPHER:
   1368 		ireq->i_val = rsn->rsn_ucastcipher;
   1369 		break;
   1370 	case IEEE80211_IOC_UCASTKEYLEN:
   1371 		ireq->i_val = rsn->rsn_ucastkeylen;
   1372 		break;
   1373 	case IEEE80211_IOC_KEYMGTALGS:
   1374 		ireq->i_val = rsn->rsn_keymgmtset;
   1375 		break;
   1376 	case IEEE80211_IOC_RSNCAPS:
   1377 		ireq->i_val = rsn->rsn_caps;
   1378 		break;
   1379 	case IEEE80211_IOC_WPA:
   1380 		switch (ic->ic_flags & IEEE80211_F_WPA) {
   1381 		case IEEE80211_F_WPA1:
   1382 			ireq->i_val = 1;
   1383 			break;
   1384 		case IEEE80211_F_WPA2:
   1385 			ireq->i_val = 2;
   1386 			break;
   1387 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
   1388 			ireq->i_val = 3;
   1389 			break;
   1390 		default:
   1391 			ireq->i_val = 0;
   1392 			break;
   1393 		}
   1394 		break;
   1395 	case IEEE80211_IOC_CHANLIST:
   1396 		error = ieee80211_ioctl_getchanlist(ic, ireq);
   1397 		break;
   1398 	case IEEE80211_IOC_ROAMING:
   1399 		ireq->i_val = ic->ic_roaming;
   1400 		break;
   1401 #ifdef COMPAT_FREEBSD
   1402 	case IEEE80211_IOC_PRIVACY:
   1403 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
   1404 		break;
   1405 	case IEEE80211_IOC_DROPUNENCRYPTED:
   1406 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
   1407 		break;
   1408 #endif /* COMPAT_FREEBSD */
   1409 	case IEEE80211_IOC_COUNTERMEASURES:
   1410 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
   1411 		break;
   1412 	case IEEE80211_IOC_DRIVER_CAPS:
   1413 		ireq->i_val = ic->ic_caps>>16;
   1414 		ireq->i_len = ic->ic_caps&0xffff;
   1415 		break;
   1416 	case IEEE80211_IOC_WME:
   1417 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
   1418 		break;
   1419 	case IEEE80211_IOC_HIDESSID:
   1420 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
   1421 		break;
   1422 	case IEEE80211_IOC_APBRIDGE:
   1423 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
   1424 		break;
   1425 	case IEEE80211_IOC_OPTIE:
   1426 		if (ic->ic_opt_ie == NULL)
   1427 			return EINVAL;
   1428 		/* NB: truncate, caller can check length */
   1429 		if (ireq->i_len > ic->ic_opt_ie_len)
   1430 			ireq->i_len = ic->ic_opt_ie_len;
   1431 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
   1432 		break;
   1433 	case IEEE80211_IOC_WPAKEY:
   1434 		error = ieee80211_ioctl_getkey(ic, ireq);
   1435 		break;
   1436 	case IEEE80211_IOC_CHANINFO:
   1437 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
   1438 		break;
   1439 #ifdef COMPAT_FREEBSD
   1440 	case IEEE80211_IOC_BSSID:
   1441 		if (ireq->i_len != IEEE80211_ADDR_LEN)
   1442 			return EINVAL;
   1443 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
   1444 					ic->ic_bss->ni_bssid :
   1445 					ic->ic_des_bssid,
   1446 				ireq->i_data, ireq->i_len);
   1447 		break;
   1448 #endif /* COMPAT_FREEBSD */
   1449 	case IEEE80211_IOC_WPAIE:
   1450 		error = ieee80211_ioctl_getwpaie(ic, ireq);
   1451 		break;
   1452 	case IEEE80211_IOC_SCAN_RESULTS:
   1453 		error = ieee80211_ioctl_getscanresults(ic, ireq);
   1454 		break;
   1455 	case IEEE80211_IOC_STA_STATS:
   1456 		error = ieee80211_ioctl_getstastats(ic, ireq);
   1457 		break;
   1458 	case IEEE80211_IOC_TXPOWMAX:
   1459 		ireq->i_val = ic->ic_bss->ni_txpower;
   1460 		break;
   1461 	case IEEE80211_IOC_STA_TXPOW:
   1462 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
   1463 		break;
   1464 	case IEEE80211_IOC_STA_INFO:
   1465 		error = ieee80211_ioctl_getstainfo(ic, ireq);
   1466 		break;
   1467 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1468 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1469 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1470 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1471 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1472 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   1473 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
   1474 		break;
   1475 	case IEEE80211_IOC_DTIM_PERIOD:
   1476 		ireq->i_val = ic->ic_dtim_period;
   1477 		break;
   1478 #ifdef COMPAT_FREEBSD
   1479 	case IEEE80211_IOC_BEACON_INTERVAL:
   1480 		/* NB: get from ic_bss for station mode */
   1481 		ireq->i_val = ic->ic_bss->ni_intval;
   1482 		break;
   1483 #endif /* COMPAT_FREEBSD */
   1484 	case IEEE80211_IOC_PUREG:
   1485 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
   1486 		break;
   1487 	default:
   1488 		error = EINVAL;
   1489 		break;
   1490 	}
   1491 	return error;
   1492 }
   1493 
   1494 static int
   1495 ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
   1496 {
   1497 	int error;
   1498 	void *ie;
   1499 
   1500 	/*
   1501 	 * NB: Doing this for ap operation could be useful (e.g. for
   1502 	 *     WPA and/or WME) except that it typically is worthless
   1503 	 *     without being able to intervene when processing
   1504 	 *     association response frames--so disallow it for now.
   1505 	 */
   1506 	if (ic->ic_opmode != IEEE80211_M_STA)
   1507 		return EINVAL;
   1508 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
   1509 		return EINVAL;
   1510 	/* NB: data.length is validated by the wireless extensions code */
   1511 	MALLOC(ie, void *, (u_long)ireq->i_len, M_DEVBUF, M_WAITOK);
   1512 	if (ie == NULL)
   1513 		return ENOMEM;
   1514 	error = copyin(ireq->i_data, ie, ireq->i_len);
   1515 	/* XXX sanity check data? */
   1516 	if (ic->ic_opt_ie != NULL)
   1517 		FREE(ic->ic_opt_ie, M_DEVBUF);
   1518 	ic->ic_opt_ie = ie;
   1519 	ic->ic_opt_ie_len = ireq->i_len;
   1520 	return 0;
   1521 }
   1522 
   1523 static int
   1524 ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1525 {
   1526 	struct ieee80211req_key ik;
   1527 	struct ieee80211_node *ni;
   1528 	struct ieee80211_key *wk;
   1529 	u_int16_t kid;
   1530 	int error;
   1531 
   1532 	if (ireq->i_len != sizeof(ik))
   1533 		return EINVAL;
   1534 	error = copyin(ireq->i_data, &ik, sizeof(ik));
   1535 	if (error)
   1536 		return error;
   1537 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
   1538 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
   1539 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
   1540 		return E2BIG;
   1541 	kid = ik.ik_keyix;
   1542 	if (kid == IEEE80211_KEYIX_NONE) {
   1543 		/* XXX unicast keys currently must be tx/rx */
   1544 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
   1545 			return EINVAL;
   1546 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1547 			ni = ieee80211_ref_node(ic->ic_bss);
   1548 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
   1549 				ieee80211_free_node(ni);
   1550 				return EADDRNOTAVAIL;
   1551 			}
   1552 		} else {
   1553 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
   1554 			if (ni == NULL)
   1555 				return ENOENT;
   1556 		}
   1557 		wk = &ni->ni_ucastkey;
   1558 	} else {
   1559 		if (kid >= IEEE80211_WEP_NKID)
   1560 			return EINVAL;
   1561 		wk = &ic->ic_nw_keys[kid];
   1562 		ni = NULL;
   1563 	}
   1564 	error = 0;
   1565 	ieee80211_key_update_begin(ic);
   1566 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
   1567 		wk->wk_keylen = ik.ik_keylen;
   1568 		/* NB: MIC presence is implied by cipher type */
   1569 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
   1570 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
   1571 		wk->wk_keyrsc = ik.ik_keyrsc;
   1572 		wk->wk_keytsc = 0;			/* new key, reset */
   1573 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
   1574 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
   1575 		if (!ieee80211_crypto_setkey(ic, wk,
   1576 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
   1577 			error = EIO;
   1578 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
   1579 			ic->ic_def_txkey = kid;
   1580 	} else
   1581 		error = ENXIO;
   1582 	ieee80211_key_update_end(ic);
   1583 	if (ni != NULL)
   1584 		ieee80211_free_node(ni);
   1585 	return error;
   1586 }
   1587 
   1588 static int
   1589 ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1590 {
   1591 	struct ieee80211req_del_key dk;
   1592 	int kid, error;
   1593 
   1594 	if (ireq->i_len != sizeof(dk))
   1595 		return EINVAL;
   1596 	error = copyin(ireq->i_data, &dk, sizeof(dk));
   1597 	if (error)
   1598 		return error;
   1599 	kid = dk.idk_keyix;
   1600 	/* XXX u_int8_t -> u_int16_t */
   1601 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
   1602 		struct ieee80211_node *ni;
   1603 
   1604 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1605 			ni = ieee80211_ref_node(ic->ic_bss);
   1606 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
   1607 				ieee80211_free_node(ni);
   1608 				return EADDRNOTAVAIL;
   1609 			}
   1610 		} else {
   1611 			ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
   1612 			if (ni == NULL)
   1613 				return ENOENT;
   1614 		}
   1615 		/* XXX error return */
   1616 		ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
   1617 		ieee80211_free_node(ni);
   1618 	} else {
   1619 		if (kid >= IEEE80211_WEP_NKID)
   1620 			return EINVAL;
   1621 		/* XXX error return */
   1622 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
   1623 	}
   1624 	return 0;
   1625 }
   1626 
   1627 #ifndef IEEE80211_NO_HOSTAP
   1628 static void
   1629 domlme(void *arg, struct ieee80211_node *ni)
   1630 {
   1631 	struct ieee80211com *ic = ni->ni_ic;
   1632 	struct ieee80211req_mlme *mlme = arg;
   1633 
   1634 	if (ni->ni_associd != 0) {
   1635 		IEEE80211_SEND_MGMT(ic, ni,
   1636 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
   1637 				IEEE80211_FC0_SUBTYPE_DEAUTH :
   1638 				IEEE80211_FC0_SUBTYPE_DISASSOC,
   1639 			mlme->im_reason);
   1640 	}
   1641 	ieee80211_node_leave(ic, ni);
   1642 }
   1643 #endif /* !IEEE80211_NO_HOSTAP */
   1644 
   1645 static int
   1646 ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
   1647 {
   1648 	struct ieee80211req_mlme mlme;
   1649 	struct ieee80211_node *ni;
   1650 	int error;
   1651 
   1652 	if (ireq->i_len != sizeof(mlme))
   1653 		return EINVAL;
   1654 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
   1655 	if (error)
   1656 		return error;
   1657 	switch (mlme.im_op) {
   1658 	case IEEE80211_MLME_ASSOC:
   1659 		if (ic->ic_opmode != IEEE80211_M_STA)
   1660 			return EINVAL;
   1661 		/* XXX must be in S_SCAN state? */
   1662 
   1663 		if (mlme.im_ssid_len != 0) {
   1664 			/*
   1665 			 * Desired ssid specified; must match both bssid and
   1666 			 * ssid to distinguish ap advertising multiple ssid's.
   1667 			 */
   1668 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
   1669 				mlme.im_macaddr,
   1670 				mlme.im_ssid_len, mlme.im_ssid);
   1671 		} else {
   1672 			/*
   1673 			 * Normal case; just match bssid.
   1674 			 */
   1675 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
   1676 		}
   1677 		if (ni == NULL)
   1678 			return EINVAL;
   1679 		if (!ieee80211_sta_join(ic, ni)) {
   1680 			ieee80211_free_node(ni);
   1681 			return EINVAL;
   1682 		}
   1683 		break;
   1684 	case IEEE80211_MLME_DISASSOC:
   1685 	case IEEE80211_MLME_DEAUTH:
   1686 		switch (ic->ic_opmode) {
   1687 		case IEEE80211_M_STA:
   1688 			/* XXX not quite right */
   1689 			ieee80211_new_state(ic, IEEE80211_S_INIT,
   1690 				mlme.im_reason);
   1691 			break;
   1692 		case IEEE80211_M_HOSTAP:
   1693 #ifndef IEEE80211_NO_HOSTAP
   1694 			/* NB: the broadcast address means do 'em all */
   1695 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
   1696 				if ((ni = ieee80211_find_node(&ic->ic_sta,
   1697 						mlme.im_macaddr)) == NULL)
   1698 					return EINVAL;
   1699 				domlme(&mlme, ni);
   1700 				ieee80211_free_node(ni);
   1701 			} else {
   1702 				ieee80211_iterate_nodes(&ic->ic_sta,
   1703 						domlme, &mlme);
   1704 			}
   1705 #endif /* !IEEE80211_NO_HOSTAP */
   1706 			break;
   1707 		default:
   1708 			return EINVAL;
   1709 		}
   1710 		break;
   1711 	case IEEE80211_MLME_AUTHORIZE:
   1712 	case IEEE80211_MLME_UNAUTHORIZE:
   1713 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
   1714 			return EINVAL;
   1715 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
   1716 		if (ni == NULL)
   1717 			return EINVAL;
   1718 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
   1719 			ieee80211_node_authorize(ic, ni);
   1720 		else
   1721 			ieee80211_node_unauthorize(ic, ni);
   1722 		ieee80211_free_node(ni);
   1723 		break;
   1724 	default:
   1725 		return EINVAL;
   1726 	}
   1727 	return 0;
   1728 }
   1729 
   1730 static int
   1731 ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
   1732 {
   1733 	u_int8_t mac[IEEE80211_ADDR_LEN];
   1734 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1735 	int error;
   1736 
   1737 	if (ireq->i_len != sizeof(mac))
   1738 		return EINVAL;
   1739 	error = copyin(ireq->i_data, mac, ireq->i_len);
   1740 	if (error)
   1741 		return error;
   1742 	if (acl == NULL) {
   1743 		acl = ieee80211_aclator_get("mac");
   1744 		if (acl == NULL || !acl->iac_attach(ic))
   1745 			return EINVAL;
   1746 		ic->ic_acl = acl;
   1747 	}
   1748 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
   1749 		acl->iac_add(ic, mac);
   1750 	else
   1751 		acl->iac_remove(ic, mac);
   1752 	return 0;
   1753 }
   1754 
   1755 static int
   1756 ieee80211_ioctl_maccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
   1757 {
   1758 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1759 
   1760 	switch (ireq->i_val) {
   1761 	case IEEE80211_MACCMD_POLICY_OPEN:
   1762 	case IEEE80211_MACCMD_POLICY_ALLOW:
   1763 	case IEEE80211_MACCMD_POLICY_DENY:
   1764 		if (acl == NULL) {
   1765 			acl = ieee80211_aclator_get("mac");
   1766 			if (acl == NULL || !acl->iac_attach(ic))
   1767 				return EINVAL;
   1768 			ic->ic_acl = acl;
   1769 		}
   1770 		acl->iac_setpolicy(ic, ireq->i_val);
   1771 		break;
   1772 	case IEEE80211_MACCMD_FLUSH:
   1773 		if (acl != NULL)
   1774 			acl->iac_flush(ic);
   1775 		/* NB: silently ignore when not in use */
   1776 		break;
   1777 	case IEEE80211_MACCMD_DETACH:
   1778 		if (acl != NULL) {
   1779 			ic->ic_acl = NULL;
   1780 			acl->iac_detach(ic);
   1781 		}
   1782 		break;
   1783 	default:
   1784 		return EINVAL;
   1785 	}
   1786 	return 0;
   1787 }
   1788 
   1789 static int
   1790 ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
   1791 {
   1792 	struct ieee80211req_chanlist list;
   1793 	u_char chanlist[IEEE80211_CHAN_BYTES];
   1794 	int i, j, error;
   1795 
   1796 	if (ireq->i_len != sizeof(list))
   1797 		return EINVAL;
   1798 	error = copyin(ireq->i_data, &list, sizeof(list));
   1799 	if (error)
   1800 		return error;
   1801 	memset(chanlist, 0, sizeof(chanlist));
   1802 	/*
   1803 	 * Since channel 0 is not available for DS, channel 1
   1804 	 * is assigned to LSB on WaveLAN.
   1805 	 */
   1806 	if (ic->ic_phytype == IEEE80211_T_DS)
   1807 		i = 1;
   1808 	else
   1809 		i = 0;
   1810 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
   1811 		/*
   1812 		 * NB: silently discard unavailable channels so users
   1813 		 *     can specify 1-255 to get all available channels.
   1814 		 */
   1815 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
   1816 			setbit(chanlist, i);
   1817 	}
   1818 	if (ic->ic_ibss_chan == NULL ||
   1819 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
   1820 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
   1821 			if (isset(chanlist, i)) {
   1822 				ic->ic_ibss_chan = &ic->ic_channels[i];
   1823 				goto found;
   1824 			}
   1825 		return EINVAL;			/* no active channels */
   1826 found:
   1827 		;
   1828 	}
   1829 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
   1830 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
   1831 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
   1832 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   1833 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
   1834 }
   1835 
   1836 static int
   1837 ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1838 {
   1839 	struct ieee80211_node *ni;
   1840 	struct ieee80211req_sta_txpow txpow;
   1841 	int error;
   1842 
   1843 	if (ireq->i_len != sizeof(txpow))
   1844 		return EINVAL;
   1845 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1846 	if (error != 0)
   1847 		return error;
   1848 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1849 	if (ni == NULL)
   1850 		return EINVAL;		/* XXX */
   1851 	ni->ni_txpower = txpow.it_txpow;
   1852 	ieee80211_free_node(ni);
   1853 	return error;
   1854 }
   1855 
   1856 static int
   1857 ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1858 {
   1859 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1860 	struct wmeParams *wmep, *chanp;
   1861 	int isbss, ac;
   1862 
   1863 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1864 		return EINVAL;
   1865 
   1866 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
   1867 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1868 	if (ac >= WME_NUM_AC)
   1869 		ac = WME_AC_BE;
   1870 	if (isbss) {
   1871 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
   1872 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1873 	} else {
   1874 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
   1875 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1876 	}
   1877 	switch (ireq->i_type) {
   1878 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1879 		if (isbss) {
   1880 			wmep->wmep_logcwmin = ireq->i_val;
   1881 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1882 				chanp->wmep_logcwmin = ireq->i_val;
   1883 		} else {
   1884 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
   1885 				ireq->i_val;
   1886 		}
   1887 		break;
   1888 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1889 		if (isbss) {
   1890 			wmep->wmep_logcwmax = ireq->i_val;
   1891 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1892 				chanp->wmep_logcwmax = ireq->i_val;
   1893 		} else {
   1894 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
   1895 				ireq->i_val;
   1896 		}
   1897 		break;
   1898 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1899 		if (isbss) {
   1900 			wmep->wmep_aifsn = ireq->i_val;
   1901 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1902 				chanp->wmep_aifsn = ireq->i_val;
   1903 		} else {
   1904 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
   1905 		}
   1906 		break;
   1907 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1908 		if (isbss) {
   1909 			wmep->wmep_txopLimit = ireq->i_val;
   1910 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1911 				chanp->wmep_txopLimit = ireq->i_val;
   1912 		} else {
   1913 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
   1914 				ireq->i_val;
   1915 		}
   1916 		break;
   1917 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1918 		wmep->wmep_acm = ireq->i_val;
   1919 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1920 			chanp->wmep_acm = ireq->i_val;
   1921 		break;
   1922 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1923 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
   1924 			(ireq->i_val) == 0;
   1925 		break;
   1926 	}
   1927 	ieee80211_wme_updateparams(ic);
   1928 	return 0;
   1929 }
   1930 
   1931 static int
   1932 cipher2cap(int cipher)
   1933 {
   1934 	switch (cipher) {
   1935 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
   1936 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
   1937 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
   1938 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
   1939 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
   1940 	}
   1941 	return 0;
   1942 }
   1943 
   1944 static int
   1945 ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1946 {
   1947 #ifdef COMPAT_FREEBSD
   1948 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
   1949 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1950 	char tmpssid[IEEE80211_NWID_LEN];
   1951 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
   1952 	struct ieee80211_key *k;
   1953 	u_int kid;
   1954 #endif /* COMPAT_FREEBSD */
   1955 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1956 	int error;
   1957 	const struct ieee80211_authenticator *auth;
   1958 	int j, caps;
   1959 
   1960 	error = 0;
   1961 	switch (ireq->i_type) {
   1962 #ifdef COMPAT_FREEBSD
   1963 	case IEEE80211_IOC_SSID:
   1964 		if (ireq->i_val != 0 ||
   1965 		    ireq->i_len > IEEE80211_NWID_LEN)
   1966 			return EINVAL;
   1967 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
   1968 		if (error)
   1969 			break;
   1970 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   1971 		ic->ic_des_esslen = ireq->i_len;
   1972 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
   1973 		error = ENETRESET;
   1974 		break;
   1975 #endif /* COMPAT_FREEBSD */
   1976 	case IEEE80211_IOC_WEP:
   1977 		switch (ireq->i_val) {
   1978 		case IEEE80211_WEP_OFF:
   1979 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   1980 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1981 			break;
   1982 		case IEEE80211_WEP_ON:
   1983 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1984 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   1985 			break;
   1986 		case IEEE80211_WEP_MIXED:
   1987 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1988 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1989 			break;
   1990 		}
   1991 		error = ENETRESET;
   1992 		break;
   1993 #ifdef COMPAT_FREEBSD
   1994 	case IEEE80211_IOC_WEPKEY:
   1995 		kid = (u_int) ireq->i_val;
   1996 		if (kid >= IEEE80211_WEP_NKID)
   1997 			return EINVAL;
   1998 		k = &ic->ic_nw_keys[kid];
   1999 		if (ireq->i_len == 0) {
   2000 			/* zero-len =>'s delete any existing key */
   2001 			(void) ieee80211_crypto_delkey(ic, k);
   2002 			break;
   2003 		}
   2004 		if (ireq->i_len > sizeof(tmpkey))
   2005 			return EINVAL;
   2006 		memset(tmpkey, 0, sizeof(tmpkey));
   2007 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
   2008 		if (error)
   2009 			break;
   2010 		ieee80211_key_update_begin(ic);
   2011 		k->wk_keyix = kid;	/* NB: force fixed key id */
   2012 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   2013 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2014 			k->wk_keylen = ireq->i_len;
   2015 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
   2016 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   2017 				error = EINVAL;
   2018 		} else
   2019 			error = EINVAL;
   2020 		ieee80211_key_update_end(ic);
   2021 		if (!error)			/* NB: for compatibility */
   2022 			error = ENETRESET;
   2023 		break;
   2024 	case IEEE80211_IOC_WEPTXKEY:
   2025 		kid = (u_int) ireq->i_val;
   2026 		if (kid >= IEEE80211_WEP_NKID &&
   2027 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
   2028 			return EINVAL;
   2029 		ic->ic_def_txkey = kid;
   2030 		error = ENETRESET;	/* push to hardware */
   2031 		break;
   2032 #endif /* COMPAT_FREEBSD */
   2033 	case IEEE80211_IOC_AUTHMODE:
   2034 		switch (ireq->i_val) {
   2035 		case IEEE80211_AUTH_WPA:
   2036 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2037 		case IEEE80211_AUTH_OPEN:	/* open */
   2038 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2039 		case IEEE80211_AUTH_AUTO:	/* auto */
   2040 			auth = ieee80211_authenticator_get(ireq->i_val);
   2041 			if (auth == NULL)
   2042 				return EINVAL;
   2043 			break;
   2044 		default:
   2045 			return EINVAL;
   2046 		}
   2047 		switch (ireq->i_val) {
   2048 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
   2049 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2050 			ireq->i_val = IEEE80211_AUTH_8021X;
   2051 			break;
   2052 		case IEEE80211_AUTH_OPEN:	/* open */
   2053 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
   2054 			break;
   2055 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2056 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2057 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2058 			/* both require a key so mark the PRIVACY capability */
   2059 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2060 			break;
   2061 		case IEEE80211_AUTH_AUTO:	/* auto */
   2062 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2063 			/* XXX PRIVACY handling? */
   2064 			/* XXX what's the right way to do this? */
   2065 			break;
   2066 		}
   2067 		/* NB: authenticator attach/detach happens on state change */
   2068 		ic->ic_bss->ni_authmode = ireq->i_val;
   2069 		/* XXX mixed/mode/usage? */
   2070 		ic->ic_auth = auth;
   2071 		error = ENETRESET;
   2072 		break;
   2073 #ifdef COMPAT_FREEBSD
   2074 	case IEEE80211_IOC_CHANNEL:
   2075 		/* XXX 0xffff overflows 16-bit signed */
   2076 		if (ireq->i_val == 0 ||
   2077 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
   2078 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2079 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
   2080 		    isclr(ic->ic_chan_active, ireq->i_val)) {
   2081 			return EINVAL;
   2082 		} else
   2083 			ic->ic_ibss_chan = ic->ic_des_chan =
   2084 				&ic->ic_channels[ireq->i_val];
   2085 		switch (ic->ic_state) {
   2086 		case IEEE80211_S_INIT:
   2087 		case IEEE80211_S_SCAN:
   2088 			error = ENETRESET;
   2089 			break;
   2090 		default:
   2091 			/*
   2092 			 * If the desired channel has changed (to something
   2093 			 * other than any) and we're not already scanning,
   2094 			 * then kick the state machine.
   2095 			 */
   2096 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2097 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
   2098 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
   2099 				error = ENETRESET;
   2100 			break;
   2101 		}
   2102 		if (error == ENETRESET && ic->ic_opmode == IEEE80211_M_MONITOR)
   2103 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2104 		break;
   2105 	case IEEE80211_IOC_POWERSAVE:
   2106 		switch (ireq->i_val) {
   2107 		case IEEE80211_POWERSAVE_OFF:
   2108 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2109 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2110 				error = ENETRESET;
   2111 			}
   2112 			break;
   2113 		case IEEE80211_POWERSAVE_ON:
   2114 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2115 				error = EINVAL;
   2116 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2117 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2118 				error = ENETRESET;
   2119 			}
   2120 			break;
   2121 		default:
   2122 			error = EINVAL;
   2123 			break;
   2124 		}
   2125 		break;
   2126 	case IEEE80211_IOC_POWERSAVESLEEP:
   2127 		if (ireq->i_val < 0)
   2128 			return EINVAL;
   2129 		ic->ic_lintval = ireq->i_val;
   2130 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2131 		break;
   2132 #endif /* COMPAT_FREEBSD */
   2133 	case IEEE80211_IOC_RTSTHRESHOLD:
   2134 		if (!(IEEE80211_RTS_MIN < ireq->i_val &&
   2135 		      ireq->i_val < IEEE80211_RTS_MAX))
   2136 			return EINVAL;
   2137 		ic->ic_rtsthreshold = ireq->i_val;
   2138 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2139 		break;
   2140 	case IEEE80211_IOC_PROTMODE:
   2141 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
   2142 			return EINVAL;
   2143 		ic->ic_protmode = ireq->i_val;
   2144 		/* NB: if not operating in 11g this can wait */
   2145 		if (ic->ic_curmode == IEEE80211_MODE_11G)
   2146 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2147 		break;
   2148 	case IEEE80211_IOC_TXPOWER:
   2149 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   2150 			return EINVAL;
   2151 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
   2152 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
   2153 			return EINVAL;
   2154 		ic->ic_txpowlimit = ireq->i_val;
   2155 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2156 		break;
   2157 	case IEEE80211_IOC_ROAMING:
   2158 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
   2159 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
   2160 			return EINVAL;
   2161 		ic->ic_roaming = ireq->i_val;
   2162 		/* XXXX reset? */
   2163 		break;
   2164 #ifdef COMPAT_FREEBSD
   2165 	case IEEE80211_IOC_PRIVACY:
   2166 		if (ireq->i_val) {
   2167 			/* XXX check for key state? */
   2168 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2169 		} else
   2170 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2171 		break;
   2172 	case IEEE80211_IOC_DROPUNENCRYPTED:
   2173 		if (ireq->i_val)
   2174 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2175 		else
   2176 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2177 		break;
   2178 #endif /* COMPAT_FREEBSD */
   2179 	case IEEE80211_IOC_WPAKEY:
   2180 		error = ieee80211_ioctl_setkey(ic, ireq);
   2181 		break;
   2182 	case IEEE80211_IOC_DELKEY:
   2183 		error = ieee80211_ioctl_delkey(ic, ireq);
   2184 		break;
   2185 	case IEEE80211_IOC_MLME:
   2186 		error = ieee80211_ioctl_setmlme(ic, ireq);
   2187 		break;
   2188 	case IEEE80211_IOC_OPTIE:
   2189 		error = ieee80211_ioctl_setoptie(ic, ireq);
   2190 		break;
   2191 	case IEEE80211_IOC_COUNTERMEASURES:
   2192 		if (ireq->i_val) {
   2193 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
   2194 				return EINVAL;
   2195 			ic->ic_flags |= IEEE80211_F_COUNTERM;
   2196 		} else
   2197 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
   2198 		break;
   2199 	case IEEE80211_IOC_WPA:
   2200 		if (ireq->i_val > 3)
   2201 			return EINVAL;
   2202 		/* XXX verify ciphers available */
   2203 		ic->ic_flags &= ~IEEE80211_F_WPA;
   2204 		switch (ireq->i_val) {
   2205 		case 1:
   2206 			ic->ic_flags |= IEEE80211_F_WPA1;
   2207 			break;
   2208 		case 2:
   2209 			ic->ic_flags |= IEEE80211_F_WPA2;
   2210 			break;
   2211 		case 3:
   2212 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
   2213 			break;
   2214 		}
   2215 		error = ENETRESET;		/* XXX? */
   2216 		break;
   2217 	case IEEE80211_IOC_WME:
   2218 		if (ireq->i_val) {
   2219 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   2220 				return EINVAL;
   2221 			ic->ic_flags |= IEEE80211_F_WME;
   2222 		} else
   2223 			ic->ic_flags &= ~IEEE80211_F_WME;
   2224 		error = ENETRESET;		/* XXX maybe not for station? */
   2225 		break;
   2226 	case IEEE80211_IOC_HIDESSID:
   2227 		if (ireq->i_val)
   2228 			ic->ic_flags |= IEEE80211_F_HIDESSID;
   2229 		else
   2230 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
   2231 		error = ENETRESET;
   2232 		break;
   2233 	case IEEE80211_IOC_APBRIDGE:
   2234 		if (ireq->i_val == 0)
   2235 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
   2236 		else
   2237 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
   2238 		break;
   2239 	case IEEE80211_IOC_MCASTCIPHER:
   2240 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
   2241 		    !ieee80211_crypto_available(ireq->i_val))
   2242 			return EINVAL;
   2243 		rsn->rsn_mcastcipher = ireq->i_val;
   2244 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2245 		break;
   2246 	case IEEE80211_IOC_MCASTKEYLEN:
   2247 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2248 			return EINVAL;
   2249 		/* XXX no way to verify driver capability */
   2250 		rsn->rsn_mcastkeylen = ireq->i_val;
   2251 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2252 		break;
   2253 	case IEEE80211_IOC_UCASTCIPHERS:
   2254 		/*
   2255 		 * Convert user-specified cipher set to the set
   2256 		 * we can support (via hardware or software).
   2257 		 * NB: this logic intentionally ignores unknown and
   2258 		 * unsupported ciphers so folks can specify 0xff or
   2259 		 * similar and get all available ciphers.
   2260 		 */
   2261 		caps = 0;
   2262 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
   2263 			if ((ireq->i_val & (1<<j)) &&
   2264 			    ((ic->ic_caps & cipher2cap(j)) ||
   2265 			     ieee80211_crypto_available(j)))
   2266 				caps |= 1<<j;
   2267 		if (caps == 0)			/* nothing available */
   2268 			return EINVAL;
   2269 		/* XXX verify ciphers ok for unicast use? */
   2270 		/* XXX disallow if running as it'll have no effect */
   2271 		rsn->rsn_ucastcipherset = caps;
   2272 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2273 		break;
   2274 	case IEEE80211_IOC_UCASTCIPHER:
   2275 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
   2276 			return EINVAL;
   2277 		rsn->rsn_ucastcipher = ireq->i_val;
   2278 		break;
   2279 	case IEEE80211_IOC_UCASTKEYLEN:
   2280 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2281 			return EINVAL;
   2282 		/* XXX no way to verify driver capability */
   2283 		rsn->rsn_ucastkeylen = ireq->i_val;
   2284 		break;
   2285 	case IEEE80211_IOC_DRIVER_CAPS:
   2286 		/* NB: for testing */
   2287 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
   2288 			       ((u_int16_t) ireq->i_len);
   2289 		break;
   2290 	case IEEE80211_IOC_KEYMGTALGS:
   2291 		/* XXX check */
   2292 		rsn->rsn_keymgmtset = ireq->i_val;
   2293 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2294 		break;
   2295 	case IEEE80211_IOC_RSNCAPS:
   2296 		/* XXX check */
   2297 		rsn->rsn_caps = ireq->i_val;
   2298 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2299 		break;
   2300 #ifdef COMPAT_FREEBSD
   2301 	case IEEE80211_IOC_BSSID:
   2302 		/* NB: should only be set when in STA mode */
   2303 		if (ic->ic_opmode != IEEE80211_M_STA)
   2304 			return EINVAL;
   2305 		if (ireq->i_len != sizeof(tmpbssid))
   2306 			return EINVAL;
   2307 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
   2308 		if (error)
   2309 			break;
   2310 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
   2311 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
   2312 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2313 		else
   2314 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2315 		error = ENETRESET;
   2316 		break;
   2317 #endif /* COMPAT_FREEBSD */
   2318 	case IEEE80211_IOC_CHANLIST:
   2319 		error = ieee80211_ioctl_setchanlist(ic, ireq);
   2320 		break;
   2321 	case IEEE80211_IOC_SCAN_REQ:
   2322 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
   2323 			break;
   2324 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
   2325 		if (error == 0)		/* XXX background scan */
   2326 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2327 		break;
   2328 	case IEEE80211_IOC_ADDMAC:
   2329 	case IEEE80211_IOC_DELMAC:
   2330 		error = ieee80211_ioctl_macmac(ic, ireq);
   2331 		break;
   2332 	case IEEE80211_IOC_MACCMD:
   2333 		error = ieee80211_ioctl_maccmd(ic, ireq);
   2334 		break;
   2335 	case IEEE80211_IOC_STA_TXPOW:
   2336 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
   2337 		break;
   2338 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   2339 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   2340 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   2341 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   2342 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   2343 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   2344 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
   2345 		break;
   2346 	case IEEE80211_IOC_DTIM_PERIOD:
   2347 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2348 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2349 			return EINVAL;
   2350 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
   2351 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
   2352 			ic->ic_dtim_period = ireq->i_val;
   2353 			error = ENETRESET;		/* requires restart */
   2354 		} else
   2355 			error = EINVAL;
   2356 		break;
   2357 #ifdef COMPAT_FREEBSD
   2358 	case IEEE80211_IOC_BEACON_INTERVAL:
   2359 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2360 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2361 			return EINVAL;
   2362 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
   2363 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
   2364 			ic->ic_lintval = ireq->i_val;
   2365 			error = ENETRESET;		/* requires restart */
   2366 		} else
   2367 			error = EINVAL;
   2368 		break;
   2369 #endif /* COMPAT_FREEBSD */
   2370 	case IEEE80211_IOC_PUREG:
   2371 		if (ireq->i_val)
   2372 			ic->ic_flags |= IEEE80211_F_PUREG;
   2373 		else
   2374 			ic->ic_flags &= ~IEEE80211_F_PUREG;
   2375 		/* NB: reset only if we're operating on an 11g channel */
   2376 		if (ic->ic_curmode == IEEE80211_MODE_11G)
   2377 			error = ENETRESET;
   2378 		break;
   2379 	default:
   2380 		error = EINVAL;
   2381 		break;
   2382 	}
   2383 	if (error == ENETRESET && !IS_UP_AUTO(ic))
   2384 		error = 0;
   2385 	return error;
   2386 }
   2387 
   2388 #ifdef __FreeBSD__
   2389 int
   2390 ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2391 {
   2392 	struct ifnet *ifp = ic->ic_ifp;
   2393 	int error = 0;
   2394 	struct ifreq *ifr;
   2395 	struct ifaddr *ifa;			/* XXX */
   2396 
   2397 	switch (cmd) {
   2398 	case SIOCSIFMEDIA:
   2399 	case SIOCGIFMEDIA:
   2400 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
   2401 				&ic->ic_media, cmd);
   2402 		break;
   2403 	case SIOCG80211:
   2404 		error = ieee80211_ioctl_get80211(ic, cmd,
   2405 				(struct ieee80211req *) data);
   2406 		break;
   2407 	case SIOCS80211:
   2408 		error = suser(curthread);
   2409 		if (error == 0)
   2410 			error = ieee80211_ioctl_set80211(ic, cmd,
   2411 					(struct ieee80211req *) data);
   2412 		break;
   2413 	case SIOCGIFGENERIC:
   2414 		error = ieee80211_cfgget(ic, cmd, data);
   2415 		break;
   2416 	case SIOCSIFGENERIC:
   2417 		error = suser(curthread);
   2418 		if (error)
   2419 			break;
   2420 		error = ieee80211_cfgset(ic, cmd, data);
   2421 		break;
   2422 	case SIOCG80211STATS:
   2423 		ifr = (struct ifreq *)data;
   2424 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
   2425 		break;
   2426 	case SIOCSIFMTU:
   2427 		ifr = (struct ifreq *)data;
   2428 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2429 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2430 			error = EINVAL;
   2431 		else
   2432 			ifp->if_mtu = ifr->ifr_mtu;
   2433 		break;
   2434 	case SIOCSIFADDR:
   2435 		/*
   2436 		 * XXX Handle this directly so we can supress if_init calls.
   2437 		 * XXX This should be done in ether_ioctl but for the moment
   2438 		 * XXX there are too many other parts of the system that
   2439 		 * XXX set IFF_UP and so supress if_init being called when
   2440 		 * XXX it should be.
   2441 		 */
   2442 		ifa = (struct ifaddr *) data;
   2443 		switch (ifa->ifa_addr->sa_family) {
   2444 #ifdef INET
   2445 		case AF_INET:
   2446 			if ((ifp->if_flags & IFF_UP) == 0) {
   2447 				ifp->if_flags |= IFF_UP;
   2448 				ifp->if_init(ifp->if_softc);
   2449 			}
   2450 			arp_ifinit(ifp, ifa);
   2451 			break;
   2452 #endif
   2453 #ifdef IPX
   2454 		/*
   2455 		 * XXX - This code is probably wrong,
   2456 		 *	 but has been copied many times.
   2457 		 */
   2458 		case AF_IPX: {
   2459 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
   2460 
   2461 			if (ipx_nullhost(*ina))
   2462 				ina->x_host = *(union ipx_host *)
   2463 				    IFP2ENADDR(ifp);
   2464 			else
   2465 				bcopy((caddr_t) ina->x_host.c_host,
   2466 				      (caddr_t) IFP2ENADDR(ifp),
   2467 				      ETHER_ADDR_LEN);
   2468 			/* fall thru... */
   2469 		}
   2470 #endif
   2471 		default:
   2472 			if ((ifp->if_flags & IFF_UP) == 0) {
   2473 				ifp->if_flags |= IFF_UP;
   2474 				ifp->if_init(ifp->if_softc);
   2475 			}
   2476 			break;
   2477 		}
   2478 		break;
   2479 	default:
   2480 		error = ether_ioctl(ifp, cmd, data);
   2481 		break;
   2482 	}
   2483 	return error;
   2484 }
   2485 #endif /* __FreeBSD__ */
   2486 
   2487 #ifdef COMPAT_20
   2488 static void
   2489 ieee80211_get_ostats(struct ieee80211_ostats *ostats,
   2490     struct ieee80211_stats *stats)
   2491 {
   2492 #define	COPYSTATS1(__ostats, __nstats, __dstmemb, __srcmemb, __lastmemb)\
   2493 	(void)memcpy(&(__ostats)->__dstmemb, &(__nstats)->__srcmemb,	\
   2494 	    offsetof(struct ieee80211_stats, __lastmemb) -		\
   2495 	    offsetof(struct ieee80211_stats, __srcmemb))
   2496 #define	COPYSTATS(__ostats, __nstats, __dstmemb, __lastmemb)		\
   2497 	COPYSTATS1(__ostats, __nstats, __dstmemb, __dstmemb, __lastmemb)
   2498 
   2499 	COPYSTATS(ostats, stats, is_rx_badversion, is_rx_unencrypted);
   2500 	COPYSTATS(ostats, stats, is_rx_wepfail, is_rx_beacon);
   2501 	COPYSTATS(ostats, stats, is_rx_rstoobig, is_rx_auth_countermeasures);
   2502 	COPYSTATS(ostats, stats, is_rx_assoc_bss, is_rx_assoc_badwpaie);
   2503 	COPYSTATS(ostats, stats, is_rx_deauth, is_rx_unauth);
   2504 	COPYSTATS1(ostats, stats, is_tx_nombuf, is_tx_nobuf, is_tx_badcipher);
   2505 	COPYSTATS(ostats, stats, is_scan_active, is_crypto_tkip);
   2506 }
   2507 #endif /* COMPAT_20 */
   2508 
   2509 #ifdef __NetBSD__
   2510 int
   2511 ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2512 {
   2513 	struct ifnet *ifp = ic->ic_ifp;
   2514 	struct ifreq *ifr = (struct ifreq *)data;
   2515 	int i, error = 0, kid, klen, s;
   2516 	struct ieee80211_key *k;
   2517 	struct ieee80211_nwid nwid;
   2518 	struct ieee80211_nwkey *nwkey;
   2519 	struct ieee80211_power *power;
   2520 	struct ieee80211_bssid *bssid;
   2521 	struct ieee80211chanreq *chanreq;
   2522 	struct ieee80211_channel *chan;
   2523 	uint32_t oflags;
   2524 #ifdef COMPAT_20
   2525 	struct ieee80211_ostats ostats;
   2526 #endif /* COMPAT_20 */
   2527 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
   2528 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   2529 	};
   2530 	u_int8_t tmpkey[IEEE80211_WEP_NKID][IEEE80211_KEYBUF_SIZE];
   2531 
   2532 	switch (cmd) {
   2533 	case SIOCSIFMEDIA:
   2534 	case SIOCGIFMEDIA:
   2535 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   2536 		break;
   2537 	case SIOCG80211:
   2538 		error = ieee80211_ioctl_get80211(ic, cmd,
   2539 				(struct ieee80211req *) data);
   2540 		break;
   2541 	case SIOCS80211:
   2542 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   2543 			break;
   2544 		error = ieee80211_ioctl_set80211(ic, cmd,
   2545 				(struct ieee80211req *) data);
   2546 		break;
   2547 	case SIOCS80211NWID:
   2548 		if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
   2549 			break;
   2550 		if (nwid.i_len > IEEE80211_NWID_LEN) {
   2551 			error = EINVAL;
   2552 			break;
   2553 		}
   2554 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   2555 		ic->ic_des_esslen = nwid.i_len;
   2556 		memcpy(ic->ic_des_essid, nwid.i_nwid, nwid.i_len);
   2557 		error = ENETRESET;
   2558 		break;
   2559 	case SIOCG80211NWID:
   2560 		memset(&nwid, 0, sizeof(nwid));
   2561 		switch (ic->ic_state) {
   2562 		case IEEE80211_S_INIT:
   2563 		case IEEE80211_S_SCAN:
   2564 			nwid.i_len = ic->ic_des_esslen;
   2565 			memcpy(nwid.i_nwid, ic->ic_des_essid, nwid.i_len);
   2566 			break;
   2567 		default:
   2568 			nwid.i_len = ic->ic_bss->ni_esslen;
   2569 			memcpy(nwid.i_nwid, ic->ic_bss->ni_essid, nwid.i_len);
   2570 			break;
   2571 		}
   2572 		error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
   2573 		break;
   2574 	case SIOCS80211NWKEY:
   2575 		nwkey = (struct ieee80211_nwkey *)data;
   2576 		/* transmit key index out of range? */
   2577 		kid = nwkey->i_defkid - 1;
   2578 		if (kid < 0 || kid >= IEEE80211_WEP_NKID) {
   2579 			error = EINVAL;
   2580 			break;
   2581 		}
   2582 		/* no such transmit key is set? */
   2583 		if (nwkey->i_key[kid].i_keylen == 0 ||
   2584 		    (nwkey->i_key[kid].i_keylen == -1 &&
   2585 		     ic->ic_nw_keys[kid].wk_keylen == 0)) {
   2586 			if (nwkey->i_wepon != IEEE80211_NWKEY_OPEN) {
   2587 				error = EINVAL;
   2588 				break;
   2589 			}
   2590 		}
   2591 		/* check key lengths */
   2592 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2593 			klen = nwkey->i_key[kid].i_keylen;
   2594 			if ((klen > 0 &&
   2595 			    klen < IEEE80211_WEP_KEYLEN) ||
   2596 			    klen > sizeof(ic->ic_nw_keys[kid].wk_key)) {
   2597 				error = EINVAL;
   2598 				break;
   2599 			}
   2600 		}
   2601 
   2602 		if (error)
   2603 			break;
   2604 
   2605 		/* copy in keys */
   2606 		(void)memset(tmpkey, 0, sizeof(tmpkey));
   2607 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2608 			klen = nwkey->i_key[kid].i_keylen;
   2609 			if (klen <= 0)
   2610 				continue;
   2611 			if ((error = copyin(nwkey->i_key[kid].i_keydat,
   2612 			    tmpkey[kid], klen)) != 0)
   2613 				break;
   2614 		}
   2615 
   2616 		if (error)
   2617 			break;
   2618 
   2619 		/* set keys */
   2620 		ieee80211_key_update_begin(ic);
   2621 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2622 			klen = nwkey->i_key[kid].i_keylen;
   2623 			if (klen <= 0)
   2624 				continue;
   2625 			k = &ic->ic_nw_keys[kid];
   2626 			k->wk_keyix = kid;
   2627 			if (!ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   2628 			    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2629 				error = EINVAL;
   2630 				continue;
   2631 			}
   2632 			k->wk_keylen = nwkey->i_key[kid].i_keylen;
   2633 			(void)memcpy(k->wk_key, tmpkey[kid],
   2634 			    sizeof(tmpkey[kid]));
   2635 			if (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   2636 				error = EINVAL;
   2637 		}
   2638 		ieee80211_key_update_end(ic);
   2639 
   2640 		if (error)
   2641 			break;
   2642 
   2643 		/* delete keys */
   2644 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2645 			klen = nwkey->i_key[kid].i_keylen;
   2646 			k = &ic->ic_nw_keys[kid];
   2647 			if (klen <= 0)
   2648 				(void)ieee80211_crypto_delkey(ic, k);
   2649 		}
   2650 
   2651 		/* set transmit key */
   2652 		kid = nwkey->i_defkid - 1;
   2653 		if (ic->ic_def_txkey != kid) {
   2654 			ic->ic_def_txkey = kid;
   2655 			error = ENETRESET;
   2656 		}
   2657 		oflags = ic->ic_flags;
   2658 		if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN) {
   2659 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2660 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2661 		} else {
   2662 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2663 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2664 		}
   2665 		if (oflags != ic->ic_flags)
   2666 			error = ENETRESET;
   2667 		break;
   2668 	case SIOCG80211NWKEY:
   2669 		nwkey = (struct ieee80211_nwkey *)data;
   2670 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2671 			nwkey->i_wepon = IEEE80211_NWKEY_WEP;
   2672 		else
   2673 			nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
   2674 		nwkey->i_defkid = ic->ic_def_txkey + 1;
   2675 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2676 			if (nwkey->i_key[i].i_keydat == NULL)
   2677 				continue;
   2678 			/* do not show any keys to non-root user */
   2679 			if ((error = suser(curproc->p_ucred,
   2680 			    &curproc->p_acflag)) != 0)
   2681 				break;
   2682 			nwkey->i_key[i].i_keylen = ic->ic_nw_keys[i].wk_keylen;
   2683 			if ((error = copyout(ic->ic_nw_keys[i].wk_key,
   2684 			    nwkey->i_key[i].i_keydat,
   2685 			    ic->ic_nw_keys[i].wk_keylen)) != 0)
   2686 				break;
   2687 		}
   2688 		break;
   2689 	case SIOCS80211POWER:
   2690 		power = (struct ieee80211_power *)data;
   2691 		ic->ic_lintval = power->i_maxsleep;
   2692 		if (power->i_enabled != 0) {
   2693 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2694 				error = EINVAL;
   2695 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2696 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2697 				error = ENETRESET;
   2698 			}
   2699 		} else {
   2700 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2701 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2702 				error = ENETRESET;
   2703 			}
   2704 		}
   2705 		break;
   2706 	case SIOCG80211POWER:
   2707 		power = (struct ieee80211_power *)data;
   2708 		power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
   2709 		power->i_maxsleep = ic->ic_lintval;
   2710 		break;
   2711 	case SIOCS80211BSSID:
   2712 		bssid = (struct ieee80211_bssid *)data;
   2713 		if (IEEE80211_ADDR_EQ(bssid->i_bssid, empty_macaddr))
   2714 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2715 		else {
   2716 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2717 			IEEE80211_ADDR_COPY(ic->ic_des_bssid, bssid->i_bssid);
   2718 		}
   2719 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2720 			break;
   2721 		switch (ic->ic_state) {
   2722 		case IEEE80211_S_INIT:
   2723 		case IEEE80211_S_SCAN:
   2724 			error = ENETRESET;
   2725 			break;
   2726 		default:
   2727 			if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
   2728 			    !IEEE80211_ADDR_EQ(ic->ic_des_bssid,
   2729 			    ic->ic_bss->ni_bssid))
   2730 				error = ENETRESET;
   2731 			break;
   2732 		}
   2733 		break;
   2734 	case SIOCG80211BSSID:
   2735 		bssid = (struct ieee80211_bssid *)data;
   2736 		switch (ic->ic_state) {
   2737 		case IEEE80211_S_INIT:
   2738 		case IEEE80211_S_SCAN:
   2739 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2740 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2741 				    ic->ic_myaddr);
   2742 			else if (ic->ic_flags & IEEE80211_F_DESBSSID)
   2743 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2744 				    ic->ic_des_bssid);
   2745 			else
   2746 				memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   2747 			break;
   2748 		default:
   2749 			IEEE80211_ADDR_COPY(bssid->i_bssid,
   2750 			    ic->ic_bss->ni_bssid);
   2751 			break;
   2752 		}
   2753 		break;
   2754 	case SIOCS80211CHANNEL:
   2755 		chanreq = (struct ieee80211chanreq *)data;
   2756 		if (chanreq->i_channel == IEEE80211_CHAN_ANY)
   2757 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2758 		else if (chanreq->i_channel > IEEE80211_CHAN_MAX ||
   2759 		    isclr(ic->ic_chan_active, chanreq->i_channel)) {
   2760 			error = EINVAL;
   2761 			break;
   2762 		} else
   2763 			ic->ic_ibss_chan = ic->ic_des_chan =
   2764 			    &ic->ic_channels[chanreq->i_channel];
   2765 		switch (ic->ic_state) {
   2766 		case IEEE80211_S_INIT:
   2767 		case IEEE80211_S_SCAN:
   2768 			error = ENETRESET;
   2769 			break;
   2770 		default:
   2771 			if (ic->ic_opmode == IEEE80211_M_STA) {
   2772 				if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2773 				    ic->ic_bss->ni_chan != ic->ic_des_chan)
   2774 					error = ENETRESET;
   2775 			} else {
   2776 				if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
   2777 					error = ENETRESET;
   2778 			}
   2779 			break;
   2780 		}
   2781 		break;
   2782 	case SIOCG80211CHANNEL:
   2783 		chanreq = (struct ieee80211chanreq *)data;
   2784 		switch (ic->ic_state) {
   2785 		case IEEE80211_S_INIT:
   2786 		case IEEE80211_S_SCAN:
   2787 			if (ic->ic_opmode == IEEE80211_M_STA)
   2788 				chan = ic->ic_des_chan;
   2789 			else
   2790 				chan = ic->ic_ibss_chan;
   2791 			break;
   2792 		default:
   2793 			chan = ic->ic_bss->ni_chan;
   2794 			break;
   2795 		}
   2796 		chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
   2797 		break;
   2798 	case SIOCGIFGENERIC:
   2799 		error = ieee80211_cfgget(ic, cmd, data);
   2800 		break;
   2801 	case SIOCSIFGENERIC:
   2802 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   2803 		if (error)
   2804 			break;
   2805 		error = ieee80211_cfgset(ic, cmd, data);
   2806 		break;
   2807 #ifdef COMPAT_20
   2808 	case OSIOCG80211STATS:
   2809 	case OSIOCG80211ZSTATS:
   2810 		ifr = (struct ifreq *)data;
   2811 		s = splnet();
   2812 		ieee80211_get_ostats(&ostats, &ic->ic_stats);
   2813 		error = copyout(&ostats, ifr->ifr_data, sizeof(ostats));
   2814 		if (error == 0 && cmd == OSIOCG80211ZSTATS)
   2815 			(void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
   2816 		splx(s);
   2817 		break;
   2818 #endif /* COMPAT_20 */
   2819 	case SIOCG80211ZSTATS:
   2820 	case SIOCG80211STATS:
   2821 		ifr = (struct ifreq *)data;
   2822 		s = splnet();
   2823 		error = copyout(&ic->ic_stats, ifr->ifr_buf,
   2824 		    MIN(sizeof(ic->ic_stats), ifr->ifr_buflen));
   2825 		if (error == 0 && cmd == SIOCG80211ZSTATS)
   2826 			(void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
   2827 		splx(s);
   2828 		break;
   2829 	case SIOCSIFMTU:
   2830 		ifr = (struct ifreq *)data;
   2831 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2832 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2833 			error = EINVAL;
   2834 		else
   2835 			ifp->if_mtu = ifr->ifr_mtu;
   2836 		break;
   2837 	default:
   2838 		error = ether_ioctl(ifp, cmd, data);
   2839 		break;
   2840 	}
   2841 	return error;
   2842 }
   2843 #endif /* __NetBSD__ */
   2844