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