ieee80211_ioctl.c revision 187678
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ioctl.c 187678 2009-01-25 07:31:51Z thompsa $");
29116742Ssam
30116742Ssam/*
31116742Ssam * IEEE 802.11 ioctl support (FreeBSD-specific)
32116742Ssam */
33116742Ssam
34127646Ssam#include "opt_inet.h"
35127646Ssam#include "opt_ipx.h"
36178354Ssam#include "opt_wlan.h"
37127646Ssam
38116742Ssam#include <sys/endian.h>
39116742Ssam#include <sys/param.h>
40116742Ssam#include <sys/kernel.h>
41164033Srwatson#include <sys/priv.h>
42116742Ssam#include <sys/socket.h>
43116742Ssam#include <sys/sockio.h>
44116742Ssam#include <sys/systm.h>
45187678Sthompsa#include <sys/taskqueue.h>
46116742Ssam
47116742Ssam#include <net/if.h>
48152315Sru#include <net/if_dl.h>
49116742Ssam#include <net/if_media.h>
50116742Ssam#include <net/ethernet.h>
51116742Ssam
52127646Ssam#ifdef INET
53127646Ssam#include <netinet/in.h>
54127646Ssam#include <netinet/if_ether.h>
55127646Ssam#endif
56127646Ssam
57127646Ssam#ifdef IPX
58127646Ssam#include <netipx/ipx.h>
59127646Ssam#include <netipx/ipx_if.h>
60127646Ssam#endif
61127646Ssam
62116742Ssam#include <net80211/ieee80211_var.h>
63116742Ssam#include <net80211/ieee80211_ioctl.h>
64178354Ssam#include <net80211/ieee80211_regdomain.h>
65178354Ssam#include <net80211/ieee80211_input.h>
66186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
67186904Ssam#include <net80211/ieee80211_tdma.h>
68186904Ssam#endif
69116742Ssam
70178354Ssam#define	IS_UP_AUTO(_vap) \
71178354Ssam	(IFNET_IS_UP_RUNNING(vap->iv_ifp) && \
72178354Ssam	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
73138568Ssam
74178354Ssamstatic const uint8_t zerobssid[IEEE80211_ADDR_LEN];
75170530Ssamstatic struct ieee80211_channel *findchannel(struct ieee80211com *,
76170530Ssam		int ieee, int mode);
77116742Ssam
78178354Ssamstatic __noinline int
79178354Ssamieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
80138568Ssam{
81178354Ssam	struct ieee80211com *ic = vap->iv_ic;
82138568Ssam	struct ieee80211_node *ni;
83138568Ssam	struct ieee80211req_key ik;
84138568Ssam	struct ieee80211_key *wk;
85138568Ssam	const struct ieee80211_cipher *cip;
86138568Ssam	u_int kid;
87138568Ssam	int error;
88138568Ssam
89138568Ssam	if (ireq->i_len != sizeof(ik))
90138568Ssam		return EINVAL;
91138568Ssam	error = copyin(ireq->i_data, &ik, sizeof(ik));
92138568Ssam	if (error)
93138568Ssam		return error;
94138568Ssam	kid = ik.ik_keyix;
95138568Ssam	if (kid == IEEE80211_KEYIX_NONE) {
96178354Ssam		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
97138568Ssam		if (ni == NULL)
98178354Ssam			return ENOENT;
99138568Ssam		wk = &ni->ni_ucastkey;
100138568Ssam	} else {
101138568Ssam		if (kid >= IEEE80211_WEP_NKID)
102138568Ssam			return EINVAL;
103178354Ssam		wk = &vap->iv_nw_keys[kid];
104178354Ssam		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
105138568Ssam		ni = NULL;
106138568Ssam	}
107138568Ssam	cip = wk->wk_cipher;
108138568Ssam	ik.ik_type = cip->ic_cipher;
109138568Ssam	ik.ik_keylen = wk->wk_keylen;
110138568Ssam	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
111178354Ssam	if (wk->wk_keyix == vap->iv_def_txkey)
112138568Ssam		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
113164033Srwatson	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
114138568Ssam		/* NB: only root can read key data */
115178354Ssam		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
116138568Ssam		ik.ik_keytsc = wk->wk_keytsc;
117138568Ssam		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
118138568Ssam		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
119138568Ssam			memcpy(ik.ik_keydata+wk->wk_keylen,
120138568Ssam				wk->wk_key + IEEE80211_KEYBUF_SIZE,
121138568Ssam				IEEE80211_MICBUF_SIZE);
122138568Ssam			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
123138568Ssam		}
124138568Ssam	} else {
125138568Ssam		ik.ik_keyrsc = 0;
126138568Ssam		ik.ik_keytsc = 0;
127138568Ssam		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
128138568Ssam	}
129138568Ssam	if (ni != NULL)
130138568Ssam		ieee80211_free_node(ni);
131138568Ssam	return copyout(&ik, ireq->i_data, sizeof(ik));
132138568Ssam}
133138568Ssam
134178354Ssamstatic __noinline int
135178354Ssamieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
136138568Ssam{
137178354Ssam	struct ieee80211com *ic = vap->iv_ic;
138138568Ssam
139153344Ssam	if (sizeof(ic->ic_chan_active) < ireq->i_len)
140138568Ssam		ireq->i_len = sizeof(ic->ic_chan_active);
141138568Ssam	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
142138568Ssam}
143138568Ssam
144178354Ssamstatic __noinline int
145178354Ssamieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
146138568Ssam{
147178354Ssam	struct ieee80211com *ic = vap->iv_ic;
148170530Ssam	int space;
149138568Ssam
150138568Ssam	space = __offsetof(struct ieee80211req_chaninfo,
151170530Ssam			ic_chans[ic->ic_nchans]);
152138568Ssam	if (space > ireq->i_len)
153138568Ssam		space = ireq->i_len;
154170530Ssam	/* XXX assumes compatible layout */
155170530Ssam	return copyout(&ic->ic_nchans, ireq->i_data, space);
156138568Ssam}
157138568Ssam
158178354Ssamstatic __noinline int
159178354Ssamieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
160178354Ssam	struct ieee80211req *ireq, int req)
161138568Ssam{
162138568Ssam	struct ieee80211_node *ni;
163170530Ssam	struct ieee80211req_wpaie2 wpaie;
164138568Ssam	int error;
165138568Ssam
166138568Ssam	if (ireq->i_len < IEEE80211_ADDR_LEN)
167138568Ssam		return EINVAL;
168138568Ssam	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
169138568Ssam	if (error != 0)
170138568Ssam		return error;
171178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie.wpa_macaddr);
172138568Ssam	if (ni == NULL)
173178354Ssam		return ENOENT;
174138568Ssam	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
175178354Ssam	if (ni->ni_ies.wpa_ie != NULL) {
176178354Ssam		int ielen = ni->ni_ies.wpa_ie[1] + 2;
177138568Ssam		if (ielen > sizeof(wpaie.wpa_ie))
178138568Ssam			ielen = sizeof(wpaie.wpa_ie);
179178354Ssam		memcpy(wpaie.wpa_ie, ni->ni_ies.wpa_ie, ielen);
180138568Ssam	}
181170530Ssam	if (req == IEEE80211_IOC_WPAIE2) {
182170530Ssam		memset(wpaie.rsn_ie, 0, sizeof(wpaie.rsn_ie));
183178354Ssam		if (ni->ni_ies.rsn_ie != NULL) {
184178354Ssam			int ielen = ni->ni_ies.rsn_ie[1] + 2;
185170530Ssam			if (ielen > sizeof(wpaie.rsn_ie))
186170530Ssam				ielen = sizeof(wpaie.rsn_ie);
187178354Ssam			memcpy(wpaie.rsn_ie, ni->ni_ies.rsn_ie, ielen);
188170530Ssam		}
189170530Ssam		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
190170530Ssam			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
191170530Ssam	} else {
192170530Ssam		/* compatibility op, may overwrite wpa ie */
193170530Ssam		/* XXX check ic_flags? */
194178354Ssam		if (ni->ni_ies.rsn_ie != NULL) {
195178354Ssam			int ielen = ni->ni_ies.rsn_ie[1] + 2;
196170530Ssam			if (ielen > sizeof(wpaie.wpa_ie))
197170530Ssam				ielen = sizeof(wpaie.wpa_ie);
198178354Ssam			memcpy(wpaie.wpa_ie, ni->ni_ies.rsn_ie, ielen);
199170530Ssam		}
200170530Ssam		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
201170530Ssam			ireq->i_len = sizeof(struct ieee80211req_wpaie);
202170530Ssam	}
203138568Ssam	ieee80211_free_node(ni);
204138568Ssam	return copyout(&wpaie, ireq->i_data, ireq->i_len);
205138568Ssam}
206138568Ssam
207178354Ssamstatic __noinline int
208178354Ssamieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
209138568Ssam{
210138568Ssam	struct ieee80211_node *ni;
211170530Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
212138568Ssam	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
213138568Ssam	int error;
214138568Ssam
215138568Ssam	if (ireq->i_len < off)
216138568Ssam		return EINVAL;
217138568Ssam	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
218138568Ssam	if (error != 0)
219138568Ssam		return error;
220178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
221170530Ssam	if (ni == NULL)
222178354Ssam		return ENOENT;
223138568Ssam	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
224138568Ssam		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
225138568Ssam	/* NB: copy out only the statistics */
226170530Ssam	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
227138568Ssam			ireq->i_len - off);
228138568Ssam	ieee80211_free_node(ni);
229138568Ssam	return error;
230138568Ssam}
231138568Ssam
232170530Ssamstruct scanreq {
233154541Ssam	struct ieee80211req_scan_result *sr;
234170530Ssam	size_t space;
235154541Ssam};
236154541Ssam
237154541Ssamstatic size_t
238170530Ssamscan_space(const struct ieee80211_scan_entry *se, int *ielen)
239154541Ssam{
240154541Ssam	size_t len;
241154541Ssam
242178354Ssam	*ielen = se->se_ies.len;
243154541Ssam	/*
244154541Ssam	 * NB: ie's can be no more than 255 bytes and the max 802.11
245154541Ssam	 * packet is <3Kbytes so we are sure this doesn't overflow
246154541Ssam	 * 16-bits; if this is a concern we can drop the ie's.
247154541Ssam	 */
248170530Ssam	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] + *ielen;
249170530Ssam	return roundup(len, sizeof(uint32_t));
250154541Ssam}
251154541Ssam
252154541Ssamstatic void
253170530Ssamget_scan_space(void *arg, const struct ieee80211_scan_entry *se)
254154541Ssam{
255170530Ssam	struct scanreq *req = arg;
256170530Ssam	int ielen;
257154541Ssam
258170530Ssam	req->space += scan_space(se, &ielen);
259154541Ssam}
260154541Ssam
261178354Ssamstatic __noinline void
262170530Ssamget_scan_result(void *arg, const struct ieee80211_scan_entry *se)
263154541Ssam{
264170530Ssam	struct scanreq *req = arg;
265154541Ssam	struct ieee80211req_scan_result *sr;
266170530Ssam	int ielen, len, nr, nxr;
267170530Ssam	uint8_t *cp;
268154541Ssam
269170530Ssam	len = scan_space(se, &ielen);
270154541Ssam	if (len > req->space)
271154541Ssam		return;
272170530Ssam
273154541Ssam	sr = req->sr;
274154541Ssam	KASSERT(len <= 65535 && ielen <= 65535,
275170530Ssam	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
276178354Ssam	sr->isr_len = len;
277170530Ssam	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
278170530Ssam	sr->isr_ie_len = ielen;
279170530Ssam	sr->isr_freq = se->se_chan->ic_freq;
280170530Ssam	sr->isr_flags = se->se_chan->ic_flags;
281170530Ssam	sr->isr_rssi = se->se_rssi;
282170530Ssam	sr->isr_noise = se->se_noise;
283170530Ssam	sr->isr_intval = se->se_intval;
284170530Ssam	sr->isr_capinfo = se->se_capinfo;
285170530Ssam	sr->isr_erp = se->se_erp;
286170530Ssam	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
287170530Ssam	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
288170530Ssam	memcpy(sr->isr_rates, se->se_rates+2, nr);
289170530Ssam	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
290170530Ssam	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
291170530Ssam	sr->isr_nrates = nr + nxr;
292170530Ssam
293170530Ssam	sr->isr_ssid_len = se->se_ssid[1];
294170530Ssam	cp = ((uint8_t *)sr) + sr->isr_ie_off;
295170530Ssam	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
296170530Ssam
297170530Ssam	if (ielen) {
298170530Ssam		cp += sr->isr_ssid_len;
299178354Ssam		memcpy(cp, se->se_ies.data, ielen);
300154541Ssam	}
301154541Ssam
302154541Ssam	req->space -= len;
303170530Ssam	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
304154541Ssam}
305154541Ssam
306178354Ssamstatic __noinline int
307178354Ssamieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
308178354Ssam	struct ieee80211req *ireq)
309154541Ssam{
310170530Ssam	struct scanreq req;
311154541Ssam	int error;
312154541Ssam
313178354Ssam	if (ireq->i_len < sizeof(struct scanreq))
314154541Ssam		return EFAULT;
315154541Ssam
316154541Ssam	error = 0;
317154541Ssam	req.space = 0;
318178354Ssam	ieee80211_scan_iterate(vap, get_scan_space, &req);
319154541Ssam	if (req.space > ireq->i_len)
320154541Ssam		req.space = ireq->i_len;
321154541Ssam	if (req.space > 0) {
322154541Ssam		size_t space;
323154541Ssam		void *p;
324154541Ssam
325154541Ssam		space = req.space;
326154541Ssam		/* XXX M_WAITOK after driver lock released */
327186302Ssam		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
328154541Ssam		if (p == NULL)
329154541Ssam			return ENOMEM;
330154541Ssam		req.sr = p;
331178354Ssam		ieee80211_scan_iterate(vap, get_scan_result, &req);
332154541Ssam		ireq->i_len = space - req.space;
333154541Ssam		error = copyout(p, ireq->i_data, ireq->i_len);
334186302Ssam		free(p, M_TEMP);
335154541Ssam	} else
336154541Ssam		ireq->i_len = 0;
337154541Ssam
338154541Ssam	return error;
339154541Ssam}
340154541Ssam
341148845Ssamstruct stainforeq {
342178354Ssam	struct ieee80211vap *vap;
343148845Ssam	struct ieee80211req_sta_info *si;
344148845Ssam	size_t	space;
345148845Ssam};
346148845Ssam
347148845Ssamstatic size_t
348148845Ssamsta_space(const struct ieee80211_node *ni, size_t *ielen)
349148845Ssam{
350178354Ssam	*ielen = ni->ni_ies.len;
351148845Ssam	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
352170530Ssam		      sizeof(uint32_t));
353148845Ssam}
354148845Ssam
355138568Ssamstatic void
356148845Ssamget_sta_space(void *arg, struct ieee80211_node *ni)
357138568Ssam{
358148845Ssam	struct stainforeq *req = arg;
359148845Ssam	size_t ielen;
360138568Ssam
361178354Ssam	if (req->vap != ni->ni_vap)
362178354Ssam		return;
363178354Ssam	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
364148845Ssam	    ni->ni_associd == 0)	/* only associated stations */
365148845Ssam		return;
366148845Ssam	req->space += sta_space(ni, &ielen);
367148845Ssam}
368148845Ssam
369178354Ssamstatic __noinline void
370148845Ssamget_sta_info(void *arg, struct ieee80211_node *ni)
371148845Ssam{
372148845Ssam	struct stainforeq *req = arg;
373178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
374148845Ssam	struct ieee80211req_sta_info *si;
375148845Ssam	size_t ielen, len;
376170530Ssam	uint8_t *cp;
377148845Ssam
378178354Ssam	if (req->vap != ni->ni_vap)
379178354Ssam		return;
380178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
381148845Ssam	    ni->ni_associd == 0)	/* only associated stations */
382148845Ssam		return;
383148845Ssam	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
384148845Ssam		return;
385148845Ssam	len = sta_space(ni, &ielen);
386148845Ssam	if (len > req->space)
387148845Ssam		return;
388148845Ssam	si = req->si;
389148845Ssam	si->isi_len = len;
390170530Ssam	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
391148845Ssam	si->isi_ie_len = ielen;
392138568Ssam	si->isi_freq = ni->ni_chan->ic_freq;
393138568Ssam	si->isi_flags = ni->ni_chan->ic_flags;
394138568Ssam	si->isi_state = ni->ni_flags;
395138568Ssam	si->isi_authmode = ni->ni_authmode;
396178354Ssam	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
397178354Ssam	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
398138568Ssam	si->isi_capinfo = ni->ni_capinfo;
399138568Ssam	si->isi_erp = ni->ni_erp;
400138568Ssam	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
401138568Ssam	si->isi_nrates = ni->ni_rates.rs_nrates;
402138568Ssam	if (si->isi_nrates > 15)
403138568Ssam		si->isi_nrates = 15;
404138568Ssam	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
405138568Ssam	si->isi_txrate = ni->ni_txrate;
406178354Ssam	if (si->isi_txrate & IEEE80211_RATE_MCS) {
407178354Ssam		const struct ieee80211_mcs_rates *mcs =
408178354Ssam		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
409178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
410178354Ssam			if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40)
411178354Ssam				si->isi_txmbps = mcs->ht40_rate_800ns;
412178354Ssam			else
413178354Ssam				si->isi_txmbps = mcs->ht40_rate_400ns;
414178354Ssam		} else {
415178354Ssam			if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20)
416178354Ssam				si->isi_txmbps = mcs->ht20_rate_800ns;
417178354Ssam			else
418178354Ssam				si->isi_txmbps = mcs->ht20_rate_400ns;
419178354Ssam		}
420178354Ssam	} else
421178354Ssam		si->isi_txmbps = si->isi_txrate;
422138568Ssam	si->isi_associd = ni->ni_associd;
423138568Ssam	si->isi_txpower = ni->ni_txpower;
424138568Ssam	si->isi_vlan = ni->ni_vlan;
425138568Ssam	if (ni->ni_flags & IEEE80211_NODE_QOS) {
426138568Ssam		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
427138568Ssam		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
428138568Ssam	} else {
429167439Ssam		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
430167439Ssam		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
431138568Ssam	}
432148845Ssam	/* NB: leave all cases in case we relax ni_associd == 0 check */
433148845Ssam	if (ieee80211_node_is_authorized(ni))
434178354Ssam		si->isi_inact = vap->iv_inact_run;
435178354Ssam	else if (ni->ni_associd != 0 ||
436178354Ssam	    (vap->iv_opmode == IEEE80211_M_WDS &&
437178354Ssam	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
438178354Ssam		si->isi_inact = vap->iv_inact_auth;
439138568Ssam	else
440178354Ssam		si->isi_inact = vap->iv_inact_init;
441138568Ssam	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
442148845Ssam
443170530Ssam	if (ielen) {
444170530Ssam		cp = ((uint8_t *)si) + si->isi_ie_off;
445178354Ssam		memcpy(cp, ni->ni_ies.data, ielen);
446148845Ssam	}
447148845Ssam
448170530Ssam	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
449148845Ssam	req->space -= len;
450138568Ssam}
451138568Ssam
452178354Ssamstatic __noinline int
453178354Ssamgetstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
454161146Ssam	struct ieee80211_node *ni, int off)
455138568Ssam{
456178354Ssam	struct ieee80211com *ic = vap->iv_ic;
457148845Ssam	struct stainforeq req;
458161146Ssam	size_t space;
459161146Ssam	void *p;
460148845Ssam	int error;
461138568Ssam
462138568Ssam	error = 0;
463148845Ssam	req.space = 0;
464178354Ssam	req.vap = vap;
465161146Ssam	if (ni == NULL)
466161146Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
467161146Ssam	else
468161146Ssam		get_sta_space(&req, ni);
469148845Ssam	if (req.space > ireq->i_len)
470148845Ssam		req.space = ireq->i_len;
471148845Ssam	if (req.space > 0) {
472148845Ssam		space = req.space;
473148845Ssam		/* XXX M_WAITOK after driver lock released */
474186302Ssam		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
475161146Ssam		if (p == NULL) {
476161146Ssam			error = ENOMEM;
477161146Ssam			goto bad;
478161146Ssam		}
479148845Ssam		req.si = p;
480161146Ssam		if (ni == NULL)
481161146Ssam			ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
482161146Ssam		else
483161146Ssam			get_sta_info(&req, ni);
484148845Ssam		ireq->i_len = space - req.space;
485170530Ssam		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
486186302Ssam		free(p, M_TEMP);
487148845Ssam	} else
488148845Ssam		ireq->i_len = 0;
489161146Ssambad:
490161146Ssam	if (ni != NULL)
491161146Ssam		ieee80211_free_node(ni);
492138568Ssam	return error;
493138568Ssam}
494138568Ssam
495178354Ssamstatic __noinline int
496178354Ssamieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
497161146Ssam{
498170530Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
499161146Ssam	const int off = __offsetof(struct ieee80211req_sta_req, info);
500161146Ssam	struct ieee80211_node *ni;
501161146Ssam	int error;
502161146Ssam
503161146Ssam	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
504161146Ssam		return EFAULT;
505161146Ssam	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
506161146Ssam	if (error != 0)
507161146Ssam		return error;
508178354Ssam	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
509161146Ssam		ni = NULL;
510161146Ssam	} else {
511178354Ssam		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
512170530Ssam		if (ni == NULL)
513178354Ssam			return ENOENT;
514161146Ssam	}
515178354Ssam	return getstainfo_common(vap, ireq, ni, off);
516161146Ssam}
517161146Ssam
518178354Ssamstatic __noinline int
519178354Ssamieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
520161146Ssam{
521138568Ssam	struct ieee80211_node *ni;
522138568Ssam	struct ieee80211req_sta_txpow txpow;
523138568Ssam	int error;
524138568Ssam
525138568Ssam	if (ireq->i_len != sizeof(txpow))
526138568Ssam		return EINVAL;
527138568Ssam	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
528138568Ssam	if (error != 0)
529138568Ssam		return error;
530178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
531138568Ssam	if (ni == NULL)
532178354Ssam		return ENOENT;
533138568Ssam	txpow.it_txpow = ni->ni_txpower;
534138568Ssam	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
535138568Ssam	ieee80211_free_node(ni);
536138568Ssam	return error;
537138568Ssam}
538138568Ssam
539178354Ssamstatic __noinline int
540178354Ssamieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
541138568Ssam{
542178354Ssam	struct ieee80211com *ic = vap->iv_ic;
543138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
544138568Ssam	struct wmeParams *wmep;
545138568Ssam	int ac;
546138568Ssam
547138568Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
548138568Ssam		return EINVAL;
549138568Ssam
550138568Ssam	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
551138568Ssam	if (ac >= WME_NUM_AC)
552138568Ssam		ac = WME_AC_BE;
553138568Ssam	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
554138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
555138568Ssam	else
556138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
557138568Ssam	switch (ireq->i_type) {
558138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
559138568Ssam		ireq->i_val = wmep->wmep_logcwmin;
560138568Ssam		break;
561138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
562138568Ssam		ireq->i_val = wmep->wmep_logcwmax;
563138568Ssam		break;
564138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
565138568Ssam		ireq->i_val = wmep->wmep_aifsn;
566138568Ssam		break;
567138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
568138568Ssam		ireq->i_val = wmep->wmep_txopLimit;
569138568Ssam		break;
570138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
571138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
572138568Ssam		ireq->i_val = wmep->wmep_acm;
573138568Ssam		break;
574138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
575138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
576138568Ssam		ireq->i_val = !wmep->wmep_noackPolicy;
577138568Ssam		break;
578138568Ssam	}
579138568Ssam	return 0;
580138568Ssam}
581138568Ssam
582178354Ssamstatic __noinline int
583178354Ssamieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
584149028Ssam{
585178354Ssam	const struct ieee80211_aclator *acl = vap->iv_acl;
586149028Ssam
587178354Ssam	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
588149028Ssam}
589149028Ssam
590143110Swpaul/*
591170530Ssam * Return the current ``state'' of an Atheros capbility.
592170530Ssam * If associated in station mode report the negotiated
593170530Ssam * setting. Otherwise report the current setting.
594170530Ssam */
595170530Ssamstatic int
596178354Ssamgetathcap(struct ieee80211vap *vap, int cap)
597170530Ssam{
598178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
599178354Ssam	    vap->iv_state == IEEE80211_S_RUN)
600178354Ssam		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
601170530Ssam	else
602178354Ssam		return (vap->iv_flags & cap) != 0;
603170530Ssam}
604170530Ssam
605178354Ssamstatic __noinline int
606178354Ssamieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
607170530Ssam{
608178354Ssam	struct ieee80211com *ic = vap->iv_ic;
609178354Ssam	struct ieee80211_channel *c;
610178354Ssam
611170530Ssam	if (ireq->i_len != sizeof(struct ieee80211_channel))
612170530Ssam		return EINVAL;
613178354Ssam	/*
614178354Ssam	 * vap's may have different operating channels when HT is
615178354Ssam	 * in use.  When in RUN state report the vap-specific channel.
616178354Ssam	 * Otherwise return curchan.
617178354Ssam	 */
618178354Ssam	if (vap->iv_state == IEEE80211_S_RUN)
619178354Ssam		c = vap->iv_bss->ni_chan;
620178354Ssam	else
621178354Ssam		c = ic->ic_curchan;
622178354Ssam	return copyout(c, ireq->i_data, sizeof(*c));
623170530Ssam}
624170530Ssam
625178354Ssamstatic int
626178354Ssamgetappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
627178354Ssam{
628178354Ssam	if (aie == NULL)
629178354Ssam		return EINVAL;
630178354Ssam	/* NB: truncate, caller can check length */
631178354Ssam	if (ireq->i_len > aie->ie_len)
632178354Ssam		ireq->i_len = aie->ie_len;
633178354Ssam	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
634178354Ssam}
635178354Ssam
636178354Ssamstatic int
637178354Ssamieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
638178354Ssam{
639178354Ssam	uint8_t fc0;
640178354Ssam
641178354Ssam	fc0 = ireq->i_val & 0xff;
642178354Ssam	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
643178354Ssam		return EINVAL;
644178354Ssam	/* NB: could check iv_opmode and reject but hardly worth the effort */
645178354Ssam	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
646178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
647178354Ssam		return getappie(vap->iv_appie_beacon, ireq);
648178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
649178354Ssam		return getappie(vap->iv_appie_proberesp, ireq);
650178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
651178354Ssam		return getappie(vap->iv_appie_assocresp, ireq);
652178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
653178354Ssam		return getappie(vap->iv_appie_probereq, ireq);
654178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
655178354Ssam		return getappie(vap->iv_appie_assocreq, ireq);
656178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
657178354Ssam		return getappie(vap->iv_appie_wpa, ireq);
658178354Ssam	}
659178354Ssam	return EINVAL;
660178354Ssam}
661178354Ssam
662178354Ssamstatic __noinline int
663178354Ssamieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
664178354Ssam	const struct ieee80211req *ireq)
665178354Ssam{
666178354Ssam	struct ieee80211com *ic = vap->iv_ic;
667178354Ssam
668178354Ssam	if (ireq->i_len != sizeof(ic->ic_regdomain))
669178354Ssam		return EINVAL;
670178354Ssam	return copyout(&ic->ic_regdomain, ireq->i_data,
671178354Ssam	    sizeof(ic->ic_regdomain));
672178354Ssam}
673178354Ssam
674178354Ssamstatic __noinline int
675178354Ssamieee80211_ioctl_getroam(struct ieee80211vap *vap,
676178354Ssam	const struct ieee80211req *ireq)
677178354Ssam{
678178354Ssam	if (ireq->i_len != sizeof(vap->iv_roamparms))
679178354Ssam		return EINVAL;
680178354Ssam	return copyout(vap->iv_roamparms, ireq->i_data,
681178354Ssam	    sizeof(vap->iv_roamparms));
682178354Ssam}
683178354Ssam
684178354Ssamstatic __noinline int
685178354Ssamieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
686178354Ssam	const struct ieee80211req *ireq)
687178354Ssam{
688178354Ssam	if (ireq->i_len != sizeof(vap->iv_txparms))
689178354Ssam		return EINVAL;
690178354Ssam	return copyout(vap->iv_txparms, ireq->i_data, sizeof(vap->iv_txparms));
691178354Ssam}
692178354Ssam
693178354Ssamstatic __noinline int
694178354Ssamieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
695178354Ssam	const struct ieee80211req *ireq)
696178354Ssam{
697178354Ssam	struct ieee80211_devcaps_req *dc;
698178354Ssam	struct ieee80211req_chaninfo *ci;
699178354Ssam	int error;
700178354Ssam
701178354Ssam	if (ireq->i_len != sizeof(struct ieee80211_devcaps_req))
702178354Ssam		return EINVAL;
703186302Ssam	dc = (struct ieee80211_devcaps_req *) malloc(
704184210Sdes	    sizeof(struct ieee80211_devcaps_req), M_TEMP, M_NOWAIT | M_ZERO);
705178354Ssam	if (dc == NULL)
706178354Ssam		return ENOMEM;
707178354Ssam	dc->dc_drivercaps = ic->ic_caps;
708178354Ssam	dc->dc_cryptocaps = ic->ic_cryptocaps;
709178354Ssam	dc->dc_htcaps = ic->ic_htcaps;
710178354Ssam	ci = &dc->dc_chaninfo;
711178354Ssam	ic->ic_getradiocaps(ic, &ci->ic_nchans, ci->ic_chans);
712178354Ssam	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
713178354Ssam	error = copyout(dc, ireq->i_data, sizeof(*dc));
714186302Ssam	free(dc, M_TEMP);
715178354Ssam	return error;
716178354Ssam}
717178354Ssam
718178354Ssamstatic __noinline int
719178354Ssamieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
720178354Ssam{
721178354Ssam	struct ieee80211_node *ni;
722178354Ssam	struct ieee80211req_sta_vlan vlan;
723178354Ssam	int error;
724178354Ssam
725178354Ssam	if (ireq->i_len != sizeof(vlan))
726178354Ssam		return EINVAL;
727178354Ssam	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
728178354Ssam	if (error != 0)
729178354Ssam		return error;
730178354Ssam	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
731178354Ssam		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
732178354Ssam		    vlan.sv_macaddr);
733178354Ssam		if (ni == NULL)
734178354Ssam			return ENOENT;
735178354Ssam	} else
736178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
737178354Ssam	vlan.sv_vlan = ni->ni_vlan;
738178354Ssam	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
739178354Ssam	ieee80211_free_node(ni);
740178354Ssam	return error;
741178354Ssam}
742178354Ssam
743170530Ssam/*
744143110Swpaul * When building the kernel with -O2 on the i386 architecture, gcc
745143110Swpaul * seems to want to inline this function into ieee80211_ioctl()
746143110Swpaul * (which is the only routine that calls it). When this happens,
747143110Swpaul * ieee80211_ioctl() ends up consuming an additional 2K of stack
748143110Swpaul * space. (Exactly why it needs so much is unclear.) The problem
749143110Swpaul * is that it's possible for ieee80211_ioctl() to invoke other
750143110Swpaul * routines (including driver init functions) which could then find
751143110Swpaul * themselves perilously close to exhausting the stack.
752143110Swpaul *
753143110Swpaul * To avoid this, we deliberately prevent gcc from inlining this
754143110Swpaul * routine. Another way to avoid this is to use less agressive
755143110Swpaul * optimization when compiling this file (i.e. -O instead of -O2)
756143110Swpaul * but special-casing the compilation of this one module in the
757143110Swpaul * build system would be awkward.
758143110Swpaul */
759178354Ssamstatic __noinline int
760178354Ssamieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
761178354Ssam    struct ieee80211req *ireq)
762138568Ssam{
763178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
764178354Ssam	struct ieee80211com *ic = vap->iv_ic;
765178354Ssam	u_int kid, len;
766170530Ssam	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
767116742Ssam	char tmpssid[IEEE80211_NWID_LEN];
768178354Ssam	int error = 0;
769116742Ssam
770138568Ssam	switch (ireq->i_type) {
771138568Ssam	case IEEE80211_IOC_SSID:
772178354Ssam		switch (vap->iv_state) {
773138568Ssam		case IEEE80211_S_INIT:
774138568Ssam		case IEEE80211_S_SCAN:
775178354Ssam			ireq->i_len = vap->iv_des_ssid[0].len;
776178354Ssam			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
777138568Ssam			break;
778138568Ssam		default:
779178354Ssam			ireq->i_len = vap->iv_bss->ni_esslen;
780178354Ssam			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
781138568Ssam			break;
782138568Ssam		}
783138568Ssam		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
784116742Ssam		break;
785138568Ssam	case IEEE80211_IOC_NUMSSIDS:
786138568Ssam		ireq->i_val = 1;
787138568Ssam		break;
788138568Ssam	case IEEE80211_IOC_WEP:
789178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
790138568Ssam			ireq->i_val = IEEE80211_WEP_OFF;
791178354Ssam		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
792138568Ssam			ireq->i_val = IEEE80211_WEP_ON;
793138568Ssam		else
794138568Ssam			ireq->i_val = IEEE80211_WEP_MIXED;
795138568Ssam		break;
796138568Ssam	case IEEE80211_IOC_WEPKEY:
797138568Ssam		kid = (u_int) ireq->i_val;
798138568Ssam		if (kid >= IEEE80211_WEP_NKID)
799138568Ssam			return EINVAL;
800178354Ssam		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
801138568Ssam		/* NB: only root can read WEP keys */
802164033Srwatson		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
803178354Ssam			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
804138568Ssam		} else {
805138568Ssam			bzero(tmpkey, len);
806138568Ssam		}
807138568Ssam		ireq->i_len = len;
808138568Ssam		error = copyout(tmpkey, ireq->i_data, len);
809138568Ssam		break;
810138568Ssam	case IEEE80211_IOC_NUMWEPKEYS:
811138568Ssam		ireq->i_val = IEEE80211_WEP_NKID;
812138568Ssam		break;
813138568Ssam	case IEEE80211_IOC_WEPTXKEY:
814178354Ssam		ireq->i_val = vap->iv_def_txkey;
815138568Ssam		break;
816138568Ssam	case IEEE80211_IOC_AUTHMODE:
817178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA)
818138568Ssam			ireq->i_val = IEEE80211_AUTH_WPA;
819138568Ssam		else
820178354Ssam			ireq->i_val = vap->iv_bss->ni_authmode;
821138568Ssam		break;
822138568Ssam	case IEEE80211_IOC_CHANNEL:
823148936Ssam		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
824138568Ssam		break;
825138568Ssam	case IEEE80211_IOC_POWERSAVE:
826178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON)
827138568Ssam			ireq->i_val = IEEE80211_POWERSAVE_ON;
828138568Ssam		else
829138568Ssam			ireq->i_val = IEEE80211_POWERSAVE_OFF;
830138568Ssam		break;
831138568Ssam	case IEEE80211_IOC_POWERSAVESLEEP:
832138568Ssam		ireq->i_val = ic->ic_lintval;
833138568Ssam		break;
834138568Ssam	case IEEE80211_IOC_RTSTHRESHOLD:
835178354Ssam		ireq->i_val = vap->iv_rtsthreshold;
836138568Ssam		break;
837138568Ssam	case IEEE80211_IOC_PROTMODE:
838138568Ssam		ireq->i_val = ic->ic_protmode;
839138568Ssam		break;
840138568Ssam	case IEEE80211_IOC_TXPOWER:
841178354Ssam		/*
842178354Ssam		 * Tx power limit is the min of max regulatory
843178354Ssam		 * power, any user-set limit, and the max the
844178354Ssam		 * radio can do.
845178354Ssam		 */
846178354Ssam		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
847178354Ssam		if (ireq->i_val > ic->ic_txpowlimit)
848178354Ssam			ireq->i_val = ic->ic_txpowlimit;
849178354Ssam		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
850178354Ssam			ireq->i_val = ic->ic_curchan->ic_maxpower;
851138568Ssam		break;
852138568Ssam	case IEEE80211_IOC_WPA:
853178354Ssam		switch (vap->iv_flags & IEEE80211_F_WPA) {
854138568Ssam		case IEEE80211_F_WPA1:
855116742Ssam			ireq->i_val = 1;
856116742Ssam			break;
857138568Ssam		case IEEE80211_F_WPA2:
858138568Ssam			ireq->i_val = 2;
859116742Ssam			break;
860138568Ssam		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
861138568Ssam			ireq->i_val = 3;
862116742Ssam			break;
863138568Ssam		default:
864138568Ssam			ireq->i_val = 0;
865116742Ssam			break;
866138568Ssam		}
867138568Ssam		break;
868138568Ssam	case IEEE80211_IOC_CHANLIST:
869178354Ssam		error = ieee80211_ioctl_getchanlist(vap, ireq);
870138568Ssam		break;
871138568Ssam	case IEEE80211_IOC_ROAMING:
872178354Ssam		ireq->i_val = vap->iv_roaming;
873138568Ssam		break;
874138568Ssam	case IEEE80211_IOC_PRIVACY:
875178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
876138568Ssam		break;
877138568Ssam	case IEEE80211_IOC_DROPUNENCRYPTED:
878178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
879138568Ssam		break;
880138568Ssam	case IEEE80211_IOC_COUNTERMEASURES:
881178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
882138568Ssam		break;
883138568Ssam	case IEEE80211_IOC_WME:
884178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
885138568Ssam		break;
886138568Ssam	case IEEE80211_IOC_HIDESSID:
887178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
888138568Ssam		break;
889138568Ssam	case IEEE80211_IOC_APBRIDGE:
890178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
891138568Ssam		break;
892138568Ssam	case IEEE80211_IOC_WPAKEY:
893178354Ssam		error = ieee80211_ioctl_getkey(vap, ireq);
894138568Ssam		break;
895138568Ssam	case IEEE80211_IOC_CHANINFO:
896178354Ssam		error = ieee80211_ioctl_getchaninfo(vap, ireq);
897138568Ssam		break;
898138568Ssam	case IEEE80211_IOC_BSSID:
899138568Ssam		if (ireq->i_len != IEEE80211_ADDR_LEN)
900138568Ssam			return EINVAL;
901178354Ssam		error = copyout(vap->iv_state == IEEE80211_S_RUN ?
902178354Ssam					vap->iv_bss->ni_bssid :
903178354Ssam					vap->iv_des_bssid,
904138568Ssam				ireq->i_data, ireq->i_len);
905138568Ssam		break;
906138568Ssam	case IEEE80211_IOC_WPAIE:
907178354Ssam		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
908138568Ssam		break;
909170530Ssam	case IEEE80211_IOC_WPAIE2:
910178354Ssam		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
911170530Ssam		break;
912138568Ssam	case IEEE80211_IOC_SCAN_RESULTS:
913178354Ssam		error = ieee80211_ioctl_getscanresults(vap, ireq);
914138568Ssam		break;
915138568Ssam	case IEEE80211_IOC_STA_STATS:
916178354Ssam		error = ieee80211_ioctl_getstastats(vap, ireq);
917138568Ssam		break;
918138568Ssam	case IEEE80211_IOC_TXPOWMAX:
919178354Ssam		ireq->i_val = vap->iv_bss->ni_txpower;
920138568Ssam		break;
921138568Ssam	case IEEE80211_IOC_STA_TXPOW:
922178354Ssam		error = ieee80211_ioctl_getstatxpow(vap, ireq);
923138568Ssam		break;
924138568Ssam	case IEEE80211_IOC_STA_INFO:
925178354Ssam		error = ieee80211_ioctl_getstainfo(vap, ireq);
926138568Ssam		break;
927138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
928138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
929138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
930138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
931138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
932138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
933178354Ssam		error = ieee80211_ioctl_getwmeparam(vap, ireq);
934138568Ssam		break;
935138568Ssam	case IEEE80211_IOC_DTIM_PERIOD:
936178354Ssam		ireq->i_val = vap->iv_dtim_period;
937138568Ssam		break;
938138568Ssam	case IEEE80211_IOC_BEACON_INTERVAL:
939138568Ssam		/* NB: get from ic_bss for station mode */
940178354Ssam		ireq->i_val = vap->iv_bss->ni_intval;
941138568Ssam		break;
942147794Ssam	case IEEE80211_IOC_PUREG:
943178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
944147794Ssam		break;
945170530Ssam	case IEEE80211_IOC_FF:
946178354Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
947170530Ssam		break;
948170530Ssam	case IEEE80211_IOC_TURBOP:
949178354Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
950170530Ssam		break;
951170530Ssam	case IEEE80211_IOC_BGSCAN:
952178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
953170530Ssam		break;
954170530Ssam	case IEEE80211_IOC_BGSCAN_IDLE:
955178354Ssam		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
956170530Ssam		break;
957170530Ssam	case IEEE80211_IOC_BGSCAN_INTERVAL:
958178354Ssam		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
959170530Ssam		break;
960170530Ssam	case IEEE80211_IOC_SCANVALID:
961178354Ssam		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
962170530Ssam		break;
963148292Ssam	case IEEE80211_IOC_FRAGTHRESHOLD:
964178354Ssam		ireq->i_val = vap->iv_fragthreshold;
965148292Ssam		break;
966149028Ssam	case IEEE80211_IOC_MACCMD:
967178354Ssam		error = ieee80211_ioctl_getmaccmd(vap, ireq);
968149028Ssam		break;
969153421Ssam	case IEEE80211_IOC_BURST:
970178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
971153421Ssam		break;
972160686Ssam	case IEEE80211_IOC_BMISSTHRESHOLD:
973178354Ssam		ireq->i_val = vap->iv_bmissthreshold;
974160686Ssam		break;
975170530Ssam	case IEEE80211_IOC_CURCHAN:
976178354Ssam		error = ieee80211_ioctl_getcurchan(vap, ireq);
977170530Ssam		break;
978170530Ssam	case IEEE80211_IOC_SHORTGI:
979170530Ssam		ireq->i_val = 0;
980178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
981170530Ssam			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
982178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
983170530Ssam			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
984170530Ssam		break;
985170530Ssam	case IEEE80211_IOC_AMPDU:
986170530Ssam		ireq->i_val = 0;
987178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)
988170530Ssam			ireq->i_val |= 1;
989178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)
990170530Ssam			ireq->i_val |= 2;
991170530Ssam		break;
992170530Ssam	case IEEE80211_IOC_AMPDU_LIMIT:
993178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
994178354Ssam			ireq->i_val = vap->iv_ampdu_rxmax;
995178354Ssam		else if (vap->iv_state == IEEE80211_S_RUN)
996178354Ssam			ireq->i_val = MS(vap->iv_bss->ni_htparam,
997178354Ssam			    IEEE80211_HTCAP_MAXRXAMPDU);
998178354Ssam		else
999178354Ssam			ireq->i_val = vap->iv_ampdu_limit;
1000170530Ssam		break;
1001170530Ssam	case IEEE80211_IOC_AMPDU_DENSITY:
1002182832Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1003182832Ssam		    vap->iv_state == IEEE80211_S_RUN)
1004178354Ssam			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1005178354Ssam			    IEEE80211_HTCAP_MPDUDENSITY);
1006178354Ssam		else
1007178354Ssam			ireq->i_val = vap->iv_ampdu_density;
1008170530Ssam		break;
1009170530Ssam	case IEEE80211_IOC_AMSDU:
1010170530Ssam		ireq->i_val = 0;
1011178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_TX)
1012170530Ssam			ireq->i_val |= 1;
1013178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_RX)
1014170530Ssam			ireq->i_val |= 2;
1015170530Ssam		break;
1016170530Ssam	case IEEE80211_IOC_AMSDU_LIMIT:
1017178354Ssam		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1018170530Ssam		break;
1019170530Ssam	case IEEE80211_IOC_PUREN:
1020178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) != 0;
1021170530Ssam		break;
1022170530Ssam	case IEEE80211_IOC_DOTH:
1023178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1024170530Ssam		break;
1025178354Ssam	case IEEE80211_IOC_REGDOMAIN:
1026178354Ssam		error = ieee80211_ioctl_getregdomain(vap, ireq);
1027178354Ssam		break;
1028178354Ssam	case IEEE80211_IOC_ROAM:
1029178354Ssam		error = ieee80211_ioctl_getroam(vap, ireq);
1030178354Ssam		break;
1031178354Ssam	case IEEE80211_IOC_TXPARAMS:
1032178354Ssam		error = ieee80211_ioctl_gettxparams(vap, ireq);
1033178354Ssam		break;
1034170530Ssam	case IEEE80211_IOC_HTCOMPAT:
1035178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) != 0;
1036170530Ssam		break;
1037178354Ssam	case IEEE80211_IOC_DWDS:
1038178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1039178354Ssam		break;
1040172062Ssam	case IEEE80211_IOC_INACTIVITY:
1041178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1042172062Ssam		break;
1043178354Ssam	case IEEE80211_IOC_APPIE:
1044178354Ssam		error = ieee80211_ioctl_getappie(vap, ireq);
1045178354Ssam		break;
1046178354Ssam	case IEEE80211_IOC_WPS:
1047178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1048178354Ssam		break;
1049178354Ssam	case IEEE80211_IOC_TSN:
1050178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1051178354Ssam		break;
1052178354Ssam	case IEEE80211_IOC_DFS:
1053178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1054178354Ssam		break;
1055178354Ssam	case IEEE80211_IOC_DOTD:
1056178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1057178354Ssam		break;
1058178354Ssam	case IEEE80211_IOC_DEVCAPS:
1059178354Ssam		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1060178354Ssam		break;
1061173273Ssam	case IEEE80211_IOC_HTPROTMODE:
1062173273Ssam		ireq->i_val = ic->ic_htprotmode;
1063173273Ssam		break;
1064173273Ssam	case IEEE80211_IOC_HTCONF:
1065178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_HT) {
1066173273Ssam			ireq->i_val = 1;
1067178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
1068173273Ssam				ireq->i_val |= 2;
1069173273Ssam		} else
1070173273Ssam			ireq->i_val = 0;
1071173273Ssam		break;
1072178354Ssam	case IEEE80211_IOC_STA_VLAN:
1073178354Ssam		error = ieee80211_ioctl_getstavlan(vap, ireq);
1074178354Ssam		break;
1075183255Ssam	case IEEE80211_IOC_SMPS:
1076183255Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1077183255Ssam		    vap->iv_state == IEEE80211_S_RUN) {
1078183255Ssam			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1079183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1080183255Ssam			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1081183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1082183255Ssam			else
1083183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1084183255Ssam		} else
1085183255Ssam			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1086183255Ssam		break;
1087183256Ssam	case IEEE80211_IOC_RIFS:
1088183256Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1089183256Ssam		    vap->iv_state == IEEE80211_S_RUN)
1090183256Ssam			ireq->i_val =
1091183256Ssam			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1092183256Ssam		else
1093183256Ssam			ireq->i_val =
1094183256Ssam			    (vap->iv_flags_ext & IEEE80211_FEXT_RIFS) != 0;
1095183256Ssam		break;
1096186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
1097186904Ssam	case IEEE80211_IOC_TDMA_SLOT:
1098186904Ssam	case IEEE80211_IOC_TDMA_SLOTCNT:
1099186904Ssam	case IEEE80211_IOC_TDMA_SLOTLEN:
1100186904Ssam	case IEEE80211_IOC_TDMA_BINTERVAL:
1101186904Ssam		error = ieee80211_tdma_ioctl_get80211(vap, ireq);
1102186904Ssam		break;
1103186904Ssam#endif
1104138568Ssam	default:
1105138568Ssam		error = EINVAL;
1106138568Ssam		break;
1107138568Ssam	}
1108138568Ssam	return error;
1109178354Ssam#undef MS
1110138568Ssam}
1111138568Ssam
1112178354Ssamstatic __noinline int
1113178354Ssamieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1114138568Ssam{
1115138568Ssam	struct ieee80211req_key ik;
1116138568Ssam	struct ieee80211_node *ni;
1117138568Ssam	struct ieee80211_key *wk;
1118170530Ssam	uint16_t kid;
1119178354Ssam	int error, i;
1120138568Ssam
1121138568Ssam	if (ireq->i_len != sizeof(ik))
1122138568Ssam		return EINVAL;
1123138568Ssam	error = copyin(ireq->i_data, &ik, sizeof(ik));
1124138568Ssam	if (error)
1125138568Ssam		return error;
1126138568Ssam	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1127138568Ssam	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1128138568Ssam	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1129138568Ssam		return E2BIG;
1130138568Ssam	kid = ik.ik_keyix;
1131138568Ssam	if (kid == IEEE80211_KEYIX_NONE) {
1132138568Ssam		/* XXX unicast keys currently must be tx/rx */
1133138568Ssam		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1134138568Ssam			return EINVAL;
1135178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1136178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1137147775Ssam			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1138147775Ssam				ieee80211_free_node(ni);
1139138568Ssam				return EADDRNOTAVAIL;
1140147775Ssam			}
1141138568Ssam		} else {
1142178354Ssam			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1143178354Ssam				ik.ik_macaddr);
1144138568Ssam			if (ni == NULL)
1145138568Ssam				return ENOENT;
1146138568Ssam		}
1147138568Ssam		wk = &ni->ni_ucastkey;
1148138568Ssam	} else {
1149138568Ssam		if (kid >= IEEE80211_WEP_NKID)
1150138568Ssam			return EINVAL;
1151178354Ssam		wk = &vap->iv_nw_keys[kid];
1152155885Ssam		/*
1153155885Ssam		 * Global slots start off w/o any assigned key index.
1154155885Ssam		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1155155885Ssam		 */
1156155885Ssam		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1157155885Ssam			wk->wk_keyix = kid;
1158138568Ssam		ni = NULL;
1159138568Ssam	}
1160138568Ssam	error = 0;
1161178354Ssam	ieee80211_key_update_begin(vap);
1162178354Ssam	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1163138568Ssam		wk->wk_keylen = ik.ik_keylen;
1164138568Ssam		/* NB: MIC presence is implied by cipher type */
1165138568Ssam		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1166138568Ssam			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1167178354Ssam		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1168178354Ssam			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1169138568Ssam		wk->wk_keytsc = 0;			/* new key, reset */
1170138568Ssam		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1171138568Ssam		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1172179395Ssam		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1173179395Ssam		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1174179395Ssam		if (!ieee80211_crypto_setkey(vap, wk))
1175138568Ssam			error = EIO;
1176138568Ssam		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1177178354Ssam			vap->iv_def_txkey = kid;
1178138568Ssam	} else
1179138568Ssam		error = ENXIO;
1180178354Ssam	ieee80211_key_update_end(vap);
1181138568Ssam	if (ni != NULL)
1182138568Ssam		ieee80211_free_node(ni);
1183138568Ssam	return error;
1184138568Ssam}
1185138568Ssam
1186178354Ssamstatic __noinline int
1187178354Ssamieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1188138568Ssam{
1189138568Ssam	struct ieee80211req_del_key dk;
1190138568Ssam	int kid, error;
1191138568Ssam
1192138568Ssam	if (ireq->i_len != sizeof(dk))
1193138568Ssam		return EINVAL;
1194138568Ssam	error = copyin(ireq->i_data, &dk, sizeof(dk));
1195138568Ssam	if (error)
1196138568Ssam		return error;
1197138568Ssam	kid = dk.idk_keyix;
1198170530Ssam	/* XXX uint8_t -> uint16_t */
1199170530Ssam	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1200138568Ssam		struct ieee80211_node *ni;
1201138568Ssam
1202178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1203178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1204147775Ssam			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1205147775Ssam				ieee80211_free_node(ni);
1206147775Ssam				return EADDRNOTAVAIL;
1207147775Ssam			}
1208147775Ssam		} else {
1209178354Ssam			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1210178354Ssam				dk.idk_macaddr);
1211147775Ssam			if (ni == NULL)
1212147775Ssam				return ENOENT;
1213147775Ssam		}
1214138568Ssam		/* XXX error return */
1215148863Ssam		ieee80211_node_delucastkey(ni);
1216138568Ssam		ieee80211_free_node(ni);
1217138568Ssam	} else {
1218138568Ssam		if (kid >= IEEE80211_WEP_NKID)
1219138568Ssam			return EINVAL;
1220138568Ssam		/* XXX error return */
1221178354Ssam		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1222138568Ssam	}
1223138568Ssam	return 0;
1224138568Ssam}
1225138568Ssam
1226178354Ssamstruct mlmeop {
1227178354Ssam	struct ieee80211vap *vap;
1228178354Ssam	int	op;
1229178354Ssam	int	reason;
1230178354Ssam};
1231178354Ssam
1232138568Ssamstatic void
1233178354Ssammlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1234178354Ssam	int op, int reason)
1235178354Ssam{
1236178354Ssam#ifdef IEEE80211_DEBUG
1237178354Ssam	static const struct {
1238178354Ssam		int mask;
1239178354Ssam		const char *opstr;
1240178354Ssam	} ops[] = {
1241178354Ssam		{ 0, "op#0" },
1242178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1243178354Ssam		  IEEE80211_MSG_ASSOC, "assoc" },
1244178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1245178354Ssam		  IEEE80211_MSG_ASSOC, "disassoc" },
1246178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1247178354Ssam		  IEEE80211_MSG_AUTH, "deauth" },
1248178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1249178354Ssam		  IEEE80211_MSG_AUTH, "authorize" },
1250178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1251178354Ssam		  IEEE80211_MSG_AUTH, "unauthorize" },
1252178354Ssam	};
1253178354Ssam
1254178354Ssam	if (op == IEEE80211_MLME_AUTH) {
1255178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1256178354Ssam		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1257178354Ssam		    "station authenticate %s via MLME (reason %d)",
1258178354Ssam		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1259178354Ssam		    reason);
1260178354Ssam	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1261178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1262178354Ssam		    "unknown MLME request %d (reason %d)", op, reason);
1263178354Ssam	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1264178354Ssam		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1265178354Ssam		    "station %s via MLME", ops[op].opstr);
1266178354Ssam	} else {
1267178354Ssam		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1268178354Ssam		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1269178354Ssam	}
1270178354Ssam#endif /* IEEE80211_DEBUG */
1271178354Ssam}
1272178354Ssam
1273178354Ssamstatic void
1274138568Ssamdomlme(void *arg, struct ieee80211_node *ni)
1275138568Ssam{
1276178354Ssam	struct mlmeop *mop = arg;
1277178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1278138568Ssam
1279178354Ssam	if (vap != mop->vap)
1280178354Ssam		return;
1281178354Ssam	/*
1282178354Ssam	 * NB: if ni_associd is zero then the node is already cleaned
1283178354Ssam	 * up and we don't need to do this (we're safely holding a
1284178354Ssam	 * reference but should otherwise not modify it's state).
1285178354Ssam	 */
1286178354Ssam	if (ni->ni_associd == 0)
1287178354Ssam		return;
1288178354Ssam	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1289178354Ssam	if (mop->op == IEEE80211_MLME_DEAUTH) {
1290178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1291178354Ssam		    mop->reason);
1292178354Ssam	} else {
1293178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1294178354Ssam		    mop->reason);
1295138568Ssam	}
1296178354Ssam	ieee80211_node_leave(ni);
1297138568Ssam}
1298138568Ssam
1299178354Ssamstatic int
1300178354Ssamsetmlme_dropsta(struct ieee80211vap *vap,
1301178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1302178354Ssam{
1303178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1304178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1305178354Ssam	struct ieee80211_node *ni;
1306178354Ssam	int error = 0;
1307178354Ssam
1308178354Ssam	/* NB: the broadcast address means do 'em all */
1309178354Ssam	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1310178354Ssam		IEEE80211_NODE_LOCK(nt);
1311178354Ssam		ni = ieee80211_find_node_locked(nt, mac);
1312178354Ssam		if (ni != NULL) {
1313178354Ssam			domlme(mlmeop, ni);
1314178354Ssam			ieee80211_free_node(ni);
1315178354Ssam		} else
1316178354Ssam			error = ENOENT;
1317178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1318178354Ssam	} else {
1319178354Ssam		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1320178354Ssam	}
1321178354Ssam	return error;
1322178354Ssam}
1323178354Ssam
1324178354Ssamstatic __noinline int
1325178354Ssamsetmlme_common(struct ieee80211vap *vap, int op,
1326178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1327178354Ssam{
1328178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1329178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1330178354Ssam	struct ieee80211_node *ni;
1331178354Ssam	struct mlmeop mlmeop;
1332178354Ssam	int error;
1333178354Ssam
1334178354Ssam	error = 0;
1335178354Ssam	switch (op) {
1336178354Ssam	case IEEE80211_MLME_DISASSOC:
1337178354Ssam	case IEEE80211_MLME_DEAUTH:
1338178354Ssam		switch (vap->iv_opmode) {
1339178354Ssam		case IEEE80211_M_STA:
1340178354Ssam			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1341178354Ssam			/* XXX not quite right */
1342178354Ssam			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1343178354Ssam			break;
1344178354Ssam		case IEEE80211_M_HOSTAP:
1345178354Ssam			mlmeop.vap = vap;
1346178354Ssam			mlmeop.op = op;
1347178354Ssam			mlmeop.reason = reason;
1348178354Ssam			error = setmlme_dropsta(vap, mac, &mlmeop);
1349178354Ssam			break;
1350178354Ssam		case IEEE80211_M_WDS:
1351178354Ssam			/* XXX user app should send raw frame? */
1352178354Ssam			if (op != IEEE80211_MLME_DEAUTH) {
1353178354Ssam				error = EINVAL;
1354178354Ssam				break;
1355178354Ssam			}
1356178354Ssam#if 0
1357178354Ssam			/* XXX accept any address, simplifies user code */
1358178354Ssam			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1359178354Ssam				error = EINVAL;
1360178354Ssam				break;
1361178354Ssam			}
1362178354Ssam#endif
1363178354Ssam			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1364178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1365178354Ssam			IEEE80211_SEND_MGMT(ni,
1366178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1367178354Ssam			ieee80211_free_node(ni);
1368178354Ssam			break;
1369178354Ssam		default:
1370178354Ssam			error = EINVAL;
1371178354Ssam			break;
1372178354Ssam		}
1373178354Ssam		break;
1374178354Ssam	case IEEE80211_MLME_AUTHORIZE:
1375178354Ssam	case IEEE80211_MLME_UNAUTHORIZE:
1376178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1377178354Ssam		    vap->iv_opmode != IEEE80211_M_WDS) {
1378178354Ssam			error = EINVAL;
1379178354Ssam			break;
1380178354Ssam		}
1381178354Ssam		IEEE80211_NODE_LOCK(nt);
1382178354Ssam		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1383178354Ssam		if (ni != NULL) {
1384178354Ssam			mlmedebug(vap, mac, op, reason);
1385178354Ssam			if (op == IEEE80211_MLME_AUTHORIZE)
1386178354Ssam				ieee80211_node_authorize(ni);
1387178354Ssam			else
1388178354Ssam				ieee80211_node_unauthorize(ni);
1389178354Ssam			ieee80211_free_node(ni);
1390178354Ssam		} else
1391178354Ssam			error = ENOENT;
1392178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1393178354Ssam		break;
1394178354Ssam	case IEEE80211_MLME_AUTH:
1395178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1396178354Ssam			error = EINVAL;
1397178354Ssam			break;
1398178354Ssam		}
1399178354Ssam		IEEE80211_NODE_LOCK(nt);
1400178354Ssam		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1401178354Ssam		if (ni != NULL) {
1402178354Ssam			mlmedebug(vap, mac, op, reason);
1403178354Ssam			if (reason == IEEE80211_STATUS_SUCCESS) {
1404178354Ssam				IEEE80211_SEND_MGMT(ni,
1405178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1406178354Ssam				/*
1407178354Ssam				 * For shared key auth, just continue the
1408178354Ssam				 * exchange.  Otherwise when 802.1x is not in
1409178354Ssam				 * use mark the port authorized at this point
1410178354Ssam				 * so traffic can flow.
1411178354Ssam				 */
1412178354Ssam				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1413178354Ssam				    ni->ni_challenge == NULL)
1414178354Ssam				      ieee80211_node_authorize(ni);
1415178354Ssam			} else {
1416178354Ssam				vap->iv_stats.is_rx_acl++;
1417178354Ssam				ieee80211_send_error(ni, ni->ni_macaddr,
1418178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1419178354Ssam				ieee80211_node_leave(ni);
1420178354Ssam			}
1421178354Ssam			ieee80211_free_node(ni);
1422178354Ssam		} else
1423178354Ssam			error = ENOENT;
1424178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1425178354Ssam		break;
1426178354Ssam	default:
1427178354Ssam		error = EINVAL;
1428178354Ssam		break;
1429178354Ssam	}
1430178354Ssam	return error;
1431178354Ssam}
1432178354Ssam
1433170530Ssamstruct scanlookup {
1434170530Ssam	const uint8_t *mac;
1435170530Ssam	int esslen;
1436170530Ssam	const uint8_t *essid;
1437170530Ssam	const struct ieee80211_scan_entry *se;
1438170530Ssam};
1439170530Ssam
1440170530Ssam/*
1441170530Ssam * Match mac address and any ssid.
1442170530Ssam */
1443170530Ssamstatic void
1444170530Ssammlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1445170530Ssam{
1446170530Ssam	struct scanlookup *look = arg;
1447170530Ssam
1448170530Ssam	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1449170530Ssam		return;
1450170530Ssam	if (look->esslen != 0) {
1451170530Ssam		if (se->se_ssid[1] != look->esslen)
1452170530Ssam			return;
1453170530Ssam		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1454170530Ssam			return;
1455170530Ssam	}
1456170530Ssam	look->se = se;
1457170530Ssam}
1458170530Ssam
1459178354Ssamstatic __noinline int
1460178354Ssamsetmlme_assoc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1461178354Ssam	int ssid_len, const uint8_t ssid[IEEE80211_NWID_LEN])
1462138568Ssam{
1463178354Ssam	struct scanlookup lookup;
1464178354Ssam
1465178354Ssam	/* XXX ibss/ahdemo */
1466178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
1467178354Ssam		return EINVAL;
1468178354Ssam
1469178354Ssam	/* NB: this is racey if roaming is !manual */
1470178354Ssam	lookup.se = NULL;
1471178354Ssam	lookup.mac = mac;
1472178354Ssam	lookup.esslen = ssid_len;
1473178354Ssam	lookup.essid = ssid;
1474178354Ssam	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1475178354Ssam	if (lookup.se == NULL)
1476178354Ssam		return ENOENT;
1477178354Ssam	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1478184274Ssam	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1479178354Ssam		return EIO;		/* XXX unique but could be better */
1480178354Ssam	return 0;
1481178354Ssam}
1482178354Ssam
1483178354Ssamstatic __noinline int
1484178354Ssamieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1485178354Ssam{
1486138568Ssam	struct ieee80211req_mlme mlme;
1487138568Ssam	int error;
1488138568Ssam
1489138568Ssam	if (ireq->i_len != sizeof(mlme))
1490138568Ssam		return EINVAL;
1491138568Ssam	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1492138568Ssam	if (error)
1493138568Ssam		return error;
1494178354Ssam	if  (mlme.im_op == IEEE80211_MLME_ASSOC)
1495178354Ssam		return setmlme_assoc(vap, mlme.im_macaddr,
1496178354Ssam		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1497178354Ssam	else
1498178354Ssam		return setmlme_common(vap, mlme.im_op,
1499178354Ssam		    mlme.im_macaddr, mlme.im_reason);
1500138568Ssam}
1501138568Ssam
1502178354Ssamstatic __noinline int
1503178354Ssamieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1504138568Ssam{
1505170530Ssam	uint8_t mac[IEEE80211_ADDR_LEN];
1506178354Ssam	const struct ieee80211_aclator *acl = vap->iv_acl;
1507138568Ssam	int error;
1508138568Ssam
1509138568Ssam	if (ireq->i_len != sizeof(mac))
1510138568Ssam		return EINVAL;
1511138568Ssam	error = copyin(ireq->i_data, mac, ireq->i_len);
1512138568Ssam	if (error)
1513138568Ssam		return error;
1514138568Ssam	if (acl == NULL) {
1515138568Ssam		acl = ieee80211_aclator_get("mac");
1516178354Ssam		if (acl == NULL || !acl->iac_attach(vap))
1517138568Ssam			return EINVAL;
1518178354Ssam		vap->iv_acl = acl;
1519138568Ssam	}
1520138568Ssam	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1521178354Ssam		acl->iac_add(vap, mac);
1522138568Ssam	else
1523178354Ssam		acl->iac_remove(vap, mac);
1524138568Ssam	return 0;
1525138568Ssam}
1526138568Ssam
1527178354Ssamstatic __noinline int
1528178354Ssamieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1529138568Ssam{
1530178354Ssam	const struct ieee80211_aclator *acl = vap->iv_acl;
1531138568Ssam
1532138568Ssam	switch (ireq->i_val) {
1533138568Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
1534138568Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
1535138568Ssam	case IEEE80211_MACCMD_POLICY_DENY:
1536178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
1537138568Ssam		if (acl == NULL) {
1538138568Ssam			acl = ieee80211_aclator_get("mac");
1539178354Ssam			if (acl == NULL || !acl->iac_attach(vap))
1540138568Ssam				return EINVAL;
1541178354Ssam			vap->iv_acl = acl;
1542138568Ssam		}
1543178354Ssam		acl->iac_setpolicy(vap, ireq->i_val);
1544138568Ssam		break;
1545138568Ssam	case IEEE80211_MACCMD_FLUSH:
1546138568Ssam		if (acl != NULL)
1547178354Ssam			acl->iac_flush(vap);
1548138568Ssam		/* NB: silently ignore when not in use */
1549138568Ssam		break;
1550138568Ssam	case IEEE80211_MACCMD_DETACH:
1551138568Ssam		if (acl != NULL) {
1552178354Ssam			vap->iv_acl = NULL;
1553178354Ssam			acl->iac_detach(vap);
1554138568Ssam		}
1555138568Ssam		break;
1556138568Ssam	default:
1557149028Ssam		if (acl == NULL)
1558149028Ssam			return EINVAL;
1559149028Ssam		else
1560178354Ssam			return acl->iac_setioctl(vap, ireq);
1561138568Ssam	}
1562138568Ssam	return 0;
1563138568Ssam}
1564138568Ssam
1565178354Ssamstatic __noinline int
1566178354Ssamieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1567138568Ssam{
1568178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1569138568Ssam	struct ieee80211req_chanlist list;
1570138568Ssam	u_char chanlist[IEEE80211_CHAN_BYTES];
1571186107Ssam	int i, nchan, error;
1572138568Ssam
1573138568Ssam	if (ireq->i_len != sizeof(list))
1574138568Ssam		return EINVAL;
1575138568Ssam	error = copyin(ireq->i_data, &list, sizeof(list));
1576138568Ssam	if (error)
1577138568Ssam		return error;
1578138568Ssam	memset(chanlist, 0, sizeof(chanlist));
1579170530Ssam	nchan = 0;
1580186107Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1581186107Ssam		const struct ieee80211_channel *c = &ic->ic_channels[i];
1582138568Ssam		/*
1583186107Ssam		 * Calculate the intersection of the user list and the
1584186107Ssam		 * available channels so users can do things like specify
1585186107Ssam		 * 1-255 to get all available channels.
1586138568Ssam		 */
1587186107Ssam		if (isset(list.ic_channels, c->ic_ieee)) {
1588186107Ssam			setbit(chanlist, c->ic_ieee);
1589170530Ssam			nchan++;
1590170530Ssam		}
1591138568Ssam	}
1592170530Ssam	if (nchan == 0)
1593170530Ssam		return EINVAL;
1594170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1595170530Ssam	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1596170530Ssam		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1597138568Ssam	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
1598178354Ssam	ieee80211_scan_flush(vap);
1599178354Ssam	return ENETRESET;
1600138568Ssam}
1601138568Ssam
1602178354Ssamstatic __noinline int
1603178354Ssamieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1604157172Ssam{
1605157172Ssam	struct ieee80211_node *ni;
1606170530Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
1607157172Ssam	int error;
1608157172Ssam
1609157172Ssam	/*
1610157172Ssam	 * NB: we could copyin ieee80211req_sta_stats so apps
1611157172Ssam	 *     could make selective changes but that's overkill;
1612157172Ssam	 *     just clear all stats for now.
1613157172Ssam	 */
1614157172Ssam	if (ireq->i_len < IEEE80211_ADDR_LEN)
1615157172Ssam		return EINVAL;
1616157172Ssam	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1617157172Ssam	if (error != 0)
1618157172Ssam		return error;
1619178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1620157172Ssam	if (ni == NULL)
1621178354Ssam		return ENOENT;
1622178354Ssam	/* XXX require ni_vap == vap? */
1623157172Ssam	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1624157172Ssam	ieee80211_free_node(ni);
1625157172Ssam	return 0;
1626157172Ssam}
1627157172Ssam
1628178354Ssamstatic __noinline int
1629178354Ssamieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1630138568Ssam{
1631138568Ssam	struct ieee80211_node *ni;
1632138568Ssam	struct ieee80211req_sta_txpow txpow;
1633138568Ssam	int error;
1634138568Ssam
1635138568Ssam	if (ireq->i_len != sizeof(txpow))
1636138568Ssam		return EINVAL;
1637138568Ssam	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1638138568Ssam	if (error != 0)
1639138568Ssam		return error;
1640178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1641138568Ssam	if (ni == NULL)
1642178354Ssam		return ENOENT;
1643138568Ssam	ni->ni_txpower = txpow.it_txpow;
1644138568Ssam	ieee80211_free_node(ni);
1645138568Ssam	return error;
1646138568Ssam}
1647138568Ssam
1648178354Ssamstatic __noinline int
1649178354Ssamieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1650138568Ssam{
1651178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1652138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
1653138568Ssam	struct wmeParams *wmep, *chanp;
1654138568Ssam	int isbss, ac;
1655138568Ssam
1656138568Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1657178354Ssam		return EOPNOTSUPP;
1658138568Ssam
1659138568Ssam	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1660138568Ssam	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1661138568Ssam	if (ac >= WME_NUM_AC)
1662138568Ssam		ac = WME_AC_BE;
1663138568Ssam	if (isbss) {
1664138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1665138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1666138568Ssam	} else {
1667138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1668138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1669138568Ssam	}
1670138568Ssam	switch (ireq->i_type) {
1671138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1672138568Ssam		if (isbss) {
1673138568Ssam			wmep->wmep_logcwmin = ireq->i_val;
1674138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1675138568Ssam				chanp->wmep_logcwmin = ireq->i_val;
1676138568Ssam		} else {
1677138568Ssam			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
1678138568Ssam				ireq->i_val;
1679138568Ssam		}
1680138568Ssam		break;
1681138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1682138568Ssam		if (isbss) {
1683138568Ssam			wmep->wmep_logcwmax = ireq->i_val;
1684138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1685138568Ssam				chanp->wmep_logcwmax = ireq->i_val;
1686138568Ssam		} else {
1687138568Ssam			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
1688138568Ssam				ireq->i_val;
1689138568Ssam		}
1690138568Ssam		break;
1691138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1692138568Ssam		if (isbss) {
1693138568Ssam			wmep->wmep_aifsn = ireq->i_val;
1694138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1695138568Ssam				chanp->wmep_aifsn = ireq->i_val;
1696138568Ssam		} else {
1697138568Ssam			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
1698138568Ssam		}
1699138568Ssam		break;
1700138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1701138568Ssam		if (isbss) {
1702138568Ssam			wmep->wmep_txopLimit = ireq->i_val;
1703138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1704138568Ssam				chanp->wmep_txopLimit = ireq->i_val;
1705138568Ssam		} else {
1706138568Ssam			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
1707138568Ssam				ireq->i_val;
1708138568Ssam		}
1709138568Ssam		break;
1710138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1711138568Ssam		wmep->wmep_acm = ireq->i_val;
1712138568Ssam		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1713138568Ssam			chanp->wmep_acm = ireq->i_val;
1714138568Ssam		break;
1715138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1716138568Ssam		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1717138568Ssam			(ireq->i_val) == 0;
1718138568Ssam		break;
1719138568Ssam	}
1720178354Ssam	ieee80211_wme_updateparams(vap);
1721138568Ssam	return 0;
1722138568Ssam}
1723138568Ssam
1724138568Ssamstatic int
1725170530Ssamfind11gchannel(struct ieee80211com *ic, int start, int freq)
1726170530Ssam{
1727170530Ssam	const struct ieee80211_channel *c;
1728170530Ssam	int i;
1729170530Ssam
1730170530Ssam	for (i = start+1; i < ic->ic_nchans; i++) {
1731170530Ssam		c = &ic->ic_channels[i];
1732170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1733170530Ssam			return 1;
1734170530Ssam	}
1735170530Ssam	/* NB: should not be needed but in case things are mis-sorted */
1736170530Ssam	for (i = 0; i < start; i++) {
1737170530Ssam		c = &ic->ic_channels[i];
1738170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1739170530Ssam			return 1;
1740170530Ssam	}
1741170530Ssam	return 0;
1742170530Ssam}
1743170530Ssam
1744170530Ssamstatic struct ieee80211_channel *
1745170530Ssamfindchannel(struct ieee80211com *ic, int ieee, int mode)
1746170530Ssam{
1747170530Ssam	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1748170530Ssam		0,			/* IEEE80211_MODE_AUTO */
1749170530Ssam		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
1750170530Ssam		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
1751170530Ssam		IEEE80211_CHAN_G,	/* IEEE80211_MODE_11G */
1752170530Ssam		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
1753170530Ssam		IEEE80211_CHAN_108A,	/* IEEE80211_MODE_TURBO_A */
1754170530Ssam		IEEE80211_CHAN_108G,	/* IEEE80211_MODE_TURBO_G */
1755170530Ssam		IEEE80211_CHAN_STURBO,	/* IEEE80211_MODE_STURBO_A */
1756170530Ssam		/* NB: handled specially below */
1757170530Ssam		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11NA */
1758170530Ssam		IEEE80211_CHAN_G,	/* IEEE80211_MODE_11NG */
1759170530Ssam	};
1760170530Ssam	u_int modeflags;
1761170530Ssam	int i;
1762170530Ssam
1763170530Ssam	modeflags = chanflags[mode];
1764170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1765170530Ssam		struct ieee80211_channel *c = &ic->ic_channels[i];
1766170530Ssam
1767170530Ssam		if (c->ic_ieee != ieee)
1768170530Ssam			continue;
1769170530Ssam		if (mode == IEEE80211_MODE_AUTO) {
1770170530Ssam			/* ignore turbo channels for autoselect */
1771170530Ssam			if (IEEE80211_IS_CHAN_TURBO(c))
1772170530Ssam				continue;
1773170530Ssam			/*
1774170530Ssam			 * XXX special-case 11b/g channels so we
1775170530Ssam			 *     always select the g channel if both
1776170530Ssam			 *     are present.
1777170530Ssam			 * XXX prefer HT to non-HT?
1778170530Ssam			 */
1779170530Ssam			if (!IEEE80211_IS_CHAN_B(c) ||
1780170530Ssam			    !find11gchannel(ic, i, c->ic_freq))
1781170530Ssam				return c;
1782170530Ssam		} else {
1783170530Ssam			/* must check HT specially */
1784170530Ssam			if ((mode == IEEE80211_MODE_11NA ||
1785170530Ssam			    mode == IEEE80211_MODE_11NG) &&
1786170530Ssam			    !IEEE80211_IS_CHAN_HT(c))
1787170530Ssam				continue;
1788170530Ssam			if ((c->ic_flags & modeflags) == modeflags)
1789170530Ssam				return c;
1790170530Ssam		}
1791170530Ssam	}
1792170530Ssam	return NULL;
1793170530Ssam}
1794170530Ssam
1795170530Ssam/*
1796170530Ssam * Check the specified against any desired mode (aka netband).
1797170530Ssam * This is only used (presently) when operating in hostap mode
1798170530Ssam * to enforce consistency.
1799170530Ssam */
1800170530Ssamstatic int
1801170530Ssamcheck_mode_consistency(const struct ieee80211_channel *c, int mode)
1802170530Ssam{
1803170530Ssam	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1804170530Ssam
1805170530Ssam	switch (mode) {
1806170530Ssam	case IEEE80211_MODE_11B:
1807170530Ssam		return (IEEE80211_IS_CHAN_B(c));
1808170530Ssam	case IEEE80211_MODE_11G:
1809170530Ssam		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1810170530Ssam	case IEEE80211_MODE_11A:
1811170530Ssam		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1812170530Ssam	case IEEE80211_MODE_STURBO_A:
1813170530Ssam		return (IEEE80211_IS_CHAN_STURBO(c));
1814170530Ssam	case IEEE80211_MODE_11NA:
1815170530Ssam		return (IEEE80211_IS_CHAN_HTA(c));
1816170530Ssam	case IEEE80211_MODE_11NG:
1817170530Ssam		return (IEEE80211_IS_CHAN_HTG(c));
1818170530Ssam	}
1819170530Ssam	return 1;
1820170530Ssam
1821170530Ssam}
1822170530Ssam
1823170530Ssam/*
1824170530Ssam * Common code to set the current channel.  If the device
1825170530Ssam * is up and running this may result in an immediate channel
1826170530Ssam * change or a kick of the state machine.
1827170530Ssam */
1828170530Ssamstatic int
1829178354Ssamsetcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1830170530Ssam{
1831178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1832170530Ssam	int error;
1833170530Ssam
1834170530Ssam	if (c != IEEE80211_CHAN_ANYC) {
1835178354Ssam		if (IEEE80211_IS_CHAN_RADAR(c))
1836178354Ssam			return EBUSY;	/* XXX better code? */
1837178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1838178354Ssam			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1839178354Ssam				return EINVAL;
1840178354Ssam			if (!check_mode_consistency(c, vap->iv_des_mode))
1841178354Ssam				return EINVAL;
1842178354Ssam		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1843178354Ssam			if (IEEE80211_IS_CHAN_NOADHOC(c))
1844178354Ssam				return EINVAL;
1845178354Ssam		}
1846178354Ssam		if (vap->iv_state == IEEE80211_S_RUN &&
1847178354Ssam		    vap->iv_bss->ni_chan == c)
1848170530Ssam			return 0;	/* NB: nothing to do */
1849170530Ssam	}
1850178354Ssam	vap->iv_des_chan = c;
1851170530Ssam
1852170530Ssam	error = 0;
1853178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1854178354Ssam	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1855170530Ssam		/*
1856178354Ssam		 * Monitor mode can switch directly.
1857170530Ssam		 */
1858178354Ssam		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1859178354Ssam			/* XXX need state machine for other vap's to follow */
1860178354Ssam			ieee80211_setcurchan(ic, vap->iv_des_chan);
1861178354Ssam			vap->iv_bss->ni_chan = ic->ic_curchan;
1862178354Ssam		} else
1863178354Ssam			ic->ic_curchan = vap->iv_des_chan;
1864170530Ssam	} else {
1865170530Ssam		/*
1866170530Ssam		 * Need to go through the state machine in case we
1867170530Ssam		 * need to reassociate or the like.  The state machine
1868170530Ssam		 * will pickup the desired channel and avoid scanning.
1869170530Ssam		 */
1870178354Ssam		if (IS_UP_AUTO(vap))
1871178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1872178354Ssam		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1873170530Ssam			/*
1874170530Ssam			 * When not up+running and a real channel has
1875170530Ssam			 * been specified fix the current channel so
1876170530Ssam			 * there is immediate feedback; e.g. via ifconfig.
1877170530Ssam			 */
1878178354Ssam			ic->ic_curchan = vap->iv_des_chan;
1879170530Ssam		}
1880170530Ssam	}
1881170530Ssam	return error;
1882170530Ssam}
1883170530Ssam
1884170530Ssam/*
1885170530Ssam * Old api for setting the current channel; this is
1886170530Ssam * deprecated because channel numbers are ambiguous.
1887170530Ssam */
1888178354Ssamstatic __noinline int
1889178354Ssamieee80211_ioctl_setchannel(struct ieee80211vap *vap,
1890170530Ssam	const struct ieee80211req *ireq)
1891170530Ssam{
1892178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1893170530Ssam	struct ieee80211_channel *c;
1894170530Ssam
1895170530Ssam	/* XXX 0xffff overflows 16-bit signed */
1896170530Ssam	if (ireq->i_val == 0 ||
1897170530Ssam	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
1898170530Ssam		c = IEEE80211_CHAN_ANYC;
1899170530Ssam	} else {
1900170530Ssam		struct ieee80211_channel *c2;
1901170530Ssam
1902178354Ssam		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
1903170530Ssam		if (c == NULL) {
1904170530Ssam			c = findchannel(ic, ireq->i_val,
1905170530Ssam				IEEE80211_MODE_AUTO);
1906170530Ssam			if (c == NULL)
1907170530Ssam				return EINVAL;
1908170530Ssam		}
1909170530Ssam		/*
1910170530Ssam		 * Fine tune channel selection based on desired mode:
1911170530Ssam		 *   if 11b is requested, find the 11b version of any
1912170530Ssam		 *      11g channel returned,
1913170530Ssam		 *   if static turbo, find the turbo version of any
1914170530Ssam		 *	11a channel return,
1915170530Ssam		 *   if 11na is requested, find the ht version of any
1916170530Ssam		 *      11a channel returned,
1917170530Ssam		 *   if 11ng is requested, find the ht version of any
1918170530Ssam		 *      11g channel returned,
1919170530Ssam		 *   otherwise we should be ok with what we've got.
1920170530Ssam		 */
1921178354Ssam		switch (vap->iv_des_mode) {
1922170530Ssam		case IEEE80211_MODE_11B:
1923170530Ssam			if (IEEE80211_IS_CHAN_ANYG(c)) {
1924170530Ssam				c2 = findchannel(ic, ireq->i_val,
1925170530Ssam					IEEE80211_MODE_11B);
1926170530Ssam				/* NB: should not happen, =>'s 11g w/o 11b */
1927170530Ssam				if (c2 != NULL)
1928170530Ssam					c = c2;
1929170530Ssam			}
1930170530Ssam			break;
1931170530Ssam		case IEEE80211_MODE_TURBO_A:
1932170530Ssam			if (IEEE80211_IS_CHAN_A(c)) {
1933170530Ssam				c2 = findchannel(ic, ireq->i_val,
1934170530Ssam					IEEE80211_MODE_TURBO_A);
1935170530Ssam				if (c2 != NULL)
1936170530Ssam					c = c2;
1937170530Ssam			}
1938170530Ssam			break;
1939170530Ssam		case IEEE80211_MODE_11NA:
1940170530Ssam			if (IEEE80211_IS_CHAN_A(c)) {
1941170530Ssam				c2 = findchannel(ic, ireq->i_val,
1942170530Ssam					IEEE80211_MODE_11NA);
1943170530Ssam				if (c2 != NULL)
1944170530Ssam					c = c2;
1945170530Ssam			}
1946170530Ssam			break;
1947170530Ssam		case IEEE80211_MODE_11NG:
1948170530Ssam			if (IEEE80211_IS_CHAN_ANYG(c)) {
1949170530Ssam				c2 = findchannel(ic, ireq->i_val,
1950170530Ssam					IEEE80211_MODE_11NG);
1951170530Ssam				if (c2 != NULL)
1952170530Ssam					c = c2;
1953170530Ssam			}
1954170530Ssam			break;
1955170530Ssam		default:		/* NB: no static turboG */
1956170530Ssam			break;
1957170530Ssam		}
1958170530Ssam	}
1959178354Ssam	return setcurchan(vap, c);
1960170530Ssam}
1961170530Ssam
1962170530Ssam/*
1963170530Ssam * New/current api for setting the current channel; a complete
1964170530Ssam * channel description is provide so there is no ambiguity in
1965170530Ssam * identifying the channel.
1966170530Ssam */
1967178354Ssamstatic __noinline int
1968178354Ssamieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
1969170530Ssam	const struct ieee80211req *ireq)
1970170530Ssam{
1971178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1972170530Ssam	struct ieee80211_channel chan, *c;
1973170530Ssam	int error;
1974170530Ssam
1975170530Ssam	if (ireq->i_len != sizeof(chan))
1976170530Ssam		return EINVAL;
1977170530Ssam	error = copyin(ireq->i_data, &chan, sizeof(chan));
1978170530Ssam	if (error != 0)
1979170530Ssam		return error;
1980170530Ssam	/* XXX 0xffff overflows 16-bit signed */
1981170530Ssam	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
1982170530Ssam		c = IEEE80211_CHAN_ANYC;
1983170530Ssam	} else {
1984170530Ssam		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
1985170530Ssam		if (c == NULL)
1986170530Ssam			return EINVAL;
1987170530Ssam	}
1988178354Ssam	return setcurchan(vap, c);
1989170530Ssam}
1990170530Ssam
1991178354Ssamstatic __noinline int
1992178354Ssamieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
1993178354Ssam	const struct ieee80211req *ireq)
1994178354Ssam{
1995178354Ssam	struct ieee80211_regdomain_req *reg;
1996178354Ssam	int error;
1997178354Ssam
1998178354Ssam	if (ireq->i_len != sizeof(struct ieee80211_regdomain_req))
1999178354Ssam		return EINVAL;
2000186302Ssam	reg = (struct ieee80211_regdomain_req *) malloc(
2001184210Sdes	    sizeof(struct ieee80211_regdomain_req), M_TEMP, M_NOWAIT);
2002178354Ssam	if (reg == NULL)
2003178354Ssam		return ENOMEM;
2004178354Ssam	error = copyin(ireq->i_data, reg, sizeof(*reg));
2005178354Ssam	if (error == 0)
2006178354Ssam		error = ieee80211_setregdomain(vap, reg);
2007186302Ssam	free(reg, M_TEMP);
2008178354Ssam
2009178354Ssam	return (error == 0 ? ENETRESET : error);
2010178354Ssam}
2011178354Ssam
2012170530Ssamstatic int
2013178354Ssamieee80211_ioctl_setroam(struct ieee80211vap *vap,
2014178354Ssam	const struct ieee80211req *ireq)
2015138568Ssam{
2016178354Ssam	if (ireq->i_len != sizeof(vap->iv_roamparms))
2017178354Ssam		return EINVAL;
2018178354Ssam	/* XXX validate params */
2019178354Ssam	/* XXX? ENETRESET to push to device? */
2020178354Ssam	return copyin(ireq->i_data, vap->iv_roamparms,
2021178354Ssam	    sizeof(vap->iv_roamparms));
2022178354Ssam}
2023178354Ssam
2024178354Ssamstatic int
2025178354Ssamcheckrate(const struct ieee80211_rateset *rs, int rate)
2026178354Ssam{
2027178354Ssam	int i;
2028178354Ssam
2029178354Ssam	if (rate == IEEE80211_FIXED_RATE_NONE)
2030178354Ssam		return 1;
2031178354Ssam	for (i = 0; i < rs->rs_nrates; i++)
2032178354Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2033178354Ssam			return 1;
2034178354Ssam	return 0;
2035178354Ssam}
2036178354Ssam
2037178354Ssamstatic int
2038178354Ssamcheckmcs(int mcs)
2039178354Ssam{
2040178354Ssam	if (mcs == IEEE80211_FIXED_RATE_NONE)
2041178354Ssam		return 1;
2042178354Ssam	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2043178354Ssam		return 0;
2044178354Ssam	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2045178354Ssam}
2046178354Ssam
2047178354Ssamstatic __noinline int
2048178354Ssamieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2049178354Ssam	const struct ieee80211req *ireq)
2050178354Ssam{
2051178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2052178354Ssam	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2053178354Ssam	struct ieee80211_txparam *src, *dst;
2054178354Ssam	const struct ieee80211_rateset *rs;
2055178354Ssam	int error, i, changed;
2056178354Ssam
2057178354Ssam	if (ireq->i_len != sizeof(parms))
2058178354Ssam		return EINVAL;
2059178354Ssam	error = copyin(ireq->i_data, &parms, sizeof(parms));
2060178354Ssam	if (error != 0)
2061178354Ssam		return error;
2062178354Ssam	changed = 0;
2063178354Ssam	/* validate parameters and check if anything changed */
2064178354Ssam	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_11NA; i++) {
2065178354Ssam		if (isclr(ic->ic_modecaps, i))
2066178354Ssam			continue;
2067178354Ssam		src = &parms.params[i];
2068178354Ssam		dst = &vap->iv_txparms[i];
2069178354Ssam		rs = &ic->ic_sup_rates[i];
2070178354Ssam		if (src->ucastrate != dst->ucastrate) {
2071178354Ssam			if (!checkrate(rs, src->ucastrate))
2072178354Ssam				return EINVAL;
2073178354Ssam			changed++;
2074178354Ssam		}
2075178354Ssam		if (src->mcastrate != dst->mcastrate) {
2076178354Ssam			if (!checkrate(rs, src->mcastrate))
2077178354Ssam				return EINVAL;
2078178354Ssam			changed++;
2079178354Ssam		}
2080178354Ssam		if (src->mgmtrate != dst->mgmtrate) {
2081178354Ssam			if (!checkrate(rs, src->mgmtrate))
2082178354Ssam				return EINVAL;
2083178354Ssam			changed++;
2084178354Ssam		}
2085178354Ssam		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2086178354Ssam			changed++;
2087178354Ssam	}
2088178354Ssam	/* 11n parameters are handled differently */
2089178354Ssam	for (; i < IEEE80211_MODE_MAX; i++) {
2090178354Ssam		if (isclr(ic->ic_modecaps, i))
2091178354Ssam			continue;
2092178354Ssam		src = &parms.params[i];
2093178354Ssam		dst = &vap->iv_txparms[i];
2094178354Ssam		rs = &ic->ic_sup_rates[i == IEEE80211_MODE_11NA ?
2095178354Ssam		    IEEE80211_MODE_11A : IEEE80211_MODE_11G];
2096178354Ssam		if (src->ucastrate != dst->ucastrate) {
2097178354Ssam			if (!checkmcs(src->ucastrate) &&
2098178354Ssam			    !checkrate(rs, src->ucastrate))
2099178354Ssam				return EINVAL;
2100178354Ssam			changed++;
2101178354Ssam		}
2102178354Ssam		if (src->mcastrate != dst->mcastrate) {
2103178354Ssam			if (!checkmcs(src->mcastrate) &&
2104178354Ssam			    !checkrate(rs, src->mcastrate))
2105178354Ssam				return EINVAL;
2106178354Ssam			changed++;
2107178354Ssam		}
2108178354Ssam		if (src->mgmtrate != dst->mgmtrate) {
2109178354Ssam			if (!checkmcs(src->mgmtrate) &&
2110178354Ssam			    !checkrate(rs, src->mgmtrate))
2111178354Ssam				return EINVAL;
2112178354Ssam			changed++;
2113178354Ssam		}
2114178354Ssam		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2115178354Ssam			changed++;
2116178354Ssam	}
2117178354Ssam	if (changed) {
2118178354Ssam		/*
2119178354Ssam		 * Copy new parameters in place and notify the
2120178354Ssam		 * driver so it can push state to the device.
2121178354Ssam		 */
2122178354Ssam		for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
2123178354Ssam			if (isset(ic->ic_modecaps, i))
2124178354Ssam				vap->iv_txparms[i] = parms.params[i];
2125178354Ssam		}
2126178354Ssam		/* XXX could be more intelligent,
2127178354Ssam		   e.g. don't reset if setting not being used */
2128178354Ssam		return ENETRESET;
2129178354Ssam	}
2130178354Ssam	return 0;
2131178354Ssam}
2132178354Ssam
2133178354Ssam/*
2134178354Ssam * Application Information Element support.
2135178354Ssam */
2136178354Ssamstatic int
2137178354Ssamsetappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2138178354Ssam{
2139178354Ssam	struct ieee80211_appie *app = *aie;
2140178354Ssam	struct ieee80211_appie *napp;
2141138568Ssam	int error;
2142178354Ssam
2143178354Ssam	if (ireq->i_len == 0) {		/* delete any existing ie */
2144178354Ssam		if (app != NULL) {
2145178354Ssam			*aie = NULL;	/* XXX racey */
2146186302Ssam			free(app, M_80211_NODE_IE);
2147178354Ssam		}
2148178354Ssam		return 0;
2149178354Ssam	}
2150178354Ssam	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2151178354Ssam		return EINVAL;
2152178354Ssam	/*
2153178354Ssam	 * Allocate a new appie structure and copy in the user data.
2154178354Ssam	 * When done swap in the new structure.  Note that we do not
2155178354Ssam	 * guard against users holding a ref to the old structure;
2156178354Ssam	 * this must be handled outside this code.
2157178354Ssam	 *
2158178354Ssam	 * XXX bad bad bad
2159178354Ssam	 */
2160186302Ssam	napp = (struct ieee80211_appie *) malloc(
2161184210Sdes	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2162178354Ssam	if (napp == NULL)
2163178354Ssam		return ENOMEM;
2164178354Ssam	/* XXX holding ic lock */
2165178354Ssam	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2166178354Ssam	if (error) {
2167186302Ssam		free(napp, M_80211_NODE_IE);
2168178354Ssam		return error;
2169178354Ssam	}
2170178354Ssam	napp->ie_len = ireq->i_len;
2171178354Ssam	*aie = napp;
2172178354Ssam	if (app != NULL)
2173186302Ssam		free(app, M_80211_NODE_IE);
2174178354Ssam	return 0;
2175178354Ssam}
2176178354Ssam
2177178354Ssamstatic void
2178178354Ssamsetwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2179178354Ssam{
2180178354Ssam	/* validate data is present as best we can */
2181178354Ssam	if (space == 0 || 2+ie[1] > space)
2182178354Ssam		return;
2183178354Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2184178354Ssam		vap->iv_wpa_ie = ie;
2185178354Ssam	else if (ie[0] == IEEE80211_ELEMID_RSN)
2186178354Ssam		vap->iv_rsn_ie = ie;
2187178354Ssam}
2188178354Ssam
2189178354Ssamstatic __noinline int
2190178354Ssamieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2191178354Ssam	const struct ieee80211req *ireq, int fc0)
2192178354Ssam{
2193178354Ssam	int error;
2194178354Ssam
2195178354Ssam	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2196178354Ssam
2197178354Ssam	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2198178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
2199178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2200178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS) {
2201178354Ssam			error = EINVAL;
2202178354Ssam			break;
2203178354Ssam		}
2204178354Ssam		error = setappie(&vap->iv_appie_beacon, ireq);
2205178354Ssam		if (error == 0)
2206178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2207178354Ssam		break;
2208178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2209178354Ssam		error = setappie(&vap->iv_appie_proberesp, ireq);
2210178354Ssam		break;
2211178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2212178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2213178354Ssam			error = setappie(&vap->iv_appie_assocresp, ireq);
2214178354Ssam		else
2215178354Ssam			error = EINVAL;
2216178354Ssam		break;
2217178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2218178354Ssam		error = setappie(&vap->iv_appie_probereq, ireq);
2219178354Ssam		break;
2220178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2221178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA)
2222178354Ssam			error = setappie(&vap->iv_appie_assocreq, ireq);
2223178354Ssam		else
2224178354Ssam			error = EINVAL;
2225178354Ssam		break;
2226178354Ssam	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2227178354Ssam		error = setappie(&vap->iv_appie_wpa, ireq);
2228178354Ssam		if (error == 0) {
2229178354Ssam			/*
2230178354Ssam			 * Must split single blob of data into separate
2231178354Ssam			 * WPA and RSN ie's because they go in different
2232178354Ssam			 * locations in the mgt frames.
2233178354Ssam			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2234178354Ssam			 */
2235178354Ssam			vap->iv_wpa_ie = NULL;
2236178354Ssam			vap->iv_rsn_ie = NULL;
2237178354Ssam			if (vap->iv_appie_wpa != NULL) {
2238178354Ssam				struct ieee80211_appie *appie =
2239178354Ssam				    vap->iv_appie_wpa;
2240178354Ssam				uint8_t *data = appie->ie_data;
2241178354Ssam
2242178354Ssam				/* XXX ie length validate is painful, cheat */
2243178354Ssam				setwparsnie(vap, data, appie->ie_len);
2244178354Ssam				setwparsnie(vap, data + 2 + data[1],
2245178354Ssam				    appie->ie_len - (2 + data[1]));
2246178354Ssam			}
2247178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2248178354Ssam			    vap->iv_opmode == IEEE80211_M_IBSS) {
2249178354Ssam				/*
2250178354Ssam				 * Must rebuild beacon frame as the update
2251178354Ssam				 * mechanism doesn't handle WPA/RSN ie's.
2252178354Ssam				 * Could extend it but it doesn't normally
2253178354Ssam				 * change; this is just to deal with hostapd
2254178354Ssam				 * plumbing the ie after the interface is up.
2255178354Ssam				 */
2256178354Ssam				error = ENETRESET;
2257178354Ssam			}
2258178354Ssam		}
2259178354Ssam		break;
2260178354Ssam	default:
2261178354Ssam		error = EINVAL;
2262178354Ssam		break;
2263178354Ssam	}
2264178354Ssam	return error;
2265178354Ssam}
2266178354Ssam
2267178354Ssamstatic __noinline int
2268178354Ssamieee80211_ioctl_setappie(struct ieee80211vap *vap,
2269178354Ssam	const struct ieee80211req *ireq)
2270178354Ssam{
2271178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2272178354Ssam	int error;
2273178354Ssam	uint8_t fc0;
2274178354Ssam
2275178354Ssam	fc0 = ireq->i_val & 0xff;
2276178354Ssam	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2277178354Ssam		return EINVAL;
2278178354Ssam	/* NB: could check iv_opmode and reject but hardly worth the effort */
2279178354Ssam	IEEE80211_LOCK(ic);
2280178354Ssam	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2281178354Ssam	IEEE80211_UNLOCK(ic);
2282178354Ssam	return error;
2283178354Ssam}
2284178354Ssam
2285178354Ssamstatic __noinline int
2286178354Ssamieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2287178354Ssam{
2288178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2289178354Ssam	struct ieee80211_chanswitch_req csr;
2290178354Ssam	struct ieee80211_channel *c;
2291178354Ssam	int error;
2292178354Ssam
2293178354Ssam	if (ireq->i_len != sizeof(csr))
2294178354Ssam		return EINVAL;
2295178354Ssam	error = copyin(ireq->i_data, &csr, sizeof(csr));
2296178354Ssam	if (error != 0)
2297178354Ssam		return error;
2298178354Ssam	if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
2299178354Ssam		return EINVAL;
2300178354Ssam	c = ieee80211_find_channel(ic,
2301178354Ssam	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2302178354Ssam	if (c == NULL)
2303178354Ssam		return ENOENT;
2304178354Ssam	IEEE80211_LOCK(ic);
2305178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2306178354Ssam		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2307178354Ssam	else
2308178354Ssam		error = EBUSY;
2309178354Ssam	IEEE80211_UNLOCK(ic);
2310178354Ssam	return error;
2311178354Ssam}
2312178354Ssam
2313178354Ssamstatic __noinline int
2314178354Ssamieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2315178354Ssam{
2316178354Ssam#define	IEEE80211_IOC_SCAN_FLAGS \
2317178354Ssam	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2318178354Ssam	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2319178354Ssam	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2320178354Ssam	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2321178354Ssam	 IEEE80211_IOC_SCAN_CHECK)
2322178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2323178354Ssam	struct ieee80211_scan_req sr;		/* XXX off stack? */
2324178354Ssam	int error, i;
2325178354Ssam
2326178354Ssam	/* NB: parent must be running */
2327178354Ssam	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2328178354Ssam		return ENXIO;
2329178354Ssam
2330178354Ssam	if (ireq->i_len != sizeof(sr))
2331178354Ssam		return EINVAL;
2332178354Ssam	error = copyin(ireq->i_data, &sr, sizeof(sr));
2333178354Ssam	if (error != 0)
2334178354Ssam		return error;
2335178354Ssam	/* convert duration */
2336178354Ssam	if (sr.sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2337178354Ssam		sr.sr_duration = IEEE80211_SCAN_FOREVER;
2338178354Ssam	else {
2339178354Ssam		if (sr.sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2340178354Ssam		    sr.sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2341178354Ssam			return EINVAL;
2342178354Ssam		sr.sr_duration = msecs_to_ticks(sr.sr_duration);
2343178354Ssam		if (sr.sr_duration < 1)
2344178354Ssam			sr.sr_duration = 1;
2345178354Ssam	}
2346178354Ssam	/* convert min/max channel dwell */
2347178354Ssam	if (sr.sr_mindwell != 0) {
2348178354Ssam		sr.sr_mindwell = msecs_to_ticks(sr.sr_mindwell);
2349178354Ssam		if (sr.sr_mindwell < 1)
2350178354Ssam			sr.sr_mindwell = 1;
2351178354Ssam	}
2352178354Ssam	if (sr.sr_maxdwell != 0) {
2353178354Ssam		sr.sr_maxdwell = msecs_to_ticks(sr.sr_maxdwell);
2354178354Ssam		if (sr.sr_maxdwell < 1)
2355178354Ssam			sr.sr_maxdwell = 1;
2356178354Ssam	}
2357178354Ssam	/* NB: silently reduce ssid count to what is supported */
2358178354Ssam	if (sr.sr_nssid > IEEE80211_SCAN_MAX_SSID)
2359178354Ssam		sr.sr_nssid = IEEE80211_SCAN_MAX_SSID;
2360178354Ssam	for (i = 0; i < sr.sr_nssid; i++)
2361178354Ssam		if (sr.sr_ssid[i].len > IEEE80211_NWID_LEN)
2362178354Ssam			return EINVAL;
2363178354Ssam	/* cleanse flags just in case, could reject if invalid flags */
2364178354Ssam	sr.sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2365178354Ssam	/*
2366178354Ssam	 * Add an implicit NOPICK if the vap is not marked UP.  This
2367178354Ssam	 * allows applications to scan without joining a bss (or picking
2368178354Ssam	 * a channel and setting up a bss) and without forcing manual
2369178354Ssam	 * roaming mode--you just need to mark the parent device UP.
2370178354Ssam	 */
2371178354Ssam	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2372178354Ssam		sr.sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2373178354Ssam
2374178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2375178354Ssam	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2376178354Ssam	    __func__, sr.sr_flags,
2377178354Ssam	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2378178354Ssam	    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell, sr.sr_nssid);
2379178354Ssam	/*
2380178354Ssam	 * If we are in INIT state then the driver has never had a chance
2381178354Ssam	 * to setup hardware state to do a scan; we must use the state
2382178354Ssam	 * machine to get us up to the SCAN state but once we reach SCAN
2383178354Ssam	 * state we then want to use the supplied params.  Stash the
2384178354Ssam	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2385178354Ssam	 * state machines will recognize this and use the stashed params
2386178354Ssam	 * to issue the scan request.
2387178354Ssam	 *
2388178354Ssam	 * Otherwise just invoke the scan machinery directly.
2389178354Ssam	 */
2390178354Ssam	IEEE80211_LOCK(ic);
2391178354Ssam	if (vap->iv_state == IEEE80211_S_INIT) {
2392178354Ssam		/* NB: clobbers previous settings */
2393178354Ssam		vap->iv_scanreq_flags = sr.sr_flags;
2394178354Ssam		vap->iv_scanreq_duration = sr.sr_duration;
2395178354Ssam		vap->iv_scanreq_nssid = sr.sr_nssid;
2396178354Ssam		for (i = 0; i < sr.sr_nssid; i++) {
2397178354Ssam			vap->iv_scanreq_ssid[i].len = sr.sr_ssid[i].len;
2398178354Ssam			memcpy(vap->iv_scanreq_ssid[i].ssid, sr.sr_ssid[i].ssid,
2399178354Ssam			    sr.sr_ssid[i].len);
2400178354Ssam		}
2401178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2402178354Ssam		IEEE80211_UNLOCK(ic);
2403178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2404178354Ssam	} else {
2405178354Ssam		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2406178354Ssam		IEEE80211_UNLOCK(ic);
2407178354Ssam		/* XXX neeed error return codes */
2408178354Ssam		if (sr.sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2409178354Ssam			(void) ieee80211_check_scan(vap, sr.sr_flags,
2410178354Ssam			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2411178354Ssam			    sr.sr_nssid,
2412178354Ssam			    /* NB: cheat, we assume structures are compatible */
2413178354Ssam			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2414178354Ssam		} else {
2415178354Ssam			(void) ieee80211_start_scan(vap, sr.sr_flags,
2416178354Ssam			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2417178354Ssam			    sr.sr_nssid,
2418178354Ssam			    /* NB: cheat, we assume structures are compatible */
2419178354Ssam			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2420178354Ssam		}
2421178354Ssam	}
2422178354Ssam	return error;
2423178354Ssam#undef IEEE80211_IOC_SCAN_FLAGS
2424178354Ssam}
2425178354Ssam
2426178354Ssamstatic __noinline int
2427178354Ssamieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2428178354Ssam{
2429178354Ssam	struct ieee80211_node *ni;
2430178354Ssam	struct ieee80211req_sta_vlan vlan;
2431178354Ssam	int error;
2432178354Ssam
2433178354Ssam	if (ireq->i_len != sizeof(vlan))
2434178354Ssam		return EINVAL;
2435178354Ssam	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2436178354Ssam	if (error != 0)
2437178354Ssam		return error;
2438178354Ssam	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2439178354Ssam		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2440178354Ssam		    vlan.sv_macaddr);
2441178354Ssam		if (ni == NULL)
2442178354Ssam			return ENOENT;
2443178354Ssam	} else
2444178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
2445178354Ssam	ni->ni_vlan = vlan.sv_vlan;
2446178354Ssam	ieee80211_free_node(ni);
2447178354Ssam	return error;
2448178354Ssam}
2449178354Ssam
2450178354Ssamstatic int
2451178354Ssamisvap11g(const struct ieee80211vap *vap)
2452178354Ssam{
2453178354Ssam	const struct ieee80211_node *bss = vap->iv_bss;
2454178354Ssam	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2455178354Ssam	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2456178354Ssam}
2457178354Ssam
2458178354Ssamstatic int
2459178354Ssamisvapht(const struct ieee80211vap *vap)
2460178354Ssam{
2461178354Ssam	const struct ieee80211_node *bss = vap->iv_bss;
2462178354Ssam	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2463178354Ssam	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2464178354Ssam}
2465178354Ssam
2466178354Ssamstatic __noinline int
2467178354Ssamieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2468178354Ssam{
2469178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2470178354Ssam	int error;
2471138568Ssam	const struct ieee80211_authenticator *auth;
2472170530Ssam	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2473138568Ssam	char tmpssid[IEEE80211_NWID_LEN];
2474170530Ssam	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2475138568Ssam	struct ieee80211_key *k;
2476138568Ssam	u_int kid;
2477181455Simp	uint32_t flags;
2478138568Ssam
2479138568Ssam	error = 0;
2480138568Ssam	switch (ireq->i_type) {
2481138568Ssam	case IEEE80211_IOC_SSID:
2482138568Ssam		if (ireq->i_val != 0 ||
2483138568Ssam		    ireq->i_len > IEEE80211_NWID_LEN)
2484138568Ssam			return EINVAL;
2485138568Ssam		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2486138568Ssam		if (error)
2487116742Ssam			break;
2488178354Ssam		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2489178354Ssam		vap->iv_des_ssid[0].len = ireq->i_len;
2490178354Ssam		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2491178354Ssam		vap->iv_des_nssid = (ireq->i_len > 0);
2492178354Ssam		error = ENETRESET;
2493138568Ssam		break;
2494138568Ssam	case IEEE80211_IOC_WEP:
2495138568Ssam		switch (ireq->i_val) {
2496138568Ssam		case IEEE80211_WEP_OFF:
2497178354Ssam			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2498178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2499116742Ssam			break;
2500138568Ssam		case IEEE80211_WEP_ON:
2501178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2502178354Ssam			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2503116742Ssam			break;
2504138568Ssam		case IEEE80211_WEP_MIXED:
2505178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2506178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2507116742Ssam			break;
2508138568Ssam		}
2509178354Ssam		error = ENETRESET;
2510138568Ssam		break;
2511138568Ssam	case IEEE80211_IOC_WEPKEY:
2512138568Ssam		kid = (u_int) ireq->i_val;
2513138568Ssam		if (kid >= IEEE80211_WEP_NKID)
2514138568Ssam			return EINVAL;
2515178354Ssam		k = &vap->iv_nw_keys[kid];
2516138568Ssam		if (ireq->i_len == 0) {
2517138568Ssam			/* zero-len =>'s delete any existing key */
2518178354Ssam			(void) ieee80211_crypto_delkey(vap, k);
2519127648Ssam			break;
2520138568Ssam		}
2521138568Ssam		if (ireq->i_len > sizeof(tmpkey))
2522138568Ssam			return EINVAL;
2523138568Ssam		memset(tmpkey, 0, sizeof(tmpkey));
2524138568Ssam		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2525138568Ssam		if (error)
2526138568Ssam			break;
2527178354Ssam		ieee80211_key_update_begin(vap);
2528144960Ssam		k->wk_keyix = kid;	/* NB: force fixed key id */
2529178354Ssam		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2530144960Ssam		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2531138568Ssam			k->wk_keylen = ireq->i_len;
2532138568Ssam			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2533179395Ssam			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2534179395Ssam			if  (!ieee80211_crypto_setkey(vap, k))
2535127648Ssam				error = EINVAL;
2536138568Ssam		} else
2537138568Ssam			error = EINVAL;
2538178354Ssam		ieee80211_key_update_end(vap);
2539138568Ssam		break;
2540138568Ssam	case IEEE80211_IOC_WEPTXKEY:
2541138568Ssam		kid = (u_int) ireq->i_val;
2542139519Ssam		if (kid >= IEEE80211_WEP_NKID &&
2543170530Ssam		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2544138568Ssam			return EINVAL;
2545178354Ssam		vap->iv_def_txkey = kid;
2546138568Ssam		break;
2547138568Ssam	case IEEE80211_IOC_AUTHMODE:
2548138568Ssam		switch (ireq->i_val) {
2549138568Ssam		case IEEE80211_AUTH_WPA:
2550138568Ssam		case IEEE80211_AUTH_8021X:	/* 802.1x */
2551138568Ssam		case IEEE80211_AUTH_OPEN:	/* open */
2552138568Ssam		case IEEE80211_AUTH_SHARED:	/* shared-key */
2553138568Ssam		case IEEE80211_AUTH_AUTO:	/* auto */
2554138568Ssam			auth = ieee80211_authenticator_get(ireq->i_val);
2555138568Ssam			if (auth == NULL)
2556138568Ssam				return EINVAL;
2557127648Ssam			break;
2558116742Ssam		default:
2559138568Ssam			return EINVAL;
2560138568Ssam		}
2561138568Ssam		switch (ireq->i_val) {
2562138568Ssam		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2563178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2564138568Ssam			ireq->i_val = IEEE80211_AUTH_8021X;
2565127646Ssam			break;
2566138568Ssam		case IEEE80211_AUTH_OPEN:	/* open */
2567178354Ssam			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2568138568Ssam			break;
2569138568Ssam		case IEEE80211_AUTH_SHARED:	/* shared-key */
2570138568Ssam		case IEEE80211_AUTH_8021X:	/* 802.1x */
2571178354Ssam			vap->iv_flags &= ~IEEE80211_F_WPA;
2572138568Ssam			/* both require a key so mark the PRIVACY capability */
2573178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2574138568Ssam			break;
2575138568Ssam		case IEEE80211_AUTH_AUTO:	/* auto */
2576178354Ssam			vap->iv_flags &= ~IEEE80211_F_WPA;
2577138568Ssam			/* XXX PRIVACY handling? */
2578138568Ssam			/* XXX what's the right way to do this? */
2579138568Ssam			break;
2580116742Ssam		}
2581138568Ssam		/* NB: authenticator attach/detach happens on state change */
2582178354Ssam		vap->iv_bss->ni_authmode = ireq->i_val;
2583138568Ssam		/* XXX mixed/mode/usage? */
2584178354Ssam		vap->iv_auth = auth;
2585178354Ssam		error = ENETRESET;
2586116742Ssam		break;
2587138568Ssam	case IEEE80211_IOC_CHANNEL:
2588178354Ssam		error = ieee80211_ioctl_setchannel(vap, ireq);
2589138568Ssam		break;
2590138568Ssam	case IEEE80211_IOC_POWERSAVE:
2591138568Ssam		switch (ireq->i_val) {
2592138568Ssam		case IEEE80211_POWERSAVE_OFF:
2593178354Ssam			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2594178354Ssam				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2595178354Ssam				error = ERESTART;
2596116742Ssam			}
2597116742Ssam			break;
2598138568Ssam		case IEEE80211_POWERSAVE_ON:
2599178354Ssam			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2600178354Ssam				error = EOPNOTSUPP;
2601178354Ssam			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2602178354Ssam				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2603178354Ssam				error = ERESTART;
2604116742Ssam			}
2605116742Ssam			break;
2606138568Ssam		default:
2607138568Ssam			error = EINVAL;
2608116742Ssam			break;
2609138568Ssam		}
2610138568Ssam		break;
2611138568Ssam	case IEEE80211_IOC_POWERSAVESLEEP:
2612138568Ssam		if (ireq->i_val < 0)
2613138568Ssam			return EINVAL;
2614138568Ssam		ic->ic_lintval = ireq->i_val;
2615178354Ssam		error = ERESTART;
2616138568Ssam		break;
2617138568Ssam	case IEEE80211_IOC_RTSTHRESHOLD:
2618148292Ssam		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2619148292Ssam		      ireq->i_val <= IEEE80211_RTS_MAX))
2620138568Ssam			return EINVAL;
2621178354Ssam		vap->iv_rtsthreshold = ireq->i_val;
2622178354Ssam		error = ERESTART;
2623138568Ssam		break;
2624138568Ssam	case IEEE80211_IOC_PROTMODE:
2625138568Ssam		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2626138568Ssam			return EINVAL;
2627138568Ssam		ic->ic_protmode = ireq->i_val;
2628138568Ssam		/* NB: if not operating in 11g this can wait */
2629170530Ssam		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2630170530Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2631178354Ssam			error = ERESTART;
2632138568Ssam		break;
2633138568Ssam	case IEEE80211_IOC_TXPOWER:
2634138568Ssam		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2635178354Ssam			return EOPNOTSUPP;
2636170530Ssam		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2637170530Ssam		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2638138568Ssam			return EINVAL;
2639138568Ssam		ic->ic_txpowlimit = ireq->i_val;
2640178354Ssam		error = ERESTART;
2641138568Ssam		break;
2642138568Ssam	case IEEE80211_IOC_ROAMING:
2643138568Ssam		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2644138568Ssam		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2645138568Ssam			return EINVAL;
2646178354Ssam		vap->iv_roaming = ireq->i_val;
2647138568Ssam		/* XXXX reset? */
2648138568Ssam		break;
2649138568Ssam	case IEEE80211_IOC_PRIVACY:
2650138568Ssam		if (ireq->i_val) {
2651138568Ssam			/* XXX check for key state? */
2652178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2653138568Ssam		} else
2654178354Ssam			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2655178354Ssam		/* XXX ERESTART? */
2656138568Ssam		break;
2657138568Ssam	case IEEE80211_IOC_DROPUNENCRYPTED:
2658138568Ssam		if (ireq->i_val)
2659178354Ssam			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2660138568Ssam		else
2661178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2662178354Ssam		/* XXX ERESTART? */
2663138568Ssam		break;
2664138568Ssam	case IEEE80211_IOC_WPAKEY:
2665178354Ssam		error = ieee80211_ioctl_setkey(vap, ireq);
2666138568Ssam		break;
2667138568Ssam	case IEEE80211_IOC_DELKEY:
2668178354Ssam		error = ieee80211_ioctl_delkey(vap, ireq);
2669138568Ssam		break;
2670138568Ssam	case IEEE80211_IOC_MLME:
2671178354Ssam		error = ieee80211_ioctl_setmlme(vap, ireq);
2672138568Ssam		break;
2673138568Ssam	case IEEE80211_IOC_COUNTERMEASURES:
2674138568Ssam		if (ireq->i_val) {
2675178354Ssam			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2676178354Ssam				return EOPNOTSUPP;
2677178354Ssam			vap->iv_flags |= IEEE80211_F_COUNTERM;
2678138568Ssam		} else
2679178354Ssam			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2680178354Ssam		/* XXX ERESTART? */
2681138568Ssam		break;
2682138568Ssam	case IEEE80211_IOC_WPA:
2683138568Ssam		if (ireq->i_val > 3)
2684138568Ssam			return EINVAL;
2685138568Ssam		/* XXX verify ciphers available */
2686181455Simp		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2687138568Ssam		switch (ireq->i_val) {
2688138568Ssam		case 1:
2689181455Simp			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2690181455Simp				return EOPNOTSUPP;
2691181455Simp			flags |= IEEE80211_F_WPA1;
2692116742Ssam			break;
2693138568Ssam		case 2:
2694181455Simp			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2695181455Simp				return EOPNOTSUPP;
2696181455Simp			flags |= IEEE80211_F_WPA2;
2697116742Ssam			break;
2698138568Ssam		case 3:
2699181455Simp			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2700187060Ssam				return EOPNOTSUPP;
2701181455Simp			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2702127648Ssam			break;
2703181455Simp		default:	/*  Can't set any -> error */
2704181455Simp			return EOPNOTSUPP;
2705138568Ssam		}
2706181455Simp		vap->iv_flags = flags;
2707178354Ssam		error = ERESTART;	/* NB: can change beacon frame */
2708138568Ssam		break;
2709138568Ssam	case IEEE80211_IOC_WME:
2710138568Ssam		if (ireq->i_val) {
2711178354Ssam			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2712178354Ssam				return EOPNOTSUPP;
2713178354Ssam			ieee80211_syncflag(vap, IEEE80211_F_WME);
2714138568Ssam		} else
2715178354Ssam			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2716178354Ssam		error = ERESTART;	/* NB: can change beacon frame */
2717138568Ssam		break;
2718138568Ssam	case IEEE80211_IOC_HIDESSID:
2719138568Ssam		if (ireq->i_val)
2720178354Ssam			vap->iv_flags |= IEEE80211_F_HIDESSID;
2721138568Ssam		else
2722178354Ssam			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2723178354Ssam		error = ERESTART;		/* XXX ENETRESET? */
2724138568Ssam		break;
2725138568Ssam	case IEEE80211_IOC_APBRIDGE:
2726138568Ssam		if (ireq->i_val == 0)
2727178354Ssam			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2728138568Ssam		else
2729178354Ssam			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2730138568Ssam		break;
2731138568Ssam	case IEEE80211_IOC_BSSID:
2732138568Ssam		if (ireq->i_len != sizeof(tmpbssid))
2733138568Ssam			return EINVAL;
2734138568Ssam		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2735138568Ssam		if (error)
2736127648Ssam			break;
2737178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2738178354Ssam		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2739178354Ssam			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2740138568Ssam		else
2741178354Ssam			vap->iv_flags |= IEEE80211_F_DESBSSID;
2742178354Ssam		error = ENETRESET;
2743138568Ssam		break;
2744138568Ssam	case IEEE80211_IOC_CHANLIST:
2745178354Ssam		error = ieee80211_ioctl_setchanlist(vap, ireq);
2746138568Ssam		break;
2747178354Ssam#define	OLD_IEEE80211_IOC_SCAN_REQ	23
2748178354Ssam#ifdef OLD_IEEE80211_IOC_SCAN_REQ
2749178354Ssam	case OLD_IEEE80211_IOC_SCAN_REQ:
2750178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2751178354Ssam			"%s: active scan request\n", __func__);
2752178354Ssam		/*
2753178354Ssam		 * If we are in INIT state then the driver has never
2754178354Ssam		 * had a chance to setup hardware state to do a scan;
2755178354Ssam		 * use the state machine to get us up the SCAN state.
2756178354Ssam		 * Otherwise just invoke the scan machinery to start
2757178354Ssam		 * a one-time scan.
2758178354Ssam		 */
2759178354Ssam		if (vap->iv_state == IEEE80211_S_INIT)
2760178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2761178354Ssam		else
2762178354Ssam			(void) ieee80211_start_scan(vap,
2763178354Ssam				IEEE80211_SCAN_ACTIVE |
2764178354Ssam				IEEE80211_SCAN_NOPICK |
2765178354Ssam				IEEE80211_SCAN_ONCE,
2766178354Ssam				IEEE80211_SCAN_FOREVER, 0, 0,
2767178354Ssam				/* XXX use ioctl params */
2768178354Ssam				vap->iv_des_nssid, vap->iv_des_ssid);
2769178354Ssam		break;
2770178354Ssam#endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2771138568Ssam	case IEEE80211_IOC_SCAN_REQ:
2772178354Ssam		error = ieee80211_ioctl_scanreq(vap, ireq);
2773116742Ssam		break;
2774178354Ssam	case IEEE80211_IOC_SCAN_CANCEL:
2775178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2776178354Ssam		    "%s: cancel scan\n", __func__);
2777178354Ssam		ieee80211_cancel_scan(vap);
2778178354Ssam		break;
2779178354Ssam	case IEEE80211_IOC_HTCONF:
2780178354Ssam		if (ireq->i_val & 1)
2781178354Ssam			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_HT);
2782178354Ssam		else
2783178354Ssam			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_HT);
2784178354Ssam		if (ireq->i_val & 2)
2785178354Ssam			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_USEHT40);
2786178354Ssam		else
2787178354Ssam			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_USEHT40);
2788178354Ssam		error = ENETRESET;
2789178354Ssam		break;
2790138568Ssam	case IEEE80211_IOC_ADDMAC:
2791138568Ssam	case IEEE80211_IOC_DELMAC:
2792178354Ssam		error = ieee80211_ioctl_macmac(vap, ireq);
2793138568Ssam		break;
2794138568Ssam	case IEEE80211_IOC_MACCMD:
2795178354Ssam		error = ieee80211_ioctl_setmaccmd(vap, ireq);
2796138568Ssam		break;
2797157172Ssam	case IEEE80211_IOC_STA_STATS:
2798178354Ssam		error = ieee80211_ioctl_setstastats(vap, ireq);
2799157172Ssam		break;
2800138568Ssam	case IEEE80211_IOC_STA_TXPOW:
2801178354Ssam		error = ieee80211_ioctl_setstatxpow(vap, ireq);
2802138568Ssam		break;
2803138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
2804138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
2805138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
2806138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
2807138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
2808138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2809178354Ssam		error = ieee80211_ioctl_setwmeparam(vap, ireq);
2810138568Ssam		break;
2811138568Ssam	case IEEE80211_IOC_DTIM_PERIOD:
2812178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2813178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS)
2814138568Ssam			return EINVAL;
2815138568Ssam		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
2816138568Ssam		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2817178354Ssam			vap->iv_dtim_period = ireq->i_val;
2818138568Ssam			error = ENETRESET;		/* requires restart */
2819138568Ssam		} else
2820138568Ssam			error = EINVAL;
2821138568Ssam		break;
2822138568Ssam	case IEEE80211_IOC_BEACON_INTERVAL:
2823178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2824178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS)
2825138568Ssam			return EINVAL;
2826138568Ssam		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
2827138568Ssam		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2828148843Ssam			ic->ic_bintval = ireq->i_val;
2829138568Ssam			error = ENETRESET;		/* requires restart */
2830138568Ssam		} else
2831138568Ssam			error = EINVAL;
2832138568Ssam		break;
2833147794Ssam	case IEEE80211_IOC_PUREG:
2834147794Ssam		if (ireq->i_val)
2835178354Ssam			vap->iv_flags |= IEEE80211_F_PUREG;
2836147794Ssam		else
2837178354Ssam			vap->iv_flags &= ~IEEE80211_F_PUREG;
2838147794Ssam		/* NB: reset only if we're operating on an 11g channel */
2839178354Ssam		if (isvap11g(vap))
2840147794Ssam			error = ENETRESET;
2841147794Ssam		break;
2842170530Ssam	case IEEE80211_IOC_FF:
2843170530Ssam		if (ireq->i_val) {
2844178354Ssam			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
2845178354Ssam				return EOPNOTSUPP;
2846178354Ssam			vap->iv_flags |= IEEE80211_F_FF;
2847170530Ssam		} else
2848178354Ssam			vap->iv_flags &= ~IEEE80211_F_FF;
2849178354Ssam		error = ERESTART;
2850170530Ssam		break;
2851170530Ssam	case IEEE80211_IOC_TURBOP:
2852170530Ssam		if (ireq->i_val) {
2853178354Ssam			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
2854178354Ssam				return EOPNOTSUPP;
2855178354Ssam			vap->iv_flags |= IEEE80211_F_TURBOP;
2856170530Ssam		} else
2857178354Ssam			vap->iv_flags &= ~IEEE80211_F_TURBOP;
2858170530Ssam		error = ENETRESET;
2859170530Ssam		break;
2860170530Ssam	case IEEE80211_IOC_BGSCAN:
2861170530Ssam		if (ireq->i_val) {
2862178354Ssam			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
2863178354Ssam				return EOPNOTSUPP;
2864178354Ssam			vap->iv_flags |= IEEE80211_F_BGSCAN;
2865170530Ssam		} else
2866178354Ssam			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
2867170530Ssam		break;
2868170530Ssam	case IEEE80211_IOC_BGSCAN_IDLE:
2869170530Ssam		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
2870178354Ssam			vap->iv_bgscanidle = ireq->i_val*hz/1000;
2871170530Ssam		else
2872170530Ssam			error = EINVAL;
2873170530Ssam		break;
2874170530Ssam	case IEEE80211_IOC_BGSCAN_INTERVAL:
2875170530Ssam		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
2876178354Ssam			vap->iv_bgscanintvl = ireq->i_val*hz;
2877170530Ssam		else
2878170530Ssam			error = EINVAL;
2879170530Ssam		break;
2880170530Ssam	case IEEE80211_IOC_SCANVALID:
2881170530Ssam		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
2882178354Ssam			vap->iv_scanvalid = ireq->i_val*hz;
2883170530Ssam		else
2884170530Ssam			error = EINVAL;
2885170530Ssam		break;
2886148292Ssam	case IEEE80211_IOC_FRAGTHRESHOLD:
2887178354Ssam		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
2888148292Ssam		    ireq->i_val != IEEE80211_FRAG_MAX)
2889178354Ssam			return EOPNOTSUPP;
2890148292Ssam		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
2891148292Ssam		      ireq->i_val <= IEEE80211_FRAG_MAX))
2892148292Ssam			return EINVAL;
2893178354Ssam		vap->iv_fragthreshold = ireq->i_val;
2894178354Ssam		error = ERESTART;
2895148292Ssam		break;
2896153421Ssam	case IEEE80211_IOC_BURST:
2897153421Ssam		if (ireq->i_val) {
2898178354Ssam			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
2899178354Ssam				return EOPNOTSUPP;
2900178354Ssam			ieee80211_syncflag(vap, IEEE80211_F_BURST);
2901153421Ssam		} else
2902178354Ssam			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
2903178354Ssam		error = ERESTART;
2904153421Ssam		break;
2905160686Ssam	case IEEE80211_IOC_BMISSTHRESHOLD:
2906160686Ssam		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
2907160686Ssam		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
2908160686Ssam			return EINVAL;
2909178354Ssam		vap->iv_bmissthreshold = ireq->i_val;
2910178354Ssam		error = ERESTART;
2911160686Ssam		break;
2912170530Ssam	case IEEE80211_IOC_CURCHAN:
2913178354Ssam		error = ieee80211_ioctl_setcurchan(vap, ireq);
2914170530Ssam		break;
2915170530Ssam	case IEEE80211_IOC_SHORTGI:
2916170530Ssam		if (ireq->i_val) {
2917170530Ssam#define	IEEE80211_HTCAP_SHORTGI \
2918170530Ssam	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
2919178354Ssam			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
2920170530Ssam				return EINVAL;
2921170530Ssam			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
2922178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI20;
2923170530Ssam			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
2924178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI40;
2925170530Ssam#undef IEEE80211_HTCAP_SHORTGI
2926170530Ssam		} else
2927178354Ssam			vap->iv_flags_ext &=
2928170530Ssam			    ~(IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40);
2929178354Ssam		error = ERESTART;
2930170530Ssam		break;
2931170530Ssam	case IEEE80211_IOC_AMPDU:
2932178354Ssam		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
2933178354Ssam			return EINVAL;
2934178354Ssam		if (ireq->i_val & 1)
2935178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
2936178354Ssam		else
2937178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_TX;
2938178354Ssam		if (ireq->i_val & 2)
2939178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
2940178354Ssam		else
2941178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_RX;
2942170530Ssam		/* NB: reset only if we're operating on an 11n channel */
2943178354Ssam		if (isvapht(vap))
2944178354Ssam			error = ERESTART;
2945170530Ssam		break;
2946170530Ssam	case IEEE80211_IOC_AMPDU_LIMIT:
2947178354Ssam		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
2948178354Ssam		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
2949178354Ssam			return EINVAL;
2950178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2951178354Ssam			vap->iv_ampdu_rxmax = ireq->i_val;
2952178354Ssam		else
2953178354Ssam			vap->iv_ampdu_limit = ireq->i_val;
2954178354Ssam		error = ERESTART;
2955170530Ssam		break;
2956170530Ssam	case IEEE80211_IOC_AMPDU_DENSITY:
2957178354Ssam		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
2958178354Ssam		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
2959178354Ssam			return EINVAL;
2960178354Ssam		vap->iv_ampdu_density = ireq->i_val;
2961178354Ssam		error = ERESTART;
2962170530Ssam		break;
2963170530Ssam	case IEEE80211_IOC_AMSDU:
2964178354Ssam		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
2965178354Ssam			return EINVAL;
2966178354Ssam		if (ireq->i_val & 1)
2967178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
2968178354Ssam		else
2969178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_TX;
2970178354Ssam		if (ireq->i_val & 2)
2971178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
2972178354Ssam		else
2973178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_RX;
2974170530Ssam		/* NB: reset only if we're operating on an 11n channel */
2975178354Ssam		if (isvapht(vap))
2976178354Ssam			error = ERESTART;
2977170530Ssam		break;
2978170530Ssam	case IEEE80211_IOC_AMSDU_LIMIT:
2979170530Ssam		/* XXX validate */
2980178354Ssam		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
2981170530Ssam		break;
2982170530Ssam	case IEEE80211_IOC_PUREN:
2983170530Ssam		if (ireq->i_val) {
2984178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
2985170530Ssam				return EINVAL;
2986178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_PUREN;
2987170530Ssam		} else
2988178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_PUREN;
2989170530Ssam		/* NB: reset only if we're operating on an 11n channel */
2990178354Ssam		if (isvapht(vap))
2991178354Ssam			error = ERESTART;
2992170530Ssam		break;
2993170530Ssam	case IEEE80211_IOC_DOTH:
2994170530Ssam		if (ireq->i_val) {
2995170530Ssam#if 0
2996170530Ssam			/* XXX no capability */
2997178354Ssam			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
2998178354Ssam				return EOPNOTSUPP;
2999170530Ssam#endif
3000178354Ssam			vap->iv_flags |= IEEE80211_F_DOTH;
3001170530Ssam		} else
3002178354Ssam			vap->iv_flags &= ~IEEE80211_F_DOTH;
3003178354Ssam		error = ENETRESET;
3004170530Ssam		break;
3005178354Ssam	case IEEE80211_IOC_REGDOMAIN:
3006178354Ssam		error = ieee80211_ioctl_setregdomain(vap, ireq);
3007178354Ssam		break;
3008178354Ssam	case IEEE80211_IOC_ROAM:
3009178354Ssam		error = ieee80211_ioctl_setroam(vap, ireq);
3010178354Ssam		break;
3011178354Ssam	case IEEE80211_IOC_TXPARAMS:
3012178354Ssam		error = ieee80211_ioctl_settxparams(vap, ireq);
3013178354Ssam		break;
3014170530Ssam	case IEEE80211_IOC_HTCOMPAT:
3015170530Ssam		if (ireq->i_val) {
3016178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
3017178354Ssam				return EOPNOTSUPP;
3018178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_HTCOMPAT;
3019170530Ssam		} else
3020178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_HTCOMPAT;
3021170530Ssam		/* NB: reset only if we're operating on an 11n channel */
3022178354Ssam		if (isvapht(vap))
3023178354Ssam			error = ERESTART;
3024170530Ssam		break;
3025178354Ssam	case IEEE80211_IOC_DWDS:
3026178354Ssam		if (ireq->i_val) {
3027178354Ssam			/* NB: DWDS only makes sense for WDS-capable devices */
3028178354Ssam			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3029178354Ssam				return EOPNOTSUPP;
3030178354Ssam			/* NB: DWDS is used only with ap+sta vaps */
3031178354Ssam			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3032178354Ssam			    vap->iv_opmode != IEEE80211_M_STA)
3033178354Ssam				return EINVAL;
3034178354Ssam			vap->iv_flags |= IEEE80211_F_DWDS;
3035178354Ssam		} else
3036178354Ssam			vap->iv_flags &= ~IEEE80211_F_DWDS;
3037178354Ssam		break;
3038172062Ssam	case IEEE80211_IOC_INACTIVITY:
3039172062Ssam		if (ireq->i_val)
3040178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3041172062Ssam		else
3042178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3043172062Ssam		break;
3044178354Ssam	case IEEE80211_IOC_APPIE:
3045178354Ssam		error = ieee80211_ioctl_setappie(vap, ireq);
3046178354Ssam		break;
3047178354Ssam	case IEEE80211_IOC_WPS:
3048178354Ssam		if (ireq->i_val) {
3049178354Ssam			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3050178354Ssam				return EOPNOTSUPP;
3051178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3052178354Ssam		} else
3053178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3054178354Ssam		break;
3055178354Ssam	case IEEE80211_IOC_TSN:
3056178354Ssam		if (ireq->i_val) {
3057178354Ssam			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3058178354Ssam				return EOPNOTSUPP;
3059178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3060178354Ssam		} else
3061178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3062178354Ssam		break;
3063178354Ssam	case IEEE80211_IOC_CHANSWITCH:
3064178354Ssam		error = ieee80211_ioctl_chanswitch(vap, ireq);
3065178354Ssam		break;
3066178354Ssam	case IEEE80211_IOC_DFS:
3067178354Ssam		if (ireq->i_val) {
3068178354Ssam			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3069178354Ssam				return EOPNOTSUPP;
3070178354Ssam			/* NB: DFS requires 11h support */
3071178354Ssam			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3072178354Ssam				return EINVAL;
3073178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3074178354Ssam		} else
3075178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3076178354Ssam		break;
3077178354Ssam	case IEEE80211_IOC_DOTD:
3078178354Ssam		if (ireq->i_val)
3079178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3080178354Ssam		else
3081178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3082178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA)
3083178354Ssam			error = ENETRESET;
3084178354Ssam		break;
3085173273Ssam	case IEEE80211_IOC_HTPROTMODE:
3086173273Ssam		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3087173273Ssam			return EINVAL;
3088173273Ssam		ic->ic_htprotmode = ireq->i_val ?
3089173273Ssam		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3090173273Ssam		/* NB: if not operating in 11n this can wait */
3091178354Ssam		if (isvapht(vap))
3092173273Ssam			error = ERESTART;
3093173273Ssam		break;
3094178354Ssam	case IEEE80211_IOC_STA_VLAN:
3095178354Ssam		error = ieee80211_ioctl_setstavlan(vap, ireq);
3096173273Ssam		break;
3097183255Ssam	case IEEE80211_IOC_SMPS:
3098183255Ssam		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3099183255Ssam		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3100183255Ssam			return EINVAL;
3101183255Ssam		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3102183255Ssam		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3103183255Ssam			return EOPNOTSUPP;
3104183255Ssam		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3105183255Ssam			ireq->i_val;
3106183255Ssam		/* NB: if not operating in 11n this can wait */
3107183255Ssam		if (isvapht(vap))
3108183255Ssam			error = ERESTART;
3109183255Ssam		break;
3110183256Ssam	case IEEE80211_IOC_RIFS:
3111183256Ssam		if (ireq->i_val != 0) {
3112183256Ssam			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3113183256Ssam				return EOPNOTSUPP;
3114183256Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RIFS;
3115183256Ssam		} else
3116183256Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RIFS;
3117183256Ssam		/* NB: if not operating in 11n this can wait */
3118183256Ssam		if (isvapht(vap))
3119183256Ssam			error = ERESTART;
3120183256Ssam		break;
3121186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
3122186904Ssam	case IEEE80211_IOC_TDMA_SLOT:
3123186904Ssam	case IEEE80211_IOC_TDMA_SLOTCNT:
3124186904Ssam	case IEEE80211_IOC_TDMA_SLOTLEN:
3125186904Ssam	case IEEE80211_IOC_TDMA_BINTERVAL:
3126186904Ssam		error = ieee80211_tdma_ioctl_set80211(vap, ireq);
3127186904Ssam		break;
3128186904Ssam#endif
3129138568Ssam	default:
3130138568Ssam		error = EINVAL;
3131138568Ssam		break;
3132138568Ssam	}
3133178354Ssam	/*
3134178354Ssam	 * The convention is that ENETRESET means an operation
3135178354Ssam	 * requires a complete re-initialization of the device (e.g.
3136178354Ssam	 * changing something that affects the association state).
3137178354Ssam	 * ERESTART means the request may be handled with only a
3138178354Ssam	 * reload of the hardware state.  We hand ERESTART requests
3139178354Ssam	 * to the iv_reset callback so the driver can decide.  If
3140178354Ssam	 * a device does not fillin iv_reset then it defaults to one
3141178354Ssam	 * that returns ENETRESET.  Otherwise a driver may return
3142178354Ssam	 * ENETRESET (in which case a full reset will be done) or
3143178354Ssam	 * 0 to mean there's no need to do anything (e.g. when the
3144178354Ssam	 * change has no effect on the driver/device).
3145178354Ssam	 */
3146178354Ssam	if (error == ERESTART)
3147178354Ssam		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3148178354Ssam		    vap->iv_reset(vap, ireq->i_type) : 0;
3149178354Ssam	if (error == ENETRESET) {
3150178354Ssam		/* XXX need to re-think AUTO handling */
3151178354Ssam		if (IS_UP_AUTO(vap))
3152178354Ssam			ieee80211_init(vap);
3153178354Ssam		error = 0;
3154178354Ssam	}
3155138568Ssam	return error;
3156138568Ssam}
3157138568Ssam
3158178354Ssam/*
3159178354Ssam * Rebuild the parent's multicast address list after an add/del
3160178354Ssam * of a multicast address for a vap.  We have no way to tell
3161178354Ssam * what happened above to optimize the work so we purge the entire
3162178354Ssam * list and rebuild from scratch.  This is way expensive.
3163178354Ssam * Note also the half-baked workaround for if_addmulti calling
3164178354Ssam * back to the parent device; there's no way to insert mcast
3165178354Ssam * entries quietly and/or cheaply.
3166178354Ssam */
3167178354Ssamstatic void
3168178354Ssamieee80211_ioctl_updatemulti(struct ieee80211com *ic)
3169178354Ssam{
3170178354Ssam	struct ifnet *parent = ic->ic_ifp;
3171178354Ssam	struct ieee80211vap *vap;
3172178354Ssam	void *ioctl;
3173178354Ssam
3174178354Ssam	IEEE80211_LOCK(ic);
3175178354Ssam	if_purgemaddrs(parent);
3176178354Ssam	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3177178354Ssam	parent->if_ioctl = NULL;
3178178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3179178354Ssam		struct ifnet *ifp = vap->iv_ifp;
3180178354Ssam		struct ifmultiaddr *ifma;
3181178354Ssam
3182178354Ssam		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
3183178354Ssam			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3184178354Ssam	}
3185178354Ssam	parent->if_ioctl = ioctl;
3186178354Ssam
3187178354Ssam	ic->ic_update_mcast(ic->ic_ifp);
3188178354Ssam	IEEE80211_UNLOCK(ic);
3189178354Ssam}
3190178354Ssam
3191138568Ssamint
3192178354Ssamieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3193138568Ssam{
3194178354Ssam	struct ieee80211vap *vap;
3195178354Ssam	struct ieee80211com *ic;
3196138568Ssam	int error = 0;
3197138568Ssam	struct ifreq *ifr;
3198138568Ssam	struct ifaddr *ifa;			/* XXX */
3199138568Ssam
3200178354Ssam	vap = ifp->if_softc;
3201178354Ssam	if (vap == NULL) {
3202178354Ssam		/*
3203178354Ssam		 * During detach we clear the backpointer in the softc
3204178354Ssam		 * so any ioctl request through the ifnet that arrives
3205178354Ssam		 * before teardown is ignored/rejected.  In particular
3206178354Ssam		 * this hack handles destroying a vap used by an app
3207178354Ssam		 * like wpa_supplicant that will respond to the vap
3208178354Ssam		 * being forced into INIT state by immediately trying
3209178354Ssam		 * to force it back up.  We can yank this hack if/when
3210178354Ssam		 * we can destroy the ifnet before cleaning up vap state.
3211178354Ssam		 */
3212178354Ssam		return ENXIO;
3213178354Ssam	}
3214138568Ssam	switch (cmd) {
3215178354Ssam	case SIOCSIFFLAGS:
3216178354Ssam		ic = vap->iv_ic;
3217178354Ssam		IEEE80211_LOCK(ic);
3218178354Ssam		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3219178354Ssam		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3220178354Ssam		if (ifp->if_flags & IFF_UP) {
3221178354Ssam			/*
3222178354Ssam			 * Bring ourself up unless we're already operational.
3223178354Ssam			 * If we're the first vap and the parent is not up
3224178354Ssam			 * then it will automatically be brought up as a
3225178354Ssam			 * side-effect of bringing ourself up.
3226178354Ssam			 */
3227178354Ssam			if (vap->iv_state == IEEE80211_S_INIT)
3228178354Ssam				ieee80211_start_locked(vap);
3229178354Ssam		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3230178354Ssam			/*
3231178354Ssam			 * Stop ourself.  If we are the last vap to be
3232178354Ssam			 * marked down the parent will also be taken down.
3233178354Ssam			 */
3234178354Ssam			ieee80211_stop_locked(vap);
3235178354Ssam		}
3236178354Ssam		IEEE80211_UNLOCK(ic);
3237187678Sthompsa		/* Wait for parent ioctl handler if it was queued */
3238187678Sthompsa		taskqueue_drain(taskqueue_thread, &ic->ic_parent_task);
3239178354Ssam		break;
3240178354Ssam	case SIOCADDMULTI:
3241178354Ssam	case SIOCDELMULTI:
3242178354Ssam		ieee80211_ioctl_updatemulti(vap->iv_ic);
3243178354Ssam		break;
3244138568Ssam	case SIOCSIFMEDIA:
3245138568Ssam	case SIOCGIFMEDIA:
3246178354Ssam		ifr = (struct ifreq *)data;
3247178354Ssam		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3248138568Ssam		break;
3249138568Ssam	case SIOCG80211:
3250178354Ssam		error = ieee80211_ioctl_get80211(vap, cmd,
3251138568Ssam				(struct ieee80211req *) data);
3252138568Ssam		break;
3253138568Ssam	case SIOCS80211:
3254164033Srwatson		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3255138568Ssam		if (error == 0)
3256178354Ssam			error = ieee80211_ioctl_set80211(vap, cmd,
3257138568Ssam					(struct ieee80211req *) data);
3258138568Ssam		break;
3259121180Ssam	case SIOCG80211STATS:
3260121180Ssam		ifr = (struct ifreq *)data;
3261178354Ssam		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3262121180Ssam		break;
3263124457Ssam	case SIOCSIFMTU:
3264124457Ssam		ifr = (struct ifreq *)data;
3265124457Ssam		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3266124457Ssam		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3267124457Ssam			error = EINVAL;
3268124457Ssam		else
3269124457Ssam			ifp->if_mtu = ifr->ifr_mtu;
3270124457Ssam		break;
3271127646Ssam	case SIOCSIFADDR:
3272127646Ssam		/*
3273127646Ssam		 * XXX Handle this directly so we can supress if_init calls.
3274127646Ssam		 * XXX This should be done in ether_ioctl but for the moment
3275127646Ssam		 * XXX there are too many other parts of the system that
3276127646Ssam		 * XXX set IFF_UP and so supress if_init being called when
3277127646Ssam		 * XXX it should be.
3278127646Ssam		 */
3279127646Ssam		ifa = (struct ifaddr *) data;
3280127646Ssam		switch (ifa->ifa_addr->sa_family) {
3281127646Ssam#ifdef INET
3282127646Ssam		case AF_INET:
3283127646Ssam			if ((ifp->if_flags & IFF_UP) == 0) {
3284127646Ssam				ifp->if_flags |= IFF_UP;
3285127646Ssam				ifp->if_init(ifp->if_softc);
3286127646Ssam			}
3287127646Ssam			arp_ifinit(ifp, ifa);
3288127646Ssam			break;
3289127646Ssam#endif
3290127646Ssam#ifdef IPX
3291127646Ssam		/*
3292127646Ssam		 * XXX - This code is probably wrong,
3293127646Ssam		 *	 but has been copied many times.
3294127646Ssam		 */
3295127646Ssam		case AF_IPX: {
3296127646Ssam			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3297127646Ssam
3298127646Ssam			if (ipx_nullhost(*ina))
3299147256Sbrooks				ina->x_host = *(union ipx_host *)
3300152315Sru				    IF_LLADDR(ifp);
3301127646Ssam			else
3302127646Ssam				bcopy((caddr_t) ina->x_host.c_host,
3303152315Sru				      (caddr_t) IF_LLADDR(ifp),
3304147256Sbrooks				      ETHER_ADDR_LEN);
3305127646Ssam			/* fall thru... */
3306127646Ssam		}
3307127646Ssam#endif
3308127646Ssam		default:
3309127646Ssam			if ((ifp->if_flags & IFF_UP) == 0) {
3310127646Ssam				ifp->if_flags |= IFF_UP;
3311127646Ssam				ifp->if_init(ifp->if_softc);
3312127646Ssam			}
3313127646Ssam			break;
3314127646Ssam		}
3315127646Ssam		break;
3316178354Ssam	/* Pass NDIS ioctls up to the driver */
3317178354Ssam	case SIOCGDRVSPEC:
3318178354Ssam	case SIOCSDRVSPEC:
3319178354Ssam	case SIOCGPRIVATE_0: {
3320178354Ssam		struct ifnet *parent = vap->iv_ic->ic_ifp;
3321178354Ssam		error = parent->if_ioctl(parent, cmd, data);
3322178354Ssam		break;
3323178354Ssam	}
3324116742Ssam	default:
3325116742Ssam		error = ether_ioctl(ifp, cmd, data);
3326116742Ssam		break;
3327116742Ssam	}
3328116742Ssam	return error;
3329116742Ssam}
3330