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