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 <netproto/802_11/ieee80211_var.h>
48 
49 /* XXX until it's implemented as attach ops */
50 #include <netproto/802_11/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 	 * If there's no scan method pointer, attach the
77 	 * swscan set as a default.
78 	 */
79 	if (ic->ic_scan_methods == NULL)
80 		ieee80211_swscan_attach(ic);
81 	else
82 		ic->ic_scan_methods->sc_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 sc_detach() would need to be split in two.
92 	 *
93 	 * I'll do that later.
94 	 */
95 	ic->ic_scan_methods->sc_detach(ic);
96 }
97 
98 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
99 	[IEEE80211_MODE_11A]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
100 				    .rate = ROAM_RATE_11A_DEFAULT },
101 	[IEEE80211_MODE_11G]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
102 				    .rate = ROAM_RATE_11B_DEFAULT },
103 	[IEEE80211_MODE_11B]	= { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
104 				    .rate = ROAM_RATE_11BONLY_DEFAULT },
105 	[IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
106 				    .rate = ROAM_RATE_11A_DEFAULT },
107 	[IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
108 				    .rate = ROAM_RATE_11A_DEFAULT },
109 	[IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
110 				    .rate = ROAM_RATE_11A_DEFAULT },
111 	[IEEE80211_MODE_HALF]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
112 				    .rate = ROAM_RATE_HALF_DEFAULT },
113 	[IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
114 				    .rate = ROAM_RATE_QUARTER_DEFAULT },
115 	[IEEE80211_MODE_11NA]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
116 				    .rate = ROAM_MCS_11N_DEFAULT },
117 	[IEEE80211_MODE_11NG]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
118 				    .rate = ROAM_MCS_11N_DEFAULT },
119 };
120 
121 void
122 ieee80211_scan_vattach(struct ieee80211vap *vap)
123 {
124 	struct ieee80211com *ic = vap->iv_ic;
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 	ic->ic_scan_methods->sc_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 	ic->ic_scan_methods->sc_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 		kprintf("%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 	kprintf(" 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(struct ieee80211vap *vap, int flags,
313 	u_int duration, u_int mindwell, u_int maxdwell,
314 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
315 {
316 	const struct ieee80211_scanner *scan;
317 	struct ieee80211com *ic = vap->iv_ic;
318 
319 	scan = ieee80211_scanner_get(vap->iv_opmode);
320 	if (scan == NULL) {
321 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
322 		    "%s: no scanner support for %s mode\n",
323 		    __func__, ieee80211_opmode_name[vap->iv_opmode]);
324 		/* XXX stat */
325 		return 0;
326 	}
327 
328 	return ic->ic_scan_methods->sc_start_scan(scan, vap, flags, duration,
329 	    mindwell, maxdwell, nssid, ssids);
330 }
331 
332 /*
333  * Check the scan cache for an ap/channel to use; if that
334  * fails then kick off a new scan.
335  */
336 int
337 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
338 	u_int duration, u_int mindwell, u_int maxdwell,
339 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
340 {
341 	struct ieee80211com *ic = vap->iv_ic;
342 	struct ieee80211_scan_state *ss = ic->ic_scan;
343 	const struct ieee80211_scanner *scan;
344 	int result;
345 
346 	scan = ieee80211_scanner_get(vap->iv_opmode);
347 	if (scan == NULL) {
348 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
349 		    "%s: no scanner support for %d mode\n",
350 		    __func__, vap->iv_opmode);
351 		/* XXX stat */
352 		return 0;
353 	}
354 
355 	/*
356 	 * Check if there's a list of scan candidates already.
357 	 * XXX want more than the ap we're currently associated with
358 	 */
359 
360 	IEEE80211_LOCK(ic);
361 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
362 	    "%s: %s scan, %s%s%s%s%s\n"
363 	    , __func__
364 	    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
365 	    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
366 	    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
367 	    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
368 	    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
369 	    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
370 	);
371 
372 	if (ss->ss_ops != scan) {
373 		/* XXX re-use cache contents? e.g. adhoc<->sta */
374 		flags |= IEEE80211_SCAN_FLUSH;
375 	}
376 
377 	/*
378 	 * XXX TODO: separate things out a bit better.
379 	 */
380 	ieee80211_scan_update_locked(vap, scan);
381 
382 	result = ic->ic_scan_methods->sc_check_scan(scan, vap, flags, duration,
383 	    mindwell, maxdwell, nssid, ssids);
384 
385 	IEEE80211_UNLOCK(ic);
386 
387 	return (result);
388 }
389 
390 /*
391  * Check the scan cache for an ap/channel to use; if that fails
392  * then kick off a scan using the current settings.
393  */
394 int
395 ieee80211_check_scan_current(struct ieee80211vap *vap)
396 {
397 	return ieee80211_check_scan(vap,
398 	    IEEE80211_SCAN_ACTIVE,
399 	    IEEE80211_SCAN_FOREVER, 0, 0,
400 	    vap->iv_des_nssid, vap->iv_des_ssid);
401 }
402 
403 /*
404  * Restart a previous scan.  If the previous scan completed
405  * then we start again using the existing channel list.
406  */
407 int
408 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
409 {
410 	struct ieee80211com *ic = vap->iv_ic;
411 	const struct ieee80211_scanner *scan;
412 
413 	// IEEE80211_UNLOCK_ASSERT(sc);
414 
415 	scan = ieee80211_scanner_get(vap->iv_opmode);
416 	if (scan == NULL) {
417 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
418 		    "%s: no scanner support for %d mode\n",
419 		    __func__, vap->iv_opmode);
420 		/* XXX stat */
421 		return 0;
422 	}
423 
424 	/*
425 	 * XXX TODO: pull apart the bgscan logic into whatever
426 	 * belongs here and whatever belongs in the software
427 	 * scanner.
428 	 */
429 	return (ic->ic_scan_methods->sc_bg_scan(scan, vap, flags));
430 }
431 
432 /*
433  * Cancel any scan currently going on for the specified vap.
434  */
435 void
436 ieee80211_cancel_scan(struct ieee80211vap *vap)
437 {
438 	struct ieee80211com *ic = vap->iv_ic;
439 
440 	ic->ic_scan_methods->sc_cancel_scan(vap);
441 }
442 
443 /*
444  * Cancel any scan currently going on.
445  */
446 void
447 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
448 {
449 	struct ieee80211com *ic = vap->iv_ic;
450 
451 	ic->ic_scan_methods->sc_cancel_anyscan(vap);
452 }
453 
454 /*
455  * Public access to scan_next for drivers that manage
456  * scanning themselves (e.g. for firmware-based devices).
457  */
458 void
459 ieee80211_scan_next(struct ieee80211vap *vap)
460 {
461 	struct ieee80211com *ic = vap->iv_ic;
462 
463 	ic->ic_scan_methods->sc_scan_next(vap);
464 }
465 
466 /*
467  * Public access to scan_next for drivers that are not able to scan single
468  * channels (e.g. for firmware-based devices).
469  */
470 void
471 ieee80211_scan_done(struct ieee80211vap *vap)
472 {
473 	struct ieee80211com *ic = vap->iv_ic;
474 	struct ieee80211_scan_state *ss;
475 
476 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
477 
478 	IEEE80211_LOCK(ic);
479 	ss = ic->ic_scan;
480 	ss->ss_next = ss->ss_last; /* all channels are complete */
481 
482 	ic->ic_scan_methods->sc_scan_done(vap);
483 
484 	IEEE80211_UNLOCK(ic);
485 }
486 
487 /*
488  * Probe the curent channel, if allowed, while scanning.
489  * If the channel is not marked passive-only then send
490  * a probe request immediately.  Otherwise mark state and
491  * listen for beacons on the channel; if we receive something
492  * then we'll transmit a probe request.
493  */
494 void
495 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
496 {
497 	struct ieee80211com *ic = vap->iv_ic;
498 
499 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
500 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
501 		return;
502 	}
503 
504 	ic->ic_scan_methods->sc_scan_probe_curchan(vap, force);
505 }
506 
507 #ifdef IEEE80211_DEBUG
508 static void
509 dump_country(const uint8_t *ie)
510 {
511 	const struct ieee80211_country_ie *cie =
512 	   (const struct ieee80211_country_ie *) ie;
513 	int i, nbands, schan, nchan;
514 
515 	if (cie->len < 3) {
516 		kprintf(" <bogus country ie, len %d>", cie->len);
517 		return;
518 	}
519 	kprintf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
520 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
521 	for (i = 0; i < nbands; i++) {
522 		schan = cie->band[i].schan;
523 		nchan = cie->band[i].nchan;
524 		if (nchan != 1)
525 			kprintf(" %u-%u,%u", schan, schan + nchan-1,
526 			    cie->band[i].maxtxpwr);
527 		else
528 			kprintf(" %u,%u", schan, cie->band[i].maxtxpwr);
529 	}
530 	kprintf("]");
531 }
532 
533 void
534 ieee80211_scan_dump_probe_beacon(uint8_t subtype, int isnew,
535 	const uint8_t mac[IEEE80211_ADDR_LEN],
536 	const struct ieee80211_scanparams *sp, int rssi)
537 {
538 
539 	kprintf("[%s] %s%s on chan %u (bss chan %u) ",
540 	    ether_sprintf(mac), isnew ? "new " : "",
541 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
542 	    sp->chan, sp->bchan);
543 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
544 	kprintf(" rssi %d\n", rssi);
545 
546 	if (isnew) {
547 		kprintf("[%s] caps 0x%x bintval %u erp 0x%x",
548 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
549 		if (sp->country != NULL)
550 			dump_country(sp->country);
551 		kprintf("\n");
552 	}
553 }
554 #endif /* IEEE80211_DEBUG */
555 
556 /*
557  * Process a beacon or probe response frame.
558  */
559 void
560 ieee80211_add_scan(struct ieee80211vap *vap,
561 	struct ieee80211_channel *curchan,
562 	const struct ieee80211_scanparams *sp,
563 	const struct ieee80211_frame *wh,
564 	int subtype, int rssi, int noise)
565 {
566 	struct ieee80211com *ic = vap->iv_ic;
567 
568 	return (ic->ic_scan_methods->sc_add_scan(vap, curchan, sp, wh, subtype,
569 	    rssi, noise));
570 }
571 
572 /*
573  * Timeout/age scan cache entries; called from sta timeout
574  * timer (XXX should be self-contained).
575  */
576 void
577 ieee80211_scan_timeout(struct ieee80211com *ic)
578 {
579 	struct ieee80211_scan_state *ss = ic->ic_scan;
580 
581 	if (ss->ss_ops != NULL)
582 		ss->ss_ops->scan_age(ss);
583 }
584 
585 /*
586  * Mark a scan cache entry after a successful associate.
587  */
588 void
589 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
590 {
591 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
592 
593 	if (ss->ss_ops != NULL) {
594 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
595 			mac, "%s",  __func__);
596 		ss->ss_ops->scan_assoc_success(ss, mac);
597 	}
598 }
599 
600 /*
601  * Demerit a scan cache entry after failing to associate.
602  */
603 void
604 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
605 	const uint8_t mac[], int reason)
606 {
607 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
608 
609 	if (ss->ss_ops != NULL) {
610 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
611 			"%s: reason %u", __func__, reason);
612 		ss->ss_ops->scan_assoc_fail(ss, mac, reason);
613 	}
614 }
615 
616 /*
617  * Iterate over the contents of the scan cache.
618  */
619 void
620 ieee80211_scan_iterate(struct ieee80211vap *vap,
621 	ieee80211_scan_iter_func *f, void *arg)
622 {
623 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
624 
625 	if (ss->ss_ops != NULL)
626 		ss->ss_ops->scan_iterate(ss, f, arg);
627 }
628 
629 /*
630  * Flush the contents of the scan cache.
631  */
632 void
633 ieee80211_scan_flush(struct ieee80211vap *vap)
634 {
635 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
636 
637 	if (ss->ss_ops != NULL && ss->ss_vap == vap) {
638 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
639 		ss->ss_ops->scan_flush(ss);
640 	}
641 }
642 
643 /*
644  * Check the scan cache for an ap/channel to use; if that
645  * fails then kick off a new scan.
646  */
647 struct ieee80211_channel *
648 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
649 {
650 	struct ieee80211_scan_state *ss = ic->ic_scan;
651 
652 	IEEE80211_LOCK_ASSERT(ic);
653 
654 	if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
655 		/* XXX printf? */
656 		return NULL;
657 	}
658 	if (ss->ss_ops->scan_pickchan == NULL) {
659 		IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
660 		    "%s: scan module does not support picking a channel, "
661 		    "opmode %d\n", __func__, ss->ss_vap->iv_opmode);
662 		return NULL;
663 	}
664 	return ss->ss_ops->scan_pickchan(ss, flags);
665 }
666