iwd/src/wiphy.c

998 lines
24 KiB
C
Raw Normal View History

2014-07-29 21:25:01 +02:00
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2013-2014 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
#include <stdlib.h>
2014-10-23 21:32:12 +02:00
#include <stdio.h>
#include <errno.h>
#include <linux/if_ether.h>
2016-06-10 11:50:22 +02:00
#include <time.h>
2014-07-29 21:25:01 +02:00
#include <ell/ell.h>
#include "linux/nl80211.h"
2014-10-30 06:26:49 +01:00
#include "src/iwd.h"
#include "src/ie.h"
2014-07-29 21:25:01 +02:00
#include "src/wiphy.h"
2014-10-23 21:32:12 +02:00
#include "src/dbus.h"
#include "src/scan.h"
#include "src/util.h"
#include "src/common.h"
#include "src/eapol.h"
#include "src/agent.h"
#include "src/crypto.h"
#include "src/netdev.h"
2015-03-30 03:27:57 +02:00
#include "src/mpdu.h"
#include "src/storage.h"
#include "src/network.h"
#include "src/device.h"
2014-10-23 21:32:12 +02:00
2014-07-29 21:25:01 +02:00
static struct l_genl_family *nl80211 = NULL;
struct wiphy {
uint32_t id;
char name[20];
uint32_t feature_flags;
2015-04-17 19:27:02 +02:00
bool support_scheduled_scan:1;
bool support_rekey_offload:1;
uint16_t pairwise_ciphers;
struct scan_freq_set *supported_freqs;
};
struct autoconnect_entry {
uint16_t rank;
struct network *network;
struct scan_bss *bss;
};
static struct l_queue *wiphy_list = NULL;
static bool new_scan_results(uint32_t wiphy_id, uint32_t ifindex,
struct l_queue *bss_list, void *userdata);
2016-05-31 19:57:24 +02:00
static const char *iwd_network_get_path(struct device *device,
const uint8_t *ssid, size_t ssid_len,
enum security security)
{
static char path[256];
unsigned int pos, i;
2016-05-31 19:57:24 +02:00
pos = snprintf(path, sizeof(path), "%s/", device_get_path(device));
for (i = 0; i < ssid_len && pos < sizeof(path); i++)
pos += snprintf(path + pos, sizeof(path) - pos, "%02x",
ssid[i]);
snprintf(path + pos, sizeof(path) - pos, "_%s",
security_to_str(security));
2014-10-23 22:09:52 +02:00
return path;
}
static const char *device_state_to_string(enum device_state state)
2015-06-23 00:02:52 +02:00
{
switch (state) {
case DEVICE_STATE_DISCONNECTED:
2015-06-23 00:02:52 +02:00
return "disconnected";
case DEVICE_STATE_AUTOCONNECT:
2015-06-23 00:02:52 +02:00
return "autoconnect";
case DEVICE_STATE_CONNECTING:
2015-06-23 00:02:52 +02:00
return "connecting";
case DEVICE_STATE_CONNECTED:
2015-06-23 00:02:52 +02:00
return "connected";
case DEVICE_STATE_DISCONNECTING:
2015-06-23 00:02:52 +02:00
return "disconnecting";
}
return "invalid";
}
2016-06-14 18:13:34 +02:00
void device_enter_state(struct device *device, enum device_state state)
2015-06-23 00:02:52 +02:00
{
l_debug("Old State: %s, new state: %s",
2016-05-31 19:57:24 +02:00
device_state_to_string(device->state),
device_state_to_string(state));
2015-06-23 00:02:52 +02:00
switch (state) {
case DEVICE_STATE_AUTOCONNECT:
2016-05-31 19:57:24 +02:00
scan_periodic_start(device->index, new_scan_results, device);
2015-06-23 00:02:52 +02:00
break;
case DEVICE_STATE_DISCONNECTED:
2016-05-31 19:57:24 +02:00
scan_periodic_stop(device->index);
2015-06-23 00:02:52 +02:00
break;
case DEVICE_STATE_CONNECTED:
2016-05-31 19:57:24 +02:00
scan_periodic_stop(device->index);
2015-06-23 00:02:52 +02:00
break;
case DEVICE_STATE_CONNECTING:
2015-06-23 00:02:52 +02:00
break;
case DEVICE_STATE_DISCONNECTING:
2015-06-23 00:02:52 +02:00
break;
}
2016-05-31 19:57:24 +02:00
device->state = state;
2015-06-23 00:02:52 +02:00
}
2016-05-16 19:21:26 +02:00
enum ie_rsn_cipher_suite wiphy_select_cipher(struct wiphy *wiphy, uint16_t mask)
{
mask &= wiphy->pairwise_ciphers;
/* CCMP is our first choice, TKIP second */
if (mask & IE_RSN_CIPHER_SUITE_CCMP)
return IE_RSN_CIPHER_SUITE_CCMP;
if (mask & IE_RSN_CIPHER_SUITE_TKIP)
return IE_RSN_CIPHER_SUITE_TKIP;
return 0;
}
static void bss_free(void *data)
{
struct scan_bss *bss = data;
2016-06-07 01:25:00 +02:00
const char *addr;
2016-06-07 01:25:00 +02:00
addr = util_address_to_string(bss->addr);
l_debug("Freeing BSS %s", addr);
2015-05-01 05:21:31 +02:00
scan_bss_free(bss);
}
static void device_scan_triggered(int err, void *user_data)
2014-10-28 05:43:21 +01:00
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
2014-10-28 05:43:21 +01:00
struct l_dbus_message *reply;
l_debug("device_scan_triggered: %i", err);
if (err < 0) {
2016-05-31 19:57:24 +02:00
dbus_pending_reply(&device->scan_pending,
dbus_error_failed(device->scan_pending));
2014-10-28 05:43:21 +01:00
return;
}
2016-06-02 04:09:22 +02:00
l_debug("Scan triggered for %s", netdev_get_name(device->netdev));
2014-10-28 05:43:21 +01:00
2016-05-31 19:57:24 +02:00
reply = l_dbus_message_new_method_return(device->scan_pending);
2014-10-28 05:43:21 +01:00
l_dbus_message_set_arguments(reply, "");
2016-05-31 19:57:24 +02:00
dbus_pending_reply(&device->scan_pending, reply);
2014-10-28 05:43:21 +01:00
}
static struct l_dbus_message *device_scan(struct l_dbus *dbus,
struct l_dbus_message *message,
void *user_data)
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
2014-10-28 05:43:21 +01:00
l_debug("Scan called from DBus");
2016-05-31 19:57:24 +02:00
if (device->scan_pending)
2014-10-28 05:43:21 +01:00
return dbus_error_busy(message);
2016-05-31 19:57:24 +02:00
device->scan_pending = l_dbus_message_ref(message);
2014-10-28 05:43:21 +01:00
2016-05-31 19:57:24 +02:00
if (!scan_passive(device->index, device_scan_triggered,
new_scan_results, device, NULL))
return dbus_error_failed(message);
2014-10-28 05:43:21 +01:00
return NULL;
}
2015-09-28 18:32:26 +02:00
static void device_disconnect_cb(struct l_genl_msg *msg, void *user_data)
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
struct l_dbus_message *reply;
if (l_genl_msg_get_error(msg) < 0) {
2016-05-31 19:57:24 +02:00
dbus_pending_reply(&device->disconnect_pending,
dbus_error_failed(device->disconnect_pending));
return;
}
2016-05-31 19:57:24 +02:00
device_disassociated(device);
2016-05-31 19:57:24 +02:00
reply = l_dbus_message_new_method_return(device->disconnect_pending);
l_dbus_message_set_arguments(reply, "");
2016-05-31 19:57:24 +02:00
dbus_pending_reply(&device->disconnect_pending, reply);
}
static struct l_dbus_message *device_disconnect(struct l_dbus *dbus,
struct l_dbus_message *message,
void *user_data)
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
struct l_genl_msg *msg;
2015-03-30 03:27:57 +02:00
uint16_t reason_code = MPDU_REASON_CODE_DEAUTH_LEAVING;
2016-05-12 05:11:08 +02:00
enum security security;
l_debug("");
2016-05-31 19:57:24 +02:00
if (device->state == DEVICE_STATE_CONNECTING ||
device->state == DEVICE_STATE_DISCONNECTING)
return dbus_error_busy(message);
2016-05-31 19:57:24 +02:00
if (!device->connected_bss)
return dbus_error_not_connected(message);
2016-05-31 19:57:24 +02:00
security = network_get_security(device->connected_network);
2016-05-12 05:11:08 +02:00
if (security == SECURITY_PSK || security == SECURITY_8021X)
2016-05-31 19:57:24 +02:00
eapol_cancel(device->index);
msg = l_genl_msg_new_sized(NL80211_CMD_DEAUTHENTICATE, 512);
2016-05-31 19:57:24 +02:00
msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &device->index);
msg_append_attr(msg, NL80211_ATTR_REASON_CODE, 2, &reason_code);
msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN,
2016-05-31 19:57:24 +02:00
device->connected_bss->addr);
l_genl_family_send(nl80211, msg, device_disconnect_cb, device, NULL);
2016-05-31 19:57:24 +02:00
device_enter_state(device, DEVICE_STATE_DISCONNECTING);
2015-06-23 00:02:52 +02:00
2016-05-31 19:57:24 +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;
}
2016-02-11 23:19:55 +01:00
static bool device_property_get_name(struct l_dbus *dbus,
struct l_dbus_message *message,
struct l_dbus_message_builder *builder,
void *user_data)
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
2016-02-11 23:19:55 +01:00
2016-06-02 04:09:22 +02:00
l_dbus_message_builder_append_basic(builder, 's',
netdev_get_name(device->netdev));
2016-02-11 23:19:55 +01:00
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)
{
2016-05-31 19:57:24 +02:00
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)
{
2016-05-31 19:57:24 +02:00
struct device *device = user_data;
if (!device->connected_network)
return false;
l_dbus_message_builder_append_basic(builder, 'o',
2016-05-31 19:57:24 +02:00
network_get_path(device->connected_network));
return true;
}
2014-10-23 21:32:12 +02:00
static void setup_device_interface(struct l_dbus_interface *interface)
{
2014-10-28 05:43:21 +01:00
l_dbus_interface_method(interface, "Scan", 0,
device_scan, "", "");
l_dbus_interface_method(interface, "Disconnect", 0,
device_disconnect, "", "");
l_dbus_interface_method(interface, "GetOrderedNetworks", 0,
device_get_networks, "a(osns)", "",
"networks");
2014-10-23 21:32:12 +02:00
2016-02-11 23:19:55 +01:00
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);
2014-10-23 21:32:12 +02:00
}
2014-10-30 04:50:27 +01:00
static bool bss_match(const void *a, const void *b)
{
const struct scan_bss *bss_a = a;
const struct scan_bss *bss_b = b;
2014-10-30 04:50:27 +01:00
return !memcmp(bss_a->addr, bss_b->addr, sizeof(bss_a->addr));
}
2016-05-31 19:57:24 +02:00
static void device_autoconnect_next(struct device *device)
2015-06-23 01:33:55 +02:00
{
struct autoconnect_entry *entry;
2016-05-16 21:24:47 +02:00
int r;
2015-06-23 01:33:55 +02:00
2016-05-31 19:57:24 +02:00
while ((entry = l_queue_pop_head(device->autoconnect_list))) {
2015-06-23 01:33:55 +02:00
l_debug("Considering autoconnecting to BSS '%s' with SSID: %s,"
" freq: %u, rank: %u, strength: %i",
2016-06-07 01:25:00 +02:00
util_address_to_string(entry->bss->addr),
2016-05-16 21:23:48 +02:00
network_get_ssid(entry->network),
2015-06-23 01:33:55 +02:00
entry->bss->frequency, entry->rank,
entry->bss->signal_strength);
2016-05-16 21:24:47 +02:00
/* TODO: Blacklist the network from auto-connect */
r = network_autoconnect(entry->network, entry->bss);
2015-06-23 01:33:55 +02:00
l_free(entry);
2016-05-16 21:24:47 +02:00
if (!r)
2015-06-23 01:33:55 +02:00
return;
}
}
static void wiphy_free(void *data)
{
struct wiphy *wiphy = data;
l_debug("Freeing wiphy %s", wiphy->name);
scan_freq_set_free(wiphy->supported_freqs);
l_free(wiphy);
}
static bool wiphy_match(const void *a, const void *b)
{
const struct wiphy *wiphy = a;
uint32_t id = L_PTR_TO_UINT(b);
return (wiphy->id == id);
}
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);
2016-05-16 23:14:15 +02:00
return false;
}
/* Drop networks that have no more BSSs in range */
l_debug("No remaining BSSs for SSID: %s -- Removing network",
2016-05-16 21:23:48 +02:00
network_get_ssid(network));
network_remove(network, -ERANGE);
return true;
}
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;
}
2016-06-10 11:50:22 +02:00
static void process_bss(struct device *device, struct scan_bss *bss,
struct timespec *timestamp)
{
2015-02-26 17:03:51 +01:00
struct network *network;
enum security security;
const char *path;
double rankmod;
struct autoconnect_entry *entry;
2014-10-28 17:04:31 +01:00
l_debug("Found BSS '%s' with SSID: %s, freq: %u, rank: %u, "
"strength: %i",
2016-06-07 01:25:00 +02:00
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;
}
/*
* If both an RSN and a WPA elements are present currently
* RSN takes priority and the WPA IE is ignored.
*/
if (bss->rsne) {
struct ie_rsn_info rsne;
int res = ie_parse_rsne_from_data(bss->rsne, bss->rsne[1] + 2,
&rsne);
if (res < 0) {
l_debug("Cannot parse RSN field (%d, %s)",
res, strerror(-res));
return;
}
security = scan_get_security(bss->capability, &rsne);
if (security == SECURITY_PSK)
bss->sha256 =
rsne.akm_suites & IE_RSN_AKM_SUITE_PSK_SHA256;
else if (security == SECURITY_8021X)
bss->sha256 =
rsne.akm_suites & IE_RSN_AKM_SUITE_8021X_SHA256;
} else if (bss->wpa) {
struct ie_rsn_info wpa;
int res = ie_parse_wpa_from_data(bss->wpa, bss->wpa[1] + 2,
&wpa);
if (res < 0) {
l_debug("Cannot parse WPA IE (%d, %s)",
res, strerror(-res));
return;
}
security = scan_get_security(bss->capability, &wpa);
} else
security = scan_get_security(bss->capability, NULL);
2015-02-25 06:11:48 +01:00
2016-05-31 19:57:24 +02:00
path = iwd_network_get_path(device, bss->ssid, bss->ssid_len,
security);
2016-05-31 19:57:24 +02:00
network = l_hashmap_lookup(device->networks, path);
2015-01-28 22:37:25 +01:00
if (!network) {
2016-05-31 19:57:24 +02:00
network = network_create(device, bss->ssid, bss->ssid_len,
security);
2015-01-28 22:37:25 +01:00
if (!network_register(network, path)) {
network_remove(network, -EINVAL);
return;
}
2016-05-31 19:57:24 +02:00
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));
2015-01-28 22:37:25 +01:00
}
2016-06-10 11:50:22 +02:00
if (network_bss_list_isempty(network))
network_seen(network, timestamp);
2016-05-16 23:14:15 +02:00
network_bss_add(network, bss);
/* 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;
2016-05-31 19:57:24 +02:00
l_queue_insert(device->autoconnect_list, entry,
autoconnect_rank_compare, NULL);
}
static bool new_scan_results(uint32_t wiphy_id, uint32_t ifindex,
struct l_queue *bss_list, void *userdata)
{
2016-05-31 19:57:24 +02:00
struct device *device = userdata;
struct network *network;
const struct l_queue_entry *bss_entry;
2016-06-10 11:50:22 +02:00
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
2016-05-31 19:57:24 +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);
2016-05-31 19:57:24 +02:00
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;
2016-06-10 11:50:22 +02:00
process_bss(device, bss, &now);
}
2014-10-30 04:50:27 +01:00
l_hashmap_foreach_remove(device->networks, process_network, device);
2016-05-31 19:57:24 +02:00
if (device->connected_bss) {
struct scan_bss *bss;
2016-05-31 19:57:24 +02:00
bss = l_queue_find(device->bss_list, bss_match,
device->connected_bss);
if (!bss) {
l_warn("Connected BSS not in scan results!");
2016-05-31 19:57:24 +02:00
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
2016-05-31 19:57:24 +02:00
device->connected_bss = bss;
}
2016-05-31 19:57:24 +02:00
l_queue_destroy(device->old_bss_list, bss_free);
device->old_bss_list = NULL;
2016-05-31 19:57:24 +02:00
if (device->state == DEVICE_STATE_AUTOCONNECT)
device_autoconnect_next(device);
2015-06-23 01:33:55 +02:00
return true;
2014-10-28 17:14:40 +01:00
}
2016-06-01 22:20:33 +02:00
struct wiphy *wiphy_find(int wiphy_id)
{
return l_queue_find(wiphy_list, wiphy_match, L_UINT_TO_PTR(wiphy_id));
}
static void parse_supported_commands(struct wiphy *wiphy,
struct l_genl_attr *attr)
{
uint16_t type, len;
const void *data;
while (l_genl_attr_next(attr, &type, &len, &data)) {
uint32_t cmd = *(uint32_t *)data;
switch (cmd) {
case NL80211_CMD_START_SCHED_SCAN:
wiphy->support_scheduled_scan = true;
break;
case NL80211_CMD_SET_REKEY_OFFLOAD:
wiphy->support_rekey_offload = true;
}
}
}
static void parse_supported_ciphers(struct wiphy *wiphy, const void *data,
uint16_t len)
{
bool s;
while (len >= 4) {
uint32_t cipher = *(uint32_t *)data;
switch (cipher) {
case CRYPTO_CIPHER_CCMP:
wiphy->pairwise_ciphers |= IE_RSN_CIPHER_SUITE_CCMP;
break;
case CRYPTO_CIPHER_TKIP:
wiphy->pairwise_ciphers |= IE_RSN_CIPHER_SUITE_TKIP;
break;
case CRYPTO_CIPHER_WEP40:
wiphy->pairwise_ciphers |= IE_RSN_CIPHER_SUITE_WEP40;
break;
case CRYPTO_CIPHER_WEP104:
wiphy->pairwise_ciphers |= IE_RSN_CIPHER_SUITE_WEP104;
break;
case CRYPTO_CIPHER_BIP:
wiphy->pairwise_ciphers |= IE_RSN_CIPHER_SUITE_BIP;
break;
default: /* TODO: Support other ciphers */
break;
}
len -= 4;
data += 4;
}
s = wiphy->pairwise_ciphers & IE_RSN_CIPHER_SUITE_CCMP;
l_info("Wiphy supports CCMP: %s", s ? "true" : "false");
s = wiphy->pairwise_ciphers & IE_RSN_CIPHER_SUITE_TKIP;
l_info("Wiphy supports TKIP: %s", s ? "true" : "false");
}
static void parse_supported_frequencies(struct wiphy *wiphy,
struct l_genl_attr *freqs)
{
uint16_t type, len;
const void *data;
struct l_genl_attr attr;
l_debug("");
while (l_genl_attr_next(freqs, NULL, NULL, NULL)) {
if (!l_genl_attr_recurse(freqs, &attr))
continue;
while (l_genl_attr_next(&attr, &type, &len, &data)) {
uint32_t u32;
switch (type) {
case NL80211_FREQUENCY_ATTR_FREQ:
u32 = *((uint32_t *) data);
scan_freq_set_add(wiphy->supported_freqs, u32);
break;
}
}
}
}
static void parse_supported_bands(struct wiphy *wiphy,
struct l_genl_attr *bands)
{
uint16_t type, len;
const void *data;
struct l_genl_attr attr;
l_debug("");
while (l_genl_attr_next(bands, NULL, NULL, NULL)) {
if (!l_genl_attr_recurse(bands, &attr))
continue;
while (l_genl_attr_next(&attr, &type, &len, &data)) {
struct l_genl_attr freqs;
switch (type) {
case NL80211_BAND_ATTR_FREQS:
if (!l_genl_attr_recurse(&attr, &freqs))
continue;
parse_supported_frequencies(wiphy, &freqs);
break;
}
}
}
}
#define FAIL_NO_WIPHY() \
if (!wiphy) { \
l_warn("No wiphy structure found"); \
return; \
} \
static void wiphy_dump_callback(struct l_genl_msg *msg, void *user_data)
{
struct wiphy *wiphy = NULL;
struct l_genl_attr attr, nested;
uint16_t type, len;
const void *data;
uint32_t id;
if (!l_genl_attr_init(&attr, msg))
return;
/*
* The wiphy attribute is always the first attribute in the
* list. If not then error out with a warning and ignore the
* whole message.
*
* In most cases multiple of these message will be send
* since the information included can not fit into a single
* message.
*/
while (l_genl_attr_next(&attr, &type, &len, &data)) {
switch (type) {
case NL80211_ATTR_WIPHY:
if (wiphy) {
l_warn("Duplicate wiphy attribute");
return;
}
if (len != sizeof(uint32_t)) {
l_warn("Invalid wiphy attribute");
return;
}
id = *((uint32_t *) data);
wiphy = l_queue_find(wiphy_list, wiphy_match,
L_UINT_TO_PTR(id));
if (!wiphy) {
wiphy = l_new(struct wiphy, 1);
wiphy->id = id;
wiphy->supported_freqs = scan_freq_set_new();
l_queue_push_head(wiphy_list, wiphy);
}
break;
case NL80211_ATTR_WIPHY_NAME:
FAIL_NO_WIPHY();
if (len > sizeof(wiphy->name)) {
l_warn("Invalid wiphy name attribute");
return;
}
memcpy(wiphy->name, data, len);
break;
case NL80211_ATTR_FEATURE_FLAGS:
FAIL_NO_WIPHY();
if (len != sizeof(uint32_t)) {
l_warn("Invalid feature flags attribute");
return;
}
wiphy->feature_flags = *((uint32_t *) data);
break;
case NL80211_ATTR_SUPPORTED_COMMANDS:
FAIL_NO_WIPHY();
if (!l_genl_attr_recurse(&attr, &nested))
return;
parse_supported_commands(wiphy, &nested);
break;
case NL80211_ATTR_CIPHER_SUITES:
FAIL_NO_WIPHY();
parse_supported_ciphers(wiphy, data, len);
break;
case NL80211_ATTR_WIPHY_BANDS:
FAIL_NO_WIPHY();
if (!l_genl_attr_recurse(&attr, &nested))
return;
parse_supported_bands(wiphy, &nested);
break;
}
}
}
static void wiphy_dump_done(void *user)
{
const struct l_queue_entry *wiphy_entry;
for (wiphy_entry = l_queue_get_entries(wiphy_list); wiphy_entry;
wiphy_entry = wiphy_entry->next) {
struct wiphy *wiphy = wiphy_entry->data;
uint32_t bands;
l_info("Wiphy: %d, Name: %s", wiphy->id, wiphy->name);
l_info("Bands:");
bands = scan_freq_set_get_bands(wiphy->supported_freqs);
if (bands & SCAN_BAND_2_4_GHZ)
l_info("\t2.4 Ghz");
if (bands & SCAN_BAND_5_GHZ)
l_info("\t5.0 Ghz");
}
}
static void wiphy_config_notify(struct l_genl_msg *msg, void *user_data)
{
struct l_genl_attr attr;
uint16_t type, len;
const void *data;
uint8_t cmd;
cmd = l_genl_msg_get_command(msg);
l_debug("Notification of command %u", cmd);
if (!l_genl_attr_init(&attr, msg))
return;
switch (cmd) {
case NL80211_CMD_NEW_WIPHY:
case NL80211_CMD_DEL_WIPHY:
{
const uint32_t *wiphy_id = NULL;
const char *wiphy_name = NULL;
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;
}
wiphy_id = data;
break;
case NL80211_ATTR_WIPHY_NAME:
wiphy_name = data;
break;
}
}
if (!wiphy_id)
return;
if (cmd == NL80211_CMD_NEW_WIPHY)
l_info("New Wiphy %s[%d] added", wiphy_name, *wiphy_id);
else
l_info("Wiphy %s[%d] removed", wiphy_name, *wiphy_id);
break;
}
}
}
static void wiphy_regulatory_notify(struct l_genl_msg *msg, void *user_data)
{
struct l_genl_attr attr;
uint16_t type, len;
const void *data;
uint8_t cmd;
cmd = l_genl_msg_get_command(msg);
l_debug("Regulatory notification %u", cmd);
if (!l_genl_attr_init(&attr, msg))
return;
while (l_genl_attr_next(&attr, &type, &len, &data)) {
}
}
static void regulatory_info_callback(struct l_genl_msg *msg, void *user_data)
{
struct l_genl_attr attr;
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_REG_ALPHA2:
if (len != 3) {
l_warn("Invalid regulatory alpha2 attribute");
return;
}
l_debug("Regulatory alpha2 is %s", (char *) data);
break;
}
}
}
static void protocol_features_callback(struct l_genl_msg *msg, void *user_data)
{
struct l_genl_attr attr;
uint16_t type, len;
const void *data;
uint32_t features = 0;
if (!l_genl_attr_init(&attr, msg))
return;
while (l_genl_attr_next(&attr, &type, &len, &data)) {
switch (type) {
case NL80211_ATTR_PROTOCOL_FEATURES:
if (len != sizeof(uint32_t)) {
l_warn("Invalid protocol features attribute");
return;
}
features = *((uint32_t *) data);
break;
}
}
if (features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
l_debug("Found split wiphy dump support");
}
2015-09-29 02:51:40 +02:00
bool wiphy_init(struct l_genl_family *in)
2014-07-29 21:25:01 +02:00
{
struct l_genl_msg *msg;
/*
* This is an extra sanity check so that no memory is leaked
* in case the generic netlink handling gets confused.
*/
if (wiphy_list) {
l_warn("Destroying existing list of wiphy devices");
l_queue_destroy(wiphy_list, NULL);
}
2016-02-11 23:19:55 +01:00
if (!l_dbus_register_interface(dbus_get_bus(),
IWD_DEVICE_INTERFACE,
setup_device_interface,
NULL, true))
2016-02-11 23:19:55 +01:00
return false;
2015-09-29 02:51:40 +02:00
nl80211 = in;
if (!l_genl_family_register(nl80211, "config", wiphy_config_notify,
NULL, NULL))
l_error("Registering for config notification failed");
if (!l_genl_family_register(nl80211, "regulatory",
wiphy_regulatory_notify, NULL, NULL))
l_error("Registering for regulatory notification failed");
wiphy_list = l_queue_new();
msg = l_genl_msg_new(NL80211_CMD_GET_PROTOCOL_FEATURES);
if (!l_genl_family_send(nl80211, msg, protocol_features_callback,
NULL, NULL))
l_error("Getting protocol features failed");
msg = l_genl_msg_new(NL80211_CMD_GET_REG);
if (!l_genl_family_send(nl80211, msg, regulatory_info_callback,
NULL, NULL))
l_error("Getting regulatory info failed");
2014-07-29 21:25:01 +02:00
msg = l_genl_msg_new(NL80211_CMD_GET_WIPHY);
if (!l_genl_family_dump(nl80211, msg, wiphy_dump_callback,
NULL, wiphy_dump_done))
l_error("Getting all wiphy devices failed");
2014-07-29 21:25:01 +02:00
return true;
}
bool wiphy_exit(void)
{
2015-09-29 02:51:40 +02:00
l_queue_destroy(wiphy_list, wiphy_free);
wiphy_list = NULL;
2014-07-29 21:25:01 +02:00
2015-09-29 02:51:40 +02:00
nl80211 = NULL;
2016-02-11 23:19:55 +01:00
l_dbus_unregister_interface(dbus_get_bus(), IWD_DEVICE_INTERFACE);
2014-07-29 21:25:01 +02:00
return true;
}