2009-09-14 23:08:24 +02:00
|
|
|
/*
|
|
|
|
* WPA Supplicant - background scan and roaming module: simple
|
2010-03-29 00:32:34 +02:00
|
|
|
* Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
|
2009-09-14 23:08:24 +02:00
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2009-09-14 23:08:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "drivers/driver.h"
|
|
|
|
#include "config_ssid.h"
|
|
|
|
#include "wpa_supplicant_i.h"
|
2010-03-29 00:32:34 +02:00
|
|
|
#include "driver_i.h"
|
2010-01-03 17:48:11 +01:00
|
|
|
#include "scan.h"
|
2023-11-26 19:22:29 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "wnm_sta.h"
|
|
|
|
#include "bss.h"
|
2009-09-14 23:08:24 +02:00
|
|
|
#include "bgscan.h"
|
|
|
|
|
|
|
|
struct bgscan_simple_data {
|
|
|
|
struct wpa_supplicant *wpa_s;
|
|
|
|
const struct wpa_ssid *ssid;
|
2023-11-26 19:22:29 +01:00
|
|
|
unsigned int use_btm_query;
|
|
|
|
unsigned int scan_action_count;
|
2009-09-14 23:08:24 +02:00
|
|
|
int scan_interval;
|
2010-03-29 00:32:34 +02:00
|
|
|
int signal_threshold;
|
2011-03-31 17:04:48 +02:00
|
|
|
int short_scan_count; /* counter for scans using short scan interval */
|
2012-02-05 17:43:06 +01:00
|
|
|
int max_short_scans; /* maximum times we short-scan before back-off */
|
2010-03-29 00:32:34 +02:00
|
|
|
int short_interval; /* use if signal < threshold */
|
|
|
|
int long_interval; /* use if signal > threshold */
|
2013-12-16 21:08:32 +01:00
|
|
|
struct os_reltime last_bgscan;
|
2009-09-14 23:08:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-11-26 19:22:29 +01:00
|
|
|
static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx);
|
|
|
|
|
|
|
|
|
|
|
|
static bool bgscan_simple_btm_query(struct wpa_supplicant *wpa_s,
|
|
|
|
struct bgscan_simple_data *data)
|
|
|
|
{
|
|
|
|
unsigned int mod;
|
|
|
|
|
|
|
|
if (!data->use_btm_query || wpa_s->conf->disable_btm ||
|
|
|
|
!wpa_s->current_bss ||
|
|
|
|
!wpa_bss_ext_capab(wpa_s->current_bss,
|
|
|
|
WLAN_EXT_CAPAB_BSS_TRANSITION))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Try BTM x times, scan on x + 1 */
|
|
|
|
data->scan_action_count++;
|
|
|
|
mod = data->scan_action_count % (data->use_btm_query + 1);
|
|
|
|
if (mod >= data->use_btm_query)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"bgscan simple: Send BSS transition management query %d/%d",
|
|
|
|
mod, data->use_btm_query);
|
|
|
|
if (wnm_send_bss_transition_mgmt_query(
|
|
|
|
wpa_s, WNM_TRANSITION_REASON_BETTER_AP_FOUND, NULL, 0)) {
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"bgscan simple: Failed to send BSS transition management query");
|
|
|
|
/* Fall through and do regular scan */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a new timeout for the next one. We don't have scan callback to
|
|
|
|
* otherwise trigger future progress when using BTM path. */
|
|
|
|
eloop_register_timeout(data->scan_interval, 0,
|
|
|
|
bgscan_simple_timeout, data, NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-14 23:08:24 +02:00
|
|
|
static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx)
|
|
|
|
{
|
|
|
|
struct bgscan_simple_data *data = eloop_ctx;
|
|
|
|
struct wpa_supplicant *wpa_s = data->wpa_s;
|
|
|
|
struct wpa_driver_scan_params params;
|
|
|
|
|
2023-11-26 19:22:29 +01:00
|
|
|
if (bgscan_simple_btm_query(wpa_s, data))
|
|
|
|
goto scan_ok;
|
|
|
|
|
2009-09-14 23:08:24 +02:00
|
|
|
os_memset(¶ms, 0, sizeof(params));
|
|
|
|
params.num_ssids = 1;
|
|
|
|
params.ssids[0].ssid = data->ssid->ssid;
|
|
|
|
params.ssids[0].ssid_len = data->ssid->ssid_len;
|
|
|
|
params.freqs = data->ssid->scan_freq;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A more advanced bgscan module would learn about most like channels
|
|
|
|
* over time and request scans only for some channels (probing others
|
|
|
|
* every now and then) to reduce effect on the data connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Request a background scan");
|
2023-06-03 00:15:11 +02:00
|
|
|
if (wpa_supplicant_trigger_scan(wpa_s, ¶ms, true, false)) {
|
2009-09-14 23:08:24 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Failed to trigger scan");
|
|
|
|
eloop_register_timeout(data->scan_interval, 0,
|
|
|
|
bgscan_simple_timeout, data, NULL);
|
2011-03-31 17:04:48 +02:00
|
|
|
} else {
|
2023-11-26 19:22:29 +01:00
|
|
|
scan_ok:
|
2011-03-31 17:04:48 +02:00
|
|
|
if (data->scan_interval == data->short_interval) {
|
|
|
|
data->short_scan_count++;
|
2017-01-02 15:26:26 +01:00
|
|
|
if (data->short_scan_count >= data->max_short_scans) {
|
2011-03-31 17:04:48 +02:00
|
|
|
data->scan_interval = data->long_interval;
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
|
|
|
|
"off to long scan interval");
|
|
|
|
}
|
2012-02-05 17:43:06 +01:00
|
|
|
} else if (data->short_scan_count > 0) {
|
|
|
|
/*
|
|
|
|
* If we lasted a long scan interval without any
|
|
|
|
* CQM triggers, decrease the short-scan count,
|
|
|
|
* which allows 1 more short-scan interval to
|
|
|
|
* occur in the future when CQM triggers.
|
|
|
|
*/
|
|
|
|
data->short_scan_count--;
|
2011-03-31 17:04:48 +02:00
|
|
|
}
|
2013-12-16 21:08:32 +01:00
|
|
|
os_get_reltime(&data->last_bgscan);
|
2011-03-31 17:04:48 +02:00
|
|
|
}
|
2010-03-29 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int bgscan_simple_get_params(struct bgscan_simple_data *data,
|
|
|
|
const char *params)
|
|
|
|
{
|
|
|
|
const char *pos;
|
|
|
|
|
2023-11-26 19:22:29 +01:00
|
|
|
data->use_btm_query = 0;
|
|
|
|
|
2010-03-29 00:32:34 +02:00
|
|
|
data->short_interval = atoi(params);
|
|
|
|
|
|
|
|
pos = os_strchr(params, ':');
|
|
|
|
if (pos == NULL)
|
|
|
|
return 0;
|
|
|
|
pos++;
|
|
|
|
data->signal_threshold = atoi(pos);
|
|
|
|
pos = os_strchr(pos, ':');
|
|
|
|
if (pos == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
|
|
|
|
"for high signal");
|
|
|
|
return -1;
|
2009-09-14 23:08:24 +02:00
|
|
|
}
|
2010-03-29 00:32:34 +02:00
|
|
|
pos++;
|
|
|
|
data->long_interval = atoi(pos);
|
2023-11-26 19:22:29 +01:00
|
|
|
pos = os_strchr(pos, ':');
|
|
|
|
if (pos) {
|
|
|
|
pos++;
|
|
|
|
data->use_btm_query = atoi(pos);
|
|
|
|
}
|
2010-03-29 00:32:34 +02:00
|
|
|
|
|
|
|
return 0;
|
2009-09-14 23:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
|
|
|
|
const char *params,
|
|
|
|
const struct wpa_ssid *ssid)
|
|
|
|
{
|
|
|
|
struct bgscan_simple_data *data;
|
|
|
|
|
|
|
|
data = os_zalloc(sizeof(*data));
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
data->wpa_s = wpa_s;
|
|
|
|
data->ssid = ssid;
|
2010-03-29 00:32:34 +02:00
|
|
|
if (bgscan_simple_get_params(data, params) < 0) {
|
|
|
|
os_free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (data->short_interval <= 0)
|
|
|
|
data->short_interval = 30;
|
|
|
|
if (data->long_interval <= 0)
|
|
|
|
data->long_interval = 30;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d "
|
|
|
|
"Short bgscan interval %d Long bgscan interval %d",
|
|
|
|
data->signal_threshold, data->short_interval,
|
|
|
|
data->long_interval);
|
|
|
|
|
|
|
|
if (data->signal_threshold &&
|
|
|
|
wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
|
|
|
|
"signal strength monitoring");
|
|
|
|
}
|
|
|
|
|
|
|
|
data->scan_interval = data->short_interval;
|
2012-02-05 17:43:06 +01:00
|
|
|
data->max_short_scans = data->long_interval / data->short_interval + 1;
|
2011-03-31 17:02:37 +02:00
|
|
|
if (data->signal_threshold) {
|
|
|
|
/* Poll for signal info to set initial scan interval */
|
|
|
|
struct wpa_signal_info siginfo;
|
|
|
|
if (wpa_drv_signal_poll(wpa_s, &siginfo) == 0 &&
|
2022-12-01 00:33:09 +01:00
|
|
|
siginfo.data.signal >= data->signal_threshold)
|
2011-03-31 17:02:37 +02:00
|
|
|
data->scan_interval = data->long_interval;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Init scan interval: %d",
|
|
|
|
data->scan_interval);
|
2009-09-14 23:08:24 +02:00
|
|
|
eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
|
|
|
|
data, NULL);
|
2010-08-27 19:29:02 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called immediately after an association, so it is
|
|
|
|
* reasonable to assume that a scan was completed recently. This makes
|
|
|
|
* us skip an immediate new scan in cases where the current signal
|
|
|
|
* level is below the bgscan threshold.
|
|
|
|
*/
|
2013-12-16 21:08:32 +01:00
|
|
|
os_get_reltime(&data->last_bgscan);
|
2010-08-27 19:29:02 +02:00
|
|
|
|
2009-09-14 23:08:24 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void bgscan_simple_deinit(void *priv)
|
|
|
|
{
|
|
|
|
struct bgscan_simple_data *data = priv;
|
|
|
|
eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
|
2010-03-29 00:32:34 +02:00
|
|
|
if (data->signal_threshold)
|
|
|
|
wpa_drv_signal_monitor(data->wpa_s, 0, 0);
|
2009-09-14 23:08:24 +02:00
|
|
|
os_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-11 00:43:44 +02:00
|
|
|
static int bgscan_simple_notify_scan(void *priv,
|
|
|
|
struct wpa_scan_results *scan_res)
|
2009-09-14 23:08:24 +02:00
|
|
|
{
|
|
|
|
struct bgscan_simple_data *data = priv;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
|
|
|
|
|
|
|
|
eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
|
|
|
|
eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
|
|
|
|
data, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A more advanced bgscan could process scan results internally, select
|
|
|
|
* the BSS and request roam if needed. This sample uses the existing
|
|
|
|
* BSS/ESS selection routine. Change this to return 1 if selection is
|
|
|
|
* done inside the bgscan module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void bgscan_simple_notify_beacon_loss(void *priv)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
|
|
|
|
/* TODO: speed up background scanning */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-27 15:58:06 +02:00
|
|
|
static void bgscan_simple_notify_signal_change(void *priv, int above,
|
2010-10-12 19:03:36 +02:00
|
|
|
int current_signal,
|
|
|
|
int current_noise,
|
|
|
|
int current_txrate)
|
2009-09-14 23:08:24 +02:00
|
|
|
{
|
2010-03-29 00:32:34 +02:00
|
|
|
struct bgscan_simple_data *data = priv;
|
2010-08-27 19:29:02 +02:00
|
|
|
int scan = 0;
|
2013-12-16 21:08:32 +01:00
|
|
|
struct os_reltime now;
|
2010-03-29 00:32:34 +02:00
|
|
|
|
|
|
|
if (data->short_interval == data->long_interval ||
|
|
|
|
data->signal_threshold == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
|
2010-10-12 19:03:36 +02:00
|
|
|
"(above=%d current_signal=%d current_noise=%d "
|
|
|
|
"current_txrate=%d))", above, current_signal,
|
|
|
|
current_noise, current_txrate);
|
2010-03-29 00:32:34 +02:00
|
|
|
if (data->scan_interval == data->long_interval && !above) {
|
2010-08-27 19:29:02 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Start using short "
|
|
|
|
"bgscan interval");
|
2010-03-29 00:32:34 +02:00
|
|
|
data->scan_interval = data->short_interval;
|
2013-12-16 21:08:32 +01:00
|
|
|
os_get_reltime(&now);
|
2012-02-05 17:43:06 +01:00
|
|
|
if (now.sec > data->last_bgscan.sec + 1 &&
|
|
|
|
data->short_scan_count <= data->max_short_scans)
|
|
|
|
/*
|
|
|
|
* If we haven't just previously (<1 second ago)
|
|
|
|
* performed a scan, and we haven't depleted our
|
|
|
|
* budget for short-scans, perform a scan
|
|
|
|
* immediately.
|
|
|
|
*/
|
2010-08-27 19:29:02 +02:00
|
|
|
scan = 1;
|
2011-03-31 17:06:09 +02:00
|
|
|
else if (data->last_bgscan.sec + data->long_interval >
|
|
|
|
now.sec + data->scan_interval) {
|
|
|
|
/*
|
|
|
|
* Restart scan interval timer if currently scheduled
|
|
|
|
* scan is too far in the future.
|
|
|
|
*/
|
|
|
|
eloop_cancel_timeout(bgscan_simple_timeout, data,
|
|
|
|
NULL);
|
|
|
|
eloop_register_timeout(data->scan_interval, 0,
|
|
|
|
bgscan_simple_timeout, data,
|
|
|
|
NULL);
|
|
|
|
}
|
2010-03-29 00:32:34 +02:00
|
|
|
} else if (data->scan_interval == data->short_interval && above) {
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
|
|
|
|
"interval");
|
|
|
|
data->scan_interval = data->long_interval;
|
|
|
|
eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
|
|
|
|
eloop_register_timeout(data->scan_interval, 0,
|
|
|
|
bgscan_simple_timeout, data, NULL);
|
|
|
|
} else if (!above) {
|
|
|
|
/*
|
|
|
|
* Signal dropped further 4 dB. Request a new scan if we have
|
|
|
|
* not yet scanned in a while.
|
|
|
|
*/
|
2013-12-16 21:08:32 +01:00
|
|
|
os_get_reltime(&now);
|
2010-08-27 19:29:02 +02:00
|
|
|
if (now.sec > data->last_bgscan.sec + 10)
|
|
|
|
scan = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scan) {
|
|
|
|
wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan");
|
|
|
|
eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
|
|
|
|
eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
|
|
|
|
NULL);
|
2010-03-29 00:32:34 +02:00
|
|
|
}
|
2009-09-14 23:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const struct bgscan_ops bgscan_simple_ops = {
|
|
|
|
.name = "simple",
|
|
|
|
.init = bgscan_simple_init,
|
|
|
|
.deinit = bgscan_simple_deinit,
|
|
|
|
.notify_scan = bgscan_simple_notify_scan,
|
|
|
|
.notify_beacon_loss = bgscan_simple_notify_beacon_loss,
|
|
|
|
.notify_signal_change = bgscan_simple_notify_signal_change,
|
|
|
|
};
|