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