xref: /freebsd/contrib/wpa/wpa_supplicant/bgscan_simple.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * WPA Supplicant - background scan and roaming module: simple
3e28a4053SRui Paulo  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  */
8e28a4053SRui Paulo 
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "common.h"
12e28a4053SRui Paulo #include "eloop.h"
13e28a4053SRui Paulo #include "drivers/driver.h"
14e28a4053SRui Paulo #include "config_ssid.h"
15e28a4053SRui Paulo #include "wpa_supplicant_i.h"
16e28a4053SRui Paulo #include "driver_i.h"
17e28a4053SRui Paulo #include "scan.h"
18*a90b9d01SCy Schubert #include "config.h"
19*a90b9d01SCy Schubert #include "wnm_sta.h"
20*a90b9d01SCy Schubert #include "bss.h"
21e28a4053SRui Paulo #include "bgscan.h"
22e28a4053SRui Paulo 
23e28a4053SRui Paulo struct bgscan_simple_data {
24e28a4053SRui Paulo 	struct wpa_supplicant *wpa_s;
25e28a4053SRui Paulo 	const struct wpa_ssid *ssid;
26*a90b9d01SCy Schubert 	unsigned int use_btm_query;
27*a90b9d01SCy Schubert 	unsigned int scan_action_count;
28e28a4053SRui Paulo 	int scan_interval;
29e28a4053SRui Paulo 	int signal_threshold;
30f05cddf9SRui Paulo 	int short_scan_count; /* counter for scans using short scan interval */
31f05cddf9SRui Paulo 	int max_short_scans; /* maximum times we short-scan before back-off */
32e28a4053SRui Paulo 	int short_interval; /* use if signal < threshold */
33e28a4053SRui Paulo 	int long_interval; /* use if signal > threshold */
345b9c547cSRui Paulo 	struct os_reltime last_bgscan;
35e28a4053SRui Paulo };
36e28a4053SRui Paulo 
37e28a4053SRui Paulo 
38*a90b9d01SCy Schubert static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx);
39*a90b9d01SCy Schubert 
40*a90b9d01SCy Schubert 
bgscan_simple_btm_query(struct wpa_supplicant * wpa_s,struct bgscan_simple_data * data)41*a90b9d01SCy Schubert static bool bgscan_simple_btm_query(struct wpa_supplicant *wpa_s,
42*a90b9d01SCy Schubert 				    struct bgscan_simple_data *data)
43*a90b9d01SCy Schubert {
44*a90b9d01SCy Schubert 	unsigned int mod;
45*a90b9d01SCy Schubert 
46*a90b9d01SCy Schubert 	if (!data->use_btm_query || wpa_s->conf->disable_btm ||
47*a90b9d01SCy Schubert 	    !wpa_s->current_bss ||
48*a90b9d01SCy Schubert 	    !wpa_bss_ext_capab(wpa_s->current_bss,
49*a90b9d01SCy Schubert 			       WLAN_EXT_CAPAB_BSS_TRANSITION))
50*a90b9d01SCy Schubert 		return false;
51*a90b9d01SCy Schubert 
52*a90b9d01SCy Schubert 	/* Try BTM x times, scan on x + 1 */
53*a90b9d01SCy Schubert 	data->scan_action_count++;
54*a90b9d01SCy Schubert 	mod = data->scan_action_count % (data->use_btm_query + 1);
55*a90b9d01SCy Schubert 	if (mod >= data->use_btm_query)
56*a90b9d01SCy Schubert 		return false;
57*a90b9d01SCy Schubert 
58*a90b9d01SCy Schubert 	wpa_printf(MSG_DEBUG,
59*a90b9d01SCy Schubert 		   "bgscan simple: Send BSS transition management query %d/%d",
60*a90b9d01SCy Schubert 		   mod, data->use_btm_query);
61*a90b9d01SCy Schubert 	if (wnm_send_bss_transition_mgmt_query(
62*a90b9d01SCy Schubert 		    wpa_s, WNM_TRANSITION_REASON_BETTER_AP_FOUND, NULL, 0)) {
63*a90b9d01SCy Schubert 		wpa_printf(MSG_DEBUG,
64*a90b9d01SCy Schubert 			   "bgscan simple: Failed to send BSS transition management query");
65*a90b9d01SCy Schubert 		/* Fall through and do regular scan */
66*a90b9d01SCy Schubert 		return false;
67*a90b9d01SCy Schubert 	}
68*a90b9d01SCy Schubert 
69*a90b9d01SCy Schubert 	/* Start a new timeout for the next one. We don't have scan callback to
70*a90b9d01SCy Schubert 	 * otherwise trigger future progress when using BTM path. */
71*a90b9d01SCy Schubert 	eloop_register_timeout(data->scan_interval, 0,
72*a90b9d01SCy Schubert 			       bgscan_simple_timeout, data, NULL);
73*a90b9d01SCy Schubert 	return true;
74*a90b9d01SCy Schubert }
75*a90b9d01SCy Schubert 
76*a90b9d01SCy Schubert 
bgscan_simple_timeout(void * eloop_ctx,void * timeout_ctx)77e28a4053SRui Paulo static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx)
78e28a4053SRui Paulo {
79e28a4053SRui Paulo 	struct bgscan_simple_data *data = eloop_ctx;
80e28a4053SRui Paulo 	struct wpa_supplicant *wpa_s = data->wpa_s;
81e28a4053SRui Paulo 	struct wpa_driver_scan_params params;
82e28a4053SRui Paulo 
83*a90b9d01SCy Schubert 	if (bgscan_simple_btm_query(wpa_s, data))
84*a90b9d01SCy Schubert 		goto scan_ok;
85*a90b9d01SCy Schubert 
86e28a4053SRui Paulo 	os_memset(&params, 0, sizeof(params));
87e28a4053SRui Paulo 	params.num_ssids = 1;
88e28a4053SRui Paulo 	params.ssids[0].ssid = data->ssid->ssid;
89e28a4053SRui Paulo 	params.ssids[0].ssid_len = data->ssid->ssid_len;
90e28a4053SRui Paulo 	params.freqs = data->ssid->scan_freq;
91e28a4053SRui Paulo 
92e28a4053SRui Paulo 	/*
93e28a4053SRui Paulo 	 * A more advanced bgscan module would learn about most like channels
94e28a4053SRui Paulo 	 * over time and request scans only for some channels (probing others
95e28a4053SRui Paulo 	 * every now and then) to reduce effect on the data connection.
96e28a4053SRui Paulo 	 */
97e28a4053SRui Paulo 
98e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: Request a background scan");
99*a90b9d01SCy Schubert 	if (wpa_supplicant_trigger_scan(wpa_s, &params, true, false)) {
100e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "bgscan simple: Failed to trigger scan");
101e28a4053SRui Paulo 		eloop_register_timeout(data->scan_interval, 0,
102e28a4053SRui Paulo 				       bgscan_simple_timeout, data, NULL);
103f05cddf9SRui Paulo 	} else {
104*a90b9d01SCy Schubert 	scan_ok:
105f05cddf9SRui Paulo 		if (data->scan_interval == data->short_interval) {
106f05cddf9SRui Paulo 			data->short_scan_count++;
10785732ac8SCy Schubert 			if (data->short_scan_count >= data->max_short_scans) {
108f05cddf9SRui Paulo 				data->scan_interval = data->long_interval;
109f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
110f05cddf9SRui Paulo 					   "off to long scan interval");
111f05cddf9SRui Paulo 			}
112f05cddf9SRui Paulo 		} else if (data->short_scan_count > 0) {
113f05cddf9SRui Paulo 			/*
114f05cddf9SRui Paulo 			 * If we lasted a long scan interval without any
115f05cddf9SRui Paulo 			 * CQM triggers, decrease the short-scan count,
116f05cddf9SRui Paulo 			 * which allows 1 more short-scan interval to
117f05cddf9SRui Paulo 			 * occur in the future when CQM triggers.
118f05cddf9SRui Paulo 			 */
119f05cddf9SRui Paulo 			data->short_scan_count--;
120f05cddf9SRui Paulo 		}
1215b9c547cSRui Paulo 		os_get_reltime(&data->last_bgscan);
122e28a4053SRui Paulo 	}
123f05cddf9SRui Paulo }
124e28a4053SRui Paulo 
125e28a4053SRui Paulo 
bgscan_simple_get_params(struct bgscan_simple_data * data,const char * params)126e28a4053SRui Paulo static int bgscan_simple_get_params(struct bgscan_simple_data *data,
127e28a4053SRui Paulo 				    const char *params)
128e28a4053SRui Paulo {
129e28a4053SRui Paulo 	const char *pos;
130e28a4053SRui Paulo 
131*a90b9d01SCy Schubert 	data->use_btm_query = 0;
132*a90b9d01SCy Schubert 
133e28a4053SRui Paulo 	data->short_interval = atoi(params);
134e28a4053SRui Paulo 
135e28a4053SRui Paulo 	pos = os_strchr(params, ':');
136e28a4053SRui Paulo 	if (pos == NULL)
137e28a4053SRui Paulo 		return 0;
138e28a4053SRui Paulo 	pos++;
139e28a4053SRui Paulo 	data->signal_threshold = atoi(pos);
140e28a4053SRui Paulo 	pos = os_strchr(pos, ':');
141e28a4053SRui Paulo 	if (pos == NULL) {
142e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
143e28a4053SRui Paulo 			   "for high signal");
144e28a4053SRui Paulo 		return -1;
145e28a4053SRui Paulo 	}
146e28a4053SRui Paulo 	pos++;
147e28a4053SRui Paulo 	data->long_interval = atoi(pos);
148*a90b9d01SCy Schubert 	pos = os_strchr(pos, ':');
149*a90b9d01SCy Schubert 	if (pos) {
150*a90b9d01SCy Schubert 		pos++;
151*a90b9d01SCy Schubert 		data->use_btm_query = atoi(pos);
152*a90b9d01SCy Schubert 	}
153e28a4053SRui Paulo 
154e28a4053SRui Paulo 	return 0;
155e28a4053SRui Paulo }
156e28a4053SRui Paulo 
157e28a4053SRui Paulo 
bgscan_simple_init(struct wpa_supplicant * wpa_s,const char * params,const struct wpa_ssid * ssid)158e28a4053SRui Paulo static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
159e28a4053SRui Paulo 				 const char *params,
160e28a4053SRui Paulo 				 const struct wpa_ssid *ssid)
161e28a4053SRui Paulo {
162e28a4053SRui Paulo 	struct bgscan_simple_data *data;
163e28a4053SRui Paulo 
164e28a4053SRui Paulo 	data = os_zalloc(sizeof(*data));
165e28a4053SRui Paulo 	if (data == NULL)
166e28a4053SRui Paulo 		return NULL;
167e28a4053SRui Paulo 	data->wpa_s = wpa_s;
168e28a4053SRui Paulo 	data->ssid = ssid;
169e28a4053SRui Paulo 	if (bgscan_simple_get_params(data, params) < 0) {
170e28a4053SRui Paulo 		os_free(data);
171e28a4053SRui Paulo 		return NULL;
172e28a4053SRui Paulo 	}
173e28a4053SRui Paulo 	if (data->short_interval <= 0)
174e28a4053SRui Paulo 		data->short_interval = 30;
175e28a4053SRui Paulo 	if (data->long_interval <= 0)
176e28a4053SRui Paulo 		data->long_interval = 30;
177e28a4053SRui Paulo 
178e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d  "
179e28a4053SRui Paulo 		   "Short bgscan interval %d  Long bgscan interval %d",
180e28a4053SRui Paulo 		   data->signal_threshold, data->short_interval,
181e28a4053SRui Paulo 		   data->long_interval);
182e28a4053SRui Paulo 
183e28a4053SRui Paulo 	if (data->signal_threshold &&
184e28a4053SRui Paulo 	    wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
185e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
186e28a4053SRui Paulo 			   "signal strength monitoring");
187e28a4053SRui Paulo 	}
188e28a4053SRui Paulo 
189e28a4053SRui Paulo 	data->scan_interval = data->short_interval;
190f05cddf9SRui Paulo 	data->max_short_scans = data->long_interval / data->short_interval + 1;
191f05cddf9SRui Paulo 	if (data->signal_threshold) {
192*a90b9d01SCy Schubert 		wpa_s->signal_threshold = data->signal_threshold;
193f05cddf9SRui Paulo 		/* Poll for signal info to set initial scan interval */
194f05cddf9SRui Paulo 		struct wpa_signal_info siginfo;
195f05cddf9SRui Paulo 		if (wpa_drv_signal_poll(wpa_s, &siginfo) == 0 &&
196*a90b9d01SCy Schubert 		    siginfo.data.signal >= data->signal_threshold)
197f05cddf9SRui Paulo 			data->scan_interval = data->long_interval;
198f05cddf9SRui Paulo 	}
199f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: Init scan interval: %d",
200f05cddf9SRui Paulo 		   data->scan_interval);
201e28a4053SRui Paulo 	eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
202e28a4053SRui Paulo 			       data, NULL);
203e28a4053SRui Paulo 
204e28a4053SRui Paulo 	/*
205e28a4053SRui Paulo 	 * This function is called immediately after an association, so it is
206e28a4053SRui Paulo 	 * reasonable to assume that a scan was completed recently. This makes
207e28a4053SRui Paulo 	 * us skip an immediate new scan in cases where the current signal
208e28a4053SRui Paulo 	 * level is below the bgscan threshold.
209e28a4053SRui Paulo 	 */
2105b9c547cSRui Paulo 	os_get_reltime(&data->last_bgscan);
211e28a4053SRui Paulo 
212e28a4053SRui Paulo 	return data;
213e28a4053SRui Paulo }
214e28a4053SRui Paulo 
215e28a4053SRui Paulo 
bgscan_simple_deinit(void * priv)216e28a4053SRui Paulo static void bgscan_simple_deinit(void *priv)
217e28a4053SRui Paulo {
218e28a4053SRui Paulo 	struct bgscan_simple_data *data = priv;
219e28a4053SRui Paulo 	eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
220*a90b9d01SCy Schubert 	if (data->signal_threshold) {
221*a90b9d01SCy Schubert 		data->wpa_s->signal_threshold = 0;
222e28a4053SRui Paulo 		wpa_drv_signal_monitor(data->wpa_s, 0, 0);
223*a90b9d01SCy Schubert 	}
224e28a4053SRui Paulo 	os_free(data);
225e28a4053SRui Paulo }
226e28a4053SRui Paulo 
227e28a4053SRui Paulo 
bgscan_simple_notify_scan(void * priv,struct wpa_scan_results * scan_res)228f05cddf9SRui Paulo static int bgscan_simple_notify_scan(void *priv,
229f05cddf9SRui Paulo 				     struct wpa_scan_results *scan_res)
230e28a4053SRui Paulo {
231e28a4053SRui Paulo 	struct bgscan_simple_data *data = priv;
232e28a4053SRui Paulo 
233e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
234e28a4053SRui Paulo 
235e28a4053SRui Paulo 	eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
236e28a4053SRui Paulo 	eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
237e28a4053SRui Paulo 			       data, NULL);
238e28a4053SRui Paulo 
239e28a4053SRui Paulo 	/*
240e28a4053SRui Paulo 	 * A more advanced bgscan could process scan results internally, select
241e28a4053SRui Paulo 	 * the BSS and request roam if needed. This sample uses the existing
242e28a4053SRui Paulo 	 * BSS/ESS selection routine. Change this to return 1 if selection is
243e28a4053SRui Paulo 	 * done inside the bgscan module.
244e28a4053SRui Paulo 	 */
245e28a4053SRui Paulo 
246e28a4053SRui Paulo 	return 0;
247e28a4053SRui Paulo }
248e28a4053SRui Paulo 
249e28a4053SRui Paulo 
bgscan_simple_notify_beacon_loss(void * priv)250e28a4053SRui Paulo static void bgscan_simple_notify_beacon_loss(void *priv)
251e28a4053SRui Paulo {
252e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
253e28a4053SRui Paulo 	/* TODO: speed up background scanning */
254e28a4053SRui Paulo }
255e28a4053SRui Paulo 
256e28a4053SRui Paulo 
bgscan_simple_notify_signal_change(void * priv,int above,int current_signal,int current_noise,int current_txrate)257f05cddf9SRui Paulo static void bgscan_simple_notify_signal_change(void *priv, int above,
258f05cddf9SRui Paulo 					       int current_signal,
259f05cddf9SRui Paulo 					       int current_noise,
260f05cddf9SRui Paulo 					       int current_txrate)
261e28a4053SRui Paulo {
262e28a4053SRui Paulo 	struct bgscan_simple_data *data = priv;
263e28a4053SRui Paulo 	int scan = 0;
2645b9c547cSRui Paulo 	struct os_reltime now;
265e28a4053SRui Paulo 
266e28a4053SRui Paulo 	if (data->short_interval == data->long_interval ||
267e28a4053SRui Paulo 	    data->signal_threshold == 0)
268e28a4053SRui Paulo 		return;
269e28a4053SRui Paulo 
270e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
271f05cddf9SRui Paulo 		   "(above=%d current_signal=%d current_noise=%d "
272f05cddf9SRui Paulo 		   "current_txrate=%d))", above, current_signal,
273f05cddf9SRui Paulo 		   current_noise, current_txrate);
274e28a4053SRui Paulo 	if (data->scan_interval == data->long_interval && !above) {
275e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "bgscan simple: Start using short "
276e28a4053SRui Paulo 			   "bgscan interval");
277e28a4053SRui Paulo 		data->scan_interval = data->short_interval;
2785b9c547cSRui Paulo 		os_get_reltime(&now);
279f05cddf9SRui Paulo 		if (now.sec > data->last_bgscan.sec + 1 &&
280f05cddf9SRui Paulo 		    data->short_scan_count <= data->max_short_scans)
281f05cddf9SRui Paulo 			/*
282f05cddf9SRui Paulo 			 * If we haven't just previously (<1 second ago)
283f05cddf9SRui Paulo 			 * performed a scan, and we haven't depleted our
284f05cddf9SRui Paulo 			 * budget for short-scans, perform a scan
285f05cddf9SRui Paulo 			 * immediately.
286f05cddf9SRui Paulo 			 */
287e28a4053SRui Paulo 			scan = 1;
288f05cddf9SRui Paulo 		else if (data->last_bgscan.sec + data->long_interval >
289f05cddf9SRui Paulo 			 now.sec + data->scan_interval) {
290f05cddf9SRui Paulo 			/*
291f05cddf9SRui Paulo 			 * Restart scan interval timer if currently scheduled
292f05cddf9SRui Paulo 			 * scan is too far in the future.
293f05cddf9SRui Paulo 			 */
294f05cddf9SRui Paulo 			eloop_cancel_timeout(bgscan_simple_timeout, data,
295f05cddf9SRui Paulo 					     NULL);
296f05cddf9SRui Paulo 			eloop_register_timeout(data->scan_interval, 0,
297f05cddf9SRui Paulo 					       bgscan_simple_timeout, data,
298f05cddf9SRui Paulo 					       NULL);
299f05cddf9SRui Paulo 		}
300e28a4053SRui Paulo 	} else if (data->scan_interval == data->short_interval && above) {
301e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
302e28a4053SRui Paulo 			   "interval");
303e28a4053SRui Paulo 		data->scan_interval = data->long_interval;
304e28a4053SRui Paulo 		eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
305e28a4053SRui Paulo 		eloop_register_timeout(data->scan_interval, 0,
306e28a4053SRui Paulo 				       bgscan_simple_timeout, data, NULL);
307e28a4053SRui Paulo 	} else if (!above) {
308e28a4053SRui Paulo 		/*
309e28a4053SRui Paulo 		 * Signal dropped further 4 dB. Request a new scan if we have
310e28a4053SRui Paulo 		 * not yet scanned in a while.
311e28a4053SRui Paulo 		 */
3125b9c547cSRui Paulo 		os_get_reltime(&now);
313e28a4053SRui Paulo 		if (now.sec > data->last_bgscan.sec + 10)
314e28a4053SRui Paulo 			scan = 1;
315e28a4053SRui Paulo 	}
316e28a4053SRui Paulo 
317e28a4053SRui Paulo 	if (scan) {
318e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan");
319e28a4053SRui Paulo 		eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
320e28a4053SRui Paulo 		eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
321e28a4053SRui Paulo 				       NULL);
322e28a4053SRui Paulo 	}
323e28a4053SRui Paulo }
324e28a4053SRui Paulo 
325e28a4053SRui Paulo 
326e28a4053SRui Paulo const struct bgscan_ops bgscan_simple_ops = {
327e28a4053SRui Paulo 	.name = "simple",
328e28a4053SRui Paulo 	.init = bgscan_simple_init,
329e28a4053SRui Paulo 	.deinit = bgscan_simple_deinit,
330e28a4053SRui Paulo 	.notify_scan = bgscan_simple_notify_scan,
331e28a4053SRui Paulo 	.notify_beacon_loss = bgscan_simple_notify_beacon_loss,
332e28a4053SRui Paulo 	.notify_signal_change = bgscan_simple_notify_signal_change,
333e28a4053SRui Paulo };
334