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