update madwifi
[linux-2.6/zen-sources.git] / drivers / net / wireless / madwifi / net80211 / ieee80211_scan_sta.c
blob657276749520eb29f13788f6b666752cc44ded98
1 /*-
2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3 * All rights reserved.
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 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") version 2 as published by the Free
18 * Software Foundation.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $Id: ieee80211_scan_sta.c 3733 2008-06-19 02:34:23Z mentor $
33 #ifndef EXPORT_SYMTAB
34 #define EXPORT_SYMTAB
35 #endif
38 * IEEE 802.11 station scanning support.
40 #ifndef AUTOCONF_INCLUDED
41 #include <linux/config.h>
42 #endif
43 #include <linux/version.h>
44 #include <linux/module.h>
45 #include <linux/skbuff.h>
46 #include <linux/netdevice.h>
47 #include <linux/init.h>
48 #include <linux/delay.h>
50 #include "if_media.h"
52 #include <net80211/ieee80211_var.h>
55 * Parameters for managing cache entries:
57 * o a station with STA_FAILS_MAX failures is not considered
58 * when picking a candidate
59 * o a station that hasn't had an update in STA_PURGE_SCANS
60 * (background) scans is discarded
61 * o after STA_FAILS_AGE seconds we clear the failure count
63 #define STA_FAILS_MAX 2 /* assoc failures before ignored */
64 #define STA_FAILS_AGE (2 * 60) /* time before clearing fails (secs) */
65 #define STA_PURGE_SCANS 2 /* age for purging entries (scans) */
67 #define RSSI_LPF_LEN 10
68 #define RSSI_EP_MULTIPLIER (1<<7) /* pow2 to optimize out * and / */
69 #define RSSI_IN(x) ((x) * RSSI_EP_MULTIPLIER)
70 #define LPF_RSSI(x, y, len) (((x) * ((len) - 1) + (y)) / (len))
71 #define RSSI_LPF(x, y) do { \
72 if ((y) >= -20) \
73 x = LPF_RSSI((x), RSSI_IN((y)), RSSI_LPF_LEN); \
74 } while (0)
75 #define EP_RND(x, mul) \
76 ((((x)%(mul)) >= ((mul)/2)) ? howmany(x, mul) : (x)/(mul))
77 #define RSSI_GET(x) EP_RND(x, RSSI_EP_MULTIPLIER)
79 struct sta_entry {
80 struct ieee80211_scan_entry base;
81 TAILQ_ENTRY(sta_entry) se_list;
82 LIST_ENTRY(sta_entry) se_hash;
83 u_int8_t se_fails; /* failure to associate count */
84 u_int8_t se_seen; /* seen during current scan */
85 u_int8_t se_notseen; /* not seen in previous scans */
86 u_int32_t se_avgrssi; /* LPF rssi state */
87 unsigned long se_lastupdate; /* time of last update */
88 unsigned long se_lastfail; /* time of last failure */
89 unsigned long se_lastassoc; /* time of last association */
90 u_int se_scangen; /* iterator scan gen# */
93 #define STA_HASHSIZE 32
94 /* simple hash is enough for variation of macaddr */
95 #define STA_HASH(addr) \
96 (((const u_int8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
98 struct sta_table {
99 spinlock_t st_lock; /* on scan table */
100 TAILQ_HEAD(, sta_entry) st_entry; /* all entries */
101 ATH_LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
102 spinlock_t st_scanlock; /* on st_scangen */
103 u_int st_scangen; /* gen# for iterator */
104 int st_newscan;
105 struct IEEE80211_TQ_STRUCT st_actiontq; /* tasklet for "action" */
106 struct ieee80211_scan_entry st_selbss; /* selected bss for action tasklet */
107 int (*st_action)(struct ieee80211vap *, const struct ieee80211_scan_entry *);
110 #define SCAN_STA_LOCK_INIT(_st, _name) \
111 spin_lock_init(&(_st)->st_lock)
112 #define SCAN_STA_LOCK_DESTROY(_st)
113 #define SCAN_STA_LOCK_IRQ(_st) do { \
114 unsigned long __stlockflags; \
115 spin_lock_irqsave(&(_st)->st_lock, __stlockflags);
116 #define SCAN_STA_UNLOCK_IRQ(_st) \
117 spin_unlock_irqrestore(&(_st)->st_lock, __stlockflags); \
118 } while (0)
119 #define SCAN_STA_UNLOCK_IRQ_EARLY(_st) \
120 spin_unlock_irqrestore(&(_st)->st_lock, __stlockflags);
122 #define SCAN_STA_GEN_LOCK_INIT(_st, _name) \
123 spin_lock_init(&(_st)->st_scanlock)
124 #define SCAN_STA_GEN_LOCK_DESTROY(_st)
125 #define SCAN_STA_GEN_LOCK(_st) spin_lock(&(_st)->st_scanlock);
126 #define SCAN_STA_GEN_UNLOCK(_st) spin_unlock(&(_st)->st_scanlock);
128 static void sta_flush_table(struct sta_table *);
129 static int match_bss(struct ieee80211vap *, const struct ieee80211_scan_state *,
130 const struct sta_entry *);
131 static void action_tasklet(IEEE80211_TQUEUE_ARG);
134 * Attach prior to any scanning work.
136 static int
137 sta_attach(struct ieee80211_scan_state *ss)
139 struct sta_table *st;
141 _MOD_INC_USE(THIS_MODULE, return 0);
143 MALLOC(st, struct sta_table *, sizeof(struct sta_table),
144 M_80211_SCAN, M_NOWAIT | M_ZERO);
145 if (st == NULL)
146 return 0;
147 SCAN_STA_LOCK_INIT(st, "scan_sta");
148 SCAN_STA_GEN_LOCK_INIT(st, "scan_sta_gen");
149 TAILQ_INIT(&st->st_entry);
150 IEEE80211_INIT_TQUEUE(&st->st_actiontq, action_tasklet, ss);
151 ss->ss_priv = st;
152 return 1;
156 * Cleanup any private state.
158 static int
159 sta_detach(struct ieee80211_scan_state *ss)
161 struct sta_table *st = ss->ss_priv;
163 if (st != NULL) {
164 IEEE80211_CANCEL_TQUEUE(&st->st_actiontq);
165 sta_flush_table(st);
166 FREE(st, M_80211_SCAN);
169 _MOD_DEC_USE(THIS_MODULE);
170 return 1;
174 * Flush all per-scan state.
176 static int
177 sta_flush(struct ieee80211_scan_state *ss)
179 struct sta_table *st = ss->ss_priv;
181 SCAN_STA_LOCK_IRQ(st);
182 sta_flush_table(st);
183 SCAN_STA_UNLOCK_IRQ(st);
184 ss->ss_last = 0;
185 return 0;
189 * Flush all entries in the scan cache.
191 static void
192 sta_flush_table(struct sta_table *st)
194 struct sta_entry *se, *next;
196 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
197 TAILQ_REMOVE(&st->st_entry, se, se_list);
198 LIST_REMOVE(se, se_hash);
199 FREE(se, M_80211_SCAN);
203 static void
204 saveie(u_int8_t **iep, const u_int8_t *ie)
206 if (ie == NULL)
207 *iep = NULL;
208 else
209 ieee80211_saveie(iep, ie);
213 * Process a beacon or probe response frame; create an
214 * entry in the scan cache or update any previous entry.
216 static int
217 sta_add(struct ieee80211_scan_state *ss, const struct ieee80211_scanparams *sp,
218 const struct ieee80211_frame *wh, int subtype, int rssi, u_int64_t rtsf)
220 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
221 #define PICK1ST(_ss) \
222 ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
223 IEEE80211_SCAN_PICK1ST)
224 struct sta_table *st = ss->ss_priv;
225 const u_int8_t *macaddr = wh->i_addr2;
226 struct ieee80211vap *vap = ss->ss_vap;
227 struct ieee80211com *ic = vap->iv_ic;
228 struct sta_entry *se;
229 struct ieee80211_scan_entry *ise;
230 int hash;
232 hash = STA_HASH(macaddr);
233 SCAN_STA_LOCK_IRQ(st);
234 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
235 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr) &&
236 sp->ssid[1] == se->base.se_ssid[1] &&
237 !memcmp(se->base.se_ssid+2, sp->ssid+2, se->base.se_ssid[1]))
238 goto found;
240 MALLOC(se, struct sta_entry *, sizeof(struct sta_entry),
241 M_80211_SCAN, M_NOWAIT | M_ZERO);
242 if (se == NULL) {
243 SCAN_STA_UNLOCK_IRQ_EARLY(st);
244 return 0;
246 se->se_scangen = st->st_scangen-1;
247 IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
248 TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
249 LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
251 found:
252 ise = &se->base;
254 /* XXX ap beaconing multiple ssid w/ same bssid */
255 if (sp->ssid[1] != 0 &&
256 (ISPROBE(subtype) || ise->se_ssid[1] == 0))
257 memcpy(ise->se_ssid, sp->ssid, 2 + sp->ssid[1]);
259 memcpy(ise->se_rates, sp->rates,
260 2 + IEEE80211_SANITISE_RATESIZE(sp->rates[1]));
261 if (sp->xrates != NULL) {
262 memcpy(ise->se_xrates, sp->xrates,
263 2 + IEEE80211_SANITISE_RATESIZE(sp->xrates[1]));
264 } else
265 ise->se_xrates[1] = 0;
267 IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
269 * Record rssi data using extended precision LPF filter.
271 if (se->se_lastupdate == 0) /* first sample */
272 se->se_avgrssi = RSSI_IN(rssi);
273 else /* avg w/ previous samples */
274 RSSI_LPF(se->se_avgrssi, rssi);
275 se->base.se_rssi = RSSI_GET(se->se_avgrssi);
276 ise->se_rtsf = rtsf;
277 memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
278 ise->se_intval = sp->bintval;
279 ise->se_capinfo = sp->capinfo;
280 ise->se_chan = ic->ic_curchan;
281 ise->se_fhdwell = sp->fhdwell;
282 ise->se_fhindex = sp->fhindex;
283 ise->se_erp = sp->erp;
284 ise->se_timoff = sp->timoff;
285 if (sp->tim != NULL) {
286 const struct ieee80211_tim_ie *tim =
287 (const struct ieee80211_tim_ie *)sp->tim;
288 ise->se_dtimperiod = tim->tim_period;
290 saveie(&ise->se_wme_ie, sp->wme);
291 saveie(&ise->se_wpa_ie, sp->wpa);
292 saveie(&ise->se_rsn_ie, sp->rsn);
293 saveie(&ise->se_ath_ie, sp->ath);
295 /* clear failure count after STA_FAIL_AGE passes */
296 if (se->se_fails &&
297 time_after(jiffies, se->se_lastfail + (STA_FAILS_AGE * HZ))) {
298 se->se_fails = 0;
299 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
300 "%s: fails %u", __func__, se->se_fails);
303 se->se_lastupdate = jiffies; /* update time */
304 se->se_seen = 1;
305 se->se_notseen = 0;
307 SCAN_STA_UNLOCK_IRQ(st);
310 * If looking for a quick choice and nothing's
311 * been found check here.
313 if (PICK1ST(ss) && match_bss(vap, ss, se) == 0)
314 ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
316 return 1;
317 #undef PICK1ST
318 #undef ISPROBE
321 static struct ieee80211_channel *
322 find11gchannel(struct ieee80211com *ic, int i, int freq)
324 struct ieee80211_channel *c;
325 int j;
328 * The normal ordering in the channel list is b channel
329 * immediately followed by g so optimize the search for
330 * this. We'll still do a full search just in case.
332 for (j = i+1; j < ic->ic_nchans; j++) {
333 c = &ic->ic_channels[j];
334 if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
335 return c;
337 for (j = 0; j < i; j++) {
338 c = &ic->ic_channels[j];
339 if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
340 return c;
342 return NULL;
344 static const u_int chanflags[] = {
345 IEEE80211_CHAN_B, /* IEEE80211_MODE_AUTO */
346 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
347 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
348 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
349 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
350 IEEE80211_CHAN_A, /* IEEE80211_MODE_TURBO_A */ /* for turbo mode look for AP in normal channel */
351 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_TURBO_G */
352 IEEE80211_CHAN_ST, /* IEEE80211_MODE_TURBO_STATIC_A */
355 static void
356 add_channels(struct ieee80211com *ic,
357 struct ieee80211_scan_state *ss,
358 enum ieee80211_phymode mode, const u_int16_t freq[], int nfreq)
360 struct ieee80211_channel *c, *cg;
361 u_int modeflags;
362 int i;
364 KASSERT(mode < ARRAY_SIZE(chanflags), ("Unexpected mode %u", mode));
365 modeflags = chanflags[mode];
366 for (i = 0; i < nfreq; i++) {
367 c = ieee80211_find_channel(ic, freq[i], modeflags);
368 if (c == NULL || isclr(ic->ic_chan_active, c->ic_ieee))
369 continue;
370 if (mode == IEEE80211_MODE_AUTO) {
372 * XXX special-case 11b/g channels so we select
373 * the g channel if both are present.
375 if (IEEE80211_IS_CHAN_B(c) &&
376 (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
377 c = cg;
379 if (ss->ss_last >= IEEE80211_SCAN_MAX)
380 break;
381 ss->ss_chans[ss->ss_last++] = c;
385 static const u_int16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
386 { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
387 static const u_int16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */
388 { 5170, 5190, 5210, 5230 };
389 static const u_int16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */
390 { 2412, 2437, 2462, 2442, 2472 };
391 static const u_int16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */
392 { 5745, 5765, 5785, 5805, 5825 };
393 static const u_int16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
394 { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
395 static const u_int16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
396 { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
397 static const u_int16_t rcl9[] = /* 2.4Ghz ch: 14 */
398 { 2484 };
399 static const u_int16_t rcl10[] = /* Added Korean channels 2312-2372 */
400 { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
401 static const u_int16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */
402 { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
403 #ifdef ATH_TURBO_SCAN
404 static const u_int16_t rcl5[] = /* 3 static turbo channels */
405 { 5210, 5250, 5290 };
406 static const u_int16_t rcl6[] = /* 2 static turbo channels */
407 { 5760, 5800 };
408 static const u_int16_t rcl6x[] = /* 4 FCC3 turbo channels */
409 { 5540, 5580, 5620, 5660 };
410 static const u_int16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */
411 { 2437 };
412 static const u_int16_t rcl13[] = /* dynamic Turbo channels */
413 { 5200, 5240, 5280, 5765, 5805 };
414 #endif /* ATH_TURBO_SCAN */
416 struct scanlist {
417 u_int16_t mode;
418 u_int16_t count;
419 const u_int16_t *list;
422 #define IEEE80211_MODE_TURBO_STATIC_A IEEE80211_MODE_MAX
423 #define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a
425 static const struct scanlist staScanTable[] = {
426 { IEEE80211_MODE_11B, X(rcl3) },
427 { IEEE80211_MODE_11A, X(rcl1) },
428 { IEEE80211_MODE_11A, X(rcl2) },
429 { IEEE80211_MODE_11B, X(rcl8) },
430 { IEEE80211_MODE_11B, X(rcl9) },
431 { IEEE80211_MODE_11A, X(rcl4) },
432 #ifdef ATH_TURBO_SCAN
433 { IEEE80211_MODE_TURBO_STATIC_A, X(rcl5) },
434 { IEEE80211_MODE_TURBO_STATIC_A, X(rcl6) },
435 { IEEE80211_MODE_TURBO_A, X(rcl6x) },
436 { IEEE80211_MODE_TURBO_A, X(rcl13) },
437 #endif /* ATH_TURBO_SCAN */
438 { IEEE80211_MODE_11A, X(rcl7) },
439 { IEEE80211_MODE_11B, X(rcl10) },
440 { IEEE80211_MODE_11A, X(rcl11) },
441 #ifdef ATH_TURBO_SCAN
442 { IEEE80211_MODE_TURBO_G, X(rcl12) },
443 #endif /* ATH_TURBO_SCAN */
444 { .list = NULL }
447 #undef X
449 static int
450 checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
452 int i;
454 for (; scan->list != NULL; scan++) {
455 for (i = 0; i < scan->count; i++)
456 if (scan->list[i] == c->ic_freq)
457 return 1;
459 return 0;
463 * Start a station-mode scan by populating the channel list.
465 static int
466 sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
468 struct ieee80211com *ic = vap->iv_ic;
469 struct sta_table *st = ss->ss_priv;
470 const struct scanlist *scan;
471 enum ieee80211_phymode mode;
472 struct ieee80211_channel *c;
473 int i;
475 ss->ss_last = 0;
477 * Use the table of ordered channels to construct the list
478 * of channels for scanning. Any channels in the ordered
479 * list not in the master list will be discarded.
481 for (scan = staScanTable; scan->list != NULL; scan++) {
482 mode = scan->mode;
483 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
485 * If a desired mode was specified, scan only
486 * channels that satisfy that constraint.
488 if (vap->iv_des_mode != mode) {
490 * The scan table marks 2.4Ghz channels as b
491 * so if the desired mode is 11g, then use
492 * the 11b channel list but upgrade the mode.
494 if (vap->iv_des_mode != IEEE80211_MODE_11G ||
495 mode != IEEE80211_MODE_11B)
496 continue;
497 mode = IEEE80211_MODE_11G; /* upgrade */
499 } else {
501 * This lets ieee80211_scan_add_channels
502 * upgrade an 11b channel to 11g if available.
504 if (mode == IEEE80211_MODE_11B)
505 mode = IEEE80211_MODE_AUTO;
507 /* XR does not operate on turbo channels */
508 if ((vap->iv_flags & IEEE80211_F_XR) &&
509 (mode == IEEE80211_MODE_TURBO_A ||
510 mode == IEEE80211_MODE_TURBO_G))
511 continue;
513 * Add the list of the channels; any that are not
514 * in the master channel list will be discarded.
516 add_channels(ic, ss, mode, scan->list, scan->count);
520 * Add the channels from the ic (from HAL) that are not present
521 * in the staScanTable.
523 for (i = 0; i < ic->ic_nchans; i++) {
524 c = &ic->ic_channels[i];
526 * scan dynamic turbo channels in normal mode.
528 if (IEEE80211_IS_CHAN_DTURBO(c))
529 continue;
530 mode = ieee80211_chan2mode(c);
531 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
533 * If a desired mode was specified, scan only
534 * channels that satisfy that constraint.
536 if (vap->iv_des_mode != mode)
537 continue;
540 if (!checktable(staScanTable, c))
541 ss->ss_chans[ss->ss_last++] = c;
544 ss->ss_next = 0;
545 /* XXX tunables */
547 * The scanner will stay on station for ss_maxdwell ms (using a
548 * timer), collecting responses. ss_maxdwell can adjusted downward
549 * so the station gets back on channel before DTIM occurs. If the
550 * station receives probe responses before ss_mindwell has elapsed, the
551 * timer continues. If it receives probe responses after ss_mindwell
552 * then the timer is cancelled and the next channel is chosen.
553 * Basically, you are going to get the mindwell if you are scanning an
554 * occupied channel in the real world and the maxdwell if it's empty.
556 * This seems somehow wrong to me, as you tend to want to fish where the
557 * fish is bitin'.
559 * I'm bumping mindwell up to 60ms (was 20ms). This gives us a reasonable
560 * chance to find all the APs with active scans, and should pick up
561 * everything within a few passes for passive.
563 ss->ss_mindwell = msecs_to_jiffies(60); /* 60ms */
564 ss->ss_maxdwell = msecs_to_jiffies(200); /* 200ms */
566 #ifdef IEEE80211_DEBUG
567 if (ieee80211_msg_scan(vap)) {
568 printk("%s: scan set ", vap->iv_dev->name);
569 ieee80211_scan_dump_channels(ss);
570 printk(" dwell min %ld max %ld\n",
571 ss->ss_mindwell, ss->ss_maxdwell);
573 #endif /* IEEE80211_DEBUG */
575 st->st_newscan = 1;
577 return 0;
581 * Restart a bg scan.
583 static int
584 sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
586 struct sta_table *st = ss->ss_priv;
588 st->st_newscan = 1;
589 return 0;
593 * Cancel an ongoing scan.
595 static int
596 sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
598 struct sta_table *st = ss->ss_priv;
600 IEEE80211_CANCEL_TQUEUE(&st->st_actiontq);
601 return 0;
604 static u_int8_t
605 maxrate(const struct ieee80211_scan_entry *se)
607 u_int8_t max, r;
608 int i;
610 max = 0;
611 for (i = 0; i < se->se_rates[1]; i++) {
612 r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
613 if (r > max)
614 max = r;
616 for (i = 0; i < se->se_xrates[1]; i++) {
617 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
618 if (r > max)
619 max = r;
621 return max;
625 * Compare the capabilities of two entries and decide which is
626 * more desirable (return >0 if a is considered better). Note
627 * that we assume compatibility/usability has already been checked
628 * so we don't need to (e.g. validate whether privacy is supported).
629 * Used to select the best scan candidate for association in a BSS.
631 static int
632 sta_compare(const struct sta_entry *a, const struct sta_entry *b)
634 u_int8_t maxa, maxb;
635 int weight;
637 /* privacy support preferred */
638 if ((a->base.se_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
639 (b->base.se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
640 return 1;
641 if ((a->base.se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
642 (b->base.se_capinfo & IEEE80211_CAPINFO_PRIVACY))
643 return -1;
645 /* compare count of previous failures */
646 weight = b->se_fails - a->se_fails;
647 if (abs(weight) > 1)
648 return weight;
650 if (abs(b->base.se_rssi - a->base.se_rssi) < 5) {
651 /* best/max rate preferred if signal level close enough XXX */
652 maxa = maxrate(&a->base);
653 maxb = maxrate(&b->base);
654 if (maxa != maxb)
655 return maxa - maxb;
656 /* XXX use freq for channel preference */
657 /* for now just prefer 5Ghz band to all other bands */
658 if (IEEE80211_IS_CHAN_5GHZ(a->base.se_chan) &&
659 !IEEE80211_IS_CHAN_5GHZ(b->base.se_chan))
660 return 1;
661 if (!IEEE80211_IS_CHAN_5GHZ(a->base.se_chan) &&
662 IEEE80211_IS_CHAN_5GHZ(b->base.se_chan))
663 return -1;
665 /* all things being equal, use signal level */
666 return a->base.se_rssi - b->base.se_rssi;
670 * Check rate set suitability and return the best supported rate.
672 static int
673 check_rate(struct ieee80211vap *vap, const struct ieee80211_scan_entry *se)
675 #define RV(v) ((v) & IEEE80211_RATE_VAL)
676 struct ieee80211com *ic = vap->iv_ic;
677 const struct ieee80211_rateset *srs;
678 int i, j, nrs, r, okrate, badrate, fixedrate;
679 const u_int8_t *rs;
681 okrate = badrate = fixedrate = 0;
683 if (IEEE80211_IS_CHAN_HALF(se->se_chan))
684 srs = &ic->ic_sup_half_rates;
685 else if (IEEE80211_IS_CHAN_QUARTER(se->se_chan))
686 srs = &ic->ic_sup_quarter_rates;
687 else
688 srs = &ic->ic_sup_rates[ieee80211_chan2mode(se->se_chan)];
689 nrs = se->se_rates[1];
690 rs = se->se_rates + 2;
691 fixedrate = IEEE80211_FIXED_RATE_NONE;
692 again:
693 for (i = 0; i < nrs; i++) {
694 r = RV(rs[i]);
695 badrate = r;
697 * Check any fixed rate is included.
699 if (r == vap->iv_fixed_rate)
700 fixedrate = r;
702 * Check against our supported rates.
704 for (j = 0; j < srs->rs_nrates; j++)
705 if (r == RV(srs->rs_rates[j])) {
706 if (r > okrate) /* NB: track max */
707 okrate = r;
708 break;
711 if (rs == se->se_rates+2) {
712 /* scan xrates too; sort of an algol68-style for loop */
713 nrs = se->se_xrates[1];
714 rs = se->se_xrates + 2;
715 goto again;
717 if (okrate == 0 || vap->iv_fixed_rate != fixedrate)
718 return badrate | IEEE80211_RATE_BASIC;
719 else
720 return RV(okrate);
721 #undef RV
724 static int
725 match_ssid(const u_int8_t *ie,
726 int nssid, const struct ieee80211_scan_ssid ssids[])
728 int i;
730 for (i = 0; i < nssid; i++) {
731 if (ie[1] == ssids[i].len &&
732 memcmp(ie + 2, ssids[i].ssid, ie[1]) == 0)
733 return 1;
735 return 0;
739 * Test a scan candidate for suitability/compatibility.
741 static int
742 match_bss(struct ieee80211vap *vap,
743 const struct ieee80211_scan_state *ss, const struct sta_entry *se0)
745 struct ieee80211com *ic = vap->iv_ic;
746 const struct ieee80211_scan_entry *se = &se0->base;
747 u_int8_t rate;
748 int fail;
750 fail = 0;
751 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
752 fail |= 0x01;
754 * NB: normally the desired mode is used to construct
755 * the channel list, but it's possible for the scan
756 * cache to include entries for stations outside this
757 * list so we check the desired mode here to weed them
758 * out.
760 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
761 (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
762 chanflags[vap->iv_des_mode])
763 fail |= 0x01;
764 if (vap->iv_opmode == IEEE80211_M_IBSS) {
765 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
766 fail |= 0x02;
767 } else {
768 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
769 fail |= 0x02;
771 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
772 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
773 fail |= 0x04;
774 } else {
775 /* XXX does this mean privacy is supported or required? */
776 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
777 fail |= 0x04;
779 rate = check_rate(vap, se);
780 if (rate & IEEE80211_RATE_BASIC)
781 fail |= 0x08;
782 if (ss->ss_nssid != 0 &&
783 !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
784 fail |= 0x10;
785 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
786 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
787 fail |= 0x20;
788 if (se0->se_fails >= STA_FAILS_MAX)
789 fail |= 0x40;
790 if (se0->se_notseen >= STA_PURGE_SCANS)
791 fail |= 0x80;
792 #ifdef IEEE80211_DEBUG
793 if (ieee80211_msg_is_reported(vap, IEEE80211_MSG_SCAN | IEEE80211_MSG_ROAM)) {
794 printk(" %03x", fail);
795 printk(" %c " MAC_FMT,
796 fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
797 MAC_ADDR(se->se_macaddr));
798 printk(" " MAC_FMT "%c", MAC_ADDR(se->se_bssid),
799 fail & 0x20 ? '!' : ' ');
800 printk(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
801 fail & 0x01 ? '!' : ' ');
802 printk(" %+4d", se->se_rssi);
803 printk(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
804 fail & 0x08 ? '!' : ' ');
805 printk(" %4s%c",
806 (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
807 (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "????",
808 fail & 0x02 ? '!' : ' ');
809 printk(" %3s%c ",
810 (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
811 fail & 0x04 ? '!' : ' ');
812 ieee80211_print_essid(se->se_ssid + 2, se->se_ssid[1]);
813 printk("%s\n", fail & 0x10 ? "!" : "");
815 #endif
816 return fail;
819 static void
820 sta_update_notseen(struct sta_table *st)
822 struct sta_entry *se;
824 SCAN_STA_LOCK_IRQ(st);
825 TAILQ_FOREACH(se, &st->st_entry, se_list) {
827 * If seen then reset and don't bump the count;
828 * otherwise bump the ``not seen'' count. Note
829 * that this ensures that stations for which we
830 * see frames while not scanning but not during
831 * this scan will not be penalized.
833 if (se->se_seen)
834 se->se_seen = 0;
835 else
836 se->se_notseen++;
838 SCAN_STA_UNLOCK_IRQ(st);
841 static void
842 sta_dec_fails(struct sta_table *st)
844 struct sta_entry *se;
846 SCAN_STA_LOCK_IRQ(st);
847 TAILQ_FOREACH(se, &st->st_entry, se_list)
848 if (se->se_fails)
849 se->se_fails--;
850 SCAN_STA_UNLOCK_IRQ(st);
853 static struct sta_entry *
854 select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
856 struct sta_table *st = ss->ss_priv;
857 struct sta_entry *se, *selbs = NULL;
859 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN | IEEE80211_MSG_ROAM, " %s\n",
860 "macaddr bssid chan rssi rate flag wep essid");
861 SCAN_STA_LOCK_IRQ(st);
862 TAILQ_FOREACH(se, &st->st_entry, se_list) {
863 if (match_bss(vap, ss, se) == 0) {
864 if (selbs == NULL)
865 selbs = se;
866 else if (sta_compare(se, selbs) > 0)
867 selbs = se;
870 SCAN_STA_UNLOCK_IRQ(st);
872 return selbs;
876 * Pick an ap or ibss network to join or find a channel
877 * to use to start an ibss network.
879 static int
880 sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
881 int (*action)(struct ieee80211vap *, const struct ieee80211_scan_entry *),
882 u_int32_t flags)
884 struct sta_table *st = ss->ss_priv;
885 struct sta_entry *selbss;
887 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s Checking scan results\n",
888 __func__);
890 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
891 ("wrong mode %u", vap->iv_opmode));
893 if (st->st_newscan) {
894 sta_update_notseen(st);
895 st->st_newscan = 0;
897 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
899 * Manual/background scan, don't select+join the
900 * bss, just return. The scanning framework will
901 * handle notification that this has completed.
903 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
904 return 1;
907 * Automatic sequencing; look for a candidate and
908 * if found join the network.
910 /* NB: unlocked read should be ok */
911 if (TAILQ_FIRST(&st->st_entry) == NULL) {
912 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
913 "%s: no scan candidate\n", __func__);
914 notfound:
916 * If nothing suitable was found decrement
917 * the failure counts so entries will be
918 * reconsidered the next time around. We
919 * really want to do this only for STAs
920 * where we've previously had some success.
922 sta_dec_fails(st);
923 st->st_newscan = 1;
924 return 0; /* restart scan */
926 st->st_action = ss->ss_ops->scan_default;
927 if (action)
928 st->st_action = action;
929 if ((selbss = select_bss(ss, vap)) == NULL) {
930 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
931 "%s: select_bss failed\n", __func__);
932 goto notfound;
934 st->st_selbss = selbss->base;
937 * Must defer action to avoid possible recursive call through 80211
938 * state machine, which would result in recursive locking.
940 IEEE80211_SCHEDULE_TQUEUE(&st->st_actiontq);
942 return 1; /* terminate scan */
947 * Lookup an entry in the scan cache. We assume we're
948 * called from the bottom half or such that we don't need
949 * to block the bottom half so that it's safe to return
950 * a reference to an entry w/o holding the lock on the table.
952 static struct sta_entry *
953 sta_lookup(struct sta_table *st, const u_int8_t macaddr[IEEE80211_ADDR_LEN],
954 struct ieee80211_scan_ssid *essid)
956 struct sta_entry *se;
957 int hash = STA_HASH(macaddr);
959 SCAN_STA_LOCK_IRQ(st);
960 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
961 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr) &&
962 (essid->len == se->base.se_ssid[1] &&
963 !memcmp(se->base.se_ssid+2, essid->ssid,
964 se->base.se_ssid[1])))
965 break;
966 SCAN_STA_UNLOCK_IRQ(st);
968 return se; /* NB: unlocked */
971 static void
972 sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
974 struct ieee80211_node *ni = vap->iv_bss;
975 struct ieee80211com *ic = vap->iv_ic;
976 struct sta_table *st = ss->ss_priv;
977 struct sta_entry *se, *selbs;
978 u_int8_t roamRate, curRate;
979 int8_t roamRssi, curRssi;
981 se = sta_lookup(st, ni->ni_macaddr, ss->ss_ssid);
982 if (se == NULL) {
983 /* XXX something is wrong */
984 return;
987 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan)) {
988 roamRate = vap->iv_roam.rate11g;
989 roamRssi = vap->iv_roam.rssi11g;
990 } else if (IEEE80211_IS_CHAN_B(ic->ic_bsschan)) {
991 roamRate = vap->iv_roam.rate11bOnly;
992 roamRssi = vap->iv_roam.rssi11bOnly;
993 } else {
994 roamRate = vap->iv_roam.rate11a;
995 roamRssi = vap->iv_roam.rssi11a;
997 /* NB: the most up to date rssi is in the node, not the scan cache */
998 curRssi = ic->ic_node_getrssi(ni);
999 if (vap->iv_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1000 curRate = ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL;
1001 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1002 "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1003 __func__, curRssi, curRate, roamRssi, roamRate);
1004 } else {
1005 curRate = roamRate; /* NB: ensure compare below fails */
1006 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1007 "%s: currssi %d roamrssi %d\n",
1008 __func__, curRssi, roamRssi);
1010 if ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1011 time_after(jiffies, ic->ic_lastscan + vap->iv_scanvalid)) {
1013 * Scan cache contents is too old; check about updating it.
1015 if (curRate < roamRate || curRssi < roamRssi) {
1017 * Thresholds exceeded, force a scan now so we
1018 * have current state to make a decision with.
1020 ieee80211_bg_scan(vap);
1021 } else if (time_after(jiffies,
1022 ic->ic_lastdata + vap->iv_bgscanidle)) {
1024 * We're not in need of a new ap, but idle;
1025 * kick off a bg scan to replenish the cache.
1027 ieee80211_bg_scan(vap);
1029 } else {
1031 * Scan cache contents are warm enough to use;
1032 * check if a new ap should be used and switch.
1033 * XXX deauth current ap
1035 if (curRate < roamRate || curRssi < roamRssi) {
1036 se->base.se_rssi = curRssi;
1037 selbs = select_bss(ss, vap);
1038 if (selbs != NULL && selbs != se)
1039 ieee80211_sta_join(vap, &selbs->base);
1045 * Age entries in the scan cache.
1046 * XXX also do roaming since it's convenient
1048 static void
1049 sta_age(struct ieee80211_scan_state *ss)
1051 struct ieee80211vap *vap = ss->ss_vap;
1052 struct sta_table *st = ss->ss_priv;
1053 struct sta_entry *se, *next;
1055 SCAN_STA_LOCK_IRQ(st);
1056 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1057 if (se->se_notseen > STA_PURGE_SCANS) {
1058 TAILQ_REMOVE(&st->st_entry, se, se_list);
1059 LIST_REMOVE(se, se_hash);
1060 FREE(se, M_80211_SCAN);
1063 SCAN_STA_UNLOCK_IRQ(st);
1065 * If rate control is enabled check periodically to see if
1066 * we should roam from our current connection to one that
1067 * might be better. This only applies when we're operating
1068 * in sta mode and automatic roaming is set.
1069 * XXX defer if busy
1070 * XXX repeater station
1072 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1073 ("wrong mode %u", vap->iv_opmode));
1074 if (vap->iv_opmode == IEEE80211_M_STA &&
1075 vap->iv_ic->ic_roaming == IEEE80211_ROAMING_AUTO &&
1076 vap->iv_state >= IEEE80211_S_RUN)
1077 /* XXX vap is implicit */
1078 sta_roam_check(ss, vap);
1082 * Iterate over the entries in the scan cache, invoking
1083 * the callback function on each one.
1085 static int
1086 sta_iterate(struct ieee80211_scan_state *ss,
1087 ieee80211_scan_iter_func *f, void *arg)
1089 struct sta_table *st = ss->ss_priv;
1090 struct sta_entry *se;
1091 u_int gen;
1092 int res = 0;
1094 SCAN_STA_GEN_LOCK(st);
1095 gen = st->st_scangen++;
1096 restart:
1097 SCAN_STA_LOCK_IRQ(st);
1098 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1099 if (se->se_scangen != gen) {
1100 se->se_scangen = gen;
1101 /* update public state */
1102 se->base.se_age = jiffies - se->se_lastupdate;
1103 SCAN_STA_UNLOCK_IRQ_EARLY(st);
1105 res = (*f)(arg, &se->base);
1107 if (res != 0)
1108 /* We probably ran out of buffer space. */
1109 goto done;
1111 goto restart;
1115 SCAN_STA_UNLOCK_IRQ(st);
1117 done:
1118 SCAN_STA_GEN_UNLOCK(st);
1120 return res;
1123 static void
1124 sta_assoc_fail(struct ieee80211_scan_state *ss,
1125 const u_int8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1127 struct sta_table *st = ss->ss_priv;
1128 struct sta_entry *se;
1130 if (ss->ss_vap->iv_ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
1131 return;
1133 se = sta_lookup(st, macaddr, ss->ss_ssid);
1134 if (se != NULL) {
1135 se->se_fails++;
1136 se->se_lastfail = jiffies;
1137 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1138 macaddr, "%s: reason %u fails %u",
1139 __func__, reason, se->se_fails);
1143 static void
1144 sta_assoc_success(struct ieee80211_scan_state *ss,
1145 const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1147 struct sta_table *st = ss->ss_priv;
1148 struct sta_entry *se;
1150 se = sta_lookup(st, macaddr, ss->ss_ssid);
1151 if (se != NULL) {
1152 #if 0
1153 se->se_fails = 0;
1154 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1155 macaddr, "%s: fails %u", __func__, se->se_fails);
1156 #endif
1157 se->se_lastassoc = jiffies;
1161 static const struct ieee80211_scanner sta_default = {
1162 .scan_name = "default",
1163 .scan_attach = sta_attach,
1164 .scan_detach = sta_detach,
1165 .scan_start = sta_start,
1166 .scan_restart = sta_restart,
1167 .scan_cancel = sta_cancel,
1168 .scan_end = sta_pick_bss,
1169 .scan_flush = sta_flush,
1170 .scan_add = sta_add,
1171 .scan_age = sta_age,
1172 .scan_iterate = sta_iterate,
1173 .scan_assoc_fail = sta_assoc_fail,
1174 .scan_assoc_success = sta_assoc_success,
1175 .scan_default = ieee80211_sta_join,
1179 * Start an adhoc-mode scan by populating the channel list.
1181 static int
1182 adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1184 struct ieee80211com *ic = vap->iv_ic;
1185 struct sta_table *st = ss->ss_priv;
1186 const struct scanlist *scan;
1187 enum ieee80211_phymode mode;
1189 ss->ss_last = 0;
1191 * Use the table of ordered channels to construct the list
1192 * of channels for scanning. Any channels in the ordered
1193 * list not in the master list will be discarded.
1195 for (scan = staScanTable; scan->list != NULL; scan++) {
1196 mode = scan->mode;
1197 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
1199 * If a desired mode was specified, scan only
1200 * channels that satisfy that constraint.
1202 if (vap->iv_des_mode != mode) {
1204 * The scan table marks 2.4Ghz channels as b
1205 * so if the desired mode is 11g, then use
1206 * the 11b channel list but upgrade the mode.
1208 if (vap->iv_des_mode != IEEE80211_MODE_11G ||
1209 mode != IEEE80211_MODE_11B)
1210 continue;
1211 mode = IEEE80211_MODE_11G; /* upgrade */
1213 } else {
1215 * This lets ieee80211_scan_add_channels
1216 * upgrade an 11b channel to 11g if available.
1218 if (mode == IEEE80211_MODE_11B)
1219 mode = IEEE80211_MODE_AUTO;
1221 /* XR does not operate on turbo channels */
1222 if ((vap->iv_flags & IEEE80211_F_XR) &&
1223 (mode == IEEE80211_MODE_TURBO_A ||
1224 mode == IEEE80211_MODE_TURBO_G))
1225 continue;
1227 * Add the list of the channels; any that are not
1228 * in the master channel list will be discarded.
1230 add_channels(ic, ss, mode, scan->list, scan->count);
1232 ss->ss_next = 0;
1233 /* XXX tunables */
1234 ss->ss_mindwell = msecs_to_jiffies(200); /* 200ms */
1235 ss->ss_maxdwell = msecs_to_jiffies(200); /* 200ms */
1237 #ifdef IEEE80211_DEBUG
1238 if (ieee80211_msg_scan(vap)) {
1239 printk("%s: scan set ", vap->iv_dev->name);
1240 ieee80211_scan_dump_channels(ss);
1241 printk(" dwell min %ld max %ld\n",
1242 ss->ss_mindwell, ss->ss_maxdwell);
1244 #endif /* IEEE80211_DEBUG */
1246 st->st_newscan = 1;
1248 return 0;
1252 * Select a channel to start an adhoc network on.
1253 * The channel list was populated with appropriate
1254 * channels so select one that looks least occupied.
1255 * XXX need regulatory domain constraints
1257 static struct ieee80211_channel *
1258 adhoc_pick_channel(struct ieee80211_scan_state *ss)
1260 struct sta_table *st = ss->ss_priv;
1261 struct sta_entry *se;
1262 struct ieee80211_channel *c, *bestchan;
1263 int i, bestrssi, maxrssi;
1265 bestchan = NULL;
1266 bestrssi = -1;
1268 SCAN_STA_LOCK_IRQ(st);
1269 for (i = 0; i < ss->ss_last; i++) {
1270 c = ss->ss_chans[i];
1271 maxrssi = 0;
1272 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1273 if (se->base.se_chan != c)
1274 continue;
1275 if (se->base.se_rssi > maxrssi)
1276 maxrssi = se->base.se_rssi;
1278 if (bestchan == NULL || maxrssi < bestrssi)
1279 bestchan = c;
1281 SCAN_STA_UNLOCK_IRQ(st);
1283 return bestchan;
1287 * Pick an ibss network to join or find a channel
1288 * to use to start an ibss network.
1290 static int
1291 adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
1292 int (*action)(struct ieee80211vap *, const struct ieee80211_scan_entry *),
1293 u_int32_t flags)
1295 struct sta_table *st = ss->ss_priv;
1296 struct sta_entry *selbs;
1297 struct ieee80211_channel *chan;
1299 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1300 vap->iv_opmode == IEEE80211_M_AHDEMO,
1301 ("wrong opmode %u", vap->iv_opmode));
1303 if (st->st_newscan) {
1304 sta_update_notseen(st);
1305 st->st_newscan = 0;
1307 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1309 * Manual/background scan, don't select+join the
1310 * bss, just return. The scanning framework will
1311 * handle notification that this has completed.
1313 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1314 return 1;
1317 st->st_action = ss->ss_ops->scan_default;
1318 if (action)
1319 st->st_action = action;
1322 * Automatic sequencing; look for a candidate and
1323 * if found join the network.
1325 /* NB: unlocked read should be ok */
1326 if (TAILQ_FIRST(&st->st_entry) == NULL ||
1327 (selbs = select_bss(ss, vap)) == NULL) {
1328 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1329 "%s: no scan candidate\n", __func__);
1330 if (vap->iv_des_nssid) {
1332 * No existing adhoc network to join and we have
1333 * an ssid; start one up. If no channel was
1334 * specified, try to select a channel.
1336 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC)
1337 chan = adhoc_pick_channel(ss);
1338 else
1339 chan = vap->iv_des_chan;
1340 if (chan != NULL) {
1341 struct ieee80211_scan_entry se;
1343 memset(&se, 0, sizeof(se));
1344 se.se_chan = chan;
1345 st->st_selbss = se;
1346 /* defer action */
1347 IEEE80211_SCHEDULE_TQUEUE(&st->st_actiontq);
1348 return 1;
1352 * If nothing suitable was found decrement
1353 * the failure counts so entries will be
1354 * reconsidered the next time around. We
1355 * really want to do this only for STAs
1356 * where we've previously had some success.
1358 sta_dec_fails(st);
1359 st->st_newscan = 1;
1360 return 0; /* restart scan */
1364 * Must defer action to avoid possible recursive call through 80211
1365 * state machine, which would result in recursive locking.
1367 st->st_selbss = selbs->base;
1368 IEEE80211_SCHEDULE_TQUEUE(&st->st_actiontq);
1370 return 1; /* terminate scan */
1374 * Age entries in the scan cache.
1376 static void
1377 adhoc_age(struct ieee80211_scan_state *ss)
1379 struct sta_table *st = ss->ss_priv;
1380 struct sta_entry *se, *next;
1382 SCAN_STA_LOCK_IRQ(st);
1383 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1384 if (se->se_notseen > STA_PURGE_SCANS) {
1385 TAILQ_REMOVE(&st->st_entry, se, se_list);
1386 LIST_REMOVE(se, se_hash);
1387 FREE(se, M_80211_SCAN);
1390 SCAN_STA_UNLOCK_IRQ(st);
1394 * Default action to execute when a scan entry is found for adhoc
1395 * mode. Return 1 on success, 0 on failure
1397 static int
1398 adhoc_default_action(struct ieee80211vap *vap,
1399 const struct ieee80211_scan_entry *se)
1401 u_int8_t zeroMacAddr[IEEE80211_ADDR_LEN];
1403 memset(&zeroMacAddr, 0, IEEE80211_ADDR_LEN);
1404 if (IEEE80211_ADDR_EQ(se->se_bssid, &zeroMacAddr[0])) {
1405 ieee80211_create_ibss(vap, se->se_chan);
1406 return 1;
1407 } else
1408 return ieee80211_sta_join(vap, se);
1411 static const struct ieee80211_scanner adhoc_default = {
1412 .scan_name = "default",
1413 .scan_attach = sta_attach,
1414 .scan_detach = sta_detach,
1415 .scan_start = adhoc_start,
1416 .scan_restart = sta_restart,
1417 .scan_cancel = sta_cancel,
1418 .scan_end = adhoc_pick_bss,
1419 .scan_flush = sta_flush,
1420 .scan_add = sta_add,
1421 .scan_age = adhoc_age,
1422 .scan_iterate = sta_iterate,
1423 .scan_assoc_fail = sta_assoc_fail,
1424 .scan_assoc_success = sta_assoc_success,
1425 .scan_default = adhoc_default_action,
1428 static void
1429 action_tasklet(IEEE80211_TQUEUE_ARG data)
1431 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *)data;
1432 struct sta_table *st = (struct sta_table *)ss->ss_priv;
1433 struct ieee80211vap *vap = ss->ss_vap;
1434 struct ieee80211_channel *chan;
1436 if ((*ss->ss_ops->scan_default)(vap, &st->st_selbss))
1437 return;
1439 switch (vap->iv_opmode) {
1440 case IEEE80211_M_STA:
1441 sta_dec_fails(st);
1442 st->st_newscan = 1;
1443 break;
1444 default:
1445 /* ADHOC */
1446 if (vap->iv_des_nssid) {
1448 * No existing adhoc network to join and we have
1449 * an ssid; start one up. If no channel was
1450 * specified, try to select a channel.
1452 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC)
1453 chan = adhoc_pick_channel(ss);
1454 else
1455 chan = vap->iv_des_chan;
1456 if (chan != NULL) {
1457 struct ieee80211_scan_entry se;
1459 memset(&se, 0, sizeof(se));
1460 se.se_chan = chan;
1461 if ((*ss->ss_ops->scan_default)(vap, &se))
1462 return;
1466 * If nothing suitable was found decrement
1467 * the failure counts so entries will be
1468 * reconsidered the next time around. We
1469 * really want to do this only for STAs
1470 * where we've previously had some success.
1472 sta_dec_fails(st);
1473 st->st_newscan = 1;
1474 break;
1478 * restart scan
1481 /* no ap, clear the flag for a new scan */
1482 vap->iv_ic->ic_flags &= ~IEEE80211_F_SCAN;
1483 if ((ss->ss_flags & IEEE80211_SCAN_USECACHE) == 0)
1484 ieee80211_start_scan(vap, ss->ss_flags, ss->ss_duration, ss->ss_nssid, ss->ss_ssid);
1488 * Module glue.
1490 MODULE_AUTHOR("Errno Consulting, Sam Leffler");
1491 MODULE_DESCRIPTION("802.11 wireless support: default station scanner");
1492 #ifdef MODULE_LICENSE
1493 MODULE_LICENSE("Dual BSD/GPL");
1494 #endif
1496 static int __init
1497 init_scanner_sta(void)
1499 ieee80211_scanner_register(IEEE80211_M_STA, &sta_default);
1500 ieee80211_scanner_register(IEEE80211_M_IBSS, &adhoc_default);
1501 ieee80211_scanner_register(IEEE80211_M_AHDEMO, &adhoc_default);
1502 return 0;
1504 module_init(init_scanner_sta);
1506 static void __exit
1507 exit_scanner_sta(void)
1509 ieee80211_scanner_unregister_all(&sta_default);
1510 ieee80211_scanner_unregister_all(&adhoc_default);
1512 module_exit(exit_scanner_sta);