Skip to content

Commit

Permalink
Linux netlink and route setting... work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamierymenko committed Nov 14, 2020
1 parent 6c5a097 commit e8be287
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
58 changes: 30 additions & 28 deletions osdep/LinuxNetLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "../node/Constants.hpp"

#define ZT_NETLINK_TRACE

#ifdef __LINUX__

#include "LinuxNetLink.hpp"
Expand Down Expand Up @@ -85,7 +87,7 @@ void LinuxNetLink::_setSocketTimeout(int fd, int seconds)
tv.tv_sec = seconds;
tv.tv_usec = 0;
if(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) != 0) {
#ifdef ZT_TRACE
#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
#endif
}
Expand Down Expand Up @@ -119,8 +121,8 @@ int LinuxNetLink::_doRecv(int fd)
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) {
#ifdef ZT_TRACE
//fprintf(stderr, "rtnetlink error: %s\n", strerror(-(err->error)));
#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "rtnetlink error: %s\n", strerror(-(err->error)));
#endif
}
p = buf;
Expand All @@ -145,7 +147,7 @@ int LinuxNetLink::_doRecv(int fd)
}

if (nlp->nlmsg_type == NLMSG_OVERRUN) {
//#ifdef ZT_TRACE
//#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "NLMSG_OVERRUN: Data lost\n");
//#endif
p = buf;
Expand Down Expand Up @@ -242,8 +244,8 @@ void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp)
}
}

#ifdef ZT_TRACE
//fprintf(stderr,"Added IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#ifdef ZT_NETLINK_TRACE
fprintf(stderr,"Added IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#endif
}

Expand Down Expand Up @@ -276,8 +278,8 @@ void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp)
}
}

#ifdef ZT_TRACE
//fprintf(stderr, "Removed IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "Removed IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#endif
}

Expand Down Expand Up @@ -313,8 +315,8 @@ void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp)
}
sprintf(ms, "%d", rtp->rtm_dst_len);

#ifdef ZT_TRACE
//fprintf(stderr, "Route Added: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "Route Added: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#endif
}

Expand Down Expand Up @@ -350,8 +352,8 @@ void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp)
}
sprintf(ms, "%d", rtp->rtm_dst_len);

#ifdef ZT_TRACE
//fprintf(stderr, "Route Deleted: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#ifdef ZT_NETLINK_TRACE
fprintf(stderr, "Route Deleted: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#endif
}

Expand Down Expand Up @@ -605,11 +607,11 @@ void LinuxNetLink::addRoute(const InetAddress &target, const InetAddress &via, c
return;
}

#ifdef ZT_TRACE
//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);
#ifdef ZT_NETLINK_TRACE
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);
#endif

int rtl = sizeof(struct rtmsg);
Expand Down Expand Up @@ -720,11 +722,11 @@ void LinuxNetLink::delRoute(const InetAddress &target, const InetAddress &via, c
return;
}

#ifdef ZT_TRACE
//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);
#ifdef ZT_NETLINK_TRACE
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);
#endif

int rtl = sizeof(struct rtmsg);
Expand Down Expand Up @@ -839,9 +841,9 @@ void LinuxNetLink::addAddress(const InetAddress &addr, const char *iface)
return;
}

#ifdef ZT_TRACE
//char tmp[128];
//fprintf(stderr, "Adding IP address %s to interface %s", addr.toString(tmp), iface);
#ifdef ZT_NETLINK_TRACE
char tmp[128];
fprintf(stderr, "Adding IP address %s to interface %s\n", addr.toString(tmp), iface);
#endif

int interface_index = _indexForInterface(iface);
Expand Down Expand Up @@ -955,9 +957,9 @@ void LinuxNetLink::removeAddress(const InetAddress &addr, const char *iface)
return;
}

#ifdef ZT_TRACE
//char tmp[128];
//fprintf(stderr, "Removing IP address %s from interface %s", addr.toString(tmp), iface);
#ifdef ZT_NETLINK_TRACE
char tmp[128];
fprintf(stderr, "Removing IP address %s from interface %s\n", addr.toString(tmp), iface);
#endif

int interface_index = _indexForInterface(iface);
Expand Down
4 changes: 2 additions & 2 deletions osdep/ManagedRoute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,13 @@ bool ManagedRoute::sync()

if (!_applied.count(leftt)) {
_applied[leftt] = false; // boolean unused
LinuxNetLink::getInstance().delRoute(leftt, _via, _src, (_via) ? (const char *)0 : _device);
//LinuxNetLink::getInstance().delRoute(leftt, _via, _src, (_via) ? (const char *)0 : _device);
LinuxNetLink::getInstance().addRoute(leftt, _via, _src, (_via) ? (const char *)0 : _device);
//_routeCmd("replace",leftt,_via,(_via) ? (const char *)0 : _device);
}
if ((rightt)&&(!_applied.count(rightt))) {
_applied[rightt] = false; // boolean unused
LinuxNetLink::getInstance().delRoute(rightt, _via, _src, (_via) ? (const char *)0 : _device);
//LinuxNetLink::getInstance().delRoute(rightt, _via, _src, (_via) ? (const char *)0 : _device);
LinuxNetLink::getInstance().addRoute(rightt, _via, _src, (_via) ? (const char *)0 : _device);
//_routeCmd("replace",rightt,_via,(_via) ? (const char *)0 : _device);
}
Expand Down
13 changes: 12 additions & 1 deletion service/OneService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,6 @@ class OneServiceImpl : public OneService
SharedPtr<ManagedRoute> &mr = n.managedRoutes[*target];
if (!mr)
mr.set(new ManagedRoute(*target, *via, *src, tapdev.c_str()));
mr->sync();
#endif
}

Expand All @@ -1998,6 +1997,18 @@ class OneServiceImpl : public OneService
n.managedRoutes.erase(r++);
else ++r;
}

// Sync device-local managed routes first, then indirect results. That way
// we don't get destination unreachable for routes that are via things
// that do not yet have routes in the system.
for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes.begin());r!=n.managedRoutes.end();++r) {
if (!r->second->via())
r->second->sync();
}
for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes.begin());r!=n.managedRoutes.end();++r) {
if (r->second->via())
r->second->sync();
}
}

if (syncDns) {
Expand Down

0 comments on commit e8be287

Please sign in to comment.