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