xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 8657387683946d0c03e09fe77029edfe309eeb20)
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 		}
2021 	} else {
2022 		/*
2023 		 * Need to go through the state machine in case we
2024 		 * need to reassociate or the like.  The state machine
2025 		 * will pickup the desired channel and avoid scanning.
2026 		 */
2027 		if (IS_UP_AUTO(vap))
2028 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2029 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
2030 			/*
2031 			 * When not up+running and a real channel has
2032 			 * been specified fix the current channel so
2033 			 * there is immediate feedback; e.g. via ifconfig.
2034 			 */
2035 			ic->ic_curchan = vap->iv_des_chan;
2036 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
2037 		}
2038 	}
2039 	return error;
2040 }
2041 
2042 /*
2043  * Old api for setting the current channel; this is
2044  * deprecated because channel numbers are ambiguous.
2045  */
2046 static int
2047 ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
2048 	const struct ieee80211req *ireq)
2049 {
2050 	struct ieee80211com *ic = vap->iv_ic;
2051 	struct ieee80211_channel *c;
2052 
2053 	/* XXX 0xffff overflows 16-bit signed */
2054 	if (ireq->i_val == 0 ||
2055 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
2056 		c = IEEE80211_CHAN_ANYC;
2057 	} else {
2058 		struct ieee80211_channel *c2;
2059 
2060 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
2061 		if (c == NULL) {
2062 			c = findchannel(ic, ireq->i_val,
2063 				IEEE80211_MODE_AUTO);
2064 			if (c == NULL)
2065 				return EINVAL;
2066 		}
2067 
2068 		/*
2069 		 * Fine tune channel selection based on desired mode:
2070 		 *   if 11b is requested, find the 11b version of any
2071 		 *      11g channel returned,
2072 		 *   if static turbo, find the turbo version of any
2073 		 *	11a channel return,
2074 		 *   if 11na is requested, find the ht version of any
2075 		 *      11a channel returned,
2076 		 *   if 11ng is requested, find the ht version of any
2077 		 *      11g channel returned,
2078 		 *   if 11ac is requested, find the 11ac version
2079 		 *      of any 11a/11na channel returned,
2080 		 *   (TBD) 11acg (2GHz VHT)
2081 		 *   otherwise we should be ok with what we've got.
2082 		 */
2083 		switch (vap->iv_des_mode) {
2084 		case IEEE80211_MODE_11B:
2085 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2086 				c2 = findchannel(ic, ireq->i_val,
2087 					IEEE80211_MODE_11B);
2088 				/* NB: should not happen, =>'s 11g w/o 11b */
2089 				if (c2 != NULL)
2090 					c = c2;
2091 			}
2092 			break;
2093 		case IEEE80211_MODE_TURBO_A:
2094 			if (IEEE80211_IS_CHAN_A(c)) {
2095 				c2 = findchannel(ic, ireq->i_val,
2096 					IEEE80211_MODE_TURBO_A);
2097 				if (c2 != NULL)
2098 					c = c2;
2099 			}
2100 			break;
2101 		case IEEE80211_MODE_11NA:
2102 			if (IEEE80211_IS_CHAN_A(c)) {
2103 				c2 = findchannel(ic, ireq->i_val,
2104 					IEEE80211_MODE_11NA);
2105 				if (c2 != NULL)
2106 					c = c2;
2107 			}
2108 			break;
2109 		case IEEE80211_MODE_11NG:
2110 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2111 				c2 = findchannel(ic, ireq->i_val,
2112 					IEEE80211_MODE_11NG);
2113 				if (c2 != NULL)
2114 					c = c2;
2115 			}
2116 			break;
2117 		case IEEE80211_MODE_VHT_2GHZ:
2118 			printf("%s: TBD\n", __func__);
2119 			break;
2120 		case IEEE80211_MODE_VHT_5GHZ:
2121 			if (IEEE80211_IS_CHAN_A(c)) {
2122 				c2 = findchannel(ic, ireq->i_val,
2123 					IEEE80211_MODE_VHT_5GHZ);
2124 				if (c2 != NULL)
2125 					c = c2;
2126 			}
2127 			break;
2128 		default:		/* NB: no static turboG */
2129 			break;
2130 		}
2131 	}
2132 	return setcurchan(vap, c);
2133 }
2134 
2135 /*
2136  * New/current api for setting the current channel; a complete
2137  * channel description is provide so there is no ambiguity in
2138  * identifying the channel.
2139  */
2140 static int
2141 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
2142 	const struct ieee80211req *ireq)
2143 {
2144 	struct ieee80211com *ic = vap->iv_ic;
2145 	struct ieee80211_channel chan, *c;
2146 	int error;
2147 
2148 	if (ireq->i_len != sizeof(chan))
2149 		return EINVAL;
2150 	error = copyin(ireq->i_data, &chan, sizeof(chan));
2151 	if (error != 0)
2152 		return error;
2153 
2154 	/* XXX 0xffff overflows 16-bit signed */
2155 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
2156 		c = IEEE80211_CHAN_ANYC;
2157 	} else {
2158 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
2159 		if (c == NULL)
2160 			return EINVAL;
2161 	}
2162 	return setcurchan(vap, c);
2163 }
2164 
2165 static int
2166 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2167 	const struct ieee80211req *ireq)
2168 {
2169 	struct ieee80211_regdomain_req *reg;
2170 	int nchans, error;
2171 
2172 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2173 	    sizeof(struct ieee80211_channel));
2174 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2175 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2176 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2177 		    ireq->i_len, nchans);
2178 		return EINVAL;
2179 	}
2180 	reg = (struct ieee80211_regdomain_req *)
2181 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2182 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2183 	if (reg == NULL) {
2184 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2185 		    "%s: no memory, nchans %d\n", __func__, nchans);
2186 		return ENOMEM;
2187 	}
2188 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2189 	if (error == 0) {
2190 		/* NB: validate inline channel count against storage size */
2191 		if (reg->chaninfo.ic_nchans != nchans) {
2192 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2193 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2194 				reg->chaninfo.ic_nchans, nchans);
2195 			error = EINVAL;
2196 		} else
2197 			error = ieee80211_setregdomain(vap, reg);
2198 	}
2199 	IEEE80211_FREE(reg, M_TEMP);
2200 
2201 	return (error == 0 ? ENETRESET : error);
2202 }
2203 
2204 static int
2205 ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2206 	const struct ieee80211req *ireq)
2207 {
2208 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2209 		return EINVAL;
2210 	/* XXX validate params */
2211 	/* XXX? ENETRESET to push to device? */
2212 	return copyin(ireq->i_data, vap->iv_roamparms,
2213 	    sizeof(vap->iv_roamparms));
2214 }
2215 
2216 static int
2217 checkrate(const struct ieee80211_rateset *rs, int rate)
2218 {
2219 	int i;
2220 
2221 	if (rate == IEEE80211_FIXED_RATE_NONE)
2222 		return 1;
2223 	for (i = 0; i < rs->rs_nrates; i++)
2224 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2225 			return 1;
2226 	return 0;
2227 }
2228 
2229 static int
2230 checkmcs(const struct ieee80211_htrateset *rs, int mcs)
2231 {
2232 	int rate_val = IEEE80211_RV(mcs);
2233 	int i;
2234 
2235 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2236 		return 1;
2237 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2238 		return 0;
2239 	for (i = 0; i < rs->rs_nrates; i++)
2240 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
2241 			return 1;
2242 	return 0;
2243 }
2244 
2245 static int
2246 ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2247 	const struct ieee80211req *ireq)
2248 {
2249 	struct ieee80211com *ic = vap->iv_ic;
2250 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2251 	struct ieee80211_txparam *src, *dst;
2252 	const struct ieee80211_htrateset *rs_ht;
2253 	const struct ieee80211_rateset *rs;
2254 	int error, mode, changed, is11n, nmodes;
2255 
2256 	/* NB: accept short requests for backwards compat */
2257 	if (ireq->i_len > sizeof(parms))
2258 		return EINVAL;
2259 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2260 	if (error != 0)
2261 		return error;
2262 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2263 	changed = 0;
2264 	/* validate parameters and check if anything changed */
2265 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2266 		if (isclr(ic->ic_modecaps, mode))
2267 			continue;
2268 		src = &parms.params[mode];
2269 		dst = &vap->iv_txparms[mode];
2270 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2271 		rs_ht = &ic->ic_sup_htrates;
2272 		is11n = (mode == IEEE80211_MODE_11NA ||
2273 			 mode == IEEE80211_MODE_11NG);
2274 		if (src->ucastrate != dst->ucastrate) {
2275 			if (!checkrate(rs, src->ucastrate) &&
2276 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
2277 				return EINVAL;
2278 			changed++;
2279 		}
2280 		if (src->mcastrate != dst->mcastrate) {
2281 			if (!checkrate(rs, src->mcastrate) &&
2282 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
2283 				return EINVAL;
2284 			changed++;
2285 		}
2286 		if (src->mgmtrate != dst->mgmtrate) {
2287 			if (!checkrate(rs, src->mgmtrate) &&
2288 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
2289 				return EINVAL;
2290 			changed++;
2291 		}
2292 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2293 			changed++;
2294 	}
2295 	if (changed) {
2296 		/*
2297 		 * Copy new parameters in place and notify the
2298 		 * driver so it can push state to the device.
2299 		 */
2300 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2301 			if (isset(ic->ic_modecaps, mode))
2302 				vap->iv_txparms[mode] = parms.params[mode];
2303 		}
2304 		/* XXX could be more intelligent,
2305 		   e.g. don't reset if setting not being used */
2306 		return ENETRESET;
2307 	}
2308 	return 0;
2309 }
2310 
2311 /*
2312  * Application Information Element support.
2313  */
2314 static int
2315 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2316 {
2317 	struct ieee80211_appie *app = *aie;
2318 	struct ieee80211_appie *napp;
2319 	int error;
2320 
2321 	if (ireq->i_len == 0) {		/* delete any existing ie */
2322 		if (app != NULL) {
2323 			*aie = NULL;	/* XXX racey */
2324 			IEEE80211_FREE(app, M_80211_NODE_IE);
2325 		}
2326 		return 0;
2327 	}
2328 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2329 		return EINVAL;
2330 	/*
2331 	 * Allocate a new appie structure and copy in the user data.
2332 	 * When done swap in the new structure.  Note that we do not
2333 	 * guard against users holding a ref to the old structure;
2334 	 * this must be handled outside this code.
2335 	 *
2336 	 * XXX bad bad bad
2337 	 */
2338 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2339 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2340 	    IEEE80211_M_NOWAIT);
2341 	if (napp == NULL)
2342 		return ENOMEM;
2343 	/* XXX holding ic lock */
2344 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2345 	if (error) {
2346 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2347 		return error;
2348 	}
2349 	napp->ie_len = ireq->i_len;
2350 	*aie = napp;
2351 	if (app != NULL)
2352 		IEEE80211_FREE(app, M_80211_NODE_IE);
2353 	return 0;
2354 }
2355 
2356 static void
2357 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2358 {
2359 	/* validate data is present as best we can */
2360 	if (space == 0 || 2+ie[1] > space)
2361 		return;
2362 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2363 		vap->iv_wpa_ie = ie;
2364 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2365 		vap->iv_rsn_ie = ie;
2366 }
2367 
2368 static int
2369 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2370 	const struct ieee80211req *ireq, int fc0)
2371 {
2372 	int error;
2373 
2374 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2375 
2376 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2377 	case IEEE80211_FC0_SUBTYPE_BEACON:
2378 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2379 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2380 			error = EINVAL;
2381 			break;
2382 		}
2383 		error = setappie(&vap->iv_appie_beacon, ireq);
2384 		if (error == 0)
2385 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2386 		break;
2387 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2388 		error = setappie(&vap->iv_appie_proberesp, ireq);
2389 		break;
2390 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2391 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2392 			error = setappie(&vap->iv_appie_assocresp, ireq);
2393 		else
2394 			error = EINVAL;
2395 		break;
2396 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2397 		error = setappie(&vap->iv_appie_probereq, ireq);
2398 		break;
2399 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2400 		if (vap->iv_opmode == IEEE80211_M_STA)
2401 			error = setappie(&vap->iv_appie_assocreq, ireq);
2402 		else
2403 			error = EINVAL;
2404 		break;
2405 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2406 		error = setappie(&vap->iv_appie_wpa, ireq);
2407 		if (error == 0) {
2408 			/*
2409 			 * Must split single blob of data into separate
2410 			 * WPA and RSN ie's because they go in different
2411 			 * locations in the mgt frames.
2412 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2413 			 */
2414 			vap->iv_wpa_ie = NULL;
2415 			vap->iv_rsn_ie = NULL;
2416 			if (vap->iv_appie_wpa != NULL) {
2417 				struct ieee80211_appie *appie =
2418 				    vap->iv_appie_wpa;
2419 				uint8_t *data = appie->ie_data;
2420 
2421 				/* XXX ie length validate is painful, cheat */
2422 				setwparsnie(vap, data, appie->ie_len);
2423 				setwparsnie(vap, data + 2 + data[1],
2424 				    appie->ie_len - (2 + data[1]));
2425 			}
2426 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2427 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2428 				/*
2429 				 * Must rebuild beacon frame as the update
2430 				 * mechanism doesn't handle WPA/RSN ie's.
2431 				 * Could extend it but it doesn't normally
2432 				 * change; this is just to deal with hostapd
2433 				 * plumbing the ie after the interface is up.
2434 				 */
2435 				error = ENETRESET;
2436 			}
2437 		}
2438 		break;
2439 	default:
2440 		error = EINVAL;
2441 		break;
2442 	}
2443 	return error;
2444 }
2445 
2446 static int
2447 ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2448 	const struct ieee80211req *ireq)
2449 {
2450 	struct ieee80211com *ic = vap->iv_ic;
2451 	int error;
2452 	uint8_t fc0;
2453 
2454 	fc0 = ireq->i_val & 0xff;
2455 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2456 		return EINVAL;
2457 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2458 	IEEE80211_LOCK(ic);
2459 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2460 	IEEE80211_UNLOCK(ic);
2461 	return error;
2462 }
2463 
2464 static int
2465 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2466 {
2467 	struct ieee80211com *ic = vap->iv_ic;
2468 	struct ieee80211_chanswitch_req csr;
2469 	struct ieee80211_channel *c;
2470 	int error;
2471 
2472 	if (ireq->i_len != sizeof(csr))
2473 		return EINVAL;
2474 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2475 	if (error != 0)
2476 		return error;
2477 	/* XXX adhoc mode not supported */
2478 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2479 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2480 		return EOPNOTSUPP;
2481 	c = ieee80211_find_channel(ic,
2482 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2483 	if (c == NULL)
2484 		return ENOENT;
2485 	IEEE80211_LOCK(ic);
2486 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2487 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2488 	else if (csr.csa_count == 0)
2489 		ieee80211_csa_cancelswitch(ic);
2490 	else
2491 		error = EBUSY;
2492 	IEEE80211_UNLOCK(ic);
2493 	return error;
2494 }
2495 
2496 static int
2497 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2498 {
2499 #define	IEEE80211_IOC_SCAN_FLAGS \
2500 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2501 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2502 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2503 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2504 	 IEEE80211_IOC_SCAN_CHECK)
2505 	struct ieee80211com *ic = vap->iv_ic;
2506 	int error, i;
2507 
2508 	/* convert duration */
2509 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2510 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2511 	else {
2512 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2513 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2514 			return EINVAL;
2515 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2516 		if (sr->sr_duration < 1)
2517 			sr->sr_duration = 1;
2518 	}
2519 	/* convert min/max channel dwell */
2520 	if (sr->sr_mindwell != 0) {
2521 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2522 		if (sr->sr_mindwell < 1)
2523 			sr->sr_mindwell = 1;
2524 	}
2525 	if (sr->sr_maxdwell != 0) {
2526 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2527 		if (sr->sr_maxdwell < 1)
2528 			sr->sr_maxdwell = 1;
2529 	}
2530 	/* NB: silently reduce ssid count to what is supported */
2531 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2532 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2533 	for (i = 0; i < sr->sr_nssid; i++)
2534 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2535 			return EINVAL;
2536 	/* cleanse flags just in case, could reject if invalid flags */
2537 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2538 	/*
2539 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2540 	 * allows applications to scan without joining a bss (or picking
2541 	 * a channel and setting up a bss) and without forcing manual
2542 	 * roaming mode--you just need to mark the parent device UP.
2543 	 */
2544 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2545 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2546 
2547 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2548 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2549 	    __func__, sr->sr_flags,
2550 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2551 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2552 	/*
2553 	 * If we are in INIT state then the driver has never had a chance
2554 	 * to setup hardware state to do a scan; we must use the state
2555 	 * machine to get us up to the SCAN state but once we reach SCAN
2556 	 * state we then want to use the supplied params.  Stash the
2557 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2558 	 * state machines will recognize this and use the stashed params
2559 	 * to issue the scan request.
2560 	 *
2561 	 * Otherwise just invoke the scan machinery directly.
2562 	 */
2563 	IEEE80211_LOCK(ic);
2564 	if (ic->ic_nrunning == 0) {
2565 		IEEE80211_UNLOCK(ic);
2566 		return ENXIO;
2567 	}
2568 
2569 	if (vap->iv_state == IEEE80211_S_INIT) {
2570 		/* NB: clobbers previous settings */
2571 		vap->iv_scanreq_flags = sr->sr_flags;
2572 		vap->iv_scanreq_duration = sr->sr_duration;
2573 		vap->iv_scanreq_nssid = sr->sr_nssid;
2574 		for (i = 0; i < sr->sr_nssid; i++) {
2575 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2576 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2577 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2578 		}
2579 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2580 		IEEE80211_UNLOCK(ic);
2581 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2582 	} else {
2583 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2584 		IEEE80211_UNLOCK(ic);
2585 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2586 			error = ieee80211_check_scan(vap, sr->sr_flags,
2587 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2588 			    sr->sr_nssid,
2589 			    /* NB: cheat, we assume structures are compatible */
2590 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2591 		} else {
2592 			error = ieee80211_start_scan(vap, sr->sr_flags,
2593 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2594 			    sr->sr_nssid,
2595 			    /* NB: cheat, we assume structures are compatible */
2596 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2597 		}
2598 		if (error == 0)
2599 			return EINPROGRESS;
2600 	}
2601 	return 0;
2602 #undef IEEE80211_IOC_SCAN_FLAGS
2603 }
2604 
2605 static int
2606 ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2607 {
2608 	struct ieee80211_scan_req *sr;
2609 	int error;
2610 
2611 	if (ireq->i_len != sizeof(*sr))
2612 		return EINVAL;
2613 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
2614 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2615 	if (sr == NULL)
2616 		return ENOMEM;
2617 	error = copyin(ireq->i_data, sr, sizeof(*sr));
2618 	if (error != 0)
2619 		goto bad;
2620 	error = ieee80211_scanreq(vap, sr);
2621 bad:
2622 	IEEE80211_FREE(sr, M_TEMP);
2623 	return error;
2624 }
2625 
2626 static int
2627 ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2628 {
2629 	struct ieee80211_node *ni;
2630 	struct ieee80211req_sta_vlan vlan;
2631 	int error;
2632 
2633 	if (ireq->i_len != sizeof(vlan))
2634 		return EINVAL;
2635 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2636 	if (error != 0)
2637 		return error;
2638 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2639 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2640 		    vlan.sv_macaddr);
2641 		if (ni == NULL)
2642 			return ENOENT;
2643 	} else
2644 		ni = ieee80211_ref_node(vap->iv_bss);
2645 	ni->ni_vlan = vlan.sv_vlan;
2646 	ieee80211_free_node(ni);
2647 	return error;
2648 }
2649 
2650 static int
2651 isvap11g(const struct ieee80211vap *vap)
2652 {
2653 	const struct ieee80211_node *bss = vap->iv_bss;
2654 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2655 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2656 }
2657 
2658 static int
2659 isvapht(const struct ieee80211vap *vap)
2660 {
2661 	const struct ieee80211_node *bss = vap->iv_bss;
2662 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2663 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2664 }
2665 
2666 /*
2667  * Dummy ioctl set handler so the linker set is defined.
2668  */
2669 static int
2670 dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
2671 {
2672 	return ENOSYS;
2673 }
2674 IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
2675 
2676 static int
2677 ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
2678 {
2679 	ieee80211_ioctl_setfunc * const *set;
2680 	int error;
2681 
2682 	SET_FOREACH(set, ieee80211_ioctl_setset) {
2683 		error = (*set)(vap, ireq);
2684 		if (error != ENOSYS)
2685 			return error;
2686 	}
2687 	return EINVAL;
2688 }
2689 
2690 static int
2691 ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2692 {
2693 	struct ieee80211com *ic = vap->iv_ic;
2694 	int error;
2695 	const struct ieee80211_authenticator *auth;
2696 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2697 	char tmpssid[IEEE80211_NWID_LEN];
2698 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2699 	struct ieee80211_key *k;
2700 	u_int kid;
2701 	uint32_t flags;
2702 
2703 	error = 0;
2704 	switch (ireq->i_type) {
2705 	case IEEE80211_IOC_SSID:
2706 		if (ireq->i_val != 0 ||
2707 		    ireq->i_len > IEEE80211_NWID_LEN)
2708 			return EINVAL;
2709 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2710 		if (error)
2711 			break;
2712 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2713 		vap->iv_des_ssid[0].len = ireq->i_len;
2714 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2715 		vap->iv_des_nssid = (ireq->i_len > 0);
2716 		error = ENETRESET;
2717 		break;
2718 	case IEEE80211_IOC_WEP:
2719 		switch (ireq->i_val) {
2720 		case IEEE80211_WEP_OFF:
2721 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2722 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2723 			break;
2724 		case IEEE80211_WEP_ON:
2725 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2726 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2727 			break;
2728 		case IEEE80211_WEP_MIXED:
2729 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2730 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2731 			break;
2732 		}
2733 		error = ENETRESET;
2734 		break;
2735 	case IEEE80211_IOC_WEPKEY:
2736 		kid = (u_int) ireq->i_val;
2737 		if (kid >= IEEE80211_WEP_NKID)
2738 			return EINVAL;
2739 		k = &vap->iv_nw_keys[kid];
2740 		if (ireq->i_len == 0) {
2741 			/* zero-len =>'s delete any existing key */
2742 			(void) ieee80211_crypto_delkey(vap, k);
2743 			break;
2744 		}
2745 		if (ireq->i_len > sizeof(tmpkey))
2746 			return EINVAL;
2747 		memset(tmpkey, 0, sizeof(tmpkey));
2748 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2749 		if (error)
2750 			break;
2751 		ieee80211_key_update_begin(vap);
2752 		k->wk_keyix = kid;	/* NB: force fixed key id */
2753 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2754 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2755 			k->wk_keylen = ireq->i_len;
2756 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2757 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2758 			if  (!ieee80211_crypto_setkey(vap, k))
2759 				error = EINVAL;
2760 		} else
2761 			error = EINVAL;
2762 		ieee80211_key_update_end(vap);
2763 		break;
2764 	case IEEE80211_IOC_WEPTXKEY:
2765 		kid = (u_int) ireq->i_val;
2766 		if (kid >= IEEE80211_WEP_NKID &&
2767 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2768 			return EINVAL;
2769 		/*
2770 		 * Firmware devices may need to be told about an explicit
2771 		 * key index here, versus just inferring it from the
2772 		 * key set / change.  Since we may also need to pause
2773 		 * things like transmit before the key is updated,
2774 		 * give the driver a chance to flush things by tying
2775 		 * into key update begin/end.
2776 		 */
2777 		ieee80211_key_update_begin(vap);
2778 		ieee80211_crypto_set_deftxkey(vap, kid);
2779 		ieee80211_key_update_end(vap);
2780 		break;
2781 	case IEEE80211_IOC_AUTHMODE:
2782 		switch (ireq->i_val) {
2783 		case IEEE80211_AUTH_WPA:
2784 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2785 		case IEEE80211_AUTH_OPEN:	/* open */
2786 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2787 		case IEEE80211_AUTH_AUTO:	/* auto */
2788 			auth = ieee80211_authenticator_get(ireq->i_val);
2789 			if (auth == NULL)
2790 				return EINVAL;
2791 			break;
2792 		default:
2793 			return EINVAL;
2794 		}
2795 		switch (ireq->i_val) {
2796 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2797 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2798 			ireq->i_val = IEEE80211_AUTH_8021X;
2799 			break;
2800 		case IEEE80211_AUTH_OPEN:	/* open */
2801 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2802 			break;
2803 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2804 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2805 			vap->iv_flags &= ~IEEE80211_F_WPA;
2806 			/* both require a key so mark the PRIVACY capability */
2807 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2808 			break;
2809 		case IEEE80211_AUTH_AUTO:	/* auto */
2810 			vap->iv_flags &= ~IEEE80211_F_WPA;
2811 			/* XXX PRIVACY handling? */
2812 			/* XXX what's the right way to do this? */
2813 			break;
2814 		}
2815 		/* NB: authenticator attach/detach happens on state change */
2816 		vap->iv_bss->ni_authmode = ireq->i_val;
2817 		/* XXX mixed/mode/usage? */
2818 		vap->iv_auth = auth;
2819 		error = ENETRESET;
2820 		break;
2821 	case IEEE80211_IOC_CHANNEL:
2822 		error = ieee80211_ioctl_setchannel(vap, ireq);
2823 		break;
2824 	case IEEE80211_IOC_POWERSAVE:
2825 		switch (ireq->i_val) {
2826 		case IEEE80211_POWERSAVE_OFF:
2827 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2828 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2829 				error = ERESTART;
2830 			}
2831 			break;
2832 		case IEEE80211_POWERSAVE_ON:
2833 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2834 				error = EOPNOTSUPP;
2835 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2836 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2837 				error = ERESTART;
2838 			}
2839 			break;
2840 		default:
2841 			error = EINVAL;
2842 			break;
2843 		}
2844 		break;
2845 	case IEEE80211_IOC_POWERSAVESLEEP:
2846 		if (ireq->i_val < 0)
2847 			return EINVAL;
2848 		ic->ic_lintval = ireq->i_val;
2849 		error = ERESTART;
2850 		break;
2851 	case IEEE80211_IOC_RTSTHRESHOLD:
2852 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2853 		      ireq->i_val <= IEEE80211_RTS_MAX))
2854 			return EINVAL;
2855 		vap->iv_rtsthreshold = ireq->i_val;
2856 		error = ERESTART;
2857 		break;
2858 	case IEEE80211_IOC_PROTMODE:
2859 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2860 			return EINVAL;
2861 		ic->ic_protmode = (enum ieee80211_protmode)ireq->i_val;
2862 		/* NB: if not operating in 11g this can wait */
2863 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2864 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2865 			error = ERESTART;
2866 		break;
2867 	case IEEE80211_IOC_TXPOWER:
2868 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2869 			return EOPNOTSUPP;
2870 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2871 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2872 			return EINVAL;
2873 		ic->ic_txpowlimit = ireq->i_val;
2874 		error = ERESTART;
2875 		break;
2876 	case IEEE80211_IOC_ROAMING:
2877 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2878 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2879 			return EINVAL;
2880 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
2881 		/* XXXX reset? */
2882 		break;
2883 	case IEEE80211_IOC_PRIVACY:
2884 		if (ireq->i_val) {
2885 			/* XXX check for key state? */
2886 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2887 		} else
2888 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2889 		/* XXX ERESTART? */
2890 		break;
2891 	case IEEE80211_IOC_DROPUNENCRYPTED:
2892 		if (ireq->i_val)
2893 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2894 		else
2895 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2896 		/* XXX ERESTART? */
2897 		break;
2898 	case IEEE80211_IOC_WPAKEY:
2899 		error = ieee80211_ioctl_setkey(vap, ireq);
2900 		break;
2901 	case IEEE80211_IOC_DELKEY:
2902 		error = ieee80211_ioctl_delkey(vap, ireq);
2903 		break;
2904 	case IEEE80211_IOC_MLME:
2905 		error = ieee80211_ioctl_setmlme(vap, ireq);
2906 		break;
2907 	case IEEE80211_IOC_COUNTERMEASURES:
2908 		if (ireq->i_val) {
2909 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2910 				return EOPNOTSUPP;
2911 			vap->iv_flags |= IEEE80211_F_COUNTERM;
2912 		} else
2913 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2914 		/* XXX ERESTART? */
2915 		break;
2916 	case IEEE80211_IOC_WPA:
2917 		if (ireq->i_val > 3)
2918 			return EINVAL;
2919 		/* XXX verify ciphers available */
2920 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2921 		switch (ireq->i_val) {
2922 		case 0:
2923 			/* wpa_supplicant calls this to clear the WPA config */
2924 			break;
2925 		case 1:
2926 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2927 				return EOPNOTSUPP;
2928 			flags |= IEEE80211_F_WPA1;
2929 			break;
2930 		case 2:
2931 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2932 				return EOPNOTSUPP;
2933 			flags |= IEEE80211_F_WPA2;
2934 			break;
2935 		case 3:
2936 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2937 				return EOPNOTSUPP;
2938 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2939 			break;
2940 		default:	/*  Can't set any -> error */
2941 			return EOPNOTSUPP;
2942 		}
2943 		vap->iv_flags = flags;
2944 		error = ERESTART;	/* NB: can change beacon frame */
2945 		break;
2946 	case IEEE80211_IOC_WME:
2947 		if (ireq->i_val) {
2948 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2949 				return EOPNOTSUPP;
2950 			ieee80211_syncflag(vap, IEEE80211_F_WME);
2951 		} else
2952 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2953 		error = ERESTART;	/* NB: can change beacon frame */
2954 		break;
2955 	case IEEE80211_IOC_HIDESSID:
2956 		if (ireq->i_val)
2957 			vap->iv_flags |= IEEE80211_F_HIDESSID;
2958 		else
2959 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2960 		error = ERESTART;		/* XXX ENETRESET? */
2961 		break;
2962 	case IEEE80211_IOC_APBRIDGE:
2963 		if (ireq->i_val == 0)
2964 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2965 		else
2966 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2967 		break;
2968 	case IEEE80211_IOC_BSSID:
2969 		if (ireq->i_len != sizeof(tmpbssid))
2970 			return EINVAL;
2971 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2972 		if (error)
2973 			break;
2974 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2975 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2976 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2977 		else
2978 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2979 		error = ENETRESET;
2980 		break;
2981 	case IEEE80211_IOC_CHANLIST:
2982 		error = ieee80211_ioctl_setchanlist(vap, ireq);
2983 		break;
2984 #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2985 #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2986 	case OLD_IEEE80211_IOC_SCAN_REQ:
2987 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2988 			"%s: active scan request\n", __func__);
2989 		/*
2990 		 * If we are in INIT state then the driver has never
2991 		 * had a chance to setup hardware state to do a scan;
2992 		 * use the state machine to get us up the SCAN state.
2993 		 * Otherwise just invoke the scan machinery to start
2994 		 * a one-time scan.
2995 		 */
2996 		if (vap->iv_state == IEEE80211_S_INIT)
2997 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2998 		else
2999 			(void) ieee80211_start_scan(vap,
3000 				IEEE80211_SCAN_ACTIVE |
3001 				IEEE80211_SCAN_NOPICK |
3002 				IEEE80211_SCAN_ONCE,
3003 				IEEE80211_SCAN_FOREVER, 0, 0,
3004 				/* XXX use ioctl params */
3005 				vap->iv_des_nssid, vap->iv_des_ssid);
3006 		break;
3007 #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
3008 	case IEEE80211_IOC_SCAN_REQ:
3009 		error = ieee80211_ioctl_scanreq(vap, ireq);
3010 		break;
3011 	case IEEE80211_IOC_SCAN_CANCEL:
3012 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
3013 		    "%s: cancel scan\n", __func__);
3014 		ieee80211_cancel_scan(vap);
3015 		break;
3016 	case IEEE80211_IOC_HTCONF:
3017 		if (ireq->i_val & 1)
3018 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
3019 		else
3020 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
3021 		if (ireq->i_val & 2)
3022 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
3023 		else
3024 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
3025 		error = ENETRESET;
3026 		break;
3027 	case IEEE80211_IOC_ADDMAC:
3028 	case IEEE80211_IOC_DELMAC:
3029 		error = ieee80211_ioctl_macmac(vap, ireq);
3030 		break;
3031 	case IEEE80211_IOC_MACCMD:
3032 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
3033 		break;
3034 	case IEEE80211_IOC_STA_STATS:
3035 		error = ieee80211_ioctl_setstastats(vap, ireq);
3036 		break;
3037 	case IEEE80211_IOC_STA_TXPOW:
3038 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
3039 		break;
3040 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
3041 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
3042 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
3043 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
3044 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
3045 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
3046 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
3047 		break;
3048 	case IEEE80211_IOC_DTIM_PERIOD:
3049 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3050 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3051 		    vap->iv_opmode != IEEE80211_M_IBSS)
3052 			return EINVAL;
3053 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
3054 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
3055 			vap->iv_dtim_period = ireq->i_val;
3056 			error = ENETRESET;		/* requires restart */
3057 		} else
3058 			error = EINVAL;
3059 		break;
3060 	case IEEE80211_IOC_BEACON_INTERVAL:
3061 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3062 		    vap->iv_opmode != IEEE80211_M_MBSS &&
3063 		    vap->iv_opmode != IEEE80211_M_IBSS)
3064 			return EINVAL;
3065 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
3066 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
3067 			ic->ic_bintval = ireq->i_val;
3068 			error = ENETRESET;		/* requires restart */
3069 		} else
3070 			error = EINVAL;
3071 		break;
3072 	case IEEE80211_IOC_PUREG:
3073 		if (ireq->i_val)
3074 			vap->iv_flags |= IEEE80211_F_PUREG;
3075 		else
3076 			vap->iv_flags &= ~IEEE80211_F_PUREG;
3077 		/* NB: reset only if we're operating on an 11g channel */
3078 		if (isvap11g(vap))
3079 			error = ENETRESET;
3080 		break;
3081 	case IEEE80211_IOC_QUIET:
3082 		vap->iv_quiet= ireq->i_val;
3083 		break;
3084 	case IEEE80211_IOC_QUIET_COUNT:
3085 		vap->iv_quiet_count=ireq->i_val;
3086 		break;
3087 	case IEEE80211_IOC_QUIET_PERIOD:
3088 		vap->iv_quiet_period=ireq->i_val;
3089 		break;
3090 	case IEEE80211_IOC_QUIET_OFFSET:
3091 		vap->iv_quiet_offset=ireq->i_val;
3092 		break;
3093 	case IEEE80211_IOC_QUIET_DUR:
3094 		if(ireq->i_val < vap->iv_bss->ni_intval)
3095 			vap->iv_quiet_duration = ireq->i_val;
3096 		else
3097 			error = EINVAL;
3098 		break;
3099 	case IEEE80211_IOC_BGSCAN:
3100 		if (ireq->i_val) {
3101 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3102 				return EOPNOTSUPP;
3103 			vap->iv_flags |= IEEE80211_F_BGSCAN;
3104 		} else
3105 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
3106 		break;
3107 	case IEEE80211_IOC_BGSCAN_IDLE:
3108 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3109 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
3110 		else
3111 			error = EINVAL;
3112 		break;
3113 	case IEEE80211_IOC_BGSCAN_INTERVAL:
3114 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3115 			vap->iv_bgscanintvl = ireq->i_val*hz;
3116 		else
3117 			error = EINVAL;
3118 		break;
3119 	case IEEE80211_IOC_SCANVALID:
3120 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3121 			vap->iv_scanvalid = ireq->i_val*hz;
3122 		else
3123 			error = EINVAL;
3124 		break;
3125 	case IEEE80211_IOC_FRAGTHRESHOLD:
3126 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
3127 		    ireq->i_val != IEEE80211_FRAG_MAX)
3128 			return EOPNOTSUPP;
3129 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
3130 		      ireq->i_val <= IEEE80211_FRAG_MAX))
3131 			return EINVAL;
3132 		vap->iv_fragthreshold = ireq->i_val;
3133 		error = ERESTART;
3134 		break;
3135 	case IEEE80211_IOC_BURST:
3136 		if (ireq->i_val) {
3137 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3138 				return EOPNOTSUPP;
3139 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3140 		} else
3141 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3142 		error = ERESTART;
3143 		break;
3144 	case IEEE80211_IOC_BMISSTHRESHOLD:
3145 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3146 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3147 			return EINVAL;
3148 		vap->iv_bmissthreshold = ireq->i_val;
3149 		error = ERESTART;
3150 		break;
3151 	case IEEE80211_IOC_CURCHAN:
3152 		error = ieee80211_ioctl_setcurchan(vap, ireq);
3153 		break;
3154 	case IEEE80211_IOC_SHORTGI:
3155 		if (ireq->i_val) {
3156 #define	IEEE80211_HTCAP_SHORTGI \
3157 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3158 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
3159 				return EINVAL;
3160 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
3161 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
3162 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
3163 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
3164 #undef IEEE80211_HTCAP_SHORTGI
3165 		} else
3166 			vap->iv_flags_ht &=
3167 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3168 		error = ERESTART;
3169 		break;
3170 	case IEEE80211_IOC_AMPDU:
3171 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
3172 			return EINVAL;
3173 		if (ireq->i_val & 1)
3174 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3175 		else
3176 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3177 		if (ireq->i_val & 2)
3178 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3179 		else
3180 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
3181 		/* NB: reset only if we're operating on an 11n channel */
3182 		if (isvapht(vap))
3183 			error = ERESTART;
3184 		break;
3185 	case IEEE80211_IOC_AMPDU_LIMIT:
3186 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
3187 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3188 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3189 			return EINVAL;
3190 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3191 			vap->iv_ampdu_rxmax = ireq->i_val;
3192 		else
3193 			vap->iv_ampdu_limit = ireq->i_val;
3194 		error = ERESTART;
3195 		break;
3196 	case IEEE80211_IOC_AMPDU_DENSITY:
3197 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3198 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3199 			return EINVAL;
3200 		vap->iv_ampdu_density = ireq->i_val;
3201 		error = ERESTART;
3202 		break;
3203 	case IEEE80211_IOC_AMSDU:
3204 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
3205 			return EINVAL;
3206 		if (ireq->i_val & 1)
3207 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3208 		else
3209 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
3210 		if (ireq->i_val & 2)
3211 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3212 		else
3213 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
3214 		/* NB: reset only if we're operating on an 11n channel */
3215 		if (isvapht(vap))
3216 			error = ERESTART;
3217 		break;
3218 	case IEEE80211_IOC_AMSDU_LIMIT:
3219 		/* XXX validate */
3220 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
3221 		break;
3222 	case IEEE80211_IOC_PUREN:
3223 		if (ireq->i_val) {
3224 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3225 				return EINVAL;
3226 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
3227 		} else
3228 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
3229 		/* NB: reset only if we're operating on an 11n channel */
3230 		if (isvapht(vap))
3231 			error = ERESTART;
3232 		break;
3233 	case IEEE80211_IOC_DOTH:
3234 		if (ireq->i_val) {
3235 #if 0
3236 			/* XXX no capability */
3237 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3238 				return EOPNOTSUPP;
3239 #endif
3240 			vap->iv_flags |= IEEE80211_F_DOTH;
3241 		} else
3242 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3243 		error = ENETRESET;
3244 		break;
3245 	case IEEE80211_IOC_REGDOMAIN:
3246 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3247 		break;
3248 	case IEEE80211_IOC_ROAM:
3249 		error = ieee80211_ioctl_setroam(vap, ireq);
3250 		break;
3251 	case IEEE80211_IOC_TXPARAMS:
3252 		error = ieee80211_ioctl_settxparams(vap, ireq);
3253 		break;
3254 	case IEEE80211_IOC_HTCOMPAT:
3255 		if (ireq->i_val) {
3256 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3257 				return EOPNOTSUPP;
3258 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
3259 		} else
3260 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
3261 		/* NB: reset only if we're operating on an 11n channel */
3262 		if (isvapht(vap))
3263 			error = ERESTART;
3264 		break;
3265 	case IEEE80211_IOC_DWDS:
3266 		if (ireq->i_val) {
3267 			/* NB: DWDS only makes sense for WDS-capable devices */
3268 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3269 				return EOPNOTSUPP;
3270 			/* NB: DWDS is used only with ap+sta vaps */
3271 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3272 			    vap->iv_opmode != IEEE80211_M_STA)
3273 				return EINVAL;
3274 			vap->iv_flags |= IEEE80211_F_DWDS;
3275 			if (vap->iv_opmode == IEEE80211_M_STA)
3276 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3277 		} else {
3278 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3279 			if (vap->iv_opmode == IEEE80211_M_STA)
3280 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3281 		}
3282 		break;
3283 	case IEEE80211_IOC_INACTIVITY:
3284 		if (ireq->i_val)
3285 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3286 		else
3287 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3288 		break;
3289 	case IEEE80211_IOC_APPIE:
3290 		error = ieee80211_ioctl_setappie(vap, ireq);
3291 		break;
3292 	case IEEE80211_IOC_WPS:
3293 		if (ireq->i_val) {
3294 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3295 				return EOPNOTSUPP;
3296 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3297 		} else
3298 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3299 		break;
3300 	case IEEE80211_IOC_TSN:
3301 		if (ireq->i_val) {
3302 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3303 				return EOPNOTSUPP;
3304 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3305 		} else
3306 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3307 		break;
3308 	case IEEE80211_IOC_CHANSWITCH:
3309 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3310 		break;
3311 	case IEEE80211_IOC_DFS:
3312 		if (ireq->i_val) {
3313 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3314 				return EOPNOTSUPP;
3315 			/* NB: DFS requires 11h support */
3316 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3317 				return EINVAL;
3318 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3319 		} else
3320 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3321 		break;
3322 	case IEEE80211_IOC_DOTD:
3323 		if (ireq->i_val)
3324 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3325 		else
3326 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3327 		if (vap->iv_opmode == IEEE80211_M_STA)
3328 			error = ENETRESET;
3329 		break;
3330 	case IEEE80211_IOC_HTPROTMODE:
3331 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3332 			return EINVAL;
3333 		ic->ic_htprotmode = ireq->i_val ?
3334 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3335 		/* NB: if not operating in 11n this can wait */
3336 		if (isvapht(vap))
3337 			error = ERESTART;
3338 		break;
3339 	case IEEE80211_IOC_STA_VLAN:
3340 		error = ieee80211_ioctl_setstavlan(vap, ireq);
3341 		break;
3342 	case IEEE80211_IOC_SMPS:
3343 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3344 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3345 			return EINVAL;
3346 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3347 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3348 			return EOPNOTSUPP;
3349 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3350 			ireq->i_val;
3351 		/* NB: if not operating in 11n this can wait */
3352 		if (isvapht(vap))
3353 			error = ERESTART;
3354 		break;
3355 	case IEEE80211_IOC_RIFS:
3356 		if (ireq->i_val != 0) {
3357 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3358 				return EOPNOTSUPP;
3359 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
3360 		} else
3361 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
3362 		/* NB: if not operating in 11n this can wait */
3363 		if (isvapht(vap))
3364 			error = ERESTART;
3365 		break;
3366 	case IEEE80211_IOC_STBC:
3367 		/* Check if we can do STBC TX/RX before changing the setting */
3368 		if ((ireq->i_val & 1) &&
3369 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
3370 			return EOPNOTSUPP;
3371 		if ((ireq->i_val & 2) &&
3372 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
3373 			return EOPNOTSUPP;
3374 
3375 		/* TX */
3376 		if (ireq->i_val & 1)
3377 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
3378 		else
3379 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
3380 
3381 		/* RX */
3382 		if (ireq->i_val & 2)
3383 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
3384 		else
3385 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
3386 
3387 		/* NB: reset only if we're operating on an 11n channel */
3388 		if (isvapht(vap))
3389 			error = ERESTART;
3390 		break;
3391 	case IEEE80211_IOC_LDPC:
3392 		/* Check if we can do LDPC TX/RX before changing the setting */
3393 		if ((ireq->i_val & 1) &&
3394 		    (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0)
3395 			return EOPNOTSUPP;
3396 		if ((ireq->i_val & 2) &&
3397 		    (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0)
3398 			return EOPNOTSUPP;
3399 
3400 		/* TX */
3401 		if (ireq->i_val & 1)
3402 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
3403 		else
3404 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX;
3405 
3406 		/* RX */
3407 		if (ireq->i_val & 2)
3408 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
3409 		else
3410 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX;
3411 
3412 		/* NB: reset only if we're operating on an 11n channel */
3413 		if (isvapht(vap))
3414 			error = ERESTART;
3415 		break;
3416 
3417 	/* VHT */
3418 	case IEEE80211_IOC_VHTCONF:
3419 		if (ireq->i_val & 1)
3420 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT);
3421 		else
3422 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT);
3423 
3424 		if (ireq->i_val & 2)
3425 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40);
3426 		else
3427 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40);
3428 
3429 		if (ireq->i_val & 4)
3430 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80);
3431 		else
3432 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80);
3433 
3434 		if (ireq->i_val & 8)
3435 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80);
3436 		else
3437 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80);
3438 
3439 		if (ireq->i_val & 16)
3440 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160);
3441 		else
3442 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160);
3443 
3444 		error = ENETRESET;
3445 		break;
3446 
3447 	default:
3448 		error = ieee80211_ioctl_setdefault(vap, ireq);
3449 		break;
3450 	}
3451 	/*
3452 	 * The convention is that ENETRESET means an operation
3453 	 * requires a complete re-initialization of the device (e.g.
3454 	 * changing something that affects the association state).
3455 	 * ERESTART means the request may be handled with only a
3456 	 * reload of the hardware state.  We hand ERESTART requests
3457 	 * to the iv_reset callback so the driver can decide.  If
3458 	 * a device does not fillin iv_reset then it defaults to one
3459 	 * that returns ENETRESET.  Otherwise a driver may return
3460 	 * ENETRESET (in which case a full reset will be done) or
3461 	 * 0 to mean there's no need to do anything (e.g. when the
3462 	 * change has no effect on the driver/device).
3463 	 */
3464 	if (error == ERESTART)
3465 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3466 		    vap->iv_reset(vap, ireq->i_type) : 0;
3467 	if (error == ENETRESET) {
3468 		/* XXX need to re-think AUTO handling */
3469 		if (IS_UP_AUTO(vap))
3470 			ieee80211_init(vap);
3471 		error = 0;
3472 	}
3473 	return error;
3474 }
3475 
3476 int
3477 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3478 {
3479 	struct ieee80211vap *vap = ifp->if_softc;
3480 	struct ieee80211com *ic = vap->iv_ic;
3481 	int error = 0, wait = 0;
3482 	struct ifreq *ifr;
3483 	struct ifaddr *ifa;			/* XXX */
3484 
3485 	switch (cmd) {
3486 	case SIOCSIFFLAGS:
3487 		IEEE80211_LOCK(ic);
3488 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
3489 			/*
3490 			 * Enable promiscuous mode when:
3491 			 * 1. Interface is not a member of bridge, or
3492 			 * 2. Requested by user, or
3493 			 * 3. In monitor (or adhoc-demo) mode.
3494 			 */
3495 			if (ifp->if_bridge == NULL ||
3496 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
3497 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
3498 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
3499 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
3500 				ieee80211_promisc(vap,
3501 				    ifp->if_flags & IFF_PROMISC);
3502 				vap->iv_ifflags ^= IFF_PROMISC;
3503 			}
3504 		}
3505 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
3506 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
3507 			vap->iv_ifflags ^= IFF_ALLMULTI;
3508 		}
3509 		if (ifp->if_flags & IFF_UP) {
3510 			/*
3511 			 * Bring ourself up unless we're already operational.
3512 			 * If we're the first vap and the parent is not up
3513 			 * then it will automatically be brought up as a
3514 			 * side-effect of bringing ourself up.
3515 			 */
3516 			if (vap->iv_state == IEEE80211_S_INIT) {
3517 				if (ic->ic_nrunning == 0)
3518 					wait = 1;
3519 				ieee80211_start_locked(vap);
3520 			}
3521 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3522 			/*
3523 			 * Stop ourself.  If we are the last vap to be
3524 			 * marked down the parent will also be taken down.
3525 			 */
3526 			if (ic->ic_nrunning == 1)
3527 				wait = 1;
3528 			ieee80211_stop_locked(vap);
3529 		}
3530 		IEEE80211_UNLOCK(ic);
3531 		/* Wait for parent ioctl handler if it was queued */
3532 		if (wait) {
3533 			ieee80211_waitfor_parent(ic);
3534 
3535 			/*
3536 			 * Check if the MAC address was changed
3537 			 * via SIOCSIFLLADDR ioctl.
3538 			 */
3539 			if_addr_rlock(ifp);
3540 			if ((ifp->if_flags & IFF_UP) == 0 &&
3541 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
3542 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
3543 				    IF_LLADDR(ifp));
3544 			if_addr_runlock(ifp);
3545 		}
3546 		break;
3547 	case SIOCADDMULTI:
3548 	case SIOCDELMULTI:
3549 		ieee80211_runtask(ic, &ic->ic_mcast_task);
3550 		break;
3551 	case SIOCSIFMEDIA:
3552 	case SIOCGIFMEDIA:
3553 		ifr = (struct ifreq *)data;
3554 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3555 		break;
3556 	case SIOCG80211:
3557 		error = ieee80211_ioctl_get80211(vap, cmd,
3558 				(struct ieee80211req *) data);
3559 		break;
3560 	case SIOCS80211:
3561 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3562 		if (error == 0)
3563 			error = ieee80211_ioctl_set80211(vap, cmd,
3564 					(struct ieee80211req *) data);
3565 		break;
3566 	case SIOCG80211STATS:
3567 		ifr = (struct ifreq *)data;
3568 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3569 		break;
3570 	case SIOCSIFMTU:
3571 		ifr = (struct ifreq *)data;
3572 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3573 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3574 			error = EINVAL;
3575 		else
3576 			ifp->if_mtu = ifr->ifr_mtu;
3577 		break;
3578 	case SIOCSIFADDR:
3579 		/*
3580 		 * XXX Handle this directly so we can suppress if_init calls.
3581 		 * XXX This should be done in ether_ioctl but for the moment
3582 		 * XXX there are too many other parts of the system that
3583 		 * XXX set IFF_UP and so suppress if_init being called when
3584 		 * XXX it should be.
3585 		 */
3586 		ifa = (struct ifaddr *) data;
3587 		switch (ifa->ifa_addr->sa_family) {
3588 #ifdef INET
3589 		case AF_INET:
3590 			if ((ifp->if_flags & IFF_UP) == 0) {
3591 				ifp->if_flags |= IFF_UP;
3592 				ifp->if_init(ifp->if_softc);
3593 			}
3594 			arp_ifinit(ifp, ifa);
3595 			break;
3596 #endif
3597 		default:
3598 			if ((ifp->if_flags & IFF_UP) == 0) {
3599 				ifp->if_flags |= IFF_UP;
3600 				ifp->if_init(ifp->if_softc);
3601 			}
3602 			break;
3603 		}
3604 		break;
3605 	default:
3606 		/*
3607 		 * Pass unknown ioctls first to the driver, and if it
3608 		 * returns ENOTTY, then to the generic Ethernet handler.
3609 		 */
3610 		if (ic->ic_ioctl != NULL &&
3611 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
3612 			break;
3613 		error = ether_ioctl(ifp, cmd, data);
3614 		break;
3615 	}
3616 	return (error);
3617 }
3618