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