xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * IEEE 802.11 ioctl support (FreeBSD-specific)
32  */
33 
34 #include "opt_inet.h"
35 #include "opt_ipx.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/endian.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/priv.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 #endif
55 
56 #ifdef IPX
57 #include <netipx/ipx.h>
58 #include <netipx/ipx_if.h>
59 #endif
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_ioctl.h>
63 #include <net80211/ieee80211_regdomain.h>
64 #include <net80211/ieee80211_input.h>
65 
66 #define	IS_UP_AUTO(_vap) \
67 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
68 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
69 
70 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
71 static struct ieee80211_channel *findchannel(struct ieee80211com *,
72 		int ieee, int mode);
73 static int ieee80211_scanreq(struct ieee80211vap *,
74 		struct ieee80211_scan_req *);
75 
76 static __noinline int
77 ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
78 {
79 	struct ieee80211com *ic = vap->iv_ic;
80 	struct ieee80211_node *ni;
81 	struct ieee80211req_key ik;
82 	struct ieee80211_key *wk;
83 	const struct ieee80211_cipher *cip;
84 	u_int kid;
85 	int error;
86 
87 	if (ireq->i_len != sizeof(ik))
88 		return EINVAL;
89 	error = copyin(ireq->i_data, &ik, sizeof(ik));
90 	if (error)
91 		return error;
92 	kid = ik.ik_keyix;
93 	if (kid == IEEE80211_KEYIX_NONE) {
94 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
95 		if (ni == NULL)
96 			return ENOENT;
97 		wk = &ni->ni_ucastkey;
98 	} else {
99 		if (kid >= IEEE80211_WEP_NKID)
100 			return EINVAL;
101 		wk = &vap->iv_nw_keys[kid];
102 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
103 		ni = NULL;
104 	}
105 	cip = wk->wk_cipher;
106 	ik.ik_type = cip->ic_cipher;
107 	ik.ik_keylen = wk->wk_keylen;
108 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
109 	if (wk->wk_keyix == vap->iv_def_txkey)
110 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
111 	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
112 		/* NB: only root can read key data */
113 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
114 		ik.ik_keytsc = wk->wk_keytsc;
115 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
116 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
117 			memcpy(ik.ik_keydata+wk->wk_keylen,
118 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
119 				IEEE80211_MICBUF_SIZE);
120 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
121 		}
122 	} else {
123 		ik.ik_keyrsc = 0;
124 		ik.ik_keytsc = 0;
125 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
126 	}
127 	if (ni != NULL)
128 		ieee80211_free_node(ni);
129 	return copyout(&ik, ireq->i_data, sizeof(ik));
130 }
131 
132 static __noinline int
133 ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
134 {
135 	struct ieee80211com *ic = vap->iv_ic;
136 
137 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
138 		ireq->i_len = sizeof(ic->ic_chan_active);
139 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
140 }
141 
142 static __noinline int
143 ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
144 {
145 	struct ieee80211com *ic = vap->iv_ic;
146 	int space;
147 
148 	space = __offsetof(struct ieee80211req_chaninfo,
149 			ic_chans[ic->ic_nchans]);
150 	if (space > ireq->i_len)
151 		space = ireq->i_len;
152 	/* XXX assumes compatible layout */
153 	return copyout(&ic->ic_nchans, ireq->i_data, space);
154 }
155 
156 static __noinline int
157 ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
158 	struct ieee80211req *ireq, int req)
159 {
160 	struct ieee80211_node *ni;
161 	struct ieee80211req_wpaie2 wpaie;
162 	int error;
163 
164 	if (ireq->i_len < IEEE80211_ADDR_LEN)
165 		return EINVAL;
166 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
167 	if (error != 0)
168 		return error;
169 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie.wpa_macaddr);
170 	if (ni == NULL)
171 		return ENOENT;
172 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
173 	if (ni->ni_ies.wpa_ie != NULL) {
174 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
175 		if (ielen > sizeof(wpaie.wpa_ie))
176 			ielen = sizeof(wpaie.wpa_ie);
177 		memcpy(wpaie.wpa_ie, ni->ni_ies.wpa_ie, ielen);
178 	}
179 	if (req == IEEE80211_IOC_WPAIE2) {
180 		memset(wpaie.rsn_ie, 0, sizeof(wpaie.rsn_ie));
181 		if (ni->ni_ies.rsn_ie != NULL) {
182 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
183 			if (ielen > sizeof(wpaie.rsn_ie))
184 				ielen = sizeof(wpaie.rsn_ie);
185 			memcpy(wpaie.rsn_ie, ni->ni_ies.rsn_ie, ielen);
186 		}
187 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
188 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
189 	} else {
190 		/* compatibility op, may overwrite wpa ie */
191 		/* XXX check ic_flags? */
192 		if (ni->ni_ies.rsn_ie != NULL) {
193 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
194 			if (ielen > sizeof(wpaie.wpa_ie))
195 				ielen = sizeof(wpaie.wpa_ie);
196 			memcpy(wpaie.wpa_ie, ni->ni_ies.rsn_ie, ielen);
197 		}
198 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
199 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
200 	}
201 	ieee80211_free_node(ni);
202 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
203 }
204 
205 static __noinline int
206 ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
207 {
208 	struct ieee80211_node *ni;
209 	uint8_t macaddr[IEEE80211_ADDR_LEN];
210 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
211 	int error;
212 
213 	if (ireq->i_len < off)
214 		return EINVAL;
215 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
216 	if (error != 0)
217 		return error;
218 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
219 	if (ni == NULL)
220 		return ENOENT;
221 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
222 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
223 	/* NB: copy out only the statistics */
224 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
225 			ireq->i_len - off);
226 	ieee80211_free_node(ni);
227 	return error;
228 }
229 
230 struct scanreq {
231 	struct ieee80211req_scan_result *sr;
232 	size_t space;
233 };
234 
235 static size_t
236 scan_space(const struct ieee80211_scan_entry *se, int *ielen)
237 {
238 	size_t len;
239 
240 	*ielen = se->se_ies.len;
241 	/*
242 	 * NB: ie's can be no more than 255 bytes and the max 802.11
243 	 * packet is <3Kbytes so we are sure this doesn't overflow
244 	 * 16-bits; if this is a concern we can drop the ie's.
245 	 */
246 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
247 	    se->se_meshid[1] + *ielen;
248 	return roundup(len, sizeof(uint32_t));
249 }
250 
251 static void
252 get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
253 {
254 	struct scanreq *req = arg;
255 	int ielen;
256 
257 	req->space += scan_space(se, &ielen);
258 }
259 
260 static __noinline void
261 get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
262 {
263 	struct scanreq *req = arg;
264 	struct ieee80211req_scan_result *sr;
265 	int ielen, len, nr, nxr;
266 	uint8_t *cp;
267 
268 	len = scan_space(se, &ielen);
269 	if (len > req->space)
270 		return;
271 
272 	sr = req->sr;
273 	KASSERT(len <= 65535 && ielen <= 65535,
274 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
275 	sr->isr_len = len;
276 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
277 	sr->isr_ie_len = ielen;
278 	sr->isr_freq = se->se_chan->ic_freq;
279 	sr->isr_flags = se->se_chan->ic_flags;
280 	sr->isr_rssi = se->se_rssi;
281 	sr->isr_noise = se->se_noise;
282 	sr->isr_intval = se->se_intval;
283 	sr->isr_capinfo = se->se_capinfo;
284 	sr->isr_erp = se->se_erp;
285 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
286 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
287 	memcpy(sr->isr_rates, se->se_rates+2, nr);
288 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
289 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
290 	sr->isr_nrates = nr + nxr;
291 
292 	/* copy SSID */
293 	sr->isr_ssid_len = se->se_ssid[1];
294 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
295 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
296 
297 	/* copy mesh id */
298 	cp += sr->isr_ssid_len;
299 	sr->isr_meshid_len = se->se_meshid[1];
300 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
301 	cp += sr->isr_meshid_len;
302 
303 	if (ielen)
304 		memcpy(cp, se->se_ies.data, ielen);
305 
306 	req->space -= len;
307 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
308 }
309 
310 static __noinline int
311 ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
312 	struct ieee80211req *ireq)
313 {
314 	struct scanreq req;
315 	int error;
316 
317 	if (ireq->i_len < sizeof(struct scanreq))
318 		return EFAULT;
319 
320 	error = 0;
321 	req.space = 0;
322 	ieee80211_scan_iterate(vap, get_scan_space, &req);
323 	if (req.space > ireq->i_len)
324 		req.space = ireq->i_len;
325 	if (req.space > 0) {
326 		size_t space;
327 		void *p;
328 
329 		space = req.space;
330 		/* XXX M_WAITOK after driver lock released */
331 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
332 		if (p == NULL)
333 			return ENOMEM;
334 		req.sr = p;
335 		ieee80211_scan_iterate(vap, get_scan_result, &req);
336 		ireq->i_len = space - req.space;
337 		error = copyout(p, ireq->i_data, ireq->i_len);
338 		free(p, M_TEMP);
339 	} else
340 		ireq->i_len = 0;
341 
342 	return error;
343 }
344 
345 struct stainforeq {
346 	struct ieee80211vap *vap;
347 	struct ieee80211req_sta_info *si;
348 	size_t	space;
349 };
350 
351 static size_t
352 sta_space(const struct ieee80211_node *ni, size_t *ielen)
353 {
354 	*ielen = ni->ni_ies.len;
355 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
356 		      sizeof(uint32_t));
357 }
358 
359 static void
360 get_sta_space(void *arg, struct ieee80211_node *ni)
361 {
362 	struct stainforeq *req = arg;
363 	size_t ielen;
364 
365 	if (req->vap != ni->ni_vap)
366 		return;
367 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
368 	    ni->ni_associd == 0)	/* only associated stations */
369 		return;
370 	req->space += sta_space(ni, &ielen);
371 }
372 
373 static __noinline void
374 get_sta_info(void *arg, struct ieee80211_node *ni)
375 {
376 	struct stainforeq *req = arg;
377 	struct ieee80211vap *vap = ni->ni_vap;
378 	struct ieee80211req_sta_info *si;
379 	size_t ielen, len;
380 	uint8_t *cp;
381 
382 	if (req->vap != ni->ni_vap)
383 		return;
384 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
385 	    ni->ni_associd == 0)	/* only associated stations */
386 		return;
387 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
388 		return;
389 	len = sta_space(ni, &ielen);
390 	if (len > req->space)
391 		return;
392 	si = req->si;
393 	si->isi_len = len;
394 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
395 	si->isi_ie_len = ielen;
396 	si->isi_freq = ni->ni_chan->ic_freq;
397 	si->isi_flags = ni->ni_chan->ic_flags;
398 	si->isi_state = ni->ni_flags;
399 	si->isi_authmode = ni->ni_authmode;
400 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
401 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
402 	si->isi_capinfo = ni->ni_capinfo;
403 	si->isi_erp = ni->ni_erp;
404 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
405 	si->isi_nrates = ni->ni_rates.rs_nrates;
406 	if (si->isi_nrates > 15)
407 		si->isi_nrates = 15;
408 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
409 	si->isi_txrate = ni->ni_txrate;
410 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
411 		const struct ieee80211_mcs_rates *mcs =
412 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
413 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
414 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
415 				si->isi_txmbps = mcs->ht40_rate_800ns;
416 			else
417 				si->isi_txmbps = mcs->ht40_rate_400ns;
418 		} else {
419 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
420 				si->isi_txmbps = mcs->ht20_rate_800ns;
421 			else
422 				si->isi_txmbps = mcs->ht20_rate_400ns;
423 		}
424 	} else
425 		si->isi_txmbps = si->isi_txrate;
426 	si->isi_associd = ni->ni_associd;
427 	si->isi_txpower = ni->ni_txpower;
428 	si->isi_vlan = ni->ni_vlan;
429 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
430 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
431 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
432 	} else {
433 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
434 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
435 	}
436 	/* NB: leave all cases in case we relax ni_associd == 0 check */
437 	if (ieee80211_node_is_authorized(ni))
438 		si->isi_inact = vap->iv_inact_run;
439 	else if (ni->ni_associd != 0 ||
440 	    (vap->iv_opmode == IEEE80211_M_WDS &&
441 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
442 		si->isi_inact = vap->iv_inact_auth;
443 	else
444 		si->isi_inact = vap->iv_inact_init;
445 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
446 	si->isi_localid = ni->ni_mllid;
447 	si->isi_peerid = ni->ni_mlpid;
448 	si->isi_peerstate = ni->ni_mlstate;
449 
450 	if (ielen) {
451 		cp = ((uint8_t *)si) + si->isi_ie_off;
452 		memcpy(cp, ni->ni_ies.data, ielen);
453 	}
454 
455 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
456 	req->space -= len;
457 }
458 
459 static __noinline int
460 getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
461 	struct ieee80211_node *ni, int off)
462 {
463 	struct ieee80211com *ic = vap->iv_ic;
464 	struct stainforeq req;
465 	size_t space;
466 	void *p;
467 	int error;
468 
469 	error = 0;
470 	req.space = 0;
471 	req.vap = vap;
472 	if (ni == NULL)
473 		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
474 	else
475 		get_sta_space(&req, ni);
476 	if (req.space > ireq->i_len)
477 		req.space = ireq->i_len;
478 	if (req.space > 0) {
479 		space = req.space;
480 		/* XXX M_WAITOK after driver lock released */
481 		p = malloc(space, M_TEMP, M_NOWAIT | M_ZERO);
482 		if (p == NULL) {
483 			error = ENOMEM;
484 			goto bad;
485 		}
486 		req.si = p;
487 		if (ni == NULL)
488 			ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
489 		else
490 			get_sta_info(&req, ni);
491 		ireq->i_len = space - req.space;
492 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
493 		free(p, M_TEMP);
494 	} else
495 		ireq->i_len = 0;
496 bad:
497 	if (ni != NULL)
498 		ieee80211_free_node(ni);
499 	return error;
500 }
501 
502 static __noinline int
503 ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
504 {
505 	uint8_t macaddr[IEEE80211_ADDR_LEN];
506 	const int off = __offsetof(struct ieee80211req_sta_req, info);
507 	struct ieee80211_node *ni;
508 	int error;
509 
510 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
511 		return EFAULT;
512 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
513 	if (error != 0)
514 		return error;
515 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
516 		ni = NULL;
517 	} else {
518 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
519 		if (ni == NULL)
520 			return ENOENT;
521 	}
522 	return getstainfo_common(vap, ireq, ni, off);
523 }
524 
525 static __noinline int
526 ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
527 {
528 	struct ieee80211_node *ni;
529 	struct ieee80211req_sta_txpow txpow;
530 	int error;
531 
532 	if (ireq->i_len != sizeof(txpow))
533 		return EINVAL;
534 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
535 	if (error != 0)
536 		return error;
537 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
538 	if (ni == NULL)
539 		return ENOENT;
540 	txpow.it_txpow = ni->ni_txpower;
541 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
542 	ieee80211_free_node(ni);
543 	return error;
544 }
545 
546 static __noinline int
547 ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
548 {
549 	struct ieee80211com *ic = vap->iv_ic;
550 	struct ieee80211_wme_state *wme = &ic->ic_wme;
551 	struct wmeParams *wmep;
552 	int ac;
553 
554 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
555 		return EINVAL;
556 
557 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
558 	if (ac >= WME_NUM_AC)
559 		ac = WME_AC_BE;
560 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
561 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
562 	else
563 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
564 	switch (ireq->i_type) {
565 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
566 		ireq->i_val = wmep->wmep_logcwmin;
567 		break;
568 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
569 		ireq->i_val = wmep->wmep_logcwmax;
570 		break;
571 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
572 		ireq->i_val = wmep->wmep_aifsn;
573 		break;
574 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
575 		ireq->i_val = wmep->wmep_txopLimit;
576 		break;
577 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
578 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
579 		ireq->i_val = wmep->wmep_acm;
580 		break;
581 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
582 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
583 		ireq->i_val = !wmep->wmep_noackPolicy;
584 		break;
585 	}
586 	return 0;
587 }
588 
589 static __noinline int
590 ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
591 {
592 	const struct ieee80211_aclator *acl = vap->iv_acl;
593 
594 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
595 }
596 
597 static __noinline int
598 ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
599 {
600 	struct ieee80211com *ic = vap->iv_ic;
601 	struct ieee80211_channel *c;
602 
603 	if (ireq->i_len != sizeof(struct ieee80211_channel))
604 		return EINVAL;
605 	/*
606 	 * vap's may have different operating channels when HT is
607 	 * in use.  When in RUN state report the vap-specific channel.
608 	 * Otherwise return curchan.
609 	 */
610 	if (vap->iv_state == IEEE80211_S_RUN)
611 		c = vap->iv_bss->ni_chan;
612 	else
613 		c = ic->ic_curchan;
614 	return copyout(c, ireq->i_data, sizeof(*c));
615 }
616 
617 static int
618 getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
619 {
620 	if (aie == NULL)
621 		return EINVAL;
622 	/* NB: truncate, caller can check length */
623 	if (ireq->i_len > aie->ie_len)
624 		ireq->i_len = aie->ie_len;
625 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
626 }
627 
628 static int
629 ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
630 {
631 	uint8_t fc0;
632 
633 	fc0 = ireq->i_val & 0xff;
634 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
635 		return EINVAL;
636 	/* NB: could check iv_opmode and reject but hardly worth the effort */
637 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
638 	case IEEE80211_FC0_SUBTYPE_BEACON:
639 		return getappie(vap->iv_appie_beacon, ireq);
640 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
641 		return getappie(vap->iv_appie_proberesp, ireq);
642 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
643 		return getappie(vap->iv_appie_assocresp, ireq);
644 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
645 		return getappie(vap->iv_appie_probereq, ireq);
646 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
647 		return getappie(vap->iv_appie_assocreq, ireq);
648 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
649 		return getappie(vap->iv_appie_wpa, ireq);
650 	}
651 	return EINVAL;
652 }
653 
654 static __noinline int
655 ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
656 	const struct ieee80211req *ireq)
657 {
658 	struct ieee80211com *ic = vap->iv_ic;
659 
660 	if (ireq->i_len != sizeof(ic->ic_regdomain))
661 		return EINVAL;
662 	return copyout(&ic->ic_regdomain, ireq->i_data,
663 	    sizeof(ic->ic_regdomain));
664 }
665 
666 static __noinline int
667 ieee80211_ioctl_getroam(struct ieee80211vap *vap,
668 	const struct ieee80211req *ireq)
669 {
670 	size_t len = ireq->i_len;
671 	/* NB: accept short requests for backwards compat */
672 	if (len > sizeof(vap->iv_roamparms))
673 		len = sizeof(vap->iv_roamparms);
674 	return copyout(vap->iv_roamparms, ireq->i_data, len);
675 }
676 
677 static __noinline int
678 ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
679 	const struct ieee80211req *ireq)
680 {
681 	size_t len = ireq->i_len;
682 	/* NB: accept short requests for backwards compat */
683 	if (len > sizeof(vap->iv_txparms))
684 		len = sizeof(vap->iv_txparms);
685 	return copyout(vap->iv_txparms, ireq->i_data, len);
686 }
687 
688 static __noinline int
689 ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
690 	const struct ieee80211req *ireq)
691 {
692 	struct ieee80211_devcaps_req *dc;
693 	struct ieee80211req_chaninfo *ci;
694 	int maxchans, error;
695 
696 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
697 	    sizeof(struct ieee80211_channel));
698 	/* NB: require 1 so we know ic_nchans is accessible */
699 	if (maxchans < 1)
700 		return EINVAL;
701 	/* constrain max request size, 2K channels is ~24Kbytes */
702 	if (maxchans > 2048)
703 		maxchans = 2048;
704 	dc = (struct ieee80211_devcaps_req *)
705 	    malloc(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP, M_NOWAIT | M_ZERO);
706 	if (dc == NULL)
707 		return ENOMEM;
708 	dc->dc_drivercaps = ic->ic_caps;
709 	dc->dc_cryptocaps = ic->ic_cryptocaps;
710 	dc->dc_htcaps = ic->ic_htcaps;
711 	ci = &dc->dc_chaninfo;
712 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
713 	KASSERT(ci->ic_nchans <= maxchans,
714 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
715 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
716 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
717 	free(dc, M_TEMP);
718 	return error;
719 }
720 
721 static __noinline int
722 ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
723 {
724 	struct ieee80211_node *ni;
725 	struct ieee80211req_sta_vlan vlan;
726 	int error;
727 
728 	if (ireq->i_len != sizeof(vlan))
729 		return EINVAL;
730 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
731 	if (error != 0)
732 		return error;
733 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
734 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
735 		    vlan.sv_macaddr);
736 		if (ni == NULL)
737 			return ENOENT;
738 	} else
739 		ni = ieee80211_ref_node(vap->iv_bss);
740 	vlan.sv_vlan = ni->ni_vlan;
741 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
742 	ieee80211_free_node(ni);
743 	return error;
744 }
745 
746 /*
747  * Dummy ioctl get handler so the linker set is defined.
748  */
749 static int
750 dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
751 {
752 	return ENOSYS;
753 }
754 IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
755 
756 static int
757 ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
758 {
759 	ieee80211_ioctl_getfunc * const *get;
760 	int error;
761 
762 	SET_FOREACH(get, ieee80211_ioctl_getset) {
763 		error = (*get)(vap, ireq);
764 		if (error != ENOSYS)
765 			return error;
766 	}
767 	return EINVAL;
768 }
769 
770 /*
771  * When building the kernel with -O2 on the i386 architecture, gcc
772  * seems to want to inline this function into ieee80211_ioctl()
773  * (which is the only routine that calls it). When this happens,
774  * ieee80211_ioctl() ends up consuming an additional 2K of stack
775  * space. (Exactly why it needs so much is unclear.) The problem
776  * is that it's possible for ieee80211_ioctl() to invoke other
777  * routines (including driver init functions) which could then find
778  * themselves perilously close to exhausting the stack.
779  *
780  * To avoid this, we deliberately prevent gcc from inlining this
781  * routine. Another way to avoid this is to use less agressive
782  * optimization when compiling this file (i.e. -O instead of -O2)
783  * but special-casing the compilation of this one module in the
784  * build system would be awkward.
785  */
786 static __noinline int
787 ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
788     struct ieee80211req *ireq)
789 {
790 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
791 	struct ieee80211com *ic = vap->iv_ic;
792 	u_int kid, len;
793 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
794 	char tmpssid[IEEE80211_NWID_LEN];
795 	int error = 0;
796 
797 	switch (ireq->i_type) {
798 	case IEEE80211_IOC_SSID:
799 		switch (vap->iv_state) {
800 		case IEEE80211_S_INIT:
801 		case IEEE80211_S_SCAN:
802 			ireq->i_len = vap->iv_des_ssid[0].len;
803 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
804 			break;
805 		default:
806 			ireq->i_len = vap->iv_bss->ni_esslen;
807 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
808 			break;
809 		}
810 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
811 		break;
812 	case IEEE80211_IOC_NUMSSIDS:
813 		ireq->i_val = 1;
814 		break;
815 	case IEEE80211_IOC_WEP:
816 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
817 			ireq->i_val = IEEE80211_WEP_OFF;
818 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
819 			ireq->i_val = IEEE80211_WEP_ON;
820 		else
821 			ireq->i_val = IEEE80211_WEP_MIXED;
822 		break;
823 	case IEEE80211_IOC_WEPKEY:
824 		kid = (u_int) ireq->i_val;
825 		if (kid >= IEEE80211_WEP_NKID)
826 			return EINVAL;
827 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
828 		/* NB: only root can read WEP keys */
829 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
830 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
831 		} else {
832 			bzero(tmpkey, len);
833 		}
834 		ireq->i_len = len;
835 		error = copyout(tmpkey, ireq->i_data, len);
836 		break;
837 	case IEEE80211_IOC_NUMWEPKEYS:
838 		ireq->i_val = IEEE80211_WEP_NKID;
839 		break;
840 	case IEEE80211_IOC_WEPTXKEY:
841 		ireq->i_val = vap->iv_def_txkey;
842 		break;
843 	case IEEE80211_IOC_AUTHMODE:
844 		if (vap->iv_flags & IEEE80211_F_WPA)
845 			ireq->i_val = IEEE80211_AUTH_WPA;
846 		else
847 			ireq->i_val = vap->iv_bss->ni_authmode;
848 		break;
849 	case IEEE80211_IOC_CHANNEL:
850 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
851 		break;
852 	case IEEE80211_IOC_POWERSAVE:
853 		if (vap->iv_flags & IEEE80211_F_PMGTON)
854 			ireq->i_val = IEEE80211_POWERSAVE_ON;
855 		else
856 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
857 		break;
858 	case IEEE80211_IOC_POWERSAVESLEEP:
859 		ireq->i_val = ic->ic_lintval;
860 		break;
861 	case IEEE80211_IOC_RTSTHRESHOLD:
862 		ireq->i_val = vap->iv_rtsthreshold;
863 		break;
864 	case IEEE80211_IOC_PROTMODE:
865 		ireq->i_val = ic->ic_protmode;
866 		break;
867 	case IEEE80211_IOC_TXPOWER:
868 		/*
869 		 * Tx power limit is the min of max regulatory
870 		 * power, any user-set limit, and the max the
871 		 * radio can do.
872 		 */
873 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
874 		if (ireq->i_val > ic->ic_txpowlimit)
875 			ireq->i_val = ic->ic_txpowlimit;
876 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
877 			ireq->i_val = ic->ic_curchan->ic_maxpower;
878 		break;
879 	case IEEE80211_IOC_WPA:
880 		switch (vap->iv_flags & IEEE80211_F_WPA) {
881 		case IEEE80211_F_WPA1:
882 			ireq->i_val = 1;
883 			break;
884 		case IEEE80211_F_WPA2:
885 			ireq->i_val = 2;
886 			break;
887 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
888 			ireq->i_val = 3;
889 			break;
890 		default:
891 			ireq->i_val = 0;
892 			break;
893 		}
894 		break;
895 	case IEEE80211_IOC_CHANLIST:
896 		error = ieee80211_ioctl_getchanlist(vap, ireq);
897 		break;
898 	case IEEE80211_IOC_ROAMING:
899 		ireq->i_val = vap->iv_roaming;
900 		break;
901 	case IEEE80211_IOC_PRIVACY:
902 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
903 		break;
904 	case IEEE80211_IOC_DROPUNENCRYPTED:
905 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
906 		break;
907 	case IEEE80211_IOC_COUNTERMEASURES:
908 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
909 		break;
910 	case IEEE80211_IOC_WME:
911 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
912 		break;
913 	case IEEE80211_IOC_HIDESSID:
914 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
915 		break;
916 	case IEEE80211_IOC_APBRIDGE:
917 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
918 		break;
919 	case IEEE80211_IOC_WPAKEY:
920 		error = ieee80211_ioctl_getkey(vap, ireq);
921 		break;
922 	case IEEE80211_IOC_CHANINFO:
923 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
924 		break;
925 	case IEEE80211_IOC_BSSID:
926 		if (ireq->i_len != IEEE80211_ADDR_LEN)
927 			return EINVAL;
928 		if (vap->iv_state == IEEE80211_S_RUN) {
929 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
930 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
931 			    ireq->i_data, ireq->i_len);
932 		} else
933 			error = copyout(vap->iv_des_bssid, ireq->i_data,
934 			    ireq->i_len);
935 		break;
936 	case IEEE80211_IOC_WPAIE:
937 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
938 		break;
939 	case IEEE80211_IOC_WPAIE2:
940 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
941 		break;
942 	case IEEE80211_IOC_SCAN_RESULTS:
943 		error = ieee80211_ioctl_getscanresults(vap, ireq);
944 		break;
945 	case IEEE80211_IOC_STA_STATS:
946 		error = ieee80211_ioctl_getstastats(vap, ireq);
947 		break;
948 	case IEEE80211_IOC_TXPOWMAX:
949 		ireq->i_val = vap->iv_bss->ni_txpower;
950 		break;
951 	case IEEE80211_IOC_STA_TXPOW:
952 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
953 		break;
954 	case IEEE80211_IOC_STA_INFO:
955 		error = ieee80211_ioctl_getstainfo(vap, ireq);
956 		break;
957 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
958 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
959 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
960 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
961 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
962 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
963 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
964 		break;
965 	case IEEE80211_IOC_DTIM_PERIOD:
966 		ireq->i_val = vap->iv_dtim_period;
967 		break;
968 	case IEEE80211_IOC_BEACON_INTERVAL:
969 		/* NB: get from ic_bss for station mode */
970 		ireq->i_val = vap->iv_bss->ni_intval;
971 		break;
972 	case IEEE80211_IOC_PUREG:
973 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
974 		break;
975 	case IEEE80211_IOC_BGSCAN:
976 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
977 		break;
978 	case IEEE80211_IOC_BGSCAN_IDLE:
979 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
980 		break;
981 	case IEEE80211_IOC_BGSCAN_INTERVAL:
982 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
983 		break;
984 	case IEEE80211_IOC_SCANVALID:
985 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
986 		break;
987 	case IEEE80211_IOC_FRAGTHRESHOLD:
988 		ireq->i_val = vap->iv_fragthreshold;
989 		break;
990 	case IEEE80211_IOC_MACCMD:
991 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
992 		break;
993 	case IEEE80211_IOC_BURST:
994 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
995 		break;
996 	case IEEE80211_IOC_BMISSTHRESHOLD:
997 		ireq->i_val = vap->iv_bmissthreshold;
998 		break;
999 	case IEEE80211_IOC_CURCHAN:
1000 		error = ieee80211_ioctl_getcurchan(vap, ireq);
1001 		break;
1002 	case IEEE80211_IOC_SHORTGI:
1003 		ireq->i_val = 0;
1004 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1005 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
1006 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1007 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
1008 		break;
1009 	case IEEE80211_IOC_AMPDU:
1010 		ireq->i_val = 0;
1011 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
1012 			ireq->i_val |= 1;
1013 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
1014 			ireq->i_val |= 2;
1015 		break;
1016 	case IEEE80211_IOC_AMPDU_LIMIT:
1017 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1018 			ireq->i_val = vap->iv_ampdu_rxmax;
1019 		else if (vap->iv_state == IEEE80211_S_RUN)
1020 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1021 			    IEEE80211_HTCAP_MAXRXAMPDU);
1022 		else
1023 			ireq->i_val = vap->iv_ampdu_limit;
1024 		break;
1025 	case IEEE80211_IOC_AMPDU_DENSITY:
1026 		if (vap->iv_opmode == IEEE80211_M_STA &&
1027 		    vap->iv_state == IEEE80211_S_RUN)
1028 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1029 			    IEEE80211_HTCAP_MPDUDENSITY);
1030 		else
1031 			ireq->i_val = vap->iv_ampdu_density;
1032 		break;
1033 	case IEEE80211_IOC_AMSDU:
1034 		ireq->i_val = 0;
1035 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
1036 			ireq->i_val |= 1;
1037 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
1038 			ireq->i_val |= 2;
1039 		break;
1040 	case IEEE80211_IOC_AMSDU_LIMIT:
1041 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1042 		break;
1043 	case IEEE80211_IOC_PUREN:
1044 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
1045 		break;
1046 	case IEEE80211_IOC_DOTH:
1047 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1048 		break;
1049 	case IEEE80211_IOC_REGDOMAIN:
1050 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1051 		break;
1052 	case IEEE80211_IOC_ROAM:
1053 		error = ieee80211_ioctl_getroam(vap, ireq);
1054 		break;
1055 	case IEEE80211_IOC_TXPARAMS:
1056 		error = ieee80211_ioctl_gettxparams(vap, ireq);
1057 		break;
1058 	case IEEE80211_IOC_HTCOMPAT:
1059 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1060 		break;
1061 	case IEEE80211_IOC_DWDS:
1062 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1063 		break;
1064 	case IEEE80211_IOC_INACTIVITY:
1065 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1066 		break;
1067 	case IEEE80211_IOC_APPIE:
1068 		error = ieee80211_ioctl_getappie(vap, ireq);
1069 		break;
1070 	case IEEE80211_IOC_WPS:
1071 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1072 		break;
1073 	case IEEE80211_IOC_TSN:
1074 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1075 		break;
1076 	case IEEE80211_IOC_DFS:
1077 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1078 		break;
1079 	case IEEE80211_IOC_DOTD:
1080 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1081 		break;
1082 	case IEEE80211_IOC_DEVCAPS:
1083 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1084 		break;
1085 	case IEEE80211_IOC_HTPROTMODE:
1086 		ireq->i_val = ic->ic_htprotmode;
1087 		break;
1088 	case IEEE80211_IOC_HTCONF:
1089 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
1090 			ireq->i_val = 1;
1091 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
1092 				ireq->i_val |= 2;
1093 		} else
1094 			ireq->i_val = 0;
1095 		break;
1096 	case IEEE80211_IOC_STA_VLAN:
1097 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1098 		break;
1099 	case IEEE80211_IOC_SMPS:
1100 		if (vap->iv_opmode == IEEE80211_M_STA &&
1101 		    vap->iv_state == IEEE80211_S_RUN) {
1102 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1103 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1104 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1105 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1106 			else
1107 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1108 		} else
1109 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1110 		break;
1111 	case IEEE80211_IOC_RIFS:
1112 		if (vap->iv_opmode == IEEE80211_M_STA &&
1113 		    vap->iv_state == IEEE80211_S_RUN)
1114 			ireq->i_val =
1115 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1116 		else
1117 			ireq->i_val =
1118 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
1119 		break;
1120 	default:
1121 		error = ieee80211_ioctl_getdefault(vap, ireq);
1122 		break;
1123 	}
1124 	return error;
1125 #undef MS
1126 }
1127 
1128 static __noinline int
1129 ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1130 {
1131 	struct ieee80211req_key ik;
1132 	struct ieee80211_node *ni;
1133 	struct ieee80211_key *wk;
1134 	uint16_t kid;
1135 	int error, i;
1136 
1137 	if (ireq->i_len != sizeof(ik))
1138 		return EINVAL;
1139 	error = copyin(ireq->i_data, &ik, sizeof(ik));
1140 	if (error)
1141 		return error;
1142 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1143 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1144 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1145 		return E2BIG;
1146 	kid = ik.ik_keyix;
1147 	if (kid == IEEE80211_KEYIX_NONE) {
1148 		/* XXX unicast keys currently must be tx/rx */
1149 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1150 			return EINVAL;
1151 		if (vap->iv_opmode == IEEE80211_M_STA) {
1152 			ni = ieee80211_ref_node(vap->iv_bss);
1153 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1154 				ieee80211_free_node(ni);
1155 				return EADDRNOTAVAIL;
1156 			}
1157 		} else {
1158 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1159 				ik.ik_macaddr);
1160 			if (ni == NULL)
1161 				return ENOENT;
1162 		}
1163 		wk = &ni->ni_ucastkey;
1164 	} else {
1165 		if (kid >= IEEE80211_WEP_NKID)
1166 			return EINVAL;
1167 		wk = &vap->iv_nw_keys[kid];
1168 		/*
1169 		 * Global slots start off w/o any assigned key index.
1170 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1171 		 */
1172 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1173 			wk->wk_keyix = kid;
1174 		ni = NULL;
1175 	}
1176 	error = 0;
1177 	ieee80211_key_update_begin(vap);
1178 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1179 		wk->wk_keylen = ik.ik_keylen;
1180 		/* NB: MIC presence is implied by cipher type */
1181 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1182 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1183 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1184 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1185 		wk->wk_keytsc = 0;			/* new key, reset */
1186 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1187 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1188 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1189 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1190 		if (!ieee80211_crypto_setkey(vap, wk))
1191 			error = EIO;
1192 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1193 			vap->iv_def_txkey = kid;
1194 	} else
1195 		error = ENXIO;
1196 	ieee80211_key_update_end(vap);
1197 	if (ni != NULL)
1198 		ieee80211_free_node(ni);
1199 	return error;
1200 }
1201 
1202 static __noinline int
1203 ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1204 {
1205 	struct ieee80211req_del_key dk;
1206 	int kid, error;
1207 
1208 	if (ireq->i_len != sizeof(dk))
1209 		return EINVAL;
1210 	error = copyin(ireq->i_data, &dk, sizeof(dk));
1211 	if (error)
1212 		return error;
1213 	kid = dk.idk_keyix;
1214 	/* XXX uint8_t -> uint16_t */
1215 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1216 		struct ieee80211_node *ni;
1217 
1218 		if (vap->iv_opmode == IEEE80211_M_STA) {
1219 			ni = ieee80211_ref_node(vap->iv_bss);
1220 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1221 				ieee80211_free_node(ni);
1222 				return EADDRNOTAVAIL;
1223 			}
1224 		} else {
1225 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1226 				dk.idk_macaddr);
1227 			if (ni == NULL)
1228 				return ENOENT;
1229 		}
1230 		/* XXX error return */
1231 		ieee80211_node_delucastkey(ni);
1232 		ieee80211_free_node(ni);
1233 	} else {
1234 		if (kid >= IEEE80211_WEP_NKID)
1235 			return EINVAL;
1236 		/* XXX error return */
1237 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1238 	}
1239 	return 0;
1240 }
1241 
1242 struct mlmeop {
1243 	struct ieee80211vap *vap;
1244 	int	op;
1245 	int	reason;
1246 };
1247 
1248 static void
1249 mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1250 	int op, int reason)
1251 {
1252 #ifdef IEEE80211_DEBUG
1253 	static const struct {
1254 		int mask;
1255 		const char *opstr;
1256 	} ops[] = {
1257 		{ 0, "op#0" },
1258 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1259 		  IEEE80211_MSG_ASSOC, "assoc" },
1260 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1261 		  IEEE80211_MSG_ASSOC, "disassoc" },
1262 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1263 		  IEEE80211_MSG_AUTH, "deauth" },
1264 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1265 		  IEEE80211_MSG_AUTH, "authorize" },
1266 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1267 		  IEEE80211_MSG_AUTH, "unauthorize" },
1268 	};
1269 
1270 	if (op == IEEE80211_MLME_AUTH) {
1271 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1272 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1273 		    "station authenticate %s via MLME (reason %d)",
1274 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1275 		    reason);
1276 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1277 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1278 		    "unknown MLME request %d (reason %d)", op, reason);
1279 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1280 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1281 		    "station %s via MLME", ops[op].opstr);
1282 	} else {
1283 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1284 		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1285 	}
1286 #endif /* IEEE80211_DEBUG */
1287 }
1288 
1289 static void
1290 domlme(void *arg, struct ieee80211_node *ni)
1291 {
1292 	struct mlmeop *mop = arg;
1293 	struct ieee80211vap *vap = ni->ni_vap;
1294 
1295 	if (vap != mop->vap)
1296 		return;
1297 	/*
1298 	 * NB: if ni_associd is zero then the node is already cleaned
1299 	 * up and we don't need to do this (we're safely holding a
1300 	 * reference but should otherwise not modify it's state).
1301 	 */
1302 	if (ni->ni_associd == 0)
1303 		return;
1304 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1305 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1306 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1307 		    mop->reason);
1308 	} else {
1309 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1310 		    mop->reason);
1311 	}
1312 	ieee80211_node_leave(ni);
1313 }
1314 
1315 static int
1316 setmlme_dropsta(struct ieee80211vap *vap,
1317 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1318 {
1319 	struct ieee80211com *ic = vap->iv_ic;
1320 	struct ieee80211_node_table *nt = &ic->ic_sta;
1321 	struct ieee80211_node *ni;
1322 	int error = 0;
1323 
1324 	/* NB: the broadcast address means do 'em all */
1325 	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1326 		IEEE80211_NODE_LOCK(nt);
1327 		ni = ieee80211_find_node_locked(nt, mac);
1328 		if (ni != NULL) {
1329 			domlme(mlmeop, ni);
1330 			ieee80211_free_node(ni);
1331 		} else
1332 			error = ENOENT;
1333 		IEEE80211_NODE_UNLOCK(nt);
1334 	} else {
1335 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1336 	}
1337 	return error;
1338 }
1339 
1340 static __noinline int
1341 setmlme_common(struct ieee80211vap *vap, int op,
1342 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1343 {
1344 	struct ieee80211com *ic = vap->iv_ic;
1345 	struct ieee80211_node_table *nt = &ic->ic_sta;
1346 	struct ieee80211_node *ni;
1347 	struct mlmeop mlmeop;
1348 	int error;
1349 
1350 	error = 0;
1351 	switch (op) {
1352 	case IEEE80211_MLME_DISASSOC:
1353 	case IEEE80211_MLME_DEAUTH:
1354 		switch (vap->iv_opmode) {
1355 		case IEEE80211_M_STA:
1356 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1357 			/* XXX not quite right */
1358 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1359 			break;
1360 		case IEEE80211_M_HOSTAP:
1361 			mlmeop.vap = vap;
1362 			mlmeop.op = op;
1363 			mlmeop.reason = reason;
1364 			error = setmlme_dropsta(vap, mac, &mlmeop);
1365 			break;
1366 		case IEEE80211_M_WDS:
1367 			/* XXX user app should send raw frame? */
1368 			if (op != IEEE80211_MLME_DEAUTH) {
1369 				error = EINVAL;
1370 				break;
1371 			}
1372 #if 0
1373 			/* XXX accept any address, simplifies user code */
1374 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1375 				error = EINVAL;
1376 				break;
1377 			}
1378 #endif
1379 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1380 			ni = ieee80211_ref_node(vap->iv_bss);
1381 			IEEE80211_SEND_MGMT(ni,
1382 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1383 			ieee80211_free_node(ni);
1384 			break;
1385 		default:
1386 			error = EINVAL;
1387 			break;
1388 		}
1389 		break;
1390 	case IEEE80211_MLME_AUTHORIZE:
1391 	case IEEE80211_MLME_UNAUTHORIZE:
1392 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1393 		    vap->iv_opmode != IEEE80211_M_WDS) {
1394 			error = EINVAL;
1395 			break;
1396 		}
1397 		IEEE80211_NODE_LOCK(nt);
1398 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1399 		if (ni != NULL) {
1400 			mlmedebug(vap, mac, op, reason);
1401 			if (op == IEEE80211_MLME_AUTHORIZE)
1402 				ieee80211_node_authorize(ni);
1403 			else
1404 				ieee80211_node_unauthorize(ni);
1405 			ieee80211_free_node(ni);
1406 		} else
1407 			error = ENOENT;
1408 		IEEE80211_NODE_UNLOCK(nt);
1409 		break;
1410 	case IEEE80211_MLME_AUTH:
1411 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1412 			error = EINVAL;
1413 			break;
1414 		}
1415 		IEEE80211_NODE_LOCK(nt);
1416 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1417 		if (ni != NULL) {
1418 			mlmedebug(vap, mac, op, reason);
1419 			if (reason == IEEE80211_STATUS_SUCCESS) {
1420 				IEEE80211_SEND_MGMT(ni,
1421 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1422 				/*
1423 				 * For shared key auth, just continue the
1424 				 * exchange.  Otherwise when 802.1x is not in
1425 				 * use mark the port authorized at this point
1426 				 * so traffic can flow.
1427 				 */
1428 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1429 				    ni->ni_challenge == NULL)
1430 				      ieee80211_node_authorize(ni);
1431 			} else {
1432 				vap->iv_stats.is_rx_acl++;
1433 				ieee80211_send_error(ni, ni->ni_macaddr,
1434 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1435 				ieee80211_node_leave(ni);
1436 			}
1437 			ieee80211_free_node(ni);
1438 		} else
1439 			error = ENOENT;
1440 		IEEE80211_NODE_UNLOCK(nt);
1441 		break;
1442 	default:
1443 		error = EINVAL;
1444 		break;
1445 	}
1446 	return error;
1447 }
1448 
1449 struct scanlookup {
1450 	const uint8_t *mac;
1451 	int esslen;
1452 	const uint8_t *essid;
1453 	const struct ieee80211_scan_entry *se;
1454 };
1455 
1456 /*
1457  * Match mac address and any ssid.
1458  */
1459 static void
1460 mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1461 {
1462 	struct scanlookup *look = arg;
1463 
1464 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1465 		return;
1466 	if (look->esslen != 0) {
1467 		if (se->se_ssid[1] != look->esslen)
1468 			return;
1469 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1470 			return;
1471 	}
1472 	look->se = se;
1473 }
1474 
1475 static __noinline int
1476 setmlme_assoc_sta(struct ieee80211vap *vap,
1477 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1478 	const uint8_t ssid[IEEE80211_NWID_LEN])
1479 {
1480 	struct scanlookup lookup;
1481 
1482 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1483 	    ("expected opmode STA not %s",
1484 	    ieee80211_opmode_name[vap->iv_opmode]));
1485 
1486 	/* NB: this is racey if roaming is !manual */
1487 	lookup.se = NULL;
1488 	lookup.mac = mac;
1489 	lookup.esslen = ssid_len;
1490 	lookup.essid = ssid;
1491 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1492 	if (lookup.se == NULL)
1493 		return ENOENT;
1494 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1495 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1496 		return EIO;		/* XXX unique but could be better */
1497 	return 0;
1498 }
1499 
1500 static __noinline int
1501 setmlme_assoc_adhoc(struct ieee80211vap *vap,
1502 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1503 	const uint8_t ssid[IEEE80211_NWID_LEN])
1504 {
1505 	struct ieee80211_scan_req sr;
1506 
1507 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1508 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1509 	    ("expected opmode IBSS or AHDEMO not %s",
1510 	    ieee80211_opmode_name[vap->iv_opmode]));
1511 
1512 	if (ssid_len == 0)
1513 		return EINVAL;
1514 
1515 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1516 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1517 	vap->iv_des_ssid[0].len = ssid_len;
1518 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1519 	vap->iv_des_nssid = 1;
1520 
1521 	memset(&sr, 0, sizeof(sr));
1522 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
1523 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1524 	memcpy(sr.sr_ssid[0].ssid, ssid, ssid_len);
1525 	sr.sr_ssid[0].len = ssid_len;
1526 	sr.sr_nssid = 1;
1527 
1528 	return ieee80211_scanreq(vap, &sr);
1529 }
1530 
1531 static __noinline int
1532 ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1533 {
1534 	struct ieee80211req_mlme mlme;
1535 	int error;
1536 
1537 	if (ireq->i_len != sizeof(mlme))
1538 		return EINVAL;
1539 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1540 	if (error)
1541 		return error;
1542 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1543 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1544 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1545 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1546 	else if (mlme.im_op == IEEE80211_MLME_ASSOC)
1547 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1548 		    mlme.im_ssid_len, mlme.im_ssid);
1549 	else
1550 		return setmlme_common(vap, mlme.im_op,
1551 		    mlme.im_macaddr, mlme.im_reason);
1552 }
1553 
1554 static __noinline int
1555 ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1556 {
1557 	uint8_t mac[IEEE80211_ADDR_LEN];
1558 	const struct ieee80211_aclator *acl = vap->iv_acl;
1559 	int error;
1560 
1561 	if (ireq->i_len != sizeof(mac))
1562 		return EINVAL;
1563 	error = copyin(ireq->i_data, mac, ireq->i_len);
1564 	if (error)
1565 		return error;
1566 	if (acl == NULL) {
1567 		acl = ieee80211_aclator_get("mac");
1568 		if (acl == NULL || !acl->iac_attach(vap))
1569 			return EINVAL;
1570 		vap->iv_acl = acl;
1571 	}
1572 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1573 		acl->iac_add(vap, mac);
1574 	else
1575 		acl->iac_remove(vap, mac);
1576 	return 0;
1577 }
1578 
1579 static __noinline int
1580 ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1581 {
1582 	const struct ieee80211_aclator *acl = vap->iv_acl;
1583 
1584 	switch (ireq->i_val) {
1585 	case IEEE80211_MACCMD_POLICY_OPEN:
1586 	case IEEE80211_MACCMD_POLICY_ALLOW:
1587 	case IEEE80211_MACCMD_POLICY_DENY:
1588 	case IEEE80211_MACCMD_POLICY_RADIUS:
1589 		if (acl == NULL) {
1590 			acl = ieee80211_aclator_get("mac");
1591 			if (acl == NULL || !acl->iac_attach(vap))
1592 				return EINVAL;
1593 			vap->iv_acl = acl;
1594 		}
1595 		acl->iac_setpolicy(vap, ireq->i_val);
1596 		break;
1597 	case IEEE80211_MACCMD_FLUSH:
1598 		if (acl != NULL)
1599 			acl->iac_flush(vap);
1600 		/* NB: silently ignore when not in use */
1601 		break;
1602 	case IEEE80211_MACCMD_DETACH:
1603 		if (acl != NULL) {
1604 			vap->iv_acl = NULL;
1605 			acl->iac_detach(vap);
1606 		}
1607 		break;
1608 	default:
1609 		if (acl == NULL)
1610 			return EINVAL;
1611 		else
1612 			return acl->iac_setioctl(vap, ireq);
1613 	}
1614 	return 0;
1615 }
1616 
1617 static __noinline int
1618 ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1619 {
1620 	struct ieee80211com *ic = vap->iv_ic;
1621 	uint8_t *chanlist, *list;
1622 	int i, nchan, maxchan, error;
1623 
1624 	if (ireq->i_len > sizeof(ic->ic_chan_active))
1625 		ireq->i_len = sizeof(ic->ic_chan_active);
1626 	list = malloc(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1627 	    M_NOWAIT | M_ZERO);
1628 	if (list == NULL)
1629 		return ENOMEM;
1630 	error = copyin(ireq->i_data, list, ireq->i_len);
1631 	if (error) {
1632 		free(list, M_TEMP);
1633 		return error;
1634 	}
1635 	nchan = 0;
1636 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
1637 	maxchan = ireq->i_len * NBBY;
1638 	for (i = 0; i < ic->ic_nchans; i++) {
1639 		const struct ieee80211_channel *c = &ic->ic_channels[i];
1640 		/*
1641 		 * Calculate the intersection of the user list and the
1642 		 * available channels so users can do things like specify
1643 		 * 1-255 to get all available channels.
1644 		 */
1645 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
1646 			setbit(chanlist, c->ic_ieee);
1647 			nchan++;
1648 		}
1649 	}
1650 	if (nchan == 0) {
1651 		free(list, M_TEMP);
1652 		return EINVAL;
1653 	}
1654 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1655 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1656 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1657 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1658 	ieee80211_scan_flush(vap);
1659 	free(list, M_TEMP);
1660 	return ENETRESET;
1661 }
1662 
1663 static __noinline int
1664 ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1665 {
1666 	struct ieee80211_node *ni;
1667 	uint8_t macaddr[IEEE80211_ADDR_LEN];
1668 	int error;
1669 
1670 	/*
1671 	 * NB: we could copyin ieee80211req_sta_stats so apps
1672 	 *     could make selective changes but that's overkill;
1673 	 *     just clear all stats for now.
1674 	 */
1675 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1676 		return EINVAL;
1677 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1678 	if (error != 0)
1679 		return error;
1680 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1681 	if (ni == NULL)
1682 		return ENOENT;
1683 	/* XXX require ni_vap == vap? */
1684 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1685 	ieee80211_free_node(ni);
1686 	return 0;
1687 }
1688 
1689 static __noinline int
1690 ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1691 {
1692 	struct ieee80211_node *ni;
1693 	struct ieee80211req_sta_txpow txpow;
1694 	int error;
1695 
1696 	if (ireq->i_len != sizeof(txpow))
1697 		return EINVAL;
1698 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1699 	if (error != 0)
1700 		return error;
1701 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1702 	if (ni == NULL)
1703 		return ENOENT;
1704 	ni->ni_txpower = txpow.it_txpow;
1705 	ieee80211_free_node(ni);
1706 	return error;
1707 }
1708 
1709 static __noinline int
1710 ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1711 {
1712 	struct ieee80211com *ic = vap->iv_ic;
1713 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1714 	struct wmeParams *wmep, *chanp;
1715 	int isbss, ac;
1716 
1717 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1718 		return EOPNOTSUPP;
1719 
1720 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1721 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1722 	if (ac >= WME_NUM_AC)
1723 		ac = WME_AC_BE;
1724 	if (isbss) {
1725 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1726 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1727 	} else {
1728 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1729 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1730 	}
1731 	switch (ireq->i_type) {
1732 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1733 		if (isbss) {
1734 			wmep->wmep_logcwmin = ireq->i_val;
1735 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1736 				chanp->wmep_logcwmin = ireq->i_val;
1737 		} else {
1738 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
1739 				ireq->i_val;
1740 		}
1741 		break;
1742 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1743 		if (isbss) {
1744 			wmep->wmep_logcwmax = ireq->i_val;
1745 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1746 				chanp->wmep_logcwmax = ireq->i_val;
1747 		} else {
1748 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
1749 				ireq->i_val;
1750 		}
1751 		break;
1752 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1753 		if (isbss) {
1754 			wmep->wmep_aifsn = ireq->i_val;
1755 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1756 				chanp->wmep_aifsn = ireq->i_val;
1757 		} else {
1758 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
1759 		}
1760 		break;
1761 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1762 		if (isbss) {
1763 			wmep->wmep_txopLimit = ireq->i_val;
1764 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1765 				chanp->wmep_txopLimit = ireq->i_val;
1766 		} else {
1767 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
1768 				ireq->i_val;
1769 		}
1770 		break;
1771 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1772 		wmep->wmep_acm = ireq->i_val;
1773 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1774 			chanp->wmep_acm = ireq->i_val;
1775 		break;
1776 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1777 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1778 			(ireq->i_val) == 0;
1779 		break;
1780 	}
1781 	ieee80211_wme_updateparams(vap);
1782 	return 0;
1783 }
1784 
1785 static int
1786 find11gchannel(struct ieee80211com *ic, int start, int freq)
1787 {
1788 	const struct ieee80211_channel *c;
1789 	int i;
1790 
1791 	for (i = start+1; i < ic->ic_nchans; i++) {
1792 		c = &ic->ic_channels[i];
1793 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1794 			return 1;
1795 	}
1796 	/* NB: should not be needed but in case things are mis-sorted */
1797 	for (i = 0; i < start; i++) {
1798 		c = &ic->ic_channels[i];
1799 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1800 			return 1;
1801 	}
1802 	return 0;
1803 }
1804 
1805 static struct ieee80211_channel *
1806 findchannel(struct ieee80211com *ic, int ieee, int mode)
1807 {
1808 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1809 	    [IEEE80211_MODE_AUTO]	= 0,
1810 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1811 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1812 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1813 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1814 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1815 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1816 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
1817 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
1818 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
1819 	    /* NB: handled specially below */
1820 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1821 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
1822 	};
1823 	u_int modeflags;
1824 	int i;
1825 
1826 	modeflags = chanflags[mode];
1827 	for (i = 0; i < ic->ic_nchans; i++) {
1828 		struct ieee80211_channel *c = &ic->ic_channels[i];
1829 
1830 		if (c->ic_ieee != ieee)
1831 			continue;
1832 		if (mode == IEEE80211_MODE_AUTO) {
1833 			/* ignore turbo channels for autoselect */
1834 			if (IEEE80211_IS_CHAN_TURBO(c))
1835 				continue;
1836 			/*
1837 			 * XXX special-case 11b/g channels so we
1838 			 *     always select the g channel if both
1839 			 *     are present.
1840 			 * XXX prefer HT to non-HT?
1841 			 */
1842 			if (!IEEE80211_IS_CHAN_B(c) ||
1843 			    !find11gchannel(ic, i, c->ic_freq))
1844 				return c;
1845 		} else {
1846 			/* must check HT specially */
1847 			if ((mode == IEEE80211_MODE_11NA ||
1848 			    mode == IEEE80211_MODE_11NG) &&
1849 			    !IEEE80211_IS_CHAN_HT(c))
1850 				continue;
1851 			if ((c->ic_flags & modeflags) == modeflags)
1852 				return c;
1853 		}
1854 	}
1855 	return NULL;
1856 }
1857 
1858 /*
1859  * Check the specified against any desired mode (aka netband).
1860  * This is only used (presently) when operating in hostap mode
1861  * to enforce consistency.
1862  */
1863 static int
1864 check_mode_consistency(const struct ieee80211_channel *c, int mode)
1865 {
1866 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1867 
1868 	switch (mode) {
1869 	case IEEE80211_MODE_11B:
1870 		return (IEEE80211_IS_CHAN_B(c));
1871 	case IEEE80211_MODE_11G:
1872 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1873 	case IEEE80211_MODE_11A:
1874 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1875 	case IEEE80211_MODE_STURBO_A:
1876 		return (IEEE80211_IS_CHAN_STURBO(c));
1877 	case IEEE80211_MODE_11NA:
1878 		return (IEEE80211_IS_CHAN_HTA(c));
1879 	case IEEE80211_MODE_11NG:
1880 		return (IEEE80211_IS_CHAN_HTG(c));
1881 	}
1882 	return 1;
1883 
1884 }
1885 
1886 /*
1887  * Common code to set the current channel.  If the device
1888  * is up and running this may result in an immediate channel
1889  * change or a kick of the state machine.
1890  */
1891 static int
1892 setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1893 {
1894 	struct ieee80211com *ic = vap->iv_ic;
1895 	int error;
1896 
1897 	if (c != IEEE80211_CHAN_ANYC) {
1898 		if (IEEE80211_IS_CHAN_RADAR(c))
1899 			return EBUSY;	/* XXX better code? */
1900 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1901 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1902 				return EINVAL;
1903 			if (!check_mode_consistency(c, vap->iv_des_mode))
1904 				return EINVAL;
1905 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1906 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1907 				return EINVAL;
1908 		}
1909 		if (vap->iv_state == IEEE80211_S_RUN &&
1910 		    vap->iv_bss->ni_chan == c)
1911 			return 0;	/* NB: nothing to do */
1912 	}
1913 	vap->iv_des_chan = c;
1914 
1915 	error = 0;
1916 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1917 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1918 		/*
1919 		 * Monitor mode can switch directly.
1920 		 */
1921 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1922 			/* XXX need state machine for other vap's to follow */
1923 			ieee80211_setcurchan(ic, vap->iv_des_chan);
1924 			vap->iv_bss->ni_chan = ic->ic_curchan;
1925 		} else
1926 			ic->ic_curchan = vap->iv_des_chan;
1927 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1928 	} else {
1929 		/*
1930 		 * Need to go through the state machine in case we
1931 		 * need to reassociate or the like.  The state machine
1932 		 * will pickup the desired channel and avoid scanning.
1933 		 */
1934 		if (IS_UP_AUTO(vap))
1935 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1936 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1937 			/*
1938 			 * When not up+running and a real channel has
1939 			 * been specified fix the current channel so
1940 			 * there is immediate feedback; e.g. via ifconfig.
1941 			 */
1942 			ic->ic_curchan = vap->iv_des_chan;
1943 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1944 		}
1945 	}
1946 	return error;
1947 }
1948 
1949 /*
1950  * Old api for setting the current channel; this is
1951  * deprecated because channel numbers are ambiguous.
1952  */
1953 static __noinline int
1954 ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
1955 	const struct ieee80211req *ireq)
1956 {
1957 	struct ieee80211com *ic = vap->iv_ic;
1958 	struct ieee80211_channel *c;
1959 
1960 	/* XXX 0xffff overflows 16-bit signed */
1961 	if (ireq->i_val == 0 ||
1962 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
1963 		c = IEEE80211_CHAN_ANYC;
1964 	} else {
1965 		struct ieee80211_channel *c2;
1966 
1967 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
1968 		if (c == NULL) {
1969 			c = findchannel(ic, ireq->i_val,
1970 				IEEE80211_MODE_AUTO);
1971 			if (c == NULL)
1972 				return EINVAL;
1973 		}
1974 		/*
1975 		 * Fine tune channel selection based on desired mode:
1976 		 *   if 11b is requested, find the 11b version of any
1977 		 *      11g channel returned,
1978 		 *   if static turbo, find the turbo version of any
1979 		 *	11a channel return,
1980 		 *   if 11na is requested, find the ht version of any
1981 		 *      11a channel returned,
1982 		 *   if 11ng is requested, find the ht version of any
1983 		 *      11g channel returned,
1984 		 *   otherwise we should be ok with what we've got.
1985 		 */
1986 		switch (vap->iv_des_mode) {
1987 		case IEEE80211_MODE_11B:
1988 			if (IEEE80211_IS_CHAN_ANYG(c)) {
1989 				c2 = findchannel(ic, ireq->i_val,
1990 					IEEE80211_MODE_11B);
1991 				/* NB: should not happen, =>'s 11g w/o 11b */
1992 				if (c2 != NULL)
1993 					c = c2;
1994 			}
1995 			break;
1996 		case IEEE80211_MODE_TURBO_A:
1997 			if (IEEE80211_IS_CHAN_A(c)) {
1998 				c2 = findchannel(ic, ireq->i_val,
1999 					IEEE80211_MODE_TURBO_A);
2000 				if (c2 != NULL)
2001 					c = c2;
2002 			}
2003 			break;
2004 		case IEEE80211_MODE_11NA:
2005 			if (IEEE80211_IS_CHAN_A(c)) {
2006 				c2 = findchannel(ic, ireq->i_val,
2007 					IEEE80211_MODE_11NA);
2008 				if (c2 != NULL)
2009 					c = c2;
2010 			}
2011 			break;
2012 		case IEEE80211_MODE_11NG:
2013 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2014 				c2 = findchannel(ic, ireq->i_val,
2015 					IEEE80211_MODE_11NG);
2016 				if (c2 != NULL)
2017 					c = c2;
2018 			}
2019 			break;
2020 		default:		/* NB: no static turboG */
2021 			break;
2022 		}
2023 	}
2024 	return setcurchan(vap, c);
2025 }
2026 
2027 /*
2028  * New/current api for setting the current channel; a complete
2029  * channel description is provide so there is no ambiguity in
2030  * identifying the channel.
2031  */
2032 static __noinline int
2033 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
2034 	const struct ieee80211req *ireq)
2035 {
2036 	struct ieee80211com *ic = vap->iv_ic;
2037 	struct ieee80211_channel chan, *c;
2038 	int error;
2039 
2040 	if (ireq->i_len != sizeof(chan))
2041 		return EINVAL;
2042 	error = copyin(ireq->i_data, &chan, sizeof(chan));
2043 	if (error != 0)
2044 		return error;
2045 	/* XXX 0xffff overflows 16-bit signed */
2046 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
2047 		c = IEEE80211_CHAN_ANYC;
2048 	} else {
2049 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
2050 		if (c == NULL)
2051 			return EINVAL;
2052 	}
2053 	return setcurchan(vap, c);
2054 }
2055 
2056 static __noinline int
2057 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2058 	const struct ieee80211req *ireq)
2059 {
2060 	struct ieee80211_regdomain_req *reg;
2061 	int nchans, error;
2062 
2063 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2064 	    sizeof(struct ieee80211_channel));
2065 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2066 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2067 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2068 		    ireq->i_len, nchans);
2069 		return EINVAL;
2070 	}
2071 	reg = (struct ieee80211_regdomain_req *)
2072 	    malloc(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, M_NOWAIT);
2073 	if (reg == NULL) {
2074 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2075 		    "%s: no memory, nchans %d\n", __func__, nchans);
2076 		return ENOMEM;
2077 	}
2078 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2079 	if (error == 0) {
2080 		/* NB: validate inline channel count against storage size */
2081 		if (reg->chaninfo.ic_nchans != nchans) {
2082 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2083 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2084 				reg->chaninfo.ic_nchans, nchans);
2085 			error = EINVAL;
2086 		} else
2087 			error = ieee80211_setregdomain(vap, reg);
2088 	}
2089 	free(reg, M_TEMP);
2090 
2091 	return (error == 0 ? ENETRESET : error);
2092 }
2093 
2094 static int
2095 ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2096 	const struct ieee80211req *ireq)
2097 {
2098 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2099 		return EINVAL;
2100 	/* XXX validate params */
2101 	/* XXX? ENETRESET to push to device? */
2102 	return copyin(ireq->i_data, vap->iv_roamparms,
2103 	    sizeof(vap->iv_roamparms));
2104 }
2105 
2106 static int
2107 checkrate(const struct ieee80211_rateset *rs, int rate)
2108 {
2109 	int i;
2110 
2111 	if (rate == IEEE80211_FIXED_RATE_NONE)
2112 		return 1;
2113 	for (i = 0; i < rs->rs_nrates; i++)
2114 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2115 			return 1;
2116 	return 0;
2117 }
2118 
2119 static int
2120 checkmcs(int mcs)
2121 {
2122 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2123 		return 1;
2124 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2125 		return 0;
2126 	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2127 }
2128 
2129 static __noinline int
2130 ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2131 	const struct ieee80211req *ireq)
2132 {
2133 	struct ieee80211com *ic = vap->iv_ic;
2134 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2135 	struct ieee80211_txparam *src, *dst;
2136 	const struct ieee80211_rateset *rs;
2137 	int error, mode, changed, is11n, nmodes;
2138 
2139 	/* NB: accept short requests for backwards compat */
2140 	if (ireq->i_len > sizeof(parms))
2141 		return EINVAL;
2142 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2143 	if (error != 0)
2144 		return error;
2145 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2146 	changed = 0;
2147 	/* validate parameters and check if anything changed */
2148 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2149 		if (isclr(ic->ic_modecaps, mode))
2150 			continue;
2151 		src = &parms.params[mode];
2152 		dst = &vap->iv_txparms[mode];
2153 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2154 		is11n = (mode == IEEE80211_MODE_11NA ||
2155 			 mode == IEEE80211_MODE_11NG);
2156 		if (src->ucastrate != dst->ucastrate) {
2157 			if (!checkrate(rs, src->ucastrate) &&
2158 			    (!is11n || !checkmcs(src->ucastrate)))
2159 				return EINVAL;
2160 			changed++;
2161 		}
2162 		if (src->mcastrate != dst->mcastrate) {
2163 			if (!checkrate(rs, src->mcastrate) &&
2164 			    (!is11n || !checkmcs(src->mcastrate)))
2165 				return EINVAL;
2166 			changed++;
2167 		}
2168 		if (src->mgmtrate != dst->mgmtrate) {
2169 			if (!checkrate(rs, src->mgmtrate) &&
2170 			    (!is11n || !checkmcs(src->mgmtrate)))
2171 				return EINVAL;
2172 			changed++;
2173 		}
2174 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2175 			changed++;
2176 	}
2177 	if (changed) {
2178 		/*
2179 		 * Copy new parameters in place and notify the
2180 		 * driver so it can push state to the device.
2181 		 */
2182 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2183 			if (isset(ic->ic_modecaps, mode))
2184 				vap->iv_txparms[mode] = parms.params[mode];
2185 		}
2186 		/* XXX could be more intelligent,
2187 		   e.g. don't reset if setting not being used */
2188 		return ENETRESET;
2189 	}
2190 	return 0;
2191 }
2192 
2193 /*
2194  * Application Information Element support.
2195  */
2196 static int
2197 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2198 {
2199 	struct ieee80211_appie *app = *aie;
2200 	struct ieee80211_appie *napp;
2201 	int error;
2202 
2203 	if (ireq->i_len == 0) {		/* delete any existing ie */
2204 		if (app != NULL) {
2205 			*aie = NULL;	/* XXX racey */
2206 			free(app, M_80211_NODE_IE);
2207 		}
2208 		return 0;
2209 	}
2210 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2211 		return EINVAL;
2212 	/*
2213 	 * Allocate a new appie structure and copy in the user data.
2214 	 * When done swap in the new structure.  Note that we do not
2215 	 * guard against users holding a ref to the old structure;
2216 	 * this must be handled outside this code.
2217 	 *
2218 	 * XXX bad bad bad
2219 	 */
2220 	napp = (struct ieee80211_appie *) malloc(
2221 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2222 	if (napp == NULL)
2223 		return ENOMEM;
2224 	/* XXX holding ic lock */
2225 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2226 	if (error) {
2227 		free(napp, M_80211_NODE_IE);
2228 		return error;
2229 	}
2230 	napp->ie_len = ireq->i_len;
2231 	*aie = napp;
2232 	if (app != NULL)
2233 		free(app, M_80211_NODE_IE);
2234 	return 0;
2235 }
2236 
2237 static void
2238 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2239 {
2240 	/* validate data is present as best we can */
2241 	if (space == 0 || 2+ie[1] > space)
2242 		return;
2243 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2244 		vap->iv_wpa_ie = ie;
2245 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2246 		vap->iv_rsn_ie = ie;
2247 }
2248 
2249 static __noinline int
2250 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2251 	const struct ieee80211req *ireq, int fc0)
2252 {
2253 	int error;
2254 
2255 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2256 
2257 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2258 	case IEEE80211_FC0_SUBTYPE_BEACON:
2259 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2260 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2261 			error = EINVAL;
2262 			break;
2263 		}
2264 		error = setappie(&vap->iv_appie_beacon, ireq);
2265 		if (error == 0)
2266 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2267 		break;
2268 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2269 		error = setappie(&vap->iv_appie_proberesp, ireq);
2270 		break;
2271 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2272 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2273 			error = setappie(&vap->iv_appie_assocresp, ireq);
2274 		else
2275 			error = EINVAL;
2276 		break;
2277 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2278 		error = setappie(&vap->iv_appie_probereq, ireq);
2279 		break;
2280 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2281 		if (vap->iv_opmode == IEEE80211_M_STA)
2282 			error = setappie(&vap->iv_appie_assocreq, ireq);
2283 		else
2284 			error = EINVAL;
2285 		break;
2286 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2287 		error = setappie(&vap->iv_appie_wpa, ireq);
2288 		if (error == 0) {
2289 			/*
2290 			 * Must split single blob of data into separate
2291 			 * WPA and RSN ie's because they go in different
2292 			 * locations in the mgt frames.
2293 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2294 			 */
2295 			vap->iv_wpa_ie = NULL;
2296 			vap->iv_rsn_ie = NULL;
2297 			if (vap->iv_appie_wpa != NULL) {
2298 				struct ieee80211_appie *appie =
2299 				    vap->iv_appie_wpa;
2300 				uint8_t *data = appie->ie_data;
2301 
2302 				/* XXX ie length validate is painful, cheat */
2303 				setwparsnie(vap, data, appie->ie_len);
2304 				setwparsnie(vap, data + 2 + data[1],
2305 				    appie->ie_len - (2 + data[1]));
2306 			}
2307 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2308 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2309 				/*
2310 				 * Must rebuild beacon frame as the update
2311 				 * mechanism doesn't handle WPA/RSN ie's.
2312 				 * Could extend it but it doesn't normally
2313 				 * change; this is just to deal with hostapd
2314 				 * plumbing the ie after the interface is up.
2315 				 */
2316 				error = ENETRESET;
2317 			}
2318 		}
2319 		break;
2320 	default:
2321 		error = EINVAL;
2322 		break;
2323 	}
2324 	return error;
2325 }
2326 
2327 static __noinline int
2328 ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2329 	const struct ieee80211req *ireq)
2330 {
2331 	struct ieee80211com *ic = vap->iv_ic;
2332 	int error;
2333 	uint8_t fc0;
2334 
2335 	fc0 = ireq->i_val & 0xff;
2336 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2337 		return EINVAL;
2338 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2339 	IEEE80211_LOCK(ic);
2340 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2341 	IEEE80211_UNLOCK(ic);
2342 	return error;
2343 }
2344 
2345 static __noinline int
2346 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2347 {
2348 	struct ieee80211com *ic = vap->iv_ic;
2349 	struct ieee80211_chanswitch_req csr;
2350 	struct ieee80211_channel *c;
2351 	int error;
2352 
2353 	if (ireq->i_len != sizeof(csr))
2354 		return EINVAL;
2355 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2356 	if (error != 0)
2357 		return error;
2358 	/* XXX adhoc mode not supported */
2359 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2360 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2361 		return EOPNOTSUPP;
2362 	c = ieee80211_find_channel(ic,
2363 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2364 	if (c == NULL)
2365 		return ENOENT;
2366 	IEEE80211_LOCK(ic);
2367 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2368 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2369 	else if (csr.csa_count == 0)
2370 		ieee80211_csa_cancelswitch(ic);
2371 	else
2372 		error = EBUSY;
2373 	IEEE80211_UNLOCK(ic);
2374 	return error;
2375 }
2376 
2377 static int
2378 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2379 {
2380 #define	IEEE80211_IOC_SCAN_FLAGS \
2381 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2382 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2383 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2384 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2385 	 IEEE80211_IOC_SCAN_CHECK)
2386 	struct ieee80211com *ic = vap->iv_ic;
2387 	int i;
2388 
2389 	/* convert duration */
2390 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2391 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2392 	else {
2393 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2394 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2395 			return EINVAL;
2396 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2397 		if (sr->sr_duration < 1)
2398 			sr->sr_duration = 1;
2399 	}
2400 	/* convert min/max channel dwell */
2401 	if (sr->sr_mindwell != 0) {
2402 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2403 		if (sr->sr_mindwell < 1)
2404 			sr->sr_mindwell = 1;
2405 	}
2406 	if (sr->sr_maxdwell != 0) {
2407 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2408 		if (sr->sr_maxdwell < 1)
2409 			sr->sr_maxdwell = 1;
2410 	}
2411 	/* NB: silently reduce ssid count to what is supported */
2412 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2413 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2414 	for (i = 0; i < sr->sr_nssid; i++)
2415 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2416 			return EINVAL;
2417 	/* cleanse flags just in case, could reject if invalid flags */
2418 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2419 	/*
2420 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2421 	 * allows applications to scan without joining a bss (or picking
2422 	 * a channel and setting up a bss) and without forcing manual
2423 	 * roaming mode--you just need to mark the parent device UP.
2424 	 */
2425 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2426 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2427 
2428 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2429 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2430 	    __func__, sr->sr_flags,
2431 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2432 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2433 	/*
2434 	 * If we are in INIT state then the driver has never had a chance
2435 	 * to setup hardware state to do a scan; we must use the state
2436 	 * machine to get us up to the SCAN state but once we reach SCAN
2437 	 * state we then want to use the supplied params.  Stash the
2438 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2439 	 * state machines will recognize this and use the stashed params
2440 	 * to issue the scan request.
2441 	 *
2442 	 * Otherwise just invoke the scan machinery directly.
2443 	 */
2444 	IEEE80211_LOCK(ic);
2445 	if (vap->iv_state == IEEE80211_S_INIT) {
2446 		/* NB: clobbers previous settings */
2447 		vap->iv_scanreq_flags = sr->sr_flags;
2448 		vap->iv_scanreq_duration = sr->sr_duration;
2449 		vap->iv_scanreq_nssid = sr->sr_nssid;
2450 		for (i = 0; i < sr->sr_nssid; i++) {
2451 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2452 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2453 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2454 		}
2455 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2456 		IEEE80211_UNLOCK(ic);
2457 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2458 	} else {
2459 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2460 		IEEE80211_UNLOCK(ic);
2461 		/* XXX neeed error return codes */
2462 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2463 			(void) ieee80211_check_scan(vap, sr->sr_flags,
2464 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2465 			    sr->sr_nssid,
2466 			    /* NB: cheat, we assume structures are compatible */
2467 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2468 		} else {
2469 			(void) ieee80211_start_scan(vap, sr->sr_flags,
2470 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2471 			    sr->sr_nssid,
2472 			    /* NB: cheat, we assume structures are compatible */
2473 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2474 		}
2475 	}
2476 	return 0;
2477 #undef IEEE80211_IOC_SCAN_FLAGS
2478 }
2479 
2480 static __noinline int
2481 ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2482 {
2483 	struct ieee80211com *ic = vap->iv_ic;
2484 	struct ieee80211_scan_req sr;		/* XXX off stack? */
2485 	int error;
2486 
2487 	/* NB: parent must be running */
2488 	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2489 		return ENXIO;
2490 
2491 	if (ireq->i_len != sizeof(sr))
2492 		return EINVAL;
2493 	error = copyin(ireq->i_data, &sr, sizeof(sr));
2494 	if (error != 0)
2495 		return error;
2496 	return ieee80211_scanreq(vap, &sr);
2497 }
2498 
2499 static __noinline int
2500 ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2501 {
2502 	struct ieee80211_node *ni;
2503 	struct ieee80211req_sta_vlan vlan;
2504 	int error;
2505 
2506 	if (ireq->i_len != sizeof(vlan))
2507 		return EINVAL;
2508 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2509 	if (error != 0)
2510 		return error;
2511 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2512 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2513 		    vlan.sv_macaddr);
2514 		if (ni == NULL)
2515 			return ENOENT;
2516 	} else
2517 		ni = ieee80211_ref_node(vap->iv_bss);
2518 	ni->ni_vlan = vlan.sv_vlan;
2519 	ieee80211_free_node(ni);
2520 	return error;
2521 }
2522 
2523 static int
2524 isvap11g(const struct ieee80211vap *vap)
2525 {
2526 	const struct ieee80211_node *bss = vap->iv_bss;
2527 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2528 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2529 }
2530 
2531 static int
2532 isvapht(const struct ieee80211vap *vap)
2533 {
2534 	const struct ieee80211_node *bss = vap->iv_bss;
2535 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2536 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2537 }
2538 
2539 /*
2540  * Dummy ioctl set handler so the linker set is defined.
2541  */
2542 static int
2543 dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
2544 {
2545 	return ENOSYS;
2546 }
2547 IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
2548 
2549 static int
2550 ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
2551 {
2552 	ieee80211_ioctl_setfunc * const *set;
2553 	int error;
2554 
2555 	SET_FOREACH(set, ieee80211_ioctl_setset) {
2556 		error = (*set)(vap, ireq);
2557 		if (error != ENOSYS)
2558 			return error;
2559 	}
2560 	return EINVAL;
2561 }
2562 
2563 static __noinline int
2564 ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2565 {
2566 	struct ieee80211com *ic = vap->iv_ic;
2567 	int error;
2568 	const struct ieee80211_authenticator *auth;
2569 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2570 	char tmpssid[IEEE80211_NWID_LEN];
2571 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2572 	struct ieee80211_key *k;
2573 	u_int kid;
2574 	uint32_t flags;
2575 
2576 	error = 0;
2577 	switch (ireq->i_type) {
2578 	case IEEE80211_IOC_SSID:
2579 		if (ireq->i_val != 0 ||
2580 		    ireq->i_len > IEEE80211_NWID_LEN)
2581 			return EINVAL;
2582 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2583 		if (error)
2584 			break;
2585 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2586 		vap->iv_des_ssid[0].len = ireq->i_len;
2587 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2588 		vap->iv_des_nssid = (ireq->i_len > 0);
2589 		error = ENETRESET;
2590 		break;
2591 	case IEEE80211_IOC_WEP:
2592 		switch (ireq->i_val) {
2593 		case IEEE80211_WEP_OFF:
2594 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2595 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2596 			break;
2597 		case IEEE80211_WEP_ON:
2598 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2599 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2600 			break;
2601 		case IEEE80211_WEP_MIXED:
2602 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2603 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2604 			break;
2605 		}
2606 		error = ENETRESET;
2607 		break;
2608 	case IEEE80211_IOC_WEPKEY:
2609 		kid = (u_int) ireq->i_val;
2610 		if (kid >= IEEE80211_WEP_NKID)
2611 			return EINVAL;
2612 		k = &vap->iv_nw_keys[kid];
2613 		if (ireq->i_len == 0) {
2614 			/* zero-len =>'s delete any existing key */
2615 			(void) ieee80211_crypto_delkey(vap, k);
2616 			break;
2617 		}
2618 		if (ireq->i_len > sizeof(tmpkey))
2619 			return EINVAL;
2620 		memset(tmpkey, 0, sizeof(tmpkey));
2621 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2622 		if (error)
2623 			break;
2624 		ieee80211_key_update_begin(vap);
2625 		k->wk_keyix = kid;	/* NB: force fixed key id */
2626 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2627 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2628 			k->wk_keylen = ireq->i_len;
2629 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2630 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2631 			if  (!ieee80211_crypto_setkey(vap, k))
2632 				error = EINVAL;
2633 		} else
2634 			error = EINVAL;
2635 		ieee80211_key_update_end(vap);
2636 		break;
2637 	case IEEE80211_IOC_WEPTXKEY:
2638 		kid = (u_int) ireq->i_val;
2639 		if (kid >= IEEE80211_WEP_NKID &&
2640 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2641 			return EINVAL;
2642 		vap->iv_def_txkey = kid;
2643 		break;
2644 	case IEEE80211_IOC_AUTHMODE:
2645 		switch (ireq->i_val) {
2646 		case IEEE80211_AUTH_WPA:
2647 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2648 		case IEEE80211_AUTH_OPEN:	/* open */
2649 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2650 		case IEEE80211_AUTH_AUTO:	/* auto */
2651 			auth = ieee80211_authenticator_get(ireq->i_val);
2652 			if (auth == NULL)
2653 				return EINVAL;
2654 			break;
2655 		default:
2656 			return EINVAL;
2657 		}
2658 		switch (ireq->i_val) {
2659 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2660 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2661 			ireq->i_val = IEEE80211_AUTH_8021X;
2662 			break;
2663 		case IEEE80211_AUTH_OPEN:	/* open */
2664 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2665 			break;
2666 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2667 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2668 			vap->iv_flags &= ~IEEE80211_F_WPA;
2669 			/* both require a key so mark the PRIVACY capability */
2670 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2671 			break;
2672 		case IEEE80211_AUTH_AUTO:	/* auto */
2673 			vap->iv_flags &= ~IEEE80211_F_WPA;
2674 			/* XXX PRIVACY handling? */
2675 			/* XXX what's the right way to do this? */
2676 			break;
2677 		}
2678 		/* NB: authenticator attach/detach happens on state change */
2679 		vap->iv_bss->ni_authmode = ireq->i_val;
2680 		/* XXX mixed/mode/usage? */
2681 		vap->iv_auth = auth;
2682 		error = ENETRESET;
2683 		break;
2684 	case IEEE80211_IOC_CHANNEL:
2685 		error = ieee80211_ioctl_setchannel(vap, ireq);
2686 		break;
2687 	case IEEE80211_IOC_POWERSAVE:
2688 		switch (ireq->i_val) {
2689 		case IEEE80211_POWERSAVE_OFF:
2690 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2691 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2692 				error = ERESTART;
2693 			}
2694 			break;
2695 		case IEEE80211_POWERSAVE_ON:
2696 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2697 				error = EOPNOTSUPP;
2698 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2699 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2700 				error = ERESTART;
2701 			}
2702 			break;
2703 		default:
2704 			error = EINVAL;
2705 			break;
2706 		}
2707 		break;
2708 	case IEEE80211_IOC_POWERSAVESLEEP:
2709 		if (ireq->i_val < 0)
2710 			return EINVAL;
2711 		ic->ic_lintval = ireq->i_val;
2712 		error = ERESTART;
2713 		break;
2714 	case IEEE80211_IOC_RTSTHRESHOLD:
2715 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2716 		      ireq->i_val <= IEEE80211_RTS_MAX))
2717 			return EINVAL;
2718 		vap->iv_rtsthreshold = ireq->i_val;
2719 		error = ERESTART;
2720 		break;
2721 	case IEEE80211_IOC_PROTMODE:
2722 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2723 			return EINVAL;
2724 		ic->ic_protmode = ireq->i_val;
2725 		/* NB: if not operating in 11g this can wait */
2726 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2727 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2728 			error = ERESTART;
2729 		break;
2730 	case IEEE80211_IOC_TXPOWER:
2731 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2732 			return EOPNOTSUPP;
2733 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2734 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2735 			return EINVAL;
2736 		ic->ic_txpowlimit = ireq->i_val;
2737 		error = ERESTART;
2738 		break;
2739 	case IEEE80211_IOC_ROAMING:
2740 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2741 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2742 			return EINVAL;
2743 		vap->iv_roaming = ireq->i_val;
2744 		/* XXXX reset? */
2745 		break;
2746 	case IEEE80211_IOC_PRIVACY:
2747 		if (ireq->i_val) {
2748 			/* XXX check for key state? */
2749 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2750 		} else
2751 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2752 		/* XXX ERESTART? */
2753 		break;
2754 	case IEEE80211_IOC_DROPUNENCRYPTED:
2755 		if (ireq->i_val)
2756 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2757 		else
2758 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2759 		/* XXX ERESTART? */
2760 		break;
2761 	case IEEE80211_IOC_WPAKEY:
2762 		error = ieee80211_ioctl_setkey(vap, ireq);
2763 		break;
2764 	case IEEE80211_IOC_DELKEY:
2765 		error = ieee80211_ioctl_delkey(vap, ireq);
2766 		break;
2767 	case IEEE80211_IOC_MLME:
2768 		error = ieee80211_ioctl_setmlme(vap, ireq);
2769 		break;
2770 	case IEEE80211_IOC_COUNTERMEASURES:
2771 		if (ireq->i_val) {
2772 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2773 				return EOPNOTSUPP;
2774 			vap->iv_flags |= IEEE80211_F_COUNTERM;
2775 		} else
2776 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2777 		/* XXX ERESTART? */
2778 		break;
2779 	case IEEE80211_IOC_WPA:
2780 		if (ireq->i_val > 3)
2781 			return EINVAL;
2782 		/* XXX verify ciphers available */
2783 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2784 		switch (ireq->i_val) {
2785 		case 1:
2786 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2787 				return EOPNOTSUPP;
2788 			flags |= IEEE80211_F_WPA1;
2789 			break;
2790 		case 2:
2791 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2792 				return EOPNOTSUPP;
2793 			flags |= IEEE80211_F_WPA2;
2794 			break;
2795 		case 3:
2796 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2797 				return EOPNOTSUPP;
2798 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2799 			break;
2800 		default:	/*  Can't set any -> error */
2801 			return EOPNOTSUPP;
2802 		}
2803 		vap->iv_flags = flags;
2804 		error = ERESTART;	/* NB: can change beacon frame */
2805 		break;
2806 	case IEEE80211_IOC_WME:
2807 		if (ireq->i_val) {
2808 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2809 				return EOPNOTSUPP;
2810 			ieee80211_syncflag(vap, IEEE80211_F_WME);
2811 		} else
2812 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2813 		error = ERESTART;	/* NB: can change beacon frame */
2814 		break;
2815 	case IEEE80211_IOC_HIDESSID:
2816 		if (ireq->i_val)
2817 			vap->iv_flags |= IEEE80211_F_HIDESSID;
2818 		else
2819 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2820 		error = ERESTART;		/* XXX ENETRESET? */
2821 		break;
2822 	case IEEE80211_IOC_APBRIDGE:
2823 		if (ireq->i_val == 0)
2824 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2825 		else
2826 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2827 		break;
2828 	case IEEE80211_IOC_BSSID:
2829 		if (ireq->i_len != sizeof(tmpbssid))
2830 			return EINVAL;
2831 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2832 		if (error)
2833 			break;
2834 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2835 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2836 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2837 		else
2838 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2839 		error = ENETRESET;
2840 		break;
2841 	case IEEE80211_IOC_CHANLIST:
2842 		error = ieee80211_ioctl_setchanlist(vap, ireq);
2843 		break;
2844 #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2845 #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2846 	case OLD_IEEE80211_IOC_SCAN_REQ:
2847 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2848 			"%s: active scan request\n", __func__);
2849 		/*
2850 		 * If we are in INIT state then the driver has never
2851 		 * had a chance to setup hardware state to do a scan;
2852 		 * use the state machine to get us up the SCAN state.
2853 		 * Otherwise just invoke the scan machinery to start
2854 		 * a one-time scan.
2855 		 */
2856 		if (vap->iv_state == IEEE80211_S_INIT)
2857 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2858 		else
2859 			(void) ieee80211_start_scan(vap,
2860 				IEEE80211_SCAN_ACTIVE |
2861 				IEEE80211_SCAN_NOPICK |
2862 				IEEE80211_SCAN_ONCE,
2863 				IEEE80211_SCAN_FOREVER, 0, 0,
2864 				/* XXX use ioctl params */
2865 				vap->iv_des_nssid, vap->iv_des_ssid);
2866 		break;
2867 #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2868 	case IEEE80211_IOC_SCAN_REQ:
2869 		error = ieee80211_ioctl_scanreq(vap, ireq);
2870 		break;
2871 	case IEEE80211_IOC_SCAN_CANCEL:
2872 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2873 		    "%s: cancel scan\n", __func__);
2874 		ieee80211_cancel_scan(vap);
2875 		break;
2876 	case IEEE80211_IOC_HTCONF:
2877 		if (ireq->i_val & 1)
2878 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
2879 		else
2880 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
2881 		if (ireq->i_val & 2)
2882 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
2883 		else
2884 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
2885 		error = ENETRESET;
2886 		break;
2887 	case IEEE80211_IOC_ADDMAC:
2888 	case IEEE80211_IOC_DELMAC:
2889 		error = ieee80211_ioctl_macmac(vap, ireq);
2890 		break;
2891 	case IEEE80211_IOC_MACCMD:
2892 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
2893 		break;
2894 	case IEEE80211_IOC_STA_STATS:
2895 		error = ieee80211_ioctl_setstastats(vap, ireq);
2896 		break;
2897 	case IEEE80211_IOC_STA_TXPOW:
2898 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
2899 		break;
2900 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
2901 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
2902 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
2903 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
2904 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
2905 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2906 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
2907 		break;
2908 	case IEEE80211_IOC_DTIM_PERIOD:
2909 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2910 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2911 		    vap->iv_opmode != IEEE80211_M_IBSS)
2912 			return EINVAL;
2913 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
2914 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2915 			vap->iv_dtim_period = ireq->i_val;
2916 			error = ENETRESET;		/* requires restart */
2917 		} else
2918 			error = EINVAL;
2919 		break;
2920 	case IEEE80211_IOC_BEACON_INTERVAL:
2921 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2922 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2923 		    vap->iv_opmode != IEEE80211_M_IBSS)
2924 			return EINVAL;
2925 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
2926 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2927 			ic->ic_bintval = ireq->i_val;
2928 			error = ENETRESET;		/* requires restart */
2929 		} else
2930 			error = EINVAL;
2931 		break;
2932 	case IEEE80211_IOC_PUREG:
2933 		if (ireq->i_val)
2934 			vap->iv_flags |= IEEE80211_F_PUREG;
2935 		else
2936 			vap->iv_flags &= ~IEEE80211_F_PUREG;
2937 		/* NB: reset only if we're operating on an 11g channel */
2938 		if (isvap11g(vap))
2939 			error = ENETRESET;
2940 		break;
2941 	case IEEE80211_IOC_BGSCAN:
2942 		if (ireq->i_val) {
2943 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
2944 				return EOPNOTSUPP;
2945 			vap->iv_flags |= IEEE80211_F_BGSCAN;
2946 		} else
2947 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
2948 		break;
2949 	case IEEE80211_IOC_BGSCAN_IDLE:
2950 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
2951 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
2952 		else
2953 			error = EINVAL;
2954 		break;
2955 	case IEEE80211_IOC_BGSCAN_INTERVAL:
2956 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
2957 			vap->iv_bgscanintvl = ireq->i_val*hz;
2958 		else
2959 			error = EINVAL;
2960 		break;
2961 	case IEEE80211_IOC_SCANVALID:
2962 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
2963 			vap->iv_scanvalid = ireq->i_val*hz;
2964 		else
2965 			error = EINVAL;
2966 		break;
2967 	case IEEE80211_IOC_FRAGTHRESHOLD:
2968 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
2969 		    ireq->i_val != IEEE80211_FRAG_MAX)
2970 			return EOPNOTSUPP;
2971 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
2972 		      ireq->i_val <= IEEE80211_FRAG_MAX))
2973 			return EINVAL;
2974 		vap->iv_fragthreshold = ireq->i_val;
2975 		error = ERESTART;
2976 		break;
2977 	case IEEE80211_IOC_BURST:
2978 		if (ireq->i_val) {
2979 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
2980 				return EOPNOTSUPP;
2981 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
2982 		} else
2983 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
2984 		error = ERESTART;
2985 		break;
2986 	case IEEE80211_IOC_BMISSTHRESHOLD:
2987 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
2988 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
2989 			return EINVAL;
2990 		vap->iv_bmissthreshold = ireq->i_val;
2991 		error = ERESTART;
2992 		break;
2993 	case IEEE80211_IOC_CURCHAN:
2994 		error = ieee80211_ioctl_setcurchan(vap, ireq);
2995 		break;
2996 	case IEEE80211_IOC_SHORTGI:
2997 		if (ireq->i_val) {
2998 #define	IEEE80211_HTCAP_SHORTGI \
2999 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3000 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
3001 				return EINVAL;
3002 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
3003 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
3004 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
3005 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
3006 #undef IEEE80211_HTCAP_SHORTGI
3007 		} else
3008 			vap->iv_flags_ht &=
3009 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3010 		error = ERESTART;
3011 		break;
3012 	case IEEE80211_IOC_AMPDU:
3013 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
3014 			return EINVAL;
3015 		if (ireq->i_val & 1)
3016 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3017 		else
3018 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3019 		if (ireq->i_val & 2)
3020 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3021 		else
3022 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
3023 		/* NB: reset only if we're operating on an 11n channel */
3024 		if (isvapht(vap))
3025 			error = ERESTART;
3026 		break;
3027 	case IEEE80211_IOC_AMPDU_LIMIT:
3028 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3029 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3030 			return EINVAL;
3031 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3032 			vap->iv_ampdu_rxmax = ireq->i_val;
3033 		else
3034 			vap->iv_ampdu_limit = ireq->i_val;
3035 		error = ERESTART;
3036 		break;
3037 	case IEEE80211_IOC_AMPDU_DENSITY:
3038 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3039 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3040 			return EINVAL;
3041 		vap->iv_ampdu_density = ireq->i_val;
3042 		error = ERESTART;
3043 		break;
3044 	case IEEE80211_IOC_AMSDU:
3045 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
3046 			return EINVAL;
3047 		if (ireq->i_val & 1)
3048 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3049 		else
3050 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
3051 		if (ireq->i_val & 2)
3052 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3053 		else
3054 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
3055 		/* NB: reset only if we're operating on an 11n channel */
3056 		if (isvapht(vap))
3057 			error = ERESTART;
3058 		break;
3059 	case IEEE80211_IOC_AMSDU_LIMIT:
3060 		/* XXX validate */
3061 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
3062 		break;
3063 	case IEEE80211_IOC_PUREN:
3064 		if (ireq->i_val) {
3065 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3066 				return EINVAL;
3067 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
3068 		} else
3069 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
3070 		/* NB: reset only if we're operating on an 11n channel */
3071 		if (isvapht(vap))
3072 			error = ERESTART;
3073 		break;
3074 	case IEEE80211_IOC_DOTH:
3075 		if (ireq->i_val) {
3076 #if 0
3077 			/* XXX no capability */
3078 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3079 				return EOPNOTSUPP;
3080 #endif
3081 			vap->iv_flags |= IEEE80211_F_DOTH;
3082 		} else
3083 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3084 		error = ENETRESET;
3085 		break;
3086 	case IEEE80211_IOC_REGDOMAIN:
3087 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3088 		break;
3089 	case IEEE80211_IOC_ROAM:
3090 		error = ieee80211_ioctl_setroam(vap, ireq);
3091 		break;
3092 	case IEEE80211_IOC_TXPARAMS:
3093 		error = ieee80211_ioctl_settxparams(vap, ireq);
3094 		break;
3095 	case IEEE80211_IOC_HTCOMPAT:
3096 		if (ireq->i_val) {
3097 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3098 				return EOPNOTSUPP;
3099 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
3100 		} else
3101 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
3102 		/* NB: reset only if we're operating on an 11n channel */
3103 		if (isvapht(vap))
3104 			error = ERESTART;
3105 		break;
3106 	case IEEE80211_IOC_DWDS:
3107 		if (ireq->i_val) {
3108 			/* NB: DWDS only makes sense for WDS-capable devices */
3109 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3110 				return EOPNOTSUPP;
3111 			/* NB: DWDS is used only with ap+sta vaps */
3112 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3113 			    vap->iv_opmode != IEEE80211_M_STA)
3114 				return EINVAL;
3115 			vap->iv_flags |= IEEE80211_F_DWDS;
3116 			if (vap->iv_opmode == IEEE80211_M_STA)
3117 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3118 		} else {
3119 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3120 			if (vap->iv_opmode == IEEE80211_M_STA)
3121 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3122 		}
3123 		break;
3124 	case IEEE80211_IOC_INACTIVITY:
3125 		if (ireq->i_val)
3126 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3127 		else
3128 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3129 		break;
3130 	case IEEE80211_IOC_APPIE:
3131 		error = ieee80211_ioctl_setappie(vap, ireq);
3132 		break;
3133 	case IEEE80211_IOC_WPS:
3134 		if (ireq->i_val) {
3135 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3136 				return EOPNOTSUPP;
3137 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3138 		} else
3139 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3140 		break;
3141 	case IEEE80211_IOC_TSN:
3142 		if (ireq->i_val) {
3143 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3144 				return EOPNOTSUPP;
3145 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3146 		} else
3147 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3148 		break;
3149 	case IEEE80211_IOC_CHANSWITCH:
3150 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3151 		break;
3152 	case IEEE80211_IOC_DFS:
3153 		if (ireq->i_val) {
3154 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3155 				return EOPNOTSUPP;
3156 			/* NB: DFS requires 11h support */
3157 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3158 				return EINVAL;
3159 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3160 		} else
3161 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3162 		break;
3163 	case IEEE80211_IOC_DOTD:
3164 		if (ireq->i_val)
3165 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3166 		else
3167 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3168 		if (vap->iv_opmode == IEEE80211_M_STA)
3169 			error = ENETRESET;
3170 		break;
3171 	case IEEE80211_IOC_HTPROTMODE:
3172 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3173 			return EINVAL;
3174 		ic->ic_htprotmode = ireq->i_val ?
3175 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3176 		/* NB: if not operating in 11n this can wait */
3177 		if (isvapht(vap))
3178 			error = ERESTART;
3179 		break;
3180 	case IEEE80211_IOC_STA_VLAN:
3181 		error = ieee80211_ioctl_setstavlan(vap, ireq);
3182 		break;
3183 	case IEEE80211_IOC_SMPS:
3184 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3185 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3186 			return EINVAL;
3187 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3188 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3189 			return EOPNOTSUPP;
3190 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3191 			ireq->i_val;
3192 		/* NB: if not operating in 11n this can wait */
3193 		if (isvapht(vap))
3194 			error = ERESTART;
3195 		break;
3196 	case IEEE80211_IOC_RIFS:
3197 		if (ireq->i_val != 0) {
3198 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3199 				return EOPNOTSUPP;
3200 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
3201 		} else
3202 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
3203 		/* NB: if not operating in 11n this can wait */
3204 		if (isvapht(vap))
3205 			error = ERESTART;
3206 		break;
3207 	default:
3208 		error = ieee80211_ioctl_setdefault(vap, ireq);
3209 		break;
3210 	}
3211 	/*
3212 	 * The convention is that ENETRESET means an operation
3213 	 * requires a complete re-initialization of the device (e.g.
3214 	 * changing something that affects the association state).
3215 	 * ERESTART means the request may be handled with only a
3216 	 * reload of the hardware state.  We hand ERESTART requests
3217 	 * to the iv_reset callback so the driver can decide.  If
3218 	 * a device does not fillin iv_reset then it defaults to one
3219 	 * that returns ENETRESET.  Otherwise a driver may return
3220 	 * ENETRESET (in which case a full reset will be done) or
3221 	 * 0 to mean there's no need to do anything (e.g. when the
3222 	 * change has no effect on the driver/device).
3223 	 */
3224 	if (error == ERESTART)
3225 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3226 		    vap->iv_reset(vap, ireq->i_type) : 0;
3227 	if (error == ENETRESET) {
3228 		/* XXX need to re-think AUTO handling */
3229 		if (IS_UP_AUTO(vap))
3230 			ieee80211_init(vap);
3231 		error = 0;
3232 	}
3233 	return error;
3234 }
3235 
3236 /*
3237  * Rebuild the parent's multicast address list after an add/del
3238  * of a multicast address for a vap.  We have no way to tell
3239  * what happened above to optimize the work so we purge the entire
3240  * list and rebuild from scratch.  This is way expensive.
3241  * Note also the half-baked workaround for if_addmulti calling
3242  * back to the parent device; there's no way to insert mcast
3243  * entries quietly and/or cheaply.
3244  */
3245 static void
3246 ieee80211_ioctl_updatemulti(struct ieee80211com *ic)
3247 {
3248 	struct ifnet *parent = ic->ic_ifp;
3249 	struct ieee80211vap *vap;
3250 	void *ioctl;
3251 
3252 	IEEE80211_LOCK(ic);
3253 	if_delallmulti(parent);
3254 	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3255 	parent->if_ioctl = NULL;
3256 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3257 		struct ifnet *ifp = vap->iv_ifp;
3258 		struct ifmultiaddr *ifma;
3259 
3260 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3261 			if (ifma->ifma_addr->sa_family != AF_LINK)
3262 				continue;
3263 			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3264 		}
3265 	}
3266 	parent->if_ioctl = ioctl;
3267 	ieee80211_runtask(ic, &ic->ic_mcast_task);
3268 	IEEE80211_UNLOCK(ic);
3269 }
3270 
3271 int
3272 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3273 {
3274 	struct ieee80211vap *vap = ifp->if_softc;
3275 	struct ieee80211com *ic = vap->iv_ic;
3276 	int error = 0;
3277 	struct ifreq *ifr;
3278 	struct ifaddr *ifa;			/* XXX */
3279 
3280 	switch (cmd) {
3281 	case SIOCSIFFLAGS:
3282 		IEEE80211_LOCK(ic);
3283 		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3284 		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3285 		if (ifp->if_flags & IFF_UP) {
3286 			/*
3287 			 * Bring ourself up unless we're already operational.
3288 			 * If we're the first vap and the parent is not up
3289 			 * then it will automatically be brought up as a
3290 			 * side-effect of bringing ourself up.
3291 			 */
3292 			if (vap->iv_state == IEEE80211_S_INIT)
3293 				ieee80211_start_locked(vap);
3294 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3295 			/*
3296 			 * Stop ourself.  If we are the last vap to be
3297 			 * marked down the parent will also be taken down.
3298 			 */
3299 			ieee80211_stop_locked(vap);
3300 		}
3301 		IEEE80211_UNLOCK(ic);
3302 		/* Wait for parent ioctl handler if it was queued */
3303 		ieee80211_waitfor_parent(ic);
3304 		break;
3305 	case SIOCADDMULTI:
3306 	case SIOCDELMULTI:
3307 		ieee80211_ioctl_updatemulti(ic);
3308 		break;
3309 	case SIOCSIFMEDIA:
3310 	case SIOCGIFMEDIA:
3311 		ifr = (struct ifreq *)data;
3312 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3313 		break;
3314 	case SIOCG80211:
3315 		error = ieee80211_ioctl_get80211(vap, cmd,
3316 				(struct ieee80211req *) data);
3317 		break;
3318 	case SIOCS80211:
3319 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3320 		if (error == 0)
3321 			error = ieee80211_ioctl_set80211(vap, cmd,
3322 					(struct ieee80211req *) data);
3323 		break;
3324 	case SIOCG80211STATS:
3325 		ifr = (struct ifreq *)data;
3326 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3327 		break;
3328 	case SIOCSIFMTU:
3329 		ifr = (struct ifreq *)data;
3330 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3331 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3332 			error = EINVAL;
3333 		else
3334 			ifp->if_mtu = ifr->ifr_mtu;
3335 		break;
3336 	case SIOCSIFADDR:
3337 		/*
3338 		 * XXX Handle this directly so we can supress if_init calls.
3339 		 * XXX This should be done in ether_ioctl but for the moment
3340 		 * XXX there are too many other parts of the system that
3341 		 * XXX set IFF_UP and so supress if_init being called when
3342 		 * XXX it should be.
3343 		 */
3344 		ifa = (struct ifaddr *) data;
3345 		switch (ifa->ifa_addr->sa_family) {
3346 #ifdef INET
3347 		case AF_INET:
3348 			if ((ifp->if_flags & IFF_UP) == 0) {
3349 				ifp->if_flags |= IFF_UP;
3350 				ifp->if_init(ifp->if_softc);
3351 			}
3352 			arp_ifinit(ifp, ifa);
3353 			break;
3354 #endif
3355 #ifdef IPX
3356 		/*
3357 		 * XXX - This code is probably wrong,
3358 		 *	 but has been copied many times.
3359 		 */
3360 		case AF_IPX: {
3361 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3362 
3363 			if (ipx_nullhost(*ina))
3364 				ina->x_host = *(union ipx_host *)
3365 				    IF_LLADDR(ifp);
3366 			else
3367 				bcopy((caddr_t) ina->x_host.c_host,
3368 				      (caddr_t) IF_LLADDR(ifp),
3369 				      ETHER_ADDR_LEN);
3370 			/* fall thru... */
3371 		}
3372 #endif
3373 		default:
3374 			if ((ifp->if_flags & IFF_UP) == 0) {
3375 				ifp->if_flags |= IFF_UP;
3376 				ifp->if_init(ifp->if_softc);
3377 			}
3378 			break;
3379 		}
3380 		break;
3381 	/* Pass NDIS ioctls up to the driver */
3382 	case SIOCGDRVSPEC:
3383 	case SIOCSDRVSPEC:
3384 	case SIOCGPRIVATE_0: {
3385 		struct ifnet *parent = vap->iv_ic->ic_ifp;
3386 		error = parent->if_ioctl(parent, cmd, data);
3387 		break;
3388 	}
3389 	default:
3390 		error = ether_ioctl(ifp, cmd, data);
3391 		break;
3392 	}
3393 	return error;
3394 }
3395