2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 $
38 * IEEE 802.11 station scanning support.
40 #ifndef AUTOCONF_INCLUDED
41 #include <linux/config.h>
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>
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 { \
73 x = LPF_RSSI((x), RSSI_IN((y)), RSSI_LPF_LEN); \
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)
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)
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 */
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); \
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.
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
);
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
);
156 * Cleanup any private state.
159 sta_detach(struct ieee80211_scan_state
*ss
)
161 struct sta_table
*st
= ss
->ss_priv
;
164 IEEE80211_CANCEL_TQUEUE(&st
->st_actiontq
);
166 FREE(st
, M_80211_SCAN
);
169 _MOD_DEC_USE(THIS_MODULE
);
174 * Flush all per-scan state.
177 sta_flush(struct ieee80211_scan_state
*ss
)
179 struct sta_table
*st
= ss
->ss_priv
;
181 SCAN_STA_LOCK_IRQ(st
);
183 SCAN_STA_UNLOCK_IRQ(st
);
189 * Flush all entries in the scan cache.
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
);
204 saveie(u_int8_t
**iep
, const u_int8_t
*ie
)
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.
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
;
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]))
240 MALLOC(se
, struct sta_entry
*, sizeof(struct sta_entry
),
241 M_80211_SCAN
, M_NOWAIT
| M_ZERO
);
243 SCAN_STA_UNLOCK_IRQ_EARLY(st
);
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
);
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]));
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
);
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 */
297 time_after(jiffies
, se
->se_lastfail
+ (STA_FAILS_AGE
* HZ
))) {
299 IEEE80211_NOTE_MAC(vap
, IEEE80211_MSG_SCAN
, macaddr
,
300 "%s: fails %u", __func__
, se
->se_fails
);
303 se
->se_lastupdate
= jiffies
; /* update time */
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
;
321 static struct ieee80211_channel
*
322 find11gchannel(struct ieee80211com
*ic
, int i
, int freq
)
324 struct ieee80211_channel
*c
;
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
))
337 for (j
= 0; j
< i
; j
++) {
338 c
= &ic
->ic_channels
[j
];
339 if (c
->ic_freq
== freq
&& IEEE80211_IS_CHAN_ANYG(c
))
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 */
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
;
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
))
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
)
379 if (ss
->ss_last
>= IEEE80211_SCAN_MAX
)
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 */
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 */
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 */
412 static const u_int16_t rcl13
[] = /* dynamic Turbo channels */
413 { 5200, 5240, 5280, 5765, 5805 };
414 #endif /* ATH_TURBO_SCAN */
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 */
450 checktable(const struct scanlist
*scan
, const struct ieee80211_channel
*c
)
454 for (; scan
->list
!= NULL
; scan
++) {
455 for (i
= 0; i
< scan
->count
; i
++)
456 if (scan
->list
[i
] == c
->ic_freq
)
463 * Start a station-mode scan by populating the channel list.
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
;
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
++) {
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
)
497 mode
= IEEE80211_MODE_11G
; /* upgrade */
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
))
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
))
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
)
540 if (!checktable(staScanTable
, c
))
541 ss
->ss_chans
[ss
->ss_last
++] = c
;
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
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 */
584 sta_restart(struct ieee80211_scan_state
*ss
, struct ieee80211vap
*vap
)
586 struct sta_table
*st
= ss
->ss_priv
;
593 * Cancel an ongoing scan.
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
);
605 maxrate(const struct ieee80211_scan_entry
*se
)
611 for (i
= 0; i
< se
->se_rates
[1]; i
++) {
612 r
= se
->se_rates
[2+i
] & IEEE80211_RATE_VAL
;
616 for (i
= 0; i
< se
->se_xrates
[1]; i
++) {
617 r
= se
->se_xrates
[2+i
] & IEEE80211_RATE_VAL
;
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.
632 sta_compare(const struct sta_entry
*a
, const struct sta_entry
*b
)
637 /* privacy support preferred */
638 if ((a
->base
.se_capinfo
& IEEE80211_CAPINFO_PRIVACY
) &&
639 (b
->base
.se_capinfo
& IEEE80211_CAPINFO_PRIVACY
) == 0)
641 if ((a
->base
.se_capinfo
& IEEE80211_CAPINFO_PRIVACY
) == 0 &&
642 (b
->base
.se_capinfo
& IEEE80211_CAPINFO_PRIVACY
))
645 /* compare count of previous failures */
646 weight
= b
->se_fails
- a
->se_fails
;
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
);
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
))
661 if (!IEEE80211_IS_CHAN_5GHZ(a
->base
.se_chan
) &&
662 IEEE80211_IS_CHAN_5GHZ(b
->base
.se_chan
))
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.
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
;
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
;
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
;
693 for (i
= 0; i
< nrs
; i
++) {
697 * Check any fixed rate is included.
699 if (r
== vap
->iv_fixed_rate
)
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 */
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;
717 if (okrate
== 0 || vap
->iv_fixed_rate
!= fixedrate
)
718 return badrate
| IEEE80211_RATE_BASIC
;
725 match_ssid(const u_int8_t
*ie
,
726 int nssid
, const struct ieee80211_scan_ssid ssids
[])
730 for (i
= 0; i
< nssid
; i
++) {
731 if (ie
[1] == ssids
[i
].len
&&
732 memcmp(ie
+ 2, ssids
[i
].ssid
, ie
[1]) == 0)
739 * Test a scan candidate for suitability/compatibility.
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
;
751 if (isclr(ic
->ic_chan_active
, ieee80211_chan2ieee(ic
, se
->se_chan
)))
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
760 if (vap
->iv_des_mode
!= IEEE80211_MODE_AUTO
&&
761 (se
->se_chan
->ic_flags
& IEEE80211_CHAN_ALLTURBO
) !=
762 chanflags
[vap
->iv_des_mode
])
764 if (vap
->iv_opmode
== IEEE80211_M_IBSS
) {
765 if ((se
->se_capinfo
& IEEE80211_CAPINFO_IBSS
) == 0)
768 if ((se
->se_capinfo
& IEEE80211_CAPINFO_ESS
) == 0)
771 if (vap
->iv_flags
& IEEE80211_F_PRIVACY
) {
772 if ((se
->se_capinfo
& IEEE80211_CAPINFO_PRIVACY
) == 0)
775 /* XXX does this mean privacy is supported or required? */
776 if (se
->se_capinfo
& IEEE80211_CAPINFO_PRIVACY
)
779 rate
= check_rate(vap
, se
);
780 if (rate
& IEEE80211_RATE_BASIC
)
782 if (ss
->ss_nssid
!= 0 &&
783 !match_ssid(se
->se_ssid
, ss
->ss_nssid
, ss
->ss_ssid
))
785 if ((vap
->iv_flags
& IEEE80211_F_DESBSSID
) &&
786 !IEEE80211_ADDR_EQ(vap
->iv_des_bssid
, se
->se_bssid
))
788 if (se0
->se_fails
>= STA_FAILS_MAX
)
790 if (se0
->se_notseen
>= STA_PURGE_SCANS
)
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 ? '!' : ' ');
806 (se
->se_capinfo
& IEEE80211_CAPINFO_ESS
) ? "ess" :
807 (se
->se_capinfo
& IEEE80211_CAPINFO_IBSS
) ? "ibss" : "????",
808 fail
& 0x02 ? '!' : ' ');
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 ? "!" : "");
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.
838 SCAN_STA_UNLOCK_IRQ(st
);
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
)
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) {
866 else if (sta_compare(se
, selbs
) > 0)
870 SCAN_STA_UNLOCK_IRQ(st
);
876 * Pick an ap or ibss network to join or find a channel
877 * to use to start an ibss network.
880 sta_pick_bss(struct ieee80211_scan_state
*ss
, struct ieee80211vap
*vap
,
881 int (*action
)(struct ieee80211vap
*, const struct ieee80211_scan_entry
*),
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",
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
);
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
;
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__
);
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.
924 return 0; /* restart scan */
926 st
->st_action
= ss
->ss_ops
->scan_default
;
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__
);
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])))
966 SCAN_STA_UNLOCK_IRQ(st
);
968 return se
; /* NB: unlocked */
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
);
983 /* XXX something is wrong */
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
;
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
);
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
);
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
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.
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.
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
;
1094 SCAN_STA_GEN_LOCK(st
);
1095 gen
= st
->st_scangen
++;
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
);
1108 /* We probably ran out of buffer space. */
1115 SCAN_STA_UNLOCK_IRQ(st
);
1118 SCAN_STA_GEN_UNLOCK(st
);
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
)
1133 se
= sta_lookup(st
, macaddr
, ss
->ss_ssid
);
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
);
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
);
1154 IEEE80211_NOTE_MAC(ss
->ss_vap
, IEEE80211_MSG_SCAN
,
1155 macaddr
, "%s: fails %u", __func__
, se
->se_fails
);
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.
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
;
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
++) {
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
)
1211 mode
= IEEE80211_MODE_11G
; /* upgrade */
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
))
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
);
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 */
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
;
1268 SCAN_STA_LOCK_IRQ(st
);
1269 for (i
= 0; i
< ss
->ss_last
; i
++) {
1270 c
= ss
->ss_chans
[i
];
1272 TAILQ_FOREACH(se
, &st
->st_entry
, se_list
) {
1273 if (se
->base
.se_chan
!= c
)
1275 if (se
->base
.se_rssi
> maxrssi
)
1276 maxrssi
= se
->base
.se_rssi
;
1278 if (bestchan
== NULL
|| maxrssi
< bestrssi
)
1281 SCAN_STA_UNLOCK_IRQ(st
);
1287 * Pick an ibss network to join or find a channel
1288 * to use to start an ibss network.
1291 adhoc_pick_bss(struct ieee80211_scan_state
*ss
, struct ieee80211vap
*vap
,
1292 int (*action
)(struct ieee80211vap
*, const struct ieee80211_scan_entry
*),
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
);
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
;
1317 st
->st_action
= ss
->ss_ops
->scan_default
;
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
);
1339 chan
= vap
->iv_des_chan
;
1341 struct ieee80211_scan_entry se
;
1343 memset(&se
, 0, sizeof(se
));
1347 IEEE80211_SCHEDULE_TQUEUE(&st
->st_actiontq
);
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.
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.
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
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
);
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
,
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
))
1439 switch (vap
->iv_opmode
) {
1440 case IEEE80211_M_STA
:
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
);
1455 chan
= vap
->iv_des_chan
;
1457 struct ieee80211_scan_entry se
;
1459 memset(&se
, 0, sizeof(se
));
1461 if ((*ss
->ss_ops
->scan_default
)(vap
, &se
))
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.
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
);
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");
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
);
1504 module_init(init_scanner_sta
);
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
);