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