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