2018-05-24 22:22:11 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2018-05-24 22:22:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-01-14 18:25:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-24 22:22:11 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LinuxNetLink.hpp"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2018-05-25 22:37:00 +00:00
|
|
|
#include <linux/if_tun.h>
|
|
|
|
|
2018-05-24 22:22:11 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
struct nl_route_req {
|
2019-03-25 22:53:06 +00:00
|
|
|
struct nlmsghdr nl;
|
|
|
|
struct rtmsg rt;
|
|
|
|
char buf[8192];
|
2018-05-25 21:18:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nl_if_req {
|
2019-03-25 22:53:06 +00:00
|
|
|
struct nlmsghdr nl;
|
|
|
|
struct ifinfomsg ifa;
|
|
|
|
char buf[8192];
|
2018-05-25 21:18:06 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 22:37:00 +00:00
|
|
|
struct nl_adr_req {
|
|
|
|
struct nlmsghdr nl;
|
|
|
|
struct ifaddrmsg ifa;
|
|
|
|
char buf[8192];
|
|
|
|
};
|
|
|
|
|
2018-05-24 22:22:11 +00:00
|
|
|
LinuxNetLink::LinuxNetLink()
|
2018-05-25 21:18:06 +00:00
|
|
|
: _t()
|
|
|
|
, _running(false)
|
|
|
|
, _routes_ipv4()
|
2018-06-04 19:24:12 +00:00
|
|
|
, _rv4_m()
|
2018-05-25 21:18:06 +00:00
|
|
|
, _routes_ipv6()
|
2018-06-04 19:24:12 +00:00
|
|
|
, _rv6_m()
|
2018-05-25 21:18:06 +00:00
|
|
|
, _seq(0)
|
2018-06-04 19:24:12 +00:00
|
|
|
, _interfaces()
|
|
|
|
, _if_m()
|
2018-05-25 21:18:06 +00:00
|
|
|
, _fd(socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE))
|
|
|
|
, _la({0})
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
// set socket timeout to 1 sec so we're not permablocking recv() calls
|
2018-06-04 19:24:12 +00:00
|
|
|
_setSocketTimeout(_fd, 1);
|
2018-05-25 21:18:06 +00:00
|
|
|
|
|
|
|
_la.nl_family = AF_NETLINK;
|
2018-06-04 19:24:12 +00:00
|
|
|
_la.nl_pid = getpid()+1;
|
2018-05-25 21:18:06 +00:00
|
|
|
_la.nl_groups = RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR|RTMGRP_IPV4_ROUTE|RTMGRP_IPV6_ROUTE|RTMGRP_NOTIFY;
|
|
|
|
if (bind(_fd, (struct sockaddr*)&_la, sizeof(_la))) {
|
|
|
|
fprintf(stderr, "Error connecting to RTNETLINK: %s\n", strerror(errno));
|
|
|
|
::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestIPv4Routes();
|
|
|
|
_requestIPv6Routes();
|
|
|
|
_requestInterfaceList();
|
2018-06-04 19:24:12 +00:00
|
|
|
|
|
|
|
_running = true;
|
|
|
|
_t = Thread::start(this);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LinuxNetLink::~LinuxNetLink()
|
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
_running = false;
|
|
|
|
Thread::join(_t);
|
|
|
|
::close(_fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
void LinuxNetLink::_setSocketTimeout(int fd, int seconds)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) != 0) {
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2018-06-04 19:24:12 +00:00
|
|
|
fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-06-04 19:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:53:06 +00:00
|
|
|
#define ZT_NL_BUF_SIZE 16384
|
2018-06-04 19:24:12 +00:00
|
|
|
int LinuxNetLink::_doRecv(int fd)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2019-03-25 22:53:06 +00:00
|
|
|
char *const buf = (char *)valloc(ZT_NL_BUF_SIZE);
|
2019-03-25 22:19:52 +00:00
|
|
|
if (!buf) {
|
|
|
|
fprintf(stderr,"malloc failed!\n");
|
|
|
|
::exit(1);
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
char *p = NULL;
|
|
|
|
struct nlmsghdr *nlp;
|
|
|
|
int nll = 0;
|
|
|
|
int rtn = 0;
|
|
|
|
p = buf;
|
|
|
|
|
2019-03-25 22:19:52 +00:00
|
|
|
for(;;) {
|
2019-03-25 22:53:06 +00:00
|
|
|
rtn = recv(fd, p, ZT_NL_BUF_SIZE - nll, 0);
|
2018-05-25 21:18:06 +00:00
|
|
|
|
|
|
|
if (rtn > 0) {
|
|
|
|
nlp = (struct nlmsghdr *)p;
|
|
|
|
|
|
|
|
if(nlp->nlmsg_type == NLMSG_ERROR && (nlp->nlmsg_flags & NLM_F_ACK) != NLM_F_ACK) {
|
|
|
|
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(nlp);
|
|
|
|
if (err->error != 0) {
|
2019-03-25 22:19:52 +00:00
|
|
|
//#ifdef ZT_TRACE
|
2018-05-25 21:18:06 +00:00
|
|
|
fprintf(stderr, "rtnetlink error: %s\n", strerror(-(err->error)));
|
2019-03-25 22:19:52 +00:00
|
|
|
//#endif
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
nll = 0;
|
2018-06-04 19:24:12 +00:00
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nlp->nlmsg_type == NLMSG_NOOP) {
|
2018-06-04 19:24:12 +00:00
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( (nlp->nlmsg_flags & NLM_F_MULTI) == NLM_F_MULTI || (nlp->nlmsg_type == NLMSG_DONE))
|
|
|
|
{
|
|
|
|
if (nlp->nlmsg_type == NLMSG_DONE) {
|
|
|
|
_processMessage(nlp, nll);
|
|
|
|
p = buf;
|
|
|
|
nll = 0;
|
2018-06-04 19:24:12 +00:00
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
p += rtn;
|
|
|
|
nll += rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nlp->nlmsg_type == NLMSG_OVERRUN) {
|
2019-03-25 22:19:52 +00:00
|
|
|
//#ifdef ZT_TRACE
|
2018-05-25 21:18:06 +00:00
|
|
|
fprintf(stderr, "NLMSG_OVERRUN: Data lost\n");
|
2019-03-25 22:19:52 +00:00
|
|
|
//#endif
|
2018-05-25 21:18:06 +00:00
|
|
|
p = buf;
|
|
|
|
nll = 0;
|
2018-06-04 19:24:12 +00:00
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nll += rtn;
|
|
|
|
|
|
|
|
_processMessage(nlp, nll);
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
p = buf;
|
|
|
|
nll = 0;
|
2018-06-04 19:24:12 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
2018-06-04 19:24:12 +00:00
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxNetLink::threadMain() throw()
|
|
|
|
{
|
|
|
|
int rtn = 0;
|
|
|
|
|
|
|
|
while(_running) {
|
|
|
|
rtn = _doRecv(_fd);
|
|
|
|
if (rtn <= 0) {
|
2018-05-25 21:18:06 +00:00
|
|
|
Thread::sleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_processMessage(struct nlmsghdr *nlp, int nll)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
for(; NLMSG_OK(nlp, nll); nlp=NLMSG_NEXT(nlp, nll))
|
|
|
|
{
|
|
|
|
switch(nlp->nlmsg_type)
|
|
|
|
{
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
_linkAdded(nlp);
|
|
|
|
break;
|
|
|
|
case RTM_DELLINK:
|
|
|
|
_linkDeleted(nlp);
|
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
_ipAddressAdded(nlp);
|
|
|
|
break;
|
|
|
|
case RTM_DELADDR:
|
|
|
|
_ipAddressDeleted(nlp);
|
|
|
|
break;
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
_routeAdded(nlp);
|
|
|
|
break;
|
|
|
|
case RTM_DELROUTE:
|
|
|
|
_routeDeleted(nlp);
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-11 17:47:02 +00:00
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp);
|
|
|
|
struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap);
|
|
|
|
int ifal = IFA_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
char addr[40] = {0};
|
|
|
|
char local[40] = {0};
|
|
|
|
char label[40] = {0};
|
|
|
|
char bcast[40] = {0};
|
|
|
|
|
|
|
|
for(;RTA_OK(rtap, ifal); rtap=RTA_NEXT(rtap,ifal))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type) {
|
|
|
|
case IFA_ADDRESS:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), addr, 40);
|
|
|
|
break;
|
|
|
|
case IFA_LOCAL:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), local, 40);
|
|
|
|
break;
|
|
|
|
case IFA_LABEL:
|
|
|
|
memcpy(label, RTA_DATA(rtap), 40);
|
|
|
|
break;
|
|
|
|
case IFA_BROADCAST:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), bcast, 40);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//fprintf(stderr,"Added IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp);
|
|
|
|
struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap);
|
|
|
|
int ifal = IFA_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
char addr[40] = {0};
|
|
|
|
char local[40] = {0};
|
|
|
|
char label[40] = {0};
|
|
|
|
char bcast[40] = {0};
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
for(;RTA_OK(rtap, ifal); rtap=RTA_NEXT(rtap,ifal))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type) {
|
|
|
|
case IFA_ADDRESS:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), addr, 40);
|
|
|
|
break;
|
|
|
|
case IFA_LOCAL:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), local, 40);
|
|
|
|
break;
|
|
|
|
case IFA_LABEL:
|
|
|
|
memcpy(label, RTA_DATA(rtap), 40);
|
|
|
|
break;
|
|
|
|
case IFA_BROADCAST:
|
|
|
|
inet_ntop(ifap->ifa_family, RTA_DATA(rtap), bcast, 40);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//fprintf(stderr, "Removed IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
char dsts[40] = {0};
|
|
|
|
char gws[40] = {0};
|
2018-06-04 19:24:12 +00:00
|
|
|
char srcs[40] = {0};
|
2018-05-25 21:18:06 +00:00
|
|
|
char ifs[16] = {0};
|
|
|
|
char ms[24] = {0};
|
|
|
|
|
2019-03-25 22:19:52 +00:00
|
|
|
struct rtmsg *rtp = (struct rtmsg *)NLMSG_DATA(nlp);
|
2018-05-25 21:18:06 +00:00
|
|
|
struct rtattr *rtap = (struct rtattr *)RTM_RTA(rtp);
|
|
|
|
int rtl = RTM_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
for(;RTA_OK(rtap, rtl); rtap=RTA_NEXT(rtap, rtl))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type)
|
|
|
|
{
|
|
|
|
case RTA_DST:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), dsts, rtp->rtm_family == AF_INET ? 24 : 40);
|
|
|
|
break;
|
2018-06-04 19:24:12 +00:00
|
|
|
case RTA_SRC:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), srcs, rtp->rtm_family == AF_INET ? 24: 40);
|
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
case RTA_GATEWAY:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), gws, rtp->rtm_family == AF_INET ? 24 : 40);
|
|
|
|
break;
|
|
|
|
case RTA_OIF:
|
|
|
|
sprintf(ifs, "%d", *((int*)RTA_DATA(rtap)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprintf(ms, "%d", rtp->rtm_dst_len);
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//fprintf(stderr, "Route Added: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
char dsts[40] = {0};
|
|
|
|
char gws[40] = {0};
|
2018-06-04 19:24:12 +00:00
|
|
|
char srcs[40] = {0};
|
2018-05-25 21:18:06 +00:00
|
|
|
char ifs[16] = {0};
|
|
|
|
char ms[24] = {0};
|
|
|
|
|
|
|
|
struct rtmsg *rtp = (struct rtmsg *) NLMSG_DATA(nlp);
|
|
|
|
struct rtattr *rtap = (struct rtattr *)RTM_RTA(rtp);
|
|
|
|
int rtl = RTM_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
for(;RTA_OK(rtap, rtl); rtap=RTA_NEXT(rtap, rtl))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type)
|
|
|
|
{
|
|
|
|
case RTA_DST:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), dsts, rtp->rtm_family == AF_INET ? 24 : 40);
|
|
|
|
break;
|
2018-06-04 19:24:12 +00:00
|
|
|
case RTA_SRC:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), srcs, rtp->rtm_family == AF_INET ? 24 : 40);
|
|
|
|
break;
|
2018-05-25 21:18:06 +00:00
|
|
|
case RTA_GATEWAY:
|
|
|
|
inet_ntop(rtp->rtm_family, RTA_DATA(rtap), gws, rtp->rtm_family == AF_INET ? 24 : 40);
|
|
|
|
break;
|
|
|
|
case RTA_OIF:
|
|
|
|
sprintf(ifs, "%d", *((int*)RTA_DATA(rtap)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprintf(ms, "%d", rtp->rtm_dst_len);
|
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//fprintf(stderr, "Route Deleted: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_linkAdded(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:25:44 +00:00
|
|
|
char mac_bin[6] = {0};
|
2018-05-25 21:18:06 +00:00
|
|
|
unsigned int mtu = 0;
|
|
|
|
char ifname[IFNAMSIZ] = {0};
|
|
|
|
|
|
|
|
struct ifinfomsg *ifip = (struct ifinfomsg *)NLMSG_DATA(nlp);
|
|
|
|
struct rtattr *rtap = (struct rtattr *)IFLA_RTA(ifip);
|
|
|
|
int ifil = RTM_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
const char *ptr;
|
|
|
|
unsigned char *ptr2;
|
|
|
|
for(;RTA_OK(rtap, ifil);rtap=RTA_NEXT(rtap, ifil))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type) {
|
|
|
|
case IFLA_ADDRESS:
|
|
|
|
ptr2 = (unsigned char*)RTA_DATA(rtap);
|
2018-05-25 21:25:44 +00:00
|
|
|
memcpy(mac_bin, ptr, 6);
|
2018-05-25 21:18:06 +00:00
|
|
|
break;
|
|
|
|
case IFLA_IFNAME:
|
|
|
|
ptr = (const char*)RTA_DATA(rtap);
|
|
|
|
memcpy(ifname, ptr, strlen(ptr));
|
|
|
|
break;
|
|
|
|
case IFLA_MTU:
|
|
|
|
memcpy(&mtu, RTA_DATA(rtap), sizeof(unsigned int));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_if_m);
|
|
|
|
struct iface_entry &entry = _interfaces[ifip->ifi_index];
|
|
|
|
entry.index = ifip->ifi_index;
|
|
|
|
memcpy(entry.ifacename, ifname, sizeof(ifname));
|
|
|
|
memcpy(entry.mac, mac, sizeof(mac));
|
|
|
|
memcpy(entry.mac_bin, mac_bin, 6);
|
|
|
|
entry.mtu = mtu;
|
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:15:03 +00:00
|
|
|
void LinuxNetLink::_linkDeleted(struct nlmsghdr *nlp)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
unsigned int mtu = 0;
|
|
|
|
char ifname[40] = {0};
|
|
|
|
|
|
|
|
struct ifinfomsg *ifip = (struct ifinfomsg *)NLMSG_DATA(nlp);
|
|
|
|
struct rtattr *rtap = (struct rtattr *)IFLA_RTA(ifip);
|
|
|
|
int ifil = RTM_PAYLOAD(nlp);
|
|
|
|
|
|
|
|
const char *ptr;
|
|
|
|
unsigned char *ptr2;
|
|
|
|
for(;RTA_OK(rtap, ifil);rtap=RTA_NEXT(rtap, ifil))
|
|
|
|
{
|
|
|
|
switch(rtap->rta_type) {
|
|
|
|
case IFLA_ADDRESS:
|
|
|
|
ptr2 = (unsigned char*)RTA_DATA(rtap);
|
|
|
|
break;
|
|
|
|
case IFLA_IFNAME:
|
|
|
|
ptr = (const char*)RTA_DATA(rtap);
|
|
|
|
memcpy(ifname, ptr, strlen(ptr));
|
|
|
|
break;
|
|
|
|
case IFLA_MTU:
|
|
|
|
memcpy(&mtu, RTA_DATA(rtap), sizeof(unsigned int));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_if_m);
|
|
|
|
if(_interfaces.contains(ifip->ifi_index)) {
|
|
|
|
_interfaces.erase(ifip->ifi_index);
|
|
|
|
}
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxNetLink::_requestIPv4Routes()
|
|
|
|
{
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
la.nl_groups = RTMGRP_IPV4_ROUTE;
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(la))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (_requiestIPv4Routes #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
struct nl_route_req req;
|
|
|
|
bzero(&req, sizeof(req));
|
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
|
|
|
req.nl.nlmsg_type = RTM_GETROUTE;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.rt.rtm_family = AF_INET;
|
|
|
|
req.rt.rtm_table = RT_TABLE_MAIN;
|
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(pa));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
bzero(&iov, sizeof(iov));
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxNetLink::_requestIPv6Routes()
|
|
|
|
{
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
la.nl_groups = RTMGRP_IPV6_ROUTE;
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (_requestIPv6Routes #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
struct nl_route_req req;
|
|
|
|
bzero(&req, sizeof(req));
|
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
|
|
|
req.nl.nlmsg_type = RTM_GETROUTE;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.rt.rtm_family = AF_INET6;
|
|
|
|
req.rt.rtm_table = RT_TABLE_MAIN;
|
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(pa));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
bzero(&iov, sizeof(iov));
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
void LinuxNetLink::_requestInterfaceList()
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
la.nl_groups = RTMGRP_LINK;
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (_requestInterfaceList #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
struct nl_if_req req;
|
|
|
|
bzero(&req, sizeof(req));
|
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
|
|
|
req.nl.nlmsg_type = RTM_GETLINK;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.ifa.ifi_family = AF_UNSPEC;
|
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(pa));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
bzero(&iov, sizeof(iov));
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
2018-06-04 19:24:12 +00:00
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
void LinuxNetLink::addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName)
|
2018-05-25 21:18:06 +00:00
|
|
|
{
|
2019-03-25 22:19:52 +00:00
|
|
|
if (!target) return;
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
bzero(&la, sizeof(la));
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (addRoute #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//char tmp[64];
|
|
|
|
//char tmp2[64];
|
|
|
|
//char tmp3[64];
|
|
|
|
//fprintf(stderr, "Adding Route. target: %s via: %s src: %s iface: %s\n", target.toString(tmp), via.toString(tmp2), src.toString(tmp3), ifaceName);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-06-04 19:24:12 +00:00
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
int rtl = sizeof(struct rtmsg);
|
|
|
|
struct nl_route_req req;
|
|
|
|
bzero(&req, sizeof(req));
|
|
|
|
|
|
|
|
struct rtattr *rtap = (struct rtattr *)req.buf;
|
|
|
|
rtap->rta_type = RTA_DST;
|
|
|
|
if (target.isV4()) {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&target)->sin_addr, sizeof(struct in_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
} else {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&target)->sin6_addr, sizeof(struct in6_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
if(via) {
|
|
|
|
rtap = (struct rtattr *)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_GATEWAY;
|
|
|
|
if(via.isV4()) {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&via)->sin_addr, sizeof(struct in_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
} else {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&via)->sin6_addr, sizeof(struct in6_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
rtl += rtap->rta_len;
|
2018-06-04 19:24:12 +00:00
|
|
|
} else if (src) {
|
|
|
|
rtap = (struct rtattr *)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_SRC;
|
|
|
|
if(src.isV4()) {
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&src)->sin_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&src)->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
req.rt.rtm_src_len = src.netmaskBits();
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
if (ifaceName != NULL) {
|
|
|
|
int interface_index = _indexForInterface(ifaceName);
|
|
|
|
if (interface_index != -1) {
|
|
|
|
rtap = (struct rtattr *) (((char*)rtap) + rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_OIF;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(int));
|
|
|
|
memcpy(RTA_DATA(rtap), &interface_index, sizeof(int));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(rtl);
|
2018-06-04 19:24:12 +00:00
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE | NLM_F_ACK;
|
2018-05-25 21:18:06 +00:00
|
|
|
req.nl.nlmsg_type = RTM_NEWROUTE;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.rt.rtm_family = target.ss_family;
|
|
|
|
req.rt.rtm_table = RT_TABLE_MAIN;
|
|
|
|
req.rt.rtm_protocol = RTPROT_STATIC;
|
|
|
|
req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
req.rt.rtm_type = RTN_UNICAST;
|
|
|
|
req.rt.rtm_dst_len = target.netmaskBits();
|
2018-06-04 19:24:12 +00:00
|
|
|
req.rt.rtm_flags = 0;
|
2018-05-25 21:18:06 +00:00
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(pa));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
bzero(&iov, sizeof(iov));
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
2018-06-04 19:24:12 +00:00
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
void LinuxNetLink::delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2019-03-25 22:19:52 +00:00
|
|
|
if (!target) return;
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (delRoute #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-11 17:47:02 +00:00
|
|
|
|
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//char tmp[64];
|
|
|
|
//char tmp2[64];
|
|
|
|
//char tmp3[64];
|
|
|
|
//fprintf(stderr, "Removing Route. target: %s via: %s src: %s iface: %s\n", target.toString(tmp), via.toString(tmp2), src.toString(tmp3), ifaceName);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2018-06-04 19:24:12 +00:00
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
int rtl = sizeof(struct rtmsg);
|
|
|
|
struct nl_route_req req;
|
|
|
|
bzero(&req, sizeof(req));
|
|
|
|
|
|
|
|
struct rtattr *rtap = (struct rtattr *)req.buf;
|
|
|
|
rtap->rta_type = RTA_DST;
|
|
|
|
if (target.isV4()) {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&target)->sin_addr, sizeof(struct in_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
} else {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&target)->sin6_addr, sizeof(struct in6_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
if(via) {
|
|
|
|
rtap = (struct rtattr *)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_GATEWAY;
|
|
|
|
if(via.isV4()) {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&via)->sin_addr, sizeof(struct in_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
} else {
|
2018-06-04 19:24:12 +00:00
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&via)->sin6_addr, sizeof(struct in6_addr));
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
rtl += rtap->rta_len;
|
2018-06-04 19:24:12 +00:00
|
|
|
} else if (src) {
|
|
|
|
rtap = (struct rtattr *)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_SRC;
|
|
|
|
if(src.isV4()) {
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in*)&src)->sin_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &((struct sockaddr_in6*)&src)->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
req.rt.rtm_src_len = src.netmaskBits();
|
2018-05-25 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
if (ifaceName != NULL) {
|
|
|
|
int interface_index = _indexForInterface(ifaceName);
|
|
|
|
if (interface_index != -1) {
|
|
|
|
rtap = (struct rtattr *) (((char*)rtap) + rtap->rta_len);
|
|
|
|
rtap->rta_type = RTA_OIF;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(int));
|
|
|
|
memcpy(RTA_DATA(rtap), &interface_index, sizeof(int));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:06 +00:00
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(rtl);
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.nl.nlmsg_type = RTM_DELROUTE;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.rt.rtm_family = target.ss_family;
|
|
|
|
req.rt.rtm_table = RT_TABLE_MAIN;
|
|
|
|
req.rt.rtm_protocol = RTPROT_STATIC;
|
|
|
|
req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
req.rt.rtm_type = RTN_UNICAST;
|
|
|
|
req.rt.rtm_dst_len = target.netmaskBits();
|
2018-06-04 19:24:12 +00:00
|
|
|
req.rt.rtm_flags = 0;
|
2018-05-25 21:18:06 +00:00
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(pa));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
bzero(&iov, sizeof(iov));
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
2018-06-04 19:24:12 +00:00
|
|
|
sendmsg(fd, &msg, 0);
|
2018-05-24 22:22:11 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
2018-05-25 22:37:00 +00:00
|
|
|
|
|
|
|
void LinuxNetLink::addAddress(const InetAddress &addr, const char *iface)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2018-05-24 22:22:11 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
2019-03-25 22:19:52 +00:00
|
|
|
memset(&la,0,sizeof(la));
|
2018-06-04 19:24:12 +00:00
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
if (addr.isV4()) {
|
|
|
|
la.nl_groups = RTMGRP_IPV4_IFADDR;
|
|
|
|
} else {
|
|
|
|
la.nl_groups = RTMGRP_IPV6_IFADDR;
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (addAddress #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//char tmp[128];
|
|
|
|
//fprintf(stderr, "Adding IP address %s to interface %s", addr.toString(tmp), iface);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
int interface_index = _indexForInterface(iface);
|
|
|
|
|
|
|
|
if (interface_index == -1) {
|
|
|
|
fprintf(stderr, "Unable to find index for interface %s\n", iface);
|
2019-03-25 22:19:52 +00:00
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtl = sizeof(struct ifaddrmsg);
|
|
|
|
struct nl_adr_req req;
|
|
|
|
bzero(&req, sizeof(struct nl_adr_req));
|
|
|
|
|
|
|
|
struct rtattr *rtap = (struct rtattr *)req.buf;;
|
|
|
|
if(addr.isV4()) {
|
|
|
|
struct sockaddr_in *addr_v4 = (struct sockaddr_in*)&addr;
|
|
|
|
rtap->rta_type = IFA_ADDRESS;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v4->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap) + rtap->rta_len);
|
|
|
|
rtap->rta_type = IFA_LOCAL;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v4->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
InetAddress broadcast = addr.broadcast();
|
|
|
|
if(broadcast) {
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap)+rtap->rta_len);
|
|
|
|
struct sockaddr_in *bcast = (struct sockaddr_in*)&broadcast;
|
|
|
|
rtap->rta_type = IFA_BROADCAST;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &bcast->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
} else { //V6
|
|
|
|
rtap->rta_type = IFA_ADDRESS;
|
|
|
|
struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6*)&addr;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v6->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iface) {
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = IFA_LABEL;
|
|
|
|
rtap->rta_len = RTA_LENGTH(strlen(iface));
|
|
|
|
memcpy(RTA_DATA(rtap), iface, strlen(iface));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(rtl);
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
|
|
|
|
req.nl.nlmsg_type = RTM_NEWADDR;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.ifa.ifa_family = addr.ss_family;
|
|
|
|
req.ifa.ifa_prefixlen = addr.port();
|
|
|
|
req.ifa.ifa_flags = IFA_F_PERMANENT;
|
|
|
|
req.ifa.ifa_scope = 0;
|
|
|
|
req.ifa.ifa_index = interface_index;
|
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(sockaddr_nl));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
|
|
|
|
|
|
|
struct iovec iov;
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 22:37:00 +00:00
|
|
|
void LinuxNetLink::removeAddress(const InetAddress &addr, const char *iface)
|
2018-05-24 22:22:11 +00:00
|
|
|
{
|
2018-06-04 19:24:12 +00:00
|
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSocketTimeout(fd);
|
|
|
|
|
|
|
|
struct sockaddr_nl la;
|
|
|
|
la.nl_family = AF_NETLINK;
|
|
|
|
la.nl_pid = getpid();
|
|
|
|
if (addr.isV4()) {
|
|
|
|
la.nl_groups = RTMGRP_IPV4_IFADDR;
|
|
|
|
} else {
|
|
|
|
la.nl_groups = RTMGRP_IPV6_IFADDR;
|
|
|
|
}
|
|
|
|
if(bind(fd, (struct sockaddr*)&la, sizeof(struct sockaddr_nl))) {
|
2019-03-25 22:19:52 +00:00
|
|
|
fprintf(stderr, "Error binding RTNETLINK (removeAddress #1): %s\n", strerror(errno));
|
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-11 17:47:02 +00:00
|
|
|
#ifdef ZT_TRACE
|
2019-03-25 22:19:52 +00:00
|
|
|
//char tmp[128];
|
|
|
|
//fprintf(stderr, "Removing IP address %s from interface %s", addr.toString(tmp), iface);
|
2018-06-11 17:47:02 +00:00
|
|
|
#endif
|
2019-03-25 22:19:52 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
int interface_index = _indexForInterface(iface);
|
|
|
|
|
|
|
|
if (interface_index == -1) {
|
|
|
|
fprintf(stderr, "Unable to find index for interface %s\n", iface);
|
2019-03-25 22:19:52 +00:00
|
|
|
close(fd);
|
2018-06-04 19:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtl = sizeof(struct ifaddrmsg);
|
|
|
|
struct nl_adr_req req;
|
|
|
|
bzero(&req, sizeof(struct nl_adr_req));
|
|
|
|
|
|
|
|
struct rtattr *rtap = (struct rtattr *)req.buf;
|
|
|
|
if(addr.isV4()) {
|
|
|
|
struct sockaddr_in *addr_v4 = (struct sockaddr_in*)&addr;
|
|
|
|
rtap->rta_type = IFA_ADDRESS;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v4->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap) + rtap->rta_len);
|
|
|
|
rtap->rta_type = IFA_LOCAL;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v4->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
|
|
|
|
InetAddress broadcast = addr.broadcast();
|
|
|
|
if(broadcast) {
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap)+rtap->rta_len);
|
|
|
|
struct sockaddr_in *bcast = (struct sockaddr_in*)&broadcast;
|
|
|
|
rtap->rta_type = IFA_BROADCAST;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &bcast->sin_addr, sizeof(struct in_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
} else { //V6
|
|
|
|
rtap->rta_type = IFA_ADDRESS;
|
|
|
|
struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6*)&addr;
|
|
|
|
rtap->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
|
|
|
|
memcpy(RTA_DATA(rtap), &addr_v6->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iface) {
|
|
|
|
rtap = (struct rtattr*)(((char*)rtap)+rtap->rta_len);
|
|
|
|
rtap->rta_type = IFA_LABEL;
|
|
|
|
rtap->rta_len = RTA_LENGTH(strlen(iface));
|
|
|
|
memcpy(RTA_DATA(rtap), iface, strlen(iface));
|
|
|
|
rtl += rtap->rta_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
req.nl.nlmsg_len = NLMSG_LENGTH(rtl);
|
|
|
|
req.nl.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.nl.nlmsg_type = RTM_DELADDR;
|
|
|
|
req.nl.nlmsg_pid = 0;
|
|
|
|
req.nl.nlmsg_seq = ++_seq;
|
|
|
|
req.ifa.ifa_family = addr.ss_family;
|
|
|
|
req.ifa.ifa_prefixlen = addr.port();
|
|
|
|
req.ifa.ifa_flags = IFA_F_PERMANENT;
|
|
|
|
req.ifa.ifa_scope = 0;
|
|
|
|
req.ifa.ifa_index = interface_index;
|
|
|
|
|
|
|
|
struct sockaddr_nl pa;
|
|
|
|
bzero(&pa, sizeof(sockaddr_nl));
|
|
|
|
pa.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
struct msghdr msg;
|
|
|
|
bzero(&msg, sizeof(msg));
|
|
|
|
msg.msg_name = (void*)&pa;
|
|
|
|
msg.msg_namelen = sizeof(pa);
|
2018-05-24 22:22:11 +00:00
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
struct iovec iov;
|
|
|
|
iov.iov_base = (void*)&req.nl;
|
|
|
|
iov.iov_len = req.nl.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
|
|
_doRecv(fd);
|
|
|
|
|
|
|
|
close(fd);
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RouteList LinuxNetLink::getIPV4Routes() const
|
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
return _routes_ipv4;
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RouteList LinuxNetLink::getIPV6Routes() const
|
|
|
|
{
|
2018-05-25 21:18:06 +00:00
|
|
|
return _routes_ipv6;
|
2018-05-24 22:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:24:12 +00:00
|
|
|
int LinuxNetLink::_indexForInterface(const char *iface)
|
|
|
|
{
|
|
|
|
Mutex::Lock l(_if_m);
|
|
|
|
int interface_index = -1;
|
|
|
|
Hashtable<int, iface_entry>::Iterator iter(_interfaces);
|
|
|
|
int *k = NULL;
|
|
|
|
iface_entry *v = NULL;
|
|
|
|
while(iter.next(k,v)) {
|
|
|
|
if(strcmp(iface, v->ifacename) == 0) {
|
|
|
|
interface_index = v->index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return interface_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|