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