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