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  * Copyright (c) 2001 Atsushi Onoe
    3  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * Alternatively, this software may be distributed under the terms of the
   18  * GNU General Public License ("GPL") version 2 as published by the Free
   19  * Software Foundation.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/6.4/sys/net80211/ieee80211_node.c 173105 2007-10-28 17:46:39Z sam $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h> 
   38 #include <sys/mbuf.h>   
   39 #include <sys/malloc.h>
   40 #include <sys/kernel.h>
   41 
   42 #include <sys/socket.h>
   43  
   44 #include <net/if.h>
   45 #include <net/if_media.h>
   46 #include <net/ethernet.h>
   47 
   48 #include <net80211/ieee80211_var.h>
   49 
   50 #include <net/bpf.h>
   51 
   52 /*
   53  * Association id's are managed with a bit vector.
   54  */
   55 #define IEEE80211_AID_SET(b, w) \
   56         ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
   57 #define IEEE80211_AID_CLR(b, w) \
   58         ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
   59 #define IEEE80211_AID_ISSET(b, w) \
   60         ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
   61 
   62 #ifdef IEEE80211_DEBUG_REFCNT
   63 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
   64 #else
   65 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
   66 #endif
   67 
   68 static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
   69 static void node_cleanup(struct ieee80211_node *);
   70 static void node_free(struct ieee80211_node *);
   71 static u_int8_t node_getrssi(const struct ieee80211_node *);
   72 
   73 static void ieee80211_setup_node(struct ieee80211_node_table *,
   74                 struct ieee80211_node *, const u_int8_t *);
   75 static void _ieee80211_free_node(struct ieee80211_node *);
   76 static void ieee80211_free_allnodes(struct ieee80211_node_table *);
   77 
   78 static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
   79 static void ieee80211_timeout_stations(struct ieee80211_node_table *);
   80 
   81 static void ieee80211_set_tim(struct ieee80211_node *, int set);
   82 
   83 static void ieee80211_node_table_init(struct ieee80211com *ic,
   84         struct ieee80211_node_table *nt, const char *name,
   85         int inact, int keyixmax,
   86         void (*timeout)(struct ieee80211_node_table *));
   87 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
   88 
   89 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
   90 
   91 void
   92 ieee80211_node_attach(struct ieee80211com *ic)
   93 {
   94 
   95         ic->ic_node_alloc = node_alloc;
   96         ic->ic_node_free = node_free;
   97         ic->ic_node_cleanup = node_cleanup;
   98         ic->ic_node_getrssi = node_getrssi;
   99 
  100         /* default station inactivity timer setings */
  101         ic->ic_inact_init = IEEE80211_INACT_INIT;
  102         ic->ic_inact_auth = IEEE80211_INACT_AUTH;
  103         ic->ic_inact_run = IEEE80211_INACT_RUN;
  104         ic->ic_inact_probe = IEEE80211_INACT_PROBE;
  105 
  106         /* NB: driver should override */
  107         ic->ic_max_aid = IEEE80211_AID_DEF;
  108         ic->ic_set_tim = ieee80211_set_tim;
  109 }
  110 
  111 void
  112 ieee80211_node_lateattach(struct ieee80211com *ic)
  113 {
  114         struct ieee80211_rsnparms *rsn;
  115 
  116         if (ic->ic_max_aid > IEEE80211_AID_MAX)
  117                 ic->ic_max_aid = IEEE80211_AID_MAX;
  118         MALLOC(ic->ic_aid_bitmap, u_int32_t *,
  119                 howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
  120                 M_DEVBUF, M_NOWAIT | M_ZERO);
  121         if (ic->ic_aid_bitmap == NULL) {
  122                 /* XXX no way to recover */
  123                 printf("%s: no memory for AID bitmap!\n", __func__);
  124                 ic->ic_max_aid = 0;
  125         }
  126 
  127         /* XXX defer until using hostap/ibss mode */
  128         ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
  129         MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
  130                 M_DEVBUF, M_NOWAIT | M_ZERO);
  131         if (ic->ic_tim_bitmap == NULL) {
  132                 /* XXX no way to recover */
  133                 printf("%s: no memory for TIM bitmap!\n", __func__);
  134         }
  135 
  136         ieee80211_node_table_init(ic, &ic->ic_sta, "station",
  137                 IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
  138                 ieee80211_timeout_stations);
  139         ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
  140                 IEEE80211_INACT_SCAN, 0,
  141                 ieee80211_timeout_scan_candidates);
  142 
  143         ieee80211_reset_bss(ic);
  144         /*
  145          * Setup "global settings" in the bss node so that
  146          * each new station automatically inherits them.
  147          */
  148         rsn = &ic->ic_bss->ni_rsn;
  149         /* WEP, TKIP, and AES-CCM are always supported */
  150         rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
  151         rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
  152         rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
  153         if (ic->ic_caps & IEEE80211_C_AES)
  154                 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
  155         if (ic->ic_caps & IEEE80211_C_CKIP)
  156                 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
  157         /*
  158          * Default unicast cipher to WEP for 802.1x use.  If
  159          * WPA is enabled the management code will set these
  160          * values to reflect.
  161          */
  162         rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
  163         rsn->rsn_ucastkeylen = 104 / NBBY;
  164         /*
  165          * WPA says the multicast cipher is the lowest unicast
  166          * cipher supported.  But we skip WEP which would
  167          * otherwise be used based on this criteria.
  168          */
  169         rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
  170         rsn->rsn_mcastkeylen = 128 / NBBY;
  171 
  172         /*
  173          * We support both WPA-PSK and 802.1x; the one used
  174          * is determined by the authentication mode and the
  175          * setting of the PSK state.
  176          */
  177         rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
  178         rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
  179 
  180         ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
  181 }
  182 
  183 void
  184 ieee80211_node_detach(struct ieee80211com *ic)
  185 {
  186 
  187         if (ic->ic_bss != NULL) {
  188                 ieee80211_free_node(ic->ic_bss);
  189                 ic->ic_bss = NULL;
  190         }
  191         ieee80211_node_table_cleanup(&ic->ic_scan);
  192         ieee80211_node_table_cleanup(&ic->ic_sta);
  193         if (ic->ic_aid_bitmap != NULL) {
  194                 FREE(ic->ic_aid_bitmap, M_DEVBUF);
  195                 ic->ic_aid_bitmap = NULL;
  196         }
  197         if (ic->ic_tim_bitmap != NULL) {
  198                 FREE(ic->ic_tim_bitmap, M_DEVBUF);
  199                 ic->ic_tim_bitmap = NULL;
  200         }
  201 }
  202 
  203 /* 
  204  * Port authorize/unauthorize interfaces for use by an authenticator.
  205  */
  206 
  207 void
  208 ieee80211_node_authorize(struct ieee80211_node *ni)
  209 {
  210         struct ieee80211com *ic = ni->ni_ic;
  211 
  212         ni->ni_flags |= IEEE80211_NODE_AUTH;
  213         ni->ni_inact_reload = ic->ic_inact_run;
  214 }
  215 
  216 void
  217 ieee80211_node_unauthorize(struct ieee80211_node *ni)
  218 {
  219         ni->ni_flags &= ~IEEE80211_NODE_AUTH;
  220 }
  221 
  222 /*
  223  * Set/change the channel.  The rate set is also updated as
  224  * to insure a consistent view by drivers.
  225  */
  226 static void
  227 ieee80211_set_chan(struct ieee80211com *ic,
  228         struct ieee80211_node *ni, struct ieee80211_channel *chan)
  229 {
  230         if (chan == IEEE80211_CHAN_ANYC)        /* XXX while scanning */
  231                 chan = ic->ic_curchan;
  232         ni->ni_chan = chan;
  233         ni->ni_rates = *ieee80211_get_suprates(ic, chan);
  234 }
  235 
  236 /*
  237  * AP scanning support.
  238  */
  239 
  240 #ifdef IEEE80211_DEBUG
  241 static void
  242 dump_chanlist(const u_char chans[])
  243 {
  244         const char *sep;
  245         int i;
  246 
  247         sep = " ";
  248         for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  249                 if (isset(chans, i)) {
  250                         printf("%s%u", sep, i);
  251                         sep = ", ";
  252                 }
  253 }
  254 #endif /* IEEE80211_DEBUG */
  255 
  256 /*
  257  * Initialize the channel set to scan based on the
  258  * of available channels and the current PHY mode.
  259  */
  260 static void
  261 ieee80211_reset_scan(struct ieee80211com *ic)
  262 {
  263 
  264         /* XXX ic_des_chan should be handled with ic_chan_active */
  265         if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
  266                 memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
  267                 setbit(ic->ic_chan_scan,
  268                         ieee80211_chan2ieee(ic, ic->ic_des_chan));
  269         } else
  270                 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
  271                         sizeof(ic->ic_chan_active));
  272 #ifdef IEEE80211_DEBUG
  273         if (ieee80211_msg_scan(ic)) {
  274                 printf("%s: scan set:", __func__);
  275                 dump_chanlist(ic->ic_chan_scan);
  276                 printf(" start chan %u\n",
  277                         ieee80211_chan2ieee(ic, ic->ic_curchan));
  278         }
  279 #endif /* IEEE80211_DEBUG */
  280 }
  281 
  282 /*
  283  * Begin an active scan.
  284  */
  285 void
  286 ieee80211_begin_scan(struct ieee80211com *ic, int reset)
  287 {
  288 
  289         ic->ic_scan.nt_scangen++;
  290         /*
  291          * In all but hostap mode scanning starts off in
  292          * an active mode before switching to passive.
  293          */
  294         if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
  295                 ic->ic_flags |= IEEE80211_F_ASCAN;
  296                 ic->ic_stats.is_scan_active++;
  297         } else
  298                 ic->ic_stats.is_scan_passive++;
  299         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
  300                 "begin %s scan in %s mode, scangen %u\n",
  301                 (ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
  302                 ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
  303         /*
  304          * Clear scan state and flush any previously seen AP's.
  305          */
  306         ieee80211_reset_scan(ic);
  307         if (reset)
  308                 ieee80211_free_allnodes(&ic->ic_scan);
  309 
  310         ic->ic_flags |= IEEE80211_F_SCAN;
  311 
  312         /* Scan the next channel. */
  313         ieee80211_next_scan(ic);
  314 }
  315 
  316 /*
  317  * Switch to the next channel marked for scanning.
  318  */
  319 int
  320 ieee80211_next_scan(struct ieee80211com *ic)
  321 {
  322         struct ieee80211_channel *chan;
  323 
  324         /*
  325          * Insure any previous mgt frame timeouts don't fire.
  326          * This assumes the driver does the right thing in
  327          * flushing anything queued in the driver and below.
  328          */
  329         ic->ic_mgt_timer = 0;
  330         ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
  331 
  332         chan = ic->ic_curchan;
  333         do {
  334                 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
  335                         chan = &ic->ic_channels[0];
  336                 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
  337                         clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
  338                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
  339                             "%s: chan %d->%d\n", __func__,
  340                             ieee80211_chan2ieee(ic, ic->ic_curchan),
  341                             ieee80211_chan2ieee(ic, chan));
  342                         ic->ic_curchan = chan;
  343                         /*
  344                          * XXX drivers should do this as needed,
  345                          * XXX for now maintain compatibility
  346                          */
  347                         ic->ic_bss->ni_rates = *ieee80211_get_suprates(ic, chan);
  348                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  349                         return 1;
  350                 }
  351         } while (chan != ic->ic_curchan);
  352         ieee80211_end_scan(ic);
  353         return 0;
  354 }
  355 
  356 /*
  357  * Probe the curent channel, if allowed, while scanning.
  358  * If the channel is not marked passive-only then send
  359  * a probe request immediately.  Otherwise mark state and
  360  * listen for beacons on the channel; if we receive something
  361  * then we'll transmit a probe request.
  362  */
  363 void
  364 ieee80211_probe_curchan(struct ieee80211com *ic, int force)
  365 {
  366         struct ifnet *ifp = ic->ic_ifp;
  367 
  368         if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
  369                 /*
  370                  * XXX send both broadcast+directed probe request
  371                  */
  372                 ieee80211_send_probereq(ic->ic_bss,
  373                         ic->ic_myaddr, ifp->if_broadcastaddr,
  374                         ifp->if_broadcastaddr,
  375                         ic->ic_des_essid, ic->ic_des_esslen,
  376                         ic->ic_opt_ie, ic->ic_opt_ie_len);
  377         } else
  378                 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
  379 }
  380 
  381 static __inline void
  382 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
  383 {
  384         /* propagate useful state */
  385         nbss->ni_authmode = obss->ni_authmode;
  386         nbss->ni_txpower = obss->ni_txpower;
  387         nbss->ni_vlan = obss->ni_vlan;
  388         nbss->ni_rsn = obss->ni_rsn;
  389         /* XXX statistics? */
  390 }
  391 
  392 void
  393 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
  394 {
  395         struct ieee80211_node_table *nt;
  396         struct ieee80211_node *ni;
  397 
  398         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
  399                 "%s: creating ibss\n", __func__);
  400 
  401         /*
  402          * Create the station/neighbor table.  Note that for adhoc
  403          * mode we make the initial inactivity timer longer since
  404          * we create nodes only through discovery and they typically
  405          * are long-lived associations.
  406          */
  407         nt = &ic->ic_sta;
  408         IEEE80211_NODE_LOCK(nt);
  409         if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  410                 nt->nt_name = "station";
  411                 nt->nt_inact_init = ic->ic_inact_init;
  412         } else {
  413                 nt->nt_name = "neighbor";
  414                 nt->nt_inact_init = ic->ic_inact_run;
  415         }
  416         IEEE80211_NODE_UNLOCK(nt);
  417 
  418         ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
  419         if (ni == NULL) {
  420                 /* XXX recovery? */
  421                 return;
  422         }
  423         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
  424         ni->ni_esslen = ic->ic_des_esslen;
  425         memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
  426         copy_bss(ni, ic->ic_bss);
  427         ni->ni_intval = ic->ic_bintval;
  428         if (ic->ic_flags & IEEE80211_F_PRIVACY)
  429                 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
  430         if (ic->ic_phytype == IEEE80211_T_FH) {
  431                 ni->ni_fhdwell = 200;   /* XXX */
  432                 ni->ni_fhindex = 1;
  433         }
  434         if (ic->ic_opmode == IEEE80211_M_IBSS) {
  435                 ic->ic_flags |= IEEE80211_F_SIBSS;
  436                 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;       /* XXX */
  437                 if (ic->ic_flags & IEEE80211_F_DESBSSID)
  438                         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
  439                 else {
  440                         get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
  441                         /* clear group bit, add local bit */
  442                         ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
  443                 }
  444         } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
  445                 if (ic->ic_flags & IEEE80211_F_DESBSSID)
  446                         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
  447                 else
  448                         memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
  449         }
  450         /* 
  451          * Fix the channel and related attributes.
  452          */
  453         ieee80211_set_chan(ic, ni, chan);
  454         ic->ic_curchan = chan;
  455         ic->ic_curmode = ieee80211_chan2mode(ic, chan);
  456         /*
  457          * Do mode-specific rate setup.
  458          */
  459         if (IEEE80211_IS_CHAN_FULL(chan) &&
  460             (ic->ic_curmode == IEEE80211_MODE_11G ||
  461              ic->ic_curmode == IEEE80211_MODE_11B))
  462                 ieee80211_set11gbasicrates(&ni->ni_rates, ic->ic_curmode);
  463 
  464         (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
  465 }
  466 
  467 void
  468 ieee80211_reset_bss(struct ieee80211com *ic)
  469 {
  470         struct ieee80211_node *ni, *obss;
  471 
  472         ieee80211_node_table_reset(&ic->ic_scan);
  473         ieee80211_node_table_reset(&ic->ic_sta);
  474 
  475         ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
  476         KASSERT(ni != NULL, ("unable to setup inital BSS node"));
  477         obss = ic->ic_bss;
  478         ic->ic_bss = ieee80211_ref_node(ni);
  479         if (obss != NULL) {
  480                 copy_bss(ni, obss);
  481                 ni->ni_intval = ic->ic_bintval;
  482                 ieee80211_free_node(obss);
  483         }
  484 }
  485 
  486 /* XXX tunable */
  487 #define STA_FAILS_MAX   2               /* assoc failures before ignored */
  488 
  489 static int
  490 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
  491 {
  492         u_int8_t rate;
  493         int fail;
  494 
  495         fail = 0;
  496         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
  497                 fail |= 0x01;
  498         if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
  499             ni->ni_chan != ic->ic_des_chan)
  500                 fail |= 0x01;
  501         if (ic->ic_opmode == IEEE80211_M_IBSS) {
  502                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  503                         fail |= 0x02;
  504         } else {
  505                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
  506                         fail |= 0x02;
  507         }
  508         if (ic->ic_flags & IEEE80211_F_PRIVACY) {
  509                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  510                         fail |= 0x04;
  511         } else {
  512                 /* XXX does this mean privacy is supported or required? */
  513                 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
  514                         fail |= 0x04;
  515         }
  516         rate = ieee80211_fix_rate(ni,
  517              IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
  518         if (rate & IEEE80211_RATE_BASIC)
  519                 fail |= 0x08;
  520         if (ic->ic_des_esslen != 0 &&
  521             (ni->ni_esslen != ic->ic_des_esslen ||
  522              memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
  523                 fail |= 0x10;
  524         if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
  525             !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
  526                 fail |= 0x20;
  527         if (ni->ni_fails >= STA_FAILS_MAX)
  528                 fail |= 0x40;
  529 #ifdef IEEE80211_DEBUG
  530         if (ieee80211_msg_scan(ic)) {
  531                 printf(" %c %s",
  532                     fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
  533                     ether_sprintf(ni->ni_macaddr));
  534                 printf(" %s%c", ether_sprintf(ni->ni_bssid),
  535                     fail & 0x20 ? '!' : ' ');
  536                 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
  537                         fail & 0x01 ? '!' : ' ');
  538                 printf(" %+4d", ni->ni_rssi);
  539                 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
  540                     fail & 0x08 ? '!' : ' ');
  541                 printf(" %4s%c",
  542                     (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
  543                     (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
  544                     "????",
  545                     fail & 0x02 ? '!' : ' ');
  546                 printf(" %3s%c ",
  547                     (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
  548                     "wep" : "no",
  549                     fail & 0x04 ? '!' : ' ');
  550                 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
  551                 printf("%s\n", fail & 0x10 ? "!" : "");
  552         }
  553 #endif
  554         return fail;
  555 }
  556 
  557 static __inline u_int8_t
  558 maxrate(const struct ieee80211_node *ni)
  559 {
  560         const struct ieee80211_rateset *rs = &ni->ni_rates;
  561         /* NB: assumes rate set is sorted (happens on frame receive) */
  562         return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
  563 }
  564 
  565 /*
  566  * Compare the capabilities of two nodes and decide which is
  567  * more desirable (return >0 if a is considered better).  Note
  568  * that we assume compatibility/usability has already been checked
  569  * so we don't need to (e.g. validate whether privacy is supported).
  570  * Used to select the best scan candidate for association in a BSS.
  571  */
  572 static int
  573 ieee80211_node_compare(struct ieee80211com *ic,
  574                        const struct ieee80211_node *a,
  575                        const struct ieee80211_node *b)
  576 {
  577         u_int8_t maxa, maxb;
  578         u_int8_t rssia, rssib;
  579         int weight;
  580 
  581         /* privacy support preferred */
  582         if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
  583             (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  584                 return 1;
  585         if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
  586             (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
  587                 return -1;
  588 
  589         /* compare count of previous failures */
  590         weight = b->ni_fails - a->ni_fails;
  591         if (abs(weight) > 1)
  592                 return weight;
  593 
  594         rssia = ic->ic_node_getrssi(a);
  595         rssib = ic->ic_node_getrssi(b);
  596         if (abs(rssib - rssia) < 5) {
  597                 /* best/max rate preferred if signal level close enough XXX */
  598                 maxa = maxrate(a);
  599                 maxb = maxrate(b);
  600                 if (maxa != maxb)
  601                         return maxa - maxb;
  602                 /* XXX use freq for channel preference */
  603                 /* for now just prefer 5Ghz band to all other bands */
  604                 if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
  605                    !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
  606                         return 1;
  607                 if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
  608                      IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
  609                         return -1;
  610         }
  611         /* all things being equal, use signal level */
  612         return rssia - rssib;
  613 }
  614 
  615 /*
  616  * Mark an ongoing scan stopped.
  617  */
  618 void
  619 ieee80211_cancel_scan(struct ieee80211com *ic)
  620 {
  621 
  622         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
  623                 __func__,
  624                 (ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
  625 
  626         ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
  627         ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
  628 }
  629 
  630 /*
  631  * Complete a scan of potential channels.
  632  */
  633 void
  634 ieee80211_end_scan(struct ieee80211com *ic)
  635 {
  636         struct ieee80211_node_table *nt = &ic->ic_scan;
  637         struct ieee80211_node *ni, *selbs;
  638 
  639         ieee80211_cancel_scan(ic);
  640         ieee80211_notify_scan_done(ic);
  641 
  642         if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  643                 u_int8_t maxrssi[IEEE80211_CHAN_MAX];   /* XXX off stack? */
  644                 int i, bestchan;
  645                 u_int8_t rssi;
  646 
  647                 /*
  648                  * The passive scan to look for existing AP's completed,
  649                  * select a channel to camp on.  Identify the channels
  650                  * that already have one or more AP's and try to locate
  651                  * an unoccupied one.  If that fails, pick a channel that
  652                  * looks to be quietest.
  653                  */
  654                 memset(maxrssi, 0, sizeof(maxrssi));
  655                 IEEE80211_NODE_LOCK(nt);
  656                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
  657                         rssi = ic->ic_node_getrssi(ni);
  658                         i = ieee80211_chan2ieee(ic, ni->ni_chan);
  659                         if (rssi > maxrssi[i])
  660                                 maxrssi[i] = rssi;
  661                 }
  662                 IEEE80211_NODE_UNLOCK(nt);
  663                 /* XXX select channel more intelligently */
  664                 bestchan = -1;
  665                 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  666                         if (isset(ic->ic_chan_active, i)) {
  667                                 /*
  668                                  * If the channel is unoccupied the max rssi
  669                                  * should be zero; just take it.  Otherwise
  670                                  * track the channel with the lowest rssi and
  671                                  * use that when all channels appear occupied.
  672                                  */
  673                                 if (maxrssi[i] == 0) {
  674                                         bestchan = i;
  675                                         break;
  676                                 }
  677                                 if (bestchan == -1 ||
  678                                     maxrssi[i] < maxrssi[bestchan])
  679                                         bestchan = i;
  680                         }
  681                 if (bestchan != -1) {
  682                         ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
  683                         return;
  684                 }
  685                 /* no suitable channel, should not happen */
  686         }
  687 
  688         /*
  689          * When manually sequencing the state machine; scan just once
  690          * regardless of whether we have a candidate or not.  The
  691          * controlling application is expected to setup state and
  692          * initiate an association.
  693          */
  694         if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
  695                 return;
  696         /*
  697          * Automatic sequencing; look for a candidate and
  698          * if found join the network.
  699          */
  700         /* NB: unlocked read should be ok */
  701         if (TAILQ_FIRST(&nt->nt_node) == NULL) {
  702                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
  703                         "%s: no scan candidate\n", __func__);
  704   notfound:
  705                 if (ic->ic_opmode == IEEE80211_M_IBSS &&
  706                     (ic->ic_flags & IEEE80211_F_IBSSON) &&
  707                     ic->ic_des_esslen != 0) {
  708                         ieee80211_create_ibss(ic, ic->ic_ibss_chan);
  709                         return;
  710                 }
  711                 /*
  712                  * Decrement the failure counts so entries will be
  713                  * reconsidered the next time around.  We really want
  714                  * to do this only for sta's where we've previously
  715                  * had some success.
  716                  */
  717                 IEEE80211_NODE_LOCK(nt);
  718                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
  719                         if (ni->ni_fails)
  720                                 ni->ni_fails--;
  721                 IEEE80211_NODE_UNLOCK(nt);
  722                 /*
  723                  * Reset the list of channels to scan and start again.
  724                  */
  725                 ieee80211_reset_scan(ic);
  726                 ic->ic_flags |= IEEE80211_F_SCAN;
  727                 ieee80211_next_scan(ic);
  728                 return;
  729         }
  730         selbs = NULL;
  731         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
  732             "macaddr          bssid         chan  rssi rate flag  wep  essid");
  733         IEEE80211_NODE_LOCK(nt);
  734         TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
  735                 if (ieee80211_match_bss(ic, ni) == 0) {
  736                         if (selbs == NULL)
  737                                 selbs = ni;
  738                         else if (ieee80211_node_compare(ic, ni, selbs) > 0)
  739                                 selbs = ni;
  740                 }
  741         }
  742         if (selbs != NULL)              /* NB: grab ref while dropping lock */
  743                 (void) ieee80211_ref_node(selbs);
  744         IEEE80211_NODE_UNLOCK(nt);
  745         if (selbs == NULL)
  746                 goto notfound;
  747         if (!ieee80211_sta_join(ic, selbs)) {
  748                 ieee80211_free_node(selbs);
  749                 goto notfound;
  750         }
  751 }
  752  
  753 /*
  754  * Handle 802.11 ad hoc network merge.  The
  755  * convention, set by the Wireless Ethernet Compatibility Alliance
  756  * (WECA), is that an 802.11 station will change its BSSID to match
  757  * the "oldest" 802.11 ad hoc network, on the same channel, that
  758  * has the station's desired SSID.  The "oldest" 802.11 network
  759  * sends beacons with the greatest TSF timestamp.
  760  *
  761  * The caller is assumed to validate TSF's before attempting a merge.
  762  *
  763  * Return !0 if the BSSID changed, 0 otherwise.
  764  */
  765 int
  766 ieee80211_ibss_merge(struct ieee80211_node *ni)
  767 {
  768         struct ieee80211com *ic = ni->ni_ic;
  769 
  770         if (ni == ic->ic_bss ||
  771             IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
  772                 /* unchanged, nothing to do */
  773                 return 0;
  774         }
  775         if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */
  776                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
  777                     "%s: merge failed, capabilities mismatch\n", __func__);
  778                 ic->ic_stats.is_ibss_capmismatch++;
  779                 return 0;
  780         }
  781         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
  782                 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
  783                 ether_sprintf(ni->ni_bssid),
  784                 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
  785                 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
  786                 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
  787         );
  788         return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
  789 }
  790 
  791 /*
  792  * Join the specified IBSS/BSS network.  The node is assumed to
  793  * be passed in with a held reference.
  794  */
  795 int
  796 ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
  797 {
  798         struct ieee80211_node *obss;
  799 
  800         if (ic->ic_opmode == IEEE80211_M_IBSS) {
  801                 struct ieee80211_node_table *nt;
  802                 /*
  803                  * Fillin the neighbor table; it will already
  804                  * exist if we are simply switching mastership.
  805                  * XXX ic_sta always setup so this is unnecessary?
  806                  */
  807                 nt = &ic->ic_sta;
  808                 IEEE80211_NODE_LOCK(nt);
  809                 nt->nt_name = "neighbor";
  810                 nt->nt_inact_init = ic->ic_inact_run;
  811                 IEEE80211_NODE_UNLOCK(nt);
  812         }
  813 
  814         /*
  815          * Committed to selbs, setup state.
  816          */
  817         obss = ic->ic_bss;
  818         ic->ic_bss = selbs;             /* NB: caller assumed to bump refcnt */
  819         if (obss != NULL) {
  820                 copy_bss(selbs, obss);
  821                 ieee80211_free_node(obss);
  822         }
  823 
  824         /*
  825          * Delete unusable rates; we've already checked
  826          * that the negotiated rate set is acceptable.
  827          */
  828         ieee80211_fix_rate(ic->ic_bss, IEEE80211_F_DODEL | IEEE80211_F_JOIN);
  829 
  830         /*
  831          * Set the erp state (mostly the slot time) to deal with
  832          * the auto-select case; this should be redundant if the
  833          * mode is locked.
  834          */ 
  835         ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
  836         ic->ic_curchan = selbs->ni_chan;
  837         ieee80211_reset_erp(ic);
  838         ieee80211_wme_initparams(ic);
  839 
  840         if (ic->ic_opmode == IEEE80211_M_STA)
  841                 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
  842         else
  843                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  844         return 1;
  845 }
  846 
  847 /*
  848  * Leave the specified IBSS/BSS network.  The node is assumed to
  849  * be passed in with a held reference.
  850  */
  851 void
  852 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
  853 {
  854         ic->ic_node_cleanup(ni);
  855         ieee80211_notify_node_leave(ic, ni);
  856 }
  857 
  858 static struct ieee80211_node *
  859 node_alloc(struct ieee80211_node_table *nt)
  860 {
  861         struct ieee80211_node *ni;
  862 
  863         MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
  864                 M_80211_NODE, M_NOWAIT | M_ZERO);
  865         return ni;
  866 }
  867 
  868 /*
  869  * Reclaim any resources in a node and reset any critical
  870  * state.  Typically nodes are free'd immediately after,
  871  * but in some cases the storage may be reused so we need
  872  * to insure consistent state (should probably fix that).
  873  */
  874 static void
  875 node_cleanup(struct ieee80211_node *ni)
  876 {
  877 #define N(a)    (sizeof(a)/sizeof(a[0]))
  878         struct ieee80211com *ic = ni->ni_ic;
  879         int i, qlen;
  880 
  881         /* NB: preserve ni_table */
  882         if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
  883                 ic->ic_ps_sta--;
  884                 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
  885                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
  886                     "[%s] power save mode off, %u sta's in ps mode\n",
  887                     ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
  888         }
  889         /*
  890          * Clear AREF flag that marks the authorization refcnt bump
  891          * has happened.  This is probably not needed as the node
  892          * should always be removed from the table so not found but
  893          * do it just in case.
  894          */
  895         ni->ni_flags &= ~IEEE80211_NODE_AREF;
  896 
  897         /*
  898          * Drain power save queue and, if needed, clear TIM.
  899          */
  900         IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
  901         if (qlen != 0 && ic->ic_set_tim != NULL)
  902                 ic->ic_set_tim(ni, 0);
  903 
  904         ni->ni_associd = 0;
  905         if (ni->ni_challenge != NULL) {
  906                 FREE(ni->ni_challenge, M_DEVBUF);
  907                 ni->ni_challenge = NULL;
  908         }
  909         /*
  910          * Preserve SSID, WPA, and WME ie's so the bss node is
  911          * reusable during a re-auth/re-assoc state transition.
  912          * If we remove these data they will not be recreated
  913          * because they come from a probe-response or beacon frame
  914          * which cannot be expected prior to the association-response.
  915          * This should not be an issue when operating in other modes
  916          * as stations leaving always go through a full state transition
  917          * which will rebuild this state.
  918          *
  919          * XXX does this leave us open to inheriting old state?
  920          */
  921         for (i = 0; i < N(ni->ni_rxfrag); i++)
  922                 if (ni->ni_rxfrag[i] != NULL) {
  923                         m_freem(ni->ni_rxfrag[i]);
  924                         ni->ni_rxfrag[i] = NULL;
  925                 }
  926         /*
  927          * Must be careful here to remove any key map entry w/o a LOR.
  928          */
  929         ieee80211_node_delucastkey(ni);
  930 #undef N
  931 }
  932 
  933 static void
  934 node_free(struct ieee80211_node *ni)
  935 {
  936         struct ieee80211com *ic = ni->ni_ic;
  937 
  938         ic->ic_node_cleanup(ni);
  939         if (ni->ni_wpa_ie != NULL)
  940                 FREE(ni->ni_wpa_ie, M_DEVBUF);
  941         if (ni->ni_wme_ie != NULL)
  942                 FREE(ni->ni_wme_ie, M_DEVBUF);
  943         IEEE80211_NODE_SAVEQ_DESTROY(ni);
  944         FREE(ni, M_80211_NODE);
  945 }
  946 
  947 static u_int8_t
  948 node_getrssi(const struct ieee80211_node *ni)
  949 {
  950         return ni->ni_rssi;
  951 }
  952 
  953 static void
  954 ieee80211_setup_node(struct ieee80211_node_table *nt,
  955         struct ieee80211_node *ni, const u_int8_t *macaddr)
  956 {
  957         struct ieee80211com *ic = nt->nt_ic;
  958         int hash;
  959 
  960         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
  961                 "%s %p<%s> in %s table\n", __func__, ni,
  962                 ether_sprintf(macaddr), nt->nt_name);
  963 
  964         IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
  965         hash = IEEE80211_NODE_HASH(macaddr);
  966         ieee80211_node_initref(ni);             /* mark referenced */
  967         ni->ni_chan = IEEE80211_CHAN_ANYC;
  968         ni->ni_authmode = IEEE80211_AUTH_OPEN;
  969         ni->ni_txpower = ic->ic_txpowlimit;     /* max power */
  970         ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
  971         ni->ni_inact_reload = nt->nt_inact_init;
  972         ni->ni_inact = ni->ni_inact_reload;
  973         IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
  974 
  975         IEEE80211_NODE_LOCK(nt);
  976         TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
  977         LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
  978         ni->ni_table = nt;
  979         ni->ni_ic = ic;
  980         IEEE80211_NODE_UNLOCK(nt);
  981 }
  982 
  983 struct ieee80211_node *
  984 ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
  985 {
  986         struct ieee80211com *ic = nt->nt_ic;
  987         struct ieee80211_node *ni;
  988 
  989         ni = ic->ic_node_alloc(nt);
  990         if (ni != NULL)
  991                 ieee80211_setup_node(nt, ni, macaddr);
  992         else
  993                 ic->ic_stats.is_rx_nodealloc++;
  994         return ni;
  995 }
  996 
  997 /*
  998  * Craft a temporary node suitable for sending a management frame
  999  * to the specified station.  We craft only as much state as we
 1000  * need to do the work since the node will be immediately reclaimed
 1001  * once the send completes.
 1002  */
 1003 struct ieee80211_node *
 1004 ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
 1005 {
 1006         struct ieee80211_node *ni;
 1007 
 1008         ni = ic->ic_node_alloc(&ic->ic_sta);
 1009         if (ni != NULL) {
 1010                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1011                         "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
 1012 
 1013                 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
 1014                 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
 1015                 ieee80211_node_initref(ni);             /* mark referenced */
 1016                 ni->ni_txpower = ic->ic_bss->ni_txpower;
 1017                 /* NB: required by ieee80211_fix_rate */
 1018                 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
 1019                 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
 1020                         IEEE80211_KEYIX_NONE);
 1021                 /* XXX optimize away */
 1022                 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
 1023 
 1024                 ni->ni_table = NULL;            /* NB: pedantic */
 1025                 ni->ni_ic = ic;
 1026         } else {
 1027                 /* XXX msg */
 1028                 ic->ic_stats.is_rx_nodealloc++;
 1029         }
 1030         return ni;
 1031 }
 1032 
 1033 struct ieee80211_node *
 1034 ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
 1035 {
 1036         struct ieee80211com *ic = nt->nt_ic;
 1037         struct ieee80211_node *ni;
 1038 
 1039         ni = ic->ic_node_alloc(nt);
 1040         if (ni != NULL) {
 1041                 ieee80211_setup_node(nt, ni, macaddr);
 1042                 /*
 1043                  * Inherit from ic_bss.
 1044                  */
 1045                 ni->ni_authmode = ic->ic_bss->ni_authmode;
 1046                 ni->ni_txpower = ic->ic_bss->ni_txpower;
 1047                 ni->ni_vlan = ic->ic_bss->ni_vlan;      /* XXX?? */
 1048                 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
 1049                 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
 1050                 ni->ni_rsn = ic->ic_bss->ni_rsn;
 1051         } else
 1052                 ic->ic_stats.is_rx_nodealloc++;
 1053         return ni;
 1054 }
 1055 
 1056 static struct ieee80211_node *
 1057 #ifdef IEEE80211_DEBUG_REFCNT
 1058 _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
 1059         const u_int8_t *macaddr, const char *func, int line)
 1060 #else
 1061 _ieee80211_find_node(struct ieee80211_node_table *nt,
 1062         const u_int8_t *macaddr)
 1063 #endif
 1064 {
 1065         struct ieee80211_node *ni;
 1066         int hash;
 1067 
 1068         IEEE80211_NODE_LOCK_ASSERT(nt);
 1069 
 1070         hash = IEEE80211_NODE_HASH(macaddr);
 1071         LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
 1072                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
 1073                         ieee80211_ref_node(ni); /* mark referenced */
 1074 #ifdef IEEE80211_DEBUG_REFCNT
 1075                         IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
 1076                             "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
 1077                             func, line,
 1078                             ni, ether_sprintf(ni->ni_macaddr),
 1079                             ieee80211_node_refcnt(ni));
 1080 #endif
 1081                         return ni;
 1082                 }
 1083         }
 1084         return NULL;
 1085 }
 1086 #ifdef IEEE80211_DEBUG_REFCNT
 1087 #define _ieee80211_find_node(nt, mac) \
 1088         _ieee80211_find_node_debug(nt, mac, func, line)
 1089 #endif
 1090 
 1091 struct ieee80211_node *
 1092 #ifdef IEEE80211_DEBUG_REFCNT
 1093 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
 1094         const u_int8_t *macaddr, const char *func, int line)
 1095 #else
 1096 ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
 1097 #endif
 1098 {
 1099         struct ieee80211_node *ni;
 1100 
 1101         IEEE80211_NODE_LOCK(nt);
 1102         ni = _ieee80211_find_node(nt, macaddr);
 1103         IEEE80211_NODE_UNLOCK(nt);
 1104         return ni;
 1105 }
 1106 
 1107 /*
 1108  * Fake up a node; this handles node discovery in adhoc mode.
 1109  * Note that for the driver's benefit we we treat this like
 1110  * an association so the driver has an opportunity to setup
 1111  * it's private state.
 1112  */
 1113 struct ieee80211_node *
 1114 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
 1115         const u_int8_t macaddr[IEEE80211_ADDR_LEN])
 1116 {
 1117         struct ieee80211com *ic = nt->nt_ic;
 1118         struct ieee80211_node *ni;
 1119 
 1120         IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
 1121             "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
 1122         ni = ieee80211_dup_bss(nt, macaddr);
 1123         if (ni != NULL) {
 1124                 /* XXX no rate negotiation; just dup */
 1125                 ni->ni_rates = ic->ic_bss->ni_rates;
 1126                 if (ic->ic_newassoc != NULL)
 1127                         ic->ic_newassoc(ni, 1);
 1128                 /* XXX not right for 802.1x/WPA */
 1129                 ieee80211_node_authorize(ni);
 1130                 if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
 1131                         /*
 1132                          * Blindly propagate capabilities based on the
 1133                          * local configuration.  In particular this permits
 1134                          * us to use QoS to disable ACK's.
 1135                          */
 1136                         if (ic->ic_flags & IEEE80211_F_WME)
 1137                                 ni->ni_flags |= IEEE80211_NODE_QOS;
 1138                 }
 1139         }
 1140         return ni;
 1141 }
 1142 
 1143 #ifdef IEEE80211_DEBUG
 1144 static void
 1145 dump_probe_beacon(u_int8_t subtype, int isnew,
 1146         const u_int8_t mac[IEEE80211_ADDR_LEN],
 1147         const struct ieee80211_scanparams *sp)
 1148 {
 1149 
 1150         printf("[%s] %s%s on chan %u (bss chan %u) ",
 1151             ether_sprintf(mac), isnew ? "new " : "",
 1152             ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
 1153             sp->chan, sp->bchan);
 1154         ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
 1155         printf("\n");
 1156 
 1157         if (isnew) {
 1158                 printf("[%s] caps 0x%x bintval %u erp 0x%x", 
 1159                         ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
 1160                 if (sp->country != NULL) {
 1161 #ifdef __FreeBSD__
 1162                         printf(" country info %*D",
 1163                                 sp->country[1], sp->country+2, " ");
 1164 #else
 1165                         int i;
 1166                         printf(" country info");
 1167                         for (i = 0; i < sp->country[1]; i++)
 1168                                 printf(" %02x", sp->country[i+2]);
 1169 #endif
 1170                 }
 1171                 printf("\n");
 1172         }
 1173 }
 1174 #endif /* IEEE80211_DEBUG */
 1175 
 1176 static void
 1177 saveie(u_int8_t **iep, const u_int8_t *ie)
 1178 {
 1179 
 1180         if (ie == NULL)
 1181                 *iep = NULL;
 1182         else
 1183                 ieee80211_saveie(iep, ie);
 1184 }
 1185 
 1186 /*
 1187  * Process a beacon or probe response frame.
 1188  */
 1189 void
 1190 ieee80211_add_scan(struct ieee80211com *ic,
 1191         const struct ieee80211_scanparams *sp,
 1192         const struct ieee80211_frame *wh,
 1193         int subtype, int rssi, int rstamp)
 1194 {
 1195 #define ISPROBE(_st)    ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
 1196         struct ieee80211_node_table *nt = &ic->ic_scan;
 1197         struct ieee80211_node *ni;
 1198         int newnode = 0;
 1199 
 1200         ni = ieee80211_find_node(nt, wh->i_addr2);
 1201         if (ni == NULL) {
 1202                 /*
 1203                  * Create a new entry.
 1204                  */
 1205                 ni = ic->ic_node_alloc(nt);
 1206                 if (ni == NULL) {
 1207                         ic->ic_stats.is_rx_nodealloc++;
 1208                         return;
 1209                 }
 1210                 ieee80211_setup_node(nt, ni, wh->i_addr2);
 1211                 /*
 1212                  * XXX inherit from ic_bss.
 1213                  */
 1214                 ni->ni_authmode = ic->ic_bss->ni_authmode;
 1215                 ni->ni_txpower = ic->ic_bss->ni_txpower;
 1216                 ni->ni_vlan = ic->ic_bss->ni_vlan;      /* XXX?? */
 1217                 ieee80211_set_chan(ic, ni, ic->ic_curchan);
 1218                 ni->ni_rsn = ic->ic_bss->ni_rsn;
 1219                 newnode = 1;
 1220         }
 1221 #ifdef IEEE80211_DEBUG
 1222         if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
 1223                 dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
 1224 #endif
 1225         /* XXX ap beaconing multiple ssid w/ same bssid */
 1226         if (sp->ssid[1] != 0 &&
 1227             (ISPROBE(subtype) || ni->ni_esslen == 0)) {
 1228                 ni->ni_esslen = sp->ssid[1];
 1229                 memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
 1230                 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
 1231         }
 1232         ni->ni_scangen = ic->ic_scan.nt_scangen;
 1233         IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
 1234         ni->ni_rssi = rssi;
 1235         ni->ni_rstamp = rstamp;
 1236         memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
 1237         ni->ni_intval = sp->bintval;
 1238         ni->ni_capinfo = sp->capinfo;
 1239         ni->ni_chan = &ic->ic_channels[sp->chan];
 1240         ni->ni_fhdwell = sp->fhdwell;
 1241         ni->ni_fhindex = sp->fhindex;
 1242         ni->ni_erp = sp->erp;
 1243         if (sp->tim != NULL) {
 1244                 struct ieee80211_tim_ie *ie =
 1245                     (struct ieee80211_tim_ie *) sp->tim;
 1246 
 1247                 ni->ni_dtim_count = ie->tim_count;
 1248                 ni->ni_dtim_period = ie->tim_period;
 1249         }
 1250         /*
 1251          * Record the byte offset from the mac header to
 1252          * the start of the TIM information element for
 1253          * use by hardware and/or to speedup software
 1254          * processing of beacon frames.
 1255          */
 1256         ni->ni_timoff = sp->timoff;
 1257         /*
 1258          * Record optional information elements that might be
 1259          * used by applications or drivers.
 1260          */
 1261         saveie(&ni->ni_wme_ie, sp->wme);
 1262         saveie(&ni->ni_wpa_ie, sp->wpa);
 1263 
 1264         /* NB: must be after ni_chan is setup */
 1265         ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
 1266 
 1267         if (!newnode)
 1268                 ieee80211_free_node(ni);
 1269 #undef ISPROBE
 1270 }
 1271 
 1272 void
 1273 ieee80211_init_neighbor(struct ieee80211_node *ni,
 1274         const struct ieee80211_frame *wh,
 1275         const struct ieee80211_scanparams *sp)
 1276 {
 1277 
 1278         IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1279             "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
 1280         ni->ni_esslen = sp->ssid[1];
 1281         memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
 1282         IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
 1283         memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
 1284         ni->ni_intval = sp->bintval;
 1285         ni->ni_capinfo = sp->capinfo;
 1286         ni->ni_chan = ni->ni_ic->ic_curchan;
 1287         ni->ni_fhdwell = sp->fhdwell;
 1288         ni->ni_fhindex = sp->fhindex;
 1289         ni->ni_erp = sp->erp;
 1290         ni->ni_timoff = sp->timoff;
 1291         if (sp->wme != NULL)
 1292                 ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
 1293         if (sp->wpa != NULL)
 1294                 ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
 1295 
 1296         /* NB: must be after ni_chan is setup */
 1297         ieee80211_setup_rates(ni, sp->rates, sp->xrates,
 1298                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
 1299                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
 1300 }
 1301 
 1302 /*
 1303  * Do node discovery in adhoc mode on receipt of a beacon
 1304  * or probe response frame.  Note that for the driver's
 1305  * benefit we we treat this like an association so the
 1306  * driver has an opportunity to setup it's private state.
 1307  */
 1308 struct ieee80211_node *
 1309 ieee80211_add_neighbor(struct ieee80211com *ic,
 1310         const struct ieee80211_frame *wh,
 1311         const struct ieee80211_scanparams *sp)
 1312 {
 1313         struct ieee80211_node *ni;
 1314 
 1315         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1316             "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
 1317         ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
 1318         if (ni != NULL) {
 1319                 ieee80211_init_neighbor(ni, wh, sp);
 1320                 if (ic->ic_newassoc != NULL)
 1321                         ic->ic_newassoc(ni, 1);
 1322                 /* XXX not right for 802.1x/WPA */
 1323                 ieee80211_node_authorize(ni);
 1324         }
 1325         return ni;
 1326 }
 1327 
 1328 #define IS_CTL(wh) \
 1329         ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
 1330 #define IS_PSPOLL(wh) \
 1331         ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
 1332 /*
 1333  * Locate the node for sender, track state, and then pass the
 1334  * (referenced) node up to the 802.11 layer for its use.  We
 1335  * are required to pass some node so we fall back to ic_bss
 1336  * when this frame is from an unknown sender.  The 802.11 layer
 1337  * knows this means the sender wasn't in the node table and
 1338  * acts accordingly. 
 1339  */
 1340 struct ieee80211_node *
 1341 #ifdef IEEE80211_DEBUG_REFCNT
 1342 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
 1343         const struct ieee80211_frame_min *wh, const char *func, int line)
 1344 #else
 1345 ieee80211_find_rxnode(struct ieee80211com *ic,
 1346         const struct ieee80211_frame_min *wh)
 1347 #endif
 1348 {
 1349         struct ieee80211_node_table *nt;
 1350         struct ieee80211_node *ni;
 1351 
 1352         /* XXX may want scanned nodes in the neighbor table for adhoc */
 1353         if (ic->ic_opmode == IEEE80211_M_STA ||
 1354             ic->ic_opmode == IEEE80211_M_MONITOR ||
 1355             (ic->ic_flags & IEEE80211_F_SCAN))
 1356                 nt = &ic->ic_scan;
 1357         else
 1358                 nt = &ic->ic_sta;
 1359         /* XXX check ic_bss first in station mode */
 1360         /* XXX 4-address frames? */
 1361         IEEE80211_NODE_LOCK(nt);
 1362         if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
 1363                 ni = _ieee80211_find_node(nt, wh->i_addr1);
 1364         else
 1365                 ni = _ieee80211_find_node(nt, wh->i_addr2);
 1366         if (ni == NULL)
 1367                 ni = ieee80211_ref_node(ic->ic_bss);
 1368         IEEE80211_NODE_UNLOCK(nt);
 1369 
 1370         return ni;
 1371 }
 1372 
 1373 /*
 1374  * Like ieee80211_find_rxnode but use the supplied h/w
 1375  * key index as a hint to locate the node in the key
 1376  * mapping table.  If an entry is present at the key
 1377  * index we return it; otherwise do a normal lookup and
 1378  * update the mapping table if the station has a unicast
 1379  * key assigned to it.
 1380  */
 1381 struct ieee80211_node *
 1382 #ifdef IEEE80211_DEBUG_REFCNT
 1383 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
 1384         const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
 1385         const char *func, int line)
 1386 #else
 1387 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
 1388         const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
 1389 #endif
 1390 {
 1391         struct ieee80211_node_table *nt;
 1392         struct ieee80211_node *ni;
 1393 
 1394         if (ic->ic_opmode == IEEE80211_M_STA ||
 1395             ic->ic_opmode == IEEE80211_M_MONITOR ||
 1396             (ic->ic_flags & IEEE80211_F_SCAN))
 1397                 nt = &ic->ic_scan;
 1398         else
 1399                 nt = &ic->ic_sta;
 1400         IEEE80211_NODE_LOCK(nt);
 1401         if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
 1402                 ni = nt->nt_keyixmap[keyix];
 1403         else
 1404                 ni = NULL;
 1405         if (ni == NULL) {
 1406                 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
 1407                         ni = _ieee80211_find_node(nt, wh->i_addr1);
 1408                 else
 1409                         ni = _ieee80211_find_node(nt, wh->i_addr2);
 1410                 if (ni == NULL)
 1411                         ni = ieee80211_ref_node(ic->ic_bss);
 1412                 if (nt->nt_keyixmap != NULL) {
 1413                         /*
 1414                          * If the station has a unicast key cache slot
 1415                          * assigned update the key->node mapping table.
 1416                          */
 1417                         keyix = ni->ni_ucastkey.wk_rxkeyix;
 1418                         /* XXX can keyixmap[keyix] != NULL? */
 1419                         if (keyix < nt->nt_keyixmax &&
 1420                             nt->nt_keyixmap[keyix] == NULL) {
 1421                                 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1422                                     "%s: add key map entry %p<%s> refcnt %d\n",
 1423                                     __func__, ni, ether_sprintf(ni->ni_macaddr),
 1424                                     ieee80211_node_refcnt(ni)+1);
 1425                                 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
 1426                         }
 1427                 }
 1428         } else {
 1429                 ieee80211_ref_node(ni);
 1430         }
 1431         IEEE80211_NODE_UNLOCK(nt);
 1432 
 1433         return ni;
 1434 }
 1435 #undef IS_PSPOLL
 1436 #undef IS_CTL
 1437 
 1438 /*
 1439  * Return a reference to the appropriate node for sending
 1440  * a data frame.  This handles node discovery in adhoc networks.
 1441  */
 1442 struct ieee80211_node *
 1443 #ifdef IEEE80211_DEBUG_REFCNT
 1444 ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
 1445         const char *func, int line)
 1446 #else
 1447 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
 1448 #endif
 1449 {
 1450         struct ieee80211_node_table *nt = &ic->ic_sta;
 1451         struct ieee80211_node *ni;
 1452 
 1453         /*
 1454          * The destination address should be in the node table
 1455          * unless this is a multicast/broadcast frame.  We can
 1456          * also optimize station mode operation, all frames go
 1457          * to the bss node.
 1458          */
 1459         /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
 1460         IEEE80211_NODE_LOCK(nt);
 1461         if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
 1462                 ni = ieee80211_ref_node(ic->ic_bss);
 1463         else {
 1464                 ni = _ieee80211_find_node(nt, macaddr);
 1465                 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 
 1466                     (ni != NULL && ni->ni_associd == 0)) {
 1467                         /*
 1468                          * Station is not associated; don't permit the
 1469                          * data frame to be sent by returning NULL.  This
 1470                          * is kinda a kludge but the least intrusive way
 1471                          * to add this check into all drivers.
 1472                          */
 1473                         ieee80211_unref_node(&ni);      /* NB: null's ni */
 1474                 }
 1475         }
 1476         IEEE80211_NODE_UNLOCK(nt);
 1477 
 1478         if (ni == NULL) {
 1479                 if (ic->ic_opmode == IEEE80211_M_IBSS ||
 1480                     ic->ic_opmode == IEEE80211_M_AHDEMO) {
 1481                         /*
 1482                          * In adhoc mode cons up a node for the destination.
 1483                          * Note that we need an additional reference for the
 1484                          * caller to be consistent with _ieee80211_find_node.
 1485                          */
 1486                         ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
 1487                         if (ni != NULL)
 1488                                 (void) ieee80211_ref_node(ni);
 1489                 } else {
 1490                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
 1491                                 "[%s] no node, discard frame (%s)\n",
 1492                                 ether_sprintf(macaddr), __func__);
 1493                         ic->ic_stats.is_tx_nonode++;
 1494                 }
 1495         }
 1496         return ni;
 1497 }
 1498 
 1499 /*
 1500  * Like find but search based on the channel too.
 1501  */
 1502 struct ieee80211_node *
 1503 #ifdef IEEE80211_DEBUG_REFCNT
 1504 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
 1505         const u_int8_t *macaddr, struct ieee80211_channel *chan,
 1506         const char *func, int line)
 1507 #else
 1508 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
 1509         const u_int8_t *macaddr, struct ieee80211_channel *chan)
 1510 #endif
 1511 {
 1512         struct ieee80211_node *ni;
 1513         int hash;
 1514 
 1515         hash = IEEE80211_NODE_HASH(macaddr);
 1516         IEEE80211_NODE_LOCK(nt);
 1517         LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
 1518                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
 1519                     ni->ni_chan == chan) {
 1520                         ieee80211_ref_node(ni);         /* mark referenced */
 1521                         IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
 1522                             REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
 1523                             ieee80211_node_refcnt(ni));
 1524                         break;
 1525                 }
 1526         }
 1527         IEEE80211_NODE_UNLOCK(nt);
 1528         return ni;
 1529 }
 1530 
 1531 /*
 1532  * Like find but search based on the ssid too.
 1533  */
 1534 struct ieee80211_node *
 1535 #ifdef IEEE80211_DEBUG_REFCNT
 1536 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
 1537         const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
 1538         const char *func, int line)
 1539 #else
 1540 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
 1541         const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
 1542 #endif
 1543 {
 1544 #define MATCH_SSID(ni, ssid, ssidlen) \
 1545         (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
 1546         static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
 1547         struct ieee80211com *ic = nt->nt_ic;
 1548         struct ieee80211_node *ni;
 1549         int hash;
 1550 
 1551         IEEE80211_NODE_LOCK(nt);
 1552         /*
 1553          * A mac address that is all zero means match only the ssid;
 1554          * otherwise we must match both.
 1555          */
 1556         if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
 1557                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 1558                         if (MATCH_SSID(ni, ssid, ssidlen))
 1559                                 break;
 1560                 }
 1561         } else {
 1562                 hash = IEEE80211_NODE_HASH(macaddr);
 1563                 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
 1564                         if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
 1565                             MATCH_SSID(ni, ssid, ssidlen))
 1566                                 break;
 1567                 }
 1568         }
 1569         if (ni != NULL) {
 1570                 ieee80211_ref_node(ni); /* mark referenced */
 1571                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1572                      REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
 1573                      ieee80211_node_refcnt(ni));
 1574         }
 1575         IEEE80211_NODE_UNLOCK(nt);
 1576         return ni;
 1577 #undef MATCH_SSID
 1578 }
 1579 
 1580 static void
 1581 _ieee80211_free_node(struct ieee80211_node *ni)
 1582 {
 1583         struct ieee80211com *ic = ni->ni_ic;
 1584         struct ieee80211_node_table *nt = ni->ni_table;
 1585 
 1586         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1587                 "%s %p<%s> in %s table\n", __func__, ni,
 1588                 ether_sprintf(ni->ni_macaddr),
 1589                 nt != NULL ? nt->nt_name : "<gone>");
 1590 
 1591         IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
 1592         if (nt != NULL) {
 1593                 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
 1594                 LIST_REMOVE(ni, ni_hash);
 1595         }
 1596         ic->ic_node_free(ni);
 1597 }
 1598 
 1599 void
 1600 #ifdef IEEE80211_DEBUG_REFCNT
 1601 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
 1602 #else
 1603 ieee80211_free_node(struct ieee80211_node *ni)
 1604 #endif
 1605 {
 1606         struct ieee80211_node_table *nt = ni->ni_table;
 1607 
 1608 #ifdef IEEE80211_DEBUG_REFCNT
 1609         IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1610                 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
 1611                  ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
 1612 #endif
 1613         if (nt != NULL) {
 1614                 IEEE80211_NODE_LOCK(nt);
 1615                 if (ieee80211_node_dectestref(ni)) {
 1616                         /*
 1617                          * Last reference, reclaim state.
 1618                          */
 1619                         _ieee80211_free_node(ni);
 1620                 } else if (ieee80211_node_refcnt(ni) == 1 &&
 1621                     nt->nt_keyixmap != NULL) {
 1622                         ieee80211_keyix keyix;
 1623                         /*
 1624                          * Check for a last reference in the key mapping table.
 1625                          */
 1626                         keyix = ni->ni_ucastkey.wk_rxkeyix;
 1627                         if (keyix < nt->nt_keyixmax &&
 1628                             nt->nt_keyixmap[keyix] == ni) {
 1629                                 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1630                                     "%s: %p<%s> clear key map entry", __func__,
 1631                                     ni, ether_sprintf(ni->ni_macaddr));
 1632                                 nt->nt_keyixmap[keyix] = NULL;
 1633                                 ieee80211_node_decref(ni); /* XXX needed? */
 1634                                 _ieee80211_free_node(ni);
 1635                         }
 1636                 }
 1637                 IEEE80211_NODE_UNLOCK(nt);
 1638         } else {
 1639                 if (ieee80211_node_dectestref(ni))
 1640                         _ieee80211_free_node(ni);
 1641         }
 1642 }
 1643 
 1644 /*
 1645  * Reclaim a unicast key and clear any key cache state.
 1646  */
 1647 int
 1648 ieee80211_node_delucastkey(struct ieee80211_node *ni)
 1649 {
 1650         struct ieee80211com *ic = ni->ni_ic;
 1651         struct ieee80211_node_table *nt = &ic->ic_sta;
 1652         struct ieee80211_node *nikey;
 1653         ieee80211_keyix keyix;
 1654         int isowned, status;
 1655 
 1656         /*
 1657          * NB: We must beware of LOR here; deleting the key
 1658          * can cause the crypto layer to block traffic updates
 1659          * which can generate a LOR against the node table lock;
 1660          * grab it here and stash the key index for our use below.
 1661          *
 1662          * Must also beware of recursion on the node table lock.
 1663          * When called from node_cleanup we may already have
 1664          * the node table lock held.  Unfortunately there's no
 1665          * way to separate out this path so we must do this
 1666          * conditionally.
 1667          */
 1668         isowned = IEEE80211_NODE_IS_LOCKED(nt);
 1669         if (!isowned)
 1670                 IEEE80211_NODE_LOCK(nt);
 1671         keyix = ni->ni_ucastkey.wk_rxkeyix;
 1672         status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
 1673         if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
 1674                 nikey = nt->nt_keyixmap[keyix];
 1675                 nt->nt_keyixmap[keyix] = NULL;;
 1676         } else
 1677                 nikey = NULL;
 1678         if (!isowned)
 1679                 IEEE80211_NODE_UNLOCK(&ic->ic_sta);
 1680 
 1681         if (nikey != NULL) {
 1682                 KASSERT(nikey == ni,
 1683                         ("key map out of sync, ni %p nikey %p", ni, nikey));
 1684                 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1685                         "%s: delete key map entry %p<%s> refcnt %d\n",
 1686                         __func__, ni, ether_sprintf(ni->ni_macaddr),
 1687                         ieee80211_node_refcnt(ni)-1);
 1688                 ieee80211_free_node(ni);
 1689         }
 1690         return status;
 1691 }
 1692 
 1693 /*
 1694  * Reclaim a node.  If this is the last reference count then
 1695  * do the normal free work.  Otherwise remove it from the node
 1696  * table and mark it gone by clearing the back-reference.
 1697  */
 1698 static void
 1699 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
 1700 {
 1701         ieee80211_keyix keyix;
 1702 
 1703         IEEE80211_NODE_LOCK_ASSERT(nt);
 1704 
 1705         IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1706                 "%s: remove %p<%s> from %s table, refcnt %d\n",
 1707                 __func__, ni, ether_sprintf(ni->ni_macaddr),
 1708                 nt->nt_name, ieee80211_node_refcnt(ni)-1);
 1709         /*
 1710          * Clear any entry in the unicast key mapping table.
 1711          * We need to do it here so rx lookups don't find it
 1712          * in the mapping table even if it's not in the hash
 1713          * table.  We cannot depend on the mapping table entry
 1714          * being cleared because the node may not be free'd.
 1715          */
 1716         keyix = ni->ni_ucastkey.wk_rxkeyix;
 1717         if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
 1718             nt->nt_keyixmap[keyix] == ni) {
 1719                 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
 1720                         "%s: %p<%s> clear key map entry\n",
 1721                         __func__, ni, ether_sprintf(ni->ni_macaddr));
 1722                 nt->nt_keyixmap[keyix] = NULL;
 1723                 ieee80211_node_decref(ni);      /* NB: don't need free */
 1724         }
 1725         if (!ieee80211_node_dectestref(ni)) {
 1726                 /*
 1727                  * Other references are present, just remove the
 1728                  * node from the table so it cannot be found.  When
 1729                  * the references are dropped storage will be
 1730                  * reclaimed.
 1731                  */
 1732                 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
 1733                 LIST_REMOVE(ni, ni_hash);
 1734                 ni->ni_table = NULL;            /* clear reference */
 1735         } else
 1736                 _ieee80211_free_node(ni);
 1737 }
 1738 
 1739 static void
 1740 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
 1741 {
 1742         struct ieee80211com *ic = nt->nt_ic;
 1743         struct ieee80211_node *ni;
 1744 
 1745         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1746                 "%s: free all nodes in %s table\n", __func__, nt->nt_name);
 1747 
 1748         while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
 1749                 if (ni->ni_associd != 0) {
 1750                         if (ic->ic_auth->ia_node_leave != NULL)
 1751                                 ic->ic_auth->ia_node_leave(ic, ni);
 1752                         IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
 1753                 }
 1754                 node_reclaim(nt, ni);
 1755         }
 1756         ieee80211_reset_erp(ic);
 1757 }
 1758 
 1759 static void
 1760 ieee80211_free_allnodes(struct ieee80211_node_table *nt)
 1761 {
 1762 
 1763         IEEE80211_NODE_LOCK(nt);
 1764         ieee80211_free_allnodes_locked(nt);
 1765         IEEE80211_NODE_UNLOCK(nt);
 1766 }
 1767 
 1768 /*
 1769  * Timeout entries in the scan cache.
 1770  */
 1771 static void
 1772 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
 1773 {
 1774         struct ieee80211com *ic = nt->nt_ic;
 1775         struct ieee80211_node *ni, *tni;
 1776 
 1777         IEEE80211_NODE_LOCK(nt);
 1778         ni = ic->ic_bss;
 1779         /* XXX belongs elsewhere */
 1780         if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
 1781                 m_freem(ni->ni_rxfrag[0]);
 1782                 ni->ni_rxfrag[0] = NULL;
 1783         }
 1784         TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
 1785                 if (ni->ni_inact && --ni->ni_inact == 0) {
 1786                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1787                             "[%s] scan candidate purged from cache "
 1788                             "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
 1789                             ieee80211_node_refcnt(ni));
 1790                         node_reclaim(nt, ni);
 1791                 }
 1792         }
 1793         IEEE80211_NODE_UNLOCK(nt);
 1794 
 1795         nt->nt_inact_timer = IEEE80211_INACT_WAIT;
 1796 }
 1797 
 1798 /*
 1799  * Timeout inactive stations and do related housekeeping.
 1800  * Note that we cannot hold the node lock while sending a
 1801  * frame as this would lead to a LOR.  Instead we use a
 1802  * generation number to mark nodes that we've scanned and
 1803  * drop the lock and restart a scan if we have to time out
 1804  * a node.  Since we are single-threaded by virtue of
 1805  * controlling the inactivity timer we can be sure this will
 1806  * process each node only once.
 1807  */
 1808 static void
 1809 ieee80211_timeout_stations(struct ieee80211_node_table *nt)
 1810 {
 1811         struct ieee80211com *ic = nt->nt_ic;
 1812         struct ieee80211_node *ni;
 1813         u_int gen;
 1814         int isadhoc;
 1815 
 1816         isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
 1817                    ic->ic_opmode == IEEE80211_M_AHDEMO);
 1818         IEEE80211_SCAN_LOCK(nt);
 1819         gen = ++nt->nt_scangen;
 1820         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 1821                 "%s: %s scangen %u\n", __func__, nt->nt_name, gen);
 1822 restart:
 1823         IEEE80211_NODE_LOCK(nt);
 1824         TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 1825                 if (ni->ni_scangen == gen)      /* previously handled */
 1826                         continue;
 1827                 ni->ni_scangen = gen;
 1828                 /*
 1829                  * Ignore entries for which have yet to receive an
 1830                  * authentication frame.  These are transient and
 1831                  * will be reclaimed when the last reference to them
 1832                  * goes away (when frame xmits complete).
 1833                  */
 1834                 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
 1835                     (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
 1836                         continue;
 1837                 /*
 1838                  * Free fragment if not needed anymore
 1839                  * (last fragment older than 1s).
 1840                  * XXX doesn't belong here
 1841                  */
 1842                 if (ni->ni_rxfrag[0] != NULL &&
 1843                     ticks > ni->ni_rxfragstamp + hz) {
 1844                         m_freem(ni->ni_rxfrag[0]);
 1845                         ni->ni_rxfrag[0] = NULL;
 1846                 }
 1847                 /*
 1848                  * Special case ourself; we may be idle for extended periods
 1849                  * of time and regardless reclaiming our state is wrong.
 1850                  */
 1851                 if (ni == ic->ic_bss)
 1852                         continue;
 1853                 ni->ni_inact--;
 1854                 if (ni->ni_associd != 0 || isadhoc) {
 1855                         /*
 1856                          * Age frames on the power save queue. The
 1857                          * aging interval is 4 times the listen
 1858                          * interval specified by the station.  This
 1859                          * number is factored into the age calculations
 1860                          * when the frame is placed on the queue.  We
 1861                          * store ages as time differences we can check
 1862                          * and/or adjust only the head of the list.
 1863                          */
 1864                         if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
 1865                                 struct mbuf *m;
 1866                                 int discard = 0;
 1867 
 1868                                 IEEE80211_NODE_SAVEQ_LOCK(ni);
 1869                                 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
 1870                                      M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
 1871 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
 1872                                         _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
 1873                                         m_freem(m);
 1874                                         discard++;
 1875                                 }
 1876                                 if (m != NULL)
 1877                                         M_AGE_SUB(m, IEEE80211_INACT_WAIT);
 1878                                 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
 1879 
 1880                                 if (discard != 0) {
 1881                                         IEEE80211_DPRINTF(ic,
 1882                                             IEEE80211_MSG_POWER,
 1883                                             "[%s] discard %u frames for age\n",
 1884                                             ether_sprintf(ni->ni_macaddr),
 1885                                             discard);
 1886                                         IEEE80211_NODE_STAT_ADD(ni,
 1887                                                 ps_discard, discard);
 1888                                         if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
 1889                                                 ic->ic_set_tim(ni, 0);
 1890                                 }
 1891                         }
 1892                         /*
 1893                          * Probe the station before time it out.  We
 1894                          * send a null data frame which may not be
 1895                          * universally supported by drivers (need it
 1896                          * for ps-poll support so it should be...).
 1897                          */
 1898                         if (0 < ni->ni_inact &&
 1899                             ni->ni_inact <= ic->ic_inact_probe) {
 1900                                 IEEE80211_NOTE(ic,
 1901                                     IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
 1902                                     ni, "%s",
 1903                                     "probe station due to inactivity");
 1904                                 /*
 1905                                  * Grab a reference before unlocking the table
 1906                                  * so the node cannot be reclaimed before we
 1907                                  * send the frame. ieee80211_send_nulldata
 1908                                  * understands we've done this and reclaims the
 1909                                  * ref for us as needed.
 1910                                  */
 1911                                 ieee80211_ref_node(ni);
 1912                                 IEEE80211_NODE_UNLOCK(nt);
 1913                                 ieee80211_send_nulldata(ni);
 1914                                 /* XXX stat? */
 1915                                 goto restart;
 1916                         }
 1917                 }
 1918                 if (ni->ni_inact <= 0) {
 1919                         IEEE80211_NOTE(ic,
 1920                             IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
 1921                             "station timed out due to inactivity "
 1922                             "(refcnt %u)", ieee80211_node_refcnt(ni));
 1923                         /*
 1924                          * Send a deauthenticate frame and drop the station.
 1925                          * This is somewhat complicated due to reference counts
 1926                          * and locking.  At this point a station will typically
 1927                          * have a reference count of 1.  ieee80211_node_leave
 1928                          * will do a "free" of the node which will drop the
 1929                          * reference count.  But in the meantime a reference
 1930                          * wil be held by the deauth frame.  The actual reclaim
 1931                          * of the node will happen either after the tx is
 1932                          * completed or by ieee80211_node_leave.
 1933                          *
 1934                          * Separately we must drop the node lock before sending
 1935                          * in case the driver takes a lock, as this will result
 1936                          * in  LOR between the node lock and the driver lock.
 1937                          */
 1938                         ieee80211_ref_node(ni);
 1939                         IEEE80211_NODE_UNLOCK(nt);
 1940                         if (ni->ni_associd != 0) {
 1941                                 IEEE80211_SEND_MGMT(ic, ni,
 1942                                     IEEE80211_FC0_SUBTYPE_DEAUTH,
 1943                                     IEEE80211_REASON_AUTH_EXPIRE);
 1944                         }
 1945                         ieee80211_node_leave(ic, ni);
 1946                         ieee80211_free_node(ni);
 1947                         ic->ic_stats.is_node_timeout++;
 1948                         goto restart;
 1949                 }
 1950         }
 1951         IEEE80211_NODE_UNLOCK(nt);
 1952 
 1953         IEEE80211_SCAN_UNLOCK(nt);
 1954 
 1955         nt->nt_inact_timer = IEEE80211_INACT_WAIT;
 1956 }
 1957 
 1958 void
 1959 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
 1960 {
 1961         struct ieee80211_node *ni;
 1962         u_int gen;
 1963 
 1964         IEEE80211_SCAN_LOCK(nt);
 1965         gen = ++nt->nt_scangen;
 1966 restart:
 1967         IEEE80211_NODE_LOCK(nt);
 1968         TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 1969                 if (ni->ni_scangen != gen) {
 1970                         ni->ni_scangen = gen;
 1971                         (void) ieee80211_ref_node(ni);
 1972                         IEEE80211_NODE_UNLOCK(nt);
 1973                         (*f)(arg, ni);
 1974                         ieee80211_free_node(ni);
 1975                         goto restart;
 1976                 }
 1977         }
 1978         IEEE80211_NODE_UNLOCK(nt);
 1979 
 1980         IEEE80211_SCAN_UNLOCK(nt);
 1981 }
 1982 
 1983 void
 1984 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
 1985 {
 1986         printf("0x%p: mac %s refcnt %d\n", ni,
 1987                 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
 1988         printf("\tscangen %u authmode %u flags 0x%x\n",
 1989                 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
 1990         printf("\tassocid 0x%x txpower %u vlan %u\n",
 1991                 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
 1992         printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
 1993                 ni->ni_txseqs[IEEE80211_NONQOS_TID],
 1994                 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
 1995                 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
 1996                 ni->ni_rxfragstamp);
 1997         printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
 1998                 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
 1999         printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
 2000                 ether_sprintf(ni->ni_bssid),
 2001                 ni->ni_esslen, ni->ni_essid,
 2002                 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
 2003         printf("\tfails %u inact %u txrate %u\n",
 2004                 ni->ni_fails, ni->ni_inact, ni->ni_txrate);
 2005 }
 2006 
 2007 void
 2008 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
 2009 {
 2010         ieee80211_iterate_nodes(nt,
 2011                 (ieee80211_iter_func *) ieee80211_dump_node, nt);
 2012 }
 2013 
 2014 /*
 2015  * Handle a station joining an 11g network.
 2016  */
 2017 static void
 2018 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
 2019 {
 2020 
 2021         /*
 2022          * Station isn't capable of short slot time.  Bump
 2023          * the count of long slot time stations and disable
 2024          * use of short slot time.  Note that the actual switch
 2025          * over to long slot time use may not occur until the
 2026          * next beacon transmission (per sec. 7.3.1.4 of 11g).
 2027          */
 2028         if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
 2029                 ic->ic_longslotsta++;
 2030                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2031                     "[%s] station needs long slot time, count %d\n",
 2032                     ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
 2033                 /* XXX vap's w/ conflicting needs won't work */
 2034                 ieee80211_set_shortslottime(ic, 0);
 2035         }
 2036         /*
 2037          * If the new station is not an ERP station
 2038          * then bump the counter and enable protection
 2039          * if configured.
 2040          */
 2041         if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
 2042                 ic->ic_nonerpsta++;
 2043                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2044                     "[%s] station is !ERP, %d non-ERP stations associated\n",
 2045                     ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
 2046                 /*
 2047                  * If protection is configured, enable it.
 2048                  */
 2049                 if (ic->ic_protmode != IEEE80211_PROT_NONE) {
 2050                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2051                             "%s: enable use of protection\n", __func__);
 2052                         ic->ic_flags |= IEEE80211_F_USEPROT;
 2053                 }
 2054                 /*
 2055                  * If station does not support short preamble
 2056                  * then we must enable use of Barker preamble.
 2057                  */
 2058                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
 2059                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2060                             "[%s] station needs long preamble\n",
 2061                             ether_sprintf(ni->ni_macaddr));
 2062                         ic->ic_flags |= IEEE80211_F_USEBARKER;
 2063                         ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
 2064                 }
 2065                 if (ic->ic_nonerpsta == 1)
 2066                         ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
 2067         } else
 2068                 ni->ni_flags |= IEEE80211_NODE_ERP;
 2069 }
 2070 
 2071 void
 2072 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
 2073 {
 2074         int newassoc;
 2075 
 2076         if (ni->ni_associd == 0) {
 2077                 u_int16_t aid;
 2078 
 2079                 /*
 2080                  * It would be good to search the bitmap
 2081                  * more efficiently, but this will do for now.
 2082                  */
 2083                 for (aid = 1; aid < ic->ic_max_aid; aid++) {
 2084                         if (!IEEE80211_AID_ISSET(aid,
 2085                             ic->ic_aid_bitmap))
 2086                                 break;
 2087                 }
 2088                 if (aid >= ic->ic_max_aid) {
 2089                         IEEE80211_SEND_MGMT(ic, ni, resp,
 2090                             IEEE80211_REASON_ASSOC_TOOMANY);
 2091                         ieee80211_node_leave(ic, ni);
 2092                         return;
 2093                 }
 2094                 ni->ni_associd = aid | 0xc000;
 2095                 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
 2096                 ic->ic_sta_assoc++;
 2097                 newassoc = 1;
 2098                 if (ic->ic_curmode == IEEE80211_MODE_11G &&
 2099                     IEEE80211_IS_CHAN_FULL(ni->ni_chan))
 2100                         ieee80211_node_join_11g(ic, ni);
 2101         } else
 2102                 newassoc = 0;
 2103 
 2104         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
 2105             "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
 2106             ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
 2107             IEEE80211_NODE_AID(ni),
 2108             ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
 2109             ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
 2110             ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
 2111             ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
 2112         );
 2113 
 2114         /* give driver a chance to setup state like ni_txrate */
 2115         if (ic->ic_newassoc != NULL)
 2116                 ic->ic_newassoc(ni, newassoc);
 2117         ni->ni_inact_reload = ic->ic_inact_auth;
 2118         ni->ni_inact = ni->ni_inact_reload;
 2119         IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
 2120         /* tell the authenticator about new station */
 2121         if (ic->ic_auth->ia_node_join != NULL)
 2122                 ic->ic_auth->ia_node_join(ic, ni);
 2123         ieee80211_notify_node_join(ic, ni, newassoc);
 2124 }
 2125 
 2126 /*
 2127  * Handle a station leaving an 11g network.
 2128  */
 2129 static void
 2130 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
 2131 {
 2132 
 2133         KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
 2134              ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
 2135               ni->ni_chan->ic_flags, ic->ic_curmode));
 2136 
 2137         /*
 2138          * If a long slot station do the slot time bookkeeping.
 2139          */
 2140         if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
 2141                 KASSERT(ic->ic_longslotsta > 0,
 2142                     ("bogus long slot station count %d", ic->ic_longslotsta));
 2143                 ic->ic_longslotsta--;
 2144                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2145                     "[%s] long slot time station leaves, count now %d\n",
 2146                     ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
 2147                 if (ic->ic_longslotsta == 0) {
 2148                         /*
 2149                          * Re-enable use of short slot time if supported
 2150                          * and not operating in IBSS mode (per spec).
 2151                          */
 2152                         if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
 2153                             ic->ic_opmode != IEEE80211_M_IBSS) {
 2154                                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2155                                     "%s: re-enable use of short slot time\n",
 2156                                     __func__);
 2157                                 ieee80211_set_shortslottime(ic, 1);
 2158                         }
 2159                 }
 2160         }
 2161         /*
 2162          * If a non-ERP station do the protection-related bookkeeping.
 2163          */
 2164         if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
 2165                 KASSERT(ic->ic_nonerpsta > 0,
 2166                     ("bogus non-ERP station count %d", ic->ic_nonerpsta));
 2167                 ic->ic_nonerpsta--;
 2168                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2169                     "[%s] non-ERP station leaves, count now %d\n",
 2170                     ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
 2171                 if (ic->ic_nonerpsta == 0) {
 2172                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2173                                 "%s: disable use of protection\n", __func__);
 2174                         ic->ic_flags &= ~IEEE80211_F_USEPROT;
 2175                         /* XXX verify mode? */
 2176                         if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
 2177                                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
 2178                                     "%s: re-enable use of short preamble\n",
 2179                                     __func__);
 2180                                 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
 2181                                 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
 2182                         }
 2183                         ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
 2184                 }
 2185         }
 2186 }
 2187 
 2188 /*
 2189  * Handle bookkeeping for station deauthentication/disassociation
 2190  * when operating as an ap.
 2191  */
 2192 void
 2193 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
 2194 {
 2195         struct ieee80211_node_table *nt = ni->ni_table;
 2196 
 2197         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
 2198             "[%s] station with aid %d leaves\n",
 2199             ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
 2200 
 2201         KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
 2202                 ic->ic_opmode == IEEE80211_M_IBSS ||
 2203                 ic->ic_opmode == IEEE80211_M_AHDEMO,
 2204                 ("unexpected operating mode %u", ic->ic_opmode));
 2205         /*
 2206          * If node wasn't previously associated all
 2207          * we need to do is reclaim the reference.
 2208          */
 2209         /* XXX ibss mode bypasses 11g and notification */
 2210         if (ni->ni_associd == 0)
 2211                 goto done;
 2212         /*
 2213          * Tell the authenticator the station is leaving.
 2214          * Note that we must do this before yanking the
 2215          * association id as the authenticator uses the
 2216          * associd to locate it's state block.
 2217          */
 2218         if (ic->ic_auth->ia_node_leave != NULL)
 2219                 ic->ic_auth->ia_node_leave(ic, ni);
 2220         IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
 2221         ni->ni_associd = 0;
 2222         ic->ic_sta_assoc--;
 2223 
 2224         if (ic->ic_curmode == IEEE80211_MODE_11G &&
 2225             IEEE80211_IS_CHAN_FULL(ni->ni_chan))
 2226                 ieee80211_node_leave_11g(ic, ni);
 2227         /*
 2228          * Cleanup station state.  In particular clear various
 2229          * state that might otherwise be reused if the node
 2230          * is reused before the reference count goes to zero
 2231          * (and memory is reclaimed).
 2232          */
 2233         ieee80211_sta_leave(ic, ni);
 2234 done:
 2235         /*
 2236          * Remove the node from any table it's recorded in and
 2237          * drop the caller's reference.  Removal from the table
 2238          * is important to insure the node is not reprocessed
 2239          * for inactivity.
 2240          */
 2241         if (nt != NULL) {
 2242                 IEEE80211_NODE_LOCK(nt);
 2243                 node_reclaim(nt, ni);
 2244                 IEEE80211_NODE_UNLOCK(nt);
 2245         } else
 2246                 ieee80211_free_node(ni);
 2247 }
 2248 
 2249 u_int8_t
 2250 ieee80211_getrssi(struct ieee80211com *ic)
 2251 {
 2252 #define NZ(x)   ((x) == 0 ? 1 : (x))
 2253         struct ieee80211_node_table *nt = &ic->ic_sta;
 2254         u_int32_t rssi_samples, rssi_total;
 2255         struct ieee80211_node *ni;
 2256 
 2257         rssi_total = 0;
 2258         rssi_samples = 0;
 2259         switch (ic->ic_opmode) {
 2260         case IEEE80211_M_IBSS:          /* average of all ibss neighbors */
 2261                 /* XXX locking */
 2262                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
 2263                         if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
 2264                                 rssi_samples++;
 2265                                 rssi_total += ic->ic_node_getrssi(ni);
 2266                         }
 2267                 break;
 2268         case IEEE80211_M_AHDEMO:        /* average of all neighbors */
 2269                 /* XXX locking */
 2270                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
 2271                         rssi_samples++;
 2272                         rssi_total += ic->ic_node_getrssi(ni);
 2273                 }
 2274                 break;
 2275         case IEEE80211_M_HOSTAP:        /* average of all associated stations */
 2276                 /* XXX locking */
 2277                 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
 2278                         if (IEEE80211_AID(ni->ni_associd) != 0) {
 2279                                 rssi_samples++;
 2280                                 rssi_total += ic->ic_node_getrssi(ni);
 2281                         }
 2282                 break;
 2283         case IEEE80211_M_MONITOR:       /* XXX */
 2284         case IEEE80211_M_STA:           /* use stats from associated ap */
 2285         default:
 2286                 if (ic->ic_bss != NULL)
 2287                         rssi_total = ic->ic_node_getrssi(ic->ic_bss);
 2288                 rssi_samples = 1;
 2289                 break;
 2290         }
 2291         return rssi_total / NZ(rssi_samples);
 2292 #undef NZ
 2293 }
 2294 
 2295 /*
 2296  * Indicate whether there are frames queued for a station in power-save mode.
 2297  */
 2298 static void
 2299 ieee80211_set_tim(struct ieee80211_node *ni, int set)
 2300 {
 2301         struct ieee80211com *ic = ni->ni_ic;
 2302         u_int16_t aid;
 2303 
 2304         KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
 2305                 ic->ic_opmode == IEEE80211_M_IBSS,
 2306                 ("operating mode %u", ic->ic_opmode));
 2307 
 2308         aid = IEEE80211_AID(ni->ni_associd);
 2309         KASSERT(aid < ic->ic_max_aid,
 2310                 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
 2311 
 2312         IEEE80211_BEACON_LOCK(ic);
 2313         if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
 2314                 if (set) {
 2315                         setbit(ic->ic_tim_bitmap, aid);
 2316                         ic->ic_ps_pending++;
 2317                 } else {
 2318                         clrbit(ic->ic_tim_bitmap, aid);
 2319                         ic->ic_ps_pending--;
 2320                 }
 2321                 ic->ic_flags |= IEEE80211_F_TIMUPDATE;
 2322         }
 2323         IEEE80211_BEACON_UNLOCK(ic);
 2324 }
 2325 
 2326 /*
 2327  * Node table support.
 2328  */
 2329 
 2330 static void
 2331 ieee80211_node_table_init(struct ieee80211com *ic,
 2332         struct ieee80211_node_table *nt,
 2333         const char *name, int inact, int keyixmax,
 2334         void (*timeout)(struct ieee80211_node_table *))
 2335 {
 2336 
 2337         IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
 2338                 "%s %s table, inact %u\n", __func__, name, inact);
 2339 
 2340         nt->nt_ic = ic;
 2341         /* XXX need unit */
 2342         IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
 2343         IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
 2344         TAILQ_INIT(&nt->nt_node);
 2345         nt->nt_name = name;
 2346         nt->nt_scangen = 1;
 2347         nt->nt_inact_init = inact;
 2348         nt->nt_timeout = timeout;
 2349         nt->nt_keyixmax = keyixmax;
 2350         if (nt->nt_keyixmax > 0) {
 2351                 MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
 2352                         keyixmax * sizeof(struct ieee80211_node *),
 2353                         M_80211_NODE, M_NOWAIT | M_ZERO);
 2354                 if (nt->nt_keyixmap == NULL)
 2355                         if_printf(ic->ic_ifp,
 2356                             "Cannot allocate key index map with %u entries\n",
 2357                             keyixmax);
 2358         } else
 2359                 nt->nt_keyixmap = NULL;
 2360 }
 2361 
 2362 void
 2363 ieee80211_node_table_reset(struct ieee80211_node_table *nt)
 2364 {
 2365 
 2366         IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
 2367                 "%s %s table\n", __func__, nt->nt_name);
 2368 
 2369         IEEE80211_NODE_LOCK(nt);
 2370         nt->nt_inact_timer = 0;
 2371         ieee80211_free_allnodes_locked(nt);
 2372         IEEE80211_NODE_UNLOCK(nt);
 2373 }
 2374 
 2375 static void
 2376 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
 2377 {
 2378 
 2379         IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
 2380                 "%s %s table\n", __func__, nt->nt_name);
 2381 
 2382         IEEE80211_NODE_LOCK(nt);
 2383         ieee80211_free_allnodes_locked(nt);
 2384         if (nt->nt_keyixmap != NULL) {
 2385                 /* XXX verify all entries are NULL */
 2386                 int i;
 2387                 for (i = 0; i < nt->nt_keyixmax; i++)
 2388                         if (nt->nt_keyixmap[i] != NULL)
 2389                                 printf("%s: %s[%u] still active\n", __func__,
 2390                                         nt->nt_name, i);
 2391                 FREE(nt->nt_keyixmap, M_80211_NODE);
 2392                 nt->nt_keyixmap = NULL;
 2393         }
 2394         IEEE80211_SCAN_LOCK_DESTROY(nt);
 2395         IEEE80211_NODE_LOCK_DESTROY(nt);
 2396 }

Cache object: 0c76da32d778b77fb692ead4ce83a236


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