xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 541d96aaaf46cde0f4247c226ed29d635b1915c4)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
321a1e1d21SSam Leffler /*
331a1e1d21SSam Leffler  * IEEE 802.11 ioctl support (FreeBSD-specific)
341a1e1d21SSam Leffler  */
351a1e1d21SSam Leffler 
36b2e95691SSam Leffler #include "opt_inet.h"
37b032f27cSSam Leffler #include "opt_wlan.h"
38b2e95691SSam Leffler 
391a1e1d21SSam Leffler #include <sys/endian.h>
401a1e1d21SSam Leffler #include <sys/param.h>
411a1e1d21SSam Leffler #include <sys/kernel.h>
428ec07310SGleb Smirnoff #include <sys/malloc.h>
43acd3428bSRobert Watson #include <sys/priv.h>
441a1e1d21SSam Leffler #include <sys/socket.h>
451a1e1d21SSam Leffler #include <sys/sockio.h>
461a1e1d21SSam Leffler #include <sys/systm.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
504a0d6638SRuslan Ermilov #include <net/if_dl.h>
511a1e1d21SSam Leffler #include <net/if_media.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
75b032f27cSSam Leffler ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
768a1b9b6aSSam Leffler {
77b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
788a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
798a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
808a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
818a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
828a1b9b6aSSam Leffler 	u_int kid;
838a1b9b6aSSam Leffler 	int error;
848a1b9b6aSSam Leffler 
858a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
868a1b9b6aSSam Leffler 		return EINVAL;
878a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
888a1b9b6aSSam Leffler 	if (error)
898a1b9b6aSSam Leffler 		return error;
908a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
918a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
92b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
938a1b9b6aSSam Leffler 		if (ni == NULL)
94b032f27cSSam Leffler 			return ENOENT;
958a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
968a1b9b6aSSam Leffler 	} else {
978a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
988a1b9b6aSSam Leffler 			return EINVAL;
99b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
100b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
1018a1b9b6aSSam Leffler 		ni = NULL;
1028a1b9b6aSSam Leffler 	}
1038a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
1048a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
1058a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
1068a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
107b032f27cSSam Leffler 	if (wk->wk_keyix == vap->iv_def_txkey)
1088a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
109acd3428bSRobert Watson 	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
1108a1b9b6aSSam Leffler 		/* NB: only root can read key data */
111b032f27cSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
1128a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
1138a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
1148a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
1158a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
1168a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
1178a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
1188a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
1198a1b9b6aSSam Leffler 		}
1208a1b9b6aSSam Leffler 	} else {
1218a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
1228a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
1238a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
1248a1b9b6aSSam Leffler 	}
1258a1b9b6aSSam Leffler 	if (ni != NULL)
1268a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
1278a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
1288a1b9b6aSSam Leffler }
1298a1b9b6aSSam Leffler 
13074bdb731SAndriy Voskoboinyk static int
131b032f27cSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1328a1b9b6aSSam Leffler {
133b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1348a1b9b6aSSam Leffler 
135e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
1368a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1378a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
1388a1b9b6aSSam Leffler }
1398a1b9b6aSSam Leffler 
14074bdb731SAndriy Voskoboinyk static int
141b032f27cSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
1428a1b9b6aSSam Leffler {
143b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
144db9ff08bSKevin Lo 	uint32_t space;
1458a1b9b6aSSam Leffler 
1468a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
14768e8e04eSSam Leffler 			ic_chans[ic->ic_nchans]);
1488a1b9b6aSSam Leffler 	if (space > ireq->i_len)
1498a1b9b6aSSam Leffler 		space = ireq->i_len;
15068e8e04eSSam Leffler 	/* XXX assumes compatible layout */
15168e8e04eSSam Leffler 	return copyout(&ic->ic_nchans, ireq->i_data, space);
1528a1b9b6aSSam Leffler }
1538a1b9b6aSSam Leffler 
15474bdb731SAndriy Voskoboinyk static int
155b032f27cSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
156b032f27cSSam Leffler 	struct ieee80211req *ireq, int req)
1578a1b9b6aSSam Leffler {
1588a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
15974bdb731SAndriy Voskoboinyk 	struct ieee80211req_wpaie2 *wpaie;
1608a1b9b6aSSam Leffler 	int error;
1618a1b9b6aSSam Leffler 
1628a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1638a1b9b6aSSam Leffler 		return EINVAL;
16474bdb731SAndriy Voskoboinyk 	wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP,
16574bdb731SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
16674bdb731SAndriy Voskoboinyk 	if (wpaie == NULL)
16774bdb731SAndriy Voskoboinyk 		return ENOMEM;
16874bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN);
1698a1b9b6aSSam Leffler 	if (error != 0)
17074bdb731SAndriy Voskoboinyk 		goto bad;
17174bdb731SAndriy Voskoboinyk 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr);
17274bdb731SAndriy Voskoboinyk 	if (ni == NULL) {
17374bdb731SAndriy Voskoboinyk 		error = ENOENT;
17474bdb731SAndriy Voskoboinyk 		goto bad;
17574bdb731SAndriy Voskoboinyk 	}
176b032f27cSSam Leffler 	if (ni->ni_ies.wpa_ie != NULL) {
177b032f27cSSam Leffler 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
17874bdb731SAndriy Voskoboinyk 		if (ielen > sizeof(wpaie->wpa_ie))
17974bdb731SAndriy Voskoboinyk 			ielen = sizeof(wpaie->wpa_ie);
18074bdb731SAndriy Voskoboinyk 		memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen);
1818a1b9b6aSSam Leffler 	}
18268e8e04eSSam Leffler 	if (req == IEEE80211_IOC_WPAIE2) {
183b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
184b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
18574bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->rsn_ie))
18674bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->rsn_ie);
18774bdb731SAndriy Voskoboinyk 			memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen);
18868e8e04eSSam Leffler 		}
18968e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
19068e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
19168e8e04eSSam Leffler 	} else {
19268e8e04eSSam Leffler 		/* compatibility op, may overwrite wpa ie */
19368e8e04eSSam Leffler 		/* XXX check ic_flags? */
194b032f27cSSam Leffler 		if (ni->ni_ies.rsn_ie != NULL) {
195b032f27cSSam Leffler 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
19674bdb731SAndriy Voskoboinyk 			if (ielen > sizeof(wpaie->wpa_ie))
19774bdb731SAndriy Voskoboinyk 				ielen = sizeof(wpaie->wpa_ie);
19874bdb731SAndriy Voskoboinyk 			memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen);
19968e8e04eSSam Leffler 		}
20068e8e04eSSam Leffler 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
20168e8e04eSSam Leffler 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
20268e8e04eSSam Leffler 	}
2038a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20474bdb731SAndriy Voskoboinyk 	error = copyout(wpaie, ireq->i_data, ireq->i_len);
20574bdb731SAndriy Voskoboinyk bad:
20674bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(wpaie, M_TEMP);
20774bdb731SAndriy Voskoboinyk 	return error;
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
21074bdb731SAndriy Voskoboinyk static int
211b032f27cSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
21468e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
215db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
2168a1b9b6aSSam Leffler 	int error;
2178a1b9b6aSSam Leffler 
2188a1b9b6aSSam Leffler 	if (ireq->i_len < off)
2198a1b9b6aSSam Leffler 		return EINVAL;
2208a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
2218a1b9b6aSSam Leffler 	if (error != 0)
2228a1b9b6aSSam Leffler 		return error;
223b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
22468e8e04eSSam Leffler 	if (ni == NULL)
225b032f27cSSam Leffler 		return ENOENT;
2268a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
2278a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
2288a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
22968e8e04eSSam Leffler 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
2308a1b9b6aSSam Leffler 			ireq->i_len - off);
2318a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
2328a1b9b6aSSam Leffler 	return error;
2338a1b9b6aSSam Leffler }
2348a1b9b6aSSam Leffler 
23568e8e04eSSam Leffler struct scanreq {
23668e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
23768e8e04eSSam Leffler 	size_t space;
23868e8e04eSSam Leffler };
23968e8e04eSSam Leffler 
24068e8e04eSSam Leffler static size_t
24168e8e04eSSam Leffler scan_space(const struct ieee80211_scan_entry *se, int *ielen)
24268e8e04eSSam Leffler {
24368e8e04eSSam Leffler 	size_t len;
24468e8e04eSSam Leffler 
245b032f27cSSam Leffler 	*ielen = se->se_ies.len;
24668e8e04eSSam Leffler 	/*
24768e8e04eSSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
24868e8e04eSSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
24968e8e04eSSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
25068e8e04eSSam Leffler 	 */
25159aa14a9SRui Paulo 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
25259aa14a9SRui Paulo 	    se->se_meshid[1] + *ielen;
25368e8e04eSSam Leffler 	return roundup(len, sizeof(uint32_t));
25468e8e04eSSam Leffler }
25568e8e04eSSam Leffler 
25668e8e04eSSam Leffler static void
25768e8e04eSSam Leffler get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
25868e8e04eSSam Leffler {
25968e8e04eSSam Leffler 	struct scanreq *req = arg;
26068e8e04eSSam Leffler 	int ielen;
26168e8e04eSSam Leffler 
26268e8e04eSSam Leffler 	req->space += scan_space(se, &ielen);
26368e8e04eSSam Leffler }
26468e8e04eSSam Leffler 
26574bdb731SAndriy Voskoboinyk static void
26668e8e04eSSam Leffler get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
26768e8e04eSSam Leffler {
26868e8e04eSSam Leffler 	struct scanreq *req = arg;
26968e8e04eSSam Leffler 	struct ieee80211req_scan_result *sr;
27068e8e04eSSam Leffler 	int ielen, len, nr, nxr;
27168e8e04eSSam Leffler 	uint8_t *cp;
27268e8e04eSSam Leffler 
27368e8e04eSSam Leffler 	len = scan_space(se, &ielen);
27468e8e04eSSam Leffler 	if (len > req->space)
27568e8e04eSSam Leffler 		return;
27668e8e04eSSam Leffler 
27768e8e04eSSam Leffler 	sr = req->sr;
27868e8e04eSSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535,
27968e8e04eSSam Leffler 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
280b032f27cSSam Leffler 	sr->isr_len = len;
28168e8e04eSSam Leffler 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
28268e8e04eSSam Leffler 	sr->isr_ie_len = ielen;
28368e8e04eSSam Leffler 	sr->isr_freq = se->se_chan->ic_freq;
28468e8e04eSSam Leffler 	sr->isr_flags = se->se_chan->ic_flags;
28568e8e04eSSam Leffler 	sr->isr_rssi = se->se_rssi;
28668e8e04eSSam Leffler 	sr->isr_noise = se->se_noise;
28768e8e04eSSam Leffler 	sr->isr_intval = se->se_intval;
28868e8e04eSSam Leffler 	sr->isr_capinfo = se->se_capinfo;
28968e8e04eSSam Leffler 	sr->isr_erp = se->se_erp;
29068e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
29168e8e04eSSam Leffler 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
29268e8e04eSSam Leffler 	memcpy(sr->isr_rates, se->se_rates+2, nr);
29368e8e04eSSam Leffler 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
29468e8e04eSSam Leffler 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
29568e8e04eSSam Leffler 	sr->isr_nrates = nr + nxr;
29668e8e04eSSam Leffler 
29759aa14a9SRui Paulo 	/* copy SSID */
29868e8e04eSSam Leffler 	sr->isr_ssid_len = se->se_ssid[1];
29968e8e04eSSam Leffler 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
30068e8e04eSSam Leffler 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
30168e8e04eSSam Leffler 
30259aa14a9SRui Paulo 	/* copy mesh id */
30368e8e04eSSam Leffler 	cp += sr->isr_ssid_len;
30459aa14a9SRui Paulo 	sr->isr_meshid_len = se->se_meshid[1];
30559aa14a9SRui Paulo 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
30659aa14a9SRui Paulo 	cp += sr->isr_meshid_len;
30759aa14a9SRui Paulo 
30859aa14a9SRui Paulo 	if (ielen)
309b032f27cSSam Leffler 		memcpy(cp, se->se_ies.data, ielen);
31068e8e04eSSam Leffler 
31168e8e04eSSam Leffler 	req->space -= len;
31268e8e04eSSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
31368e8e04eSSam Leffler }
31468e8e04eSSam Leffler 
31574bdb731SAndriy Voskoboinyk static int
316b032f27cSSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
317b032f27cSSam Leffler 	struct ieee80211req *ireq)
31868e8e04eSSam Leffler {
31968e8e04eSSam Leffler 	struct scanreq req;
32068e8e04eSSam Leffler 	int error;
32168e8e04eSSam Leffler 
322b032f27cSSam Leffler 	if (ireq->i_len < sizeof(struct scanreq))
32368e8e04eSSam Leffler 		return EFAULT;
32468e8e04eSSam Leffler 
32568e8e04eSSam Leffler 	error = 0;
32668e8e04eSSam Leffler 	req.space = 0;
327b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, get_scan_space, &req);
32868e8e04eSSam Leffler 	if (req.space > ireq->i_len)
32968e8e04eSSam Leffler 		req.space = ireq->i_len;
33068e8e04eSSam Leffler 	if (req.space > 0) {
331db9ff08bSKevin Lo 		uint32_t space;
33268e8e04eSSam Leffler 		void *p;
33368e8e04eSSam Leffler 
33468e8e04eSSam Leffler 		space = req.space;
33568e8e04eSSam Leffler 		/* XXX M_WAITOK after driver lock released */
336b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
337b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
33868e8e04eSSam Leffler 		if (p == NULL)
33968e8e04eSSam Leffler 			return ENOMEM;
34068e8e04eSSam Leffler 		req.sr = p;
341b032f27cSSam Leffler 		ieee80211_scan_iterate(vap, get_scan_result, &req);
342239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
343239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
344b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
345239cc3b6SSam Leffler 	} else
346239cc3b6SSam Leffler 		ireq->i_len = 0;
347239cc3b6SSam Leffler 
348239cc3b6SSam Leffler 	return error;
349239cc3b6SSam Leffler }
3508a1b9b6aSSam Leffler 
3512cab1d3dSSam Leffler struct stainforeq {
3522cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3532cab1d3dSSam Leffler 	size_t	space;
3542cab1d3dSSam Leffler };
3558a1b9b6aSSam Leffler 
3562cab1d3dSSam Leffler static size_t
3572cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
3582cab1d3dSSam Leffler {
359b032f27cSSam Leffler 	*ielen = ni->ni_ies.len;
3602cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
36168e8e04eSSam Leffler 		      sizeof(uint32_t));
3622cab1d3dSSam Leffler }
3632cab1d3dSSam Leffler 
3642cab1d3dSSam Leffler static void
3652cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
3662cab1d3dSSam Leffler {
3672cab1d3dSSam Leffler 	struct stainforeq *req = arg;
3682cab1d3dSSam Leffler 	size_t ielen;
3692cab1d3dSSam Leffler 
370b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
3712cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3722cab1d3dSSam Leffler 		return;
3732cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
3742cab1d3dSSam Leffler }
3752cab1d3dSSam Leffler 
37674bdb731SAndriy Voskoboinyk static void
3772cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
3782cab1d3dSSam Leffler {
3792cab1d3dSSam Leffler 	struct stainforeq *req = arg;
380b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3812cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
3822cab1d3dSSam Leffler 	size_t ielen, len;
38368e8e04eSSam Leffler 	uint8_t *cp;
3842cab1d3dSSam Leffler 
385b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
3862cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
3872cab1d3dSSam Leffler 		return;
3882cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
3892cab1d3dSSam Leffler 		return;
3902cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
3912cab1d3dSSam Leffler 	if (len > req->space)
3922cab1d3dSSam Leffler 		return;
3932cab1d3dSSam Leffler 	si = req->si;
3942cab1d3dSSam Leffler 	si->isi_len = len;
39568e8e04eSSam Leffler 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
3962cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
3978a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
3988a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
3998a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
4008a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
401b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
402b032f27cSSam Leffler 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
4038a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
4048a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
4058a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
4068a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
4078a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
4088a1b9b6aSSam Leffler 		si->isi_nrates = 15;
4098a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
4108a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
411b032f27cSSam Leffler 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
412b032f27cSSam Leffler 		const struct ieee80211_mcs_rates *mcs =
413b032f27cSSam Leffler 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
414b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
4156ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
416b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_800ns;
417b032f27cSSam Leffler 			else
418b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht40_rate_400ns;
419b032f27cSSam Leffler 		} else {
4206ac5ee4aSSam Leffler 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
421b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_800ns;
422b032f27cSSam Leffler 			else
423b032f27cSSam Leffler 				si->isi_txmbps = mcs->ht20_rate_400ns;
424b032f27cSSam Leffler 		}
425b032f27cSSam Leffler 	} else
426b032f27cSSam Leffler 		si->isi_txmbps = si->isi_txrate;
4278a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
4288a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
4298a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
4308a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
4318a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
4328a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
4338a1b9b6aSSam Leffler 	} else {
434801df4a5SSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
435801df4a5SSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
4368a1b9b6aSSam Leffler 	}
4372cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
4382cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
439b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_run;
440b032f27cSSam Leffler 	else if (ni->ni_associd != 0 ||
441b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_WDS &&
442b032f27cSSam Leffler 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
443b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_auth;
4448a1b9b6aSSam Leffler 	else
445b032f27cSSam Leffler 		si->isi_inact = vap->iv_inact_init;
4468a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
44759aa14a9SRui Paulo 	si->isi_localid = ni->ni_mllid;
44859aa14a9SRui Paulo 	si->isi_peerid = ni->ni_mlpid;
44959aa14a9SRui Paulo 	si->isi_peerstate = ni->ni_mlstate;
4508a1b9b6aSSam Leffler 
45168e8e04eSSam Leffler 	if (ielen) {
45268e8e04eSSam Leffler 		cp = ((uint8_t *)si) + si->isi_ie_off;
453b032f27cSSam Leffler 		memcpy(cp, ni->ni_ies.data, ielen);
4548a1b9b6aSSam Leffler 	}
4552cab1d3dSSam Leffler 
45668e8e04eSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
4572cab1d3dSSam Leffler 	req->space -= len;
4588a1b9b6aSSam Leffler }
4592cab1d3dSSam Leffler 
46074bdb731SAndriy Voskoboinyk static int
461b032f27cSSam Leffler getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
462db9ff08bSKevin Lo 	struct ieee80211_node *ni, size_t off)
4632cab1d3dSSam Leffler {
464b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4652cab1d3dSSam Leffler 	struct stainforeq req;
466d1c85daeSSam Leffler 	size_t space;
467d1c85daeSSam Leffler 	void *p;
4682cab1d3dSSam Leffler 	int error;
4692cab1d3dSSam Leffler 
4702cab1d3dSSam Leffler 	error = 0;
4712cab1d3dSSam Leffler 	req.space = 0;
4727db788c6SAndriy Voskoboinyk 	if (ni == NULL) {
4737db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space,
4747db788c6SAndriy Voskoboinyk 		    &req);
4757db788c6SAndriy Voskoboinyk 	} else
476d1c85daeSSam Leffler 		get_sta_space(&req, ni);
4772cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
4782cab1d3dSSam Leffler 		req.space = ireq->i_len;
4792cab1d3dSSam Leffler 	if (req.space > 0) {
4802cab1d3dSSam Leffler 		space = req.space;
4812cab1d3dSSam Leffler 		/* XXX M_WAITOK after driver lock released */
482b9b53389SAdrian Chadd 		p = IEEE80211_MALLOC(space, M_TEMP,
483b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
484d1c85daeSSam Leffler 		if (p == NULL) {
485d1c85daeSSam Leffler 			error = ENOMEM;
486d1c85daeSSam Leffler 			goto bad;
487d1c85daeSSam Leffler 		}
4882cab1d3dSSam Leffler 		req.si = p;
4897db788c6SAndriy Voskoboinyk 		if (ni == NULL) {
4907db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
4917db788c6SAndriy Voskoboinyk 			    get_sta_info, &req);
4927db788c6SAndriy Voskoboinyk 		} else
493d1c85daeSSam Leffler 			get_sta_info(&req, ni);
4942cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
49568e8e04eSSam Leffler 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
496b9b53389SAdrian Chadd 		IEEE80211_FREE(p, M_TEMP);
4972cab1d3dSSam Leffler 	} else
4982cab1d3dSSam Leffler 		ireq->i_len = 0;
499d1c85daeSSam Leffler bad:
500d1c85daeSSam Leffler 	if (ni != NULL)
501d1c85daeSSam Leffler 		ieee80211_free_node(ni);
5028a1b9b6aSSam Leffler 	return error;
5038a1b9b6aSSam Leffler }
5048a1b9b6aSSam Leffler 
50574bdb731SAndriy Voskoboinyk static int
506b032f27cSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
507d1c85daeSSam Leffler {
50868e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
509db9ff08bSKevin Lo 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
510d1c85daeSSam Leffler 	struct ieee80211_node *ni;
511d1c85daeSSam Leffler 	int error;
512d1c85daeSSam Leffler 
513d1c85daeSSam Leffler 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
514d1c85daeSSam Leffler 		return EFAULT;
515d1c85daeSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
516d1c85daeSSam Leffler 	if (error != 0)
517d1c85daeSSam Leffler 		return error;
518b032f27cSSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
519d1c85daeSSam Leffler 		ni = NULL;
520d1c85daeSSam Leffler 	} else {
521b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
52268e8e04eSSam Leffler 		if (ni == NULL)
523b032f27cSSam Leffler 			return ENOENT;
524d1c85daeSSam Leffler 	}
525b032f27cSSam Leffler 	return getstainfo_common(vap, ireq, ni, off);
526d1c85daeSSam Leffler }
527d1c85daeSSam Leffler 
52874bdb731SAndriy Voskoboinyk static int
529b032f27cSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
5308a1b9b6aSSam Leffler {
5318a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
5328a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
5338a1b9b6aSSam Leffler 	int error;
5348a1b9b6aSSam Leffler 
5358a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
5368a1b9b6aSSam Leffler 		return EINVAL;
5378a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
5388a1b9b6aSSam Leffler 	if (error != 0)
5398a1b9b6aSSam Leffler 		return error;
540b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
5418a1b9b6aSSam Leffler 	if (ni == NULL)
542b032f27cSSam Leffler 		return ENOENT;
5438a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
5448a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
5458a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
5468a1b9b6aSSam Leffler 	return error;
5478a1b9b6aSSam Leffler }
5488a1b9b6aSSam Leffler 
54974bdb731SAndriy Voskoboinyk static int
550b032f27cSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
5518a1b9b6aSSam Leffler {
552b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5538a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
5548a1b9b6aSSam Leffler 	struct wmeParams *wmep;
5558a1b9b6aSSam Leffler 	int ac;
5568a1b9b6aSSam Leffler 
5578a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
5588a1b9b6aSSam Leffler 		return EINVAL;
5598a1b9b6aSSam Leffler 
5608a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
5618a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
5628a1b9b6aSSam Leffler 		ac = WME_AC_BE;
5638a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
5648a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5658a1b9b6aSSam Leffler 	else
5668a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5678a1b9b6aSSam Leffler 	switch (ireq->i_type) {
5688a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
5698a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
5708a1b9b6aSSam Leffler 		break;
5718a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
5728a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
5738a1b9b6aSSam Leffler 		break;
5748a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
5758a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
5768a1b9b6aSSam Leffler 		break;
5778a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
5788a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
5798a1b9b6aSSam Leffler 		break;
5808a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
5818a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
5828a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
5838a1b9b6aSSam Leffler 		break;
5848a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
5858a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
5868a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
5878a1b9b6aSSam Leffler 		break;
5888a1b9b6aSSam Leffler 	}
5898a1b9b6aSSam Leffler 	return 0;
5908a1b9b6aSSam Leffler }
5918a1b9b6aSSam Leffler 
59274bdb731SAndriy Voskoboinyk static int
593b032f27cSSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
594188757f5SSam Leffler {
595b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
596188757f5SSam Leffler 
597b032f27cSSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
598188757f5SSam Leffler }
599188757f5SSam Leffler 
60074bdb731SAndriy Voskoboinyk static int
601b032f27cSSam Leffler ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
602b032f27cSSam Leffler {
603b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
604b032f27cSSam Leffler 	struct ieee80211_channel *c;
605b032f27cSSam Leffler 
606b032f27cSSam Leffler 	if (ireq->i_len != sizeof(struct ieee80211_channel))
607b032f27cSSam Leffler 		return EINVAL;
608b032f27cSSam Leffler 	/*
609b032f27cSSam Leffler 	 * vap's may have different operating channels when HT is
610b032f27cSSam Leffler 	 * in use.  When in RUN state report the vap-specific channel.
611b032f27cSSam Leffler 	 * Otherwise return curchan.
612b032f27cSSam Leffler 	 */
613daa96325SIan Lepore 	if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
614b032f27cSSam Leffler 		c = vap->iv_bss->ni_chan;
615b032f27cSSam Leffler 	else
616b032f27cSSam Leffler 		c = ic->ic_curchan;
617b032f27cSSam Leffler 	return copyout(c, ireq->i_data, sizeof(*c));
61868e8e04eSSam Leffler }
61968e8e04eSSam Leffler 
62068e8e04eSSam Leffler static int
621b032f27cSSam Leffler getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
62268e8e04eSSam Leffler {
623b032f27cSSam Leffler 	if (aie == NULL)
62468e8e04eSSam Leffler 		return EINVAL;
625b032f27cSSam Leffler 	/* NB: truncate, caller can check length */
626b032f27cSSam Leffler 	if (ireq->i_len > aie->ie_len)
627b032f27cSSam Leffler 		ireq->i_len = aie->ie_len;
628b032f27cSSam Leffler 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
629b032f27cSSam Leffler }
630b032f27cSSam Leffler 
631b032f27cSSam Leffler static int
632b032f27cSSam Leffler ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
633b032f27cSSam Leffler {
634b032f27cSSam Leffler 	uint8_t fc0;
635b032f27cSSam Leffler 
636b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
637b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
638b032f27cSSam Leffler 		return EINVAL;
639b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
640b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
641b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
642b032f27cSSam Leffler 		return getappie(vap->iv_appie_beacon, ireq);
643b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
644b032f27cSSam Leffler 		return getappie(vap->iv_appie_proberesp, ireq);
645b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
646b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocresp, ireq);
647b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
648b032f27cSSam Leffler 		return getappie(vap->iv_appie_probereq, ireq);
649b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
650b032f27cSSam Leffler 		return getappie(vap->iv_appie_assocreq, ireq);
651b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
652b032f27cSSam Leffler 		return getappie(vap->iv_appie_wpa, ireq);
653b032f27cSSam Leffler 	}
654b032f27cSSam Leffler 	return EINVAL;
655b032f27cSSam Leffler }
656b032f27cSSam Leffler 
65774bdb731SAndriy Voskoboinyk static int
658b032f27cSSam Leffler ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
659b032f27cSSam Leffler 	const struct ieee80211req *ireq)
660b032f27cSSam Leffler {
661b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
662b032f27cSSam Leffler 
663b032f27cSSam Leffler 	if (ireq->i_len != sizeof(ic->ic_regdomain))
664b032f27cSSam Leffler 		return EINVAL;
665b032f27cSSam Leffler 	return copyout(&ic->ic_regdomain, ireq->i_data,
666b032f27cSSam Leffler 	    sizeof(ic->ic_regdomain));
667b032f27cSSam Leffler }
668b032f27cSSam Leffler 
66974bdb731SAndriy Voskoboinyk static int
670b032f27cSSam Leffler ieee80211_ioctl_getroam(struct ieee80211vap *vap,
671b032f27cSSam Leffler 	const struct ieee80211req *ireq)
672b032f27cSSam Leffler {
673cbb24c24SSam Leffler 	size_t len = ireq->i_len;
674cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
675cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_roamparms))
676cbb24c24SSam Leffler 		len = sizeof(vap->iv_roamparms);
677cbb24c24SSam Leffler 	return copyout(vap->iv_roamparms, ireq->i_data, len);
678b032f27cSSam Leffler }
679b032f27cSSam Leffler 
68074bdb731SAndriy Voskoboinyk static int
681b032f27cSSam Leffler ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
682b032f27cSSam Leffler 	const struct ieee80211req *ireq)
683b032f27cSSam Leffler {
684cbb24c24SSam Leffler 	size_t len = ireq->i_len;
685cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
686cbb24c24SSam Leffler 	if (len > sizeof(vap->iv_txparms))
687cbb24c24SSam Leffler 		len = sizeof(vap->iv_txparms);
688cbb24c24SSam Leffler 	return copyout(vap->iv_txparms, ireq->i_data, len);
689b032f27cSSam Leffler }
690b032f27cSSam Leffler 
69174bdb731SAndriy Voskoboinyk static int
692b032f27cSSam Leffler ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
693b032f27cSSam Leffler 	const struct ieee80211req *ireq)
694b032f27cSSam Leffler {
695b032f27cSSam Leffler 	struct ieee80211_devcaps_req *dc;
696b032f27cSSam Leffler 	struct ieee80211req_chaninfo *ci;
6978658b18bSSam Leffler 	int maxchans, error;
698b032f27cSSam Leffler 
6998658b18bSSam Leffler 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
7008658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
7018658b18bSSam Leffler 	/* NB: require 1 so we know ic_nchans is accessible */
7028658b18bSSam Leffler 	if (maxchans < 1)
703b032f27cSSam Leffler 		return EINVAL;
7048658b18bSSam Leffler 	/* constrain max request size, 2K channels is ~24Kbytes */
7058658b18bSSam Leffler 	if (maxchans > 2048)
7068658b18bSSam Leffler 		maxchans = 2048;
7078658b18bSSam Leffler 	dc = (struct ieee80211_devcaps_req *)
708b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP,
709b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
710b032f27cSSam Leffler 	if (dc == NULL)
711b032f27cSSam Leffler 		return ENOMEM;
712b032f27cSSam Leffler 	dc->dc_drivercaps = ic->ic_caps;
713b032f27cSSam Leffler 	dc->dc_cryptocaps = ic->ic_cryptocaps;
714b032f27cSSam Leffler 	dc->dc_htcaps = ic->ic_htcaps;
71535bcfd1cSAdrian Chadd 	dc->dc_vhtcaps = ic->ic_vhtcaps;
716b032f27cSSam Leffler 	ci = &dc->dc_chaninfo;
7178658b18bSSam Leffler 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
718f8b1ea17SSam Leffler 	KASSERT(ci->ic_nchans <= maxchans,
719f8b1ea17SSam Leffler 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
720b032f27cSSam Leffler 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
7218658b18bSSam Leffler 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
722b9b53389SAdrian Chadd 	IEEE80211_FREE(dc, M_TEMP);
723b032f27cSSam Leffler 	return error;
724b032f27cSSam Leffler }
725b032f27cSSam Leffler 
72674bdb731SAndriy Voskoboinyk static int
727b032f27cSSam Leffler ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
728b032f27cSSam Leffler {
729b032f27cSSam Leffler 	struct ieee80211_node *ni;
730b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
731b032f27cSSam Leffler 	int error;
732b032f27cSSam Leffler 
733b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
734b032f27cSSam Leffler 		return EINVAL;
735b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
736b032f27cSSam Leffler 	if (error != 0)
737b032f27cSSam Leffler 		return error;
738b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
739b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
740b032f27cSSam Leffler 		    vlan.sv_macaddr);
741b032f27cSSam Leffler 		if (ni == NULL)
742b032f27cSSam Leffler 			return ENOENT;
743b032f27cSSam Leffler 	} else
744b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
745b032f27cSSam Leffler 	vlan.sv_vlan = ni->ni_vlan;
746b032f27cSSam Leffler 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
747b032f27cSSam Leffler 	ieee80211_free_node(ni);
748b032f27cSSam Leffler 	return error;
74968e8e04eSSam Leffler }
75068e8e04eSSam Leffler 
75168e8e04eSSam Leffler /*
7528c4e758aSSam Leffler  * Dummy ioctl get handler so the linker set is defined.
7538c4e758aSSam Leffler  */
7548c4e758aSSam Leffler static int
7558c4e758aSSam Leffler dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
7568c4e758aSSam Leffler {
7578c4e758aSSam Leffler 	return ENOSYS;
7588c4e758aSSam Leffler }
7598c4e758aSSam Leffler IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
7608c4e758aSSam Leffler 
7618c4e758aSSam Leffler static int
7628c4e758aSSam Leffler ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
7638c4e758aSSam Leffler {
7648c4e758aSSam Leffler 	ieee80211_ioctl_getfunc * const *get;
7658c4e758aSSam Leffler 	int error;
7668c4e758aSSam Leffler 
7678c4e758aSSam Leffler 	SET_FOREACH(get, ieee80211_ioctl_getset) {
7688c4e758aSSam Leffler 		error = (*get)(vap, ireq);
7698c4e758aSSam Leffler 		if (error != ENOSYS)
7708c4e758aSSam Leffler 			return error;
7718c4e758aSSam Leffler 	}
7728c4e758aSSam Leffler 	return EINVAL;
7738c4e758aSSam Leffler }
7748c4e758aSSam Leffler 
77574bdb731SAndriy Voskoboinyk static int
776b032f27cSSam Leffler ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
777b032f27cSSam Leffler     struct ieee80211req *ireq)
7788a1b9b6aSSam Leffler {
779b032f27cSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
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) {
7871a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
788b032f27cSSam Leffler 		switch (vap->iv_state) {
7891a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
7901a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
791b032f27cSSam Leffler 			ireq->i_len = vap->iv_des_ssid[0].len;
792b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
7931a1e1d21SSam Leffler 			break;
7941a1e1d21SSam Leffler 		default:
795b032f27cSSam Leffler 			ireq->i_len = vap->iv_bss->ni_esslen;
796b032f27cSSam Leffler 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
7971a1e1d21SSam Leffler 			break;
7981a1e1d21SSam Leffler 		}
7991a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
8001a1e1d21SSam Leffler 		break;
8011a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
8021a1e1d21SSam Leffler 		ireq->i_val = 1;
8031a1e1d21SSam Leffler 		break;
8041a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
805b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
8068a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
807b032f27cSSam Leffler 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
8088a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
8098a1b9b6aSSam Leffler 		else
8108a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
8111a1e1d21SSam Leffler 		break;
8121a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
8131a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
8148a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8158a1b9b6aSSam Leffler 			return EINVAL;
816b032f27cSSam Leffler 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
8171a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
818acd3428bSRobert Watson 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
819b032f27cSSam Leffler 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
8201a1e1d21SSam Leffler 		} else {
8211a1e1d21SSam Leffler 			bzero(tmpkey, len);
8221a1e1d21SSam Leffler 		}
8231a1e1d21SSam Leffler 		ireq->i_len = len;
8241a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
8251a1e1d21SSam Leffler 		break;
8261a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
8271a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
8281a1e1d21SSam Leffler 		break;
8291a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
830b032f27cSSam Leffler 		ireq->i_val = vap->iv_def_txkey;
8311a1e1d21SSam Leffler 		break;
8321a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
833b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA)
8348a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
8358a1b9b6aSSam Leffler 		else
836b032f27cSSam Leffler 			ireq->i_val = vap->iv_bss->ni_authmode;
8371a1e1d21SSam Leffler 		break;
8381a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
839b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
8401a1e1d21SSam Leffler 		break;
8411a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
842b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PMGTON)
8431a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
8441a1e1d21SSam Leffler 		else
8451a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
8461a1e1d21SSam Leffler 		break;
8471a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
8481a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
8491a1e1d21SSam Leffler 		break;
85013604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
851b032f27cSSam Leffler 		ireq->i_val = vap->iv_rtsthreshold;
8521a1e1d21SSam Leffler 		break;
8532e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
8542e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
8552e79ca97SSam Leffler 		break;
8562e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
857b032f27cSSam Leffler 		/*
858b032f27cSSam Leffler 		 * Tx power limit is the min of max regulatory
859b032f27cSSam Leffler 		 * power, any user-set limit, and the max the
860b032f27cSSam Leffler 		 * radio can do.
861781487cfSAdrian Chadd 		 *
862781487cfSAdrian Chadd 		 * TODO: methodize this
863b032f27cSSam Leffler 		 */
864b032f27cSSam Leffler 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
865b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_txpowlimit)
8668a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_txpowlimit;
867b032f27cSSam Leffler 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
868b032f27cSSam Leffler 			ireq->i_val = ic->ic_curchan->ic_maxpower;
8698a1b9b6aSSam Leffler 		break;
8708a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
871b032f27cSSam Leffler 		switch (vap->iv_flags & IEEE80211_F_WPA) {
8728a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
8738a1b9b6aSSam Leffler 			ireq->i_val = 1;
8748a1b9b6aSSam Leffler 			break;
8758a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
8768a1b9b6aSSam Leffler 			ireq->i_val = 2;
8778a1b9b6aSSam Leffler 			break;
8788a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
8798a1b9b6aSSam Leffler 			ireq->i_val = 3;
8808a1b9b6aSSam Leffler 			break;
8818a1b9b6aSSam Leffler 		default:
8828a1b9b6aSSam Leffler 			ireq->i_val = 0;
8838a1b9b6aSSam Leffler 			break;
8848a1b9b6aSSam Leffler 		}
8858a1b9b6aSSam Leffler 		break;
8868a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
887b032f27cSSam Leffler 		error = ieee80211_ioctl_getchanlist(vap, ireq);
8888a1b9b6aSSam Leffler 		break;
8898a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
890b032f27cSSam Leffler 		ireq->i_val = vap->iv_roaming;
8918a1b9b6aSSam Leffler 		break;
8928a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
893b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
8948a1b9b6aSSam Leffler 		break;
8958a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
896b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
8978a1b9b6aSSam Leffler 		break;
8988a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
899b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
9008a1b9b6aSSam Leffler 		break;
9018a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
902b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
9038a1b9b6aSSam Leffler 		break;
9048a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
905b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
9068a1b9b6aSSam Leffler 		break;
9078a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
908b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
9098a1b9b6aSSam Leffler 		break;
9108a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
911b032f27cSSam Leffler 		error = ieee80211_ioctl_getkey(vap, ireq);
9128a1b9b6aSSam Leffler 		break;
9138a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
914b032f27cSSam Leffler 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
9158a1b9b6aSSam Leffler 		break;
9168a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
9178a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
9188a1b9b6aSSam Leffler 			return EINVAL;
9191b74d2a4SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
9207cd89f64SSam Leffler 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
9217cd89f64SSam Leffler 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
9228a1b9b6aSSam Leffler 			    ireq->i_data, ireq->i_len);
9237cd89f64SSam Leffler 		} else
9247cd89f64SSam Leffler 			error = copyout(vap->iv_des_bssid, ireq->i_data,
9257cd89f64SSam Leffler 			    ireq->i_len);
9268a1b9b6aSSam Leffler 		break;
9278a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
92868e8e04eSSam Leffler 	case IEEE80211_IOC_WPAIE2:
929b032f27cSSam Leffler 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
9308a1b9b6aSSam Leffler 		break;
9318a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
932b032f27cSSam Leffler 		error = ieee80211_ioctl_getscanresults(vap, ireq);
9338a1b9b6aSSam Leffler 		break;
9348a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
935b032f27cSSam Leffler 		error = ieee80211_ioctl_getstastats(vap, ireq);
9368a1b9b6aSSam Leffler 		break;
9378a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
938b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_txpower;
9398a1b9b6aSSam Leffler 		break;
9408a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
941b032f27cSSam Leffler 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
9428a1b9b6aSSam Leffler 		break;
9438a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
944b032f27cSSam Leffler 		error = ieee80211_ioctl_getstainfo(vap, ireq);
9458a1b9b6aSSam Leffler 		break;
9468a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
9478a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
9488a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
9498a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
9508a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
951fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
952b032f27cSSam Leffler 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
9538a1b9b6aSSam Leffler 		break;
9548a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
955b032f27cSSam Leffler 		ireq->i_val = vap->iv_dtim_period;
9568a1b9b6aSSam Leffler 		break;
9578a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
9588a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
959b032f27cSSam Leffler 		ireq->i_val = vap->iv_bss->ni_intval;
9602e79ca97SSam Leffler 		break;
961c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
962b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
963c4f040c3SSam Leffler 		break;
96432b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
96532b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet;
96632b0e64bSAdrian Chadd 		break;
96732b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
96832b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_count;
96932b0e64bSAdrian Chadd 		break;
97032b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
97132b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_period;
97232b0e64bSAdrian Chadd 		break;
97332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
97432b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_duration;
97532b0e64bSAdrian Chadd 		break;
97632b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
97732b0e64bSAdrian Chadd 		ireq->i_val = vap->iv_quiet_offset;
97832b0e64bSAdrian Chadd 		break;
97968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
980b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
98168e8e04eSSam Leffler 		break;
98268e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
983b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
98468e8e04eSSam Leffler 		break;
98568e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
986b032f27cSSam Leffler 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
98768e8e04eSSam Leffler 		break;
98868e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
989b032f27cSSam Leffler 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
99064353cb0SSam Leffler 		break;
99170231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
992b032f27cSSam Leffler 		ireq->i_val = vap->iv_fragthreshold;
99370231e3dSSam Leffler 		break;
994188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
995b032f27cSSam Leffler 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
996188757f5SSam Leffler 		break;
997c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
998b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
999c27e4e31SSam Leffler 		break;
1000546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
1001b032f27cSSam Leffler 		ireq->i_val = vap->iv_bmissthreshold;
1002546786c9SSam Leffler 		break;
100368e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
1004b032f27cSSam Leffler 		error = ieee80211_ioctl_getcurchan(vap, ireq);
100568e8e04eSSam Leffler 		break;
100668e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
100768e8e04eSSam Leffler 		ireq->i_val = 0;
10082bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
100968e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
10102bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
101168e8e04eSSam Leffler 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
101268e8e04eSSam Leffler 		break;
101368e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
101468e8e04eSSam Leffler 		ireq->i_val = 0;
10152bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
101668e8e04eSSam Leffler 			ireq->i_val |= 1;
10172bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
101868e8e04eSSam Leffler 			ireq->i_val |= 2;
101968e8e04eSSam Leffler 		break;
102068e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
1021781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1022b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1023b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_rxmax;
10241b74d2a4SAdrian Chadd 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
1025cac03190SAdrian Chadd 			/*
1026cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1027cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1028cac03190SAdrian Chadd 			 */
1029b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1030b032f27cSSam Leffler 			    IEEE80211_HTCAP_MAXRXAMPDU);
1031b032f27cSSam Leffler 		else
1032b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_limit;
103368e8e04eSSam Leffler 		break;
103468e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
1035781487cfSAdrian Chadd 		/* XXX TODO: make this a per-node thing; and leave this as global */
1036900de995SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
10371b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1038cac03190SAdrian Chadd 			/*
1039cac03190SAdrian Chadd 			 * XXX TODO: this isn't completely correct, as we've
1040cac03190SAdrian Chadd 			 * negotiated the higher of the two.
1041cac03190SAdrian Chadd 			 */
1042b032f27cSSam Leffler 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1043b032f27cSSam Leffler 			    IEEE80211_HTCAP_MPDUDENSITY);
1044b032f27cSSam Leffler 		else
1045b032f27cSSam Leffler 			ireq->i_val = vap->iv_ampdu_density;
104668e8e04eSSam Leffler 		break;
104768e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
104868e8e04eSSam Leffler 		ireq->i_val = 0;
10492bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
105068e8e04eSSam Leffler 			ireq->i_val |= 1;
10512bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
105268e8e04eSSam Leffler 			ireq->i_val |= 2;
105368e8e04eSSam Leffler 		break;
105468e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
1055b032f27cSSam Leffler 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
105668e8e04eSSam Leffler 		break;
105768e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
10582bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
105968e8e04eSSam Leffler 		break;
106068e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
1061b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1062b032f27cSSam Leffler 		break;
1063b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
1064b032f27cSSam Leffler 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1065b032f27cSSam Leffler 		break;
1066b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
1067b032f27cSSam Leffler 		error = ieee80211_ioctl_getroam(vap, ireq);
1068b032f27cSSam Leffler 		break;
1069b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
1070b032f27cSSam Leffler 		error = ieee80211_ioctl_gettxparams(vap, ireq);
107168e8e04eSSam Leffler 		break;
107268e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
10732bfc8a91SSam Leffler 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1074b032f27cSSam Leffler 		break;
1075b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
1076b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
107768e8e04eSSam Leffler 		break;
1078c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
1079b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1080b032f27cSSam Leffler 		break;
1081b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
1082b032f27cSSam Leffler 		error = ieee80211_ioctl_getappie(vap, ireq);
1083b032f27cSSam Leffler 		break;
1084b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
1085b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1086b032f27cSSam Leffler 		break;
1087b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
1088b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1089b032f27cSSam Leffler 		break;
1090b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
1091b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1092b032f27cSSam Leffler 		break;
1093b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
1094b032f27cSSam Leffler 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1095b032f27cSSam Leffler 		break;
1096b032f27cSSam Leffler 	case IEEE80211_IOC_DEVCAPS:
1097b032f27cSSam Leffler 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1098c066143cSSam Leffler 		break;
10991b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
11001b6167d2SSam Leffler 		ireq->i_val = ic->ic_htprotmode;
11011b6167d2SSam Leffler 		break;
11021b6167d2SSam Leffler 	case IEEE80211_IOC_HTCONF:
11032bfc8a91SSam Leffler 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
11041b6167d2SSam Leffler 			ireq->i_val = 1;
11052bfc8a91SSam Leffler 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
11061b6167d2SSam Leffler 				ireq->i_val |= 2;
11071b6167d2SSam Leffler 		} else
11081b6167d2SSam Leffler 			ireq->i_val = 0;
11091b6167d2SSam Leffler 		break;
1110b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
1111b032f27cSSam Leffler 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1112b032f27cSSam Leffler 		break;
11138c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
11148c070d69SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11151b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
11168c070d69SSam Leffler 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
11178c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
11188c070d69SSam Leffler 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
11198c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
11208c070d69SSam Leffler 			else
11218c070d69SSam Leffler 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
11228c070d69SSam Leffler 		} else
11238c070d69SSam Leffler 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
11248c070d69SSam Leffler 		break;
112544f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
112644f7a6edSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
11271b74d2a4SAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
112844f7a6edSSam Leffler 			ireq->i_val =
112944f7a6edSSam Leffler 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
113044f7a6edSSam Leffler 		else
113144f7a6edSSam Leffler 			ireq->i_val =
11322bfc8a91SSam Leffler 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
113344f7a6edSSam Leffler 		break;
1134ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
1135ec3763bbSAdrian Chadd 		ireq->i_val = 0;
1136ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
1137ec3763bbSAdrian Chadd 			ireq->i_val |= 1;
1138ec3763bbSAdrian Chadd 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
1139ec3763bbSAdrian Chadd 			ireq->i_val |= 2;
1140ec3763bbSAdrian Chadd 		break;
1141c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
1142c5bba9daSAndriy Voskoboinyk 		ireq->i_val = 0;
1143c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)
1144c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 1;
1145c5bba9daSAndriy Voskoboinyk 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX)
1146c5bba9daSAndriy Voskoboinyk 			ireq->i_val |= 2;
1147c5bba9daSAndriy Voskoboinyk 		break;
114835bcfd1cSAdrian Chadd 
114935bcfd1cSAdrian Chadd 	/* VHT */
115035bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
115135bcfd1cSAdrian Chadd 		ireq->i_val = 0;
115235bcfd1cSAdrian Chadd 		if (vap->iv_flags_vht & IEEE80211_FVHT_VHT)
115335bcfd1cSAdrian Chadd 			ireq->i_val |= 1;
115435bcfd1cSAdrian Chadd 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)
115535bcfd1cSAdrian Chadd 			ireq->i_val |= 2;
115635bcfd1cSAdrian Chadd 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)
115735bcfd1cSAdrian Chadd 			ireq->i_val |= 4;
115835bcfd1cSAdrian Chadd 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)
115935bcfd1cSAdrian Chadd 			ireq->i_val |= 8;
116035bcfd1cSAdrian Chadd 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)
116135bcfd1cSAdrian Chadd 			ireq->i_val |= 16;
116235bcfd1cSAdrian Chadd 		break;
116335bcfd1cSAdrian Chadd 
11641a1e1d21SSam Leffler 	default:
11658c4e758aSSam Leffler 		error = ieee80211_ioctl_getdefault(vap, ireq);
1166b2e95691SSam Leffler 		break;
11671a1e1d21SSam Leffler 	}
11688a1b9b6aSSam Leffler 	return error;
1169b032f27cSSam Leffler #undef MS
11708a1b9b6aSSam Leffler }
11718a1b9b6aSSam Leffler 
117274bdb731SAndriy Voskoboinyk static int
1173b032f27cSSam Leffler ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
11748a1b9b6aSSam Leffler {
11758a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
11768a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11778a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
117868e8e04eSSam Leffler 	uint16_t kid;
1179b032f27cSSam Leffler 	int error, i;
11808a1b9b6aSSam Leffler 
11818a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
11828a1b9b6aSSam Leffler 		return EINVAL;
11838a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
11841a1e1d21SSam Leffler 	if (error)
11858a1b9b6aSSam Leffler 		return error;
11868a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
11878a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
11888a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
11898a1b9b6aSSam Leffler 		return E2BIG;
11908a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
11918a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
11928a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
11938a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
11948a1b9b6aSSam Leffler 			return EINVAL;
1195b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1196b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1197b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1198b5d4660fSSam Leffler 				ieee80211_free_node(ni);
11998a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1200b5d4660fSSam Leffler 			}
12018a1b9b6aSSam Leffler 		} else {
1202b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1203b032f27cSSam Leffler 				ik.ik_macaddr);
12048a1b9b6aSSam Leffler 			if (ni == NULL)
12058a1b9b6aSSam Leffler 				return ENOENT;
12068a1b9b6aSSam Leffler 		}
12078a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
12088a1b9b6aSSam Leffler 	} else {
12098a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12108a1b9b6aSSam Leffler 			return EINVAL;
1211b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[kid];
1212386d84f6SSam Leffler 		/*
1213386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1214386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1215386d84f6SSam Leffler 		 */
1216386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1217386d84f6SSam Leffler 			wk->wk_keyix = kid;
12188a1b9b6aSSam Leffler 		ni = NULL;
12198a1b9b6aSSam Leffler 	}
12208a1b9b6aSSam Leffler 	error = 0;
1221b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
1222b032f27cSSam Leffler 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
12238a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
12248a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
12258a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
12268a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1227b032f27cSSam Leffler 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1228b032f27cSSam Leffler 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
12298a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
12308a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
12318a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
123271fe06caSSam Leffler 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
123371fe06caSSam Leffler 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
123471fe06caSSam Leffler 		if (!ieee80211_crypto_setkey(vap, wk))
12358a1b9b6aSSam Leffler 			error = EIO;
12368a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1237781487cfSAdrian Chadd 			/*
1238781487cfSAdrian Chadd 			 * Inform the driver that this is the default
1239781487cfSAdrian Chadd 			 * transmit key.  Now, ideally we'd just set
1240781487cfSAdrian Chadd 			 * a flag in the key update that would
1241781487cfSAdrian Chadd 			 * say "yes, we're the default key", but
1242781487cfSAdrian Chadd 			 * that currently isn't the way the ioctl ->
1243781487cfSAdrian Chadd 			 * key interface works.
1244781487cfSAdrian Chadd 			 */
1245781487cfSAdrian Chadd 			ieee80211_crypto_set_deftxkey(vap, kid);
12468a1b9b6aSSam Leffler 	} else
12478a1b9b6aSSam Leffler 		error = ENXIO;
1248b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
12498a1b9b6aSSam Leffler 	if (ni != NULL)
12508a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12518a1b9b6aSSam Leffler 	return error;
12528a1b9b6aSSam Leffler }
12538a1b9b6aSSam Leffler 
125474bdb731SAndriy Voskoboinyk static int
1255b032f27cSSam Leffler ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
12568a1b9b6aSSam Leffler {
12578a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
12588a1b9b6aSSam Leffler 	int kid, error;
12598a1b9b6aSSam Leffler 
12608a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
12618a1b9b6aSSam Leffler 		return EINVAL;
12628a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
12638a1b9b6aSSam Leffler 	if (error)
12648a1b9b6aSSam Leffler 		return error;
12658a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
126668e8e04eSSam Leffler 	/* XXX uint8_t -> uint16_t */
126768e8e04eSSam Leffler 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
12688a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
12698a1b9b6aSSam Leffler 
1270b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1271b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1272b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1273b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1274b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1275b5d4660fSSam Leffler 			}
1276b5d4660fSSam Leffler 		} else {
1277b032f27cSSam Leffler 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1278b032f27cSSam Leffler 				dk.idk_macaddr);
12798a1b9b6aSSam Leffler 			if (ni == NULL)
1280b5d4660fSSam Leffler 				return ENOENT;
1281b5d4660fSSam Leffler 		}
12828a1b9b6aSSam Leffler 		/* XXX error return */
1283c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
12848a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12858a1b9b6aSSam Leffler 	} else {
12868a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
12878a1b9b6aSSam Leffler 			return EINVAL;
12888a1b9b6aSSam Leffler 		/* XXX error return */
1289b032f27cSSam Leffler 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
12908a1b9b6aSSam Leffler 	}
12918a1b9b6aSSam Leffler 	return 0;
12928a1b9b6aSSam Leffler }
12938a1b9b6aSSam Leffler 
1294b032f27cSSam Leffler struct mlmeop {
1295b032f27cSSam Leffler 	struct ieee80211vap *vap;
1296b032f27cSSam Leffler 	int	op;
1297b032f27cSSam Leffler 	int	reason;
1298b032f27cSSam Leffler };
1299b032f27cSSam Leffler 
1300b032f27cSSam Leffler static void
1301b032f27cSSam Leffler mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1302b032f27cSSam Leffler 	int op, int reason)
1303b032f27cSSam Leffler {
1304b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1305b032f27cSSam Leffler 	static const struct {
1306b032f27cSSam Leffler 		int mask;
1307b032f27cSSam Leffler 		const char *opstr;
1308b032f27cSSam Leffler 	} ops[] = {
1309b032f27cSSam Leffler 		{ 0, "op#0" },
1310b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1311b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "assoc" },
1312b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1313b032f27cSSam Leffler 		  IEEE80211_MSG_ASSOC, "disassoc" },
1314b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1315b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "deauth" },
1316b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1317b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "authorize" },
1318b032f27cSSam Leffler 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1319b032f27cSSam Leffler 		  IEEE80211_MSG_AUTH, "unauthorize" },
1320b032f27cSSam Leffler 	};
1321b032f27cSSam Leffler 
1322b032f27cSSam Leffler 	if (op == IEEE80211_MLME_AUTH) {
1323b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1324b032f27cSSam Leffler 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1325d72d72d3SAndriy Voskoboinyk 		    "station authenticate %s via MLME (reason: %d (%s))",
1326b032f27cSSam Leffler 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1327d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1328b032f27cSSam Leffler 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1329b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1330d72d72d3SAndriy Voskoboinyk 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
1331d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(reason));
1332b032f27cSSam Leffler 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1333b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1334b032f27cSSam Leffler 		    "station %s via MLME", ops[op].opstr);
1335b032f27cSSam Leffler 	} else {
1336b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1337d72d72d3SAndriy Voskoboinyk 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
1338d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
1339b032f27cSSam Leffler 	}
1340b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
1341b032f27cSSam Leffler }
1342b032f27cSSam Leffler 
13438a1b9b6aSSam Leffler static void
13448a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
13458a1b9b6aSSam Leffler {
1346b032f27cSSam Leffler 	struct mlmeop *mop = arg;
1347b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
13488a1b9b6aSSam Leffler 
1349b032f27cSSam Leffler 	if (vap != mop->vap)
1350b032f27cSSam Leffler 		return;
1351b032f27cSSam Leffler 	/*
1352b032f27cSSam Leffler 	 * NB: if ni_associd is zero then the node is already cleaned
1353b032f27cSSam Leffler 	 * up and we don't need to do this (we're safely holding a
1354b032f27cSSam Leffler 	 * reference but should otherwise not modify it's state).
1355b032f27cSSam Leffler 	 */
1356b032f27cSSam Leffler 	if (ni->ni_associd == 0)
1357b032f27cSSam Leffler 		return;
1358b032f27cSSam Leffler 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1359b032f27cSSam Leffler 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1360b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1361b032f27cSSam Leffler 		    mop->reason);
1362b032f27cSSam Leffler 	} else {
1363b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1364b032f27cSSam Leffler 		    mop->reason);
13658a1b9b6aSSam Leffler 	}
1366b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1367b032f27cSSam Leffler }
1368b032f27cSSam Leffler 
1369b032f27cSSam Leffler static int
1370b032f27cSSam Leffler setmlme_dropsta(struct ieee80211vap *vap,
1371b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1372b032f27cSSam Leffler {
13737a79cebfSGleb Smirnoff 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1374b032f27cSSam Leffler 	struct ieee80211_node *ni;
1375b032f27cSSam Leffler 	int error = 0;
1376b032f27cSSam Leffler 
1377b032f27cSSam Leffler 	/* NB: the broadcast address means do 'em all */
13787a79cebfSGleb Smirnoff 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
1379b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1380b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, mac);
13819984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
13829984c9baSAdrian Chadd 		/*
13839984c9baSAdrian Chadd 		 * Don't do the node update inside the node
13849984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
13859984c9baSAdrian Chadd 		 * with drivers and their TX paths.
13869984c9baSAdrian Chadd 		 */
1387b032f27cSSam Leffler 		if (ni != NULL) {
1388b032f27cSSam Leffler 			domlme(mlmeop, ni);
1389b032f27cSSam Leffler 			ieee80211_free_node(ni);
1390b032f27cSSam Leffler 		} else
1391b032f27cSSam Leffler 			error = ENOENT;
1392b032f27cSSam Leffler 	} else {
1393b032f27cSSam Leffler 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1394b032f27cSSam Leffler 	}
1395b032f27cSSam Leffler 	return error;
1396b032f27cSSam Leffler }
1397b032f27cSSam Leffler 
139874bdb731SAndriy Voskoboinyk static int
1399b032f27cSSam Leffler setmlme_common(struct ieee80211vap *vap, int op,
1400b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1401b032f27cSSam Leffler {
1402b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1403b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1404b032f27cSSam Leffler 	struct ieee80211_node *ni;
1405b032f27cSSam Leffler 	struct mlmeop mlmeop;
1406b032f27cSSam Leffler 	int error;
1407b032f27cSSam Leffler 
1408b032f27cSSam Leffler 	error = 0;
1409b032f27cSSam Leffler 	switch (op) {
1410b032f27cSSam Leffler 	case IEEE80211_MLME_DISASSOC:
1411b032f27cSSam Leffler 	case IEEE80211_MLME_DEAUTH:
1412b032f27cSSam Leffler 		switch (vap->iv_opmode) {
1413b032f27cSSam Leffler 		case IEEE80211_M_STA:
1414b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1415b032f27cSSam Leffler 			/* XXX not quite right */
1416b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1417b032f27cSSam Leffler 			break;
1418b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
1419b032f27cSSam Leffler 			mlmeop.vap = vap;
1420b032f27cSSam Leffler 			mlmeop.op = op;
1421b032f27cSSam Leffler 			mlmeop.reason = reason;
1422b032f27cSSam Leffler 			error = setmlme_dropsta(vap, mac, &mlmeop);
1423b032f27cSSam Leffler 			break;
1424b032f27cSSam Leffler 		case IEEE80211_M_WDS:
1425b032f27cSSam Leffler 			/* XXX user app should send raw frame? */
1426b032f27cSSam Leffler 			if (op != IEEE80211_MLME_DEAUTH) {
1427b032f27cSSam Leffler 				error = EINVAL;
1428b032f27cSSam Leffler 				break;
1429b032f27cSSam Leffler 			}
1430b032f27cSSam Leffler #if 0
1431b032f27cSSam Leffler 			/* XXX accept any address, simplifies user code */
1432b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1433b032f27cSSam Leffler 				error = EINVAL;
1434b032f27cSSam Leffler 				break;
1435b032f27cSSam Leffler 			}
1436b032f27cSSam Leffler #endif
1437b032f27cSSam Leffler 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1438b032f27cSSam Leffler 			ni = ieee80211_ref_node(vap->iv_bss);
1439b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
1440b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1441b032f27cSSam Leffler 			ieee80211_free_node(ni);
1442b032f27cSSam Leffler 			break;
1443136cb222SAdrian Chadd 		case IEEE80211_M_MBSS:
1444136cb222SAdrian Chadd 			IEEE80211_NODE_LOCK(nt);
1445136cb222SAdrian Chadd 			ni = ieee80211_find_node_locked(nt, mac);
14469984c9baSAdrian Chadd 			/*
14479984c9baSAdrian Chadd 			 * Don't do the node update inside the node
14489984c9baSAdrian Chadd 			 * table lock.  This unfortunately causes LORs
14499984c9baSAdrian Chadd 			 * with drivers and their TX paths.
14509984c9baSAdrian Chadd 			 */
14519984c9baSAdrian Chadd 			IEEE80211_NODE_UNLOCK(nt);
1452136cb222SAdrian Chadd 			if (ni != NULL) {
1453136cb222SAdrian Chadd 				ieee80211_node_leave(ni);
1454136cb222SAdrian Chadd 				ieee80211_free_node(ni);
1455136cb222SAdrian Chadd 			} else {
1456136cb222SAdrian Chadd 				error = ENOENT;
1457136cb222SAdrian Chadd 			}
1458136cb222SAdrian Chadd 			break;
1459b032f27cSSam Leffler 		default:
1460b032f27cSSam Leffler 			error = EINVAL;
1461b032f27cSSam Leffler 			break;
1462b032f27cSSam Leffler 		}
1463b032f27cSSam Leffler 		break;
1464b032f27cSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
1465b032f27cSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
1466b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1467b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_WDS) {
1468b032f27cSSam Leffler 			error = EINVAL;
1469b032f27cSSam Leffler 			break;
1470b032f27cSSam Leffler 		}
1471b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1472b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14739984c9baSAdrian Chadd 		/*
14749984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14759984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14769984c9baSAdrian Chadd 		 * with drivers and their TX paths.
14779984c9baSAdrian Chadd 		 */
14789984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1479b032f27cSSam Leffler 		if (ni != NULL) {
1480b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1481b032f27cSSam Leffler 			if (op == IEEE80211_MLME_AUTHORIZE)
1482b032f27cSSam Leffler 				ieee80211_node_authorize(ni);
1483b032f27cSSam Leffler 			else
1484b032f27cSSam Leffler 				ieee80211_node_unauthorize(ni);
1485b032f27cSSam Leffler 			ieee80211_free_node(ni);
1486b032f27cSSam Leffler 		} else
1487b032f27cSSam Leffler 			error = ENOENT;
1488b032f27cSSam Leffler 		break;
1489b032f27cSSam Leffler 	case IEEE80211_MLME_AUTH:
1490b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1491b032f27cSSam Leffler 			error = EINVAL;
1492b032f27cSSam Leffler 			break;
1493b032f27cSSam Leffler 		}
1494b032f27cSSam Leffler 		IEEE80211_NODE_LOCK(nt);
1495b032f27cSSam Leffler 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
14969984c9baSAdrian Chadd 		/*
14979984c9baSAdrian Chadd 		 * Don't do the node update inside the node
14989984c9baSAdrian Chadd 		 * table lock.  This unfortunately causes LORs
14999984c9baSAdrian Chadd 		 * with drivers and their TX paths.
15009984c9baSAdrian Chadd 		 */
15019984c9baSAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
1502b032f27cSSam Leffler 		if (ni != NULL) {
1503b032f27cSSam Leffler 			mlmedebug(vap, mac, op, reason);
1504b032f27cSSam Leffler 			if (reason == IEEE80211_STATUS_SUCCESS) {
1505b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
1506b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1507b032f27cSSam Leffler 				/*
1508b032f27cSSam Leffler 				 * For shared key auth, just continue the
1509b032f27cSSam Leffler 				 * exchange.  Otherwise when 802.1x is not in
1510b032f27cSSam Leffler 				 * use mark the port authorized at this point
1511b032f27cSSam Leffler 				 * so traffic can flow.
1512b032f27cSSam Leffler 				 */
1513b032f27cSSam Leffler 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1514b032f27cSSam Leffler 				    ni->ni_challenge == NULL)
1515b032f27cSSam Leffler 				      ieee80211_node_authorize(ni);
1516b032f27cSSam Leffler 			} else {
1517b032f27cSSam Leffler 				vap->iv_stats.is_rx_acl++;
1518b032f27cSSam Leffler 				ieee80211_send_error(ni, ni->ni_macaddr,
1519b032f27cSSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1520b032f27cSSam Leffler 				ieee80211_node_leave(ni);
1521b032f27cSSam Leffler 			}
1522b032f27cSSam Leffler 			ieee80211_free_node(ni);
1523b032f27cSSam Leffler 		} else
1524b032f27cSSam Leffler 			error = ENOENT;
1525b032f27cSSam Leffler 		break;
1526b032f27cSSam Leffler 	default:
1527b032f27cSSam Leffler 		error = EINVAL;
1528b032f27cSSam Leffler 		break;
1529b032f27cSSam Leffler 	}
1530b032f27cSSam Leffler 	return error;
15318a1b9b6aSSam Leffler }
15328a1b9b6aSSam Leffler 
153368e8e04eSSam Leffler struct scanlookup {
153468e8e04eSSam Leffler 	const uint8_t *mac;
153568e8e04eSSam Leffler 	int esslen;
153668e8e04eSSam Leffler 	const uint8_t *essid;
153768e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se;
153868e8e04eSSam Leffler };
153968e8e04eSSam Leffler 
154068e8e04eSSam Leffler /*
154168e8e04eSSam Leffler  * Match mac address and any ssid.
154268e8e04eSSam Leffler  */
154368e8e04eSSam Leffler static void
154468e8e04eSSam Leffler mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
154568e8e04eSSam Leffler {
154668e8e04eSSam Leffler 	struct scanlookup *look = arg;
154768e8e04eSSam Leffler 
154868e8e04eSSam Leffler 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
154968e8e04eSSam Leffler 		return;
155068e8e04eSSam Leffler 	if (look->esslen != 0) {
155168e8e04eSSam Leffler 		if (se->se_ssid[1] != look->esslen)
155268e8e04eSSam Leffler 			return;
155368e8e04eSSam Leffler 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
155468e8e04eSSam Leffler 			return;
155568e8e04eSSam Leffler 	}
155668e8e04eSSam Leffler 	look->se = se;
155768e8e04eSSam Leffler }
155868e8e04eSSam Leffler 
155974bdb731SAndriy Voskoboinyk static int
1560632ee7e3SBernhard Schmidt setmlme_assoc_sta(struct ieee80211vap *vap,
1561632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1562632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1563b032f27cSSam Leffler {
1564b032f27cSSam Leffler 	struct scanlookup lookup;
1565b032f27cSSam Leffler 
1566632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1567632ee7e3SBernhard Schmidt 	    ("expected opmode STA not %s",
1568632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1569b032f27cSSam Leffler 
1570b032f27cSSam Leffler 	/* NB: this is racey if roaming is !manual */
1571b032f27cSSam Leffler 	lookup.se = NULL;
1572b032f27cSSam Leffler 	lookup.mac = mac;
1573b032f27cSSam Leffler 	lookup.esslen = ssid_len;
1574b032f27cSSam Leffler 	lookup.essid = ssid;
1575b032f27cSSam Leffler 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1576b032f27cSSam Leffler 	if (lookup.se == NULL)
1577b032f27cSSam Leffler 		return ENOENT;
1578b032f27cSSam Leffler 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
157910959256SSam Leffler 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1580b032f27cSSam Leffler 		return EIO;		/* XXX unique but could be better */
1581b032f27cSSam Leffler 	return 0;
1582b032f27cSSam Leffler }
1583b032f27cSSam Leffler 
158474bdb731SAndriy Voskoboinyk static int
1585632ee7e3SBernhard Schmidt setmlme_assoc_adhoc(struct ieee80211vap *vap,
1586632ee7e3SBernhard Schmidt 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1587632ee7e3SBernhard Schmidt 	const uint8_t ssid[IEEE80211_NWID_LEN])
1588632ee7e3SBernhard Schmidt {
158974bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
159074bdb731SAndriy Voskoboinyk 	int error;
1591632ee7e3SBernhard Schmidt 
1592632ee7e3SBernhard Schmidt 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1593632ee7e3SBernhard Schmidt 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1594632ee7e3SBernhard Schmidt 	    ("expected opmode IBSS or AHDEMO not %s",
1595632ee7e3SBernhard Schmidt 	    ieee80211_opmode_name[vap->iv_opmode]));
1596632ee7e3SBernhard Schmidt 
1597632ee7e3SBernhard Schmidt 	if (ssid_len == 0)
1598632ee7e3SBernhard Schmidt 		return EINVAL;
1599632ee7e3SBernhard Schmidt 
160074bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
160174bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
160274bdb731SAndriy Voskoboinyk 	if (sr == NULL)
160374bdb731SAndriy Voskoboinyk 		return ENOMEM;
160474bdb731SAndriy Voskoboinyk 
1605632ee7e3SBernhard Schmidt 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1606632ee7e3SBernhard Schmidt 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1607632ee7e3SBernhard Schmidt 	vap->iv_des_ssid[0].len = ssid_len;
1608632ee7e3SBernhard Schmidt 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1609632ee7e3SBernhard Schmidt 	vap->iv_des_nssid = 1;
1610632ee7e3SBernhard Schmidt 
161174bdb731SAndriy Voskoboinyk 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
161274bdb731SAndriy Voskoboinyk 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
161374bdb731SAndriy Voskoboinyk 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
161474bdb731SAndriy Voskoboinyk 	sr->sr_ssid[0].len = ssid_len;
161574bdb731SAndriy Voskoboinyk 	sr->sr_nssid = 1;
1616632ee7e3SBernhard Schmidt 
161774bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
161874bdb731SAndriy Voskoboinyk 
161974bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
162074bdb731SAndriy Voskoboinyk 	return error;
1621632ee7e3SBernhard Schmidt }
1622632ee7e3SBernhard Schmidt 
162374bdb731SAndriy Voskoboinyk static int
1624b032f27cSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
16258a1b9b6aSSam Leffler {
16268a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
16278a1b9b6aSSam Leffler 	int error;
16288a1b9b6aSSam Leffler 
16298a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
16308a1b9b6aSSam Leffler 		return EINVAL;
16318a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
16328a1b9b6aSSam Leffler 	if (error)
16338a1b9b6aSSam Leffler 		return error;
1634632ee7e3SBernhard Schmidt 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1635632ee7e3SBernhard Schmidt 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1636632ee7e3SBernhard Schmidt 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1637b032f27cSSam Leffler 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1638edd39a8eSRui Paulo 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1639edd39a8eSRui Paulo 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1640edd39a8eSRui Paulo 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1641632ee7e3SBernhard Schmidt 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1642632ee7e3SBernhard Schmidt 		    mlme.im_ssid_len, mlme.im_ssid);
16438a1b9b6aSSam Leffler 	else
1644b032f27cSSam Leffler 		return setmlme_common(vap, mlme.im_op,
1645b032f27cSSam Leffler 		    mlme.im_macaddr, mlme.im_reason);
16468a1b9b6aSSam Leffler }
16478a1b9b6aSSam Leffler 
164874bdb731SAndriy Voskoboinyk static int
1649b032f27cSSam Leffler ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
16508a1b9b6aSSam Leffler {
165168e8e04eSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1652b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16538a1b9b6aSSam Leffler 	int error;
16548a1b9b6aSSam Leffler 
16558a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
16568a1b9b6aSSam Leffler 		return EINVAL;
16578a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
16588a1b9b6aSSam Leffler 	if (error)
16598a1b9b6aSSam Leffler 		return error;
16608a1b9b6aSSam Leffler 	if (acl == NULL) {
16618a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
1662b032f27cSSam Leffler 		if (acl == NULL || !acl->iac_attach(vap))
16638a1b9b6aSSam Leffler 			return EINVAL;
1664b032f27cSSam Leffler 		vap->iv_acl = acl;
16658a1b9b6aSSam Leffler 	}
16668a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1667b032f27cSSam Leffler 		acl->iac_add(vap, mac);
16688a1b9b6aSSam Leffler 	else
1669b032f27cSSam Leffler 		acl->iac_remove(vap, mac);
16708a1b9b6aSSam Leffler 	return 0;
16718a1b9b6aSSam Leffler }
16728a1b9b6aSSam Leffler 
167374bdb731SAndriy Voskoboinyk static int
1674b032f27cSSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
16758a1b9b6aSSam Leffler {
1676b032f27cSSam Leffler 	const struct ieee80211_aclator *acl = vap->iv_acl;
16778a1b9b6aSSam Leffler 
16788a1b9b6aSSam Leffler 	switch (ireq->i_val) {
16798a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
16808a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
16818a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
1682b032f27cSSam Leffler 	case IEEE80211_MACCMD_POLICY_RADIUS:
16838a1b9b6aSSam Leffler 		if (acl == NULL) {
16848a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
1685b032f27cSSam Leffler 			if (acl == NULL || !acl->iac_attach(vap))
16868a1b9b6aSSam Leffler 				return EINVAL;
1687b032f27cSSam Leffler 			vap->iv_acl = acl;
16888a1b9b6aSSam Leffler 		}
1689b032f27cSSam Leffler 		acl->iac_setpolicy(vap, ireq->i_val);
16908a1b9b6aSSam Leffler 		break;
16918a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
16928a1b9b6aSSam Leffler 		if (acl != NULL)
1693b032f27cSSam Leffler 			acl->iac_flush(vap);
16948a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
16958a1b9b6aSSam Leffler 		break;
16968a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
16978a1b9b6aSSam Leffler 		if (acl != NULL) {
1698b032f27cSSam Leffler 			vap->iv_acl = NULL;
1699b032f27cSSam Leffler 			acl->iac_detach(vap);
17008a1b9b6aSSam Leffler 		}
17018a1b9b6aSSam Leffler 		break;
17028a1b9b6aSSam Leffler 	default:
1703188757f5SSam Leffler 		if (acl == NULL)
17048a1b9b6aSSam Leffler 			return EINVAL;
1705188757f5SSam Leffler 		else
1706b032f27cSSam Leffler 			return acl->iac_setioctl(vap, ireq);
17078a1b9b6aSSam Leffler 	}
17088a1b9b6aSSam Leffler 	return 0;
17098a1b9b6aSSam Leffler }
17108a1b9b6aSSam Leffler 
171174bdb731SAndriy Voskoboinyk static int
1712b032f27cSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
17138a1b9b6aSSam Leffler {
1714b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17158658b18bSSam Leffler 	uint8_t *chanlist, *list;
17168658b18bSSam Leffler 	int i, nchan, maxchan, error;
17178a1b9b6aSSam Leffler 
17188658b18bSSam Leffler 	if (ireq->i_len > sizeof(ic->ic_chan_active))
17198658b18bSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
1720b9b53389SAdrian Chadd 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1721b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
17228658b18bSSam Leffler 	if (list == NULL)
17238658b18bSSam Leffler 		return ENOMEM;
17248658b18bSSam Leffler 	error = copyin(ireq->i_data, list, ireq->i_len);
17257b4d954cSRui Paulo 	if (error) {
1726b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
17278a1b9b6aSSam Leffler 		return error;
17287b4d954cSRui Paulo 	}
172968e8e04eSSam Leffler 	nchan = 0;
17308658b18bSSam Leffler 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
17318658b18bSSam Leffler 	maxchan = ireq->i_len * NBBY;
173231378b1cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
173331378b1cSSam Leffler 		const struct ieee80211_channel *c = &ic->ic_channels[i];
17348a1b9b6aSSam Leffler 		/*
173531378b1cSSam Leffler 		 * Calculate the intersection of the user list and the
173631378b1cSSam Leffler 		 * available channels so users can do things like specify
173731378b1cSSam Leffler 		 * 1-255 to get all available channels.
17388a1b9b6aSSam Leffler 		 */
17398658b18bSSam Leffler 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
174031378b1cSSam Leffler 			setbit(chanlist, c->ic_ieee);
174168e8e04eSSam Leffler 			nchan++;
17428a1b9b6aSSam Leffler 		}
17438a1b9b6aSSam Leffler 	}
17447b4d954cSRui Paulo 	if (nchan == 0) {
1745b9b53389SAdrian Chadd 		IEEE80211_FREE(list, M_TEMP);
174668e8e04eSSam Leffler 		return EINVAL;
17477b4d954cSRui Paulo 	}
174868e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
174968e8e04eSSam Leffler 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
175068e8e04eSSam Leffler 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
17518658b18bSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1752b032f27cSSam Leffler 	ieee80211_scan_flush(vap);
1753b9b53389SAdrian Chadd 	IEEE80211_FREE(list, M_TEMP);
1754b032f27cSSam Leffler 	return ENETRESET;
17558a1b9b6aSSam Leffler }
17568a1b9b6aSSam Leffler 
175774bdb731SAndriy Voskoboinyk static int
1758b032f27cSSam Leffler ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
175942568791SSam Leffler {
176042568791SSam Leffler 	struct ieee80211_node *ni;
176168e8e04eSSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
176242568791SSam Leffler 	int error;
176342568791SSam Leffler 
176442568791SSam Leffler 	/*
176542568791SSam Leffler 	 * NB: we could copyin ieee80211req_sta_stats so apps
176642568791SSam Leffler 	 *     could make selective changes but that's overkill;
176742568791SSam Leffler 	 *     just clear all stats for now.
176842568791SSam Leffler 	 */
176942568791SSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
177042568791SSam Leffler 		return EINVAL;
177142568791SSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
177242568791SSam Leffler 	if (error != 0)
177342568791SSam Leffler 		return error;
1774b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
177542568791SSam Leffler 	if (ni == NULL)
1776b032f27cSSam Leffler 		return ENOENT;
1777b032f27cSSam Leffler 	/* XXX require ni_vap == vap? */
177842568791SSam Leffler 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
177942568791SSam Leffler 	ieee80211_free_node(ni);
178042568791SSam Leffler 	return 0;
178142568791SSam Leffler }
178242568791SSam Leffler 
178374bdb731SAndriy Voskoboinyk static int
1784b032f27cSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
17858a1b9b6aSSam Leffler {
17868a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17878a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
17888a1b9b6aSSam Leffler 	int error;
17898a1b9b6aSSam Leffler 
17908a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
17918a1b9b6aSSam Leffler 		return EINVAL;
17928a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
17938a1b9b6aSSam Leffler 	if (error != 0)
17948a1b9b6aSSam Leffler 		return error;
1795b032f27cSSam Leffler 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
17968a1b9b6aSSam Leffler 	if (ni == NULL)
1797b032f27cSSam Leffler 		return ENOENT;
17988a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
17998a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
18008a1b9b6aSSam Leffler 	return error;
18018a1b9b6aSSam Leffler }
18028a1b9b6aSSam Leffler 
180374bdb731SAndriy Voskoboinyk static int
1804b032f27cSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
18058a1b9b6aSSam Leffler {
1806b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
18078a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
18088a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
1809fa46d9dbSAdrian Chadd 	int isbss, ac, aggrmode;
18108a1b9b6aSSam Leffler 
18118a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1812b032f27cSSam Leffler 		return EOPNOTSUPP;
18138a1b9b6aSSam Leffler 
18148a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
18158a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1816fa46d9dbSAdrian Chadd 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
18178a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
18188a1b9b6aSSam Leffler 		ac = WME_AC_BE;
18198a1b9b6aSSam Leffler 	if (isbss) {
18208a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
18218a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
18228a1b9b6aSSam Leffler 	} else {
18238a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
18248a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
18258a1b9b6aSSam Leffler 	}
18268a1b9b6aSSam Leffler 	switch (ireq->i_type) {
18278a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
18288a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = ireq->i_val;
1829fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18308a1b9b6aSSam Leffler 			chanp->wmep_logcwmin = ireq->i_val;
18318a1b9b6aSSam Leffler 		break;
18328a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
18338a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = ireq->i_val;
1834fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18358a1b9b6aSSam Leffler 			chanp->wmep_logcwmax = ireq->i_val;
18368a1b9b6aSSam Leffler 		break;
18378a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
18388a1b9b6aSSam Leffler 		wmep->wmep_aifsn = ireq->i_val;
1839fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18408a1b9b6aSSam Leffler 			chanp->wmep_aifsn = ireq->i_val;
18418a1b9b6aSSam Leffler 		break;
18428a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
18438a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = ireq->i_val;
1844fa46d9dbSAdrian Chadd 		if (!isbss || !aggrmode)
18458a1b9b6aSSam Leffler 			chanp->wmep_txopLimit = ireq->i_val;
18468a1b9b6aSSam Leffler 		break;
18478a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
18488a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
1849fa46d9dbSAdrian Chadd 		if (!aggrmode)
18508a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
18518a1b9b6aSSam Leffler 		break;
18528a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
18538a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
18548a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
18558a1b9b6aSSam Leffler 		break;
18568a1b9b6aSSam Leffler 	}
1857b032f27cSSam Leffler 	ieee80211_wme_updateparams(vap);
18588a1b9b6aSSam Leffler 	return 0;
18598a1b9b6aSSam Leffler }
18608a1b9b6aSSam Leffler 
18618a1b9b6aSSam Leffler static int
186268e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int start, int freq)
186368e8e04eSSam Leffler {
186468e8e04eSSam Leffler 	const struct ieee80211_channel *c;
186568e8e04eSSam Leffler 	int i;
186668e8e04eSSam Leffler 
186768e8e04eSSam Leffler 	for (i = start+1; i < ic->ic_nchans; i++) {
186868e8e04eSSam Leffler 		c = &ic->ic_channels[i];
186968e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
187068e8e04eSSam Leffler 			return 1;
187168e8e04eSSam Leffler 	}
187268e8e04eSSam Leffler 	/* NB: should not be needed but in case things are mis-sorted */
187368e8e04eSSam Leffler 	for (i = 0; i < start; i++) {
187468e8e04eSSam Leffler 		c = &ic->ic_channels[i];
187568e8e04eSSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
187668e8e04eSSam Leffler 			return 1;
187768e8e04eSSam Leffler 	}
187868e8e04eSSam Leffler 	return 0;
187968e8e04eSSam Leffler }
188068e8e04eSSam Leffler 
188168e8e04eSSam Leffler static struct ieee80211_channel *
188268e8e04eSSam Leffler findchannel(struct ieee80211com *ic, int ieee, int mode)
188368e8e04eSSam Leffler {
188468e8e04eSSam Leffler 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1885be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= 0,
1886be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1887be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1888be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1889be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1890be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1891be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1892be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
18936a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
18946a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
189568e8e04eSSam Leffler 	    /* NB: handled specially below */
1896be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1897be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
189835bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IEEE80211_CHAN_A,
189935bcfd1cSAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IEEE80211_CHAN_G,
190068e8e04eSSam Leffler 	};
190168e8e04eSSam Leffler 	u_int modeflags;
190268e8e04eSSam Leffler 	int i;
190368e8e04eSSam Leffler 
190468e8e04eSSam Leffler 	modeflags = chanflags[mode];
190568e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
190668e8e04eSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
190768e8e04eSSam Leffler 
190868e8e04eSSam Leffler 		if (c->ic_ieee != ieee)
190968e8e04eSSam Leffler 			continue;
191068e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
191168e8e04eSSam Leffler 			/* ignore turbo channels for autoselect */
191268e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_TURBO(c))
191368e8e04eSSam Leffler 				continue;
191468e8e04eSSam Leffler 			/*
191568e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we
191668e8e04eSSam Leffler 			 *     always select the g channel if both
191768e8e04eSSam Leffler 			 *     are present.
191868e8e04eSSam Leffler 			 * XXX prefer HT to non-HT?
191968e8e04eSSam Leffler 			 */
192068e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_B(c) ||
192168e8e04eSSam Leffler 			    !find11gchannel(ic, i, c->ic_freq))
192268e8e04eSSam Leffler 				return c;
192368e8e04eSSam Leffler 		} else {
192435bcfd1cSAdrian Chadd 			/* must check VHT specifically */
192535bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_VHT_5GHZ ||
192635bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_VHT_2GHZ) &&
192735bcfd1cSAdrian Chadd 			    !IEEE80211_IS_CHAN_VHT(c))
192835bcfd1cSAdrian Chadd 				continue;
192935bcfd1cSAdrian Chadd 
193035bcfd1cSAdrian Chadd 			/*
193135bcfd1cSAdrian Chadd 			 * Must check HT specially - only match on HT,
193235bcfd1cSAdrian Chadd 			 * not HT+VHT channels
193335bcfd1cSAdrian Chadd 			 */
193468e8e04eSSam Leffler 			if ((mode == IEEE80211_MODE_11NA ||
193568e8e04eSSam Leffler 			    mode == IEEE80211_MODE_11NG) &&
193668e8e04eSSam Leffler 			    !IEEE80211_IS_CHAN_HT(c))
193768e8e04eSSam Leffler 				continue;
193835bcfd1cSAdrian Chadd 
193935bcfd1cSAdrian Chadd 			if ((mode == IEEE80211_MODE_11NA ||
194035bcfd1cSAdrian Chadd 			    mode == IEEE80211_MODE_11NG) &&
194135bcfd1cSAdrian Chadd 			    IEEE80211_IS_CHAN_VHT(c))
194235bcfd1cSAdrian Chadd 				continue;
194335bcfd1cSAdrian Chadd 
194435bcfd1cSAdrian Chadd 			/* Check that the modeflags above match */
194568e8e04eSSam Leffler 			if ((c->ic_flags & modeflags) == modeflags)
194668e8e04eSSam Leffler 				return c;
194768e8e04eSSam Leffler 		}
194868e8e04eSSam Leffler 	}
194968e8e04eSSam Leffler 	return NULL;
195068e8e04eSSam Leffler }
195168e8e04eSSam Leffler 
195268e8e04eSSam Leffler /*
195368e8e04eSSam Leffler  * Check the specified against any desired mode (aka netband).
195468e8e04eSSam Leffler  * This is only used (presently) when operating in hostap mode
195568e8e04eSSam Leffler  * to enforce consistency.
195668e8e04eSSam Leffler  */
195768e8e04eSSam Leffler static int
195868e8e04eSSam Leffler check_mode_consistency(const struct ieee80211_channel *c, int mode)
195968e8e04eSSam Leffler {
196068e8e04eSSam Leffler 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
196168e8e04eSSam Leffler 
196268e8e04eSSam Leffler 	switch (mode) {
196368e8e04eSSam Leffler 	case IEEE80211_MODE_11B:
196468e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_B(c));
196568e8e04eSSam Leffler 	case IEEE80211_MODE_11G:
196668e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
196768e8e04eSSam Leffler 	case IEEE80211_MODE_11A:
196868e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
196968e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
197068e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_STURBO(c));
197168e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
197268e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTA(c));
197368e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
197468e8e04eSSam Leffler 		return (IEEE80211_IS_CHAN_HTG(c));
197568e8e04eSSam Leffler 	}
197668e8e04eSSam Leffler 	return 1;
197768e8e04eSSam Leffler 
197868e8e04eSSam Leffler }
197968e8e04eSSam Leffler 
198068e8e04eSSam Leffler /*
198168e8e04eSSam Leffler  * Common code to set the current channel.  If the device
198268e8e04eSSam Leffler  * is up and running this may result in an immediate channel
198368e8e04eSSam Leffler  * change or a kick of the state machine.
198468e8e04eSSam Leffler  */
198568e8e04eSSam Leffler static int
1986b032f27cSSam Leffler setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
198768e8e04eSSam Leffler {
1988b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
198968e8e04eSSam Leffler 	int error;
199068e8e04eSSam Leffler 
199168e8e04eSSam Leffler 	if (c != IEEE80211_CHAN_ANYC) {
1992b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1993b032f27cSSam Leffler 			return EBUSY;	/* XXX better code? */
1994b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1995b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
199668e8e04eSSam Leffler 				return EINVAL;
1997b032f27cSSam Leffler 			if (!check_mode_consistency(c, vap->iv_des_mode))
1998b032f27cSSam Leffler 				return EINVAL;
1999b032f27cSSam Leffler 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
2000b032f27cSSam Leffler 			if (IEEE80211_IS_CHAN_NOADHOC(c))
2001b032f27cSSam Leffler 				return EINVAL;
2002b032f27cSSam Leffler 		}
20031b74d2a4SAdrian Chadd 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
2004b032f27cSSam Leffler 		    vap->iv_bss->ni_chan == c)
200568e8e04eSSam Leffler 			return 0;	/* NB: nothing to do */
200668e8e04eSSam Leffler 	}
2007b032f27cSSam Leffler 	vap->iv_des_chan = c;
200868e8e04eSSam Leffler 
200968e8e04eSSam Leffler 	error = 0;
2010b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
2011b032f27cSSam Leffler 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
201268e8e04eSSam Leffler 		/*
2013b032f27cSSam Leffler 		 * Monitor mode can switch directly.
201468e8e04eSSam Leffler 		 */
2015b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
2016b032f27cSSam Leffler 			/* XXX need state machine for other vap's to follow */
2017b032f27cSSam Leffler 			ieee80211_setcurchan(ic, vap->iv_des_chan);
2018b032f27cSSam Leffler 			vap->iv_bss->ni_chan = ic->ic_curchan;
2019d2ffc7afSPedro F. Giffuni 		} else {
2020b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
202126d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2022d2ffc7afSPedro F. Giffuni 		}
202368e8e04eSSam Leffler 	} else {
202468e8e04eSSam Leffler 		/*
202568e8e04eSSam Leffler 		 * Need to go through the state machine in case we
202668e8e04eSSam Leffler 		 * need to reassociate or the like.  The state machine
202768e8e04eSSam Leffler 		 * will pickup the desired channel and avoid scanning.
202868e8e04eSSam Leffler 		 */
2029b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
2030b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2031b032f27cSSam Leffler 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
203268e8e04eSSam Leffler 			/*
203368e8e04eSSam Leffler 			 * When not up+running and a real channel has
203468e8e04eSSam Leffler 			 * been specified fix the current channel so
203568e8e04eSSam Leffler 			 * there is immediate feedback; e.g. via ifconfig.
203668e8e04eSSam Leffler 			 */
2037b032f27cSSam Leffler 			ic->ic_curchan = vap->iv_des_chan;
203826d39e2cSSam Leffler 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
203968e8e04eSSam Leffler 		}
204068e8e04eSSam Leffler 	}
204168e8e04eSSam Leffler 	return error;
204268e8e04eSSam Leffler }
204368e8e04eSSam Leffler 
204468e8e04eSSam Leffler /*
204568e8e04eSSam Leffler  * Old api for setting the current channel; this is
204668e8e04eSSam Leffler  * deprecated because channel numbers are ambiguous.
204768e8e04eSSam Leffler  */
204874bdb731SAndriy Voskoboinyk static int
2049b032f27cSSam Leffler ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
205068e8e04eSSam Leffler 	const struct ieee80211req *ireq)
205168e8e04eSSam Leffler {
2052b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
205368e8e04eSSam Leffler 	struct ieee80211_channel *c;
205468e8e04eSSam Leffler 
205568e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
205668e8e04eSSam Leffler 	if (ireq->i_val == 0 ||
205768e8e04eSSam Leffler 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
205868e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
205968e8e04eSSam Leffler 	} else {
206068e8e04eSSam Leffler 		struct ieee80211_channel *c2;
206168e8e04eSSam Leffler 
2062b032f27cSSam Leffler 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
206368e8e04eSSam Leffler 		if (c == NULL) {
206468e8e04eSSam Leffler 			c = findchannel(ic, ireq->i_val,
206568e8e04eSSam Leffler 				IEEE80211_MODE_AUTO);
206668e8e04eSSam Leffler 			if (c == NULL)
206768e8e04eSSam Leffler 				return EINVAL;
206868e8e04eSSam Leffler 		}
206935bcfd1cSAdrian Chadd 
207068e8e04eSSam Leffler 		/*
207168e8e04eSSam Leffler 		 * Fine tune channel selection based on desired mode:
207268e8e04eSSam Leffler 		 *   if 11b is requested, find the 11b version of any
207368e8e04eSSam Leffler 		 *      11g channel returned,
207468e8e04eSSam Leffler 		 *   if static turbo, find the turbo version of any
207568e8e04eSSam Leffler 		 *	11a channel return,
207668e8e04eSSam Leffler 		 *   if 11na is requested, find the ht version of any
207768e8e04eSSam Leffler 		 *      11a channel returned,
207868e8e04eSSam Leffler 		 *   if 11ng is requested, find the ht version of any
207968e8e04eSSam Leffler 		 *      11g channel returned,
208035bcfd1cSAdrian Chadd 		 *   if 11ac is requested, find the 11ac version
208135bcfd1cSAdrian Chadd 		 *      of any 11a/11na channel returned,
208235bcfd1cSAdrian Chadd 		 *   (TBD) 11acg (2GHz VHT)
208368e8e04eSSam Leffler 		 *   otherwise we should be ok with what we've got.
208468e8e04eSSam Leffler 		 */
2085b032f27cSSam Leffler 		switch (vap->iv_des_mode) {
208668e8e04eSSam Leffler 		case IEEE80211_MODE_11B:
208768e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
208868e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
208968e8e04eSSam Leffler 					IEEE80211_MODE_11B);
209068e8e04eSSam Leffler 				/* NB: should not happen, =>'s 11g w/o 11b */
209168e8e04eSSam Leffler 				if (c2 != NULL)
209268e8e04eSSam Leffler 					c = c2;
209368e8e04eSSam Leffler 			}
209468e8e04eSSam Leffler 			break;
209568e8e04eSSam Leffler 		case IEEE80211_MODE_TURBO_A:
209668e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
209768e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
209868e8e04eSSam Leffler 					IEEE80211_MODE_TURBO_A);
209968e8e04eSSam Leffler 				if (c2 != NULL)
210068e8e04eSSam Leffler 					c = c2;
210168e8e04eSSam Leffler 			}
210268e8e04eSSam Leffler 			break;
210368e8e04eSSam Leffler 		case IEEE80211_MODE_11NA:
210468e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_A(c)) {
210568e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
210668e8e04eSSam Leffler 					IEEE80211_MODE_11NA);
210768e8e04eSSam Leffler 				if (c2 != NULL)
210868e8e04eSSam Leffler 					c = c2;
210968e8e04eSSam Leffler 			}
211068e8e04eSSam Leffler 			break;
211168e8e04eSSam Leffler 		case IEEE80211_MODE_11NG:
211268e8e04eSSam Leffler 			if (IEEE80211_IS_CHAN_ANYG(c)) {
211368e8e04eSSam Leffler 				c2 = findchannel(ic, ireq->i_val,
211468e8e04eSSam Leffler 					IEEE80211_MODE_11NG);
211568e8e04eSSam Leffler 				if (c2 != NULL)
211668e8e04eSSam Leffler 					c = c2;
211768e8e04eSSam Leffler 			}
211868e8e04eSSam Leffler 			break;
211935bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_2GHZ:
212035bcfd1cSAdrian Chadd 			printf("%s: TBD\n", __func__);
212135bcfd1cSAdrian Chadd 			break;
212235bcfd1cSAdrian Chadd 		case IEEE80211_MODE_VHT_5GHZ:
212335bcfd1cSAdrian Chadd 			if (IEEE80211_IS_CHAN_A(c)) {
212435bcfd1cSAdrian Chadd 				c2 = findchannel(ic, ireq->i_val,
212535bcfd1cSAdrian Chadd 					IEEE80211_MODE_VHT_5GHZ);
212635bcfd1cSAdrian Chadd 				if (c2 != NULL)
212735bcfd1cSAdrian Chadd 					c = c2;
212835bcfd1cSAdrian Chadd 			}
212935bcfd1cSAdrian Chadd 			break;
213068e8e04eSSam Leffler 		default:		/* NB: no static turboG */
213168e8e04eSSam Leffler 			break;
213268e8e04eSSam Leffler 		}
213368e8e04eSSam Leffler 	}
2134b032f27cSSam Leffler 	return setcurchan(vap, c);
213568e8e04eSSam Leffler }
213668e8e04eSSam Leffler 
213768e8e04eSSam Leffler /*
213868e8e04eSSam Leffler  * New/current api for setting the current channel; a complete
213968e8e04eSSam Leffler  * channel description is provide so there is no ambiguity in
214068e8e04eSSam Leffler  * identifying the channel.
214168e8e04eSSam Leffler  */
214274bdb731SAndriy Voskoboinyk static int
2143b032f27cSSam Leffler ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
214468e8e04eSSam Leffler 	const struct ieee80211req *ireq)
214568e8e04eSSam Leffler {
2146b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
214768e8e04eSSam Leffler 	struct ieee80211_channel chan, *c;
214868e8e04eSSam Leffler 	int error;
214968e8e04eSSam Leffler 
215068e8e04eSSam Leffler 	if (ireq->i_len != sizeof(chan))
215168e8e04eSSam Leffler 		return EINVAL;
215268e8e04eSSam Leffler 	error = copyin(ireq->i_data, &chan, sizeof(chan));
215368e8e04eSSam Leffler 	if (error != 0)
215468e8e04eSSam Leffler 		return error;
215535bcfd1cSAdrian Chadd 
215668e8e04eSSam Leffler 	/* XXX 0xffff overflows 16-bit signed */
215768e8e04eSSam Leffler 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
215868e8e04eSSam Leffler 		c = IEEE80211_CHAN_ANYC;
215968e8e04eSSam Leffler 	} else {
216068e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
216168e8e04eSSam Leffler 		if (c == NULL)
216268e8e04eSSam Leffler 			return EINVAL;
216368e8e04eSSam Leffler 	}
2164b032f27cSSam Leffler 	return setcurchan(vap, c);
2165b032f27cSSam Leffler }
2166b032f27cSSam Leffler 
216774bdb731SAndriy Voskoboinyk static int
2168b032f27cSSam Leffler ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2169b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2170b032f27cSSam Leffler {
2171b032f27cSSam Leffler 	struct ieee80211_regdomain_req *reg;
21728658b18bSSam Leffler 	int nchans, error;
2173b032f27cSSam Leffler 
21748658b18bSSam Leffler 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
21758658b18bSSam Leffler 	    sizeof(struct ieee80211_channel));
21768658b18bSSam Leffler 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
21778658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21788658b18bSSam Leffler 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
21798658b18bSSam Leffler 		    ireq->i_len, nchans);
2180b032f27cSSam Leffler 		return EINVAL;
21818658b18bSSam Leffler 	}
21828658b18bSSam Leffler 	reg = (struct ieee80211_regdomain_req *)
2183b9b53389SAdrian Chadd 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2184b9b53389SAdrian Chadd 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
21858658b18bSSam Leffler 	if (reg == NULL) {
21868658b18bSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21878658b18bSSam Leffler 		    "%s: no memory, nchans %d\n", __func__, nchans);
2188b032f27cSSam Leffler 		return ENOMEM;
21898658b18bSSam Leffler 	}
21908658b18bSSam Leffler 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
21918658b18bSSam Leffler 	if (error == 0) {
21928658b18bSSam Leffler 		/* NB: validate inline channel count against storage size */
21938658b18bSSam Leffler 		if (reg->chaninfo.ic_nchans != nchans) {
21948658b18bSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
21958658b18bSSam Leffler 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
21968658b18bSSam Leffler 				reg->chaninfo.ic_nchans, nchans);
21978658b18bSSam Leffler 			error = EINVAL;
21988658b18bSSam Leffler 		} else
2199b032f27cSSam Leffler 			error = ieee80211_setregdomain(vap, reg);
22008658b18bSSam Leffler 	}
2201b9b53389SAdrian Chadd 	IEEE80211_FREE(reg, M_TEMP);
2202b032f27cSSam Leffler 
2203b032f27cSSam Leffler 	return (error == 0 ? ENETRESET : error);
220468e8e04eSSam Leffler }
220568e8e04eSSam Leffler 
220668e8e04eSSam Leffler static int
2207b032f27cSSam Leffler ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2208b032f27cSSam Leffler 	const struct ieee80211req *ireq)
22098a1b9b6aSSam Leffler {
2210b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2211b032f27cSSam Leffler 		return EINVAL;
2212b032f27cSSam Leffler 	/* XXX validate params */
2213b032f27cSSam Leffler 	/* XXX? ENETRESET to push to device? */
2214b032f27cSSam Leffler 	return copyin(ireq->i_data, vap->iv_roamparms,
2215b032f27cSSam Leffler 	    sizeof(vap->iv_roamparms));
2216b032f27cSSam Leffler }
2217b032f27cSSam Leffler 
2218b032f27cSSam Leffler static int
2219b032f27cSSam Leffler checkrate(const struct ieee80211_rateset *rs, int rate)
2220b032f27cSSam Leffler {
2221b032f27cSSam Leffler 	int i;
2222b032f27cSSam Leffler 
2223b032f27cSSam Leffler 	if (rate == IEEE80211_FIXED_RATE_NONE)
2224b032f27cSSam Leffler 		return 1;
2225b032f27cSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
2226b032f27cSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2227b032f27cSSam Leffler 			return 1;
2228b032f27cSSam Leffler 	return 0;
2229b032f27cSSam Leffler }
2230b032f27cSSam Leffler 
2231b032f27cSSam Leffler static int
2232dfabbaa0SAndriy Voskoboinyk checkmcs(const struct ieee80211_htrateset *rs, int mcs)
2233b032f27cSSam Leffler {
2234dfabbaa0SAndriy Voskoboinyk 	int rate_val = IEEE80211_RV(mcs);
2235dfabbaa0SAndriy Voskoboinyk 	int i;
2236dfabbaa0SAndriy Voskoboinyk 
2237b032f27cSSam Leffler 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2238b032f27cSSam Leffler 		return 1;
2239b032f27cSSam Leffler 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2240b032f27cSSam Leffler 		return 0;
2241dfabbaa0SAndriy Voskoboinyk 	for (i = 0; i < rs->rs_nrates; i++)
2242dfabbaa0SAndriy Voskoboinyk 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
2243dfabbaa0SAndriy Voskoboinyk 			return 1;
2244dfabbaa0SAndriy Voskoboinyk 	return 0;
2245b032f27cSSam Leffler }
2246b032f27cSSam Leffler 
224774bdb731SAndriy Voskoboinyk static int
2248b032f27cSSam Leffler ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2249b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2250b032f27cSSam Leffler {
2251b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2252b032f27cSSam Leffler 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2253b032f27cSSam Leffler 	struct ieee80211_txparam *src, *dst;
2254dfabbaa0SAndriy Voskoboinyk 	const struct ieee80211_htrateset *rs_ht;
2255b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2256cbb24c24SSam Leffler 	int error, mode, changed, is11n, nmodes;
2257b032f27cSSam Leffler 
2258cbb24c24SSam Leffler 	/* NB: accept short requests for backwards compat */
2259cbb24c24SSam Leffler 	if (ireq->i_len > sizeof(parms))
2260b032f27cSSam Leffler 		return EINVAL;
2261cbb24c24SSam Leffler 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2262b032f27cSSam Leffler 	if (error != 0)
2263b032f27cSSam Leffler 		return error;
2264cbb24c24SSam Leffler 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2265b032f27cSSam Leffler 	changed = 0;
2266b032f27cSSam Leffler 	/* validate parameters and check if anything changed */
2267cbb24c24SSam Leffler 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2268cbb24c24SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
2269b032f27cSSam Leffler 			continue;
2270cbb24c24SSam Leffler 		src = &parms.params[mode];
2271cbb24c24SSam Leffler 		dst = &vap->iv_txparms[mode];
2272cbb24c24SSam Leffler 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2273dfabbaa0SAndriy Voskoboinyk 		rs_ht = &ic->ic_sup_htrates;
2274cbb24c24SSam Leffler 		is11n = (mode == IEEE80211_MODE_11NA ||
2275cbb24c24SSam Leffler 			 mode == IEEE80211_MODE_11NG);
2276b032f27cSSam Leffler 		if (src->ucastrate != dst->ucastrate) {
2277cbb24c24SSam Leffler 			if (!checkrate(rs, src->ucastrate) &&
2278dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
2279b032f27cSSam Leffler 				return EINVAL;
2280b032f27cSSam Leffler 			changed++;
2281b032f27cSSam Leffler 		}
2282b032f27cSSam Leffler 		if (src->mcastrate != dst->mcastrate) {
2283cbb24c24SSam Leffler 			if (!checkrate(rs, src->mcastrate) &&
2284dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
2285b032f27cSSam Leffler 				return EINVAL;
2286b032f27cSSam Leffler 			changed++;
2287b032f27cSSam Leffler 		}
2288b032f27cSSam Leffler 		if (src->mgmtrate != dst->mgmtrate) {
2289cbb24c24SSam Leffler 			if (!checkrate(rs, src->mgmtrate) &&
2290dfabbaa0SAndriy Voskoboinyk 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
2291b032f27cSSam Leffler 				return EINVAL;
2292b032f27cSSam Leffler 			changed++;
2293b032f27cSSam Leffler 		}
2294b032f27cSSam Leffler 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2295b032f27cSSam Leffler 			changed++;
2296b032f27cSSam Leffler 	}
2297b032f27cSSam Leffler 	if (changed) {
2298b032f27cSSam Leffler 		/*
2299b032f27cSSam Leffler 		 * Copy new parameters in place and notify the
2300b032f27cSSam Leffler 		 * driver so it can push state to the device.
2301b032f27cSSam Leffler 		 */
2302cbb24c24SSam Leffler 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2303cbb24c24SSam Leffler 			if (isset(ic->ic_modecaps, mode))
2304cbb24c24SSam Leffler 				vap->iv_txparms[mode] = parms.params[mode];
2305b032f27cSSam Leffler 		}
2306b032f27cSSam Leffler 		/* XXX could be more intelligent,
2307b032f27cSSam Leffler 		   e.g. don't reset if setting not being used */
2308b032f27cSSam Leffler 		return ENETRESET;
2309b032f27cSSam Leffler 	}
2310b032f27cSSam Leffler 	return 0;
2311b032f27cSSam Leffler }
2312b032f27cSSam Leffler 
2313b032f27cSSam Leffler /*
2314b032f27cSSam Leffler  * Application Information Element support.
2315b032f27cSSam Leffler  */
2316b032f27cSSam Leffler static int
2317b032f27cSSam Leffler setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2318b032f27cSSam Leffler {
2319b032f27cSSam Leffler 	struct ieee80211_appie *app = *aie;
2320b032f27cSSam Leffler 	struct ieee80211_appie *napp;
2321b032f27cSSam Leffler 	int error;
2322b032f27cSSam Leffler 
2323b032f27cSSam Leffler 	if (ireq->i_len == 0) {		/* delete any existing ie */
2324b032f27cSSam Leffler 		if (app != NULL) {
2325b032f27cSSam Leffler 			*aie = NULL;	/* XXX racey */
2326b9b53389SAdrian Chadd 			IEEE80211_FREE(app, M_80211_NODE_IE);
2327b032f27cSSam Leffler 		}
2328b032f27cSSam Leffler 		return 0;
2329b032f27cSSam Leffler 	}
2330b032f27cSSam Leffler 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2331b032f27cSSam Leffler 		return EINVAL;
2332b032f27cSSam Leffler 	/*
2333b032f27cSSam Leffler 	 * Allocate a new appie structure and copy in the user data.
2334b032f27cSSam Leffler 	 * When done swap in the new structure.  Note that we do not
2335b032f27cSSam Leffler 	 * guard against users holding a ref to the old structure;
2336b032f27cSSam Leffler 	 * this must be handled outside this code.
2337b032f27cSSam Leffler 	 *
2338b032f27cSSam Leffler 	 * XXX bad bad bad
2339b032f27cSSam Leffler 	 */
2340b9b53389SAdrian Chadd 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2341b9b53389SAdrian Chadd 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2342b9b53389SAdrian Chadd 	    IEEE80211_M_NOWAIT);
2343b032f27cSSam Leffler 	if (napp == NULL)
2344b032f27cSSam Leffler 		return ENOMEM;
2345b032f27cSSam Leffler 	/* XXX holding ic lock */
2346b032f27cSSam Leffler 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2347b032f27cSSam Leffler 	if (error) {
2348b9b53389SAdrian Chadd 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2349b032f27cSSam Leffler 		return error;
2350b032f27cSSam Leffler 	}
2351b032f27cSSam Leffler 	napp->ie_len = ireq->i_len;
2352b032f27cSSam Leffler 	*aie = napp;
2353b032f27cSSam Leffler 	if (app != NULL)
2354b9b53389SAdrian Chadd 		IEEE80211_FREE(app, M_80211_NODE_IE);
2355b032f27cSSam Leffler 	return 0;
2356b032f27cSSam Leffler }
2357b032f27cSSam Leffler 
2358b032f27cSSam Leffler static void
2359b032f27cSSam Leffler setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2360b032f27cSSam Leffler {
2361b032f27cSSam Leffler 	/* validate data is present as best we can */
2362b032f27cSSam Leffler 	if (space == 0 || 2+ie[1] > space)
2363b032f27cSSam Leffler 		return;
2364b032f27cSSam Leffler 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2365b032f27cSSam Leffler 		vap->iv_wpa_ie = ie;
2366b032f27cSSam Leffler 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2367b032f27cSSam Leffler 		vap->iv_rsn_ie = ie;
2368b032f27cSSam Leffler }
2369b032f27cSSam Leffler 
237074bdb731SAndriy Voskoboinyk static int
2371b032f27cSSam Leffler ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2372b032f27cSSam Leffler 	const struct ieee80211req *ireq, int fc0)
2373b032f27cSSam Leffler {
2374b032f27cSSam Leffler 	int error;
2375b032f27cSSam Leffler 
2376b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2377b032f27cSSam Leffler 
2378b032f27cSSam Leffler 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2379b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2380b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2381b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2382b032f27cSSam Leffler 			error = EINVAL;
2383b032f27cSSam Leffler 			break;
2384b032f27cSSam Leffler 		}
2385b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_beacon, ireq);
2386b032f27cSSam Leffler 		if (error == 0)
2387b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2388b032f27cSSam Leffler 		break;
2389b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2390b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_proberesp, ireq);
2391b032f27cSSam Leffler 		break;
2392b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2393b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2394b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocresp, ireq);
2395b032f27cSSam Leffler 		else
2396b032f27cSSam Leffler 			error = EINVAL;
2397b032f27cSSam Leffler 		break;
2398b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2399b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_probereq, ireq);
2400b032f27cSSam Leffler 		break;
2401b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2402b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
2403b032f27cSSam Leffler 			error = setappie(&vap->iv_appie_assocreq, ireq);
2404b032f27cSSam Leffler 		else
2405b032f27cSSam Leffler 			error = EINVAL;
2406b032f27cSSam Leffler 		break;
2407b032f27cSSam Leffler 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2408b032f27cSSam Leffler 		error = setappie(&vap->iv_appie_wpa, ireq);
2409b032f27cSSam Leffler 		if (error == 0) {
2410b032f27cSSam Leffler 			/*
2411b032f27cSSam Leffler 			 * Must split single blob of data into separate
2412b032f27cSSam Leffler 			 * WPA and RSN ie's because they go in different
2413b032f27cSSam Leffler 			 * locations in the mgt frames.
2414b032f27cSSam Leffler 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2415b032f27cSSam Leffler 			 */
2416b032f27cSSam Leffler 			vap->iv_wpa_ie = NULL;
2417b032f27cSSam Leffler 			vap->iv_rsn_ie = NULL;
2418b032f27cSSam Leffler 			if (vap->iv_appie_wpa != NULL) {
2419b032f27cSSam Leffler 				struct ieee80211_appie *appie =
2420b032f27cSSam Leffler 				    vap->iv_appie_wpa;
2421b032f27cSSam Leffler 				uint8_t *data = appie->ie_data;
2422b032f27cSSam Leffler 
2423b032f27cSSam Leffler 				/* XXX ie length validate is painful, cheat */
2424b032f27cSSam Leffler 				setwparsnie(vap, data, appie->ie_len);
2425b032f27cSSam Leffler 				setwparsnie(vap, data + 2 + data[1],
2426b032f27cSSam Leffler 				    appie->ie_len - (2 + data[1]));
2427b032f27cSSam Leffler 			}
2428b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2429b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2430b032f27cSSam Leffler 				/*
2431b032f27cSSam Leffler 				 * Must rebuild beacon frame as the update
2432b032f27cSSam Leffler 				 * mechanism doesn't handle WPA/RSN ie's.
2433b032f27cSSam Leffler 				 * Could extend it but it doesn't normally
2434b032f27cSSam Leffler 				 * change; this is just to deal with hostapd
2435b032f27cSSam Leffler 				 * plumbing the ie after the interface is up.
2436b032f27cSSam Leffler 				 */
2437b032f27cSSam Leffler 				error = ENETRESET;
2438b032f27cSSam Leffler 			}
2439b032f27cSSam Leffler 		}
2440b032f27cSSam Leffler 		break;
2441b032f27cSSam Leffler 	default:
2442b032f27cSSam Leffler 		error = EINVAL;
2443b032f27cSSam Leffler 		break;
2444b032f27cSSam Leffler 	}
2445b032f27cSSam Leffler 	return error;
2446b032f27cSSam Leffler }
2447b032f27cSSam Leffler 
244874bdb731SAndriy Voskoboinyk static int
2449b032f27cSSam Leffler ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2450b032f27cSSam Leffler 	const struct ieee80211req *ireq)
2451b032f27cSSam Leffler {
2452b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2453b032f27cSSam Leffler 	int error;
2454b032f27cSSam Leffler 	uint8_t fc0;
2455b032f27cSSam Leffler 
2456b032f27cSSam Leffler 	fc0 = ireq->i_val & 0xff;
2457b032f27cSSam Leffler 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2458b032f27cSSam Leffler 		return EINVAL;
2459b032f27cSSam Leffler 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2460b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2461b032f27cSSam Leffler 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2462b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2463b032f27cSSam Leffler 	return error;
2464b032f27cSSam Leffler }
2465b032f27cSSam Leffler 
246674bdb731SAndriy Voskoboinyk static int
2467b032f27cSSam Leffler ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2468b032f27cSSam Leffler {
2469b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2470b032f27cSSam Leffler 	struct ieee80211_chanswitch_req csr;
2471b032f27cSSam Leffler 	struct ieee80211_channel *c;
2472b032f27cSSam Leffler 	int error;
2473b032f27cSSam Leffler 
2474b032f27cSSam Leffler 	if (ireq->i_len != sizeof(csr))
2475b032f27cSSam Leffler 		return EINVAL;
2476b032f27cSSam Leffler 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2477b032f27cSSam Leffler 	if (error != 0)
2478b032f27cSSam Leffler 		return error;
2479c70761e6SSam Leffler 	/* XXX adhoc mode not supported */
2480c70761e6SSam Leffler 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2481c70761e6SSam Leffler 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2482c70761e6SSam Leffler 		return EOPNOTSUPP;
2483b032f27cSSam Leffler 	c = ieee80211_find_channel(ic,
2484b032f27cSSam Leffler 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2485b032f27cSSam Leffler 	if (c == NULL)
2486b032f27cSSam Leffler 		return ENOENT;
2487b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2488b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2489b032f27cSSam Leffler 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2490c70761e6SSam Leffler 	else if (csr.csa_count == 0)
2491c70761e6SSam Leffler 		ieee80211_csa_cancelswitch(ic);
2492b032f27cSSam Leffler 	else
2493b032f27cSSam Leffler 		error = EBUSY;
2494b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2495b032f27cSSam Leffler 	return error;
2496b032f27cSSam Leffler }
2497b032f27cSSam Leffler 
2498632ee7e3SBernhard Schmidt static int
2499632ee7e3SBernhard Schmidt ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2500b032f27cSSam Leffler {
2501b032f27cSSam Leffler #define	IEEE80211_IOC_SCAN_FLAGS \
2502b032f27cSSam Leffler 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2503b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2504b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2505b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2506b032f27cSSam Leffler 	 IEEE80211_IOC_SCAN_CHECK)
2507b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25087ca7a42cSBernhard Schmidt 	int error, i;
2509b032f27cSSam Leffler 
2510b032f27cSSam Leffler 	/* convert duration */
2511632ee7e3SBernhard Schmidt 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2512632ee7e3SBernhard Schmidt 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2513b032f27cSSam Leffler 	else {
2514632ee7e3SBernhard Schmidt 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2515632ee7e3SBernhard Schmidt 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2516b032f27cSSam Leffler 			return EINVAL;
2517632ee7e3SBernhard Schmidt 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2518632ee7e3SBernhard Schmidt 		if (sr->sr_duration < 1)
2519632ee7e3SBernhard Schmidt 			sr->sr_duration = 1;
2520b032f27cSSam Leffler 	}
2521b032f27cSSam Leffler 	/* convert min/max channel dwell */
2522632ee7e3SBernhard Schmidt 	if (sr->sr_mindwell != 0) {
2523632ee7e3SBernhard Schmidt 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2524632ee7e3SBernhard Schmidt 		if (sr->sr_mindwell < 1)
2525632ee7e3SBernhard Schmidt 			sr->sr_mindwell = 1;
2526b032f27cSSam Leffler 	}
2527632ee7e3SBernhard Schmidt 	if (sr->sr_maxdwell != 0) {
2528632ee7e3SBernhard Schmidt 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2529632ee7e3SBernhard Schmidt 		if (sr->sr_maxdwell < 1)
2530632ee7e3SBernhard Schmidt 			sr->sr_maxdwell = 1;
2531b032f27cSSam Leffler 	}
2532b032f27cSSam Leffler 	/* NB: silently reduce ssid count to what is supported */
2533632ee7e3SBernhard Schmidt 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2534632ee7e3SBernhard Schmidt 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2535632ee7e3SBernhard Schmidt 	for (i = 0; i < sr->sr_nssid; i++)
2536632ee7e3SBernhard Schmidt 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2537b032f27cSSam Leffler 			return EINVAL;
2538b032f27cSSam Leffler 	/* cleanse flags just in case, could reject if invalid flags */
2539632ee7e3SBernhard Schmidt 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2540b032f27cSSam Leffler 	/*
2541b032f27cSSam Leffler 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2542b032f27cSSam Leffler 	 * allows applications to scan without joining a bss (or picking
2543b032f27cSSam Leffler 	 * a channel and setting up a bss) and without forcing manual
2544b032f27cSSam Leffler 	 * roaming mode--you just need to mark the parent device UP.
2545b032f27cSSam Leffler 	 */
2546b032f27cSSam Leffler 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2547632ee7e3SBernhard Schmidt 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2548b032f27cSSam Leffler 
2549b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2550b032f27cSSam Leffler 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2551632ee7e3SBernhard Schmidt 	    __func__, sr->sr_flags,
2552b032f27cSSam Leffler 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2553632ee7e3SBernhard Schmidt 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2554b032f27cSSam Leffler 	/*
2555b032f27cSSam Leffler 	 * If we are in INIT state then the driver has never had a chance
2556b032f27cSSam Leffler 	 * to setup hardware state to do a scan; we must use the state
2557b032f27cSSam Leffler 	 * machine to get us up to the SCAN state but once we reach SCAN
2558b032f27cSSam Leffler 	 * state we then want to use the supplied params.  Stash the
2559b032f27cSSam Leffler 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2560b032f27cSSam Leffler 	 * state machines will recognize this and use the stashed params
2561b032f27cSSam Leffler 	 * to issue the scan request.
2562b032f27cSSam Leffler 	 *
2563b032f27cSSam Leffler 	 * Otherwise just invoke the scan machinery directly.
2564b032f27cSSam Leffler 	 */
2565b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2566f20503faSAndriy Voskoboinyk 	if (ic->ic_nrunning == 0) {
2567f20503faSAndriy Voskoboinyk 		IEEE80211_UNLOCK(ic);
2568f20503faSAndriy Voskoboinyk 		return ENXIO;
2569f20503faSAndriy Voskoboinyk 	}
2570f20503faSAndriy Voskoboinyk 
2571b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_INIT) {
2572b032f27cSSam Leffler 		/* NB: clobbers previous settings */
2573632ee7e3SBernhard Schmidt 		vap->iv_scanreq_flags = sr->sr_flags;
2574632ee7e3SBernhard Schmidt 		vap->iv_scanreq_duration = sr->sr_duration;
2575632ee7e3SBernhard Schmidt 		vap->iv_scanreq_nssid = sr->sr_nssid;
2576632ee7e3SBernhard Schmidt 		for (i = 0; i < sr->sr_nssid; i++) {
2577632ee7e3SBernhard Schmidt 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2578632ee7e3SBernhard Schmidt 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2579632ee7e3SBernhard Schmidt 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2580b032f27cSSam Leffler 		}
2581b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2582b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2583b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2584b032f27cSSam Leffler 	} else {
2585b032f27cSSam Leffler 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2586b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2587632ee7e3SBernhard Schmidt 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
25887ca7a42cSBernhard Schmidt 			error = ieee80211_check_scan(vap, sr->sr_flags,
2589632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2590632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2591b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2592632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2593b032f27cSSam Leffler 		} else {
25947ca7a42cSBernhard Schmidt 			error = ieee80211_start_scan(vap, sr->sr_flags,
2595632ee7e3SBernhard Schmidt 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2596632ee7e3SBernhard Schmidt 			    sr->sr_nssid,
2597b032f27cSSam Leffler 			    /* NB: cheat, we assume structures are compatible */
2598632ee7e3SBernhard Schmidt 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2599b032f27cSSam Leffler 		}
26007ca7a42cSBernhard Schmidt 		if (error == 0)
26017ca7a42cSBernhard Schmidt 			return EINPROGRESS;
2602b032f27cSSam Leffler 	}
2603632ee7e3SBernhard Schmidt 	return 0;
2604b032f27cSSam Leffler #undef IEEE80211_IOC_SCAN_FLAGS
2605b032f27cSSam Leffler }
2606b032f27cSSam Leffler 
260774bdb731SAndriy Voskoboinyk static int
2608632ee7e3SBernhard Schmidt ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2609632ee7e3SBernhard Schmidt {
261074bdb731SAndriy Voskoboinyk 	struct ieee80211_scan_req *sr;
2611632ee7e3SBernhard Schmidt 	int error;
2612632ee7e3SBernhard Schmidt 
261374bdb731SAndriy Voskoboinyk 	if (ireq->i_len != sizeof(*sr))
2614632ee7e3SBernhard Schmidt 		return EINVAL;
261574bdb731SAndriy Voskoboinyk 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
261674bdb731SAndriy Voskoboinyk 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
261774bdb731SAndriy Voskoboinyk 	if (sr == NULL)
261874bdb731SAndriy Voskoboinyk 		return ENOMEM;
261974bdb731SAndriy Voskoboinyk 	error = copyin(ireq->i_data, sr, sizeof(*sr));
2620632ee7e3SBernhard Schmidt 	if (error != 0)
262174bdb731SAndriy Voskoboinyk 		goto bad;
262274bdb731SAndriy Voskoboinyk 	error = ieee80211_scanreq(vap, sr);
262374bdb731SAndriy Voskoboinyk bad:
262474bdb731SAndriy Voskoboinyk 	IEEE80211_FREE(sr, M_TEMP);
2625632ee7e3SBernhard Schmidt 	return error;
2626632ee7e3SBernhard Schmidt }
2627632ee7e3SBernhard Schmidt 
262874bdb731SAndriy Voskoboinyk static int
2629b032f27cSSam Leffler ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2630b032f27cSSam Leffler {
2631b032f27cSSam Leffler 	struct ieee80211_node *ni;
2632b032f27cSSam Leffler 	struct ieee80211req_sta_vlan vlan;
2633b032f27cSSam Leffler 	int error;
2634b032f27cSSam Leffler 
2635b032f27cSSam Leffler 	if (ireq->i_len != sizeof(vlan))
2636b032f27cSSam Leffler 		return EINVAL;
2637b032f27cSSam Leffler 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2638b032f27cSSam Leffler 	if (error != 0)
2639b032f27cSSam Leffler 		return error;
2640b032f27cSSam Leffler 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2641b032f27cSSam Leffler 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2642b032f27cSSam Leffler 		    vlan.sv_macaddr);
2643b032f27cSSam Leffler 		if (ni == NULL)
2644b032f27cSSam Leffler 			return ENOENT;
2645b032f27cSSam Leffler 	} else
2646b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
2647b032f27cSSam Leffler 	ni->ni_vlan = vlan.sv_vlan;
2648b032f27cSSam Leffler 	ieee80211_free_node(ni);
2649b032f27cSSam Leffler 	return error;
2650b032f27cSSam Leffler }
2651b032f27cSSam Leffler 
2652b032f27cSSam Leffler static int
2653b032f27cSSam Leffler isvap11g(const struct ieee80211vap *vap)
2654b032f27cSSam Leffler {
2655b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2656b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2657b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2658b032f27cSSam Leffler }
2659b032f27cSSam Leffler 
2660b032f27cSSam Leffler static int
2661b032f27cSSam Leffler isvapht(const struct ieee80211vap *vap)
2662b032f27cSSam Leffler {
2663b032f27cSSam Leffler 	const struct ieee80211_node *bss = vap->iv_bss;
2664b032f27cSSam Leffler 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2665b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2666b032f27cSSam Leffler }
2667b032f27cSSam Leffler 
26688c4e758aSSam Leffler /*
26698c4e758aSSam Leffler  * Dummy ioctl set handler so the linker set is defined.
26708c4e758aSSam Leffler  */
26718c4e758aSSam Leffler static int
26728c4e758aSSam Leffler dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
26738c4e758aSSam Leffler {
26748c4e758aSSam Leffler 	return ENOSYS;
26758c4e758aSSam Leffler }
26768c4e758aSSam Leffler IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
26778c4e758aSSam Leffler 
26788c4e758aSSam Leffler static int
26798c4e758aSSam Leffler ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
26808c4e758aSSam Leffler {
26818c4e758aSSam Leffler 	ieee80211_ioctl_setfunc * const *set;
26828c4e758aSSam Leffler 	int error;
26838c4e758aSSam Leffler 
26848c4e758aSSam Leffler 	SET_FOREACH(set, ieee80211_ioctl_setset) {
26858c4e758aSSam Leffler 		error = (*set)(vap, ireq);
26868c4e758aSSam Leffler 		if (error != ENOSYS)
26878c4e758aSSam Leffler 			return error;
26888c4e758aSSam Leffler 	}
26898c4e758aSSam Leffler 	return EINVAL;
26908c4e758aSSam Leffler }
26918c4e758aSSam Leffler 
269274bdb731SAndriy Voskoboinyk static int
2693b032f27cSSam Leffler ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2694b032f27cSSam Leffler {
2695b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
26968a1b9b6aSSam Leffler 	int error;
26978a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
269868e8e04eSSam Leffler 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
26998a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
270068e8e04eSSam Leffler 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
27018a1b9b6aSSam Leffler 	struct ieee80211_key *k;
27028a1b9b6aSSam Leffler 	u_int kid;
27033dfff737SWarner Losh 	uint32_t flags;
27048a1b9b6aSSam Leffler 
27058a1b9b6aSSam Leffler 	error = 0;
27061a1e1d21SSam Leffler 	switch (ireq->i_type) {
27071a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
27081a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
27098a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
27108a1b9b6aSSam Leffler 			return EINVAL;
27111a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
27121a1e1d21SSam Leffler 		if (error)
27131a1e1d21SSam Leffler 			break;
2714b032f27cSSam Leffler 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2715b032f27cSSam Leffler 		vap->iv_des_ssid[0].len = ireq->i_len;
2716b032f27cSSam Leffler 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2717b032f27cSSam Leffler 		vap->iv_des_nssid = (ireq->i_len > 0);
2718b032f27cSSam Leffler 		error = ENETRESET;
27191a1e1d21SSam Leffler 		break;
27201a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
27218a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27228a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
2723b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2724b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27258a1b9b6aSSam Leffler 			break;
27268a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
2727b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2728b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
27298a1b9b6aSSam Leffler 			break;
27308a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
2731b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2732b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
27338a1b9b6aSSam Leffler 			break;
27341a1e1d21SSam Leffler 		}
2735b032f27cSSam Leffler 		error = ENETRESET;
27361a1e1d21SSam Leffler 		break;
27371a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
27381a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
27398a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
27408a1b9b6aSSam Leffler 			return EINVAL;
2741b032f27cSSam Leffler 		k = &vap->iv_nw_keys[kid];
27428a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
27438a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
2744b032f27cSSam Leffler 			(void) ieee80211_crypto_delkey(vap, k);
27451a1e1d21SSam Leffler 			break;
27461a1e1d21SSam Leffler 		}
27478a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
27488a1b9b6aSSam Leffler 			return EINVAL;
27491a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
27501a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
27511a1e1d21SSam Leffler 		if (error)
27521a1e1d21SSam Leffler 			break;
2753b032f27cSSam Leffler 		ieee80211_key_update_begin(vap);
2754dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2755b032f27cSSam Leffler 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2756dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
27578a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
27588a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
275971fe06caSSam Leffler 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
276071fe06caSSam Leffler 			if  (!ieee80211_crypto_setkey(vap, k))
27618a1b9b6aSSam Leffler 				error = EINVAL;
27628a1b9b6aSSam Leffler 		} else
27638a1b9b6aSSam Leffler 			error = EINVAL;
2764b032f27cSSam Leffler 		ieee80211_key_update_end(vap);
27651a1e1d21SSam Leffler 		break;
27661a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
27671a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2768380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
276968e8e04eSSam Leffler 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
27708a1b9b6aSSam Leffler 			return EINVAL;
2771781487cfSAdrian Chadd 		/*
2772781487cfSAdrian Chadd 		 * Firmware devices may need to be told about an explicit
2773781487cfSAdrian Chadd 		 * key index here, versus just inferring it from the
2774781487cfSAdrian Chadd 		 * key set / change.  Since we may also need to pause
2775781487cfSAdrian Chadd 		 * things like transmit before the key is updated,
2776781487cfSAdrian Chadd 		 * give the driver a chance to flush things by tying
2777781487cfSAdrian Chadd 		 * into key update begin/end.
2778781487cfSAdrian Chadd 		 */
2779781487cfSAdrian Chadd 		ieee80211_key_update_begin(vap);
2780781487cfSAdrian Chadd 		ieee80211_crypto_set_deftxkey(vap, kid);
2781781487cfSAdrian Chadd 		ieee80211_key_update_end(vap);
27828a1b9b6aSSam Leffler 		break;
27838a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
27848a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27858a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
27868a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
27878a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
27888a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
27898a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
27908a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
27918a1b9b6aSSam Leffler 			if (auth == NULL)
27928a1b9b6aSSam Leffler 				return EINVAL;
27938a1b9b6aSSam Leffler 			break;
27948a1b9b6aSSam Leffler 		default:
27958a1b9b6aSSam Leffler 			return EINVAL;
27968a1b9b6aSSam Leffler 		}
27978a1b9b6aSSam Leffler 		switch (ireq->i_val) {
27988a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2799b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28008a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
28018a1b9b6aSSam Leffler 			break;
28028a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
2803b032f27cSSam Leffler 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
28048a1b9b6aSSam Leffler 			break;
28058a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
28068a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2807b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28088a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
2809b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28108a1b9b6aSSam Leffler 			break;
28118a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
2812b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_WPA;
28138a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
28148a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
28151a1e1d21SSam Leffler 			break;
28161a1e1d21SSam Leffler 		}
28178a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
2818b032f27cSSam Leffler 		vap->iv_bss->ni_authmode = ireq->i_val;
28198a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
2820b032f27cSSam Leffler 		vap->iv_auth = auth;
2821b032f27cSSam Leffler 		error = ENETRESET;
28221a1e1d21SSam Leffler 		break;
28231a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
2824b032f27cSSam Leffler 		error = ieee80211_ioctl_setchannel(vap, ireq);
28251a1e1d21SSam Leffler 		break;
28261a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
28271a1e1d21SSam Leffler 		switch (ireq->i_val) {
28281a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
2829b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2830b032f27cSSam Leffler 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2831b032f27cSSam Leffler 				error = ERESTART;
28321a1e1d21SSam Leffler 			}
28331a1e1d21SSam Leffler 			break;
28341a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
2835b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2836b032f27cSSam Leffler 				error = EOPNOTSUPP;
2837b032f27cSSam Leffler 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2838b032f27cSSam Leffler 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2839b032f27cSSam Leffler 				error = ERESTART;
28401a1e1d21SSam Leffler 			}
28411a1e1d21SSam Leffler 			break;
28421a1e1d21SSam Leffler 		default:
28431a1e1d21SSam Leffler 			error = EINVAL;
28441a1e1d21SSam Leffler 			break;
28451a1e1d21SSam Leffler 		}
28461a1e1d21SSam Leffler 		break;
28471a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
28488a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
28498a1b9b6aSSam Leffler 			return EINVAL;
28501a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
2851b032f27cSSam Leffler 		error = ERESTART;
28521a1e1d21SSam Leffler 		break;
285313604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
285470231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
285570231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
28568a1b9b6aSSam Leffler 			return EINVAL;
2857b032f27cSSam Leffler 		vap->iv_rtsthreshold = ireq->i_val;
2858b032f27cSSam Leffler 		error = ERESTART;
28591a1e1d21SSam Leffler 		break;
28602e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
28618a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
28628a1b9b6aSSam Leffler 			return EINVAL;
2863fcd9500fSBernhard Schmidt 		ic->ic_protmode = (enum ieee80211_protmode)ireq->i_val;
28642e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
286568e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
286668e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2867b032f27cSSam Leffler 			error = ERESTART;
28682e79ca97SSam Leffler 		break;
28692e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
28708a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2871b032f27cSSam Leffler 			return EOPNOTSUPP;
287268e8e04eSSam Leffler 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
287368e8e04eSSam Leffler 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
28748a1b9b6aSSam Leffler 			return EINVAL;
28758a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
2876b032f27cSSam Leffler 		error = ERESTART;
28778a1b9b6aSSam Leffler 		break;
28788a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
28798a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
28808a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
28818a1b9b6aSSam Leffler 			return EINVAL;
2882fcd9500fSBernhard Schmidt 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
28838a1b9b6aSSam Leffler 		/* XXXX reset? */
28848a1b9b6aSSam Leffler 		break;
28858a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
28868a1b9b6aSSam Leffler 		if (ireq->i_val) {
28878a1b9b6aSSam Leffler 			/* XXX check for key state? */
2888b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PRIVACY;
28898a1b9b6aSSam Leffler 		} else
2890b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2891b032f27cSSam Leffler 		/* XXX ERESTART? */
28928a1b9b6aSSam Leffler 		break;
28938a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
28948a1b9b6aSSam Leffler 		if (ireq->i_val)
2895b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
28968a1b9b6aSSam Leffler 		else
2897b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2898b032f27cSSam Leffler 		/* XXX ERESTART? */
28998a1b9b6aSSam Leffler 		break;
29008a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
2901b032f27cSSam Leffler 		error = ieee80211_ioctl_setkey(vap, ireq);
29028a1b9b6aSSam Leffler 		break;
29038a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
2904b032f27cSSam Leffler 		error = ieee80211_ioctl_delkey(vap, ireq);
29058a1b9b6aSSam Leffler 		break;
29068a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
2907b032f27cSSam Leffler 		error = ieee80211_ioctl_setmlme(vap, ireq);
29088a1b9b6aSSam Leffler 		break;
29098a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
29108a1b9b6aSSam Leffler 		if (ireq->i_val) {
2911b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2912b032f27cSSam Leffler 				return EOPNOTSUPP;
2913b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_COUNTERM;
29148a1b9b6aSSam Leffler 		} else
2915b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2916b032f27cSSam Leffler 		/* XXX ERESTART? */
29178a1b9b6aSSam Leffler 		break;
29188a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
29198a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
29208a1b9b6aSSam Leffler 			return EINVAL;
29218a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
29223dfff737SWarner Losh 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
29238a1b9b6aSSam Leffler 		switch (ireq->i_val) {
292491356585SAdrian Chadd 		case 0:
292591356585SAdrian Chadd 			/* wpa_supplicant calls this to clear the WPA config */
292691356585SAdrian Chadd 			break;
29278a1b9b6aSSam Leffler 		case 1:
29283dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
29293dfff737SWarner Losh 				return EOPNOTSUPP;
29303dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1;
29318a1b9b6aSSam Leffler 			break;
29328a1b9b6aSSam Leffler 		case 2:
29333dfff737SWarner Losh 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
29343dfff737SWarner Losh 				return EOPNOTSUPP;
29353dfff737SWarner Losh 			flags |= IEEE80211_F_WPA2;
29368a1b9b6aSSam Leffler 			break;
29378a1b9b6aSSam Leffler 		case 3:
29383dfff737SWarner Losh 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
29398d092df9SSam Leffler 				return EOPNOTSUPP;
29403dfff737SWarner Losh 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
29412e79ca97SSam Leffler 			break;
29423dfff737SWarner Losh 		default:	/*  Can't set any -> error */
29433dfff737SWarner Losh 			return EOPNOTSUPP;
29442e79ca97SSam Leffler 		}
29453dfff737SWarner Losh 		vap->iv_flags = flags;
2946b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
29478a1b9b6aSSam Leffler 		break;
29488a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
29498a1b9b6aSSam Leffler 		if (ireq->i_val) {
2950b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2951b032f27cSSam Leffler 				return EOPNOTSUPP;
2952b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_WME);
29538a1b9b6aSSam Leffler 		} else
2954b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2955b032f27cSSam Leffler 		error = ERESTART;	/* NB: can change beacon frame */
29568a1b9b6aSSam Leffler 		break;
29578a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
29588a1b9b6aSSam Leffler 		if (ireq->i_val)
2959b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_HIDESSID;
29608a1b9b6aSSam Leffler 		else
2961b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2962b032f27cSSam Leffler 		error = ERESTART;		/* XXX ENETRESET? */
29632e79ca97SSam Leffler 		break;
29648a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
29658a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
2966b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
29678a1b9b6aSSam Leffler 		else
2968b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
29698a1b9b6aSSam Leffler 		break;
29708a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
29718a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
29728a1b9b6aSSam Leffler 			return EINVAL;
29738a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
29748a1b9b6aSSam Leffler 		if (error)
29758a1b9b6aSSam Leffler 			break;
2976b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2977b032f27cSSam Leffler 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2978b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
29798a1b9b6aSSam Leffler 		else
2980b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2981b032f27cSSam Leffler 		error = ENETRESET;
29828a1b9b6aSSam Leffler 		break;
29838a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
2984b032f27cSSam Leffler 		error = ieee80211_ioctl_setchanlist(vap, ireq);
29858a1b9b6aSSam Leffler 		break;
2986b032f27cSSam Leffler #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2987b032f27cSSam Leffler #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2988b032f27cSSam Leffler 	case OLD_IEEE80211_IOC_SCAN_REQ:
2989b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2990b032f27cSSam Leffler 			"%s: active scan request\n", __func__);
2991b032f27cSSam Leffler 		/*
2992b032f27cSSam Leffler 		 * If we are in INIT state then the driver has never
2993b032f27cSSam Leffler 		 * had a chance to setup hardware state to do a scan;
2994b032f27cSSam Leffler 		 * use the state machine to get us up the SCAN state.
2995b032f27cSSam Leffler 		 * Otherwise just invoke the scan machinery to start
2996b032f27cSSam Leffler 		 * a one-time scan.
2997b032f27cSSam Leffler 		 */
2998b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
2999b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
3000b032f27cSSam Leffler 		else
3001b032f27cSSam Leffler 			(void) ieee80211_start_scan(vap,
300268e8e04eSSam Leffler 				IEEE80211_SCAN_ACTIVE |
300368e8e04eSSam Leffler 				IEEE80211_SCAN_NOPICK |
3004b032f27cSSam Leffler 				IEEE80211_SCAN_ONCE,
3005b032f27cSSam Leffler 				IEEE80211_SCAN_FOREVER, 0, 0,
300668e8e04eSSam Leffler 				/* XXX use ioctl params */
3007b032f27cSSam Leffler 				vap->iv_des_nssid, vap->iv_des_ssid);
3008b032f27cSSam Leffler 		break;
3009b032f27cSSam Leffler #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
3010b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
3011b032f27cSSam Leffler 		error = ieee80211_ioctl_scanreq(vap, ireq);
3012b032f27cSSam Leffler 		break;
3013b032f27cSSam Leffler 	case IEEE80211_IOC_SCAN_CANCEL:
3014b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3015b032f27cSSam Leffler 		    "%s: cancel scan\n", __func__);
3016b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);
3017b032f27cSSam Leffler 		break;
3018b032f27cSSam Leffler 	case IEEE80211_IOC_HTCONF:
3019b032f27cSSam Leffler 		if (ireq->i_val & 1)
30202bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
3021b032f27cSSam Leffler 		else
30222bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
3023b032f27cSSam Leffler 		if (ireq->i_val & 2)
30242bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
3025b032f27cSSam Leffler 		else
30262bfc8a91SSam Leffler 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
3027b032f27cSSam Leffler 		error = ENETRESET;
30288a1b9b6aSSam Leffler 		break;
30298a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
30308a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
3031b032f27cSSam Leffler 		error = ieee80211_ioctl_macmac(vap, ireq);
30328a1b9b6aSSam Leffler 		break;
30338a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
3034b032f27cSSam Leffler 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
30358a1b9b6aSSam Leffler 		break;
303642568791SSam Leffler 	case IEEE80211_IOC_STA_STATS:
3037b032f27cSSam Leffler 		error = ieee80211_ioctl_setstastats(vap, ireq);
303842568791SSam Leffler 		break;
30398a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
3040b032f27cSSam Leffler 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
30418a1b9b6aSSam Leffler 		break;
30428a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
30438a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
30448a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
30458a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
30468a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
3047fa46d9dbSAdrian Chadd 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
3048b032f27cSSam Leffler 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
30498a1b9b6aSSam Leffler 		break;
30508a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
3051b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
305259aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3053b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
30548a1b9b6aSSam Leffler 			return EINVAL;
30558a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
30568a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
3057b032f27cSSam Leffler 			vap->iv_dtim_period = ireq->i_val;
30588a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
30598a1b9b6aSSam Leffler 		} else
30608a1b9b6aSSam Leffler 			error = EINVAL;
30618a1b9b6aSSam Leffler 		break;
30628a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
3063b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
306459aa14a9SRui Paulo 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3065b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_IBSS)
30668a1b9b6aSSam Leffler 			return EINVAL;
30678a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
30688a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
3069d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
30708a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
30718a1b9b6aSSam Leffler 		} else
30728a1b9b6aSSam Leffler 			error = EINVAL;
30738a1b9b6aSSam Leffler 		break;
3074c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
3075c4f040c3SSam Leffler 		if (ireq->i_val)
3076b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_PUREG;
3077c4f040c3SSam Leffler 		else
3078b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_PUREG;
3079c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
3080b032f27cSSam Leffler 		if (isvap11g(vap))
3081c4f040c3SSam Leffler 			error = ENETRESET;
3082c4f040c3SSam Leffler 		break;
308332b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET:
308432b0e64bSAdrian Chadd 		vap->iv_quiet= ireq->i_val;
308532b0e64bSAdrian Chadd 		break;
308632b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_COUNT:
308732b0e64bSAdrian Chadd 		vap->iv_quiet_count=ireq->i_val;
308832b0e64bSAdrian Chadd 		break;
308932b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_PERIOD:
309032b0e64bSAdrian Chadd 		vap->iv_quiet_period=ireq->i_val;
309132b0e64bSAdrian Chadd 		break;
309232b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_OFFSET:
309332b0e64bSAdrian Chadd 		vap->iv_quiet_offset=ireq->i_val;
309432b0e64bSAdrian Chadd 		break;
309532b0e64bSAdrian Chadd 	case IEEE80211_IOC_QUIET_DUR:
309632b0e64bSAdrian Chadd 		if(ireq->i_val < vap->iv_bss->ni_intval)
309732b0e64bSAdrian Chadd 			vap->iv_quiet_duration = ireq->i_val;
309832b0e64bSAdrian Chadd 		else
309932b0e64bSAdrian Chadd 			error = EINVAL;
310032b0e64bSAdrian Chadd 		break;
310168e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN:
310268e8e04eSSam Leffler 		if (ireq->i_val) {
3103b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3104b032f27cSSam Leffler 				return EOPNOTSUPP;
3105b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_BGSCAN;
310668e8e04eSSam Leffler 		} else
3107b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
310868e8e04eSSam Leffler 		break;
310968e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_IDLE:
311068e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3111b032f27cSSam Leffler 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
311268e8e04eSSam Leffler 		else
311368e8e04eSSam Leffler 			error = EINVAL;
311468e8e04eSSam Leffler 		break;
311568e8e04eSSam Leffler 	case IEEE80211_IOC_BGSCAN_INTERVAL:
311668e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3117b032f27cSSam Leffler 			vap->iv_bgscanintvl = ireq->i_val*hz;
311868e8e04eSSam Leffler 		else
311968e8e04eSSam Leffler 			error = EINVAL;
312068e8e04eSSam Leffler 		break;
312168e8e04eSSam Leffler 	case IEEE80211_IOC_SCANVALID:
312268e8e04eSSam Leffler 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3123b032f27cSSam Leffler 			vap->iv_scanvalid = ireq->i_val*hz;
312468e8e04eSSam Leffler 		else
312568e8e04eSSam Leffler 			error = EINVAL;
312668e8e04eSSam Leffler 		break;
312770231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
3128b032f27cSSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
312970231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
3130b032f27cSSam Leffler 			return EOPNOTSUPP;
313170231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
313270231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
313370231e3dSSam Leffler 			return EINVAL;
3134b032f27cSSam Leffler 		vap->iv_fragthreshold = ireq->i_val;
3135b032f27cSSam Leffler 		error = ERESTART;
313670231e3dSSam Leffler 		break;
3137c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
3138c27e4e31SSam Leffler 		if (ireq->i_val) {
3139b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3140b032f27cSSam Leffler 				return EOPNOTSUPP;
3141b032f27cSSam Leffler 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3142c27e4e31SSam Leffler 		} else
3143b032f27cSSam Leffler 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3144b032f27cSSam Leffler 		error = ERESTART;
3145c27e4e31SSam Leffler 		break;
3146546786c9SSam Leffler 	case IEEE80211_IOC_BMISSTHRESHOLD:
3147546786c9SSam Leffler 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3148546786c9SSam Leffler 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3149546786c9SSam Leffler 			return EINVAL;
3150b032f27cSSam Leffler 		vap->iv_bmissthreshold = ireq->i_val;
3151b032f27cSSam Leffler 		error = ERESTART;
3152546786c9SSam Leffler 		break;
315368e8e04eSSam Leffler 	case IEEE80211_IOC_CURCHAN:
3154b032f27cSSam Leffler 		error = ieee80211_ioctl_setcurchan(vap, ireq);
315568e8e04eSSam Leffler 		break;
315668e8e04eSSam Leffler 	case IEEE80211_IOC_SHORTGI:
315768e8e04eSSam Leffler 		if (ireq->i_val) {
315868e8e04eSSam Leffler #define	IEEE80211_HTCAP_SHORTGI \
315968e8e04eSSam Leffler 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3160b032f27cSSam Leffler 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
316168e8e04eSSam Leffler 				return EINVAL;
316268e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
31632bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
316468e8e04eSSam Leffler 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
31652bfc8a91SSam Leffler 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
316668e8e04eSSam Leffler #undef IEEE80211_HTCAP_SHORTGI
316768e8e04eSSam Leffler 		} else
31682bfc8a91SSam Leffler 			vap->iv_flags_ht &=
31692bfc8a91SSam Leffler 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3170b032f27cSSam Leffler 		error = ERESTART;
317168e8e04eSSam Leffler 		break;
317268e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU:
3173b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
317468e8e04eSSam Leffler 			return EINVAL;
317568e8e04eSSam Leffler 		if (ireq->i_val & 1)
31762bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3177b032f27cSSam Leffler 		else
31782bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
317968e8e04eSSam Leffler 		if (ireq->i_val & 2)
31802bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3181b032f27cSSam Leffler 		else
31822bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
318368e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3184b032f27cSSam Leffler 		if (isvapht(vap))
3185b032f27cSSam Leffler 			error = ERESTART;
318668e8e04eSSam Leffler 		break;
318768e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_LIMIT:
3188781487cfSAdrian Chadd 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
3189b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3190b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3191b032f27cSSam Leffler 			return EINVAL;
3192b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3193b032f27cSSam Leffler 			vap->iv_ampdu_rxmax = ireq->i_val;
3194b032f27cSSam Leffler 		else
3195b032f27cSSam Leffler 			vap->iv_ampdu_limit = ireq->i_val;
3196b032f27cSSam Leffler 		error = ERESTART;
319768e8e04eSSam Leffler 		break;
319868e8e04eSSam Leffler 	case IEEE80211_IOC_AMPDU_DENSITY:
3199b032f27cSSam Leffler 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3200b032f27cSSam Leffler 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3201b032f27cSSam Leffler 			return EINVAL;
3202b032f27cSSam Leffler 		vap->iv_ampdu_density = ireq->i_val;
3203b032f27cSSam Leffler 		error = ERESTART;
320468e8e04eSSam Leffler 		break;
320568e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU:
3206b032f27cSSam Leffler 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
320768e8e04eSSam Leffler 			return EINVAL;
320868e8e04eSSam Leffler 		if (ireq->i_val & 1)
32092bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3210b032f27cSSam Leffler 		else
32112bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
321268e8e04eSSam Leffler 		if (ireq->i_val & 2)
32132bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3214b032f27cSSam Leffler 		else
32152bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
321668e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3217b032f27cSSam Leffler 		if (isvapht(vap))
3218b032f27cSSam Leffler 			error = ERESTART;
321968e8e04eSSam Leffler 		break;
322068e8e04eSSam Leffler 	case IEEE80211_IOC_AMSDU_LIMIT:
322168e8e04eSSam Leffler 		/* XXX validate */
3222b032f27cSSam Leffler 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
322368e8e04eSSam Leffler 		break;
322468e8e04eSSam Leffler 	case IEEE80211_IOC_PUREN:
322568e8e04eSSam Leffler 		if (ireq->i_val) {
32262bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
322768e8e04eSSam Leffler 				return EINVAL;
32282bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
322968e8e04eSSam Leffler 		} else
32302bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
323168e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3232b032f27cSSam Leffler 		if (isvapht(vap))
3233b032f27cSSam Leffler 			error = ERESTART;
323468e8e04eSSam Leffler 		break;
323568e8e04eSSam Leffler 	case IEEE80211_IOC_DOTH:
323668e8e04eSSam Leffler 		if (ireq->i_val) {
323768e8e04eSSam Leffler #if 0
323868e8e04eSSam Leffler 			/* XXX no capability */
3239b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3240b032f27cSSam Leffler 				return EOPNOTSUPP;
324168e8e04eSSam Leffler #endif
3242b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DOTH;
324368e8e04eSSam Leffler 		} else
3244b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3245b032f27cSSam Leffler 		error = ENETRESET;
3246b032f27cSSam Leffler 		break;
3247b032f27cSSam Leffler 	case IEEE80211_IOC_REGDOMAIN:
3248b032f27cSSam Leffler 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3249b032f27cSSam Leffler 		break;
3250b032f27cSSam Leffler 	case IEEE80211_IOC_ROAM:
3251b032f27cSSam Leffler 		error = ieee80211_ioctl_setroam(vap, ireq);
3252b032f27cSSam Leffler 		break;
3253b032f27cSSam Leffler 	case IEEE80211_IOC_TXPARAMS:
3254b032f27cSSam Leffler 		error = ieee80211_ioctl_settxparams(vap, ireq);
325568e8e04eSSam Leffler 		break;
325668e8e04eSSam Leffler 	case IEEE80211_IOC_HTCOMPAT:
325768e8e04eSSam Leffler 		if (ireq->i_val) {
32582bfc8a91SSam Leffler 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3259b032f27cSSam Leffler 				return EOPNOTSUPP;
32602bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
326168e8e04eSSam Leffler 		} else
32622bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
326368e8e04eSSam Leffler 		/* NB: reset only if we're operating on an 11n channel */
3264b032f27cSSam Leffler 		if (isvapht(vap))
3265b032f27cSSam Leffler 			error = ERESTART;
3266b032f27cSSam Leffler 		break;
3267b032f27cSSam Leffler 	case IEEE80211_IOC_DWDS:
3268b032f27cSSam Leffler 		if (ireq->i_val) {
3269b032f27cSSam Leffler 			/* NB: DWDS only makes sense for WDS-capable devices */
3270b032f27cSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3271b032f27cSSam Leffler 				return EOPNOTSUPP;
3272b032f27cSSam Leffler 			/* NB: DWDS is used only with ap+sta vaps */
3273b032f27cSSam Leffler 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3274b032f27cSSam Leffler 			    vap->iv_opmode != IEEE80211_M_STA)
3275b032f27cSSam Leffler 				return EINVAL;
3276b032f27cSSam Leffler 			vap->iv_flags |= IEEE80211_F_DWDS;
3277f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3278f2a6a13cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3279f2a6a13cSSam Leffler 		} else {
3280b032f27cSSam Leffler 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3281f2a6a13cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_STA)
3282f2a6a13cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3283f2a6a13cSSam Leffler 		}
328468e8e04eSSam Leffler 		break;
3285c066143cSSam Leffler 	case IEEE80211_IOC_INACTIVITY:
3286c066143cSSam Leffler 		if (ireq->i_val)
3287b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3288c066143cSSam Leffler 		else
3289b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3290b032f27cSSam Leffler 		break;
3291b032f27cSSam Leffler 	case IEEE80211_IOC_APPIE:
3292b032f27cSSam Leffler 		error = ieee80211_ioctl_setappie(vap, ireq);
3293b032f27cSSam Leffler 		break;
3294b032f27cSSam Leffler 	case IEEE80211_IOC_WPS:
3295b032f27cSSam Leffler 		if (ireq->i_val) {
3296b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3297b032f27cSSam Leffler 				return EOPNOTSUPP;
3298b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3299b032f27cSSam Leffler 		} else
3300b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3301b032f27cSSam Leffler 		break;
3302b032f27cSSam Leffler 	case IEEE80211_IOC_TSN:
3303b032f27cSSam Leffler 		if (ireq->i_val) {
3304b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3305b032f27cSSam Leffler 				return EOPNOTSUPP;
3306b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3307b032f27cSSam Leffler 		} else
3308b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3309b032f27cSSam Leffler 		break;
3310b032f27cSSam Leffler 	case IEEE80211_IOC_CHANSWITCH:
3311b032f27cSSam Leffler 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3312b032f27cSSam Leffler 		break;
3313b032f27cSSam Leffler 	case IEEE80211_IOC_DFS:
3314b032f27cSSam Leffler 		if (ireq->i_val) {
3315b032f27cSSam Leffler 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3316b032f27cSSam Leffler 				return EOPNOTSUPP;
3317b032f27cSSam Leffler 			/* NB: DFS requires 11h support */
3318b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3319b032f27cSSam Leffler 				return EINVAL;
3320b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3321b032f27cSSam Leffler 		} else
3322b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3323b032f27cSSam Leffler 		break;
3324b032f27cSSam Leffler 	case IEEE80211_IOC_DOTD:
3325b032f27cSSam Leffler 		if (ireq->i_val)
3326b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3327b032f27cSSam Leffler 		else
3328b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3329b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
3330b032f27cSSam Leffler 			error = ENETRESET;
3331c066143cSSam Leffler 		break;
33321b6167d2SSam Leffler 	case IEEE80211_IOC_HTPROTMODE:
33331b6167d2SSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
33341b6167d2SSam Leffler 			return EINVAL;
33351b6167d2SSam Leffler 		ic->ic_htprotmode = ireq->i_val ?
33361b6167d2SSam Leffler 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
33371b6167d2SSam Leffler 		/* NB: if not operating in 11n this can wait */
3338b032f27cSSam Leffler 		if (isvapht(vap))
33391b6167d2SSam Leffler 			error = ERESTART;
33401b6167d2SSam Leffler 		break;
3341b032f27cSSam Leffler 	case IEEE80211_IOC_STA_VLAN:
3342b032f27cSSam Leffler 		error = ieee80211_ioctl_setstavlan(vap, ireq);
33431b6167d2SSam Leffler 		break;
33448c070d69SSam Leffler 	case IEEE80211_IOC_SMPS:
33458c070d69SSam Leffler 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
33468c070d69SSam Leffler 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
33478c070d69SSam Leffler 			return EINVAL;
33488c070d69SSam Leffler 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
33498c070d69SSam Leffler 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
33508c070d69SSam Leffler 			return EOPNOTSUPP;
33518c070d69SSam Leffler 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
33528c070d69SSam Leffler 			ireq->i_val;
33538c070d69SSam Leffler 		/* NB: if not operating in 11n this can wait */
33548c070d69SSam Leffler 		if (isvapht(vap))
33558c070d69SSam Leffler 			error = ERESTART;
33568c070d69SSam Leffler 		break;
335744f7a6edSSam Leffler 	case IEEE80211_IOC_RIFS:
335844f7a6edSSam Leffler 		if (ireq->i_val != 0) {
335944f7a6edSSam Leffler 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
336044f7a6edSSam Leffler 				return EOPNOTSUPP;
33612bfc8a91SSam Leffler 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
336244f7a6edSSam Leffler 		} else
33632bfc8a91SSam Leffler 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
336444f7a6edSSam Leffler 		/* NB: if not operating in 11n this can wait */
336544f7a6edSSam Leffler 		if (isvapht(vap))
336644f7a6edSSam Leffler 			error = ERESTART;
336744f7a6edSSam Leffler 		break;
3368ec3763bbSAdrian Chadd 	case IEEE80211_IOC_STBC:
3369ec3763bbSAdrian Chadd 		/* Check if we can do STBC TX/RX before changing the setting */
3370ec3763bbSAdrian Chadd 		if ((ireq->i_val & 1) &&
3371ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
3372ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3373ec3763bbSAdrian Chadd 		if ((ireq->i_val & 2) &&
3374ec3763bbSAdrian Chadd 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
3375ec3763bbSAdrian Chadd 			return EOPNOTSUPP;
3376ec3763bbSAdrian Chadd 
3377ec3763bbSAdrian Chadd 		/* TX */
3378ec3763bbSAdrian Chadd 		if (ireq->i_val & 1)
3379ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
3380ec3763bbSAdrian Chadd 		else
3381ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
3382ec3763bbSAdrian Chadd 
3383ec3763bbSAdrian Chadd 		/* RX */
3384ec3763bbSAdrian Chadd 		if (ireq->i_val & 2)
3385ec3763bbSAdrian Chadd 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
3386ec3763bbSAdrian Chadd 		else
3387ec3763bbSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
3388ec3763bbSAdrian Chadd 
3389ec3763bbSAdrian Chadd 		/* NB: reset only if we're operating on an 11n channel */
3390ec3763bbSAdrian Chadd 		if (isvapht(vap))
3391ec3763bbSAdrian Chadd 			error = ERESTART;
3392ec3763bbSAdrian Chadd 		break;
3393c5bba9daSAndriy Voskoboinyk 	case IEEE80211_IOC_LDPC:
3394c5bba9daSAndriy Voskoboinyk 		/* Check if we can do LDPC TX/RX before changing the setting */
3395c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 1) &&
3396c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0)
3397c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3398c5bba9daSAndriy Voskoboinyk 		if ((ireq->i_val & 2) &&
3399c5bba9daSAndriy Voskoboinyk 		    (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0)
3400c5bba9daSAndriy Voskoboinyk 			return EOPNOTSUPP;
3401c5bba9daSAndriy Voskoboinyk 
3402c5bba9daSAndriy Voskoboinyk 		/* TX */
3403c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 1)
3404c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
3405c5bba9daSAndriy Voskoboinyk 		else
3406c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX;
3407c5bba9daSAndriy Voskoboinyk 
3408c5bba9daSAndriy Voskoboinyk 		/* RX */
3409c5bba9daSAndriy Voskoboinyk 		if (ireq->i_val & 2)
3410c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
3411c5bba9daSAndriy Voskoboinyk 		else
3412c5bba9daSAndriy Voskoboinyk 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX;
3413c5bba9daSAndriy Voskoboinyk 
3414c5bba9daSAndriy Voskoboinyk 		/* NB: reset only if we're operating on an 11n channel */
3415c5bba9daSAndriy Voskoboinyk 		if (isvapht(vap))
3416c5bba9daSAndriy Voskoboinyk 			error = ERESTART;
3417c5bba9daSAndriy Voskoboinyk 		break;
341835bcfd1cSAdrian Chadd 
341935bcfd1cSAdrian Chadd 	/* VHT */
342035bcfd1cSAdrian Chadd 	case IEEE80211_IOC_VHTCONF:
342135bcfd1cSAdrian Chadd 		if (ireq->i_val & 1)
342235bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT);
342335bcfd1cSAdrian Chadd 		else
342435bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT);
342535bcfd1cSAdrian Chadd 
342635bcfd1cSAdrian Chadd 		if (ireq->i_val & 2)
342735bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40);
342835bcfd1cSAdrian Chadd 		else
342935bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40);
343035bcfd1cSAdrian Chadd 
343135bcfd1cSAdrian Chadd 		if (ireq->i_val & 4)
343235bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80);
343335bcfd1cSAdrian Chadd 		else
343435bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80);
343535bcfd1cSAdrian Chadd 
343635bcfd1cSAdrian Chadd 		if (ireq->i_val & 8)
343735bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80);
343835bcfd1cSAdrian Chadd 		else
343935bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80);
344035bcfd1cSAdrian Chadd 
344135bcfd1cSAdrian Chadd 		if (ireq->i_val & 16)
344235bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160);
344335bcfd1cSAdrian Chadd 		else
344435bcfd1cSAdrian Chadd 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160);
344535bcfd1cSAdrian Chadd 
344635bcfd1cSAdrian Chadd 		error = ENETRESET;
344735bcfd1cSAdrian Chadd 		break;
344835bcfd1cSAdrian Chadd 
34491a1e1d21SSam Leffler 	default:
34508c4e758aSSam Leffler 		error = ieee80211_ioctl_setdefault(vap, ireq);
34511a1e1d21SSam Leffler 		break;
34521a1e1d21SSam Leffler 	}
3453b032f27cSSam Leffler 	/*
3454b032f27cSSam Leffler 	 * The convention is that ENETRESET means an operation
3455b032f27cSSam Leffler 	 * requires a complete re-initialization of the device (e.g.
3456b032f27cSSam Leffler 	 * changing something that affects the association state).
3457b032f27cSSam Leffler 	 * ERESTART means the request may be handled with only a
3458b032f27cSSam Leffler 	 * reload of the hardware state.  We hand ERESTART requests
3459b032f27cSSam Leffler 	 * to the iv_reset callback so the driver can decide.  If
3460b032f27cSSam Leffler 	 * a device does not fillin iv_reset then it defaults to one
3461b032f27cSSam Leffler 	 * that returns ENETRESET.  Otherwise a driver may return
3462b032f27cSSam Leffler 	 * ENETRESET (in which case a full reset will be done) or
3463b032f27cSSam Leffler 	 * 0 to mean there's no need to do anything (e.g. when the
3464b032f27cSSam Leffler 	 * change has no effect on the driver/device).
3465b032f27cSSam Leffler 	 */
3466b032f27cSSam Leffler 	if (error == ERESTART)
3467b032f27cSSam Leffler 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3468b032f27cSSam Leffler 		    vap->iv_reset(vap, ireq->i_type) : 0;
3469b032f27cSSam Leffler 	if (error == ENETRESET) {
3470b032f27cSSam Leffler 		/* XXX need to re-think AUTO handling */
3471b032f27cSSam Leffler 		if (IS_UP_AUTO(vap))
3472b032f27cSSam Leffler 			ieee80211_init(vap);
3473b032f27cSSam Leffler 		error = 0;
3474b032f27cSSam Leffler 	}
34758a1b9b6aSSam Leffler 	return error;
34768a1b9b6aSSam Leffler }
34778a1b9b6aSSam Leffler 
3478b032f27cSSam Leffler int
3479b032f27cSSam Leffler ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3480b032f27cSSam Leffler {
34811da89db5SSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
34821da89db5SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
348360d75eb6SAndriy Voskoboinyk 	int error = 0, wait = 0;
34848a1b9b6aSSam Leffler 	struct ifreq *ifr;
34858a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
34868a1b9b6aSSam Leffler 
34878a1b9b6aSSam Leffler 	switch (cmd) {
3488b032f27cSSam Leffler 	case SIOCSIFFLAGS:
3489b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
34906459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
34916459bd28SAndriy Voskoboinyk 			/*
34926459bd28SAndriy Voskoboinyk 			 * Enable promiscuous mode when:
34936459bd28SAndriy Voskoboinyk 			 * 1. Interface is not a member of bridge, or
34946459bd28SAndriy Voskoboinyk 			 * 2. Requested by user, or
34956459bd28SAndriy Voskoboinyk 			 * 3. In monitor (or adhoc-demo) mode.
34966459bd28SAndriy Voskoboinyk 			 */
34976459bd28SAndriy Voskoboinyk 			if (ifp->if_bridge == NULL ||
34986459bd28SAndriy Voskoboinyk 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
34996459bd28SAndriy Voskoboinyk 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
35006459bd28SAndriy Voskoboinyk 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
35016459bd28SAndriy Voskoboinyk 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
35026459bd28SAndriy Voskoboinyk 				ieee80211_promisc(vap,
35036459bd28SAndriy Voskoboinyk 				    ifp->if_flags & IFF_PROMISC);
35046459bd28SAndriy Voskoboinyk 				vap->iv_ifflags ^= IFF_PROMISC;
35056459bd28SAndriy Voskoboinyk 			}
35066459bd28SAndriy Voskoboinyk 		}
35076459bd28SAndriy Voskoboinyk 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
35087a79cebfSGleb Smirnoff 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
35096459bd28SAndriy Voskoboinyk 			vap->iv_ifflags ^= IFF_ALLMULTI;
35106459bd28SAndriy Voskoboinyk 		}
3511b032f27cSSam Leffler 		if (ifp->if_flags & IFF_UP) {
3512b032f27cSSam Leffler 			/*
3513b032f27cSSam Leffler 			 * Bring ourself up unless we're already operational.
3514b032f27cSSam Leffler 			 * If we're the first vap and the parent is not up
3515b032f27cSSam Leffler 			 * then it will automatically be brought up as a
3516b032f27cSSam Leffler 			 * side-effect of bringing ourself up.
3517b032f27cSSam Leffler 			 */
351860d75eb6SAndriy Voskoboinyk 			if (vap->iv_state == IEEE80211_S_INIT) {
351960d75eb6SAndriy Voskoboinyk 				if (ic->ic_nrunning == 0)
352060d75eb6SAndriy Voskoboinyk 					wait = 1;
3521b032f27cSSam Leffler 				ieee80211_start_locked(vap);
352260d75eb6SAndriy Voskoboinyk 			}
3523b032f27cSSam Leffler 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3524b032f27cSSam Leffler 			/*
3525b032f27cSSam Leffler 			 * Stop ourself.  If we are the last vap to be
3526b032f27cSSam Leffler 			 * marked down the parent will also be taken down.
3527b032f27cSSam Leffler 			 */
352860d75eb6SAndriy Voskoboinyk 			if (ic->ic_nrunning == 1)
352960d75eb6SAndriy Voskoboinyk 				wait = 1;
3530b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
3531b032f27cSSam Leffler 		}
3532b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
35338d9b29f3SAndrew Thompson 		/* Wait for parent ioctl handler if it was queued */
3534c6a35ee4SAndriy Voskoboinyk 		if (wait) {
3535ae55932eSAndrew Thompson 			ieee80211_waitfor_parent(ic);
3536c6a35ee4SAndriy Voskoboinyk 
3537c6a35ee4SAndriy Voskoboinyk 			/*
3538c6a35ee4SAndriy Voskoboinyk 			 * Check if the MAC address was changed
3539c6a35ee4SAndriy Voskoboinyk 			 * via SIOCSIFLLADDR ioctl.
3540c6a35ee4SAndriy Voskoboinyk 			 */
3541c7ee5294SAdrian Chadd 			if_addr_rlock(ifp);
3542c6a35ee4SAndriy Voskoboinyk 			if ((ifp->if_flags & IFF_UP) == 0 &&
3543c6a35ee4SAndriy Voskoboinyk 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
3544c6a35ee4SAndriy Voskoboinyk 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
3545c6a35ee4SAndriy Voskoboinyk 				    IF_LLADDR(ifp));
3546c7ee5294SAdrian Chadd 			if_addr_runlock(ifp);
3547c6a35ee4SAndriy Voskoboinyk 		}
3548b032f27cSSam Leffler 		break;
3549b032f27cSSam Leffler 	case SIOCADDMULTI:
3550b032f27cSSam Leffler 	case SIOCDELMULTI:
35517a79cebfSGleb Smirnoff 		ieee80211_runtask(ic, &ic->ic_mcast_task);
3552b032f27cSSam Leffler 		break;
35538a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
35548a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
3555b032f27cSSam Leffler 		ifr = (struct ifreq *)data;
3556b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
35578a1b9b6aSSam Leffler 		break;
35588a1b9b6aSSam Leffler 	case SIOCG80211:
3559b032f27cSSam Leffler 		error = ieee80211_ioctl_get80211(vap, cmd,
35608a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
35618a1b9b6aSSam Leffler 		break;
35628a1b9b6aSSam Leffler 	case SIOCS80211:
3563acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
35648a1b9b6aSSam Leffler 		if (error == 0)
3565b032f27cSSam Leffler 			error = ieee80211_ioctl_set80211(vap, cmd,
35668a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
35671a1e1d21SSam Leffler 		break;
35681be50176SSam Leffler 	case SIOCG80211STATS:
35691be50176SSam Leffler 		ifr = (struct ifreq *)data;
3570*541d96aaSBrooks Davis 		copyout(&vap->iv_stats, ifr_data_get_ptr(ifr),
3571*541d96aaSBrooks Davis 		    sizeof (vap->iv_stats));
35721be50176SSam Leffler 		break;
35736f161f03SSam Leffler 	case SIOCSIFMTU:
35746f161f03SSam Leffler 		ifr = (struct ifreq *)data;
35756f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
35766f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
35776f161f03SSam Leffler 			error = EINVAL;
35786f161f03SSam Leffler 		else
35796f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
35806f161f03SSam Leffler 		break;
3581b2e95691SSam Leffler 	case SIOCSIFADDR:
3582b2e95691SSam Leffler 		/*
3583a4641f4eSPedro F. Giffuni 		 * XXX Handle this directly so we can suppress if_init calls.
3584b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
3585b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
3586a4641f4eSPedro F. Giffuni 		 * XXX set IFF_UP and so suppress if_init being called when
3587b2e95691SSam Leffler 		 * XXX it should be.
3588b2e95691SSam Leffler 		 */
3589b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
3590b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
3591b2e95691SSam Leffler #ifdef INET
3592b2e95691SSam Leffler 		case AF_INET:
3593b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3594b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3595b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3596b2e95691SSam Leffler 			}
3597b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
3598b2e95691SSam Leffler 			break;
3599b2e95691SSam Leffler #endif
3600b2e95691SSam Leffler 		default:
3601b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
3602b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
3603b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
3604b2e95691SSam Leffler 			}
3605b2e95691SSam Leffler 			break;
3606b2e95691SSam Leffler 		}
3607b2e95691SSam Leffler 		break;
3608ba2c1fbcSAdrian Chadd 	default:
36097a79cebfSGleb Smirnoff 		/*
36107a79cebfSGleb Smirnoff 		 * Pass unknown ioctls first to the driver, and if it
36117a79cebfSGleb Smirnoff 		 * returns ENOTTY, then to the generic Ethernet handler.
36127a79cebfSGleb Smirnoff 		 */
36137a79cebfSGleb Smirnoff 		if (ic->ic_ioctl != NULL &&
36147a79cebfSGleb Smirnoff 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
36157a79cebfSGleb Smirnoff 			break;
36161a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
36171a1e1d21SSam Leffler 		break;
36181a1e1d21SSam Leffler 	}
36197a79cebfSGleb Smirnoff 	return (error);
36201a1e1d21SSam Leffler }
3621