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