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