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