xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision be0df3e7c014be2ccb6cd8b858640eb3f849a5a5)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 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.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler /*
311a1e1d21SSam Leffler  * IEEE 802.11 ioctl support (FreeBSD-specific)
321a1e1d21SSam Leffler  */
331a1e1d21SSam Leffler 
34b2e95691SSam Leffler #include "opt_inet.h"
35b2e95691SSam Leffler #include "opt_ipx.h"
36b032f27cSSam Leffler #include "opt_wlan.h"
37b2e95691SSam Leffler 
381a1e1d21SSam Leffler #include <sys/endian.h>
391a1e1d21SSam Leffler #include <sys/param.h>
401a1e1d21SSam Leffler #include <sys/kernel.h>
41acd3428bSRobert Watson #include <sys/priv.h>
421a1e1d21SSam Leffler #include <sys/socket.h>
431a1e1d21SSam Leffler #include <sys/sockio.h>
441a1e1d21SSam Leffler #include <sys/systm.h>
458d9b29f3SAndrew Thompson #include <sys/taskqueue.h>
461a1e1d21SSam Leffler 
471a1e1d21SSam Leffler #include <net/if.h>
484a0d6638SRuslan Ermilov #include <net/if_dl.h>
491a1e1d21SSam Leffler #include <net/if_media.h>
501a1e1d21SSam Leffler #include <net/ethernet.h>
511a1e1d21SSam Leffler 
52b2e95691SSam Leffler #ifdef INET
53b2e95691SSam Leffler #include <netinet/in.h>
54b2e95691SSam Leffler #include <netinet/if_ether.h>
55b2e95691SSam Leffler #endif
56b2e95691SSam Leffler 
57b2e95691SSam Leffler #ifdef IPX
58b2e95691SSam Leffler #include <netipx/ipx.h>
59b2e95691SSam Leffler #include <netipx/ipx_if.h>
60b2e95691SSam Leffler #endif
61b2e95691SSam Leffler 
621a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
631a1e1d21SSam Leffler #include <net80211/ieee80211_ioctl.h>
64b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
65b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
6610ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
6710ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
6810ad9a77SSam Leffler #endif
691a1e1d21SSam Leffler 
70b032f27cSSam Leffler #define	IS_UP_AUTO(_vap) \
71b032f27cSSam Leffler 	(IFNET_IS_UP_RUNNING(vap->iv_ifp) && \
72b032f27cSSam Leffler 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
738a1b9b6aSSam Leffler 
74b032f27cSSam Leffler static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
7568e8e04eSSam Leffler static struct ieee80211_channel *findchannel(struct ieee80211com *,
7668e8e04eSSam Leffler 		int ieee, int mode);
771a1e1d21SSam Leffler 
78b032f27cSSam Leffler static __noinline int
79b032f27cSSam Leffler ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
808a1b9b6aSSam Leffler {
81b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
828a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
838a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
848a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
858a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
868a1b9b6aSSam Leffler 	u_int kid;
878a1b9b6aSSam Leffler 	int error;
888a1b9b6aSSam Leffler 
898a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
908a1b9b6aSSam Leffler 		return EINVAL;
918a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
928a1b9b6aSSam Leffler 	if (error)
938a1b9b6aSSam Leffler 		return error;
948a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
958a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
96b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
978a1b9b6aSSam Leffler 		if (ni == NULL)
98b032f27cSSam Leffler 			return ENOENT;
998a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
1008a1b9b6aSSam Leffler 	} else {
1018a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
1028a1b9b6aSSam Leffler 			return EINVAL;
103b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
104b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
1058a1b9b6aSSam Leffler 		ni = NULL;
1068a1b9b6aSSam Leffler 	}
1078a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
1088a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
1098a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
1108a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
111b032f27cSSam Leffler 	if (wk->wk_keyix == vap->iv_def_txkey)
1128a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
113acd3428bSRobert Watson 	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
1148a1b9b6aSSam Leffler 		/* NB: only root can read key data */
115b032f27cSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
1168a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
1178a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
1188a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
1198a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
1208a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
1218a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
1228a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
1238a1b9b6aSSam Leffler 		}
1248a1b9b6aSSam Leffler 	} else {
1258a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
1268a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
1278a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
1288a1b9b6aSSam Leffler 	}
1298a1b9b6aSSam Leffler 	if (ni != NULL)
1308a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
1318a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
1328a1b9b6aSSam Leffler }
1338a1b9b6aSSam Leffler 
134b032f27cSSam Leffler static __noinline int
135b032f27cSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1368a1b9b6aSSam Leffler {
137b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1388a1b9b6aSSam Leffler 
139e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
1408a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1418a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
1428a1b9b6aSSam Leffler }
1438a1b9b6aSSam Leffler 
144b032f27cSSam Leffler static __noinline int
145b032f27cSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
1468a1b9b6aSSam Leffler {
147b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
14868e8e04eSSam Leffler 	int space;
1498a1b9b6aSSam Leffler 
1508a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
15168e8e04eSSam Leffler 			ic_chans[ic->ic_nchans]);
1528a1b9b6aSSam Leffler 	if (space > ireq->i_len)
1538a1b9b6aSSam Leffler 		space = ireq->i_len;
15468e8e04eSSam Leffler 	/* XXX assumes compatible layout */
15568e8e04eSSam Leffler 	return copyout(&ic->ic_nchans, ireq->i_data, space);
1568a1b9b6aSSam Leffler }
1578a1b9b6aSSam Leffler 
158b032f27cSSam Leffler static __noinline int
159b032f27cSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
160b032f27cSSam Leffler 	struct ieee80211req *ireq, int req)
1618a1b9b6aSSam Leffler {
1628a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16368e8e04eSSam Leffler 	struct ieee80211req_wpaie2 wpaie;
1648a1b9b6aSSam Leffler 	int error;
1658a1b9b6aSSam Leffler 
1668a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1678a1b9b6aSSam Leffler 		return EINVAL;
1688a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
1698a1b9b6aSSam Leffler 	if (error != 0)
1708a1b9b6aSSam Leffler 		return error;
171b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie.wpa_macaddr);
1728a1b9b6aSSam Leffler 	if (ni == NULL)
173b032f27cSSam Leffler 		return ENOENT;
1748a1b9b6aSSam Leffler 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
175b032f27cSSam Leffler 	if (ni->ni_ies.wpa_ie != NULL) {
176b032f27cSSam Leffler 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
1778a1b9b6aSSam Leffler 		if (ielen > sizeof(wpaie.wpa_ie))
1788a1b9b6aSSam Leffler 			ielen = sizeof(wpaie.wpa_ie);
179b032f27cSSam Leffler 		memcpy(wpaie.wpa_ie, ni->ni_ies.wpa_ie, ielen);
1808a1b9b6aSSam Leffler 	}
18168e8e04eSSam Leffler 	if (req == IEEE80211_IOC_WPAIE2) {
18268e8e04eSSam Leffler 		memset(wpaie.rsn_ie, 0, sizeof(wpaie.rsn_ie));
183b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
184b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
18568e8e04eSSam Leffler 			if (ielen > sizeof(wpaie.rsn_ie))
18668e8e04eSSam Leffler 				ielen = sizeof(wpaie.rsn_ie);
187b032f27cSSam Leffler 			memcpy(wpaie.rsn_ie, ni->ni_ies.rsn_ie, ielen);
18868e8e04eSSam Leffler 		}
18968e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
19068e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
19168e8e04eSSam Leffler 	} else {
19268e8e04eSSam Leffler 		/* compatibility op, may overwrite wpa ie */
19368e8e04eSSam Leffler 		/* XXX check ic_flags? */
194b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
195b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
19668e8e04eSSam Leffler 			if (ielen > sizeof(wpaie.wpa_ie))
19768e8e04eSSam Leffler 				ielen = sizeof(wpaie.wpa_ie);
198b032f27cSSam Leffler 			memcpy(wpaie.wpa_ie, ni->ni_ies.rsn_ie, ielen);
19968e8e04eSSam Leffler 		}
20068e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
20168e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
20268e8e04eSSam Leffler 	}
2038a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2048a1b9b6aSSam Leffler 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
2058a1b9b6aSSam Leffler }
2068a1b9b6aSSam Leffler 
207b032f27cSSam Leffler static __noinline int
208b032f27cSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
2098a1b9b6aSSam Leffler {
2108a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
21168e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
2128a1b9b6aSSam Leffler 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
2138a1b9b6aSSam Leffler 	int error;
2148a1b9b6aSSam Leffler 
2158a1b9b6aSSam Leffler 	if (ireq->i_len < off)
2168a1b9b6aSSam Leffler 		return EINVAL;
2178a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
2188a1b9b6aSSam Leffler 	if (error != 0)
2198a1b9b6aSSam Leffler 		return error;
220b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
22168e8e04eSSam Leffler 	if (ni == NULL)
222b032f27cSSam Leffler 		return ENOENT;
2238a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
2248a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
2258a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
22668e8e04eSSam Leffler 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
2278a1b9b6aSSam Leffler 			ireq->i_len - off);
2288a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2298a1b9b6aSSam Leffler 	return error;
2308a1b9b6aSSam Leffler }
2318a1b9b6aSSam Leffler 
23268e8e04eSSam Leffler struct scanreq {
23368e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
23468e8e04eSSam Leffler 	size_t space;
23568e8e04eSSam Leffler };
23668e8e04eSSam Leffler 
23768e8e04eSSam Leffler static size_t
23868e8e04eSSam Leffler scan_space(const struct ieee80211_scan_entry *se, int *ielen)
23968e8e04eSSam Leffler {
24068e8e04eSSam Leffler 	size_t len;
24168e8e04eSSam Leffler 
242b032f27cSSam Leffler 	*ielen = se->se_ies.len;
24368e8e04eSSam Leffler 	/*
24468e8e04eSSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
24568e8e04eSSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
24668e8e04eSSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
24768e8e04eSSam Leffler 	 */
24868e8e04eSSam Leffler 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] + *ielen;
24968e8e04eSSam Leffler 	return roundup(len, sizeof(uint32_t));
25068e8e04eSSam Leffler }
25168e8e04eSSam Leffler 
25268e8e04eSSam Leffler static void
25368e8e04eSSam Leffler get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
25468e8e04eSSam Leffler {
25568e8e04eSSam Leffler 	struct scanreq *req = arg;
25668e8e04eSSam Leffler 	int ielen;
25768e8e04eSSam Leffler 
25868e8e04eSSam Leffler 	req->space += scan_space(se, &ielen);
25968e8e04eSSam Leffler }
26068e8e04eSSam Leffler 
261b032f27cSSam Leffler static __noinline void
26268e8e04eSSam Leffler get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
26368e8e04eSSam Leffler {
26468e8e04eSSam Leffler 	struct scanreq *req = arg;
26568e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
26668e8e04eSSam Leffler 	int ielen, len, nr, nxr;
26768e8e04eSSam Leffler 	uint8_t *cp;
26868e8e04eSSam Leffler 
26968e8e04eSSam Leffler 	len = scan_space(se, &ielen);
27068e8e04eSSam Leffler 	if (len > req->space)
27168e8e04eSSam Leffler 		return;
27268e8e04eSSam Leffler 
27368e8e04eSSam Leffler 	sr = req->sr;
27468e8e04eSSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535,
27568e8e04eSSam Leffler 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
276b032f27cSSam Leffler 	sr->isr_len = len;
27768e8e04eSSam Leffler 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
27868e8e04eSSam Leffler 	sr->isr_ie_len = ielen;
27968e8e04eSSam Leffler 	sr->isr_freq = se->se_chan->ic_freq;
28068e8e04eSSam Leffler 	sr->isr_flags = se->se_chan->ic_flags;
28168e8e04eSSam Leffler 	sr->isr_rssi = se->se_rssi;
28268e8e04eSSam Leffler 	sr->isr_noise = se->se_noise;
28368e8e04eSSam Leffler 	sr->isr_intval = se->se_intval;
28468e8e04eSSam Leffler 	sr->isr_capinfo = se->se_capinfo;
28568e8e04eSSam Leffler 	sr->isr_erp = se->se_erp;
28668e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
28768e8e04eSSam Leffler 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
28868e8e04eSSam Leffler 	memcpy(sr->isr_rates, se->se_rates+2, nr);
28968e8e04eSSam Leffler 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
29068e8e04eSSam Leffler 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
29168e8e04eSSam Leffler 	sr->isr_nrates = nr + nxr;
29268e8e04eSSam Leffler 
29368e8e04eSSam Leffler 	sr->isr_ssid_len = se->se_ssid[1];
29468e8e04eSSam Leffler 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
29568e8e04eSSam Leffler 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
29668e8e04eSSam Leffler 
29768e8e04eSSam Leffler 	if (ielen) {
29868e8e04eSSam Leffler 		cp += sr->isr_ssid_len;
299b032f27cSSam Leffler 		memcpy(cp, se->se_ies.data, ielen);
30068e8e04eSSam Leffler 	}
30168e8e04eSSam Leffler 
30268e8e04eSSam Leffler 	req->space -= len;
30368e8e04eSSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
30468e8e04eSSam Leffler }
30568e8e04eSSam Leffler 
306b032f27cSSam Leffler static __noinline int
307b032f27cSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
308b032f27cSSam Leffler 	struct ieee80211req *ireq)
30968e8e04eSSam Leffler {
31068e8e04eSSam Leffler 	struct scanreq req;
31168e8e04eSSam Leffler 	int error;
31268e8e04eSSam Leffler 
313b032f27cSSam Leffler 	if (ireq->i_len < sizeof(struct scanreq))
31468e8e04eSSam Leffler 		return EFAULT;
31568e8e04eSSam Leffler 
31668e8e04eSSam Leffler 	error = 0;
31768e8e04eSSam Leffler 	req.space = 0;
318b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, get_scan_space, &req);
31968e8e04eSSam Leffler 	if (req.space > ireq->i_len)
32068e8e04eSSam Leffler 		req.space = ireq->i_len;
32168e8e04eSSam Leffler 	if (req.space > 0) {
32268e8e04eSSam Leffler 		size_t space;
32368e8e04eSSam Leffler 		void *p;
32468e8e04eSSam Leffler 
32568e8e04eSSam Leffler 		space = req.space;
32668e8e04eSSam Leffler 		/* XXX M_WAITOK after driver lock released */
327e2126decSSam Leffler 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
32868e8e04eSSam Leffler 		if (p == NULL)
32968e8e04eSSam Leffler 			return ENOMEM;
33068e8e04eSSam Leffler 		req.sr = p;
331b032f27cSSam Leffler 		ieee80211_scan_iterate(vap, get_scan_result, &req);
332239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
333239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
334e2126decSSam Leffler 		free(p, M_TEMP);
335239cc3b6SSam Leffler 	} else
336239cc3b6SSam Leffler 		ireq->i_len = 0;
337239cc3b6SSam Leffler 
338239cc3b6SSam Leffler 	return error;
339239cc3b6SSam Leffler }
3408a1b9b6aSSam Leffler 
3412cab1d3dSSam Leffler struct stainforeq {
342b032f27cSSam Leffler 	struct ieee80211vap *vap;
3432cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3442cab1d3dSSam Leffler 	size_t	space;
3452cab1d3dSSam Leffler };
3468a1b9b6aSSam Leffler 
3472cab1d3dSSam Leffler static size_t
3482cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
3492cab1d3dSSam Leffler {
350b032f27cSSam Leffler 	*ielen = ni->ni_ies.len;
3512cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
35268e8e04eSSam Leffler 		      sizeof(uint32_t));
3532cab1d3dSSam Leffler }
3542cab1d3dSSam Leffler 
3552cab1d3dSSam Leffler static void
3562cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
3572cab1d3dSSam Leffler {
3582cab1d3dSSam Leffler 	struct stainforeq *req = arg;
3592cab1d3dSSam Leffler 	size_t ielen;
3602cab1d3dSSam Leffler 
361b032f27cSSam Leffler 	if (req->vap != ni->ni_vap)
362b032f27cSSam Leffler 		return;
363b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
3642cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3652cab1d3dSSam Leffler 		return;
3662cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
3672cab1d3dSSam Leffler }
3682cab1d3dSSam Leffler 
369b032f27cSSam Leffler static __noinline void
3702cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
3712cab1d3dSSam Leffler {
3722cab1d3dSSam Leffler 	struct stainforeq *req = arg;
373b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3742cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3752cab1d3dSSam Leffler 	size_t ielen, len;
37668e8e04eSSam Leffler 	uint8_t *cp;
3772cab1d3dSSam Leffler 
378b032f27cSSam Leffler 	if (req->vap != ni->ni_vap)
379b032f27cSSam Leffler 		return;
380b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
3812cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3822cab1d3dSSam Leffler 		return;
3832cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
3842cab1d3dSSam Leffler 		return;
3852cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
3862cab1d3dSSam Leffler 	if (len > req->space)
3872cab1d3dSSam Leffler 		return;
3882cab1d3dSSam Leffler 	si = req->si;
3892cab1d3dSSam Leffler 	si->isi_len = len;
39068e8e04eSSam Leffler 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
3912cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
3928a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
3938a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
3948a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
3958a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
396b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
397b032f27cSSam Leffler 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
3988a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
3998a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
4008a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
4018a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
4028a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
4038a1b9b6aSSam Leffler 		si->isi_nrates = 15;
4048a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
4058a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
406b032f27cSSam Leffler 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
407b032f27cSSam Leffler 		const struct ieee80211_mcs_rates *mcs =
408b032f27cSSam Leffler 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
409b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
410b032f27cSSam Leffler 			if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40)
411b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_800ns;
412b032f27cSSam Leffler 			else
413b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_400ns;
414b032f27cSSam Leffler 		} else {
415b032f27cSSam Leffler 			if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20)
416b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_800ns;
417b032f27cSSam Leffler 			else
418b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_400ns;
419b032f27cSSam Leffler 		}
420b032f27cSSam Leffler 	} else
421b032f27cSSam Leffler 		si->isi_txmbps = si->isi_txrate;
4228a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
4238a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
4248a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
4258a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
4268a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
4278a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
4288a1b9b6aSSam Leffler 	} else {
429801df4a5SSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
430801df4a5SSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
4318a1b9b6aSSam Leffler 	}
4322cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
4332cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
434b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_run;
435b032f27cSSam Leffler 	else if (ni->ni_associd != 0 ||
436b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_WDS &&
437b032f27cSSam Leffler 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
438b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_auth;
4398a1b9b6aSSam Leffler 	else
440b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_init;
4418a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
4428a1b9b6aSSam Leffler 
44368e8e04eSSam Leffler 	if (ielen) {
44468e8e04eSSam Leffler 		cp = ((uint8_t *)si) + si->isi_ie_off;
445b032f27cSSam Leffler 		memcpy(cp, ni->ni_ies.data, ielen);
4468a1b9b6aSSam Leffler 	}
4472cab1d3dSSam Leffler 
44868e8e04eSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
4492cab1d3dSSam Leffler 	req->space -= len;
4508a1b9b6aSSam Leffler }
4512cab1d3dSSam Leffler 
452b032f27cSSam Leffler static __noinline int
453b032f27cSSam Leffler getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
454d1c85daeSSam Leffler 	struct ieee80211_node *ni, int off)
4552cab1d3dSSam Leffler {
456b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4572cab1d3dSSam Leffler 	struct stainforeq req;
458d1c85daeSSam Leffler 	size_t space;
459d1c85daeSSam Leffler 	void *p;
4602cab1d3dSSam Leffler 	int error;
4612cab1d3dSSam Leffler 
4622cab1d3dSSam Leffler 	error = 0;
4632cab1d3dSSam Leffler 	req.space = 0;
464b032f27cSSam Leffler 	req.vap = vap;
465d1c85daeSSam Leffler 	if (ni == NULL)
4662cab1d3dSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
467d1c85daeSSam Leffler 	else
468d1c85daeSSam Leffler 		get_sta_space(&req, ni);
4692cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
4702cab1d3dSSam Leffler 		req.space = ireq->i_len;
4712cab1d3dSSam Leffler 	if (req.space > 0) {
4722cab1d3dSSam Leffler 		space = req.space;
4732cab1d3dSSam Leffler 		/* XXX M_WAITOK after driver lock released */
474e2126decSSam Leffler 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
475d1c85daeSSam Leffler 		if (p == NULL) {
476d1c85daeSSam Leffler 			error = ENOMEM;
477d1c85daeSSam Leffler 			goto bad;
478d1c85daeSSam Leffler 		}
4792cab1d3dSSam Leffler 		req.si = p;
480d1c85daeSSam Leffler 		if (ni == NULL)
4812cab1d3dSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
482d1c85daeSSam Leffler 		else
483d1c85daeSSam Leffler 			get_sta_info(&req, ni);
4842cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
48568e8e04eSSam Leffler 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
486e2126decSSam Leffler 		free(p, M_TEMP);
4872cab1d3dSSam Leffler 	} else
4882cab1d3dSSam Leffler 		ireq->i_len = 0;
489d1c85daeSSam Leffler bad:
490d1c85daeSSam Leffler 	if (ni != NULL)
491d1c85daeSSam Leffler 		ieee80211_free_node(ni);
4928a1b9b6aSSam Leffler 	return error;
4938a1b9b6aSSam Leffler }
4948a1b9b6aSSam Leffler 
495b032f27cSSam Leffler static __noinline int
496b032f27cSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
497d1c85daeSSam Leffler {
49868e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
499d1c85daeSSam Leffler 	const int off = __offsetof(struct ieee80211req_sta_req, info);
500d1c85daeSSam Leffler 	struct ieee80211_node *ni;
501d1c85daeSSam Leffler 	int error;
502d1c85daeSSam Leffler 
503d1c85daeSSam Leffler 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
504d1c85daeSSam Leffler 		return EFAULT;
505d1c85daeSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
506d1c85daeSSam Leffler 	if (error != 0)
507d1c85daeSSam Leffler 		return error;
508b032f27cSSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
509d1c85daeSSam Leffler 		ni = NULL;
510d1c85daeSSam Leffler 	} else {
511b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
51268e8e04eSSam Leffler 		if (ni == NULL)
513b032f27cSSam Leffler 			return ENOENT;
514d1c85daeSSam Leffler 	}
515b032f27cSSam Leffler 	return getstainfo_common(vap, ireq, ni, off);
516d1c85daeSSam Leffler }
517d1c85daeSSam Leffler 
518b032f27cSSam Leffler static __noinline int
519b032f27cSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
5208a1b9b6aSSam Leffler {
5218a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
5228a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
5238a1b9b6aSSam Leffler 	int error;
5248a1b9b6aSSam Leffler 
5258a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
5268a1b9b6aSSam Leffler 		return EINVAL;
5278a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
5288a1b9b6aSSam Leffler 	if (error != 0)
5298a1b9b6aSSam Leffler 		return error;
530b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
5318a1b9b6aSSam Leffler 	if (ni == NULL)
532b032f27cSSam Leffler 		return ENOENT;
5338a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
5348a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
5358a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
5368a1b9b6aSSam Leffler 	return error;
5378a1b9b6aSSam Leffler }
5388a1b9b6aSSam Leffler 
539b032f27cSSam Leffler static __noinline int
540b032f27cSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
5418a1b9b6aSSam Leffler {
542b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5438a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
5448a1b9b6aSSam Leffler 	struct wmeParams *wmep;
5458a1b9b6aSSam Leffler 	int ac;
5468a1b9b6aSSam Leffler 
5478a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
5488a1b9b6aSSam Leffler 		return EINVAL;
5498a1b9b6aSSam Leffler 
5508a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
5518a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
5528a1b9b6aSSam Leffler 		ac = WME_AC_BE;
5538a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
5548a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5558a1b9b6aSSam Leffler 	else
5568a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5578a1b9b6aSSam Leffler 	switch (ireq->i_type) {
5588a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
5598a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
5608a1b9b6aSSam Leffler 		break;
5618a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
5628a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
5638a1b9b6aSSam Leffler 		break;
5648a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
5658a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
5668a1b9b6aSSam Leffler 		break;
5678a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
5688a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
5698a1b9b6aSSam Leffler 		break;
5708a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
5718a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5728a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
5738a1b9b6aSSam Leffler 		break;
5748a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
5758a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5768a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
5778a1b9b6aSSam Leffler 		break;
5788a1b9b6aSSam Leffler 	}
5798a1b9b6aSSam Leffler 	return 0;
5808a1b9b6aSSam Leffler }
5818a1b9b6aSSam Leffler 
582b032f27cSSam Leffler static __noinline int
583b032f27cSSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
584188757f5SSam Leffler {
585b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
586188757f5SSam Leffler 
587b032f27cSSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
588188757f5SSam Leffler }
589188757f5SSam Leffler 
590c788ca3eSBill Paul /*
59168e8e04eSSam Leffler  * Return the current ``state'' of an Atheros capbility.
59268e8e04eSSam Leffler  * If associated in station mode report the negotiated
59368e8e04eSSam Leffler  * setting. Otherwise report the current setting.
59468e8e04eSSam Leffler  */
59568e8e04eSSam Leffler static int
596b032f27cSSam Leffler getathcap(struct ieee80211vap *vap, int cap)
59768e8e04eSSam Leffler {
598b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
599b032f27cSSam Leffler 	    vap->iv_state == IEEE80211_S_RUN)
600b032f27cSSam Leffler 		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
60168e8e04eSSam Leffler 	else
602b032f27cSSam Leffler 		return (vap->iv_flags & cap) != 0;
603b032f27cSSam Leffler }
604b032f27cSSam Leffler 
605b032f27cSSam Leffler static __noinline int
606b032f27cSSam Leffler ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
607b032f27cSSam Leffler {
608b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
609b032f27cSSam Leffler 	struct ieee80211_channel *c;
610b032f27cSSam Leffler 
611b032f27cSSam Leffler 	if (ireq->i_len != sizeof(struct ieee80211_channel))
612b032f27cSSam Leffler 		return EINVAL;
613b032f27cSSam Leffler 	/*
614b032f27cSSam Leffler 	 * vap's may have different operating channels when HT is
615b032f27cSSam Leffler 	 * in use.  When in RUN state report the vap-specific channel.
616b032f27cSSam Leffler 	 * Otherwise return curchan.
617b032f27cSSam Leffler 	 */
618b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_RUN)
619b032f27cSSam Leffler 		c = vap->iv_bss->ni_chan;
620b032f27cSSam Leffler 	else
621b032f27cSSam Leffler 		c = ic->ic_curchan;
622b032f27cSSam Leffler 	return copyout(c, ireq->i_data, sizeof(*c));
62368e8e04eSSam Leffler }
62468e8e04eSSam Leffler 
62568e8e04eSSam Leffler static int
626b032f27cSSam Leffler getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
62768e8e04eSSam Leffler {
628b032f27cSSam Leffler 	if (aie == NULL)
62968e8e04eSSam Leffler 		return EINVAL;
630b032f27cSSam Leffler 	/* NB: truncate, caller can check length */
631b032f27cSSam Leffler 	if (ireq->i_len > aie->ie_len)
632b032f27cSSam Leffler 		ireq->i_len = aie->ie_len;
633b032f27cSSam Leffler 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
634b032f27cSSam Leffler }
635b032f27cSSam Leffler 
636b032f27cSSam Leffler static int
637b032f27cSSam Leffler ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
638b032f27cSSam Leffler {
639b032f27cSSam Leffler 	uint8_t fc0;
640b032f27cSSam Leffler 
641b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
642b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
643b032f27cSSam Leffler 		return EINVAL;
644b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
645b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
646b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
647b032f27cSSam Leffler 		return getappie(vap->iv_appie_beacon, ireq);
648b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
649b032f27cSSam Leffler 		return getappie(vap->iv_appie_proberesp, ireq);
650b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
651b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocresp, ireq);
652b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
653b032f27cSSam Leffler 		return getappie(vap->iv_appie_probereq, ireq);
654b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
655b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocreq, ireq);
656b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
657b032f27cSSam Leffler 		return getappie(vap->iv_appie_wpa, ireq);
658b032f27cSSam Leffler 	}
659b032f27cSSam Leffler 	return EINVAL;
660b032f27cSSam Leffler }
661b032f27cSSam Leffler 
662b032f27cSSam Leffler static __noinline int
663b032f27cSSam Leffler ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
664b032f27cSSam Leffler 	const struct ieee80211req *ireq)
665b032f27cSSam Leffler {
666b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
667b032f27cSSam Leffler 
668b032f27cSSam Leffler 	if (ireq->i_len != sizeof(ic->ic_regdomain))
669b032f27cSSam Leffler 		return EINVAL;
670b032f27cSSam Leffler 	return copyout(&ic->ic_regdomain, ireq->i_data,
671b032f27cSSam Leffler 	    sizeof(ic->ic_regdomain));
672b032f27cSSam Leffler }
673b032f27cSSam Leffler 
674b032f27cSSam Leffler static __noinline int
675b032f27cSSam Leffler ieee80211_ioctl_getroam(struct ieee80211vap *vap,
676b032f27cSSam Leffler 	const struct ieee80211req *ireq)
677b032f27cSSam Leffler {
678b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vap->iv_roamparms))
679b032f27cSSam Leffler 		return EINVAL;
680b032f27cSSam Leffler 	return copyout(vap->iv_roamparms, ireq->i_data,
681b032f27cSSam Leffler 	    sizeof(vap->iv_roamparms));
682b032f27cSSam Leffler }
683b032f27cSSam Leffler 
684b032f27cSSam Leffler static __noinline int
685b032f27cSSam Leffler ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
686b032f27cSSam Leffler 	const struct ieee80211req *ireq)
687b032f27cSSam Leffler {
688b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vap->iv_txparms))
689b032f27cSSam Leffler 		return EINVAL;
690b032f27cSSam Leffler 	return copyout(vap->iv_txparms, ireq->i_data, sizeof(vap->iv_txparms));
691b032f27cSSam Leffler }
692b032f27cSSam Leffler 
693b032f27cSSam Leffler static __noinline int
694b032f27cSSam Leffler ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
695b032f27cSSam Leffler 	const struct ieee80211req *ireq)
696b032f27cSSam Leffler {
697b032f27cSSam Leffler 	struct ieee80211_devcaps_req *dc;
698b032f27cSSam Leffler 	struct ieee80211req_chaninfo *ci;
6998658b18bSSam Leffler 	int maxchans, error;
700b032f27cSSam Leffler 
7018658b18bSSam Leffler 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
7028658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
7038658b18bSSam Leffler 	/* NB: require 1 so we know ic_nchans is accessible */
7048658b18bSSam Leffler 	if (maxchans < 1)
705b032f27cSSam Leffler 		return EINVAL;
7068658b18bSSam Leffler 	/* constrain max request size, 2K channels is ~24Kbytes */
7078658b18bSSam Leffler 	if (maxchans > 2048)
7088658b18bSSam Leffler 		maxchans = 2048;
7098658b18bSSam Leffler 	dc = (struct ieee80211_devcaps_req *)
7108658b18bSSam Leffler 	    malloc(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP, M_NOWAIT | M_ZERO);
711b032f27cSSam Leffler 	if (dc == NULL)
712b032f27cSSam Leffler 		return ENOMEM;
713b032f27cSSam Leffler 	dc->dc_drivercaps = ic->ic_caps;
714b032f27cSSam Leffler 	dc->dc_cryptocaps = ic->ic_cryptocaps;
715b032f27cSSam Leffler 	dc->dc_htcaps = ic->ic_htcaps;
716b032f27cSSam Leffler 	ci = &dc->dc_chaninfo;
7178658b18bSSam Leffler 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
718f8b1ea17SSam Leffler 	KASSERT(ci->ic_nchans <= maxchans,
719f8b1ea17SSam Leffler 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
720b032f27cSSam Leffler 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
7218658b18bSSam Leffler 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
722e2126decSSam Leffler 	free(dc, M_TEMP);
723b032f27cSSam Leffler 	return error;
724b032f27cSSam Leffler }
725b032f27cSSam Leffler 
726b032f27cSSam Leffler static __noinline int
727b032f27cSSam Leffler ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
728b032f27cSSam Leffler {
729b032f27cSSam Leffler 	struct ieee80211_node *ni;
730b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
731b032f27cSSam Leffler 	int error;
732b032f27cSSam Leffler 
733b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
734b032f27cSSam Leffler 		return EINVAL;
735b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
736b032f27cSSam Leffler 	if (error != 0)
737b032f27cSSam Leffler 		return error;
738b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
739b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
740b032f27cSSam Leffler 		    vlan.sv_macaddr);
741b032f27cSSam Leffler 		if (ni == NULL)
742b032f27cSSam Leffler 			return ENOENT;
743b032f27cSSam Leffler 	} else
744b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
745b032f27cSSam Leffler 	vlan.sv_vlan = ni->ni_vlan;
746b032f27cSSam Leffler 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
747b032f27cSSam Leffler 	ieee80211_free_node(ni);
748b032f27cSSam Leffler 	return error;
74968e8e04eSSam Leffler }
75068e8e04eSSam Leffler 
75168e8e04eSSam Leffler /*
752c788ca3eSBill Paul  * When building the kernel with -O2 on the i386 architecture, gcc
753c788ca3eSBill Paul  * seems to want to inline this function into ieee80211_ioctl()
754c788ca3eSBill Paul  * (which is the only routine that calls it). When this happens,
755c788ca3eSBill Paul  * ieee80211_ioctl() ends up consuming an additional 2K of stack
756c788ca3eSBill Paul  * space. (Exactly why it needs so much is unclear.) The problem
757c788ca3eSBill Paul  * is that it's possible for ieee80211_ioctl() to invoke other
758c788ca3eSBill Paul  * routines (including driver init functions) which could then find
759c788ca3eSBill Paul  * themselves perilously close to exhausting the stack.
760c788ca3eSBill Paul  *
761c788ca3eSBill Paul  * To avoid this, we deliberately prevent gcc from inlining this
762c788ca3eSBill Paul  * routine. Another way to avoid this is to use less agressive
763c788ca3eSBill Paul  * optimization when compiling this file (i.e. -O instead of -O2)
764c788ca3eSBill Paul  * but special-casing the compilation of this one module in the
765c788ca3eSBill Paul  * build system would be awkward.
766c788ca3eSBill Paul  */
767b032f27cSSam Leffler static __noinline int
768b032f27cSSam Leffler ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
769b032f27cSSam Leffler     struct ieee80211req *ireq)
7708a1b9b6aSSam Leffler {
771b032f27cSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
772b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
773b032f27cSSam Leffler 	u_int kid, len;
77468e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
7751a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
776b032f27cSSam Leffler 	int error = 0;
7771a1e1d21SSam Leffler 
7781a1e1d21SSam Leffler 	switch (ireq->i_type) {
7791a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
780b032f27cSSam Leffler 		switch (vap->iv_state) {
7811a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
7821a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
783b032f27cSSam Leffler 			ireq->i_len = vap->iv_des_ssid[0].len;
784b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
7851a1e1d21SSam Leffler 			break;
7861a1e1d21SSam Leffler 		default:
787b032f27cSSam Leffler 			ireq->i_len = vap->iv_bss->ni_esslen;
788b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
7891a1e1d21SSam Leffler 			break;
7901a1e1d21SSam Leffler 		}
7911a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
7921a1e1d21SSam Leffler 		break;
7931a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
7941a1e1d21SSam Leffler 		ireq->i_val = 1;
7951a1e1d21SSam Leffler 		break;
7961a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
797b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
7988a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
799b032f27cSSam Leffler 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
8008a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
8018a1b9b6aSSam Leffler 		else
8028a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
8031a1e1d21SSam Leffler 		break;
8041a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
8051a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
8068a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8078a1b9b6aSSam Leffler 			return EINVAL;
808b032f27cSSam Leffler 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
8091a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
810acd3428bSRobert Watson 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
811b032f27cSSam Leffler 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
8121a1e1d21SSam Leffler 		} else {
8131a1e1d21SSam Leffler 			bzero(tmpkey, len);
8141a1e1d21SSam Leffler 		}
8151a1e1d21SSam Leffler 		ireq->i_len = len;
8161a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
8171a1e1d21SSam Leffler 		break;
8181a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
8191a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
8201a1e1d21SSam Leffler 		break;
8211a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
822b032f27cSSam Leffler 		ireq->i_val = vap->iv_def_txkey;
8231a1e1d21SSam Leffler 		break;
8241a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
825b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA)
8268a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
8278a1b9b6aSSam Leffler 		else
828b032f27cSSam Leffler 			ireq->i_val = vap->iv_bss->ni_authmode;
8291a1e1d21SSam Leffler 		break;
8301a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
831b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
8321a1e1d21SSam Leffler 		break;
8331a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
834b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PMGTON)
8351a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
8361a1e1d21SSam Leffler 		else
8371a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
8381a1e1d21SSam Leffler 		break;
8391a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
8401a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
8411a1e1d21SSam Leffler 		break;
84213604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
843b032f27cSSam Leffler 		ireq->i_val = vap->iv_rtsthreshold;
8441a1e1d21SSam Leffler 		break;
8452e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
8462e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
8472e79ca97SSam Leffler 		break;
8482e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
849b032f27cSSam Leffler 		/*
850b032f27cSSam Leffler 		 * Tx power limit is the min of max regulatory
851b032f27cSSam Leffler 		 * power, any user-set limit, and the max the
852b032f27cSSam Leffler 		 * radio can do.
853b032f27cSSam Leffler 		 */
854b032f27cSSam Leffler 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
855b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_txpowlimit)
8568a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_txpowlimit;
857b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
858b032f27cSSam Leffler 			ireq->i_val = ic->ic_curchan->ic_maxpower;
8598a1b9b6aSSam Leffler 		break;
8608a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
861b032f27cSSam Leffler 		switch (vap->iv_flags & IEEE80211_F_WPA) {
8628a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
8638a1b9b6aSSam Leffler 			ireq->i_val = 1;
8648a1b9b6aSSam Leffler 			break;
8658a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
8668a1b9b6aSSam Leffler 			ireq->i_val = 2;
8678a1b9b6aSSam Leffler 			break;
8688a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
8698a1b9b6aSSam Leffler 			ireq->i_val = 3;
8708a1b9b6aSSam Leffler 			break;
8718a1b9b6aSSam Leffler 		default:
8728a1b9b6aSSam Leffler 			ireq->i_val = 0;
8738a1b9b6aSSam Leffler 			break;
8748a1b9b6aSSam Leffler 		}
8758a1b9b6aSSam Leffler 		break;
8768a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
877b032f27cSSam Leffler 		error = ieee80211_ioctl_getchanlist(vap, ireq);
8788a1b9b6aSSam Leffler 		break;
8798a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
880b032f27cSSam Leffler 		ireq->i_val = vap->iv_roaming;
8818a1b9b6aSSam Leffler 		break;
8828a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
883b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
8848a1b9b6aSSam Leffler 		break;
8858a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
886b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
8878a1b9b6aSSam Leffler 		break;
8888a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
889b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
8908a1b9b6aSSam Leffler 		break;
8918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
892b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
8938a1b9b6aSSam Leffler 		break;
8948a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
895b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
8968a1b9b6aSSam Leffler 		break;
8978a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
898b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
8998a1b9b6aSSam Leffler 		break;
9008a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
901b032f27cSSam Leffler 		error = ieee80211_ioctl_getkey(vap, ireq);
9028a1b9b6aSSam Leffler 		break;
9038a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
904b032f27cSSam Leffler 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
9058a1b9b6aSSam Leffler 		break;
9068a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
9078a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
9088a1b9b6aSSam Leffler 			return EINVAL;
909b032f27cSSam Leffler 		error = copyout(vap->iv_state == IEEE80211_S_RUN ?
910b032f27cSSam Leffler 					vap->iv_bss->ni_bssid :
911b032f27cSSam Leffler 					vap->iv_des_bssid,
9128a1b9b6aSSam Leffler 				ireq->i_data, ireq->i_len);
9138a1b9b6aSSam Leffler 		break;
9148a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
915b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
91668e8e04eSSam Leffler 		break;
91768e8e04eSSam Leffler 	case IEEE80211_IOC_WPAIE2:
918b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
9198a1b9b6aSSam Leffler 		break;
9208a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
921b032f27cSSam Leffler 		error = ieee80211_ioctl_getscanresults(vap, ireq);
9228a1b9b6aSSam Leffler 		break;
9238a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
924b032f27cSSam Leffler 		error = ieee80211_ioctl_getstastats(vap, ireq);
9258a1b9b6aSSam Leffler 		break;
9268a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
927b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_txpower;
9288a1b9b6aSSam Leffler 		break;
9298a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
930b032f27cSSam Leffler 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
9318a1b9b6aSSam Leffler 		break;
9328a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
933b032f27cSSam Leffler 		error = ieee80211_ioctl_getstainfo(vap, ireq);
9348a1b9b6aSSam Leffler 		break;
9358a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
9368a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
9378a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
9388a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
9398a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
9408a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
941b032f27cSSam Leffler 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
9428a1b9b6aSSam Leffler 		break;
9438a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
944b032f27cSSam Leffler 		ireq->i_val = vap->iv_dtim_period;
9458a1b9b6aSSam Leffler 		break;
9468a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
9478a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
948b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_intval;
9492e79ca97SSam Leffler 		break;
950c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
951b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
952c4f040c3SSam Leffler 		break;
95368e8e04eSSam Leffler 	case IEEE80211_IOC_FF:
954b032f27cSSam Leffler 		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
95568e8e04eSSam Leffler 		break;
95668e8e04eSSam Leffler 	case IEEE80211_IOC_TURBOP:
957b032f27cSSam Leffler 		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
95868e8e04eSSam Leffler 		break;
95968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
960b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
96168e8e04eSSam Leffler 		break;
96268e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
963b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
96468e8e04eSSam Leffler 		break;
96568e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
966b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
96768e8e04eSSam Leffler 		break;
96868e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
969b032f27cSSam Leffler 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
97064353cb0SSam Leffler 		break;
97170231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
972b032f27cSSam Leffler 		ireq->i_val = vap->iv_fragthreshold;
97370231e3dSSam Leffler 		break;
974188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
975b032f27cSSam Leffler 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
976188757f5SSam Leffler 		break;
977c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
978b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
979c27e4e31SSam Leffler 		break;
980546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
981b032f27cSSam Leffler 		ireq->i_val = vap->iv_bmissthreshold;
982546786c9SSam Leffler 		break;
98368e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
984b032f27cSSam Leffler 		error = ieee80211_ioctl_getcurchan(vap, ireq);
98568e8e04eSSam Leffler 		break;
98668e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
98768e8e04eSSam Leffler 		ireq->i_val = 0;
988b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
98968e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
990b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
99168e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
99268e8e04eSSam Leffler 		break;
99368e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
99468e8e04eSSam Leffler 		ireq->i_val = 0;
995b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)
99668e8e04eSSam Leffler 			ireq->i_val |= 1;
997b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)
99868e8e04eSSam Leffler 			ireq->i_val |= 2;
99968e8e04eSSam Leffler 		break;
100068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
1001b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1002b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_rxmax;
1003b032f27cSSam Leffler 		else if (vap->iv_state == IEEE80211_S_RUN)
1004b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1005b032f27cSSam Leffler 			    IEEE80211_HTCAP_MAXRXAMPDU);
1006b032f27cSSam Leffler 		else
1007b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_limit;
100868e8e04eSSam Leffler 		break;
100968e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
1010900de995SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1011900de995SSam Leffler 		    vap->iv_state == IEEE80211_S_RUN)
1012b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1013b032f27cSSam Leffler 			    IEEE80211_HTCAP_MPDUDENSITY);
1014b032f27cSSam Leffler 		else
1015b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_density;
101668e8e04eSSam Leffler 		break;
101768e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
101868e8e04eSSam Leffler 		ireq->i_val = 0;
1019b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_TX)
102068e8e04eSSam Leffler 			ireq->i_val |= 1;
1021b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_RX)
102268e8e04eSSam Leffler 			ireq->i_val |= 2;
102368e8e04eSSam Leffler 		break;
102468e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
1025b032f27cSSam Leffler 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
102668e8e04eSSam Leffler 		break;
102768e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
1028b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) != 0;
102968e8e04eSSam Leffler 		break;
103068e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
1031b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1032b032f27cSSam Leffler 		break;
1033b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
1034b032f27cSSam Leffler 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1035b032f27cSSam Leffler 		break;
1036b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
1037b032f27cSSam Leffler 		error = ieee80211_ioctl_getroam(vap, ireq);
1038b032f27cSSam Leffler 		break;
1039b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
1040b032f27cSSam Leffler 		error = ieee80211_ioctl_gettxparams(vap, ireq);
104168e8e04eSSam Leffler 		break;
104268e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
1043b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) != 0;
1044b032f27cSSam Leffler 		break;
1045b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
1046b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
104768e8e04eSSam Leffler 		break;
1048c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
1049b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1050b032f27cSSam Leffler 		break;
1051b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
1052b032f27cSSam Leffler 		error = ieee80211_ioctl_getappie(vap, ireq);
1053b032f27cSSam Leffler 		break;
1054b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
1055b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1056b032f27cSSam Leffler 		break;
1057b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
1058b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1059b032f27cSSam Leffler 		break;
1060b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
1061b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1062b032f27cSSam Leffler 		break;
1063b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
1064b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1065b032f27cSSam Leffler 		break;
1066b032f27cSSam Leffler 	case IEEE80211_IOC_DEVCAPS:
1067b032f27cSSam Leffler 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1068c066143cSSam Leffler 		break;
10691b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
10701b6167d2SSam Leffler 		ireq->i_val = ic->ic_htprotmode;
10711b6167d2SSam Leffler 		break;
10721b6167d2SSam Leffler 	case IEEE80211_IOC_HTCONF:
1073b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_HT) {
10741b6167d2SSam Leffler 			ireq->i_val = 1;
1075b032f27cSSam Leffler 			if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
10761b6167d2SSam Leffler 				ireq->i_val |= 2;
10771b6167d2SSam Leffler 		} else
10781b6167d2SSam Leffler 			ireq->i_val = 0;
10791b6167d2SSam Leffler 		break;
1080b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
1081b032f27cSSam Leffler 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1082b032f27cSSam Leffler 		break;
10838c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
10848c070d69SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
10858c070d69SSam Leffler 		    vap->iv_state == IEEE80211_S_RUN) {
10868c070d69SSam Leffler 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
10878c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
10888c070d69SSam Leffler 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
10898c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
10908c070d69SSam Leffler 			else
10918c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
10928c070d69SSam Leffler 		} else
10938c070d69SSam Leffler 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
10948c070d69SSam Leffler 		break;
109544f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
109644f7a6edSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
109744f7a6edSSam Leffler 		    vap->iv_state == IEEE80211_S_RUN)
109844f7a6edSSam Leffler 			ireq->i_val =
109944f7a6edSSam Leffler 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
110044f7a6edSSam Leffler 		else
110144f7a6edSSam Leffler 			ireq->i_val =
110244f7a6edSSam Leffler 			    (vap->iv_flags_ext & IEEE80211_FEXT_RIFS) != 0;
110344f7a6edSSam Leffler 		break;
110410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
110510ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOT:
110610ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOTCNT:
110710ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOTLEN:
110810ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_BINTERVAL:
110910ad9a77SSam Leffler 		error = ieee80211_tdma_ioctl_get80211(vap, ireq);
111010ad9a77SSam Leffler 		break;
111110ad9a77SSam Leffler #endif
11121a1e1d21SSam Leffler 	default:
11131a1e1d21SSam Leffler 		error = EINVAL;
1114b2e95691SSam Leffler 		break;
11151a1e1d21SSam Leffler 	}
11168a1b9b6aSSam Leffler 	return error;
1117b032f27cSSam Leffler #undef MS
11188a1b9b6aSSam Leffler }
11198a1b9b6aSSam Leffler 
1120b032f27cSSam Leffler static __noinline int
1121b032f27cSSam Leffler ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11228a1b9b6aSSam Leffler {
11238a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
11248a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11258a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
112668e8e04eSSam Leffler 	uint16_t kid;
1127b032f27cSSam Leffler 	int error, i;
11288a1b9b6aSSam Leffler 
11298a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
11308a1b9b6aSSam Leffler 		return EINVAL;
11318a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
11321a1e1d21SSam Leffler 	if (error)
11338a1b9b6aSSam Leffler 		return error;
11348a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
11358a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
11368a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
11378a1b9b6aSSam Leffler 		return E2BIG;
11388a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
11398a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
11408a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
11418a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
11428a1b9b6aSSam Leffler 			return EINVAL;
1143b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1144b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1145b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1146b5d4660fSSam Leffler 				ieee80211_free_node(ni);
11478a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1148b5d4660fSSam Leffler 			}
11498a1b9b6aSSam Leffler 		} else {
1150b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1151b032f27cSSam Leffler 				ik.ik_macaddr);
11528a1b9b6aSSam Leffler 			if (ni == NULL)
11538a1b9b6aSSam Leffler 				return ENOENT;
11548a1b9b6aSSam Leffler 		}
11558a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
11568a1b9b6aSSam Leffler 	} else {
11578a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
11588a1b9b6aSSam Leffler 			return EINVAL;
1159b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
1160386d84f6SSam Leffler 		/*
1161386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1162386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1163386d84f6SSam Leffler 		 */
1164386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1165386d84f6SSam Leffler 			wk->wk_keyix = kid;
11668a1b9b6aSSam Leffler 		ni = NULL;
11678a1b9b6aSSam Leffler 	}
11688a1b9b6aSSam Leffler 	error = 0;
1169b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
1170b032f27cSSam Leffler 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
11718a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
11728a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
11738a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
11748a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1175b032f27cSSam Leffler 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1176b032f27cSSam Leffler 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
11778a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
11788a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
11798a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
118071fe06caSSam Leffler 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
118171fe06caSSam Leffler 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
118271fe06caSSam Leffler 		if (!ieee80211_crypto_setkey(vap, wk))
11838a1b9b6aSSam Leffler 			error = EIO;
11848a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1185b032f27cSSam Leffler 			vap->iv_def_txkey = kid;
11868a1b9b6aSSam Leffler 	} else
11878a1b9b6aSSam Leffler 		error = ENXIO;
1188b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
11898a1b9b6aSSam Leffler 	if (ni != NULL)
11908a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
11918a1b9b6aSSam Leffler 	return error;
11928a1b9b6aSSam Leffler }
11938a1b9b6aSSam Leffler 
1194b032f27cSSam Leffler static __noinline int
1195b032f27cSSam Leffler ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11968a1b9b6aSSam Leffler {
11978a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
11988a1b9b6aSSam Leffler 	int kid, error;
11998a1b9b6aSSam Leffler 
12008a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
12018a1b9b6aSSam Leffler 		return EINVAL;
12028a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
12038a1b9b6aSSam Leffler 	if (error)
12048a1b9b6aSSam Leffler 		return error;
12058a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
120668e8e04eSSam Leffler 	/* XXX uint8_t -> uint16_t */
120768e8e04eSSam Leffler 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
12088a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
12098a1b9b6aSSam Leffler 
1210b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1211b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1212b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1213b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1214b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1215b5d4660fSSam Leffler 			}
1216b5d4660fSSam Leffler 		} else {
1217b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1218b032f27cSSam Leffler 				dk.idk_macaddr);
12198a1b9b6aSSam Leffler 			if (ni == NULL)
1220b5d4660fSSam Leffler 				return ENOENT;
1221b5d4660fSSam Leffler 		}
12228a1b9b6aSSam Leffler 		/* XXX error return */
1223c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
12248a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12258a1b9b6aSSam Leffler 	} else {
12268a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12278a1b9b6aSSam Leffler 			return EINVAL;
12288a1b9b6aSSam Leffler 		/* XXX error return */
1229b032f27cSSam Leffler 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
12308a1b9b6aSSam Leffler 	}
12318a1b9b6aSSam Leffler 	return 0;
12328a1b9b6aSSam Leffler }
12338a1b9b6aSSam Leffler 
1234b032f27cSSam Leffler struct mlmeop {
1235b032f27cSSam Leffler 	struct ieee80211vap *vap;
1236b032f27cSSam Leffler 	int	op;
1237b032f27cSSam Leffler 	int	reason;
1238b032f27cSSam Leffler };
1239b032f27cSSam Leffler 
1240b032f27cSSam Leffler static void
1241b032f27cSSam Leffler mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1242b032f27cSSam Leffler 	int op, int reason)
1243b032f27cSSam Leffler {
1244b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1245b032f27cSSam Leffler 	static const struct {
1246b032f27cSSam Leffler 		int mask;
1247b032f27cSSam Leffler 		const char *opstr;
1248b032f27cSSam Leffler 	} ops[] = {
1249b032f27cSSam Leffler 		{ 0, "op#0" },
1250b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1251b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "assoc" },
1252b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1253b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "disassoc" },
1254b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1255b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "deauth" },
1256b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1257b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "authorize" },
1258b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1259b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "unauthorize" },
1260b032f27cSSam Leffler 	};
1261b032f27cSSam Leffler 
1262b032f27cSSam Leffler 	if (op == IEEE80211_MLME_AUTH) {
1263b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1264b032f27cSSam Leffler 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1265b032f27cSSam Leffler 		    "station authenticate %s via MLME (reason %d)",
1266b032f27cSSam Leffler 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1267b032f27cSSam Leffler 		    reason);
1268b032f27cSSam Leffler 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1269b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1270b032f27cSSam Leffler 		    "unknown MLME request %d (reason %d)", op, reason);
1271b032f27cSSam Leffler 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1272b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1273b032f27cSSam Leffler 		    "station %s via MLME", ops[op].opstr);
1274b032f27cSSam Leffler 	} else {
1275b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1276b032f27cSSam Leffler 		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1277b032f27cSSam Leffler 	}
1278b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
1279b032f27cSSam Leffler }
1280b032f27cSSam Leffler 
12818a1b9b6aSSam Leffler static void
12828a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
12838a1b9b6aSSam Leffler {
1284b032f27cSSam Leffler 	struct mlmeop *mop = arg;
1285b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
12868a1b9b6aSSam Leffler 
1287b032f27cSSam Leffler 	if (vap != mop->vap)
1288b032f27cSSam Leffler 		return;
1289b032f27cSSam Leffler 	/*
1290b032f27cSSam Leffler 	 * NB: if ni_associd is zero then the node is already cleaned
1291b032f27cSSam Leffler 	 * up and we don't need to do this (we're safely holding a
1292b032f27cSSam Leffler 	 * reference but should otherwise not modify it's state).
1293b032f27cSSam Leffler 	 */
1294b032f27cSSam Leffler 	if (ni->ni_associd == 0)
1295b032f27cSSam Leffler 		return;
1296b032f27cSSam Leffler 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1297b032f27cSSam Leffler 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1298b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1299b032f27cSSam Leffler 		    mop->reason);
1300b032f27cSSam Leffler 	} else {
1301b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1302b032f27cSSam Leffler 		    mop->reason);
13038a1b9b6aSSam Leffler 	}
1304b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1305b032f27cSSam Leffler }
1306b032f27cSSam Leffler 
1307b032f27cSSam Leffler static int
1308b032f27cSSam Leffler setmlme_dropsta(struct ieee80211vap *vap,
1309b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1310b032f27cSSam Leffler {
1311b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1312b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1313b032f27cSSam Leffler 	struct ieee80211_node *ni;
1314b032f27cSSam Leffler 	int error = 0;
1315b032f27cSSam Leffler 
1316b032f27cSSam Leffler 	/* NB: the broadcast address means do 'em all */
1317b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1318b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1319b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, mac);
1320b032f27cSSam Leffler 		if (ni != NULL) {
1321b032f27cSSam Leffler 			domlme(mlmeop, ni);
1322b032f27cSSam Leffler 			ieee80211_free_node(ni);
1323b032f27cSSam Leffler 		} else
1324b032f27cSSam Leffler 			error = ENOENT;
1325b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1326b032f27cSSam Leffler 	} else {
1327b032f27cSSam Leffler 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1328b032f27cSSam Leffler 	}
1329b032f27cSSam Leffler 	return error;
1330b032f27cSSam Leffler }
1331b032f27cSSam Leffler 
1332b032f27cSSam Leffler static __noinline int
1333b032f27cSSam Leffler setmlme_common(struct ieee80211vap *vap, int op,
1334b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1335b032f27cSSam Leffler {
1336b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1337b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1338b032f27cSSam Leffler 	struct ieee80211_node *ni;
1339b032f27cSSam Leffler 	struct mlmeop mlmeop;
1340b032f27cSSam Leffler 	int error;
1341b032f27cSSam Leffler 
1342b032f27cSSam Leffler 	error = 0;
1343b032f27cSSam Leffler 	switch (op) {
1344b032f27cSSam Leffler 	case IEEE80211_MLME_DISASSOC:
1345b032f27cSSam Leffler 	case IEEE80211_MLME_DEAUTH:
1346b032f27cSSam Leffler 		switch (vap->iv_opmode) {
1347b032f27cSSam Leffler 		case IEEE80211_M_STA:
1348b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1349b032f27cSSam Leffler 			/* XXX not quite right */
1350b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1351b032f27cSSam Leffler 			break;
1352b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
1353b032f27cSSam Leffler 			mlmeop.vap = vap;
1354b032f27cSSam Leffler 			mlmeop.op = op;
1355b032f27cSSam Leffler 			mlmeop.reason = reason;
1356b032f27cSSam Leffler 			error = setmlme_dropsta(vap, mac, &mlmeop);
1357b032f27cSSam Leffler 			break;
1358b032f27cSSam Leffler 		case IEEE80211_M_WDS:
1359b032f27cSSam Leffler 			/* XXX user app should send raw frame? */
1360b032f27cSSam Leffler 			if (op != IEEE80211_MLME_DEAUTH) {
1361b032f27cSSam Leffler 				error = EINVAL;
1362b032f27cSSam Leffler 				break;
1363b032f27cSSam Leffler 			}
1364b032f27cSSam Leffler #if 0
1365b032f27cSSam Leffler 			/* XXX accept any address, simplifies user code */
1366b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1367b032f27cSSam Leffler 				error = EINVAL;
1368b032f27cSSam Leffler 				break;
1369b032f27cSSam Leffler 			}
1370b032f27cSSam Leffler #endif
1371b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1372b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1373b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
1374b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1375b032f27cSSam Leffler 			ieee80211_free_node(ni);
1376b032f27cSSam Leffler 			break;
1377b032f27cSSam Leffler 		default:
1378b032f27cSSam Leffler 			error = EINVAL;
1379b032f27cSSam Leffler 			break;
1380b032f27cSSam Leffler 		}
1381b032f27cSSam Leffler 		break;
1382b032f27cSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
1383b032f27cSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
1384b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1385b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_WDS) {
1386b032f27cSSam Leffler 			error = EINVAL;
1387b032f27cSSam Leffler 			break;
1388b032f27cSSam Leffler 		}
1389b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1390b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1391b032f27cSSam Leffler 		if (ni != NULL) {
1392b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1393b032f27cSSam Leffler 			if (op == IEEE80211_MLME_AUTHORIZE)
1394b032f27cSSam Leffler 				ieee80211_node_authorize(ni);
1395b032f27cSSam Leffler 			else
1396b032f27cSSam Leffler 				ieee80211_node_unauthorize(ni);
1397b032f27cSSam Leffler 			ieee80211_free_node(ni);
1398b032f27cSSam Leffler 		} else
1399b032f27cSSam Leffler 			error = ENOENT;
1400b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1401b032f27cSSam Leffler 		break;
1402b032f27cSSam Leffler 	case IEEE80211_MLME_AUTH:
1403b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1404b032f27cSSam Leffler 			error = EINVAL;
1405b032f27cSSam Leffler 			break;
1406b032f27cSSam Leffler 		}
1407b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1408b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1409b032f27cSSam Leffler 		if (ni != NULL) {
1410b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1411b032f27cSSam Leffler 			if (reason == IEEE80211_STATUS_SUCCESS) {
1412b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
1413b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1414b032f27cSSam Leffler 				/*
1415b032f27cSSam Leffler 				 * For shared key auth, just continue the
1416b032f27cSSam Leffler 				 * exchange.  Otherwise when 802.1x is not in
1417b032f27cSSam Leffler 				 * use mark the port authorized at this point
1418b032f27cSSam Leffler 				 * so traffic can flow.
1419b032f27cSSam Leffler 				 */
1420b032f27cSSam Leffler 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1421b032f27cSSam Leffler 				    ni->ni_challenge == NULL)
1422b032f27cSSam Leffler 				      ieee80211_node_authorize(ni);
1423b032f27cSSam Leffler 			} else {
1424b032f27cSSam Leffler 				vap->iv_stats.is_rx_acl++;
1425b032f27cSSam Leffler 				ieee80211_send_error(ni, ni->ni_macaddr,
1426b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1427b032f27cSSam Leffler 				ieee80211_node_leave(ni);
1428b032f27cSSam Leffler 			}
1429b032f27cSSam Leffler 			ieee80211_free_node(ni);
1430b032f27cSSam Leffler 		} else
1431b032f27cSSam Leffler 			error = ENOENT;
1432b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1433b032f27cSSam Leffler 		break;
1434b032f27cSSam Leffler 	default:
1435b032f27cSSam Leffler 		error = EINVAL;
1436b032f27cSSam Leffler 		break;
1437b032f27cSSam Leffler 	}
1438b032f27cSSam Leffler 	return error;
14398a1b9b6aSSam Leffler }
14408a1b9b6aSSam Leffler 
144168e8e04eSSam Leffler struct scanlookup {
144268e8e04eSSam Leffler 	const uint8_t *mac;
144368e8e04eSSam Leffler 	int esslen;
144468e8e04eSSam Leffler 	const uint8_t *essid;
144568e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se;
144668e8e04eSSam Leffler };
144768e8e04eSSam Leffler 
144868e8e04eSSam Leffler /*
144968e8e04eSSam Leffler  * Match mac address and any ssid.
145068e8e04eSSam Leffler  */
145168e8e04eSSam Leffler static void
145268e8e04eSSam Leffler mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
145368e8e04eSSam Leffler {
145468e8e04eSSam Leffler 	struct scanlookup *look = arg;
145568e8e04eSSam Leffler 
145668e8e04eSSam Leffler 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
145768e8e04eSSam Leffler 		return;
145868e8e04eSSam Leffler 	if (look->esslen != 0) {
145968e8e04eSSam Leffler 		if (se->se_ssid[1] != look->esslen)
146068e8e04eSSam Leffler 			return;
146168e8e04eSSam Leffler 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
146268e8e04eSSam Leffler 			return;
146368e8e04eSSam Leffler 	}
146468e8e04eSSam Leffler 	look->se = se;
146568e8e04eSSam Leffler }
146668e8e04eSSam Leffler 
1467b032f27cSSam Leffler static __noinline int
1468b032f27cSSam Leffler setmlme_assoc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1469b032f27cSSam Leffler 	int ssid_len, const uint8_t ssid[IEEE80211_NWID_LEN])
1470b032f27cSSam Leffler {
1471b032f27cSSam Leffler 	struct scanlookup lookup;
1472b032f27cSSam Leffler 
1473b032f27cSSam Leffler 	/* XXX ibss/ahdemo */
1474b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
1475b032f27cSSam Leffler 		return EINVAL;
1476b032f27cSSam Leffler 
1477b032f27cSSam Leffler 	/* NB: this is racey if roaming is !manual */
1478b032f27cSSam Leffler 	lookup.se = NULL;
1479b032f27cSSam Leffler 	lookup.mac = mac;
1480b032f27cSSam Leffler 	lookup.esslen = ssid_len;
1481b032f27cSSam Leffler 	lookup.essid = ssid;
1482b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1483b032f27cSSam Leffler 	if (lookup.se == NULL)
1484b032f27cSSam Leffler 		return ENOENT;
1485b032f27cSSam Leffler 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
148610959256SSam Leffler 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1487b032f27cSSam Leffler 		return EIO;		/* XXX unique but could be better */
1488b032f27cSSam Leffler 	return 0;
1489b032f27cSSam Leffler }
1490b032f27cSSam Leffler 
1491b032f27cSSam Leffler static __noinline int
1492b032f27cSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
14938a1b9b6aSSam Leffler {
14948a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
14958a1b9b6aSSam Leffler 	int error;
14968a1b9b6aSSam Leffler 
14978a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
14988a1b9b6aSSam Leffler 		return EINVAL;
14998a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
15008a1b9b6aSSam Leffler 	if (error)
15018a1b9b6aSSam Leffler 		return error;
1502b032f27cSSam Leffler 	if  (mlme.im_op == IEEE80211_MLME_ASSOC)
1503b032f27cSSam Leffler 		return setmlme_assoc(vap, mlme.im_macaddr,
1504b032f27cSSam Leffler 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
15058a1b9b6aSSam Leffler 	else
1506b032f27cSSam Leffler 		return setmlme_common(vap, mlme.im_op,
1507b032f27cSSam Leffler 		    mlme.im_macaddr, mlme.im_reason);
15088a1b9b6aSSam Leffler }
15098a1b9b6aSSam Leffler 
1510b032f27cSSam Leffler static __noinline int
1511b032f27cSSam Leffler ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
15128a1b9b6aSSam Leffler {
151368e8e04eSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1514b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
15158a1b9b6aSSam Leffler 	int error;
15168a1b9b6aSSam Leffler 
15178a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
15188a1b9b6aSSam Leffler 		return EINVAL;
15198a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
15208a1b9b6aSSam Leffler 	if (error)
15218a1b9b6aSSam Leffler 		return error;
15228a1b9b6aSSam Leffler 	if (acl == NULL) {
15238a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
1524b032f27cSSam Leffler 		if (acl == NULL || !acl->iac_attach(vap))
15258a1b9b6aSSam Leffler 			return EINVAL;
1526b032f27cSSam Leffler 		vap->iv_acl = acl;
15278a1b9b6aSSam Leffler 	}
15288a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1529b032f27cSSam Leffler 		acl->iac_add(vap, mac);
15308a1b9b6aSSam Leffler 	else
1531b032f27cSSam Leffler 		acl->iac_remove(vap, mac);
15328a1b9b6aSSam Leffler 	return 0;
15338a1b9b6aSSam Leffler }
15348a1b9b6aSSam Leffler 
1535b032f27cSSam Leffler static __noinline int
1536b032f27cSSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
15378a1b9b6aSSam Leffler {
1538b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
15398a1b9b6aSSam Leffler 
15408a1b9b6aSSam Leffler 	switch (ireq->i_val) {
15418a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
15428a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
15438a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
1544b032f27cSSam Leffler 	case IEEE80211_MACCMD_POLICY_RADIUS:
15458a1b9b6aSSam Leffler 		if (acl == NULL) {
15468a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
1547b032f27cSSam Leffler 			if (acl == NULL || !acl->iac_attach(vap))
15488a1b9b6aSSam Leffler 				return EINVAL;
1549b032f27cSSam Leffler 			vap->iv_acl = acl;
15508a1b9b6aSSam Leffler 		}
1551b032f27cSSam Leffler 		acl->iac_setpolicy(vap, ireq->i_val);
15528a1b9b6aSSam Leffler 		break;
15538a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
15548a1b9b6aSSam Leffler 		if (acl != NULL)
1555b032f27cSSam Leffler 			acl->iac_flush(vap);
15568a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
15578a1b9b6aSSam Leffler 		break;
15588a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
15598a1b9b6aSSam Leffler 		if (acl != NULL) {
1560b032f27cSSam Leffler 			vap->iv_acl = NULL;
1561b032f27cSSam Leffler 			acl->iac_detach(vap);
15628a1b9b6aSSam Leffler 		}
15638a1b9b6aSSam Leffler 		break;
15648a1b9b6aSSam Leffler 	default:
1565188757f5SSam Leffler 		if (acl == NULL)
15668a1b9b6aSSam Leffler 			return EINVAL;
1567188757f5SSam Leffler 		else
1568b032f27cSSam Leffler 			return acl->iac_setioctl(vap, ireq);
15698a1b9b6aSSam Leffler 	}
15708a1b9b6aSSam Leffler 	return 0;
15718a1b9b6aSSam Leffler }
15728a1b9b6aSSam Leffler 
1573b032f27cSSam Leffler static __noinline int
1574b032f27cSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
15758a1b9b6aSSam Leffler {
1576b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
15778658b18bSSam Leffler 	uint8_t *chanlist, *list;
15788658b18bSSam Leffler 	int i, nchan, maxchan, error;
15798a1b9b6aSSam Leffler 
15808658b18bSSam Leffler 	if (ireq->i_len > sizeof(ic->ic_chan_active))
15818658b18bSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
15828658b18bSSam Leffler 	list = malloc(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
15838658b18bSSam Leffler 	    M_NOWAIT | M_ZERO);
15848658b18bSSam Leffler 	if (list == NULL)
15858658b18bSSam Leffler 		return ENOMEM;
15868658b18bSSam Leffler 	error = copyin(ireq->i_data, list, ireq->i_len);
15878a1b9b6aSSam Leffler 	if (error)
15888a1b9b6aSSam Leffler 		return error;
158968e8e04eSSam Leffler 	nchan = 0;
15908658b18bSSam Leffler 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
15918658b18bSSam Leffler 	maxchan = ireq->i_len * NBBY;
159231378b1cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
159331378b1cSSam Leffler 		const struct ieee80211_channel *c = &ic->ic_channels[i];
15948a1b9b6aSSam Leffler 		/*
159531378b1cSSam Leffler 		 * Calculate the intersection of the user list and the
159631378b1cSSam Leffler 		 * available channels so users can do things like specify
159731378b1cSSam Leffler 		 * 1-255 to get all available channels.
15988a1b9b6aSSam Leffler 		 */
15998658b18bSSam Leffler 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
160031378b1cSSam Leffler 			setbit(chanlist, c->ic_ieee);
160168e8e04eSSam Leffler 			nchan++;
16028a1b9b6aSSam Leffler 		}
16038a1b9b6aSSam Leffler 	}
160468e8e04eSSam Leffler 	if (nchan == 0)
160568e8e04eSSam Leffler 		return EINVAL;
160668e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
160768e8e04eSSam Leffler 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
160868e8e04eSSam Leffler 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
16098658b18bSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1610b032f27cSSam Leffler 	ieee80211_scan_flush(vap);
16118658b18bSSam Leffler 	free(list, M_TEMP);
1612b032f27cSSam Leffler 	return ENETRESET;
16138a1b9b6aSSam Leffler }
16148a1b9b6aSSam Leffler 
1615b032f27cSSam Leffler static __noinline int
1616b032f27cSSam Leffler ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
161742568791SSam Leffler {
161842568791SSam Leffler 	struct ieee80211_node *ni;
161968e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
162042568791SSam Leffler 	int error;
162142568791SSam Leffler 
162242568791SSam Leffler 	/*
162342568791SSam Leffler 	 * NB: we could copyin ieee80211req_sta_stats so apps
162442568791SSam Leffler 	 *     could make selective changes but that's overkill;
162542568791SSam Leffler 	 *     just clear all stats for now.
162642568791SSam Leffler 	 */
162742568791SSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
162842568791SSam Leffler 		return EINVAL;
162942568791SSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
163042568791SSam Leffler 	if (error != 0)
163142568791SSam Leffler 		return error;
1632b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
163342568791SSam Leffler 	if (ni == NULL)
1634b032f27cSSam Leffler 		return ENOENT;
1635b032f27cSSam Leffler 	/* XXX require ni_vap == vap? */
163642568791SSam Leffler 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
163742568791SSam Leffler 	ieee80211_free_node(ni);
163842568791SSam Leffler 	return 0;
163942568791SSam Leffler }
164042568791SSam Leffler 
1641b032f27cSSam Leffler static __noinline int
1642b032f27cSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
16438a1b9b6aSSam Leffler {
16448a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16458a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
16468a1b9b6aSSam Leffler 	int error;
16478a1b9b6aSSam Leffler 
16488a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
16498a1b9b6aSSam Leffler 		return EINVAL;
16508a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
16518a1b9b6aSSam Leffler 	if (error != 0)
16528a1b9b6aSSam Leffler 		return error;
1653b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
16548a1b9b6aSSam Leffler 	if (ni == NULL)
1655b032f27cSSam Leffler 		return ENOENT;
16568a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
16578a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
16588a1b9b6aSSam Leffler 	return error;
16598a1b9b6aSSam Leffler }
16608a1b9b6aSSam Leffler 
1661b032f27cSSam Leffler static __noinline int
1662b032f27cSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
16638a1b9b6aSSam Leffler {
1664b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
16658a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
16668a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
16678a1b9b6aSSam Leffler 	int isbss, ac;
16688a1b9b6aSSam Leffler 
16698a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1670b032f27cSSam Leffler 		return EOPNOTSUPP;
16718a1b9b6aSSam Leffler 
16728a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
16738a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
16748a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
16758a1b9b6aSSam Leffler 		ac = WME_AC_BE;
16768a1b9b6aSSam Leffler 	if (isbss) {
16778a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
16788a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
16798a1b9b6aSSam Leffler 	} else {
16808a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
16818a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
16828a1b9b6aSSam Leffler 	}
16838a1b9b6aSSam Leffler 	switch (ireq->i_type) {
16848a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
16858a1b9b6aSSam Leffler 		if (isbss) {
16868a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = ireq->i_val;
16878a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
16888a1b9b6aSSam Leffler 				chanp->wmep_logcwmin = ireq->i_val;
16898a1b9b6aSSam Leffler 		} else {
16908a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
16918a1b9b6aSSam Leffler 				ireq->i_val;
16928a1b9b6aSSam Leffler 		}
16938a1b9b6aSSam Leffler 		break;
16948a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
16958a1b9b6aSSam Leffler 		if (isbss) {
16968a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = ireq->i_val;
16978a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
16988a1b9b6aSSam Leffler 				chanp->wmep_logcwmax = ireq->i_val;
16998a1b9b6aSSam Leffler 		} else {
17008a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
17018a1b9b6aSSam Leffler 				ireq->i_val;
17028a1b9b6aSSam Leffler 		}
17038a1b9b6aSSam Leffler 		break;
17048a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
17058a1b9b6aSSam Leffler 		if (isbss) {
17068a1b9b6aSSam Leffler 			wmep->wmep_aifsn = ireq->i_val;
17078a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
17088a1b9b6aSSam Leffler 				chanp->wmep_aifsn = ireq->i_val;
17098a1b9b6aSSam Leffler 		} else {
17108a1b9b6aSSam Leffler 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
17118a1b9b6aSSam Leffler 		}
17128a1b9b6aSSam Leffler 		break;
17138a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
17148a1b9b6aSSam Leffler 		if (isbss) {
17158a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = ireq->i_val;
17168a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
17178a1b9b6aSSam Leffler 				chanp->wmep_txopLimit = ireq->i_val;
17188a1b9b6aSSam Leffler 		} else {
17198a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
17208a1b9b6aSSam Leffler 				ireq->i_val;
17218a1b9b6aSSam Leffler 		}
17228a1b9b6aSSam Leffler 		break;
17238a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
17248a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
17258a1b9b6aSSam Leffler 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
17268a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
17278a1b9b6aSSam Leffler 		break;
17288a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
17298a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
17308a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
17318a1b9b6aSSam Leffler 		break;
17328a1b9b6aSSam Leffler 	}
1733b032f27cSSam Leffler 	ieee80211_wme_updateparams(vap);
17348a1b9b6aSSam Leffler 	return 0;
17358a1b9b6aSSam Leffler }
17368a1b9b6aSSam Leffler 
17378a1b9b6aSSam Leffler static int
173868e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int start, int freq)
173968e8e04eSSam Leffler {
174068e8e04eSSam Leffler 	const struct ieee80211_channel *c;
174168e8e04eSSam Leffler 	int i;
174268e8e04eSSam Leffler 
174368e8e04eSSam Leffler 	for (i = start+1; i < ic->ic_nchans; i++) {
174468e8e04eSSam Leffler 		c = &ic->ic_channels[i];
174568e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
174668e8e04eSSam Leffler 			return 1;
174768e8e04eSSam Leffler 	}
174868e8e04eSSam Leffler 	/* NB: should not be needed but in case things are mis-sorted */
174968e8e04eSSam Leffler 	for (i = 0; i < start; i++) {
175068e8e04eSSam Leffler 		c = &ic->ic_channels[i];
175168e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
175268e8e04eSSam Leffler 			return 1;
175368e8e04eSSam Leffler 	}
175468e8e04eSSam Leffler 	return 0;
175568e8e04eSSam Leffler }
175668e8e04eSSam Leffler 
175768e8e04eSSam Leffler static struct ieee80211_channel *
175868e8e04eSSam Leffler findchannel(struct ieee80211com *ic, int ieee, int mode)
175968e8e04eSSam Leffler {
176068e8e04eSSam Leffler 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1761be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= 0,
1762be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1763be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1764be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1765be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1766be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1767be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1768be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
176968e8e04eSSam Leffler 	    /* NB: handled specially below */
1770be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1771be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
177268e8e04eSSam Leffler 	};
177368e8e04eSSam Leffler 	u_int modeflags;
177468e8e04eSSam Leffler 	int i;
177568e8e04eSSam Leffler 
177668e8e04eSSam Leffler 	modeflags = chanflags[mode];
177768e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
177868e8e04eSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
177968e8e04eSSam Leffler 
178068e8e04eSSam Leffler 		if (c->ic_ieee != ieee)
178168e8e04eSSam Leffler 			continue;
178268e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
178368e8e04eSSam Leffler 			/* ignore turbo channels for autoselect */
178468e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_TURBO(c))
178568e8e04eSSam Leffler 				continue;
178668e8e04eSSam Leffler 			/*
178768e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we
178868e8e04eSSam Leffler 			 *     always select the g channel if both
178968e8e04eSSam Leffler 			 *     are present.
179068e8e04eSSam Leffler 			 * XXX prefer HT to non-HT?
179168e8e04eSSam Leffler 			 */
179268e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_B(c) ||
179368e8e04eSSam Leffler 			    !find11gchannel(ic, i, c->ic_freq))
179468e8e04eSSam Leffler 				return c;
179568e8e04eSSam Leffler 		} else {
179668e8e04eSSam Leffler 			/* must check HT specially */
179768e8e04eSSam Leffler 			if ((mode == IEEE80211_MODE_11NA ||
179868e8e04eSSam Leffler 			    mode == IEEE80211_MODE_11NG) &&
179968e8e04eSSam Leffler 			    !IEEE80211_IS_CHAN_HT(c))
180068e8e04eSSam Leffler 				continue;
180168e8e04eSSam Leffler 			if ((c->ic_flags & modeflags) == modeflags)
180268e8e04eSSam Leffler 				return c;
180368e8e04eSSam Leffler 		}
180468e8e04eSSam Leffler 	}
180568e8e04eSSam Leffler 	return NULL;
180668e8e04eSSam Leffler }
180768e8e04eSSam Leffler 
180868e8e04eSSam Leffler /*
180968e8e04eSSam Leffler  * Check the specified against any desired mode (aka netband).
181068e8e04eSSam Leffler  * This is only used (presently) when operating in hostap mode
181168e8e04eSSam Leffler  * to enforce consistency.
181268e8e04eSSam Leffler  */
181368e8e04eSSam Leffler static int
181468e8e04eSSam Leffler check_mode_consistency(const struct ieee80211_channel *c, int mode)
181568e8e04eSSam Leffler {
181668e8e04eSSam Leffler 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
181768e8e04eSSam Leffler 
181868e8e04eSSam Leffler 	switch (mode) {
181968e8e04eSSam Leffler 	case IEEE80211_MODE_11B:
182068e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_B(c));
182168e8e04eSSam Leffler 	case IEEE80211_MODE_11G:
182268e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
182368e8e04eSSam Leffler 	case IEEE80211_MODE_11A:
182468e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
182568e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
182668e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_STURBO(c));
182768e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
182868e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTA(c));
182968e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
183068e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTG(c));
183168e8e04eSSam Leffler 	}
183268e8e04eSSam Leffler 	return 1;
183368e8e04eSSam Leffler 
183468e8e04eSSam Leffler }
183568e8e04eSSam Leffler 
183668e8e04eSSam Leffler /*
183768e8e04eSSam Leffler  * Common code to set the current channel.  If the device
183868e8e04eSSam Leffler  * is up and running this may result in an immediate channel
183968e8e04eSSam Leffler  * change or a kick of the state machine.
184068e8e04eSSam Leffler  */
184168e8e04eSSam Leffler static int
1842b032f27cSSam Leffler setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
184368e8e04eSSam Leffler {
1844b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
184568e8e04eSSam Leffler 	int error;
184668e8e04eSSam Leffler 
184768e8e04eSSam Leffler 	if (c != IEEE80211_CHAN_ANYC) {
1848b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1849b032f27cSSam Leffler 			return EBUSY;	/* XXX better code? */
1850b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1851b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
185268e8e04eSSam Leffler 				return EINVAL;
1853b032f27cSSam Leffler 			if (!check_mode_consistency(c, vap->iv_des_mode))
1854b032f27cSSam Leffler 				return EINVAL;
1855b032f27cSSam Leffler 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1856b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1857b032f27cSSam Leffler 				return EINVAL;
1858b032f27cSSam Leffler 		}
1859b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN &&
1860b032f27cSSam Leffler 		    vap->iv_bss->ni_chan == c)
186168e8e04eSSam Leffler 			return 0;	/* NB: nothing to do */
186268e8e04eSSam Leffler 	}
1863b032f27cSSam Leffler 	vap->iv_des_chan = c;
186468e8e04eSSam Leffler 
186568e8e04eSSam Leffler 	error = 0;
1866b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1867b032f27cSSam Leffler 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
186868e8e04eSSam Leffler 		/*
1869b032f27cSSam Leffler 		 * Monitor mode can switch directly.
187068e8e04eSSam Leffler 		 */
1871b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1872b032f27cSSam Leffler 			/* XXX need state machine for other vap's to follow */
1873b032f27cSSam Leffler 			ieee80211_setcurchan(ic, vap->iv_des_chan);
1874b032f27cSSam Leffler 			vap->iv_bss->ni_chan = ic->ic_curchan;
1875b032f27cSSam Leffler 		} else
1876b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
187768e8e04eSSam Leffler 	} else {
187868e8e04eSSam Leffler 		/*
187968e8e04eSSam Leffler 		 * Need to go through the state machine in case we
188068e8e04eSSam Leffler 		 * need to reassociate or the like.  The state machine
188168e8e04eSSam Leffler 		 * will pickup the desired channel and avoid scanning.
188268e8e04eSSam Leffler 		 */
1883b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
1884b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1885b032f27cSSam Leffler 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
188668e8e04eSSam Leffler 			/*
188768e8e04eSSam Leffler 			 * When not up+running and a real channel has
188868e8e04eSSam Leffler 			 * been specified fix the current channel so
188968e8e04eSSam Leffler 			 * there is immediate feedback; e.g. via ifconfig.
189068e8e04eSSam Leffler 			 */
1891b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
189268e8e04eSSam Leffler 		}
189368e8e04eSSam Leffler 	}
189468e8e04eSSam Leffler 	return error;
189568e8e04eSSam Leffler }
189668e8e04eSSam Leffler 
189768e8e04eSSam Leffler /*
189868e8e04eSSam Leffler  * Old api for setting the current channel; this is
189968e8e04eSSam Leffler  * deprecated because channel numbers are ambiguous.
190068e8e04eSSam Leffler  */
1901b032f27cSSam Leffler static __noinline int
1902b032f27cSSam Leffler ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
190368e8e04eSSam Leffler 	const struct ieee80211req *ireq)
190468e8e04eSSam Leffler {
1905b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
190668e8e04eSSam Leffler 	struct ieee80211_channel *c;
190768e8e04eSSam Leffler 
190868e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
190968e8e04eSSam Leffler 	if (ireq->i_val == 0 ||
191068e8e04eSSam Leffler 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
191168e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
191268e8e04eSSam Leffler 	} else {
191368e8e04eSSam Leffler 		struct ieee80211_channel *c2;
191468e8e04eSSam Leffler 
1915b032f27cSSam Leffler 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
191668e8e04eSSam Leffler 		if (c == NULL) {
191768e8e04eSSam Leffler 			c = findchannel(ic, ireq->i_val,
191868e8e04eSSam Leffler 				IEEE80211_MODE_AUTO);
191968e8e04eSSam Leffler 			if (c == NULL)
192068e8e04eSSam Leffler 				return EINVAL;
192168e8e04eSSam Leffler 		}
192268e8e04eSSam Leffler 		/*
192368e8e04eSSam Leffler 		 * Fine tune channel selection based on desired mode:
192468e8e04eSSam Leffler 		 *   if 11b is requested, find the 11b version of any
192568e8e04eSSam Leffler 		 *      11g channel returned,
192668e8e04eSSam Leffler 		 *   if static turbo, find the turbo version of any
192768e8e04eSSam Leffler 		 *	11a channel return,
192868e8e04eSSam Leffler 		 *   if 11na is requested, find the ht version of any
192968e8e04eSSam Leffler 		 *      11a channel returned,
193068e8e04eSSam Leffler 		 *   if 11ng is requested, find the ht version of any
193168e8e04eSSam Leffler 		 *      11g channel returned,
193268e8e04eSSam Leffler 		 *   otherwise we should be ok with what we've got.
193368e8e04eSSam Leffler 		 */
1934b032f27cSSam Leffler 		switch (vap->iv_des_mode) {
193568e8e04eSSam Leffler 		case IEEE80211_MODE_11B:
193668e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
193768e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
193868e8e04eSSam Leffler 					IEEE80211_MODE_11B);
193968e8e04eSSam Leffler 				/* NB: should not happen, =>'s 11g w/o 11b */
194068e8e04eSSam Leffler 				if (c2 != NULL)
194168e8e04eSSam Leffler 					c = c2;
194268e8e04eSSam Leffler 			}
194368e8e04eSSam Leffler 			break;
194468e8e04eSSam Leffler 		case IEEE80211_MODE_TURBO_A:
194568e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
194668e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
194768e8e04eSSam Leffler 					IEEE80211_MODE_TURBO_A);
194868e8e04eSSam Leffler 				if (c2 != NULL)
194968e8e04eSSam Leffler 					c = c2;
195068e8e04eSSam Leffler 			}
195168e8e04eSSam Leffler 			break;
195268e8e04eSSam Leffler 		case IEEE80211_MODE_11NA:
195368e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
195468e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
195568e8e04eSSam Leffler 					IEEE80211_MODE_11NA);
195668e8e04eSSam Leffler 				if (c2 != NULL)
195768e8e04eSSam Leffler 					c = c2;
195868e8e04eSSam Leffler 			}
195968e8e04eSSam Leffler 			break;
196068e8e04eSSam Leffler 		case IEEE80211_MODE_11NG:
196168e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
196268e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
196368e8e04eSSam Leffler 					IEEE80211_MODE_11NG);
196468e8e04eSSam Leffler 				if (c2 != NULL)
196568e8e04eSSam Leffler 					c = c2;
196668e8e04eSSam Leffler 			}
196768e8e04eSSam Leffler 			break;
196868e8e04eSSam Leffler 		default:		/* NB: no static turboG */
196968e8e04eSSam Leffler 			break;
197068e8e04eSSam Leffler 		}
197168e8e04eSSam Leffler 	}
1972b032f27cSSam Leffler 	return setcurchan(vap, c);
197368e8e04eSSam Leffler }
197468e8e04eSSam Leffler 
197568e8e04eSSam Leffler /*
197668e8e04eSSam Leffler  * New/current api for setting the current channel; a complete
197768e8e04eSSam Leffler  * channel description is provide so there is no ambiguity in
197868e8e04eSSam Leffler  * identifying the channel.
197968e8e04eSSam Leffler  */
1980b032f27cSSam Leffler static __noinline int
1981b032f27cSSam Leffler ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
198268e8e04eSSam Leffler 	const struct ieee80211req *ireq)
198368e8e04eSSam Leffler {
1984b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
198568e8e04eSSam Leffler 	struct ieee80211_channel chan, *c;
198668e8e04eSSam Leffler 	int error;
198768e8e04eSSam Leffler 
198868e8e04eSSam Leffler 	if (ireq->i_len != sizeof(chan))
198968e8e04eSSam Leffler 		return EINVAL;
199068e8e04eSSam Leffler 	error = copyin(ireq->i_data, &chan, sizeof(chan));
199168e8e04eSSam Leffler 	if (error != 0)
199268e8e04eSSam Leffler 		return error;
199368e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
199468e8e04eSSam Leffler 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
199568e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
199668e8e04eSSam Leffler 	} else {
199768e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
199868e8e04eSSam Leffler 		if (c == NULL)
199968e8e04eSSam Leffler 			return EINVAL;
200068e8e04eSSam Leffler 	}
2001b032f27cSSam Leffler 	return setcurchan(vap, c);
2002b032f27cSSam Leffler }
2003b032f27cSSam Leffler 
2004b032f27cSSam Leffler static __noinline int
2005b032f27cSSam Leffler ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2006b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2007b032f27cSSam Leffler {
2008b032f27cSSam Leffler 	struct ieee80211_regdomain_req *reg;
20098658b18bSSam Leffler 	int nchans, error;
2010b032f27cSSam Leffler 
20118658b18bSSam Leffler 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
20128658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
20138658b18bSSam Leffler 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
20148658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
20158658b18bSSam Leffler 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
20168658b18bSSam Leffler 		    ireq->i_len, nchans);
2017b032f27cSSam Leffler 		return EINVAL;
20188658b18bSSam Leffler 	}
20198658b18bSSam Leffler 	reg = (struct ieee80211_regdomain_req *)
20208658b18bSSam Leffler 	    malloc(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, M_NOWAIT);
20218658b18bSSam Leffler 	if (reg == NULL) {
20228658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
20238658b18bSSam Leffler 		    "%s: no memory, nchans %d\n", __func__, nchans);
2024b032f27cSSam Leffler 		return ENOMEM;
20258658b18bSSam Leffler 	}
20268658b18bSSam Leffler 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
20278658b18bSSam Leffler 	if (error == 0) {
20288658b18bSSam Leffler 		/* NB: validate inline channel count against storage size */
20298658b18bSSam Leffler 		if (reg->chaninfo.ic_nchans != nchans) {
20308658b18bSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
20318658b18bSSam Leffler 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
20328658b18bSSam Leffler 				reg->chaninfo.ic_nchans, nchans);
20338658b18bSSam Leffler 			error = EINVAL;
20348658b18bSSam Leffler 		} else
2035b032f27cSSam Leffler 			error = ieee80211_setregdomain(vap, reg);
20368658b18bSSam Leffler 	}
2037e2126decSSam Leffler 	free(reg, M_TEMP);
2038b032f27cSSam Leffler 
2039b032f27cSSam Leffler 	return (error == 0 ? ENETRESET : error);
204068e8e04eSSam Leffler }
204168e8e04eSSam Leffler 
204268e8e04eSSam Leffler static int
2043b032f27cSSam Leffler ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2044b032f27cSSam Leffler 	const struct ieee80211req *ireq)
20458a1b9b6aSSam Leffler {
2046b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2047b032f27cSSam Leffler 		return EINVAL;
2048b032f27cSSam Leffler 	/* XXX validate params */
2049b032f27cSSam Leffler 	/* XXX? ENETRESET to push to device? */
2050b032f27cSSam Leffler 	return copyin(ireq->i_data, vap->iv_roamparms,
2051b032f27cSSam Leffler 	    sizeof(vap->iv_roamparms));
2052b032f27cSSam Leffler }
2053b032f27cSSam Leffler 
2054b032f27cSSam Leffler static int
2055b032f27cSSam Leffler checkrate(const struct ieee80211_rateset *rs, int rate)
2056b032f27cSSam Leffler {
2057b032f27cSSam Leffler 	int i;
2058b032f27cSSam Leffler 
2059b032f27cSSam Leffler 	if (rate == IEEE80211_FIXED_RATE_NONE)
2060b032f27cSSam Leffler 		return 1;
2061b032f27cSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
2062b032f27cSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2063b032f27cSSam Leffler 			return 1;
2064b032f27cSSam Leffler 	return 0;
2065b032f27cSSam Leffler }
2066b032f27cSSam Leffler 
2067b032f27cSSam Leffler static int
2068b032f27cSSam Leffler checkmcs(int mcs)
2069b032f27cSSam Leffler {
2070b032f27cSSam Leffler 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2071b032f27cSSam Leffler 		return 1;
2072b032f27cSSam Leffler 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2073b032f27cSSam Leffler 		return 0;
2074b032f27cSSam Leffler 	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2075b032f27cSSam Leffler }
2076b032f27cSSam Leffler 
2077b032f27cSSam Leffler static __noinline int
2078b032f27cSSam Leffler ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2079b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2080b032f27cSSam Leffler {
2081b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2082b032f27cSSam Leffler 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2083b032f27cSSam Leffler 	struct ieee80211_txparam *src, *dst;
2084b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2085b032f27cSSam Leffler 	int error, i, changed;
2086b032f27cSSam Leffler 
2087b032f27cSSam Leffler 	if (ireq->i_len != sizeof(parms))
2088b032f27cSSam Leffler 		return EINVAL;
2089b032f27cSSam Leffler 	error = copyin(ireq->i_data, &parms, sizeof(parms));
2090b032f27cSSam Leffler 	if (error != 0)
2091b032f27cSSam Leffler 		return error;
2092b032f27cSSam Leffler 	changed = 0;
2093b032f27cSSam Leffler 	/* validate parameters and check if anything changed */
2094b032f27cSSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_11NA; i++) {
2095b032f27cSSam Leffler 		if (isclr(ic->ic_modecaps, i))
2096b032f27cSSam Leffler 			continue;
2097b032f27cSSam Leffler 		src = &parms.params[i];
2098b032f27cSSam Leffler 		dst = &vap->iv_txparms[i];
2099b032f27cSSam Leffler 		rs = &ic->ic_sup_rates[i];
2100b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2101b032f27cSSam Leffler 			if (!checkrate(rs, src->ucastrate))
2102b032f27cSSam Leffler 				return EINVAL;
2103b032f27cSSam Leffler 			changed++;
2104b032f27cSSam Leffler 		}
2105b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2106b032f27cSSam Leffler 			if (!checkrate(rs, src->mcastrate))
2107b032f27cSSam Leffler 				return EINVAL;
2108b032f27cSSam Leffler 			changed++;
2109b032f27cSSam Leffler 		}
2110b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2111b032f27cSSam Leffler 			if (!checkrate(rs, src->mgmtrate))
2112b032f27cSSam Leffler 				return EINVAL;
2113b032f27cSSam Leffler 			changed++;
2114b032f27cSSam Leffler 		}
2115b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2116b032f27cSSam Leffler 			changed++;
2117b032f27cSSam Leffler 	}
2118b032f27cSSam Leffler 	/* 11n parameters are handled differently */
2119b032f27cSSam Leffler 	for (; i < IEEE80211_MODE_MAX; i++) {
2120b032f27cSSam Leffler 		if (isclr(ic->ic_modecaps, i))
2121b032f27cSSam Leffler 			continue;
2122b032f27cSSam Leffler 		src = &parms.params[i];
2123b032f27cSSam Leffler 		dst = &vap->iv_txparms[i];
212440432d36SSam Leffler 		rs = &ic->ic_sup_rates[i];
2125b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2126b032f27cSSam Leffler 			if (!checkmcs(src->ucastrate) &&
2127b032f27cSSam Leffler 			    !checkrate(rs, src->ucastrate))
2128b032f27cSSam Leffler 				return EINVAL;
2129b032f27cSSam Leffler 			changed++;
2130b032f27cSSam Leffler 		}
2131b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2132b032f27cSSam Leffler 			if (!checkmcs(src->mcastrate) &&
2133b032f27cSSam Leffler 			    !checkrate(rs, src->mcastrate))
2134b032f27cSSam Leffler 				return EINVAL;
2135b032f27cSSam Leffler 			changed++;
2136b032f27cSSam Leffler 		}
2137b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2138b032f27cSSam Leffler 			if (!checkmcs(src->mgmtrate) &&
2139b032f27cSSam Leffler 			    !checkrate(rs, src->mgmtrate))
2140b032f27cSSam Leffler 				return EINVAL;
2141b032f27cSSam Leffler 			changed++;
2142b032f27cSSam Leffler 		}
2143b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2144b032f27cSSam Leffler 			changed++;
2145b032f27cSSam Leffler 	}
2146b032f27cSSam Leffler 	if (changed) {
2147b032f27cSSam Leffler 		/*
2148b032f27cSSam Leffler 		 * Copy new parameters in place and notify the
2149b032f27cSSam Leffler 		 * driver so it can push state to the device.
2150b032f27cSSam Leffler 		 */
2151b032f27cSSam Leffler 		for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
2152b032f27cSSam Leffler 			if (isset(ic->ic_modecaps, i))
2153b032f27cSSam Leffler 				vap->iv_txparms[i] = parms.params[i];
2154b032f27cSSam Leffler 		}
2155b032f27cSSam Leffler 		/* XXX could be more intelligent,
2156b032f27cSSam Leffler 		   e.g. don't reset if setting not being used */
2157b032f27cSSam Leffler 		return ENETRESET;
2158b032f27cSSam Leffler 	}
2159b032f27cSSam Leffler 	return 0;
2160b032f27cSSam Leffler }
2161b032f27cSSam Leffler 
2162b032f27cSSam Leffler /*
2163b032f27cSSam Leffler  * Application Information Element support.
2164b032f27cSSam Leffler  */
2165b032f27cSSam Leffler static int
2166b032f27cSSam Leffler setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2167b032f27cSSam Leffler {
2168b032f27cSSam Leffler 	struct ieee80211_appie *app = *aie;
2169b032f27cSSam Leffler 	struct ieee80211_appie *napp;
2170b032f27cSSam Leffler 	int error;
2171b032f27cSSam Leffler 
2172b032f27cSSam Leffler 	if (ireq->i_len == 0) {		/* delete any existing ie */
2173b032f27cSSam Leffler 		if (app != NULL) {
2174b032f27cSSam Leffler 			*aie = NULL;	/* XXX racey */
2175e2126decSSam Leffler 			free(app, M_80211_NODE_IE);
2176b032f27cSSam Leffler 		}
2177b032f27cSSam Leffler 		return 0;
2178b032f27cSSam Leffler 	}
2179b032f27cSSam Leffler 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2180b032f27cSSam Leffler 		return EINVAL;
2181b032f27cSSam Leffler 	/*
2182b032f27cSSam Leffler 	 * Allocate a new appie structure and copy in the user data.
2183b032f27cSSam Leffler 	 * When done swap in the new structure.  Note that we do not
2184b032f27cSSam Leffler 	 * guard against users holding a ref to the old structure;
2185b032f27cSSam Leffler 	 * this must be handled outside this code.
2186b032f27cSSam Leffler 	 *
2187b032f27cSSam Leffler 	 * XXX bad bad bad
2188b032f27cSSam Leffler 	 */
2189e2126decSSam Leffler 	napp = (struct ieee80211_appie *) malloc(
2190c5abbba3SDag-Erling Smørgrav 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2191b032f27cSSam Leffler 	if (napp == NULL)
2192b032f27cSSam Leffler 		return ENOMEM;
2193b032f27cSSam Leffler 	/* XXX holding ic lock */
2194b032f27cSSam Leffler 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2195b032f27cSSam Leffler 	if (error) {
2196e2126decSSam Leffler 		free(napp, M_80211_NODE_IE);
2197b032f27cSSam Leffler 		return error;
2198b032f27cSSam Leffler 	}
2199b032f27cSSam Leffler 	napp->ie_len = ireq->i_len;
2200b032f27cSSam Leffler 	*aie = napp;
2201b032f27cSSam Leffler 	if (app != NULL)
2202e2126decSSam Leffler 		free(app, M_80211_NODE_IE);
2203b032f27cSSam Leffler 	return 0;
2204b032f27cSSam Leffler }
2205b032f27cSSam Leffler 
2206b032f27cSSam Leffler static void
2207b032f27cSSam Leffler setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2208b032f27cSSam Leffler {
2209b032f27cSSam Leffler 	/* validate data is present as best we can */
2210b032f27cSSam Leffler 	if (space == 0 || 2+ie[1] > space)
2211b032f27cSSam Leffler 		return;
2212b032f27cSSam Leffler 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2213b032f27cSSam Leffler 		vap->iv_wpa_ie = ie;
2214b032f27cSSam Leffler 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2215b032f27cSSam Leffler 		vap->iv_rsn_ie = ie;
2216b032f27cSSam Leffler }
2217b032f27cSSam Leffler 
2218b032f27cSSam Leffler static __noinline int
2219b032f27cSSam Leffler ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2220b032f27cSSam Leffler 	const struct ieee80211req *ireq, int fc0)
2221b032f27cSSam Leffler {
2222b032f27cSSam Leffler 	int error;
2223b032f27cSSam Leffler 
2224b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2225b032f27cSSam Leffler 
2226b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2227b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2228b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2229b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2230b032f27cSSam Leffler 			error = EINVAL;
2231b032f27cSSam Leffler 			break;
2232b032f27cSSam Leffler 		}
2233b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_beacon, ireq);
2234b032f27cSSam Leffler 		if (error == 0)
2235b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2236b032f27cSSam Leffler 		break;
2237b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2238b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_proberesp, ireq);
2239b032f27cSSam Leffler 		break;
2240b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2241b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2242b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocresp, ireq);
2243b032f27cSSam Leffler 		else
2244b032f27cSSam Leffler 			error = EINVAL;
2245b032f27cSSam Leffler 		break;
2246b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2247b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_probereq, ireq);
2248b032f27cSSam Leffler 		break;
2249b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2250b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
2251b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocreq, ireq);
2252b032f27cSSam Leffler 		else
2253b032f27cSSam Leffler 			error = EINVAL;
2254b032f27cSSam Leffler 		break;
2255b032f27cSSam Leffler 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2256b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_wpa, ireq);
2257b032f27cSSam Leffler 		if (error == 0) {
2258b032f27cSSam Leffler 			/*
2259b032f27cSSam Leffler 			 * Must split single blob of data into separate
2260b032f27cSSam Leffler 			 * WPA and RSN ie's because they go in different
2261b032f27cSSam Leffler 			 * locations in the mgt frames.
2262b032f27cSSam Leffler 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2263b032f27cSSam Leffler 			 */
2264b032f27cSSam Leffler 			vap->iv_wpa_ie = NULL;
2265b032f27cSSam Leffler 			vap->iv_rsn_ie = NULL;
2266b032f27cSSam Leffler 			if (vap->iv_appie_wpa != NULL) {
2267b032f27cSSam Leffler 				struct ieee80211_appie *appie =
2268b032f27cSSam Leffler 				    vap->iv_appie_wpa;
2269b032f27cSSam Leffler 				uint8_t *data = appie->ie_data;
2270b032f27cSSam Leffler 
2271b032f27cSSam Leffler 				/* XXX ie length validate is painful, cheat */
2272b032f27cSSam Leffler 				setwparsnie(vap, data, appie->ie_len);
2273b032f27cSSam Leffler 				setwparsnie(vap, data + 2 + data[1],
2274b032f27cSSam Leffler 				    appie->ie_len - (2 + data[1]));
2275b032f27cSSam Leffler 			}
2276b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2277b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2278b032f27cSSam Leffler 				/*
2279b032f27cSSam Leffler 				 * Must rebuild beacon frame as the update
2280b032f27cSSam Leffler 				 * mechanism doesn't handle WPA/RSN ie's.
2281b032f27cSSam Leffler 				 * Could extend it but it doesn't normally
2282b032f27cSSam Leffler 				 * change; this is just to deal with hostapd
2283b032f27cSSam Leffler 				 * plumbing the ie after the interface is up.
2284b032f27cSSam Leffler 				 */
2285b032f27cSSam Leffler 				error = ENETRESET;
2286b032f27cSSam Leffler 			}
2287b032f27cSSam Leffler 		}
2288b032f27cSSam Leffler 		break;
2289b032f27cSSam Leffler 	default:
2290b032f27cSSam Leffler 		error = EINVAL;
2291b032f27cSSam Leffler 		break;
2292b032f27cSSam Leffler 	}
2293b032f27cSSam Leffler 	return error;
2294b032f27cSSam Leffler }
2295b032f27cSSam Leffler 
2296b032f27cSSam Leffler static __noinline int
2297b032f27cSSam Leffler ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2298b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2299b032f27cSSam Leffler {
2300b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2301b032f27cSSam Leffler 	int error;
2302b032f27cSSam Leffler 	uint8_t fc0;
2303b032f27cSSam Leffler 
2304b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
2305b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2306b032f27cSSam Leffler 		return EINVAL;
2307b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2308b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2309b032f27cSSam Leffler 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2310b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2311b032f27cSSam Leffler 	return error;
2312b032f27cSSam Leffler }
2313b032f27cSSam Leffler 
2314b032f27cSSam Leffler static __noinline int
2315b032f27cSSam Leffler ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2316b032f27cSSam Leffler {
2317b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2318b032f27cSSam Leffler 	struct ieee80211_chanswitch_req csr;
2319b032f27cSSam Leffler 	struct ieee80211_channel *c;
2320b032f27cSSam Leffler 	int error;
2321b032f27cSSam Leffler 
2322b032f27cSSam Leffler 	if (ireq->i_len != sizeof(csr))
2323b032f27cSSam Leffler 		return EINVAL;
2324b032f27cSSam Leffler 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2325b032f27cSSam Leffler 	if (error != 0)
2326b032f27cSSam Leffler 		return error;
2327b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
2328b032f27cSSam Leffler 		return EINVAL;
2329b032f27cSSam Leffler 	c = ieee80211_find_channel(ic,
2330b032f27cSSam Leffler 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2331b032f27cSSam Leffler 	if (c == NULL)
2332b032f27cSSam Leffler 		return ENOENT;
2333b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2334b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2335b032f27cSSam Leffler 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2336b032f27cSSam Leffler 	else
2337b032f27cSSam Leffler 		error = EBUSY;
2338b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2339b032f27cSSam Leffler 	return error;
2340b032f27cSSam Leffler }
2341b032f27cSSam Leffler 
2342b032f27cSSam Leffler static __noinline int
2343b032f27cSSam Leffler ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2344b032f27cSSam Leffler {
2345b032f27cSSam Leffler #define	IEEE80211_IOC_SCAN_FLAGS \
2346b032f27cSSam Leffler 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2347b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2348b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2349b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2350b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_CHECK)
2351b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2352b032f27cSSam Leffler 	struct ieee80211_scan_req sr;		/* XXX off stack? */
2353b032f27cSSam Leffler 	int error, i;
2354b032f27cSSam Leffler 
2355b032f27cSSam Leffler 	/* NB: parent must be running */
2356b032f27cSSam Leffler 	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2357b032f27cSSam Leffler 		return ENXIO;
2358b032f27cSSam Leffler 
2359b032f27cSSam Leffler 	if (ireq->i_len != sizeof(sr))
2360b032f27cSSam Leffler 		return EINVAL;
2361b032f27cSSam Leffler 	error = copyin(ireq->i_data, &sr, sizeof(sr));
2362b032f27cSSam Leffler 	if (error != 0)
2363b032f27cSSam Leffler 		return error;
2364b032f27cSSam Leffler 	/* convert duration */
2365b032f27cSSam Leffler 	if (sr.sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2366b032f27cSSam Leffler 		sr.sr_duration = IEEE80211_SCAN_FOREVER;
2367b032f27cSSam Leffler 	else {
2368b032f27cSSam Leffler 		if (sr.sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2369b032f27cSSam Leffler 		    sr.sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2370b032f27cSSam Leffler 			return EINVAL;
2371b032f27cSSam Leffler 		sr.sr_duration = msecs_to_ticks(sr.sr_duration);
2372b032f27cSSam Leffler 		if (sr.sr_duration < 1)
2373b032f27cSSam Leffler 			sr.sr_duration = 1;
2374b032f27cSSam Leffler 	}
2375b032f27cSSam Leffler 	/* convert min/max channel dwell */
2376b032f27cSSam Leffler 	if (sr.sr_mindwell != 0) {
2377b032f27cSSam Leffler 		sr.sr_mindwell = msecs_to_ticks(sr.sr_mindwell);
2378b032f27cSSam Leffler 		if (sr.sr_mindwell < 1)
2379b032f27cSSam Leffler 			sr.sr_mindwell = 1;
2380b032f27cSSam Leffler 	}
2381b032f27cSSam Leffler 	if (sr.sr_maxdwell != 0) {
2382b032f27cSSam Leffler 		sr.sr_maxdwell = msecs_to_ticks(sr.sr_maxdwell);
2383b032f27cSSam Leffler 		if (sr.sr_maxdwell < 1)
2384b032f27cSSam Leffler 			sr.sr_maxdwell = 1;
2385b032f27cSSam Leffler 	}
2386b032f27cSSam Leffler 	/* NB: silently reduce ssid count to what is supported */
2387b032f27cSSam Leffler 	if (sr.sr_nssid > IEEE80211_SCAN_MAX_SSID)
2388b032f27cSSam Leffler 		sr.sr_nssid = IEEE80211_SCAN_MAX_SSID;
2389b032f27cSSam Leffler 	for (i = 0; i < sr.sr_nssid; i++)
2390b032f27cSSam Leffler 		if (sr.sr_ssid[i].len > IEEE80211_NWID_LEN)
2391b032f27cSSam Leffler 			return EINVAL;
2392b032f27cSSam Leffler 	/* cleanse flags just in case, could reject if invalid flags */
2393b032f27cSSam Leffler 	sr.sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2394b032f27cSSam Leffler 	/*
2395b032f27cSSam Leffler 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2396b032f27cSSam Leffler 	 * allows applications to scan without joining a bss (or picking
2397b032f27cSSam Leffler 	 * a channel and setting up a bss) and without forcing manual
2398b032f27cSSam Leffler 	 * roaming mode--you just need to mark the parent device UP.
2399b032f27cSSam Leffler 	 */
2400b032f27cSSam Leffler 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2401b032f27cSSam Leffler 		sr.sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2402b032f27cSSam Leffler 
2403b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2404b032f27cSSam Leffler 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2405b032f27cSSam Leffler 	    __func__, sr.sr_flags,
2406b032f27cSSam Leffler 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2407b032f27cSSam Leffler 	    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell, sr.sr_nssid);
2408b032f27cSSam Leffler 	/*
2409b032f27cSSam Leffler 	 * If we are in INIT state then the driver has never had a chance
2410b032f27cSSam Leffler 	 * to setup hardware state to do a scan; we must use the state
2411b032f27cSSam Leffler 	 * machine to get us up to the SCAN state but once we reach SCAN
2412b032f27cSSam Leffler 	 * state we then want to use the supplied params.  Stash the
2413b032f27cSSam Leffler 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2414b032f27cSSam Leffler 	 * state machines will recognize this and use the stashed params
2415b032f27cSSam Leffler 	 * to issue the scan request.
2416b032f27cSSam Leffler 	 *
2417b032f27cSSam Leffler 	 * Otherwise just invoke the scan machinery directly.
2418b032f27cSSam Leffler 	 */
2419b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2420b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_INIT) {
2421b032f27cSSam Leffler 		/* NB: clobbers previous settings */
2422b032f27cSSam Leffler 		vap->iv_scanreq_flags = sr.sr_flags;
2423b032f27cSSam Leffler 		vap->iv_scanreq_duration = sr.sr_duration;
2424b032f27cSSam Leffler 		vap->iv_scanreq_nssid = sr.sr_nssid;
2425b032f27cSSam Leffler 		for (i = 0; i < sr.sr_nssid; i++) {
2426b032f27cSSam Leffler 			vap->iv_scanreq_ssid[i].len = sr.sr_ssid[i].len;
2427b032f27cSSam Leffler 			memcpy(vap->iv_scanreq_ssid[i].ssid, sr.sr_ssid[i].ssid,
2428b032f27cSSam Leffler 			    sr.sr_ssid[i].len);
2429b032f27cSSam Leffler 		}
2430b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2431b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2432b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2433b032f27cSSam Leffler 	} else {
2434b032f27cSSam Leffler 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2435b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2436b032f27cSSam Leffler 		/* XXX neeed error return codes */
2437b032f27cSSam Leffler 		if (sr.sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2438b032f27cSSam Leffler 			(void) ieee80211_check_scan(vap, sr.sr_flags,
2439b032f27cSSam Leffler 			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2440b032f27cSSam Leffler 			    sr.sr_nssid,
2441b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2442b032f27cSSam Leffler 			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2443b032f27cSSam Leffler 		} else {
2444b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap, sr.sr_flags,
2445b032f27cSSam Leffler 			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2446b032f27cSSam Leffler 			    sr.sr_nssid,
2447b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2448b032f27cSSam Leffler 			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2449b032f27cSSam Leffler 		}
2450b032f27cSSam Leffler 	}
2451b032f27cSSam Leffler 	return error;
2452b032f27cSSam Leffler #undef IEEE80211_IOC_SCAN_FLAGS
2453b032f27cSSam Leffler }
2454b032f27cSSam Leffler 
2455b032f27cSSam Leffler static __noinline int
2456b032f27cSSam Leffler ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2457b032f27cSSam Leffler {
2458b032f27cSSam Leffler 	struct ieee80211_node *ni;
2459b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
2460b032f27cSSam Leffler 	int error;
2461b032f27cSSam Leffler 
2462b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
2463b032f27cSSam Leffler 		return EINVAL;
2464b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2465b032f27cSSam Leffler 	if (error != 0)
2466b032f27cSSam Leffler 		return error;
2467b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2468b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2469b032f27cSSam Leffler 		    vlan.sv_macaddr);
2470b032f27cSSam Leffler 		if (ni == NULL)
2471b032f27cSSam Leffler 			return ENOENT;
2472b032f27cSSam Leffler 	} else
2473b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
2474b032f27cSSam Leffler 	ni->ni_vlan = vlan.sv_vlan;
2475b032f27cSSam Leffler 	ieee80211_free_node(ni);
2476b032f27cSSam Leffler 	return error;
2477b032f27cSSam Leffler }
2478b032f27cSSam Leffler 
2479b032f27cSSam Leffler static int
2480b032f27cSSam Leffler isvap11g(const struct ieee80211vap *vap)
2481b032f27cSSam Leffler {
2482b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2483b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2484b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2485b032f27cSSam Leffler }
2486b032f27cSSam Leffler 
2487b032f27cSSam Leffler static int
2488b032f27cSSam Leffler isvapht(const struct ieee80211vap *vap)
2489b032f27cSSam Leffler {
2490b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2491b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2492b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2493b032f27cSSam Leffler }
2494b032f27cSSam Leffler 
2495b032f27cSSam Leffler static __noinline int
2496b032f27cSSam Leffler ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2497b032f27cSSam Leffler {
2498b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
24998a1b9b6aSSam Leffler 	int error;
25008a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
250168e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
25028a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
250368e8e04eSSam Leffler 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
25048a1b9b6aSSam Leffler 	struct ieee80211_key *k;
25058a1b9b6aSSam Leffler 	u_int kid;
25063dfff737SWarner Losh 	uint32_t flags;
25078a1b9b6aSSam Leffler 
25088a1b9b6aSSam Leffler 	error = 0;
25091a1e1d21SSam Leffler 	switch (ireq->i_type) {
25101a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
25111a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
25128a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
25138a1b9b6aSSam Leffler 			return EINVAL;
25141a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
25151a1e1d21SSam Leffler 		if (error)
25161a1e1d21SSam Leffler 			break;
2517b032f27cSSam Leffler 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2518b032f27cSSam Leffler 		vap->iv_des_ssid[0].len = ireq->i_len;
2519b032f27cSSam Leffler 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2520b032f27cSSam Leffler 		vap->iv_des_nssid = (ireq->i_len > 0);
2521b032f27cSSam Leffler 		error = ENETRESET;
25221a1e1d21SSam Leffler 		break;
25231a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
25248a1b9b6aSSam Leffler 		switch (ireq->i_val) {
25258a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
2526b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2527b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
25288a1b9b6aSSam Leffler 			break;
25298a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
2530b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2531b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
25328a1b9b6aSSam Leffler 			break;
25338a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
2534b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2535b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
25368a1b9b6aSSam Leffler 			break;
25371a1e1d21SSam Leffler 		}
2538b032f27cSSam Leffler 		error = ENETRESET;
25391a1e1d21SSam Leffler 		break;
25401a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
25411a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
25428a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
25438a1b9b6aSSam Leffler 			return EINVAL;
2544b032f27cSSam Leffler 		k = &vap->iv_nw_keys[kid];
25458a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
25468a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
2547b032f27cSSam Leffler 			(void) ieee80211_crypto_delkey(vap, k);
25481a1e1d21SSam Leffler 			break;
25491a1e1d21SSam Leffler 		}
25508a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
25518a1b9b6aSSam Leffler 			return EINVAL;
25521a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
25531a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
25541a1e1d21SSam Leffler 		if (error)
25551a1e1d21SSam Leffler 			break;
2556b032f27cSSam Leffler 		ieee80211_key_update_begin(vap);
2557dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2558b032f27cSSam Leffler 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2559dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
25608a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
25618a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
256271fe06caSSam Leffler 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
256371fe06caSSam Leffler 			if  (!ieee80211_crypto_setkey(vap, k))
25648a1b9b6aSSam Leffler 				error = EINVAL;
25658a1b9b6aSSam Leffler 		} else
25668a1b9b6aSSam Leffler 			error = EINVAL;
2567b032f27cSSam Leffler 		ieee80211_key_update_end(vap);
25681a1e1d21SSam Leffler 		break;
25691a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
25701a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2571380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
257268e8e04eSSam Leffler 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
25738a1b9b6aSSam Leffler 			return EINVAL;
2574b032f27cSSam Leffler 		vap->iv_def_txkey = kid;
25758a1b9b6aSSam Leffler 		break;
25768a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
25778a1b9b6aSSam Leffler 		switch (ireq->i_val) {
25788a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
25798a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
25808a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
25818a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
25828a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
25838a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
25848a1b9b6aSSam Leffler 			if (auth == NULL)
25858a1b9b6aSSam Leffler 				return EINVAL;
25868a1b9b6aSSam Leffler 			break;
25878a1b9b6aSSam Leffler 		default:
25888a1b9b6aSSam Leffler 			return EINVAL;
25898a1b9b6aSSam Leffler 		}
25908a1b9b6aSSam Leffler 		switch (ireq->i_val) {
25918a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2592b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
25938a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
25948a1b9b6aSSam Leffler 			break;
25958a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
2596b032f27cSSam Leffler 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
25978a1b9b6aSSam Leffler 			break;
25988a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
25998a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2600b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
26018a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
2602b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
26038a1b9b6aSSam Leffler 			break;
26048a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
2605b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
26068a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
26078a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
26081a1e1d21SSam Leffler 			break;
26091a1e1d21SSam Leffler 		}
26108a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
2611b032f27cSSam Leffler 		vap->iv_bss->ni_authmode = ireq->i_val;
26128a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
2613b032f27cSSam Leffler 		vap->iv_auth = auth;
2614b032f27cSSam Leffler 		error = ENETRESET;
26151a1e1d21SSam Leffler 		break;
26161a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
2617b032f27cSSam Leffler 		error = ieee80211_ioctl_setchannel(vap, ireq);
26181a1e1d21SSam Leffler 		break;
26191a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
26201a1e1d21SSam Leffler 		switch (ireq->i_val) {
26211a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
2622b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2623b032f27cSSam Leffler 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2624b032f27cSSam Leffler 				error = ERESTART;
26251a1e1d21SSam Leffler 			}
26261a1e1d21SSam Leffler 			break;
26271a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
2628b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2629b032f27cSSam Leffler 				error = EOPNOTSUPP;
2630b032f27cSSam Leffler 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2631b032f27cSSam Leffler 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2632b032f27cSSam Leffler 				error = ERESTART;
26331a1e1d21SSam Leffler 			}
26341a1e1d21SSam Leffler 			break;
26351a1e1d21SSam Leffler 		default:
26361a1e1d21SSam Leffler 			error = EINVAL;
26371a1e1d21SSam Leffler 			break;
26381a1e1d21SSam Leffler 		}
26391a1e1d21SSam Leffler 		break;
26401a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
26418a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
26428a1b9b6aSSam Leffler 			return EINVAL;
26431a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
2644b032f27cSSam Leffler 		error = ERESTART;
26451a1e1d21SSam Leffler 		break;
264613604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
264770231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
264870231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
26498a1b9b6aSSam Leffler 			return EINVAL;
2650b032f27cSSam Leffler 		vap->iv_rtsthreshold = ireq->i_val;
2651b032f27cSSam Leffler 		error = ERESTART;
26521a1e1d21SSam Leffler 		break;
26532e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
26548a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
26558a1b9b6aSSam Leffler 			return EINVAL;
26562e79ca97SSam Leffler 		ic->ic_protmode = ireq->i_val;
26572e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
265868e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
265968e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2660b032f27cSSam Leffler 			error = ERESTART;
26612e79ca97SSam Leffler 		break;
26622e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
26638a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2664b032f27cSSam Leffler 			return EOPNOTSUPP;
266568e8e04eSSam Leffler 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
266668e8e04eSSam Leffler 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
26678a1b9b6aSSam Leffler 			return EINVAL;
26688a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
2669b032f27cSSam Leffler 		error = ERESTART;
26708a1b9b6aSSam Leffler 		break;
26718a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
26728a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
26738a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
26748a1b9b6aSSam Leffler 			return EINVAL;
2675b032f27cSSam Leffler 		vap->iv_roaming = ireq->i_val;
26768a1b9b6aSSam Leffler 		/* XXXX reset? */
26778a1b9b6aSSam Leffler 		break;
26788a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
26798a1b9b6aSSam Leffler 		if (ireq->i_val) {
26808a1b9b6aSSam Leffler 			/* XXX check for key state? */
2681b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
26828a1b9b6aSSam Leffler 		} else
2683b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2684b032f27cSSam Leffler 		/* XXX ERESTART? */
26858a1b9b6aSSam Leffler 		break;
26868a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
26878a1b9b6aSSam Leffler 		if (ireq->i_val)
2688b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
26898a1b9b6aSSam Leffler 		else
2690b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2691b032f27cSSam Leffler 		/* XXX ERESTART? */
26928a1b9b6aSSam Leffler 		break;
26938a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
2694b032f27cSSam Leffler 		error = ieee80211_ioctl_setkey(vap, ireq);
26958a1b9b6aSSam Leffler 		break;
26968a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
2697b032f27cSSam Leffler 		error = ieee80211_ioctl_delkey(vap, ireq);
26988a1b9b6aSSam Leffler 		break;
26998a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
2700b032f27cSSam Leffler 		error = ieee80211_ioctl_setmlme(vap, ireq);
27018a1b9b6aSSam Leffler 		break;
27028a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
27038a1b9b6aSSam Leffler 		if (ireq->i_val) {
2704b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2705b032f27cSSam Leffler 				return EOPNOTSUPP;
2706b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_COUNTERM;
27078a1b9b6aSSam Leffler 		} else
2708b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2709b032f27cSSam Leffler 		/* XXX ERESTART? */
27108a1b9b6aSSam Leffler 		break;
27118a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
27128a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
27138a1b9b6aSSam Leffler 			return EINVAL;
27148a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
27153dfff737SWarner Losh 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
27168a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27178a1b9b6aSSam Leffler 		case 1:
27183dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
27193dfff737SWarner Losh 				return EOPNOTSUPP;
27203dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1;
27218a1b9b6aSSam Leffler 			break;
27228a1b9b6aSSam Leffler 		case 2:
27233dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
27243dfff737SWarner Losh 				return EOPNOTSUPP;
27253dfff737SWarner Losh 			flags |= IEEE80211_F_WPA2;
27268a1b9b6aSSam Leffler 			break;
27278a1b9b6aSSam Leffler 		case 3:
27283dfff737SWarner Losh 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
27298d092df9SSam Leffler 				return EOPNOTSUPP;
27303dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
27312e79ca97SSam Leffler 			break;
27323dfff737SWarner Losh 		default:	/*  Can't set any -> error */
27333dfff737SWarner Losh 			return EOPNOTSUPP;
27342e79ca97SSam Leffler 		}
27353dfff737SWarner Losh 		vap->iv_flags = flags;
2736b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
27378a1b9b6aSSam Leffler 		break;
27388a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
27398a1b9b6aSSam Leffler 		if (ireq->i_val) {
2740b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2741b032f27cSSam Leffler 				return EOPNOTSUPP;
2742b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_WME);
27438a1b9b6aSSam Leffler 		} else
2744b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2745b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
27468a1b9b6aSSam Leffler 		break;
27478a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
27488a1b9b6aSSam Leffler 		if (ireq->i_val)
2749b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_HIDESSID;
27508a1b9b6aSSam Leffler 		else
2751b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2752b032f27cSSam Leffler 		error = ERESTART;		/* XXX ENETRESET? */
27532e79ca97SSam Leffler 		break;
27548a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
27558a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
2756b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
27578a1b9b6aSSam Leffler 		else
2758b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
27598a1b9b6aSSam Leffler 		break;
27608a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
27618a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
27628a1b9b6aSSam Leffler 			return EINVAL;
27638a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
27648a1b9b6aSSam Leffler 		if (error)
27658a1b9b6aSSam Leffler 			break;
2766b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2767b032f27cSSam Leffler 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2768b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
27698a1b9b6aSSam Leffler 		else
2770b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2771b032f27cSSam Leffler 		error = ENETRESET;
27728a1b9b6aSSam Leffler 		break;
27738a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
2774b032f27cSSam Leffler 		error = ieee80211_ioctl_setchanlist(vap, ireq);
27758a1b9b6aSSam Leffler 		break;
2776b032f27cSSam Leffler #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2777b032f27cSSam Leffler #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2778b032f27cSSam Leffler 	case OLD_IEEE80211_IOC_SCAN_REQ:
2779b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2780b032f27cSSam Leffler 			"%s: active scan request\n", __func__);
2781b032f27cSSam Leffler 		/*
2782b032f27cSSam Leffler 		 * If we are in INIT state then the driver has never
2783b032f27cSSam Leffler 		 * had a chance to setup hardware state to do a scan;
2784b032f27cSSam Leffler 		 * use the state machine to get us up the SCAN state.
2785b032f27cSSam Leffler 		 * Otherwise just invoke the scan machinery to start
2786b032f27cSSam Leffler 		 * a one-time scan.
2787b032f27cSSam Leffler 		 */
2788b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
2789b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2790b032f27cSSam Leffler 		else
2791b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap,
279268e8e04eSSam Leffler 				IEEE80211_SCAN_ACTIVE |
279368e8e04eSSam Leffler 				IEEE80211_SCAN_NOPICK |
2794b032f27cSSam Leffler 				IEEE80211_SCAN_ONCE,
2795b032f27cSSam Leffler 				IEEE80211_SCAN_FOREVER, 0, 0,
279668e8e04eSSam Leffler 				/* XXX use ioctl params */
2797b032f27cSSam Leffler 				vap->iv_des_nssid, vap->iv_des_ssid);
2798b032f27cSSam Leffler 		break;
2799b032f27cSSam Leffler #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2800b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
2801b032f27cSSam Leffler 		error = ieee80211_ioctl_scanreq(vap, ireq);
2802b032f27cSSam Leffler 		break;
2803b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_CANCEL:
2804b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2805b032f27cSSam Leffler 		    "%s: cancel scan\n", __func__);
2806b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);
2807b032f27cSSam Leffler 		break;
2808b032f27cSSam Leffler 	case IEEE80211_IOC_HTCONF:
2809b032f27cSSam Leffler 		if (ireq->i_val & 1)
2810b032f27cSSam Leffler 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_HT);
2811b032f27cSSam Leffler 		else
2812b032f27cSSam Leffler 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_HT);
2813b032f27cSSam Leffler 		if (ireq->i_val & 2)
2814b032f27cSSam Leffler 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_USEHT40);
2815b032f27cSSam Leffler 		else
2816b032f27cSSam Leffler 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_USEHT40);
2817b032f27cSSam Leffler 		error = ENETRESET;
28188a1b9b6aSSam Leffler 		break;
28198a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
28208a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
2821b032f27cSSam Leffler 		error = ieee80211_ioctl_macmac(vap, ireq);
28228a1b9b6aSSam Leffler 		break;
28238a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
2824b032f27cSSam Leffler 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
28258a1b9b6aSSam Leffler 		break;
282642568791SSam Leffler 	case IEEE80211_IOC_STA_STATS:
2827b032f27cSSam Leffler 		error = ieee80211_ioctl_setstastats(vap, ireq);
282842568791SSam Leffler 		break;
28298a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
2830b032f27cSSam Leffler 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
28318a1b9b6aSSam Leffler 		break;
28328a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
28338a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
28348a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
28358a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
28368a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
28378a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2838b032f27cSSam Leffler 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
28398a1b9b6aSSam Leffler 		break;
28408a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
2841b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2842b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
28438a1b9b6aSSam Leffler 			return EINVAL;
28448a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
28458a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2846b032f27cSSam Leffler 			vap->iv_dtim_period = ireq->i_val;
28478a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
28488a1b9b6aSSam Leffler 		} else
28498a1b9b6aSSam Leffler 			error = EINVAL;
28508a1b9b6aSSam Leffler 		break;
28518a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
2852b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2853b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
28548a1b9b6aSSam Leffler 			return EINVAL;
28558a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
28568a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2857d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
28588a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
28598a1b9b6aSSam Leffler 		} else
28608a1b9b6aSSam Leffler 			error = EINVAL;
28618a1b9b6aSSam Leffler 		break;
2862c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
2863c4f040c3SSam Leffler 		if (ireq->i_val)
2864b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PUREG;
2865c4f040c3SSam Leffler 		else
2866b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PUREG;
2867c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
2868b032f27cSSam Leffler 		if (isvap11g(vap))
2869c4f040c3SSam Leffler 			error = ENETRESET;
2870c4f040c3SSam Leffler 		break;
287168e8e04eSSam Leffler 	case IEEE80211_IOC_FF:
287268e8e04eSSam Leffler 		if (ireq->i_val) {
2873b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
2874b032f27cSSam Leffler 				return EOPNOTSUPP;
2875b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_FF;
287668e8e04eSSam Leffler 		} else
2877b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_FF;
2878b032f27cSSam Leffler 		error = ERESTART;
287968e8e04eSSam Leffler 		break;
288068e8e04eSSam Leffler 	case IEEE80211_IOC_TURBOP:
288168e8e04eSSam Leffler 		if (ireq->i_val) {
2882b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
2883b032f27cSSam Leffler 				return EOPNOTSUPP;
2884b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_TURBOP;
288568e8e04eSSam Leffler 		} else
2886b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_TURBOP;
288768e8e04eSSam Leffler 		error = ENETRESET;
288868e8e04eSSam Leffler 		break;
288968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
289068e8e04eSSam Leffler 		if (ireq->i_val) {
2891b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
2892b032f27cSSam Leffler 				return EOPNOTSUPP;
2893b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_BGSCAN;
289468e8e04eSSam Leffler 		} else
2895b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
289668e8e04eSSam Leffler 		break;
289768e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
289868e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
2899b032f27cSSam Leffler 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
290068e8e04eSSam Leffler 		else
290168e8e04eSSam Leffler 			error = EINVAL;
290268e8e04eSSam Leffler 		break;
290368e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
290468e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
2905b032f27cSSam Leffler 			vap->iv_bgscanintvl = ireq->i_val*hz;
290668e8e04eSSam Leffler 		else
290768e8e04eSSam Leffler 			error = EINVAL;
290868e8e04eSSam Leffler 		break;
290968e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
291068e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
2911b032f27cSSam Leffler 			vap->iv_scanvalid = ireq->i_val*hz;
291268e8e04eSSam Leffler 		else
291368e8e04eSSam Leffler 			error = EINVAL;
291468e8e04eSSam Leffler 		break;
291570231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
2916b032f27cSSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
291770231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
2918b032f27cSSam Leffler 			return EOPNOTSUPP;
291970231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
292070231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
292170231e3dSSam Leffler 			return EINVAL;
2922b032f27cSSam Leffler 		vap->iv_fragthreshold = ireq->i_val;
2923b032f27cSSam Leffler 		error = ERESTART;
292470231e3dSSam Leffler 		break;
2925c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
2926c27e4e31SSam Leffler 		if (ireq->i_val) {
2927b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
2928b032f27cSSam Leffler 				return EOPNOTSUPP;
2929b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
2930c27e4e31SSam Leffler 		} else
2931b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
2932b032f27cSSam Leffler 		error = ERESTART;
2933c27e4e31SSam Leffler 		break;
2934546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
2935546786c9SSam Leffler 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
2936546786c9SSam Leffler 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
2937546786c9SSam Leffler 			return EINVAL;
2938b032f27cSSam Leffler 		vap->iv_bmissthreshold = ireq->i_val;
2939b032f27cSSam Leffler 		error = ERESTART;
2940546786c9SSam Leffler 		break;
294168e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
2942b032f27cSSam Leffler 		error = ieee80211_ioctl_setcurchan(vap, ireq);
294368e8e04eSSam Leffler 		break;
294468e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
294568e8e04eSSam Leffler 		if (ireq->i_val) {
294668e8e04eSSam Leffler #define	IEEE80211_HTCAP_SHORTGI \
294768e8e04eSSam Leffler 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
2948b032f27cSSam Leffler 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
294968e8e04eSSam Leffler 				return EINVAL;
295068e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
2951b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI20;
295268e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
2953b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI40;
295468e8e04eSSam Leffler #undef IEEE80211_HTCAP_SHORTGI
295568e8e04eSSam Leffler 		} else
2956b032f27cSSam Leffler 			vap->iv_flags_ext &=
295768e8e04eSSam Leffler 			    ~(IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40);
2958b032f27cSSam Leffler 		error = ERESTART;
295968e8e04eSSam Leffler 		break;
296068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
2961b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
296268e8e04eSSam Leffler 			return EINVAL;
296368e8e04eSSam Leffler 		if (ireq->i_val & 1)
2964b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
2965b032f27cSSam Leffler 		else
2966b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_TX;
296768e8e04eSSam Leffler 		if (ireq->i_val & 2)
2968b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
2969b032f27cSSam Leffler 		else
2970b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_RX;
297168e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
2972b032f27cSSam Leffler 		if (isvapht(vap))
2973b032f27cSSam Leffler 			error = ERESTART;
297468e8e04eSSam Leffler 		break;
297568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
2976b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
2977b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
2978b032f27cSSam Leffler 			return EINVAL;
2979b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2980b032f27cSSam Leffler 			vap->iv_ampdu_rxmax = ireq->i_val;
2981b032f27cSSam Leffler 		else
2982b032f27cSSam Leffler 			vap->iv_ampdu_limit = ireq->i_val;
2983b032f27cSSam Leffler 		error = ERESTART;
298468e8e04eSSam Leffler 		break;
298568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
2986b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
2987b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
2988b032f27cSSam Leffler 			return EINVAL;
2989b032f27cSSam Leffler 		vap->iv_ampdu_density = ireq->i_val;
2990b032f27cSSam Leffler 		error = ERESTART;
299168e8e04eSSam Leffler 		break;
299268e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
2993b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
299468e8e04eSSam Leffler 			return EINVAL;
299568e8e04eSSam Leffler 		if (ireq->i_val & 1)
2996b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
2997b032f27cSSam Leffler 		else
2998b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_TX;
299968e8e04eSSam Leffler 		if (ireq->i_val & 2)
3000b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
3001b032f27cSSam Leffler 		else
3002b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_RX;
300368e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3004b032f27cSSam Leffler 		if (isvapht(vap))
3005b032f27cSSam Leffler 			error = ERESTART;
300668e8e04eSSam Leffler 		break;
300768e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
300868e8e04eSSam Leffler 		/* XXX validate */
3009b032f27cSSam Leffler 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
301068e8e04eSSam Leffler 		break;
301168e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
301268e8e04eSSam Leffler 		if (ireq->i_val) {
3013b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
301468e8e04eSSam Leffler 				return EINVAL;
3015b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_PUREN;
301668e8e04eSSam Leffler 		} else
3017b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_PUREN;
301868e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3019b032f27cSSam Leffler 		if (isvapht(vap))
3020b032f27cSSam Leffler 			error = ERESTART;
302168e8e04eSSam Leffler 		break;
302268e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
302368e8e04eSSam Leffler 		if (ireq->i_val) {
302468e8e04eSSam Leffler #if 0
302568e8e04eSSam Leffler 			/* XXX no capability */
3026b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3027b032f27cSSam Leffler 				return EOPNOTSUPP;
302868e8e04eSSam Leffler #endif
3029b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DOTH;
303068e8e04eSSam Leffler 		} else
3031b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3032b032f27cSSam Leffler 		error = ENETRESET;
3033b032f27cSSam Leffler 		break;
3034b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
3035b032f27cSSam Leffler 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3036b032f27cSSam Leffler 		break;
3037b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
3038b032f27cSSam Leffler 		error = ieee80211_ioctl_setroam(vap, ireq);
3039b032f27cSSam Leffler 		break;
3040b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
3041b032f27cSSam Leffler 		error = ieee80211_ioctl_settxparams(vap, ireq);
304268e8e04eSSam Leffler 		break;
304368e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
304468e8e04eSSam Leffler 		if (ireq->i_val) {
3045b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
3046b032f27cSSam Leffler 				return EOPNOTSUPP;
3047b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_HTCOMPAT;
304868e8e04eSSam Leffler 		} else
3049b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_HTCOMPAT;
305068e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3051b032f27cSSam Leffler 		if (isvapht(vap))
3052b032f27cSSam Leffler 			error = ERESTART;
3053b032f27cSSam Leffler 		break;
3054b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
3055b032f27cSSam Leffler 		if (ireq->i_val) {
3056b032f27cSSam Leffler 			/* NB: DWDS only makes sense for WDS-capable devices */
3057b032f27cSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3058b032f27cSSam Leffler 				return EOPNOTSUPP;
3059b032f27cSSam Leffler 			/* NB: DWDS is used only with ap+sta vaps */
3060b032f27cSSam Leffler 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3061b032f27cSSam Leffler 			    vap->iv_opmode != IEEE80211_M_STA)
3062b032f27cSSam Leffler 				return EINVAL;
3063b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DWDS;
3064b032f27cSSam Leffler 		} else
3065b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DWDS;
306668e8e04eSSam Leffler 		break;
3067c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
3068c066143cSSam Leffler 		if (ireq->i_val)
3069b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3070c066143cSSam Leffler 		else
3071b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3072b032f27cSSam Leffler 		break;
3073b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
3074b032f27cSSam Leffler 		error = ieee80211_ioctl_setappie(vap, ireq);
3075b032f27cSSam Leffler 		break;
3076b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
3077b032f27cSSam Leffler 		if (ireq->i_val) {
3078b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3079b032f27cSSam Leffler 				return EOPNOTSUPP;
3080b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3081b032f27cSSam Leffler 		} else
3082b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3083b032f27cSSam Leffler 		break;
3084b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
3085b032f27cSSam Leffler 		if (ireq->i_val) {
3086b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3087b032f27cSSam Leffler 				return EOPNOTSUPP;
3088b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3089b032f27cSSam Leffler 		} else
3090b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3091b032f27cSSam Leffler 		break;
3092b032f27cSSam Leffler 	case IEEE80211_IOC_CHANSWITCH:
3093b032f27cSSam Leffler 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3094b032f27cSSam Leffler 		break;
3095b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
3096b032f27cSSam Leffler 		if (ireq->i_val) {
3097b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3098b032f27cSSam Leffler 				return EOPNOTSUPP;
3099b032f27cSSam Leffler 			/* NB: DFS requires 11h support */
3100b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3101b032f27cSSam Leffler 				return EINVAL;
3102b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3103b032f27cSSam Leffler 		} else
3104b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3105b032f27cSSam Leffler 		break;
3106b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
3107b032f27cSSam Leffler 		if (ireq->i_val)
3108b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3109b032f27cSSam Leffler 		else
3110b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3111b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
3112b032f27cSSam Leffler 			error = ENETRESET;
3113c066143cSSam Leffler 		break;
31141b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
31151b6167d2SSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
31161b6167d2SSam Leffler 			return EINVAL;
31171b6167d2SSam Leffler 		ic->ic_htprotmode = ireq->i_val ?
31181b6167d2SSam Leffler 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
31191b6167d2SSam Leffler 		/* NB: if not operating in 11n this can wait */
3120b032f27cSSam Leffler 		if (isvapht(vap))
31211b6167d2SSam Leffler 			error = ERESTART;
31221b6167d2SSam Leffler 		break;
3123b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
3124b032f27cSSam Leffler 		error = ieee80211_ioctl_setstavlan(vap, ireq);
31251b6167d2SSam Leffler 		break;
31268c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
31278c070d69SSam Leffler 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
31288c070d69SSam Leffler 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
31298c070d69SSam Leffler 			return EINVAL;
31308c070d69SSam Leffler 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
31318c070d69SSam Leffler 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
31328c070d69SSam Leffler 			return EOPNOTSUPP;
31338c070d69SSam Leffler 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
31348c070d69SSam Leffler 			ireq->i_val;
31358c070d69SSam Leffler 		/* NB: if not operating in 11n this can wait */
31368c070d69SSam Leffler 		if (isvapht(vap))
31378c070d69SSam Leffler 			error = ERESTART;
31388c070d69SSam Leffler 		break;
313944f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
314044f7a6edSSam Leffler 		if (ireq->i_val != 0) {
314144f7a6edSSam Leffler 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
314244f7a6edSSam Leffler 				return EOPNOTSUPP;
314344f7a6edSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RIFS;
314444f7a6edSSam Leffler 		} else
314544f7a6edSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RIFS;
314644f7a6edSSam Leffler 		/* NB: if not operating in 11n this can wait */
314744f7a6edSSam Leffler 		if (isvapht(vap))
314844f7a6edSSam Leffler 			error = ERESTART;
314944f7a6edSSam Leffler 		break;
315010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
315110ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOT:
315210ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOTCNT:
315310ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_SLOTLEN:
315410ad9a77SSam Leffler 	case IEEE80211_IOC_TDMA_BINTERVAL:
315510ad9a77SSam Leffler 		error = ieee80211_tdma_ioctl_set80211(vap, ireq);
315610ad9a77SSam Leffler 		break;
315710ad9a77SSam Leffler #endif
31581a1e1d21SSam Leffler 	default:
31591a1e1d21SSam Leffler 		error = EINVAL;
31601a1e1d21SSam Leffler 		break;
31611a1e1d21SSam Leffler 	}
3162b032f27cSSam Leffler 	/*
3163b032f27cSSam Leffler 	 * The convention is that ENETRESET means an operation
3164b032f27cSSam Leffler 	 * requires a complete re-initialization of the device (e.g.
3165b032f27cSSam Leffler 	 * changing something that affects the association state).
3166b032f27cSSam Leffler 	 * ERESTART means the request may be handled with only a
3167b032f27cSSam Leffler 	 * reload of the hardware state.  We hand ERESTART requests
3168b032f27cSSam Leffler 	 * to the iv_reset callback so the driver can decide.  If
3169b032f27cSSam Leffler 	 * a device does not fillin iv_reset then it defaults to one
3170b032f27cSSam Leffler 	 * that returns ENETRESET.  Otherwise a driver may return
3171b032f27cSSam Leffler 	 * ENETRESET (in which case a full reset will be done) or
3172b032f27cSSam Leffler 	 * 0 to mean there's no need to do anything (e.g. when the
3173b032f27cSSam Leffler 	 * change has no effect on the driver/device).
3174b032f27cSSam Leffler 	 */
3175b032f27cSSam Leffler 	if (error == ERESTART)
3176b032f27cSSam Leffler 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3177b032f27cSSam Leffler 		    vap->iv_reset(vap, ireq->i_type) : 0;
3178b032f27cSSam Leffler 	if (error == ENETRESET) {
3179b032f27cSSam Leffler 		/* XXX need to re-think AUTO handling */
3180b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
3181b032f27cSSam Leffler 			ieee80211_init(vap);
3182b032f27cSSam Leffler 		error = 0;
3183b032f27cSSam Leffler 	}
31848a1b9b6aSSam Leffler 	return error;
31858a1b9b6aSSam Leffler }
31868a1b9b6aSSam Leffler 
3187b032f27cSSam Leffler /*
3188b032f27cSSam Leffler  * Rebuild the parent's multicast address list after an add/del
3189b032f27cSSam Leffler  * of a multicast address for a vap.  We have no way to tell
3190b032f27cSSam Leffler  * what happened above to optimize the work so we purge the entire
3191b032f27cSSam Leffler  * list and rebuild from scratch.  This is way expensive.
3192b032f27cSSam Leffler  * Note also the half-baked workaround for if_addmulti calling
3193b032f27cSSam Leffler  * back to the parent device; there's no way to insert mcast
3194b032f27cSSam Leffler  * entries quietly and/or cheaply.
3195b032f27cSSam Leffler  */
3196b032f27cSSam Leffler static void
3197b032f27cSSam Leffler ieee80211_ioctl_updatemulti(struct ieee80211com *ic)
31988a1b9b6aSSam Leffler {
3199b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
3200b032f27cSSam Leffler 	struct ieee80211vap *vap;
3201b032f27cSSam Leffler 	void *ioctl;
3202b032f27cSSam Leffler 
3203b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
3204b032f27cSSam Leffler 	if_purgemaddrs(parent);
3205b032f27cSSam Leffler 	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3206b032f27cSSam Leffler 	parent->if_ioctl = NULL;
3207b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3208b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
3209b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
3210b032f27cSSam Leffler 
3211b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
3212b032f27cSSam Leffler 			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3213b032f27cSSam Leffler 	}
3214b032f27cSSam Leffler 	parent->if_ioctl = ioctl;
3215b032f27cSSam Leffler 
3216b032f27cSSam Leffler 	ic->ic_update_mcast(ic->ic_ifp);
3217b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
3218b032f27cSSam Leffler }
3219b032f27cSSam Leffler 
3220b032f27cSSam Leffler int
3221b032f27cSSam Leffler ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3222b032f27cSSam Leffler {
3223b032f27cSSam Leffler 	struct ieee80211vap *vap;
3224b032f27cSSam Leffler 	struct ieee80211com *ic;
32258a1b9b6aSSam Leffler 	int error = 0;
32268a1b9b6aSSam Leffler 	struct ifreq *ifr;
32278a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
32288a1b9b6aSSam Leffler 
3229b032f27cSSam Leffler 	vap = ifp->if_softc;
3230b032f27cSSam Leffler 	if (vap == NULL) {
3231b032f27cSSam Leffler 		/*
3232b032f27cSSam Leffler 		 * During detach we clear the backpointer in the softc
3233b032f27cSSam Leffler 		 * so any ioctl request through the ifnet that arrives
3234b032f27cSSam Leffler 		 * before teardown is ignored/rejected.  In particular
3235b032f27cSSam Leffler 		 * this hack handles destroying a vap used by an app
3236b032f27cSSam Leffler 		 * like wpa_supplicant that will respond to the vap
3237b032f27cSSam Leffler 		 * being forced into INIT state by immediately trying
3238b032f27cSSam Leffler 		 * to force it back up.  We can yank this hack if/when
3239b032f27cSSam Leffler 		 * we can destroy the ifnet before cleaning up vap state.
3240b032f27cSSam Leffler 		 */
3241b032f27cSSam Leffler 		return ENXIO;
3242b032f27cSSam Leffler 	}
32438a1b9b6aSSam Leffler 	switch (cmd) {
3244b032f27cSSam Leffler 	case SIOCSIFFLAGS:
3245b032f27cSSam Leffler 		ic = vap->iv_ic;
3246b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
3247b032f27cSSam Leffler 		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3248b032f27cSSam Leffler 		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3249b032f27cSSam Leffler 		if (ifp->if_flags & IFF_UP) {
3250b032f27cSSam Leffler 			/*
3251b032f27cSSam Leffler 			 * Bring ourself up unless we're already operational.
3252b032f27cSSam Leffler 			 * If we're the first vap and the parent is not up
3253b032f27cSSam Leffler 			 * then it will automatically be brought up as a
3254b032f27cSSam Leffler 			 * side-effect of bringing ourself up.
3255b032f27cSSam Leffler 			 */
3256b032f27cSSam Leffler 			if (vap->iv_state == IEEE80211_S_INIT)
3257b032f27cSSam Leffler 				ieee80211_start_locked(vap);
3258b032f27cSSam Leffler 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3259b032f27cSSam Leffler 			/*
3260b032f27cSSam Leffler 			 * Stop ourself.  If we are the last vap to be
3261b032f27cSSam Leffler 			 * marked down the parent will also be taken down.
3262b032f27cSSam Leffler 			 */
3263b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
3264b032f27cSSam Leffler 		}
3265b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
32668d9b29f3SAndrew Thompson 		/* Wait for parent ioctl handler if it was queued */
3267ae55932eSAndrew Thompson 		ieee80211_waitfor_parent(ic);
3268b032f27cSSam Leffler 		break;
3269b032f27cSSam Leffler 	case SIOCADDMULTI:
3270b032f27cSSam Leffler 	case SIOCDELMULTI:
3271b032f27cSSam Leffler 		ieee80211_ioctl_updatemulti(vap->iv_ic);
3272b032f27cSSam Leffler 		break;
32738a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
32748a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
3275b032f27cSSam Leffler 		ifr = (struct ifreq *)data;
3276b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
32778a1b9b6aSSam Leffler 		break;
32788a1b9b6aSSam Leffler 	case SIOCG80211:
3279b032f27cSSam Leffler 		error = ieee80211_ioctl_get80211(vap, cmd,
32808a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
32818a1b9b6aSSam Leffler 		break;
32828a1b9b6aSSam Leffler 	case SIOCS80211:
3283acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
32848a1b9b6aSSam Leffler 		if (error == 0)
3285b032f27cSSam Leffler 			error = ieee80211_ioctl_set80211(vap, cmd,
32868a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
32871a1e1d21SSam Leffler 		break;
32881be50176SSam Leffler 	case SIOCG80211STATS:
32891be50176SSam Leffler 		ifr = (struct ifreq *)data;
3290b032f27cSSam Leffler 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
32911be50176SSam Leffler 		break;
32926f161f03SSam Leffler 	case SIOCSIFMTU:
32936f161f03SSam Leffler 		ifr = (struct ifreq *)data;
32946f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
32956f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
32966f161f03SSam Leffler 			error = EINVAL;
32976f161f03SSam Leffler 		else
32986f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
32996f161f03SSam Leffler 		break;
3300b2e95691SSam Leffler 	case SIOCSIFADDR:
3301b2e95691SSam Leffler 		/*
3302b2e95691SSam Leffler 		 * XXX Handle this directly so we can supress if_init calls.
3303b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
3304b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
3305b2e95691SSam Leffler 		 * XXX set IFF_UP and so supress if_init being called when
3306b2e95691SSam Leffler 		 * XXX it should be.
3307b2e95691SSam Leffler 		 */
3308b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
3309b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
3310b2e95691SSam Leffler #ifdef INET
3311b2e95691SSam Leffler 		case AF_INET:
3312b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3313b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3314b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3315b2e95691SSam Leffler 			}
3316b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
3317b2e95691SSam Leffler 			break;
3318b2e95691SSam Leffler #endif
3319b2e95691SSam Leffler #ifdef IPX
3320b2e95691SSam Leffler 		/*
3321b2e95691SSam Leffler 		 * XXX - This code is probably wrong,
3322b2e95691SSam Leffler 		 *	 but has been copied many times.
3323b2e95691SSam Leffler 		 */
3324b2e95691SSam Leffler 		case AF_IPX: {
3325b2e95691SSam Leffler 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3326b2e95691SSam Leffler 
3327b2e95691SSam Leffler 			if (ipx_nullhost(*ina))
3328fc74a9f9SBrooks Davis 				ina->x_host = *(union ipx_host *)
33294a0d6638SRuslan Ermilov 				    IF_LLADDR(ifp);
3330b2e95691SSam Leffler 			else
3331b2e95691SSam Leffler 				bcopy((caddr_t) ina->x_host.c_host,
33324a0d6638SRuslan Ermilov 				      (caddr_t) IF_LLADDR(ifp),
3333fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
3334b2e95691SSam Leffler 			/* fall thru... */
3335b2e95691SSam Leffler 		}
3336b2e95691SSam Leffler #endif
3337b2e95691SSam Leffler 		default:
3338b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3339b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3340b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3341b2e95691SSam Leffler 			}
3342b2e95691SSam Leffler 			break;
3343b2e95691SSam Leffler 		}
3344b2e95691SSam Leffler 		break;
3345b032f27cSSam Leffler 	/* Pass NDIS ioctls up to the driver */
3346b032f27cSSam Leffler 	case SIOCGDRVSPEC:
3347b032f27cSSam Leffler 	case SIOCSDRVSPEC:
3348b032f27cSSam Leffler 	case SIOCGPRIVATE_0: {
3349b032f27cSSam Leffler 		struct ifnet *parent = vap->iv_ic->ic_ifp;
3350b032f27cSSam Leffler 		error = parent->if_ioctl(parent, cmd, data);
3351b032f27cSSam Leffler 		break;
3352b032f27cSSam Leffler 	}
33531a1e1d21SSam Leffler 	default:
33541a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
33551a1e1d21SSam Leffler 		break;
33561a1e1d21SSam Leffler 	}
33571a1e1d21SSam Leffler 	return error;
33581a1e1d21SSam Leffler }
3359