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 /*
  109  * Reason code descriptions were (mostly) obtained from
  110  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
  111  */
  112 const char *
  113 ieee80211_reason_to_string(uint16_t reason)
  114 {
  115         switch (reason) {
  116         case IEEE80211_REASON_UNSPECIFIED:
  117                 return ("unspecified");
  118         case IEEE80211_REASON_AUTH_EXPIRE:
  119                 return ("previous authentication is expired");
  120         case IEEE80211_REASON_AUTH_LEAVE:
  121                 return ("sending STA is leaving/has left IBSS or ESS");
  122         case IEEE80211_REASON_ASSOC_EXPIRE:
  123                 return ("disassociated due to inactivity");
  124         case IEEE80211_REASON_ASSOC_TOOMANY:
  125                 return ("too many associated STAs");
  126         case IEEE80211_REASON_NOT_AUTHED:
  127                 return ("class 2 frame received from nonauthenticated STA");
  128         case IEEE80211_REASON_NOT_ASSOCED:
  129                 return ("class 3 frame received from nonassociated STA");
  130         case IEEE80211_REASON_ASSOC_LEAVE:
  131                 return ("sending STA is leaving/has left BSS");
  132         case IEEE80211_REASON_ASSOC_NOT_AUTHED:
  133                 return ("STA requesting (re)association is not authenticated");
  134         case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
  135                 return ("information in the Power Capability element is "
  136                         "unacceptable");
  137         case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
  138                 return ("information in the Supported Channels element is "
  139                         "unacceptable");
  140         case IEEE80211_REASON_IE_INVALID:
  141                 return ("invalid element");
  142         case IEEE80211_REASON_MIC_FAILURE:
  143                 return ("MIC failure");
  144         case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
  145                 return ("4-Way handshake timeout");
  146         case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
  147                 return ("group key update timeout");
  148         case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
  149                 return ("element in 4-Way handshake different from "
  150                         "(re)association request/probe response/beacon frame");
  151         case IEEE80211_REASON_GROUP_CIPHER_INVALID:
  152                 return ("invalid group cipher");
  153         case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
  154                 return ("invalid pairwise cipher");
  155         case IEEE80211_REASON_AKMP_INVALID:
  156                 return ("invalid AKMP");
  157         case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
  158                 return ("unsupported version in RSN IE");
  159         case IEEE80211_REASON_INVALID_RSN_IE_CAP:
  160                 return ("invalid capabilities in RSN IE");
  161         case IEEE80211_REASON_802_1X_AUTH_FAILED:
  162                 return ("IEEE 802.1X authentication failed");
  163         case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
  164                 return ("cipher suite rejected because of the security "
  165                         "policy");
  166         case IEEE80211_REASON_UNSPECIFIED_QOS:
  167                 return ("unspecified (QoS-related)");
  168         case IEEE80211_REASON_INSUFFICIENT_BW:
  169                 return ("QoS AP lacks sufficient bandwidth for this QoS STA");
  170         case IEEE80211_REASON_TOOMANY_FRAMES:
  171                 return ("too many frames need to be acknowledged");
  172         case IEEE80211_REASON_OUTSIDE_TXOP:
  173                 return ("STA is transmitting outside the limits of its TXOPs");
  174         case IEEE80211_REASON_LEAVING_QBSS:
  175                 return ("requested from peer STA (the STA is "
  176                         "resetting/leaving the BSS)");
  177         case IEEE80211_REASON_BAD_MECHANISM:
  178                 return ("requested from peer STA (it does not want to use "
  179                         "the mechanism)");
  180         case IEEE80211_REASON_SETUP_NEEDED:
  181                 return ("requested from peer STA (setup is required for the "
  182                         "used mechanism)");
  183         case IEEE80211_REASON_TIMEOUT:
  184                 return ("requested from peer STA (timeout)");
  185         case IEEE80211_REASON_PEER_LINK_CANCELED:
  186                 return ("SME cancels the mesh peering instance (not related "
  187                         "to the maximum number of peer mesh STAs)");
  188         case IEEE80211_REASON_MESH_MAX_PEERS:
  189                 return ("maximum number of peer mesh STAs was reached");
  190         case IEEE80211_REASON_MESH_CPVIOLATION:
  191                 return ("the received information violates the Mesh "
  192                         "Configuration policy configured in the mesh STA "
  193                         "profile");
  194         case IEEE80211_REASON_MESH_CLOSE_RCVD:
  195                 return ("the mesh STA has received a Mesh Peering Close "
  196                         "message requesting to close the mesh peering");
  197         case IEEE80211_REASON_MESH_MAX_RETRIES:
  198                 return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
  199                         "Peering Open messages, without receiving a Mesh "
  200                         "Peering Confirm message");
  201         case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
  202                 return ("the confirmTimer for the mesh peering instance times "
  203                         "out");
  204         case IEEE80211_REASON_MESH_INVALID_GTK:
  205                 return ("the mesh STA fails to unwrap the GTK or the values "
  206                         "in the wrapped contents do not match");
  207         case IEEE80211_REASON_MESH_INCONS_PARAMS:
  208                 return ("the mesh STA receives inconsistent information about "
  209                         "the mesh parameters between Mesh Peering Management "
  210                         "frames");
  211         case IEEE80211_REASON_MESH_INVALID_SECURITY:
  212                 return ("the mesh STA fails the authenticated mesh peering "
  213                         "exchange because due to failure in selecting "
  214                         "pairwise/group ciphersuite");
  215         case IEEE80211_REASON_MESH_PERR_NO_PROXY:
  216                 return ("the mesh STA does not have proxy information for "
  217                         "this external destination");
  218         case IEEE80211_REASON_MESH_PERR_NO_FI:
  219                 return ("the mesh STA does not have forwarding information "
  220                         "for this destination");
  221         case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
  222                 return ("the mesh STA determines that the link to the next "
  223                         "hop of an active path in its forwarding information "
  224                         "is no longer usable");
  225         case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
  226                 return ("the MAC address of the STA already exists in the "
  227                         "mesh BSS");
  228         case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
  229                 return ("the mesh STA performs channel switch to meet "
  230                         "regulatory requirements");
  231         case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
  232                 return ("the mesh STA performs channel switch with "
  233                         "unspecified reason");
  234         default:
  235                 return ("reserved/unknown");
  236         }
  237 }
  238 
  239 static void beacon_miss(void *, int);
  240 static void beacon_swmiss(void *, int);
  241 static void parent_updown(void *, int);
  242 static void update_mcast(void *, int);
  243 static void update_promisc(void *, int);
  244 static void update_channel(void *, int);
  245 static void update_chw(void *, int);
  246 static void vap_update_wme(void *, int);
  247 static void vap_update_slot(void *, int);
  248 static void restart_vaps(void *, int);
  249 static void vap_update_erp_protmode(void *, int);
  250 static void vap_update_preamble(void *, int);
  251 static void vap_update_ht_protmode(void *, int);
  252 static void ieee80211_newstate_cb(void *, int);
  253 
  254 static int
  255 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
  256         const struct ieee80211_bpf_params *params)
  257 {
  258 
  259         ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
  260         m_freem(m);
  261         return ENETDOWN;
  262 }
  263 
  264 void
  265 ieee80211_proto_attach(struct ieee80211com *ic)
  266 {
  267         uint8_t hdrlen;
  268 
  269         /* override the 802.3 setting */
  270         hdrlen = ic->ic_headroom
  271                 + sizeof(struct ieee80211_qosframe_addr4)
  272                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
  273                 + IEEE80211_WEP_EXTIVLEN;
  274         /* XXX no way to recalculate on ifdetach */
  275         if (ALIGN(hdrlen) > max_linkhdr) {
  276                 /* XXX sanity check... */
  277                 max_linkhdr = ALIGN(hdrlen);
  278                 max_hdr = max_linkhdr + max_protohdr;
  279                 max_datalen = MHLEN - max_hdr;
  280         }
  281         //ic->ic_protmode = IEEE80211_PROT_CTSONLY;
  282 
  283         TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
  284         TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
  285         TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
  286         TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
  287         TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
  288         TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
  289         TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
  290 
  291         ic->ic_wme.wme_hipri_switch_hysteresis =
  292                 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
  293 
  294         /* initialize management frame handlers */
  295         ic->ic_send_mgmt = ieee80211_send_mgmt;
  296         ic->ic_raw_xmit = null_raw_xmit;
  297 
  298         ieee80211_adhoc_attach(ic);
  299         ieee80211_sta_attach(ic);
  300         ieee80211_wds_attach(ic);
  301         ieee80211_hostap_attach(ic);
  302 #ifdef IEEE80211_SUPPORT_MESH
  303         ieee80211_mesh_attach(ic);
  304 #endif
  305         ieee80211_monitor_attach(ic);
  306 }
  307 
  308 void
  309 ieee80211_proto_detach(struct ieee80211com *ic)
  310 {
  311         ieee80211_monitor_detach(ic);
  312 #ifdef IEEE80211_SUPPORT_MESH
  313         ieee80211_mesh_detach(ic);
  314 #endif
  315         ieee80211_hostap_detach(ic);
  316         ieee80211_wds_detach(ic);
  317         ieee80211_adhoc_detach(ic);
  318         ieee80211_sta_detach(ic);
  319 }
  320 
  321 static void
  322 null_update_beacon(struct ieee80211vap *vap, int item)
  323 {
  324 }
  325 
  326 void
  327 ieee80211_proto_vattach(struct ieee80211vap *vap)
  328 {
  329         struct ieee80211com *ic = vap->iv_ic;
  330         struct ifnet *ifp = vap->iv_ifp;
  331         int i;
  332 
  333         /* override the 802.3 setting */
  334         ifp->if_hdrlen = ic->ic_headroom
  335                 + sizeof(struct ieee80211_qosframe_addr4)
  336                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
  337                 + IEEE80211_WEP_EXTIVLEN;
  338 
  339         vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
  340         vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
  341         vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
  342         callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
  343         callout_init(&vap->iv_mgtsend, 1);
  344         TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
  345         TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
  346         TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
  347         TASK_INIT(&vap->iv_slot_task, 0, vap_update_slot, vap);
  348         TASK_INIT(&vap->iv_erp_protmode_task, 0, vap_update_erp_protmode, vap);
  349         TASK_INIT(&vap->iv_ht_protmode_task, 0, vap_update_ht_protmode, vap);
  350         TASK_INIT(&vap->iv_preamble_task, 0, vap_update_preamble, vap);
  351         /*
  352          * Install default tx rate handling: no fixed rate, lowest
  353          * supported rate for mgmt and multicast frames.  Default
  354          * max retry count.  These settings can be changed by the
  355          * driver and/or user applications.
  356          */
  357         for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
  358                 if (isclr(ic->ic_modecaps, i))
  359                         continue;
  360 
  361                 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
  362 
  363                 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
  364 
  365                 /*
  366                  * Setting the management rate to MCS 0 assumes that the
  367                  * BSS Basic rate set is empty and the BSS Basic MCS set
  368                  * is not.
  369                  *
  370                  * Since we're not checking this, default to the lowest
  371                  * defined rate for this mode.
  372                  *
  373                  * At least one 11n AP (DLINK DIR-825) is reported to drop
  374                  * some MCS management traffic (eg BA response frames.)
  375                  *
  376                  * See also: 9.6.0 of the 802.11n-2009 specification.
  377                  */
  378 #ifdef  NOTYET
  379                 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
  380                         vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
  381                         vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
  382                 } else {
  383                         vap->iv_txparms[i].mgmtrate =
  384                             rs->rs_rates[0] & IEEE80211_RATE_VAL;
  385                         vap->iv_txparms[i].mcastrate = 
  386                             rs->rs_rates[0] & IEEE80211_RATE_VAL;
  387                 }
  388 #endif
  389                 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
  390                 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
  391                 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
  392         }
  393         vap->iv_roaming = IEEE80211_ROAMING_AUTO;
  394 
  395         vap->iv_update_beacon = null_update_beacon;
  396         vap->iv_deliver_data = ieee80211_deliver_data;
  397         vap->iv_protmode = IEEE80211_PROT_CTSONLY;
  398 
  399         /* attach support for operating mode */
  400         ic->ic_vattach[vap->iv_opmode](vap);
  401 }
  402 
  403 void
  404 ieee80211_proto_vdetach(struct ieee80211vap *vap)
  405 {
  406 #define FREEAPPIE(ie) do { \
  407         if (ie != NULL) \
  408                 IEEE80211_FREE(ie, M_80211_NODE_IE); \
  409 } while (0)
  410         /*
  411          * Detach operating mode module.
  412          */
  413         if (vap->iv_opdetach != NULL)
  414                 vap->iv_opdetach(vap);
  415         /*
  416          * This should not be needed as we detach when reseting
  417          * the state but be conservative here since the
  418          * authenticator may do things like spawn kernel threads.
  419          */
  420         if (vap->iv_auth->ia_detach != NULL)
  421                 vap->iv_auth->ia_detach(vap);
  422         /*
  423          * Detach any ACL'ator.
  424          */
  425         if (vap->iv_acl != NULL)
  426                 vap->iv_acl->iac_detach(vap);
  427 
  428         FREEAPPIE(vap->iv_appie_beacon);
  429         FREEAPPIE(vap->iv_appie_probereq);
  430         FREEAPPIE(vap->iv_appie_proberesp);
  431         FREEAPPIE(vap->iv_appie_assocreq);
  432         FREEAPPIE(vap->iv_appie_assocresp);
  433         FREEAPPIE(vap->iv_appie_wpa);
  434 #undef FREEAPPIE
  435 }
  436 
  437 /*
  438  * Simple-minded authenticator module support.
  439  */
  440 
  441 #define IEEE80211_AUTH_MAX      (IEEE80211_AUTH_WPA+1)
  442 /* XXX well-known names */
  443 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
  444         "wlan_internal",        /* IEEE80211_AUTH_NONE */
  445         "wlan_internal",        /* IEEE80211_AUTH_OPEN */
  446         "wlan_internal",        /* IEEE80211_AUTH_SHARED */
  447         "wlan_xauth",           /* IEEE80211_AUTH_8021X  */
  448         "wlan_internal",        /* IEEE80211_AUTH_AUTO */
  449         "wlan_xauth",           /* IEEE80211_AUTH_WPA */
  450 };
  451 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
  452 
  453 static const struct ieee80211_authenticator auth_internal = {
  454         .ia_name                = "wlan_internal",
  455         .ia_attach              = NULL,
  456         .ia_detach              = NULL,
  457         .ia_node_join           = NULL,
  458         .ia_node_leave          = NULL,
  459 };
  460 
  461 /*
  462  * Setup internal authenticators once; they are never unregistered.
  463  */
  464 static void
  465 ieee80211_auth_setup(void)
  466 {
  467         ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
  468         ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
  469         ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
  470 }
  471 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
  472 
  473 const struct ieee80211_authenticator *
  474 ieee80211_authenticator_get(int auth)
  475 {
  476         if (auth >= IEEE80211_AUTH_MAX)
  477                 return NULL;
  478         if (authenticators[auth] == NULL)
  479                 ieee80211_load_module(auth_modnames[auth]);
  480         return authenticators[auth];
  481 }
  482 
  483 void
  484 ieee80211_authenticator_register(int type,
  485         const struct ieee80211_authenticator *auth)
  486 {
  487         if (type >= IEEE80211_AUTH_MAX)
  488                 return;
  489         authenticators[type] = auth;
  490 }
  491 
  492 void
  493 ieee80211_authenticator_unregister(int type)
  494 {
  495 
  496         if (type >= IEEE80211_AUTH_MAX)
  497                 return;
  498         authenticators[type] = NULL;
  499 }
  500 
  501 /*
  502  * Very simple-minded ACL module support.
  503  */
  504 /* XXX just one for now */
  505 static  const struct ieee80211_aclator *acl = NULL;
  506 
  507 void
  508 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
  509 {
  510         printf("wlan: %s acl policy registered\n", iac->iac_name);
  511         acl = iac;
  512 }
  513 
  514 void
  515 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
  516 {
  517         if (acl == iac)
  518                 acl = NULL;
  519         printf("wlan: %s acl policy unregistered\n", iac->iac_name);
  520 }
  521 
  522 const struct ieee80211_aclator *
  523 ieee80211_aclator_get(const char *name)
  524 {
  525         if (acl == NULL)
  526                 ieee80211_load_module("wlan_acl");
  527         return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
  528 }
  529 
  530 void
  531 ieee80211_print_essid(const uint8_t *essid, int len)
  532 {
  533         const uint8_t *p;
  534         int i;
  535 
  536         if (len > IEEE80211_NWID_LEN)
  537                 len = IEEE80211_NWID_LEN;
  538         /* determine printable or not */
  539         for (i = 0, p = essid; i < len; i++, p++) {
  540                 if (*p < ' ' || *p > 0x7e)
  541                         break;
  542         }
  543         if (i == len) {
  544                 printf("\"");
  545                 for (i = 0, p = essid; i < len; i++, p++)
  546                         printf("%c", *p);
  547                 printf("\"");
  548         } else {
  549                 printf("0x");
  550                 for (i = 0, p = essid; i < len; i++, p++)
  551                         printf("%02x", *p);
  552         }
  553 }
  554 
  555 void
  556 ieee80211_dump_pkt(struct ieee80211com *ic,
  557         const uint8_t *buf, int len, int rate, int rssi)
  558 {
  559         const struct ieee80211_frame *wh;
  560         int i;
  561 
  562         wh = (const struct ieee80211_frame *)buf;
  563         switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  564         case IEEE80211_FC1_DIR_NODS:
  565                 printf("NODS %s", ether_sprintf(wh->i_addr2));
  566                 printf("->%s", ether_sprintf(wh->i_addr1));
  567                 printf("(%s)", ether_sprintf(wh->i_addr3));
  568                 break;
  569         case IEEE80211_FC1_DIR_TODS:
  570                 printf("TODS %s", ether_sprintf(wh->i_addr2));
  571                 printf("->%s", ether_sprintf(wh->i_addr3));
  572                 printf("(%s)", ether_sprintf(wh->i_addr1));
  573                 break;
  574         case IEEE80211_FC1_DIR_FROMDS:
  575                 printf("FRDS %s", ether_sprintf(wh->i_addr3));
  576                 printf("->%s", ether_sprintf(wh->i_addr1));
  577                 printf("(%s)", ether_sprintf(wh->i_addr2));
  578                 break;
  579         case IEEE80211_FC1_DIR_DSTODS:
  580                 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
  581                 printf("->%s", ether_sprintf(wh->i_addr3));
  582                 printf("(%s", ether_sprintf(wh->i_addr2));
  583                 printf("->%s)", ether_sprintf(wh->i_addr1));
  584                 break;
  585         }
  586         switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
  587         case IEEE80211_FC0_TYPE_DATA:
  588                 printf(" data");
  589                 break;
  590         case IEEE80211_FC0_TYPE_MGT:
  591                 printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
  592                 break;
  593         default:
  594                 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
  595                 break;
  596         }
  597         if (IEEE80211_QOS_HAS_SEQ(wh)) {
  598                 const struct ieee80211_qosframe *qwh = 
  599                         (const struct ieee80211_qosframe *)buf;
  600                 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
  601                         qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
  602         }
  603         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
  604                 int off;
  605 
  606                 off = ieee80211_anyhdrspace(ic, wh);
  607                 printf(" WEP [IV %.02x %.02x %.02x",
  608                         buf[off+0], buf[off+1], buf[off+2]);
  609                 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
  610                         printf(" %.02x %.02x %.02x",
  611                                 buf[off+4], buf[off+5], buf[off+6]);
  612                 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
  613         }
  614         if (rate >= 0)
  615                 printf(" %dM", rate / 2);
  616         if (rssi >= 0)
  617                 printf(" +%d", rssi);
  618         printf("\n");
  619         if (len > 0) {
  620                 for (i = 0; i < len; i++) {
  621                         if ((i & 1) == 0)
  622                                 printf(" ");
  623                         printf("%02x", buf[i]);
  624                 }
  625                 printf("\n");
  626         }
  627 }
  628 
  629 static __inline int
  630 findrix(const struct ieee80211_rateset *rs, int r)
  631 {
  632         int i;
  633 
  634         for (i = 0; i < rs->rs_nrates; i++)
  635                 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
  636                         return i;
  637         return -1;
  638 }
  639 
  640 int
  641 ieee80211_fix_rate(struct ieee80211_node *ni,
  642         struct ieee80211_rateset *nrs, int flags)
  643 {
  644         struct ieee80211vap *vap = ni->ni_vap;
  645         struct ieee80211com *ic = ni->ni_ic;
  646         int i, j, rix, error;
  647         int okrate, badrate, fixedrate, ucastrate;
  648         const struct ieee80211_rateset *srs;
  649         uint8_t r;
  650 
  651         error = 0;
  652         okrate = badrate = 0;
  653         ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
  654         if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
  655                 /*
  656                  * Workaround awkwardness with fixed rate.  We are called
  657                  * to check both the legacy rate set and the HT rate set
  658                  * but we must apply any legacy fixed rate check only to the
  659                  * legacy rate set and vice versa.  We cannot tell what type
  660                  * of rate set we've been given (legacy or HT) but we can
  661                  * distinguish the fixed rate type (MCS have 0x80 set).
  662                  * So to deal with this the caller communicates whether to
  663                  * check MCS or legacy rate using the flags and we use the
  664                  * type of any fixed rate to avoid applying an MCS to a
  665                  * legacy rate and vice versa.
  666                  */
  667                 if (ucastrate & 0x80) {
  668                         if (flags & IEEE80211_F_DOFRATE)
  669                                 flags &= ~IEEE80211_F_DOFRATE;
  670                 } else if ((ucastrate & 0x80) == 0) {
  671                         if (flags & IEEE80211_F_DOFMCS)
  672                                 flags &= ~IEEE80211_F_DOFMCS;
  673                 }
  674                 /* NB: required to make MCS match below work */
  675                 ucastrate &= IEEE80211_RATE_VAL;
  676         }
  677         fixedrate = IEEE80211_FIXED_RATE_NONE;
  678         /*
  679          * XXX we are called to process both MCS and legacy rates;
  680          * we must use the appropriate basic rate set or chaos will
  681          * ensue; for now callers that want MCS must supply
  682          * IEEE80211_F_DOBRS; at some point we'll need to split this
  683          * function so there are two variants, one for MCS and one
  684          * for legacy rates.
  685          */
  686         if (flags & IEEE80211_F_DOBRS)
  687                 srs = (const struct ieee80211_rateset *)
  688                     ieee80211_get_suphtrates(ic, ni->ni_chan);
  689         else
  690                 srs = ieee80211_get_suprates(ic, ni->ni_chan);
  691         for (i = 0; i < nrs->rs_nrates; ) {
  692                 if (flags & IEEE80211_F_DOSORT) {
  693                         /*
  694                          * Sort rates.
  695                          */
  696                         for (j = i + 1; j < nrs->rs_nrates; j++) {
  697                                 if (IEEE80211_RV(nrs->rs_rates[i]) >
  698                                     IEEE80211_RV(nrs->rs_rates[j])) {
  699                                         r = nrs->rs_rates[i];
  700                                         nrs->rs_rates[i] = nrs->rs_rates[j];
  701                                         nrs->rs_rates[j] = r;
  702                                 }
  703                         }
  704                 }
  705                 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
  706                 badrate = r;
  707                 /*
  708                  * Check for fixed rate.
  709                  */
  710                 if (r == ucastrate)
  711                         fixedrate = r;
  712                 /*
  713                  * Check against supported rates.
  714                  */
  715                 rix = findrix(srs, r);
  716                 if (flags & IEEE80211_F_DONEGO) {
  717                         if (rix < 0) {
  718                                 /*
  719                                  * A rate in the node's rate set is not
  720                                  * supported.  If this is a basic rate and we
  721                                  * are operating as a STA then this is an error.
  722                                  * Otherwise we just discard/ignore the rate.
  723                                  */
  724                                 if ((flags & IEEE80211_F_JOIN) &&
  725                                     (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
  726                                         error++;
  727                         } else if ((flags & IEEE80211_F_JOIN) == 0) {
  728                                 /*
  729                                  * Overwrite with the supported rate
  730                                  * value so any basic rate bit is set.
  731                                  */
  732                                 nrs->rs_rates[i] = srs->rs_rates[rix];
  733                         }
  734                 }
  735                 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
  736                         /*
  737                          * Delete unacceptable rates.
  738                          */
  739                         nrs->rs_nrates--;
  740                         for (j = i; j < nrs->rs_nrates; j++)
  741                                 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
  742                         nrs->rs_rates[j] = 0;
  743                         continue;
  744                 }
  745                 if (rix >= 0)
  746                         okrate = nrs->rs_rates[i];
  747                 i++;
  748         }
  749         if (okrate == 0 || error != 0 ||
  750             ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
  751              fixedrate != ucastrate)) {
  752                 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
  753                     "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
  754                     "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
  755                 return badrate | IEEE80211_RATE_BASIC;
  756         } else
  757                 return IEEE80211_RV(okrate);
  758 }
  759 
  760 /*
  761  * Reset 11g-related state.
  762  *
  763  * This is for per-VAP ERP/11g state.
  764  *
  765  * Eventually everything in ieee80211_reset_erp() will be
  766  * per-VAP and in here.
  767  */
  768 void
  769 ieee80211_vap_reset_erp(struct ieee80211vap *vap)
  770 {
  771         struct ieee80211com *ic = vap->iv_ic;
  772 
  773         vap->iv_nonerpsta = 0;
  774         vap->iv_longslotsta = 0;
  775 
  776         vap->iv_flags &= ~IEEE80211_F_USEPROT;
  777         /*
  778          * Set short preamble and ERP barker-preamble flags.
  779          */
  780         if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
  781             (vap->iv_caps & IEEE80211_C_SHPREAMBLE)) {
  782                 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
  783                 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
  784         } else {
  785                 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
  786                 vap->iv_flags |= IEEE80211_F_USEBARKER;
  787         }
  788 
  789         /*
  790          * Short slot time is enabled only when operating in 11g
  791          * and not in an IBSS.  We must also honor whether or not
  792          * the driver is capable of doing it.
  793          */
  794         ieee80211_vap_set_shortslottime(vap,
  795                 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
  796                 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
  797                 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
  798                 vap->iv_opmode == IEEE80211_M_HOSTAP &&
  799                 (ic->ic_caps & IEEE80211_C_SHSLOT)));
  800 }
  801 
  802 /*
  803  * Reset 11g-related state.
  804  *
  805  * Note this resets the global state and a caller should schedule
  806  * a re-check of all the VAPs after setup to update said state.
  807  */
  808 void
  809 ieee80211_reset_erp(struct ieee80211com *ic)
  810 {
  811 #if 0
  812         ic->ic_flags &= ~IEEE80211_F_USEPROT;
  813         /*
  814          * Set short preamble and ERP barker-preamble flags.
  815          */
  816         if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
  817             (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
  818                 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
  819                 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
  820         } else {
  821                 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
  822                 ic->ic_flags |= IEEE80211_F_USEBARKER;
  823         }
  824 #endif
  825         /* XXX TODO: schedule a new per-VAP ERP calculation */
  826 }
  827 
  828 /*
  829  * Deferred slot time update.
  830  *
  831  * For per-VAP slot time configuration, call the VAP
  832  * method if the VAP requires it.  Otherwise, just call the
  833  * older global method.
  834  *
  835  * If the per-VAP method is called then it's expected that
  836  * the driver/firmware will take care of turning the per-VAP
  837  * flags into slot time configuration.
  838  *
  839  * If the per-VAP method is not called then the global flags will be
  840  * flipped into sync with the VAPs; ic_flags IEEE80211_F_SHSLOT will
  841  * be set only if all of the vaps will have it set.
  842  *
  843  * Look at the comments for vap_update_erp_protmode() for more
  844  * background; this assumes all VAPs are on the same channel.
  845  */
  846 static void
  847 vap_update_slot(void *arg, int npending)
  848 {
  849         struct ieee80211vap *vap = arg;
  850         struct ieee80211com *ic = vap->iv_ic;
  851         struct ieee80211vap *iv;
  852         int num_shslot = 0, num_lgslot = 0;
  853 
  854         /*
  855          * Per-VAP path - we've already had the flags updated;
  856          * so just notify the driver and move on.
  857          */
  858         if (vap->iv_updateslot != NULL) {
  859                 vap->iv_updateslot(vap);
  860                 return;
  861         }
  862 
  863         /*
  864          * Iterate over all of the VAP flags to update the
  865          * global flag.
  866          *
  867          * If all vaps have short slot enabled then flip on
  868          * short slot.  If any vap has it disabled then
  869          * we leave it globally disabled.  This should provide
  870          * correct behaviour in a multi-BSS scenario where
  871          * at least one VAP has short slot disabled for some
  872          * reason.
  873          */
  874         IEEE80211_LOCK(ic);
  875         TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
  876                 if (iv->iv_flags & IEEE80211_F_SHSLOT)
  877                         num_shslot++;
  878                 else
  879                         num_lgslot++;
  880         }
  881 
  882         /*
  883          * It looks backwards but - if the number of short slot VAPs
  884          * is zero then we're not short slot.  Else, we have one
  885          * or more short slot VAPs and we're checking to see if ANY
  886          * of them have short slot disabled.
  887          */
  888         if (num_shslot == 0)
  889                 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
  890         else if (num_lgslot == 0)
  891                 ic->ic_flags |= IEEE80211_F_SHSLOT;
  892         IEEE80211_UNLOCK(ic);
  893 
  894         /*
  895          * Call the driver with our new global slot time flags.
  896          */
  897         if (ic->ic_updateslot != NULL)
  898                 ic->ic_updateslot(ic);
  899 }
  900 
  901 /*
  902  * Deferred ERP protmode update.
  903  *
  904  * This currently calculates the global ERP protection mode flag
  905  * based on each of the VAPs.  Any VAP with it enabled is enough
  906  * for the global flag to be enabled.  All VAPs with it disabled
  907  * is enough for it to be disabled.
  908  *
  909  * This may make sense right now for the supported hardware where
  910  * net80211 is controlling the single channel configuration, but
  911  * offload firmware that's doing channel changes (eg off-channel
  912  * TDLS, off-channel STA, off-channel P2P STA/AP) may get some
  913  * silly looking flag updates.
  914  *
  915  * Ideally the protection mode calculation is done based on the
  916  * channel, and all VAPs using that channel will inherit it.
  917  * But until that's what net80211 does, this wil have to do.
  918  */
  919 static void
  920 vap_update_erp_protmode(void *arg, int npending)
  921 {
  922         struct ieee80211vap *vap = arg;
  923         struct ieee80211com *ic = vap->iv_ic;
  924         struct ieee80211vap *iv;
  925         int enable_protmode = 0;
  926         int non_erp_present = 0;
  927 
  928         /*
  929          * Iterate over all of the VAPs to calculate the overlapping
  930          * ERP protection mode configuration and ERP present math.
  931          *
  932          * For now we assume that if a driver can handle this per-VAP
  933          * then it'll ignore the ic->ic_protmode variant and instead
  934          * will look at the vap related flags.
  935          */
  936         IEEE80211_LOCK(ic);
  937         TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
  938                 if (iv->iv_flags & IEEE80211_F_USEPROT)
  939                         enable_protmode = 1;
  940                 if (iv->iv_flags_ext & IEEE80211_FEXT_NONERP_PR)
  941                         non_erp_present = 1;
  942         }
  943 
  944         if (enable_protmode)
  945                 ic->ic_flags |= IEEE80211_F_USEPROT;
  946         else
  947                 ic->ic_flags &= ~IEEE80211_F_USEPROT;
  948 
  949         if (non_erp_present)
  950                 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
  951         else
  952                 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
  953 
  954         /* Beacon update on all VAPs */
  955         ieee80211_notify_erp_locked(ic);
  956 
  957         IEEE80211_UNLOCK(ic);
  958 
  959         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
  960             "%s: called; enable_protmode=%d, non_erp_present=%d\n",
  961             __func__, enable_protmode, non_erp_present);
  962 
  963         /*
  964          * Now that the global configuration flags are calculated,
  965          * notify the VAP about its configuration.
  966          *
  967          * The global flags will be used when assembling ERP IEs
  968          * for multi-VAP operation, even if it's on a different
  969          * channel.  Yes, that's going to need fixing in the
  970          * future.
  971          */
  972         if (vap->iv_erp_protmode_update != NULL)
  973                 vap->iv_erp_protmode_update(vap);
  974 }
  975 
  976 /*
  977  * Deferred ERP short preamble/barker update.
  978  *
  979  * All VAPs need to use short preamble for it to be globally
  980  * enabled or not.
  981  *
  982  * Look at the comments for vap_update_erp_protmode() for more
  983  * background; this assumes all VAPs are on the same channel.
  984  */
  985 static void
  986 vap_update_preamble(void *arg, int npending)
  987 {
  988         struct ieee80211vap *vap = arg;
  989         struct ieee80211com *ic = vap->iv_ic;
  990         struct ieee80211vap *iv;
  991         int barker_count = 0, short_preamble_count = 0, count = 0;
  992 
  993         /*
  994          * Iterate over all of the VAPs to calculate the overlapping
  995          * short or long preamble configuration.
  996          *
  997          * For now we assume that if a driver can handle this per-VAP
  998          * then it'll ignore the ic->ic_flags variant and instead
  999          * will look at the vap related flags.
 1000          */
 1001         IEEE80211_LOCK(ic);
 1002         TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
 1003                 if (iv->iv_flags & IEEE80211_F_USEBARKER)
 1004                         barker_count++;
 1005                 if (iv->iv_flags & IEEE80211_F_SHPREAMBLE)
 1006                         short_preamble_count++;
 1007                 count++;
 1008         }
 1009 
 1010         /*
 1011          * As with vap_update_erp_protmode(), the global flags are
 1012          * currently used for beacon IEs.
 1013          */
 1014         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1015             "%s: called; barker_count=%d, short_preamble_count=%d\n",
 1016             __func__, barker_count, short_preamble_count);
 1017 
 1018         /*
 1019          * Only flip on short preamble if all of the VAPs support
 1020          * it.
 1021          */
 1022         if (barker_count == 0 && short_preamble_count == count) {
 1023                 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
 1024                 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
 1025         } else {
 1026                 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
 1027                 ic->ic_flags |= IEEE80211_F_USEBARKER;
 1028         }
 1029         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1030           "%s: global barker=%d preamble=%d\n",
 1031           __func__,
 1032           !! (ic->ic_flags & IEEE80211_F_USEBARKER),
 1033           !! (ic->ic_flags & IEEE80211_F_SHPREAMBLE));
 1034 
 1035         /* Beacon update on all VAPs */
 1036         ieee80211_notify_erp_locked(ic);
 1037 
 1038         IEEE80211_UNLOCK(ic);
 1039 
 1040         /* Driver notification */
 1041         if (vap->iv_erp_protmode_update != NULL)
 1042                 vap->iv_preamble_update(vap);
 1043 }
 1044 
 1045 /*
 1046  * Deferred HT protmode update and beacon update.
 1047  *
 1048  * Look at the comments for vap_update_erp_protmode() for more
 1049  * background; this assumes all VAPs are on the same channel.
 1050  */
 1051 static void
 1052 vap_update_ht_protmode(void *arg, int npending)
 1053 {
 1054         struct ieee80211vap *vap = arg;
 1055         struct ieee80211vap *iv;
 1056         struct ieee80211com *ic = vap->iv_ic;
 1057         int num_vaps = 0, num_pure = 0;
 1058         int num_optional = 0, num_ht2040 = 0, num_nonht = 0;
 1059         int num_ht_sta = 0, num_ht40_sta = 0, num_sta = 0;
 1060         int num_nonhtpr = 0;
 1061 
 1062         /*
 1063          * Iterate over all of the VAPs to calculate everything.
 1064          *
 1065          * There are a few different flags to calculate:
 1066          *
 1067          * + whether there's HT only or HT+legacy stations;
 1068          * + whether there's HT20, HT40, or HT20+HT40 stations;
 1069          * + whether the desired protection mode is mixed, pure or
 1070          *   one of the two above.
 1071          *
 1072          * For now we assume that if a driver can handle this per-VAP
 1073          * then it'll ignore the ic->ic_htprotmode / ic->ic_curhtprotmode
 1074          * variant and instead will look at the vap related variables.
 1075          *
 1076          * XXX TODO: non-greenfield STAs present (IEEE80211_HTINFO_NONGF_PRESENT) !
 1077          */
 1078 
 1079         IEEE80211_LOCK(ic);
 1080         TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
 1081                 num_vaps++;
 1082                 /* overlapping BSSes advertising non-HT status present */
 1083                 if (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR)
 1084                         num_nonht++;
 1085                 /* Operating mode flags */
 1086                 if (iv->iv_curhtprotmode & IEEE80211_HTINFO_NONHT_PRESENT)
 1087                         num_nonhtpr++;
 1088                 switch (iv->iv_curhtprotmode & IEEE80211_HTINFO_OPMODE) {
 1089                 case IEEE80211_HTINFO_OPMODE_PURE:
 1090                         num_pure++;
 1091                         break;
 1092                 case IEEE80211_HTINFO_OPMODE_PROTOPT:
 1093                         num_optional++;
 1094                         break;
 1095                 case IEEE80211_HTINFO_OPMODE_HT20PR:
 1096                         num_ht2040++;
 1097                         break;
 1098                 }
 1099 
 1100                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
 1101                     "%s: vap %s: nonht_pr=%d, curhtprotmode=0x%02x\n",
 1102                     __func__,
 1103                     ieee80211_get_vap_ifname(iv),
 1104                     !! (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR),
 1105                     iv->iv_curhtprotmode);
 1106 
 1107                 num_ht_sta += iv->iv_ht_sta_assoc;
 1108                 num_ht40_sta += iv->iv_ht40_sta_assoc;
 1109                 num_sta += iv->iv_sta_assoc;
 1110         }
 1111 
 1112         /*
 1113          * Step 1 - if any VAPs indicate NONHT_PR set (overlapping BSS
 1114          * non-HT present), set it here.  This shouldn't be used by
 1115          * anything but the old overlapping BSS logic so if any drivers
 1116          * consume it, it's up to date.
 1117          */
 1118         if (num_nonht > 0)
 1119                 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
 1120         else
 1121                 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
 1122 
 1123         /*
 1124          * Step 2 - default HT protection mode to MIXED (802.11-2016 10.26.3.1.)
 1125          *
 1126          * + If all VAPs are PURE, we can stay PURE.
 1127          * + If all VAPs are PROTOPT, we can go to PROTOPT.
 1128          * + If any VAP has HT20PR then it sees at least a HT40+HT20 station.
 1129          *   Note that we may have a VAP with one HT20 and a VAP with one HT40;
 1130          *   So we look at the sum ht and sum ht40 sta counts; if we have a
 1131          *   HT station and the HT20 != HT40 count, we have to do HT20PR here.
 1132          *   Note all stations need to be HT for this to be an option.
 1133          * + The fall-through is MIXED, because it means we have some odd
 1134          *   non HT40-involved combination of opmode and this is the most
 1135          *   sensible default.
 1136          */
 1137         ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
 1138 
 1139         if (num_pure == num_vaps)
 1140                 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
 1141 
 1142         if (num_optional == num_vaps)
 1143                 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PROTOPT;
 1144 
 1145         /*
 1146          * Note: we need /a/ HT40 station somewhere for this to
 1147          * be a possibility.
 1148          */
 1149         if ((num_ht2040 > 0) ||
 1150             ((num_ht_sta > 0) && (num_ht40_sta > 0) &&
 1151              (num_ht_sta != num_ht40_sta)))
 1152                 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_HT20PR;
 1153 
 1154         /*
 1155          * Step 3 - if any of the stations across the VAPs are
 1156          * non-HT then this needs to be flipped back to MIXED.
 1157          */
 1158         if (num_ht_sta != num_sta)
 1159                 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
 1160 
 1161         /*
 1162          * Step 4 - If we see any overlapping BSS non-HT stations
 1163          * via beacons then flip on NONHT_PRESENT.
 1164          */
 1165         if (num_nonhtpr > 0)
 1166                 ic->ic_curhtprotmode |= IEEE80211_HTINFO_NONHT_PRESENT;
 1167 
 1168         /* Notify all VAPs to potentially update their beacons */
 1169         TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next)
 1170                 ieee80211_htinfo_notify(iv);
 1171 
 1172         IEEE80211_UNLOCK(ic);
 1173 
 1174         IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
 1175           "%s: global: nonht_pr=%d ht_opmode=0x%02x\n",
 1176           __func__,
 1177           !! (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR),
 1178           ic->ic_curhtprotmode);
 1179 
 1180         /* Driver update */
 1181         if (vap->iv_erp_protmode_update != NULL)
 1182                 vap->iv_ht_protmode_update(vap);
 1183 }
 1184 
 1185 /*
 1186  * Set the short slot time state and notify the driver.
 1187  *
 1188  * This is the per-VAP slot time state.
 1189  */
 1190 void
 1191 ieee80211_vap_set_shortslottime(struct ieee80211vap *vap, int onoff)
 1192 {
 1193         struct ieee80211com *ic = vap->iv_ic;
 1194 
 1195         /* XXX lock? */
 1196 
 1197         /*
 1198          * Only modify the per-VAP slot time.
 1199          */
 1200         if (onoff)
 1201                 vap->iv_flags |= IEEE80211_F_SHSLOT;
 1202         else
 1203                 vap->iv_flags &= ~IEEE80211_F_SHSLOT;
 1204 
 1205         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1206             "%s: called; onoff=%d\n", __func__, onoff);
 1207         /* schedule the deferred slot flag update and update */
 1208         ieee80211_runtask(ic, &vap->iv_slot_task);
 1209 }
 1210 
 1211 /*
 1212  * Update the VAP short /long / barker preamble state and
 1213  * update beacon state if needed.
 1214  *
 1215  * For now it simply copies the global flags into the per-vap
 1216  * flags and schedules the callback.  Later this will support
 1217  * both global and per-VAP flags, especially useful for
 1218  * and STA+STA multi-channel operation (eg p2p).
 1219  */
 1220 void
 1221 ieee80211_vap_update_preamble(struct ieee80211vap *vap)
 1222 {
 1223         struct ieee80211com *ic = vap->iv_ic;
 1224 
 1225         /* XXX lock? */
 1226 
 1227         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1228             "%s: called\n", __func__);
 1229         /* schedule the deferred slot flag update and update */
 1230         ieee80211_runtask(ic, &vap->iv_preamble_task);
 1231 }
 1232 
 1233 /*
 1234  * Update the VAP 11g protection mode and update beacon state
 1235  * if needed.
 1236  */
 1237 void
 1238 ieee80211_vap_update_erp_protmode(struct ieee80211vap *vap)
 1239 {
 1240         struct ieee80211com *ic = vap->iv_ic;
 1241 
 1242         /* XXX lock? */
 1243 
 1244         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1245             "%s: called\n", __func__);
 1246         /* schedule the deferred slot flag update and update */
 1247         ieee80211_runtask(ic, &vap->iv_erp_protmode_task);
 1248 }
 1249 
 1250 /*
 1251  * Update the VAP 11n protection mode and update beacon state
 1252  * if needed.
 1253  */
 1254 void
 1255 ieee80211_vap_update_ht_protmode(struct ieee80211vap *vap)
 1256 {
 1257         struct ieee80211com *ic = vap->iv_ic;
 1258 
 1259         /* XXX lock? */
 1260 
 1261         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
 1262             "%s: called\n", __func__);
 1263         /* schedule the deferred protmode update */
 1264         ieee80211_runtask(ic, &vap->iv_ht_protmode_task);
 1265 }
 1266 
 1267 /*
 1268  * Check if the specified rate set supports ERP.
 1269  * NB: the rate set is assumed to be sorted.
 1270  */
 1271 int
 1272 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
 1273 {
 1274         static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
 1275         int i, j;
 1276 
 1277         if (rs->rs_nrates < nitems(rates))
 1278                 return 0;
 1279         for (i = 0; i < nitems(rates); i++) {
 1280                 for (j = 0; j < rs->rs_nrates; j++) {
 1281                         int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
 1282                         if (rates[i] == r)
 1283                                 goto next;
 1284                         if (r > rates[i])
 1285                                 return 0;
 1286                 }
 1287                 return 0;
 1288         next:
 1289                 ;
 1290         }
 1291         return 1;
 1292 }
 1293 
 1294 /*
 1295  * Mark the basic rates for the rate table based on the
 1296  * operating mode.  For real 11g we mark all the 11b rates
 1297  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
 1298  * 11b rates.  There's also a pseudo 11a-mode used to mark only
 1299  * the basic OFDM rates.
 1300  */
 1301 static void
 1302 setbasicrates(struct ieee80211_rateset *rs,
 1303     enum ieee80211_phymode mode, int add)
 1304 {
 1305         static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
 1306             [IEEE80211_MODE_11A]        = { 3, { 12, 24, 48 } },
 1307             [IEEE80211_MODE_11B]        = { 2, { 2, 4 } },
 1308                                             /* NB: mixed b/g */
 1309             [IEEE80211_MODE_11G]        = { 4, { 2, 4, 11, 22 } },
 1310             [IEEE80211_MODE_TURBO_A]    = { 3, { 12, 24, 48 } },
 1311             [IEEE80211_MODE_TURBO_G]    = { 4, { 2, 4, 11, 22 } },
 1312             [IEEE80211_MODE_STURBO_A]   = { 3, { 12, 24, 48 } },
 1313             [IEEE80211_MODE_HALF]       = { 3, { 6, 12, 24 } },
 1314             [IEEE80211_MODE_QUARTER]    = { 3, { 3, 6, 12 } },
 1315             [IEEE80211_MODE_11NA]       = { 3, { 12, 24, 48 } },
 1316                                             /* NB: mixed b/g */
 1317             [IEEE80211_MODE_11NG]       = { 4, { 2, 4, 11, 22 } },
 1318                                             /* NB: mixed b/g */
 1319             [IEEE80211_MODE_VHT_2GHZ]   = { 4, { 2, 4, 11, 22 } },
 1320             [IEEE80211_MODE_VHT_5GHZ]   = { 3, { 12, 24, 48 } },
 1321         };
 1322         int i, j;
 1323 
 1324         for (i = 0; i < rs->rs_nrates; i++) {
 1325                 if (!add)
 1326                         rs->rs_rates[i] &= IEEE80211_RATE_VAL;
 1327                 for (j = 0; j < basic[mode].rs_nrates; j++)
 1328                         if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
 1329                                 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
 1330                                 break;
 1331                         }
 1332         }
 1333 }
 1334 
 1335 /*
 1336  * Set the basic rates in a rate set.
 1337  */
 1338 void
 1339 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
 1340     enum ieee80211_phymode mode)
 1341 {
 1342         setbasicrates(rs, mode, 0);
 1343 }
 1344 
 1345 /*
 1346  * Add basic rates to a rate set.
 1347  */
 1348 void
 1349 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
 1350     enum ieee80211_phymode mode)
 1351 {
 1352         setbasicrates(rs, mode, 1);
 1353 }
 1354 
 1355 /*
 1356  * WME protocol support.
 1357  *
 1358  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
 1359  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
 1360  * Draft 2.0 Test Plan (Appendix D).
 1361  *
 1362  * Static/Dynamic Turbo mode settings come from Atheros.
 1363  */
 1364 typedef struct phyParamType {
 1365         uint8_t         aifsn;
 1366         uint8_t         logcwmin;
 1367         uint8_t         logcwmax;
 1368         uint16_t        txopLimit;
 1369         uint8_t         acm;
 1370 } paramType;
 1371 
 1372 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
 1373         [IEEE80211_MODE_AUTO]   = { 3, 4,  6,  0, 0 },
 1374         [IEEE80211_MODE_11A]    = { 3, 4,  6,  0, 0 },
 1375         [IEEE80211_MODE_11B]    = { 3, 4,  6,  0, 0 },
 1376         [IEEE80211_MODE_11G]    = { 3, 4,  6,  0, 0 },
 1377         [IEEE80211_MODE_FH]     = { 3, 4,  6,  0, 0 },
 1378         [IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
 1379         [IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
 1380         [IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
 1381         [IEEE80211_MODE_HALF]   = { 3, 4,  6,  0, 0 },
 1382         [IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
 1383         [IEEE80211_MODE_11NA]   = { 3, 4,  6,  0, 0 },
 1384         [IEEE80211_MODE_11NG]   = { 3, 4,  6,  0, 0 },
 1385         [IEEE80211_MODE_VHT_2GHZ]       = { 3, 4,  6,  0, 0 },
 1386         [IEEE80211_MODE_VHT_5GHZ]       = { 3, 4,  6,  0, 0 },
 1387 };
 1388 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
 1389         [IEEE80211_MODE_AUTO]   = { 7, 4, 10,  0, 0 },
 1390         [IEEE80211_MODE_11A]    = { 7, 4, 10,  0, 0 },
 1391         [IEEE80211_MODE_11B]    = { 7, 4, 10,  0, 0 },
 1392         [IEEE80211_MODE_11G]    = { 7, 4, 10,  0, 0 },
 1393         [IEEE80211_MODE_FH]     = { 7, 4, 10,  0, 0 },
 1394         [IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
 1395         [IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
 1396         [IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
 1397         [IEEE80211_MODE_HALF]   = { 7, 4, 10,  0, 0 },
 1398         [IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
 1399         [IEEE80211_MODE_11NA]   = { 7, 4, 10,  0, 0 },
 1400         [IEEE80211_MODE_11NG]   = { 7, 4, 10,  0, 0 },
 1401         [IEEE80211_MODE_VHT_2GHZ]       = { 7, 4, 10,  0, 0 },
 1402         [IEEE80211_MODE_VHT_5GHZ]       = { 7, 4, 10,  0, 0 },
 1403 };
 1404 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
 1405         [IEEE80211_MODE_AUTO]   = { 1, 3, 4,  94, 0 },
 1406         [IEEE80211_MODE_11A]    = { 1, 3, 4,  94, 0 },
 1407         [IEEE80211_MODE_11B]    = { 1, 3, 4, 188, 0 },
 1408         [IEEE80211_MODE_11G]    = { 1, 3, 4,  94, 0 },
 1409         [IEEE80211_MODE_FH]     = { 1, 3, 4, 188, 0 },
 1410         [IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
 1411         [IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
 1412         [IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
 1413         [IEEE80211_MODE_HALF]   = { 1, 3, 4,  94, 0 },
 1414         [IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
 1415         [IEEE80211_MODE_11NA]   = { 1, 3, 4,  94, 0 },
 1416         [IEEE80211_MODE_11NG]   = { 1, 3, 4,  94, 0 },
 1417         [IEEE80211_MODE_VHT_2GHZ]       = { 1, 3, 4,  94, 0 },
 1418         [IEEE80211_MODE_VHT_5GHZ]       = { 1, 3, 4,  94, 0 },
 1419 };
 1420 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
 1421         [IEEE80211_MODE_AUTO]   = { 1, 2, 3,  47, 0 },
 1422         [IEEE80211_MODE_11A]    = { 1, 2, 3,  47, 0 },
 1423         [IEEE80211_MODE_11B]    = { 1, 2, 3, 102, 0 },
 1424         [IEEE80211_MODE_11G]    = { 1, 2, 3,  47, 0 },
 1425         [IEEE80211_MODE_FH]     = { 1, 2, 3, 102, 0 },
 1426         [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
 1427         [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
 1428         [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
 1429         [IEEE80211_MODE_HALF]   = { 1, 2, 3,  47, 0 },
 1430         [IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
 1431         [IEEE80211_MODE_11NA]   = { 1, 2, 3,  47, 0 },
 1432         [IEEE80211_MODE_11NG]   = { 1, 2, 3,  47, 0 },
 1433         [IEEE80211_MODE_VHT_2GHZ]       = { 1, 2, 3,  47, 0 },
 1434         [IEEE80211_MODE_VHT_5GHZ]       = { 1, 2, 3,  47, 0 },
 1435 };
 1436 
 1437 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
 1438         [IEEE80211_MODE_AUTO]   = { 3, 4, 10,  0, 0 },
 1439         [IEEE80211_MODE_11A]    = { 3, 4, 10,  0, 0 },
 1440         [IEEE80211_MODE_11B]    = { 3, 4, 10,  0, 0 },
 1441         [IEEE80211_MODE_11G]    = { 3, 4, 10,  0, 0 },
 1442         [IEEE80211_MODE_FH]     = { 3, 4, 10,  0, 0 },
 1443         [IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
 1444         [IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
 1445         [IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
 1446         [IEEE80211_MODE_HALF]   = { 3, 4, 10,  0, 0 },
 1447         [IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
 1448         [IEEE80211_MODE_11NA]   = { 3, 4, 10,  0, 0 },
 1449         [IEEE80211_MODE_11NG]   = { 3, 4, 10,  0, 0 },
 1450 };
 1451 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
 1452         [IEEE80211_MODE_AUTO]   = { 2, 3, 4,  94, 0 },
 1453         [IEEE80211_MODE_11A]    = { 2, 3, 4,  94, 0 },
 1454         [IEEE80211_MODE_11B]    = { 2, 3, 4, 188, 0 },
 1455         [IEEE80211_MODE_11G]    = { 2, 3, 4,  94, 0 },
 1456         [IEEE80211_MODE_FH]     = { 2, 3, 4, 188, 0 },
 1457         [IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
 1458         [IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
 1459         [IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
 1460         [IEEE80211_MODE_HALF]   = { 2, 3, 4,  94, 0 },
 1461         [IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
 1462         [IEEE80211_MODE_11NA]   = { 2, 3, 4,  94, 0 },
 1463         [IEEE80211_MODE_11NG]   = { 2, 3, 4,  94, 0 },
 1464 };
 1465 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
 1466         [IEEE80211_MODE_AUTO]   = { 2, 2, 3,  47, 0 },
 1467         [IEEE80211_MODE_11A]    = { 2, 2, 3,  47, 0 },
 1468         [IEEE80211_MODE_11B]    = { 2, 2, 3, 102, 0 },
 1469         [IEEE80211_MODE_11G]    = { 2, 2, 3,  47, 0 },
 1470         [IEEE80211_MODE_FH]     = { 2, 2, 3, 102, 0 },
 1471         [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
 1472         [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
 1473         [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
 1474         [IEEE80211_MODE_HALF]   = { 2, 2, 3,  47, 0 },
 1475         [IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
 1476         [IEEE80211_MODE_11NA]   = { 2, 2, 3,  47, 0 },
 1477         [IEEE80211_MODE_11NG]   = { 2, 2, 3,  47, 0 },
 1478 };
 1479 
 1480 static void
 1481 _setifsparams(struct wmeParams *wmep, const paramType *phy)
 1482 {
 1483         wmep->wmep_aifsn = phy->aifsn;
 1484         wmep->wmep_logcwmin = phy->logcwmin;    
 1485         wmep->wmep_logcwmax = phy->logcwmax;            
 1486         wmep->wmep_txopLimit = phy->txopLimit;
 1487 }
 1488 
 1489 static void
 1490 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
 1491         struct wmeParams *wmep, const paramType *phy)
 1492 {
 1493         wmep->wmep_acm = phy->acm;
 1494         _setifsparams(wmep, phy);
 1495 
 1496         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1497             "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
 1498             ieee80211_wme_acnames[ac], type,
 1499             wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
 1500             wmep->wmep_logcwmax, wmep->wmep_txopLimit);
 1501 }
 1502 
 1503 static void
 1504 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
 1505 {
 1506         struct ieee80211com *ic = vap->iv_ic;
 1507         struct ieee80211_wme_state *wme = &ic->ic_wme;
 1508         const paramType *pPhyParam, *pBssPhyParam;
 1509         struct wmeParams *wmep;
 1510         enum ieee80211_phymode mode;
 1511         int i;
 1512 
 1513         IEEE80211_LOCK_ASSERT(ic);
 1514 
 1515         if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
 1516                 return;
 1517 
 1518         /*
 1519          * Clear the wme cap_info field so a qoscount from a previous
 1520          * vap doesn't confuse later code which only parses the beacon
 1521          * field and updates hardware when said field changes.
 1522          * Otherwise the hardware is programmed with defaults, not what
 1523          * the beacon actually announces.
 1524          *
 1525          * Note that we can't ever have 0xff as an actual value;
 1526          * the only valid values are 0..15.
 1527          */
 1528         wme->wme_wmeChanParams.cap_info = 0xfe;
 1529 
 1530         /*
 1531          * Select mode; we can be called early in which case we
 1532          * always use auto mode.  We know we'll be called when
 1533          * entering the RUN state with bsschan setup properly
 1534          * so state will eventually get set correctly
 1535          */
 1536         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
 1537                 mode = ieee80211_chan2mode(ic->ic_bsschan);
 1538         else
 1539                 mode = IEEE80211_MODE_AUTO;
 1540         for (i = 0; i < WME_NUM_AC; i++) {
 1541                 switch (i) {
 1542                 case WME_AC_BK:
 1543                         pPhyParam = &phyParamForAC_BK[mode];
 1544                         pBssPhyParam = &phyParamForAC_BK[mode];
 1545                         break;
 1546                 case WME_AC_VI:
 1547                         pPhyParam = &phyParamForAC_VI[mode];
 1548                         pBssPhyParam = &bssPhyParamForAC_VI[mode];
 1549                         break;
 1550                 case WME_AC_VO:
 1551                         pPhyParam = &phyParamForAC_VO[mode];
 1552                         pBssPhyParam = &bssPhyParamForAC_VO[mode];
 1553                         break;
 1554                 case WME_AC_BE:
 1555                 default:
 1556                         pPhyParam = &phyParamForAC_BE[mode];
 1557                         pBssPhyParam = &bssPhyParamForAC_BE[mode];
 1558                         break;
 1559                 }
 1560                 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
 1561                 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
 1562                         setwmeparams(vap, "chan", i, wmep, pPhyParam);
 1563                 } else {
 1564                         setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
 1565                 }       
 1566                 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
 1567                 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
 1568         }
 1569         /* NB: check ic_bss to avoid NULL deref on initial attach */
 1570         if (vap->iv_bss != NULL) {
 1571                 /*
 1572                  * Calculate aggressive mode switching threshold based
 1573                  * on beacon interval.  This doesn't need locking since
 1574                  * we're only called before entering the RUN state at
 1575                  * which point we start sending beacon frames.
 1576                  */
 1577                 wme->wme_hipri_switch_thresh =
 1578                         (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
 1579                 wme->wme_flags &= ~WME_F_AGGRMODE;
 1580                 ieee80211_wme_updateparams(vap);
 1581         }
 1582 }
 1583 
 1584 void
 1585 ieee80211_wme_initparams(struct ieee80211vap *vap)
 1586 {
 1587         struct ieee80211com *ic = vap->iv_ic;
 1588 
 1589         IEEE80211_LOCK(ic);
 1590         ieee80211_wme_initparams_locked(vap);
 1591         IEEE80211_UNLOCK(ic);
 1592 }
 1593 
 1594 /*
 1595  * Update WME parameters for ourself and the BSS.
 1596  */
 1597 void
 1598 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
 1599 {
 1600         static const paramType aggrParam[IEEE80211_MODE_MAX] = {
 1601             [IEEE80211_MODE_AUTO]       = { 2, 4, 10, 64, 0 },
 1602             [IEEE80211_MODE_11A]        = { 2, 4, 10, 64, 0 },
 1603             [IEEE80211_MODE_11B]        = { 2, 5, 10, 64, 0 },
 1604             [IEEE80211_MODE_11G]        = { 2, 4, 10, 64, 0 },
 1605             [IEEE80211_MODE_FH]         = { 2, 5, 10, 64, 0 },
 1606             [IEEE80211_MODE_TURBO_A]    = { 1, 3, 10, 64, 0 },
 1607             [IEEE80211_MODE_TURBO_G]    = { 1, 3, 10, 64, 0 },
 1608             [IEEE80211_MODE_STURBO_A]   = { 1, 3, 10, 64, 0 },
 1609             [IEEE80211_MODE_HALF]       = { 2, 4, 10, 64, 0 },
 1610             [IEEE80211_MODE_QUARTER]    = { 2, 4, 10, 64, 0 },
 1611             [IEEE80211_MODE_11NA]       = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1612             [IEEE80211_MODE_11NG]       = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1613             [IEEE80211_MODE_VHT_2GHZ]   = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1614             [IEEE80211_MODE_VHT_5GHZ]   = { 2, 4, 10, 64, 0 },  /* XXXcheck*/
 1615         };
 1616         struct ieee80211com *ic = vap->iv_ic;
 1617         struct ieee80211_wme_state *wme = &ic->ic_wme;
 1618         const struct wmeParams *wmep;
 1619         struct wmeParams *chanp, *bssp;
 1620         enum ieee80211_phymode mode;
 1621         int i;
 1622         int do_aggrmode = 0;
 1623 
 1624         /*
 1625          * Set up the channel access parameters for the physical
 1626          * device.  First populate the configured settings.
 1627          */
 1628         for (i = 0; i < WME_NUM_AC; i++) {
 1629                 chanp = &wme->wme_chanParams.cap_wmeParams[i];
 1630                 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
 1631                 chanp->wmep_aifsn = wmep->wmep_aifsn;
 1632                 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
 1633                 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
 1634                 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
 1635 
 1636                 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
 1637                 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
 1638                 chanp->wmep_aifsn = wmep->wmep_aifsn;
 1639                 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
 1640                 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
 1641                 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
 1642         }
 1643 
 1644         /*
 1645          * Select mode; we can be called early in which case we
 1646          * always use auto mode.  We know we'll be called when
 1647          * entering the RUN state with bsschan setup properly
 1648          * so state will eventually get set correctly
 1649          */
 1650         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
 1651                 mode = ieee80211_chan2mode(ic->ic_bsschan);
 1652         else
 1653                 mode = IEEE80211_MODE_AUTO;
 1654 
 1655         /*
 1656          * This implements aggressive mode as found in certain
 1657          * vendors' AP's.  When there is significant high
 1658          * priority (VI/VO) traffic in the BSS throttle back BE
 1659          * traffic by using conservative parameters.  Otherwise
 1660          * BE uses aggressive params to optimize performance of
 1661          * legacy/non-QoS traffic.
 1662          */
 1663 
 1664         /* Hostap? Only if aggressive mode is enabled */
 1665         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1666              (wme->wme_flags & WME_F_AGGRMODE) != 0)
 1667                 do_aggrmode = 1;
 1668 
 1669         /*
 1670          * Station? Only if we're in a non-QoS BSS.
 1671          */
 1672         else if ((vap->iv_opmode == IEEE80211_M_STA &&
 1673              (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
 1674                 do_aggrmode = 1;
 1675 
 1676         /*
 1677          * IBSS? Only if we we have WME enabled.
 1678          */
 1679         else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
 1680             (vap->iv_flags & IEEE80211_F_WME))
 1681                 do_aggrmode = 1;
 1682 
 1683         /*
 1684          * If WME is disabled on this VAP, default to aggressive mode
 1685          * regardless of the configuration.
 1686          */
 1687         if ((vap->iv_flags & IEEE80211_F_WME) == 0)
 1688                 do_aggrmode = 1;
 1689 
 1690         /* XXX WDS? */
 1691 
 1692         /* XXX MBSS? */
 1693         
 1694         if (do_aggrmode) {
 1695                 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
 1696                 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
 1697 
 1698                 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
 1699                 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
 1700                     aggrParam[mode].logcwmin;
 1701                 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
 1702                     aggrParam[mode].logcwmax;
 1703                 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
 1704                     (vap->iv_flags & IEEE80211_F_BURST) ?
 1705                         aggrParam[mode].txopLimit : 0;          
 1706                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1707                     "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
 1708                     "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
 1709                     chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
 1710                     chanp->wmep_logcwmax, chanp->wmep_txopLimit);
 1711         }
 1712 
 1713 
 1714         /*
 1715          * Change the contention window based on the number of associated
 1716          * stations.  If the number of associated stations is 1 and
 1717          * aggressive mode is enabled, lower the contention window even
 1718          * further.
 1719          */
 1720         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1721             vap->iv_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
 1722                 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
 1723                     [IEEE80211_MODE_AUTO]       = 3,
 1724                     [IEEE80211_MODE_11A]        = 3,
 1725                     [IEEE80211_MODE_11B]        = 4,
 1726                     [IEEE80211_MODE_11G]        = 3,
 1727                     [IEEE80211_MODE_FH]         = 4,
 1728                     [IEEE80211_MODE_TURBO_A]    = 3,
 1729                     [IEEE80211_MODE_TURBO_G]    = 3,
 1730                     [IEEE80211_MODE_STURBO_A]   = 3,
 1731                     [IEEE80211_MODE_HALF]       = 3,
 1732                     [IEEE80211_MODE_QUARTER]    = 3,
 1733                     [IEEE80211_MODE_11NA]       = 3,
 1734                     [IEEE80211_MODE_11NG]       = 3,
 1735                     [IEEE80211_MODE_VHT_2GHZ]   = 3,
 1736                     [IEEE80211_MODE_VHT_5GHZ]   = 3,
 1737                 };
 1738                 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
 1739                 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
 1740 
 1741                 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
 1742                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1743                     "update %s (chan+bss) logcwmin %u\n",
 1744                     ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
 1745         }
 1746 
 1747         /* schedule the deferred WME update */
 1748         ieee80211_runtask(ic, &vap->iv_wme_task);
 1749 
 1750         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 1751             "%s: WME params updated, cap_info 0x%x\n", __func__,
 1752             vap->iv_opmode == IEEE80211_M_STA ?
 1753                 wme->wme_wmeChanParams.cap_info :
 1754                 wme->wme_bssChanParams.cap_info);
 1755 }
 1756 
 1757 void
 1758 ieee80211_wme_updateparams(struct ieee80211vap *vap)
 1759 {
 1760         struct ieee80211com *ic = vap->iv_ic;
 1761 
 1762         if (ic->ic_caps & IEEE80211_C_WME) {
 1763                 IEEE80211_LOCK(ic);
 1764                 ieee80211_wme_updateparams_locked(vap);
 1765                 IEEE80211_UNLOCK(ic);
 1766         }
 1767 }
 1768 
 1769 /*
 1770  * Fetch the WME parameters for the given VAP.
 1771  *
 1772  * When net80211 grows p2p, etc support, this may return different
 1773  * parameters for each VAP.
 1774  */
 1775 void
 1776 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
 1777 {
 1778 
 1779         memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
 1780 }
 1781 
 1782 /*
 1783  * For NICs which only support one set of WME parameters (ie, softmac NICs)
 1784  * there may be different VAP WME parameters but only one is "active".
 1785  * This returns the "NIC" WME parameters for the currently active
 1786  * context.
 1787  */
 1788 void
 1789 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
 1790 {
 1791 
 1792         memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
 1793 }
 1794 
 1795 /*
 1796  * Return whether to use QoS on a given WME queue.
 1797  *
 1798  * This is intended to be called from the transmit path of softmac drivers
 1799  * which are setting NoAck bits in transmit descriptors.
 1800  *
 1801  * Ideally this would be set in some transmit field before the packet is
 1802  * queued to the driver but net80211 isn't quite there yet.
 1803  */
 1804 int
 1805 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
 1806 {
 1807         /* Bounds/sanity check */
 1808         if (ac < 0 || ac >= WME_NUM_AC)
 1809                 return (0);
 1810 
 1811         /* Again, there's only one global context for now */
 1812         return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
 1813 }
 1814 
 1815 static void
 1816 parent_updown(void *arg, int npending)
 1817 {
 1818         struct ieee80211com *ic = arg;
 1819 
 1820         ic->ic_parent(ic);
 1821 }
 1822 
 1823 static void
 1824 update_mcast(void *arg, int npending)
 1825 {
 1826         struct ieee80211com *ic = arg;
 1827 
 1828         ic->ic_update_mcast(ic);
 1829 }
 1830 
 1831 static void
 1832 update_promisc(void *arg, int npending)
 1833 {
 1834         struct ieee80211com *ic = arg;
 1835 
 1836         ic->ic_update_promisc(ic);
 1837 }
 1838 
 1839 static void
 1840 update_channel(void *arg, int npending)
 1841 {
 1842         struct ieee80211com *ic = arg;
 1843 
 1844         ic->ic_set_channel(ic);
 1845         ieee80211_radiotap_chan_change(ic);
 1846 }
 1847 
 1848 static void
 1849 update_chw(void *arg, int npending)
 1850 {
 1851         struct ieee80211com *ic = arg;
 1852 
 1853         /*
 1854          * XXX should we defer the channel width _config_ update until now?
 1855          */
 1856         ic->ic_update_chw(ic);
 1857 }
 1858 
 1859 /*
 1860  * Deferred WME parameter and beacon update.
 1861  *
 1862  * In preparation for per-VAP WME configuration, call the VAP
 1863  * method if the VAP requires it.  Otherwise, just call the
 1864  * older global method.  There isn't a per-VAP WME configuration
 1865  * just yet so for now just use the global configuration.
 1866  */
 1867 static void
 1868 vap_update_wme(void *arg, int npending)
 1869 {
 1870         struct ieee80211vap *vap = arg;
 1871         struct ieee80211com *ic = vap->iv_ic;
 1872         struct ieee80211_wme_state *wme = &ic->ic_wme;
 1873 
 1874         /* Driver update */
 1875         if (vap->iv_wme_update != NULL)
 1876                 vap->iv_wme_update(vap,
 1877                     ic->ic_wme.wme_chanParams.cap_wmeParams);
 1878         else
 1879                 ic->ic_wme.wme_update(ic);
 1880 
 1881         IEEE80211_LOCK(ic);
 1882         /*
 1883          * Arrange for the beacon update.
 1884          *
 1885          * XXX what about MBSS, WDS?
 1886          */
 1887         if (vap->iv_opmode == IEEE80211_M_HOSTAP
 1888             || vap->iv_opmode == IEEE80211_M_IBSS) {
 1889                 /*
 1890                  * Arrange for a beacon update and bump the parameter
 1891                  * set number so associated stations load the new values.
 1892                  */
 1893                 wme->wme_bssChanParams.cap_info =
 1894                         (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
 1895                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
 1896         }
 1897         IEEE80211_UNLOCK(ic);
 1898 }
 1899 
 1900 static void
 1901 restart_vaps(void *arg, int npending)
 1902 {
 1903         struct ieee80211com *ic = arg;
 1904 
 1905         ieee80211_suspend_all(ic);
 1906         ieee80211_resume_all(ic);
 1907 }
 1908 
 1909 /*
 1910  * Block until the parent is in a known state.  This is
 1911  * used after any operations that dispatch a task (e.g.
 1912  * to auto-configure the parent device up/down).
 1913  */
 1914 void
 1915 ieee80211_waitfor_parent(struct ieee80211com *ic)
 1916 {
 1917         taskqueue_block(ic->ic_tq);
 1918         ieee80211_draintask(ic, &ic->ic_parent_task);
 1919         ieee80211_draintask(ic, &ic->ic_mcast_task);
 1920         ieee80211_draintask(ic, &ic->ic_promisc_task);
 1921         ieee80211_draintask(ic, &ic->ic_chan_task);
 1922         ieee80211_draintask(ic, &ic->ic_bmiss_task);
 1923         ieee80211_draintask(ic, &ic->ic_chw_task);
 1924         taskqueue_unblock(ic->ic_tq);
 1925 }
 1926 
 1927 /*
 1928  * Check to see whether the current channel needs reset.
 1929  *
 1930  * Some devices don't handle being given an invalid channel
 1931  * in their operating mode very well (eg wpi(4) will throw a
 1932  * firmware exception.)
 1933  *
 1934  * Return 0 if we're ok, 1 if the channel needs to be reset.
 1935  *
 1936  * See PR kern/202502.
 1937  */
 1938 static int
 1939 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
 1940 {
 1941         struct ieee80211com *ic = vap->iv_ic;
 1942 
 1943         if ((vap->iv_opmode == IEEE80211_M_IBSS &&
 1944              IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
 1945             (vap->iv_opmode == IEEE80211_M_HOSTAP &&
 1946              IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
 1947                 return (1);
 1948         return (0);
 1949 }
 1950 
 1951 /*
 1952  * Reset the curchan to a known good state.
 1953  */
 1954 static void
 1955 ieee80211_start_reset_chan(struct ieee80211vap *vap)
 1956 {
 1957         struct ieee80211com *ic = vap->iv_ic;
 1958 
 1959         ic->ic_curchan = &ic->ic_channels[0];
 1960 }
 1961 
 1962 /*
 1963  * Start a vap running.  If this is the first vap to be
 1964  * set running on the underlying device then we
 1965  * automatically bring the device up.
 1966  */
 1967 void
 1968 ieee80211_start_locked(struct ieee80211vap *vap)
 1969 {
 1970         struct ifnet *ifp = vap->iv_ifp;
 1971         struct ieee80211com *ic = vap->iv_ic;
 1972 
 1973         IEEE80211_LOCK_ASSERT(ic);
 1974 
 1975         IEEE80211_DPRINTF(vap,
 1976                 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
 1977                 "start running, %d vaps running\n", ic->ic_nrunning);
 1978 
 1979         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 1980                 /*
 1981                  * Mark us running.  Note that it's ok to do this first;
 1982                  * if we need to bring the parent device up we defer that
 1983                  * to avoid dropping the com lock.  We expect the device
 1984                  * to respond to being marked up by calling back into us
 1985                  * through ieee80211_start_all at which point we'll come
 1986                  * back in here and complete the work.
 1987                  */
 1988                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1989                 ieee80211_notify_ifnet_change(vap);
 1990 
 1991                 /*
 1992                  * We are not running; if this we are the first vap
 1993                  * to be brought up auto-up the parent if necessary.
 1994                  */
 1995                 if (ic->ic_nrunning++ == 0) {
 1996 
 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: 4d4298b6e05d50e3d17a78f1ccbfffb2


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