xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
11a1e1d21SSam Leffler /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
513d0d5b21SJustin Hibbits #include <net/if_private.h>
521a1e1d21SSam Leffler #include <net/ethernet.h>
531a1e1d21SSam Leffler 
54b2e95691SSam Leffler #ifdef INET
55b2e95691SSam Leffler #include <netinet/in.h>
56b2e95691SSam Leffler #include <netinet/if_ether.h>
57b2e95691SSam Leffler #endif
58b2e95691SSam Leffler 
591a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
601a1e1d21SSam Leffler #include <net80211/ieee80211_ioctl.h>
61b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
62b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
631a1e1d21SSam Leffler 
64b032f27cSSam Leffler #define	IS_UP_AUTO(_vap) \
653cdd9880SSam Leffler 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
66b032f27cSSam Leffler 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
678a1b9b6aSSam Leffler 
68b032f27cSSam Leffler static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
6968e8e04eSSam Leffler static struct ieee80211_channel *findchannel(struct ieee80211com *,
7068e8e04eSSam Leffler 		int ieee, int mode);
71632ee7e3SBernhard Schmidt static int ieee80211_scanreq(struct ieee80211vap *,
72632ee7e3SBernhard Schmidt 		struct ieee80211_scan_req *);
731a1e1d21SSam Leffler 
7474bdb731SAndriy Voskoboinyk static int
7501e57940SBjoern A. Zeeb ieee80211_ioctl_getkey(u_long cmd, struct ieee80211vap *vap,
7601e57940SBjoern A. Zeeb     struct ieee80211req *ireq)
778a1b9b6aSSam Leffler {
78b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
798a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
808a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
818a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
828a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
838a1b9b6aSSam Leffler 	u_int kid;
848a1b9b6aSSam Leffler 	int error;
858a1b9b6aSSam Leffler 
868a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
878a1b9b6aSSam Leffler 		return EINVAL;
888a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
898a1b9b6aSSam Leffler 	if (error)
908a1b9b6aSSam Leffler 		return error;
918a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
928a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
93b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
948a1b9b6aSSam Leffler 		if (ni == NULL)
95b032f27cSSam Leffler 			return ENOENT;
968a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
978a1b9b6aSSam Leffler 	} else {
988a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
998a1b9b6aSSam Leffler 			return EINVAL;
100b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
101b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
1028a1b9b6aSSam Leffler 		ni = NULL;
1038a1b9b6aSSam Leffler 	}
1048a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
1058a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
1068a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
1078a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
108b032f27cSSam Leffler 	if (wk->wk_keyix == vap->iv_def_txkey)
1098a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
11001e57940SBjoern A. Zeeb 	if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
1118a1b9b6aSSam Leffler 		/* NB: only root can read key data */
112b032f27cSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
1138a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
1148a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
1158a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
1168a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
1178a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
1188a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
1198a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
1208a1b9b6aSSam Leffler 		}
1218a1b9b6aSSam Leffler 	} else {
1228a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
1238a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
1248a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
1258a1b9b6aSSam Leffler 	}
1268a1b9b6aSSam Leffler 	if (ni != NULL)
1278a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
1288a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
1298a1b9b6aSSam Leffler }
1308a1b9b6aSSam Leffler 
13174bdb731SAndriy Voskoboinyk static int
132b032f27cSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1338a1b9b6aSSam Leffler {
134b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1358a1b9b6aSSam Leffler 
136e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
1378a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1388a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
1398a1b9b6aSSam Leffler }
1408a1b9b6aSSam Leffler 
14174bdb731SAndriy Voskoboinyk static int
142b032f27cSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
1438a1b9b6aSSam Leffler {
144b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
145db9ff08bSKevin Lo 	uint32_t space;
1468a1b9b6aSSam Leffler 
1478a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
14868e8e04eSSam Leffler 			ic_chans[ic->ic_nchans]);
1498a1b9b6aSSam Leffler 	if (space > ireq->i_len)
1508a1b9b6aSSam Leffler 		space = ireq->i_len;
15168e8e04eSSam Leffler 	/* XXX assumes compatible layout */
15268e8e04eSSam Leffler 	return copyout(&ic->ic_nchans, ireq->i_data, space);
1538a1b9b6aSSam Leffler }
1548a1b9b6aSSam Leffler 
15574bdb731SAndriy Voskoboinyk static int
156b032f27cSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
157b032f27cSSam Leffler 	struct ieee80211req *ireq, int req)
1588a1b9b6aSSam Leffler {
1598a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16074bdb731SAndriy Voskoboinyk 	struct ieee80211req_wpaie2 *wpaie;
1618a1b9b6aSSam Leffler 	int error;
1628a1b9b6aSSam Leffler 
1638a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1648a1b9b6aSSam Leffler 		return EINVAL;
16574bdb731SAndriy Voskoboinyk 	wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP,
16674bdb731SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
16774bdb731SAndriy Voskoboinyk 	if (wpaie == NULL)
16874bdb731SAndriy Voskoboinyk 		return ENOMEM;
16974bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN);
1708a1b9b6aSSam Leffler 	if (error != 0)
17174bdb731SAndriy Voskoboinyk 		goto bad;
17274bdb731SAndriy Voskoboinyk 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr);
17374bdb731SAndriy Voskoboinyk 	if (ni == NULL) {
17474bdb731SAndriy Voskoboinyk 		error = ENOENT;
17574bdb731SAndriy Voskoboinyk 		goto bad;
17674bdb731SAndriy Voskoboinyk 	}
177b032f27cSSam Leffler 	if (ni->ni_ies.wpa_ie != NULL) {
178b032f27cSSam Leffler 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
17974bdb731SAndriy Voskoboinyk 		if (ielen > sizeof(wpaie->wpa_ie))
18074bdb731SAndriy Voskoboinyk 			ielen = sizeof(wpaie->wpa_ie);
18174bdb731SAndriy Voskoboinyk 		memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen);
1828a1b9b6aSSam Leffler 	}
18368e8e04eSSam Leffler 	if (req == IEEE80211_IOC_WPAIE2) {
184b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
185b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
18674bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->rsn_ie))
18774bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->rsn_ie);
18874bdb731SAndriy Voskoboinyk 			memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen);
18968e8e04eSSam Leffler 		}
19068e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
19168e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
19268e8e04eSSam Leffler 	} else {
19368e8e04eSSam Leffler 		/* compatibility op, may overwrite wpa ie */
19468e8e04eSSam Leffler 		/* XXX check ic_flags? */
195b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
196b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
19774bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->wpa_ie))
19874bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->wpa_ie);
19974bdb731SAndriy Voskoboinyk 			memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen);
20068e8e04eSSam Leffler 		}
20168e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
20268e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
20368e8e04eSSam Leffler 	}
2048a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20574bdb731SAndriy Voskoboinyk 	error = copyout(wpaie, ireq->i_data, ireq->i_len);
20674bdb731SAndriy Voskoboinyk bad:
20774bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(wpaie, M_TEMP);
20874bdb731SAndriy Voskoboinyk 	return error;
2098a1b9b6aSSam Leffler }
2108a1b9b6aSSam Leffler 
21174bdb731SAndriy Voskoboinyk static int
212b032f27cSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
2138a1b9b6aSSam Leffler {
2148a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
21568e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
216db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
2178a1b9b6aSSam Leffler 	int error;
2188a1b9b6aSSam Leffler 
2198a1b9b6aSSam Leffler 	if (ireq->i_len < off)
2208a1b9b6aSSam Leffler 		return EINVAL;
2218a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
2228a1b9b6aSSam Leffler 	if (error != 0)
2238a1b9b6aSSam Leffler 		return error;
224b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
22568e8e04eSSam Leffler 	if (ni == NULL)
226b032f27cSSam Leffler 		return ENOENT;
2278a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
2288a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
2298a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
23068e8e04eSSam Leffler 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
2318a1b9b6aSSam Leffler 			ireq->i_len - off);
2328a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2338a1b9b6aSSam Leffler 	return error;
2348a1b9b6aSSam Leffler }
2358a1b9b6aSSam Leffler 
23668e8e04eSSam Leffler struct scanreq {
23768e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
23868e8e04eSSam Leffler 	size_t space;
23968e8e04eSSam Leffler };
24068e8e04eSSam Leffler 
24168e8e04eSSam Leffler static size_t
24268e8e04eSSam Leffler scan_space(const struct ieee80211_scan_entry *se, int *ielen)
24368e8e04eSSam Leffler {
24468e8e04eSSam Leffler 	size_t len;
24568e8e04eSSam Leffler 
246b032f27cSSam Leffler 	*ielen = se->se_ies.len;
24768e8e04eSSam Leffler 	/*
24868e8e04eSSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
24968e8e04eSSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
25068e8e04eSSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
25168e8e04eSSam Leffler 	 */
25259aa14a9SRui Paulo 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
25359aa14a9SRui Paulo 	    se->se_meshid[1] + *ielen;
25468e8e04eSSam Leffler 	return roundup(len, sizeof(uint32_t));
25568e8e04eSSam Leffler }
25668e8e04eSSam Leffler 
25768e8e04eSSam Leffler static void
25868e8e04eSSam Leffler get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
25968e8e04eSSam Leffler {
26068e8e04eSSam Leffler 	struct scanreq *req = arg;
26168e8e04eSSam Leffler 	int ielen;
26268e8e04eSSam Leffler 
26368e8e04eSSam Leffler 	req->space += scan_space(se, &ielen);
26468e8e04eSSam Leffler }
26568e8e04eSSam Leffler 
26674bdb731SAndriy Voskoboinyk static void
26768e8e04eSSam Leffler get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
26868e8e04eSSam Leffler {
26968e8e04eSSam Leffler 	struct scanreq *req = arg;
27068e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
27168e8e04eSSam Leffler 	int ielen, len, nr, nxr;
27268e8e04eSSam Leffler 	uint8_t *cp;
27368e8e04eSSam Leffler 
27468e8e04eSSam Leffler 	len = scan_space(se, &ielen);
27568e8e04eSSam Leffler 	if (len > req->space)
27668e8e04eSSam Leffler 		return;
27768e8e04eSSam Leffler 
27868e8e04eSSam Leffler 	sr = req->sr;
27968e8e04eSSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535,
28068e8e04eSSam Leffler 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
281b032f27cSSam Leffler 	sr->isr_len = len;
28268e8e04eSSam Leffler 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
28368e8e04eSSam Leffler 	sr->isr_ie_len = ielen;
28468e8e04eSSam Leffler 	sr->isr_freq = se->se_chan->ic_freq;
28568e8e04eSSam Leffler 	sr->isr_flags = se->se_chan->ic_flags;
28668e8e04eSSam Leffler 	sr->isr_rssi = se->se_rssi;
28768e8e04eSSam Leffler 	sr->isr_noise = se->se_noise;
28868e8e04eSSam Leffler 	sr->isr_intval = se->se_intval;
28968e8e04eSSam Leffler 	sr->isr_capinfo = se->se_capinfo;
29068e8e04eSSam Leffler 	sr->isr_erp = se->se_erp;
29168e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
29268e8e04eSSam Leffler 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
29368e8e04eSSam Leffler 	memcpy(sr->isr_rates, se->se_rates+2, nr);
29468e8e04eSSam Leffler 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
29568e8e04eSSam Leffler 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
29668e8e04eSSam Leffler 	sr->isr_nrates = nr + nxr;
29768e8e04eSSam Leffler 
29859aa14a9SRui Paulo 	/* copy SSID */
29968e8e04eSSam Leffler 	sr->isr_ssid_len = se->se_ssid[1];
30068e8e04eSSam Leffler 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
30168e8e04eSSam Leffler 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
30268e8e04eSSam Leffler 
30359aa14a9SRui Paulo 	/* copy mesh id */
30468e8e04eSSam Leffler 	cp += sr->isr_ssid_len;
30559aa14a9SRui Paulo 	sr->isr_meshid_len = se->se_meshid[1];
30659aa14a9SRui Paulo 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
30759aa14a9SRui Paulo 	cp += sr->isr_meshid_len;
30859aa14a9SRui Paulo 
30959aa14a9SRui Paulo 	if (ielen)
310b032f27cSSam Leffler 		memcpy(cp, se->se_ies.data, ielen);
31168e8e04eSSam Leffler 
31268e8e04eSSam Leffler 	req->space -= len;
31368e8e04eSSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
31468e8e04eSSam Leffler }
31568e8e04eSSam Leffler 
31674bdb731SAndriy Voskoboinyk static int
317b032f27cSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
318b032f27cSSam Leffler 	struct ieee80211req *ireq)
31968e8e04eSSam Leffler {
32068e8e04eSSam Leffler 	struct scanreq req;
32168e8e04eSSam Leffler 	int error;
32268e8e04eSSam Leffler 
323b032f27cSSam Leffler 	if (ireq->i_len < sizeof(struct scanreq))
32468e8e04eSSam Leffler 		return EFAULT;
32568e8e04eSSam Leffler 
32668e8e04eSSam Leffler 	error = 0;
32768e8e04eSSam Leffler 	req.space = 0;
328b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, get_scan_space, &req);
32968e8e04eSSam Leffler 	if (req.space > ireq->i_len)
33068e8e04eSSam Leffler 		req.space = ireq->i_len;
33168e8e04eSSam Leffler 	if (req.space > 0) {
332db9ff08bSKevin Lo 		uint32_t space;
33368e8e04eSSam Leffler 		void *p;
33468e8e04eSSam Leffler 
33568e8e04eSSam Leffler 		space = req.space;
336bd29f817SBjoern A. Zeeb 		/* XXX IEEE80211_M_WAITOK after driver lock released */
337b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
338b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
33968e8e04eSSam Leffler 		if (p == NULL)
34068e8e04eSSam Leffler 			return ENOMEM;
34168e8e04eSSam Leffler 		req.sr = p;
342b032f27cSSam Leffler 		ieee80211_scan_iterate(vap, get_scan_result, &req);
343239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
344239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
345b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
346239cc3b6SSam Leffler 	} else
347239cc3b6SSam Leffler 		ireq->i_len = 0;
348239cc3b6SSam Leffler 
349239cc3b6SSam Leffler 	return error;
350239cc3b6SSam Leffler }
3518a1b9b6aSSam Leffler 
3522cab1d3dSSam Leffler struct stainforeq {
3532cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3542cab1d3dSSam Leffler 	size_t	space;
3552cab1d3dSSam Leffler };
3568a1b9b6aSSam Leffler 
3572cab1d3dSSam Leffler static size_t
3582cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
3592cab1d3dSSam Leffler {
360b032f27cSSam Leffler 	*ielen = ni->ni_ies.len;
3612cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
36268e8e04eSSam Leffler 		      sizeof(uint32_t));
3632cab1d3dSSam Leffler }
3642cab1d3dSSam Leffler 
3652cab1d3dSSam Leffler static void
3662cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
3672cab1d3dSSam Leffler {
3682cab1d3dSSam Leffler 	struct stainforeq *req = arg;
3692cab1d3dSSam Leffler 	size_t ielen;
3702cab1d3dSSam Leffler 
371b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
3722cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3732cab1d3dSSam Leffler 		return;
3742cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
3752cab1d3dSSam Leffler }
3762cab1d3dSSam Leffler 
37774bdb731SAndriy Voskoboinyk static void
3782cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
3792cab1d3dSSam Leffler {
3802cab1d3dSSam Leffler 	struct stainforeq *req = arg;
381b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3822cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3832cab1d3dSSam Leffler 	size_t ielen, len;
38468e8e04eSSam Leffler 	uint8_t *cp;
3852cab1d3dSSam Leffler 
386b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
3872cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3882cab1d3dSSam Leffler 		return;
3892cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
3902cab1d3dSSam Leffler 		return;
3912cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
3922cab1d3dSSam Leffler 	if (len > req->space)
3932cab1d3dSSam Leffler 		return;
3942cab1d3dSSam Leffler 	si = req->si;
3952cab1d3dSSam Leffler 	si->isi_len = len;
39668e8e04eSSam Leffler 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
3972cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
3988a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
3998a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
4008a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
4018a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
402b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
403b032f27cSSam Leffler 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
4048a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
4058a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
4068a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
4078a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
4088a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
4098a1b9b6aSSam Leffler 		si->isi_nrates = 15;
4108a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
4118a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
412b032f27cSSam Leffler 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
413b032f27cSSam Leffler 		const struct ieee80211_mcs_rates *mcs =
414b032f27cSSam Leffler 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
415b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
4166ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
417b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_800ns;
418b032f27cSSam Leffler 			else
419b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_400ns;
420b032f27cSSam Leffler 		} else {
4216ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
422b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_800ns;
423b032f27cSSam Leffler 			else
424b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_400ns;
425b032f27cSSam Leffler 		}
426b032f27cSSam Leffler 	} else
427b032f27cSSam Leffler 		si->isi_txmbps = si->isi_txrate;
4288a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
4298a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
4308a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
4318a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
4328a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
4338a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
4348a1b9b6aSSam Leffler 	} else {
435801df4a5SSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
436801df4a5SSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
4378a1b9b6aSSam Leffler 	}
4382cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
4392cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
440b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_run;
441b032f27cSSam Leffler 	else if (ni->ni_associd != 0 ||
442b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_WDS &&
443b032f27cSSam Leffler 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
444b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_auth;
4458a1b9b6aSSam Leffler 	else
446b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_init;
4478a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
44859aa14a9SRui Paulo 	si->isi_localid = ni->ni_mllid;
44959aa14a9SRui Paulo 	si->isi_peerid = ni->ni_mlpid;
45059aa14a9SRui Paulo 	si->isi_peerstate = ni->ni_mlstate;
4518a1b9b6aSSam Leffler 
45268e8e04eSSam Leffler 	if (ielen) {
45368e8e04eSSam Leffler 		cp = ((uint8_t *)si) + si->isi_ie_off;
454b032f27cSSam Leffler 		memcpy(cp, ni->ni_ies.data, ielen);
4558a1b9b6aSSam Leffler 	}
4562cab1d3dSSam Leffler 
45768e8e04eSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
4582cab1d3dSSam Leffler 	req->space -= len;
4598a1b9b6aSSam Leffler }
4602cab1d3dSSam Leffler 
46174bdb731SAndriy Voskoboinyk static int
462b032f27cSSam Leffler getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
463db9ff08bSKevin Lo 	struct ieee80211_node *ni, size_t off)
4642cab1d3dSSam Leffler {
465b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4662cab1d3dSSam Leffler 	struct stainforeq req;
467d1c85daeSSam Leffler 	size_t space;
468d1c85daeSSam Leffler 	void *p;
4692cab1d3dSSam Leffler 	int error;
4702cab1d3dSSam Leffler 
4712cab1d3dSSam Leffler 	error = 0;
4722cab1d3dSSam Leffler 	req.space = 0;
4737db788c6SAndriy Voskoboinyk 	if (ni == NULL) {
4747db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space,
4757db788c6SAndriy Voskoboinyk 		    &req);
4767db788c6SAndriy Voskoboinyk 	} else
477d1c85daeSSam Leffler 		get_sta_space(&req, ni);
4782cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
4792cab1d3dSSam Leffler 		req.space = ireq->i_len;
4802cab1d3dSSam Leffler 	if (req.space > 0) {
4812cab1d3dSSam Leffler 		space = req.space;
482bd29f817SBjoern A. Zeeb 		/* XXX IEEE80211_M_WAITOK after driver lock released */
483b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
484b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
485d1c85daeSSam Leffler 		if (p == NULL) {
486d1c85daeSSam Leffler 			error = ENOMEM;
487d1c85daeSSam Leffler 			goto bad;
488d1c85daeSSam Leffler 		}
4892cab1d3dSSam Leffler 		req.si = p;
4907db788c6SAndriy Voskoboinyk 		if (ni == NULL) {
4917db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
4927db788c6SAndriy Voskoboinyk 			    get_sta_info, &req);
4937db788c6SAndriy Voskoboinyk 		} else
494d1c85daeSSam Leffler 			get_sta_info(&req, ni);
4952cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
49668e8e04eSSam Leffler 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
497b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
4982cab1d3dSSam Leffler 	} else
4992cab1d3dSSam Leffler 		ireq->i_len = 0;
500d1c85daeSSam Leffler bad:
501d1c85daeSSam Leffler 	if (ni != NULL)
502d1c85daeSSam Leffler 		ieee80211_free_node(ni);
5038a1b9b6aSSam Leffler 	return error;
5048a1b9b6aSSam Leffler }
5058a1b9b6aSSam Leffler 
50674bdb731SAndriy Voskoboinyk static int
507b032f27cSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
508d1c85daeSSam Leffler {
50968e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
510db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
511d1c85daeSSam Leffler 	struct ieee80211_node *ni;
512d1c85daeSSam Leffler 	int error;
513d1c85daeSSam Leffler 
514d1c85daeSSam Leffler 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
515d1c85daeSSam Leffler 		return EFAULT;
516d1c85daeSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
517d1c85daeSSam Leffler 	if (error != 0)
518d1c85daeSSam Leffler 		return error;
519b032f27cSSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
520d1c85daeSSam Leffler 		ni = NULL;
521d1c85daeSSam Leffler 	} else {
522b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
52368e8e04eSSam Leffler 		if (ni == NULL)
524b032f27cSSam Leffler 			return ENOENT;
525d1c85daeSSam Leffler 	}
526b032f27cSSam Leffler 	return getstainfo_common(vap, ireq, ni, off);
527d1c85daeSSam Leffler }
528d1c85daeSSam Leffler 
52974bdb731SAndriy Voskoboinyk static int
530b032f27cSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
5318a1b9b6aSSam Leffler {
5328a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
5338a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
5348a1b9b6aSSam Leffler 	int error;
5358a1b9b6aSSam Leffler 
5368a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
5378a1b9b6aSSam Leffler 		return EINVAL;
5388a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
5398a1b9b6aSSam Leffler 	if (error != 0)
5408a1b9b6aSSam Leffler 		return error;
541b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
5428a1b9b6aSSam Leffler 	if (ni == NULL)
543b032f27cSSam Leffler 		return ENOENT;
5448a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
5458a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
5468a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
5478a1b9b6aSSam Leffler 	return error;
5488a1b9b6aSSam Leffler }
5498a1b9b6aSSam Leffler 
55074bdb731SAndriy Voskoboinyk static int
551b032f27cSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
5528a1b9b6aSSam Leffler {
553b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5548a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
5558a1b9b6aSSam Leffler 	struct wmeParams *wmep;
5568a1b9b6aSSam Leffler 	int ac;
5578a1b9b6aSSam Leffler 
5588a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
5598a1b9b6aSSam Leffler 		return EINVAL;
5608a1b9b6aSSam Leffler 
5618a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
5628a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
5638a1b9b6aSSam Leffler 		ac = WME_AC_BE;
5648a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
5658a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5668a1b9b6aSSam Leffler 	else
5678a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5688a1b9b6aSSam Leffler 	switch (ireq->i_type) {
5698a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
5708a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
5718a1b9b6aSSam Leffler 		break;
5728a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
5738a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
5748a1b9b6aSSam Leffler 		break;
5758a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
5768a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
5778a1b9b6aSSam Leffler 		break;
5788a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
5798a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
5808a1b9b6aSSam Leffler 		break;
5818a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
5828a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5838a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
5848a1b9b6aSSam Leffler 		break;
5858a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
5868a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5878a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
5888a1b9b6aSSam Leffler 		break;
5898a1b9b6aSSam Leffler 	}
5908a1b9b6aSSam Leffler 	return 0;
5918a1b9b6aSSam Leffler }
5928a1b9b6aSSam Leffler 
59374bdb731SAndriy Voskoboinyk static int
594b032f27cSSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
595188757f5SSam Leffler {
596b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
597188757f5SSam Leffler 
598b032f27cSSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
599188757f5SSam Leffler }
600188757f5SSam Leffler 
60174bdb731SAndriy Voskoboinyk static int
602b032f27cSSam Leffler ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
603b032f27cSSam Leffler {
604b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
605b032f27cSSam Leffler 	struct ieee80211_channel *c;
606b032f27cSSam Leffler 
607b032f27cSSam Leffler 	if (ireq->i_len != sizeof(struct ieee80211_channel))
608b032f27cSSam Leffler 		return EINVAL;
609b032f27cSSam Leffler 	/*
610b032f27cSSam Leffler 	 * vap's may have different operating channels when HT is
611b032f27cSSam Leffler 	 * in use.  When in RUN state report the vap-specific channel.
612b032f27cSSam Leffler 	 * Otherwise return curchan.
613b032f27cSSam Leffler 	 */
614daa96325SIan Lepore 	if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
615b032f27cSSam Leffler 		c = vap->iv_bss->ni_chan;
616b032f27cSSam Leffler 	else
617b032f27cSSam Leffler 		c = ic->ic_curchan;
618b032f27cSSam Leffler 	return copyout(c, ireq->i_data, sizeof(*c));
61968e8e04eSSam Leffler }
62068e8e04eSSam Leffler 
62168e8e04eSSam Leffler static int
622b032f27cSSam Leffler getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
62368e8e04eSSam Leffler {
624b032f27cSSam Leffler 	if (aie == NULL)
62568e8e04eSSam Leffler 		return EINVAL;
626b032f27cSSam Leffler 	/* NB: truncate, caller can check length */
627b032f27cSSam Leffler 	if (ireq->i_len > aie->ie_len)
628b032f27cSSam Leffler 		ireq->i_len = aie->ie_len;
629b032f27cSSam Leffler 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
630b032f27cSSam Leffler }
631b032f27cSSam Leffler 
632b032f27cSSam Leffler static int
633b032f27cSSam Leffler ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
634b032f27cSSam Leffler {
635b032f27cSSam Leffler 	uint8_t fc0;
636b032f27cSSam Leffler 
637b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
638b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
639b032f27cSSam Leffler 		return EINVAL;
640b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
641b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
642b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
643b032f27cSSam Leffler 		return getappie(vap->iv_appie_beacon, ireq);
644b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
645b032f27cSSam Leffler 		return getappie(vap->iv_appie_proberesp, ireq);
646b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
647b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocresp, ireq);
648b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
649b032f27cSSam Leffler 		return getappie(vap->iv_appie_probereq, ireq);
650b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
651b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocreq, ireq);
652b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
653b032f27cSSam Leffler 		return getappie(vap->iv_appie_wpa, ireq);
654b032f27cSSam Leffler 	}
655b032f27cSSam Leffler 	return EINVAL;
656b032f27cSSam Leffler }
657b032f27cSSam Leffler 
65874bdb731SAndriy Voskoboinyk static int
659b032f27cSSam Leffler ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
660b032f27cSSam Leffler 	const struct ieee80211req *ireq)
661b032f27cSSam Leffler {
662b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
663b032f27cSSam Leffler 
664b032f27cSSam Leffler 	if (ireq->i_len != sizeof(ic->ic_regdomain))
665b032f27cSSam Leffler 		return EINVAL;
666b032f27cSSam Leffler 	return copyout(&ic->ic_regdomain, ireq->i_data,
667b032f27cSSam Leffler 	    sizeof(ic->ic_regdomain));
668b032f27cSSam Leffler }
669b032f27cSSam Leffler 
67074bdb731SAndriy Voskoboinyk static int
671b032f27cSSam Leffler ieee80211_ioctl_getroam(struct ieee80211vap *vap,
672b032f27cSSam Leffler 	const struct ieee80211req *ireq)
673b032f27cSSam Leffler {
674cbb24c24SSam Leffler 	size_t len = ireq->i_len;
675cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
676cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_roamparms))
677cbb24c24SSam Leffler 		len = sizeof(vap->iv_roamparms);
678cbb24c24SSam Leffler 	return copyout(vap->iv_roamparms, ireq->i_data, len);
679b032f27cSSam Leffler }
680b032f27cSSam Leffler 
68174bdb731SAndriy Voskoboinyk static int
682b032f27cSSam Leffler ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
683b032f27cSSam Leffler 	const struct ieee80211req *ireq)
684b032f27cSSam Leffler {
685cbb24c24SSam Leffler 	size_t len = ireq->i_len;
686cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
687cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_txparms))
688cbb24c24SSam Leffler 		len = sizeof(vap->iv_txparms);
689cbb24c24SSam Leffler 	return copyout(vap->iv_txparms, ireq->i_data, len);
690b032f27cSSam Leffler }
691b032f27cSSam Leffler 
69274bdb731SAndriy Voskoboinyk static int
693b032f27cSSam Leffler ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
694b032f27cSSam Leffler 	const struct ieee80211req *ireq)
695b032f27cSSam Leffler {
696b032f27cSSam Leffler 	struct ieee80211_devcaps_req *dc;
697b032f27cSSam Leffler 	struct ieee80211req_chaninfo *ci;
6988658b18bSSam Leffler 	int maxchans, error;
699b032f27cSSam Leffler 
7008658b18bSSam Leffler 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
7018658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
7028658b18bSSam Leffler 	/* NB: require 1 so we know ic_nchans is accessible */
7038658b18bSSam Leffler 	if (maxchans < 1)
704b032f27cSSam Leffler 		return EINVAL;
7058658b18bSSam Leffler 	/* constrain max request size, 2K channels is ~24Kbytes */
7068658b18bSSam Leffler 	if (maxchans > 2048)
7078658b18bSSam Leffler 		maxchans = 2048;
7088658b18bSSam Leffler 	dc = (struct ieee80211_devcaps_req *)
709b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP,
710b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
711b032f27cSSam Leffler 	if (dc == NULL)
712b032f27cSSam Leffler 		return ENOMEM;
713b032f27cSSam Leffler 	dc->dc_drivercaps = ic->ic_caps;
714b032f27cSSam Leffler 	dc->dc_cryptocaps = ic->ic_cryptocaps;
715b032f27cSSam Leffler 	dc->dc_htcaps = ic->ic_htcaps;
71635bcfd1cSAdrian Chadd 	dc->dc_vhtcaps = ic->ic_vhtcaps;
717b032f27cSSam Leffler 	ci = &dc->dc_chaninfo;
7188658b18bSSam Leffler 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
719f8b1ea17SSam Leffler 	KASSERT(ci->ic_nchans <= maxchans,
720f8b1ea17SSam Leffler 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
721b032f27cSSam Leffler 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
7228658b18bSSam Leffler 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
723b9b53389SAdrian Chadd 	IEEE80211_FREE(dc, M_TEMP);
724b032f27cSSam Leffler 	return error;
725b032f27cSSam Leffler }
726b032f27cSSam Leffler 
72774bdb731SAndriy Voskoboinyk static int
728b032f27cSSam Leffler ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
729b032f27cSSam Leffler {
730b032f27cSSam Leffler 	struct ieee80211_node *ni;
731b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
732b032f27cSSam Leffler 	int error;
733b032f27cSSam Leffler 
734b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
735b032f27cSSam Leffler 		return EINVAL;
736b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
737b032f27cSSam Leffler 	if (error != 0)
738b032f27cSSam Leffler 		return error;
739b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
740b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
741b032f27cSSam Leffler 		    vlan.sv_macaddr);
742b032f27cSSam Leffler 		if (ni == NULL)
743b032f27cSSam Leffler 			return ENOENT;
744b032f27cSSam Leffler 	} else
745b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
746b032f27cSSam Leffler 	vlan.sv_vlan = ni->ni_vlan;
747b032f27cSSam Leffler 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
748b032f27cSSam Leffler 	ieee80211_free_node(ni);
749b032f27cSSam Leffler 	return error;
75068e8e04eSSam Leffler }
75168e8e04eSSam Leffler 
75268e8e04eSSam Leffler /*
7538c4e758aSSam Leffler  * Dummy ioctl get handler so the linker set is defined.
7548c4e758aSSam Leffler  */
7558c4e758aSSam Leffler static int
7568c4e758aSSam Leffler dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
7578c4e758aSSam Leffler {
7588c4e758aSSam Leffler 	return ENOSYS;
7598c4e758aSSam Leffler }
7608c4e758aSSam Leffler IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
7618c4e758aSSam Leffler 
7628c4e758aSSam Leffler static int
7638c4e758aSSam Leffler ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
7648c4e758aSSam Leffler {
7658c4e758aSSam Leffler 	ieee80211_ioctl_getfunc * const *get;
7668c4e758aSSam Leffler 	int error;
7678c4e758aSSam Leffler 
7688c4e758aSSam Leffler 	SET_FOREACH(get, ieee80211_ioctl_getset) {
7698c4e758aSSam Leffler 		error = (*get)(vap, ireq);
7708c4e758aSSam Leffler 		if (error != ENOSYS)
7718c4e758aSSam Leffler 			return error;
7728c4e758aSSam Leffler 	}
7738c4e758aSSam Leffler 	return EINVAL;
7748c4e758aSSam Leffler }
7758c4e758aSSam Leffler 
77674bdb731SAndriy Voskoboinyk static int
777b032f27cSSam Leffler ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
778b032f27cSSam Leffler     struct ieee80211req *ireq)
7798a1b9b6aSSam Leffler {
780b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
781b032f27cSSam Leffler 	u_int kid, len;
78268e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
7831a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
784b032f27cSSam Leffler 	int error = 0;
7851a1e1d21SSam Leffler 
7861a1e1d21SSam Leffler 	switch (ireq->i_type) {
7877d1d4407SBjoern A. Zeeb 	case IEEE80211_IOC_IC_NAME:
7887d1d4407SBjoern A. Zeeb 		len = strlen(ic->ic_name) + 1;
7897d1d4407SBjoern A. Zeeb 		if (len > ireq->i_len)
7907d1d4407SBjoern A. Zeeb 			return (EINVAL);
7917d1d4407SBjoern A. Zeeb 		ireq->i_len = len;
7927d1d4407SBjoern A. Zeeb 		error = copyout(ic->ic_name, ireq->i_data, ireq->i_len);
7937d1d4407SBjoern A. Zeeb 		break;
7941a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
795b032f27cSSam Leffler 		switch (vap->iv_state) {
7961a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
7971a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
798b032f27cSSam Leffler 			ireq->i_len = vap->iv_des_ssid[0].len;
799b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
8001a1e1d21SSam Leffler 			break;
8011a1e1d21SSam Leffler 		default:
802b032f27cSSam Leffler 			ireq->i_len = vap->iv_bss->ni_esslen;
803b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
8041a1e1d21SSam Leffler 			break;
8051a1e1d21SSam Leffler 		}
8061a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
8071a1e1d21SSam Leffler 		break;
8081a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
8091a1e1d21SSam Leffler 		ireq->i_val = 1;
8101a1e1d21SSam Leffler 		break;
8111a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
812b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
8138a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
814b032f27cSSam Leffler 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
8158a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
8168a1b9b6aSSam Leffler 		else
8178a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
8181a1e1d21SSam Leffler 		break;
8191a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
8201a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
8218a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8228a1b9b6aSSam Leffler 			return EINVAL;
823b032f27cSSam Leffler 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
8241a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
82501e57940SBjoern A. Zeeb 		if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
826b032f27cSSam Leffler 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
8271a1e1d21SSam Leffler 		} else {
8281a1e1d21SSam Leffler 			bzero(tmpkey, len);
8291a1e1d21SSam Leffler 		}
8301a1e1d21SSam Leffler 		ireq->i_len = len;
8311a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
8321a1e1d21SSam Leffler 		break;
8331a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
8341a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
8351a1e1d21SSam Leffler 		break;
8361a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
837b032f27cSSam Leffler 		ireq->i_val = vap->iv_def_txkey;
8381a1e1d21SSam Leffler 		break;
8391a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
840b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA)
8418a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
8428a1b9b6aSSam Leffler 		else
843b032f27cSSam Leffler 			ireq->i_val = vap->iv_bss->ni_authmode;
8441a1e1d21SSam Leffler 		break;
8451a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
846b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
8471a1e1d21SSam Leffler 		break;
8481a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
849b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PMGTON)
8501a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
8511a1e1d21SSam Leffler 		else
8521a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
8531a1e1d21SSam Leffler 		break;
8541a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
8551a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
8561a1e1d21SSam Leffler 		break;
85713604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
858b032f27cSSam Leffler 		ireq->i_val = vap->iv_rtsthreshold;
8591a1e1d21SSam Leffler 		break;
8602e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
861f1481c8dSAdrian Chadd 		ireq->i_val = vap->iv_protmode;
8622e79ca97SSam Leffler 		break;
8632e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
864b032f27cSSam Leffler 		/*
865b032f27cSSam Leffler 		 * Tx power limit is the min of max regulatory
866b032f27cSSam Leffler 		 * power, any user-set limit, and the max the
867b032f27cSSam Leffler 		 * radio can do.
868781487cfSAdrian Chadd 		 *
869781487cfSAdrian Chadd 		 * TODO: methodize this
870b032f27cSSam Leffler 		 */
871b032f27cSSam Leffler 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
872b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_txpowlimit)
8738a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_txpowlimit;
874b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
875b032f27cSSam Leffler 			ireq->i_val = ic->ic_curchan->ic_maxpower;
8768a1b9b6aSSam Leffler 		break;
8778a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
878b032f27cSSam Leffler 		switch (vap->iv_flags & IEEE80211_F_WPA) {
8798a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
8808a1b9b6aSSam Leffler 			ireq->i_val = 1;
8818a1b9b6aSSam Leffler 			break;
8828a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
8838a1b9b6aSSam Leffler 			ireq->i_val = 2;
8848a1b9b6aSSam Leffler 			break;
8858a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
8868a1b9b6aSSam Leffler 			ireq->i_val = 3;
8878a1b9b6aSSam Leffler 			break;
8888a1b9b6aSSam Leffler 		default:
8898a1b9b6aSSam Leffler 			ireq->i_val = 0;
8908a1b9b6aSSam Leffler 			break;
8918a1b9b6aSSam Leffler 		}
8928a1b9b6aSSam Leffler 		break;
8938a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
894b032f27cSSam Leffler 		error = ieee80211_ioctl_getchanlist(vap, ireq);
8958a1b9b6aSSam Leffler 		break;
8968a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
897b032f27cSSam Leffler 		ireq->i_val = vap->iv_roaming;
8988a1b9b6aSSam Leffler 		break;
8998a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
900b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
9018a1b9b6aSSam Leffler 		break;
9028a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
903b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
9048a1b9b6aSSam Leffler 		break;
9058a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
906b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
9078a1b9b6aSSam Leffler 		break;
9088a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
909b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
9108a1b9b6aSSam Leffler 		break;
9118a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
912b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
9138a1b9b6aSSam Leffler 		break;
9148a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
915b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
9168a1b9b6aSSam Leffler 		break;
9178a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
91801e57940SBjoern A. Zeeb 		error = ieee80211_ioctl_getkey(cmd, vap, ireq);
9198a1b9b6aSSam Leffler 		break;
9208a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
921b032f27cSSam Leffler 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
9228a1b9b6aSSam Leffler 		break;
9238a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
9248a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
9258a1b9b6aSSam Leffler 			return EINVAL;
9261b74d2a4SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
9277cd89f64SSam Leffler 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
9287cd89f64SSam Leffler 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
9298a1b9b6aSSam Leffler 			    ireq->i_data, ireq->i_len);
9307cd89f64SSam Leffler 		} else
9317cd89f64SSam Leffler 			error = copyout(vap->iv_des_bssid, ireq->i_data,
9327cd89f64SSam Leffler 			    ireq->i_len);
9338a1b9b6aSSam Leffler 		break;
9348a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
93568e8e04eSSam Leffler 	case IEEE80211_IOC_WPAIE2:
936b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
9378a1b9b6aSSam Leffler 		break;
9388a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
939b032f27cSSam Leffler 		error = ieee80211_ioctl_getscanresults(vap, ireq);
9408a1b9b6aSSam Leffler 		break;
9418a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
942b032f27cSSam Leffler 		error = ieee80211_ioctl_getstastats(vap, ireq);
9438a1b9b6aSSam Leffler 		break;
9448a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
945b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_txpower;
9468a1b9b6aSSam Leffler 		break;
9478a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
948b032f27cSSam Leffler 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
9498a1b9b6aSSam Leffler 		break;
9508a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
951b032f27cSSam Leffler 		error = ieee80211_ioctl_getstainfo(vap, ireq);
9528a1b9b6aSSam Leffler 		break;
9538a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
9548a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
9558a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
9568a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
9578a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
958fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
959b032f27cSSam Leffler 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
9608a1b9b6aSSam Leffler 		break;
9618a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
962b032f27cSSam Leffler 		ireq->i_val = vap->iv_dtim_period;
9638a1b9b6aSSam Leffler 		break;
9648a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
9658a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
966b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_intval;
9672e79ca97SSam Leffler 		break;
968c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
969b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
970c4f040c3SSam Leffler 		break;
97132b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
97232b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet;
97332b0e64bSAdrian Chadd 		break;
97432b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
97532b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_count;
97632b0e64bSAdrian Chadd 		break;
97732b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
97832b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_period;
97932b0e64bSAdrian Chadd 		break;
98032b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
98132b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_duration;
98232b0e64bSAdrian Chadd 		break;
98332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
98432b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_offset;
98532b0e64bSAdrian Chadd 		break;
98668e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
987b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
98868e8e04eSSam Leffler 		break;
98968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
990b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
99168e8e04eSSam Leffler 		break;
99268e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
993b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
99468e8e04eSSam Leffler 		break;
99568e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
996b032f27cSSam Leffler 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
99764353cb0SSam Leffler 		break;
99870231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
999b032f27cSSam Leffler 		ireq->i_val = vap->iv_fragthreshold;
100070231e3dSSam Leffler 		break;
1001188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
1002b032f27cSSam Leffler 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
1003188757f5SSam Leffler 		break;
1004c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
1005b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
1006c27e4e31SSam Leffler 		break;
1007546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
1008b032f27cSSam Leffler 		ireq->i_val = vap->iv_bmissthreshold;
1009546786c9SSam Leffler 		break;
101068e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
1011b032f27cSSam Leffler 		error = ieee80211_ioctl_getcurchan(vap, ireq);
101268e8e04eSSam Leffler 		break;
101368e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
101468e8e04eSSam Leffler 		ireq->i_val = 0;
10152bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
101668e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
10172bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
101868e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
101968e8e04eSSam Leffler 		break;
102068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
102168e8e04eSSam Leffler 		ireq->i_val = 0;
10222bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
102368e8e04eSSam Leffler 			ireq->i_val |= 1;
10242bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
102568e8e04eSSam Leffler 			ireq->i_val |= 2;
102668e8e04eSSam Leffler 		break;
102768e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
1028781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1029b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1030b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_rxmax;
10311b74d2a4SAdrian Chadd 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
1032cac03190SAdrian Chadd 			/*
1033cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1034cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1035cac03190SAdrian Chadd 			 */
1036fe5ebb23SBjoern A. Zeeb 			ireq->i_val = _IEEE80211_MASKSHIFT( vap->iv_bss->ni_htparam,
1037b032f27cSSam Leffler 			    IEEE80211_HTCAP_MAXRXAMPDU);
1038b032f27cSSam Leffler 		else
1039b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_limit;
104068e8e04eSSam Leffler 		break;
104168e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
1042781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1043900de995SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
10441b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1045cac03190SAdrian Chadd 			/*
1046cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1047cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1048cac03190SAdrian Chadd 			 */
1049fe5ebb23SBjoern A. Zeeb 			ireq->i_val = _IEEE80211_MASKSHIFT(vap->iv_bss->ni_htparam,
1050b032f27cSSam Leffler 			    IEEE80211_HTCAP_MPDUDENSITY);
1051b032f27cSSam Leffler 		else
1052b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_density;
105368e8e04eSSam Leffler 		break;
105468e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
105568e8e04eSSam Leffler 		ireq->i_val = 0;
10562bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
105768e8e04eSSam Leffler 			ireq->i_val |= 1;
10582bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
105968e8e04eSSam Leffler 			ireq->i_val |= 2;
106068e8e04eSSam Leffler 		break;
106168e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
1062b032f27cSSam Leffler 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
106368e8e04eSSam Leffler 		break;
106468e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
10652bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
106668e8e04eSSam Leffler 		break;
106768e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
1068b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1069b032f27cSSam Leffler 		break;
1070b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
1071b032f27cSSam Leffler 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1072b032f27cSSam Leffler 		break;
1073b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
1074b032f27cSSam Leffler 		error = ieee80211_ioctl_getroam(vap, ireq);
1075b032f27cSSam Leffler 		break;
1076b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
1077b032f27cSSam Leffler 		error = ieee80211_ioctl_gettxparams(vap, ireq);
107868e8e04eSSam Leffler 		break;
107968e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
10802bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1081b032f27cSSam Leffler 		break;
1082b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
1083b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
108468e8e04eSSam Leffler 		break;
1085c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
1086b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1087b032f27cSSam Leffler 		break;
1088b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
1089b032f27cSSam Leffler 		error = ieee80211_ioctl_getappie(vap, ireq);
1090b032f27cSSam Leffler 		break;
1091b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
1092b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1093b032f27cSSam Leffler 		break;
1094b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
1095b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1096b032f27cSSam Leffler 		break;
1097b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
1098b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1099b032f27cSSam Leffler 		break;
1100b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
1101b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1102b032f27cSSam Leffler 		break;
1103b032f27cSSam Leffler 	case IEEE80211_IOC_DEVCAPS:
1104b032f27cSSam Leffler 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1105c066143cSSam Leffler 		break;
11061b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
1107f1481c8dSAdrian Chadd 		ireq->i_val = vap->iv_htprotmode;
11081b6167d2SSam Leffler 		break;
11091b6167d2SSam Leffler 	case IEEE80211_IOC_HTCONF:
11102bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
11111b6167d2SSam Leffler 			ireq->i_val = 1;
11122bfc8a91SSam Leffler 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
11131b6167d2SSam Leffler 				ireq->i_val |= 2;
11141b6167d2SSam Leffler 		} else
11151b6167d2SSam Leffler 			ireq->i_val = 0;
11161b6167d2SSam Leffler 		break;
1117b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
1118b032f27cSSam Leffler 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1119b032f27cSSam Leffler 		break;
11208c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
11218c070d69SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11221b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
11238c070d69SSam Leffler 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
11248c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
11258c070d69SSam Leffler 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
11268c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
11278c070d69SSam Leffler 			else
11288c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
11298c070d69SSam Leffler 		} else
11308c070d69SSam Leffler 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
11318c070d69SSam Leffler 		break;
113244f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
113344f7a6edSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11341b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
113544f7a6edSSam Leffler 			ireq->i_val =
113644f7a6edSSam Leffler 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
113744f7a6edSSam Leffler 		else
113844f7a6edSSam Leffler 			ireq->i_val =
11392bfc8a91SSam Leffler 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
114044f7a6edSSam Leffler 		break;
1141ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
1142ec3763bbSAdrian Chadd 		ireq->i_val = 0;
1143ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
1144ec3763bbSAdrian Chadd 			ireq->i_val |= 1;
1145ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
1146ec3763bbSAdrian Chadd 			ireq->i_val |= 2;
1147ec3763bbSAdrian Chadd 		break;
1148c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
1149c5bba9daSAndriy Voskoboinyk 		ireq->i_val = 0;
1150c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)
1151c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 1;
1152c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX)
1153c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 2;
1154c5bba9daSAndriy Voskoboinyk 		break;
11558379e8dbSAdrian Chadd 	case IEEE80211_IOC_UAPSD:
11568379e8dbSAdrian Chadd 		ireq->i_val = 0;
11578379e8dbSAdrian Chadd 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
11588379e8dbSAdrian Chadd 			ireq->i_val = 1;
11598379e8dbSAdrian Chadd 		break;
116035bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
116196811be2SBjoern A. Zeeb 		ireq->i_val = vap->iv_flags_vht & IEEE80211_FVHT_MASK;
116235bcfd1cSAdrian Chadd 		break;
11631a1e1d21SSam Leffler 	default:
11648c4e758aSSam Leffler 		error = ieee80211_ioctl_getdefault(vap, ireq);
1165b2e95691SSam Leffler 		break;
11661a1e1d21SSam Leffler 	}
11678a1b9b6aSSam Leffler 	return error;
11688a1b9b6aSSam Leffler }
11698a1b9b6aSSam Leffler 
117074bdb731SAndriy Voskoboinyk static int
1171b032f27cSSam Leffler ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11728a1b9b6aSSam Leffler {
11738a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
11748a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11758a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
117668e8e04eSSam Leffler 	uint16_t kid;
1177b032f27cSSam Leffler 	int error, i;
11788a1b9b6aSSam Leffler 
11798a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
11808a1b9b6aSSam Leffler 		return EINVAL;
11818a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
11821a1e1d21SSam Leffler 	if (error)
11838a1b9b6aSSam Leffler 		return error;
11848a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
11858a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
11868a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
11878a1b9b6aSSam Leffler 		return E2BIG;
11888a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
11898a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
11908a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
11918a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
11928a1b9b6aSSam Leffler 			return EINVAL;
1193b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1194b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1195b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1196b5d4660fSSam Leffler 				ieee80211_free_node(ni);
11978a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1198b5d4660fSSam Leffler 			}
11998a1b9b6aSSam Leffler 		} else {
1200b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1201b032f27cSSam Leffler 				ik.ik_macaddr);
12028a1b9b6aSSam Leffler 			if (ni == NULL)
12038a1b9b6aSSam Leffler 				return ENOENT;
12048a1b9b6aSSam Leffler 		}
12058a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
12068a1b9b6aSSam Leffler 	} else {
12078a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12088a1b9b6aSSam Leffler 			return EINVAL;
1209b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
1210386d84f6SSam Leffler 		/*
1211386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1212386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1213386d84f6SSam Leffler 		 */
1214386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1215386d84f6SSam Leffler 			wk->wk_keyix = kid;
12168a1b9b6aSSam Leffler 		ni = NULL;
12178a1b9b6aSSam Leffler 	}
12188a1b9b6aSSam Leffler 	error = 0;
1219b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
1220b032f27cSSam Leffler 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
12218a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
12228a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
12238a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
12248a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1225b032f27cSSam Leffler 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1226b032f27cSSam Leffler 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
12278a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
12288a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
12298a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
123071fe06caSSam Leffler 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
123171fe06caSSam Leffler 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
123271fe06caSSam Leffler 		if (!ieee80211_crypto_setkey(vap, wk))
12338a1b9b6aSSam Leffler 			error = EIO;
12348a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1235781487cfSAdrian Chadd 			/*
1236781487cfSAdrian Chadd 			 * Inform the driver that this is the default
1237781487cfSAdrian Chadd 			 * transmit key.  Now, ideally we'd just set
1238781487cfSAdrian Chadd 			 * a flag in the key update that would
1239781487cfSAdrian Chadd 			 * say "yes, we're the default key", but
1240781487cfSAdrian Chadd 			 * that currently isn't the way the ioctl ->
1241781487cfSAdrian Chadd 			 * key interface works.
1242781487cfSAdrian Chadd 			 */
1243781487cfSAdrian Chadd 			ieee80211_crypto_set_deftxkey(vap, kid);
12448a1b9b6aSSam Leffler 	} else
12458a1b9b6aSSam Leffler 		error = ENXIO;
1246b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
12478a1b9b6aSSam Leffler 	if (ni != NULL)
12488a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12498a1b9b6aSSam Leffler 	return error;
12508a1b9b6aSSam Leffler }
12518a1b9b6aSSam Leffler 
125274bdb731SAndriy Voskoboinyk static int
1253b032f27cSSam Leffler ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
12548a1b9b6aSSam Leffler {
12558a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
12568a1b9b6aSSam Leffler 	int kid, error;
12578a1b9b6aSSam Leffler 
12588a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
12598a1b9b6aSSam Leffler 		return EINVAL;
12608a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
12618a1b9b6aSSam Leffler 	if (error)
12628a1b9b6aSSam Leffler 		return error;
12638a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
126468e8e04eSSam Leffler 	/* XXX uint8_t -> uint16_t */
126568e8e04eSSam Leffler 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
12668a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
12678a1b9b6aSSam Leffler 
1268b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1269b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1270b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1271b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1272b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1273b5d4660fSSam Leffler 			}
1274b5d4660fSSam Leffler 		} else {
1275b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1276b032f27cSSam Leffler 				dk.idk_macaddr);
12778a1b9b6aSSam Leffler 			if (ni == NULL)
1278b5d4660fSSam Leffler 				return ENOENT;
1279b5d4660fSSam Leffler 		}
12808a1b9b6aSSam Leffler 		/* XXX error return */
1281c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
12828a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12838a1b9b6aSSam Leffler 	} else {
12848a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12858a1b9b6aSSam Leffler 			return EINVAL;
12868a1b9b6aSSam Leffler 		/* XXX error return */
1287b032f27cSSam Leffler 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
12888a1b9b6aSSam Leffler 	}
12898a1b9b6aSSam Leffler 	return 0;
12908a1b9b6aSSam Leffler }
12918a1b9b6aSSam Leffler 
1292b032f27cSSam Leffler struct mlmeop {
1293b032f27cSSam Leffler 	struct ieee80211vap *vap;
1294b032f27cSSam Leffler 	int	op;
1295b032f27cSSam Leffler 	int	reason;
1296b032f27cSSam Leffler };
1297b032f27cSSam Leffler 
1298b032f27cSSam Leffler static void
1299b032f27cSSam Leffler mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1300b032f27cSSam Leffler 	int op, int reason)
1301b032f27cSSam Leffler {
1302b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1303b032f27cSSam Leffler 	static const struct {
1304b032f27cSSam Leffler 		int mask;
1305b032f27cSSam Leffler 		const char *opstr;
1306b032f27cSSam Leffler 	} ops[] = {
1307b032f27cSSam Leffler 		{ 0, "op#0" },
1308b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1309b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "assoc" },
1310b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1311b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "disassoc" },
1312b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1313b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "deauth" },
1314b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1315b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "authorize" },
1316b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1317b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "unauthorize" },
1318b032f27cSSam Leffler 	};
1319b032f27cSSam Leffler 
1320b032f27cSSam Leffler 	if (op == IEEE80211_MLME_AUTH) {
1321b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1322b032f27cSSam Leffler 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1323d72d72d3SAndriy Voskoboinyk 		    "station authenticate %s via MLME (reason: %d (%s))",
1324b032f27cSSam Leffler 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1325d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1326b032f27cSSam Leffler 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1327b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1328d72d72d3SAndriy Voskoboinyk 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
1329d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(reason));
1330b032f27cSSam Leffler 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1331b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1332b032f27cSSam Leffler 		    "station %s via MLME", ops[op].opstr);
1333b032f27cSSam Leffler 	} else {
1334b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1335d72d72d3SAndriy Voskoboinyk 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
1336d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1337b032f27cSSam Leffler 	}
1338b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
1339b032f27cSSam Leffler }
1340b032f27cSSam Leffler 
13418a1b9b6aSSam Leffler static void
13428a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
13438a1b9b6aSSam Leffler {
1344b032f27cSSam Leffler 	struct mlmeop *mop = arg;
1345b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
13468a1b9b6aSSam Leffler 
1347b032f27cSSam Leffler 	if (vap != mop->vap)
1348b032f27cSSam Leffler 		return;
1349b032f27cSSam Leffler 	/*
1350b032f27cSSam Leffler 	 * NB: if ni_associd is zero then the node is already cleaned
1351b032f27cSSam Leffler 	 * up and we don't need to do this (we're safely holding a
1352b032f27cSSam Leffler 	 * reference but should otherwise not modify it's state).
1353b032f27cSSam Leffler 	 */
1354b032f27cSSam Leffler 	if (ni->ni_associd == 0)
1355b032f27cSSam Leffler 		return;
1356b032f27cSSam Leffler 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1357b032f27cSSam Leffler 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1358b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1359b032f27cSSam Leffler 		    mop->reason);
1360b032f27cSSam Leffler 	} else {
1361b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1362b032f27cSSam Leffler 		    mop->reason);
13638a1b9b6aSSam Leffler 	}
1364b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1365b032f27cSSam Leffler }
1366b032f27cSSam Leffler 
1367b032f27cSSam Leffler static int
1368b032f27cSSam Leffler setmlme_dropsta(struct ieee80211vap *vap,
1369b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1370b032f27cSSam Leffler {
13717a79cebfSGleb Smirnoff 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1372b032f27cSSam Leffler 	struct ieee80211_node *ni;
1373b032f27cSSam Leffler 	int error = 0;
1374b032f27cSSam Leffler 
1375b032f27cSSam Leffler 	/* NB: the broadcast address means do 'em all */
13767a79cebfSGleb Smirnoff 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
1377b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1378b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, mac);
13799984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
13809984c9baSAdrian Chadd 		/*
13819984c9baSAdrian Chadd 		 * Don't do the node update inside the node
13829984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
13839984c9baSAdrian Chadd 		 * with drivers and their TX paths.
13849984c9baSAdrian Chadd 		 */
1385b032f27cSSam Leffler 		if (ni != NULL) {
1386b032f27cSSam Leffler 			domlme(mlmeop, ni);
1387b032f27cSSam Leffler 			ieee80211_free_node(ni);
1388b032f27cSSam Leffler 		} else
1389b032f27cSSam Leffler 			error = ENOENT;
1390b032f27cSSam Leffler 	} else {
1391b032f27cSSam Leffler 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1392b032f27cSSam Leffler 	}
1393b032f27cSSam Leffler 	return error;
1394b032f27cSSam Leffler }
1395b032f27cSSam Leffler 
139674bdb731SAndriy Voskoboinyk static int
1397b032f27cSSam Leffler setmlme_common(struct ieee80211vap *vap, int op,
1398b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1399b032f27cSSam Leffler {
1400b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1401b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1402b032f27cSSam Leffler 	struct ieee80211_node *ni;
1403b032f27cSSam Leffler 	struct mlmeop mlmeop;
1404b032f27cSSam Leffler 	int error;
1405b032f27cSSam Leffler 
1406b032f27cSSam Leffler 	error = 0;
1407b032f27cSSam Leffler 	switch (op) {
1408b032f27cSSam Leffler 	case IEEE80211_MLME_DISASSOC:
1409b032f27cSSam Leffler 	case IEEE80211_MLME_DEAUTH:
1410b032f27cSSam Leffler 		switch (vap->iv_opmode) {
1411b032f27cSSam Leffler 		case IEEE80211_M_STA:
1412b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1413b032f27cSSam Leffler 			/* XXX not quite right */
1414b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1415b032f27cSSam Leffler 			break;
1416b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
1417b032f27cSSam Leffler 			mlmeop.vap = vap;
1418b032f27cSSam Leffler 			mlmeop.op = op;
1419b032f27cSSam Leffler 			mlmeop.reason = reason;
1420b032f27cSSam Leffler 			error = setmlme_dropsta(vap, mac, &mlmeop);
1421b032f27cSSam Leffler 			break;
1422b032f27cSSam Leffler 		case IEEE80211_M_WDS:
1423b032f27cSSam Leffler 			/* XXX user app should send raw frame? */
1424b032f27cSSam Leffler 			if (op != IEEE80211_MLME_DEAUTH) {
1425b032f27cSSam Leffler 				error = EINVAL;
1426b032f27cSSam Leffler 				break;
1427b032f27cSSam Leffler 			}
1428b032f27cSSam Leffler #if 0
1429b032f27cSSam Leffler 			/* XXX accept any address, simplifies user code */
1430b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1431b032f27cSSam Leffler 				error = EINVAL;
1432b032f27cSSam Leffler 				break;
1433b032f27cSSam Leffler 			}
1434b032f27cSSam Leffler #endif
1435b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1436b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1437b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
1438b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1439b032f27cSSam Leffler 			ieee80211_free_node(ni);
1440b032f27cSSam Leffler 			break;
1441136cb222SAdrian Chadd 		case IEEE80211_M_MBSS:
1442136cb222SAdrian Chadd 			IEEE80211_NODE_LOCK(nt);
1443136cb222SAdrian Chadd 			ni = ieee80211_find_node_locked(nt, mac);
14449984c9baSAdrian Chadd 			/*
14459984c9baSAdrian Chadd 			 * Don't do the node update inside the node
14469984c9baSAdrian Chadd 			 * table lock.  This unfortunately causes LORs
14479984c9baSAdrian Chadd 			 * with drivers and their TX paths.
14489984c9baSAdrian Chadd 			 */
14499984c9baSAdrian Chadd 			IEEE80211_NODE_UNLOCK(nt);
1450136cb222SAdrian Chadd 			if (ni != NULL) {
1451136cb222SAdrian Chadd 				ieee80211_node_leave(ni);
1452136cb222SAdrian Chadd 				ieee80211_free_node(ni);
1453136cb222SAdrian Chadd 			} else {
1454136cb222SAdrian Chadd 				error = ENOENT;
1455136cb222SAdrian Chadd 			}
1456136cb222SAdrian Chadd 			break;
1457b032f27cSSam Leffler 		default:
1458b032f27cSSam Leffler 			error = EINVAL;
1459b032f27cSSam Leffler 			break;
1460b032f27cSSam Leffler 		}
1461b032f27cSSam Leffler 		break;
1462b032f27cSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
1463b032f27cSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
1464b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1465b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_WDS) {
1466b032f27cSSam Leffler 			error = EINVAL;
1467b032f27cSSam Leffler 			break;
1468b032f27cSSam Leffler 		}
1469b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1470b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14719984c9baSAdrian Chadd 		/*
14729984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14739984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14749984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14759984c9baSAdrian Chadd 		 */
14769984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1477b032f27cSSam Leffler 		if (ni != NULL) {
1478b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1479b032f27cSSam Leffler 			if (op == IEEE80211_MLME_AUTHORIZE)
1480b032f27cSSam Leffler 				ieee80211_node_authorize(ni);
1481b032f27cSSam Leffler 			else
1482b032f27cSSam Leffler 				ieee80211_node_unauthorize(ni);
1483b032f27cSSam Leffler 			ieee80211_free_node(ni);
1484b032f27cSSam Leffler 		} else
1485b032f27cSSam Leffler 			error = ENOENT;
1486b032f27cSSam Leffler 		break;
1487b032f27cSSam Leffler 	case IEEE80211_MLME_AUTH:
1488b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1489b032f27cSSam Leffler 			error = EINVAL;
1490b032f27cSSam Leffler 			break;
1491b032f27cSSam Leffler 		}
1492b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1493b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14949984c9baSAdrian Chadd 		/*
14959984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14969984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14979984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14989984c9baSAdrian Chadd 		 */
14999984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1500b032f27cSSam Leffler 		if (ni != NULL) {
1501b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1502b032f27cSSam Leffler 			if (reason == IEEE80211_STATUS_SUCCESS) {
1503b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
1504b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1505b032f27cSSam Leffler 				/*
1506b032f27cSSam Leffler 				 * For shared key auth, just continue the
1507b032f27cSSam Leffler 				 * exchange.  Otherwise when 802.1x is not in
1508b032f27cSSam Leffler 				 * use mark the port authorized at this point
1509b032f27cSSam Leffler 				 * so traffic can flow.
1510b032f27cSSam Leffler 				 */
1511b032f27cSSam Leffler 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1512b032f27cSSam Leffler 				    ni->ni_challenge == NULL)
1513b032f27cSSam Leffler 				      ieee80211_node_authorize(ni);
1514b032f27cSSam Leffler 			} else {
1515b032f27cSSam Leffler 				vap->iv_stats.is_rx_acl++;
1516b032f27cSSam Leffler 				ieee80211_send_error(ni, ni->ni_macaddr,
1517b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1518b032f27cSSam Leffler 				ieee80211_node_leave(ni);
1519b032f27cSSam Leffler 			}
1520b032f27cSSam Leffler 			ieee80211_free_node(ni);
1521b032f27cSSam Leffler 		} else
1522b032f27cSSam Leffler 			error = ENOENT;
1523b032f27cSSam Leffler 		break;
1524b032f27cSSam Leffler 	default:
1525b032f27cSSam Leffler 		error = EINVAL;
1526b032f27cSSam Leffler 		break;
1527b032f27cSSam Leffler 	}
1528b032f27cSSam Leffler 	return error;
15298a1b9b6aSSam Leffler }
15308a1b9b6aSSam Leffler 
153168e8e04eSSam Leffler struct scanlookup {
153268e8e04eSSam Leffler 	const uint8_t *mac;
153368e8e04eSSam Leffler 	int esslen;
153468e8e04eSSam Leffler 	const uint8_t *essid;
153568e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se;
153668e8e04eSSam Leffler };
153768e8e04eSSam Leffler 
153868e8e04eSSam Leffler /*
153968e8e04eSSam Leffler  * Match mac address and any ssid.
154068e8e04eSSam Leffler  */
154168e8e04eSSam Leffler static void
154268e8e04eSSam Leffler mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
154368e8e04eSSam Leffler {
154468e8e04eSSam Leffler 	struct scanlookup *look = arg;
154568e8e04eSSam Leffler 
154668e8e04eSSam Leffler 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
154768e8e04eSSam Leffler 		return;
154868e8e04eSSam Leffler 	if (look->esslen != 0) {
154968e8e04eSSam Leffler 		if (se->se_ssid[1] != look->esslen)
155068e8e04eSSam Leffler 			return;
155168e8e04eSSam Leffler 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
155268e8e04eSSam Leffler 			return;
155368e8e04eSSam Leffler 	}
155468e8e04eSSam Leffler 	look->se = se;
155568e8e04eSSam Leffler }
155668e8e04eSSam Leffler 
155774bdb731SAndriy Voskoboinyk static int
1558632ee7e3SBernhard Schmidt setmlme_assoc_sta(struct ieee80211vap *vap,
1559632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1560632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1561b032f27cSSam Leffler {
1562b032f27cSSam Leffler 	struct scanlookup lookup;
1563b032f27cSSam Leffler 
1564632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1565632ee7e3SBernhard Schmidt 	    ("expected opmode STA not %s",
1566632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1567b032f27cSSam Leffler 
1568b032f27cSSam Leffler 	/* NB: this is racey if roaming is !manual */
1569b032f27cSSam Leffler 	lookup.se = NULL;
1570b032f27cSSam Leffler 	lookup.mac = mac;
1571b032f27cSSam Leffler 	lookup.esslen = ssid_len;
1572b032f27cSSam Leffler 	lookup.essid = ssid;
1573b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1574b032f27cSSam Leffler 	if (lookup.se == NULL)
1575b032f27cSSam Leffler 		return ENOENT;
1576b032f27cSSam Leffler 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
157710959256SSam Leffler 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1578b032f27cSSam Leffler 		return EIO;		/* XXX unique but could be better */
1579b032f27cSSam Leffler 	return 0;
1580b032f27cSSam Leffler }
1581b032f27cSSam Leffler 
158274bdb731SAndriy Voskoboinyk static int
1583632ee7e3SBernhard Schmidt setmlme_assoc_adhoc(struct ieee80211vap *vap,
1584632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1585632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1586632ee7e3SBernhard Schmidt {
158774bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
158874bdb731SAndriy Voskoboinyk 	int error;
1589632ee7e3SBernhard Schmidt 
1590632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1591632ee7e3SBernhard Schmidt 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1592632ee7e3SBernhard Schmidt 	    ("expected opmode IBSS or AHDEMO not %s",
1593632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1594632ee7e3SBernhard Schmidt 
15950525ece3SBjoern A. Zeeb 	if (ssid_len == 0 || ssid_len > IEEE80211_NWID_LEN)
1596632ee7e3SBernhard Schmidt 		return EINVAL;
1597632ee7e3SBernhard Schmidt 
159874bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
159974bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
160074bdb731SAndriy Voskoboinyk 	if (sr == NULL)
160174bdb731SAndriy Voskoboinyk 		return ENOMEM;
160274bdb731SAndriy Voskoboinyk 
1603632ee7e3SBernhard Schmidt 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1604632ee7e3SBernhard Schmidt 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1605632ee7e3SBernhard Schmidt 	vap->iv_des_ssid[0].len = ssid_len;
1606632ee7e3SBernhard Schmidt 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1607632ee7e3SBernhard Schmidt 	vap->iv_des_nssid = 1;
1608632ee7e3SBernhard Schmidt 
160974bdb731SAndriy Voskoboinyk 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
161074bdb731SAndriy Voskoboinyk 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
161174bdb731SAndriy Voskoboinyk 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
161274bdb731SAndriy Voskoboinyk 	sr->sr_ssid[0].len = ssid_len;
161374bdb731SAndriy Voskoboinyk 	sr->sr_nssid = 1;
1614632ee7e3SBernhard Schmidt 
161574bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
161674bdb731SAndriy Voskoboinyk 
161774bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
161874bdb731SAndriy Voskoboinyk 	return error;
1619632ee7e3SBernhard Schmidt }
1620632ee7e3SBernhard Schmidt 
162174bdb731SAndriy Voskoboinyk static int
1622b032f27cSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
16238a1b9b6aSSam Leffler {
16248a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
16258a1b9b6aSSam Leffler 	int error;
16268a1b9b6aSSam Leffler 
16278a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
16288a1b9b6aSSam Leffler 		return EINVAL;
16298a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
16308a1b9b6aSSam Leffler 	if (error)
16318a1b9b6aSSam Leffler 		return error;
1632632ee7e3SBernhard Schmidt 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1633632ee7e3SBernhard Schmidt 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1634632ee7e3SBernhard Schmidt 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1635b032f27cSSam Leffler 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1636edd39a8eSRui Paulo 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1637edd39a8eSRui Paulo 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1638edd39a8eSRui Paulo 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1639632ee7e3SBernhard Schmidt 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1640632ee7e3SBernhard Schmidt 		    mlme.im_ssid_len, mlme.im_ssid);
16418a1b9b6aSSam Leffler 	else
1642b032f27cSSam Leffler 		return setmlme_common(vap, mlme.im_op,
1643b032f27cSSam Leffler 		    mlme.im_macaddr, mlme.im_reason);
16448a1b9b6aSSam Leffler }
16458a1b9b6aSSam Leffler 
164674bdb731SAndriy Voskoboinyk static int
1647b032f27cSSam Leffler ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
16488a1b9b6aSSam Leffler {
164968e8e04eSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1650b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16518a1b9b6aSSam Leffler 	int error;
16528a1b9b6aSSam Leffler 
16538a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
16548a1b9b6aSSam Leffler 		return EINVAL;
16558a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
16568a1b9b6aSSam Leffler 	if (error)
16578a1b9b6aSSam Leffler 		return error;
16588a1b9b6aSSam Leffler 	if (acl == NULL) {
16598a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
1660b032f27cSSam Leffler 		if (acl == NULL || !acl->iac_attach(vap))
16618a1b9b6aSSam Leffler 			return EINVAL;
1662b032f27cSSam Leffler 		vap->iv_acl = acl;
16638a1b9b6aSSam Leffler 	}
16648a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1665b032f27cSSam Leffler 		acl->iac_add(vap, mac);
16668a1b9b6aSSam Leffler 	else
1667b032f27cSSam Leffler 		acl->iac_remove(vap, mac);
16688a1b9b6aSSam Leffler 	return 0;
16698a1b9b6aSSam Leffler }
16708a1b9b6aSSam Leffler 
167174bdb731SAndriy Voskoboinyk static int
1672b032f27cSSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
16738a1b9b6aSSam Leffler {
1674b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16758a1b9b6aSSam Leffler 
16768a1b9b6aSSam Leffler 	switch (ireq->i_val) {
16778a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
16788a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
16798a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
1680b032f27cSSam Leffler 	case IEEE80211_MACCMD_POLICY_RADIUS:
16818a1b9b6aSSam Leffler 		if (acl == NULL) {
16828a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
1683b032f27cSSam Leffler 			if (acl == NULL || !acl->iac_attach(vap))
16848a1b9b6aSSam Leffler 				return EINVAL;
1685b032f27cSSam Leffler 			vap->iv_acl = acl;
16868a1b9b6aSSam Leffler 		}
1687b032f27cSSam Leffler 		acl->iac_setpolicy(vap, ireq->i_val);
16888a1b9b6aSSam Leffler 		break;
16898a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
16908a1b9b6aSSam Leffler 		if (acl != NULL)
1691b032f27cSSam Leffler 			acl->iac_flush(vap);
16928a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
16938a1b9b6aSSam Leffler 		break;
16948a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
16958a1b9b6aSSam Leffler 		if (acl != NULL) {
1696b032f27cSSam Leffler 			vap->iv_acl = NULL;
1697b032f27cSSam Leffler 			acl->iac_detach(vap);
16988a1b9b6aSSam Leffler 		}
16998a1b9b6aSSam Leffler 		break;
17008a1b9b6aSSam Leffler 	default:
1701188757f5SSam Leffler 		if (acl == NULL)
17028a1b9b6aSSam Leffler 			return EINVAL;
1703188757f5SSam Leffler 		else
1704b032f27cSSam Leffler 			return acl->iac_setioctl(vap, ireq);
17058a1b9b6aSSam Leffler 	}
17068a1b9b6aSSam Leffler 	return 0;
17078a1b9b6aSSam Leffler }
17088a1b9b6aSSam Leffler 
170974bdb731SAndriy Voskoboinyk static int
1710b032f27cSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
17118a1b9b6aSSam Leffler {
1712b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17138658b18bSSam Leffler 	uint8_t *chanlist, *list;
17148658b18bSSam Leffler 	int i, nchan, maxchan, error;
17158a1b9b6aSSam Leffler 
17168658b18bSSam Leffler 	if (ireq->i_len > sizeof(ic->ic_chan_active))
17178658b18bSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1718b9b53389SAdrian Chadd 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1719b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
17208658b18bSSam Leffler 	if (list == NULL)
17218658b18bSSam Leffler 		return ENOMEM;
17228658b18bSSam Leffler 	error = copyin(ireq->i_data, list, ireq->i_len);
17237b4d954cSRui Paulo 	if (error) {
1724b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
17258a1b9b6aSSam Leffler 		return error;
17267b4d954cSRui Paulo 	}
172768e8e04eSSam Leffler 	nchan = 0;
17288658b18bSSam Leffler 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
17298658b18bSSam Leffler 	maxchan = ireq->i_len * NBBY;
173031378b1cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
173131378b1cSSam Leffler 		const struct ieee80211_channel *c = &ic->ic_channels[i];
17328a1b9b6aSSam Leffler 		/*
173331378b1cSSam Leffler 		 * Calculate the intersection of the user list and the
173431378b1cSSam Leffler 		 * available channels so users can do things like specify
173531378b1cSSam Leffler 		 * 1-255 to get all available channels.
17368a1b9b6aSSam Leffler 		 */
17378658b18bSSam Leffler 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
173831378b1cSSam Leffler 			setbit(chanlist, c->ic_ieee);
173968e8e04eSSam Leffler 			nchan++;
17408a1b9b6aSSam Leffler 		}
17418a1b9b6aSSam Leffler 	}
17427b4d954cSRui Paulo 	if (nchan == 0) {
1743b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
174468e8e04eSSam Leffler 		return EINVAL;
17457b4d954cSRui Paulo 	}
174668e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
174768e8e04eSSam Leffler 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
174868e8e04eSSam Leffler 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
17498658b18bSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1750b032f27cSSam Leffler 	ieee80211_scan_flush(vap);
1751b9b53389SAdrian Chadd 	IEEE80211_FREE(list, M_TEMP);
1752b032f27cSSam Leffler 	return ENETRESET;
17538a1b9b6aSSam Leffler }
17548a1b9b6aSSam Leffler 
175574bdb731SAndriy Voskoboinyk static int
1756b032f27cSSam Leffler ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
175742568791SSam Leffler {
175842568791SSam Leffler 	struct ieee80211_node *ni;
175968e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
176042568791SSam Leffler 	int error;
176142568791SSam Leffler 
176242568791SSam Leffler 	/*
176342568791SSam Leffler 	 * NB: we could copyin ieee80211req_sta_stats so apps
176442568791SSam Leffler 	 *     could make selective changes but that's overkill;
176542568791SSam Leffler 	 *     just clear all stats for now.
176642568791SSam Leffler 	 */
176742568791SSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
176842568791SSam Leffler 		return EINVAL;
176942568791SSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
177042568791SSam Leffler 	if (error != 0)
177142568791SSam Leffler 		return error;
1772b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
177342568791SSam Leffler 	if (ni == NULL)
1774b032f27cSSam Leffler 		return ENOENT;
1775b032f27cSSam Leffler 	/* XXX require ni_vap == vap? */
177642568791SSam Leffler 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
177742568791SSam Leffler 	ieee80211_free_node(ni);
177842568791SSam Leffler 	return 0;
177942568791SSam Leffler }
178042568791SSam Leffler 
178174bdb731SAndriy Voskoboinyk static int
1782b032f27cSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
17838a1b9b6aSSam Leffler {
17848a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17858a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
17868a1b9b6aSSam Leffler 	int error;
17878a1b9b6aSSam Leffler 
17888a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
17898a1b9b6aSSam Leffler 		return EINVAL;
17908a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
17918a1b9b6aSSam Leffler 	if (error != 0)
17928a1b9b6aSSam Leffler 		return error;
1793b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
17948a1b9b6aSSam Leffler 	if (ni == NULL)
1795b032f27cSSam Leffler 		return ENOENT;
17968a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
17978a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
17988a1b9b6aSSam Leffler 	return error;
17998a1b9b6aSSam Leffler }
18008a1b9b6aSSam Leffler 
180174bdb731SAndriy Voskoboinyk static int
1802b032f27cSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
18038a1b9b6aSSam Leffler {
1804b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
18058a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
18068a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
1807fa46d9dbSAdrian Chadd 	int isbss, ac, aggrmode;
18088a1b9b6aSSam Leffler 
18098a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1810b032f27cSSam Leffler 		return EOPNOTSUPP;
18118a1b9b6aSSam Leffler 
18128a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
18138a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1814fa46d9dbSAdrian Chadd 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
18158a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
18168a1b9b6aSSam Leffler 		ac = WME_AC_BE;
18178a1b9b6aSSam Leffler 	if (isbss) {
18188a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
18198a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
18208a1b9b6aSSam Leffler 	} else {
18218a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
18228a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
18238a1b9b6aSSam Leffler 	}
18248a1b9b6aSSam Leffler 	switch (ireq->i_type) {
18258a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
18268a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = ireq->i_val;
1827fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18288a1b9b6aSSam Leffler 			chanp->wmep_logcwmin = ireq->i_val;
18298a1b9b6aSSam Leffler 		break;
18308a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
18318a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = ireq->i_val;
1832fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18338a1b9b6aSSam Leffler 			chanp->wmep_logcwmax = ireq->i_val;
18348a1b9b6aSSam Leffler 		break;
18358a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18368a1b9b6aSSam Leffler 		wmep->wmep_aifsn = ireq->i_val;
1837fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18388a1b9b6aSSam Leffler 			chanp->wmep_aifsn = ireq->i_val;
18398a1b9b6aSSam Leffler 		break;
18408a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18418a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = ireq->i_val;
1842fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18438a1b9b6aSSam Leffler 			chanp->wmep_txopLimit = ireq->i_val;
18448a1b9b6aSSam Leffler 		break;
18458a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
18468a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
1847fa46d9dbSAdrian Chadd 		if (!aggrmode)
18488a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
18498a1b9b6aSSam Leffler 		break;
18508a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
18518a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
18528a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
18538a1b9b6aSSam Leffler 		break;
18548a1b9b6aSSam Leffler 	}
1855b032f27cSSam Leffler 	ieee80211_wme_updateparams(vap);
18568a1b9b6aSSam Leffler 	return 0;
18578a1b9b6aSSam Leffler }
18588a1b9b6aSSam Leffler 
18598a1b9b6aSSam Leffler static int
186068e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int start, int freq)
186168e8e04eSSam Leffler {
186268e8e04eSSam Leffler 	const struct ieee80211_channel *c;
186368e8e04eSSam Leffler 	int i;
186468e8e04eSSam Leffler 
186568e8e04eSSam Leffler 	for (i = start+1; i < ic->ic_nchans; i++) {
186668e8e04eSSam Leffler 		c = &ic->ic_channels[i];
186768e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
186868e8e04eSSam Leffler 			return 1;
186968e8e04eSSam Leffler 	}
187068e8e04eSSam Leffler 	/* NB: should not be needed but in case things are mis-sorted */
187168e8e04eSSam Leffler 	for (i = 0; i < start; i++) {
187268e8e04eSSam Leffler 		c = &ic->ic_channels[i];
187368e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
187468e8e04eSSam Leffler 			return 1;
187568e8e04eSSam Leffler 	}
187668e8e04eSSam Leffler 	return 0;
187768e8e04eSSam Leffler }
187868e8e04eSSam Leffler 
187968e8e04eSSam Leffler static struct ieee80211_channel *
188068e8e04eSSam Leffler findchannel(struct ieee80211com *ic, int ieee, int mode)
188168e8e04eSSam Leffler {
188268e8e04eSSam Leffler 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1883be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= 0,
1884be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1885be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1886be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1887be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1888be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1889be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1890be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
18916a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
18926a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
189368e8e04eSSam Leffler 	    /* NB: handled specially below */
1894be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1895be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
189635bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IEEE80211_CHAN_A,
189735bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IEEE80211_CHAN_G,
189868e8e04eSSam Leffler 	};
189968e8e04eSSam Leffler 	u_int modeflags;
190068e8e04eSSam Leffler 	int i;
190168e8e04eSSam Leffler 
190268e8e04eSSam Leffler 	modeflags = chanflags[mode];
190368e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
190468e8e04eSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
190568e8e04eSSam Leffler 
190668e8e04eSSam Leffler 		if (c->ic_ieee != ieee)
190768e8e04eSSam Leffler 			continue;
190868e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
190968e8e04eSSam Leffler 			/* ignore turbo channels for autoselect */
191068e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_TURBO(c))
191168e8e04eSSam Leffler 				continue;
191268e8e04eSSam Leffler 			/*
191368e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we
191468e8e04eSSam Leffler 			 *     always select the g channel if both
191568e8e04eSSam Leffler 			 *     are present.
191668e8e04eSSam Leffler 			 * XXX prefer HT to non-HT?
191768e8e04eSSam Leffler 			 */
191868e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_B(c) ||
191968e8e04eSSam Leffler 			    !find11gchannel(ic, i, c->ic_freq))
192068e8e04eSSam Leffler 				return c;
192168e8e04eSSam Leffler 		} else {
192235bcfd1cSAdrian Chadd 			/* must check VHT specifically */
192335bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_VHT_5GHZ ||
192435bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_VHT_2GHZ) &&
192535bcfd1cSAdrian Chadd 			    !IEEE80211_IS_CHAN_VHT(c))
192635bcfd1cSAdrian Chadd 				continue;
192735bcfd1cSAdrian Chadd 
192835bcfd1cSAdrian Chadd 			/*
192935bcfd1cSAdrian Chadd 			 * Must check HT specially - only match on HT,
193035bcfd1cSAdrian Chadd 			 * not HT+VHT channels
193135bcfd1cSAdrian Chadd 			 */
193268e8e04eSSam Leffler 			if ((mode == IEEE80211_MODE_11NA ||
193368e8e04eSSam Leffler 			    mode == IEEE80211_MODE_11NG) &&
193468e8e04eSSam Leffler 			    !IEEE80211_IS_CHAN_HT(c))
193568e8e04eSSam Leffler 				continue;
193635bcfd1cSAdrian Chadd 
193735bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_11NA ||
193835bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_11NG) &&
193935bcfd1cSAdrian Chadd 			    IEEE80211_IS_CHAN_VHT(c))
194035bcfd1cSAdrian Chadd 				continue;
194135bcfd1cSAdrian Chadd 
194235bcfd1cSAdrian Chadd 			/* Check that the modeflags above match */
194368e8e04eSSam Leffler 			if ((c->ic_flags & modeflags) == modeflags)
194468e8e04eSSam Leffler 				return c;
194568e8e04eSSam Leffler 		}
194668e8e04eSSam Leffler 	}
194768e8e04eSSam Leffler 	return NULL;
194868e8e04eSSam Leffler }
194968e8e04eSSam Leffler 
195068e8e04eSSam Leffler /*
195168e8e04eSSam Leffler  * Check the specified against any desired mode (aka netband).
195268e8e04eSSam Leffler  * This is only used (presently) when operating in hostap mode
195368e8e04eSSam Leffler  * to enforce consistency.
195468e8e04eSSam Leffler  */
195568e8e04eSSam Leffler static int
195668e8e04eSSam Leffler check_mode_consistency(const struct ieee80211_channel *c, int mode)
195768e8e04eSSam Leffler {
195868e8e04eSSam Leffler 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
195968e8e04eSSam Leffler 
196068e8e04eSSam Leffler 	switch (mode) {
196168e8e04eSSam Leffler 	case IEEE80211_MODE_11B:
196268e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_B(c));
196368e8e04eSSam Leffler 	case IEEE80211_MODE_11G:
196468e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
196568e8e04eSSam Leffler 	case IEEE80211_MODE_11A:
196668e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
196768e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
196868e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_STURBO(c));
196968e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
197068e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTA(c));
197168e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
197268e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTG(c));
197368e8e04eSSam Leffler 	}
197468e8e04eSSam Leffler 	return 1;
197568e8e04eSSam Leffler 
197668e8e04eSSam Leffler }
197768e8e04eSSam Leffler 
197868e8e04eSSam Leffler /*
197968e8e04eSSam Leffler  * Common code to set the current channel.  If the device
198068e8e04eSSam Leffler  * is up and running this may result in an immediate channel
198168e8e04eSSam Leffler  * change or a kick of the state machine.
198268e8e04eSSam Leffler  */
198368e8e04eSSam Leffler static int
1984b032f27cSSam Leffler setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
198568e8e04eSSam Leffler {
1986b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
198768e8e04eSSam Leffler 	int error;
198868e8e04eSSam Leffler 
198968e8e04eSSam Leffler 	if (c != IEEE80211_CHAN_ANYC) {
1990b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1991b032f27cSSam Leffler 			return EBUSY;	/* XXX better code? */
1992b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1993b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
199468e8e04eSSam Leffler 				return EINVAL;
1995b032f27cSSam Leffler 			if (!check_mode_consistency(c, vap->iv_des_mode))
1996b032f27cSSam Leffler 				return EINVAL;
1997b032f27cSSam Leffler 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1998b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1999b032f27cSSam Leffler 				return EINVAL;
2000b032f27cSSam Leffler 		}
20011b74d2a4SAdrian Chadd 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
2002b032f27cSSam Leffler 		    vap->iv_bss->ni_chan == c)
200368e8e04eSSam Leffler 			return 0;	/* NB: nothing to do */
200468e8e04eSSam Leffler 	}
2005b032f27cSSam Leffler 	vap->iv_des_chan = c;
200668e8e04eSSam Leffler 
200768e8e04eSSam Leffler 	error = 0;
2008b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
2009b032f27cSSam Leffler 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
201068e8e04eSSam Leffler 		/*
2011b032f27cSSam Leffler 		 * Monitor mode can switch directly.
201268e8e04eSSam Leffler 		 */
2013b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
2014b032f27cSSam Leffler 			/* XXX need state machine for other vap's to follow */
2015b032f27cSSam Leffler 			ieee80211_setcurchan(ic, vap->iv_des_chan);
2016b032f27cSSam Leffler 			vap->iv_bss->ni_chan = ic->ic_curchan;
2017d2ffc7afSPedro F. Giffuni 		} else {
2018b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
201926d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2020d2ffc7afSPedro F. Giffuni 		}
202168e8e04eSSam Leffler 	} else {
202268e8e04eSSam Leffler 		/*
202368e8e04eSSam Leffler 		 * Need to go through the state machine in case we
202468e8e04eSSam Leffler 		 * need to reassociate or the like.  The state machine
202568e8e04eSSam Leffler 		 * will pickup the desired channel and avoid scanning.
202668e8e04eSSam Leffler 		 */
2027b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
2028b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2029b032f27cSSam Leffler 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
203068e8e04eSSam Leffler 			/*
203168e8e04eSSam Leffler 			 * When not up+running and a real channel has
203268e8e04eSSam Leffler 			 * been specified fix the current channel so
203368e8e04eSSam Leffler 			 * there is immediate feedback; e.g. via ifconfig.
203468e8e04eSSam Leffler 			 */
2035b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
203626d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
203768e8e04eSSam Leffler 		}
203868e8e04eSSam Leffler 	}
203968e8e04eSSam Leffler 	return error;
204068e8e04eSSam Leffler }
204168e8e04eSSam Leffler 
204268e8e04eSSam Leffler /*
204368e8e04eSSam Leffler  * Old api for setting the current channel; this is
204468e8e04eSSam Leffler  * deprecated because channel numbers are ambiguous.
204568e8e04eSSam Leffler  */
204674bdb731SAndriy Voskoboinyk static int
2047b032f27cSSam Leffler ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
204868e8e04eSSam Leffler 	const struct ieee80211req *ireq)
204968e8e04eSSam Leffler {
2050b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
205168e8e04eSSam Leffler 	struct ieee80211_channel *c;
205268e8e04eSSam Leffler 
205368e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
205468e8e04eSSam Leffler 	if (ireq->i_val == 0 ||
205568e8e04eSSam Leffler 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
205668e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
205768e8e04eSSam Leffler 	} else {
205868e8e04eSSam Leffler 		struct ieee80211_channel *c2;
205968e8e04eSSam Leffler 
2060b032f27cSSam Leffler 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
206168e8e04eSSam Leffler 		if (c == NULL) {
206268e8e04eSSam Leffler 			c = findchannel(ic, ireq->i_val,
206368e8e04eSSam Leffler 				IEEE80211_MODE_AUTO);
206468e8e04eSSam Leffler 			if (c == NULL)
206568e8e04eSSam Leffler 				return EINVAL;
206668e8e04eSSam Leffler 		}
206735bcfd1cSAdrian Chadd 
206868e8e04eSSam Leffler 		/*
206968e8e04eSSam Leffler 		 * Fine tune channel selection based on desired mode:
207068e8e04eSSam Leffler 		 *   if 11b is requested, find the 11b version of any
207168e8e04eSSam Leffler 		 *      11g channel returned,
207268e8e04eSSam Leffler 		 *   if static turbo, find the turbo version of any
207368e8e04eSSam Leffler 		 *	11a channel return,
207468e8e04eSSam Leffler 		 *   if 11na is requested, find the ht version of any
207568e8e04eSSam Leffler 		 *      11a channel returned,
207668e8e04eSSam Leffler 		 *   if 11ng is requested, find the ht version of any
207768e8e04eSSam Leffler 		 *      11g channel returned,
207835bcfd1cSAdrian Chadd 		 *   if 11ac is requested, find the 11ac version
207935bcfd1cSAdrian Chadd 		 *      of any 11a/11na channel returned,
208035bcfd1cSAdrian Chadd 		 *   (TBD) 11acg (2GHz VHT)
208168e8e04eSSam Leffler 		 *   otherwise we should be ok with what we've got.
208268e8e04eSSam Leffler 		 */
2083b032f27cSSam Leffler 		switch (vap->iv_des_mode) {
208468e8e04eSSam Leffler 		case IEEE80211_MODE_11B:
208568e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
208668e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
208768e8e04eSSam Leffler 					IEEE80211_MODE_11B);
208868e8e04eSSam Leffler 				/* NB: should not happen, =>'s 11g w/o 11b */
208968e8e04eSSam Leffler 				if (c2 != NULL)
209068e8e04eSSam Leffler 					c = c2;
209168e8e04eSSam Leffler 			}
209268e8e04eSSam Leffler 			break;
209368e8e04eSSam Leffler 		case IEEE80211_MODE_TURBO_A:
209468e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
209568e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
209668e8e04eSSam Leffler 					IEEE80211_MODE_TURBO_A);
209768e8e04eSSam Leffler 				if (c2 != NULL)
209868e8e04eSSam Leffler 					c = c2;
209968e8e04eSSam Leffler 			}
210068e8e04eSSam Leffler 			break;
210168e8e04eSSam Leffler 		case IEEE80211_MODE_11NA:
210268e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
210368e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
210468e8e04eSSam Leffler 					IEEE80211_MODE_11NA);
210568e8e04eSSam Leffler 				if (c2 != NULL)
210668e8e04eSSam Leffler 					c = c2;
210768e8e04eSSam Leffler 			}
210868e8e04eSSam Leffler 			break;
210968e8e04eSSam Leffler 		case IEEE80211_MODE_11NG:
211068e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
211168e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
211268e8e04eSSam Leffler 					IEEE80211_MODE_11NG);
211368e8e04eSSam Leffler 				if (c2 != NULL)
211468e8e04eSSam Leffler 					c = c2;
211568e8e04eSSam Leffler 			}
211668e8e04eSSam Leffler 			break;
211735bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_2GHZ:
211835bcfd1cSAdrian Chadd 			printf("%s: TBD\n", __func__);
211935bcfd1cSAdrian Chadd 			break;
212035bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_5GHZ:
212135bcfd1cSAdrian Chadd 			if (IEEE80211_IS_CHAN_A(c)) {
212235bcfd1cSAdrian Chadd 				c2 = findchannel(ic, ireq->i_val,
212335bcfd1cSAdrian Chadd 					IEEE80211_MODE_VHT_5GHZ);
212435bcfd1cSAdrian Chadd 				if (c2 != NULL)
212535bcfd1cSAdrian Chadd 					c = c2;
212635bcfd1cSAdrian Chadd 			}
212735bcfd1cSAdrian Chadd 			break;
212868e8e04eSSam Leffler 		default:		/* NB: no static turboG */
212968e8e04eSSam Leffler 			break;
213068e8e04eSSam Leffler 		}
213168e8e04eSSam Leffler 	}
2132b032f27cSSam Leffler 	return setcurchan(vap, c);
213368e8e04eSSam Leffler }
213468e8e04eSSam Leffler 
213568e8e04eSSam Leffler /*
213668e8e04eSSam Leffler  * New/current api for setting the current channel; a complete
213768e8e04eSSam Leffler  * channel description is provide so there is no ambiguity in
213868e8e04eSSam Leffler  * identifying the channel.
213968e8e04eSSam Leffler  */
214074bdb731SAndriy Voskoboinyk static int
2141b032f27cSSam Leffler ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
214268e8e04eSSam Leffler 	const struct ieee80211req *ireq)
214368e8e04eSSam Leffler {
2144b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
214568e8e04eSSam Leffler 	struct ieee80211_channel chan, *c;
214668e8e04eSSam Leffler 	int error;
214768e8e04eSSam Leffler 
214868e8e04eSSam Leffler 	if (ireq->i_len != sizeof(chan))
214968e8e04eSSam Leffler 		return EINVAL;
215068e8e04eSSam Leffler 	error = copyin(ireq->i_data, &chan, sizeof(chan));
215168e8e04eSSam Leffler 	if (error != 0)
215268e8e04eSSam Leffler 		return error;
215335bcfd1cSAdrian Chadd 
215468e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
215568e8e04eSSam Leffler 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
215668e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
215768e8e04eSSam Leffler 	} else {
215868e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
215968e8e04eSSam Leffler 		if (c == NULL)
216068e8e04eSSam Leffler 			return EINVAL;
216168e8e04eSSam Leffler 	}
2162b032f27cSSam Leffler 	return setcurchan(vap, c);
2163b032f27cSSam Leffler }
2164b032f27cSSam Leffler 
216574bdb731SAndriy Voskoboinyk static int
2166b032f27cSSam Leffler ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2167b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2168b032f27cSSam Leffler {
2169b032f27cSSam Leffler 	struct ieee80211_regdomain_req *reg;
21708658b18bSSam Leffler 	int nchans, error;
2171b032f27cSSam Leffler 
21728658b18bSSam Leffler 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
21738658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
21748658b18bSSam Leffler 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
21758658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21768658b18bSSam Leffler 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
21778658b18bSSam Leffler 		    ireq->i_len, nchans);
2178b032f27cSSam Leffler 		return EINVAL;
21798658b18bSSam Leffler 	}
21808658b18bSSam Leffler 	reg = (struct ieee80211_regdomain_req *)
2181b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2182b9b53389SAdrian Chadd 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
21838658b18bSSam Leffler 	if (reg == NULL) {
21848658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21858658b18bSSam Leffler 		    "%s: no memory, nchans %d\n", __func__, nchans);
2186b032f27cSSam Leffler 		return ENOMEM;
21878658b18bSSam Leffler 	}
21888658b18bSSam Leffler 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
21898658b18bSSam Leffler 	if (error == 0) {
21908658b18bSSam Leffler 		/* NB: validate inline channel count against storage size */
21918658b18bSSam Leffler 		if (reg->chaninfo.ic_nchans != nchans) {
21928658b18bSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21938658b18bSSam Leffler 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
21948658b18bSSam Leffler 				reg->chaninfo.ic_nchans, nchans);
21958658b18bSSam Leffler 			error = EINVAL;
21968658b18bSSam Leffler 		} else
2197b032f27cSSam Leffler 			error = ieee80211_setregdomain(vap, reg);
21988658b18bSSam Leffler 	}
2199b9b53389SAdrian Chadd 	IEEE80211_FREE(reg, M_TEMP);
2200b032f27cSSam Leffler 
2201b032f27cSSam Leffler 	return (error == 0 ? ENETRESET : error);
220268e8e04eSSam Leffler }
220368e8e04eSSam Leffler 
220468e8e04eSSam Leffler static int
2205b032f27cSSam Leffler checkrate(const struct ieee80211_rateset *rs, int rate)
2206b032f27cSSam Leffler {
2207b032f27cSSam Leffler 	int i;
2208b032f27cSSam Leffler 
2209b032f27cSSam Leffler 	if (rate == IEEE80211_FIXED_RATE_NONE)
2210b032f27cSSam Leffler 		return 1;
2211b032f27cSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
2212b032f27cSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2213b032f27cSSam Leffler 			return 1;
2214b032f27cSSam Leffler 	return 0;
2215b032f27cSSam Leffler }
2216b032f27cSSam Leffler 
2217b032f27cSSam Leffler static int
2218dfabbaa0SAndriy Voskoboinyk checkmcs(const struct ieee80211_htrateset *rs, int mcs)
2219b032f27cSSam Leffler {
2220dfabbaa0SAndriy Voskoboinyk 	int rate_val = IEEE80211_RV(mcs);
2221dfabbaa0SAndriy Voskoboinyk 	int i;
2222dfabbaa0SAndriy Voskoboinyk 
2223b032f27cSSam Leffler 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2224b032f27cSSam Leffler 		return 1;
2225b032f27cSSam Leffler 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2226b032f27cSSam Leffler 		return 0;
2227dfabbaa0SAndriy Voskoboinyk 	for (i = 0; i < rs->rs_nrates; i++)
2228dfabbaa0SAndriy Voskoboinyk 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
2229dfabbaa0SAndriy Voskoboinyk 			return 1;
2230dfabbaa0SAndriy Voskoboinyk 	return 0;
2231b032f27cSSam Leffler }
2232b032f27cSSam Leffler 
223374bdb731SAndriy Voskoboinyk static int
2234545619f3SAndriy Voskoboinyk ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2235545619f3SAndriy Voskoboinyk         const struct ieee80211req *ireq)
2236545619f3SAndriy Voskoboinyk {
2237545619f3SAndriy Voskoboinyk 	struct ieee80211com *ic = vap->iv_ic;
2238545619f3SAndriy Voskoboinyk 	struct ieee80211_roamparams_req *parms;
2239545619f3SAndriy Voskoboinyk 	struct ieee80211_roamparam *src, *dst;
2240545619f3SAndriy Voskoboinyk 	const struct ieee80211_htrateset *rs_ht;
2241545619f3SAndriy Voskoboinyk 	const struct ieee80211_rateset *rs;
2242545619f3SAndriy Voskoboinyk 	int changed, error, mode, is11n, nmodes;
2243545619f3SAndriy Voskoboinyk 
2244545619f3SAndriy Voskoboinyk 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2245545619f3SAndriy Voskoboinyk 		return EINVAL;
2246545619f3SAndriy Voskoboinyk 
2247545619f3SAndriy Voskoboinyk 	parms = IEEE80211_MALLOC(sizeof(*parms), M_TEMP,
2248545619f3SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2249545619f3SAndriy Voskoboinyk 	if (parms == NULL)
2250545619f3SAndriy Voskoboinyk 		return ENOMEM;
2251545619f3SAndriy Voskoboinyk 
2252545619f3SAndriy Voskoboinyk 	error = copyin(ireq->i_data, parms, ireq->i_len);
2253545619f3SAndriy Voskoboinyk 	if (error != 0)
2254545619f3SAndriy Voskoboinyk 		goto fail;
2255545619f3SAndriy Voskoboinyk 
2256545619f3SAndriy Voskoboinyk 	changed = 0;
2257545619f3SAndriy Voskoboinyk 	nmodes = IEEE80211_MODE_MAX;
2258545619f3SAndriy Voskoboinyk 
2259545619f3SAndriy Voskoboinyk 	/* validate parameters and check if anything changed */
2260545619f3SAndriy Voskoboinyk 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2261545619f3SAndriy Voskoboinyk 		if (isclr(ic->ic_modecaps, mode))
2262545619f3SAndriy Voskoboinyk 			continue;
2263545619f3SAndriy Voskoboinyk 		src = &parms->params[mode];
2264545619f3SAndriy Voskoboinyk 		dst = &vap->iv_roamparms[mode];
2265545619f3SAndriy Voskoboinyk 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2266545619f3SAndriy Voskoboinyk 		rs_ht = &ic->ic_sup_htrates;
2267545619f3SAndriy Voskoboinyk 		is11n = (mode == IEEE80211_MODE_11NA ||
2268545619f3SAndriy Voskoboinyk 			 mode == IEEE80211_MODE_11NG);
2269545619f3SAndriy Voskoboinyk 		/* XXX TODO: 11ac */
2270545619f3SAndriy Voskoboinyk 		if (src->rate != dst->rate) {
2271545619f3SAndriy Voskoboinyk 			if (!checkrate(rs, src->rate) &&
2272545619f3SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->rate))) {
2273545619f3SAndriy Voskoboinyk 				error = EINVAL;
2274545619f3SAndriy Voskoboinyk 				goto fail;
2275545619f3SAndriy Voskoboinyk 			}
2276545619f3SAndriy Voskoboinyk 			changed++;
2277545619f3SAndriy Voskoboinyk 		}
2278545619f3SAndriy Voskoboinyk 		if (src->rssi != dst->rssi)
2279545619f3SAndriy Voskoboinyk 			changed++;
2280545619f3SAndriy Voskoboinyk 	}
2281545619f3SAndriy Voskoboinyk 	if (changed) {
2282545619f3SAndriy Voskoboinyk 		/*
2283545619f3SAndriy Voskoboinyk 		 * Copy new parameters in place and notify the
2284545619f3SAndriy Voskoboinyk 		 * driver so it can push state to the device.
2285545619f3SAndriy Voskoboinyk 		 */
2286545619f3SAndriy Voskoboinyk 		/* XXX locking? */
2287545619f3SAndriy Voskoboinyk 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2288545619f3SAndriy Voskoboinyk 			if (isset(ic->ic_modecaps, mode))
2289545619f3SAndriy Voskoboinyk 				vap->iv_roamparms[mode] = parms->params[mode];
2290545619f3SAndriy Voskoboinyk 		}
2291545619f3SAndriy Voskoboinyk 
2292545619f3SAndriy Voskoboinyk 		if (vap->iv_roaming == IEEE80211_ROAMING_DEVICE)
2293545619f3SAndriy Voskoboinyk 			error = ERESTART;
2294545619f3SAndriy Voskoboinyk 	}
2295545619f3SAndriy Voskoboinyk 
2296545619f3SAndriy Voskoboinyk fail:	IEEE80211_FREE(parms, M_TEMP);
2297545619f3SAndriy Voskoboinyk 	return error;
2298545619f3SAndriy Voskoboinyk }
2299545619f3SAndriy Voskoboinyk 
2300545619f3SAndriy Voskoboinyk static int
2301b032f27cSSam Leffler ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2302b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2303b032f27cSSam Leffler {
2304b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2305b032f27cSSam Leffler 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2306b032f27cSSam Leffler 	struct ieee80211_txparam *src, *dst;
2307dfabbaa0SAndriy Voskoboinyk 	const struct ieee80211_htrateset *rs_ht;
2308b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2309cbb24c24SSam Leffler 	int error, mode, changed, is11n, nmodes;
2310b032f27cSSam Leffler 
2311cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
2312cbb24c24SSam Leffler 	if (ireq->i_len > sizeof(parms))
2313b032f27cSSam Leffler 		return EINVAL;
2314cbb24c24SSam Leffler 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2315b032f27cSSam Leffler 	if (error != 0)
2316b032f27cSSam Leffler 		return error;
2317cbb24c24SSam Leffler 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2318b032f27cSSam Leffler 	changed = 0;
2319b032f27cSSam Leffler 	/* validate parameters and check if anything changed */
2320cbb24c24SSam Leffler 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2321cbb24c24SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
2322b032f27cSSam Leffler 			continue;
2323cbb24c24SSam Leffler 		src = &parms.params[mode];
2324cbb24c24SSam Leffler 		dst = &vap->iv_txparms[mode];
2325cbb24c24SSam Leffler 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2326dfabbaa0SAndriy Voskoboinyk 		rs_ht = &ic->ic_sup_htrates;
2327cbb24c24SSam Leffler 		is11n = (mode == IEEE80211_MODE_11NA ||
2328cbb24c24SSam Leffler 			 mode == IEEE80211_MODE_11NG);
2329b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2330cbb24c24SSam Leffler 			if (!checkrate(rs, src->ucastrate) &&
2331dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
2332b032f27cSSam Leffler 				return EINVAL;
2333b032f27cSSam Leffler 			changed++;
2334b032f27cSSam Leffler 		}
2335b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2336cbb24c24SSam Leffler 			if (!checkrate(rs, src->mcastrate) &&
2337dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
2338b032f27cSSam Leffler 				return EINVAL;
2339b032f27cSSam Leffler 			changed++;
2340b032f27cSSam Leffler 		}
2341b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2342cbb24c24SSam Leffler 			if (!checkrate(rs, src->mgmtrate) &&
2343dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
2344b032f27cSSam Leffler 				return EINVAL;
2345b032f27cSSam Leffler 			changed++;
2346b032f27cSSam Leffler 		}
2347b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2348b032f27cSSam Leffler 			changed++;
2349b032f27cSSam Leffler 	}
2350b032f27cSSam Leffler 	if (changed) {
2351b032f27cSSam Leffler 		/*
2352b032f27cSSam Leffler 		 * Copy new parameters in place and notify the
2353b032f27cSSam Leffler 		 * driver so it can push state to the device.
2354b032f27cSSam Leffler 		 */
2355cbb24c24SSam Leffler 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2356cbb24c24SSam Leffler 			if (isset(ic->ic_modecaps, mode))
2357cbb24c24SSam Leffler 				vap->iv_txparms[mode] = parms.params[mode];
2358b032f27cSSam Leffler 		}
2359b032f27cSSam Leffler 		/* XXX could be more intelligent,
2360b032f27cSSam Leffler 		   e.g. don't reset if setting not being used */
2361b032f27cSSam Leffler 		return ENETRESET;
2362b032f27cSSam Leffler 	}
2363b032f27cSSam Leffler 	return 0;
2364b032f27cSSam Leffler }
2365b032f27cSSam Leffler 
2366b032f27cSSam Leffler /*
2367b032f27cSSam Leffler  * Application Information Element support.
2368b032f27cSSam Leffler  */
2369b032f27cSSam Leffler static int
2370b032f27cSSam Leffler setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2371b032f27cSSam Leffler {
2372b032f27cSSam Leffler 	struct ieee80211_appie *app = *aie;
2373b032f27cSSam Leffler 	struct ieee80211_appie *napp;
2374b032f27cSSam Leffler 	int error;
2375b032f27cSSam Leffler 
2376b032f27cSSam Leffler 	if (ireq->i_len == 0) {		/* delete any existing ie */
2377b032f27cSSam Leffler 		if (app != NULL) {
2378b032f27cSSam Leffler 			*aie = NULL;	/* XXX racey */
2379b9b53389SAdrian Chadd 			IEEE80211_FREE(app, M_80211_NODE_IE);
2380b032f27cSSam Leffler 		}
2381b032f27cSSam Leffler 		return 0;
2382b032f27cSSam Leffler 	}
2383b032f27cSSam Leffler 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2384b032f27cSSam Leffler 		return EINVAL;
2385b032f27cSSam Leffler 	/*
2386b032f27cSSam Leffler 	 * Allocate a new appie structure and copy in the user data.
2387b032f27cSSam Leffler 	 * When done swap in the new structure.  Note that we do not
2388b032f27cSSam Leffler 	 * guard against users holding a ref to the old structure;
2389b032f27cSSam Leffler 	 * this must be handled outside this code.
2390b032f27cSSam Leffler 	 *
2391b032f27cSSam Leffler 	 * XXX bad bad bad
2392b032f27cSSam Leffler 	 */
2393b9b53389SAdrian Chadd 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2394b9b53389SAdrian Chadd 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2395b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT);
2396b032f27cSSam Leffler 	if (napp == NULL)
2397b032f27cSSam Leffler 		return ENOMEM;
2398b032f27cSSam Leffler 	/* XXX holding ic lock */
2399b032f27cSSam Leffler 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2400b032f27cSSam Leffler 	if (error) {
2401b9b53389SAdrian Chadd 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2402b032f27cSSam Leffler 		return error;
2403b032f27cSSam Leffler 	}
2404b032f27cSSam Leffler 	napp->ie_len = ireq->i_len;
2405b032f27cSSam Leffler 	*aie = napp;
2406b032f27cSSam Leffler 	if (app != NULL)
2407b9b53389SAdrian Chadd 		IEEE80211_FREE(app, M_80211_NODE_IE);
2408b032f27cSSam Leffler 	return 0;
2409b032f27cSSam Leffler }
2410b032f27cSSam Leffler 
2411b032f27cSSam Leffler static void
2412b032f27cSSam Leffler setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2413b032f27cSSam Leffler {
2414b032f27cSSam Leffler 	/* validate data is present as best we can */
2415b032f27cSSam Leffler 	if (space == 0 || 2+ie[1] > space)
2416b032f27cSSam Leffler 		return;
2417b032f27cSSam Leffler 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2418b032f27cSSam Leffler 		vap->iv_wpa_ie = ie;
2419b032f27cSSam Leffler 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2420b032f27cSSam Leffler 		vap->iv_rsn_ie = ie;
2421b032f27cSSam Leffler }
2422b032f27cSSam Leffler 
242374bdb731SAndriy Voskoboinyk static int
2424b032f27cSSam Leffler ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2425b032f27cSSam Leffler 	const struct ieee80211req *ireq, int fc0)
2426b032f27cSSam Leffler {
2427b032f27cSSam Leffler 	int error;
2428b032f27cSSam Leffler 
2429b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2430b032f27cSSam Leffler 
2431b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2432b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2433b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2434b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2435b032f27cSSam Leffler 			error = EINVAL;
2436b032f27cSSam Leffler 			break;
2437b032f27cSSam Leffler 		}
2438b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_beacon, ireq);
2439b032f27cSSam Leffler 		if (error == 0)
2440b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2441b032f27cSSam Leffler 		break;
2442b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2443b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_proberesp, ireq);
2444b032f27cSSam Leffler 		break;
2445b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2446b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2447b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocresp, ireq);
2448b032f27cSSam Leffler 		else
2449b032f27cSSam Leffler 			error = EINVAL;
2450b032f27cSSam Leffler 		break;
2451b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2452b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_probereq, ireq);
2453b032f27cSSam Leffler 		break;
2454b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2455b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
2456b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocreq, ireq);
2457b032f27cSSam Leffler 		else
2458b032f27cSSam Leffler 			error = EINVAL;
2459b032f27cSSam Leffler 		break;
2460b032f27cSSam Leffler 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2461b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_wpa, ireq);
2462b032f27cSSam Leffler 		if (error == 0) {
2463b032f27cSSam Leffler 			/*
2464b032f27cSSam Leffler 			 * Must split single blob of data into separate
2465b032f27cSSam Leffler 			 * WPA and RSN ie's because they go in different
2466b032f27cSSam Leffler 			 * locations in the mgt frames.
2467b032f27cSSam Leffler 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2468b032f27cSSam Leffler 			 */
2469b032f27cSSam Leffler 			vap->iv_wpa_ie = NULL;
2470b032f27cSSam Leffler 			vap->iv_rsn_ie = NULL;
2471b032f27cSSam Leffler 			if (vap->iv_appie_wpa != NULL) {
2472b032f27cSSam Leffler 				struct ieee80211_appie *appie =
2473b032f27cSSam Leffler 				    vap->iv_appie_wpa;
2474b032f27cSSam Leffler 				uint8_t *data = appie->ie_data;
2475b032f27cSSam Leffler 
2476b032f27cSSam Leffler 				/* XXX ie length validate is painful, cheat */
2477b032f27cSSam Leffler 				setwparsnie(vap, data, appie->ie_len);
2478b032f27cSSam Leffler 				setwparsnie(vap, data + 2 + data[1],
2479b032f27cSSam Leffler 				    appie->ie_len - (2 + data[1]));
2480b032f27cSSam Leffler 			}
2481b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2482b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2483b032f27cSSam Leffler 				/*
2484b032f27cSSam Leffler 				 * Must rebuild beacon frame as the update
2485b032f27cSSam Leffler 				 * mechanism doesn't handle WPA/RSN ie's.
2486b032f27cSSam Leffler 				 * Could extend it but it doesn't normally
2487b032f27cSSam Leffler 				 * change; this is just to deal with hostapd
2488b032f27cSSam Leffler 				 * plumbing the ie after the interface is up.
2489b032f27cSSam Leffler 				 */
2490b032f27cSSam Leffler 				error = ENETRESET;
2491b032f27cSSam Leffler 			}
2492b032f27cSSam Leffler 		}
2493b032f27cSSam Leffler 		break;
2494b032f27cSSam Leffler 	default:
2495b032f27cSSam Leffler 		error = EINVAL;
2496b032f27cSSam Leffler 		break;
2497b032f27cSSam Leffler 	}
2498b032f27cSSam Leffler 	return error;
2499b032f27cSSam Leffler }
2500b032f27cSSam Leffler 
250174bdb731SAndriy Voskoboinyk static int
2502b032f27cSSam Leffler ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2503b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2504b032f27cSSam Leffler {
2505b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2506b032f27cSSam Leffler 	int error;
2507b032f27cSSam Leffler 	uint8_t fc0;
2508b032f27cSSam Leffler 
2509b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
2510b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2511b032f27cSSam Leffler 		return EINVAL;
2512b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2513b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2514b032f27cSSam Leffler 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2515b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2516b032f27cSSam Leffler 	return error;
2517b032f27cSSam Leffler }
2518b032f27cSSam Leffler 
251974bdb731SAndriy Voskoboinyk static int
2520b032f27cSSam Leffler ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2521b032f27cSSam Leffler {
2522b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2523b032f27cSSam Leffler 	struct ieee80211_chanswitch_req csr;
2524b032f27cSSam Leffler 	struct ieee80211_channel *c;
2525b032f27cSSam Leffler 	int error;
2526b032f27cSSam Leffler 
2527b032f27cSSam Leffler 	if (ireq->i_len != sizeof(csr))
2528b032f27cSSam Leffler 		return EINVAL;
2529b032f27cSSam Leffler 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2530b032f27cSSam Leffler 	if (error != 0)
2531b032f27cSSam Leffler 		return error;
2532c70761e6SSam Leffler 	/* XXX adhoc mode not supported */
2533c70761e6SSam Leffler 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2534c70761e6SSam Leffler 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2535c70761e6SSam Leffler 		return EOPNOTSUPP;
2536b032f27cSSam Leffler 	c = ieee80211_find_channel(ic,
2537b032f27cSSam Leffler 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2538b032f27cSSam Leffler 	if (c == NULL)
2539b032f27cSSam Leffler 		return ENOENT;
2540b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2541b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2542b032f27cSSam Leffler 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2543c70761e6SSam Leffler 	else if (csr.csa_count == 0)
2544c70761e6SSam Leffler 		ieee80211_csa_cancelswitch(ic);
2545b032f27cSSam Leffler 	else
2546b032f27cSSam Leffler 		error = EBUSY;
2547b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2548b032f27cSSam Leffler 	return error;
2549b032f27cSSam Leffler }
2550b032f27cSSam Leffler 
2551632ee7e3SBernhard Schmidt static int
2552632ee7e3SBernhard Schmidt ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2553b032f27cSSam Leffler {
2554b032f27cSSam Leffler #define	IEEE80211_IOC_SCAN_FLAGS \
2555b032f27cSSam Leffler 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2556b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2557b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2558b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2559b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_CHECK)
2560b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25617ca7a42cSBernhard Schmidt 	int error, i;
2562b032f27cSSam Leffler 
2563b032f27cSSam Leffler 	/* convert duration */
2564632ee7e3SBernhard Schmidt 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2565632ee7e3SBernhard Schmidt 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2566b032f27cSSam Leffler 	else {
2567632ee7e3SBernhard Schmidt 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2568632ee7e3SBernhard Schmidt 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2569b032f27cSSam Leffler 			return EINVAL;
2570632ee7e3SBernhard Schmidt 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2571b032f27cSSam Leffler 	}
2572b032f27cSSam Leffler 	/* convert min/max channel dwell */
25739df9e936SAndriy Voskoboinyk 	if (sr->sr_mindwell != 0)
2574632ee7e3SBernhard Schmidt 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
25759df9e936SAndriy Voskoboinyk 	if (sr->sr_maxdwell != 0)
2576632ee7e3SBernhard Schmidt 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2577b032f27cSSam Leffler 	/* NB: silently reduce ssid count to what is supported */
2578632ee7e3SBernhard Schmidt 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2579632ee7e3SBernhard Schmidt 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2580632ee7e3SBernhard Schmidt 	for (i = 0; i < sr->sr_nssid; i++)
2581632ee7e3SBernhard Schmidt 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2582b032f27cSSam Leffler 			return EINVAL;
2583b032f27cSSam Leffler 	/* cleanse flags just in case, could reject if invalid flags */
2584632ee7e3SBernhard Schmidt 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2585b032f27cSSam Leffler 	/*
2586b032f27cSSam Leffler 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2587b032f27cSSam Leffler 	 * allows applications to scan without joining a bss (or picking
2588b032f27cSSam Leffler 	 * a channel and setting up a bss) and without forcing manual
2589b032f27cSSam Leffler 	 * roaming mode--you just need to mark the parent device UP.
2590b032f27cSSam Leffler 	 */
2591b032f27cSSam Leffler 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2592632ee7e3SBernhard Schmidt 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2593b032f27cSSam Leffler 
2594b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2595b032f27cSSam Leffler 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2596632ee7e3SBernhard Schmidt 	    __func__, sr->sr_flags,
2597b032f27cSSam Leffler 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2598632ee7e3SBernhard Schmidt 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2599b032f27cSSam Leffler 	/*
2600b032f27cSSam Leffler 	 * If we are in INIT state then the driver has never had a chance
2601b032f27cSSam Leffler 	 * to setup hardware state to do a scan; we must use the state
2602b032f27cSSam Leffler 	 * machine to get us up to the SCAN state but once we reach SCAN
2603b032f27cSSam Leffler 	 * state we then want to use the supplied params.  Stash the
2604b032f27cSSam Leffler 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2605b032f27cSSam Leffler 	 * state machines will recognize this and use the stashed params
2606b032f27cSSam Leffler 	 * to issue the scan request.
2607b032f27cSSam Leffler 	 *
2608b032f27cSSam Leffler 	 * Otherwise just invoke the scan machinery directly.
2609b032f27cSSam Leffler 	 */
2610b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2611f20503faSAndriy Voskoboinyk 	if (ic->ic_nrunning == 0) {
2612f20503faSAndriy Voskoboinyk 		IEEE80211_UNLOCK(ic);
2613f20503faSAndriy Voskoboinyk 		return ENXIO;
2614f20503faSAndriy Voskoboinyk 	}
2615f20503faSAndriy Voskoboinyk 
2616b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_INIT) {
2617b032f27cSSam Leffler 		/* NB: clobbers previous settings */
2618632ee7e3SBernhard Schmidt 		vap->iv_scanreq_flags = sr->sr_flags;
2619632ee7e3SBernhard Schmidt 		vap->iv_scanreq_duration = sr->sr_duration;
2620632ee7e3SBernhard Schmidt 		vap->iv_scanreq_nssid = sr->sr_nssid;
2621632ee7e3SBernhard Schmidt 		for (i = 0; i < sr->sr_nssid; i++) {
2622632ee7e3SBernhard Schmidt 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2623632ee7e3SBernhard Schmidt 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2624632ee7e3SBernhard Schmidt 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2625b032f27cSSam Leffler 		}
2626b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2627b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2628b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2629b032f27cSSam Leffler 	} else {
2630b032f27cSSam Leffler 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2631b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2632632ee7e3SBernhard Schmidt 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
26337ca7a42cSBernhard Schmidt 			error = ieee80211_check_scan(vap, sr->sr_flags,
2634632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2635632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2636b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2637632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2638b032f27cSSam Leffler 		} else {
26397ca7a42cSBernhard Schmidt 			error = ieee80211_start_scan(vap, sr->sr_flags,
2640632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2641632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2642b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2643632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2644b032f27cSSam Leffler 		}
26457ca7a42cSBernhard Schmidt 		if (error == 0)
26467ca7a42cSBernhard Schmidt 			return EINPROGRESS;
2647b032f27cSSam Leffler 	}
2648632ee7e3SBernhard Schmidt 	return 0;
2649b032f27cSSam Leffler #undef IEEE80211_IOC_SCAN_FLAGS
2650b032f27cSSam Leffler }
2651b032f27cSSam Leffler 
265274bdb731SAndriy Voskoboinyk static int
2653632ee7e3SBernhard Schmidt ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2654632ee7e3SBernhard Schmidt {
265574bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
2656632ee7e3SBernhard Schmidt 	int error;
2657632ee7e3SBernhard Schmidt 
265874bdb731SAndriy Voskoboinyk 	if (ireq->i_len != sizeof(*sr))
2659632ee7e3SBernhard Schmidt 		return EINVAL;
266074bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
266174bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
266274bdb731SAndriy Voskoboinyk 	if (sr == NULL)
266374bdb731SAndriy Voskoboinyk 		return ENOMEM;
266474bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, sr, sizeof(*sr));
2665632ee7e3SBernhard Schmidt 	if (error != 0)
266674bdb731SAndriy Voskoboinyk 		goto bad;
266774bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
266874bdb731SAndriy Voskoboinyk bad:
266974bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
2670632ee7e3SBernhard Schmidt 	return error;
2671632ee7e3SBernhard Schmidt }
2672632ee7e3SBernhard Schmidt 
267374bdb731SAndriy Voskoboinyk static int
2674b032f27cSSam Leffler ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2675b032f27cSSam Leffler {
2676b032f27cSSam Leffler 	struct ieee80211_node *ni;
2677b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
2678b032f27cSSam Leffler 	int error;
2679b032f27cSSam Leffler 
2680b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
2681b032f27cSSam Leffler 		return EINVAL;
2682b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2683b032f27cSSam Leffler 	if (error != 0)
2684b032f27cSSam Leffler 		return error;
2685b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2686b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2687b032f27cSSam Leffler 		    vlan.sv_macaddr);
2688b032f27cSSam Leffler 		if (ni == NULL)
2689b032f27cSSam Leffler 			return ENOENT;
2690b032f27cSSam Leffler 	} else
2691b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
2692b032f27cSSam Leffler 	ni->ni_vlan = vlan.sv_vlan;
2693b032f27cSSam Leffler 	ieee80211_free_node(ni);
2694b032f27cSSam Leffler 	return error;
2695b032f27cSSam Leffler }
2696b032f27cSSam Leffler 
2697b032f27cSSam Leffler static int
2698b032f27cSSam Leffler isvap11g(const struct ieee80211vap *vap)
2699b032f27cSSam Leffler {
2700b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2701b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2702b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2703b032f27cSSam Leffler }
2704b032f27cSSam Leffler 
2705b032f27cSSam Leffler static int
2706b032f27cSSam Leffler isvapht(const struct ieee80211vap *vap)
2707b032f27cSSam Leffler {
2708b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2709b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2710b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2711b032f27cSSam Leffler }
2712b032f27cSSam Leffler 
27138c4e758aSSam Leffler /*
27148c4e758aSSam Leffler  * Dummy ioctl set handler so the linker set is defined.
27158c4e758aSSam Leffler  */
27168c4e758aSSam Leffler static int
27178c4e758aSSam Leffler dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
27188c4e758aSSam Leffler {
27198c4e758aSSam Leffler 	return ENOSYS;
27208c4e758aSSam Leffler }
27218c4e758aSSam Leffler IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
27228c4e758aSSam Leffler 
27238c4e758aSSam Leffler static int
27248c4e758aSSam Leffler ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
27258c4e758aSSam Leffler {
27268c4e758aSSam Leffler 	ieee80211_ioctl_setfunc * const *set;
27278c4e758aSSam Leffler 	int error;
27288c4e758aSSam Leffler 
27298c4e758aSSam Leffler 	SET_FOREACH(set, ieee80211_ioctl_setset) {
27308c4e758aSSam Leffler 		error = (*set)(vap, ireq);
27318c4e758aSSam Leffler 		if (error != ENOSYS)
27328c4e758aSSam Leffler 			return error;
27338c4e758aSSam Leffler 	}
27348c4e758aSSam Leffler 	return EINVAL;
27358c4e758aSSam Leffler }
27368c4e758aSSam Leffler 
273774bdb731SAndriy Voskoboinyk static int
2738b032f27cSSam Leffler ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2739b032f27cSSam Leffler {
2740b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
27418a1b9b6aSSam Leffler 	int error;
27428a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
274368e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
27448a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
274568e8e04eSSam Leffler 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
27468a1b9b6aSSam Leffler 	struct ieee80211_key *k;
27478a1b9b6aSSam Leffler 	u_int kid;
27483dfff737SWarner Losh 	uint32_t flags;
27498a1b9b6aSSam Leffler 
27508a1b9b6aSSam Leffler 	error = 0;
27511a1e1d21SSam Leffler 	switch (ireq->i_type) {
27521a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
27531a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
27548a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
27558a1b9b6aSSam Leffler 			return EINVAL;
27561a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
27571a1e1d21SSam Leffler 		if (error)
27581a1e1d21SSam Leffler 			break;
2759b032f27cSSam Leffler 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2760b032f27cSSam Leffler 		vap->iv_des_ssid[0].len = ireq->i_len;
2761b032f27cSSam Leffler 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2762b032f27cSSam Leffler 		vap->iv_des_nssid = (ireq->i_len > 0);
2763b032f27cSSam Leffler 		error = ENETRESET;
27641a1e1d21SSam Leffler 		break;
27651a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
27668a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27678a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
2768b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2769b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27708a1b9b6aSSam Leffler 			break;
27718a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
2772b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2773b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
27748a1b9b6aSSam Leffler 			break;
27758a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
2776b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2777b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27788a1b9b6aSSam Leffler 			break;
27791a1e1d21SSam Leffler 		}
2780b032f27cSSam Leffler 		error = ENETRESET;
27811a1e1d21SSam Leffler 		break;
27821a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
27831a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
27848a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
27858a1b9b6aSSam Leffler 			return EINVAL;
2786b032f27cSSam Leffler 		k = &vap->iv_nw_keys[kid];
27878a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
27888a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
2789b032f27cSSam Leffler 			(void) ieee80211_crypto_delkey(vap, k);
27901a1e1d21SSam Leffler 			break;
27911a1e1d21SSam Leffler 		}
27928a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
27938a1b9b6aSSam Leffler 			return EINVAL;
27941a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
27951a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
27961a1e1d21SSam Leffler 		if (error)
27971a1e1d21SSam Leffler 			break;
2798b032f27cSSam Leffler 		ieee80211_key_update_begin(vap);
2799dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2800b032f27cSSam Leffler 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2801dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
28028a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
28038a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
280471fe06caSSam Leffler 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
280571fe06caSSam Leffler 			if  (!ieee80211_crypto_setkey(vap, k))
28068a1b9b6aSSam Leffler 				error = EINVAL;
28078a1b9b6aSSam Leffler 		} else
28088a1b9b6aSSam Leffler 			error = EINVAL;
2809b032f27cSSam Leffler 		ieee80211_key_update_end(vap);
28101a1e1d21SSam Leffler 		break;
28111a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
28121a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2813380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
281468e8e04eSSam Leffler 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
28158a1b9b6aSSam Leffler 			return EINVAL;
2816781487cfSAdrian Chadd 		/*
2817781487cfSAdrian Chadd 		 * Firmware devices may need to be told about an explicit
2818781487cfSAdrian Chadd 		 * key index here, versus just inferring it from the
2819781487cfSAdrian Chadd 		 * key set / change.  Since we may also need to pause
2820781487cfSAdrian Chadd 		 * things like transmit before the key is updated,
2821781487cfSAdrian Chadd 		 * give the driver a chance to flush things by tying
2822781487cfSAdrian Chadd 		 * into key update begin/end.
2823781487cfSAdrian Chadd 		 */
2824781487cfSAdrian Chadd 		ieee80211_key_update_begin(vap);
2825781487cfSAdrian Chadd 		ieee80211_crypto_set_deftxkey(vap, kid);
2826781487cfSAdrian Chadd 		ieee80211_key_update_end(vap);
28278a1b9b6aSSam Leffler 		break;
28288a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
28298a1b9b6aSSam Leffler 		switch (ireq->i_val) {
28308a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
28318a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
28328a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
28338a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
28348a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
28358a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
28368a1b9b6aSSam Leffler 			if (auth == NULL)
28378a1b9b6aSSam Leffler 				return EINVAL;
28388a1b9b6aSSam Leffler 			break;
28398a1b9b6aSSam Leffler 		default:
28408a1b9b6aSSam Leffler 			return EINVAL;
28418a1b9b6aSSam Leffler 		}
28428a1b9b6aSSam Leffler 		switch (ireq->i_val) {
28438a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2844b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28458a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
28468a1b9b6aSSam Leffler 			break;
28478a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
2848b032f27cSSam Leffler 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
28498a1b9b6aSSam Leffler 			break;
28508a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
28518a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2852b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28538a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
2854b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28558a1b9b6aSSam Leffler 			break;
28568a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
2857b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28588a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
28598a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
28601a1e1d21SSam Leffler 			break;
28611a1e1d21SSam Leffler 		}
28628a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
2863b032f27cSSam Leffler 		vap->iv_bss->ni_authmode = ireq->i_val;
28648a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
2865b032f27cSSam Leffler 		vap->iv_auth = auth;
2866b032f27cSSam Leffler 		error = ENETRESET;
28671a1e1d21SSam Leffler 		break;
28681a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
2869b032f27cSSam Leffler 		error = ieee80211_ioctl_setchannel(vap, ireq);
28701a1e1d21SSam Leffler 		break;
28711a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
28721a1e1d21SSam Leffler 		switch (ireq->i_val) {
28731a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
2874b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2875b032f27cSSam Leffler 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2876b032f27cSSam Leffler 				error = ERESTART;
28771a1e1d21SSam Leffler 			}
28781a1e1d21SSam Leffler 			break;
28791a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
2880b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2881b032f27cSSam Leffler 				error = EOPNOTSUPP;
2882b032f27cSSam Leffler 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2883b032f27cSSam Leffler 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2884b032f27cSSam Leffler 				error = ERESTART;
28851a1e1d21SSam Leffler 			}
28861a1e1d21SSam Leffler 			break;
28871a1e1d21SSam Leffler 		default:
28881a1e1d21SSam Leffler 			error = EINVAL;
28891a1e1d21SSam Leffler 			break;
28901a1e1d21SSam Leffler 		}
28911a1e1d21SSam Leffler 		break;
28921a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
28938a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
28948a1b9b6aSSam Leffler 			return EINVAL;
28951a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
2896b032f27cSSam Leffler 		error = ERESTART;
28971a1e1d21SSam Leffler 		break;
289813604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
289970231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
290070231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
29018a1b9b6aSSam Leffler 			return EINVAL;
2902b032f27cSSam Leffler 		vap->iv_rtsthreshold = ireq->i_val;
2903b032f27cSSam Leffler 		error = ERESTART;
29041a1e1d21SSam Leffler 		break;
29052e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
29068a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
29078a1b9b6aSSam Leffler 			return EINVAL;
2908f1481c8dSAdrian Chadd 		vap->iv_protmode = (enum ieee80211_protmode)ireq->i_val;
29092e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
291068e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
291168e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2912b032f27cSSam Leffler 			error = ERESTART;
2913f1481c8dSAdrian Chadd 		/* driver callback for protection mode update */
2914f1481c8dSAdrian Chadd 		ieee80211_vap_update_erp_protmode(vap);
29152e79ca97SSam Leffler 		break;
29162e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
29178a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2918b032f27cSSam Leffler 			return EOPNOTSUPP;
291968e8e04eSSam Leffler 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
292068e8e04eSSam Leffler 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
29218a1b9b6aSSam Leffler 			return EINVAL;
29228a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
2923b032f27cSSam Leffler 		error = ERESTART;
29248a1b9b6aSSam Leffler 		break;
29258a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
29268a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
29278a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
29288a1b9b6aSSam Leffler 			return EINVAL;
2929fcd9500fSBernhard Schmidt 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
29308a1b9b6aSSam Leffler 		/* XXXX reset? */
29318a1b9b6aSSam Leffler 		break;
29328a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
29338a1b9b6aSSam Leffler 		if (ireq->i_val) {
29348a1b9b6aSSam Leffler 			/* XXX check for key state? */
2935b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
29368a1b9b6aSSam Leffler 		} else
2937b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2938b032f27cSSam Leffler 		/* XXX ERESTART? */
29398a1b9b6aSSam Leffler 		break;
29408a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
29418a1b9b6aSSam Leffler 		if (ireq->i_val)
2942b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
29438a1b9b6aSSam Leffler 		else
2944b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2945b032f27cSSam Leffler 		/* XXX ERESTART? */
29468a1b9b6aSSam Leffler 		break;
29478a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
2948b032f27cSSam Leffler 		error = ieee80211_ioctl_setkey(vap, ireq);
29498a1b9b6aSSam Leffler 		break;
29508a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
2951b032f27cSSam Leffler 		error = ieee80211_ioctl_delkey(vap, ireq);
29528a1b9b6aSSam Leffler 		break;
29538a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
2954b032f27cSSam Leffler 		error = ieee80211_ioctl_setmlme(vap, ireq);
29558a1b9b6aSSam Leffler 		break;
29568a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
29578a1b9b6aSSam Leffler 		if (ireq->i_val) {
2958b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2959b032f27cSSam Leffler 				return EOPNOTSUPP;
2960b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_COUNTERM;
29618a1b9b6aSSam Leffler 		} else
2962b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2963b032f27cSSam Leffler 		/* XXX ERESTART? */
29648a1b9b6aSSam Leffler 		break;
29658a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
29668a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
29678a1b9b6aSSam Leffler 			return EINVAL;
29688a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
29693dfff737SWarner Losh 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
29708a1b9b6aSSam Leffler 		switch (ireq->i_val) {
297191356585SAdrian Chadd 		case 0:
297291356585SAdrian Chadd 			/* wpa_supplicant calls this to clear the WPA config */
297391356585SAdrian Chadd 			break;
29748a1b9b6aSSam Leffler 		case 1:
29753dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
29763dfff737SWarner Losh 				return EOPNOTSUPP;
29773dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1;
29788a1b9b6aSSam Leffler 			break;
29798a1b9b6aSSam Leffler 		case 2:
29803dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
29813dfff737SWarner Losh 				return EOPNOTSUPP;
29823dfff737SWarner Losh 			flags |= IEEE80211_F_WPA2;
29838a1b9b6aSSam Leffler 			break;
29848a1b9b6aSSam Leffler 		case 3:
29853dfff737SWarner Losh 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
29868d092df9SSam Leffler 				return EOPNOTSUPP;
29873dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
29882e79ca97SSam Leffler 			break;
29893dfff737SWarner Losh 		default:	/*  Can't set any -> error */
29903dfff737SWarner Losh 			return EOPNOTSUPP;
29912e79ca97SSam Leffler 		}
29923dfff737SWarner Losh 		vap->iv_flags = flags;
2993b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
29948a1b9b6aSSam Leffler 		break;
29958a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
29968a1b9b6aSSam Leffler 		if (ireq->i_val) {
2997b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2998b032f27cSSam Leffler 				return EOPNOTSUPP;
2999b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_WME);
30008a1b9b6aSSam Leffler 		} else
3001b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
3002b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
30038a1b9b6aSSam Leffler 		break;
30048a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
30058a1b9b6aSSam Leffler 		if (ireq->i_val)
3006b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_HIDESSID;
30078a1b9b6aSSam Leffler 		else
3008b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
3009b032f27cSSam Leffler 		error = ERESTART;		/* XXX ENETRESET? */
30102e79ca97SSam Leffler 		break;
30118a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
30128a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
3013b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
30148a1b9b6aSSam Leffler 		else
3015b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
30168a1b9b6aSSam Leffler 		break;
30178a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
30188a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
30198a1b9b6aSSam Leffler 			return EINVAL;
30208a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
30218a1b9b6aSSam Leffler 		if (error)
30228a1b9b6aSSam Leffler 			break;
3023b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
3024b032f27cSSam Leffler 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
3025b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
30268a1b9b6aSSam Leffler 		else
3027b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DESBSSID;
3028b032f27cSSam Leffler 		error = ENETRESET;
30298a1b9b6aSSam Leffler 		break;
30308a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
3031b032f27cSSam Leffler 		error = ieee80211_ioctl_setchanlist(vap, ireq);
30328a1b9b6aSSam Leffler 		break;
3033b032f27cSSam Leffler #define	OLD_IEEE80211_IOC_SCAN_REQ	23
3034b032f27cSSam Leffler #ifdef OLD_IEEE80211_IOC_SCAN_REQ
3035b032f27cSSam Leffler 	case OLD_IEEE80211_IOC_SCAN_REQ:
3036b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3037b032f27cSSam Leffler 			"%s: active scan request\n", __func__);
3038b032f27cSSam Leffler 		/*
3039b032f27cSSam Leffler 		 * If we are in INIT state then the driver has never
3040b032f27cSSam Leffler 		 * had a chance to setup hardware state to do a scan;
3041b032f27cSSam Leffler 		 * use the state machine to get us up the SCAN state.
3042b032f27cSSam Leffler 		 * Otherwise just invoke the scan machinery to start
3043b032f27cSSam Leffler 		 * a one-time scan.
3044b032f27cSSam Leffler 		 */
3045b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
3046b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
3047b032f27cSSam Leffler 		else
3048b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap,
304968e8e04eSSam Leffler 				IEEE80211_SCAN_ACTIVE |
305068e8e04eSSam Leffler 				IEEE80211_SCAN_NOPICK |
3051b032f27cSSam Leffler 				IEEE80211_SCAN_ONCE,
3052b032f27cSSam Leffler 				IEEE80211_SCAN_FOREVER, 0, 0,
305368e8e04eSSam Leffler 				/* XXX use ioctl params */
3054b032f27cSSam Leffler 				vap->iv_des_nssid, vap->iv_des_ssid);
3055b032f27cSSam Leffler 		break;
3056b032f27cSSam Leffler #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
3057b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
3058b032f27cSSam Leffler 		error = ieee80211_ioctl_scanreq(vap, ireq);
3059b032f27cSSam Leffler 		break;
3060b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_CANCEL:
3061b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3062b032f27cSSam Leffler 		    "%s: cancel scan\n", __func__);
3063b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);
3064b032f27cSSam Leffler 		break;
3065b032f27cSSam Leffler 	case IEEE80211_IOC_HTCONF:
3066b032f27cSSam Leffler 		if (ireq->i_val & 1)
30672bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
3068b032f27cSSam Leffler 		else
30692bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
3070b032f27cSSam Leffler 		if (ireq->i_val & 2)
30712bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
3072b032f27cSSam Leffler 		else
30732bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
3074b032f27cSSam Leffler 		error = ENETRESET;
30758a1b9b6aSSam Leffler 		break;
30768a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
30778a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
3078b032f27cSSam Leffler 		error = ieee80211_ioctl_macmac(vap, ireq);
30798a1b9b6aSSam Leffler 		break;
30808a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
3081b032f27cSSam Leffler 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
30828a1b9b6aSSam Leffler 		break;
308342568791SSam Leffler 	case IEEE80211_IOC_STA_STATS:
3084b032f27cSSam Leffler 		error = ieee80211_ioctl_setstastats(vap, ireq);
308542568791SSam Leffler 		break;
30868a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
3087b032f27cSSam Leffler 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
30888a1b9b6aSSam Leffler 		break;
30898a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
30908a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
30918a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
30928a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
30938a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
3094fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
3095b032f27cSSam Leffler 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
30968a1b9b6aSSam Leffler 		break;
30978a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
3098b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
309959aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3100b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
31018a1b9b6aSSam Leffler 			return EINVAL;
31028a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
31038a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
3104b032f27cSSam Leffler 			vap->iv_dtim_period = ireq->i_val;
31058a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
31068a1b9b6aSSam Leffler 		} else
31078a1b9b6aSSam Leffler 			error = EINVAL;
31088a1b9b6aSSam Leffler 		break;
31098a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
3110b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
311159aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3112b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
31138a1b9b6aSSam Leffler 			return EINVAL;
31148a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
31158a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
3116d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
31178a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
31188a1b9b6aSSam Leffler 		} else
31198a1b9b6aSSam Leffler 			error = EINVAL;
31208a1b9b6aSSam Leffler 		break;
3121c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
3122c4f040c3SSam Leffler 		if (ireq->i_val)
3123b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PUREG;
3124c4f040c3SSam Leffler 		else
3125b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PUREG;
3126c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
3127b032f27cSSam Leffler 		if (isvap11g(vap))
3128c4f040c3SSam Leffler 			error = ENETRESET;
3129c4f040c3SSam Leffler 		break;
313032b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
313132b0e64bSAdrian Chadd 		vap->iv_quiet= ireq->i_val;
313232b0e64bSAdrian Chadd 		break;
313332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
313432b0e64bSAdrian Chadd 		vap->iv_quiet_count=ireq->i_val;
313532b0e64bSAdrian Chadd 		break;
313632b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
313732b0e64bSAdrian Chadd 		vap->iv_quiet_period=ireq->i_val;
313832b0e64bSAdrian Chadd 		break;
313932b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
314032b0e64bSAdrian Chadd 		vap->iv_quiet_offset=ireq->i_val;
314132b0e64bSAdrian Chadd 		break;
314232b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
314332b0e64bSAdrian Chadd 		if(ireq->i_val < vap->iv_bss->ni_intval)
314432b0e64bSAdrian Chadd 			vap->iv_quiet_duration = ireq->i_val;
314532b0e64bSAdrian Chadd 		else
314632b0e64bSAdrian Chadd 			error = EINVAL;
314732b0e64bSAdrian Chadd 		break;
314868e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
314968e8e04eSSam Leffler 		if (ireq->i_val) {
3150b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3151b032f27cSSam Leffler 				return EOPNOTSUPP;
3152b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_BGSCAN;
315368e8e04eSSam Leffler 		} else
3154b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
315568e8e04eSSam Leffler 		break;
315668e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
315768e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3158b032f27cSSam Leffler 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
315968e8e04eSSam Leffler 		else
316068e8e04eSSam Leffler 			error = EINVAL;
316168e8e04eSSam Leffler 		break;
316268e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
316368e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3164b032f27cSSam Leffler 			vap->iv_bgscanintvl = ireq->i_val*hz;
316568e8e04eSSam Leffler 		else
316668e8e04eSSam Leffler 			error = EINVAL;
316768e8e04eSSam Leffler 		break;
316868e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
316968e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3170b032f27cSSam Leffler 			vap->iv_scanvalid = ireq->i_val*hz;
317168e8e04eSSam Leffler 		else
317268e8e04eSSam Leffler 			error = EINVAL;
317368e8e04eSSam Leffler 		break;
317470231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
3175b032f27cSSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
317670231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
3177b032f27cSSam Leffler 			return EOPNOTSUPP;
317870231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
317970231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
318070231e3dSSam Leffler 			return EINVAL;
3181b032f27cSSam Leffler 		vap->iv_fragthreshold = ireq->i_val;
3182b032f27cSSam Leffler 		error = ERESTART;
318370231e3dSSam Leffler 		break;
3184c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
3185c27e4e31SSam Leffler 		if (ireq->i_val) {
3186b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3187b032f27cSSam Leffler 				return EOPNOTSUPP;
3188b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3189c27e4e31SSam Leffler 		} else
3190b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3191b032f27cSSam Leffler 		error = ERESTART;
3192c27e4e31SSam Leffler 		break;
3193546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
3194546786c9SSam Leffler 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3195546786c9SSam Leffler 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3196546786c9SSam Leffler 			return EINVAL;
3197b032f27cSSam Leffler 		vap->iv_bmissthreshold = ireq->i_val;
3198b032f27cSSam Leffler 		error = ERESTART;
3199546786c9SSam Leffler 		break;
320068e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
3201b032f27cSSam Leffler 		error = ieee80211_ioctl_setcurchan(vap, ireq);
320268e8e04eSSam Leffler 		break;
320368e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
320468e8e04eSSam Leffler 		if (ireq->i_val) {
320568e8e04eSSam Leffler #define	IEEE80211_HTCAP_SHORTGI \
320668e8e04eSSam Leffler 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3207b032f27cSSam Leffler 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
320868e8e04eSSam Leffler 				return EINVAL;
320968e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
32102bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
321168e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
32122bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
321368e8e04eSSam Leffler #undef IEEE80211_HTCAP_SHORTGI
321468e8e04eSSam Leffler 		} else
32152bfc8a91SSam Leffler 			vap->iv_flags_ht &=
32162bfc8a91SSam Leffler 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3217b032f27cSSam Leffler 		error = ERESTART;
321868e8e04eSSam Leffler 		break;
321968e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
3220b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
322168e8e04eSSam Leffler 			return EINVAL;
322268e8e04eSSam Leffler 		if (ireq->i_val & 1)
32232bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3224b032f27cSSam Leffler 		else
32252bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
322668e8e04eSSam Leffler 		if (ireq->i_val & 2)
32272bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3228b032f27cSSam Leffler 		else
32292bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
323068e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3231b032f27cSSam Leffler 		if (isvapht(vap))
3232b032f27cSSam Leffler 			error = ERESTART;
323368e8e04eSSam Leffler 		break;
323468e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
3235781487cfSAdrian Chadd 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
3236b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3237b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3238b032f27cSSam Leffler 			return EINVAL;
3239b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3240b032f27cSSam Leffler 			vap->iv_ampdu_rxmax = ireq->i_val;
3241b032f27cSSam Leffler 		else
3242b032f27cSSam Leffler 			vap->iv_ampdu_limit = ireq->i_val;
3243b032f27cSSam Leffler 		error = ERESTART;
324468e8e04eSSam Leffler 		break;
324568e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
3246b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3247b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3248b032f27cSSam Leffler 			return EINVAL;
3249b032f27cSSam Leffler 		vap->iv_ampdu_density = ireq->i_val;
3250b032f27cSSam Leffler 		error = ERESTART;
325168e8e04eSSam Leffler 		break;
325268e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
3253b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
325468e8e04eSSam Leffler 			return EINVAL;
325568e8e04eSSam Leffler 		if (ireq->i_val & 1)
32562bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3257b032f27cSSam Leffler 		else
32582bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
325968e8e04eSSam Leffler 		if (ireq->i_val & 2)
32602bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3261b032f27cSSam Leffler 		else
32622bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
326368e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3264b032f27cSSam Leffler 		if (isvapht(vap))
3265b032f27cSSam Leffler 			error = ERESTART;
326668e8e04eSSam Leffler 		break;
326768e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
326868e8e04eSSam Leffler 		/* XXX validate */
3269b032f27cSSam Leffler 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
327068e8e04eSSam Leffler 		break;
327168e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
327268e8e04eSSam Leffler 		if (ireq->i_val) {
32732bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
327468e8e04eSSam Leffler 				return EINVAL;
32752bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
327668e8e04eSSam Leffler 		} else
32772bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
327868e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3279b032f27cSSam Leffler 		if (isvapht(vap))
3280b032f27cSSam Leffler 			error = ERESTART;
328168e8e04eSSam Leffler 		break;
328268e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
328368e8e04eSSam Leffler 		if (ireq->i_val) {
328468e8e04eSSam Leffler #if 0
328568e8e04eSSam Leffler 			/* XXX no capability */
3286b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3287b032f27cSSam Leffler 				return EOPNOTSUPP;
328868e8e04eSSam Leffler #endif
3289b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DOTH;
329068e8e04eSSam Leffler 		} else
3291b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3292b032f27cSSam Leffler 		error = ENETRESET;
3293b032f27cSSam Leffler 		break;
3294b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
3295b032f27cSSam Leffler 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3296b032f27cSSam Leffler 		break;
3297b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
3298b032f27cSSam Leffler 		error = ieee80211_ioctl_setroam(vap, ireq);
3299b032f27cSSam Leffler 		break;
3300b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
3301b032f27cSSam Leffler 		error = ieee80211_ioctl_settxparams(vap, ireq);
330268e8e04eSSam Leffler 		break;
330368e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
330468e8e04eSSam Leffler 		if (ireq->i_val) {
33052bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3306b032f27cSSam Leffler 				return EOPNOTSUPP;
33072bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
330868e8e04eSSam Leffler 		} else
33092bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
331068e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3311b032f27cSSam Leffler 		if (isvapht(vap))
3312b032f27cSSam Leffler 			error = ERESTART;
3313b032f27cSSam Leffler 		break;
3314b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
3315b032f27cSSam Leffler 		if (ireq->i_val) {
3316b032f27cSSam Leffler 			/* NB: DWDS only makes sense for WDS-capable devices */
3317b032f27cSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3318b032f27cSSam Leffler 				return EOPNOTSUPP;
3319b032f27cSSam Leffler 			/* NB: DWDS is used only with ap+sta vaps */
3320b032f27cSSam Leffler 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3321b032f27cSSam Leffler 			    vap->iv_opmode != IEEE80211_M_STA)
3322b032f27cSSam Leffler 				return EINVAL;
3323b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DWDS;
3324f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3325f2a6a13cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3326f2a6a13cSSam Leffler 		} else {
3327b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3328f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3329f2a6a13cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3330f2a6a13cSSam Leffler 		}
333168e8e04eSSam Leffler 		break;
3332c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
3333c066143cSSam Leffler 		if (ireq->i_val)
3334b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3335c066143cSSam Leffler 		else
3336b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3337b032f27cSSam Leffler 		break;
3338b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
3339b032f27cSSam Leffler 		error = ieee80211_ioctl_setappie(vap, ireq);
3340b032f27cSSam Leffler 		break;
3341b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
3342b032f27cSSam Leffler 		if (ireq->i_val) {
3343b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3344b032f27cSSam Leffler 				return EOPNOTSUPP;
3345b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3346b032f27cSSam Leffler 		} else
3347b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3348b032f27cSSam Leffler 		break;
3349b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
3350b032f27cSSam Leffler 		if (ireq->i_val) {
3351b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3352b032f27cSSam Leffler 				return EOPNOTSUPP;
3353b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3354b032f27cSSam Leffler 		} else
3355b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3356b032f27cSSam Leffler 		break;
3357b032f27cSSam Leffler 	case IEEE80211_IOC_CHANSWITCH:
3358b032f27cSSam Leffler 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3359b032f27cSSam Leffler 		break;
3360b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
3361b032f27cSSam Leffler 		if (ireq->i_val) {
3362b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3363b032f27cSSam Leffler 				return EOPNOTSUPP;
3364b032f27cSSam Leffler 			/* NB: DFS requires 11h support */
3365b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3366b032f27cSSam Leffler 				return EINVAL;
3367b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3368b032f27cSSam Leffler 		} else
3369b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3370b032f27cSSam Leffler 		break;
3371b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
3372b032f27cSSam Leffler 		if (ireq->i_val)
3373b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3374b032f27cSSam Leffler 		else
3375b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3376b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
3377b032f27cSSam Leffler 			error = ENETRESET;
3378c066143cSSam Leffler 		break;
33791b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
33801b6167d2SSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
33811b6167d2SSam Leffler 			return EINVAL;
3382f1481c8dSAdrian Chadd 		vap->iv_htprotmode = ireq->i_val ?
33831b6167d2SSam Leffler 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
33841b6167d2SSam Leffler 		/* NB: if not operating in 11n this can wait */
3385b032f27cSSam Leffler 		if (isvapht(vap))
33861b6167d2SSam Leffler 			error = ERESTART;
3387f1481c8dSAdrian Chadd 		/* Notify driver layer of HT protmode changes */
3388f1481c8dSAdrian Chadd 		ieee80211_vap_update_ht_protmode(vap);
33891b6167d2SSam Leffler 		break;
3390b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
3391b032f27cSSam Leffler 		error = ieee80211_ioctl_setstavlan(vap, ireq);
33921b6167d2SSam Leffler 		break;
33938c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
33948c070d69SSam Leffler 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
33958c070d69SSam Leffler 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
33968c070d69SSam Leffler 			return EINVAL;
33978c070d69SSam Leffler 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
33988c070d69SSam Leffler 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
33998c070d69SSam Leffler 			return EOPNOTSUPP;
34008c070d69SSam Leffler 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
34018c070d69SSam Leffler 			ireq->i_val;
34028c070d69SSam Leffler 		/* NB: if not operating in 11n this can wait */
34038c070d69SSam Leffler 		if (isvapht(vap))
34048c070d69SSam Leffler 			error = ERESTART;
34058c070d69SSam Leffler 		break;
340644f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
340744f7a6edSSam Leffler 		if (ireq->i_val != 0) {
340844f7a6edSSam Leffler 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
340944f7a6edSSam Leffler 				return EOPNOTSUPP;
34102bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
341144f7a6edSSam Leffler 		} else
34122bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
341344f7a6edSSam Leffler 		/* NB: if not operating in 11n this can wait */
341444f7a6edSSam Leffler 		if (isvapht(vap))
341544f7a6edSSam Leffler 			error = ERESTART;
341644f7a6edSSam Leffler 		break;
3417ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
3418ec3763bbSAdrian Chadd 		/* Check if we can do STBC TX/RX before changing the setting */
3419ec3763bbSAdrian Chadd 		if ((ireq->i_val & 1) &&
3420ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
3421ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3422ec3763bbSAdrian Chadd 		if ((ireq->i_val & 2) &&
3423ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
3424ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3425ec3763bbSAdrian Chadd 
3426ec3763bbSAdrian Chadd 		/* TX */
3427ec3763bbSAdrian Chadd 		if (ireq->i_val & 1)
3428ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
3429ec3763bbSAdrian Chadd 		else
3430ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
3431ec3763bbSAdrian Chadd 
3432ec3763bbSAdrian Chadd 		/* RX */
3433ec3763bbSAdrian Chadd 		if (ireq->i_val & 2)
3434ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
3435ec3763bbSAdrian Chadd 		else
3436ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
3437ec3763bbSAdrian Chadd 
3438ec3763bbSAdrian Chadd 		/* NB: reset only if we're operating on an 11n channel */
3439ec3763bbSAdrian Chadd 		if (isvapht(vap))
3440ec3763bbSAdrian Chadd 			error = ERESTART;
3441ec3763bbSAdrian Chadd 		break;
3442c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
3443c5bba9daSAndriy Voskoboinyk 		/* Check if we can do LDPC TX/RX before changing the setting */
3444c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 1) &&
3445c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0)
3446c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3447c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 2) &&
3448c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0)
3449c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3450c5bba9daSAndriy Voskoboinyk 
3451c5bba9daSAndriy Voskoboinyk 		/* TX */
3452c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 1)
3453c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
3454c5bba9daSAndriy Voskoboinyk 		else
3455c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX;
3456c5bba9daSAndriy Voskoboinyk 
3457c5bba9daSAndriy Voskoboinyk 		/* RX */
3458c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 2)
3459c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
3460c5bba9daSAndriy Voskoboinyk 		else
3461c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX;
3462c5bba9daSAndriy Voskoboinyk 
3463c5bba9daSAndriy Voskoboinyk 		/* NB: reset only if we're operating on an 11n channel */
3464c5bba9daSAndriy Voskoboinyk 		if (isvapht(vap))
3465c5bba9daSAndriy Voskoboinyk 			error = ERESTART;
3466c5bba9daSAndriy Voskoboinyk 		break;
34678379e8dbSAdrian Chadd 	case IEEE80211_IOC_UAPSD:
34688379e8dbSAdrian Chadd 		if ((vap->iv_caps & IEEE80211_C_UAPSD) == 0)
34698379e8dbSAdrian Chadd 			return EOPNOTSUPP;
34708379e8dbSAdrian Chadd 		if (ireq->i_val == 0)
34718379e8dbSAdrian Chadd 			vap->iv_flags_ext &= ~IEEE80211_FEXT_UAPSD;
34728379e8dbSAdrian Chadd 		else if (ireq->i_val == 1)
34738379e8dbSAdrian Chadd 			vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
34748379e8dbSAdrian Chadd 		else
34758379e8dbSAdrian Chadd 			return EINVAL;
34768379e8dbSAdrian Chadd 		break;
347735bcfd1cSAdrian Chadd 
347835bcfd1cSAdrian Chadd 	/* VHT */
347935bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
348096811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_VHT)
348135bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT);
348235bcfd1cSAdrian Chadd 		else
348335bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT);
348435bcfd1cSAdrian Chadd 
348596811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT40)
348635bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40);
348735bcfd1cSAdrian Chadd 		else
348835bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40);
348935bcfd1cSAdrian Chadd 
349096811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT80)
349135bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80);
349235bcfd1cSAdrian Chadd 		else
349335bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80);
349435bcfd1cSAdrian Chadd 
349596811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT160)
349635bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160);
349735bcfd1cSAdrian Chadd 		else
349835bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160);
349935bcfd1cSAdrian Chadd 
350096811be2SBjoern A. Zeeb 		if (ireq->i_val & IEEE80211_FVHT_USEVHT80P80)
350196811be2SBjoern A. Zeeb 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80);
350296811be2SBjoern A. Zeeb 		else
350396811be2SBjoern A. Zeeb 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80);
350496811be2SBjoern A. Zeeb 
350535bcfd1cSAdrian Chadd 		error = ENETRESET;
350635bcfd1cSAdrian Chadd 		break;
350735bcfd1cSAdrian Chadd 
35081a1e1d21SSam Leffler 	default:
35098c4e758aSSam Leffler 		error = ieee80211_ioctl_setdefault(vap, ireq);
35101a1e1d21SSam Leffler 		break;
35111a1e1d21SSam Leffler 	}
3512b032f27cSSam Leffler 	/*
3513b032f27cSSam Leffler 	 * The convention is that ENETRESET means an operation
3514b032f27cSSam Leffler 	 * requires a complete re-initialization of the device (e.g.
3515b032f27cSSam Leffler 	 * changing something that affects the association state).
3516b032f27cSSam Leffler 	 * ERESTART means the request may be handled with only a
3517b032f27cSSam Leffler 	 * reload of the hardware state.  We hand ERESTART requests
3518b032f27cSSam Leffler 	 * to the iv_reset callback so the driver can decide.  If
3519b032f27cSSam Leffler 	 * a device does not fillin iv_reset then it defaults to one
3520b032f27cSSam Leffler 	 * that returns ENETRESET.  Otherwise a driver may return
3521b032f27cSSam Leffler 	 * ENETRESET (in which case a full reset will be done) or
3522b032f27cSSam Leffler 	 * 0 to mean there's no need to do anything (e.g. when the
3523b032f27cSSam Leffler 	 * change has no effect on the driver/device).
3524b032f27cSSam Leffler 	 */
3525b032f27cSSam Leffler 	if (error == ERESTART)
3526b032f27cSSam Leffler 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3527b032f27cSSam Leffler 		    vap->iv_reset(vap, ireq->i_type) : 0;
3528b032f27cSSam Leffler 	if (error == ENETRESET) {
3529b032f27cSSam Leffler 		/* XXX need to re-think AUTO handling */
3530b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
3531b032f27cSSam Leffler 			ieee80211_init(vap);
3532b032f27cSSam Leffler 		error = 0;
3533b032f27cSSam Leffler 	}
35348a1b9b6aSSam Leffler 	return error;
35358a1b9b6aSSam Leffler }
35368a1b9b6aSSam Leffler 
3537b032f27cSSam Leffler int
3538b032f27cSSam Leffler ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3539b032f27cSSam Leffler {
35401da89db5SSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
35411da89db5SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3542dab61567SAndriy Voskoboinyk 	int error = 0, wait = 0, ic_used;
35438a1b9b6aSSam Leffler 	struct ifreq *ifr;
35448a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
35458a1b9b6aSSam Leffler 
3546dab61567SAndriy Voskoboinyk 	ic_used = (cmd != SIOCSIFMTU && cmd != SIOCG80211STATS);
3547dab61567SAndriy Voskoboinyk 	if (ic_used && (error = ieee80211_com_vincref(vap)) != 0)
3548dab61567SAndriy Voskoboinyk 		return (error);
3549dab61567SAndriy Voskoboinyk 
35508a1b9b6aSSam Leffler 	switch (cmd) {
3551b032f27cSSam Leffler 	case SIOCSIFFLAGS:
3552b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
35536459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
35546459bd28SAndriy Voskoboinyk 			/*
35556459bd28SAndriy Voskoboinyk 			 * Enable promiscuous mode when:
35566459bd28SAndriy Voskoboinyk 			 * 1. Interface is not a member of bridge, or
35576459bd28SAndriy Voskoboinyk 			 * 2. Requested by user, or
35586459bd28SAndriy Voskoboinyk 			 * 3. In monitor (or adhoc-demo) mode.
35596459bd28SAndriy Voskoboinyk 			 */
35606459bd28SAndriy Voskoboinyk 			if (ifp->if_bridge == NULL ||
35616459bd28SAndriy Voskoboinyk 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
35626459bd28SAndriy Voskoboinyk 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
35636459bd28SAndriy Voskoboinyk 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
35646459bd28SAndriy Voskoboinyk 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
35656459bd28SAndriy Voskoboinyk 				ieee80211_promisc(vap,
35666459bd28SAndriy Voskoboinyk 				    ifp->if_flags & IFF_PROMISC);
35676459bd28SAndriy Voskoboinyk 				vap->iv_ifflags ^= IFF_PROMISC;
35686459bd28SAndriy Voskoboinyk 			}
35696459bd28SAndriy Voskoboinyk 		}
35706459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
35717a79cebfSGleb Smirnoff 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
35726459bd28SAndriy Voskoboinyk 			vap->iv_ifflags ^= IFF_ALLMULTI;
35736459bd28SAndriy Voskoboinyk 		}
3574b032f27cSSam Leffler 		if (ifp->if_flags & IFF_UP) {
3575b032f27cSSam Leffler 			/*
3576b032f27cSSam Leffler 			 * Bring ourself up unless we're already operational.
3577b032f27cSSam Leffler 			 * If we're the first vap and the parent is not up
3578b032f27cSSam Leffler 			 * then it will automatically be brought up as a
3579b032f27cSSam Leffler 			 * side-effect of bringing ourself up.
3580b032f27cSSam Leffler 			 */
358160d75eb6SAndriy Voskoboinyk 			if (vap->iv_state == IEEE80211_S_INIT) {
358260d75eb6SAndriy Voskoboinyk 				if (ic->ic_nrunning == 0)
358360d75eb6SAndriy Voskoboinyk 					wait = 1;
3584b032f27cSSam Leffler 				ieee80211_start_locked(vap);
358560d75eb6SAndriy Voskoboinyk 			}
3586b032f27cSSam Leffler 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3587b032f27cSSam Leffler 			/*
3588b032f27cSSam Leffler 			 * Stop ourself.  If we are the last vap to be
3589b032f27cSSam Leffler 			 * marked down the parent will also be taken down.
3590b032f27cSSam Leffler 			 */
359160d75eb6SAndriy Voskoboinyk 			if (ic->ic_nrunning == 1)
359260d75eb6SAndriy Voskoboinyk 				wait = 1;
3593b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
3594b032f27cSSam Leffler 		}
3595b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
35968d9b29f3SAndrew Thompson 		/* Wait for parent ioctl handler if it was queued */
3597c6a35ee4SAndriy Voskoboinyk 		if (wait) {
35982b60ecf1SGleb Smirnoff 			struct epoch_tracker et;
35992b60ecf1SGleb Smirnoff 
3600ae55932eSAndrew Thompson 			ieee80211_waitfor_parent(ic);
3601c6a35ee4SAndriy Voskoboinyk 
3602c6a35ee4SAndriy Voskoboinyk 			/*
3603c6a35ee4SAndriy Voskoboinyk 			 * Check if the MAC address was changed
3604c6a35ee4SAndriy Voskoboinyk 			 * via SIOCSIFLLADDR ioctl.
36057071b803SAndriy Voskoboinyk 			 *
36067071b803SAndriy Voskoboinyk 			 * NB: device may be detached during initialization;
36077071b803SAndriy Voskoboinyk 			 * use if_ioctl for existence check.
3608c6a35ee4SAndriy Voskoboinyk 			 */
36092b60ecf1SGleb Smirnoff 			NET_EPOCH_ENTER(et);
36107071b803SAndriy Voskoboinyk 			if (ifp->if_ioctl == ieee80211_ioctl &&
36117071b803SAndriy Voskoboinyk 			    (ifp->if_flags & IFF_UP) == 0 &&
3612c6a35ee4SAndriy Voskoboinyk 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
3613c6a35ee4SAndriy Voskoboinyk 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
3614c6a35ee4SAndriy Voskoboinyk 				    IF_LLADDR(ifp));
36152b60ecf1SGleb Smirnoff 			NET_EPOCH_EXIT(et);
3616c6a35ee4SAndriy Voskoboinyk 		}
3617b032f27cSSam Leffler 		break;
3618b032f27cSSam Leffler 	case SIOCADDMULTI:
3619b032f27cSSam Leffler 	case SIOCDELMULTI:
36207a79cebfSGleb Smirnoff 		ieee80211_runtask(ic, &ic->ic_mcast_task);
3621b032f27cSSam Leffler 		break;
36228a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
36238a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
3624b032f27cSSam Leffler 		ifr = (struct ifreq *)data;
3625b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
36268a1b9b6aSSam Leffler 		break;
36278a1b9b6aSSam Leffler 	case SIOCG80211:
3628b032f27cSSam Leffler 		error = ieee80211_ioctl_get80211(vap, cmd,
36298a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
36308a1b9b6aSSam Leffler 		break;
36318a1b9b6aSSam Leffler 	case SIOCS80211:
363201e57940SBjoern A. Zeeb 		error = ieee80211_priv_check_vap_manage(cmd, vap, ifp);
36338a1b9b6aSSam Leffler 		if (error == 0)
3634b032f27cSSam Leffler 			error = ieee80211_ioctl_set80211(vap, cmd,
36358a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
36361a1e1d21SSam Leffler 		break;
36371be50176SSam Leffler 	case SIOCG80211STATS:
36381be50176SSam Leffler 		ifr = (struct ifreq *)data;
3639541d96aaSBrooks Davis 		copyout(&vap->iv_stats, ifr_data_get_ptr(ifr),
3640541d96aaSBrooks Davis 		    sizeof (vap->iv_stats));
36411be50176SSam Leffler 		break;
36426f161f03SSam Leffler 	case SIOCSIFMTU:
36436f161f03SSam Leffler 		ifr = (struct ifreq *)data;
36446f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
36456f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
36466f161f03SSam Leffler 			error = EINVAL;
36476f161f03SSam Leffler 		else
36486f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
36496f161f03SSam Leffler 		break;
3650b2e95691SSam Leffler 	case SIOCSIFADDR:
3651b2e95691SSam Leffler 		/*
3652a4641f4eSPedro F. Giffuni 		 * XXX Handle this directly so we can suppress if_init calls.
3653b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
3654b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
3655a4641f4eSPedro F. Giffuni 		 * XXX set IFF_UP and so suppress if_init being called when
3656b2e95691SSam Leffler 		 * XXX it should be.
3657b2e95691SSam Leffler 		 */
3658b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
3659b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
3660b2e95691SSam Leffler #ifdef INET
3661b2e95691SSam Leffler 		case AF_INET:
3662b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3663b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3664b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3665b2e95691SSam Leffler 			}
3666b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
3667b2e95691SSam Leffler 			break;
3668b2e95691SSam Leffler #endif
3669b2e95691SSam Leffler 		default:
3670b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3671b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3672b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3673b2e95691SSam Leffler 			}
3674b2e95691SSam Leffler 			break;
3675b2e95691SSam Leffler 		}
3676b2e95691SSam Leffler 		break;
3677f7d38a13SAdrian Chadd 	case SIOCSIFLLADDR:
367801e57940SBjoern A. Zeeb 		error = ieee80211_priv_check_vap_setmac(cmd, vap, ifp);
3679f7d38a13SAdrian Chadd 		if (error == 0)
3680f7d38a13SAdrian Chadd 			break;
3681f7d38a13SAdrian Chadd 		/* Fallthrough */
3682ba2c1fbcSAdrian Chadd 	default:
36837a79cebfSGleb Smirnoff 		/*
36847a79cebfSGleb Smirnoff 		 * Pass unknown ioctls first to the driver, and if it
36857a79cebfSGleb Smirnoff 		 * returns ENOTTY, then to the generic Ethernet handler.
36867a79cebfSGleb Smirnoff 		 */
36877a79cebfSGleb Smirnoff 		if (ic->ic_ioctl != NULL &&
36887a79cebfSGleb Smirnoff 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
36897a79cebfSGleb Smirnoff 			break;
36901a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
36911a1e1d21SSam Leffler 		break;
36921a1e1d21SSam Leffler 	}
3693dab61567SAndriy Voskoboinyk 
3694dab61567SAndriy Voskoboinyk 	if (ic_used)
3695dab61567SAndriy Voskoboinyk 		ieee80211_com_vdecref(vap);
3696dab61567SAndriy Voskoboinyk 
36977a79cebfSGleb Smirnoff 	return (error);
36981a1e1d21SSam Leffler }
3699