Bring back r313037, with fixes for mips:
[freebsd.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/malloc.h>
39 #include <sys/condvar.h>
40  
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
47
48 #include <net80211/ieee80211_var.h>
49
50 /* XXX until it's implemented as attach ops */
51 #include <net80211/ieee80211_scan_sw.h>
52
53 #include <net/bpf.h>
54
55 /*
56  * Roaming-related defaults.  RSSI thresholds are as returned by the
57  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
58  * .5M units) or MCS.
59  */
60 /* rssi thresholds */
61 #define ROAM_RSSI_11A_DEFAULT           14      /* 11a bss */
62 #define ROAM_RSSI_11B_DEFAULT           14      /* 11b bss */
63 #define ROAM_RSSI_11BONLY_DEFAULT       14      /* 11b-only bss */
64 /* transmit rate thresholds */
65 #define ROAM_RATE_11A_DEFAULT           2*12    /* 11a bss */
66 #define ROAM_RATE_11B_DEFAULT           2*5     /* 11b bss */
67 #define ROAM_RATE_11BONLY_DEFAULT       2*1     /* 11b-only bss */
68 #define ROAM_RATE_HALF_DEFAULT          2*6     /* half-width 11a/g bss */
69 #define ROAM_RATE_QUARTER_DEFAULT       2*3     /* quarter-width 11a/g bss */
70 #define ROAM_MCS_11N_DEFAULT            (1 | IEEE80211_RATE_MCS) /* 11n bss */
71 #define ROAM_MCS_11AC_DEFAULT           (1 | IEEE80211_RATE_MCS) /* 11ac bss; XXX not used yet */
72
73 void
74 ieee80211_scan_attach(struct ieee80211com *ic)
75 {
76         /*
77          * If there's no scan method pointer, attach the
78          * swscan set as a default.
79          */
80         if (ic->ic_scan_methods == NULL)
81                 ieee80211_swscan_attach(ic);
82         else
83                 ic->ic_scan_methods->sc_attach(ic);
84 }
85
86 void
87 ieee80211_scan_detach(struct ieee80211com *ic)
88 {
89
90         /*
91          * Ideally we'd do the ss_ops detach call here;
92          * but then sc_detach() would need to be split in two.
93          *
94          * I'll do that later.
95          */
96         ic->ic_scan_methods->sc_detach(ic);
97 }
98
99 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
100         [IEEE80211_MODE_11A]    = { .rssi = ROAM_RSSI_11A_DEFAULT,
101                                     .rate = ROAM_RATE_11A_DEFAULT },
102         [IEEE80211_MODE_11G]    = { .rssi = ROAM_RSSI_11B_DEFAULT,
103                                     .rate = ROAM_RATE_11B_DEFAULT },
104         [IEEE80211_MODE_11B]    = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
105                                     .rate = ROAM_RATE_11BONLY_DEFAULT },
106         [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
107                                     .rate = ROAM_RATE_11A_DEFAULT },
108         [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
109                                     .rate = ROAM_RATE_11A_DEFAULT },
110         [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
111                                     .rate = ROAM_RATE_11A_DEFAULT },
112         [IEEE80211_MODE_HALF]   = { .rssi = ROAM_RSSI_11A_DEFAULT,
113                                     .rate = ROAM_RATE_HALF_DEFAULT },
114         [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
115                                     .rate = ROAM_RATE_QUARTER_DEFAULT },
116         [IEEE80211_MODE_11NA]   = { .rssi = ROAM_RSSI_11A_DEFAULT,
117                                     .rate = ROAM_MCS_11N_DEFAULT },
118         [IEEE80211_MODE_11NG]   = { .rssi = ROAM_RSSI_11B_DEFAULT,
119                                     .rate = ROAM_MCS_11N_DEFAULT },
120         [IEEE80211_MODE_VHT_2GHZ]       = { .rssi = ROAM_RSSI_11B_DEFAULT,
121                                     .rate = ROAM_MCS_11AC_DEFAULT },
122         [IEEE80211_MODE_VHT_5GHZ]       = { .rssi = ROAM_RSSI_11A_DEFAULT,
123                                     .rate = ROAM_MCS_11AC_DEFAULT },
124
125 };
126
127 void
128 ieee80211_scan_vattach(struct ieee80211vap *vap)
129 {
130         struct ieee80211com *ic = vap->iv_ic;
131
132         vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
133         vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
134         vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
135
136         vap->iv_roaming = IEEE80211_ROAMING_AUTO;
137         memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
138
139         ic->ic_scan_methods->sc_vattach(vap);
140 }
141
142 void
143 ieee80211_scan_vdetach(struct ieee80211vap *vap)
144 {
145         struct ieee80211com *ic = vap->iv_ic;
146         struct ieee80211_scan_state *ss;
147
148         IEEE80211_LOCK(ic);
149         ss = ic->ic_scan;
150
151         ic->ic_scan_methods->sc_vdetach(vap);
152
153         if (ss != NULL && ss->ss_vap == vap) {
154                 if (ss->ss_ops != NULL) {
155                         ss->ss_ops->scan_detach(ss);
156                         ss->ss_ops = NULL;
157                 }
158                 ss->ss_vap = NULL;
159         }
160         IEEE80211_UNLOCK(ic);
161 }
162
163 /*
164  * Simple-minded scanner module support.
165  */
166 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
167         "wlan_scan_sta",        /* IEEE80211_M_IBSS */
168         "wlan_scan_sta",        /* IEEE80211_M_STA */
169         "wlan_scan_wds",        /* IEEE80211_M_WDS */
170         "wlan_scan_sta",        /* IEEE80211_M_AHDEMO */
171         "wlan_scan_ap",         /* IEEE80211_M_HOSTAP */
172         "wlan_scan_monitor",    /* IEEE80211_M_MONITOR */
173         "wlan_scan_sta",        /* IEEE80211_M_MBSS */
174 };
175 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
176
177 const struct ieee80211_scanner *
178 ieee80211_scanner_get(enum ieee80211_opmode mode)
179 {
180         if (mode >= IEEE80211_OPMODE_MAX)
181                 return NULL;
182         if (scanners[mode] == NULL)
183                 ieee80211_load_module(scan_modnames[mode]);
184         return scanners[mode];
185 }
186
187 void
188 ieee80211_scanner_register(enum ieee80211_opmode mode,
189         const struct ieee80211_scanner *scan)
190 {
191         if (mode >= IEEE80211_OPMODE_MAX)
192                 return;
193         scanners[mode] = scan;
194 }
195
196 void
197 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
198         const struct ieee80211_scanner *scan)
199 {
200         if (mode >= IEEE80211_OPMODE_MAX)
201                 return;
202         if (scanners[mode] == scan)
203                 scanners[mode] = NULL;
204 }
205
206 void
207 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
208 {
209         int m;
210
211         for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
212                 if (scanners[m] == scan)
213                         scanners[m] = NULL;
214 }
215
216 /*
217  * Update common scanner state to reflect the current
218  * operating mode.  This is called when the state machine
219  * is transitioned to RUN state w/o scanning--e.g. when
220  * operating in monitor mode.  The purpose of this is to
221  * ensure later callbacks find ss_ops set to properly
222  * reflect current operating mode.
223  */
224 void
225 ieee80211_scan_update_locked(struct ieee80211vap *vap,
226         const struct ieee80211_scanner *scan)
227 {
228         struct ieee80211com *ic = vap->iv_ic;
229         struct ieee80211_scan_state *ss = ic->ic_scan;
230
231         IEEE80211_LOCK_ASSERT(ic);
232
233 #ifdef IEEE80211_DEBUG
234         if (ss->ss_vap != vap || ss->ss_ops != scan) {
235                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
236                     "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
237                     __func__,
238                     ss->ss_vap != NULL ?
239                         ss->ss_vap->iv_ifp->if_xname : "none",
240                     ss->ss_vap != NULL ?
241                         ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
242                     vap->iv_ifp->if_xname,
243                     ieee80211_opmode_name[vap->iv_opmode]);
244         }
245 #endif
246         ss->ss_vap = vap;
247         if (ss->ss_ops != scan) {
248                 /*
249                  * Switch scanners; detach old, attach new.  Special
250                  * case where a single scan module implements multiple
251                  * policies by using different scan ops but a common
252                  * core.  We assume if the old and new attach methods
253                  * are identical then it's ok to just change ss_ops
254                  * and not flush the internal state of the module.
255                  */
256                 if (scan == NULL || ss->ss_ops == NULL ||
257                     ss->ss_ops->scan_attach != scan->scan_attach) {
258                         if (ss->ss_ops != NULL)
259                                 ss->ss_ops->scan_detach(ss);
260                         if (scan != NULL && !scan->scan_attach(ss)) {
261                                 /* XXX attach failure */
262                                 /* XXX stat+msg */
263                                 scan = NULL;
264                         }
265                 }
266                 ss->ss_ops = scan;
267         }
268 }
269
270 void
271 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
272 {
273         struct ieee80211com *ic = ss->ss_ic;
274         const char *sep;
275         int i;
276
277         sep = "";
278         for (i = ss->ss_next; i < ss->ss_last; i++) {
279                 const struct ieee80211_channel *c = ss->ss_chans[i];
280
281                 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
282                     ieee80211_channel_type_char(c));
283                 sep = ", ";
284         }
285 }
286
287 #ifdef IEEE80211_DEBUG
288 void
289 ieee80211_scan_dump(struct ieee80211_scan_state *ss)
290 {
291         struct ieee80211vap *vap = ss->ss_vap;
292
293         if_printf(vap->iv_ifp, "scan set ");
294         ieee80211_scan_dump_channels(ss);
295         printf(" dwell min %lums max %lums\n",
296             ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
297 }
298 #endif /* IEEE80211_DEBUG */
299
300 void
301 ieee80211_scan_copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
302         int nssid, const struct ieee80211_scan_ssid ssids[])
303 {
304         if (nssid > IEEE80211_SCAN_MAX_SSID) {
305                 /* XXX printf */
306                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
307                     "%s: too many ssid %d, ignoring all of them\n",
308                     __func__, nssid);
309                 return;
310         }
311         memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
312         ss->ss_nssid = nssid;
313 }
314
315 /*
316  * Start a scan unless one is already going.
317  */
318 int
319 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
320         u_int duration, u_int mindwell, u_int maxdwell,
321         u_int nssid, const struct ieee80211_scan_ssid ssids[])
322 {
323         const struct ieee80211_scanner *scan;
324         struct ieee80211com *ic = vap->iv_ic;
325
326         scan = ieee80211_scanner_get(vap->iv_opmode);
327         if (scan == NULL) {
328                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
329                     "%s: no scanner support for %s mode\n",
330                     __func__, ieee80211_opmode_name[vap->iv_opmode]);
331                 /* XXX stat */
332                 return 0;
333         }
334
335         return ic->ic_scan_methods->sc_start_scan(scan, vap, flags, duration,
336             mindwell, maxdwell, nssid, ssids);
337 }
338
339 /*
340  * Check the scan cache for an ap/channel to use; if that
341  * fails then kick off a new scan.
342  */
343 int
344 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
345         u_int duration, u_int mindwell, u_int maxdwell,
346         u_int nssid, const struct ieee80211_scan_ssid ssids[])
347 {
348         struct ieee80211com *ic = vap->iv_ic;
349         struct ieee80211_scan_state *ss = ic->ic_scan;
350         const struct ieee80211_scanner *scan;
351         int result;
352
353         scan = ieee80211_scanner_get(vap->iv_opmode);
354         if (scan == NULL) {
355                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
356                     "%s: no scanner support for %s mode\n",
357                     __func__, vap->iv_opmode);
358                 /* XXX stat */
359                 return 0;
360         }
361
362         /*
363          * Check if there's a list of scan candidates already.
364          * XXX want more than the ap we're currently associated with
365          */
366
367         IEEE80211_LOCK(ic);
368         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
369             "%s: %s scan, %s%s%s%s%s\n"
370             , __func__
371             , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
372             , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
373             , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
374             , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
375             , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
376             , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
377         );
378
379         if (ss->ss_ops != scan) {
380                 /* XXX re-use cache contents? e.g. adhoc<->sta */
381                 flags |= IEEE80211_SCAN_FLUSH;
382         }
383
384         /*
385          * XXX TODO: separate things out a bit better.
386          */
387         ieee80211_scan_update_locked(vap, scan);
388
389         result = ic->ic_scan_methods->sc_check_scan(scan, vap, flags, duration,
390             mindwell, maxdwell, nssid, ssids);
391
392         IEEE80211_UNLOCK(ic);
393
394         return (result);
395 }
396
397 /*
398  * Check the scan cache for an ap/channel to use; if that fails
399  * then kick off a scan using the current settings.
400  */
401 int
402 ieee80211_check_scan_current(struct ieee80211vap *vap)
403 {
404         return ieee80211_check_scan(vap,
405             IEEE80211_SCAN_ACTIVE,
406             IEEE80211_SCAN_FOREVER, 0, 0,
407             vap->iv_des_nssid, vap->iv_des_ssid);
408 }
409
410 /*
411  * Restart a previous scan.  If the previous scan completed
412  * then we start again using the existing channel list.
413  */
414 int
415 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
416 {
417         struct ieee80211com *ic = vap->iv_ic;
418         const struct ieee80211_scanner *scan;
419
420         // IEEE80211_UNLOCK_ASSERT(sc);
421
422         scan = ieee80211_scanner_get(vap->iv_opmode);
423         if (scan == NULL) {
424                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
425                     "%s: no scanner support for %s mode\n",
426                     __func__, vap->iv_opmode);
427                 /* XXX stat */
428                 return 0;
429         }
430
431         /*
432          * XXX TODO: pull apart the bgscan logic into whatever
433          * belongs here and whatever belongs in the software
434          * scanner.
435          */
436         return (ic->ic_scan_methods->sc_bg_scan(scan, vap, flags));
437 }
438
439 /*
440  * Cancel any scan currently going on for the specified vap.
441  */
442 void
443 ieee80211_cancel_scan(struct ieee80211vap *vap)
444 {
445         struct ieee80211com *ic = vap->iv_ic;
446
447         ic->ic_scan_methods->sc_cancel_scan(vap);
448 }
449
450 /*
451  * Cancel any scan currently going on.
452  *
453  * This is called during normal 802.11 data path to cancel
454  * a scan so a newly arrived normal data packet can be sent.
455  */
456 void
457 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
458 {
459         struct ieee80211com *ic = vap->iv_ic;
460
461         ic->ic_scan_methods->sc_cancel_anyscan(vap);
462 }
463
464 /*
465  * Manually switch to the next channel in the channel list.
466  * Provided for drivers that manage scanning themselves
467  * (e.g. for firmware-based devices).
468  */
469 void
470 ieee80211_scan_next(struct ieee80211vap *vap)
471 {
472         struct ieee80211com *ic = vap->iv_ic;
473
474         ic->ic_scan_methods->sc_scan_next(vap);
475 }
476
477 /*
478  * Manually stop a scan that is currently running.
479  * Provided for drivers that are not able to scan single channels
480  * (e.g. for firmware-based devices).
481  */
482 void
483 ieee80211_scan_done(struct ieee80211vap *vap)
484 {
485         struct ieee80211com *ic = vap->iv_ic;
486         struct ieee80211_scan_state *ss;
487
488         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
489
490         IEEE80211_LOCK(ic);
491         ss = ic->ic_scan;
492         ss->ss_next = ss->ss_last; /* all channels are complete */
493
494         ic->ic_scan_methods->sc_scan_done(vap);
495
496         IEEE80211_UNLOCK(ic);
497 }
498
499 /*
500  * Probe the current channel, if allowed, while scanning.
501  * If the channel is not marked passive-only then send
502  * a probe request immediately.  Otherwise mark state and
503  * listen for beacons on the channel; if we receive something
504  * then we'll transmit a probe request.
505  */
506 void
507 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
508 {
509         struct ieee80211com *ic = vap->iv_ic;
510
511         if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
512                 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
513                 return;
514         }
515
516         ic->ic_scan_methods->sc_scan_probe_curchan(vap, force);
517 }
518
519 #ifdef IEEE80211_DEBUG
520 static void
521 dump_country(const uint8_t *ie)
522 {
523         const struct ieee80211_country_ie *cie =
524            (const struct ieee80211_country_ie *) ie;
525         int i, nbands, schan, nchan;
526
527         if (cie->len < 3) {
528                 printf(" <bogus country ie, len %d>", cie->len);
529                 return;
530         }
531         printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
532         nbands = (cie->len - 3) / sizeof(cie->band[0]);
533         for (i = 0; i < nbands; i++) {
534                 schan = cie->band[i].schan;
535                 nchan = cie->band[i].nchan;
536                 if (nchan != 1)
537                         printf(" %u-%u,%u", schan, schan + nchan-1,
538                             cie->band[i].maxtxpwr);
539                 else
540                         printf(" %u,%u", schan, cie->band[i].maxtxpwr);
541         }
542         printf("]");
543 }
544
545 void
546 ieee80211_scan_dump_probe_beacon(uint8_t subtype, int isnew,
547         const uint8_t mac[IEEE80211_ADDR_LEN],
548         const struct ieee80211_scanparams *sp, int rssi)
549 {
550
551         printf("[%s] %s%s on chan %u (bss chan %u) ",
552             ether_sprintf(mac), isnew ? "new " : "",
553             ieee80211_mgt_subtype_name(subtype), sp->chan, sp->bchan);
554         ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
555         printf(" rssi %d\n", rssi);
556
557         if (isnew) {
558                 printf("[%s] caps 0x%x bintval %u erp 0x%x", 
559                         ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
560                 if (sp->country != NULL)
561                         dump_country(sp->country);
562                 printf("\n");
563         }
564 }
565 #endif /* IEEE80211_DEBUG */
566
567 /*
568  * Process a beacon or probe response frame.
569  */
570 void
571 ieee80211_add_scan(struct ieee80211vap *vap,
572         struct ieee80211_channel *curchan,
573         const struct ieee80211_scanparams *sp,
574         const struct ieee80211_frame *wh,
575         int subtype, int rssi, int noise)
576 {
577         struct ieee80211com *ic = vap->iv_ic;
578
579         return (ic->ic_scan_methods->sc_add_scan(vap, curchan, sp, wh, subtype,
580             rssi, noise));
581 }
582
583 /*
584  * Timeout/age scan cache entries; called from sta timeout
585  * timer (XXX should be self-contained).
586  */
587 void
588 ieee80211_scan_timeout(struct ieee80211com *ic)
589 {
590         struct ieee80211_scan_state *ss = ic->ic_scan;
591
592         if (ss->ss_ops != NULL)
593                 ss->ss_ops->scan_age(ss);
594 }
595
596 /*
597  * Mark a scan cache entry after a successful associate.
598  */
599 void
600 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
601 {
602         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
603
604         if (ss->ss_ops != NULL) {
605                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
606                         mac, "%s",  __func__);
607                 ss->ss_ops->scan_assoc_success(ss, mac);
608         }
609 }
610
611 /*
612  * Demerit a scan cache entry after failing to associate.
613  */
614 void
615 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
616         const uint8_t mac[], int reason)
617 {
618         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
619
620         if (ss->ss_ops != NULL) {
621                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
622                         "%s: reason %u", __func__, reason);
623                 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
624         }
625 }
626
627 /*
628  * Iterate over the contents of the scan cache.
629  */
630 void
631 ieee80211_scan_iterate(struct ieee80211vap *vap,
632         ieee80211_scan_iter_func *f, void *arg)
633 {
634         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
635
636         if (ss->ss_ops != NULL)
637                 ss->ss_ops->scan_iterate(ss, f, arg);
638 }
639
640 /*
641  * Flush the contents of the scan cache.
642  */
643 void
644 ieee80211_scan_flush(struct ieee80211vap *vap)
645 {
646         struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
647
648         if (ss->ss_ops != NULL && ss->ss_vap == vap) {
649                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
650                 ss->ss_ops->scan_flush(ss);
651         }
652 }
653
654 /*
655  * Check the scan cache for an ap/channel to use; if that
656  * fails then kick off a new scan.
657  */
658 struct ieee80211_channel *
659 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
660 {
661         struct ieee80211_scan_state *ss = ic->ic_scan;
662
663         IEEE80211_LOCK_ASSERT(ic);
664
665         if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
666                 /* XXX printf? */
667                 return NULL;
668         }
669         if (ss->ss_ops->scan_pickchan == NULL) {
670                 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
671                     "%s: scan module does not support picking a channel, "
672                     "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
673                 return NULL;
674         }
675         return ss->ss_ops->scan_pickchan(ss, flags);
676 }