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