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