xref: /freebsd/sys/net80211/ieee80211_scan.c (revision db3cb3640f547c063293e9fdc4db69e9dc120951)
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 /* XXX until it's implemented as attach ops */
50 #include <net80211/ieee80211_scan_sw.h>
51 
52 #include <net/bpf.h>
53 
54 /*
55  * Roaming-related defaults.  RSSI thresholds are as returned by the
56  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
57  * .5M units) or MCS.
58  */
59 /* rssi thresholds */
60 #define	ROAM_RSSI_11A_DEFAULT		14	/* 11a bss */
61 #define	ROAM_RSSI_11B_DEFAULT		14	/* 11b bss */
62 #define	ROAM_RSSI_11BONLY_DEFAULT	14	/* 11b-only bss */
63 /* transmit rate thresholds */
64 #define	ROAM_RATE_11A_DEFAULT		2*12	/* 11a bss */
65 #define	ROAM_RATE_11B_DEFAULT		2*5	/* 11b bss */
66 #define	ROAM_RATE_11BONLY_DEFAULT	2*1	/* 11b-only bss */
67 #define	ROAM_RATE_HALF_DEFAULT		2*6	/* half-width 11a/g bss */
68 #define	ROAM_RATE_QUARTER_DEFAULT	2*3	/* quarter-width 11a/g bss */
69 #define	ROAM_MCS_11N_DEFAULT		(1 | IEEE80211_RATE_MCS) /* 11n bss */
70 
71 void
72 ieee80211_scan_attach(struct ieee80211com *ic)
73 {
74 
75 	/*
76 	 * For now, the swscan module does both the
77 	 * allocation (so it can pad it) and sets up the net80211
78 	 * bits.
79 	 *
80 	 * I'll split this stuff later.
81 	 */
82 	ieee80211_swscan_attach(ic);
83 }
84 
85 void
86 ieee80211_scan_detach(struct ieee80211com *ic)
87 {
88 
89 	/*
90 	 * Ideally we'd do the ss_ops detach call here;
91 	 * but then ieee80211_swscan_detach would need
92 	 * to be split in two.
93 	 *
94 	 * I'll do that later.
95 	 */
96 	ieee80211_swscan_detach(ic);
97 }
98 
99 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
100 	[IEEE80211_MODE_11A]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
101 				    .rate = ROAM_RATE_11A_DEFAULT },
102 	[IEEE80211_MODE_11G]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
103 				    .rate = ROAM_RATE_11B_DEFAULT },
104 	[IEEE80211_MODE_11B]	= { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
105 				    .rate = ROAM_RATE_11BONLY_DEFAULT },
106 	[IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
107 				    .rate = ROAM_RATE_11A_DEFAULT },
108 	[IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
109 				    .rate = ROAM_RATE_11A_DEFAULT },
110 	[IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
111 				    .rate = ROAM_RATE_11A_DEFAULT },
112 	[IEEE80211_MODE_HALF]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
113 				    .rate = ROAM_RATE_HALF_DEFAULT },
114 	[IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
115 				    .rate = ROAM_RATE_QUARTER_DEFAULT },
116 	[IEEE80211_MODE_11NA]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
117 				    .rate = ROAM_MCS_11N_DEFAULT },
118 	[IEEE80211_MODE_11NG]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
119 				    .rate = ROAM_MCS_11N_DEFAULT },
120 };
121 
122 void
123 ieee80211_scan_vattach(struct ieee80211vap *vap)
124 {
125 	vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
126 	vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
127 	vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
128 
129 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
130 	memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
131 
132 	ieee80211_swscan_vattach(vap);
133 }
134 
135 void
136 ieee80211_scan_vdetach(struct ieee80211vap *vap)
137 {
138 	struct ieee80211com *ic = vap->iv_ic;
139 	struct ieee80211_scan_state *ss;
140 
141 	IEEE80211_LOCK(ic);
142 	ss = ic->ic_scan;
143 
144 	ieee80211_swscan_vdetach(vap);
145 
146 	if (ss != NULL && ss->ss_vap == vap) {
147 		if (ss->ss_ops != NULL) {
148 			ss->ss_ops->scan_detach(ss);
149 			ss->ss_ops = NULL;
150 		}
151 		ss->ss_vap = NULL;
152 	}
153 	IEEE80211_UNLOCK(ic);
154 }
155 
156 /*
157  * Simple-minded scanner module support.
158  */
159 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
160 	"wlan_scan_sta",	/* IEEE80211_M_IBSS */
161 	"wlan_scan_sta",	/* IEEE80211_M_STA */
162 	"wlan_scan_wds",	/* IEEE80211_M_WDS */
163 	"wlan_scan_sta",	/* IEEE80211_M_AHDEMO */
164 	"wlan_scan_ap",		/* IEEE80211_M_HOSTAP */
165 	"wlan_scan_monitor",	/* IEEE80211_M_MONITOR */
166 	"wlan_scan_sta",	/* IEEE80211_M_MBSS */
167 };
168 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
169 
170 const struct ieee80211_scanner *
171 ieee80211_scanner_get(enum ieee80211_opmode mode)
172 {
173 	if (mode >= IEEE80211_OPMODE_MAX)
174 		return NULL;
175 	if (scanners[mode] == NULL)
176 		ieee80211_load_module(scan_modnames[mode]);
177 	return scanners[mode];
178 }
179 
180 void
181 ieee80211_scanner_register(enum ieee80211_opmode mode,
182 	const struct ieee80211_scanner *scan)
183 {
184 	if (mode >= IEEE80211_OPMODE_MAX)
185 		return;
186 	scanners[mode] = scan;
187 }
188 
189 void
190 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
191 	const struct ieee80211_scanner *scan)
192 {
193 	if (mode >= IEEE80211_OPMODE_MAX)
194 		return;
195 	if (scanners[mode] == scan)
196 		scanners[mode] = NULL;
197 }
198 
199 void
200 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
201 {
202 	int m;
203 
204 	for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
205 		if (scanners[m] == scan)
206 			scanners[m] = NULL;
207 }
208 
209 /*
210  * Update common scanner state to reflect the current
211  * operating mode.  This is called when the state machine
212  * is transitioned to RUN state w/o scanning--e.g. when
213  * operating in monitor mode.  The purpose of this is to
214  * ensure later callbacks find ss_ops set to properly
215  * reflect current operating mode.
216  */
217 void
218 ieee80211_scan_update_locked(struct ieee80211vap *vap,
219 	const struct ieee80211_scanner *scan)
220 {
221 	struct ieee80211com *ic = vap->iv_ic;
222 	struct ieee80211_scan_state *ss = ic->ic_scan;
223 
224 	IEEE80211_LOCK_ASSERT(ic);
225 
226 #ifdef IEEE80211_DEBUG
227 	if (ss->ss_vap != vap || ss->ss_ops != scan) {
228 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
229 		    "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
230 		    __func__,
231 		    ss->ss_vap != NULL ?
232 			ss->ss_vap->iv_ifp->if_xname : "none",
233 		    ss->ss_vap != NULL ?
234 			ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
235 		    vap->iv_ifp->if_xname,
236 		    ieee80211_opmode_name[vap->iv_opmode]);
237 	}
238 #endif
239 	ss->ss_vap = vap;
240 	if (ss->ss_ops != scan) {
241 		/*
242 		 * Switch scanners; detach old, attach new.  Special
243 		 * case where a single scan module implements multiple
244 		 * policies by using different scan ops but a common
245 		 * core.  We assume if the old and new attach methods
246 		 * are identical then it's ok to just change ss_ops
247 		 * and not flush the internal state of the module.
248 		 */
249 		if (scan == NULL || ss->ss_ops == NULL ||
250 		    ss->ss_ops->scan_attach != scan->scan_attach) {
251 			if (ss->ss_ops != NULL)
252 				ss->ss_ops->scan_detach(ss);
253 			if (scan != NULL && !scan->scan_attach(ss)) {
254 				/* XXX attach failure */
255 				/* XXX stat+msg */
256 				scan = NULL;
257 			}
258 		}
259 		ss->ss_ops = scan;
260 	}
261 }
262 
263 void
264 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
265 {
266 	struct ieee80211com *ic = ss->ss_ic;
267 	const char *sep;
268 	int i;
269 
270 	sep = "";
271 	for (i = ss->ss_next; i < ss->ss_last; i++) {
272 		const struct ieee80211_channel *c = ss->ss_chans[i];
273 
274 		printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
275 		    ieee80211_channel_type_char(c));
276 		sep = ", ";
277 	}
278 }
279 
280 #ifdef IEEE80211_DEBUG
281 void
282 ieee80211_scan_dump(struct ieee80211_scan_state *ss)
283 {
284 	struct ieee80211vap *vap = ss->ss_vap;
285 
286 	if_printf(vap->iv_ifp, "scan set ");
287 	ieee80211_scan_dump_channels(ss);
288 	printf(" dwell min %lums max %lums\n",
289 	    ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
290 }
291 #endif /* IEEE80211_DEBUG */
292 
293 void
294 ieee80211_scan_copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
295 	int nssid, const struct ieee80211_scan_ssid ssids[])
296 {
297 	if (nssid > IEEE80211_SCAN_MAX_SSID) {
298 		/* XXX printf */
299 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
300 		    "%s: too many ssid %d, ignoring all of them\n",
301 		    __func__, nssid);
302 		return;
303 	}
304 	memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
305 	ss->ss_nssid = nssid;
306 }
307 
308 /*
309  * Start a scan unless one is already going.
310  */
311 int
312 ieee80211_start_scan_locked(const struct ieee80211_scanner *scan,
313 	struct ieee80211vap *vap, int flags, u_int duration,
314 	u_int mindwell, u_int maxdwell,
315 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
316 {
317 	struct ieee80211com *ic = vap->iv_ic;
318 	struct ieee80211_scan_state *ss = ic->ic_scan;
319 
320 	IEEE80211_LOCK_ASSERT(ic);
321 
322 	if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
323 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
324 		    "%s: scan inhibited by pending channel change\n", __func__);
325 	} else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
326 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
327 		    "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
328 		    , __func__
329 		    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
330 		    , duration, mindwell, maxdwell
331 		    , ieee80211_phymode_name[vap->iv_des_mode]
332 		    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
333 		    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
334 		    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
335 		    , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
336 		    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
337 		    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
338 		);
339 
340 		ieee80211_scan_update_locked(vap, scan);
341 		if (ss->ss_ops != NULL) {
342 			if ((flags & IEEE80211_SCAN_NOSSID) == 0)
343 				ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
344 
345 			/* NB: top 4 bits for internal use */
346 			ss->ss_flags = flags & 0xfff;
347 			if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
348 				vap->iv_stats.is_scan_active++;
349 			else
350 				vap->iv_stats.is_scan_passive++;
351 			if (flags & IEEE80211_SCAN_FLUSH)
352 				ss->ss_ops->scan_flush(ss);
353 			if (flags & IEEE80211_SCAN_BGSCAN)
354 				ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
355 
356 			/* Set duration for this particular scan */
357 			ieee80211_swscan_set_scan_duration(vap, duration);
358 
359 			ss->ss_next = 0;
360 			ss->ss_mindwell = mindwell;
361 			ss->ss_maxdwell = maxdwell;
362 			/* NB: scan_start must be before the scan runtask */
363 			ss->ss_ops->scan_start(ss, vap);
364 #ifdef IEEE80211_DEBUG
365 			if (ieee80211_msg_scan(vap))
366 				ieee80211_scan_dump(ss);
367 #endif /* IEEE80211_DEBUG */
368 			ic->ic_flags |= IEEE80211_F_SCAN;
369 
370 			/* Start scan task */
371 			ieee80211_swscan_run_scan_task(vap);
372 		}
373 		return 1;
374 	} else {
375 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
376 		    "%s: %s scan already in progress\n", __func__,
377 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
378 	}
379 	return 0;
380 }
381 
382 /*
383  * Start a scan unless one is already going.
384  */
385 int
386 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
387 	u_int duration, u_int mindwell, u_int maxdwell,
388 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
389 {
390 	const struct ieee80211_scanner *scan;
391 
392 	scan = ieee80211_scanner_get(vap->iv_opmode);
393 	if (scan == NULL) {
394 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
395 		    "%s: no scanner support for %s mode\n",
396 		    __func__, ieee80211_opmode_name[vap->iv_opmode]);
397 		/* XXX stat */
398 		return 0;
399 	}
400 
401 	/* XXX ops */
402 	return ieee80211_swscan_start_scan(scan, vap, flags, duration,
403 	    mindwell, maxdwell, nssid, ssids);
404 }
405 
406 /*
407  * Check the scan cache for an ap/channel to use; if that
408  * fails then kick off a new scan.
409  */
410 int
411 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
412 	u_int duration, u_int mindwell, u_int maxdwell,
413 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
414 {
415 	struct ieee80211com *ic = vap->iv_ic;
416 	struct ieee80211_scan_state *ss = ic->ic_scan;
417 	const struct ieee80211_scanner *scan;
418 	int result;
419 
420 	scan = ieee80211_scanner_get(vap->iv_opmode);
421 	if (scan == NULL) {
422 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
423 		    "%s: no scanner support for %s mode\n",
424 		    __func__, vap->iv_opmode);
425 		/* XXX stat */
426 		return 0;
427 	}
428 
429 	/*
430 	 * Check if there's a list of scan candidates already.
431 	 * XXX want more than the ap we're currently associated with
432 	 */
433 
434 	IEEE80211_LOCK(ic);
435 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
436 	    "%s: %s scan, %s%s%s%s%s\n"
437 	    , __func__
438 	    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
439 	    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
440 	    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
441 	    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
442 	    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
443 	    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
444 	);
445 
446 	if (ss->ss_ops != scan) {
447 		/* XXX re-use cache contents? e.g. adhoc<->sta */
448 		flags |= IEEE80211_SCAN_FLUSH;
449 	}
450 
451 	/*
452 	 * XXX TODO: separate things out a bit better.
453 	 * XXX TODO: ops
454 	 */
455 	ieee80211_scan_update_locked(vap, scan);
456 
457 	result = ieee80211_swscan_check_scan(scan, vap, flags, duration,
458 	    mindwell, maxdwell, nssid, ssids);
459 
460 	IEEE80211_UNLOCK(ic);
461 
462 	return (result);
463 }
464 
465 /*
466  * Check the scan cache for an ap/channel to use; if that fails
467  * then kick off a scan using the current settings.
468  */
469 int
470 ieee80211_check_scan_current(struct ieee80211vap *vap)
471 {
472 	return ieee80211_check_scan(vap,
473 	    IEEE80211_SCAN_ACTIVE,
474 	    IEEE80211_SCAN_FOREVER, 0, 0,
475 	    vap->iv_des_nssid, vap->iv_des_ssid);
476 }
477 
478 /*
479  * Restart a previous scan.  If the previous scan completed
480  * then we start again using the existing channel list.
481  */
482 int
483 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
484 {
485 	const struct ieee80211_scanner *scan;
486 
487 	// IEEE80211_UNLOCK_ASSERT(sc);
488 
489 	scan = ieee80211_scanner_get(vap->iv_opmode);
490 	if (scan == NULL) {
491 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
492 		    "%s: no scanner support for %s mode\n",
493 		    __func__, vap->iv_opmode);
494 		/* XXX stat */
495 		return 0;
496 	}
497 
498 	/*
499 	 * XXX TODO: pull apart the bgscan logic into whatever
500 	 * belongs here and whatever belongs in the software
501 	 * scanner.
502 	 *
503 	 * XXX TODO: ops
504 	 */
505 	return (ieee80211_swscan_bg_scan(scan, vap, flags));
506 }
507 
508 /*
509  * Cancel any scan currently going on for the specified vap.
510  */
511 void
512 ieee80211_cancel_scan(struct ieee80211vap *vap)
513 {
514 
515 	/* XXX TODO: ops */
516 	ieee80211_swscan_cancel_scan(vap);
517 }
518 
519 /*
520  * Cancel any scan currently going on.
521  */
522 void
523 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
524 {
525 
526 	/* XXX TODO: ops */
527 	ieee80211_swscan_cancel_anyscan(vap);
528 }
529 
530 /*
531  * Public access to scan_next for drivers that manage
532  * scanning themselves (e.g. for firmware-based devices).
533  */
534 void
535 ieee80211_scan_next(struct ieee80211vap *vap)
536 {
537 
538 	/* XXX TODO: ops */
539 	ieee80211_swscan_scan_next(vap);
540 }
541 
542 /*
543  * Public access to scan_next for drivers that are not able to scan single
544  * channels (e.g. for firmware-based devices).
545  */
546 void
547 ieee80211_scan_done(struct ieee80211vap *vap)
548 {
549 	struct ieee80211com *ic = vap->iv_ic;
550 	struct ieee80211_scan_state *ss;
551 
552 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
553 
554 	IEEE80211_LOCK(ic);
555 	ss = ic->ic_scan;
556 	ss->ss_next = ss->ss_last; /* all channels are complete */
557 
558 	/* XXX TODO: ops */
559 	ieee80211_swscan_scan_done(vap);
560 
561 	IEEE80211_UNLOCK(ic);
562 }
563 
564 /*
565  * Probe the curent channel, if allowed, while scanning.
566  * If the channel is not marked passive-only then send
567  * a probe request immediately.  Otherwise mark state and
568  * listen for beacons on the channel; if we receive something
569  * then we'll transmit a probe request.
570  */
571 void
572 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
573 {
574 	struct ieee80211com *ic = vap->iv_ic;
575 
576 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
577 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
578 		return;
579 	}
580 
581 	/* XXX TODO: ops */
582 	ieee80211_swscan_probe_curchan(vap, force);
583 }
584 
585 #ifdef IEEE80211_DEBUG
586 static void
587 dump_country(const uint8_t *ie)
588 {
589 	const struct ieee80211_country_ie *cie =
590 	   (const struct ieee80211_country_ie *) ie;
591 	int i, nbands, schan, nchan;
592 
593 	if (cie->len < 3) {
594 		printf(" <bogus country ie, len %d>", cie->len);
595 		return;
596 	}
597 	printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
598 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
599 	for (i = 0; i < nbands; i++) {
600 		schan = cie->band[i].schan;
601 		nchan = cie->band[i].nchan;
602 		if (nchan != 1)
603 			printf(" %u-%u,%u", schan, schan + nchan-1,
604 			    cie->band[i].maxtxpwr);
605 		else
606 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
607 	}
608 	printf("]");
609 }
610 
611 void
612 ieee80211_scan_dump_probe_beacon(uint8_t subtype, int isnew,
613 	const uint8_t mac[IEEE80211_ADDR_LEN],
614 	const struct ieee80211_scanparams *sp, int rssi)
615 {
616 
617 	printf("[%s] %s%s on chan %u (bss chan %u) ",
618 	    ether_sprintf(mac), isnew ? "new " : "",
619 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
620 	    sp->chan, sp->bchan);
621 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
622 	printf(" rssi %d\n", rssi);
623 
624 	if (isnew) {
625 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
626 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
627 		if (sp->country != NULL)
628 			dump_country(sp->country);
629 		printf("\n");
630 	}
631 }
632 #endif /* IEEE80211_DEBUG */
633 
634 /*
635  * Process a beacon or probe response frame.
636  */
637 void
638 ieee80211_add_scan(struct ieee80211vap *vap,
639 	const struct ieee80211_scanparams *sp,
640 	const struct ieee80211_frame *wh,
641 	int subtype, int rssi, int noise)
642 {
643 
644 	return (ieee80211_swscan_add_scan(vap, sp, wh, subtype, rssi, noise));
645 }
646 
647 /*
648  * Timeout/age scan cache entries; called from sta timeout
649  * timer (XXX should be self-contained).
650  */
651 void
652 ieee80211_scan_timeout(struct ieee80211com *ic)
653 {
654 	struct ieee80211_scan_state *ss = ic->ic_scan;
655 
656 	if (ss->ss_ops != NULL)
657 		ss->ss_ops->scan_age(ss);
658 }
659 
660 /*
661  * Mark a scan cache entry after a successful associate.
662  */
663 void
664 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
665 {
666 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
667 
668 	if (ss->ss_ops != NULL) {
669 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
670 			mac, "%s",  __func__);
671 		ss->ss_ops->scan_assoc_success(ss, mac);
672 	}
673 }
674 
675 /*
676  * Demerit a scan cache entry after failing to associate.
677  */
678 void
679 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
680 	const uint8_t mac[], int reason)
681 {
682 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
683 
684 	if (ss->ss_ops != NULL) {
685 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
686 			"%s: reason %u", __func__, reason);
687 		ss->ss_ops->scan_assoc_fail(ss, mac, reason);
688 	}
689 }
690 
691 /*
692  * Iterate over the contents of the scan cache.
693  */
694 void
695 ieee80211_scan_iterate(struct ieee80211vap *vap,
696 	ieee80211_scan_iter_func *f, void *arg)
697 {
698 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
699 
700 	if (ss->ss_ops != NULL)
701 		ss->ss_ops->scan_iterate(ss, f, arg);
702 }
703 
704 /*
705  * Flush the contents of the scan cache.
706  */
707 void
708 ieee80211_scan_flush(struct ieee80211vap *vap)
709 {
710 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
711 
712 	if (ss->ss_ops != NULL && ss->ss_vap == vap) {
713 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
714 		ss->ss_ops->scan_flush(ss);
715 	}
716 }
717 
718 /*
719  * Check the scan cache for an ap/channel to use; if that
720  * fails then kick off a new scan.
721  */
722 struct ieee80211_channel *
723 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
724 {
725 	struct ieee80211_scan_state *ss = ic->ic_scan;
726 
727 	IEEE80211_LOCK_ASSERT(ic);
728 
729 	if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
730 		/* XXX printf? */
731 		return NULL;
732 	}
733 	if (ss->ss_ops->scan_pickchan == NULL) {
734 		IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
735 		    "%s: scan module does not support picking a channel, "
736 		    "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
737 		return NULL;
738 	}
739 	return ss->ss_ops->scan_pickchan(ss, flags);
740 }
741