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