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