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