xref: /freebsd/sys/net80211/ieee80211_scan_sw.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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/malloc.h>
39 #include <sys/condvar.h>
40 
41 #include <sys/socket.h>
42 
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
47 
48 #include <net80211/ieee80211_var.h>
49 
50 #include <net80211/ieee80211_scan_sw.h>
51 
52 #include <net/bpf.h>
53 
54 struct scan_state {
55 	struct ieee80211_scan_state base;	/* public state */
56 
57 	u_int		ss_iflags;		/* flags used internally */
58 #define	ISCAN_MINDWELL 	0x0001		/* min dwell time reached */
59 #define	ISCAN_DISCARD	0x0002		/* discard rx'd frames */
60 #define	ISCAN_CANCEL	0x0004		/* cancel current scan */
61 #define	ISCAN_ABORT	0x0008		/* end the scan immediately */
62 	unsigned long	ss_chanmindwell;	/* min dwell on curchan */
63 	unsigned long	ss_scanend;		/* time scan must stop */
64 	u_int		ss_duration;		/* duration for next scan */
65 	struct task	ss_scan_task;		/* scan execution */
66 	struct cv	ss_scan_cv;		/* scan signal */
67 	struct callout	ss_scan_timer;		/* scan timer */
68 };
69 #define	SCAN_PRIVATE(ss)	((struct scan_state *) ss)
70 
71 /*
72  * Amount of time to go off-channel during a background
73  * scan.  This value should be large enough to catch most
74  * ap's but short enough that we can return on-channel
75  * before our listen interval expires.
76  *
77  * XXX tunable
78  * XXX check against configured listen interval
79  */
80 #define	IEEE80211_SCAN_OFFCHANNEL	msecs_to_ticks(150)
81 
82 /*
83  * Roaming-related defaults.  RSSI thresholds are as returned by the
84  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
85  * .5M units) or MCS.
86  */
87 /* rssi thresholds */
88 #define	ROAM_RSSI_11A_DEFAULT		14	/* 11a bss */
89 #define	ROAM_RSSI_11B_DEFAULT		14	/* 11b bss */
90 #define	ROAM_RSSI_11BONLY_DEFAULT	14	/* 11b-only bss */
91 /* transmit rate thresholds */
92 #define	ROAM_RATE_11A_DEFAULT		2*12	/* 11a bss */
93 #define	ROAM_RATE_11B_DEFAULT		2*5	/* 11b bss */
94 #define	ROAM_RATE_11BONLY_DEFAULT	2*1	/* 11b-only bss */
95 #define	ROAM_RATE_HALF_DEFAULT		2*6	/* half-width 11a/g bss */
96 #define	ROAM_RATE_QUARTER_DEFAULT	2*3	/* quarter-width 11a/g bss */
97 #define	ROAM_MCS_11N_DEFAULT		(1 | IEEE80211_RATE_MCS) /* 11n bss */
98 
99 static	void scan_curchan(struct ieee80211_scan_state *, unsigned long);
100 static	void scan_mindwell(struct ieee80211_scan_state *);
101 static	void scan_signal(void *);
102 static	void scan_task(void *, int);
103 
104 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
105 
106 static void
107 ieee80211_swscan_detach(struct ieee80211com *ic)
108 {
109 	struct ieee80211_scan_state *ss = ic->ic_scan;
110 
111 	if (ss != NULL) {
112 		IEEE80211_LOCK(ic);
113 		SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
114 		scan_signal(ss);
115 		IEEE80211_UNLOCK(ic);
116 		ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
117 		callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
118 		KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
119 		    ("scan still running"));
120 
121 		/*
122 		 * For now, do the ss_ops detach here rather
123 		 * than ieee80211_scan_detach().
124 		 *
125 		 * I'll figure out how to cleanly split things up
126 		 * at a later date.
127 		 */
128 		if (ss->ss_ops != NULL) {
129 			ss->ss_ops->scan_detach(ss);
130 			ss->ss_ops = NULL;
131 		}
132 		ic->ic_scan = NULL;
133 		IEEE80211_FREE(SCAN_PRIVATE(ss), M_80211_SCAN);
134 	}
135 }
136 
137 static void
138 ieee80211_swscan_vattach(struct ieee80211vap *vap)
139 {
140 	/* nothing to do for now */
141 	/*
142 	 * TODO: all of the vap scan calls should be methods!
143 	 */
144 
145 }
146 
147 static void
148 ieee80211_swscan_vdetach(struct ieee80211vap *vap)
149 {
150 	struct ieee80211com *ic = vap->iv_ic;
151 	struct ieee80211_scan_state *ss;
152 
153 	IEEE80211_LOCK_ASSERT(ic);
154 	ss = ic->ic_scan;
155 	if (ss != NULL && ss->ss_vap == vap) {
156 		if (ic->ic_flags & IEEE80211_F_SCAN) {
157 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
158 			scan_signal(ss);
159 		}
160 	}
161 }
162 
163 static void
164 ieee80211_swscan_set_scan_duration(struct ieee80211vap *vap, u_int duration)
165 {
166 	struct ieee80211com *ic = vap->iv_ic;
167 	struct ieee80211_scan_state *ss = ic->ic_scan;
168 
169 	IEEE80211_LOCK_ASSERT(ic);
170 
171 	/* NB: flush frames rx'd before 1st channel change */
172 	SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
173 	SCAN_PRIVATE(ss)->ss_duration = duration;
174 }
175 
176 /*
177  * Start a scan unless one is already going.
178  */
179 static int
180 ieee80211_swscan_start_scan_locked(const struct ieee80211_scanner *scan,
181 	struct ieee80211vap *vap, int flags, u_int duration,
182 	u_int mindwell, u_int maxdwell,
183 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
184 {
185 	struct ieee80211com *ic = vap->iv_ic;
186 	struct ieee80211_scan_state *ss = ic->ic_scan;
187 
188 	IEEE80211_LOCK_ASSERT(ic);
189 
190 	if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
191 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
192 		    "%s: scan inhibited by pending channel change\n", __func__);
193 	} else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
194 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
195 		    "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
196 		    , __func__
197 		    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
198 		    , duration, mindwell, maxdwell
199 		    , ieee80211_phymode_name[vap->iv_des_mode]
200 		    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
201 		    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
202 		    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
203 		    , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
204 		    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
205 		    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
206 		);
207 
208 		ieee80211_scan_update_locked(vap, scan);
209 		if (ss->ss_ops != NULL) {
210 			if ((flags & IEEE80211_SCAN_NOSSID) == 0)
211 				ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
212 
213 			/* NB: top 4 bits for internal use */
214 			ss->ss_flags = flags & 0xfff;
215 			if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
216 				vap->iv_stats.is_scan_active++;
217 			else
218 				vap->iv_stats.is_scan_passive++;
219 			if (flags & IEEE80211_SCAN_FLUSH)
220 				ss->ss_ops->scan_flush(ss);
221 			if (flags & IEEE80211_SCAN_BGSCAN)
222 				ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
223 
224 			/* Set duration for this particular scan */
225 			ieee80211_swscan_set_scan_duration(vap, duration);
226 
227 			ss->ss_next = 0;
228 			ss->ss_mindwell = mindwell;
229 			ss->ss_maxdwell = maxdwell;
230 			/* NB: scan_start must be before the scan runtask */
231 			ss->ss_ops->scan_start(ss, vap);
232 #ifdef IEEE80211_DEBUG
233 			if (ieee80211_msg_scan(vap))
234 				ieee80211_scan_dump(ss);
235 #endif /* IEEE80211_DEBUG */
236 			ic->ic_flags |= IEEE80211_F_SCAN;
237 
238 			/* Start scan task */
239 			ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
240 		}
241 		return 1;
242 	} else {
243 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
244 		    "%s: %s scan already in progress\n", __func__,
245 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
246 	}
247 	return 0;
248 }
249 
250 
251 /*
252  * Start a scan unless one is already going.
253  *
254  * Called without the comlock held; grab the comlock as appropriate.
255  */
256 static int
257 ieee80211_swscan_start_scan(const struct ieee80211_scanner *scan,
258     struct ieee80211vap *vap, int flags,
259     u_int duration, u_int mindwell, u_int maxdwell,
260     u_int nssid, const struct ieee80211_scan_ssid ssids[])
261 {
262 	struct ieee80211com *ic = vap->iv_ic;
263 	int result;
264 
265 	IEEE80211_UNLOCK_ASSERT(ic);
266 
267 	IEEE80211_LOCK(ic);
268 	result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
269 	    mindwell, maxdwell, nssid, ssids);
270 	IEEE80211_UNLOCK(ic);
271 
272 	return result;
273 }
274 
275 /*
276  * Check the scan cache for an ap/channel to use; if that
277  * fails then kick off a new scan.
278  *
279  * Called with the comlock held.
280  *
281  * XXX TODO: split out!
282  */
283 static int
284 ieee80211_swscan_check_scan(const struct ieee80211_scanner *scan,
285     struct ieee80211vap *vap, int flags,
286     u_int duration, u_int mindwell, u_int maxdwell,
287     u_int nssid, const struct ieee80211_scan_ssid ssids[])
288 {
289 	struct ieee80211com *ic = vap->iv_ic;
290 	struct ieee80211_scan_state *ss = ic->ic_scan;
291 	int result;
292 
293 	IEEE80211_LOCK_ASSERT(ic);
294 
295 	if (ss->ss_ops != NULL) {
296 		/* XXX verify ss_ops matches vap->iv_opmode */
297 		if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
298 			/*
299 			 * Update the ssid list and mark flags so if
300 			 * we call start_scan it doesn't duplicate work.
301 			 */
302 			ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
303 			flags |= IEEE80211_SCAN_NOSSID;
304 		}
305 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
306 		    (flags & IEEE80211_SCAN_FLUSH) == 0 &&
307 		    time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
308 			/*
309 			 * We're not currently scanning and the cache is
310 			 * deemed hot enough to consult.  Lock out others
311 			 * by marking IEEE80211_F_SCAN while we decide if
312 			 * something is already in the scan cache we can
313 			 * use.  Also discard any frames that might come
314 			 * in while temporarily marked as scanning.
315 			 */
316 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
317 			ic->ic_flags |= IEEE80211_F_SCAN;
318 
319 			/* NB: need to use supplied flags in check */
320 			ss->ss_flags = flags & 0xff;
321 			result = ss->ss_ops->scan_end(ss, vap);
322 
323 			ic->ic_flags &= ~IEEE80211_F_SCAN;
324 			SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
325 			if (result) {
326 				ieee80211_notify_scan_done(vap);
327 				return 1;
328 			}
329 		}
330 	}
331 	result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
332 	    mindwell, maxdwell, nssid, ssids);
333 
334 	return result;
335 }
336 
337 /*
338  * Restart a previous scan.  If the previous scan completed
339  * then we start again using the existing channel list.
340  */
341 static int
342 ieee80211_swscan_bg_scan(const struct ieee80211_scanner *scan,
343     struct ieee80211vap *vap, int flags)
344 {
345 	struct ieee80211com *ic = vap->iv_ic;
346 	struct ieee80211_scan_state *ss = ic->ic_scan;
347 
348 	/* XXX assert unlocked? */
349 	// IEEE80211_UNLOCK_ASSERT(ic);
350 
351 	IEEE80211_LOCK(ic);
352 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
353 		u_int duration;
354 		/*
355 		 * Go off-channel for a fixed interval that is large
356 		 * enough to catch most ap's but short enough that
357 		 * we can return on-channel before our listen interval
358 		 * expires.
359 		 */
360 		duration = IEEE80211_SCAN_OFFCHANNEL;
361 
362 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
363 		    "%s: %s scan, ticks %u duration %u\n", __func__,
364 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
365 		    ticks, duration);
366 
367 		ieee80211_scan_update_locked(vap, scan);
368 		if (ss->ss_ops != NULL) {
369 			ss->ss_vap = vap;
370 			/*
371 			 * A background scan does not select a new sta; it
372 			 * just refreshes the scan cache.  Also, indicate
373 			 * the scan logic should follow the beacon schedule:
374 			 * we go off-channel and scan for a while, then
375 			 * return to the bss channel to receive a beacon,
376 			 * then go off-channel again.  All during this time
377 			 * we notify the ap we're in power save mode.  When
378 			 * the scan is complete we leave power save mode.
379 			 * If any beacon indicates there are frames pending
380 			 * for us then we drop out of power save mode
381 			 * (and background scan) automatically by way of the
382 			 * usual sta power save logic.
383 			 */
384 			ss->ss_flags |= IEEE80211_SCAN_NOPICK
385 				     |  IEEE80211_SCAN_BGSCAN
386 				     |  flags
387 				     ;
388 			/* if previous scan completed, restart */
389 			if (ss->ss_next >= ss->ss_last) {
390 				if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
391 					vap->iv_stats.is_scan_active++;
392 				else
393 					vap->iv_stats.is_scan_passive++;
394 				/*
395 				 * NB: beware of the scan cache being flushed;
396 				 *     if the channel list is empty use the
397 				 *     scan_start method to populate it.
398 				 */
399 				ss->ss_next = 0;
400 				if (ss->ss_last != 0)
401 					ss->ss_ops->scan_restart(ss, vap);
402 				else {
403 					ss->ss_ops->scan_start(ss, vap);
404 #ifdef IEEE80211_DEBUG
405 					if (ieee80211_msg_scan(vap))
406 						ieee80211_scan_dump(ss);
407 #endif /* IEEE80211_DEBUG */
408 				}
409 			}
410 			ieee80211_swscan_set_scan_duration(vap, duration);
411 			ss->ss_maxdwell = duration;
412 			ic->ic_flags |= IEEE80211_F_SCAN;
413 			ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
414 			ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
415 		} else {
416 			/* XXX msg+stat */
417 		}
418 	} else {
419 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
420 		    "%s: %s scan already in progress\n", __func__,
421 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
422 	}
423 	IEEE80211_UNLOCK(ic);
424 
425 	/* NB: racey, does it matter? */
426 	return (ic->ic_flags & IEEE80211_F_SCAN);
427 }
428 
429 /*
430  * Cancel any scan currently going on for the specified vap.
431  */
432 static void
433 ieee80211_swscan_cancel_scan(struct ieee80211vap *vap)
434 {
435 	struct ieee80211com *ic = vap->iv_ic;
436 	struct ieee80211_scan_state *ss = ic->ic_scan;
437 
438 	IEEE80211_LOCK(ic);
439 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
440 	    ss->ss_vap == vap &&
441 	    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
442 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
443 		    "%s: cancel %s scan\n", __func__,
444 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
445 			"active" : "passive");
446 
447 		/* clear bg scan NOPICK and mark cancel request */
448 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
449 		SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
450 		/* wake up the scan task */
451 		scan_signal(ss);
452 	} else {
453 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
454 		    "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
455 		        __func__,
456 			!! (ic->ic_flags & IEEE80211_F_SCAN),
457 			(ss->ss_vap == vap ? "match" : "nomatch"),
458 			!! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
459 	}
460 	IEEE80211_UNLOCK(ic);
461 }
462 
463 /*
464  * Cancel any scan currently going on.
465  */
466 static void
467 ieee80211_swscan_cancel_anyscan(struct ieee80211vap *vap)
468 {
469 	struct ieee80211com *ic = vap->iv_ic;
470 	struct ieee80211_scan_state *ss = ic->ic_scan;
471 
472 	IEEE80211_LOCK(ic);
473 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
474 	    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
475 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
476 		    "%s: cancel %s scan\n", __func__,
477 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
478 			"active" : "passive");
479 
480 		/* clear bg scan NOPICK and mark cancel request */
481 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
482 		SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
483 		/* wake up the scan task */
484 		scan_signal(ss);
485 	} else {
486 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
487 		    "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
488 		        __func__,
489 			!! (ic->ic_flags & IEEE80211_F_SCAN),
490 			(ss->ss_vap == vap ? "match" : "nomatch"),
491 			!! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
492 	}
493 	IEEE80211_UNLOCK(ic);
494 }
495 
496 /*
497  * Public access to scan_next for drivers that manage
498  * scanning themselves (e.g. for firmware-based devices).
499  */
500 static void
501 ieee80211_swscan_scan_next(struct ieee80211vap *vap)
502 {
503 	struct ieee80211com *ic = vap->iv_ic;
504 	struct ieee80211_scan_state *ss = ic->ic_scan;
505 
506 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
507 
508 	/* wake up the scan task */
509 	IEEE80211_LOCK(ic);
510 	scan_signal(ss);
511 	IEEE80211_UNLOCK(ic);
512 }
513 
514 /*
515  * Public access to scan_next for drivers that are not able to scan single
516  * channels (e.g. for firmware-based devices).
517  */
518 static void
519 ieee80211_swscan_scan_done(struct ieee80211vap *vap)
520 {
521 	struct ieee80211com *ic = vap->iv_ic;
522 	struct ieee80211_scan_state *ss;
523 
524 	IEEE80211_LOCK_ASSERT(ic);
525 
526 	ss = ic->ic_scan;
527 	scan_signal(ss);
528 }
529 
530 /*
531  * Probe the curent channel, if allowed, while scanning.
532  * If the channel is not marked passive-only then send
533  * a probe request immediately.  Otherwise mark state and
534  * listen for beacons on the channel; if we receive something
535  * then we'll transmit a probe request.
536  */
537 static void
538 ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, int force)
539 {
540 	struct ieee80211com *ic = vap->iv_ic;
541 	struct ieee80211_scan_state *ss = ic->ic_scan;
542 	struct ifnet *ifp = vap->iv_ifp;
543 	int i;
544 
545 	/*
546 	 * Send directed probe requests followed by any
547 	 * broadcast probe request.
548 	 * XXX remove dependence on ic/vap->iv_bss
549 	 */
550 	for (i = 0; i < ss->ss_nssid; i++)
551 		ieee80211_send_probereq(vap->iv_bss,
552 			vap->iv_myaddr, ifp->if_broadcastaddr,
553 			ifp->if_broadcastaddr,
554 			ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
555 	if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
556 		ieee80211_send_probereq(vap->iv_bss,
557 			vap->iv_myaddr, ifp->if_broadcastaddr,
558 			ifp->if_broadcastaddr,
559 			"", 0);
560 }
561 
562 /*
563  * Scan curchan.  If this is an active scan and the channel
564  * is not marked passive then send probe request frame(s).
565  * Arrange for the channel change after maxdwell ticks.
566  */
567 static void
568 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
569 {
570 	struct ieee80211vap *vap  = ss->ss_vap;
571 
572 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
573 	    "%s: calling; maxdwell=%lu\n",
574 	    __func__,
575 	    maxdwell);
576 	IEEE80211_LOCK(vap->iv_ic);
577 	if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
578 		ieee80211_probe_curchan(vap, 0);
579 	callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
580 	    maxdwell, scan_signal, ss);
581 	IEEE80211_UNLOCK(vap->iv_ic);
582 }
583 
584 static void
585 scan_signal(void *arg)
586 {
587 	struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
588 
589 	IEEE80211_LOCK_ASSERT(ss->ss_ic);
590 	cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
591 }
592 
593 /*
594  * Handle mindwell requirements completed; initiate a channel
595  * change to the next channel asap.
596  */
597 static void
598 scan_mindwell(struct ieee80211_scan_state *ss)
599 {
600 	struct ieee80211com *ic = ss->ss_ic;
601 
602 	IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
603 
604 	IEEE80211_LOCK(ic);
605 	scan_signal(ss);
606 	IEEE80211_UNLOCK(ic);
607 }
608 
609 static void
610 scan_task(void *arg, int pending)
611 {
612 #define	ISCAN_REP	(ISCAN_MINDWELL | ISCAN_DISCARD)
613 	struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
614 	struct ieee80211vap *vap = ss->ss_vap;
615 	struct ieee80211com *ic = ss->ss_ic;
616 	struct ieee80211_channel *chan;
617 	unsigned long maxdwell, scanend;
618 	int scandone = 0;
619 
620 	IEEE80211_LOCK(ic);
621 	if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
622 	    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
623 		/* Cancelled before we started */
624 		goto done;
625 	}
626 
627 	if (ss->ss_next == ss->ss_last) {
628 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
629 			"%s: no channels to scan\n", __func__);
630 		scandone = 1;
631 		goto done;
632 	}
633 
634 	if (vap->iv_opmode == IEEE80211_M_STA &&
635 	    vap->iv_state == IEEE80211_S_RUN) {
636 		if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
637 			/* Enable station power save mode */
638 			vap->iv_sta_ps(vap, 1);
639 			/*
640 			 * Use an 1ms delay so the null data frame has a chance
641 			 * to go out.
642 			 * XXX Should use M_TXCB mechanism to eliminate this.
643 			 */
644 			cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
645 			    IEEE80211_LOCK_OBJ(ic), msecs_to_ticks(1));
646 			if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
647 				goto done;
648 		}
649 	}
650 
651 	scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
652 
653 	/* XXX scan state can change! Re-validate scan state! */
654 
655 	IEEE80211_UNLOCK(ic);
656 	ic->ic_scan_start(ic);		/* notify driver */
657 	IEEE80211_LOCK(ic);
658 
659 	for (;;) {
660 
661 		scandone = (ss->ss_next >= ss->ss_last) ||
662 		    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
663 
664 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
665 		    "%s: loop start; scandone=%d\n",
666 		    __func__,
667 		    scandone);
668 
669 		if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
670 		    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
671 		     time_after(ticks + ss->ss_mindwell, scanend))
672 			break;
673 
674 		chan = ss->ss_chans[ss->ss_next++];
675 
676 		/*
677 		 * Watch for truncation due to the scan end time.
678 		 */
679 		if (time_after(ticks + ss->ss_maxdwell, scanend))
680 			maxdwell = scanend - ticks;
681 		else
682 			maxdwell = ss->ss_maxdwell;
683 
684 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
685 		    "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
686 		    __func__,
687 		    ieee80211_chan2ieee(ic, ic->ic_curchan),
688 		    ieee80211_channel_type_char(ic->ic_curchan),
689 		    ieee80211_chan2ieee(ic, chan),
690 		    ieee80211_channel_type_char(chan),
691 		    (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
692 			(chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
693 			"active" : "passive",
694 		    ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
695 
696 		/*
697 		 * Potentially change channel and phy mode.
698 		 */
699 		ic->ic_curchan = chan;
700 		ic->ic_rt = ieee80211_get_ratetable(chan);
701 		IEEE80211_UNLOCK(ic);
702 		/*
703 		 * Perform the channel change and scan unlocked so the driver
704 		 * may sleep. Once set_channel returns the hardware has
705 		 * completed the channel change.
706 		 */
707 		ic->ic_set_channel(ic);
708 		ieee80211_radiotap_chan_change(ic);
709 
710 		/*
711 		 * Scan curchan.  Drivers for "intelligent hardware"
712 		 * override ic_scan_curchan to tell the device to do
713 		 * the work.  Otherwise we manage the work outselves;
714 		 * sending a probe request (as needed), and arming the
715 		 * timeout to switch channels after maxdwell ticks.
716 		 *
717 		 * scan_curchan should only pause for the time required to
718 		 * prepare/initiate the hardware for the scan (if at all), the
719 		 * below condvar is used to sleep for the channels dwell time
720 		 * and allows it to be signalled for abort.
721 		 */
722 		ic->ic_scan_curchan(ss, maxdwell);
723 		IEEE80211_LOCK(ic);
724 
725 		/* XXX scan state can change! Re-validate scan state! */
726 
727 		SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
728 		/* clear mindwell lock and initial channel change flush */
729 		SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
730 
731 		if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
732 			continue;
733 
734 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: waiting\n", __func__);
735 		/* Wait to be signalled to scan the next channel */
736 		cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
737 	}
738 
739 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__);
740 
741 	if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
742 		goto done;
743 
744 	IEEE80211_UNLOCK(ic);
745 	ic->ic_scan_end(ic);		/* notify driver */
746 	IEEE80211_LOCK(ic);
747 	/* XXX scan state can change! Re-validate scan state! */
748 
749 	/*
750 	 * Since a cancellation may have occured during one of the
751 	 * driver calls (whilst unlocked), update scandone.
752 	 */
753 	if (scandone == 0 &&
754 	    ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
755 		/* XXX printf? */
756 		if_printf(vap->iv_ifp,
757 		    "%s: OOPS! scan cancelled during driver call (1)!\n",
758 		    __func__);
759 		scandone = 1;
760 	}
761 
762 	/*
763 	 * Record scan complete time.  Note that we also do
764 	 * this when canceled so any background scan will
765 	 * not be restarted for a while.
766 	 */
767 	if (scandone)
768 		ic->ic_lastscan = ticks;
769 	/* return to the bss channel */
770 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
771 	    ic->ic_curchan != ic->ic_bsschan) {
772 		ieee80211_setupcurchan(ic, ic->ic_bsschan);
773 		IEEE80211_UNLOCK(ic);
774 		ic->ic_set_channel(ic);
775 		ieee80211_radiotap_chan_change(ic);
776 		IEEE80211_LOCK(ic);
777 	}
778 	/* clear internal flags and any indication of a pick */
779 	SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
780 	ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
781 
782 	/*
783 	 * If not canceled and scan completed, do post-processing.
784 	 * If the callback function returns 0, then it wants to
785 	 * continue/restart scanning.  Unfortunately we needed to
786 	 * notify the driver to end the scan above to avoid having
787 	 * rx frames alter the scan candidate list.
788 	 */
789 	if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
790 	    !ss->ss_ops->scan_end(ss, vap) &&
791 	    (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
792 	    time_before(ticks + ss->ss_mindwell, scanend)) {
793 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
794 		    "%s: done, restart "
795 		    "[ticks %u, dwell min %lu scanend %lu]\n",
796 		    __func__,
797 		    ticks, ss->ss_mindwell, scanend);
798 		ss->ss_next = 0;	/* reset to begining */
799 		if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
800 			vap->iv_stats.is_scan_active++;
801 		else
802 			vap->iv_stats.is_scan_passive++;
803 
804 		ss->ss_ops->scan_restart(ss, vap);	/* XXX? */
805 		ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
806 		IEEE80211_UNLOCK(ic);
807 		return;
808 	}
809 
810 	/* past here, scandone is ``true'' if not in bg mode */
811 	if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
812 		scandone = 1;
813 
814 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
815 	    "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
816 	    __func__, scandone ? "done" : "stopped",
817 	    ticks, ss->ss_mindwell, scanend);
818 
819 	/*
820 	 * Since a cancellation may have occured during one of the
821 	 * driver calls (whilst unlocked), update scandone.
822 	 */
823 	if (scandone == 0 &&
824 	    ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
825 		/* XXX printf? */
826 		if_printf(vap->iv_ifp,
827 		    "%s: OOPS! scan cancelled during driver call (2)!\n",
828 		    __func__);
829 		scandone = 1;
830 	}
831 
832 	/*
833 	 * Clear the SCAN bit first in case frames are
834 	 * pending on the station power save queue.  If
835 	 * we defer this then the dispatch of the frames
836 	 * may generate a request to cancel scanning.
837 	 */
838 done:
839 	ic->ic_flags &= ~IEEE80211_F_SCAN;
840 	/*
841 	 * Drop out of power save mode when a scan has
842 	 * completed.  If this scan was prematurely terminated
843 	 * because it is a background scan then don't notify
844 	 * the ap; we'll either return to scanning after we
845 	 * receive the beacon frame or we'll drop out of power
846 	 * save mode because the beacon indicates we have frames
847 	 * waiting for us.
848 	 */
849 	if (scandone) {
850 		vap->iv_sta_ps(vap, 0);
851 		if (ss->ss_next >= ss->ss_last) {
852 			ieee80211_notify_scan_done(vap);
853 			ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
854 		}
855 	}
856 	SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
857 	ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
858 	IEEE80211_UNLOCK(ic);
859 #undef ISCAN_REP
860 }
861 
862 /*
863  * Process a beacon or probe response frame.
864  */
865 static void
866 ieee80211_swscan_add_scan(struct ieee80211vap *vap,
867 	struct ieee80211_channel *curchan,
868 	const struct ieee80211_scanparams *sp,
869 	const struct ieee80211_frame *wh,
870 	int subtype, int rssi, int noise)
871 {
872 	struct ieee80211com *ic = vap->iv_ic;
873 	struct ieee80211_scan_state *ss = ic->ic_scan;
874 
875 	/* XXX locking */
876 	/*
877 	 * Frames received during startup are discarded to avoid
878 	 * using scan state setup on the initial entry to the timer
879 	 * callback.  This can occur because the device may enable
880 	 * rx prior to our doing the initial channel change in the
881 	 * timer routine.
882 	 */
883 	if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
884 		return;
885 #ifdef IEEE80211_DEBUG
886 	if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
887 		ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
888 #endif
889 	if (ss->ss_ops != NULL &&
890 	    ss->ss_ops->scan_add(ss, curchan, sp, wh, subtype, rssi, noise)) {
891 		/*
892 		 * If we've reached the min dwell time terminate
893 		 * the timer so we'll switch to the next channel.
894 		 */
895 		if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
896 		    time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
897 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
898 			    "%s: chan %3d%c min dwell met (%u > %lu)\n",
899 			    __func__,
900 			    ieee80211_chan2ieee(ic, ic->ic_curchan),
901 			    ieee80211_channel_type_char(ic->ic_curchan),
902 			    ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
903 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
904 			/*
905 			 * NB: trigger at next clock tick or wait for the
906 			 * hardware.
907 			 */
908 			ic->ic_scan_mindwell(ss);
909 		}
910 	}
911 }
912 
913 static struct ieee80211_scan_methods swscan_methods = {
914 	.sc_attach = ieee80211_swscan_attach,
915 	.sc_detach = ieee80211_swscan_detach,
916 	.sc_vattach = ieee80211_swscan_vattach,
917 	.sc_vdetach = ieee80211_swscan_vdetach,
918 	.sc_set_scan_duration = ieee80211_swscan_set_scan_duration,
919 	.sc_start_scan = ieee80211_swscan_start_scan,
920 	.sc_check_scan = ieee80211_swscan_check_scan,
921 	.sc_bg_scan = ieee80211_swscan_bg_scan,
922 	.sc_cancel_scan = ieee80211_swscan_cancel_scan,
923 	.sc_cancel_anyscan = ieee80211_swscan_cancel_anyscan,
924 	.sc_scan_next = ieee80211_swscan_scan_next,
925 	.sc_scan_done = ieee80211_swscan_scan_done,
926 	.sc_scan_probe_curchan = ieee80211_swscan_probe_curchan,
927 	.sc_add_scan = ieee80211_swscan_add_scan
928 };
929 
930 /*
931  * Default scan attach method.
932  */
933 void
934 ieee80211_swscan_attach(struct ieee80211com *ic)
935 {
936 	struct scan_state *ss;
937 
938 	/*
939 	 * Setup the default methods
940 	 */
941 	ic->ic_scan_methods = &swscan_methods;
942 
943 	/* Allocate initial scan state */
944 	ss = (struct scan_state *) IEEE80211_MALLOC(sizeof(struct scan_state),
945 		M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
946 	if (ss == NULL) {
947 		ic->ic_scan = NULL;
948 		return;
949 	}
950 	callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
951 	cv_init(&ss->ss_scan_cv, "scan");
952 	TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
953 
954 	ic->ic_scan = &ss->base;
955 	ss->base.ss_ic = ic;
956 
957 	ic->ic_scan_curchan = scan_curchan;
958 	ic->ic_scan_mindwell = scan_mindwell;
959 }
960