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_node.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-2009 Sam Leffler, Errno Consulting
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include "opt_wlan.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/mbuf.h>
   37 #include <sys/malloc.h>
   38 #include <sys/kernel.h>
   39 
   40 #include <sys/socket.h>
   41 
   42 #include <net/if.h>
   43 #include <net/if_var.h>
   44 #include <net/if_media.h>
   45 #include <net/ethernet.h>
   46 
   47 #include <net80211/ieee80211_var.h>
   48 #include <net80211/ieee80211_input.h>
   49 #ifdef IEEE80211_SUPPORT_SUPERG
   50 #include <net80211/ieee80211_superg.h>
   51 #endif
   52 #ifdef IEEE80211_SUPPORT_TDMA
   53 #include <net80211/ieee80211_tdma.h>
   54 #endif
   55 #include <net80211/ieee80211_wds.h>
   56 #include <net80211/ieee80211_mesh.h>
   57 #include <net80211/ieee80211_ratectl.h>
   58 #include <net80211/ieee80211_vht.h>
   59 
   60 #include <net/bpf.h>
   61 
   62 /*
   63  * IEEE80211_NODE_HASHSIZE must be a power of 2.
   64  */
   65 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
   66 
   67 /*
   68  * Association id's are managed with a bit vector.
   69  */
   70 #define IEEE80211_AID_SET(_vap, b) \
   71         ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
   72                 (1 << (IEEE80211_AID(b) % 32)))
   73 #define IEEE80211_AID_CLR(_vap, b) \
   74         ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
   75                 ~(1 << (IEEE80211_AID(b) % 32)))
   76 #define IEEE80211_AID_ISSET(_vap, b) \
   77         ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
   78 
   79 static int ieee80211_sta_join1(struct ieee80211_node *);
   80 
   81 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
   82         const uint8_t [IEEE80211_ADDR_LEN]);
   83 static int node_init(struct ieee80211_node *);
   84 static void node_cleanup(struct ieee80211_node *);
   85 static void node_free(struct ieee80211_node *);
   86 static void node_age(struct ieee80211_node *);
   87 static int8_t node_getrssi(const struct ieee80211_node *);
   88 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
   89 static void node_getmimoinfo(const struct ieee80211_node *,
   90         struct ieee80211_mimo_info *);
   91 
   92 static void _ieee80211_free_node(struct ieee80211_node *);
   93 
   94 static void node_reclaim(struct ieee80211_node_table *nt,
   95         struct ieee80211_node *ni);
   96 static void ieee80211_node_table_init(struct ieee80211com *ic,
   97         struct ieee80211_node_table *nt, const char *name,
   98         int inact, int keymaxix);
   99 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
  100         struct ieee80211vap *);
  101 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
  102 static void ieee80211_vap_erp_timeout(struct ieee80211vap *);
  103 
  104 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
  105 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
  106 
  107 void
  108 ieee80211_node_attach(struct ieee80211com *ic)
  109 {
  110         /* XXX really want maxlen enforced per-sta */
  111         ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
  112             "802.11 staging q");
  113         ieee80211_node_table_init(ic, &ic->ic_sta, "station",
  114                 IEEE80211_INACT_INIT, ic->ic_max_keyix);
  115         callout_init(&ic->ic_inact, 1);
  116         callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
  117                 ieee80211_node_timeout, ic);
  118 
  119         ic->ic_node_alloc = node_alloc;
  120         ic->ic_node_init = node_init;
  121         ic->ic_node_free = node_free;
  122         ic->ic_node_cleanup = node_cleanup;
  123         ic->ic_node_age = node_age;
  124         ic->ic_node_drain = node_age;           /* NB: same as age */
  125         ic->ic_node_getrssi = node_getrssi;
  126         ic->ic_node_getsignal = node_getsignal;
  127         ic->ic_node_getmimoinfo = node_getmimoinfo;
  128 
  129         /*
  130          * Set flags to be propagated to all vap's;
  131          * these define default behaviour/configuration.
  132          */
  133         ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
  134 }
  135 
  136 void
  137 ieee80211_node_detach(struct ieee80211com *ic)
  138 {
  139 
  140         callout_drain(&ic->ic_inact);
  141         ieee80211_node_table_cleanup(&ic->ic_sta);
  142         ieee80211_ageq_drain(&ic->ic_stageq);
  143         ieee80211_ageq_cleanup(&ic->ic_stageq);
  144 }
  145 
  146 void
  147 ieee80211_node_vattach(struct ieee80211vap *vap)
  148 {
  149         /* NB: driver can override */
  150         vap->iv_max_aid = IEEE80211_AID_DEF;
  151 
  152         /* default station inactivity timer setings */
  153         vap->iv_inact_init = IEEE80211_INACT_INIT;
  154         vap->iv_inact_auth = IEEE80211_INACT_AUTH;
  155         vap->iv_inact_run = IEEE80211_INACT_RUN;
  156         vap->iv_inact_probe = IEEE80211_INACT_PROBE;
  157 
  158         IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
  159             "%s: init %u auth %u run %u probe %u\n", __func__,
  160             vap->iv_inact_init, vap->iv_inact_auth,
  161             vap->iv_inact_run, vap->iv_inact_probe);
  162 }
  163 
  164 void
  165 ieee80211_node_latevattach(struct ieee80211vap *vap)
  166 {
  167         if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
  168                 /* XXX should we allow max aid to be zero? */
  169                 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
  170                         vap->iv_max_aid = IEEE80211_AID_MIN;
  171                         if_printf(vap->iv_ifp,
  172                             "WARNING: max aid too small, changed to %d\n",
  173                             vap->iv_max_aid);
  174                 }
  175                 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
  176                         howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
  177                         M_80211_NODE,
  178                         IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  179                 if (vap->iv_aid_bitmap == NULL) {
  180                         /* XXX no way to recover */
  181                         printf("%s: no memory for AID bitmap, max aid %d!\n",
  182                             __func__, vap->iv_max_aid);
  183                         vap->iv_max_aid = 0;
  184                 }
  185         }
  186 
  187         ieee80211_reset_bss(vap);
  188 
  189         vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
  190 }
  191 
  192 void
  193 ieee80211_node_vdetach(struct ieee80211vap *vap)
  194 {
  195         struct ieee80211com *ic = vap->iv_ic;
  196 
  197         ieee80211_node_table_reset(&ic->ic_sta, vap);
  198         if (vap->iv_bss != NULL) {
  199                 ieee80211_free_node(vap->iv_bss);
  200                 vap->iv_bss = NULL;
  201         }
  202         if (vap->iv_aid_bitmap != NULL) {
  203                 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
  204                 vap->iv_aid_bitmap = NULL;
  205         }
  206 }
  207 
  208 /*
  209  * Port authorize/unauthorize interfaces for use by an authenticator.
  210  */
  211 
  212 void
  213 ieee80211_node_authorize(struct ieee80211_node *ni)
  214 {
  215         struct ieee80211vap *vap = ni->ni_vap;
  216 
  217         ni->ni_flags |= IEEE80211_NODE_AUTH;
  218         ni->ni_inact_reload = vap->iv_inact_run;
  219         ni->ni_inact = ni->ni_inact_reload;
  220 
  221         IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
  222             "%s: inact_reload %u", __func__, ni->ni_inact_reload);
  223 }
  224 
  225 void
  226 ieee80211_node_unauthorize(struct ieee80211_node *ni)
  227 {
  228         struct ieee80211vap *vap = ni->ni_vap;
  229 
  230         ni->ni_flags &= ~IEEE80211_NODE_AUTH;
  231         ni->ni_inact_reload = vap->iv_inact_auth;
  232         if (ni->ni_inact > ni->ni_inact_reload)
  233                 ni->ni_inact = ni->ni_inact_reload;
  234 
  235         IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
  236             "%s: inact_reload %u inact %u", __func__,
  237             ni->ni_inact_reload, ni->ni_inact);
  238 }
  239 
  240 /*
  241  * Fix tx parameters for a node according to ``association state''.
  242  */
  243 void
  244 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
  245 {
  246         struct ieee80211vap *vap = ni->ni_vap;
  247         enum ieee80211_phymode mode;
  248 
  249         if (ni->ni_flags & IEEE80211_NODE_VHT) {
  250                 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
  251                         mode = IEEE80211_MODE_VHT_5GHZ;
  252                 else
  253                         mode = IEEE80211_MODE_VHT_2GHZ;
  254         } else if (ni->ni_flags & IEEE80211_NODE_HT) {
  255                 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
  256                         mode = IEEE80211_MODE_11NA;
  257                 else
  258                         mode = IEEE80211_MODE_11NG;
  259         } else {                                /* legacy rate handling */
  260                 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
  261                         mode = IEEE80211_MODE_STURBO_A;
  262                 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
  263                         mode = IEEE80211_MODE_HALF;
  264                 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
  265                         mode = IEEE80211_MODE_QUARTER;
  266                 /* NB: 108A should be handled as 11a */
  267                 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
  268                         mode = IEEE80211_MODE_11A;
  269                 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
  270                     (ni->ni_flags & IEEE80211_NODE_ERP))
  271                         mode = IEEE80211_MODE_11G;
  272                 else
  273                         mode = IEEE80211_MODE_11B;
  274         }
  275         ni->ni_txparms = &vap->iv_txparms[mode];
  276 }
  277 
  278 /*
  279  * Set/change the channel.  The rate set is also updated as
  280  * to insure a consistent view by drivers.
  281  * XXX should be private but hostap needs it to deal with CSA
  282  */
  283 void
  284 ieee80211_node_set_chan(struct ieee80211_node *ni,
  285         struct ieee80211_channel *chan)
  286 {
  287         struct ieee80211com *ic = ni->ni_ic;
  288         struct ieee80211vap *vap = ni->ni_vap;
  289         enum ieee80211_phymode mode;
  290 
  291         KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
  292 
  293         ni->ni_chan = chan;
  294         mode = ieee80211_chan2mode(chan);
  295         if (IEEE80211_IS_CHAN_HT(chan)) {
  296                 /*
  297                  * We must install the legacy rate est in ni_rates and the
  298                  * HT rate set in ni_htrates.
  299                  */
  300                 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
  301                 /*
  302                  * Setup bss tx parameters based on operating mode.  We
  303                  * use legacy rates when operating in a mixed HT+non-HT bss
  304                  * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
  305                  */
  306                 if (mode == IEEE80211_MODE_11NA &&
  307                     (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
  308                         mode = IEEE80211_MODE_11A;
  309                 else if (mode == IEEE80211_MODE_11NG &&
  310                     (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
  311                         mode = IEEE80211_MODE_11G;
  312                 if (mode == IEEE80211_MODE_11G &&
  313                     (vap->iv_flags & IEEE80211_F_PUREG) == 0)
  314                         mode = IEEE80211_MODE_11B;
  315         }
  316         ni->ni_txparms = &vap->iv_txparms[mode];
  317         ni->ni_rates = *ieee80211_get_suprates(ic, chan);
  318 }
  319 
  320 static __inline void
  321 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
  322 {
  323         /* propagate useful state */
  324         nbss->ni_authmode = obss->ni_authmode;
  325         nbss->ni_txpower = obss->ni_txpower;
  326         nbss->ni_vlan = obss->ni_vlan;
  327         /* XXX statistics? */
  328         /* XXX legacy WDS bssid? */
  329 }
  330 
  331 void
  332 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
  333 {
  334         struct ieee80211com *ic = vap->iv_ic;
  335         struct ieee80211_node *ni;
  336 
  337         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
  338                 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
  339                 ieee80211_opmode_name[vap->iv_opmode],
  340                 ieee80211_chan2ieee(ic, chan),
  341                 ieee80211_channel_type_char(chan),
  342                 chan->ic_flags);
  343 
  344         ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
  345         if (ni == NULL) {
  346                 /* XXX recovery? */
  347                 return;
  348         }
  349         IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
  350         ni->ni_esslen = vap->iv_des_ssid[0].len;
  351         memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
  352         if (vap->iv_bss != NULL)
  353                 copy_bss(ni, vap->iv_bss);
  354         ni->ni_intval = ic->ic_bintval;
  355         if (vap->iv_flags & IEEE80211_F_PRIVACY)
  356                 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
  357         if (ic->ic_phytype == IEEE80211_T_FH) {
  358                 ni->ni_fhdwell = 200;   /* XXX */
  359                 ni->ni_fhindex = 1;
  360         }
  361         if (vap->iv_opmode == IEEE80211_M_IBSS) {
  362                 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;       /* XXX */
  363                 if (vap->iv_flags & IEEE80211_F_DESBSSID)
  364                         IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
  365                 else {
  366                         net80211_get_random_bytes(ni->ni_bssid,
  367                             IEEE80211_ADDR_LEN);
  368                         /* clear group bit, add local bit */
  369                         ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
  370                 }
  371         } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
  372                 if (vap->iv_flags & IEEE80211_F_DESBSSID)
  373                         IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
  374                 else
  375 #ifdef IEEE80211_SUPPORT_TDMA
  376                 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
  377 #endif
  378                         memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
  379 #ifdef IEEE80211_SUPPORT_MESH
  380         } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
  381                 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
  382                 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
  383 #endif
  384         }
  385         /*
  386          * Fix the channel and related attributes.
  387          */
  388         /* clear DFS CAC state on previous channel */
  389         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
  390             ic->ic_bsschan->ic_freq != chan->ic_freq &&
  391             IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
  392                 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
  393         ic->ic_bsschan = chan;
  394         ieee80211_node_set_chan(ni, chan);
  395         ic->ic_curmode = ieee80211_chan2mode(chan);
  396         /*
  397          * Do mode-specific setup.
  398          */
  399         if (IEEE80211_IS_CHAN_FULL(chan)) {
  400                 if (IEEE80211_IS_CHAN_ANYG(chan)) {
  401                         /*
  402                          * Use a mixed 11b/11g basic rate set.
  403                          */
  404                         ieee80211_setbasicrates(&ni->ni_rates,
  405                             IEEE80211_MODE_11G);
  406                         if (vap->iv_flags & IEEE80211_F_PUREG) {
  407                                 /*
  408                                  * Also mark OFDM rates basic so 11b
  409                                  * stations do not join (WiFi compliance).
  410                                  */
  411                                 ieee80211_addbasicrates(&ni->ni_rates,
  412                                     IEEE80211_MODE_11A);
  413                         }
  414                 } else if (IEEE80211_IS_CHAN_B(chan)) {
  415                         /*
  416                          * Force pure 11b rate set.
  417                          */
  418                         ieee80211_setbasicrates(&ni->ni_rates,
  419                                 IEEE80211_MODE_11B);
  420                 }
  421         }
  422 
  423         /* XXX TODO: other bits and pieces - eg fast-frames? */
  424 
  425         /* If we're an 11n channel then initialise the 11n bits */
  426         if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
  427                 /* XXX what else? */
  428                 ieee80211_ht_node_init(ni);
  429                 ieee80211_vht_node_init(ni);
  430         } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
  431                 /* XXX what else? */
  432                 ieee80211_ht_node_init(ni);
  433         }
  434 
  435         (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
  436 }
  437 
  438 /*
  439  * Reset bss state on transition to the INIT state.
  440  * Clear any stations from the table (they have been
  441  * deauth'd) and reset the bss node (clears key, rate
  442  * etc. state).
  443  */
  444 void
  445 ieee80211_reset_bss(struct ieee80211vap *vap)
  446 {
  447         struct ieee80211com *ic = vap->iv_ic;
  448         struct ieee80211_node *ni, *obss;
  449 
  450         ieee80211_node_table_reset(&ic->ic_sta, vap);
  451         /* XXX multi-bss: wrong */
  452         ieee80211_vap_reset_erp(vap);
  453 
  454         ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
  455         KASSERT(ni != NULL, ("unable to setup initial BSS node"));
  456         obss = vap->iv_bss;
  457         vap->iv_bss = ieee80211_ref_node(ni);
  458         if (obss != NULL) {
  459                 copy_bss(ni, obss);
  460                 ni->ni_intval = ic->ic_bintval;
  461                 ieee80211_free_node(obss);
  462         } else
  463                 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
  464 }
  465 
  466 static int
  467 match_ssid(const struct ieee80211_node *ni,
  468         int nssid, const struct ieee80211_scan_ssid ssids[])
  469 {
  470         int i;
  471 
  472         for (i = 0; i < nssid; i++) {
  473                 if (ni->ni_esslen == ssids[i].len &&
  474                      memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
  475                         return 1;
  476         }
  477         return 0;
  478 }
  479 
  480 /*
  481  * Test a node for suitability/compatibility.
  482  */
  483 static int
  484 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
  485 {
  486         struct ieee80211com *ic = ni->ni_ic;
  487         uint8_t rate;
  488 
  489         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
  490                 return 0;
  491         if (vap->iv_opmode == IEEE80211_M_IBSS) {
  492                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  493                         return 0;
  494         } else {
  495                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
  496                         return 0;
  497         }
  498         if (vap->iv_flags & IEEE80211_F_PRIVACY) {
  499                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  500                         return 0;
  501         } else {
  502                 /* XXX does this mean privacy is supported or required? */
  503                 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
  504                         return 0;
  505         }
  506         rate = ieee80211_fix_rate(ni, &ni->ni_rates,
  507             IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
  508         if (rate & IEEE80211_RATE_BASIC)
  509                 return 0;
  510         if (vap->iv_des_nssid != 0 &&
  511             !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
  512                 return 0;
  513         if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
  514             !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
  515                 return 0;
  516         return 1;
  517 }
  518 
  519 #ifdef IEEE80211_DEBUG
  520 /*
  521  * Display node suitability/compatibility.
  522  */
  523 static void
  524 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
  525 {
  526         struct ieee80211com *ic = ni->ni_ic;
  527         uint8_t rate;
  528         int fail;
  529 
  530         fail = 0;
  531         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
  532                 fail |= 0x01;
  533         if (vap->iv_opmode == IEEE80211_M_IBSS) {
  534                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  535                         fail |= 0x02;
  536         } else {
  537                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
  538                         fail |= 0x02;
  539         }
  540         if (vap->iv_flags & IEEE80211_F_PRIVACY) {
  541                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  542                         fail |= 0x04;
  543         } else {
  544                 /* XXX does this mean privacy is supported or required? */
  545                 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
  546                         fail |= 0x04;
  547         }
  548         rate = ieee80211_fix_rate(ni, &ni->ni_rates,
  549              IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
  550         if (rate & IEEE80211_RATE_BASIC)
  551                 fail |= 0x08;
  552         if (vap->iv_des_nssid != 0 &&
  553             !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
  554                 fail |= 0x10;
  555         if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
  556             !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
  557                 fail |= 0x20;
  558 
  559         printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
  560         printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
  561         printf(" %3d%c",
  562             ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
  563         printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
  564             fail & 0x08 ? '!' : ' ');
  565         printf(" %4s%c",
  566             (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
  567             (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
  568             "????",
  569             fail & 0x02 ? '!' : ' ');
  570         printf(" %3s%c ",
  571             (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
  572             fail & 0x04 ? '!' : ' ');
  573         ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
  574         printf("%s\n", fail & 0x10 ? "!" : "");
  575 }
  576 #endif /* IEEE80211_DEBUG */
  577 
  578 int
  579 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
  580 {
  581         struct ieee80211vap *vap = ni->ni_vap;
  582 
  583         if (ni == vap->iv_bss ||
  584             IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
  585                 /* unchanged, nothing to do */
  586                 return 0;
  587         }
  588 
  589         if (!check_bss(vap, ni)) {
  590                 /* capabilities mismatch */
  591                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
  592                     "%s: merge failed, capabilities mismatch\n", __func__);
  593 #ifdef IEEE80211_DEBUG
  594                 if (ieee80211_msg_assoc(vap))
  595                         check_bss_debug(vap, ni);
  596 #endif
  597                 vap->iv_stats.is_ibss_capmismatch++;
  598                 return 0;
  599         }
  600 
  601         return 1;
  602 }
  603 
  604 /*
  605  * Check if the given node should populate the node table.
  606  *
  607  * We need to be in "see all beacons for all ssids" mode in order
  608  * to do IBSS merges, however this means we will populate nodes for
  609  * /all/ IBSS SSIDs, versus just the one we care about.
  610  *
  611  * So this check ensures the node can actually belong to our IBSS
  612  * configuration.  For now it simply checks the SSID.
  613  */
  614 int
  615 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
  616     const struct ieee80211_scanparams *scan)
  617 {
  618         struct ieee80211vap *vap = ni->ni_vap;
  619         int i;
  620 
  621         /*
  622          * If we have no SSID and no scan SSID, return OK.
  623          */
  624         if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
  625                 goto ok;
  626 
  627         /*
  628          * If we have one of (SSID, scan SSID) then return error.
  629          */
  630         if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
  631                 goto mismatch;
  632 
  633         /*
  634          * Double-check - we need scan SSID.
  635          */
  636         if (scan->ssid == NULL)
  637                 goto mismatch;
  638 
  639         /*
  640          * Check if the scan SSID matches the SSID list for the VAP.
  641          */
  642         for (i = 0; i < vap->iv_des_nssid; i++) {
  643                 /* Sanity length check */
  644                 if (vap->iv_des_ssid[i].len != scan->ssid[1])
  645                         continue;
  646 
  647                 /* Note: SSID in the scan entry is the IE format */
  648                 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
  649                     vap->iv_des_ssid[i].len) == 0)
  650                         goto ok;
  651         }
  652 
  653 mismatch:
  654         return (0);
  655 ok:
  656         return (1);
  657 }
  658 
  659 /*
  660  * Handle 802.11 ad hoc network merge.  The
  661  * convention, set by the Wireless Ethernet Compatibility Alliance
  662  * (WECA), is that an 802.11 station will change its BSSID to match
  663  * the "oldest" 802.11 ad hoc network, on the same channel, that
  664  * has the station's desired SSID.  The "oldest" 802.11 network
  665  * sends beacons with the greatest TSF timestamp.
  666  *
  667  * The caller is assumed to validate TSF's before attempting a merge.
  668  *
  669  * Return !0 if the BSSID changed, 0 otherwise.
  670  */
  671 int
  672 ieee80211_ibss_merge(struct ieee80211_node *ni)
  673 {
  674 #ifdef IEEE80211_DEBUG
  675         struct ieee80211vap *vap = ni->ni_vap;
  676 #endif
  677 
  678         if (! ieee80211_ibss_merge_check(ni))
  679                 return 0;
  680 
  681         IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
  682                 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
  683                 ether_sprintf(ni->ni_bssid),
  684                 vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
  685                 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
  686                 vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
  687         );
  688         return ieee80211_sta_join1(ieee80211_ref_node(ni));
  689 }
  690 
  691 /*
  692  * Calculate HT channel promotion flags for all vaps.
  693  * This assumes ni_chan have been setup for each vap.
  694  */
  695 static int
  696 gethtadjustflags(struct ieee80211com *ic)
  697 {
  698         struct ieee80211vap *vap;
  699         int flags;
  700 
  701         flags = 0;
  702         /* XXX locking */
  703         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
  704                 if (vap->iv_state < IEEE80211_S_RUN)
  705                         continue;
  706                 switch (vap->iv_opmode) {
  707                 case IEEE80211_M_WDS:
  708                 case IEEE80211_M_STA:
  709                 case IEEE80211_M_AHDEMO:
  710                 case IEEE80211_M_HOSTAP:
  711                 case IEEE80211_M_IBSS:
  712                 case IEEE80211_M_MBSS:
  713                         flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
  714                         break;
  715                 default:
  716                         break;
  717                 }
  718         }
  719         return flags;
  720 }
  721 
  722 /*
  723  * Calculate VHT channel promotion flags for all vaps.
  724  * This assumes ni_chan have been setup for each vap.
  725  */
  726 static int
  727 getvhtadjustflags(struct ieee80211com *ic)
  728 {
  729         struct ieee80211vap *vap;
  730         int flags;
  731 
  732         flags = 0;
  733         /* XXX locking */
  734         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
  735                 if (vap->iv_state < IEEE80211_S_RUN)
  736                         continue;
  737                 switch (vap->iv_opmode) {
  738                 case IEEE80211_M_WDS:
  739                 case IEEE80211_M_STA:
  740                 case IEEE80211_M_AHDEMO:
  741                 case IEEE80211_M_HOSTAP:
  742                 case IEEE80211_M_IBSS:
  743                 case IEEE80211_M_MBSS:
  744                         flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
  745                         break;
  746                 default:
  747                         break;
  748                 }
  749         }
  750         return flags;
  751 }
  752 
  753 /*
  754  * Check if the current channel needs to change based on whether
  755  * any vap's are using HT20/HT40.  This is used to sync the state
  756  * of ic_curchan after a channel width change on a running vap.
  757  *
  758  * Same applies for VHT.
  759  */
  760 void
  761 ieee80211_sync_curchan(struct ieee80211com *ic)
  762 {
  763         struct ieee80211_channel *c;
  764 
  765         c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
  766         c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
  767 
  768         if (c != ic->ic_curchan) {
  769                 ic->ic_curchan = c;
  770                 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
  771                 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
  772                 IEEE80211_UNLOCK(ic);
  773                 ic->ic_set_channel(ic);
  774                 ieee80211_radiotap_chan_change(ic);
  775                 IEEE80211_LOCK(ic);
  776         }
  777 }
  778 
  779 /*
  780  * Setup the current channel.  The request channel may be
  781  * promoted if other vap's are operating with HT20/HT40.
  782  */
  783 void
  784 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
  785 {
  786         if (ic->ic_htcaps & IEEE80211_HTC_HT) {
  787                 int flags = gethtadjustflags(ic);
  788                 /*
  789                  * Check for channel promotion required to support the
  790                  * set of running vap's.  This assumes we are called
  791                  * after ni_chan is setup for each vap.
  792                  */
  793                 /* XXX VHT? */
  794                 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
  795                 if (flags > ieee80211_htchanflags(c))
  796                         c = ieee80211_ht_adjust_channel(ic, c, flags);
  797         }
  798 
  799         /*
  800          * VHT promotion - this will at least promote to VHT20/40
  801          * based on what HT has done; it may further promote the
  802          * channel to VHT80 or above.
  803          */
  804         if (ic->ic_vhtcaps != 0) {
  805                 int flags = getvhtadjustflags(ic);
  806                 if (flags > ieee80211_vhtchanflags(c))
  807                         c = ieee80211_vht_adjust_channel(ic, c, flags);
  808         }
  809 
  810         ic->ic_bsschan = ic->ic_curchan = c;
  811         ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
  812         ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
  813 }
  814 
  815 /*
  816  * Change the current channel.  The channel change is guaranteed to have
  817  * happened before the next state change.
  818  */
  819 void
  820 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
  821 {
  822         ieee80211_setupcurchan(ic, c);
  823         ieee80211_runtask(ic, &ic->ic_chan_task);
  824 }
  825 
  826 void
  827 ieee80211_update_chw(struct ieee80211com *ic)
  828 {
  829 
  830         ieee80211_setupcurchan(ic, ic->ic_curchan);
  831         ieee80211_runtask(ic, &ic->ic_chw_task);
  832 }
  833 
  834 /*
  835  * Join the specified IBSS/BSS network.  The node is assumed to
  836  * be passed in with a held reference.
  837  */
  838 static int
  839 ieee80211_sta_join1(struct ieee80211_node *selbs)
  840 {
  841         struct ieee80211vap *vap = selbs->ni_vap;
  842         struct ieee80211com *ic = selbs->ni_ic;
  843         struct ieee80211_node *obss;
  844         int canreassoc;
  845 
  846         /*
  847          * Committed to selbs, setup state.
  848          */
  849         obss = vap->iv_bss;
  850         /*
  851          * Check if old+new node have the same address in which
  852          * case we can reassociate when operating in sta mode.
  853          */
  854         canreassoc = (obss != NULL &&
  855                 vap->iv_state == IEEE80211_S_RUN &&
  856                 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
  857         vap->iv_bss = selbs;            /* NB: caller assumed to bump refcnt */
  858         if (obss != NULL) {
  859                 struct ieee80211_node_table *nt = obss->ni_table;
  860 
  861                 copy_bss(selbs, obss);
  862                 ieee80211_node_decref(obss);    /* iv_bss reference */
  863 
  864                 IEEE80211_NODE_LOCK(nt);
  865                 node_reclaim(nt, obss);         /* station table reference */
  866                 IEEE80211_NODE_UNLOCK(nt);
  867 
  868                 obss = NULL;            /* NB: guard against later use */
  869         }
  870 
  871         /*
  872          * Delete unusable rates; we've already checked
  873          * that the negotiated rate set is acceptable.
  874          */
  875         ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
  876                 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
  877 
  878         ieee80211_setcurchan(ic, selbs->ni_chan);
  879         /*
  880          * Set the erp state (mostly the slot time) to deal with
  881          * the auto-select case; this should be redundant if the
  882          * mode is locked.
  883          */
  884         ieee80211_vap_reset_erp(vap);
  885         ieee80211_wme_initparams(vap);
  886 
  887         if (vap->iv_opmode == IEEE80211_M_STA) {
  888                 if (canreassoc) {
  889                         /* Reassociate */
  890                         ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
  891                 } else {
  892                         /*
  893                          * Act as if we received a DEAUTH frame in case we
  894                          * are invoked from the RUN state.  This will cause
  895                          * us to try to re-authenticate if we are operating
  896                          * as a station.
  897                          */
  898                         ieee80211_new_state(vap, IEEE80211_S_AUTH,
  899                                 IEEE80211_FC0_SUBTYPE_DEAUTH);
  900                 }
  901         } else
  902                 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
  903         return 1;
  904 }
  905 
  906 int
  907 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
  908         const struct ieee80211_scan_entry *se)
  909 {
  910         struct ieee80211com *ic = vap->iv_ic;
  911         struct ieee80211_node *ni;
  912         int do_ht = 0;
  913 
  914         ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
  915         if (ni == NULL) {
  916                 /* XXX msg */
  917                 return 0;
  918         }
  919 
  920         /*
  921          * Expand scan state into node's format.
  922          * XXX may not need all this stuff
  923          */
  924         IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
  925         ni->ni_esslen = se->se_ssid[1];
  926         memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
  927         ni->ni_tstamp.tsf = se->se_tstamp.tsf;
  928         ni->ni_intval = se->se_intval;
  929         ni->ni_capinfo = se->se_capinfo;
  930         ni->ni_chan = chan;
  931         ni->ni_timoff = se->se_timoff;
  932         ni->ni_fhdwell = se->se_fhdwell;
  933         ni->ni_fhindex = se->se_fhindex;
  934         ni->ni_erp = se->se_erp;
  935         IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
  936         ni->ni_noise = se->se_noise;
  937         if (vap->iv_opmode == IEEE80211_M_STA) {
  938                 /* NB: only infrastructure mode requires an associd */
  939                 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
  940         }
  941 
  942         if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
  943                 ieee80211_ies_expand(&ni->ni_ies);
  944 #ifdef IEEE80211_SUPPORT_SUPERG
  945                 if (ni->ni_ies.ath_ie != NULL)
  946                         ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
  947 #endif
  948                 if (ni->ni_ies.htcap_ie != NULL)
  949                         ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
  950                 if (ni->ni_ies.htinfo_ie != NULL)
  951                         ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
  952 #ifdef IEEE80211_SUPPORT_MESH
  953                 if (ni->ni_ies.meshid_ie != NULL)
  954                         ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
  955 #endif
  956 #ifdef IEEE80211_SUPPORT_TDMA
  957                 if (ni->ni_ies.tdma_ie != NULL)
  958                         ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
  959 #endif
  960                 if (ni->ni_ies.vhtcap_ie != NULL)
  961                         ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
  962                 if (ni->ni_ies.vhtopmode_ie != NULL)
  963                         ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
  964 
  965                 /* XXX parse BSSLOAD IE */
  966                 /* XXX parse TXPWRENV IE */
  967                 /* XXX parse APCHANREP IE */
  968         }
  969 
  970         vap->iv_dtim_period = se->se_dtimperiod;
  971         vap->iv_dtim_count = 0;
  972 
  973         /* NB: must be after ni_chan is setup */
  974         ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
  975                 IEEE80211_F_DOSORT);
  976         if (ieee80211_iserp_rateset(&ni->ni_rates))
  977                 ni->ni_flags |= IEEE80211_NODE_ERP;
  978 
  979         /*
  980          * Setup HT state for this node if it's available, otherwise
  981          * non-STA modes won't pick this state up.
  982          *
  983          * For IBSS and related modes that don't go through an
  984          * association request/response, the only appropriate place
  985          * to setup the HT state is here.
  986          */
  987         if (ni->ni_ies.htinfo_ie != NULL &&
  988             ni->ni_ies.htcap_ie != NULL &&
  989             vap->iv_flags_ht & IEEE80211_FHT_HT) {
  990                 ieee80211_ht_node_init(ni);
  991                 ieee80211_ht_updateparams(ni,
  992                     ni->ni_ies.htcap_ie,
  993                     ni->ni_ies.htinfo_ie);
  994                 do_ht = 1;
  995         }
  996 
  997         /*
  998          * Setup VHT state for this node if it's available.
  999          * Same as the above.
 1000          *
 1001          * For now, don't allow 2GHz VHT operation.
 1002          */
 1003         if (ni->ni_ies.vhtopmode_ie != NULL &&
 1004             ni->ni_ies.vhtcap_ie != NULL &&
 1005             vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
 1006                 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
 1007                         printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
 1008                             __func__,
 1009                             ni->ni_macaddr,
 1010                             ":");
 1011                 } else {
 1012                         ieee80211_vht_node_init(ni);
 1013                         ieee80211_vht_updateparams(ni,
 1014                             ni->ni_ies.vhtcap_ie,
 1015                             ni->ni_ies.vhtopmode_ie);
 1016                         ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
 1017                             ni->ni_ies.vhtopmode_ie);
 1018                         do_ht = 1;
 1019                 }
 1020         }
 1021 
 1022         /* Finally do the node channel change */
 1023         if (do_ht) {
 1024                 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
 1025                     ni->ni_ies.htinfo_ie);
 1026                 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
 1027                     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
 1028                 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
 1029         }
 1030 
 1031         /* XXX else check for ath FF? */
 1032         /* XXX QoS? Difficult given that WME config is specific to a master */
 1033 
 1034         ieee80211_node_setuptxparms(ni);
 1035         ieee80211_ratectl_node_init(ni);
 1036 
 1037         return ieee80211_sta_join1(ieee80211_ref_node(ni));
 1038 }
 1039 
 1040 /*
 1041  * Leave the specified IBSS/BSS network.  The node is assumed to
 1042  * be passed in with a held reference.
 1043  */
 1044 void
 1045 ieee80211_sta_leave(struct ieee80211_node *ni)
 1046 {
 1047         struct ieee80211com *ic = ni->ni_ic;
 1048 
 1049         ic->ic_node_cleanup(ni);
 1050         ieee80211_notify_node_leave(ni);
 1051 }
 1052 
 1053 /*
 1054  * Send a deauthenticate frame and drop the station.
 1055  */
 1056 void
 1057 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
 1058 {
 1059         /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
 1060         ieee80211_ref_node(ni);
 1061         if (ni->ni_associd != 0)
 1062                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
 1063         ieee80211_node_leave(ni);
 1064         ieee80211_free_node(ni);
 1065 }
 1066 
 1067 static struct ieee80211_node *
 1068 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1069 {
 1070         struct ieee80211_node *ni;
 1071 
 1072         ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
 1073                 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
 1074         return ni;
 1075 }
 1076 
 1077 static int
 1078 node_init(struct ieee80211_node *ni)
 1079 {
 1080         return 0;
 1081 }
 1082 
 1083 /*
 1084  * Initialize an ie blob with the specified data.  If previous
 1085  * data exists re-use the data block.  As a side effect we clear
 1086  * all references to specific ie's; the caller is required to
 1087  * recalculate them.
 1088  */
 1089 int
 1090 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
 1091 {
 1092         /* NB: assumes data+len are the last fields */
 1093         memset(ies, 0, offsetof(struct ieee80211_ies, data));
 1094         if (ies->data != NULL && ies->len != len) {
 1095                 /* data size changed */
 1096                 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
 1097                 ies->data = NULL;
 1098         }
 1099         if (ies->data == NULL) {
 1100                 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
 1101                     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
 1102                 if (ies->data == NULL) {
 1103                         ies->len = 0;
 1104                         /* NB: pointers have already been zero'd above */
 1105                         return 0;
 1106                 }
 1107         }
 1108         memcpy(ies->data, data, len);
 1109         ies->len = len;
 1110         return 1;
 1111 }
 1112 
 1113 /*
 1114  * Reclaim storage for an ie blob.
 1115  */
 1116 void
 1117 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
 1118 {
 1119         if (ies->data != NULL)
 1120                 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
 1121 }
 1122 
 1123 /*
 1124  * Expand an ie blob data contents and to fillin individual
 1125  * ie pointers.  The data blob is assumed to be well-formed;
 1126  * we don't do any validity checking of ie lengths.
 1127  */
 1128 void
 1129 ieee80211_ies_expand(struct ieee80211_ies *ies)
 1130 {
 1131         uint8_t *ie;
 1132         int ielen;
 1133 
 1134         ie = ies->data;
 1135         ielen = ies->len;
 1136         while (ielen > 1) {
 1137                 switch (ie[0]) {
 1138                 case IEEE80211_ELEMID_VENDOR:
 1139                         if (iswpaoui(ie))
 1140                                 ies->wpa_ie = ie;
 1141                         else if (iswmeoui(ie))
 1142                                 ies->wme_ie = ie;
 1143 #ifdef IEEE80211_SUPPORT_SUPERG
 1144                         else if (isatherosoui(ie))
 1145                                 ies->ath_ie = ie;
 1146 #endif
 1147 #ifdef IEEE80211_SUPPORT_TDMA
 1148                         else if (istdmaoui(ie))
 1149                                 ies->tdma_ie = ie;
 1150 #endif
 1151                         break;
 1152                 case IEEE80211_ELEMID_RSN:
 1153                         ies->rsn_ie = ie;
 1154                         break;
 1155                 case IEEE80211_ELEMID_HTCAP:
 1156                         ies->htcap_ie = ie;
 1157                         break;
 1158                 case IEEE80211_ELEMID_HTINFO:
 1159                         ies->htinfo_ie = ie;
 1160                         break;
 1161 #ifdef IEEE80211_SUPPORT_MESH
 1162                 case IEEE80211_ELEMID_MESHID:
 1163                         ies->meshid_ie = ie;
 1164                         break;
 1165 #endif
 1166                 case IEEE80211_ELEMID_VHT_CAP:
 1167                         ies->vhtcap_ie = ie;
 1168                         break;
 1169                 case IEEE80211_ELEMID_VHT_OPMODE:
 1170                         ies->vhtopmode_ie = ie;
 1171                         break;
 1172                 case IEEE80211_ELEMID_VHT_PWR_ENV:
 1173                         ies->vhtpwrenv_ie = ie;
 1174                         break;
 1175                 case IEEE80211_ELEMID_BSSLOAD:
 1176                         ies->bssload_ie = ie;
 1177                         break;
 1178                 case IEEE80211_ELEMID_APCHANREP:
 1179                         ies->apchanrep_ie = ie;
 1180                         break;
 1181                 }
 1182                 ielen -= 2 + ie[1];
 1183                 ie += 2 + ie[1];
 1184         }
 1185 }
 1186 
 1187 /*
 1188  * Reclaim any resources in a node and reset any critical
 1189  * state.  Typically nodes are free'd immediately after,
 1190  * but in some cases the storage may be reused so we need
 1191  * to insure consistent state (should probably fix that).
 1192  */
 1193 static void
 1194 node_cleanup(struct ieee80211_node *ni)
 1195 {
 1196         struct ieee80211vap *vap = ni->ni_vap;
 1197         struct ieee80211com *ic = ni->ni_ic;
 1198         int i;
 1199 
 1200         /* NB: preserve ni_table */
 1201         if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
 1202                 if (vap->iv_opmode != IEEE80211_M_STA)
 1203                         vap->iv_ps_sta--;
 1204                 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
 1205                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
 1206                     "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
 1207         }
 1208         /*
 1209          * Cleanup any VHT and HT-related state.
 1210          */
 1211         if (ni->ni_flags & IEEE80211_NODE_VHT)
 1212                 ieee80211_vht_node_cleanup(ni);
 1213         if (ni->ni_flags & IEEE80211_NODE_HT)
 1214                 ieee80211_ht_node_cleanup(ni);
 1215 #ifdef IEEE80211_SUPPORT_SUPERG
 1216         /* Always do FF node cleanup; for A-MSDU */
 1217         ieee80211_ff_node_cleanup(ni);
 1218 #endif
 1219 #ifdef IEEE80211_SUPPORT_MESH
 1220         /*
 1221          * Cleanup any mesh-related state.
 1222          */
 1223         if (vap->iv_opmode == IEEE80211_M_MBSS)
 1224                 ieee80211_mesh_node_cleanup(ni);
 1225 #endif
 1226         /*
 1227          * Clear any staging queue entries.
 1228          */
 1229         ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
 1230 
 1231         /*
 1232          * Clear AREF flag that marks the authorization refcnt bump
 1233          * has happened.  This is probably not needed as the node
 1234          * should always be removed from the table so not found but
 1235          * do it just in case.
 1236          * Likewise clear the ASSOCID flag as these flags are intended
 1237          * to be managed in tandem.
 1238          */
 1239         ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
 1240 
 1241         /*
 1242          * Drain power save queue and, if needed, clear TIM.
 1243          */
 1244         if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
 1245                 vap->iv_set_tim(ni, 0);
 1246 
 1247         ni->ni_associd = 0;
 1248         if (ni->ni_challenge != NULL) {
 1249                 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
 1250                 ni->ni_challenge = NULL;
 1251         }
 1252         /*
 1253          * Preserve SSID, WPA, and WME ie's so the bss node is
 1254          * reusable during a re-auth/re-assoc state transition.
 1255          * If we remove these data they will not be recreated
 1256          * because they come from a probe-response or beacon frame
 1257          * which cannot be expected prior to the association-response.
 1258          * This should not be an issue when operating in other modes
 1259          * as stations leaving always go through a full state transition
 1260          * which will rebuild this state.
 1261          *
 1262          * XXX does this leave us open to inheriting old state?
 1263          */
 1264         for (i = 0; i < nitems(ni->ni_rxfrag); i++)
 1265                 if (ni->ni_rxfrag[i] != NULL) {
 1266                         m_freem(ni->ni_rxfrag[i]);
 1267                         ni->ni_rxfrag[i] = NULL;
 1268                 }
 1269         /*
 1270          * Must be careful here to remove any key map entry w/o a LOR.
 1271          */
 1272         ieee80211_node_delucastkey(ni);
 1273 }
 1274 
 1275 static void
 1276 node_free(struct ieee80211_node *ni)
 1277 {
 1278         struct ieee80211com *ic = ni->ni_ic;
 1279 
 1280         ieee80211_ratectl_node_deinit(ni);
 1281         ic->ic_node_cleanup(ni);
 1282         ieee80211_ies_cleanup(&ni->ni_ies);
 1283         ieee80211_psq_cleanup(&ni->ni_psq);
 1284         IEEE80211_FREE(ni, M_80211_NODE);
 1285 }
 1286 
 1287 static void
 1288 node_age(struct ieee80211_node *ni)
 1289 {
 1290         struct ieee80211vap *vap = ni->ni_vap;
 1291 
 1292         /*
 1293          * Age frames on the power save queue.
 1294          */
 1295         if (ieee80211_node_psq_age(ni) != 0 &&
 1296             ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
 1297                 vap->iv_set_tim(ni, 0);
 1298         /*
 1299          * Age out HT resources (e.g. frames on the
 1300          * A-MPDU reorder queues).
 1301          */
 1302         if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
 1303                 ieee80211_ht_node_age(ni);
 1304 }
 1305 
 1306 static int8_t
 1307 node_getrssi(const struct ieee80211_node *ni)
 1308 {
 1309         uint32_t avgrssi = ni->ni_avgrssi;
 1310         int32_t rssi;
 1311 
 1312         if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
 1313                 return 0;
 1314         rssi = IEEE80211_RSSI_GET(avgrssi);
 1315         return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
 1316 }
 1317 
 1318 static void
 1319 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
 1320 {
 1321         *rssi = node_getrssi(ni);
 1322         *noise = ni->ni_noise;
 1323 }
 1324 
 1325 static void
 1326 node_getmimoinfo(const struct ieee80211_node *ni,
 1327         struct ieee80211_mimo_info *info)
 1328 {
 1329         int i;
 1330         uint32_t avgrssi;
 1331         int32_t rssi;
 1332 
 1333         bzero(info, sizeof(*info));
 1334 
 1335         for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
 1336                 /* Note: for now, just pri20 channel info */
 1337                 avgrssi = ni->ni_mimo_rssi_ctl[i];
 1338                 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
 1339                         info->ch[i].rssi[0] = 0;
 1340                 } else {
 1341                         rssi = IEEE80211_RSSI_GET(avgrssi);
 1342                         info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
 1343                 }
 1344                 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
 1345         }
 1346 
 1347         /* XXX ext radios? */
 1348 
 1349         /* XXX EVM? */
 1350 }
 1351 
 1352 static void
 1353 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
 1354     struct ieee80211_node *ni)
 1355 {
 1356         struct ieee80211com *ic = nt->nt_ic;
 1357         int hash;
 1358 
 1359         IEEE80211_NODE_LOCK_ASSERT(nt);
 1360 
 1361         hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
 1362         (void) ic;      /* XXX IEEE80211_NODE_HASH */
 1363         TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
 1364         LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
 1365         nt->nt_count++;
 1366         ni->ni_table = nt;
 1367 }
 1368 
 1369 static void
 1370 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
 1371     struct ieee80211_node *ni)
 1372 {
 1373 
 1374         IEEE80211_NODE_LOCK_ASSERT(nt);
 1375 
 1376         TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
 1377         LIST_REMOVE(ni, ni_hash);
 1378         nt->nt_count--;
 1379         KASSERT(nt->nt_count >= 0,
 1380             ("nt_count is negative (%d)!\n", nt->nt_count));
 1381         ni->ni_table = NULL;
 1382 }
 1383 
 1384 struct ieee80211_node *
 1385 ieee80211_alloc_node(struct ieee80211_node_table *nt,
 1386         struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1387 {
 1388         struct ieee80211com *ic = nt->nt_ic;
 1389         struct ieee80211_node *ni;
 1390 
 1391         ni = ic->ic_node_alloc(vap, macaddr);
 1392         if (ni == NULL) {
 1393                 vap->iv_stats.is_rx_nodealloc++;
 1394                 return NULL;
 1395         }
 1396 
 1397         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 1398                 "%s %p<%s> in %s table\n", __func__, ni,
 1399                 ether_sprintf(macaddr), nt->nt_name);
 1400 
 1401         IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
 1402         ieee80211_node_initref(ni);             /* mark referenced */
 1403         ni->ni_chan = IEEE80211_CHAN_ANYC;
 1404         ni->ni_authmode = IEEE80211_AUTH_OPEN;
 1405         ni->ni_txpower = ic->ic_txpowlimit;     /* max power */
 1406         ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
 1407         ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
 1408         ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
 1409         ni->ni_inact_reload = nt->nt_inact_init;
 1410         ni->ni_inact = ni->ni_inact_reload;
 1411         ni->ni_ath_defkeyix = 0x7fff;
 1412         ieee80211_psq_init(&ni->ni_psq, "unknown");
 1413 #ifdef IEEE80211_SUPPORT_MESH
 1414         if (vap->iv_opmode == IEEE80211_M_MBSS)
 1415                 ieee80211_mesh_node_init(vap, ni);
 1416 #endif
 1417         IEEE80211_NODE_LOCK(nt);
 1418         ieee80211_add_node_nt(nt, ni);
 1419         ni->ni_vap = vap;
 1420         ni->ni_ic = ic;
 1421         IEEE80211_NODE_UNLOCK(nt);
 1422 
 1423         /* handle failure; free node state */
 1424         if (ic->ic_node_init(ni) != 0) {
 1425                 vap->iv_stats.is_rx_nodealloc++;
 1426                 ieee80211_psq_cleanup(&ni->ni_psq);
 1427                 ieee80211_ratectl_node_deinit(ni);
 1428                 _ieee80211_free_node(ni);
 1429                 return NULL;
 1430         }
 1431 
 1432         IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
 1433             "%s: inact_reload %u", __func__, ni->ni_inact_reload);
 1434 
 1435         return ni;
 1436 }
 1437 
 1438 /*
 1439  * Craft a temporary node suitable for sending a management frame
 1440  * to the specified station.  We craft only as much state as we
 1441  * need to do the work since the node will be immediately reclaimed
 1442  * once the send completes.
 1443  */
 1444 struct ieee80211_node *
 1445 ieee80211_tmp_node(struct ieee80211vap *vap,
 1446         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1447 {
 1448         struct ieee80211com *ic = vap->iv_ic;
 1449         struct ieee80211_node *ni;
 1450 
 1451         ni = ic->ic_node_alloc(vap, macaddr);
 1452         if (ni != NULL) {
 1453                 struct ieee80211_node *bss = vap->iv_bss;
 1454 
 1455                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 1456                         "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
 1457 
 1458                 ni->ni_table = NULL;            /* NB: pedantic */
 1459                 ni->ni_ic = ic;                 /* NB: needed to set channel */
 1460                 ni->ni_vap = vap;
 1461 
 1462                 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
 1463                 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
 1464                 ieee80211_node_initref(ni);             /* mark referenced */
 1465                 /* NB: required by ieee80211_fix_rate */
 1466                 ieee80211_node_set_chan(ni, bss->ni_chan);
 1467                 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
 1468                         IEEE80211_KEYIX_NONE);
 1469                 ni->ni_txpower = bss->ni_txpower;
 1470                 /* XXX optimize away */
 1471                 ieee80211_psq_init(&ni->ni_psq, "unknown");
 1472 
 1473                 ieee80211_ratectl_node_init(ni);
 1474 
 1475                 /* handle failure; free node state */
 1476                 if (ic->ic_node_init(ni) != 0) {
 1477                         vap->iv_stats.is_rx_nodealloc++;
 1478                         ieee80211_psq_cleanup(&ni->ni_psq);
 1479                         ieee80211_ratectl_node_deinit(ni);
 1480                         _ieee80211_free_node(ni);
 1481                         return NULL;
 1482                 }
 1483 
 1484         } else {
 1485                 /* XXX msg */
 1486                 vap->iv_stats.is_rx_nodealloc++;
 1487         }
 1488         return ni;
 1489 }
 1490 
 1491 struct ieee80211_node *
 1492 ieee80211_dup_bss(struct ieee80211vap *vap,
 1493         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1494 {
 1495         struct ieee80211com *ic = vap->iv_ic;
 1496         struct ieee80211_node *ni;
 1497 
 1498         ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
 1499         if (ni != NULL) {
 1500                 struct ieee80211_node *bss = vap->iv_bss;
 1501                 /*
 1502                  * Inherit from iv_bss.
 1503                  */
 1504                 copy_bss(ni, bss);
 1505                 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
 1506                 ieee80211_node_set_chan(ni, bss->ni_chan);
 1507         }
 1508         return ni;
 1509 }
 1510 
 1511 /*
 1512  * Create a bss node for a legacy WDS vap.  The far end does
 1513  * not associate so we just create create a new node and
 1514  * simulate an association.  The caller is responsible for
 1515  * installing the node as the bss node and handling any further
 1516  * setup work like authorizing the port.
 1517  */
 1518 struct ieee80211_node *
 1519 ieee80211_node_create_wds(struct ieee80211vap *vap,
 1520         const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
 1521 {
 1522         struct ieee80211com *ic = vap->iv_ic;
 1523         struct ieee80211_node *ni;
 1524 
 1525         /* XXX check if node already in sta table? */
 1526         ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
 1527         if (ni != NULL) {
 1528                 ni->ni_wdsvap = vap;
 1529                 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
 1530                 /*
 1531                  * Inherit any manually configured settings.
 1532                  */
 1533                 copy_bss(ni, vap->iv_bss);
 1534                 ieee80211_node_set_chan(ni, chan);
 1535                 /* NB: propagate ssid so available to WPA supplicant */
 1536                 ni->ni_esslen = vap->iv_des_ssid[0].len;
 1537                 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
 1538                 /* NB: no associd for peer */
 1539                 /*
 1540                  * There are no management frames to use to
 1541                  * discover neighbor capabilities, so blindly
 1542                  * propagate the local configuration.
 1543                  */
 1544                 if (vap->iv_flags & IEEE80211_F_WME)
 1545                         ni->ni_flags |= IEEE80211_NODE_QOS;
 1546 #ifdef IEEE80211_SUPPORT_SUPERG
 1547                 if (vap->iv_flags & IEEE80211_F_FF)
 1548                         ni->ni_flags |= IEEE80211_NODE_FF;
 1549 #endif
 1550                 /* XXX VHT */
 1551                 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
 1552                     (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
 1553                         /*
 1554                          * Device is HT-capable and HT is enabled for
 1555                          * the vap; setup HT operation.  On return
 1556                          * ni_chan will be adjusted to an HT channel.
 1557                          */
 1558                         ieee80211_ht_wds_init(ni);
 1559                         if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
 1560                                 printf("%s: TODO: vht_wds_init\n", __func__);
 1561                         }
 1562                 } else {
 1563                         struct ieee80211_channel *c = ni->ni_chan;
 1564                         /*
 1565                          * Force a legacy channel to be used.
 1566                          */
 1567                         c = ieee80211_find_channel(ic,
 1568                             c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
 1569                         KASSERT(c != NULL, ("no legacy channel, %u/%x",
 1570                             ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
 1571                         ni->ni_chan = c;
 1572                 }
 1573         }
 1574         return ni;
 1575 }
 1576 
 1577 struct ieee80211_node *
 1578 #ifdef IEEE80211_DEBUG_REFCNT
 1579 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
 1580         const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
 1581 #else
 1582 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
 1583         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1584 #endif
 1585 {
 1586         struct ieee80211_node *ni;
 1587         int hash;
 1588 
 1589         IEEE80211_NODE_LOCK_ASSERT(nt);
 1590 
 1591         hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
 1592         LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
 1593                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
 1594                         ieee80211_ref_node(ni); /* mark referenced */
 1595 #ifdef IEEE80211_DEBUG_REFCNT
 1596                         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 1597                             "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
 1598                             func, line,
 1599                             ni, ether_sprintf(ni->ni_macaddr),
 1600                             ieee80211_node_refcnt(ni));
 1601 #endif
 1602                         return ni;
 1603                 }
 1604         }
 1605         return NULL;
 1606 }
 1607 
 1608 struct ieee80211_node *
 1609 #ifdef IEEE80211_DEBUG_REFCNT
 1610 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
 1611         const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
 1612 #else
 1613 ieee80211_find_node(struct ieee80211_node_table *nt,
 1614         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1615 #endif
 1616 {
 1617         struct ieee80211_node *ni;
 1618 
 1619         IEEE80211_NODE_LOCK(nt);
 1620         ni = ieee80211_find_node_locked(nt, macaddr);
 1621         IEEE80211_NODE_UNLOCK(nt);
 1622         return ni;
 1623 }
 1624 
 1625 struct ieee80211_node *
 1626 #ifdef IEEE80211_DEBUG_REFCNT
 1627 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
 1628         const struct ieee80211vap *vap,
 1629         const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
 1630 #else
 1631 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
 1632         const struct ieee80211vap *vap,
 1633         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1634 #endif
 1635 {
 1636         struct ieee80211_node *ni;
 1637         int hash;
 1638 
 1639         IEEE80211_NODE_LOCK_ASSERT(nt);
 1640 
 1641         hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
 1642         LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
 1643                 if (ni->ni_vap == vap &&
 1644                     IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
 1645                         ieee80211_ref_node(ni); /* mark referenced */
 1646 #ifdef IEEE80211_DEBUG_REFCNT
 1647                         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 1648                             "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
 1649                             func, line,
 1650                             ni, ether_sprintf(ni->ni_macaddr),
 1651                             ieee80211_node_refcnt(ni));
 1652 #endif
 1653                         return ni;
 1654                 }
 1655         }
 1656         return NULL;
 1657 }
 1658 
 1659 struct ieee80211_node *
 1660 #ifdef IEEE80211_DEBUG_REFCNT
 1661 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
 1662         const struct ieee80211vap *vap,
 1663         const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
 1664 #else
 1665 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
 1666         const struct ieee80211vap *vap,
 1667         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1668 #endif
 1669 {
 1670         struct ieee80211_node *ni;
 1671 
 1672         IEEE80211_NODE_LOCK(nt);
 1673         ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
 1674         IEEE80211_NODE_UNLOCK(nt);
 1675         return ni;
 1676 }
 1677 
 1678 /*
 1679  * Fake up a node; this handles node discovery in adhoc mode.
 1680  * Note that for the driver's benefit we we treat this like
 1681  * an association so the driver has an opportunity to setup
 1682  * it's private state.
 1683  */
 1684 struct ieee80211_node *
 1685 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
 1686         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1687 {
 1688         struct ieee80211_node *ni;
 1689 
 1690         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
 1691             "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
 1692         ni = ieee80211_dup_bss(vap, macaddr);
 1693         if (ni != NULL) {
 1694                 struct ieee80211com *ic = vap->iv_ic;
 1695 
 1696                 /* XXX no rate negotiation; just dup */
 1697                 ni->ni_rates = vap->iv_bss->ni_rates;
 1698                 if (ieee80211_iserp_rateset(&ni->ni_rates))
 1699                         ni->ni_flags |= IEEE80211_NODE_ERP;
 1700                 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
 1701                         /*
 1702                          * In adhoc demo mode there are no management
 1703                          * frames to use to discover neighbor capabilities,
 1704                          * so blindly propagate the local configuration
 1705                          * so we can do interesting things (e.g. use
 1706                          * WME to disable ACK's).
 1707                          */
 1708                         /*
 1709                          * XXX TODO: 11n?
 1710                          */
 1711                         if (vap->iv_flags & IEEE80211_F_WME)
 1712                                 ni->ni_flags |= IEEE80211_NODE_QOS;
 1713 #ifdef IEEE80211_SUPPORT_SUPERG
 1714                         if (vap->iv_flags & IEEE80211_F_FF)
 1715                                 ni->ni_flags |= IEEE80211_NODE_FF;
 1716 #endif
 1717                 }
 1718                 ieee80211_node_setuptxparms(ni);
 1719                 ieee80211_ratectl_node_init(ni);
 1720 
 1721                 /*
 1722                  * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
 1723                  * not A-MPDU TX; not 11n rates, etc.  We'll cycle
 1724                  * that after we hear that we can indeed do 11n
 1725                  * (either by a beacon frame or by a probe response.)
 1726                  */
 1727 
 1728                 /*
 1729                  * This is the first time we see the node.
 1730                  */
 1731                 if (ic->ic_newassoc != NULL)
 1732                         ic->ic_newassoc(ni, 1);
 1733 
 1734                 /*
 1735                  * Kick off a probe request to the given node;
 1736                  * we will then use the probe response to update
 1737                  * 11n/etc configuration state.
 1738                  *
 1739                  * XXX TODO: this isn't guaranteed, and until we get
 1740                  * a probe response, we won't be able to actually
 1741                  * do anything 802.11n related to the node.
 1742                  * So if this does indeed work, maybe we should hold
 1743                  * off on sending responses until we get the probe
 1744                  * response, or just default to some sensible subset
 1745                  * of 802.11n behaviour (eg always allow aggregation
 1746                  * negotiation TO us, but not FROM us, etc) so we
 1747                  * aren't entirely busted.
 1748                  */
 1749                 if (vap->iv_opmode == IEEE80211_M_IBSS) {
 1750                         ieee80211_send_probereq(ni, /* node */
 1751                                 vap->iv_myaddr, /* SA */
 1752                                 ni->ni_macaddr, /* DA */
 1753                                 vap->iv_bss->ni_bssid, /* BSSID */
 1754                                 vap->iv_bss->ni_essid,
 1755                                 vap->iv_bss->ni_esslen); /* SSID */
 1756                 }
 1757 
 1758                 /* XXX not right for 802.1x/WPA */
 1759                 ieee80211_node_authorize(ni);
 1760         }
 1761         return ni;
 1762 }
 1763 
 1764 void
 1765 ieee80211_init_neighbor(struct ieee80211_node *ni,
 1766         const struct ieee80211_frame *wh,
 1767         const struct ieee80211_scanparams *sp)
 1768 {
 1769         int do_ht_setup = 0, do_vht_setup = 0;
 1770 
 1771         ni->ni_esslen = sp->ssid[1];
 1772         memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
 1773         IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
 1774         memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
 1775         ni->ni_intval = sp->bintval;
 1776         ni->ni_capinfo = sp->capinfo;
 1777         ni->ni_chan = ni->ni_ic->ic_curchan;
 1778         ni->ni_fhdwell = sp->fhdwell;
 1779         ni->ni_fhindex = sp->fhindex;
 1780         ni->ni_erp = sp->erp;
 1781         ni->ni_timoff = sp->timoff;
 1782 #ifdef IEEE80211_SUPPORT_MESH
 1783         if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
 1784                 ieee80211_mesh_init_neighbor(ni, wh, sp);
 1785 #endif
 1786         if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
 1787                 ieee80211_ies_expand(&ni->ni_ies);
 1788                 if (ni->ni_ies.wme_ie != NULL)
 1789                         ni->ni_flags |= IEEE80211_NODE_QOS;
 1790                 else
 1791                         ni->ni_flags &= ~IEEE80211_NODE_QOS;
 1792 #ifdef IEEE80211_SUPPORT_SUPERG
 1793                 if (ni->ni_ies.ath_ie != NULL)
 1794                         ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
 1795 #endif
 1796                 if (ni->ni_ies.htcap_ie != NULL)
 1797                         ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
 1798                 if (ni->ni_ies.htinfo_ie != NULL)
 1799                         ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
 1800 
 1801                 if (ni->ni_ies.vhtcap_ie != NULL)
 1802                         ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
 1803                 if (ni->ni_ies.vhtopmode_ie != NULL)
 1804                         ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
 1805 
 1806                 if ((ni->ni_ies.htcap_ie != NULL) &&
 1807                     (ni->ni_ies.htinfo_ie != NULL) &&
 1808                     (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
 1809                         do_ht_setup = 1;
 1810                 }
 1811 
 1812                 if ((ni->ni_ies.vhtcap_ie != NULL) &&
 1813                     (ni->ni_ies.vhtopmode_ie != NULL) &&
 1814                     (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
 1815                         do_vht_setup = 1;
 1816                 }
 1817         }
 1818 
 1819         /* NB: must be after ni_chan is setup */
 1820         ieee80211_setup_rates(ni, sp->rates, sp->xrates,
 1821                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
 1822                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
 1823 
 1824         /*
 1825          * If the neighbor is HT compatible, flip that on.
 1826          */
 1827         if (do_ht_setup) {
 1828                 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
 1829                     "%s: doing HT setup\n", __func__);
 1830                 ieee80211_ht_node_init(ni);
 1831                 ieee80211_ht_updateparams(ni,
 1832                     ni->ni_ies.htcap_ie,
 1833                     ni->ni_ies.htinfo_ie);
 1834 
 1835                 if (do_vht_setup) {
 1836                         if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
 1837                                 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
 1838                                     __func__,
 1839                                     ni->ni_macaddr,
 1840                                     ":");
 1841                         } else {
 1842                                 ieee80211_vht_node_init(ni);
 1843                                 ieee80211_vht_updateparams(ni,
 1844                                     ni->ni_ies.vhtcap_ie,
 1845                                     ni->ni_ies.vhtopmode_ie);
 1846                                 ieee80211_setup_vht_rates(ni,
 1847                                     ni->ni_ies.vhtcap_ie,
 1848                                     ni->ni_ies.vhtopmode_ie);
 1849                         }
 1850                 }
 1851 
 1852                 /*
 1853                  * Finally do the channel upgrade/change based
 1854                  * on the HT/VHT configuration.
 1855                  */
 1856                 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
 1857                     ni->ni_ies.htinfo_ie);
 1858                 ieee80211_setup_htrates(ni,
 1859                     ni->ni_ies.htcap_ie,
 1860                     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
 1861                 ieee80211_setup_basic_htrates(ni,
 1862                     ni->ni_ies.htinfo_ie);
 1863 
 1864                 ieee80211_node_setuptxparms(ni);
 1865                 ieee80211_ratectl_node_init(ni);
 1866 
 1867                 /* Reassociate; we're now 11n/11ac */
 1868                 /*
 1869                  * XXX TODO: this is the wrong thing to do -
 1870                  * we're calling it with isnew=1 so the ath(4)
 1871                  * driver reinitialises the rate tables.
 1872                  * This "mostly" works for ath(4), but it won't
 1873                  * be right for firmware devices which allocate
 1874                  * node states.
 1875                  *
 1876                  * So, do we just create a new node and delete
 1877                  * the old one? Or?
 1878                  */
 1879                 if (ni->ni_ic->ic_newassoc)
 1880                         ni->ni_ic->ic_newassoc(ni, 1);
 1881         }
 1882 }
 1883 
 1884 /*
 1885  * Do node discovery in adhoc mode on receipt of a beacon
 1886  * or probe response frame.  Note that for the driver's
 1887  * benefit we we treat this like an association so the
 1888  * driver has an opportunity to setup it's private state.
 1889  */
 1890 struct ieee80211_node *
 1891 ieee80211_add_neighbor(struct ieee80211vap *vap,
 1892         const struct ieee80211_frame *wh,
 1893         const struct ieee80211_scanparams *sp)
 1894 {
 1895         struct ieee80211_node *ni;
 1896 
 1897         IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
 1898             "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
 1899         ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
 1900         if (ni != NULL) {
 1901                 struct ieee80211com *ic = vap->iv_ic;
 1902 
 1903                 ieee80211_init_neighbor(ni, wh, sp);
 1904                 if (ieee80211_iserp_rateset(&ni->ni_rates))
 1905                         ni->ni_flags |= IEEE80211_NODE_ERP;
 1906                 ieee80211_node_setuptxparms(ni);
 1907                 ieee80211_ratectl_node_init(ni);
 1908                 if (ic->ic_newassoc != NULL)
 1909                         ic->ic_newassoc(ni, 1);
 1910                 /* XXX not right for 802.1x/WPA */
 1911                 ieee80211_node_authorize(ni);
 1912         }
 1913         return ni;
 1914 }
 1915 
 1916 #define IS_PROBEREQ(wh) \
 1917         ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
 1918             == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
 1919 #define IS_BCAST_PROBEREQ(wh) \
 1920         (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
 1921             ((const struct ieee80211_frame *)(wh))->i_addr3))
 1922 
 1923 static __inline struct ieee80211_node *
 1924 _find_rxnode(struct ieee80211_node_table *nt,
 1925     const struct ieee80211_frame_min *wh)
 1926 {
 1927         if (IS_BCAST_PROBEREQ(wh))
 1928                 return NULL;            /* spam bcast probe req to all vap's */
 1929         return ieee80211_find_node_locked(nt, wh->i_addr2);
 1930 }
 1931 
 1932 /*
 1933  * Locate the node for sender, track state, and then pass the
 1934  * (referenced) node up to the 802.11 layer for its use.  Note
 1935  * we can return NULL if the sender is not in the table.
 1936  */
 1937 struct ieee80211_node *
 1938 #ifdef IEEE80211_DEBUG_REFCNT
 1939 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
 1940         const struct ieee80211_frame_min *wh, const char *func, int line)
 1941 #else
 1942 ieee80211_find_rxnode(struct ieee80211com *ic,
 1943         const struct ieee80211_frame_min *wh)
 1944 #endif
 1945 {
 1946         struct ieee80211_node_table *nt;
 1947         struct ieee80211_node *ni;
 1948 
 1949         nt = &ic->ic_sta;
 1950         IEEE80211_NODE_LOCK(nt);
 1951         ni = _find_rxnode(nt, wh);
 1952         IEEE80211_NODE_UNLOCK(nt);
 1953 
 1954         return ni;
 1955 }
 1956 
 1957 /*
 1958  * Like ieee80211_find_rxnode but use the supplied h/w
 1959  * key index as a hint to locate the node in the key
 1960  * mapping table.  If an entry is present at the key
 1961  * index we return it; otherwise do a normal lookup and
 1962  * update the mapping table if the station has a unicast
 1963  * key assigned to it.
 1964  */
 1965 struct ieee80211_node *
 1966 #ifdef IEEE80211_DEBUG_REFCNT
 1967 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
 1968         const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
 1969         const char *func, int line)
 1970 #else
 1971 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
 1972         const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
 1973 #endif
 1974 {
 1975         struct ieee80211_node_table *nt;
 1976         struct ieee80211_node *ni;
 1977 
 1978         nt = &ic->ic_sta;
 1979         IEEE80211_NODE_LOCK(nt);
 1980         if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
 1981                 ni = nt->nt_keyixmap[keyix];
 1982         else
 1983                 ni = NULL;
 1984         if (ni == NULL) {
 1985                 ni = _find_rxnode(nt, wh);
 1986                 if (ni != NULL && nt->nt_keyixmap != NULL) {
 1987                         /*
 1988                          * If the station has a unicast key cache slot
 1989                          * assigned update the key->node mapping table.
 1990                          */
 1991                         keyix = ni->ni_ucastkey.wk_rxkeyix;
 1992                         /* XXX can keyixmap[keyix] != NULL? */
 1993                         if (keyix < nt->nt_keyixmax &&
 1994                             nt->nt_keyixmap[keyix] == NULL) {
 1995                                 IEEE80211_DPRINTF(ni->ni_vap,
 1996                                     IEEE80211_MSG_NODE,
 1997                                     "%s: add key map entry %p<%s> refcnt %d\n",
 1998                                     __func__, ni, ether_sprintf(ni->ni_macaddr),
 1999                                     ieee80211_node_refcnt(ni)+1);
 2000                                 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
 2001                         }
 2002                 }
 2003         } else {
 2004                 if (IS_BCAST_PROBEREQ(wh))
 2005                         ni = NULL;      /* spam bcast probe req to all vap's */
 2006                 else
 2007                         ieee80211_ref_node(ni);
 2008         }
 2009         IEEE80211_NODE_UNLOCK(nt);
 2010 
 2011         return ni;
 2012 }
 2013 #undef IS_BCAST_PROBEREQ
 2014 #undef IS_PROBEREQ
 2015 
 2016 /*
 2017  * Return a reference to the appropriate node for sending
 2018  * a data frame.  This handles node discovery in adhoc networks.
 2019  */
 2020 struct ieee80211_node *
 2021 #ifdef IEEE80211_DEBUG_REFCNT
 2022 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
 2023         const uint8_t macaddr[IEEE80211_ADDR_LEN],
 2024         const char *func, int line)
 2025 #else
 2026 ieee80211_find_txnode(struct ieee80211vap *vap,
 2027         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 2028 #endif
 2029 {
 2030         struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
 2031         struct ieee80211_node *ni;
 2032 
 2033         /*
 2034          * The destination address should be in the node table
 2035          * unless this is a multicast/broadcast frame.  We can
 2036          * also optimize station mode operation, all frames go
 2037          * to the bss node.
 2038          */
 2039         /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
 2040         IEEE80211_NODE_LOCK(nt);
 2041         if (vap->iv_opmode == IEEE80211_M_STA ||
 2042             vap->iv_opmode == IEEE80211_M_WDS ||
 2043             IEEE80211_IS_MULTICAST(macaddr))
 2044                 ni = ieee80211_ref_node(vap->iv_bss);
 2045         else
 2046                 ni = ieee80211_find_node_locked(nt, macaddr);
 2047         IEEE80211_NODE_UNLOCK(nt);
 2048 
 2049         if (ni == NULL) {
 2050                 if (vap->iv_opmode == IEEE80211_M_IBSS ||
 2051                     vap->iv_opmode == IEEE80211_M_AHDEMO) {
 2052                         /*
 2053                          * In adhoc mode cons up a node for the destination.
 2054                          * Note that we need an additional reference for the
 2055                          * caller to be consistent with
 2056                          * ieee80211_find_node_locked.
 2057                          */
 2058                         /*
 2059                          * XXX TODO: this doesn't fake up 11n state; we need
 2060                          * to find another way to get it upgraded.
 2061                          */
 2062                         ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
 2063                         if (ni != NULL)
 2064                                 (void) ieee80211_ref_node(ni);
 2065                 } else {
 2066                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
 2067                             "no node, discard frame (%s)", __func__);
 2068                         vap->iv_stats.is_tx_nonode++;
 2069                 }
 2070         }
 2071         return ni;
 2072 }
 2073 
 2074 static void
 2075 _ieee80211_free_node(struct ieee80211_node *ni)
 2076 {
 2077         struct ieee80211_node_table *nt = ni->ni_table;
 2078 
 2079         /*
 2080          * NB: careful about referencing the vap as it may be
 2081          * gone if the last reference was held by a driver.
 2082          * We know the com will always be present so it's safe
 2083          * to use ni_ic below to reclaim resources.
 2084          */
 2085 #if 0
 2086         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2087                 "%s %p<%s> in %s table\n", __func__, ni,
 2088                 ether_sprintf(ni->ni_macaddr),
 2089                 nt != NULL ? nt->nt_name : "<gone>");
 2090 #endif
 2091         if (ni->ni_associd != 0) {
 2092                 struct ieee80211vap *vap = ni->ni_vap;
 2093                 if (vap->iv_aid_bitmap != NULL)
 2094                         IEEE80211_AID_CLR(vap, ni->ni_associd);
 2095         }
 2096         if (nt != NULL)
 2097                 ieee80211_del_node_nt(nt, ni);
 2098         ni->ni_ic->ic_node_free(ni);
 2099 }
 2100 
 2101 /*
 2102  * Clear any entry in the unicast key mapping table.
 2103  */
 2104 static int
 2105 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
 2106 {
 2107         ieee80211_keyix keyix;
 2108 
 2109         keyix = ni->ni_ucastkey.wk_rxkeyix;
 2110         if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
 2111             nt->nt_keyixmap[keyix] == ni) {
 2112                 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 2113                         "%s: %p<%s> clear key map entry %u\n",
 2114                         __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
 2115                 nt->nt_keyixmap[keyix] = NULL;
 2116                 ieee80211_node_decref(ni);
 2117                 return 1;
 2118         }
 2119 
 2120         return 0;
 2121 }
 2122 
 2123 void
 2124 #ifdef IEEE80211_DEBUG_REFCNT
 2125 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
 2126 #else
 2127 ieee80211_free_node(struct ieee80211_node *ni)
 2128 #endif
 2129 {
 2130         struct ieee80211_node_table *nt = ni->ni_table;
 2131 
 2132 #ifdef IEEE80211_DEBUG_REFCNT
 2133         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 2134                 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
 2135                  ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
 2136 #endif
 2137         if (nt != NULL) {
 2138                 IEEE80211_NODE_LOCK(nt);
 2139                 if (ieee80211_node_dectestref(ni)) {
 2140                         /*
 2141                          * Last reference, reclaim state.
 2142                          */
 2143                         _ieee80211_free_node(ni);
 2144                 } else if (ieee80211_node_refcnt(ni) == 1)
 2145                         if (node_clear_keyixmap(nt, ni))
 2146                                 _ieee80211_free_node(ni);
 2147                 IEEE80211_NODE_UNLOCK(nt);
 2148         } else {
 2149                 if (ieee80211_node_dectestref(ni))
 2150                         _ieee80211_free_node(ni);
 2151         }
 2152 }
 2153 
 2154 /*
 2155  * Reclaim a unicast key and clear any key cache state.
 2156  */
 2157 int
 2158 ieee80211_node_delucastkey(struct ieee80211_node *ni)
 2159 {
 2160         struct ieee80211com *ic = ni->ni_ic;
 2161         struct ieee80211_node_table *nt = &ic->ic_sta;
 2162         struct ieee80211_node *nikey;
 2163         ieee80211_keyix keyix;
 2164         int isowned, status;
 2165 
 2166         /*
 2167          * NB: We must beware of LOR here; deleting the key
 2168          * can cause the crypto layer to block traffic updates
 2169          * which can generate a LOR against the node table lock;
 2170          * grab it here and stash the key index for our use below.
 2171          *
 2172          * Must also beware of recursion on the node table lock.
 2173          * When called from node_cleanup we may already have
 2174          * the node table lock held.  Unfortunately there's no
 2175          * way to separate out this path so we must do this
 2176          * conditionally.
 2177          */
 2178         isowned = IEEE80211_NODE_IS_LOCKED(nt);
 2179         if (!isowned)
 2180                 IEEE80211_NODE_LOCK(nt);
 2181         nikey = NULL;
 2182         status = 1;             /* NB: success */
 2183         if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
 2184                 keyix = ni->ni_ucastkey.wk_rxkeyix;
 2185                 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
 2186                 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
 2187                         nikey = nt->nt_keyixmap[keyix];
 2188                         nt->nt_keyixmap[keyix] = NULL;
 2189                 }
 2190         }
 2191         if (!isowned)
 2192                 IEEE80211_NODE_UNLOCK(nt);
 2193 
 2194         if (nikey != NULL) {
 2195                 KASSERT(nikey == ni,
 2196                         ("key map out of sync, ni %p nikey %p", ni, nikey));
 2197                 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 2198                         "%s: delete key map entry %p<%s> refcnt %d\n",
 2199                         __func__, ni, ether_sprintf(ni->ni_macaddr),
 2200                         ieee80211_node_refcnt(ni)-1);
 2201                 ieee80211_free_node(ni);
 2202         }
 2203         return status;
 2204 }
 2205 
 2206 /*
 2207  * Reclaim a node.  If this is the last reference count then
 2208  * do the normal free work.  Otherwise remove it from the node
 2209  * table and mark it gone by clearing the back-reference.
 2210  */
 2211 static void
 2212 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
 2213 {
 2214 
 2215         IEEE80211_NODE_LOCK_ASSERT(nt);
 2216 
 2217         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
 2218                 "%s: remove %p<%s> from %s table, refcnt %d\n",
 2219                 __func__, ni, ether_sprintf(ni->ni_macaddr),
 2220                 nt->nt_name, ieee80211_node_refcnt(ni)-1);
 2221         /*
 2222          * Clear any entry in the unicast key mapping table.
 2223          * We need to do it here so rx lookups don't find it
 2224          * in the mapping table even if it's not in the hash
 2225          * table.  We cannot depend on the mapping table entry
 2226          * being cleared because the node may not be free'd.
 2227          */
 2228         (void)node_clear_keyixmap(nt, ni);
 2229         if (!ieee80211_node_dectestref(ni)) {
 2230                 /*
 2231                  * Other references are present, just remove the
 2232                  * node from the table so it cannot be found.  When
 2233                  * the references are dropped storage will be
 2234                  * reclaimed.
 2235                  */
 2236                 ieee80211_del_node_nt(nt, ni);
 2237         } else
 2238                 _ieee80211_free_node(ni);
 2239 }
 2240 
 2241 /*
 2242  * Node table support.
 2243  */
 2244 
 2245 static void
 2246 ieee80211_node_table_init(struct ieee80211com *ic,
 2247         struct ieee80211_node_table *nt,
 2248         const char *name, int inact, int keyixmax)
 2249 {
 2250 
 2251         nt->nt_ic = ic;
 2252         IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
 2253         TAILQ_INIT(&nt->nt_node);
 2254         nt->nt_count = 0;
 2255         nt->nt_name = name;
 2256         nt->nt_inact_init = inact;
 2257         nt->nt_keyixmax = keyixmax;
 2258         if (nt->nt_keyixmax > 0) {
 2259                 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
 2260                         keyixmax * sizeof(struct ieee80211_node *),
 2261                         M_80211_NODE,
 2262                         IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
 2263                 if (nt->nt_keyixmap == NULL)
 2264                         ic_printf(ic,
 2265                             "Cannot allocate key index map with %u entries\n",
 2266                             keyixmax);
 2267         } else
 2268                 nt->nt_keyixmap = NULL;
 2269 }
 2270 
 2271 static void
 2272 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
 2273         struct ieee80211vap *match)
 2274 {
 2275         struct ieee80211_node *ni, *next;
 2276 
 2277         IEEE80211_NODE_LOCK(nt);
 2278         TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
 2279                 if (match != NULL && ni->ni_vap != match)
 2280                         continue;
 2281                 /* XXX can this happen?  if so need's work */
 2282                 if (ni->ni_associd != 0) {
 2283                         struct ieee80211vap *vap = ni->ni_vap;
 2284 
 2285                         if (vap->iv_auth->ia_node_leave != NULL)
 2286                                 vap->iv_auth->ia_node_leave(ni);
 2287                         if (vap->iv_aid_bitmap != NULL)
 2288                                 IEEE80211_AID_CLR(vap, ni->ni_associd);
 2289                 }
 2290                 ni->ni_wdsvap = NULL;           /* clear reference */
 2291                 node_reclaim(nt, ni);
 2292         }
 2293         if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
 2294                 /*
 2295                  * Make a separate pass to clear references to this vap
 2296                  * held by DWDS entries.  They will not be matched above
 2297                  * because ni_vap will point to the ap vap but we still
 2298                  * need to clear ni_wdsvap when the WDS vap is destroyed
 2299                  * and/or reset.
 2300                  */
 2301                 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
 2302                         if (ni->ni_wdsvap == match)
 2303                                 ni->ni_wdsvap = NULL;
 2304         }
 2305         IEEE80211_NODE_UNLOCK(nt);
 2306 }
 2307 
 2308 static void
 2309 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
 2310 {
 2311         ieee80211_node_table_reset(nt, NULL);
 2312         if (nt->nt_keyixmap != NULL) {
 2313 #ifdef DIAGNOSTIC
 2314                 /* XXX verify all entries are NULL */
 2315                 int i;
 2316                 for (i = 0; i < nt->nt_keyixmax; i++)
 2317                         if (nt->nt_keyixmap[i] != NULL)
 2318                                 printf("%s: %s[%u] still active\n", __func__,
 2319                                         nt->nt_name, i);
 2320 #endif
 2321                 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
 2322                 nt->nt_keyixmap = NULL;
 2323         }
 2324         IEEE80211_NODE_LOCK_DESTROY(nt);
 2325 }
 2326 
 2327 static void
 2328 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
 2329 {
 2330         struct ieee80211com *ic = ni->ni_ic;
 2331         struct ieee80211vap *vap = ni->ni_vap;
 2332 
 2333         /*
 2334          * Only process stations when in RUN state.  This
 2335          * insures, for example, that we don't timeout an
 2336          * inactive station during CAC.  Note that CSA state
 2337          * is actually handled in ieee80211_node_timeout as
 2338          * it applies to more than timeout processing.
 2339          */
 2340         if (vap->iv_state != IEEE80211_S_RUN)
 2341                 return;
 2342         /*
 2343          * Ignore entries for which have yet to receive an
 2344          * authentication frame.  These are transient and
 2345          * will be reclaimed when the last reference to them
 2346          * goes away (when frame xmits complete).
 2347          */
 2348         if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
 2349              vap->iv_opmode == IEEE80211_M_STA) &&
 2350             (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
 2351                 return;
 2352         /*
 2353          * Free fragment if not needed anymore
 2354          * (last fragment older than 1s).
 2355          * XXX doesn't belong here, move to node_age
 2356          */
 2357         if (ni->ni_rxfrag[0] != NULL &&
 2358             ticks > ni->ni_rxfragstamp + hz) {
 2359                 m_freem(ni->ni_rxfrag[0]);
 2360                 ni->ni_rxfrag[0] = NULL;
 2361         }
 2362         if (ni->ni_inact > 0) {
 2363                 ni->ni_inact--;
 2364                 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
 2365                     "%s: inact %u inact_reload %u nrates %u",
 2366                     __func__, ni->ni_inact, ni->ni_inact_reload,
 2367                     ni->ni_rates.rs_nrates);
 2368         }
 2369         /*
 2370          * Special case ourself; we may be idle for extended periods
 2371          * of time and regardless reclaiming our state is wrong.
 2372          * XXX run ic_node_age
 2373          */
 2374         /* XXX before inact decrement? */
 2375         if (ni == vap->iv_bss)
 2376                 return;
 2377         if (ni->ni_associd != 0 ||
 2378             (vap->iv_opmode == IEEE80211_M_IBSS ||
 2379              vap->iv_opmode == IEEE80211_M_AHDEMO)) {
 2380                 /*
 2381                  * Age/drain resources held by the station.
 2382                  */
 2383                 ic->ic_node_age(ni);
 2384                 /*
 2385                  * Probe the station before time it out.  We
 2386                  * send a null data frame which may not be
 2387                  * universally supported by drivers (need it
 2388                  * for ps-poll support so it should be...).
 2389                  *
 2390                  * XXX don't probe the station unless we've
 2391                  *     received a frame from them (and have
 2392                  *     some idea of the rates they are capable
 2393                  *     of); this will get fixed more properly
 2394                  *     soon with better handling of the rate set.
 2395                  */
 2396                 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
 2397                     (0 < ni->ni_inact &&
 2398                      ni->ni_inact <= vap->iv_inact_probe) &&
 2399                     ni->ni_rates.rs_nrates != 0) {
 2400                         IEEE80211_NOTE(vap,
 2401                             IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
 2402                             ni, "%s",
 2403                             "probe station due to inactivity");
 2404                         /*
 2405                          * Grab a reference so the node cannot
 2406                          * be reclaimed before we send the frame.
 2407                          * ieee80211_send_nulldata understands
 2408                          * we've done this and reclaims the
 2409                          * ref for us as needed.
 2410                          */
 2411                         /* XXX fix this (not required anymore). */
 2412                         ieee80211_ref_node(ni);
 2413                         /* XXX useless */
 2414                         ieee80211_send_nulldata(ni);
 2415                         /* XXX stat? */
 2416                         return;
 2417                 }
 2418         }
 2419         if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
 2420             ni->ni_inact <= 0) {
 2421                 IEEE80211_NOTE(vap,
 2422                     IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
 2423                     "station timed out due to inactivity "
 2424                     "(refcnt %u)", ieee80211_node_refcnt(ni));
 2425                 /*
 2426                  * Send a deauthenticate frame and drop the station.
 2427                  * This is somewhat complicated due to reference counts
 2428                  * and locking.  At this point a station will typically
 2429                  * have a reference count of 2.  ieee80211_node_leave
 2430                  * will do a "free" of the node which will drop the
 2431                  * reference count.  But in the meantime a reference
 2432                  * wil be held by the deauth frame.  The actual reclaim
 2433                  * of the node will happen either after the tx is
 2434                  * completed or by ieee80211_node_leave.
 2435                  */
 2436                 if (ni->ni_associd != 0) {
 2437                         IEEE80211_SEND_MGMT(ni,
 2438                             IEEE80211_FC0_SUBTYPE_DEAUTH,
 2439                             IEEE80211_REASON_AUTH_EXPIRE);
 2440                 }
 2441                 ieee80211_node_leave(ni);
 2442                 vap->iv_stats.is_node_timeout++;
 2443         }
 2444 }
 2445 
 2446 /*
 2447  * Timeout inactive stations and do related housekeeping.
 2448  */
 2449 static void
 2450 ieee80211_timeout_stations(struct ieee80211com *ic)
 2451 {
 2452         struct ieee80211_node_table *nt = &ic->ic_sta;
 2453 
 2454         ieee80211_iterate_nodes(nt, timeout_stations, NULL);
 2455 }
 2456 
 2457 /*
 2458  * Aggressively reclaim resources.  This should be used
 2459  * only in a critical situation to reclaim mbuf resources.
 2460  */
 2461 void
 2462 ieee80211_drain(struct ieee80211com *ic)
 2463 {
 2464         struct ieee80211_node_table *nt = &ic->ic_sta;
 2465         struct ieee80211vap *vap;
 2466         struct ieee80211_node *ni;
 2467 
 2468         IEEE80211_NODE_LOCK(nt);
 2469         TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 2470                 /*
 2471                  * Ignore entries for which have yet to receive an
 2472                  * authentication frame.  These are transient and
 2473                  * will be reclaimed when the last reference to them
 2474                  * goes away (when frame xmits complete).
 2475                  */
 2476                 vap = ni->ni_vap;
 2477                 /*
 2478                  * Only process stations when in RUN state.  This
 2479                  * insures, for example, that we don't timeout an
 2480                  * inactive station during CAC.  Note that CSA state
 2481                  * is actually handled in ieee80211_node_timeout as
 2482                  * it applies to more than timeout processing.
 2483                  */
 2484                 if (vap->iv_state != IEEE80211_S_RUN)
 2485                         continue;
 2486                 /* XXX can vap be NULL? */
 2487                 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
 2488                      vap->iv_opmode == IEEE80211_M_STA) &&
 2489                     (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
 2490                         continue;
 2491                 /*
 2492                  * Free fragments.
 2493                  * XXX doesn't belong here, move to node_drain
 2494                  */
 2495                 if (ni->ni_rxfrag[0] != NULL) {
 2496                         m_freem(ni->ni_rxfrag[0]);
 2497                         ni->ni_rxfrag[0] = NULL;
 2498                 }
 2499                 /*
 2500                  * Drain resources held by the station.
 2501                  */
 2502                 ic->ic_node_drain(ni);
 2503         }
 2504         IEEE80211_NODE_UNLOCK(nt);
 2505 }
 2506 
 2507 /*
 2508  * Per-ieee80211vap inactivity timer callback.
 2509  */
 2510 static void
 2511 ieee80211_vap_timeout(struct ieee80211vap *vap)
 2512 {
 2513 
 2514         IEEE80211_LOCK_ASSERT(vap->iv_ic);
 2515 
 2516         ieee80211_vap_erp_timeout(vap);
 2517         ieee80211_ht_timeout(vap);
 2518         ieee80211_vht_timeout(vap);
 2519 }
 2520 
 2521 /*
 2522  * Per-ieee80211com inactivity timer callback.
 2523  */
 2524 void
 2525 ieee80211_node_timeout(void *arg)
 2526 {
 2527         struct ieee80211com *ic = arg;
 2528         struct ieee80211vap *vap;
 2529 
 2530         /*
 2531          * Defer timeout processing if a channel switch is pending.
 2532          * We typically need to be mute so not doing things that
 2533          * might generate frames is good to handle in one place.
 2534          * Suppressing the station timeout processing may extend the
 2535          * lifetime of inactive stations (by not decrementing their
 2536          * idle counters) but this should be ok unless the CSA is
 2537          * active for an unusually long time.
 2538          */
 2539         if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
 2540                 ieee80211_scan_timeout(ic);
 2541                 ieee80211_timeout_stations(ic);
 2542                 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
 2543 
 2544                 IEEE80211_LOCK(ic);
 2545                 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
 2546                         ieee80211_vap_timeout(vap);
 2547                 IEEE80211_UNLOCK(ic);
 2548         }
 2549         callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
 2550                 ieee80211_node_timeout, ic);
 2551 }
 2552 
 2553 /*
 2554  * The same as ieee80211_iterate_nodes(), but for one vap only.
 2555  */
 2556 int
 2557 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
 2558     struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
 2559 {
 2560         struct ieee80211_node **ni_arr;
 2561         struct ieee80211_node *ni;
 2562         size_t size;
 2563         int count, i;
 2564 
 2565         /*
 2566          * Iterate over the node table and save an array of ref'ed nodes.
 2567          *
 2568          * This is separated out from calling the actual node function so that
 2569          * no LORs will occur.
 2570          */
 2571         IEEE80211_NODE_LOCK(nt);
 2572         count = nt->nt_count;
 2573         size = count * sizeof(struct ieee80211_node *);
 2574         ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
 2575             IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
 2576         if (ni_arr == NULL) {
 2577                 IEEE80211_NODE_UNLOCK(nt);
 2578                 return (ENOMEM);
 2579         }
 2580 
 2581         i = 0;
 2582         TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 2583                 if (vap != NULL && ni->ni_vap != vap)
 2584                         continue;
 2585                 KASSERT(i < count,
 2586                     ("node array overflow (vap %p, i %d, count %d)\n",
 2587                     vap, i, count));
 2588                 ni_arr[i] = ieee80211_ref_node(ni);
 2589                 i++;
 2590         }
 2591         IEEE80211_NODE_UNLOCK(nt);
 2592 
 2593         for (i = 0; i < count; i++) {
 2594                 if (ni_arr[i] == NULL)  /* end of the list */
 2595                         break;
 2596                 (*f)(arg, ni_arr[i]);
 2597                 /* ieee80211_free_node() locks by itself */
 2598                 ieee80211_free_node(ni_arr[i]);
 2599         }
 2600 
 2601         IEEE80211_FREE(ni_arr, M_80211_NODE);
 2602 
 2603         return (0);
 2604 }
 2605 
 2606 /*
 2607  * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
 2608  * reference in the source.
 2609  */
 2610 void
 2611 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
 2612         ieee80211_iter_func *f, void *arg)
 2613 {
 2614         /* XXX no way to pass error to the caller. */
 2615         (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
 2616 }
 2617 
 2618 void
 2619 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
 2620 {
 2621         printf("0x%p: mac %s refcnt %d\n", ni,
 2622                 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
 2623         printf("\tauthmode %u flags 0x%x\n",
 2624                 ni->ni_authmode, ni->ni_flags);
 2625         printf("\tassocid 0x%x txpower %u vlan %u\n",
 2626                 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
 2627         printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
 2628                 ni->ni_txseqs[IEEE80211_NONQOS_TID],
 2629                 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
 2630                 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
 2631                 ni->ni_rxfragstamp);
 2632         printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
 2633                 node_getrssi(ni), ni->ni_noise,
 2634                 ni->ni_intval, ni->ni_capinfo);
 2635         printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
 2636                 ether_sprintf(ni->ni_bssid),
 2637                 ni->ni_esslen, ni->ni_essid,
 2638                 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
 2639         printf("\tinact %u inact_reload %u txrate %u\n",
 2640                 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
 2641         printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
 2642                 ni->ni_htcap, ni->ni_htparam,
 2643                 ni->ni_htctlchan, ni->ni_ht2ndchan);
 2644         printf("\thtopmode %x htstbc %x htchw %u\n",
 2645                 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
 2646         printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
 2647                 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
 2648                 (int) ni->ni_vht_basicmcs);
 2649         /* XXX VHT state */
 2650 }
 2651 
 2652 void
 2653 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
 2654 {
 2655         ieee80211_iterate_nodes(nt,
 2656                 (ieee80211_iter_func *) ieee80211_dump_node, nt);
 2657 }
 2658 
 2659 /*
 2660  * Iterate over the VAPs and update their ERP beacon IEs.
 2661  *
 2662  * Note this must be called from the deferred ERP update task paths.
 2663  */
 2664 void
 2665 ieee80211_notify_erp_locked(struct ieee80211com *ic)
 2666 {
 2667         struct ieee80211vap *vap;
 2668 
 2669         IEEE80211_LOCK_ASSERT(ic);
 2670 
 2671         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
 2672                 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
 2673                         ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
 2674 }
 2675 
 2676 /*
 2677  * Handle a station joining an 11g network.
 2678  */
 2679 static void
 2680 ieee80211_node_join_11g(struct ieee80211_node *ni)
 2681 {
 2682         struct ieee80211com *ic = ni->ni_ic;
 2683         struct ieee80211vap *vap = ni->ni_vap;
 2684 
 2685         IEEE80211_LOCK_ASSERT(ic);
 2686 
 2687         /*
 2688          * Station isn't capable of short slot time.  Bump
 2689          * the count of long slot time stations and disable
 2690          * use of short slot time.  Note that the actual switch
 2691          * over to long slot time use may not occur until the
 2692          * next beacon transmission (per sec. 7.3.1.4 of 11g).
 2693          */
 2694         if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
 2695                 vap->iv_longslotsta++;
 2696                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
 2697                     "station needs long slot time, count %d",
 2698                     vap->iv_longslotsta);
 2699                 /*
 2700                  * XXX TODO: this may need all VAPs checked!
 2701                  */
 2702                 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
 2703                         /*
 2704                          * Don't force slot time when switched to turbo
 2705                          * mode as non-ERP stations won't be present; this
 2706                          * need only be done when on the normal G channel.
 2707                          */
 2708                         ieee80211_vap_set_shortslottime(vap, 0);
 2709                 }
 2710         }
 2711         /*
 2712          * If the new station is not an ERP station
 2713          * then bump the counter and enable protection
 2714          * if configured.
 2715          */
 2716         if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
 2717                 vap->iv_nonerpsta++;
 2718                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
 2719                     "station is !ERP, %d non-ERP stations associated",
 2720                     vap->iv_nonerpsta);
 2721                 /*
 2722                  * If station does not support short preamble
 2723                  * then we must enable use of Barker preamble.
 2724                  */
 2725                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
 2726                         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
 2727                             "%s", "station needs long preamble");
 2728                         vap->iv_flags |= IEEE80211_F_USEBARKER;
 2729                         vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
 2730                         ieee80211_vap_update_preamble(vap);
 2731                 }
 2732                 /*
 2733                  * If protection is configured and this is the first
 2734                  * indication we should use protection, enable it.
 2735                  */
 2736                 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
 2737                     vap->iv_nonerpsta == 1 &&
 2738                     (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
 2739                         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
 2740                             "%s: enable use of protection\n", __func__);
 2741                         vap->iv_flags |= IEEE80211_F_USEPROT;
 2742                         ieee80211_vap_update_erp_protmode(vap);
 2743                 }
 2744         } else
 2745                 ni->ni_flags |= IEEE80211_NODE_ERP;
 2746 }
 2747 
 2748 void
 2749 ieee80211_node_join(struct ieee80211_node *ni, int resp)
 2750 {
 2751         struct ieee80211com *ic = ni->ni_ic;
 2752         struct ieee80211vap *vap = ni->ni_vap;
 2753         int newassoc;
 2754 
 2755         if (ni->ni_associd == 0) {
 2756                 uint16_t aid;
 2757 
 2758                 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
 2759                 /*
 2760                  * It would be good to search the bitmap
 2761                  * more efficiently, but this will do for now.
 2762                  */
 2763                 for (aid = 1; aid < vap->iv_max_aid; aid++) {
 2764                         if (!IEEE80211_AID_ISSET(vap, aid))
 2765                                 break;
 2766                 }
 2767                 if (aid >= vap->iv_max_aid) {
 2768                         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
 2769                         ieee80211_node_leave(ni);
 2770                         return;
 2771                 }
 2772                 ni->ni_associd = aid | 0xc000;
 2773                 ni->ni_jointime = time_uptime;
 2774                 IEEE80211_LOCK(ic);
 2775                 IEEE80211_AID_SET(vap, ni->ni_associd);
 2776                 vap->iv_sta_assoc++;
 2777 
 2778                 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
 2779                         ieee80211_ht_node_join(ni);
 2780                 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
 2781                         ieee80211_vht_node_join(ni);
 2782                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
 2783                     IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
 2784                         ieee80211_node_join_11g(ni);
 2785                 IEEE80211_UNLOCK(ic);
 2786 
 2787                 newassoc = 1;
 2788         } else
 2789                 newassoc = 0;
 2790 
 2791         /*
 2792          * XXX VHT - should log VHT channel width, etc
 2793          */
 2794         IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
 2795             "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
 2796             IEEE80211_NODE_AID(ni),
 2797             vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
 2798             vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
 2799             vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
 2800             ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
 2801             /* XXX update for VHT string */
 2802             ni->ni_flags & IEEE80211_NODE_HT ?
 2803                 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
 2804             ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
 2805             ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
 2806             ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
 2807                 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
 2808             ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
 2809             IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
 2810                 ", fast-frames" : "",
 2811             IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
 2812                 ", turbo" : ""
 2813         );
 2814 
 2815         ieee80211_node_setuptxparms(ni);
 2816         ieee80211_ratectl_node_init(ni);
 2817         /* give driver a chance to setup state like ni_txrate */
 2818         if (ic->ic_newassoc != NULL)
 2819                 ic->ic_newassoc(ni, newassoc);
 2820         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
 2821         /* tell the authenticator about new station */
 2822         if (vap->iv_auth->ia_node_join != NULL)
 2823                 vap->iv_auth->ia_node_join(ni);
 2824         ieee80211_notify_node_join(ni,
 2825             resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
 2826 }
 2827 
 2828 static void
 2829 disable_protection(struct ieee80211vap *vap)
 2830 {
 2831         struct ieee80211com *ic = vap->iv_ic;
 2832 
 2833         KASSERT(vap->iv_nonerpsta == 0 &&
 2834             (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
 2835            ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
 2836            vap->iv_flags_ext));
 2837 
 2838         vap->iv_flags &= ~IEEE80211_F_USEPROT;
 2839         /* XXX verify mode? */
 2840         if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
 2841                 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
 2842                 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
 2843         }
 2844         ieee80211_vap_update_erp_protmode(vap);
 2845         ieee80211_vap_update_preamble(vap);
 2846 }
 2847 
 2848 /*
 2849  * Handle a station leaving an 11g network.
 2850  */
 2851 static void
 2852 ieee80211_node_leave_11g(struct ieee80211_node *ni)
 2853 {
 2854         struct ieee80211com *ic = ni->ni_ic;
 2855         struct ieee80211vap *vap = ni->ni_vap;
 2856 
 2857         IEEE80211_LOCK_ASSERT(ic);
 2858 
 2859         KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
 2860              ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
 2861               ic->ic_bsschan->ic_flags));
 2862 
 2863         /*
 2864          * If a long slot station do the slot time bookkeeping.
 2865          */
 2866         if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
 2867                 KASSERT(vap->iv_longslotsta > 0,
 2868                     ("bogus long slot station count %d", vap->iv_longslotsta));
 2869                 vap->iv_longslotsta--;
 2870                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
 2871                     "long slot time station leaves, count now %d",
 2872                     vap->iv_longslotsta);
 2873                 /*
 2874                  * XXX TODO: this may need all VAPs checked!
 2875                  */
 2876                 if (vap->iv_longslotsta == 0) {
 2877                         /*
 2878                          * Re-enable use of short slot time if supported
 2879                          * and not operating in IBSS mode (per spec).
 2880                          */
 2881                         if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
 2882                             ic->ic_opmode != IEEE80211_M_IBSS) {
 2883                                 IEEE80211_DPRINTF(ni->ni_vap,
 2884                                     IEEE80211_MSG_ASSOC,
 2885                                     "%s: re-enable use of short slot time\n",
 2886                                     __func__);
 2887                                 ieee80211_vap_set_shortslottime(vap, 1);
 2888                         }
 2889                 }
 2890         }
 2891         /*
 2892          * If a non-ERP station do the protection-related bookkeeping.
 2893          */
 2894         if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
 2895                 KASSERT(vap->iv_nonerpsta > 0,
 2896                     ("bogus non-ERP station count %d", vap->iv_nonerpsta));
 2897                 vap->iv_nonerpsta--;
 2898                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
 2899                     "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
 2900                     (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
 2901                         " (non-ERP sta present)" : "");
 2902                 if (vap->iv_nonerpsta == 0 &&
 2903                     (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
 2904                         IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
 2905                                 "%s: disable use of protection\n", __func__);
 2906                         disable_protection(vap);
 2907                 }
 2908         }
 2909 }
 2910 
 2911 /*
 2912  * Time out presence of an overlapping bss with non-ERP
 2913  * stations.  When operating in hostap mode we listen for
 2914  * beacons from other stations and if we identify a non-ERP
 2915  * station is present we enable protection.  To identify
 2916  * when all non-ERP stations are gone we time out this
 2917  * condition.
 2918  */
 2919 static void
 2920 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
 2921 {
 2922 
 2923         IEEE80211_LOCK_ASSERT(vap->iv_ic);
 2924 
 2925         if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
 2926             ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
 2927                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
 2928                     "%s", "age out non-ERP sta present on channel");
 2929                 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
 2930                 if (vap->iv_nonerpsta == 0)
 2931                         disable_protection(vap);
 2932         }
 2933 }
 2934 
 2935 /*
 2936  * Handle bookkeeping for station deauthentication/disassociation
 2937  * when operating as an ap.
 2938  */
 2939 void
 2940 ieee80211_node_leave(struct ieee80211_node *ni)
 2941 {
 2942         struct ieee80211com *ic = ni->ni_ic;
 2943         struct ieee80211vap *vap = ni->ni_vap;
 2944         struct ieee80211_node_table *nt = ni->ni_table;
 2945 
 2946         IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
 2947             "station with aid %d leaves", IEEE80211_NODE_AID(ni));
 2948 
 2949         KASSERT(vap->iv_opmode != IEEE80211_M_STA,
 2950                 ("unexpected operating mode %u", vap->iv_opmode));
 2951         /*
 2952          * If node wasn't previously associated all
 2953          * we need to do is reclaim the reference.
 2954          */
 2955         /* XXX ibss mode bypasses 11g and notification */
 2956         if (ni->ni_associd == 0)
 2957                 goto done;
 2958         /*
 2959          * Tell the authenticator the station is leaving.
 2960          * Note that we must do this before yanking the
 2961          * association id as the authenticator uses the
 2962          * associd to locate it's state block.
 2963          */
 2964         if (vap->iv_auth->ia_node_leave != NULL)
 2965                 vap->iv_auth->ia_node_leave(ni);
 2966 
 2967         IEEE80211_LOCK(ic);
 2968         IEEE80211_AID_CLR(vap, ni->ni_associd);
 2969         vap->iv_sta_assoc--;
 2970 
 2971         if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
 2972                 ieee80211_vht_node_leave(ni);
 2973         if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
 2974                 ieee80211_ht_node_leave(ni);
 2975         if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
 2976             IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
 2977                 ieee80211_node_leave_11g(ni);
 2978         IEEE80211_UNLOCK(ic);
 2979         /*
 2980          * Cleanup station state.  In particular clear various
 2981          * state that might otherwise be reused if the node
 2982          * is reused before the reference count goes to zero
 2983          * (and memory is reclaimed).
 2984          */
 2985         ieee80211_sta_leave(ni);
 2986 done:
 2987         /*
 2988          * Remove the node from any table it's recorded in and
 2989          * drop the caller's reference.  Removal from the table
 2990          * is important to insure the node is not reprocessed
 2991          * for inactivity.
 2992          */
 2993         if (nt != NULL) {
 2994                 IEEE80211_NODE_LOCK(nt);
 2995                 node_reclaim(nt, ni);
 2996                 IEEE80211_NODE_UNLOCK(nt);
 2997         } else
 2998                 ieee80211_free_node(ni);
 2999 }
 3000 
 3001 struct rssiinfo {
 3002         int     rssi_samples;
 3003         uint32_t rssi_total;
 3004 };
 3005 
 3006 static void
 3007 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
 3008 {
 3009         struct rssiinfo *info = arg;
 3010         struct ieee80211vap *vap = ni->ni_vap;
 3011         int8_t rssi;
 3012 
 3013         /* only associated stations */
 3014         if (ni->ni_associd == 0)
 3015                 return;
 3016         rssi = vap->iv_ic->ic_node_getrssi(ni);
 3017         if (rssi != 0) {
 3018                 info->rssi_samples++;
 3019                 info->rssi_total += rssi;
 3020         }
 3021 }
 3022 
 3023 static void
 3024 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
 3025 {
 3026         struct rssiinfo *info = arg;
 3027         struct ieee80211vap *vap = ni->ni_vap;
 3028         int8_t rssi;
 3029 
 3030         /* only neighbors */
 3031         /* XXX check bssid */
 3032         if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
 3033                 return;
 3034         rssi = vap->iv_ic->ic_node_getrssi(ni);
 3035         if (rssi != 0) {
 3036                 info->rssi_samples++;
 3037                 info->rssi_total += rssi;
 3038         }
 3039 }
 3040 
 3041 #ifdef IEEE80211_SUPPORT_MESH
 3042 static void
 3043 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
 3044 {
 3045         struct rssiinfo *info = arg;
 3046         struct ieee80211vap *vap = ni->ni_vap;
 3047         int8_t rssi;
 3048 
 3049         /* only neighbors that peered successfully */
 3050         if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
 3051                 return;
 3052         rssi = vap->iv_ic->ic_node_getrssi(ni);
 3053         if (rssi != 0) {
 3054                 info->rssi_samples++;
 3055                 info->rssi_total += rssi;
 3056         }
 3057 }
 3058 #endif /* IEEE80211_SUPPORT_MESH */
 3059 
 3060 int8_t
 3061 ieee80211_getrssi(struct ieee80211vap *vap)
 3062 {
 3063 #define NZ(x)   ((x) == 0 ? 1 : (x))
 3064         struct ieee80211com *ic = vap->iv_ic;
 3065         struct rssiinfo info;
 3066 
 3067         info.rssi_total = 0;
 3068         info.rssi_samples = 0;
 3069         switch (vap->iv_opmode) {
 3070         case IEEE80211_M_IBSS:          /* average of all ibss neighbors */
 3071         case IEEE80211_M_AHDEMO:        /* average of all neighbors */
 3072                 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
 3073                     &info);
 3074                 break;
 3075         case IEEE80211_M_HOSTAP:        /* average of all associated stations */
 3076                 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
 3077                     &info);
 3078                 break;
 3079 #ifdef IEEE80211_SUPPORT_MESH
 3080         case IEEE80211_M_MBSS:          /* average of all mesh neighbors */
 3081                 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
 3082                     &info);
 3083                 break;
 3084 #endif
 3085         case IEEE80211_M_MONITOR:       /* XXX */
 3086         case IEEE80211_M_STA:           /* use stats from associated ap */
 3087         default:
 3088                 if (vap->iv_bss != NULL)
 3089                         info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
 3090                 info.rssi_samples = 1;
 3091                 break;
 3092         }
 3093         return info.rssi_total / NZ(info.rssi_samples);
 3094 #undef NZ
 3095 }
 3096 
 3097 void
 3098 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
 3099 {
 3100 
 3101         if (vap->iv_bss == NULL)                /* NB: shouldn't happen */
 3102                 return;
 3103         vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
 3104         /* for non-station mode return avg'd rssi accounting */
 3105         if (vap->iv_opmode != IEEE80211_M_STA)
 3106                 *rssi = ieee80211_getrssi(vap);
 3107 }

Cache object: 25a27e0f3d03335ba1a07d8651badce7


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