xref: /freebsd/sys/net80211/ieee80211_scan.c (revision 6a76ae216efd73fc435943a34162db007684ea9e)
168e8e04eSSam Leffler /*-
2b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
368e8e04eSSam Leffler  * All rights reserved.
468e8e04eSSam Leffler  *
568e8e04eSSam Leffler  * Redistribution and use in source and binary forms, with or without
668e8e04eSSam Leffler  * modification, are permitted provided that the following conditions
768e8e04eSSam Leffler  * are met:
868e8e04eSSam Leffler  * 1. Redistributions of source code must retain the above copyright
968e8e04eSSam Leffler  *    notice, this list of conditions and the following disclaimer.
1068e8e04eSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
1168e8e04eSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
1268e8e04eSSam Leffler  *    documentation and/or other materials provided with the distribution.
1368e8e04eSSam Leffler  *
1468e8e04eSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1568e8e04eSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1668e8e04eSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1768e8e04eSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1868e8e04eSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1968e8e04eSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2068e8e04eSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2168e8e04eSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2268e8e04eSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2368e8e04eSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2468e8e04eSSam Leffler  */
2568e8e04eSSam Leffler 
2668e8e04eSSam Leffler #include <sys/cdefs.h>
2768e8e04eSSam Leffler __FBSDID("$FreeBSD$");
2868e8e04eSSam Leffler 
2968e8e04eSSam Leffler /*
3068e8e04eSSam Leffler  * IEEE 802.11 scanning support.
3168e8e04eSSam Leffler  */
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler 
3468e8e04eSSam Leffler #include <sys/param.h>
3568e8e04eSSam Leffler #include <sys/systm.h>
3668e8e04eSSam Leffler #include <sys/kernel.h>
3768e8e04eSSam Leffler 
3868e8e04eSSam Leffler #include <sys/socket.h>
3968e8e04eSSam Leffler 
4068e8e04eSSam Leffler #include <net/if.h>
4168e8e04eSSam Leffler #include <net/if_media.h>
4268e8e04eSSam Leffler #include <net/ethernet.h>
4368e8e04eSSam Leffler 
4468e8e04eSSam Leffler #include <net80211/ieee80211_var.h>
4568e8e04eSSam Leffler 
4668e8e04eSSam Leffler #include <net/bpf.h>
4768e8e04eSSam Leffler 
4868e8e04eSSam Leffler struct scan_state {
4968e8e04eSSam Leffler 	struct ieee80211_scan_state base;	/* public state */
5068e8e04eSSam Leffler 
5168e8e04eSSam Leffler 	u_int		ss_iflags;		/* flags used internally */
5268e8e04eSSam Leffler #define	ISCAN_MINDWELL 	0x0001		/* min dwell time reached */
5368e8e04eSSam Leffler #define	ISCAN_DISCARD	0x0002		/* discard rx'd frames */
5468e8e04eSSam Leffler #define	ISCAN_CANCEL	0x0004		/* cancel current scan */
5568e8e04eSSam Leffler #define	ISCAN_START	0x0008		/* 1st time through next_scan */
5668e8e04eSSam Leffler 	unsigned long	ss_chanmindwell;	/* min dwell on curchan */
5768e8e04eSSam Leffler 	unsigned long	ss_scanend;		/* time scan must stop */
5868e8e04eSSam Leffler 	u_int		ss_duration;		/* duration for next scan */
5968e8e04eSSam Leffler 	struct callout	ss_scan_timer;		/* scan timer */
6068e8e04eSSam Leffler };
6168e8e04eSSam Leffler #define	SCAN_PRIVATE(ss)	((struct scan_state *) ss)
6268e8e04eSSam Leffler 
6368e8e04eSSam Leffler /*
6468e8e04eSSam Leffler  * Amount of time to go off-channel during a background
6568e8e04eSSam Leffler  * scan.  This value should be large enough to catch most
6668e8e04eSSam Leffler  * ap's but short enough that we can return on-channel
6768e8e04eSSam Leffler  * before our listen interval expires.
6868e8e04eSSam Leffler  *
6968e8e04eSSam Leffler  * XXX tunable
7068e8e04eSSam Leffler  * XXX check against configured listen interval
7168e8e04eSSam Leffler  */
7268e8e04eSSam Leffler #define	IEEE80211_SCAN_OFFCHANNEL	msecs_to_ticks(150)
7368e8e04eSSam Leffler 
7468e8e04eSSam Leffler /*
7568e8e04eSSam Leffler  * Roaming-related defaults.  RSSI thresholds are as returned by the
7682c990a4SSam Leffler  * driver (.5dBm).  Transmit rate thresholds are IEEE rate codes (i.e
77b032f27cSSam Leffler  * .5M units) or MCS.
7868e8e04eSSam Leffler  */
7982c990a4SSam Leffler /* rssi thresholds */
8082c990a4SSam Leffler #define	ROAM_RSSI_11A_DEFAULT		14	/* 11a bss */
8182c990a4SSam Leffler #define	ROAM_RSSI_11B_DEFAULT		14	/* 11b bss */
8282c990a4SSam Leffler #define	ROAM_RSSI_11BONLY_DEFAULT	14	/* 11b-only bss */
8382c990a4SSam Leffler /* transmit rate thresholds */
8482c990a4SSam Leffler #define	ROAM_RATE_11A_DEFAULT		2*12	/* 11a bss */
8582c990a4SSam Leffler #define	ROAM_RATE_11B_DEFAULT		2*5	/* 11b bss */
8682c990a4SSam Leffler #define	ROAM_RATE_11BONLY_DEFAULT	2*1	/* 11b-only bss */
876a76ae21SSam Leffler #define	ROAM_RATE_HALF_DEFAULT		2*6	/* half-width 11a/g bss */
886a76ae21SSam Leffler #define	ROAM_RATE_QUARTER_DEFAULT	2*3	/* quarter-width 11a/g bss */
8982c990a4SSam Leffler #define	ROAM_MCS_11N_DEFAULT		(1 | IEEE80211_RATE_MCS) /* 11n bss */
9068e8e04eSSam Leffler 
9168e8e04eSSam Leffler static	void scan_restart_pwrsav(void *);
92b032f27cSSam Leffler static	void scan_curchan(struct ieee80211_scan_state *, unsigned long);
93b032f27cSSam Leffler static	void scan_mindwell(struct ieee80211_scan_state *);
9468e8e04eSSam Leffler static	void scan_next(void *);
9568e8e04eSSam Leffler 
9668e8e04eSSam Leffler MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
9768e8e04eSSam Leffler 
9868e8e04eSSam Leffler void
9968e8e04eSSam Leffler ieee80211_scan_attach(struct ieee80211com *ic)
10068e8e04eSSam Leffler {
10168e8e04eSSam Leffler 	struct scan_state *ss;
10268e8e04eSSam Leffler 
103e2126decSSam Leffler 	ss = (struct scan_state *) malloc(sizeof(struct scan_state),
10468e8e04eSSam Leffler 		M_80211_SCAN, M_NOWAIT | M_ZERO);
10568e8e04eSSam Leffler 	if (ss == NULL) {
10668e8e04eSSam Leffler 		ic->ic_scan = NULL;
10768e8e04eSSam Leffler 		return;
10868e8e04eSSam Leffler 	}
109978359b3SSam Leffler 	callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
11068e8e04eSSam Leffler 	ic->ic_scan = &ss->base;
11168e8e04eSSam Leffler 
11268e8e04eSSam Leffler 	ic->ic_scan_curchan = scan_curchan;
11368e8e04eSSam Leffler 	ic->ic_scan_mindwell = scan_mindwell;
11468e8e04eSSam Leffler }
11568e8e04eSSam Leffler 
11668e8e04eSSam Leffler void
11768e8e04eSSam Leffler ieee80211_scan_detach(struct ieee80211com *ic)
11868e8e04eSSam Leffler {
11968e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
12068e8e04eSSam Leffler 
12168e8e04eSSam Leffler 	if (ss != NULL) {
12268e8e04eSSam Leffler 		callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
12368e8e04eSSam Leffler 		if (ss->ss_ops != NULL) {
12468e8e04eSSam Leffler 			ss->ss_ops->scan_detach(ss);
12568e8e04eSSam Leffler 			ss->ss_ops = NULL;
12668e8e04eSSam Leffler 		}
12768e8e04eSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SCAN;
12868e8e04eSSam Leffler 		ic->ic_scan = NULL;
129e2126decSSam Leffler 		free(SCAN_PRIVATE(ss), M_80211_SCAN);
13068e8e04eSSam Leffler 	}
13168e8e04eSSam Leffler }
13268e8e04eSSam Leffler 
13382c990a4SSam Leffler static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
13482c990a4SSam Leffler 	[IEEE80211_MODE_11A]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
13582c990a4SSam Leffler 				    .rate = ROAM_RATE_11A_DEFAULT },
13682c990a4SSam Leffler 	[IEEE80211_MODE_11G]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
13782c990a4SSam Leffler 				    .rate = ROAM_RATE_11B_DEFAULT },
13882c990a4SSam Leffler 	[IEEE80211_MODE_11B]	= { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
13982c990a4SSam Leffler 				    .rate = ROAM_RATE_11BONLY_DEFAULT },
14082c990a4SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
14182c990a4SSam Leffler 				    .rate = ROAM_RATE_11A_DEFAULT },
14282c990a4SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
14382c990a4SSam Leffler 				    .rate = ROAM_RATE_11A_DEFAULT },
14482c990a4SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
14582c990a4SSam Leffler 				    .rate = ROAM_RATE_11A_DEFAULT },
1466a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
1476a76ae21SSam Leffler 				    .rate = ROAM_RATE_HALF_DEFAULT },
1486a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
1496a76ae21SSam Leffler 				    .rate = ROAM_RATE_QUARTER_DEFAULT },
15082c990a4SSam Leffler 	[IEEE80211_MODE_11NA]	= { .rssi = ROAM_RSSI_11A_DEFAULT,
15182c990a4SSam Leffler 				    .rate = ROAM_MCS_11N_DEFAULT },
15282c990a4SSam Leffler 	[IEEE80211_MODE_11NG]	= { .rssi = ROAM_RSSI_11B_DEFAULT,
15382c990a4SSam Leffler 				    .rate = ROAM_MCS_11N_DEFAULT },
15482c990a4SSam Leffler };
155b032f27cSSam Leffler 
156b032f27cSSam Leffler void
157b032f27cSSam Leffler ieee80211_scan_vattach(struct ieee80211vap *vap)
158b032f27cSSam Leffler {
159b032f27cSSam Leffler 	vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
160b032f27cSSam Leffler 	vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
161b032f27cSSam Leffler 	vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
162b032f27cSSam Leffler 
163b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
16482c990a4SSam Leffler 	memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
165b032f27cSSam Leffler }
166b032f27cSSam Leffler 
167b032f27cSSam Leffler void
168b032f27cSSam Leffler ieee80211_scan_vdetach(struct ieee80211vap *vap)
169b032f27cSSam Leffler {
170b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
171b032f27cSSam Leffler 	struct ieee80211_scan_state *ss;
172b032f27cSSam Leffler 
173b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
174b032f27cSSam Leffler 	ss = ic->ic_scan;
175b032f27cSSam Leffler 	if (ss != NULL && ss->ss_vap == vap) {
176b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN) {
177b032f27cSSam Leffler 			/* XXX callout_drain */
178b032f27cSSam Leffler 			callout_stop(&SCAN_PRIVATE(ss)->ss_scan_timer);
179b032f27cSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SCAN;
180b032f27cSSam Leffler 		}
181b032f27cSSam Leffler 		if (ss->ss_ops != NULL) {
182b032f27cSSam Leffler 			ss->ss_ops->scan_detach(ss);
183b032f27cSSam Leffler 			ss->ss_ops = NULL;
184b032f27cSSam Leffler 		}
185b032f27cSSam Leffler 		ss->ss_vap = NULL;
186b032f27cSSam Leffler 	}
187b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
188b032f27cSSam Leffler }
189b032f27cSSam Leffler 
19068e8e04eSSam Leffler /*
19168e8e04eSSam Leffler  * Simple-minded scanner module support.
19268e8e04eSSam Leffler  */
193b032f27cSSam Leffler static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
19468e8e04eSSam Leffler 	"wlan_scan_sta",	/* IEEE80211_M_IBSS */
19568e8e04eSSam Leffler 	"wlan_scan_sta",	/* IEEE80211_M_STA */
19668e8e04eSSam Leffler 	"wlan_scan_wds",	/* IEEE80211_M_WDS */
19768e8e04eSSam Leffler 	"wlan_scan_sta",	/* IEEE80211_M_AHDEMO */
19868e8e04eSSam Leffler 	"wlan_scan_ap",		/* IEEE80211_M_HOSTAP */
19968e8e04eSSam Leffler 	"wlan_scan_monitor",	/* IEEE80211_M_MONITOR */
20068e8e04eSSam Leffler };
201b032f27cSSam Leffler static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
20268e8e04eSSam Leffler 
20368e8e04eSSam Leffler const struct ieee80211_scanner *
20468e8e04eSSam Leffler ieee80211_scanner_get(enum ieee80211_opmode mode)
20568e8e04eSSam Leffler {
206b032f27cSSam Leffler 	if (mode >= IEEE80211_OPMODE_MAX)
20768e8e04eSSam Leffler 		return NULL;
208b032f27cSSam Leffler 	if (scanners[mode] == NULL)
20968e8e04eSSam Leffler 		ieee80211_load_module(scan_modnames[mode]);
21068e8e04eSSam Leffler 	return scanners[mode];
21168e8e04eSSam Leffler }
21268e8e04eSSam Leffler 
21368e8e04eSSam Leffler void
21468e8e04eSSam Leffler ieee80211_scanner_register(enum ieee80211_opmode mode,
21568e8e04eSSam Leffler 	const struct ieee80211_scanner *scan)
21668e8e04eSSam Leffler {
217b032f27cSSam Leffler 	if (mode >= IEEE80211_OPMODE_MAX)
21868e8e04eSSam Leffler 		return;
21968e8e04eSSam Leffler 	scanners[mode] = scan;
22068e8e04eSSam Leffler }
22168e8e04eSSam Leffler 
22268e8e04eSSam Leffler void
22368e8e04eSSam Leffler ieee80211_scanner_unregister(enum ieee80211_opmode mode,
22468e8e04eSSam Leffler 	const struct ieee80211_scanner *scan)
22568e8e04eSSam Leffler {
226b032f27cSSam Leffler 	if (mode >= IEEE80211_OPMODE_MAX)
22768e8e04eSSam Leffler 		return;
22868e8e04eSSam Leffler 	if (scanners[mode] == scan)
22968e8e04eSSam Leffler 		scanners[mode] = NULL;
23068e8e04eSSam Leffler }
23168e8e04eSSam Leffler 
23268e8e04eSSam Leffler void
23368e8e04eSSam Leffler ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
23468e8e04eSSam Leffler {
23568e8e04eSSam Leffler 	int m;
23668e8e04eSSam Leffler 
237b032f27cSSam Leffler 	for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
23868e8e04eSSam Leffler 		if (scanners[m] == scan)
23968e8e04eSSam Leffler 			scanners[m] = NULL;
24068e8e04eSSam Leffler }
24168e8e04eSSam Leffler 
24268e8e04eSSam Leffler /*
24368e8e04eSSam Leffler  * Update common scanner state to reflect the current
24468e8e04eSSam Leffler  * operating mode.  This is called when the state machine
24568e8e04eSSam Leffler  * is transitioned to RUN state w/o scanning--e.g. when
24668e8e04eSSam Leffler  * operating in monitor mode.  The purpose of this is to
24768e8e04eSSam Leffler  * ensure later callbacks find ss_ops set to properly
24868e8e04eSSam Leffler  * reflect current operating mode.
24968e8e04eSSam Leffler  */
250b032f27cSSam Leffler static void
251b032f27cSSam Leffler scan_update_locked(struct ieee80211vap *vap,
252b032f27cSSam Leffler 	const struct ieee80211_scanner *scan)
25368e8e04eSSam Leffler {
254b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25568e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
25668e8e04eSSam Leffler 
257b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
258b032f27cSSam Leffler 
259b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
260b032f27cSSam Leffler 	if (ss->ss_vap != vap || ss->ss_ops != scan) {
261b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
262b032f27cSSam Leffler 		    "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
263b032f27cSSam Leffler 		    __func__,
264b032f27cSSam Leffler 		    ss->ss_vap != NULL ?
265b032f27cSSam Leffler 			ss->ss_vap->iv_ifp->if_xname : "none",
266b032f27cSSam Leffler 		    ss->ss_vap != NULL ?
267b032f27cSSam Leffler 			ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
268b032f27cSSam Leffler 		    vap->iv_ifp->if_xname,
269b032f27cSSam Leffler 		    ieee80211_opmode_name[vap->iv_opmode]);
27068e8e04eSSam Leffler 	}
271b032f27cSSam Leffler #endif
272b032f27cSSam Leffler 	ss->ss_vap = vap;
27368e8e04eSSam Leffler 	if (ss->ss_ops != scan) {
274b032f27cSSam Leffler 		/*
275b032f27cSSam Leffler 		 * Switch scanners; detach old, attach new.  Special
276b032f27cSSam Leffler 		 * case where a single scan module implements multiple
277b032f27cSSam Leffler 		 * policies by using different scan ops but a common
278b032f27cSSam Leffler 		 * core.  We assume if the old and new attach methods
279b032f27cSSam Leffler 		 * are identical then it's ok to just change ss_ops
280b032f27cSSam Leffler 		 * and not flush the internal state of the module.
281b032f27cSSam Leffler 		 */
282b032f27cSSam Leffler 		if (scan == NULL || ss->ss_ops == NULL ||
283b032f27cSSam Leffler 		    ss->ss_ops->scan_attach != scan->scan_attach) {
28468e8e04eSSam Leffler 			if (ss->ss_ops != NULL)
28568e8e04eSSam Leffler 				ss->ss_ops->scan_detach(ss);
28668e8e04eSSam Leffler 			if (scan != NULL && !scan->scan_attach(ss)) {
28768e8e04eSSam Leffler 				/* XXX attach failure */
28868e8e04eSSam Leffler 				/* XXX stat+msg */
289b032f27cSSam Leffler 				scan = NULL;
290b032f27cSSam Leffler 			}
291b032f27cSSam Leffler 		}
29268e8e04eSSam Leffler 		ss->ss_ops = scan;
29368e8e04eSSam Leffler 	}
29468e8e04eSSam Leffler }
29568e8e04eSSam Leffler 
29668e8e04eSSam Leffler static void
29768e8e04eSSam Leffler change_channel(struct ieee80211com *ic,
29868e8e04eSSam Leffler 	struct ieee80211_channel *chan)
29968e8e04eSSam Leffler {
30068e8e04eSSam Leffler 	ic->ic_curchan = chan;
30168e8e04eSSam Leffler 	ic->ic_set_channel(ic);
30268e8e04eSSam Leffler }
30368e8e04eSSam Leffler 
30468e8e04eSSam Leffler static char
30568e8e04eSSam Leffler channel_type(const struct ieee80211_channel *c)
30668e8e04eSSam Leffler {
30768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ST(c))
30868e8e04eSSam Leffler 		return 'S';
30968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_108A(c))
31068e8e04eSSam Leffler 		return 'T';
31168e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_108G(c))
31268e8e04eSSam Leffler 		return 'G';
31368e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(c))
31468e8e04eSSam Leffler 		return 'n';
31568e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(c))
31668e8e04eSSam Leffler 		return 'a';
31768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(c))
31868e8e04eSSam Leffler 		return 'g';
31968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_B(c))
32068e8e04eSSam Leffler 		return 'b';
32168e8e04eSSam Leffler 	return 'f';
32268e8e04eSSam Leffler }
32368e8e04eSSam Leffler 
32468e8e04eSSam Leffler void
32568e8e04eSSam Leffler ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
32668e8e04eSSam Leffler {
327b032f27cSSam Leffler 	struct ieee80211com *ic = ss->ss_vap->iv_ic;
32868e8e04eSSam Leffler 	const char *sep;
32968e8e04eSSam Leffler 	int i;
33068e8e04eSSam Leffler 
33168e8e04eSSam Leffler 	sep = "";
33268e8e04eSSam Leffler 	for (i = ss->ss_next; i < ss->ss_last; i++) {
33368e8e04eSSam Leffler 		const struct ieee80211_channel *c = ss->ss_chans[i];
33468e8e04eSSam Leffler 
33568e8e04eSSam Leffler 		printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
33668e8e04eSSam Leffler 			channel_type(c));
33768e8e04eSSam Leffler 		sep = ", ";
33868e8e04eSSam Leffler 	}
33968e8e04eSSam Leffler }
34068e8e04eSSam Leffler 
341b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
342b032f27cSSam Leffler static void
343b032f27cSSam Leffler scan_dump(struct ieee80211_scan_state *ss)
344b032f27cSSam Leffler {
345b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->ss_vap;
346b032f27cSSam Leffler 
347b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "scan set ");
348b032f27cSSam Leffler 	ieee80211_scan_dump_channels(ss);
349b032f27cSSam Leffler 	printf(" dwell min %lu max %lu\n", ss->ss_mindwell, ss->ss_maxdwell);
350b032f27cSSam Leffler }
351b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
352b032f27cSSam Leffler 
35368e8e04eSSam Leffler /*
35468e8e04eSSam Leffler  * Enable station power save mode and start/restart the scanning thread.
35568e8e04eSSam Leffler  */
35668e8e04eSSam Leffler static void
35768e8e04eSSam Leffler scan_restart_pwrsav(void *arg)
35868e8e04eSSam Leffler {
35968e8e04eSSam Leffler 	struct scan_state *ss = (struct scan_state *) arg;
360b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->base.ss_vap;
361b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
362b032f27cSSam Leffler 	int ticksdelay;
36368e8e04eSSam Leffler 
364b032f27cSSam Leffler 	ieee80211_sta_pwrsave(vap, 1);
36568e8e04eSSam Leffler 	/*
366b032f27cSSam Leffler 	 * Use an initial 1ms delay so the null
36768e8e04eSSam Leffler 	 * data frame has a chance to go out.
36868e8e04eSSam Leffler 	 * XXX 1ms is a lot, better to trigger scan
36968e8e04eSSam Leffler 	 * on tx complete.
37068e8e04eSSam Leffler 	 */
371b032f27cSSam Leffler 	ticksdelay = msecs_to_ticks(1);
372b032f27cSSam Leffler 	if (ticksdelay < 1)
373b032f27cSSam Leffler 		ticksdelay = 1;
37468e8e04eSSam Leffler 	ic->ic_scan_start(ic);			/* notify driver */
375b032f27cSSam Leffler 	ss->ss_scanend = ticks + ticksdelay + ss->ss_duration;
37668e8e04eSSam Leffler 	ss->ss_iflags |= ISCAN_START;
377b032f27cSSam Leffler 	callout_reset(&ss->ss_scan_timer, ticksdelay, scan_next, ss);
37868e8e04eSSam Leffler }
37968e8e04eSSam Leffler 
38068e8e04eSSam Leffler /*
38168e8e04eSSam Leffler  * Start/restart scanning.  If we're operating in station mode
38268e8e04eSSam Leffler  * and associated notify the ap we're going into power save mode
38368e8e04eSSam Leffler  * and schedule a callback to initiate the work (where there's a
38468e8e04eSSam Leffler  * better context for doing the work).  Otherwise, start the scan
38568e8e04eSSam Leffler  * directly.
38668e8e04eSSam Leffler  */
38768e8e04eSSam Leffler static int
38868e8e04eSSam Leffler scan_restart(struct scan_state *ss, u_int duration)
38968e8e04eSSam Leffler {
390b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->base.ss_vap;
391b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
39268e8e04eSSam Leffler 	int defer = 0;
39368e8e04eSSam Leffler 
39468e8e04eSSam Leffler 	if (ss->base.ss_next == ss->base.ss_last) {
395b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
39668e8e04eSSam Leffler 			"%s: no channels to scan\n", __func__);
39768e8e04eSSam Leffler 		return 0;
39868e8e04eSSam Leffler 	}
399b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
400b032f27cSSam Leffler 	    vap->iv_state == IEEE80211_S_RUN) {
401b032f27cSSam Leffler 		if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
40268e8e04eSSam Leffler 			/*
40368e8e04eSSam Leffler 			 * Initiate power save before going off-channel.
40468e8e04eSSam Leffler 			 * Note that we cannot do this directly because
40568e8e04eSSam Leffler 			 * of locking issues; instead we defer it to a
40668e8e04eSSam Leffler 			 * tasklet.
40768e8e04eSSam Leffler 			 */
40868e8e04eSSam Leffler 			ss->ss_duration = duration;
40968e8e04eSSam Leffler 			defer = 1;
41068e8e04eSSam Leffler 		}
41168e8e04eSSam Leffler 	}
41268e8e04eSSam Leffler 
41368e8e04eSSam Leffler 	if (!defer) {
41468e8e04eSSam Leffler 		ic->ic_scan_start(ic);		/* notify driver */
41568e8e04eSSam Leffler 		ss->ss_scanend = ticks + duration;
41668e8e04eSSam Leffler 		ss->ss_iflags |= ISCAN_START;
41768e8e04eSSam Leffler 		callout_reset(&ss->ss_scan_timer, 0, scan_next, ss);
41868e8e04eSSam Leffler 	} else
41968e8e04eSSam Leffler 		scan_restart_pwrsav(ss);
42068e8e04eSSam Leffler 	return 1;
42168e8e04eSSam Leffler }
42268e8e04eSSam Leffler 
42368e8e04eSSam Leffler static void
424b032f27cSSam Leffler copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
42568e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
42668e8e04eSSam Leffler {
42768e8e04eSSam Leffler 	if (nssid > IEEE80211_SCAN_MAX_SSID) {
42868e8e04eSSam Leffler 		/* XXX printf */
429b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
43068e8e04eSSam Leffler 		    "%s: too many ssid %d, ignoring all of them\n",
43168e8e04eSSam Leffler 		    __func__, nssid);
43268e8e04eSSam Leffler 		return;
43368e8e04eSSam Leffler 	}
43468e8e04eSSam Leffler 	memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
43568e8e04eSSam Leffler 	ss->ss_nssid = nssid;
43668e8e04eSSam Leffler }
43768e8e04eSSam Leffler 
43868e8e04eSSam Leffler /*
43968e8e04eSSam Leffler  * Start a scan unless one is already going.
44068e8e04eSSam Leffler  */
441b032f27cSSam Leffler static int
442b032f27cSSam Leffler start_scan_locked(const struct ieee80211_scanner *scan,
443b032f27cSSam Leffler 	struct ieee80211vap *vap, int flags, u_int duration,
444b032f27cSSam Leffler 	u_int mindwell, u_int maxdwell,
44568e8e04eSSam Leffler 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
44668e8e04eSSam Leffler {
447b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
44868e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
44968e8e04eSSam Leffler 
450b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
45168e8e04eSSam Leffler 
452b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
453b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
454b032f27cSSam Leffler 		    "%s: scan inhibited by pending channel change\n", __func__);
455b032f27cSSam Leffler 	} else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
456b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
457b032f27cSSam Leffler 		    "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
45868e8e04eSSam Leffler 		    , __func__
45968e8e04eSSam Leffler 		    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
460b032f27cSSam Leffler 		    , duration, mindwell, maxdwell
461b032f27cSSam Leffler 		    , ieee80211_phymode_name[vap->iv_des_mode]
46268e8e04eSSam Leffler 		    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
46368e8e04eSSam Leffler 		    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
464b032f27cSSam Leffler 		    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
465b032f27cSSam Leffler 		    , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
46668e8e04eSSam Leffler 		    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
46768e8e04eSSam Leffler 		    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
46868e8e04eSSam Leffler 		);
46968e8e04eSSam Leffler 
470b032f27cSSam Leffler 		scan_update_locked(vap, scan);
47168e8e04eSSam Leffler 		if (ss->ss_ops != NULL) {
47268e8e04eSSam Leffler 			if ((flags & IEEE80211_SCAN_NOSSID) == 0)
473b032f27cSSam Leffler 				copy_ssid(vap, ss, nssid, ssids);
47468e8e04eSSam Leffler 
47568e8e04eSSam Leffler 			/* NB: top 4 bits for internal use */
47668e8e04eSSam Leffler 			ss->ss_flags = flags & 0xfff;
47768e8e04eSSam Leffler 			if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
478b032f27cSSam Leffler 				vap->iv_stats.is_scan_active++;
47968e8e04eSSam Leffler 			else
480b032f27cSSam Leffler 				vap->iv_stats.is_scan_passive++;
48168e8e04eSSam Leffler 			if (flags & IEEE80211_SCAN_FLUSH)
48268e8e04eSSam Leffler 				ss->ss_ops->scan_flush(ss);
48368e8e04eSSam Leffler 
48468e8e04eSSam Leffler 			/* NB: flush frames rx'd before 1st channel change */
48568e8e04eSSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
486b032f27cSSam Leffler 			ss->ss_next = 0;
487b032f27cSSam Leffler 			ss->ss_mindwell = mindwell;
488b032f27cSSam Leffler 			ss->ss_maxdwell = maxdwell;
489b032f27cSSam Leffler 			ss->ss_ops->scan_start(ss, vap);
490b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
491b032f27cSSam Leffler 			if (ieee80211_msg_scan(vap))
492b032f27cSSam Leffler 				scan_dump(ss);
493b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
49468e8e04eSSam Leffler 			if (scan_restart(SCAN_PRIVATE(ss), duration))
49568e8e04eSSam Leffler 				ic->ic_flags |= IEEE80211_F_SCAN;
49668e8e04eSSam Leffler 		}
49768e8e04eSSam Leffler 	} else {
498b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
49968e8e04eSSam Leffler 		    "%s: %s scan already in progress\n", __func__,
50068e8e04eSSam Leffler 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
50168e8e04eSSam Leffler 	}
502b032f27cSSam Leffler 	return (ic->ic_flags & IEEE80211_F_SCAN);
503b032f27cSSam Leffler }
504b032f27cSSam Leffler 
505b032f27cSSam Leffler /*
506b032f27cSSam Leffler  * Start a scan unless one is already going.
507b032f27cSSam Leffler  */
508b032f27cSSam Leffler int
509b032f27cSSam Leffler ieee80211_start_scan(struct ieee80211vap *vap, int flags,
510b032f27cSSam Leffler 	u_int duration, u_int mindwell, u_int maxdwell,
511b032f27cSSam Leffler 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
512b032f27cSSam Leffler {
513b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
514b032f27cSSam Leffler 	const struct ieee80211_scanner *scan;
515b032f27cSSam Leffler 	int result;
516b032f27cSSam Leffler 
517b032f27cSSam Leffler 	scan = ieee80211_scanner_get(vap->iv_opmode);
518b032f27cSSam Leffler 	if (scan == NULL) {
519b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
520b032f27cSSam Leffler 		    "%s: no scanner support for %s mode\n",
521b032f27cSSam Leffler 		    __func__, ieee80211_opmode_name[vap->iv_opmode]);
522b032f27cSSam Leffler 		/* XXX stat */
523b032f27cSSam Leffler 		return 0;
524b032f27cSSam Leffler 	}
525b032f27cSSam Leffler 
526b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
527b032f27cSSam Leffler 	result = start_scan_locked(scan, vap, flags, duration,
528b032f27cSSam Leffler 	    mindwell, maxdwell, nssid, ssids);
52968e8e04eSSam Leffler 	IEEE80211_UNLOCK(ic);
53068e8e04eSSam Leffler 
531b032f27cSSam Leffler 	return result;
53268e8e04eSSam Leffler }
53368e8e04eSSam Leffler 
53468e8e04eSSam Leffler /*
53568e8e04eSSam Leffler  * Check the scan cache for an ap/channel to use; if that
53668e8e04eSSam Leffler  * fails then kick off a new scan.
53768e8e04eSSam Leffler  */
53868e8e04eSSam Leffler int
539b032f27cSSam Leffler ieee80211_check_scan(struct ieee80211vap *vap, int flags,
540b032f27cSSam Leffler 	u_int duration, u_int mindwell, u_int maxdwell,
54168e8e04eSSam Leffler 	u_int nssid, const struct ieee80211_scan_ssid ssids[])
54268e8e04eSSam Leffler {
543b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
54468e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
545b032f27cSSam Leffler 	const struct ieee80211_scanner *scan;
546aa0fbc73SSam Leffler 	int result;
547b032f27cSSam Leffler 
548b032f27cSSam Leffler 	scan = ieee80211_scanner_get(vap->iv_opmode);
549b032f27cSSam Leffler 	if (scan == NULL) {
550b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
551b032f27cSSam Leffler 		    "%s: no scanner support for %s mode\n",
552b032f27cSSam Leffler 		    __func__, vap->iv_opmode);
553b032f27cSSam Leffler 		/* XXX stat */
554b032f27cSSam Leffler 		return 0;
555b032f27cSSam Leffler 	}
55668e8e04eSSam Leffler 
55768e8e04eSSam Leffler 	/*
55868e8e04eSSam Leffler 	 * Check if there's a list of scan candidates already.
55968e8e04eSSam Leffler 	 * XXX want more than the ap we're currently associated with
56068e8e04eSSam Leffler 	 */
56168e8e04eSSam Leffler 
56268e8e04eSSam Leffler 	IEEE80211_LOCK(ic);
563b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
564b032f27cSSam Leffler 	    "%s: %s scan, %s%s%s%s%s\n"
56568e8e04eSSam Leffler 	    , __func__
56668e8e04eSSam Leffler 	    , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
56768e8e04eSSam Leffler 	    , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
56868e8e04eSSam Leffler 	    , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
569b032f27cSSam Leffler 	    , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
57068e8e04eSSam Leffler 	    , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
57168e8e04eSSam Leffler 	    , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
57268e8e04eSSam Leffler 	);
57368e8e04eSSam Leffler 
574b032f27cSSam Leffler 	if (ss->ss_ops != scan) {
575b032f27cSSam Leffler 		/* XXX re-use cache contents? e.g. adhoc<->sta */
576b032f27cSSam Leffler 		flags |= IEEE80211_SCAN_FLUSH;
577b032f27cSSam Leffler 	}
578b032f27cSSam Leffler 	scan_update_locked(vap, scan);
57968e8e04eSSam Leffler 	if (ss->ss_ops != NULL) {
580b032f27cSSam Leffler 		/* XXX verify ss_ops matches vap->iv_opmode */
58168e8e04eSSam Leffler 		if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
58268e8e04eSSam Leffler 			/*
58368e8e04eSSam Leffler 			 * Update the ssid list and mark flags so if
58468e8e04eSSam Leffler 			 * we call start_scan it doesn't duplicate work.
58568e8e04eSSam Leffler 			 */
586b032f27cSSam Leffler 			copy_ssid(vap, ss, nssid, ssids);
58768e8e04eSSam Leffler 			flags |= IEEE80211_SCAN_NOSSID;
58868e8e04eSSam Leffler 		}
58968e8e04eSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
590b032f27cSSam Leffler 		    (flags & IEEE80211_SCAN_FLUSH) == 0 &&
591b032f27cSSam Leffler 		    time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
59268e8e04eSSam Leffler 			/*
59368e8e04eSSam Leffler 			 * We're not currently scanning and the cache is
59468e8e04eSSam Leffler 			 * deemed hot enough to consult.  Lock out others
59568e8e04eSSam Leffler 			 * by marking IEEE80211_F_SCAN while we decide if
59668e8e04eSSam Leffler 			 * something is already in the scan cache we can
59768e8e04eSSam Leffler 			 * use.  Also discard any frames that might come
59868e8e04eSSam Leffler 			 * in while temporarily marked as scanning.
59968e8e04eSSam Leffler 			 */
60068e8e04eSSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
60168e8e04eSSam Leffler 			ic->ic_flags |= IEEE80211_F_SCAN;
602aa0fbc73SSam Leffler 
603aa0fbc73SSam Leffler 			/* NB: need to use supplied flags in check */
604b032f27cSSam Leffler 			ss->ss_flags = flags & 0xff;
605aa0fbc73SSam Leffler 			result = ss->ss_ops->scan_end(ss, vap);
606aa0fbc73SSam Leffler 
60768e8e04eSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SCAN;
608aa0fbc73SSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
609aa0fbc73SSam Leffler 			if (result) {
610b032f27cSSam Leffler 				ieee80211_notify_scan_done(vap);
611b032f27cSSam Leffler 				IEEE80211_UNLOCK(ic);
61268e8e04eSSam Leffler 				return 1;
61368e8e04eSSam Leffler 			}
614aa0fbc73SSam Leffler 		}
61568e8e04eSSam Leffler 	}
616b032f27cSSam Leffler 	result = start_scan_locked(scan, vap, flags, duration,
617b032f27cSSam Leffler 	    mindwell, maxdwell, nssid, ssids);
618b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
619b032f27cSSam Leffler 
620b032f27cSSam Leffler 	return result;
621b032f27cSSam Leffler }
622b032f27cSSam Leffler 
623b032f27cSSam Leffler /*
624b032f27cSSam Leffler  * Check the scan cache for an ap/channel to use; if that fails
625b032f27cSSam Leffler  * then kick off a scan using the current settings.
626b032f27cSSam Leffler  */
627b032f27cSSam Leffler int
628b032f27cSSam Leffler ieee80211_check_scan_current(struct ieee80211vap *vap)
629b032f27cSSam Leffler {
630b032f27cSSam Leffler 	return ieee80211_check_scan(vap,
631b032f27cSSam Leffler 	    IEEE80211_SCAN_ACTIVE,
632b032f27cSSam Leffler 	    IEEE80211_SCAN_FOREVER, 0, 0,
633b032f27cSSam Leffler 	    vap->iv_des_nssid, vap->iv_des_ssid);
63468e8e04eSSam Leffler }
63568e8e04eSSam Leffler 
63668e8e04eSSam Leffler /*
63768e8e04eSSam Leffler  * Restart a previous scan.  If the previous scan completed
63868e8e04eSSam Leffler  * then we start again using the existing channel list.
63968e8e04eSSam Leffler  */
64068e8e04eSSam Leffler int
641b032f27cSSam Leffler ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
64268e8e04eSSam Leffler {
643b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
64468e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
645b032f27cSSam Leffler 	const struct ieee80211_scanner *scan;
646b032f27cSSam Leffler 
647b032f27cSSam Leffler 	scan = ieee80211_scanner_get(vap->iv_opmode);
648b032f27cSSam Leffler 	if (scan == NULL) {
649b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
650b032f27cSSam Leffler 		    "%s: no scanner support for %s mode\n",
651b032f27cSSam Leffler 		    __func__, vap->iv_opmode);
652b032f27cSSam Leffler 		/* XXX stat */
653b032f27cSSam Leffler 		return 0;
654b032f27cSSam Leffler 	}
65568e8e04eSSam Leffler 
65668e8e04eSSam Leffler 	IEEE80211_LOCK(ic);
65768e8e04eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
65868e8e04eSSam Leffler 		u_int duration;
65968e8e04eSSam Leffler 		/*
66068e8e04eSSam Leffler 		 * Go off-channel for a fixed interval that is large
66168e8e04eSSam Leffler 		 * enough to catch most ap's but short enough that
66268e8e04eSSam Leffler 		 * we can return on-channel before our listen interval
66368e8e04eSSam Leffler 		 * expires.
66468e8e04eSSam Leffler 		 */
66568e8e04eSSam Leffler 		duration = IEEE80211_SCAN_OFFCHANNEL;
66668e8e04eSSam Leffler 
667b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
66868e8e04eSSam Leffler 		    "%s: %s scan, ticks %u duration %lu\n", __func__,
66968e8e04eSSam Leffler 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
67068e8e04eSSam Leffler 		    ticks, duration);
67168e8e04eSSam Leffler 
672b032f27cSSam Leffler 		scan_update_locked(vap, scan);
67368e8e04eSSam Leffler 		if (ss->ss_ops != NULL) {
674b032f27cSSam Leffler 			ss->ss_vap = vap;
67568e8e04eSSam Leffler 			/*
67668e8e04eSSam Leffler 			 * A background scan does not select a new sta; it
67768e8e04eSSam Leffler 			 * just refreshes the scan cache.  Also, indicate
67868e8e04eSSam Leffler 			 * the scan logic should follow the beacon schedule:
67968e8e04eSSam Leffler 			 * we go off-channel and scan for a while, then
68068e8e04eSSam Leffler 			 * return to the bss channel to receive a beacon,
68168e8e04eSSam Leffler 			 * then go off-channel again.  All during this time
68268e8e04eSSam Leffler 			 * we notify the ap we're in power save mode.  When
68368e8e04eSSam Leffler 			 * the scan is complete we leave power save mode.
68468e8e04eSSam Leffler 			 * If any beacon indicates there are frames pending
68568e8e04eSSam Leffler 			 * for us then we drop out of power save mode
68668e8e04eSSam Leffler 			 * (and background scan) automatically by way of the
68768e8e04eSSam Leffler 			 * usual sta power save logic.
68868e8e04eSSam Leffler 			 */
68968e8e04eSSam Leffler 			ss->ss_flags |= IEEE80211_SCAN_NOPICK
690b032f27cSSam Leffler 				     |  IEEE80211_SCAN_BGSCAN
691b032f27cSSam Leffler 				     |  flags
692b032f27cSSam Leffler 				     ;
69368e8e04eSSam Leffler 			/* if previous scan completed, restart */
69468e8e04eSSam Leffler 			if (ss->ss_next >= ss->ss_last) {
69568e8e04eSSam Leffler 				if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
696b032f27cSSam Leffler 					vap->iv_stats.is_scan_active++;
69768e8e04eSSam Leffler 				else
698b032f27cSSam Leffler 					vap->iv_stats.is_scan_passive++;
699b032f27cSSam Leffler 				/*
700b032f27cSSam Leffler 				 * NB: beware of the scan cache being flushed;
701b032f27cSSam Leffler 				 *     if the channel list is empty use the
702b032f27cSSam Leffler 				 *     scan_start method to populate it.
703b032f27cSSam Leffler 				 */
704b032f27cSSam Leffler 				ss->ss_next = 0;
705b032f27cSSam Leffler 				if (ss->ss_last != 0)
706b032f27cSSam Leffler 					ss->ss_ops->scan_restart(ss, vap);
707b032f27cSSam Leffler 				else {
708b032f27cSSam Leffler 					ss->ss_ops->scan_start(ss, vap);
709b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
710b032f27cSSam Leffler 					if (ieee80211_msg_scan(vap))
711b032f27cSSam Leffler 						scan_dump(ss);
712b032f27cSSam Leffler #endif /* IEEE80211_DEBUG */
713b032f27cSSam Leffler 				}
71468e8e04eSSam Leffler 			}
71568e8e04eSSam Leffler 			/* NB: flush frames rx'd before 1st channel change */
71668e8e04eSSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
71768e8e04eSSam Leffler 			ss->ss_maxdwell = duration;
71868e8e04eSSam Leffler 			if (scan_restart(SCAN_PRIVATE(ss), duration)) {
71968e8e04eSSam Leffler 				ic->ic_flags |= IEEE80211_F_SCAN;
72068e8e04eSSam Leffler 				ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
72168e8e04eSSam Leffler 			}
72268e8e04eSSam Leffler 		} else {
72368e8e04eSSam Leffler 			/* XXX msg+stat */
72468e8e04eSSam Leffler 		}
72568e8e04eSSam Leffler 	} else {
726b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
72768e8e04eSSam Leffler 		    "%s: %s scan already in progress\n", __func__,
72868e8e04eSSam Leffler 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
72968e8e04eSSam Leffler 	}
73068e8e04eSSam Leffler 	IEEE80211_UNLOCK(ic);
73168e8e04eSSam Leffler 
73268e8e04eSSam Leffler 	/* NB: racey, does it matter? */
73368e8e04eSSam Leffler 	return (ic->ic_flags & IEEE80211_F_SCAN);
73468e8e04eSSam Leffler }
73568e8e04eSSam Leffler 
73668e8e04eSSam Leffler /*
737b032f27cSSam Leffler  * Cancel any scan currently going on for the specified vap.
738b032f27cSSam Leffler  */
739b032f27cSSam Leffler void
740b032f27cSSam Leffler ieee80211_cancel_scan(struct ieee80211vap *vap)
741b032f27cSSam Leffler {
742b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
743b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
744b032f27cSSam Leffler 
745b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
746b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
747b032f27cSSam Leffler 	    ss->ss_vap == vap &&
748b032f27cSSam Leffler 	    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
749b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
750b032f27cSSam Leffler 		    "%s: cancel %s scan\n", __func__,
751b032f27cSSam Leffler 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
752b032f27cSSam Leffler 			"active" : "passive");
753b032f27cSSam Leffler 
754b032f27cSSam Leffler 		/* clear bg scan NOPICK and mark cancel request */
755b032f27cSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
756b032f27cSSam Leffler 		SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
757b032f27cSSam Leffler 		/* force it to fire asap */
758b032f27cSSam Leffler 		callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
759b032f27cSSam Leffler 			0, scan_next, ss);
760b032f27cSSam Leffler 	}
761b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
762b032f27cSSam Leffler }
763b032f27cSSam Leffler 
764b032f27cSSam Leffler /*
76568e8e04eSSam Leffler  * Cancel any scan currently going on.
76668e8e04eSSam Leffler  */
76768e8e04eSSam Leffler void
768b032f27cSSam Leffler ieee80211_cancel_anyscan(struct ieee80211vap *vap)
76968e8e04eSSam Leffler {
770b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
77168e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
77268e8e04eSSam Leffler 
77368e8e04eSSam Leffler 	IEEE80211_LOCK(ic);
77468e8e04eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
77568e8e04eSSam Leffler 	    (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
776b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
77768e8e04eSSam Leffler 		    "%s: cancel %s scan\n", __func__,
77868e8e04eSSam Leffler 		    ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
77968e8e04eSSam Leffler 			"active" : "passive");
78068e8e04eSSam Leffler 
78168e8e04eSSam Leffler 		/* clear bg scan NOPICK and mark cancel request */
78268e8e04eSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
78368e8e04eSSam Leffler 		SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
78468e8e04eSSam Leffler 		/* force it to fire asap */
78568e8e04eSSam Leffler 		callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
78668e8e04eSSam Leffler 			0, scan_next, ss);
78768e8e04eSSam Leffler 	}
78868e8e04eSSam Leffler 	IEEE80211_UNLOCK(ic);
78968e8e04eSSam Leffler }
79068e8e04eSSam Leffler 
79168e8e04eSSam Leffler /*
79268e8e04eSSam Leffler  * Public access to scan_next for drivers that manage
79368e8e04eSSam Leffler  * scanning themselves (e.g. for firmware-based devices).
79468e8e04eSSam Leffler  */
79568e8e04eSSam Leffler void
796b032f27cSSam Leffler ieee80211_scan_next(struct ieee80211vap *vap)
79768e8e04eSSam Leffler {
798b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
799b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
800b032f27cSSam Leffler 
801b032f27cSSam Leffler 	callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
80268e8e04eSSam Leffler }
80368e8e04eSSam Leffler 
80468e8e04eSSam Leffler /*
805d81b3a55SAndrew Thompson  * Public access to scan_next for drivers that are not able to scan single
806d81b3a55SAndrew Thompson  * channels (e.g. for firmware-based devices).
807d81b3a55SAndrew Thompson  */
808d81b3a55SAndrew Thompson void
809b032f27cSSam Leffler ieee80211_scan_done(struct ieee80211vap *vap)
810d81b3a55SAndrew Thompson {
811b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
812b032f27cSSam Leffler 	struct ieee80211_scan_state *ss;
813d81b3a55SAndrew Thompson 
814b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
815b032f27cSSam Leffler 	ss = ic->ic_scan;
816d81b3a55SAndrew Thompson 	ss->ss_next = ss->ss_last; /* all channels are complete */
817d81b3a55SAndrew Thompson 	scan_next(ss);
818b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
819b032f27cSSam Leffler }
820b032f27cSSam Leffler 
821b032f27cSSam Leffler /*
822b032f27cSSam Leffler  * Probe the curent channel, if allowed, while scanning.
823b032f27cSSam Leffler  * If the channel is not marked passive-only then send
824b032f27cSSam Leffler  * a probe request immediately.  Otherwise mark state and
825b032f27cSSam Leffler  * listen for beacons on the channel; if we receive something
826b032f27cSSam Leffler  * then we'll transmit a probe request.
827b032f27cSSam Leffler  */
828b032f27cSSam Leffler void
829b032f27cSSam Leffler ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
830b032f27cSSam Leffler {
831b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
832b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
833b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
834b032f27cSSam Leffler 	int i;
835b032f27cSSam Leffler 
836b032f27cSSam Leffler 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
837b032f27cSSam Leffler 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
838b032f27cSSam Leffler 		return;
839b032f27cSSam Leffler 	}
840b032f27cSSam Leffler 	/*
841b032f27cSSam Leffler 	 * Send directed probe requests followed by any
842b032f27cSSam Leffler 	 * broadcast probe request.
843b032f27cSSam Leffler 	 * XXX remove dependence on ic/vap->iv_bss
844b032f27cSSam Leffler 	 */
845b032f27cSSam Leffler 	for (i = 0; i < ss->ss_nssid; i++)
846b032f27cSSam Leffler 		ieee80211_send_probereq(vap->iv_bss,
847b032f27cSSam Leffler 			vap->iv_myaddr, ifp->if_broadcastaddr,
848b032f27cSSam Leffler 			ifp->if_broadcastaddr,
849b032f27cSSam Leffler 			ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
850b032f27cSSam Leffler 	if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
851b032f27cSSam Leffler 		ieee80211_send_probereq(vap->iv_bss,
852b032f27cSSam Leffler 			vap->iv_myaddr, ifp->if_broadcastaddr,
853b032f27cSSam Leffler 			ifp->if_broadcastaddr,
854b032f27cSSam Leffler 			"", 0);
855d81b3a55SAndrew Thompson }
856d81b3a55SAndrew Thompson 
857d81b3a55SAndrew Thompson /*
85868e8e04eSSam Leffler  * Scan curchan.  If this is an active scan and the channel
85968e8e04eSSam Leffler  * is not marked passive then send probe request frame(s).
86068e8e04eSSam Leffler  * Arrange for the channel change after maxdwell ticks.
86168e8e04eSSam Leffler  */
86268e8e04eSSam Leffler static void
863b032f27cSSam Leffler scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
86468e8e04eSSam Leffler {
865b032f27cSSam Leffler 	struct ieee80211vap *vap  = ss->ss_vap;
86668e8e04eSSam Leffler 
867b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
86868e8e04eSSam Leffler 
869b032f27cSSam Leffler 	if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
870b032f27cSSam Leffler 		ieee80211_probe_curchan(vap, 0);
87168e8e04eSSam Leffler 	callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
87268e8e04eSSam Leffler 	    maxdwell, scan_next, ss);
87368e8e04eSSam Leffler }
87468e8e04eSSam Leffler 
87568e8e04eSSam Leffler /*
87668e8e04eSSam Leffler  * Handle mindwell requirements completed; initiate a channel
87768e8e04eSSam Leffler  * change to the next channel asap.
87868e8e04eSSam Leffler  */
87968e8e04eSSam Leffler static void
880b032f27cSSam Leffler scan_mindwell(struct ieee80211_scan_state *ss)
88168e8e04eSSam Leffler {
88268e8e04eSSam Leffler 	callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
88368e8e04eSSam Leffler }
88468e8e04eSSam Leffler 
88568e8e04eSSam Leffler /*
88668e8e04eSSam Leffler  * Switch to the next channel marked for scanning.
88768e8e04eSSam Leffler  */
88868e8e04eSSam Leffler static void
88968e8e04eSSam Leffler scan_next(void *arg)
89068e8e04eSSam Leffler {
89168e8e04eSSam Leffler #define	ISCAN_REP	(ISCAN_MINDWELL | ISCAN_START | ISCAN_DISCARD)
89268e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
893b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->ss_vap;
894b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
89568e8e04eSSam Leffler 	struct ieee80211_channel *chan;
89668e8e04eSSam Leffler 	unsigned long maxdwell, scanend;
897b032f27cSSam Leffler 	int scandone;
89868e8e04eSSam Leffler 
899b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
900b032f27cSSam Leffler 
901b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0)
90268e8e04eSSam Leffler 		return;
90368e8e04eSSam Leffler again:
90468e8e04eSSam Leffler 	scandone = (ss->ss_next >= ss->ss_last) ||
90568e8e04eSSam Leffler 		(SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
90668e8e04eSSam Leffler 	scanend = SCAN_PRIVATE(ss)->ss_scanend;
90768e8e04eSSam Leffler 	if (!scandone &&
90868e8e04eSSam Leffler 	    (ss->ss_flags & IEEE80211_SCAN_GOTPICK) == 0 &&
90968e8e04eSSam Leffler 	    ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_START) ||
91068e8e04eSSam Leffler 	     time_before(ticks + ss->ss_mindwell, scanend))) {
91168e8e04eSSam Leffler 		chan = ss->ss_chans[ss->ss_next++];
91268e8e04eSSam Leffler 
91368e8e04eSSam Leffler 		/*
91468e8e04eSSam Leffler 		 * Watch for truncation due to the scan end time.
91568e8e04eSSam Leffler 		 */
91668e8e04eSSam Leffler 		if (time_after(ticks + ss->ss_maxdwell, scanend))
91768e8e04eSSam Leffler 			maxdwell = scanend - ticks;
91868e8e04eSSam Leffler 		else
91968e8e04eSSam Leffler 			maxdwell = ss->ss_maxdwell;
92068e8e04eSSam Leffler 
921b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
92268e8e04eSSam Leffler 		    "%s: chan %3d%c -> %3d%c [%s, dwell min %lu max %lu]\n",
92368e8e04eSSam Leffler 		    __func__,
92468e8e04eSSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan),
92568e8e04eSSam Leffler 		        channel_type(ic->ic_curchan),
92668e8e04eSSam Leffler 		    ieee80211_chan2ieee(ic, chan), channel_type(chan),
92768e8e04eSSam Leffler 		    (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
92868e8e04eSSam Leffler 			(chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
92968e8e04eSSam Leffler 			"active" : "passive",
93068e8e04eSSam Leffler 		    ss->ss_mindwell, maxdwell);
93168e8e04eSSam Leffler 
93268e8e04eSSam Leffler 		/*
93368e8e04eSSam Leffler 		 * Potentially change channel and phy mode.
93468e8e04eSSam Leffler 		 */
93568e8e04eSSam Leffler 		change_channel(ic, chan);
93668e8e04eSSam Leffler 
93768e8e04eSSam Leffler 		/*
93868e8e04eSSam Leffler 		 * Scan curchan.  Drivers for "intelligent hardware"
93968e8e04eSSam Leffler 		 * override ic_scan_curchan to tell the device to do
94068e8e04eSSam Leffler 		 * the work.  Otherwise we manage the work outselves;
94168e8e04eSSam Leffler 		 * sending a probe request (as needed), and arming the
94268e8e04eSSam Leffler 		 * timeout to switch channels after maxdwell ticks.
94368e8e04eSSam Leffler 		 */
944b032f27cSSam Leffler 		ic->ic_scan_curchan(ss, maxdwell);
94568e8e04eSSam Leffler 
94668e8e04eSSam Leffler 		SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
94768e8e04eSSam Leffler 		/* clear mindwell lock and initial channel change flush */
94868e8e04eSSam Leffler 		SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
94968e8e04eSSam Leffler 	} else {
95068e8e04eSSam Leffler 		ic->ic_scan_end(ic);		/* notify driver */
95168e8e04eSSam Leffler 		/*
95268e8e04eSSam Leffler 		 * Record scan complete time.  Note that we also do
95368e8e04eSSam Leffler 		 * this when canceled so any background scan will
95468e8e04eSSam Leffler 		 * not be restarted for a while.
95568e8e04eSSam Leffler 		 */
95668e8e04eSSam Leffler 		if (scandone)
95768e8e04eSSam Leffler 			ic->ic_lastscan = ticks;
95868e8e04eSSam Leffler 		/* return to the bss channel */
95968e8e04eSSam Leffler 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
96068e8e04eSSam Leffler 		    ic->ic_curchan != ic->ic_bsschan)
961b032f27cSSam Leffler 			ieee80211_setcurchan(ic, ic->ic_bsschan);
96268e8e04eSSam Leffler 		/* clear internal flags and any indication of a pick */
96368e8e04eSSam Leffler 		SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
96468e8e04eSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
96568e8e04eSSam Leffler 
96668e8e04eSSam Leffler 		/*
96768e8e04eSSam Leffler 		 * If not canceled and scan completed, do post-processing.
96868e8e04eSSam Leffler 		 * If the callback function returns 0, then it wants to
96968e8e04eSSam Leffler 		 * continue/restart scanning.  Unfortunately we needed to
97068e8e04eSSam Leffler 		 * notify the driver to end the scan above to avoid having
97168e8e04eSSam Leffler 		 * rx frames alter the scan candidate list.
97268e8e04eSSam Leffler 		 */
97368e8e04eSSam Leffler 		if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
974b032f27cSSam Leffler 		    !ss->ss_ops->scan_end(ss, vap) &&
97568e8e04eSSam Leffler 		    (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
97668e8e04eSSam Leffler 		    time_before(ticks + ss->ss_mindwell, scanend)) {
977b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
97868e8e04eSSam Leffler 			    "%s: done, restart "
97968e8e04eSSam Leffler 			    "[ticks %u, dwell min %lu scanend %lu]\n",
98068e8e04eSSam Leffler 			    __func__,
98168e8e04eSSam Leffler 			    ticks, ss->ss_mindwell, scanend);
98268e8e04eSSam Leffler 			ss->ss_next = 0;	/* reset to begining */
98368e8e04eSSam Leffler 			if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
984b032f27cSSam Leffler 				vap->iv_stats.is_scan_active++;
98568e8e04eSSam Leffler 			else
986b032f27cSSam Leffler 				vap->iv_stats.is_scan_passive++;
98768e8e04eSSam Leffler 
988b032f27cSSam Leffler 			ss->ss_ops->scan_restart(ss, vap);	/* XXX? */
98968e8e04eSSam Leffler 			ic->ic_scan_start(ic);	/* notify driver */
99068e8e04eSSam Leffler 			goto again;
99168e8e04eSSam Leffler 		} else {
99268e8e04eSSam Leffler 			/* past here, scandone is ``true'' if not in bg mode */
99368e8e04eSSam Leffler 			if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
99468e8e04eSSam Leffler 				scandone = 1;
99568e8e04eSSam Leffler 
996b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
99768e8e04eSSam Leffler 			    "%s: %s, "
99868e8e04eSSam Leffler 			    "[ticks %u, dwell min %lu scanend %lu]\n",
99968e8e04eSSam Leffler 			    __func__, scandone ? "done" : "stopped",
100068e8e04eSSam Leffler 			    ticks, ss->ss_mindwell, scanend);
100168e8e04eSSam Leffler 
100268e8e04eSSam Leffler 			/*
100368e8e04eSSam Leffler 			 * Clear the SCAN bit first in case frames are
100468e8e04eSSam Leffler 			 * pending on the station power save queue.  If
100568e8e04eSSam Leffler 			 * we defer this then the dispatch of the frames
100668e8e04eSSam Leffler 			 * may generate a request to cancel scanning.
100768e8e04eSSam Leffler 			 */
100868e8e04eSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SCAN;
100968e8e04eSSam Leffler 			/*
101068e8e04eSSam Leffler 			 * Drop out of power save mode when a scan has
101168e8e04eSSam Leffler 			 * completed.  If this scan was prematurely terminated
101268e8e04eSSam Leffler 			 * because it is a background scan then don't notify
101368e8e04eSSam Leffler 			 * the ap; we'll either return to scanning after we
101468e8e04eSSam Leffler 			 * receive the beacon frame or we'll drop out of power
101568e8e04eSSam Leffler 			 * save mode because the beacon indicates we have frames
101668e8e04eSSam Leffler 			 * waiting for us.
101768e8e04eSSam Leffler 			 */
101868e8e04eSSam Leffler 			if (scandone) {
1019b032f27cSSam Leffler 				ieee80211_sta_pwrsave(vap, 0);
102068e8e04eSSam Leffler 				if (ss->ss_next >= ss->ss_last) {
1021b032f27cSSam Leffler 					ieee80211_notify_scan_done(vap);
102268e8e04eSSam Leffler 					ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
102368e8e04eSSam Leffler 				}
102468e8e04eSSam Leffler 			}
102568e8e04eSSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_CANCEL;
102668e8e04eSSam Leffler 			ss->ss_flags &=
102768e8e04eSSam Leffler 			    ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
102868e8e04eSSam Leffler 		}
102968e8e04eSSam Leffler 	}
103068e8e04eSSam Leffler #undef ISCAN_REP
103168e8e04eSSam Leffler }
103268e8e04eSSam Leffler 
103368e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
103468e8e04eSSam Leffler static void
1035b032f27cSSam Leffler dump_country(const uint8_t *ie)
1036b032f27cSSam Leffler {
1037b032f27cSSam Leffler 	const struct ieee80211_country_ie *cie =
1038b032f27cSSam Leffler 	   (const struct ieee80211_country_ie *) ie;
1039b032f27cSSam Leffler 	int i, nbands, schan, nchan;
1040b032f27cSSam Leffler 
1041b032f27cSSam Leffler 	if (cie->len < 3) {
1042b032f27cSSam Leffler 		printf(" <bogus country ie, len %d>", cie->len);
1043b032f27cSSam Leffler 		return;
1044b032f27cSSam Leffler 	}
1045b032f27cSSam Leffler 	printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1046b032f27cSSam Leffler 	nbands = (cie->len - 3) / sizeof(cie->band[0]);
1047b032f27cSSam Leffler 	for (i = 0; i < nbands; i++) {
1048b032f27cSSam Leffler 		schan = cie->band[i].schan;
1049b032f27cSSam Leffler 		nchan = cie->band[i].nchan;
1050b032f27cSSam Leffler 		if (nchan != 1)
1051b032f27cSSam Leffler 			printf(" %u-%u,%u", schan, schan + nchan-1,
1052b032f27cSSam Leffler 			    cie->band[i].maxtxpwr);
1053b032f27cSSam Leffler 		else
1054b032f27cSSam Leffler 			printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1055b032f27cSSam Leffler 	}
1056b032f27cSSam Leffler 	printf("]");
1057b032f27cSSam Leffler }
1058b032f27cSSam Leffler 
1059b032f27cSSam Leffler static void
106068e8e04eSSam Leffler dump_probe_beacon(uint8_t subtype, int isnew,
106168e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
1062b032f27cSSam Leffler 	const struct ieee80211_scanparams *sp, int rssi)
106368e8e04eSSam Leffler {
106468e8e04eSSam Leffler 
106568e8e04eSSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
106668e8e04eSSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
106768e8e04eSSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1068b032f27cSSam Leffler 	    sp->chan, sp->bchan);
106968e8e04eSSam Leffler 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1070b032f27cSSam Leffler 	printf(" rssi %d\n", rssi);
107168e8e04eSSam Leffler 
107268e8e04eSSam Leffler 	if (isnew) {
107368e8e04eSSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
107468e8e04eSSam Leffler 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1075b032f27cSSam Leffler 		if (sp->country != NULL)
1076b032f27cSSam Leffler 			dump_country(sp->country);
107768e8e04eSSam Leffler 		printf("\n");
107868e8e04eSSam Leffler 	}
107968e8e04eSSam Leffler }
108068e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
108168e8e04eSSam Leffler 
108268e8e04eSSam Leffler /*
108368e8e04eSSam Leffler  * Process a beacon or probe response frame.
108468e8e04eSSam Leffler  */
108568e8e04eSSam Leffler void
1086b032f27cSSam Leffler ieee80211_add_scan(struct ieee80211vap *vap,
108768e8e04eSSam Leffler 	const struct ieee80211_scanparams *sp,
108868e8e04eSSam Leffler 	const struct ieee80211_frame *wh,
108968e8e04eSSam Leffler 	int subtype, int rssi, int noise, int rstamp)
109068e8e04eSSam Leffler {
1091b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
109268e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
109368e8e04eSSam Leffler 
1094b032f27cSSam Leffler 	/* XXX locking */
109568e8e04eSSam Leffler 	/*
109668e8e04eSSam Leffler 	 * Frames received during startup are discarded to avoid
109768e8e04eSSam Leffler 	 * using scan state setup on the initial entry to the timer
109868e8e04eSSam Leffler 	 * callback.  This can occur because the device may enable
109968e8e04eSSam Leffler 	 * rx prior to our doing the initial channel change in the
1100b032f27cSSam Leffler 	 * timer routine.
110168e8e04eSSam Leffler 	 */
110268e8e04eSSam Leffler 	if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
110368e8e04eSSam Leffler 		return;
110468e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
1105b032f27cSSam Leffler 	if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1106b032f27cSSam Leffler 		dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
110768e8e04eSSam Leffler #endif
110868e8e04eSSam Leffler 	if (ss->ss_ops != NULL &&
110968e8e04eSSam Leffler 	    ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise, rstamp)) {
111068e8e04eSSam Leffler 		/*
111168e8e04eSSam Leffler 		 * If we've reached the min dwell time terminate
111268e8e04eSSam Leffler 		 * the timer so we'll switch to the next channel.
111368e8e04eSSam Leffler 		 */
111468e8e04eSSam Leffler 		if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
111568e8e04eSSam Leffler 		    time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1116b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
111768e8e04eSSam Leffler 			    "%s: chan %3d%c min dwell met (%u > %lu)\n",
111868e8e04eSSam Leffler 			    __func__,
111968e8e04eSSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_curchan),
112068e8e04eSSam Leffler 				channel_type(ic->ic_curchan),
112168e8e04eSSam Leffler 			    ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
112268e8e04eSSam Leffler 			SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
112368e8e04eSSam Leffler 			/*
112468e8e04eSSam Leffler 			 * NB: trigger at next clock tick or wait for the
1125b032f27cSSam Leffler 			 * hardware.
112668e8e04eSSam Leffler 			 */
1127b032f27cSSam Leffler 			ic->ic_scan_mindwell(ss);
112868e8e04eSSam Leffler 		}
112968e8e04eSSam Leffler 	}
113068e8e04eSSam Leffler }
113168e8e04eSSam Leffler 
113268e8e04eSSam Leffler /*
113368e8e04eSSam Leffler  * Timeout/age scan cache entries; called from sta timeout
113468e8e04eSSam Leffler  * timer (XXX should be self-contained).
113568e8e04eSSam Leffler  */
113668e8e04eSSam Leffler void
113768e8e04eSSam Leffler ieee80211_scan_timeout(struct ieee80211com *ic)
113868e8e04eSSam Leffler {
113968e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
114068e8e04eSSam Leffler 
114168e8e04eSSam Leffler 	if (ss->ss_ops != NULL)
114268e8e04eSSam Leffler 		ss->ss_ops->scan_age(ss);
114368e8e04eSSam Leffler }
114468e8e04eSSam Leffler 
114568e8e04eSSam Leffler /*
114668e8e04eSSam Leffler  * Mark a scan cache entry after a successful associate.
114768e8e04eSSam Leffler  */
114868e8e04eSSam Leffler void
1149b032f27cSSam Leffler ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
115068e8e04eSSam Leffler {
1151b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
115268e8e04eSSam Leffler 
115368e8e04eSSam Leffler 	if (ss->ss_ops != NULL) {
1154b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
115568e8e04eSSam Leffler 			mac, "%s",  __func__);
115668e8e04eSSam Leffler 		ss->ss_ops->scan_assoc_success(ss, mac);
115768e8e04eSSam Leffler 	}
115868e8e04eSSam Leffler }
115968e8e04eSSam Leffler 
116068e8e04eSSam Leffler /*
116168e8e04eSSam Leffler  * Demerit a scan cache entry after failing to associate.
116268e8e04eSSam Leffler  */
116368e8e04eSSam Leffler void
1164b032f27cSSam Leffler ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
116568e8e04eSSam Leffler 	const uint8_t mac[], int reason)
116668e8e04eSSam Leffler {
1167b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
116868e8e04eSSam Leffler 
116968e8e04eSSam Leffler 	if (ss->ss_ops != NULL) {
1170b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
117168e8e04eSSam Leffler 			"%s: reason %u", __func__, reason);
117268e8e04eSSam Leffler 		ss->ss_ops->scan_assoc_fail(ss, mac, reason);
117368e8e04eSSam Leffler 	}
117468e8e04eSSam Leffler }
117568e8e04eSSam Leffler 
117668e8e04eSSam Leffler /*
117768e8e04eSSam Leffler  * Iterate over the contents of the scan cache.
117868e8e04eSSam Leffler  */
117968e8e04eSSam Leffler void
1180b032f27cSSam Leffler ieee80211_scan_iterate(struct ieee80211vap *vap,
118168e8e04eSSam Leffler 	ieee80211_scan_iter_func *f, void *arg)
118268e8e04eSSam Leffler {
1183b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
118468e8e04eSSam Leffler 
118568e8e04eSSam Leffler 	if (ss->ss_ops != NULL)
118668e8e04eSSam Leffler 		ss->ss_ops->scan_iterate(ss, f, arg);
118768e8e04eSSam Leffler }
118868e8e04eSSam Leffler 
118968e8e04eSSam Leffler /*
119068e8e04eSSam Leffler  * Flush the contents of the scan cache.
119168e8e04eSSam Leffler  */
119268e8e04eSSam Leffler void
1193b032f27cSSam Leffler ieee80211_scan_flush(struct ieee80211vap *vap)
1194b032f27cSSam Leffler {
1195b032f27cSSam Leffler 	struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1196b032f27cSSam Leffler 
1197b032f27cSSam Leffler 	if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1198b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n",  __func__);
1199b032f27cSSam Leffler 		ss->ss_ops->scan_flush(ss);
1200b032f27cSSam Leffler 	}
1201b032f27cSSam Leffler }
1202b032f27cSSam Leffler 
1203b032f27cSSam Leffler /*
1204b032f27cSSam Leffler  * Check the scan cache for an ap/channel to use; if that
1205b032f27cSSam Leffler  * fails then kick off a new scan.
1206b032f27cSSam Leffler  */
1207b032f27cSSam Leffler struct ieee80211_channel *
1208b032f27cSSam Leffler ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
120968e8e04eSSam Leffler {
121068e8e04eSSam Leffler 	struct ieee80211_scan_state *ss = ic->ic_scan;
121168e8e04eSSam Leffler 
1212b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1213b032f27cSSam Leffler 
1214b032f27cSSam Leffler 	if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1215b032f27cSSam Leffler 		/* XXX printf? */
1216b032f27cSSam Leffler 		return NULL;
121768e8e04eSSam Leffler 	}
1218b032f27cSSam Leffler 	if (ss->ss_ops->scan_pickchan == NULL) {
1219b032f27cSSam Leffler 		IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1220b032f27cSSam Leffler 		    "%s: scan module does not support picking a channel, "
1221b032f27cSSam Leffler 		    "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1222b032f27cSSam Leffler 		return NULL;
1223b032f27cSSam Leffler 	}
1224b032f27cSSam Leffler 	return ss->ss_ops->scan_pickchan(ss, flags);
122568e8e04eSSam Leffler }
1226