]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/net80211/ieee80211_scan.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / net80211 / ieee80211_scan.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_media.h>
44 #include <net/ethernet.h>
45
46 #include <net80211/ieee80211_var.h>
47
48 #include <net/bpf.h>
49
50 struct scan_state {
51         struct ieee80211_scan_state base;       /* public state */
52
53         u_int           ss_iflags;              /* flags used internally */
54 #define ISCAN_MINDWELL  0x0001          /* min dwell time reached */
55 #define ISCAN_DISCARD   0x0002          /* discard rx'd frames */
56 #define ISCAN_CANCEL    0x0004          /* cancel current scan */
57 #define ISCAN_ABORT     0x0008          /* end the scan immediately */
58         unsigned long   ss_chanmindwell;        /* min dwell on curchan */
59         unsigned long   ss_scanend;             /* time scan must stop */
60         u_int           ss_duration;            /* duration for next scan */
61         struct task     ss_scan_task;           /* scan execution */
62         struct cv       ss_scan_cv;             /* scan signal */
63         struct callout  ss_scan_timer;          /* scan timer */
64 };
65 #define SCAN_PRIVATE(ss)        ((struct scan_state *) ss)
66
67 /*
68  * Amount of time to go off-channel during a background
69  * scan.  This value should be large enough to catch most
70  * ap's but short enough that we can return on-channel
71  * before our listen interval expires.
72  *
73  * XXX tunable
74  * XXX check against configured listen interval
75  */
76 #define IEEE80211_SCAN_OFFCHANNEL       msecs_to_ticks(150)
77
78 /*
79  * Roaming-related defaults.  RSSI thresholds are as returned by the
80  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
81  * .5M units) or MCS.
82  */
83 /* rssi thresholds */
84 #define ROAM_RSSI_11A_DEFAULT           14      /* 11a bss */
85 #define ROAM_RSSI_11B_DEFAULT           14      /* 11b bss */
86 #define ROAM_RSSI_11BONLY_DEFAULT       14      /* 11b-only bss */
87 /* transmit rate thresholds */
88 #define ROAM_RATE_11A_DEFAULT           2*12    /* 11a bss */
89 #define ROAM_RATE_11B_DEFAULT           2*5     /* 11b bss */
90 #define ROAM_RATE_11BONLY_DEFAULT       2*1     /* 11b-only bss */
91 #define ROAM_RATE_HALF_DEFAULT          2*6     /* half-width 11a/g bss */
92 #define ROAM_RATE_QUARTER_DEFAULT       2*3     /* quarter-width 11a/g bss */
93 #define ROAM_MCS_11N_DEFAULT            (1 | IEEE80211_RATE_MCS) /* 11n bss */
94
95 static  void scan_curchan(struct ieee80211_scan_state *, unsigned long);
96 static  void scan_mindwell(struct ieee80211_scan_state *);
97 static  void scan_signal(void *);
98 static  void scan_task(void *, int);
99
100 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
101
102 void
103 ieee80211_scan_attach(struct ieee80211com *ic)
104 {
105         struct scan_state *ss;
106
107         ss = (struct scan_state *) malloc(sizeof(struct scan_state),
108                 M_80211_SCAN, M_NOWAIT | M_ZERO);
109         if (ss == NULL) {
110                 ic->ic_scan = NULL;
111                 return;
112         }
113         callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
114         cv_init(&ss->ss_scan_cv, "scan");
115         TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
116         ic->ic_scan = &ss->base;
117         ss->base.ss_ic = ic;
118
119         ic->ic_scan_curchan = scan_curchan;
120         ic->ic_scan_mindwell = scan_mindwell;
121 }
122
123 void
124 ieee80211_scan_detach(struct ieee80211com *ic)
125 {
126         struct ieee80211_scan_state *ss = ic->ic_scan;
127
128         if (ss != NULL) {
129                 IEEE80211_LOCK(ic);
130                 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
131                 scan_signal(ss);
132                 IEEE80211_UNLOCK(ic);
133                 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
134                 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
135                 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
136                     ("scan still running"));
137                 if (ss->ss_ops != NULL) {
138                         ss->ss_ops->scan_detach(ss);
139                         ss->ss_ops = NULL;
140                 }
141                 ic->ic_scan = NULL;
142                 free(SCAN_PRIVATE(ss), M_80211_SCAN);
143         }
144 }
145
146 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
147         [IEEE80211_MODE_11A]    = { .rssi = ROAM_RSSI_11A_DEFAULT,
148                                     .rate = ROAM_RATE_11A_DEFAULT },
149         [IEEE80211_MODE_11G]    = { .rssi = ROAM_RSSI_11B_DEFAULT,
150                                     .rate = ROAM_RATE_11B_DEFAULT },
151         [IEEE80211_MODE_11B]    = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
152                                     .rate = ROAM_RATE_11BONLY_DEFAULT },
153         [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
154                                     .rate = ROAM_RATE_11A_DEFAULT },
155         [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
156                                     .rate = ROAM_RATE_11A_DEFAULT },
157         [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
158                                     .rate = ROAM_RATE_11A_DEFAULT },
159         [IEEE80211_MODE_HALF]   = { .rssi = ROAM_RSSI_11A_DEFAULT,
160                                     .rate = ROAM_RATE_HALF_DEFAULT },
161         [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
162                                     .rate = ROAM_RATE_QUARTER_DEFAULT },
163         [IEEE80211_MODE_11NA]   = { .rssi = ROAM_RSSI_11A_DEFAULT,
164                                     .rate = ROAM_MCS_11N_DEFAULT },
165         [IEEE80211_MODE_11NG]   = { .rssi = ROAM_RSSI_11B_DEFAULT,
166                                     .rate = ROAM_MCS_11N_DEFAULT },
167 };
168
169 void
170 ieee80211_scan_vattach(struct ieee80211vap *vap)
171 {
172         vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
173         vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
174         vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
175
176         vap->iv_roaming = IEEE80211_ROAMING_AUTO;
177         memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
178 }
179
180 void
181 ieee80211_scan_vdetach(struct ieee80211vap *vap)
182 {
183         struct ieee80211com *ic = vap->iv_ic;
184         struct ieee80211_scan_state *ss;
185
186         IEEE80211_LOCK(ic);
187         ss = ic->ic_scan;
188         if (ss != NULL && ss->ss_vap == vap) {
189                 if (ic->ic_flags & IEEE80211_F_SCAN) {
190                         SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
191                         scan_signal(ss);
192                 }
193                 if (ss->ss_ops != NULL) {
194                         ss->ss_ops->scan_detach(ss);
195                         ss->ss_ops = NULL;
196                 }
197                 ss->ss_vap = NULL;
198         }
199         IEEE80211_UNLOCK(ic);
200 }
201
202 /*
203  * Simple-minded scanner module support.
204  */
205 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
206         "wlan_scan_sta",        /* IEEE80211_M_IBSS */
207         "wlan_scan_sta",        /* IEEE80211_M_STA */
208         "wlan_scan_wds",        /* IEEE80211_M_WDS */
209         "wlan_scan_sta",        /* IEEE80211_M_AHDEMO */
210         "wlan_scan_ap",         /* IEEE80211_M_HOSTAP */
211         "wlan_scan_monitor",    /* IEEE80211_M_MONITOR */
212         "wlan_scan_sta",        /* IEEE80211_M_MBSS */
213 };
214 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
215
216 const struct ieee80211_scanner *
217 ieee80211_scanner_get(enum ieee80211_opmode mode)
218 {
219         if (mode >= IEEE80211_OPMODE_MAX)
220                 return NULL;
221         if (scanners[mode] == NULL)
222                 ieee80211_load_module(scan_modnames[mode]);
223         return scanners[mode];
224 }
225
226 void
227 ieee80211_scanner_register(enum ieee80211_opmode mode,
228         const struct ieee80211_scanner *scan)
229 {
230         if (mode >= IEEE80211_OPMODE_MAX)
231                 return;
232         scanners[mode] = scan;
233 }
234
235 void
236 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
237         const struct ieee80211_scanner *scan)
238 {
239         if (mode >= IEEE80211_OPMODE_MAX)
240                 return;
241         if (scanners[mode] == scan)
242                 scanners[mode] = NULL;
243 }
244
245 void
246 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
247 {
248         int m;
249
250         for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
251                 if (scanners[m] == scan)
252                         scanners[m] = NULL;
253 }
254
255 /*
256  * Update common scanner state to reflect the current
257  * operating mode.  This is called when the state machine
258  * is transitioned to RUN state w/o scanning--e.g. when
259  * operating in monitor mode.  The purpose of this is to
260  * ensure later callbacks find ss_ops set to properly
261  * reflect current operating mode.
262  */
263 static void
264 scan_update_locked(struct ieee80211vap *vap,
265         const struct ieee80211_scanner *scan)
266 {
267         struct ieee80211com *ic = vap->iv_ic;
268         struct ieee80211_scan_state *ss = ic->ic_scan;
269
270         IEEE80211_LOCK_ASSERT(ic);
271
272 #ifdef IEEE80211_DEBUG
273         if (ss->ss_vap != vap || ss->ss_ops != scan) {
274                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
275                     "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
276                     __func__,
277                     ss->ss_vap != NULL ?
278                         ss->ss_vap->iv_ifp->if_xname : "none",
279                     ss->ss_vap != NULL ?
280                         ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
281                     vap->iv_ifp->if_xname,
282                     ieee80211_opmode_name[vap->iv_opmode]);
283         }
284 #endif
285         ss->ss_vap = vap;
286         if (ss->ss_ops != scan) {
287                 /*
288                  * Switch scanners; detach old, attach new.  Special
289                  * case where a single scan module implements multiple
290                  * policies by using different scan ops but a common
291                  * core.  We assume if the old and new attach methods
292                  * are identical then it's ok to just change ss_ops
293                  * and not flush the internal state of the module.
294                  */
295                 if (scan == NULL || ss->ss_ops == NULL ||
296                     ss->ss_ops->scan_attach != scan->scan_attach) {
297                         if (ss->ss_ops != NULL)
298                                 ss->ss_ops->scan_detach(ss);
299                         if (scan != NULL && !scan->scan_attach(ss)) {
300                                 /* XXX attach failure */
301                                 /* XXX stat+msg */
302                                 scan = NULL;
303                         }
304                 }
305                 ss->ss_ops = scan;
306         }
307 }
308
309 static char
310 channel_type(const struct ieee80211_channel *c)
311 {
312         if (IEEE80211_IS_CHAN_ST(c))
313                 return 'S';
314         if (IEEE80211_IS_CHAN_108A(c))
315                 return 'T';
316         if (IEEE80211_IS_CHAN_108G(c))
317                 return 'G';
318         if (IEEE80211_IS_CHAN_HT(c))
319                 return 'n';
320         if (IEEE80211_IS_CHAN_A(c))
321                 return 'a';
322         if (IEEE80211_IS_CHAN_ANYG(c))
323                 return 'g';
324         if (IEEE80211_IS_CHAN_B(c))
325                 return 'b';
326         return 'f';
327 }
328
329 void
330 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
331 {
332         struct ieee80211com *ic = ss->ss_ic;
333         const char *sep;
334         int i;
335
336         sep = "";
337         for (i = ss->ss_next; i < ss->ss_last; i++) {
338                 const struct ieee80211_channel *c = ss->ss_chans[i];
339
340                 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
341                         channel_type(c));
342                 sep = ", ";
343         }
344 }
345
346 #ifdef IEEE80211_DEBUG
347 static void
348 scan_dump(struct ieee80211_scan_state *ss)
349 {
350         struct ieee80211vap *vap = ss->ss_vap;
351
352         if_printf(vap->iv_ifp, "scan set ");
353         ieee80211_scan_dump_channels(ss);
354         printf(" dwell min %lums max %lums\n",
355             ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
356 }
357 #endif /* IEEE80211_DEBUG */
358
359 static void
360 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
361         int nssid, const struct ieee80211_scan_ssid ssids[])
362 {
363         if (nssid > IEEE80211_SCAN_MAX_SSID) {
364                 /* XXX printf */
365                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
366                     "%s: too many ssid %d, ignoring all of them\n",
367                     __func__, nssid);
368                 return;
369         }
370         memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
371         ss->ss_nssid = nssid;
372 }
373
374 /*
375  * Start a scan unless one is already going.
376  */
377 static int
378 start_scan_locked(const struct ieee80211_scanner *scan,
379         struct ieee80211vap *vap, int flags, u_int duration,
380         u_int mindwell, u_int maxdwell,
381         u_int nssid, const struct ieee80211_scan_ssid ssids[])
382 {
383         struct ieee80211com *ic = vap->iv_ic;
384         struct ieee80211_scan_state *ss = ic->ic_scan;
385
386         IEEE80211_LOCK_ASSERT(ic);
387
388         if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
389                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
390                     "%s: scan inhibited by pending channel change\n", __func__);
391         } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
392                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
393                     "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
394                     , __func__
395                     , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
396                     , duration, mindwell, maxdwell
397                     , ieee80211_phymode_name[vap->iv_des_mode]
398                     , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
399                     , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
400                     , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
401                     , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
402                     , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
403                     , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
404                 );
405
406                 scan_update_locked(vap, scan);
407                 if (ss->ss_ops != NULL) {
408                         if ((flags & IEEE80211_SCAN_NOSSID) == 0)
409                                 copy_ssid(vap, ss, nssid, ssids);
410
411                         /* NB: top 4 bits for internal use */
412                         ss->ss_flags = flags & 0xfff;
413                         if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
414                                 vap->iv_stats.is_scan_active++;
415                         else
416                                 vap->iv_stats.is_scan_passive++;
417                         if (flags & IEEE80211_SCAN_FLUSH)
418                                 ss->ss_ops->scan_flush(ss);
419
420                         /* NB: flush frames rx'd before 1st channel change */
421                         SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
422                         SCAN_PRIVATE(ss)->ss_duration = duration;
423                         ss->ss_next = 0;
424                         ss->ss_mindwell = mindwell;
425                         ss->ss_maxdwell = maxdwell;
426                         /* NB: scan_start must be before the scan runtask */
427                         ss->ss_ops->scan_start(ss, vap);
428 #ifdef IEEE80211_DEBUG
429                         if (ieee80211_msg_scan(vap))
430                                 scan_dump(ss);
431 #endif /* IEEE80211_DEBUG */
432                         ic->ic_flags |= IEEE80211_F_SCAN;
433                         ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
434                 }
435         } else {
436                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
437                     "%s: %s scan already in progress\n", __func__,
438                     ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
439         }
440         return (ic->ic_flags & IEEE80211_F_SCAN);
441 }
442
443 /*
444  * Start a scan unless one is already going.
445  */
446 int
447 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
448         u_int duration, u_int mindwell, u_int maxdwell,
449         u_int nssid, const struct ieee80211_scan_ssid ssids[])
450 {
451         struct ieee80211com *ic = vap->iv_ic;
452         const struct ieee80211_scanner *scan;
453         int result;
454
455         scan = ieee80211_scanner_get(vap->iv_opmode);
456         if (scan == NULL) {
457                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
458                     "%s: no scanner support for %s mode\n",
459                     __func__, ieee80211_opmode_name[vap->iv_opmode]);
460                 /* XXX stat */
461                 return 0;
462         }
463
464         IEEE80211_LOCK(ic);
465         result = start_scan_locked(scan, vap, flags, duration,
466             mindwell, maxdwell, nssid, ssids);
467         IEEE80211_UNLOCK(ic);
468
469         return result;
470 }
471
472 /*
473  * Check the scan cache for an ap/channel to use; if that
474  * fails then kick off a new scan.
475  */
476 int
477 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
478         u_int duration, u_int mindwell, u_int maxdwell,
479         u_int nssid, const struct ieee80211_scan_ssid ssids[])
480 {
481         struct ieee80211com *ic = vap->iv_ic;
482         struct ieee80211_scan_state *ss = ic->ic_scan;
483         const struct ieee80211_scanner *scan;
484         int result;
485
486         scan = ieee80211_scanner_get(vap->iv_opmode);
487         if (scan == NULL) {
488                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
489                     "%s: no scanner support for %s mode\n",
490                     __func__, vap->iv_opmode);
491                 /* XXX stat */
492                 return 0;
493         }
494
495         /*
496          * Check if there's a list of scan candidates already.
497          * XXX want more than the ap we're currently associated with
498          */
499
500         IEEE80211_LOCK(ic);
501         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
502             "%s: %s scan, %s%s%s%s%s\n"
503             , __func__
504             , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
505             , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
506             , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
507             , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
508             , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
509             , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
510         );
511
512         if (ss->ss_ops != scan) {
513                 /* XXX re-use cache contents? e.g. adhoc<->sta */
514                 flags |= IEEE80211_SCAN_FLUSH;
515         }
516         scan_update_locked(vap, scan);
517         if (ss->ss_ops != NULL) {
518                 /* XXX verify ss_ops matches vap->iv_opmode */
519                 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
520                         /*
521                          * Update the ssid list and mark flags so if
522                          * we call start_scan it doesn't duplicate work.
523                          */
524                         copy_ssid(vap, ss, nssid, ssids);
525                         flags |= IEEE80211_SCAN_NOSSID;
526                 }
527                 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
528                     (flags & IEEE80211_SCAN_FLUSH) == 0 &&
529                     time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
530                         /*
531                          * We're not currently scanning and the cache is
532                          * deemed hot enough to consult.  Lock out others
533                          * by marking IEEE80211_F_SCAN while we decide if
534                          * something is already in the scan cache we can
535                          * use.  Also discard any frames that might come
536                          * in while temporarily marked as scanning.
537                          */
538                         SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
539                         ic->ic_flags |= IEEE80211_F_SCAN;
540
541                         /* NB: need to use supplied flags in check */
542                         ss->ss_flags = flags & 0xff;
543                         result = ss->ss_ops->scan_end(ss, vap);
544
545                         ic->ic_flags &= ~IEEE80211_F_SCAN;
546                         SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
547                         if (result) {
548                                 ieee80211_notify_scan_done(vap);
549                                 IEEE80211_UNLOCK(ic);
550                                 return 1;
551                         }
552                 }
553         }
554         result = start_scan_locked(scan, vap, flags, duration,
555             mindwell, maxdwell, nssid, ssids);
556         IEEE80211_UNLOCK(ic);
557
558         return result;
559 }
560
561 /*
562  * Check the scan cache for an ap/channel to use; if that fails
563  * then kick off a scan using the current settings.
564  */
565 int
566 ieee80211_check_scan_current(struct ieee80211vap *vap)
567 {
568         return ieee80211_check_scan(vap,
569             IEEE80211_SCAN_ACTIVE,
570             IEEE80211_SCAN_FOREVER, 0, 0,
571             vap->iv_des_nssid, vap->iv_des_ssid);
572 }
573
574 /*
575  * Restart a previous scan.  If the previous scan completed
576  * then we start again using the existing channel list.
577  */
578 int
579 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
580 {
581         struct ieee80211com *ic = vap->iv_ic;
582         struct ieee80211_scan_state *ss = ic->ic_scan;
583         const struct ieee80211_scanner *scan;
584
585         scan = ieee80211_scanner_get(vap->iv_opmode);
586         if (scan == NULL) {
587                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
588                     "%s: no scanner support for %s mode\n",
589                     __func__, vap->iv_opmode);
590                 /* XXX stat */
591                 return 0;
592         }
593
594         IEEE80211_LOCK(ic);
595         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
596                 u_int duration;
597                 /*
598                  * Go off-channel for a fixed interval that is large
599                  * enough to catch most ap's but short enough that
600                  * we can return on-channel before our listen interval
601                  * expires.
602                  */
603                 duration = IEEE80211_SCAN_OFFCHANNEL;
604
605                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
606                     "%s: %s scan, ticks %u duration %lu\n", __func__,
607                     ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
608                     ticks, duration);
609
610                 scan_update_locked(vap, scan);
611                 if (ss->ss_ops != NULL) {
612                         ss->ss_vap = vap;
613                         /*
614                          * A background scan does not select a new sta; it
615                          * just refreshes the scan cache.  Also, indicate
616                          * the scan logic should follow the beacon schedule:
617                          * we go off-channel and scan for a while, then
618                          * return to the bss channel to receive a beacon,
619                          * then go off-channel again.  All during this time
620                          * we notify the ap we're in power save mode.  When
621                          * the scan is complete we leave power save mode.
622                          * If any beacon indicates there are frames pending
623                          * for us then we drop out of power save mode
624                          * (and background scan) automatically by way of the
625                          * usual sta power save logic.
626                          */
627                         ss->ss_flags |= IEEE80211_SCAN_NOPICK
628                                      |  IEEE80211_SCAN_BGSCAN
629                                      |  flags
630                                      ;
631                         /* if previous scan completed, restart */
632                         if (ss->ss_next >= ss->ss_last) {
633                                 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
634                                         vap->iv_stats.is_scan_active++;
635                                 else
636                                         vap->iv_stats.is_scan_passive++;
637                                 /*
638                                  * NB: beware of the scan cache being flushed;
639                                  *     if the channel list is empty use the
640                                  *     scan_start method to populate it.
641                                  */
642                                 ss->ss_next = 0;
643                                 if (ss->ss_last != 0)
644                                         ss->ss_ops->scan_restart(ss, vap);
645                                 else {
646                                         ss->ss_ops->scan_start(ss, vap);
647 #ifdef IEEE80211_DEBUG
648                                         if (ieee80211_msg_scan(vap))
649                                                 scan_dump(ss);
650 #endif /* IEEE80211_DEBUG */
651                                 }
652                         }
653                         /* NB: flush frames rx'd before 1st channel change */
654                         SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
655                         SCAN_PRIVATE(ss)->ss_duration = duration;
656                         ss->ss_maxdwell = duration;
657                         ic->ic_flags |= IEEE80211_F_SCAN;
658                         ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
659                         ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
660                 } else {
661                         /* XXX msg+stat */
662                 }
663         } else {
664                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
665                     "%s: %s scan already in progress\n", __func__,
666                     ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
667         }
668         IEEE80211_UNLOCK(ic);
669
670         /* NB: racey, does it matter? */
671         return (ic->ic_flags & IEEE80211_F_SCAN);
672 }
673
674 /*
675  * Cancel any scan currently going on for the specified vap.
676  */
677 void
678 ieee80211_cancel_scan(struct ieee80211vap *vap)
679 {
680         struct ieee80211com *ic = vap->iv_ic;
681         struct ieee80211_scan_state *ss = ic->ic_scan;
682
683         IEEE80211_LOCK(ic);
684         if ((ic->ic_flags & IEEE80211_F_SCAN) &&
685             ss->ss_vap == vap &&
686             (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
687                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
688                     "%s: cancel %s scan\n", __func__,
689                     ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
690                         "active" : "passive");
691
692                 /* clear bg scan NOPICK and mark cancel request */
693                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
694                 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
695                 /* wake up the scan task */
696                 scan_signal(ss);
697         }
698         IEEE80211_UNLOCK(ic);
699 }
700
701 /*
702  * Cancel any scan currently going on.
703  */
704 void
705 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
706 {
707         struct ieee80211com *ic = vap->iv_ic;
708         struct ieee80211_scan_state *ss = ic->ic_scan;
709
710         IEEE80211_LOCK(ic);
711         if ((ic->ic_flags & IEEE80211_F_SCAN) &&
712             (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
713                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
714                     "%s: cancel %s scan\n", __func__,
715                     ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
716                         "active" : "passive");
717
718                 /* clear bg scan NOPICK and mark cancel request */
719                 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
720                 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
721                 /* wake up the scan task */
722                 scan_signal(ss);
723         }
724         IEEE80211_UNLOCK(ic);
725 }
726
727 /*
728  * Public access to scan_next for drivers that manage
729  * scanning themselves (e.g. for firmware-based devices).
730  */
731 void
732 ieee80211_scan_next(struct ieee80211vap *vap)
733 {
734         struct ieee80211com *ic = vap->iv_ic;
735         struct ieee80211_scan_state *ss = ic->ic_scan;
736
737         /* wake up the scan task */
738         IEEE80211_LOCK(ic);
739         scan_signal(ss);
740         IEEE80211_UNLOCK(ic);
741 }
742
743 /*
744  * Public access to scan_next for drivers that are not able to scan single
745  * channels (e.g. for firmware-based devices).
746  */
747 void
748 ieee80211_scan_done(struct ieee80211vap *vap)
749 {
750         struct ieee80211com *ic = vap->iv_ic;
751         struct ieee80211_scan_state *ss;
752
753         IEEE80211_LOCK(ic);
754         ss = ic->ic_scan;
755         ss->ss_next = ss->ss_last; /* all channels are complete */
756         scan_signal(ss);
757         IEEE80211_UNLOCK(ic);
758 }
759
760 /*
761  * Probe the curent channel, if allowed, while scanning.
762  * If the channel is not marked passive-only then send
763  * a probe request immediately.  Otherwise mark state and
764  * listen for beacons on the channel; if we receive something
765  * then we'll transmit a probe request.
766  */
767 void
768 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
769 {
770         struct ieee80211com *ic = vap->iv_ic;
771         struct ieee80211_scan_state *ss = ic->ic_scan;
772         struct ifnet *ifp = vap->iv_ifp;
773         int i;
774
775         if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
776                 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
777                 return;
778         }
779         /*
780          * Send directed probe requests followed by any
781          * broadcast probe request.
782          * XXX remove dependence on ic/vap->iv_bss
783          */
784         for (i = 0; i < ss->ss_nssid; i++)
785                 ieee80211_send_probereq(vap->iv_bss,
786                         vap->iv_myaddr, ifp->if_broadcastaddr,
787                         ifp->if_broadcastaddr,
788                         ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
789         if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
790                 ieee80211_send_probereq(vap->iv_bss,
791                         vap->iv_myaddr, ifp->if_broadcastaddr,
792                         ifp->if_broadcastaddr,
793                         "", 0);
794 }
795
796 /*
797  * Scan curchan.  If this is an active scan and the channel
798  * is not marked passive then send probe request frame(s).
799  * Arrange for the channel change after maxdwell ticks.
800  */
801 static void
802 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
803 {
804         struct ieee80211vap *vap  = ss->ss_vap;
805
806         IEEE80211_LOCK(vap->iv_ic);
807         if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
808                 ieee80211_probe_curchan(vap, 0);
809         callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
810             maxdwell, scan_signal, ss);
811         IEEE80211_UNLOCK(vap->iv_ic);
812 }
813
814 static void
815 scan_signal(void *arg)
816 {
817         struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
818
819         IEEE80211_LOCK_ASSERT(ss->ss_ic);
820
821         cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
822 }
823
824 /*
825  * Handle mindwell requirements completed; initiate a channel
826  * change to the next channel asap.
827  */
828 static void
829 scan_mindwell(struct ieee80211_scan_state *ss)
830 {
831         struct ieee80211com *ic = ss->ss_ic;
832
833         IEEE80211_LOCK(ic);
834         scan_signal(ss);
835         IEEE80211_UNLOCK(ic);
836 }
837
838 static void
839 scan_task(void *arg, int pending)
840 {
841 #define ISCAN_REP       (ISCAN_MINDWELL | ISCAN_DISCARD)
842         struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
843         struct ieee80211vap *vap = ss->ss_vap;
844         struct ieee80211com *ic = ss->ss_ic;
845         struct ieee80211_channel *chan;
846         unsigned long maxdwell, scanend;
847         int scandone = 0;
848
849         IEEE80211_LOCK(ic);
850         if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
851             (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
852                 /* Cancelled before we started */
853                 goto done;
854         }
855
856         if (ss->ss_next == ss->ss_last) {
857                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
858                         "%s: no channels to scan\n", __func__);
859                 goto done;
860         }
861
862         if (vap->iv_opmode == IEEE80211_M_STA &&
863             vap->iv_state == IEEE80211_S_RUN) {
864                 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
865                         /* Enable station power save mode */
866                         ieee80211_sta_pwrsave(vap, 1);
867                         /*
868                          * Use an 1ms delay so the null data frame has a chance
869                          * to go out.
870                          * XXX Should use M_TXCB mechanism to eliminate this.
871                          */
872                         cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
873                             IEEE80211_LOCK_OBJ(ic), hz / 1000);
874                         if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
875                                 goto done;
876                 }
877         }
878
879         scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
880         IEEE80211_UNLOCK(ic);
881         ic->ic_scan_start(ic);          /* notify driver */
882         IEEE80211_LOCK(ic);
883
884         for (;;) {
885                 scandone = (ss->ss_next >= ss->ss_last) ||
886                     (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
887                 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
888                     (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
889                      time_after(ticks + ss->ss_mindwell, scanend))
890                         break;
891
892                 chan = ss->ss_chans[ss->ss_next++];
893
894                 /*
895                  * Watch for truncation due to the scan end time.
896                  */
897                 if (time_after(ticks + ss->ss_maxdwell, scanend))
898                         maxdwell = scanend - ticks;
899                 else
900                         maxdwell = ss->ss_maxdwell;
901
902                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
903                     "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
904                     __func__,
905                     ieee80211_chan2ieee(ic, ic->ic_curchan),
906                         channel_type(ic->ic_curchan),
907                     ieee80211_chan2ieee(ic, chan), channel_type(chan),
908                     (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
909                         (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
910                         "active" : "passive",
911                     ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
912
913                 /*
914                  * Potentially change channel and phy mode.
915                  */
916                 ic->ic_curchan = chan;
917                 ic->ic_rt = ieee80211_get_ratetable(chan);
918                 IEEE80211_UNLOCK(ic);
919                 /*
920                  * Perform the channel change and scan unlocked so the driver
921                  * may sleep. Once set_channel returns the hardware has
922                  * completed the channel change.
923                  */
924                 ic->ic_set_channel(ic);
925                 ieee80211_radiotap_chan_change(ic);
926
927                 /*
928                  * Scan curchan.  Drivers for "intelligent hardware"
929                  * override ic_scan_curchan to tell the device to do
930                  * the work.  Otherwise we manage the work outselves;
931                  * sending a probe request (as needed), and arming the
932                  * timeout to switch channels after maxdwell ticks.
933                  *
934                  * scan_curchan should only pause for the time required to
935                  * prepare/initiate the hardware for the scan (if at all), the
936                  * below condvar is used to sleep for the channels dwell time
937                  * and allows it to be signalled for abort.
938                  */
939                 ic->ic_scan_curchan(ss, maxdwell);
940                 IEEE80211_LOCK(ic);
941
942                 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
943                 /* clear mindwell lock and initial channel change flush */
944                 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
945
946                 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
947                         continue;
948
949                 /* Wait to be signalled to scan the next channel */
950                 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
951         }
952         if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
953                 goto done;
954
955         IEEE80211_UNLOCK(ic);
956         ic->ic_scan_end(ic);            /* notify driver */
957         IEEE80211_LOCK(ic);
958
959         /*
960          * Record scan complete time.  Note that we also do
961          * this when canceled so any background scan will
962          * not be restarted for a while.
963          */
964         if (scandone)
965                 ic->ic_lastscan = ticks;
966         /* return to the bss channel */
967         if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
968             ic->ic_curchan != ic->ic_bsschan) {
969                 ieee80211_setupcurchan(ic, ic->ic_bsschan);
970                 IEEE80211_UNLOCK(ic);
971                 ic->ic_set_channel(ic);
972                 ieee80211_radiotap_chan_change(ic);
973                 IEEE80211_LOCK(ic);
974         }
975         /* clear internal flags and any indication of a pick */
976         SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
977         ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
978
979         /*
980          * If not canceled and scan completed, do post-processing.
981          * If the callback function returns 0, then it wants to
982          * continue/restart scanning.  Unfortunately we needed to
983          * notify the driver to end the scan above to avoid having
984          * rx frames alter the scan candidate list.
985          */
986         if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
987             !ss->ss_ops->scan_end(ss, vap) &&
988             (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
989             time_before(ticks + ss->ss_mindwell, scanend)) {
990                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
991                     "%s: done, restart "
992                     "[ticks %u, dwell min %lu scanend %lu]\n",
993                     __func__,
994                     ticks, ss->ss_mindwell, scanend);
995                 ss->ss_next = 0;        /* reset to begining */
996                 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
997                         vap->iv_stats.is_scan_active++;
998                 else
999                         vap->iv_stats.is_scan_passive++;
1000
1001                 ss->ss_ops->scan_restart(ss, vap);      /* XXX? */
1002                 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
1003                 IEEE80211_UNLOCK(ic);
1004                 return;
1005         }
1006
1007         /* past here, scandone is ``true'' if not in bg mode */
1008         if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
1009                 scandone = 1;
1010
1011         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1012             "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
1013             __func__, scandone ? "done" : "stopped",
1014             ticks, ss->ss_mindwell, scanend);
1015
1016         /*
1017          * Clear the SCAN bit first in case frames are
1018          * pending on the station power save queue.  If
1019          * we defer this then the dispatch of the frames
1020          * may generate a request to cancel scanning.
1021          */
1022 done:
1023         ic->ic_flags &= ~IEEE80211_F_SCAN;
1024         /*
1025          * Drop out of power save mode when a scan has
1026          * completed.  If this scan was prematurely terminated
1027          * because it is a background scan then don't notify
1028          * the ap; we'll either return to scanning after we
1029          * receive the beacon frame or we'll drop out of power
1030          * save mode because the beacon indicates we have frames
1031          * waiting for us.
1032          */
1033         if (scandone) {
1034                 ieee80211_sta_pwrsave(vap, 0);
1035                 if (ss->ss_next >= ss->ss_last) {
1036                         ieee80211_notify_scan_done(vap);
1037                         ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
1038                 }
1039         }
1040         SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
1041         ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
1042         IEEE80211_UNLOCK(ic);
1043 #undef ISCAN_REP
1044 }
1045
1046 #ifdef IEEE80211_DEBUG
1047 static void
1048 dump_country(const uint8_t *ie)
1049 {
1050         const struct ieee80211_country_ie *cie =
1051            (const struct ieee80211_country_ie *) ie;
1052         int i, nbands, schan, nchan;
1053
1054         if (cie->len < 3) {
1055                 printf(" <bogus country ie, len %d>", cie->len);
1056                 return;
1057         }
1058         printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1059         nbands = (cie->len - 3) / sizeof(cie->band[0]);
1060         for (i = 0; i < nbands; i++) {
1061                 schan = cie->band[i].schan;
1062                 nchan = cie->band[i].nchan;
1063                 if (nchan != 1)
1064                         printf(" %u-%u,%u", schan, schan + nchan-1,
1065                             cie->band[i].maxtxpwr);
1066                 else
1067                         printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1068         }
1069         printf("]");
1070 }
1071
1072 static void
1073 dump_probe_beacon(uint8_t subtype, int isnew,
1074         const uint8_t mac[IEEE80211_ADDR_LEN],
1075         const struct ieee80211_scanparams *sp, int rssi)
1076 {
1077
1078         printf("[%s] %s%s on chan %u (bss chan %u) ",
1079             ether_sprintf(mac), isnew ? "new " : "",
1080             ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1081             sp->chan, sp->bchan);
1082         ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1083         printf(" rssi %d\n", rssi);
1084
1085         if (isnew) {
1086                 printf("[%s] caps 0x%x bintval %u erp 0x%x", 
1087                         ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1088                 if (sp->country != NULL)
1089                         dump_country(sp->country);
1090                 printf("\n");
1091         }
1092 }
1093 #endif /* IEEE80211_DEBUG */
1094
1095 /*
1096  * Process a beacon or probe response frame.
1097  */
1098 void
1099 ieee80211_add_scan(struct ieee80211vap *vap,
1100         const struct ieee80211_scanparams *sp,
1101         const struct ieee80211_frame *wh,
1102         int subtype, int rssi, int noise)
1103 {
1104         struct ieee80211com *ic = vap->iv_ic;
1105         struct ieee80211_scan_state *ss = ic->ic_scan;
1106
1107         /* XXX locking */
1108         /*
1109          * Frames received during startup are discarded to avoid
1110          * using scan state setup on the initial entry to the timer
1111          * callback.  This can occur because the device may enable
1112          * rx prior to our doing the initial channel change in the
1113          * timer routine.
1114          */
1115         if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
1116                 return;
1117 #ifdef IEEE80211_DEBUG
1118         if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1119                 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
1120 #endif
1121         if (ss->ss_ops != NULL &&
1122             ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
1123                 /*
1124                  * If we've reached the min dwell time terminate
1125                  * the timer so we'll switch to the next channel.
1126                  */
1127                 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
1128                     time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1129                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1130                             "%s: chan %3d%c min dwell met (%u > %lu)\n",
1131                             __func__,
1132                             ieee80211_chan2ieee(ic, ic->ic_curchan),
1133                                 channel_type(ic->ic_curchan),
1134                             ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
1135                         SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
1136                         /*
1137                          * NB: trigger at next clock tick or wait for the
1138                          * hardware.
1139                          */
1140                         ic->ic_scan_mindwell(ss);
1141                 }
1142         }
1143 }
1144
1145 /*
1146  * Timeout/age scan cache entries; called from sta timeout
1147  * timer (XXX should be self-contained).
1148  */
1149 void
1150 ieee80211_scan_timeout(struct ieee80211com *ic)
1151 {
1152         struct ieee80211_scan_state *ss = ic->ic_scan;
1153
1154         if (ss->ss_ops != NULL)
1155                 ss->ss_ops->scan_age(ss);
1156 }
1157
1158 /*
1159  * Mark a scan cache entry after a successful associate.
1160  */
1161 void
1162 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
1163 {
1164         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1165
1166         if (ss->ss_ops != NULL) {
1167                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
1168                         mac, "%s",  __func__);
1169                 ss->ss_ops->scan_assoc_success(ss, mac);
1170         }
1171 }
1172
1173 /*
1174  * Demerit a scan cache entry after failing to associate.
1175  */
1176 void
1177 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
1178         const uint8_t mac[], int reason)
1179 {
1180         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1181
1182         if (ss->ss_ops != NULL) {
1183                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
1184                         "%s: reason %u", __func__, reason);
1185                 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
1186         }
1187 }
1188
1189 /*
1190  * Iterate over the contents of the scan cache.
1191  */
1192 void
1193 ieee80211_scan_iterate(struct ieee80211vap *vap,
1194         ieee80211_scan_iter_func *f, void *arg)
1195 {
1196         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1197
1198         if (ss->ss_ops != NULL)
1199                 ss->ss_ops->scan_iterate(ss, f, arg);
1200 }
1201
1202 /*
1203  * Flush the contents of the scan cache.
1204  */
1205 void
1206 ieee80211_scan_flush(struct ieee80211vap *vap)
1207 {
1208         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1209
1210         if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1211                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
1212                 ss->ss_ops->scan_flush(ss);
1213         }
1214 }
1215
1216 /*
1217  * Check the scan cache for an ap/channel to use; if that
1218  * fails then kick off a new scan.
1219  */
1220 struct ieee80211_channel *
1221 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
1222 {
1223         struct ieee80211_scan_state *ss = ic->ic_scan;
1224
1225         IEEE80211_LOCK_ASSERT(ic);
1226
1227         if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1228                 /* XXX printf? */
1229                 return NULL;
1230         }
1231         if (ss->ss_ops->scan_pickchan == NULL) {
1232                 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1233                     "%s: scan module does not support picking a channel, "
1234                     "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1235                 return NULL;
1236         }
1237         return ss->ss_ops->scan_pickchan(ss, flags);
1238 }