1 /*-
2 * Copyright (c) 2002-2008 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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_scan.c,v 1.9 2008/10/23 19:57:13 des Exp $");
28
29 /*
30 * IEEE 802.11 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
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
43
44 #include <net80211/ieee80211_var.h>
45
46 #include <net/bpf.h>
47
48 struct scan_state {
49 struct ieee80211_scan_state base; /* public state */
50
51 u_int ss_iflags; /* flags used internally */
52 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
53 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
54 #define ISCAN_CANCEL 0x0004 /* cancel current scan */
55 #define ISCAN_START 0x0008 /* 1st time through next_scan */
56 unsigned long ss_chanmindwell; /* min dwell on curchan */
57 unsigned long ss_scanend; /* time scan must stop */
58 u_int ss_duration; /* duration for next scan */
59 struct callout ss_scan_timer; /* scan timer */
60 };
61 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
62
63 /*
64 * Amount of time to go off-channel during a background
65 * scan. This value should be large enough to catch most
66 * ap's but short enough that we can return on-channel
67 * before our listen interval expires.
68 *
69 * XXX tunable
70 * XXX check against configured listen interval
71 */
72 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
73
74 /*
75 * Roaming-related defaults. RSSI thresholds are as returned by the
76 * driver (dBm). Transmit rate thresholds are IEEE rate codes (i.e
77 * .5M units) or MCS.
78 */
79 #define ROAM_RSSI_11A_DEFAULT 14 /* rssi threshold for 11a bss */
80 #define ROAM_RSSI_11B_DEFAULT 14 /* rssi threshold for 11b bss */
81 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* rssi threshold for 11b-only bss */
82 #define ROAM_RATE_11A_DEFAULT 2*12 /* tx rate thresh for 11a bss */
83 #define ROAM_RATE_11B_DEFAULT 2*5 /* tx rate thresh for 11b bss */
84 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* tx rate thresh for 11b-only bss */
85 #define ROAM_MCS_11N_DEFAULT 1 /* tx MCS thresh for 11n bss*/
86
87 static void scan_restart_pwrsav(void *);
88 static void scan_curchan(struct ieee80211_scan_state *, unsigned long);
89 static void scan_mindwell(struct ieee80211_scan_state *);
90 static void scan_next(void *);
91
92 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
93
94 void
95 ieee80211_scan_attach(struct ieee80211com *ic)
96 {
97 struct scan_state *ss;
98
99 MALLOC(ss, struct scan_state *, sizeof(struct scan_state),
100 M_80211_SCAN, M_NOWAIT | M_ZERO);
101 if (ss == NULL) {
102 ic->ic_scan = NULL;
103 return;
104 }
105 callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
106 ic->ic_scan = &ss->base;
107
108 ic->ic_scan_curchan = scan_curchan;
109 ic->ic_scan_mindwell = scan_mindwell;
110 }
111
112 void
113 ieee80211_scan_detach(struct ieee80211com *ic)
114 {
115 struct ieee80211_scan_state *ss = ic->ic_scan;
116
117 if (ss != NULL) {
118 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
119 if (ss->ss_ops != NULL) {
120 ss->ss_ops->scan_detach(ss);
121 ss->ss_ops = NULL;
122 }
123 ic->ic_flags &= ~IEEE80211_F_SCAN;
124 ic->ic_scan = NULL;
125 FREE(SCAN_PRIVATE(ss), M_80211_SCAN);
126 }
127 }
128
129 static __inline void
130 setparams(struct ieee80211_roamparam *rp, int8_t rssi, uint8_t txrate)
131 {
132 rp->rssi = rssi;
133 rp->rate = txrate;
134 }
135
136 void
137 ieee80211_scan_vattach(struct ieee80211vap *vap)
138 {
139 struct ieee80211com *ic = vap->iv_ic;
140
141 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
142 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
143 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
144
145 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
146
147 /* NB: only set supported modes so user apps can identify */
148 if (isset(ic->ic_modecaps, IEEE80211_MODE_11A))
149 setparams(&vap->iv_roamparms[IEEE80211_MODE_11A],
150 ROAM_RSSI_11A_DEFAULT, ROAM_RATE_11A_DEFAULT);
151 if (isset(ic->ic_modecaps, IEEE80211_MODE_11G))
152 setparams(&vap->iv_roamparms[IEEE80211_MODE_11G],
153 ROAM_RSSI_11B_DEFAULT, ROAM_RATE_11B_DEFAULT);
154 if (isset(ic->ic_modecaps, IEEE80211_MODE_11B))
155 setparams(&vap->iv_roamparms[IEEE80211_MODE_11B],
156 ROAM_RSSI_11BONLY_DEFAULT, ROAM_RATE_11BONLY_DEFAULT);
157 /* NB: default turbo controls to be the same as !turbo */
158 if (isset(ic->ic_modecaps, IEEE80211_MODE_TURBO_A))
159 vap->iv_roamparms[IEEE80211_MODE_TURBO_A] =
160 vap->iv_roamparms[IEEE80211_MODE_11A];
161 if (isset(ic->ic_modecaps, IEEE80211_MODE_TURBO_G))
162 vap->iv_roamparms[IEEE80211_MODE_TURBO_G] =
163 vap->iv_roamparms[IEEE80211_MODE_11G];
164 if (isset(ic->ic_modecaps, IEEE80211_MODE_STURBO_A))
165 vap->iv_roamparms[IEEE80211_MODE_STURBO_A] =
166 vap->iv_roamparms[IEEE80211_MODE_11A];
167 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
168 setparams(&vap->iv_roamparms[IEEE80211_MODE_11NA],
169 ROAM_RSSI_11A_DEFAULT, ROAM_MCS_11N_DEFAULT | 0x80);
170 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
171 setparams(&vap->iv_roamparms[IEEE80211_MODE_11NG],
172 ROAM_RSSI_11B_DEFAULT, ROAM_MCS_11N_DEFAULT | 0x80);
173 }
174
175 void
176 ieee80211_scan_vdetach(struct ieee80211vap *vap)
177 {
178 struct ieee80211com *ic = vap->iv_ic;
179 struct ieee80211_scan_state *ss;
180
181 IEEE80211_LOCK(ic);
182 ss = ic->ic_scan;
183 if (ss != NULL && ss->ss_vap == vap) {
184 if (ic->ic_flags & IEEE80211_F_SCAN) {
185 /* XXX callout_drain */
186 callout_stop(&SCAN_PRIVATE(ss)->ss_scan_timer);
187 ic->ic_flags &= ~IEEE80211_F_SCAN;
188 }
189 if (ss->ss_ops != NULL) {
190 ss->ss_ops->scan_detach(ss);
191 ss->ss_ops = NULL;
192 }
193 ss->ss_vap = NULL;
194 }
195 IEEE80211_UNLOCK(ic);
196 }
197
198 /*
199 * Simple-minded scanner module support.
200 */
201 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
202 "wlan_scan_sta", /* IEEE80211_M_IBSS */
203 "wlan_scan_sta", /* IEEE80211_M_STA */
204 "wlan_scan_wds", /* IEEE80211_M_WDS */
205 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
206 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
207 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
208 };
209 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
210
211 const struct ieee80211_scanner *
212 ieee80211_scanner_get(enum ieee80211_opmode mode)
213 {
214 if (mode >= IEEE80211_OPMODE_MAX)
215 return NULL;
216 if (scanners[mode] == NULL)
217 ieee80211_load_module(scan_modnames[mode]);
218 return scanners[mode];
219 }
220
221 void
222 ieee80211_scanner_register(enum ieee80211_opmode mode,
223 const struct ieee80211_scanner *scan)
224 {
225 if (mode >= IEEE80211_OPMODE_MAX)
226 return;
227 scanners[mode] = scan;
228 }
229
230 void
231 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
232 const struct ieee80211_scanner *scan)
233 {
234 if (mode >= IEEE80211_OPMODE_MAX)
235 return;
236 if (scanners[mode] == scan)
237 scanners[mode] = NULL;
238 }
239
240 void
241 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
242 {
243 int m;
244
245 for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
246 if (scanners[m] == scan)
247 scanners[m] = NULL;
248 }
249
250 /*
251 * Update common scanner state to reflect the current
252 * operating mode. This is called when the state machine
253 * is transitioned to RUN state w/o scanning--e.g. when
254 * operating in monitor mode. The purpose of this is to
255 * ensure later callbacks find ss_ops set to properly
256 * reflect current operating mode.
257 */
258 static void
259 scan_update_locked(struct ieee80211vap *vap,
260 const struct ieee80211_scanner *scan)
261 {
262 struct ieee80211com *ic = vap->iv_ic;
263 struct ieee80211_scan_state *ss = ic->ic_scan;
264
265 IEEE80211_LOCK_ASSERT(ic);
266
267 #ifdef IEEE80211_DEBUG
268 if (ss->ss_vap != vap || ss->ss_ops != scan) {
269 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
270 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
271 __func__,
272 ss->ss_vap != NULL ?
273 ss->ss_vap->iv_ifp->if_xname : "none",
274 ss->ss_vap != NULL ?
275 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
276 vap->iv_ifp->if_xname,
277 ieee80211_opmode_name[vap->iv_opmode]);
278 }
279 #endif
280 ss->ss_vap = vap;
281 if (ss->ss_ops != scan) {
282 /*
283 * Switch scanners; detach old, attach new. Special
284 * case where a single scan module implements multiple
285 * policies by using different scan ops but a common
286 * core. We assume if the old and new attach methods
287 * are identical then it's ok to just change ss_ops
288 * and not flush the internal state of the module.
289 */
290 if (scan == NULL || ss->ss_ops == NULL ||
291 ss->ss_ops->scan_attach != scan->scan_attach) {
292 if (ss->ss_ops != NULL)
293 ss->ss_ops->scan_detach(ss);
294 if (scan != NULL && !scan->scan_attach(ss)) {
295 /* XXX attach failure */
296 /* XXX stat+msg */
297 scan = NULL;
298 }
299 }
300 ss->ss_ops = scan;
301 }
302 }
303
304 static void
305 change_channel(struct ieee80211com *ic,
306 struct ieee80211_channel *chan)
307 {
308 ic->ic_curchan = chan;
309 ic->ic_set_channel(ic);
310 }
311
312 static char
313 channel_type(const struct ieee80211_channel *c)
314 {
315 if (IEEE80211_IS_CHAN_ST(c))
316 return 'S';
317 if (IEEE80211_IS_CHAN_108A(c))
318 return 'T';
319 if (IEEE80211_IS_CHAN_108G(c))
320 return 'G';
321 if (IEEE80211_IS_CHAN_HT(c))
322 return 'n';
323 if (IEEE80211_IS_CHAN_A(c))
324 return 'a';
325 if (IEEE80211_IS_CHAN_ANYG(c))
326 return 'g';
327 if (IEEE80211_IS_CHAN_B(c))
328 return 'b';
329 return 'f';
330 }
331
332 void
333 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
334 {
335 struct ieee80211com *ic = ss->ss_vap->iv_ic;
336 const char *sep;
337 int i;
338
339 sep = "";
340 for (i = ss->ss_next; i < ss->ss_last; i++) {
341 const struct ieee80211_channel *c = ss->ss_chans[i];
342
343 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
344 channel_type(c));
345 sep = ", ";
346 }
347 }
348
349 #ifdef IEEE80211_DEBUG
350 static void
351 scan_dump(struct ieee80211_scan_state *ss)
352 {
353 struct ieee80211vap *vap = ss->ss_vap;
354
355 if_printf(vap->iv_ifp, "scan set ");
356 ieee80211_scan_dump_channels(ss);
357 printf(" dwell min %lu max %lu\n", ss->ss_mindwell, ss->ss_maxdwell);
358 }
359 #endif /* IEEE80211_DEBUG */
360
361 /*
362 * Enable station power save mode and start/restart the scanning thread.
363 */
364 static void
365 scan_restart_pwrsav(void *arg)
366 {
367 struct scan_state *ss = (struct scan_state *) arg;
368 struct ieee80211vap *vap = ss->base.ss_vap;
369 struct ieee80211com *ic = vap->iv_ic;
370 int ticksdelay;
371
372 ieee80211_sta_pwrsave(vap, 1);
373 /*
374 * Use an initial 1ms delay so the null
375 * data frame has a chance to go out.
376 * XXX 1ms is a lot, better to trigger scan
377 * on tx complete.
378 */
379 ticksdelay = msecs_to_ticks(1);
380 if (ticksdelay < 1)
381 ticksdelay = 1;
382 ic->ic_scan_start(ic); /* notify driver */
383 ss->ss_scanend = ticks + ticksdelay + ss->ss_duration;
384 ss->ss_iflags |= ISCAN_START;
385 callout_reset(&ss->ss_scan_timer, ticksdelay, scan_next, ss);
386 }
387
388 /*
389 * Start/restart scanning. If we're operating in station mode
390 * and associated notify the ap we're going into power save mode
391 * and schedule a callback to initiate the work (where there's a
392 * better context for doing the work). Otherwise, start the scan
393 * directly.
394 */
395 static int
396 scan_restart(struct scan_state *ss, u_int duration)
397 {
398 struct ieee80211vap *vap = ss->base.ss_vap;
399 struct ieee80211com *ic = vap->iv_ic;
400 int defer = 0;
401
402 if (ss->base.ss_next == ss->base.ss_last) {
403 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
404 "%s: no channels to scan\n", __func__);
405 return 0;
406 }
407 if (vap->iv_opmode == IEEE80211_M_STA &&
408 vap->iv_state == IEEE80211_S_RUN) {
409 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
410 /*
411 * Initiate power save before going off-channel.
412 * Note that we cannot do this directly because
413 * of locking issues; instead we defer it to a
414 * tasklet.
415 */
416 ss->ss_duration = duration;
417 defer = 1;
418 }
419 }
420
421 if (!defer) {
422 ic->ic_scan_start(ic); /* notify driver */
423 ss->ss_scanend = ticks + duration;
424 ss->ss_iflags |= ISCAN_START;
425 callout_reset(&ss->ss_scan_timer, 0, scan_next, ss);
426 } else
427 scan_restart_pwrsav(ss);
428 return 1;
429 }
430
431 static void
432 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
433 int nssid, const struct ieee80211_scan_ssid ssids[])
434 {
435 if (nssid > IEEE80211_SCAN_MAX_SSID) {
436 /* XXX printf */
437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
438 "%s: too many ssid %d, ignoring all of them\n",
439 __func__, nssid);
440 return;
441 }
442 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
443 ss->ss_nssid = nssid;
444 }
445
446 /*
447 * Start a scan unless one is already going.
448 */
449 static int
450 start_scan_locked(const struct ieee80211_scanner *scan,
451 struct ieee80211vap *vap, int flags, u_int duration,
452 u_int mindwell, u_int maxdwell,
453 u_int nssid, const struct ieee80211_scan_ssid ssids[])
454 {
455 struct ieee80211com *ic = vap->iv_ic;
456 struct ieee80211_scan_state *ss = ic->ic_scan;
457
458 IEEE80211_LOCK_ASSERT(ic);
459
460 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
461 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
462 "%s: scan inhibited by pending channel change\n", __func__);
463 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
464 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
465 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
466 , __func__
467 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
468 , duration, mindwell, maxdwell
469 , ieee80211_phymode_name[vap->iv_des_mode]
470 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
471 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
472 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
473 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
474 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
475 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
476 );
477
478 scan_update_locked(vap, scan);
479 if (ss->ss_ops != NULL) {
480 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
481 copy_ssid(vap, ss, nssid, ssids);
482
483 /* NB: top 4 bits for internal use */
484 ss->ss_flags = flags & 0xfff;
485 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
486 vap->iv_stats.is_scan_active++;
487 else
488 vap->iv_stats.is_scan_passive++;
489 if (flags & IEEE80211_SCAN_FLUSH)
490 ss->ss_ops->scan_flush(ss);
491
492 /* NB: flush frames rx'd before 1st channel change */
493 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
494 ss->ss_next = 0;
495 ss->ss_mindwell = mindwell;
496 ss->ss_maxdwell = maxdwell;
497 ss->ss_ops->scan_start(ss, vap);
498 #ifdef IEEE80211_DEBUG
499 if (ieee80211_msg_scan(vap))
500 scan_dump(ss);
501 #endif /* IEEE80211_DEBUG */
502 if (scan_restart(SCAN_PRIVATE(ss), duration))
503 ic->ic_flags |= IEEE80211_F_SCAN;
504 }
505 } else {
506 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
507 "%s: %s scan already in progress\n", __func__,
508 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
509 }
510 return (ic->ic_flags & IEEE80211_F_SCAN);
511 }
512
513 /*
514 * Start a scan unless one is already going.
515 */
516 int
517 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
518 u_int duration, u_int mindwell, u_int maxdwell,
519 u_int nssid, const struct ieee80211_scan_ssid ssids[])
520 {
521 struct ieee80211com *ic = vap->iv_ic;
522 const struct ieee80211_scanner *scan;
523 int result;
524
525 scan = ieee80211_scanner_get(vap->iv_opmode);
526 if (scan == NULL) {
527 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
528 "%s: no scanner support for %s mode\n",
529 __func__, ieee80211_opmode_name[vap->iv_opmode]);
530 /* XXX stat */
531 return 0;
532 }
533
534 IEEE80211_LOCK(ic);
535 result = start_scan_locked(scan, vap, flags, duration,
536 mindwell, maxdwell, nssid, ssids);
537 IEEE80211_UNLOCK(ic);
538
539 return result;
540 }
541
542 /*
543 * Check the scan cache for an ap/channel to use; if that
544 * fails then kick off a new scan.
545 */
546 int
547 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
548 u_int duration, u_int mindwell, u_int maxdwell,
549 u_int nssid, const struct ieee80211_scan_ssid ssids[])
550 {
551 struct ieee80211com *ic = vap->iv_ic;
552 struct ieee80211_scan_state *ss = ic->ic_scan;
553 const struct ieee80211_scanner *scan;
554 int result;
555
556 scan = ieee80211_scanner_get(vap->iv_opmode);
557 if (scan == NULL) {
558 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
559 "%s: no scanner support for %s mode\n",
560 __func__, vap->iv_opmode);
561 /* XXX stat */
562 return 0;
563 }
564
565 /*
566 * Check if there's a list of scan candidates already.
567 * XXX want more than the ap we're currently associated with
568 */
569
570 IEEE80211_LOCK(ic);
571 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
572 "%s: %s scan, %s%s%s%s%s\n"
573 , __func__
574 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
575 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
576 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
577 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
578 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
579 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
580 );
581
582 if (ss->ss_ops != scan) {
583 /* XXX re-use cache contents? e.g. adhoc<->sta */
584 flags |= IEEE80211_SCAN_FLUSH;
585 }
586 scan_update_locked(vap, scan);
587 if (ss->ss_ops != NULL) {
588 /* XXX verify ss_ops matches vap->iv_opmode */
589 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
590 /*
591 * Update the ssid list and mark flags so if
592 * we call start_scan it doesn't duplicate work.
593 */
594 copy_ssid(vap, ss, nssid, ssids);
595 flags |= IEEE80211_SCAN_NOSSID;
596 }
597 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
598 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
599 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
600 /*
601 * We're not currently scanning and the cache is
602 * deemed hot enough to consult. Lock out others
603 * by marking IEEE80211_F_SCAN while we decide if
604 * something is already in the scan cache we can
605 * use. Also discard any frames that might come
606 * in while temporarily marked as scanning.
607 */
608 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
609 ic->ic_flags |= IEEE80211_F_SCAN;
610
611 /* NB: need to use supplied flags in check */
612 ss->ss_flags = flags & 0xff;
613 result = ss->ss_ops->scan_end(ss, vap);
614
615 ic->ic_flags &= ~IEEE80211_F_SCAN;
616 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
617 if (result) {
618 ieee80211_notify_scan_done(vap);
619 IEEE80211_UNLOCK(ic);
620 return 1;
621 }
622 }
623 }
624 result = start_scan_locked(scan, vap, flags, duration,
625 mindwell, maxdwell, nssid, ssids);
626 IEEE80211_UNLOCK(ic);
627
628 return result;
629 }
630
631 /*
632 * Check the scan cache for an ap/channel to use; if that fails
633 * then kick off a scan using the current settings.
634 */
635 int
636 ieee80211_check_scan_current(struct ieee80211vap *vap)
637 {
638 return ieee80211_check_scan(vap,
639 IEEE80211_SCAN_ACTIVE,
640 IEEE80211_SCAN_FOREVER, 0, 0,
641 vap->iv_des_nssid, vap->iv_des_ssid);
642 }
643
644 /*
645 * Restart a previous scan. If the previous scan completed
646 * then we start again using the existing channel list.
647 */
648 int
649 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
650 {
651 struct ieee80211com *ic = vap->iv_ic;
652 struct ieee80211_scan_state *ss = ic->ic_scan;
653 const struct ieee80211_scanner *scan;
654
655 scan = ieee80211_scanner_get(vap->iv_opmode);
656 if (scan == NULL) {
657 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
658 "%s: no scanner support for %s mode\n",
659 __func__, vap->iv_opmode);
660 /* XXX stat */
661 return 0;
662 }
663
664 IEEE80211_LOCK(ic);
665 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
666 u_int duration;
667 /*
668 * Go off-channel for a fixed interval that is large
669 * enough to catch most ap's but short enough that
670 * we can return on-channel before our listen interval
671 * expires.
672 */
673 duration = IEEE80211_SCAN_OFFCHANNEL;
674
675 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
676 "%s: %s scan, ticks %u duration %lu\n", __func__,
677 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
678 ticks, duration);
679
680 scan_update_locked(vap, scan);
681 if (ss->ss_ops != NULL) {
682 ss->ss_vap = vap;
683 /*
684 * A background scan does not select a new sta; it
685 * just refreshes the scan cache. Also, indicate
686 * the scan logic should follow the beacon schedule:
687 * we go off-channel and scan for a while, then
688 * return to the bss channel to receive a beacon,
689 * then go off-channel again. All during this time
690 * we notify the ap we're in power save mode. When
691 * the scan is complete we leave power save mode.
692 * If any beacon indicates there are frames pending
693 * for us then we drop out of power save mode
694 * (and background scan) automatically by way of the
695 * usual sta power save logic.
696 */
697 ss->ss_flags |= IEEE80211_SCAN_NOPICK
698 | IEEE80211_SCAN_BGSCAN
699 | flags
700 ;
701 /* if previous scan completed, restart */
702 if (ss->ss_next >= ss->ss_last) {
703 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
704 vap->iv_stats.is_scan_active++;
705 else
706 vap->iv_stats.is_scan_passive++;
707 /*
708 * NB: beware of the scan cache being flushed;
709 * if the channel list is empty use the
710 * scan_start method to populate it.
711 */
712 ss->ss_next = 0;
713 if (ss->ss_last != 0)
714 ss->ss_ops->scan_restart(ss, vap);
715 else {
716 ss->ss_ops->scan_start(ss, vap);
717 #ifdef IEEE80211_DEBUG
718 if (ieee80211_msg_scan(vap))
719 scan_dump(ss);
720 #endif /* IEEE80211_DEBUG */
721 }
722 }
723 /* NB: flush frames rx'd before 1st channel change */
724 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
725 ss->ss_maxdwell = duration;
726 if (scan_restart(SCAN_PRIVATE(ss), duration)) {
727 ic->ic_flags |= IEEE80211_F_SCAN;
728 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
729 }
730 } else {
731 /* XXX msg+stat */
732 }
733 } else {
734 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
735 "%s: %s scan already in progress\n", __func__,
736 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
737 }
738 IEEE80211_UNLOCK(ic);
739
740 /* NB: racey, does it matter? */
741 return (ic->ic_flags & IEEE80211_F_SCAN);
742 }
743
744 /*
745 * Cancel any scan currently going on for the specified vap.
746 */
747 void
748 ieee80211_cancel_scan(struct ieee80211vap *vap)
749 {
750 struct ieee80211com *ic = vap->iv_ic;
751 struct ieee80211_scan_state *ss = ic->ic_scan;
752
753 IEEE80211_LOCK(ic);
754 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
755 ss->ss_vap == vap &&
756 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
757 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
758 "%s: cancel %s scan\n", __func__,
759 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
760 "active" : "passive");
761
762 /* clear bg scan NOPICK and mark cancel request */
763 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
764 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
765 /* force it to fire asap */
766 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
767 0, scan_next, ss);
768 }
769 IEEE80211_UNLOCK(ic);
770 }
771
772 /*
773 * Cancel any scan currently going on.
774 */
775 void
776 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
777 {
778 struct ieee80211com *ic = vap->iv_ic;
779 struct ieee80211_scan_state *ss = ic->ic_scan;
780
781 IEEE80211_LOCK(ic);
782 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
783 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
784 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
785 "%s: cancel %s scan\n", __func__,
786 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
787 "active" : "passive");
788
789 /* clear bg scan NOPICK and mark cancel request */
790 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
791 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
792 /* force it to fire asap */
793 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
794 0, scan_next, ss);
795 }
796 IEEE80211_UNLOCK(ic);
797 }
798
799 /*
800 * Public access to scan_next for drivers that manage
801 * scanning themselves (e.g. for firmware-based devices).
802 */
803 void
804 ieee80211_scan_next(struct ieee80211vap *vap)
805 {
806 struct ieee80211com *ic = vap->iv_ic;
807 struct ieee80211_scan_state *ss = ic->ic_scan;
808
809 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
810 }
811
812 /*
813 * Public access to scan_next for drivers that are not able to scan single
814 * channels (e.g. for firmware-based devices).
815 */
816 void
817 ieee80211_scan_done(struct ieee80211vap *vap)
818 {
819 struct ieee80211com *ic = vap->iv_ic;
820 struct ieee80211_scan_state *ss;
821
822 IEEE80211_LOCK(ic);
823 ss = ic->ic_scan;
824 ss->ss_next = ss->ss_last; /* all channels are complete */
825 scan_next(ss);
826 IEEE80211_UNLOCK(ic);
827 }
828
829 /*
830 * Probe the curent channel, if allowed, while scanning.
831 * If the channel is not marked passive-only then send
832 * a probe request immediately. Otherwise mark state and
833 * listen for beacons on the channel; if we receive something
834 * then we'll transmit a probe request.
835 */
836 void
837 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
838 {
839 struct ieee80211com *ic = vap->iv_ic;
840 struct ieee80211_scan_state *ss = ic->ic_scan;
841 struct ifnet *ifp = vap->iv_ifp;
842 int i;
843
844 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
845 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
846 return;
847 }
848 /*
849 * Send directed probe requests followed by any
850 * broadcast probe request.
851 * XXX remove dependence on ic/vap->iv_bss
852 */
853 for (i = 0; i < ss->ss_nssid; i++)
854 ieee80211_send_probereq(vap->iv_bss,
855 vap->iv_myaddr, ifp->if_broadcastaddr,
856 ifp->if_broadcastaddr,
857 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
858 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
859 ieee80211_send_probereq(vap->iv_bss,
860 vap->iv_myaddr, ifp->if_broadcastaddr,
861 ifp->if_broadcastaddr,
862 "", 0);
863 }
864
865 /*
866 * Scan curchan. If this is an active scan and the channel
867 * is not marked passive then send probe request frame(s).
868 * Arrange for the channel change after maxdwell ticks.
869 */
870 static void
871 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
872 {
873 struct ieee80211vap *vap = ss->ss_vap;
874
875 IEEE80211_LOCK_ASSERT(vap->iv_ic);
876
877 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
878 ieee80211_probe_curchan(vap, 0);
879 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
880 maxdwell, scan_next, ss);
881 }
882
883 /*
884 * Handle mindwell requirements completed; initiate a channel
885 * change to the next channel asap.
886 */
887 static void
888 scan_mindwell(struct ieee80211_scan_state *ss)
889 {
890 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
|