2015-01-12 16:52:37 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
2018-11-15 21:54:24 +01:00
|
|
|
* Copyright (C) 2015-2018 Intel Corporation. All rights reserved.
|
2015-01-12 16:52:37 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-01 22:37:11 +01:00
|
|
|
#define _GNU_SOURCE
|
2015-01-12 16:52:37 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <time.h>
|
2015-01-12 16:52:37 +01:00
|
|
|
#include <sys/socket.h>
|
2015-06-06 01:18:55 +02:00
|
|
|
#include <limits.h>
|
2015-01-12 16:52:37 +01:00
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "linux/nl80211.h"
|
|
|
|
#include "src/iwd.h"
|
|
|
|
#include "src/wiphy.h"
|
2018-05-24 19:12:12 +02:00
|
|
|
#include "src/netdev.h"
|
2015-01-28 13:14:53 +01:00
|
|
|
#include "src/ie.h"
|
2018-07-11 00:47:00 +02:00
|
|
|
#include "src/common.h"
|
|
|
|
#include "src/network.h"
|
2018-07-22 14:15:18 +02:00
|
|
|
#include "src/knownnetworks.h"
|
2018-07-11 00:47:01 +02:00
|
|
|
#include "src/util.h"
|
2015-01-12 16:52:37 +01:00
|
|
|
#include "src/scan.h"
|
|
|
|
|
2015-06-05 05:31:39 +02:00
|
|
|
#define SCAN_MAX_INTERVAL 320
|
|
|
|
#define SCAN_INIT_INTERVAL 10
|
|
|
|
|
2019-05-01 17:32:30 +02:00
|
|
|
static struct l_queue *scan_contexts;
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2019-05-01 17:32:30 +02:00
|
|
|
static struct l_genl_family *nl80211;
|
|
|
|
static uint32_t next_scan_request_id;
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_periodic {
|
|
|
|
struct l_timeout *timeout;
|
|
|
|
uint16_t interval;
|
2016-06-25 20:42:23 +02:00
|
|
|
scan_trigger_func_t trigger;
|
2015-09-20 21:58:23 +02:00
|
|
|
scan_notify_func_t callback;
|
|
|
|
void *userdata;
|
2015-09-22 21:05:50 +02:00
|
|
|
bool retry:1;
|
2019-05-08 03:15:23 +02:00
|
|
|
uint32_t id;
|
2018-07-11 00:47:01 +02:00
|
|
|
bool needs_active_scan:1;
|
2015-09-22 21:05:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scan_request {
|
2015-10-06 05:37:12 +02:00
|
|
|
uint32_t id;
|
2015-09-22 21:05:50 +02:00
|
|
|
scan_trigger_func_t trigger;
|
|
|
|
scan_notify_func_t callback;
|
|
|
|
void *userdata;
|
|
|
|
scan_destroy_func_t destroy;
|
|
|
|
bool passive:1; /* Active or Passive scan? */
|
2018-07-11 00:47:00 +02:00
|
|
|
struct l_queue *cmds;
|
2015-09-20 21:58:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scan_context {
|
|
|
|
uint32_t ifindex;
|
2019-05-08 03:15:23 +02:00
|
|
|
/*
|
|
|
|
* Tells us whether a scan, our own or external, is running.
|
|
|
|
* Set when scan gets triggered, cleared when scan done and
|
|
|
|
* before actual results are queried.
|
|
|
|
*/
|
2015-09-20 21:58:23 +02:00
|
|
|
enum scan_state state;
|
|
|
|
struct scan_periodic sp;
|
2015-09-22 21:05:50 +02:00
|
|
|
struct l_queue *requests;
|
2019-05-08 03:15:23 +02:00
|
|
|
/* Non-zero if SCAN_TRIGGER is still running */
|
scan: Cancel CMD_TRIGGER_SCAN when removing scan context
Save the ids of the netlink trigger scan commands that we send and
cancel them in scan_ifindex_remove to fix a race leading to a
segfault. The segfault would happen every time if scan_ifindex_remove
was called in the same main loop iteration in which we sent the
command, on shutdown:
^CTerminate
src/netdev.c:netdev_free() Freeing netdev wlan3[6]
src/device.c:device_disassociated() 6
src/device.c:device_enter_state() Old State: connected, new state:
disconnected
src/device.c:device_enter_state() Old State: disconnected, new state:
autoconnect
src/scan.c:scan_periodic_start() Starting periodic scan for ifindex: 6
src/device.c:device_free()
src/device.c:bss_free() Freeing BSS 02:00:00:00:00:00
src/device.c:bss_free() Freeing BSS 02:00:00:00:01:00
Removing scan context for ifindex: 6
src/scan.c:scan_context_free() sc: 0x5555557ca290
src/scan.c:scan_notify() Scan notification 33
src/netdev.c:netdev_operstate_down_cb() netdev: 6, success: 1
src/scan.c:scan_periodic_done()
src/scan.c:scan_periodic_done() Periodic scan triggered for ifindex:
1434209520
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000064 in ?? ()
(gdb) bt
#0 0x0000000000000064 in ?? ()
#1 0x0000555555583560 in process_unicast (nlmsg=0x7fffffffc1a0,
genl=0x5555557c1d60) at ell/genl.c:390
#2 received_data (io=<optimized out>, user_data=0x5555557c1d60)
at ell/genl.c:506
#3 0x0000555555580d45 in io_callback (fd=<optimized out>,
events=1, user_data=0x5555557c1e60) at ell/io.c:120
#4 0x000055555558005f in l_main_run () at ell/main.c:381
#5 0x00005555555599c1 in main (argc=<optimized out>, argv=<optimized out>)
at src/main.c:259
2017-02-01 11:58:42 +01:00
|
|
|
unsigned int start_cmd_id;
|
2019-05-08 03:15:23 +02:00
|
|
|
/* Non-zero if GET_SCAN is still running */
|
|
|
|
unsigned int get_scan_cmd_id;
|
|
|
|
/*
|
|
|
|
* Whether the top request in the queue has triggered the current
|
|
|
|
* scan. May be set and cleared multiple times during a single
|
|
|
|
* request. May be false when the current request is waiting due
|
|
|
|
* to an EBUSY or an external scan (sr->cmds non-empty), when
|
|
|
|
* start_cmd_id is non-zero and for a brief moment when GET_SCAN
|
|
|
|
* is running.
|
|
|
|
*/
|
2018-12-04 03:44:34 +01:00
|
|
|
bool triggered:1;
|
2019-05-11 01:18:55 +02:00
|
|
|
/* Whether any commands from current request's queue have started */
|
|
|
|
bool started:1;
|
2018-05-24 19:12:12 +02:00
|
|
|
struct wiphy *wiphy;
|
2015-09-20 21:58:23 +02:00
|
|
|
};
|
2015-06-01 16:28:05 +02:00
|
|
|
|
|
|
|
struct scan_results {
|
|
|
|
uint32_t wiphy;
|
|
|
|
uint32_t ifindex;
|
|
|
|
struct l_queue *bss_list;
|
2015-06-10 23:24:31 +02:00
|
|
|
struct scan_freq_set *freqs;
|
2019-04-11 21:14:24 +02:00
|
|
|
uint64_t time_stamp;
|
2019-05-08 03:15:23 +02:00
|
|
|
struct scan_request *sr;
|
2015-06-01 16:28:05 +02:00
|
|
|
};
|
|
|
|
|
2017-02-09 17:18:50 +01:00
|
|
|
static bool start_next_scan_request(struct scan_context *sc);
|
2017-03-14 16:11:33 +01:00
|
|
|
static void scan_periodic_rearm(struct scan_context *sc);
|
2015-09-22 21:05:50 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
static bool scan_context_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct scan_context *sc = a;
|
|
|
|
uint32_t ifindex = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return (sc->ifindex == ifindex);
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:37:12 +02:00
|
|
|
static bool scan_request_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct scan_request *sr = a;
|
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return sr->id == id;
|
|
|
|
}
|
|
|
|
|
2015-09-22 21:05:50 +02:00
|
|
|
static void scan_request_free(void *data)
|
|
|
|
{
|
|
|
|
struct scan_request *sr = data;
|
|
|
|
|
2018-11-29 19:56:00 +01:00
|
|
|
if (sr->destroy)
|
|
|
|
sr->destroy(sr->userdata);
|
|
|
|
|
2018-07-11 23:20:29 +02:00
|
|
|
l_queue_destroy(sr->cmds, (l_queue_destroy_func_t) l_genl_msg_unref);
|
2018-07-11 00:47:00 +02:00
|
|
|
|
|
|
|
l_free(sr);
|
2015-09-22 21:05:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
static void scan_request_failed(struct scan_context *sc,
|
|
|
|
struct scan_request *sr, int err)
|
2018-07-12 00:20:06 +02:00
|
|
|
{
|
2018-12-04 03:44:34 +01:00
|
|
|
l_queue_remove(sc->requests, sr);
|
|
|
|
|
2018-07-12 00:20:06 +02:00
|
|
|
if (sr->trigger)
|
|
|
|
sr->trigger(err, sr->userdata);
|
2018-12-04 03:44:34 +01:00
|
|
|
else if (sr->callback)
|
2019-03-14 21:42:11 +01:00
|
|
|
sr->callback(err, NULL, sr->userdata);
|
2018-07-12 00:20:06 +02:00
|
|
|
|
|
|
|
scan_request_free(sr);
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
static struct scan_context *scan_context_new(uint32_t ifindex)
|
|
|
|
{
|
2018-05-24 19:12:12 +02:00
|
|
|
struct netdev *netdev = netdev_find(ifindex);
|
|
|
|
struct wiphy *wiphy;
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc;
|
|
|
|
|
2018-05-24 19:12:12 +02:00
|
|
|
if (!netdev)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wiphy = netdev_get_wiphy(netdev);
|
|
|
|
if (!wiphy)
|
|
|
|
return NULL;
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc = l_new(struct scan_context, 1);
|
|
|
|
|
|
|
|
sc->ifindex = ifindex;
|
2018-05-24 19:12:12 +02:00
|
|
|
sc->wiphy = wiphy;
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->state = SCAN_STATE_NOT_RUNNING;
|
2015-09-22 21:05:50 +02:00
|
|
|
sc->requests = l_queue_new();
|
2015-09-20 21:58:23 +02:00
|
|
|
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scan_context_free(struct scan_context *sc)
|
|
|
|
{
|
|
|
|
l_debug("sc: %p", sc);
|
|
|
|
|
2015-09-22 21:05:50 +02:00
|
|
|
l_queue_destroy(sc->requests, scan_request_free);
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
if (sc->sp.timeout)
|
|
|
|
l_timeout_remove(sc->sp.timeout);
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sc->start_cmd_id && nl80211)
|
|
|
|
l_genl_family_cancel(nl80211, sc->start_cmd_id);
|
|
|
|
|
|
|
|
if (sc->get_scan_cmd_id && nl80211)
|
|
|
|
l_genl_family_cancel(nl80211, sc->get_scan_cmd_id);
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
l_free(sc);
|
|
|
|
}
|
|
|
|
|
2019-02-26 23:53:54 +01:00
|
|
|
static void scan_request_triggered(struct l_genl_msg *msg, void *userdata)
|
2015-09-22 21:05:50 +02:00
|
|
|
{
|
|
|
|
struct scan_context *sc = userdata;
|
2019-05-08 03:15:23 +02:00
|
|
|
struct scan_request *sr = l_queue_peek_head(sc->requests);
|
2015-09-22 21:05:50 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
scan: Cancel CMD_TRIGGER_SCAN when removing scan context
Save the ids of the netlink trigger scan commands that we send and
cancel them in scan_ifindex_remove to fix a race leading to a
segfault. The segfault would happen every time if scan_ifindex_remove
was called in the same main loop iteration in which we sent the
command, on shutdown:
^CTerminate
src/netdev.c:netdev_free() Freeing netdev wlan3[6]
src/device.c:device_disassociated() 6
src/device.c:device_enter_state() Old State: connected, new state:
disconnected
src/device.c:device_enter_state() Old State: disconnected, new state:
autoconnect
src/scan.c:scan_periodic_start() Starting periodic scan for ifindex: 6
src/device.c:device_free()
src/device.c:bss_free() Freeing BSS 02:00:00:00:00:00
src/device.c:bss_free() Freeing BSS 02:00:00:00:01:00
Removing scan context for ifindex: 6
src/scan.c:scan_context_free() sc: 0x5555557ca290
src/scan.c:scan_notify() Scan notification 33
src/netdev.c:netdev_operstate_down_cb() netdev: 6, success: 1
src/scan.c:scan_periodic_done()
src/scan.c:scan_periodic_done() Periodic scan triggered for ifindex:
1434209520
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000064 in ?? ()
(gdb) bt
#0 0x0000000000000064 in ?? ()
#1 0x0000555555583560 in process_unicast (nlmsg=0x7fffffffc1a0,
genl=0x5555557c1d60) at ell/genl.c:390
#2 received_data (io=<optimized out>, user_data=0x5555557c1d60)
at ell/genl.c:506
#3 0x0000555555580d45 in io_callback (fd=<optimized out>,
events=1, user_data=0x5555557c1e60) at ell/io.c:120
#4 0x000055555558005f in l_main_run () at ell/main.c:381
#5 0x00005555555599c1 in main (argc=<optimized out>, argv=<optimized out>)
at src/main.c:259
2017-02-01 11:58:42 +01:00
|
|
|
sc->start_cmd_id = 0;
|
|
|
|
|
2015-09-22 21:05:50 +02:00
|
|
|
err = l_genl_msg_get_error(msg);
|
|
|
|
if (err < 0) {
|
2019-05-08 03:15:23 +02:00
|
|
|
/* Scan in progress, assume another scan is running */
|
|
|
|
if (err == -EBUSY) {
|
|
|
|
sc->state = SCAN_STATE_PASSIVE;
|
2015-09-22 21:05:50 +02:00
|
|
|
return;
|
2019-05-08 03:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l_queue_remove(sc->requests, sr);
|
|
|
|
start_next_scan_request(sc);
|
2015-09-22 21:05:50 +02:00
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
scan_request_failed(sc, sr, err);
|
2015-09-22 21:05:50 +02:00
|
|
|
|
2017-02-08 01:38:41 +01:00
|
|
|
l_error("Received error during CMD_TRIGGER_SCAN: %s (%d)",
|
|
|
|
strerror(-err), -err);
|
2015-09-22 21:05:50 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->state = sr->passive ? SCAN_STATE_PASSIVE : SCAN_STATE_ACTIVE;
|
2015-09-30 20:38:41 +02:00
|
|
|
l_debug("%s scan triggered for ifindex: %u",
|
|
|
|
sr->passive ? "Passive" : "Active", sc->ifindex);
|
2019-02-27 00:28:58 +01:00
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
sc->triggered = true;
|
2019-05-11 01:18:55 +02:00
|
|
|
sc->started = true;
|
2019-02-27 00:28:58 +01:00
|
|
|
l_genl_msg_unref(l_queue_pop_head(sr->cmds));
|
2015-10-30 11:12:14 +01:00
|
|
|
|
2018-09-18 23:48:50 +02:00
|
|
|
if (sr->trigger) {
|
2015-10-30 11:12:14 +01:00
|
|
|
sr->trigger(0, sr->userdata);
|
2018-09-18 23:48:50 +02:00
|
|
|
|
2018-11-21 16:14:04 +01:00
|
|
|
/*
|
|
|
|
* Reset callback for the consequent scan triggerings of the
|
|
|
|
* multi-segmented scans.
|
|
|
|
*/
|
|
|
|
sr->trigger = NULL;
|
|
|
|
}
|
2015-09-22 21:05:50 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:37:59 +01:00
|
|
|
struct scan_freq_append_data {
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void scan_freq_append(uint32_t freq, void *user_data)
|
|
|
|
{
|
|
|
|
struct scan_freq_append_data *data = user_data;
|
|
|
|
|
|
|
|
l_genl_msg_append_attr(data->msg, data->count++, 4, &freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scan_build_attr_scan_frequencies(struct l_genl_msg *msg,
|
|
|
|
struct scan_freq_set *freqs)
|
|
|
|
{
|
|
|
|
struct scan_freq_append_data append_data = { msg, 0 };
|
|
|
|
|
|
|
|
l_genl_msg_enter_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES);
|
|
|
|
|
|
|
|
scan_freq_set_foreach(freqs, scan_freq_append, &append_data);
|
|
|
|
|
|
|
|
l_genl_msg_leave_nested(msg);
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:14:55 +01:00
|
|
|
static bool scan_mac_address_randomization_is_disabled(void)
|
|
|
|
{
|
|
|
|
const struct l_settings *config = iwd_get_config();
|
|
|
|
bool disabled;
|
|
|
|
|
|
|
|
if (!l_settings_get_bool(config, "Scan",
|
|
|
|
"disable_mac_address_randomization",
|
|
|
|
&disabled))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return disabled;
|
|
|
|
}
|
|
|
|
|
2018-07-11 00:47:00 +02:00
|
|
|
static struct l_genl_msg *scan_build_cmd(struct scan_context *sc,
|
2019-04-11 22:56:06 +02:00
|
|
|
bool ignore_flush_flag, bool is_passive,
|
2016-12-23 11:37:58 +01:00
|
|
|
const struct scan_parameters *params)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
2017-01-31 03:42:57 +01:00
|
|
|
uint32_t flags = 0;
|
2016-12-23 11:37:59 +01:00
|
|
|
|
2019-02-28 01:44:16 +01:00
|
|
|
msg = l_genl_msg_new(NL80211_CMD_TRIGGER_SCAN);
|
2016-12-23 11:37:58 +01:00
|
|
|
|
2018-07-11 00:47:00 +02:00
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &sc->ifindex);
|
2016-12-23 11:37:58 +01:00
|
|
|
|
|
|
|
if (params->extra_ie && params->extra_ie_size)
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IE,
|
|
|
|
params->extra_ie_size,
|
|
|
|
params->extra_ie);
|
|
|
|
|
2016-12-23 11:37:59 +01:00
|
|
|
if (params->freqs)
|
|
|
|
scan_build_attr_scan_frequencies(msg, params->freqs);
|
|
|
|
|
2018-07-11 00:47:00 +02:00
|
|
|
if (params->flush && !ignore_flush_flag)
|
2017-01-31 03:42:57 +01:00
|
|
|
flags |= NL80211_SCAN_FLAG_FLUSH;
|
|
|
|
|
2019-04-11 22:56:06 +02:00
|
|
|
if (!is_passive && params->randomize_mac_addr_hint &&
|
2019-01-16 04:14:55 +01:00
|
|
|
wiphy_can_randomize_mac_addr(sc->wiphy) &&
|
|
|
|
!scan_mac_address_randomization_is_disabled())
|
|
|
|
/*
|
|
|
|
* Randomizing 46 bits (locally administered 1 and multicast 0
|
|
|
|
* is assumed).
|
|
|
|
*/
|
|
|
|
flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
|
2018-07-11 00:47:02 +02:00
|
|
|
|
2019-04-11 22:56:07 +02:00
|
|
|
if (!is_passive && wiphy_has_ext_feature(sc->wiphy,
|
|
|
|
NL80211_EXT_FEATURE_SCAN_RANDOM_SN))
|
|
|
|
flags |= NL80211_SCAN_FLAG_RANDOM_SN;
|
|
|
|
|
2017-01-31 03:42:57 +01:00
|
|
|
if (flags)
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_SCAN_FLAGS, 4, &flags);
|
|
|
|
|
2016-12-23 11:37:58 +01:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-07-22 14:15:18 +02:00
|
|
|
struct scan_cmds_add_data {
|
|
|
|
struct scan_context *sc;
|
|
|
|
const struct scan_parameters *params;
|
|
|
|
struct l_queue *cmds;
|
|
|
|
struct l_genl_msg **cmd;
|
|
|
|
uint8_t max_ssids_per_scan;
|
|
|
|
uint8_t num_ssids_can_append;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool scan_cmds_add_hidden(const struct network_info *network,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct scan_cmds_add_data *data = user_data;
|
|
|
|
|
|
|
|
if (!network->is_hidden)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
l_genl_msg_append_attr(*data->cmd, NL80211_ATTR_SSID,
|
|
|
|
strlen(network->ssid), network->ssid);
|
|
|
|
data->num_ssids_can_append--;
|
|
|
|
|
|
|
|
if (!data->num_ssids_can_append) {
|
|
|
|
l_genl_msg_leave_nested(*data->cmd);
|
|
|
|
l_queue_push_tail(data->cmds, *data->cmd);
|
|
|
|
|
|
|
|
data->num_ssids_can_append = data->max_ssids_per_scan;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a consecutive scan trigger in the batch of scans.
|
|
|
|
* The 'flush' flag is ignored, this allows to get the results
|
|
|
|
* of all scans in the batch after the last scan is finished.
|
|
|
|
*/
|
2019-04-11 22:56:06 +02:00
|
|
|
*data->cmd = scan_build_cmd(data->sc, true, false,
|
|
|
|
data->params);
|
2018-07-22 14:15:18 +02:00
|
|
|
l_genl_msg_enter_nested(*data->cmd, NL80211_ATTR_SCAN_SSIDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-11 00:47:00 +02:00
|
|
|
static void scan_cmds_add(struct l_queue *cmds, struct scan_context *sc,
|
|
|
|
bool passive,
|
|
|
|
const struct scan_parameters *params)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *cmd;
|
2018-07-22 14:15:18 +02:00
|
|
|
struct scan_cmds_add_data data = {
|
|
|
|
sc,
|
|
|
|
params,
|
|
|
|
cmds,
|
|
|
|
&cmd,
|
|
|
|
wiphy_get_max_num_ssids_per_scan(sc->wiphy),
|
|
|
|
};
|
2018-07-11 00:47:00 +02:00
|
|
|
|
2019-04-11 22:56:06 +02:00
|
|
|
cmd = scan_build_cmd(sc, false, passive, params);
|
2018-07-11 00:47:00 +02:00
|
|
|
|
|
|
|
if (passive) {
|
|
|
|
/* passive scan */
|
|
|
|
l_queue_push_tail(cmds, cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_genl_msg_enter_nested(cmd, NL80211_ATTR_SCAN_SSIDS);
|
|
|
|
|
|
|
|
if (params->ssid) {
|
|
|
|
/* direct probe request scan */
|
|
|
|
l_genl_msg_append_attr(cmd, NL80211_ATTR_SSID,
|
|
|
|
strlen(params->ssid), params->ssid);
|
|
|
|
l_genl_msg_leave_nested(cmd);
|
|
|
|
|
|
|
|
l_queue_push_tail(cmds, cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-22 14:15:18 +02:00
|
|
|
data.num_ssids_can_append = data.max_ssids_per_scan;
|
|
|
|
known_networks_foreach(scan_cmds_add_hidden, &data);
|
2018-07-11 00:47:00 +02:00
|
|
|
|
|
|
|
l_genl_msg_append_attr(cmd, NL80211_ATTR_SSID, 0, NULL);
|
|
|
|
l_genl_msg_leave_nested(cmd);
|
|
|
|
l_queue_push_tail(cmds, cmd);
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:50:16 +01:00
|
|
|
static int scan_request_send_trigger(struct scan_context *sc,
|
2018-07-12 00:20:06 +02:00
|
|
|
struct scan_request *sr)
|
2018-07-11 00:47:00 +02:00
|
|
|
{
|
2019-02-27 00:28:58 +01:00
|
|
|
struct l_genl_msg *cmd = l_queue_peek_head(sr->cmds);
|
2018-07-11 00:47:00 +02:00
|
|
|
if (!cmd)
|
2018-07-12 00:20:06 +02:00
|
|
|
return -ENOMSG;
|
2018-07-11 00:47:00 +02:00
|
|
|
|
2019-02-28 22:50:15 +01:00
|
|
|
sc->start_cmd_id = l_genl_family_send(nl80211, cmd,
|
|
|
|
scan_request_triggered, sc,
|
|
|
|
NULL);
|
2018-07-13 19:30:25 +02:00
|
|
|
if (sc->start_cmd_id) {
|
2019-02-27 22:59:01 +01:00
|
|
|
l_genl_msg_ref(cmd);
|
|
|
|
|
2018-07-12 00:20:06 +02:00
|
|
|
return 0;
|
2018-07-13 19:30:25 +02:00
|
|
|
}
|
2018-07-11 00:47:00 +02:00
|
|
|
|
2019-02-28 22:50:15 +01:00
|
|
|
l_error("Scan request: failed to trigger scan.");
|
|
|
|
|
2018-07-12 00:20:06 +02:00
|
|
|
return -EIO;
|
2018-07-11 00:47:00 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 05:37:12 +02:00
|
|
|
static uint32_t scan_common(uint32_t ifindex, bool passive,
|
2016-12-23 11:37:58 +01:00
|
|
|
const struct scan_parameters *params,
|
2015-09-30 18:32:02 +02:00
|
|
|
scan_trigger_func_t trigger,
|
|
|
|
scan_notify_func_t notify, void *userdata,
|
|
|
|
scan_destroy_func_t destroy)
|
2015-09-22 21:05:50 +02:00
|
|
|
{
|
|
|
|
struct scan_context *sc;
|
|
|
|
struct scan_request *sr;
|
|
|
|
|
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(ifindex));
|
|
|
|
|
|
|
|
if (!sc)
|
2015-10-06 05:37:12 +02:00
|
|
|
return 0;
|
2015-09-22 21:05:50 +02:00
|
|
|
|
|
|
|
sr = l_new(struct scan_request, 1);
|
|
|
|
sr->trigger = trigger;
|
|
|
|
sr->callback = notify;
|
|
|
|
sr->userdata = userdata;
|
|
|
|
sr->destroy = destroy;
|
2015-09-30 18:32:02 +02:00
|
|
|
sr->passive = passive;
|
2015-10-06 05:37:12 +02:00
|
|
|
sr->id = ++next_scan_request_id;
|
2018-07-11 00:47:00 +02:00
|
|
|
sr->cmds = l_queue_new();
|
2015-09-22 21:05:50 +02:00
|
|
|
|
2018-07-11 00:47:00 +02:00
|
|
|
scan_cmds_add(sr->cmds, sc, passive, params);
|
2016-12-23 11:37:58 +01:00
|
|
|
|
2019-05-01 17:32:31 +02:00
|
|
|
/* Queue empty implies !sc->triggered && !sc->start_cmd_id */
|
|
|
|
if (!l_queue_isempty(sc->requests))
|
2015-09-22 21:05:50 +02:00
|
|
|
goto done;
|
|
|
|
|
2019-05-01 17:32:31 +02:00
|
|
|
if (sc->state != SCAN_STATE_NOT_RUNNING)
|
2015-09-22 21:05:50 +02:00
|
|
|
goto done;
|
|
|
|
|
2019-02-28 22:50:16 +01:00
|
|
|
if (!scan_request_send_trigger(sc, sr))
|
2016-12-23 11:37:58 +01:00
|
|
|
goto done;
|
2015-09-30 18:32:02 +02:00
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
sr->destroy = NULL; /* Don't call destroy when returning error */
|
2018-11-27 01:00:00 +01:00
|
|
|
scan_request_free(sr);
|
2016-12-23 11:37:58 +01:00
|
|
|
return 0;
|
2015-09-22 21:05:50 +02:00
|
|
|
done:
|
|
|
|
l_queue_push_tail(sc->requests, sr);
|
|
|
|
|
2015-10-06 05:37:12 +02:00
|
|
|
return sr->id;
|
2015-01-12 16:52:37 +01:00
|
|
|
}
|
2015-01-12 16:52:38 +01:00
|
|
|
|
2019-03-22 22:17:39 +01:00
|
|
|
uint32_t scan_passive(uint32_t ifindex, struct scan_freq_set *freqs,
|
|
|
|
scan_trigger_func_t trigger, scan_notify_func_t notify,
|
|
|
|
void *userdata, scan_destroy_func_t destroy)
|
2015-09-30 18:32:02 +02:00
|
|
|
{
|
2019-03-22 22:17:39 +01:00
|
|
|
struct scan_parameters params = { .freqs = freqs };
|
2016-12-23 11:37:58 +01:00
|
|
|
|
|
|
|
return scan_common(ifindex, true, ¶ms, trigger, notify,
|
2015-09-30 18:32:02 +02:00
|
|
|
userdata, destroy);
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:37:12 +02:00
|
|
|
uint32_t scan_active(uint32_t ifindex, uint8_t *extra_ie, size_t extra_ie_size,
|
2015-09-30 18:32:02 +02:00
|
|
|
scan_trigger_func_t trigger,
|
|
|
|
scan_notify_func_t notify, void *userdata,
|
|
|
|
scan_destroy_func_t destroy)
|
|
|
|
{
|
2016-12-23 11:37:58 +01:00
|
|
|
struct scan_parameters params = {};
|
|
|
|
|
|
|
|
params.extra_ie = extra_ie;
|
|
|
|
params.extra_ie_size = extra_ie_size;
|
|
|
|
|
|
|
|
return scan_common(ifindex, false, ¶ms,
|
|
|
|
trigger, notify, userdata, destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t scan_active_full(uint32_t ifindex,
|
|
|
|
const struct scan_parameters *params,
|
|
|
|
scan_trigger_func_t trigger, scan_notify_func_t notify,
|
|
|
|
void *userdata, scan_destroy_func_t destroy)
|
|
|
|
{
|
|
|
|
return scan_common(ifindex, false, params,
|
2015-09-30 18:32:02 +02:00
|
|
|
trigger, notify, userdata, destroy);
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:37:12 +02:00
|
|
|
bool scan_cancel(uint32_t ifindex, uint32_t id)
|
|
|
|
{
|
|
|
|
struct scan_context *sc;
|
|
|
|
struct scan_request *sr;
|
|
|
|
|
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(ifindex));
|
|
|
|
if (!sc)
|
|
|
|
return false;
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
sr = l_queue_find(sc->requests, scan_request_match, L_UINT_TO_PTR(id));
|
|
|
|
if (!sr)
|
|
|
|
return false;
|
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
/* If already triggered, just zero out the callback */
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sr == l_queue_peek_head(sc->requests) && sc->triggered) {
|
2015-10-06 05:37:12 +02:00
|
|
|
sr->callback = NULL;
|
|
|
|
|
2016-09-21 18:54:53 +02:00
|
|
|
if (sr->destroy) {
|
2015-10-06 05:37:12 +02:00
|
|
|
sr->destroy(sr->userdata);
|
2016-09-21 18:54:53 +02:00
|
|
|
sr->destroy = NULL;
|
|
|
|
}
|
2015-10-06 05:37:12 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
/* If we already sent the trigger command, cancel the scan */
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sr == l_queue_peek_head(sc->requests)) {
|
|
|
|
if (sc->start_cmd_id)
|
|
|
|
l_genl_family_cancel(nl80211, sc->start_cmd_id);
|
|
|
|
|
|
|
|
if (sc->get_scan_cmd_id)
|
|
|
|
l_genl_family_cancel(nl80211, sc->get_scan_cmd_id);
|
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
sc->start_cmd_id = 0;
|
2019-05-08 03:15:23 +02:00
|
|
|
sc->get_scan_cmd_id = 0;
|
|
|
|
l_queue_remove(sc->requests, sr);
|
2019-05-11 01:18:55 +02:00
|
|
|
sc->started = false;
|
2018-12-04 03:44:34 +01:00
|
|
|
start_next_scan_request(sc);
|
2019-05-11 01:18:56 +02:00
|
|
|
} else
|
|
|
|
l_queue_remove(sc->requests, sr);
|
2017-02-08 01:38:42 +01:00
|
|
|
|
|
|
|
scan_request_free(sr);
|
2015-10-06 05:37:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
static void scan_periodic_triggered(int err, void *user_data)
|
2015-06-05 05:31:39 +02:00
|
|
|
{
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc = user_data;
|
2017-02-08 01:38:41 +01:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (err) {
|
|
|
|
scan_periodic_rearm(sc);
|
2015-06-05 05:31:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
l_debug("Periodic scan triggered for ifindex: %u", sc->ifindex);
|
2017-03-14 16:11:34 +01:00
|
|
|
|
2016-06-25 20:42:23 +02:00
|
|
|
if (sc->sp.trigger)
|
|
|
|
sc->sp.trigger(0, sc->sp.userdata);
|
2015-06-05 05:31:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
static bool scan_periodic_notify(int err, struct l_queue *bss_list,
|
|
|
|
void *user_data)
|
2016-12-23 11:37:58 +01:00
|
|
|
{
|
2019-05-08 03:15:23 +02:00
|
|
|
struct scan_context *sc = user_data;
|
2018-07-11 00:47:01 +02:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
scan_periodic_rearm(sc);
|
2018-07-11 00:47:01 +02:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sc->sp.callback)
|
|
|
|
return sc->sp.callback(err, bss_list, sc->sp.userdata);
|
2018-07-11 00:47:01 +02:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 11:37:58 +01:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
static bool scan_periodic_queue(struct scan_context *sc)
|
|
|
|
{
|
|
|
|
if (!l_queue_isempty(sc->requests)) {
|
|
|
|
sc->sp.retry = true;
|
2016-12-23 11:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sc->sp.needs_active_scan && known_networks_has_hidden()) {
|
|
|
|
struct scan_parameters params = {
|
|
|
|
.randomize_mac_addr_hint = true
|
|
|
|
};
|
|
|
|
|
|
|
|
sc->sp.needs_active_scan = false;
|
|
|
|
|
|
|
|
sc->sp.id = scan_active_full(sc->ifindex, ¶ms,
|
|
|
|
scan_periodic_triggered,
|
|
|
|
scan_periodic_notify, sc, NULL);
|
|
|
|
} else
|
|
|
|
sc->sp.id = scan_passive(sc->ifindex, NULL,
|
|
|
|
scan_periodic_triggered,
|
|
|
|
scan_periodic_notify, sc, NULL);
|
|
|
|
|
|
|
|
return sc->sp.id != 0;
|
2016-12-23 11:37:58 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 21:54:24 +01:00
|
|
|
static bool scan_periodic_is_disabled(void)
|
|
|
|
{
|
|
|
|
const struct l_settings *config = iwd_get_config();
|
|
|
|
bool disabled;
|
|
|
|
|
|
|
|
if (!l_settings_get_bool(config, "Scan", "disable_periodic_scan",
|
|
|
|
&disabled))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return disabled;
|
|
|
|
}
|
|
|
|
|
2016-06-25 20:42:23 +02:00
|
|
|
void scan_periodic_start(uint32_t ifindex, scan_trigger_func_t trigger,
|
|
|
|
scan_notify_func_t func, void *userdata)
|
2015-06-05 05:31:39 +02:00
|
|
|
{
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc;
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2018-11-15 21:54:24 +01:00
|
|
|
if (scan_periodic_is_disabled())
|
|
|
|
return;
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
2015-06-05 05:31:39 +02:00
|
|
|
L_UINT_TO_PTR(ifindex));
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
if (!sc) {
|
|
|
|
l_error("scan_periodic_start called without scan_ifindex_add");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sp.interval)
|
2015-06-05 05:31:39 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("Starting periodic scan for ifindex: %u", ifindex);
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.interval = SCAN_INIT_INTERVAL;
|
2016-06-25 20:42:23 +02:00
|
|
|
sc->sp.trigger = trigger;
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.callback = func;
|
|
|
|
sc->sp.userdata = userdata;
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
/* If nothing queued, start the first periodic scan */
|
|
|
|
scan_periodic_queue(sc);
|
2015-06-05 05:31:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool scan_periodic_stop(uint32_t ifindex)
|
|
|
|
{
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc;
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
2015-06-05 05:31:39 +02:00
|
|
|
L_UINT_TO_PTR(ifindex));
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
if (!sc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!sc->sp.interval)
|
2015-06-05 05:31:39 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
l_debug("Stopping periodic scan for ifindex: %u", ifindex);
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sc->sp.timeout)
|
2016-04-13 23:03:33 +02:00
|
|
|
l_timeout_remove(sc->sp.timeout);
|
2019-05-08 03:15:23 +02:00
|
|
|
|
|
|
|
if (sc->sp.id) {
|
|
|
|
scan_cancel(ifindex, sc->sp.id);
|
|
|
|
sc->sp.id = 0;
|
2016-04-13 23:03:33 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.interval = 0;
|
2016-06-25 20:42:23 +02:00
|
|
|
sc->sp.trigger = NULL;
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.callback = NULL;
|
|
|
|
sc->sp.userdata = NULL;
|
2015-09-22 21:05:50 +02:00
|
|
|
sc->sp.retry = false;
|
2018-07-11 00:47:01 +02:00
|
|
|
sc->sp.needs_active_scan = false;
|
|
|
|
|
2015-06-05 05:31:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scan_periodic_timeout(struct l_timeout *timeout, void *user_data)
|
|
|
|
{
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc = user_data;
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
l_debug("scan_periodic_timeout: %u", sc->ifindex);
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.interval *= 2;
|
2016-12-23 11:37:58 +01:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
scan_periodic_queue(sc);
|
2015-06-05 05:31:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:32:32 +02:00
|
|
|
static void scan_periodic_timeout_destroy(void *user_data)
|
|
|
|
{
|
|
|
|
struct scan_context *sc = user_data;
|
|
|
|
|
|
|
|
sc->sp.timeout = NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
static void scan_periodic_rearm(struct scan_context *sc)
|
2015-06-05 05:31:39 +02:00
|
|
|
{
|
2015-09-20 21:58:23 +02:00
|
|
|
l_debug("Arming periodic scan timer: %u", sc->sp.interval);
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
if (sc->sp.timeout)
|
|
|
|
l_timeout_modify(sc->sp.timeout, sc->sp.interval);
|
2015-06-05 05:31:39 +02:00
|
|
|
else
|
2015-09-20 21:58:23 +02:00
|
|
|
sc->sp.timeout = l_timeout_create(sc->sp.interval,
|
2019-05-01 17:32:32 +02:00
|
|
|
scan_periodic_timeout, sc,
|
|
|
|
scan_periodic_timeout_destroy);
|
2015-06-05 05:31:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 17:18:50 +01:00
|
|
|
static bool start_next_scan_request(struct scan_context *sc)
|
2017-02-08 01:38:41 +01:00
|
|
|
{
|
2019-05-08 03:15:23 +02:00
|
|
|
struct scan_request *sr = l_queue_peek_head(sc->requests);
|
2017-02-08 01:38:41 +01:00
|
|
|
|
2019-05-01 17:32:31 +02:00
|
|
|
if (sc->state != SCAN_STATE_NOT_RUNNING)
|
2017-02-09 17:18:50 +01:00
|
|
|
return true;
|
2017-02-08 01:38:41 +01:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
while (sr) {
|
2019-02-28 22:50:16 +01:00
|
|
|
if (!scan_request_send_trigger(sc, sr))
|
2017-02-09 17:18:50 +01:00
|
|
|
return true;
|
2017-02-08 01:38:41 +01:00
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
scan_request_failed(sc, sr, -EIO);
|
2019-05-08 03:15:23 +02:00
|
|
|
|
|
|
|
sr = l_queue_peek_head(sc->requests);
|
2017-02-08 01:38:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 17:18:50 +01:00
|
|
|
if (sc->sp.retry) {
|
2019-05-08 03:15:23 +02:00
|
|
|
sc->sp.retry = false;
|
|
|
|
scan_periodic_queue(sc);
|
2017-02-09 17:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-02-08 01:38:41 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 00:46:58 +02:00
|
|
|
static bool scan_parse_vendor_specific(struct scan_bss *bss, const void *data,
|
|
|
|
uint16_t len)
|
|
|
|
{
|
2019-06-14 22:12:08 +02:00
|
|
|
if (!bss->wpa && is_ie_wpa_ie(data, len))
|
2019-06-11 00:46:58 +02:00
|
|
|
bss->wpa = l_memdup(data - 2, len + 2);
|
2019-06-14 22:12:08 +02:00
|
|
|
else if (!bss->osen && is_ie_wfa_ie(data, len, IE_WFA_OI_OSEN))
|
2019-06-11 00:46:58 +02:00
|
|
|
bss->osen = l_memdup(data - 2, len + 2);
|
2019-06-14 22:12:08 +02:00
|
|
|
else if (is_ie_wfa_ie(data, len, IE_WFA_OI_HS20_INDICATION))
|
|
|
|
bss->hs20_capable = true;
|
2019-06-11 00:46:58 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2019-06-14 22:12:08 +02:00
|
|
|
return true;
|
2019-06-11 00:46:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 22:12:07 +02:00
|
|
|
/*
|
|
|
|
* Fully parses the Advertisement Protocol Element. The only thing being looked
|
|
|
|
* for is the ANQP protocol ID, but this could be burried behind several other
|
|
|
|
* advertisement tuples so the entire IE may need to be parsed.
|
|
|
|
*/
|
|
|
|
static bool scan_parse_advertisement_protocol(struct scan_bss *bss,
|
|
|
|
const void *data, uint16_t len)
|
|
|
|
{
|
|
|
|
const uint8_t *ptr = data;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
/*
|
|
|
|
* TODO: Store query info for GAS response length verification
|
|
|
|
*/
|
|
|
|
uint8_t id = ptr[1];
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
/*
|
|
|
|
* IEEE 802.11-2016 Section 11.25.3.3.1
|
|
|
|
*
|
|
|
|
* "A non-AP STA shall not transmit an ANQP request to
|
|
|
|
* an AP for any ANQP-element unless the ANQP
|
|
|
|
* Advertisement Protocol ID is included..."
|
|
|
|
*/
|
|
|
|
case IE_ADVERTISEMENT_ANQP:
|
|
|
|
bss->anqp_capable = true;
|
|
|
|
return true;
|
|
|
|
case IE_ADVERTISEMENT_MIH_SERVICE:
|
|
|
|
case IE_ADVERTISEMENT_MIH_DISCOVERY:
|
|
|
|
case IE_ADVERTISEMENT_EAS:
|
|
|
|
case IE_ADVERTISEMENT_RLQP:
|
|
|
|
len -= 2;
|
|
|
|
ptr += 2;
|
|
|
|
break;
|
|
|
|
case IE_ADVERTISEMENT_VENDOR_SPECIFIC:
|
|
|
|
/* IEEE 802.11-2016 Section 9.4.2.26 */
|
|
|
|
len -= ptr[3];
|
|
|
|
ptr += ptr[3];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:17:54 +02:00
|
|
|
static bool scan_parse_bss_information_elements(struct scan_bss *bss,
|
|
|
|
const void *data, uint16_t len)
|
|
|
|
{
|
|
|
|
struct ie_tlv_iter iter;
|
2015-06-01 16:28:05 +02:00
|
|
|
bool have_ssid = false;
|
2015-05-01 16:17:54 +02:00
|
|
|
|
|
|
|
ie_tlv_iter_init(&iter, data, len);
|
|
|
|
|
|
|
|
while (ie_tlv_iter_next(&iter)) {
|
|
|
|
uint8_t tag = ie_tlv_iter_get_tag(&iter);
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
case IE_TYPE_SSID:
|
|
|
|
if (iter.len > 32)
|
|
|
|
return false;
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
memcpy(bss->ssid, iter.data, iter.len);
|
|
|
|
bss->ssid_len = iter.len;
|
|
|
|
have_ssid = true;
|
2015-05-01 16:17:54 +02:00
|
|
|
break;
|
2015-06-10 20:05:58 +02:00
|
|
|
case IE_TYPE_SUPPORTED_RATES:
|
2019-02-23 00:39:40 +01:00
|
|
|
if (iter.len > 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bss->has_sup_rates = true;
|
|
|
|
memcpy(bss->supp_rates_ie, iter.data - 2, iter.len + 2);
|
|
|
|
|
|
|
|
break;
|
2015-06-10 20:05:58 +02:00
|
|
|
case IE_TYPE_EXTENDED_SUPPORTED_RATES:
|
2019-02-23 00:39:40 +01:00
|
|
|
bss->ext_supp_rates_ie = l_memdup(iter.data - 2,
|
|
|
|
iter.len + 2);
|
2015-06-10 20:05:58 +02:00
|
|
|
break;
|
2015-05-01 16:17:54 +02:00
|
|
|
case IE_TYPE_RSN:
|
|
|
|
if (!bss->rsne)
|
|
|
|
bss->rsne = l_memdup(iter.data - 2,
|
|
|
|
iter.len + 2);
|
|
|
|
break;
|
2015-06-10 19:37:50 +02:00
|
|
|
case IE_TYPE_BSS_LOAD:
|
|
|
|
if (ie_parse_bss_load(&iter, NULL, &bss->utilization,
|
|
|
|
NULL) < 0)
|
2018-11-05 19:25:52 +01:00
|
|
|
l_warn("Unable to parse BSS Load IE for "
|
|
|
|
MAC, MAC_STR(bss->addr));
|
2015-06-10 19:37:50 +02:00
|
|
|
else
|
|
|
|
l_debug("Load: %u/255", bss->utilization);
|
|
|
|
|
|
|
|
break;
|
2015-05-01 16:17:54 +02:00
|
|
|
case IE_TYPE_VENDOR_SPECIFIC:
|
2019-06-11 00:46:58 +02:00
|
|
|
/* Interested only in WPA/WFA IE from Vendor data */
|
|
|
|
scan_parse_vendor_specific(bss, iter.data, iter.len);
|
2015-05-01 16:17:54 +02:00
|
|
|
break;
|
2016-10-28 05:59:52 +02:00
|
|
|
case IE_TYPE_MOBILITY_DOMAIN:
|
|
|
|
if (!bss->mde_present && iter.len == 3) {
|
|
|
|
memcpy(bss->mde, iter.data, iter.len);
|
|
|
|
bss->mde_present = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-10-28 05:59:53 +02:00
|
|
|
case IE_TYPE_RM_ENABLED_CAPABILITIES:
|
|
|
|
if (iter.len != 5)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Only interested in Neighbor Reports */
|
|
|
|
|
|
|
|
bss->cap_rm_neighbor_report =
|
|
|
|
(iter.data[0] & IE_RM_CAP_NEIGHBOR_REPORT) > 0;
|
|
|
|
break;
|
2016-10-28 05:59:54 +02:00
|
|
|
case IE_TYPE_COUNTRY:
|
|
|
|
if (bss->cc_present || iter.len < 6)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bss->cc[0] = iter.data[0];
|
|
|
|
bss->cc[1] = iter.data[1];
|
|
|
|
bss->cc[2] = iter.data[2];
|
|
|
|
bss->cc_present = true;
|
|
|
|
|
2019-02-23 01:43:44 +01:00
|
|
|
break;
|
|
|
|
case IE_TYPE_HT_CAPABILITIES:
|
|
|
|
if (iter.len != 26)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bss->ht_capable = true;
|
|
|
|
memcpy(bss->ht_ie, iter.data - 2, iter.len + 2);
|
|
|
|
|
2019-02-25 21:29:32 +01:00
|
|
|
break;
|
|
|
|
case IE_TYPE_VHT_CAPABILITIES:
|
|
|
|
if (iter.len != 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bss->vht_capable = true;
|
|
|
|
memcpy(bss->vht_ie, iter.data - 2, iter.len + 2);
|
|
|
|
|
2016-10-28 05:59:54 +02:00
|
|
|
break;
|
2019-06-14 22:12:07 +02:00
|
|
|
case IE_TYPE_ADVERTISEMENT_PROTOCOL:
|
|
|
|
if (iter.len < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
scan_parse_advertisement_protocol(bss, iter.data,
|
|
|
|
iter.len);
|
|
|
|
break;
|
2015-05-01 16:17:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
return have_ssid;
|
2015-05-01 16:17:54 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr)
|
2015-05-01 16:17:54 +02:00
|
|
|
{
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
struct scan_bss *bss;
|
|
|
|
|
|
|
|
bss = l_new(struct scan_bss, 1);
|
2015-06-10 19:37:50 +02:00
|
|
|
bss->utilization = 127;
|
2015-05-01 16:17:54 +02:00
|
|
|
|
|
|
|
while (l_genl_attr_next(attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_BSS_BSSID:
|
|
|
|
if (len != sizeof(bss->addr))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(bss->addr, data, len);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_CAPABILITY:
|
|
|
|
if (len != sizeof(uint16_t))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
bss->capability = *((uint16_t *) data);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_FREQUENCY:
|
|
|
|
if (len != sizeof(uint32_t))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
bss->frequency = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_SIGNAL_MBM:
|
|
|
|
if (len != sizeof(int32_t))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
bss->signal_strength = *((int32_t *) data);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_INFORMATION_ELEMENTS:
|
|
|
|
if (!scan_parse_bss_information_elements(bss,
|
2015-06-01 16:28:05 +02:00
|
|
|
data, len))
|
2015-05-01 16:17:54 +02:00
|
|
|
goto fail;
|
|
|
|
|
2015-09-23 15:44:46 +02:00
|
|
|
bss->wsc = ie_tlv_extract_wsc_payload(data, len,
|
|
|
|
&bss->wsc_size);
|
|
|
|
|
2015-05-01 16:17:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bss;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
scan_bss_free(bss);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:24:31 +02:00
|
|
|
static struct scan_freq_set *scan_parse_attr_scan_frequencies(
|
|
|
|
struct l_genl_attr *attr)
|
|
|
|
{
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
struct scan_freq_set *set;
|
|
|
|
|
|
|
|
set = scan_freq_set_new();
|
|
|
|
|
|
|
|
while (l_genl_attr_next(attr, &type, &len, &data)) {
|
|
|
|
uint32_t freq;
|
|
|
|
|
|
|
|
if (len != sizeof(uint32_t))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
freq = *((uint32_t *) data);
|
|
|
|
scan_freq_set_add(set, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
static struct scan_bss *scan_parse_result(struct l_genl_msg *msg,
|
2015-05-01 16:17:54 +02:00
|
|
|
uint32_t *out_ifindex,
|
2015-06-01 16:28:05 +02:00
|
|
|
uint64_t *out_wdev)
|
2015-05-01 16:17:54 +02:00
|
|
|
{
|
|
|
|
struct l_genl_attr attr, nested;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint32_t ifindex;
|
|
|
|
uint64_t wdev;
|
|
|
|
struct scan_bss *bss = NULL;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != sizeof(uint32_t))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ifindex = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_WDEV:
|
|
|
|
if (len != sizeof(uint64_t))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wdev = *((uint64_t *) data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_BSS:
|
|
|
|
if (!l_genl_attr_recurse(&attr, &nested))
|
|
|
|
return NULL;
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
bss = scan_parse_attr_bss(&nested);
|
2015-05-01 16:17:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bss)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (out_ifindex)
|
|
|
|
*out_ifindex = ifindex;
|
|
|
|
|
|
|
|
if (out_wdev)
|
|
|
|
*out_wdev = wdev;
|
|
|
|
|
|
|
|
return bss;
|
|
|
|
}
|
|
|
|
|
2019-02-26 19:31:58 +01:00
|
|
|
/* User configurable options */
|
|
|
|
static double RANK_5G_FACTOR;
|
|
|
|
|
2015-06-22 21:12:09 +02:00
|
|
|
static void scan_bss_compute_rank(struct scan_bss *bss)
|
2015-06-06 01:18:55 +02:00
|
|
|
{
|
2015-06-10 19:25:27 +02:00
|
|
|
static const double RANK_RSNE_FACTOR = 1.2;
|
|
|
|
static const double RANK_WPA_FACTOR = 1.0;
|
|
|
|
static const double RANK_OPEN_FACTOR = 0.5;
|
|
|
|
static const double RANK_NO_PRIVACY_FACTOR = 0.5;
|
2015-06-10 19:37:50 +02:00
|
|
|
static const double RANK_HIGH_UTILIZATION_FACTOR = 0.8;
|
|
|
|
static const double RANK_LOW_UTILIZATION_FACTOR = 1.2;
|
2019-02-26 19:32:00 +01:00
|
|
|
static const double RANK_MIN_SUPPORTED_RATE_FACTOR = 0.6;
|
|
|
|
static const double RANK_MAX_SUPPORTED_RATE_FACTOR = 1.3;
|
2015-06-06 01:18:55 +02:00
|
|
|
double rank;
|
|
|
|
uint32_t irank;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal strength is in mBm (100 * dBm) and is negative.
|
|
|
|
* WiFi range is -0 to -100 dBm
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Heavily slanted towards signal strength */
|
2015-06-10 19:36:53 +02:00
|
|
|
rank = 10000 + bss->signal_strength;
|
2015-06-06 01:18:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prefer RSNE first, WPA second. Open networks are much less
|
|
|
|
* desirable.
|
|
|
|
*/
|
|
|
|
if (bss->rsne)
|
|
|
|
rank *= RANK_RSNE_FACTOR;
|
|
|
|
else if (bss->wpa)
|
|
|
|
rank *= RANK_WPA_FACTOR;
|
|
|
|
else
|
|
|
|
rank *= RANK_OPEN_FACTOR;
|
|
|
|
|
|
|
|
/* We prefer networks with CAP PRIVACY */
|
|
|
|
if (!(bss->capability & IE_BSS_CAP_PRIVACY))
|
|
|
|
rank *= RANK_NO_PRIVACY_FACTOR;
|
|
|
|
|
|
|
|
/* Prefer 5G networks over 2.4G */
|
|
|
|
if (bss->frequency > 4000)
|
|
|
|
rank *= RANK_5G_FACTOR;
|
|
|
|
|
2015-06-10 19:37:50 +02:00
|
|
|
/* Rank loaded APs lower and lighly loaded APs higher */
|
|
|
|
if (bss->utilization >= 192)
|
|
|
|
rank *= RANK_HIGH_UTILIZATION_FACTOR;
|
|
|
|
else if (bss->utilization <= 63)
|
|
|
|
rank *= RANK_LOW_UTILIZATION_FACTOR;
|
2015-06-06 01:18:55 +02:00
|
|
|
|
2019-02-23 00:39:40 +01:00
|
|
|
if (bss->has_sup_rates || bss->ext_supp_rates_ie) {
|
|
|
|
uint64_t data_rate;
|
|
|
|
|
2019-02-23 01:43:44 +01:00
|
|
|
if (ie_parse_data_rates(bss->has_sup_rates ?
|
|
|
|
bss->supp_rates_ie : NULL,
|
|
|
|
bss->ext_supp_rates_ie,
|
|
|
|
bss->ht_capable ? bss->ht_ie : NULL,
|
2019-02-25 21:29:32 +01:00
|
|
|
bss->vht_capable ? bss->vht_ie : NULL,
|
2019-02-23 01:43:44 +01:00
|
|
|
bss->signal_strength / 100,
|
|
|
|
&data_rate) == 0) {
|
2019-02-23 00:39:40 +01:00
|
|
|
double factor = RANK_MAX_SUPPORTED_RATE_FACTOR -
|
2015-06-10 20:06:19 +02:00
|
|
|
RANK_MIN_SUPPORTED_RATE_FACTOR;
|
|
|
|
|
2019-02-23 00:39:40 +01:00
|
|
|
/*
|
2019-02-25 21:29:32 +01:00
|
|
|
* Maximum rate is 2340Mbps (VHT)
|
2019-02-23 00:39:40 +01:00
|
|
|
*/
|
2019-04-03 18:54:41 +02:00
|
|
|
factor = factor * data_rate / 2340000000U +
|
2019-02-23 00:39:40 +01:00
|
|
|
RANK_MIN_SUPPORTED_RATE_FACTOR;
|
|
|
|
rank *= factor;
|
|
|
|
} else
|
|
|
|
rank *= RANK_MIN_SUPPORTED_RATE_FACTOR;
|
2015-06-10 20:06:19 +02:00
|
|
|
}
|
2015-06-06 01:18:55 +02:00
|
|
|
|
|
|
|
irank = rank;
|
|
|
|
|
|
|
|
if (irank > USHRT_MAX)
|
|
|
|
bss->rank = USHRT_MAX;
|
|
|
|
else
|
|
|
|
bss->rank = irank;
|
|
|
|
}
|
|
|
|
|
2015-05-01 05:21:31 +02:00
|
|
|
void scan_bss_free(struct scan_bss *bss)
|
|
|
|
{
|
2019-02-23 00:39:40 +01:00
|
|
|
l_free(bss->ext_supp_rates_ie);
|
2015-05-01 05:21:31 +02:00
|
|
|
l_free(bss->rsne);
|
|
|
|
l_free(bss->wpa);
|
2015-09-23 15:44:46 +02:00
|
|
|
l_free(bss->wsc);
|
2019-06-14 23:56:16 +02:00
|
|
|
l_free(bss->osen);
|
2015-05-01 05:21:31 +02:00
|
|
|
l_free(bss);
|
|
|
|
}
|
2015-05-18 13:31:38 +02:00
|
|
|
|
2018-11-15 18:28:46 +01:00
|
|
|
int scan_bss_get_rsn_info(const struct scan_bss *bss, struct ie_rsn_info *info)
|
2015-05-18 13:31:38 +02:00
|
|
|
{
|
2016-10-25 05:12:25 +02:00
|
|
|
/*
|
|
|
|
* If both an RSN and a WPA elements are present currently
|
|
|
|
* RSN takes priority and the WPA IE is ignored.
|
|
|
|
*/
|
2015-05-18 13:31:38 +02:00
|
|
|
if (bss->rsne) {
|
|
|
|
int res = ie_parse_rsne_from_data(bss->rsne, bss->rsne[1] + 2,
|
2016-10-25 05:12:25 +02:00
|
|
|
info);
|
2015-05-18 13:31:38 +02:00
|
|
|
if (res < 0) {
|
|
|
|
l_debug("Cannot parse RSN field (%d, %s)",
|
|
|
|
res, strerror(-res));
|
2016-10-25 05:12:25 +02:00
|
|
|
return res;
|
2015-05-18 13:31:38 +02:00
|
|
|
}
|
|
|
|
} else if (bss->wpa) {
|
|
|
|
int res = ie_parse_wpa_from_data(bss->wpa, bss->wpa[1] + 2,
|
2016-10-25 05:12:25 +02:00
|
|
|
info);
|
2015-05-18 13:31:38 +02:00
|
|
|
if (res < 0) {
|
|
|
|
l_debug("Cannot parse WPA IE (%d, %s)",
|
|
|
|
res, strerror(-res));
|
2016-10-25 05:12:25 +02:00
|
|
|
return res;
|
2015-05-18 13:31:38 +02:00
|
|
|
}
|
2019-06-11 00:46:58 +02:00
|
|
|
} else if (bss->osen) {
|
|
|
|
int res = ie_parse_osen_from_data(bss->osen, bss->osen[1] + 2,
|
|
|
|
info);
|
|
|
|
if (res < 0) {
|
|
|
|
l_debug("Cannot parse OSEN IE (%d, %s)",
|
|
|
|
res, strerror(-res));
|
|
|
|
return res;
|
|
|
|
}
|
2015-05-18 13:31:38 +02:00
|
|
|
} else
|
2016-10-25 05:12:25 +02:00
|
|
|
return -ENOENT;
|
2015-05-18 13:31:38 +02:00
|
|
|
|
2016-10-25 05:12:25 +02:00
|
|
|
return 0;
|
2015-05-18 13:31:38 +02:00
|
|
|
}
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2015-06-16 20:56:03 +02:00
|
|
|
int scan_bss_rank_compare(const void *a, const void *b, void *user_data)
|
|
|
|
{
|
|
|
|
const struct scan_bss *new_bss = a, *bss = b;
|
|
|
|
|
|
|
|
return bss->rank - new_bss->rank;
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
static void get_scan_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct scan_results *results = user_data;
|
|
|
|
struct scan_bss *bss;
|
|
|
|
uint32_t ifindex;
|
|
|
|
|
|
|
|
l_debug("get_scan_callback");
|
|
|
|
|
|
|
|
if (!results->bss_list)
|
|
|
|
results->bss_list = l_queue_new();
|
|
|
|
|
|
|
|
bss = scan_parse_result(msg, &ifindex, NULL);
|
|
|
|
if (!bss)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ifindex != results->ifindex) {
|
|
|
|
l_warn("ifindex mismatch in get_scan_callback");
|
|
|
|
scan_bss_free(bss);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:14:24 +02:00
|
|
|
bss->time_stamp = results->time_stamp;
|
|
|
|
|
2015-06-06 01:18:55 +02:00
|
|
|
scan_bss_compute_rank(bss);
|
2015-06-16 20:56:03 +02:00
|
|
|
l_queue_insert(results->bss_list, bss, scan_bss_rank_compare, NULL);
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 00:47:01 +02:00
|
|
|
static void discover_hidden_network_bsses(struct scan_context *sc,
|
|
|
|
struct l_queue *bss_list)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *bss_entry;
|
|
|
|
|
|
|
|
for (bss_entry = l_queue_get_entries(bss_list); bss_entry;
|
|
|
|
bss_entry = bss_entry->next) {
|
|
|
|
struct scan_bss *bss = bss_entry->data;
|
|
|
|
|
|
|
|
if (!util_ssid_is_hidden(bss->ssid_len, bss->ssid))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sc->sp.needs_active_scan = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:11:34 +01:00
|
|
|
static void scan_finished(struct scan_context *sc, uint32_t wiphy,
|
2019-05-08 03:15:23 +02:00
|
|
|
int err, struct l_queue *bss_list,
|
|
|
|
struct scan_request *sr)
|
2015-06-01 16:28:05 +02:00
|
|
|
{
|
|
|
|
bool new_owner = false;
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (bss_list)
|
|
|
|
discover_hidden_network_bsses(sc, bss_list);
|
|
|
|
|
|
|
|
if (sr) {
|
|
|
|
l_queue_remove(sc->requests, sr);
|
2019-05-11 01:18:55 +02:00
|
|
|
sc->started = false;
|
2015-10-06 05:37:12 +02:00
|
|
|
|
2017-03-14 16:11:34 +01:00
|
|
|
/*
|
2019-05-08 03:15:23 +02:00
|
|
|
* Can start a new scan now that we've removed this one from
|
|
|
|
* the queue. If this were an external scan request (sr NULL)
|
|
|
|
* then the SCAN_FINISHED or SCAN_ABORTED handler would have
|
|
|
|
* taken care of sending the next command for a new or ongoing
|
|
|
|
* scan, or scheduling the next periodic scan.
|
2017-03-14 16:11:34 +01:00
|
|
|
*/
|
2019-05-08 03:15:23 +02:00
|
|
|
start_next_scan_request(sc);
|
2015-09-20 21:58:23 +02:00
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sr->callback)
|
|
|
|
new_owner = sr->callback(err, bss_list, sr->userdata);
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2018-12-04 03:44:34 +01:00
|
|
|
scan_request_free(sr);
|
2019-05-08 03:15:23 +02:00
|
|
|
} else if (sc->sp.callback)
|
|
|
|
new_owner = sc->sp.callback(err, bss_list, sc->sp.userdata);
|
2015-10-06 05:37:12 +02:00
|
|
|
|
2017-03-14 16:11:34 +01:00
|
|
|
if (bss_list && !new_owner)
|
|
|
|
l_queue_destroy(bss_list,
|
|
|
|
(l_queue_destroy_func_t) scan_bss_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_scan_done(void *user)
|
|
|
|
{
|
|
|
|
struct scan_results *results = user;
|
|
|
|
struct scan_context *sc;
|
|
|
|
|
|
|
|
l_debug("get_scan_done");
|
|
|
|
|
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(results->ifindex));
|
|
|
|
if (sc)
|
2019-05-08 03:15:23 +02:00
|
|
|
scan_finished(sc, results->wiphy, 0, results->bss_list,
|
|
|
|
results->sr);
|
2017-03-14 16:11:34 +01:00
|
|
|
else
|
2015-06-01 16:28:05 +02:00
|
|
|
l_queue_destroy(results->bss_list,
|
|
|
|
(l_queue_destroy_func_t) scan_bss_free);
|
|
|
|
|
2015-06-10 23:24:31 +02:00
|
|
|
if (results->freqs)
|
|
|
|
scan_freq_set_free(results->freqs);
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
l_free(results);
|
|
|
|
}
|
|
|
|
|
2019-05-11 01:18:55 +02:00
|
|
|
static bool scan_parse_flush_flag_from_msg(struct l_genl_msg *msg)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data))
|
|
|
|
if (type == NL80211_SCAN_FLAG_FLUSH)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:24:31 +02:00
|
|
|
static void scan_parse_new_scan_results(struct l_genl_msg *msg,
|
|
|
|
struct scan_results *results)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr, nested;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_SCAN_FREQUENCIES:
|
|
|
|
if (!l_genl_attr_recurse(&attr, &nested)) {
|
|
|
|
l_warn("Failed to parse ATTR_SCAN_FREQUENCIES");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
results->freqs =
|
|
|
|
scan_parse_attr_scan_frequencies(&nested);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
static void scan_notify(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t cmd;
|
|
|
|
uint32_t uninitialized_var(attr_ifindex);
|
|
|
|
bool have_ifindex;
|
|
|
|
uint32_t uninitialized_var(attr_wiphy);
|
|
|
|
bool have_wiphy;
|
2015-09-20 21:58:23 +02:00
|
|
|
struct scan_context *sc;
|
|
|
|
bool active_scan = false;
|
2015-06-01 16:28:05 +02:00
|
|
|
|
|
|
|
cmd = l_genl_msg_get_command(msg);
|
|
|
|
|
|
|
|
l_debug("Scan notification %u", cmd);
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
have_wiphy = true;
|
|
|
|
attr_wiphy = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid interface index attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
have_ifindex = true;
|
|
|
|
attr_ifindex = *((uint32_t *) data);
|
|
|
|
break;
|
2015-09-20 21:58:23 +02:00
|
|
|
case NL80211_ATTR_SCAN_SSIDS:
|
|
|
|
active_scan = true;
|
|
|
|
break;
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_wiphy) {
|
|
|
|
l_warn("Scan results do not contain wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_ifindex) {
|
|
|
|
l_warn("Scan results do not contain ifindex attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(attr_ifindex));
|
|
|
|
if (!sc)
|
2015-06-01 16:28:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case NL80211_CMD_NEW_SCAN_RESULTS:
|
|
|
|
{
|
2015-06-10 23:24:31 +02:00
|
|
|
struct l_genl_msg *scan_msg;
|
2015-06-01 16:28:05 +02:00
|
|
|
struct scan_results *results;
|
2019-05-08 03:15:23 +02:00
|
|
|
struct scan_request *sr = l_queue_peek_head(sc->requests);
|
|
|
|
bool send_next = false;
|
|
|
|
bool get_results = false;
|
|
|
|
|
|
|
|
if (sc->state == SCAN_STATE_NOT_RUNNING)
|
|
|
|
break;
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2019-03-11 15:43:14 +01:00
|
|
|
sc->state = SCAN_STATE_NOT_RUNNING;
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
/* Was this our own scan or an external scan */
|
|
|
|
if (sc->triggered) {
|
|
|
|
if (!sr->callback) {
|
|
|
|
scan_finished(sc, attr_wiphy, -ECANCELED, NULL, sr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->triggered = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this was the last command for the current request
|
|
|
|
* avoid starting the next request until the GET_SCAN
|
|
|
|
* dump callback so that any current request is always
|
|
|
|
* at the top of the queue and handling is simpler.
|
|
|
|
*/
|
|
|
|
if (l_queue_isempty(sr->cmds))
|
|
|
|
get_results = true;
|
|
|
|
else
|
|
|
|
send_next = true;
|
|
|
|
} else {
|
|
|
|
if (sc->get_scan_cmd_id)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (sc->sp.callback)
|
|
|
|
get_results = true;
|
|
|
|
|
2019-05-11 01:18:55 +02:00
|
|
|
/* An external scan may have flushed our results */
|
|
|
|
if (sc->started && scan_parse_flush_flag_from_msg(msg))
|
|
|
|
scan_finished(sc, attr_wiphy, -EAGAIN, NULL, sr);
|
|
|
|
else if (sr && !sc->start_cmd_id)
|
2019-05-08 03:15:23 +02:00
|
|
|
send_next = true;
|
|
|
|
|
|
|
|
sr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the next command of a new or an ongoing request */
|
|
|
|
if (send_next)
|
|
|
|
start_next_scan_request(sc);
|
|
|
|
|
|
|
|
if (!get_results)
|
|
|
|
break;
|
2018-07-11 00:47:00 +02:00
|
|
|
|
2015-06-01 16:28:05 +02:00
|
|
|
results = l_new(struct scan_results, 1);
|
|
|
|
results->wiphy = attr_wiphy;
|
|
|
|
results->ifindex = attr_ifindex;
|
2019-04-11 21:14:24 +02:00
|
|
|
results->time_stamp = l_time_now();
|
2019-05-08 03:15:23 +02:00
|
|
|
results->sr = sr;
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2015-06-10 23:24:31 +02:00
|
|
|
scan_parse_new_scan_results(msg, results);
|
|
|
|
|
|
|
|
scan_msg = l_genl_msg_new_sized(NL80211_CMD_GET_SCAN, 8);
|
|
|
|
l_genl_msg_append_attr(scan_msg, NL80211_ATTR_IFINDEX, 4,
|
2015-06-01 16:28:05 +02:00
|
|
|
&attr_ifindex);
|
2015-06-10 23:24:31 +02:00
|
|
|
l_genl_family_dump(nl80211, scan_msg, get_scan_callback,
|
|
|
|
results, get_scan_done);
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2015-09-20 21:58:23 +02:00
|
|
|
break;
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
2015-09-20 21:58:23 +02:00
|
|
|
|
|
|
|
case NL80211_CMD_TRIGGER_SCAN:
|
|
|
|
if (active_scan)
|
|
|
|
sc->state = SCAN_STATE_ACTIVE;
|
|
|
|
else
|
|
|
|
sc->state = SCAN_STATE_PASSIVE;
|
|
|
|
|
|
|
|
break;
|
2016-04-12 00:53:06 +02:00
|
|
|
|
|
|
|
case NL80211_CMD_SCAN_ABORTED:
|
2019-05-08 03:15:23 +02:00
|
|
|
{
|
|
|
|
struct scan_request *sr = l_queue_peek_head(sc->requests);
|
|
|
|
|
|
|
|
if (sc->state == SCAN_STATE_NOT_RUNNING)
|
|
|
|
break;
|
|
|
|
|
2019-03-11 15:43:14 +01:00
|
|
|
sc->state = SCAN_STATE_NOT_RUNNING;
|
|
|
|
|
2019-05-08 03:15:23 +02:00
|
|
|
if (sc->triggered) {
|
|
|
|
sc->triggered = false;
|
|
|
|
|
|
|
|
scan_finished(sc, attr_wiphy, -ECANCELED, NULL,
|
|
|
|
l_queue_peek_head(sc->requests));
|
|
|
|
} else if (sr && !sc->start_cmd_id && !sc->get_scan_cmd_id) {
|
|
|
|
/*
|
|
|
|
* If this was an external scan that got aborted
|
|
|
|
* we may be able to now queue our own scan although
|
|
|
|
* the abort could also have been triggered by the
|
|
|
|
* hardware or the driver because of another activity
|
|
|
|
* starting in which case we should just get an EBUSY.
|
|
|
|
*/
|
|
|
|
start_next_scan_request(sc);
|
|
|
|
}
|
2017-03-13 16:30:46 +01:00
|
|
|
|
2016-04-12 00:53:06 +02:00
|
|
|
break;
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
2019-05-08 03:15:23 +02:00
|
|
|
}
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 22:31:13 +02:00
|
|
|
uint8_t scan_freq_to_channel(uint32_t freq, enum scan_band *out_band)
|
|
|
|
{
|
|
|
|
uint32_t channel = 0;
|
|
|
|
|
|
|
|
if (freq >= 2412 && freq <= 2484) {
|
|
|
|
if (freq == 2484)
|
|
|
|
channel = 14;
|
|
|
|
else {
|
|
|
|
channel = freq - 2407;
|
|
|
|
|
|
|
|
if (channel % 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
channel /= 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_band)
|
|
|
|
*out_band = SCAN_BAND_2_4_GHZ;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freq >= 5005 && freq < 5900) {
|
|
|
|
if (channel % 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
channel = (freq - 5000) / 5;
|
|
|
|
|
|
|
|
if (out_band)
|
|
|
|
*out_band = SCAN_BAND_5_GHZ;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freq >= 4905 && freq < 5000) {
|
|
|
|
if (channel % 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
channel = (freq - 4000) / 5;
|
|
|
|
|
|
|
|
if (out_band)
|
|
|
|
*out_band = SCAN_BAND_5_GHZ;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 05:47:52 +02:00
|
|
|
uint32_t scan_channel_to_freq(uint8_t channel, enum scan_band band)
|
|
|
|
{
|
|
|
|
if (band == SCAN_BAND_2_4_GHZ) {
|
|
|
|
if (channel >= 1 && channel <= 13)
|
|
|
|
return 2407 + 5 * channel;
|
|
|
|
|
|
|
|
if (channel == 14)
|
|
|
|
return 2484;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (band == SCAN_BAND_5_GHZ) {
|
|
|
|
if (channel >= 1 && channel <= 179)
|
|
|
|
return 5000 + 5 * channel;
|
|
|
|
|
|
|
|
if (channel >= 181 && channel <= 199)
|
|
|
|
return 4000 + 5 * channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 05:47:53 +02:00
|
|
|
static const char *const oper_class_us_codes[] = {
|
|
|
|
"US", "CA"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const oper_class_eu_codes[] = {
|
|
|
|
"AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
|
|
|
|
"DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
|
|
|
|
"LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
|
|
|
|
"RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Annex E, table E-1 */
|
|
|
|
static const uint8_t oper_class_us_to_global[] = {
|
|
|
|
[1] = 115, [2] = 118, [3] = 124, [4] = 121,
|
|
|
|
[5] = 125, [6] = 103, [7] = 103, [8] = 102,
|
|
|
|
[9] = 102, [10] = 101, [11] = 101, [12] = 81,
|
|
|
|
[13] = 94, [14] = 95, [15] = 96, [22] = 116,
|
|
|
|
[23] = 119, [24] = 122, [25] = 126, [26] = 126,
|
|
|
|
[27] = 117, [28] = 120, [29] = 123, [30] = 127,
|
2018-02-13 18:43:58 +01:00
|
|
|
[31] = 127, [32] = 83, [33] = 84, [34] = 180,
|
|
|
|
/* 128 - 130 is a 1 to 1 mapping */
|
2016-10-04 05:47:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Annex E, table E-2 */
|
|
|
|
static const uint8_t oper_class_eu_to_global[] = {
|
|
|
|
[1] = 115, [2] = 118, [3] = 121, [4] = 81,
|
|
|
|
[5] = 116, [6] = 119, [7] = 122, [8] = 117,
|
|
|
|
[9] = 120, [10] = 123, [11] = 83, [12] = 84,
|
2018-02-13 18:43:58 +01:00
|
|
|
[17] = 125, [18] = 130,
|
|
|
|
/* 128 - 130 is a 1 to 1 mapping */
|
2016-10-04 05:47:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Annex E, table E-3 */
|
|
|
|
static const uint8_t oper_class_jp_to_global[] = {
|
|
|
|
[1] = 115, [2] = 112, [3] = 112, [4] = 112,
|
|
|
|
[5] = 112, [6] = 112, [7] = 109, [8] = 109,
|
|
|
|
[9] = 109, [10] = 109, [11] = 109, [12] = 113,
|
|
|
|
[13] = 113, [14] = 113, [15] = 113, [16] = 110,
|
|
|
|
[17] = 110, [18] = 110, [19] = 110, [20] = 110,
|
|
|
|
[21] = 114, [22] = 114, [23] = 114, [24] = 114,
|
|
|
|
[25] = 111, [26] = 111, [27] = 111, [28] = 111,
|
|
|
|
[29] = 111, [30] = 81, [31] = 82, [32] = 118,
|
|
|
|
[33] = 118, [34] = 121, [35] = 121, [36] = 116,
|
|
|
|
[37] = 119, [38] = 119, [39] = 122, [40] = 122,
|
|
|
|
[41] = 117, [42] = 120, [43] = 120, [44] = 123,
|
|
|
|
[45] = 123, [46] = 104, [47] = 104, [48] = 104,
|
|
|
|
[49] = 104, [50] = 104, [51] = 105, [52] = 105,
|
|
|
|
[53] = 105, [54] = 105, [55] = 105, [56] = 83,
|
2018-02-13 18:43:58 +01:00
|
|
|
[57] = 84, [58] = 121, [59] = 180,
|
|
|
|
/* 128 - 130 is a 1 to 1 mapping */
|
2016-10-04 05:47:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Annex E, table E-4 (only 2.4GHz and 4.9 / 5GHz bands) */
|
|
|
|
static const enum scan_band oper_class_to_band_global[] = {
|
|
|
|
[81 ... 84] = SCAN_BAND_2_4_GHZ,
|
2018-02-13 18:43:58 +01:00
|
|
|
[104 ... 130] = SCAN_BAND_5_GHZ,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Annex E, table E-5 */
|
|
|
|
static const uint8_t oper_class_cn_to_global[] = {
|
|
|
|
[1] = 115, [2] = 118, [3] = 125, [4] = 116,
|
|
|
|
[5] = 119, [6] = 126, [7] = 81, [8] = 83,
|
|
|
|
[9] = 84,
|
|
|
|
/* 128 - 130 is a 1 to 1 mapping */
|
2016-10-04 05:47:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum scan_band scan_oper_class_to_band(const uint8_t *country,
|
|
|
|
uint8_t oper_class)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int table = 0;
|
|
|
|
|
2018-02-13 18:43:58 +01:00
|
|
|
if (country && country[2] >= 1 && country[2] <= 5)
|
2016-10-04 05:47:53 +02:00
|
|
|
table = country[2];
|
|
|
|
else if (country) {
|
|
|
|
for (i = 0; i < L_ARRAY_SIZE(oper_class_us_codes); i++)
|
|
|
|
if (!memcmp(oper_class_us_codes[i], country, 2)) {
|
|
|
|
/* Use table E-1 */
|
|
|
|
table = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < L_ARRAY_SIZE(oper_class_eu_codes); i++)
|
|
|
|
if (!memcmp(oper_class_eu_codes[i], country, 2)) {
|
|
|
|
/* Use table E-2 */
|
|
|
|
table = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!memcmp("JP", country, 2))
|
|
|
|
/* Use table E-3 */
|
|
|
|
table = 3;
|
2018-02-13 18:43:58 +01:00
|
|
|
|
|
|
|
if (!memcmp("CN", country, 2))
|
|
|
|
/* Use table E-5 */
|
|
|
|
table = 5;
|
2016-10-04 05:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (table) {
|
|
|
|
case 1:
|
|
|
|
if (oper_class < L_ARRAY_SIZE(oper_class_us_to_global))
|
|
|
|
oper_class = oper_class_us_to_global[oper_class];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (oper_class < L_ARRAY_SIZE(oper_class_eu_to_global))
|
|
|
|
oper_class = oper_class_eu_to_global[oper_class];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (oper_class < L_ARRAY_SIZE(oper_class_jp_to_global))
|
|
|
|
oper_class = oper_class_jp_to_global[oper_class];
|
|
|
|
break;
|
2018-02-13 18:43:58 +01:00
|
|
|
case 5:
|
|
|
|
if (oper_class < L_ARRAY_SIZE(oper_class_cn_to_global))
|
|
|
|
oper_class = oper_class_cn_to_global[oper_class];
|
|
|
|
break;
|
2016-10-04 05:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oper_class < L_ARRAY_SIZE(oper_class_to_band_global))
|
|
|
|
return oper_class_to_band_global[oper_class];
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:24:06 +02:00
|
|
|
struct scan_freq_set {
|
|
|
|
uint16_t channels_2ghz;
|
|
|
|
struct l_uintset *channels_5ghz;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct scan_freq_set *scan_freq_set_new(void)
|
|
|
|
{
|
|
|
|
struct scan_freq_set *ret = l_new(struct scan_freq_set, 1);
|
|
|
|
|
|
|
|
/* 802.11-2012, 8.4.2.10 hints that 200 is the largest channel number */
|
|
|
|
ret->channels_5ghz = l_uintset_new_from_range(1, 200);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void scan_freq_set_free(struct scan_freq_set *freqs)
|
|
|
|
{
|
2015-06-10 23:24:31 +02:00
|
|
|
l_uintset_free(freqs->channels_5ghz);
|
2015-06-10 23:24:06 +02:00
|
|
|
l_free(freqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool scan_freq_set_add(struct scan_freq_set *freqs, uint32_t freq)
|
|
|
|
{
|
|
|
|
enum scan_band band;
|
|
|
|
uint8_t channel;
|
|
|
|
|
|
|
|
channel = scan_freq_to_channel(freq, &band);
|
|
|
|
if (!channel)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (band) {
|
|
|
|
case SCAN_BAND_2_4_GHZ:
|
|
|
|
freqs->channels_2ghz |= 1 << (channel - 1);
|
|
|
|
return true;
|
|
|
|
case SCAN_BAND_5_GHZ:
|
|
|
|
return l_uintset_put(freqs->channels_5ghz, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool scan_freq_set_contains(struct scan_freq_set *freqs, uint32_t freq)
|
|
|
|
{
|
|
|
|
enum scan_band band;
|
|
|
|
uint8_t channel;
|
|
|
|
|
|
|
|
channel = scan_freq_to_channel(freq, &band);
|
|
|
|
if (!channel)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (band) {
|
|
|
|
case SCAN_BAND_2_4_GHZ:
|
|
|
|
return freqs->channels_2ghz & (1 << (channel - 1));
|
|
|
|
case SCAN_BAND_5_GHZ:
|
|
|
|
return l_uintset_contains(freqs->channels_5ghz, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-06 22:23:09 +02:00
|
|
|
uint32_t scan_freq_set_get_bands(struct scan_freq_set *freqs)
|
|
|
|
{
|
|
|
|
uint32_t bands = 0;
|
|
|
|
uint32_t max;
|
|
|
|
|
|
|
|
if (freqs->channels_2ghz)
|
|
|
|
bands |= SCAN_BAND_2_4_GHZ;
|
|
|
|
|
|
|
|
max = l_uintset_get_max(freqs->channels_5ghz);
|
|
|
|
|
|
|
|
if (l_uintset_find_min(freqs->channels_5ghz) <= max)
|
|
|
|
bands |= SCAN_BAND_5_GHZ;
|
|
|
|
|
|
|
|
return bands;
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:17:44 +01:00
|
|
|
static void scan_channels_5ghz_add(uint32_t channel, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_uintset *to = user_data;
|
|
|
|
|
|
|
|
l_uintset_put(to, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scan_freq_set_merge(struct scan_freq_set *to,
|
|
|
|
const struct scan_freq_set *from)
|
|
|
|
{
|
|
|
|
to->channels_2ghz |= from->channels_2ghz;
|
|
|
|
|
|
|
|
l_uintset_foreach(from->channels_5ghz, scan_channels_5ghz_add,
|
|
|
|
to->channels_5ghz);
|
|
|
|
}
|
|
|
|
|
2019-03-07 22:06:09 +01:00
|
|
|
struct channels_5ghz_foreach_data {
|
|
|
|
scan_freq_set_func_t func;
|
|
|
|
void *user_data;
|
|
|
|
};
|
|
|
|
|
2019-03-15 00:06:18 +01:00
|
|
|
static void scan_channels_5ghz_frequency(uint32_t channel, void *user_data)
|
2019-03-07 22:06:09 +01:00
|
|
|
{
|
|
|
|
const struct channels_5ghz_foreach_data *channels_5ghz_data = user_data;
|
2019-03-15 00:06:18 +01:00
|
|
|
uint32_t freq;
|
|
|
|
|
|
|
|
freq = scan_channel_to_freq(channel, SCAN_BAND_5_GHZ);
|
2019-03-07 22:06:09 +01:00
|
|
|
|
|
|
|
channels_5ghz_data->func(freq, channels_5ghz_data->user_data);
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:17:36 +01:00
|
|
|
void scan_freq_set_foreach(const struct scan_freq_set *freqs,
|
2016-10-04 05:47:49 +02:00
|
|
|
scan_freq_set_func_t func, void *user_data)
|
|
|
|
{
|
2019-03-07 22:06:09 +01:00
|
|
|
struct channels_5ghz_foreach_data data = { };
|
2016-10-04 05:47:49 +02:00
|
|
|
uint8_t channel;
|
|
|
|
uint32_t freq;
|
|
|
|
|
2019-03-07 22:06:09 +01:00
|
|
|
if (unlikely(!freqs || !func))
|
|
|
|
return;
|
2016-10-04 05:47:49 +02:00
|
|
|
|
2019-03-07 22:06:09 +01:00
|
|
|
data.func = func;
|
|
|
|
data.user_data = user_data;
|
|
|
|
|
|
|
|
l_uintset_foreach(freqs->channels_5ghz, scan_channels_5ghz_frequency,
|
|
|
|
&data);
|
|
|
|
|
|
|
|
if (!freqs->channels_2ghz)
|
|
|
|
return;
|
2016-10-04 05:47:49 +02:00
|
|
|
|
2019-03-07 22:06:09 +01:00
|
|
|
for (channel = 1; channel <= 14; channel++) {
|
|
|
|
if (freqs->channels_2ghz & (1 << (channel - 1))) {
|
|
|
|
freq = scan_channel_to_freq(channel, SCAN_BAND_2_4_GHZ);
|
2016-10-04 05:47:49 +02:00
|
|
|
|
|
|
|
func(freq, user_data);
|
|
|
|
}
|
2019-03-07 22:06:09 +01:00
|
|
|
}
|
2016-10-04 05:47:49 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:14:27 +02:00
|
|
|
void scan_freq_set_constrain(struct scan_freq_set *set,
|
|
|
|
const struct scan_freq_set *constraint)
|
|
|
|
{
|
|
|
|
struct l_uintset *intersection;
|
|
|
|
|
|
|
|
intersection = l_uintset_intersect(constraint->channels_5ghz,
|
|
|
|
set->channels_5ghz);
|
|
|
|
if (!intersection)
|
|
|
|
/* This shouldn't ever be the case. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_uintset_free(set->channels_5ghz);
|
|
|
|
set->channels_5ghz = intersection;
|
|
|
|
|
|
|
|
set->channels_2ghz &= constraint->channels_2ghz;
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
bool scan_ifindex_add(uint32_t ifindex)
|
2015-06-01 16:28:05 +02:00
|
|
|
{
|
2019-05-28 21:12:25 +02:00
|
|
|
struct scan_context *sc;
|
2019-02-26 19:31:58 +01:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
sc = l_queue_find(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(ifindex));
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
if (sc)
|
2015-06-01 16:28:05 +02:00
|
|
|
return false;
|
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
sc = scan_context_new(ifindex);
|
|
|
|
if (!sc)
|
|
|
|
return false;
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
l_queue_push_head(scan_contexts, sc);
|
|
|
|
|
|
|
|
if (l_queue_length(scan_contexts) > 1)
|
|
|
|
goto done;
|
2019-02-26 19:31:58 +01:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
nl80211 = l_genl_family_new(iwd_get_genl(), NL80211_GENL_NAME);
|
|
|
|
l_genl_family_register(nl80211, "scan", scan_notify, NULL, NULL);
|
|
|
|
|
|
|
|
done:
|
2015-06-01 16:28:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
bool scan_ifindex_remove(uint32_t ifindex)
|
2015-06-01 16:28:05 +02:00
|
|
|
{
|
2019-05-28 21:12:25 +02:00
|
|
|
struct scan_context *sc;
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
sc = l_queue_remove_if(scan_contexts, scan_context_match,
|
|
|
|
L_UINT_TO_PTR(ifindex));
|
2015-09-22 21:07:27 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
if (!sc)
|
2015-06-01 16:28:05 +02:00
|
|
|
return false;
|
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
l_info("Removing scan context for ifindex: %u", ifindex);
|
|
|
|
scan_context_free(sc);
|
2015-06-05 05:31:39 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
if (l_queue_isempty(scan_contexts)) {
|
|
|
|
l_genl_family_free(nl80211);
|
|
|
|
nl80211 = NULL;
|
|
|
|
}
|
2015-06-01 16:28:05 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-22 21:07:02 +02:00
|
|
|
|
2019-05-28 21:12:25 +02:00
|
|
|
static int scan_init(void)
|
|
|
|
{
|
|
|
|
const struct l_settings *config = iwd_get_config();
|
|
|
|
|
|
|
|
scan_contexts = l_queue_new();
|
|
|
|
|
|
|
|
if (!l_settings_get_double(config, "Rank", "rank_5g_factor",
|
|
|
|
&RANK_5G_FACTOR))
|
|
|
|
RANK_5G_FACTOR = 1.0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scan_exit()
|
|
|
|
{
|
|
|
|
l_queue_destroy(scan_contexts,
|
|
|
|
(l_queue_destroy_func_t) scan_context_free);
|
|
|
|
scan_contexts = NULL;
|
|
|
|
l_genl_family_free(nl80211);
|
|
|
|
nl80211 = NULL;
|
2015-06-01 16:28:05 +02:00
|
|
|
}
|
2019-05-28 21:12:25 +02:00
|
|
|
|
|
|
|
IWD_MODULE(scan, scan_init, scan_exit)
|