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