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