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