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 /*
28 * IEEE 802.11 scanning support.
29 */
30 #include "opt_wlan.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/condvar.h>
38
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
45
46 #include <net80211/ieee80211_var.h>
47
48 #include <net80211/ieee80211_scan_sw.h>
49
50 #include <net/bpf.h>
51
52 struct scan_state {
53 struct ieee80211_scan_state base; /* public state */
54
55 u_int ss_iflags; /* flags used internally */
56 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
57 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
58 #define ISCAN_INTERRUPT 0x0004 /* interrupt current scan */
59 #define ISCAN_CANCEL 0x0008 /* cancel current scan */
60 #define ISCAN_PAUSE (ISCAN_INTERRUPT | ISCAN_CANCEL)
61 #define ISCAN_ABORT 0x0010 /* end the scan immediately */
62 #define ISCAN_RUNNING 0x0020 /* scan was started */
63
64 unsigned long ss_chanmindwell; /* min dwell on curchan */
65 unsigned long ss_scanend; /* time scan must stop */
66 u_int ss_duration; /* duration for next scan */
67 struct task ss_scan_start; /* scan start */
68 struct timeout_task ss_scan_curchan; /* scan execution */
69 };
70 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
71
72 /*
73 * Amount of time to go off-channel during a background
74 * scan. This value should be large enough to catch most
75 * ap's but short enough that we can return on-channel
76 * before our listen interval expires.
77 *
78 * XXX tunable
79 * XXX check against configured listen interval
80 */
81 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
82
83 static void scan_curchan(struct ieee80211_scan_state *, unsigned long);
84 static void scan_mindwell(struct ieee80211_scan_state *);
85 static void scan_signal(struct ieee80211_scan_state *, int);
86 static void scan_signal_locked(struct ieee80211_scan_state *, int);
87 static void scan_start(void *, int);
88 static void scan_curchan_task(void *, int);
89 static void scan_end(struct ieee80211_scan_state *, int);
90 static void scan_done(struct ieee80211_scan_state *, int);
91
92 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
93
94 static void
ieee80211_swscan_detach(struct ieee80211com * ic)95 ieee80211_swscan_detach(struct ieee80211com *ic)
96 {
97 struct ieee80211_scan_state *ss = ic->ic_scan;
98
99 if (ss != NULL) {
100 scan_signal(ss, ISCAN_ABORT);
101 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_start);
102 taskqueue_drain_timeout(ic->ic_tq,
103 &SCAN_PRIVATE(ss)->ss_scan_curchan);
104 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
105 ("scan still running"));
106
107 /*
108 * For now, do the ss_ops detach here rather
109 * than ieee80211_scan_detach().
110 *
111 * I'll figure out how to cleanly split things up
112 * at a later date.
113 */
114 if (ss->ss_ops != NULL) {
115 ss->ss_ops->scan_detach(ss);
116 ss->ss_ops = NULL;
117 }
118 ic->ic_scan = NULL;
119 IEEE80211_FREE(SCAN_PRIVATE(ss), M_80211_SCAN);
120 }
121 }
122
123 static void
ieee80211_swscan_vattach(struct ieee80211vap * vap)124 ieee80211_swscan_vattach(struct ieee80211vap *vap)
125 {
126 /* nothing to do for now */
127 /*
128 * TODO: all of the vap scan calls should be methods!
129 */
130
131 }
132
133 static void
ieee80211_swscan_vdetach(struct ieee80211vap * vap)134 ieee80211_swscan_vdetach(struct ieee80211vap *vap)
135 {
136 struct ieee80211com *ic = vap->iv_ic;
137 struct ieee80211_scan_state *ss = ic->ic_scan;
138
139 IEEE80211_LOCK_ASSERT(ic);
140
141 if (ss != NULL && ss->ss_vap == vap &&
142 (ic->ic_flags & IEEE80211_F_SCAN))
143 scan_signal_locked(ss, ISCAN_ABORT);
144 }
145
146 static void
ieee80211_swscan_set_scan_duration(struct ieee80211vap * vap,u_int duration)147 ieee80211_swscan_set_scan_duration(struct ieee80211vap *vap, u_int duration)
148 {
149 struct ieee80211com *ic = vap->iv_ic;
150 struct ieee80211_scan_state *ss = ic->ic_scan;
151
152 IEEE80211_LOCK_ASSERT(ic);
153
154 /* NB: flush frames rx'd before 1st channel change */
155 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
156 SCAN_PRIVATE(ss)->ss_duration = duration;
157 }
158
159 /*
160 * Start a scan unless one is already going.
161 */
162 static int
ieee80211_swscan_start_scan_locked(const struct ieee80211_scanner * scan,struct ieee80211vap * vap,int flags,u_int duration,u_int mindwell,u_int maxdwell,u_int nssid,const struct ieee80211_scan_ssid ssids[])163 ieee80211_swscan_start_scan_locked(const struct ieee80211_scanner *scan,
164 struct ieee80211vap *vap, int flags, u_int duration,
165 u_int mindwell, u_int maxdwell,
166 u_int nssid, const struct ieee80211_scan_ssid ssids[])
167 {
168 struct ieee80211com *ic = vap->iv_ic;
169 struct ieee80211_scan_state *ss = ic->ic_scan;
170
171 IEEE80211_LOCK_ASSERT(ic);
172
173 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
174 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
175 "%s: scan inhibited by pending channel change\n", __func__);
176 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
177 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
178 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
179 , __func__
180 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
181 , duration, mindwell, maxdwell
182 , ieee80211_phymode_name[vap->iv_des_mode]
183 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
184 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
185 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
186 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
187 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
188 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
189 );
190
191 ieee80211_scan_update_locked(vap, scan);
192 if (ss->ss_ops != NULL) {
193 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
194 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
195
196 ss->ss_flags = flags & IEEE80211_SCAN_PUBLIC_MASK;
197 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
198 vap->iv_stats.is_scan_active++;
199 else
200 vap->iv_stats.is_scan_passive++;
201 if (flags & IEEE80211_SCAN_FLUSH)
202 ss->ss_ops->scan_flush(ss);
203 /* Only BGSCAN if enabled and requested. */
204 if ((vap->iv_flags & IEEE80211_F_BGSCAN) != 0 &&
205 (flags & IEEE80211_SCAN_BGSCAN) != 0)
206 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
207
208 /* Set duration for this particular scan */
209 ieee80211_swscan_set_scan_duration(vap, duration);
210
211 ss->ss_next = 0;
212 ss->ss_mindwell = mindwell;
213 ss->ss_maxdwell = maxdwell;
214 /* NB: scan_start must be before the scan runtask */
215 ss->ss_ops->scan_start(ss, vap);
216 #ifdef IEEE80211_DEBUG
217 if (ieee80211_msg_scan(vap))
218 ieee80211_scan_dump(ss);
219 #endif /* IEEE80211_DEBUG */
220 ic->ic_flags |= IEEE80211_F_SCAN;
221
222 /* Start scan task */
223 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_start);
224 }
225 return 1;
226 } else {
227 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
228 "%s: %s scan already in progress\n", __func__,
229 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
230 }
231 return 0;
232 }
233
234 /*
235 * Start a scan unless one is already going.
236 *
237 * Called without the comlock held; grab the comlock as appropriate.
238 */
239 static int
ieee80211_swscan_start_scan(const struct ieee80211_scanner * scan,struct ieee80211vap * vap,int flags,u_int duration,u_int mindwell,u_int maxdwell,u_int nssid,const struct ieee80211_scan_ssid ssids[])240 ieee80211_swscan_start_scan(const struct ieee80211_scanner *scan,
241 struct ieee80211vap *vap, int flags,
242 u_int duration, u_int mindwell, u_int maxdwell,
243 u_int nssid, const struct ieee80211_scan_ssid ssids[])
244 {
245 struct ieee80211com *ic = vap->iv_ic;
246 int result;
247
248 IEEE80211_UNLOCK_ASSERT(ic);
249
250 IEEE80211_LOCK(ic);
251 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
252 mindwell, maxdwell, nssid, ssids);
253 IEEE80211_UNLOCK(ic);
254
255 return result;
256 }
257
258 /*
259 * Check the scan cache for an ap/channel to use; if that
260 * fails then kick off a new scan.
261 *
262 * Called with the comlock held.
263 *
264 * XXX TODO: split out!
265 */
266 static int
ieee80211_swscan_check_scan(const struct ieee80211_scanner * scan,struct ieee80211vap * vap,int flags,u_int duration,u_int mindwell,u_int maxdwell,u_int nssid,const struct ieee80211_scan_ssid ssids[])267 ieee80211_swscan_check_scan(const struct ieee80211_scanner *scan,
268 struct ieee80211vap *vap, int flags,
269 u_int duration, u_int mindwell, u_int maxdwell,
270 u_int nssid, const struct ieee80211_scan_ssid ssids[])
271 {
272 struct ieee80211com *ic = vap->iv_ic;
273 struct ieee80211_scan_state *ss = ic->ic_scan;
274 int result;
275
276 IEEE80211_LOCK_ASSERT(ic);
277
278 if (ss->ss_ops != NULL) {
279 /* XXX verify ss_ops matches vap->iv_opmode */
280 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
281 /*
282 * Update the ssid list and mark flags so if
283 * we call start_scan it doesn't duplicate work.
284 */
285 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
286 flags |= IEEE80211_SCAN_NOSSID;
287 }
288 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
289 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
290 ieee80211_time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
291 /*
292 * We're not currently scanning and the cache is
293 * deemed hot enough to consult. Lock out others
294 * by marking IEEE80211_F_SCAN while we decide if
295 * something is already in the scan cache we can
296 * use. Also discard any frames that might come
297 * in while temporarily marked as scanning.
298 */
299 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
300 "cache hot; ic_lastscan=%d, scanvalid=%d, ticks=%d\n",
301 ic->ic_lastscan,
302 vap->iv_scanvalid,
303 ticks);
304 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
305 ic->ic_flags |= IEEE80211_F_SCAN;
306
307 /* NB: need to use supplied flags in check */
308 ss->ss_flags = flags & IEEE80211_SCAN_PUBLIC_MASK;
309 result = ss->ss_ops->scan_end(ss, vap);
310
311 ic->ic_flags &= ~IEEE80211_F_SCAN;
312 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
313 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
314 "%s: scan_end returned %d\n", __func__, result);
315 if (result) {
316 ieee80211_notify_scan_done(vap);
317 return 1;
318 }
319 }
320 }
321 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
322 mindwell, maxdwell, nssid, ssids);
323
324 return result;
325 }
326
327 /*
328 * Restart a previous scan. If the previous scan completed
329 * then we start again using the existing channel list.
330 */
331 static int
ieee80211_swscan_bg_scan(const struct ieee80211_scanner * scan,struct ieee80211vap * vap,int flags)332 ieee80211_swscan_bg_scan(const struct ieee80211_scanner *scan,
333 struct ieee80211vap *vap, int flags)
334 {
335 struct ieee80211com *ic = vap->iv_ic;
336 struct ieee80211_scan_state *ss = ic->ic_scan;
337 bool scanning;
338
339 /* XXX assert unlocked? */
340 // IEEE80211_UNLOCK_ASSERT(ic);
341
342 IEEE80211_LOCK(ic);
343 KASSERT((vap->iv_flags & IEEE80211_F_BGSCAN) != 0,
344 ("%s: vap %p iv_flags %#010x no IEEE80211_F_BGSCAN set",
345 __func__, vap, vap->iv_flags));
346
347 scanning = ic->ic_flags & IEEE80211_F_SCAN;
348 if (!scanning) {
349 u_int duration;
350 /*
351 * Go off-channel for a fixed interval that is large
352 * enough to catch most ap's but short enough that
353 * we can return on-channel before our listen interval
354 * expires.
355 */
356 duration = IEEE80211_SCAN_OFFCHANNEL;
357
358 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
359 "%s: %s scan, ticks %u duration %u\n", __func__,
360 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
361 ticks, duration);
362
363 ieee80211_scan_update_locked(vap, scan);
364 if (ss->ss_ops != NULL) {
365 ss->ss_vap = vap;
366 /*
367 * A background scan does not select a new sta; it
368 * just refreshes the scan cache. Also, indicate
369 * the scan logic should follow the beacon schedule:
370 * we go off-channel and scan for a while, then
371 * return to the bss channel to receive a beacon,
372 * then go off-channel again. All during this time
373 * we notify the ap we're in power save mode. When
374 * the scan is complete we leave power save mode.
375 * If any beacon indicates there are frames pending
376 * for us then we drop out of power save mode
377 * (and background scan) automatically by way of the
378 * usual sta power save logic.
379 */
380 ss->ss_flags |= IEEE80211_SCAN_NOPICK
381 | IEEE80211_SCAN_BGSCAN
382 | flags
383 ;
384 /* if previous scan completed, restart */
385 if (ss->ss_next >= ss->ss_last) {
386 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
387 vap->iv_stats.is_scan_active++;
388 else
389 vap->iv_stats.is_scan_passive++;
390 /*
391 * NB: beware of the scan cache being flushed;
392 * if the channel list is empty use the
393 * scan_start method to populate it.
394 */
395 ss->ss_next = 0;
396 if (ss->ss_last != 0) {
397 ieee80211_notify_scan_done(vap);
398 ss->ss_ops->scan_restart(ss, vap);
399 } else {
400 ss->ss_ops->scan_start(ss, vap);
401 #ifdef IEEE80211_DEBUG
402 if (ieee80211_msg_scan(vap))
403 ieee80211_scan_dump(ss);
404 #endif /* IEEE80211_DEBUG */
405 }
406 }
407 ieee80211_swscan_set_scan_duration(vap, duration);
408 ss->ss_maxdwell = duration;
409 ic->ic_flags |= IEEE80211_F_SCAN;
410 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
411 ieee80211_runtask(ic,
412 &SCAN_PRIVATE(ss)->ss_scan_start);
413 scanning = true;
414 } else {
415 /* XXX msg+stat */
416 }
417 } else {
418 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
419 "%s: %s scan already in progress\n", __func__,
420 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
421 }
422 IEEE80211_UNLOCK(ic);
423
424 return (scanning);
425 }
426
427 /*
428 * Taskqueue work to cancel a scan.
429 *
430 * Note: for offload scan devices, we may want to call into the
431 * driver to try and cancel scanning, however it may not be cancelable.
432 */
433 static void
cancel_scan(struct ieee80211vap * vap,int any,const char * func)434 cancel_scan(struct ieee80211vap *vap, int any, const char *func)
435 {
436 struct ieee80211com *ic = vap->iv_ic;
437 struct ieee80211_scan_state *ss = ic->ic_scan;
438 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
439 int signal;
440
441 IEEE80211_LOCK(ic);
442 signal = any ? ISCAN_PAUSE : ISCAN_CANCEL;
443 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
444 (any || ss->ss_vap == vap) &&
445 (ss_priv->ss_iflags & signal) == 0) {
446 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
447 "%s: %s %s scan\n", func,
448 any ? "pause" : "cancel",
449 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
450 "active" : "passive");
451
452 /* clear bg scan NOPICK */
453 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
454 /* mark request and wake up the scan task */
455 scan_signal_locked(ss, signal);
456 } else {
457 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
458 "%s: called; F_SCAN=%d, vap=%s, signal=%d\n",
459 func,
460 !! (ic->ic_flags & IEEE80211_F_SCAN),
461 (ss->ss_vap == vap ? "match" : "nomatch"),
462 !! (ss_priv->ss_iflags & signal));
463 }
464 IEEE80211_UNLOCK(ic);
465 }
466
467 /*
468 * Cancel any scan currently going on for the specified vap.
469 */
470 static void
ieee80211_swscan_cancel_scan(struct ieee80211vap * vap)471 ieee80211_swscan_cancel_scan(struct ieee80211vap *vap)
472 {
473 cancel_scan(vap, 0, __func__);
474 }
475
476 /*
477 * Cancel any scan currently going on.
478 */
479 static void
ieee80211_swscan_cancel_anyscan(struct ieee80211vap * vap)480 ieee80211_swscan_cancel_anyscan(struct ieee80211vap *vap)
481 {
482
483 /* XXX for now - just don't do this per packet. */
484 if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD)
485 return;
486
487 cancel_scan(vap, 1, __func__);
488 }
489
490 /*
491 * Manually switch to the next channel in the channel list.
492 * Provided for drivers that manage scanning themselves
493 * (e.g. for firmware-based devices).
494 */
495 static void
ieee80211_swscan_scan_next(struct ieee80211vap * vap)496 ieee80211_swscan_scan_next(struct ieee80211vap *vap)
497 {
498 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
499
500 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
501
502 /* wake up the scan task */
503 scan_signal(ss, 0);
504 }
505
506 /*
507 * Manually stop a scan that is currently running.
508 * Provided for drivers that are not able to scan single channels
509 * (e.g. for firmware-based devices).
510 */
511 static void
ieee80211_swscan_scan_done(struct ieee80211vap * vap)512 ieee80211_swscan_scan_done(struct ieee80211vap *vap)
513 {
514 struct ieee80211com *ic = vap->iv_ic;
515 struct ieee80211_scan_state *ss = ic->ic_scan;
516
517 IEEE80211_LOCK_ASSERT(ic);
518
519 scan_signal_locked(ss, 0);
520 }
521
522 /*
523 * Probe the current channel, if allowed, while scanning.
524 * If the channel is not marked passive-only then send
525 * a probe request immediately. Otherwise mark state and
526 * listen for beacons on the channel; if we receive something
527 * then we'll transmit a probe request.
528 */
529 static void
ieee80211_swscan_probe_curchan(struct ieee80211vap * vap,bool force __unused)530 ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, bool force __unused)
531 {
532 struct ieee80211com *ic = vap->iv_ic;
533 struct ieee80211_scan_state *ss = ic->ic_scan;
534 int i;
535
536 /*
537 * Full-offload scan devices don't require this.
538 */
539 if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD)
540 return;
541
542 /*
543 * Send directed probe requests followed by any
544 * broadcast probe request.
545 * XXX remove dependence on ic/vap->iv_bss
546 */
547 for (i = 0; i < ss->ss_nssid; i++)
548 ieee80211_send_probereq(vap->iv_bss,
549 vap->iv_myaddr,
550 ieee80211_vap_get_broadcast_address(vap),
551 ieee80211_vap_get_broadcast_address(vap),
552 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
553 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
554 ieee80211_send_probereq(vap->iv_bss,
555 vap->iv_myaddr,
556 ieee80211_vap_get_broadcast_address(vap),
557 ieee80211_vap_get_broadcast_address(vap),
558 "", 0);
559 }
560
561 /*
562 * Scan curchan. If this is an active scan and the channel
563 * is not marked passive then send probe request frame(s).
564 * Arrange for the channel change after maxdwell ticks.
565 */
566 static void
scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)567 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
568 {
569 struct ieee80211vap *vap = ss->ss_vap;
570 struct ieee80211com *ic = ss->ss_ic;
571
572 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
573 "%s: calling; maxdwell=%lu\n",
574 __func__,
575 maxdwell);
576 IEEE80211_LOCK(ic);
577 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
578 ieee80211_probe_curchan(vap, false);
579 taskqueue_enqueue_timeout(ic->ic_tq,
580 &SCAN_PRIVATE(ss)->ss_scan_curchan, maxdwell);
581 IEEE80211_UNLOCK(ic);
582 }
583
584 static void
scan_signal(struct ieee80211_scan_state * ss,int iflags)585 scan_signal(struct ieee80211_scan_state *ss, int iflags)
586 {
587 struct ieee80211com *ic = ss->ss_ic;
588
589 IEEE80211_UNLOCK_ASSERT(ic);
590
591 IEEE80211_LOCK(ic);
592 scan_signal_locked(ss, iflags);
593 IEEE80211_UNLOCK(ic);
594 }
595
596 static void
scan_signal_locked(struct ieee80211_scan_state * ss,int iflags)597 scan_signal_locked(struct ieee80211_scan_state *ss, int iflags)
598 {
599 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
600 struct timeout_task *scan_task = &ss_priv->ss_scan_curchan;
601 struct ieee80211com *ic = ss->ss_ic;
602
603 IEEE80211_LOCK_ASSERT(ic);
604
605 ss_priv->ss_iflags |= iflags;
606 if (ss_priv->ss_iflags & ISCAN_RUNNING) {
607 if (taskqueue_cancel_timeout(ic->ic_tq, scan_task, NULL) == 0)
608 taskqueue_enqueue_timeout(ic->ic_tq, scan_task, 0);
609 }
610 }
611
612 /*
613 * Handle mindwell requirements completed; initiate a channel
614 * change to the next channel asap.
615 */
616 static void
scan_mindwell(struct ieee80211_scan_state * ss)617 scan_mindwell(struct ieee80211_scan_state *ss)
618 {
619
620 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n",
621 __func__);
622
623 scan_signal(ss, 0);
624 }
625
626 static void
scan_start(void * arg,int pending)627 scan_start(void *arg, int pending)
628 {
629 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
630 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
631 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
632 struct ieee80211vap *vap = ss->ss_vap;
633 struct ieee80211com *ic = ss->ss_ic;
634
635 IEEE80211_LOCK(ic);
636 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
637 (ss_priv->ss_iflags & ISCAN_ABORT)) {
638 /* Cancelled before we started */
639 scan_done(ss, 0);
640 return;
641 }
642
643 if (ss->ss_next == ss->ss_last) {
644 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
645 "%s: no channels to scan\n", __func__);
646 scan_done(ss, 1);
647 return;
648 }
649
650 /*
651 * Put the station into power save mode.
652 *
653 * This is only required if we're not a full-offload devices;
654 * those devices manage scan/traffic differently.
655 */
656 if (((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) &&
657 vap->iv_opmode == IEEE80211_M_STA &&
658 vap->iv_state == IEEE80211_S_RUN) {
659 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
660 /* Enable station power save mode */
661 vap->iv_sta_ps(vap, 1);
662 /* Wait until null data frame will be ACK'ed */
663 mtx_sleep(vap, IEEE80211_LOCK_OBJ(ic), PCATCH,
664 "sta_ps", msecs_to_ticks(10));
665 if (ss_priv->ss_iflags & ISCAN_ABORT) {
666 scan_done(ss, 0);
667 return;
668 }
669 }
670 }
671
672 ss_priv->ss_scanend = ticks + ss_priv->ss_duration;
673
674 /* XXX scan state can change! Re-validate scan state! */
675
676 IEEE80211_UNLOCK(ic);
677
678 ic->ic_scan_start(ic); /* notify driver */
679
680 scan_curchan_task(ss, 0);
681 }
682
683 static void
scan_curchan_task(void * arg,int pending __unused)684 scan_curchan_task(void *arg, int pending __unused)
685 {
686 struct ieee80211_scan_state *ss = arg;
687 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
688 struct ieee80211com *ic = ss->ss_ic;
689 struct ieee80211_channel *chan;
690 unsigned long maxdwell;
691 int scandone, scanstop;
692
693 IEEE80211_LOCK(ic);
694 end:
695 /*
696 * Note: only /end/ the scan if we're CANCEL rather than
697 * CANCEL+INTERRUPT (ie, 'PAUSE').
698 *
699 * We can stop the scan if we hit cancel, but we shouldn't
700 * call scan_end(ss, 1) if we're just PAUSEing the scan.
701 */
702 scandone = (ss->ss_next >= ss->ss_last) ||
703 ((ss_priv->ss_iflags & ISCAN_PAUSE) == ISCAN_CANCEL);
704 scanstop = (ss->ss_next >= ss->ss_last) ||
705 ((ss_priv->ss_iflags & ISCAN_CANCEL) != 0);
706
707 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
708 "%s: loop start; scandone=%d, scanstop=%d, ss_iflags=0x%x, ss_next=%u, ss_last=%u\n",
709 __func__,
710 scandone,
711 scanstop,
712 (uint32_t) ss_priv->ss_iflags,
713 (uint32_t) ss->ss_next,
714 (uint32_t) ss->ss_last);
715
716 if (scanstop || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
717 (ss_priv->ss_iflags & ISCAN_ABORT) ||
718 ieee80211_time_after(ticks + ss->ss_mindwell, ss_priv->ss_scanend)) {
719 ss_priv->ss_iflags &= ~ISCAN_RUNNING;
720 scan_end(ss, scandone);
721 return;
722 } else
723 ss_priv->ss_iflags |= ISCAN_RUNNING;
724
725 chan = ss->ss_chans[ss->ss_next++];
726
727 /*
728 * Watch for truncation due to the scan end time.
729 */
730 if (ieee80211_time_after(ticks + ss->ss_maxdwell, ss_priv->ss_scanend))
731 maxdwell = ss_priv->ss_scanend - ticks;
732 else
733 maxdwell = ss->ss_maxdwell;
734
735 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
736 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
737 __func__,
738 ieee80211_chan2ieee(ic, ic->ic_curchan),
739 ieee80211_channel_type_char(ic->ic_curchan),
740 ieee80211_chan2ieee(ic, chan),
741 ieee80211_channel_type_char(chan),
742 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
743 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
744 "active" : "passive",
745 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
746
747 /*
748 * Potentially change channel and phy mode.
749 */
750 ic->ic_curchan = chan;
751 ic->ic_rt = ieee80211_get_ratetable(chan);
752 IEEE80211_UNLOCK(ic);
753 /*
754 * Perform the channel change and scan unlocked so the driver
755 * may sleep. Once set_channel returns the hardware has
756 * completed the channel change.
757 */
758 ic->ic_set_channel(ic);
759 ieee80211_radiotap_chan_change(ic);
760
761 /*
762 * Scan curchan. Drivers for "intelligent hardware"
763 * override ic_scan_curchan to tell the device to do
764 * the work. Otherwise we manage the work ourselves;
765 * sending a probe request (as needed), and arming the
766 * timeout to switch channels after maxdwell ticks.
767 *
768 * scan_curchan should only pause for the time required to
769 * prepare/initiate the hardware for the scan (if at all).
770 */
771 ic->ic_scan_curchan(ss, maxdwell);
772 IEEE80211_LOCK(ic);
773
774 /* XXX scan state can change! Re-validate scan state! */
775
776 ss_priv->ss_chanmindwell = ticks + ss->ss_mindwell;
777 /* clear mindwell lock and initial channel change flush */
778 ss_priv->ss_iflags &= ~ISCAN_REP;
779
780 if (ss_priv->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)) {
781 taskqueue_cancel_timeout(ic->ic_tq, &ss_priv->ss_scan_curchan,
782 NULL);
783 goto end;
784 }
785
786 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: waiting\n",
787 __func__);
788 IEEE80211_UNLOCK(ic);
789 }
790
791 static void
scan_end(struct ieee80211_scan_state * ss,int scandone)792 scan_end(struct ieee80211_scan_state *ss, int scandone)
793 {
794 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
795 struct ieee80211vap *vap = ss->ss_vap;
796 struct ieee80211com *ic = ss->ss_ic;
797
798 IEEE80211_LOCK_ASSERT(ic);
799
800 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__);
801
802 if (ss_priv->ss_iflags & ISCAN_ABORT) {
803 scan_done(ss, scandone);
804 return;
805 }
806
807 IEEE80211_UNLOCK(ic);
808 ic->ic_scan_end(ic); /* notify driver */
809 IEEE80211_LOCK(ic);
810 /* XXX scan state can change! Re-validate scan state! */
811
812 /*
813 * Since a cancellation may have occurred during one of the
814 * driver calls (whilst unlocked), update scandone.
815 */
816 if ((scandone == 0) && ((ss_priv->ss_iflags & ISCAN_PAUSE) == ISCAN_CANCEL)) {
817 net80211_vap_printf(vap,
818 "%s: OOPS! scan cancelled during driver call (1) (ss_iflags=0x%x)!\n",
819 __func__, ss_priv->ss_iflags);
820 scandone = 1;
821 }
822
823 /*
824 * Record scan complete time. Note that we also do
825 * this when canceled so any background scan will
826 * not be restarted for a while.
827 */
828 if (scandone)
829 ic->ic_lastscan = ticks;
830 /* return to the bss channel */
831 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
832 ic->ic_curchan != ic->ic_bsschan) {
833 ieee80211_setupcurchan(ic, ic->ic_bsschan);
834 IEEE80211_UNLOCK(ic);
835 ic->ic_set_channel(ic);
836 ieee80211_radiotap_chan_change(ic);
837 IEEE80211_LOCK(ic);
838 }
839 /* clear internal flags and any indication of a pick */
840 ss_priv->ss_iflags &= ~ISCAN_REP;
841 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
842
843 /*
844 * If not canceled and scan completed, do post-processing.
845 * If the callback function returns 0, then it wants to
846 * continue/restart scanning. Unfortunately we needed to
847 * notify the driver to end the scan above to avoid having
848 * rx frames alter the scan candidate list.
849 */
850 if ((ss_priv->ss_iflags & ISCAN_CANCEL) == 0 &&
851 !ss->ss_ops->scan_end(ss, vap) &&
852 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
853 ieee80211_time_before(ticks + ss->ss_mindwell, ss_priv->ss_scanend)) {
854 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
855 "%s: done, restart "
856 "[ticks %u, dwell min %lu scanend %lu]\n",
857 __func__,
858 ticks, ss->ss_mindwell, ss_priv->ss_scanend);
859 ss->ss_next = 0; /* reset to beginning */
860 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
861 vap->iv_stats.is_scan_active++;
862 else
863 vap->iv_stats.is_scan_passive++;
864
865 ieee80211_notify_scan_done(vap);
866 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
867 ieee80211_runtask(ic, &ss_priv->ss_scan_start);
868 IEEE80211_UNLOCK(ic);
869 return;
870 }
871
872 /* past here, scandone is ``true'' if not in bg mode */
873 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
874 scandone = 1;
875
876 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
877 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
878 __func__, scandone ? "done" : "stopped",
879 ticks, ss->ss_mindwell, ss_priv->ss_scanend);
880
881 /*
882 * Since a cancellation may have occurred during one of the
883 * driver calls (whilst unlocked), update scandone.
884 */
885 if (scandone == 0 && (ss_priv->ss_iflags & ISCAN_PAUSE) == ISCAN_CANCEL) {
886 net80211_vap_printf(vap,
887 "%s: OOPS! scan cancelled during driver call (2) (ss_iflags=0x%x)!\n",
888 __func__, ss_priv->ss_iflags);
889 scandone = 1;
890 }
891
892 scan_done(ss, scandone);
893 }
894
895 static void
scan_done(struct ieee80211_scan_state * ss,int scandone)896 scan_done(struct ieee80211_scan_state *ss, int scandone)
897 {
898 struct scan_state *ss_priv = SCAN_PRIVATE(ss);
899 struct ieee80211com *ic = ss->ss_ic;
900 struct ieee80211vap *vap = ss->ss_vap;
901
902 IEEE80211_LOCK_ASSERT(ic);
903
904 /*
905 * Clear the SCAN bit first in case frames are
906 * pending on the station power save queue. If
907 * we defer this then the dispatch of the frames
908 * may generate a request to cancel scanning.
909 */
910 ic->ic_flags &= ~IEEE80211_F_SCAN;
911
912 /*
913 * Drop out of power save mode when a scan has
914 * completed. If this scan was prematurely terminated
915 * because it is a background scan then don't notify
916 * the ap; we'll either return to scanning after we
917 * receive the beacon frame or we'll drop out of power
918 * save mode because the beacon indicates we have frames
919 * waiting for us.
920 */
921 if (scandone) {
922 /*
923 * If we're not a scan offload device, come back out of
924 * station powersave. Offload devices handle this themselves.
925 */
926 if ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0)
927 vap->iv_sta_ps(vap, 0);
928 if (ss->ss_next >= ss->ss_last) {
929 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
930 "%s: Dropping out of scan; ss_next=%u, ss_last=%u\n",
931 __func__,
932 (uint32_t) ss->ss_next,
933 (uint32_t) ss->ss_last);
934 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
935 }
936
937 /* send 'scan done' event if not interrupted due to traffic. */
938 if (!(ss_priv->ss_iflags & ISCAN_INTERRUPT) ||
939 (ss->ss_next >= ss->ss_last))
940 ieee80211_notify_scan_done(vap);
941 }
942 ss_priv->ss_iflags &= ~(ISCAN_PAUSE | ISCAN_ABORT);
943 ss_priv->ss_scanend = 0;
944 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
945 IEEE80211_UNLOCK(ic);
946 #undef ISCAN_REP
947 }
948
949 /*
950 * Process a beacon or probe response frame.
951 */
952 static void
ieee80211_swscan_add_scan(struct ieee80211vap * vap,struct ieee80211_channel * curchan,const struct ieee80211_scanparams * sp,const struct ieee80211_frame * wh,int subtype,int rssi,int noise)953 ieee80211_swscan_add_scan(struct ieee80211vap *vap,
954 struct ieee80211_channel *curchan,
955 const struct ieee80211_scanparams *sp,
956 const struct ieee80211_frame *wh,
957 int subtype, int rssi, int noise)
958 {
959 struct ieee80211com *ic = vap->iv_ic;
960 struct ieee80211_scan_state *ss = ic->ic_scan;
961
962 /* XXX locking */
963 /*
964 * Frames received during startup are discarded to avoid
965 * using scan state setup on the initial entry to the timer
966 * callback. This can occur because the device may enable
967 * rx prior to our doing the initial channel change in the
968 * timer routine.
969 */
970 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
971 return;
972 #ifdef IEEE80211_DEBUG
973 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
974 ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
975 #endif
976 if (ss->ss_ops != NULL &&
977 ss->ss_ops->scan_add(ss, curchan, sp, wh, subtype, rssi, noise)) {
978 /*
979 * If we've reached the min dwell time terminate
980 * the timer so we'll switch to the next channel.
981 */
982 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
983 ieee80211_time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
984 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
985 "%s: chan %3d%c min dwell met (%u > %lu)\n",
986 __func__,
987 ieee80211_chan2ieee(ic, ic->ic_curchan),
988 ieee80211_channel_type_char(ic->ic_curchan),
989 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
990 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
991 /*
992 * NB: trigger at next clock tick or wait for the
993 * hardware.
994 */
995 ic->ic_scan_mindwell(ss);
996 }
997 }
998 }
999
1000 static struct ieee80211_scan_methods swscan_methods = {
1001 .sc_attach = ieee80211_swscan_attach,
1002 .sc_detach = ieee80211_swscan_detach,
1003 .sc_vattach = ieee80211_swscan_vattach,
1004 .sc_vdetach = ieee80211_swscan_vdetach,
1005 .sc_set_scan_duration = ieee80211_swscan_set_scan_duration,
1006 .sc_start_scan = ieee80211_swscan_start_scan,
1007 .sc_check_scan = ieee80211_swscan_check_scan,
1008 .sc_bg_scan = ieee80211_swscan_bg_scan,
1009 .sc_cancel_scan = ieee80211_swscan_cancel_scan,
1010 .sc_cancel_anyscan = ieee80211_swscan_cancel_anyscan,
1011 .sc_scan_next = ieee80211_swscan_scan_next,
1012 .sc_scan_done = ieee80211_swscan_scan_done,
1013 .sc_scan_probe_curchan = ieee80211_swscan_probe_curchan,
1014 .sc_add_scan = ieee80211_swscan_add_scan
1015 };
1016
1017 /*
1018 * Default scan attach method.
1019 */
1020 void
ieee80211_swscan_attach(struct ieee80211com * ic)1021 ieee80211_swscan_attach(struct ieee80211com *ic)
1022 {
1023 struct scan_state *ss;
1024
1025 /*
1026 * Setup the default methods
1027 */
1028 ic->ic_scan_methods = &swscan_methods;
1029
1030 /* Allocate initial scan state */
1031 ss = (struct scan_state *) IEEE80211_MALLOC(sizeof(struct scan_state),
1032 M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1033 if (ss == NULL) {
1034 ic->ic_scan = NULL;
1035 return;
1036 }
1037 TASK_INIT(&ss->ss_scan_start, 0, scan_start, ss);
1038 TIMEOUT_TASK_INIT(ic->ic_tq, &ss->ss_scan_curchan, 0,
1039 scan_curchan_task, ss);
1040
1041 ic->ic_scan = &ss->base;
1042 ss->base.ss_ic = ic;
1043
1044 ic->ic_scan_curchan = scan_curchan;
1045 ic->ic_scan_mindwell = scan_mindwell;
1046 }
1047