2022-02-07 06:35:29 +05:30
|
|
|
// Copyright 2004-2018 Nicholas J. Kain <njkain at gmail dot com>
|
|
|
|
// SPDX-License-Identifier: MIT
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <unistd.h>
|
2014-03-20 13:37:12 +05:30
|
|
|
#include <stdbool.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-17 03:37:09 +05:30
|
|
|
#include <limits.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netpacket/packet.h>
|
2014-03-09 19:16:05 +05:30
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/log.h"
|
2014-04-07 13:45:02 +05:30
|
|
|
#include "nk/io.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2014-03-11 05:01:46 +05:30
|
|
|
#include "ifset.h"
|
2014-03-10 10:22:56 +05:30
|
|
|
#include "ifchd.h"
|
2014-03-13 02:35:43 +05:30
|
|
|
#include "ndhc.h"
|
2014-03-13 00:55:07 +05:30
|
|
|
#include "nl.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2014-03-15 14:13:29 +05:30
|
|
|
static uint32_t ifset_nl_seq = 1;
|
2014-03-15 12:14:43 +05:30
|
|
|
|
2022-09-24 23:42:02 +05:30
|
|
|
// Portable ctz() that gives a defined result for x == 0.
|
|
|
|
static int trailz(uint32_t x)
|
2014-03-09 19:16:05 +05:30
|
|
|
{
|
2022-09-24 23:42:02 +05:30
|
|
|
static const int ctzt[32] = {
|
|
|
|
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
|
|
|
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10,
|
2014-03-09 19:16:05 +05:30
|
|
|
};
|
2022-09-24 23:42:02 +05:30
|
|
|
return x ? ctzt[((x & -x) * 0x077cb531u) >> 27] : 32;
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// sn must be in network order
|
|
|
|
static inline int subnet4_to_prefixlen(uint32_t sn)
|
|
|
|
{
|
2014-03-15 08:52:23 +05:30
|
|
|
return 32 - trailz(ntohl(sn));
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
|
|
|
|
2014-03-15 12:14:43 +05:30
|
|
|
struct ipbcpfx {
|
|
|
|
int fd;
|
|
|
|
uint32_t ipaddr;
|
|
|
|
uint32_t bcast;
|
|
|
|
uint8_t prefixlen;
|
|
|
|
bool already_ok;
|
|
|
|
};
|
|
|
|
|
2014-04-07 13:45:02 +05:30
|
|
|
static ssize_t rtnl_do_send(int fd, const uint8_t *sbuf, size_t slen,
|
2014-03-16 14:40:21 +05:30
|
|
|
const char *fnname)
|
|
|
|
{
|
|
|
|
uint8_t response[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + 64];
|
|
|
|
struct sockaddr_nl nl_addr;
|
|
|
|
|
|
|
|
memset(&nl_addr, 0, sizeof nl_addr);
|
|
|
|
nl_addr.nl_family = AF_NETLINK;
|
|
|
|
|
2014-04-07 13:45:02 +05:30
|
|
|
ssize_t r = safe_sendto(fd, (const char *)sbuf, slen, 0,
|
|
|
|
(struct sockaddr *)&nl_addr, sizeof nl_addr);
|
|
|
|
if (r < 0 || (size_t)r != slen) {
|
|
|
|
if (r < 0)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink sendto failed: %s",
|
|
|
|
client_config.interface, fnname, strerror(errno));
|
2014-04-07 13:45:02 +05:30
|
|
|
else
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink sendto short write: %zd < %zu",
|
|
|
|
client_config.interface, fnname, r, slen);
|
2014-04-07 13:45:02 +05:30
|
|
|
return -1;
|
2014-03-16 14:40:21 +05:30
|
|
|
}
|
2014-03-17 09:09:24 +05:30
|
|
|
struct iovec iov = {
|
|
|
|
.iov_base = response,
|
|
|
|
.iov_len = sizeof response,
|
|
|
|
};
|
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_name = &nl_addr,
|
|
|
|
.msg_namelen = sizeof nl_addr,
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
};
|
2014-04-07 13:52:32 +05:30
|
|
|
r = safe_recvmsg(fd, &msg, 0);
|
2014-03-16 14:40:21 +05:30
|
|
|
if (r < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink recvmsg failed: %s",
|
|
|
|
client_config.interface, fnname, strerror(errno));
|
2014-04-07 13:52:32 +05:30
|
|
|
return -1;
|
2014-03-16 14:40:21 +05:30
|
|
|
}
|
2014-03-17 09:09:24 +05:30
|
|
|
if (msg.msg_flags & MSG_TRUNC) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Buffer not long enough for message.",
|
|
|
|
client_config.interface, fnname);
|
2014-03-17 09:09:24 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-16 14:40:21 +05:30
|
|
|
if ((size_t)r < sizeof(struct nlmsghdr)) {
|
2014-03-17 09:09:24 +05:30
|
|
|
log_line("%s: (%s) netlink recvmsg returned a headerless response",
|
2014-03-16 14:40:21 +05:30
|
|
|
client_config.interface, fnname);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-17 09:09:24 +05:30
|
|
|
const struct nlmsghdr *nlh = (const struct nlmsghdr *)response;
|
2014-03-17 11:37:30 +05:30
|
|
|
if (nlh->nlmsg_type == NLMSG_ERROR) {
|
2015-02-14 04:20:24 +05:30
|
|
|
int nlerr = nlmsg_get_error(nlh);
|
|
|
|
if (nlerr == 0)
|
2014-03-17 11:37:30 +05:30
|
|
|
return 0;
|
|
|
|
else {
|
2015-02-14 04:20:24 +05:30
|
|
|
if (nlerr == 132) {
|
|
|
|
log_line("%s: (%s) RF-kill is set (%d). Cannot change interface.",
|
|
|
|
client_config.interface, fnname, nlerr);
|
|
|
|
return -3;
|
|
|
|
}
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink sendto returned NLMSG_ERROR: %s",
|
|
|
|
client_config.interface, fnname, strerror(nlerr));
|
2014-03-17 11:37:30 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-16 14:40:21 +05:30
|
|
|
}
|
2014-03-17 15:11:48 +05:30
|
|
|
if (nlh->nlmsg_type == NLMSG_DONE)
|
|
|
|
return -2;
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink sendto returned an error.",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 11:37:30 +05:30
|
|
|
return -1;
|
2014-03-16 14:40:21 +05:30
|
|
|
}
|
|
|
|
|
2018-02-09 13:09:46 +05:30
|
|
|
static ssize_t rtnl_if_flags_send(int fd, int type, unsigned ifi_flags)
|
2014-03-16 14:40:21 +05:30
|
|
|
{
|
|
|
|
uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
|
2014-03-17 11:37:30 +05:30
|
|
|
NLMSG_ALIGN(sizeof(struct ifinfomsg))];
|
2014-03-16 14:40:21 +05:30
|
|
|
struct nlmsghdr *header;
|
|
|
|
struct ifinfomsg *ifinfomsg;
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof request);
|
|
|
|
header = (struct nlmsghdr *)request;
|
|
|
|
header->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
|
|
|
|
header->nlmsg_type = type;
|
2014-03-17 11:37:30 +05:30
|
|
|
header->nlmsg_flags = NLM_F_ACK | NLM_F_REQUEST;
|
2014-03-16 14:40:21 +05:30
|
|
|
header->nlmsg_seq = ifset_nl_seq++;
|
|
|
|
|
|
|
|
ifinfomsg = NLMSG_DATA(header);
|
|
|
|
ifinfomsg->ifi_flags = ifi_flags;
|
|
|
|
ifinfomsg->ifi_index = client_config.ifindex;
|
|
|
|
ifinfomsg->ifi_change = 0xffffffff;
|
|
|
|
|
|
|
|
return rtnl_do_send(fd, request, header->nlmsg_len, __func__);
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:14:43 +05:30
|
|
|
static ssize_t rtnl_addr_broadcast_send(int fd, int type, int ifa_flags,
|
2014-03-15 14:13:29 +05:30
|
|
|
int ifa_scope, uint32_t *ipaddr,
|
|
|
|
uint32_t *bcast, uint8_t prefixlen)
|
2014-03-09 19:16:05 +05:30
|
|
|
{
|
|
|
|
uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
|
2014-03-17 11:37:30 +05:30
|
|
|
NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
|
|
|
|
2 * RTA_LENGTH(sizeof(struct in6_addr))];
|
2014-03-09 19:16:05 +05:30
|
|
|
struct nlmsghdr *header;
|
|
|
|
struct ifaddrmsg *ifaddrmsg;
|
|
|
|
|
2014-03-15 12:14:43 +05:30
|
|
|
if (!ipaddr && !bcast) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) no ipaddr or bcast!",
|
|
|
|
client_config.interface, __func__);
|
2014-03-15 12:14:43 +05:30
|
|
|
return -1;
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof request);
|
|
|
|
header = (struct nlmsghdr *)request;
|
|
|
|
header->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
|
2014-03-15 12:14:43 +05:30
|
|
|
header->nlmsg_type = type;
|
2014-03-09 19:16:05 +05:30
|
|
|
header->nlmsg_flags = NLM_F_REPLACE | NLM_F_ACK | NLM_F_REQUEST;
|
2014-03-15 12:14:43 +05:30
|
|
|
header->nlmsg_seq = ifset_nl_seq++;
|
2014-03-09 19:16:05 +05:30
|
|
|
|
|
|
|
ifaddrmsg = NLMSG_DATA(header);
|
|
|
|
ifaddrmsg->ifa_family = AF_INET;
|
|
|
|
ifaddrmsg->ifa_prefixlen = prefixlen;
|
2014-03-15 12:14:43 +05:30
|
|
|
ifaddrmsg->ifa_flags = ifa_flags;
|
|
|
|
ifaddrmsg->ifa_scope = ifa_scope;
|
2018-02-09 13:09:46 +05:30
|
|
|
// Linux is inconsistent about the type of ifindex.
|
|
|
|
ifaddrmsg->ifa_index = (uint32_t)client_config.ifindex;
|
2014-03-09 19:16:05 +05:30
|
|
|
|
2014-03-15 12:14:43 +05:30
|
|
|
if (ipaddr) {
|
|
|
|
if (nl_add_rtattr(header, sizeof request, IFA_LOCAL,
|
2014-03-15 14:13:29 +05:30
|
|
|
ipaddr, sizeof *ipaddr) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add IFA_LOCAL to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-15 12:14:43 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
2014-03-15 12:14:43 +05:30
|
|
|
if (bcast) {
|
|
|
|
if (nl_add_rtattr(header, sizeof request, IFA_BROADCAST,
|
2014-03-15 14:13:29 +05:30
|
|
|
bcast, sizeof *bcast) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add IFA_BROADCAST to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-15 12:14:43 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
|
|
|
|
2014-03-16 14:40:21 +05:30
|
|
|
return rtnl_do_send(fd, request, header->nlmsg_len, __func__);
|
|
|
|
}
|
2014-03-09 19:16:05 +05:30
|
|
|
|
2014-03-17 06:35:50 +05:30
|
|
|
static ssize_t rtnl_set_default_gw_v4(int fd, uint32_t gw4, int metric)
|
|
|
|
{
|
|
|
|
uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
|
2014-03-17 11:37:30 +05:30
|
|
|
NLMSG_ALIGN(sizeof(struct rtmsg)) +
|
|
|
|
3 * RTA_LENGTH(sizeof(struct in6_addr)) +
|
|
|
|
RTA_LENGTH(sizeof(int))];
|
2014-03-17 06:35:50 +05:30
|
|
|
struct nlmsghdr *header;
|
|
|
|
struct rtmsg *rtmsg;
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof request);
|
|
|
|
header = (struct nlmsghdr *)request;
|
|
|
|
header->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
header->nlmsg_type = RTM_NEWROUTE;
|
2014-03-17 11:37:30 +05:30
|
|
|
header->nlmsg_flags = NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK
|
|
|
|
| NLM_F_REQUEST;
|
2014-03-17 06:35:50 +05:30
|
|
|
header->nlmsg_seq = ifset_nl_seq++;
|
|
|
|
|
|
|
|
rtmsg = NLMSG_DATA(header);
|
|
|
|
rtmsg->rtm_family = AF_INET;
|
2014-03-17 11:37:30 +05:30
|
|
|
rtmsg->rtm_protocol = RTPROT_DHCP;
|
2014-03-17 06:35:50 +05:30
|
|
|
rtmsg->rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
rtmsg->rtm_type = RTN_UNICAST;
|
|
|
|
|
|
|
|
uint32_t dstaddr4 = 0;
|
|
|
|
if (nl_add_rtattr(header, sizeof request, RTA_DST,
|
|
|
|
&dstaddr4, sizeof dstaddr4) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add RTA_DST to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 06:35:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nl_add_rtattr(header, sizeof request, RTA_OIF,
|
|
|
|
&client_config.ifindex,
|
|
|
|
sizeof client_config.ifindex) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add RTA_OIF to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 06:35:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nl_add_rtattr(header, sizeof request, RTA_GATEWAY,
|
|
|
|
&gw4, sizeof gw4) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add RTA_GATEWAY to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 06:35:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-17 11:37:30 +05:30
|
|
|
if (metric > 0) {
|
|
|
|
if (nl_add_rtattr(header, sizeof request, RTA_PRIORITY,
|
|
|
|
&metric, sizeof metric) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add RTA_PRIORITY to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 11:37:30 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-17 06:35:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return rtnl_do_send(fd, request, header->nlmsg_len, __func__);
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:40:21 +05:30
|
|
|
struct link_flag_data {
|
|
|
|
int fd;
|
|
|
|
uint32_t flags;
|
|
|
|
bool got_flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void link_flags_get_do(const struct nlmsghdr *nlh, void *data)
|
|
|
|
{
|
2014-03-18 05:56:37 +05:30
|
|
|
struct ifinfomsg *ifm = NLMSG_DATA(nlh);
|
2014-03-16 14:40:21 +05:30
|
|
|
struct link_flag_data *ifd = data;
|
|
|
|
|
|
|
|
switch(nlh->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
if (ifm->ifi_index != client_config.ifindex)
|
|
|
|
break;
|
|
|
|
ifd->flags = ifm->ifi_flags;
|
|
|
|
ifd->got_flags = true;
|
|
|
|
break;
|
|
|
|
case RTM_DELLINK:
|
|
|
|
log_line("%s: got RTM_DELLINK", __func__);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_line("%s: got %u", __func__, nlh->nlmsg_type);
|
|
|
|
break;
|
2014-03-15 08:58:56 +05:30
|
|
|
}
|
2014-03-16 14:40:21 +05:30
|
|
|
}
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
static int link_flags_get(int fd, uint32_t *flags)
|
2014-03-16 14:40:21 +05:30
|
|
|
{
|
|
|
|
char nlbuf[8192];
|
|
|
|
struct link_flag_data ipx = { .fd = fd, .flags = 0, .got_flags = false };
|
|
|
|
ssize_t ret;
|
|
|
|
uint32_t seq = ifset_nl_seq++;
|
|
|
|
if (nl_sendgetlink(fd, seq, client_config.ifindex) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (ret < 0)
|
2014-03-16 14:40:21 +05:30
|
|
|
return -2;
|
2018-02-09 13:09:46 +05:30
|
|
|
if (nl_foreach_nlmsg(nlbuf, (size_t)ret, seq, 0, link_flags_get_do, &ipx) < 0)
|
2014-03-16 14:40:21 +05:30
|
|
|
return -3;
|
|
|
|
} while (ret > 0);
|
|
|
|
if (ipx.got_flags) {
|
|
|
|
*flags = ipx.flags;
|
|
|
|
return 0;
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
2014-03-16 14:40:21 +05:30
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2015-02-15 13:20:29 +05:30
|
|
|
int perform_carrier(void)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
uint32_t flags;
|
2022-08-13 10:12:27 +05:30
|
|
|
int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2015-02-15 13:20:29 +05:30
|
|
|
if (fd < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2015-02-15 13:20:29 +05:30
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link_flags_get(fd, &flags) < 0)
|
2015-07-03 10:19:13 +05:30
|
|
|
goto fail_fd;
|
2015-02-15 13:20:29 +05:30
|
|
|
if ((flags & IFF_RUNNING) && (flags & IFF_UP))
|
|
|
|
ret = 0;
|
|
|
|
fail_fd:
|
|
|
|
close(fd);
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:52:22 +05:30
|
|
|
// Return 0 if flags were successfully changed.
|
|
|
|
// Return 1 if flags were already set.
|
|
|
|
// Return -1 on error.
|
|
|
|
// Return -2 if NL response had no status notification.
|
|
|
|
// Return -3 if RFKILL is set and flags cannot be changed.
|
2014-03-16 14:40:21 +05:30
|
|
|
static int link_set_flags(int fd, uint32_t flags)
|
|
|
|
{
|
|
|
|
uint32_t oldflags;
|
|
|
|
|
|
|
|
int r = link_flags_get(fd, &oldflags);
|
|
|
|
if (r < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) failed to get old link flags: %u",
|
|
|
|
client_config.interface, __func__, r);
|
2014-03-15 12:14:43 +05:30
|
|
|
return -1;
|
2014-03-15 08:58:56 +05:30
|
|
|
}
|
2014-03-17 15:05:34 +05:30
|
|
|
if ((oldflags & flags) == flags)
|
|
|
|
return 1;
|
2014-03-16 14:40:21 +05:30
|
|
|
return (int)rtnl_if_flags_send(fd, RTM_SETLINK, flags | oldflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int link_unset_flags(int fd, uint32_t flags)
|
|
|
|
{
|
|
|
|
uint32_t oldflags;
|
|
|
|
|
|
|
|
int r = link_flags_get(fd, &oldflags);
|
|
|
|
if (r < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) failed to get old link flags: %u",
|
|
|
|
client_config.interface, __func__, r);
|
2014-03-15 12:14:43 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-03-17 15:05:34 +05:30
|
|
|
if ((oldflags & flags) == 0)
|
|
|
|
return 1;
|
2014-03-16 14:40:21 +05:30
|
|
|
return (int)rtnl_if_flags_send(fd, RTM_SETLINK, oldflags & ~flags);
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
2014-03-16 14:40:21 +05:30
|
|
|
#endif
|
2014-03-15 12:14:43 +05:30
|
|
|
|
|
|
|
static void ipbcpfx_clear_others_do(const struct nlmsghdr *nlh, void *data)
|
|
|
|
{
|
|
|
|
struct rtattr *tb[IFA_MAX] = {0};
|
2014-03-18 05:56:37 +05:30
|
|
|
struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
|
2014-03-15 12:14:43 +05:30
|
|
|
struct ipbcpfx *ipx = data;
|
|
|
|
int r;
|
|
|
|
|
2014-03-15 14:13:29 +05:30
|
|
|
nl_rtattr_parse(nlh, sizeof *ifm, rtattr_assign, tb);
|
2014-03-15 12:14:43 +05:30
|
|
|
switch(nlh->nlmsg_type) {
|
|
|
|
case RTM_NEWADDR:
|
2014-03-17 03:36:42 +05:30
|
|
|
if (ifm->ifa_index != (unsigned)client_config.ifindex)
|
2014-03-15 12:14:43 +05:30
|
|
|
return;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (ifm->ifa_family != AF_INET)
|
2014-03-15 12:14:43 +05:30
|
|
|
return;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (!(ifm->ifa_flags & IFA_F_PERMANENT))
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (ifm->ifa_scope != RT_SCOPE_UNIVERSE)
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (ifm->ifa_prefixlen != ipx->prefixlen)
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (!tb[IFA_ADDRESS])
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-18 05:58:26 +05:30
|
|
|
if (memcmp(RTA_DATA(tb[IFA_ADDRESS]), &ipx->ipaddr,
|
2014-03-15 14:13:29 +05:30
|
|
|
sizeof ipx->ipaddr))
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-15 14:13:29 +05:30
|
|
|
if (!tb[IFA_BROADCAST])
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
2014-03-18 05:58:26 +05:30
|
|
|
if (memcmp(RTA_DATA(tb[IFA_BROADCAST]), &ipx->bcast,
|
2014-03-15 14:13:29 +05:30
|
|
|
sizeof ipx->bcast))
|
2014-03-15 12:14:43 +05:30
|
|
|
goto erase;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We already have the proper IP+broadcast+prefix.
|
|
|
|
ipx->already_ok = true;
|
|
|
|
return;
|
|
|
|
|
|
|
|
erase:
|
|
|
|
r = rtnl_addr_broadcast_send(ipx->fd, RTM_DELADDR, ifm->ifa_flags,
|
|
|
|
ifm->ifa_scope,
|
2018-10-26 16:47:39 +05:30
|
|
|
tb[IFA_ADDRESS] ? RTA_DATA(tb[IFA_ADDRESS]) : (uint32_t *)0,
|
|
|
|
tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : (uint32_t *)0,
|
2014-03-15 12:14:43 +05:30
|
|
|
ifm->ifa_prefixlen);
|
2014-03-17 15:11:48 +05:30
|
|
|
if (r < 0 && r != -2) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to delete IP and broadcast addresses.",
|
|
|
|
client_config.interface, __func__);
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipbcpfx_clear_others(int fd, uint32_t ipaddr, uint32_t bcast,
|
|
|
|
uint8_t prefixlen)
|
|
|
|
{
|
|
|
|
char nlbuf[8192];
|
|
|
|
struct ipbcpfx ipx = { .fd = fd, .ipaddr = ipaddr, .bcast = bcast,
|
|
|
|
.prefixlen = prefixlen, .already_ok = false };
|
|
|
|
ssize_t ret;
|
2014-03-15 14:13:29 +05:30
|
|
|
uint32_t seq = ifset_nl_seq++;
|
2018-02-09 13:09:46 +05:30
|
|
|
if (nl_sendgetaddr4(fd, seq, (uint32_t)client_config.ifindex) < 0)
|
2014-03-15 14:13:29 +05:30
|
|
|
return -1;
|
2014-03-15 12:14:43 +05:30
|
|
|
|
|
|
|
do {
|
|
|
|
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (ret < 0)
|
2014-03-15 14:13:29 +05:30
|
|
|
return -2;
|
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
|
|
|
ipbcpfx_clear_others_do, &ipx) < 0)
|
2014-03-15 14:13:29 +05:30
|
|
|
return -3;
|
2014-03-15 12:14:43 +05:30
|
|
|
} while (ret > 0);
|
2014-03-15 14:13:29 +05:30
|
|
|
return ipx.already_ok ? 1 : 0;
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
|
2014-03-17 12:46:02 +05:30
|
|
|
static ssize_t rtnl_if_mtu_set(int fd, unsigned int mtu)
|
|
|
|
{
|
|
|
|
uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
|
|
|
|
NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
|
|
|
|
RTA_LENGTH(sizeof(unsigned int))];
|
|
|
|
struct nlmsghdr *header;
|
|
|
|
struct ifinfomsg *ifinfomsg;
|
|
|
|
uint32_t oldflags;
|
|
|
|
|
|
|
|
int r = link_flags_get(fd, &oldflags);
|
|
|
|
if (r < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) failed to get old link flags: %u",
|
|
|
|
client_config.interface, __func__, r);
|
2014-03-17 12:46:02 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof request);
|
|
|
|
header = (struct nlmsghdr *)request;
|
|
|
|
header->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
|
|
|
|
header->nlmsg_type = RTM_SETLINK;
|
|
|
|
header->nlmsg_flags = NLM_F_ACK | NLM_F_REQUEST;
|
|
|
|
header->nlmsg_seq = ifset_nl_seq++;
|
|
|
|
|
|
|
|
ifinfomsg = NLMSG_DATA(header);
|
|
|
|
ifinfomsg->ifi_flags = oldflags;
|
|
|
|
ifinfomsg->ifi_index = client_config.ifindex;
|
|
|
|
ifinfomsg->ifi_change = 0xffffffff;
|
|
|
|
|
|
|
|
if (nl_add_rtattr(header, sizeof request, IFLA_MTU,
|
|
|
|
&mtu, sizeof mtu) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) couldn't add IFLA_MTU to nlmsg",
|
|
|
|
client_config.interface, __func__);
|
2014-03-17 12:46:02 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtnl_do_send(fd, request, header->nlmsg_len, __func__);
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:26:30 +05:30
|
|
|
int perform_ifup(void)
|
|
|
|
{
|
2022-08-13 10:12:27 +05:30
|
|
|
int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2014-03-17 15:26:30 +05:30
|
|
|
if (fd < 0) {
|
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = link_set_flags(fd, IFF_UP);
|
2015-02-14 04:20:24 +05:30
|
|
|
if (r < 0) {
|
|
|
|
if (r != -3)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to set link to be up.",
|
|
|
|
client_config.interface, __func__);
|
2015-02-14 04:20:24 +05:30
|
|
|
else
|
|
|
|
log_line("%s: (%s) rfkill is set; waiting until it is unset",
|
|
|
|
client_config.interface, __func__);
|
|
|
|
}
|
2014-03-17 15:26:30 +05:30
|
|
|
close(fd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:14:43 +05:30
|
|
|
// str_bcast is optional.
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_ip_subnet_bcast(const char *str_ipaddr,
|
|
|
|
const char *str_subnet,
|
2015-02-15 12:59:37 +05:30
|
|
|
const char *str_bcast)
|
2014-03-15 12:14:43 +05:30
|
|
|
{
|
|
|
|
struct in_addr ipaddr, subnet, bcast;
|
2015-07-03 10:19:13 +05:30
|
|
|
int fd, r, ret = -99;
|
2014-03-15 12:14:43 +05:30
|
|
|
uint8_t prefixlen;
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, str_ipaddr, &ipaddr) <= 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) bad interface ip address: '%s'",
|
|
|
|
client_config.interface, __func__, str_ipaddr);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, str_subnet, &subnet) <= 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) bad interface subnet address: '%s'",
|
|
|
|
client_config.interface, __func__, str_subnet);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
prefixlen = subnet4_to_prefixlen(subnet.s_addr);
|
|
|
|
|
|
|
|
if (str_bcast) {
|
|
|
|
if (inet_pton(AF_INET, str_bcast, &bcast) <= 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) bad interface broadcast address: '%s'",
|
|
|
|
client_config.interface, __func__, str_bcast);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Generate the standard broadcast address if unspecified.
|
|
|
|
bcast.s_addr = ipaddr.s_addr | htonl(0xfffffffflu >> prefixlen);
|
|
|
|
}
|
|
|
|
|
2022-08-13 10:12:27 +05:30
|
|
|
fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2014-03-15 12:14:43 +05:30
|
|
|
if (fd < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-15 08:58:56 +05:30
|
|
|
}
|
2014-03-15 12:14:43 +05:30
|
|
|
|
|
|
|
r = ipbcpfx_clear_others(fd, ipaddr.s_addr, bcast.s_addr, prefixlen);
|
2014-03-15 14:13:29 +05:30
|
|
|
if (r < 0 && r > -3) {
|
2014-03-17 03:36:42 +05:30
|
|
|
if (r == -1)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) error requesting link ip address list",
|
|
|
|
client_config.interface, __func__);
|
2014-03-15 14:13:29 +05:30
|
|
|
else if (r == -2)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) error receiving link ip address list",
|
|
|
|
client_config.interface, __func__);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail_fd;
|
2014-03-15 12:14:43 +05:30
|
|
|
}
|
|
|
|
|
2014-03-15 14:13:29 +05:30
|
|
|
if (r < 1) {
|
|
|
|
r = rtnl_addr_broadcast_send(fd, RTM_NEWADDR, IFA_F_PERMANENT,
|
|
|
|
RT_SCOPE_UNIVERSE, &ipaddr.s_addr, &bcast.s_addr,
|
|
|
|
prefixlen);
|
2015-02-15 12:59:37 +05:30
|
|
|
if (r < 0)
|
|
|
|
goto fail_fd;
|
2014-03-15 12:14:43 +05:30
|
|
|
|
2014-04-16 00:54:22 +05:30
|
|
|
log_line("%s: Interface IP set to: '%s'", client_config.interface,
|
|
|
|
str_ipaddr);
|
|
|
|
log_line("%s: Interface subnet set to: '%s'", client_config.interface,
|
|
|
|
str_subnet);
|
2014-03-15 14:13:29 +05:30
|
|
|
if (str_bcast)
|
2014-04-16 00:54:22 +05:30
|
|
|
log_line("%s: Broadcast address set to: '%s'",
|
|
|
|
client_config.interface, str_bcast);
|
2014-03-15 14:13:29 +05:30
|
|
|
} else
|
2014-04-16 00:54:22 +05:30
|
|
|
log_line("%s: Interface IP, subnet, and broadcast were already OK.",
|
|
|
|
client_config.interface);
|
2014-03-09 19:16:05 +05:30
|
|
|
|
2015-02-15 12:59:37 +05:30
|
|
|
if (link_set_flags(fd, IFF_UP | IFF_RUNNING) < 0) {
|
2015-07-03 10:19:13 +05:30
|
|
|
ret = -1;
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to set link to be up and running.",
|
|
|
|
client_config.interface, __func__);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail_fd;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
fail_fd:
|
2014-03-16 14:40:21 +05:30
|
|
|
close(fd);
|
2015-02-15 12:59:37 +05:30
|
|
|
fail:
|
|
|
|
return ret;
|
2014-03-09 19:16:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_router(const char *str_router, size_t len)
|
2010-11-12 14:32:18 +05:30
|
|
|
{
|
2015-07-03 10:19:13 +05:30
|
|
|
int ret = -99;
|
2014-03-17 06:35:50 +05:30
|
|
|
if (len < 7)
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 06:35:50 +05:30
|
|
|
struct in_addr router;
|
|
|
|
if (inet_pton(AF_INET, str_router, &router) <= 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) bad router ip address: '%s'",
|
|
|
|
client_config.interface, __func__, str_router);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 06:35:50 +05:30
|
|
|
}
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2022-08-13 10:12:27 +05:30
|
|
|
int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2014-03-17 06:35:50 +05:30
|
|
|
if (fd < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
2014-03-17 06:35:50 +05:30
|
|
|
|
2015-02-15 12:59:37 +05:30
|
|
|
if (rtnl_set_default_gw_v4(fd, router.s_addr, client_config.metric) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) failed to set route: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail_fd;
|
|
|
|
}
|
|
|
|
log_line("%s: Gateway router set to: '%s'", client_config.interface,
|
|
|
|
str_router);
|
|
|
|
ret = 0;
|
|
|
|
fail_fd:
|
2010-11-12 14:32:18 +05:30
|
|
|
close(fd);
|
2015-02-15 12:59:37 +05:30
|
|
|
fail:
|
|
|
|
return ret;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_mtu(const char *str, size_t len)
|
2010-11-12 14:32:18 +05:30
|
|
|
{
|
2015-02-15 12:59:37 +05:30
|
|
|
unsigned int mtu;
|
2015-07-03 10:19:13 +05:30
|
|
|
int fd, ret = -99;
|
2014-03-11 05:01:46 +05:30
|
|
|
if (len < 2)
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2014-03-17 03:37:09 +05:30
|
|
|
char *estr;
|
|
|
|
long tmtu = strtol(str, &estr, 10);
|
|
|
|
if (estr == str) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) provided mtu arg isn't a valid number",
|
|
|
|
client_config.interface, __func__);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 03:37:09 +05:30
|
|
|
}
|
|
|
|
if ((tmtu == LONG_MAX || tmtu == LONG_MIN) && errno == ERANGE) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) provided mtu arg would overflow a long",
|
|
|
|
client_config.interface, __func__);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 03:37:09 +05:30
|
|
|
}
|
2014-03-17 11:37:30 +05:30
|
|
|
if (tmtu > INT_MAX) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) provided mtu arg would overflow int",
|
|
|
|
client_config.interface, __func__);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 03:37:09 +05:30
|
|
|
}
|
|
|
|
// 68 bytes for IPv4. 1280 bytes for IPv6.
|
|
|
|
if (tmtu < 68) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) provided mtu arg (%ld) less than minimum MTU (68)",
|
|
|
|
client_config.interface, __func__, tmtu);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2014-03-17 03:37:09 +05:30
|
|
|
}
|
2015-02-15 12:59:37 +05:30
|
|
|
mtu = (unsigned int)tmtu;
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2022-08-13 10:12:27 +05:30
|
|
|
fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2014-03-17 03:37:09 +05:30
|
|
|
if (fd < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) netlink socket open failed: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
2014-03-17 03:37:09 +05:30
|
|
|
|
2015-02-15 12:59:37 +05:30
|
|
|
if (rtnl_if_mtu_set(fd, mtu) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) failed to set MTU [%d]",
|
|
|
|
client_config.interface, __func__, mtu);
|
2015-02-15 12:59:37 +05:30
|
|
|
goto fail_fd;
|
|
|
|
}
|
|
|
|
log_line("%s: MTU set to: '%s'", client_config.interface, str);
|
|
|
|
ret = 0;
|
|
|
|
fail_fd:
|
2010-11-12 14:32:18 +05:30
|
|
|
close(fd);
|
2015-02-15 12:59:37 +05:30
|
|
|
fail:
|
|
|
|
return ret;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|