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