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