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