2011-07-02 11:04:50 +05:30
|
|
|
/* netlink.c - netlink physical link notification handling and info retrieval
|
|
|
|
*
|
2018-10-26 16:41:16 +05:30
|
|
|
* Copyright 2011-2018 Nicholas J. Kain <njkain at gmail dot com>
|
2011-07-25 12:00:57 +05:30
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2014-03-17 15:26:30 +05:30
|
|
|
#include <unistd.h>
|
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>
|
2014-03-15 10:00:39 +05:30
|
|
|
#include <time.h>
|
2011-07-02 11:04:50 +05:30
|
|
|
#include <errno.h>
|
2011-07-04 03:40:00 +05:30
|
|
|
#include <poll.h>
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/log.h"
|
2011-07-02 11:04:50 +05:30
|
|
|
|
|
|
|
#include "netlink.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
|
|
|
|
2017-01-12 16:35:00 +05:30
|
|
|
// Returns true if the current interface state is UP.
|
|
|
|
bool nl_event_carrier_wentup(int state)
|
2011-07-02 11:04:50 +05:30
|
|
|
{
|
2015-02-15 17:08:03 +05:30
|
|
|
switch (state) {
|
|
|
|
case IFS_UP:
|
2017-01-12 16:35:00 +05:30
|
|
|
log_line("%s: Carrier up.", client_config.interface);
|
|
|
|
return true;
|
2015-02-15 17:08:03 +05:30
|
|
|
case IFS_DOWN:
|
|
|
|
// Interface configured, but no hardware carrier.
|
2015-02-18 16:01:13 +05:30
|
|
|
log_line("%s: Carrier down.", client_config.interface);
|
2017-01-12 16:35:00 +05:30
|
|
|
return false;
|
2015-02-15 17:08:03 +05:30
|
|
|
case IFS_SHUT:
|
|
|
|
// User shut down the interface.
|
2017-01-12 16:35:00 +05:30
|
|
|
log_line("%s: Interface shut down.", client_config.interface);
|
|
|
|
return false;
|
2015-02-15 17:08:03 +05:30
|
|
|
case IFS_REMOVED:
|
|
|
|
log_line("Interface removed. Exiting.");
|
|
|
|
exit(EXIT_SUCCESS);
|
2017-01-12 16:35:00 +05:30
|
|
|
default: return false;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
2015-02-15 17:08:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int nl_process_msgs_return;
|
|
|
|
static void nl_process_msgs(const struct nlmsghdr *nlh, void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
struct ifinfomsg *ifm = NLMSG_DATA(nlh);
|
|
|
|
|
|
|
|
if (ifm->ifi_index != client_config.ifindex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nlh->nlmsg_type == RTM_NEWLINK) {
|
|
|
|
// IFF_UP corresponds to ifconfig down or ifconfig up.
|
|
|
|
// IFF_RUNNING is the hardware carrier.
|
|
|
|
if (ifm->ifi_flags & IFF_UP) {
|
|
|
|
if (ifm->ifi_flags & IFF_RUNNING)
|
|
|
|
nl_process_msgs_return = IFS_UP;
|
|
|
|
else
|
|
|
|
nl_process_msgs_return = IFS_DOWN;
|
|
|
|
} else {
|
|
|
|
nl_process_msgs_return = IFS_SHUT;
|
|
|
|
}
|
|
|
|
} else if (nlh->nlmsg_type == RTM_DELLINK)
|
|
|
|
nl_process_msgs_return = IFS_REMOVED;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
int nl_event_get(struct client_state_t *cs)
|
2011-07-02 11:04:50 +05:30
|
|
|
{
|
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);
|
2015-02-15 17:08:03 +05:30
|
|
|
nl_process_msgs_return = IFS_NONE;
|
2011-07-02 11:04:50 +05:30
|
|
|
do {
|
2011-07-04 03:00:55 +05:30
|
|
|
ret = nl_recv_buf(cs->nlFd, nlbuf, sizeof nlbuf);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (ret < 0)
|
2011-07-04 03:00:55 +05:30
|
|
|
break;
|
2018-02-09 13:09:46 +05:30
|
|
|
if (nl_foreach_nlmsg(nlbuf, (size_t)ret, 0, cs->nlPortId, nl_process_msgs, 0) < 0)
|
2011-07-04 03:00:55 +05:30
|
|
|
break;
|
2011-07-02 11:04:50 +05:30
|
|
|
} while (ret > 0);
|
2015-02-15 17:08:03 +05:30
|
|
|
return nl_process_msgs_return;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
|
2014-03-17 15:26:30 +05:30
|
|
|
static int get_if_index_and_mac(const struct nlmsghdr *nlh,
|
|
|
|
struct ifinfomsg *ifm)
|
|
|
|
{
|
2014-03-18 05:52:20 +05:30
|
|
|
struct rtattr *tb[IFLA_MAX] = {0};
|
|
|
|
nl_rtattr_parse(nlh, sizeof *ifm, rtattr_assign, tb);
|
2014-03-17 15:26:30 +05:30
|
|
|
if (tb[IFLA_IFNAME] && !strncmp(client_config.interface,
|
2014-03-18 05:58:26 +05:30
|
|
|
RTA_DATA(tb[IFLA_IFNAME]),
|
2014-03-17 15:26:30 +05:30
|
|
|
sizeof client_config.interface)) {
|
|
|
|
client_config.ifindex = ifm->ifi_index;
|
|
|
|
if (!tb[IFLA_ADDRESS])
|
2020-11-25 07:32:51 +05:30
|
|
|
suicide("FATAL: Adapter %s lacks a hardware address.", client_config.interface);
|
2014-03-18 05:52:20 +05:30
|
|
|
int maclen = tb[IFLA_ADDRESS]->rta_len - 4;
|
2014-03-17 15:26:30 +05:30
|
|
|
if (maclen != 6)
|
|
|
|
suicide("FATAL: Adapter hardware address length should be 6, but is %u.",
|
|
|
|
maclen);
|
|
|
|
|
2014-03-18 05:58:26 +05:30
|
|
|
const unsigned char *mac = RTA_DATA(tb[IFLA_ADDRESS]);
|
2014-03-17 15:26:30 +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);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_handle_getifdata(const struct nlmsghdr *nlh, void *data)
|
|
|
|
{
|
|
|
|
int *got_ifdata = (int *)data;
|
2014-03-18 05:56:37 +05:30
|
|
|
struct ifinfomsg *ifm = NLMSG_DATA(nlh);
|
2014-03-17 15:26:30 +05:30
|
|
|
|
|
|
|
switch(nlh->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
*got_ifdata |= get_if_index_and_mac(nlh, ifm);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:45:59 +05:30
|
|
|
static int handle_getifdata(int fd, uint32_t seq)
|
2014-03-17 15:26:30 +05:30
|
|
|
{
|
|
|
|
char nlbuf[8192];
|
|
|
|
ssize_t ret;
|
|
|
|
int got_ifdata = 0;
|
|
|
|
do {
|
|
|
|
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (ret < 0)
|
2014-03-17 15:26:30 +05:30
|
|
|
return -1;
|
2018-02-09 13:09:46 +05:30
|
|
|
if (nl_foreach_nlmsg(nlbuf, (size_t)ret, seq, 0,
|
2014-04-06 16:03:14 +05:30
|
|
|
do_handle_getifdata, &got_ifdata) < 0)
|
2014-03-17 15:26:30 +05:30
|
|
|
return -1;
|
|
|
|
} while (ret > 0);
|
|
|
|
return got_ifdata ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nl_getifdata(void)
|
2012-04-04 07:30:47 +05:30
|
|
|
{
|
2014-03-17 15:26:30 +05:30
|
|
|
int ret = -1;
|
|
|
|
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
|
|
|
|
if (fd < 0) {
|
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:45:59 +05:30
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
|
|
|
|
log_line("%s: (%s) clock_gettime failed",
|
|
|
|
client_config.interface, __func__);
|
|
|
|
goto fail_fd;
|
|
|
|
}
|
|
|
|
uint32_t seq = ts.tv_nsec;
|
|
|
|
if (nl_sendgetlinks(fd, seq)) {
|
2014-03-17 15:26:30 +05:30
|
|
|
log_line("%s: (%s) nl_sendgetlinks failed",
|
|
|
|
client_config.interface, __func__);
|
|
|
|
goto fail_fd;
|
|
|
|
}
|
2011-07-02 11:04:50 +05:30
|
|
|
|
2011-07-04 03:40:00 +05:30
|
|
|
for (int pr = 0; !pr;) {
|
2014-03-17 15:26:30 +05:30
|
|
|
pr = poll(&((struct pollfd){.fd=fd,.events=POLLIN}), 1, -1);
|
2011-07-04 03:40:00 +05:30
|
|
|
if (pr == 1)
|
2014-03-17 15:45:59 +05:30
|
|
|
ret = handle_getifdata(fd, seq);
|
2014-04-06 16:03:14 +05:30
|
|
|
else if (pr < 0 && errno != EINTR)
|
2014-03-17 15:26:30 +05:30
|
|
|
goto fail_fd;
|
2011-07-04 03:40:00 +05:30
|
|
|
}
|
2014-03-17 15:26:30 +05:30
|
|
|
fail_fd:
|
|
|
|
close(fd);
|
|
|
|
fail:
|
|
|
|
return ret;
|
2011-07-02 11:04:50 +05:30
|
|
|
}
|
|
|
|
|