xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 01e579408b2076c47112d4a568eff5d016fd4410)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
321a1e1d21SSam Leffler /*
331a1e1d21SSam Leffler  * IEEE 802.11 ioctl support (FreeBSD-specific)
341a1e1d21SSam Leffler  */
351a1e1d21SSam Leffler 
36b2e95691SSam Leffler #include "opt_inet.h"
37b032f27cSSam Leffler #include "opt_wlan.h"
38b2e95691SSam Leffler 
391a1e1d21SSam Leffler #include <sys/endian.h>
401a1e1d21SSam Leffler #include <sys/param.h>
411a1e1d21SSam Leffler #include <sys/kernel.h>
428ec07310SGleb Smirnoff #include <sys/malloc.h>
431a1e1d21SSam Leffler #include <sys/socket.h>
441a1e1d21SSam Leffler #include <sys/sockio.h>
451a1e1d21SSam Leffler #include <sys/systm.h>
461a1e1d21SSam Leffler 
471a1e1d21SSam Leffler #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
494a0d6638SRuslan Ermilov #include <net/if_dl.h>
501a1e1d21SSam Leffler #include <net/if_media.h>
511a1e1d21SSam Leffler #include <net/ethernet.h>
521a1e1d21SSam Leffler 
53b2e95691SSam Leffler #ifdef INET
54b2e95691SSam Leffler #include <netinet/in.h>
55b2e95691SSam Leffler #include <netinet/if_ether.h>
56b2e95691SSam Leffler #endif
57b2e95691SSam Leffler 
581a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
591a1e1d21SSam Leffler #include <net80211/ieee80211_ioctl.h>
60b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
61b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
621a1e1d21SSam Leffler 
63b032f27cSSam Leffler #define	IS_UP_AUTO(_vap) \
643cdd9880SSam Leffler 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
65b032f27cSSam Leffler 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
668a1b9b6aSSam Leffler 
67b032f27cSSam Leffler static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
6868e8e04eSSam Leffler static struct ieee80211_channel *findchannel(struct ieee80211com *,
6968e8e04eSSam Leffler 		int ieee, int mode);
70632ee7e3SBernhard Schmidt static int ieee80211_scanreq(struct ieee80211vap *,
71632ee7e3SBernhard Schmidt 		struct ieee80211_scan_req *);
721a1e1d21SSam Leffler 
7374bdb731SAndriy Voskoboinyk static int
74*01e57940SBjoern A. Zeeb ieee80211_ioctl_getkey(u_long cmd, struct ieee80211vap *vap,
75*01e57940SBjoern A. Zeeb     struct ieee80211req *ireq)
768a1b9b6aSSam Leffler {
77b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
788a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
798a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
808a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
818a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
828a1b9b6aSSam Leffler 	u_int kid;
838a1b9b6aSSam Leffler 	int error;
848a1b9b6aSSam Leffler 
858a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
868a1b9b6aSSam Leffler 		return EINVAL;
878a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
888a1b9b6aSSam Leffler 	if (error)
898a1b9b6aSSam Leffler 		return error;
908a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
918a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
92b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
938a1b9b6aSSam Leffler 		if (ni == NULL)
94b032f27cSSam Leffler 			return ENOENT;
958a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
968a1b9b6aSSam Leffler 	} else {
978a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
988a1b9b6aSSam Leffler 			return EINVAL;
99b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
100b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
1018a1b9b6aSSam Leffler 		ni = NULL;
1028a1b9b6aSSam Leffler 	}
1038a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
1048a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
1058a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
1068a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
107b032f27cSSam Leffler 	if (wk->wk_keyix == vap->iv_def_txkey)
1088a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
109*01e57940SBjoern A. Zeeb 	if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
1108a1b9b6aSSam Leffler 		/* NB: only root can read key data */
111b032f27cSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
1128a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
1138a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
1148a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
1158a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
1168a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
1178a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
1188a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
1198a1b9b6aSSam Leffler 		}
1208a1b9b6aSSam Leffler 	} else {
1218a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
1228a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
1238a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
1248a1b9b6aSSam Leffler 	}
1258a1b9b6aSSam Leffler 	if (ni != NULL)
1268a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
1278a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
1288a1b9b6aSSam Leffler }
1298a1b9b6aSSam Leffler 
13074bdb731SAndriy Voskoboinyk static int
131b032f27cSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1328a1b9b6aSSam Leffler {
133b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1348a1b9b6aSSam Leffler 
135e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
1368a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1378a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
1388a1b9b6aSSam Leffler }
1398a1b9b6aSSam Leffler 
14074bdb731SAndriy Voskoboinyk static int
141b032f27cSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
1428a1b9b6aSSam Leffler {
143b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
144db9ff08bSKevin Lo 	uint32_t space;
1458a1b9b6aSSam Leffler 
1468a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
14768e8e04eSSam Leffler 			ic_chans[ic->ic_nchans]);
1488a1b9b6aSSam Leffler 	if (space > ireq->i_len)
1498a1b9b6aSSam Leffler 		space = ireq->i_len;
15068e8e04eSSam Leffler 	/* XXX assumes compatible layout */
15168e8e04eSSam Leffler 	return copyout(&ic->ic_nchans, ireq->i_data, space);
1528a1b9b6aSSam Leffler }
1538a1b9b6aSSam Leffler 
15474bdb731SAndriy Voskoboinyk static int
155b032f27cSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
156b032f27cSSam Leffler 	struct ieee80211req *ireq, int req)
1578a1b9b6aSSam Leffler {
1588a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
15974bdb731SAndriy Voskoboinyk 	struct ieee80211req_wpaie2 *wpaie;
1608a1b9b6aSSam Leffler 	int error;
1618a1b9b6aSSam Leffler 
1628a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1638a1b9b6aSSam Leffler 		return EINVAL;
16474bdb731SAndriy Voskoboinyk 	wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP,
16574bdb731SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
16674bdb731SAndriy Voskoboinyk 	if (wpaie == NULL)
16774bdb731SAndriy Voskoboinyk 		return ENOMEM;
16874bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN);
1698a1b9b6aSSam Leffler 	if (error != 0)
17074bdb731SAndriy Voskoboinyk 		goto bad;
17174bdb731SAndriy Voskoboinyk 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr);
17274bdb731SAndriy Voskoboinyk 	if (ni == NULL) {
17374bdb731SAndriy Voskoboinyk 		error = ENOENT;
17474bdb731SAndriy Voskoboinyk 		goto bad;
17574bdb731SAndriy Voskoboinyk 	}
176b032f27cSSam Leffler 	if (ni->ni_ies.wpa_ie != NULL) {
177b032f27cSSam Leffler 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
17874bdb731SAndriy Voskoboinyk 		if (ielen > sizeof(wpaie->wpa_ie))
17974bdb731SAndriy Voskoboinyk 			ielen = sizeof(wpaie->wpa_ie);
18074bdb731SAndriy Voskoboinyk 		memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen);
1818a1b9b6aSSam Leffler 	}
18268e8e04eSSam Leffler 	if (req == IEEE80211_IOC_WPAIE2) {
183b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
184b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
18574bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->rsn_ie))
18674bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->rsn_ie);
18774bdb731SAndriy Voskoboinyk 			memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen);
18868e8e04eSSam Leffler 		}
18968e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
19068e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
19168e8e04eSSam Leffler 	} else {
19268e8e04eSSam Leffler 		/* compatibility op, may overwrite wpa ie */
19368e8e04eSSam Leffler 		/* XXX check ic_flags? */
194b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
195b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
19674bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->wpa_ie))
19774bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->wpa_ie);
19874bdb731SAndriy Voskoboinyk 			memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen);
19968e8e04eSSam Leffler 		}
20068e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
20168e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
20268e8e04eSSam Leffler 	}
2038a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20474bdb731SAndriy Voskoboinyk 	error = copyout(wpaie, ireq->i_data, ireq->i_len);
20574bdb731SAndriy Voskoboinyk bad:
20674bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(wpaie, M_TEMP);
20774bdb731SAndriy Voskoboinyk 	return error;
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
21074bdb731SAndriy Voskoboinyk static int
211b032f27cSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
21468e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
215db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
2168a1b9b6aSSam Leffler 	int error;
2178a1b9b6aSSam Leffler 
2188a1b9b6aSSam Leffler 	if (ireq->i_len < off)
2198a1b9b6aSSam Leffler 		return EINVAL;
2208a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
2218a1b9b6aSSam Leffler 	if (error != 0)
2228a1b9b6aSSam Leffler 		return error;
223b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
22468e8e04eSSam Leffler 	if (ni == NULL)
225b032f27cSSam Leffler 		return ENOENT;
2268a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
2278a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
2288a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
22968e8e04eSSam Leffler 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
2308a1b9b6aSSam Leffler 			ireq->i_len - off);
2318a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2328a1b9b6aSSam Leffler 	return error;
2338a1b9b6aSSam Leffler }
2348a1b9b6aSSam Leffler 
23568e8e04eSSam Leffler struct scanreq {
23668e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
23768e8e04eSSam Leffler 	size_t space;
23868e8e04eSSam Leffler };
23968e8e04eSSam Leffler 
24068e8e04eSSam Leffler static size_t
24168e8e04eSSam Leffler scan_space(const struct ieee80211_scan_entry *se, int *ielen)
24268e8e04eSSam Leffler {
24368e8e04eSSam Leffler 	size_t len;
24468e8e04eSSam Leffler 
245b032f27cSSam Leffler 	*ielen = se->se_ies.len;
24668e8e04eSSam Leffler 	/*
24768e8e04eSSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
24868e8e04eSSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
24968e8e04eSSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
25068e8e04eSSam Leffler 	 */
25159aa14a9SRui Paulo 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
25259aa14a9SRui Paulo 	    se->se_meshid[1] + *ielen;
25368e8e04eSSam Leffler 	return roundup(len, sizeof(uint32_t));
25468e8e04eSSam Leffler }
25568e8e04eSSam Leffler 
25668e8e04eSSam Leffler static void
25768e8e04eSSam Leffler get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
25868e8e04eSSam Leffler {
25968e8e04eSSam Leffler 	struct scanreq *req = arg;
26068e8e04eSSam Leffler 	int ielen;
26168e8e04eSSam Leffler 
26268e8e04eSSam Leffler 	req->space += scan_space(se, &ielen);
26368e8e04eSSam Leffler }
26468e8e04eSSam Leffler 
26574bdb731SAndriy Voskoboinyk static void
26668e8e04eSSam Leffler get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
26768e8e04eSSam Leffler {
26868e8e04eSSam Leffler 	struct scanreq *req = arg;
26968e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
27068e8e04eSSam Leffler 	int ielen, len, nr, nxr;
27168e8e04eSSam Leffler 	uint8_t *cp;
27268e8e04eSSam Leffler 
27368e8e04eSSam Leffler 	len = scan_space(se, &ielen);
27468e8e04eSSam Leffler 	if (len > req->space)
27568e8e04eSSam Leffler 		return;
27668e8e04eSSam Leffler 
27768e8e04eSSam Leffler 	sr = req->sr;
27868e8e04eSSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535,
27968e8e04eSSam Leffler 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
280b032f27cSSam Leffler 	sr->isr_len = len;
28168e8e04eSSam Leffler 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
28268e8e04eSSam Leffler 	sr->isr_ie_len = ielen;
28368e8e04eSSam Leffler 	sr->isr_freq = se->se_chan->ic_freq;
28468e8e04eSSam Leffler 	sr->isr_flags = se->se_chan->ic_flags;
28568e8e04eSSam Leffler 	sr->isr_rssi = se->se_rssi;
28668e8e04eSSam Leffler 	sr->isr_noise = se->se_noise;
28768e8e04eSSam Leffler 	sr->isr_intval = se->se_intval;
28868e8e04eSSam Leffler 	sr->isr_capinfo = se->se_capinfo;
28968e8e04eSSam Leffler 	sr->isr_erp = se->se_erp;
29068e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
29168e8e04eSSam Leffler 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
29268e8e04eSSam Leffler 	memcpy(sr->isr_rates, se->se_rates+2, nr);
29368e8e04eSSam Leffler 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
29468e8e04eSSam Leffler 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
29568e8e04eSSam Leffler 	sr->isr_nrates = nr + nxr;
29668e8e04eSSam Leffler 
29759aa14a9SRui Paulo 	/* copy SSID */
29868e8e04eSSam Leffler 	sr->isr_ssid_len = se->se_ssid[1];
29968e8e04eSSam Leffler 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
30068e8e04eSSam Leffler 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
30168e8e04eSSam Leffler 
30259aa14a9SRui Paulo 	/* copy mesh id */
30368e8e04eSSam Leffler 	cp += sr->isr_ssid_len;
30459aa14a9SRui Paulo 	sr->isr_meshid_len = se->se_meshid[1];
30559aa14a9SRui Paulo 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
30659aa14a9SRui Paulo 	cp += sr->isr_meshid_len;
30759aa14a9SRui Paulo 
30859aa14a9SRui Paulo 	if (ielen)
309b032f27cSSam Leffler 		memcpy(cp, se->se_ies.data, ielen);
31068e8e04eSSam Leffler 
31168e8e04eSSam Leffler 	req->space -= len;
31268e8e04eSSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
31368e8e04eSSam Leffler }
31468e8e04eSSam Leffler 
31574bdb731SAndriy Voskoboinyk static int
316b032f27cSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
317b032f27cSSam Leffler 	struct ieee80211req *ireq)
31868e8e04eSSam Leffler {
31968e8e04eSSam Leffler 	struct scanreq req;
32068e8e04eSSam Leffler 	int error;
32168e8e04eSSam Leffler 
322b032f27cSSam Leffler 	if (ireq->i_len < sizeof(struct scanreq))
32368e8e04eSSam Leffler 		return EFAULT;
32468e8e04eSSam Leffler 
32568e8e04eSSam Leffler 	error = 0;
32668e8e04eSSam Leffler 	req.space = 0;
327b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, get_scan_space, &req);
32868e8e04eSSam Leffler 	if (req.space > ireq->i_len)
32968e8e04eSSam Leffler 		req.space = ireq->i_len;
33068e8e04eSSam Leffler 	if (req.space > 0) {
331db9ff08bSKevin Lo 		uint32_t space;
33268e8e04eSSam Leffler 		void *p;
33368e8e04eSSam Leffler 
33468e8e04eSSam Leffler 		space = req.space;
33568e8e04eSSam Leffler 		/* XXX M_WAITOK after driver lock released */
336b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
337b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
33868e8e04eSSam Leffler 		if (p == NULL)
33968e8e04eSSam Leffler 			return ENOMEM;
34068e8e04eSSam Leffler 		req.sr = p;
341b032f27cSSam Leffler 		ieee80211_scan_iterate(vap, get_scan_result, &req);
342239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
343239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
344b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
345239cc3b6SSam Leffler 	} else
346239cc3b6SSam Leffler 		ireq->i_len = 0;
347239cc3b6SSam Leffler 
348239cc3b6SSam Leffler 	return error;
349239cc3b6SSam Leffler }
3508a1b9b6aSSam Leffler 
3512cab1d3dSSam Leffler struct stainforeq {
3522cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3532cab1d3dSSam Leffler 	size_t	space;
3542cab1d3dSSam Leffler };
3558a1b9b6aSSam Leffler 
3562cab1d3dSSam Leffler static size_t
3572cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
3582cab1d3dSSam Leffler {
359b032f27cSSam Leffler 	*ielen = ni->ni_ies.len;
3602cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
36168e8e04eSSam Leffler 		      sizeof(uint32_t));
3622cab1d3dSSam Leffler }
3632cab1d3dSSam Leffler 
3642cab1d3dSSam Leffler static void
3652cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
3662cab1d3dSSam Leffler {
3672cab1d3dSSam Leffler 	struct stainforeq *req = arg;
3682cab1d3dSSam Leffler 	size_t ielen;
3692cab1d3dSSam Leffler 
370b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
3712cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3722cab1d3dSSam Leffler 		return;
3732cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
3742cab1d3dSSam Leffler }
3752cab1d3dSSam Leffler 
37674bdb731SAndriy Voskoboinyk static void
3772cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
3782cab1d3dSSam Leffler {
3792cab1d3dSSam Leffler 	struct stainforeq *req = arg;
380b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3812cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3822cab1d3dSSam Leffler 	size_t ielen, len;
38368e8e04eSSam Leffler 	uint8_t *cp;
3842cab1d3dSSam Leffler 
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 
46074bdb731SAndriy Voskoboinyk static 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;
4727db788c6SAndriy Voskoboinyk 	if (ni == NULL) {
4737db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space,
4747db788c6SAndriy Voskoboinyk 		    &req);
4757db788c6SAndriy Voskoboinyk 	} 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 */
482b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
483b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
484d1c85daeSSam Leffler 		if (p == NULL) {
485d1c85daeSSam Leffler 			error = ENOMEM;
486d1c85daeSSam Leffler 			goto bad;
487d1c85daeSSam Leffler 		}
4882cab1d3dSSam Leffler 		req.si = p;
4897db788c6SAndriy Voskoboinyk 		if (ni == NULL) {
4907db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
4917db788c6SAndriy Voskoboinyk 			    get_sta_info, &req);
4927db788c6SAndriy Voskoboinyk 		} else
493d1c85daeSSam Leffler 			get_sta_info(&req, ni);
4942cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
49568e8e04eSSam Leffler 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
496b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
4972cab1d3dSSam Leffler 	} else
4982cab1d3dSSam Leffler 		ireq->i_len = 0;
499d1c85daeSSam Leffler bad:
500d1c85daeSSam Leffler 	if (ni != NULL)
501d1c85daeSSam Leffler 		ieee80211_free_node(ni);
5028a1b9b6aSSam Leffler 	return error;
5038a1b9b6aSSam Leffler }
5048a1b9b6aSSam Leffler 
50574bdb731SAndriy Voskoboinyk static int
506b032f27cSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
507d1c85daeSSam Leffler {
50868e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
509db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
510d1c85daeSSam Leffler 	struct ieee80211_node *ni;
511d1c85daeSSam Leffler 	int error;
512d1c85daeSSam Leffler 
513d1c85daeSSam Leffler 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
514d1c85daeSSam Leffler 		return EFAULT;
515d1c85daeSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
516d1c85daeSSam Leffler 	if (error != 0)
517d1c85daeSSam Leffler 		return error;
518b032f27cSSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
519d1c85daeSSam Leffler 		ni = NULL;
520d1c85daeSSam Leffler 	} else {
521b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
52268e8e04eSSam Leffler 		if (ni == NULL)
523b032f27cSSam Leffler 			return ENOENT;
524d1c85daeSSam Leffler 	}
525b032f27cSSam Leffler 	return getstainfo_common(vap, ireq, ni, off);
526d1c85daeSSam Leffler }
527d1c85daeSSam Leffler 
52874bdb731SAndriy Voskoboinyk static int
529b032f27cSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
5308a1b9b6aSSam Leffler {
5318a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
5328a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
5338a1b9b6aSSam Leffler 	int error;
5348a1b9b6aSSam Leffler 
5358a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
5368a1b9b6aSSam Leffler 		return EINVAL;
5378a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
5388a1b9b6aSSam Leffler 	if (error != 0)
5398a1b9b6aSSam Leffler 		return error;
540b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
5418a1b9b6aSSam Leffler 	if (ni == NULL)
542b032f27cSSam Leffler 		return ENOENT;
5438a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
5448a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
5458a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
5468a1b9b6aSSam Leffler 	return error;
5478a1b9b6aSSam Leffler }
5488a1b9b6aSSam Leffler 
54974bdb731SAndriy Voskoboinyk static int
550b032f27cSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
5518a1b9b6aSSam Leffler {
552b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5538a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
5548a1b9b6aSSam Leffler 	struct wmeParams *wmep;
5558a1b9b6aSSam Leffler 	int ac;
5568a1b9b6aSSam Leffler 
5578a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
5588a1b9b6aSSam Leffler 		return EINVAL;
5598a1b9b6aSSam Leffler 
5608a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
5618a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
5628a1b9b6aSSam Leffler 		ac = WME_AC_BE;
5638a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
5648a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5658a1b9b6aSSam Leffler 	else
5668a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5678a1b9b6aSSam Leffler 	switch (ireq->i_type) {
5688a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
5698a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
5708a1b9b6aSSam Leffler 		break;
5718a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
5728a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
5738a1b9b6aSSam Leffler 		break;
5748a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
5758a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
5768a1b9b6aSSam Leffler 		break;
5778a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
5788a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
5798a1b9b6aSSam Leffler 		break;
5808a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
5818a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5828a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
5838a1b9b6aSSam Leffler 		break;
5848a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
5858a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5868a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
5878a1b9b6aSSam Leffler 		break;
5888a1b9b6aSSam Leffler 	}
5898a1b9b6aSSam Leffler 	return 0;
5908a1b9b6aSSam Leffler }
5918a1b9b6aSSam Leffler 
59274bdb731SAndriy Voskoboinyk static int
593b032f27cSSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
594188757f5SSam Leffler {
595b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
596188757f5SSam Leffler 
597b032f27cSSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
598188757f5SSam Leffler }
599188757f5SSam Leffler 
60074bdb731SAndriy Voskoboinyk static int
601b032f27cSSam Leffler ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
602b032f27cSSam Leffler {
603b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
604b032f27cSSam Leffler 	struct ieee80211_channel *c;
605b032f27cSSam Leffler 
606b032f27cSSam Leffler 	if (ireq->i_len != sizeof(struct ieee80211_channel))
607b032f27cSSam Leffler 		return EINVAL;
608b032f27cSSam Leffler 	/*
609b032f27cSSam Leffler 	 * vap's may have different operating channels when HT is
610b032f27cSSam Leffler 	 * in use.  When in RUN state report the vap-specific channel.
611b032f27cSSam Leffler 	 * Otherwise return curchan.
612b032f27cSSam Leffler 	 */
613daa96325SIan Lepore 	if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
614b032f27cSSam Leffler 		c = vap->iv_bss->ni_chan;
615b032f27cSSam Leffler 	else
616b032f27cSSam Leffler 		c = ic->ic_curchan;
617b032f27cSSam Leffler 	return copyout(c, ireq->i_data, sizeof(*c));
61868e8e04eSSam Leffler }
61968e8e04eSSam Leffler 
62068e8e04eSSam Leffler static int
621b032f27cSSam Leffler getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
62268e8e04eSSam Leffler {
623b032f27cSSam Leffler 	if (aie == NULL)
62468e8e04eSSam Leffler 		return EINVAL;
625b032f27cSSam Leffler 	/* NB: truncate, caller can check length */
626b032f27cSSam Leffler 	if (ireq->i_len > aie->ie_len)
627b032f27cSSam Leffler 		ireq->i_len = aie->ie_len;
628b032f27cSSam Leffler 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
629b032f27cSSam Leffler }
630b032f27cSSam Leffler 
631b032f27cSSam Leffler static int
632b032f27cSSam Leffler ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
633b032f27cSSam Leffler {
634b032f27cSSam Leffler 	uint8_t fc0;
635b032f27cSSam Leffler 
636b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
637b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
638b032f27cSSam Leffler 		return EINVAL;
639b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
640b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
641b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
642b032f27cSSam Leffler 		return getappie(vap->iv_appie_beacon, ireq);
643b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
644b032f27cSSam Leffler 		return getappie(vap->iv_appie_proberesp, ireq);
645b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
646b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocresp, ireq);
647b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
648b032f27cSSam Leffler 		return getappie(vap->iv_appie_probereq, ireq);
649b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
650b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocreq, ireq);
651b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
652b032f27cSSam Leffler 		return getappie(vap->iv_appie_wpa, ireq);
653b032f27cSSam Leffler 	}
654b032f27cSSam Leffler 	return EINVAL;
655b032f27cSSam Leffler }
656b032f27cSSam Leffler 
65774bdb731SAndriy Voskoboinyk static int
658b032f27cSSam Leffler ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
659b032f27cSSam Leffler 	const struct ieee80211req *ireq)
660b032f27cSSam Leffler {
661b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
662b032f27cSSam Leffler 
663b032f27cSSam Leffler 	if (ireq->i_len != sizeof(ic->ic_regdomain))
664b032f27cSSam Leffler 		return EINVAL;
665b032f27cSSam Leffler 	return copyout(&ic->ic_regdomain, ireq->i_data,
666b032f27cSSam Leffler 	    sizeof(ic->ic_regdomain));
667b032f27cSSam Leffler }
668b032f27cSSam Leffler 
66974bdb731SAndriy Voskoboinyk static int
670b032f27cSSam Leffler ieee80211_ioctl_getroam(struct ieee80211vap *vap,
671b032f27cSSam Leffler 	const struct ieee80211req *ireq)
672b032f27cSSam Leffler {
673cbb24c24SSam Leffler 	size_t len = ireq->i_len;
674cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
675cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_roamparms))
676cbb24c24SSam Leffler 		len = sizeof(vap->iv_roamparms);
677cbb24c24SSam Leffler 	return copyout(vap->iv_roamparms, ireq->i_data, len);
678b032f27cSSam Leffler }
679b032f27cSSam Leffler 
68074bdb731SAndriy Voskoboinyk static int
681b032f27cSSam Leffler ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
682b032f27cSSam Leffler 	const struct ieee80211req *ireq)
683b032f27cSSam Leffler {
684cbb24c24SSam Leffler 	size_t len = ireq->i_len;
685cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
686cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_txparms))
687cbb24c24SSam Leffler 		len = sizeof(vap->iv_txparms);
688cbb24c24SSam Leffler 	return copyout(vap->iv_txparms, ireq->i_data, len);
689b032f27cSSam Leffler }
690b032f27cSSam Leffler 
69174bdb731SAndriy Voskoboinyk static int
692b032f27cSSam Leffler ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
693b032f27cSSam Leffler 	const struct ieee80211req *ireq)
694b032f27cSSam Leffler {
695b032f27cSSam Leffler 	struct ieee80211_devcaps_req *dc;
696b032f27cSSam Leffler 	struct ieee80211req_chaninfo *ci;
6978658b18bSSam Leffler 	int maxchans, error;
698b032f27cSSam Leffler 
6998658b18bSSam Leffler 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
7008658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
7018658b18bSSam Leffler 	/* NB: require 1 so we know ic_nchans is accessible */
7028658b18bSSam Leffler 	if (maxchans < 1)
703b032f27cSSam Leffler 		return EINVAL;
7048658b18bSSam Leffler 	/* constrain max request size, 2K channels is ~24Kbytes */
7058658b18bSSam Leffler 	if (maxchans > 2048)
7068658b18bSSam Leffler 		maxchans = 2048;
7078658b18bSSam Leffler 	dc = (struct ieee80211_devcaps_req *)
708b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP,
709b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
710b032f27cSSam Leffler 	if (dc == NULL)
711b032f27cSSam Leffler 		return ENOMEM;
712b032f27cSSam Leffler 	dc->dc_drivercaps = ic->ic_caps;
713b032f27cSSam Leffler 	dc->dc_cryptocaps = ic->ic_cryptocaps;
714b032f27cSSam Leffler 	dc->dc_htcaps = ic->ic_htcaps;
71535bcfd1cSAdrian Chadd 	dc->dc_vhtcaps = ic->ic_vhtcaps;
716b032f27cSSam Leffler 	ci = &dc->dc_chaninfo;
7178658b18bSSam Leffler 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
718f8b1ea17SSam Leffler 	KASSERT(ci->ic_nchans <= maxchans,
719f8b1ea17SSam Leffler 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
720b032f27cSSam Leffler 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
7218658b18bSSam Leffler 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
722b9b53389SAdrian Chadd 	IEEE80211_FREE(dc, M_TEMP);
723b032f27cSSam Leffler 	return error;
724b032f27cSSam Leffler }
725b032f27cSSam Leffler 
72674bdb731SAndriy Voskoboinyk static int
727b032f27cSSam Leffler ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
728b032f27cSSam Leffler {
729b032f27cSSam Leffler 	struct ieee80211_node *ni;
730b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
731b032f27cSSam Leffler 	int error;
732b032f27cSSam Leffler 
733b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
734b032f27cSSam Leffler 		return EINVAL;
735b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
736b032f27cSSam Leffler 	if (error != 0)
737b032f27cSSam Leffler 		return error;
738b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
739b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
740b032f27cSSam Leffler 		    vlan.sv_macaddr);
741b032f27cSSam Leffler 		if (ni == NULL)
742b032f27cSSam Leffler 			return ENOENT;
743b032f27cSSam Leffler 	} else
744b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
745b032f27cSSam Leffler 	vlan.sv_vlan = ni->ni_vlan;
746b032f27cSSam Leffler 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
747b032f27cSSam Leffler 	ieee80211_free_node(ni);
748b032f27cSSam Leffler 	return error;
74968e8e04eSSam Leffler }
75068e8e04eSSam Leffler 
75168e8e04eSSam Leffler /*
7528c4e758aSSam Leffler  * Dummy ioctl get handler so the linker set is defined.
7538c4e758aSSam Leffler  */
7548c4e758aSSam Leffler static int
7558c4e758aSSam Leffler dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
7568c4e758aSSam Leffler {
7578c4e758aSSam Leffler 	return ENOSYS;
7588c4e758aSSam Leffler }
7598c4e758aSSam Leffler IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
7608c4e758aSSam Leffler 
7618c4e758aSSam Leffler static int
7628c4e758aSSam Leffler ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
7638c4e758aSSam Leffler {
7648c4e758aSSam Leffler 	ieee80211_ioctl_getfunc * const *get;
7658c4e758aSSam Leffler 	int error;
7668c4e758aSSam Leffler 
7678c4e758aSSam Leffler 	SET_FOREACH(get, ieee80211_ioctl_getset) {
7688c4e758aSSam Leffler 		error = (*get)(vap, ireq);
7698c4e758aSSam Leffler 		if (error != ENOSYS)
7708c4e758aSSam Leffler 			return error;
7718c4e758aSSam Leffler 	}
7728c4e758aSSam Leffler 	return EINVAL;
7738c4e758aSSam Leffler }
7748c4e758aSSam Leffler 
77574bdb731SAndriy Voskoboinyk static int
776b032f27cSSam Leffler ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
777b032f27cSSam Leffler     struct ieee80211req *ireq)
7788a1b9b6aSSam Leffler {
779b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
780b032f27cSSam Leffler 	u_int kid, len;
78168e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
7821a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
783b032f27cSSam Leffler 	int error = 0;
7841a1e1d21SSam Leffler 
7851a1e1d21SSam Leffler 	switch (ireq->i_type) {
7867d1d4407SBjoern A. Zeeb 	case IEEE80211_IOC_IC_NAME:
7877d1d4407SBjoern A. Zeeb 		len = strlen(ic->ic_name) + 1;
7887d1d4407SBjoern A. Zeeb 		if (len > ireq->i_len)
7897d1d4407SBjoern A. Zeeb 			return (EINVAL);
7907d1d4407SBjoern A. Zeeb 		ireq->i_len = len;
7917d1d4407SBjoern A. Zeeb 		error = copyout(ic->ic_name, ireq->i_data, ireq->i_len);
7927d1d4407SBjoern A. Zeeb 		break;
7931a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
794b032f27cSSam Leffler 		switch (vap->iv_state) {
7951a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
7961a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
797b032f27cSSam Leffler 			ireq->i_len = vap->iv_des_ssid[0].len;
798b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
7991a1e1d21SSam Leffler 			break;
8001a1e1d21SSam Leffler 		default:
801b032f27cSSam Leffler 			ireq->i_len = vap->iv_bss->ni_esslen;
802b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
8031a1e1d21SSam Leffler 			break;
8041a1e1d21SSam Leffler 		}
8051a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
8061a1e1d21SSam Leffler 		break;
8071a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
8081a1e1d21SSam Leffler 		ireq->i_val = 1;
8091a1e1d21SSam Leffler 		break;
8101a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
811b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
8128a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
813b032f27cSSam Leffler 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
8148a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
8158a1b9b6aSSam Leffler 		else
8168a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
8171a1e1d21SSam Leffler 		break;
8181a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
8191a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
8208a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8218a1b9b6aSSam Leffler 			return EINVAL;
822b032f27cSSam Leffler 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
8231a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
824*01e57940SBjoern A. Zeeb 		if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
825b032f27cSSam Leffler 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
8261a1e1d21SSam Leffler 		} else {
8271a1e1d21SSam Leffler 			bzero(tmpkey, len);
8281a1e1d21SSam Leffler 		}
8291a1e1d21SSam Leffler 		ireq->i_len = len;
8301a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
8311a1e1d21SSam Leffler 		break;
8321a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
8331a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
8341a1e1d21SSam Leffler 		break;
8351a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
836b032f27cSSam Leffler 		ireq->i_val = vap->iv_def_txkey;
8371a1e1d21SSam Leffler 		break;
8381a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
839b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA)
8408a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
8418a1b9b6aSSam Leffler 		else
842b032f27cSSam Leffler 			ireq->i_val = vap->iv_bss->ni_authmode;
8431a1e1d21SSam Leffler 		break;
8441a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
845b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
8461a1e1d21SSam Leffler 		break;
8471a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
848b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PMGTON)
8491a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
8501a1e1d21SSam Leffler 		else
8511a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
8521a1e1d21SSam Leffler 		break;
8531a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
8541a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
8551a1e1d21SSam Leffler 		break;
85613604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
857b032f27cSSam Leffler 		ireq->i_val = vap->iv_rtsthreshold;
8581a1e1d21SSam Leffler 		break;
8592e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
860f1481c8dSAdrian Chadd 		ireq->i_val = vap->iv_protmode;
8612e79ca97SSam Leffler 		break;
8622e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
863b032f27cSSam Leffler 		/*
864b032f27cSSam Leffler 		 * Tx power limit is the min of max regulatory
865b032f27cSSam Leffler 		 * power, any user-set limit, and the max the
866b032f27cSSam Leffler 		 * radio can do.
867781487cfSAdrian Chadd 		 *
868781487cfSAdrian Chadd 		 * TODO: methodize this
869b032f27cSSam Leffler 		 */
870b032f27cSSam Leffler 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
871b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_txpowlimit)
8728a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_txpowlimit;
873b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
874b032f27cSSam Leffler 			ireq->i_val = ic->ic_curchan->ic_maxpower;
8758a1b9b6aSSam Leffler 		break;
8768a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
877b032f27cSSam Leffler 		switch (vap->iv_flags & IEEE80211_F_WPA) {
8788a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
8798a1b9b6aSSam Leffler 			ireq->i_val = 1;
8808a1b9b6aSSam Leffler 			break;
8818a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
8828a1b9b6aSSam Leffler 			ireq->i_val = 2;
8838a1b9b6aSSam Leffler 			break;
8848a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
8858a1b9b6aSSam Leffler 			ireq->i_val = 3;
8868a1b9b6aSSam Leffler 			break;
8878a1b9b6aSSam Leffler 		default:
8888a1b9b6aSSam Leffler 			ireq->i_val = 0;
8898a1b9b6aSSam Leffler 			break;
8908a1b9b6aSSam Leffler 		}
8918a1b9b6aSSam Leffler 		break;
8928a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
893b032f27cSSam Leffler 		error = ieee80211_ioctl_getchanlist(vap, ireq);
8948a1b9b6aSSam Leffler 		break;
8958a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
896b032f27cSSam Leffler 		ireq->i_val = vap->iv_roaming;
8978a1b9b6aSSam Leffler 		break;
8988a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
899b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
9008a1b9b6aSSam Leffler 		break;
9018a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
902b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
9038a1b9b6aSSam Leffler 		break;
9048a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
905b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
9068a1b9b6aSSam Leffler 		break;
9078a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
908b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
9098a1b9b6aSSam Leffler 		break;
9108a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
911b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
9128a1b9b6aSSam Leffler 		break;
9138a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
914b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
9158a1b9b6aSSam Leffler 		break;
9168a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
917*01e57940SBjoern A. Zeeb 		error = ieee80211_ioctl_getkey(cmd, vap, ireq);
9188a1b9b6aSSam Leffler 		break;
9198a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
920b032f27cSSam Leffler 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
9218a1b9b6aSSam Leffler 		break;
9228a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
9238a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
9248a1b9b6aSSam Leffler 			return EINVAL;
9251b74d2a4SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
9267cd89f64SSam Leffler 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
9277cd89f64SSam Leffler 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
9288a1b9b6aSSam Leffler 			    ireq->i_data, ireq->i_len);
9297cd89f64SSam Leffler 		} else
9307cd89f64SSam Leffler 			error = copyout(vap->iv_des_bssid, ireq->i_data,
9317cd89f64SSam Leffler 			    ireq->i_len);
9328a1b9b6aSSam Leffler 		break;
9338a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
93468e8e04eSSam Leffler 	case IEEE80211_IOC_WPAIE2:
935b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
9368a1b9b6aSSam Leffler 		break;
9378a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
938b032f27cSSam Leffler 		error = ieee80211_ioctl_getscanresults(vap, ireq);
9398a1b9b6aSSam Leffler 		break;
9408a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
941b032f27cSSam Leffler 		error = ieee80211_ioctl_getstastats(vap, ireq);
9428a1b9b6aSSam Leffler 		break;
9438a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
944b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_txpower;
9458a1b9b6aSSam Leffler 		break;
9468a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
947b032f27cSSam Leffler 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
9488a1b9b6aSSam Leffler 		break;
9498a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
950b032f27cSSam Leffler 		error = ieee80211_ioctl_getstainfo(vap, ireq);
9518a1b9b6aSSam Leffler 		break;
9528a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
9538a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
9548a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
9558a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
9568a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
957fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
958b032f27cSSam Leffler 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
9598a1b9b6aSSam Leffler 		break;
9608a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
961b032f27cSSam Leffler 		ireq->i_val = vap->iv_dtim_period;
9628a1b9b6aSSam Leffler 		break;
9638a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
9648a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
965b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_intval;
9662e79ca97SSam Leffler 		break;
967c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
968b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
969c4f040c3SSam Leffler 		break;
97032b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
97132b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet;
97232b0e64bSAdrian Chadd 		break;
97332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
97432b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_count;
97532b0e64bSAdrian Chadd 		break;
97632b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
97732b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_period;
97832b0e64bSAdrian Chadd 		break;
97932b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
98032b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_duration;
98132b0e64bSAdrian Chadd 		break;
98232b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
98332b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_offset;
98432b0e64bSAdrian Chadd 		break;
98568e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
986b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
98768e8e04eSSam Leffler 		break;
98868e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
989b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
99068e8e04eSSam Leffler 		break;
99168e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
992b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
99368e8e04eSSam Leffler 		break;
99468e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
995b032f27cSSam Leffler 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
99664353cb0SSam Leffler 		break;
99770231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
998b032f27cSSam Leffler 		ireq->i_val = vap->iv_fragthreshold;
99970231e3dSSam Leffler 		break;
1000188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
1001b032f27cSSam Leffler 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
1002188757f5SSam Leffler 		break;
1003c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
1004b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
1005c27e4e31SSam Leffler 		break;
1006546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
1007b032f27cSSam Leffler 		ireq->i_val = vap->iv_bmissthreshold;
1008546786c9SSam Leffler 		break;
100968e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
1010b032f27cSSam Leffler 		error = ieee80211_ioctl_getcurchan(vap, ireq);
101168e8e04eSSam Leffler 		break;
101268e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
101368e8e04eSSam Leffler 		ireq->i_val = 0;
10142bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
101568e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
10162bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
101768e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
101868e8e04eSSam Leffler 		break;
101968e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
102068e8e04eSSam Leffler 		ireq->i_val = 0;
10212bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
102268e8e04eSSam Leffler 			ireq->i_val |= 1;
10232bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
102468e8e04eSSam Leffler 			ireq->i_val |= 2;
102568e8e04eSSam Leffler 		break;
102668e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
1027781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1028b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1029b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_rxmax;
10301b74d2a4SAdrian Chadd 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
1031cac03190SAdrian Chadd 			/*
1032cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1033cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1034cac03190SAdrian Chadd 			 */
1035fe5ebb23SBjoern A. Zeeb 			ireq->i_val = _IEEE80211_MASKSHIFT( vap->iv_bss->ni_htparam,
1036b032f27cSSam Leffler 			    IEEE80211_HTCAP_MAXRXAMPDU);
1037b032f27cSSam Leffler 		else
1038b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_limit;
103968e8e04eSSam Leffler 		break;
104068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
1041781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1042900de995SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
10431b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1044cac03190SAdrian Chadd 			/*
1045cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1046cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1047cac03190SAdrian Chadd 			 */
1048fe5ebb23SBjoern A. Zeeb 			ireq->i_val = _IEEE80211_MASKSHIFT(vap->iv_bss->ni_htparam,
1049b032f27cSSam Leffler 			    IEEE80211_HTCAP_MPDUDENSITY);
1050b032f27cSSam Leffler 		else
1051b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_density;
105268e8e04eSSam Leffler 		break;
105368e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
105468e8e04eSSam Leffler 		ireq->i_val = 0;
10552bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
105668e8e04eSSam Leffler 			ireq->i_val |= 1;
10572bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
105868e8e04eSSam Leffler 			ireq->i_val |= 2;
105968e8e04eSSam Leffler 		break;
106068e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
1061b032f27cSSam Leffler 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
106268e8e04eSSam Leffler 		break;
106368e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
10642bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
106568e8e04eSSam Leffler 		break;
106668e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
1067b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1068b032f27cSSam Leffler 		break;
1069b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
1070b032f27cSSam Leffler 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1071b032f27cSSam Leffler 		break;
1072b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
1073b032f27cSSam Leffler 		error = ieee80211_ioctl_getroam(vap, ireq);
1074b032f27cSSam Leffler 		break;
1075b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
1076b032f27cSSam Leffler 		error = ieee80211_ioctl_gettxparams(vap, ireq);
107768e8e04eSSam Leffler 		break;
107868e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
10792bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1080b032f27cSSam Leffler 		break;
1081b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
1082b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
108368e8e04eSSam Leffler 		break;
1084c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
1085b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1086b032f27cSSam Leffler 		break;
1087b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
1088b032f27cSSam Leffler 		error = ieee80211_ioctl_getappie(vap, ireq);
1089b032f27cSSam Leffler 		break;
1090b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
1091b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1092b032f27cSSam Leffler 		break;
1093b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
1094b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1095b032f27cSSam Leffler 		break;
1096b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
1097b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1098b032f27cSSam Leffler 		break;
1099b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
1100b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1101b032f27cSSam Leffler 		break;
1102b032f27cSSam Leffler 	case IEEE80211_IOC_DEVCAPS:
1103b032f27cSSam Leffler 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1104c066143cSSam Leffler 		break;
11051b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
1106f1481c8dSAdrian Chadd 		ireq->i_val = vap->iv_htprotmode;
11071b6167d2SSam Leffler 		break;
11081b6167d2SSam Leffler 	case IEEE80211_IOC_HTCONF:
11092bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
11101b6167d2SSam Leffler 			ireq->i_val = 1;
11112bfc8a91SSam Leffler 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
11121b6167d2SSam Leffler 				ireq->i_val |= 2;
11131b6167d2SSam Leffler 		} else
11141b6167d2SSam Leffler 			ireq->i_val = 0;
11151b6167d2SSam Leffler 		break;
1116b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
1117b032f27cSSam Leffler 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1118b032f27cSSam Leffler 		break;
11198c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
11208c070d69SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11211b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
11228c070d69SSam Leffler 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
11238c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
11248c070d69SSam Leffler 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
11258c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
11268c070d69SSam Leffler 			else
11278c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
11288c070d69SSam Leffler 		} else
11298c070d69SSam Leffler 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
11308c070d69SSam Leffler 		break;
113144f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
113244f7a6edSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11331b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
113444f7a6edSSam Leffler 			ireq->i_val =
113544f7a6edSSam Leffler 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
113644f7a6edSSam Leffler 		else
113744f7a6edSSam Leffler 			ireq->i_val =
11382bfc8a91SSam Leffler 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
113944f7a6edSSam Leffler 		break;
1140ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
1141ec3763bbSAdrian Chadd 		ireq->i_val = 0;
1142ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
1143ec3763bbSAdrian Chadd 			ireq->i_val |= 1;
1144ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
1145ec3763bbSAdrian Chadd 			ireq->i_val |= 2;
1146ec3763bbSAdrian Chadd 		break;
1147c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
1148c5bba9daSAndriy Voskoboinyk 		ireq->i_val = 0;
1149c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)
1150c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 1;
1151c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX)
1152c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 2;
1153c5bba9daSAndriy Voskoboinyk 		break;
11548379e8dbSAdrian Chadd 	case IEEE80211_IOC_UAPSD:
11558379e8dbSAdrian Chadd 		ireq->i_val = 0;
11568379e8dbSAdrian Chadd 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
11578379e8dbSAdrian Chadd 			ireq->i_val = 1;
11588379e8dbSAdrian Chadd 		break;
115935bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
116096811be2SBjoern A. Zeeb 		ireq->i_val = vap->iv_flags_vht & IEEE80211_FVHT_MASK;
116135bcfd1cSAdrian Chadd 		break;
11621a1e1d21SSam Leffler 	default:
11638c4e758aSSam Leffler 		error = ieee80211_ioctl_getdefault(vap, ireq);
1164b2e95691SSam Leffler 		break;
11651a1e1d21SSam Leffler 	}
11668a1b9b6aSSam Leffler 	return error;
11678a1b9b6aSSam Leffler }
11688a1b9b6aSSam Leffler 
116974bdb731SAndriy Voskoboinyk static int
1170b032f27cSSam Leffler ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11718a1b9b6aSSam Leffler {
11728a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
11738a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11748a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
117568e8e04eSSam Leffler 	uint16_t kid;
1176b032f27cSSam Leffler 	int error, i;
11778a1b9b6aSSam Leffler 
11788a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
11798a1b9b6aSSam Leffler 		return EINVAL;
11808a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
11811a1e1d21SSam Leffler 	if (error)
11828a1b9b6aSSam Leffler 		return error;
11838a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
11848a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
11858a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
11868a1b9b6aSSam Leffler 		return E2BIG;
11878a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
11888a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
11898a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
11908a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
11918a1b9b6aSSam Leffler 			return EINVAL;
1192b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1193b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1194b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1195b5d4660fSSam Leffler 				ieee80211_free_node(ni);
11968a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1197b5d4660fSSam Leffler 			}
11988a1b9b6aSSam Leffler 		} else {
1199b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1200b032f27cSSam Leffler 				ik.ik_macaddr);
12018a1b9b6aSSam Leffler 			if (ni == NULL)
12028a1b9b6aSSam Leffler 				return ENOENT;
12038a1b9b6aSSam Leffler 		}
12048a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
12058a1b9b6aSSam Leffler 	} else {
12068a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12078a1b9b6aSSam Leffler 			return EINVAL;
1208b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
1209386d84f6SSam Leffler 		/*
1210386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1211386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1212386d84f6SSam Leffler 		 */
1213386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1214386d84f6SSam Leffler 			wk->wk_keyix = kid;
12158a1b9b6aSSam Leffler 		ni = NULL;
12168a1b9b6aSSam Leffler 	}
12178a1b9b6aSSam Leffler 	error = 0;
1218b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
1219b032f27cSSam Leffler 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
12208a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
12218a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
12228a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
12238a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1224b032f27cSSam Leffler 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1225b032f27cSSam Leffler 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
12268a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
12278a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
12288a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
122971fe06caSSam Leffler 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
123071fe06caSSam Leffler 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
123171fe06caSSam Leffler 		if (!ieee80211_crypto_setkey(vap, wk))
12328a1b9b6aSSam Leffler 			error = EIO;
12338a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1234781487cfSAdrian Chadd 			/*
1235781487cfSAdrian Chadd 			 * Inform the driver that this is the default
1236781487cfSAdrian Chadd 			 * transmit key.  Now, ideally we'd just set
1237781487cfSAdrian Chadd 			 * a flag in the key update that would
1238781487cfSAdrian Chadd 			 * say "yes, we're the default key", but
1239781487cfSAdrian Chadd 			 * that currently isn't the way the ioctl ->
1240781487cfSAdrian Chadd 			 * key interface works.
1241781487cfSAdrian Chadd 			 */
1242781487cfSAdrian Chadd 			ieee80211_crypto_set_deftxkey(vap, kid);
12438a1b9b6aSSam Leffler 	} else
12448a1b9b6aSSam Leffler 		error = ENXIO;
1245b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
12468a1b9b6aSSam Leffler 	if (ni != NULL)
12478a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12488a1b9b6aSSam Leffler 	return error;
12498a1b9b6aSSam Leffler }
12508a1b9b6aSSam Leffler 
125174bdb731SAndriy Voskoboinyk static int
1252b032f27cSSam Leffler ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
12538a1b9b6aSSam Leffler {
12548a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
12558a1b9b6aSSam Leffler 	int kid, error;
12568a1b9b6aSSam Leffler 
12578a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
12588a1b9b6aSSam Leffler 		return EINVAL;
12598a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
12608a1b9b6aSSam Leffler 	if (error)
12618a1b9b6aSSam Leffler 		return error;
12628a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
126368e8e04eSSam Leffler 	/* XXX uint8_t -> uint16_t */
126468e8e04eSSam Leffler 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
12658a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
12668a1b9b6aSSam Leffler 
1267b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1268b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1269b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1270b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1271b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1272b5d4660fSSam Leffler 			}
1273b5d4660fSSam Leffler 		} else {
1274b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1275b032f27cSSam Leffler 				dk.idk_macaddr);
12768a1b9b6aSSam Leffler 			if (ni == NULL)
1277b5d4660fSSam Leffler 				return ENOENT;
1278b5d4660fSSam Leffler 		}
12798a1b9b6aSSam Leffler 		/* XXX error return */
1280c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
12818a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12828a1b9b6aSSam Leffler 	} else {
12838a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12848a1b9b6aSSam Leffler 			return EINVAL;
12858a1b9b6aSSam Leffler 		/* XXX error return */
1286b032f27cSSam Leffler 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
12878a1b9b6aSSam Leffler 	}
12888a1b9b6aSSam Leffler 	return 0;
12898a1b9b6aSSam Leffler }
12908a1b9b6aSSam Leffler 
1291b032f27cSSam Leffler struct mlmeop {
1292b032f27cSSam Leffler 	struct ieee80211vap *vap;
1293b032f27cSSam Leffler 	int	op;
1294b032f27cSSam Leffler 	int	reason;
1295b032f27cSSam Leffler };
1296b032f27cSSam Leffler 
1297b032f27cSSam Leffler static void
1298b032f27cSSam Leffler mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1299b032f27cSSam Leffler 	int op, int reason)
1300b032f27cSSam Leffler {
1301b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1302b032f27cSSam Leffler 	static const struct {
1303b032f27cSSam Leffler 		int mask;
1304b032f27cSSam Leffler 		const char *opstr;
1305b032f27cSSam Leffler 	} ops[] = {
1306b032f27cSSam Leffler 		{ 0, "op#0" },
1307b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1308b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "assoc" },
1309b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1310b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "disassoc" },
1311b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1312b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "deauth" },
1313b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1314b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "authorize" },
1315b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1316b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "unauthorize" },
1317b032f27cSSam Leffler 	};
1318b032f27cSSam Leffler 
1319b032f27cSSam Leffler 	if (op == IEEE80211_MLME_AUTH) {
1320b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1321b032f27cSSam Leffler 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1322d72d72d3SAndriy Voskoboinyk 		    "station authenticate %s via MLME (reason: %d (%s))",
1323b032f27cSSam Leffler 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1324d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1325b032f27cSSam Leffler 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1326b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1327d72d72d3SAndriy Voskoboinyk 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
1328d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(reason));
1329b032f27cSSam Leffler 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1330b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1331b032f27cSSam Leffler 		    "station %s via MLME", ops[op].opstr);
1332b032f27cSSam Leffler 	} else {
1333b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1334d72d72d3SAndriy Voskoboinyk 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
1335d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1336b032f27cSSam Leffler 	}
1337b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
1338b032f27cSSam Leffler }
1339b032f27cSSam Leffler 
13408a1b9b6aSSam Leffler static void
13418a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
13428a1b9b6aSSam Leffler {
1343b032f27cSSam Leffler 	struct mlmeop *mop = arg;
1344b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
13458a1b9b6aSSam Leffler 
1346b032f27cSSam Leffler 	if (vap != mop->vap)
1347b032f27cSSam Leffler 		return;
1348b032f27cSSam Leffler 	/*
1349b032f27cSSam Leffler 	 * NB: if ni_associd is zero then the node is already cleaned
1350b032f27cSSam Leffler 	 * up and we don't need to do this (we're safely holding a
1351b032f27cSSam Leffler 	 * reference but should otherwise not modify it's state).
1352b032f27cSSam Leffler 	 */
1353b032f27cSSam Leffler 	if (ni->ni_associd == 0)
1354b032f27cSSam Leffler 		return;
1355b032f27cSSam Leffler 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1356b032f27cSSam Leffler 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1357b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1358b032f27cSSam Leffler 		    mop->reason);
1359b032f27cSSam Leffler 	} else {
1360b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1361b032f27cSSam Leffler 		    mop->reason);
13628a1b9b6aSSam Leffler 	}
1363b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1364b032f27cSSam Leffler }
1365b032f27cSSam Leffler 
1366b032f27cSSam Leffler static int
1367b032f27cSSam Leffler setmlme_dropsta(struct ieee80211vap *vap,
1368b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1369b032f27cSSam Leffler {
13707a79cebfSGleb Smirnoff 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1371b032f27cSSam Leffler 	struct ieee80211_node *ni;
1372b032f27cSSam Leffler 	int error = 0;
1373b032f27cSSam Leffler 
1374b032f27cSSam Leffler 	/* NB: the broadcast address means do 'em all */
13757a79cebfSGleb Smirnoff 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
1376b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1377b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, mac);
13789984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
13799984c9baSAdrian Chadd 		/*
13809984c9baSAdrian Chadd 		 * Don't do the node update inside the node
13819984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
13829984c9baSAdrian Chadd 		 * with drivers and their TX paths.
13839984c9baSAdrian Chadd 		 */
1384b032f27cSSam Leffler 		if (ni != NULL) {
1385b032f27cSSam Leffler 			domlme(mlmeop, ni);
1386b032f27cSSam Leffler 			ieee80211_free_node(ni);
1387b032f27cSSam Leffler 		} else
1388b032f27cSSam Leffler 			error = ENOENT;
1389b032f27cSSam Leffler 	} else {
1390b032f27cSSam Leffler 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1391b032f27cSSam Leffler 	}
1392b032f27cSSam Leffler 	return error;
1393b032f27cSSam Leffler }
1394b032f27cSSam Leffler 
139574bdb731SAndriy Voskoboinyk static int
1396b032f27cSSam Leffler setmlme_common(struct ieee80211vap *vap, int op,
1397b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1398b032f27cSSam Leffler {
1399b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1400b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1401b032f27cSSam Leffler 	struct ieee80211_node *ni;
1402b032f27cSSam Leffler 	struct mlmeop mlmeop;
1403b032f27cSSam Leffler 	int error;
1404b032f27cSSam Leffler 
1405b032f27cSSam Leffler 	error = 0;
1406b032f27cSSam Leffler 	switch (op) {
1407b032f27cSSam Leffler 	case IEEE80211_MLME_DISASSOC:
1408b032f27cSSam Leffler 	case IEEE80211_MLME_DEAUTH:
1409b032f27cSSam Leffler 		switch (vap->iv_opmode) {
1410b032f27cSSam Leffler 		case IEEE80211_M_STA:
1411b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1412b032f27cSSam Leffler 			/* XXX not quite right */
1413b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1414b032f27cSSam Leffler 			break;
1415b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
1416b032f27cSSam Leffler 			mlmeop.vap = vap;
1417b032f27cSSam Leffler 			mlmeop.op = op;
1418b032f27cSSam Leffler 			mlmeop.reason = reason;
1419b032f27cSSam Leffler 			error = setmlme_dropsta(vap, mac, &mlmeop);
1420b032f27cSSam Leffler 			break;
1421b032f27cSSam Leffler 		case IEEE80211_M_WDS:
1422b032f27cSSam Leffler 			/* XXX user app should send raw frame? */
1423b032f27cSSam Leffler 			if (op != IEEE80211_MLME_DEAUTH) {
1424b032f27cSSam Leffler 				error = EINVAL;
1425b032f27cSSam Leffler 				break;
1426b032f27cSSam Leffler 			}
1427b032f27cSSam Leffler #if 0
1428b032f27cSSam Leffler 			/* XXX accept any address, simplifies user code */
1429b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1430b032f27cSSam Leffler 				error = EINVAL;
1431b032f27cSSam Leffler 				break;
1432b032f27cSSam Leffler 			}
1433b032f27cSSam Leffler #endif
1434b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1435b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1436b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
1437b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1438b032f27cSSam Leffler 			ieee80211_free_node(ni);
1439b032f27cSSam Leffler 			break;
1440136cb222SAdrian Chadd 		case IEEE80211_M_MBSS:
1441136cb222SAdrian Chadd 			IEEE80211_NODE_LOCK(nt);
1442136cb222SAdrian Chadd 			ni = ieee80211_find_node_locked(nt, mac);
14439984c9baSAdrian Chadd 			/*
14449984c9baSAdrian Chadd 			 * Don't do the node update inside the node
14459984c9baSAdrian Chadd 			 * table lock.  This unfortunately causes LORs
14469984c9baSAdrian Chadd 			 * with drivers and their TX paths.
14479984c9baSAdrian Chadd 			 */
14489984c9baSAdrian Chadd 			IEEE80211_NODE_UNLOCK(nt);
1449136cb222SAdrian Chadd 			if (ni != NULL) {
1450136cb222SAdrian Chadd 				ieee80211_node_leave(ni);
1451136cb222SAdrian Chadd 				ieee80211_free_node(ni);
1452136cb222SAdrian Chadd 			} else {
1453136cb222SAdrian Chadd 				error = ENOENT;
1454136cb222SAdrian Chadd 			}
1455136cb222SAdrian Chadd 			break;
1456b032f27cSSam Leffler 		default:
1457b032f27cSSam Leffler 			error = EINVAL;
1458b032f27cSSam Leffler 			break;
1459b032f27cSSam Leffler 		}
1460b032f27cSSam Leffler 		break;
1461b032f27cSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
1462b032f27cSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
1463b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1464b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_WDS) {
1465b032f27cSSam Leffler 			error = EINVAL;
1466b032f27cSSam Leffler 			break;
1467b032f27cSSam Leffler 		}
1468b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1469b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14709984c9baSAdrian Chadd 		/*
14719984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14729984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14739984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14749984c9baSAdrian Chadd 		 */
14759984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1476b032f27cSSam Leffler 		if (ni != NULL) {
1477b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1478b032f27cSSam Leffler 			if (op == IEEE80211_MLME_AUTHORIZE)
1479b032f27cSSam Leffler 				ieee80211_node_authorize(ni);
1480b032f27cSSam Leffler 			else
1481b032f27cSSam Leffler 				ieee80211_node_unauthorize(ni);
1482b032f27cSSam Leffler 			ieee80211_free_node(ni);
1483b032f27cSSam Leffler 		} else
1484b032f27cSSam Leffler 			error = ENOENT;
1485b032f27cSSam Leffler 		break;
1486b032f27cSSam Leffler 	case IEEE80211_MLME_AUTH:
1487b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1488b032f27cSSam Leffler 			error = EINVAL;
1489b032f27cSSam Leffler 			break;
1490b032f27cSSam Leffler 		}
1491b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1492b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14939984c9baSAdrian Chadd 		/*
14949984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14959984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14969984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14979984c9baSAdrian Chadd 		 */
14989984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1499b032f27cSSam Leffler 		if (ni != NULL) {
1500b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1501b032f27cSSam Leffler 			if (reason == IEEE80211_STATUS_SUCCESS) {
1502b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
1503b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1504b032f27cSSam Leffler 				/*
1505b032f27cSSam Leffler 				 * For shared key auth, just continue the
1506b032f27cSSam Leffler 				 * exchange.  Otherwise when 802.1x is not in
1507b032f27cSSam Leffler 				 * use mark the port authorized at this point
1508b032f27cSSam Leffler 				 * so traffic can flow.
1509b032f27cSSam Leffler 				 */
1510b032f27cSSam Leffler 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1511b032f27cSSam Leffler 				    ni->ni_challenge == NULL)
1512b032f27cSSam Leffler 				      ieee80211_node_authorize(ni);
1513b032f27cSSam Leffler 			} else {
1514b032f27cSSam Leffler 				vap->iv_stats.is_rx_acl++;
1515b032f27cSSam Leffler 				ieee80211_send_error(ni, ni->ni_macaddr,
1516b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1517b032f27cSSam Leffler 				ieee80211_node_leave(ni);
1518b032f27cSSam Leffler 			}
1519b032f27cSSam Leffler 			ieee80211_free_node(ni);
1520b032f27cSSam Leffler 		} else
1521b032f27cSSam Leffler 			error = ENOENT;
1522b032f27cSSam Leffler 		break;
1523b032f27cSSam Leffler 	default:
1524b032f27cSSam Leffler 		error = EINVAL;
1525b032f27cSSam Leffler 		break;
1526b032f27cSSam Leffler 	}
1527b032f27cSSam Leffler 	return error;
15288a1b9b6aSSam Leffler }
15298a1b9b6aSSam Leffler 
153068e8e04eSSam Leffler struct scanlookup {
153168e8e04eSSam Leffler 	const uint8_t *mac;
153268e8e04eSSam Leffler 	int esslen;
153368e8e04eSSam Leffler 	const uint8_t *essid;
153468e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se;
153568e8e04eSSam Leffler };
153668e8e04eSSam Leffler 
153768e8e04eSSam Leffler /*
153868e8e04eSSam Leffler  * Match mac address and any ssid.
153968e8e04eSSam Leffler  */
154068e8e04eSSam Leffler static void
154168e8e04eSSam Leffler mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
154268e8e04eSSam Leffler {
154368e8e04eSSam Leffler 	struct scanlookup *look = arg;
154468e8e04eSSam Leffler 
154568e8e04eSSam Leffler 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
154668e8e04eSSam Leffler 		return;
154768e8e04eSSam Leffler 	if (look->esslen != 0) {
154868e8e04eSSam Leffler 		if (se->se_ssid[1] != look->esslen)
154968e8e04eSSam Leffler 			return;
155068e8e04eSSam Leffler 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
155168e8e04eSSam Leffler 			return;
155268e8e04eSSam Leffler 	}
155368e8e04eSSam Leffler 	look->se = se;
155468e8e04eSSam Leffler }
155568e8e04eSSam Leffler 
155674bdb731SAndriy Voskoboinyk static int
1557632ee7e3SBernhard Schmidt setmlme_assoc_sta(struct ieee80211vap *vap,
1558632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1559632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1560b032f27cSSam Leffler {
1561b032f27cSSam Leffler 	struct scanlookup lookup;
1562b032f27cSSam Leffler 
1563632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1564632ee7e3SBernhard Schmidt 	    ("expected opmode STA not %s",
1565632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1566b032f27cSSam Leffler 
1567b032f27cSSam Leffler 	/* NB: this is racey if roaming is !manual */
1568b032f27cSSam Leffler 	lookup.se = NULL;
1569b032f27cSSam Leffler 	lookup.mac = mac;
1570b032f27cSSam Leffler 	lookup.esslen = ssid_len;
1571b032f27cSSam Leffler 	lookup.essid = ssid;
1572b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1573b032f27cSSam Leffler 	if (lookup.se == NULL)
1574b032f27cSSam Leffler 		return ENOENT;
1575b032f27cSSam Leffler 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
157610959256SSam Leffler 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1577b032f27cSSam Leffler 		return EIO;		/* XXX unique but could be better */
1578b032f27cSSam Leffler 	return 0;
1579b032f27cSSam Leffler }
1580b032f27cSSam Leffler 
158174bdb731SAndriy Voskoboinyk static int
1582632ee7e3SBernhard Schmidt setmlme_assoc_adhoc(struct ieee80211vap *vap,
1583632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1584632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1585632ee7e3SBernhard Schmidt {
158674bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
158774bdb731SAndriy Voskoboinyk 	int error;
1588632ee7e3SBernhard Schmidt 
1589632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1590632ee7e3SBernhard Schmidt 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1591632ee7e3SBernhard Schmidt 	    ("expected opmode IBSS or AHDEMO not %s",
1592632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1593632ee7e3SBernhard Schmidt 
1594632ee7e3SBernhard Schmidt 	if (ssid_len == 0)
1595632ee7e3SBernhard Schmidt 		return EINVAL;
1596632ee7e3SBernhard Schmidt 
159774bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
159874bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
159974bdb731SAndriy Voskoboinyk 	if (sr == NULL)
160074bdb731SAndriy Voskoboinyk 		return ENOMEM;
160174bdb731SAndriy Voskoboinyk 
1602632ee7e3SBernhard Schmidt 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1603632ee7e3SBernhard Schmidt 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1604632ee7e3SBernhard Schmidt 	vap->iv_des_ssid[0].len = ssid_len;
1605632ee7e3SBernhard Schmidt 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1606632ee7e3SBernhard Schmidt 	vap->iv_des_nssid = 1;
1607632ee7e3SBernhard Schmidt 
160874bdb731SAndriy Voskoboinyk 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
160974bdb731SAndriy Voskoboinyk 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
161074bdb731SAndriy Voskoboinyk 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
161174bdb731SAndriy Voskoboinyk 	sr->sr_ssid[0].len = ssid_len;
161274bdb731SAndriy Voskoboinyk 	sr->sr_nssid = 1;
1613632ee7e3SBernhard Schmidt 
161474bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
161574bdb731SAndriy Voskoboinyk 
161674bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
161774bdb731SAndriy Voskoboinyk 	return error;
1618632ee7e3SBernhard Schmidt }
1619632ee7e3SBernhard Schmidt 
162074bdb731SAndriy Voskoboinyk static int
1621b032f27cSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
16228a1b9b6aSSam Leffler {
16238a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
16248a1b9b6aSSam Leffler 	int error;
16258a1b9b6aSSam Leffler 
16268a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
16278a1b9b6aSSam Leffler 		return EINVAL;
16288a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
16298a1b9b6aSSam Leffler 	if (error)
16308a1b9b6aSSam Leffler 		return error;
1631632ee7e3SBernhard Schmidt 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1632632ee7e3SBernhard Schmidt 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1633632ee7e3SBernhard Schmidt 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1634b032f27cSSam Leffler 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1635edd39a8eSRui Paulo 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1636edd39a8eSRui Paulo 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1637edd39a8eSRui Paulo 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1638632ee7e3SBernhard Schmidt 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1639632ee7e3SBernhard Schmidt 		    mlme.im_ssid_len, mlme.im_ssid);
16408a1b9b6aSSam Leffler 	else
1641b032f27cSSam Leffler 		return setmlme_common(vap, mlme.im_op,
1642b032f27cSSam Leffler 		    mlme.im_macaddr, mlme.im_reason);
16438a1b9b6aSSam Leffler }
16448a1b9b6aSSam Leffler 
164574bdb731SAndriy Voskoboinyk static int
1646b032f27cSSam Leffler ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
16478a1b9b6aSSam Leffler {
164868e8e04eSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1649b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16508a1b9b6aSSam Leffler 	int error;
16518a1b9b6aSSam Leffler 
16528a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
16538a1b9b6aSSam Leffler 		return EINVAL;
16548a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
16558a1b9b6aSSam Leffler 	if (error)
16568a1b9b6aSSam Leffler 		return error;
16578a1b9b6aSSam Leffler 	if (acl == NULL) {
16588a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
1659b032f27cSSam Leffler 		if (acl == NULL || !acl->iac_attach(vap))
16608a1b9b6aSSam Leffler 			return EINVAL;
1661b032f27cSSam Leffler 		vap->iv_acl = acl;
16628a1b9b6aSSam Leffler 	}
16638a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1664b032f27cSSam Leffler 		acl->iac_add(vap, mac);
16658a1b9b6aSSam Leffler 	else
1666b032f27cSSam Leffler 		acl->iac_remove(vap, mac);
16678a1b9b6aSSam Leffler 	return 0;
16688a1b9b6aSSam Leffler }
16698a1b9b6aSSam Leffler 
167074bdb731SAndriy Voskoboinyk static int
1671b032f27cSSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
16728a1b9b6aSSam Leffler {
1673b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16748a1b9b6aSSam Leffler 
16758a1b9b6aSSam Leffler 	switch (ireq->i_val) {
16768a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
16778a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
16788a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
1679b032f27cSSam Leffler 	case IEEE80211_MACCMD_POLICY_RADIUS:
16808a1b9b6aSSam Leffler 		if (acl == NULL) {
16818a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
1682b032f27cSSam Leffler 			if (acl == NULL || !acl->iac_attach(vap))
16838a1b9b6aSSam Leffler 				return EINVAL;
1684b032f27cSSam Leffler 			vap->iv_acl = acl;
16858a1b9b6aSSam Leffler 		}
1686b032f27cSSam Leffler 		acl->iac_setpolicy(vap, ireq->i_val);
16878a1b9b6aSSam Leffler 		break;
16888a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
16898a1b9b6aSSam Leffler 		if (acl != NULL)
1690b032f27cSSam Leffler 			acl->iac_flush(vap);
16918a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
16928a1b9b6aSSam Leffler 		break;
16938a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
16948a1b9b6aSSam Leffler 		if (acl != NULL) {
1695b032f27cSSam Leffler 			vap->iv_acl = NULL;
1696b032f27cSSam Leffler 			acl->iac_detach(vap);
16978a1b9b6aSSam Leffler 		}
16988a1b9b6aSSam Leffler 		break;
16998a1b9b6aSSam Leffler 	default:
1700188757f5SSam Leffler 		if (acl == NULL)
17018a1b9b6aSSam Leffler 			return EINVAL;
1702188757f5SSam Leffler 		else
1703b032f27cSSam Leffler 			return acl->iac_setioctl(vap, ireq);
17048a1b9b6aSSam Leffler 	}
17058a1b9b6aSSam Leffler 	return 0;
17068a1b9b6aSSam Leffler }
17078a1b9b6aSSam Leffler 
170874bdb731SAndriy Voskoboinyk static int
1709b032f27cSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
17108a1b9b6aSSam Leffler {
1711b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17128658b18bSSam Leffler 	uint8_t *chanlist, *list;
17138658b18bSSam Leffler 	int i, nchan, maxchan, error;
17148a1b9b6aSSam Leffler 
17158658b18bSSam Leffler 	if (ireq->i_len > sizeof(ic->ic_chan_active))
17168658b18bSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1717b9b53389SAdrian Chadd 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1718b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
17198658b18bSSam Leffler 	if (list == NULL)
17208658b18bSSam Leffler 		return ENOMEM;
17218658b18bSSam Leffler 	error = copyin(ireq->i_data, list, ireq->i_len);
17227b4d954cSRui Paulo 	if (error) {
1723b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
17248a1b9b6aSSam Leffler 		return error;
17257b4d954cSRui Paulo 	}
172668e8e04eSSam Leffler 	nchan = 0;
17278658b18bSSam Leffler 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
17288658b18bSSam Leffler 	maxchan = ireq->i_len * NBBY;
172931378b1cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
173031378b1cSSam Leffler 		const struct ieee80211_channel *c = &ic->ic_channels[i];
17318a1b9b6aSSam Leffler 		/*
173231378b1cSSam Leffler 		 * Calculate the intersection of the user list and the
173331378b1cSSam Leffler 		 * available channels so users can do things like specify
173431378b1cSSam Leffler 		 * 1-255 to get all available channels.
17358a1b9b6aSSam Leffler 		 */
17368658b18bSSam Leffler 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
173731378b1cSSam Leffler 			setbit(chanlist, c->ic_ieee);
173868e8e04eSSam Leffler 			nchan++;
17398a1b9b6aSSam Leffler 		}
17408a1b9b6aSSam Leffler 	}
17417b4d954cSRui Paulo 	if (nchan == 0) {
1742b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
174368e8e04eSSam Leffler 		return EINVAL;
17447b4d954cSRui Paulo 	}
174568e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
174668e8e04eSSam Leffler 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
174768e8e04eSSam Leffler 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
17488658b18bSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1749b032f27cSSam Leffler 	ieee80211_scan_flush(vap);
1750b9b53389SAdrian Chadd 	IEEE80211_FREE(list, M_TEMP);
1751b032f27cSSam Leffler 	return ENETRESET;
17528a1b9b6aSSam Leffler }
17538a1b9b6aSSam Leffler 
175474bdb731SAndriy Voskoboinyk static int
1755b032f27cSSam Leffler ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
175642568791SSam Leffler {
175742568791SSam Leffler 	struct ieee80211_node *ni;
175868e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
175942568791SSam Leffler 	int error;
176042568791SSam Leffler 
176142568791SSam Leffler 	/*
176242568791SSam Leffler 	 * NB: we could copyin ieee80211req_sta_stats so apps
176342568791SSam Leffler 	 *     could make selective changes but that's overkill;
176442568791SSam Leffler 	 *     just clear all stats for now.
176542568791SSam Leffler 	 */
176642568791SSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
176742568791SSam Leffler 		return EINVAL;
176842568791SSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
176942568791SSam Leffler 	if (error != 0)
177042568791SSam Leffler 		return error;
1771b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
177242568791SSam Leffler 	if (ni == NULL)
1773b032f27cSSam Leffler 		return ENOENT;
1774b032f27cSSam Leffler 	/* XXX require ni_vap == vap? */
177542568791SSam Leffler 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
177642568791SSam Leffler 	ieee80211_free_node(ni);
177742568791SSam Leffler 	return 0;
177842568791SSam Leffler }
177942568791SSam Leffler 
178074bdb731SAndriy Voskoboinyk static int
1781b032f27cSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
17828a1b9b6aSSam Leffler {
17838a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17848a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
17858a1b9b6aSSam Leffler 	int error;
17868a1b9b6aSSam Leffler 
17878a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
17888a1b9b6aSSam Leffler 		return EINVAL;
17898a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
17908a1b9b6aSSam Leffler 	if (error != 0)
17918a1b9b6aSSam Leffler 		return error;
1792b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
17938a1b9b6aSSam Leffler 	if (ni == NULL)
1794b032f27cSSam Leffler 		return ENOENT;
17958a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
17968a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
17978a1b9b6aSSam Leffler 	return error;
17988a1b9b6aSSam Leffler }
17998a1b9b6aSSam Leffler 
180074bdb731SAndriy Voskoboinyk static int
1801b032f27cSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
18028a1b9b6aSSam Leffler {
1803b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
18048a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
18058a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
1806fa46d9dbSAdrian Chadd 	int isbss, ac, aggrmode;
18078a1b9b6aSSam Leffler 
18088a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1809b032f27cSSam Leffler 		return EOPNOTSUPP;
18108a1b9b6aSSam Leffler 
18118a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
18128a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1813fa46d9dbSAdrian Chadd 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
18148a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
18158a1b9b6aSSam Leffler 		ac = WME_AC_BE;
18168a1b9b6aSSam Leffler 	if (isbss) {
18178a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
18188a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
18198a1b9b6aSSam Leffler 	} else {
18208a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
18218a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
18228a1b9b6aSSam Leffler 	}
18238a1b9b6aSSam Leffler 	switch (ireq->i_type) {
18248a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
18258a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = ireq->i_val;
1826fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18278a1b9b6aSSam Leffler 			chanp->wmep_logcwmin = ireq->i_val;
18288a1b9b6aSSam Leffler 		break;
18298a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
18308a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = ireq->i_val;
1831fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18328a1b9b6aSSam Leffler 			chanp->wmep_logcwmax = ireq->i_val;
18338a1b9b6aSSam Leffler 		break;
18348a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18358a1b9b6aSSam Leffler 		wmep->wmep_aifsn = ireq->i_val;
1836fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18378a1b9b6aSSam Leffler 			chanp->wmep_aifsn = ireq->i_val;
18388a1b9b6aSSam Leffler 		break;
18398a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18408a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = ireq->i_val;
1841fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18428a1b9b6aSSam Leffler 			chanp->wmep_txopLimit = ireq->i_val;
18438a1b9b6aSSam Leffler 		break;
18448a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
18458a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
1846fa46d9dbSAdrian Chadd 		if (!aggrmode)
18478a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
18488a1b9b6aSSam Leffler 		break;
18498a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
18508a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
18518a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
18528a1b9b6aSSam Leffler 		break;
18538a1b9b6aSSam Leffler 	}
1854b032f27cSSam Leffler 	ieee80211_wme_updateparams(vap);
18558a1b9b6aSSam Leffler 	return 0;
18568a1b9b6aSSam Leffler }
18578a1b9b6aSSam Leffler 
18588a1b9b6aSSam Leffler static int
185968e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int start, int freq)
186068e8e04eSSam Leffler {
186168e8e04eSSam Leffler 	const struct ieee80211_channel *c;
186268e8e04eSSam Leffler 	int i;
186368e8e04eSSam Leffler 
186468e8e04eSSam Leffler 	for (i = start+1; i < ic->ic_nchans; i++) {
186568e8e04eSSam Leffler 		c = &ic->ic_channels[i];
186668e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
186768e8e04eSSam Leffler 			return 1;
186868e8e04eSSam Leffler 	}
186968e8e04eSSam Leffler 	/* NB: should not be needed but in case things are mis-sorted */
187068e8e04eSSam Leffler 	for (i = 0; i < start; i++) {
187168e8e04eSSam Leffler 		c = &ic->ic_channels[i];
187268e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
187368e8e04eSSam Leffler 			return 1;
187468e8e04eSSam Leffler 	}
187568e8e04eSSam Leffler 	return 0;
187668e8e04eSSam Leffler }
187768e8e04eSSam Leffler 
187868e8e04eSSam Leffler static struct ieee80211_channel *
187968e8e04eSSam Leffler findchannel(struct ieee80211com *ic, int ieee, int mode)
188068e8e04eSSam Leffler {
188168e8e04eSSam Leffler 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1882be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= 0,
1883be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1884be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1885be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1886be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1887be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1888be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1889be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
18906a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
18916a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
189268e8e04eSSam Leffler 	    /* NB: handled specially below */
1893be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1894be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
189535bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IEEE80211_CHAN_A,
189635bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IEEE80211_CHAN_G,
189768e8e04eSSam Leffler 	};
189868e8e04eSSam Leffler 	u_int modeflags;
189968e8e04eSSam Leffler 	int i;
190068e8e04eSSam Leffler 
190168e8e04eSSam Leffler 	modeflags = chanflags[mode];
190268e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
190368e8e04eSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
190468e8e04eSSam Leffler 
190568e8e04eSSam Leffler 		if (c->ic_ieee != ieee)
190668e8e04eSSam Leffler 			continue;
190768e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
190868e8e04eSSam Leffler 			/* ignore turbo channels for autoselect */
190968e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_TURBO(c))
191068e8e04eSSam Leffler 				continue;
191168e8e04eSSam Leffler 			/*
191268e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we
191368e8e04eSSam Leffler 			 *     always select the g channel if both
191468e8e04eSSam Leffler 			 *     are present.
191568e8e04eSSam Leffler 			 * XXX prefer HT to non-HT?
191668e8e04eSSam Leffler 			 */
191768e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_B(c) ||
191868e8e04eSSam Leffler 			    !find11gchannel(ic, i, c->ic_freq))
191968e8e04eSSam Leffler 				return c;
192068e8e04eSSam Leffler 		} else {
192135bcfd1cSAdrian Chadd 			/* must check VHT specifically */
192235bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_VHT_5GHZ ||
192335bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_VHT_2GHZ) &&
192435bcfd1cSAdrian Chadd 			    !IEEE80211_IS_CHAN_VHT(c))
192535bcfd1cSAdrian Chadd 				continue;
192635bcfd1cSAdrian Chadd 
192735bcfd1cSAdrian Chadd 			/*
192835bcfd1cSAdrian Chadd 			 * Must check HT specially - only match on HT,
192935bcfd1cSAdrian Chadd 			 * not HT+VHT channels
193035bcfd1cSAdrian Chadd 			 */
193168e8e04eSSam Leffler 			if ((mode == IEEE80211_MODE_11NA ||
193268e8e04eSSam Leffler 			    mode == IEEE80211_MODE_11NG) &&
193368e8e04eSSam Leffler 			    !IEEE80211_IS_CHAN_HT(c))
193468e8e04eSSam Leffler 				continue;
193535bcfd1cSAdrian Chadd 
193635bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_11NA ||
193735bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_11NG) &&
193835bcfd1cSAdrian Chadd 			    IEEE80211_IS_CHAN_VHT(c))
193935bcfd1cSAdrian Chadd 				continue;
194035bcfd1cSAdrian Chadd 
194135bcfd1cSAdrian Chadd 			/* Check that the modeflags above match */
194268e8e04eSSam Leffler 			if ((c->ic_flags & modeflags) == modeflags)
194368e8e04eSSam Leffler 				return c;
194468e8e04eSSam Leffler 		}
194568e8e04eSSam Leffler 	}
194668e8e04eSSam Leffler 	return NULL;
194768e8e04eSSam Leffler }
194868e8e04eSSam Leffler 
194968e8e04eSSam Leffler /*
195068e8e04eSSam Leffler  * Check the specified against any desired mode (aka netband).
195168e8e04eSSam Leffler  * This is only used (presently) when operating in hostap mode
195268e8e04eSSam Leffler  * to enforce consistency.
195368e8e04eSSam Leffler  */
195468e8e04eSSam Leffler static int
195568e8e04eSSam Leffler check_mode_consistency(const struct ieee80211_channel *c, int mode)
195668e8e04eSSam Leffler {
195768e8e04eSSam Leffler 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
195868e8e04eSSam Leffler 
195968e8e04eSSam Leffler 	switch (mode) {
196068e8e04eSSam Leffler 	case IEEE80211_MODE_11B:
196168e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_B(c));
196268e8e04eSSam Leffler 	case IEEE80211_MODE_11G:
196368e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
196468e8e04eSSam Leffler 	case IEEE80211_MODE_11A:
196568e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
196668e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
196768e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_STURBO(c));
196868e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
196968e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTA(c));
197068e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
197168e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTG(c));
197268e8e04eSSam Leffler 	}
197368e8e04eSSam Leffler 	return 1;
197468e8e04eSSam Leffler 
197568e8e04eSSam Leffler }
197668e8e04eSSam Leffler 
197768e8e04eSSam Leffler /*
197868e8e04eSSam Leffler  * Common code to set the current channel.  If the device
197968e8e04eSSam Leffler  * is up and running this may result in an immediate channel
198068e8e04eSSam Leffler  * change or a kick of the state machine.
198168e8e04eSSam Leffler  */
198268e8e04eSSam Leffler static int
1983b032f27cSSam Leffler setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
198468e8e04eSSam Leffler {
1985b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
198668e8e04eSSam Leffler 	int error;
198768e8e04eSSam Leffler 
198868e8e04eSSam Leffler 	if (c != IEEE80211_CHAN_ANYC) {
1989b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1990b032f27cSSam Leffler 			return EBUSY;	/* XXX better code? */
1991b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1992b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
199368e8e04eSSam Leffler 				return EINVAL;
1994b032f27cSSam Leffler 			if (!check_mode_consistency(c, vap->iv_des_mode))
1995b032f27cSSam Leffler 				return EINVAL;
1996b032f27cSSam Leffler 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1997b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1998b032f27cSSam Leffler 				return EINVAL;
1999b032f27cSSam Leffler 		}
20001b74d2a4SAdrian Chadd 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
2001b032f27cSSam Leffler 		    vap->iv_bss->ni_chan == c)
200268e8e04eSSam Leffler 			return 0;	/* NB: nothing to do */
200368e8e04eSSam Leffler 	}
2004b032f27cSSam Leffler 	vap->iv_des_chan = c;
200568e8e04eSSam Leffler 
200668e8e04eSSam Leffler 	error = 0;
2007b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
2008b032f27cSSam Leffler 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
200968e8e04eSSam Leffler 		/*
2010b032f27cSSam Leffler 		 * Monitor mode can switch directly.
201168e8e04eSSam Leffler 		 */
2012b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
2013b032f27cSSam Leffler 			/* XXX need state machine for other vap's to follow */
2014b032f27cSSam Leffler 			ieee80211_setcurchan(ic, vap->iv_des_chan);
2015b032f27cSSam Leffler 			vap->iv_bss->ni_chan = ic->ic_curchan;
2016d2ffc7afSPedro F. Giffuni 		} else {
2017b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
201826d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2019d2ffc7afSPedro F. Giffuni 		}
202068e8e04eSSam Leffler 	} else {
202168e8e04eSSam Leffler 		/*
202268e8e04eSSam Leffler 		 * Need to go through the state machine in case we
202368e8e04eSSam Leffler 		 * need to reassociate or the like.  The state machine
202468e8e04eSSam Leffler 		 * will pickup the desired channel and avoid scanning.
202568e8e04eSSam Leffler 		 */
2026b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
2027b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2028b032f27cSSam Leffler 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
202968e8e04eSSam Leffler 			/*
203068e8e04eSSam Leffler 			 * When not up+running and a real channel has
203168e8e04eSSam Leffler 			 * been specified fix the current channel so
203268e8e04eSSam Leffler 			 * there is immediate feedback; e.g. via ifconfig.
203368e8e04eSSam Leffler 			 */
2034b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
203526d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
203668e8e04eSSam Leffler 		}
203768e8e04eSSam Leffler 	}
203868e8e04eSSam Leffler 	return error;
203968e8e04eSSam Leffler }
204068e8e04eSSam Leffler 
204168e8e04eSSam Leffler /*
204268e8e04eSSam Leffler  * Old api for setting the current channel; this is
204368e8e04eSSam Leffler  * deprecated because channel numbers are ambiguous.
204468e8e04eSSam Leffler  */
204574bdb731SAndriy Voskoboinyk static int
2046b032f27cSSam Leffler ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
204768e8e04eSSam Leffler 	const struct ieee80211req *ireq)
204868e8e04eSSam Leffler {
2049b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
205068e8e04eSSam Leffler 	struct ieee80211_channel *c;
205168e8e04eSSam Leffler 
205268e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
205368e8e04eSSam Leffler 	if (ireq->i_val == 0 ||
205468e8e04eSSam Leffler 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
205568e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
205668e8e04eSSam Leffler 	} else {
205768e8e04eSSam Leffler 		struct ieee80211_channel *c2;
205868e8e04eSSam Leffler 
2059b032f27cSSam Leffler 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
206068e8e04eSSam Leffler 		if (c == NULL) {
206168e8e04eSSam Leffler 			c = findchannel(ic, ireq->i_val,
206268e8e04eSSam Leffler 				IEEE80211_MODE_AUTO);
206368e8e04eSSam Leffler 			if (c == NULL)
206468e8e04eSSam Leffler 				return EINVAL;
206568e8e04eSSam Leffler 		}
206635bcfd1cSAdrian Chadd 
206768e8e04eSSam Leffler 		/*
206868e8e04eSSam Leffler 		 * Fine tune channel selection based on desired mode:
206968e8e04eSSam Leffler 		 *   if 11b is requested, find the 11b version of any
207068e8e04eSSam Leffler 		 *      11g channel returned,
207168e8e04eSSam Leffler 		 *   if static turbo, find the turbo version of any
207268e8e04eSSam Leffler 		 *	11a channel return,
207368e8e04eSSam Leffler 		 *   if 11na is requested, find the ht version of any
207468e8e04eSSam Leffler 		 *      11a channel returned,
207568e8e04eSSam Leffler 		 *   if 11ng is requested, find the ht version of any
207668e8e04eSSam Leffler 		 *      11g channel returned,
207735bcfd1cSAdrian Chadd 		 *   if 11ac is requested, find the 11ac version
207835bcfd1cSAdrian Chadd 		 *      of any 11a/11na channel returned,
207935bcfd1cSAdrian Chadd 		 *   (TBD) 11acg (2GHz VHT)
208068e8e04eSSam Leffler 		 *   otherwise we should be ok with what we've got.
208168e8e04eSSam Leffler 		 */
2082b032f27cSSam Leffler 		switch (vap->iv_des_mode) {
208368e8e04eSSam Leffler 		case IEEE80211_MODE_11B:
208468e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
208568e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
208668e8e04eSSam Leffler 					IEEE80211_MODE_11B);
208768e8e04eSSam Leffler 				/* NB: should not happen, =>'s 11g w/o 11b */
208868e8e04eSSam Leffler 				if (c2 != NULL)
208968e8e04eSSam Leffler 					c = c2;
209068e8e04eSSam Leffler 			}
209168e8e04eSSam Leffler 			break;
209268e8e04eSSam Leffler 		case IEEE80211_MODE_TURBO_A:
209368e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
209468e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
209568e8e04eSSam Leffler 					IEEE80211_MODE_TURBO_A);
209668e8e04eSSam Leffler 				if (c2 != NULL)
209768e8e04eSSam Leffler 					c = c2;
209868e8e04eSSam Leffler 			}
209968e8e04eSSam Leffler 			break;
210068e8e04eSSam Leffler 		case IEEE80211_MODE_11NA:
210168e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
210268e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
210368e8e04eSSam Leffler 					IEEE80211_MODE_11NA);
210468e8e04eSSam Leffler 				if (c2 != NULL)
210568e8e04eSSam Leffler 					c = c2;
210668e8e04eSSam Leffler 			}
210768e8e04eSSam Leffler 			break;
210868e8e04eSSam Leffler 		case IEEE80211_MODE_11NG:
210968e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
211068e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
211168e8e04eSSam Leffler 					IEEE80211_MODE_11NG);
211268e8e04eSSam Leffler 				if (c2 != NULL)
211368e8e04eSSam Leffler 					c = c2;
211468e8e04eSSam Leffler 			}
211568e8e04eSSam Leffler 			break;
211635bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_2GHZ:
211735bcfd1cSAdrian Chadd 			printf("%s: TBD\n", __func__);
211835bcfd1cSAdrian Chadd 			break;
211935bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_5GHZ:
212035bcfd1cSAdrian Chadd 			if (IEEE80211_IS_CHAN_A(c)) {
212135bcfd1cSAdrian Chadd 				c2 = findchannel(ic, ireq->i_val,
212235bcfd1cSAdrian Chadd 					IEEE80211_MODE_VHT_5GHZ);
212335bcfd1cSAdrian Chadd 				if (c2 != NULL)
212435bcfd1cSAdrian Chadd 					c = c2;
212535bcfd1cSAdrian Chadd 			}
212635bcfd1cSAdrian Chadd 			break;
212768e8e04eSSam Leffler 		default:		/* NB: no static turboG */
212868e8e04eSSam Leffler 			break;
212968e8e04eSSam Leffler 		}
213068e8e04eSSam Leffler 	}
2131b032f27cSSam Leffler 	return setcurchan(vap, c);
213268e8e04eSSam Leffler }
213368e8e04eSSam Leffler 
213468e8e04eSSam Leffler /*
213568e8e04eSSam Leffler  * New/current api for setting the current channel; a complete
213668e8e04eSSam Leffler  * channel description is provide so there is no ambiguity in
213768e8e04eSSam Leffler  * identifying the channel.
213868e8e04eSSam Leffler  */
213974bdb731SAndriy Voskoboinyk static int
2140b032f27cSSam Leffler ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
214168e8e04eSSam Leffler 	const struct ieee80211req *ireq)
214268e8e04eSSam Leffler {
2143b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
214468e8e04eSSam Leffler 	struct ieee80211_channel chan, *c;
214568e8e04eSSam Leffler 	int error;
214668e8e04eSSam Leffler 
214768e8e04eSSam Leffler 	if (ireq->i_len != sizeof(chan))
214868e8e04eSSam Leffler 		return EINVAL;
214968e8e04eSSam Leffler 	error = copyin(ireq->i_data, &chan, sizeof(chan));
215068e8e04eSSam Leffler 	if (error != 0)
215168e8e04eSSam Leffler 		return error;
215235bcfd1cSAdrian Chadd 
215368e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
215468e8e04eSSam Leffler 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
215568e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
215668e8e04eSSam Leffler 	} else {
215768e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
215868e8e04eSSam Leffler 		if (c == NULL)
215968e8e04eSSam Leffler 			return EINVAL;
216068e8e04eSSam Leffler 	}
2161b032f27cSSam Leffler 	return setcurchan(vap, c);
2162b032f27cSSam Leffler }
2163b032f27cSSam Leffler 
216474bdb731SAndriy Voskoboinyk static int
2165b032f27cSSam Leffler ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2166b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2167b032f27cSSam Leffler {
2168b032f27cSSam Leffler 	struct ieee80211_regdomain_req *reg;
21698658b18bSSam Leffler 	int nchans, error;
2170b032f27cSSam Leffler 
21718658b18bSSam Leffler 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
21728658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
21738658b18bSSam Leffler 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
21748658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21758658b18bSSam Leffler 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
21768658b18bSSam Leffler 		    ireq->i_len, nchans);
2177b032f27cSSam Leffler 		return EINVAL;
21788658b18bSSam Leffler 	}
21798658b18bSSam Leffler 	reg = (struct ieee80211_regdomain_req *)
2180b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2181b9b53389SAdrian Chadd 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
21828658b18bSSam Leffler 	if (reg == NULL) {
21838658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21848658b18bSSam Leffler 		    "%s: no memory, nchans %d\n", __func__, nchans);
2185b032f27cSSam Leffler 		return ENOMEM;
21868658b18bSSam Leffler 	}
21878658b18bSSam Leffler 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
21888658b18bSSam Leffler 	if (error == 0) {
21898658b18bSSam Leffler 		/* NB: validate inline channel count against storage size */
21908658b18bSSam Leffler 		if (reg->chaninfo.ic_nchans != nchans) {
21918658b18bSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21928658b18bSSam Leffler 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
21938658b18bSSam Leffler 				reg->chaninfo.ic_nchans, nchans);
21948658b18bSSam Leffler 			error = EINVAL;
21958658b18bSSam Leffler 		} else
2196b032f27cSSam Leffler 			error = ieee80211_setregdomain(vap, reg);
21978658b18bSSam Leffler 	}
2198b9b53389SAdrian Chadd 	IEEE80211_FREE(reg, M_TEMP);
2199b032f27cSSam Leffler 
2200b032f27cSSam Leffler 	return (error == 0 ? ENETRESET : error);
220168e8e04eSSam Leffler }
220268e8e04eSSam Leffler 
220368e8e04eSSam Leffler static int
2204b032f27cSSam Leffler checkrate(const struct ieee80211_rateset *rs, int rate)
2205b032f27cSSam Leffler {
2206b032f27cSSam Leffler 	int i;
2207b032f27cSSam Leffler 
2208b032f27cSSam Leffler 	if (rate == IEEE80211_FIXED_RATE_NONE)
2209b032f27cSSam Leffler 		return 1;
2210b032f27cSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
2211b032f27cSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2212b032f27cSSam Leffler 			return 1;
2213b032f27cSSam Leffler 	return 0;
2214b032f27cSSam Leffler }
2215b032f27cSSam Leffler 
2216b032f27cSSam Leffler static int
2217dfabbaa0SAndriy Voskoboinyk checkmcs(const struct ieee80211_htrateset *rs, int mcs)
2218b032f27cSSam Leffler {
2219dfabbaa0SAndriy Voskoboinyk 	int rate_val = IEEE80211_RV(mcs);
2220dfabbaa0SAndriy Voskoboinyk 	int i;
2221dfabbaa0SAndriy Voskoboinyk 
2222b032f27cSSam Leffler 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2223b032f27cSSam Leffler 		return 1;
2224b032f27cSSam Leffler 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2225b032f27cSSam Leffler 		return 0;
2226dfabbaa0SAndriy Voskoboinyk 	for (i = 0; i < rs->rs_nrates; i++)
2227dfabbaa0SAndriy Voskoboinyk 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
2228dfabbaa0SAndriy Voskoboinyk 			return 1;
2229dfabbaa0SAndriy Voskoboinyk 	return 0;
2230b032f27cSSam Leffler }
2231b032f27cSSam Leffler 
223274bdb731SAndriy Voskoboinyk static int
2233545619f3SAndriy Voskoboinyk ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2234545619f3SAndriy Voskoboinyk         const struct ieee80211req *ireq)
2235545619f3SAndriy Voskoboinyk {
2236545619f3SAndriy Voskoboinyk 	struct ieee80211com *ic = vap->iv_ic;
2237545619f3SAndriy Voskoboinyk 	struct ieee80211_roamparams_req *parms;
2238545619f3SAndriy Voskoboinyk 	struct ieee80211_roamparam *src, *dst;
2239545619f3SAndriy Voskoboinyk 	const struct ieee80211_htrateset *rs_ht;
2240545619f3SAndriy Voskoboinyk 	const struct ieee80211_rateset *rs;
2241545619f3SAndriy Voskoboinyk 	int changed, error, mode, is11n, nmodes;
2242545619f3SAndriy Voskoboinyk 
2243545619f3SAndriy Voskoboinyk 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2244545619f3SAndriy Voskoboinyk 		return EINVAL;
2245545619f3SAndriy Voskoboinyk 
2246545619f3SAndriy Voskoboinyk 	parms = IEEE80211_MALLOC(sizeof(*parms), M_TEMP,
2247545619f3SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2248545619f3SAndriy Voskoboinyk 	if (parms == NULL)
2249545619f3SAndriy Voskoboinyk 		return ENOMEM;
2250545619f3SAndriy Voskoboinyk 
2251545619f3SAndriy Voskoboinyk 	error = copyin(ireq->i_data, parms, ireq->i_len);
2252545619f3SAndriy Voskoboinyk 	if (error != 0)
2253545619f3SAndriy Voskoboinyk 		goto fail;
2254545619f3SAndriy Voskoboinyk 
2255545619f3SAndriy Voskoboinyk 	changed = 0;
2256545619f3SAndriy Voskoboinyk 	nmodes = IEEE80211_MODE_MAX;
2257545619f3SAndriy Voskoboinyk 
2258545619f3SAndriy Voskoboinyk 	/* validate parameters and check if anything changed */
2259545619f3SAndriy Voskoboinyk 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2260545619f3SAndriy Voskoboinyk 		if (isclr(ic->ic_modecaps, mode))
2261545619f3SAndriy Voskoboinyk 			continue;
2262545619f3SAndriy Voskoboinyk 		src = &parms->params[mode];
2263545619f3SAndriy Voskoboinyk 		dst = &vap->iv_roamparms[mode];
2264545619f3SAndriy Voskoboinyk 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2265545619f3SAndriy Voskoboinyk 		rs_ht = &ic->ic_sup_htrates;
2266545619f3SAndriy Voskoboinyk 		is11n = (mode == IEEE80211_MODE_11NA ||
2267545619f3SAndriy Voskoboinyk 			 mode == IEEE80211_MODE_11NG);
2268545619f3SAndriy Voskoboinyk 		/* XXX TODO: 11ac */
2269545619f3SAndriy Voskoboinyk 		if (src->rate != dst->rate) {
2270545619f3SAndriy Voskoboinyk 			if (!checkrate(rs, src->rate) &&
2271545619f3SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->rate))) {
2272545619f3SAndriy Voskoboinyk 				error = EINVAL;
2273545619f3SAndriy Voskoboinyk 				goto fail;
2274545619f3SAndriy Voskoboinyk 			}
2275545619f3SAndriy Voskoboinyk 			changed++;
2276545619f3SAndriy Voskoboinyk 		}
2277545619f3SAndriy Voskoboinyk 		if (src->rssi != dst->rssi)
2278545619f3SAndriy Voskoboinyk 			changed++;
2279545619f3SAndriy Voskoboinyk 	}
2280545619f3SAndriy Voskoboinyk 	if (changed) {
2281545619f3SAndriy Voskoboinyk 		/*
2282545619f3SAndriy Voskoboinyk 		 * Copy new parameters in place and notify the
2283545619f3SAndriy Voskoboinyk 		 * driver so it can push state to the device.
2284545619f3SAndriy Voskoboinyk 		 */
2285545619f3SAndriy Voskoboinyk 		/* XXX locking? */
2286545619f3SAndriy Voskoboinyk 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2287545619f3SAndriy Voskoboinyk 			if (isset(ic->ic_modecaps, mode))
2288545619f3SAndriy Voskoboinyk 				vap->iv_roamparms[mode] = parms->params[mode];
2289545619f3SAndriy Voskoboinyk 		}
2290545619f3SAndriy Voskoboinyk 
2291545619f3SAndriy Voskoboinyk 		if (vap->iv_roaming == IEEE80211_ROAMING_DEVICE)
2292545619f3SAndriy Voskoboinyk 			error = ERESTART;
2293545619f3SAndriy Voskoboinyk 	}
2294545619f3SAndriy Voskoboinyk 
2295545619f3SAndriy Voskoboinyk fail:	IEEE80211_FREE(parms, M_TEMP);
2296545619f3SAndriy Voskoboinyk 	return error;
2297545619f3SAndriy Voskoboinyk }
2298545619f3SAndriy Voskoboinyk 
2299545619f3SAndriy Voskoboinyk static int
2300b032f27cSSam Leffler ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2301b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2302b032f27cSSam Leffler {
2303b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2304b032f27cSSam Leffler 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2305b032f27cSSam Leffler 	struct ieee80211_txparam *src, *dst;
2306dfabbaa0SAndriy Voskoboinyk 	const struct ieee80211_htrateset *rs_ht;
2307b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2308cbb24c24SSam Leffler 	int error, mode, changed, is11n, nmodes;
2309b032f27cSSam Leffler 
2310cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
2311cbb24c24SSam Leffler 	if (ireq->i_len > sizeof(parms))
2312b032f27cSSam Leffler 		return EINVAL;
2313cbb24c24SSam Leffler 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2314b032f27cSSam Leffler 	if (error != 0)
2315b032f27cSSam Leffler 		return error;
2316cbb24c24SSam Leffler 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2317b032f27cSSam Leffler 	changed = 0;
2318b032f27cSSam Leffler 	/* validate parameters and check if anything changed */
2319cbb24c24SSam Leffler 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2320cbb24c24SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
2321b032f27cSSam Leffler 			continue;
2322cbb24c24SSam Leffler 		src = &parms.params[mode];
2323cbb24c24SSam Leffler 		dst = &vap->iv_txparms[mode];
2324cbb24c24SSam Leffler 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2325dfabbaa0SAndriy Voskoboinyk 		rs_ht = &ic->ic_sup_htrates;
2326cbb24c24SSam Leffler 		is11n = (mode == IEEE80211_MODE_11NA ||
2327cbb24c24SSam Leffler 			 mode == IEEE80211_MODE_11NG);
2328b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2329cbb24c24SSam Leffler 			if (!checkrate(rs, src->ucastrate) &&
2330dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
2331b032f27cSSam Leffler 				return EINVAL;
2332b032f27cSSam Leffler 			changed++;
2333b032f27cSSam Leffler 		}
2334b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2335cbb24c24SSam Leffler 			if (!checkrate(rs, src->mcastrate) &&
2336dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
2337b032f27cSSam Leffler 				return EINVAL;
2338b032f27cSSam Leffler 			changed++;
2339b032f27cSSam Leffler 		}
2340b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2341cbb24c24SSam Leffler 			if (!checkrate(rs, src->mgmtrate) &&
2342dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
2343b032f27cSSam Leffler 				return EINVAL;
2344b032f27cSSam Leffler 			changed++;
2345b032f27cSSam Leffler 		}
2346b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2347b032f27cSSam Leffler 			changed++;
2348b032f27cSSam Leffler 	}
2349b032f27cSSam Leffler 	if (changed) {
2350b032f27cSSam Leffler 		/*
2351b032f27cSSam Leffler 		 * Copy new parameters in place and notify the
2352b032f27cSSam Leffler 		 * driver so it can push state to the device.
2353b032f27cSSam Leffler 		 */
2354cbb24c24SSam Leffler 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2355cbb24c24SSam Leffler 			if (isset(ic->ic_modecaps, mode))
2356cbb24c24SSam Leffler 				vap->iv_txparms[mode] = parms.params[mode];
2357b032f27cSSam Leffler 		}
2358b032f27cSSam Leffler 		/* XXX could be more intelligent,
2359b032f27cSSam Leffler 		   e.g. don't reset if setting not being used */
2360b032f27cSSam Leffler 		return ENETRESET;
2361b032f27cSSam Leffler 	}
2362b032f27cSSam Leffler 	return 0;
2363b032f27cSSam Leffler }
2364b032f27cSSam Leffler 
2365b032f27cSSam Leffler /*
2366b032f27cSSam Leffler  * Application Information Element support.
2367b032f27cSSam Leffler  */
2368b032f27cSSam Leffler static int
2369b032f27cSSam Leffler setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2370b032f27cSSam Leffler {
2371b032f27cSSam Leffler 	struct ieee80211_appie *app = *aie;
2372b032f27cSSam Leffler 	struct ieee80211_appie *napp;
2373b032f27cSSam Leffler 	int error;
2374b032f27cSSam Leffler 
2375b032f27cSSam Leffler 	if (ireq->i_len == 0) {		/* delete any existing ie */
2376b032f27cSSam Leffler 		if (app != NULL) {
2377b032f27cSSam Leffler 			*aie = NULL;	/* XXX racey */
2378b9b53389SAdrian Chadd 			IEEE80211_FREE(app, M_80211_NODE_IE);
2379b032f27cSSam Leffler 		}
2380b032f27cSSam Leffler 		return 0;
2381b032f27cSSam Leffler 	}
2382b032f27cSSam Leffler 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2383b032f27cSSam Leffler 		return EINVAL;
2384b032f27cSSam Leffler 	/*
2385b032f27cSSam Leffler 	 * Allocate a new appie structure and copy in the user data.
2386b032f27cSSam Leffler 	 * When done swap in the new structure.  Note that we do not
2387b032f27cSSam Leffler 	 * guard against users holding a ref to the old structure;
2388b032f27cSSam Leffler 	 * this must be handled outside this code.
2389b032f27cSSam Leffler 	 *
2390b032f27cSSam Leffler 	 * XXX bad bad bad
2391b032f27cSSam Leffler 	 */
2392b9b53389SAdrian Chadd 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2393b9b53389SAdrian Chadd 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2394b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT);
2395b032f27cSSam Leffler 	if (napp == NULL)
2396b032f27cSSam Leffler 		return ENOMEM;
2397b032f27cSSam Leffler 	/* XXX holding ic lock */
2398b032f27cSSam Leffler 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2399b032f27cSSam Leffler 	if (error) {
2400b9b53389SAdrian Chadd 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2401b032f27cSSam Leffler 		return error;
2402b032f27cSSam Leffler 	}
2403b032f27cSSam Leffler 	napp->ie_len = ireq->i_len;
2404b032f27cSSam Leffler 	*aie = napp;
2405b032f27cSSam Leffler 	if (app != NULL)
2406b9b53389SAdrian Chadd 		IEEE80211_FREE(app, M_80211_NODE_IE);
2407b032f27cSSam Leffler 	return 0;
2408b032f27cSSam Leffler }
2409b032f27cSSam Leffler 
2410b032f27cSSam Leffler static void
2411b032f27cSSam Leffler setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2412b032f27cSSam Leffler {
2413b032f27cSSam Leffler 	/* validate data is present as best we can */
2414b032f27cSSam Leffler 	if (space == 0 || 2+ie[1] > space)
2415b032f27cSSam Leffler 		return;
2416b032f27cSSam Leffler 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2417b032f27cSSam Leffler 		vap->iv_wpa_ie = ie;
2418b032f27cSSam Leffler 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2419b032f27cSSam Leffler 		vap->iv_rsn_ie = ie;
2420b032f27cSSam Leffler }
2421b032f27cSSam Leffler 
242274bdb731SAndriy Voskoboinyk static int
2423b032f27cSSam Leffler ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2424b032f27cSSam Leffler 	const struct ieee80211req *ireq, int fc0)
2425b032f27cSSam Leffler {
2426b032f27cSSam Leffler 	int error;
2427b032f27cSSam Leffler 
2428b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2429b032f27cSSam Leffler 
2430b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2431b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2432b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2433b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2434b032f27cSSam Leffler 			error = EINVAL;
2435b032f27cSSam Leffler 			break;
2436b032f27cSSam Leffler 		}
2437b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_beacon, ireq);
2438b032f27cSSam Leffler 		if (error == 0)
2439b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2440b032f27cSSam Leffler 		break;
2441b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2442b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_proberesp, ireq);
2443b032f27cSSam Leffler 		break;
2444b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2445b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2446b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocresp, ireq);
2447b032f27cSSam Leffler 		else
2448b032f27cSSam Leffler 			error = EINVAL;
2449b032f27cSSam Leffler 		break;
2450b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2451b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_probereq, ireq);
2452b032f27cSSam Leffler 		break;
2453b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2454b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
2455b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocreq, ireq);
2456b032f27cSSam Leffler 		else
2457b032f27cSSam Leffler 			error = EINVAL;
2458b032f27cSSam Leffler 		break;
2459b032f27cSSam Leffler 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2460b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_wpa, ireq);
2461b032f27cSSam Leffler 		if (error == 0) {
2462b032f27cSSam Leffler 			/*
2463b032f27cSSam Leffler 			 * Must split single blob of data into separate
2464b032f27cSSam Leffler 			 * WPA and RSN ie's because they go in different
2465b032f27cSSam Leffler 			 * locations in the mgt frames.
2466b032f27cSSam Leffler 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2467b032f27cSSam Leffler 			 */
2468b032f27cSSam Leffler 			vap->iv_wpa_ie = NULL;
2469b032f27cSSam Leffler 			vap->iv_rsn_ie = NULL;
2470b032f27cSSam Leffler 			if (vap->iv_appie_wpa != NULL) {
2471b032f27cSSam Leffler 				struct ieee80211_appie *appie =
2472b032f27cSSam Leffler 				    vap->iv_appie_wpa;
2473b032f27cSSam Leffler 				uint8_t *data = appie->ie_data;
2474b032f27cSSam Leffler 
2475b032f27cSSam Leffler 				/* XXX ie length validate is painful, cheat */
2476b032f27cSSam Leffler 				setwparsnie(vap, data, appie->ie_len);
2477b032f27cSSam Leffler 				setwparsnie(vap, data + 2 + data[1],
2478b032f27cSSam Leffler 				    appie->ie_len - (2 + data[1]));
2479b032f27cSSam Leffler 			}
2480b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2481b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2482b032f27cSSam Leffler 				/*
2483b032f27cSSam Leffler 				 * Must rebuild beacon frame as the update
2484b032f27cSSam Leffler 				 * mechanism doesn't handle WPA/RSN ie's.
2485b032f27cSSam Leffler 				 * Could extend it but it doesn't normally
2486b032f27cSSam Leffler 				 * change; this is just to deal with hostapd
2487b032f27cSSam Leffler 				 * plumbing the ie after the interface is up.
2488b032f27cSSam Leffler 				 */
2489b032f27cSSam Leffler 				error = ENETRESET;
2490b032f27cSSam Leffler 			}
2491b032f27cSSam Leffler 		}
2492b032f27cSSam Leffler 		break;
2493b032f27cSSam Leffler 	default:
2494b032f27cSSam Leffler 		error = EINVAL;
2495b032f27cSSam Leffler 		break;
2496b032f27cSSam Leffler 	}
2497b032f27cSSam Leffler 	return error;
2498b032f27cSSam Leffler }
2499b032f27cSSam Leffler 
250074bdb731SAndriy Voskoboinyk static int
2501b032f27cSSam Leffler ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2502b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2503b032f27cSSam Leffler {
2504b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2505b032f27cSSam Leffler 	int error;
2506b032f27cSSam Leffler 	uint8_t fc0;
2507b032f27cSSam Leffler 
2508b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
2509b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2510b032f27cSSam Leffler 		return EINVAL;
2511b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2512b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2513b032f27cSSam Leffler 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2514b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2515b032f27cSSam Leffler 	return error;
2516b032f27cSSam Leffler }
2517b032f27cSSam Leffler 
251874bdb731SAndriy Voskoboinyk static int
2519b032f27cSSam Leffler ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2520b032f27cSSam Leffler {
2521b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2522b032f27cSSam Leffler 	struct ieee80211_chanswitch_req csr;
2523b032f27cSSam Leffler 	struct ieee80211_channel *c;
2524b032f27cSSam Leffler 	int error;
2525b032f27cSSam Leffler 
2526b032f27cSSam Leffler 	if (ireq->i_len != sizeof(csr))
2527b032f27cSSam Leffler 		return EINVAL;
2528b032f27cSSam Leffler 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2529b032f27cSSam Leffler 	if (error != 0)
2530b032f27cSSam Leffler 		return error;
2531c70761e6SSam Leffler 	/* XXX adhoc mode not supported */
2532c70761e6SSam Leffler 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2533c70761e6SSam Leffler 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2534c70761e6SSam Leffler 		return EOPNOTSUPP;
2535b032f27cSSam Leffler 	c = ieee80211_find_channel(ic,
2536b032f27cSSam Leffler 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2537b032f27cSSam Leffler 	if (c == NULL)
2538b032f27cSSam Leffler 		return ENOENT;
2539b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2540b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2541b032f27cSSam Leffler 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2542c70761e6SSam Leffler 	else if (csr.csa_count == 0)
2543c70761e6SSam Leffler 		ieee80211_csa_cancelswitch(ic);
2544b032f27cSSam Leffler 	else
2545b032f27cSSam Leffler 		error = EBUSY;
2546b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2547b032f27cSSam Leffler 	return error;
2548b032f27cSSam Leffler }
2549b032f27cSSam Leffler 
2550632ee7e3SBernhard Schmidt static int
2551632ee7e3SBernhard Schmidt ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2552b032f27cSSam Leffler {
2553b032f27cSSam Leffler #define	IEEE80211_IOC_SCAN_FLAGS \
2554b032f27cSSam Leffler 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2555b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2556b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2557b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2558b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_CHECK)
2559b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25607ca7a42cSBernhard Schmidt 	int error, i;
2561b032f27cSSam Leffler 
2562b032f27cSSam Leffler 	/* convert duration */
2563632ee7e3SBernhard Schmidt 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2564632ee7e3SBernhard Schmidt 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2565b032f27cSSam Leffler 	else {
2566632ee7e3SBernhard Schmidt 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2567632ee7e3SBernhard Schmidt 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2568b032f27cSSam Leffler 			return EINVAL;
2569632ee7e3SBernhard Schmidt 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2570b032f27cSSam Leffler 	}
2571b032f27cSSam Leffler 	/* convert min/max channel dwell */
25729df9e936SAndriy Voskoboinyk 	if (sr->sr_mindwell != 0)
2573632ee7e3SBernhard Schmidt 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
25749df9e936SAndriy Voskoboinyk 	if (sr->sr_maxdwell != 0)
2575632ee7e3SBernhard Schmidt 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2576b032f27cSSam Leffler 	/* NB: silently reduce ssid count to what is supported */
2577632ee7e3SBernhard Schmidt 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2578632ee7e3SBernhard Schmidt 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2579632ee7e3SBernhard Schmidt 	for (i = 0; i < sr->sr_nssid; i++)
2580632ee7e3SBernhard Schmidt 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2581b032f27cSSam Leffler 			return EINVAL;
2582b032f27cSSam Leffler 	/* cleanse flags just in case, could reject if invalid flags */
2583632ee7e3SBernhard Schmidt 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2584b032f27cSSam Leffler 	/*
2585b032f27cSSam Leffler 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2586b032f27cSSam Leffler 	 * allows applications to scan without joining a bss (or picking
2587b032f27cSSam Leffler 	 * a channel and setting up a bss) and without forcing manual
2588b032f27cSSam Leffler 	 * roaming mode--you just need to mark the parent device UP.
2589b032f27cSSam Leffler 	 */
2590b032f27cSSam Leffler 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2591632ee7e3SBernhard Schmidt 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2592b032f27cSSam Leffler 
2593b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2594b032f27cSSam Leffler 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2595632ee7e3SBernhard Schmidt 	    __func__, sr->sr_flags,
2596b032f27cSSam Leffler 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2597632ee7e3SBernhard Schmidt 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2598b032f27cSSam Leffler 	/*
2599b032f27cSSam Leffler 	 * If we are in INIT state then the driver has never had a chance
2600b032f27cSSam Leffler 	 * to setup hardware state to do a scan; we must use the state
2601b032f27cSSam Leffler 	 * machine to get us up to the SCAN state but once we reach SCAN
2602b032f27cSSam Leffler 	 * state we then want to use the supplied params.  Stash the
2603b032f27cSSam Leffler 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2604b032f27cSSam Leffler 	 * state machines will recognize this and use the stashed params
2605b032f27cSSam Leffler 	 * to issue the scan request.
2606b032f27cSSam Leffler 	 *
2607b032f27cSSam Leffler 	 * Otherwise just invoke the scan machinery directly.
2608b032f27cSSam Leffler 	 */
2609b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2610f20503faSAndriy Voskoboinyk 	if (ic->ic_nrunning == 0) {
2611f20503faSAndriy Voskoboinyk 		IEEE80211_UNLOCK(ic);
2612f20503faSAndriy Voskoboinyk 		return ENXIO;
2613f20503faSAndriy Voskoboinyk 	}
2614f20503faSAndriy Voskoboinyk 
2615b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_INIT) {
2616b032f27cSSam Leffler 		/* NB: clobbers previous settings */
2617632ee7e3SBernhard Schmidt 		vap->iv_scanreq_flags = sr->sr_flags;
2618632ee7e3SBernhard Schmidt 		vap->iv_scanreq_duration = sr->sr_duration;
2619632ee7e3SBernhard Schmidt 		vap->iv_scanreq_nssid = sr->sr_nssid;
2620632ee7e3SBernhard Schmidt 		for (i = 0; i < sr->sr_nssid; i++) {
2621632ee7e3SBernhard Schmidt 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2622632ee7e3SBernhard Schmidt 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2623632ee7e3SBernhard Schmidt 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2624b032f27cSSam Leffler 		}
2625b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2626b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2627b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2628b032f27cSSam Leffler 	} else {
2629b032f27cSSam Leffler 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2630b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2631632ee7e3SBernhard Schmidt 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
26327ca7a42cSBernhard Schmidt 			error = ieee80211_check_scan(vap, sr->sr_flags,
2633632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2634632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2635b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2636632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2637b032f27cSSam Leffler 		} else {
26387ca7a42cSBernhard Schmidt 			error = ieee80211_start_scan(vap, sr->sr_flags,
2639632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2640632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2641b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2642632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2643b032f27cSSam Leffler 		}
26447ca7a42cSBernhard Schmidt 		if (error == 0)
26457ca7a42cSBernhard Schmidt 			return EINPROGRESS;
2646b032f27cSSam Leffler 	}
2647632ee7e3SBernhard Schmidt 	return 0;
2648b032f27cSSam Leffler #undef IEEE80211_IOC_SCAN_FLAGS
2649b032f27cSSam Leffler }
2650b032f27cSSam Leffler 
265174bdb731SAndriy Voskoboinyk static int
2652632ee7e3SBernhard Schmidt ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2653632ee7e3SBernhard Schmidt {
265474bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
2655632ee7e3SBernhard Schmidt 	int error;
2656632ee7e3SBernhard Schmidt 
265774bdb731SAndriy Voskoboinyk 	if (ireq->i_len != sizeof(*sr))
2658632ee7e3SBernhard Schmidt 		return EINVAL;
265974bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
266074bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
266174bdb731SAndriy Voskoboinyk 	if (sr == NULL)
266274bdb731SAndriy Voskoboinyk 		return ENOMEM;
266374bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, sr, sizeof(*sr));
2664632ee7e3SBernhard Schmidt 	if (error != 0)
266574bdb731SAndriy Voskoboinyk 		goto bad;
266674bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
266774bdb731SAndriy Voskoboinyk bad:
266874bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
2669632ee7e3SBernhard Schmidt 	return error;
2670632ee7e3SBernhard Schmidt }
2671632ee7e3SBernhard Schmidt 
267274bdb731SAndriy Voskoboinyk static int
2673b032f27cSSam Leffler ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2674b032f27cSSam Leffler {
2675b032f27cSSam Leffler 	struct ieee80211_node *ni;
2676b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
2677b032f27cSSam Leffler 	int error;
2678b032f27cSSam Leffler 
2679b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
2680b032f27cSSam Leffler 		return EINVAL;
2681b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2682b032f27cSSam Leffler 	if (error != 0)
2683b032f27cSSam Leffler 		return error;
2684b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2685b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2686b032f27cSSam Leffler 		    vlan.sv_macaddr);
2687b032f27cSSam Leffler 		if (ni == NULL)
2688b032f27cSSam Leffler 			return ENOENT;
2689b032f27cSSam Leffler 	} else
2690b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
2691b032f27cSSam Leffler 	ni->ni_vlan = vlan.sv_vlan;
2692b032f27cSSam Leffler 	ieee80211_free_node(ni);
2693b032f27cSSam Leffler 	return error;
2694b032f27cSSam Leffler }
2695b032f27cSSam Leffler 
2696b032f27cSSam Leffler static int
2697b032f27cSSam Leffler isvap11g(const struct ieee80211vap *vap)
2698b032f27cSSam Leffler {
2699b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2700b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2701b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2702b032f27cSSam Leffler }
2703b032f27cSSam Leffler 
2704b032f27cSSam Leffler static int
2705b032f27cSSam Leffler isvapht(const struct ieee80211vap *vap)
2706b032f27cSSam Leffler {
2707b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2708b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2709b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2710b032f27cSSam Leffler }
2711b032f27cSSam Leffler 
27128c4e758aSSam Leffler /*
27138c4e758aSSam Leffler  * Dummy ioctl set handler so the linker set is defined.
27148c4e758aSSam Leffler  */
27158c4e758aSSam Leffler static int
27168c4e758aSSam Leffler dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
27178c4e758aSSam Leffler {
27188c4e758aSSam Leffler 	return ENOSYS;
27198c4e758aSSam Leffler }
27208c4e758aSSam Leffler IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
27218c4e758aSSam Leffler 
27228c4e758aSSam Leffler static int
27238c4e758aSSam Leffler ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
27248c4e758aSSam Leffler {
27258c4e758aSSam Leffler 	ieee80211_ioctl_setfunc * const *set;
27268c4e758aSSam Leffler 	int error;
27278c4e758aSSam Leffler 
27288c4e758aSSam Leffler 	SET_FOREACH(set, ieee80211_ioctl_setset) {
27298c4e758aSSam Leffler 		error = (*set)(vap, ireq);
27308c4e758aSSam Leffler 		if (error != ENOSYS)
27318c4e758aSSam Leffler 			return error;
27328c4e758aSSam Leffler 	}
27338c4e758aSSam Leffler 	return EINVAL;
27348c4e758aSSam Leffler }
27358c4e758aSSam Leffler 
273674bdb731SAndriy Voskoboinyk static int
2737b032f27cSSam Leffler ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2738b032f27cSSam Leffler {
2739b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
27408a1b9b6aSSam Leffler 	int error;
27418a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
274268e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
27438a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
274468e8e04eSSam Leffler 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
27458a1b9b6aSSam Leffler 	struct ieee80211_key *k;
27468a1b9b6aSSam Leffler 	u_int kid;
27473dfff737SWarner Losh 	uint32_t flags;
27488a1b9b6aSSam Leffler 
27498a1b9b6aSSam Leffler 	error = 0;
27501a1e1d21SSam Leffler 	switch (ireq->i_type) {
27511a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
27521a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
27538a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
27548a1b9b6aSSam Leffler 			return EINVAL;
27551a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
27561a1e1d21SSam Leffler 		if (error)
27571a1e1d21SSam Leffler 			break;
2758b032f27cSSam Leffler 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2759b032f27cSSam Leffler 		vap->iv_des_ssid[0].len = ireq->i_len;
2760b032f27cSSam Leffler 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2761b032f27cSSam Leffler 		vap->iv_des_nssid = (ireq->i_len > 0);
2762b032f27cSSam Leffler 		error = ENETRESET;
27631a1e1d21SSam Leffler 		break;
27641a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
27658a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27668a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
2767b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2768b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27698a1b9b6aSSam Leffler 			break;
27708a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
2771b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2772b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
27738a1b9b6aSSam Leffler 			break;
27748a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
2775b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2776b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27778a1b9b6aSSam Leffler 			break;
27781a1e1d21SSam Leffler 		}
2779b032f27cSSam Leffler 		error = ENETRESET;
27801a1e1d21SSam Leffler 		break;
27811a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
27821a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
27838a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
27848a1b9b6aSSam Leffler 			return EINVAL;
2785b032f27cSSam Leffler 		k = &vap->iv_nw_keys[kid];
27868a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
27878a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
2788b032f27cSSam Leffler 			(void) ieee80211_crypto_delkey(vap, k);
27891a1e1d21SSam Leffler 			break;
27901a1e1d21SSam Leffler 		}
27918a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
27928a1b9b6aSSam Leffler 			return EINVAL;
27931a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
27941a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
27951a1e1d21SSam Leffler 		if (error)
27961a1e1d21SSam Leffler 			break;
2797b032f27cSSam Leffler 		ieee80211_key_update_begin(vap);
2798dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2799b032f27cSSam Leffler 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2800dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
28018a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
28028a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
280371fe06caSSam Leffler 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
280471fe06caSSam Leffler 			if  (!ieee80211_crypto_setkey(vap, k))
28058a1b9b6aSSam Leffler 				error = EINVAL;
28068a1b9b6aSSam Leffler 		} else
28078a1b9b6aSSam Leffler 			error = EINVAL;
2808b032f27cSSam Leffler 		ieee80211_key_update_end(vap);
28091a1e1d21SSam Leffler 		break;
28101a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
28111a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2812380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
281368e8e04eSSam Leffler 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
28148a1b9b6aSSam Leffler 			return EINVAL;
2815781487cfSAdrian Chadd 		/*
2816781487cfSAdrian Chadd 		 * Firmware devices may need to be told about an explicit
2817781487cfSAdrian Chadd 		 * key index here, versus just inferring it from the
2818781487cfSAdrian Chadd 		 * key set / change.  Since we may also need to pause
2819781487cfSAdrian Chadd 		 * things like transmit before the key is updated,
2820781487cfSAdrian Chadd 		 * give the driver a chance to flush things by tying
2821781487cfSAdrian Chadd 		 * into key update begin/end.
2822781487cfSAdrian Chadd 		 */
2823781487cfSAdrian Chadd 		ieee80211_key_update_begin(vap);
2824781487cfSAdrian Chadd 		ieee80211_crypto_set_deftxkey(vap, kid);
2825781487cfSAdrian Chadd 		ieee80211_key_update_end(vap);
28268a1b9b6aSSam Leffler 		break;
28278a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
28288a1b9b6aSSam Leffler 		switch (ireq->i_val) {
28298a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
28308a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
28318a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
28328a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
28338a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
28348a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
28358a1b9b6aSSam Leffler 			if (auth == NULL)
28368a1b9b6aSSam Leffler 				return EINVAL;
28378a1b9b6aSSam Leffler 			break;
28388a1b9b6aSSam Leffler 		default:
28398a1b9b6aSSam Leffler 			return EINVAL;
28408a1b9b6aSSam Leffler 		}
28418a1b9b6aSSam Leffler 		switch (ireq->i_val) {
28428a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2843b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28448a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
28458a1b9b6aSSam Leffler 			break;
28468a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
2847b032f27cSSam Leffler 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
28488a1b9b6aSSam Leffler 			break;
28498a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
28508a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2851b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28528a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
2853b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28548a1b9b6aSSam Leffler 			break;
28558a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
2856b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28578a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
28588a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
28591a1e1d21SSam Leffler 			break;
28601a1e1d21SSam Leffler 		}
28618a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
2862b032f27cSSam Leffler 		vap->iv_bss->ni_authmode = ireq->i_val;
28638a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
2864b032f27cSSam Leffler 		vap->iv_auth = auth;
2865b032f27cSSam Leffler 		error = ENETRESET;
28661a1e1d21SSam Leffler 		break;
28671a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
2868b032f27cSSam Leffler 		error = ieee80211_ioctl_setchannel(vap, ireq);
28691a1e1d21SSam Leffler 		break;
28701a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
28711a1e1d21SSam Leffler 		switch (ireq->i_val) {
28721a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
2873b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2874b032f27cSSam Leffler 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2875b032f27cSSam Leffler 				error = ERESTART;
28761a1e1d21SSam Leffler 			}
28771a1e1d21SSam Leffler 			break;
28781a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
2879b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2880b032f27cSSam Leffler 				error = EOPNOTSUPP;
2881b032f27cSSam Leffler 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2882b032f27cSSam Leffler 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2883b032f27cSSam Leffler 				error = ERESTART;
28841a1e1d21SSam Leffler 			}
28851a1e1d21SSam Leffler 			break;
28861a1e1d21SSam Leffler 		default:
28871a1e1d21SSam Leffler 			error = EINVAL;
28881a1e1d21SSam Leffler 			break;
28891a1e1d21SSam Leffler 		}
28901a1e1d21SSam Leffler 		break;
28911a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
28928a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
28938a1b9b6aSSam Leffler 			return EINVAL;
28941a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
2895b032f27cSSam Leffler 		error = ERESTART;
28961a1e1d21SSam Leffler 		break;
289713604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
289870231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
289970231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
29008a1b9b6aSSam Leffler 			return EINVAL;
2901b032f27cSSam Leffler 		vap->iv_rtsthreshold = ireq->i_val;
2902b032f27cSSam Leffler 		error = ERESTART;
29031a1e1d21SSam Leffler 		break;
29042e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
29058a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
29068a1b9b6aSSam Leffler 			return EINVAL;
2907f1481c8dSAdrian Chadd 		vap->iv_protmode = (enum ieee80211_protmode)ireq->i_val;
29082e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
290968e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
291068e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2911b032f27cSSam Leffler 			error = ERESTART;
2912f1481c8dSAdrian Chadd 		/* driver callback for protection mode update */
2913f1481c8dSAdrian Chadd 		ieee80211_vap_update_erp_protmode(vap);
29142e79ca97SSam Leffler 		break;
29152e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
29168a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2917b032f27cSSam Leffler 			return EOPNOTSUPP;
291868e8e04eSSam Leffler 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
291968e8e04eSSam Leffler 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
29208a1b9b6aSSam Leffler 			return EINVAL;
29218a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
2922b032f27cSSam Leffler 		error = ERESTART;
29238a1b9b6aSSam Leffler 		break;
29248a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
29258a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
29268a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
29278a1b9b6aSSam Leffler 			return EINVAL;
2928fcd9500fSBernhard Schmidt 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
29298a1b9b6aSSam Leffler 		/* XXXX reset? */
29308a1b9b6aSSam Leffler 		break;
29318a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
29328a1b9b6aSSam Leffler 		if (ireq->i_val) {
29338a1b9b6aSSam Leffler 			/* XXX check for key state? */
2934b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
29358a1b9b6aSSam Leffler 		} else
2936b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2937b032f27cSSam Leffler 		/* XXX ERESTART? */
29388a1b9b6aSSam Leffler 		break;
29398a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
29408a1b9b6aSSam Leffler 		if (ireq->i_val)
2941b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
29428a1b9b6aSSam Leffler 		else
2943b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2944b032f27cSSam Leffler 		/* XXX ERESTART? */
29458a1b9b6aSSam Leffler 		break;
29468a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
2947b032f27cSSam Leffler 		error = ieee80211_ioctl_setkey(vap, ireq);
29488a1b9b6aSSam Leffler 		break;
29498a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
2950b032f27cSSam Leffler 		error = ieee80211_ioctl_delkey(vap, ireq);
29518a1b9b6aSSam Leffler 		break;
29528a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
2953b032f27cSSam Leffler 		error = ieee80211_ioctl_setmlme(vap, ireq);
29548a1b9b6aSSam Leffler 		break;
29558a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
29568a1b9b6aSSam Leffler 		if (ireq->i_val) {
2957b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2958b032f27cSSam Leffler 				return EOPNOTSUPP;
2959b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_COUNTERM;
29608a1b9b6aSSam Leffler 		} else
2961b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2962b032f27cSSam Leffler 		/* XXX ERESTART? */
29638a1b9b6aSSam Leffler 		break;
29648a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
29658a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
29668a1b9b6aSSam Leffler 			return EINVAL;
29678a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
29683dfff737SWarner Losh 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
29698a1b9b6aSSam Leffler 		switch (ireq->i_val) {
297091356585SAdrian Chadd 		case 0:
297191356585SAdrian Chadd 			/* wpa_supplicant calls this to clear the WPA config */
297291356585SAdrian Chadd 			break;
29738a1b9b6aSSam Leffler 		case 1:
29743dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
29753dfff737SWarner Losh 				return EOPNOTSUPP;
29763dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1;
29778a1b9b6aSSam Leffler 			break;
29788a1b9b6aSSam Leffler 		case 2:
29793dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
29803dfff737SWarner Losh 				return EOPNOTSUPP;
29813dfff737SWarner Losh 			flags |= IEEE80211_F_WPA2;
29828a1b9b6aSSam Leffler 			break;
29838a1b9b6aSSam Leffler 		case 3:
29843dfff737SWarner Losh 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
29858d092df9SSam Leffler 				return EOPNOTSUPP;
29863dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
29872e79ca97SSam Leffler 			break;
29883dfff737SWarner Losh 		default:	/*  Can't set any -> error */
29893dfff737SWarner Losh 			return EOPNOTSUPP;
29902e79ca97SSam Leffler 		}
29913dfff737SWarner Losh 		vap->iv_flags = flags;
2992b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
29938a1b9b6aSSam Leffler 		break;
29948a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
29958a1b9b6aSSam Leffler 		if (ireq->i_val) {
2996b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2997b032f27cSSam Leffler 				return EOPNOTSUPP;
2998b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_WME);
29998a1b9b6aSSam Leffler 		} else
3000b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
3001b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
30028a1b9b6aSSam Leffler 		break;
30038a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
30048a1b9b6aSSam Leffler 		if (ireq->i_val)
3005b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_HIDESSID;
30068a1b9b6aSSam Leffler 		else
3007b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
3008b032f27cSSam Leffler 		error = ERESTART;		/* XXX ENETRESET? */
30092e79ca97SSam Leffler 		break;
30108a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
30118a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
3012b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
30138a1b9b6aSSam Leffler 		else
3014b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
30158a1b9b6aSSam Leffler 		break;
30168a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
30178a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
30188a1b9b6aSSam Leffler 			return EINVAL;
30198a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
30208a1b9b6aSSam Leffler 		if (error)
30218a1b9b6aSSam Leffler 			break;
3022b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
3023b032f27cSSam Leffler 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
3024b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
30258a1b9b6aSSam Leffler 		else
3026b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DESBSSID;
3027b032f27cSSam Leffler 		error = ENETRESET;
30288a1b9b6aSSam Leffler 		break;
30298a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
3030b032f27cSSam Leffler 		error = ieee80211_ioctl_setchanlist(vap, ireq);
30318a1b9b6aSSam Leffler 		break;
3032b032f27cSSam Leffler #define	OLD_IEEE80211_IOC_SCAN_REQ	23
3033b032f27cSSam Leffler #ifdef OLD_IEEE80211_IOC_SCAN_REQ
3034b032f27cSSam Leffler 	case OLD_IEEE80211_IOC_SCAN_REQ:
3035b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3036b032f27cSSam Leffler 			"%s: active scan request\n", __func__);
3037b032f27cSSam Leffler 		/*
3038b032f27cSSam Leffler 		 * If we are in INIT state then the driver has never
3039b032f27cSSam Leffler 		 * had a chance to setup hardware state to do a scan;
3040b032f27cSSam Leffler 		 * use the state machine to get us up the SCAN state.
3041b032f27cSSam Leffler 		 * Otherwise just invoke the scan machinery to start
3042b032f27cSSam Leffler 		 * a one-time scan.
3043b032f27cSSam Leffler 		 */
3044b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
3045b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
3046b032f27cSSam Leffler 		else
3047b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap,
304868e8e04eSSam Leffler 				IEEE80211_SCAN_ACTIVE |
304968e8e04eSSam Leffler 				IEEE80211_SCAN_NOPICK |
3050b032f27cSSam Leffler 				IEEE80211_SCAN_ONCE,
3051b032f27cSSam Leffler 				IEEE80211_SCAN_FOREVER, 0, 0,
305268e8e04eSSam Leffler 				/* XXX use ioctl params */
3053b032f27cSSam Leffler 				vap->iv_des_nssid, vap->iv_des_ssid);
3054b032f27cSSam Leffler 		break;
3055b032f27cSSam Leffler #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
3056b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
3057b032f27cSSam Leffler 		error = ieee80211_ioctl_scanreq(vap, ireq);
3058b032f27cSSam Leffler 		break;
3059b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_CANCEL:
3060b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3061b032f27cSSam Leffler 		    "%s: cancel scan\n", __func__);
3062b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);
3063b032f27cSSam Leffler 		break;
3064b032f27cSSam Leffler 	case IEEE80211_IOC_HTCONF:
3065b032f27cSSam Leffler 		if (ireq->i_val & 1)
30662bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
3067b032f27cSSam Leffler 		else
30682bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
3069b032f27cSSam Leffler 		if (ireq->i_val & 2)
30702bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
3071b032f27cSSam Leffler 		else
30722bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
3073b032f27cSSam Leffler 		error = ENETRESET;
30748a1b9b6aSSam Leffler 		break;
30758a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
30768a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
3077b032f27cSSam Leffler 		error = ieee80211_ioctl_macmac(vap, ireq);
30788a1b9b6aSSam Leffler 		break;
30798a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
3080b032f27cSSam Leffler 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
30818a1b9b6aSSam Leffler 		break;
308242568791SSam Leffler 	case IEEE80211_IOC_STA_STATS:
3083b032f27cSSam Leffler 		error = ieee80211_ioctl_setstastats(vap, ireq);
308442568791SSam Leffler 		break;
30858a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
3086b032f27cSSam Leffler 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
30878a1b9b6aSSam Leffler 		break;
30888a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
30898a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
30908a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
30918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
30928a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
3093fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
3094b032f27cSSam Leffler 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
30958a1b9b6aSSam Leffler 		break;
30968a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
3097b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
309859aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3099b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
31008a1b9b6aSSam Leffler 			return EINVAL;
31018a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
31028a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
3103b032f27cSSam Leffler 			vap->iv_dtim_period = ireq->i_val;
31048a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
31058a1b9b6aSSam Leffler 		} else
31068a1b9b6aSSam Leffler 			error = EINVAL;
31078a1b9b6aSSam Leffler 		break;
31088a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
3109b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
311059aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3111b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
31128a1b9b6aSSam Leffler 			return EINVAL;
31138a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
31148a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
3115d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
31168a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
31178a1b9b6aSSam Leffler 		} else
31188a1b9b6aSSam Leffler 			error = EINVAL;
31198a1b9b6aSSam Leffler 		break;
3120c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
3121c4f040c3SSam Leffler 		if (ireq->i_val)
3122b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PUREG;
3123c4f040c3SSam Leffler 		else
3124b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PUREG;
3125c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
3126b032f27cSSam Leffler 		if (isvap11g(vap))
3127c4f040c3SSam Leffler 			error = ENETRESET;
3128c4f040c3SSam Leffler 		break;
312932b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
313032b0e64bSAdrian Chadd 		vap->iv_quiet= ireq->i_val;
313132b0e64bSAdrian Chadd 		break;
313232b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
313332b0e64bSAdrian Chadd 		vap->iv_quiet_count=ireq->i_val;
313432b0e64bSAdrian Chadd 		break;
313532b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
313632b0e64bSAdrian Chadd 		vap->iv_quiet_period=ireq->i_val;
313732b0e64bSAdrian Chadd 		break;
313832b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
313932b0e64bSAdrian Chadd 		vap->iv_quiet_offset=ireq->i_val;
314032b0e64bSAdrian Chadd 		break;
314132b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
314232b0e64bSAdrian Chadd 		if(ireq->i_val < vap->iv_bss->ni_intval)
314332b0e64bSAdrian Chadd 			vap->iv_quiet_duration = ireq->i_val;
314432b0e64bSAdrian Chadd 		else
314532b0e64bSAdrian Chadd 			error = EINVAL;
314632b0e64bSAdrian Chadd 		break;
314768e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
314868e8e04eSSam Leffler 		if (ireq->i_val) {
3149b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3150b032f27cSSam Leffler 				return EOPNOTSUPP;
3151b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_BGSCAN;
315268e8e04eSSam Leffler 		} else
3153b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
315468e8e04eSSam Leffler 		break;
315568e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
315668e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3157b032f27cSSam Leffler 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
315868e8e04eSSam Leffler 		else
315968e8e04eSSam Leffler 			error = EINVAL;
316068e8e04eSSam Leffler 		break;
316168e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
316268e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3163b032f27cSSam Leffler 			vap->iv_bgscanintvl = ireq->i_val*hz;
316468e8e04eSSam Leffler 		else
316568e8e04eSSam Leffler 			error = EINVAL;
316668e8e04eSSam Leffler 		break;
316768e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
316868e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3169b032f27cSSam Leffler 			vap->iv_scanvalid = ireq->i_val*hz;
317068e8e04eSSam Leffler 		else
317168e8e04eSSam Leffler 			error = EINVAL;
317268e8e04eSSam Leffler 		break;
317370231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
3174b032f27cSSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
317570231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
3176b032f27cSSam Leffler 			return EOPNOTSUPP;
317770231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
317870231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
317970231e3dSSam Leffler 			return EINVAL;
3180b032f27cSSam Leffler 		vap->iv_fragthreshold = ireq->i_val;
3181b032f27cSSam Leffler 		error = ERESTART;
318270231e3dSSam Leffler 		break;
3183c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
3184c27e4e31SSam Leffler 		if (ireq->i_val) {
3185b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3186b032f27cSSam Leffler 				return EOPNOTSUPP;
3187b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3188c27e4e31SSam Leffler 		} else
3189b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3190b032f27cSSam Leffler 		error = ERESTART;
3191c27e4e31SSam Leffler 		break;
3192546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
3193546786c9SSam Leffler 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3194546786c9SSam Leffler 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3195546786c9SSam Leffler 			return EINVAL;
3196b032f27cSSam Leffler 		vap->iv_bmissthreshold = ireq->i_val;
3197b032f27cSSam Leffler 		error = ERESTART;
3198546786c9SSam Leffler 		break;
319968e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
3200b032f27cSSam Leffler 		error = ieee80211_ioctl_setcurchan(vap, ireq);
320168e8e04eSSam Leffler 		break;
320268e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
320368e8e04eSSam Leffler 		if (ireq->i_val) {
320468e8e04eSSam Leffler #define	IEEE80211_HTCAP_SHORTGI \
320568e8e04eSSam Leffler 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3206b032f27cSSam Leffler 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
320768e8e04eSSam Leffler 				return EINVAL;
320868e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
32092bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
321068e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
32112bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
321268e8e04eSSam Leffler #undef IEEE80211_HTCAP_SHORTGI
321368e8e04eSSam Leffler 		} else
32142bfc8a91SSam Leffler 			vap->iv_flags_ht &=
32152bfc8a91SSam Leffler 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3216b032f27cSSam Leffler 		error = ERESTART;
321768e8e04eSSam Leffler 		break;
321868e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
3219b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
322068e8e04eSSam Leffler 			return EINVAL;
322168e8e04eSSam Leffler 		if (ireq->i_val & 1)
32222bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3223b032f27cSSam Leffler 		else
32242bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
322568e8e04eSSam Leffler 		if (ireq->i_val & 2)
32262bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3227b032f27cSSam Leffler 		else
32282bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
322968e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3230b032f27cSSam Leffler 		if (isvapht(vap))
3231b032f27cSSam Leffler 			error = ERESTART;
323268e8e04eSSam Leffler 		break;
323368e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
3234781487cfSAdrian Chadd 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
3235b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3236b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3237b032f27cSSam Leffler 			return EINVAL;
3238b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3239b032f27cSSam Leffler 			vap->iv_ampdu_rxmax = ireq->i_val;
3240b032f27cSSam Leffler 		else
3241b032f27cSSam Leffler 			vap->iv_ampdu_limit = ireq->i_val;
3242b032f27cSSam Leffler 		error = ERESTART;
324368e8e04eSSam Leffler 		break;
324468e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
3245b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3246b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3247b032f27cSSam Leffler 			return EINVAL;
3248b032f27cSSam Leffler 		vap->iv_ampdu_density = ireq->i_val;
3249b032f27cSSam Leffler 		error = ERESTART;
325068e8e04eSSam Leffler 		break;
325168e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
3252b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
325368e8e04eSSam Leffler 			return EINVAL;
325468e8e04eSSam Leffler 		if (ireq->i_val & 1)
32552bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3256b032f27cSSam Leffler 		else
32572bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
325868e8e04eSSam Leffler 		if (ireq->i_val & 2)
32592bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3260b032f27cSSam Leffler 		else
32612bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
326268e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3263b032f27cSSam Leffler 		if (isvapht(vap))
3264b032f27cSSam Leffler 			error = ERESTART;
326568e8e04eSSam Leffler 		break;
326668e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
326768e8e04eSSam Leffler 		/* XXX validate */
3268b032f27cSSam Leffler 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
326968e8e04eSSam Leffler 		break;
327068e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
327168e8e04eSSam Leffler 		if (ireq->i_val) {
32722bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
327368e8e04eSSam Leffler 				return EINVAL;
32742bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
327568e8e04eSSam Leffler 		} else
32762bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
327768e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3278b032f27cSSam Leffler 		if (isvapht(vap))
3279b032f27cSSam Leffler 			error = ERESTART;
328068e8e04eSSam Leffler 		break;
328168e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
328268e8e04eSSam Leffler 		if (ireq->i_val) {
328368e8e04eSSam Leffler #if 0
328468e8e04eSSam Leffler 			/* XXX no capability */
3285b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3286b032f27cSSam Leffler 				return EOPNOTSUPP;
328768e8e04eSSam Leffler #endif
3288b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DOTH;
328968e8e04eSSam Leffler 		} else
3290b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3291b032f27cSSam Leffler 		error = ENETRESET;
3292b032f27cSSam Leffler 		break;
3293b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
3294b032f27cSSam Leffler 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3295b032f27cSSam Leffler 		break;
3296b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
3297b032f27cSSam Leffler 		error = ieee80211_ioctl_setroam(vap, ireq);
3298b032f27cSSam Leffler 		break;
3299b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
3300b032f27cSSam Leffler 		error = ieee80211_ioctl_settxparams(vap, ireq);
330168e8e04eSSam Leffler 		break;
330268e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
330368e8e04eSSam Leffler 		if (ireq->i_val) {
33042bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3305b032f27cSSam Leffler 				return EOPNOTSUPP;
33062bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
330768e8e04eSSam Leffler 		} else
33082bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
330968e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3310b032f27cSSam Leffler 		if (isvapht(vap))
3311b032f27cSSam Leffler 			error = ERESTART;
3312b032f27cSSam Leffler 		break;
3313b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
3314b032f27cSSam Leffler 		if (ireq->i_val) {
3315b032f27cSSam Leffler 			/* NB: DWDS only makes sense for WDS-capable devices */
3316b032f27cSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3317b032f27cSSam Leffler 				return EOPNOTSUPP;
3318b032f27cSSam Leffler 			/* NB: DWDS is used only with ap+sta vaps */
3319b032f27cSSam Leffler 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3320b032f27cSSam Leffler 			    vap->iv_opmode != IEEE80211_M_STA)
3321b032f27cSSam Leffler 				return EINVAL;
3322b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DWDS;
3323f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3324f2a6a13cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3325f2a6a13cSSam Leffler 		} else {
3326b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3327f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3328f2a6a13cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3329f2a6a13cSSam Leffler 		}
333068e8e04eSSam Leffler 		break;
3331c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
3332c066143cSSam Leffler 		if (ireq->i_val)
3333b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3334c066143cSSam Leffler 		else
3335b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3336b032f27cSSam Leffler 		break;
3337b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
3338b032f27cSSam Leffler 		error = ieee80211_ioctl_setappie(vap, ireq);
3339b032f27cSSam Leffler 		break;
3340b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
3341b032f27cSSam Leffler 		if (ireq->i_val) {
3342b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3343b032f27cSSam Leffler 				return EOPNOTSUPP;
3344b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3345b032f27cSSam Leffler 		} else
3346b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3347b032f27cSSam Leffler 		break;
3348b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
3349b032f27cSSam Leffler 		if (ireq->i_val) {
3350b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3351b032f27cSSam Leffler 				return EOPNOTSUPP;
3352b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3353b032f27cSSam Leffler 		} else
3354b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3355b032f27cSSam Leffler 		break;
3356b032f27cSSam Leffler 	case IEEE80211_IOC_CHANSWITCH:
3357b032f27cSSam Leffler 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3358b032f27cSSam Leffler 		break;
3359b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
3360b032f27cSSam Leffler 		if (ireq->i_val) {
3361b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3362b032f27cSSam Leffler 				return EOPNOTSUPP;
3363b032f27cSSam Leffler 			/* NB: DFS requires 11h support */
3364b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3365b032f27cSSam Leffler 				return EINVAL;
3366b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3367b032f27cSSam Leffler 		} else
3368b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3369b032f27cSSam Leffler 		break;
3370b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
3371b032f27cSSam Leffler 		if (ireq->i_val)
3372b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3373b032f27cSSam Leffler 		else
3374b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3375b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
3376b032f27cSSam Leffler 			error = ENETRESET;
3377c066143cSSam Leffler 		break;
33781b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
33791b6167d2SSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
33801b6167d2SSam Leffler 			return EINVAL;
3381f1481c8dSAdrian Chadd 		vap->iv_htprotmode = ireq->i_val ?
33821b6167d2SSam Leffler 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
33831b6167d2SSam Leffler 		/* NB: if not operating in 11n this can wait */
3384b032f27cSSam Leffler 		if (isvapht(vap))
33851b6167d2SSam Leffler 			error = ERESTART;
3386f1481c8dSAdrian Chadd 		/* Notify driver layer of HT protmode changes */
3387f1481c8dSAdrian Chadd 		ieee80211_vap_update_ht_protmode(vap);
33881b6167d2SSam Leffler 		break;
3389b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
3390b032f27cSSam Leffler 		error = ieee80211_ioctl_setstavlan(vap, ireq);
33911b6167d2SSam Leffler 		break;
33928c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
33938c070d69SSam Leffler 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
33948c070d69SSam Leffler 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
33958c070d69SSam Leffler 			return EINVAL;
33968c070d69SSam Leffler 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
33978c070d69SSam Leffler 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
33988c070d69SSam Leffler 			return EOPNOTSUPP;
33998c070d69SSam Leffler 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
34008c070d69SSam Leffler 			ireq->i_val;
34018c070d69SSam Leffler 		/* NB: if not operating in 11n this can wait */
34028c070d69SSam Leffler 		if (isvapht(vap))
34038c070d69SSam Leffler 			error = ERESTART;
34048c070d69SSam Leffler 		break;
340544f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
340644f7a6edSSam Leffler 		if (ireq->i_val != 0) {
340744f7a6edSSam Leffler 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
340844f7a6edSSam Leffler 				return EOPNOTSUPP;
34092bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
341044f7a6edSSam Leffler 		} else
34112bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
341244f7a6edSSam Leffler 		/* NB: if not operating in 11n this can wait */
341344f7a6edSSam Leffler 		if (isvapht(vap))
341444f7a6edSSam Leffler 			error = ERESTART;
341544f7a6edSSam Leffler 		break;
3416ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
3417ec3763bbSAdrian Chadd 		/* Check if we can do STBC TX/RX before changing the setting */
3418ec3763bbSAdrian Chadd 		if ((ireq->i_val & 1) &&
3419ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
3420ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3421ec3763bbSAdrian Chadd 		if ((ireq->i_val & 2) &&
3422ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
3423ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3424ec3763bbSAdrian Chadd 
3425ec3763bbSAdrian Chadd 		/* TX */
3426ec3763bbSAdrian Chadd 		if (ireq->i_val & 1)
3427ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
3428ec3763bbSAdrian Chadd 		else
3429ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
3430ec3763bbSAdrian Chadd 
3431ec3763bbSAdrian Chadd 		/* RX */
3432ec3763bbSAdrian Chadd 		if (ireq->i_val & 2)
3433ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
3434ec3763bbSAdrian Chadd 		else
3435ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
3436ec3763bbSAdrian Chadd 
3437ec3763bbSAdrian Chadd 		/* NB: reset only if we're operating on an 11n channel */
3438ec3763bbSAdrian Chadd 		if (isvapht(vap))
3439ec3763bbSAdrian Chadd 			error = ERESTART;
3440ec3763bbSAdrian Chadd 		break;
3441c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
3442c5bba9daSAndriy Voskoboinyk 		/* Check if we can do LDPC TX/RX before changing the setting */
3443c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 1) &&
3444c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0)
3445c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3446c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 2) &&
3447c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0)
3448c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3449c5bba9daSAndriy Voskoboinyk 
3450c5bba9daSAndriy Voskoboinyk 		/* TX */
3451c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 1)
3452c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
3453c5bba9daSAndriy Voskoboinyk 		else
3454c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX;
3455c5bba9daSAndriy Voskoboinyk 
3456c5bba9daSAndriy Voskoboinyk 		/* RX */
3457c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 2)
3458c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
3459c5bba9daSAndriy Voskoboinyk 		else
3460c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX;
3461c5bba9daSAndriy Voskoboinyk 
3462c5bba9daSAndriy Voskoboinyk 		/* NB: reset only if we're operating on an 11n channel */
3463c5bba9daSAndriy Voskoboinyk 		if (isvapht(vap))
3464c5bba9daSAndriy Voskoboinyk 			error = ERESTART;
3465c5bba9daSAndriy Voskoboinyk 		break;
34668379e8dbSAdrian Chadd 	case IEEE80211_IOC_UAPSD:
34678379e8dbSAdrian Chadd 		if ((vap->iv_caps & IEEE80211_C_UAPSD) == 0)
34688379e8dbSAdrian Chadd 			return EOPNOTSUPP;
34698379e8dbSAdrian Chadd 		if (ireq->i_val == 0)
34708379e8dbSAdrian Chadd 			vap->iv_flags_ext &= ~IEEE80211_FEXT_UAPSD;
34718379e8dbSAdrian Chadd 		else if (ireq->i_val == 1)
34728379e8dbSAdrian Chadd 			vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
34738379e8dbSAdrian Chadd 		else
34748379e8dbSAdrian Chadd 			return EINVAL;
34758379e8dbSAdrian Chadd 		break;
347635bcfd1cSAdrian Chadd 
347735bcfd1cSAdrian Chadd 	/* VHT */
347835bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
347996811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_VHT)
348035bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT);
348135bcfd1cSAdrian Chadd 		else
348235bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT);
348335bcfd1cSAdrian Chadd 
348496811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT40)
348535bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40);
348635bcfd1cSAdrian Chadd 		else
348735bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40);
348835bcfd1cSAdrian Chadd 
348996811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT80)
349035bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80);
349135bcfd1cSAdrian Chadd 		else
349235bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80);
349335bcfd1cSAdrian Chadd 
349496811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT160)
349535bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160);
349635bcfd1cSAdrian Chadd 		else
349735bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160);
349835bcfd1cSAdrian Chadd 
349996811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT80P80)
350096811be2SBjoern A. Zeeb 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80);
350196811be2SBjoern A. Zeeb 		else
350296811be2SBjoern A. Zeeb 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80);
350396811be2SBjoern A. Zeeb 
350435bcfd1cSAdrian Chadd 		error = ENETRESET;
350535bcfd1cSAdrian Chadd 		break;
350635bcfd1cSAdrian Chadd 
35071a1e1d21SSam Leffler 	default:
35088c4e758aSSam Leffler 		error = ieee80211_ioctl_setdefault(vap, ireq);
35091a1e1d21SSam Leffler 		break;
35101a1e1d21SSam Leffler 	}
3511b032f27cSSam Leffler 	/*
3512b032f27cSSam Leffler 	 * The convention is that ENETRESET means an operation
3513b032f27cSSam Leffler 	 * requires a complete re-initialization of the device (e.g.
3514b032f27cSSam Leffler 	 * changing something that affects the association state).
3515b032f27cSSam Leffler 	 * ERESTART means the request may be handled with only a
3516b032f27cSSam Leffler 	 * reload of the hardware state.  We hand ERESTART requests
3517b032f27cSSam Leffler 	 * to the iv_reset callback so the driver can decide.  If
3518b032f27cSSam Leffler 	 * a device does not fillin iv_reset then it defaults to one
3519b032f27cSSam Leffler 	 * that returns ENETRESET.  Otherwise a driver may return
3520b032f27cSSam Leffler 	 * ENETRESET (in which case a full reset will be done) or
3521b032f27cSSam Leffler 	 * 0 to mean there's no need to do anything (e.g. when the
3522b032f27cSSam Leffler 	 * change has no effect on the driver/device).
3523b032f27cSSam Leffler 	 */
3524b032f27cSSam Leffler 	if (error == ERESTART)
3525b032f27cSSam Leffler 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3526b032f27cSSam Leffler 		    vap->iv_reset(vap, ireq->i_type) : 0;
3527b032f27cSSam Leffler 	if (error == ENETRESET) {
3528b032f27cSSam Leffler 		/* XXX need to re-think AUTO handling */
3529b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
3530b032f27cSSam Leffler 			ieee80211_init(vap);
3531b032f27cSSam Leffler 		error = 0;
3532b032f27cSSam Leffler 	}
35338a1b9b6aSSam Leffler 	return error;
35348a1b9b6aSSam Leffler }
35358a1b9b6aSSam Leffler 
3536b032f27cSSam Leffler int
3537b032f27cSSam Leffler ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3538b032f27cSSam Leffler {
35391da89db5SSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
35401da89db5SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3541dab61567SAndriy Voskoboinyk 	int error = 0, wait = 0, ic_used;
35428a1b9b6aSSam Leffler 	struct ifreq *ifr;
35438a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
35448a1b9b6aSSam Leffler 
3545dab61567SAndriy Voskoboinyk 	ic_used = (cmd != SIOCSIFMTU && cmd != SIOCG80211STATS);
3546dab61567SAndriy Voskoboinyk 	if (ic_used && (error = ieee80211_com_vincref(vap)) != 0)
3547dab61567SAndriy Voskoboinyk 		return (error);
3548dab61567SAndriy Voskoboinyk 
35498a1b9b6aSSam Leffler 	switch (cmd) {
3550b032f27cSSam Leffler 	case SIOCSIFFLAGS:
3551b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
35526459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
35536459bd28SAndriy Voskoboinyk 			/*
35546459bd28SAndriy Voskoboinyk 			 * Enable promiscuous mode when:
35556459bd28SAndriy Voskoboinyk 			 * 1. Interface is not a member of bridge, or
35566459bd28SAndriy Voskoboinyk 			 * 2. Requested by user, or
35576459bd28SAndriy Voskoboinyk 			 * 3. In monitor (or adhoc-demo) mode.
35586459bd28SAndriy Voskoboinyk 			 */
35596459bd28SAndriy Voskoboinyk 			if (ifp->if_bridge == NULL ||
35606459bd28SAndriy Voskoboinyk 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
35616459bd28SAndriy Voskoboinyk 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
35626459bd28SAndriy Voskoboinyk 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
35636459bd28SAndriy Voskoboinyk 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
35646459bd28SAndriy Voskoboinyk 				ieee80211_promisc(vap,
35656459bd28SAndriy Voskoboinyk 				    ifp->if_flags & IFF_PROMISC);
35666459bd28SAndriy Voskoboinyk 				vap->iv_ifflags ^= IFF_PROMISC;
35676459bd28SAndriy Voskoboinyk 			}
35686459bd28SAndriy Voskoboinyk 		}
35696459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
35707a79cebfSGleb Smirnoff 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
35716459bd28SAndriy Voskoboinyk 			vap->iv_ifflags ^= IFF_ALLMULTI;
35726459bd28SAndriy Voskoboinyk 		}
3573b032f27cSSam Leffler 		if (ifp->if_flags & IFF_UP) {
3574b032f27cSSam Leffler 			/*
3575b032f27cSSam Leffler 			 * Bring ourself up unless we're already operational.
3576b032f27cSSam Leffler 			 * If we're the first vap and the parent is not up
3577b032f27cSSam Leffler 			 * then it will automatically be brought up as a
3578b032f27cSSam Leffler 			 * side-effect of bringing ourself up.
3579b032f27cSSam Leffler 			 */
358060d75eb6SAndriy Voskoboinyk 			if (vap->iv_state == IEEE80211_S_INIT) {
358160d75eb6SAndriy Voskoboinyk 				if (ic->ic_nrunning == 0)
358260d75eb6SAndriy Voskoboinyk 					wait = 1;
3583b032f27cSSam Leffler 				ieee80211_start_locked(vap);
358460d75eb6SAndriy Voskoboinyk 			}
3585b032f27cSSam Leffler 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3586b032f27cSSam Leffler 			/*
3587b032f27cSSam Leffler 			 * Stop ourself.  If we are the last vap to be
3588b032f27cSSam Leffler 			 * marked down the parent will also be taken down.
3589b032f27cSSam Leffler 			 */
359060d75eb6SAndriy Voskoboinyk 			if (ic->ic_nrunning == 1)
359160d75eb6SAndriy Voskoboinyk 				wait = 1;
3592b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
3593b032f27cSSam Leffler 		}
3594b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
35958d9b29f3SAndrew Thompson 		/* Wait for parent ioctl handler if it was queued */
3596c6a35ee4SAndriy Voskoboinyk 		if (wait) {
35972b60ecf1SGleb Smirnoff 			struct epoch_tracker et;
35982b60ecf1SGleb Smirnoff 
3599ae55932eSAndrew Thompson 			ieee80211_waitfor_parent(ic);
3600c6a35ee4SAndriy Voskoboinyk 
3601c6a35ee4SAndriy Voskoboinyk 			/*
3602c6a35ee4SAndriy Voskoboinyk 			 * Check if the MAC address was changed
3603c6a35ee4SAndriy Voskoboinyk 			 * via SIOCSIFLLADDR ioctl.
36047071b803SAndriy Voskoboinyk 			 *
36057071b803SAndriy Voskoboinyk 			 * NB: device may be detached during initialization;
36067071b803SAndriy Voskoboinyk 			 * use if_ioctl for existence check.
3607c6a35ee4SAndriy Voskoboinyk 			 */
36082b60ecf1SGleb Smirnoff 			NET_EPOCH_ENTER(et);
36097071b803SAndriy Voskoboinyk 			if (ifp->if_ioctl == ieee80211_ioctl &&
36107071b803SAndriy Voskoboinyk 			    (ifp->if_flags & IFF_UP) == 0 &&
3611c6a35ee4SAndriy Voskoboinyk 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
3612c6a35ee4SAndriy Voskoboinyk 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
3613c6a35ee4SAndriy Voskoboinyk 				    IF_LLADDR(ifp));
36142b60ecf1SGleb Smirnoff 			NET_EPOCH_EXIT(et);
3615c6a35ee4SAndriy Voskoboinyk 		}
3616b032f27cSSam Leffler 		break;
3617b032f27cSSam Leffler 	case SIOCADDMULTI:
3618b032f27cSSam Leffler 	case SIOCDELMULTI:
36197a79cebfSGleb Smirnoff 		ieee80211_runtask(ic, &ic->ic_mcast_task);
3620b032f27cSSam Leffler 		break;
36218a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
36228a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
3623b032f27cSSam Leffler 		ifr = (struct ifreq *)data;
3624b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
36258a1b9b6aSSam Leffler 		break;
36268a1b9b6aSSam Leffler 	case SIOCG80211:
3627b032f27cSSam Leffler 		error = ieee80211_ioctl_get80211(vap, cmd,
36288a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
36298a1b9b6aSSam Leffler 		break;
36308a1b9b6aSSam Leffler 	case SIOCS80211:
3631*01e57940SBjoern A. Zeeb 		error = ieee80211_priv_check_vap_manage(cmd, vap, ifp);
36328a1b9b6aSSam Leffler 		if (error == 0)
3633b032f27cSSam Leffler 			error = ieee80211_ioctl_set80211(vap, cmd,
36348a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
36351a1e1d21SSam Leffler 		break;
36361be50176SSam Leffler 	case SIOCG80211STATS:
36371be50176SSam Leffler 		ifr = (struct ifreq *)data;
3638541d96aaSBrooks Davis 		copyout(&vap->iv_stats, ifr_data_get_ptr(ifr),
3639541d96aaSBrooks Davis 		    sizeof (vap->iv_stats));
36401be50176SSam Leffler 		break;
36416f161f03SSam Leffler 	case SIOCSIFMTU:
36426f161f03SSam Leffler 		ifr = (struct ifreq *)data;
36436f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
36446f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
36456f161f03SSam Leffler 			error = EINVAL;
36466f161f03SSam Leffler 		else
36476f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
36486f161f03SSam Leffler 		break;
3649b2e95691SSam Leffler 	case SIOCSIFADDR:
3650b2e95691SSam Leffler 		/*
3651a4641f4eSPedro F. Giffuni 		 * XXX Handle this directly so we can suppress if_init calls.
3652b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
3653b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
3654a4641f4eSPedro F. Giffuni 		 * XXX set IFF_UP and so suppress if_init being called when
3655b2e95691SSam Leffler 		 * XXX it should be.
3656b2e95691SSam Leffler 		 */
3657b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
3658b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
3659b2e95691SSam Leffler #ifdef INET
3660b2e95691SSam Leffler 		case AF_INET:
3661b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3662b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3663b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3664b2e95691SSam Leffler 			}
3665b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
3666b2e95691SSam Leffler 			break;
3667b2e95691SSam Leffler #endif
3668b2e95691SSam Leffler 		default:
3669b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3670b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3671b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3672b2e95691SSam Leffler 			}
3673b2e95691SSam Leffler 			break;
3674b2e95691SSam Leffler 		}
3675b2e95691SSam Leffler 		break;
3676f7d38a13SAdrian Chadd 	case SIOCSIFLLADDR:
3677*01e57940SBjoern A. Zeeb 		error = ieee80211_priv_check_vap_setmac(cmd, vap, ifp);
3678f7d38a13SAdrian Chadd 		if (error == 0)
3679f7d38a13SAdrian Chadd 			break;
3680f7d38a13SAdrian Chadd 		/* Fallthrough */
3681ba2c1fbcSAdrian Chadd 	default:
36827a79cebfSGleb Smirnoff 		/*
36837a79cebfSGleb Smirnoff 		 * Pass unknown ioctls first to the driver, and if it
36847a79cebfSGleb Smirnoff 		 * returns ENOTTY, then to the generic Ethernet handler.
36857a79cebfSGleb Smirnoff 		 */
36867a79cebfSGleb Smirnoff 		if (ic->ic_ioctl != NULL &&
36877a79cebfSGleb Smirnoff 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
36887a79cebfSGleb Smirnoff 			break;
36891a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
36901a1e1d21SSam Leffler 		break;
36911a1e1d21SSam Leffler 	}
3692dab61567SAndriy Voskoboinyk 
3693dab61567SAndriy Voskoboinyk 	if (ic_used)
3694dab61567SAndriy Voskoboinyk 		ieee80211_com_vdecref(vap);
3695dab61567SAndriy Voskoboinyk 
36967a79cebfSGleb Smirnoff 	return (error);
36971a1e1d21SSam Leffler }
3698