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