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