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