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
|
|
|
|
|
2014-08-07 01:06:51 +02:00
|
|
|
#include <stdlib.h>
|
2014-10-23 21:32:12 +02:00
|
|
|
#include <stdio.h>
|
2014-08-07 05:15:20 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_ether.h>
|
2014-07-29 21:25:01 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "linux/nl80211.h"
|
2014-08-09 08:23:56 +02:00
|
|
|
#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"
|
|
|
|
|
2014-08-09 07:19:47 +02:00
|
|
|
static const char *network_ssid = NULL;
|
|
|
|
|
2014-07-29 21:25:01 +02:00
|
|
|
static struct l_genl *genl = NULL;
|
|
|
|
static struct l_genl_family *nl80211 = NULL;
|
|
|
|
|
2014-08-09 08:23:56 +02:00
|
|
|
struct bss {
|
|
|
|
uint8_t addr[ETH_ALEN];
|
|
|
|
uint32_t frequency;
|
|
|
|
char *ssid;
|
|
|
|
};
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
struct netdev {
|
|
|
|
uint32_t index;
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
uint32_t type;
|
|
|
|
uint8_t addr[ETH_ALEN];
|
2014-08-09 08:23:56 +02:00
|
|
|
struct l_queue *bss_list;
|
2014-08-07 05:15:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wiphy {
|
|
|
|
uint32_t id;
|
|
|
|
char name[20];
|
2014-08-08 00:41:30 +02:00
|
|
|
uint32_t feature_flags;
|
2014-08-07 05:15:20 +02:00
|
|
|
struct l_queue *netdev_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct l_queue *wiphy_list = NULL;
|
|
|
|
|
2014-07-29 21:25:01 +02:00
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
2014-10-23 22:09:52 +02:00
|
|
|
static const char *device_get_path(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
static char path[256];
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/%u", netdev->index);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:32:12 +02:00
|
|
|
static void device_emit_added(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_emit_removed(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_set_property(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const char *property;
|
|
|
|
struct l_dbus_message_iter variant;
|
|
|
|
|
|
|
|
if (!l_dbus_message_get_arguments(message, "sv", &property, &variant))
|
|
|
|
return l_dbus_message_new_error(message,
|
|
|
|
"org.test.InvalidArguments",
|
|
|
|
"Invalid arguments");
|
|
|
|
|
|
|
|
return l_dbus_message_new_error(message, "org.test.InvalidArguments",
|
|
|
|
"Unknown Property %s", property);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *device_get_properties(struct l_dbus *dbus,
|
|
|
|
struct l_dbus_message *message,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct l_dbus_message *reply;
|
|
|
|
|
|
|
|
reply = l_dbus_message_new_method_return(message);
|
|
|
|
l_dbus_message_set_arguments(reply, "a{sv}", 0);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_device_interface(struct l_dbus_interface *interface)
|
|
|
|
{
|
|
|
|
l_dbus_interface_method(interface, "GetProperties", 0,
|
|
|
|
device_get_properties,
|
|
|
|
"a{sv}", "", "properties");
|
|
|
|
l_dbus_interface_method(interface, "SetProperty", 0,
|
|
|
|
device_set_property,
|
|
|
|
"", "sv", "name", "value");
|
|
|
|
|
|
|
|
l_dbus_interface_signal(interface, "PropertyChanged", 0,
|
|
|
|
"sv", "name", "value");
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:23:56 +02:00
|
|
|
static void bss_free(void *data)
|
|
|
|
{
|
|
|
|
struct bss *bss = data;
|
|
|
|
|
|
|
|
l_debug("Freeing BSS %02X:%02X:%02X:%02X:%02X:%02X [%s]",
|
|
|
|
bss->addr[0], bss->addr[1], bss->addr[2],
|
|
|
|
bss->addr[3], bss->addr[4], bss->addr[5], bss->ssid);
|
|
|
|
|
|
|
|
l_free(bss->ssid);
|
|
|
|
l_free(bss);
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
static void netdev_free(void *data)
|
|
|
|
{
|
|
|
|
struct netdev *netdev = data;
|
2014-10-23 21:32:12 +02:00
|
|
|
struct l_dbus *dbus;
|
|
|
|
|
|
|
|
dbus = dbus_get_bus();
|
2014-10-23 22:09:52 +02:00
|
|
|
l_dbus_unregister_interface(dbus, device_get_path(netdev),
|
|
|
|
IWD_DEVICE_INTERFACE);
|
2014-10-23 21:32:12 +02:00
|
|
|
|
|
|
|
device_emit_removed(netdev);
|
2014-08-07 05:15:20 +02:00
|
|
|
|
|
|
|
l_debug("Freeing interface %s", netdev->name);
|
|
|
|
|
2014-08-09 08:23:56 +02:00
|
|
|
l_queue_destroy(netdev->bss_list, bss_free);
|
2014-08-07 05:15:20 +02:00
|
|
|
l_free(netdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool netdev_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct netdev *netdev = a;
|
|
|
|
uint32_t index = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return (netdev->index == index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wiphy_free(void *data)
|
|
|
|
{
|
|
|
|
struct wiphy *wiphy = data;
|
|
|
|
|
|
|
|
l_debug("Freeing wiphy %s", wiphy->name);
|
|
|
|
|
|
|
|
l_queue_destroy(wiphy->netdev_list, netdev_free);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:45:15 +02:00
|
|
|
static void mlme_authenticate(struct netdev *netdev, struct bss *bss)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
uint32_t auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
|
|
|
|
|
2014-08-09 09:01:22 +02:00
|
|
|
if (!bss) {
|
|
|
|
bss = l_queue_peek_head(netdev->bss_list);
|
|
|
|
if (!bss)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:45:15 +02:00
|
|
|
msg = l_genl_msg_new_sized(NL80211_CMD_AUTHENTICATE, 512);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4,
|
|
|
|
&bss->frequency);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, bss->addr);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, strlen(bss->ssid),
|
|
|
|
bss->ssid);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_AUTH_TYPE, 4, &auth_type);
|
|
|
|
l_genl_family_send(nl80211, msg, NULL, NULL, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
}
|
|
|
|
|
2014-08-09 09:01:22 +02:00
|
|
|
static void mlme_associate(struct netdev *netdev, struct bss *bss)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
|
|
|
if (!bss) {
|
|
|
|
bss = l_queue_peek_head(netdev->bss_list);
|
|
|
|
if (!bss)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = l_genl_msg_new_sized(NL80211_CMD_ASSOCIATE, 512);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4,
|
|
|
|
&bss->frequency);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, bss->addr);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, strlen(bss->ssid),
|
|
|
|
bss->ssid);
|
|
|
|
l_genl_family_send(nl80211, msg, NULL, NULL, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
}
|
|
|
|
|
2014-08-09 07:21:30 +02:00
|
|
|
static void trigger_scan_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct wiphy *wiphy = 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)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
l_debug("Scan triggered for wiphy %s", wiphy->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trigger_scan(struct wiphy *wiphy)
|
|
|
|
{
|
|
|
|
struct netdev *netdev;
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
|
|
|
if (!network_ssid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netdev = l_queue_peek_head(wiphy->netdev_list);
|
|
|
|
if (!netdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg = l_genl_msg_new_sized(NL80211_CMD_TRIGGER_SCAN, 16);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
|
|
|
|
l_genl_family_send(nl80211, msg, trigger_scan_callback, wiphy, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:23:56 +02:00
|
|
|
static bool parse_ie(const void *data, uint16_t len)
|
|
|
|
{
|
|
|
|
struct ie_tlv_iter iter;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
ie_tlv_iter_init(&iter, data, len);
|
|
|
|
|
|
|
|
while (ie_tlv_iter_next(&iter)) {
|
|
|
|
uint8_t tag = ie_tlv_iter_get_tag(&iter);
|
|
|
|
|
|
|
|
if (tag == 0) {
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = l_strndup((const char *) iter.data, iter.len);
|
|
|
|
if (!strcmp(str, network_ssid)) {
|
|
|
|
l_debug("Found network SSID=%s", str);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
l_free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_bss(struct netdev *netdev, struct l_genl_attr *attr)
|
|
|
|
{
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t bssid[ETH_ALEN];
|
|
|
|
uint32_t frequency;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_BSS_BSSID:
|
|
|
|
if (len != sizeof(bssid)) {
|
|
|
|
l_warn("Invalid BSSID attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(bssid, data, len);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_FREQUENCY:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid frequency attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
frequency = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
case NL80211_BSS_INFORMATION_ELEMENTS:
|
|
|
|
found = parse_ie(data, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
struct bss *bss;
|
|
|
|
|
|
|
|
l_debug("Frequency %u", frequency);
|
|
|
|
|
|
|
|
bss = l_new(struct bss, 1);
|
|
|
|
memcpy(bss->addr, bssid, sizeof(bss->addr));
|
|
|
|
bss->frequency = frequency;
|
|
|
|
bss->ssid = l_strdup(network_ssid);
|
|
|
|
l_queue_push_head(netdev->bss_list, bss);
|
2014-08-09 08:45:15 +02:00
|
|
|
|
|
|
|
mlme_authenticate(netdev, bss);
|
2014-08-09 08:23:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 07:21:30 +02:00
|
|
|
static void get_scan_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct wiphy *wiphy = user_data;
|
2014-08-09 08:23:56 +02:00
|
|
|
struct netdev *netdev = NULL;
|
|
|
|
struct l_genl_attr attr, nested;
|
2014-08-09 07:21:30 +02:00
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
2014-08-09 08:23:56 +02:00
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid interface index attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
netdev = l_queue_find(wiphy->netdev_list, netdev_match,
|
|
|
|
L_UINT_TO_PTR(*((uint32_t *) data)));
|
|
|
|
if (!netdev) {
|
|
|
|
l_warn("No interface structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-09 07:21:30 +02:00
|
|
|
|
2014-08-09 08:23:56 +02:00
|
|
|
case NL80211_ATTR_BSS:
|
|
|
|
if (!netdev) {
|
|
|
|
l_warn("No interface structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!l_genl_attr_recurse(&attr, &nested))
|
|
|
|
return;
|
|
|
|
|
|
|
|
parse_bss(netdev, &nested);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-09 07:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_scan(struct wiphy *wiphy)
|
|
|
|
{
|
|
|
|
struct netdev *netdev;
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
|
|
|
if (!network_ssid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netdev = l_queue_peek_head(wiphy->netdev_list);
|
|
|
|
if (!netdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg = l_genl_msg_new_sized(NL80211_CMD_GET_SCAN, 8);
|
|
|
|
l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
|
|
|
|
l_genl_family_dump(nl80211, msg, get_scan_callback, wiphy, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
}
|
|
|
|
|
2014-08-07 06:27:32 +02:00
|
|
|
static void interface_dump_callback(struct l_genl_msg *msg, void *user_data)
|
2014-08-07 05:15:20 +02:00
|
|
|
{
|
2014-08-07 06:27:32 +02:00
|
|
|
struct wiphy *wiphy = NULL;
|
2014-08-07 05:15:20 +02:00
|
|
|
struct netdev *netdev;
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
uint8_t ifaddr[ETH_ALEN];
|
|
|
|
uint32_t ifindex, iftype;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(ifname, 0, sizeof(ifname));
|
|
|
|
memset(ifaddr, 0, sizeof(ifaddr));
|
|
|
|
iftype = NL80211_IFTYPE_UNSPECIFIED;
|
|
|
|
ifindex = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The interface index and interface name attributes are normally
|
|
|
|
* listed before the wiphy attribute. This handling assumes that
|
|
|
|
* all attributes are included in the same message.
|
|
|
|
*
|
|
|
|
* If any required attribute is missing, the whole message will
|
|
|
|
* be ignored.
|
|
|
|
*/
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid interface index attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifindex = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_IFNAME:
|
|
|
|
if (len > sizeof(ifname)) {
|
|
|
|
l_warn("Invalid interface name attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ifname, data, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 06:27:32 +02:00
|
|
|
wiphy = l_queue_find(wiphy_list, wiphy_match,
|
|
|
|
L_UINT_TO_PTR(*((uint32_t *) data)));
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
2014-08-07 05:15:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_IFTYPE:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid interface type attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iftype = *((uint32_t *) data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_MAC:
|
|
|
|
if (len != sizeof(ifaddr)) {
|
|
|
|
l_warn("Invalid interface address attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ifaddr, data, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ifindex) {
|
|
|
|
l_warn("Missing interface index attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
netdev = l_queue_find(wiphy->netdev_list, netdev_match,
|
|
|
|
L_UINT_TO_PTR(ifindex));
|
|
|
|
if (!netdev) {
|
2014-10-23 21:32:12 +02:00
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
char path[256];
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/%u", ifindex);
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
netdev = l_new(struct netdev, 1);
|
2014-08-09 08:23:56 +02:00
|
|
|
netdev->bss_list = l_queue_new();
|
2014-08-07 08:51:25 +02:00
|
|
|
l_queue_push_head(wiphy->netdev_list, netdev);
|
2014-10-23 21:32:12 +02:00
|
|
|
|
|
|
|
if (!l_dbus_register_interface(dbus, path, IWD_DEVICE_INTERFACE,
|
|
|
|
setup_device_interface, netdev, NULL))
|
|
|
|
l_info("Unable to register %s interface",
|
|
|
|
IWD_DEVICE_INTERFACE);
|
|
|
|
else
|
|
|
|
device_emit_added(netdev);
|
2014-08-07 05:15:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(netdev->name, ifname, sizeof(netdev->name));
|
|
|
|
memcpy(netdev->addr, ifaddr, sizeof(netdev->addr));
|
|
|
|
netdev->index = ifindex;
|
|
|
|
netdev->type = iftype;
|
|
|
|
|
|
|
|
l_debug("Found interface %s", netdev->name);
|
2014-08-09 07:21:30 +02:00
|
|
|
|
|
|
|
trigger_scan(wiphy);
|
2014-08-07 05:15:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wiphy_dump_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct wiphy *wiphy = NULL;
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
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->netdev_list = l_queue_new();
|
2014-08-07 06:27:32 +02:00
|
|
|
l_queue_push_head(wiphy_list, wiphy);
|
2014-08-07 05:15:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_WIPHY_NAME:
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > sizeof(wiphy->name)) {
|
|
|
|
l_warn("Invalid wiphy name attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(wiphy->name, data, len);
|
|
|
|
break;
|
2014-08-08 00:41:30 +02:00
|
|
|
|
|
|
|
case NL80211_ATTR_FEATURE_FLAGS:
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid feature flags attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wiphy->feature_flags = *((uint32_t *) data);
|
|
|
|
break;
|
2014-08-07 05:15:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 08:52:08 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wiphy_scan_notify(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
2014-08-09 07:21:30 +02:00
|
|
|
struct wiphy *wiphy = NULL;
|
2014-08-07 08:52:08 +02:00
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t cmd;
|
|
|
|
|
|
|
|
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)) {
|
2014-08-09 07:21:30 +02:00
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wiphy = l_queue_find(wiphy_list, wiphy_match,
|
|
|
|
L_UINT_TO_PTR(*((uint32_t *) data)));
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("Scan notification is missing wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == NL80211_CMD_NEW_SCAN_RESULTS) {
|
|
|
|
get_scan(wiphy);
|
|
|
|
return;
|
2014-08-07 08:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wiphy_mlme_notify(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
2014-08-09 09:01:22 +02:00
|
|
|
struct wiphy *wiphy = NULL;
|
|
|
|
struct netdev *netdev = NULL;
|
2014-08-07 08:52:08 +02:00
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t cmd;
|
|
|
|
|
|
|
|
cmd = l_genl_msg_get_command(msg);
|
|
|
|
|
|
|
|
l_debug("MLME notification %u", cmd);
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
2014-08-09 09:01:22 +02:00
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wiphy = l_queue_find(wiphy_list, wiphy_match,
|
|
|
|
L_UINT_TO_PTR(*((uint32_t *) data)));
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("No wiphy structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid interface index attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
netdev = l_queue_find(wiphy->netdev_list, netdev_match,
|
|
|
|
L_UINT_TO_PTR(*((uint32_t *) data)));
|
|
|
|
if (!netdev) {
|
|
|
|
l_warn("No interface structure found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wiphy) {
|
|
|
|
l_warn("Scan notification is missing wiphy attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!netdev) {
|
|
|
|
l_warn("Scan notification is missing interface attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == NL80211_CMD_AUTHENTICATE) {
|
|
|
|
mlme_associate(netdev, NULL);
|
|
|
|
return;
|
2014-08-07 08:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 22:23:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 22:13:31 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:25:01 +02:00
|
|
|
static void nl80211_appeared(void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
|
|
|
l_debug("Found nl80211 interface");
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2014-08-07 08:52:08 +02:00
|
|
|
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, "scan", wiphy_scan_notify,
|
|
|
|
NULL, NULL))
|
|
|
|
l_error("Registering for scan notification failed");
|
|
|
|
|
|
|
|
if (!l_genl_family_register(nl80211, "mlme", wiphy_mlme_notify,
|
|
|
|
NULL, NULL))
|
|
|
|
l_error("Registering for MLME notification failed");
|
|
|
|
|
|
|
|
if (!l_genl_family_register(nl80211, "regulatory",
|
|
|
|
wiphy_regulatory_notify, NULL, NULL))
|
|
|
|
l_error("Registering for regulatory notification failed");
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
wiphy_list = l_queue_new();
|
|
|
|
|
2014-08-07 22:13:31 +02:00
|
|
|
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");
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
|
2014-08-07 22:23:02 +02:00
|
|
|
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");
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
|
2014-07-29 21:25:01 +02:00
|
|
|
msg = l_genl_msg_new(NL80211_CMD_GET_WIPHY);
|
2014-08-07 06:27:32 +02:00
|
|
|
if (!l_genl_family_dump(nl80211, msg, wiphy_dump_callback, NULL, NULL))
|
|
|
|
l_error("Getting all wiphy devices failed");
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
|
|
|
|
msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE);
|
|
|
|
if (!l_genl_family_dump(nl80211, msg, interface_dump_callback,
|
|
|
|
NULL, NULL))
|
|
|
|
l_error("Getting all interface information failed");
|
|
|
|
l_genl_msg_unref(msg);
|
2014-07-29 21:25:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nl80211_vanished(void *user_data)
|
|
|
|
{
|
|
|
|
l_debug("Lost nl80211 interface");
|
2014-08-07 05:15:20 +02:00
|
|
|
|
|
|
|
l_queue_destroy(wiphy_list, wiphy_free);
|
|
|
|
wiphy_list = NULL;
|
2014-07-29 21:25:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wiphy_init(void)
|
|
|
|
{
|
|
|
|
if (genl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
genl = l_genl_new_default();
|
|
|
|
if (!genl) {
|
|
|
|
l_error("Failed to open generic netlink socket");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:06:51 +02:00
|
|
|
if (getenv("IWD_GENL_DEBUG"))
|
|
|
|
l_genl_set_debug(genl, do_debug, "[GENL] ", NULL);
|
2014-07-29 21:25:01 +02:00
|
|
|
|
|
|
|
l_debug("Opening nl80211 interface");
|
|
|
|
|
|
|
|
nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
|
|
|
|
if (!nl80211) {
|
|
|
|
l_error("Failed to open nl80211 interface");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_genl_family_set_watches(nl80211, nl80211_appeared, nl80211_vanished,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
l_genl_unref(genl);
|
|
|
|
genl = NULL;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wiphy_exit(void)
|
|
|
|
{
|
|
|
|
if (!genl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
l_debug("Closing nl80211 interface");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The generic netlink master object keeps track of all families
|
|
|
|
* and closing it will take care of freeing all associated resources.
|
|
|
|
*/
|
|
|
|
l_genl_unref(genl);
|
|
|
|
genl = NULL;
|
|
|
|
|
2014-08-07 05:15:20 +02:00
|
|
|
/*
|
|
|
|
* This is an extra sanity check so that no memory is leaked
|
|
|
|
* in case the generic netlink handling forgets to call the
|
|
|
|
* vanished callback.
|
|
|
|
*/
|
|
|
|
if (wiphy_list) {
|
|
|
|
l_warn("Found leftover list of wiphy devices");
|
|
|
|
l_queue_destroy(wiphy_list, wiphy_free);
|
|
|
|
wiphy_list = NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:25:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-07 08:52:42 +02:00
|
|
|
|
|
|
|
static void wiphy_check_dellink(void *data, void *user_data)
|
|
|
|
{
|
|
|
|
uint32_t index = L_PTR_TO_UINT(user_data);
|
|
|
|
struct wiphy *wiphy = data;
|
|
|
|
struct netdev *netdev;
|
|
|
|
|
|
|
|
netdev = l_queue_remove_if(wiphy->netdev_list, netdev_match,
|
|
|
|
L_UINT_TO_PTR(index));
|
|
|
|
if (netdev) {
|
|
|
|
l_warn("Removing leftover interface %s", netdev->name);
|
|
|
|
netdev_free(netdev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wiphy_notify_dellink(uint32_t index)
|
|
|
|
{
|
|
|
|
if (!wiphy_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_queue_foreach(wiphy_list, wiphy_check_dellink, L_UINT_TO_PTR(index));
|
|
|
|
}
|
2014-08-09 07:19:47 +02:00
|
|
|
|
|
|
|
void wiphy_set_ssid(const char *ssid)
|
|
|
|
{
|
|
|
|
network_ssid = ssid;
|
|
|
|
}
|