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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6dabdf97640c61dff236eb2f7b914794


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