xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 /*
31  * IEEE 802.11 ioctl support (FreeBSD-specific)
32  */
33 
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/endian.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/systm.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_private.h>
50 #include <net/ethernet.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
55 #endif
56 
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_ioctl.h>
59 #include <net80211/ieee80211_regdomain.h>
60 #include <net80211/ieee80211_input.h>
61 
62 #define	IS_UP_AUTO(_vap) \
63 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
64 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
65 
66 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
67 static struct ieee80211_channel *findchannel(struct ieee80211com *,
68 		int ieee, int mode);
69 static int ieee80211_scanreq(struct ieee80211vap *,
70 		struct ieee80211_scan_req *);
71 
72 static int
73 ieee80211_ioctl_getkey(u_long cmd, struct ieee80211vap *vap,
74     struct ieee80211req *ireq)
75 {
76 	struct ieee80211com *ic = vap->iv_ic;
77 	struct ieee80211_node *ni;
78 	struct ieee80211req_key ik;
79 	struct ieee80211_key *wk;
80 	const struct ieee80211_cipher *cip;
81 	u_int kid;
82 	int error;
83 
84 	if (ireq->i_len != sizeof(ik))
85 		return EINVAL;
86 	error = copyin(ireq->i_data, &ik, sizeof(ik));
87 	if (error)
88 		return error;
89 	kid = ik.ik_keyix;
90 	if (kid == IEEE80211_KEYIX_NONE) {
91 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
92 		if (ni == NULL)
93 			return ENOENT;
94 		wk = &ni->ni_ucastkey;
95 	} else {
96 		if (kid >= IEEE80211_WEP_NKID)
97 			return EINVAL;
98 		wk = &vap->iv_nw_keys[kid];
99 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
100 		ni = NULL;
101 	}
102 	cip = wk->wk_cipher;
103 	ik.ik_type = cip->ic_cipher;
104 	ik.ik_keylen = wk->wk_keylen;
105 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
106 	if (wk->wk_keyix == vap->iv_def_txkey)
107 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
108 	if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
109 		/* NB: only root can read key data */
110 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
111 		ik.ik_keytsc = wk->wk_keytsc;
112 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
113 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
114 			memcpy(ik.ik_keydata+wk->wk_keylen,
115 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
116 				IEEE80211_MICBUF_SIZE);
117 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
118 		}
119 	} else {
120 		ik.ik_keyrsc = 0;
121 		ik.ik_keytsc = 0;
122 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
123 	}
124 	if (ni != NULL)
125 		ieee80211_free_node(ni);
126 	return copyout(&ik, ireq->i_data, sizeof(ik));
127 }
128 
129 static int
130 ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
131 {
132 	struct ieee80211com *ic = vap->iv_ic;
133 
134 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
135 		ireq->i_len = sizeof(ic->ic_chan_active);
136 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
137 }
138 
139 static int
140 ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
141 {
142 	struct ieee80211com *ic = vap->iv_ic;
143 	uint32_t space;
144 
145 	space = __offsetof(struct ieee80211req_chaninfo,
146 			ic_chans[ic->ic_nchans]);
147 	if (space > ireq->i_len)
148 		space = ireq->i_len;
149 	/* XXX assumes compatible layout */
150 	return copyout(&ic->ic_nchans, ireq->i_data, space);
151 }
152 
153 static int
154 ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
155 	struct ieee80211req *ireq, int req)
156 {
157 	struct ieee80211_node *ni;
158 	struct ieee80211req_wpaie2 *wpaie;
159 	int error;
160 
161 	if (ireq->i_len < IEEE80211_ADDR_LEN)
162 		return EINVAL;
163 	wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP,
164 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
165 	if (wpaie == NULL)
166 		return ENOMEM;
167 	error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN);
168 	if (error != 0)
169 		goto bad;
170 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr);
171 	if (ni == NULL) {
172 		error = ENOENT;
173 		goto bad;
174 	}
175 	if (ni->ni_ies.wpa_ie != NULL) {
176 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
177 		if (ielen > sizeof(wpaie->wpa_ie))
178 			ielen = sizeof(wpaie->wpa_ie);
179 		memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen);
180 	}
181 	if (req == IEEE80211_IOC_WPAIE2) {
182 		if (ni->ni_ies.rsn_ie != NULL) {
183 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
184 			if (ielen > sizeof(wpaie->rsn_ie))
185 				ielen = sizeof(wpaie->rsn_ie);
186 			memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen);
187 		}
188 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
189 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
190 	} else {
191 		/* compatibility op, may overwrite wpa ie */
192 		/* XXX check ic_flags? */
193 		if (ni->ni_ies.rsn_ie != NULL) {
194 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
195 			if (ielen > sizeof(wpaie->wpa_ie))
196 				ielen = sizeof(wpaie->wpa_ie);
197 			memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen);
198 		}
199 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
200 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
201 	}
202 	ieee80211_free_node(ni);
203 	error = copyout(wpaie, ireq->i_data, ireq->i_len);
204 bad:
205 	IEEE80211_FREE(wpaie, M_TEMP);
206 	return error;
207 }
208 
209 static int
210 ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
211 {
212 	struct ieee80211_node *ni;
213 	uint8_t macaddr[IEEE80211_ADDR_LEN];
214 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
215 	int error;
216 
217 	if (ireq->i_len < off)
218 		return EINVAL;
219 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
220 	if (error != 0)
221 		return error;
222 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
223 	if (ni == NULL)
224 		return ENOENT;
225 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
226 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
227 	/* NB: copy out only the statistics */
228 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
229 			ireq->i_len - off);
230 	ieee80211_free_node(ni);
231 	return error;
232 }
233 
234 struct scanreq {
235 	struct ieee80211req_scan_result *sr;
236 	size_t space;
237 };
238 
239 static size_t
240 scan_space(const struct ieee80211_scan_entry *se, int *ielen)
241 {
242 	size_t len;
243 
244 	*ielen = se->se_ies.len;
245 	/*
246 	 * NB: ie's can be no more than 255 bytes and the max 802.11
247 	 * packet is <3Kbytes so we are sure this doesn't overflow
248 	 * 16-bits; if this is a concern we can drop the ie's.
249 	 */
250 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
251 	    se->se_meshid[1] + *ielen;
252 	return roundup(len, sizeof(uint32_t));
253 }
254 
255 static void
256 get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
257 {
258 	struct scanreq *req = arg;
259 	int ielen;
260 
261 	req->space += scan_space(se, &ielen);
262 }
263 
264 static void
265 get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
266 {
267 	struct scanreq *req = arg;
268 	struct ieee80211req_scan_result *sr;
269 	int ielen, len, nr, nxr;
270 	uint8_t *cp;
271 
272 	len = scan_space(se, &ielen);
273 	if (len > req->space)
274 		return;
275 
276 	sr = req->sr;
277 	KASSERT(len <= 65535 && ielen <= 65535,
278 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
279 	sr->isr_len = len;
280 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
281 	sr->isr_ie_len = ielen;
282 	sr->isr_freq = se->se_chan->ic_freq;
283 	sr->isr_flags = se->se_chan->ic_flags;
284 	sr->isr_rssi = se->se_rssi;
285 	sr->isr_noise = se->se_noise;
286 	sr->isr_intval = se->se_intval;
287 	sr->isr_capinfo = se->se_capinfo;
288 	sr->isr_erp = se->se_erp;
289 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
290 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
291 	memcpy(sr->isr_rates, se->se_rates+2, nr);
292 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
293 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
294 	sr->isr_nrates = nr + nxr;
295 
296 	/* copy SSID */
297 	sr->isr_ssid_len = se->se_ssid[1];
298 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
299 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
300 
301 	/* copy mesh id */
302 	cp += sr->isr_ssid_len;
303 	sr->isr_meshid_len = se->se_meshid[1];
304 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
305 	cp += sr->isr_meshid_len;
306 
307 	if (ielen)
308 		memcpy(cp, se->se_ies.data, ielen);
309 
310 	req->space -= len;
311 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
312 }
313 
314 static int
315 ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
316 	struct ieee80211req *ireq)
317 {
318 	struct scanreq req;
319 	int error;
320 
321 	if (ireq->i_len < sizeof(struct scanreq))
322 		return EFAULT;
323 
324 	error = 0;
325 	req.space = 0;
326 	ieee80211_scan_iterate(vap, get_scan_space, &req);
327 	if (req.space > ireq->i_len)
328 		req.space = ireq->i_len;
329 	if (req.space > 0) {
330 		uint32_t space;
331 		void *p;
332 
333 		space = req.space;
334 		/* XXX IEEE80211_M_WAITOK after driver lock released */
335 		p = IEEE80211_MALLOC(space, M_TEMP,
336 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
337 		if (p == NULL)
338 			return ENOMEM;
339 		req.sr = p;
340 		ieee80211_scan_iterate(vap, get_scan_result, &req);
341 		ireq->i_len = space - req.space;
342 		error = copyout(p, ireq->i_data, ireq->i_len);
343 		IEEE80211_FREE(p, M_TEMP);
344 	} else
345 		ireq->i_len = 0;
346 
347 	return error;
348 }
349 
350 struct stainforeq {
351 	struct ieee80211req_sta_info *si;
352 	size_t	space;
353 };
354 
355 static size_t
356 sta_space(const struct ieee80211_node *ni, size_t *ielen)
357 {
358 	*ielen = ni->ni_ies.len;
359 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
360 		      sizeof(uint32_t));
361 }
362 
363 static void
364 get_sta_space(void *arg, struct ieee80211_node *ni)
365 {
366 	struct stainforeq *req = arg;
367 	size_t ielen;
368 
369 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
370 	    ni->ni_associd == 0)	/* only associated stations */
371 		return;
372 	req->space += sta_space(ni, &ielen);
373 }
374 
375 static void
376 get_sta_info(void *arg, struct ieee80211_node *ni)
377 {
378 	struct stainforeq *req = arg;
379 	struct ieee80211vap *vap = ni->ni_vap;
380 	struct ieee80211req_sta_info *si;
381 	size_t ielen, len;
382 	uint8_t *cp;
383 
384 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
385 	    ni->ni_associd == 0)	/* only associated stations */
386 		return;
387 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
388 		return;
389 	len = sta_space(ni, &ielen);
390 	if (len > req->space)
391 		return;
392 	si = req->si;
393 	si->isi_len = len;
394 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
395 	si->isi_ie_len = ielen;
396 	si->isi_freq = ni->ni_chan->ic_freq;
397 	si->isi_flags = ni->ni_chan->ic_flags;
398 	si->isi_state = ni->ni_flags;
399 	si->isi_authmode = ni->ni_authmode;
400 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
401 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
402 	si->isi_capinfo = ni->ni_capinfo;
403 	si->isi_erp = ni->ni_erp;
404 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
405 	si->isi_nrates = ni->ni_rates.rs_nrates;
406 	if (si->isi_nrates > 15)
407 		si->isi_nrates = 15;
408 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
409 	si->isi_txrate = ni->ni_txrate;
410 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
411 		const struct ieee80211_mcs_rates *mcs =
412 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
413 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
414 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
415 				si->isi_txmbps = mcs->ht40_rate_800ns;
416 			else
417 				si->isi_txmbps = mcs->ht40_rate_400ns;
418 		} else {
419 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
420 				si->isi_txmbps = mcs->ht20_rate_800ns;
421 			else
422 				si->isi_txmbps = mcs->ht20_rate_400ns;
423 		}
424 	} else
425 		si->isi_txmbps = si->isi_txrate;
426 	si->isi_associd = ni->ni_associd;
427 	si->isi_txpower = ni->ni_txpower;
428 	si->isi_vlan = ni->ni_vlan;
429 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
430 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
431 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
432 	} else {
433 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
434 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
435 	}
436 	/* NB: leave all cases in case we relax ni_associd == 0 check */
437 	if (ieee80211_node_is_authorized(ni))
438 		si->isi_inact = vap->iv_inact_run;
439 	else if (ni->ni_associd != 0 ||
440 	    (vap->iv_opmode == IEEE80211_M_WDS &&
441 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
442 		si->isi_inact = vap->iv_inact_auth;
443 	else
444 		si->isi_inact = vap->iv_inact_init;
445 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
446 	si->isi_localid = ni->ni_mllid;
447 	si->isi_peerid = ni->ni_mlpid;
448 	si->isi_peerstate = ni->ni_mlstate;
449 
450 	if (ielen) {
451 		cp = ((uint8_t *)si) + si->isi_ie_off;
452 		memcpy(cp, ni->ni_ies.data, ielen);
453 	}
454 
455 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
456 	req->space -= len;
457 }
458 
459 static int
460 getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
461 	struct ieee80211_node *ni, size_t off)
462 {
463 	struct ieee80211com *ic = vap->iv_ic;
464 	struct stainforeq req;
465 	size_t space;
466 	void *p;
467 	int error;
468 
469 	error = 0;
470 	req.space = 0;
471 	if (ni == NULL) {
472 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space,
473 		    &req);
474 	} else
475 		get_sta_space(&req, ni);
476 	if (req.space > ireq->i_len)
477 		req.space = ireq->i_len;
478 	if (req.space > 0) {
479 		space = req.space;
480 		/* XXX IEEE80211_M_WAITOK after driver lock released */
481 		p = IEEE80211_MALLOC(space, M_TEMP,
482 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
483 		if (p == NULL) {
484 			error = ENOMEM;
485 			goto bad;
486 		}
487 		req.si = p;
488 		if (ni == NULL) {
489 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
490 			    get_sta_info, &req);
491 		} else
492 			get_sta_info(&req, ni);
493 		ireq->i_len = space - req.space;
494 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
495 		IEEE80211_FREE(p, M_TEMP);
496 	} else
497 		ireq->i_len = 0;
498 bad:
499 	if (ni != NULL)
500 		ieee80211_free_node(ni);
501 	return error;
502 }
503 
504 static int
505 ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
506 {
507 	uint8_t macaddr[IEEE80211_ADDR_LEN];
508 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
509 	struct ieee80211_node *ni;
510 	int error;
511 
512 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
513 		return EFAULT;
514 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
515 	if (error != 0)
516 		return error;
517 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
518 		ni = NULL;
519 	} else {
520 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
521 		if (ni == NULL)
522 			return ENOENT;
523 	}
524 	return getstainfo_common(vap, ireq, ni, off);
525 }
526 
527 static int
528 ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
529 {
530 	struct ieee80211_node *ni;
531 	struct ieee80211req_sta_txpow txpow;
532 	int error;
533 
534 	if (ireq->i_len != sizeof(txpow))
535 		return EINVAL;
536 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
537 	if (error != 0)
538 		return error;
539 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
540 	if (ni == NULL)
541 		return ENOENT;
542 	txpow.it_txpow = ni->ni_txpower;
543 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
544 	ieee80211_free_node(ni);
545 	return error;
546 }
547 
548 static int
549 ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
550 {
551 	struct ieee80211com *ic = vap->iv_ic;
552 	struct ieee80211_wme_state *wme = &ic->ic_wme;
553 	struct wmeParams *wmep;
554 	int ac;
555 
556 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
557 		return EINVAL;
558 
559 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
560 	if (ac >= WME_NUM_AC)
561 		ac = WME_AC_BE;
562 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
563 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
564 	else
565 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
566 	switch (ireq->i_type) {
567 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
568 		ireq->i_val = wmep->wmep_logcwmin;
569 		break;
570 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
571 		ireq->i_val = wmep->wmep_logcwmax;
572 		break;
573 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
574 		ireq->i_val = wmep->wmep_aifsn;
575 		break;
576 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
577 		ireq->i_val = wmep->wmep_txopLimit;
578 		break;
579 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
580 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
581 		ireq->i_val = wmep->wmep_acm;
582 		break;
583 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
584 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
585 		ireq->i_val = !wmep->wmep_noackPolicy;
586 		break;
587 	}
588 	return 0;
589 }
590 
591 static int
592 ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
593 {
594 	const struct ieee80211_aclator *acl = vap->iv_acl;
595 
596 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
597 }
598 
599 static int
600 ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
601 {
602 	struct ieee80211com *ic = vap->iv_ic;
603 	struct ieee80211_channel *c;
604 
605 	if (ireq->i_len != sizeof(struct ieee80211_channel))
606 		return EINVAL;
607 	/*
608 	 * vap's may have different operating channels when HT is
609 	 * in use.  When in RUN state report the vap-specific channel.
610 	 * Otherwise return curchan.
611 	 */
612 	if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
613 		c = vap->iv_bss->ni_chan;
614 	else
615 		c = ic->ic_curchan;
616 	return copyout(c, ireq->i_data, sizeof(*c));
617 }
618 
619 static int
620 getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
621 {
622 	if (aie == NULL)
623 		return EINVAL;
624 	/* NB: truncate, caller can check length */
625 	if (ireq->i_len > aie->ie_len)
626 		ireq->i_len = aie->ie_len;
627 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
628 }
629 
630 static int
631 ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
632 {
633 	uint8_t fc0;
634 
635 	fc0 = ireq->i_val & 0xff;
636 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
637 		return EINVAL;
638 	/* NB: could check iv_opmode and reject but hardly worth the effort */
639 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
640 	case IEEE80211_FC0_SUBTYPE_BEACON:
641 		return getappie(vap->iv_appie_beacon, ireq);
642 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
643 		return getappie(vap->iv_appie_proberesp, ireq);
644 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
645 		return getappie(vap->iv_appie_assocresp, ireq);
646 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
647 		return getappie(vap->iv_appie_probereq, ireq);
648 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
649 		return getappie(vap->iv_appie_assocreq, ireq);
650 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
651 		return getappie(vap->iv_appie_wpa, ireq);
652 	}
653 	return EINVAL;
654 }
655 
656 static int
657 ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
658 	const struct ieee80211req *ireq)
659 {
660 	struct ieee80211com *ic = vap->iv_ic;
661 
662 	if (ireq->i_len != sizeof(ic->ic_regdomain))
663 		return EINVAL;
664 	return copyout(&ic->ic_regdomain, ireq->i_data,
665 	    sizeof(ic->ic_regdomain));
666 }
667 
668 static int
669 ieee80211_ioctl_getroam(struct ieee80211vap *vap,
670 	const struct ieee80211req *ireq)
671 {
672 	size_t len = ireq->i_len;
673 	/* NB: accept short requests for backwards compat */
674 	if (len > sizeof(vap->iv_roamparms))
675 		len = sizeof(vap->iv_roamparms);
676 	return copyout(vap->iv_roamparms, ireq->i_data, len);
677 }
678 
679 static int
680 ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
681 	const struct ieee80211req *ireq)
682 {
683 	size_t len = ireq->i_len;
684 	/* NB: accept short requests for backwards compat */
685 	if (len > sizeof(vap->iv_txparms))
686 		len = sizeof(vap->iv_txparms);
687 	return copyout(vap->iv_txparms, ireq->i_data, len);
688 }
689 
690 static int
691 ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
692 	const struct ieee80211req *ireq)
693 {
694 	struct ieee80211_devcaps_req *dc;
695 	struct ieee80211req_chaninfo *ci;
696 	int maxchans, error;
697 
698 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
699 	    sizeof(struct ieee80211_channel));
700 	/* NB: require 1 so we know ic_nchans is accessible */
701 	if (maxchans < 1)
702 		return EINVAL;
703 	/* constrain max request size, 2K channels is ~24Kbytes */
704 	if (maxchans > 2048)
705 		maxchans = 2048;
706 	dc = (struct ieee80211_devcaps_req *)
707 	    IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP,
708 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
709 	if (dc == NULL)
710 		return ENOMEM;
711 	dc->dc_drivercaps = ic->ic_caps;
712 	dc->dc_cryptocaps = ic->ic_cryptocaps;
713 	dc->dc_htcaps = ic->ic_htcaps;
714 	dc->dc_vhtcaps = ic->ic_vht_cap.vht_cap_info;
715 	ci = &dc->dc_chaninfo;
716 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
717 	KASSERT(ci->ic_nchans <= maxchans,
718 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
719 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
720 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
721 	IEEE80211_FREE(dc, M_TEMP);
722 	return error;
723 }
724 
725 static int
726 ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
727 {
728 	struct ieee80211_node *ni;
729 	struct ieee80211req_sta_vlan vlan;
730 	int error;
731 
732 	if (ireq->i_len != sizeof(vlan))
733 		return EINVAL;
734 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
735 	if (error != 0)
736 		return error;
737 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
738 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
739 		    vlan.sv_macaddr);
740 		if (ni == NULL)
741 			return ENOENT;
742 	} else
743 		ni = ieee80211_ref_node(vap->iv_bss);
744 	vlan.sv_vlan = ni->ni_vlan;
745 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
746 	ieee80211_free_node(ni);
747 	return error;
748 }
749 
750 /*
751  * Dummy ioctl get handler so the linker set is defined.
752  */
753 static int
754 dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
755 {
756 	return ENOSYS;
757 }
758 IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
759 
760 static int
761 ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
762 {
763 	ieee80211_ioctl_getfunc * const *get;
764 	int error;
765 
766 	SET_FOREACH(get, ieee80211_ioctl_getset) {
767 		error = (*get)(vap, ireq);
768 		if (error != ENOSYS)
769 			return error;
770 	}
771 	return EINVAL;
772 }
773 
774 static int
775 ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
776     struct ieee80211req *ireq)
777 {
778 	struct ieee80211com *ic = vap->iv_ic;
779 	u_int kid, len;
780 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
781 	char tmpssid[IEEE80211_NWID_LEN];
782 	int error = 0;
783 
784 	switch (ireq->i_type) {
785 	case IEEE80211_IOC_IC_NAME:
786 		len = strlen(ic->ic_name) + 1;
787 		if (len > ireq->i_len)
788 			return (EINVAL);
789 		ireq->i_len = len;
790 		error = copyout(ic->ic_name, ireq->i_data, ireq->i_len);
791 		break;
792 	case IEEE80211_IOC_SSID:
793 		switch (vap->iv_state) {
794 		case IEEE80211_S_INIT:
795 		case IEEE80211_S_SCAN:
796 			ireq->i_len = vap->iv_des_ssid[0].len;
797 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
798 			break;
799 		default:
800 			ireq->i_len = vap->iv_bss->ni_esslen;
801 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
802 			break;
803 		}
804 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
805 		break;
806 	case IEEE80211_IOC_NUMSSIDS:
807 		ireq->i_val = 1;
808 		break;
809 	case IEEE80211_IOC_WEP:
810 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
811 			ireq->i_val = IEEE80211_WEP_OFF;
812 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
813 			ireq->i_val = IEEE80211_WEP_ON;
814 		else
815 			ireq->i_val = IEEE80211_WEP_MIXED;
816 		break;
817 	case IEEE80211_IOC_WEPKEY:
818 		kid = (u_int) ireq->i_val;
819 		if (kid >= IEEE80211_WEP_NKID)
820 			return EINVAL;
821 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
822 		/* NB: only root can read WEP keys */
823 		if (ieee80211_priv_check_vap_getkey(cmd, vap, NULL) == 0) {
824 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
825 		} else {
826 			bzero(tmpkey, len);
827 		}
828 		ireq->i_len = len;
829 		error = copyout(tmpkey, ireq->i_data, len);
830 		break;
831 	case IEEE80211_IOC_NUMWEPKEYS:
832 		ireq->i_val = IEEE80211_WEP_NKID;
833 		break;
834 	case IEEE80211_IOC_WEPTXKEY:
835 		ireq->i_val = vap->iv_def_txkey;
836 		break;
837 	case IEEE80211_IOC_AUTHMODE:
838 		if (vap->iv_flags & IEEE80211_F_WPA)
839 			ireq->i_val = IEEE80211_AUTH_WPA;
840 		else
841 			ireq->i_val = vap->iv_bss->ni_authmode;
842 		break;
843 	case IEEE80211_IOC_CHANNEL:
844 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
845 		break;
846 	case IEEE80211_IOC_POWERSAVE:
847 		if (vap->iv_flags & IEEE80211_F_PMGTON)
848 			ireq->i_val = IEEE80211_POWERSAVE_ON;
849 		else
850 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
851 		break;
852 	case IEEE80211_IOC_POWERSAVESLEEP:
853 		ireq->i_val = ic->ic_lintval;
854 		break;
855 	case IEEE80211_IOC_RTSTHRESHOLD:
856 		ireq->i_val = vap->iv_rtsthreshold;
857 		break;
858 	case IEEE80211_IOC_PROTMODE:
859 		ireq->i_val = vap->iv_protmode;
860 		break;
861 	case IEEE80211_IOC_TXPOWER:
862 		/*
863 		 * Tx power limit is the min of max regulatory
864 		 * power, any user-set limit, and the max the
865 		 * radio can do.
866 		 *
867 		 * TODO: methodize this
868 		 */
869 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
870 		if (ireq->i_val > ic->ic_txpowlimit)
871 			ireq->i_val = ic->ic_txpowlimit;
872 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
873 			ireq->i_val = ic->ic_curchan->ic_maxpower;
874 		break;
875 	case IEEE80211_IOC_WPA:
876 		switch (vap->iv_flags & IEEE80211_F_WPA) {
877 		case IEEE80211_F_WPA1:
878 			ireq->i_val = 1;
879 			break;
880 		case IEEE80211_F_WPA2:
881 			ireq->i_val = 2;
882 			break;
883 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
884 			ireq->i_val = 3;
885 			break;
886 		default:
887 			ireq->i_val = 0;
888 			break;
889 		}
890 		break;
891 	case IEEE80211_IOC_CHANLIST:
892 		error = ieee80211_ioctl_getchanlist(vap, ireq);
893 		break;
894 	case IEEE80211_IOC_ROAMING:
895 		ireq->i_val = vap->iv_roaming;
896 		break;
897 	case IEEE80211_IOC_PRIVACY:
898 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
899 		break;
900 	case IEEE80211_IOC_DROPUNENCRYPTED:
901 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
902 		break;
903 	case IEEE80211_IOC_COUNTERMEASURES:
904 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
905 		break;
906 	case IEEE80211_IOC_WME:
907 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
908 		break;
909 	case IEEE80211_IOC_HIDESSID:
910 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
911 		break;
912 	case IEEE80211_IOC_APBRIDGE:
913 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
914 		break;
915 	case IEEE80211_IOC_WPAKEY:
916 		error = ieee80211_ioctl_getkey(cmd, vap, ireq);
917 		break;
918 	case IEEE80211_IOC_CHANINFO:
919 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
920 		break;
921 	case IEEE80211_IOC_BSSID:
922 		if (ireq->i_len != IEEE80211_ADDR_LEN)
923 			return EINVAL;
924 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
925 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
926 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
927 			    ireq->i_data, ireq->i_len);
928 		} else
929 			error = copyout(vap->iv_des_bssid, ireq->i_data,
930 			    ireq->i_len);
931 		break;
932 	case IEEE80211_IOC_WPAIE:
933 	case IEEE80211_IOC_WPAIE2:
934 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
935 		break;
936 	case IEEE80211_IOC_SCAN_RESULTS:
937 		error = ieee80211_ioctl_getscanresults(vap, ireq);
938 		break;
939 	case IEEE80211_IOC_STA_STATS:
940 		error = ieee80211_ioctl_getstastats(vap, ireq);
941 		break;
942 	case IEEE80211_IOC_TXPOWMAX:
943 		ireq->i_val = vap->iv_bss->ni_txpower;
944 		break;
945 	case IEEE80211_IOC_STA_TXPOW:
946 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
947 		break;
948 	case IEEE80211_IOC_STA_INFO:
949 		error = ieee80211_ioctl_getstainfo(vap, ireq);
950 		break;
951 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
952 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
953 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
954 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
955 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
956 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
957 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
958 		break;
959 	case IEEE80211_IOC_DTIM_PERIOD:
960 		ireq->i_val = vap->iv_dtim_period;
961 		break;
962 	case IEEE80211_IOC_BEACON_INTERVAL:
963 		/* NB: get from ic_bss for station mode */
964 		ireq->i_val = vap->iv_bss->ni_intval;
965 		break;
966 	case IEEE80211_IOC_PUREG:
967 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
968 		break;
969 	case IEEE80211_IOC_QUIET:
970 		ireq->i_val = vap->iv_quiet;
971 		break;
972 	case IEEE80211_IOC_QUIET_COUNT:
973 		ireq->i_val = vap->iv_quiet_count;
974 		break;
975 	case IEEE80211_IOC_QUIET_PERIOD:
976 		ireq->i_val = vap->iv_quiet_period;
977 		break;
978 	case IEEE80211_IOC_QUIET_DUR:
979 		ireq->i_val = vap->iv_quiet_duration;
980 		break;
981 	case IEEE80211_IOC_QUIET_OFFSET:
982 		ireq->i_val = vap->iv_quiet_offset;
983 		break;
984 	case IEEE80211_IOC_BGSCAN:
985 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
986 		break;
987 	case IEEE80211_IOC_BGSCAN_IDLE:
988 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
989 		break;
990 	case IEEE80211_IOC_BGSCAN_INTERVAL:
991 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
992 		break;
993 	case IEEE80211_IOC_SCANVALID:
994 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
995 		break;
996 	case IEEE80211_IOC_FRAGTHRESHOLD:
997 		ireq->i_val = vap->iv_fragthreshold;
998 		break;
999 	case IEEE80211_IOC_MACCMD:
1000 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
1001 		break;
1002 	case IEEE80211_IOC_BURST:
1003 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
1004 		break;
1005 	case IEEE80211_IOC_BMISSTHRESHOLD:
1006 		ireq->i_val = vap->iv_bmissthreshold;
1007 		break;
1008 	case IEEE80211_IOC_CURCHAN:
1009 		error = ieee80211_ioctl_getcurchan(vap, ireq);
1010 		break;
1011 	case IEEE80211_IOC_SHORTGI:
1012 		ireq->i_val = 0;
1013 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1014 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
1015 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1016 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
1017 		break;
1018 	case IEEE80211_IOC_AMPDU:
1019 		ireq->i_val = 0;
1020 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
1021 			ireq->i_val |= 1;
1022 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
1023 			ireq->i_val |= 2;
1024 		break;
1025 	case IEEE80211_IOC_AMPDU_LIMIT:
1026 		/* XXX TODO: make this a per-node thing; and leave this as global */
1027 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1028 			ireq->i_val = vap->iv_ampdu_rxmax;
1029 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
1030 			/*
1031 			 * XXX TODO: this isn't completely correct, as we've
1032 			 * negotiated the higher of the two.
1033 			 */
1034 			ireq->i_val = _IEEE80211_MASKSHIFT( vap->iv_bss->ni_htparam,
1035 			    IEEE80211_HTCAP_MAXRXAMPDU);
1036 		else
1037 			ireq->i_val = vap->iv_ampdu_limit;
1038 		break;
1039 	case IEEE80211_IOC_AMPDU_DENSITY:
1040 		/* XXX TODO: make this a per-node thing; and leave this as global */
1041 		if (vap->iv_opmode == IEEE80211_M_STA &&
1042 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1043 			/*
1044 			 * XXX TODO: this isn't completely correct, as we've
1045 			 * negotiated the higher of the two.
1046 			 */
1047 			ireq->i_val = _IEEE80211_MASKSHIFT(vap->iv_bss->ni_htparam,
1048 			    IEEE80211_HTCAP_MPDUDENSITY);
1049 		else
1050 			ireq->i_val = vap->iv_ampdu_density;
1051 		break;
1052 	case IEEE80211_IOC_AMSDU:
1053 		ireq->i_val = 0;
1054 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
1055 			ireq->i_val |= 1;
1056 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
1057 			ireq->i_val |= 2;
1058 		break;
1059 	case IEEE80211_IOC_AMSDU_LIMIT:
1060 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1061 		break;
1062 	case IEEE80211_IOC_PUREN:
1063 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
1064 		break;
1065 	case IEEE80211_IOC_DOTH:
1066 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1067 		break;
1068 	case IEEE80211_IOC_REGDOMAIN:
1069 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1070 		break;
1071 	case IEEE80211_IOC_ROAM:
1072 		error = ieee80211_ioctl_getroam(vap, ireq);
1073 		break;
1074 	case IEEE80211_IOC_TXPARAMS:
1075 		error = ieee80211_ioctl_gettxparams(vap, ireq);
1076 		break;
1077 	case IEEE80211_IOC_HTCOMPAT:
1078 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1079 		break;
1080 	case IEEE80211_IOC_DWDS:
1081 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1082 		break;
1083 	case IEEE80211_IOC_INACTIVITY:
1084 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1085 		break;
1086 	case IEEE80211_IOC_APPIE:
1087 		error = ieee80211_ioctl_getappie(vap, ireq);
1088 		break;
1089 	case IEEE80211_IOC_WPS:
1090 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1091 		break;
1092 	case IEEE80211_IOC_TSN:
1093 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1094 		break;
1095 	case IEEE80211_IOC_DFS:
1096 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1097 		break;
1098 	case IEEE80211_IOC_DOTD:
1099 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1100 		break;
1101 	case IEEE80211_IOC_DEVCAPS:
1102 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1103 		break;
1104 	case IEEE80211_IOC_HTPROTMODE:
1105 		ireq->i_val = vap->iv_htprotmode;
1106 		break;
1107 	case IEEE80211_IOC_HTCONF:
1108 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
1109 			ireq->i_val = 1;
1110 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
1111 				ireq->i_val |= 2;
1112 		} else
1113 			ireq->i_val = 0;
1114 		break;
1115 	case IEEE80211_IOC_STA_VLAN:
1116 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1117 		break;
1118 	case IEEE80211_IOC_SMPS:
1119 		if (vap->iv_opmode == IEEE80211_M_STA &&
1120 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
1121 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1122 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1123 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1124 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1125 			else
1126 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1127 		} else
1128 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1129 		break;
1130 	case IEEE80211_IOC_RIFS:
1131 		if (vap->iv_opmode == IEEE80211_M_STA &&
1132 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1133 			ireq->i_val =
1134 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1135 		else
1136 			ireq->i_val =
1137 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
1138 		break;
1139 	case IEEE80211_IOC_STBC:
1140 		ireq->i_val = 0;
1141 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
1142 			ireq->i_val |= 1;
1143 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
1144 			ireq->i_val |= 2;
1145 		break;
1146 	case IEEE80211_IOC_LDPC:
1147 		ireq->i_val = 0;
1148 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)
1149 			ireq->i_val |= 1;
1150 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX)
1151 			ireq->i_val |= 2;
1152 		break;
1153 	case IEEE80211_IOC_UAPSD:
1154 		ireq->i_val = 0;
1155 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
1156 			ireq->i_val = 1;
1157 		break;
1158 	case IEEE80211_IOC_VHTCONF:
1159 		ireq->i_val = vap->iv_vht_flags & IEEE80211_FVHT_MASK;
1160 		break;
1161 	default:
1162 		error = ieee80211_ioctl_getdefault(vap, ireq);
1163 		break;
1164 	}
1165 	return error;
1166 }
1167 
1168 static int
1169 ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1170 {
1171 	struct ieee80211req_key ik;
1172 	struct ieee80211_node *ni;
1173 	struct ieee80211_key *wk;
1174 	uint16_t kid;
1175 	int error, i;
1176 
1177 	if (ireq->i_len != sizeof(ik))
1178 		return EINVAL;
1179 	error = copyin(ireq->i_data, &ik, sizeof(ik));
1180 	if (error)
1181 		return error;
1182 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1183 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1184 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1185 		return E2BIG;
1186 	kid = ik.ik_keyix;
1187 	if (kid == IEEE80211_KEYIX_NONE) {
1188 		/* XXX unicast keys currently must be tx/rx */
1189 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1190 			return EINVAL;
1191 		if (vap->iv_opmode == IEEE80211_M_STA) {
1192 			ni = ieee80211_ref_node(vap->iv_bss);
1193 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1194 				ieee80211_free_node(ni);
1195 				return EADDRNOTAVAIL;
1196 			}
1197 		} else {
1198 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1199 				ik.ik_macaddr);
1200 			if (ni == NULL)
1201 				return ENOENT;
1202 		}
1203 		wk = &ni->ni_ucastkey;
1204 	} else {
1205 		if (kid >= IEEE80211_WEP_NKID)
1206 			return EINVAL;
1207 		wk = &vap->iv_nw_keys[kid];
1208 		/*
1209 		 * Global slots start off w/o any assigned key index.
1210 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1211 		 */
1212 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1213 			wk->wk_keyix = kid;
1214 		ni = NULL;
1215 	}
1216 	error = 0;
1217 	ieee80211_key_update_begin(vap);
1218 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1219 		wk->wk_keylen = ik.ik_keylen;
1220 		/* NB: MIC presence is implied by cipher type */
1221 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1222 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1223 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1224 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1225 		wk->wk_keytsc = 0;			/* new key, reset */
1226 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1227 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1228 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1229 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1230 		if (!ieee80211_crypto_setkey(vap, wk))
1231 			error = EIO;
1232 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1233 			/*
1234 			 * Inform the driver that this is the default
1235 			 * transmit key.  Now, ideally we'd just set
1236 			 * a flag in the key update that would
1237 			 * say "yes, we're the default key", but
1238 			 * that currently isn't the way the ioctl ->
1239 			 * key interface works.
1240 			 */
1241 			ieee80211_crypto_set_deftxkey(vap, kid);
1242 	} else
1243 		error = ENXIO;
1244 	ieee80211_key_update_end(vap);
1245 	if (ni != NULL)
1246 		ieee80211_free_node(ni);
1247 	return error;
1248 }
1249 
1250 static int
1251 ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1252 {
1253 	struct ieee80211req_del_key dk;
1254 	int kid, error;
1255 
1256 	if (ireq->i_len != sizeof(dk))
1257 		return EINVAL;
1258 	error = copyin(ireq->i_data, &dk, sizeof(dk));
1259 	if (error)
1260 		return error;
1261 	kid = dk.idk_keyix;
1262 	/* XXX uint8_t -> uint16_t */
1263 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1264 		struct ieee80211_node *ni;
1265 
1266 		if (vap->iv_opmode == IEEE80211_M_STA) {
1267 			ni = ieee80211_ref_node(vap->iv_bss);
1268 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1269 				ieee80211_free_node(ni);
1270 				return EADDRNOTAVAIL;
1271 			}
1272 		} else {
1273 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1274 				dk.idk_macaddr);
1275 			if (ni == NULL)
1276 				return ENOENT;
1277 		}
1278 		/* XXX error return */
1279 		ieee80211_node_delucastkey(ni);
1280 		ieee80211_free_node(ni);
1281 	} else {
1282 		if (kid >= IEEE80211_WEP_NKID)
1283 			return EINVAL;
1284 		/* XXX error return */
1285 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1286 	}
1287 	return 0;
1288 }
1289 
1290 struct mlmeop {
1291 	struct ieee80211vap *vap;
1292 	int	op;
1293 	int	reason;
1294 };
1295 
1296 static void
1297 mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1298 	int op, int reason)
1299 {
1300 #ifdef IEEE80211_DEBUG
1301 	static const struct {
1302 		int mask;
1303 		const char *opstr;
1304 	} ops[] = {
1305 		{ 0, "op#0" },
1306 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1307 		  IEEE80211_MSG_ASSOC, "assoc" },
1308 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1309 		  IEEE80211_MSG_ASSOC, "disassoc" },
1310 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1311 		  IEEE80211_MSG_AUTH, "deauth" },
1312 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1313 		  IEEE80211_MSG_AUTH, "authorize" },
1314 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1315 		  IEEE80211_MSG_AUTH, "unauthorize" },
1316 	};
1317 
1318 	if (op == IEEE80211_MLME_AUTH) {
1319 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1320 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1321 		    "station authenticate %s via MLME (reason: %d (%s))",
1322 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1323 		    reason, ieee80211_reason_to_string(reason));
1324 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1325 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1326 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
1327 		    ieee80211_reason_to_string(reason));
1328 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1329 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1330 		    "station %s via MLME", ops[op].opstr);
1331 	} else {
1332 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1333 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
1334 		    reason, ieee80211_reason_to_string(reason));
1335 	}
1336 #endif /* IEEE80211_DEBUG */
1337 }
1338 
1339 static void
1340 domlme(void *arg, struct ieee80211_node *ni)
1341 {
1342 	struct mlmeop *mop = arg;
1343 	struct ieee80211vap *vap = ni->ni_vap;
1344 
1345 	if (vap != mop->vap)
1346 		return;
1347 	/*
1348 	 * NB: if ni_associd is zero then the node is already cleaned
1349 	 * up and we don't need to do this (we're safely holding a
1350 	 * reference but should otherwise not modify it's state).
1351 	 */
1352 	if (ni->ni_associd == 0)
1353 		return;
1354 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1355 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1356 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1357 		    mop->reason);
1358 	} else {
1359 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1360 		    mop->reason);
1361 	}
1362 	ieee80211_node_leave(ni);
1363 }
1364 
1365 static int
1366 setmlme_dropsta(struct ieee80211vap *vap,
1367 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1368 {
1369 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1370 	struct ieee80211_node *ni;
1371 	int error = 0;
1372 
1373 	/* NB: the broadcast address means do 'em all */
1374 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
1375 		IEEE80211_NODE_LOCK(nt);
1376 		ni = ieee80211_find_node_locked(nt, mac);
1377 		IEEE80211_NODE_UNLOCK(nt);
1378 		/*
1379 		 * Don't do the node update inside the node
1380 		 * table lock.  This unfortunately causes LORs
1381 		 * with drivers and their TX paths.
1382 		 */
1383 		if (ni != NULL) {
1384 			domlme(mlmeop, ni);
1385 			ieee80211_free_node(ni);
1386 		} else
1387 			error = ENOENT;
1388 	} else {
1389 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1390 	}
1391 	return error;
1392 }
1393 
1394 static int
1395 setmlme_common(struct ieee80211vap *vap, int op,
1396 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1397 {
1398 	struct ieee80211com *ic = vap->iv_ic;
1399 	struct ieee80211_node_table *nt = &ic->ic_sta;
1400 	struct ieee80211_node *ni;
1401 	struct mlmeop mlmeop;
1402 	int error;
1403 
1404 	error = 0;
1405 	switch (op) {
1406 	case IEEE80211_MLME_DISASSOC:
1407 	case IEEE80211_MLME_DEAUTH:
1408 		switch (vap->iv_opmode) {
1409 		case IEEE80211_M_STA:
1410 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1411 			/* XXX not quite right */
1412 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1413 			break;
1414 		case IEEE80211_M_HOSTAP:
1415 			mlmeop.vap = vap;
1416 			mlmeop.op = op;
1417 			mlmeop.reason = reason;
1418 			error = setmlme_dropsta(vap, mac, &mlmeop);
1419 			break;
1420 		case IEEE80211_M_WDS:
1421 			/* XXX user app should send raw frame? */
1422 			if (op != IEEE80211_MLME_DEAUTH) {
1423 				error = EINVAL;
1424 				break;
1425 			}
1426 #if 0
1427 			/* XXX accept any address, simplifies user code */
1428 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1429 				error = EINVAL;
1430 				break;
1431 			}
1432 #endif
1433 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1434 			ni = ieee80211_ref_node(vap->iv_bss);
1435 			IEEE80211_SEND_MGMT(ni,
1436 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1437 			ieee80211_free_node(ni);
1438 			break;
1439 		case IEEE80211_M_MBSS:
1440 			IEEE80211_NODE_LOCK(nt);
1441 			ni = ieee80211_find_node_locked(nt, mac);
1442 			/*
1443 			 * Don't do the node update inside the node
1444 			 * table lock.  This unfortunately causes LORs
1445 			 * with drivers and their TX paths.
1446 			 */
1447 			IEEE80211_NODE_UNLOCK(nt);
1448 			if (ni != NULL) {
1449 				ieee80211_node_leave(ni);
1450 				ieee80211_free_node(ni);
1451 			} else {
1452 				error = ENOENT;
1453 			}
1454 			break;
1455 		default:
1456 			error = EINVAL;
1457 			break;
1458 		}
1459 		break;
1460 	case IEEE80211_MLME_AUTHORIZE:
1461 	case IEEE80211_MLME_UNAUTHORIZE:
1462 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1463 		    vap->iv_opmode != IEEE80211_M_WDS) {
1464 			error = EINVAL;
1465 			break;
1466 		}
1467 		IEEE80211_NODE_LOCK(nt);
1468 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1469 		/*
1470 		 * Don't do the node update inside the node
1471 		 * table lock.  This unfortunately causes LORs
1472 		 * with drivers and their TX paths.
1473 		 */
1474 		IEEE80211_NODE_UNLOCK(nt);
1475 		if (ni != NULL) {
1476 			mlmedebug(vap, mac, op, reason);
1477 			if (op == IEEE80211_MLME_AUTHORIZE)
1478 				ieee80211_node_authorize(ni);
1479 			else
1480 				ieee80211_node_unauthorize(ni);
1481 			ieee80211_free_node(ni);
1482 		} else
1483 			error = ENOENT;
1484 		break;
1485 	case IEEE80211_MLME_AUTH:
1486 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1487 			error = EINVAL;
1488 			break;
1489 		}
1490 		IEEE80211_NODE_LOCK(nt);
1491 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1492 		/*
1493 		 * Don't do the node update inside the node
1494 		 * table lock.  This unfortunately causes LORs
1495 		 * with drivers and their TX paths.
1496 		 */
1497 		IEEE80211_NODE_UNLOCK(nt);
1498 		if (ni != NULL) {
1499 			mlmedebug(vap, mac, op, reason);
1500 			if (reason == IEEE80211_STATUS_SUCCESS) {
1501 				IEEE80211_SEND_MGMT(ni,
1502 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1503 				/*
1504 				 * For shared key auth, just continue the
1505 				 * exchange.  Otherwise when 802.1x is not in
1506 				 * use mark the port authorized at this point
1507 				 * so traffic can flow.
1508 				 */
1509 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1510 				    ni->ni_challenge == NULL)
1511 				      ieee80211_node_authorize(ni);
1512 			} else {
1513 				vap->iv_stats.is_rx_acl++;
1514 				ieee80211_send_error(ni, ni->ni_macaddr,
1515 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1516 				ieee80211_node_leave(ni);
1517 			}
1518 			ieee80211_free_node(ni);
1519 		} else
1520 			error = ENOENT;
1521 		break;
1522 	default:
1523 		error = EINVAL;
1524 		break;
1525 	}
1526 	return error;
1527 }
1528 
1529 struct scanlookup {
1530 	const uint8_t *mac;
1531 	int esslen;
1532 	const uint8_t *essid;
1533 	const struct ieee80211_scan_entry *se;
1534 };
1535 
1536 /*
1537  * Match mac address and any ssid.
1538  */
1539 static void
1540 mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1541 {
1542 	struct scanlookup *look = arg;
1543 
1544 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1545 		return;
1546 	if (look->esslen != 0) {
1547 		if (se->se_ssid[1] != look->esslen)
1548 			return;
1549 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1550 			return;
1551 	}
1552 	look->se = se;
1553 }
1554 
1555 static int
1556 setmlme_assoc_sta(struct ieee80211vap *vap,
1557 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1558 	const uint8_t ssid[IEEE80211_NWID_LEN])
1559 {
1560 	struct scanlookup lookup;
1561 
1562 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1563 	    ("expected opmode STA not %s",
1564 	    ieee80211_opmode_name[vap->iv_opmode]));
1565 
1566 	/* NB: this is racey if roaming is !manual */
1567 	lookup.se = NULL;
1568 	lookup.mac = mac;
1569 	lookup.esslen = ssid_len;
1570 	lookup.essid = ssid;
1571 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1572 	if (lookup.se == NULL)
1573 		return ENOENT;
1574 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1575 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1576 		return EIO;		/* XXX unique but could be better */
1577 	return 0;
1578 }
1579 
1580 static int
1581 setmlme_assoc_adhoc(struct ieee80211vap *vap,
1582 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1583 	const uint8_t ssid[IEEE80211_NWID_LEN])
1584 {
1585 	struct ieee80211_scan_req *sr;
1586 	int error;
1587 
1588 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1589 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1590 	    ("expected opmode IBSS or AHDEMO not %s",
1591 	    ieee80211_opmode_name[vap->iv_opmode]));
1592 
1593 	if (ssid_len == 0 || ssid_len > IEEE80211_NWID_LEN)
1594 		return EINVAL;
1595 
1596 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
1597 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1598 	if (sr == NULL)
1599 		return ENOMEM;
1600 
1601 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1602 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1603 	vap->iv_des_ssid[0].len = ssid_len;
1604 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1605 	vap->iv_des_nssid = 1;
1606 
1607 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
1608 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1609 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
1610 	sr->sr_ssid[0].len = ssid_len;
1611 	sr->sr_nssid = 1;
1612 
1613 	error = ieee80211_scanreq(vap, sr);
1614 
1615 	IEEE80211_FREE(sr, M_TEMP);
1616 	return error;
1617 }
1618 
1619 static int
1620 ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1621 {
1622 	struct ieee80211req_mlme mlme;
1623 	int error;
1624 
1625 	if (ireq->i_len != sizeof(mlme))
1626 		return EINVAL;
1627 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1628 	if (error)
1629 		return error;
1630 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1631 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1632 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1633 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1634 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1635 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1636 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1637 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1638 		    mlme.im_ssid_len, mlme.im_ssid);
1639 	else
1640 		return setmlme_common(vap, mlme.im_op,
1641 		    mlme.im_macaddr, mlme.im_reason);
1642 }
1643 
1644 static int
1645 ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1646 {
1647 	uint8_t mac[IEEE80211_ADDR_LEN];
1648 	const struct ieee80211_aclator *acl = vap->iv_acl;
1649 	int error;
1650 
1651 	if (ireq->i_len != sizeof(mac))
1652 		return EINVAL;
1653 	error = copyin(ireq->i_data, mac, ireq->i_len);
1654 	if (error)
1655 		return error;
1656 	if (acl == NULL) {
1657 		acl = ieee80211_aclator_get("mac");
1658 		if (acl == NULL || !acl->iac_attach(vap))
1659 			return EINVAL;
1660 		vap->iv_acl = acl;
1661 	}
1662 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1663 		acl->iac_add(vap, mac);
1664 	else
1665 		acl->iac_remove(vap, mac);
1666 	return 0;
1667 }
1668 
1669 static int
1670 ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1671 {
1672 	const struct ieee80211_aclator *acl = vap->iv_acl;
1673 
1674 	switch (ireq->i_val) {
1675 	case IEEE80211_MACCMD_POLICY_OPEN:
1676 	case IEEE80211_MACCMD_POLICY_ALLOW:
1677 	case IEEE80211_MACCMD_POLICY_DENY:
1678 	case IEEE80211_MACCMD_POLICY_RADIUS:
1679 		if (acl == NULL) {
1680 			acl = ieee80211_aclator_get("mac");
1681 			if (acl == NULL || !acl->iac_attach(vap))
1682 				return EINVAL;
1683 			vap->iv_acl = acl;
1684 		}
1685 		acl->iac_setpolicy(vap, ireq->i_val);
1686 		break;
1687 	case IEEE80211_MACCMD_FLUSH:
1688 		if (acl != NULL)
1689 			acl->iac_flush(vap);
1690 		/* NB: silently ignore when not in use */
1691 		break;
1692 	case IEEE80211_MACCMD_DETACH:
1693 		if (acl != NULL) {
1694 			vap->iv_acl = NULL;
1695 			acl->iac_detach(vap);
1696 		}
1697 		break;
1698 	default:
1699 		if (acl == NULL)
1700 			return EINVAL;
1701 		else
1702 			return acl->iac_setioctl(vap, ireq);
1703 	}
1704 	return 0;
1705 }
1706 
1707 static int
1708 ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1709 {
1710 	struct ieee80211com *ic = vap->iv_ic;
1711 	uint8_t *chanlist, *list;
1712 	int i, nchan, maxchan, error;
1713 
1714 	if (ireq->i_len > sizeof(ic->ic_chan_active))
1715 		ireq->i_len = sizeof(ic->ic_chan_active);
1716 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1717 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1718 	if (list == NULL)
1719 		return ENOMEM;
1720 	error = copyin(ireq->i_data, list, ireq->i_len);
1721 	if (error) {
1722 		IEEE80211_FREE(list, M_TEMP);
1723 		return error;
1724 	}
1725 	nchan = 0;
1726 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
1727 	maxchan = ireq->i_len * NBBY;
1728 	for (i = 0; i < ic->ic_nchans; i++) {
1729 		const struct ieee80211_channel *c = &ic->ic_channels[i];
1730 		/*
1731 		 * Calculate the intersection of the user list and the
1732 		 * available channels so users can do things like specify
1733 		 * 1-255 to get all available channels.
1734 		 */
1735 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
1736 			setbit(chanlist, c->ic_ieee);
1737 			nchan++;
1738 		}
1739 	}
1740 	if (nchan == 0) {
1741 		IEEE80211_FREE(list, M_TEMP);
1742 		return EINVAL;
1743 	}
1744 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1745 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1746 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1747 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1748 	ieee80211_scan_flush(vap);
1749 	IEEE80211_FREE(list, M_TEMP);
1750 	return ENETRESET;
1751 }
1752 
1753 static int
1754 ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1755 {
1756 	struct ieee80211_node *ni;
1757 	uint8_t macaddr[IEEE80211_ADDR_LEN];
1758 	int error;
1759 
1760 	/*
1761 	 * NB: we could copyin ieee80211req_sta_stats so apps
1762 	 *     could make selective changes but that's overkill;
1763 	 *     just clear all stats for now.
1764 	 */
1765 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1766 		return EINVAL;
1767 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1768 	if (error != 0)
1769 		return error;
1770 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1771 	if (ni == NULL)
1772 		return ENOENT;
1773 	/* XXX require ni_vap == vap? */
1774 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1775 	ieee80211_free_node(ni);
1776 	return 0;
1777 }
1778 
1779 static int
1780 ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1781 {
1782 	struct ieee80211_node *ni;
1783 	struct ieee80211req_sta_txpow txpow;
1784 	int error;
1785 
1786 	if (ireq->i_len != sizeof(txpow))
1787 		return EINVAL;
1788 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1789 	if (error != 0)
1790 		return error;
1791 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1792 	if (ni == NULL)
1793 		return ENOENT;
1794 	ni->ni_txpower = txpow.it_txpow;
1795 	ieee80211_free_node(ni);
1796 	return error;
1797 }
1798 
1799 static int
1800 ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1801 {
1802 	struct ieee80211com *ic = vap->iv_ic;
1803 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1804 	struct wmeParams *wmep, *chanp;
1805 	int isbss, ac, aggrmode;
1806 
1807 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1808 		return EOPNOTSUPP;
1809 
1810 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1811 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1812 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
1813 	if (ac >= WME_NUM_AC)
1814 		ac = WME_AC_BE;
1815 	if (isbss) {
1816 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1817 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1818 	} else {
1819 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1820 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1821 	}
1822 	switch (ireq->i_type) {
1823 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1824 		wmep->wmep_logcwmin = ireq->i_val;
1825 		if (!isbss || !aggrmode)
1826 			chanp->wmep_logcwmin = ireq->i_val;
1827 		break;
1828 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1829 		wmep->wmep_logcwmax = ireq->i_val;
1830 		if (!isbss || !aggrmode)
1831 			chanp->wmep_logcwmax = ireq->i_val;
1832 		break;
1833 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1834 		wmep->wmep_aifsn = ireq->i_val;
1835 		if (!isbss || !aggrmode)
1836 			chanp->wmep_aifsn = ireq->i_val;
1837 		break;
1838 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1839 		wmep->wmep_txopLimit = ireq->i_val;
1840 		if (!isbss || !aggrmode)
1841 			chanp->wmep_txopLimit = ireq->i_val;
1842 		break;
1843 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1844 		wmep->wmep_acm = ireq->i_val;
1845 		if (!aggrmode)
1846 			chanp->wmep_acm = ireq->i_val;
1847 		break;
1848 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1849 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1850 			(ireq->i_val) == 0;
1851 		break;
1852 	}
1853 	ieee80211_wme_updateparams(vap);
1854 	return 0;
1855 }
1856 
1857 static int
1858 find11gchannel(struct ieee80211com *ic, int start, int freq)
1859 {
1860 	const struct ieee80211_channel *c;
1861 	int i;
1862 
1863 	for (i = start+1; i < ic->ic_nchans; i++) {
1864 		c = &ic->ic_channels[i];
1865 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1866 			return 1;
1867 	}
1868 	/* NB: should not be needed but in case things are mis-sorted */
1869 	for (i = 0; i < start; i++) {
1870 		c = &ic->ic_channels[i];
1871 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1872 			return 1;
1873 	}
1874 	return 0;
1875 }
1876 
1877 static struct ieee80211_channel *
1878 findchannel(struct ieee80211com *ic, int ieee, int mode)
1879 {
1880 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1881 	    [IEEE80211_MODE_AUTO]	= 0,
1882 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1883 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1884 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1885 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1886 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1887 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1888 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
1889 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
1890 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
1891 	    /* NB: handled specially below */
1892 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1893 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
1894 	    [IEEE80211_MODE_VHT_5GHZ]	= IEEE80211_CHAN_A,
1895 	    [IEEE80211_MODE_VHT_2GHZ]	= IEEE80211_CHAN_G,
1896 	};
1897 	u_int modeflags;
1898 	int i;
1899 
1900 	modeflags = chanflags[mode];
1901 	for (i = 0; i < ic->ic_nchans; i++) {
1902 		struct ieee80211_channel *c = &ic->ic_channels[i];
1903 
1904 		if (c->ic_ieee != ieee)
1905 			continue;
1906 		if (mode == IEEE80211_MODE_AUTO) {
1907 			/* ignore turbo channels for autoselect */
1908 			if (IEEE80211_IS_CHAN_TURBO(c))
1909 				continue;
1910 			/*
1911 			 * XXX special-case 11b/g channels so we
1912 			 *     always select the g channel if both
1913 			 *     are present.
1914 			 * XXX prefer HT to non-HT?
1915 			 */
1916 			if (!IEEE80211_IS_CHAN_B(c) ||
1917 			    !find11gchannel(ic, i, c->ic_freq))
1918 				return c;
1919 		} else {
1920 			/* must check VHT specifically */
1921 			if ((mode == IEEE80211_MODE_VHT_5GHZ ||
1922 			    mode == IEEE80211_MODE_VHT_2GHZ) &&
1923 			    !IEEE80211_IS_CHAN_VHT(c))
1924 				continue;
1925 
1926 			/*
1927 			 * Must check HT specially - only match on HT,
1928 			 * not HT+VHT channels
1929 			 */
1930 			if ((mode == IEEE80211_MODE_11NA ||
1931 			    mode == IEEE80211_MODE_11NG) &&
1932 			    !IEEE80211_IS_CHAN_HT(c))
1933 				continue;
1934 
1935 			if ((mode == IEEE80211_MODE_11NA ||
1936 			    mode == IEEE80211_MODE_11NG) &&
1937 			    IEEE80211_IS_CHAN_VHT(c))
1938 				continue;
1939 
1940 			/* Check that the modeflags above match */
1941 			if ((c->ic_flags & modeflags) == modeflags)
1942 				return c;
1943 		}
1944 	}
1945 	return NULL;
1946 }
1947 
1948 /*
1949  * Check the specified against any desired mode (aka netband).
1950  * This is only used (presently) when operating in hostap mode
1951  * to enforce consistency.
1952  */
1953 static int
1954 check_mode_consistency(const struct ieee80211_channel *c, int mode)
1955 {
1956 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1957 
1958 	switch (mode) {
1959 	case IEEE80211_MODE_11B:
1960 		return (IEEE80211_IS_CHAN_B(c));
1961 	case IEEE80211_MODE_11G:
1962 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1963 	case IEEE80211_MODE_11A:
1964 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1965 	case IEEE80211_MODE_STURBO_A:
1966 		return (IEEE80211_IS_CHAN_STURBO(c));
1967 	case IEEE80211_MODE_11NA:
1968 		return (IEEE80211_IS_CHAN_HTA(c));
1969 	case IEEE80211_MODE_11NG:
1970 		return (IEEE80211_IS_CHAN_HTG(c));
1971 	}
1972 	return 1;
1973 
1974 }
1975 
1976 /*
1977  * Common code to set the current channel.  If the device
1978  * is up and running this may result in an immediate channel
1979  * change or a kick of the state machine.
1980  */
1981 static int
1982 setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1983 {
1984 	struct ieee80211com *ic = vap->iv_ic;
1985 	int error;
1986 
1987 	if (c != IEEE80211_CHAN_ANYC) {
1988 		if (IEEE80211_IS_CHAN_RADAR(c))
1989 			return EBUSY;	/* XXX better code? */
1990 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1991 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1992 				return EINVAL;
1993 			if (!check_mode_consistency(c, vap->iv_des_mode))
1994 				return EINVAL;
1995 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1996 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1997 				return EINVAL;
1998 		}
1999 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
2000 		    vap->iv_bss->ni_chan == c)
2001 			return 0;	/* NB: nothing to do */
2002 	}
2003 	vap->iv_des_chan = c;
2004 
2005 	error = 0;
2006 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
2007 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
2008 		/*
2009 		 * Monitor mode can switch directly.
2010 		 */
2011 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
2012 			/* XXX need state machine for other vap's to follow */
2013 			ieee80211_setcurchan(ic, vap->iv_des_chan);
2014 			vap->iv_bss->ni_chan = ic->ic_curchan;
2015 		} else {
2016 			ic->ic_curchan = vap->iv_des_chan;
2017 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2018 		}
2019 	} else {
2020 		/*
2021 		 * Need to go through the state machine in case we
2022 		 * need to reassociate or the like.  The state machine
2023 		 * will pickup the desired channel and avoid scanning.
2024 		 */
2025 		if (IS_UP_AUTO(vap))
2026 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2027 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
2028 			/*
2029 			 * When not up+running and a real channel has
2030 			 * been specified fix the current channel so
2031 			 * there is immediate feedback; e.g. via ifconfig.
2032 			 */
2033 			ic->ic_curchan = vap->iv_des_chan;
2034 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2035 		}
2036 	}
2037 	return error;
2038 }
2039 
2040 /*
2041  * Old api for setting the current channel; this is
2042  * deprecated because channel numbers are ambiguous.
2043  */
2044 static int
2045 ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
2046 	const struct ieee80211req *ireq)
2047 {
2048 	struct ieee80211com *ic = vap->iv_ic;
2049 	struct ieee80211_channel *c;
2050 
2051 	/* XXX 0xffff overflows 16-bit signed */
2052 	if (ireq->i_val == 0 ||
2053 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
2054 		c = IEEE80211_CHAN_ANYC;
2055 	} else {
2056 		struct ieee80211_channel *c2;
2057 
2058 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
2059 		if (c == NULL) {
2060 			c = findchannel(ic, ireq->i_val,
2061 				IEEE80211_MODE_AUTO);
2062 			if (c == NULL)
2063 				return EINVAL;
2064 		}
2065 
2066 		/*
2067 		 * Fine tune channel selection based on desired mode:
2068 		 *   if 11b is requested, find the 11b version of any
2069 		 *      11g channel returned,
2070 		 *   if static turbo, find the turbo version of any
2071 		 *	11a channel return,
2072 		 *   if 11na is requested, find the ht version of any
2073 		 *      11a channel returned,
2074 		 *   if 11ng is requested, find the ht version of any
2075 		 *      11g channel returned,
2076 		 *   if 11ac is requested, find the 11ac version
2077 		 *      of any 11a/11na channel returned,
2078 		 *   (TBD) 11acg (2GHz VHT)
2079 		 *   otherwise we should be ok with what we've got.
2080 		 */
2081 		switch (vap->iv_des_mode) {
2082 		case IEEE80211_MODE_11B:
2083 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2084 				c2 = findchannel(ic, ireq->i_val,
2085 					IEEE80211_MODE_11B);
2086 				/* NB: should not happen, =>'s 11g w/o 11b */
2087 				if (c2 != NULL)
2088 					c = c2;
2089 			}
2090 			break;
2091 		case IEEE80211_MODE_TURBO_A:
2092 			if (IEEE80211_IS_CHAN_A(c)) {
2093 				c2 = findchannel(ic, ireq->i_val,
2094 					IEEE80211_MODE_TURBO_A);
2095 				if (c2 != NULL)
2096 					c = c2;
2097 			}
2098 			break;
2099 		case IEEE80211_MODE_11NA:
2100 			if (IEEE80211_IS_CHAN_A(c)) {
2101 				c2 = findchannel(ic, ireq->i_val,
2102 					IEEE80211_MODE_11NA);
2103 				if (c2 != NULL)
2104 					c = c2;
2105 			}
2106 			break;
2107 		case IEEE80211_MODE_11NG:
2108 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2109 				c2 = findchannel(ic, ireq->i_val,
2110 					IEEE80211_MODE_11NG);
2111 				if (c2 != NULL)
2112 					c = c2;
2113 			}
2114 			break;
2115 		case IEEE80211_MODE_VHT_2GHZ:
2116 			printf("%s: TBD\n", __func__);
2117 			break;
2118 		case IEEE80211_MODE_VHT_5GHZ:
2119 			if (IEEE80211_IS_CHAN_A(c)) {
2120 				c2 = findchannel(ic, ireq->i_val,
2121 					IEEE80211_MODE_VHT_5GHZ);
2122 				if (c2 != NULL)
2123 					c = c2;
2124 			}
2125 			break;
2126 		default:		/* NB: no static turboG */
2127 			break;
2128 		}
2129 	}
2130 	return setcurchan(vap, c);
2131 }
2132 
2133 /*
2134  * New/current api for setting the current channel; a complete
2135  * channel description is provide so there is no ambiguity in
2136  * identifying the channel.
2137  */
2138 static int
2139 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
2140 	const struct ieee80211req *ireq)
2141 {
2142 	struct ieee80211com *ic = vap->iv_ic;
2143 	struct ieee80211_channel chan, *c;
2144 	int error;
2145 
2146 	if (ireq->i_len != sizeof(chan))
2147 		return EINVAL;
2148 	error = copyin(ireq->i_data, &chan, sizeof(chan));
2149 	if (error != 0)
2150 		return error;
2151 
2152 	/* XXX 0xffff overflows 16-bit signed */
2153 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
2154 		c = IEEE80211_CHAN_ANYC;
2155 	} else {
2156 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
2157 		if (c == NULL)
2158 			return EINVAL;
2159 	}
2160 	return setcurchan(vap, c);
2161 }
2162 
2163 static int
2164 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2165 	const struct ieee80211req *ireq)
2166 {
2167 	struct ieee80211_regdomain_req *reg;
2168 	int nchans, error;
2169 
2170 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2171 	    sizeof(struct ieee80211_channel));
2172 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2173 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2174 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2175 		    ireq->i_len, nchans);
2176 		return EINVAL;
2177 	}
2178 	reg = (struct ieee80211_regdomain_req *)
2179 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2180 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2181 	if (reg == NULL) {
2182 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2183 		    "%s: no memory, nchans %d\n", __func__, nchans);
2184 		return ENOMEM;
2185 	}
2186 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2187 	if (error == 0) {
2188 		/* NB: validate inline channel count against storage size */
2189 		if (reg->chaninfo.ic_nchans != nchans) {
2190 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2191 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2192 				reg->chaninfo.ic_nchans, nchans);
2193 			error = EINVAL;
2194 		} else
2195 			error = ieee80211_setregdomain(vap, reg);
2196 	}
2197 	IEEE80211_FREE(reg, M_TEMP);
2198 
2199 	return (error == 0 ? ENETRESET : error);
2200 }
2201 
2202 static int
2203 checkrate(const struct ieee80211_rateset *rs, int rate)
2204 {
2205 	int i;
2206 
2207 	if (rate == IEEE80211_FIXED_RATE_NONE)
2208 		return 1;
2209 	for (i = 0; i < rs->rs_nrates; i++)
2210 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2211 			return 1;
2212 	return 0;
2213 }
2214 
2215 static int
2216 checkmcs(const struct ieee80211_htrateset *rs, int mcs)
2217 {
2218 	int rate_val = IEEE80211_RV(mcs);
2219 	int i;
2220 
2221 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2222 		return 1;
2223 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2224 		return 0;
2225 	for (i = 0; i < rs->rs_nrates; i++)
2226 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
2227 			return 1;
2228 	return 0;
2229 }
2230 
2231 static int
2232 ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2233         const struct ieee80211req *ireq)
2234 {
2235 	struct ieee80211com *ic = vap->iv_ic;
2236 	struct ieee80211_roamparams_req *parms;
2237 	struct ieee80211_roamparam *src, *dst;
2238 	const struct ieee80211_htrateset *rs_ht;
2239 	const struct ieee80211_rateset *rs;
2240 	int changed, error, mode, is11n, nmodes;
2241 
2242 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2243 		return EINVAL;
2244 
2245 	parms = IEEE80211_MALLOC(sizeof(*parms), M_TEMP,
2246 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2247 	if (parms == NULL)
2248 		return ENOMEM;
2249 
2250 	error = copyin(ireq->i_data, parms, ireq->i_len);
2251 	if (error != 0)
2252 		goto fail;
2253 
2254 	changed = 0;
2255 	nmodes = IEEE80211_MODE_MAX;
2256 
2257 	/* validate parameters and check if anything changed */
2258 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2259 		if (isclr(ic->ic_modecaps, mode))
2260 			continue;
2261 		src = &parms->params[mode];
2262 		dst = &vap->iv_roamparms[mode];
2263 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2264 		rs_ht = &ic->ic_sup_htrates;
2265 		is11n = (mode == IEEE80211_MODE_11NA ||
2266 			 mode == IEEE80211_MODE_11NG);
2267 		/* XXX TODO: 11ac */
2268 		if (src->rate != dst->rate) {
2269 			if (!checkrate(rs, src->rate) &&
2270 			    (!is11n || !checkmcs(rs_ht, src->rate))) {
2271 				error = EINVAL;
2272 				goto fail;
2273 			}
2274 			changed++;
2275 		}
2276 		if (src->rssi != dst->rssi)
2277 			changed++;
2278 	}
2279 	if (changed) {
2280 		/*
2281 		 * Copy new parameters in place and notify the
2282 		 * driver so it can push state to the device.
2283 		 */
2284 		/* XXX locking? */
2285 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2286 			if (isset(ic->ic_modecaps, mode))
2287 				vap->iv_roamparms[mode] = parms->params[mode];
2288 		}
2289 
2290 		if (vap->iv_roaming == IEEE80211_ROAMING_DEVICE)
2291 			error = ERESTART;
2292 	}
2293 
2294 fail:	IEEE80211_FREE(parms, M_TEMP);
2295 	return error;
2296 }
2297 
2298 static int
2299 ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2300 	const struct ieee80211req *ireq)
2301 {
2302 	struct ieee80211com *ic = vap->iv_ic;
2303 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2304 	struct ieee80211_txparam *src, *dst;
2305 	const struct ieee80211_htrateset *rs_ht;
2306 	const struct ieee80211_rateset *rs;
2307 	int error, mode, changed, is11n, nmodes;
2308 
2309 	/* NB: accept short requests for backwards compat */
2310 	if (ireq->i_len > sizeof(parms))
2311 		return EINVAL;
2312 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2313 	if (error != 0)
2314 		return error;
2315 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2316 	changed = 0;
2317 	/* validate parameters and check if anything changed */
2318 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2319 		if (isclr(ic->ic_modecaps, mode))
2320 			continue;
2321 		src = &parms.params[mode];
2322 		dst = &vap->iv_txparms[mode];
2323 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2324 		rs_ht = &ic->ic_sup_htrates;
2325 		is11n = (mode == IEEE80211_MODE_11NA ||
2326 			 mode == IEEE80211_MODE_11NG);
2327 		if (src->ucastrate != dst->ucastrate) {
2328 			if (!checkrate(rs, src->ucastrate) &&
2329 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
2330 				return EINVAL;
2331 			changed++;
2332 		}
2333 		if (src->mcastrate != dst->mcastrate) {
2334 			if (!checkrate(rs, src->mcastrate) &&
2335 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
2336 				return EINVAL;
2337 			changed++;
2338 		}
2339 		if (src->mgmtrate != dst->mgmtrate) {
2340 			if (!checkrate(rs, src->mgmtrate) &&
2341 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
2342 				return EINVAL;
2343 			changed++;
2344 		}
2345 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2346 			changed++;
2347 	}
2348 	if (changed) {
2349 		/*
2350 		 * Copy new parameters in place and notify the
2351 		 * driver so it can push state to the device.
2352 		 */
2353 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2354 			if (isset(ic->ic_modecaps, mode))
2355 				vap->iv_txparms[mode] = parms.params[mode];
2356 		}
2357 		/* XXX could be more intelligent,
2358 		   e.g. don't reset if setting not being used */
2359 		return ENETRESET;
2360 	}
2361 	return 0;
2362 }
2363 
2364 /*
2365  * Application Information Element support.
2366  */
2367 static int
2368 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2369 {
2370 	struct ieee80211_appie *app = *aie;
2371 	struct ieee80211_appie *napp;
2372 	int error;
2373 
2374 	if (ireq->i_len == 0) {		/* delete any existing ie */
2375 		if (app != NULL) {
2376 			*aie = NULL;	/* XXX racey */
2377 			IEEE80211_FREE(app, M_80211_NODE_IE);
2378 		}
2379 		return 0;
2380 	}
2381 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2382 		return EINVAL;
2383 	/*
2384 	 * Allocate a new appie structure and copy in the user data.
2385 	 * When done swap in the new structure.  Note that we do not
2386 	 * guard against users holding a ref to the old structure;
2387 	 * this must be handled outside this code.
2388 	 *
2389 	 * XXX bad bad bad
2390 	 */
2391 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2392 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2393 	    IEEE80211_M_NOWAIT);
2394 	if (napp == NULL)
2395 		return ENOMEM;
2396 	/* XXX holding ic lock */
2397 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2398 	if (error) {
2399 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2400 		return error;
2401 	}
2402 	napp->ie_len = ireq->i_len;
2403 	*aie = napp;
2404 	if (app != NULL)
2405 		IEEE80211_FREE(app, M_80211_NODE_IE);
2406 	return 0;
2407 }
2408 
2409 static void
2410 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2411 {
2412 	/* validate data is present as best we can */
2413 	if (space == 0 || 2+ie[1] > space)
2414 		return;
2415 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2416 		vap->iv_wpa_ie = ie;
2417 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2418 		vap->iv_rsn_ie = ie;
2419 }
2420 
2421 static int
2422 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2423 	const struct ieee80211req *ireq, int fc0)
2424 {
2425 	int error;
2426 
2427 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2428 
2429 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2430 	case IEEE80211_FC0_SUBTYPE_BEACON:
2431 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2432 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2433 			error = EINVAL;
2434 			break;
2435 		}
2436 		error = setappie(&vap->iv_appie_beacon, ireq);
2437 		if (error == 0)
2438 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2439 		break;
2440 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2441 		error = setappie(&vap->iv_appie_proberesp, ireq);
2442 		break;
2443 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2444 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2445 			error = setappie(&vap->iv_appie_assocresp, ireq);
2446 		else
2447 			error = EINVAL;
2448 		break;
2449 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2450 		error = setappie(&vap->iv_appie_probereq, ireq);
2451 		break;
2452 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2453 		if (vap->iv_opmode == IEEE80211_M_STA)
2454 			error = setappie(&vap->iv_appie_assocreq, ireq);
2455 		else
2456 			error = EINVAL;
2457 		break;
2458 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2459 		error = setappie(&vap->iv_appie_wpa, ireq);
2460 		if (error == 0) {
2461 			/*
2462 			 * Must split single blob of data into separate
2463 			 * WPA and RSN ie's because they go in different
2464 			 * locations in the mgt frames.
2465 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2466 			 */
2467 			vap->iv_wpa_ie = NULL;
2468 			vap->iv_rsn_ie = NULL;
2469 			if (vap->iv_appie_wpa != NULL) {
2470 				struct ieee80211_appie *appie =
2471 				    vap->iv_appie_wpa;
2472 				uint8_t *data = appie->ie_data;
2473 
2474 				/* XXX ie length validate is painful, cheat */
2475 				setwparsnie(vap, data, appie->ie_len);
2476 				setwparsnie(vap, data + 2 + data[1],
2477 				    appie->ie_len - (2 + data[1]));
2478 			}
2479 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2480 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2481 				/*
2482 				 * Must rebuild beacon frame as the update
2483 				 * mechanism doesn't handle WPA/RSN ie's.
2484 				 * Could extend it but it doesn't normally
2485 				 * change; this is just to deal with hostapd
2486 				 * plumbing the ie after the interface is up.
2487 				 */
2488 				error = ENETRESET;
2489 			}
2490 		}
2491 		break;
2492 	default:
2493 		error = EINVAL;
2494 		break;
2495 	}
2496 	return error;
2497 }
2498 
2499 static int
2500 ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2501 	const struct ieee80211req *ireq)
2502 {
2503 	struct ieee80211com *ic = vap->iv_ic;
2504 	int error;
2505 	uint8_t fc0;
2506 
2507 	fc0 = ireq->i_val & 0xff;
2508 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2509 		return EINVAL;
2510 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2511 	IEEE80211_LOCK(ic);
2512 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2513 	IEEE80211_UNLOCK(ic);
2514 	return error;
2515 }
2516 
2517 static int
2518 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2519 {
2520 	struct ieee80211com *ic = vap->iv_ic;
2521 	struct ieee80211_chanswitch_req csr;
2522 	struct ieee80211_channel *c;
2523 	int error;
2524 
2525 	if (ireq->i_len != sizeof(csr))
2526 		return EINVAL;
2527 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2528 	if (error != 0)
2529 		return error;
2530 	/* XXX adhoc mode not supported */
2531 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2532 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2533 		return EOPNOTSUPP;
2534 	c = ieee80211_find_channel(ic,
2535 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2536 	if (c == NULL)
2537 		return ENOENT;
2538 	IEEE80211_LOCK(ic);
2539 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2540 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2541 	else if (csr.csa_count == 0)
2542 		ieee80211_csa_cancelswitch(ic);
2543 	else
2544 		error = EBUSY;
2545 	IEEE80211_UNLOCK(ic);
2546 	return error;
2547 }
2548 
2549 static int
2550 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2551 {
2552 #define	IEEE80211_IOC_SCAN_FLAGS \
2553 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2554 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2555 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2556 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2557 	 IEEE80211_IOC_SCAN_CHECK)
2558 	struct ieee80211com *ic = vap->iv_ic;
2559 	int error, i;
2560 
2561 	/* convert duration */
2562 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2563 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2564 	else {
2565 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2566 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2567 			return EINVAL;
2568 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2569 	}
2570 	/* convert min/max channel dwell */
2571 	if (sr->sr_mindwell != 0)
2572 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2573 	if (sr->sr_maxdwell != 0)
2574 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2575 	/* NB: silently reduce ssid count to what is supported */
2576 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2577 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2578 	for (i = 0; i < sr->sr_nssid; i++)
2579 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2580 			return EINVAL;
2581 	/* cleanse flags just in case, could reject if invalid flags */
2582 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2583 	/*
2584 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2585 	 * allows applications to scan without joining a bss (or picking
2586 	 * a channel and setting up a bss) and without forcing manual
2587 	 * roaming mode--you just need to mark the parent device UP.
2588 	 */
2589 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2590 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2591 
2592 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2593 	    "%s: vap %p iv_state %#x (%s) flags 0x%x%s "
2594 	    "duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2595 	    __func__, vap, vap->iv_state, ieee80211_state_name[vap->iv_state],
2596 	    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 		error = 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