2009-12-18 15:35:33 +01:00
|
|
|
/*
|
|
|
|
* Netlink helper functions for driver wrappers
|
2014-01-06 10:49:46 +01:00
|
|
|
* Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
|
2009-12-18 15:35:33 +01:00
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2009-12-18 15:35:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
2009-12-18 16:11:54 +01:00
|
|
|
#include "eloop.h"
|
2009-12-18 15:35:33 +01:00
|
|
|
#include "priv_netlink.h"
|
|
|
|
#include "netlink.h"
|
|
|
|
|
|
|
|
|
2009-12-18 16:11:54 +01:00
|
|
|
struct netlink_data {
|
|
|
|
struct netlink_config *cfg;
|
|
|
|
int sock;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-12-18 16:49:07 +01:00
|
|
|
static void netlink_receive_link(struct netlink_data *netlink,
|
|
|
|
void (*cb)(void *ctx, struct ifinfomsg *ifi,
|
|
|
|
u8 *buf, size_t len),
|
|
|
|
struct nlmsghdr *h)
|
|
|
|
{
|
|
|
|
if (cb == NULL || NLMSG_PAYLOAD(h, 0) < sizeof(struct ifinfomsg))
|
|
|
|
return;
|
|
|
|
cb(netlink->cfg->ctx, NLMSG_DATA(h),
|
2011-10-18 16:00:08 +02:00
|
|
|
(u8 *) NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg)),
|
2009-12-18 16:49:07 +01:00
|
|
|
NLMSG_PAYLOAD(h, sizeof(struct ifinfomsg)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-18 16:11:54 +01:00
|
|
|
static void netlink_receive(int sock, void *eloop_ctx, void *sock_ctx)
|
|
|
|
{
|
|
|
|
struct netlink_data *netlink = eloop_ctx;
|
|
|
|
char buf[8192];
|
|
|
|
int left;
|
|
|
|
struct sockaddr_nl from;
|
|
|
|
socklen_t fromlen;
|
|
|
|
struct nlmsghdr *h;
|
|
|
|
int max_events = 10;
|
|
|
|
|
|
|
|
try_again:
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (left < 0) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
wpa_printf(MSG_INFO, "netlink: recvfrom failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = (struct nlmsghdr *) buf;
|
2009-12-18 16:22:35 +01:00
|
|
|
while (NLMSG_OK(h, left)) {
|
2009-12-18 16:11:54 +01:00
|
|
|
switch (h->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
2009-12-18 16:49:07 +01:00
|
|
|
netlink_receive_link(netlink, netlink->cfg->newlink_cb,
|
|
|
|
h);
|
2009-12-18 16:11:54 +01:00
|
|
|
break;
|
|
|
|
case RTM_DELLINK:
|
2009-12-18 16:49:07 +01:00
|
|
|
netlink_receive_link(netlink, netlink->cfg->dellink_cb,
|
|
|
|
h);
|
2009-12-18 16:11:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-18 16:22:35 +01:00
|
|
|
h = NLMSG_NEXT(h, left);
|
2009-12-18 16:11:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (left > 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "netlink: %d extra bytes in the end of "
|
|
|
|
"netlink message", left);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--max_events > 0) {
|
|
|
|
/*
|
|
|
|
* Try to receive all events in one eloop call in order to
|
|
|
|
* limit race condition on cases where AssocInfo event, Assoc
|
|
|
|
* event, and EAPOL frames are received more or less at the
|
|
|
|
* same time. We want to process the event messages first
|
|
|
|
* before starting EAPOL processing.
|
|
|
|
*/
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct netlink_data * netlink_init(struct netlink_config *cfg)
|
|
|
|
{
|
|
|
|
struct netlink_data *netlink;
|
|
|
|
struct sockaddr_nl local;
|
|
|
|
|
|
|
|
netlink = os_zalloc(sizeof(*netlink));
|
|
|
|
if (netlink == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
netlink->sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (netlink->sock < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "netlink: Failed to open netlink "
|
|
|
|
"socket: %s", strerror(errno));
|
|
|
|
netlink_deinit(netlink);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&local, 0, sizeof(local));
|
|
|
|
local.nl_family = AF_NETLINK;
|
|
|
|
local.nl_groups = RTMGRP_LINK;
|
|
|
|
if (bind(netlink->sock, (struct sockaddr *) &local, sizeof(local)) < 0)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_ERROR, "netlink: Failed to bind netlink "
|
|
|
|
"socket: %s", strerror(errno));
|
|
|
|
netlink_deinit(netlink);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eloop_register_read_sock(netlink->sock, netlink_receive, netlink,
|
|
|
|
NULL);
|
|
|
|
|
2012-11-24 15:47:20 +01:00
|
|
|
netlink->cfg = cfg;
|
|
|
|
|
2009-12-18 16:11:54 +01:00
|
|
|
return netlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void netlink_deinit(struct netlink_data *netlink)
|
|
|
|
{
|
|
|
|
if (netlink == NULL)
|
|
|
|
return;
|
|
|
|
if (netlink->sock >= 0) {
|
|
|
|
eloop_unregister_read_sock(netlink->sock);
|
|
|
|
close(netlink->sock);
|
|
|
|
}
|
|
|
|
os_free(netlink->cfg);
|
|
|
|
os_free(netlink);
|
|
|
|
}
|
|
|
|
|
2014-01-06 10:49:46 +01:00
|
|
|
|
|
|
|
static const char * linkmode_str(int mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case -1:
|
|
|
|
return "no change";
|
|
|
|
case 0:
|
|
|
|
return "kernel-control";
|
|
|
|
case 1:
|
|
|
|
return "userspace-control";
|
|
|
|
}
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char * operstate_str(int state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case -1:
|
|
|
|
return "no change";
|
|
|
|
case IF_OPER_DORMANT:
|
|
|
|
return "IF_OPER_DORMANT";
|
|
|
|
case IF_OPER_UP:
|
|
|
|
return "IF_OPER_UP";
|
|
|
|
}
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-18 16:11:54 +01:00
|
|
|
int netlink_send_oper_ifla(struct netlink_data *netlink, int ifindex,
|
|
|
|
int linkmode, int operstate)
|
2009-12-18 15:35:33 +01:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct nlmsghdr hdr;
|
|
|
|
struct ifinfomsg ifinfo;
|
|
|
|
char opts[16];
|
|
|
|
} req;
|
|
|
|
struct rtattr *rta;
|
|
|
|
static int nl_seq;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
os_memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
|
|
|
|
req.hdr.nlmsg_type = RTM_SETLINK;
|
|
|
|
req.hdr.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.hdr.nlmsg_seq = ++nl_seq;
|
|
|
|
req.hdr.nlmsg_pid = 0;
|
|
|
|
|
|
|
|
req.ifinfo.ifi_family = AF_UNSPEC;
|
|
|
|
req.ifinfo.ifi_type = 0;
|
|
|
|
req.ifinfo.ifi_index = ifindex;
|
|
|
|
req.ifinfo.ifi_flags = 0;
|
|
|
|
req.ifinfo.ifi_change = 0;
|
|
|
|
|
|
|
|
if (linkmode != -1) {
|
|
|
|
rta = aliasing_hide_typecast(
|
|
|
|
((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
|
|
|
|
struct rtattr);
|
|
|
|
rta->rta_type = IFLA_LINKMODE;
|
|
|
|
rta->rta_len = RTA_LENGTH(sizeof(char));
|
|
|
|
*((char *) RTA_DATA(rta)) = linkmode;
|
|
|
|
req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
|
|
|
|
RTA_LENGTH(sizeof(char));
|
|
|
|
}
|
|
|
|
if (operstate != -1) {
|
|
|
|
rta = aliasing_hide_typecast(
|
|
|
|
((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
|
|
|
|
struct rtattr);
|
|
|
|
rta->rta_type = IFLA_OPERSTATE;
|
|
|
|
rta->rta_len = RTA_LENGTH(sizeof(char));
|
|
|
|
*((char *) RTA_DATA(rta)) = operstate;
|
|
|
|
req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
|
|
|
|
RTA_LENGTH(sizeof(char));
|
|
|
|
}
|
|
|
|
|
2014-01-06 10:49:46 +01:00
|
|
|
wpa_printf(MSG_DEBUG, "netlink: Operstate: ifindex=%d linkmode=%d (%s), operstate=%d (%s)",
|
|
|
|
ifindex, linkmode, linkmode_str(linkmode),
|
|
|
|
operstate, operstate_str(operstate));
|
2009-12-18 15:35:33 +01:00
|
|
|
|
2009-12-18 16:11:54 +01:00
|
|
|
ret = send(netlink->sock, &req, req.hdr.nlmsg_len, 0);
|
2009-12-18 15:35:33 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "netlink: Sending operstate IFLA "
|
|
|
|
"failed: %s (assume operstate is not supported)",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret < 0 ? -1 : 0;
|
|
|
|
}
|