2011-07-02 11:04:50 +05:30
|
|
|
/* netlink.c - netlink physical link notification handling and info retrieval
|
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* Copyright (c) 2011 Nicholas J. Kain <njkain at gmail dot com>
|
|
|
|
* All rights reserved.
|
2011-07-02 11:04:50 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2011-07-02 11:04:50 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
2011-07-02 11:04:50 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2011-07-02 11:04:50 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2011-07-04 03:40:00 +05:30
|
|
|
#include <poll.h>
|
2011-07-02 11:04:50 +05:30
|
|
|
|
|
|
|
#include "netlink.h"
|
|
|
|
#include "log.h"
|
2011-07-04 03:00:55 +05:30
|
|
|
#include "nl.h"
|
2011-07-05 07:40:14 +05:30
|
|
|
#include "state.h"
|
2011-07-02 11:04:50 +05:30
|
|
|
|
2011-07-04 03:00:55 +05:30
|
|
|
static int nlrtattr_assign(struct nlattr *attr, int type, void *data)
|
2011-07-02 11:04:50 +05:30
|
|
|
{
|
2011-07-04 03:00:55 +05:30
|
|
|
struct nlattr **tb = data;
|
|
|
|
if (type >= IFLA_MAX)
|
|
|
|
return 0;
|
2011-07-02 11:04:50 +05:30
|
|
|
tb[type] = attr;
|
2011-07-04 03:00:55 +05:30
|
|
|
return 0;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void get_if_index_and_mac(const struct nlmsghdr *nlh,
|
2011-07-04 03:00:55 +05:30
|
|
|
struct ifinfomsg *ifm)
|
2011-07-02 11:04:50 +05:30
|
|
|
{
|
2011-07-04 03:00:55 +05:30
|
|
|
struct nlattr *tb[IFLA_MAX] = {0};
|
|
|
|
nl_attr_parse(nlh, sizeof *ifm, nlrtattr_assign, tb);
|
2011-07-02 11:04:50 +05:30
|
|
|
if (!tb[IFLA_IFNAME])
|
|
|
|
return;
|
2014-03-12 22:33:34 +05:30
|
|
|
if (!strncmp(client_config.interface,
|
|
|
|
nlattr_get_data(tb[IFLA_IFNAME]),
|
|
|
|
sizeof client_config.interface)) {
|
2011-07-02 11:04:50 +05:30
|
|
|
client_config.ifindex = ifm->ifi_index;
|
|
|
|
if (!tb[IFLA_ADDRESS])
|
2011-07-12 02:03:57 +05:30
|
|
|
suicide("FATAL: Adapter %s lacks a hardware address.");
|
2011-07-04 03:00:55 +05:30
|
|
|
int maclen = nlattr_get_len(tb[IFLA_ADDRESS]) - 4;
|
2011-07-02 11:04:50 +05:30
|
|
|
if (maclen != 6)
|
2011-07-12 02:03:57 +05:30
|
|
|
suicide("FATAL: Adapter hardware address length should be 6, but is %u.",
|
2011-07-02 11:04:50 +05:30
|
|
|
maclen);
|
|
|
|
|
|
|
|
const unsigned char *mac =
|
2011-07-04 03:00:55 +05:30
|
|
|
(unsigned char *)nlattr_get_data(tb[IFLA_ADDRESS]);
|
2011-07-02 11:04:50 +05:30
|
|
|
log_line("%s hardware address %x:%x:%x:%x:%x:%x",
|
|
|
|
client_config.interface,
|
|
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
|
|
memcpy(client_config.arp, mac, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 16:01:57 +05:30
|
|
|
static int nl_process_msgs(const struct nlmsghdr *nlh, void *data)
|
2011-07-02 11:04:50 +05:30
|
|
|
{
|
2011-07-04 03:00:55 +05:30
|
|
|
struct ifinfomsg *ifm = nlmsg_get_data(nlh);
|
2011-07-02 11:04:50 +05:30
|
|
|
struct client_state_t *cs = data;
|
|
|
|
|
|
|
|
switch(nlh->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
if (!client_config.ifindex)
|
2011-07-04 03:00:55 +05:30
|
|
|
get_if_index_and_mac(nlh, ifm);
|
2011-07-02 11:04:50 +05:30
|
|
|
if (ifm->ifi_index != client_config.ifindex)
|
|
|
|
break;
|
2011-07-02 11:21:32 +05:30
|
|
|
// IFF_UP corresponds to ifconfig down or ifconfig up.
|
2011-07-02 11:04:50 +05:30
|
|
|
if (ifm->ifi_flags & IFF_UP) {
|
2011-07-02 11:21:32 +05:30
|
|
|
// IFF_RUNNING is the hardware carrier.
|
2011-07-02 11:04:50 +05:30
|
|
|
if (ifm->ifi_flags & IFF_RUNNING) {
|
|
|
|
if (cs->ifsPrevState != IFS_UP) {
|
|
|
|
cs->ifsPrevState = IFS_UP;
|
2011-07-05 07:40:14 +05:30
|
|
|
ifup_action(cs);
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
2011-07-05 07:40:14 +05:30
|
|
|
} else if (cs->ifsPrevState != IFS_DOWN) {
|
|
|
|
// Interface configured, but no hardware carrier.
|
|
|
|
cs->ifsPrevState = IFS_DOWN;
|
|
|
|
ifnocarrier_action(cs);
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
2011-07-05 07:40:14 +05:30
|
|
|
} else if (cs->ifsPrevState != IFS_SHUT) {
|
2011-07-02 11:21:32 +05:30
|
|
|
// User shut down the interface.
|
2011-07-05 07:40:14 +05:30
|
|
|
cs->ifsPrevState = IFS_SHUT;
|
|
|
|
ifdown_action(cs);
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RTM_DELLINK:
|
|
|
|
if (ifm->ifi_index != client_config.ifindex)
|
|
|
|
break;
|
|
|
|
if (cs->ifsPrevState != IFS_REMOVED) {
|
|
|
|
cs->ifsPrevState = IFS_REMOVED;
|
2011-07-12 02:03:57 +05:30
|
|
|
log_line("Interface removed. Exiting.");
|
2011-07-02 11:04:50 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 03:00:55 +05:30
|
|
|
return 1;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void handle_nl_message(struct client_state_t *cs)
|
|
|
|
{
|
2012-04-13 05:36:05 +05:30
|
|
|
char nlbuf[8192];
|
2011-07-04 03:00:55 +05:30
|
|
|
ssize_t ret;
|
2011-07-02 11:04:50 +05:30
|
|
|
assert(cs->nlFd != -1);
|
|
|
|
do {
|
2011-07-04 03:00:55 +05:30
|
|
|
ret = nl_recv_buf(cs->nlFd, nlbuf, sizeof nlbuf);
|
|
|
|
if (ret == -1)
|
|
|
|
break;
|
2012-04-13 05:36:05 +05:30
|
|
|
if (nl_foreach_nlmsg(nlbuf, ret, cs->nlPortId, nl_process_msgs, cs)
|
|
|
|
== -1)
|
2011-07-04 03:00:55 +05:30
|
|
|
break;
|
2011-07-02 11:04:50 +05:30
|
|
|
} while (ret > 0);
|
|
|
|
}
|
|
|
|
|
2012-04-04 07:30:47 +05:30
|
|
|
int nl_getifdata(struct client_state_t *cs)
|
|
|
|
{
|
2014-03-15 09:24:21 +05:30
|
|
|
if (nl_sendgetlink(cs->nlFd))
|
2012-04-04 07:30:47 +05:30
|
|
|
return -1;
|
2011-07-02 11:04:50 +05:30
|
|
|
|
2011-07-04 03:40:00 +05:30
|
|
|
for (int pr = 0; !pr;) {
|
|
|
|
pr = poll(&((struct pollfd){.fd=cs->nlFd,.events=POLLIN}), 1, -1);
|
|
|
|
if (pr == 1)
|
|
|
|
handle_nl_message(cs);
|
|
|
|
else if (pr == -1)
|
|
|
|
suicide("nl: poll failed");
|
|
|
|
}
|
2011-07-02 11:04:50 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|