xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision cc349066556bcdeed0d6cc72aad340d0f383e35c)
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 	ci = &dc->dc_chaninfo;
714 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
715 	KASSERT(ci->ic_nchans <= maxchans,
716 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
717 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
718 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
719 	IEEE80211_FREE(dc, M_TEMP);
720 	return error;
721 }
722 
723 static int
724 ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
725 {
726 	struct ieee80211_node *ni;
727 	struct ieee80211req_sta_vlan vlan;
728 	int error;
729 
730 	if (ireq->i_len != sizeof(vlan))
731 		return EINVAL;
732 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
733 	if (error != 0)
734 		return error;
735 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
736 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
737 		    vlan.sv_macaddr);
738 		if (ni == NULL)
739 			return ENOENT;
740 	} else
741 		ni = ieee80211_ref_node(vap->iv_bss);
742 	vlan.sv_vlan = ni->ni_vlan;
743 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
744 	ieee80211_free_node(ni);
745 	return error;
746 }
747 
748 /*
749  * Dummy ioctl get handler so the linker set is defined.
750  */
751 static int
752 dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
753 {
754 	return ENOSYS;
755 }
756 IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
757 
758 static int
759 ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
760 {
761 	ieee80211_ioctl_getfunc * const *get;
762 	int error;
763 
764 	SET_FOREACH(get, ieee80211_ioctl_getset) {
765 		error = (*get)(vap, ireq);
766 		if (error != ENOSYS)
767 			return error;
768 	}
769 	return EINVAL;
770 }
771 
772 static int
773 ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
774     struct ieee80211req *ireq)
775 {
776 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
777 	struct ieee80211com *ic = vap->iv_ic;
778 	u_int kid, len;
779 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
780 	char tmpssid[IEEE80211_NWID_LEN];
781 	int error = 0;
782 
783 	switch (ireq->i_type) {
784 	case IEEE80211_IOC_SSID:
785 		switch (vap->iv_state) {
786 		case IEEE80211_S_INIT:
787 		case IEEE80211_S_SCAN:
788 			ireq->i_len = vap->iv_des_ssid[0].len;
789 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
790 			break;
791 		default:
792 			ireq->i_len = vap->iv_bss->ni_esslen;
793 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
794 			break;
795 		}
796 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
797 		break;
798 	case IEEE80211_IOC_NUMSSIDS:
799 		ireq->i_val = 1;
800 		break;
801 	case IEEE80211_IOC_WEP:
802 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
803 			ireq->i_val = IEEE80211_WEP_OFF;
804 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
805 			ireq->i_val = IEEE80211_WEP_ON;
806 		else
807 			ireq->i_val = IEEE80211_WEP_MIXED;
808 		break;
809 	case IEEE80211_IOC_WEPKEY:
810 		kid = (u_int) ireq->i_val;
811 		if (kid >= IEEE80211_WEP_NKID)
812 			return EINVAL;
813 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
814 		/* NB: only root can read WEP keys */
815 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
816 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
817 		} else {
818 			bzero(tmpkey, len);
819 		}
820 		ireq->i_len = len;
821 		error = copyout(tmpkey, ireq->i_data, len);
822 		break;
823 	case IEEE80211_IOC_NUMWEPKEYS:
824 		ireq->i_val = IEEE80211_WEP_NKID;
825 		break;
826 	case IEEE80211_IOC_WEPTXKEY:
827 		ireq->i_val = vap->iv_def_txkey;
828 		break;
829 	case IEEE80211_IOC_AUTHMODE:
830 		if (vap->iv_flags & IEEE80211_F_WPA)
831 			ireq->i_val = IEEE80211_AUTH_WPA;
832 		else
833 			ireq->i_val = vap->iv_bss->ni_authmode;
834 		break;
835 	case IEEE80211_IOC_CHANNEL:
836 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
837 		break;
838 	case IEEE80211_IOC_POWERSAVE:
839 		if (vap->iv_flags & IEEE80211_F_PMGTON)
840 			ireq->i_val = IEEE80211_POWERSAVE_ON;
841 		else
842 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
843 		break;
844 	case IEEE80211_IOC_POWERSAVESLEEP:
845 		ireq->i_val = ic->ic_lintval;
846 		break;
847 	case IEEE80211_IOC_RTSTHRESHOLD:
848 		ireq->i_val = vap->iv_rtsthreshold;
849 		break;
850 	case IEEE80211_IOC_PROTMODE:
851 		ireq->i_val = ic->ic_protmode;
852 		break;
853 	case IEEE80211_IOC_TXPOWER:
854 		/*
855 		 * Tx power limit is the min of max regulatory
856 		 * power, any user-set limit, and the max the
857 		 * radio can do.
858 		 *
859 		 * TODO: methodize this
860 		 */
861 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
862 		if (ireq->i_val > ic->ic_txpowlimit)
863 			ireq->i_val = ic->ic_txpowlimit;
864 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
865 			ireq->i_val = ic->ic_curchan->ic_maxpower;
866 		break;
867 	case IEEE80211_IOC_WPA:
868 		switch (vap->iv_flags & IEEE80211_F_WPA) {
869 		case IEEE80211_F_WPA1:
870 			ireq->i_val = 1;
871 			break;
872 		case IEEE80211_F_WPA2:
873 			ireq->i_val = 2;
874 			break;
875 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
876 			ireq->i_val = 3;
877 			break;
878 		default:
879 			ireq->i_val = 0;
880 			break;
881 		}
882 		break;
883 	case IEEE80211_IOC_CHANLIST:
884 		error = ieee80211_ioctl_getchanlist(vap, ireq);
885 		break;
886 	case IEEE80211_IOC_ROAMING:
887 		ireq->i_val = vap->iv_roaming;
888 		break;
889 	case IEEE80211_IOC_PRIVACY:
890 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
891 		break;
892 	case IEEE80211_IOC_DROPUNENCRYPTED:
893 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
894 		break;
895 	case IEEE80211_IOC_COUNTERMEASURES:
896 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
897 		break;
898 	case IEEE80211_IOC_WME:
899 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
900 		break;
901 	case IEEE80211_IOC_HIDESSID:
902 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
903 		break;
904 	case IEEE80211_IOC_APBRIDGE:
905 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
906 		break;
907 	case IEEE80211_IOC_WPAKEY:
908 		error = ieee80211_ioctl_getkey(vap, ireq);
909 		break;
910 	case IEEE80211_IOC_CHANINFO:
911 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
912 		break;
913 	case IEEE80211_IOC_BSSID:
914 		if (ireq->i_len != IEEE80211_ADDR_LEN)
915 			return EINVAL;
916 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
917 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
918 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
919 			    ireq->i_data, ireq->i_len);
920 		} else
921 			error = copyout(vap->iv_des_bssid, ireq->i_data,
922 			    ireq->i_len);
923 		break;
924 	case IEEE80211_IOC_WPAIE:
925 	case IEEE80211_IOC_WPAIE2:
926 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
927 		break;
928 	case IEEE80211_IOC_SCAN_RESULTS:
929 		error = ieee80211_ioctl_getscanresults(vap, ireq);
930 		break;
931 	case IEEE80211_IOC_STA_STATS:
932 		error = ieee80211_ioctl_getstastats(vap, ireq);
933 		break;
934 	case IEEE80211_IOC_TXPOWMAX:
935 		ireq->i_val = vap->iv_bss->ni_txpower;
936 		break;
937 	case IEEE80211_IOC_STA_TXPOW:
938 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
939 		break;
940 	case IEEE80211_IOC_STA_INFO:
941 		error = ieee80211_ioctl_getstainfo(vap, ireq);
942 		break;
943 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
944 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
945 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
946 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
947 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
948 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
949 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
950 		break;
951 	case IEEE80211_IOC_DTIM_PERIOD:
952 		ireq->i_val = vap->iv_dtim_period;
953 		break;
954 	case IEEE80211_IOC_BEACON_INTERVAL:
955 		/* NB: get from ic_bss for station mode */
956 		ireq->i_val = vap->iv_bss->ni_intval;
957 		break;
958 	case IEEE80211_IOC_PUREG:
959 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
960 		break;
961 	case IEEE80211_IOC_QUIET:
962 		ireq->i_val = vap->iv_quiet;
963 		break;
964 	case IEEE80211_IOC_QUIET_COUNT:
965 		ireq->i_val = vap->iv_quiet_count;
966 		break;
967 	case IEEE80211_IOC_QUIET_PERIOD:
968 		ireq->i_val = vap->iv_quiet_period;
969 		break;
970 	case IEEE80211_IOC_QUIET_DUR:
971 		ireq->i_val = vap->iv_quiet_duration;
972 		break;
973 	case IEEE80211_IOC_QUIET_OFFSET:
974 		ireq->i_val = vap->iv_quiet_offset;
975 		break;
976 	case IEEE80211_IOC_BGSCAN:
977 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
978 		break;
979 	case IEEE80211_IOC_BGSCAN_IDLE:
980 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
981 		break;
982 	case IEEE80211_IOC_BGSCAN_INTERVAL:
983 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
984 		break;
985 	case IEEE80211_IOC_SCANVALID:
986 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
987 		break;
988 	case IEEE80211_IOC_FRAGTHRESHOLD:
989 		ireq->i_val = vap->iv_fragthreshold;
990 		break;
991 	case IEEE80211_IOC_MACCMD:
992 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
993 		break;
994 	case IEEE80211_IOC_BURST:
995 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
996 		break;
997 	case IEEE80211_IOC_BMISSTHRESHOLD:
998 		ireq->i_val = vap->iv_bmissthreshold;
999 		break;
1000 	case IEEE80211_IOC_CURCHAN:
1001 		error = ieee80211_ioctl_getcurchan(vap, ireq);
1002 		break;
1003 	case IEEE80211_IOC_SHORTGI:
1004 		ireq->i_val = 0;
1005 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1006 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
1007 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1008 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
1009 		break;
1010 	case IEEE80211_IOC_AMPDU:
1011 		ireq->i_val = 0;
1012 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
1013 			ireq->i_val |= 1;
1014 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
1015 			ireq->i_val |= 2;
1016 		break;
1017 	case IEEE80211_IOC_AMPDU_LIMIT:
1018 		/* XXX TODO: make this a per-node thing; and leave this as global */
1019 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1020 			ireq->i_val = vap->iv_ampdu_rxmax;
1021 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
1022 			/*
1023 			 * XXX TODO: this isn't completely correct, as we've
1024 			 * negotiated the higher of the two.
1025 			 */
1026 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1027 			    IEEE80211_HTCAP_MAXRXAMPDU);
1028 		else
1029 			ireq->i_val = vap->iv_ampdu_limit;
1030 		break;
1031 	case IEEE80211_IOC_AMPDU_DENSITY:
1032 		/* XXX TODO: make this a per-node thing; and leave this as global */
1033 		if (vap->iv_opmode == IEEE80211_M_STA &&
1034 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1035 			/*
1036 			 * XXX TODO: this isn't completely correct, as we've
1037 			 * negotiated the higher of the two.
1038 			 */
1039 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
1040 			    IEEE80211_HTCAP_MPDUDENSITY);
1041 		else
1042 			ireq->i_val = vap->iv_ampdu_density;
1043 		break;
1044 	case IEEE80211_IOC_AMSDU:
1045 		ireq->i_val = 0;
1046 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
1047 			ireq->i_val |= 1;
1048 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
1049 			ireq->i_val |= 2;
1050 		break;
1051 	case IEEE80211_IOC_AMSDU_LIMIT:
1052 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
1053 		break;
1054 	case IEEE80211_IOC_PUREN:
1055 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
1056 		break;
1057 	case IEEE80211_IOC_DOTH:
1058 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
1059 		break;
1060 	case IEEE80211_IOC_REGDOMAIN:
1061 		error = ieee80211_ioctl_getregdomain(vap, ireq);
1062 		break;
1063 	case IEEE80211_IOC_ROAM:
1064 		error = ieee80211_ioctl_getroam(vap, ireq);
1065 		break;
1066 	case IEEE80211_IOC_TXPARAMS:
1067 		error = ieee80211_ioctl_gettxparams(vap, ireq);
1068 		break;
1069 	case IEEE80211_IOC_HTCOMPAT:
1070 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
1071 		break;
1072 	case IEEE80211_IOC_DWDS:
1073 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
1074 		break;
1075 	case IEEE80211_IOC_INACTIVITY:
1076 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
1077 		break;
1078 	case IEEE80211_IOC_APPIE:
1079 		error = ieee80211_ioctl_getappie(vap, ireq);
1080 		break;
1081 	case IEEE80211_IOC_WPS:
1082 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
1083 		break;
1084 	case IEEE80211_IOC_TSN:
1085 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
1086 		break;
1087 	case IEEE80211_IOC_DFS:
1088 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
1089 		break;
1090 	case IEEE80211_IOC_DOTD:
1091 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
1092 		break;
1093 	case IEEE80211_IOC_DEVCAPS:
1094 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
1095 		break;
1096 	case IEEE80211_IOC_HTPROTMODE:
1097 		ireq->i_val = ic->ic_htprotmode;
1098 		break;
1099 	case IEEE80211_IOC_HTCONF:
1100 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
1101 			ireq->i_val = 1;
1102 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
1103 				ireq->i_val |= 2;
1104 		} else
1105 			ireq->i_val = 0;
1106 		break;
1107 	case IEEE80211_IOC_STA_VLAN:
1108 		error = ieee80211_ioctl_getstavlan(vap, ireq);
1109 		break;
1110 	case IEEE80211_IOC_SMPS:
1111 		if (vap->iv_opmode == IEEE80211_M_STA &&
1112 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
1113 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
1114 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
1115 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
1116 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
1117 			else
1118 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
1119 		} else
1120 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
1121 		break;
1122 	case IEEE80211_IOC_RIFS:
1123 		if (vap->iv_opmode == IEEE80211_M_STA &&
1124 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
1125 			ireq->i_val =
1126 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
1127 		else
1128 			ireq->i_val =
1129 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
1130 		break;
1131 	case IEEE80211_IOC_STBC:
1132 		ireq->i_val = 0;
1133 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
1134 			ireq->i_val |= 1;
1135 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
1136 			ireq->i_val |= 2;
1137 		break;
1138 	default:
1139 		error = ieee80211_ioctl_getdefault(vap, ireq);
1140 		break;
1141 	}
1142 	return error;
1143 #undef MS
1144 }
1145 
1146 static int
1147 ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1148 {
1149 	struct ieee80211req_key ik;
1150 	struct ieee80211_node *ni;
1151 	struct ieee80211_key *wk;
1152 	uint16_t kid;
1153 	int error, i;
1154 
1155 	if (ireq->i_len != sizeof(ik))
1156 		return EINVAL;
1157 	error = copyin(ireq->i_data, &ik, sizeof(ik));
1158 	if (error)
1159 		return error;
1160 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
1161 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
1162 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
1163 		return E2BIG;
1164 	kid = ik.ik_keyix;
1165 	if (kid == IEEE80211_KEYIX_NONE) {
1166 		/* XXX unicast keys currently must be tx/rx */
1167 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1168 			return EINVAL;
1169 		if (vap->iv_opmode == IEEE80211_M_STA) {
1170 			ni = ieee80211_ref_node(vap->iv_bss);
1171 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1172 				ieee80211_free_node(ni);
1173 				return EADDRNOTAVAIL;
1174 			}
1175 		} else {
1176 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1177 				ik.ik_macaddr);
1178 			if (ni == NULL)
1179 				return ENOENT;
1180 		}
1181 		wk = &ni->ni_ucastkey;
1182 	} else {
1183 		if (kid >= IEEE80211_WEP_NKID)
1184 			return EINVAL;
1185 		wk = &vap->iv_nw_keys[kid];
1186 		/*
1187 		 * Global slots start off w/o any assigned key index.
1188 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1189 		 */
1190 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1191 			wk->wk_keyix = kid;
1192 		ni = NULL;
1193 	}
1194 	error = 0;
1195 	ieee80211_key_update_begin(vap);
1196 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
1197 		wk->wk_keylen = ik.ik_keylen;
1198 		/* NB: MIC presence is implied by cipher type */
1199 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
1200 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
1201 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
1202 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
1203 		wk->wk_keytsc = 0;			/* new key, reset */
1204 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
1205 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
1206 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
1207 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
1208 		if (!ieee80211_crypto_setkey(vap, wk))
1209 			error = EIO;
1210 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
1211 			/*
1212 			 * Inform the driver that this is the default
1213 			 * transmit key.  Now, ideally we'd just set
1214 			 * a flag in the key update that would
1215 			 * say "yes, we're the default key", but
1216 			 * that currently isn't the way the ioctl ->
1217 			 * key interface works.
1218 			 */
1219 			ieee80211_crypto_set_deftxkey(vap, kid);
1220 	} else
1221 		error = ENXIO;
1222 	ieee80211_key_update_end(vap);
1223 	if (ni != NULL)
1224 		ieee80211_free_node(ni);
1225 	return error;
1226 }
1227 
1228 static int
1229 ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
1230 {
1231 	struct ieee80211req_del_key dk;
1232 	int kid, error;
1233 
1234 	if (ireq->i_len != sizeof(dk))
1235 		return EINVAL;
1236 	error = copyin(ireq->i_data, &dk, sizeof(dk));
1237 	if (error)
1238 		return error;
1239 	kid = dk.idk_keyix;
1240 	/* XXX uint8_t -> uint16_t */
1241 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
1242 		struct ieee80211_node *ni;
1243 
1244 		if (vap->iv_opmode == IEEE80211_M_STA) {
1245 			ni = ieee80211_ref_node(vap->iv_bss);
1246 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1247 				ieee80211_free_node(ni);
1248 				return EADDRNOTAVAIL;
1249 			}
1250 		} else {
1251 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
1252 				dk.idk_macaddr);
1253 			if (ni == NULL)
1254 				return ENOENT;
1255 		}
1256 		/* XXX error return */
1257 		ieee80211_node_delucastkey(ni);
1258 		ieee80211_free_node(ni);
1259 	} else {
1260 		if (kid >= IEEE80211_WEP_NKID)
1261 			return EINVAL;
1262 		/* XXX error return */
1263 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
1264 	}
1265 	return 0;
1266 }
1267 
1268 struct mlmeop {
1269 	struct ieee80211vap *vap;
1270 	int	op;
1271 	int	reason;
1272 };
1273 
1274 static void
1275 mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1276 	int op, int reason)
1277 {
1278 #ifdef IEEE80211_DEBUG
1279 	static const struct {
1280 		int mask;
1281 		const char *opstr;
1282 	} ops[] = {
1283 		{ 0, "op#0" },
1284 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1285 		  IEEE80211_MSG_ASSOC, "assoc" },
1286 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1287 		  IEEE80211_MSG_ASSOC, "disassoc" },
1288 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1289 		  IEEE80211_MSG_AUTH, "deauth" },
1290 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1291 		  IEEE80211_MSG_AUTH, "authorize" },
1292 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
1293 		  IEEE80211_MSG_AUTH, "unauthorize" },
1294 	};
1295 
1296 	if (op == IEEE80211_MLME_AUTH) {
1297 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
1298 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
1299 		    "station authenticate %s via MLME (reason: %d (%s))",
1300 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
1301 		    reason, ieee80211_reason_to_string(reason));
1302 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
1303 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
1304 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
1305 		    ieee80211_reason_to_string(reason));
1306 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
1307 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1308 		    "station %s via MLME", ops[op].opstr);
1309 	} else {
1310 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
1311 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
1312 		    reason, ieee80211_reason_to_string(reason));
1313 	}
1314 #endif /* IEEE80211_DEBUG */
1315 }
1316 
1317 static void
1318 domlme(void *arg, struct ieee80211_node *ni)
1319 {
1320 	struct mlmeop *mop = arg;
1321 	struct ieee80211vap *vap = ni->ni_vap;
1322 
1323 	if (vap != mop->vap)
1324 		return;
1325 	/*
1326 	 * NB: if ni_associd is zero then the node is already cleaned
1327 	 * up and we don't need to do this (we're safely holding a
1328 	 * reference but should otherwise not modify it's state).
1329 	 */
1330 	if (ni->ni_associd == 0)
1331 		return;
1332 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
1333 	if (mop->op == IEEE80211_MLME_DEAUTH) {
1334 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1335 		    mop->reason);
1336 	} else {
1337 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
1338 		    mop->reason);
1339 	}
1340 	ieee80211_node_leave(ni);
1341 }
1342 
1343 static int
1344 setmlme_dropsta(struct ieee80211vap *vap,
1345 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
1346 {
1347 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1348 	struct ieee80211_node *ni;
1349 	int error = 0;
1350 
1351 	/* NB: the broadcast address means do 'em all */
1352 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
1353 		IEEE80211_NODE_LOCK(nt);
1354 		ni = ieee80211_find_node_locked(nt, mac);
1355 		IEEE80211_NODE_UNLOCK(nt);
1356 		/*
1357 		 * Don't do the node update inside the node
1358 		 * table lock.  This unfortunately causes LORs
1359 		 * with drivers and their TX paths.
1360 		 */
1361 		if (ni != NULL) {
1362 			domlme(mlmeop, ni);
1363 			ieee80211_free_node(ni);
1364 		} else
1365 			error = ENOENT;
1366 	} else {
1367 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
1368 	}
1369 	return error;
1370 }
1371 
1372 static int
1373 setmlme_common(struct ieee80211vap *vap, int op,
1374 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
1375 {
1376 	struct ieee80211com *ic = vap->iv_ic;
1377 	struct ieee80211_node_table *nt = &ic->ic_sta;
1378 	struct ieee80211_node *ni;
1379 	struct mlmeop mlmeop;
1380 	int error;
1381 
1382 	error = 0;
1383 	switch (op) {
1384 	case IEEE80211_MLME_DISASSOC:
1385 	case IEEE80211_MLME_DEAUTH:
1386 		switch (vap->iv_opmode) {
1387 		case IEEE80211_M_STA:
1388 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1389 			/* XXX not quite right */
1390 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
1391 			break;
1392 		case IEEE80211_M_HOSTAP:
1393 			mlmeop.vap = vap;
1394 			mlmeop.op = op;
1395 			mlmeop.reason = reason;
1396 			error = setmlme_dropsta(vap, mac, &mlmeop);
1397 			break;
1398 		case IEEE80211_M_WDS:
1399 			/* XXX user app should send raw frame? */
1400 			if (op != IEEE80211_MLME_DEAUTH) {
1401 				error = EINVAL;
1402 				break;
1403 			}
1404 #if 0
1405 			/* XXX accept any address, simplifies user code */
1406 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
1407 				error = EINVAL;
1408 				break;
1409 			}
1410 #endif
1411 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
1412 			ni = ieee80211_ref_node(vap->iv_bss);
1413 			IEEE80211_SEND_MGMT(ni,
1414 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1415 			ieee80211_free_node(ni);
1416 			break;
1417 		case IEEE80211_M_MBSS:
1418 			IEEE80211_NODE_LOCK(nt);
1419 			ni = ieee80211_find_node_locked(nt, mac);
1420 			/*
1421 			 * Don't do the node update inside the node
1422 			 * table lock.  This unfortunately causes LORs
1423 			 * with drivers and their TX paths.
1424 			 */
1425 			IEEE80211_NODE_UNLOCK(nt);
1426 			if (ni != NULL) {
1427 				ieee80211_node_leave(ni);
1428 				ieee80211_free_node(ni);
1429 			} else {
1430 				error = ENOENT;
1431 			}
1432 			break;
1433 		default:
1434 			error = EINVAL;
1435 			break;
1436 		}
1437 		break;
1438 	case IEEE80211_MLME_AUTHORIZE:
1439 	case IEEE80211_MLME_UNAUTHORIZE:
1440 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
1441 		    vap->iv_opmode != IEEE80211_M_WDS) {
1442 			error = EINVAL;
1443 			break;
1444 		}
1445 		IEEE80211_NODE_LOCK(nt);
1446 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1447 		/*
1448 		 * Don't do the node update inside the node
1449 		 * table lock.  This unfortunately causes LORs
1450 		 * with drivers and their TX paths.
1451 		 */
1452 		IEEE80211_NODE_UNLOCK(nt);
1453 		if (ni != NULL) {
1454 			mlmedebug(vap, mac, op, reason);
1455 			if (op == IEEE80211_MLME_AUTHORIZE)
1456 				ieee80211_node_authorize(ni);
1457 			else
1458 				ieee80211_node_unauthorize(ni);
1459 			ieee80211_free_node(ni);
1460 		} else
1461 			error = ENOENT;
1462 		break;
1463 	case IEEE80211_MLME_AUTH:
1464 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
1465 			error = EINVAL;
1466 			break;
1467 		}
1468 		IEEE80211_NODE_LOCK(nt);
1469 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
1470 		/*
1471 		 * Don't do the node update inside the node
1472 		 * table lock.  This unfortunately causes LORs
1473 		 * with drivers and their TX paths.
1474 		 */
1475 		IEEE80211_NODE_UNLOCK(nt);
1476 		if (ni != NULL) {
1477 			mlmedebug(vap, mac, op, reason);
1478 			if (reason == IEEE80211_STATUS_SUCCESS) {
1479 				IEEE80211_SEND_MGMT(ni,
1480 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1481 				/*
1482 				 * For shared key auth, just continue the
1483 				 * exchange.  Otherwise when 802.1x is not in
1484 				 * use mark the port authorized at this point
1485 				 * so traffic can flow.
1486 				 */
1487 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
1488 				    ni->ni_challenge == NULL)
1489 				      ieee80211_node_authorize(ni);
1490 			} else {
1491 				vap->iv_stats.is_rx_acl++;
1492 				ieee80211_send_error(ni, ni->ni_macaddr,
1493 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
1494 				ieee80211_node_leave(ni);
1495 			}
1496 			ieee80211_free_node(ni);
1497 		} else
1498 			error = ENOENT;
1499 		break;
1500 	default:
1501 		error = EINVAL;
1502 		break;
1503 	}
1504 	return error;
1505 }
1506 
1507 struct scanlookup {
1508 	const uint8_t *mac;
1509 	int esslen;
1510 	const uint8_t *essid;
1511 	const struct ieee80211_scan_entry *se;
1512 };
1513 
1514 /*
1515  * Match mac address and any ssid.
1516  */
1517 static void
1518 mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
1519 {
1520 	struct scanlookup *look = arg;
1521 
1522 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
1523 		return;
1524 	if (look->esslen != 0) {
1525 		if (se->se_ssid[1] != look->esslen)
1526 			return;
1527 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
1528 			return;
1529 	}
1530 	look->se = se;
1531 }
1532 
1533 static int
1534 setmlme_assoc_sta(struct ieee80211vap *vap,
1535 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1536 	const uint8_t ssid[IEEE80211_NWID_LEN])
1537 {
1538 	struct scanlookup lookup;
1539 
1540 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1541 	    ("expected opmode STA not %s",
1542 	    ieee80211_opmode_name[vap->iv_opmode]));
1543 
1544 	/* NB: this is racey if roaming is !manual */
1545 	lookup.se = NULL;
1546 	lookup.mac = mac;
1547 	lookup.esslen = ssid_len;
1548 	lookup.essid = ssid;
1549 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
1550 	if (lookup.se == NULL)
1551 		return ENOENT;
1552 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
1553 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
1554 		return EIO;		/* XXX unique but could be better */
1555 	return 0;
1556 }
1557 
1558 static int
1559 setmlme_assoc_adhoc(struct ieee80211vap *vap,
1560 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
1561 	const uint8_t ssid[IEEE80211_NWID_LEN])
1562 {
1563 	struct ieee80211_scan_req *sr;
1564 	int error;
1565 
1566 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1567 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
1568 	    ("expected opmode IBSS or AHDEMO not %s",
1569 	    ieee80211_opmode_name[vap->iv_opmode]));
1570 
1571 	if (ssid_len == 0)
1572 		return EINVAL;
1573 
1574 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
1575 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1576 	if (sr == NULL)
1577 		return ENOMEM;
1578 
1579 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
1580 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
1581 	vap->iv_des_ssid[0].len = ssid_len;
1582 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
1583 	vap->iv_des_nssid = 1;
1584 
1585 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
1586 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1587 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
1588 	sr->sr_ssid[0].len = ssid_len;
1589 	sr->sr_nssid = 1;
1590 
1591 	error = ieee80211_scanreq(vap, sr);
1592 
1593 	IEEE80211_FREE(sr, M_TEMP);
1594 	return error;
1595 }
1596 
1597 static int
1598 ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
1599 {
1600 	struct ieee80211req_mlme mlme;
1601 	int error;
1602 
1603 	if (ireq->i_len != sizeof(mlme))
1604 		return EINVAL;
1605 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
1606 	if (error)
1607 		return error;
1608 	if  (vap->iv_opmode == IEEE80211_M_STA &&
1609 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1610 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
1611 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
1612 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
1613 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
1614 	    mlme.im_op == IEEE80211_MLME_ASSOC)
1615 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
1616 		    mlme.im_ssid_len, mlme.im_ssid);
1617 	else
1618 		return setmlme_common(vap, mlme.im_op,
1619 		    mlme.im_macaddr, mlme.im_reason);
1620 }
1621 
1622 static int
1623 ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
1624 {
1625 	uint8_t mac[IEEE80211_ADDR_LEN];
1626 	const struct ieee80211_aclator *acl = vap->iv_acl;
1627 	int error;
1628 
1629 	if (ireq->i_len != sizeof(mac))
1630 		return EINVAL;
1631 	error = copyin(ireq->i_data, mac, ireq->i_len);
1632 	if (error)
1633 		return error;
1634 	if (acl == NULL) {
1635 		acl = ieee80211_aclator_get("mac");
1636 		if (acl == NULL || !acl->iac_attach(vap))
1637 			return EINVAL;
1638 		vap->iv_acl = acl;
1639 	}
1640 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
1641 		acl->iac_add(vap, mac);
1642 	else
1643 		acl->iac_remove(vap, mac);
1644 	return 0;
1645 }
1646 
1647 static int
1648 ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
1649 {
1650 	const struct ieee80211_aclator *acl = vap->iv_acl;
1651 
1652 	switch (ireq->i_val) {
1653 	case IEEE80211_MACCMD_POLICY_OPEN:
1654 	case IEEE80211_MACCMD_POLICY_ALLOW:
1655 	case IEEE80211_MACCMD_POLICY_DENY:
1656 	case IEEE80211_MACCMD_POLICY_RADIUS:
1657 		if (acl == NULL) {
1658 			acl = ieee80211_aclator_get("mac");
1659 			if (acl == NULL || !acl->iac_attach(vap))
1660 				return EINVAL;
1661 			vap->iv_acl = acl;
1662 		}
1663 		acl->iac_setpolicy(vap, ireq->i_val);
1664 		break;
1665 	case IEEE80211_MACCMD_FLUSH:
1666 		if (acl != NULL)
1667 			acl->iac_flush(vap);
1668 		/* NB: silently ignore when not in use */
1669 		break;
1670 	case IEEE80211_MACCMD_DETACH:
1671 		if (acl != NULL) {
1672 			vap->iv_acl = NULL;
1673 			acl->iac_detach(vap);
1674 		}
1675 		break;
1676 	default:
1677 		if (acl == NULL)
1678 			return EINVAL;
1679 		else
1680 			return acl->iac_setioctl(vap, ireq);
1681 	}
1682 	return 0;
1683 }
1684 
1685 static int
1686 ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
1687 {
1688 	struct ieee80211com *ic = vap->iv_ic;
1689 	uint8_t *chanlist, *list;
1690 	int i, nchan, maxchan, error;
1691 
1692 	if (ireq->i_len > sizeof(ic->ic_chan_active))
1693 		ireq->i_len = sizeof(ic->ic_chan_active);
1694 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
1695 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1696 	if (list == NULL)
1697 		return ENOMEM;
1698 	error = copyin(ireq->i_data, list, ireq->i_len);
1699 	if (error) {
1700 		IEEE80211_FREE(list, M_TEMP);
1701 		return error;
1702 	}
1703 	nchan = 0;
1704 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
1705 	maxchan = ireq->i_len * NBBY;
1706 	for (i = 0; i < ic->ic_nchans; i++) {
1707 		const struct ieee80211_channel *c = &ic->ic_channels[i];
1708 		/*
1709 		 * Calculate the intersection of the user list and the
1710 		 * available channels so users can do things like specify
1711 		 * 1-255 to get all available channels.
1712 		 */
1713 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
1714 			setbit(chanlist, c->ic_ieee);
1715 			nchan++;
1716 		}
1717 	}
1718 	if (nchan == 0) {
1719 		IEEE80211_FREE(list, M_TEMP);
1720 		return EINVAL;
1721 	}
1722 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
1723 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
1724 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
1725 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
1726 	ieee80211_scan_flush(vap);
1727 	IEEE80211_FREE(list, M_TEMP);
1728 	return ENETRESET;
1729 }
1730 
1731 static int
1732 ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
1733 {
1734 	struct ieee80211_node *ni;
1735 	uint8_t macaddr[IEEE80211_ADDR_LEN];
1736 	int error;
1737 
1738 	/*
1739 	 * NB: we could copyin ieee80211req_sta_stats so apps
1740 	 *     could make selective changes but that's overkill;
1741 	 *     just clear all stats for now.
1742 	 */
1743 	if (ireq->i_len < IEEE80211_ADDR_LEN)
1744 		return EINVAL;
1745 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
1746 	if (error != 0)
1747 		return error;
1748 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
1749 	if (ni == NULL)
1750 		return ENOENT;
1751 	/* XXX require ni_vap == vap? */
1752 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
1753 	ieee80211_free_node(ni);
1754 	return 0;
1755 }
1756 
1757 static int
1758 ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
1759 {
1760 	struct ieee80211_node *ni;
1761 	struct ieee80211req_sta_txpow txpow;
1762 	int error;
1763 
1764 	if (ireq->i_len != sizeof(txpow))
1765 		return EINVAL;
1766 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
1767 	if (error != 0)
1768 		return error;
1769 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
1770 	if (ni == NULL)
1771 		return ENOENT;
1772 	ni->ni_txpower = txpow.it_txpow;
1773 	ieee80211_free_node(ni);
1774 	return error;
1775 }
1776 
1777 static int
1778 ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
1779 {
1780 	struct ieee80211com *ic = vap->iv_ic;
1781 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1782 	struct wmeParams *wmep, *chanp;
1783 	int isbss, ac, aggrmode;
1784 
1785 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
1786 		return EOPNOTSUPP;
1787 
1788 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
1789 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
1790 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
1791 	if (ac >= WME_NUM_AC)
1792 		ac = WME_AC_BE;
1793 	if (isbss) {
1794 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
1795 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
1796 	} else {
1797 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
1798 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
1799 	}
1800 	switch (ireq->i_type) {
1801 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
1802 		wmep->wmep_logcwmin = ireq->i_val;
1803 		if (!isbss || !aggrmode)
1804 			chanp->wmep_logcwmin = ireq->i_val;
1805 		break;
1806 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
1807 		wmep->wmep_logcwmax = ireq->i_val;
1808 		if (!isbss || !aggrmode)
1809 			chanp->wmep_logcwmax = ireq->i_val;
1810 		break;
1811 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
1812 		wmep->wmep_aifsn = ireq->i_val;
1813 		if (!isbss || !aggrmode)
1814 			chanp->wmep_aifsn = ireq->i_val;
1815 		break;
1816 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
1817 		wmep->wmep_txopLimit = ireq->i_val;
1818 		if (!isbss || !aggrmode)
1819 			chanp->wmep_txopLimit = ireq->i_val;
1820 		break;
1821 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
1822 		wmep->wmep_acm = ireq->i_val;
1823 		if (!aggrmode)
1824 			chanp->wmep_acm = ireq->i_val;
1825 		break;
1826 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
1827 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
1828 			(ireq->i_val) == 0;
1829 		break;
1830 	}
1831 	ieee80211_wme_updateparams(vap);
1832 	return 0;
1833 }
1834 
1835 static int
1836 find11gchannel(struct ieee80211com *ic, int start, int freq)
1837 {
1838 	const struct ieee80211_channel *c;
1839 	int i;
1840 
1841 	for (i = start+1; i < ic->ic_nchans; i++) {
1842 		c = &ic->ic_channels[i];
1843 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1844 			return 1;
1845 	}
1846 	/* NB: should not be needed but in case things are mis-sorted */
1847 	for (i = 0; i < start; i++) {
1848 		c = &ic->ic_channels[i];
1849 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
1850 			return 1;
1851 	}
1852 	return 0;
1853 }
1854 
1855 static struct ieee80211_channel *
1856 findchannel(struct ieee80211com *ic, int ieee, int mode)
1857 {
1858 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
1859 	    [IEEE80211_MODE_AUTO]	= 0,
1860 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
1861 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
1862 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
1863 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
1864 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
1865 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
1866 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
1867 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
1868 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
1869 	    /* NB: handled specially below */
1870 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
1871 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
1872 	};
1873 	u_int modeflags;
1874 	int i;
1875 
1876 	modeflags = chanflags[mode];
1877 	for (i = 0; i < ic->ic_nchans; i++) {
1878 		struct ieee80211_channel *c = &ic->ic_channels[i];
1879 
1880 		if (c->ic_ieee != ieee)
1881 			continue;
1882 		if (mode == IEEE80211_MODE_AUTO) {
1883 			/* ignore turbo channels for autoselect */
1884 			if (IEEE80211_IS_CHAN_TURBO(c))
1885 				continue;
1886 			/*
1887 			 * XXX special-case 11b/g channels so we
1888 			 *     always select the g channel if both
1889 			 *     are present.
1890 			 * XXX prefer HT to non-HT?
1891 			 */
1892 			if (!IEEE80211_IS_CHAN_B(c) ||
1893 			    !find11gchannel(ic, i, c->ic_freq))
1894 				return c;
1895 		} else {
1896 			/* must check HT specially */
1897 			if ((mode == IEEE80211_MODE_11NA ||
1898 			    mode == IEEE80211_MODE_11NG) &&
1899 			    !IEEE80211_IS_CHAN_HT(c))
1900 				continue;
1901 			if ((c->ic_flags & modeflags) == modeflags)
1902 				return c;
1903 		}
1904 	}
1905 	return NULL;
1906 }
1907 
1908 /*
1909  * Check the specified against any desired mode (aka netband).
1910  * This is only used (presently) when operating in hostap mode
1911  * to enforce consistency.
1912  */
1913 static int
1914 check_mode_consistency(const struct ieee80211_channel *c, int mode)
1915 {
1916 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
1917 
1918 	switch (mode) {
1919 	case IEEE80211_MODE_11B:
1920 		return (IEEE80211_IS_CHAN_B(c));
1921 	case IEEE80211_MODE_11G:
1922 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
1923 	case IEEE80211_MODE_11A:
1924 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
1925 	case IEEE80211_MODE_STURBO_A:
1926 		return (IEEE80211_IS_CHAN_STURBO(c));
1927 	case IEEE80211_MODE_11NA:
1928 		return (IEEE80211_IS_CHAN_HTA(c));
1929 	case IEEE80211_MODE_11NG:
1930 		return (IEEE80211_IS_CHAN_HTG(c));
1931 	}
1932 	return 1;
1933 
1934 }
1935 
1936 /*
1937  * Common code to set the current channel.  If the device
1938  * is up and running this may result in an immediate channel
1939  * change or a kick of the state machine.
1940  */
1941 static int
1942 setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
1943 {
1944 	struct ieee80211com *ic = vap->iv_ic;
1945 	int error;
1946 
1947 	if (c != IEEE80211_CHAN_ANYC) {
1948 		if (IEEE80211_IS_CHAN_RADAR(c))
1949 			return EBUSY;	/* XXX better code? */
1950 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1951 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
1952 				return EINVAL;
1953 			if (!check_mode_consistency(c, vap->iv_des_mode))
1954 				return EINVAL;
1955 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
1956 			if (IEEE80211_IS_CHAN_NOADHOC(c))
1957 				return EINVAL;
1958 		}
1959 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
1960 		    vap->iv_bss->ni_chan == c)
1961 			return 0;	/* NB: nothing to do */
1962 	}
1963 	vap->iv_des_chan = c;
1964 
1965 	error = 0;
1966 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
1967 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1968 		/*
1969 		 * Monitor mode can switch directly.
1970 		 */
1971 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
1972 			/* XXX need state machine for other vap's to follow */
1973 			ieee80211_setcurchan(ic, vap->iv_des_chan);
1974 			vap->iv_bss->ni_chan = ic->ic_curchan;
1975 		} else
1976 			ic->ic_curchan = vap->iv_des_chan;
1977 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1978 	} else {
1979 		/*
1980 		 * Need to go through the state machine in case we
1981 		 * need to reassociate or the like.  The state machine
1982 		 * will pickup the desired channel and avoid scanning.
1983 		 */
1984 		if (IS_UP_AUTO(vap))
1985 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1986 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
1987 			/*
1988 			 * When not up+running and a real channel has
1989 			 * been specified fix the current channel so
1990 			 * there is immediate feedback; e.g. via ifconfig.
1991 			 */
1992 			ic->ic_curchan = vap->iv_des_chan;
1993 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
1994 		}
1995 	}
1996 	return error;
1997 }
1998 
1999 /*
2000  * Old api for setting the current channel; this is
2001  * deprecated because channel numbers are ambiguous.
2002  */
2003 static int
2004 ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
2005 	const struct ieee80211req *ireq)
2006 {
2007 	struct ieee80211com *ic = vap->iv_ic;
2008 	struct ieee80211_channel *c;
2009 
2010 	/* XXX 0xffff overflows 16-bit signed */
2011 	if (ireq->i_val == 0 ||
2012 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
2013 		c = IEEE80211_CHAN_ANYC;
2014 	} else {
2015 		struct ieee80211_channel *c2;
2016 
2017 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
2018 		if (c == NULL) {
2019 			c = findchannel(ic, ireq->i_val,
2020 				IEEE80211_MODE_AUTO);
2021 			if (c == NULL)
2022 				return EINVAL;
2023 		}
2024 		/*
2025 		 * Fine tune channel selection based on desired mode:
2026 		 *   if 11b is requested, find the 11b version of any
2027 		 *      11g channel returned,
2028 		 *   if static turbo, find the turbo version of any
2029 		 *	11a channel return,
2030 		 *   if 11na is requested, find the ht version of any
2031 		 *      11a channel returned,
2032 		 *   if 11ng is requested, find the ht version of any
2033 		 *      11g channel returned,
2034 		 *   otherwise we should be ok with what we've got.
2035 		 */
2036 		switch (vap->iv_des_mode) {
2037 		case IEEE80211_MODE_11B:
2038 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2039 				c2 = findchannel(ic, ireq->i_val,
2040 					IEEE80211_MODE_11B);
2041 				/* NB: should not happen, =>'s 11g w/o 11b */
2042 				if (c2 != NULL)
2043 					c = c2;
2044 			}
2045 			break;
2046 		case IEEE80211_MODE_TURBO_A:
2047 			if (IEEE80211_IS_CHAN_A(c)) {
2048 				c2 = findchannel(ic, ireq->i_val,
2049 					IEEE80211_MODE_TURBO_A);
2050 				if (c2 != NULL)
2051 					c = c2;
2052 			}
2053 			break;
2054 		case IEEE80211_MODE_11NA:
2055 			if (IEEE80211_IS_CHAN_A(c)) {
2056 				c2 = findchannel(ic, ireq->i_val,
2057 					IEEE80211_MODE_11NA);
2058 				if (c2 != NULL)
2059 					c = c2;
2060 			}
2061 			break;
2062 		case IEEE80211_MODE_11NG:
2063 			if (IEEE80211_IS_CHAN_ANYG(c)) {
2064 				c2 = findchannel(ic, ireq->i_val,
2065 					IEEE80211_MODE_11NG);
2066 				if (c2 != NULL)
2067 					c = c2;
2068 			}
2069 			break;
2070 		default:		/* NB: no static turboG */
2071 			break;
2072 		}
2073 	}
2074 	return setcurchan(vap, c);
2075 }
2076 
2077 /*
2078  * New/current api for setting the current channel; a complete
2079  * channel description is provide so there is no ambiguity in
2080  * identifying the channel.
2081  */
2082 static int
2083 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
2084 	const struct ieee80211req *ireq)
2085 {
2086 	struct ieee80211com *ic = vap->iv_ic;
2087 	struct ieee80211_channel chan, *c;
2088 	int error;
2089 
2090 	if (ireq->i_len != sizeof(chan))
2091 		return EINVAL;
2092 	error = copyin(ireq->i_data, &chan, sizeof(chan));
2093 	if (error != 0)
2094 		return error;
2095 	/* XXX 0xffff overflows 16-bit signed */
2096 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
2097 		c = IEEE80211_CHAN_ANYC;
2098 	} else {
2099 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
2100 		if (c == NULL)
2101 			return EINVAL;
2102 	}
2103 	return setcurchan(vap, c);
2104 }
2105 
2106 static int
2107 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
2108 	const struct ieee80211req *ireq)
2109 {
2110 	struct ieee80211_regdomain_req *reg;
2111 	int nchans, error;
2112 
2113 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
2114 	    sizeof(struct ieee80211_channel));
2115 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
2116 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2117 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
2118 		    ireq->i_len, nchans);
2119 		return EINVAL;
2120 	}
2121 	reg = (struct ieee80211_regdomain_req *)
2122 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
2123 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2124 	if (reg == NULL) {
2125 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2126 		    "%s: no memory, nchans %d\n", __func__, nchans);
2127 		return ENOMEM;
2128 	}
2129 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
2130 	if (error == 0) {
2131 		/* NB: validate inline channel count against storage size */
2132 		if (reg->chaninfo.ic_nchans != nchans) {
2133 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
2134 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
2135 				reg->chaninfo.ic_nchans, nchans);
2136 			error = EINVAL;
2137 		} else
2138 			error = ieee80211_setregdomain(vap, reg);
2139 	}
2140 	IEEE80211_FREE(reg, M_TEMP);
2141 
2142 	return (error == 0 ? ENETRESET : error);
2143 }
2144 
2145 static int
2146 ieee80211_ioctl_setroam(struct ieee80211vap *vap,
2147 	const struct ieee80211req *ireq)
2148 {
2149 	if (ireq->i_len != sizeof(vap->iv_roamparms))
2150 		return EINVAL;
2151 	/* XXX validate params */
2152 	/* XXX? ENETRESET to push to device? */
2153 	return copyin(ireq->i_data, vap->iv_roamparms,
2154 	    sizeof(vap->iv_roamparms));
2155 }
2156 
2157 static int
2158 checkrate(const struct ieee80211_rateset *rs, int rate)
2159 {
2160 	int i;
2161 
2162 	if (rate == IEEE80211_FIXED_RATE_NONE)
2163 		return 1;
2164 	for (i = 0; i < rs->rs_nrates; i++)
2165 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
2166 			return 1;
2167 	return 0;
2168 }
2169 
2170 static int
2171 checkmcs(int mcs)
2172 {
2173 	if (mcs == IEEE80211_FIXED_RATE_NONE)
2174 		return 1;
2175 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
2176 		return 0;
2177 	return (mcs & 0x7f) <= 15;	/* XXX could search ht rate set */
2178 }
2179 
2180 static int
2181 ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
2182 	const struct ieee80211req *ireq)
2183 {
2184 	struct ieee80211com *ic = vap->iv_ic;
2185 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
2186 	struct ieee80211_txparam *src, *dst;
2187 	const struct ieee80211_rateset *rs;
2188 	int error, mode, changed, is11n, nmodes;
2189 
2190 	/* NB: accept short requests for backwards compat */
2191 	if (ireq->i_len > sizeof(parms))
2192 		return EINVAL;
2193 	error = copyin(ireq->i_data, &parms, ireq->i_len);
2194 	if (error != 0)
2195 		return error;
2196 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
2197 	changed = 0;
2198 	/* validate parameters and check if anything changed */
2199 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2200 		if (isclr(ic->ic_modecaps, mode))
2201 			continue;
2202 		src = &parms.params[mode];
2203 		dst = &vap->iv_txparms[mode];
2204 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
2205 		is11n = (mode == IEEE80211_MODE_11NA ||
2206 			 mode == IEEE80211_MODE_11NG);
2207 		if (src->ucastrate != dst->ucastrate) {
2208 			if (!checkrate(rs, src->ucastrate) &&
2209 			    (!is11n || !checkmcs(src->ucastrate)))
2210 				return EINVAL;
2211 			changed++;
2212 		}
2213 		if (src->mcastrate != dst->mcastrate) {
2214 			if (!checkrate(rs, src->mcastrate) &&
2215 			    (!is11n || !checkmcs(src->mcastrate)))
2216 				return EINVAL;
2217 			changed++;
2218 		}
2219 		if (src->mgmtrate != dst->mgmtrate) {
2220 			if (!checkrate(rs, src->mgmtrate) &&
2221 			    (!is11n || !checkmcs(src->mgmtrate)))
2222 				return EINVAL;
2223 			changed++;
2224 		}
2225 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
2226 			changed++;
2227 	}
2228 	if (changed) {
2229 		/*
2230 		 * Copy new parameters in place and notify the
2231 		 * driver so it can push state to the device.
2232 		 */
2233 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
2234 			if (isset(ic->ic_modecaps, mode))
2235 				vap->iv_txparms[mode] = parms.params[mode];
2236 		}
2237 		/* XXX could be more intelligent,
2238 		   e.g. don't reset if setting not being used */
2239 		return ENETRESET;
2240 	}
2241 	return 0;
2242 }
2243 
2244 /*
2245  * Application Information Element support.
2246  */
2247 static int
2248 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
2249 {
2250 	struct ieee80211_appie *app = *aie;
2251 	struct ieee80211_appie *napp;
2252 	int error;
2253 
2254 	if (ireq->i_len == 0) {		/* delete any existing ie */
2255 		if (app != NULL) {
2256 			*aie = NULL;	/* XXX racey */
2257 			IEEE80211_FREE(app, M_80211_NODE_IE);
2258 		}
2259 		return 0;
2260 	}
2261 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
2262 		return EINVAL;
2263 	/*
2264 	 * Allocate a new appie structure and copy in the user data.
2265 	 * When done swap in the new structure.  Note that we do not
2266 	 * guard against users holding a ref to the old structure;
2267 	 * this must be handled outside this code.
2268 	 *
2269 	 * XXX bad bad bad
2270 	 */
2271 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
2272 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
2273 	    IEEE80211_M_NOWAIT);
2274 	if (napp == NULL)
2275 		return ENOMEM;
2276 	/* XXX holding ic lock */
2277 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
2278 	if (error) {
2279 		IEEE80211_FREE(napp, M_80211_NODE_IE);
2280 		return error;
2281 	}
2282 	napp->ie_len = ireq->i_len;
2283 	*aie = napp;
2284 	if (app != NULL)
2285 		IEEE80211_FREE(app, M_80211_NODE_IE);
2286 	return 0;
2287 }
2288 
2289 static void
2290 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
2291 {
2292 	/* validate data is present as best we can */
2293 	if (space == 0 || 2+ie[1] > space)
2294 		return;
2295 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
2296 		vap->iv_wpa_ie = ie;
2297 	else if (ie[0] == IEEE80211_ELEMID_RSN)
2298 		vap->iv_rsn_ie = ie;
2299 }
2300 
2301 static int
2302 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
2303 	const struct ieee80211req *ireq, int fc0)
2304 {
2305 	int error;
2306 
2307 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2308 
2309 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
2310 	case IEEE80211_FC0_SUBTYPE_BEACON:
2311 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2312 		    vap->iv_opmode != IEEE80211_M_IBSS) {
2313 			error = EINVAL;
2314 			break;
2315 		}
2316 		error = setappie(&vap->iv_appie_beacon, ireq);
2317 		if (error == 0)
2318 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
2319 		break;
2320 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2321 		error = setappie(&vap->iv_appie_proberesp, ireq);
2322 		break;
2323 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2324 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2325 			error = setappie(&vap->iv_appie_assocresp, ireq);
2326 		else
2327 			error = EINVAL;
2328 		break;
2329 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2330 		error = setappie(&vap->iv_appie_probereq, ireq);
2331 		break;
2332 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2333 		if (vap->iv_opmode == IEEE80211_M_STA)
2334 			error = setappie(&vap->iv_appie_assocreq, ireq);
2335 		else
2336 			error = EINVAL;
2337 		break;
2338 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
2339 		error = setappie(&vap->iv_appie_wpa, ireq);
2340 		if (error == 0) {
2341 			/*
2342 			 * Must split single blob of data into separate
2343 			 * WPA and RSN ie's because they go in different
2344 			 * locations in the mgt frames.
2345 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
2346 			 */
2347 			vap->iv_wpa_ie = NULL;
2348 			vap->iv_rsn_ie = NULL;
2349 			if (vap->iv_appie_wpa != NULL) {
2350 				struct ieee80211_appie *appie =
2351 				    vap->iv_appie_wpa;
2352 				uint8_t *data = appie->ie_data;
2353 
2354 				/* XXX ie length validate is painful, cheat */
2355 				setwparsnie(vap, data, appie->ie_len);
2356 				setwparsnie(vap, data + 2 + data[1],
2357 				    appie->ie_len - (2 + data[1]));
2358 			}
2359 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2360 			    vap->iv_opmode == IEEE80211_M_IBSS) {
2361 				/*
2362 				 * Must rebuild beacon frame as the update
2363 				 * mechanism doesn't handle WPA/RSN ie's.
2364 				 * Could extend it but it doesn't normally
2365 				 * change; this is just to deal with hostapd
2366 				 * plumbing the ie after the interface is up.
2367 				 */
2368 				error = ENETRESET;
2369 			}
2370 		}
2371 		break;
2372 	default:
2373 		error = EINVAL;
2374 		break;
2375 	}
2376 	return error;
2377 }
2378 
2379 static int
2380 ieee80211_ioctl_setappie(struct ieee80211vap *vap,
2381 	const struct ieee80211req *ireq)
2382 {
2383 	struct ieee80211com *ic = vap->iv_ic;
2384 	int error;
2385 	uint8_t fc0;
2386 
2387 	fc0 = ireq->i_val & 0xff;
2388 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2389 		return EINVAL;
2390 	/* NB: could check iv_opmode and reject but hardly worth the effort */
2391 	IEEE80211_LOCK(ic);
2392 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
2393 	IEEE80211_UNLOCK(ic);
2394 	return error;
2395 }
2396 
2397 static int
2398 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
2399 {
2400 	struct ieee80211com *ic = vap->iv_ic;
2401 	struct ieee80211_chanswitch_req csr;
2402 	struct ieee80211_channel *c;
2403 	int error;
2404 
2405 	if (ireq->i_len != sizeof(csr))
2406 		return EINVAL;
2407 	error = copyin(ireq->i_data, &csr, sizeof(csr));
2408 	if (error != 0)
2409 		return error;
2410 	/* XXX adhoc mode not supported */
2411 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2412 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
2413 		return EOPNOTSUPP;
2414 	c = ieee80211_find_channel(ic,
2415 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
2416 	if (c == NULL)
2417 		return ENOENT;
2418 	IEEE80211_LOCK(ic);
2419 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
2420 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
2421 	else if (csr.csa_count == 0)
2422 		ieee80211_csa_cancelswitch(ic);
2423 	else
2424 		error = EBUSY;
2425 	IEEE80211_UNLOCK(ic);
2426 	return error;
2427 }
2428 
2429 static int
2430 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
2431 {
2432 #define	IEEE80211_IOC_SCAN_FLAGS \
2433 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
2434 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
2435 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
2436 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
2437 	 IEEE80211_IOC_SCAN_CHECK)
2438 	struct ieee80211com *ic = vap->iv_ic;
2439 	int error, i;
2440 
2441 	/* convert duration */
2442 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
2443 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
2444 	else {
2445 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
2446 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
2447 			return EINVAL;
2448 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
2449 		if (sr->sr_duration < 1)
2450 			sr->sr_duration = 1;
2451 	}
2452 	/* convert min/max channel dwell */
2453 	if (sr->sr_mindwell != 0) {
2454 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
2455 		if (sr->sr_mindwell < 1)
2456 			sr->sr_mindwell = 1;
2457 	}
2458 	if (sr->sr_maxdwell != 0) {
2459 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
2460 		if (sr->sr_maxdwell < 1)
2461 			sr->sr_maxdwell = 1;
2462 	}
2463 	/* NB: silently reduce ssid count to what is supported */
2464 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
2465 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
2466 	for (i = 0; i < sr->sr_nssid; i++)
2467 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
2468 			return EINVAL;
2469 	/* cleanse flags just in case, could reject if invalid flags */
2470 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
2471 	/*
2472 	 * Add an implicit NOPICK if the vap is not marked UP.  This
2473 	 * allows applications to scan without joining a bss (or picking
2474 	 * a channel and setting up a bss) and without forcing manual
2475 	 * roaming mode--you just need to mark the parent device UP.
2476 	 */
2477 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
2478 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
2479 
2480 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2481 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
2482 	    __func__, sr->sr_flags,
2483 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
2484 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
2485 	/*
2486 	 * If we are in INIT state then the driver has never had a chance
2487 	 * to setup hardware state to do a scan; we must use the state
2488 	 * machine to get us up to the SCAN state but once we reach SCAN
2489 	 * state we then want to use the supplied params.  Stash the
2490 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
2491 	 * state machines will recognize this and use the stashed params
2492 	 * to issue the scan request.
2493 	 *
2494 	 * Otherwise just invoke the scan machinery directly.
2495 	 */
2496 	IEEE80211_LOCK(ic);
2497 	if (ic->ic_nrunning == 0) {
2498 		IEEE80211_UNLOCK(ic);
2499 		return ENXIO;
2500 	}
2501 
2502 	if (vap->iv_state == IEEE80211_S_INIT) {
2503 		/* NB: clobbers previous settings */
2504 		vap->iv_scanreq_flags = sr->sr_flags;
2505 		vap->iv_scanreq_duration = sr->sr_duration;
2506 		vap->iv_scanreq_nssid = sr->sr_nssid;
2507 		for (i = 0; i < sr->sr_nssid; i++) {
2508 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
2509 			memcpy(vap->iv_scanreq_ssid[i].ssid,
2510 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
2511 		}
2512 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
2513 		IEEE80211_UNLOCK(ic);
2514 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2515 	} else {
2516 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
2517 		IEEE80211_UNLOCK(ic);
2518 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
2519 			error = ieee80211_check_scan(vap, sr->sr_flags,
2520 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2521 			    sr->sr_nssid,
2522 			    /* NB: cheat, we assume structures are compatible */
2523 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2524 		} else {
2525 			error = ieee80211_start_scan(vap, sr->sr_flags,
2526 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
2527 			    sr->sr_nssid,
2528 			    /* NB: cheat, we assume structures are compatible */
2529 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
2530 		}
2531 		if (error == 0)
2532 			return EINPROGRESS;
2533 	}
2534 	return 0;
2535 #undef IEEE80211_IOC_SCAN_FLAGS
2536 }
2537 
2538 static int
2539 ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
2540 {
2541 	struct ieee80211_scan_req *sr;
2542 	int error;
2543 
2544 	if (ireq->i_len != sizeof(*sr))
2545 		return EINVAL;
2546 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
2547 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2548 	if (sr == NULL)
2549 		return ENOMEM;
2550 	error = copyin(ireq->i_data, sr, sizeof(*sr));
2551 	if (error != 0)
2552 		goto bad;
2553 	error = ieee80211_scanreq(vap, sr);
2554 bad:
2555 	IEEE80211_FREE(sr, M_TEMP);
2556 	return error;
2557 }
2558 
2559 static int
2560 ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
2561 {
2562 	struct ieee80211_node *ni;
2563 	struct ieee80211req_sta_vlan vlan;
2564 	int error;
2565 
2566 	if (ireq->i_len != sizeof(vlan))
2567 		return EINVAL;
2568 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
2569 	if (error != 0)
2570 		return error;
2571 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
2572 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
2573 		    vlan.sv_macaddr);
2574 		if (ni == NULL)
2575 			return ENOENT;
2576 	} else
2577 		ni = ieee80211_ref_node(vap->iv_bss);
2578 	ni->ni_vlan = vlan.sv_vlan;
2579 	ieee80211_free_node(ni);
2580 	return error;
2581 }
2582 
2583 static int
2584 isvap11g(const struct ieee80211vap *vap)
2585 {
2586 	const struct ieee80211_node *bss = vap->iv_bss;
2587 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2588 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
2589 }
2590 
2591 static int
2592 isvapht(const struct ieee80211vap *vap)
2593 {
2594 	const struct ieee80211_node *bss = vap->iv_bss;
2595 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
2596 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
2597 }
2598 
2599 /*
2600  * Dummy ioctl set handler so the linker set is defined.
2601  */
2602 static int
2603 dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
2604 {
2605 	return ENOSYS;
2606 }
2607 IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
2608 
2609 static int
2610 ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
2611 {
2612 	ieee80211_ioctl_setfunc * const *set;
2613 	int error;
2614 
2615 	SET_FOREACH(set, ieee80211_ioctl_setset) {
2616 		error = (*set)(vap, ireq);
2617 		if (error != ENOSYS)
2618 			return error;
2619 	}
2620 	return EINVAL;
2621 }
2622 
2623 static int
2624 ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
2625 {
2626 	struct ieee80211com *ic = vap->iv_ic;
2627 	int error;
2628 	const struct ieee80211_authenticator *auth;
2629 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
2630 	char tmpssid[IEEE80211_NWID_LEN];
2631 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
2632 	struct ieee80211_key *k;
2633 	u_int kid;
2634 	uint32_t flags;
2635 
2636 	error = 0;
2637 	switch (ireq->i_type) {
2638 	case IEEE80211_IOC_SSID:
2639 		if (ireq->i_val != 0 ||
2640 		    ireq->i_len > IEEE80211_NWID_LEN)
2641 			return EINVAL;
2642 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
2643 		if (error)
2644 			break;
2645 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
2646 		vap->iv_des_ssid[0].len = ireq->i_len;
2647 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
2648 		vap->iv_des_nssid = (ireq->i_len > 0);
2649 		error = ENETRESET;
2650 		break;
2651 	case IEEE80211_IOC_WEP:
2652 		switch (ireq->i_val) {
2653 		case IEEE80211_WEP_OFF:
2654 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2655 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2656 			break;
2657 		case IEEE80211_WEP_ON:
2658 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2659 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2660 			break;
2661 		case IEEE80211_WEP_MIXED:
2662 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2663 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2664 			break;
2665 		}
2666 		error = ENETRESET;
2667 		break;
2668 	case IEEE80211_IOC_WEPKEY:
2669 		kid = (u_int) ireq->i_val;
2670 		if (kid >= IEEE80211_WEP_NKID)
2671 			return EINVAL;
2672 		k = &vap->iv_nw_keys[kid];
2673 		if (ireq->i_len == 0) {
2674 			/* zero-len =>'s delete any existing key */
2675 			(void) ieee80211_crypto_delkey(vap, k);
2676 			break;
2677 		}
2678 		if (ireq->i_len > sizeof(tmpkey))
2679 			return EINVAL;
2680 		memset(tmpkey, 0, sizeof(tmpkey));
2681 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
2682 		if (error)
2683 			break;
2684 		ieee80211_key_update_begin(vap);
2685 		k->wk_keyix = kid;	/* NB: force fixed key id */
2686 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
2687 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
2688 			k->wk_keylen = ireq->i_len;
2689 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
2690 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
2691 			if  (!ieee80211_crypto_setkey(vap, k))
2692 				error = EINVAL;
2693 		} else
2694 			error = EINVAL;
2695 		ieee80211_key_update_end(vap);
2696 		break;
2697 	case IEEE80211_IOC_WEPTXKEY:
2698 		kid = (u_int) ireq->i_val;
2699 		if (kid >= IEEE80211_WEP_NKID &&
2700 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
2701 			return EINVAL;
2702 		/*
2703 		 * Firmware devices may need to be told about an explicit
2704 		 * key index here, versus just inferring it from the
2705 		 * key set / change.  Since we may also need to pause
2706 		 * things like transmit before the key is updated,
2707 		 * give the driver a chance to flush things by tying
2708 		 * into key update begin/end.
2709 		 */
2710 		ieee80211_key_update_begin(vap);
2711 		ieee80211_crypto_set_deftxkey(vap, kid);
2712 		ieee80211_key_update_end(vap);
2713 		break;
2714 	case IEEE80211_IOC_AUTHMODE:
2715 		switch (ireq->i_val) {
2716 		case IEEE80211_AUTH_WPA:
2717 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2718 		case IEEE80211_AUTH_OPEN:	/* open */
2719 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2720 		case IEEE80211_AUTH_AUTO:	/* auto */
2721 			auth = ieee80211_authenticator_get(ireq->i_val);
2722 			if (auth == NULL)
2723 				return EINVAL;
2724 			break;
2725 		default:
2726 			return EINVAL;
2727 		}
2728 		switch (ireq->i_val) {
2729 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
2730 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2731 			ireq->i_val = IEEE80211_AUTH_8021X;
2732 			break;
2733 		case IEEE80211_AUTH_OPEN:	/* open */
2734 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
2735 			break;
2736 		case IEEE80211_AUTH_SHARED:	/* shared-key */
2737 		case IEEE80211_AUTH_8021X:	/* 802.1x */
2738 			vap->iv_flags &= ~IEEE80211_F_WPA;
2739 			/* both require a key so mark the PRIVACY capability */
2740 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2741 			break;
2742 		case IEEE80211_AUTH_AUTO:	/* auto */
2743 			vap->iv_flags &= ~IEEE80211_F_WPA;
2744 			/* XXX PRIVACY handling? */
2745 			/* XXX what's the right way to do this? */
2746 			break;
2747 		}
2748 		/* NB: authenticator attach/detach happens on state change */
2749 		vap->iv_bss->ni_authmode = ireq->i_val;
2750 		/* XXX mixed/mode/usage? */
2751 		vap->iv_auth = auth;
2752 		error = ENETRESET;
2753 		break;
2754 	case IEEE80211_IOC_CHANNEL:
2755 		error = ieee80211_ioctl_setchannel(vap, ireq);
2756 		break;
2757 	case IEEE80211_IOC_POWERSAVE:
2758 		switch (ireq->i_val) {
2759 		case IEEE80211_POWERSAVE_OFF:
2760 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
2761 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
2762 				error = ERESTART;
2763 			}
2764 			break;
2765 		case IEEE80211_POWERSAVE_ON:
2766 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
2767 				error = EOPNOTSUPP;
2768 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
2769 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
2770 				error = ERESTART;
2771 			}
2772 			break;
2773 		default:
2774 			error = EINVAL;
2775 			break;
2776 		}
2777 		break;
2778 	case IEEE80211_IOC_POWERSAVESLEEP:
2779 		if (ireq->i_val < 0)
2780 			return EINVAL;
2781 		ic->ic_lintval = ireq->i_val;
2782 		error = ERESTART;
2783 		break;
2784 	case IEEE80211_IOC_RTSTHRESHOLD:
2785 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
2786 		      ireq->i_val <= IEEE80211_RTS_MAX))
2787 			return EINVAL;
2788 		vap->iv_rtsthreshold = ireq->i_val;
2789 		error = ERESTART;
2790 		break;
2791 	case IEEE80211_IOC_PROTMODE:
2792 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
2793 			return EINVAL;
2794 		ic->ic_protmode = (enum ieee80211_protmode)ireq->i_val;
2795 		/* NB: if not operating in 11g this can wait */
2796 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
2797 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
2798 			error = ERESTART;
2799 		break;
2800 	case IEEE80211_IOC_TXPOWER:
2801 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
2802 			return EOPNOTSUPP;
2803 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
2804 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
2805 			return EINVAL;
2806 		ic->ic_txpowlimit = ireq->i_val;
2807 		error = ERESTART;
2808 		break;
2809 	case IEEE80211_IOC_ROAMING:
2810 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
2811 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
2812 			return EINVAL;
2813 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
2814 		/* XXXX reset? */
2815 		break;
2816 	case IEEE80211_IOC_PRIVACY:
2817 		if (ireq->i_val) {
2818 			/* XXX check for key state? */
2819 			vap->iv_flags |= IEEE80211_F_PRIVACY;
2820 		} else
2821 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
2822 		/* XXX ERESTART? */
2823 		break;
2824 	case IEEE80211_IOC_DROPUNENCRYPTED:
2825 		if (ireq->i_val)
2826 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
2827 		else
2828 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
2829 		/* XXX ERESTART? */
2830 		break;
2831 	case IEEE80211_IOC_WPAKEY:
2832 		error = ieee80211_ioctl_setkey(vap, ireq);
2833 		break;
2834 	case IEEE80211_IOC_DELKEY:
2835 		error = ieee80211_ioctl_delkey(vap, ireq);
2836 		break;
2837 	case IEEE80211_IOC_MLME:
2838 		error = ieee80211_ioctl_setmlme(vap, ireq);
2839 		break;
2840 	case IEEE80211_IOC_COUNTERMEASURES:
2841 		if (ireq->i_val) {
2842 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
2843 				return EOPNOTSUPP;
2844 			vap->iv_flags |= IEEE80211_F_COUNTERM;
2845 		} else
2846 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
2847 		/* XXX ERESTART? */
2848 		break;
2849 	case IEEE80211_IOC_WPA:
2850 		if (ireq->i_val > 3)
2851 			return EINVAL;
2852 		/* XXX verify ciphers available */
2853 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
2854 		switch (ireq->i_val) {
2855 		case 0:
2856 			/* wpa_supplicant calls this to clear the WPA config */
2857 			break;
2858 		case 1:
2859 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
2860 				return EOPNOTSUPP;
2861 			flags |= IEEE80211_F_WPA1;
2862 			break;
2863 		case 2:
2864 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
2865 				return EOPNOTSUPP;
2866 			flags |= IEEE80211_F_WPA2;
2867 			break;
2868 		case 3:
2869 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
2870 				return EOPNOTSUPP;
2871 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
2872 			break;
2873 		default:	/*  Can't set any -> error */
2874 			return EOPNOTSUPP;
2875 		}
2876 		vap->iv_flags = flags;
2877 		error = ERESTART;	/* NB: can change beacon frame */
2878 		break;
2879 	case IEEE80211_IOC_WME:
2880 		if (ireq->i_val) {
2881 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
2882 				return EOPNOTSUPP;
2883 			ieee80211_syncflag(vap, IEEE80211_F_WME);
2884 		} else
2885 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
2886 		error = ERESTART;	/* NB: can change beacon frame */
2887 		break;
2888 	case IEEE80211_IOC_HIDESSID:
2889 		if (ireq->i_val)
2890 			vap->iv_flags |= IEEE80211_F_HIDESSID;
2891 		else
2892 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
2893 		error = ERESTART;		/* XXX ENETRESET? */
2894 		break;
2895 	case IEEE80211_IOC_APBRIDGE:
2896 		if (ireq->i_val == 0)
2897 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
2898 		else
2899 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
2900 		break;
2901 	case IEEE80211_IOC_BSSID:
2902 		if (ireq->i_len != sizeof(tmpbssid))
2903 			return EINVAL;
2904 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
2905 		if (error)
2906 			break;
2907 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
2908 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
2909 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
2910 		else
2911 			vap->iv_flags |= IEEE80211_F_DESBSSID;
2912 		error = ENETRESET;
2913 		break;
2914 	case IEEE80211_IOC_CHANLIST:
2915 		error = ieee80211_ioctl_setchanlist(vap, ireq);
2916 		break;
2917 #define	OLD_IEEE80211_IOC_SCAN_REQ	23
2918 #ifdef OLD_IEEE80211_IOC_SCAN_REQ
2919 	case OLD_IEEE80211_IOC_SCAN_REQ:
2920 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2921 			"%s: active scan request\n", __func__);
2922 		/*
2923 		 * If we are in INIT state then the driver has never
2924 		 * had a chance to setup hardware state to do a scan;
2925 		 * use the state machine to get us up the SCAN state.
2926 		 * Otherwise just invoke the scan machinery to start
2927 		 * a one-time scan.
2928 		 */
2929 		if (vap->iv_state == IEEE80211_S_INIT)
2930 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
2931 		else
2932 			(void) ieee80211_start_scan(vap,
2933 				IEEE80211_SCAN_ACTIVE |
2934 				IEEE80211_SCAN_NOPICK |
2935 				IEEE80211_SCAN_ONCE,
2936 				IEEE80211_SCAN_FOREVER, 0, 0,
2937 				/* XXX use ioctl params */
2938 				vap->iv_des_nssid, vap->iv_des_ssid);
2939 		break;
2940 #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
2941 	case IEEE80211_IOC_SCAN_REQ:
2942 		error = ieee80211_ioctl_scanreq(vap, ireq);
2943 		break;
2944 	case IEEE80211_IOC_SCAN_CANCEL:
2945 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
2946 		    "%s: cancel scan\n", __func__);
2947 		ieee80211_cancel_scan(vap);
2948 		break;
2949 	case IEEE80211_IOC_HTCONF:
2950 		if (ireq->i_val & 1)
2951 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
2952 		else
2953 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
2954 		if (ireq->i_val & 2)
2955 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
2956 		else
2957 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
2958 		error = ENETRESET;
2959 		break;
2960 	case IEEE80211_IOC_ADDMAC:
2961 	case IEEE80211_IOC_DELMAC:
2962 		error = ieee80211_ioctl_macmac(vap, ireq);
2963 		break;
2964 	case IEEE80211_IOC_MACCMD:
2965 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
2966 		break;
2967 	case IEEE80211_IOC_STA_STATS:
2968 		error = ieee80211_ioctl_setstastats(vap, ireq);
2969 		break;
2970 	case IEEE80211_IOC_STA_TXPOW:
2971 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
2972 		break;
2973 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
2974 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
2975 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
2976 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
2977 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
2978 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
2979 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
2980 		break;
2981 	case IEEE80211_IOC_DTIM_PERIOD:
2982 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2983 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2984 		    vap->iv_opmode != IEEE80211_M_IBSS)
2985 			return EINVAL;
2986 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
2987 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
2988 			vap->iv_dtim_period = ireq->i_val;
2989 			error = ENETRESET;		/* requires restart */
2990 		} else
2991 			error = EINVAL;
2992 		break;
2993 	case IEEE80211_IOC_BEACON_INTERVAL:
2994 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
2995 		    vap->iv_opmode != IEEE80211_M_MBSS &&
2996 		    vap->iv_opmode != IEEE80211_M_IBSS)
2997 			return EINVAL;
2998 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
2999 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
3000 			ic->ic_bintval = ireq->i_val;
3001 			error = ENETRESET;		/* requires restart */
3002 		} else
3003 			error = EINVAL;
3004 		break;
3005 	case IEEE80211_IOC_PUREG:
3006 		if (ireq->i_val)
3007 			vap->iv_flags |= IEEE80211_F_PUREG;
3008 		else
3009 			vap->iv_flags &= ~IEEE80211_F_PUREG;
3010 		/* NB: reset only if we're operating on an 11g channel */
3011 		if (isvap11g(vap))
3012 			error = ENETRESET;
3013 		break;
3014 	case IEEE80211_IOC_QUIET:
3015 		vap->iv_quiet= ireq->i_val;
3016 		break;
3017 	case IEEE80211_IOC_QUIET_COUNT:
3018 		vap->iv_quiet_count=ireq->i_val;
3019 		break;
3020 	case IEEE80211_IOC_QUIET_PERIOD:
3021 		vap->iv_quiet_period=ireq->i_val;
3022 		break;
3023 	case IEEE80211_IOC_QUIET_OFFSET:
3024 		vap->iv_quiet_offset=ireq->i_val;
3025 		break;
3026 	case IEEE80211_IOC_QUIET_DUR:
3027 		if(ireq->i_val < vap->iv_bss->ni_intval)
3028 			vap->iv_quiet_duration = ireq->i_val;
3029 		else
3030 			error = EINVAL;
3031 		break;
3032 	case IEEE80211_IOC_BGSCAN:
3033 		if (ireq->i_val) {
3034 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
3035 				return EOPNOTSUPP;
3036 			vap->iv_flags |= IEEE80211_F_BGSCAN;
3037 		} else
3038 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
3039 		break;
3040 	case IEEE80211_IOC_BGSCAN_IDLE:
3041 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
3042 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
3043 		else
3044 			error = EINVAL;
3045 		break;
3046 	case IEEE80211_IOC_BGSCAN_INTERVAL:
3047 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
3048 			vap->iv_bgscanintvl = ireq->i_val*hz;
3049 		else
3050 			error = EINVAL;
3051 		break;
3052 	case IEEE80211_IOC_SCANVALID:
3053 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
3054 			vap->iv_scanvalid = ireq->i_val*hz;
3055 		else
3056 			error = EINVAL;
3057 		break;
3058 	case IEEE80211_IOC_FRAGTHRESHOLD:
3059 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
3060 		    ireq->i_val != IEEE80211_FRAG_MAX)
3061 			return EOPNOTSUPP;
3062 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
3063 		      ireq->i_val <= IEEE80211_FRAG_MAX))
3064 			return EINVAL;
3065 		vap->iv_fragthreshold = ireq->i_val;
3066 		error = ERESTART;
3067 		break;
3068 	case IEEE80211_IOC_BURST:
3069 		if (ireq->i_val) {
3070 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
3071 				return EOPNOTSUPP;
3072 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
3073 		} else
3074 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
3075 		error = ERESTART;
3076 		break;
3077 	case IEEE80211_IOC_BMISSTHRESHOLD:
3078 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
3079 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
3080 			return EINVAL;
3081 		vap->iv_bmissthreshold = ireq->i_val;
3082 		error = ERESTART;
3083 		break;
3084 	case IEEE80211_IOC_CURCHAN:
3085 		error = ieee80211_ioctl_setcurchan(vap, ireq);
3086 		break;
3087 	case IEEE80211_IOC_SHORTGI:
3088 		if (ireq->i_val) {
3089 #define	IEEE80211_HTCAP_SHORTGI \
3090 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
3091 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
3092 				return EINVAL;
3093 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
3094 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
3095 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
3096 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
3097 #undef IEEE80211_HTCAP_SHORTGI
3098 		} else
3099 			vap->iv_flags_ht &=
3100 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
3101 		error = ERESTART;
3102 		break;
3103 	case IEEE80211_IOC_AMPDU:
3104 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
3105 			return EINVAL;
3106 		if (ireq->i_val & 1)
3107 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
3108 		else
3109 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3110 		if (ireq->i_val & 2)
3111 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
3112 		else
3113 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
3114 		/* NB: reset only if we're operating on an 11n channel */
3115 		if (isvapht(vap))
3116 			error = ERESTART;
3117 		break;
3118 	case IEEE80211_IOC_AMPDU_LIMIT:
3119 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
3120 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
3121 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
3122 			return EINVAL;
3123 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3124 			vap->iv_ampdu_rxmax = ireq->i_val;
3125 		else
3126 			vap->iv_ampdu_limit = ireq->i_val;
3127 		error = ERESTART;
3128 		break;
3129 	case IEEE80211_IOC_AMPDU_DENSITY:
3130 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
3131 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
3132 			return EINVAL;
3133 		vap->iv_ampdu_density = ireq->i_val;
3134 		error = ERESTART;
3135 		break;
3136 	case IEEE80211_IOC_AMSDU:
3137 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
3138 			return EINVAL;
3139 		if (ireq->i_val & 1)
3140 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
3141 		else
3142 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
3143 		if (ireq->i_val & 2)
3144 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
3145 		else
3146 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
3147 		/* NB: reset only if we're operating on an 11n channel */
3148 		if (isvapht(vap))
3149 			error = ERESTART;
3150 		break;
3151 	case IEEE80211_IOC_AMSDU_LIMIT:
3152 		/* XXX validate */
3153 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
3154 		break;
3155 	case IEEE80211_IOC_PUREN:
3156 		if (ireq->i_val) {
3157 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3158 				return EINVAL;
3159 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
3160 		} else
3161 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
3162 		/* NB: reset only if we're operating on an 11n channel */
3163 		if (isvapht(vap))
3164 			error = ERESTART;
3165 		break;
3166 	case IEEE80211_IOC_DOTH:
3167 		if (ireq->i_val) {
3168 #if 0
3169 			/* XXX no capability */
3170 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
3171 				return EOPNOTSUPP;
3172 #endif
3173 			vap->iv_flags |= IEEE80211_F_DOTH;
3174 		} else
3175 			vap->iv_flags &= ~IEEE80211_F_DOTH;
3176 		error = ENETRESET;
3177 		break;
3178 	case IEEE80211_IOC_REGDOMAIN:
3179 		error = ieee80211_ioctl_setregdomain(vap, ireq);
3180 		break;
3181 	case IEEE80211_IOC_ROAM:
3182 		error = ieee80211_ioctl_setroam(vap, ireq);
3183 		break;
3184 	case IEEE80211_IOC_TXPARAMS:
3185 		error = ieee80211_ioctl_settxparams(vap, ireq);
3186 		break;
3187 	case IEEE80211_IOC_HTCOMPAT:
3188 		if (ireq->i_val) {
3189 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
3190 				return EOPNOTSUPP;
3191 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
3192 		} else
3193 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
3194 		/* NB: reset only if we're operating on an 11n channel */
3195 		if (isvapht(vap))
3196 			error = ERESTART;
3197 		break;
3198 	case IEEE80211_IOC_DWDS:
3199 		if (ireq->i_val) {
3200 			/* NB: DWDS only makes sense for WDS-capable devices */
3201 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
3202 				return EOPNOTSUPP;
3203 			/* NB: DWDS is used only with ap+sta vaps */
3204 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
3205 			    vap->iv_opmode != IEEE80211_M_STA)
3206 				return EINVAL;
3207 			vap->iv_flags |= IEEE80211_F_DWDS;
3208 			if (vap->iv_opmode == IEEE80211_M_STA)
3209 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
3210 		} else {
3211 			vap->iv_flags &= ~IEEE80211_F_DWDS;
3212 			if (vap->iv_opmode == IEEE80211_M_STA)
3213 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
3214 		}
3215 		break;
3216 	case IEEE80211_IOC_INACTIVITY:
3217 		if (ireq->i_val)
3218 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
3219 		else
3220 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
3221 		break;
3222 	case IEEE80211_IOC_APPIE:
3223 		error = ieee80211_ioctl_setappie(vap, ireq);
3224 		break;
3225 	case IEEE80211_IOC_WPS:
3226 		if (ireq->i_val) {
3227 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3228 				return EOPNOTSUPP;
3229 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
3230 		} else
3231 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
3232 		break;
3233 	case IEEE80211_IOC_TSN:
3234 		if (ireq->i_val) {
3235 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
3236 				return EOPNOTSUPP;
3237 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
3238 		} else
3239 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
3240 		break;
3241 	case IEEE80211_IOC_CHANSWITCH:
3242 		error = ieee80211_ioctl_chanswitch(vap, ireq);
3243 		break;
3244 	case IEEE80211_IOC_DFS:
3245 		if (ireq->i_val) {
3246 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
3247 				return EOPNOTSUPP;
3248 			/* NB: DFS requires 11h support */
3249 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
3250 				return EINVAL;
3251 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
3252 		} else
3253 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
3254 		break;
3255 	case IEEE80211_IOC_DOTD:
3256 		if (ireq->i_val)
3257 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
3258 		else
3259 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
3260 		if (vap->iv_opmode == IEEE80211_M_STA)
3261 			error = ENETRESET;
3262 		break;
3263 	case IEEE80211_IOC_HTPROTMODE:
3264 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
3265 			return EINVAL;
3266 		ic->ic_htprotmode = ireq->i_val ?
3267 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
3268 		/* NB: if not operating in 11n this can wait */
3269 		if (isvapht(vap))
3270 			error = ERESTART;
3271 		break;
3272 	case IEEE80211_IOC_STA_VLAN:
3273 		error = ieee80211_ioctl_setstavlan(vap, ireq);
3274 		break;
3275 	case IEEE80211_IOC_SMPS:
3276 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
3277 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
3278 			return EINVAL;
3279 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
3280 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
3281 			return EOPNOTSUPP;
3282 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
3283 			ireq->i_val;
3284 		/* NB: if not operating in 11n this can wait */
3285 		if (isvapht(vap))
3286 			error = ERESTART;
3287 		break;
3288 	case IEEE80211_IOC_RIFS:
3289 		if (ireq->i_val != 0) {
3290 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
3291 				return EOPNOTSUPP;
3292 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
3293 		} else
3294 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
3295 		/* NB: if not operating in 11n this can wait */
3296 		if (isvapht(vap))
3297 			error = ERESTART;
3298 		break;
3299 	case IEEE80211_IOC_STBC:
3300 		/* Check if we can do STBC TX/RX before changing the setting */
3301 		if ((ireq->i_val & 1) &&
3302 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
3303 			return EOPNOTSUPP;
3304 		if ((ireq->i_val & 2) &&
3305 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
3306 			return EOPNOTSUPP;
3307 
3308 		/* TX */
3309 		if (ireq->i_val & 1)
3310 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
3311 		else
3312 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
3313 
3314 		/* RX */
3315 		if (ireq->i_val & 2)
3316 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
3317 		else
3318 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
3319 
3320 		/* NB: reset only if we're operating on an 11n channel */
3321 		if (isvapht(vap))
3322 			error = ERESTART;
3323 		break;
3324 	default:
3325 		error = ieee80211_ioctl_setdefault(vap, ireq);
3326 		break;
3327 	}
3328 	/*
3329 	 * The convention is that ENETRESET means an operation
3330 	 * requires a complete re-initialization of the device (e.g.
3331 	 * changing something that affects the association state).
3332 	 * ERESTART means the request may be handled with only a
3333 	 * reload of the hardware state.  We hand ERESTART requests
3334 	 * to the iv_reset callback so the driver can decide.  If
3335 	 * a device does not fillin iv_reset then it defaults to one
3336 	 * that returns ENETRESET.  Otherwise a driver may return
3337 	 * ENETRESET (in which case a full reset will be done) or
3338 	 * 0 to mean there's no need to do anything (e.g. when the
3339 	 * change has no effect on the driver/device).
3340 	 */
3341 	if (error == ERESTART)
3342 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
3343 		    vap->iv_reset(vap, ireq->i_type) : 0;
3344 	if (error == ENETRESET) {
3345 		/* XXX need to re-think AUTO handling */
3346 		if (IS_UP_AUTO(vap))
3347 			ieee80211_init(vap);
3348 		error = 0;
3349 	}
3350 	return error;
3351 }
3352 
3353 int
3354 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3355 {
3356 	struct ieee80211vap *vap = ifp->if_softc;
3357 	struct ieee80211com *ic = vap->iv_ic;
3358 	int error = 0, wait = 0;
3359 	struct ifreq *ifr;
3360 	struct ifaddr *ifa;			/* XXX */
3361 
3362 	switch (cmd) {
3363 	case SIOCSIFFLAGS:
3364 		IEEE80211_LOCK(ic);
3365 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
3366 			/*
3367 			 * Enable promiscuous mode when:
3368 			 * 1. Interface is not a member of bridge, or
3369 			 * 2. Requested by user, or
3370 			 * 3. In monitor (or adhoc-demo) mode.
3371 			 */
3372 			if (ifp->if_bridge == NULL ||
3373 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
3374 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
3375 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
3376 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
3377 				ieee80211_promisc(vap,
3378 				    ifp->if_flags & IFF_PROMISC);
3379 				vap->iv_ifflags ^= IFF_PROMISC;
3380 			}
3381 		}
3382 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
3383 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
3384 			vap->iv_ifflags ^= IFF_ALLMULTI;
3385 		}
3386 		if (ifp->if_flags & IFF_UP) {
3387 			/*
3388 			 * Bring ourself up unless we're already operational.
3389 			 * If we're the first vap and the parent is not up
3390 			 * then it will automatically be brought up as a
3391 			 * side-effect of bringing ourself up.
3392 			 */
3393 			if (vap->iv_state == IEEE80211_S_INIT) {
3394 				if (ic->ic_nrunning == 0)
3395 					wait = 1;
3396 				ieee80211_start_locked(vap);
3397 			}
3398 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3399 			/*
3400 			 * Stop ourself.  If we are the last vap to be
3401 			 * marked down the parent will also be taken down.
3402 			 */
3403 			if (ic->ic_nrunning == 1)
3404 				wait = 1;
3405 			ieee80211_stop_locked(vap);
3406 		}
3407 		IEEE80211_UNLOCK(ic);
3408 		/* Wait for parent ioctl handler if it was queued */
3409 		if (wait) {
3410 			ieee80211_waitfor_parent(ic);
3411 
3412 			/*
3413 			 * Check if the MAC address was changed
3414 			 * via SIOCSIFLLADDR ioctl.
3415 			 */
3416 			if_addr_rlock(ifp);
3417 			if ((ifp->if_flags & IFF_UP) == 0 &&
3418 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
3419 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
3420 				    IF_LLADDR(ifp));
3421 			if_addr_runlock(ifp);
3422 		}
3423 		break;
3424 	case SIOCADDMULTI:
3425 	case SIOCDELMULTI:
3426 		ieee80211_runtask(ic, &ic->ic_mcast_task);
3427 		break;
3428 	case SIOCSIFMEDIA:
3429 	case SIOCGIFMEDIA:
3430 		ifr = (struct ifreq *)data;
3431 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
3432 		break;
3433 	case SIOCG80211:
3434 		error = ieee80211_ioctl_get80211(vap, cmd,
3435 				(struct ieee80211req *) data);
3436 		break;
3437 	case SIOCS80211:
3438 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
3439 		if (error == 0)
3440 			error = ieee80211_ioctl_set80211(vap, cmd,
3441 					(struct ieee80211req *) data);
3442 		break;
3443 	case SIOCG80211STATS:
3444 		ifr = (struct ifreq *)data;
3445 		copyout(&vap->iv_stats, ifr->ifr_data, sizeof (vap->iv_stats));
3446 		break;
3447 	case SIOCSIFMTU:
3448 		ifr = (struct ifreq *)data;
3449 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
3450 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
3451 			error = EINVAL;
3452 		else
3453 			ifp->if_mtu = ifr->ifr_mtu;
3454 		break;
3455 	case SIOCSIFADDR:
3456 		/*
3457 		 * XXX Handle this directly so we can suppress if_init calls.
3458 		 * XXX This should be done in ether_ioctl but for the moment
3459 		 * XXX there are too many other parts of the system that
3460 		 * XXX set IFF_UP and so suppress if_init being called when
3461 		 * XXX it should be.
3462 		 */
3463 		ifa = (struct ifaddr *) data;
3464 		switch (ifa->ifa_addr->sa_family) {
3465 #ifdef INET
3466 		case AF_INET:
3467 			if ((ifp->if_flags & IFF_UP) == 0) {
3468 				ifp->if_flags |= IFF_UP;
3469 				ifp->if_init(ifp->if_softc);
3470 			}
3471 			arp_ifinit(ifp, ifa);
3472 			break;
3473 #endif
3474 		default:
3475 			if ((ifp->if_flags & IFF_UP) == 0) {
3476 				ifp->if_flags |= IFF_UP;
3477 				ifp->if_init(ifp->if_softc);
3478 			}
3479 			break;
3480 		}
3481 		break;
3482 	default:
3483 		/*
3484 		 * Pass unknown ioctls first to the driver, and if it
3485 		 * returns ENOTTY, then to the generic Ethernet handler.
3486 		 */
3487 		if (ic->ic_ioctl != NULL &&
3488 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
3489 			break;
3490 		error = ether_ioctl(ifp, cmd, data);
3491 		break;
3492 	}
3493 	return (error);
3494 }
3495