xref: /freebsd/sys/net80211/ieee80211_scan.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * IEEE 802.11 scanning support.
33  */
34 #include "opt_wlan.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/condvar.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48 #include <net/if_private.h>
49 #include <net/ethernet.h>
50 
51 #include <net80211/ieee80211_var.h>
52 
53 /* XXX until it's implemented as attach ops */
54 #include <net80211/ieee80211_scan_sw.h>
55 
56 #include <net/bpf.h>
57 
58 /*
59  * Roaming-related defaults.  RSSI thresholds are as returned by the
60  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
61  * .5M units) or MCS.
62  */
63 /* rssi thresholds */
64 #define	ROAM_RSSI_11A_DEFAULT		14	/* 11a bss */
65 #define	ROAM_RSSI_11B_DEFAULT		14	/* 11b bss */
66 #define	ROAM_RSSI_11BONLY_DEFAULT	14	/* 11b-only bss */
67 /* transmit rate thresholds */
68 #define	ROAM_RATE_11A_DEFAULT		2*12	/* 11a bss */
69 #define	ROAM_RATE_11B_DEFAULT		2*5	/* 11b bss */
70 #define	ROAM_RATE_11BONLY_DEFAULT	2*1	/* 11b-only bss */
71 #define	ROAM_RATE_HALF_DEFAULT		2*6	/* half-width 11a/g bss */
72 #define	ROAM_RATE_QUARTER_DEFAULT	2*3	/* quarter-width 11a/g bss */
73 #define	ROAM_MCS_11N_DEFAULT		(1 | IEEE80211_RATE_MCS) /* 11n bss */
74 #define	ROAM_MCS_11AC_DEFAULT		(1 | IEEE80211_RATE_MCS) /* 11ac bss; XXX not used yet */
75 
76 void
77 ieee80211_scan_attach(struct ieee80211com *ic)
78 {
79 	/*
80 	 * If there's no scan method pointer, attach the
81 	 * swscan set as a default.
82 	 */
83 	if (ic->ic_scan_methods == NULL)
84 		ieee80211_swscan_attach(ic);
85 	else
86 		ic->ic_scan_methods->sc_attach(ic);
87 }
88 
89 void
90 ieee80211_scan_detach(struct ieee80211com *ic)
91 {
92 
93 	/*
94 	 * Ideally we'd do the ss_ops detach call here;
95 	 * but then sc_detach() would need to be split in two.
96 	 *
97 	 * I'll do that later.
98 	 */
99 	ic->ic_scan_methods->sc_detach(ic);
100 }
101 
102 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
103 	[IEEE80211_MODE_11A]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
104 				    .rate = ROAM_RATE_11A_DEFAULT },
105 	[IEEE80211_MODE_11G]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
106 				    .rate = ROAM_RATE_11B_DEFAULT },
107 	[IEEE80211_MODE_11B]	= { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
108 				    .rate = ROAM_RATE_11BONLY_DEFAULT },
109 	[IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
110 				    .rate = ROAM_RATE_11A_DEFAULT },
111 	[IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
112 				    .rate = ROAM_RATE_11A_DEFAULT },
113 	[IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
114 				    .rate = ROAM_RATE_11A_DEFAULT },
115 	[IEEE80211_MODE_HALF]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
116 				    .rate = ROAM_RATE_HALF_DEFAULT },
117 	[IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
118 				    .rate = ROAM_RATE_QUARTER_DEFAULT },
119 	[IEEE80211_MODE_11NA]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
120 				    .rate = ROAM_MCS_11N_DEFAULT },
121 	[IEEE80211_MODE_11NG]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
122 				    .rate = ROAM_MCS_11N_DEFAULT },
123 	[IEEE80211_MODE_VHT_2GHZ]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
124 				    .rate = ROAM_MCS_11AC_DEFAULT },
125 	[IEEE80211_MODE_VHT_5GHZ]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
126 				    .rate = ROAM_MCS_11AC_DEFAULT },
127 
128 };
129 
130 void
131 ieee80211_scan_vattach(struct ieee80211vap *vap)
132 {
133 	struct ieee80211com *ic = vap->iv_ic;
134 	int m;
135 
136 	vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
137 	vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
138 	vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
139 
140 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
141 
142 	memset(vap->iv_roamparms, 0, sizeof(vap->iv_roamparms));
143 	for (m = IEEE80211_MODE_AUTO + 1; m < IEEE80211_MODE_MAX; m++) {
144 		if (isclr(ic->ic_modecaps, m))
145 			continue;
146 
147 		memcpy(&vap->iv_roamparms[m], &defroam[m], sizeof(defroam[m]));
148 	}
149 
150 	ic->ic_scan_methods->sc_vattach(vap);
151 }
152 
153 void
154 ieee80211_scan_vdetach(struct ieee80211vap *vap)
155 {
156 	struct ieee80211com *ic = vap->iv_ic;
157 	struct ieee80211_scan_state *ss;
158 
159 	IEEE80211_LOCK(ic);
160 	ss = ic->ic_scan;
161 
162 	ic->ic_scan_methods->sc_vdetach(vap);
163 
164 	if (ss != NULL && ss->ss_vap == vap) {
165 		if (ss->ss_ops != NULL) {
166 			ss->ss_ops->scan_detach(ss);
167 			ss->ss_ops = NULL;
168 		}
169 		ss->ss_vap = NULL;
170 	}
171 	IEEE80211_UNLOCK(ic);
172 }
173 
174 /*
175  * Simple-minded scanner module support.
176  */
177 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
178 	"wlan_scan_sta",	/* IEEE80211_M_IBSS */
179 	"wlan_scan_sta",	/* IEEE80211_M_STA */
180 	"wlan_scan_wds",	/* IEEE80211_M_WDS */
181 	"wlan_scan_sta",	/* IEEE80211_M_AHDEMO */
182 	"wlan_scan_ap",		/* IEEE80211_M_HOSTAP */
183 	"wlan_scan_monitor",	/* IEEE80211_M_MONITOR */
184 	"wlan_scan_sta",	/* IEEE80211_M_MBSS */
185 };
186 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
187 
188 const struct ieee80211_scanner *
189 ieee80211_scanner_get(enum ieee80211_opmode mode)
190 {
191 	if (mode >= IEEE80211_OPMODE_MAX)
192 		return NULL;
193 	if (scanners[mode] == NULL)
194 		ieee80211_load_module(scan_modnames[mode]);
195 	return scanners[mode];
196 }
197 
198 void
199 ieee80211_scanner_register(enum ieee80211_opmode mode,
200 	const struct ieee80211_scanner *scan)
201 {
202 	if (mode >= IEEE80211_OPMODE_MAX)
203 		return;
204 	scanners[mode] = scan;
205 }
206 
207 void
208 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
209 	const struct ieee80211_scanner *scan)
210 {
211 	if (mode >= IEEE80211_OPMODE_MAX)
212 		return;
213 	if (scanners[mode] == scan)
214 		scanners[mode] = NULL;
215 }
216 
217 void
218 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
219 {
220 	int m;
221 
222 	for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
223 		if (scanners[m] == scan)
224 			scanners[m] = NULL;
225 }
226 
227 /*
228  * Update common scanner state to reflect the current
229  * operating mode.  This is called when the state machine
230  * is transitioned to RUN state w/o scanning--e.g. when
231  * operating in monitor mode.  The purpose of this is to
232  * ensure later callbacks find ss_ops set to properly
233  * reflect current operating mode.
234  */
235 void
236 ieee80211_scan_update_locked(struct ieee80211vap *vap,
237 	const struct ieee80211_scanner *scan)
238 {
239 	struct ieee80211com *ic = vap->iv_ic;
240 	struct ieee80211_scan_state *ss = ic->ic_scan;
241 
242 	IEEE80211_LOCK_ASSERT(ic);
243 
244 #ifdef IEEE80211_DEBUG
245 	if (ss->ss_vap != vap || ss->ss_ops != scan) {
246 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
247 		    "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
248 		    __func__,
249 		    ss->ss_vap != NULL ?
250 			ss->ss_vap->iv_ifp->if_xname : "none",
251 		    ss->ss_vap != NULL ?
252 			ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
253 		    vap->iv_ifp->if_xname,
254 		    ieee80211_opmode_name[vap->iv_opmode]);
255 	}
256 #endif
257 	ss->ss_vap = vap;
258 	if (ss->ss_ops != scan) {
259 		/*
260 		 * Switch scanners; detach old, attach new.  Special
261 		 * case where a single scan module implements multiple
262 		 * policies by using different scan ops but a common
263 		 * core.  We assume if the old and new attach methods
264 		 * are identical then it's ok to just change ss_ops
265 		 * and not flush the internal state of the module.
266 		 */
267 		if (scan == NULL || ss->ss_ops == NULL ||
268 		    ss->ss_ops->scan_attach != scan->scan_attach) {
269 			if (ss->ss_ops != NULL)
270 				ss->ss_ops->scan_detach(ss);
271 			if (scan != NULL && !scan->scan_attach(ss)) {
272 				/* XXX attach failure */
273 				/* XXX stat+msg */
274 				scan = NULL;
275 			}
276 		}
277 		ss->ss_ops = scan;
278 	}
279 }
280 
281 #ifdef IEEE80211_DEBUG
282 static void
283 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
284 {
285 	struct ieee80211com *ic = ss->ss_ic;
286 	const char *sep;
287 	int i;
288 
289 	sep = "";
290 	for (i = ss->ss_next; i < ss->ss_last; i++) {
291 		const struct ieee80211_channel *c = ss->ss_chans[i];
292 
293 		printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
294 		    ieee80211_channel_type_char(c));
295 		sep = ", ";
296 	}
297 }
298 
299 void
300 ieee80211_scan_dump(struct ieee80211_scan_state *ss)
301 {
302 	struct ieee80211vap *vap = ss->ss_vap;
303 
304 	if_printf(vap->iv_ifp, "scan set ");
305 	ieee80211_scan_dump_channels(ss);
306 	printf(" dwell min %ums max %ums\n",
307 	    ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
308 }
309 #endif /* IEEE80211_DEBUG */
310 
311 void
312 ieee80211_scan_copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
313 	int nssid, const struct ieee80211_scan_ssid ssids[])
314 {
315 	if (nssid > IEEE80211_SCAN_MAX_SSID) {
316 		/* XXX printf */
317 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
318 		    "%s: too many ssid %d, ignoring all of them\n",
319 		    __func__, nssid);
320 		return;
321 	}
322 	memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
323 	ss->ss_nssid = nssid;
324 }
325 
326 /*
327  * Start a scan unless one is already going.
328  */
329 int
330 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
331 	u_int duration, u_int mindwell, u_int maxdwell,
332 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
333 {
334 	const struct ieee80211_scanner *scan;
335 	struct ieee80211com *ic = vap->iv_ic;
336 
337 	scan = ieee80211_scanner_get(vap->iv_opmode);
338 	if (scan == NULL) {
339 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
340 		    "%s: no scanner support for %s mode\n",
341 		    __func__, ieee80211_opmode_name[vap->iv_opmode]);
342 		/* XXX stat */
343 		return 0;
344 	}
345 
346 	return ic->ic_scan_methods->sc_start_scan(scan, vap, flags, duration,
347 	    mindwell, maxdwell, nssid, ssids);
348 }
349 
350 /*
351  * Check the scan cache for an ap/channel to use; if that
352  * fails then kick off a new scan.
353  */
354 int
355 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
356 	u_int duration, u_int mindwell, u_int maxdwell,
357 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
358 {
359 	struct ieee80211com *ic = vap->iv_ic;
360 	struct ieee80211_scan_state *ss = ic->ic_scan;
361 	const struct ieee80211_scanner *scan;
362 	int result;
363 
364 	scan = ieee80211_scanner_get(vap->iv_opmode);
365 	if (scan == NULL) {
366 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
367 		    "%s: no scanner support for %s mode\n",
368 		    __func__, vap->iv_opmode);
369 		/* XXX stat */
370 		return 0;
371 	}
372 
373 	/*
374 	 * Check if there's a list of scan candidates already.
375 	 * XXX want more than the ap we're currently associated with
376 	 */
377 
378 	IEEE80211_LOCK(ic);
379 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
380 	    "%s: %s scan, %s%s%s%s%s\n"
381 	    , __func__
382 	    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
383 	    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
384 	    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
385 	    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
386 	    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
387 	    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
388 	);
389 
390 	if (ss->ss_ops != scan) {
391 		/* XXX re-use cache contents? e.g. adhoc<->sta */
392 		flags |= IEEE80211_SCAN_FLUSH;
393 	}
394 
395 	/*
396 	 * XXX TODO: separate things out a bit better.
397 	 */
398 	ieee80211_scan_update_locked(vap, scan);
399 
400 	result = ic->ic_scan_methods->sc_check_scan(scan, vap, flags, duration,
401 	    mindwell, maxdwell, nssid, ssids);
402 
403 	IEEE80211_UNLOCK(ic);
404 
405 	return (result);
406 }
407 
408 /*
409  * Check the scan cache for an ap/channel to use; if that fails
410  * then kick off a scan using the current settings.
411  */
412 int
413 ieee80211_check_scan_current(struct ieee80211vap *vap)
414 {
415 	return ieee80211_check_scan(vap,
416 	    IEEE80211_SCAN_ACTIVE,
417 	    IEEE80211_SCAN_FOREVER, 0, 0,
418 	    vap->iv_des_nssid, vap->iv_des_ssid);
419 }
420 
421 /*
422  * Restart a previous scan.  If the previous scan completed
423  * then we start again using the existing channel list.
424  */
425 int
426 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
427 {
428 	struct ieee80211com *ic = vap->iv_ic;
429 	const struct ieee80211_scanner *scan;
430 
431 	// IEEE80211_UNLOCK_ASSERT(sc);
432 
433 	scan = ieee80211_scanner_get(vap->iv_opmode);
434 	if (scan == NULL) {
435 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
436 		    "%s: no scanner support for %s mode\n",
437 		    __func__, vap->iv_opmode);
438 		/* XXX stat */
439 		return 0;
440 	}
441 
442 	/*
443 	 * XXX TODO: pull apart the bgscan logic into whatever
444 	 * belongs here and whatever belongs in the software
445 	 * scanner.
446 	 */
447 	return (ic->ic_scan_methods->sc_bg_scan(scan, vap, flags));
448 }
449 
450 /*
451  * Cancel any scan currently going on for the specified vap.
452  */
453 void
454 ieee80211_cancel_scan(struct ieee80211vap *vap)
455 {
456 	struct ieee80211com *ic = vap->iv_ic;
457 
458 	ic->ic_scan_methods->sc_cancel_scan(vap);
459 }
460 
461 /*
462  * Cancel any scan currently going on.
463  *
464  * This is called during normal 802.11 data path to cancel
465  * a scan so a newly arrived normal data packet can be sent.
466  */
467 void
468 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
469 {
470 	struct ieee80211com *ic = vap->iv_ic;
471 
472 	ic->ic_scan_methods->sc_cancel_anyscan(vap);
473 }
474 
475 /*
476  * Manually switch to the next channel in the channel list.
477  * Provided for drivers that manage scanning themselves
478  * (e.g. for firmware-based devices).
479  */
480 void
481 ieee80211_scan_next(struct ieee80211vap *vap)
482 {
483 	struct ieee80211com *ic = vap->iv_ic;
484 
485 	ic->ic_scan_methods->sc_scan_next(vap);
486 }
487 
488 /*
489  * Manually stop a scan that is currently running.
490  * Provided for drivers that are not able to scan single channels
491  * (e.g. for firmware-based devices).
492  */
493 void
494 ieee80211_scan_done(struct ieee80211vap *vap)
495 {
496 	struct ieee80211com *ic = vap->iv_ic;
497 	struct ieee80211_scan_state *ss;
498 
499 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
500 
501 	IEEE80211_LOCK(ic);
502 	ss = ic->ic_scan;
503 	ss->ss_next = ss->ss_last; /* all channels are complete */
504 
505 	ic->ic_scan_methods->sc_scan_done(vap);
506 
507 	IEEE80211_UNLOCK(ic);
508 }
509 
510 /*
511  * Probe the current channel, if allowed, while scanning.
512  * If the channel is not marked passive-only then send
513  * a probe request immediately.  Otherwise mark state and
514  * listen for beacons on the channel; if we receive something
515  * then we'll transmit a probe request.
516  */
517 void
518 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
519 {
520 	struct ieee80211com *ic = vap->iv_ic;
521 
522 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
523 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
524 		return;
525 	}
526 
527 	ic->ic_scan_methods->sc_scan_probe_curchan(vap, force);
528 }
529 
530 #ifdef IEEE80211_DEBUG
531 static void
532 dump_country(const uint8_t *ie)
533 {
534 	const struct ieee80211_country_ie *cie =
535 	   (const struct ieee80211_country_ie *) ie;
536 	int i, nbands, schan, nchan;
537 
538 	if (cie->len < 3) {
539 		printf(" <bogus country ie, len %d>", cie->len);
540 		return;
541 	}
542 	printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
543 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
544 	for (i = 0; i < nbands; i++) {
545 		schan = cie->band[i].schan;
546 		nchan = cie->band[i].nchan;
547 		if (nchan != 1)
548 			printf(" %u-%u,%u", schan, schan + nchan-1,
549 			    cie->band[i].maxtxpwr);
550 		else
551 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
552 	}
553 	printf("]");
554 }
555 
556 void
557 ieee80211_scan_dump_probe_beacon(uint8_t subtype, int isnew,
558 	const uint8_t mac[IEEE80211_ADDR_LEN],
559 	const struct ieee80211_scanparams *sp, int rssi)
560 {
561 
562 	printf("[%s] %s%s on chan %u (bss chan %u) ",
563 	    ether_sprintf(mac), isnew ? "new " : "",
564 	    ieee80211_mgt_subtype_name(subtype), sp->chan, sp->bchan);
565 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
566 	printf(" rssi %d\n", rssi);
567 
568 	if (isnew) {
569 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
570 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
571 		if (sp->country != NULL)
572 			dump_country(sp->country);
573 		printf("\n");
574 	}
575 }
576 #endif /* IEEE80211_DEBUG */
577 
578 /*
579  * Process a beacon or probe response frame.
580  */
581 void
582 ieee80211_add_scan(struct ieee80211vap *vap,
583 	struct ieee80211_channel *curchan,
584 	const struct ieee80211_scanparams *sp,
585 	const struct ieee80211_frame *wh,
586 	int subtype, int rssi, int noise)
587 {
588 	struct ieee80211com *ic = vap->iv_ic;
589 
590 	return (ic->ic_scan_methods->sc_add_scan(vap, curchan, sp, wh, subtype,
591 	    rssi, noise));
592 }
593 
594 /*
595  * Timeout/age scan cache entries; called from sta timeout
596  * timer (XXX should be self-contained).
597  */
598 void
599 ieee80211_scan_timeout(struct ieee80211com *ic)
600 {
601 	struct ieee80211_scan_state *ss = ic->ic_scan;
602 
603 	if (ss->ss_ops != NULL)
604 		ss->ss_ops->scan_age(ss);
605 }
606 
607 /*
608  * Mark a scan cache entry after a successful associate.
609  */
610 void
611 ieee80211_scan_assoc_success(struct ieee80211vap *vap,
612     const uint8_t mac[IEEE80211_ADDR_LEN])
613 {
614 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
615 
616 	if (ss->ss_ops != NULL) {
617 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
618 			mac, "%s",  __func__);
619 		ss->ss_ops->scan_assoc_success(ss, mac);
620 	}
621 }
622 
623 /*
624  * Demerit a scan cache entry after failing to associate.
625  */
626 void
627 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
628 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
629 {
630 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
631 
632 	if (ss->ss_ops != NULL) {
633 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
634 			"%s: reason %u", __func__, reason);
635 		ss->ss_ops->scan_assoc_fail(ss, mac, reason);
636 	}
637 }
638 
639 /*
640  * Iterate over the contents of the scan cache.
641  */
642 void
643 ieee80211_scan_iterate(struct ieee80211vap *vap,
644 	ieee80211_scan_iter_func *f, void *arg)
645 {
646 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
647 
648 	if (ss->ss_ops != NULL)
649 		ss->ss_ops->scan_iterate(ss, f, arg);
650 }
651 
652 /*
653  * Flush the contents of the scan cache.
654  */
655 void
656 ieee80211_scan_flush(struct ieee80211vap *vap)
657 {
658 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
659 
660 	if (ss->ss_ops != NULL && ss->ss_vap == vap) {
661 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
662 		ss->ss_ops->scan_flush(ss);
663 	}
664 }
665 
666 /*
667  * Check the scan cache for an ap/channel to use; if that
668  * fails then kick off a new scan.
669  */
670 struct ieee80211_channel *
671 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
672 {
673 	struct ieee80211_scan_state *ss = ic->ic_scan;
674 
675 	IEEE80211_LOCK_ASSERT(ic);
676 
677 	if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
678 		/* XXX printf? */
679 		return NULL;
680 	}
681 	if (ss->ss_ops->scan_pickchan == NULL) {
682 		IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
683 		    "%s: scan module does not support picking a channel, "
684 		    "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
685 		return NULL;
686 	}
687 	return ss->ss_ops->scan_pickchan(ss, flags);
688 }
689