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