2016-05-05 19:13:45 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2016 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2016-06-14 18:31:48 +02:00
|
|
|
#include <stdio.h>
|
2016-06-16 22:37:50 +02:00
|
|
|
#include <errno.h>
|
2016-06-16 23:22:14 +02:00
|
|
|
#include <time.h>
|
2016-06-14 18:31:48 +02:00
|
|
|
|
2016-05-05 19:13:45 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2016-06-15 17:54:13 +02:00
|
|
|
#include "src/iwd.h"
|
2016-06-14 18:14:45 +02:00
|
|
|
#include "src/common.h"
|
2016-06-16 22:37:50 +02:00
|
|
|
#include "src/util.h"
|
2016-06-15 17:54:13 +02:00
|
|
|
#include "src/ie.h"
|
2016-11-02 23:46:19 +01:00
|
|
|
#include "src/handshake.h"
|
2016-06-15 17:54:13 +02:00
|
|
|
#include "src/wiphy.h"
|
|
|
|
#include "src/scan.h"
|
2016-06-14 18:19:05 +02:00
|
|
|
#include "src/netdev.h"
|
2016-06-14 18:14:45 +02:00
|
|
|
#include "src/dbus.h"
|
|
|
|
#include "src/network.h"
|
2016-05-05 19:13:45 +02:00
|
|
|
#include "src/device.h"
|
2016-09-14 16:09:44 +02:00
|
|
|
#include "src/watchlist.h"
|
2016-05-05 19:13:45 +02:00
|
|
|
|
|
|
|
struct device_watchlist_item {
|
|
|
|
uint32_t id;
|
|
|
|
device_watch_func_t added;
|
|
|
|
device_watch_func_t removed;
|
|
|
|
void *userdata;
|
|
|
|
device_destroy_func_t destroy;
|
|
|
|
};
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
struct autoconnect_entry {
|
|
|
|
uint16_t rank;
|
|
|
|
struct network *network;
|
|
|
|
struct scan_bss *bss;
|
|
|
|
};
|
|
|
|
|
2016-06-16 23:23:00 +02:00
|
|
|
struct device {
|
|
|
|
uint32_t index;
|
|
|
|
enum device_state state;
|
|
|
|
struct l_queue *bss_list;
|
|
|
|
struct l_queue *old_bss_list;
|
|
|
|
struct l_dbus_message *scan_pending;
|
|
|
|
struct l_hashmap *networks;
|
|
|
|
struct l_queue *networks_sorted;
|
|
|
|
struct scan_bss *connected_bss;
|
|
|
|
struct network *connected_network;
|
|
|
|
struct l_queue *autoconnect_list;
|
|
|
|
struct l_dbus_message *connect_pending;
|
|
|
|
struct l_dbus_message *disconnect_pending;
|
2016-07-13 04:29:51 +02:00
|
|
|
uint32_t netdev_watch_id;
|
2016-09-14 16:09:44 +02:00
|
|
|
struct watchlist state_watches;
|
2016-06-16 23:23:00 +02:00
|
|
|
|
|
|
|
struct wiphy *wiphy;
|
|
|
|
struct netdev *netdev;
|
2016-09-22 19:57:10 +02:00
|
|
|
|
|
|
|
bool scanning : 1;
|
2016-09-22 18:57:15 +02:00
|
|
|
bool autoconnect : 1;
|
2016-06-16 23:23:00 +02:00
|
|
|
};
|
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
static struct watchlist device_watches;
|
2016-06-16 22:37:50 +02:00
|
|
|
static struct l_queue *device_list;
|
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
uint32_t device_watch_add(device_watch_func_t func,
|
2016-05-05 19:13:45 +02:00
|
|
|
void *userdata, device_destroy_func_t destroy)
|
|
|
|
{
|
2016-09-14 16:31:11 +02:00
|
|
|
return watchlist_add(&device_watches, func, userdata, destroy);
|
2016-05-05 19:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool device_watch_remove(uint32_t id)
|
|
|
|
{
|
2016-09-14 16:31:11 +02:00
|
|
|
return watchlist_remove(&device_watches, id);
|
2016-05-05 19:13:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
void __iwd_device_foreach(iwd_device_foreach_func func, void *user_data)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *device_entry;
|
|
|
|
|
|
|
|
for (device_entry = l_queue_get_entries(device_list); device_entry;
|
|
|
|
device_entry = device_entry->next) {
|
|
|
|
struct device *device = device_entry->data;
|
|
|
|
|
|
|
|
func(device, user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
static const char *iwd_network_get_path(struct device *device,
|
2016-09-20 03:17:32 +02:00
|
|
|
const char *ssid,
|
2016-06-16 23:22:14 +02:00
|
|
|
enum security security)
|
|
|
|
{
|
|
|
|
static char path[256];
|
|
|
|
unsigned int pos, i;
|
|
|
|
|
|
|
|
pos = snprintf(path, sizeof(path), "%s/", device_get_path(device));
|
|
|
|
|
2016-09-20 03:17:32 +02:00
|
|
|
for (i = 0; ssid[i] && pos < sizeof(path); i++)
|
2016-06-16 23:22:14 +02:00
|
|
|
pos += snprintf(path + pos, sizeof(path) - pos, "%02x",
|
|
|
|
ssid[i]);
|
|
|
|
|
|
|
|
snprintf(path + pos, sizeof(path) - pos, "_%s",
|
|
|
|
security_to_str(security));
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *device_state_to_string(enum device_state state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
2016-06-20 12:42:05 +02:00
|
|
|
case DEVICE_STATE_OFF:
|
|
|
|
return "off";
|
2016-06-16 23:22:14 +02:00
|
|
|
case DEVICE_STATE_DISCONNECTED:
|
|
|
|
return "disconnected";
|
|
|
|
case DEVICE_STATE_AUTOCONNECT:
|
|
|
|
return "autoconnect";
|
|
|
|
case DEVICE_STATE_CONNECTING:
|
|
|
|
return "connecting";
|
|
|
|
case DEVICE_STATE_CONNECTED:
|
|
|
|
return "connected";
|
|
|
|
case DEVICE_STATE_DISCONNECTING:
|
|
|
|
return "disconnecting";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_autoconnect_next(struct device *device)
|
|
|
|
{
|
|
|
|
struct autoconnect_entry *entry;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
while ((entry = l_queue_pop_head(device->autoconnect_list))) {
|
|
|
|
l_debug("Considering autoconnecting to BSS '%s' with SSID: %s,"
|
|
|
|
" freq: %u, rank: %u, strength: %i",
|
|
|
|
util_address_to_string(entry->bss->addr),
|
|
|
|
network_get_ssid(entry->network),
|
|
|
|
entry->bss->frequency, entry->rank,
|
|
|
|
entry->bss->signal_strength);
|
|
|
|
|
|
|
|
/* TODO: Blacklist the network from auto-connect */
|
|
|
|
r = network_autoconnect(entry->network, entry->bss);
|
|
|
|
l_free(entry);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
static void bss_free(void *data)
|
|
|
|
{
|
|
|
|
struct scan_bss *bss = data;
|
|
|
|
const char *addr;
|
|
|
|
|
|
|
|
addr = util_address_to_string(bss->addr);
|
|
|
|
l_debug("Freeing BSS %s", addr);
|
|
|
|
|
|
|
|
scan_bss_free(bss);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void network_free(void *data)
|
|
|
|
{
|
|
|
|
struct network *network = data;
|
|
|
|
|
|
|
|
network_remove(network, -ESHUTDOWN);
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
static int autoconnect_rank_compare(const void *a, const void *b, void *user)
|
|
|
|
{
|
|
|
|
const struct autoconnect_entry *new_ae = a;
|
|
|
|
const struct autoconnect_entry *ae = b;
|
|
|
|
|
|
|
|
return ae->rank - new_ae->rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool process_network(const void *key, void *data, void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = data;
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
|
|
|
if (!network_bss_list_isempty(network)) {
|
|
|
|
/* Build the network list ordered by rank */
|
|
|
|
network_rank_update(network);
|
|
|
|
|
|
|
|
l_queue_insert(device->networks_sorted, network,
|
|
|
|
network_rank_compare, NULL);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop networks that have no more BSSs in range */
|
|
|
|
l_debug("No remaining BSSs for SSID: %s -- Removing network",
|
|
|
|
network_get_ssid(network));
|
|
|
|
network_remove(network, -ERANGE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_bss(struct device *device, struct scan_bss *bss,
|
|
|
|
struct timespec *timestamp)
|
|
|
|
{
|
|
|
|
struct network *network;
|
2016-10-25 05:12:25 +02:00
|
|
|
struct ie_rsn_info info;
|
|
|
|
int r;
|
2016-06-16 23:22:14 +02:00
|
|
|
enum security security;
|
|
|
|
const char *path;
|
|
|
|
double rankmod;
|
|
|
|
struct autoconnect_entry *entry;
|
2016-09-20 03:17:32 +02:00
|
|
|
char ssid[33];
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
l_debug("Found BSS '%s' with SSID: %s, freq: %u, rank: %u, "
|
|
|
|
"strength: %i",
|
|
|
|
util_address_to_string(bss->addr),
|
|
|
|
util_ssid_to_utf8(bss->ssid_len, bss->ssid),
|
|
|
|
bss->frequency, bss->rank, bss->signal_strength);
|
|
|
|
|
|
|
|
if (!util_ssid_is_utf8(bss->ssid_len, bss->ssid)) {
|
|
|
|
l_warn("Ignoring BSS with non-UTF8 SSID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:17:32 +02:00
|
|
|
memcpy(ssid, bss->ssid, bss->ssid_len);
|
|
|
|
ssid[bss->ssid_len] = '\0';
|
|
|
|
|
2016-10-25 05:12:25 +02:00
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
r = scan_bss_get_rsn_info(bss, &info);
|
|
|
|
if (r < 0) {
|
|
|
|
if (r != -ENOENT)
|
2016-06-16 23:22:14 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
security = scan_get_security(bss->capability, NULL);
|
2016-10-25 05:12:25 +02:00
|
|
|
} else
|
|
|
|
security = scan_get_security(bss->capability, &info);
|
|
|
|
|
2016-09-20 03:17:32 +02:00
|
|
|
path = iwd_network_get_path(device, ssid, security);
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
network = l_hashmap_lookup(device->networks, path);
|
|
|
|
if (!network) {
|
2016-09-20 03:17:32 +02:00
|
|
|
network = network_create(device, ssid, security);
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
if (!network_register(network, path)) {
|
|
|
|
network_remove(network, -EINVAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_hashmap_insert(device->networks,
|
|
|
|
network_get_path(network), network);
|
|
|
|
l_debug("Added new Network \"%s\" security %s",
|
|
|
|
network_get_ssid(network), security_to_str(security));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (network_bss_list_isempty(network))
|
|
|
|
network_seen(network, timestamp);
|
|
|
|
|
|
|
|
network_bss_add(network, bss);
|
|
|
|
|
2016-08-01 14:18:24 +02:00
|
|
|
if (device->state != DEVICE_STATE_AUTOCONNECT)
|
|
|
|
return;
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
/* See if network is autoconnectable (is a known network) */
|
|
|
|
if (!network_rankmod(network, &rankmod))
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry = l_new(struct autoconnect_entry, 1);
|
|
|
|
entry->network = network;
|
|
|
|
entry->bss = bss;
|
|
|
|
entry->rank = bss->rank * rankmod;
|
|
|
|
l_queue_insert(device->autoconnect_list, entry,
|
|
|
|
autoconnect_rank_compare, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bss_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct scan_bss *bss_a = a;
|
|
|
|
const struct scan_bss *bss_b = b;
|
|
|
|
|
|
|
|
return !memcmp(bss_a->addr, bss_b->addr, sizeof(bss_a->addr));
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:35:57 +02:00
|
|
|
/*
|
|
|
|
* Used when scan results were obtained; either from passive scan running
|
|
|
|
* inside device.c or active scans running in other state machines, e.g. wsc.c
|
|
|
|
*/
|
|
|
|
void device_set_scan_results(struct device *device, struct l_queue *bss_list)
|
2016-06-16 23:22:14 +02:00
|
|
|
{
|
|
|
|
struct network *network;
|
|
|
|
const struct l_queue_entry *bss_entry;
|
|
|
|
struct timespec now;
|
2016-07-12 04:13:53 +02:00
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
|
2016-07-12 04:13:53 +02:00
|
|
|
device->scanning = false;
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Scanning");
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
device->old_bss_list = device->bss_list;
|
|
|
|
device->bss_list = bss_list;
|
|
|
|
|
|
|
|
while ((network = l_queue_pop_head(device->networks_sorted)))
|
|
|
|
network_bss_list_clear(network);
|
|
|
|
|
|
|
|
l_queue_destroy(device->autoconnect_list, l_free);
|
|
|
|
device->autoconnect_list = l_queue_new();
|
|
|
|
|
|
|
|
for (bss_entry = l_queue_get_entries(bss_list); bss_entry;
|
|
|
|
bss_entry = bss_entry->next) {
|
|
|
|
struct scan_bss *bss = bss_entry->data;
|
|
|
|
|
|
|
|
process_bss(device, bss, &now);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device->connected_bss) {
|
|
|
|
struct scan_bss *bss;
|
|
|
|
|
|
|
|
bss = l_queue_find(device->bss_list, bss_match,
|
|
|
|
device->connected_bss);
|
|
|
|
|
|
|
|
if (!bss) {
|
|
|
|
l_warn("Connected BSS not in scan results!");
|
|
|
|
l_queue_push_tail(device->bss_list,
|
|
|
|
device->connected_bss);
|
|
|
|
network_bss_add(device->connected_network,
|
|
|
|
device->connected_bss);
|
|
|
|
l_queue_remove(device->old_bss_list,
|
|
|
|
device->connected_bss);
|
|
|
|
} else
|
|
|
|
device->connected_bss = bss;
|
|
|
|
}
|
|
|
|
|
2016-06-24 05:09:05 +02:00
|
|
|
l_hashmap_foreach_remove(device->networks, process_network, device);
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
l_queue_destroy(device->old_bss_list, bss_free);
|
|
|
|
device->old_bss_list = NULL;
|
|
|
|
|
|
|
|
if (device->state == DEVICE_STATE_AUTOCONNECT)
|
|
|
|
device_autoconnect_next(device);
|
2016-09-20 03:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool new_scan_results(uint32_t wiphy_id, uint32_t ifindex,
|
|
|
|
struct l_queue *bss_list, void *userdata)
|
|
|
|
{
|
|
|
|
struct device *device = userdata;
|
2016-06-16 23:22:14 +02:00
|
|
|
|
2016-09-20 03:35:57 +02:00
|
|
|
device_set_scan_results(device, bss_list);
|
2016-06-16 23:22:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:30:19 +02:00
|
|
|
struct network *device_get_connected_network(struct device *device)
|
|
|
|
{
|
|
|
|
return device->connected_network;
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:31:48 +02:00
|
|
|
const char *device_get_path(struct device *device)
|
|
|
|
{
|
2016-07-12 04:13:54 +02:00
|
|
|
static char path[26];
|
2016-06-14 18:31:48 +02:00
|
|
|
|
2016-07-12 04:13:54 +02:00
|
|
|
snprintf(path, sizeof(path), "%s/%u", wiphy_get_path(device->wiphy),
|
|
|
|
device->index);
|
2016-06-14 18:31:48 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:34:35 +02:00
|
|
|
bool device_is_busy(struct device *device)
|
|
|
|
{
|
|
|
|
if (device->state != DEVICE_STATE_DISCONNECTED &&
|
2016-06-20 12:42:05 +02:00
|
|
|
device->state != DEVICE_STATE_AUTOCONNECT &&
|
|
|
|
device->state != DEVICE_STATE_OFF)
|
2016-06-14 18:34:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:10:09 +02:00
|
|
|
struct wiphy *device_get_wiphy(struct device *device)
|
|
|
|
{
|
|
|
|
return device->wiphy;
|
|
|
|
}
|
|
|
|
|
2016-09-14 18:10:10 +02:00
|
|
|
struct netdev *device_get_netdev(struct device *device)
|
|
|
|
{
|
|
|
|
return device->netdev;
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:10:44 +02:00
|
|
|
uint32_t device_get_ifindex(struct device *device)
|
|
|
|
{
|
|
|
|
return device->index;
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:11:16 +02:00
|
|
|
const uint8_t *device_get_address(struct device *device)
|
|
|
|
{
|
|
|
|
return netdev_get_address(device->netdev);
|
|
|
|
}
|
|
|
|
|
2016-09-22 19:40:09 +02:00
|
|
|
enum device_state device_get_state(struct device *device)
|
|
|
|
{
|
|
|
|
return device->state;
|
|
|
|
}
|
|
|
|
|
2016-07-12 04:13:53 +02:00
|
|
|
static void periodic_scan_trigger(int err, void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
|
|
|
|
device->scanning = true;
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Scanning");
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:09:44 +02:00
|
|
|
uint32_t device_add_state_watch(struct device *device,
|
|
|
|
device_state_watch_func_t func,
|
|
|
|
void *user_data,
|
|
|
|
device_destroy_func_t destroy)
|
|
|
|
{
|
|
|
|
return watchlist_add(&device->state_watches, func, user_data, destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool device_remove_state_watch(struct device *device, uint32_t id)
|
|
|
|
{
|
|
|
|
return watchlist_remove(&device->state_watches, id);
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:19:34 +02:00
|
|
|
struct network *device_network_find(struct device *device, const char *ssid,
|
|
|
|
enum security security)
|
|
|
|
{
|
|
|
|
const char *path = iwd_network_get_path(device, ssid, security);
|
|
|
|
|
|
|
|
return l_hashmap_lookup(device->networks, path);
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:10:24 +02:00
|
|
|
static void device_enter_state(struct device *device, enum device_state state)
|
2016-06-16 23:22:14 +02:00
|
|
|
{
|
2016-07-20 00:48:39 +02:00
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
bool disconnected;
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
l_debug("Old State: %s, new state: %s",
|
|
|
|
device_state_to_string(device->state),
|
|
|
|
device_state_to_string(state));
|
|
|
|
|
|
|
|
switch (state) {
|
2016-06-20 12:42:05 +02:00
|
|
|
case DEVICE_STATE_OFF:
|
|
|
|
scan_periodic_stop(device->index);
|
|
|
|
break;
|
2016-06-16 23:22:14 +02:00
|
|
|
case DEVICE_STATE_AUTOCONNECT:
|
2016-07-12 04:13:53 +02:00
|
|
|
scan_periodic_start(device->index, periodic_scan_trigger,
|
2016-06-25 20:42:23 +02:00
|
|
|
new_scan_results, device);
|
2016-06-16 23:22:14 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_STATE_DISCONNECTED:
|
|
|
|
scan_periodic_stop(device->index);
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_CONNECTED:
|
|
|
|
scan_periodic_stop(device->index);
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_CONNECTING:
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_DISCONNECTING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-20 00:48:39 +02:00
|
|
|
disconnected = device->state <= DEVICE_STATE_AUTOCONNECT;
|
|
|
|
|
|
|
|
if ((disconnected && state > DEVICE_STATE_AUTOCONNECT) ||
|
|
|
|
(!disconnected && state != device->state))
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "State");
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
device->state = state;
|
2016-09-14 16:09:44 +02:00
|
|
|
|
|
|
|
WATCHLIST_NOTIFY(&device->state_watches,
|
|
|
|
device_state_watch_func_t, state);
|
2016-06-16 23:22:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
static void device_reset_connection_state(struct device *device)
|
2016-06-14 18:14:45 +02:00
|
|
|
{
|
|
|
|
struct network *network = device->connected_network;
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
if (!network)
|
|
|
|
return;
|
2016-08-04 17:39:41 +02:00
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
if (device->state == DEVICE_STATE_CONNECTED)
|
|
|
|
network_disconnected(network);
|
2016-06-24 05:09:04 +02:00
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
device->connected_bss = NULL;
|
|
|
|
device->connected_network = NULL;
|
2016-06-24 05:09:04 +02:00
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "ConnectedNetwork");
|
|
|
|
l_dbus_property_changed(dbus, network_get_path(network),
|
|
|
|
IWD_NETWORK_INTERFACE, "Connected");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_disassociated(struct device *device)
|
|
|
|
{
|
|
|
|
l_debug("%d", device->index);
|
|
|
|
|
|
|
|
device_reset_connection_state(device);
|
2016-06-14 18:14:45 +02:00
|
|
|
|
2016-10-10 22:38:03 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_DISCONNECTED);
|
|
|
|
|
|
|
|
if (device->autoconnect)
|
|
|
|
device_enter_state(device, DEVICE_STATE_AUTOCONNECT);
|
2016-06-14 18:14:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 18:06:17 +02:00
|
|
|
static void device_lost_beacon(struct device *device)
|
|
|
|
{
|
|
|
|
l_debug("%d", device->index);
|
|
|
|
|
|
|
|
if (device->connect_pending)
|
|
|
|
dbus_pending_reply(&device->connect_pending,
|
|
|
|
dbus_error_failed(device->connect_pending));
|
|
|
|
|
|
|
|
device_disassociated(device);
|
|
|
|
}
|
|
|
|
|
2016-06-16 18:21:12 +02:00
|
|
|
static void device_disconnect_by_ap(struct device *device)
|
|
|
|
{
|
|
|
|
l_debug("%d", device->index);
|
|
|
|
|
|
|
|
if (device->connect_pending) {
|
|
|
|
struct network *network = device->connected_network;
|
|
|
|
|
|
|
|
dbus_pending_reply(&device->connect_pending,
|
|
|
|
dbus_error_failed(device->connect_pending));
|
|
|
|
|
|
|
|
network_connect_failed(network);
|
|
|
|
}
|
|
|
|
|
|
|
|
device_disassociated(device);
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
static void device_connect_cb(struct netdev *netdev, enum netdev_result result,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
2016-08-04 17:39:41 +02:00
|
|
|
l_debug("%d, result: %d", device->index, result);
|
|
|
|
|
2016-08-02 23:58:57 +02:00
|
|
|
if (device->connect_pending) {
|
2016-07-26 14:42:21 +02:00
|
|
|
struct l_dbus_message *reply;
|
|
|
|
|
2016-08-02 23:58:57 +02:00
|
|
|
switch (result) {
|
|
|
|
case NETDEV_RESULT_ABORTED:
|
|
|
|
reply = dbus_error_aborted(device->connect_pending);
|
|
|
|
break;
|
|
|
|
case NETDEV_RESULT_OK:
|
|
|
|
reply = l_dbus_message_new_method_return(
|
|
|
|
device->connect_pending);
|
|
|
|
l_dbus_message_set_arguments(reply, "");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reply = dbus_error_failed(device->connect_pending);
|
|
|
|
break;
|
2016-07-26 14:42:21 +02:00
|
|
|
}
|
2016-06-14 18:19:05 +02:00
|
|
|
|
2016-08-02 23:58:57 +02:00
|
|
|
dbus_pending_reply(&device->connect_pending, reply);
|
2016-06-14 18:19:05 +02:00
|
|
|
}
|
2016-06-16 17:53:26 +02:00
|
|
|
|
2016-08-02 23:58:57 +02:00
|
|
|
if (result != NETDEV_RESULT_OK) {
|
2016-10-10 21:53:01 +02:00
|
|
|
if (result != NETDEV_RESULT_ABORTED) {
|
|
|
|
network_connect_failed(device->connected_network);
|
2016-08-04 18:33:13 +02:00
|
|
|
device_disassociated(device);
|
2016-10-10 21:53:01 +02:00
|
|
|
}
|
2016-08-04 18:33:13 +02:00
|
|
|
|
2016-08-02 23:58:57 +02:00
|
|
|
return;
|
2016-06-16 17:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
network_connected(device->connected_network);
|
|
|
|
device_enter_state(device, DEVICE_STATE_CONNECTED);
|
2016-09-22 18:57:15 +02:00
|
|
|
device->autoconnect = true;
|
2016-06-14 18:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void device_netdev_event(struct netdev *netdev, enum netdev_event event,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2016-06-16 18:06:17 +02:00
|
|
|
struct device *device = user_data;
|
2016-06-16 21:45:56 +02:00
|
|
|
struct network *network = device->connected_network;
|
2016-06-16 18:06:17 +02:00
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
switch (event) {
|
|
|
|
case NETDEV_EVENT_AUTHENTICATING:
|
|
|
|
l_debug("Authenticating");
|
|
|
|
break;
|
|
|
|
case NETDEV_EVENT_ASSOCIATING:
|
|
|
|
l_debug("Associating");
|
|
|
|
break;
|
|
|
|
case NETDEV_EVENT_4WAY_HANDSHAKE:
|
|
|
|
l_debug("Handshaking");
|
|
|
|
break;
|
|
|
|
case NETDEV_EVENT_SETTING_KEYS:
|
|
|
|
l_debug("Setting keys");
|
2016-06-16 21:45:56 +02:00
|
|
|
|
|
|
|
/* If we got here, then our PSK works. Save if required */
|
|
|
|
network_sync_psk(network);
|
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
break;
|
|
|
|
case NETDEV_EVENT_LOST_BEACON:
|
2016-06-16 18:06:17 +02:00
|
|
|
device_lost_beacon(device);
|
2016-06-14 18:19:05 +02:00
|
|
|
break;
|
2016-06-16 18:21:12 +02:00
|
|
|
case NETDEV_EVENT_DISCONNECT_BY_AP:
|
|
|
|
device_disconnect_by_ap(device);
|
2016-12-12 18:34:28 +01:00
|
|
|
break;
|
|
|
|
case NETDEV_EVENT_DISCONNECT_BY_SME:
|
|
|
|
device_disassociated(device);
|
|
|
|
break;
|
2016-12-23 11:38:03 +01:00
|
|
|
case NETDEV_EVENT_RSSI_THRESHOLD_LOW:
|
|
|
|
case NETDEV_EVENT_RSSI_THRESHOLD_HIGH:
|
|
|
|
break;
|
2016-06-14 18:19:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-22 18:57:15 +02:00
|
|
|
bool device_set_autoconnect(struct device *device, bool autoconnect)
|
|
|
|
{
|
|
|
|
if (device->autoconnect == autoconnect)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
device->autoconnect = autoconnect;
|
|
|
|
|
|
|
|
if (device->state == DEVICE_STATE_DISCONNECTED && autoconnect)
|
|
|
|
device_enter_state(device, DEVICE_STATE_AUTOCONNECT);
|
|
|
|
|
|
|
|
if (device->state == DEVICE_STATE_AUTOCONNECT && !autoconnect)
|
|
|
|
device_enter_state(device, DEVICE_STATE_DISCONNECTED);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:46:11 +01:00
|
|
|
static enum ie_rsn_akm_suite device_select_akm_suite(struct network *network,
|
|
|
|
struct scan_bss *bss,
|
|
|
|
struct ie_rsn_info *info)
|
|
|
|
{
|
|
|
|
enum security security = network_get_security(network);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If FT is available, use FT authentication to keep the door open
|
|
|
|
* for fast transitions. Otherwise use SHA256 version if present.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (security == SECURITY_8021X) {
|
|
|
|
if ((info->akm_suites & IE_RSN_AKM_SUITE_FT_OVER_8021X) &&
|
|
|
|
bss->rsne && bss->mde_present)
|
|
|
|
return IE_RSN_AKM_SUITE_FT_OVER_8021X;
|
|
|
|
|
|
|
|
if (info->akm_suites & IE_RSN_AKM_SUITE_8021X_SHA256)
|
|
|
|
return IE_RSN_AKM_SUITE_8021X_SHA256;
|
|
|
|
|
|
|
|
if (info->akm_suites & IE_RSN_AKM_SUITE_8021X)
|
|
|
|
return IE_RSN_AKM_SUITE_8021X;
|
|
|
|
} else if (security == SECURITY_PSK) {
|
|
|
|
if ((info->akm_suites & IE_RSN_AKM_SUITE_FT_USING_PSK) &&
|
|
|
|
bss->rsne && bss->mde_present)
|
|
|
|
return IE_RSN_AKM_SUITE_FT_USING_PSK;
|
|
|
|
|
|
|
|
if (info->akm_suites & IE_RSN_AKM_SUITE_PSK_SHA256)
|
|
|
|
return IE_RSN_AKM_SUITE_PSK_SHA256;
|
|
|
|
|
|
|
|
if (info->akm_suites & IE_RSN_AKM_SUITE_PSK)
|
|
|
|
return IE_RSN_AKM_SUITE_PSK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
void device_connect_network(struct device *device, struct network *network,
|
|
|
|
struct scan_bss *bss,
|
|
|
|
struct l_dbus_message *message)
|
|
|
|
{
|
2016-06-15 17:54:13 +02:00
|
|
|
enum security security = network_get_security(network);
|
|
|
|
struct wiphy *wiphy = device->wiphy;
|
2016-06-14 18:19:05 +02:00
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
2016-12-12 18:34:19 +01:00
|
|
|
struct handshake_state *hs;
|
2016-11-02 23:46:17 +01:00
|
|
|
bool add_mde = false;
|
|
|
|
uint8_t *mde;
|
2016-06-15 17:54:13 +02:00
|
|
|
|
2016-12-12 18:34:19 +01:00
|
|
|
hs = handshake_state_new(netdev_get_ifindex(device->netdev));
|
|
|
|
|
2016-06-15 17:54:13 +02:00
|
|
|
if (security == SECURITY_PSK || security == SECURITY_8021X) {
|
2016-10-25 05:12:25 +02:00
|
|
|
struct ie_rsn_info bss_info;
|
2016-06-15 17:54:13 +02:00
|
|
|
uint8_t rsne_buf[256];
|
|
|
|
struct ie_rsn_info info;
|
2016-11-02 23:46:17 +01:00
|
|
|
const char *ssid;
|
2016-06-15 17:54:13 +02:00
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
2016-10-25 05:12:25 +02:00
|
|
|
memset(&bss_info, 0, sizeof(bss_info));
|
|
|
|
scan_bss_get_rsn_info(bss, &bss_info);
|
2016-06-15 17:54:13 +02:00
|
|
|
|
2016-11-02 23:46:11 +01:00
|
|
|
info.akm_suites = device_select_akm_suite(network, bss,
|
|
|
|
&bss_info);
|
|
|
|
|
|
|
|
if (!info.akm_suites) {
|
|
|
|
l_dbus_send(dbus, dbus_error_not_supported(message));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:54:13 +02:00
|
|
|
info.pairwise_ciphers = wiphy_select_cipher(wiphy,
|
2016-10-25 05:12:25 +02:00
|
|
|
bss_info.pairwise_ciphers);
|
|
|
|
info.group_cipher = wiphy_select_cipher(wiphy,
|
|
|
|
bss_info.group_cipher);
|
2016-11-02 22:29:18 +01:00
|
|
|
info.group_management_cipher = wiphy_select_cipher(wiphy,
|
|
|
|
bss_info.group_management_cipher);
|
|
|
|
|
2016-11-02 23:46:11 +01:00
|
|
|
if (!info.pairwise_ciphers || !info.group_cipher) {
|
|
|
|
l_dbus_send(dbus, dbus_error_not_supported(message));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:29:18 +01:00
|
|
|
if (info.group_management_cipher == 0 && bss_info.mfpr) {
|
|
|
|
l_dbus_send(dbus, dbus_error_not_supported(message));
|
|
|
|
return;
|
|
|
|
} else if (info.group_management_cipher != 0)
|
|
|
|
info.mfpc = true;
|
2016-06-15 17:54:13 +02:00
|
|
|
|
2016-11-02 23:46:17 +01:00
|
|
|
ssid = network_get_ssid(network);
|
2016-11-02 23:46:19 +01:00
|
|
|
handshake_state_set_ssid(hs, (void *) ssid, strlen(ssid));
|
2016-11-02 23:46:17 +01:00
|
|
|
|
2016-06-15 17:54:13 +02:00
|
|
|
/* RSN takes priority */
|
|
|
|
if (bss->rsne) {
|
|
|
|
ie_build_rsne(&info, rsne_buf);
|
2016-11-02 23:46:19 +01:00
|
|
|
handshake_state_set_ap_rsn(hs, bss->rsne);
|
|
|
|
handshake_state_set_own_rsn(hs, rsne_buf);
|
2016-06-15 17:54:13 +02:00
|
|
|
} else {
|
|
|
|
ie_build_wpa(&info, rsne_buf);
|
2016-11-02 23:46:19 +01:00
|
|
|
handshake_state_set_ap_wpa(hs, bss->wpa);
|
|
|
|
handshake_state_set_own_wpa(hs, rsne_buf);
|
2016-06-15 17:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (security == SECURITY_PSK)
|
2016-11-02 23:46:19 +01:00
|
|
|
handshake_state_set_pmk(hs, network_get_psk(network));
|
2016-06-15 17:54:13 +02:00
|
|
|
else
|
2016-11-02 23:46:19 +01:00
|
|
|
handshake_state_set_8021x_config(hs,
|
|
|
|
network_get_settings(network));
|
2016-11-02 23:46:17 +01:00
|
|
|
|
|
|
|
if (info.akm_suites & (IE_RSN_AKM_SUITE_FT_OVER_8021X |
|
|
|
|
IE_RSN_AKM_SUITE_FT_USING_PSK |
|
|
|
|
IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256))
|
|
|
|
add_mde = true;
|
2016-06-15 17:54:13 +02:00
|
|
|
}
|
2016-06-14 18:19:05 +02:00
|
|
|
|
2016-11-02 23:46:17 +01:00
|
|
|
if (security == SECURITY_NONE)
|
|
|
|
/* Perform FT association if available */
|
|
|
|
add_mde = bss->mde_present;
|
|
|
|
|
|
|
|
if (add_mde) {
|
|
|
|
mde = l_malloc(5);
|
|
|
|
|
|
|
|
/* The MDE advertised by the BSS must be passed verbatim */
|
|
|
|
mde[0] = IE_TYPE_MOBILITY_DOMAIN;
|
|
|
|
mde[1] = 3;
|
|
|
|
memcpy(mde + 2, bss->mde, 3);
|
|
|
|
|
2016-12-12 18:34:19 +01:00
|
|
|
handshake_state_set_mde(hs, mde);
|
2016-11-02 23:46:17 +01:00
|
|
|
} else
|
|
|
|
mde = NULL;
|
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
device->connect_pending = l_dbus_message_ref(message);
|
|
|
|
|
2016-12-12 18:34:20 +01:00
|
|
|
if (netdev_connect(device->netdev, bss, hs, device_netdev_event,
|
2016-06-14 18:19:05 +02:00
|
|
|
device_connect_cb, device) < 0) {
|
2016-12-12 18:34:19 +01:00
|
|
|
handshake_state_free(hs);
|
2016-09-14 16:50:08 +02:00
|
|
|
|
2016-11-02 23:46:17 +01:00
|
|
|
l_free(mde);
|
|
|
|
|
2016-06-14 18:19:05 +02:00
|
|
|
dbus_pending_reply(&device->connect_pending,
|
|
|
|
dbus_error_failed(device->connect_pending));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
device->connected_bss = bss;
|
|
|
|
device->connected_network = network;
|
|
|
|
|
|
|
|
device_enter_state(device, DEVICE_STATE_CONNECTING);
|
|
|
|
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "ConnectedNetwork");
|
|
|
|
l_dbus_property_changed(dbus, network_get_path(network),
|
|
|
|
IWD_NETWORK_INTERFACE, "Connected");
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
static void device_scan_triggered(int err, void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
struct l_dbus_message *reply;
|
2016-07-12 04:13:53 +02:00
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
l_debug("device_scan_triggered: %i", err);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
dbus_pending_reply(&device->scan_pending,
|
|
|
|
dbus_error_failed(device->scan_pending));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_debug("Scan triggered for %s", netdev_get_name(device->netdev));
|
|
|
|
|
2016-07-12 04:13:53 +02:00
|
|
|
device->scanning = true;
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Scanning");
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
reply = l_dbus_message_new_method_return(device->scan_pending);
|
|
|
|
l_dbus_message_set_arguments(reply, "");
|
|
|
|
dbus_pending_reply(&device->scan_pending, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_scan(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
|
|
|
l_debug("Scan called from DBus");
|
|
|
|
|
|
|
|
if (device->scan_pending)
|
|
|
|
return dbus_error_busy(message);
|
|
|
|
|
|
|
|
device->scan_pending = l_dbus_message_ref(message);
|
|
|
|
|
|
|
|
if (!scan_passive(device->index, device_scan_triggered,
|
|
|
|
new_scan_results, device, NULL))
|
|
|
|
return dbus_error_failed(message);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_disconnect_cb(struct netdev *netdev, bool success,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
2016-08-04 17:39:41 +02:00
|
|
|
l_debug("%d, success: %d", device->index, success);
|
|
|
|
|
2016-08-04 18:37:23 +02:00
|
|
|
if (device->disconnect_pending) {
|
|
|
|
struct l_dbus_message *reply;
|
2016-07-02 16:42:32 +02:00
|
|
|
|
2016-08-04 18:37:23 +02:00
|
|
|
if (success) {
|
|
|
|
reply = l_dbus_message_new_method_return(
|
|
|
|
device->disconnect_pending);
|
|
|
|
l_dbus_message_set_arguments(reply, "");
|
|
|
|
} else
|
|
|
|
reply = dbus_error_failed(device->disconnect_pending);
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
|
2016-08-04 18:37:23 +02:00
|
|
|
dbus_pending_reply(&device->disconnect_pending, reply);
|
2016-08-04 18:44:24 +02:00
|
|
|
|
2016-08-04 18:37:23 +02:00
|
|
|
}
|
2016-07-02 16:42:32 +02:00
|
|
|
|
2016-08-04 18:44:24 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_DISCONNECTED);
|
|
|
|
|
2016-09-22 18:57:15 +02:00
|
|
|
if (device->autoconnect)
|
2016-08-04 18:44:24 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_AUTOCONNECT);
|
2016-06-16 23:22:14 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 16:42:32 +02:00
|
|
|
int device_disconnect(struct device *device)
|
2016-06-16 23:22:14 +02:00
|
|
|
{
|
2016-07-02 16:42:31 +02:00
|
|
|
if (device->state == DEVICE_STATE_DISCONNECTING)
|
2016-07-02 16:42:32 +02:00
|
|
|
return -EBUSY;
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
if (!device->connected_bss)
|
2016-07-02 16:42:32 +02:00
|
|
|
return -ENOTCONN;
|
2016-06-16 23:22:14 +02:00
|
|
|
|
|
|
|
if (netdev_disconnect(device->netdev, device_disconnect_cb, device) < 0)
|
2016-07-02 16:42:32 +02:00
|
|
|
return -EIO;
|
2016-06-16 23:22:14 +02:00
|
|
|
|
2016-06-24 05:09:04 +02:00
|
|
|
/*
|
|
|
|
* If the disconnect somehow fails we won't know if we're still
|
|
|
|
* connected so we may as well indicate now that we're no longer
|
|
|
|
* connected.
|
|
|
|
*/
|
2016-12-23 11:38:08 +01:00
|
|
|
device_reset_connection_state(device);
|
2016-06-24 05:09:04 +02:00
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_DISCONNECTING);
|
|
|
|
|
2016-07-02 16:42:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_dbus_disconnect(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
2016-09-22 18:57:15 +02:00
|
|
|
/*
|
|
|
|
* Disconnect was triggered by the user, don't autoconnect. Wait for
|
|
|
|
* the user's explicit instructions to scan and connect to the network
|
|
|
|
*/
|
|
|
|
device_set_autoconnect(device, false);
|
2016-07-02 16:42:32 +02:00
|
|
|
|
2016-09-22 18:57:15 +02:00
|
|
|
result = device_disconnect(device);
|
2016-07-02 16:42:32 +02:00
|
|
|
if (result == -EBUSY)
|
|
|
|
return dbus_error_busy(message);
|
|
|
|
|
|
|
|
if (result == -ENOTCONN)
|
|
|
|
return dbus_error_not_connected(message);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
return dbus_error_failed(message);
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
device->disconnect_pending = l_dbus_message_ref(message);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_get_networks(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
struct l_dbus_message *reply;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
reply = l_dbus_message_new_method_return(message);
|
|
|
|
builder = l_dbus_message_builder_new(reply);
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_array(builder, "(osns)");
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(device->networks_sorted); entry;
|
|
|
|
entry = entry->next) {
|
|
|
|
const struct network *network = entry->data;
|
|
|
|
enum security security = network_get_security(network);
|
|
|
|
int32_t signal_strength = network_get_signal_strength(network);
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_struct(builder, "osns");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o',
|
|
|
|
network_get_path(network));
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's',
|
|
|
|
network_get_ssid(network));
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'n',
|
|
|
|
&signal_strength);
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's',
|
|
|
|
security_to_str(security));
|
|
|
|
l_dbus_message_builder_leave_struct(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
|
|
|
|
l_dbus_message_builder_finalize(builder);
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool device_property_get_name(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's',
|
|
|
|
netdev_get_name(device->netdev));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool device_property_get_address(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
str = util_address_to_string(netdev_get_address(device->netdev));
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', str);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool device_property_get_connected_network(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
if (!device->connected_network)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o',
|
|
|
|
network_get_path(device->connected_network));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-20 12:42:06 +02:00
|
|
|
static bool device_property_get_powered(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
2016-06-25 20:42:19 +02:00
|
|
|
bool powered = device->state != DEVICE_STATE_OFF;
|
2016-06-20 12:42:06 +02:00
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &powered);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-12 04:13:52 +02:00
|
|
|
struct set_powered_cb_data {
|
|
|
|
struct device *device;
|
|
|
|
struct l_dbus *dbus;
|
|
|
|
struct l_dbus_message *message;
|
|
|
|
l_dbus_property_complete_cb_t complete;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void set_powered_cb(struct netdev *netdev, int result, void *user_data)
|
|
|
|
{
|
|
|
|
struct set_powered_cb_data *cb_data = user_data;
|
|
|
|
struct l_dbus_message *reply = NULL;
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
reply = dbus_error_failed(cb_data->message);
|
|
|
|
|
|
|
|
cb_data->complete(cb_data->dbus, cb_data->message, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_property_set_powered(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_iter *new_value,
|
|
|
|
l_dbus_property_complete_cb_t complete,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
bool powered;
|
|
|
|
struct set_powered_cb_data *cb_data;
|
|
|
|
|
|
|
|
if (!l_dbus_message_iter_get_variant(new_value, "b", &powered))
|
|
|
|
return dbus_error_invalid_args(message);
|
|
|
|
|
|
|
|
if (powered == (device->state != DEVICE_STATE_OFF)) {
|
|
|
|
complete(dbus, message, NULL);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb_data = l_new(struct set_powered_cb_data, 1);
|
|
|
|
cb_data->device = device;
|
|
|
|
cb_data->dbus = dbus;
|
|
|
|
cb_data->message = message;
|
|
|
|
cb_data->complete = complete;
|
|
|
|
|
|
|
|
netdev_set_powered(device->netdev, powered, set_powered_cb, cb_data,
|
|
|
|
l_free);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-12 04:13:53 +02:00
|
|
|
static bool device_property_get_scanning(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
2016-09-22 19:57:10 +02:00
|
|
|
bool scanning = device->scanning;
|
2016-07-12 04:13:53 +02:00
|
|
|
|
2016-09-22 19:57:10 +02:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &scanning);
|
2016-07-12 04:13:53 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-20 00:48:39 +02:00
|
|
|
static bool device_property_get_state(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
const char *statestr = "unknown";
|
|
|
|
|
|
|
|
switch (device->state) {
|
|
|
|
case DEVICE_STATE_CONNECTED:
|
|
|
|
statestr = "connected";
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_CONNECTING:
|
|
|
|
statestr = "connecting";
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_DISCONNECTING:
|
|
|
|
statestr = "disconnecting";
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_OFF:
|
|
|
|
case DEVICE_STATE_DISCONNECTED:
|
|
|
|
case DEVICE_STATE_AUTOCONNECT:
|
|
|
|
statestr = "disconnected";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', statestr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:24:31 +01:00
|
|
|
static bool device_property_get_adapter(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
struct l_dbus_message_builder *builder,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o',
|
|
|
|
wiphy_get_path(device->wiphy));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
static void setup_device_interface(struct l_dbus_interface *interface)
|
|
|
|
{
|
|
|
|
l_dbus_interface_method(interface, "Scan", 0,
|
|
|
|
device_scan, "", "");
|
|
|
|
l_dbus_interface_method(interface, "Disconnect", 0,
|
2016-07-02 16:42:32 +02:00
|
|
|
device_dbus_disconnect, "", "");
|
2016-06-16 23:22:14 +02:00
|
|
|
l_dbus_interface_method(interface, "GetOrderedNetworks", 0,
|
|
|
|
device_get_networks, "a(osns)", "",
|
|
|
|
"networks");
|
|
|
|
|
|
|
|
l_dbus_interface_property(interface, "Name", 0, "s",
|
|
|
|
device_property_get_name, NULL);
|
|
|
|
l_dbus_interface_property(interface, "Address", 0, "s",
|
|
|
|
device_property_get_address, NULL);
|
|
|
|
l_dbus_interface_property(interface, "ConnectedNetwork", 0, "o",
|
|
|
|
device_property_get_connected_network,
|
|
|
|
NULL);
|
2016-06-20 12:42:06 +02:00
|
|
|
l_dbus_interface_property(interface, "Powered", 0, "b",
|
2016-07-12 04:13:52 +02:00
|
|
|
device_property_get_powered,
|
|
|
|
device_property_set_powered);
|
2016-07-12 04:13:53 +02:00
|
|
|
l_dbus_interface_property(interface, "Scanning", 0, "b",
|
|
|
|
device_property_get_scanning, NULL);
|
2016-07-20 00:48:39 +02:00
|
|
|
l_dbus_interface_property(interface, "State", 0, "s",
|
|
|
|
device_property_get_state, NULL);
|
2016-11-02 15:24:31 +01:00
|
|
|
l_dbus_interface_property(interface, "Adapter", 0, "o",
|
|
|
|
device_property_get_adapter, NULL);
|
2016-06-16 23:22:14 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 12:42:05 +02:00
|
|
|
static bool device_remove_network(const void *key, void *data, void *user_data)
|
|
|
|
{
|
|
|
|
struct network *network = data;
|
|
|
|
|
|
|
|
network_remove(network, -ESHUTDOWN);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:38:05 +02:00
|
|
|
static void device_netdev_notify(struct netdev *netdev,
|
|
|
|
enum netdev_watch_event event,
|
2016-06-20 12:42:05 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct device *device = user_data;
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
|
2016-07-20 01:03:21 +02:00
|
|
|
switch (event) {
|
|
|
|
case NETDEV_WATCH_EVENT_UP:
|
2016-06-20 12:42:05 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_AUTOCONNECT);
|
2016-06-25 20:42:20 +02:00
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
WATCHLIST_NOTIFY(&device_watches, device_watch_func_t,
|
|
|
|
device, DEVICE_EVENT_INSERTED);
|
2016-07-14 02:38:05 +02:00
|
|
|
|
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Powered");
|
2016-07-20 01:03:21 +02:00
|
|
|
break;
|
|
|
|
case NETDEV_WATCH_EVENT_DOWN:
|
2016-06-20 12:42:05 +02:00
|
|
|
device_enter_state(device, DEVICE_STATE_OFF);
|
|
|
|
|
|
|
|
if (device->scan_pending)
|
|
|
|
dbus_pending_reply(&device->scan_pending,
|
|
|
|
dbus_error_aborted(device->scan_pending));
|
|
|
|
|
|
|
|
if (device->connect_pending)
|
|
|
|
dbus_pending_reply(&device->connect_pending,
|
|
|
|
dbus_error_aborted(device->connect_pending));
|
|
|
|
|
2016-12-23 11:38:08 +01:00
|
|
|
device_reset_connection_state(device);
|
2016-06-20 12:42:05 +02:00
|
|
|
|
|
|
|
l_hashmap_foreach_remove(device->networks,
|
|
|
|
device_remove_network, device);
|
|
|
|
|
|
|
|
l_queue_destroy(device->autoconnect_list, l_free);
|
|
|
|
device->autoconnect_list = l_queue_new();
|
|
|
|
|
|
|
|
l_queue_destroy(device->bss_list, bss_free);
|
|
|
|
device->bss_list = l_queue_new();
|
|
|
|
|
|
|
|
l_queue_destroy(device->networks_sorted, NULL);
|
|
|
|
device->networks_sorted = l_queue_new();
|
2016-06-25 20:42:20 +02:00
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
WATCHLIST_NOTIFY(&device_watches, device_watch_func_t,
|
|
|
|
device, DEVICE_EVENT_REMOVED);
|
2016-06-20 12:42:06 +02:00
|
|
|
|
2016-07-14 02:38:05 +02:00
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Powered");
|
2016-07-20 01:03:21 +02:00
|
|
|
break;
|
|
|
|
case NETDEV_WATCH_EVENT_NAME_CHANGE:
|
2016-07-14 02:38:06 +02:00
|
|
|
l_dbus_property_changed(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, "Name");
|
2016-07-20 01:03:21 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-06-20 12:42:05 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
struct device *device_create(struct wiphy *wiphy, struct netdev *netdev)
|
|
|
|
{
|
|
|
|
struct device *device;
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
uint32_t ifindex = netdev_get_ifindex(netdev);
|
|
|
|
|
|
|
|
device = l_new(struct device, 1);
|
|
|
|
device->bss_list = l_queue_new();
|
|
|
|
device->networks = l_hashmap_new();
|
2016-09-14 16:09:44 +02:00
|
|
|
watchlist_init(&device->state_watches);
|
2016-06-16 22:37:50 +02:00
|
|
|
l_hashmap_set_hash_function(device->networks, l_str_hash);
|
|
|
|
l_hashmap_set_compare_function(device->networks,
|
|
|
|
(l_hashmap_compare_func_t) strcmp);
|
|
|
|
device->networks_sorted = l_queue_new();
|
|
|
|
device->index = ifindex;
|
|
|
|
device->wiphy = wiphy;
|
|
|
|
device->netdev = netdev;
|
2016-09-22 18:57:15 +02:00
|
|
|
device->autoconnect = true;
|
2016-06-16 22:37:50 +02:00
|
|
|
|
|
|
|
l_queue_push_head(device_list, device);
|
|
|
|
|
|
|
|
if (!l_dbus_object_add_interface(dbus, device_get_path(device),
|
|
|
|
IWD_DEVICE_INTERFACE, device))
|
|
|
|
l_info("Unable to register %s interface", IWD_DEVICE_INTERFACE);
|
|
|
|
|
2016-09-21 18:51:55 +02:00
|
|
|
if (!l_dbus_object_add_interface(dbus, device_get_path(device),
|
|
|
|
L_DBUS_INTERFACE_PROPERTIES, device))
|
|
|
|
l_info("Unable to register %s interface",
|
|
|
|
L_DBUS_INTERFACE_PROPERTIES);
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
scan_ifindex_add(device->index);
|
2016-06-20 12:42:05 +02:00
|
|
|
|
2016-07-14 02:38:05 +02:00
|
|
|
device_netdev_notify(netdev, netdev_get_is_up(netdev) ?
|
2016-07-20 01:03:21 +02:00
|
|
|
NETDEV_WATCH_EVENT_UP :
|
|
|
|
NETDEV_WATCH_EVENT_DOWN,
|
|
|
|
device);
|
2016-07-13 04:29:51 +02:00
|
|
|
device->netdev_watch_id =
|
|
|
|
netdev_watch_add(netdev, device_netdev_notify, device);
|
2016-06-16 22:37:50 +02:00
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_free(void *user)
|
|
|
|
{
|
|
|
|
struct device *device = user;
|
|
|
|
struct l_dbus *dbus;
|
|
|
|
|
|
|
|
l_debug("");
|
|
|
|
|
|
|
|
if (device->scan_pending)
|
|
|
|
dbus_pending_reply(&device->scan_pending,
|
|
|
|
dbus_error_aborted(device->scan_pending));
|
|
|
|
|
|
|
|
if (device->connect_pending)
|
|
|
|
dbus_pending_reply(&device->connect_pending,
|
|
|
|
dbus_error_aborted(device->connect_pending));
|
|
|
|
|
2016-06-25 20:42:20 +02:00
|
|
|
if (device->state != DEVICE_STATE_OFF)
|
2016-09-14 16:31:11 +02:00
|
|
|
WATCHLIST_NOTIFY(&device_watches, device_watch_func_t,
|
|
|
|
device, DEVICE_EVENT_REMOVED);
|
2016-06-16 22:37:50 +02:00
|
|
|
|
2016-09-22 22:28:56 +02:00
|
|
|
watchlist_destroy(&device->state_watches);
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
dbus = dbus_get_bus();
|
|
|
|
l_dbus_unregister_object(dbus, device_get_path(device));
|
|
|
|
|
|
|
|
l_queue_destroy(device->networks_sorted, NULL);
|
|
|
|
l_hashmap_destroy(device->networks, network_free);
|
|
|
|
|
|
|
|
l_queue_destroy(device->bss_list, bss_free);
|
|
|
|
l_queue_destroy(device->old_bss_list, bss_free);
|
|
|
|
l_queue_destroy(device->autoconnect_list, l_free);
|
|
|
|
|
2016-07-13 04:29:51 +02:00
|
|
|
netdev_watch_remove(device->netdev, device->netdev_watch_id);
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
scan_ifindex_remove(device->index);
|
|
|
|
l_free(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void device_remove(struct device *device)
|
|
|
|
{
|
|
|
|
if (!l_queue_remove(device_list, device))
|
|
|
|
return;
|
|
|
|
|
|
|
|
device_free(device);
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:13:45 +02:00
|
|
|
bool device_init(void)
|
|
|
|
{
|
2016-06-16 23:22:14 +02:00
|
|
|
if (!l_dbus_register_interface(dbus_get_bus(),
|
|
|
|
IWD_DEVICE_INTERFACE,
|
|
|
|
setup_device_interface,
|
2016-09-21 18:51:55 +02:00
|
|
|
NULL, false))
|
2016-06-16 23:22:14 +02:00
|
|
|
return false;
|
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
watchlist_init(&device_watches);
|
2016-06-16 22:37:50 +02:00
|
|
|
device_list = l_queue_new();
|
2016-05-05 19:13:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool device_exit(void)
|
|
|
|
{
|
2016-07-19 23:49:37 +02:00
|
|
|
if (!l_queue_isempty(device_list))
|
|
|
|
l_warn("device_list isn't empty!");
|
|
|
|
|
2016-06-16 22:37:50 +02:00
|
|
|
l_queue_destroy(device_list, device_free);
|
|
|
|
device_list = NULL;
|
|
|
|
|
2016-09-14 16:31:11 +02:00
|
|
|
watchlist_destroy(&device_watches);
|
2016-05-05 19:13:45 +02:00
|
|
|
|
2016-06-16 23:22:14 +02:00
|
|
|
l_dbus_unregister_interface(dbus_get_bus(), IWD_DEVICE_INTERFACE);
|
|
|
|
|
2016-05-05 19:13:45 +02:00
|
|
|
return true;
|
|
|
|
}
|