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_proto.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-2008 Sam Leffler, Errno Consulting
    4  * Copyright (c) 2012 IEEE
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/11.0/sys/net80211/ieee80211_proto.c 298995 2016-05-03 18:05:43Z pfg $");
   30 
   31 /*
   32  * IEEE 802.11 protocol support.
   33  */
   34 
   35 #include "opt_inet.h"
   36 #include "opt_wlan.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/kernel.h>
   41 #include <sys/malloc.h>
   42 
   43 #include <sys/socket.h>
   44 #include <sys/sockio.h>
   45 
   46 #include <net/if.h>
   47 #include <net/if_var.h>
   48 #include <net/if_media.h>
   49 #include <net/ethernet.h>               /* XXX for ether_sprintf */
   50 
   51 #include <net80211/ieee80211_var.h>
   52 #include <net80211/ieee80211_adhoc.h>
   53 #include <net80211/ieee80211_sta.h>
   54 #include <net80211/ieee80211_hostap.h>
   55 #include <net80211/ieee80211_wds.h>
   56 #ifdef IEEE80211_SUPPORT_MESH
   57 #include <net80211/ieee80211_mesh.h>
   58 #endif
   59 #include <net80211/ieee80211_monitor.h>
   60 #include <net80211/ieee80211_input.h>
   61 
   62 /* XXX tunables */
   63 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS       3       /* pkts / 100ms */
   64 #define HIGH_PRI_SWITCH_THRESH                  10      /* pkts / 100ms */
   65 
   66 const char *mgt_subtype_name[] = {
   67         "assoc_req",    "assoc_resp",   "reassoc_req",  "reassoc_resp",
   68         "probe_req",    "probe_resp",   "timing_adv",   "reserved#7",
   69         "beacon",       "atim",         "disassoc",     "auth",
   70         "deauth",       "action",       "action_noack", "reserved#15"
   71 };
   72 const char *ctl_subtype_name[] = {
   73         "reserved#0",   "reserved#1",   "reserved#2",   "reserved#3",
   74         "reserved#4",   "reserved#5",   "reserved#6",   "control_wrap",
   75         "bar",          "ba",           "ps_poll",      "rts",
   76         "cts",          "ack",          "cf_end",       "cf_end_ack"
   77 };
   78 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
   79         "IBSS",         /* IEEE80211_M_IBSS */
   80         "STA",          /* IEEE80211_M_STA */
   81         "WDS",          /* IEEE80211_M_WDS */
   82         "AHDEMO",       /* IEEE80211_M_AHDEMO */
   83         "HOSTAP",       /* IEEE80211_M_HOSTAP */
   84         "MONITOR",      /* IEEE80211_M_MONITOR */
   85         "MBSS"          /* IEEE80211_M_MBSS */
   86 };
   87 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
   88         "INIT",         /* IEEE80211_S_INIT */
   89         "SCAN",         /* IEEE80211_S_SCAN */
   90         "AUTH",         /* IEEE80211_S_AUTH */
   91         "ASSOC",        /* IEEE80211_S_ASSOC */
   92         "CAC",          /* IEEE80211_S_CAC */
   93         "RUN",          /* IEEE80211_S_RUN */
   94         "CSA",          /* IEEE80211_S_CSA */
   95         "SLEEP",        /* IEEE80211_S_SLEEP */
   96 };
   97 const char *ieee80211_wme_acnames[] = {
   98         "WME_AC_BE",
   99         "WME_AC_BK",
  100         "WME_AC_VI",
  101         "WME_AC_VO",
  102         "WME_UPSD",
  103 };
  104 
  105 
  106 /*
  107  * Reason code descriptions were (mostly) obtained from
  108  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
  109  */
  110 const char *
  111 ieee80211_reason_to_string(uint16_t reason)
  112 {
  113         switch (reason) {
  114         case IEEE80211_REASON_UNSPECIFIED:
  115                 return ("unspecified");
  116         case IEEE80211_REASON_AUTH_EXPIRE:
  117                 return ("previous authentication is expired");
  118         case IEEE80211_REASON_AUTH_LEAVE:
  119                 return ("sending STA is leaving/has left IBSS or ESS");
  120         case IEEE80211_REASON_ASSOC_EXPIRE:
  121                 return ("disassociated due to inactivity");
  122         case IEEE80211_REASON_ASSOC_TOOMANY:
  123                 return ("too many associated STAs");
  124         case IEEE80211_REASON_NOT_AUTHED:
  125                 return ("class 2 frame received from nonauthenticated STA");
  126         case IEEE80211_REASON_NOT_ASSOCED:
  127                 return ("class 3 frame received from nonassociated STA");
  128         case IEEE80211_REASON_ASSOC_LEAVE:
  129                 return ("sending STA is leaving/has left BSS");
  130         case IEEE80211_REASON_ASSOC_NOT_AUTHED:
  131                 return ("STA requesting (re)association is not authenticated");
  132         case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
  133                 return ("information in the Power Capability element is "
  134                         "unacceptable");
  135         case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
  136                 return ("information in the Supported Channels element is "
  137                         "unacceptable");
  138         case IEEE80211_REASON_IE_INVALID:
  139                 return ("invalid element");
  140         case IEEE80211_REASON_MIC_FAILURE:
  141                 return ("MIC failure");
  142         case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
  143                 return ("4-Way handshake timeout");
  144         case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
  145                 return ("group key update timeout");
  146         case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
  147                 return ("element in 4-Way handshake different from "
  148                         "(re)association request/probe response/beacon frame");
  149         case IEEE80211_REASON_GROUP_CIPHER_INVALID:
  150                 return ("invalid group cipher");
  151         case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
  152                 return ("invalid pairwise cipher");
  153         case IEEE80211_REASON_AKMP_INVALID:
  154                 return ("invalid AKMP");
  155         case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
  156                 return ("unsupported version in RSN IE");
  157         case IEEE80211_REASON_INVALID_RSN_IE_CAP:
  158                 return ("invalid capabilities in RSN IE");
  159         case IEEE80211_REASON_802_1X_AUTH_FAILED:
  160                 return ("IEEE 802.1X authentication failed");
  161         case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
  162                 return ("cipher suite rejected because of the security "
  163                         "policy");
  164         case IEEE80211_REASON_UNSPECIFIED_QOS:
  165                 return ("unspecified (QoS-related)");
  166         case IEEE80211_REASON_INSUFFICIENT_BW:
  167                 return ("QoS AP lacks sufficient bandwidth for this QoS STA");
  168         case IEEE80211_REASON_TOOMANY_FRAMES:
  169                 return ("too many frames need to be acknowledged");
  170         case IEEE80211_REASON_OUTSIDE_TXOP:
  171                 return ("STA is transmitting outside the limits of its TXOPs");
  172         case IEEE80211_REASON_LEAVING_QBSS:
  173                 return ("requested from peer STA (the STA is "
  174                         "resetting/leaving the BSS)");
  175         case IEEE80211_REASON_BAD_MECHANISM:
  176                 return ("requested from peer STA (it does not want to use "
  177                         "the mechanism)");
  178         case IEEE80211_REASON_SETUP_NEEDED:
  179                 return ("requested from peer STA (setup is required for the "
  180                         "used mechanism)");
  181         case IEEE80211_REASON_TIMEOUT:
  182                 return ("requested from peer STA (timeout)");
  183         case IEEE80211_REASON_PEER_LINK_CANCELED:
  184                 return ("SME cancels the mesh peering instance (not related "
  185                         "to the maximum number of peer mesh STAs)");
  186         case IEEE80211_REASON_MESH_MAX_PEERS:
  187                 return ("maximum number of peer mesh STAs was reached");
  188         case IEEE80211_REASON_MESH_CPVIOLATION:
  189                 return ("the received information violates the Mesh "
  190                         "Configuration policy configured in the mesh STA "
  191                         "profile");
  192         case IEEE80211_REASON_MESH_CLOSE_RCVD:
  193                 return ("the mesh STA has received a Mesh Peering Close "
  194                         "message requesting to close the mesh peering");
  195         case IEEE80211_REASON_MESH_MAX_RETRIES:
  196                 return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
  197                         "Peering Open messages, without receiving a Mesh "
  198                         "Peering Confirm message");
  199         case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
  200                 return ("the confirmTimer for the mesh peering instance times "
  201                         "out");
  202         case IEEE80211_REASON_MESH_INVALID_GTK:
  203                 return ("the mesh STA fails to unwrap the GTK or the values "
  204                         "in the wrapped contents do not match");
  205         case IEEE80211_REASON_MESH_INCONS_PARAMS:
  206                 return ("the mesh STA receives inconsistent information about "
  207                         "the mesh parameters between Mesh Peering Management "
  208                         "frames");
  209         case IEEE80211_REASON_MESH_INVALID_SECURITY:
  210                 return ("the mesh STA fails the authenticated mesh peering "
  211                         "exchange because due to failure in selecting "
  212                         "pairwise/group ciphersuite");
  213         case IEEE80211_REASON_MESH_PERR_NO_PROXY:
  214                 return ("the mesh STA does not have proxy information for "
  215                         "this external destination");
  216         case IEEE80211_REASON_MESH_PERR_NO_FI:
  217                 return ("the mesh STA does not have forwarding information "
  218                         "for this destination");
  219         case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
  220                 return ("the mesh STA determines that the link to the next "
  221                         "hop of an active path in its forwarding information "
  222                         "is no longer usable");
  223         case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
  224                 return ("the MAC address of the STA already exists in the "
  225                         "mesh BSS");
  226         case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
  227                 return ("the mesh STA performs channel switch to meet "
  228                         "regulatory requirements");
  229         case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
  230                 return ("the mesh STA performs channel switch with "
  231                         "unspecified reason");
  232         default:
  233                 return ("reserved/unknown");
  234         }
  235 }
  236 
  237 static void beacon_miss(void *, int);
  238 static void beacon_swmiss(void *, int);
  239 static void parent_updown(void *, int);
  240 static void update_mcast(void *, int);
  241 static void update_promisc(void *, int);
  242 static void update_channel(void *, int);
  243 static void update_chw(void *, int);
  244 static void update_wme(void *, int);
  245 static void restart_vaps(void *, int);
  246 static void ieee80211_newstate_cb(void *, int);
  247 
  248 static int
  249 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
  250         const struct ieee80211_bpf_params *params)
  251 {
  252 
  253         ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
  254         m_freem(m);
  255         return ENETDOWN;
  256 }
  257 
  258 void
  259 ieee80211_proto_attach(struct ieee80211com *ic)
  260 {
  261         uint8_t hdrlen;
  262 
  263         /* override the 802.3 setting */
  264         hdrlen = ic->ic_headroom
  265                 + sizeof(struct ieee80211_qosframe_addr4)
  266                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
  267                 + IEEE80211_WEP_EXTIVLEN;
  268         /* XXX no way to recalculate on ifdetach */
  269         if (ALIGN(hdrlen) > max_linkhdr) {
  270                 /* XXX sanity check... */
  271                 max_linkhdr = ALIGN(hdrlen);
  272                 max_hdr = max_linkhdr + max_protohdr;
  273                 max_datalen = MHLEN - max_hdr;
  274         }
  275         ic->ic_protmode = IEEE80211_PROT_CTSONLY;
  276 
  277         TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
  278         TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
  279         TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
  280         TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
  281         TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
  282         TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
  283         TASK_INIT(&ic->ic_wme_task, 0, update_wme, ic);
  284         TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
  285 
  286         ic->ic_wme.wme_hipri_switch_hysteresis =
  287                 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
  288 
  289         /* initialize management frame handlers */
  290         ic->ic_send_mgmt = ieee80211_send_mgmt;
  291         ic->ic_raw_xmit = null_raw_xmit;
  292 
  293         ieee80211_adhoc_attach(ic);
  294         ieee80211_sta_attach(ic);
  295         ieee80211_wds_attach(ic);
  296         ieee80211_hostap_attach(ic);
  297 #ifdef IEEE80211_SUPPORT_MESH
  298         ieee80211_mesh_attach(ic);
  299 #endif
  300         ieee80211_monitor_attach(ic);
  301 }
  302 
  303 void
  304 ieee80211_proto_detach(struct ieee80211com *ic)
  305 {
  306         ieee80211_monitor_detach(ic);
  307 #ifdef IEEE80211_SUPPORT_MESH
  308         ieee80211_mesh_detach(ic);
  309 #endif
  310         ieee80211_hostap_detach(ic);
  311         ieee80211_wds_detach(ic);
  312         ieee80211_adhoc_detach(ic);
  313         ieee80211_sta_detach(ic);
  314 }
  315 
  316 static void
  317 null_update_beacon(struct ieee80211vap *vap, int item)
  318 {
  319 }
  320 
  321 void
  322 ieee80211_proto_vattach(struct ieee80211vap *vap)
  323 {
  324         struct ieee80211com *ic = vap->iv_ic;
  325         struct ifnet *ifp = vap->iv_ifp;
  326         int i;
  327 
  328         /* override the 802.3 setting */
  329         ifp->if_hdrlen = ic->ic_headroom
  330                 + sizeof(struct ieee80211_qosframe_addr4)
  331                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
  332                 + IEEE80211_WEP_EXTIVLEN;
  333 
  334         vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
  335         vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
  336         vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
  337         callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
  338         callout_init(&vap->iv_mgtsend, 1);
  339         TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
  340         TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
  341         /*
  342          * Install default tx rate handling: no fixed rate, lowest
  343          * supported rate for mgmt and multicast frames.  Default
  344          * max retry count.  These settings can be changed by the
  345          * driver and/or user applications.
  346          */
  347         for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
  348                 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
  349 
  350                 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
  351 
  352                 /*
  353                  * Setting the management rate to MCS 0 assumes that the
  354                  * BSS Basic rate set is empty and the BSS Basic MCS set
  355                  * is not.
  356                  *
  357                  * Since we're not checking this, default to the lowest
  358                  * defined rate for this mode.
  359                  *
  360                  * At least one 11n AP (DLINK DIR-825) is reported to drop
  361                  * some MCS management traffic (eg BA response frames.)
  362                  *
  363                  * See also: 9.6.0 of the 802.11n-2009 specification.
  364                  */
  365 #ifdef  NOTYET
  366                 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
  367                         vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
  368                         vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
  369                 } else {
  370                         vap->iv_txparms[i].mgmtrate =
  371                             rs->rs_rates[0] & IEEE80211_RATE_VAL;
  372                         vap->iv_txparms[i].mcastrate = 
  373                             rs->rs_rates[0] & IEEE80211_RATE_VAL;
  374                 }
  375 #endif
  376                 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
  377                 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
  378                 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
  379         }
  380         vap->iv_roaming = IEEE80211_ROAMING_AUTO;
  381 
  382         vap->iv_update_beacon = null_update_beacon;
  383         vap->iv_deliver_data = ieee80211_deliver_data;
  384 
  385         /* attach support for operating mode */
  386         ic->ic_vattach[vap->iv_opmode](vap);
  387 }
  388 
  389 void
  390 ieee80211_proto_vdetach(struct ieee80211vap *vap)
  391 {
  392 #define FREEAPPIE(ie) do { \
  393         if (ie != NULL) \
  394                 IEEE80211_FREE(ie, M_80211_NODE_IE); \
  395 } while (0)
  396         /*
  397          * Detach operating mode module.
  398          */
  399         if (vap->iv_opdetach != NULL)
  400                 vap->iv_opdetach(vap);
  401         /*
  402          * This should not be needed as we detach when reseting
  403          * the state but be conservative here since the
  404          * authenticator may do things like spawn kernel threads.
  405          */
  406         if (vap->iv_auth->ia_detach != NULL)
  407                 vap->iv_auth->ia_detach(vap);
  408         /*
  409          * Detach any ACL'ator.
  410          */
  411         if (vap->iv_acl != NULL)
  412                 vap->iv_acl->iac_detach(vap);
  413 
  414         FREEAPPIE(vap->iv_appie_beacon);
  415         FREEAPPIE(vap->iv_appie_probereq);
  416         FREEAPPIE(vap->iv_appie_proberesp);
  417         FREEAPPIE(vap->iv_appie_assocreq);
  418         FREEAPPIE(vap->iv_appie_assocresp);
  419         FREEAPPIE(vap->iv_appie_wpa);
  420 #undef FREEAPPIE
  421 }
  422 
  423 /*
  424  * Simple-minded authenticator module support.
  425  */
  426 
  427 #define IEEE80211_AUTH_MAX      (IEEE80211_AUTH_WPA+1)
  428 /* XXX well-known names */
  429 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
  430         "wlan_internal",        /* IEEE80211_AUTH_NONE */
  431         "wlan_internal",        /* IEEE80211_AUTH_OPEN */
  432         "wlan_internal",        /* IEEE80211_AUTH_SHARED */
  433         "wlan_xauth",           /* IEEE80211_AUTH_8021X  */
  434         "wlan_internal",        /* IEEE80211_AUTH_AUTO */
  435         "wlan_xauth",           /* IEEE80211_AUTH_WPA */
  436 };
  437 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
  438 
  439 static const struct ieee80211_authenticator auth_internal = {
  440         .ia_name                = "wlan_internal",
  441         .ia_attach              = NULL,
  442         .ia_detach              = NULL,
  443         .ia_node_join           = NULL,
  444         .ia_node_leave          = NULL,
  445 };
  446 
  447 /*
  448  * Setup internal authenticators once; they are never unregistered.
  449  */
  450 static void
  451 ieee80211_auth_setup(void)
  452 {
  453         ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
  454         ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
  455         ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
  456 }
  457 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
  458 
  459 const struct ieee80211_authenticator *
  460 ieee80211_authenticator_get(int auth)
  461 {
  462         if (auth >= IEEE80211_AUTH_MAX)
  463                 return NULL;
  464         if (authenticators[auth] == NULL)
  465                 ieee80211_load_module(auth_modnames[auth]);
  466         return authenticators[auth];
  467 }
  468 
  469 void
  470 ieee80211_authenticator_register(int type,
  471         const struct ieee80211_authenticator *auth)
  472 {
  473         if (type >= IEEE80211_AUTH_MAX)
  474                 return;
  475         authenticators[type] = auth;
  476 }
  477 
  478 void
  479 ieee80211_authenticator_unregister(int type)
  480 {
  481 
  482         if (type >= IEEE80211_AUTH_MAX)
  483                 return;
  484         authenticators[type] = NULL;
  485 }
  486 
  487 /*
  488  * Very simple-minded ACL module support.
  489  */
  490 /* XXX just one for now */
  491 static  const struct ieee80211_aclator *acl = NULL;
  492 
  493 void
  494 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
  495 {
  496         printf("wlan: %s acl policy registered\n", iac->iac_name);
  497         acl = iac;
  498 }
  499 
  500 void
  501 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
  502 {
  503         if (acl == iac)
  504                 acl = NULL;
  505         printf("wlan: %s acl policy unregistered\n", iac->iac_name);
  506 }
  507 
  508 const struct ieee80211_aclator *
  509 ieee80211_aclator_get(const char *name)
  510 {
  511         if (acl == NULL)
  512                 ieee80211_load_module("wlan_acl");
  513         return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
  514 }
  515 
  516 void
  517 ieee80211_print_essid(const uint8_t *essid, int len)
  518 {
  519         const uint8_t *p;
  520         int i;
  521 
  522         if (len > IEEE80211_NWID_LEN)
  523                 len = IEEE80211_NWID_LEN;
  524         /* determine printable or not */
  525         for (i = 0, p = essid; i < len; i++, p++) {
  526                 if (*p < ' ' || *p > 0x7e)
  527                         break;
  528         }
  529         if (i == len) {
  530                 printf("\"");
  531                 for (i = 0, p = essid; i < len; i++, p++)
  532                         printf("%c", *p);
  533                 printf("\"");
  534         } else {
  535                 printf("0x");
  536                 for (i = 0, p = essid; i < len; i++, p++)
  537                         printf("%02x", *p);
  538         }
  539 }
  540 
  541 void
  542 ieee80211_dump_pkt(struct ieee80211com *ic,
  543         const uint8_t *buf, int len, int rate, int rssi)
  544 {
  545         const struct ieee80211_frame *wh;
  546         int i;
  547 
  548         wh = (const struct ieee80211_frame *)buf;
  549         switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  550         case IEEE80211_FC1_DIR_NODS:
  551                 printf("NODS %s", ether_sprintf(wh->i_addr2));
  552                 printf("->%s", ether_sprintf(wh->i_addr1));
  553                 printf("(%s)", ether_sprintf(wh->i_addr3));
  554                 break;
  555         case IEEE80211_FC1_DIR_TODS:
  556                 printf("TODS %s", ether_sprintf(wh->i_addr2));
  557                 printf("->%s", ether_sprintf(wh->i_addr3));
  558                 printf("(%s)", ether_sprintf(wh->i_addr1));
  559                 break;
  560         case IEEE80211_FC1_DIR_FROMDS:
  561                 printf("FRDS %s", ether_sprintf(wh->i_addr3));
  562                 printf("->%s", ether_sprintf(wh->i_addr1));
  563                 printf("(%s)", ether_sprintf(wh->i_addr2));
  564                 break;
  565         case IEEE80211_FC1_DIR_DSTODS:
  566                 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
  567                 printf("->%s", ether_sprintf(wh->i_addr3));
  568                 printf("(%s", ether_sprintf(wh->i_addr2));
  569                 printf("->%s)", ether_sprintf(wh->i_addr1));
  570                 break;
  571         }
  572         switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
  573         case IEEE80211_FC0_TYPE_DATA:
  574                 printf(" data");
  575                 break;
  576         case IEEE80211_FC0_TYPE_MGT:
  577                 printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
  578                 break;
  579         default:
  580                 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
  581                 break;
  582         }
  583         if (IEEE80211_QOS_HAS_SEQ(wh)) {
  584                 const struct ieee80211_qosframe *qwh = 
  585                         (const struct ieee80211_qosframe *)buf;
  586                 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
  587                         qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
  588         }
  589         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
  590                 int off;
  591 
  592                 off = ieee80211_anyhdrspace(ic, wh);
  593                 printf(" WEP [IV %.02x %.02x %.02x",
  594                         buf[off+0], buf[off+1], buf[off+2]);
  595                 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
  596                         printf(" %.02x %.02x %.02x",
  597                                 buf[off+4], buf[off+5], buf[off+6]);
  598                 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
  599         }
  600         if (rate >= 0)
  601                 printf(" %dM", rate / 2);
  602         if (rssi >= 0)
  603                 printf(" +%d", rssi);
  604         printf("\n");
  605         if (len > 0) {
  606                 for (i = 0; i < len; i++) {
  607                         if ((i & 1) == 0)
  608                                 printf(" ");
  609                         printf("%02x", buf[i]);
  610                 }
  611                 printf("\n");
  612         }
  613 }
  614 
  615 static __inline int
  616 findrix(const struct ieee80211_rateset *rs, int r)
  617 {
  618         int i;
  619 
  620         for (i = 0; i < rs->rs_nrates; i++)
  621                 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
  622                         return i;
  623         return -1;
  624 }
  625 
  626 int
  627 ieee80211_fix_rate(struct ieee80211_node *ni,
  628         struct ieee80211_rateset *nrs, int flags)
  629 {
  630         struct ieee80211vap *vap = ni->ni_vap;
  631         struct ieee80211com *ic = ni->ni_ic;
  632         int i, j, rix, error;
  633         int okrate, badrate, fixedrate, ucastrate;
  634         const struct ieee80211_rateset *srs;
  635         uint8_t r;
  636 
  637         error = 0;
  638         okrate = badrate = 0;
  639         ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
  640         if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
  641                 /*
  642                  * Workaround awkwardness with fixed rate.  We are called
  643                  * to check both the legacy rate set and the HT rate set
  644                  * but we must apply any legacy fixed rate check only to the
  645                  * legacy rate set and vice versa.  We cannot tell what type
  646                  * of rate set we've been given (legacy or HT) but we can
  647                  * distinguish the fixed rate type (MCS have 0x80 set).
  648                  * So to deal with this the caller communicates whether to
  649                  * check MCS or legacy rate using the flags and we use the
  650                  * type of any fixed rate to avoid applying an MCS to a
  651                  * legacy rate and vice versa.
  652                  */
  653                 if (ucastrate & 0x80) {
  654                         if (flags & IEEE80211_F_DOFRATE)
  655                                 flags &= ~IEEE80211_F_DOFRATE;
  656                 } else if ((ucastrate & 0x80) == 0) {
  657                         if (flags & IEEE80211_F_DOFMCS)
  658                                 flags &= ~IEEE80211_F_DOFMCS;
  659                 }
  660                 /* NB: required to make MCS match below work */
  661                 ucastrate &= IEEE80211_RATE_VAL;
  662         }
  663         fixedrate = IEEE80211_FIXED_RATE_NONE;
  664         /*
  665          * XXX we are called to process both MCS and legacy rates;
  666          * we must use the appropriate basic rate set or chaos will
  667          * ensue; for now callers that want MCS must supply
  668          * IEEE80211_F_DOBRS; at some point we'll need to split this
  669          * function so there are two variants, one for MCS and one
  670          * for legacy rates.
  671          */
  672         if (flags & IEEE80211_F_DOBRS)
  673                 srs = (const struct ieee80211_rateset *)
  674                     ieee80211_get_suphtrates(ic, ni->ni_chan);
  675         else
  676                 srs = ieee80211_get_suprates(ic, ni->ni_chan);
  677         for (i = 0; i < nrs->rs_nrates; ) {
  678                 if (flags & IEEE80211_F_DOSORT) {
  679                         /*
  680                          * Sort rates.
  681                          */
  682                         for (j = i + 1; j < nrs->rs_nrates; j++) {
  683                                 if (IEEE80211_RV(nrs->rs_rates[i]) >
  684                                     IEEE80211_RV(nrs->rs_rates[j])) {
  685                                         r = nrs->rs_rates[i];
  686                                         nrs->rs_rates[i] = nrs->rs_rates[j];
  687                                         nrs->rs_rates[j] = r;
  688                                 }
  689                         }
  690                 }
  691                 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
  692                 badrate = r;
  693                 /*
  694                  * Check for fixed rate.
  695                  */
  696                 if (r == ucastrate)
  697                         fixedrate = r;
  698                 /*
  699                  * Check against supported rates.
  700                  */
  701                 rix = findrix(srs, r);
  702                 if (flags & IEEE80211_F_DONEGO) {
  703                         if (rix < 0) {
  704                                 /*
  705                                  * A rate in the node's rate set is not
  706                                  * supported.  If this is a basic rate and we
  707                                  * are operating as a STA then this is an error.
  708                                  * Otherwise we just discard/ignore the rate.
  709                                  */
  710                                 if ((flags & IEEE80211_F_JOIN) &&
  711                                     (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
  712                                         error++;
  713                         } else if ((flags & IEEE80211_F_JOIN) == 0) {
  714                                 /*
  715                                  * Overwrite with the supported rate
  716                                  * value so any basic rate bit is set.
  717                                  */
  718                                 nrs->rs_rates[i] = srs->rs_rates[rix];
  719                         }
  720                 }
  721                 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
  722                         /*
  723                          * Delete unacceptable rates.
  724                          */
  725                         nrs->rs_nrates--;
  726                         for (j = i; j < nrs->rs_nrates; j++)
  727                                 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
  728                         nrs->rs_rates[j] = 0;
  729                         continue;
  730                 }
  731                 if (rix >= 0)
  732                         okrate = nrs->rs_rates[i];
  733                 i++;
  734         }
  735         if (okrate == 0 || error != 0 ||
  736             ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
  737              fixedrate != ucastrate)) {
  738                 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
  739                     "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
  740                     "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
  741                 return badrate | IEEE80211_RATE_BASIC;
  742         } else
  743                 return IEEE80211_RV(okrate);
  744 }
  745 
  746 /*
  747  * Reset 11g-related state.
  748  */
  749 void
  750 ieee80211_reset_erp(struct ieee80211com *ic)
  751 {
  752         ic->ic_flags &= ~IEEE80211_F_USEPROT;
  753         ic->ic_nonerpsta = 0;
  754         ic->ic_longslotsta = 0;
  755         /*
  756          * Short slot time is enabled only when operating in 11g
  757          * and not in an IBSS.  We must also honor whether or not
  758          * the driver is capable of doing it.
  759          */
  760         ieee80211_set_shortslottime(ic,
  761                 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
  762                 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
  763                 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
  764                 ic->ic_opmode == IEEE80211_M_HOSTAP &&
  765                 (ic->ic_caps & IEEE80211_C_SHSLOT)));
  766         /*
  767          * Set short preamble and ERP barker-preamble flags.
  768          */
  769         if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
  770             (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
  771                 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
  772                 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
  773         } else {
  774                 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
  775                 ic->ic_flags |= IEEE80211_F_USEBARKER;
  776         }
  777 }
  778 
  779 /*
  780  * Set the short slot time state and notify the driver.
  781  */
  782 void
  783 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
  784 {
  785         if (onoff)
  786                 ic->ic_flags |= IEEE80211_F_SHSLOT;
  787         else
  788                 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
  789         /* notify driver */
  790         if (ic->ic_updateslot != NULL)
  791                 ic->ic_updateslot(ic);
  792 }
  793 
  794 /*
  795  * Check if the specified rate set supports ERP.
  796  * NB: the rate set is assumed to be sorted.
  797  */
  798 int
  799 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
  800 {
  801         static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
  802         int i, j;
  803 
  804         if (rs->rs_nrates < nitems(rates))
  805                 return 0;
  806         for (i = 0; i < nitems(rates); i++) {
  807                 for (j = 0; j < rs->rs_nrates; j++) {
  808                         int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
  809                         if (rates[i] == r)
  810                                 goto next;
  811                         if (r > rates[i])
  812                                 return 0;
  813                 }
  814                 return 0;
  815         next:
  816                 ;
  817         }
  818         return 1;
  819 }
  820 
  821 /*
  822  * Mark the basic rates for the rate table based on the
  823  * operating mode.  For real 11g we mark all the 11b rates
  824  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
  825  * 11b rates.  There's also a pseudo 11a-mode used to mark only
  826  * the basic OFDM rates.
  827  */
  828 static void
  829 setbasicrates(struct ieee80211_rateset *rs,
  830     enum ieee80211_phymode mode, int add)
  831 {
  832         static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
  833             [IEEE80211_MODE_11A]        = { 3, { 12, 24, 48 } },
  834             [IEEE80211_MODE_11B]        = { 2, { 2, 4 } },
  835                                             /* NB: mixed b/g */
  836             [IEEE80211_MODE_11G]        = { 4, { 2, 4, 11, 22 } },
  837             [IEEE80211_MODE_TURBO_A]    = { 3, { 12, 24, 48 } },
  838             [IEEE80211_MODE_TURBO_G]    = { 4, { 2, 4, 11, 22 } },
  839             [IEEE80211_MODE_STURBO_A]   = { 3, { 12, 24, 48 } },
  840             [IEEE80211_MODE_HALF]       = { 3, { 6, 12, 24 } },
  841             [IEEE80211_MODE_QUARTER]    = { 3, { 3, 6, 12 } },
  842             [IEEE80211_MODE_11NA]       = { 3, { 12, 24, 48 } },
  843                                             /* NB: mixed b/g */
  844             [IEEE80211_MODE_11NG]       = { 4, { 2, 4, 11, 22 } },
  845         };
  846         int i, j;
  847 
  848         for (i = 0; i < rs->rs_nrates; i++) {
  849                 if (!add)
  850                         rs->rs_rates[i] &= IEEE80211_RATE_VAL;
  851                 for (j = 0; j < basic[mode].rs_nrates; j++)
  852                         if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
  853                                 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
  854                                 break;
  855                         }
  856         }
  857 }
  858 
  859 /*
  860  * Set the basic rates in a rate set.
  861  */
  862 void
  863 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
  864     enum ieee80211_phymode mode)
  865 {
  866         setbasicrates(rs, mode, 0);
  867 }
  868 
  869 /*
  870  * Add basic rates to a rate set.
  871  */
  872 void
  873 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
  874     enum ieee80211_phymode mode)
  875 {
  876         setbasicrates(rs, mode, 1);
  877 }
  878 
  879 /*
  880  * WME protocol support.
  881  *
  882  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
  883  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
  884  * Draft 2.0 Test Plan (Appendix D).
  885  *
  886  * Static/Dynamic Turbo mode settings come from Atheros.
  887  */
  888 typedef struct phyParamType {
  889         uint8_t         aifsn;
  890         uint8_t         logcwmin;
  891         uint8_t         logcwmax;
  892         uint16_t        txopLimit;
  893         uint8_t         acm;
  894 } paramType;
  895 
  896 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
  897         [IEEE80211_MODE_AUTO]   = { 3, 4,  6,  0, 0 },
  898         [IEEE80211_MODE_11A]    = { 3, 4,  6,  0, 0 },
  899         [IEEE80211_MODE_11B]    = { 3, 4,  6,  0, 0 },
  900         [IEEE80211_MODE_11G]    = { 3, 4,  6,  0, 0 },
  901         [IEEE80211_MODE_FH]     = { 3, 4,  6,  0, 0 },
  902         [IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
  903         [IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
  904         [IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
  905         [IEEE80211_MODE_HALF]   = { 3, 4,  6,  0, 0 },
  906         [IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
  907         [IEEE80211_MODE_11NA]   = { 3, 4,  6,  0, 0 },
  908         [IEEE80211_MODE_11NG]   = { 3, 4,  6,  0, 0 },
  909 };
  910 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
  911         [IEEE80211_MODE_AUTO]   = { 7, 4, 10,  0, 0 },
  912         [IEEE80211_MODE_11A]    = { 7, 4, 10,  0, 0 },
  913         [IEEE80211_MODE_11B]    = { 7, 4, 10,  0, 0 },
  914         [IEEE80211_MODE_11G]    = { 7, 4, 10,  0, 0 },
  915         [IEEE80211_MODE_FH]     = { 7, 4, 10,  0, 0 },
  916         [IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
  917         [IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
  918         [IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
  919         [IEEE80211_MODE_HALF]   = { 7, 4, 10,  0, 0 },
  920         [IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
  921         [IEEE80211_MODE_11NA]   = { 7, 4, 10,  0, 0 },
  922         [IEEE80211_MODE_11NG]   = { 7, 4, 10,  0, 0 },
  923 };
  924 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
  925         [IEEE80211_MODE_AUTO]   = { 1, 3, 4,  94, 0 },
  926         [IEEE80211_MODE_11A]    = { 1, 3, 4,  94, 0 },
  927         [IEEE80211_MODE_11B]    = { 1, 3, 4, 188, 0 },
  928         [IEEE80211_MODE_11G]    = { 1, 3, 4,  94, 0 },
  929         [IEEE80211_MODE_FH]     = { 1, 3, 4, 188, 0 },
  930         [IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
  931         [IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
  932         [IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
  933         [IEEE80211_MODE_HALF]   = { 1, 3, 4,  94, 0 },
  934         [IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
  935         [IEEE80211_MODE_11NA]   = { 1, 3, 4,  94, 0 },
  936         [IEEE80211_MODE_11NG]   = { 1, 3, 4,  94, 0 },
  937 };
  938 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
  939         [IEEE80211_MODE_AUTO]   = { 1, 2, 3,  47, 0 },
  940         [IEEE80211_MODE_11A]    = { 1, 2, 3,  47, 0 },
  941         [IEEE80211_MODE_11B]    = { 1, 2, 3, 102, 0 },
  942         [IEEE80211_MODE_11G]    = { 1, 2, 3,  47, 0 },
  943         [IEEE80211_MODE_FH]     = { 1, 2, 3, 102, 0 },
  944         [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
  945         [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
  946         [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
  947         [IEEE80211_MODE_HALF]   = { 1, 2, 3,  47, 0 },
  948         [IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
  949         [IEEE80211_MODE_11NA]   = { 1, 2, 3,  47, 0 },
  950         [IEEE80211_MODE_11NG]   = { 1, 2, 3,  47, 0 },
  951 };
  952 
  953 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
  954         [IEEE80211_MODE_AUTO]   = { 3, 4, 10,  0, 0 },
  955         [IEEE80211_MODE_11A]    = { 3, 4, 10,  0, 0 },
  956         [IEEE80211_MODE_11B]    = { 3, 4, 10,  0, 0 },
  957         [IEEE80211_MODE_11G]    = { 3, 4, 10,  0, 0 },
  958         [IEEE80211_MODE_FH]     = { 3, 4, 10,  0, 0 },
  959         [IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
  960         [IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
  961         [IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
  962         [IEEE80211_MODE_HALF]   = { 3, 4, 10,  0, 0 },
  963         [IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
  964         [IEEE80211_MODE_11NA]   = { 3, 4, 10,  0, 0 },
  965         [IEEE80211_MODE_11NG]   = { 3, 4, 10,  0, 0 },
  966 };
  967 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
  968         [IEEE80211_MODE_AUTO]   = { 2, 3, 4,  94, 0 },
  969         [IEEE80211_MODE_11A]    = { 2, 3, 4,  94, 0 },
  970         [IEEE80211_MODE_11B]    = { 2, 3, 4, 188, 0 },
  971         [IEEE80211_MODE_11G]    = { 2, 3, 4,  94, 0 },
  972         [IEEE80211_MODE_FH]     = { 2, 3, 4, 188, 0 },
  973         [IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
  974         [IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
  975         [IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
  976         [IEEE80211_MODE_HALF]   = { 2, 3, 4,  94, 0 },
  977         [IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
  978         [IEEE80211_MODE_11NA]   = { 2, 3, 4,  94, 0 },
  979         [IEEE80211_MODE_11NG]   = { 2, 3, 4,  94, 0 },
  980 };
  981 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
  982         [IEEE80211_MODE_AUTO]   = { 2, 2, 3,  47, 0 },
  983         [IEEE80211_MODE_11A]    = { 2, 2, 3,  47, 0 },
  984         [IEEE80211_MODE_11B]    = { 2, 2, 3, 102, 0 },
  985         [IEEE80211_MODE_11G]    = { 2, 2, 3,  47, 0 },
  986         [IEEE80211_MODE_FH]     = { 2, 2, 3, 102, 0 },
  987         [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
  988         [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
  989         [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
  990         [IEEE80211_MODE_HALF]   = { 2, 2, 3,  47, 0 },
  991         [IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
  992         [IEEE80211_MODE_11NA]   = { 2, 2, 3,  47, 0 },
  993         [IEEE80211_MODE_11NG]   = { 2, 2, 3,  47, 0 },
  994 };
  995 
  996 static void
  997 _setifsparams(struct wmeParams *wmep, const paramType *phy)
  998 {
  999         wmep->wmep_aifsn = phy->aifsn;
 1000         wmep->wmep_logcwmin = phy->logcwmin;    
 1001         wmep->wmep_logcwmax = phy->logcwmax;            
 1002         wmep->wmep_txopLimit = phy->txopLimit;
 1003 }
 1004 
 1005 static void
 1006 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
 1007         struct wmeParams *wmep, const paramType *phy)
 1008 {
 1009         wmep->wmep_acm = phy->acm;
 1010         _setifsparams(wmep, phy);
 1011 
 1012         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1013             "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
 1014             ieee80211_wme_acnames[ac], type,
 1015             wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
 1016             wmep->wmep_logcwmax, wmep->wmep_txopLimit);
 1017 }
 1018 
 1019 static void
 1020 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
 1021 {
 1022         struct ieee80211com *ic = vap->iv_ic;
 1023         struct ieee80211_wme_state *wme = &ic->ic_wme;
 1024         const paramType *pPhyParam, *pBssPhyParam;
 1025         struct wmeParams *wmep;
 1026         enum ieee80211_phymode mode;
 1027         int i;
 1028 
 1029         IEEE80211_LOCK_ASSERT(ic);
 1030 
 1031         if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
 1032                 return;
 1033 
 1034         /*
 1035          * Clear the wme cap_info field so a qoscount from a previous
 1036          * vap doesn't confuse later code which only parses the beacon
 1037          * field and updates hardware when said field changes.
 1038          * Otherwise the hardware is programmed with defaults, not what
 1039          * the beacon actually announces.
 1040          */
 1041         wme->wme_wmeChanParams.cap_info = 0;
 1042 
 1043         /*
 1044          * Select mode; we can be called early in which case we
 1045          * always use auto mode.  We know we'll be called when
 1046          * entering the RUN state with bsschan setup properly
 1047          * so state will eventually get set correctly
 1048          */
 1049         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
 1050                 mode = ieee80211_chan2mode(ic->ic_bsschan);
 1051         else
 1052                 mode = IEEE80211_MODE_AUTO;
 1053         for (i = 0; i < WME_NUM_AC; i++) {
 1054                 switch (i) {
 1055                 case WME_AC_BK:
 1056                         pPhyParam = &phyParamForAC_BK[mode];
 1057                         pBssPhyParam = &phyParamForAC_BK[mode];
 1058                         break;
 1059                 case WME_AC_VI:
 1060                         pPhyParam = &phyParamForAC_VI[mode];
 1061                         pBssPhyParam = &bssPhyParamForAC_VI[mode];
 1062                         break;
 1063                 case WME_AC_VO:
 1064                         pPhyParam = &phyParamForAC_VO[mode];
 1065                         pBssPhyParam = &bssPhyParamForAC_VO[mode];
 1066                         break;
 1067                 case WME_AC_BE:
 1068                 default:
 1069                         pPhyParam = &phyParamForAC_BE[mode];
 1070                         pBssPhyParam = &bssPhyParamForAC_BE[mode];
 1071                         break;
 1072                 }
 1073                 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
 1074                 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
 1075                         setwmeparams(vap, "chan", i, wmep, pPhyParam);
 1076                 } else {
 1077                         setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
 1078                 }       
 1079                 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
 1080                 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
 1081         }
 1082         /* NB: check ic_bss to avoid NULL deref on initial attach */
 1083         if (vap->iv_bss != NULL) {
 1084                 /*
 1085                  * Calculate aggressive mode switching threshold based
 1086                  * on beacon interval.  This doesn't need locking since
 1087                  * we're only called before entering the RUN state at
 1088                  * which point we start sending beacon frames.
 1089                  */
 1090                 wme->wme_hipri_switch_thresh =
 1091                         (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
 1092                 wme->wme_flags &= ~WME_F_AGGRMODE;
 1093                 ieee80211_wme_updateparams(vap);
 1094         }
 1095 }
 1096 
 1097 void
 1098 ieee80211_wme_initparams(struct ieee80211vap *vap)
 1099 {
 1100         struct ieee80211com *ic = vap->iv_ic;
 1101 
 1102         IEEE80211_LOCK(ic);
 1103         ieee80211_wme_initparams_locked(vap);
 1104         IEEE80211_UNLOCK(ic);
 1105 }
 1106 
 1107 /*
 1108  * Update WME parameters for ourself and the BSS.
 1109  */
 1110 void
 1111 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
 1112 {
 1113         static const paramType aggrParam[IEEE80211_MODE_MAX] = {
 1114             [IEEE80211_MODE_AUTO]       = { 2, 4, 10, 64, 0 },
 1115             [IEEE80211_MODE_11A]        = { 2, 4, 10, 64, 0 },
 1116             [IEEE80211_MODE_11B]        = { 2, 5, 10, 64, 0 },
 1117             [IEEE80211_MODE_11G]        = { 2, 4, 10, 64, 0 },
 1118             [IEEE80211_MODE_FH]         = { 2, 5, 10, 64, 0 },
 1119             [IEEE80211_MODE_TURBO_A]    = { 1, 3, 10, 64, 0 },
 1120             [IEEE80211_MODE_TURBO_G]    = { 1, 3, 10, 64, 0 },
 1121             [IEEE80211_MODE_STURBO_A]   = { 1, 3, 10, 64, 0 },
 1122             [IEEE80211_MODE_HALF]       = { 2, 4, 10, 64, 0 },
 1123             [IEEE80211_MODE_QUARTER]    = { 2, 4, 10, 64, 0 },
 1124             [IEEE80211_MODE_11NA]       = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1125             [IEEE80211_MODE_11NG]       = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1126         };
 1127         struct ieee80211com *ic = vap->iv_ic;
 1128         struct ieee80211_wme_state *wme = &ic->ic_wme;
 1129         const struct wmeParams *wmep;
 1130         struct wmeParams *chanp, *bssp;
 1131         enum ieee80211_phymode mode;
 1132         int i;
 1133         int do_aggrmode = 0;
 1134 
 1135         /*
 1136          * Set up the channel access parameters for the physical
 1137          * device.  First populate the configured settings.
 1138          */
 1139         for (i = 0; i < WME_NUM_AC; i++) {
 1140                 chanp = &wme->wme_chanParams.cap_wmeParams[i];
 1141                 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
 1142                 chanp->wmep_aifsn = wmep->wmep_aifsn;
 1143                 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
 1144                 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
 1145                 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
 1146 
 1147                 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
 1148                 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
 1149                 chanp->wmep_aifsn = wmep->wmep_aifsn;
 1150                 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
 1151                 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
 1152                 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
 1153         }
 1154 
 1155         /*
 1156          * Select mode; we can be called early in which case we
 1157          * always use auto mode.  We know we'll be called when
 1158          * entering the RUN state with bsschan setup properly
 1159          * so state will eventually get set correctly
 1160          */
 1161         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
 1162                 mode = ieee80211_chan2mode(ic->ic_bsschan);
 1163         else
 1164                 mode = IEEE80211_MODE_AUTO;
 1165 
 1166         /*
 1167          * This implements aggressive mode as found in certain
 1168          * vendors' AP's.  When there is significant high
 1169          * priority (VI/VO) traffic in the BSS throttle back BE
 1170          * traffic by using conservative parameters.  Otherwise
 1171          * BE uses aggressive params to optimize performance of
 1172          * legacy/non-QoS traffic.
 1173          */
 1174 
 1175         /* Hostap? Only if aggressive mode is enabled */
 1176         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1177              (wme->wme_flags & WME_F_AGGRMODE) != 0)
 1178                 do_aggrmode = 1;
 1179 
 1180         /*
 1181          * Station? Only if we're in a non-QoS BSS.
 1182          */
 1183         else if ((vap->iv_opmode == IEEE80211_M_STA &&
 1184              (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
 1185                 do_aggrmode = 1;
 1186 
 1187         /*
 1188          * IBSS? Only if we we have WME enabled.
 1189          */
 1190         else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
 1191             (vap->iv_flags & IEEE80211_F_WME))
 1192                 do_aggrmode = 1;
 1193 
 1194         /*
 1195          * If WME is disabled on this VAP, default to aggressive mode
 1196          * regardless of the configuration.
 1197          */
 1198         if ((vap->iv_flags & IEEE80211_F_WME) == 0)
 1199                 do_aggrmode = 1;
 1200 
 1201         /* XXX WDS? */
 1202 
 1203         /* XXX MBSS? */
 1204         
 1205         if (do_aggrmode) {
 1206                 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
 1207                 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
 1208 
 1209                 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
 1210                 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
 1211                     aggrParam[mode].logcwmin;
 1212                 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
 1213                     aggrParam[mode].logcwmax;
 1214                 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
 1215                     (vap->iv_flags & IEEE80211_F_BURST) ?
 1216                         aggrParam[mode].txopLimit : 0;          
 1217                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1218                     "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
 1219                     "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
 1220                     chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
 1221                     chanp->wmep_logcwmax, chanp->wmep_txopLimit);
 1222         }
 1223 
 1224 
 1225         /*
 1226          * Change the contention window based on the number of associated
 1227          * stations.  If the number of associated stations is 1 and
 1228          * aggressive mode is enabled, lower the contention window even
 1229          * further.
 1230          */
 1231         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1232             ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
 1233                 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
 1234                     [IEEE80211_MODE_AUTO]       = 3,
 1235                     [IEEE80211_MODE_11A]        = 3,
 1236                     [IEEE80211_MODE_11B]        = 4,
 1237                     [IEEE80211_MODE_11G]        = 3,
 1238                     [IEEE80211_MODE_FH]         = 4,
 1239                     [IEEE80211_MODE_TURBO_A]    = 3,
 1240                     [IEEE80211_MODE_TURBO_G]    = 3,
 1241                     [IEEE80211_MODE_STURBO_A]   = 3,
 1242                     [IEEE80211_MODE_HALF]       = 3,
 1243                     [IEEE80211_MODE_QUARTER]    = 3,
 1244                     [IEEE80211_MODE_11NA]       = 3,
 1245                     [IEEE80211_MODE_11NG]       = 3,
 1246                 };
 1247                 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
 1248                 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
 1249 
 1250                 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
 1251                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1252                     "update %s (chan+bss) logcwmin %u\n",
 1253                     ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
 1254         }
 1255 
 1256         /*
 1257          * Arrange for the beacon update.
 1258          *
 1259          * XXX what about MBSS, WDS?
 1260          */
 1261         if (vap->iv_opmode == IEEE80211_M_HOSTAP
 1262             || vap->iv_opmode == IEEE80211_M_IBSS) {
 1263                 /*
 1264                  * Arrange for a beacon update and bump the parameter
 1265                  * set number so associated stations load the new values.
 1266                  */
 1267                 wme->wme_bssChanParams.cap_info =
 1268                         (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
 1269                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
 1270         }
 1271 
 1272         /* schedule the deferred WME update */
 1273         ieee80211_runtask(ic, &ic->ic_wme_task);
 1274 
 1275         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1276             "%s: WME params updated, cap_info 0x%x\n", __func__,
 1277             vap->iv_opmode == IEEE80211_M_STA ?
 1278                 wme->wme_wmeChanParams.cap_info :
 1279                 wme->wme_bssChanParams.cap_info);
 1280 }
 1281 
 1282 void
 1283 ieee80211_wme_updateparams(struct ieee80211vap *vap)
 1284 {
 1285         struct ieee80211com *ic = vap->iv_ic;
 1286 
 1287         if (ic->ic_caps & IEEE80211_C_WME) {
 1288                 IEEE80211_LOCK(ic);
 1289                 ieee80211_wme_updateparams_locked(vap);
 1290                 IEEE80211_UNLOCK(ic);
 1291         }
 1292 }
 1293 
 1294 static void
 1295 parent_updown(void *arg, int npending)
 1296 {
 1297         struct ieee80211com *ic = arg;
 1298 
 1299         ic->ic_parent(ic);
 1300 }
 1301 
 1302 static void
 1303 update_mcast(void *arg, int npending)
 1304 {
 1305         struct ieee80211com *ic = arg;
 1306 
 1307         ic->ic_update_mcast(ic);
 1308 }
 1309 
 1310 static void
 1311 update_promisc(void *arg, int npending)
 1312 {
 1313         struct ieee80211com *ic = arg;
 1314 
 1315         ic->ic_update_promisc(ic);
 1316 }
 1317 
 1318 static void
 1319 update_channel(void *arg, int npending)
 1320 {
 1321         struct ieee80211com *ic = arg;
 1322 
 1323         ic->ic_set_channel(ic);
 1324         ieee80211_radiotap_chan_change(ic);
 1325 }
 1326 
 1327 static void
 1328 update_chw(void *arg, int npending)
 1329 {
 1330         struct ieee80211com *ic = arg;
 1331 
 1332         /*
 1333          * XXX should we defer the channel width _config_ update until now?
 1334          */
 1335         ic->ic_update_chw(ic);
 1336 }
 1337 
 1338 static void
 1339 update_wme(void *arg, int npending)
 1340 {
 1341         struct ieee80211com *ic = arg;
 1342 
 1343         /*
 1344          * XXX should we defer the WME configuration update until now?
 1345          */
 1346         ic->ic_wme.wme_update(ic);
 1347 }
 1348 
 1349 static void
 1350 restart_vaps(void *arg, int npending)
 1351 {
 1352         struct ieee80211com *ic = arg;
 1353 
 1354         ieee80211_suspend_all(ic);
 1355         ieee80211_resume_all(ic);
 1356 }
 1357 
 1358 /*
 1359  * Block until the parent is in a known state.  This is
 1360  * used after any operations that dispatch a task (e.g.
 1361  * to auto-configure the parent device up/down).
 1362  */
 1363 void
 1364 ieee80211_waitfor_parent(struct ieee80211com *ic)
 1365 {
 1366         taskqueue_block(ic->ic_tq);
 1367         ieee80211_draintask(ic, &ic->ic_parent_task);
 1368         ieee80211_draintask(ic, &ic->ic_mcast_task);
 1369         ieee80211_draintask(ic, &ic->ic_promisc_task);
 1370         ieee80211_draintask(ic, &ic->ic_chan_task);
 1371         ieee80211_draintask(ic, &ic->ic_bmiss_task);
 1372         ieee80211_draintask(ic, &ic->ic_chw_task);
 1373         ieee80211_draintask(ic, &ic->ic_wme_task);
 1374         taskqueue_unblock(ic->ic_tq);
 1375 }
 1376 
 1377 /*
 1378  * Check to see whether the current channel needs reset.
 1379  *
 1380  * Some devices don't handle being given an invalid channel
 1381  * in their operating mode very well (eg wpi(4) will throw a
 1382  * firmware exception.)
 1383  *
 1384  * Return 0 if we're ok, 1 if the channel needs to be reset.
 1385  *
 1386  * See PR kern/202502.
 1387  */
 1388 static int
 1389 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
 1390 {
 1391         struct ieee80211com *ic = vap->iv_ic;
 1392 
 1393         if ((vap->iv_opmode == IEEE80211_M_IBSS &&
 1394              IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
 1395             (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1396              IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
 1397                 return (1);
 1398         return (0);
 1399 }
 1400 
 1401 /*
 1402  * Reset the curchan to a known good state.
 1403  */
 1404 static void
 1405 ieee80211_start_reset_chan(struct ieee80211vap *vap)
 1406 {
 1407         struct ieee80211com *ic = vap->iv_ic;
 1408 
 1409         ic->ic_curchan = &ic->ic_channels[0];
 1410 }
 1411 
 1412 /*
 1413  * Start a vap running.  If this is the first vap to be
 1414  * set running on the underlying device then we
 1415  * automatically bring the device up.
 1416  */
 1417 void
 1418 ieee80211_start_locked(struct ieee80211vap *vap)
 1419 {
 1420         struct ifnet *ifp = vap->iv_ifp;
 1421         struct ieee80211com *ic = vap->iv_ic;
 1422 
 1423         IEEE80211_LOCK_ASSERT(ic);
 1424 
 1425         IEEE80211_DPRINTF(vap,
 1426                 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1427                 "start running, %d vaps running\n", ic->ic_nrunning);
 1428 
 1429         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 1430                 /*
 1431                  * Mark us running.  Note that it's ok to do this first;
 1432                  * if we need to bring the parent device up we defer that
 1433                  * to avoid dropping the com lock.  We expect the device
 1434                  * to respond to being marked up by calling back into us
 1435                  * through ieee80211_start_all at which point we'll come
 1436                  * back in here and complete the work.
 1437                  */
 1438                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1439                 /*
 1440                  * We are not running; if this we are the first vap
 1441                  * to be brought up auto-up the parent if necessary.
 1442                  */
 1443                 if (ic->ic_nrunning++ == 0) {
 1444 
 1445                         /* reset the channel to a known good channel */
 1446                         if (ieee80211_start_check_reset_chan(vap))
 1447                                 ieee80211_start_reset_chan(vap);
 1448 
 1449                         IEEE80211_DPRINTF(vap,
 1450                             IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1451                             "%s: up parent %s\n", __func__, ic->ic_name);
 1452                         ieee80211_runtask(ic, &ic->ic_parent_task);
 1453                         return;
 1454                 }
 1455         }
 1456         /*
 1457          * If the parent is up and running, then kick the
 1458          * 802.11 state machine as appropriate.
 1459          */
 1460         if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
 1461                 if (vap->iv_opmode == IEEE80211_M_STA) {
 1462 #if 0
 1463                         /* XXX bypasses scan too easily; disable for now */
 1464                         /*
 1465                          * Try to be intelligent about clocking the state
 1466                          * machine.  If we're currently in RUN state then
 1467                          * we should be able to apply any new state/parameters
 1468                          * simply by re-associating.  Otherwise we need to
 1469                          * re-scan to select an appropriate ap.
 1470                          */ 
 1471                         if (vap->iv_state >= IEEE80211_S_RUN)
 1472                                 ieee80211_new_state_locked(vap,
 1473                                     IEEE80211_S_ASSOC, 1);
 1474                         else
 1475 #endif
 1476                                 ieee80211_new_state_locked(vap,
 1477                                     IEEE80211_S_SCAN, 0);
 1478                 } else {
 1479                         /*
 1480                          * For monitor+wds mode there's nothing to do but
 1481                          * start running.  Otherwise if this is the first
 1482                          * vap to be brought up, start a scan which may be
 1483                          * preempted if the station is locked to a particular
 1484                          * channel.
 1485                          */
 1486                         vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
 1487                         if (vap->iv_opmode == IEEE80211_M_MONITOR ||
 1488                             vap->iv_opmode == IEEE80211_M_WDS)
 1489                                 ieee80211_new_state_locked(vap,
 1490                                     IEEE80211_S_RUN, -1);
 1491                         else
 1492                                 ieee80211_new_state_locked(vap,
 1493                                     IEEE80211_S_SCAN, 0);
 1494                 }
 1495         }
 1496 }
 1497 
 1498 /*
 1499  * Start a single vap.
 1500  */
 1501 void
 1502 ieee80211_init(void *arg)
 1503 {
 1504         struct ieee80211vap *vap = arg;
 1505 
 1506         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1507             "%s\n", __func__);
 1508 
 1509         IEEE80211_LOCK(vap->iv_ic);
 1510         ieee80211_start_locked(vap);
 1511         IEEE80211_UNLOCK(vap->iv_ic);
 1512 }
 1513 
 1514 /*
 1515  * Start all runnable vap's on a device.
 1516  */
 1517 void
 1518 ieee80211_start_all(struct ieee80211com *ic)
 1519 {
 1520         struct ieee80211vap *vap;
 1521 
 1522         IEEE80211_LOCK(ic);
 1523         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1524                 struct ifnet *ifp = vap->iv_ifp;
 1525                 if (IFNET_IS_UP_RUNNING(ifp))   /* NB: avoid recursion */
 1526                         ieee80211_start_locked(vap);
 1527         }
 1528         IEEE80211_UNLOCK(ic);
 1529 }
 1530 
 1531 /*
 1532  * Stop a vap.  We force it down using the state machine
 1533  * then mark it's ifnet not running.  If this is the last
 1534  * vap running on the underlying device then we close it
 1535  * too to insure it will be properly initialized when the
 1536  * next vap is brought up.
 1537  */
 1538 void
 1539 ieee80211_stop_locked(struct ieee80211vap *vap)
 1540 {
 1541         struct ieee80211com *ic = vap->iv_ic;
 1542         struct ifnet *ifp = vap->iv_ifp;
 1543 
 1544         IEEE80211_LOCK_ASSERT(ic);
 1545 
 1546         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1547             "stop running, %d vaps running\n", ic->ic_nrunning);
 1548 
 1549         ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
 1550         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
 1551                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;  /* mark us stopped */
 1552                 if (--ic->ic_nrunning == 0) {
 1553                         IEEE80211_DPRINTF(vap,
 1554                             IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1555                             "down parent %s\n", ic->ic_name);
 1556                         ieee80211_runtask(ic, &ic->ic_parent_task);
 1557                 }
 1558         }
 1559 }
 1560 
 1561 void
 1562 ieee80211_stop(struct ieee80211vap *vap)
 1563 {
 1564         struct ieee80211com *ic = vap->iv_ic;
 1565 
 1566         IEEE80211_LOCK(ic);
 1567         ieee80211_stop_locked(vap);
 1568         IEEE80211_UNLOCK(ic);
 1569 }
 1570 
 1571 /*
 1572  * Stop all vap's running on a device.
 1573  */
 1574 void
 1575 ieee80211_stop_all(struct ieee80211com *ic)
 1576 {
 1577         struct ieee80211vap *vap;
 1578 
 1579         IEEE80211_LOCK(ic);
 1580         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1581                 struct ifnet *ifp = vap->iv_ifp;
 1582                 if (IFNET_IS_UP_RUNNING(ifp))   /* NB: avoid recursion */
 1583                         ieee80211_stop_locked(vap);
 1584         }
 1585         IEEE80211_UNLOCK(ic);
 1586 
 1587         ieee80211_waitfor_parent(ic);
 1588 }
 1589 
 1590 /*
 1591  * Stop all vap's running on a device and arrange
 1592  * for those that were running to be resumed.
 1593  */
 1594 void
 1595 ieee80211_suspend_all(struct ieee80211com *ic)
 1596 {
 1597         struct ieee80211vap *vap;
 1598 
 1599         IEEE80211_LOCK(ic);
 1600         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1601                 struct ifnet *ifp = vap->iv_ifp;
 1602                 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
 1603                         vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
 1604                         ieee80211_stop_locked(vap);
 1605                 }
 1606         }
 1607         IEEE80211_UNLOCK(ic);
 1608 
 1609         ieee80211_waitfor_parent(ic);
 1610 }
 1611 
 1612 /*
 1613  * Start all vap's marked for resume.
 1614  */
 1615 void
 1616 ieee80211_resume_all(struct ieee80211com *ic)
 1617 {
 1618         struct ieee80211vap *vap;
 1619 
 1620         IEEE80211_LOCK(ic);
 1621         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1622                 struct ifnet *ifp = vap->iv_ifp;
 1623                 if (!IFNET_IS_UP_RUNNING(ifp) &&
 1624                     (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
 1625                         vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
 1626                         ieee80211_start_locked(vap);
 1627                 }
 1628         }
 1629         IEEE80211_UNLOCK(ic);
 1630 }
 1631 
 1632 /*
 1633  * Restart all vap's running on a device.
 1634  */
 1635 void
 1636 ieee80211_restart_all(struct ieee80211com *ic)
 1637 {
 1638         /*
 1639          * NB: do not use ieee80211_runtask here, we will
 1640          * block & drain net80211 taskqueue.
 1641          */
 1642         taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
 1643 }
 1644 
 1645 void
 1646 ieee80211_beacon_miss(struct ieee80211com *ic)
 1647 {
 1648         IEEE80211_LOCK(ic);
 1649         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
 1650                 /* Process in a taskq, the handler may reenter the driver */
 1651                 ieee80211_runtask(ic, &ic->ic_bmiss_task);
 1652         }
 1653         IEEE80211_UNLOCK(ic);
 1654 }
 1655 
 1656 static void
 1657 beacon_miss(void *arg, int npending)
 1658 {
 1659         struct ieee80211com *ic = arg;
 1660         struct ieee80211vap *vap;
 1661 
 1662         IEEE80211_LOCK(ic);
 1663         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1664                 /*
 1665                  * We only pass events through for sta vap's in RUN+ state;
 1666                  * may be too restrictive but for now this saves all the
 1667                  * handlers duplicating these checks.
 1668                  */
 1669                 if (vap->iv_opmode == IEEE80211_M_STA &&
 1670                     vap->iv_state >= IEEE80211_S_RUN &&
 1671                     vap->iv_bmiss != NULL)
 1672                         vap->iv_bmiss(vap);
 1673         }
 1674         IEEE80211_UNLOCK(ic);
 1675 }
 1676 
 1677 static void
 1678 beacon_swmiss(void *arg, int npending)
 1679 {
 1680         struct ieee80211vap *vap = arg;
 1681         struct ieee80211com *ic = vap->iv_ic;
 1682 
 1683         IEEE80211_LOCK(ic);
 1684         if (vap->iv_state >= IEEE80211_S_RUN) {
 1685                 /* XXX Call multiple times if npending > zero? */
 1686                 vap->iv_bmiss(vap);
 1687         }
 1688         IEEE80211_UNLOCK(ic);
 1689 }
 1690 
 1691 /*
 1692  * Software beacon miss handling.  Check if any beacons
 1693  * were received in the last period.  If not post a
 1694  * beacon miss; otherwise reset the counter.
 1695  */
 1696 void
 1697 ieee80211_swbmiss(void *arg)
 1698 {
 1699         struct ieee80211vap *vap = arg;
 1700         struct ieee80211com *ic = vap->iv_ic;
 1701 
 1702         IEEE80211_LOCK_ASSERT(ic);
 1703 
 1704         KASSERT(vap->iv_state >= IEEE80211_S_RUN,
 1705             ("wrong state %d", vap->iv_state));
 1706 
 1707         if (ic->ic_flags & IEEE80211_F_SCAN) {
 1708                 /*
 1709                  * If scanning just ignore and reset state.  If we get a
 1710                  * bmiss after coming out of scan because we haven't had
 1711                  * time to receive a beacon then we should probe the AP
 1712                  * before posting a real bmiss (unless iv_bmiss_max has
 1713                  * been artifiically lowered).  A cleaner solution might
 1714                  * be to disable the timer on scan start/end but to handle
 1715                  * case of multiple sta vap's we'd need to disable the
 1716                  * timers of all affected vap's.
 1717                  */
 1718                 vap->iv_swbmiss_count = 0;
 1719         } else if (vap->iv_swbmiss_count == 0) {
 1720                 if (vap->iv_bmiss != NULL)
 1721                         ieee80211_runtask(ic, &vap->iv_swbmiss_task);
 1722         } else
 1723                 vap->iv_swbmiss_count = 0;
 1724         callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
 1725                 ieee80211_swbmiss, vap);
 1726 }
 1727 
 1728 /*
 1729  * Start an 802.11h channel switch.  We record the parameters,
 1730  * mark the operation pending, notify each vap through the
 1731  * beacon update mechanism so it can update the beacon frame
 1732  * contents, and then switch vap's to CSA state to block outbound
 1733  * traffic.  Devices that handle CSA directly can use the state
 1734  * switch to do the right thing so long as they call
 1735  * ieee80211_csa_completeswitch when it's time to complete the
 1736  * channel change.  Devices that depend on the net80211 layer can
 1737  * use ieee80211_beacon_update to handle the countdown and the
 1738  * channel switch.
 1739  */
 1740 void
 1741 ieee80211_csa_startswitch(struct ieee80211com *ic,
 1742         struct ieee80211_channel *c, int mode, int count)
 1743 {
 1744         struct ieee80211vap *vap;
 1745 
 1746         IEEE80211_LOCK_ASSERT(ic);
 1747 
 1748         ic->ic_csa_newchan = c;
 1749         ic->ic_csa_mode = mode;
 1750         ic->ic_csa_count = count;
 1751         ic->ic_flags |= IEEE80211_F_CSAPENDING;
 1752         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1753                 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
 1754                     vap->iv_opmode == IEEE80211_M_IBSS ||
 1755                     vap->iv_opmode == IEEE80211_M_MBSS)
 1756                         ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
 1757                 /* switch to CSA state to block outbound traffic */
 1758                 if (vap->iv_state == IEEE80211_S_RUN)
 1759                         ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
 1760         }
 1761         ieee80211_notify_csa(ic, c, mode, count);
 1762 }
 1763 
 1764 /*
 1765  * Complete the channel switch by transitioning all CSA VAPs to RUN.
 1766  * This is called by both the completion and cancellation functions
 1767  * so each VAP is placed back in the RUN state and can thus transmit.
 1768  */
 1769 static void
 1770 csa_completeswitch(struct ieee80211com *ic)
 1771 {
 1772         struct ieee80211vap *vap;
 1773 
 1774         ic->ic_csa_newchan = NULL;
 1775         ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
 1776 
 1777         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
 1778                 if (vap->iv_state == IEEE80211_S_CSA)
 1779                         ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
 1780 }
 1781 
 1782 /*
 1783  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
 1784  * We clear state and move all vap's in CSA state to RUN state
 1785  * so they can again transmit.
 1786  *
 1787  * Although this may not be completely correct, update the BSS channel
 1788  * for each VAP to the newly configured channel. The setcurchan sets
 1789  * the current operating channel for the interface (so the radio does
 1790  * switch over) but the VAP BSS isn't updated, leading to incorrectly
 1791  * reported information via ioctl.
 1792  */
 1793 void
 1794 ieee80211_csa_completeswitch(struct ieee80211com *ic)
 1795 {
 1796         struct ieee80211vap *vap;
 1797 
 1798         IEEE80211_LOCK_ASSERT(ic);
 1799 
 1800         KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
 1801 
 1802         ieee80211_setcurchan(ic, ic->ic_csa_newchan);
 1803         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
 1804                 if (vap->iv_state == IEEE80211_S_CSA)
 1805                         vap->iv_bss->ni_chan = ic->ic_curchan;
 1806 
 1807         csa_completeswitch(ic);
 1808 }
 1809 
 1810 /*
 1811  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
 1812  * We clear state and move all vap's in CSA state to RUN state
 1813  * so they can again transmit.
 1814  */
 1815 void
 1816 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
 1817 {
 1818         IEEE80211_LOCK_ASSERT(ic);
 1819 
 1820         csa_completeswitch(ic);
 1821 }
 1822 
 1823 /*
 1824  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
 1825  * We clear state and move all vap's in CAC state to RUN state.
 1826  */
 1827 void
 1828 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
 1829 {
 1830         struct ieee80211com *ic = vap0->iv_ic;
 1831         struct ieee80211vap *vap;
 1832 
 1833         IEEE80211_LOCK(ic);
 1834         /*
 1835          * Complete CAC state change for lead vap first; then
 1836          * clock all the other vap's waiting.
 1837          */
 1838         KASSERT(vap0->iv_state == IEEE80211_S_CAC,
 1839             ("wrong state %d", vap0->iv_state));
 1840         ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
 1841 
 1842         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
 1843                 if (vap->iv_state == IEEE80211_S_CAC)
 1844                         ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
 1845         IEEE80211_UNLOCK(ic);
 1846 }
 1847 
 1848 /*
 1849  * Force all vap's other than the specified vap to the INIT state
 1850  * and mark them as waiting for a scan to complete.  These vaps
 1851  * will be brought up when the scan completes and the scanning vap
 1852  * reaches RUN state by wakeupwaiting.
 1853  */
 1854 static void
 1855 markwaiting(struct ieee80211vap *vap0)
 1856 {
 1857         struct ieee80211com *ic = vap0->iv_ic;
 1858         struct ieee80211vap *vap;
 1859 
 1860         IEEE80211_LOCK_ASSERT(ic);
 1861 
 1862         /*
 1863          * A vap list entry can not disappear since we are running on the
 1864          * taskqueue and a vap destroy will queue and drain another state
 1865          * change task.
 1866          */
 1867         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1868                 if (vap == vap0)
 1869                         continue;
 1870                 if (vap->iv_state != IEEE80211_S_INIT) {
 1871                         /* NB: iv_newstate may drop the lock */
 1872                         vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
 1873                         IEEE80211_LOCK_ASSERT(ic);
 1874                         vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
 1875                 }
 1876         }
 1877 }
 1878 
 1879 /*
 1880  * Wakeup all vap's waiting for a scan to complete.  This is the
 1881  * companion to markwaiting (above) and is used to coordinate
 1882  * multiple vaps scanning.
 1883  * This is called from the state taskqueue.
 1884  */
 1885 static void
 1886 wakeupwaiting(struct ieee80211vap *vap0)
 1887 {
 1888         struct ieee80211com *ic = vap0->iv_ic;
 1889         struct ieee80211vap *vap;
 1890 
 1891         IEEE80211_LOCK_ASSERT(ic);
 1892 
 1893         /*
 1894          * A vap list entry can not disappear since we are running on the
 1895          * taskqueue and a vap destroy will queue and drain another state
 1896          * change task.
 1897          */
 1898         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1899                 if (vap == vap0)
 1900                         continue;
 1901                 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
 1902                         vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
 1903                         /* NB: sta's cannot go INIT->RUN */
 1904                         /* NB: iv_newstate may drop the lock */
 1905                         vap->iv_newstate(vap,
 1906                             vap->iv_opmode == IEEE80211_M_STA ?
 1907                                 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
 1908                         IEEE80211_LOCK_ASSERT(ic);
 1909                 }
 1910         }
 1911 }
 1912 
 1913 /*
 1914  * Handle post state change work common to all operating modes.
 1915  */
 1916 static void
 1917 ieee80211_newstate_cb(void *xvap, int npending)
 1918 {
 1919         struct ieee80211vap *vap = xvap;
 1920         struct ieee80211com *ic = vap->iv_ic;
 1921         enum ieee80211_state nstate, ostate;
 1922         int arg, rc;
 1923 
 1924         IEEE80211_LOCK(ic);
 1925         nstate = vap->iv_nstate;
 1926         arg = vap->iv_nstate_arg;
 1927 
 1928         if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
 1929                 /*
 1930                  * We have been requested to drop back to the INIT before
 1931                  * proceeding to the new state.
 1932                  */
 1933                 /* Deny any state changes while we are here. */
 1934                 vap->iv_nstate = IEEE80211_S_INIT;
 1935                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 1936                     "%s: %s -> %s arg %d\n", __func__,
 1937                     ieee80211_state_name[vap->iv_state],
 1938                     ieee80211_state_name[vap->iv_nstate], arg);
 1939                 vap->iv_newstate(vap, vap->iv_nstate, 0);
 1940                 IEEE80211_LOCK_ASSERT(ic);
 1941                 vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
 1942                     IEEE80211_FEXT_STATEWAIT);
 1943                 /* enqueue new state transition after cancel_scan() task */
 1944                 ieee80211_new_state_locked(vap, nstate, arg);
 1945                 goto done;
 1946         }
 1947 
 1948         ostate = vap->iv_state;
 1949         if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
 1950                 /*
 1951                  * SCAN was forced; e.g. on beacon miss.  Force other running
 1952                  * vap's to INIT state and mark them as waiting for the scan to
 1953                  * complete.  This insures they don't interfere with our
 1954                  * scanning.  Since we are single threaded the vaps can not
 1955                  * transition again while we are executing.
 1956                  *
 1957                  * XXX not always right, assumes ap follows sta
 1958                  */
 1959                 markwaiting(vap);
 1960         }
 1961         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 1962             "%s: %s -> %s arg %d\n", __func__,
 1963             ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
 1964 
 1965         rc = vap->iv_newstate(vap, nstate, arg);
 1966         IEEE80211_LOCK_ASSERT(ic);
 1967         vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
 1968         if (rc != 0) {
 1969                 /* State transition failed */
 1970                 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
 1971                 KASSERT(nstate != IEEE80211_S_INIT,
 1972                     ("INIT state change failed"));
 1973                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 1974                     "%s: %s returned error %d\n", __func__,
 1975                     ieee80211_state_name[nstate], rc);
 1976                 goto done;
 1977         }
 1978 
 1979         /* No actual transition, skip post processing */
 1980         if (ostate == nstate)
 1981                 goto done;
 1982 
 1983         if (nstate == IEEE80211_S_RUN) {
 1984                 /*
 1985                  * OACTIVE may be set on the vap if the upper layer
 1986                  * tried to transmit (e.g. IPv6 NDP) before we reach
 1987                  * RUN state.  Clear it and restart xmit.
 1988                  *
 1989                  * Note this can also happen as a result of SLEEP->RUN
 1990                  * (i.e. coming out of power save mode).
 1991                  */
 1992                 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1993 
 1994                 /*
 1995                  * XXX TODO Kick-start a VAP queue - this should be a method!
 1996                  */
 1997 
 1998                 /* bring up any vaps waiting on us */
 1999                 wakeupwaiting(vap);
 2000         } else if (nstate == IEEE80211_S_INIT) {
 2001                 /*
 2002                  * Flush the scan cache if we did the last scan (XXX?)
 2003                  * and flush any frames on send queues from this vap.
 2004                  * Note the mgt q is used only for legacy drivers and
 2005                  * will go away shortly.
 2006                  */
 2007                 ieee80211_scan_flush(vap);
 2008 
 2009                 /*
 2010                  * XXX TODO: ic/vap queue flush
 2011                  */
 2012         }
 2013 done:
 2014         IEEE80211_UNLOCK(ic);
 2015 }
 2016 
 2017 /*
 2018  * Public interface for initiating a state machine change.
 2019  * This routine single-threads the request and coordinates
 2020  * the scheduling of multiple vaps for the purpose of selecting
 2021  * an operating channel.  Specifically the following scenarios
 2022  * are handled:
 2023  * o only one vap can be selecting a channel so on transition to
 2024  *   SCAN state if another vap is already scanning then
 2025  *   mark the caller for later processing and return without
 2026  *   doing anything (XXX? expectations by caller of synchronous operation)
 2027  * o only one vap can be doing CAC of a channel so on transition to
 2028  *   CAC state if another vap is already scanning for radar then
 2029  *   mark the caller for later processing and return without
 2030  *   doing anything (XXX? expectations by caller of synchronous operation)
 2031  * o if another vap is already running when a request is made
 2032  *   to SCAN then an operating channel has been chosen; bypass
 2033  *   the scan and just join the channel
 2034  *
 2035  * Note that the state change call is done through the iv_newstate
 2036  * method pointer so any driver routine gets invoked.  The driver
 2037  * will normally call back into operating mode-specific
 2038  * ieee80211_newstate routines (below) unless it needs to completely
 2039  * bypass the state machine (e.g. because the firmware has it's
 2040  * own idea how things should work).  Bypassing the net80211 layer
 2041  * is usually a mistake and indicates lack of proper integration
 2042  * with the net80211 layer.
 2043  */
 2044 int
 2045 ieee80211_new_state_locked(struct ieee80211vap *vap,
 2046         enum ieee80211_state nstate, int arg)
 2047 {
 2048         struct ieee80211com *ic = vap->iv_ic;
 2049         struct ieee80211vap *vp;
 2050         enum ieee80211_state ostate;
 2051         int nrunning, nscanning;
 2052 
 2053         IEEE80211_LOCK_ASSERT(ic);
 2054 
 2055         if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
 2056                 if (vap->iv_nstate == IEEE80211_S_INIT ||
 2057                     ((vap->iv_state == IEEE80211_S_INIT ||
 2058                     (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
 2059                     vap->iv_nstate == IEEE80211_S_SCAN &&
 2060                     nstate > IEEE80211_S_SCAN)) {
 2061                         /*
 2062                          * XXX The vap is being stopped/started,
 2063                          * do not allow any other state changes
 2064                          * until this is completed.
 2065                          */
 2066                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2067                             "%s: %s -> %s (%s) transition discarded\n",
 2068                             __func__,
 2069                             ieee80211_state_name[vap->iv_state],
 2070                             ieee80211_state_name[nstate],
 2071                             ieee80211_state_name[vap->iv_nstate]);
 2072                         return -1;
 2073                 } else if (vap->iv_state != vap->iv_nstate) {
 2074 #if 0
 2075                         /* Warn if the previous state hasn't completed. */
 2076                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2077                             "%s: pending %s -> %s transition lost\n", __func__,
 2078                             ieee80211_state_name[vap->iv_state],
 2079                             ieee80211_state_name[vap->iv_nstate]);
 2080 #else
 2081                         /* XXX temporarily enable to identify issues */
 2082                         if_printf(vap->iv_ifp,
 2083                             "%s: pending %s -> %s transition lost\n",
 2084                             __func__, ieee80211_state_name[vap->iv_state],
 2085                             ieee80211_state_name[vap->iv_nstate]);
 2086 #endif
 2087                 }
 2088         }
 2089 
 2090         nrunning = nscanning = 0;
 2091         /* XXX can track this state instead of calculating */
 2092         TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
 2093                 if (vp != vap) {
 2094                         if (vp->iv_state >= IEEE80211_S_RUN)
 2095                                 nrunning++;
 2096                         /* XXX doesn't handle bg scan */
 2097                         /* NB: CAC+AUTH+ASSOC treated like SCAN */
 2098                         else if (vp->iv_state > IEEE80211_S_INIT)
 2099                                 nscanning++;
 2100                 }
 2101         }
 2102         ostate = vap->iv_state;
 2103         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2104             "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
 2105             ieee80211_state_name[ostate], ieee80211_state_name[nstate],
 2106             nrunning, nscanning);
 2107         switch (nstate) {
 2108         case IEEE80211_S_SCAN:
 2109                 if (ostate == IEEE80211_S_INIT) {
 2110                         /*
 2111                          * INIT -> SCAN happens on initial bringup.
 2112                          */
 2113                         KASSERT(!(nscanning && nrunning),
 2114                             ("%d scanning and %d running", nscanning, nrunning));
 2115                         if (nscanning) {
 2116                                 /*
 2117                                  * Someone is scanning, defer our state
 2118                                  * change until the work has completed.
 2119                                  */
 2120                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2121                                     "%s: defer %s -> %s\n",
 2122                                     __func__, ieee80211_state_name[ostate],
 2123                                     ieee80211_state_name[nstate]);
 2124                                 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
 2125                                 return 0;
 2126                         }
 2127                         if (nrunning) {
 2128                                 /*
 2129                                  * Someone is operating; just join the channel
 2130                                  * they have chosen.
 2131                                  */
 2132                                 /* XXX kill arg? */
 2133                                 /* XXX check each opmode, adhoc? */
 2134                                 if (vap->iv_opmode == IEEE80211_M_STA)
 2135                                         nstate = IEEE80211_S_SCAN;
 2136                                 else
 2137                                         nstate = IEEE80211_S_RUN;
 2138 #ifdef IEEE80211_DEBUG
 2139                                 if (nstate != IEEE80211_S_SCAN) {
 2140                                         IEEE80211_DPRINTF(vap,
 2141                                             IEEE80211_MSG_STATE,
 2142                                             "%s: override, now %s -> %s\n",
 2143                                             __func__,
 2144                                             ieee80211_state_name[ostate],
 2145                                             ieee80211_state_name[nstate]);
 2146                                 }
 2147 #endif
 2148                         }
 2149                 }
 2150                 break;
 2151         case IEEE80211_S_RUN:
 2152                 if (vap->iv_opmode == IEEE80211_M_WDS &&
 2153                     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
 2154                     nscanning) {
 2155                         /*
 2156                          * Legacy WDS with someone else scanning; don't
 2157                          * go online until that completes as we should
 2158                          * follow the other vap to the channel they choose.
 2159                          */
 2160                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2161                              "%s: defer %s -> %s (legacy WDS)\n", __func__,
 2162                              ieee80211_state_name[ostate],
 2163                              ieee80211_state_name[nstate]);
 2164                         vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
 2165                         return 0;
 2166                 }
 2167                 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 2168                     IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
 2169                     (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
 2170                     !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
 2171                         /*
 2172                          * This is a DFS channel, transition to CAC state
 2173                          * instead of RUN.  This allows us to initiate
 2174                          * Channel Availability Check (CAC) as specified
 2175                          * by 11h/DFS.
 2176                          */
 2177                         nstate = IEEE80211_S_CAC;
 2178                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
 2179                              "%s: override %s -> %s (DFS)\n", __func__,
 2180                              ieee80211_state_name[ostate],
 2181                              ieee80211_state_name[nstate]);
 2182                 }
 2183                 break;
 2184         case IEEE80211_S_INIT:
 2185                 /* cancel any scan in progress */
 2186                 ieee80211_cancel_scan(vap);
 2187                 if (ostate == IEEE80211_S_INIT ) {
 2188                         /* XXX don't believe this */
 2189                         /* INIT -> INIT. nothing to do */
 2190                         vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
 2191                 }
 2192                 /* fall thru... */
 2193         default:
 2194                 break;
 2195         }
 2196         /* defer the state change to a thread */
 2197         vap->iv_nstate = nstate;
 2198         vap->iv_nstate_arg = arg;
 2199         vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
 2200         ieee80211_runtask(ic, &vap->iv_nstate_task);
 2201         return EINPROGRESS;
 2202 }
 2203 
 2204 int
 2205 ieee80211_new_state(struct ieee80211vap *vap,
 2206         enum ieee80211_state nstate, int arg)
 2207 {
 2208         struct ieee80211com *ic = vap->iv_ic;
 2209         int rc;
 2210 
 2211         IEEE80211_LOCK(ic);
 2212         rc = ieee80211_new_state_locked(vap, nstate, arg);
 2213         IEEE80211_UNLOCK(ic);
 2214         return rc;
 2215 }

Cache object: 171cd04f15306f74e9c1ee66c2e4204b


[ 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.