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