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

Cache object: 7684640792dc1b509d8a2fecfd1a41a3


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