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