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