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