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