xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 76039bc84fae9915788b54ff28fe0cc4876952d2)
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>
451a1e1d21SSam Leffler 
461a1e1d21SSam Leffler #include <net/if.h>
47*76039bc8SGleb Smirnoff #include <net/if_var.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>
661a1e1d21SSam Leffler 
67b032f27cSSam Leffler #define	IS_UP_AUTO(_vap) \
683cdd9880SSam Leffler 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
69b032f27cSSam Leffler 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
708a1b9b6aSSam Leffler 
71b032f27cSSam Leffler static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
7268e8e04eSSam Leffler static struct ieee80211_channel *findchannel(struct ieee80211com *,
7368e8e04eSSam Leffler 		int ieee, int mode);
74632ee7e3SBernhard Schmidt static int ieee80211_scanreq(struct ieee80211vap *,
75632ee7e3SBernhard Schmidt 		struct ieee80211_scan_req *);
761a1e1d21SSam Leffler 
77b032f27cSSam Leffler static __noinline int
78b032f27cSSam Leffler ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
798a1b9b6aSSam Leffler {
80b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
818a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
828a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
838a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
848a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
858a1b9b6aSSam Leffler 	u_int kid;
868a1b9b6aSSam Leffler 	int error;
878a1b9b6aSSam Leffler 
888a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
898a1b9b6aSSam Leffler 		return EINVAL;
908a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
918a1b9b6aSSam Leffler 	if (error)
928a1b9b6aSSam Leffler 		return error;
938a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
948a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
95b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
968a1b9b6aSSam Leffler 		if (ni == NULL)
97b032f27cSSam Leffler 			return ENOENT;
988a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
998a1b9b6aSSam Leffler 	} else {
1008a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
1018a1b9b6aSSam Leffler 			return EINVAL;
102b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
103b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
1048a1b9b6aSSam Leffler 		ni = NULL;
1058a1b9b6aSSam Leffler 	}
1068a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
1078a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
1088a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
1098a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
110b032f27cSSam Leffler 	if (wk->wk_keyix == vap->iv_def_txkey)
1118a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
112acd3428bSRobert Watson 	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
1138a1b9b6aSSam Leffler 		/* NB: only root can read key data */
114b032f27cSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
1158a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
1168a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
1178a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
1188a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
1198a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
1208a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
1218a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
1228a1b9b6aSSam Leffler 		}
1238a1b9b6aSSam Leffler 	} else {
1248a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
1258a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
1268a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
1278a1b9b6aSSam Leffler 	}
1288a1b9b6aSSam Leffler 	if (ni != NULL)
1298a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
1308a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
1318a1b9b6aSSam Leffler }
1328a1b9b6aSSam Leffler 
133b032f27cSSam Leffler static __noinline int
134b032f27cSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1358a1b9b6aSSam Leffler {
136b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1378a1b9b6aSSam Leffler 
138e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
1398a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1408a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
1418a1b9b6aSSam Leffler }
1428a1b9b6aSSam Leffler 
143b032f27cSSam Leffler static __noinline int
144b032f27cSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
1458a1b9b6aSSam Leffler {
146b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
147db9ff08bSKevin Lo 	uint32_t space;
1488a1b9b6aSSam Leffler 
1498a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
15068e8e04eSSam Leffler 			ic_chans[ic->ic_nchans]);
1518a1b9b6aSSam Leffler 	if (space > ireq->i_len)
1528a1b9b6aSSam Leffler 		space = ireq->i_len;
15368e8e04eSSam Leffler 	/* XXX assumes compatible layout */
15468e8e04eSSam Leffler 	return copyout(&ic->ic_nchans, ireq->i_data, space);
1558a1b9b6aSSam Leffler }
1568a1b9b6aSSam Leffler 
157b032f27cSSam Leffler static __noinline int
158b032f27cSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
159b032f27cSSam Leffler 	struct ieee80211req *ireq, int req)
1608a1b9b6aSSam Leffler {
1618a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16268e8e04eSSam Leffler 	struct ieee80211req_wpaie2 wpaie;
1638a1b9b6aSSam Leffler 	int error;
1648a1b9b6aSSam Leffler 
1658a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1668a1b9b6aSSam Leffler 		return EINVAL;
1678a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
1688a1b9b6aSSam Leffler 	if (error != 0)
1698a1b9b6aSSam Leffler 		return error;
170b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie.wpa_macaddr);
1718a1b9b6aSSam Leffler 	if (ni == NULL)
172b032f27cSSam Leffler 		return ENOENT;
1738a1b9b6aSSam Leffler 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
174b032f27cSSam Leffler 	if (ni->ni_ies.wpa_ie != NULL) {
175b032f27cSSam Leffler 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
1768a1b9b6aSSam Leffler 		if (ielen > sizeof(wpaie.wpa_ie))
1778a1b9b6aSSam Leffler 			ielen = sizeof(wpaie.wpa_ie);
178b032f27cSSam Leffler 		memcpy(wpaie.wpa_ie, ni->ni_ies.wpa_ie, ielen);
1798a1b9b6aSSam Leffler 	}
18068e8e04eSSam Leffler 	if (req == IEEE80211_IOC_WPAIE2) {
18168e8e04eSSam Leffler 		memset(wpaie.rsn_ie, 0, sizeof(wpaie.rsn_ie));
182b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
183b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
18468e8e04eSSam Leffler 			if (ielen > sizeof(wpaie.rsn_ie))
18568e8e04eSSam Leffler 				ielen = sizeof(wpaie.rsn_ie);
186b032f27cSSam Leffler 			memcpy(wpaie.rsn_ie, ni->ni_ies.rsn_ie, ielen);
18768e8e04eSSam Leffler 		}
18868e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
18968e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
19068e8e04eSSam Leffler 	} else {
19168e8e04eSSam Leffler 		/* compatibility op, may overwrite wpa ie */
19268e8e04eSSam Leffler 		/* XXX check ic_flags? */
193b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
194b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
19568e8e04eSSam Leffler 			if (ielen > sizeof(wpaie.wpa_ie))
19668e8e04eSSam Leffler 				ielen = sizeof(wpaie.wpa_ie);
197b032f27cSSam Leffler 			memcpy(wpaie.wpa_ie, ni->ni_ies.rsn_ie, ielen);
19868e8e04eSSam Leffler 		}
19968e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
20068e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
20168e8e04eSSam Leffler 	}
2028a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2038a1b9b6aSSam Leffler 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
2048a1b9b6aSSam Leffler }
2058a1b9b6aSSam Leffler 
206b032f27cSSam Leffler static __noinline int
207b032f27cSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
2088a1b9b6aSSam Leffler {
2098a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
21068e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
211db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
2128a1b9b6aSSam Leffler 	int error;
2138a1b9b6aSSam Leffler 
2148a1b9b6aSSam Leffler 	if (ireq->i_len < off)
2158a1b9b6aSSam Leffler 		return EINVAL;
2168a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
2178a1b9b6aSSam Leffler 	if (error != 0)
2188a1b9b6aSSam Leffler 		return error;
219b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
22068e8e04eSSam Leffler 	if (ni == NULL)
221b032f27cSSam Leffler 		return ENOENT;
2228a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
2238a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
2248a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
22568e8e04eSSam Leffler 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
2268a1b9b6aSSam Leffler 			ireq->i_len - off);
2278a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2288a1b9b6aSSam Leffler 	return error;
2298a1b9b6aSSam Leffler }
2308a1b9b6aSSam Leffler 
23168e8e04eSSam Leffler struct scanreq {
23268e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
23368e8e04eSSam Leffler 	size_t space;
23468e8e04eSSam Leffler };
23568e8e04eSSam Leffler 
23668e8e04eSSam Leffler static size_t
23768e8e04eSSam Leffler scan_space(const struct ieee80211_scan_entry *se, int *ielen)
23868e8e04eSSam Leffler {
23968e8e04eSSam Leffler 	size_t len;
24068e8e04eSSam Leffler 
241b032f27cSSam Leffler 	*ielen = se->se_ies.len;
24268e8e04eSSam Leffler 	/*
24368e8e04eSSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
24468e8e04eSSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
24568e8e04eSSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
24668e8e04eSSam Leffler 	 */
24759aa14a9SRui Paulo 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
24859aa14a9SRui Paulo 	    se->se_meshid[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 
29359aa14a9SRui Paulo 	/* copy SSID */
29468e8e04eSSam Leffler 	sr->isr_ssid_len = se->se_ssid[1];
29568e8e04eSSam Leffler 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
29668e8e04eSSam Leffler 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
29768e8e04eSSam Leffler 
29859aa14a9SRui Paulo 	/* copy mesh id */
29968e8e04eSSam Leffler 	cp += sr->isr_ssid_len;
30059aa14a9SRui Paulo 	sr->isr_meshid_len = se->se_meshid[1];
30159aa14a9SRui Paulo 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
30259aa14a9SRui Paulo 	cp += sr->isr_meshid_len;
30359aa14a9SRui Paulo 
30459aa14a9SRui Paulo 	if (ielen)
305b032f27cSSam Leffler 		memcpy(cp, se->se_ies.data, ielen);
30668e8e04eSSam Leffler 
30768e8e04eSSam Leffler 	req->space -= len;
30868e8e04eSSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
30968e8e04eSSam Leffler }
31068e8e04eSSam Leffler 
311b032f27cSSam Leffler static __noinline int
312b032f27cSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
313b032f27cSSam Leffler 	struct ieee80211req *ireq)
31468e8e04eSSam Leffler {
31568e8e04eSSam Leffler 	struct scanreq req;
31668e8e04eSSam Leffler 	int error;
31768e8e04eSSam Leffler 
318b032f27cSSam Leffler 	if (ireq->i_len < sizeof(struct scanreq))
31968e8e04eSSam Leffler 		return EFAULT;
32068e8e04eSSam Leffler 
32168e8e04eSSam Leffler 	error = 0;
32268e8e04eSSam Leffler 	req.space = 0;
323b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, get_scan_space, &req);
32468e8e04eSSam Leffler 	if (req.space > ireq->i_len)
32568e8e04eSSam Leffler 		req.space = ireq->i_len;
32668e8e04eSSam Leffler 	if (req.space > 0) {
327db9ff08bSKevin Lo 		uint32_t space;
32868e8e04eSSam Leffler 		void *p;
32968e8e04eSSam Leffler 
33068e8e04eSSam Leffler 		space = req.space;
33168e8e04eSSam Leffler 		/* XXX M_WAITOK after driver lock released */
332e2126decSSam Leffler 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
33368e8e04eSSam Leffler 		if (p == NULL)
33468e8e04eSSam Leffler 			return ENOMEM;
33568e8e04eSSam Leffler 		req.sr = p;
336b032f27cSSam Leffler 		ieee80211_scan_iterate(vap, get_scan_result, &req);
337239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
338239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
339e2126decSSam Leffler 		free(p, M_TEMP);
340239cc3b6SSam Leffler 	} else
341239cc3b6SSam Leffler 		ireq->i_len = 0;
342239cc3b6SSam Leffler 
343239cc3b6SSam Leffler 	return error;
344239cc3b6SSam Leffler }
3458a1b9b6aSSam Leffler 
3462cab1d3dSSam Leffler struct stainforeq {
347b032f27cSSam Leffler 	struct ieee80211vap *vap;
3482cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3492cab1d3dSSam Leffler 	size_t	space;
3502cab1d3dSSam Leffler };
3518a1b9b6aSSam Leffler 
3522cab1d3dSSam Leffler static size_t
3532cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
3542cab1d3dSSam Leffler {
355b032f27cSSam Leffler 	*ielen = ni->ni_ies.len;
3562cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
35768e8e04eSSam Leffler 		      sizeof(uint32_t));
3582cab1d3dSSam Leffler }
3592cab1d3dSSam Leffler 
3602cab1d3dSSam Leffler static void
3612cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
3622cab1d3dSSam Leffler {
3632cab1d3dSSam Leffler 	struct stainforeq *req = arg;
3642cab1d3dSSam Leffler 	size_t ielen;
3652cab1d3dSSam Leffler 
366b032f27cSSam Leffler 	if (req->vap != ni->ni_vap)
367b032f27cSSam Leffler 		return;
368b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
3692cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3702cab1d3dSSam Leffler 		return;
3712cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
3722cab1d3dSSam Leffler }
3732cab1d3dSSam Leffler 
374b032f27cSSam Leffler static __noinline void
3752cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
3762cab1d3dSSam Leffler {
3772cab1d3dSSam Leffler 	struct stainforeq *req = arg;
378b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3792cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3802cab1d3dSSam Leffler 	size_t ielen, len;
38168e8e04eSSam Leffler 	uint8_t *cp;
3822cab1d3dSSam Leffler 
383b032f27cSSam Leffler 	if (req->vap != ni->ni_vap)
384b032f27cSSam Leffler 		return;
385b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
3862cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3872cab1d3dSSam Leffler 		return;
3882cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
3892cab1d3dSSam Leffler 		return;
3902cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
3912cab1d3dSSam Leffler 	if (len > req->space)
3922cab1d3dSSam Leffler 		return;
3932cab1d3dSSam Leffler 	si = req->si;
3942cab1d3dSSam Leffler 	si->isi_len = len;
39568e8e04eSSam Leffler 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
3962cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
3978a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
3988a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
3998a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
4008a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
401b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
402b032f27cSSam Leffler 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
4038a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
4048a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
4058a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
4068a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
4078a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
4088a1b9b6aSSam Leffler 		si->isi_nrates = 15;
4098a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
4108a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
411b032f27cSSam Leffler 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
412b032f27cSSam Leffler 		const struct ieee80211_mcs_rates *mcs =
413b032f27cSSam Leffler 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
414b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
4156ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
416b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_800ns;
417b032f27cSSam Leffler 			else
418b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_400ns;
419b032f27cSSam Leffler 		} else {
4206ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
421b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_800ns;
422b032f27cSSam Leffler 			else
423b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_400ns;
424b032f27cSSam Leffler 		}
425b032f27cSSam Leffler 	} else
426b032f27cSSam Leffler 		si->isi_txmbps = si->isi_txrate;
4278a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
4288a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
4298a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
4308a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
4318a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
4328a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
4338a1b9b6aSSam Leffler 	} else {
434801df4a5SSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
435801df4a5SSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
4368a1b9b6aSSam Leffler 	}
4372cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
4382cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
439b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_run;
440b032f27cSSam Leffler 	else if (ni->ni_associd != 0 ||
441b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_WDS &&
442b032f27cSSam Leffler 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
443b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_auth;
4448a1b9b6aSSam Leffler 	else
445b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_init;
4468a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
44759aa14a9SRui Paulo 	si->isi_localid = ni->ni_mllid;
44859aa14a9SRui Paulo 	si->isi_peerid = ni->ni_mlpid;
44959aa14a9SRui Paulo 	si->isi_peerstate = ni->ni_mlstate;
4508a1b9b6aSSam Leffler 
45168e8e04eSSam Leffler 	if (ielen) {
45268e8e04eSSam Leffler 		cp = ((uint8_t *)si) + si->isi_ie_off;
453b032f27cSSam Leffler 		memcpy(cp, ni->ni_ies.data, ielen);
4548a1b9b6aSSam Leffler 	}
4552cab1d3dSSam Leffler 
45668e8e04eSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
4572cab1d3dSSam Leffler 	req->space -= len;
4588a1b9b6aSSam Leffler }
4592cab1d3dSSam Leffler 
460b032f27cSSam Leffler static __noinline int
461b032f27cSSam Leffler getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
462db9ff08bSKevin Lo 	struct ieee80211_node *ni, size_t off)
4632cab1d3dSSam Leffler {
464b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4652cab1d3dSSam Leffler 	struct stainforeq req;
466d1c85daeSSam Leffler 	size_t space;
467d1c85daeSSam Leffler 	void *p;
4682cab1d3dSSam Leffler 	int error;
4692cab1d3dSSam Leffler 
4702cab1d3dSSam Leffler 	error = 0;
4712cab1d3dSSam Leffler 	req.space = 0;
472b032f27cSSam Leffler 	req.vap = vap;
473d1c85daeSSam Leffler 	if (ni == NULL)
4742cab1d3dSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
475d1c85daeSSam Leffler 	else
476d1c85daeSSam Leffler 		get_sta_space(&req, ni);
4772cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
4782cab1d3dSSam Leffler 		req.space = ireq->i_len;
4792cab1d3dSSam Leffler 	if (req.space > 0) {
4802cab1d3dSSam Leffler 		space = req.space;
4812cab1d3dSSam Leffler 		/* XXX M_WAITOK after driver lock released */
482e2126decSSam Leffler 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
483d1c85daeSSam Leffler 		if (p == NULL) {
484d1c85daeSSam Leffler 			error = ENOMEM;
485d1c85daeSSam Leffler 			goto bad;
486d1c85daeSSam Leffler 		}
4872cab1d3dSSam Leffler 		req.si = p;
488d1c85daeSSam Leffler 		if (ni == NULL)
4892cab1d3dSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
490d1c85daeSSam Leffler 		else
491d1c85daeSSam Leffler 			get_sta_info(&req, ni);
4922cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
49368e8e04eSSam Leffler 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
494e2126decSSam Leffler 		free(p, M_TEMP);
4952cab1d3dSSam Leffler 	} else
4962cab1d3dSSam Leffler 		ireq->i_len = 0;
497d1c85daeSSam Leffler bad:
498d1c85daeSSam Leffler 	if (ni != NULL)
499d1c85daeSSam Leffler 		ieee80211_free_node(ni);
5008a1b9b6aSSam Leffler 	return error;
5018a1b9b6aSSam Leffler }
5028a1b9b6aSSam Leffler 
503b032f27cSSam Leffler static __noinline int
504b032f27cSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
505d1c85daeSSam Leffler {
50668e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
507db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
508d1c85daeSSam Leffler 	struct ieee80211_node *ni;
509d1c85daeSSam Leffler 	int error;
510d1c85daeSSam Leffler 
511d1c85daeSSam Leffler 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
512d1c85daeSSam Leffler 		return EFAULT;
513d1c85daeSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
514d1c85daeSSam Leffler 	if (error != 0)
515d1c85daeSSam Leffler 		return error;
516b032f27cSSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
517d1c85daeSSam Leffler 		ni = NULL;
518d1c85daeSSam Leffler 	} else {
519b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
52068e8e04eSSam Leffler 		if (ni == NULL)
521b032f27cSSam Leffler 			return ENOENT;
522d1c85daeSSam Leffler 	}
523b032f27cSSam Leffler 	return getstainfo_common(vap, ireq, ni, off);
524d1c85daeSSam Leffler }
525d1c85daeSSam Leffler 
526b032f27cSSam Leffler static __noinline int
527b032f27cSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
5288a1b9b6aSSam Leffler {
5298a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
5308a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
5318a1b9b6aSSam Leffler 	int error;
5328a1b9b6aSSam Leffler 
5338a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
5348a1b9b6aSSam Leffler 		return EINVAL;
5358a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
5368a1b9b6aSSam Leffler 	if (error != 0)
5378a1b9b6aSSam Leffler 		return error;
538b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
5398a1b9b6aSSam Leffler 	if (ni == NULL)
540b032f27cSSam Leffler 		return ENOENT;
5418a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
5428a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
5438a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
5448a1b9b6aSSam Leffler 	return error;
5458a1b9b6aSSam Leffler }
5468a1b9b6aSSam Leffler 
547b032f27cSSam Leffler static __noinline int
548b032f27cSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
5498a1b9b6aSSam Leffler {
550b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5518a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
5528a1b9b6aSSam Leffler 	struct wmeParams *wmep;
5538a1b9b6aSSam Leffler 	int ac;
5548a1b9b6aSSam Leffler 
5558a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
5568a1b9b6aSSam Leffler 		return EINVAL;
5578a1b9b6aSSam Leffler 
5588a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
5598a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
5608a1b9b6aSSam Leffler 		ac = WME_AC_BE;
5618a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
5628a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5638a1b9b6aSSam Leffler 	else
5648a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5658a1b9b6aSSam Leffler 	switch (ireq->i_type) {
5668a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
5678a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
5688a1b9b6aSSam Leffler 		break;
5698a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
5708a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
5718a1b9b6aSSam Leffler 		break;
5728a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
5738a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
5748a1b9b6aSSam Leffler 		break;
5758a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
5768a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
5778a1b9b6aSSam Leffler 		break;
5788a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
5798a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5808a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
5818a1b9b6aSSam Leffler 		break;
5828a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
5838a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5848a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
5858a1b9b6aSSam Leffler 		break;
5868a1b9b6aSSam Leffler 	}
5878a1b9b6aSSam Leffler 	return 0;
5888a1b9b6aSSam Leffler }
5898a1b9b6aSSam Leffler 
590b032f27cSSam Leffler static __noinline int
591b032f27cSSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
592188757f5SSam Leffler {
593b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
594188757f5SSam Leffler 
595b032f27cSSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
596188757f5SSam Leffler }
597188757f5SSam Leffler 
598b032f27cSSam Leffler static __noinline int
599b032f27cSSam Leffler ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
600b032f27cSSam Leffler {
601b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
602b032f27cSSam Leffler 	struct ieee80211_channel *c;
603b032f27cSSam Leffler 
604b032f27cSSam Leffler 	if (ireq->i_len != sizeof(struct ieee80211_channel))
605b032f27cSSam Leffler 		return EINVAL;
606b032f27cSSam Leffler 	/*
607b032f27cSSam Leffler 	 * vap's may have different operating channels when HT is
608b032f27cSSam Leffler 	 * in use.  When in RUN state report the vap-specific channel.
609b032f27cSSam Leffler 	 * Otherwise return curchan.
610b032f27cSSam Leffler 	 */
611b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_RUN)
612b032f27cSSam Leffler 		c = vap->iv_bss->ni_chan;
613b032f27cSSam Leffler 	else
614b032f27cSSam Leffler 		c = ic->ic_curchan;
615b032f27cSSam Leffler 	return copyout(c, ireq->i_data, sizeof(*c));
61668e8e04eSSam Leffler }
61768e8e04eSSam Leffler 
61868e8e04eSSam Leffler static int
619b032f27cSSam Leffler getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
62068e8e04eSSam Leffler {
621b032f27cSSam Leffler 	if (aie == NULL)
62268e8e04eSSam Leffler 		return EINVAL;
623b032f27cSSam Leffler 	/* NB: truncate, caller can check length */
624b032f27cSSam Leffler 	if (ireq->i_len > aie->ie_len)
625b032f27cSSam Leffler 		ireq->i_len = aie->ie_len;
626b032f27cSSam Leffler 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
627b032f27cSSam Leffler }
628b032f27cSSam Leffler 
629b032f27cSSam Leffler static int
630b032f27cSSam Leffler ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
631b032f27cSSam Leffler {
632b032f27cSSam Leffler 	uint8_t fc0;
633b032f27cSSam Leffler 
634b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
635b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
636b032f27cSSam Leffler 		return EINVAL;
637b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
638b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
639b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
640b032f27cSSam Leffler 		return getappie(vap->iv_appie_beacon, ireq);
641b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
642b032f27cSSam Leffler 		return getappie(vap->iv_appie_proberesp, ireq);
643b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
644b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocresp, ireq);
645b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
646b032f27cSSam Leffler 		return getappie(vap->iv_appie_probereq, ireq);
647b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
648b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocreq, ireq);
649b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
650b032f27cSSam Leffler 		return getappie(vap->iv_appie_wpa, ireq);
651b032f27cSSam Leffler 	}
652b032f27cSSam Leffler 	return EINVAL;
653b032f27cSSam Leffler }
654b032f27cSSam Leffler 
655b032f27cSSam Leffler static __noinline int
656b032f27cSSam Leffler ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
657b032f27cSSam Leffler 	const struct ieee80211req *ireq)
658b032f27cSSam Leffler {
659b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
660b032f27cSSam Leffler 
661b032f27cSSam Leffler 	if (ireq->i_len != sizeof(ic->ic_regdomain))
662b032f27cSSam Leffler 		return EINVAL;
663b032f27cSSam Leffler 	return copyout(&ic->ic_regdomain, ireq->i_data,
664b032f27cSSam Leffler 	    sizeof(ic->ic_regdomain));
665b032f27cSSam Leffler }
666b032f27cSSam Leffler 
667b032f27cSSam Leffler static __noinline int
668b032f27cSSam Leffler ieee80211_ioctl_getroam(struct ieee80211vap *vap,
669b032f27cSSam Leffler 	const struct ieee80211req *ireq)
670b032f27cSSam Leffler {
671cbb24c24SSam Leffler 	size_t len = ireq->i_len;
672cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
673cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_roamparms))
674cbb24c24SSam Leffler 		len = sizeof(vap->iv_roamparms);
675cbb24c24SSam Leffler 	return copyout(vap->iv_roamparms, ireq->i_data, len);
676b032f27cSSam Leffler }
677b032f27cSSam Leffler 
678b032f27cSSam Leffler static __noinline int
679b032f27cSSam Leffler ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
680b032f27cSSam Leffler 	const struct ieee80211req *ireq)
681b032f27cSSam Leffler {
682cbb24c24SSam Leffler 	size_t len = ireq->i_len;
683cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
684cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_txparms))
685cbb24c24SSam Leffler 		len = sizeof(vap->iv_txparms);
686cbb24c24SSam Leffler 	return copyout(vap->iv_txparms, ireq->i_data, len);
687b032f27cSSam Leffler }
688b032f27cSSam Leffler 
689b032f27cSSam Leffler static __noinline int
690b032f27cSSam Leffler ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
691b032f27cSSam Leffler 	const struct ieee80211req *ireq)
692b032f27cSSam Leffler {
693b032f27cSSam Leffler 	struct ieee80211_devcaps_req *dc;
694b032f27cSSam Leffler 	struct ieee80211req_chaninfo *ci;
6958658b18bSSam Leffler 	int maxchans, error;
696b032f27cSSam Leffler 
6978658b18bSSam Leffler 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
6988658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
6998658b18bSSam Leffler 	/* NB: require 1 so we know ic_nchans is accessible */
7008658b18bSSam Leffler 	if (maxchans < 1)
701b032f27cSSam Leffler 		return EINVAL;
7028658b18bSSam Leffler 	/* constrain max request size, 2K channels is ~24Kbytes */
7038658b18bSSam Leffler 	if (maxchans > 2048)
7048658b18bSSam Leffler 		maxchans = 2048;
7058658b18bSSam Leffler 	dc = (struct ieee80211_devcaps_req *)
7068658b18bSSam Leffler 	    malloc(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP, M_NOWAIT | M_ZERO);
707b032f27cSSam Leffler 	if (dc == NULL)
708b032f27cSSam Leffler 		return ENOMEM;
709b032f27cSSam Leffler 	dc->dc_drivercaps = ic->ic_caps;
710b032f27cSSam Leffler 	dc->dc_cryptocaps = ic->ic_cryptocaps;
711b032f27cSSam Leffler 	dc->dc_htcaps = ic->ic_htcaps;
712b032f27cSSam Leffler 	ci = &dc->dc_chaninfo;
7138658b18bSSam Leffler 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
714f8b1ea17SSam Leffler 	KASSERT(ci->ic_nchans <= maxchans,
715f8b1ea17SSam Leffler 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
716b032f27cSSam Leffler 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
7178658b18bSSam Leffler 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
718e2126decSSam Leffler 	free(dc, M_TEMP);
719b032f27cSSam Leffler 	return error;
720b032f27cSSam Leffler }
721b032f27cSSam Leffler 
722b032f27cSSam Leffler static __noinline int
723b032f27cSSam Leffler ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
724b032f27cSSam Leffler {
725b032f27cSSam Leffler 	struct ieee80211_node *ni;
726b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
727b032f27cSSam Leffler 	int error;
728b032f27cSSam Leffler 
729b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
730b032f27cSSam Leffler 		return EINVAL;
731b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
732b032f27cSSam Leffler 	if (error != 0)
733b032f27cSSam Leffler 		return error;
734b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
735b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
736b032f27cSSam Leffler 		    vlan.sv_macaddr);
737b032f27cSSam Leffler 		if (ni == NULL)
738b032f27cSSam Leffler 			return ENOENT;
739b032f27cSSam Leffler 	} else
740b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
741b032f27cSSam Leffler 	vlan.sv_vlan = ni->ni_vlan;
742b032f27cSSam Leffler 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
743b032f27cSSam Leffler 	ieee80211_free_node(ni);
744b032f27cSSam Leffler 	return error;
74568e8e04eSSam Leffler }
74668e8e04eSSam Leffler 
74768e8e04eSSam Leffler /*
7488c4e758aSSam Leffler  * Dummy ioctl get handler so the linker set is defined.
7498c4e758aSSam Leffler  */
7508c4e758aSSam Leffler static int
7518c4e758aSSam Leffler dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
7528c4e758aSSam Leffler {
7538c4e758aSSam Leffler 	return ENOSYS;
7548c4e758aSSam Leffler }
7558c4e758aSSam Leffler IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
7568c4e758aSSam Leffler 
7578c4e758aSSam Leffler static int
7588c4e758aSSam Leffler ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
7598c4e758aSSam Leffler {
7608c4e758aSSam Leffler 	ieee80211_ioctl_getfunc * const *get;
7618c4e758aSSam Leffler 	int error;
7628c4e758aSSam Leffler 
7638c4e758aSSam Leffler 	SET_FOREACH(get, ieee80211_ioctl_getset) {
7648c4e758aSSam Leffler 		error = (*get)(vap, ireq);
7658c4e758aSSam Leffler 		if (error != ENOSYS)
7668c4e758aSSam Leffler 			return error;
7678c4e758aSSam Leffler 	}
7688c4e758aSSam Leffler 	return EINVAL;
7698c4e758aSSam Leffler }
7708c4e758aSSam Leffler 
7718c4e758aSSam Leffler /*
772c788ca3eSBill Paul  * When building the kernel with -O2 on the i386 architecture, gcc
773c788ca3eSBill Paul  * seems to want to inline this function into ieee80211_ioctl()
774c788ca3eSBill Paul  * (which is the only routine that calls it). When this happens,
775c788ca3eSBill Paul  * ieee80211_ioctl() ends up consuming an additional 2K of stack
776c788ca3eSBill Paul  * space. (Exactly why it needs so much is unclear.) The problem
777c788ca3eSBill Paul  * is that it's possible for ieee80211_ioctl() to invoke other
778c788ca3eSBill Paul  * routines (including driver init functions) which could then find
779c788ca3eSBill Paul  * themselves perilously close to exhausting the stack.
780c788ca3eSBill Paul  *
781c788ca3eSBill Paul  * To avoid this, we deliberately prevent gcc from inlining this
782c788ca3eSBill Paul  * routine. Another way to avoid this is to use less agressive
783c788ca3eSBill Paul  * optimization when compiling this file (i.e. -O instead of -O2)
784c788ca3eSBill Paul  * but special-casing the compilation of this one module in the
785c788ca3eSBill Paul  * build system would be awkward.
786c788ca3eSBill Paul  */
787b032f27cSSam Leffler static __noinline int
788b032f27cSSam Leffler ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
789b032f27cSSam Leffler     struct ieee80211req *ireq)
7908a1b9b6aSSam Leffler {
791b032f27cSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
792b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
793b032f27cSSam Leffler 	u_int kid, len;
79468e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
7951a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
796b032f27cSSam Leffler 	int error = 0;
7971a1e1d21SSam Leffler 
7981a1e1d21SSam Leffler 	switch (ireq->i_type) {
7991a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
800b032f27cSSam Leffler 		switch (vap->iv_state) {
8011a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
8021a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
803b032f27cSSam Leffler 			ireq->i_len = vap->iv_des_ssid[0].len;
804b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
8051a1e1d21SSam Leffler 			break;
8061a1e1d21SSam Leffler 		default:
807b032f27cSSam Leffler 			ireq->i_len = vap->iv_bss->ni_esslen;
808b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
8091a1e1d21SSam Leffler 			break;
8101a1e1d21SSam Leffler 		}
8111a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
8121a1e1d21SSam Leffler 		break;
8131a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
8141a1e1d21SSam Leffler 		ireq->i_val = 1;
8151a1e1d21SSam Leffler 		break;
8161a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
817b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
8188a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
819b032f27cSSam Leffler 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
8208a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
8218a1b9b6aSSam Leffler 		else
8228a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
8231a1e1d21SSam Leffler 		break;
8241a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
8251a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
8268a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8278a1b9b6aSSam Leffler 			return EINVAL;
828b032f27cSSam Leffler 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
8291a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
830acd3428bSRobert Watson 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
831b032f27cSSam Leffler 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
8321a1e1d21SSam Leffler 		} else {
8331a1e1d21SSam Leffler 			bzero(tmpkey, len);
8341a1e1d21SSam Leffler 		}
8351a1e1d21SSam Leffler 		ireq->i_len = len;
8361a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
8371a1e1d21SSam Leffler 		break;
8381a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
8391a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
8401a1e1d21SSam Leffler 		break;
8411a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
842b032f27cSSam Leffler 		ireq->i_val = vap->iv_def_txkey;
8431a1e1d21SSam Leffler 		break;
8441a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
845b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA)
8468a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
8478a1b9b6aSSam Leffler 		else
848b032f27cSSam Leffler 			ireq->i_val = vap->iv_bss->ni_authmode;
8491a1e1d21SSam Leffler 		break;
8501a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
851b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
8521a1e1d21SSam Leffler 		break;
8531a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
854b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PMGTON)
8551a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
8561a1e1d21SSam Leffler 		else
8571a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
8581a1e1d21SSam Leffler 		break;
8591a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
8601a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
8611a1e1d21SSam Leffler 		break;
86213604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
863b032f27cSSam Leffler 		ireq->i_val = vap->iv_rtsthreshold;
8641a1e1d21SSam Leffler 		break;
8652e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
8662e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
8672e79ca97SSam Leffler 		break;
8682e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
869b032f27cSSam Leffler 		/*
870b032f27cSSam Leffler 		 * Tx power limit is the min of max regulatory
871b032f27cSSam Leffler 		 * power, any user-set limit, and the max the
872b032f27cSSam Leffler 		 * radio can do.
873b032f27cSSam Leffler 		 */
874b032f27cSSam Leffler 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
875b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_txpowlimit)
8768a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_txpowlimit;
877b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
878b032f27cSSam Leffler 			ireq->i_val = ic->ic_curchan->ic_maxpower;
8798a1b9b6aSSam Leffler 		break;
8808a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
881b032f27cSSam Leffler 		switch (vap->iv_flags & IEEE80211_F_WPA) {
8828a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
8838a1b9b6aSSam Leffler 			ireq->i_val = 1;
8848a1b9b6aSSam Leffler 			break;
8858a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
8868a1b9b6aSSam Leffler 			ireq->i_val = 2;
8878a1b9b6aSSam Leffler 			break;
8888a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
8898a1b9b6aSSam Leffler 			ireq->i_val = 3;
8908a1b9b6aSSam Leffler 			break;
8918a1b9b6aSSam Leffler 		default:
8928a1b9b6aSSam Leffler 			ireq->i_val = 0;
8938a1b9b6aSSam Leffler 			break;
8948a1b9b6aSSam Leffler 		}
8958a1b9b6aSSam Leffler 		break;
8968a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
897b032f27cSSam Leffler 		error = ieee80211_ioctl_getchanlist(vap, ireq);
8988a1b9b6aSSam Leffler 		break;
8998a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
900b032f27cSSam Leffler 		ireq->i_val = vap->iv_roaming;
9018a1b9b6aSSam Leffler 		break;
9028a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
903b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
9048a1b9b6aSSam Leffler 		break;
9058a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
906b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
9078a1b9b6aSSam Leffler 		break;
9088a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
909b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
9108a1b9b6aSSam Leffler 		break;
9118a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
912b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
9138a1b9b6aSSam Leffler 		break;
9148a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
915b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
9168a1b9b6aSSam Leffler 		break;
9178a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
918b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
9198a1b9b6aSSam Leffler 		break;
9208a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
921b032f27cSSam Leffler 		error = ieee80211_ioctl_getkey(vap, ireq);
9228a1b9b6aSSam Leffler 		break;
9238a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
924b032f27cSSam Leffler 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
9258a1b9b6aSSam Leffler 		break;
9268a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
9278a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
9288a1b9b6aSSam Leffler 			return EINVAL;
9297cd89f64SSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN) {
9307cd89f64SSam Leffler 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
9317cd89f64SSam Leffler 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
9328a1b9b6aSSam Leffler 			    ireq->i_data, ireq->i_len);
9337cd89f64SSam Leffler 		} else
9347cd89f64SSam Leffler 			error = copyout(vap->iv_des_bssid, ireq->i_data,
9357cd89f64SSam Leffler 			    ireq->i_len);
9368a1b9b6aSSam Leffler 		break;
9378a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
938b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
93968e8e04eSSam Leffler 		break;
94068e8e04eSSam Leffler 	case IEEE80211_IOC_WPAIE2:
941b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
9428a1b9b6aSSam Leffler 		break;
9438a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
944b032f27cSSam Leffler 		error = ieee80211_ioctl_getscanresults(vap, ireq);
9458a1b9b6aSSam Leffler 		break;
9468a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
947b032f27cSSam Leffler 		error = ieee80211_ioctl_getstastats(vap, ireq);
9488a1b9b6aSSam Leffler 		break;
9498a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
950b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_txpower;
9518a1b9b6aSSam Leffler 		break;
9528a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
953b032f27cSSam Leffler 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
9548a1b9b6aSSam Leffler 		break;
9558a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
956b032f27cSSam Leffler 		error = ieee80211_ioctl_getstainfo(vap, ireq);
9578a1b9b6aSSam Leffler 		break;
9588a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
9598a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
9608a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
9618a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
9628a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
9638a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
964b032f27cSSam Leffler 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
9658a1b9b6aSSam Leffler 		break;
9668a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
967b032f27cSSam Leffler 		ireq->i_val = vap->iv_dtim_period;
9688a1b9b6aSSam Leffler 		break;
9698a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
9708a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
971b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_intval;
9722e79ca97SSam Leffler 		break;
973c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
974b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
975c4f040c3SSam Leffler 		break;
97632b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
97732b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet;
97832b0e64bSAdrian Chadd 		break;
97932b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
98032b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_count;
98132b0e64bSAdrian Chadd 		break;
98232b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
98332b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_period;
98432b0e64bSAdrian Chadd 		break;
98532b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
98632b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_duration;
98732b0e64bSAdrian Chadd 		break;
98832b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
98932b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_offset;
99032b0e64bSAdrian Chadd 		break;
99168e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
992b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
99368e8e04eSSam Leffler 		break;
99468e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
995b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
99668e8e04eSSam Leffler 		break;
99768e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
998b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
99968e8e04eSSam Leffler 		break;
100068e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
1001b032f27cSSam Leffler 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
100264353cb0SSam Leffler 		break;
100370231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
1004b032f27cSSam Leffler 		ireq->i_val = vap->iv_fragthreshold;
100570231e3dSSam Leffler 		break;
1006188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
1007b032f27cSSam Leffler 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
1008188757f5SSam Leffler 		break;
1009c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
1010b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
1011c27e4e31SSam Leffler 		break;
1012546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
1013b032f27cSSam Leffler 		ireq->i_val = vap->iv_bmissthreshold;
1014546786c9SSam Leffler 		break;
101568e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
1016b032f27cSSam Leffler 		error = ieee80211_ioctl_getcurchan(vap, ireq);
101768e8e04eSSam Leffler 		break;
101868e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
101968e8e04eSSam Leffler 		ireq->i_val = 0;
10202bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
102168e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
10222bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
102368e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
102468e8e04eSSam Leffler 		break;
102568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
102668e8e04eSSam Leffler 		ireq->i_val = 0;
10272bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
102868e8e04eSSam Leffler 			ireq->i_val |= 1;
10292bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
103068e8e04eSSam Leffler 			ireq->i_val |= 2;
103168e8e04eSSam Leffler 		break;
103268e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
1033b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1034b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_rxmax;
1035b032f27cSSam Leffler 		else if (vap->iv_state == IEEE80211_S_RUN)
1036b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1037b032f27cSSam Leffler 			    IEEE80211_HTCAP_MAXRXAMPDU);
1038b032f27cSSam Leffler 		else
1039b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_limit;
104068e8e04eSSam Leffler 		break;
104168e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
1042900de995SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1043900de995SSam Leffler 		    vap->iv_state == IEEE80211_S_RUN)
1044b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1045b032f27cSSam Leffler 			    IEEE80211_HTCAP_MPDUDENSITY);
1046b032f27cSSam Leffler 		else
1047b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_density;
104868e8e04eSSam Leffler 		break;
104968e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
105068e8e04eSSam Leffler 		ireq->i_val = 0;
10512bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
105268e8e04eSSam Leffler 			ireq->i_val |= 1;
10532bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
105468e8e04eSSam Leffler 			ireq->i_val |= 2;
105568e8e04eSSam Leffler 		break;
105668e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
1057b032f27cSSam Leffler 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
105868e8e04eSSam Leffler 		break;
105968e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
10602bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
106168e8e04eSSam Leffler 		break;
106268e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
1063b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1064b032f27cSSam Leffler 		break;
1065b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
1066b032f27cSSam Leffler 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1067b032f27cSSam Leffler 		break;
1068b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
1069b032f27cSSam Leffler 		error = ieee80211_ioctl_getroam(vap, ireq);
1070b032f27cSSam Leffler 		break;
1071b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
1072b032f27cSSam Leffler 		error = ieee80211_ioctl_gettxparams(vap, ireq);
107368e8e04eSSam Leffler 		break;
107468e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
10752bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1076b032f27cSSam Leffler 		break;
1077b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
1078b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
107968e8e04eSSam Leffler 		break;
1080c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
1081b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1082b032f27cSSam Leffler 		break;
1083b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
1084b032f27cSSam Leffler 		error = ieee80211_ioctl_getappie(vap, ireq);
1085b032f27cSSam Leffler 		break;
1086b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
1087b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1088b032f27cSSam Leffler 		break;
1089b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
1090b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1091b032f27cSSam Leffler 		break;
1092b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
1093b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1094b032f27cSSam Leffler 		break;
1095b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
1096b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1097b032f27cSSam Leffler 		break;
1098b032f27cSSam Leffler 	case IEEE80211_IOC_DEVCAPS:
1099b032f27cSSam Leffler 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1100c066143cSSam Leffler 		break;
11011b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
11021b6167d2SSam Leffler 		ireq->i_val = ic->ic_htprotmode;
11031b6167d2SSam Leffler 		break;
11041b6167d2SSam Leffler 	case IEEE80211_IOC_HTCONF:
11052bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
11061b6167d2SSam Leffler 			ireq->i_val = 1;
11072bfc8a91SSam Leffler 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
11081b6167d2SSam Leffler 				ireq->i_val |= 2;
11091b6167d2SSam Leffler 		} else
11101b6167d2SSam Leffler 			ireq->i_val = 0;
11111b6167d2SSam Leffler 		break;
1112b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
1113b032f27cSSam Leffler 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1114b032f27cSSam Leffler 		break;
11158c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
11168c070d69SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11178c070d69SSam Leffler 		    vap->iv_state == IEEE80211_S_RUN) {
11188c070d69SSam Leffler 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
11198c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
11208c070d69SSam Leffler 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
11218c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
11228c070d69SSam Leffler 			else
11238c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
11248c070d69SSam Leffler 		} else
11258c070d69SSam Leffler 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
11268c070d69SSam Leffler 		break;
112744f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
112844f7a6edSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
112944f7a6edSSam Leffler 		    vap->iv_state == IEEE80211_S_RUN)
113044f7a6edSSam Leffler 			ireq->i_val =
113144f7a6edSSam Leffler 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
113244f7a6edSSam Leffler 		else
113344f7a6edSSam Leffler 			ireq->i_val =
11342bfc8a91SSam Leffler 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
113544f7a6edSSam Leffler 		break;
11361a1e1d21SSam Leffler 	default:
11378c4e758aSSam Leffler 		error = ieee80211_ioctl_getdefault(vap, ireq);
1138b2e95691SSam Leffler 		break;
11391a1e1d21SSam Leffler 	}
11408a1b9b6aSSam Leffler 	return error;
1141b032f27cSSam Leffler #undef MS
11428a1b9b6aSSam Leffler }
11438a1b9b6aSSam Leffler 
1144b032f27cSSam Leffler static __noinline int
1145b032f27cSSam Leffler ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11468a1b9b6aSSam Leffler {
11478a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
11488a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11498a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
115068e8e04eSSam Leffler 	uint16_t kid;
1151b032f27cSSam Leffler 	int error, i;
11528a1b9b6aSSam Leffler 
11538a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
11548a1b9b6aSSam Leffler 		return EINVAL;
11558a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
11561a1e1d21SSam Leffler 	if (error)
11578a1b9b6aSSam Leffler 		return error;
11588a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
11598a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
11608a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
11618a1b9b6aSSam Leffler 		return E2BIG;
11628a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
11638a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
11648a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
11658a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
11668a1b9b6aSSam Leffler 			return EINVAL;
1167b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1168b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1169b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1170b5d4660fSSam Leffler 				ieee80211_free_node(ni);
11718a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1172b5d4660fSSam Leffler 			}
11738a1b9b6aSSam Leffler 		} else {
1174b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1175b032f27cSSam Leffler 				ik.ik_macaddr);
11768a1b9b6aSSam Leffler 			if (ni == NULL)
11778a1b9b6aSSam Leffler 				return ENOENT;
11788a1b9b6aSSam Leffler 		}
11798a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
11808a1b9b6aSSam Leffler 	} else {
11818a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
11828a1b9b6aSSam Leffler 			return EINVAL;
1183b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
1184386d84f6SSam Leffler 		/*
1185386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1186386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1187386d84f6SSam Leffler 		 */
1188386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1189386d84f6SSam Leffler 			wk->wk_keyix = kid;
11908a1b9b6aSSam Leffler 		ni = NULL;
11918a1b9b6aSSam Leffler 	}
11928a1b9b6aSSam Leffler 	error = 0;
1193b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
1194b032f27cSSam Leffler 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
11958a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
11968a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
11978a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
11988a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1199b032f27cSSam Leffler 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1200b032f27cSSam Leffler 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
12018a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
12028a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
12038a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
120471fe06caSSam Leffler 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
120571fe06caSSam Leffler 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
120671fe06caSSam Leffler 		if (!ieee80211_crypto_setkey(vap, wk))
12078a1b9b6aSSam Leffler 			error = EIO;
12088a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1209b032f27cSSam Leffler 			vap->iv_def_txkey = kid;
12108a1b9b6aSSam Leffler 	} else
12118a1b9b6aSSam Leffler 		error = ENXIO;
1212b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
12138a1b9b6aSSam Leffler 	if (ni != NULL)
12148a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12158a1b9b6aSSam Leffler 	return error;
12168a1b9b6aSSam Leffler }
12178a1b9b6aSSam Leffler 
1218b032f27cSSam Leffler static __noinline int
1219b032f27cSSam Leffler ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
12208a1b9b6aSSam Leffler {
12218a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
12228a1b9b6aSSam Leffler 	int kid, error;
12238a1b9b6aSSam Leffler 
12248a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
12258a1b9b6aSSam Leffler 		return EINVAL;
12268a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
12278a1b9b6aSSam Leffler 	if (error)
12288a1b9b6aSSam Leffler 		return error;
12298a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
123068e8e04eSSam Leffler 	/* XXX uint8_t -> uint16_t */
123168e8e04eSSam Leffler 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
12328a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
12338a1b9b6aSSam Leffler 
1234b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1235b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1236b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1237b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1238b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1239b5d4660fSSam Leffler 			}
1240b5d4660fSSam Leffler 		} else {
1241b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1242b032f27cSSam Leffler 				dk.idk_macaddr);
12438a1b9b6aSSam Leffler 			if (ni == NULL)
1244b5d4660fSSam Leffler 				return ENOENT;
1245b5d4660fSSam Leffler 		}
12468a1b9b6aSSam Leffler 		/* XXX error return */
1247c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
12488a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12498a1b9b6aSSam Leffler 	} else {
12508a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12518a1b9b6aSSam Leffler 			return EINVAL;
12528a1b9b6aSSam Leffler 		/* XXX error return */
1253b032f27cSSam Leffler 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
12548a1b9b6aSSam Leffler 	}
12558a1b9b6aSSam Leffler 	return 0;
12568a1b9b6aSSam Leffler }
12578a1b9b6aSSam Leffler 
1258b032f27cSSam Leffler struct mlmeop {
1259b032f27cSSam Leffler 	struct ieee80211vap *vap;
1260b032f27cSSam Leffler 	int	op;
1261b032f27cSSam Leffler 	int	reason;
1262b032f27cSSam Leffler };
1263b032f27cSSam Leffler 
1264b032f27cSSam Leffler static void
1265b032f27cSSam Leffler mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1266b032f27cSSam Leffler 	int op, int reason)
1267b032f27cSSam Leffler {
1268b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1269b032f27cSSam Leffler 	static const struct {
1270b032f27cSSam Leffler 		int mask;
1271b032f27cSSam Leffler 		const char *opstr;
1272b032f27cSSam Leffler 	} ops[] = {
1273b032f27cSSam Leffler 		{ 0, "op#0" },
1274b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1275b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "assoc" },
1276b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1277b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "disassoc" },
1278b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1279b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "deauth" },
1280b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1281b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "authorize" },
1282b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1283b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "unauthorize" },
1284b032f27cSSam Leffler 	};
1285b032f27cSSam Leffler 
1286b032f27cSSam Leffler 	if (op == IEEE80211_MLME_AUTH) {
1287b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1288b032f27cSSam Leffler 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1289b032f27cSSam Leffler 		    "station authenticate %s via MLME (reason %d)",
1290b032f27cSSam Leffler 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1291b032f27cSSam Leffler 		    reason);
1292b032f27cSSam Leffler 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1293b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1294b032f27cSSam Leffler 		    "unknown MLME request %d (reason %d)", op, reason);
1295b032f27cSSam Leffler 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1296b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1297b032f27cSSam Leffler 		    "station %s via MLME", ops[op].opstr);
1298b032f27cSSam Leffler 	} else {
1299b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1300b032f27cSSam Leffler 		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1301b032f27cSSam Leffler 	}
1302b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
1303b032f27cSSam Leffler }
1304b032f27cSSam Leffler 
13058a1b9b6aSSam Leffler static void
13068a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
13078a1b9b6aSSam Leffler {
1308b032f27cSSam Leffler 	struct mlmeop *mop = arg;
1309b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
13108a1b9b6aSSam Leffler 
1311b032f27cSSam Leffler 	if (vap != mop->vap)
1312b032f27cSSam Leffler 		return;
1313b032f27cSSam Leffler 	/*
1314b032f27cSSam Leffler 	 * NB: if ni_associd is zero then the node is already cleaned
1315b032f27cSSam Leffler 	 * up and we don't need to do this (we're safely holding a
1316b032f27cSSam Leffler 	 * reference but should otherwise not modify it's state).
1317b032f27cSSam Leffler 	 */
1318b032f27cSSam Leffler 	if (ni->ni_associd == 0)
1319b032f27cSSam Leffler 		return;
1320b032f27cSSam Leffler 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1321b032f27cSSam Leffler 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1322b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1323b032f27cSSam Leffler 		    mop->reason);
1324b032f27cSSam Leffler 	} else {
1325b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1326b032f27cSSam Leffler 		    mop->reason);
13278a1b9b6aSSam Leffler 	}
1328b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1329b032f27cSSam Leffler }
1330b032f27cSSam Leffler 
1331b032f27cSSam Leffler static int
1332b032f27cSSam Leffler setmlme_dropsta(struct ieee80211vap *vap,
1333b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1334b032f27cSSam Leffler {
1335b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1336b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1337b032f27cSSam Leffler 	struct ieee80211_node *ni;
1338b032f27cSSam Leffler 	int error = 0;
1339b032f27cSSam Leffler 
1340b032f27cSSam Leffler 	/* NB: the broadcast address means do 'em all */
1341b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1342b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1343b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, mac);
13449984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
13459984c9baSAdrian Chadd 		/*
13469984c9baSAdrian Chadd 		 * Don't do the node update inside the node
13479984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
13489984c9baSAdrian Chadd 		 * with drivers and their TX paths.
13499984c9baSAdrian Chadd 		 */
1350b032f27cSSam Leffler 		if (ni != NULL) {
1351b032f27cSSam Leffler 			domlme(mlmeop, ni);
1352b032f27cSSam Leffler 			ieee80211_free_node(ni);
1353b032f27cSSam Leffler 		} else
1354b032f27cSSam Leffler 			error = ENOENT;
1355b032f27cSSam Leffler 	} else {
1356b032f27cSSam Leffler 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1357b032f27cSSam Leffler 	}
1358b032f27cSSam Leffler 	return error;
1359b032f27cSSam Leffler }
1360b032f27cSSam Leffler 
1361b032f27cSSam Leffler static __noinline int
1362b032f27cSSam Leffler setmlme_common(struct ieee80211vap *vap, int op,
1363b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1364b032f27cSSam Leffler {
1365b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1366b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1367b032f27cSSam Leffler 	struct ieee80211_node *ni;
1368b032f27cSSam Leffler 	struct mlmeop mlmeop;
1369b032f27cSSam Leffler 	int error;
1370b032f27cSSam Leffler 
1371b032f27cSSam Leffler 	error = 0;
1372b032f27cSSam Leffler 	switch (op) {
1373b032f27cSSam Leffler 	case IEEE80211_MLME_DISASSOC:
1374b032f27cSSam Leffler 	case IEEE80211_MLME_DEAUTH:
1375b032f27cSSam Leffler 		switch (vap->iv_opmode) {
1376b032f27cSSam Leffler 		case IEEE80211_M_STA:
1377b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1378b032f27cSSam Leffler 			/* XXX not quite right */
1379b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1380b032f27cSSam Leffler 			break;
1381b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
1382b032f27cSSam Leffler 			mlmeop.vap = vap;
1383b032f27cSSam Leffler 			mlmeop.op = op;
1384b032f27cSSam Leffler 			mlmeop.reason = reason;
1385b032f27cSSam Leffler 			error = setmlme_dropsta(vap, mac, &mlmeop);
1386b032f27cSSam Leffler 			break;
1387b032f27cSSam Leffler 		case IEEE80211_M_WDS:
1388b032f27cSSam Leffler 			/* XXX user app should send raw frame? */
1389b032f27cSSam Leffler 			if (op != IEEE80211_MLME_DEAUTH) {
1390b032f27cSSam Leffler 				error = EINVAL;
1391b032f27cSSam Leffler 				break;
1392b032f27cSSam Leffler 			}
1393b032f27cSSam Leffler #if 0
1394b032f27cSSam Leffler 			/* XXX accept any address, simplifies user code */
1395b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1396b032f27cSSam Leffler 				error = EINVAL;
1397b032f27cSSam Leffler 				break;
1398b032f27cSSam Leffler 			}
1399b032f27cSSam Leffler #endif
1400b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1401b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1402b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
1403b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1404b032f27cSSam Leffler 			ieee80211_free_node(ni);
1405b032f27cSSam Leffler 			break;
1406136cb222SAdrian Chadd 		case IEEE80211_M_MBSS:
1407136cb222SAdrian Chadd 			IEEE80211_NODE_LOCK(nt);
1408136cb222SAdrian Chadd 			ni = ieee80211_find_node_locked(nt, mac);
14099984c9baSAdrian Chadd 			/*
14109984c9baSAdrian Chadd 			 * Don't do the node update inside the node
14119984c9baSAdrian Chadd 			 * table lock.  This unfortunately causes LORs
14129984c9baSAdrian Chadd 			 * with drivers and their TX paths.
14139984c9baSAdrian Chadd 			 */
14149984c9baSAdrian Chadd 			IEEE80211_NODE_UNLOCK(nt);
1415136cb222SAdrian Chadd 			if (ni != NULL) {
1416136cb222SAdrian Chadd 				ieee80211_node_leave(ni);
1417136cb222SAdrian Chadd 				ieee80211_free_node(ni);
1418136cb222SAdrian Chadd 			} else {
1419136cb222SAdrian Chadd 				error = ENOENT;
1420136cb222SAdrian Chadd 			}
1421136cb222SAdrian Chadd 			break;
1422b032f27cSSam Leffler 		default:
1423b032f27cSSam Leffler 			error = EINVAL;
1424b032f27cSSam Leffler 			break;
1425b032f27cSSam Leffler 		}
1426b032f27cSSam Leffler 		break;
1427b032f27cSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
1428b032f27cSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
1429b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1430b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_WDS) {
1431b032f27cSSam Leffler 			error = EINVAL;
1432b032f27cSSam Leffler 			break;
1433b032f27cSSam Leffler 		}
1434b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1435b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14369984c9baSAdrian Chadd 		/*
14379984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14389984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14399984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14409984c9baSAdrian Chadd 		 */
14419984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1442b032f27cSSam Leffler 		if (ni != NULL) {
1443b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1444b032f27cSSam Leffler 			if (op == IEEE80211_MLME_AUTHORIZE)
1445b032f27cSSam Leffler 				ieee80211_node_authorize(ni);
1446b032f27cSSam Leffler 			else
1447b032f27cSSam Leffler 				ieee80211_node_unauthorize(ni);
1448b032f27cSSam Leffler 			ieee80211_free_node(ni);
1449b032f27cSSam Leffler 		} else
1450b032f27cSSam Leffler 			error = ENOENT;
1451b032f27cSSam Leffler 		break;
1452b032f27cSSam Leffler 	case IEEE80211_MLME_AUTH:
1453b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1454b032f27cSSam Leffler 			error = EINVAL;
1455b032f27cSSam Leffler 			break;
1456b032f27cSSam Leffler 		}
1457b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1458b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14599984c9baSAdrian Chadd 		/*
14609984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14619984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14629984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14639984c9baSAdrian Chadd 		 */
14649984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1465b032f27cSSam Leffler 		if (ni != NULL) {
1466b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1467b032f27cSSam Leffler 			if (reason == IEEE80211_STATUS_SUCCESS) {
1468b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
1469b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1470b032f27cSSam Leffler 				/*
1471b032f27cSSam Leffler 				 * For shared key auth, just continue the
1472b032f27cSSam Leffler 				 * exchange.  Otherwise when 802.1x is not in
1473b032f27cSSam Leffler 				 * use mark the port authorized at this point
1474b032f27cSSam Leffler 				 * so traffic can flow.
1475b032f27cSSam Leffler 				 */
1476b032f27cSSam Leffler 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1477b032f27cSSam Leffler 				    ni->ni_challenge == NULL)
1478b032f27cSSam Leffler 				      ieee80211_node_authorize(ni);
1479b032f27cSSam Leffler 			} else {
1480b032f27cSSam Leffler 				vap->iv_stats.is_rx_acl++;
1481b032f27cSSam Leffler 				ieee80211_send_error(ni, ni->ni_macaddr,
1482b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1483b032f27cSSam Leffler 				ieee80211_node_leave(ni);
1484b032f27cSSam Leffler 			}
1485b032f27cSSam Leffler 			ieee80211_free_node(ni);
1486b032f27cSSam Leffler 		} else
1487b032f27cSSam Leffler 			error = ENOENT;
1488b032f27cSSam Leffler 		break;
1489b032f27cSSam Leffler 	default:
1490b032f27cSSam Leffler 		error = EINVAL;
1491b032f27cSSam Leffler 		break;
1492b032f27cSSam Leffler 	}
1493b032f27cSSam Leffler 	return error;
14948a1b9b6aSSam Leffler }
14958a1b9b6aSSam Leffler 
149668e8e04eSSam Leffler struct scanlookup {
149768e8e04eSSam Leffler 	const uint8_t *mac;
149868e8e04eSSam Leffler 	int esslen;
149968e8e04eSSam Leffler 	const uint8_t *essid;
150068e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se;
150168e8e04eSSam Leffler };
150268e8e04eSSam Leffler 
150368e8e04eSSam Leffler /*
150468e8e04eSSam Leffler  * Match mac address and any ssid.
150568e8e04eSSam Leffler  */
150668e8e04eSSam Leffler static void
150768e8e04eSSam Leffler mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
150868e8e04eSSam Leffler {
150968e8e04eSSam Leffler 	struct scanlookup *look = arg;
151068e8e04eSSam Leffler 
151168e8e04eSSam Leffler 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
151268e8e04eSSam Leffler 		return;
151368e8e04eSSam Leffler 	if (look->esslen != 0) {
151468e8e04eSSam Leffler 		if (se->se_ssid[1] != look->esslen)
151568e8e04eSSam Leffler 			return;
151668e8e04eSSam Leffler 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
151768e8e04eSSam Leffler 			return;
151868e8e04eSSam Leffler 	}
151968e8e04eSSam Leffler 	look->se = se;
152068e8e04eSSam Leffler }
152168e8e04eSSam Leffler 
1522b032f27cSSam Leffler static __noinline int
1523632ee7e3SBernhard Schmidt setmlme_assoc_sta(struct ieee80211vap *vap,
1524632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1525632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1526b032f27cSSam Leffler {
1527b032f27cSSam Leffler 	struct scanlookup lookup;
1528b032f27cSSam Leffler 
1529632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1530632ee7e3SBernhard Schmidt 	    ("expected opmode STA not %s",
1531632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1532b032f27cSSam Leffler 
1533b032f27cSSam Leffler 	/* NB: this is racey if roaming is !manual */
1534b032f27cSSam Leffler 	lookup.se = NULL;
1535b032f27cSSam Leffler 	lookup.mac = mac;
1536b032f27cSSam Leffler 	lookup.esslen = ssid_len;
1537b032f27cSSam Leffler 	lookup.essid = ssid;
1538b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1539b032f27cSSam Leffler 	if (lookup.se == NULL)
1540b032f27cSSam Leffler 		return ENOENT;
1541b032f27cSSam Leffler 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
154210959256SSam Leffler 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1543b032f27cSSam Leffler 		return EIO;		/* XXX unique but could be better */
1544b032f27cSSam Leffler 	return 0;
1545b032f27cSSam Leffler }
1546b032f27cSSam Leffler 
1547b032f27cSSam Leffler static __noinline int
1548632ee7e3SBernhard Schmidt setmlme_assoc_adhoc(struct ieee80211vap *vap,
1549632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1550632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1551632ee7e3SBernhard Schmidt {
1552632ee7e3SBernhard Schmidt 	struct ieee80211_scan_req sr;
1553632ee7e3SBernhard Schmidt 
1554632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1555632ee7e3SBernhard Schmidt 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1556632ee7e3SBernhard Schmidt 	    ("expected opmode IBSS or AHDEMO not %s",
1557632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1558632ee7e3SBernhard Schmidt 
1559632ee7e3SBernhard Schmidt 	if (ssid_len == 0)
1560632ee7e3SBernhard Schmidt 		return EINVAL;
1561632ee7e3SBernhard Schmidt 
1562632ee7e3SBernhard Schmidt 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1563632ee7e3SBernhard Schmidt 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1564632ee7e3SBernhard Schmidt 	vap->iv_des_ssid[0].len = ssid_len;
1565632ee7e3SBernhard Schmidt 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1566632ee7e3SBernhard Schmidt 	vap->iv_des_nssid = 1;
1567632ee7e3SBernhard Schmidt 
1568525065baSRui Paulo 	memset(&sr, 0, sizeof(sr));
1569632ee7e3SBernhard Schmidt 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
1570632ee7e3SBernhard Schmidt 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1571632ee7e3SBernhard Schmidt 	memcpy(sr.sr_ssid[0].ssid, ssid, ssid_len);
1572632ee7e3SBernhard Schmidt 	sr.sr_ssid[0].len = ssid_len;
1573632ee7e3SBernhard Schmidt 	sr.sr_nssid = 1;
1574632ee7e3SBernhard Schmidt 
1575632ee7e3SBernhard Schmidt 	return ieee80211_scanreq(vap, &sr);
1576632ee7e3SBernhard Schmidt }
1577632ee7e3SBernhard Schmidt 
1578632ee7e3SBernhard Schmidt static __noinline int
1579b032f27cSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
15808a1b9b6aSSam Leffler {
15818a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
15828a1b9b6aSSam Leffler 	int error;
15838a1b9b6aSSam Leffler 
15848a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
15858a1b9b6aSSam Leffler 		return EINVAL;
15868a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
15878a1b9b6aSSam Leffler 	if (error)
15888a1b9b6aSSam Leffler 		return error;
1589632ee7e3SBernhard Schmidt 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1590632ee7e3SBernhard Schmidt 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1591632ee7e3SBernhard Schmidt 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1592b032f27cSSam Leffler 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1593edd39a8eSRui Paulo 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1594edd39a8eSRui Paulo 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1595edd39a8eSRui Paulo 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1596632ee7e3SBernhard Schmidt 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1597632ee7e3SBernhard Schmidt 		    mlme.im_ssid_len, mlme.im_ssid);
15988a1b9b6aSSam Leffler 	else
1599b032f27cSSam Leffler 		return setmlme_common(vap, mlme.im_op,
1600b032f27cSSam Leffler 		    mlme.im_macaddr, mlme.im_reason);
16018a1b9b6aSSam Leffler }
16028a1b9b6aSSam Leffler 
1603b032f27cSSam Leffler static __noinline int
1604b032f27cSSam Leffler ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
16058a1b9b6aSSam Leffler {
160668e8e04eSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1607b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16088a1b9b6aSSam Leffler 	int error;
16098a1b9b6aSSam Leffler 
16108a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
16118a1b9b6aSSam Leffler 		return EINVAL;
16128a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
16138a1b9b6aSSam Leffler 	if (error)
16148a1b9b6aSSam Leffler 		return error;
16158a1b9b6aSSam Leffler 	if (acl == NULL) {
16168a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
1617b032f27cSSam Leffler 		if (acl == NULL || !acl->iac_attach(vap))
16188a1b9b6aSSam Leffler 			return EINVAL;
1619b032f27cSSam Leffler 		vap->iv_acl = acl;
16208a1b9b6aSSam Leffler 	}
16218a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1622b032f27cSSam Leffler 		acl->iac_add(vap, mac);
16238a1b9b6aSSam Leffler 	else
1624b032f27cSSam Leffler 		acl->iac_remove(vap, mac);
16258a1b9b6aSSam Leffler 	return 0;
16268a1b9b6aSSam Leffler }
16278a1b9b6aSSam Leffler 
1628b032f27cSSam Leffler static __noinline int
1629b032f27cSSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
16308a1b9b6aSSam Leffler {
1631b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16328a1b9b6aSSam Leffler 
16338a1b9b6aSSam Leffler 	switch (ireq->i_val) {
16348a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
16358a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
16368a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
1637b032f27cSSam Leffler 	case IEEE80211_MACCMD_POLICY_RADIUS:
16388a1b9b6aSSam Leffler 		if (acl == NULL) {
16398a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
1640b032f27cSSam Leffler 			if (acl == NULL || !acl->iac_attach(vap))
16418a1b9b6aSSam Leffler 				return EINVAL;
1642b032f27cSSam Leffler 			vap->iv_acl = acl;
16438a1b9b6aSSam Leffler 		}
1644b032f27cSSam Leffler 		acl->iac_setpolicy(vap, ireq->i_val);
16458a1b9b6aSSam Leffler 		break;
16468a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
16478a1b9b6aSSam Leffler 		if (acl != NULL)
1648b032f27cSSam Leffler 			acl->iac_flush(vap);
16498a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
16508a1b9b6aSSam Leffler 		break;
16518a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
16528a1b9b6aSSam Leffler 		if (acl != NULL) {
1653b032f27cSSam Leffler 			vap->iv_acl = NULL;
1654b032f27cSSam Leffler 			acl->iac_detach(vap);
16558a1b9b6aSSam Leffler 		}
16568a1b9b6aSSam Leffler 		break;
16578a1b9b6aSSam Leffler 	default:
1658188757f5SSam Leffler 		if (acl == NULL)
16598a1b9b6aSSam Leffler 			return EINVAL;
1660188757f5SSam Leffler 		else
1661b032f27cSSam Leffler 			return acl->iac_setioctl(vap, ireq);
16628a1b9b6aSSam Leffler 	}
16638a1b9b6aSSam Leffler 	return 0;
16648a1b9b6aSSam Leffler }
16658a1b9b6aSSam Leffler 
1666b032f27cSSam Leffler static __noinline int
1667b032f27cSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
16688a1b9b6aSSam Leffler {
1669b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
16708658b18bSSam Leffler 	uint8_t *chanlist, *list;
16718658b18bSSam Leffler 	int i, nchan, maxchan, error;
16728a1b9b6aSSam Leffler 
16738658b18bSSam Leffler 	if (ireq->i_len > sizeof(ic->ic_chan_active))
16748658b18bSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
16758658b18bSSam Leffler 	list = malloc(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
16768658b18bSSam Leffler 	    M_NOWAIT | M_ZERO);
16778658b18bSSam Leffler 	if (list == NULL)
16788658b18bSSam Leffler 		return ENOMEM;
16798658b18bSSam Leffler 	error = copyin(ireq->i_data, list, ireq->i_len);
16807b4d954cSRui Paulo 	if (error) {
16817b4d954cSRui Paulo 		free(list, M_TEMP);
16828a1b9b6aSSam Leffler 		return error;
16837b4d954cSRui Paulo 	}
168468e8e04eSSam Leffler 	nchan = 0;
16858658b18bSSam Leffler 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
16868658b18bSSam Leffler 	maxchan = ireq->i_len * NBBY;
168731378b1cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
168831378b1cSSam Leffler 		const struct ieee80211_channel *c = &ic->ic_channels[i];
16898a1b9b6aSSam Leffler 		/*
169031378b1cSSam Leffler 		 * Calculate the intersection of the user list and the
169131378b1cSSam Leffler 		 * available channels so users can do things like specify
169231378b1cSSam Leffler 		 * 1-255 to get all available channels.
16938a1b9b6aSSam Leffler 		 */
16948658b18bSSam Leffler 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
169531378b1cSSam Leffler 			setbit(chanlist, c->ic_ieee);
169668e8e04eSSam Leffler 			nchan++;
16978a1b9b6aSSam Leffler 		}
16988a1b9b6aSSam Leffler 	}
16997b4d954cSRui Paulo 	if (nchan == 0) {
17007b4d954cSRui Paulo 		free(list, M_TEMP);
170168e8e04eSSam Leffler 		return EINVAL;
17027b4d954cSRui Paulo 	}
170368e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
170468e8e04eSSam Leffler 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
170568e8e04eSSam Leffler 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
17068658b18bSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1707b032f27cSSam Leffler 	ieee80211_scan_flush(vap);
17088658b18bSSam Leffler 	free(list, M_TEMP);
1709b032f27cSSam Leffler 	return ENETRESET;
17108a1b9b6aSSam Leffler }
17118a1b9b6aSSam Leffler 
1712b032f27cSSam Leffler static __noinline int
1713b032f27cSSam Leffler ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
171442568791SSam Leffler {
171542568791SSam Leffler 	struct ieee80211_node *ni;
171668e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
171742568791SSam Leffler 	int error;
171842568791SSam Leffler 
171942568791SSam Leffler 	/*
172042568791SSam Leffler 	 * NB: we could copyin ieee80211req_sta_stats so apps
172142568791SSam Leffler 	 *     could make selective changes but that's overkill;
172242568791SSam Leffler 	 *     just clear all stats for now.
172342568791SSam Leffler 	 */
172442568791SSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
172542568791SSam Leffler 		return EINVAL;
172642568791SSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
172742568791SSam Leffler 	if (error != 0)
172842568791SSam Leffler 		return error;
1729b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
173042568791SSam Leffler 	if (ni == NULL)
1731b032f27cSSam Leffler 		return ENOENT;
1732b032f27cSSam Leffler 	/* XXX require ni_vap == vap? */
173342568791SSam Leffler 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
173442568791SSam Leffler 	ieee80211_free_node(ni);
173542568791SSam Leffler 	return 0;
173642568791SSam Leffler }
173742568791SSam Leffler 
1738b032f27cSSam Leffler static __noinline int
1739b032f27cSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
17408a1b9b6aSSam Leffler {
17418a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17428a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
17438a1b9b6aSSam Leffler 	int error;
17448a1b9b6aSSam Leffler 
17458a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
17468a1b9b6aSSam Leffler 		return EINVAL;
17478a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
17488a1b9b6aSSam Leffler 	if (error != 0)
17498a1b9b6aSSam Leffler 		return error;
1750b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
17518a1b9b6aSSam Leffler 	if (ni == NULL)
1752b032f27cSSam Leffler 		return ENOENT;
17538a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
17548a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
17558a1b9b6aSSam Leffler 	return error;
17568a1b9b6aSSam Leffler }
17578a1b9b6aSSam Leffler 
1758b032f27cSSam Leffler static __noinline int
1759b032f27cSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
17608a1b9b6aSSam Leffler {
1761b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17628a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
17638a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
17648a1b9b6aSSam Leffler 	int isbss, ac;
17658a1b9b6aSSam Leffler 
17668a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1767b032f27cSSam Leffler 		return EOPNOTSUPP;
17688a1b9b6aSSam Leffler 
17698a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
17708a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
17718a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
17728a1b9b6aSSam Leffler 		ac = WME_AC_BE;
17738a1b9b6aSSam Leffler 	if (isbss) {
17748a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
17758a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
17768a1b9b6aSSam Leffler 	} else {
17778a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
17788a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
17798a1b9b6aSSam Leffler 	}
17808a1b9b6aSSam Leffler 	switch (ireq->i_type) {
17818a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
17828a1b9b6aSSam Leffler 		if (isbss) {
17838a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = ireq->i_val;
17848a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
17858a1b9b6aSSam Leffler 				chanp->wmep_logcwmin = ireq->i_val;
17868a1b9b6aSSam Leffler 		} else {
17878a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
17888a1b9b6aSSam Leffler 				ireq->i_val;
17898a1b9b6aSSam Leffler 		}
17908a1b9b6aSSam Leffler 		break;
17918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
17928a1b9b6aSSam Leffler 		if (isbss) {
17938a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = ireq->i_val;
17948a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
17958a1b9b6aSSam Leffler 				chanp->wmep_logcwmax = ireq->i_val;
17968a1b9b6aSSam Leffler 		} else {
17978a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
17988a1b9b6aSSam Leffler 				ireq->i_val;
17998a1b9b6aSSam Leffler 		}
18008a1b9b6aSSam Leffler 		break;
18018a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18028a1b9b6aSSam Leffler 		if (isbss) {
18038a1b9b6aSSam Leffler 			wmep->wmep_aifsn = ireq->i_val;
18048a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18058a1b9b6aSSam Leffler 				chanp->wmep_aifsn = ireq->i_val;
18068a1b9b6aSSam Leffler 		} else {
18078a1b9b6aSSam Leffler 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
18088a1b9b6aSSam Leffler 		}
18098a1b9b6aSSam Leffler 		break;
18108a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18118a1b9b6aSSam Leffler 		if (isbss) {
18128a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = ireq->i_val;
18138a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18148a1b9b6aSSam Leffler 				chanp->wmep_txopLimit = ireq->i_val;
18158a1b9b6aSSam Leffler 		} else {
18168a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
18178a1b9b6aSSam Leffler 				ireq->i_val;
18188a1b9b6aSSam Leffler 		}
18198a1b9b6aSSam Leffler 		break;
18208a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
18218a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
18228a1b9b6aSSam Leffler 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
18238a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
18248a1b9b6aSSam Leffler 		break;
18258a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
18268a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
18278a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
18288a1b9b6aSSam Leffler 		break;
18298a1b9b6aSSam Leffler 	}
1830b032f27cSSam Leffler 	ieee80211_wme_updateparams(vap);
18318a1b9b6aSSam Leffler 	return 0;
18328a1b9b6aSSam Leffler }
18338a1b9b6aSSam Leffler 
18348a1b9b6aSSam Leffler static int
183568e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int start, int freq)
183668e8e04eSSam Leffler {
183768e8e04eSSam Leffler 	const struct ieee80211_channel *c;
183868e8e04eSSam Leffler 	int i;
183968e8e04eSSam Leffler 
184068e8e04eSSam Leffler 	for (i = start+1; i < ic->ic_nchans; i++) {
184168e8e04eSSam Leffler 		c = &ic->ic_channels[i];
184268e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
184368e8e04eSSam Leffler 			return 1;
184468e8e04eSSam Leffler 	}
184568e8e04eSSam Leffler 	/* NB: should not be needed but in case things are mis-sorted */
184668e8e04eSSam Leffler 	for (i = 0; i < start; i++) {
184768e8e04eSSam Leffler 		c = &ic->ic_channels[i];
184868e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
184968e8e04eSSam Leffler 			return 1;
185068e8e04eSSam Leffler 	}
185168e8e04eSSam Leffler 	return 0;
185268e8e04eSSam Leffler }
185368e8e04eSSam Leffler 
185468e8e04eSSam Leffler static struct ieee80211_channel *
185568e8e04eSSam Leffler findchannel(struct ieee80211com *ic, int ieee, int mode)
185668e8e04eSSam Leffler {
185768e8e04eSSam Leffler 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1858be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= 0,
1859be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1860be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1861be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1862be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1863be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1864be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1865be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
18666a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
18676a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
186868e8e04eSSam Leffler 	    /* NB: handled specially below */
1869be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1870be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
187168e8e04eSSam Leffler 	};
187268e8e04eSSam Leffler 	u_int modeflags;
187368e8e04eSSam Leffler 	int i;
187468e8e04eSSam Leffler 
187568e8e04eSSam Leffler 	modeflags = chanflags[mode];
187668e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
187768e8e04eSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
187868e8e04eSSam Leffler 
187968e8e04eSSam Leffler 		if (c->ic_ieee != ieee)
188068e8e04eSSam Leffler 			continue;
188168e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
188268e8e04eSSam Leffler 			/* ignore turbo channels for autoselect */
188368e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_TURBO(c))
188468e8e04eSSam Leffler 				continue;
188568e8e04eSSam Leffler 			/*
188668e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we
188768e8e04eSSam Leffler 			 *     always select the g channel if both
188868e8e04eSSam Leffler 			 *     are present.
188968e8e04eSSam Leffler 			 * XXX prefer HT to non-HT?
189068e8e04eSSam Leffler 			 */
189168e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_B(c) ||
189268e8e04eSSam Leffler 			    !find11gchannel(ic, i, c->ic_freq))
189368e8e04eSSam Leffler 				return c;
189468e8e04eSSam Leffler 		} else {
189568e8e04eSSam Leffler 			/* must check HT specially */
189668e8e04eSSam Leffler 			if ((mode == IEEE80211_MODE_11NA ||
189768e8e04eSSam Leffler 			    mode == IEEE80211_MODE_11NG) &&
189868e8e04eSSam Leffler 			    !IEEE80211_IS_CHAN_HT(c))
189968e8e04eSSam Leffler 				continue;
190068e8e04eSSam Leffler 			if ((c->ic_flags & modeflags) == modeflags)
190168e8e04eSSam Leffler 				return c;
190268e8e04eSSam Leffler 		}
190368e8e04eSSam Leffler 	}
190468e8e04eSSam Leffler 	return NULL;
190568e8e04eSSam Leffler }
190668e8e04eSSam Leffler 
190768e8e04eSSam Leffler /*
190868e8e04eSSam Leffler  * Check the specified against any desired mode (aka netband).
190968e8e04eSSam Leffler  * This is only used (presently) when operating in hostap mode
191068e8e04eSSam Leffler  * to enforce consistency.
191168e8e04eSSam Leffler  */
191268e8e04eSSam Leffler static int
191368e8e04eSSam Leffler check_mode_consistency(const struct ieee80211_channel *c, int mode)
191468e8e04eSSam Leffler {
191568e8e04eSSam Leffler 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
191668e8e04eSSam Leffler 
191768e8e04eSSam Leffler 	switch (mode) {
191868e8e04eSSam Leffler 	case IEEE80211_MODE_11B:
191968e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_B(c));
192068e8e04eSSam Leffler 	case IEEE80211_MODE_11G:
192168e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
192268e8e04eSSam Leffler 	case IEEE80211_MODE_11A:
192368e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
192468e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
192568e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_STURBO(c));
192668e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
192768e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTA(c));
192868e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
192968e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTG(c));
193068e8e04eSSam Leffler 	}
193168e8e04eSSam Leffler 	return 1;
193268e8e04eSSam Leffler 
193368e8e04eSSam Leffler }
193468e8e04eSSam Leffler 
193568e8e04eSSam Leffler /*
193668e8e04eSSam Leffler  * Common code to set the current channel.  If the device
193768e8e04eSSam Leffler  * is up and running this may result in an immediate channel
193868e8e04eSSam Leffler  * change or a kick of the state machine.
193968e8e04eSSam Leffler  */
194068e8e04eSSam Leffler static int
1941b032f27cSSam Leffler setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
194268e8e04eSSam Leffler {
1943b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
194468e8e04eSSam Leffler 	int error;
194568e8e04eSSam Leffler 
194668e8e04eSSam Leffler 	if (c != IEEE80211_CHAN_ANYC) {
1947b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1948b032f27cSSam Leffler 			return EBUSY;	/* XXX better code? */
1949b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1950b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
195168e8e04eSSam Leffler 				return EINVAL;
1952b032f27cSSam Leffler 			if (!check_mode_consistency(c, vap->iv_des_mode))
1953b032f27cSSam Leffler 				return EINVAL;
1954b032f27cSSam Leffler 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1955b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1956b032f27cSSam Leffler 				return EINVAL;
1957b032f27cSSam Leffler 		}
1958b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN &&
1959b032f27cSSam Leffler 		    vap->iv_bss->ni_chan == c)
196068e8e04eSSam Leffler 			return 0;	/* NB: nothing to do */
196168e8e04eSSam Leffler 	}
1962b032f27cSSam Leffler 	vap->iv_des_chan = c;
196368e8e04eSSam Leffler 
196468e8e04eSSam Leffler 	error = 0;
1965b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1966b032f27cSSam Leffler 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
196768e8e04eSSam Leffler 		/*
1968b032f27cSSam Leffler 		 * Monitor mode can switch directly.
196968e8e04eSSam Leffler 		 */
1970b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1971b032f27cSSam Leffler 			/* XXX need state machine for other vap's to follow */
1972b032f27cSSam Leffler 			ieee80211_setcurchan(ic, vap->iv_des_chan);
1973b032f27cSSam Leffler 			vap->iv_bss->ni_chan = ic->ic_curchan;
1974b032f27cSSam Leffler 		} else
1975b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
197626d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
197768e8e04eSSam Leffler 	} else {
197868e8e04eSSam Leffler 		/*
197968e8e04eSSam Leffler 		 * Need to go through the state machine in case we
198068e8e04eSSam Leffler 		 * need to reassociate or the like.  The state machine
198168e8e04eSSam Leffler 		 * will pickup the desired channel and avoid scanning.
198268e8e04eSSam Leffler 		 */
1983b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
1984b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1985b032f27cSSam Leffler 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
198668e8e04eSSam Leffler 			/*
198768e8e04eSSam Leffler 			 * When not up+running and a real channel has
198868e8e04eSSam Leffler 			 * been specified fix the current channel so
198968e8e04eSSam Leffler 			 * there is immediate feedback; e.g. via ifconfig.
199068e8e04eSSam Leffler 			 */
1991b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
199226d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
199368e8e04eSSam Leffler 		}
199468e8e04eSSam Leffler 	}
199568e8e04eSSam Leffler 	return error;
199668e8e04eSSam Leffler }
199768e8e04eSSam Leffler 
199868e8e04eSSam Leffler /*
199968e8e04eSSam Leffler  * Old api for setting the current channel; this is
200068e8e04eSSam Leffler  * deprecated because channel numbers are ambiguous.
200168e8e04eSSam Leffler  */
2002b032f27cSSam Leffler static __noinline int
2003b032f27cSSam Leffler ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
200468e8e04eSSam Leffler 	const struct ieee80211req *ireq)
200568e8e04eSSam Leffler {
2006b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
200768e8e04eSSam Leffler 	struct ieee80211_channel *c;
200868e8e04eSSam Leffler 
200968e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
201068e8e04eSSam Leffler 	if (ireq->i_val == 0 ||
201168e8e04eSSam Leffler 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
201268e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
201368e8e04eSSam Leffler 	} else {
201468e8e04eSSam Leffler 		struct ieee80211_channel *c2;
201568e8e04eSSam Leffler 
2016b032f27cSSam Leffler 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
201768e8e04eSSam Leffler 		if (c == NULL) {
201868e8e04eSSam Leffler 			c = findchannel(ic, ireq->i_val,
201968e8e04eSSam Leffler 				IEEE80211_MODE_AUTO);
202068e8e04eSSam Leffler 			if (c == NULL)
202168e8e04eSSam Leffler 				return EINVAL;
202268e8e04eSSam Leffler 		}
202368e8e04eSSam Leffler 		/*
202468e8e04eSSam Leffler 		 * Fine tune channel selection based on desired mode:
202568e8e04eSSam Leffler 		 *   if 11b is requested, find the 11b version of any
202668e8e04eSSam Leffler 		 *      11g channel returned,
202768e8e04eSSam Leffler 		 *   if static turbo, find the turbo version of any
202868e8e04eSSam Leffler 		 *	11a channel return,
202968e8e04eSSam Leffler 		 *   if 11na is requested, find the ht version of any
203068e8e04eSSam Leffler 		 *      11a channel returned,
203168e8e04eSSam Leffler 		 *   if 11ng is requested, find the ht version of any
203268e8e04eSSam Leffler 		 *      11g channel returned,
203368e8e04eSSam Leffler 		 *   otherwise we should be ok with what we've got.
203468e8e04eSSam Leffler 		 */
2035b032f27cSSam Leffler 		switch (vap->iv_des_mode) {
203668e8e04eSSam Leffler 		case IEEE80211_MODE_11B:
203768e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
203868e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
203968e8e04eSSam Leffler 					IEEE80211_MODE_11B);
204068e8e04eSSam Leffler 				/* NB: should not happen, =>'s 11g w/o 11b */
204168e8e04eSSam Leffler 				if (c2 != NULL)
204268e8e04eSSam Leffler 					c = c2;
204368e8e04eSSam Leffler 			}
204468e8e04eSSam Leffler 			break;
204568e8e04eSSam Leffler 		case IEEE80211_MODE_TURBO_A:
204668e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
204768e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
204868e8e04eSSam Leffler 					IEEE80211_MODE_TURBO_A);
204968e8e04eSSam Leffler 				if (c2 != NULL)
205068e8e04eSSam Leffler 					c = c2;
205168e8e04eSSam Leffler 			}
205268e8e04eSSam Leffler 			break;
205368e8e04eSSam Leffler 		case IEEE80211_MODE_11NA:
205468e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
205568e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
205668e8e04eSSam Leffler 					IEEE80211_MODE_11NA);
205768e8e04eSSam Leffler 				if (c2 != NULL)
205868e8e04eSSam Leffler 					c = c2;
205968e8e04eSSam Leffler 			}
206068e8e04eSSam Leffler 			break;
206168e8e04eSSam Leffler 		case IEEE80211_MODE_11NG:
206268e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
206368e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
206468e8e04eSSam Leffler 					IEEE80211_MODE_11NG);
206568e8e04eSSam Leffler 				if (c2 != NULL)
206668e8e04eSSam Leffler 					c = c2;
206768e8e04eSSam Leffler 			}
206868e8e04eSSam Leffler 			break;
206968e8e04eSSam Leffler 		default:		/* NB: no static turboG */
207068e8e04eSSam Leffler 			break;
207168e8e04eSSam Leffler 		}
207268e8e04eSSam Leffler 	}
2073b032f27cSSam Leffler 	return setcurchan(vap, c);
207468e8e04eSSam Leffler }
207568e8e04eSSam Leffler 
207668e8e04eSSam Leffler /*
207768e8e04eSSam Leffler  * New/current api for setting the current channel; a complete
207868e8e04eSSam Leffler  * channel description is provide so there is no ambiguity in
207968e8e04eSSam Leffler  * identifying the channel.
208068e8e04eSSam Leffler  */
2081b032f27cSSam Leffler static __noinline int
2082b032f27cSSam Leffler ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
208368e8e04eSSam Leffler 	const struct ieee80211req *ireq)
208468e8e04eSSam Leffler {
2085b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
208668e8e04eSSam Leffler 	struct ieee80211_channel chan, *c;
208768e8e04eSSam Leffler 	int error;
208868e8e04eSSam Leffler 
208968e8e04eSSam Leffler 	if (ireq->i_len != sizeof(chan))
209068e8e04eSSam Leffler 		return EINVAL;
209168e8e04eSSam Leffler 	error = copyin(ireq->i_data, &chan, sizeof(chan));
209268e8e04eSSam Leffler 	if (error != 0)
209368e8e04eSSam Leffler 		return error;
209468e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
209568e8e04eSSam Leffler 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
209668e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
209768e8e04eSSam Leffler 	} else {
209868e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
209968e8e04eSSam Leffler 		if (c == NULL)
210068e8e04eSSam Leffler 			return EINVAL;
210168e8e04eSSam Leffler 	}
2102b032f27cSSam Leffler 	return setcurchan(vap, c);
2103b032f27cSSam Leffler }
2104b032f27cSSam Leffler 
2105b032f27cSSam Leffler static __noinline int
2106b032f27cSSam Leffler ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2107b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2108b032f27cSSam Leffler {
2109b032f27cSSam Leffler 	struct ieee80211_regdomain_req *reg;
21108658b18bSSam Leffler 	int nchans, error;
2111b032f27cSSam Leffler 
21128658b18bSSam Leffler 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
21138658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
21148658b18bSSam Leffler 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
21158658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21168658b18bSSam Leffler 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
21178658b18bSSam Leffler 		    ireq->i_len, nchans);
2118b032f27cSSam Leffler 		return EINVAL;
21198658b18bSSam Leffler 	}
21208658b18bSSam Leffler 	reg = (struct ieee80211_regdomain_req *)
21218658b18bSSam Leffler 	    malloc(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, M_NOWAIT);
21228658b18bSSam Leffler 	if (reg == NULL) {
21238658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21248658b18bSSam Leffler 		    "%s: no memory, nchans %d\n", __func__, nchans);
2125b032f27cSSam Leffler 		return ENOMEM;
21268658b18bSSam Leffler 	}
21278658b18bSSam Leffler 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
21288658b18bSSam Leffler 	if (error == 0) {
21298658b18bSSam Leffler 		/* NB: validate inline channel count against storage size */
21308658b18bSSam Leffler 		if (reg->chaninfo.ic_nchans != nchans) {
21318658b18bSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21328658b18bSSam Leffler 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
21338658b18bSSam Leffler 				reg->chaninfo.ic_nchans, nchans);
21348658b18bSSam Leffler 			error = EINVAL;
21358658b18bSSam Leffler 		} else
2136b032f27cSSam Leffler 			error = ieee80211_setregdomain(vap, reg);
21378658b18bSSam Leffler 	}
2138e2126decSSam Leffler 	free(reg, M_TEMP);
2139b032f27cSSam Leffler 
2140b032f27cSSam Leffler 	return (error == 0 ? ENETRESET : error);
214168e8e04eSSam Leffler }
214268e8e04eSSam Leffler 
214368e8e04eSSam Leffler static int
2144b032f27cSSam Leffler ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2145b032f27cSSam Leffler 	const struct ieee80211req *ireq)
21468a1b9b6aSSam Leffler {
2147b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2148b032f27cSSam Leffler 		return EINVAL;
2149b032f27cSSam Leffler 	/* XXX validate params */
2150b032f27cSSam Leffler 	/* XXX? ENETRESET to push to device? */
2151b032f27cSSam Leffler 	return copyin(ireq->i_data, vap->iv_roamparms,
2152b032f27cSSam Leffler 	    sizeof(vap->iv_roamparms));
2153b032f27cSSam Leffler }
2154b032f27cSSam Leffler 
2155b032f27cSSam Leffler static int
2156b032f27cSSam Leffler checkrate(const struct ieee80211_rateset *rs, int rate)
2157b032f27cSSam Leffler {
2158b032f27cSSam Leffler 	int i;
2159b032f27cSSam Leffler 
2160b032f27cSSam Leffler 	if (rate == IEEE80211_FIXED_RATE_NONE)
2161b032f27cSSam Leffler 		return 1;
2162b032f27cSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
2163b032f27cSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2164b032f27cSSam Leffler 			return 1;
2165b032f27cSSam Leffler 	return 0;
2166b032f27cSSam Leffler }
2167b032f27cSSam Leffler 
2168b032f27cSSam Leffler static int
2169b032f27cSSam Leffler checkmcs(int mcs)
2170b032f27cSSam Leffler {
2171b032f27cSSam Leffler 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2172b032f27cSSam Leffler 		return 1;
2173b032f27cSSam Leffler 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2174b032f27cSSam Leffler 		return 0;
2175b032f27cSSam Leffler 	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2176b032f27cSSam Leffler }
2177b032f27cSSam Leffler 
2178b032f27cSSam Leffler static __noinline int
2179b032f27cSSam Leffler ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2180b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2181b032f27cSSam Leffler {
2182b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2183b032f27cSSam Leffler 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2184b032f27cSSam Leffler 	struct ieee80211_txparam *src, *dst;
2185b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2186cbb24c24SSam Leffler 	int error, mode, changed, is11n, nmodes;
2187b032f27cSSam Leffler 
2188cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
2189cbb24c24SSam Leffler 	if (ireq->i_len > sizeof(parms))
2190b032f27cSSam Leffler 		return EINVAL;
2191cbb24c24SSam Leffler 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2192b032f27cSSam Leffler 	if (error != 0)
2193b032f27cSSam Leffler 		return error;
2194cbb24c24SSam Leffler 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2195b032f27cSSam Leffler 	changed = 0;
2196b032f27cSSam Leffler 	/* validate parameters and check if anything changed */
2197cbb24c24SSam Leffler 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2198cbb24c24SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
2199b032f27cSSam Leffler 			continue;
2200cbb24c24SSam Leffler 		src = &parms.params[mode];
2201cbb24c24SSam Leffler 		dst = &vap->iv_txparms[mode];
2202cbb24c24SSam Leffler 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2203cbb24c24SSam Leffler 		is11n = (mode == IEEE80211_MODE_11NA ||
2204cbb24c24SSam Leffler 			 mode == IEEE80211_MODE_11NG);
2205b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2206cbb24c24SSam Leffler 			if (!checkrate(rs, src->ucastrate) &&
2207cbb24c24SSam Leffler 			    (!is11n || !checkmcs(src->ucastrate)))
2208b032f27cSSam Leffler 				return EINVAL;
2209b032f27cSSam Leffler 			changed++;
2210b032f27cSSam Leffler 		}
2211b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2212cbb24c24SSam Leffler 			if (!checkrate(rs, src->mcastrate) &&
2213cbb24c24SSam Leffler 			    (!is11n || !checkmcs(src->mcastrate)))
2214b032f27cSSam Leffler 				return EINVAL;
2215b032f27cSSam Leffler 			changed++;
2216b032f27cSSam Leffler 		}
2217b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2218cbb24c24SSam Leffler 			if (!checkrate(rs, src->mgmtrate) &&
2219cbb24c24SSam Leffler 			    (!is11n || !checkmcs(src->mgmtrate)))
2220b032f27cSSam Leffler 				return EINVAL;
2221b032f27cSSam Leffler 			changed++;
2222b032f27cSSam Leffler 		}
2223b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2224b032f27cSSam Leffler 			changed++;
2225b032f27cSSam Leffler 	}
2226b032f27cSSam Leffler 	if (changed) {
2227b032f27cSSam Leffler 		/*
2228b032f27cSSam Leffler 		 * Copy new parameters in place and notify the
2229b032f27cSSam Leffler 		 * driver so it can push state to the device.
2230b032f27cSSam Leffler 		 */
2231cbb24c24SSam Leffler 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2232cbb24c24SSam Leffler 			if (isset(ic->ic_modecaps, mode))
2233cbb24c24SSam Leffler 				vap->iv_txparms[mode] = parms.params[mode];
2234b032f27cSSam Leffler 		}
2235b032f27cSSam Leffler 		/* XXX could be more intelligent,
2236b032f27cSSam Leffler 		   e.g. don't reset if setting not being used */
2237b032f27cSSam Leffler 		return ENETRESET;
2238b032f27cSSam Leffler 	}
2239b032f27cSSam Leffler 	return 0;
2240b032f27cSSam Leffler }
2241b032f27cSSam Leffler 
2242b032f27cSSam Leffler /*
2243b032f27cSSam Leffler  * Application Information Element support.
2244b032f27cSSam Leffler  */
2245b032f27cSSam Leffler static int
2246b032f27cSSam Leffler setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2247b032f27cSSam Leffler {
2248b032f27cSSam Leffler 	struct ieee80211_appie *app = *aie;
2249b032f27cSSam Leffler 	struct ieee80211_appie *napp;
2250b032f27cSSam Leffler 	int error;
2251b032f27cSSam Leffler 
2252b032f27cSSam Leffler 	if (ireq->i_len == 0) {		/* delete any existing ie */
2253b032f27cSSam Leffler 		if (app != NULL) {
2254b032f27cSSam Leffler 			*aie = NULL;	/* XXX racey */
2255e2126decSSam Leffler 			free(app, M_80211_NODE_IE);
2256b032f27cSSam Leffler 		}
2257b032f27cSSam Leffler 		return 0;
2258b032f27cSSam Leffler 	}
2259b032f27cSSam Leffler 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2260b032f27cSSam Leffler 		return EINVAL;
2261b032f27cSSam Leffler 	/*
2262b032f27cSSam Leffler 	 * Allocate a new appie structure and copy in the user data.
2263b032f27cSSam Leffler 	 * When done swap in the new structure.  Note that we do not
2264b032f27cSSam Leffler 	 * guard against users holding a ref to the old structure;
2265b032f27cSSam Leffler 	 * this must be handled outside this code.
2266b032f27cSSam Leffler 	 *
2267b032f27cSSam Leffler 	 * XXX bad bad bad
2268b032f27cSSam Leffler 	 */
2269e2126decSSam Leffler 	napp = (struct ieee80211_appie *) malloc(
2270c5abbba3SDag-Erling Smørgrav 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2271b032f27cSSam Leffler 	if (napp == NULL)
2272b032f27cSSam Leffler 		return ENOMEM;
2273b032f27cSSam Leffler 	/* XXX holding ic lock */
2274b032f27cSSam Leffler 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2275b032f27cSSam Leffler 	if (error) {
2276e2126decSSam Leffler 		free(napp, M_80211_NODE_IE);
2277b032f27cSSam Leffler 		return error;
2278b032f27cSSam Leffler 	}
2279b032f27cSSam Leffler 	napp->ie_len = ireq->i_len;
2280b032f27cSSam Leffler 	*aie = napp;
2281b032f27cSSam Leffler 	if (app != NULL)
2282e2126decSSam Leffler 		free(app, M_80211_NODE_IE);
2283b032f27cSSam Leffler 	return 0;
2284b032f27cSSam Leffler }
2285b032f27cSSam Leffler 
2286b032f27cSSam Leffler static void
2287b032f27cSSam Leffler setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2288b032f27cSSam Leffler {
2289b032f27cSSam Leffler 	/* validate data is present as best we can */
2290b032f27cSSam Leffler 	if (space == 0 || 2+ie[1] > space)
2291b032f27cSSam Leffler 		return;
2292b032f27cSSam Leffler 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2293b032f27cSSam Leffler 		vap->iv_wpa_ie = ie;
2294b032f27cSSam Leffler 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2295b032f27cSSam Leffler 		vap->iv_rsn_ie = ie;
2296b032f27cSSam Leffler }
2297b032f27cSSam Leffler 
2298b032f27cSSam Leffler static __noinline int
2299b032f27cSSam Leffler ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2300b032f27cSSam Leffler 	const struct ieee80211req *ireq, int fc0)
2301b032f27cSSam Leffler {
2302b032f27cSSam Leffler 	int error;
2303b032f27cSSam Leffler 
2304b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2305b032f27cSSam Leffler 
2306b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2307b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2308b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2309b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2310b032f27cSSam Leffler 			error = EINVAL;
2311b032f27cSSam Leffler 			break;
2312b032f27cSSam Leffler 		}
2313b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_beacon, ireq);
2314b032f27cSSam Leffler 		if (error == 0)
2315b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2316b032f27cSSam Leffler 		break;
2317b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2318b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_proberesp, ireq);
2319b032f27cSSam Leffler 		break;
2320b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2321b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2322b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocresp, ireq);
2323b032f27cSSam Leffler 		else
2324b032f27cSSam Leffler 			error = EINVAL;
2325b032f27cSSam Leffler 		break;
2326b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2327b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_probereq, ireq);
2328b032f27cSSam Leffler 		break;
2329b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2330b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
2331b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocreq, ireq);
2332b032f27cSSam Leffler 		else
2333b032f27cSSam Leffler 			error = EINVAL;
2334b032f27cSSam Leffler 		break;
2335b032f27cSSam Leffler 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2336b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_wpa, ireq);
2337b032f27cSSam Leffler 		if (error == 0) {
2338b032f27cSSam Leffler 			/*
2339b032f27cSSam Leffler 			 * Must split single blob of data into separate
2340b032f27cSSam Leffler 			 * WPA and RSN ie's because they go in different
2341b032f27cSSam Leffler 			 * locations in the mgt frames.
2342b032f27cSSam Leffler 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2343b032f27cSSam Leffler 			 */
2344b032f27cSSam Leffler 			vap->iv_wpa_ie = NULL;
2345b032f27cSSam Leffler 			vap->iv_rsn_ie = NULL;
2346b032f27cSSam Leffler 			if (vap->iv_appie_wpa != NULL) {
2347b032f27cSSam Leffler 				struct ieee80211_appie *appie =
2348b032f27cSSam Leffler 				    vap->iv_appie_wpa;
2349b032f27cSSam Leffler 				uint8_t *data = appie->ie_data;
2350b032f27cSSam Leffler 
2351b032f27cSSam Leffler 				/* XXX ie length validate is painful, cheat */
2352b032f27cSSam Leffler 				setwparsnie(vap, data, appie->ie_len);
2353b032f27cSSam Leffler 				setwparsnie(vap, data + 2 + data[1],
2354b032f27cSSam Leffler 				    appie->ie_len - (2 + data[1]));
2355b032f27cSSam Leffler 			}
2356b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2357b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2358b032f27cSSam Leffler 				/*
2359b032f27cSSam Leffler 				 * Must rebuild beacon frame as the update
2360b032f27cSSam Leffler 				 * mechanism doesn't handle WPA/RSN ie's.
2361b032f27cSSam Leffler 				 * Could extend it but it doesn't normally
2362b032f27cSSam Leffler 				 * change; this is just to deal with hostapd
2363b032f27cSSam Leffler 				 * plumbing the ie after the interface is up.
2364b032f27cSSam Leffler 				 */
2365b032f27cSSam Leffler 				error = ENETRESET;
2366b032f27cSSam Leffler 			}
2367b032f27cSSam Leffler 		}
2368b032f27cSSam Leffler 		break;
2369b032f27cSSam Leffler 	default:
2370b032f27cSSam Leffler 		error = EINVAL;
2371b032f27cSSam Leffler 		break;
2372b032f27cSSam Leffler 	}
2373b032f27cSSam Leffler 	return error;
2374b032f27cSSam Leffler }
2375b032f27cSSam Leffler 
2376b032f27cSSam Leffler static __noinline int
2377b032f27cSSam Leffler ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2378b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2379b032f27cSSam Leffler {
2380b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2381b032f27cSSam Leffler 	int error;
2382b032f27cSSam Leffler 	uint8_t fc0;
2383b032f27cSSam Leffler 
2384b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
2385b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2386b032f27cSSam Leffler 		return EINVAL;
2387b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2388b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2389b032f27cSSam Leffler 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2390b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2391b032f27cSSam Leffler 	return error;
2392b032f27cSSam Leffler }
2393b032f27cSSam Leffler 
2394b032f27cSSam Leffler static __noinline int
2395b032f27cSSam Leffler ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2396b032f27cSSam Leffler {
2397b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2398b032f27cSSam Leffler 	struct ieee80211_chanswitch_req csr;
2399b032f27cSSam Leffler 	struct ieee80211_channel *c;
2400b032f27cSSam Leffler 	int error;
2401b032f27cSSam Leffler 
2402b032f27cSSam Leffler 	if (ireq->i_len != sizeof(csr))
2403b032f27cSSam Leffler 		return EINVAL;
2404b032f27cSSam Leffler 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2405b032f27cSSam Leffler 	if (error != 0)
2406b032f27cSSam Leffler 		return error;
2407c70761e6SSam Leffler 	/* XXX adhoc mode not supported */
2408c70761e6SSam Leffler 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2409c70761e6SSam Leffler 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2410c70761e6SSam Leffler 		return EOPNOTSUPP;
2411b032f27cSSam Leffler 	c = ieee80211_find_channel(ic,
2412b032f27cSSam Leffler 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2413b032f27cSSam Leffler 	if (c == NULL)
2414b032f27cSSam Leffler 		return ENOENT;
2415b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2416b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2417b032f27cSSam Leffler 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2418c70761e6SSam Leffler 	else if (csr.csa_count == 0)
2419c70761e6SSam Leffler 		ieee80211_csa_cancelswitch(ic);
2420b032f27cSSam Leffler 	else
2421b032f27cSSam Leffler 		error = EBUSY;
2422b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2423b032f27cSSam Leffler 	return error;
2424b032f27cSSam Leffler }
2425b032f27cSSam Leffler 
2426632ee7e3SBernhard Schmidt static int
2427632ee7e3SBernhard Schmidt ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2428b032f27cSSam Leffler {
2429b032f27cSSam Leffler #define	IEEE80211_IOC_SCAN_FLAGS \
2430b032f27cSSam Leffler 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2431b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2432b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2433b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2434b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_CHECK)
2435b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
24367ca7a42cSBernhard Schmidt 	int error, i;
2437b032f27cSSam Leffler 
2438b032f27cSSam Leffler 	/* convert duration */
2439632ee7e3SBernhard Schmidt 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2440632ee7e3SBernhard Schmidt 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2441b032f27cSSam Leffler 	else {
2442632ee7e3SBernhard Schmidt 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2443632ee7e3SBernhard Schmidt 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2444b032f27cSSam Leffler 			return EINVAL;
2445632ee7e3SBernhard Schmidt 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2446632ee7e3SBernhard Schmidt 		if (sr->sr_duration < 1)
2447632ee7e3SBernhard Schmidt 			sr->sr_duration = 1;
2448b032f27cSSam Leffler 	}
2449b032f27cSSam Leffler 	/* convert min/max channel dwell */
2450632ee7e3SBernhard Schmidt 	if (sr->sr_mindwell != 0) {
2451632ee7e3SBernhard Schmidt 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2452632ee7e3SBernhard Schmidt 		if (sr->sr_mindwell < 1)
2453632ee7e3SBernhard Schmidt 			sr->sr_mindwell = 1;
2454b032f27cSSam Leffler 	}
2455632ee7e3SBernhard Schmidt 	if (sr->sr_maxdwell != 0) {
2456632ee7e3SBernhard Schmidt 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2457632ee7e3SBernhard Schmidt 		if (sr->sr_maxdwell < 1)
2458632ee7e3SBernhard Schmidt 			sr->sr_maxdwell = 1;
2459b032f27cSSam Leffler 	}
2460b032f27cSSam Leffler 	/* NB: silently reduce ssid count to what is supported */
2461632ee7e3SBernhard Schmidt 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2462632ee7e3SBernhard Schmidt 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2463632ee7e3SBernhard Schmidt 	for (i = 0; i < sr->sr_nssid; i++)
2464632ee7e3SBernhard Schmidt 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2465b032f27cSSam Leffler 			return EINVAL;
2466b032f27cSSam Leffler 	/* cleanse flags just in case, could reject if invalid flags */
2467632ee7e3SBernhard Schmidt 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2468b032f27cSSam Leffler 	/*
2469b032f27cSSam Leffler 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2470b032f27cSSam Leffler 	 * allows applications to scan without joining a bss (or picking
2471b032f27cSSam Leffler 	 * a channel and setting up a bss) and without forcing manual
2472b032f27cSSam Leffler 	 * roaming mode--you just need to mark the parent device UP.
2473b032f27cSSam Leffler 	 */
2474b032f27cSSam Leffler 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2475632ee7e3SBernhard Schmidt 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2476b032f27cSSam Leffler 
2477b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2478b032f27cSSam Leffler 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2479632ee7e3SBernhard Schmidt 	    __func__, sr->sr_flags,
2480b032f27cSSam Leffler 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2481632ee7e3SBernhard Schmidt 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2482b032f27cSSam Leffler 	/*
2483b032f27cSSam Leffler 	 * If we are in INIT state then the driver has never had a chance
2484b032f27cSSam Leffler 	 * to setup hardware state to do a scan; we must use the state
2485b032f27cSSam Leffler 	 * machine to get us up to the SCAN state but once we reach SCAN
2486b032f27cSSam Leffler 	 * state we then want to use the supplied params.  Stash the
2487b032f27cSSam Leffler 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2488b032f27cSSam Leffler 	 * state machines will recognize this and use the stashed params
2489b032f27cSSam Leffler 	 * to issue the scan request.
2490b032f27cSSam Leffler 	 *
2491b032f27cSSam Leffler 	 * Otherwise just invoke the scan machinery directly.
2492b032f27cSSam Leffler 	 */
2493b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2494b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_INIT) {
2495b032f27cSSam Leffler 		/* NB: clobbers previous settings */
2496632ee7e3SBernhard Schmidt 		vap->iv_scanreq_flags = sr->sr_flags;
2497632ee7e3SBernhard Schmidt 		vap->iv_scanreq_duration = sr->sr_duration;
2498632ee7e3SBernhard Schmidt 		vap->iv_scanreq_nssid = sr->sr_nssid;
2499632ee7e3SBernhard Schmidt 		for (i = 0; i < sr->sr_nssid; i++) {
2500632ee7e3SBernhard Schmidt 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2501632ee7e3SBernhard Schmidt 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2502632ee7e3SBernhard Schmidt 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2503b032f27cSSam Leffler 		}
2504b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2505b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2506b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2507b032f27cSSam Leffler 	} else {
2508b032f27cSSam Leffler 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2509b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2510632ee7e3SBernhard Schmidt 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
25117ca7a42cSBernhard Schmidt 			error = ieee80211_check_scan(vap, sr->sr_flags,
2512632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2513632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2514b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2515632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2516b032f27cSSam Leffler 		} else {
25177ca7a42cSBernhard Schmidt 			error = ieee80211_start_scan(vap, sr->sr_flags,
2518632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2519632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2520b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2521632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2522b032f27cSSam Leffler 		}
25237ca7a42cSBernhard Schmidt 		if (error == 0)
25247ca7a42cSBernhard Schmidt 			return EINPROGRESS;
2525b032f27cSSam Leffler 	}
2526632ee7e3SBernhard Schmidt 	return 0;
2527b032f27cSSam Leffler #undef IEEE80211_IOC_SCAN_FLAGS
2528b032f27cSSam Leffler }
2529b032f27cSSam Leffler 
2530b032f27cSSam Leffler static __noinline int
2531632ee7e3SBernhard Schmidt ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2532632ee7e3SBernhard Schmidt {
2533632ee7e3SBernhard Schmidt 	struct ieee80211com *ic = vap->iv_ic;
2534632ee7e3SBernhard Schmidt 	struct ieee80211_scan_req sr;		/* XXX off stack? */
2535632ee7e3SBernhard Schmidt 	int error;
2536632ee7e3SBernhard Schmidt 
2537632ee7e3SBernhard Schmidt 	/* NB: parent must be running */
2538632ee7e3SBernhard Schmidt 	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2539632ee7e3SBernhard Schmidt 		return ENXIO;
2540632ee7e3SBernhard Schmidt 
2541632ee7e3SBernhard Schmidt 	if (ireq->i_len != sizeof(sr))
2542632ee7e3SBernhard Schmidt 		return EINVAL;
2543632ee7e3SBernhard Schmidt 	error = copyin(ireq->i_data, &sr, sizeof(sr));
2544632ee7e3SBernhard Schmidt 	if (error != 0)
2545632ee7e3SBernhard Schmidt 		return error;
2546632ee7e3SBernhard Schmidt 	return ieee80211_scanreq(vap, &sr);
2547632ee7e3SBernhard Schmidt }
2548632ee7e3SBernhard Schmidt 
2549632ee7e3SBernhard Schmidt static __noinline int
2550b032f27cSSam Leffler ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2551b032f27cSSam Leffler {
2552b032f27cSSam Leffler 	struct ieee80211_node *ni;
2553b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
2554b032f27cSSam Leffler 	int error;
2555b032f27cSSam Leffler 
2556b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
2557b032f27cSSam Leffler 		return EINVAL;
2558b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2559b032f27cSSam Leffler 	if (error != 0)
2560b032f27cSSam Leffler 		return error;
2561b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2562b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2563b032f27cSSam Leffler 		    vlan.sv_macaddr);
2564b032f27cSSam Leffler 		if (ni == NULL)
2565b032f27cSSam Leffler 			return ENOENT;
2566b032f27cSSam Leffler 	} else
2567b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
2568b032f27cSSam Leffler 	ni->ni_vlan = vlan.sv_vlan;
2569b032f27cSSam Leffler 	ieee80211_free_node(ni);
2570b032f27cSSam Leffler 	return error;
2571b032f27cSSam Leffler }
2572b032f27cSSam Leffler 
2573b032f27cSSam Leffler static int
2574b032f27cSSam Leffler isvap11g(const struct ieee80211vap *vap)
2575b032f27cSSam Leffler {
2576b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2577b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2578b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2579b032f27cSSam Leffler }
2580b032f27cSSam Leffler 
2581b032f27cSSam Leffler static int
2582b032f27cSSam Leffler isvapht(const struct ieee80211vap *vap)
2583b032f27cSSam Leffler {
2584b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2585b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2586b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2587b032f27cSSam Leffler }
2588b032f27cSSam Leffler 
25898c4e758aSSam Leffler /*
25908c4e758aSSam Leffler  * Dummy ioctl set handler so the linker set is defined.
25918c4e758aSSam Leffler  */
25928c4e758aSSam Leffler static int
25938c4e758aSSam Leffler dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
25948c4e758aSSam Leffler {
25958c4e758aSSam Leffler 	return ENOSYS;
25968c4e758aSSam Leffler }
25978c4e758aSSam Leffler IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
25988c4e758aSSam Leffler 
25998c4e758aSSam Leffler static int
26008c4e758aSSam Leffler ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
26018c4e758aSSam Leffler {
26028c4e758aSSam Leffler 	ieee80211_ioctl_setfunc * const *set;
26038c4e758aSSam Leffler 	int error;
26048c4e758aSSam Leffler 
26058c4e758aSSam Leffler 	SET_FOREACH(set, ieee80211_ioctl_setset) {
26068c4e758aSSam Leffler 		error = (*set)(vap, ireq);
26078c4e758aSSam Leffler 		if (error != ENOSYS)
26088c4e758aSSam Leffler 			return error;
26098c4e758aSSam Leffler 	}
26108c4e758aSSam Leffler 	return EINVAL;
26118c4e758aSSam Leffler }
26128c4e758aSSam Leffler 
2613b032f27cSSam Leffler static __noinline int
2614b032f27cSSam Leffler ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2615b032f27cSSam Leffler {
2616b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
26178a1b9b6aSSam Leffler 	int error;
26188a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
261968e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
26208a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
262168e8e04eSSam Leffler 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
26228a1b9b6aSSam Leffler 	struct ieee80211_key *k;
26238a1b9b6aSSam Leffler 	u_int kid;
26243dfff737SWarner Losh 	uint32_t flags;
26258a1b9b6aSSam Leffler 
26268a1b9b6aSSam Leffler 	error = 0;
26271a1e1d21SSam Leffler 	switch (ireq->i_type) {
26281a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
26291a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
26308a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
26318a1b9b6aSSam Leffler 			return EINVAL;
26321a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
26331a1e1d21SSam Leffler 		if (error)
26341a1e1d21SSam Leffler 			break;
2635b032f27cSSam Leffler 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2636b032f27cSSam Leffler 		vap->iv_des_ssid[0].len = ireq->i_len;
2637b032f27cSSam Leffler 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2638b032f27cSSam Leffler 		vap->iv_des_nssid = (ireq->i_len > 0);
2639b032f27cSSam Leffler 		error = ENETRESET;
26401a1e1d21SSam Leffler 		break;
26411a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
26428a1b9b6aSSam Leffler 		switch (ireq->i_val) {
26438a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
2644b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2645b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
26468a1b9b6aSSam Leffler 			break;
26478a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
2648b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2649b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
26508a1b9b6aSSam Leffler 			break;
26518a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
2652b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2653b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
26548a1b9b6aSSam Leffler 			break;
26551a1e1d21SSam Leffler 		}
2656b032f27cSSam Leffler 		error = ENETRESET;
26571a1e1d21SSam Leffler 		break;
26581a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
26591a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
26608a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
26618a1b9b6aSSam Leffler 			return EINVAL;
2662b032f27cSSam Leffler 		k = &vap->iv_nw_keys[kid];
26638a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
26648a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
2665b032f27cSSam Leffler 			(void) ieee80211_crypto_delkey(vap, k);
26661a1e1d21SSam Leffler 			break;
26671a1e1d21SSam Leffler 		}
26688a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
26698a1b9b6aSSam Leffler 			return EINVAL;
26701a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
26711a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
26721a1e1d21SSam Leffler 		if (error)
26731a1e1d21SSam Leffler 			break;
2674b032f27cSSam Leffler 		ieee80211_key_update_begin(vap);
2675dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2676b032f27cSSam Leffler 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2677dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
26788a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
26798a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
268071fe06caSSam Leffler 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
268171fe06caSSam Leffler 			if  (!ieee80211_crypto_setkey(vap, k))
26828a1b9b6aSSam Leffler 				error = EINVAL;
26838a1b9b6aSSam Leffler 		} else
26848a1b9b6aSSam Leffler 			error = EINVAL;
2685b032f27cSSam Leffler 		ieee80211_key_update_end(vap);
26861a1e1d21SSam Leffler 		break;
26871a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
26881a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2689380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
269068e8e04eSSam Leffler 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
26918a1b9b6aSSam Leffler 			return EINVAL;
2692b032f27cSSam Leffler 		vap->iv_def_txkey = kid;
26938a1b9b6aSSam Leffler 		break;
26948a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
26958a1b9b6aSSam Leffler 		switch (ireq->i_val) {
26968a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
26978a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
26988a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
26998a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
27008a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
27018a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
27028a1b9b6aSSam Leffler 			if (auth == NULL)
27038a1b9b6aSSam Leffler 				return EINVAL;
27048a1b9b6aSSam Leffler 			break;
27058a1b9b6aSSam Leffler 		default:
27068a1b9b6aSSam Leffler 			return EINVAL;
27078a1b9b6aSSam Leffler 		}
27088a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27098a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2710b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
27118a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
27128a1b9b6aSSam Leffler 			break;
27138a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
2714b032f27cSSam Leffler 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
27158a1b9b6aSSam Leffler 			break;
27168a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
27178a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2718b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
27198a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
2720b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
27218a1b9b6aSSam Leffler 			break;
27228a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
2723b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
27248a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
27258a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
27261a1e1d21SSam Leffler 			break;
27271a1e1d21SSam Leffler 		}
27288a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
2729b032f27cSSam Leffler 		vap->iv_bss->ni_authmode = ireq->i_val;
27308a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
2731b032f27cSSam Leffler 		vap->iv_auth = auth;
2732b032f27cSSam Leffler 		error = ENETRESET;
27331a1e1d21SSam Leffler 		break;
27341a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
2735b032f27cSSam Leffler 		error = ieee80211_ioctl_setchannel(vap, ireq);
27361a1e1d21SSam Leffler 		break;
27371a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
27381a1e1d21SSam Leffler 		switch (ireq->i_val) {
27391a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
2740b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2741b032f27cSSam Leffler 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2742b032f27cSSam Leffler 				error = ERESTART;
27431a1e1d21SSam Leffler 			}
27441a1e1d21SSam Leffler 			break;
27451a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
2746b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2747b032f27cSSam Leffler 				error = EOPNOTSUPP;
2748b032f27cSSam Leffler 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2749b032f27cSSam Leffler 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2750b032f27cSSam Leffler 				error = ERESTART;
27511a1e1d21SSam Leffler 			}
27521a1e1d21SSam Leffler 			break;
27531a1e1d21SSam Leffler 		default:
27541a1e1d21SSam Leffler 			error = EINVAL;
27551a1e1d21SSam Leffler 			break;
27561a1e1d21SSam Leffler 		}
27571a1e1d21SSam Leffler 		break;
27581a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
27598a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
27608a1b9b6aSSam Leffler 			return EINVAL;
27611a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
2762b032f27cSSam Leffler 		error = ERESTART;
27631a1e1d21SSam Leffler 		break;
276413604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
276570231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
276670231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
27678a1b9b6aSSam Leffler 			return EINVAL;
2768b032f27cSSam Leffler 		vap->iv_rtsthreshold = ireq->i_val;
2769b032f27cSSam Leffler 		error = ERESTART;
27701a1e1d21SSam Leffler 		break;
27712e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
27728a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
27738a1b9b6aSSam Leffler 			return EINVAL;
2774fcd9500fSBernhard Schmidt 		ic->ic_protmode = (enum ieee80211_protmode)ireq->i_val;
27752e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
277668e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
277768e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2778b032f27cSSam Leffler 			error = ERESTART;
27792e79ca97SSam Leffler 		break;
27802e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
27818a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2782b032f27cSSam Leffler 			return EOPNOTSUPP;
278368e8e04eSSam Leffler 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
278468e8e04eSSam Leffler 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
27858a1b9b6aSSam Leffler 			return EINVAL;
27868a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
2787b032f27cSSam Leffler 		error = ERESTART;
27888a1b9b6aSSam Leffler 		break;
27898a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
27908a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
27918a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
27928a1b9b6aSSam Leffler 			return EINVAL;
2793fcd9500fSBernhard Schmidt 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
27948a1b9b6aSSam Leffler 		/* XXXX reset? */
27958a1b9b6aSSam Leffler 		break;
27968a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
27978a1b9b6aSSam Leffler 		if (ireq->i_val) {
27988a1b9b6aSSam Leffler 			/* XXX check for key state? */
2799b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28008a1b9b6aSSam Leffler 		} else
2801b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2802b032f27cSSam Leffler 		/* XXX ERESTART? */
28038a1b9b6aSSam Leffler 		break;
28048a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
28058a1b9b6aSSam Leffler 		if (ireq->i_val)
2806b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
28078a1b9b6aSSam Leffler 		else
2808b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2809b032f27cSSam Leffler 		/* XXX ERESTART? */
28108a1b9b6aSSam Leffler 		break;
28118a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
2812b032f27cSSam Leffler 		error = ieee80211_ioctl_setkey(vap, ireq);
28138a1b9b6aSSam Leffler 		break;
28148a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
2815b032f27cSSam Leffler 		error = ieee80211_ioctl_delkey(vap, ireq);
28168a1b9b6aSSam Leffler 		break;
28178a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
2818b032f27cSSam Leffler 		error = ieee80211_ioctl_setmlme(vap, ireq);
28198a1b9b6aSSam Leffler 		break;
28208a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
28218a1b9b6aSSam Leffler 		if (ireq->i_val) {
2822b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2823b032f27cSSam Leffler 				return EOPNOTSUPP;
2824b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_COUNTERM;
28258a1b9b6aSSam Leffler 		} else
2826b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2827b032f27cSSam Leffler 		/* XXX ERESTART? */
28288a1b9b6aSSam Leffler 		break;
28298a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
28308a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
28318a1b9b6aSSam Leffler 			return EINVAL;
28328a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
28333dfff737SWarner Losh 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
28348a1b9b6aSSam Leffler 		switch (ireq->i_val) {
28358a1b9b6aSSam Leffler 		case 1:
28363dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
28373dfff737SWarner Losh 				return EOPNOTSUPP;
28383dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1;
28398a1b9b6aSSam Leffler 			break;
28408a1b9b6aSSam Leffler 		case 2:
28413dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
28423dfff737SWarner Losh 				return EOPNOTSUPP;
28433dfff737SWarner Losh 			flags |= IEEE80211_F_WPA2;
28448a1b9b6aSSam Leffler 			break;
28458a1b9b6aSSam Leffler 		case 3:
28463dfff737SWarner Losh 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
28478d092df9SSam Leffler 				return EOPNOTSUPP;
28483dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
28492e79ca97SSam Leffler 			break;
28503dfff737SWarner Losh 		default:	/*  Can't set any -> error */
28513dfff737SWarner Losh 			return EOPNOTSUPP;
28522e79ca97SSam Leffler 		}
28533dfff737SWarner Losh 		vap->iv_flags = flags;
2854b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
28558a1b9b6aSSam Leffler 		break;
28568a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
28578a1b9b6aSSam Leffler 		if (ireq->i_val) {
2858b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2859b032f27cSSam Leffler 				return EOPNOTSUPP;
2860b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_WME);
28618a1b9b6aSSam Leffler 		} else
2862b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2863b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
28648a1b9b6aSSam Leffler 		break;
28658a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
28668a1b9b6aSSam Leffler 		if (ireq->i_val)
2867b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_HIDESSID;
28688a1b9b6aSSam Leffler 		else
2869b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2870b032f27cSSam Leffler 		error = ERESTART;		/* XXX ENETRESET? */
28712e79ca97SSam Leffler 		break;
28728a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
28738a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
2874b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
28758a1b9b6aSSam Leffler 		else
2876b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
28778a1b9b6aSSam Leffler 		break;
28788a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
28798a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
28808a1b9b6aSSam Leffler 			return EINVAL;
28818a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
28828a1b9b6aSSam Leffler 		if (error)
28838a1b9b6aSSam Leffler 			break;
2884b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2885b032f27cSSam Leffler 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2886b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
28878a1b9b6aSSam Leffler 		else
2888b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2889b032f27cSSam Leffler 		error = ENETRESET;
28908a1b9b6aSSam Leffler 		break;
28918a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
2892b032f27cSSam Leffler 		error = ieee80211_ioctl_setchanlist(vap, ireq);
28938a1b9b6aSSam Leffler 		break;
2894b032f27cSSam Leffler #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2895b032f27cSSam Leffler #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2896b032f27cSSam Leffler 	case OLD_IEEE80211_IOC_SCAN_REQ:
2897b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2898b032f27cSSam Leffler 			"%s: active scan request\n", __func__);
2899b032f27cSSam Leffler 		/*
2900b032f27cSSam Leffler 		 * If we are in INIT state then the driver has never
2901b032f27cSSam Leffler 		 * had a chance to setup hardware state to do a scan;
2902b032f27cSSam Leffler 		 * use the state machine to get us up the SCAN state.
2903b032f27cSSam Leffler 		 * Otherwise just invoke the scan machinery to start
2904b032f27cSSam Leffler 		 * a one-time scan.
2905b032f27cSSam Leffler 		 */
2906b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
2907b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2908b032f27cSSam Leffler 		else
2909b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap,
291068e8e04eSSam Leffler 				IEEE80211_SCAN_ACTIVE |
291168e8e04eSSam Leffler 				IEEE80211_SCAN_NOPICK |
2912b032f27cSSam Leffler 				IEEE80211_SCAN_ONCE,
2913b032f27cSSam Leffler 				IEEE80211_SCAN_FOREVER, 0, 0,
291468e8e04eSSam Leffler 				/* XXX use ioctl params */
2915b032f27cSSam Leffler 				vap->iv_des_nssid, vap->iv_des_ssid);
2916b032f27cSSam Leffler 		break;
2917b032f27cSSam Leffler #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2918b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
2919b032f27cSSam Leffler 		error = ieee80211_ioctl_scanreq(vap, ireq);
2920b032f27cSSam Leffler 		break;
2921b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_CANCEL:
2922b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2923b032f27cSSam Leffler 		    "%s: cancel scan\n", __func__);
2924b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);
2925b032f27cSSam Leffler 		break;
2926b032f27cSSam Leffler 	case IEEE80211_IOC_HTCONF:
2927b032f27cSSam Leffler 		if (ireq->i_val & 1)
29282bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
2929b032f27cSSam Leffler 		else
29302bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
2931b032f27cSSam Leffler 		if (ireq->i_val & 2)
29322bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
2933b032f27cSSam Leffler 		else
29342bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
2935b032f27cSSam Leffler 		error = ENETRESET;
29368a1b9b6aSSam Leffler 		break;
29378a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
29388a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
2939b032f27cSSam Leffler 		error = ieee80211_ioctl_macmac(vap, ireq);
29408a1b9b6aSSam Leffler 		break;
29418a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
2942b032f27cSSam Leffler 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
29438a1b9b6aSSam Leffler 		break;
294442568791SSam Leffler 	case IEEE80211_IOC_STA_STATS:
2945b032f27cSSam Leffler 		error = ieee80211_ioctl_setstastats(vap, ireq);
294642568791SSam Leffler 		break;
29478a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
2948b032f27cSSam Leffler 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
29498a1b9b6aSSam Leffler 		break;
29508a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
29518a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
29528a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
29538a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
29548a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
29558a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2956b032f27cSSam Leffler 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
29578a1b9b6aSSam Leffler 		break;
29588a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
2959b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
296059aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2961b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
29628a1b9b6aSSam Leffler 			return EINVAL;
29638a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
29648a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2965b032f27cSSam Leffler 			vap->iv_dtim_period = ireq->i_val;
29668a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
29678a1b9b6aSSam Leffler 		} else
29688a1b9b6aSSam Leffler 			error = EINVAL;
29698a1b9b6aSSam Leffler 		break;
29708a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
2971b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
297259aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2973b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
29748a1b9b6aSSam Leffler 			return EINVAL;
29758a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
29768a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2977d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
29788a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
29798a1b9b6aSSam Leffler 		} else
29808a1b9b6aSSam Leffler 			error = EINVAL;
29818a1b9b6aSSam Leffler 		break;
2982c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
2983c4f040c3SSam Leffler 		if (ireq->i_val)
2984b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PUREG;
2985c4f040c3SSam Leffler 		else
2986b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PUREG;
2987c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
2988b032f27cSSam Leffler 		if (isvap11g(vap))
2989c4f040c3SSam Leffler 			error = ENETRESET;
2990c4f040c3SSam Leffler 		break;
299132b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
299232b0e64bSAdrian Chadd 		vap->iv_quiet= ireq->i_val;
299332b0e64bSAdrian Chadd 		break;
299432b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
299532b0e64bSAdrian Chadd 		vap->iv_quiet_count=ireq->i_val;
299632b0e64bSAdrian Chadd 		break;
299732b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
299832b0e64bSAdrian Chadd 		vap->iv_quiet_period=ireq->i_val;
299932b0e64bSAdrian Chadd 		break;
300032b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
300132b0e64bSAdrian Chadd 		vap->iv_quiet_offset=ireq->i_val;
300232b0e64bSAdrian Chadd 		break;
300332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
300432b0e64bSAdrian Chadd 		if(ireq->i_val < vap->iv_bss->ni_intval)
300532b0e64bSAdrian Chadd 			vap->iv_quiet_duration = ireq->i_val;
300632b0e64bSAdrian Chadd 		else
300732b0e64bSAdrian Chadd 			error = EINVAL;
300832b0e64bSAdrian Chadd 		break;
300968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
301068e8e04eSSam Leffler 		if (ireq->i_val) {
3011b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3012b032f27cSSam Leffler 				return EOPNOTSUPP;
3013b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_BGSCAN;
301468e8e04eSSam Leffler 		} else
3015b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
301668e8e04eSSam Leffler 		break;
301768e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
301868e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3019b032f27cSSam Leffler 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
302068e8e04eSSam Leffler 		else
302168e8e04eSSam Leffler 			error = EINVAL;
302268e8e04eSSam Leffler 		break;
302368e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
302468e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3025b032f27cSSam Leffler 			vap->iv_bgscanintvl = ireq->i_val*hz;
302668e8e04eSSam Leffler 		else
302768e8e04eSSam Leffler 			error = EINVAL;
302868e8e04eSSam Leffler 		break;
302968e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
303068e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3031b032f27cSSam Leffler 			vap->iv_scanvalid = ireq->i_val*hz;
303268e8e04eSSam Leffler 		else
303368e8e04eSSam Leffler 			error = EINVAL;
303468e8e04eSSam Leffler 		break;
303570231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
3036b032f27cSSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
303770231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
3038b032f27cSSam Leffler 			return EOPNOTSUPP;
303970231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
304070231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
304170231e3dSSam Leffler 			return EINVAL;
3042b032f27cSSam Leffler 		vap->iv_fragthreshold = ireq->i_val;
3043b032f27cSSam Leffler 		error = ERESTART;
304470231e3dSSam Leffler 		break;
3045c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
3046c27e4e31SSam Leffler 		if (ireq->i_val) {
3047b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3048b032f27cSSam Leffler 				return EOPNOTSUPP;
3049b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3050c27e4e31SSam Leffler 		} else
3051b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3052b032f27cSSam Leffler 		error = ERESTART;
3053c27e4e31SSam Leffler 		break;
3054546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
3055546786c9SSam Leffler 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3056546786c9SSam Leffler 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3057546786c9SSam Leffler 			return EINVAL;
3058b032f27cSSam Leffler 		vap->iv_bmissthreshold = ireq->i_val;
3059b032f27cSSam Leffler 		error = ERESTART;
3060546786c9SSam Leffler 		break;
306168e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
3062b032f27cSSam Leffler 		error = ieee80211_ioctl_setcurchan(vap, ireq);
306368e8e04eSSam Leffler 		break;
306468e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
306568e8e04eSSam Leffler 		if (ireq->i_val) {
306668e8e04eSSam Leffler #define	IEEE80211_HTCAP_SHORTGI \
306768e8e04eSSam Leffler 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3068b032f27cSSam Leffler 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
306968e8e04eSSam Leffler 				return EINVAL;
307068e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
30712bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
307268e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
30732bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
307468e8e04eSSam Leffler #undef IEEE80211_HTCAP_SHORTGI
307568e8e04eSSam Leffler 		} else
30762bfc8a91SSam Leffler 			vap->iv_flags_ht &=
30772bfc8a91SSam Leffler 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3078b032f27cSSam Leffler 		error = ERESTART;
307968e8e04eSSam Leffler 		break;
308068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
3081b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
308268e8e04eSSam Leffler 			return EINVAL;
308368e8e04eSSam Leffler 		if (ireq->i_val & 1)
30842bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3085b032f27cSSam Leffler 		else
30862bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
308768e8e04eSSam Leffler 		if (ireq->i_val & 2)
30882bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3089b032f27cSSam Leffler 		else
30902bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
309168e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3092b032f27cSSam Leffler 		if (isvapht(vap))
3093b032f27cSSam Leffler 			error = ERESTART;
309468e8e04eSSam Leffler 		break;
309568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
3096b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3097b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3098b032f27cSSam Leffler 			return EINVAL;
3099b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3100b032f27cSSam Leffler 			vap->iv_ampdu_rxmax = ireq->i_val;
3101b032f27cSSam Leffler 		else
3102b032f27cSSam Leffler 			vap->iv_ampdu_limit = ireq->i_val;
3103b032f27cSSam Leffler 		error = ERESTART;
310468e8e04eSSam Leffler 		break;
310568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
3106b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3107b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3108b032f27cSSam Leffler 			return EINVAL;
3109b032f27cSSam Leffler 		vap->iv_ampdu_density = ireq->i_val;
3110b032f27cSSam Leffler 		error = ERESTART;
311168e8e04eSSam Leffler 		break;
311268e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
3113b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
311468e8e04eSSam Leffler 			return EINVAL;
311568e8e04eSSam Leffler 		if (ireq->i_val & 1)
31162bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3117b032f27cSSam Leffler 		else
31182bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
311968e8e04eSSam Leffler 		if (ireq->i_val & 2)
31202bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3121b032f27cSSam Leffler 		else
31222bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
312368e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3124b032f27cSSam Leffler 		if (isvapht(vap))
3125b032f27cSSam Leffler 			error = ERESTART;
312668e8e04eSSam Leffler 		break;
312768e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
312868e8e04eSSam Leffler 		/* XXX validate */
3129b032f27cSSam Leffler 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
313068e8e04eSSam Leffler 		break;
313168e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
313268e8e04eSSam Leffler 		if (ireq->i_val) {
31332bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
313468e8e04eSSam Leffler 				return EINVAL;
31352bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
313668e8e04eSSam Leffler 		} else
31372bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
313868e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3139b032f27cSSam Leffler 		if (isvapht(vap))
3140b032f27cSSam Leffler 			error = ERESTART;
314168e8e04eSSam Leffler 		break;
314268e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
314368e8e04eSSam Leffler 		if (ireq->i_val) {
314468e8e04eSSam Leffler #if 0
314568e8e04eSSam Leffler 			/* XXX no capability */
3146b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3147b032f27cSSam Leffler 				return EOPNOTSUPP;
314868e8e04eSSam Leffler #endif
3149b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DOTH;
315068e8e04eSSam Leffler 		} else
3151b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3152b032f27cSSam Leffler 		error = ENETRESET;
3153b032f27cSSam Leffler 		break;
3154b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
3155b032f27cSSam Leffler 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3156b032f27cSSam Leffler 		break;
3157b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
3158b032f27cSSam Leffler 		error = ieee80211_ioctl_setroam(vap, ireq);
3159b032f27cSSam Leffler 		break;
3160b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
3161b032f27cSSam Leffler 		error = ieee80211_ioctl_settxparams(vap, ireq);
316268e8e04eSSam Leffler 		break;
316368e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
316468e8e04eSSam Leffler 		if (ireq->i_val) {
31652bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3166b032f27cSSam Leffler 				return EOPNOTSUPP;
31672bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
316868e8e04eSSam Leffler 		} else
31692bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
317068e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3171b032f27cSSam Leffler 		if (isvapht(vap))
3172b032f27cSSam Leffler 			error = ERESTART;
3173b032f27cSSam Leffler 		break;
3174b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
3175b032f27cSSam Leffler 		if (ireq->i_val) {
3176b032f27cSSam Leffler 			/* NB: DWDS only makes sense for WDS-capable devices */
3177b032f27cSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3178b032f27cSSam Leffler 				return EOPNOTSUPP;
3179b032f27cSSam Leffler 			/* NB: DWDS is used only with ap+sta vaps */
3180b032f27cSSam Leffler 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3181b032f27cSSam Leffler 			    vap->iv_opmode != IEEE80211_M_STA)
3182b032f27cSSam Leffler 				return EINVAL;
3183b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DWDS;
3184f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3185f2a6a13cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3186f2a6a13cSSam Leffler 		} else {
3187b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3188f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3189f2a6a13cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3190f2a6a13cSSam Leffler 		}
319168e8e04eSSam Leffler 		break;
3192c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
3193c066143cSSam Leffler 		if (ireq->i_val)
3194b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3195c066143cSSam Leffler 		else
3196b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3197b032f27cSSam Leffler 		break;
3198b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
3199b032f27cSSam Leffler 		error = ieee80211_ioctl_setappie(vap, ireq);
3200b032f27cSSam Leffler 		break;
3201b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
3202b032f27cSSam Leffler 		if (ireq->i_val) {
3203b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3204b032f27cSSam Leffler 				return EOPNOTSUPP;
3205b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3206b032f27cSSam Leffler 		} else
3207b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3208b032f27cSSam Leffler 		break;
3209b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
3210b032f27cSSam Leffler 		if (ireq->i_val) {
3211b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3212b032f27cSSam Leffler 				return EOPNOTSUPP;
3213b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3214b032f27cSSam Leffler 		} else
3215b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3216b032f27cSSam Leffler 		break;
3217b032f27cSSam Leffler 	case IEEE80211_IOC_CHANSWITCH:
3218b032f27cSSam Leffler 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3219b032f27cSSam Leffler 		break;
3220b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
3221b032f27cSSam Leffler 		if (ireq->i_val) {
3222b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3223b032f27cSSam Leffler 				return EOPNOTSUPP;
3224b032f27cSSam Leffler 			/* NB: DFS requires 11h support */
3225b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3226b032f27cSSam Leffler 				return EINVAL;
3227b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3228b032f27cSSam Leffler 		} else
3229b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3230b032f27cSSam Leffler 		break;
3231b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
3232b032f27cSSam Leffler 		if (ireq->i_val)
3233b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3234b032f27cSSam Leffler 		else
3235b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3236b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
3237b032f27cSSam Leffler 			error = ENETRESET;
3238c066143cSSam Leffler 		break;
32391b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
32401b6167d2SSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
32411b6167d2SSam Leffler 			return EINVAL;
32421b6167d2SSam Leffler 		ic->ic_htprotmode = ireq->i_val ?
32431b6167d2SSam Leffler 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
32441b6167d2SSam Leffler 		/* NB: if not operating in 11n this can wait */
3245b032f27cSSam Leffler 		if (isvapht(vap))
32461b6167d2SSam Leffler 			error = ERESTART;
32471b6167d2SSam Leffler 		break;
3248b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
3249b032f27cSSam Leffler 		error = ieee80211_ioctl_setstavlan(vap, ireq);
32501b6167d2SSam Leffler 		break;
32518c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
32528c070d69SSam Leffler 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
32538c070d69SSam Leffler 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
32548c070d69SSam Leffler 			return EINVAL;
32558c070d69SSam Leffler 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
32568c070d69SSam Leffler 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
32578c070d69SSam Leffler 			return EOPNOTSUPP;
32588c070d69SSam Leffler 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
32598c070d69SSam Leffler 			ireq->i_val;
32608c070d69SSam Leffler 		/* NB: if not operating in 11n this can wait */
32618c070d69SSam Leffler 		if (isvapht(vap))
32628c070d69SSam Leffler 			error = ERESTART;
32638c070d69SSam Leffler 		break;
326444f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
326544f7a6edSSam Leffler 		if (ireq->i_val != 0) {
326644f7a6edSSam Leffler 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
326744f7a6edSSam Leffler 				return EOPNOTSUPP;
32682bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
326944f7a6edSSam Leffler 		} else
32702bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
327144f7a6edSSam Leffler 		/* NB: if not operating in 11n this can wait */
327244f7a6edSSam Leffler 		if (isvapht(vap))
327344f7a6edSSam Leffler 			error = ERESTART;
327444f7a6edSSam Leffler 		break;
32751a1e1d21SSam Leffler 	default:
32768c4e758aSSam Leffler 		error = ieee80211_ioctl_setdefault(vap, ireq);
32771a1e1d21SSam Leffler 		break;
32781a1e1d21SSam Leffler 	}
3279b032f27cSSam Leffler 	/*
3280b032f27cSSam Leffler 	 * The convention is that ENETRESET means an operation
3281b032f27cSSam Leffler 	 * requires a complete re-initialization of the device (e.g.
3282b032f27cSSam Leffler 	 * changing something that affects the association state).
3283b032f27cSSam Leffler 	 * ERESTART means the request may be handled with only a
3284b032f27cSSam Leffler 	 * reload of the hardware state.  We hand ERESTART requests
3285b032f27cSSam Leffler 	 * to the iv_reset callback so the driver can decide.  If
3286b032f27cSSam Leffler 	 * a device does not fillin iv_reset then it defaults to one
3287b032f27cSSam Leffler 	 * that returns ENETRESET.  Otherwise a driver may return
3288b032f27cSSam Leffler 	 * ENETRESET (in which case a full reset will be done) or
3289b032f27cSSam Leffler 	 * 0 to mean there's no need to do anything (e.g. when the
3290b032f27cSSam Leffler 	 * change has no effect on the driver/device).
3291b032f27cSSam Leffler 	 */
3292b032f27cSSam Leffler 	if (error == ERESTART)
3293b032f27cSSam Leffler 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3294b032f27cSSam Leffler 		    vap->iv_reset(vap, ireq->i_type) : 0;
3295b032f27cSSam Leffler 	if (error == ENETRESET) {
3296b032f27cSSam Leffler 		/* XXX need to re-think AUTO handling */
3297b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
3298b032f27cSSam Leffler 			ieee80211_init(vap);
3299b032f27cSSam Leffler 		error = 0;
3300b032f27cSSam Leffler 	}
33018a1b9b6aSSam Leffler 	return error;
33028a1b9b6aSSam Leffler }
33038a1b9b6aSSam Leffler 
3304b032f27cSSam Leffler /*
3305b032f27cSSam Leffler  * Rebuild the parent's multicast address list after an add/del
3306b032f27cSSam Leffler  * of a multicast address for a vap.  We have no way to tell
3307b032f27cSSam Leffler  * what happened above to optimize the work so we purge the entire
3308b032f27cSSam Leffler  * list and rebuild from scratch.  This is way expensive.
3309b032f27cSSam Leffler  * Note also the half-baked workaround for if_addmulti calling
3310b032f27cSSam Leffler  * back to the parent device; there's no way to insert mcast
3311b032f27cSSam Leffler  * entries quietly and/or cheaply.
3312b032f27cSSam Leffler  */
3313b032f27cSSam Leffler static void
3314b032f27cSSam Leffler ieee80211_ioctl_updatemulti(struct ieee80211com *ic)
33158a1b9b6aSSam Leffler {
3316b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
3317b032f27cSSam Leffler 	struct ieee80211vap *vap;
3318b032f27cSSam Leffler 	void *ioctl;
3319b032f27cSSam Leffler 
3320b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
332193ec7edcSShteryana Shopova 	if_delallmulti(parent);
3322b032f27cSSam Leffler 	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3323b032f27cSSam Leffler 	parent->if_ioctl = NULL;
3324b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3325b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
3326b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
3327b032f27cSSam Leffler 
332893ec7edcSShteryana Shopova 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
332993ec7edcSShteryana Shopova 			if (ifma->ifma_addr->sa_family != AF_LINK)
333093ec7edcSShteryana Shopova 				continue;
3331b032f27cSSam Leffler 			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3332b032f27cSSam Leffler 		}
333393ec7edcSShteryana Shopova 	}
3334b032f27cSSam Leffler 	parent->if_ioctl = ioctl;
33355efea30fSAndrew Thompson 	ieee80211_runtask(ic, &ic->ic_mcast_task);
3336b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
3337b032f27cSSam Leffler }
3338b032f27cSSam Leffler 
3339b032f27cSSam Leffler int
3340b032f27cSSam Leffler ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3341b032f27cSSam Leffler {
33421da89db5SSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
33431da89db5SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
33448a1b9b6aSSam Leffler 	int error = 0;
33458a1b9b6aSSam Leffler 	struct ifreq *ifr;
33468a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
33478a1b9b6aSSam Leffler 
33488a1b9b6aSSam Leffler 	switch (cmd) {
3349b032f27cSSam Leffler 	case SIOCSIFFLAGS:
3350b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
3351b032f27cSSam Leffler 		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3352b032f27cSSam Leffler 		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3353b032f27cSSam Leffler 		if (ifp->if_flags & IFF_UP) {
3354b032f27cSSam Leffler 			/*
3355b032f27cSSam Leffler 			 * Bring ourself up unless we're already operational.
3356b032f27cSSam Leffler 			 * If we're the first vap and the parent is not up
3357b032f27cSSam Leffler 			 * then it will automatically be brought up as a
3358b032f27cSSam Leffler 			 * side-effect of bringing ourself up.
3359b032f27cSSam Leffler 			 */
3360b032f27cSSam Leffler 			if (vap->iv_state == IEEE80211_S_INIT)
3361b032f27cSSam Leffler 				ieee80211_start_locked(vap);
3362b032f27cSSam Leffler 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3363b032f27cSSam Leffler 			/*
3364b032f27cSSam Leffler 			 * Stop ourself.  If we are the last vap to be
3365b032f27cSSam Leffler 			 * marked down the parent will also be taken down.
3366b032f27cSSam Leffler 			 */
3367b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
3368b032f27cSSam Leffler 		}
3369b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
33708d9b29f3SAndrew Thompson 		/* Wait for parent ioctl handler if it was queued */
3371ae55932eSAndrew Thompson 		ieee80211_waitfor_parent(ic);
3372b032f27cSSam Leffler 		break;
3373b032f27cSSam Leffler 	case SIOCADDMULTI:
3374b032f27cSSam Leffler 	case SIOCDELMULTI:
33751da89db5SSam Leffler 		ieee80211_ioctl_updatemulti(ic);
3376b032f27cSSam Leffler 		break;
33778a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
33788a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
3379b032f27cSSam Leffler 		ifr = (struct ifreq *)data;
3380b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
33818a1b9b6aSSam Leffler 		break;
33828a1b9b6aSSam Leffler 	case SIOCG80211:
3383b032f27cSSam Leffler 		error = ieee80211_ioctl_get80211(vap, cmd,
33848a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
33858a1b9b6aSSam Leffler 		break;
33868a1b9b6aSSam Leffler 	case SIOCS80211:
3387acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
33888a1b9b6aSSam Leffler 		if (error == 0)
3389b032f27cSSam Leffler 			error = ieee80211_ioctl_set80211(vap, cmd,
33908a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
33911a1e1d21SSam Leffler 		break;
33921be50176SSam Leffler 	case SIOCG80211STATS:
33931be50176SSam Leffler 		ifr = (struct ifreq *)data;
3394b032f27cSSam Leffler 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
33951be50176SSam Leffler 		break;
33966f161f03SSam Leffler 	case SIOCSIFMTU:
33976f161f03SSam Leffler 		ifr = (struct ifreq *)data;
33986f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
33996f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
34006f161f03SSam Leffler 			error = EINVAL;
34016f161f03SSam Leffler 		else
34026f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
34036f161f03SSam Leffler 		break;
3404b2e95691SSam Leffler 	case SIOCSIFADDR:
3405b2e95691SSam Leffler 		/*
3406b2e95691SSam Leffler 		 * XXX Handle this directly so we can supress if_init calls.
3407b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
3408b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
3409b2e95691SSam Leffler 		 * XXX set IFF_UP and so supress if_init being called when
3410b2e95691SSam Leffler 		 * XXX it should be.
3411b2e95691SSam Leffler 		 */
3412b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
3413b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
3414b2e95691SSam Leffler #ifdef INET
3415b2e95691SSam Leffler 		case AF_INET:
3416b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3417b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3418b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3419b2e95691SSam Leffler 			}
3420b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
3421b2e95691SSam Leffler 			break;
3422b2e95691SSam Leffler #endif
3423b2e95691SSam Leffler #ifdef IPX
3424b2e95691SSam Leffler 		/*
3425b2e95691SSam Leffler 		 * XXX - This code is probably wrong,
3426b2e95691SSam Leffler 		 *	 but has been copied many times.
3427b2e95691SSam Leffler 		 */
3428b2e95691SSam Leffler 		case AF_IPX: {
3429b2e95691SSam Leffler 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3430b2e95691SSam Leffler 
3431b2e95691SSam Leffler 			if (ipx_nullhost(*ina))
3432fc74a9f9SBrooks Davis 				ina->x_host = *(union ipx_host *)
34334a0d6638SRuslan Ermilov 				    IF_LLADDR(ifp);
3434b2e95691SSam Leffler 			else
3435b2e95691SSam Leffler 				bcopy((caddr_t) ina->x_host.c_host,
34364a0d6638SRuslan Ermilov 				      (caddr_t) IF_LLADDR(ifp),
3437fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
3438b2e95691SSam Leffler 			/* fall thru... */
3439b2e95691SSam Leffler 		}
3440b2e95691SSam Leffler #endif
3441b2e95691SSam Leffler 		default:
3442b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3443b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3444b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3445b2e95691SSam Leffler 			}
3446b2e95691SSam Leffler 			break;
3447b2e95691SSam Leffler 		}
3448b2e95691SSam Leffler 		break;
3449b032f27cSSam Leffler 	/* Pass NDIS ioctls up to the driver */
3450b032f27cSSam Leffler 	case SIOCGDRVSPEC:
3451b032f27cSSam Leffler 	case SIOCSDRVSPEC:
3452b032f27cSSam Leffler 	case SIOCGPRIVATE_0: {
3453b032f27cSSam Leffler 		struct ifnet *parent = vap->iv_ic->ic_ifp;
3454b032f27cSSam Leffler 		error = parent->if_ioctl(parent, cmd, data);
3455b032f27cSSam Leffler 		break;
3456b032f27cSSam Leffler 	}
34571a1e1d21SSam Leffler 	default:
34581a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
34591a1e1d21SSam Leffler 		break;
34601a1e1d21SSam Leffler 	}
34611a1e1d21SSam Leffler 	return error;
34621a1e1d21SSam Leffler }
3463