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

Cache object: bfba7af1a757014fc98f61e392c9c36d


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