xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision b5c9941514a11ce20e15aebce59c994fd33b5230)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler /*
371a1e1d21SSam Leffler  * IEEE 802.11 ioctl support (FreeBSD-specific)
381a1e1d21SSam Leffler  */
391a1e1d21SSam Leffler 
40b2e95691SSam Leffler #include "opt_inet.h"
41b2e95691SSam Leffler #include "opt_ipx.h"
42b2e95691SSam Leffler 
431a1e1d21SSam Leffler #include <sys/endian.h>
441a1e1d21SSam Leffler #include <sys/param.h>
451a1e1d21SSam Leffler #include <sys/kernel.h>
461a1e1d21SSam Leffler #include <sys/socket.h>
471a1e1d21SSam Leffler #include <sys/sockio.h>
481a1e1d21SSam Leffler #include <sys/systm.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net/if.h>
511a1e1d21SSam Leffler #include <net/if_arp.h>
521a1e1d21SSam Leffler #include <net/if_media.h>
531a1e1d21SSam Leffler #include <net/ethernet.h>
541a1e1d21SSam Leffler 
55b2e95691SSam Leffler #ifdef INET
56b2e95691SSam Leffler #include <netinet/in.h>
57b2e95691SSam Leffler #include <netinet/if_ether.h>
58b2e95691SSam Leffler #endif
59b2e95691SSam Leffler 
60b2e95691SSam Leffler #ifdef IPX
61b2e95691SSam Leffler #include <netipx/ipx.h>
62b2e95691SSam Leffler #include <netipx/ipx_if.h>
63b2e95691SSam Leffler #endif
64b2e95691SSam Leffler 
651a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
661a1e1d21SSam Leffler #include <net80211/ieee80211_ioctl.h>
671a1e1d21SSam Leffler 
681a1e1d21SSam Leffler #include <dev/wi/if_wavelan_ieee.h>
691a1e1d21SSam Leffler 
708a1b9b6aSSam Leffler #define	IS_UP(_ic) \
7113f4c340SRobert Watson 	(((_ic)->ic_ifp->if_flags & IFF_UP) &&			\
7213f4c340SRobert Watson 	    ((_ic)->ic_ifp->if_drv_flags & IFF_DRV_RUNNING))
738a1b9b6aSSam Leffler #define	IS_UP_AUTO(_ic) \
748a1b9b6aSSam Leffler 	(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
758a1b9b6aSSam Leffler 
761a1e1d21SSam Leffler /*
771a1e1d21SSam Leffler  * XXX
781a1e1d21SSam Leffler  * Wireless LAN specific configuration interface, which is compatible
791a1e1d21SSam Leffler  * with wicontrol(8).
801a1e1d21SSam Leffler  */
811a1e1d21SSam Leffler 
828a1b9b6aSSam Leffler struct wi_read_ap_args {
838a1b9b6aSSam Leffler 	int	i;		/* result count */
848a1b9b6aSSam Leffler 	struct wi_apinfo *ap;	/* current entry in result buffer */
858a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
868a1b9b6aSSam Leffler };
878a1b9b6aSSam Leffler 
888a1b9b6aSSam Leffler static void
898a1b9b6aSSam Leffler wi_read_ap_result(void *arg, struct ieee80211_node *ni)
901a1e1d21SSam Leffler {
918a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
928a1b9b6aSSam Leffler 	struct wi_read_ap_args *sa = arg;
938a1b9b6aSSam Leffler 	struct wi_apinfo *ap = sa->ap;
948a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
958a1b9b6aSSam Leffler 	int j;
968a1b9b6aSSam Leffler 
978a1b9b6aSSam Leffler 	if ((caddr_t)(ap + 1) > sa->max)
988a1b9b6aSSam Leffler 		return;
998a1b9b6aSSam Leffler 	memset(ap, 0, sizeof(struct wi_apinfo));
1008a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1018a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
1028a1b9b6aSSam Leffler 		ap->namelen = ic->ic_des_esslen;
1038a1b9b6aSSam Leffler 		if (ic->ic_des_esslen)
1048a1b9b6aSSam Leffler 			memcpy(ap->name, ic->ic_des_essid,
1058a1b9b6aSSam Leffler 			    ic->ic_des_esslen);
1068a1b9b6aSSam Leffler 	} else {
1078a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
1088a1b9b6aSSam Leffler 		ap->namelen = ni->ni_esslen;
1098a1b9b6aSSam Leffler 		if (ni->ni_esslen)
1108a1b9b6aSSam Leffler 			memcpy(ap->name, ni->ni_essid,
1118a1b9b6aSSam Leffler 			    ni->ni_esslen);
1128a1b9b6aSSam Leffler 	}
1138a1b9b6aSSam Leffler 	ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
1148a1b9b6aSSam Leffler 	ap->signal = ic->ic_node_getrssi(ni);
1158a1b9b6aSSam Leffler 	ap->capinfo = ni->ni_capinfo;
1168a1b9b6aSSam Leffler 	ap->interval = ni->ni_intval;
1178a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
1188a1b9b6aSSam Leffler 	for (j = 0; j < rs->rs_nrates; j++) {
1198a1b9b6aSSam Leffler 		if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
1208a1b9b6aSSam Leffler 			ap->rate = (rs->rs_rates[j] &
1218a1b9b6aSSam Leffler 			    IEEE80211_RATE_VAL) * 5; /* XXX */
1228a1b9b6aSSam Leffler 		}
1238a1b9b6aSSam Leffler 	}
1248a1b9b6aSSam Leffler 	sa->i++;
1258a1b9b6aSSam Leffler 	sa->ap++;
1268a1b9b6aSSam Leffler }
1278a1b9b6aSSam Leffler 
1288a1b9b6aSSam Leffler struct wi_read_prism2_args {
1298a1b9b6aSSam Leffler 	int	i;		/* result count */
1308a1b9b6aSSam Leffler 	struct wi_scan_res *res;/* current entry in result buffer */
1318a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1328a1b9b6aSSam Leffler };
1338a1b9b6aSSam Leffler 
1348a1b9b6aSSam Leffler static void
1358a1b9b6aSSam Leffler wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
1368a1b9b6aSSam Leffler {
1378a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1388a1b9b6aSSam Leffler 	struct wi_read_prism2_args *sa = arg;
1398a1b9b6aSSam Leffler 	struct wi_scan_res *res = sa->res;
1408a1b9b6aSSam Leffler 
1418a1b9b6aSSam Leffler 	if ((caddr_t)(res + 1) > sa->max)
1428a1b9b6aSSam Leffler 		return;
1438a1b9b6aSSam Leffler 	res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1448a1b9b6aSSam Leffler 	res->wi_noise = 0;
1458a1b9b6aSSam Leffler 	res->wi_signal = ic->ic_node_getrssi(ni);
1468a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
1478a1b9b6aSSam Leffler 	res->wi_interval = ni->ni_intval;
1488a1b9b6aSSam Leffler 	res->wi_capinfo = ni->ni_capinfo;
1498a1b9b6aSSam Leffler 	res->wi_ssid_len = ni->ni_esslen;
1508a1b9b6aSSam Leffler 	memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
1518a1b9b6aSSam Leffler 	/* NB: assumes wi_srates holds <= ni->ni_rates */
1528a1b9b6aSSam Leffler 	memcpy(res->wi_srates, ni->ni_rates.rs_rates,
1538a1b9b6aSSam Leffler 		sizeof(res->wi_srates));
1548a1b9b6aSSam Leffler 	if (ni->ni_rates.rs_nrates < 10)
1558a1b9b6aSSam Leffler 		res->wi_srates[ni->ni_rates.rs_nrates] = 0;
1568a1b9b6aSSam Leffler 	res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1578a1b9b6aSSam Leffler 	res->wi_rsvd = 0;
1588a1b9b6aSSam Leffler 
1598a1b9b6aSSam Leffler 	sa->i++;
1608a1b9b6aSSam Leffler 	sa->res++;
1618a1b9b6aSSam Leffler }
1628a1b9b6aSSam Leffler 
1638a1b9b6aSSam Leffler struct wi_read_sigcache_args {
1648a1b9b6aSSam Leffler 	int	i;		/* result count */
1658a1b9b6aSSam Leffler 	struct wi_sigcache *wsc;/* current entry in result buffer */
1668a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1678a1b9b6aSSam Leffler };
1688a1b9b6aSSam Leffler 
1698a1b9b6aSSam Leffler static void
1708a1b9b6aSSam Leffler wi_read_sigcache(void *arg, struct ieee80211_node *ni)
1718a1b9b6aSSam Leffler {
1728a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1738a1b9b6aSSam Leffler 	struct wi_read_sigcache_args *sa = arg;
1748a1b9b6aSSam Leffler 	struct wi_sigcache *wsc = sa->wsc;
1758a1b9b6aSSam Leffler 
1768a1b9b6aSSam Leffler 	if ((caddr_t)(wsc + 1) > sa->max)
1778a1b9b6aSSam Leffler 		return;
1788a1b9b6aSSam Leffler 	memset(wsc, 0, sizeof(struct wi_sigcache));
1798a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
1808a1b9b6aSSam Leffler 	wsc->signal = ic->ic_node_getrssi(ni);
1818a1b9b6aSSam Leffler 
1828a1b9b6aSSam Leffler 	sa->wsc++;
1838a1b9b6aSSam Leffler 	sa->i++;
1848a1b9b6aSSam Leffler }
1858a1b9b6aSSam Leffler 
1868a1b9b6aSSam Leffler int
1878a1b9b6aSSam Leffler ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
1888a1b9b6aSSam Leffler {
1898a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1901a1e1d21SSam Leffler 	int i, j, error;
1911a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
1921a1e1d21SSam Leffler 	struct wi_req wreq;
1931a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
1941a1e1d21SSam Leffler 
1951a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1961a1e1d21SSam Leffler 	if (error)
1971a1e1d21SSam Leffler 		return error;
1981a1e1d21SSam Leffler 	wreq.wi_len = 0;
1991a1e1d21SSam Leffler 	switch (wreq.wi_type) {
2001a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
2011a1e1d21SSam Leffler 		/* nothing appropriate */
2021a1e1d21SSam Leffler 		break;
2031a1e1d21SSam Leffler 	case WI_RID_NODENAME:
2041a1e1d21SSam Leffler 		strcpy((char *)&wreq.wi_val[1], hostname);
2051a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(strlen(hostname));
2061a1e1d21SSam Leffler 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
2071a1e1d21SSam Leffler 		break;
2081a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
2091a1e1d21SSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
2101a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;
2111a1e1d21SSam Leffler 			wreq.wi_len = 1;
2121a1e1d21SSam Leffler 			break;
2131a1e1d21SSam Leffler 		}
2141a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
2151a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
2161a1e1d21SSam Leffler 		    ic->ic_bss->ni_esslen);
2171a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
2181a1e1d21SSam Leffler 		break;
2191a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
2201a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
2211a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
2221a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
2231a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
2241a1e1d21SSam Leffler 		break;
2251a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
2261a1e1d21SSam Leffler 		if (ic->ic_state == IEEE80211_S_RUN)
2271a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
2281a1e1d21SSam Leffler 		else
2291a1e1d21SSam Leffler 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
2301a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2311a1e1d21SSam Leffler 		break;
2321a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
2331a1e1d21SSam Leffler 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
2341a1e1d21SSam Leffler 		/*
2351a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
2361a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
2371a1e1d21SSam Leffler 		 */
2381a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
2391a1e1d21SSam Leffler 			i = 1;
2401a1e1d21SSam Leffler 		else
2411a1e1d21SSam Leffler 			i = 0;
2421a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
2431a1e1d21SSam Leffler 			if (isset(ic->ic_chan_active, i)) {
2441a1e1d21SSam Leffler 				setbit((u_int8_t *)wreq.wi_val, j);
2451a1e1d21SSam Leffler 				wreq.wi_len = j / 16 + 1;
2461a1e1d21SSam Leffler 			}
2471a1e1d21SSam Leffler 		break;
2481a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
2491a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2501a1e1d21SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
2511a1e1d21SSam Leffler 		wreq.wi_len = 1;
2521a1e1d21SSam Leffler 		break;
2531a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
2541a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
255b5c99415SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_curchan));
2561a1e1d21SSam Leffler 		wreq.wi_len = 1;
2571a1e1d21SSam Leffler 		break;
2581a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
2591a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;				/* quality */
2608a1b9b6aSSam Leffler 		wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
2611a1e1d21SSam Leffler 		wreq.wi_val[2] = 0;				/* noise */
2621a1e1d21SSam Leffler 		wreq.wi_len = 3;
2631a1e1d21SSam Leffler 		break;
2641a1e1d21SSam Leffler 	case WI_RID_PROMISC:
2651a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
2661a1e1d21SSam Leffler 		wreq.wi_len = 1;
2671a1e1d21SSam Leffler 		break;
2681a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
2691a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_opmode);
2701a1e1d21SSam Leffler 		wreq.wi_len = 1;
2711a1e1d21SSam Leffler 		break;
2721a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
2731a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
2741a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2751a1e1d21SSam Leffler 		break;
2761a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
2772c39b32cSSam Leffler 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
2781a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;	/* auto */
2791a1e1d21SSam Leffler 		else
2801a1e1d21SSam Leffler 			wreq.wi_val[0] = htole16(
2811a1e1d21SSam Leffler 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
2821a1e1d21SSam Leffler 			    IEEE80211_RATE_VAL) / 2);
2831a1e1d21SSam Leffler 		wreq.wi_len = 1;
2841a1e1d21SSam Leffler 		break;
2851a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
2861a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2871a1e1d21SSam Leffler 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
2881a1e1d21SSam Leffler 		    IEEE80211_RATE_VAL) / 2);
2891a1e1d21SSam Leffler 		wreq.wi_len = 1;
2901a1e1d21SSam Leffler 		break;
2911a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
2921a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2931a1e1d21SSam Leffler 		wreq.wi_len = 1;
2941a1e1d21SSam Leffler 		break;
2951a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
2961a1e1d21SSam Leffler 		wreq.wi_val[0] =
2971a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
2981a1e1d21SSam Leffler 		wreq.wi_len = 1;
2991a1e1d21SSam Leffler 		break;
3001a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
3011a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;	/* no ... not supported */
3021a1e1d21SSam Leffler 		wreq.wi_len = 1;
3031a1e1d21SSam Leffler 		break;
3041a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
3058a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_roaming);	/* XXX map */
3061a1e1d21SSam Leffler 		wreq.wi_len = 1;
3071a1e1d21SSam Leffler 		break;
3081a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
3091a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
3101a1e1d21SSam Leffler 		wreq.wi_len = 1;
3111a1e1d21SSam Leffler 		break;
3121a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
3131a1e1d21SSam Leffler 		wreq.wi_val[0] =
3141a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
3151a1e1d21SSam Leffler 		wreq.wi_len = 1;
3161a1e1d21SSam Leffler 		break;
3171a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
3181a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_lintval);
3191a1e1d21SSam Leffler 		wreq.wi_len = 1;
3201a1e1d21SSam Leffler 		break;
3211a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
3221a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
3231a1e1d21SSam Leffler 		wreq.wi_len = 1;
3241a1e1d21SSam Leffler 		break;
3251a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
3268a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(1);	/* always available */
3271a1e1d21SSam Leffler 		wreq.wi_len = 1;
3281a1e1d21SSam Leffler 		break;
3291a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
3301a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
3311a1e1d21SSam Leffler 		wreq.wi_len = 1;
3321a1e1d21SSam Leffler 		break;
3331a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
3341a1e1d21SSam Leffler 		wreq.wi_val[0] =
3358a1b9b6aSSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
3361a1e1d21SSam Leffler 		wreq.wi_len = 1;
3371a1e1d21SSam Leffler 		break;
3381a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
3398a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_def_txkey);
3401a1e1d21SSam Leffler 		wreq.wi_len = 1;
3411a1e1d21SSam Leffler 		break;
3421a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
3431a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
3441a1e1d21SSam Leffler 		/* do not show keys to non-root user */
3451a1e1d21SSam Leffler 		error = suser(curthread);
3461a1e1d21SSam Leffler 		if (error) {
3471a1e1d21SSam Leffler 			memset(keys, 0, sizeof(*keys));
3481a1e1d21SSam Leffler 			error = 0;
3491a1e1d21SSam Leffler 			break;
3501a1e1d21SSam Leffler 		}
3511a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
3521a1e1d21SSam Leffler 			keys->wi_keys[i].wi_keylen =
3538a1b9b6aSSam Leffler 			    htole16(ic->ic_nw_keys[i].wk_keylen);
3541a1e1d21SSam Leffler 			memcpy(keys->wi_keys[i].wi_keydat,
3558a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_key,
3568a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_keylen);
3571a1e1d21SSam Leffler 		}
3581a1e1d21SSam Leffler 		wreq.wi_len = sizeof(*keys) / 2;
3591a1e1d21SSam Leffler 		break;
3601a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
3618a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
3621a1e1d21SSam Leffler 		wreq.wi_len = 1;
3631a1e1d21SSam Leffler 		break;
3641a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
3651a1e1d21SSam Leffler 		/* XXX: should be implemented in lower drivers */
3661a1e1d21SSam Leffler 		break;
3671a1e1d21SSam Leffler 	case WI_RID_READ_APS:
3681a1e1d21SSam Leffler 		/*
3691a1e1d21SSam Leffler 		 * Don't return results until active scan completes.
3701a1e1d21SSam Leffler 		 */
3718a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3728a1b9b6aSSam Leffler 			struct wi_read_ap_args args;
3738a1b9b6aSSam Leffler 
3748a1b9b6aSSam Leffler 			args.i = 0;
3758a1b9b6aSSam Leffler 			args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
3768a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
3778a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
3788a1b9b6aSSam Leffler 				wi_read_ap_result, &args);
3798a1b9b6aSSam Leffler 			memcpy(wreq.wi_val, &args.i, sizeof(args.i));
3808a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(int) +
3818a1b9b6aSSam Leffler 				sizeof(struct wi_apinfo) * args.i) / 2;
3828a1b9b6aSSam Leffler 		} else
3831a1e1d21SSam Leffler 			error = EINPROGRESS;
3841a1e1d21SSam Leffler 		break;
3851a1e1d21SSam Leffler 	case WI_RID_PRISM2:
3868a1b9b6aSSam Leffler 		/* NB: we lie so WI_RID_SCAN_RES can include rates */
3878a1b9b6aSSam Leffler 		wreq.wi_val[0] = 1;
3881a1e1d21SSam Leffler 		wreq.wi_len = sizeof(u_int16_t) / 2;
3891a1e1d21SSam Leffler 		break;
3901a1e1d21SSam Leffler 	case WI_RID_SCAN_RES:			/* compatibility interface */
3918a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3928a1b9b6aSSam Leffler 			struct wi_read_prism2_args args;
3938a1b9b6aSSam Leffler 			struct wi_scan_p2_hdr *p2;
3948a1b9b6aSSam Leffler 
3958a1b9b6aSSam Leffler 			/* NB: use Prism2 format so we can include rate info */
3968a1b9b6aSSam Leffler 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
3978a1b9b6aSSam Leffler 			args.i = 0;
3988a1b9b6aSSam Leffler 			args.res = (void *)&p2[1];
3998a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
4008a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
4018a1b9b6aSSam Leffler 				wi_read_prism2_result, &args);
4028a1b9b6aSSam Leffler 			p2->wi_rsvd = 0;
4038a1b9b6aSSam Leffler 			p2->wi_reason = args.i;
4048a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(*p2) +
4058a1b9b6aSSam Leffler 				sizeof(struct wi_scan_res) * args.i) / 2;
4068a1b9b6aSSam Leffler 		} else
4071a1e1d21SSam Leffler 			error = EINPROGRESS;
4081a1e1d21SSam Leffler 		break;
4098a1b9b6aSSam Leffler 	case WI_RID_READ_CACHE: {
4108a1b9b6aSSam Leffler 		struct wi_read_sigcache_args args;
4118a1b9b6aSSam Leffler 		args.i = 0;
4128a1b9b6aSSam Leffler 		args.wsc = (struct wi_sigcache *) wreq.wi_val;
4138a1b9b6aSSam Leffler 		args.max = (void *)(&wreq + 1);
4148a1b9b6aSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
4158a1b9b6aSSam Leffler 		wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
4168a1b9b6aSSam Leffler 		break;
4171a1e1d21SSam Leffler 	}
4181a1e1d21SSam Leffler 	default:
4191a1e1d21SSam Leffler 		error = EINVAL;
4201a1e1d21SSam Leffler 		break;
4211a1e1d21SSam Leffler 	}
4221a1e1d21SSam Leffler 	if (error == 0) {
4231a1e1d21SSam Leffler 		wreq.wi_len++;
4241a1e1d21SSam Leffler 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
4251a1e1d21SSam Leffler 	}
4261a1e1d21SSam Leffler 	return error;
4271a1e1d21SSam Leffler }
4281a1e1d21SSam Leffler 
4291a1e1d21SSam Leffler static int
4301a1e1d21SSam Leffler findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
4311a1e1d21SSam Leffler {
4321a1e1d21SSam Leffler #define	IEEERATE(_ic,_m,_i) \
4331a1e1d21SSam Leffler 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
4341a1e1d21SSam Leffler 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
4351a1e1d21SSam Leffler 	for (i = 0; i < nrates; i++)
4361a1e1d21SSam Leffler 		if (IEEERATE(ic, mode, i) == rate)
4371a1e1d21SSam Leffler 			return i;
4381a1e1d21SSam Leffler 	return -1;
4391a1e1d21SSam Leffler #undef IEEERATE
4401a1e1d21SSam Leffler }
4411a1e1d21SSam Leffler 
44293685685SSam Leffler /*
44393685685SSam Leffler  * Prepare to do a user-initiated scan for AP's.  If no
44493685685SSam Leffler  * current/default channel is setup or the current channel
44593685685SSam Leffler  * is invalid then pick the first available channel from
44693685685SSam Leffler  * the active list as the place to start the scan.
44793685685SSam Leffler  */
44893685685SSam Leffler static int
4498a1b9b6aSSam Leffler ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
45093685685SSam Leffler {
45193685685SSam Leffler 
4528a1b9b6aSSam Leffler 	/*
4538a1b9b6aSSam Leffler 	 * XXX don't permit a scan to be started unless we
4548a1b9b6aSSam Leffler 	 * know the device is ready.  For the moment this means
4558a1b9b6aSSam Leffler 	 * the device is marked up as this is the required to
4568a1b9b6aSSam Leffler 	 * initialize the hardware.  It would be better to permit
4578a1b9b6aSSam Leffler 	 * scanning prior to being up but that'll require some
4588a1b9b6aSSam Leffler 	 * changes to the infrastructure.
4598a1b9b6aSSam Leffler 	 */
4608a1b9b6aSSam Leffler 	if (!IS_UP(ic))
4618a1b9b6aSSam Leffler 		return EINVAL;
4628a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
46393685685SSam Leffler 	/*
4648a1b9b6aSSam Leffler 	 * We force the state to INIT before calling ieee80211_new_state
4658a1b9b6aSSam Leffler 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
4668a1b9b6aSSam Leffler 	 * altering the current state but that's not possible right now.
46793685685SSam Leffler 	 */
4688a1b9b6aSSam Leffler 	/* XXX handle proberequest case */
4698a1b9b6aSSam Leffler 	ic->ic_state = IEEE80211_S_INIT;	/* XXX bypass state machine */
4708a1b9b6aSSam Leffler 	return 0;
47193685685SSam Leffler }
47293685685SSam Leffler 
4731a1e1d21SSam Leffler int
4748a1b9b6aSSam Leffler ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
4751a1e1d21SSam Leffler {
4768a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
4771a1e1d21SSam Leffler 	int i, j, len, error, rate;
4781a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
4791a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
4801a1e1d21SSam Leffler 	struct wi_req wreq;
4811a1e1d21SSam Leffler 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
4821a1e1d21SSam Leffler 
4831a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
4841a1e1d21SSam Leffler 	if (error)
4851a1e1d21SSam Leffler 		return error;
4861a1e1d21SSam Leffler 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
4871a1e1d21SSam Leffler 	switch (wreq.wi_type) {
4881a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
4891a1e1d21SSam Leffler 	case WI_RID_NODENAME:
4901a1e1d21SSam Leffler 		return EPERM;
4911a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
4921a1e1d21SSam Leffler 		return EPERM;
4931a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
4941a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
4951a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
4961a1e1d21SSam Leffler 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
4971a1e1d21SSam Leffler 			error = ENOSPC;
4981a1e1d21SSam Leffler 			break;
4991a1e1d21SSam Leffler 		}
5001a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
5011a1e1d21SSam Leffler 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
502b9ee58c4SSam Leffler 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
5031a1e1d21SSam Leffler 		error = ENETRESET;
5041a1e1d21SSam Leffler 		break;
5051a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
5061a1e1d21SSam Leffler 		return EPERM;
5071a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
5081a1e1d21SSam Leffler 		if (len != 2)
5091a1e1d21SSam Leffler 			return EINVAL;
5101a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
5111a1e1d21SSam Leffler 		if (i < 0 ||
5121a1e1d21SSam Leffler 		    i > IEEE80211_CHAN_MAX ||
5131a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, i))
5141a1e1d21SSam Leffler 			return EINVAL;
5151a1e1d21SSam Leffler 		ic->ic_ibss_chan = &ic->ic_channels[i];
5168a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
5178a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5188a1b9b6aSSam Leffler 		else
5191a1e1d21SSam Leffler 			error = ENETRESET;
5201a1e1d21SSam Leffler 		break;
5211a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
5221a1e1d21SSam Leffler 		return EPERM;
5231a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
5241a1e1d21SSam Leffler 		return EPERM;
5251a1e1d21SSam Leffler 	case WI_RID_PROMISC:
5261a1e1d21SSam Leffler 		if (len != 2)
5271a1e1d21SSam Leffler 			return EINVAL;
5281a1e1d21SSam Leffler 		if (ifp->if_flags & IFF_PROMISC) {
5291a1e1d21SSam Leffler 			if (wreq.wi_val[0] == 0) {
5301a1e1d21SSam Leffler 				ifp->if_flags &= ~IFF_PROMISC;
5311a1e1d21SSam Leffler 				error = ENETRESET;
5321a1e1d21SSam Leffler 			}
5331a1e1d21SSam Leffler 		} else {
5341a1e1d21SSam Leffler 			if (wreq.wi_val[0] != 0) {
5351a1e1d21SSam Leffler 				ifp->if_flags |= IFF_PROMISC;
5361a1e1d21SSam Leffler 				error = ENETRESET;
5371a1e1d21SSam Leffler 			}
5381a1e1d21SSam Leffler 		}
5391a1e1d21SSam Leffler 		break;
5401a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
5411a1e1d21SSam Leffler 		if (len != 2)
5421a1e1d21SSam Leffler 			return EINVAL;
5431a1e1d21SSam Leffler 		switch (le16toh(wreq.wi_val[0])) {
5441a1e1d21SSam Leffler 		case IEEE80211_M_STA:
5451a1e1d21SSam Leffler 			break;
5461a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
5471a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
5481a1e1d21SSam Leffler 				return EINVAL;
5491a1e1d21SSam Leffler 			break;
5501a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
5511a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_DS ||
5521a1e1d21SSam Leffler 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
5531a1e1d21SSam Leffler 				return EINVAL;
5541a1e1d21SSam Leffler 			break;
5551a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
5561a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
5571a1e1d21SSam Leffler 				return EINVAL;
5581a1e1d21SSam Leffler 			break;
5591a1e1d21SSam Leffler 		default:
5601a1e1d21SSam Leffler 			return EINVAL;
5611a1e1d21SSam Leffler 		}
5621a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
5631a1e1d21SSam Leffler 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
5648a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5651a1e1d21SSam Leffler 		}
5661a1e1d21SSam Leffler 		break;
5671a1e1d21SSam Leffler #if 0
5681a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
5691a1e1d21SSam Leffler 		if (len != IEEE80211_ADDR_LEN)
5701a1e1d21SSam Leffler 			return EINVAL;
5711a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
5721a1e1d21SSam Leffler 		/* if_init will copy lladdr into ic_myaddr */
5731a1e1d21SSam Leffler 		error = ENETRESET;
5741a1e1d21SSam Leffler 		break;
5751a1e1d21SSam Leffler #endif
5761a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
5771a1e1d21SSam Leffler 		if (len != 2)
5781a1e1d21SSam Leffler 			return EINVAL;
5791a1e1d21SSam Leffler 		if (wreq.wi_val[0] == 0) {
5801a1e1d21SSam Leffler 			/* auto */
5812c39b32cSSam Leffler 			ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
5821a1e1d21SSam Leffler 			break;
5831a1e1d21SSam Leffler 		}
5841a1e1d21SSam Leffler 		rate = 2 * le16toh(wreq.wi_val[0]);
5851a1e1d21SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
5861a1e1d21SSam Leffler 			/*
5871a1e1d21SSam Leffler 			 * In autoselect mode search for the rate.  We take
5881a1e1d21SSam Leffler 			 * the first instance which may not be right, but we
5891a1e1d21SSam Leffler 			 * are limited by the interface.  Note that we also
5901a1e1d21SSam Leffler 			 * lock the mode to insure the rate is meaningful
5911a1e1d21SSam Leffler 			 * when it is used.
5921a1e1d21SSam Leffler 			 */
5931a1e1d21SSam Leffler 			for (j = IEEE80211_MODE_11A;
5941a1e1d21SSam Leffler 			     j < IEEE80211_MODE_MAX; j++) {
5951a1e1d21SSam Leffler 				if ((ic->ic_modecaps & (1<<j)) == 0)
5961a1e1d21SSam Leffler 					continue;
5971a1e1d21SSam Leffler 				i = findrate(ic, j, rate);
5981a1e1d21SSam Leffler 				if (i != -1) {
5991a1e1d21SSam Leffler 					/* lock mode too */
6001a1e1d21SSam Leffler 					ic->ic_curmode = j;
6011a1e1d21SSam Leffler 					goto setrate;
6021a1e1d21SSam Leffler 				}
6031a1e1d21SSam Leffler 			}
6041a1e1d21SSam Leffler 		} else {
6051a1e1d21SSam Leffler 			i = findrate(ic, ic->ic_curmode, rate);
6061a1e1d21SSam Leffler 			if (i != -1)
6071a1e1d21SSam Leffler 				goto setrate;
6081a1e1d21SSam Leffler 		}
6091a1e1d21SSam Leffler 		return EINVAL;
6101a1e1d21SSam Leffler 	setrate:
6111a1e1d21SSam Leffler 		ic->ic_fixed_rate = i;
6128a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6131a1e1d21SSam Leffler 		break;
6141a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
6151a1e1d21SSam Leffler 		return EPERM;
6161a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
6171a1e1d21SSam Leffler 		if (len != 2)
6181a1e1d21SSam Leffler 			return EINVAL;
6191a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != IEEE80211_MAX_LEN)
6201a1e1d21SSam Leffler 			return EINVAL;		/* TODO: RTS */
6211a1e1d21SSam Leffler 		break;
6221a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
6231a1e1d21SSam Leffler 		if (len != 2)
6241a1e1d21SSam Leffler 			return EINVAL;
6251a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6261a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
6271a1e1d21SSam Leffler 				return EINVAL;
6281a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
6291a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_IBSSON;
6301a1e1d21SSam Leffler 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
6311a1e1d21SSam Leffler 				    ic->ic_state == IEEE80211_S_SCAN)
6328a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6331a1e1d21SSam Leffler 			}
6341a1e1d21SSam Leffler 		} else {
6351a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
6361a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
6371a1e1d21SSam Leffler 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
6381a1e1d21SSam Leffler 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
6398a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6401a1e1d21SSam Leffler 				}
6411a1e1d21SSam Leffler 			}
6421a1e1d21SSam Leffler 		}
6431a1e1d21SSam Leffler 		break;
6441a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
6451a1e1d21SSam Leffler 		if (len != 2)
6461a1e1d21SSam Leffler 			return EINVAL;
6471a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0)
6481a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6491a1e1d21SSam Leffler 		break;
6501a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
6511a1e1d21SSam Leffler 		if (len != 2)
6521a1e1d21SSam Leffler 			return EINVAL;
6538a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
6548a1b9b6aSSam Leffler 		if (i > IEEE80211_ROAMING_MANUAL)
6551a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6568a1b9b6aSSam Leffler 		ic->ic_roaming = i;
6571a1e1d21SSam Leffler 		break;
6581a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
6591a1e1d21SSam Leffler 		if (len != 2)
6601a1e1d21SSam Leffler 			return EINVAL;
6611a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != 1)
6621a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6631a1e1d21SSam Leffler 		break;
6641a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
6651a1e1d21SSam Leffler 		if (len != 2)
6661a1e1d21SSam Leffler 			return EINVAL;
6671a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6681a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
6691a1e1d21SSam Leffler 				return EINVAL;
6701a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
6711a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
6728a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6731a1e1d21SSam Leffler 			}
6741a1e1d21SSam Leffler 		} else {
6751a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
6761a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
6778a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6781a1e1d21SSam Leffler 			}
6791a1e1d21SSam Leffler 		}
6801a1e1d21SSam Leffler 		break;
6811a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
6821a1e1d21SSam Leffler 		if (len != 2)
6831a1e1d21SSam Leffler 			return EINVAL;
6841a1e1d21SSam Leffler 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
6851a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
6868a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6871a1e1d21SSam Leffler 		break;
6881a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
6891a1e1d21SSam Leffler 		return EPERM;
6901a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
6911a1e1d21SSam Leffler 		return EPERM;
6921a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
6931a1e1d21SSam Leffler 		if (len != 2)
6941a1e1d21SSam Leffler 			return EINVAL;
6958a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
6968a1b9b6aSSam Leffler 		if (i > IEEE80211_AUTH_WPA)
6978a1b9b6aSSam Leffler 			return EINVAL;
6988a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = i;		/* XXX ENETRESET? */
6998a1b9b6aSSam Leffler 		error = ENETRESET;
7001a1e1d21SSam Leffler 		break;
7011a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
7021a1e1d21SSam Leffler 		if (len != 2)
7031a1e1d21SSam Leffler 			return EINVAL;
7041a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
7051a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
7061a1e1d21SSam Leffler 				return EINVAL;
7078a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
7088a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_PRIVACY;
7091a1e1d21SSam Leffler 				error = ENETRESET;
7101a1e1d21SSam Leffler 			}
7111a1e1d21SSam Leffler 		} else {
7128a1b9b6aSSam Leffler 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
7138a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PRIVACY;
7141a1e1d21SSam Leffler 				error = ENETRESET;
7151a1e1d21SSam Leffler 			}
7161a1e1d21SSam Leffler 		}
7171a1e1d21SSam Leffler 		break;
7181a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
7191a1e1d21SSam Leffler 		if (len != 2)
7201a1e1d21SSam Leffler 			return EINVAL;
7211a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
7221a1e1d21SSam Leffler 		if (i >= IEEE80211_WEP_NKID)
7231a1e1d21SSam Leffler 			return EINVAL;
7248a1b9b6aSSam Leffler 		ic->ic_def_txkey = i;
7258a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7261a1e1d21SSam Leffler 		break;
7271a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
7281a1e1d21SSam Leffler 		if (len != sizeof(struct wi_ltv_keys))
7291a1e1d21SSam Leffler 			return EINVAL;
7301a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
7311a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7321a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7331a1e1d21SSam Leffler 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
7341a1e1d21SSam Leffler 				return EINVAL;
7358a1b9b6aSSam Leffler 			if (len > IEEE80211_KEYBUF_SIZE)
7361a1e1d21SSam Leffler 				return EINVAL;
7371a1e1d21SSam Leffler 		}
7381a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7398a1b9b6aSSam Leffler 			struct ieee80211_key *k = &ic->ic_nw_keys[i];
7408a1b9b6aSSam Leffler 
7411a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7428a1b9b6aSSam Leffler 			k->wk_keylen = len;
7438a1b9b6aSSam Leffler 			k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
7448a1b9b6aSSam Leffler 			memset(k->wk_key, 0, sizeof(k->wk_key));
7458a1b9b6aSSam Leffler 			memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
7468a1b9b6aSSam Leffler #if 0
7478a1b9b6aSSam Leffler 			k->wk_type = IEEE80211_CIPHER_WEP;
7488a1b9b6aSSam Leffler #endif
7491a1e1d21SSam Leffler 		}
7501a1e1d21SSam Leffler 		error = ENETRESET;
7511a1e1d21SSam Leffler 		break;
7521a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
7531a1e1d21SSam Leffler 		if (len != 2)
7541a1e1d21SSam Leffler 			return EINVAL;
7551a1e1d21SSam Leffler 		len = le16toh(wreq.wi_val[0]);
7561a1e1d21SSam Leffler 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
7571a1e1d21SSam Leffler 			return EINVAL;
7581a1e1d21SSam Leffler 		ic->ic_fragthreshold = len;
7598a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7601a1e1d21SSam Leffler 		break;
7611a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
7621a1e1d21SSam Leffler 		error = EPERM;
7631a1e1d21SSam Leffler 		break;
7641a1e1d21SSam Leffler 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
7651a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7661a1e1d21SSam Leffler 			break;
7678a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
76893685685SSam Leffler 		if (error == 0)
769a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
7701a1e1d21SSam Leffler 		break;
7711a1e1d21SSam Leffler 	case WI_RID_SCAN_APS:
7721a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7731a1e1d21SSam Leffler 			break;
7741a1e1d21SSam Leffler 		len--;			/* XXX: tx rate? */
7751a1e1d21SSam Leffler 		/* FALLTHRU */
7761a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
7771a1e1d21SSam Leffler 		memset(chanlist, 0, sizeof(chanlist));
7781a1e1d21SSam Leffler 		/*
7791a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
7801a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
7811a1e1d21SSam Leffler 		 */
7821a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
7831a1e1d21SSam Leffler 			i = 1;
7841a1e1d21SSam Leffler 		else
7851a1e1d21SSam Leffler 			i = 0;
7861a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
7871a1e1d21SSam Leffler 			if ((j / 8) >= len)
7881a1e1d21SSam Leffler 				break;
7891a1e1d21SSam Leffler 			if (isclr((u_int8_t *)wreq.wi_val, j))
7901a1e1d21SSam Leffler 				continue;
7911a1e1d21SSam Leffler 			if (isclr(ic->ic_chan_active, i)) {
7921a1e1d21SSam Leffler 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
7931a1e1d21SSam Leffler 					continue;
7941a1e1d21SSam Leffler 				if (isclr(ic->ic_chan_avail, i))
7951a1e1d21SSam Leffler 					return EPERM;
7961a1e1d21SSam Leffler 			}
7971a1e1d21SSam Leffler 			setbit(chanlist, i);
7981a1e1d21SSam Leffler 		}
7998a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, chanlist);
80093685685SSam Leffler 		if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
80193685685SSam Leffler 			/* NB: ignore error from ieee80211_setupscan */
8021a1e1d21SSam Leffler 			error = ENETRESET;
80393685685SSam Leffler 		} else if (error == 0)
804a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
8051a1e1d21SSam Leffler 		break;
8061a1e1d21SSam Leffler 	default:
8071a1e1d21SSam Leffler 		error = EINVAL;
8081a1e1d21SSam Leffler 		break;
8091a1e1d21SSam Leffler 	}
8108a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
8118a1b9b6aSSam Leffler 		error = 0;
8121a1e1d21SSam Leffler 	return error;
8131a1e1d21SSam Leffler }
8141a1e1d21SSam Leffler 
8158a1b9b6aSSam Leffler static int
8168a1b9b6aSSam Leffler cap2cipher(int flag)
8178a1b9b6aSSam Leffler {
8188a1b9b6aSSam Leffler 	switch (flag) {
8198a1b9b6aSSam Leffler 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
8208a1b9b6aSSam Leffler 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
8218a1b9b6aSSam Leffler 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
8228a1b9b6aSSam Leffler 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
8238a1b9b6aSSam Leffler 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
8248a1b9b6aSSam Leffler 	}
8258a1b9b6aSSam Leffler 	return -1;
8268a1b9b6aSSam Leffler }
8278a1b9b6aSSam Leffler 
8288a1b9b6aSSam Leffler static int
8298a1b9b6aSSam Leffler ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
8308a1b9b6aSSam Leffler {
8318a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8328a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
8338a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
8348a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
8358a1b9b6aSSam Leffler 	u_int kid;
8368a1b9b6aSSam Leffler 	int error;
8378a1b9b6aSSam Leffler 
8388a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
8398a1b9b6aSSam Leffler 		return EINVAL;
8408a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
8418a1b9b6aSSam Leffler 	if (error)
8428a1b9b6aSSam Leffler 		return error;
8438a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
8448a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
845acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
8468a1b9b6aSSam Leffler 		if (ni == NULL)
8478a1b9b6aSSam Leffler 			return EINVAL;		/* XXX */
8488a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
8498a1b9b6aSSam Leffler 	} else {
8508a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8518a1b9b6aSSam Leffler 			return EINVAL;
8528a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
8538a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
8548a1b9b6aSSam Leffler 		ni = NULL;
8558a1b9b6aSSam Leffler 	}
8568a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
8578a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
8588a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
8598a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
8608a1b9b6aSSam Leffler 	if (wk->wk_keyix == ic->ic_def_txkey)
8618a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
8628a1b9b6aSSam Leffler 	if (suser(curthread) == 0) {
8638a1b9b6aSSam Leffler 		/* NB: only root can read key data */
8648a1b9b6aSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc;
8658a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
8668a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
8678a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
8688a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
8698a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
8708a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
8718a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
8728a1b9b6aSSam Leffler 		}
8738a1b9b6aSSam Leffler 	} else {
8748a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
8758a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
8768a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
8778a1b9b6aSSam Leffler 	}
8788a1b9b6aSSam Leffler 	if (ni != NULL)
8798a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
8808a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
8818a1b9b6aSSam Leffler }
8828a1b9b6aSSam Leffler 
8838a1b9b6aSSam Leffler static int
8848a1b9b6aSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
8858a1b9b6aSSam Leffler {
8868a1b9b6aSSam Leffler 
8878a1b9b6aSSam Leffler 	if (sizeof(ic->ic_chan_active) > ireq->i_len)
8888a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
8898a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
8908a1b9b6aSSam Leffler }
8918a1b9b6aSSam Leffler 
8928a1b9b6aSSam Leffler static int
8938a1b9b6aSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
8948a1b9b6aSSam Leffler {
8958a1b9b6aSSam Leffler 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
8968a1b9b6aSSam Leffler 	int i, space;
8978a1b9b6aSSam Leffler 
8988a1b9b6aSSam Leffler 	/*
8998a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
9008a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
9018a1b9b6aSSam Leffler 	 */
9028a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
9038a1b9b6aSSam Leffler 		i = 1;
9048a1b9b6aSSam Leffler 	else
9058a1b9b6aSSam Leffler 		i = 0;
9068a1b9b6aSSam Leffler 	memset(&chans, 0, sizeof(chans));
9078a1b9b6aSSam Leffler 	for (; i <= IEEE80211_CHAN_MAX; i++)
9088a1b9b6aSSam Leffler 		if (isset(ic->ic_chan_avail, i)) {
9098a1b9b6aSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
9108a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
9118a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
9128a1b9b6aSSam Leffler 			chans.ic_nchans++;
9138a1b9b6aSSam Leffler 		}
9148a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
9158a1b9b6aSSam Leffler 			ic_chans[chans.ic_nchans]);
9168a1b9b6aSSam Leffler 	if (space > ireq->i_len)
9178a1b9b6aSSam Leffler 		space = ireq->i_len;
9188a1b9b6aSSam Leffler 	return copyout(&chans, ireq->i_data, space);
9198a1b9b6aSSam Leffler }
9208a1b9b6aSSam Leffler 
9218a1b9b6aSSam Leffler static int
9228a1b9b6aSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
9238a1b9b6aSSam Leffler {
9248a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9258a1b9b6aSSam Leffler 	struct ieee80211req_wpaie wpaie;
9268a1b9b6aSSam Leffler 	int error;
9278a1b9b6aSSam Leffler 
9288a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
9298a1b9b6aSSam Leffler 		return EINVAL;
9308a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
9318a1b9b6aSSam Leffler 	if (error != 0)
9328a1b9b6aSSam Leffler 		return error;
933acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
9348a1b9b6aSSam Leffler 	if (ni == NULL)
9358a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9368a1b9b6aSSam Leffler 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
9378a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL) {
9388a1b9b6aSSam Leffler 		int ielen = ni->ni_wpa_ie[1] + 2;
9398a1b9b6aSSam Leffler 		if (ielen > sizeof(wpaie.wpa_ie))
9408a1b9b6aSSam Leffler 			ielen = sizeof(wpaie.wpa_ie);
9418a1b9b6aSSam Leffler 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
9428a1b9b6aSSam Leffler 	}
9438a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9448a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(wpaie))
9458a1b9b6aSSam Leffler 		ireq->i_len = sizeof(wpaie);
9468a1b9b6aSSam Leffler 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
9478a1b9b6aSSam Leffler }
9488a1b9b6aSSam Leffler 
9498a1b9b6aSSam Leffler static int
9508a1b9b6aSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
9518a1b9b6aSSam Leffler {
9528a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9538a1b9b6aSSam Leffler 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
9548a1b9b6aSSam Leffler 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
9558a1b9b6aSSam Leffler 	int error;
9568a1b9b6aSSam Leffler 
9578a1b9b6aSSam Leffler 	if (ireq->i_len < off)
9588a1b9b6aSSam Leffler 		return EINVAL;
9598a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
9608a1b9b6aSSam Leffler 	if (error != 0)
9618a1b9b6aSSam Leffler 		return error;
962acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
9638a1b9b6aSSam Leffler 	if (ni == NULL)
9648a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9658a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
9668a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
9678a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
9688a1b9b6aSSam Leffler 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
9698a1b9b6aSSam Leffler 			ireq->i_len - off);
9708a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9718a1b9b6aSSam Leffler 	return error;
9728a1b9b6aSSam Leffler }
9738a1b9b6aSSam Leffler 
9748a1b9b6aSSam Leffler static void
9758a1b9b6aSSam Leffler get_scan_result(struct ieee80211req_scan_result *sr,
9768a1b9b6aSSam Leffler 	const struct ieee80211_node *ni)
9778a1b9b6aSSam Leffler {
9788a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9798a1b9b6aSSam Leffler 
9808a1b9b6aSSam Leffler 	memset(sr, 0, sizeof(*sr));
9818a1b9b6aSSam Leffler 	sr->isr_ssid_len = ni->ni_esslen;
9828a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
9838a1b9b6aSSam Leffler 		sr->isr_ie_len += 2+ni->ni_wpa_ie[1];
9848a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
9858a1b9b6aSSam Leffler 		sr->isr_ie_len += 2+ni->ni_wme_ie[1];
9868a1b9b6aSSam Leffler 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
9878a1b9b6aSSam Leffler 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
9888a1b9b6aSSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
9898a1b9b6aSSam Leffler 		sr->isr_freq = ni->ni_chan->ic_freq;
9908a1b9b6aSSam Leffler 		sr->isr_flags = ni->ni_chan->ic_flags;
9918a1b9b6aSSam Leffler 	}
9928a1b9b6aSSam Leffler 	sr->isr_rssi = ic->ic_node_getrssi(ni);
9938a1b9b6aSSam Leffler 	sr->isr_intval = ni->ni_intval;
9948a1b9b6aSSam Leffler 	sr->isr_capinfo = ni->ni_capinfo;
9958a1b9b6aSSam Leffler 	sr->isr_erp = ni->ni_erp;
9968a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
9978a1b9b6aSSam Leffler 	sr->isr_nrates = ni->ni_rates.rs_nrates;
9988a1b9b6aSSam Leffler 	if (sr->isr_nrates > 15)
9998a1b9b6aSSam Leffler 		sr->isr_nrates = 15;
10008a1b9b6aSSam Leffler 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
10018a1b9b6aSSam Leffler }
10028a1b9b6aSSam Leffler 
10038a1b9b6aSSam Leffler static int
10048a1b9b6aSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
10058a1b9b6aSSam Leffler {
10068a1b9b6aSSam Leffler 	union {
10078a1b9b6aSSam Leffler 		struct ieee80211req_scan_result res;
10088a1b9b6aSSam Leffler 		char data[512];		/* XXX shrink? */
10098a1b9b6aSSam Leffler 	} u;
10108a1b9b6aSSam Leffler 	struct ieee80211req_scan_result *sr = &u.res;
10118a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10128a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10138a1b9b6aSSam Leffler 	int error, space;
10148a1b9b6aSSam Leffler 	u_int8_t *p, *cp;
10158a1b9b6aSSam Leffler 
10168a1b9b6aSSam Leffler 	p = ireq->i_data;
10178a1b9b6aSSam Leffler 	space = ireq->i_len;
10188a1b9b6aSSam Leffler 	error = 0;
10198a1b9b6aSSam Leffler 	/* XXX locking */
10208a1b9b6aSSam Leffler 	nt =  &ic->ic_scan;
10218a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
10228a1b9b6aSSam Leffler 		/* NB: skip pre-scan node state */
10238a1b9b6aSSam Leffler 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
10248a1b9b6aSSam Leffler 			continue;
10258a1b9b6aSSam Leffler 		get_scan_result(sr, ni);
10268a1b9b6aSSam Leffler 		if (sr->isr_len > sizeof(u))
10278a1b9b6aSSam Leffler 			continue;		/* XXX */
10288a1b9b6aSSam Leffler 		if (space < sr->isr_len)
10298a1b9b6aSSam Leffler 			break;
10308a1b9b6aSSam Leffler 		cp = (u_int8_t *)(sr+1);
10318a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
10328a1b9b6aSSam Leffler 		cp += ni->ni_esslen;
10338a1b9b6aSSam Leffler 		if (ni->ni_wpa_ie != NULL) {
10348a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
10358a1b9b6aSSam Leffler 			cp += 2+ni->ni_wpa_ie[1];
10368a1b9b6aSSam Leffler 		}
10378a1b9b6aSSam Leffler 		if (ni->ni_wme_ie != NULL) {
10388a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
10398a1b9b6aSSam Leffler 			cp += 2+ni->ni_wme_ie[1];
10408a1b9b6aSSam Leffler 		}
10418a1b9b6aSSam Leffler 		error = copyout(sr, p, sr->isr_len);
10428a1b9b6aSSam Leffler 		if (error)
10438a1b9b6aSSam Leffler 			break;
10448a1b9b6aSSam Leffler 		p += sr->isr_len;
10458a1b9b6aSSam Leffler 		space -= sr->isr_len;
10468a1b9b6aSSam Leffler 	}
10478a1b9b6aSSam Leffler 	ireq->i_len -= space;
10488a1b9b6aSSam Leffler 	return error;
10498a1b9b6aSSam Leffler }
10508a1b9b6aSSam Leffler 
10512cab1d3dSSam Leffler struct stainforeq {
10522cab1d3dSSam Leffler 	struct ieee80211com *ic;
10532cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
10542cab1d3dSSam Leffler 	size_t	space;
10552cab1d3dSSam Leffler };
10568a1b9b6aSSam Leffler 
10572cab1d3dSSam Leffler static size_t
10582cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
10592cab1d3dSSam Leffler {
10602cab1d3dSSam Leffler 	*ielen = 0;
10618a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
10622cab1d3dSSam Leffler 		*ielen += 2+ni->ni_wpa_ie[1];
10638a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
10642cab1d3dSSam Leffler 		*ielen += 2+ni->ni_wme_ie[1];
10652cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
10662cab1d3dSSam Leffler 		      sizeof(u_int32_t));
10672cab1d3dSSam Leffler }
10682cab1d3dSSam Leffler 
10692cab1d3dSSam Leffler static void
10702cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
10712cab1d3dSSam Leffler {
10722cab1d3dSSam Leffler 	struct stainforeq *req = arg;
10732cab1d3dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10742cab1d3dSSam Leffler 	size_t ielen;
10752cab1d3dSSam Leffler 
10762cab1d3dSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
10772cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
10782cab1d3dSSam Leffler 		return;
10792cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
10802cab1d3dSSam Leffler }
10812cab1d3dSSam Leffler 
10822cab1d3dSSam Leffler static void
10832cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
10842cab1d3dSSam Leffler {
10852cab1d3dSSam Leffler 	struct stainforeq *req = arg;
10862cab1d3dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10872cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
10882cab1d3dSSam Leffler 	size_t ielen, len;
10892cab1d3dSSam Leffler 	u_int8_t *cp;
10902cab1d3dSSam Leffler 
10912cab1d3dSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
10922cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
10932cab1d3dSSam Leffler 		return;
10942cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
10952cab1d3dSSam Leffler 		return;
10962cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
10972cab1d3dSSam Leffler 	if (len > req->space)
10982cab1d3dSSam Leffler 		return;
10992cab1d3dSSam Leffler 	si = req->si;
11002cab1d3dSSam Leffler 	si->isi_len = len;
11012cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
11028a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
11038a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
11048a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
11058a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
11068a1b9b6aSSam Leffler 	si->isi_rssi = ic->ic_node_getrssi(ni);
11078a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
11088a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
11098a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
11108a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
11118a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
11128a1b9b6aSSam Leffler 		si->isi_nrates = 15;
11138a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
11148a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
11158a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
11168a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
11178a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
11188a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
11198a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
11208a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
11218a1b9b6aSSam Leffler 	} else {
11228a1b9b6aSSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[0];
11238a1b9b6aSSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
11248a1b9b6aSSam Leffler 	}
11252cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
11262cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
11278a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_run;
11282cab1d3dSSam Leffler 	else if (ni->ni_associd != 0)
11298a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_auth;
11308a1b9b6aSSam Leffler 	else
11318a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_init;
11328a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
11338a1b9b6aSSam Leffler 
11348a1b9b6aSSam Leffler 	cp = (u_int8_t *)(si+1);
11358a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL) {
11368a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
11378a1b9b6aSSam Leffler 		cp += 2+ni->ni_wpa_ie[1];
11388a1b9b6aSSam Leffler 	}
11398a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL) {
11408a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
11418a1b9b6aSSam Leffler 		cp += 2+ni->ni_wme_ie[1];
11428a1b9b6aSSam Leffler 	}
11432cab1d3dSSam Leffler 
11442cab1d3dSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((u_int8_t *)si) + len);
11452cab1d3dSSam Leffler 	req->space -= len;
11468a1b9b6aSSam Leffler }
11472cab1d3dSSam Leffler 
11482cab1d3dSSam Leffler static int
11492cab1d3dSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
11502cab1d3dSSam Leffler {
11512cab1d3dSSam Leffler 	struct stainforeq req;
11522cab1d3dSSam Leffler 	int error;
11532cab1d3dSSam Leffler 
11542cab1d3dSSam Leffler 	if (ireq->i_len < sizeof(struct stainforeq))
11552cab1d3dSSam Leffler 		return EFAULT;
11562cab1d3dSSam Leffler 
11572cab1d3dSSam Leffler 	error = 0;
11582cab1d3dSSam Leffler 	req.space = 0;
11592cab1d3dSSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
11602cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
11612cab1d3dSSam Leffler 		req.space = ireq->i_len;
11622cab1d3dSSam Leffler 	if (req.space > 0) {
11632cab1d3dSSam Leffler 		size_t space;
11642cab1d3dSSam Leffler 		void *p;
11652cab1d3dSSam Leffler 
11662cab1d3dSSam Leffler 		space = req.space;
11672cab1d3dSSam Leffler 		/* XXX M_WAITOK after driver lock released */
11682cab1d3dSSam Leffler 		MALLOC(p, void *, space, M_TEMP, M_NOWAIT);
11692cab1d3dSSam Leffler 		if (p == NULL)
11702cab1d3dSSam Leffler 			return ENOMEM;
11712cab1d3dSSam Leffler 		req.si = p;
11722cab1d3dSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
11732cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
11742cab1d3dSSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
11752cab1d3dSSam Leffler 		FREE(p, M_TEMP);
11762cab1d3dSSam Leffler 	} else
11772cab1d3dSSam Leffler 		ireq->i_len = 0;
11782cab1d3dSSam Leffler 
11798a1b9b6aSSam Leffler 	return error;
11808a1b9b6aSSam Leffler }
11818a1b9b6aSSam Leffler 
11828a1b9b6aSSam Leffler static int
11838a1b9b6aSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
11848a1b9b6aSSam Leffler {
11858a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11868a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
11878a1b9b6aSSam Leffler 	int error;
11888a1b9b6aSSam Leffler 
11898a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
11908a1b9b6aSSam Leffler 		return EINVAL;
11918a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
11928a1b9b6aSSam Leffler 	if (error != 0)
11938a1b9b6aSSam Leffler 		return error;
1194acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
11958a1b9b6aSSam Leffler 	if (ni == NULL)
11968a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
11978a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
11988a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
11998a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
12008a1b9b6aSSam Leffler 	return error;
12018a1b9b6aSSam Leffler }
12028a1b9b6aSSam Leffler 
12038a1b9b6aSSam Leffler static int
12048a1b9b6aSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
12058a1b9b6aSSam Leffler {
12068a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
12078a1b9b6aSSam Leffler 	struct wmeParams *wmep;
12088a1b9b6aSSam Leffler 	int ac;
12098a1b9b6aSSam Leffler 
12108a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
12118a1b9b6aSSam Leffler 		return EINVAL;
12128a1b9b6aSSam Leffler 
12138a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
12148a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
12158a1b9b6aSSam Leffler 		ac = WME_AC_BE;
12168a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
12178a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
12188a1b9b6aSSam Leffler 	else
12198a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
12208a1b9b6aSSam Leffler 	switch (ireq->i_type) {
12218a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
12228a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
12238a1b9b6aSSam Leffler 		break;
12248a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
12258a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
12268a1b9b6aSSam Leffler 		break;
12278a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
12288a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
12298a1b9b6aSSam Leffler 		break;
12308a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
12318a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
12328a1b9b6aSSam Leffler 		break;
12338a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
12348a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
12358a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
12368a1b9b6aSSam Leffler 		break;
12378a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
12388a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
12398a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
12408a1b9b6aSSam Leffler 		break;
12418a1b9b6aSSam Leffler 	}
12428a1b9b6aSSam Leffler 	return 0;
12438a1b9b6aSSam Leffler }
12448a1b9b6aSSam Leffler 
1245c788ca3eSBill Paul /*
1246c788ca3eSBill Paul  * When building the kernel with -O2 on the i386 architecture, gcc
1247c788ca3eSBill Paul  * seems to want to inline this function into ieee80211_ioctl()
1248c788ca3eSBill Paul  * (which is the only routine that calls it). When this happens,
1249c788ca3eSBill Paul  * ieee80211_ioctl() ends up consuming an additional 2K of stack
1250c788ca3eSBill Paul  * space. (Exactly why it needs so much is unclear.) The problem
1251c788ca3eSBill Paul  * is that it's possible for ieee80211_ioctl() to invoke other
1252c788ca3eSBill Paul  * routines (including driver init functions) which could then find
1253c788ca3eSBill Paul  * themselves perilously close to exhausting the stack.
1254c788ca3eSBill Paul  *
1255c788ca3eSBill Paul  * To avoid this, we deliberately prevent gcc from inlining this
1256c788ca3eSBill Paul  * routine. Another way to avoid this is to use less agressive
1257c788ca3eSBill Paul  * optimization when compiling this file (i.e. -O instead of -O2)
1258c788ca3eSBill Paul  * but special-casing the compilation of this one module in the
1259c788ca3eSBill Paul  * build system would be awkward.
1260c788ca3eSBill Paul  */
1261c788ca3eSBill Paul #ifdef __GNUC__
1262c788ca3eSBill Paul __attribute__ ((noinline))
1263c788ca3eSBill Paul #endif
12648a1b9b6aSSam Leffler static int
12658a1b9b6aSSam Leffler ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
12668a1b9b6aSSam Leffler {
12678a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
12681a1e1d21SSam Leffler 	int error = 0;
12698a1b9b6aSSam Leffler 	u_int kid, len, m;
12701a1e1d21SSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
12711a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
12721a1e1d21SSam Leffler 
12731a1e1d21SSam Leffler 	switch (ireq->i_type) {
12741a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
12751a1e1d21SSam Leffler 		switch (ic->ic_state) {
12761a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
12771a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
12781a1e1d21SSam Leffler 			ireq->i_len = ic->ic_des_esslen;
12791a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
12801a1e1d21SSam Leffler 			break;
12811a1e1d21SSam Leffler 		default:
12821a1e1d21SSam Leffler 			ireq->i_len = ic->ic_bss->ni_esslen;
12831a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_bss->ni_essid,
12841a1e1d21SSam Leffler 				ireq->i_len);
12851a1e1d21SSam Leffler 			break;
12861a1e1d21SSam Leffler 		}
12871a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
12881a1e1d21SSam Leffler 		break;
12891a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
12901a1e1d21SSam Leffler 		ireq->i_val = 1;
12911a1e1d21SSam Leffler 		break;
12921a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
12938a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
12948a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
12958a1b9b6aSSam Leffler 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
12968a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
12978a1b9b6aSSam Leffler 		else
12988a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
12991a1e1d21SSam Leffler 		break;
13001a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
13011a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
13028a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
13038a1b9b6aSSam Leffler 			return EINVAL;
13048a1b9b6aSSam Leffler 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
13051a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
13065c8bb90bSBrian Feldman 		if (suser(curthread) == 0) {
13071a1e1d21SSam Leffler 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
13081a1e1d21SSam Leffler 		} else {
13091a1e1d21SSam Leffler 			bzero(tmpkey, len);
13101a1e1d21SSam Leffler 		}
13111a1e1d21SSam Leffler 		ireq->i_len = len;
13121a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
13131a1e1d21SSam Leffler 		break;
13141a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
13151a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
13161a1e1d21SSam Leffler 		break;
13171a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
13188a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_def_txkey;
13191a1e1d21SSam Leffler 		break;
13201a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
13218a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
13228a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
13238a1b9b6aSSam Leffler 		else
13248a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_bss->ni_authmode;
13251a1e1d21SSam Leffler 		break;
13261a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
1327b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
13281a1e1d21SSam Leffler 		break;
13291a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
13301a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
13311a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
13321a1e1d21SSam Leffler 		else
13331a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
13341a1e1d21SSam Leffler 		break;
13351a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
13361a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
13371a1e1d21SSam Leffler 		break;
133813604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
13391a1e1d21SSam Leffler 		ireq->i_val = ic->ic_rtsthreshold;
13401a1e1d21SSam Leffler 		break;
13412e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
13422e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
13432e79ca97SSam Leffler 		break;
13442e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
13452e79ca97SSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
13468a1b9b6aSSam Leffler 			return EINVAL;
13478a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_txpowlimit;
13488a1b9b6aSSam Leffler 		break;
13498a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
13508a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastcipher;
13518a1b9b6aSSam Leffler 		break;
13528a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
13538a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastkeylen;
13548a1b9b6aSSam Leffler 		break;
13558a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
13568a1b9b6aSSam Leffler 		ireq->i_val = 0;
13578a1b9b6aSSam Leffler 		for (m = 0x1; m != 0; m <<= 1)
13588a1b9b6aSSam Leffler 			if (rsn->rsn_ucastcipherset & m)
13598a1b9b6aSSam Leffler 				ireq->i_val |= 1<<cap2cipher(m);
13608a1b9b6aSSam Leffler 		break;
13618a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
13628a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastcipher;
13638a1b9b6aSSam Leffler 		break;
13648a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
13658a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastkeylen;
13668a1b9b6aSSam Leffler 		break;
13678a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
13688a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_keymgmtset;
13698a1b9b6aSSam Leffler 		break;
13708a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
13718a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_caps;
13728a1b9b6aSSam Leffler 		break;
13738a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
13748a1b9b6aSSam Leffler 		switch (ic->ic_flags & IEEE80211_F_WPA) {
13758a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
13768a1b9b6aSSam Leffler 			ireq->i_val = 1;
13778a1b9b6aSSam Leffler 			break;
13788a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
13798a1b9b6aSSam Leffler 			ireq->i_val = 2;
13808a1b9b6aSSam Leffler 			break;
13818a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
13828a1b9b6aSSam Leffler 			ireq->i_val = 3;
13838a1b9b6aSSam Leffler 			break;
13848a1b9b6aSSam Leffler 		default:
13858a1b9b6aSSam Leffler 			ireq->i_val = 0;
13868a1b9b6aSSam Leffler 			break;
13878a1b9b6aSSam Leffler 		}
13888a1b9b6aSSam Leffler 		break;
13898a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
13908a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchanlist(ic, ireq);
13918a1b9b6aSSam Leffler 		break;
13928a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
13938a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_roaming;
13948a1b9b6aSSam Leffler 		break;
13958a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
13968a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
13978a1b9b6aSSam Leffler 		break;
13988a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
13998a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
14008a1b9b6aSSam Leffler 		break;
14018a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
14028a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
14038a1b9b6aSSam Leffler 		break;
14048a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
14058a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_caps>>16;
14068a1b9b6aSSam Leffler 		ireq->i_len = ic->ic_caps&0xffff;
14078a1b9b6aSSam Leffler 		break;
14088a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
14098a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
14108a1b9b6aSSam Leffler 		break;
14118a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
14128a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
14138a1b9b6aSSam Leffler 		break;
14148a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
14158a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
14168a1b9b6aSSam Leffler 		break;
14178a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
14188a1b9b6aSSam Leffler 		if (ic->ic_opt_ie == NULL)
14198a1b9b6aSSam Leffler 			return EINVAL;
14208a1b9b6aSSam Leffler 		/* NB: truncate, caller can check length */
14218a1b9b6aSSam Leffler 		if (ireq->i_len > ic->ic_opt_ie_len)
14228a1b9b6aSSam Leffler 			ireq->i_len = ic->ic_opt_ie_len;
14238a1b9b6aSSam Leffler 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
14248a1b9b6aSSam Leffler 		break;
14258a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
14268a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getkey(ic, ireq);
14278a1b9b6aSSam Leffler 		break;
14288a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
14298a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
14308a1b9b6aSSam Leffler 		break;
14318a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
14328a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
14338a1b9b6aSSam Leffler 			return EINVAL;
14348a1b9b6aSSam Leffler 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
14358a1b9b6aSSam Leffler 					ic->ic_bss->ni_bssid :
14368a1b9b6aSSam Leffler 					ic->ic_des_bssid,
14378a1b9b6aSSam Leffler 				ireq->i_data, ireq->i_len);
14388a1b9b6aSSam Leffler 		break;
14398a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
14408a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwpaie(ic, ireq);
14418a1b9b6aSSam Leffler 		break;
14428a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
14438a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getscanresults(ic, ireq);
14448a1b9b6aSSam Leffler 		break;
14458a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
14468a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstastats(ic, ireq);
14478a1b9b6aSSam Leffler 		break;
14488a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
14498a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_txpower;
14508a1b9b6aSSam Leffler 		break;
14518a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
14528a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
14538a1b9b6aSSam Leffler 		break;
14548a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
14558a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstainfo(ic, ireq);
14568a1b9b6aSSam Leffler 		break;
14578a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
14588a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
14598a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
14608a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
14618a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
14628a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
14638a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
14648a1b9b6aSSam Leffler 		break;
14658a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
14668a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_dtim_period;
14678a1b9b6aSSam Leffler 		break;
14688a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
14698a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
14708a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_intval;
14712e79ca97SSam Leffler 		break;
1472c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
1473c4f040c3SSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
1474c4f040c3SSam Leffler 		break;
147570231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
147670231e3dSSam Leffler 		ireq->i_val = ic->ic_fragthreshold;
147770231e3dSSam Leffler 		break;
14781a1e1d21SSam Leffler 	default:
14791a1e1d21SSam Leffler 		error = EINVAL;
1480b2e95691SSam Leffler 		break;
14811a1e1d21SSam Leffler 	}
14828a1b9b6aSSam Leffler 	return error;
14838a1b9b6aSSam Leffler }
14848a1b9b6aSSam Leffler 
14858a1b9b6aSSam Leffler static int
14868a1b9b6aSSam Leffler ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
14878a1b9b6aSSam Leffler {
14888a1b9b6aSSam Leffler 	int error;
14898a1b9b6aSSam Leffler 	void *ie;
14908a1b9b6aSSam Leffler 
14918a1b9b6aSSam Leffler 	/*
14928a1b9b6aSSam Leffler 	 * NB: Doing this for ap operation could be useful (e.g. for
14938a1b9b6aSSam Leffler 	 *     WPA and/or WME) except that it typically is worthless
14948a1b9b6aSSam Leffler 	 *     without being able to intervene when processing
14958a1b9b6aSSam Leffler 	 *     association response frames--so disallow it for now.
14968a1b9b6aSSam Leffler 	 */
14978a1b9b6aSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
14988a1b9b6aSSam Leffler 		return EINVAL;
14998a1b9b6aSSam Leffler 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
15008a1b9b6aSSam Leffler 		return EINVAL;
15018a1b9b6aSSam Leffler 	/* NB: data.length is validated by the wireless extensions code */
15028a1b9b6aSSam Leffler 	MALLOC(ie, void *, ireq->i_len, M_DEVBUF, M_WAITOK);
15038a1b9b6aSSam Leffler 	if (ie == NULL)
15048a1b9b6aSSam Leffler 		return ENOMEM;
15058a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, ie, ireq->i_len);
15068a1b9b6aSSam Leffler 	/* XXX sanity check data? */
15078a1b9b6aSSam Leffler 	if (ic->ic_opt_ie != NULL)
15088a1b9b6aSSam Leffler 		FREE(ic->ic_opt_ie, M_DEVBUF);
15098a1b9b6aSSam Leffler 	ic->ic_opt_ie = ie;
15108a1b9b6aSSam Leffler 	ic->ic_opt_ie_len = ireq->i_len;
15118a1b9b6aSSam Leffler 	return 0;
15128a1b9b6aSSam Leffler }
15138a1b9b6aSSam Leffler 
15148a1b9b6aSSam Leffler static int
15158a1b9b6aSSam Leffler ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
15168a1b9b6aSSam Leffler {
15178a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
15188a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
15198a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
15208a1b9b6aSSam Leffler 	u_int16_t kid;
15218a1b9b6aSSam Leffler 	int error;
15228a1b9b6aSSam Leffler 
15238a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
15248a1b9b6aSSam Leffler 		return EINVAL;
15258a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
15261a1e1d21SSam Leffler 	if (error)
15278a1b9b6aSSam Leffler 		return error;
15288a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
15298a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
15308a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
15318a1b9b6aSSam Leffler 		return E2BIG;
15328a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
15338a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
15348a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
15358a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
15368a1b9b6aSSam Leffler 			return EINVAL;
15378a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1538b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1539b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1540b5d4660fSSam Leffler 				ieee80211_free_node(ni);
15418a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1542b5d4660fSSam Leffler 			}
15438a1b9b6aSSam Leffler 		} else {
1544acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
15458a1b9b6aSSam Leffler 			if (ni == NULL)
15468a1b9b6aSSam Leffler 				return ENOENT;
15478a1b9b6aSSam Leffler 		}
15488a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
15498a1b9b6aSSam Leffler 	} else {
15508a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
15518a1b9b6aSSam Leffler 			return EINVAL;
15528a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
15538a1b9b6aSSam Leffler 		ni = NULL;
15548a1b9b6aSSam Leffler 	}
15558a1b9b6aSSam Leffler 	error = 0;
15568a1b9b6aSSam Leffler 	ieee80211_key_update_begin(ic);
1557dd70e17bSSam Leffler 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
15588a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
15598a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
15608a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
15618a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
15628a1b9b6aSSam Leffler 		wk->wk_keyrsc = ik.ik_keyrsc;
15638a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
15648a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
15658a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
15668a1b9b6aSSam Leffler 		if (!ieee80211_crypto_setkey(ic, wk,
15678a1b9b6aSSam Leffler 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
15688a1b9b6aSSam Leffler 			error = EIO;
15698a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
15708a1b9b6aSSam Leffler 			ic->ic_def_txkey = kid;
15718a1b9b6aSSam Leffler 	} else
15728a1b9b6aSSam Leffler 		error = ENXIO;
15738a1b9b6aSSam Leffler 	ieee80211_key_update_end(ic);
15748a1b9b6aSSam Leffler 	if (ni != NULL)
15758a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
15768a1b9b6aSSam Leffler 	return error;
15778a1b9b6aSSam Leffler }
15788a1b9b6aSSam Leffler 
15798a1b9b6aSSam Leffler static int
15808a1b9b6aSSam Leffler ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
15818a1b9b6aSSam Leffler {
15828a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
15838a1b9b6aSSam Leffler 	int kid, error;
15848a1b9b6aSSam Leffler 
15858a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
15868a1b9b6aSSam Leffler 		return EINVAL;
15878a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
15888a1b9b6aSSam Leffler 	if (error)
15898a1b9b6aSSam Leffler 		return error;
15908a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
15918a1b9b6aSSam Leffler 	/* XXX u_int8_t -> u_int16_t */
15928a1b9b6aSSam Leffler 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
15938a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
15948a1b9b6aSSam Leffler 
1595b5d4660fSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1596b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1597b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1598b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1599b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1600b5d4660fSSam Leffler 			}
1601b5d4660fSSam Leffler 		} else {
1602acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
16038a1b9b6aSSam Leffler 			if (ni == NULL)
1604b5d4660fSSam Leffler 				return ENOENT;
1605b5d4660fSSam Leffler 		}
16068a1b9b6aSSam Leffler 		/* XXX error return */
1607c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
16088a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
16098a1b9b6aSSam Leffler 	} else {
16108a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
16118a1b9b6aSSam Leffler 			return EINVAL;
16128a1b9b6aSSam Leffler 		/* XXX error return */
16138a1b9b6aSSam Leffler 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
16148a1b9b6aSSam Leffler 	}
16158a1b9b6aSSam Leffler 	return 0;
16168a1b9b6aSSam Leffler }
16178a1b9b6aSSam Leffler 
16188a1b9b6aSSam Leffler static void
16198a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
16208a1b9b6aSSam Leffler {
16218a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
16228a1b9b6aSSam Leffler 	struct ieee80211req_mlme *mlme = arg;
16238a1b9b6aSSam Leffler 
16248a1b9b6aSSam Leffler 	if (ni->ni_associd != 0) {
16258a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
16268a1b9b6aSSam Leffler 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
16278a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH :
16288a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DISASSOC,
16298a1b9b6aSSam Leffler 			mlme->im_reason);
16308a1b9b6aSSam Leffler 	}
16318a1b9b6aSSam Leffler 	ieee80211_node_leave(ic, ni);
16328a1b9b6aSSam Leffler }
16338a1b9b6aSSam Leffler 
16348a1b9b6aSSam Leffler static int
16358a1b9b6aSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
16368a1b9b6aSSam Leffler {
16378a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
16388a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16398a1b9b6aSSam Leffler 	int error;
16408a1b9b6aSSam Leffler 
16418a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
16428a1b9b6aSSam Leffler 		return EINVAL;
16438a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
16448a1b9b6aSSam Leffler 	if (error)
16458a1b9b6aSSam Leffler 		return error;
16468a1b9b6aSSam Leffler 	switch (mlme.im_op) {
16478a1b9b6aSSam Leffler 	case IEEE80211_MLME_ASSOC:
16488a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
16498a1b9b6aSSam Leffler 			return EINVAL;
16508a1b9b6aSSam Leffler 		/* XXX must be in S_SCAN state? */
16518a1b9b6aSSam Leffler 
1652f02a0bd2SSam Leffler 		if (mlme.im_ssid_len != 0) {
16538a1b9b6aSSam Leffler 			/*
16548a1b9b6aSSam Leffler 			 * Desired ssid specified; must match both bssid and
16558a1b9b6aSSam Leffler 			 * ssid to distinguish ap advertising multiple ssid's.
16568a1b9b6aSSam Leffler 			 */
16578a1b9b6aSSam Leffler 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
16588a1b9b6aSSam Leffler 				mlme.im_macaddr,
1659f02a0bd2SSam Leffler 				mlme.im_ssid_len, mlme.im_ssid);
16608a1b9b6aSSam Leffler 		} else {
16618a1b9b6aSSam Leffler 			/*
16628a1b9b6aSSam Leffler 			 * Normal case; just match bssid.
16638a1b9b6aSSam Leffler 			 */
16648a1b9b6aSSam Leffler 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
16658a1b9b6aSSam Leffler 		}
16668a1b9b6aSSam Leffler 		if (ni == NULL)
16678a1b9b6aSSam Leffler 			return EINVAL;
16688a1b9b6aSSam Leffler 		if (!ieee80211_sta_join(ic, ni)) {
16698a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
16708a1b9b6aSSam Leffler 			return EINVAL;
16718a1b9b6aSSam Leffler 		}
16721a1e1d21SSam Leffler 		break;
16738a1b9b6aSSam Leffler 	case IEEE80211_MLME_DISASSOC:
16748a1b9b6aSSam Leffler 	case IEEE80211_MLME_DEAUTH:
16758a1b9b6aSSam Leffler 		switch (ic->ic_opmode) {
16768a1b9b6aSSam Leffler 		case IEEE80211_M_STA:
16778a1b9b6aSSam Leffler 			/* XXX not quite right */
16788a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_INIT,
16798a1b9b6aSSam Leffler 				mlme.im_reason);
16808a1b9b6aSSam Leffler 			break;
16818a1b9b6aSSam Leffler 		case IEEE80211_M_HOSTAP:
16828a1b9b6aSSam Leffler 			/* NB: the broadcast address means do 'em all */
16838a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
1684acc4f7f5SSam Leffler 				if ((ni = ieee80211_find_node(&ic->ic_sta,
16858a1b9b6aSSam Leffler 						mlme.im_macaddr)) == NULL)
16868a1b9b6aSSam Leffler 					return EINVAL;
16878a1b9b6aSSam Leffler 				domlme(&mlme, ni);
16888a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
16898a1b9b6aSSam Leffler 			} else {
1690acc4f7f5SSam Leffler 				ieee80211_iterate_nodes(&ic->ic_sta,
16918a1b9b6aSSam Leffler 						domlme, &mlme);
16928a1b9b6aSSam Leffler 			}
16938a1b9b6aSSam Leffler 			break;
16948a1b9b6aSSam Leffler 		default:
16958a1b9b6aSSam Leffler 			return EINVAL;
16968a1b9b6aSSam Leffler 		}
16978a1b9b6aSSam Leffler 		break;
16988a1b9b6aSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
16998a1b9b6aSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
17008a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
17018a1b9b6aSSam Leffler 			return EINVAL;
1702acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
17038a1b9b6aSSam Leffler 		if (ni == NULL)
17048a1b9b6aSSam Leffler 			return EINVAL;
17058a1b9b6aSSam Leffler 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
1706e4918ecdSSam Leffler 			ieee80211_node_authorize(ni);
17078a1b9b6aSSam Leffler 		else
1708e4918ecdSSam Leffler 			ieee80211_node_unauthorize(ni);
17098a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
17108a1b9b6aSSam Leffler 		break;
17118a1b9b6aSSam Leffler 	default:
17128a1b9b6aSSam Leffler 		return EINVAL;
17138a1b9b6aSSam Leffler 	}
17148a1b9b6aSSam Leffler 	return 0;
17158a1b9b6aSSam Leffler }
17168a1b9b6aSSam Leffler 
17178a1b9b6aSSam Leffler static int
17188a1b9b6aSSam Leffler ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
17198a1b9b6aSSam Leffler {
17208a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN];
17218a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
17228a1b9b6aSSam Leffler 	int error;
17238a1b9b6aSSam Leffler 
17248a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
17258a1b9b6aSSam Leffler 		return EINVAL;
17268a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
17278a1b9b6aSSam Leffler 	if (error)
17288a1b9b6aSSam Leffler 		return error;
17298a1b9b6aSSam Leffler 	if (acl == NULL) {
17308a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
17318a1b9b6aSSam Leffler 		if (acl == NULL || !acl->iac_attach(ic))
17328a1b9b6aSSam Leffler 			return EINVAL;
17338a1b9b6aSSam Leffler 		ic->ic_acl = acl;
17348a1b9b6aSSam Leffler 	}
17358a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
17368a1b9b6aSSam Leffler 		acl->iac_add(ic, mac);
17378a1b9b6aSSam Leffler 	else
17388a1b9b6aSSam Leffler 		acl->iac_remove(ic, mac);
17398a1b9b6aSSam Leffler 	return 0;
17408a1b9b6aSSam Leffler }
17418a1b9b6aSSam Leffler 
17428a1b9b6aSSam Leffler static int
17438a1b9b6aSSam Leffler ieee80211_ioctl_maccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
17448a1b9b6aSSam Leffler {
17458a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
17468a1b9b6aSSam Leffler 
17478a1b9b6aSSam Leffler 	switch (ireq->i_val) {
17488a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
17498a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
17508a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
17518a1b9b6aSSam Leffler 		if (acl == NULL) {
17528a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
17538a1b9b6aSSam Leffler 			if (acl == NULL || !acl->iac_attach(ic))
17548a1b9b6aSSam Leffler 				return EINVAL;
17558a1b9b6aSSam Leffler 			ic->ic_acl = acl;
17568a1b9b6aSSam Leffler 		}
17578a1b9b6aSSam Leffler 		acl->iac_setpolicy(ic, ireq->i_val);
17588a1b9b6aSSam Leffler 		break;
17598a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
17608a1b9b6aSSam Leffler 		if (acl != NULL)
17618a1b9b6aSSam Leffler 			acl->iac_flush(ic);
17628a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
17638a1b9b6aSSam Leffler 		break;
17648a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
17658a1b9b6aSSam Leffler 		if (acl != NULL) {
17668a1b9b6aSSam Leffler 			ic->ic_acl = NULL;
17678a1b9b6aSSam Leffler 			acl->iac_detach(ic);
17688a1b9b6aSSam Leffler 		}
17698a1b9b6aSSam Leffler 		break;
17708a1b9b6aSSam Leffler 	default:
17718a1b9b6aSSam Leffler 		return EINVAL;
17728a1b9b6aSSam Leffler 	}
17738a1b9b6aSSam Leffler 	return 0;
17748a1b9b6aSSam Leffler }
17758a1b9b6aSSam Leffler 
17768a1b9b6aSSam Leffler static int
17778a1b9b6aSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
17788a1b9b6aSSam Leffler {
17798a1b9b6aSSam Leffler 	struct ieee80211req_chanlist list;
17808a1b9b6aSSam Leffler 	u_char chanlist[IEEE80211_CHAN_BYTES];
17818a1b9b6aSSam Leffler 	int i, j, error;
17828a1b9b6aSSam Leffler 
17838a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(list))
17848a1b9b6aSSam Leffler 		return EINVAL;
17858a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &list, sizeof(list));
17868a1b9b6aSSam Leffler 	if (error)
17878a1b9b6aSSam Leffler 		return error;
17888a1b9b6aSSam Leffler 	memset(chanlist, 0, sizeof(chanlist));
17898a1b9b6aSSam Leffler 	/*
17908a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
17918a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
17928a1b9b6aSSam Leffler 	 */
17938a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
17948a1b9b6aSSam Leffler 		i = 1;
17958a1b9b6aSSam Leffler 	else
17968a1b9b6aSSam Leffler 		i = 0;
17978a1b9b6aSSam Leffler 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
17988a1b9b6aSSam Leffler 		/*
17998a1b9b6aSSam Leffler 		 * NB: silently discard unavailable channels so users
18008a1b9b6aSSam Leffler 		 *     can specify 1-255 to get all available channels.
18018a1b9b6aSSam Leffler 		 */
18028a1b9b6aSSam Leffler 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
18038a1b9b6aSSam Leffler 			setbit(chanlist, i);
18048a1b9b6aSSam Leffler 	}
18058a1b9b6aSSam Leffler 	if (ic->ic_ibss_chan == NULL ||
18068a1b9b6aSSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
18078a1b9b6aSSam Leffler 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
18088a1b9b6aSSam Leffler 			if (isset(chanlist, i)) {
18098a1b9b6aSSam Leffler 				ic->ic_ibss_chan = &ic->ic_channels[i];
18108a1b9b6aSSam Leffler 				goto found;
18118a1b9b6aSSam Leffler 			}
18128a1b9b6aSSam Leffler 		return EINVAL;			/* no active channels */
18138a1b9b6aSSam Leffler found:
18148a1b9b6aSSam Leffler 		;
18158a1b9b6aSSam Leffler 	}
18168a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
18178a1b9b6aSSam Leffler 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
18188a1b9b6aSSam Leffler }
18198a1b9b6aSSam Leffler 
18208a1b9b6aSSam Leffler static int
18218a1b9b6aSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
18228a1b9b6aSSam Leffler {
18238a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
18248a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
18258a1b9b6aSSam Leffler 	int error;
18268a1b9b6aSSam Leffler 
18278a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
18288a1b9b6aSSam Leffler 		return EINVAL;
18298a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
18308a1b9b6aSSam Leffler 	if (error != 0)
18318a1b9b6aSSam Leffler 		return error;
1832acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
18338a1b9b6aSSam Leffler 	if (ni == NULL)
18348a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
18358a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
18368a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
18378a1b9b6aSSam Leffler 	return error;
18388a1b9b6aSSam Leffler }
18398a1b9b6aSSam Leffler 
18408a1b9b6aSSam Leffler static int
18418a1b9b6aSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
18428a1b9b6aSSam Leffler {
18438a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
18448a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
18458a1b9b6aSSam Leffler 	int isbss, ac;
18468a1b9b6aSSam Leffler 
18478a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
18488a1b9b6aSSam Leffler 		return EINVAL;
18498a1b9b6aSSam Leffler 
18508a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
18518a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
18528a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
18538a1b9b6aSSam Leffler 		ac = WME_AC_BE;
18548a1b9b6aSSam Leffler 	if (isbss) {
18558a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
18568a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
18578a1b9b6aSSam Leffler 	} else {
18588a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
18598a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
18608a1b9b6aSSam Leffler 	}
18618a1b9b6aSSam Leffler 	switch (ireq->i_type) {
18628a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
18638a1b9b6aSSam Leffler 		if (isbss) {
18648a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = ireq->i_val;
18658a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18668a1b9b6aSSam Leffler 				chanp->wmep_logcwmin = ireq->i_val;
18678a1b9b6aSSam Leffler 		} else {
18688a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
18698a1b9b6aSSam Leffler 				ireq->i_val;
18708a1b9b6aSSam Leffler 		}
18718a1b9b6aSSam Leffler 		break;
18728a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
18738a1b9b6aSSam Leffler 		if (isbss) {
18748a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = ireq->i_val;
18758a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18768a1b9b6aSSam Leffler 				chanp->wmep_logcwmax = ireq->i_val;
18778a1b9b6aSSam Leffler 		} else {
18788a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
18798a1b9b6aSSam Leffler 				ireq->i_val;
18808a1b9b6aSSam Leffler 		}
18818a1b9b6aSSam Leffler 		break;
18828a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18838a1b9b6aSSam Leffler 		if (isbss) {
18848a1b9b6aSSam Leffler 			wmep->wmep_aifsn = ireq->i_val;
18858a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18868a1b9b6aSSam Leffler 				chanp->wmep_aifsn = ireq->i_val;
18878a1b9b6aSSam Leffler 		} else {
18888a1b9b6aSSam Leffler 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
18898a1b9b6aSSam Leffler 		}
18908a1b9b6aSSam Leffler 		break;
18918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18928a1b9b6aSSam Leffler 		if (isbss) {
18938a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = ireq->i_val;
18948a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18958a1b9b6aSSam Leffler 				chanp->wmep_txopLimit = ireq->i_val;
18968a1b9b6aSSam Leffler 		} else {
18978a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
18988a1b9b6aSSam Leffler 				ireq->i_val;
18998a1b9b6aSSam Leffler 		}
19008a1b9b6aSSam Leffler 		break;
19018a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
19028a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
19038a1b9b6aSSam Leffler 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
19048a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
19058a1b9b6aSSam Leffler 		break;
19068a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
19078a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
19088a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
19098a1b9b6aSSam Leffler 		break;
19108a1b9b6aSSam Leffler 	}
19118a1b9b6aSSam Leffler 	ieee80211_wme_updateparams(ic);
19128a1b9b6aSSam Leffler 	return 0;
19138a1b9b6aSSam Leffler }
19148a1b9b6aSSam Leffler 
19158a1b9b6aSSam Leffler static int
19168a1b9b6aSSam Leffler cipher2cap(int cipher)
19178a1b9b6aSSam Leffler {
19188a1b9b6aSSam Leffler 	switch (cipher) {
19198a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
19208a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
19218a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
19228a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
19238a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
19248a1b9b6aSSam Leffler 	}
19258a1b9b6aSSam Leffler 	return 0;
19268a1b9b6aSSam Leffler }
19278a1b9b6aSSam Leffler 
19288a1b9b6aSSam Leffler static int
19298a1b9b6aSSam Leffler ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
19308a1b9b6aSSam Leffler {
19318a1b9b6aSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
19328a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
19338a1b9b6aSSam Leffler 	int error;
19348a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
19358a1b9b6aSSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
19368a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
19378a1b9b6aSSam Leffler 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
19388a1b9b6aSSam Leffler 	struct ieee80211_key *k;
19398a1b9b6aSSam Leffler 	int j, caps;
19408a1b9b6aSSam Leffler 	u_int kid;
19418a1b9b6aSSam Leffler 
19428a1b9b6aSSam Leffler 	error = 0;
19431a1e1d21SSam Leffler 	switch (ireq->i_type) {
19441a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
19451a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
19468a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
19478a1b9b6aSSam Leffler 			return EINVAL;
19481a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
19491a1e1d21SSam Leffler 		if (error)
19501a1e1d21SSam Leffler 			break;
19511a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
19521a1e1d21SSam Leffler 		ic->ic_des_esslen = ireq->i_len;
19531a1e1d21SSam Leffler 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
19541a1e1d21SSam Leffler 		error = ENETRESET;
19551a1e1d21SSam Leffler 		break;
19561a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
19578a1b9b6aSSam Leffler 		switch (ireq->i_val) {
19588a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
19598a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
19608a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
19618a1b9b6aSSam Leffler 			break;
19628a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
19638a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
19648a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
19658a1b9b6aSSam Leffler 			break;
19668a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
19678a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
19688a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
19698a1b9b6aSSam Leffler 			break;
19701a1e1d21SSam Leffler 		}
19711a1e1d21SSam Leffler 		error = ENETRESET;
19721a1e1d21SSam Leffler 		break;
19731a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
19741a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
19758a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
19768a1b9b6aSSam Leffler 			return EINVAL;
19778a1b9b6aSSam Leffler 		k = &ic->ic_nw_keys[kid];
19788a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
19798a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
19808a1b9b6aSSam Leffler 			(void) ieee80211_crypto_delkey(ic, k);
19811a1e1d21SSam Leffler 			break;
19821a1e1d21SSam Leffler 		}
19838a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
19848a1b9b6aSSam Leffler 			return EINVAL;
19851a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
19861a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
19871a1e1d21SSam Leffler 		if (error)
19881a1e1d21SSam Leffler 			break;
19898a1b9b6aSSam Leffler 		ieee80211_key_update_begin(ic);
1990dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
1991dd70e17bSSam Leffler 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
1992dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
19938a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
19948a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
19958a1b9b6aSSam Leffler 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
19968a1b9b6aSSam Leffler 				error = EINVAL;
19978a1b9b6aSSam Leffler 		} else
19988a1b9b6aSSam Leffler 			error = EINVAL;
19998a1b9b6aSSam Leffler 		ieee80211_key_update_end(ic);
2000b5d4660fSSam Leffler 		if (!error)			/* NB: for compatibility */
2001b5d4660fSSam Leffler 			error = ENETRESET;
20021a1e1d21SSam Leffler 		break;
20031a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
20041a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2005380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
2006380c5fa9SSam Leffler 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
20078a1b9b6aSSam Leffler 			return EINVAL;
20088a1b9b6aSSam Leffler 		ic->ic_def_txkey = kid;
2009b8d2606bSSam Leffler 		error = ENETRESET;	/* push to hardware */
20108a1b9b6aSSam Leffler 		break;
20118a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
20128a1b9b6aSSam Leffler 		switch (ireq->i_val) {
20138a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
20148a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
20158a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
20168a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
20178a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
20188a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
20198a1b9b6aSSam Leffler 			if (auth == NULL)
20208a1b9b6aSSam Leffler 				return EINVAL;
20218a1b9b6aSSam Leffler 			break;
20228a1b9b6aSSam Leffler 		default:
20238a1b9b6aSSam Leffler 			return EINVAL;
20248a1b9b6aSSam Leffler 		}
20258a1b9b6aSSam Leffler 		switch (ireq->i_val) {
20268a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
20278a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
20288a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
20298a1b9b6aSSam Leffler 			break;
20308a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
20318a1b9b6aSSam Leffler 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
20328a1b9b6aSSam Leffler 			break;
20338a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
20348a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
20358a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
20368a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
20378a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
20388a1b9b6aSSam Leffler 			break;
20398a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
20408a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
20418a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
20428a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
20431a1e1d21SSam Leffler 			break;
20441a1e1d21SSam Leffler 		}
20458a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
20468a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = ireq->i_val;
20478a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
20488a1b9b6aSSam Leffler 		ic->ic_auth = auth;
20491a1e1d21SSam Leffler 		error = ENETRESET;
20501a1e1d21SSam Leffler 		break;
20511a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
20521a1e1d21SSam Leffler 		/* XXX 0xffff overflows 16-bit signed */
20531a1e1d21SSam Leffler 		if (ireq->i_val == 0 ||
20541a1e1d21SSam Leffler 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
20551a1e1d21SSam Leffler 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
20561a1e1d21SSam Leffler 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
20571a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, ireq->i_val)) {
20588a1b9b6aSSam Leffler 			return EINVAL;
20591a1e1d21SSam Leffler 		} else
20601a1e1d21SSam Leffler 			ic->ic_ibss_chan = ic->ic_des_chan =
20611a1e1d21SSam Leffler 				&ic->ic_channels[ireq->i_val];
20621a1e1d21SSam Leffler 		switch (ic->ic_state) {
20631a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
20641a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
20651a1e1d21SSam Leffler 			error = ENETRESET;
20661a1e1d21SSam Leffler 			break;
20671a1e1d21SSam Leffler 		default:
20688a1b9b6aSSam Leffler 			/*
20698a1b9b6aSSam Leffler 			 * If the desired channel has changed (to something
20708a1b9b6aSSam Leffler 			 * other than any) and we're not already scanning,
20718a1b9b6aSSam Leffler 			 * then kick the state machine.
20728a1b9b6aSSam Leffler 			 */
20731a1e1d21SSam Leffler 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
20748a1b9b6aSSam Leffler 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
20758a1b9b6aSSam Leffler 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
20761a1e1d21SSam Leffler 				error = ENETRESET;
20771a1e1d21SSam Leffler 			break;
20781a1e1d21SSam Leffler 		}
20798a1b9b6aSSam Leffler 		if (error == ENETRESET && ic->ic_opmode == IEEE80211_M_MONITOR)
20808a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
20811a1e1d21SSam Leffler 		break;
20821a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
20831a1e1d21SSam Leffler 		switch (ireq->i_val) {
20841a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
20851a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
20861a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
20871a1e1d21SSam Leffler 				error = ENETRESET;
20881a1e1d21SSam Leffler 			}
20891a1e1d21SSam Leffler 			break;
20901a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
20911a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
20921a1e1d21SSam Leffler 				error = EINVAL;
20931a1e1d21SSam Leffler 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
20941a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
20951a1e1d21SSam Leffler 				error = ENETRESET;
20961a1e1d21SSam Leffler 			}
20971a1e1d21SSam Leffler 			break;
20981a1e1d21SSam Leffler 		default:
20991a1e1d21SSam Leffler 			error = EINVAL;
21001a1e1d21SSam Leffler 			break;
21011a1e1d21SSam Leffler 		}
21021a1e1d21SSam Leffler 		break;
21031a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
21048a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
21058a1b9b6aSSam Leffler 			return EINVAL;
21061a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
21078a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21081a1e1d21SSam Leffler 		break;
210913604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
211070231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
211170231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
21128a1b9b6aSSam Leffler 			return EINVAL;
21131a1e1d21SSam Leffler 		ic->ic_rtsthreshold = ireq->i_val;
21148a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21151a1e1d21SSam Leffler 		break;
21162e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
21178a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
21188a1b9b6aSSam Leffler 			return EINVAL;
21192e79ca97SSam Leffler 		ic->ic_protmode = ireq->i_val;
21202e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
21212e79ca97SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
21228a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21232e79ca97SSam Leffler 		break;
21242e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
21258a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
21268a1b9b6aSSam Leffler 			return EINVAL;
21272e79ca97SSam Leffler 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
21288a1b9b6aSSam Leffler 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
21298a1b9b6aSSam Leffler 			return EINVAL;
21308a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
21318a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21328a1b9b6aSSam Leffler 		break;
21338a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
21348a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
21358a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
21368a1b9b6aSSam Leffler 			return EINVAL;
21378a1b9b6aSSam Leffler 		ic->ic_roaming = ireq->i_val;
21388a1b9b6aSSam Leffler 		/* XXXX reset? */
21398a1b9b6aSSam Leffler 		break;
21408a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
21418a1b9b6aSSam Leffler 		if (ireq->i_val) {
21428a1b9b6aSSam Leffler 			/* XXX check for key state? */
21438a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
21448a1b9b6aSSam Leffler 		} else
21458a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
21468a1b9b6aSSam Leffler 		break;
21478a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
21488a1b9b6aSSam Leffler 		if (ireq->i_val)
21498a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
21508a1b9b6aSSam Leffler 		else
21518a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
21528a1b9b6aSSam Leffler 		break;
21538a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
21548a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setkey(ic, ireq);
21558a1b9b6aSSam Leffler 		break;
21568a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
21578a1b9b6aSSam Leffler 		error = ieee80211_ioctl_delkey(ic, ireq);
21588a1b9b6aSSam Leffler 		break;
21598a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
21608a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setmlme(ic, ireq);
21618a1b9b6aSSam Leffler 		break;
21628a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
21638a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setoptie(ic, ireq);
21648a1b9b6aSSam Leffler 		break;
21658a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
21668a1b9b6aSSam Leffler 		if (ireq->i_val) {
21678a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
21688a1b9b6aSSam Leffler 				return EINVAL;
21698a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_COUNTERM;
21708a1b9b6aSSam Leffler 		} else
21718a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
21728a1b9b6aSSam Leffler 		break;
21738a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
21748a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
21758a1b9b6aSSam Leffler 			return EINVAL;
21768a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
21778a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WPA;
21788a1b9b6aSSam Leffler 		switch (ireq->i_val) {
21798a1b9b6aSSam Leffler 		case 1:
21808a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1;
21818a1b9b6aSSam Leffler 			break;
21828a1b9b6aSSam Leffler 		case 2:
21838a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA2;
21848a1b9b6aSSam Leffler 			break;
21858a1b9b6aSSam Leffler 		case 3:
21868a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
21872e79ca97SSam Leffler 			break;
21882e79ca97SSam Leffler 		}
21898a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX? */
21908a1b9b6aSSam Leffler 		break;
21918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
21928a1b9b6aSSam Leffler 		if (ireq->i_val) {
21938a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
21948a1b9b6aSSam Leffler 				return EINVAL;
21958a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WME;
21968a1b9b6aSSam Leffler 		} else
21978a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WME;
21988a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX maybe not for station? */
21998a1b9b6aSSam Leffler 		break;
22008a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
22018a1b9b6aSSam Leffler 		if (ireq->i_val)
22028a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_HIDESSID;
22038a1b9b6aSSam Leffler 		else
22048a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
22052e79ca97SSam Leffler 		error = ENETRESET;
22062e79ca97SSam Leffler 		break;
22078a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
22088a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
22098a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
22108a1b9b6aSSam Leffler 		else
22118a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
22128a1b9b6aSSam Leffler 		break;
22138a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
22148a1b9b6aSSam Leffler 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
22158a1b9b6aSSam Leffler 		    !ieee80211_crypto_available(ireq->i_val))
22168a1b9b6aSSam Leffler 			return EINVAL;
22178a1b9b6aSSam Leffler 		rsn->rsn_mcastcipher = ireq->i_val;
22188a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22198a1b9b6aSSam Leffler 		break;
22208a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
22218a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
22228a1b9b6aSSam Leffler 			return EINVAL;
22238a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
22248a1b9b6aSSam Leffler 		rsn->rsn_mcastkeylen = ireq->i_val;
22258a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22268a1b9b6aSSam Leffler 		break;
22278a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
22288a1b9b6aSSam Leffler 		/*
22298a1b9b6aSSam Leffler 		 * Convert user-specified cipher set to the set
22308a1b9b6aSSam Leffler 		 * we can support (via hardware or software).
22318a1b9b6aSSam Leffler 		 * NB: this logic intentionally ignores unknown and
22328a1b9b6aSSam Leffler 		 * unsupported ciphers so folks can specify 0xff or
22338a1b9b6aSSam Leffler 		 * similar and get all available ciphers.
22348a1b9b6aSSam Leffler 		 */
22358a1b9b6aSSam Leffler 		caps = 0;
22368a1b9b6aSSam Leffler 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
22378a1b9b6aSSam Leffler 			if ((ireq->i_val & (1<<j)) &&
22388a1b9b6aSSam Leffler 			    ((ic->ic_caps & cipher2cap(j)) ||
22398a1b9b6aSSam Leffler 			     ieee80211_crypto_available(j)))
22408a1b9b6aSSam Leffler 				caps |= 1<<j;
22418a1b9b6aSSam Leffler 		if (caps == 0)			/* nothing available */
22428a1b9b6aSSam Leffler 			return EINVAL;
22438a1b9b6aSSam Leffler 		/* XXX verify ciphers ok for unicast use? */
22448a1b9b6aSSam Leffler 		/* XXX disallow if running as it'll have no effect */
22458a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset = caps;
22468a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22478a1b9b6aSSam Leffler 		break;
22488a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
22498a1b9b6aSSam Leffler 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
22508a1b9b6aSSam Leffler 			return EINVAL;
22518a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = ireq->i_val;
22528a1b9b6aSSam Leffler 		break;
22538a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
22548a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
22558a1b9b6aSSam Leffler 			return EINVAL;
22568a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
22578a1b9b6aSSam Leffler 		rsn->rsn_ucastkeylen = ireq->i_val;
22588a1b9b6aSSam Leffler 		break;
22598a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
22608a1b9b6aSSam Leffler 		/* NB: for testing */
22618a1b9b6aSSam Leffler 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
22628a1b9b6aSSam Leffler 			       ((u_int16_t) ireq->i_len);
22638a1b9b6aSSam Leffler 		break;
22648a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
22658a1b9b6aSSam Leffler 		/* XXX check */
22668a1b9b6aSSam Leffler 		rsn->rsn_keymgmtset = ireq->i_val;
22678a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22688a1b9b6aSSam Leffler 		break;
22698a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
22708a1b9b6aSSam Leffler 		/* XXX check */
22718a1b9b6aSSam Leffler 		rsn->rsn_caps = ireq->i_val;
22728a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22738a1b9b6aSSam Leffler 		break;
22748a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
22758a1b9b6aSSam Leffler 		/* NB: should only be set when in STA mode */
22768a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
22778a1b9b6aSSam Leffler 			return EINVAL;
22788a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
22798a1b9b6aSSam Leffler 			return EINVAL;
22808a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
22818a1b9b6aSSam Leffler 		if (error)
22828a1b9b6aSSam Leffler 			break;
22838a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
22848a1b9b6aSSam Leffler 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
22858a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
22868a1b9b6aSSam Leffler 		else
22878a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DESBSSID;
22888a1b9b6aSSam Leffler 		error = ENETRESET;
22898a1b9b6aSSam Leffler 		break;
22908a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
22918a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setchanlist(ic, ireq);
22928a1b9b6aSSam Leffler 		break;
22938a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
22948a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
22958a1b9b6aSSam Leffler 			break;
22968a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
22978a1b9b6aSSam Leffler 		if (error == 0)		/* XXX background scan */
22988a1b9b6aSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
22998a1b9b6aSSam Leffler 		break;
23008a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
23018a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
23028a1b9b6aSSam Leffler 		error = ieee80211_ioctl_macmac(ic, ireq);
23038a1b9b6aSSam Leffler 		break;
23048a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
23058a1b9b6aSSam Leffler 		error = ieee80211_ioctl_maccmd(ic, ireq);
23068a1b9b6aSSam Leffler 		break;
23078a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
23088a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
23098a1b9b6aSSam Leffler 		break;
23108a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
23118a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
23128a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
23138a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
23148a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
23158a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
23168a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
23178a1b9b6aSSam Leffler 		break;
23188a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
23198a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
23208a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
23218a1b9b6aSSam Leffler 			return EINVAL;
23228a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
23238a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
23248a1b9b6aSSam Leffler 			ic->ic_dtim_period = ireq->i_val;
23258a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
23268a1b9b6aSSam Leffler 		} else
23278a1b9b6aSSam Leffler 			error = EINVAL;
23288a1b9b6aSSam Leffler 		break;
23298a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
23308a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
23318a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
23328a1b9b6aSSam Leffler 			return EINVAL;
23338a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
23348a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2335d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
23368a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
23378a1b9b6aSSam Leffler 		} else
23388a1b9b6aSSam Leffler 			error = EINVAL;
23398a1b9b6aSSam Leffler 		break;
2340c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
2341c4f040c3SSam Leffler 		if (ireq->i_val)
2342c4f040c3SSam Leffler 			ic->ic_flags |= IEEE80211_F_PUREG;
2343c4f040c3SSam Leffler 		else
2344c4f040c3SSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PUREG;
2345c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
2346c4f040c3SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
2347c4f040c3SSam Leffler 			error = ENETRESET;
2348c4f040c3SSam Leffler 		break;
234970231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
235070231e3dSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXFRAG) == 0 &&
235170231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
235270231e3dSSam Leffler 			return EINVAL;
235370231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
235470231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
235570231e3dSSam Leffler 			return EINVAL;
235670231e3dSSam Leffler 		ic->ic_fragthreshold = ireq->i_val;
235770231e3dSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
235870231e3dSSam Leffler 		break;
23591a1e1d21SSam Leffler 	default:
23601a1e1d21SSam Leffler 		error = EINVAL;
23611a1e1d21SSam Leffler 		break;
23621a1e1d21SSam Leffler 	}
23638a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
23648a1b9b6aSSam Leffler 		error = 0;
23658a1b9b6aSSam Leffler 	return error;
23668a1b9b6aSSam Leffler }
23678a1b9b6aSSam Leffler 
23688a1b9b6aSSam Leffler int
23698a1b9b6aSSam Leffler ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
23708a1b9b6aSSam Leffler {
23718a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
23728a1b9b6aSSam Leffler 	int error = 0;
23738a1b9b6aSSam Leffler 	struct ifreq *ifr;
23748a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
23758a1b9b6aSSam Leffler 
23768a1b9b6aSSam Leffler 	switch (cmd) {
23778a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
23788a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
23798a1b9b6aSSam Leffler 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
23808a1b9b6aSSam Leffler 				&ic->ic_media, cmd);
23818a1b9b6aSSam Leffler 		break;
23828a1b9b6aSSam Leffler 	case SIOCG80211:
23838a1b9b6aSSam Leffler 		error = ieee80211_ioctl_get80211(ic, cmd,
23848a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
23858a1b9b6aSSam Leffler 		break;
23868a1b9b6aSSam Leffler 	case SIOCS80211:
23878a1b9b6aSSam Leffler 		error = suser(curthread);
23888a1b9b6aSSam Leffler 		if (error == 0)
23898a1b9b6aSSam Leffler 			error = ieee80211_ioctl_set80211(ic, cmd,
23908a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
23911a1e1d21SSam Leffler 		break;
23921a1e1d21SSam Leffler 	case SIOCGIFGENERIC:
23938a1b9b6aSSam Leffler 		error = ieee80211_cfgget(ic, cmd, data);
23941a1e1d21SSam Leffler 		break;
23951a1e1d21SSam Leffler 	case SIOCSIFGENERIC:
23961a1e1d21SSam Leffler 		error = suser(curthread);
23971a1e1d21SSam Leffler 		if (error)
23981a1e1d21SSam Leffler 			break;
23998a1b9b6aSSam Leffler 		error = ieee80211_cfgset(ic, cmd, data);
24001a1e1d21SSam Leffler 		break;
24011be50176SSam Leffler 	case SIOCG80211STATS:
24021be50176SSam Leffler 		ifr = (struct ifreq *)data;
24031be50176SSam Leffler 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
24041be50176SSam Leffler 		break;
24056f161f03SSam Leffler 	case SIOCSIFMTU:
24066f161f03SSam Leffler 		ifr = (struct ifreq *)data;
24076f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
24086f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
24096f161f03SSam Leffler 			error = EINVAL;
24106f161f03SSam Leffler 		else
24116f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
24126f161f03SSam Leffler 		break;
2413b2e95691SSam Leffler 	case SIOCSIFADDR:
2414b2e95691SSam Leffler 		/*
2415b2e95691SSam Leffler 		 * XXX Handle this directly so we can supress if_init calls.
2416b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
2417b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
2418b2e95691SSam Leffler 		 * XXX set IFF_UP and so supress if_init being called when
2419b2e95691SSam Leffler 		 * XXX it should be.
2420b2e95691SSam Leffler 		 */
2421b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
2422b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
2423b2e95691SSam Leffler #ifdef INET
2424b2e95691SSam Leffler 		case AF_INET:
2425b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2426b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2427b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2428b2e95691SSam Leffler 			}
2429b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
2430b2e95691SSam Leffler 			break;
2431b2e95691SSam Leffler #endif
2432b2e95691SSam Leffler #ifdef IPX
2433b2e95691SSam Leffler 		/*
2434b2e95691SSam Leffler 		 * XXX - This code is probably wrong,
2435b2e95691SSam Leffler 		 *	 but has been copied many times.
2436b2e95691SSam Leffler 		 */
2437b2e95691SSam Leffler 		case AF_IPX: {
2438b2e95691SSam Leffler 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
2439b2e95691SSam Leffler 
2440b2e95691SSam Leffler 			if (ipx_nullhost(*ina))
2441fc74a9f9SBrooks Davis 				ina->x_host = *(union ipx_host *)
2442fc74a9f9SBrooks Davis 				    IFP2ENADDR(ifp);
2443b2e95691SSam Leffler 			else
2444b2e95691SSam Leffler 				bcopy((caddr_t) ina->x_host.c_host,
2445fc74a9f9SBrooks Davis 				      (caddr_t) IFP2ENADDR(ifp),
2446fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
2447b2e95691SSam Leffler 			/* fall thru... */
2448b2e95691SSam Leffler 		}
2449b2e95691SSam Leffler #endif
2450b2e95691SSam Leffler 		default:
2451b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2452b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2453b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2454b2e95691SSam Leffler 			}
2455b2e95691SSam Leffler 			break;
2456b2e95691SSam Leffler 		}
2457b2e95691SSam Leffler 		break;
24581a1e1d21SSam Leffler 	default:
24591a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
24601a1e1d21SSam Leffler 		break;
24611a1e1d21SSam Leffler 	}
24621a1e1d21SSam Leffler 	return error;
24631a1e1d21SSam Leffler }
2464