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