Home | History | Annotate | Line # | Download | only in net80211
ieee80211_ioctl.c revision 1.4
      1 /*	$NetBSD: ieee80211_ioctl.c,v 1.4 2003/09/28 02:40:14 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002, 2003 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.4 2003/07/20 21:36:08 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_ioctl.c,v 1.4 2003/09/28 02:40:14 dyoung Exp $");
     39 #endif
     40 
     41 /*
     42  * IEEE 802.11 ioctl support (FreeBSD-specific)
     43  */
     44 
     45 #include <sys/endian.h>
     46 #include <sys/param.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/sockio.h>
     50 #include <sys/systm.h>
     51 #include <sys/proc.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_arp.h>
     55 #include <net/if_media.h>
     56 #ifdef __FreeBSD__
     57 #include <net/ethernet.h>
     58 #else
     59 #include <net/if_ether.h>
     60 #endif
     61 
     62 #include <net80211/ieee80211_var.h>
     63 #include <net80211/ieee80211_ioctl.h>
     64 
     65 #ifdef __FreeBSD__
     66 #include <dev/wi/if_wavelan_ieee.h>
     67 #else
     68 #include <dev/ic/wi_ieee.h>
     69 #endif
     70 
     71 /*
     72  * XXX
     73  * Wireless LAN specific configuration interface, which is compatible
     74  * with wicontrol(8).
     75  */
     76 
     77 int
     78 ieee80211_cfgget(struct ifnet *ifp, u_long cmd, caddr_t data)
     79 {
     80 	struct ieee80211com *ic = (void *)ifp;
     81 	int i, j, error;
     82 	struct ifreq *ifr = (struct ifreq *)data;
     83 	struct wi_req wreq;
     84 	struct wi_ltv_keys *keys;
     85 	struct wi_apinfo *ap;
     86 	struct ieee80211_node *ni;
     87 	struct ieee80211_rateset *rs;
     88 #ifdef WICACHE
     89 	struct wi_sigcache wsc;
     90 #endif /* WICACHE */
     91 #if 0 /* TBD */
     92 	struct wi_scan_p2_hdr *p2;
     93 	struct wi_scan_res *res;
     94 #endif
     95 
     96 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
     97 	if (error)
     98 		return error;
     99 	wreq.wi_len = 0;
    100 	switch (wreq.wi_type) {
    101 	case WI_RID_SERIALNO:
    102 		/* nothing appropriate */
    103 		break;
    104 	case WI_RID_NODENAME:
    105 		strcpy((char *)&wreq.wi_val[1], hostname);
    106 		wreq.wi_val[0] = htole16(strlen(hostname));
    107 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
    108 		break;
    109 	case WI_RID_CURRENT_SSID:
    110 		if (ic->ic_state != IEEE80211_S_RUN) {
    111 			wreq.wi_val[0] = 0;
    112 			wreq.wi_len = 1;
    113 			break;
    114 		}
    115 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
    116 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
    117 		    ic->ic_bss->ni_esslen);
    118 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
    119 		break;
    120 	case WI_RID_OWN_SSID:
    121 	case WI_RID_DESIRED_SSID:
    122 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
    123 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
    124 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
    125 		break;
    126 	case WI_RID_CURRENT_BSSID:
    127 		if (ic->ic_state == IEEE80211_S_RUN)
    128 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
    129 		else
    130 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
    131 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    132 		break;
    133 	case WI_RID_CHANNEL_LIST:
    134 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
    135 		/*
    136 		 * Since channel 0 is not available for DS, channel 1
    137 		 * is assigned to LSB on WaveLAN.
    138 		 */
    139 		if (ic->ic_phytype == IEEE80211_T_DS)
    140 			i = 1;
    141 		else
    142 			i = 0;
    143 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
    144 			if (isset(ic->ic_chan_active, i)) {
    145 				setbit((u_int8_t *)wreq.wi_val, j);
    146 				wreq.wi_len = j / 16 + 1;
    147 			}
    148 		break;
    149 	case WI_RID_OWN_CHNL:
    150 		wreq.wi_val[0] = htole16(
    151 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
    152 		wreq.wi_len = 1;
    153 		break;
    154 	case WI_RID_CURRENT_CHAN:
    155 		wreq.wi_val[0] = htole16(
    156 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
    157 		wreq.wi_len = 1;
    158 		break;
    159 	case WI_RID_COMMS_QUALITY:
    160 		wreq.wi_val[0] = 0;				/* quality */
    161 		wreq.wi_val[1] = htole16(ic->ic_bss->ni_rssi);	/* signal */
    162 		wreq.wi_val[2] = 0;				/* noise */
    163 		wreq.wi_len = 3;
    164 		break;
    165 	case WI_RID_PROMISC:
    166 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
    167 		wreq.wi_len = 1;
    168 		break;
    169 	case WI_RID_PORTTYPE:
    170 		wreq.wi_val[0] = htole16(ic->ic_opmode);
    171 		wreq.wi_len = 1;
    172 		break;
    173 	case WI_RID_MAC_NODE:
    174 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
    175 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    176 		break;
    177 	case WI_RID_TX_RATE:
    178 		if (ic->ic_fixed_rate == -1)
    179 			wreq.wi_val[0] = 0;	/* auto */
    180 		else
    181 			wreq.wi_val[0] = htole16(
    182 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
    183 			    IEEE80211_RATE_VAL) / 2);
    184 		wreq.wi_len = 1;
    185 		break;
    186 	case WI_RID_CUR_TX_RATE:
    187 		wreq.wi_val[0] = htole16(
    188 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
    189 		    IEEE80211_RATE_VAL) / 2);
    190 		wreq.wi_len = 1;
    191 		break;
    192 	case WI_RID_RTS_THRESH:
    193 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
    194 		wreq.wi_len = 1;
    195 		break;
    196 	case WI_RID_CREATE_IBSS:
    197 		wreq.wi_val[0] =
    198 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
    199 		wreq.wi_len = 1;
    200 		break;
    201 	case WI_RID_MICROWAVE_OVEN:
    202 		wreq.wi_val[0] = 0;	/* no ... not supported */
    203 		wreq.wi_len = 1;
    204 		break;
    205 	case WI_RID_ROAMING_MODE:
    206 		wreq.wi_val[0] = htole16(1);	/* enabled ... not supported */
    207 		wreq.wi_len = 1;
    208 		break;
    209 	case WI_RID_SYSTEM_SCALE:
    210 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
    211 		wreq.wi_len = 1;
    212 		break;
    213 	case WI_RID_PM_ENABLED:
    214 		wreq.wi_val[0] =
    215 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    216 		wreq.wi_len = 1;
    217 		break;
    218 	case WI_RID_MAX_SLEEP:
    219 		wreq.wi_val[0] = htole16(ic->ic_lintval);
    220 		wreq.wi_len = 1;
    221 		break;
    222 	case WI_RID_CUR_BEACON_INT:
    223 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
    224 		wreq.wi_len = 1;
    225 		break;
    226 	case WI_RID_WEP_AVAIL:
    227 		wreq.wi_val[0] =
    228 		    htole16((ic->ic_caps & IEEE80211_C_WEP) ? 1 : 0);
    229 		wreq.wi_len = 1;
    230 		break;
    231 	case WI_RID_CNFAUTHMODE:
    232 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
    233 		wreq.wi_len = 1;
    234 		break;
    235 	case WI_RID_ENCRYPTION:
    236 		wreq.wi_val[0] =
    237 		    htole16((ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0);
    238 		wreq.wi_len = 1;
    239 		break;
    240 	case WI_RID_TX_CRYPT_KEY:
    241 		wreq.wi_val[0] = htole16(ic->ic_wep_txkey);
    242 		wreq.wi_len = 1;
    243 		break;
    244 	case WI_RID_DEFLT_CRYPT_KEYS:
    245 		keys = (struct wi_ltv_keys *)&wreq;
    246 		/* do not show keys to non-root user */
    247 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    248 		if (error) {
    249 			memset(keys, 0, sizeof(*keys));
    250 			error = 0;
    251 			break;
    252 		}
    253 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    254 			keys->wi_keys[i].wi_keylen =
    255 			    htole16(ic->ic_nw_keys[i].wk_len);
    256 			memcpy(keys->wi_keys[i].wi_keydat,
    257 			    ic->ic_nw_keys[i].wk_key, ic->ic_nw_keys[i].wk_len);
    258 		}
    259 		wreq.wi_len = sizeof(*keys) / 2;
    260 		break;
    261 	case WI_RID_MAX_DATALEN:
    262 		wreq.wi_val[0] = htole16(IEEE80211_MAX_LEN);	/* TODO: frag */
    263 		wreq.wi_len = 1;
    264 		break;
    265 	case WI_RID_IFACE_STATS:
    266 		/* XXX: should be implemented in lower drivers */
    267 		break;
    268 	case WI_RID_READ_APS:
    269 		if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    270 			/*
    271 			 * Don't return results until active scan completes.
    272 			 */
    273 			if (ic->ic_state == IEEE80211_S_SCAN &&
    274 			    (ic->ic_flags & IEEE80211_F_ASCAN)) {
    275 				error = EINPROGRESS;
    276 				break;
    277 			}
    278 		}
    279 		i = 0;
    280 		ap = (void *)((char *)wreq.wi_val + sizeof(i));
    281 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    282 			if ((caddr_t)(ap + 1) > (caddr_t)(&wreq + 1))
    283 				break;
    284 			memset(ap, 0, sizeof(*ap));
    285 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    286 				IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
    287 				ap->namelen = ic->ic_des_esslen;
    288 				if (ic->ic_des_esslen)
    289 					memcpy(ap->name, ic->ic_des_essid,
    290 					    ic->ic_des_esslen);
    291 			} else {
    292 				IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
    293 				ap->namelen = ni->ni_esslen;
    294 				if (ni->ni_esslen)
    295 					memcpy(ap->name, ni->ni_essid,
    296 					    ni->ni_esslen);
    297 			}
    298 			ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
    299 			ap->signal = ni->ni_rssi;
    300 			ap->capinfo = ni->ni_capinfo;
    301 			ap->interval = ni->ni_intval;
    302 			rs = &ni->ni_rates;
    303 			for (j = 0; j < rs->rs_nrates; j++) {
    304 				if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
    305 					ap->rate = (rs->rs_rates[j] &
    306 					    IEEE80211_RATE_VAL) * 5; /* XXX */
    307 				}
    308 			}
    309 			i++;
    310 			ap++;
    311 		}
    312 		memcpy(wreq.wi_val, &i, sizeof(i));
    313 		wreq.wi_len = (sizeof(int) + sizeof(*ap) * i) / 2;
    314 		break;
    315 #if 0
    316 	case WI_RID_PRISM2:
    317 		wreq.wi_val[0] = 1;	/* XXX lie so SCAN_RES can give rates */
    318 		wreq.wi_len = sizeof(u_int16_t) / 2;
    319 		break;
    320 	case WI_RID_SCAN_RES:			/* compatibility interface */
    321 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    322 		    ic->ic_state == IEEE80211_S_SCAN) {
    323 			error = EINPROGRESS;
    324 			break;
    325 		}
    326 		/* NB: we use the Prism2 format so we can return rate info */
    327 		p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
    328 		res = (void *)&p2[1];
    329 		i = 0;
    330 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    331 			if ((caddr_t)(res + 1) > (caddr_t)(&wreq + 1))
    332 				break;
    333 			res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
    334 			res->wi_noise = 0;
    335 			res->wi_signal = ni->ni_rssi;
    336 			IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
    337 			res->wi_interval = ni->ni_intval;
    338 			res->wi_capinfo = ni->ni_capinfo;
    339 			res->wi_ssid_len = ni->ni_esslen;
    340 			memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
    341 			/* NB: assumes wi_srates holds <= ni->ni_rates */
    342 			memcpy(res->wi_srates, ni->ni_rates.rs_rates,
    343 				sizeof(res->wi_srates));
    344 			if (ni->ni_rates.rs_nrates < 10)
    345 				res->wi_srates[ni->ni_rates.rs_nrates] = 0;
    346 			res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
    347 			res->wi_rsvd = 0;
    348 			res++, i++;
    349 		}
    350 		p2->wi_rsvd = 0;
    351 		p2->wi_reason = i;
    352 		wreq.wi_len = (sizeof(*p2) + sizeof(*res) * i) / 2;
    353 		break;
    354 #endif /* 0 */
    355 #ifdef WICACHE
    356 	case WI_RID_READ_CACHE:
    357 		i = 0;
    358 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    359 			if (i == (WI_MAX_DATALEN/sizeof(struct wi_sigcache))-1)
    360 				break;
    361 			IEEE80211_ADDR_COPY(wsc.macsrc, ni->ni_macaddr);
    362 			memset(&wsc.ipsrc, 0, sizeof(wsc.ipsrc));
    363 			wsc.signal = ni->ni_rssi;
    364 			wsc.noise = 0;
    365 			wsc.quality = 0;
    366 			memcpy((caddr_t)wreq.wi_val + sizeof(wsc) * i,
    367 			    &wsc, sizeof(wsc));
    368 			i++;
    369 		}
    370 		wreq.wi_len = sizeof(wsc) * i / 2;
    371 		break;
    372 #endif /* WICACHE */
    373 	case WI_RID_SCAN_APS:
    374 		error = EINVAL;
    375 		break;
    376 	default:
    377 		error = EINVAL;
    378 		break;
    379 	}
    380 	if (error == 0) {
    381 		wreq.wi_len++;
    382 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
    383 	}
    384 	return error;
    385 }
    386 
    387 static int
    388 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    389 {
    390 #define	IEEERATE(_ic,_m,_i) \
    391 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    392 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    393 	for (i = 0; i < nrates; i++)
    394 		if (IEEERATE(ic, mode, i) == rate)
    395 			return i;
    396 	return -1;
    397 #undef IEEERATE
    398 }
    399 
    400 int
    401 ieee80211_cfgset(struct ifnet *ifp, u_long cmd, caddr_t data)
    402 {
    403 	struct ieee80211com *ic = (void *)ifp;
    404 	int i, j, len, error, rate;
    405 	struct ifreq *ifr = (struct ifreq *)data;
    406 	struct wi_ltv_keys *keys;
    407 	struct wi_req wreq;
    408 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
    409 
    410 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
    411 	if (error)
    412 		return error;
    413 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
    414 	switch (wreq.wi_type) {
    415 	case WI_RID_SERIALNO:
    416 	case WI_RID_NODENAME:
    417 		return EPERM;
    418 	case WI_RID_CURRENT_SSID:
    419 		return EPERM;
    420 	case WI_RID_OWN_SSID:
    421 	case WI_RID_DESIRED_SSID:
    422 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
    423 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
    424 			error = ENOSPC;
    425 			break;
    426 		}
    427 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
    428 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
    429 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
    430 		error = ENETRESET;
    431 		break;
    432 	case WI_RID_CURRENT_BSSID:
    433 		return EPERM;
    434 	case WI_RID_OWN_CHNL:
    435 		if (len != 2)
    436 			return EINVAL;
    437 		i = le16toh(wreq.wi_val[0]);
    438 		if (i < 0 ||
    439 		    i > IEEE80211_CHAN_MAX ||
    440 		    isclr(ic->ic_chan_active, i))
    441 			return EINVAL;
    442 		ic->ic_ibss_chan = &ic->ic_channels[i];
    443 		if (ic->ic_flags & IEEE80211_F_SIBSS)
    444 			error = ENETRESET;
    445 		break;
    446 	case WI_RID_CURRENT_CHAN:
    447 		return EPERM;
    448 	case WI_RID_COMMS_QUALITY:
    449 		return EPERM;
    450 	case WI_RID_PROMISC:
    451 		if (len != 2)
    452 			return EINVAL;
    453 		if (ifp->if_flags & IFF_PROMISC) {
    454 			if (wreq.wi_val[0] == 0) {
    455 				ifp->if_flags &= ~IFF_PROMISC;
    456 				error = ENETRESET;
    457 			}
    458 		} else {
    459 			if (wreq.wi_val[0] != 0) {
    460 				ifp->if_flags |= IFF_PROMISC;
    461 				error = ENETRESET;
    462 			}
    463 		}
    464 		break;
    465 	case WI_RID_PORTTYPE:
    466 		if (len != 2)
    467 			return EINVAL;
    468 		switch (le16toh(wreq.wi_val[0])) {
    469 		case IEEE80211_M_STA:
    470 			break;
    471 		case IEEE80211_M_IBSS:
    472 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
    473 				return EINVAL;
    474 			break;
    475 		case IEEE80211_M_AHDEMO:
    476 			if (ic->ic_phytype != IEEE80211_T_DS ||
    477 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
    478 				return EINVAL;
    479 			break;
    480 		case IEEE80211_M_HOSTAP:
    481 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
    482 				return EINVAL;
    483 			break;
    484 		default:
    485 			return EINVAL;
    486 		}
    487 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
    488 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
    489 			error = ENETRESET;
    490 		}
    491 		break;
    492 #if 0
    493 	case WI_RID_MAC_NODE:
    494 		if (len != IEEE80211_ADDR_LEN)
    495 			return EINVAL;
    496 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
    497 		/* if_init will copy lladdr into ic_myaddr */
    498 		error = ENETRESET;
    499 		break;
    500 #endif
    501 	case WI_RID_TX_RATE:
    502 		if (len != 2)
    503 			return EINVAL;
    504 		if (wreq.wi_val[0] == 0) {
    505 			/* auto */
    506 			ic->ic_fixed_rate = -1;
    507 			break;
    508 		}
    509 		rate = 2 * le16toh(wreq.wi_val[0]);
    510 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
    511 			/*
    512 			 * In autoselect mode search for the rate.  We take
    513 			 * the first instance which may not be right, but we
    514 			 * are limited by the interface.  Note that we also
    515 			 * lock the mode to insure the rate is meaningful
    516 			 * when it is used.
    517 			 */
    518 			for (j = IEEE80211_MODE_11A;
    519 			     j < IEEE80211_MODE_MAX; j++) {
    520 				if ((ic->ic_modecaps & (1<<j)) == 0)
    521 					continue;
    522 				i = findrate(ic, j, rate);
    523 				if (i != -1) {
    524 					/* lock mode too */
    525 					ic->ic_curmode = j;
    526 					goto setrate;
    527 				}
    528 			}
    529 		} else {
    530 			i = findrate(ic, ic->ic_curmode, rate);
    531 			if (i != -1)
    532 				goto setrate;
    533 		}
    534 		return EINVAL;
    535 	setrate:
    536 		ic->ic_fixed_rate = i;
    537 		error = ENETRESET;
    538 		break;
    539 	case WI_RID_CUR_TX_RATE:
    540 		return EPERM;
    541 	case WI_RID_RTS_THRESH:
    542 		if (len != 2)
    543 			return EINVAL;
    544 		if (le16toh(wreq.wi_val[0]) != IEEE80211_MAX_LEN)
    545 			return EINVAL;		/* TODO: RTS */
    546 		break;
    547 	case WI_RID_CREATE_IBSS:
    548 		if (len != 2)
    549 			return EINVAL;
    550 		if (wreq.wi_val[0] != 0) {
    551 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
    552 				return EINVAL;
    553 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
    554 				ic->ic_flags |= IEEE80211_F_IBSSON;
    555 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
    556 				    ic->ic_state == IEEE80211_S_SCAN)
    557 					error = ENETRESET;
    558 			}
    559 		} else {
    560 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
    561 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
    562 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
    563 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
    564 					error = ENETRESET;
    565 				}
    566 			}
    567 		}
    568 		break;
    569 	case WI_RID_MICROWAVE_OVEN:
    570 		if (len != 2)
    571 			return EINVAL;
    572 		if (wreq.wi_val[0] != 0)
    573 			return EINVAL;		/* not supported */
    574 		break;
    575 	case WI_RID_ROAMING_MODE:
    576 		if (len != 2)
    577 			return EINVAL;
    578 		if (le16toh(wreq.wi_val[0]) != 1)
    579 			return EINVAL;		/* not supported */
    580 		break;
    581 	case WI_RID_SYSTEM_SCALE:
    582 		if (len != 2)
    583 			return EINVAL;
    584 		if (le16toh(wreq.wi_val[0]) != 1)
    585 			return EINVAL;		/* not supported */
    586 		break;
    587 	case WI_RID_PM_ENABLED:
    588 		if (len != 2)
    589 			return EINVAL;
    590 		if (wreq.wi_val[0] != 0) {
    591 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
    592 				return EINVAL;
    593 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
    594 				ic->ic_flags |= IEEE80211_F_PMGTON;
    595 				error = ENETRESET;
    596 			}
    597 		} else {
    598 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
    599 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
    600 				error = ENETRESET;
    601 			}
    602 		}
    603 		break;
    604 	case WI_RID_MAX_SLEEP:
    605 		if (len != 2)
    606 			return EINVAL;
    607 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
    608 		if (ic->ic_flags & IEEE80211_F_PMGTON)
    609 			error = ENETRESET;
    610 		break;
    611 	case WI_RID_CUR_BEACON_INT:
    612 		return EPERM;
    613 	case WI_RID_WEP_AVAIL:
    614 		return EPERM;
    615 	case WI_RID_CNFAUTHMODE:
    616 		if (len != 2)
    617 			return EINVAL;
    618 		if (le16toh(wreq.wi_val[0]) != 1)
    619 			return EINVAL;		/* TODO: shared key auth */
    620 		break;
    621 	case WI_RID_ENCRYPTION:
    622 		if (len != 2)
    623 			return EINVAL;
    624 		if (wreq.wi_val[0] != 0) {
    625 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
    626 				return EINVAL;
    627 			if ((ic->ic_flags & IEEE80211_F_WEPON) == 0) {
    628 				ic->ic_flags |= IEEE80211_F_WEPON;
    629 				error = ENETRESET;
    630 			}
    631 		} else {
    632 			if (ic->ic_flags & IEEE80211_F_WEPON) {
    633 				ic->ic_flags &= ~IEEE80211_F_WEPON;
    634 				error = ENETRESET;
    635 			}
    636 		}
    637 		break;
    638 	case WI_RID_TX_CRYPT_KEY:
    639 		if (len != 2)
    640 			return EINVAL;
    641 		i = le16toh(wreq.wi_val[0]);
    642 		if (i >= IEEE80211_WEP_NKID)
    643 			return EINVAL;
    644 		ic->ic_wep_txkey = i;
    645 		break;
    646 	case WI_RID_DEFLT_CRYPT_KEYS:
    647 		if (len != sizeof(struct wi_ltv_keys))
    648 			return EINVAL;
    649 		keys = (struct wi_ltv_keys *)&wreq;
    650 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    651 			len = le16toh(keys->wi_keys[i].wi_keylen);
    652 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
    653 				return EINVAL;
    654 			if (len > sizeof(ic->ic_nw_keys[i].wk_key))
    655 				return EINVAL;
    656 		}
    657 		memset(ic->ic_nw_keys, 0, sizeof(ic->ic_nw_keys));
    658 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    659 			len = le16toh(keys->wi_keys[i].wi_keylen);
    660 			ic->ic_nw_keys[i].wk_len = len;
    661 			memcpy(ic->ic_nw_keys[i].wk_key,
    662 			    keys->wi_keys[i].wi_keydat, len);
    663 		}
    664 		error = ENETRESET;
    665 		break;
    666 	case WI_RID_MAX_DATALEN:
    667 		if (len != 2)
    668 			return EINVAL;
    669 		len = le16toh(wreq.wi_val[0]);
    670 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
    671 			return EINVAL;
    672 		if (len != IEEE80211_MAX_LEN)
    673 			return EINVAL;		/* TODO: fragment */
    674 		ic->ic_fragthreshold = len;
    675 		error = ENETRESET;
    676 		break;
    677 	case WI_RID_IFACE_STATS:
    678 		error = EPERM;
    679 		break;
    680 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
    681 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    682 			break;
    683 		/* NB: ignore channel list and tx rate parameters */
    684 		error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    685 		break;
    686 	case WI_RID_SCAN_APS:
    687 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    688 			break;
    689 		len--;			/* XXX: tx rate? */
    690 		/* FALLTHRU */
    691 	case WI_RID_CHANNEL_LIST:
    692 		memset(chanlist, 0, sizeof(chanlist));
    693 		/*
    694 		 * Since channel 0 is not available for DS, channel 1
    695 		 * is assigned to LSB on WaveLAN.
    696 		 */
    697 		if (ic->ic_phytype == IEEE80211_T_DS)
    698 			i = 1;
    699 		else
    700 			i = 0;
    701 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
    702 			if ((j / 8) >= len)
    703 				break;
    704 			if (isclr((u_int8_t *)wreq.wi_val, j))
    705 				continue;
    706 			if (isclr(ic->ic_chan_active, i)) {
    707 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
    708 					continue;
    709 				if (isclr(ic->ic_chan_avail, i))
    710 					return EPERM;
    711 			}
    712 			setbit(chanlist, i);
    713 		}
    714 		memcpy(ic->ic_chan_active, chanlist,
    715 		    sizeof(ic->ic_chan_active));
    716 		if (isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    717 			for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    718 				if (isset(chanlist, i)) {
    719 					ic->ic_ibss_chan = &ic->ic_channels[i];
    720 					break;
    721 				}
    722 		}
    723 		if (isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
    724 			ic->ic_bss->ni_chan = ic->ic_ibss_chan;
    725 		if (wreq.wi_type == WI_RID_CHANNEL_LIST)
    726 			error = ENETRESET;
    727 		else
    728 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    729 		break;
    730 	default:
    731 		error = EINVAL;
    732 		break;
    733 	}
    734 	return error;
    735 }
    736 
    737 #ifdef __FreeBSD__
    738 int
    739 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    740 {
    741 	struct ieee80211com *ic = (void *)ifp;
    742 	int error = 0;
    743 	u_int kid, len;
    744 	struct ieee80211req *ireq;
    745 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
    746 	char tmpssid[IEEE80211_NWID_LEN];
    747 	struct ieee80211_channel *chan;
    748 
    749 	switch (cmd) {
    750 	case SIOCSIFMEDIA:
    751 	case SIOCGIFMEDIA:
    752 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
    753 				&ic->ic_media, cmd);
    754 		break;
    755 	case SIOCG80211:
    756 		ireq = (struct ieee80211req *) data;
    757 		switch (ireq->i_type) {
    758 		case IEEE80211_IOC_SSID:
    759 			switch (ic->ic_state) {
    760 			case IEEE80211_S_INIT:
    761 			case IEEE80211_S_SCAN:
    762 				ireq->i_len = ic->ic_des_esslen;
    763 				memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
    764 				break;
    765 			default:
    766 				ireq->i_len = ic->ic_bss->ni_esslen;
    767 				memcpy(tmpssid, ic->ic_bss->ni_essid,
    768 					ireq->i_len);
    769 				break;
    770 			}
    771 			error = copyout(tmpssid, ireq->i_data, ireq->i_len);
    772 			break;
    773 		case IEEE80211_IOC_NUMSSIDS:
    774 			ireq->i_val = 1;
    775 			break;
    776 		case IEEE80211_IOC_WEP:
    777 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0) {
    778 				ireq->i_val = IEEE80211_WEP_NOSUP;
    779 			} else {
    780 				if (ic->ic_flags & IEEE80211_F_WEPON) {
    781 					ireq->i_val =
    782 					    IEEE80211_WEP_MIXED;
    783 				} else {
    784 					ireq->i_val =
    785 					    IEEE80211_WEP_OFF;
    786 				}
    787 			}
    788 			break;
    789 		case IEEE80211_IOC_WEPKEY:
    790 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0) {
    791 				error = EINVAL;
    792 				break;
    793 			}
    794 			kid = (u_int) ireq->i_val;
    795 			if (kid >= IEEE80211_WEP_NKID) {
    796 				error = EINVAL;
    797 				break;
    798 			}
    799 			len = (u_int) ic->ic_nw_keys[kid].wk_len;
    800 			/* NB: only root can read WEP keys */
    801 			if (suser(curproc->p_ucred, &curproc->p_acflag)) {
    802 				bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
    803 			} else {
    804 				bzero(tmpkey, len);
    805 			}
    806 			ireq->i_len = len;
    807 			error = copyout(tmpkey, ireq->i_data, len);
    808 			break;
    809 		case IEEE80211_IOC_NUMWEPKEYS:
    810 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
    811 				error = EINVAL;
    812 			else
    813 				ireq->i_val = IEEE80211_WEP_NKID;
    814 			break;
    815 		case IEEE80211_IOC_WEPTXKEY:
    816 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
    817 				error = EINVAL;
    818 			else
    819 				ireq->i_val = ic->ic_wep_txkey;
    820 			break;
    821 		case IEEE80211_IOC_AUTHMODE:
    822 			ireq->i_val = IEEE80211_AUTH_OPEN;
    823 			break;
    824 		case IEEE80211_IOC_CHANNEL:
    825 			switch (ic->ic_state) {
    826 			case IEEE80211_S_INIT:
    827 			case IEEE80211_S_SCAN:
    828 				if (ic->ic_opmode == IEEE80211_M_STA)
    829 					chan = ic->ic_des_chan;
    830 				else
    831 					chan = ic->ic_ibss_chan;
    832 				break;
    833 			default:
    834 				chan = ic->ic_bss->ni_chan;
    835 				break;
    836 			}
    837 			ireq->i_val = ieee80211_chan2ieee(ic, chan);
    838 			break;
    839 		case IEEE80211_IOC_POWERSAVE:
    840 			if (ic->ic_flags & IEEE80211_F_PMGTON)
    841 				ireq->i_val = IEEE80211_POWERSAVE_ON;
    842 			else
    843 				ireq->i_val = IEEE80211_POWERSAVE_OFF;
    844 			break;
    845 		case IEEE80211_IOC_POWERSAVESLEEP:
    846 			ireq->i_val = ic->ic_lintval;
    847 			break;
    848 		case IEEE80211_IOCT_RTSTHRESHOLD:
    849 			ireq->i_val = ic->ic_rtsthreshold;
    850 			break;
    851 		default:
    852 			error = EINVAL;
    853 		}
    854 		break;
    855 	case SIOCS80211:
    856 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    857 		if (error)
    858 			break;
    859 		ireq = (struct ieee80211req *) data;
    860 		switch (ireq->i_type) {
    861 		case IEEE80211_IOC_SSID:
    862 			if (ireq->i_val != 0 ||
    863 			    ireq->i_len > IEEE80211_NWID_LEN) {
    864 				error = EINVAL;
    865 				break;
    866 			}
    867 			error = copyin(ireq->i_data, tmpssid, ireq->i_len);
    868 			if (error)
    869 				break;
    870 			memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
    871 			ic->ic_des_esslen = ireq->i_len;
    872 			memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
    873 			error = ENETRESET;
    874 			break;
    875 		case IEEE80211_IOC_WEP:
    876 			/*
    877 			 * These cards only support one mode so
    878 			 * we just turn wep on if what ever is
    879 			 * passed in is not OFF.
    880 			 */
    881 			if (ireq->i_val == IEEE80211_WEP_OFF) {
    882 				ic->ic_flags &= ~IEEE80211_F_WEPON;
    883 			} else {
    884 				ic->ic_flags |= IEEE80211_F_WEPON;
    885 			}
    886 			error = ENETRESET;
    887 			break;
    888 		case IEEE80211_IOC_WEPKEY:
    889 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0) {
    890 				error = EINVAL;
    891 				break;
    892 			}
    893 			kid = (u_int) ireq->i_val;
    894 			if (kid >= IEEE80211_WEP_NKID) {
    895 				error = EINVAL;
    896 				break;
    897 			}
    898 			if (ireq->i_len > sizeof(tmpkey)) {
    899 				error = EINVAL;
    900 				break;
    901 			}
    902 			memset(tmpkey, 0, sizeof(tmpkey));
    903 			error = copyin(ireq->i_data, tmpkey, ireq->i_len);
    904 			if (error)
    905 				break;
    906 			memcpy(ic->ic_nw_keys[kid].wk_key, tmpkey,
    907 				sizeof(tmpkey));
    908 			ic->ic_nw_keys[kid].wk_len = ireq->i_len;
    909 			error = ENETRESET;
    910 			break;
    911 		case IEEE80211_IOC_WEPTXKEY:
    912 			kid = (u_int) ireq->i_val;
    913 			if (kid >= IEEE80211_WEP_NKID) {
    914 				error = EINVAL;
    915 				break;
    916 			}
    917 			ic->ic_wep_txkey = kid;
    918 			error = ENETRESET;
    919 			break;
    920 #if 0
    921 		case IEEE80211_IOC_AUTHMODE:
    922 			sc->wi_authmode = ireq->i_val;
    923 			break;
    924 #endif
    925 		case IEEE80211_IOC_CHANNEL:
    926 			/* XXX 0xffff overflows 16-bit signed */
    927 			if (ireq->i_val == 0 ||
    928 			    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
    929 				ic->ic_des_chan = IEEE80211_CHAN_ANYC;
    930 			else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
    931 			    isclr(ic->ic_chan_active, ireq->i_val)) {
    932 				error = EINVAL;
    933 				break;
    934 			} else
    935 				ic->ic_ibss_chan = ic->ic_des_chan =
    936 					&ic->ic_channels[ireq->i_val];
    937 			switch (ic->ic_state) {
    938 			case IEEE80211_S_INIT:
    939 			case IEEE80211_S_SCAN:
    940 				error = ENETRESET;
    941 				break;
    942 			default:
    943 				if (ic->ic_opmode == IEEE80211_M_STA) {
    944 					if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    945 					    ic->ic_bss->ni_chan != ic->ic_des_chan)
    946 						error = ENETRESET;
    947 				} else {
    948 					if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
    949 						error = ENETRESET;
    950 				}
    951 				break;
    952 			}
    953 			break;
    954 		case IEEE80211_IOC_POWERSAVE:
    955 			switch (ireq->i_val) {
    956 			case IEEE80211_POWERSAVE_OFF:
    957 				if (ic->ic_flags & IEEE80211_F_PMGTON) {
    958 					ic->ic_flags &= ~IEEE80211_F_PMGTON;
    959 					error = ENETRESET;
    960 				}
    961 				break;
    962 			case IEEE80211_POWERSAVE_ON:
    963 				if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
    964 					error = EINVAL;
    965 				else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
    966 					ic->ic_flags |= IEEE80211_F_PMGTON;
    967 					error = ENETRESET;
    968 				}
    969 				break;
    970 			default:
    971 				error = EINVAL;
    972 				break;
    973 			}
    974 			break;
    975 		case IEEE80211_IOC_POWERSAVESLEEP:
    976 			if (ireq->i_val < 0) {
    977 				error = EINVAL;
    978 				break;
    979 			}
    980 			ic->ic_lintval = ireq->i_val;
    981 			error = ENETRESET;
    982 			break;
    983 		case IEEE80211_IOCT_RTSTHRESHOLD:
    984 			if (!(IEEE80211_RTS_MIN < ireq->i_val &&
    985 			      ireq->i_val <= IEEE80211_RTS_MAX + 1)) {
    986 				error = EINVAL;
    987 				break;
    988 			}
    989 			ic->ic_rtsthreshold = ireq->i_val;
    990 			error = ENETRESET;
    991 			break;
    992 		default:
    993 			error = EINVAL;
    994 			break;
    995 		}
    996 		break;
    997 	case SIOCGIFGENERIC:
    998 		error = ieee80211_cfgget(ifp, cmd, data);
    999 		break;
   1000 	case SIOCSIFGENERIC:
   1001 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   1002 		if (error)
   1003 			break;
   1004 		error = ieee80211_cfgset(ifp, cmd, data);
   1005 		break;
   1006 	default:
   1007 		error = ether_ioctl(ifp, cmd, data);
   1008 		break;
   1009 	}
   1010 	return error;
   1011 }
   1012 #endif /* __FreeBSD__ */
   1013 
   1014 #ifdef __NetBSD__
   1015 int
   1016 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1017 {
   1018 	struct ieee80211com *ic = (void *)ifp;
   1019 	struct ifreq *ifr = (struct ifreq *)data;
   1020 	int i, error = 0;
   1021 	struct ieee80211_nwid nwid;
   1022 	struct ieee80211_nwkey *nwkey;
   1023 	struct ieee80211_power *power;
   1024 	struct ieee80211_bssid *bssid;
   1025 	struct ieee80211chanreq *chanreq;
   1026 	struct ieee80211_channel *chan;
   1027 	struct ieee80211_wepkey keys[IEEE80211_WEP_NKID];
   1028 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
   1029 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   1030 	};
   1031 
   1032 	switch (cmd) {
   1033 	case SIOCSIFMEDIA:
   1034 	case SIOCGIFMEDIA:
   1035 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   1036 		break;
   1037 	case SIOCS80211NWID:
   1038 		if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
   1039 			break;
   1040 		if (nwid.i_len > IEEE80211_NWID_LEN) {
   1041 			error = EINVAL;
   1042 			break;
   1043 		}
   1044 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   1045 		ic->ic_des_esslen = nwid.i_len;
   1046 		memcpy(ic->ic_des_essid, nwid.i_nwid, nwid.i_len);
   1047 		error = ENETRESET;
   1048 		break;
   1049 	case SIOCG80211NWID:
   1050 		memset(&nwid, 0, sizeof(nwid));
   1051 		switch (ic->ic_state) {
   1052 		case IEEE80211_S_INIT:
   1053 		case IEEE80211_S_SCAN:
   1054 			nwid.i_len = ic->ic_des_esslen;
   1055 			memcpy(nwid.i_nwid, ic->ic_des_essid, nwid.i_len);
   1056 			break;
   1057 		default:
   1058 			nwid.i_len = ic->ic_bss->ni_esslen;
   1059 			memcpy(nwid.i_nwid, ic->ic_bss->ni_essid, nwid.i_len);
   1060 			break;
   1061 		}
   1062 		error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
   1063 		break;
   1064 	case SIOCS80211NWKEY:
   1065 		nwkey = (struct ieee80211_nwkey *)data;
   1066 		if ((ic->ic_flags & IEEE80211_C_WEP) == 0 &&
   1067 		    nwkey->i_wepon != IEEE80211_NWKEY_OPEN) {
   1068 			error = EINVAL;
   1069 			break;
   1070 		}
   1071 		/* check and copy keys */
   1072 		memset(keys, 0, sizeof(keys));
   1073 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1074 			keys[i].wk_len = nwkey->i_key[i].i_keylen;
   1075 			if ((keys[i].wk_len > 0 &&
   1076 			    keys[i].wk_len < IEEE80211_WEP_KEYLEN) ||
   1077 			    keys[i].wk_len > sizeof(keys[i].wk_key)) {
   1078 				error = EINVAL;
   1079 				break;
   1080 			}
   1081 			if (keys[i].wk_len <= 0)
   1082 				continue;
   1083 			if ((error = copyin(nwkey->i_key[i].i_keydat,
   1084 			    keys[i].wk_key, keys[i].wk_len)) != 0)
   1085 				break;
   1086 		}
   1087 		if (error)
   1088 			break;
   1089 		i = nwkey->i_defkid - 1;
   1090 		if (i < 0 || i >= IEEE80211_WEP_NKID ||
   1091 		    keys[i].wk_len == 0 ||
   1092 		    (keys[i].wk_len == -1 && ic->ic_nw_keys[i].wk_len == 0)) {
   1093 			if (nwkey->i_wepon != IEEE80211_NWKEY_OPEN) {
   1094 				error = EINVAL;
   1095 				break;
   1096 			}
   1097 		} else
   1098 			ic->ic_wep_txkey = i;
   1099 		/* save the key */
   1100 		if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN)
   1101 			ic->ic_flags &= ~IEEE80211_F_WEPON;
   1102 		else
   1103 			ic->ic_flags |= IEEE80211_F_WEPON;
   1104 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1105 			if (keys[i].wk_len < 0)
   1106 				continue;
   1107 			ic->ic_nw_keys[i].wk_len = keys[i].wk_len;
   1108 			memcpy(ic->ic_nw_keys[i].wk_key, keys[i].wk_key,
   1109 			    sizeof(keys[i].wk_key));
   1110 		}
   1111 		error = ENETRESET;
   1112 		break;
   1113 	case SIOCG80211NWKEY:
   1114 		nwkey = (struct ieee80211_nwkey *)data;
   1115 		if (ic->ic_flags & IEEE80211_F_WEPON)
   1116 			nwkey->i_wepon = IEEE80211_NWKEY_WEP;
   1117 		else
   1118 			nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
   1119 		nwkey->i_defkid = ic->ic_wep_txkey + 1;
   1120 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1121 			if (nwkey->i_key[i].i_keydat == NULL)
   1122 				continue;
   1123 			/* do not show any keys to non-root user */
   1124 			if ((error = suser(curproc->p_ucred,
   1125 			    &curproc->p_acflag)) != 0)
   1126 				break;
   1127 			nwkey->i_key[i].i_keylen = ic->ic_nw_keys[i].wk_len;
   1128 			if ((error = copyout(ic->ic_nw_keys[i].wk_key,
   1129 			    nwkey->i_key[i].i_keydat,
   1130 			    ic->ic_nw_keys[i].wk_len)) != 0)
   1131 				break;
   1132 		}
   1133 		break;
   1134 	case SIOCS80211POWER:
   1135 		power = (struct ieee80211_power *)data;
   1136 		ic->ic_lintval = power->i_maxsleep;
   1137 		if (power->i_enabled != 0) {
   1138 			if ((ic->ic_flags & IEEE80211_C_PMGT) == 0)
   1139 				error = EINVAL;
   1140 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   1141 				ic->ic_flags |= IEEE80211_F_PMGTON;
   1142 				error = ENETRESET;
   1143 			}
   1144 		} else {
   1145 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   1146 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   1147 				error = ENETRESET;
   1148 			}
   1149 		}
   1150 		break;
   1151 	case SIOCG80211POWER:
   1152 		power = (struct ieee80211_power *)data;
   1153 		power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
   1154 		power->i_maxsleep = ic->ic_lintval;
   1155 		break;
   1156 	case SIOCS80211BSSID:
   1157 		bssid = (struct ieee80211_bssid *)data;
   1158 		if (IEEE80211_ADDR_EQ(bssid->i_bssid, empty_macaddr))
   1159 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   1160 		else {
   1161 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   1162 			IEEE80211_ADDR_COPY(ic->ic_des_bssid, bssid->i_bssid);
   1163 		}
   1164 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1165 			break;
   1166 		switch (ic->ic_state) {
   1167 		case IEEE80211_S_INIT:
   1168 		case IEEE80211_S_SCAN:
   1169 			error = ENETRESET;
   1170 			break;
   1171 		default:
   1172 			if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
   1173 			    !IEEE80211_ADDR_EQ(ic->ic_des_bssid,
   1174 			    ic->ic_bss->ni_bssid))
   1175 				error = ENETRESET;
   1176 			break;
   1177 		}
   1178 		break;
   1179 	case SIOCG80211BSSID:
   1180 		bssid = (struct ieee80211_bssid *)data;
   1181 		switch (ic->ic_state) {
   1182 		case IEEE80211_S_INIT:
   1183 		case IEEE80211_S_SCAN:
   1184 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1185 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   1186 				    ic->ic_myaddr);
   1187 			else if (ic->ic_flags & IEEE80211_F_DESBSSID)
   1188 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   1189 				    ic->ic_des_bssid);
   1190 			else
   1191 				memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   1192 			break;
   1193 		default:
   1194 			IEEE80211_ADDR_COPY(bssid->i_bssid,
   1195 			    ic->ic_bss->ni_bssid);
   1196 			break;
   1197 		}
   1198 		break;
   1199 	case SIOCS80211CHANNEL:
   1200 		chanreq = (struct ieee80211chanreq *)data;
   1201 		if (chanreq->i_channel == IEEE80211_CHAN_ANY)
   1202 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   1203 		else if (chanreq->i_channel > IEEE80211_CHAN_MAX ||
   1204 		    isclr(ic->ic_chan_active, chanreq->i_channel)) {
   1205 			error = EINVAL;
   1206 			break;
   1207 		} else
   1208 			ic->ic_ibss_chan = ic->ic_des_chan =
   1209 			    &ic->ic_channels[chanreq->i_channel];
   1210 		switch (ic->ic_state) {
   1211 		case IEEE80211_S_INIT:
   1212 		case IEEE80211_S_SCAN:
   1213 			error = ENETRESET;
   1214 			break;
   1215 		default:
   1216 			if (ic->ic_opmode == IEEE80211_M_STA) {
   1217 				if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   1218 				    ic->ic_bss->ni_chan != ic->ic_des_chan)
   1219 					error = ENETRESET;
   1220 			} else {
   1221 				if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
   1222 					error = ENETRESET;
   1223 			}
   1224 			break;
   1225 		}
   1226 		break;
   1227 	case SIOCG80211CHANNEL:
   1228 		chanreq = (struct ieee80211chanreq *)data;
   1229 		switch (ic->ic_state) {
   1230 		case IEEE80211_S_INIT:
   1231 		case IEEE80211_S_SCAN:
   1232 			if (ic->ic_opmode == IEEE80211_M_STA)
   1233 				chan = ic->ic_des_chan;
   1234 			else
   1235 				chan = ic->ic_ibss_chan;
   1236 			break;
   1237 		default:
   1238 			chan = ic->ic_bss->ni_chan;
   1239 			break;
   1240 		}
   1241 		chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
   1242 		break;
   1243 	case SIOCGIFGENERIC:
   1244 		error = ieee80211_cfgget(ifp, cmd, data);
   1245 		break;
   1246 	case SIOCSIFGENERIC:
   1247 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   1248 		if (error)
   1249 			break;
   1250 		error = ieee80211_cfgset(ifp, cmd, data);
   1251 		break;
   1252 	default:
   1253 		error = ether_ioctl(ifp, cmd, data);
   1254 		break;
   1255 	}
   1256 	return error;
   1257 }
   1258 #endif /* __NetBSD__ */
   1259