2014-06-21 13:41:40 +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 05:28:58 +02:00
|
|
|
#include <stdlib.h>
|
2014-06-21 13:41:40 +02:00
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <net/if.h>
|
2016-05-31 19:57:24 +02:00
|
|
|
#include <linux/if_ether.h>
|
2014-06-21 13:41:40 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2016-06-01 22:07:16 +02:00
|
|
|
#include "linux/nl80211.h"
|
2014-08-07 08:52:42 +02:00
|
|
|
#include "src/wiphy.h"
|
2014-06-21 20:51:26 +02:00
|
|
|
#include "src/netdev.h"
|
|
|
|
|
2016-06-01 22:26:02 +02:00
|
|
|
struct netdev {
|
|
|
|
uint32_t index;
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
uint32_t type;
|
|
|
|
uint8_t addr[ETH_ALEN];
|
|
|
|
};
|
|
|
|
|
2014-06-21 13:41:40 +02:00
|
|
|
static struct l_netlink *rtnl = NULL;
|
2016-06-01 21:58:51 +02:00
|
|
|
static struct l_genl_family *nl80211;
|
2016-06-01 22:35:26 +02:00
|
|
|
static struct l_queue *netdev_list;
|
2014-06-21 13:41:40 +02:00
|
|
|
|
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
2015-03-27 08:54:24 +01:00
|
|
|
struct cb_data {
|
|
|
|
netdev_command_func_t callback;
|
|
|
|
void *user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void netlink_result(int error, uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
|
|
|
struct cb_data *cb_data = user_data;
|
|
|
|
|
|
|
|
if (!cb_data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cb_data->callback(error < 0 ? false : true, cb_data->user_data);
|
|
|
|
|
|
|
|
l_free(cb_data);
|
|
|
|
}
|
|
|
|
|
2016-05-05 18:31:51 +02:00
|
|
|
static size_t rta_add_u8(void *rta_buf, unsigned short type, uint8_t value)
|
|
|
|
{
|
|
|
|
struct rtattr *rta = rta_buf;
|
|
|
|
|
|
|
|
rta->rta_len = RTA_LENGTH(sizeof(uint8_t));
|
|
|
|
rta->rta_type = type;
|
|
|
|
*((uint8_t *) RTA_DATA(rta)) = value;
|
|
|
|
|
|
|
|
return RTA_SPACE(sizeof(uint8_t));
|
|
|
|
}
|
|
|
|
|
2015-03-27 08:54:24 +01:00
|
|
|
void netdev_set_linkmode_and_operstate(uint32_t ifindex,
|
|
|
|
uint8_t linkmode, uint8_t operstate,
|
|
|
|
netdev_command_func_t callback, void *user_data)
|
|
|
|
{
|
|
|
|
struct ifinfomsg *rtmmsg;
|
|
|
|
void *rta_buf;
|
|
|
|
size_t bufsize;
|
|
|
|
struct cb_data *cb_data = NULL;
|
|
|
|
|
|
|
|
bufsize = NLMSG_LENGTH(sizeof(struct ifinfomsg)) +
|
|
|
|
RTA_SPACE(sizeof(uint8_t)) + RTA_SPACE(sizeof(uint8_t));
|
|
|
|
|
|
|
|
rtmmsg = l_malloc(bufsize);
|
|
|
|
memset(rtmmsg, 0, bufsize);
|
|
|
|
|
|
|
|
rtmmsg->ifi_family = AF_UNSPEC;
|
|
|
|
rtmmsg->ifi_index = ifindex;
|
|
|
|
|
|
|
|
rta_buf = rtmmsg + 1;
|
|
|
|
|
|
|
|
rta_buf += rta_add_u8(rta_buf, IFLA_LINKMODE, linkmode);
|
|
|
|
rta_buf += rta_add_u8(rta_buf, IFLA_OPERSTATE, operstate);
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
cb_data = l_new(struct cb_data, 1);
|
|
|
|
cb_data->callback = callback;
|
|
|
|
cb_data->user_data = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg,
|
|
|
|
rta_buf - (void *) rtmmsg,
|
|
|
|
netlink_result, cb_data, NULL);
|
|
|
|
|
|
|
|
l_free(rtmmsg);
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:27:39 +02:00
|
|
|
const uint8_t *netdev_get_address(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
return netdev->addr;
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:27:05 +02:00
|
|
|
uint32_t netdev_get_ifindex(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
return netdev->index;
|
|
|
|
}
|
|
|
|
|
2016-06-02 00:04:10 +02:00
|
|
|
uint32_t netdev_get_iftype(struct netdev *netdev)
|
|
|
|
{
|
|
|
|
return netdev->type;
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:35:26 +02:00
|
|
|
static void netdev_free(void *data)
|
|
|
|
{
|
|
|
|
struct netdev *netdev = data;
|
|
|
|
|
|
|
|
l_debug("Freeing netdev %s[%d]", netdev->name, netdev->index);
|
|
|
|
l_free(netdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool netdev_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct netdev *netdev = a;
|
|
|
|
uint32_t ifindex = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return (netdev->index == ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct netdev *netdev_find(int ifindex)
|
|
|
|
{
|
|
|
|
return l_queue_find(netdev_list, netdev_match, L_UINT_TO_PTR(ifindex));
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:07:16 +02:00
|
|
|
static void netdev_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_INTERFACE:
|
|
|
|
case NL80211_CMD_DEL_INTERFACE:
|
|
|
|
{
|
|
|
|
const uint32_t *wiphy_id = NULL;
|
|
|
|
const uint32_t *ifindex = 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_IFINDEX:
|
|
|
|
if (len != sizeof(uint32_t)) {
|
|
|
|
l_warn("Invalid ifindex attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifindex = data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wiphy_id || !ifindex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cmd == NL80211_CMD_NEW_INTERFACE)
|
|
|
|
l_info("New interface %d added", *ifindex);
|
|
|
|
else
|
|
|
|
l_info("Interface %d removed", *ifindex);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 21:58:51 +02:00
|
|
|
bool netdev_init(struct l_genl_family *in)
|
2014-06-21 13:41:40 +02:00
|
|
|
{
|
|
|
|
if (rtnl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
l_debug("Opening route netlink socket");
|
|
|
|
|
|
|
|
rtnl = l_netlink_new(NETLINK_ROUTE);
|
|
|
|
if (!rtnl) {
|
|
|
|
l_error("Failed to open route netlink socket");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:28:58 +02:00
|
|
|
if (getenv("IWD_RTNL_DEBUG"))
|
|
|
|
l_netlink_set_debug(rtnl, do_debug, "[RTNL] ", NULL);
|
2014-06-21 13:41:40 +02:00
|
|
|
|
2016-06-01 22:35:26 +02:00
|
|
|
netdev_list = l_queue_new();
|
|
|
|
|
2016-06-01 21:58:51 +02:00
|
|
|
nl80211 = in;
|
|
|
|
|
2016-06-01 22:07:16 +02:00
|
|
|
if (!l_genl_family_register(nl80211, "config", netdev_config_notify,
|
|
|
|
NULL, NULL))
|
|
|
|
l_error("Registering for config notification failed");
|
|
|
|
|
2014-06-21 13:41:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool netdev_exit(void)
|
|
|
|
{
|
|
|
|
if (!rtnl)
|
|
|
|
return false;
|
|
|
|
|
2016-06-01 21:58:51 +02:00
|
|
|
nl80211 = NULL;
|
|
|
|
|
2016-06-01 22:35:26 +02:00
|
|
|
l_queue_destroy(netdev_list, netdev_free);
|
|
|
|
netdev_list = NULL;
|
|
|
|
|
2014-06-21 13:41:40 +02:00
|
|
|
l_debug("Closing route netlink socket");
|
2016-05-31 23:23:12 +02:00
|
|
|
l_netlink_destroy(rtnl);
|
|
|
|
rtnl = NULL;
|
2014-06-21 13:41:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|