blob: d9aaa634f018695ce9c57d82f95b57d56690616c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - background scan and roaming module: simple
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000018#include "config.h"
19#include "wnm_sta.h"
20#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "bgscan.h"
22
23struct bgscan_simple_data {
24 struct wpa_supplicant *wpa_s;
25 const struct wpa_ssid *ssid;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000026 unsigned int use_btm_query;
27 unsigned int scan_action_count;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028 int scan_interval;
29 int signal_threshold;
30 int short_scan_count; /* counter for scans using short scan interval */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080031 int max_short_scans; /* maximum times we short-scan before back-off */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032 int short_interval; /* use if signal < threshold */
33 int long_interval; /* use if signal > threshold */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080034 struct os_reltime last_bgscan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035};
36
37
Sunil Ravib0ac25f2024-07-12 01:42:03 +000038static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx);
39
40
41static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077static 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
Sunil Ravib0ac25f2024-07-12 01:42:03 +000083 if (bgscan_simple_btm_query(wpa_s, data))
84 goto scan_ok;
85
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 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
Sunil Ravic0f5d412024-09-11 22:12:49 +000092 /* 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 /*
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");
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000103 if (wpa_supplicant_trigger_scan(wpa_s, &params, true, false)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 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 {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000108 scan_ok:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 if (data->scan_interval == data->short_interval) {
110 data->short_scan_count++;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800111 if (data->short_scan_count >= data->max_short_scans) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 data->scan_interval = data->long_interval;
113 wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
114 "off to long scan interval");
115 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800116 } 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--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800125 os_get_reltime(&data->last_bgscan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 }
127}
128
129
130static int bgscan_simple_get_params(struct bgscan_simple_data *data,
131 const char *params)
132{
133 const char *pos;
134
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000135 data->use_btm_query = 0;
136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 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);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000152 pos = os_strchr(pos, ':');
153 if (pos) {
154 pos++;
155 data->use_btm_query = atoi(pos);
156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157
158 return 0;
159}
160
161
162static 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;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800194 data->max_short_scans = data->long_interval / data->short_interval + 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 if (data->signal_threshold) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000196 wpa_s->signal_threshold = data->signal_threshold;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197 /* 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 &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000200 siginfo.data.signal >= data->signal_threshold)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 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 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214 os_get_reltime(&data->last_bgscan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215
216 return data;
217}
218
219
220static void bgscan_simple_deinit(void *priv)
221{
222 struct bgscan_simple_data *data = priv;
223 eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000224 if (data->signal_threshold) {
225 data->wpa_s->signal_threshold = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 wpa_drv_signal_monitor(data->wpa_s, 0, 0);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 os_free(data);
229}
230
231
232static 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
254static 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
261static 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;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800268 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269
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;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800282 os_get_reltime(&now);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800283 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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 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 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800316 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 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
330const 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};