ieee80211_ioctl.c revision 188533
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 188533 2009-02-12 18:57:18Z 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;
699187801Ssam	int maxchans, error;
700178354Ssam
701187801Ssam	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
702187801Ssam	    sizeof(struct ieee80211_channel));
703187801Ssam	/* NB: require 1 so we know ic_nchans is accessible */
704187801Ssam	if (maxchans < 1)
705178354Ssam		return EINVAL;
706187801Ssam	/* constrain max request size, 2K channels is ~24Kbytes */
707187801Ssam	if (maxchans > 2048)
708187801Ssam		maxchans = 2048;
709187801Ssam	dc = (struct ieee80211_devcaps_req *)
710187801Ssam	    malloc(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP, M_NOWAIT | M_ZERO);
711178354Ssam	if (dc == NULL)
712178354Ssam		return ENOMEM;
713178354Ssam	dc->dc_drivercaps = ic->ic_caps;
714178354Ssam	dc->dc_cryptocaps = ic->ic_cryptocaps;
715178354Ssam	dc->dc_htcaps = ic->ic_htcaps;
716178354Ssam	ci = &dc->dc_chaninfo;
717187801Ssam	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
718187835Ssam	KASSERT(ci->ic_nchans <= maxchans,
719187835Ssam	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
720178354Ssam	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
721187801Ssam	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
722186302Ssam	free(dc, M_TEMP);
723178354Ssam	return error;
724178354Ssam}
725178354Ssam
726178354Ssamstatic __noinline int
727178354Ssamieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
728178354Ssam{
729178354Ssam	struct ieee80211_node *ni;
730178354Ssam	struct ieee80211req_sta_vlan vlan;
731178354Ssam	int error;
732178354Ssam
733178354Ssam	if (ireq->i_len != sizeof(vlan))
734178354Ssam		return EINVAL;
735178354Ssam	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
736178354Ssam	if (error != 0)
737178354Ssam		return error;
738178354Ssam	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
739178354Ssam		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
740178354Ssam		    vlan.sv_macaddr);
741178354Ssam		if (ni == NULL)
742178354Ssam			return ENOENT;
743178354Ssam	} else
744178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
745178354Ssam	vlan.sv_vlan = ni->ni_vlan;
746178354Ssam	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
747178354Ssam	ieee80211_free_node(ni);
748178354Ssam	return error;
749178354Ssam}
750178354Ssam
751170530Ssam/*
752143110Swpaul * When building the kernel with -O2 on the i386 architecture, gcc
753143110Swpaul * seems to want to inline this function into ieee80211_ioctl()
754143110Swpaul * (which is the only routine that calls it). When this happens,
755143110Swpaul * ieee80211_ioctl() ends up consuming an additional 2K of stack
756143110Swpaul * space. (Exactly why it needs so much is unclear.) The problem
757143110Swpaul * is that it's possible for ieee80211_ioctl() to invoke other
758143110Swpaul * routines (including driver init functions) which could then find
759143110Swpaul * themselves perilously close to exhausting the stack.
760143110Swpaul *
761143110Swpaul * To avoid this, we deliberately prevent gcc from inlining this
762143110Swpaul * routine. Another way to avoid this is to use less agressive
763143110Swpaul * optimization when compiling this file (i.e. -O instead of -O2)
764143110Swpaul * but special-casing the compilation of this one module in the
765143110Swpaul * build system would be awkward.
766143110Swpaul */
767178354Ssamstatic __noinline int
768178354Ssamieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
769178354Ssam    struct ieee80211req *ireq)
770138568Ssam{
771178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
772178354Ssam	struct ieee80211com *ic = vap->iv_ic;
773178354Ssam	u_int kid, len;
774170530Ssam	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
775116742Ssam	char tmpssid[IEEE80211_NWID_LEN];
776178354Ssam	int error = 0;
777116742Ssam
778138568Ssam	switch (ireq->i_type) {
779138568Ssam	case IEEE80211_IOC_SSID:
780178354Ssam		switch (vap->iv_state) {
781138568Ssam		case IEEE80211_S_INIT:
782138568Ssam		case IEEE80211_S_SCAN:
783178354Ssam			ireq->i_len = vap->iv_des_ssid[0].len;
784178354Ssam			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
785138568Ssam			break;
786138568Ssam		default:
787178354Ssam			ireq->i_len = vap->iv_bss->ni_esslen;
788178354Ssam			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
789138568Ssam			break;
790138568Ssam		}
791138568Ssam		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
792116742Ssam		break;
793138568Ssam	case IEEE80211_IOC_NUMSSIDS:
794138568Ssam		ireq->i_val = 1;
795138568Ssam		break;
796138568Ssam	case IEEE80211_IOC_WEP:
797178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
798138568Ssam			ireq->i_val = IEEE80211_WEP_OFF;
799178354Ssam		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
800138568Ssam			ireq->i_val = IEEE80211_WEP_ON;
801138568Ssam		else
802138568Ssam			ireq->i_val = IEEE80211_WEP_MIXED;
803138568Ssam		break;
804138568Ssam	case IEEE80211_IOC_WEPKEY:
805138568Ssam		kid = (u_int) ireq->i_val;
806138568Ssam		if (kid >= IEEE80211_WEP_NKID)
807138568Ssam			return EINVAL;
808178354Ssam		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
809138568Ssam		/* NB: only root can read WEP keys */
810164033Srwatson		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
811178354Ssam			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
812138568Ssam		} else {
813138568Ssam			bzero(tmpkey, len);
814138568Ssam		}
815138568Ssam		ireq->i_len = len;
816138568Ssam		error = copyout(tmpkey, ireq->i_data, len);
817138568Ssam		break;
818138568Ssam	case IEEE80211_IOC_NUMWEPKEYS:
819138568Ssam		ireq->i_val = IEEE80211_WEP_NKID;
820138568Ssam		break;
821138568Ssam	case IEEE80211_IOC_WEPTXKEY:
822178354Ssam		ireq->i_val = vap->iv_def_txkey;
823138568Ssam		break;
824138568Ssam	case IEEE80211_IOC_AUTHMODE:
825178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA)
826138568Ssam			ireq->i_val = IEEE80211_AUTH_WPA;
827138568Ssam		else
828178354Ssam			ireq->i_val = vap->iv_bss->ni_authmode;
829138568Ssam		break;
830138568Ssam	case IEEE80211_IOC_CHANNEL:
831148936Ssam		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
832138568Ssam		break;
833138568Ssam	case IEEE80211_IOC_POWERSAVE:
834178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON)
835138568Ssam			ireq->i_val = IEEE80211_POWERSAVE_ON;
836138568Ssam		else
837138568Ssam			ireq->i_val = IEEE80211_POWERSAVE_OFF;
838138568Ssam		break;
839138568Ssam	case IEEE80211_IOC_POWERSAVESLEEP:
840138568Ssam		ireq->i_val = ic->ic_lintval;
841138568Ssam		break;
842138568Ssam	case IEEE80211_IOC_RTSTHRESHOLD:
843178354Ssam		ireq->i_val = vap->iv_rtsthreshold;
844138568Ssam		break;
845138568Ssam	case IEEE80211_IOC_PROTMODE:
846138568Ssam		ireq->i_val = ic->ic_protmode;
847138568Ssam		break;
848138568Ssam	case IEEE80211_IOC_TXPOWER:
849178354Ssam		/*
850178354Ssam		 * Tx power limit is the min of max regulatory
851178354Ssam		 * power, any user-set limit, and the max the
852178354Ssam		 * radio can do.
853178354Ssam		 */
854178354Ssam		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
855178354Ssam		if (ireq->i_val > ic->ic_txpowlimit)
856178354Ssam			ireq->i_val = ic->ic_txpowlimit;
857178354Ssam		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
858178354Ssam			ireq->i_val = ic->ic_curchan->ic_maxpower;
859138568Ssam		break;
860138568Ssam	case IEEE80211_IOC_WPA:
861178354Ssam		switch (vap->iv_flags & IEEE80211_F_WPA) {
862138568Ssam		case IEEE80211_F_WPA1:
863116742Ssam			ireq->i_val = 1;
864116742Ssam			break;
865138568Ssam		case IEEE80211_F_WPA2:
866138568Ssam			ireq->i_val = 2;
867116742Ssam			break;
868138568Ssam		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
869138568Ssam			ireq->i_val = 3;
870116742Ssam			break;
871138568Ssam		default:
872138568Ssam			ireq->i_val = 0;
873116742Ssam			break;
874138568Ssam		}
875138568Ssam		break;
876138568Ssam	case IEEE80211_IOC_CHANLIST:
877178354Ssam		error = ieee80211_ioctl_getchanlist(vap, ireq);
878138568Ssam		break;
879138568Ssam	case IEEE80211_IOC_ROAMING:
880178354Ssam		ireq->i_val = vap->iv_roaming;
881138568Ssam		break;
882138568Ssam	case IEEE80211_IOC_PRIVACY:
883178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
884138568Ssam		break;
885138568Ssam	case IEEE80211_IOC_DROPUNENCRYPTED:
886178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
887138568Ssam		break;
888138568Ssam	case IEEE80211_IOC_COUNTERMEASURES:
889178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
890138568Ssam		break;
891138568Ssam	case IEEE80211_IOC_WME:
892178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
893138568Ssam		break;
894138568Ssam	case IEEE80211_IOC_HIDESSID:
895178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
896138568Ssam		break;
897138568Ssam	case IEEE80211_IOC_APBRIDGE:
898178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
899138568Ssam		break;
900138568Ssam	case IEEE80211_IOC_WPAKEY:
901178354Ssam		error = ieee80211_ioctl_getkey(vap, ireq);
902138568Ssam		break;
903138568Ssam	case IEEE80211_IOC_CHANINFO:
904178354Ssam		error = ieee80211_ioctl_getchaninfo(vap, ireq);
905138568Ssam		break;
906138568Ssam	case IEEE80211_IOC_BSSID:
907138568Ssam		if (ireq->i_len != IEEE80211_ADDR_LEN)
908138568Ssam			return EINVAL;
909178354Ssam		error = copyout(vap->iv_state == IEEE80211_S_RUN ?
910178354Ssam					vap->iv_bss->ni_bssid :
911178354Ssam					vap->iv_des_bssid,
912138568Ssam				ireq->i_data, ireq->i_len);
913138568Ssam		break;
914138568Ssam	case IEEE80211_IOC_WPAIE:
915178354Ssam		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
916138568Ssam		break;
917170530Ssam	case IEEE80211_IOC_WPAIE2:
918178354Ssam		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
919170530Ssam		break;
920138568Ssam	case IEEE80211_IOC_SCAN_RESULTS:
921178354Ssam		error = ieee80211_ioctl_getscanresults(vap, ireq);
922138568Ssam		break;
923138568Ssam	case IEEE80211_IOC_STA_STATS:
924178354Ssam		error = ieee80211_ioctl_getstastats(vap, ireq);
925138568Ssam		break;
926138568Ssam	case IEEE80211_IOC_TXPOWMAX:
927178354Ssam		ireq->i_val = vap->iv_bss->ni_txpower;
928138568Ssam		break;
929138568Ssam	case IEEE80211_IOC_STA_TXPOW:
930178354Ssam		error = ieee80211_ioctl_getstatxpow(vap, ireq);
931138568Ssam		break;
932138568Ssam	case IEEE80211_IOC_STA_INFO:
933178354Ssam		error = ieee80211_ioctl_getstainfo(vap, ireq);
934138568Ssam		break;
935138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
936138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
937138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
938138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
939138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
940138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
941178354Ssam		error = ieee80211_ioctl_getwmeparam(vap, ireq);
942138568Ssam		break;
943138568Ssam	case IEEE80211_IOC_DTIM_PERIOD:
944178354Ssam		ireq->i_val = vap->iv_dtim_period;
945138568Ssam		break;
946138568Ssam	case IEEE80211_IOC_BEACON_INTERVAL:
947138568Ssam		/* NB: get from ic_bss for station mode */
948178354Ssam		ireq->i_val = vap->iv_bss->ni_intval;
949138568Ssam		break;
950147794Ssam	case IEEE80211_IOC_PUREG:
951178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
952147794Ssam		break;
953170530Ssam	case IEEE80211_IOC_FF:
954178354Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
955170530Ssam		break;
956170530Ssam	case IEEE80211_IOC_TURBOP:
957178354Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
958170530Ssam		break;
959170530Ssam	case IEEE80211_IOC_BGSCAN:
960178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
961170530Ssam		break;
962170530Ssam	case IEEE80211_IOC_BGSCAN_IDLE:
963178354Ssam		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
964170530Ssam		break;
965170530Ssam	case IEEE80211_IOC_BGSCAN_INTERVAL:
966178354Ssam		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
967170530Ssam		break;
968170530Ssam	case IEEE80211_IOC_SCANVALID:
969178354Ssam		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
970170530Ssam		break;
971148292Ssam	case IEEE80211_IOC_FRAGTHRESHOLD:
972178354Ssam		ireq->i_val = vap->iv_fragthreshold;
973148292Ssam		break;
974149028Ssam	case IEEE80211_IOC_MACCMD:
975178354Ssam		error = ieee80211_ioctl_getmaccmd(vap, ireq);
976149028Ssam		break;
977153421Ssam	case IEEE80211_IOC_BURST:
978178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
979153421Ssam		break;
980160686Ssam	case IEEE80211_IOC_BMISSTHRESHOLD:
981178354Ssam		ireq->i_val = vap->iv_bmissthreshold;
982160686Ssam		break;
983170530Ssam	case IEEE80211_IOC_CURCHAN:
984178354Ssam		error = ieee80211_ioctl_getcurchan(vap, ireq);
985170530Ssam		break;
986170530Ssam	case IEEE80211_IOC_SHORTGI:
987170530Ssam		ireq->i_val = 0;
988178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
989170530Ssam			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
990178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
991170530Ssam			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
992170530Ssam		break;
993170530Ssam	case IEEE80211_IOC_AMPDU:
994170530Ssam		ireq->i_val = 0;
995178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)
996170530Ssam			ireq->i_val |= 1;
997178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)
998170530Ssam			ireq->i_val |= 2;
999170530Ssam		break;
1000170530Ssam	case IEEE80211_IOC_AMPDU_LIMIT:
1001178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1002178354Ssam			ireq->i_val = vap->iv_ampdu_rxmax;
1003178354Ssam		else if (vap->iv_state == IEEE80211_S_RUN)
1004178354Ssam			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1005178354Ssam			    IEEE80211_HTCAP_MAXRXAMPDU);
1006178354Ssam		else
1007178354Ssam			ireq->i_val = vap->iv_ampdu_limit;
1008170530Ssam		break;
1009170530Ssam	case IEEE80211_IOC_AMPDU_DENSITY:
1010182832Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1011182832Ssam		    vap->iv_state == IEEE80211_S_RUN)
1012178354Ssam			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1013178354Ssam			    IEEE80211_HTCAP_MPDUDENSITY);
1014178354Ssam		else
1015178354Ssam			ireq->i_val = vap->iv_ampdu_density;
1016170530Ssam		break;
1017170530Ssam	case IEEE80211_IOC_AMSDU:
1018170530Ssam		ireq->i_val = 0;
1019178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_TX)
1020170530Ssam			ireq->i_val |= 1;
1021178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_AMSDU_RX)
1022170530Ssam			ireq->i_val |= 2;
1023170530Ssam		break;
1024170530Ssam	case IEEE80211_IOC_AMSDU_LIMIT:
1025178354Ssam		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1026170530Ssam		break;
1027170530Ssam	case IEEE80211_IOC_PUREN:
1028178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) != 0;
1029170530Ssam		break;
1030170530Ssam	case IEEE80211_IOC_DOTH:
1031178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1032170530Ssam		break;
1033178354Ssam	case IEEE80211_IOC_REGDOMAIN:
1034178354Ssam		error = ieee80211_ioctl_getregdomain(vap, ireq);
1035178354Ssam		break;
1036178354Ssam	case IEEE80211_IOC_ROAM:
1037178354Ssam		error = ieee80211_ioctl_getroam(vap, ireq);
1038178354Ssam		break;
1039178354Ssam	case IEEE80211_IOC_TXPARAMS:
1040178354Ssam		error = ieee80211_ioctl_gettxparams(vap, ireq);
1041178354Ssam		break;
1042170530Ssam	case IEEE80211_IOC_HTCOMPAT:
1043178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) != 0;
1044170530Ssam		break;
1045178354Ssam	case IEEE80211_IOC_DWDS:
1046178354Ssam		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1047178354Ssam		break;
1048172062Ssam	case IEEE80211_IOC_INACTIVITY:
1049178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1050172062Ssam		break;
1051178354Ssam	case IEEE80211_IOC_APPIE:
1052178354Ssam		error = ieee80211_ioctl_getappie(vap, ireq);
1053178354Ssam		break;
1054178354Ssam	case IEEE80211_IOC_WPS:
1055178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1056178354Ssam		break;
1057178354Ssam	case IEEE80211_IOC_TSN:
1058178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1059178354Ssam		break;
1060178354Ssam	case IEEE80211_IOC_DFS:
1061178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1062178354Ssam		break;
1063178354Ssam	case IEEE80211_IOC_DOTD:
1064178354Ssam		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1065178354Ssam		break;
1066178354Ssam	case IEEE80211_IOC_DEVCAPS:
1067178354Ssam		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1068178354Ssam		break;
1069173273Ssam	case IEEE80211_IOC_HTPROTMODE:
1070173273Ssam		ireq->i_val = ic->ic_htprotmode;
1071173273Ssam		break;
1072173273Ssam	case IEEE80211_IOC_HTCONF:
1073178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_HT) {
1074173273Ssam			ireq->i_val = 1;
1075178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
1076173273Ssam				ireq->i_val |= 2;
1077173273Ssam		} else
1078173273Ssam			ireq->i_val = 0;
1079173273Ssam		break;
1080178354Ssam	case IEEE80211_IOC_STA_VLAN:
1081178354Ssam		error = ieee80211_ioctl_getstavlan(vap, ireq);
1082178354Ssam		break;
1083183255Ssam	case IEEE80211_IOC_SMPS:
1084183255Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1085183255Ssam		    vap->iv_state == IEEE80211_S_RUN) {
1086183255Ssam			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1087183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1088183255Ssam			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1089183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1090183255Ssam			else
1091183255Ssam				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1092183255Ssam		} else
1093183255Ssam			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1094183255Ssam		break;
1095183256Ssam	case IEEE80211_IOC_RIFS:
1096183256Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1097183256Ssam		    vap->iv_state == IEEE80211_S_RUN)
1098183256Ssam			ireq->i_val =
1099183256Ssam			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1100183256Ssam		else
1101183256Ssam			ireq->i_val =
1102183256Ssam			    (vap->iv_flags_ext & IEEE80211_FEXT_RIFS) != 0;
1103183256Ssam		break;
1104186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
1105186904Ssam	case IEEE80211_IOC_TDMA_SLOT:
1106186904Ssam	case IEEE80211_IOC_TDMA_SLOTCNT:
1107186904Ssam	case IEEE80211_IOC_TDMA_SLOTLEN:
1108186904Ssam	case IEEE80211_IOC_TDMA_BINTERVAL:
1109186904Ssam		error = ieee80211_tdma_ioctl_get80211(vap, ireq);
1110186904Ssam		break;
1111186904Ssam#endif
1112138568Ssam	default:
1113138568Ssam		error = EINVAL;
1114138568Ssam		break;
1115138568Ssam	}
1116138568Ssam	return error;
1117178354Ssam#undef MS
1118138568Ssam}
1119138568Ssam
1120178354Ssamstatic __noinline int
1121178354Ssamieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1122138568Ssam{
1123138568Ssam	struct ieee80211req_key ik;
1124138568Ssam	struct ieee80211_node *ni;
1125138568Ssam	struct ieee80211_key *wk;
1126170530Ssam	uint16_t kid;
1127178354Ssam	int error, i;
1128138568Ssam
1129138568Ssam	if (ireq->i_len != sizeof(ik))
1130138568Ssam		return EINVAL;
1131138568Ssam	error = copyin(ireq->i_data, &ik, sizeof(ik));
1132138568Ssam	if (error)
1133138568Ssam		return error;
1134138568Ssam	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1135138568Ssam	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1136138568Ssam	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1137138568Ssam		return E2BIG;
1138138568Ssam	kid = ik.ik_keyix;
1139138568Ssam	if (kid == IEEE80211_KEYIX_NONE) {
1140138568Ssam		/* XXX unicast keys currently must be tx/rx */
1141138568Ssam		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1142138568Ssam			return EINVAL;
1143178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1144178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1145147775Ssam			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1146147775Ssam				ieee80211_free_node(ni);
1147138568Ssam				return EADDRNOTAVAIL;
1148147775Ssam			}
1149138568Ssam		} else {
1150178354Ssam			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1151178354Ssam				ik.ik_macaddr);
1152138568Ssam			if (ni == NULL)
1153138568Ssam				return ENOENT;
1154138568Ssam		}
1155138568Ssam		wk = &ni->ni_ucastkey;
1156138568Ssam	} else {
1157138568Ssam		if (kid >= IEEE80211_WEP_NKID)
1158138568Ssam			return EINVAL;
1159178354Ssam		wk = &vap->iv_nw_keys[kid];
1160155885Ssam		/*
1161155885Ssam		 * Global slots start off w/o any assigned key index.
1162155885Ssam		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1163155885Ssam		 */
1164155885Ssam		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1165155885Ssam			wk->wk_keyix = kid;
1166138568Ssam		ni = NULL;
1167138568Ssam	}
1168138568Ssam	error = 0;
1169178354Ssam	ieee80211_key_update_begin(vap);
1170178354Ssam	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1171138568Ssam		wk->wk_keylen = ik.ik_keylen;
1172138568Ssam		/* NB: MIC presence is implied by cipher type */
1173138568Ssam		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1174138568Ssam			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1175178354Ssam		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1176178354Ssam			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1177138568Ssam		wk->wk_keytsc = 0;			/* new key, reset */
1178138568Ssam		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1179138568Ssam		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1180179395Ssam		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1181179395Ssam		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1182179395Ssam		if (!ieee80211_crypto_setkey(vap, wk))
1183138568Ssam			error = EIO;
1184138568Ssam		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1185178354Ssam			vap->iv_def_txkey = kid;
1186138568Ssam	} else
1187138568Ssam		error = ENXIO;
1188178354Ssam	ieee80211_key_update_end(vap);
1189138568Ssam	if (ni != NULL)
1190138568Ssam		ieee80211_free_node(ni);
1191138568Ssam	return error;
1192138568Ssam}
1193138568Ssam
1194178354Ssamstatic __noinline int
1195178354Ssamieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1196138568Ssam{
1197138568Ssam	struct ieee80211req_del_key dk;
1198138568Ssam	int kid, error;
1199138568Ssam
1200138568Ssam	if (ireq->i_len != sizeof(dk))
1201138568Ssam		return EINVAL;
1202138568Ssam	error = copyin(ireq->i_data, &dk, sizeof(dk));
1203138568Ssam	if (error)
1204138568Ssam		return error;
1205138568Ssam	kid = dk.idk_keyix;
1206170530Ssam	/* XXX uint8_t -> uint16_t */
1207170530Ssam	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1208138568Ssam		struct ieee80211_node *ni;
1209138568Ssam
1210178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1211178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1212147775Ssam			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1213147775Ssam				ieee80211_free_node(ni);
1214147775Ssam				return EADDRNOTAVAIL;
1215147775Ssam			}
1216147775Ssam		} else {
1217178354Ssam			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1218178354Ssam				dk.idk_macaddr);
1219147775Ssam			if (ni == NULL)
1220147775Ssam				return ENOENT;
1221147775Ssam		}
1222138568Ssam		/* XXX error return */
1223148863Ssam		ieee80211_node_delucastkey(ni);
1224138568Ssam		ieee80211_free_node(ni);
1225138568Ssam	} else {
1226138568Ssam		if (kid >= IEEE80211_WEP_NKID)
1227138568Ssam			return EINVAL;
1228138568Ssam		/* XXX error return */
1229178354Ssam		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1230138568Ssam	}
1231138568Ssam	return 0;
1232138568Ssam}
1233138568Ssam
1234178354Ssamstruct mlmeop {
1235178354Ssam	struct ieee80211vap *vap;
1236178354Ssam	int	op;
1237178354Ssam	int	reason;
1238178354Ssam};
1239178354Ssam
1240138568Ssamstatic void
1241178354Ssammlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1242178354Ssam	int op, int reason)
1243178354Ssam{
1244178354Ssam#ifdef IEEE80211_DEBUG
1245178354Ssam	static const struct {
1246178354Ssam		int mask;
1247178354Ssam		const char *opstr;
1248178354Ssam	} ops[] = {
1249178354Ssam		{ 0, "op#0" },
1250178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1251178354Ssam		  IEEE80211_MSG_ASSOC, "assoc" },
1252178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1253178354Ssam		  IEEE80211_MSG_ASSOC, "disassoc" },
1254178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1255178354Ssam		  IEEE80211_MSG_AUTH, "deauth" },
1256178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1257178354Ssam		  IEEE80211_MSG_AUTH, "authorize" },
1258178354Ssam		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1259178354Ssam		  IEEE80211_MSG_AUTH, "unauthorize" },
1260178354Ssam	};
1261178354Ssam
1262178354Ssam	if (op == IEEE80211_MLME_AUTH) {
1263178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1264178354Ssam		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1265178354Ssam		    "station authenticate %s via MLME (reason %d)",
1266178354Ssam		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1267178354Ssam		    reason);
1268178354Ssam	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1269178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1270178354Ssam		    "unknown MLME request %d (reason %d)", op, reason);
1271178354Ssam	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1272178354Ssam		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1273178354Ssam		    "station %s via MLME", ops[op].opstr);
1274178354Ssam	} else {
1275178354Ssam		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1276178354Ssam		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1277178354Ssam	}
1278178354Ssam#endif /* IEEE80211_DEBUG */
1279178354Ssam}
1280178354Ssam
1281178354Ssamstatic void
1282138568Ssamdomlme(void *arg, struct ieee80211_node *ni)
1283138568Ssam{
1284178354Ssam	struct mlmeop *mop = arg;
1285178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1286138568Ssam
1287178354Ssam	if (vap != mop->vap)
1288178354Ssam		return;
1289178354Ssam	/*
1290178354Ssam	 * NB: if ni_associd is zero then the node is already cleaned
1291178354Ssam	 * up and we don't need to do this (we're safely holding a
1292178354Ssam	 * reference but should otherwise not modify it's state).
1293178354Ssam	 */
1294178354Ssam	if (ni->ni_associd == 0)
1295178354Ssam		return;
1296178354Ssam	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1297178354Ssam	if (mop->op == IEEE80211_MLME_DEAUTH) {
1298178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1299178354Ssam		    mop->reason);
1300178354Ssam	} else {
1301178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1302178354Ssam		    mop->reason);
1303138568Ssam	}
1304178354Ssam	ieee80211_node_leave(ni);
1305138568Ssam}
1306138568Ssam
1307178354Ssamstatic int
1308178354Ssamsetmlme_dropsta(struct ieee80211vap *vap,
1309178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1310178354Ssam{
1311178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1312178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1313178354Ssam	struct ieee80211_node *ni;
1314178354Ssam	int error = 0;
1315178354Ssam
1316178354Ssam	/* NB: the broadcast address means do 'em all */
1317178354Ssam	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1318178354Ssam		IEEE80211_NODE_LOCK(nt);
1319178354Ssam		ni = ieee80211_find_node_locked(nt, mac);
1320178354Ssam		if (ni != NULL) {
1321178354Ssam			domlme(mlmeop, ni);
1322178354Ssam			ieee80211_free_node(ni);
1323178354Ssam		} else
1324178354Ssam			error = ENOENT;
1325178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1326178354Ssam	} else {
1327178354Ssam		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1328178354Ssam	}
1329178354Ssam	return error;
1330178354Ssam}
1331178354Ssam
1332178354Ssamstatic __noinline int
1333178354Ssamsetmlme_common(struct ieee80211vap *vap, int op,
1334178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1335178354Ssam{
1336178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1337178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1338178354Ssam	struct ieee80211_node *ni;
1339178354Ssam	struct mlmeop mlmeop;
1340178354Ssam	int error;
1341178354Ssam
1342178354Ssam	error = 0;
1343178354Ssam	switch (op) {
1344178354Ssam	case IEEE80211_MLME_DISASSOC:
1345178354Ssam	case IEEE80211_MLME_DEAUTH:
1346178354Ssam		switch (vap->iv_opmode) {
1347178354Ssam		case IEEE80211_M_STA:
1348178354Ssam			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1349178354Ssam			/* XXX not quite right */
1350178354Ssam			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1351178354Ssam			break;
1352178354Ssam		case IEEE80211_M_HOSTAP:
1353178354Ssam			mlmeop.vap = vap;
1354178354Ssam			mlmeop.op = op;
1355178354Ssam			mlmeop.reason = reason;
1356178354Ssam			error = setmlme_dropsta(vap, mac, &mlmeop);
1357178354Ssam			break;
1358178354Ssam		case IEEE80211_M_WDS:
1359178354Ssam			/* XXX user app should send raw frame? */
1360178354Ssam			if (op != IEEE80211_MLME_DEAUTH) {
1361178354Ssam				error = EINVAL;
1362178354Ssam				break;
1363178354Ssam			}
1364178354Ssam#if 0
1365178354Ssam			/* XXX accept any address, simplifies user code */
1366178354Ssam			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1367178354Ssam				error = EINVAL;
1368178354Ssam				break;
1369178354Ssam			}
1370178354Ssam#endif
1371178354Ssam			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1372178354Ssam			ni = ieee80211_ref_node(vap->iv_bss);
1373178354Ssam			IEEE80211_SEND_MGMT(ni,
1374178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1375178354Ssam			ieee80211_free_node(ni);
1376178354Ssam			break;
1377178354Ssam		default:
1378178354Ssam			error = EINVAL;
1379178354Ssam			break;
1380178354Ssam		}
1381178354Ssam		break;
1382178354Ssam	case IEEE80211_MLME_AUTHORIZE:
1383178354Ssam	case IEEE80211_MLME_UNAUTHORIZE:
1384178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1385178354Ssam		    vap->iv_opmode != IEEE80211_M_WDS) {
1386178354Ssam			error = EINVAL;
1387178354Ssam			break;
1388178354Ssam		}
1389178354Ssam		IEEE80211_NODE_LOCK(nt);
1390178354Ssam		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1391178354Ssam		if (ni != NULL) {
1392178354Ssam			mlmedebug(vap, mac, op, reason);
1393178354Ssam			if (op == IEEE80211_MLME_AUTHORIZE)
1394178354Ssam				ieee80211_node_authorize(ni);
1395178354Ssam			else
1396178354Ssam				ieee80211_node_unauthorize(ni);
1397178354Ssam			ieee80211_free_node(ni);
1398178354Ssam		} else
1399178354Ssam			error = ENOENT;
1400178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1401178354Ssam		break;
1402178354Ssam	case IEEE80211_MLME_AUTH:
1403178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1404178354Ssam			error = EINVAL;
1405178354Ssam			break;
1406178354Ssam		}
1407178354Ssam		IEEE80211_NODE_LOCK(nt);
1408178354Ssam		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1409178354Ssam		if (ni != NULL) {
1410178354Ssam			mlmedebug(vap, mac, op, reason);
1411178354Ssam			if (reason == IEEE80211_STATUS_SUCCESS) {
1412178354Ssam				IEEE80211_SEND_MGMT(ni,
1413178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1414178354Ssam				/*
1415178354Ssam				 * For shared key auth, just continue the
1416178354Ssam				 * exchange.  Otherwise when 802.1x is not in
1417178354Ssam				 * use mark the port authorized at this point
1418178354Ssam				 * so traffic can flow.
1419178354Ssam				 */
1420178354Ssam				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1421178354Ssam				    ni->ni_challenge == NULL)
1422178354Ssam				      ieee80211_node_authorize(ni);
1423178354Ssam			} else {
1424178354Ssam				vap->iv_stats.is_rx_acl++;
1425178354Ssam				ieee80211_send_error(ni, ni->ni_macaddr,
1426178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1427178354Ssam				ieee80211_node_leave(ni);
1428178354Ssam			}
1429178354Ssam			ieee80211_free_node(ni);
1430178354Ssam		} else
1431178354Ssam			error = ENOENT;
1432178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1433178354Ssam		break;
1434178354Ssam	default:
1435178354Ssam		error = EINVAL;
1436178354Ssam		break;
1437178354Ssam	}
1438178354Ssam	return error;
1439178354Ssam}
1440178354Ssam
1441170530Ssamstruct scanlookup {
1442170530Ssam	const uint8_t *mac;
1443170530Ssam	int esslen;
1444170530Ssam	const uint8_t *essid;
1445170530Ssam	const struct ieee80211_scan_entry *se;
1446170530Ssam};
1447170530Ssam
1448170530Ssam/*
1449170530Ssam * Match mac address and any ssid.
1450170530Ssam */
1451170530Ssamstatic void
1452170530Ssammlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1453170530Ssam{
1454170530Ssam	struct scanlookup *look = arg;
1455170530Ssam
1456170530Ssam	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1457170530Ssam		return;
1458170530Ssam	if (look->esslen != 0) {
1459170530Ssam		if (se->se_ssid[1] != look->esslen)
1460170530Ssam			return;
1461170530Ssam		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1462170530Ssam			return;
1463170530Ssam	}
1464170530Ssam	look->se = se;
1465170530Ssam}
1466170530Ssam
1467178354Ssamstatic __noinline int
1468178354Ssamsetmlme_assoc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1469178354Ssam	int ssid_len, const uint8_t ssid[IEEE80211_NWID_LEN])
1470138568Ssam{
1471178354Ssam	struct scanlookup lookup;
1472178354Ssam
1473178354Ssam	/* XXX ibss/ahdemo */
1474178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
1475178354Ssam		return EINVAL;
1476178354Ssam
1477178354Ssam	/* NB: this is racey if roaming is !manual */
1478178354Ssam	lookup.se = NULL;
1479178354Ssam	lookup.mac = mac;
1480178354Ssam	lookup.esslen = ssid_len;
1481178354Ssam	lookup.essid = ssid;
1482178354Ssam	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1483178354Ssam	if (lookup.se == NULL)
1484178354Ssam		return ENOENT;
1485178354Ssam	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1486184274Ssam	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1487178354Ssam		return EIO;		/* XXX unique but could be better */
1488178354Ssam	return 0;
1489178354Ssam}
1490178354Ssam
1491178354Ssamstatic __noinline int
1492178354Ssamieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1493178354Ssam{
1494138568Ssam	struct ieee80211req_mlme mlme;
1495138568Ssam	int error;
1496138568Ssam
1497138568Ssam	if (ireq->i_len != sizeof(mlme))
1498138568Ssam		return EINVAL;
1499138568Ssam	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1500138568Ssam	if (error)
1501138568Ssam		return error;
1502178354Ssam	if  (mlme.im_op == IEEE80211_MLME_ASSOC)
1503178354Ssam		return setmlme_assoc(vap, mlme.im_macaddr,
1504178354Ssam		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1505178354Ssam	else
1506178354Ssam		return setmlme_common(vap, mlme.im_op,
1507178354Ssam		    mlme.im_macaddr, mlme.im_reason);
1508138568Ssam}
1509138568Ssam
1510178354Ssamstatic __noinline int
1511178354Ssamieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1512138568Ssam{
1513170530Ssam	uint8_t mac[IEEE80211_ADDR_LEN];
1514178354Ssam	const struct ieee80211_aclator *acl = vap->iv_acl;
1515138568Ssam	int error;
1516138568Ssam
1517138568Ssam	if (ireq->i_len != sizeof(mac))
1518138568Ssam		return EINVAL;
1519138568Ssam	error = copyin(ireq->i_data, mac, ireq->i_len);
1520138568Ssam	if (error)
1521138568Ssam		return error;
1522138568Ssam	if (acl == NULL) {
1523138568Ssam		acl = ieee80211_aclator_get("mac");
1524178354Ssam		if (acl == NULL || !acl->iac_attach(vap))
1525138568Ssam			return EINVAL;
1526178354Ssam		vap->iv_acl = acl;
1527138568Ssam	}
1528138568Ssam	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1529178354Ssam		acl->iac_add(vap, mac);
1530138568Ssam	else
1531178354Ssam		acl->iac_remove(vap, mac);
1532138568Ssam	return 0;
1533138568Ssam}
1534138568Ssam
1535178354Ssamstatic __noinline int
1536178354Ssamieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1537138568Ssam{
1538178354Ssam	const struct ieee80211_aclator *acl = vap->iv_acl;
1539138568Ssam
1540138568Ssam	switch (ireq->i_val) {
1541138568Ssam	case IEEE80211_MACCMD_POLICY_OPEN:
1542138568Ssam	case IEEE80211_MACCMD_POLICY_ALLOW:
1543138568Ssam	case IEEE80211_MACCMD_POLICY_DENY:
1544178354Ssam	case IEEE80211_MACCMD_POLICY_RADIUS:
1545138568Ssam		if (acl == NULL) {
1546138568Ssam			acl = ieee80211_aclator_get("mac");
1547178354Ssam			if (acl == NULL || !acl->iac_attach(vap))
1548138568Ssam				return EINVAL;
1549178354Ssam			vap->iv_acl = acl;
1550138568Ssam		}
1551178354Ssam		acl->iac_setpolicy(vap, ireq->i_val);
1552138568Ssam		break;
1553138568Ssam	case IEEE80211_MACCMD_FLUSH:
1554138568Ssam		if (acl != NULL)
1555178354Ssam			acl->iac_flush(vap);
1556138568Ssam		/* NB: silently ignore when not in use */
1557138568Ssam		break;
1558138568Ssam	case IEEE80211_MACCMD_DETACH:
1559138568Ssam		if (acl != NULL) {
1560178354Ssam			vap->iv_acl = NULL;
1561178354Ssam			acl->iac_detach(vap);
1562138568Ssam		}
1563138568Ssam		break;
1564138568Ssam	default:
1565149028Ssam		if (acl == NULL)
1566149028Ssam			return EINVAL;
1567149028Ssam		else
1568178354Ssam			return acl->iac_setioctl(vap, ireq);
1569138568Ssam	}
1570138568Ssam	return 0;
1571138568Ssam}
1572138568Ssam
1573178354Ssamstatic __noinline int
1574178354Ssamieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1575138568Ssam{
1576178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1577187801Ssam	uint8_t *chanlist, *list;
1578187801Ssam	int i, nchan, maxchan, error;
1579138568Ssam
1580187801Ssam	if (ireq->i_len > sizeof(ic->ic_chan_active))
1581187801Ssam		ireq->i_len = sizeof(ic->ic_chan_active);
1582187801Ssam	list = malloc(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1583187801Ssam	    M_NOWAIT | M_ZERO);
1584187801Ssam	if (list == NULL)
1585187801Ssam		return ENOMEM;
1586187801Ssam	error = copyin(ireq->i_data, list, ireq->i_len);
1587138568Ssam	if (error)
1588138568Ssam		return error;
1589170530Ssam	nchan = 0;
1590187801Ssam	chanlist = list + ireq->i_len;		/* NB: zero'd already */
1591187801Ssam	maxchan = ireq->i_len * NBBY;
1592186107Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1593186107Ssam		const struct ieee80211_channel *c = &ic->ic_channels[i];
1594138568Ssam		/*
1595186107Ssam		 * Calculate the intersection of the user list and the
1596186107Ssam		 * available channels so users can do things like specify
1597186107Ssam		 * 1-255 to get all available channels.
1598138568Ssam		 */
1599187801Ssam		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
1600186107Ssam			setbit(chanlist, c->ic_ieee);
1601170530Ssam			nchan++;
1602170530Ssam		}
1603138568Ssam	}
1604170530Ssam	if (nchan == 0)
1605170530Ssam		return EINVAL;
1606170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1607170530Ssam	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1608170530Ssam		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1609187801Ssam	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1610178354Ssam	ieee80211_scan_flush(vap);
1611187801Ssam	free(list, M_TEMP);
1612178354Ssam	return ENETRESET;
1613138568Ssam}
1614138568Ssam
1615178354Ssamstatic __noinline int
1616178354Ssamieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1617157172Ssam{
1618157172Ssam	struct ieee80211_node *ni;
1619170530Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
1620157172Ssam	int error;
1621157172Ssam
1622157172Ssam	/*
1623157172Ssam	 * NB: we could copyin ieee80211req_sta_stats so apps
1624157172Ssam	 *     could make selective changes but that's overkill;
1625157172Ssam	 *     just clear all stats for now.
1626157172Ssam	 */
1627157172Ssam	if (ireq->i_len < IEEE80211_ADDR_LEN)
1628157172Ssam		return EINVAL;
1629157172Ssam	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1630157172Ssam	if (error != 0)
1631157172Ssam		return error;
1632178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1633157172Ssam	if (ni == NULL)
1634178354Ssam		return ENOENT;
1635178354Ssam	/* XXX require ni_vap == vap? */
1636157172Ssam	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1637157172Ssam	ieee80211_free_node(ni);
1638157172Ssam	return 0;
1639157172Ssam}
1640157172Ssam
1641178354Ssamstatic __noinline int
1642178354Ssamieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1643138568Ssam{
1644138568Ssam	struct ieee80211_node *ni;
1645138568Ssam	struct ieee80211req_sta_txpow txpow;
1646138568Ssam	int error;
1647138568Ssam
1648138568Ssam	if (ireq->i_len != sizeof(txpow))
1649138568Ssam		return EINVAL;
1650138568Ssam	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1651138568Ssam	if (error != 0)
1652138568Ssam		return error;
1653178354Ssam	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1654138568Ssam	if (ni == NULL)
1655178354Ssam		return ENOENT;
1656138568Ssam	ni->ni_txpower = txpow.it_txpow;
1657138568Ssam	ieee80211_free_node(ni);
1658138568Ssam	return error;
1659138568Ssam}
1660138568Ssam
1661178354Ssamstatic __noinline int
1662178354Ssamieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1663138568Ssam{
1664178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1665138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
1666138568Ssam	struct wmeParams *wmep, *chanp;
1667138568Ssam	int isbss, ac;
1668138568Ssam
1669138568Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1670178354Ssam		return EOPNOTSUPP;
1671138568Ssam
1672138568Ssam	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1673138568Ssam	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1674138568Ssam	if (ac >= WME_NUM_AC)
1675138568Ssam		ac = WME_AC_BE;
1676138568Ssam	if (isbss) {
1677138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1678138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1679138568Ssam	} else {
1680138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1681138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1682138568Ssam	}
1683138568Ssam	switch (ireq->i_type) {
1684138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1685138568Ssam		if (isbss) {
1686138568Ssam			wmep->wmep_logcwmin = ireq->i_val;
1687138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1688138568Ssam				chanp->wmep_logcwmin = ireq->i_val;
1689138568Ssam		} else {
1690138568Ssam			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
1691138568Ssam				ireq->i_val;
1692138568Ssam		}
1693138568Ssam		break;
1694138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1695138568Ssam		if (isbss) {
1696138568Ssam			wmep->wmep_logcwmax = ireq->i_val;
1697138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1698138568Ssam				chanp->wmep_logcwmax = ireq->i_val;
1699138568Ssam		} else {
1700138568Ssam			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
1701138568Ssam				ireq->i_val;
1702138568Ssam		}
1703138568Ssam		break;
1704138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1705138568Ssam		if (isbss) {
1706138568Ssam			wmep->wmep_aifsn = ireq->i_val;
1707138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1708138568Ssam				chanp->wmep_aifsn = ireq->i_val;
1709138568Ssam		} else {
1710138568Ssam			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
1711138568Ssam		}
1712138568Ssam		break;
1713138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1714138568Ssam		if (isbss) {
1715138568Ssam			wmep->wmep_txopLimit = ireq->i_val;
1716138568Ssam			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1717138568Ssam				chanp->wmep_txopLimit = ireq->i_val;
1718138568Ssam		} else {
1719138568Ssam			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
1720138568Ssam				ireq->i_val;
1721138568Ssam		}
1722138568Ssam		break;
1723138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1724138568Ssam		wmep->wmep_acm = ireq->i_val;
1725138568Ssam		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1726138568Ssam			chanp->wmep_acm = ireq->i_val;
1727138568Ssam		break;
1728138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1729138568Ssam		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1730138568Ssam			(ireq->i_val) == 0;
1731138568Ssam		break;
1732138568Ssam	}
1733178354Ssam	ieee80211_wme_updateparams(vap);
1734138568Ssam	return 0;
1735138568Ssam}
1736138568Ssam
1737138568Ssamstatic int
1738170530Ssamfind11gchannel(struct ieee80211com *ic, int start, int freq)
1739170530Ssam{
1740170530Ssam	const struct ieee80211_channel *c;
1741170530Ssam	int i;
1742170530Ssam
1743170530Ssam	for (i = start+1; i < ic->ic_nchans; i++) {
1744170530Ssam		c = &ic->ic_channels[i];
1745170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1746170530Ssam			return 1;
1747170530Ssam	}
1748170530Ssam	/* NB: should not be needed but in case things are mis-sorted */
1749170530Ssam	for (i = 0; i < start; i++) {
1750170530Ssam		c = &ic->ic_channels[i];
1751170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1752170530Ssam			return 1;
1753170530Ssam	}
1754170530Ssam	return 0;
1755170530Ssam}
1756170530Ssam
1757170530Ssamstatic struct ieee80211_channel *
1758170530Ssamfindchannel(struct ieee80211com *ic, int ieee, int mode)
1759170530Ssam{
1760170530Ssam	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1761170530Ssam		0,			/* IEEE80211_MODE_AUTO */
1762170530Ssam		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
1763170530Ssam		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
1764170530Ssam		IEEE80211_CHAN_G,	/* IEEE80211_MODE_11G */
1765170530Ssam		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
1766170530Ssam		IEEE80211_CHAN_108A,	/* IEEE80211_MODE_TURBO_A */
1767170530Ssam		IEEE80211_CHAN_108G,	/* IEEE80211_MODE_TURBO_G */
1768170530Ssam		IEEE80211_CHAN_STURBO,	/* IEEE80211_MODE_STURBO_A */
1769170530Ssam		/* NB: handled specially below */
1770170530Ssam		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11NA */
1771170530Ssam		IEEE80211_CHAN_G,	/* IEEE80211_MODE_11NG */
1772170530Ssam	};
1773170530Ssam	u_int modeflags;
1774170530Ssam	int i;
1775170530Ssam
1776170530Ssam	modeflags = chanflags[mode];
1777170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1778170530Ssam		struct ieee80211_channel *c = &ic->ic_channels[i];
1779170530Ssam
1780170530Ssam		if (c->ic_ieee != ieee)
1781170530Ssam			continue;
1782170530Ssam		if (mode == IEEE80211_MODE_AUTO) {
1783170530Ssam			/* ignore turbo channels for autoselect */
1784170530Ssam			if (IEEE80211_IS_CHAN_TURBO(c))
1785170530Ssam				continue;
1786170530Ssam			/*
1787170530Ssam			 * XXX special-case 11b/g channels so we
1788170530Ssam			 *     always select the g channel if both
1789170530Ssam			 *     are present.
1790170530Ssam			 * XXX prefer HT to non-HT?
1791170530Ssam			 */
1792170530Ssam			if (!IEEE80211_IS_CHAN_B(c) ||
1793170530Ssam			    !find11gchannel(ic, i, c->ic_freq))
1794170530Ssam				return c;
1795170530Ssam		} else {
1796170530Ssam			/* must check HT specially */
1797170530Ssam			if ((mode == IEEE80211_MODE_11NA ||
1798170530Ssam			    mode == IEEE80211_MODE_11NG) &&
1799170530Ssam			    !IEEE80211_IS_CHAN_HT(c))
1800170530Ssam				continue;
1801170530Ssam			if ((c->ic_flags & modeflags) == modeflags)
1802170530Ssam				return c;
1803170530Ssam		}
1804170530Ssam	}
1805170530Ssam	return NULL;
1806170530Ssam}
1807170530Ssam
1808170530Ssam/*
1809170530Ssam * Check the specified against any desired mode (aka netband).
1810170530Ssam * This is only used (presently) when operating in hostap mode
1811170530Ssam * to enforce consistency.
1812170530Ssam */
1813170530Ssamstatic int
1814170530Ssamcheck_mode_consistency(const struct ieee80211_channel *c, int mode)
1815170530Ssam{
1816170530Ssam	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1817170530Ssam
1818170530Ssam	switch (mode) {
1819170530Ssam	case IEEE80211_MODE_11B:
1820170530Ssam		return (IEEE80211_IS_CHAN_B(c));
1821170530Ssam	case IEEE80211_MODE_11G:
1822170530Ssam		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1823170530Ssam	case IEEE80211_MODE_11A:
1824170530Ssam		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1825170530Ssam	case IEEE80211_MODE_STURBO_A:
1826170530Ssam		return (IEEE80211_IS_CHAN_STURBO(c));
1827170530Ssam	case IEEE80211_MODE_11NA:
1828170530Ssam		return (IEEE80211_IS_CHAN_HTA(c));
1829170530Ssam	case IEEE80211_MODE_11NG:
1830170530Ssam		return (IEEE80211_IS_CHAN_HTG(c));
1831170530Ssam	}
1832170530Ssam	return 1;
1833170530Ssam
1834170530Ssam}
1835170530Ssam
1836170530Ssam/*
1837170530Ssam * Common code to set the current channel.  If the device
1838170530Ssam * is up and running this may result in an immediate channel
1839170530Ssam * change or a kick of the state machine.
1840170530Ssam */
1841170530Ssamstatic int
1842178354Ssamsetcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1843170530Ssam{
1844178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1845170530Ssam	int error;
1846170530Ssam
1847170530Ssam	if (c != IEEE80211_CHAN_ANYC) {
1848178354Ssam		if (IEEE80211_IS_CHAN_RADAR(c))
1849178354Ssam			return EBUSY;	/* XXX better code? */
1850178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1851178354Ssam			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1852178354Ssam				return EINVAL;
1853178354Ssam			if (!check_mode_consistency(c, vap->iv_des_mode))
1854178354Ssam				return EINVAL;
1855178354Ssam		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1856178354Ssam			if (IEEE80211_IS_CHAN_NOADHOC(c))
1857178354Ssam				return EINVAL;
1858178354Ssam		}
1859178354Ssam		if (vap->iv_state == IEEE80211_S_RUN &&
1860178354Ssam		    vap->iv_bss->ni_chan == c)
1861170530Ssam			return 0;	/* NB: nothing to do */
1862170530Ssam	}
1863178354Ssam	vap->iv_des_chan = c;
1864170530Ssam
1865170530Ssam	error = 0;
1866178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1867178354Ssam	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1868170530Ssam		/*
1869178354Ssam		 * Monitor mode can switch directly.
1870170530Ssam		 */
1871178354Ssam		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1872178354Ssam			/* XXX need state machine for other vap's to follow */
1873178354Ssam			ieee80211_setcurchan(ic, vap->iv_des_chan);
1874178354Ssam			vap->iv_bss->ni_chan = ic->ic_curchan;
1875178354Ssam		} else
1876178354Ssam			ic->ic_curchan = vap->iv_des_chan;
1877170530Ssam	} else {
1878170530Ssam		/*
1879170530Ssam		 * Need to go through the state machine in case we
1880170530Ssam		 * need to reassociate or the like.  The state machine
1881170530Ssam		 * will pickup the desired channel and avoid scanning.
1882170530Ssam		 */
1883178354Ssam		if (IS_UP_AUTO(vap))
1884178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1885178354Ssam		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1886170530Ssam			/*
1887170530Ssam			 * When not up+running and a real channel has
1888170530Ssam			 * been specified fix the current channel so
1889170530Ssam			 * there is immediate feedback; e.g. via ifconfig.
1890170530Ssam			 */
1891178354Ssam			ic->ic_curchan = vap->iv_des_chan;
1892170530Ssam		}
1893170530Ssam	}
1894170530Ssam	return error;
1895170530Ssam}
1896170530Ssam
1897170530Ssam/*
1898170530Ssam * Old api for setting the current channel; this is
1899170530Ssam * deprecated because channel numbers are ambiguous.
1900170530Ssam */
1901178354Ssamstatic __noinline int
1902178354Ssamieee80211_ioctl_setchannel(struct ieee80211vap *vap,
1903170530Ssam	const struct ieee80211req *ireq)
1904170530Ssam{
1905178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1906170530Ssam	struct ieee80211_channel *c;
1907170530Ssam
1908170530Ssam	/* XXX 0xffff overflows 16-bit signed */
1909170530Ssam	if (ireq->i_val == 0 ||
1910170530Ssam	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
1911170530Ssam		c = IEEE80211_CHAN_ANYC;
1912170530Ssam	} else {
1913170530Ssam		struct ieee80211_channel *c2;
1914170530Ssam
1915178354Ssam		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
1916170530Ssam		if (c == NULL) {
1917170530Ssam			c = findchannel(ic, ireq->i_val,
1918170530Ssam				IEEE80211_MODE_AUTO);
1919170530Ssam			if (c == NULL)
1920170530Ssam				return EINVAL;
1921170530Ssam		}
1922170530Ssam		/*
1923170530Ssam		 * Fine tune channel selection based on desired mode:
1924170530Ssam		 *   if 11b is requested, find the 11b version of any
1925170530Ssam		 *      11g channel returned,
1926170530Ssam		 *   if static turbo, find the turbo version of any
1927170530Ssam		 *	11a channel return,
1928170530Ssam		 *   if 11na is requested, find the ht version of any
1929170530Ssam		 *      11a channel returned,
1930170530Ssam		 *   if 11ng is requested, find the ht version of any
1931170530Ssam		 *      11g channel returned,
1932170530Ssam		 *   otherwise we should be ok with what we've got.
1933170530Ssam		 */
1934178354Ssam		switch (vap->iv_des_mode) {
1935170530Ssam		case IEEE80211_MODE_11B:
1936170530Ssam			if (IEEE80211_IS_CHAN_ANYG(c)) {
1937170530Ssam				c2 = findchannel(ic, ireq->i_val,
1938170530Ssam					IEEE80211_MODE_11B);
1939170530Ssam				/* NB: should not happen, =>'s 11g w/o 11b */
1940170530Ssam				if (c2 != NULL)
1941170530Ssam					c = c2;
1942170530Ssam			}
1943170530Ssam			break;
1944170530Ssam		case IEEE80211_MODE_TURBO_A:
1945170530Ssam			if (IEEE80211_IS_CHAN_A(c)) {
1946170530Ssam				c2 = findchannel(ic, ireq->i_val,
1947170530Ssam					IEEE80211_MODE_TURBO_A);
1948170530Ssam				if (c2 != NULL)
1949170530Ssam					c = c2;
1950170530Ssam			}
1951170530Ssam			break;
1952170530Ssam		case IEEE80211_MODE_11NA:
1953170530Ssam			if (IEEE80211_IS_CHAN_A(c)) {
1954170530Ssam				c2 = findchannel(ic, ireq->i_val,
1955170530Ssam					IEEE80211_MODE_11NA);
1956170530Ssam				if (c2 != NULL)
1957170530Ssam					c = c2;
1958170530Ssam			}
1959170530Ssam			break;
1960170530Ssam		case IEEE80211_MODE_11NG:
1961170530Ssam			if (IEEE80211_IS_CHAN_ANYG(c)) {
1962170530Ssam				c2 = findchannel(ic, ireq->i_val,
1963170530Ssam					IEEE80211_MODE_11NG);
1964170530Ssam				if (c2 != NULL)
1965170530Ssam					c = c2;
1966170530Ssam			}
1967170530Ssam			break;
1968170530Ssam		default:		/* NB: no static turboG */
1969170530Ssam			break;
1970170530Ssam		}
1971170530Ssam	}
1972178354Ssam	return setcurchan(vap, c);
1973170530Ssam}
1974170530Ssam
1975170530Ssam/*
1976170530Ssam * New/current api for setting the current channel; a complete
1977170530Ssam * channel description is provide so there is no ambiguity in
1978170530Ssam * identifying the channel.
1979170530Ssam */
1980178354Ssamstatic __noinline int
1981178354Ssamieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
1982170530Ssam	const struct ieee80211req *ireq)
1983170530Ssam{
1984178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1985170530Ssam	struct ieee80211_channel chan, *c;
1986170530Ssam	int error;
1987170530Ssam
1988170530Ssam	if (ireq->i_len != sizeof(chan))
1989170530Ssam		return EINVAL;
1990170530Ssam	error = copyin(ireq->i_data, &chan, sizeof(chan));
1991170530Ssam	if (error != 0)
1992170530Ssam		return error;
1993170530Ssam	/* XXX 0xffff overflows 16-bit signed */
1994170530Ssam	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
1995170530Ssam		c = IEEE80211_CHAN_ANYC;
1996170530Ssam	} else {
1997170530Ssam		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
1998170530Ssam		if (c == NULL)
1999170530Ssam			return EINVAL;
2000170530Ssam	}
2001178354Ssam	return setcurchan(vap, c);
2002170530Ssam}
2003170530Ssam
2004178354Ssamstatic __noinline int
2005178354Ssamieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2006178354Ssam	const struct ieee80211req *ireq)
2007178354Ssam{
2008178354Ssam	struct ieee80211_regdomain_req *reg;
2009187801Ssam	int nchans, error;
2010178354Ssam
2011187801Ssam	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2012187801Ssam	    sizeof(struct ieee80211_channel));
2013187801Ssam	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2014187801Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2015187801Ssam		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2016187801Ssam		    ireq->i_len, nchans);
2017178354Ssam		return EINVAL;
2018187801Ssam	}
2019187801Ssam	reg = (struct ieee80211_regdomain_req *)
2020187801Ssam	    malloc(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, M_NOWAIT);
2021187801Ssam	if (reg == NULL) {
2022187801Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2023187801Ssam		    "%s: no memory, nchans %d\n", __func__, nchans);
2024178354Ssam		return ENOMEM;
2025187801Ssam	}
2026187801Ssam	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2027187801Ssam	if (error == 0) {
2028187801Ssam		/* NB: validate inline channel count against storage size */
2029187801Ssam		if (reg->chaninfo.ic_nchans != nchans) {
2030187801Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2031187801Ssam			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2032187801Ssam				reg->chaninfo.ic_nchans, nchans);
2033187801Ssam			error = EINVAL;
2034187801Ssam		} else
2035187801Ssam			error = ieee80211_setregdomain(vap, reg);
2036187801Ssam	}
2037186302Ssam	free(reg, M_TEMP);
2038178354Ssam
2039178354Ssam	return (error == 0 ? ENETRESET : error);
2040178354Ssam}
2041178354Ssam
2042170530Ssamstatic int
2043178354Ssamieee80211_ioctl_setroam(struct ieee80211vap *vap,
2044178354Ssam	const struct ieee80211req *ireq)
2045138568Ssam{
2046178354Ssam	if (ireq->i_len != sizeof(vap->iv_roamparms))
2047178354Ssam		return EINVAL;
2048178354Ssam	/* XXX validate params */
2049178354Ssam	/* XXX? ENETRESET to push to device? */
2050178354Ssam	return copyin(ireq->i_data, vap->iv_roamparms,
2051178354Ssam	    sizeof(vap->iv_roamparms));
2052178354Ssam}
2053178354Ssam
2054178354Ssamstatic int
2055178354Ssamcheckrate(const struct ieee80211_rateset *rs, int rate)
2056178354Ssam{
2057178354Ssam	int i;
2058178354Ssam
2059178354Ssam	if (rate == IEEE80211_FIXED_RATE_NONE)
2060178354Ssam		return 1;
2061178354Ssam	for (i = 0; i < rs->rs_nrates; i++)
2062178354Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2063178354Ssam			return 1;
2064178354Ssam	return 0;
2065178354Ssam}
2066178354Ssam
2067178354Ssamstatic int
2068178354Ssamcheckmcs(int mcs)
2069178354Ssam{
2070178354Ssam	if (mcs == IEEE80211_FIXED_RATE_NONE)
2071178354Ssam		return 1;
2072178354Ssam	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2073178354Ssam		return 0;
2074178354Ssam	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2075178354Ssam}
2076178354Ssam
2077178354Ssamstatic __noinline int
2078178354Ssamieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2079178354Ssam	const struct ieee80211req *ireq)
2080178354Ssam{
2081178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2082178354Ssam	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2083178354Ssam	struct ieee80211_txparam *src, *dst;
2084178354Ssam	const struct ieee80211_rateset *rs;
2085178354Ssam	int error, i, changed;
2086178354Ssam
2087178354Ssam	if (ireq->i_len != sizeof(parms))
2088178354Ssam		return EINVAL;
2089178354Ssam	error = copyin(ireq->i_data, &parms, sizeof(parms));
2090178354Ssam	if (error != 0)
2091178354Ssam		return error;
2092178354Ssam	changed = 0;
2093178354Ssam	/* validate parameters and check if anything changed */
2094178354Ssam	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_11NA; i++) {
2095178354Ssam		if (isclr(ic->ic_modecaps, i))
2096178354Ssam			continue;
2097178354Ssam		src = &parms.params[i];
2098178354Ssam		dst = &vap->iv_txparms[i];
2099178354Ssam		rs = &ic->ic_sup_rates[i];
2100178354Ssam		if (src->ucastrate != dst->ucastrate) {
2101178354Ssam			if (!checkrate(rs, src->ucastrate))
2102178354Ssam				return EINVAL;
2103178354Ssam			changed++;
2104178354Ssam		}
2105178354Ssam		if (src->mcastrate != dst->mcastrate) {
2106178354Ssam			if (!checkrate(rs, src->mcastrate))
2107178354Ssam				return EINVAL;
2108178354Ssam			changed++;
2109178354Ssam		}
2110178354Ssam		if (src->mgmtrate != dst->mgmtrate) {
2111178354Ssam			if (!checkrate(rs, src->mgmtrate))
2112178354Ssam				return EINVAL;
2113178354Ssam			changed++;
2114178354Ssam		}
2115178354Ssam		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2116178354Ssam			changed++;
2117178354Ssam	}
2118178354Ssam	/* 11n parameters are handled differently */
2119178354Ssam	for (; i < IEEE80211_MODE_MAX; i++) {
2120178354Ssam		if (isclr(ic->ic_modecaps, i))
2121178354Ssam			continue;
2122178354Ssam		src = &parms.params[i];
2123178354Ssam		dst = &vap->iv_txparms[i];
2124178354Ssam		rs = &ic->ic_sup_rates[i == IEEE80211_MODE_11NA ?
2125178354Ssam		    IEEE80211_MODE_11A : IEEE80211_MODE_11G];
2126178354Ssam		if (src->ucastrate != dst->ucastrate) {
2127178354Ssam			if (!checkmcs(src->ucastrate) &&
2128178354Ssam			    !checkrate(rs, src->ucastrate))
2129178354Ssam				return EINVAL;
2130178354Ssam			changed++;
2131178354Ssam		}
2132178354Ssam		if (src->mcastrate != dst->mcastrate) {
2133178354Ssam			if (!checkmcs(src->mcastrate) &&
2134178354Ssam			    !checkrate(rs, src->mcastrate))
2135178354Ssam				return EINVAL;
2136178354Ssam			changed++;
2137178354Ssam		}
2138178354Ssam		if (src->mgmtrate != dst->mgmtrate) {
2139178354Ssam			if (!checkmcs(src->mgmtrate) &&
2140178354Ssam			    !checkrate(rs, src->mgmtrate))
2141178354Ssam				return EINVAL;
2142178354Ssam			changed++;
2143178354Ssam		}
2144178354Ssam		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2145178354Ssam			changed++;
2146178354Ssam	}
2147178354Ssam	if (changed) {
2148178354Ssam		/*
2149178354Ssam		 * Copy new parameters in place and notify the
2150178354Ssam		 * driver so it can push state to the device.
2151178354Ssam		 */
2152178354Ssam		for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
2153178354Ssam			if (isset(ic->ic_modecaps, i))
2154178354Ssam				vap->iv_txparms[i] = parms.params[i];
2155178354Ssam		}
2156178354Ssam		/* XXX could be more intelligent,
2157178354Ssam		   e.g. don't reset if setting not being used */
2158178354Ssam		return ENETRESET;
2159178354Ssam	}
2160178354Ssam	return 0;
2161178354Ssam}
2162178354Ssam
2163178354Ssam/*
2164178354Ssam * Application Information Element support.
2165178354Ssam */
2166178354Ssamstatic int
2167178354Ssamsetappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2168178354Ssam{
2169178354Ssam	struct ieee80211_appie *app = *aie;
2170178354Ssam	struct ieee80211_appie *napp;
2171138568Ssam	int error;
2172178354Ssam
2173178354Ssam	if (ireq->i_len == 0) {		/* delete any existing ie */
2174178354Ssam		if (app != NULL) {
2175178354Ssam			*aie = NULL;	/* XXX racey */
2176186302Ssam			free(app, M_80211_NODE_IE);
2177178354Ssam		}
2178178354Ssam		return 0;
2179178354Ssam	}
2180178354Ssam	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2181178354Ssam		return EINVAL;
2182178354Ssam	/*
2183178354Ssam	 * Allocate a new appie structure and copy in the user data.
2184178354Ssam	 * When done swap in the new structure.  Note that we do not
2185178354Ssam	 * guard against users holding a ref to the old structure;
2186178354Ssam	 * this must be handled outside this code.
2187178354Ssam	 *
2188178354Ssam	 * XXX bad bad bad
2189178354Ssam	 */
2190186302Ssam	napp = (struct ieee80211_appie *) malloc(
2191184210Sdes	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2192178354Ssam	if (napp == NULL)
2193178354Ssam		return ENOMEM;
2194178354Ssam	/* XXX holding ic lock */
2195178354Ssam	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2196178354Ssam	if (error) {
2197186302Ssam		free(napp, M_80211_NODE_IE);
2198178354Ssam		return error;
2199178354Ssam	}
2200178354Ssam	napp->ie_len = ireq->i_len;
2201178354Ssam	*aie = napp;
2202178354Ssam	if (app != NULL)
2203186302Ssam		free(app, M_80211_NODE_IE);
2204178354Ssam	return 0;
2205178354Ssam}
2206178354Ssam
2207178354Ssamstatic void
2208178354Ssamsetwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2209178354Ssam{
2210178354Ssam	/* validate data is present as best we can */
2211178354Ssam	if (space == 0 || 2+ie[1] > space)
2212178354Ssam		return;
2213178354Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2214178354Ssam		vap->iv_wpa_ie = ie;
2215178354Ssam	else if (ie[0] == IEEE80211_ELEMID_RSN)
2216178354Ssam		vap->iv_rsn_ie = ie;
2217178354Ssam}
2218178354Ssam
2219178354Ssamstatic __noinline int
2220178354Ssamieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2221178354Ssam	const struct ieee80211req *ireq, int fc0)
2222178354Ssam{
2223178354Ssam	int error;
2224178354Ssam
2225178354Ssam	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2226178354Ssam
2227178354Ssam	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2228178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
2229178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2230178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS) {
2231178354Ssam			error = EINVAL;
2232178354Ssam			break;
2233178354Ssam		}
2234178354Ssam		error = setappie(&vap->iv_appie_beacon, ireq);
2235178354Ssam		if (error == 0)
2236178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2237178354Ssam		break;
2238178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2239178354Ssam		error = setappie(&vap->iv_appie_proberesp, ireq);
2240178354Ssam		break;
2241178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2242178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2243178354Ssam			error = setappie(&vap->iv_appie_assocresp, ireq);
2244178354Ssam		else
2245178354Ssam			error = EINVAL;
2246178354Ssam		break;
2247178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2248178354Ssam		error = setappie(&vap->iv_appie_probereq, ireq);
2249178354Ssam		break;
2250178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2251178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA)
2252178354Ssam			error = setappie(&vap->iv_appie_assocreq, ireq);
2253178354Ssam		else
2254178354Ssam			error = EINVAL;
2255178354Ssam		break;
2256178354Ssam	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2257178354Ssam		error = setappie(&vap->iv_appie_wpa, ireq);
2258178354Ssam		if (error == 0) {
2259178354Ssam			/*
2260178354Ssam			 * Must split single blob of data into separate
2261178354Ssam			 * WPA and RSN ie's because they go in different
2262178354Ssam			 * locations in the mgt frames.
2263178354Ssam			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2264178354Ssam			 */
2265178354Ssam			vap->iv_wpa_ie = NULL;
2266178354Ssam			vap->iv_rsn_ie = NULL;
2267178354Ssam			if (vap->iv_appie_wpa != NULL) {
2268178354Ssam				struct ieee80211_appie *appie =
2269178354Ssam				    vap->iv_appie_wpa;
2270178354Ssam				uint8_t *data = appie->ie_data;
2271178354Ssam
2272178354Ssam				/* XXX ie length validate is painful, cheat */
2273178354Ssam				setwparsnie(vap, data, appie->ie_len);
2274178354Ssam				setwparsnie(vap, data + 2 + data[1],
2275178354Ssam				    appie->ie_len - (2 + data[1]));
2276178354Ssam			}
2277178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2278178354Ssam			    vap->iv_opmode == IEEE80211_M_IBSS) {
2279178354Ssam				/*
2280178354Ssam				 * Must rebuild beacon frame as the update
2281178354Ssam				 * mechanism doesn't handle WPA/RSN ie's.
2282178354Ssam				 * Could extend it but it doesn't normally
2283178354Ssam				 * change; this is just to deal with hostapd
2284178354Ssam				 * plumbing the ie after the interface is up.
2285178354Ssam				 */
2286178354Ssam				error = ENETRESET;
2287178354Ssam			}
2288178354Ssam		}
2289178354Ssam		break;
2290178354Ssam	default:
2291178354Ssam		error = EINVAL;
2292178354Ssam		break;
2293178354Ssam	}
2294178354Ssam	return error;
2295178354Ssam}
2296178354Ssam
2297178354Ssamstatic __noinline int
2298178354Ssamieee80211_ioctl_setappie(struct ieee80211vap *vap,
2299178354Ssam	const struct ieee80211req *ireq)
2300178354Ssam{
2301178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2302178354Ssam	int error;
2303178354Ssam	uint8_t fc0;
2304178354Ssam
2305178354Ssam	fc0 = ireq->i_val & 0xff;
2306178354Ssam	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2307178354Ssam		return EINVAL;
2308178354Ssam	/* NB: could check iv_opmode and reject but hardly worth the effort */
2309178354Ssam	IEEE80211_LOCK(ic);
2310178354Ssam	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2311178354Ssam	IEEE80211_UNLOCK(ic);
2312178354Ssam	return error;
2313178354Ssam}
2314178354Ssam
2315178354Ssamstatic __noinline int
2316178354Ssamieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2317178354Ssam{
2318178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2319178354Ssam	struct ieee80211_chanswitch_req csr;
2320178354Ssam	struct ieee80211_channel *c;
2321178354Ssam	int error;
2322178354Ssam
2323178354Ssam	if (ireq->i_len != sizeof(csr))
2324178354Ssam		return EINVAL;
2325178354Ssam	error = copyin(ireq->i_data, &csr, sizeof(csr));
2326178354Ssam	if (error != 0)
2327178354Ssam		return error;
2328178354Ssam	if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
2329178354Ssam		return EINVAL;
2330178354Ssam	c = ieee80211_find_channel(ic,
2331178354Ssam	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2332178354Ssam	if (c == NULL)
2333178354Ssam		return ENOENT;
2334178354Ssam	IEEE80211_LOCK(ic);
2335178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2336178354Ssam		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2337178354Ssam	else
2338178354Ssam		error = EBUSY;
2339178354Ssam	IEEE80211_UNLOCK(ic);
2340178354Ssam	return error;
2341178354Ssam}
2342178354Ssam
2343178354Ssamstatic __noinline int
2344178354Ssamieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2345178354Ssam{
2346178354Ssam#define	IEEE80211_IOC_SCAN_FLAGS \
2347178354Ssam	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2348178354Ssam	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2349178354Ssam	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2350178354Ssam	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2351178354Ssam	 IEEE80211_IOC_SCAN_CHECK)
2352178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2353178354Ssam	struct ieee80211_scan_req sr;		/* XXX off stack? */
2354178354Ssam	int error, i;
2355178354Ssam
2356178354Ssam	/* NB: parent must be running */
2357178354Ssam	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2358178354Ssam		return ENXIO;
2359178354Ssam
2360178354Ssam	if (ireq->i_len != sizeof(sr))
2361178354Ssam		return EINVAL;
2362178354Ssam	error = copyin(ireq->i_data, &sr, sizeof(sr));
2363178354Ssam	if (error != 0)
2364178354Ssam		return error;
2365178354Ssam	/* convert duration */
2366178354Ssam	if (sr.sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2367178354Ssam		sr.sr_duration = IEEE80211_SCAN_FOREVER;
2368178354Ssam	else {
2369178354Ssam		if (sr.sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2370178354Ssam		    sr.sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2371178354Ssam			return EINVAL;
2372178354Ssam		sr.sr_duration = msecs_to_ticks(sr.sr_duration);
2373178354Ssam		if (sr.sr_duration < 1)
2374178354Ssam			sr.sr_duration = 1;
2375178354Ssam	}
2376178354Ssam	/* convert min/max channel dwell */
2377178354Ssam	if (sr.sr_mindwell != 0) {
2378178354Ssam		sr.sr_mindwell = msecs_to_ticks(sr.sr_mindwell);
2379178354Ssam		if (sr.sr_mindwell < 1)
2380178354Ssam			sr.sr_mindwell = 1;
2381178354Ssam	}
2382178354Ssam	if (sr.sr_maxdwell != 0) {
2383178354Ssam		sr.sr_maxdwell = msecs_to_ticks(sr.sr_maxdwell);
2384178354Ssam		if (sr.sr_maxdwell < 1)
2385178354Ssam			sr.sr_maxdwell = 1;
2386178354Ssam	}
2387178354Ssam	/* NB: silently reduce ssid count to what is supported */
2388178354Ssam	if (sr.sr_nssid > IEEE80211_SCAN_MAX_SSID)
2389178354Ssam		sr.sr_nssid = IEEE80211_SCAN_MAX_SSID;
2390178354Ssam	for (i = 0; i < sr.sr_nssid; i++)
2391178354Ssam		if (sr.sr_ssid[i].len > IEEE80211_NWID_LEN)
2392178354Ssam			return EINVAL;
2393178354Ssam	/* cleanse flags just in case, could reject if invalid flags */
2394178354Ssam	sr.sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2395178354Ssam	/*
2396178354Ssam	 * Add an implicit NOPICK if the vap is not marked UP.  This
2397178354Ssam	 * allows applications to scan without joining a bss (or picking
2398178354Ssam	 * a channel and setting up a bss) and without forcing manual
2399178354Ssam	 * roaming mode--you just need to mark the parent device UP.
2400178354Ssam	 */
2401178354Ssam	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2402178354Ssam		sr.sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2403178354Ssam
2404178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2405178354Ssam	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2406178354Ssam	    __func__, sr.sr_flags,
2407178354Ssam	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2408178354Ssam	    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell, sr.sr_nssid);
2409178354Ssam	/*
2410178354Ssam	 * If we are in INIT state then the driver has never had a chance
2411178354Ssam	 * to setup hardware state to do a scan; we must use the state
2412178354Ssam	 * machine to get us up to the SCAN state but once we reach SCAN
2413178354Ssam	 * state we then want to use the supplied params.  Stash the
2414178354Ssam	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2415178354Ssam	 * state machines will recognize this and use the stashed params
2416178354Ssam	 * to issue the scan request.
2417178354Ssam	 *
2418178354Ssam	 * Otherwise just invoke the scan machinery directly.
2419178354Ssam	 */
2420178354Ssam	IEEE80211_LOCK(ic);
2421178354Ssam	if (vap->iv_state == IEEE80211_S_INIT) {
2422178354Ssam		/* NB: clobbers previous settings */
2423178354Ssam		vap->iv_scanreq_flags = sr.sr_flags;
2424178354Ssam		vap->iv_scanreq_duration = sr.sr_duration;
2425178354Ssam		vap->iv_scanreq_nssid = sr.sr_nssid;
2426178354Ssam		for (i = 0; i < sr.sr_nssid; i++) {
2427178354Ssam			vap->iv_scanreq_ssid[i].len = sr.sr_ssid[i].len;
2428178354Ssam			memcpy(vap->iv_scanreq_ssid[i].ssid, sr.sr_ssid[i].ssid,
2429178354Ssam			    sr.sr_ssid[i].len);
2430178354Ssam		}
2431178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2432178354Ssam		IEEE80211_UNLOCK(ic);
2433178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2434178354Ssam	} else {
2435178354Ssam		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2436178354Ssam		IEEE80211_UNLOCK(ic);
2437178354Ssam		/* XXX neeed error return codes */
2438178354Ssam		if (sr.sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2439178354Ssam			(void) ieee80211_check_scan(vap, sr.sr_flags,
2440178354Ssam			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2441178354Ssam			    sr.sr_nssid,
2442178354Ssam			    /* NB: cheat, we assume structures are compatible */
2443178354Ssam			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2444178354Ssam		} else {
2445178354Ssam			(void) ieee80211_start_scan(vap, sr.sr_flags,
2446178354Ssam			    sr.sr_duration, sr.sr_mindwell, sr.sr_maxdwell,
2447178354Ssam			    sr.sr_nssid,
2448178354Ssam			    /* NB: cheat, we assume structures are compatible */
2449178354Ssam			    (const struct ieee80211_scan_ssid *) &sr.sr_ssid[0]);
2450178354Ssam		}
2451178354Ssam	}
2452178354Ssam	return error;
2453178354Ssam#undef IEEE80211_IOC_SCAN_FLAGS
2454178354Ssam}
2455178354Ssam
2456178354Ssamstatic __noinline int
2457178354Ssamieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2458178354Ssam{
2459178354Ssam	struct ieee80211_node *ni;
2460178354Ssam	struct ieee80211req_sta_vlan vlan;
2461178354Ssam	int error;
2462178354Ssam
2463178354Ssam	if (ireq->i_len != sizeof(vlan))
2464178354Ssam		return EINVAL;
2465178354Ssam	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2466178354Ssam	if (error != 0)
2467178354Ssam		return error;
2468178354Ssam	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2469178354Ssam		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2470178354Ssam		    vlan.sv_macaddr);
2471178354Ssam		if (ni == NULL)
2472178354Ssam			return ENOENT;
2473178354Ssam	} else
2474178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
2475178354Ssam	ni->ni_vlan = vlan.sv_vlan;
2476178354Ssam	ieee80211_free_node(ni);
2477178354Ssam	return error;
2478178354Ssam}
2479178354Ssam
2480178354Ssamstatic int
2481178354Ssamisvap11g(const struct ieee80211vap *vap)
2482178354Ssam{
2483178354Ssam	const struct ieee80211_node *bss = vap->iv_bss;
2484178354Ssam	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2485178354Ssam	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2486178354Ssam}
2487178354Ssam
2488178354Ssamstatic int
2489178354Ssamisvapht(const struct ieee80211vap *vap)
2490178354Ssam{
2491178354Ssam	const struct ieee80211_node *bss = vap->iv_bss;
2492178354Ssam	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2493178354Ssam	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2494178354Ssam}
2495178354Ssam
2496178354Ssamstatic __noinline int
2497178354Ssamieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2498178354Ssam{
2499178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2500178354Ssam	int error;
2501138568Ssam	const struct ieee80211_authenticator *auth;
2502170530Ssam	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2503138568Ssam	char tmpssid[IEEE80211_NWID_LEN];
2504170530Ssam	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2505138568Ssam	struct ieee80211_key *k;
2506138568Ssam	u_int kid;
2507181455Simp	uint32_t flags;
2508138568Ssam
2509138568Ssam	error = 0;
2510138568Ssam	switch (ireq->i_type) {
2511138568Ssam	case IEEE80211_IOC_SSID:
2512138568Ssam		if (ireq->i_val != 0 ||
2513138568Ssam		    ireq->i_len > IEEE80211_NWID_LEN)
2514138568Ssam			return EINVAL;
2515138568Ssam		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2516138568Ssam		if (error)
2517116742Ssam			break;
2518178354Ssam		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2519178354Ssam		vap->iv_des_ssid[0].len = ireq->i_len;
2520178354Ssam		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2521178354Ssam		vap->iv_des_nssid = (ireq->i_len > 0);
2522178354Ssam		error = ENETRESET;
2523138568Ssam		break;
2524138568Ssam	case IEEE80211_IOC_WEP:
2525138568Ssam		switch (ireq->i_val) {
2526138568Ssam		case IEEE80211_WEP_OFF:
2527178354Ssam			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2528178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2529116742Ssam			break;
2530138568Ssam		case IEEE80211_WEP_ON:
2531178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2532178354Ssam			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2533116742Ssam			break;
2534138568Ssam		case IEEE80211_WEP_MIXED:
2535178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2536178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2537116742Ssam			break;
2538138568Ssam		}
2539178354Ssam		error = ENETRESET;
2540138568Ssam		break;
2541138568Ssam	case IEEE80211_IOC_WEPKEY:
2542138568Ssam		kid = (u_int) ireq->i_val;
2543138568Ssam		if (kid >= IEEE80211_WEP_NKID)
2544138568Ssam			return EINVAL;
2545178354Ssam		k = &vap->iv_nw_keys[kid];
2546138568Ssam		if (ireq->i_len == 0) {
2547138568Ssam			/* zero-len =>'s delete any existing key */
2548178354Ssam			(void) ieee80211_crypto_delkey(vap, k);
2549127648Ssam			break;
2550138568Ssam		}
2551138568Ssam		if (ireq->i_len > sizeof(tmpkey))
2552138568Ssam			return EINVAL;
2553138568Ssam		memset(tmpkey, 0, sizeof(tmpkey));
2554138568Ssam		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2555138568Ssam		if (error)
2556138568Ssam			break;
2557178354Ssam		ieee80211_key_update_begin(vap);
2558144960Ssam		k->wk_keyix = kid;	/* NB: force fixed key id */
2559178354Ssam		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2560144960Ssam		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2561138568Ssam			k->wk_keylen = ireq->i_len;
2562138568Ssam			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2563179395Ssam			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2564179395Ssam			if  (!ieee80211_crypto_setkey(vap, k))
2565127648Ssam				error = EINVAL;
2566138568Ssam		} else
2567138568Ssam			error = EINVAL;
2568178354Ssam		ieee80211_key_update_end(vap);
2569138568Ssam		break;
2570138568Ssam	case IEEE80211_IOC_WEPTXKEY:
2571138568Ssam		kid = (u_int) ireq->i_val;
2572139519Ssam		if (kid >= IEEE80211_WEP_NKID &&
2573170530Ssam		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2574138568Ssam			return EINVAL;
2575178354Ssam		vap->iv_def_txkey = kid;
2576138568Ssam		break;
2577138568Ssam	case IEEE80211_IOC_AUTHMODE:
2578138568Ssam		switch (ireq->i_val) {
2579138568Ssam		case IEEE80211_AUTH_WPA:
2580138568Ssam		case IEEE80211_AUTH_8021X:	/* 802.1x */
2581138568Ssam		case IEEE80211_AUTH_OPEN:	/* open */
2582138568Ssam		case IEEE80211_AUTH_SHARED:	/* shared-key */
2583138568Ssam		case IEEE80211_AUTH_AUTO:	/* auto */
2584138568Ssam			auth = ieee80211_authenticator_get(ireq->i_val);
2585138568Ssam			if (auth == NULL)
2586138568Ssam				return EINVAL;
2587127648Ssam			break;
2588116742Ssam		default:
2589138568Ssam			return EINVAL;
2590138568Ssam		}
2591138568Ssam		switch (ireq->i_val) {
2592138568Ssam		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2593178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2594138568Ssam			ireq->i_val = IEEE80211_AUTH_8021X;
2595127646Ssam			break;
2596138568Ssam		case IEEE80211_AUTH_OPEN:	/* open */
2597178354Ssam			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2598138568Ssam			break;
2599138568Ssam		case IEEE80211_AUTH_SHARED:	/* shared-key */
2600138568Ssam		case IEEE80211_AUTH_8021X:	/* 802.1x */
2601178354Ssam			vap->iv_flags &= ~IEEE80211_F_WPA;
2602138568Ssam			/* both require a key so mark the PRIVACY capability */
2603178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2604138568Ssam			break;
2605138568Ssam		case IEEE80211_AUTH_AUTO:	/* auto */
2606178354Ssam			vap->iv_flags &= ~IEEE80211_F_WPA;
2607138568Ssam			/* XXX PRIVACY handling? */
2608138568Ssam			/* XXX what's the right way to do this? */
2609138568Ssam			break;
2610116742Ssam		}
2611138568Ssam		/* NB: authenticator attach/detach happens on state change */
2612178354Ssam		vap->iv_bss->ni_authmode = ireq->i_val;
2613138568Ssam		/* XXX mixed/mode/usage? */
2614178354Ssam		vap->iv_auth = auth;
2615178354Ssam		error = ENETRESET;
2616116742Ssam		break;
2617138568Ssam	case IEEE80211_IOC_CHANNEL:
2618178354Ssam		error = ieee80211_ioctl_setchannel(vap, ireq);
2619138568Ssam		break;
2620138568Ssam	case IEEE80211_IOC_POWERSAVE:
2621138568Ssam		switch (ireq->i_val) {
2622138568Ssam		case IEEE80211_POWERSAVE_OFF:
2623178354Ssam			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2624178354Ssam				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2625178354Ssam				error = ERESTART;
2626116742Ssam			}
2627116742Ssam			break;
2628138568Ssam		case IEEE80211_POWERSAVE_ON:
2629178354Ssam			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2630178354Ssam				error = EOPNOTSUPP;
2631178354Ssam			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2632178354Ssam				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2633178354Ssam				error = ERESTART;
2634116742Ssam			}
2635116742Ssam			break;
2636138568Ssam		default:
2637138568Ssam			error = EINVAL;
2638116742Ssam			break;
2639138568Ssam		}
2640138568Ssam		break;
2641138568Ssam	case IEEE80211_IOC_POWERSAVESLEEP:
2642138568Ssam		if (ireq->i_val < 0)
2643138568Ssam			return EINVAL;
2644138568Ssam		ic->ic_lintval = ireq->i_val;
2645178354Ssam		error = ERESTART;
2646138568Ssam		break;
2647138568Ssam	case IEEE80211_IOC_RTSTHRESHOLD:
2648148292Ssam		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2649148292Ssam		      ireq->i_val <= IEEE80211_RTS_MAX))
2650138568Ssam			return EINVAL;
2651178354Ssam		vap->iv_rtsthreshold = ireq->i_val;
2652178354Ssam		error = ERESTART;
2653138568Ssam		break;
2654138568Ssam	case IEEE80211_IOC_PROTMODE:
2655138568Ssam		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2656138568Ssam			return EINVAL;
2657138568Ssam		ic->ic_protmode = ireq->i_val;
2658138568Ssam		/* NB: if not operating in 11g this can wait */
2659170530Ssam		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2660170530Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2661178354Ssam			error = ERESTART;
2662138568Ssam		break;
2663138568Ssam	case IEEE80211_IOC_TXPOWER:
2664138568Ssam		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2665178354Ssam			return EOPNOTSUPP;
2666170530Ssam		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2667170530Ssam		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2668138568Ssam			return EINVAL;
2669138568Ssam		ic->ic_txpowlimit = ireq->i_val;
2670178354Ssam		error = ERESTART;
2671138568Ssam		break;
2672138568Ssam	case IEEE80211_IOC_ROAMING:
2673138568Ssam		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2674138568Ssam		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2675138568Ssam			return EINVAL;
2676178354Ssam		vap->iv_roaming = ireq->i_val;
2677138568Ssam		/* XXXX reset? */
2678138568Ssam		break;
2679138568Ssam	case IEEE80211_IOC_PRIVACY:
2680138568Ssam		if (ireq->i_val) {
2681138568Ssam			/* XXX check for key state? */
2682178354Ssam			vap->iv_flags |= IEEE80211_F_PRIVACY;
2683138568Ssam		} else
2684178354Ssam			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2685178354Ssam		/* XXX ERESTART? */
2686138568Ssam		break;
2687138568Ssam	case IEEE80211_IOC_DROPUNENCRYPTED:
2688138568Ssam		if (ireq->i_val)
2689178354Ssam			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2690138568Ssam		else
2691178354Ssam			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2692178354Ssam		/* XXX ERESTART? */
2693138568Ssam		break;
2694138568Ssam	case IEEE80211_IOC_WPAKEY:
2695178354Ssam		error = ieee80211_ioctl_setkey(vap, ireq);
2696138568Ssam		break;
2697138568Ssam	case IEEE80211_IOC_DELKEY:
2698178354Ssam		error = ieee80211_ioctl_delkey(vap, ireq);
2699138568Ssam		break;
2700138568Ssam	case IEEE80211_IOC_MLME:
2701178354Ssam		error = ieee80211_ioctl_setmlme(vap, ireq);
2702138568Ssam		break;
2703138568Ssam	case IEEE80211_IOC_COUNTERMEASURES:
2704138568Ssam		if (ireq->i_val) {
2705178354Ssam			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2706178354Ssam				return EOPNOTSUPP;
2707178354Ssam			vap->iv_flags |= IEEE80211_F_COUNTERM;
2708138568Ssam		} else
2709178354Ssam			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2710178354Ssam		/* XXX ERESTART? */
2711138568Ssam		break;
2712138568Ssam	case IEEE80211_IOC_WPA:
2713138568Ssam		if (ireq->i_val > 3)
2714138568Ssam			return EINVAL;
2715138568Ssam		/* XXX verify ciphers available */
2716181455Simp		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2717138568Ssam		switch (ireq->i_val) {
2718138568Ssam		case 1:
2719181455Simp			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2720181455Simp				return EOPNOTSUPP;
2721181455Simp			flags |= IEEE80211_F_WPA1;
2722116742Ssam			break;
2723138568Ssam		case 2:
2724181455Simp			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2725181455Simp				return EOPNOTSUPP;
2726181455Simp			flags |= IEEE80211_F_WPA2;
2727116742Ssam			break;
2728138568Ssam		case 3:
2729181455Simp			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2730187060Ssam				return EOPNOTSUPP;
2731181455Simp			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2732127648Ssam			break;
2733181455Simp		default:	/*  Can't set any -> error */
2734181455Simp			return EOPNOTSUPP;
2735138568Ssam		}
2736181455Simp		vap->iv_flags = flags;
2737178354Ssam		error = ERESTART;	/* NB: can change beacon frame */
2738138568Ssam		break;
2739138568Ssam	case IEEE80211_IOC_WME:
2740138568Ssam		if (ireq->i_val) {
2741178354Ssam			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2742178354Ssam				return EOPNOTSUPP;
2743178354Ssam			ieee80211_syncflag(vap, IEEE80211_F_WME);
2744138568Ssam		} else
2745178354Ssam			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2746178354Ssam		error = ERESTART;	/* NB: can change beacon frame */
2747138568Ssam		break;
2748138568Ssam	case IEEE80211_IOC_HIDESSID:
2749138568Ssam		if (ireq->i_val)
2750178354Ssam			vap->iv_flags |= IEEE80211_F_HIDESSID;
2751138568Ssam		else
2752178354Ssam			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2753178354Ssam		error = ERESTART;		/* XXX ENETRESET? */
2754138568Ssam		break;
2755138568Ssam	case IEEE80211_IOC_APBRIDGE:
2756138568Ssam		if (ireq->i_val == 0)
2757178354Ssam			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2758138568Ssam		else
2759178354Ssam			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2760138568Ssam		break;
2761138568Ssam	case IEEE80211_IOC_BSSID:
2762138568Ssam		if (ireq->i_len != sizeof(tmpbssid))
2763138568Ssam			return EINVAL;
2764138568Ssam		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2765138568Ssam		if (error)
2766127648Ssam			break;
2767178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2768178354Ssam		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2769178354Ssam			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2770138568Ssam		else
2771178354Ssam			vap->iv_flags |= IEEE80211_F_DESBSSID;
2772178354Ssam		error = ENETRESET;
2773138568Ssam		break;
2774138568Ssam	case IEEE80211_IOC_CHANLIST:
2775178354Ssam		error = ieee80211_ioctl_setchanlist(vap, ireq);
2776138568Ssam		break;
2777178354Ssam#define	OLD_IEEE80211_IOC_SCAN_REQ	23
2778178354Ssam#ifdef OLD_IEEE80211_IOC_SCAN_REQ
2779178354Ssam	case OLD_IEEE80211_IOC_SCAN_REQ:
2780178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2781178354Ssam			"%s: active scan request\n", __func__);
2782178354Ssam		/*
2783178354Ssam		 * If we are in INIT state then the driver has never
2784178354Ssam		 * had a chance to setup hardware state to do a scan;
2785178354Ssam		 * use the state machine to get us up the SCAN state.
2786178354Ssam		 * Otherwise just invoke the scan machinery to start
2787178354Ssam		 * a one-time scan.
2788178354Ssam		 */
2789178354Ssam		if (vap->iv_state == IEEE80211_S_INIT)
2790178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2791178354Ssam		else
2792178354Ssam			(void) ieee80211_start_scan(vap,
2793178354Ssam				IEEE80211_SCAN_ACTIVE |
2794178354Ssam				IEEE80211_SCAN_NOPICK |
2795178354Ssam				IEEE80211_SCAN_ONCE,
2796178354Ssam				IEEE80211_SCAN_FOREVER, 0, 0,
2797178354Ssam				/* XXX use ioctl params */
2798178354Ssam				vap->iv_des_nssid, vap->iv_des_ssid);
2799178354Ssam		break;
2800178354Ssam#endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2801138568Ssam	case IEEE80211_IOC_SCAN_REQ:
2802178354Ssam		error = ieee80211_ioctl_scanreq(vap, ireq);
2803116742Ssam		break;
2804178354Ssam	case IEEE80211_IOC_SCAN_CANCEL:
2805178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2806178354Ssam		    "%s: cancel scan\n", __func__);
2807178354Ssam		ieee80211_cancel_scan(vap);
2808178354Ssam		break;
2809178354Ssam	case IEEE80211_IOC_HTCONF:
2810178354Ssam		if (ireq->i_val & 1)
2811178354Ssam			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_HT);
2812178354Ssam		else
2813178354Ssam			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_HT);
2814178354Ssam		if (ireq->i_val & 2)
2815178354Ssam			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_USEHT40);
2816178354Ssam		else
2817178354Ssam			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_USEHT40);
2818178354Ssam		error = ENETRESET;
2819178354Ssam		break;
2820138568Ssam	case IEEE80211_IOC_ADDMAC:
2821138568Ssam	case IEEE80211_IOC_DELMAC:
2822178354Ssam		error = ieee80211_ioctl_macmac(vap, ireq);
2823138568Ssam		break;
2824138568Ssam	case IEEE80211_IOC_MACCMD:
2825178354Ssam		error = ieee80211_ioctl_setmaccmd(vap, ireq);
2826138568Ssam		break;
2827157172Ssam	case IEEE80211_IOC_STA_STATS:
2828178354Ssam		error = ieee80211_ioctl_setstastats(vap, ireq);
2829157172Ssam		break;
2830138568Ssam	case IEEE80211_IOC_STA_TXPOW:
2831178354Ssam		error = ieee80211_ioctl_setstatxpow(vap, ireq);
2832138568Ssam		break;
2833138568Ssam	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
2834138568Ssam	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
2835138568Ssam	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
2836138568Ssam	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
2837138568Ssam	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
2838138568Ssam	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2839178354Ssam		error = ieee80211_ioctl_setwmeparam(vap, ireq);
2840138568Ssam		break;
2841138568Ssam	case IEEE80211_IOC_DTIM_PERIOD:
2842178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2843178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS)
2844138568Ssam			return EINVAL;
2845138568Ssam		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
2846138568Ssam		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2847178354Ssam			vap->iv_dtim_period = ireq->i_val;
2848138568Ssam			error = ENETRESET;		/* requires restart */
2849138568Ssam		} else
2850138568Ssam			error = EINVAL;
2851138568Ssam		break;
2852138568Ssam	case IEEE80211_IOC_BEACON_INTERVAL:
2853178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2854178354Ssam		    vap->iv_opmode != IEEE80211_M_IBSS)
2855138568Ssam			return EINVAL;
2856138568Ssam		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
2857138568Ssam		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2858148843Ssam			ic->ic_bintval = ireq->i_val;
2859138568Ssam			error = ENETRESET;		/* requires restart */
2860138568Ssam		} else
2861138568Ssam			error = EINVAL;
2862138568Ssam		break;
2863147794Ssam	case IEEE80211_IOC_PUREG:
2864147794Ssam		if (ireq->i_val)
2865178354Ssam			vap->iv_flags |= IEEE80211_F_PUREG;
2866147794Ssam		else
2867178354Ssam			vap->iv_flags &= ~IEEE80211_F_PUREG;
2868147794Ssam		/* NB: reset only if we're operating on an 11g channel */
2869178354Ssam		if (isvap11g(vap))
2870147794Ssam			error = ENETRESET;
2871147794Ssam		break;
2872170530Ssam	case IEEE80211_IOC_FF:
2873170530Ssam		if (ireq->i_val) {
2874178354Ssam			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
2875178354Ssam				return EOPNOTSUPP;
2876178354Ssam			vap->iv_flags |= IEEE80211_F_FF;
2877170530Ssam		} else
2878178354Ssam			vap->iv_flags &= ~IEEE80211_F_FF;
2879178354Ssam		error = ERESTART;
2880170530Ssam		break;
2881170530Ssam	case IEEE80211_IOC_TURBOP:
2882170530Ssam		if (ireq->i_val) {
2883178354Ssam			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
2884178354Ssam				return EOPNOTSUPP;
2885178354Ssam			vap->iv_flags |= IEEE80211_F_TURBOP;
2886170530Ssam		} else
2887178354Ssam			vap->iv_flags &= ~IEEE80211_F_TURBOP;
2888170530Ssam		error = ENETRESET;
2889170530Ssam		break;
2890170530Ssam	case IEEE80211_IOC_BGSCAN:
2891170530Ssam		if (ireq->i_val) {
2892178354Ssam			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
2893178354Ssam				return EOPNOTSUPP;
2894178354Ssam			vap->iv_flags |= IEEE80211_F_BGSCAN;
2895170530Ssam		} else
2896178354Ssam			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
2897170530Ssam		break;
2898170530Ssam	case IEEE80211_IOC_BGSCAN_IDLE:
2899170530Ssam		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
2900178354Ssam			vap->iv_bgscanidle = ireq->i_val*hz/1000;
2901170530Ssam		else
2902170530Ssam			error = EINVAL;
2903170530Ssam		break;
2904170530Ssam	case IEEE80211_IOC_BGSCAN_INTERVAL:
2905170530Ssam		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
2906178354Ssam			vap->iv_bgscanintvl = ireq->i_val*hz;
2907170530Ssam		else
2908170530Ssam			error = EINVAL;
2909170530Ssam		break;
2910170530Ssam	case IEEE80211_IOC_SCANVALID:
2911170530Ssam		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
2912178354Ssam			vap->iv_scanvalid = ireq->i_val*hz;
2913170530Ssam		else
2914170530Ssam			error = EINVAL;
2915170530Ssam		break;
2916148292Ssam	case IEEE80211_IOC_FRAGTHRESHOLD:
2917178354Ssam		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
2918148292Ssam		    ireq->i_val != IEEE80211_FRAG_MAX)
2919178354Ssam			return EOPNOTSUPP;
2920148292Ssam		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
2921148292Ssam		      ireq->i_val <= IEEE80211_FRAG_MAX))
2922148292Ssam			return EINVAL;
2923178354Ssam		vap->iv_fragthreshold = ireq->i_val;
2924178354Ssam		error = ERESTART;
2925148292Ssam		break;
2926153421Ssam	case IEEE80211_IOC_BURST:
2927153421Ssam		if (ireq->i_val) {
2928178354Ssam			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
2929178354Ssam				return EOPNOTSUPP;
2930178354Ssam			ieee80211_syncflag(vap, IEEE80211_F_BURST);
2931153421Ssam		} else
2932178354Ssam			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
2933178354Ssam		error = ERESTART;
2934153421Ssam		break;
2935160686Ssam	case IEEE80211_IOC_BMISSTHRESHOLD:
2936160686Ssam		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
2937160686Ssam		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
2938160686Ssam			return EINVAL;
2939178354Ssam		vap->iv_bmissthreshold = ireq->i_val;
2940178354Ssam		error = ERESTART;
2941160686Ssam		break;
2942170530Ssam	case IEEE80211_IOC_CURCHAN:
2943178354Ssam		error = ieee80211_ioctl_setcurchan(vap, ireq);
2944170530Ssam		break;
2945170530Ssam	case IEEE80211_IOC_SHORTGI:
2946170530Ssam		if (ireq->i_val) {
2947170530Ssam#define	IEEE80211_HTCAP_SHORTGI \
2948170530Ssam	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
2949178354Ssam			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
2950170530Ssam				return EINVAL;
2951170530Ssam			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
2952178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI20;
2953170530Ssam			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
2954178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI40;
2955170530Ssam#undef IEEE80211_HTCAP_SHORTGI
2956170530Ssam		} else
2957178354Ssam			vap->iv_flags_ext &=
2958170530Ssam			    ~(IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40);
2959178354Ssam		error = ERESTART;
2960170530Ssam		break;
2961170530Ssam	case IEEE80211_IOC_AMPDU:
2962178354Ssam		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
2963178354Ssam			return EINVAL;
2964178354Ssam		if (ireq->i_val & 1)
2965178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
2966178354Ssam		else
2967178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_TX;
2968178354Ssam		if (ireq->i_val & 2)
2969178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
2970178354Ssam		else
2971178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMPDU_RX;
2972170530Ssam		/* NB: reset only if we're operating on an 11n channel */
2973178354Ssam		if (isvapht(vap))
2974178354Ssam			error = ERESTART;
2975170530Ssam		break;
2976170530Ssam	case IEEE80211_IOC_AMPDU_LIMIT:
2977178354Ssam		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
2978178354Ssam		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
2979178354Ssam			return EINVAL;
2980178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2981178354Ssam			vap->iv_ampdu_rxmax = ireq->i_val;
2982178354Ssam		else
2983178354Ssam			vap->iv_ampdu_limit = ireq->i_val;
2984178354Ssam		error = ERESTART;
2985170530Ssam		break;
2986170530Ssam	case IEEE80211_IOC_AMPDU_DENSITY:
2987178354Ssam		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
2988178354Ssam		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
2989178354Ssam			return EINVAL;
2990178354Ssam		vap->iv_ampdu_density = ireq->i_val;
2991178354Ssam		error = ERESTART;
2992170530Ssam		break;
2993170530Ssam	case IEEE80211_IOC_AMSDU:
2994178354Ssam		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
2995178354Ssam			return EINVAL;
2996178354Ssam		if (ireq->i_val & 1)
2997178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
2998178354Ssam		else
2999178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_TX;
3000178354Ssam		if (ireq->i_val & 2)
3001178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
3002178354Ssam		else
3003178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_AMSDU_RX;
3004170530Ssam		/* NB: reset only if we're operating on an 11n channel */
3005178354Ssam		if (isvapht(vap))
3006178354Ssam			error = ERESTART;
3007170530Ssam		break;
3008170530Ssam	case IEEE80211_IOC_AMSDU_LIMIT:
3009170530Ssam		/* XXX validate */
3010178354Ssam		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
3011170530Ssam		break;
3012170530Ssam	case IEEE80211_IOC_PUREN:
3013170530Ssam		if (ireq->i_val) {
3014178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
3015170530Ssam				return EINVAL;
3016178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_PUREN;
3017170530Ssam		} else
3018178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_PUREN;
3019170530Ssam		/* NB: reset only if we're operating on an 11n channel */
3020178354Ssam		if (isvapht(vap))
3021178354Ssam			error = ERESTART;
3022170530Ssam		break;
3023170530Ssam	case IEEE80211_IOC_DOTH:
3024170530Ssam		if (ireq->i_val) {
3025170530Ssam#if 0
3026170530Ssam			/* XXX no capability */
3027178354Ssam			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3028178354Ssam				return EOPNOTSUPP;
3029170530Ssam#endif
3030178354Ssam			vap->iv_flags |= IEEE80211_F_DOTH;
3031170530Ssam		} else
3032178354Ssam			vap->iv_flags &= ~IEEE80211_F_DOTH;
3033178354Ssam		error = ENETRESET;
3034170530Ssam		break;
3035178354Ssam	case IEEE80211_IOC_REGDOMAIN:
3036178354Ssam		error = ieee80211_ioctl_setregdomain(vap, ireq);
3037178354Ssam		break;
3038178354Ssam	case IEEE80211_IOC_ROAM:
3039178354Ssam		error = ieee80211_ioctl_setroam(vap, ireq);
3040178354Ssam		break;
3041178354Ssam	case IEEE80211_IOC_TXPARAMS:
3042178354Ssam		error = ieee80211_ioctl_settxparams(vap, ireq);
3043178354Ssam		break;
3044170530Ssam	case IEEE80211_IOC_HTCOMPAT:
3045170530Ssam		if (ireq->i_val) {
3046178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) == 0)
3047178354Ssam				return EOPNOTSUPP;
3048178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_HTCOMPAT;
3049170530Ssam		} else
3050178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_HTCOMPAT;
3051170530Ssam		/* NB: reset only if we're operating on an 11n channel */
3052178354Ssam		if (isvapht(vap))
3053178354Ssam			error = ERESTART;
3054170530Ssam		break;
3055178354Ssam	case IEEE80211_IOC_DWDS:
3056178354Ssam		if (ireq->i_val) {
3057178354Ssam			/* NB: DWDS only makes sense for WDS-capable devices */
3058178354Ssam			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3059178354Ssam				return EOPNOTSUPP;
3060178354Ssam			/* NB: DWDS is used only with ap+sta vaps */
3061178354Ssam			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3062178354Ssam			    vap->iv_opmode != IEEE80211_M_STA)
3063178354Ssam				return EINVAL;
3064178354Ssam			vap->iv_flags |= IEEE80211_F_DWDS;
3065178354Ssam		} else
3066178354Ssam			vap->iv_flags &= ~IEEE80211_F_DWDS;
3067178354Ssam		break;
3068172062Ssam	case IEEE80211_IOC_INACTIVITY:
3069172062Ssam		if (ireq->i_val)
3070178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3071172062Ssam		else
3072178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3073172062Ssam		break;
3074178354Ssam	case IEEE80211_IOC_APPIE:
3075178354Ssam		error = ieee80211_ioctl_setappie(vap, ireq);
3076178354Ssam		break;
3077178354Ssam	case IEEE80211_IOC_WPS:
3078178354Ssam		if (ireq->i_val) {
3079178354Ssam			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3080178354Ssam				return EOPNOTSUPP;
3081178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3082178354Ssam		} else
3083178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3084178354Ssam		break;
3085178354Ssam	case IEEE80211_IOC_TSN:
3086178354Ssam		if (ireq->i_val) {
3087178354Ssam			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3088178354Ssam				return EOPNOTSUPP;
3089178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3090178354Ssam		} else
3091178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3092178354Ssam		break;
3093178354Ssam	case IEEE80211_IOC_CHANSWITCH:
3094178354Ssam		error = ieee80211_ioctl_chanswitch(vap, ireq);
3095178354Ssam		break;
3096178354Ssam	case IEEE80211_IOC_DFS:
3097178354Ssam		if (ireq->i_val) {
3098178354Ssam			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3099178354Ssam				return EOPNOTSUPP;
3100178354Ssam			/* NB: DFS requires 11h support */
3101178354Ssam			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3102178354Ssam				return EINVAL;
3103178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3104178354Ssam		} else
3105178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3106178354Ssam		break;
3107178354Ssam	case IEEE80211_IOC_DOTD:
3108178354Ssam		if (ireq->i_val)
3109178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3110178354Ssam		else
3111178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3112178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA)
3113178354Ssam			error = ENETRESET;
3114178354Ssam		break;
3115173273Ssam	case IEEE80211_IOC_HTPROTMODE:
3116173273Ssam		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3117173273Ssam			return EINVAL;
3118173273Ssam		ic->ic_htprotmode = ireq->i_val ?
3119173273Ssam		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3120173273Ssam		/* NB: if not operating in 11n this can wait */
3121178354Ssam		if (isvapht(vap))
3122173273Ssam			error = ERESTART;
3123173273Ssam		break;
3124178354Ssam	case IEEE80211_IOC_STA_VLAN:
3125178354Ssam		error = ieee80211_ioctl_setstavlan(vap, ireq);
3126173273Ssam		break;
3127183255Ssam	case IEEE80211_IOC_SMPS:
3128183255Ssam		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3129183255Ssam		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3130183255Ssam			return EINVAL;
3131183255Ssam		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3132183255Ssam		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3133183255Ssam			return EOPNOTSUPP;
3134183255Ssam		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3135183255Ssam			ireq->i_val;
3136183255Ssam		/* NB: if not operating in 11n this can wait */
3137183255Ssam		if (isvapht(vap))
3138183255Ssam			error = ERESTART;
3139183255Ssam		break;
3140183256Ssam	case IEEE80211_IOC_RIFS:
3141183256Ssam		if (ireq->i_val != 0) {
3142183256Ssam			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3143183256Ssam				return EOPNOTSUPP;
3144183256Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RIFS;
3145183256Ssam		} else
3146183256Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RIFS;
3147183256Ssam		/* NB: if not operating in 11n this can wait */
3148183256Ssam		if (isvapht(vap))
3149183256Ssam			error = ERESTART;
3150183256Ssam		break;
3151186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
3152186904Ssam	case IEEE80211_IOC_TDMA_SLOT:
3153186904Ssam	case IEEE80211_IOC_TDMA_SLOTCNT:
3154186904Ssam	case IEEE80211_IOC_TDMA_SLOTLEN:
3155186904Ssam	case IEEE80211_IOC_TDMA_BINTERVAL:
3156186904Ssam		error = ieee80211_tdma_ioctl_set80211(vap, ireq);
3157186904Ssam		break;
3158186904Ssam#endif
3159138568Ssam	default:
3160138568Ssam		error = EINVAL;
3161138568Ssam		break;
3162138568Ssam	}
3163178354Ssam	/*
3164178354Ssam	 * The convention is that ENETRESET means an operation
3165178354Ssam	 * requires a complete re-initialization of the device (e.g.
3166178354Ssam	 * changing something that affects the association state).
3167178354Ssam	 * ERESTART means the request may be handled with only a
3168178354Ssam	 * reload of the hardware state.  We hand ERESTART requests
3169178354Ssam	 * to the iv_reset callback so the driver can decide.  If
3170178354Ssam	 * a device does not fillin iv_reset then it defaults to one
3171178354Ssam	 * that returns ENETRESET.  Otherwise a driver may return
3172178354Ssam	 * ENETRESET (in which case a full reset will be done) or
3173178354Ssam	 * 0 to mean there's no need to do anything (e.g. when the
3174178354Ssam	 * change has no effect on the driver/device).
3175178354Ssam	 */
3176178354Ssam	if (error == ERESTART)
3177178354Ssam		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3178178354Ssam		    vap->iv_reset(vap, ireq->i_type) : 0;
3179178354Ssam	if (error == ENETRESET) {
3180178354Ssam		/* XXX need to re-think AUTO handling */
3181178354Ssam		if (IS_UP_AUTO(vap))
3182178354Ssam			ieee80211_init(vap);
3183178354Ssam		error = 0;
3184178354Ssam	}
3185138568Ssam	return error;
3186138568Ssam}
3187138568Ssam
3188178354Ssam/*
3189178354Ssam * Rebuild the parent's multicast address list after an add/del
3190178354Ssam * of a multicast address for a vap.  We have no way to tell
3191178354Ssam * what happened above to optimize the work so we purge the entire
3192178354Ssam * list and rebuild from scratch.  This is way expensive.
3193178354Ssam * Note also the half-baked workaround for if_addmulti calling
3194178354Ssam * back to the parent device; there's no way to insert mcast
3195178354Ssam * entries quietly and/or cheaply.
3196178354Ssam */
3197178354Ssamstatic void
3198178354Ssamieee80211_ioctl_updatemulti(struct ieee80211com *ic)
3199178354Ssam{
3200178354Ssam	struct ifnet *parent = ic->ic_ifp;
3201178354Ssam	struct ieee80211vap *vap;
3202178354Ssam	void *ioctl;
3203178354Ssam
3204178354Ssam	IEEE80211_LOCK(ic);
3205178354Ssam	if_purgemaddrs(parent);
3206178354Ssam	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3207178354Ssam	parent->if_ioctl = NULL;
3208178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3209178354Ssam		struct ifnet *ifp = vap->iv_ifp;
3210178354Ssam		struct ifmultiaddr *ifma;
3211178354Ssam
3212178354Ssam		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
3213178354Ssam			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3214178354Ssam	}
3215178354Ssam	parent->if_ioctl = ioctl;
3216178354Ssam
3217178354Ssam	ic->ic_update_mcast(ic->ic_ifp);
3218178354Ssam	IEEE80211_UNLOCK(ic);
3219178354Ssam}
3220178354Ssam
3221138568Ssamint
3222178354Ssamieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3223138568Ssam{
3224178354Ssam	struct ieee80211vap *vap;
3225178354Ssam	struct ieee80211com *ic;
3226138568Ssam	int error = 0;
3227138568Ssam	struct ifreq *ifr;
3228138568Ssam	struct ifaddr *ifa;			/* XXX */
3229138568Ssam
3230178354Ssam	vap = ifp->if_softc;
3231178354Ssam	if (vap == NULL) {
3232178354Ssam		/*
3233178354Ssam		 * During detach we clear the backpointer in the softc
3234178354Ssam		 * so any ioctl request through the ifnet that arrives
3235178354Ssam		 * before teardown is ignored/rejected.  In particular
3236178354Ssam		 * this hack handles destroying a vap used by an app
3237178354Ssam		 * like wpa_supplicant that will respond to the vap
3238178354Ssam		 * being forced into INIT state by immediately trying
3239178354Ssam		 * to force it back up.  We can yank this hack if/when
3240178354Ssam		 * we can destroy the ifnet before cleaning up vap state.
3241178354Ssam		 */
3242178354Ssam		return ENXIO;
3243178354Ssam	}
3244138568Ssam	switch (cmd) {
3245178354Ssam	case SIOCSIFFLAGS:
3246178354Ssam		ic = vap->iv_ic;
3247178354Ssam		IEEE80211_LOCK(ic);
3248178354Ssam		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3249178354Ssam		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3250178354Ssam		if (ifp->if_flags & IFF_UP) {
3251178354Ssam			/*
3252178354Ssam			 * Bring ourself up unless we're already operational.
3253178354Ssam			 * If we're the first vap and the parent is not up
3254178354Ssam			 * then it will automatically be brought up as a
3255178354Ssam			 * side-effect of bringing ourself up.
3256178354Ssam			 */
3257178354Ssam			if (vap->iv_state == IEEE80211_S_INIT)
3258178354Ssam				ieee80211_start_locked(vap);
3259178354Ssam		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3260178354Ssam			/*
3261178354Ssam			 * Stop ourself.  If we are the last vap to be
3262178354Ssam			 * marked down the parent will also be taken down.
3263178354Ssam			 */
3264178354Ssam			ieee80211_stop_locked(vap);
3265178354Ssam		}
3266178354Ssam		IEEE80211_UNLOCK(ic);
3267187678Sthompsa		/* Wait for parent ioctl handler if it was queued */
3268188533Sthompsa		ieee80211_waitfor_parent(ic);
3269178354Ssam		break;
3270178354Ssam	case SIOCADDMULTI:
3271178354Ssam	case SIOCDELMULTI:
3272178354Ssam		ieee80211_ioctl_updatemulti(vap->iv_ic);
3273178354Ssam		break;
3274138568Ssam	case SIOCSIFMEDIA:
3275138568Ssam	case SIOCGIFMEDIA:
3276178354Ssam		ifr = (struct ifreq *)data;
3277178354Ssam		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3278138568Ssam		break;
3279138568Ssam	case SIOCG80211:
3280178354Ssam		error = ieee80211_ioctl_get80211(vap, cmd,
3281138568Ssam				(struct ieee80211req *) data);
3282138568Ssam		break;
3283138568Ssam	case SIOCS80211:
3284164033Srwatson		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3285138568Ssam		if (error == 0)
3286178354Ssam			error = ieee80211_ioctl_set80211(vap, cmd,
3287138568Ssam					(struct ieee80211req *) data);
3288138568Ssam		break;
3289121180Ssam	case SIOCG80211STATS:
3290121180Ssam		ifr = (struct ifreq *)data;
3291178354Ssam		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3292121180Ssam		break;
3293124457Ssam	case SIOCSIFMTU:
3294124457Ssam		ifr = (struct ifreq *)data;
3295124457Ssam		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3296124457Ssam		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3297124457Ssam			error = EINVAL;
3298124457Ssam		else
3299124457Ssam			ifp->if_mtu = ifr->ifr_mtu;
3300124457Ssam		break;
3301127646Ssam	case SIOCSIFADDR:
3302127646Ssam		/*
3303127646Ssam		 * XXX Handle this directly so we can supress if_init calls.
3304127646Ssam		 * XXX This should be done in ether_ioctl but for the moment
3305127646Ssam		 * XXX there are too many other parts of the system that
3306127646Ssam		 * XXX set IFF_UP and so supress if_init being called when
3307127646Ssam		 * XXX it should be.
3308127646Ssam		 */
3309127646Ssam		ifa = (struct ifaddr *) data;
3310127646Ssam		switch (ifa->ifa_addr->sa_family) {
3311127646Ssam#ifdef INET
3312127646Ssam		case AF_INET:
3313127646Ssam			if ((ifp->if_flags & IFF_UP) == 0) {
3314127646Ssam				ifp->if_flags |= IFF_UP;
3315127646Ssam				ifp->if_init(ifp->if_softc);
3316127646Ssam			}
3317127646Ssam			arp_ifinit(ifp, ifa);
3318127646Ssam			break;
3319127646Ssam#endif
3320127646Ssam#ifdef IPX
3321127646Ssam		/*
3322127646Ssam		 * XXX - This code is probably wrong,
3323127646Ssam		 *	 but has been copied many times.
3324127646Ssam		 */
3325127646Ssam		case AF_IPX: {
3326127646Ssam			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3327127646Ssam
3328127646Ssam			if (ipx_nullhost(*ina))
3329147256Sbrooks				ina->x_host = *(union ipx_host *)
3330152315Sru				    IF_LLADDR(ifp);
3331127646Ssam			else
3332127646Ssam				bcopy((caddr_t) ina->x_host.c_host,
3333152315Sru				      (caddr_t) IF_LLADDR(ifp),
3334147256Sbrooks				      ETHER_ADDR_LEN);
3335127646Ssam			/* fall thru... */
3336127646Ssam		}
3337127646Ssam#endif
3338127646Ssam		default:
3339127646Ssam			if ((ifp->if_flags & IFF_UP) == 0) {
3340127646Ssam				ifp->if_flags |= IFF_UP;
3341127646Ssam				ifp->if_init(ifp->if_softc);
3342127646Ssam			}
3343127646Ssam			break;
3344127646Ssam		}
3345127646Ssam		break;
3346178354Ssam	/* Pass NDIS ioctls up to the driver */
3347178354Ssam	case SIOCGDRVSPEC:
3348178354Ssam	case SIOCSDRVSPEC:
3349178354Ssam	case SIOCGPRIVATE_0: {
3350178354Ssam		struct ifnet *parent = vap->iv_ic->ic_ifp;
3351178354Ssam		error = parent->if_ioctl(parent, cmd, data);
3352178354Ssam		break;
3353178354Ssam	}
3354116742Ssam	default:
3355116742Ssam		error = ether_ioctl(ifp, cmd, data);
3356116742Ssam		break;
3357116742Ssam	}
3358116742Ssam	return error;
3359116742Ssam}
3360