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