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