The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_ioctl.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 88aebd3abc40265f6d6d85fdac89172f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.