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