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_scan_sta.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   24  */
   25 
   26 #include <sys/cdefs.h>
   27 __FBSDID("$FreeBSD: releng/10.0/sys/net80211/ieee80211_scan_sta.c 254315 2013-08-14 04:24:25Z rpaulo $");
   28 
   29 /*
   30  * IEEE 802.11 station scanning support.
   31  */
   32 #include "opt_wlan.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/kernel.h>
   37 #include <sys/module.h>
   38 
   39 #include <sys/socket.h>
   40 
   41 #include <net/if.h>
   42 #include <net/if_media.h>
   43 #include <net/ethernet.h>
   44 
   45 #include <net80211/ieee80211_var.h>
   46 #include <net80211/ieee80211_input.h>
   47 #include <net80211/ieee80211_regdomain.h>
   48 #ifdef IEEE80211_SUPPORT_TDMA
   49 #include <net80211/ieee80211_tdma.h>
   50 #endif
   51 #ifdef IEEE80211_SUPPORT_MESH
   52 #include <net80211/ieee80211_mesh.h>
   53 #endif
   54 #include <net80211/ieee80211_ratectl.h>
   55 
   56 #include <net/bpf.h>
   57 
   58 /*
   59  * Parameters for managing cache entries:
   60  *
   61  * o a station with STA_FAILS_MAX failures is not considered
   62  *   when picking a candidate
   63  * o a station that hasn't had an update in STA_PURGE_SCANS
   64  *   (background) scans is discarded
   65  * o after STA_FAILS_AGE seconds we clear the failure count
   66  */
   67 #define STA_FAILS_MAX   2               /* assoc failures before ignored */
   68 #define STA_FAILS_AGE   (2*60)          /* time before clearing fails (secs) */
   69 #define STA_PURGE_SCANS 2               /* age for purging entries (scans) */
   70 
   71 /* XXX tunable */
   72 #define STA_RSSI_MIN    8               /* min acceptable rssi */
   73 #define STA_RSSI_MAX    40              /* max rssi for comparison */
   74 
   75 struct sta_entry {
   76         struct ieee80211_scan_entry base;
   77         TAILQ_ENTRY(sta_entry) se_list;
   78         LIST_ENTRY(sta_entry) se_hash;
   79         uint8_t         se_fails;               /* failure to associate count */
   80         uint8_t         se_seen;                /* seen during current scan */
   81         uint8_t         se_notseen;             /* not seen in previous scans */
   82         uint8_t         se_flags;
   83 #define STA_DEMOTE11B   0x01                    /* match w/ demoted 11b chan */
   84         uint32_t        se_avgrssi;             /* LPF rssi state */
   85         unsigned long   se_lastupdate;          /* time of last update */
   86         unsigned long   se_lastfail;            /* time of last failure */
   87         unsigned long   se_lastassoc;           /* time of last association */
   88         u_int           se_scangen;             /* iterator scan gen# */
   89         u_int           se_countrygen;          /* gen# of last cc notify */
   90 };
   91 
   92 #define STA_HASHSIZE    32
   93 /* simple hash is enough for variation of macaddr */
   94 #define STA_HASH(addr)  \
   95         (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
   96 
   97 #define MAX_IEEE_CHAN   256                     /* max acceptable IEEE chan # */
   98 CTASSERT(MAX_IEEE_CHAN >= 256);
   99 
  100 struct sta_table {
  101         ieee80211_scan_table_lock_t st_lock;    /* on scan table */
  102         TAILQ_HEAD(, sta_entry) st_entry;       /* all entries */
  103         LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
  104         struct mtx      st_scanlock;            /* on st_scaniter */
  105         u_int           st_scaniter;            /* gen# for iterator */
  106         u_int           st_scangen;             /* scan generation # */
  107         int             st_newscan;
  108         /* ap-related state */
  109         int             st_maxrssi[MAX_IEEE_CHAN];
  110 };
  111 
  112 static void sta_flush_table(struct sta_table *);
  113 /*
  114  * match_bss returns a bitmask describing if an entry is suitable
  115  * for use.  If non-zero the entry was deemed not suitable and it's
  116  * contents explains why.  The following flags are or'd to to this
  117  * mask and can be used to figure out why the entry was rejected.
  118  */
  119 #define MATCH_CHANNEL           0x00001 /* channel mismatch */
  120 #define MATCH_CAPINFO           0x00002 /* capabilities mismatch, e.g. no ess */
  121 #define MATCH_PRIVACY           0x00004 /* privacy mismatch */
  122 #define MATCH_RATE              0x00008 /* rate set mismatch */
  123 #define MATCH_SSID              0x00010 /* ssid mismatch */
  124 #define MATCH_BSSID             0x00020 /* bssid mismatch */
  125 #define MATCH_FAILS             0x00040 /* too many failed auth attempts */
  126 #define MATCH_NOTSEEN           0x00080 /* not seen in recent scans */
  127 #define MATCH_RSSI              0x00100 /* rssi deemed too low to use */
  128 #define MATCH_CC                0x00200 /* country code mismatch */
  129 #define MATCH_TDMA_NOIE         0x00400 /* no TDMA ie */
  130 #define MATCH_TDMA_NOTMASTER    0x00800 /* not TDMA master */
  131 #define MATCH_TDMA_NOSLOT       0x01000 /* all TDMA slots occupied */
  132 #define MATCH_TDMA_LOCAL        0x02000 /* local address */
  133 #define MATCH_TDMA_VERSION      0x04000 /* protocol version mismatch */
  134 #define MATCH_MESH_NOID         0x10000 /* no MESHID ie */
  135 #define MATCH_MESHID            0x20000 /* meshid mismatch */
  136 static int match_bss(struct ieee80211vap *,
  137         const struct ieee80211_scan_state *, struct sta_entry *, int);
  138 static void adhoc_age(struct ieee80211_scan_state *);
  139 
  140 static __inline int
  141 isocmp(const uint8_t cc1[], const uint8_t cc2[])
  142 {
  143      return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
  144 }
  145 
  146 /* number of references from net80211 layer */
  147 static  int nrefs = 0;
  148 /*
  149  * Module glue.
  150  */
  151 IEEE80211_SCANNER_MODULE(sta, 1);
  152 
  153 /*
  154  * Attach prior to any scanning work.
  155  */
  156 static int
  157 sta_attach(struct ieee80211_scan_state *ss)
  158 {
  159         struct sta_table *st;
  160 
  161         st = (struct sta_table *) malloc(sizeof(struct sta_table),
  162                 M_80211_SCAN, M_NOWAIT | M_ZERO);
  163         if (st == NULL)
  164                 return 0;
  165         IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable");
  166         mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF);
  167         TAILQ_INIT(&st->st_entry);
  168         ss->ss_priv = st;
  169         nrefs++;                        /* NB: we assume caller locking */
  170         return 1;
  171 }
  172 
  173 /*
  174  * Cleanup any private state.
  175  */
  176 static int
  177 sta_detach(struct ieee80211_scan_state *ss)
  178 {
  179         struct sta_table *st = ss->ss_priv;
  180 
  181         if (st != NULL) {
  182                 sta_flush_table(st);
  183                 IEEE80211_SCAN_TABLE_LOCK_DESTROY(st);
  184                 mtx_destroy(&st->st_scanlock);
  185                 free(st, M_80211_SCAN);
  186                 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
  187                 nrefs--;                /* NB: we assume caller locking */
  188         }
  189         return 1;
  190 }
  191 
  192 /*
  193  * Flush all per-scan state.
  194  */
  195 static int
  196 sta_flush(struct ieee80211_scan_state *ss)
  197 {
  198         struct sta_table *st = ss->ss_priv;
  199 
  200         IEEE80211_SCAN_TABLE_LOCK(st);
  201         sta_flush_table(st);
  202         IEEE80211_SCAN_TABLE_UNLOCK(st);
  203         ss->ss_last = 0;
  204         return 0;
  205 }
  206 
  207 /*
  208  * Flush all entries in the scan cache.
  209  */
  210 static void
  211 sta_flush_table(struct sta_table *st)
  212 {
  213         struct sta_entry *se, *next;
  214 
  215         TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
  216                 TAILQ_REMOVE(&st->st_entry, se, se_list);
  217                 LIST_REMOVE(se, se_hash);
  218                 ieee80211_ies_cleanup(&se->base.se_ies);
  219                 free(se, M_80211_SCAN);
  220         }
  221         memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
  222 }
  223 
  224 /*
  225  * Process a beacon or probe response frame; create an
  226  * entry in the scan cache or update any previous entry.
  227  */
  228 static int
  229 sta_add(struct ieee80211_scan_state *ss, 
  230         const struct ieee80211_scanparams *sp,
  231         const struct ieee80211_frame *wh,
  232         int subtype, int rssi, int noise)
  233 {
  234 #define ISPROBE(_st)    ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
  235 #define PICK1ST(_ss) \
  236         ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
  237         IEEE80211_SCAN_PICK1ST)
  238         struct sta_table *st = ss->ss_priv;
  239         const uint8_t *macaddr = wh->i_addr2;
  240         struct ieee80211vap *vap = ss->ss_vap;
  241         struct ieee80211com *ic = vap->iv_ic;
  242         struct ieee80211_channel *c;
  243         struct sta_entry *se;
  244         struct ieee80211_scan_entry *ise;
  245         int hash;
  246 
  247         hash = STA_HASH(macaddr);
  248 
  249         IEEE80211_SCAN_TABLE_LOCK(st);
  250         LIST_FOREACH(se, &st->st_hash[hash], se_hash)
  251                 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
  252                         goto found;
  253         se = (struct sta_entry *) malloc(sizeof(struct sta_entry),
  254                 M_80211_SCAN, M_NOWAIT | M_ZERO);
  255         if (se == NULL) {
  256                 IEEE80211_SCAN_TABLE_UNLOCK(st);
  257                 return 0;
  258         }
  259         se->se_scangen = st->st_scaniter-1;
  260         se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
  261         IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
  262         TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
  263         LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
  264 found:
  265         ise = &se->base;
  266         /* XXX ap beaconing multiple ssid w/ same bssid */
  267         if (sp->ssid[1] != 0 &&
  268             (ISPROBE(subtype) || ise->se_ssid[1] == 0))
  269                 memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
  270         KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
  271                 ("rate set too large: %u", sp->rates[1]));
  272         memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
  273         if (sp->xrates != NULL) {
  274                 /* XXX validate xrates[1] */
  275                 KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
  276                         ("xrate set too large: %u", sp->xrates[1]));
  277                 memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
  278         } else
  279                 ise->se_xrates[1] = 0;
  280         IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
  281         if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
  282                 /*
  283                  * Record rssi data using extended precision LPF filter.
  284                  *
  285                  * NB: use only on-channel data to insure we get a good
  286                  *     estimate of the signal we'll see when associated.
  287                  */
  288                 IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
  289                 ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
  290                 ise->se_noise = noise;
  291         }
  292         memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
  293         ise->se_intval = sp->bintval;
  294         ise->se_capinfo = sp->capinfo;
  295 #ifdef IEEE80211_SUPPORT_MESH
  296         if (sp->meshid != NULL && sp->meshid[1] != 0)
  297                 memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]);
  298 #endif
  299         /*
  300          * Beware of overriding se_chan for frames seen
  301          * off-channel; this can cause us to attempt an
  302          * association on the wrong channel.
  303          */
  304         if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
  305                 /*
  306                  * Off-channel, locate the home/bss channel for the sta
  307                  * using the value broadcast in the DSPARMS ie.  We know
  308                  * sp->chan has this value because it's used to calculate
  309                  * IEEE80211_BPARSE_OFFCHAN.
  310                  */
  311                 c = ieee80211_find_channel_byieee(ic, sp->chan,
  312                     ic->ic_curchan->ic_flags);
  313                 if (c != NULL) {
  314                         ise->se_chan = c;
  315                 } else if (ise->se_chan == NULL) {
  316                         /* should not happen, pick something */
  317                         ise->se_chan = ic->ic_curchan;
  318                 }
  319         } else
  320                 ise->se_chan = ic->ic_curchan;
  321         if (IEEE80211_IS_CHAN_HT(ise->se_chan) && sp->htcap == NULL) {
  322                 /* Demote legacy networks to a non-HT channel. */
  323                 c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
  324                     ise->se_chan->ic_flags & ~IEEE80211_CHAN_HT);
  325                 KASSERT(c != NULL,
  326                     ("no legacy channel %u", ise->se_chan->ic_ieee));
  327                 ise->se_chan = c;
  328         }
  329         ise->se_fhdwell = sp->fhdwell;
  330         ise->se_fhindex = sp->fhindex;
  331         ise->se_erp = sp->erp;
  332         ise->se_timoff = sp->timoff;
  333         if (sp->tim != NULL) {
  334                 const struct ieee80211_tim_ie *tim =
  335                     (const struct ieee80211_tim_ie *) sp->tim;
  336                 ise->se_dtimperiod = tim->tim_period;
  337         }
  338         if (sp->country != NULL) {
  339                 const struct ieee80211_country_ie *cie =
  340                     (const struct ieee80211_country_ie *) sp->country;
  341                 /*
  342                  * If 11d is enabled and we're attempting to join a bss
  343                  * that advertises it's country code then compare our
  344                  * current settings to what we fetched from the country ie.
  345                  * If our country code is unspecified or different then
  346                  * dispatch an event to user space that identifies the
  347                  * country code so our regdomain config can be changed.
  348                  */
  349                 /* XXX only for STA mode? */
  350                 if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
  351                     (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
  352                     (ic->ic_regdomain.country == CTRY_DEFAULT ||
  353                      !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
  354                         /* only issue one notify event per scan */
  355                         if (se->se_countrygen != st->st_scangen) {
  356                                 ieee80211_notify_country(vap, ise->se_bssid,
  357                                     cie->cc);
  358                                 se->se_countrygen = st->st_scangen;
  359                         }
  360                 }
  361                 ise->se_cc[0] = cie->cc[0];
  362                 ise->se_cc[1] = cie->cc[1];
  363         }
  364         /* NB: no need to setup ie ptrs; they are not (currently) used */
  365         (void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
  366 
  367         /* clear failure count after STA_FAIL_AGE passes */
  368         if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
  369                 se->se_fails = 0;
  370                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
  371                     "%s: fails %u", __func__, se->se_fails);
  372         }
  373 
  374         se->se_lastupdate = ticks;              /* update time */
  375         se->se_seen = 1;
  376         se->se_notseen = 0;
  377 
  378         KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
  379         if (rssi > st->st_maxrssi[sp->bchan])
  380                 st->st_maxrssi[sp->bchan] = rssi;
  381 
  382         IEEE80211_SCAN_TABLE_UNLOCK(st);
  383 
  384         /*
  385          * If looking for a quick choice and nothing's
  386          * been found check here.
  387          */
  388         if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
  389                 ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
  390 
  391         return 1;
  392 #undef PICK1ST
  393 #undef ISPROBE
  394 }
  395 
  396 /*
  397  * Check if a channel is excluded by user request.
  398  */
  399 static int
  400 isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
  401 {
  402         return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
  403             (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
  404              c->ic_freq != vap->iv_des_chan->ic_freq));
  405 }
  406 
  407 static struct ieee80211_channel *
  408 find11gchannel(struct ieee80211com *ic, int i, int freq)
  409 {
  410         struct ieee80211_channel *c;
  411         int j;
  412 
  413         /*
  414          * The normal ordering in the channel list is b channel
  415          * immediately followed by g so optimize the search for
  416          * this.  We'll still do a full search just in case.
  417          */
  418         for (j = i+1; j < ic->ic_nchans; j++) {
  419                 c = &ic->ic_channels[j];
  420                 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
  421                         return c;
  422         }
  423         for (j = 0; j < i; j++) {
  424                 c = &ic->ic_channels[j];
  425                 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
  426                         return c;
  427         }
  428         return NULL;
  429 }
  430 
  431 static const u_int chanflags[IEEE80211_MODE_MAX] = {
  432         [IEEE80211_MODE_AUTO]     = IEEE80211_CHAN_B,
  433         [IEEE80211_MODE_11A]      = IEEE80211_CHAN_A,
  434         [IEEE80211_MODE_11B]      = IEEE80211_CHAN_B,
  435         [IEEE80211_MODE_11G]      = IEEE80211_CHAN_G,
  436         [IEEE80211_MODE_FH]       = IEEE80211_CHAN_FHSS,
  437         /* check base channel */
  438         [IEEE80211_MODE_TURBO_A]  = IEEE80211_CHAN_A,
  439         [IEEE80211_MODE_TURBO_G]  = IEEE80211_CHAN_G,
  440         [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
  441         [IEEE80211_MODE_HALF]     = IEEE80211_CHAN_HALF,
  442         [IEEE80211_MODE_QUARTER]  = IEEE80211_CHAN_QUARTER,
  443         /* check legacy */
  444         [IEEE80211_MODE_11NA]     = IEEE80211_CHAN_A,
  445         [IEEE80211_MODE_11NG]     = IEEE80211_CHAN_G,
  446 };
  447 
  448 static void
  449 add_channels(struct ieee80211vap *vap,
  450         struct ieee80211_scan_state *ss,
  451         enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
  452 {
  453         struct ieee80211com *ic = vap->iv_ic;
  454         struct ieee80211_channel *c, *cg;
  455         u_int modeflags;
  456         int i;
  457 
  458         KASSERT(mode < nitems(chanflags), ("Unexpected mode %u", mode));
  459         modeflags = chanflags[mode];
  460         for (i = 0; i < nfreq; i++) {
  461                 if (ss->ss_last >= IEEE80211_SCAN_MAX)
  462                         break;
  463 
  464                 c = ieee80211_find_channel(ic, freq[i], modeflags);
  465                 if (c == NULL || isexcluded(vap, c))
  466                         continue;
  467                 if (mode == IEEE80211_MODE_AUTO) {
  468                         /*
  469                          * XXX special-case 11b/g channels so we select
  470                          *     the g channel if both are present.
  471                          */
  472                         if (IEEE80211_IS_CHAN_B(c) &&
  473                             (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
  474                                 c = cg;
  475                 }
  476                 ss->ss_chans[ss->ss_last++] = c;
  477         }
  478 }
  479 
  480 struct scanlist {
  481         uint16_t        mode;
  482         uint16_t        count;
  483         const uint16_t  *list;
  484 };
  485 
  486 static int
  487 checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
  488 {
  489         int i;
  490 
  491         for (; scan->list != NULL; scan++) {
  492                 for (i = 0; i < scan->count; i++)
  493                         if (scan->list[i] == c->ic_freq) 
  494                                 return 1;
  495         }
  496         return 0;
  497 }
  498 
  499 static int
  500 onscanlist(const struct ieee80211_scan_state *ss,
  501         const struct ieee80211_channel *c)
  502 {
  503         int i;
  504 
  505         for (i = 0; i < ss->ss_last; i++)
  506                 if (ss->ss_chans[i] == c)
  507                         return 1;
  508         return 0;
  509 }
  510 
  511 static void
  512 sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
  513         const struct scanlist table[])
  514 {
  515         struct ieee80211com *ic = vap->iv_ic;
  516         struct ieee80211_channel *c;
  517         int i;
  518 
  519         for (i = 0; i < ic->ic_nchans; i++) {
  520                 if (ss->ss_last >= IEEE80211_SCAN_MAX)
  521                         break;
  522 
  523                 c = &ic->ic_channels[i];
  524                 /*
  525                  * Ignore dynamic turbo channels; we scan them
  526                  * in normal mode (i.e. not boosted).  Likewise
  527                  * for HT channels, they get scanned using
  528                  * legacy rates.
  529                  */
  530                 if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
  531                         continue;
  532 
  533                 /*
  534                  * If a desired mode was specified, scan only 
  535                  * channels that satisfy that constraint.
  536                  */
  537                 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
  538                     vap->iv_des_mode != ieee80211_chan2mode(c))
  539                         continue;
  540 
  541                 /*
  542                  * Skip channels excluded by user request.
  543                  */
  544                 if (isexcluded(vap, c))
  545                         continue;
  546 
  547                 /*
  548                  * Add the channel unless it is listed in the
  549                  * fixed scan order tables.  This insures we
  550                  * don't sweep back in channels we filtered out
  551                  * above.
  552                  */
  553                 if (checktable(table, c))
  554                         continue;
  555 
  556                 /* Add channel to scanning list. */
  557                 ss->ss_chans[ss->ss_last++] = c;
  558         }
  559         /*
  560          * Explicitly add any desired channel if:
  561          * - not already on the scan list
  562          * - allowed by any desired mode constraint
  563          * - there is space in the scan list
  564          * This allows the channel to be used when the filtering
  565          * mechanisms would otherwise elide it (e.g HT, turbo).
  566          */
  567         c = vap->iv_des_chan;
  568         if (c != IEEE80211_CHAN_ANYC &&
  569             !onscanlist(ss, c) &&
  570             (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
  571              vap->iv_des_mode == ieee80211_chan2mode(c)) &&
  572             ss->ss_last < IEEE80211_SCAN_MAX)
  573                 ss->ss_chans[ss->ss_last++] = c;
  574 }
  575 
  576 static void
  577 makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
  578         const struct scanlist table[])
  579 {
  580         const struct scanlist *scan;
  581         enum ieee80211_phymode mode;
  582 
  583         ss->ss_last = 0;
  584         /*
  585          * Use the table of ordered channels to construct the list
  586          * of channels for scanning.  Any channels in the ordered
  587          * list not in the master list will be discarded.
  588          */
  589         for (scan = table; scan->list != NULL; scan++) {
  590                 mode = scan->mode;
  591                 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
  592                         /*
  593                          * If a desired mode was specified, scan only 
  594                          * channels that satisfy that constraint.
  595                          */
  596                         if (vap->iv_des_mode != mode) {
  597                                 /*
  598                                  * The scan table marks 2.4Ghz channels as b
  599                                  * so if the desired mode is 11g, then use
  600                                  * the 11b channel list but upgrade the mode.
  601                                  */
  602                                 if (vap->iv_des_mode != IEEE80211_MODE_11G ||
  603                                     mode != IEEE80211_MODE_11B)
  604                                         continue;
  605                                 mode = IEEE80211_MODE_11G;      /* upgrade */
  606                         }
  607                 } else {
  608                         /*
  609                          * This lets add_channels upgrade an 11b channel
  610                          * to 11g if available.
  611                          */
  612                         if (mode == IEEE80211_MODE_11B)
  613                                 mode = IEEE80211_MODE_AUTO;
  614                 }
  615 #ifdef IEEE80211_F_XR
  616                 /* XR does not operate on turbo channels */
  617                 if ((vap->iv_flags & IEEE80211_F_XR) &&
  618                     (mode == IEEE80211_MODE_TURBO_A ||
  619                      mode == IEEE80211_MODE_TURBO_G ||
  620                      mode == IEEE80211_MODE_STURBO_A))
  621                         continue;
  622 #endif
  623                 /*
  624                  * Add the list of the channels; any that are not
  625                  * in the master channel list will be discarded.
  626                  */
  627                 add_channels(vap, ss, mode, scan->list, scan->count);
  628         }
  629 
  630         /*
  631          * Add the channels from the ic that are not present
  632          * in the table.
  633          */
  634         sweepchannels(ss, vap, table);
  635 }
  636 
  637 static const uint16_t rcl1[] =          /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
  638 { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
  639 static const uint16_t rcl2[] =          /* 4 MKK channels: 34, 38, 42, 46 */
  640 { 5170, 5190, 5210, 5230 };
  641 static const uint16_t rcl3[] =          /* 2.4Ghz ch: 1,6,11,7,13 */
  642 { 2412, 2437, 2462, 2442, 2472 };
  643 static const uint16_t rcl4[] =          /* 5 FCC channel: 149, 153, 161, 165 */
  644 { 5745, 5765, 5785, 5805, 5825 };
  645 static const uint16_t rcl7[] =          /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
  646 { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
  647 static const uint16_t rcl8[] =          /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
  648 { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
  649 static const uint16_t rcl9[] =          /* 2.4Ghz ch: 14 */
  650 { 2484 };
  651 static const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */
  652 { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
  653 static const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */
  654 { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
  655 #ifdef ATH_TURBO_SCAN
  656 static const uint16_t rcl5[] =          /* 3 static turbo channels */
  657 { 5210, 5250, 5290 };
  658 static const uint16_t rcl6[] =          /* 2 static turbo channels */
  659 { 5760, 5800 };
  660 static const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */
  661 { 5540, 5580, 5620, 5660 };
  662 static const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */
  663 { 2437 };
  664 static const uint16_t rcl13[] = /* dynamic Turbo channels */
  665 { 5200, 5240, 5280, 5765, 5805 };
  666 #endif /* ATH_TURBO_SCAN */
  667 
  668 #define X(a)    .count = sizeof(a)/sizeof(a[0]), .list = a
  669 
  670 static const struct scanlist staScanTable[] = {
  671         { IEEE80211_MODE_11B,           X(rcl3) },
  672         { IEEE80211_MODE_11A,           X(rcl1) },
  673         { IEEE80211_MODE_11A,           X(rcl2) },
  674         { IEEE80211_MODE_11B,           X(rcl8) },
  675         { IEEE80211_MODE_11B,           X(rcl9) },
  676         { IEEE80211_MODE_11A,           X(rcl4) },
  677 #ifdef ATH_TURBO_SCAN
  678         { IEEE80211_MODE_STURBO_A,      X(rcl5) },
  679         { IEEE80211_MODE_STURBO_A,      X(rcl6) },
  680         { IEEE80211_MODE_TURBO_A,       X(rcl6x) },
  681         { IEEE80211_MODE_TURBO_A,       X(rcl13) },
  682 #endif /* ATH_TURBO_SCAN */
  683         { IEEE80211_MODE_11A,           X(rcl7) },
  684         { IEEE80211_MODE_11B,           X(rcl10) },
  685         { IEEE80211_MODE_11A,           X(rcl11) },
  686 #ifdef ATH_TURBO_SCAN
  687         { IEEE80211_MODE_TURBO_G,       X(rcl12) },
  688 #endif /* ATH_TURBO_SCAN */
  689         { .list = NULL }
  690 };
  691 
  692 /*
  693  * Start a station-mode scan by populating the channel list.
  694  */
  695 static int
  696 sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
  697 {
  698         struct sta_table *st = ss->ss_priv;
  699 
  700         makescanlist(ss, vap, staScanTable);
  701 
  702         if (ss->ss_mindwell == 0)
  703                 ss->ss_mindwell = msecs_to_ticks(20);   /* 20ms */
  704         if (ss->ss_maxdwell == 0)
  705                 ss->ss_maxdwell = msecs_to_ticks(200);  /* 200ms */
  706 
  707         st->st_scangen++;
  708         st->st_newscan = 1;
  709 
  710         return 0;
  711 }
  712 
  713 /*
  714  * Restart a scan, typically a bg scan but can
  715  * also be a fg scan that came up empty.
  716  */
  717 static int
  718 sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
  719 {
  720         struct sta_table *st = ss->ss_priv;
  721 
  722         st->st_newscan = 1;
  723         return 0;
  724 }
  725 
  726 /*
  727  * Cancel an ongoing scan.
  728  */
  729 static int
  730 sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
  731 {
  732         return 0;
  733 }
  734 
  735 /* unalligned little endian access */     
  736 #define LE_READ_2(p)                                    \
  737         ((uint16_t)                                     \
  738          ((((const uint8_t *)(p))[0]      ) |           \
  739           (((const uint8_t *)(p))[1] <<  8)))
  740  
  741 /*
  742  * Demote any supplied 11g channel to 11b.  There should
  743  * always be an 11b channel but we check anyway...
  744  */
  745 static struct ieee80211_channel *
  746 demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
  747 {
  748         struct ieee80211_channel *c;
  749 
  750         if (IEEE80211_IS_CHAN_ANYG(chan) &&
  751             vap->iv_des_mode == IEEE80211_MODE_AUTO) {
  752                 c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
  753                     (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
  754                     IEEE80211_CHAN_B);
  755                 if (c != NULL)
  756                         chan = c;
  757         }
  758         return chan;
  759 }
  760 
  761 static int
  762 maxrate(const struct ieee80211_scan_entry *se)
  763 {
  764         const struct ieee80211_ie_htcap *htcap =
  765             (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
  766         int rmax, r, i, txstream;
  767         uint16_t caps;
  768         uint8_t txparams;
  769 
  770         rmax = 0;
  771         if (htcap != NULL) {
  772                 /*
  773                  * HT station; inspect supported MCS and then adjust
  774                  * rate by channel width.
  775                  */
  776                 txparams = htcap->hc_mcsset[12];
  777                 if (txparams & 0x3) {
  778                         /*
  779                          * TX MCS parameters defined and not equal to RX,
  780                          * extract the number of spartial streams and
  781                          * map it to the highest MCS rate.
  782                          */
  783                         txstream = ((txparams & 0xc) >> 2) + 1;
  784                         i = txstream * 8 - 1;
  785                 } else
  786                         for (i = 31; i >= 0 && isclr(htcap->hc_mcsset, i); i--);
  787                 if (i >= 0) {
  788                         caps = LE_READ_2(&htcap->hc_cap);
  789                         if ((caps & IEEE80211_HTCAP_CHWIDTH40) &&
  790                             (caps & IEEE80211_HTCAP_SHORTGI40))
  791                                 rmax = ieee80211_htrates[i].ht40_rate_400ns;
  792                         else if (caps & IEEE80211_HTCAP_CHWIDTH40)
  793                                 rmax = ieee80211_htrates[i].ht40_rate_800ns;
  794                         else if (caps & IEEE80211_HTCAP_SHORTGI20)
  795                                 rmax = ieee80211_htrates[i].ht20_rate_400ns;
  796                         else
  797                                 rmax = ieee80211_htrates[i].ht20_rate_800ns;
  798                 }
  799         }
  800         for (i = 0; i < se->se_rates[1]; i++) {
  801                 r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
  802                 if (r > rmax)
  803                         rmax = r;
  804         }
  805         for (i = 0; i < se->se_xrates[1]; i++) {
  806                 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
  807                 if (r > rmax)
  808                         rmax = r;
  809         }
  810         return rmax;
  811 }
  812 
  813 /*
  814  * Compare the capabilities of two entries and decide which is
  815  * more desirable (return >0 if a is considered better).  Note
  816  * that we assume compatibility/usability has already been checked
  817  * so we don't need to (e.g. validate whether privacy is supported).
  818  * Used to select the best scan candidate for association in a BSS.
  819  */
  820 static int
  821 sta_compare(const struct sta_entry *a, const struct sta_entry *b)
  822 {
  823 #define PREFER(_a,_b,_what) do {                        \
  824         if (((_a) ^ (_b)) & (_what))                    \
  825                 return ((_a) & (_what)) ? 1 : -1;       \
  826 } while (0)
  827         int maxa, maxb;
  828         int8_t rssia, rssib;
  829         int weight;
  830 
  831         /* privacy support */
  832         PREFER(a->base.se_capinfo, b->base.se_capinfo,
  833                 IEEE80211_CAPINFO_PRIVACY);
  834 
  835         /* compare count of previous failures */
  836         weight = b->se_fails - a->se_fails;
  837         if (abs(weight) > 1)
  838                 return weight;
  839 
  840         /*
  841          * Compare rssi.  If the two are considered equivalent
  842          * then fallback to other criteria.  We threshold the
  843          * comparisons to avoid selecting an ap purely by rssi
  844          * when both values may be good but one ap is otherwise
  845          * more desirable (e.g. an 11b-only ap with stronger
  846          * signal than an 11g ap).
  847          */
  848         rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
  849         rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
  850         if (abs(rssib - rssia) < 5) {
  851                 /* best/max rate preferred if signal level close enough XXX */
  852                 maxa = maxrate(&a->base);
  853                 maxb = maxrate(&b->base);
  854                 if (maxa != maxb)
  855                         return maxa - maxb;
  856                 /* XXX use freq for channel preference */
  857                 /* for now just prefer 5Ghz band to all other bands */
  858                 PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
  859                        IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
  860         }
  861         /* all things being equal, use signal level */
  862         return a->base.se_rssi - b->base.se_rssi;
  863 #undef PREFER
  864 }
  865 
  866 /*
  867  * Check rate set suitability and return the best supported rate.
  868  * XXX inspect MCS for HT
  869  */
  870 static int
  871 check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
  872     const struct ieee80211_scan_entry *se)
  873 {
  874 #define RV(v)   ((v) & IEEE80211_RATE_VAL)
  875         const struct ieee80211_rateset *srs;
  876         int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
  877         const uint8_t *rs;
  878 
  879         okrate = badrate = 0;
  880 
  881         srs = ieee80211_get_suprates(vap->iv_ic, chan);
  882         nrs = se->se_rates[1];
  883         rs = se->se_rates+2;
  884         /* XXX MCS */
  885         ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
  886         fixedrate = IEEE80211_FIXED_RATE_NONE;
  887 again:
  888         for (i = 0; i < nrs; i++) {
  889                 r = RV(rs[i]);
  890                 badrate = r;
  891                 /*
  892                  * Check any fixed rate is included. 
  893                  */
  894                 if (r == ucastrate)
  895                         fixedrate = r;
  896                 /*
  897                  * Check against our supported rates.
  898                  */
  899                 for (j = 0; j < srs->rs_nrates; j++)
  900                         if (r == RV(srs->rs_rates[j])) {
  901                                 if (r > okrate)         /* NB: track max */
  902                                         okrate = r;
  903                                 break;
  904                         }
  905 
  906                 if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
  907                         /*
  908                          * Don't try joining a BSS, if we don't support
  909                          * one of its basic rates.
  910                          */
  911                         okrate = 0;
  912                         goto back;
  913                 }
  914         }
  915         if (rs == se->se_rates+2) {
  916                 /* scan xrates too; sort of an algol68-style for loop */
  917                 nrs = se->se_xrates[1];
  918                 rs = se->se_xrates+2;
  919                 goto again;
  920         }
  921 
  922 back:
  923         if (okrate == 0 || ucastrate != fixedrate)
  924                 return badrate | IEEE80211_RATE_BASIC;
  925         else
  926                 return RV(okrate);
  927 #undef RV
  928 }
  929 
  930 static __inline int
  931 match_id(const uint8_t *ie, const uint8_t *val, int len)
  932 {
  933         return (ie[1] == len && memcmp(ie+2, val, len) == 0);
  934 }
  935 
  936 static int
  937 match_ssid(const uint8_t *ie,
  938         int nssid, const struct ieee80211_scan_ssid ssids[])
  939 {
  940         int i;
  941 
  942         for (i = 0; i < nssid; i++) {
  943                 if (match_id(ie, ssids[i].ssid, ssids[i].len))
  944                         return 1;
  945         }
  946         return 0;
  947 }
  948 
  949 #ifdef IEEE80211_SUPPORT_TDMA
  950 static int
  951 tdma_isfull(const struct ieee80211_tdma_param *tdma)
  952 {
  953         int slot, slotcnt;
  954 
  955         slotcnt = tdma->tdma_slotcnt;
  956         for (slot = slotcnt-1; slot >= 0; slot--)
  957                 if (isclr(tdma->tdma_inuse, slot))
  958                         return 0;
  959         return 1;
  960 }
  961 #endif /* IEEE80211_SUPPORT_TDMA */
  962 
  963 /*
  964  * Test a scan candidate for suitability/compatibility.
  965  */
  966 static int
  967 match_bss(struct ieee80211vap *vap,
  968         const struct ieee80211_scan_state *ss, struct sta_entry *se0,
  969         int debug)
  970 {
  971         struct ieee80211com *ic = vap->iv_ic;
  972         struct ieee80211_scan_entry *se = &se0->base;
  973         uint8_t rate;
  974         int fail;
  975 
  976         fail = 0;
  977         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
  978                 fail |= MATCH_CHANNEL;
  979         /*
  980          * NB: normally the desired mode is used to construct
  981          * the channel list, but it's possible for the scan
  982          * cache to include entries for stations outside this
  983          * list so we check the desired mode here to weed them
  984          * out.
  985          */
  986         if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
  987             (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
  988             chanflags[vap->iv_des_mode])
  989                 fail |= MATCH_CHANNEL;
  990         if (vap->iv_opmode == IEEE80211_M_IBSS) {
  991                 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  992                         fail |= MATCH_CAPINFO;
  993 #ifdef IEEE80211_SUPPORT_TDMA
  994         } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
  995                 /*
  996                  * Adhoc demo network setup shouldn't really be scanning
  997                  * but just in case skip stations operating in IBSS or
  998                  * BSS mode.
  999                  */
 1000                 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
 1001                         fail |= MATCH_CAPINFO;
 1002                 /*
 1003                  * TDMA operation cannot coexist with a normal 802.11 network;
 1004                  * skip if IBSS or ESS capabilities are marked and require
 1005                  * the beacon have a TDMA ie present.
 1006                  */
 1007                 if (vap->iv_caps & IEEE80211_C_TDMA) {
 1008                         const struct ieee80211_tdma_param *tdma =
 1009                             (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
 1010                         const struct ieee80211_tdma_state *ts = vap->iv_tdma;
 1011 
 1012                         if (tdma == NULL)
 1013                                 fail |= MATCH_TDMA_NOIE;
 1014                         else if (tdma->tdma_version != ts->tdma_version)
 1015                                 fail |= MATCH_TDMA_VERSION;
 1016                         else if (tdma->tdma_slot != 0)
 1017                                 fail |= MATCH_TDMA_NOTMASTER;
 1018                         else if (tdma_isfull(tdma))
 1019                                 fail |= MATCH_TDMA_NOSLOT;
 1020 #if 0
 1021                         else if (ieee80211_local_address(se->se_macaddr))
 1022                                 fail |= MATCH_TDMA_LOCAL;
 1023 #endif
 1024                 }
 1025 #endif /* IEEE80211_SUPPORT_TDMA */
 1026 #ifdef IEEE80211_SUPPORT_MESH
 1027         } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
 1028                 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1029                 /*
 1030                  * Mesh nodes have IBSS & ESS bits in capinfo turned off
 1031                  * and two special ie's that must be present.
 1032                  */
 1033                 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
 1034                         fail |= MATCH_CAPINFO;
 1035                 else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID)
 1036                         fail |= MATCH_MESH_NOID;
 1037                 else if (ms->ms_idlen != 0 &&
 1038                     match_id(se->se_meshid, ms->ms_id, ms->ms_idlen))
 1039                         fail |= MATCH_MESHID;
 1040 #endif
 1041         } else {
 1042                 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
 1043                         fail |= MATCH_CAPINFO;
 1044                 /*
 1045                  * If 11d is enabled and we're attempting to join a bss
 1046                  * that advertises it's country code then compare our
 1047                  * current settings to what we fetched from the country ie.
 1048                  * If our country code is unspecified or different then do
 1049                  * not attempt to join the bss.  We should have already
 1050                  * dispatched an event to user space that identifies the
 1051                  * new country code so our regdomain config should match.
 1052                  */
 1053                 if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
 1054                     (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
 1055                     se->se_cc[0] != 0 &&
 1056                     (ic->ic_regdomain.country == CTRY_DEFAULT ||
 1057                      !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
 1058                         fail |= MATCH_CC;
 1059         }
 1060         if (vap->iv_flags & IEEE80211_F_PRIVACY) {
 1061                 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
 1062                         fail |= MATCH_PRIVACY;
 1063         } else {
 1064                 /* XXX does this mean privacy is supported or required? */
 1065                 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
 1066                         fail |= MATCH_PRIVACY;
 1067         }
 1068         se0->se_flags &= ~STA_DEMOTE11B;
 1069         rate = check_rate(vap, se->se_chan, se);
 1070         if (rate & IEEE80211_RATE_BASIC) {
 1071                 fail |= MATCH_RATE;
 1072                 /*
 1073                  * An 11b-only ap will give a rate mismatch if there is an
 1074                  * OFDM fixed tx rate for 11g.  Try downgrading the channel
 1075                  * in the scan list to 11b and retry the rate check.
 1076                  */
 1077                 if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
 1078                         rate = check_rate(vap, demote11b(vap, se->se_chan), se);
 1079                         if ((rate & IEEE80211_RATE_BASIC) == 0) {
 1080                                 fail &= ~MATCH_RATE;
 1081                                 se0->se_flags |= STA_DEMOTE11B;
 1082                         }
 1083                 }
 1084         } else if (rate < 2*24) {
 1085                 /*
 1086                  * This is an 11b-only ap.  Check the desired mode in
 1087                  * case that needs to be honored (mode 11g filters out
 1088                  * 11b-only ap's).  Otherwise force any 11g channel used
 1089                  * in scanning to be demoted.
 1090                  *
 1091                  * NB: we cheat a bit here by looking at the max rate;
 1092                  *     we could/should check the rates.
 1093                  */
 1094                 if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
 1095                       vap->iv_des_mode == IEEE80211_MODE_11B))
 1096                         fail |= MATCH_RATE;
 1097                 else
 1098                         se0->se_flags |= STA_DEMOTE11B;
 1099         }
 1100         if (ss->ss_nssid != 0 &&
 1101             !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
 1102                 fail |= MATCH_SSID;
 1103         if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
 1104             !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
 1105                 fail |= MATCH_BSSID;
 1106         if (se0->se_fails >= STA_FAILS_MAX)
 1107                 fail |= MATCH_FAILS;
 1108         if (se0->se_notseen >= STA_PURGE_SCANS)
 1109                 fail |= MATCH_NOTSEEN;
 1110         if (se->se_rssi < STA_RSSI_MIN)
 1111                 fail |= MATCH_RSSI;
 1112 #ifdef IEEE80211_DEBUG
 1113         if (ieee80211_msg(vap, debug)) {
 1114                 printf(" %c %s",
 1115                     fail & MATCH_FAILS ? '=' :
 1116                     fail & MATCH_NOTSEEN ? '^' :
 1117                     fail & MATCH_CC ? '$' :
 1118 #ifdef IEEE80211_SUPPORT_TDMA
 1119                     fail & MATCH_TDMA_NOIE ? '&' :
 1120                     fail & MATCH_TDMA_VERSION ? 'v' :
 1121                     fail & MATCH_TDMA_NOTMASTER ? 's' :
 1122                     fail & MATCH_TDMA_NOSLOT ? 'f' :
 1123                     fail & MATCH_TDMA_LOCAL ? 'l' :
 1124 #endif
 1125                     fail & MATCH_MESH_NOID ? 'm' :
 1126                     fail ? '-' : '+', ether_sprintf(se->se_macaddr));
 1127                 printf(" %s%c", ether_sprintf(se->se_bssid),
 1128                     fail & MATCH_BSSID ? '!' : ' ');
 1129                 printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
 1130                         fail & MATCH_CHANNEL ? '!' : ' ');
 1131                 printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
 1132                 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
 1133                     fail & MATCH_RATE ? '!' : ' ');
 1134                 printf(" %4s%c",
 1135                     (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
 1136                     (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "",
 1137                     fail & MATCH_CAPINFO ? '!' : ' ');
 1138                 printf(" %3s%c ",
 1139                     (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
 1140                     "wep" : "no",
 1141                     fail & MATCH_PRIVACY ? '!' : ' ');
 1142                 ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
 1143                 printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : "");
 1144         }
 1145 #endif
 1146         return fail;
 1147 }
 1148 
 1149 static void
 1150 sta_update_notseen(struct sta_table *st)
 1151 {
 1152         struct sta_entry *se;
 1153 
 1154         IEEE80211_SCAN_TABLE_LOCK(st);
 1155         TAILQ_FOREACH(se, &st->st_entry, se_list) {
 1156                 /*
 1157                  * If seen the reset and don't bump the count;
 1158                  * otherwise bump the ``not seen'' count.  Note
 1159                  * that this insures that stations for which we
 1160                  * see frames while not scanning but not during
 1161                  * this scan will not be penalized.
 1162                  */
 1163                 if (se->se_seen)
 1164                         se->se_seen = 0;
 1165                 else
 1166                         se->se_notseen++;
 1167         }
 1168         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1169 }
 1170 
 1171 static void
 1172 sta_dec_fails(struct sta_table *st)
 1173 {
 1174         struct sta_entry *se;
 1175 
 1176         IEEE80211_SCAN_TABLE_LOCK(st);
 1177         TAILQ_FOREACH(se, &st->st_entry, se_list)
 1178                 if (se->se_fails)
 1179                         se->se_fails--;
 1180         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1181 }
 1182 
 1183 static struct sta_entry *
 1184 select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
 1185 {
 1186         struct sta_table *st = ss->ss_priv;
 1187         struct sta_entry *se, *selbs = NULL;
 1188 
 1189         IEEE80211_DPRINTF(vap, debug, " %s\n",
 1190             "macaddr          bssid         chan  rssi  rate flag  wep  essid");
 1191         IEEE80211_SCAN_TABLE_LOCK(st);
 1192         TAILQ_FOREACH(se, &st->st_entry, se_list) {
 1193                 ieee80211_ies_expand(&se->base.se_ies);
 1194                 if (match_bss(vap, ss, se, debug) == 0) {
 1195                         if (selbs == NULL)
 1196                                 selbs = se;
 1197                         else if (sta_compare(se, selbs) > 0)
 1198                                 selbs = se;
 1199                 }
 1200         }
 1201         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1202 
 1203         return selbs;
 1204 }
 1205 
 1206 /*
 1207  * Pick an ap or ibss network to join or find a channel
 1208  * to use to start an ibss network.
 1209  */
 1210 static int
 1211 sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1212 {
 1213         struct sta_table *st = ss->ss_priv;
 1214         struct sta_entry *selbs;
 1215         struct ieee80211_channel *chan;
 1216 
 1217         KASSERT(vap->iv_opmode == IEEE80211_M_STA,
 1218                 ("wrong mode %u", vap->iv_opmode));
 1219 
 1220         if (st->st_newscan) {
 1221                 sta_update_notseen(st);
 1222                 st->st_newscan = 0;
 1223         }
 1224         if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
 1225                 /*
 1226                  * Manual/background scan, don't select+join the
 1227                  * bss, just return.  The scanning framework will
 1228                  * handle notification that this has completed.
 1229                  */
 1230                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
 1231                 return 1;
 1232         }
 1233         /*
 1234          * Automatic sequencing; look for a candidate and
 1235          * if found join the network.
 1236          */
 1237         /* NB: unlocked read should be ok */
 1238         if (TAILQ_FIRST(&st->st_entry) == NULL) {
 1239                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
 1240                         "%s: no scan candidate\n", __func__);
 1241                 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1242                         return 0;
 1243 notfound:
 1244                 /*
 1245                  * If nothing suitable was found decrement
 1246                  * the failure counts so entries will be
 1247                  * reconsidered the next time around.  We
 1248                  * really want to do this only for sta's
 1249                  * where we've previously had some success.
 1250                  */
 1251                 sta_dec_fails(st);
 1252                 st->st_newscan = 1;
 1253                 return 0;                       /* restart scan */
 1254         }
 1255         selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
 1256         if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1257                 return (selbs != NULL);
 1258         if (selbs == NULL)
 1259                 goto notfound;
 1260         chan = selbs->base.se_chan;
 1261         if (selbs->se_flags & STA_DEMOTE11B)
 1262                 chan = demote11b(vap, chan);
 1263         if (!ieee80211_sta_join(vap, chan, &selbs->base))
 1264                 goto notfound;
 1265         return 1;                               /* terminate scan */
 1266 }
 1267 
 1268 /*
 1269  * Lookup an entry in the scan cache.  We assume we're
 1270  * called from the bottom half or such that we don't need
 1271  * to block the bottom half so that it's safe to return
 1272  * a reference to an entry w/o holding the lock on the table.
 1273  */
 1274 static struct sta_entry *
 1275 sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1276 {
 1277         struct sta_entry *se;
 1278         int hash = STA_HASH(macaddr);
 1279 
 1280         IEEE80211_SCAN_TABLE_LOCK(st);
 1281         LIST_FOREACH(se, &st->st_hash[hash], se_hash)
 1282                 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
 1283                         break;
 1284         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1285 
 1286         return se;              /* NB: unlocked */
 1287 }
 1288 
 1289 static void
 1290 sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1291 {
 1292         struct ieee80211com *ic = vap->iv_ic;
 1293         struct ieee80211_node *ni = vap->iv_bss;
 1294         struct sta_table *st = ss->ss_priv;
 1295         enum ieee80211_phymode mode;
 1296         struct sta_entry *se, *selbs;
 1297         uint8_t roamRate, curRate, ucastRate;
 1298         int8_t roamRssi, curRssi;
 1299 
 1300         se = sta_lookup(st, ni->ni_macaddr);
 1301         if (se == NULL) {
 1302                 /* XXX something is wrong */
 1303                 return;
 1304         }
 1305 
 1306         mode = ieee80211_chan2mode(ic->ic_bsschan);
 1307         roamRate = vap->iv_roamparms[mode].rate;
 1308         roamRssi = vap->iv_roamparms[mode].rssi;
 1309         ucastRate = vap->iv_txparms[mode].ucastrate;
 1310         /* NB: the most up to date rssi is in the node, not the scan cache */
 1311         curRssi = ic->ic_node_getrssi(ni);
 1312         if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
 1313                 curRate = ni->ni_txrate;
 1314                 roamRate &= IEEE80211_RATE_VAL;
 1315                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
 1316                     "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
 1317                     __func__, curRssi, curRate, roamRssi, roamRate);
 1318         } else {
 1319                 curRate = roamRate;     /* NB: insure compare below fails */
 1320                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
 1321                     "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
 1322         }
 1323         /*
 1324          * Check if a new ap should be used and switch.
 1325          * XXX deauth current ap
 1326          */
 1327         if (curRate < roamRate || curRssi < roamRssi) {
 1328                 if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
 1329                         /*
 1330                          * Scan cache contents are too old; force a scan now
 1331                          * if possible so we have current state to make a
 1332                          * decision with.  We don't kick off a bg scan if
 1333                          * we're using dynamic turbo and boosted or if the
 1334                          * channel is busy.
 1335                          * XXX force immediate switch on scan complete
 1336                          */
 1337                         if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
 1338                             time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
 1339                                 ieee80211_bg_scan(vap, 0);
 1340                         return;
 1341                 }
 1342                 se->base.se_rssi = curRssi;
 1343                 selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
 1344                 if (selbs != NULL && selbs != se) {
 1345                         struct ieee80211_channel *chan;
 1346 
 1347                         IEEE80211_DPRINTF(vap,
 1348                             IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
 1349                             "%s: ROAM: curRate %u, roamRate %u, "
 1350                             "curRssi %d, roamRssi %d\n", __func__,
 1351                             curRate, roamRate, curRssi, roamRssi);
 1352 
 1353                         chan = selbs->base.se_chan;
 1354                         if (selbs->se_flags & STA_DEMOTE11B)
 1355                                 chan = demote11b(vap, chan);
 1356                         (void) ieee80211_sta_join(vap, chan, &selbs->base);
 1357                 }
 1358         }
 1359 }
 1360 
 1361 /*
 1362  * Age entries in the scan cache.
 1363  * XXX also do roaming since it's convenient
 1364  */
 1365 static void
 1366 sta_age(struct ieee80211_scan_state *ss)
 1367 {
 1368         struct ieee80211vap *vap = ss->ss_vap;
 1369 
 1370         adhoc_age(ss);
 1371         /*
 1372          * If rate control is enabled check periodically to see if
 1373          * we should roam from our current connection to one that
 1374          * might be better.  This only applies when we're operating
 1375          * in sta mode and automatic roaming is set.
 1376          * XXX defer if busy
 1377          * XXX repeater station
 1378          * XXX do when !bgscan?
 1379          */
 1380         KASSERT(vap->iv_opmode == IEEE80211_M_STA,
 1381                 ("wrong mode %u", vap->iv_opmode));
 1382         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
 1383             (vap->iv_flags & IEEE80211_F_BGSCAN) &&
 1384             vap->iv_state >= IEEE80211_S_RUN)
 1385                 /* XXX vap is implicit */
 1386                 sta_roam_check(ss, vap);
 1387 }
 1388 
 1389 /*
 1390  * Iterate over the entries in the scan cache, invoking
 1391  * the callback function on each one.
 1392  */
 1393 static void
 1394 sta_iterate(struct ieee80211_scan_state *ss, 
 1395         ieee80211_scan_iter_func *f, void *arg)
 1396 {
 1397         struct sta_table *st = ss->ss_priv;
 1398         struct sta_entry *se;
 1399         u_int gen;
 1400 
 1401         mtx_lock(&st->st_scanlock);
 1402         gen = st->st_scaniter++;
 1403 restart:
 1404         IEEE80211_SCAN_TABLE_LOCK(st);
 1405         TAILQ_FOREACH(se, &st->st_entry, se_list) {
 1406                 if (se->se_scangen != gen) {
 1407                         se->se_scangen = gen;
 1408                         /* update public state */
 1409                         se->base.se_age = ticks - se->se_lastupdate;
 1410                         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1411                         (*f)(arg, &se->base);
 1412                         goto restart;
 1413                 }
 1414         }
 1415         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1416 
 1417         mtx_unlock(&st->st_scanlock);
 1418 }
 1419 
 1420 static void
 1421 sta_assoc_fail(struct ieee80211_scan_state *ss,
 1422         const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
 1423 {
 1424         struct sta_table *st = ss->ss_priv;
 1425         struct sta_entry *se;
 1426 
 1427         se = sta_lookup(st, macaddr);
 1428         if (se != NULL) {
 1429                 se->se_fails++;
 1430                 se->se_lastfail = ticks;
 1431                 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
 1432                     macaddr, "%s: reason %u fails %u",
 1433                     __func__, reason, se->se_fails);
 1434         }
 1435 }
 1436 
 1437 static void
 1438 sta_assoc_success(struct ieee80211_scan_state *ss,
 1439         const uint8_t macaddr[IEEE80211_ADDR_LEN])
 1440 {
 1441         struct sta_table *st = ss->ss_priv;
 1442         struct sta_entry *se;
 1443 
 1444         se = sta_lookup(st, macaddr);
 1445         if (se != NULL) {
 1446 #if 0
 1447                 se->se_fails = 0;
 1448                 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
 1449                     macaddr, "%s: fails %u",
 1450                     __func__, se->se_fails);
 1451 #endif
 1452                 se->se_lastassoc = ticks;
 1453         }
 1454 }
 1455 
 1456 static const struct ieee80211_scanner sta_default = {
 1457         .scan_name              = "default",
 1458         .scan_attach            = sta_attach,
 1459         .scan_detach            = sta_detach,
 1460         .scan_start             = sta_start,
 1461         .scan_restart           = sta_restart,
 1462         .scan_cancel            = sta_cancel,
 1463         .scan_end               = sta_pick_bss,
 1464         .scan_flush             = sta_flush,
 1465         .scan_add               = sta_add,
 1466         .scan_age               = sta_age,
 1467         .scan_iterate           = sta_iterate,
 1468         .scan_assoc_fail        = sta_assoc_fail,
 1469         .scan_assoc_success     = sta_assoc_success,
 1470 };
 1471 IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
 1472 
 1473 /*
 1474  * Adhoc mode-specific support.
 1475  */
 1476 
 1477 static const uint16_t adhocWorld[] =            /* 36, 40, 44, 48 */
 1478 { 5180, 5200, 5220, 5240 };
 1479 static const uint16_t adhocFcc3[] =             /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
 1480 { 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
 1481 static const uint16_t adhocMkk[] =              /* 34, 38, 42, 46 */
 1482 { 5170, 5190, 5210, 5230 };
 1483 static const uint16_t adhoc11b[] =              /* 10, 11 */
 1484 { 2457, 2462 };
 1485 
 1486 static const struct scanlist adhocScanTable[] = {
 1487         { IEEE80211_MODE_11B,           X(adhoc11b) },
 1488         { IEEE80211_MODE_11A,           X(adhocWorld) },
 1489         { IEEE80211_MODE_11A,           X(adhocFcc3) },
 1490         { IEEE80211_MODE_11B,           X(adhocMkk) },
 1491         { .list = NULL }
 1492 };
 1493 #undef X
 1494 
 1495 /*
 1496  * Start an adhoc-mode scan by populating the channel list.
 1497  */
 1498 static int
 1499 adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1500 {
 1501         struct sta_table *st = ss->ss_priv;
 1502         
 1503         makescanlist(ss, vap, adhocScanTable);
 1504 
 1505         if (ss->ss_mindwell == 0)
 1506                 ss->ss_mindwell = msecs_to_ticks(200);  /* 200ms */
 1507         if (ss->ss_maxdwell == 0)
 1508                 ss->ss_maxdwell = msecs_to_ticks(200);  /* 200ms */
 1509 
 1510         st->st_scangen++;
 1511         st->st_newscan = 1;
 1512 
 1513         return 0;
 1514 }
 1515 
 1516 /*
 1517  * Select a channel to start an adhoc network on.
 1518  * The channel list was populated with appropriate
 1519  * channels so select one that looks least occupied.
 1520  */
 1521 static struct ieee80211_channel *
 1522 adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
 1523 {
 1524         struct sta_table *st = ss->ss_priv;
 1525         struct sta_entry *se;
 1526         struct ieee80211_channel *c, *bestchan;
 1527         int i, bestrssi, maxrssi;
 1528 
 1529         bestchan = NULL;
 1530         bestrssi = -1;
 1531 
 1532         IEEE80211_SCAN_TABLE_LOCK(st);
 1533         for (i = 0; i < ss->ss_last; i++) {
 1534                 c = ss->ss_chans[i];
 1535                 /* never consider a channel with radar */
 1536                 if (IEEE80211_IS_CHAN_RADAR(c))
 1537                         continue;
 1538                 /* skip channels disallowed by regulatory settings */
 1539                 if (IEEE80211_IS_CHAN_NOADHOC(c))
 1540                         continue;
 1541                 /* check channel attributes for band compatibility */
 1542                 if (flags != 0 && (c->ic_flags & flags) != flags)
 1543                         continue;
 1544                 maxrssi = 0;
 1545                 TAILQ_FOREACH(se, &st->st_entry, se_list) {
 1546                         if (se->base.se_chan != c)
 1547                                 continue;
 1548                         if (se->base.se_rssi > maxrssi)
 1549                                 maxrssi = se->base.se_rssi;
 1550                 }
 1551                 if (bestchan == NULL || maxrssi < bestrssi)
 1552                         bestchan = c;
 1553         }
 1554         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1555 
 1556         return bestchan;
 1557 }
 1558 
 1559 /*
 1560  * Pick an ibss network to join or find a channel
 1561  * to use to start an ibss network.
 1562  */
 1563 static int
 1564 adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1565 {
 1566         struct sta_table *st = ss->ss_priv;
 1567         struct sta_entry *selbs;
 1568         struct ieee80211_channel *chan;
 1569         struct ieee80211com *ic = vap->iv_ic;
 1570 
 1571         KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
 1572                 vap->iv_opmode == IEEE80211_M_AHDEMO ||
 1573                 vap->iv_opmode == IEEE80211_M_MBSS,
 1574                 ("wrong opmode %u", vap->iv_opmode));
 1575 
 1576         if (st->st_newscan) {
 1577                 sta_update_notseen(st);
 1578                 st->st_newscan = 0;
 1579         }
 1580         if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
 1581                 /*
 1582                  * Manual/background scan, don't select+join the
 1583                  * bss, just return.  The scanning framework will
 1584                  * handle notification that this has completed.
 1585                  */
 1586                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
 1587                 return 1;
 1588         }
 1589         /*
 1590          * Automatic sequencing; look for a candidate and
 1591          * if found join the network.
 1592          */
 1593         /* NB: unlocked read should be ok */
 1594         if (TAILQ_FIRST(&st->st_entry) == NULL) {
 1595                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
 1596                         "%s: no scan candidate\n", __func__);
 1597                 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1598                         return 0;
 1599 notfound:
 1600                 /* NB: never auto-start a tdma network for slot !0 */
 1601 #ifdef IEEE80211_SUPPORT_TDMA
 1602                 if (vap->iv_des_nssid &&
 1603                     ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
 1604                      ieee80211_tdma_getslot(vap) == 0)) {
 1605 #else
 1606                 if (vap->iv_des_nssid) {
 1607 #endif
 1608                         /*
 1609                          * No existing adhoc network to join and we have
 1610                          * an ssid; start one up.  If no channel was
 1611                          * specified, try to select a channel.
 1612                          */
 1613                         if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
 1614                             IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
 1615                                 chan = adhoc_pick_channel(ss, 0);
 1616                         } else
 1617                                 chan = vap->iv_des_chan;
 1618                         if (chan != NULL) {
 1619                                 struct ieee80211com *ic = vap->iv_ic;
 1620                                 /*
 1621                                  * Create a HT capable IBSS; the per-node
 1622                                  * probe request/response will result in
 1623                                  * "correct" rate control capabilities being
 1624                                  * negotiated.
 1625                                  */
 1626                                 chan = ieee80211_ht_adjust_channel(ic,
 1627                                     chan, vap->iv_flags_ht);
 1628                                 ieee80211_create_ibss(vap, chan);
 1629                                 return 1;
 1630                         }
 1631                 }
 1632                 /*
 1633                  * If nothing suitable was found decrement
 1634                  * the failure counts so entries will be
 1635                  * reconsidered the next time around.  We
 1636                  * really want to do this only for sta's
 1637                  * where we've previously had some success.
 1638                  */
 1639                 sta_dec_fails(st);
 1640                 st->st_newscan = 1;
 1641                 return 0;                       /* restart scan */
 1642         }
 1643         selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
 1644         if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1645                 return (selbs != NULL);
 1646         if (selbs == NULL)
 1647                 goto notfound;
 1648         chan = selbs->base.se_chan;
 1649         if (selbs->se_flags & STA_DEMOTE11B)
 1650                 chan = demote11b(vap, chan);
 1651         /*
 1652          * If HT is available, make it a possibility here.
 1653          * The intent is to enable HT20/HT40 when joining a non-HT
 1654          * IBSS node; we can then advertise HT IEs and speak HT
 1655          * to any subsequent nodes that support it.
 1656          */
 1657         chan = ieee80211_ht_adjust_channel(ic,
 1658             chan, vap->iv_flags_ht);
 1659         if (!ieee80211_sta_join(vap, chan, &selbs->base))
 1660                 goto notfound;
 1661         return 1;                               /* terminate scan */
 1662 }
 1663 
 1664 /*
 1665  * Age entries in the scan cache.
 1666  */
 1667 static void
 1668 adhoc_age(struct ieee80211_scan_state *ss)
 1669 {
 1670         struct sta_table *st = ss->ss_priv;
 1671         struct sta_entry *se, *next;
 1672 
 1673         IEEE80211_SCAN_TABLE_LOCK(st);
 1674         TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
 1675                 if (se->se_notseen > STA_PURGE_SCANS) {
 1676                         TAILQ_REMOVE(&st->st_entry, se, se_list);
 1677                         LIST_REMOVE(se, se_hash);
 1678                         ieee80211_ies_cleanup(&se->base.se_ies);
 1679                         free(se, M_80211_SCAN);
 1680                 }
 1681         }
 1682         IEEE80211_SCAN_TABLE_UNLOCK(st);
 1683 }
 1684 
 1685 static const struct ieee80211_scanner adhoc_default = {
 1686         .scan_name              = "default",
 1687         .scan_attach            = sta_attach,
 1688         .scan_detach            = sta_detach,
 1689         .scan_start             = adhoc_start,
 1690         .scan_restart           = sta_restart,
 1691         .scan_cancel            = sta_cancel,
 1692         .scan_end               = adhoc_pick_bss,
 1693         .scan_flush             = sta_flush,
 1694         .scan_pickchan          = adhoc_pick_channel,
 1695         .scan_add               = sta_add,
 1696         .scan_age               = adhoc_age,
 1697         .scan_iterate           = sta_iterate,
 1698         .scan_assoc_fail        = sta_assoc_fail,
 1699         .scan_assoc_success     = sta_assoc_success,
 1700 };
 1701 IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
 1702 IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
 1703 
 1704 static void
 1705 ap_force_promisc(struct ieee80211com *ic)
 1706 {
 1707         struct ifnet *ifp = ic->ic_ifp;
 1708 
 1709         IEEE80211_LOCK(ic);
 1710         /* set interface into promiscuous mode */
 1711         ifp->if_flags |= IFF_PROMISC;
 1712         ieee80211_runtask(ic, &ic->ic_promisc_task);
 1713         IEEE80211_UNLOCK(ic);
 1714 }
 1715 
 1716 static void
 1717 ap_reset_promisc(struct ieee80211com *ic)
 1718 {
 1719         IEEE80211_LOCK(ic);
 1720         ieee80211_syncifflag_locked(ic, IFF_PROMISC);
 1721         IEEE80211_UNLOCK(ic);
 1722 }
 1723 
 1724 static int
 1725 ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1726 {
 1727         struct sta_table *st = ss->ss_priv;
 1728 
 1729         makescanlist(ss, vap, staScanTable);
 1730 
 1731         if (ss->ss_mindwell == 0)
 1732                 ss->ss_mindwell = msecs_to_ticks(200);  /* 200ms */
 1733         if (ss->ss_maxdwell == 0)
 1734                 ss->ss_maxdwell = msecs_to_ticks(200);  /* 200ms */
 1735 
 1736         st->st_scangen++;
 1737         st->st_newscan = 1;
 1738 
 1739         ap_force_promisc(vap->iv_ic);
 1740         return 0;
 1741 }
 1742 
 1743 /*
 1744  * Cancel an ongoing scan.
 1745  */
 1746 static int
 1747 ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1748 {
 1749         ap_reset_promisc(vap->iv_ic);
 1750         return 0;
 1751 }
 1752 
 1753 /*
 1754  * Pick a quiet channel to use for ap operation.
 1755  */
 1756 static struct ieee80211_channel *
 1757 ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
 1758 {
 1759         struct sta_table *st = ss->ss_priv;
 1760         struct ieee80211_channel *bestchan = NULL;
 1761         int i;
 1762 
 1763         /* XXX select channel more intelligently, e.g. channel spread, power */
 1764         /* NB: use scan list order to preserve channel preference */
 1765         for (i = 0; i < ss->ss_last; i++) {
 1766                 struct ieee80211_channel *chan = ss->ss_chans[i];
 1767                 /*
 1768                  * If the channel is unoccupied the max rssi
 1769                  * should be zero; just take it.  Otherwise
 1770                  * track the channel with the lowest rssi and
 1771                  * use that when all channels appear occupied.
 1772                  */
 1773                 if (IEEE80211_IS_CHAN_RADAR(chan))
 1774                         continue;
 1775                 if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
 1776                         continue;
 1777                 /* check channel attributes for band compatibility */
 1778                 if (flags != 0 && (chan->ic_flags & flags) != flags)
 1779                         continue;
 1780                 KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
 1781                 /* XXX channel have interference */
 1782                 if (st->st_maxrssi[chan->ic_ieee] == 0) {
 1783                         /* XXX use other considerations */
 1784                         return chan;
 1785                 }
 1786                 if (bestchan == NULL ||
 1787                     st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
 1788                         bestchan = chan;
 1789         }
 1790         return bestchan;
 1791 }
 1792 
 1793 /*
 1794  * Pick a quiet channel to use for ap operation.
 1795  */
 1796 static int
 1797 ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1798 {
 1799         struct ieee80211com *ic = vap->iv_ic;
 1800         struct ieee80211_channel *bestchan;
 1801 
 1802         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
 1803                 ("wrong opmode %u", vap->iv_opmode));
 1804         bestchan = ap_pick_channel(ss, 0);
 1805         if (bestchan == NULL) {
 1806                 /* no suitable channel, should not happen */
 1807                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
 1808                     "%s: no suitable channel! (should not happen)\n", __func__);
 1809                 /* XXX print something? */
 1810                 return 0;                       /* restart scan */
 1811         }
 1812         /*
 1813          * If this is a dynamic turbo channel, start with the unboosted one.
 1814          */
 1815         if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
 1816                 bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
 1817                         bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
 1818                 if (bestchan == NULL) {
 1819                         /* should never happen ?? */
 1820                         return 0;
 1821                 }
 1822         }
 1823         ap_reset_promisc(ic);
 1824         if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
 1825                 /*
 1826                  * Manual/background scan, don't select+join the
 1827                  * bss, just return.  The scanning framework will
 1828                  * handle notification that this has completed.
 1829                  */
 1830                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
 1831                 return 1;
 1832         }
 1833         ieee80211_create_ibss(vap,
 1834             ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht));
 1835         return 1;
 1836 }
 1837 
 1838 static const struct ieee80211_scanner ap_default = {
 1839         .scan_name              = "default",
 1840         .scan_attach            = sta_attach,
 1841         .scan_detach            = sta_detach,
 1842         .scan_start             = ap_start,
 1843         .scan_restart           = sta_restart,
 1844         .scan_cancel            = ap_cancel,
 1845         .scan_end               = ap_end,
 1846         .scan_flush             = sta_flush,
 1847         .scan_pickchan          = ap_pick_channel,
 1848         .scan_add               = sta_add,
 1849         .scan_age               = adhoc_age,
 1850         .scan_iterate           = sta_iterate,
 1851         .scan_assoc_success     = sta_assoc_success,
 1852         .scan_assoc_fail        = sta_assoc_fail,
 1853 };
 1854 IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
 1855 
 1856 #ifdef IEEE80211_SUPPORT_MESH
 1857 /*
 1858  * Pick an mbss network to join or find a channel
 1859  * to use to start an mbss network.
 1860  */
 1861 static int
 1862 mesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
 1863 {
 1864         struct sta_table *st = ss->ss_priv;
 1865         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1866         struct sta_entry *selbs;
 1867         struct ieee80211_channel *chan;
 1868 
 1869         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
 1870                 ("wrong opmode %u", vap->iv_opmode));
 1871 
 1872         if (st->st_newscan) {
 1873                 sta_update_notseen(st);
 1874                 st->st_newscan = 0;
 1875         }
 1876         if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
 1877                 /*
 1878                  * Manual/background scan, don't select+join the
 1879                  * bss, just return.  The scanning framework will
 1880                  * handle notification that this has completed.
 1881                  */
 1882                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
 1883                 return 1;
 1884         }
 1885         /*
 1886          * Automatic sequencing; look for a candidate and
 1887          * if found join the network.
 1888          */
 1889         /* NB: unlocked read should be ok */
 1890         if (TAILQ_FIRST(&st->st_entry) == NULL) {
 1891                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
 1892                         "%s: no scan candidate\n", __func__);
 1893                 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1894                         return 0;
 1895 notfound:
 1896                 if (ms->ms_idlen != 0) {
 1897                         /*
 1898                          * No existing mbss network to join and we have
 1899                          * a meshid; start one up.  If no channel was
 1900                          * specified, try to select a channel.
 1901                          */
 1902                         if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
 1903                             IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
 1904                                 struct ieee80211com *ic = vap->iv_ic;
 1905 
 1906                                 chan = adhoc_pick_channel(ss, 0);
 1907                                 if (chan != NULL)
 1908                                         chan = ieee80211_ht_adjust_channel(ic,
 1909                                             chan, vap->iv_flags_ht);
 1910                         } else
 1911                                 chan = vap->iv_des_chan;
 1912                         if (chan != NULL) {
 1913                                 ieee80211_create_ibss(vap, chan);
 1914                                 return 1;
 1915                         }
 1916                 }
 1917                 /*
 1918                  * If nothing suitable was found decrement
 1919                  * the failure counts so entries will be
 1920                  * reconsidered the next time around.  We
 1921                  * really want to do this only for sta's
 1922                  * where we've previously had some success.
 1923                  */
 1924                 sta_dec_fails(st);
 1925                 st->st_newscan = 1;
 1926                 return 0;                       /* restart scan */
 1927         }
 1928         selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
 1929         if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
 1930                 return (selbs != NULL);
 1931         if (selbs == NULL)
 1932                 goto notfound;
 1933         chan = selbs->base.se_chan;
 1934         if (selbs->se_flags & STA_DEMOTE11B)
 1935                 chan = demote11b(vap, chan);
 1936         if (!ieee80211_sta_join(vap, chan, &selbs->base))
 1937                 goto notfound;
 1938         return 1;                               /* terminate scan */
 1939 }
 1940 
 1941 static const struct ieee80211_scanner mesh_default = {
 1942         .scan_name              = "default",
 1943         .scan_attach            = sta_attach,
 1944         .scan_detach            = sta_detach,
 1945         .scan_start             = adhoc_start,
 1946         .scan_restart           = sta_restart,
 1947         .scan_cancel            = sta_cancel,
 1948         .scan_end               = mesh_pick_bss,
 1949         .scan_flush             = sta_flush,
 1950         .scan_pickchan          = adhoc_pick_channel,
 1951         .scan_add               = sta_add,
 1952         .scan_age               = adhoc_age,
 1953         .scan_iterate           = sta_iterate,
 1954         .scan_assoc_fail        = sta_assoc_fail,
 1955         .scan_assoc_success     = sta_assoc_success,
 1956 };
 1957 IEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default);
 1958 #endif /* IEEE80211_SUPPORT_MESH */

Cache object: 9e33b3f88cf35d5e97db36447aa96a7e


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