ieee80211_scan_sta.c revision 215699
1170530Ssam/*- 2186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3170530Ssam * All rights reserved. 4170530Ssam * 5170530Ssam * Redistribution and use in source and binary forms, with or without 6170530Ssam * modification, are permitted provided that the following conditions 7170530Ssam * are met: 8170530Ssam * 1. Redistributions of source code must retain the above copyright 9170530Ssam * notice, this list of conditions and the following disclaimer. 10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright 11170530Ssam * notice, this list of conditions and the following disclaimer in the 12170530Ssam * documentation and/or other materials provided with the distribution. 13170530Ssam * 14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24170530Ssam */ 25170530Ssam 26170530Ssam#include <sys/cdefs.h> 27170530Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_scan_sta.c 215699 2010-11-22 19:01:47Z bschmidt $"); 28170530Ssam 29170530Ssam/* 30170530Ssam * IEEE 802.11 station scanning support. 31170530Ssam */ 32178354Ssam#include "opt_wlan.h" 33178354Ssam 34170530Ssam#include <sys/param.h> 35191746Sthompsa#include <sys/systm.h> 36170530Ssam#include <sys/kernel.h> 37170530Ssam#include <sys/module.h> 38191746Sthompsa 39170530Ssam#include <sys/socket.h> 40170530Ssam 41170530Ssam#include <net/if.h> 42170530Ssam#include <net/if_media.h> 43170530Ssam#include <net/ethernet.h> 44170530Ssam 45170530Ssam#include <net80211/ieee80211_var.h> 46178354Ssam#include <net80211/ieee80211_input.h> 47178354Ssam#include <net80211/ieee80211_regdomain.h> 48186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 49186904Ssam#include <net80211/ieee80211_tdma.h> 50186904Ssam#endif 51195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH 52195618Srpaulo#include <net80211/ieee80211_mesh.h> 53195618Srpaulo#endif 54170530Ssam 55170530Ssam#include <net/bpf.h> 56170530Ssam 57170530Ssam/* 58170530Ssam * Parameters for managing cache entries: 59170530Ssam * 60170530Ssam * o a station with STA_FAILS_MAX failures is not considered 61170530Ssam * when picking a candidate 62170530Ssam * o a station that hasn't had an update in STA_PURGE_SCANS 63170530Ssam * (background) scans is discarded 64170530Ssam * o after STA_FAILS_AGE seconds we clear the failure count 65170530Ssam */ 66170530Ssam#define STA_FAILS_MAX 2 /* assoc failures before ignored */ 67170530Ssam#define STA_FAILS_AGE (2*60) /* time before clearing fails (secs) */ 68170530Ssam#define STA_PURGE_SCANS 2 /* age for purging entries (scans) */ 69170530Ssam 70170530Ssam/* XXX tunable */ 71170530Ssam#define STA_RSSI_MIN 8 /* min acceptable rssi */ 72170530Ssam#define STA_RSSI_MAX 40 /* max rssi for comparison */ 73170530Ssam 74170530Ssamstruct sta_entry { 75170530Ssam struct ieee80211_scan_entry base; 76170530Ssam TAILQ_ENTRY(sta_entry) se_list; 77170530Ssam LIST_ENTRY(sta_entry) se_hash; 78170530Ssam uint8_t se_fails; /* failure to associate count */ 79170530Ssam uint8_t se_seen; /* seen during current scan */ 80170530Ssam uint8_t se_notseen; /* not seen in previous scans */ 81170530Ssam uint8_t se_flags; 82184302Ssam#define STA_DEMOTE11B 0x01 /* match w/ demoted 11b chan */ 83170530Ssam uint32_t se_avgrssi; /* LPF rssi state */ 84170530Ssam unsigned long se_lastupdate; /* time of last update */ 85170530Ssam unsigned long se_lastfail; /* time of last failure */ 86170530Ssam unsigned long se_lastassoc; /* time of last association */ 87170530Ssam u_int se_scangen; /* iterator scan gen# */ 88178354Ssam u_int se_countrygen; /* gen# of last cc notify */ 89170530Ssam}; 90170530Ssam 91170530Ssam#define STA_HASHSIZE 32 92170530Ssam/* simple hash is enough for variation of macaddr */ 93170530Ssam#define STA_HASH(addr) \ 94170530Ssam (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE) 95170530Ssam 96186107Ssam#define MAX_IEEE_CHAN 256 /* max acceptable IEEE chan # */ 97186107SsamCTASSERT(MAX_IEEE_CHAN >= 256); 98186107Ssam 99170530Ssamstruct sta_table { 100206617Srpaulo ieee80211_scan_table_lock_t st_lock; /* on scan table */ 101170530Ssam TAILQ_HEAD(, sta_entry) st_entry; /* all entries */ 102170530Ssam LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE]; 103178354Ssam struct mtx st_scanlock; /* on st_scaniter */ 104178354Ssam u_int st_scaniter; /* gen# for iterator */ 105178354Ssam u_int st_scangen; /* scan generation # */ 106170530Ssam int st_newscan; 107178354Ssam /* ap-related state */ 108186107Ssam int st_maxrssi[MAX_IEEE_CHAN]; 109170530Ssam}; 110170530Ssam 111170530Ssamstatic void sta_flush_table(struct sta_table *); 112171409Ssam/* 113171409Ssam * match_bss returns a bitmask describing if an entry is suitable 114171409Ssam * for use. If non-zero the entry was deemed not suitable and it's 115171409Ssam * contents explains why. The following flags are or'd to to this 116171409Ssam * mask and can be used to figure out why the entry was rejected. 117171409Ssam */ 118195618Srpaulo#define MATCH_CHANNEL 0x00001 /* channel mismatch */ 119195618Srpaulo#define MATCH_CAPINFO 0x00002 /* capabilities mismatch, e.g. no ess */ 120195618Srpaulo#define MATCH_PRIVACY 0x00004 /* privacy mismatch */ 121195618Srpaulo#define MATCH_RATE 0x00008 /* rate set mismatch */ 122195618Srpaulo#define MATCH_SSID 0x00010 /* ssid mismatch */ 123195618Srpaulo#define MATCH_BSSID 0x00020 /* bssid mismatch */ 124195618Srpaulo#define MATCH_FAILS 0x00040 /* too many failed auth attempts */ 125195618Srpaulo#define MATCH_NOTSEEN 0x00080 /* not seen in recent scans */ 126195618Srpaulo#define MATCH_RSSI 0x00100 /* rssi deemed too low to use */ 127195618Srpaulo#define MATCH_CC 0x00200 /* country code mismatch */ 128195618Srpaulo#define MATCH_TDMA_NOIE 0x00400 /* no TDMA ie */ 129195618Srpaulo#define MATCH_TDMA_NOTMASTER 0x00800 /* not TDMA master */ 130195618Srpaulo#define MATCH_TDMA_NOSLOT 0x01000 /* all TDMA slots occupied */ 131195618Srpaulo#define MATCH_TDMA_LOCAL 0x02000 /* local address */ 132195618Srpaulo#define MATCH_TDMA_VERSION 0x04000 /* protocol version mismatch */ 133195618Srpaulo#define MATCH_MESH_NOID 0x10000 /* no MESHID ie */ 134195618Srpaulo#define MATCH_MESHID 0x20000 /* meshid mismatch */ 135178354Ssamstatic int match_bss(struct ieee80211vap *, 136170530Ssam const struct ieee80211_scan_state *, struct sta_entry *, int); 137178354Ssamstatic void adhoc_age(struct ieee80211_scan_state *); 138170530Ssam 139178354Ssamstatic __inline int 140178354Ssamisocmp(const uint8_t cc1[], const uint8_t cc2[]) 141178354Ssam{ 142178354Ssam return (cc1[0] == cc2[0] && cc1[1] == cc2[1]); 143178354Ssam} 144178354Ssam 145170530Ssam/* number of references from net80211 layer */ 146170530Ssamstatic int nrefs = 0; 147195618Srpaulo/* 148195618Srpaulo * Module glue. 149195618Srpaulo */ 150195618SrpauloIEEE80211_SCANNER_MODULE(sta, 1); 151170530Ssam 152170530Ssam/* 153170530Ssam * Attach prior to any scanning work. 154170530Ssam */ 155170530Ssamstatic int 156170530Ssamsta_attach(struct ieee80211_scan_state *ss) 157170530Ssam{ 158170530Ssam struct sta_table *st; 159170530Ssam 160186302Ssam st = (struct sta_table *) malloc(sizeof(struct sta_table), 161170530Ssam M_80211_SCAN, M_NOWAIT | M_ZERO); 162170530Ssam if (st == NULL) 163170530Ssam return 0; 164206617Srpaulo IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable"); 165170530Ssam mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF); 166170530Ssam TAILQ_INIT(&st->st_entry); 167170530Ssam ss->ss_priv = st; 168170530Ssam nrefs++; /* NB: we assume caller locking */ 169170530Ssam return 1; 170170530Ssam} 171170530Ssam 172170530Ssam/* 173170530Ssam * Cleanup any private state. 174170530Ssam */ 175170530Ssamstatic int 176170530Ssamsta_detach(struct ieee80211_scan_state *ss) 177170530Ssam{ 178170530Ssam struct sta_table *st = ss->ss_priv; 179170530Ssam 180170530Ssam if (st != NULL) { 181170530Ssam sta_flush_table(st); 182206617Srpaulo IEEE80211_SCAN_TABLE_LOCK_DESTROY(st); 183170530Ssam mtx_destroy(&st->st_scanlock); 184186302Ssam free(st, M_80211_SCAN); 185170530Ssam KASSERT(nrefs > 0, ("imbalanced attach/detach")); 186170530Ssam nrefs--; /* NB: we assume caller locking */ 187170530Ssam } 188170530Ssam return 1; 189170530Ssam} 190170530Ssam 191170530Ssam/* 192170530Ssam * Flush all per-scan state. 193170530Ssam */ 194170530Ssamstatic int 195170530Ssamsta_flush(struct ieee80211_scan_state *ss) 196170530Ssam{ 197170530Ssam struct sta_table *st = ss->ss_priv; 198170530Ssam 199206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 200170530Ssam sta_flush_table(st); 201206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 202170530Ssam ss->ss_last = 0; 203170530Ssam return 0; 204170530Ssam} 205170530Ssam 206170530Ssam/* 207170530Ssam * Flush all entries in the scan cache. 208170530Ssam */ 209170530Ssamstatic void 210170530Ssamsta_flush_table(struct sta_table *st) 211170530Ssam{ 212170530Ssam struct sta_entry *se, *next; 213170530Ssam 214170530Ssam TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) { 215170530Ssam TAILQ_REMOVE(&st->st_entry, se, se_list); 216170530Ssam LIST_REMOVE(se, se_hash); 217178354Ssam ieee80211_ies_cleanup(&se->base.se_ies); 218186302Ssam free(se, M_80211_SCAN); 219170530Ssam } 220178354Ssam memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi)); 221170530Ssam} 222170530Ssam 223170530Ssam/* 224170530Ssam * Process a beacon or probe response frame; create an 225170530Ssam * entry in the scan cache or update any previous entry. 226170530Ssam */ 227170530Ssamstatic int 228170530Ssamsta_add(struct ieee80211_scan_state *ss, 229170530Ssam const struct ieee80211_scanparams *sp, 230170530Ssam const struct ieee80211_frame *wh, 231192468Ssam int subtype, int rssi, int noise) 232170530Ssam{ 233170530Ssam#define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 234170530Ssam#define PICK1ST(_ss) \ 235170530Ssam ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \ 236170530Ssam IEEE80211_SCAN_PICK1ST) 237170530Ssam struct sta_table *st = ss->ss_priv; 238170530Ssam const uint8_t *macaddr = wh->i_addr2; 239178354Ssam struct ieee80211vap *vap = ss->ss_vap; 240178354Ssam struct ieee80211com *ic = vap->iv_ic; 241170530Ssam struct sta_entry *se; 242170530Ssam struct ieee80211_scan_entry *ise; 243178354Ssam int hash; 244170530Ssam 245170530Ssam hash = STA_HASH(macaddr); 246170530Ssam 247206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 248170530Ssam LIST_FOREACH(se, &st->st_hash[hash], se_hash) 249170530Ssam if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr)) 250170530Ssam goto found; 251186302Ssam se = (struct sta_entry *) malloc(sizeof(struct sta_entry), 252170530Ssam M_80211_SCAN, M_NOWAIT | M_ZERO); 253170530Ssam if (se == NULL) { 254206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 255170530Ssam return 0; 256170530Ssam } 257178354Ssam se->se_scangen = st->st_scaniter-1; 258178354Ssam se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 259170530Ssam IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr); 260170530Ssam TAILQ_INSERT_TAIL(&st->st_entry, se, se_list); 261170530Ssam LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash); 262170530Ssamfound: 263170530Ssam ise = &se->base; 264170530Ssam /* XXX ap beaconing multiple ssid w/ same bssid */ 265170530Ssam if (sp->ssid[1] != 0 && 266170530Ssam (ISPROBE(subtype) || ise->se_ssid[1] == 0)) 267170530Ssam memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]); 268170530Ssam KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE, 269170530Ssam ("rate set too large: %u", sp->rates[1])); 270170530Ssam memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]); 271170530Ssam if (sp->xrates != NULL) { 272170530Ssam /* XXX validate xrates[1] */ 273178354Ssam KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE, 274170530Ssam ("xrate set too large: %u", sp->xrates[1])); 275170530Ssam memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]); 276170530Ssam } else 277170530Ssam ise->se_xrates[1] = 0; 278170530Ssam IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3); 279178354Ssam if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) { 280173368Ssam /* 281173368Ssam * Record rssi data using extended precision LPF filter. 282173368Ssam * 283173368Ssam * NB: use only on-channel data to insure we get a good 284173368Ssam * estimate of the signal we'll see when associated. 285173368Ssam */ 286178354Ssam IEEE80211_RSSI_LPF(se->se_avgrssi, rssi); 287178354Ssam ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi); 288173368Ssam ise->se_noise = noise; 289173368Ssam } 290170530Ssam memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp)); 291170530Ssam ise->se_intval = sp->bintval; 292170530Ssam ise->se_capinfo = sp->capinfo; 293195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH 294195618Srpaulo if (sp->meshid != NULL && sp->meshid[1] != 0) 295195618Srpaulo memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]); 296195618Srpaulo#endif 297173368Ssam /* 298173368Ssam * Beware of overriding se_chan for frames seen 299173368Ssam * off-channel; this can cause us to attempt an 300178354Ssam * association on the wrong channel. 301173368Ssam */ 302178354Ssam if (sp->status & IEEE80211_BPARSE_OFFCHAN) { 303173862Ssam struct ieee80211_channel *c; 304173368Ssam /* 305173862Ssam * Off-channel, locate the home/bss channel for the sta 306178354Ssam * using the value broadcast in the DSPARMS ie. We know 307178354Ssam * sp->chan has this value because it's used to calculate 308178354Ssam * IEEE80211_BPARSE_OFFCHAN. 309173368Ssam */ 310178354Ssam c = ieee80211_find_channel_byieee(ic, sp->chan, 311178354Ssam ic->ic_curchan->ic_flags); 312173956Ssam if (c != NULL) { 313173956Ssam ise->se_chan = c; 314173956Ssam } else if (ise->se_chan == NULL) { 315173862Ssam /* should not happen, pick something */ 316178354Ssam ise->se_chan = ic->ic_curchan; 317173862Ssam } 318173862Ssam } else 319178354Ssam ise->se_chan = ic->ic_curchan; 320170530Ssam ise->se_fhdwell = sp->fhdwell; 321170530Ssam ise->se_fhindex = sp->fhindex; 322170530Ssam ise->se_erp = sp->erp; 323170530Ssam ise->se_timoff = sp->timoff; 324170530Ssam if (sp->tim != NULL) { 325170530Ssam const struct ieee80211_tim_ie *tim = 326170530Ssam (const struct ieee80211_tim_ie *) sp->tim; 327170530Ssam ise->se_dtimperiod = tim->tim_period; 328170530Ssam } 329178354Ssam if (sp->country != NULL) { 330178354Ssam const struct ieee80211_country_ie *cie = 331178354Ssam (const struct ieee80211_country_ie *) sp->country; 332178354Ssam /* 333178354Ssam * If 11d is enabled and we're attempting to join a bss 334178354Ssam * that advertises it's country code then compare our 335178354Ssam * current settings to what we fetched from the country ie. 336178354Ssam * If our country code is unspecified or different then 337178354Ssam * dispatch an event to user space that identifies the 338178354Ssam * country code so our regdomain config can be changed. 339178354Ssam */ 340178354Ssam /* XXX only for STA mode? */ 341178354Ssam if ((IEEE80211_IS_CHAN_11D(ise->se_chan) || 342178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) && 343178354Ssam (ic->ic_regdomain.country == CTRY_DEFAULT || 344178354Ssam !isocmp(cie->cc, ic->ic_regdomain.isocc))) { 345178354Ssam /* only issue one notify event per scan */ 346178354Ssam if (se->se_countrygen != st->st_scangen) { 347178354Ssam ieee80211_notify_country(vap, ise->se_bssid, 348178354Ssam cie->cc); 349178354Ssam se->se_countrygen = st->st_scangen; 350178354Ssam } 351178354Ssam } 352178354Ssam ise->se_cc[0] = cie->cc[0]; 353178354Ssam ise->se_cc[1] = cie->cc[1]; 354178354Ssam } 355178354Ssam /* NB: no need to setup ie ptrs; they are not (currently) used */ 356178354Ssam (void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len); 357170530Ssam 358170530Ssam /* clear failure count after STA_FAIL_AGE passes */ 359170530Ssam if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) { 360170530Ssam se->se_fails = 0; 361178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr, 362170530Ssam "%s: fails %u", __func__, se->se_fails); 363170530Ssam } 364170530Ssam 365170530Ssam se->se_lastupdate = ticks; /* update time */ 366170530Ssam se->se_seen = 1; 367170530Ssam se->se_notseen = 0; 368170530Ssam 369186107Ssam KASSERT(sizeof(sp->bchan) == 1, ("bchan size")); 370178354Ssam if (rssi > st->st_maxrssi[sp->bchan]) 371178354Ssam st->st_maxrssi[sp->bchan] = rssi; 372178354Ssam 373206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 374170530Ssam 375170530Ssam /* 376170530Ssam * If looking for a quick choice and nothing's 377170530Ssam * been found check here. 378170530Ssam */ 379178354Ssam if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0) 380170530Ssam ss->ss_flags |= IEEE80211_SCAN_GOTPICK; 381170530Ssam 382170530Ssam return 1; 383170530Ssam#undef PICK1ST 384170530Ssam#undef ISPROBE 385170530Ssam} 386170530Ssam 387170530Ssam/* 388170530Ssam * Check if a channel is excluded by user request. 389170530Ssam */ 390170530Ssamstatic int 391178354Ssamisexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c) 392170530Ssam{ 393178354Ssam return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) || 394178354Ssam (vap->iv_des_chan != IEEE80211_CHAN_ANYC && 395178354Ssam c->ic_freq != vap->iv_des_chan->ic_freq)); 396170530Ssam} 397170530Ssam 398170530Ssamstatic struct ieee80211_channel * 399170530Ssamfind11gchannel(struct ieee80211com *ic, int i, int freq) 400170530Ssam{ 401170530Ssam struct ieee80211_channel *c; 402170530Ssam int j; 403170530Ssam 404170530Ssam /* 405170530Ssam * The normal ordering in the channel list is b channel 406170530Ssam * immediately followed by g so optimize the search for 407170530Ssam * this. We'll still do a full search just in case. 408170530Ssam */ 409170530Ssam for (j = i+1; j < ic->ic_nchans; j++) { 410170530Ssam c = &ic->ic_channels[j]; 411187991Ssam if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c)) 412170530Ssam return c; 413170530Ssam } 414170530Ssam for (j = 0; j < i; j++) { 415170530Ssam c = &ic->ic_channels[j]; 416187991Ssam if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c)) 417170530Ssam return c; 418170530Ssam } 419170530Ssam return NULL; 420170530Ssam} 421188777Ssam 422170530Ssamstatic const u_int chanflags[IEEE80211_MODE_MAX] = { 423188777Ssam [IEEE80211_MODE_AUTO] = IEEE80211_CHAN_B, 424188777Ssam [IEEE80211_MODE_11A] = IEEE80211_CHAN_A, 425188777Ssam [IEEE80211_MODE_11B] = IEEE80211_CHAN_B, 426188777Ssam [IEEE80211_MODE_11G] = IEEE80211_CHAN_G, 427188777Ssam [IEEE80211_MODE_FH] = IEEE80211_CHAN_FHSS, 428188777Ssam /* check base channel */ 429188777Ssam [IEEE80211_MODE_TURBO_A] = IEEE80211_CHAN_A, 430188777Ssam [IEEE80211_MODE_TURBO_G] = IEEE80211_CHAN_G, 431188777Ssam [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST, 432188782Ssam [IEEE80211_MODE_HALF] = IEEE80211_CHAN_HALF, 433188782Ssam [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_QUARTER, 434188777Ssam /* check legacy */ 435188777Ssam [IEEE80211_MODE_11NA] = IEEE80211_CHAN_A, 436188777Ssam [IEEE80211_MODE_11NG] = IEEE80211_CHAN_G, 437170530Ssam}; 438170530Ssam 439170530Ssamstatic void 440178354Ssamadd_channels(struct ieee80211vap *vap, 441170530Ssam struct ieee80211_scan_state *ss, 442170530Ssam enum ieee80211_phymode mode, const uint16_t freq[], int nfreq) 443170530Ssam{ 444170530Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 445178354Ssam struct ieee80211com *ic = vap->iv_ic; 446170530Ssam struct ieee80211_channel *c, *cg; 447170530Ssam u_int modeflags; 448170530Ssam int i; 449170530Ssam 450170530Ssam KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 451170530Ssam modeflags = chanflags[mode]; 452170530Ssam for (i = 0; i < nfreq; i++) { 453170530Ssam if (ss->ss_last >= IEEE80211_SCAN_MAX) 454170530Ssam break; 455170530Ssam 456170530Ssam c = ieee80211_find_channel(ic, freq[i], modeflags); 457178354Ssam if (c == NULL || isexcluded(vap, c)) 458170530Ssam continue; 459170530Ssam if (mode == IEEE80211_MODE_AUTO) { 460170530Ssam /* 461170530Ssam * XXX special-case 11b/g channels so we select 462178354Ssam * the g channel if both are present. 463170530Ssam */ 464178354Ssam if (IEEE80211_IS_CHAN_B(c) && 465178354Ssam (cg = find11gchannel(ic, i, c->ic_freq)) != NULL) 466178354Ssam c = cg; 467170530Ssam } 468170530Ssam ss->ss_chans[ss->ss_last++] = c; 469170530Ssam } 470170530Ssam#undef N 471170530Ssam} 472170530Ssam 473170530Ssamstruct scanlist { 474170530Ssam uint16_t mode; 475170530Ssam uint16_t count; 476170530Ssam const uint16_t *list; 477170530Ssam}; 478170530Ssam 479170530Ssamstatic int 480170530Ssamchecktable(const struct scanlist *scan, const struct ieee80211_channel *c) 481170530Ssam{ 482170530Ssam int i; 483170530Ssam 484170530Ssam for (; scan->list != NULL; scan++) { 485170530Ssam for (i = 0; i < scan->count; i++) 486170530Ssam if (scan->list[i] == c->ic_freq) 487170530Ssam return 1; 488170530Ssam } 489170530Ssam return 0; 490170530Ssam} 491170530Ssam 492189377Ssamstatic int 493189377Ssamonscanlist(const struct ieee80211_scan_state *ss, 494189377Ssam const struct ieee80211_channel *c) 495189377Ssam{ 496189377Ssam int i; 497189377Ssam 498189377Ssam for (i = 0; i < ss->ss_last; i++) 499189377Ssam if (ss->ss_chans[i] == c) 500189377Ssam return 1; 501189377Ssam return 0; 502189377Ssam} 503189377Ssam 504176653Ssamstatic void 505178354Ssamsweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, 506176653Ssam const struct scanlist table[]) 507176653Ssam{ 508178354Ssam struct ieee80211com *ic = vap->iv_ic; 509176653Ssam struct ieee80211_channel *c; 510176653Ssam int i; 511176653Ssam 512176653Ssam for (i = 0; i < ic->ic_nchans; i++) { 513176653Ssam if (ss->ss_last >= IEEE80211_SCAN_MAX) 514176653Ssam break; 515176653Ssam 516176653Ssam c = &ic->ic_channels[i]; 517176653Ssam /* 518176653Ssam * Ignore dynamic turbo channels; we scan them 519176653Ssam * in normal mode (i.e. not boosted). Likewise 520176653Ssam * for HT channels, they get scanned using 521176653Ssam * legacy rates. 522176653Ssam */ 523176653Ssam if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c)) 524176653Ssam continue; 525176653Ssam 526176653Ssam /* 527176653Ssam * If a desired mode was specified, scan only 528176653Ssam * channels that satisfy that constraint. 529176653Ssam */ 530178354Ssam if (vap->iv_des_mode != IEEE80211_MODE_AUTO && 531178354Ssam vap->iv_des_mode != ieee80211_chan2mode(c)) 532176653Ssam continue; 533176653Ssam 534176653Ssam /* 535176653Ssam * Skip channels excluded by user request. 536176653Ssam */ 537178354Ssam if (isexcluded(vap, c)) 538176653Ssam continue; 539176653Ssam 540176653Ssam /* 541176653Ssam * Add the channel unless it is listed in the 542176653Ssam * fixed scan order tables. This insures we 543176653Ssam * don't sweep back in channels we filtered out 544176653Ssam * above. 545176653Ssam */ 546176653Ssam if (checktable(table, c)) 547176653Ssam continue; 548176653Ssam 549176653Ssam /* Add channel to scanning list. */ 550176653Ssam ss->ss_chans[ss->ss_last++] = c; 551176653Ssam } 552189377Ssam /* 553189377Ssam * Explicitly add any desired channel if: 554189377Ssam * - not already on the scan list 555189377Ssam * - allowed by any desired mode constraint 556189377Ssam * - there is space in the scan list 557189377Ssam * This allows the channel to be used when the filtering 558189377Ssam * mechanisms would otherwise elide it (e.g HT, turbo). 559189377Ssam */ 560189377Ssam c = vap->iv_des_chan; 561189377Ssam if (c != IEEE80211_CHAN_ANYC && 562189377Ssam !onscanlist(ss, c) && 563189377Ssam (vap->iv_des_mode == IEEE80211_MODE_AUTO || 564189377Ssam vap->iv_des_mode == ieee80211_chan2mode(c)) && 565189377Ssam ss->ss_last < IEEE80211_SCAN_MAX) 566189377Ssam ss->ss_chans[ss->ss_last++] = c; 567176653Ssam} 568176653Ssam 569178354Ssamstatic void 570178354Ssammakescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, 571178354Ssam const struct scanlist table[]) 572170530Ssam{ 573170530Ssam const struct scanlist *scan; 574170530Ssam enum ieee80211_phymode mode; 575170530Ssam 576170530Ssam ss->ss_last = 0; 577170530Ssam /* 578170530Ssam * Use the table of ordered channels to construct the list 579170530Ssam * of channels for scanning. Any channels in the ordered 580170530Ssam * list not in the master list will be discarded. 581170530Ssam */ 582178354Ssam for (scan = table; scan->list != NULL; scan++) { 583170530Ssam mode = scan->mode; 584178354Ssam if (vap->iv_des_mode != IEEE80211_MODE_AUTO) { 585170530Ssam /* 586170530Ssam * If a desired mode was specified, scan only 587170530Ssam * channels that satisfy that constraint. 588170530Ssam */ 589178354Ssam if (vap->iv_des_mode != mode) { 590170530Ssam /* 591170530Ssam * The scan table marks 2.4Ghz channels as b 592170530Ssam * so if the desired mode is 11g, then use 593170530Ssam * the 11b channel list but upgrade the mode. 594170530Ssam */ 595178354Ssam if (vap->iv_des_mode != IEEE80211_MODE_11G || 596170530Ssam mode != IEEE80211_MODE_11B) 597170530Ssam continue; 598170530Ssam mode = IEEE80211_MODE_11G; /* upgrade */ 599170530Ssam } 600170530Ssam } else { 601170530Ssam /* 602170530Ssam * This lets add_channels upgrade an 11b channel 603170530Ssam * to 11g if available. 604170530Ssam */ 605170530Ssam if (mode == IEEE80211_MODE_11B) 606170530Ssam mode = IEEE80211_MODE_AUTO; 607170530Ssam } 608170530Ssam#ifdef IEEE80211_F_XR 609170530Ssam /* XR does not operate on turbo channels */ 610178354Ssam if ((vap->iv_flags & IEEE80211_F_XR) && 611170530Ssam (mode == IEEE80211_MODE_TURBO_A || 612170530Ssam mode == IEEE80211_MODE_TURBO_G || 613170530Ssam mode == IEEE80211_MODE_STURBO_A)) 614170530Ssam continue; 615170530Ssam#endif 616170530Ssam /* 617170530Ssam * Add the list of the channels; any that are not 618170530Ssam * in the master channel list will be discarded. 619170530Ssam */ 620178354Ssam add_channels(vap, ss, mode, scan->list, scan->count); 621170530Ssam } 622170530Ssam 623170530Ssam /* 624178354Ssam * Add the channels from the ic that are not present 625178354Ssam * in the table. 626170530Ssam */ 627178354Ssam sweepchannels(ss, vap, table); 628178354Ssam} 629170530Ssam 630178354Ssamstatic const uint16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */ 631178354Ssam{ 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 }; 632178354Ssamstatic const uint16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */ 633178354Ssam{ 5170, 5190, 5210, 5230 }; 634178354Ssamstatic const uint16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */ 635178354Ssam{ 2412, 2437, 2462, 2442, 2472 }; 636178354Ssamstatic const uint16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */ 637178354Ssam{ 5745, 5765, 5785, 5805, 5825 }; 638178354Ssamstatic const uint16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */ 639178354Ssam{ 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 }; 640178354Ssamstatic const uint16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */ 641178354Ssam{ 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 }; 642178354Ssamstatic const uint16_t rcl9[] = /* 2.4Ghz ch: 14 */ 643178354Ssam{ 2484 }; 644178354Ssamstatic const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */ 645178354Ssam{ 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 }; 646178354Ssamstatic const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */ 647178354Ssam{ 5040, 5060, 5080, 4920, 4940, 4960, 4980 }; 648178354Ssam#ifdef ATH_TURBO_SCAN 649178354Ssamstatic const uint16_t rcl5[] = /* 3 static turbo channels */ 650178354Ssam{ 5210, 5250, 5290 }; 651178354Ssamstatic const uint16_t rcl6[] = /* 2 static turbo channels */ 652178354Ssam{ 5760, 5800 }; 653178354Ssamstatic const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */ 654178354Ssam{ 5540, 5580, 5620, 5660 }; 655178354Ssamstatic const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */ 656178354Ssam{ 2437 }; 657178354Ssamstatic const uint16_t rcl13[] = /* dynamic Turbo channels */ 658178354Ssam{ 5200, 5240, 5280, 5765, 5805 }; 659178354Ssam#endif /* ATH_TURBO_SCAN */ 660170530Ssam 661178354Ssam#define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a 662170530Ssam 663178354Ssamstatic const struct scanlist staScanTable[] = { 664178354Ssam { IEEE80211_MODE_11B, X(rcl3) }, 665178354Ssam { IEEE80211_MODE_11A, X(rcl1) }, 666178354Ssam { IEEE80211_MODE_11A, X(rcl2) }, 667178354Ssam { IEEE80211_MODE_11B, X(rcl8) }, 668178354Ssam { IEEE80211_MODE_11B, X(rcl9) }, 669178354Ssam { IEEE80211_MODE_11A, X(rcl4) }, 670178354Ssam#ifdef ATH_TURBO_SCAN 671178354Ssam { IEEE80211_MODE_STURBO_A, X(rcl5) }, 672178354Ssam { IEEE80211_MODE_STURBO_A, X(rcl6) }, 673178354Ssam { IEEE80211_MODE_TURBO_A, X(rcl6x) }, 674178354Ssam { IEEE80211_MODE_TURBO_A, X(rcl13) }, 675178354Ssam#endif /* ATH_TURBO_SCAN */ 676178354Ssam { IEEE80211_MODE_11A, X(rcl7) }, 677178354Ssam { IEEE80211_MODE_11B, X(rcl10) }, 678178354Ssam { IEEE80211_MODE_11A, X(rcl11) }, 679178354Ssam#ifdef ATH_TURBO_SCAN 680178354Ssam { IEEE80211_MODE_TURBO_G, X(rcl12) }, 681178354Ssam#endif /* ATH_TURBO_SCAN */ 682178354Ssam { .list = NULL } 683178354Ssam}; 684178354Ssam 685178354Ssam/* 686178354Ssam * Start a station-mode scan by populating the channel list. 687178354Ssam */ 688178354Ssamstatic int 689178354Ssamsta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 690178354Ssam{ 691178354Ssam struct sta_table *st = ss->ss_priv; 692178354Ssam 693178354Ssam makescanlist(ss, vap, staScanTable); 694178354Ssam 695178354Ssam if (ss->ss_mindwell == 0) 696178354Ssam ss->ss_mindwell = msecs_to_ticks(20); /* 20ms */ 697178354Ssam if (ss->ss_maxdwell == 0) 698178354Ssam ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 699178354Ssam 700178354Ssam st->st_scangen++; 701170530Ssam st->st_newscan = 1; 702170530Ssam 703170530Ssam return 0; 704170530Ssam} 705170530Ssam 706170530Ssam/* 707178354Ssam * Restart a scan, typically a bg scan but can 708178354Ssam * also be a fg scan that came up empty. 709170530Ssam */ 710170530Ssamstatic int 711178354Ssamsta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 712170530Ssam{ 713170530Ssam struct sta_table *st = ss->ss_priv; 714170530Ssam 715170530Ssam st->st_newscan = 1; 716170530Ssam return 0; 717170530Ssam} 718170530Ssam 719170530Ssam/* 720170530Ssam * Cancel an ongoing scan. 721170530Ssam */ 722170530Ssamstatic int 723178354Ssamsta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 724170530Ssam{ 725170530Ssam return 0; 726170530Ssam} 727170530Ssam 728178354Ssam/* unalligned little endian access */ 729178354Ssam#define LE_READ_2(p) \ 730178354Ssam ((uint16_t) \ 731178354Ssam ((((const uint8_t *)(p))[0] ) | \ 732178354Ssam (((const uint8_t *)(p))[1] << 8))) 733184302Ssam 734184302Ssam/* 735184302Ssam * Demote any supplied 11g channel to 11b. There should 736184302Ssam * always be an 11b channel but we check anyway... 737184302Ssam */ 738184302Ssamstatic struct ieee80211_channel * 739184302Ssamdemote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan) 740184302Ssam{ 741184302Ssam struct ieee80211_channel *c; 742178354Ssam 743184302Ssam if (IEEE80211_IS_CHAN_ANYG(chan) && 744184302Ssam vap->iv_des_mode == IEEE80211_MODE_AUTO) { 745184302Ssam c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq, 746184302Ssam (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) | 747184302Ssam IEEE80211_CHAN_B); 748184302Ssam if (c != NULL) 749184302Ssam chan = c; 750184302Ssam } 751184302Ssam return chan; 752184302Ssam} 753184302Ssam 754178354Ssamstatic int 755170530Ssammaxrate(const struct ieee80211_scan_entry *se) 756170530Ssam{ 757178354Ssam const struct ieee80211_ie_htcap *htcap = 758178354Ssam (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie; 759178354Ssam int rmax, r, i; 760178354Ssam uint16_t caps; 761170530Ssam 762170530Ssam rmax = 0; 763178354Ssam if (htcap != NULL) { 764178354Ssam /* 765178354Ssam * HT station; inspect supported MCS and then adjust 766178354Ssam * rate by channel width. Could also include short GI 767178354Ssam * in this if we want to be extra accurate. 768178354Ssam */ 769178354Ssam /* XXX assumes MCS15 is max */ 770178354Ssam for (i = 15; i >= 0 && isclr(htcap->hc_mcsset, i); i--) 771178354Ssam ; 772178354Ssam if (i >= 0) { 773178354Ssam caps = LE_READ_2(&htcap->hc_cap); 774178354Ssam /* XXX short/long GI */ 775178354Ssam if (caps & IEEE80211_HTCAP_CHWIDTH40) 776178354Ssam rmax = ieee80211_htrates[i].ht40_rate_400ns; 777178354Ssam else 778178354Ssam rmax = ieee80211_htrates[i].ht40_rate_800ns; 779178354Ssam } 780178354Ssam } 781170530Ssam for (i = 0; i < se->se_rates[1]; i++) { 782170530Ssam r = se->se_rates[2+i] & IEEE80211_RATE_VAL; 783170530Ssam if (r > rmax) 784170530Ssam rmax = r; 785170530Ssam } 786170530Ssam for (i = 0; i < se->se_xrates[1]; i++) { 787170530Ssam r = se->se_xrates[2+i] & IEEE80211_RATE_VAL; 788170530Ssam if (r > rmax) 789170530Ssam rmax = r; 790170530Ssam } 791170530Ssam return rmax; 792170530Ssam} 793170530Ssam 794170530Ssam/* 795170530Ssam * Compare the capabilities of two entries and decide which is 796170530Ssam * more desirable (return >0 if a is considered better). Note 797170530Ssam * that we assume compatibility/usability has already been checked 798170530Ssam * so we don't need to (e.g. validate whether privacy is supported). 799170530Ssam * Used to select the best scan candidate for association in a BSS. 800170530Ssam */ 801170530Ssamstatic int 802170530Ssamsta_compare(const struct sta_entry *a, const struct sta_entry *b) 803170530Ssam{ 804170530Ssam#define PREFER(_a,_b,_what) do { \ 805170530Ssam if (((_a) ^ (_b)) & (_what)) \ 806170530Ssam return ((_a) & (_what)) ? 1 : -1; \ 807170530Ssam} while (0) 808178354Ssam int maxa, maxb; 809170530Ssam int8_t rssia, rssib; 810170530Ssam int weight; 811170530Ssam 812170530Ssam /* privacy support */ 813170530Ssam PREFER(a->base.se_capinfo, b->base.se_capinfo, 814170530Ssam IEEE80211_CAPINFO_PRIVACY); 815170530Ssam 816170530Ssam /* compare count of previous failures */ 817170530Ssam weight = b->se_fails - a->se_fails; 818170530Ssam if (abs(weight) > 1) 819170530Ssam return weight; 820170530Ssam 821170530Ssam /* 822170530Ssam * Compare rssi. If the two are considered equivalent 823170530Ssam * then fallback to other criteria. We threshold the 824170530Ssam * comparisons to avoid selecting an ap purely by rssi 825170530Ssam * when both values may be good but one ap is otherwise 826170530Ssam * more desirable (e.g. an 11b-only ap with stronger 827170530Ssam * signal than an 11g ap). 828170530Ssam */ 829170530Ssam rssia = MIN(a->base.se_rssi, STA_RSSI_MAX); 830170530Ssam rssib = MIN(b->base.se_rssi, STA_RSSI_MAX); 831170530Ssam if (abs(rssib - rssia) < 5) { 832170530Ssam /* best/max rate preferred if signal level close enough XXX */ 833170530Ssam maxa = maxrate(&a->base); 834170530Ssam maxb = maxrate(&b->base); 835170530Ssam if (maxa != maxb) 836170530Ssam return maxa - maxb; 837170530Ssam /* XXX use freq for channel preference */ 838170530Ssam /* for now just prefer 5Ghz band to all other bands */ 839178354Ssam PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan), 840178354Ssam IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1); 841170530Ssam } 842170530Ssam /* all things being equal, use signal level */ 843170530Ssam return a->base.se_rssi - b->base.se_rssi; 844170530Ssam#undef PREFER 845170530Ssam} 846170530Ssam 847170530Ssam/* 848170530Ssam * Check rate set suitability and return the best supported rate. 849178354Ssam * XXX inspect MCS for HT 850170530Ssam */ 851170530Ssamstatic int 852184302Ssamcheck_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan, 853184302Ssam const struct ieee80211_scan_entry *se) 854170530Ssam{ 855170530Ssam#define RV(v) ((v) & IEEE80211_RATE_VAL) 856170530Ssam const struct ieee80211_rateset *srs; 857178354Ssam int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate; 858170530Ssam const uint8_t *rs; 859170530Ssam 860178354Ssam okrate = badrate = 0; 861170530Ssam 862184302Ssam srs = ieee80211_get_suprates(vap->iv_ic, chan); 863170530Ssam nrs = se->se_rates[1]; 864170530Ssam rs = se->se_rates+2; 865178354Ssam /* XXX MCS */ 866184302Ssam ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate; 867170530Ssam fixedrate = IEEE80211_FIXED_RATE_NONE; 868170530Ssamagain: 869170530Ssam for (i = 0; i < nrs; i++) { 870170530Ssam r = RV(rs[i]); 871170530Ssam badrate = r; 872170530Ssam /* 873170530Ssam * Check any fixed rate is included. 874170530Ssam */ 875178354Ssam if (r == ucastrate) 876170530Ssam fixedrate = r; 877170530Ssam /* 878170530Ssam * Check against our supported rates. 879170530Ssam */ 880170530Ssam for (j = 0; j < srs->rs_nrates; j++) 881170530Ssam if (r == RV(srs->rs_rates[j])) { 882170530Ssam if (r > okrate) /* NB: track max */ 883170530Ssam okrate = r; 884170530Ssam break; 885170530Ssam } 886170530Ssam 887170530Ssam if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) { 888170530Ssam /* 889170530Ssam * Don't try joining a BSS, if we don't support 890170530Ssam * one of its basic rates. 891170530Ssam */ 892170530Ssam okrate = 0; 893170530Ssam goto back; 894170530Ssam } 895170530Ssam } 896170530Ssam if (rs == se->se_rates+2) { 897170530Ssam /* scan xrates too; sort of an algol68-style for loop */ 898170530Ssam nrs = se->se_xrates[1]; 899170530Ssam rs = se->se_xrates+2; 900170530Ssam goto again; 901170530Ssam } 902170530Ssam 903170530Ssamback: 904178354Ssam if (okrate == 0 || ucastrate != fixedrate) 905170530Ssam return badrate | IEEE80211_RATE_BASIC; 906170530Ssam else 907170530Ssam return RV(okrate); 908170530Ssam#undef RV 909170530Ssam} 910170530Ssam 911195618Srpaulostatic __inline int 912195618Srpaulomatch_id(const uint8_t *ie, const uint8_t *val, int len) 913195618Srpaulo{ 914195618Srpaulo return (ie[1] == len && memcmp(ie+2, val, len) == 0); 915195618Srpaulo} 916195618Srpaulo 917170530Ssamstatic int 918170530Ssammatch_ssid(const uint8_t *ie, 919170530Ssam int nssid, const struct ieee80211_scan_ssid ssids[]) 920170530Ssam{ 921170530Ssam int i; 922170530Ssam 923170530Ssam for (i = 0; i < nssid; i++) { 924195618Srpaulo if (match_id(ie, ssids[i].ssid, ssids[i].len)) 925170530Ssam return 1; 926170530Ssam } 927170530Ssam return 0; 928170530Ssam} 929170530Ssam 930186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 931186904Ssamstatic int 932186904Ssamtdma_isfull(const struct ieee80211_tdma_param *tdma) 933186904Ssam{ 934186904Ssam int slot, slotcnt; 935186904Ssam 936186904Ssam slotcnt = tdma->tdma_slotcnt; 937186904Ssam for (slot = slotcnt-1; slot >= 0; slot--) 938186904Ssam if (isclr(tdma->tdma_inuse, slot)) 939186904Ssam return 0; 940186904Ssam return 1; 941186904Ssam} 942186904Ssam#endif /* IEEE80211_SUPPORT_TDMA */ 943186904Ssam 944170530Ssam/* 945170530Ssam * Test a scan candidate for suitability/compatibility. 946170530Ssam */ 947170530Ssamstatic int 948178354Ssammatch_bss(struct ieee80211vap *vap, 949170530Ssam const struct ieee80211_scan_state *ss, struct sta_entry *se0, 950170530Ssam int debug) 951170530Ssam{ 952178354Ssam struct ieee80211com *ic = vap->iv_ic; 953170530Ssam struct ieee80211_scan_entry *se = &se0->base; 954178354Ssam uint8_t rate; 955178354Ssam int fail; 956170530Ssam 957170530Ssam fail = 0; 958170530Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan))) 959171409Ssam fail |= MATCH_CHANNEL; 960170530Ssam /* 961170530Ssam * NB: normally the desired mode is used to construct 962170530Ssam * the channel list, but it's possible for the scan 963170530Ssam * cache to include entries for stations outside this 964170530Ssam * list so we check the desired mode here to weed them 965170530Ssam * out. 966170530Ssam */ 967178354Ssam if (vap->iv_des_mode != IEEE80211_MODE_AUTO && 968170530Ssam (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) != 969178354Ssam chanflags[vap->iv_des_mode]) 970171409Ssam fail |= MATCH_CHANNEL; 971178354Ssam if (vap->iv_opmode == IEEE80211_M_IBSS) { 972170530Ssam if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 973171409Ssam fail |= MATCH_CAPINFO; 974186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 975186904Ssam } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 976186904Ssam /* 977186904Ssam * Adhoc demo network setup shouldn't really be scanning 978186904Ssam * but just in case skip stations operating in IBSS or 979186904Ssam * BSS mode. 980186904Ssam */ 981186904Ssam if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) 982186904Ssam fail |= MATCH_CAPINFO; 983186904Ssam /* 984186904Ssam * TDMA operation cannot coexist with a normal 802.11 network; 985186904Ssam * skip if IBSS or ESS capabilities are marked and require 986186904Ssam * the beacon have a TDMA ie present. 987186904Ssam */ 988186904Ssam if (vap->iv_caps & IEEE80211_C_TDMA) { 989186904Ssam const struct ieee80211_tdma_param *tdma = 990186904Ssam (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie; 991189980Ssam const struct ieee80211_tdma_state *ts = vap->iv_tdma; 992186904Ssam 993186904Ssam if (tdma == NULL) 994186904Ssam fail |= MATCH_TDMA_NOIE; 995189980Ssam else if (tdma->tdma_version != ts->tdma_version) 996189980Ssam fail |= MATCH_TDMA_VERSION; 997186904Ssam else if (tdma->tdma_slot != 0) 998186904Ssam fail |= MATCH_TDMA_NOTMASTER; 999186904Ssam else if (tdma_isfull(tdma)) 1000186904Ssam fail |= MATCH_TDMA_NOSLOT; 1001186904Ssam#if 0 1002186904Ssam else if (ieee80211_local_address(se->se_macaddr)) 1003186904Ssam fail |= MATCH_TDMA_LOCAL; 1004186904Ssam#endif 1005186904Ssam } 1006186904Ssam#endif /* IEEE80211_SUPPORT_TDMA */ 1007195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH 1008195618Srpaulo } else if (vap->iv_opmode == IEEE80211_M_MBSS) { 1009195618Srpaulo const struct ieee80211_mesh_state *ms = vap->iv_mesh; 1010195618Srpaulo /* 1011195618Srpaulo * Mesh nodes have IBSS & ESS bits in capinfo turned off 1012195618Srpaulo * and two special ie's that must be present. 1013195618Srpaulo */ 1014195618Srpaulo if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) 1015195618Srpaulo fail |= MATCH_CAPINFO; 1016215699Sbschmidt else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID) 1017195618Srpaulo fail |= MATCH_MESH_NOID; 1018195618Srpaulo else if (ms->ms_idlen != 0 && 1019195618Srpaulo match_id(se->se_meshid, ms->ms_id, ms->ms_idlen)) 1020195618Srpaulo fail |= MATCH_MESHID; 1021196005Ssam#endif 1022170530Ssam } else { 1023170530Ssam if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0) 1024171409Ssam fail |= MATCH_CAPINFO; 1025178354Ssam /* 1026178354Ssam * If 11d is enabled and we're attempting to join a bss 1027178354Ssam * that advertises it's country code then compare our 1028178354Ssam * current settings to what we fetched from the country ie. 1029178354Ssam * If our country code is unspecified or different then do 1030178354Ssam * not attempt to join the bss. We should have already 1031178354Ssam * dispatched an event to user space that identifies the 1032178354Ssam * new country code so our regdomain config should match. 1033178354Ssam */ 1034178354Ssam if ((IEEE80211_IS_CHAN_11D(se->se_chan) || 1035178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) && 1036178354Ssam se->se_cc[0] != 0 && 1037178354Ssam (ic->ic_regdomain.country == CTRY_DEFAULT || 1038178354Ssam !isocmp(se->se_cc, ic->ic_regdomain.isocc))) 1039178354Ssam fail |= MATCH_CC; 1040170530Ssam } 1041178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1042170530Ssam if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 1043171409Ssam fail |= MATCH_PRIVACY; 1044170530Ssam } else { 1045170530Ssam /* XXX does this mean privacy is supported or required? */ 1046170530Ssam if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) 1047171409Ssam fail |= MATCH_PRIVACY; 1048170530Ssam } 1049184302Ssam se0->se_flags &= ~STA_DEMOTE11B; 1050184302Ssam rate = check_rate(vap, se->se_chan, se); 1051184302Ssam if (rate & IEEE80211_RATE_BASIC) { 1052171409Ssam fail |= MATCH_RATE; 1053184302Ssam /* 1054184302Ssam * An 11b-only ap will give a rate mismatch if there is an 1055184302Ssam * OFDM fixed tx rate for 11g. Try downgrading the channel 1056184302Ssam * in the scan list to 11b and retry the rate check. 1057184302Ssam */ 1058184302Ssam if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) { 1059184302Ssam rate = check_rate(vap, demote11b(vap, se->se_chan), se); 1060184302Ssam if ((rate & IEEE80211_RATE_BASIC) == 0) { 1061184302Ssam fail &= ~MATCH_RATE; 1062184302Ssam se0->se_flags |= STA_DEMOTE11B; 1063184302Ssam } 1064184302Ssam } 1065184302Ssam } else if (rate < 2*24) { 1066184302Ssam /* 1067184302Ssam * This is an 11b-only ap. Check the desired mode in 1068184302Ssam * case that needs to be honored (mode 11g filters out 1069184302Ssam * 11b-only ap's). Otherwise force any 11g channel used 1070184302Ssam * in scanning to be demoted. 1071184302Ssam * 1072184302Ssam * NB: we cheat a bit here by looking at the max rate; 1073184302Ssam * we could/should check the rates. 1074184302Ssam */ 1075184302Ssam if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO || 1076184302Ssam vap->iv_des_mode == IEEE80211_MODE_11B)) 1077184302Ssam fail |= MATCH_RATE; 1078184302Ssam else 1079184302Ssam se0->se_flags |= STA_DEMOTE11B; 1080184302Ssam } 1081170530Ssam if (ss->ss_nssid != 0 && 1082171409Ssam !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid)) 1083171409Ssam fail |= MATCH_SSID; 1084178354Ssam if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 1085178354Ssam !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid)) 1086178354Ssam fail |= MATCH_BSSID; 1087170530Ssam if (se0->se_fails >= STA_FAILS_MAX) 1088171409Ssam fail |= MATCH_FAILS; 1089170530Ssam if (se0->se_notseen >= STA_PURGE_SCANS) 1090171409Ssam fail |= MATCH_NOTSEEN; 1091170530Ssam if (se->se_rssi < STA_RSSI_MIN) 1092171409Ssam fail |= MATCH_RSSI; 1093170530Ssam#ifdef IEEE80211_DEBUG 1094178354Ssam if (ieee80211_msg(vap, debug)) { 1095170530Ssam printf(" %c %s", 1096171409Ssam fail & MATCH_FAILS ? '=' : 1097171409Ssam fail & MATCH_NOTSEEN ? '^' : 1098178354Ssam fail & MATCH_CC ? '$' : 1099186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 1100186904Ssam fail & MATCH_TDMA_NOIE ? '&' : 1101189980Ssam fail & MATCH_TDMA_VERSION ? 'v' : 1102189980Ssam fail & MATCH_TDMA_NOTMASTER ? 's' : 1103189980Ssam fail & MATCH_TDMA_NOSLOT ? 'f' : 1104189980Ssam fail & MATCH_TDMA_LOCAL ? 'l' : 1105186904Ssam#endif 1106195618Srpaulo fail & MATCH_MESH_NOID ? 'm' : 1107171409Ssam fail ? '-' : '+', ether_sprintf(se->se_macaddr)); 1108170530Ssam printf(" %s%c", ether_sprintf(se->se_bssid), 1109171409Ssam fail & MATCH_BSSID ? '!' : ' '); 1110170530Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan), 1111171409Ssam fail & MATCH_CHANNEL ? '!' : ' '); 1112171409Ssam printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' '); 1113170530Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 1114171409Ssam fail & MATCH_RATE ? '!' : ' '); 1115170530Ssam printf(" %4s%c", 1116170530Ssam (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 1117189980Ssam (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "", 1118171409Ssam fail & MATCH_CAPINFO ? '!' : ' '); 1119170530Ssam printf(" %3s%c ", 1120170530Ssam (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 1121170530Ssam "wep" : "no", 1122171409Ssam fail & MATCH_PRIVACY ? '!' : ' '); 1123170530Ssam ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]); 1124195618Srpaulo printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : ""); 1125170530Ssam } 1126170530Ssam#endif 1127170530Ssam return fail; 1128170530Ssam} 1129170530Ssam 1130170530Ssamstatic void 1131170530Ssamsta_update_notseen(struct sta_table *st) 1132170530Ssam{ 1133170530Ssam struct sta_entry *se; 1134170530Ssam 1135206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1136170530Ssam TAILQ_FOREACH(se, &st->st_entry, se_list) { 1137170530Ssam /* 1138170530Ssam * If seen the reset and don't bump the count; 1139170530Ssam * otherwise bump the ``not seen'' count. Note 1140170530Ssam * that this insures that stations for which we 1141170530Ssam * see frames while not scanning but not during 1142170530Ssam * this scan will not be penalized. 1143170530Ssam */ 1144170530Ssam if (se->se_seen) 1145170530Ssam se->se_seen = 0; 1146170530Ssam else 1147170530Ssam se->se_notseen++; 1148170530Ssam } 1149206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1150170530Ssam} 1151170530Ssam 1152170530Ssamstatic void 1153170530Ssamsta_dec_fails(struct sta_table *st) 1154170530Ssam{ 1155170530Ssam struct sta_entry *se; 1156170530Ssam 1157206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1158170530Ssam TAILQ_FOREACH(se, &st->st_entry, se_list) 1159170530Ssam if (se->se_fails) 1160170530Ssam se->se_fails--; 1161206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1162170530Ssam} 1163170530Ssam 1164170530Ssamstatic struct sta_entry * 1165178354Ssamselect_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug) 1166170530Ssam{ 1167170530Ssam struct sta_table *st = ss->ss_priv; 1168170530Ssam struct sta_entry *se, *selbs = NULL; 1169170530Ssam 1170178354Ssam IEEE80211_DPRINTF(vap, debug, " %s\n", 1171170530Ssam "macaddr bssid chan rssi rate flag wep essid"); 1172206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1173170530Ssam TAILQ_FOREACH(se, &st->st_entry, se_list) { 1174184275Ssam ieee80211_ies_expand(&se->base.se_ies); 1175178354Ssam if (match_bss(vap, ss, se, debug) == 0) { 1176170530Ssam if (selbs == NULL) 1177170530Ssam selbs = se; 1178170530Ssam else if (sta_compare(se, selbs) > 0) 1179170530Ssam selbs = se; 1180170530Ssam } 1181170530Ssam } 1182206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1183170530Ssam 1184170530Ssam return selbs; 1185170530Ssam} 1186170530Ssam 1187170530Ssam/* 1188170530Ssam * Pick an ap or ibss network to join or find a channel 1189170530Ssam * to use to start an ibss network. 1190170530Ssam */ 1191170530Ssamstatic int 1192178354Ssamsta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1193170530Ssam{ 1194170530Ssam struct sta_table *st = ss->ss_priv; 1195170530Ssam struct sta_entry *selbs; 1196184274Ssam struct ieee80211_channel *chan; 1197170530Ssam 1198178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1199178354Ssam ("wrong mode %u", vap->iv_opmode)); 1200170530Ssam 1201170530Ssam if (st->st_newscan) { 1202170530Ssam sta_update_notseen(st); 1203170530Ssam st->st_newscan = 0; 1204170530Ssam } 1205170530Ssam if (ss->ss_flags & IEEE80211_SCAN_NOPICK) { 1206170530Ssam /* 1207170530Ssam * Manual/background scan, don't select+join the 1208170530Ssam * bss, just return. The scanning framework will 1209170530Ssam * handle notification that this has completed. 1210170530Ssam */ 1211170530Ssam ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1212170530Ssam return 1; 1213170530Ssam } 1214170530Ssam /* 1215170530Ssam * Automatic sequencing; look for a candidate and 1216170530Ssam * if found join the network. 1217170530Ssam */ 1218170530Ssam /* NB: unlocked read should be ok */ 1219170530Ssam if (TAILQ_FIRST(&st->st_entry) == NULL) { 1220178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1221170530Ssam "%s: no scan candidate\n", __func__); 1222178354Ssam if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1223178354Ssam return 0; 1224170530Ssamnotfound: 1225170530Ssam /* 1226170530Ssam * If nothing suitable was found decrement 1227170530Ssam * the failure counts so entries will be 1228170530Ssam * reconsidered the next time around. We 1229170530Ssam * really want to do this only for sta's 1230170530Ssam * where we've previously had some success. 1231170530Ssam */ 1232170530Ssam sta_dec_fails(st); 1233170530Ssam st->st_newscan = 1; 1234170530Ssam return 0; /* restart scan */ 1235170530Ssam } 1236178354Ssam selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN); 1237178354Ssam if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1238178354Ssam return (selbs != NULL); 1239184274Ssam if (selbs == NULL) 1240170530Ssam goto notfound; 1241184274Ssam chan = selbs->base.se_chan; 1242184302Ssam if (selbs->se_flags & STA_DEMOTE11B) 1243184302Ssam chan = demote11b(vap, chan); 1244184274Ssam if (!ieee80211_sta_join(vap, chan, &selbs->base)) 1245184274Ssam goto notfound; 1246170530Ssam return 1; /* terminate scan */ 1247170530Ssam} 1248170530Ssam 1249170530Ssam/* 1250170530Ssam * Lookup an entry in the scan cache. We assume we're 1251170530Ssam * called from the bottom half or such that we don't need 1252170530Ssam * to block the bottom half so that it's safe to return 1253170530Ssam * a reference to an entry w/o holding the lock on the table. 1254170530Ssam */ 1255170530Ssamstatic struct sta_entry * 1256170530Ssamsta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1257170530Ssam{ 1258170530Ssam struct sta_entry *se; 1259170530Ssam int hash = STA_HASH(macaddr); 1260170530Ssam 1261206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1262170530Ssam LIST_FOREACH(se, &st->st_hash[hash], se_hash) 1263170530Ssam if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr)) 1264170530Ssam break; 1265206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1266170530Ssam 1267170530Ssam return se; /* NB: unlocked */ 1268170530Ssam} 1269170530Ssam 1270170530Ssamstatic void 1271178354Ssamsta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1272170530Ssam{ 1273178354Ssam struct ieee80211com *ic = vap->iv_ic; 1274178354Ssam struct ieee80211_node *ni = vap->iv_bss; 1275170530Ssam struct sta_table *st = ss->ss_priv; 1276178354Ssam enum ieee80211_phymode mode; 1277170530Ssam struct sta_entry *se, *selbs; 1278178354Ssam uint8_t roamRate, curRate, ucastRate; 1279170530Ssam int8_t roamRssi, curRssi; 1280170530Ssam 1281170530Ssam se = sta_lookup(st, ni->ni_macaddr); 1282170530Ssam if (se == NULL) { 1283170530Ssam /* XXX something is wrong */ 1284170530Ssam return; 1285170530Ssam } 1286170530Ssam 1287178354Ssam mode = ieee80211_chan2mode(ic->ic_bsschan); 1288178354Ssam roamRate = vap->iv_roamparms[mode].rate; 1289178354Ssam roamRssi = vap->iv_roamparms[mode].rssi; 1290178354Ssam ucastRate = vap->iv_txparms[mode].ucastrate; 1291170530Ssam /* NB: the most up to date rssi is in the node, not the scan cache */ 1292170530Ssam curRssi = ic->ic_node_getrssi(ni); 1293178354Ssam if (ucastRate == IEEE80211_FIXED_RATE_NONE) { 1294178354Ssam curRate = ni->ni_txrate; 1295178354Ssam roamRate &= IEEE80211_RATE_VAL; 1296178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM, 1297170530Ssam "%s: currssi %d currate %u roamrssi %d roamrate %u\n", 1298170530Ssam __func__, curRssi, curRate, roamRssi, roamRate); 1299170530Ssam } else { 1300170530Ssam curRate = roamRate; /* NB: insure compare below fails */ 1301178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM, 1302170530Ssam "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi); 1303170530Ssam } 1304170530Ssam /* 1305170530Ssam * Check if a new ap should be used and switch. 1306170530Ssam * XXX deauth current ap 1307170530Ssam */ 1308170530Ssam if (curRate < roamRate || curRssi < roamRssi) { 1309178354Ssam if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) { 1310170530Ssam /* 1311170530Ssam * Scan cache contents are too old; force a scan now 1312170530Ssam * if possible so we have current state to make a 1313170530Ssam * decision with. We don't kick off a bg scan if 1314170530Ssam * we're using dynamic turbo and boosted or if the 1315170530Ssam * channel is busy. 1316170530Ssam * XXX force immediate switch on scan complete 1317170530Ssam */ 1318170530Ssam if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) && 1319178354Ssam time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)) 1320178354Ssam ieee80211_bg_scan(vap, 0); 1321170530Ssam return; 1322170530Ssam } 1323170530Ssam se->base.se_rssi = curRssi; 1324178354Ssam selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM); 1325170530Ssam if (selbs != NULL && selbs != se) { 1326184274Ssam struct ieee80211_channel *chan; 1327184274Ssam 1328178354Ssam IEEE80211_DPRINTF(vap, 1329170530Ssam IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG, 1330170530Ssam "%s: ROAM: curRate %u, roamRate %u, " 1331170530Ssam "curRssi %d, roamRssi %d\n", __func__, 1332170530Ssam curRate, roamRate, curRssi, roamRssi); 1333184274Ssam 1334184274Ssam chan = selbs->base.se_chan; 1335184302Ssam if (selbs->se_flags & STA_DEMOTE11B) 1336184302Ssam chan = demote11b(vap, chan); 1337184274Ssam (void) ieee80211_sta_join(vap, chan, &selbs->base); 1338170530Ssam } 1339170530Ssam } 1340170530Ssam} 1341170530Ssam 1342170530Ssam/* 1343170530Ssam * Age entries in the scan cache. 1344170530Ssam * XXX also do roaming since it's convenient 1345170530Ssam */ 1346170530Ssamstatic void 1347170530Ssamsta_age(struct ieee80211_scan_state *ss) 1348170530Ssam{ 1349178354Ssam struct ieee80211vap *vap = ss->ss_vap; 1350170530Ssam 1351178354Ssam adhoc_age(ss); 1352170530Ssam /* 1353170530Ssam * If rate control is enabled check periodically to see if 1354170530Ssam * we should roam from our current connection to one that 1355170530Ssam * might be better. This only applies when we're operating 1356170530Ssam * in sta mode and automatic roaming is set. 1357170530Ssam * XXX defer if busy 1358170530Ssam * XXX repeater station 1359170530Ssam * XXX do when !bgscan? 1360170530Ssam */ 1361178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1362178354Ssam ("wrong mode %u", vap->iv_opmode)); 1363178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO && 1364213321Sadrian (vap->iv_flags & IEEE80211_F_BGSCAN) && 1365178354Ssam vap->iv_state >= IEEE80211_S_RUN) 1366170530Ssam /* XXX vap is implicit */ 1367178354Ssam sta_roam_check(ss, vap); 1368170530Ssam} 1369170530Ssam 1370170530Ssam/* 1371170530Ssam * Iterate over the entries in the scan cache, invoking 1372170530Ssam * the callback function on each one. 1373170530Ssam */ 1374170530Ssamstatic void 1375170530Ssamsta_iterate(struct ieee80211_scan_state *ss, 1376170530Ssam ieee80211_scan_iter_func *f, void *arg) 1377170530Ssam{ 1378170530Ssam struct sta_table *st = ss->ss_priv; 1379170530Ssam struct sta_entry *se; 1380170530Ssam u_int gen; 1381170530Ssam 1382170530Ssam mtx_lock(&st->st_scanlock); 1383178354Ssam gen = st->st_scaniter++; 1384170530Ssamrestart: 1385206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1386170530Ssam TAILQ_FOREACH(se, &st->st_entry, se_list) { 1387170530Ssam if (se->se_scangen != gen) { 1388170530Ssam se->se_scangen = gen; 1389170530Ssam /* update public state */ 1390170530Ssam se->base.se_age = ticks - se->se_lastupdate; 1391206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1392170530Ssam (*f)(arg, &se->base); 1393170530Ssam goto restart; 1394170530Ssam } 1395170530Ssam } 1396206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1397170530Ssam 1398170530Ssam mtx_unlock(&st->st_scanlock); 1399170530Ssam} 1400170530Ssam 1401170530Ssamstatic void 1402170530Ssamsta_assoc_fail(struct ieee80211_scan_state *ss, 1403170530Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason) 1404170530Ssam{ 1405170530Ssam struct sta_table *st = ss->ss_priv; 1406170530Ssam struct sta_entry *se; 1407170530Ssam 1408170530Ssam se = sta_lookup(st, macaddr); 1409170530Ssam if (se != NULL) { 1410170530Ssam se->se_fails++; 1411170530Ssam se->se_lastfail = ticks; 1412178354Ssam IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN, 1413170530Ssam macaddr, "%s: reason %u fails %u", 1414170530Ssam __func__, reason, se->se_fails); 1415170530Ssam } 1416170530Ssam} 1417170530Ssam 1418170530Ssamstatic void 1419170530Ssamsta_assoc_success(struct ieee80211_scan_state *ss, 1420170530Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1421170530Ssam{ 1422170530Ssam struct sta_table *st = ss->ss_priv; 1423170530Ssam struct sta_entry *se; 1424170530Ssam 1425170530Ssam se = sta_lookup(st, macaddr); 1426170530Ssam if (se != NULL) { 1427170530Ssam#if 0 1428170530Ssam se->se_fails = 0; 1429178354Ssam IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN, 1430170530Ssam macaddr, "%s: fails %u", 1431170530Ssam __func__, se->se_fails); 1432170530Ssam#endif 1433170530Ssam se->se_lastassoc = ticks; 1434170530Ssam } 1435170530Ssam} 1436170530Ssam 1437170530Ssamstatic const struct ieee80211_scanner sta_default = { 1438170530Ssam .scan_name = "default", 1439170530Ssam .scan_attach = sta_attach, 1440170530Ssam .scan_detach = sta_detach, 1441170530Ssam .scan_start = sta_start, 1442170530Ssam .scan_restart = sta_restart, 1443170530Ssam .scan_cancel = sta_cancel, 1444170530Ssam .scan_end = sta_pick_bss, 1445170530Ssam .scan_flush = sta_flush, 1446170530Ssam .scan_add = sta_add, 1447170530Ssam .scan_age = sta_age, 1448170530Ssam .scan_iterate = sta_iterate, 1449170530Ssam .scan_assoc_fail = sta_assoc_fail, 1450170530Ssam .scan_assoc_success = sta_assoc_success, 1451170530Ssam}; 1452195618SrpauloIEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default); 1453170530Ssam 1454170530Ssam/* 1455170530Ssam * Adhoc mode-specific support. 1456170530Ssam */ 1457170530Ssam 1458170530Ssamstatic const uint16_t adhocWorld[] = /* 36, 40, 44, 48 */ 1459170530Ssam{ 5180, 5200, 5220, 5240 }; 1460170530Ssamstatic const uint16_t adhocFcc3[] = /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */ 1461170530Ssam{ 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 }; 1462170530Ssamstatic const uint16_t adhocMkk[] = /* 34, 38, 42, 46 */ 1463170530Ssam{ 5170, 5190, 5210, 5230 }; 1464170530Ssamstatic const uint16_t adhoc11b[] = /* 10, 11 */ 1465170530Ssam{ 2457, 2462 }; 1466170530Ssam 1467170530Ssamstatic const struct scanlist adhocScanTable[] = { 1468170530Ssam { IEEE80211_MODE_11B, X(adhoc11b) }, 1469170530Ssam { IEEE80211_MODE_11A, X(adhocWorld) }, 1470170530Ssam { IEEE80211_MODE_11A, X(adhocFcc3) }, 1471170530Ssam { IEEE80211_MODE_11B, X(adhocMkk) }, 1472170530Ssam { .list = NULL } 1473170530Ssam}; 1474170530Ssam#undef X 1475170530Ssam 1476170530Ssam/* 1477170530Ssam * Start an adhoc-mode scan by populating the channel list. 1478170530Ssam */ 1479170530Ssamstatic int 1480178354Ssamadhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1481170530Ssam{ 1482170530Ssam struct sta_table *st = ss->ss_priv; 1483170530Ssam 1484178354Ssam makescanlist(ss, vap, adhocScanTable); 1485176653Ssam 1486178354Ssam if (ss->ss_mindwell == 0) 1487178354Ssam ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */ 1488178354Ssam if (ss->ss_maxdwell == 0) 1489178354Ssam ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 1490176653Ssam 1491178354Ssam st->st_scangen++; 1492170530Ssam st->st_newscan = 1; 1493170530Ssam 1494170530Ssam return 0; 1495170530Ssam} 1496170530Ssam 1497170530Ssam/* 1498170530Ssam * Select a channel to start an adhoc network on. 1499170530Ssam * The channel list was populated with appropriate 1500170530Ssam * channels so select one that looks least occupied. 1501170530Ssam */ 1502170530Ssamstatic struct ieee80211_channel * 1503178354Ssamadhoc_pick_channel(struct ieee80211_scan_state *ss, int flags) 1504170530Ssam{ 1505170530Ssam struct sta_table *st = ss->ss_priv; 1506170530Ssam struct sta_entry *se; 1507170530Ssam struct ieee80211_channel *c, *bestchan; 1508170530Ssam int i, bestrssi, maxrssi; 1509170530Ssam 1510170530Ssam bestchan = NULL; 1511170530Ssam bestrssi = -1; 1512170530Ssam 1513206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1514170530Ssam for (i = 0; i < ss->ss_last; i++) { 1515170530Ssam c = ss->ss_chans[i]; 1516178354Ssam /* never consider a channel with radar */ 1517178354Ssam if (IEEE80211_IS_CHAN_RADAR(c)) 1518176653Ssam continue; 1519178354Ssam /* skip channels disallowed by regulatory settings */ 1520178354Ssam if (IEEE80211_IS_CHAN_NOADHOC(c)) 1521178354Ssam continue; 1522178354Ssam /* check channel attributes for band compatibility */ 1523178354Ssam if (flags != 0 && (c->ic_flags & flags) != flags) 1524178354Ssam continue; 1525170530Ssam maxrssi = 0; 1526170530Ssam TAILQ_FOREACH(se, &st->st_entry, se_list) { 1527170530Ssam if (se->base.se_chan != c) 1528170530Ssam continue; 1529170530Ssam if (se->base.se_rssi > maxrssi) 1530170530Ssam maxrssi = se->base.se_rssi; 1531170530Ssam } 1532170530Ssam if (bestchan == NULL || maxrssi < bestrssi) 1533170530Ssam bestchan = c; 1534170530Ssam } 1535206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1536170530Ssam 1537170530Ssam return bestchan; 1538170530Ssam} 1539170530Ssam 1540170530Ssam/* 1541170530Ssam * Pick an ibss network to join or find a channel 1542170530Ssam * to use to start an ibss network. 1543170530Ssam */ 1544170530Ssamstatic int 1545178354Ssamadhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1546170530Ssam{ 1547170530Ssam struct sta_table *st = ss->ss_priv; 1548170530Ssam struct sta_entry *selbs; 1549170530Ssam struct ieee80211_channel *chan; 1550170530Ssam 1551178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_IBSS || 1552195618Srpaulo vap->iv_opmode == IEEE80211_M_AHDEMO || 1553195618Srpaulo vap->iv_opmode == IEEE80211_M_MBSS, 1554178354Ssam ("wrong opmode %u", vap->iv_opmode)); 1555170530Ssam 1556170530Ssam if (st->st_newscan) { 1557170530Ssam sta_update_notseen(st); 1558170530Ssam st->st_newscan = 0; 1559170530Ssam } 1560170530Ssam if (ss->ss_flags & IEEE80211_SCAN_NOPICK) { 1561170530Ssam /* 1562170530Ssam * Manual/background scan, don't select+join the 1563170530Ssam * bss, just return. The scanning framework will 1564170530Ssam * handle notification that this has completed. 1565170530Ssam */ 1566170530Ssam ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1567170530Ssam return 1; 1568170530Ssam } 1569170530Ssam /* 1570170530Ssam * Automatic sequencing; look for a candidate and 1571170530Ssam * if found join the network. 1572170530Ssam */ 1573170530Ssam /* NB: unlocked read should be ok */ 1574170530Ssam if (TAILQ_FIRST(&st->st_entry) == NULL) { 1575178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1576170530Ssam "%s: no scan candidate\n", __func__); 1577178354Ssam if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1578178354Ssam return 0; 1579170530Ssamnotfound: 1580186904Ssam /* NB: never auto-start a tdma network for slot !0 */ 1581186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 1582186904Ssam if (vap->iv_des_nssid && 1583186904Ssam ((vap->iv_caps & IEEE80211_C_TDMA) == 0 || 1584186904Ssam ieee80211_tdma_getslot(vap) == 0)) { 1585186904Ssam#else 1586178354Ssam if (vap->iv_des_nssid) { 1587186904Ssam#endif 1588170530Ssam /* 1589170530Ssam * No existing adhoc network to join and we have 1590170530Ssam * an ssid; start one up. If no channel was 1591170530Ssam * specified, try to select a channel. 1592170530Ssam */ 1593178354Ssam if (vap->iv_des_chan == IEEE80211_CHAN_ANYC || 1594178354Ssam IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) { 1595183421Ssam struct ieee80211com *ic = vap->iv_ic; 1596183421Ssam 1597183421Ssam chan = adhoc_pick_channel(ss, 0); 1598183421Ssam if (chan != NULL) 1599183421Ssam chan = ieee80211_ht_adjust_channel(ic, 1600193655Ssam chan, vap->iv_flags_ht); 1601178354Ssam } else 1602178354Ssam chan = vap->iv_des_chan; 1603170530Ssam if (chan != NULL) { 1604178354Ssam ieee80211_create_ibss(vap, chan); 1605170530Ssam return 1; 1606170530Ssam } 1607170530Ssam } 1608170530Ssam /* 1609170530Ssam * If nothing suitable was found decrement 1610170530Ssam * the failure counts so entries will be 1611170530Ssam * reconsidered the next time around. We 1612170530Ssam * really want to do this only for sta's 1613170530Ssam * where we've previously had some success. 1614170530Ssam */ 1615170530Ssam sta_dec_fails(st); 1616170530Ssam st->st_newscan = 1; 1617170530Ssam return 0; /* restart scan */ 1618170530Ssam } 1619178354Ssam selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN); 1620178354Ssam if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1621178354Ssam return (selbs != NULL); 1622184274Ssam if (selbs == NULL) 1623170530Ssam goto notfound; 1624184274Ssam chan = selbs->base.se_chan; 1625184302Ssam if (selbs->se_flags & STA_DEMOTE11B) 1626184302Ssam chan = demote11b(vap, chan); 1627184274Ssam if (!ieee80211_sta_join(vap, chan, &selbs->base)) 1628184274Ssam goto notfound; 1629170530Ssam return 1; /* terminate scan */ 1630170530Ssam} 1631170530Ssam 1632170530Ssam/* 1633170530Ssam * Age entries in the scan cache. 1634170530Ssam */ 1635170530Ssamstatic void 1636170530Ssamadhoc_age(struct ieee80211_scan_state *ss) 1637170530Ssam{ 1638170530Ssam struct sta_table *st = ss->ss_priv; 1639170530Ssam struct sta_entry *se, *next; 1640170530Ssam 1641206617Srpaulo IEEE80211_SCAN_TABLE_LOCK(st); 1642170530Ssam TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) { 1643170530Ssam if (se->se_notseen > STA_PURGE_SCANS) { 1644170530Ssam TAILQ_REMOVE(&st->st_entry, se, se_list); 1645170530Ssam LIST_REMOVE(se, se_hash); 1646178354Ssam ieee80211_ies_cleanup(&se->base.se_ies); 1647186302Ssam free(se, M_80211_SCAN); 1648170530Ssam } 1649170530Ssam } 1650206617Srpaulo IEEE80211_SCAN_TABLE_UNLOCK(st); 1651170530Ssam} 1652170530Ssam 1653170530Ssamstatic const struct ieee80211_scanner adhoc_default = { 1654170530Ssam .scan_name = "default", 1655170530Ssam .scan_attach = sta_attach, 1656170530Ssam .scan_detach = sta_detach, 1657170530Ssam .scan_start = adhoc_start, 1658170530Ssam .scan_restart = sta_restart, 1659170530Ssam .scan_cancel = sta_cancel, 1660170530Ssam .scan_end = adhoc_pick_bss, 1661170530Ssam .scan_flush = sta_flush, 1662178354Ssam .scan_pickchan = adhoc_pick_channel, 1663170530Ssam .scan_add = sta_add, 1664170530Ssam .scan_age = adhoc_age, 1665170530Ssam .scan_iterate = sta_iterate, 1666170530Ssam .scan_assoc_fail = sta_assoc_fail, 1667170530Ssam .scan_assoc_success = sta_assoc_success, 1668170530Ssam}; 1669195618SrpauloIEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default); 1670195618SrpauloIEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default); 1671170530Ssam 1672178354Ssamstatic void 1673178354Ssamap_force_promisc(struct ieee80211com *ic) 1674178354Ssam{ 1675178354Ssam struct ifnet *ifp = ic->ic_ifp; 1676178354Ssam 1677178354Ssam IEEE80211_LOCK(ic); 1678178354Ssam /* set interface into promiscuous mode */ 1679178354Ssam ifp->if_flags |= IFF_PROMISC; 1680191746Sthompsa ieee80211_runtask(ic, &ic->ic_promisc_task); 1681178354Ssam IEEE80211_UNLOCK(ic); 1682178354Ssam} 1683178354Ssam 1684178354Ssamstatic void 1685178354Ssamap_reset_promisc(struct ieee80211com *ic) 1686178354Ssam{ 1687178354Ssam IEEE80211_LOCK(ic); 1688178354Ssam ieee80211_syncifflag_locked(ic, IFF_PROMISC); 1689178354Ssam IEEE80211_UNLOCK(ic); 1690178354Ssam} 1691178354Ssam 1692178354Ssamstatic int 1693178354Ssamap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1694178354Ssam{ 1695178354Ssam struct sta_table *st = ss->ss_priv; 1696178354Ssam 1697178354Ssam makescanlist(ss, vap, staScanTable); 1698178354Ssam 1699178354Ssam if (ss->ss_mindwell == 0) 1700178354Ssam ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */ 1701178354Ssam if (ss->ss_maxdwell == 0) 1702178354Ssam ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 1703178354Ssam 1704178354Ssam st->st_scangen++; 1705178354Ssam st->st_newscan = 1; 1706178354Ssam 1707178354Ssam ap_force_promisc(vap->iv_ic); 1708178354Ssam return 0; 1709178354Ssam} 1710178354Ssam 1711170530Ssam/* 1712178354Ssam * Cancel an ongoing scan. 1713170530Ssam */ 1714170530Ssamstatic int 1715178354Ssamap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1716170530Ssam{ 1717178354Ssam ap_reset_promisc(vap->iv_ic); 1718178354Ssam return 0; 1719178354Ssam} 1720178354Ssam 1721178354Ssam/* 1722178354Ssam * Pick a quiet channel to use for ap operation. 1723178354Ssam */ 1724178354Ssamstatic struct ieee80211_channel * 1725178354Ssamap_pick_channel(struct ieee80211_scan_state *ss, int flags) 1726178354Ssam{ 1727178354Ssam struct sta_table *st = ss->ss_priv; 1728178354Ssam struct ieee80211_channel *bestchan = NULL; 1729178354Ssam int i; 1730178354Ssam 1731178354Ssam /* XXX select channel more intelligently, e.g. channel spread, power */ 1732178354Ssam /* NB: use scan list order to preserve channel preference */ 1733178354Ssam for (i = 0; i < ss->ss_last; i++) { 1734178354Ssam struct ieee80211_channel *chan = ss->ss_chans[i]; 1735178354Ssam /* 1736178354Ssam * If the channel is unoccupied the max rssi 1737178354Ssam * should be zero; just take it. Otherwise 1738178354Ssam * track the channel with the lowest rssi and 1739178354Ssam * use that when all channels appear occupied. 1740178354Ssam */ 1741178354Ssam if (IEEE80211_IS_CHAN_RADAR(chan)) 1742178354Ssam continue; 1743178354Ssam if (IEEE80211_IS_CHAN_NOHOSTAP(chan)) 1744178354Ssam continue; 1745178354Ssam /* check channel attributes for band compatibility */ 1746178354Ssam if (flags != 0 && (chan->ic_flags & flags) != flags) 1747178354Ssam continue; 1748186107Ssam KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size")); 1749178354Ssam /* XXX channel have interference */ 1750178354Ssam if (st->st_maxrssi[chan->ic_ieee] == 0) { 1751178354Ssam /* XXX use other considerations */ 1752178354Ssam return chan; 1753170530Ssam } 1754178354Ssam if (bestchan == NULL || 1755178354Ssam st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee]) 1756178354Ssam bestchan = chan; 1757178354Ssam } 1758178354Ssam return bestchan; 1759178354Ssam} 1760178354Ssam 1761178354Ssam/* 1762178354Ssam * Pick a quiet channel to use for ap operation. 1763178354Ssam */ 1764178354Ssamstatic int 1765178354Ssamap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1766178354Ssam{ 1767178354Ssam struct ieee80211com *ic = vap->iv_ic; 1768178354Ssam struct ieee80211_channel *bestchan; 1769178354Ssam 1770178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP, 1771178354Ssam ("wrong opmode %u", vap->iv_opmode)); 1772178354Ssam bestchan = ap_pick_channel(ss, 0); 1773178354Ssam if (bestchan == NULL) { 1774178354Ssam /* no suitable channel, should not happen */ 1775178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1776178354Ssam "%s: no suitable channel! (should not happen)\n", __func__); 1777178354Ssam /* XXX print something? */ 1778178354Ssam return 0; /* restart scan */ 1779178354Ssam } 1780178354Ssam /* 1781178354Ssam * If this is a dynamic turbo channel, start with the unboosted one. 1782178354Ssam */ 1783178354Ssam if (IEEE80211_IS_CHAN_TURBO(bestchan)) { 1784178354Ssam bestchan = ieee80211_find_channel(ic, bestchan->ic_freq, 1785178354Ssam bestchan->ic_flags & ~IEEE80211_CHAN_TURBO); 1786178354Ssam if (bestchan == NULL) { 1787178354Ssam /* should never happen ?? */ 1788178354Ssam return 0; 1789170530Ssam } 1790170530Ssam } 1791178354Ssam ap_reset_promisc(ic); 1792178354Ssam if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) { 1793178354Ssam /* 1794178354Ssam * Manual/background scan, don't select+join the 1795178354Ssam * bss, just return. The scanning framework will 1796178354Ssam * handle notification that this has completed. 1797178354Ssam */ 1798178354Ssam ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1799178354Ssam return 1; 1800178354Ssam } 1801178354Ssam ieee80211_create_ibss(vap, 1802193655Ssam ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht)); 1803178354Ssam return 1; 1804170530Ssam} 1805170530Ssam 1806178354Ssamstatic const struct ieee80211_scanner ap_default = { 1807178354Ssam .scan_name = "default", 1808178354Ssam .scan_attach = sta_attach, 1809178354Ssam .scan_detach = sta_detach, 1810178354Ssam .scan_start = ap_start, 1811178354Ssam .scan_restart = sta_restart, 1812178354Ssam .scan_cancel = ap_cancel, 1813178354Ssam .scan_end = ap_end, 1814178354Ssam .scan_flush = sta_flush, 1815178354Ssam .scan_pickchan = ap_pick_channel, 1816178354Ssam .scan_add = sta_add, 1817178354Ssam .scan_age = adhoc_age, 1818178354Ssam .scan_iterate = sta_iterate, 1819178354Ssam .scan_assoc_success = sta_assoc_success, 1820178354Ssam .scan_assoc_fail = sta_assoc_fail, 1821170530Ssam}; 1822195618SrpauloIEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default); 1823178354Ssam 1824195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH 1825178354Ssam/* 1826195618Srpaulo * Pick an mbss network to join or find a channel 1827195618Srpaulo * to use to start an mbss network. 1828178354Ssam */ 1829195618Srpaulostatic int 1830195618Srpaulomesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1831195618Srpaulo{ 1832195618Srpaulo struct sta_table *st = ss->ss_priv; 1833195618Srpaulo struct ieee80211_mesh_state *ms = vap->iv_mesh; 1834195618Srpaulo struct sta_entry *selbs; 1835195618Srpaulo struct ieee80211_channel *chan; 1836195618Srpaulo 1837195618Srpaulo KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, 1838195618Srpaulo ("wrong opmode %u", vap->iv_opmode)); 1839195618Srpaulo 1840195618Srpaulo if (st->st_newscan) { 1841195618Srpaulo sta_update_notseen(st); 1842195618Srpaulo st->st_newscan = 0; 1843195618Srpaulo } 1844195618Srpaulo if (ss->ss_flags & IEEE80211_SCAN_NOPICK) { 1845195618Srpaulo /* 1846195618Srpaulo * Manual/background scan, don't select+join the 1847195618Srpaulo * bss, just return. The scanning framework will 1848195618Srpaulo * handle notification that this has completed. 1849195618Srpaulo */ 1850195618Srpaulo ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1851195618Srpaulo return 1; 1852195618Srpaulo } 1853195618Srpaulo /* 1854195618Srpaulo * Automatic sequencing; look for a candidate and 1855195618Srpaulo * if found join the network. 1856195618Srpaulo */ 1857195618Srpaulo /* NB: unlocked read should be ok */ 1858195618Srpaulo if (TAILQ_FIRST(&st->st_entry) == NULL) { 1859195618Srpaulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1860195618Srpaulo "%s: no scan candidate\n", __func__); 1861195618Srpaulo if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1862195618Srpaulo return 0; 1863195618Srpaulonotfound: 1864195618Srpaulo if (ms->ms_idlen != 0) { 1865195618Srpaulo /* 1866195618Srpaulo * No existing mbss network to join and we have 1867195618Srpaulo * a meshid; start one up. If no channel was 1868195618Srpaulo * specified, try to select a channel. 1869195618Srpaulo */ 1870195618Srpaulo if (vap->iv_des_chan == IEEE80211_CHAN_ANYC || 1871195618Srpaulo IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) { 1872195618Srpaulo struct ieee80211com *ic = vap->iv_ic; 1873195618Srpaulo 1874195618Srpaulo chan = adhoc_pick_channel(ss, 0); 1875195618Srpaulo if (chan != NULL) 1876195618Srpaulo chan = ieee80211_ht_adjust_channel(ic, 1877195618Srpaulo chan, vap->iv_flags_ht); 1878195618Srpaulo } else 1879195618Srpaulo chan = vap->iv_des_chan; 1880195618Srpaulo if (chan != NULL) { 1881195618Srpaulo ieee80211_create_ibss(vap, chan); 1882195618Srpaulo return 1; 1883195618Srpaulo } 1884195618Srpaulo } 1885195618Srpaulo /* 1886195618Srpaulo * If nothing suitable was found decrement 1887195618Srpaulo * the failure counts so entries will be 1888195618Srpaulo * reconsidered the next time around. We 1889195618Srpaulo * really want to do this only for sta's 1890195618Srpaulo * where we've previously had some success. 1891195618Srpaulo */ 1892195618Srpaulo sta_dec_fails(st); 1893195618Srpaulo st->st_newscan = 1; 1894195618Srpaulo return 0; /* restart scan */ 1895195618Srpaulo } 1896195618Srpaulo selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN); 1897195618Srpaulo if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1898195618Srpaulo return (selbs != NULL); 1899195618Srpaulo if (selbs == NULL) 1900195618Srpaulo goto notfound; 1901195618Srpaulo chan = selbs->base.se_chan; 1902195618Srpaulo if (selbs->se_flags & STA_DEMOTE11B) 1903195618Srpaulo chan = demote11b(vap, chan); 1904195618Srpaulo if (!ieee80211_sta_join(vap, chan, &selbs->base)) 1905195618Srpaulo goto notfound; 1906195618Srpaulo return 1; /* terminate scan */ 1907195618Srpaulo} 1908195618Srpaulo 1909195618Srpaulostatic const struct ieee80211_scanner mesh_default = { 1910195618Srpaulo .scan_name = "default", 1911195618Srpaulo .scan_attach = sta_attach, 1912195618Srpaulo .scan_detach = sta_detach, 1913195618Srpaulo .scan_start = adhoc_start, 1914195618Srpaulo .scan_restart = sta_restart, 1915195618Srpaulo .scan_cancel = sta_cancel, 1916195618Srpaulo .scan_end = mesh_pick_bss, 1917195618Srpaulo .scan_flush = sta_flush, 1918195618Srpaulo .scan_pickchan = adhoc_pick_channel, 1919195618Srpaulo .scan_add = sta_add, 1920195618Srpaulo .scan_age = adhoc_age, 1921195618Srpaulo .scan_iterate = sta_iterate, 1922195618Srpaulo .scan_assoc_fail = sta_assoc_fail, 1923195618Srpaulo .scan_assoc_success = sta_assoc_success, 1924195618Srpaulo}; 1925195618SrpauloIEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default); 1926195618Srpaulo#endif /* IEEE80211_SUPPORT_MESH */ 1927