xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 2c39b32c9439c191bdc67411f3fee3e77e82fe32)
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) \
718a1b9b6aSSam Leffler 	(((_ic)->ic_ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
728a1b9b6aSSam Leffler #define	IS_UP_AUTO(_ic) \
738a1b9b6aSSam Leffler 	(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
748a1b9b6aSSam Leffler 
751a1e1d21SSam Leffler /*
761a1e1d21SSam Leffler  * XXX
771a1e1d21SSam Leffler  * Wireless LAN specific configuration interface, which is compatible
781a1e1d21SSam Leffler  * with wicontrol(8).
791a1e1d21SSam Leffler  */
801a1e1d21SSam Leffler 
818a1b9b6aSSam Leffler struct wi_read_ap_args {
828a1b9b6aSSam Leffler 	int	i;		/* result count */
838a1b9b6aSSam Leffler 	struct wi_apinfo *ap;	/* current entry in result buffer */
848a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
858a1b9b6aSSam Leffler };
868a1b9b6aSSam Leffler 
878a1b9b6aSSam Leffler static void
888a1b9b6aSSam Leffler wi_read_ap_result(void *arg, struct ieee80211_node *ni)
891a1e1d21SSam Leffler {
908a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
918a1b9b6aSSam Leffler 	struct wi_read_ap_args *sa = arg;
928a1b9b6aSSam Leffler 	struct wi_apinfo *ap = sa->ap;
938a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
948a1b9b6aSSam Leffler 	int j;
958a1b9b6aSSam Leffler 
968a1b9b6aSSam Leffler 	if ((caddr_t)(ap + 1) > sa->max)
978a1b9b6aSSam Leffler 		return;
988a1b9b6aSSam Leffler 	memset(ap, 0, sizeof(struct wi_apinfo));
998a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1008a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
1018a1b9b6aSSam Leffler 		ap->namelen = ic->ic_des_esslen;
1028a1b9b6aSSam Leffler 		if (ic->ic_des_esslen)
1038a1b9b6aSSam Leffler 			memcpy(ap->name, ic->ic_des_essid,
1048a1b9b6aSSam Leffler 			    ic->ic_des_esslen);
1058a1b9b6aSSam Leffler 	} else {
1068a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
1078a1b9b6aSSam Leffler 		ap->namelen = ni->ni_esslen;
1088a1b9b6aSSam Leffler 		if (ni->ni_esslen)
1098a1b9b6aSSam Leffler 			memcpy(ap->name, ni->ni_essid,
1108a1b9b6aSSam Leffler 			    ni->ni_esslen);
1118a1b9b6aSSam Leffler 	}
1128a1b9b6aSSam Leffler 	ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
1138a1b9b6aSSam Leffler 	ap->signal = ic->ic_node_getrssi(ni);
1148a1b9b6aSSam Leffler 	ap->capinfo = ni->ni_capinfo;
1158a1b9b6aSSam Leffler 	ap->interval = ni->ni_intval;
1168a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
1178a1b9b6aSSam Leffler 	for (j = 0; j < rs->rs_nrates; j++) {
1188a1b9b6aSSam Leffler 		if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
1198a1b9b6aSSam Leffler 			ap->rate = (rs->rs_rates[j] &
1208a1b9b6aSSam Leffler 			    IEEE80211_RATE_VAL) * 5; /* XXX */
1218a1b9b6aSSam Leffler 		}
1228a1b9b6aSSam Leffler 	}
1238a1b9b6aSSam Leffler 	sa->i++;
1248a1b9b6aSSam Leffler 	sa->ap++;
1258a1b9b6aSSam Leffler }
1268a1b9b6aSSam Leffler 
1278a1b9b6aSSam Leffler struct wi_read_prism2_args {
1288a1b9b6aSSam Leffler 	int	i;		/* result count */
1298a1b9b6aSSam Leffler 	struct wi_scan_res *res;/* current entry in result buffer */
1308a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1318a1b9b6aSSam Leffler };
1328a1b9b6aSSam Leffler 
1338a1b9b6aSSam Leffler static void
1348a1b9b6aSSam Leffler wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
1358a1b9b6aSSam Leffler {
1368a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1378a1b9b6aSSam Leffler 	struct wi_read_prism2_args *sa = arg;
1388a1b9b6aSSam Leffler 	struct wi_scan_res *res = sa->res;
1398a1b9b6aSSam Leffler 
1408a1b9b6aSSam Leffler 	if ((caddr_t)(res + 1) > sa->max)
1418a1b9b6aSSam Leffler 		return;
1428a1b9b6aSSam Leffler 	res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1438a1b9b6aSSam Leffler 	res->wi_noise = 0;
1448a1b9b6aSSam Leffler 	res->wi_signal = ic->ic_node_getrssi(ni);
1458a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
1468a1b9b6aSSam Leffler 	res->wi_interval = ni->ni_intval;
1478a1b9b6aSSam Leffler 	res->wi_capinfo = ni->ni_capinfo;
1488a1b9b6aSSam Leffler 	res->wi_ssid_len = ni->ni_esslen;
1498a1b9b6aSSam Leffler 	memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
1508a1b9b6aSSam Leffler 	/* NB: assumes wi_srates holds <= ni->ni_rates */
1518a1b9b6aSSam Leffler 	memcpy(res->wi_srates, ni->ni_rates.rs_rates,
1528a1b9b6aSSam Leffler 		sizeof(res->wi_srates));
1538a1b9b6aSSam Leffler 	if (ni->ni_rates.rs_nrates < 10)
1548a1b9b6aSSam Leffler 		res->wi_srates[ni->ni_rates.rs_nrates] = 0;
1558a1b9b6aSSam Leffler 	res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1568a1b9b6aSSam Leffler 	res->wi_rsvd = 0;
1578a1b9b6aSSam Leffler 
1588a1b9b6aSSam Leffler 	sa->i++;
1598a1b9b6aSSam Leffler 	sa->res++;
1608a1b9b6aSSam Leffler }
1618a1b9b6aSSam Leffler 
1628a1b9b6aSSam Leffler struct wi_read_sigcache_args {
1638a1b9b6aSSam Leffler 	int	i;		/* result count */
1648a1b9b6aSSam Leffler 	struct wi_sigcache *wsc;/* current entry in result buffer */
1658a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1668a1b9b6aSSam Leffler };
1678a1b9b6aSSam Leffler 
1688a1b9b6aSSam Leffler static void
1698a1b9b6aSSam Leffler wi_read_sigcache(void *arg, struct ieee80211_node *ni)
1708a1b9b6aSSam Leffler {
1718a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1728a1b9b6aSSam Leffler 	struct wi_read_sigcache_args *sa = arg;
1738a1b9b6aSSam Leffler 	struct wi_sigcache *wsc = sa->wsc;
1748a1b9b6aSSam Leffler 
1758a1b9b6aSSam Leffler 	if ((caddr_t)(wsc + 1) > sa->max)
1768a1b9b6aSSam Leffler 		return;
1778a1b9b6aSSam Leffler 	memset(wsc, 0, sizeof(struct wi_sigcache));
1788a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
1798a1b9b6aSSam Leffler 	wsc->signal = ic->ic_node_getrssi(ni);
1808a1b9b6aSSam Leffler 
1818a1b9b6aSSam Leffler 	sa->wsc++;
1828a1b9b6aSSam Leffler 	sa->i++;
1838a1b9b6aSSam Leffler }
1848a1b9b6aSSam Leffler 
1858a1b9b6aSSam Leffler int
1868a1b9b6aSSam Leffler ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
1878a1b9b6aSSam Leffler {
1888a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1891a1e1d21SSam Leffler 	int i, j, error;
1901a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
1911a1e1d21SSam Leffler 	struct wi_req wreq;
1921a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
1931a1e1d21SSam Leffler 
1941a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1951a1e1d21SSam Leffler 	if (error)
1961a1e1d21SSam Leffler 		return error;
1971a1e1d21SSam Leffler 	wreq.wi_len = 0;
1981a1e1d21SSam Leffler 	switch (wreq.wi_type) {
1991a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
2001a1e1d21SSam Leffler 		/* nothing appropriate */
2011a1e1d21SSam Leffler 		break;
2021a1e1d21SSam Leffler 	case WI_RID_NODENAME:
2031a1e1d21SSam Leffler 		strcpy((char *)&wreq.wi_val[1], hostname);
2041a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(strlen(hostname));
2051a1e1d21SSam Leffler 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
2061a1e1d21SSam Leffler 		break;
2071a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
2081a1e1d21SSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
2091a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;
2101a1e1d21SSam Leffler 			wreq.wi_len = 1;
2111a1e1d21SSam Leffler 			break;
2121a1e1d21SSam Leffler 		}
2131a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
2141a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
2151a1e1d21SSam Leffler 		    ic->ic_bss->ni_esslen);
2161a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
2171a1e1d21SSam Leffler 		break;
2181a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
2191a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
2201a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
2211a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
2221a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
2231a1e1d21SSam Leffler 		break;
2241a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
2251a1e1d21SSam Leffler 		if (ic->ic_state == IEEE80211_S_RUN)
2261a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
2271a1e1d21SSam Leffler 		else
2281a1e1d21SSam Leffler 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
2291a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2301a1e1d21SSam Leffler 		break;
2311a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
2321a1e1d21SSam Leffler 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
2331a1e1d21SSam Leffler 		/*
2341a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
2351a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
2361a1e1d21SSam Leffler 		 */
2371a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
2381a1e1d21SSam Leffler 			i = 1;
2391a1e1d21SSam Leffler 		else
2401a1e1d21SSam Leffler 			i = 0;
2411a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
2421a1e1d21SSam Leffler 			if (isset(ic->ic_chan_active, i)) {
2431a1e1d21SSam Leffler 				setbit((u_int8_t *)wreq.wi_val, j);
2441a1e1d21SSam Leffler 				wreq.wi_len = j / 16 + 1;
2451a1e1d21SSam Leffler 			}
2461a1e1d21SSam Leffler 		break;
2471a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
2481a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2491a1e1d21SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
2501a1e1d21SSam Leffler 		wreq.wi_len = 1;
2511a1e1d21SSam Leffler 		break;
2521a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
2531a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2541a1e1d21SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
2551a1e1d21SSam Leffler 		wreq.wi_len = 1;
2561a1e1d21SSam Leffler 		break;
2571a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
2581a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;				/* quality */
2598a1b9b6aSSam Leffler 		wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
2601a1e1d21SSam Leffler 		wreq.wi_val[2] = 0;				/* noise */
2611a1e1d21SSam Leffler 		wreq.wi_len = 3;
2621a1e1d21SSam Leffler 		break;
2631a1e1d21SSam Leffler 	case WI_RID_PROMISC:
2641a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
2651a1e1d21SSam Leffler 		wreq.wi_len = 1;
2661a1e1d21SSam Leffler 		break;
2671a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
2681a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_opmode);
2691a1e1d21SSam Leffler 		wreq.wi_len = 1;
2701a1e1d21SSam Leffler 		break;
2711a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
2721a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
2731a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2741a1e1d21SSam Leffler 		break;
2751a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
2762c39b32cSSam Leffler 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
2771a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;	/* auto */
2781a1e1d21SSam Leffler 		else
2791a1e1d21SSam Leffler 			wreq.wi_val[0] = htole16(
2801a1e1d21SSam Leffler 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
2811a1e1d21SSam Leffler 			    IEEE80211_RATE_VAL) / 2);
2821a1e1d21SSam Leffler 		wreq.wi_len = 1;
2831a1e1d21SSam Leffler 		break;
2841a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
2851a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2861a1e1d21SSam Leffler 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
2871a1e1d21SSam Leffler 		    IEEE80211_RATE_VAL) / 2);
2881a1e1d21SSam Leffler 		wreq.wi_len = 1;
2891a1e1d21SSam Leffler 		break;
2901a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
2911a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2921a1e1d21SSam Leffler 		wreq.wi_len = 1;
2931a1e1d21SSam Leffler 		break;
2941a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
2951a1e1d21SSam Leffler 		wreq.wi_val[0] =
2961a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
2971a1e1d21SSam Leffler 		wreq.wi_len = 1;
2981a1e1d21SSam Leffler 		break;
2991a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
3001a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;	/* no ... not supported */
3011a1e1d21SSam Leffler 		wreq.wi_len = 1;
3021a1e1d21SSam Leffler 		break;
3031a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
3048a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_roaming);	/* XXX map */
3051a1e1d21SSam Leffler 		wreq.wi_len = 1;
3061a1e1d21SSam Leffler 		break;
3071a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
3081a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
3091a1e1d21SSam Leffler 		wreq.wi_len = 1;
3101a1e1d21SSam Leffler 		break;
3111a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
3121a1e1d21SSam Leffler 		wreq.wi_val[0] =
3131a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
3141a1e1d21SSam Leffler 		wreq.wi_len = 1;
3151a1e1d21SSam Leffler 		break;
3161a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
3171a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_lintval);
3181a1e1d21SSam Leffler 		wreq.wi_len = 1;
3191a1e1d21SSam Leffler 		break;
3201a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
3211a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
3221a1e1d21SSam Leffler 		wreq.wi_len = 1;
3231a1e1d21SSam Leffler 		break;
3241a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
3258a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(1);	/* always available */
3261a1e1d21SSam Leffler 		wreq.wi_len = 1;
3271a1e1d21SSam Leffler 		break;
3281a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
3291a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
3301a1e1d21SSam Leffler 		wreq.wi_len = 1;
3311a1e1d21SSam Leffler 		break;
3321a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
3331a1e1d21SSam Leffler 		wreq.wi_val[0] =
3348a1b9b6aSSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
3351a1e1d21SSam Leffler 		wreq.wi_len = 1;
3361a1e1d21SSam Leffler 		break;
3371a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
3388a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_def_txkey);
3391a1e1d21SSam Leffler 		wreq.wi_len = 1;
3401a1e1d21SSam Leffler 		break;
3411a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
3421a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
3431a1e1d21SSam Leffler 		/* do not show keys to non-root user */
3441a1e1d21SSam Leffler 		error = suser(curthread);
3451a1e1d21SSam Leffler 		if (error) {
3461a1e1d21SSam Leffler 			memset(keys, 0, sizeof(*keys));
3471a1e1d21SSam Leffler 			error = 0;
3481a1e1d21SSam Leffler 			break;
3491a1e1d21SSam Leffler 		}
3501a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
3511a1e1d21SSam Leffler 			keys->wi_keys[i].wi_keylen =
3528a1b9b6aSSam Leffler 			    htole16(ic->ic_nw_keys[i].wk_keylen);
3531a1e1d21SSam Leffler 			memcpy(keys->wi_keys[i].wi_keydat,
3548a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_key,
3558a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_keylen);
3561a1e1d21SSam Leffler 		}
3571a1e1d21SSam Leffler 		wreq.wi_len = sizeof(*keys) / 2;
3581a1e1d21SSam Leffler 		break;
3591a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
3608a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
3611a1e1d21SSam Leffler 		wreq.wi_len = 1;
3621a1e1d21SSam Leffler 		break;
3631a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
3641a1e1d21SSam Leffler 		/* XXX: should be implemented in lower drivers */
3651a1e1d21SSam Leffler 		break;
3661a1e1d21SSam Leffler 	case WI_RID_READ_APS:
3671a1e1d21SSam Leffler 		/*
3681a1e1d21SSam Leffler 		 * Don't return results until active scan completes.
3691a1e1d21SSam Leffler 		 */
3708a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3718a1b9b6aSSam Leffler 			struct wi_read_ap_args args;
3728a1b9b6aSSam Leffler 
3738a1b9b6aSSam Leffler 			args.i = 0;
3748a1b9b6aSSam Leffler 			args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
3758a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
3768a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
3778a1b9b6aSSam Leffler 				wi_read_ap_result, &args);
3788a1b9b6aSSam Leffler 			memcpy(wreq.wi_val, &args.i, sizeof(args.i));
3798a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(int) +
3808a1b9b6aSSam Leffler 				sizeof(struct wi_apinfo) * args.i) / 2;
3818a1b9b6aSSam Leffler 		} else
3821a1e1d21SSam Leffler 			error = EINPROGRESS;
3831a1e1d21SSam Leffler 		break;
3841a1e1d21SSam Leffler 	case WI_RID_PRISM2:
3858a1b9b6aSSam Leffler 		/* NB: we lie so WI_RID_SCAN_RES can include rates */
3868a1b9b6aSSam Leffler 		wreq.wi_val[0] = 1;
3871a1e1d21SSam Leffler 		wreq.wi_len = sizeof(u_int16_t) / 2;
3881a1e1d21SSam Leffler 		break;
3891a1e1d21SSam Leffler 	case WI_RID_SCAN_RES:			/* compatibility interface */
3908a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3918a1b9b6aSSam Leffler 			struct wi_read_prism2_args args;
3928a1b9b6aSSam Leffler 			struct wi_scan_p2_hdr *p2;
3938a1b9b6aSSam Leffler 
3948a1b9b6aSSam Leffler 			/* NB: use Prism2 format so we can include rate info */
3958a1b9b6aSSam Leffler 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
3968a1b9b6aSSam Leffler 			args.i = 0;
3978a1b9b6aSSam Leffler 			args.res = (void *)&p2[1];
3988a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
3998a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
4008a1b9b6aSSam Leffler 				wi_read_prism2_result, &args);
4018a1b9b6aSSam Leffler 			p2->wi_rsvd = 0;
4028a1b9b6aSSam Leffler 			p2->wi_reason = args.i;
4038a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(*p2) +
4048a1b9b6aSSam Leffler 				sizeof(struct wi_scan_res) * args.i) / 2;
4058a1b9b6aSSam Leffler 		} else
4061a1e1d21SSam Leffler 			error = EINPROGRESS;
4071a1e1d21SSam Leffler 		break;
4088a1b9b6aSSam Leffler 	case WI_RID_READ_CACHE: {
4098a1b9b6aSSam Leffler 		struct wi_read_sigcache_args args;
4108a1b9b6aSSam Leffler 		args.i = 0;
4118a1b9b6aSSam Leffler 		args.wsc = (struct wi_sigcache *) wreq.wi_val;
4128a1b9b6aSSam Leffler 		args.max = (void *)(&wreq + 1);
4138a1b9b6aSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
4148a1b9b6aSSam Leffler 		wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
4158a1b9b6aSSam Leffler 		break;
4161a1e1d21SSam Leffler 	}
4171a1e1d21SSam Leffler 	default:
4181a1e1d21SSam Leffler 		error = EINVAL;
4191a1e1d21SSam Leffler 		break;
4201a1e1d21SSam Leffler 	}
4211a1e1d21SSam Leffler 	if (error == 0) {
4221a1e1d21SSam Leffler 		wreq.wi_len++;
4231a1e1d21SSam Leffler 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
4241a1e1d21SSam Leffler 	}
4251a1e1d21SSam Leffler 	return error;
4261a1e1d21SSam Leffler }
4271a1e1d21SSam Leffler 
4281a1e1d21SSam Leffler static int
4291a1e1d21SSam Leffler findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
4301a1e1d21SSam Leffler {
4311a1e1d21SSam Leffler #define	IEEERATE(_ic,_m,_i) \
4321a1e1d21SSam Leffler 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
4331a1e1d21SSam Leffler 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
4341a1e1d21SSam Leffler 	for (i = 0; i < nrates; i++)
4351a1e1d21SSam Leffler 		if (IEEERATE(ic, mode, i) == rate)
4361a1e1d21SSam Leffler 			return i;
4371a1e1d21SSam Leffler 	return -1;
4381a1e1d21SSam Leffler #undef IEEERATE
4391a1e1d21SSam Leffler }
4401a1e1d21SSam Leffler 
44193685685SSam Leffler /*
44293685685SSam Leffler  * Prepare to do a user-initiated scan for AP's.  If no
44393685685SSam Leffler  * current/default channel is setup or the current channel
44493685685SSam Leffler  * is invalid then pick the first available channel from
44593685685SSam Leffler  * the active list as the place to start the scan.
44693685685SSam Leffler  */
44793685685SSam Leffler static int
4488a1b9b6aSSam Leffler ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
44993685685SSam Leffler {
45093685685SSam Leffler 	int i;
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;
46293685685SSam Leffler 	if (ic->ic_ibss_chan == NULL ||
46393685685SSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
46493685685SSam Leffler 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
46593685685SSam Leffler 			if (isset(chanlist, i)) {
46693685685SSam Leffler 				ic->ic_ibss_chan = &ic->ic_channels[i];
46793685685SSam Leffler 				goto found;
46893685685SSam Leffler 			}
46993685685SSam Leffler 		return EINVAL;			/* no active channels */
47093685685SSam Leffler found:
47193685685SSam Leffler 		;
47293685685SSam Leffler 	}
47393685685SSam Leffler 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
47493685685SSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
47593685685SSam Leffler 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
4768a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
47793685685SSam Leffler 	/*
4788a1b9b6aSSam Leffler 	 * We force the state to INIT before calling ieee80211_new_state
4798a1b9b6aSSam Leffler 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
4808a1b9b6aSSam Leffler 	 * altering the current state but that's not possible right now.
48193685685SSam Leffler 	 */
4828a1b9b6aSSam Leffler 	/* XXX handle proberequest case */
4838a1b9b6aSSam Leffler 	ic->ic_state = IEEE80211_S_INIT;	/* XXX bypass state machine */
4848a1b9b6aSSam Leffler 	return 0;
48593685685SSam Leffler }
48693685685SSam Leffler 
4871a1e1d21SSam Leffler int
4888a1b9b6aSSam Leffler ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
4891a1e1d21SSam Leffler {
4908a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
4911a1e1d21SSam Leffler 	int i, j, len, error, rate;
4921a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
4931a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
4941a1e1d21SSam Leffler 	struct wi_req wreq;
4951a1e1d21SSam Leffler 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
4961a1e1d21SSam Leffler 
4971a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
4981a1e1d21SSam Leffler 	if (error)
4991a1e1d21SSam Leffler 		return error;
5001a1e1d21SSam Leffler 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
5011a1e1d21SSam Leffler 	switch (wreq.wi_type) {
5021a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
5031a1e1d21SSam Leffler 	case WI_RID_NODENAME:
5041a1e1d21SSam Leffler 		return EPERM;
5051a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
5061a1e1d21SSam Leffler 		return EPERM;
5071a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
5081a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
5091a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
5101a1e1d21SSam Leffler 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
5111a1e1d21SSam Leffler 			error = ENOSPC;
5121a1e1d21SSam Leffler 			break;
5131a1e1d21SSam Leffler 		}
5141a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
5151a1e1d21SSam Leffler 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
516b9ee58c4SSam Leffler 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
5171a1e1d21SSam Leffler 		error = ENETRESET;
5181a1e1d21SSam Leffler 		break;
5191a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
5201a1e1d21SSam Leffler 		return EPERM;
5211a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
5221a1e1d21SSam Leffler 		if (len != 2)
5231a1e1d21SSam Leffler 			return EINVAL;
5241a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
5251a1e1d21SSam Leffler 		if (i < 0 ||
5261a1e1d21SSam Leffler 		    i > IEEE80211_CHAN_MAX ||
5271a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, i))
5281a1e1d21SSam Leffler 			return EINVAL;
5291a1e1d21SSam Leffler 		ic->ic_ibss_chan = &ic->ic_channels[i];
5308a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
5318a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5328a1b9b6aSSam Leffler 		else
5331a1e1d21SSam Leffler 			error = ENETRESET;
5341a1e1d21SSam Leffler 		break;
5351a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
5361a1e1d21SSam Leffler 		return EPERM;
5371a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
5381a1e1d21SSam Leffler 		return EPERM;
5391a1e1d21SSam Leffler 	case WI_RID_PROMISC:
5401a1e1d21SSam Leffler 		if (len != 2)
5411a1e1d21SSam Leffler 			return EINVAL;
5421a1e1d21SSam Leffler 		if (ifp->if_flags & IFF_PROMISC) {
5431a1e1d21SSam Leffler 			if (wreq.wi_val[0] == 0) {
5441a1e1d21SSam Leffler 				ifp->if_flags &= ~IFF_PROMISC;
5451a1e1d21SSam Leffler 				error = ENETRESET;
5461a1e1d21SSam Leffler 			}
5471a1e1d21SSam Leffler 		} else {
5481a1e1d21SSam Leffler 			if (wreq.wi_val[0] != 0) {
5491a1e1d21SSam Leffler 				ifp->if_flags |= IFF_PROMISC;
5501a1e1d21SSam Leffler 				error = ENETRESET;
5511a1e1d21SSam Leffler 			}
5521a1e1d21SSam Leffler 		}
5531a1e1d21SSam Leffler 		break;
5541a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
5551a1e1d21SSam Leffler 		if (len != 2)
5561a1e1d21SSam Leffler 			return EINVAL;
5571a1e1d21SSam Leffler 		switch (le16toh(wreq.wi_val[0])) {
5581a1e1d21SSam Leffler 		case IEEE80211_M_STA:
5591a1e1d21SSam Leffler 			break;
5601a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
5611a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
5621a1e1d21SSam Leffler 				return EINVAL;
5631a1e1d21SSam Leffler 			break;
5641a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
5651a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_DS ||
5661a1e1d21SSam Leffler 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
5671a1e1d21SSam Leffler 				return EINVAL;
5681a1e1d21SSam Leffler 			break;
5691a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
5701a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
5711a1e1d21SSam Leffler 				return EINVAL;
5721a1e1d21SSam Leffler 			break;
5731a1e1d21SSam Leffler 		default:
5741a1e1d21SSam Leffler 			return EINVAL;
5751a1e1d21SSam Leffler 		}
5761a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
5771a1e1d21SSam Leffler 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
5788a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5791a1e1d21SSam Leffler 		}
5801a1e1d21SSam Leffler 		break;
5811a1e1d21SSam Leffler #if 0
5821a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
5831a1e1d21SSam Leffler 		if (len != IEEE80211_ADDR_LEN)
5841a1e1d21SSam Leffler 			return EINVAL;
5851a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
5861a1e1d21SSam Leffler 		/* if_init will copy lladdr into ic_myaddr */
5871a1e1d21SSam Leffler 		error = ENETRESET;
5881a1e1d21SSam Leffler 		break;
5891a1e1d21SSam Leffler #endif
5901a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
5911a1e1d21SSam Leffler 		if (len != 2)
5921a1e1d21SSam Leffler 			return EINVAL;
5931a1e1d21SSam Leffler 		if (wreq.wi_val[0] == 0) {
5941a1e1d21SSam Leffler 			/* auto */
5952c39b32cSSam Leffler 			ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
5961a1e1d21SSam Leffler 			break;
5971a1e1d21SSam Leffler 		}
5981a1e1d21SSam Leffler 		rate = 2 * le16toh(wreq.wi_val[0]);
5991a1e1d21SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
6001a1e1d21SSam Leffler 			/*
6011a1e1d21SSam Leffler 			 * In autoselect mode search for the rate.  We take
6021a1e1d21SSam Leffler 			 * the first instance which may not be right, but we
6031a1e1d21SSam Leffler 			 * are limited by the interface.  Note that we also
6041a1e1d21SSam Leffler 			 * lock the mode to insure the rate is meaningful
6051a1e1d21SSam Leffler 			 * when it is used.
6061a1e1d21SSam Leffler 			 */
6071a1e1d21SSam Leffler 			for (j = IEEE80211_MODE_11A;
6081a1e1d21SSam Leffler 			     j < IEEE80211_MODE_MAX; j++) {
6091a1e1d21SSam Leffler 				if ((ic->ic_modecaps & (1<<j)) == 0)
6101a1e1d21SSam Leffler 					continue;
6111a1e1d21SSam Leffler 				i = findrate(ic, j, rate);
6121a1e1d21SSam Leffler 				if (i != -1) {
6131a1e1d21SSam Leffler 					/* lock mode too */
6141a1e1d21SSam Leffler 					ic->ic_curmode = j;
6151a1e1d21SSam Leffler 					goto setrate;
6161a1e1d21SSam Leffler 				}
6171a1e1d21SSam Leffler 			}
6181a1e1d21SSam Leffler 		} else {
6191a1e1d21SSam Leffler 			i = findrate(ic, ic->ic_curmode, rate);
6201a1e1d21SSam Leffler 			if (i != -1)
6211a1e1d21SSam Leffler 				goto setrate;
6221a1e1d21SSam Leffler 		}
6231a1e1d21SSam Leffler 		return EINVAL;
6241a1e1d21SSam Leffler 	setrate:
6251a1e1d21SSam Leffler 		ic->ic_fixed_rate = i;
6268a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6271a1e1d21SSam Leffler 		break;
6281a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
6291a1e1d21SSam Leffler 		return EPERM;
6301a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
6311a1e1d21SSam Leffler 		if (len != 2)
6321a1e1d21SSam Leffler 			return EINVAL;
6331a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != IEEE80211_MAX_LEN)
6341a1e1d21SSam Leffler 			return EINVAL;		/* TODO: RTS */
6351a1e1d21SSam Leffler 		break;
6361a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
6371a1e1d21SSam Leffler 		if (len != 2)
6381a1e1d21SSam Leffler 			return EINVAL;
6391a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6401a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
6411a1e1d21SSam Leffler 				return EINVAL;
6421a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
6431a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_IBSSON;
6441a1e1d21SSam Leffler 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
6451a1e1d21SSam Leffler 				    ic->ic_state == IEEE80211_S_SCAN)
6468a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6471a1e1d21SSam Leffler 			}
6481a1e1d21SSam Leffler 		} else {
6491a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
6501a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
6511a1e1d21SSam Leffler 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
6521a1e1d21SSam Leffler 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
6538a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6541a1e1d21SSam Leffler 				}
6551a1e1d21SSam Leffler 			}
6561a1e1d21SSam Leffler 		}
6571a1e1d21SSam Leffler 		break;
6581a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
6591a1e1d21SSam Leffler 		if (len != 2)
6601a1e1d21SSam Leffler 			return EINVAL;
6611a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0)
6621a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6631a1e1d21SSam Leffler 		break;
6641a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
6651a1e1d21SSam Leffler 		if (len != 2)
6661a1e1d21SSam Leffler 			return EINVAL;
6678a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
6688a1b9b6aSSam Leffler 		if (i > IEEE80211_ROAMING_MANUAL)
6691a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6708a1b9b6aSSam Leffler 		ic->ic_roaming = i;
6711a1e1d21SSam Leffler 		break;
6721a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
6731a1e1d21SSam Leffler 		if (len != 2)
6741a1e1d21SSam Leffler 			return EINVAL;
6751a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != 1)
6761a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6771a1e1d21SSam Leffler 		break;
6781a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
6791a1e1d21SSam Leffler 		if (len != 2)
6801a1e1d21SSam Leffler 			return EINVAL;
6811a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6821a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
6831a1e1d21SSam Leffler 				return EINVAL;
6841a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
6851a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
6868a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6871a1e1d21SSam Leffler 			}
6881a1e1d21SSam Leffler 		} else {
6891a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
6901a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
6918a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6921a1e1d21SSam Leffler 			}
6931a1e1d21SSam Leffler 		}
6941a1e1d21SSam Leffler 		break;
6951a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
6961a1e1d21SSam Leffler 		if (len != 2)
6971a1e1d21SSam Leffler 			return EINVAL;
6981a1e1d21SSam Leffler 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
6991a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
7008a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7011a1e1d21SSam Leffler 		break;
7021a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
7031a1e1d21SSam Leffler 		return EPERM;
7041a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
7051a1e1d21SSam Leffler 		return EPERM;
7061a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
7071a1e1d21SSam Leffler 		if (len != 2)
7081a1e1d21SSam Leffler 			return EINVAL;
7098a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
7108a1b9b6aSSam Leffler 		if (i > IEEE80211_AUTH_WPA)
7118a1b9b6aSSam Leffler 			return EINVAL;
7128a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = i;		/* XXX ENETRESET? */
7138a1b9b6aSSam Leffler 		error = ENETRESET;
7141a1e1d21SSam Leffler 		break;
7151a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
7161a1e1d21SSam Leffler 		if (len != 2)
7171a1e1d21SSam Leffler 			return EINVAL;
7181a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
7191a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
7201a1e1d21SSam Leffler 				return EINVAL;
7218a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
7228a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_PRIVACY;
7231a1e1d21SSam Leffler 				error = ENETRESET;
7241a1e1d21SSam Leffler 			}
7251a1e1d21SSam Leffler 		} else {
7268a1b9b6aSSam Leffler 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
7278a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PRIVACY;
7281a1e1d21SSam Leffler 				error = ENETRESET;
7291a1e1d21SSam Leffler 			}
7301a1e1d21SSam Leffler 		}
7311a1e1d21SSam Leffler 		break;
7321a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
7331a1e1d21SSam Leffler 		if (len != 2)
7341a1e1d21SSam Leffler 			return EINVAL;
7351a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
7361a1e1d21SSam Leffler 		if (i >= IEEE80211_WEP_NKID)
7371a1e1d21SSam Leffler 			return EINVAL;
7388a1b9b6aSSam Leffler 		ic->ic_def_txkey = i;
7398a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7401a1e1d21SSam Leffler 		break;
7411a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
7421a1e1d21SSam Leffler 		if (len != sizeof(struct wi_ltv_keys))
7431a1e1d21SSam Leffler 			return EINVAL;
7441a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
7451a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7461a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7471a1e1d21SSam Leffler 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
7481a1e1d21SSam Leffler 				return EINVAL;
7498a1b9b6aSSam Leffler 			if (len > IEEE80211_KEYBUF_SIZE)
7501a1e1d21SSam Leffler 				return EINVAL;
7511a1e1d21SSam Leffler 		}
7521a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7538a1b9b6aSSam Leffler 			struct ieee80211_key *k = &ic->ic_nw_keys[i];
7548a1b9b6aSSam Leffler 
7551a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7568a1b9b6aSSam Leffler 			k->wk_keylen = len;
7578a1b9b6aSSam Leffler 			k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
7588a1b9b6aSSam Leffler 			memset(k->wk_key, 0, sizeof(k->wk_key));
7598a1b9b6aSSam Leffler 			memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
7608a1b9b6aSSam Leffler #if 0
7618a1b9b6aSSam Leffler 			k->wk_type = IEEE80211_CIPHER_WEP;
7628a1b9b6aSSam Leffler #endif
7631a1e1d21SSam Leffler 		}
7641a1e1d21SSam Leffler 		error = ENETRESET;
7651a1e1d21SSam Leffler 		break;
7661a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
7671a1e1d21SSam Leffler 		if (len != 2)
7681a1e1d21SSam Leffler 			return EINVAL;
7691a1e1d21SSam Leffler 		len = le16toh(wreq.wi_val[0]);
7701a1e1d21SSam Leffler 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
7711a1e1d21SSam Leffler 			return EINVAL;
7721a1e1d21SSam Leffler 		ic->ic_fragthreshold = len;
7738a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7741a1e1d21SSam Leffler 		break;
7751a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
7761a1e1d21SSam Leffler 		error = EPERM;
7771a1e1d21SSam Leffler 		break;
7781a1e1d21SSam Leffler 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
7791a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7801a1e1d21SSam Leffler 			break;
7818a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
78293685685SSam Leffler 		if (error == 0)
783a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
7841a1e1d21SSam Leffler 		break;
7851a1e1d21SSam Leffler 	case WI_RID_SCAN_APS:
7861a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7871a1e1d21SSam Leffler 			break;
7881a1e1d21SSam Leffler 		len--;			/* XXX: tx rate? */
7891a1e1d21SSam Leffler 		/* FALLTHRU */
7901a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
7911a1e1d21SSam Leffler 		memset(chanlist, 0, sizeof(chanlist));
7921a1e1d21SSam Leffler 		/*
7931a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
7941a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
7951a1e1d21SSam Leffler 		 */
7961a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
7971a1e1d21SSam Leffler 			i = 1;
7981a1e1d21SSam Leffler 		else
7991a1e1d21SSam Leffler 			i = 0;
8001a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
8011a1e1d21SSam Leffler 			if ((j / 8) >= len)
8021a1e1d21SSam Leffler 				break;
8031a1e1d21SSam Leffler 			if (isclr((u_int8_t *)wreq.wi_val, j))
8041a1e1d21SSam Leffler 				continue;
8051a1e1d21SSam Leffler 			if (isclr(ic->ic_chan_active, i)) {
8061a1e1d21SSam Leffler 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
8071a1e1d21SSam Leffler 					continue;
8081a1e1d21SSam Leffler 				if (isclr(ic->ic_chan_avail, i))
8091a1e1d21SSam Leffler 					return EPERM;
8101a1e1d21SSam Leffler 			}
8111a1e1d21SSam Leffler 			setbit(chanlist, i);
8121a1e1d21SSam Leffler 		}
8138a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, chanlist);
81493685685SSam Leffler 		if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
81593685685SSam Leffler 			/* NB: ignore error from ieee80211_setupscan */
8161a1e1d21SSam Leffler 			error = ENETRESET;
81793685685SSam Leffler 		} else if (error == 0)
818a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
8191a1e1d21SSam Leffler 		break;
8201a1e1d21SSam Leffler 	default:
8211a1e1d21SSam Leffler 		error = EINVAL;
8221a1e1d21SSam Leffler 		break;
8231a1e1d21SSam Leffler 	}
8248a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
8258a1b9b6aSSam Leffler 		error = 0;
8261a1e1d21SSam Leffler 	return error;
8271a1e1d21SSam Leffler }
8281a1e1d21SSam Leffler 
8298a1b9b6aSSam Leffler static struct ieee80211_channel *
8308a1b9b6aSSam Leffler getcurchan(struct ieee80211com *ic)
8311a1e1d21SSam Leffler {
8328a1b9b6aSSam Leffler 	switch (ic->ic_state) {
8338a1b9b6aSSam Leffler 	case IEEE80211_S_INIT:
8348a1b9b6aSSam Leffler 	case IEEE80211_S_SCAN:
8358a1b9b6aSSam Leffler 		return ic->ic_des_chan;
8368a1b9b6aSSam Leffler 	default:
8378a1b9b6aSSam Leffler 		return ic->ic_ibss_chan;
8388a1b9b6aSSam Leffler 	}
8398a1b9b6aSSam Leffler }
8408a1b9b6aSSam Leffler 
8418a1b9b6aSSam Leffler static int
8428a1b9b6aSSam Leffler cap2cipher(int flag)
8438a1b9b6aSSam Leffler {
8448a1b9b6aSSam Leffler 	switch (flag) {
8458a1b9b6aSSam Leffler 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
8468a1b9b6aSSam Leffler 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
8478a1b9b6aSSam Leffler 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
8488a1b9b6aSSam Leffler 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
8498a1b9b6aSSam Leffler 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
8508a1b9b6aSSam Leffler 	}
8518a1b9b6aSSam Leffler 	return -1;
8528a1b9b6aSSam Leffler }
8538a1b9b6aSSam Leffler 
8548a1b9b6aSSam Leffler static int
8558a1b9b6aSSam Leffler ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
8568a1b9b6aSSam Leffler {
8578a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8588a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
8598a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
8608a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
8618a1b9b6aSSam Leffler 	u_int kid;
8628a1b9b6aSSam Leffler 	int error;
8638a1b9b6aSSam Leffler 
8648a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
8658a1b9b6aSSam Leffler 		return EINVAL;
8668a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
8678a1b9b6aSSam Leffler 	if (error)
8688a1b9b6aSSam Leffler 		return error;
8698a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
8708a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
871acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
8728a1b9b6aSSam Leffler 		if (ni == NULL)
8738a1b9b6aSSam Leffler 			return EINVAL;		/* XXX */
8748a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
8758a1b9b6aSSam Leffler 	} else {
8768a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8778a1b9b6aSSam Leffler 			return EINVAL;
8788a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
8798a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
8808a1b9b6aSSam Leffler 		ni = NULL;
8818a1b9b6aSSam Leffler 	}
8828a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
8838a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
8848a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
8858a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
8868a1b9b6aSSam Leffler 	if (wk->wk_keyix == ic->ic_def_txkey)
8878a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
8888a1b9b6aSSam Leffler 	if (suser(curthread) == 0) {
8898a1b9b6aSSam Leffler 		/* NB: only root can read key data */
8908a1b9b6aSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc;
8918a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
8928a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
8938a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
8948a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
8958a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
8968a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
8978a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
8988a1b9b6aSSam Leffler 		}
8998a1b9b6aSSam Leffler 	} else {
9008a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
9018a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
9028a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
9038a1b9b6aSSam Leffler 	}
9048a1b9b6aSSam Leffler 	if (ni != NULL)
9058a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
9068a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
9078a1b9b6aSSam Leffler }
9088a1b9b6aSSam Leffler 
9098a1b9b6aSSam Leffler static int
9108a1b9b6aSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
9118a1b9b6aSSam Leffler {
9128a1b9b6aSSam Leffler 
9138a1b9b6aSSam Leffler 	if (sizeof(ic->ic_chan_active) > ireq->i_len)
9148a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
9158a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
9168a1b9b6aSSam Leffler }
9178a1b9b6aSSam Leffler 
9188a1b9b6aSSam Leffler static int
9198a1b9b6aSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
9208a1b9b6aSSam Leffler {
9218a1b9b6aSSam Leffler 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
9228a1b9b6aSSam Leffler 	int i, space;
9238a1b9b6aSSam Leffler 
9248a1b9b6aSSam Leffler 	/*
9258a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
9268a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
9278a1b9b6aSSam Leffler 	 */
9288a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
9298a1b9b6aSSam Leffler 		i = 1;
9308a1b9b6aSSam Leffler 	else
9318a1b9b6aSSam Leffler 		i = 0;
9328a1b9b6aSSam Leffler 	memset(&chans, 0, sizeof(chans));
9338a1b9b6aSSam Leffler 	for (; i <= IEEE80211_CHAN_MAX; i++)
9348a1b9b6aSSam Leffler 		if (isset(ic->ic_chan_avail, i)) {
9358a1b9b6aSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
9368a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
9378a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
9388a1b9b6aSSam Leffler 			chans.ic_nchans++;
9398a1b9b6aSSam Leffler 		}
9408a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
9418a1b9b6aSSam Leffler 			ic_chans[chans.ic_nchans]);
9428a1b9b6aSSam Leffler 	if (space > ireq->i_len)
9438a1b9b6aSSam Leffler 		space = ireq->i_len;
9448a1b9b6aSSam Leffler 	return copyout(&chans, ireq->i_data, space);
9458a1b9b6aSSam Leffler }
9468a1b9b6aSSam Leffler 
9478a1b9b6aSSam Leffler static int
9488a1b9b6aSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
9498a1b9b6aSSam Leffler {
9508a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9518a1b9b6aSSam Leffler 	struct ieee80211req_wpaie wpaie;
9528a1b9b6aSSam Leffler 	int error;
9538a1b9b6aSSam Leffler 
9548a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
9558a1b9b6aSSam Leffler 		return EINVAL;
9568a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
9578a1b9b6aSSam Leffler 	if (error != 0)
9588a1b9b6aSSam Leffler 		return error;
959acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
9608a1b9b6aSSam Leffler 	if (ni == NULL)
9618a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9628a1b9b6aSSam Leffler 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
9638a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL) {
9648a1b9b6aSSam Leffler 		int ielen = ni->ni_wpa_ie[1] + 2;
9658a1b9b6aSSam Leffler 		if (ielen > sizeof(wpaie.wpa_ie))
9668a1b9b6aSSam Leffler 			ielen = sizeof(wpaie.wpa_ie);
9678a1b9b6aSSam Leffler 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
9688a1b9b6aSSam Leffler 	}
9698a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9708a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(wpaie))
9718a1b9b6aSSam Leffler 		ireq->i_len = sizeof(wpaie);
9728a1b9b6aSSam Leffler 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
9738a1b9b6aSSam Leffler }
9748a1b9b6aSSam Leffler 
9758a1b9b6aSSam Leffler static int
9768a1b9b6aSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
9778a1b9b6aSSam Leffler {
9788a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9798a1b9b6aSSam Leffler 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
9808a1b9b6aSSam Leffler 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
9818a1b9b6aSSam Leffler 	int error;
9828a1b9b6aSSam Leffler 
9838a1b9b6aSSam Leffler 	if (ireq->i_len < off)
9848a1b9b6aSSam Leffler 		return EINVAL;
9858a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
9868a1b9b6aSSam Leffler 	if (error != 0)
9878a1b9b6aSSam Leffler 		return error;
988acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
9898a1b9b6aSSam Leffler 	if (ni == NULL)
9908a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9918a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
9928a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
9938a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
9948a1b9b6aSSam Leffler 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
9958a1b9b6aSSam Leffler 			ireq->i_len - off);
9968a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9978a1b9b6aSSam Leffler 	return error;
9988a1b9b6aSSam Leffler }
9998a1b9b6aSSam Leffler 
10008a1b9b6aSSam Leffler static void
10018a1b9b6aSSam Leffler get_scan_result(struct ieee80211req_scan_result *sr,
10028a1b9b6aSSam Leffler 	const struct ieee80211_node *ni)
10038a1b9b6aSSam Leffler {
10048a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10058a1b9b6aSSam Leffler 
10068a1b9b6aSSam Leffler 	memset(sr, 0, sizeof(*sr));
10078a1b9b6aSSam Leffler 	sr->isr_ssid_len = ni->ni_esslen;
10088a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
10098a1b9b6aSSam Leffler 		sr->isr_ie_len += 2+ni->ni_wpa_ie[1];
10108a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
10118a1b9b6aSSam Leffler 		sr->isr_ie_len += 2+ni->ni_wme_ie[1];
10128a1b9b6aSSam Leffler 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
10138a1b9b6aSSam Leffler 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
10148a1b9b6aSSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
10158a1b9b6aSSam Leffler 		sr->isr_freq = ni->ni_chan->ic_freq;
10168a1b9b6aSSam Leffler 		sr->isr_flags = ni->ni_chan->ic_flags;
10178a1b9b6aSSam Leffler 	}
10188a1b9b6aSSam Leffler 	sr->isr_rssi = ic->ic_node_getrssi(ni);
10198a1b9b6aSSam Leffler 	sr->isr_intval = ni->ni_intval;
10208a1b9b6aSSam Leffler 	sr->isr_capinfo = ni->ni_capinfo;
10218a1b9b6aSSam Leffler 	sr->isr_erp = ni->ni_erp;
10228a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
10238a1b9b6aSSam Leffler 	sr->isr_nrates = ni->ni_rates.rs_nrates;
10248a1b9b6aSSam Leffler 	if (sr->isr_nrates > 15)
10258a1b9b6aSSam Leffler 		sr->isr_nrates = 15;
10268a1b9b6aSSam Leffler 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
10278a1b9b6aSSam Leffler }
10288a1b9b6aSSam Leffler 
10298a1b9b6aSSam Leffler static int
10308a1b9b6aSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
10318a1b9b6aSSam Leffler {
10328a1b9b6aSSam Leffler 	union {
10338a1b9b6aSSam Leffler 		struct ieee80211req_scan_result res;
10348a1b9b6aSSam Leffler 		char data[512];		/* XXX shrink? */
10358a1b9b6aSSam Leffler 	} u;
10368a1b9b6aSSam Leffler 	struct ieee80211req_scan_result *sr = &u.res;
10378a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10388a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10398a1b9b6aSSam Leffler 	int error, space;
10408a1b9b6aSSam Leffler 	u_int8_t *p, *cp;
10418a1b9b6aSSam Leffler 
10428a1b9b6aSSam Leffler 	p = ireq->i_data;
10438a1b9b6aSSam Leffler 	space = ireq->i_len;
10448a1b9b6aSSam Leffler 	error = 0;
10458a1b9b6aSSam Leffler 	/* XXX locking */
10468a1b9b6aSSam Leffler 	nt =  &ic->ic_scan;
10478a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
10488a1b9b6aSSam Leffler 		/* NB: skip pre-scan node state */
10498a1b9b6aSSam Leffler 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
10508a1b9b6aSSam Leffler 			continue;
10518a1b9b6aSSam Leffler 		get_scan_result(sr, ni);
10528a1b9b6aSSam Leffler 		if (sr->isr_len > sizeof(u))
10538a1b9b6aSSam Leffler 			continue;		/* XXX */
10548a1b9b6aSSam Leffler 		if (space < sr->isr_len)
10558a1b9b6aSSam Leffler 			break;
10568a1b9b6aSSam Leffler 		cp = (u_int8_t *)(sr+1);
10578a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
10588a1b9b6aSSam Leffler 		cp += ni->ni_esslen;
10598a1b9b6aSSam Leffler 		if (ni->ni_wpa_ie != NULL) {
10608a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
10618a1b9b6aSSam Leffler 			cp += 2+ni->ni_wpa_ie[1];
10628a1b9b6aSSam Leffler 		}
10638a1b9b6aSSam Leffler 		if (ni->ni_wme_ie != NULL) {
10648a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
10658a1b9b6aSSam Leffler 			cp += 2+ni->ni_wme_ie[1];
10668a1b9b6aSSam Leffler 		}
10678a1b9b6aSSam Leffler 		error = copyout(sr, p, sr->isr_len);
10688a1b9b6aSSam Leffler 		if (error)
10698a1b9b6aSSam Leffler 			break;
10708a1b9b6aSSam Leffler 		p += sr->isr_len;
10718a1b9b6aSSam Leffler 		space -= sr->isr_len;
10728a1b9b6aSSam Leffler 	}
10738a1b9b6aSSam Leffler 	ireq->i_len -= space;
10748a1b9b6aSSam Leffler 	return error;
10758a1b9b6aSSam Leffler }
10768a1b9b6aSSam Leffler 
10778a1b9b6aSSam Leffler static void
10788a1b9b6aSSam Leffler get_sta_info(struct ieee80211req_sta_info *si, const struct ieee80211_node *ni)
10798a1b9b6aSSam Leffler {
10808a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10818a1b9b6aSSam Leffler 
10828a1b9b6aSSam Leffler 	si->isi_ie_len = 0;
10838a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
10848a1b9b6aSSam Leffler 		si->isi_ie_len += 2+ni->ni_wpa_ie[1];
10858a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
10868a1b9b6aSSam Leffler 		si->isi_ie_len += 2+ni->ni_wme_ie[1];
10878a1b9b6aSSam Leffler 	si->isi_len = sizeof(*si) + si->isi_ie_len, sizeof(u_int32_t);
10888a1b9b6aSSam Leffler 	si->isi_len = roundup(si->isi_len, sizeof(u_int32_t));
10898a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
10908a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
10918a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
10928a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
10938a1b9b6aSSam Leffler 	si->isi_rssi = ic->ic_node_getrssi(ni);
10948a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
10958a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
10968a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
10978a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
10988a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
10998a1b9b6aSSam Leffler 		si->isi_nrates = 15;
11008a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
11018a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
11028a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
11038a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
11048a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
11058a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
11068a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
11078a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
11088a1b9b6aSSam Leffler 	} else {
11098a1b9b6aSSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[0];
11108a1b9b6aSSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
11118a1b9b6aSSam Leffler 	}
11128a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS || ni->ni_associd != 0)
11138a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_run;
11148a1b9b6aSSam Leffler 	else if (ieee80211_node_is_authorized(ni))
11158a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_auth;
11168a1b9b6aSSam Leffler 	else
11178a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_init;
11188a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
11198a1b9b6aSSam Leffler }
11208a1b9b6aSSam Leffler 
11218a1b9b6aSSam Leffler static int
11228a1b9b6aSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
11238a1b9b6aSSam Leffler {
11248a1b9b6aSSam Leffler 	union {
11258a1b9b6aSSam Leffler 		struct ieee80211req_sta_info info;
11268a1b9b6aSSam Leffler 		char data[512];		/* XXX shrink? */
11278a1b9b6aSSam Leffler 	} u;
11288a1b9b6aSSam Leffler 	struct ieee80211req_sta_info *si = &u.info;
11298a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
11308a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11318a1b9b6aSSam Leffler 	int error, space;
11328a1b9b6aSSam Leffler 	u_int8_t *p, *cp;
11338a1b9b6aSSam Leffler 
1134acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
11358a1b9b6aSSam Leffler 	p = ireq->i_data;
11368a1b9b6aSSam Leffler 	space = ireq->i_len;
11378a1b9b6aSSam Leffler 	error = 0;
11388a1b9b6aSSam Leffler 	/* XXX locking */
11398a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
11408a1b9b6aSSam Leffler 		get_sta_info(si, ni);
11418a1b9b6aSSam Leffler 		if (si->isi_len > sizeof(u))
11428a1b9b6aSSam Leffler 			continue;		/* XXX */
11438a1b9b6aSSam Leffler 		if (space < si->isi_len)
11448a1b9b6aSSam Leffler 			break;
11458a1b9b6aSSam Leffler 		cp = (u_int8_t *)(si+1);
11468a1b9b6aSSam Leffler 		if (ni->ni_wpa_ie != NULL) {
11478a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
11488a1b9b6aSSam Leffler 			cp += 2+ni->ni_wpa_ie[1];
11498a1b9b6aSSam Leffler 		}
11508a1b9b6aSSam Leffler 		if (ni->ni_wme_ie != NULL) {
11518a1b9b6aSSam Leffler 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
11528a1b9b6aSSam Leffler 			cp += 2+ni->ni_wme_ie[1];
11538a1b9b6aSSam Leffler 		}
11548a1b9b6aSSam Leffler 		error = copyout(si, p, si->isi_len);
11558a1b9b6aSSam Leffler 		if (error)
11568a1b9b6aSSam Leffler 			break;
11578a1b9b6aSSam Leffler 		p += si->isi_len;
11588a1b9b6aSSam Leffler 		space -= si->isi_len;
11598a1b9b6aSSam Leffler 	}
11608a1b9b6aSSam Leffler 	ireq->i_len -= space;
11618a1b9b6aSSam Leffler 	return error;
11628a1b9b6aSSam Leffler }
11638a1b9b6aSSam Leffler 
11648a1b9b6aSSam Leffler static int
11658a1b9b6aSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
11668a1b9b6aSSam Leffler {
11678a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11688a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
11698a1b9b6aSSam Leffler 	int error;
11708a1b9b6aSSam Leffler 
11718a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
11728a1b9b6aSSam Leffler 		return EINVAL;
11738a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
11748a1b9b6aSSam Leffler 	if (error != 0)
11758a1b9b6aSSam Leffler 		return error;
1176acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
11778a1b9b6aSSam Leffler 	if (ni == NULL)
11788a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
11798a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
11808a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
11818a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
11828a1b9b6aSSam Leffler 	return error;
11838a1b9b6aSSam Leffler }
11848a1b9b6aSSam Leffler 
11858a1b9b6aSSam Leffler static int
11868a1b9b6aSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
11878a1b9b6aSSam Leffler {
11888a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
11898a1b9b6aSSam Leffler 	struct wmeParams *wmep;
11908a1b9b6aSSam Leffler 	int ac;
11918a1b9b6aSSam Leffler 
11928a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
11938a1b9b6aSSam Leffler 		return EINVAL;
11948a1b9b6aSSam Leffler 
11958a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
11968a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
11978a1b9b6aSSam Leffler 		ac = WME_AC_BE;
11988a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
11998a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
12008a1b9b6aSSam Leffler 	else
12018a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
12028a1b9b6aSSam Leffler 	switch (ireq->i_type) {
12038a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
12048a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
12058a1b9b6aSSam Leffler 		break;
12068a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
12078a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
12088a1b9b6aSSam Leffler 		break;
12098a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
12108a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
12118a1b9b6aSSam Leffler 		break;
12128a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
12138a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
12148a1b9b6aSSam Leffler 		break;
12158a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
12168a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
12178a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
12188a1b9b6aSSam Leffler 		break;
12198a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
12208a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
12218a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
12228a1b9b6aSSam Leffler 		break;
12238a1b9b6aSSam Leffler 	}
12248a1b9b6aSSam Leffler 	return 0;
12258a1b9b6aSSam Leffler }
12268a1b9b6aSSam Leffler 
1227c788ca3eSBill Paul /*
1228c788ca3eSBill Paul  * When building the kernel with -O2 on the i386 architecture, gcc
1229c788ca3eSBill Paul  * seems to want to inline this function into ieee80211_ioctl()
1230c788ca3eSBill Paul  * (which is the only routine that calls it). When this happens,
1231c788ca3eSBill Paul  * ieee80211_ioctl() ends up consuming an additional 2K of stack
1232c788ca3eSBill Paul  * space. (Exactly why it needs so much is unclear.) The problem
1233c788ca3eSBill Paul  * is that it's possible for ieee80211_ioctl() to invoke other
1234c788ca3eSBill Paul  * routines (including driver init functions) which could then find
1235c788ca3eSBill Paul  * themselves perilously close to exhausting the stack.
1236c788ca3eSBill Paul  *
1237c788ca3eSBill Paul  * To avoid this, we deliberately prevent gcc from inlining this
1238c788ca3eSBill Paul  * routine. Another way to avoid this is to use less agressive
1239c788ca3eSBill Paul  * optimization when compiling this file (i.e. -O instead of -O2)
1240c788ca3eSBill Paul  * but special-casing the compilation of this one module in the
1241c788ca3eSBill Paul  * build system would be awkward.
1242c788ca3eSBill Paul  */
1243c788ca3eSBill Paul #ifdef __GNUC__
1244c788ca3eSBill Paul __attribute__ ((noinline))
1245c788ca3eSBill Paul #endif
12468a1b9b6aSSam Leffler static int
12478a1b9b6aSSam Leffler ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
12488a1b9b6aSSam Leffler {
12498a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
12501a1e1d21SSam Leffler 	int error = 0;
12518a1b9b6aSSam Leffler 	u_int kid, len, m;
12521a1e1d21SSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
12531a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
12541a1e1d21SSam Leffler 
12551a1e1d21SSam Leffler 	switch (ireq->i_type) {
12561a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
12571a1e1d21SSam Leffler 		switch (ic->ic_state) {
12581a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
12591a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
12601a1e1d21SSam Leffler 			ireq->i_len = ic->ic_des_esslen;
12611a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
12621a1e1d21SSam Leffler 			break;
12631a1e1d21SSam Leffler 		default:
12641a1e1d21SSam Leffler 			ireq->i_len = ic->ic_bss->ni_esslen;
12651a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_bss->ni_essid,
12661a1e1d21SSam Leffler 				ireq->i_len);
12671a1e1d21SSam Leffler 			break;
12681a1e1d21SSam Leffler 		}
12691a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
12701a1e1d21SSam Leffler 		break;
12711a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
12721a1e1d21SSam Leffler 		ireq->i_val = 1;
12731a1e1d21SSam Leffler 		break;
12741a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
12758a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
12768a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
12778a1b9b6aSSam Leffler 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
12788a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
12798a1b9b6aSSam Leffler 		else
12808a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
12811a1e1d21SSam Leffler 		break;
12821a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
12831a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
12848a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12858a1b9b6aSSam Leffler 			return EINVAL;
12868a1b9b6aSSam Leffler 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
12871a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
12885c8bb90bSBrian Feldman 		if (suser(curthread) == 0) {
12891a1e1d21SSam Leffler 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
12901a1e1d21SSam Leffler 		} else {
12911a1e1d21SSam Leffler 			bzero(tmpkey, len);
12921a1e1d21SSam Leffler 		}
12931a1e1d21SSam Leffler 		ireq->i_len = len;
12941a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
12951a1e1d21SSam Leffler 		break;
12961a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
12971a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
12981a1e1d21SSam Leffler 		break;
12991a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
13008a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_def_txkey;
13011a1e1d21SSam Leffler 		break;
13021a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
13038a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
13048a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
13058a1b9b6aSSam Leffler 		else
13068a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_bss->ni_authmode;
13071a1e1d21SSam Leffler 		break;
13081a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
13098a1b9b6aSSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, getcurchan(ic));
13101a1e1d21SSam Leffler 		break;
13111a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
13121a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
13131a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
13141a1e1d21SSam Leffler 		else
13151a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
13161a1e1d21SSam Leffler 		break;
13171a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
13181a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
13191a1e1d21SSam Leffler 		break;
132013604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
13211a1e1d21SSam Leffler 		ireq->i_val = ic->ic_rtsthreshold;
13221a1e1d21SSam Leffler 		break;
13232e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
13242e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
13252e79ca97SSam Leffler 		break;
13262e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
13272e79ca97SSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
13288a1b9b6aSSam Leffler 			return EINVAL;
13298a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_txpowlimit;
13308a1b9b6aSSam Leffler 		break;
13318a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
13328a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastcipher;
13338a1b9b6aSSam Leffler 		break;
13348a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
13358a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastkeylen;
13368a1b9b6aSSam Leffler 		break;
13378a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
13388a1b9b6aSSam Leffler 		ireq->i_val = 0;
13398a1b9b6aSSam Leffler 		for (m = 0x1; m != 0; m <<= 1)
13408a1b9b6aSSam Leffler 			if (rsn->rsn_ucastcipherset & m)
13418a1b9b6aSSam Leffler 				ireq->i_val |= 1<<cap2cipher(m);
13428a1b9b6aSSam Leffler 		break;
13438a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
13448a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastcipher;
13458a1b9b6aSSam Leffler 		break;
13468a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
13478a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastkeylen;
13488a1b9b6aSSam Leffler 		break;
13498a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
13508a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_keymgmtset;
13518a1b9b6aSSam Leffler 		break;
13528a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
13538a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_caps;
13548a1b9b6aSSam Leffler 		break;
13558a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
13568a1b9b6aSSam Leffler 		switch (ic->ic_flags & IEEE80211_F_WPA) {
13578a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
13588a1b9b6aSSam Leffler 			ireq->i_val = 1;
13598a1b9b6aSSam Leffler 			break;
13608a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
13618a1b9b6aSSam Leffler 			ireq->i_val = 2;
13628a1b9b6aSSam Leffler 			break;
13638a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
13648a1b9b6aSSam Leffler 			ireq->i_val = 3;
13658a1b9b6aSSam Leffler 			break;
13668a1b9b6aSSam Leffler 		default:
13678a1b9b6aSSam Leffler 			ireq->i_val = 0;
13688a1b9b6aSSam Leffler 			break;
13698a1b9b6aSSam Leffler 		}
13708a1b9b6aSSam Leffler 		break;
13718a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
13728a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchanlist(ic, ireq);
13738a1b9b6aSSam Leffler 		break;
13748a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
13758a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_roaming;
13768a1b9b6aSSam Leffler 		break;
13778a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
13788a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
13798a1b9b6aSSam Leffler 		break;
13808a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
13818a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
13828a1b9b6aSSam Leffler 		break;
13838a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
13848a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
13858a1b9b6aSSam Leffler 		break;
13868a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
13878a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_caps>>16;
13888a1b9b6aSSam Leffler 		ireq->i_len = ic->ic_caps&0xffff;
13898a1b9b6aSSam Leffler 		break;
13908a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
13918a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
13928a1b9b6aSSam Leffler 		break;
13938a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
13948a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
13958a1b9b6aSSam Leffler 		break;
13968a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
13978a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
13988a1b9b6aSSam Leffler 		break;
13998a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
14008a1b9b6aSSam Leffler 		if (ic->ic_opt_ie == NULL)
14018a1b9b6aSSam Leffler 			return EINVAL;
14028a1b9b6aSSam Leffler 		/* NB: truncate, caller can check length */
14038a1b9b6aSSam Leffler 		if (ireq->i_len > ic->ic_opt_ie_len)
14048a1b9b6aSSam Leffler 			ireq->i_len = ic->ic_opt_ie_len;
14058a1b9b6aSSam Leffler 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
14068a1b9b6aSSam Leffler 		break;
14078a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
14088a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getkey(ic, ireq);
14098a1b9b6aSSam Leffler 		break;
14108a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
14118a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
14128a1b9b6aSSam Leffler 		break;
14138a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
14148a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
14158a1b9b6aSSam Leffler 			return EINVAL;
14168a1b9b6aSSam Leffler 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
14178a1b9b6aSSam Leffler 					ic->ic_bss->ni_bssid :
14188a1b9b6aSSam Leffler 					ic->ic_des_bssid,
14198a1b9b6aSSam Leffler 				ireq->i_data, ireq->i_len);
14208a1b9b6aSSam Leffler 		break;
14218a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
14228a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwpaie(ic, ireq);
14238a1b9b6aSSam Leffler 		break;
14248a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
14258a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getscanresults(ic, ireq);
14268a1b9b6aSSam Leffler 		break;
14278a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
14288a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstastats(ic, ireq);
14298a1b9b6aSSam Leffler 		break;
14308a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
14318a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_txpower;
14328a1b9b6aSSam Leffler 		break;
14338a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
14348a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
14358a1b9b6aSSam Leffler 		break;
14368a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
14378a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstainfo(ic, ireq);
14388a1b9b6aSSam Leffler 		break;
14398a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
14408a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
14418a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
14428a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
14438a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
14448a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
14458a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
14468a1b9b6aSSam Leffler 		break;
14478a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
14488a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_dtim_period;
14498a1b9b6aSSam Leffler 		break;
14508a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
14518a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
14528a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_intval;
14532e79ca97SSam Leffler 		break;
1454c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
1455c4f040c3SSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
1456c4f040c3SSam Leffler 		break;
14571a1e1d21SSam Leffler 	default:
14581a1e1d21SSam Leffler 		error = EINVAL;
1459b2e95691SSam Leffler 		break;
14601a1e1d21SSam Leffler 	}
14618a1b9b6aSSam Leffler 	return error;
14628a1b9b6aSSam Leffler }
14638a1b9b6aSSam Leffler 
14648a1b9b6aSSam Leffler static int
14658a1b9b6aSSam Leffler ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
14668a1b9b6aSSam Leffler {
14678a1b9b6aSSam Leffler 	int error;
14688a1b9b6aSSam Leffler 	void *ie;
14698a1b9b6aSSam Leffler 
14708a1b9b6aSSam Leffler 	/*
14718a1b9b6aSSam Leffler 	 * NB: Doing this for ap operation could be useful (e.g. for
14728a1b9b6aSSam Leffler 	 *     WPA and/or WME) except that it typically is worthless
14738a1b9b6aSSam Leffler 	 *     without being able to intervene when processing
14748a1b9b6aSSam Leffler 	 *     association response frames--so disallow it for now.
14758a1b9b6aSSam Leffler 	 */
14768a1b9b6aSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
14778a1b9b6aSSam Leffler 		return EINVAL;
14788a1b9b6aSSam Leffler 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
14798a1b9b6aSSam Leffler 		return EINVAL;
14808a1b9b6aSSam Leffler 	/* NB: data.length is validated by the wireless extensions code */
14818a1b9b6aSSam Leffler 	MALLOC(ie, void *, ireq->i_len, M_DEVBUF, M_WAITOK);
14828a1b9b6aSSam Leffler 	if (ie == NULL)
14838a1b9b6aSSam Leffler 		return ENOMEM;
14848a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, ie, ireq->i_len);
14858a1b9b6aSSam Leffler 	/* XXX sanity check data? */
14868a1b9b6aSSam Leffler 	if (ic->ic_opt_ie != NULL)
14878a1b9b6aSSam Leffler 		FREE(ic->ic_opt_ie, M_DEVBUF);
14888a1b9b6aSSam Leffler 	ic->ic_opt_ie = ie;
14898a1b9b6aSSam Leffler 	ic->ic_opt_ie_len = ireq->i_len;
14908a1b9b6aSSam Leffler 	return 0;
14918a1b9b6aSSam Leffler }
14928a1b9b6aSSam Leffler 
14938a1b9b6aSSam Leffler static int
14948a1b9b6aSSam Leffler ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
14958a1b9b6aSSam Leffler {
14968a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
14978a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14988a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
14998a1b9b6aSSam Leffler 	u_int16_t kid;
15008a1b9b6aSSam Leffler 	int error;
15018a1b9b6aSSam Leffler 
15028a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
15038a1b9b6aSSam Leffler 		return EINVAL;
15048a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
15051a1e1d21SSam Leffler 	if (error)
15068a1b9b6aSSam Leffler 		return error;
15078a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
15088a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
15098a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
15108a1b9b6aSSam Leffler 		return E2BIG;
15118a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
15128a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
15138a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
15148a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
15158a1b9b6aSSam Leffler 			return EINVAL;
15168a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1517b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1518b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1519b5d4660fSSam Leffler 				ieee80211_free_node(ni);
15208a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1521b5d4660fSSam Leffler 			}
15228a1b9b6aSSam Leffler 		} else {
1523acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
15248a1b9b6aSSam Leffler 			if (ni == NULL)
15258a1b9b6aSSam Leffler 				return ENOENT;
15268a1b9b6aSSam Leffler 		}
15278a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
15288a1b9b6aSSam Leffler 	} else {
15298a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
15308a1b9b6aSSam Leffler 			return EINVAL;
15318a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
15328a1b9b6aSSam Leffler 		ni = NULL;
15338a1b9b6aSSam Leffler 	}
15348a1b9b6aSSam Leffler 	error = 0;
15358a1b9b6aSSam Leffler 	ieee80211_key_update_begin(ic);
1536dd70e17bSSam Leffler 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
15378a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
15388a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
15398a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
15408a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
15418a1b9b6aSSam Leffler 		wk->wk_keyrsc = ik.ik_keyrsc;
15428a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
15438a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
15448a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
15458a1b9b6aSSam Leffler 		if (!ieee80211_crypto_setkey(ic, wk,
15468a1b9b6aSSam Leffler 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
15478a1b9b6aSSam Leffler 			error = EIO;
15488a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
15498a1b9b6aSSam Leffler 			ic->ic_def_txkey = kid;
15508a1b9b6aSSam Leffler 	} else
15518a1b9b6aSSam Leffler 		error = ENXIO;
15528a1b9b6aSSam Leffler 	ieee80211_key_update_end(ic);
15538a1b9b6aSSam Leffler 	if (ni != NULL)
15548a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
15558a1b9b6aSSam Leffler 	return error;
15568a1b9b6aSSam Leffler }
15578a1b9b6aSSam Leffler 
15588a1b9b6aSSam Leffler static int
15598a1b9b6aSSam Leffler ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
15608a1b9b6aSSam Leffler {
15618a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
15628a1b9b6aSSam Leffler 	int kid, error;
15638a1b9b6aSSam Leffler 
15648a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
15658a1b9b6aSSam Leffler 		return EINVAL;
15668a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
15678a1b9b6aSSam Leffler 	if (error)
15688a1b9b6aSSam Leffler 		return error;
15698a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
15708a1b9b6aSSam Leffler 	/* XXX u_int8_t -> u_int16_t */
15718a1b9b6aSSam Leffler 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
15728a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
15738a1b9b6aSSam Leffler 
1574b5d4660fSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1575b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1576b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1577b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1578b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1579b5d4660fSSam Leffler 			}
1580b5d4660fSSam Leffler 		} else {
1581acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
15828a1b9b6aSSam Leffler 			if (ni == NULL)
1583b5d4660fSSam Leffler 				return ENOENT;
1584b5d4660fSSam Leffler 		}
15858a1b9b6aSSam Leffler 		/* XXX error return */
15868a1b9b6aSSam Leffler 		ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
15878a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
15888a1b9b6aSSam Leffler 	} else {
15898a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
15908a1b9b6aSSam Leffler 			return EINVAL;
15918a1b9b6aSSam Leffler 		/* XXX error return */
15928a1b9b6aSSam Leffler 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
15938a1b9b6aSSam Leffler 	}
15948a1b9b6aSSam Leffler 	return 0;
15958a1b9b6aSSam Leffler }
15968a1b9b6aSSam Leffler 
15978a1b9b6aSSam Leffler static void
15988a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
15998a1b9b6aSSam Leffler {
16008a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
16018a1b9b6aSSam Leffler 	struct ieee80211req_mlme *mlme = arg;
16028a1b9b6aSSam Leffler 
16038a1b9b6aSSam Leffler 	if (ni->ni_associd != 0) {
16048a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
16058a1b9b6aSSam Leffler 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
16068a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH :
16078a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DISASSOC,
16088a1b9b6aSSam Leffler 			mlme->im_reason);
16098a1b9b6aSSam Leffler 	}
16108a1b9b6aSSam Leffler 	ieee80211_node_leave(ic, ni);
16118a1b9b6aSSam Leffler }
16128a1b9b6aSSam Leffler 
16138a1b9b6aSSam Leffler static int
16148a1b9b6aSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
16158a1b9b6aSSam Leffler {
16168a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
16178a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16188a1b9b6aSSam Leffler 	int error;
16198a1b9b6aSSam Leffler 
16208a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
16218a1b9b6aSSam Leffler 		return EINVAL;
16228a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
16238a1b9b6aSSam Leffler 	if (error)
16248a1b9b6aSSam Leffler 		return error;
16258a1b9b6aSSam Leffler 	switch (mlme.im_op) {
16268a1b9b6aSSam Leffler 	case IEEE80211_MLME_ASSOC:
16278a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
16288a1b9b6aSSam Leffler 			return EINVAL;
16298a1b9b6aSSam Leffler 		/* XXX must be in S_SCAN state? */
16308a1b9b6aSSam Leffler 
1631f02a0bd2SSam Leffler 		if (mlme.im_ssid_len != 0) {
16328a1b9b6aSSam Leffler 			/*
16338a1b9b6aSSam Leffler 			 * Desired ssid specified; must match both bssid and
16348a1b9b6aSSam Leffler 			 * ssid to distinguish ap advertising multiple ssid's.
16358a1b9b6aSSam Leffler 			 */
16368a1b9b6aSSam Leffler 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
16378a1b9b6aSSam Leffler 				mlme.im_macaddr,
1638f02a0bd2SSam Leffler 				mlme.im_ssid_len, mlme.im_ssid);
16398a1b9b6aSSam Leffler 		} else {
16408a1b9b6aSSam Leffler 			/*
16418a1b9b6aSSam Leffler 			 * Normal case; just match bssid.
16428a1b9b6aSSam Leffler 			 */
16438a1b9b6aSSam Leffler 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
16448a1b9b6aSSam Leffler 		}
16458a1b9b6aSSam Leffler 		if (ni == NULL)
16468a1b9b6aSSam Leffler 			return EINVAL;
16478a1b9b6aSSam Leffler 		if (!ieee80211_sta_join(ic, ni)) {
16488a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
16498a1b9b6aSSam Leffler 			return EINVAL;
16508a1b9b6aSSam Leffler 		}
16511a1e1d21SSam Leffler 		break;
16528a1b9b6aSSam Leffler 	case IEEE80211_MLME_DISASSOC:
16538a1b9b6aSSam Leffler 	case IEEE80211_MLME_DEAUTH:
16548a1b9b6aSSam Leffler 		switch (ic->ic_opmode) {
16558a1b9b6aSSam Leffler 		case IEEE80211_M_STA:
16568a1b9b6aSSam Leffler 			/* XXX not quite right */
16578a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_INIT,
16588a1b9b6aSSam Leffler 				mlme.im_reason);
16598a1b9b6aSSam Leffler 			break;
16608a1b9b6aSSam Leffler 		case IEEE80211_M_HOSTAP:
16618a1b9b6aSSam Leffler 			/* NB: the broadcast address means do 'em all */
16628a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
1663acc4f7f5SSam Leffler 				if ((ni = ieee80211_find_node(&ic->ic_sta,
16648a1b9b6aSSam Leffler 						mlme.im_macaddr)) == NULL)
16658a1b9b6aSSam Leffler 					return EINVAL;
16668a1b9b6aSSam Leffler 				domlme(&mlme, ni);
16678a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
16688a1b9b6aSSam Leffler 			} else {
1669acc4f7f5SSam Leffler 				ieee80211_iterate_nodes(&ic->ic_sta,
16708a1b9b6aSSam Leffler 						domlme, &mlme);
16718a1b9b6aSSam Leffler 			}
16728a1b9b6aSSam Leffler 			break;
16738a1b9b6aSSam Leffler 		default:
16748a1b9b6aSSam Leffler 			return EINVAL;
16758a1b9b6aSSam Leffler 		}
16768a1b9b6aSSam Leffler 		break;
16778a1b9b6aSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
16788a1b9b6aSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
16798a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
16808a1b9b6aSSam Leffler 			return EINVAL;
1681acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
16828a1b9b6aSSam Leffler 		if (ni == NULL)
16838a1b9b6aSSam Leffler 			return EINVAL;
16848a1b9b6aSSam Leffler 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
16858a1b9b6aSSam Leffler 			ieee80211_node_authorize(ic, ni);
16868a1b9b6aSSam Leffler 		else
16878a1b9b6aSSam Leffler 			ieee80211_node_unauthorize(ic, ni);
16888a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
16898a1b9b6aSSam Leffler 		break;
16908a1b9b6aSSam Leffler 	default:
16918a1b9b6aSSam Leffler 		return EINVAL;
16928a1b9b6aSSam Leffler 	}
16938a1b9b6aSSam Leffler 	return 0;
16948a1b9b6aSSam Leffler }
16958a1b9b6aSSam Leffler 
16968a1b9b6aSSam Leffler static int
16978a1b9b6aSSam Leffler ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
16988a1b9b6aSSam Leffler {
16998a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN];
17008a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
17018a1b9b6aSSam Leffler 	int error;
17028a1b9b6aSSam Leffler 
17038a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
17048a1b9b6aSSam Leffler 		return EINVAL;
17058a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
17068a1b9b6aSSam Leffler 	if (error)
17078a1b9b6aSSam Leffler 		return error;
17088a1b9b6aSSam Leffler 	if (acl == NULL) {
17098a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
17108a1b9b6aSSam Leffler 		if (acl == NULL || !acl->iac_attach(ic))
17118a1b9b6aSSam Leffler 			return EINVAL;
17128a1b9b6aSSam Leffler 		ic->ic_acl = acl;
17138a1b9b6aSSam Leffler 	}
17148a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
17158a1b9b6aSSam Leffler 		acl->iac_add(ic, mac);
17168a1b9b6aSSam Leffler 	else
17178a1b9b6aSSam Leffler 		acl->iac_remove(ic, mac);
17188a1b9b6aSSam Leffler 	return 0;
17198a1b9b6aSSam Leffler }
17208a1b9b6aSSam Leffler 
17218a1b9b6aSSam Leffler static int
17228a1b9b6aSSam Leffler ieee80211_ioctl_maccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
17238a1b9b6aSSam Leffler {
17248a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
17258a1b9b6aSSam Leffler 
17268a1b9b6aSSam Leffler 	switch (ireq->i_val) {
17278a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
17288a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
17298a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
17308a1b9b6aSSam Leffler 		if (acl == NULL) {
17318a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
17328a1b9b6aSSam Leffler 			if (acl == NULL || !acl->iac_attach(ic))
17338a1b9b6aSSam Leffler 				return EINVAL;
17348a1b9b6aSSam Leffler 			ic->ic_acl = acl;
17358a1b9b6aSSam Leffler 		}
17368a1b9b6aSSam Leffler 		acl->iac_setpolicy(ic, ireq->i_val);
17378a1b9b6aSSam Leffler 		break;
17388a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
17398a1b9b6aSSam Leffler 		if (acl != NULL)
17408a1b9b6aSSam Leffler 			acl->iac_flush(ic);
17418a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
17428a1b9b6aSSam Leffler 		break;
17438a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
17448a1b9b6aSSam Leffler 		if (acl != NULL) {
17458a1b9b6aSSam Leffler 			ic->ic_acl = NULL;
17468a1b9b6aSSam Leffler 			acl->iac_detach(ic);
17478a1b9b6aSSam Leffler 		}
17488a1b9b6aSSam Leffler 		break;
17498a1b9b6aSSam Leffler 	default:
17508a1b9b6aSSam Leffler 		return EINVAL;
17518a1b9b6aSSam Leffler 	}
17528a1b9b6aSSam Leffler 	return 0;
17538a1b9b6aSSam Leffler }
17548a1b9b6aSSam Leffler 
17558a1b9b6aSSam Leffler static int
17568a1b9b6aSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
17578a1b9b6aSSam Leffler {
17588a1b9b6aSSam Leffler 	struct ieee80211req_chanlist list;
17598a1b9b6aSSam Leffler 	u_char chanlist[IEEE80211_CHAN_BYTES];
17608a1b9b6aSSam Leffler 	int i, j, error;
17618a1b9b6aSSam Leffler 
17628a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(list))
17638a1b9b6aSSam Leffler 		return EINVAL;
17648a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &list, sizeof(list));
17658a1b9b6aSSam Leffler 	if (error)
17668a1b9b6aSSam Leffler 		return error;
17678a1b9b6aSSam Leffler 	memset(chanlist, 0, sizeof(chanlist));
17688a1b9b6aSSam Leffler 	/*
17698a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
17708a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
17718a1b9b6aSSam Leffler 	 */
17728a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
17738a1b9b6aSSam Leffler 		i = 1;
17748a1b9b6aSSam Leffler 	else
17758a1b9b6aSSam Leffler 		i = 0;
17768a1b9b6aSSam Leffler 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
17778a1b9b6aSSam Leffler 		/*
17788a1b9b6aSSam Leffler 		 * NB: silently discard unavailable channels so users
17798a1b9b6aSSam Leffler 		 *     can specify 1-255 to get all available channels.
17808a1b9b6aSSam Leffler 		 */
17818a1b9b6aSSam Leffler 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
17828a1b9b6aSSam Leffler 			setbit(chanlist, i);
17838a1b9b6aSSam Leffler 	}
17848a1b9b6aSSam Leffler 	if (ic->ic_ibss_chan == NULL ||
17858a1b9b6aSSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
17868a1b9b6aSSam Leffler 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
17878a1b9b6aSSam Leffler 			if (isset(chanlist, i)) {
17888a1b9b6aSSam Leffler 				ic->ic_ibss_chan = &ic->ic_channels[i];
17898a1b9b6aSSam Leffler 				goto found;
17908a1b9b6aSSam Leffler 			}
17918a1b9b6aSSam Leffler 		return EINVAL;			/* no active channels */
17928a1b9b6aSSam Leffler found:
17938a1b9b6aSSam Leffler 		;
17948a1b9b6aSSam Leffler 	}
17958a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
17968a1b9b6aSSam Leffler 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
17978a1b9b6aSSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
17988a1b9b6aSSam Leffler 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
17998a1b9b6aSSam Leffler 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
18008a1b9b6aSSam Leffler }
18018a1b9b6aSSam Leffler 
18028a1b9b6aSSam Leffler static int
18038a1b9b6aSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
18048a1b9b6aSSam Leffler {
18058a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
18068a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
18078a1b9b6aSSam Leffler 	int error;
18088a1b9b6aSSam Leffler 
18098a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
18108a1b9b6aSSam Leffler 		return EINVAL;
18118a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
18128a1b9b6aSSam Leffler 	if (error != 0)
18138a1b9b6aSSam Leffler 		return error;
1814acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
18158a1b9b6aSSam Leffler 	if (ni == NULL)
18168a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
18178a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
18188a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
18198a1b9b6aSSam Leffler 	return error;
18208a1b9b6aSSam Leffler }
18218a1b9b6aSSam Leffler 
18228a1b9b6aSSam Leffler static int
18238a1b9b6aSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
18248a1b9b6aSSam Leffler {
18258a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
18268a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
18278a1b9b6aSSam Leffler 	int isbss, ac;
18288a1b9b6aSSam Leffler 
18298a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
18308a1b9b6aSSam Leffler 		return EINVAL;
18318a1b9b6aSSam Leffler 
18328a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
18338a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
18348a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
18358a1b9b6aSSam Leffler 		ac = WME_AC_BE;
18368a1b9b6aSSam Leffler 	if (isbss) {
18378a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
18388a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
18398a1b9b6aSSam Leffler 	} else {
18408a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
18418a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
18428a1b9b6aSSam Leffler 	}
18438a1b9b6aSSam Leffler 	switch (ireq->i_type) {
18448a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
18458a1b9b6aSSam Leffler 		if (isbss) {
18468a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = ireq->i_val;
18478a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18488a1b9b6aSSam Leffler 				chanp->wmep_logcwmin = ireq->i_val;
18498a1b9b6aSSam Leffler 		} else {
18508a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
18518a1b9b6aSSam Leffler 				ireq->i_val;
18528a1b9b6aSSam Leffler 		}
18538a1b9b6aSSam Leffler 		break;
18548a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
18558a1b9b6aSSam Leffler 		if (isbss) {
18568a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = ireq->i_val;
18578a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18588a1b9b6aSSam Leffler 				chanp->wmep_logcwmax = ireq->i_val;
18598a1b9b6aSSam Leffler 		} else {
18608a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
18618a1b9b6aSSam Leffler 				ireq->i_val;
18628a1b9b6aSSam Leffler 		}
18638a1b9b6aSSam Leffler 		break;
18648a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18658a1b9b6aSSam Leffler 		if (isbss) {
18668a1b9b6aSSam Leffler 			wmep->wmep_aifsn = ireq->i_val;
18678a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18688a1b9b6aSSam Leffler 				chanp->wmep_aifsn = ireq->i_val;
18698a1b9b6aSSam Leffler 		} else {
18708a1b9b6aSSam Leffler 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
18718a1b9b6aSSam Leffler 		}
18728a1b9b6aSSam Leffler 		break;
18738a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18748a1b9b6aSSam Leffler 		if (isbss) {
18758a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = ireq->i_val;
18768a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18778a1b9b6aSSam Leffler 				chanp->wmep_txopLimit = ireq->i_val;
18788a1b9b6aSSam Leffler 		} else {
18798a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
18808a1b9b6aSSam Leffler 				ireq->i_val;
18818a1b9b6aSSam Leffler 		}
18828a1b9b6aSSam Leffler 		break;
18838a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
18848a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
18858a1b9b6aSSam Leffler 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18868a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
18878a1b9b6aSSam Leffler 		break;
18888a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
18898a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
18908a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
18918a1b9b6aSSam Leffler 		break;
18928a1b9b6aSSam Leffler 	}
18938a1b9b6aSSam Leffler 	ieee80211_wme_updateparams(ic);
18948a1b9b6aSSam Leffler 	return 0;
18958a1b9b6aSSam Leffler }
18968a1b9b6aSSam Leffler 
18978a1b9b6aSSam Leffler static int
18988a1b9b6aSSam Leffler cipher2cap(int cipher)
18998a1b9b6aSSam Leffler {
19008a1b9b6aSSam Leffler 	switch (cipher) {
19018a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
19028a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
19038a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
19048a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
19058a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
19068a1b9b6aSSam Leffler 	}
19078a1b9b6aSSam Leffler 	return 0;
19088a1b9b6aSSam Leffler }
19098a1b9b6aSSam Leffler 
19108a1b9b6aSSam Leffler static int
19118a1b9b6aSSam Leffler ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
19128a1b9b6aSSam Leffler {
19138a1b9b6aSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
19148a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
19158a1b9b6aSSam Leffler 	int error;
19168a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
19178a1b9b6aSSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
19188a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
19198a1b9b6aSSam Leffler 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
19208a1b9b6aSSam Leffler 	struct ieee80211_key *k;
19218a1b9b6aSSam Leffler 	int j, caps;
19228a1b9b6aSSam Leffler 	u_int kid;
19238a1b9b6aSSam Leffler 
19248a1b9b6aSSam Leffler 	error = 0;
19251a1e1d21SSam Leffler 	switch (ireq->i_type) {
19261a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
19271a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
19288a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
19298a1b9b6aSSam Leffler 			return EINVAL;
19301a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
19311a1e1d21SSam Leffler 		if (error)
19321a1e1d21SSam Leffler 			break;
19331a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
19341a1e1d21SSam Leffler 		ic->ic_des_esslen = ireq->i_len;
19351a1e1d21SSam Leffler 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
19361a1e1d21SSam Leffler 		error = ENETRESET;
19371a1e1d21SSam Leffler 		break;
19381a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
19398a1b9b6aSSam Leffler 		switch (ireq->i_val) {
19408a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
19418a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
19428a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
19438a1b9b6aSSam Leffler 			break;
19448a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
19458a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
19468a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
19478a1b9b6aSSam Leffler 			break;
19488a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
19498a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
19508a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
19518a1b9b6aSSam Leffler 			break;
19521a1e1d21SSam Leffler 		}
19531a1e1d21SSam Leffler 		error = ENETRESET;
19541a1e1d21SSam Leffler 		break;
19551a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
19561a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
19578a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
19588a1b9b6aSSam Leffler 			return EINVAL;
19598a1b9b6aSSam Leffler 		k = &ic->ic_nw_keys[kid];
19608a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
19618a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
19628a1b9b6aSSam Leffler 			(void) ieee80211_crypto_delkey(ic, k);
19631a1e1d21SSam Leffler 			break;
19641a1e1d21SSam Leffler 		}
19658a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
19668a1b9b6aSSam Leffler 			return EINVAL;
19671a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
19681a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
19691a1e1d21SSam Leffler 		if (error)
19701a1e1d21SSam Leffler 			break;
19718a1b9b6aSSam Leffler 		ieee80211_key_update_begin(ic);
1972dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
1973dd70e17bSSam Leffler 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
1974dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
19758a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
19768a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
19778a1b9b6aSSam Leffler 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
19788a1b9b6aSSam Leffler 				error = EINVAL;
19798a1b9b6aSSam Leffler 		} else
19808a1b9b6aSSam Leffler 			error = EINVAL;
19818a1b9b6aSSam Leffler 		ieee80211_key_update_end(ic);
1982b5d4660fSSam Leffler 		if (!error)			/* NB: for compatibility */
1983b5d4660fSSam Leffler 			error = ENETRESET;
19841a1e1d21SSam Leffler 		break;
19851a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
19861a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
1987380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
1988380c5fa9SSam Leffler 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
19898a1b9b6aSSam Leffler 			return EINVAL;
19908a1b9b6aSSam Leffler 		ic->ic_def_txkey = kid;
1991b8d2606bSSam Leffler 		error = ENETRESET;	/* push to hardware */
19928a1b9b6aSSam Leffler 		break;
19938a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
19948a1b9b6aSSam Leffler 		switch (ireq->i_val) {
19958a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
19968a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
19978a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
19988a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
19998a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
20008a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
20018a1b9b6aSSam Leffler 			if (auth == NULL)
20028a1b9b6aSSam Leffler 				return EINVAL;
20038a1b9b6aSSam Leffler 			break;
20048a1b9b6aSSam Leffler 		default:
20058a1b9b6aSSam Leffler 			return EINVAL;
20068a1b9b6aSSam Leffler 		}
20078a1b9b6aSSam Leffler 		switch (ireq->i_val) {
20088a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
20098a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
20108a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
20118a1b9b6aSSam Leffler 			break;
20128a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
20138a1b9b6aSSam Leffler 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
20148a1b9b6aSSam Leffler 			break;
20158a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
20168a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
20178a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
20188a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
20198a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
20208a1b9b6aSSam Leffler 			break;
20218a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
20228a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
20238a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
20248a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
20251a1e1d21SSam Leffler 			break;
20261a1e1d21SSam Leffler 		}
20278a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
20288a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = ireq->i_val;
20298a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
20308a1b9b6aSSam Leffler 		ic->ic_auth = auth;
20311a1e1d21SSam Leffler 		error = ENETRESET;
20321a1e1d21SSam Leffler 		break;
20331a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
20341a1e1d21SSam Leffler 		/* XXX 0xffff overflows 16-bit signed */
20351a1e1d21SSam Leffler 		if (ireq->i_val == 0 ||
20361a1e1d21SSam Leffler 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
20371a1e1d21SSam Leffler 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
20381a1e1d21SSam Leffler 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
20391a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, ireq->i_val)) {
20408a1b9b6aSSam Leffler 			return EINVAL;
20411a1e1d21SSam Leffler 		} else
20421a1e1d21SSam Leffler 			ic->ic_ibss_chan = ic->ic_des_chan =
20431a1e1d21SSam Leffler 				&ic->ic_channels[ireq->i_val];
20441a1e1d21SSam Leffler 		switch (ic->ic_state) {
20451a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
20461a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
20471a1e1d21SSam Leffler 			error = ENETRESET;
20481a1e1d21SSam Leffler 			break;
20491a1e1d21SSam Leffler 		default:
20508a1b9b6aSSam Leffler 			/*
20518a1b9b6aSSam Leffler 			 * If the desired channel has changed (to something
20528a1b9b6aSSam Leffler 			 * other than any) and we're not already scanning,
20538a1b9b6aSSam Leffler 			 * then kick the state machine.
20548a1b9b6aSSam Leffler 			 */
20551a1e1d21SSam Leffler 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
20568a1b9b6aSSam Leffler 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
20578a1b9b6aSSam Leffler 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
20581a1e1d21SSam Leffler 				error = ENETRESET;
20591a1e1d21SSam Leffler 			break;
20601a1e1d21SSam Leffler 		}
20618a1b9b6aSSam Leffler 		if (error == ENETRESET && ic->ic_opmode == IEEE80211_M_MONITOR)
20628a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
20631a1e1d21SSam Leffler 		break;
20641a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
20651a1e1d21SSam Leffler 		switch (ireq->i_val) {
20661a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
20671a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
20681a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
20691a1e1d21SSam Leffler 				error = ENETRESET;
20701a1e1d21SSam Leffler 			}
20711a1e1d21SSam Leffler 			break;
20721a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
20731a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
20741a1e1d21SSam Leffler 				error = EINVAL;
20751a1e1d21SSam Leffler 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
20761a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
20771a1e1d21SSam Leffler 				error = ENETRESET;
20781a1e1d21SSam Leffler 			}
20791a1e1d21SSam Leffler 			break;
20801a1e1d21SSam Leffler 		default:
20811a1e1d21SSam Leffler 			error = EINVAL;
20821a1e1d21SSam Leffler 			break;
20831a1e1d21SSam Leffler 		}
20841a1e1d21SSam Leffler 		break;
20851a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
20868a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
20878a1b9b6aSSam Leffler 			return EINVAL;
20881a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
20898a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
20901a1e1d21SSam Leffler 		break;
209113604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
20921a1e1d21SSam Leffler 		if (!(IEEE80211_RTS_MIN < ireq->i_val &&
20938a1b9b6aSSam Leffler 		      ireq->i_val < IEEE80211_RTS_MAX))
20948a1b9b6aSSam Leffler 			return EINVAL;
20951a1e1d21SSam Leffler 		ic->ic_rtsthreshold = ireq->i_val;
20968a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
20971a1e1d21SSam Leffler 		break;
20982e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
20998a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
21008a1b9b6aSSam Leffler 			return EINVAL;
21012e79ca97SSam Leffler 		ic->ic_protmode = ireq->i_val;
21022e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
21032e79ca97SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
21048a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21052e79ca97SSam Leffler 		break;
21062e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
21078a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
21088a1b9b6aSSam Leffler 			return EINVAL;
21092e79ca97SSam Leffler 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
21108a1b9b6aSSam Leffler 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
21118a1b9b6aSSam Leffler 			return EINVAL;
21128a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
21138a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
21148a1b9b6aSSam Leffler 		break;
21158a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
21168a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
21178a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
21188a1b9b6aSSam Leffler 			return EINVAL;
21198a1b9b6aSSam Leffler 		ic->ic_roaming = ireq->i_val;
21208a1b9b6aSSam Leffler 		/* XXXX reset? */
21218a1b9b6aSSam Leffler 		break;
21228a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
21238a1b9b6aSSam Leffler 		if (ireq->i_val) {
21248a1b9b6aSSam Leffler 			/* XXX check for key state? */
21258a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
21268a1b9b6aSSam Leffler 		} else
21278a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
21288a1b9b6aSSam Leffler 		break;
21298a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
21308a1b9b6aSSam Leffler 		if (ireq->i_val)
21318a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
21328a1b9b6aSSam Leffler 		else
21338a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
21348a1b9b6aSSam Leffler 		break;
21358a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
21368a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setkey(ic, ireq);
21378a1b9b6aSSam Leffler 		break;
21388a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
21398a1b9b6aSSam Leffler 		error = ieee80211_ioctl_delkey(ic, ireq);
21408a1b9b6aSSam Leffler 		break;
21418a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
21428a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setmlme(ic, ireq);
21438a1b9b6aSSam Leffler 		break;
21448a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
21458a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setoptie(ic, ireq);
21468a1b9b6aSSam Leffler 		break;
21478a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
21488a1b9b6aSSam Leffler 		if (ireq->i_val) {
21498a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
21508a1b9b6aSSam Leffler 				return EINVAL;
21518a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_COUNTERM;
21528a1b9b6aSSam Leffler 		} else
21538a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
21548a1b9b6aSSam Leffler 		break;
21558a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
21568a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
21578a1b9b6aSSam Leffler 			return EINVAL;
21588a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
21598a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WPA;
21608a1b9b6aSSam Leffler 		switch (ireq->i_val) {
21618a1b9b6aSSam Leffler 		case 1:
21628a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1;
21638a1b9b6aSSam Leffler 			break;
21648a1b9b6aSSam Leffler 		case 2:
21658a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA2;
21668a1b9b6aSSam Leffler 			break;
21678a1b9b6aSSam Leffler 		case 3:
21688a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
21692e79ca97SSam Leffler 			break;
21702e79ca97SSam Leffler 		}
21718a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX? */
21728a1b9b6aSSam Leffler 		break;
21738a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
21748a1b9b6aSSam Leffler 		if (ireq->i_val) {
21758a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
21768a1b9b6aSSam Leffler 				return EINVAL;
21778a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WME;
21788a1b9b6aSSam Leffler 		} else
21798a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WME;
21808a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX maybe not for station? */
21818a1b9b6aSSam Leffler 		break;
21828a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
21838a1b9b6aSSam Leffler 		if (ireq->i_val)
21848a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_HIDESSID;
21858a1b9b6aSSam Leffler 		else
21868a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
21872e79ca97SSam Leffler 		error = ENETRESET;
21882e79ca97SSam Leffler 		break;
21898a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
21908a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
21918a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
21928a1b9b6aSSam Leffler 		else
21938a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
21948a1b9b6aSSam Leffler 		break;
21958a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
21968a1b9b6aSSam Leffler 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
21978a1b9b6aSSam Leffler 		    !ieee80211_crypto_available(ireq->i_val))
21988a1b9b6aSSam Leffler 			return EINVAL;
21998a1b9b6aSSam Leffler 		rsn->rsn_mcastcipher = ireq->i_val;
22008a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22018a1b9b6aSSam Leffler 		break;
22028a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
22038a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
22048a1b9b6aSSam Leffler 			return EINVAL;
22058a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
22068a1b9b6aSSam Leffler 		rsn->rsn_mcastkeylen = ireq->i_val;
22078a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22088a1b9b6aSSam Leffler 		break;
22098a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
22108a1b9b6aSSam Leffler 		/*
22118a1b9b6aSSam Leffler 		 * Convert user-specified cipher set to the set
22128a1b9b6aSSam Leffler 		 * we can support (via hardware or software).
22138a1b9b6aSSam Leffler 		 * NB: this logic intentionally ignores unknown and
22148a1b9b6aSSam Leffler 		 * unsupported ciphers so folks can specify 0xff or
22158a1b9b6aSSam Leffler 		 * similar and get all available ciphers.
22168a1b9b6aSSam Leffler 		 */
22178a1b9b6aSSam Leffler 		caps = 0;
22188a1b9b6aSSam Leffler 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
22198a1b9b6aSSam Leffler 			if ((ireq->i_val & (1<<j)) &&
22208a1b9b6aSSam Leffler 			    ((ic->ic_caps & cipher2cap(j)) ||
22218a1b9b6aSSam Leffler 			     ieee80211_crypto_available(j)))
22228a1b9b6aSSam Leffler 				caps |= 1<<j;
22238a1b9b6aSSam Leffler 		if (caps == 0)			/* nothing available */
22248a1b9b6aSSam Leffler 			return EINVAL;
22258a1b9b6aSSam Leffler 		/* XXX verify ciphers ok for unicast use? */
22268a1b9b6aSSam Leffler 		/* XXX disallow if running as it'll have no effect */
22278a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset = caps;
22288a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22298a1b9b6aSSam Leffler 		break;
22308a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
22318a1b9b6aSSam Leffler 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
22328a1b9b6aSSam Leffler 			return EINVAL;
22338a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = ireq->i_val;
22348a1b9b6aSSam Leffler 		break;
22358a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
22368a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
22378a1b9b6aSSam Leffler 			return EINVAL;
22388a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
22398a1b9b6aSSam Leffler 		rsn->rsn_ucastkeylen = ireq->i_val;
22408a1b9b6aSSam Leffler 		break;
22418a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
22428a1b9b6aSSam Leffler 		/* NB: for testing */
22438a1b9b6aSSam Leffler 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
22448a1b9b6aSSam Leffler 			       ((u_int16_t) ireq->i_len);
22458a1b9b6aSSam Leffler 		break;
22468a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
22478a1b9b6aSSam Leffler 		/* XXX check */
22488a1b9b6aSSam Leffler 		rsn->rsn_keymgmtset = ireq->i_val;
22498a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22508a1b9b6aSSam Leffler 		break;
22518a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
22528a1b9b6aSSam Leffler 		/* XXX check */
22538a1b9b6aSSam Leffler 		rsn->rsn_caps = ireq->i_val;
22548a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
22558a1b9b6aSSam Leffler 		break;
22568a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
22578a1b9b6aSSam Leffler 		/* NB: should only be set when in STA mode */
22588a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
22598a1b9b6aSSam Leffler 			return EINVAL;
22608a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
22618a1b9b6aSSam Leffler 			return EINVAL;
22628a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
22638a1b9b6aSSam Leffler 		if (error)
22648a1b9b6aSSam Leffler 			break;
22658a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
22668a1b9b6aSSam Leffler 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
22678a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
22688a1b9b6aSSam Leffler 		else
22698a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DESBSSID;
22708a1b9b6aSSam Leffler 		error = ENETRESET;
22718a1b9b6aSSam Leffler 		break;
22728a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
22738a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setchanlist(ic, ireq);
22748a1b9b6aSSam Leffler 		break;
22758a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
22768a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
22778a1b9b6aSSam Leffler 			break;
22788a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
22798a1b9b6aSSam Leffler 		if (error == 0)		/* XXX background scan */
22808a1b9b6aSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
22818a1b9b6aSSam Leffler 		break;
22828a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
22838a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
22848a1b9b6aSSam Leffler 		error = ieee80211_ioctl_macmac(ic, ireq);
22858a1b9b6aSSam Leffler 		break;
22868a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
22878a1b9b6aSSam Leffler 		error = ieee80211_ioctl_maccmd(ic, ireq);
22888a1b9b6aSSam Leffler 		break;
22898a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
22908a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
22918a1b9b6aSSam Leffler 		break;
22928a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
22938a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
22948a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
22958a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
22968a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
22978a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
22988a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
22998a1b9b6aSSam Leffler 		break;
23008a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
23018a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
23028a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
23038a1b9b6aSSam Leffler 			return EINVAL;
23048a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
23058a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
23068a1b9b6aSSam Leffler 			ic->ic_dtim_period = ireq->i_val;
23078a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
23088a1b9b6aSSam Leffler 		} else
23098a1b9b6aSSam Leffler 			error = EINVAL;
23108a1b9b6aSSam Leffler 		break;
23118a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
23128a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
23138a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
23148a1b9b6aSSam Leffler 			return EINVAL;
23158a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
23168a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
23178a1b9b6aSSam Leffler 			ic->ic_lintval = ireq->i_val;
23188a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
23198a1b9b6aSSam Leffler 		} else
23208a1b9b6aSSam Leffler 			error = EINVAL;
23218a1b9b6aSSam Leffler 		break;
2322c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
2323c4f040c3SSam Leffler 		if (ireq->i_val)
2324c4f040c3SSam Leffler 			ic->ic_flags |= IEEE80211_F_PUREG;
2325c4f040c3SSam Leffler 		else
2326c4f040c3SSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PUREG;
2327c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
2328c4f040c3SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
2329c4f040c3SSam Leffler 			error = ENETRESET;
2330c4f040c3SSam Leffler 		break;
23311a1e1d21SSam Leffler 	default:
23321a1e1d21SSam Leffler 		error = EINVAL;
23331a1e1d21SSam Leffler 		break;
23341a1e1d21SSam Leffler 	}
23358a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
23368a1b9b6aSSam Leffler 		error = 0;
23378a1b9b6aSSam Leffler 	return error;
23388a1b9b6aSSam Leffler }
23398a1b9b6aSSam Leffler 
23408a1b9b6aSSam Leffler int
23418a1b9b6aSSam Leffler ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
23428a1b9b6aSSam Leffler {
23438a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
23448a1b9b6aSSam Leffler 	int error = 0;
23458a1b9b6aSSam Leffler 	struct ifreq *ifr;
23468a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
23478a1b9b6aSSam Leffler 
23488a1b9b6aSSam Leffler 	switch (cmd) {
23498a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
23508a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
23518a1b9b6aSSam Leffler 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
23528a1b9b6aSSam Leffler 				&ic->ic_media, cmd);
23538a1b9b6aSSam Leffler 		break;
23548a1b9b6aSSam Leffler 	case SIOCG80211:
23558a1b9b6aSSam Leffler 		error = ieee80211_ioctl_get80211(ic, cmd,
23568a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
23578a1b9b6aSSam Leffler 		break;
23588a1b9b6aSSam Leffler 	case SIOCS80211:
23598a1b9b6aSSam Leffler 		error = suser(curthread);
23608a1b9b6aSSam Leffler 		if (error == 0)
23618a1b9b6aSSam Leffler 			error = ieee80211_ioctl_set80211(ic, cmd,
23628a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
23631a1e1d21SSam Leffler 		break;
23641a1e1d21SSam Leffler 	case SIOCGIFGENERIC:
23658a1b9b6aSSam Leffler 		error = ieee80211_cfgget(ic, cmd, data);
23661a1e1d21SSam Leffler 		break;
23671a1e1d21SSam Leffler 	case SIOCSIFGENERIC:
23681a1e1d21SSam Leffler 		error = suser(curthread);
23691a1e1d21SSam Leffler 		if (error)
23701a1e1d21SSam Leffler 			break;
23718a1b9b6aSSam Leffler 		error = ieee80211_cfgset(ic, cmd, data);
23721a1e1d21SSam Leffler 		break;
23731be50176SSam Leffler 	case SIOCG80211STATS:
23741be50176SSam Leffler 		ifr = (struct ifreq *)data;
23751be50176SSam Leffler 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
23761be50176SSam Leffler 		break;
23776f161f03SSam Leffler 	case SIOCSIFMTU:
23786f161f03SSam Leffler 		ifr = (struct ifreq *)data;
23796f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
23806f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
23816f161f03SSam Leffler 			error = EINVAL;
23826f161f03SSam Leffler 		else
23836f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
23846f161f03SSam Leffler 		break;
2385b2e95691SSam Leffler 	case SIOCSIFADDR:
2386b2e95691SSam Leffler 		/*
2387b2e95691SSam Leffler 		 * XXX Handle this directly so we can supress if_init calls.
2388b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
2389b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
2390b2e95691SSam Leffler 		 * XXX set IFF_UP and so supress if_init being called when
2391b2e95691SSam Leffler 		 * XXX it should be.
2392b2e95691SSam Leffler 		 */
2393b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
2394b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
2395b2e95691SSam Leffler #ifdef INET
2396b2e95691SSam Leffler 		case AF_INET:
2397b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2398b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2399b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2400b2e95691SSam Leffler 			}
2401b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
2402b2e95691SSam Leffler 			break;
2403b2e95691SSam Leffler #endif
2404b2e95691SSam Leffler #ifdef IPX
2405b2e95691SSam Leffler 		/*
2406b2e95691SSam Leffler 		 * XXX - This code is probably wrong,
2407b2e95691SSam Leffler 		 *	 but has been copied many times.
2408b2e95691SSam Leffler 		 */
2409b2e95691SSam Leffler 		case AF_IPX: {
2410b2e95691SSam Leffler 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
2411b2e95691SSam Leffler 
2412b2e95691SSam Leffler 			if (ipx_nullhost(*ina))
2413fc74a9f9SBrooks Davis 				ina->x_host = *(union ipx_host *)
2414fc74a9f9SBrooks Davis 				    IFP2ENADDR(ifp);
2415b2e95691SSam Leffler 			else
2416b2e95691SSam Leffler 				bcopy((caddr_t) ina->x_host.c_host,
2417fc74a9f9SBrooks Davis 				      (caddr_t) IFP2ENADDR(ifp),
2418fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
2419b2e95691SSam Leffler 			/* fall thru... */
2420b2e95691SSam Leffler 		}
2421b2e95691SSam Leffler #endif
2422b2e95691SSam Leffler 		default:
2423b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2424b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2425b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2426b2e95691SSam Leffler 			}
2427b2e95691SSam Leffler 			break;
2428b2e95691SSam Leffler 		}
2429b2e95691SSam Leffler 		break;
24301a1e1d21SSam Leffler 	default:
24311a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
24321a1e1d21SSam Leffler 		break;
24331a1e1d21SSam Leffler 	}
24341a1e1d21SSam Leffler 	return error;
24351a1e1d21SSam Leffler }
2436