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