xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
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 	uint32_t 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 size_t 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 		uint32_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, size_t 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 size_t 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_QUIET:
976 		ireq->i_val = vap->iv_quiet;
977 		break;
978 	case IEEE80211_IOC_QUIET_COUNT:
979 		ireq->i_val = vap->iv_quiet_count;
980 		break;
981 	case IEEE80211_IOC_QUIET_PERIOD:
982 		ireq->i_val = vap->iv_quiet_period;
983 		break;
984 	case IEEE80211_IOC_QUIET_DUR:
985 		ireq->i_val = vap->iv_quiet_duration;
986 		break;
987 	case IEEE80211_IOC_QUIET_OFFSET:
988 		ireq->i_val = vap->iv_quiet_offset;
989 		break;
990 	case IEEE80211_IOC_BGSCAN:
991 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
992 		break;
993 	case IEEE80211_IOC_BGSCAN_IDLE:
994 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
995 		break;
996 	case IEEE80211_IOC_BGSCAN_INTERVAL:
997 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
998 		break;
999 	case IEEE80211_IOC_SCANVALID:
1000 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
1001 		break;
1002 	case IEEE80211_IOC_FRAGTHRESHOLD:
1003 		ireq->i_val = vap->iv_fragthreshold;
1004 		break;
1005 	case IEEE80211_IOC_MACCMD:
1006 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
1007 		break;
1008 	case IEEE80211_IOC_BURST:
1009 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
1010 		break;
1011 	case IEEE80211_IOC_BMISSTHRESHOLD:
1012 		ireq->i_val = vap->iv_bmissthreshold;
1013 		break;
1014 	case IEEE80211_IOC_CURCHAN:
1015 		error = ieee80211_ioctl_getcurchan(vap, ireq);
1016 		break;
1017 	case IEEE80211_IOC_SHORTGI:
1018 		ireq->i_val = 0;
1019 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1020 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
1021 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1022 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
1023 		break;
1024 	case IEEE80211_IOC_AMPDU:
1025 		ireq->i_val = 0;
1026 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
1027 			ireq->i_val |= 1;
1028 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
1029 			ireq->i_val |= 2;
1030 		break;
1031 	case IEEE80211_IOC_AMPDU_LIMIT:
1032 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1033 			ireq->i_val = vap->iv_ampdu_rxmax;
1034 		else if (vap->iv_state == IEEE80211_S_RUN)
1035 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1036 			    IEEE80211_HTCAP_MAXRXAMPDU);
1037 		else
1038 			ireq->i_val = vap->iv_ampdu_limit;
1039 		break;
1040 	case IEEE80211_IOC_AMPDU_DENSITY:
1041 		if (vap->iv_opmode == IEEE80211_M_STA &&
1042 		    vap->iv_state == IEEE80211_S_RUN)
1043 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1044 			    IEEE80211_HTCAP_MPDUDENSITY);
1045 		else
1046 			ireq->i_val = vap->iv_ampdu_density;
1047 		break;
1048 	case IEEE80211_IOC_AMSDU:
1049 		ireq->i_val = 0;
1050 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
1051 			ireq->i_val |= 1;
1052 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
1053 			ireq->i_val |= 2;
1054 		break;
1055 	case IEEE80211_IOC_AMSDU_LIMIT:
1056 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1057 		break;
1058 	case IEEE80211_IOC_PUREN:
1059 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
1060 		break;
1061 	case IEEE80211_IOC_DOTH:
1062 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1063 		break;
1064 	case IEEE80211_IOC_REGDOMAIN:
1065 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1066 		break;
1067 	case IEEE80211_IOC_ROAM:
1068 		error = ieee80211_ioctl_getroam(vap, ireq);
1069 		break;
1070 	case IEEE80211_IOC_TXPARAMS:
1071 		error = ieee80211_ioctl_gettxparams(vap, ireq);
1072 		break;
1073 	case IEEE80211_IOC_HTCOMPAT:
1074 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1075 		break;
1076 	case IEEE80211_IOC_DWDS:
1077 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1078 		break;
1079 	case IEEE80211_IOC_INACTIVITY:
1080 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1081 		break;
1082 	case IEEE80211_IOC_APPIE:
1083 		error = ieee80211_ioctl_getappie(vap, ireq);
1084 		break;
1085 	case IEEE80211_IOC_WPS:
1086 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1087 		break;
1088 	case IEEE80211_IOC_TSN:
1089 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1090 		break;
1091 	case IEEE80211_IOC_DFS:
1092 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1093 		break;
1094 	case IEEE80211_IOC_DOTD:
1095 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1096 		break;
1097 	case IEEE80211_IOC_DEVCAPS:
1098 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1099 		break;
1100 	case IEEE80211_IOC_HTPROTMODE:
1101 		ireq->i_val = ic->ic_htprotmode;
1102 		break;
1103 	case IEEE80211_IOC_HTCONF:
1104 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
1105 			ireq->i_val = 1;
1106 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
1107 				ireq->i_val |= 2;
1108 		} else
1109 			ireq->i_val = 0;
1110 		break;
1111 	case IEEE80211_IOC_STA_VLAN:
1112 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1113 		break;
1114 	case IEEE80211_IOC_SMPS:
1115 		if (vap->iv_opmode == IEEE80211_M_STA &&
1116 		    vap->iv_state == IEEE80211_S_RUN) {
1117 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1118 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1119 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1120 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1121 			else
1122 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1123 		} else
1124 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1125 		break;
1126 	case IEEE80211_IOC_RIFS:
1127 		if (vap->iv_opmode == IEEE80211_M_STA &&
1128 		    vap->iv_state == IEEE80211_S_RUN)
1129 			ireq->i_val =
1130 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1131 		else
1132 			ireq->i_val =
1133 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
1134 		break;
1135 	default:
1136 		error = ieee80211_ioctl_getdefault(vap, ireq);
1137 		break;
1138 	}
1139 	return error;
1140 #undef MS
1141 }
1142 
1143 static __noinline int
1144 ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1145 {
1146 	struct ieee80211req_key ik;
1147 	struct ieee80211_node *ni;
1148 	struct ieee80211_key *wk;
1149 	uint16_t kid;
1150 	int error, i;
1151 
1152 	if (ireq->i_len != sizeof(ik))
1153 		return EINVAL;
1154 	error = copyin(ireq->i_data, &ik, sizeof(ik));
1155 	if (error)
1156 		return error;
1157 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1158 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1159 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1160 		return E2BIG;
1161 	kid = ik.ik_keyix;
1162 	if (kid == IEEE80211_KEYIX_NONE) {
1163 		/* XXX unicast keys currently must be tx/rx */
1164 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1165 			return EINVAL;
1166 		if (vap->iv_opmode == IEEE80211_M_STA) {
1167 			ni = ieee80211_ref_node(vap->iv_bss);
1168 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1169 				ieee80211_free_node(ni);
1170 				return EADDRNOTAVAIL;
1171 			}
1172 		} else {
1173 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1174 				ik.ik_macaddr);
1175 			if (ni == NULL)
1176 				return ENOENT;
1177 		}
1178 		wk = &ni->ni_ucastkey;
1179 	} else {
1180 		if (kid >= IEEE80211_WEP_NKID)
1181 			return EINVAL;
1182 		wk = &vap->iv_nw_keys[kid];
1183 		/*
1184 		 * Global slots start off w/o any assigned key index.
1185 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1186 		 */
1187 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1188 			wk->wk_keyix = kid;
1189 		ni = NULL;
1190 	}
1191 	error = 0;
1192 	ieee80211_key_update_begin(vap);
1193 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1194 		wk->wk_keylen = ik.ik_keylen;
1195 		/* NB: MIC presence is implied by cipher type */
1196 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1197 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1198 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1199 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1200 		wk->wk_keytsc = 0;			/* new key, reset */
1201 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1202 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1203 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1204 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1205 		if (!ieee80211_crypto_setkey(vap, wk))
1206 			error = EIO;
1207 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1208 			vap->iv_def_txkey = kid;
1209 	} else
1210 		error = ENXIO;
1211 	ieee80211_key_update_end(vap);
1212 	if (ni != NULL)
1213 		ieee80211_free_node(ni);
1214 	return error;
1215 }
1216 
1217 static __noinline int
1218 ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1219 {
1220 	struct ieee80211req_del_key dk;
1221 	int kid, error;
1222 
1223 	if (ireq->i_len != sizeof(dk))
1224 		return EINVAL;
1225 	error = copyin(ireq->i_data, &dk, sizeof(dk));
1226 	if (error)
1227 		return error;
1228 	kid = dk.idk_keyix;
1229 	/* XXX uint8_t -> uint16_t */
1230 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1231 		struct ieee80211_node *ni;
1232 
1233 		if (vap->iv_opmode == IEEE80211_M_STA) {
1234 			ni = ieee80211_ref_node(vap->iv_bss);
1235 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1236 				ieee80211_free_node(ni);
1237 				return EADDRNOTAVAIL;
1238 			}
1239 		} else {
1240 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1241 				dk.idk_macaddr);
1242 			if (ni == NULL)
1243 				return ENOENT;
1244 		}
1245 		/* XXX error return */
1246 		ieee80211_node_delucastkey(ni);
1247 		ieee80211_free_node(ni);
1248 	} else {
1249 		if (kid >= IEEE80211_WEP_NKID)
1250 			return EINVAL;
1251 		/* XXX error return */
1252 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1253 	}
1254 	return 0;
1255 }
1256 
1257 struct mlmeop {
1258 	struct ieee80211vap *vap;
1259 	int	op;
1260 	int	reason;
1261 };
1262 
1263 static void
1264 mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1265 	int op, int reason)
1266 {
1267 #ifdef IEEE80211_DEBUG
1268 	static const struct {
1269 		int mask;
1270 		const char *opstr;
1271 	} ops[] = {
1272 		{ 0, "op#0" },
1273 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1274 		  IEEE80211_MSG_ASSOC, "assoc" },
1275 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1276 		  IEEE80211_MSG_ASSOC, "disassoc" },
1277 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1278 		  IEEE80211_MSG_AUTH, "deauth" },
1279 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1280 		  IEEE80211_MSG_AUTH, "authorize" },
1281 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1282 		  IEEE80211_MSG_AUTH, "unauthorize" },
1283 	};
1284 
1285 	if (op == IEEE80211_MLME_AUTH) {
1286 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1287 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1288 		    "station authenticate %s via MLME (reason %d)",
1289 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1290 		    reason);
1291 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1292 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1293 		    "unknown MLME request %d (reason %d)", op, reason);
1294 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1295 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1296 		    "station %s via MLME", ops[op].opstr);
1297 	} else {
1298 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1299 		    "station %s via MLME (reason %d)", ops[op].opstr, reason);
1300 	}
1301 #endif /* IEEE80211_DEBUG */
1302 }
1303 
1304 static void
1305 domlme(void *arg, struct ieee80211_node *ni)
1306 {
1307 	struct mlmeop *mop = arg;
1308 	struct ieee80211vap *vap = ni->ni_vap;
1309 
1310 	if (vap != mop->vap)
1311 		return;
1312 	/*
1313 	 * NB: if ni_associd is zero then the node is already cleaned
1314 	 * up and we don't need to do this (we're safely holding a
1315 	 * reference but should otherwise not modify it's state).
1316 	 */
1317 	if (ni->ni_associd == 0)
1318 		return;
1319 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1320 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1321 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1322 		    mop->reason);
1323 	} else {
1324 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1325 		    mop->reason);
1326 	}
1327 	ieee80211_node_leave(ni);
1328 }
1329 
1330 static int
1331 setmlme_dropsta(struct ieee80211vap *vap,
1332 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1333 {
1334 	struct ieee80211com *ic = vap->iv_ic;
1335 	struct ieee80211_node_table *nt = &ic->ic_sta;
1336 	struct ieee80211_node *ni;
1337 	int error = 0;
1338 
1339 	/* NB: the broadcast address means do 'em all */
1340 	if (!IEEE80211_ADDR_EQ(mac, ic->ic_ifp->if_broadcastaddr)) {
1341 		IEEE80211_NODE_LOCK(nt);
1342 		ni = ieee80211_find_node_locked(nt, mac);
1343 		if (ni != NULL) {
1344 			domlme(mlmeop, ni);
1345 			ieee80211_free_node(ni);
1346 		} else
1347 			error = ENOENT;
1348 		IEEE80211_NODE_UNLOCK(nt);
1349 	} else {
1350 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1351 	}
1352 	return error;
1353 }
1354 
1355 static __noinline int
1356 setmlme_common(struct ieee80211vap *vap, int op,
1357 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1358 {
1359 	struct ieee80211com *ic = vap->iv_ic;
1360 	struct ieee80211_node_table *nt = &ic->ic_sta;
1361 	struct ieee80211_node *ni;
1362 	struct mlmeop mlmeop;
1363 	int error;
1364 
1365 	error = 0;
1366 	switch (op) {
1367 	case IEEE80211_MLME_DISASSOC:
1368 	case IEEE80211_MLME_DEAUTH:
1369 		switch (vap->iv_opmode) {
1370 		case IEEE80211_M_STA:
1371 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1372 			/* XXX not quite right */
1373 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1374 			break;
1375 		case IEEE80211_M_HOSTAP:
1376 			mlmeop.vap = vap;
1377 			mlmeop.op = op;
1378 			mlmeop.reason = reason;
1379 			error = setmlme_dropsta(vap, mac, &mlmeop);
1380 			break;
1381 		case IEEE80211_M_WDS:
1382 			/* XXX user app should send raw frame? */
1383 			if (op != IEEE80211_MLME_DEAUTH) {
1384 				error = EINVAL;
1385 				break;
1386 			}
1387 #if 0
1388 			/* XXX accept any address, simplifies user code */
1389 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1390 				error = EINVAL;
1391 				break;
1392 			}
1393 #endif
1394 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1395 			ni = ieee80211_ref_node(vap->iv_bss);
1396 			IEEE80211_SEND_MGMT(ni,
1397 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1398 			ieee80211_free_node(ni);
1399 			break;
1400 		default:
1401 			error = EINVAL;
1402 			break;
1403 		}
1404 		break;
1405 	case IEEE80211_MLME_AUTHORIZE:
1406 	case IEEE80211_MLME_UNAUTHORIZE:
1407 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1408 		    vap->iv_opmode != IEEE80211_M_WDS) {
1409 			error = EINVAL;
1410 			break;
1411 		}
1412 		IEEE80211_NODE_LOCK(nt);
1413 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1414 		if (ni != NULL) {
1415 			mlmedebug(vap, mac, op, reason);
1416 			if (op == IEEE80211_MLME_AUTHORIZE)
1417 				ieee80211_node_authorize(ni);
1418 			else
1419 				ieee80211_node_unauthorize(ni);
1420 			ieee80211_free_node(ni);
1421 		} else
1422 			error = ENOENT;
1423 		IEEE80211_NODE_UNLOCK(nt);
1424 		break;
1425 	case IEEE80211_MLME_AUTH:
1426 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1427 			error = EINVAL;
1428 			break;
1429 		}
1430 		IEEE80211_NODE_LOCK(nt);
1431 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1432 		if (ni != NULL) {
1433 			mlmedebug(vap, mac, op, reason);
1434 			if (reason == IEEE80211_STATUS_SUCCESS) {
1435 				IEEE80211_SEND_MGMT(ni,
1436 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1437 				/*
1438 				 * For shared key auth, just continue the
1439 				 * exchange.  Otherwise when 802.1x is not in
1440 				 * use mark the port authorized at this point
1441 				 * so traffic can flow.
1442 				 */
1443 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1444 				    ni->ni_challenge == NULL)
1445 				      ieee80211_node_authorize(ni);
1446 			} else {
1447 				vap->iv_stats.is_rx_acl++;
1448 				ieee80211_send_error(ni, ni->ni_macaddr,
1449 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1450 				ieee80211_node_leave(ni);
1451 			}
1452 			ieee80211_free_node(ni);
1453 		} else
1454 			error = ENOENT;
1455 		IEEE80211_NODE_UNLOCK(nt);
1456 		break;
1457 	default:
1458 		error = EINVAL;
1459 		break;
1460 	}
1461 	return error;
1462 }
1463 
1464 struct scanlookup {
1465 	const uint8_t *mac;
1466 	int esslen;
1467 	const uint8_t *essid;
1468 	const struct ieee80211_scan_entry *se;
1469 };
1470 
1471 /*
1472  * Match mac address and any ssid.
1473  */
1474 static void
1475 mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1476 {
1477 	struct scanlookup *look = arg;
1478 
1479 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1480 		return;
1481 	if (look->esslen != 0) {
1482 		if (se->se_ssid[1] != look->esslen)
1483 			return;
1484 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1485 			return;
1486 	}
1487 	look->se = se;
1488 }
1489 
1490 static __noinline int
1491 setmlme_assoc_sta(struct ieee80211vap *vap,
1492 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1493 	const uint8_t ssid[IEEE80211_NWID_LEN])
1494 {
1495 	struct scanlookup lookup;
1496 
1497 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1498 	    ("expected opmode STA not %s",
1499 	    ieee80211_opmode_name[vap->iv_opmode]));
1500 
1501 	/* NB: this is racey if roaming is !manual */
1502 	lookup.se = NULL;
1503 	lookup.mac = mac;
1504 	lookup.esslen = ssid_len;
1505 	lookup.essid = ssid;
1506 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1507 	if (lookup.se == NULL)
1508 		return ENOENT;
1509 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1510 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1511 		return EIO;		/* XXX unique but could be better */
1512 	return 0;
1513 }
1514 
1515 static __noinline int
1516 setmlme_assoc_adhoc(struct ieee80211vap *vap,
1517 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1518 	const uint8_t ssid[IEEE80211_NWID_LEN])
1519 {
1520 	struct ieee80211_scan_req sr;
1521 
1522 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1523 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1524 	    ("expected opmode IBSS or AHDEMO not %s",
1525 	    ieee80211_opmode_name[vap->iv_opmode]));
1526 
1527 	if (ssid_len == 0)
1528 		return EINVAL;
1529 
1530 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1531 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1532 	vap->iv_des_ssid[0].len = ssid_len;
1533 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1534 	vap->iv_des_nssid = 1;
1535 
1536 	memset(&sr, 0, sizeof(sr));
1537 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
1538 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1539 	memcpy(sr.sr_ssid[0].ssid, ssid, ssid_len);
1540 	sr.sr_ssid[0].len = ssid_len;
1541 	sr.sr_nssid = 1;
1542 
1543 	return ieee80211_scanreq(vap, &sr);
1544 }
1545 
1546 static __noinline int
1547 ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1548 {
1549 	struct ieee80211req_mlme mlme;
1550 	int error;
1551 
1552 	if (ireq->i_len != sizeof(mlme))
1553 		return EINVAL;
1554 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1555 	if (error)
1556 		return error;
1557 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1558 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1559 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1560 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1561 	else if (mlme.im_op == IEEE80211_MLME_ASSOC)
1562 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1563 		    mlme.im_ssid_len, mlme.im_ssid);
1564 	else
1565 		return setmlme_common(vap, mlme.im_op,
1566 		    mlme.im_macaddr, mlme.im_reason);
1567 }
1568 
1569 static __noinline int
1570 ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1571 {
1572 	uint8_t mac[IEEE80211_ADDR_LEN];
1573 	const struct ieee80211_aclator *acl = vap->iv_acl;
1574 	int error;
1575 
1576 	if (ireq->i_len != sizeof(mac))
1577 		return EINVAL;
1578 	error = copyin(ireq->i_data, mac, ireq->i_len);
1579 	if (error)
1580 		return error;
1581 	if (acl == NULL) {
1582 		acl = ieee80211_aclator_get("mac");
1583 		if (acl == NULL || !acl->iac_attach(vap))
1584 			return EINVAL;
1585 		vap->iv_acl = acl;
1586 	}
1587 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1588 		acl->iac_add(vap, mac);
1589 	else
1590 		acl->iac_remove(vap, mac);
1591 	return 0;
1592 }
1593 
1594 static __noinline int
1595 ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1596 {
1597 	const struct ieee80211_aclator *acl = vap->iv_acl;
1598 
1599 	switch (ireq->i_val) {
1600 	case IEEE80211_MACCMD_POLICY_OPEN:
1601 	case IEEE80211_MACCMD_POLICY_ALLOW:
1602 	case IEEE80211_MACCMD_POLICY_DENY:
1603 	case IEEE80211_MACCMD_POLICY_RADIUS:
1604 		if (acl == NULL) {
1605 			acl = ieee80211_aclator_get("mac");
1606 			if (acl == NULL || !acl->iac_attach(vap))
1607 				return EINVAL;
1608 			vap->iv_acl = acl;
1609 		}
1610 		acl->iac_setpolicy(vap, ireq->i_val);
1611 		break;
1612 	case IEEE80211_MACCMD_FLUSH:
1613 		if (acl != NULL)
1614 			acl->iac_flush(vap);
1615 		/* NB: silently ignore when not in use */
1616 		break;
1617 	case IEEE80211_MACCMD_DETACH:
1618 		if (acl != NULL) {
1619 			vap->iv_acl = NULL;
1620 			acl->iac_detach(vap);
1621 		}
1622 		break;
1623 	default:
1624 		if (acl == NULL)
1625 			return EINVAL;
1626 		else
1627 			return acl->iac_setioctl(vap, ireq);
1628 	}
1629 	return 0;
1630 }
1631 
1632 static __noinline int
1633 ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1634 {
1635 	struct ieee80211com *ic = vap->iv_ic;
1636 	uint8_t *chanlist, *list;
1637 	int i, nchan, maxchan, error;
1638 
1639 	if (ireq->i_len > sizeof(ic->ic_chan_active))
1640 		ireq->i_len = sizeof(ic->ic_chan_active);
1641 	list = malloc(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1642 	    M_NOWAIT | M_ZERO);
1643 	if (list == NULL)
1644 		return ENOMEM;
1645 	error = copyin(ireq->i_data, list, ireq->i_len);
1646 	if (error) {
1647 		free(list, M_TEMP);
1648 		return error;
1649 	}
1650 	nchan = 0;
1651 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
1652 	maxchan = ireq->i_len * NBBY;
1653 	for (i = 0; i < ic->ic_nchans; i++) {
1654 		const struct ieee80211_channel *c = &ic->ic_channels[i];
1655 		/*
1656 		 * Calculate the intersection of the user list and the
1657 		 * available channels so users can do things like specify
1658 		 * 1-255 to get all available channels.
1659 		 */
1660 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
1661 			setbit(chanlist, c->ic_ieee);
1662 			nchan++;
1663 		}
1664 	}
1665 	if (nchan == 0) {
1666 		free(list, M_TEMP);
1667 		return EINVAL;
1668 	}
1669 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1670 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1671 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1672 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1673 	ieee80211_scan_flush(vap);
1674 	free(list, M_TEMP);
1675 	return ENETRESET;
1676 }
1677 
1678 static __noinline int
1679 ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1680 {
1681 	struct ieee80211_node *ni;
1682 	uint8_t macaddr[IEEE80211_ADDR_LEN];
1683 	int error;
1684 
1685 	/*
1686 	 * NB: we could copyin ieee80211req_sta_stats so apps
1687 	 *     could make selective changes but that's overkill;
1688 	 *     just clear all stats for now.
1689 	 */
1690 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1691 		return EINVAL;
1692 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1693 	if (error != 0)
1694 		return error;
1695 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1696 	if (ni == NULL)
1697 		return ENOENT;
1698 	/* XXX require ni_vap == vap? */
1699 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1700 	ieee80211_free_node(ni);
1701 	return 0;
1702 }
1703 
1704 static __noinline int
1705 ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1706 {
1707 	struct ieee80211_node *ni;
1708 	struct ieee80211req_sta_txpow txpow;
1709 	int error;
1710 
1711 	if (ireq->i_len != sizeof(txpow))
1712 		return EINVAL;
1713 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1714 	if (error != 0)
1715 		return error;
1716 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1717 	if (ni == NULL)
1718 		return ENOENT;
1719 	ni->ni_txpower = txpow.it_txpow;
1720 	ieee80211_free_node(ni);
1721 	return error;
1722 }
1723 
1724 static __noinline int
1725 ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1726 {
1727 	struct ieee80211com *ic = vap->iv_ic;
1728 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1729 	struct wmeParams *wmep, *chanp;
1730 	int isbss, ac;
1731 
1732 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1733 		return EOPNOTSUPP;
1734 
1735 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1736 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1737 	if (ac >= WME_NUM_AC)
1738 		ac = WME_AC_BE;
1739 	if (isbss) {
1740 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1741 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1742 	} else {
1743 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1744 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1745 	}
1746 	switch (ireq->i_type) {
1747 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1748 		if (isbss) {
1749 			wmep->wmep_logcwmin = ireq->i_val;
1750 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1751 				chanp->wmep_logcwmin = ireq->i_val;
1752 		} else {
1753 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
1754 				ireq->i_val;
1755 		}
1756 		break;
1757 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1758 		if (isbss) {
1759 			wmep->wmep_logcwmax = ireq->i_val;
1760 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1761 				chanp->wmep_logcwmax = ireq->i_val;
1762 		} else {
1763 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
1764 				ireq->i_val;
1765 		}
1766 		break;
1767 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1768 		if (isbss) {
1769 			wmep->wmep_aifsn = ireq->i_val;
1770 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1771 				chanp->wmep_aifsn = ireq->i_val;
1772 		} else {
1773 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
1774 		}
1775 		break;
1776 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1777 		if (isbss) {
1778 			wmep->wmep_txopLimit = ireq->i_val;
1779 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1780 				chanp->wmep_txopLimit = ireq->i_val;
1781 		} else {
1782 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
1783 				ireq->i_val;
1784 		}
1785 		break;
1786 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1787 		wmep->wmep_acm = ireq->i_val;
1788 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
1789 			chanp->wmep_acm = ireq->i_val;
1790 		break;
1791 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1792 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1793 			(ireq->i_val) == 0;
1794 		break;
1795 	}
1796 	ieee80211_wme_updateparams(vap);
1797 	return 0;
1798 }
1799 
1800 static int
1801 find11gchannel(struct ieee80211com *ic, int start, int freq)
1802 {
1803 	const struct ieee80211_channel *c;
1804 	int i;
1805 
1806 	for (i = start+1; i < ic->ic_nchans; i++) {
1807 		c = &ic->ic_channels[i];
1808 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1809 			return 1;
1810 	}
1811 	/* NB: should not be needed but in case things are mis-sorted */
1812 	for (i = 0; i < start; i++) {
1813 		c = &ic->ic_channels[i];
1814 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1815 			return 1;
1816 	}
1817 	return 0;
1818 }
1819 
1820 static struct ieee80211_channel *
1821 findchannel(struct ieee80211com *ic, int ieee, int mode)
1822 {
1823 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1824 	    [IEEE80211_MODE_AUTO]	= 0,
1825 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1826 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1827 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1828 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1829 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1830 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1831 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
1832 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
1833 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
1834 	    /* NB: handled specially below */
1835 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1836 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
1837 	};
1838 	u_int modeflags;
1839 	int i;
1840 
1841 	modeflags = chanflags[mode];
1842 	for (i = 0; i < ic->ic_nchans; i++) {
1843 		struct ieee80211_channel *c = &ic->ic_channels[i];
1844 
1845 		if (c->ic_ieee != ieee)
1846 			continue;
1847 		if (mode == IEEE80211_MODE_AUTO) {
1848 			/* ignore turbo channels for autoselect */
1849 			if (IEEE80211_IS_CHAN_TURBO(c))
1850 				continue;
1851 			/*
1852 			 * XXX special-case 11b/g channels so we
1853 			 *     always select the g channel if both
1854 			 *     are present.
1855 			 * XXX prefer HT to non-HT?
1856 			 */
1857 			if (!IEEE80211_IS_CHAN_B(c) ||
1858 			    !find11gchannel(ic, i, c->ic_freq))
1859 				return c;
1860 		} else {
1861 			/* must check HT specially */
1862 			if ((mode == IEEE80211_MODE_11NA ||
1863 			    mode == IEEE80211_MODE_11NG) &&
1864 			    !IEEE80211_IS_CHAN_HT(c))
1865 				continue;
1866 			if ((c->ic_flags & modeflags) == modeflags)
1867 				return c;
1868 		}
1869 	}
1870 	return NULL;
1871 }
1872 
1873 /*
1874  * Check the specified against any desired mode (aka netband).
1875  * This is only used (presently) when operating in hostap mode
1876  * to enforce consistency.
1877  */
1878 static int
1879 check_mode_consistency(const struct ieee80211_channel *c, int mode)
1880 {
1881 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1882 
1883 	switch (mode) {
1884 	case IEEE80211_MODE_11B:
1885 		return (IEEE80211_IS_CHAN_B(c));
1886 	case IEEE80211_MODE_11G:
1887 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1888 	case IEEE80211_MODE_11A:
1889 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1890 	case IEEE80211_MODE_STURBO_A:
1891 		return (IEEE80211_IS_CHAN_STURBO(c));
1892 	case IEEE80211_MODE_11NA:
1893 		return (IEEE80211_IS_CHAN_HTA(c));
1894 	case IEEE80211_MODE_11NG:
1895 		return (IEEE80211_IS_CHAN_HTG(c));
1896 	}
1897 	return 1;
1898 
1899 }
1900 
1901 /*
1902  * Common code to set the current channel.  If the device
1903  * is up and running this may result in an immediate channel
1904  * change or a kick of the state machine.
1905  */
1906 static int
1907 setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1908 {
1909 	struct ieee80211com *ic = vap->iv_ic;
1910 	int error;
1911 
1912 	if (c != IEEE80211_CHAN_ANYC) {
1913 		if (IEEE80211_IS_CHAN_RADAR(c))
1914 			return EBUSY;	/* XXX better code? */
1915 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1916 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1917 				return EINVAL;
1918 			if (!check_mode_consistency(c, vap->iv_des_mode))
1919 				return EINVAL;
1920 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1921 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1922 				return EINVAL;
1923 		}
1924 		if (vap->iv_state == IEEE80211_S_RUN &&
1925 		    vap->iv_bss->ni_chan == c)
1926 			return 0;	/* NB: nothing to do */
1927 	}
1928 	vap->iv_des_chan = c;
1929 
1930 	error = 0;
1931 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1932 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1933 		/*
1934 		 * Monitor mode can switch directly.
1935 		 */
1936 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1937 			/* XXX need state machine for other vap's to follow */
1938 			ieee80211_setcurchan(ic, vap->iv_des_chan);
1939 			vap->iv_bss->ni_chan = ic->ic_curchan;
1940 		} else
1941 			ic->ic_curchan = vap->iv_des_chan;
1942 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1943 	} else {
1944 		/*
1945 		 * Need to go through the state machine in case we
1946 		 * need to reassociate or the like.  The state machine
1947 		 * will pickup the desired channel and avoid scanning.
1948 		 */
1949 		if (IS_UP_AUTO(vap))
1950 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1951 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1952 			/*
1953 			 * When not up+running and a real channel has
1954 			 * been specified fix the current channel so
1955 			 * there is immediate feedback; e.g. via ifconfig.
1956 			 */
1957 			ic->ic_curchan = vap->iv_des_chan;
1958 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1959 		}
1960 	}
1961 	return error;
1962 }
1963 
1964 /*
1965  * Old api for setting the current channel; this is
1966  * deprecated because channel numbers are ambiguous.
1967  */
1968 static __noinline int
1969 ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
1970 	const struct ieee80211req *ireq)
1971 {
1972 	struct ieee80211com *ic = vap->iv_ic;
1973 	struct ieee80211_channel *c;
1974 
1975 	/* XXX 0xffff overflows 16-bit signed */
1976 	if (ireq->i_val == 0 ||
1977 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
1978 		c = IEEE80211_CHAN_ANYC;
1979 	} else {
1980 		struct ieee80211_channel *c2;
1981 
1982 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
1983 		if (c == NULL) {
1984 			c = findchannel(ic, ireq->i_val,
1985 				IEEE80211_MODE_AUTO);
1986 			if (c == NULL)
1987 				return EINVAL;
1988 		}
1989 		/*
1990 		 * Fine tune channel selection based on desired mode:
1991 		 *   if 11b is requested, find the 11b version of any
1992 		 *      11g channel returned,
1993 		 *   if static turbo, find the turbo version of any
1994 		 *	11a channel return,
1995 		 *   if 11na is requested, find the ht version of any
1996 		 *      11a channel returned,
1997 		 *   if 11ng is requested, find the ht version of any
1998 		 *      11g channel returned,
1999 		 *   otherwise we should be ok with what we've got.
2000 		 */
2001 		switch (vap->iv_des_mode) {
2002 		case IEEE80211_MODE_11B:
2003 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2004 				c2 = findchannel(ic, ireq->i_val,
2005 					IEEE80211_MODE_11B);
2006 				/* NB: should not happen, =>'s 11g w/o 11b */
2007 				if (c2 != NULL)
2008 					c = c2;
2009 			}
2010 			break;
2011 		case IEEE80211_MODE_TURBO_A:
2012 			if (IEEE80211_IS_CHAN_A(c)) {
2013 				c2 = findchannel(ic, ireq->i_val,
2014 					IEEE80211_MODE_TURBO_A);
2015 				if (c2 != NULL)
2016 					c = c2;
2017 			}
2018 			break;
2019 		case IEEE80211_MODE_11NA:
2020 			if (IEEE80211_IS_CHAN_A(c)) {
2021 				c2 = findchannel(ic, ireq->i_val,
2022 					IEEE80211_MODE_11NA);
2023 				if (c2 != NULL)
2024 					c = c2;
2025 			}
2026 			break;
2027 		case IEEE80211_MODE_11NG:
2028 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2029 				c2 = findchannel(ic, ireq->i_val,
2030 					IEEE80211_MODE_11NG);
2031 				if (c2 != NULL)
2032 					c = c2;
2033 			}
2034 			break;
2035 		default:		/* NB: no static turboG */
2036 			break;
2037 		}
2038 	}
2039 	return setcurchan(vap, c);
2040 }
2041 
2042 /*
2043  * New/current api for setting the current channel; a complete
2044  * channel description is provide so there is no ambiguity in
2045  * identifying the channel.
2046  */
2047 static __noinline int
2048 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
2049 	const struct ieee80211req *ireq)
2050 {
2051 	struct ieee80211com *ic = vap->iv_ic;
2052 	struct ieee80211_channel chan, *c;
2053 	int error;
2054 
2055 	if (ireq->i_len != sizeof(chan))
2056 		return EINVAL;
2057 	error = copyin(ireq->i_data, &chan, sizeof(chan));
2058 	if (error != 0)
2059 		return error;
2060 	/* XXX 0xffff overflows 16-bit signed */
2061 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
2062 		c = IEEE80211_CHAN_ANYC;
2063 	} else {
2064 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
2065 		if (c == NULL)
2066 			return EINVAL;
2067 	}
2068 	return setcurchan(vap, c);
2069 }
2070 
2071 static __noinline int
2072 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2073 	const struct ieee80211req *ireq)
2074 {
2075 	struct ieee80211_regdomain_req *reg;
2076 	int nchans, error;
2077 
2078 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2079 	    sizeof(struct ieee80211_channel));
2080 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2081 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2082 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2083 		    ireq->i_len, nchans);
2084 		return EINVAL;
2085 	}
2086 	reg = (struct ieee80211_regdomain_req *)
2087 	    malloc(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, M_NOWAIT);
2088 	if (reg == NULL) {
2089 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2090 		    "%s: no memory, nchans %d\n", __func__, nchans);
2091 		return ENOMEM;
2092 	}
2093 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2094 	if (error == 0) {
2095 		/* NB: validate inline channel count against storage size */
2096 		if (reg->chaninfo.ic_nchans != nchans) {
2097 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2098 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2099 				reg->chaninfo.ic_nchans, nchans);
2100 			error = EINVAL;
2101 		} else
2102 			error = ieee80211_setregdomain(vap, reg);
2103 	}
2104 	free(reg, M_TEMP);
2105 
2106 	return (error == 0 ? ENETRESET : error);
2107 }
2108 
2109 static int
2110 ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2111 	const struct ieee80211req *ireq)
2112 {
2113 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2114 		return EINVAL;
2115 	/* XXX validate params */
2116 	/* XXX? ENETRESET to push to device? */
2117 	return copyin(ireq->i_data, vap->iv_roamparms,
2118 	    sizeof(vap->iv_roamparms));
2119 }
2120 
2121 static int
2122 checkrate(const struct ieee80211_rateset *rs, int rate)
2123 {
2124 	int i;
2125 
2126 	if (rate == IEEE80211_FIXED_RATE_NONE)
2127 		return 1;
2128 	for (i = 0; i < rs->rs_nrates; i++)
2129 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2130 			return 1;
2131 	return 0;
2132 }
2133 
2134 static int
2135 checkmcs(int mcs)
2136 {
2137 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2138 		return 1;
2139 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2140 		return 0;
2141 	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2142 }
2143 
2144 static __noinline int
2145 ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2146 	const struct ieee80211req *ireq)
2147 {
2148 	struct ieee80211com *ic = vap->iv_ic;
2149 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2150 	struct ieee80211_txparam *src, *dst;
2151 	const struct ieee80211_rateset *rs;
2152 	int error, mode, changed, is11n, nmodes;
2153 
2154 	/* NB: accept short requests for backwards compat */
2155 	if (ireq->i_len > sizeof(parms))
2156 		return EINVAL;
2157 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2158 	if (error != 0)
2159 		return error;
2160 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2161 	changed = 0;
2162 	/* validate parameters and check if anything changed */
2163 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2164 		if (isclr(ic->ic_modecaps, mode))
2165 			continue;
2166 		src = &parms.params[mode];
2167 		dst = &vap->iv_txparms[mode];
2168 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2169 		is11n = (mode == IEEE80211_MODE_11NA ||
2170 			 mode == IEEE80211_MODE_11NG);
2171 		if (src->ucastrate != dst->ucastrate) {
2172 			if (!checkrate(rs, src->ucastrate) &&
2173 			    (!is11n || !checkmcs(src->ucastrate)))
2174 				return EINVAL;
2175 			changed++;
2176 		}
2177 		if (src->mcastrate != dst->mcastrate) {
2178 			if (!checkrate(rs, src->mcastrate) &&
2179 			    (!is11n || !checkmcs(src->mcastrate)))
2180 				return EINVAL;
2181 			changed++;
2182 		}
2183 		if (src->mgmtrate != dst->mgmtrate) {
2184 			if (!checkrate(rs, src->mgmtrate) &&
2185 			    (!is11n || !checkmcs(src->mgmtrate)))
2186 				return EINVAL;
2187 			changed++;
2188 		}
2189 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2190 			changed++;
2191 	}
2192 	if (changed) {
2193 		/*
2194 		 * Copy new parameters in place and notify the
2195 		 * driver so it can push state to the device.
2196 		 */
2197 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2198 			if (isset(ic->ic_modecaps, mode))
2199 				vap->iv_txparms[mode] = parms.params[mode];
2200 		}
2201 		/* XXX could be more intelligent,
2202 		   e.g. don't reset if setting not being used */
2203 		return ENETRESET;
2204 	}
2205 	return 0;
2206 }
2207 
2208 /*
2209  * Application Information Element support.
2210  */
2211 static int
2212 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2213 {
2214 	struct ieee80211_appie *app = *aie;
2215 	struct ieee80211_appie *napp;
2216 	int error;
2217 
2218 	if (ireq->i_len == 0) {		/* delete any existing ie */
2219 		if (app != NULL) {
2220 			*aie = NULL;	/* XXX racey */
2221 			free(app, M_80211_NODE_IE);
2222 		}
2223 		return 0;
2224 	}
2225 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2226 		return EINVAL;
2227 	/*
2228 	 * Allocate a new appie structure and copy in the user data.
2229 	 * When done swap in the new structure.  Note that we do not
2230 	 * guard against users holding a ref to the old structure;
2231 	 * this must be handled outside this code.
2232 	 *
2233 	 * XXX bad bad bad
2234 	 */
2235 	napp = (struct ieee80211_appie *) malloc(
2236 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, M_NOWAIT);
2237 	if (napp == NULL)
2238 		return ENOMEM;
2239 	/* XXX holding ic lock */
2240 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2241 	if (error) {
2242 		free(napp, M_80211_NODE_IE);
2243 		return error;
2244 	}
2245 	napp->ie_len = ireq->i_len;
2246 	*aie = napp;
2247 	if (app != NULL)
2248 		free(app, M_80211_NODE_IE);
2249 	return 0;
2250 }
2251 
2252 static void
2253 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2254 {
2255 	/* validate data is present as best we can */
2256 	if (space == 0 || 2+ie[1] > space)
2257 		return;
2258 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2259 		vap->iv_wpa_ie = ie;
2260 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2261 		vap->iv_rsn_ie = ie;
2262 }
2263 
2264 static __noinline int
2265 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2266 	const struct ieee80211req *ireq, int fc0)
2267 {
2268 	int error;
2269 
2270 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2271 
2272 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2273 	case IEEE80211_FC0_SUBTYPE_BEACON:
2274 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2275 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2276 			error = EINVAL;
2277 			break;
2278 		}
2279 		error = setappie(&vap->iv_appie_beacon, ireq);
2280 		if (error == 0)
2281 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2282 		break;
2283 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2284 		error = setappie(&vap->iv_appie_proberesp, ireq);
2285 		break;
2286 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2287 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2288 			error = setappie(&vap->iv_appie_assocresp, ireq);
2289 		else
2290 			error = EINVAL;
2291 		break;
2292 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2293 		error = setappie(&vap->iv_appie_probereq, ireq);
2294 		break;
2295 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2296 		if (vap->iv_opmode == IEEE80211_M_STA)
2297 			error = setappie(&vap->iv_appie_assocreq, ireq);
2298 		else
2299 			error = EINVAL;
2300 		break;
2301 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2302 		error = setappie(&vap->iv_appie_wpa, ireq);
2303 		if (error == 0) {
2304 			/*
2305 			 * Must split single blob of data into separate
2306 			 * WPA and RSN ie's because they go in different
2307 			 * locations in the mgt frames.
2308 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2309 			 */
2310 			vap->iv_wpa_ie = NULL;
2311 			vap->iv_rsn_ie = NULL;
2312 			if (vap->iv_appie_wpa != NULL) {
2313 				struct ieee80211_appie *appie =
2314 				    vap->iv_appie_wpa;
2315 				uint8_t *data = appie->ie_data;
2316 
2317 				/* XXX ie length validate is painful, cheat */
2318 				setwparsnie(vap, data, appie->ie_len);
2319 				setwparsnie(vap, data + 2 + data[1],
2320 				    appie->ie_len - (2 + data[1]));
2321 			}
2322 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2323 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2324 				/*
2325 				 * Must rebuild beacon frame as the update
2326 				 * mechanism doesn't handle WPA/RSN ie's.
2327 				 * Could extend it but it doesn't normally
2328 				 * change; this is just to deal with hostapd
2329 				 * plumbing the ie after the interface is up.
2330 				 */
2331 				error = ENETRESET;
2332 			}
2333 		}
2334 		break;
2335 	default:
2336 		error = EINVAL;
2337 		break;
2338 	}
2339 	return error;
2340 }
2341 
2342 static __noinline int
2343 ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2344 	const struct ieee80211req *ireq)
2345 {
2346 	struct ieee80211com *ic = vap->iv_ic;
2347 	int error;
2348 	uint8_t fc0;
2349 
2350 	fc0 = ireq->i_val & 0xff;
2351 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2352 		return EINVAL;
2353 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2354 	IEEE80211_LOCK(ic);
2355 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2356 	IEEE80211_UNLOCK(ic);
2357 	return error;
2358 }
2359 
2360 static __noinline int
2361 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2362 {
2363 	struct ieee80211com *ic = vap->iv_ic;
2364 	struct ieee80211_chanswitch_req csr;
2365 	struct ieee80211_channel *c;
2366 	int error;
2367 
2368 	if (ireq->i_len != sizeof(csr))
2369 		return EINVAL;
2370 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2371 	if (error != 0)
2372 		return error;
2373 	/* XXX adhoc mode not supported */
2374 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2375 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2376 		return EOPNOTSUPP;
2377 	c = ieee80211_find_channel(ic,
2378 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2379 	if (c == NULL)
2380 		return ENOENT;
2381 	IEEE80211_LOCK(ic);
2382 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2383 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2384 	else if (csr.csa_count == 0)
2385 		ieee80211_csa_cancelswitch(ic);
2386 	else
2387 		error = EBUSY;
2388 	IEEE80211_UNLOCK(ic);
2389 	return error;
2390 }
2391 
2392 static int
2393 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2394 {
2395 #define	IEEE80211_IOC_SCAN_FLAGS \
2396 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2397 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2398 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2399 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2400 	 IEEE80211_IOC_SCAN_CHECK)
2401 	struct ieee80211com *ic = vap->iv_ic;
2402 	int error, i;
2403 
2404 	/* convert duration */
2405 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2406 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2407 	else {
2408 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2409 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2410 			return EINVAL;
2411 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2412 		if (sr->sr_duration < 1)
2413 			sr->sr_duration = 1;
2414 	}
2415 	/* convert min/max channel dwell */
2416 	if (sr->sr_mindwell != 0) {
2417 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2418 		if (sr->sr_mindwell < 1)
2419 			sr->sr_mindwell = 1;
2420 	}
2421 	if (sr->sr_maxdwell != 0) {
2422 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2423 		if (sr->sr_maxdwell < 1)
2424 			sr->sr_maxdwell = 1;
2425 	}
2426 	/* NB: silently reduce ssid count to what is supported */
2427 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2428 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2429 	for (i = 0; i < sr->sr_nssid; i++)
2430 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2431 			return EINVAL;
2432 	/* cleanse flags just in case, could reject if invalid flags */
2433 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2434 	/*
2435 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2436 	 * allows applications to scan without joining a bss (or picking
2437 	 * a channel and setting up a bss) and without forcing manual
2438 	 * roaming mode--you just need to mark the parent device UP.
2439 	 */
2440 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2441 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2442 
2443 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2444 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2445 	    __func__, sr->sr_flags,
2446 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2447 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2448 	/*
2449 	 * If we are in INIT state then the driver has never had a chance
2450 	 * to setup hardware state to do a scan; we must use the state
2451 	 * machine to get us up to the SCAN state but once we reach SCAN
2452 	 * state we then want to use the supplied params.  Stash the
2453 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2454 	 * state machines will recognize this and use the stashed params
2455 	 * to issue the scan request.
2456 	 *
2457 	 * Otherwise just invoke the scan machinery directly.
2458 	 */
2459 	IEEE80211_LOCK(ic);
2460 	if (vap->iv_state == IEEE80211_S_INIT) {
2461 		/* NB: clobbers previous settings */
2462 		vap->iv_scanreq_flags = sr->sr_flags;
2463 		vap->iv_scanreq_duration = sr->sr_duration;
2464 		vap->iv_scanreq_nssid = sr->sr_nssid;
2465 		for (i = 0; i < sr->sr_nssid; i++) {
2466 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2467 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2468 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2469 		}
2470 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2471 		IEEE80211_UNLOCK(ic);
2472 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2473 	} else {
2474 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2475 		IEEE80211_UNLOCK(ic);
2476 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2477 			error = ieee80211_check_scan(vap, sr->sr_flags,
2478 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2479 			    sr->sr_nssid,
2480 			    /* NB: cheat, we assume structures are compatible */
2481 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2482 		} else {
2483 			error = ieee80211_start_scan(vap, sr->sr_flags,
2484 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2485 			    sr->sr_nssid,
2486 			    /* NB: cheat, we assume structures are compatible */
2487 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2488 		}
2489 		if (error == 0)
2490 			return EINPROGRESS;
2491 	}
2492 	return 0;
2493 #undef IEEE80211_IOC_SCAN_FLAGS
2494 }
2495 
2496 static __noinline int
2497 ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2498 {
2499 	struct ieee80211com *ic = vap->iv_ic;
2500 	struct ieee80211_scan_req sr;		/* XXX off stack? */
2501 	int error;
2502 
2503 	/* NB: parent must be running */
2504 	if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2505 		return ENXIO;
2506 
2507 	if (ireq->i_len != sizeof(sr))
2508 		return EINVAL;
2509 	error = copyin(ireq->i_data, &sr, sizeof(sr));
2510 	if (error != 0)
2511 		return error;
2512 	return ieee80211_scanreq(vap, &sr);
2513 }
2514 
2515 static __noinline int
2516 ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2517 {
2518 	struct ieee80211_node *ni;
2519 	struct ieee80211req_sta_vlan vlan;
2520 	int error;
2521 
2522 	if (ireq->i_len != sizeof(vlan))
2523 		return EINVAL;
2524 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2525 	if (error != 0)
2526 		return error;
2527 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2528 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2529 		    vlan.sv_macaddr);
2530 		if (ni == NULL)
2531 			return ENOENT;
2532 	} else
2533 		ni = ieee80211_ref_node(vap->iv_bss);
2534 	ni->ni_vlan = vlan.sv_vlan;
2535 	ieee80211_free_node(ni);
2536 	return error;
2537 }
2538 
2539 static int
2540 isvap11g(const struct ieee80211vap *vap)
2541 {
2542 	const struct ieee80211_node *bss = vap->iv_bss;
2543 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2544 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2545 }
2546 
2547 static int
2548 isvapht(const struct ieee80211vap *vap)
2549 {
2550 	const struct ieee80211_node *bss = vap->iv_bss;
2551 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2552 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2553 }
2554 
2555 /*
2556  * Dummy ioctl set handler so the linker set is defined.
2557  */
2558 static int
2559 dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
2560 {
2561 	return ENOSYS;
2562 }
2563 IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
2564 
2565 static int
2566 ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
2567 {
2568 	ieee80211_ioctl_setfunc * const *set;
2569 	int error;
2570 
2571 	SET_FOREACH(set, ieee80211_ioctl_setset) {
2572 		error = (*set)(vap, ireq);
2573 		if (error != ENOSYS)
2574 			return error;
2575 	}
2576 	return EINVAL;
2577 }
2578 
2579 static __noinline int
2580 ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2581 {
2582 	struct ieee80211com *ic = vap->iv_ic;
2583 	int error;
2584 	const struct ieee80211_authenticator *auth;
2585 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2586 	char tmpssid[IEEE80211_NWID_LEN];
2587 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2588 	struct ieee80211_key *k;
2589 	u_int kid;
2590 	uint32_t flags;
2591 
2592 	error = 0;
2593 	switch (ireq->i_type) {
2594 	case IEEE80211_IOC_SSID:
2595 		if (ireq->i_val != 0 ||
2596 		    ireq->i_len > IEEE80211_NWID_LEN)
2597 			return EINVAL;
2598 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2599 		if (error)
2600 			break;
2601 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2602 		vap->iv_des_ssid[0].len = ireq->i_len;
2603 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2604 		vap->iv_des_nssid = (ireq->i_len > 0);
2605 		error = ENETRESET;
2606 		break;
2607 	case IEEE80211_IOC_WEP:
2608 		switch (ireq->i_val) {
2609 		case IEEE80211_WEP_OFF:
2610 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2611 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2612 			break;
2613 		case IEEE80211_WEP_ON:
2614 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2615 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2616 			break;
2617 		case IEEE80211_WEP_MIXED:
2618 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2619 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2620 			break;
2621 		}
2622 		error = ENETRESET;
2623 		break;
2624 	case IEEE80211_IOC_WEPKEY:
2625 		kid = (u_int) ireq->i_val;
2626 		if (kid >= IEEE80211_WEP_NKID)
2627 			return EINVAL;
2628 		k = &vap->iv_nw_keys[kid];
2629 		if (ireq->i_len == 0) {
2630 			/* zero-len =>'s delete any existing key */
2631 			(void) ieee80211_crypto_delkey(vap, k);
2632 			break;
2633 		}
2634 		if (ireq->i_len > sizeof(tmpkey))
2635 			return EINVAL;
2636 		memset(tmpkey, 0, sizeof(tmpkey));
2637 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2638 		if (error)
2639 			break;
2640 		ieee80211_key_update_begin(vap);
2641 		k->wk_keyix = kid;	/* NB: force fixed key id */
2642 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2643 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2644 			k->wk_keylen = ireq->i_len;
2645 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2646 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2647 			if  (!ieee80211_crypto_setkey(vap, k))
2648 				error = EINVAL;
2649 		} else
2650 			error = EINVAL;
2651 		ieee80211_key_update_end(vap);
2652 		break;
2653 	case IEEE80211_IOC_WEPTXKEY:
2654 		kid = (u_int) ireq->i_val;
2655 		if (kid >= IEEE80211_WEP_NKID &&
2656 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2657 			return EINVAL;
2658 		vap->iv_def_txkey = kid;
2659 		break;
2660 	case IEEE80211_IOC_AUTHMODE:
2661 		switch (ireq->i_val) {
2662 		case IEEE80211_AUTH_WPA:
2663 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2664 		case IEEE80211_AUTH_OPEN:	/* open */
2665 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2666 		case IEEE80211_AUTH_AUTO:	/* auto */
2667 			auth = ieee80211_authenticator_get(ireq->i_val);
2668 			if (auth == NULL)
2669 				return EINVAL;
2670 			break;
2671 		default:
2672 			return EINVAL;
2673 		}
2674 		switch (ireq->i_val) {
2675 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2676 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2677 			ireq->i_val = IEEE80211_AUTH_8021X;
2678 			break;
2679 		case IEEE80211_AUTH_OPEN:	/* open */
2680 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2681 			break;
2682 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2683 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2684 			vap->iv_flags &= ~IEEE80211_F_WPA;
2685 			/* both require a key so mark the PRIVACY capability */
2686 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2687 			break;
2688 		case IEEE80211_AUTH_AUTO:	/* auto */
2689 			vap->iv_flags &= ~IEEE80211_F_WPA;
2690 			/* XXX PRIVACY handling? */
2691 			/* XXX what's the right way to do this? */
2692 			break;
2693 		}
2694 		/* NB: authenticator attach/detach happens on state change */
2695 		vap->iv_bss->ni_authmode = ireq->i_val;
2696 		/* XXX mixed/mode/usage? */
2697 		vap->iv_auth = auth;
2698 		error = ENETRESET;
2699 		break;
2700 	case IEEE80211_IOC_CHANNEL:
2701 		error = ieee80211_ioctl_setchannel(vap, ireq);
2702 		break;
2703 	case IEEE80211_IOC_POWERSAVE:
2704 		switch (ireq->i_val) {
2705 		case IEEE80211_POWERSAVE_OFF:
2706 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2707 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2708 				error = ERESTART;
2709 			}
2710 			break;
2711 		case IEEE80211_POWERSAVE_ON:
2712 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2713 				error = EOPNOTSUPP;
2714 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2715 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2716 				error = ERESTART;
2717 			}
2718 			break;
2719 		default:
2720 			error = EINVAL;
2721 			break;
2722 		}
2723 		break;
2724 	case IEEE80211_IOC_POWERSAVESLEEP:
2725 		if (ireq->i_val < 0)
2726 			return EINVAL;
2727 		ic->ic_lintval = ireq->i_val;
2728 		error = ERESTART;
2729 		break;
2730 	case IEEE80211_IOC_RTSTHRESHOLD:
2731 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2732 		      ireq->i_val <= IEEE80211_RTS_MAX))
2733 			return EINVAL;
2734 		vap->iv_rtsthreshold = ireq->i_val;
2735 		error = ERESTART;
2736 		break;
2737 	case IEEE80211_IOC_PROTMODE:
2738 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2739 			return EINVAL;
2740 		ic->ic_protmode = ireq->i_val;
2741 		/* NB: if not operating in 11g this can wait */
2742 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2743 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2744 			error = ERESTART;
2745 		break;
2746 	case IEEE80211_IOC_TXPOWER:
2747 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2748 			return EOPNOTSUPP;
2749 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2750 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2751 			return EINVAL;
2752 		ic->ic_txpowlimit = ireq->i_val;
2753 		error = ERESTART;
2754 		break;
2755 	case IEEE80211_IOC_ROAMING:
2756 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2757 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2758 			return EINVAL;
2759 		vap->iv_roaming = ireq->i_val;
2760 		/* XXXX reset? */
2761 		break;
2762 	case IEEE80211_IOC_PRIVACY:
2763 		if (ireq->i_val) {
2764 			/* XXX check for key state? */
2765 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2766 		} else
2767 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2768 		/* XXX ERESTART? */
2769 		break;
2770 	case IEEE80211_IOC_DROPUNENCRYPTED:
2771 		if (ireq->i_val)
2772 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2773 		else
2774 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2775 		/* XXX ERESTART? */
2776 		break;
2777 	case IEEE80211_IOC_WPAKEY:
2778 		error = ieee80211_ioctl_setkey(vap, ireq);
2779 		break;
2780 	case IEEE80211_IOC_DELKEY:
2781 		error = ieee80211_ioctl_delkey(vap, ireq);
2782 		break;
2783 	case IEEE80211_IOC_MLME:
2784 		error = ieee80211_ioctl_setmlme(vap, ireq);
2785 		break;
2786 	case IEEE80211_IOC_COUNTERMEASURES:
2787 		if (ireq->i_val) {
2788 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2789 				return EOPNOTSUPP;
2790 			vap->iv_flags |= IEEE80211_F_COUNTERM;
2791 		} else
2792 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2793 		/* XXX ERESTART? */
2794 		break;
2795 	case IEEE80211_IOC_WPA:
2796 		if (ireq->i_val > 3)
2797 			return EINVAL;
2798 		/* XXX verify ciphers available */
2799 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2800 		switch (ireq->i_val) {
2801 		case 1:
2802 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2803 				return EOPNOTSUPP;
2804 			flags |= IEEE80211_F_WPA1;
2805 			break;
2806 		case 2:
2807 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2808 				return EOPNOTSUPP;
2809 			flags |= IEEE80211_F_WPA2;
2810 			break;
2811 		case 3:
2812 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2813 				return EOPNOTSUPP;
2814 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2815 			break;
2816 		default:	/*  Can't set any -> error */
2817 			return EOPNOTSUPP;
2818 		}
2819 		vap->iv_flags = flags;
2820 		error = ERESTART;	/* NB: can change beacon frame */
2821 		break;
2822 	case IEEE80211_IOC_WME:
2823 		if (ireq->i_val) {
2824 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2825 				return EOPNOTSUPP;
2826 			ieee80211_syncflag(vap, IEEE80211_F_WME);
2827 		} else
2828 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2829 		error = ERESTART;	/* NB: can change beacon frame */
2830 		break;
2831 	case IEEE80211_IOC_HIDESSID:
2832 		if (ireq->i_val)
2833 			vap->iv_flags |= IEEE80211_F_HIDESSID;
2834 		else
2835 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2836 		error = ERESTART;		/* XXX ENETRESET? */
2837 		break;
2838 	case IEEE80211_IOC_APBRIDGE:
2839 		if (ireq->i_val == 0)
2840 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2841 		else
2842 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2843 		break;
2844 	case IEEE80211_IOC_BSSID:
2845 		if (ireq->i_len != sizeof(tmpbssid))
2846 			return EINVAL;
2847 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2848 		if (error)
2849 			break;
2850 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2851 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2852 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2853 		else
2854 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2855 		error = ENETRESET;
2856 		break;
2857 	case IEEE80211_IOC_CHANLIST:
2858 		error = ieee80211_ioctl_setchanlist(vap, ireq);
2859 		break;
2860 #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2861 #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2862 	case OLD_IEEE80211_IOC_SCAN_REQ:
2863 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2864 			"%s: active scan request\n", __func__);
2865 		/*
2866 		 * If we are in INIT state then the driver has never
2867 		 * had a chance to setup hardware state to do a scan;
2868 		 * use the state machine to get us up the SCAN state.
2869 		 * Otherwise just invoke the scan machinery to start
2870 		 * a one-time scan.
2871 		 */
2872 		if (vap->iv_state == IEEE80211_S_INIT)
2873 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2874 		else
2875 			(void) ieee80211_start_scan(vap,
2876 				IEEE80211_SCAN_ACTIVE |
2877 				IEEE80211_SCAN_NOPICK |
2878 				IEEE80211_SCAN_ONCE,
2879 				IEEE80211_SCAN_FOREVER, 0, 0,
2880 				/* XXX use ioctl params */
2881 				vap->iv_des_nssid, vap->iv_des_ssid);
2882 		break;
2883 #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2884 	case IEEE80211_IOC_SCAN_REQ:
2885 		error = ieee80211_ioctl_scanreq(vap, ireq);
2886 		break;
2887 	case IEEE80211_IOC_SCAN_CANCEL:
2888 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2889 		    "%s: cancel scan\n", __func__);
2890 		ieee80211_cancel_scan(vap);
2891 		break;
2892 	case IEEE80211_IOC_HTCONF:
2893 		if (ireq->i_val & 1)
2894 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
2895 		else
2896 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
2897 		if (ireq->i_val & 2)
2898 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
2899 		else
2900 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
2901 		error = ENETRESET;
2902 		break;
2903 	case IEEE80211_IOC_ADDMAC:
2904 	case IEEE80211_IOC_DELMAC:
2905 		error = ieee80211_ioctl_macmac(vap, ireq);
2906 		break;
2907 	case IEEE80211_IOC_MACCMD:
2908 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
2909 		break;
2910 	case IEEE80211_IOC_STA_STATS:
2911 		error = ieee80211_ioctl_setstastats(vap, ireq);
2912 		break;
2913 	case IEEE80211_IOC_STA_TXPOW:
2914 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
2915 		break;
2916 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
2917 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
2918 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
2919 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
2920 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
2921 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
2922 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
2923 		break;
2924 	case IEEE80211_IOC_DTIM_PERIOD:
2925 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2926 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2927 		    vap->iv_opmode != IEEE80211_M_IBSS)
2928 			return EINVAL;
2929 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
2930 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2931 			vap->iv_dtim_period = ireq->i_val;
2932 			error = ENETRESET;		/* requires restart */
2933 		} else
2934 			error = EINVAL;
2935 		break;
2936 	case IEEE80211_IOC_BEACON_INTERVAL:
2937 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2938 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2939 		    vap->iv_opmode != IEEE80211_M_IBSS)
2940 			return EINVAL;
2941 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
2942 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2943 			ic->ic_bintval = ireq->i_val;
2944 			error = ENETRESET;		/* requires restart */
2945 		} else
2946 			error = EINVAL;
2947 		break;
2948 	case IEEE80211_IOC_PUREG:
2949 		if (ireq->i_val)
2950 			vap->iv_flags |= IEEE80211_F_PUREG;
2951 		else
2952 			vap->iv_flags &= ~IEEE80211_F_PUREG;
2953 		/* NB: reset only if we're operating on an 11g channel */
2954 		if (isvap11g(vap))
2955 			error = ENETRESET;
2956 		break;
2957 	case IEEE80211_IOC_QUIET:
2958 		vap->iv_quiet= ireq->i_val;
2959 		break;
2960 	case IEEE80211_IOC_QUIET_COUNT:
2961 		vap->iv_quiet_count=ireq->i_val;
2962 		break;
2963 	case IEEE80211_IOC_QUIET_PERIOD:
2964 		vap->iv_quiet_period=ireq->i_val;
2965 		break;
2966 	case IEEE80211_IOC_QUIET_OFFSET:
2967 		vap->iv_quiet_offset=ireq->i_val;
2968 		break;
2969 	case IEEE80211_IOC_QUIET_DUR:
2970 		if(ireq->i_val < vap->iv_bss->ni_intval)
2971 			vap->iv_quiet_duration = ireq->i_val;
2972 		else
2973 			error = EINVAL;
2974 		break;
2975 	case IEEE80211_IOC_BGSCAN:
2976 		if (ireq->i_val) {
2977 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
2978 				return EOPNOTSUPP;
2979 			vap->iv_flags |= IEEE80211_F_BGSCAN;
2980 		} else
2981 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
2982 		break;
2983 	case IEEE80211_IOC_BGSCAN_IDLE:
2984 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
2985 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
2986 		else
2987 			error = EINVAL;
2988 		break;
2989 	case IEEE80211_IOC_BGSCAN_INTERVAL:
2990 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
2991 			vap->iv_bgscanintvl = ireq->i_val*hz;
2992 		else
2993 			error = EINVAL;
2994 		break;
2995 	case IEEE80211_IOC_SCANVALID:
2996 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
2997 			vap->iv_scanvalid = ireq->i_val*hz;
2998 		else
2999 			error = EINVAL;
3000 		break;
3001 	case IEEE80211_IOC_FRAGTHRESHOLD:
3002 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
3003 		    ireq->i_val != IEEE80211_FRAG_MAX)
3004 			return EOPNOTSUPP;
3005 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
3006 		      ireq->i_val <= IEEE80211_FRAG_MAX))
3007 			return EINVAL;
3008 		vap->iv_fragthreshold = ireq->i_val;
3009 		error = ERESTART;
3010 		break;
3011 	case IEEE80211_IOC_BURST:
3012 		if (ireq->i_val) {
3013 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3014 				return EOPNOTSUPP;
3015 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3016 		} else
3017 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3018 		error = ERESTART;
3019 		break;
3020 	case IEEE80211_IOC_BMISSTHRESHOLD:
3021 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3022 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3023 			return EINVAL;
3024 		vap->iv_bmissthreshold = ireq->i_val;
3025 		error = ERESTART;
3026 		break;
3027 	case IEEE80211_IOC_CURCHAN:
3028 		error = ieee80211_ioctl_setcurchan(vap, ireq);
3029 		break;
3030 	case IEEE80211_IOC_SHORTGI:
3031 		if (ireq->i_val) {
3032 #define	IEEE80211_HTCAP_SHORTGI \
3033 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3034 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
3035 				return EINVAL;
3036 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
3037 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
3038 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
3039 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
3040 #undef IEEE80211_HTCAP_SHORTGI
3041 		} else
3042 			vap->iv_flags_ht &=
3043 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3044 		error = ERESTART;
3045 		break;
3046 	case IEEE80211_IOC_AMPDU:
3047 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
3048 			return EINVAL;
3049 		if (ireq->i_val & 1)
3050 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3051 		else
3052 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3053 		if (ireq->i_val & 2)
3054 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3055 		else
3056 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
3057 		/* NB: reset only if we're operating on an 11n channel */
3058 		if (isvapht(vap))
3059 			error = ERESTART;
3060 		break;
3061 	case IEEE80211_IOC_AMPDU_LIMIT:
3062 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3063 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3064 			return EINVAL;
3065 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3066 			vap->iv_ampdu_rxmax = ireq->i_val;
3067 		else
3068 			vap->iv_ampdu_limit = ireq->i_val;
3069 		error = ERESTART;
3070 		break;
3071 	case IEEE80211_IOC_AMPDU_DENSITY:
3072 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3073 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3074 			return EINVAL;
3075 		vap->iv_ampdu_density = ireq->i_val;
3076 		error = ERESTART;
3077 		break;
3078 	case IEEE80211_IOC_AMSDU:
3079 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
3080 			return EINVAL;
3081 		if (ireq->i_val & 1)
3082 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3083 		else
3084 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
3085 		if (ireq->i_val & 2)
3086 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3087 		else
3088 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
3089 		/* NB: reset only if we're operating on an 11n channel */
3090 		if (isvapht(vap))
3091 			error = ERESTART;
3092 		break;
3093 	case IEEE80211_IOC_AMSDU_LIMIT:
3094 		/* XXX validate */
3095 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
3096 		break;
3097 	case IEEE80211_IOC_PUREN:
3098 		if (ireq->i_val) {
3099 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3100 				return EINVAL;
3101 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
3102 		} else
3103 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
3104 		/* NB: reset only if we're operating on an 11n channel */
3105 		if (isvapht(vap))
3106 			error = ERESTART;
3107 		break;
3108 	case IEEE80211_IOC_DOTH:
3109 		if (ireq->i_val) {
3110 #if 0
3111 			/* XXX no capability */
3112 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3113 				return EOPNOTSUPP;
3114 #endif
3115 			vap->iv_flags |= IEEE80211_F_DOTH;
3116 		} else
3117 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3118 		error = ENETRESET;
3119 		break;
3120 	case IEEE80211_IOC_REGDOMAIN:
3121 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3122 		break;
3123 	case IEEE80211_IOC_ROAM:
3124 		error = ieee80211_ioctl_setroam(vap, ireq);
3125 		break;
3126 	case IEEE80211_IOC_TXPARAMS:
3127 		error = ieee80211_ioctl_settxparams(vap, ireq);
3128 		break;
3129 	case IEEE80211_IOC_HTCOMPAT:
3130 		if (ireq->i_val) {
3131 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3132 				return EOPNOTSUPP;
3133 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
3134 		} else
3135 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
3136 		/* NB: reset only if we're operating on an 11n channel */
3137 		if (isvapht(vap))
3138 			error = ERESTART;
3139 		break;
3140 	case IEEE80211_IOC_DWDS:
3141 		if (ireq->i_val) {
3142 			/* NB: DWDS only makes sense for WDS-capable devices */
3143 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3144 				return EOPNOTSUPP;
3145 			/* NB: DWDS is used only with ap+sta vaps */
3146 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3147 			    vap->iv_opmode != IEEE80211_M_STA)
3148 				return EINVAL;
3149 			vap->iv_flags |= IEEE80211_F_DWDS;
3150 			if (vap->iv_opmode == IEEE80211_M_STA)
3151 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3152 		} else {
3153 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3154 			if (vap->iv_opmode == IEEE80211_M_STA)
3155 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3156 		}
3157 		break;
3158 	case IEEE80211_IOC_INACTIVITY:
3159 		if (ireq->i_val)
3160 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3161 		else
3162 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3163 		break;
3164 	case IEEE80211_IOC_APPIE:
3165 		error = ieee80211_ioctl_setappie(vap, ireq);
3166 		break;
3167 	case IEEE80211_IOC_WPS:
3168 		if (ireq->i_val) {
3169 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3170 				return EOPNOTSUPP;
3171 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3172 		} else
3173 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3174 		break;
3175 	case IEEE80211_IOC_TSN:
3176 		if (ireq->i_val) {
3177 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3178 				return EOPNOTSUPP;
3179 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3180 		} else
3181 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3182 		break;
3183 	case IEEE80211_IOC_CHANSWITCH:
3184 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3185 		break;
3186 	case IEEE80211_IOC_DFS:
3187 		if (ireq->i_val) {
3188 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3189 				return EOPNOTSUPP;
3190 			/* NB: DFS requires 11h support */
3191 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3192 				return EINVAL;
3193 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3194 		} else
3195 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3196 		break;
3197 	case IEEE80211_IOC_DOTD:
3198 		if (ireq->i_val)
3199 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3200 		else
3201 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3202 		if (vap->iv_opmode == IEEE80211_M_STA)
3203 			error = ENETRESET;
3204 		break;
3205 	case IEEE80211_IOC_HTPROTMODE:
3206 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3207 			return EINVAL;
3208 		ic->ic_htprotmode = ireq->i_val ?
3209 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3210 		/* NB: if not operating in 11n this can wait */
3211 		if (isvapht(vap))
3212 			error = ERESTART;
3213 		break;
3214 	case IEEE80211_IOC_STA_VLAN:
3215 		error = ieee80211_ioctl_setstavlan(vap, ireq);
3216 		break;
3217 	case IEEE80211_IOC_SMPS:
3218 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3219 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3220 			return EINVAL;
3221 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3222 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3223 			return EOPNOTSUPP;
3224 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3225 			ireq->i_val;
3226 		/* NB: if not operating in 11n this can wait */
3227 		if (isvapht(vap))
3228 			error = ERESTART;
3229 		break;
3230 	case IEEE80211_IOC_RIFS:
3231 		if (ireq->i_val != 0) {
3232 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3233 				return EOPNOTSUPP;
3234 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
3235 		} else
3236 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
3237 		/* NB: if not operating in 11n this can wait */
3238 		if (isvapht(vap))
3239 			error = ERESTART;
3240 		break;
3241 	default:
3242 		error = ieee80211_ioctl_setdefault(vap, ireq);
3243 		break;
3244 	}
3245 	/*
3246 	 * The convention is that ENETRESET means an operation
3247 	 * requires a complete re-initialization of the device (e.g.
3248 	 * changing something that affects the association state).
3249 	 * ERESTART means the request may be handled with only a
3250 	 * reload of the hardware state.  We hand ERESTART requests
3251 	 * to the iv_reset callback so the driver can decide.  If
3252 	 * a device does not fillin iv_reset then it defaults to one
3253 	 * that returns ENETRESET.  Otherwise a driver may return
3254 	 * ENETRESET (in which case a full reset will be done) or
3255 	 * 0 to mean there's no need to do anything (e.g. when the
3256 	 * change has no effect on the driver/device).
3257 	 */
3258 	if (error == ERESTART)
3259 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3260 		    vap->iv_reset(vap, ireq->i_type) : 0;
3261 	if (error == ENETRESET) {
3262 		/* XXX need to re-think AUTO handling */
3263 		if (IS_UP_AUTO(vap))
3264 			ieee80211_init(vap);
3265 		error = 0;
3266 	}
3267 	return error;
3268 }
3269 
3270 /*
3271  * Rebuild the parent's multicast address list after an add/del
3272  * of a multicast address for a vap.  We have no way to tell
3273  * what happened above to optimize the work so we purge the entire
3274  * list and rebuild from scratch.  This is way expensive.
3275  * Note also the half-baked workaround for if_addmulti calling
3276  * back to the parent device; there's no way to insert mcast
3277  * entries quietly and/or cheaply.
3278  */
3279 static void
3280 ieee80211_ioctl_updatemulti(struct ieee80211com *ic)
3281 {
3282 	struct ifnet *parent = ic->ic_ifp;
3283 	struct ieee80211vap *vap;
3284 	void *ioctl;
3285 
3286 	IEEE80211_LOCK(ic);
3287 	if_delallmulti(parent);
3288 	ioctl = parent->if_ioctl;	/* XXX WAR if_allmulti */
3289 	parent->if_ioctl = NULL;
3290 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3291 		struct ifnet *ifp = vap->iv_ifp;
3292 		struct ifmultiaddr *ifma;
3293 
3294 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3295 			if (ifma->ifma_addr->sa_family != AF_LINK)
3296 				continue;
3297 			(void) if_addmulti(parent, ifma->ifma_addr, NULL);
3298 		}
3299 	}
3300 	parent->if_ioctl = ioctl;
3301 	ieee80211_runtask(ic, &ic->ic_mcast_task);
3302 	IEEE80211_UNLOCK(ic);
3303 }
3304 
3305 int
3306 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3307 {
3308 	struct ieee80211vap *vap = ifp->if_softc;
3309 	struct ieee80211com *ic = vap->iv_ic;
3310 	int error = 0;
3311 	struct ifreq *ifr;
3312 	struct ifaddr *ifa;			/* XXX */
3313 
3314 	switch (cmd) {
3315 	case SIOCSIFFLAGS:
3316 		IEEE80211_LOCK(ic);
3317 		ieee80211_syncifflag_locked(ic, IFF_PROMISC);
3318 		ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
3319 		if (ifp->if_flags & IFF_UP) {
3320 			/*
3321 			 * Bring ourself up unless we're already operational.
3322 			 * If we're the first vap and the parent is not up
3323 			 * then it will automatically be brought up as a
3324 			 * side-effect of bringing ourself up.
3325 			 */
3326 			if (vap->iv_state == IEEE80211_S_INIT)
3327 				ieee80211_start_locked(vap);
3328 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3329 			/*
3330 			 * Stop ourself.  If we are the last vap to be
3331 			 * marked down the parent will also be taken down.
3332 			 */
3333 			ieee80211_stop_locked(vap);
3334 		}
3335 		IEEE80211_UNLOCK(ic);
3336 		/* Wait for parent ioctl handler if it was queued */
3337 		ieee80211_waitfor_parent(ic);
3338 		break;
3339 	case SIOCADDMULTI:
3340 	case SIOCDELMULTI:
3341 		ieee80211_ioctl_updatemulti(ic);
3342 		break;
3343 	case SIOCSIFMEDIA:
3344 	case SIOCGIFMEDIA:
3345 		ifr = (struct ifreq *)data;
3346 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3347 		break;
3348 	case SIOCG80211:
3349 		error = ieee80211_ioctl_get80211(vap, cmd,
3350 				(struct ieee80211req *) data);
3351 		break;
3352 	case SIOCS80211:
3353 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3354 		if (error == 0)
3355 			error = ieee80211_ioctl_set80211(vap, cmd,
3356 					(struct ieee80211req *) data);
3357 		break;
3358 	case SIOCG80211STATS:
3359 		ifr = (struct ifreq *)data;
3360 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3361 		break;
3362 	case SIOCSIFMTU:
3363 		ifr = (struct ifreq *)data;
3364 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3365 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3366 			error = EINVAL;
3367 		else
3368 			ifp->if_mtu = ifr->ifr_mtu;
3369 		break;
3370 	case SIOCSIFADDR:
3371 		/*
3372 		 * XXX Handle this directly so we can supress if_init calls.
3373 		 * XXX This should be done in ether_ioctl but for the moment
3374 		 * XXX there are too many other parts of the system that
3375 		 * XXX set IFF_UP and so supress if_init being called when
3376 		 * XXX it should be.
3377 		 */
3378 		ifa = (struct ifaddr *) data;
3379 		switch (ifa->ifa_addr->sa_family) {
3380 #ifdef INET
3381 		case AF_INET:
3382 			if ((ifp->if_flags & IFF_UP) == 0) {
3383 				ifp->if_flags |= IFF_UP;
3384 				ifp->if_init(ifp->if_softc);
3385 			}
3386 			arp_ifinit(ifp, ifa);
3387 			break;
3388 #endif
3389 #ifdef IPX
3390 		/*
3391 		 * XXX - This code is probably wrong,
3392 		 *	 but has been copied many times.
3393 		 */
3394 		case AF_IPX: {
3395 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
3396 
3397 			if (ipx_nullhost(*ina))
3398 				ina->x_host = *(union ipx_host *)
3399 				    IF_LLADDR(ifp);
3400 			else
3401 				bcopy((caddr_t) ina->x_host.c_host,
3402 				      (caddr_t) IF_LLADDR(ifp),
3403 				      ETHER_ADDR_LEN);
3404 			/* fall thru... */
3405 		}
3406 #endif
3407 		default:
3408 			if ((ifp->if_flags & IFF_UP) == 0) {
3409 				ifp->if_flags |= IFF_UP;
3410 				ifp->if_init(ifp->if_softc);
3411 			}
3412 			break;
3413 		}
3414 		break;
3415 	/* Pass NDIS ioctls up to the driver */
3416 	case SIOCGDRVSPEC:
3417 	case SIOCSDRVSPEC:
3418 	case SIOCGPRIVATE_0: {
3419 		struct ifnet *parent = vap->iv_ic->ic_ifp;
3420 		error = parent->if_ioctl(parent, cmd, data);
3421 		break;
3422 	}
3423 	default:
3424 		error = ether_ioctl(ifp, cmd, data);
3425 		break;
3426 	}
3427 	return error;
3428 }
3429