Files

159 lines
3.4 KiB
C++
Raw Permalink Normal View History

2025-08-06 12:16:44 -04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
2025-08-06 12:16:44 -04:00
* (c) ZeroTier, Inc.
* https://www.zerotier.com/
*/
#ifndef ZT_LINUX_NETLINK_HPP
#define ZT_LINUX_NETLINK_HPP
2019-08-23 12:09:31 -07:00
#include "../node/Constants.hpp"
#ifdef __LINUX__
#include <asm/types.h>
#include <linux/rtnetlink.h>
2025-07-03 11:26:23 -04:00
#include <map>
#include <set>
#include <sys/socket.h>
2025-07-03 11:26:23 -04:00
#include <vector>
// #include <linux/if.h>
2025-07-03 11:26:23 -04:00
#include "../node/Hashtable.hpp"
#include "../node/InetAddress.hpp"
#include "../node/MAC.hpp"
#include "../node/Mutex.hpp"
2025-07-03 11:26:23 -04:00
#include "Thread.hpp"
namespace ZeroTier {
/**
2019-08-23 09:23:39 -07:00
* Interface with Linux's RTNETLINK
*/
2025-07-03 11:26:23 -04:00
class LinuxNetLink {
private:
2020-11-13 21:06:34 -05:00
LinuxNetLink();
~LinuxNetLink();
2025-07-03 11:26:23 -04:00
public:
struct Route {
InetAddress target;
InetAddress via;
InetAddress src;
int ifidx;
2025-07-03 11:26:23 -04:00
inline bool operator==(const Route& r) const
{
return ((target == r.target) && (via == r.via) && (src == r.src) && (ifidx == r.ifidx));
}
inline bool operator!=(const Route& r) const
{
return (! (*this == r));
}
inline bool operator<(const Route& r) const
{
if (target < r.target) {
return true;
2025-07-03 11:26:23 -04:00
}
else if (target == r.target) {
if (via < r.via) {
return true;
2025-07-03 11:26:23 -04:00
}
else if (via == r.via) {
if (src < r.src) {
return true;
2025-07-03 11:26:23 -04:00
}
else if (src == r.src) {
return (ifidx < r.ifidx);
}
}
}
return false;
}
2025-07-03 11:26:23 -04:00
inline bool operator>(const Route& r) const
{
return (r < *this);
}
inline bool operator<=(const Route& r) const
{
return ! (r < *this);
}
inline bool operator>=(const Route& r) const
{
return ! (*this < r);
}
};
2020-11-13 21:06:34 -05:00
static LinuxNetLink& getInstance()
{
static LinuxNetLink instance;
return instance;
}
2020-11-13 21:06:34 -05:00
LinuxNetLink(LinuxNetLink const&) = delete;
void operator=(LinuxNetLink const&) = delete;
2025-07-03 11:26:23 -04:00
void addRoute(const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
void delRoute(const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
2025-07-03 11:26:23 -04:00
void addAddress(const InetAddress& addr, const char* iface);
void removeAddress(const InetAddress& addr, const char* iface);
2025-07-03 11:26:23 -04:00
bool routeIsSet(const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifname);
2020-11-13 21:06:34 -05:00
void threadMain() throw();
2025-07-03 11:26:23 -04:00
private:
2020-11-13 21:06:34 -05:00
int _doRecv(int fd);
2025-07-03 11:26:23 -04:00
void _processMessage(struct nlmsghdr* nlp, int nll);
void _routeAdded(struct nlmsghdr* nlp);
void _routeDeleted(struct nlmsghdr* nlp);
void _linkAdded(struct nlmsghdr* nlp);
void _linkDeleted(struct nlmsghdr* nlp);
void _ipAddressAdded(struct nlmsghdr* nlp);
void _ipAddressDeleted(struct nlmsghdr* nlp);
2020-11-13 21:06:34 -05:00
void _requestInterfaceList();
void _requestIPv4Routes();
void _requestIPv6Routes();
2025-07-03 11:26:23 -04:00
int _indexForInterface(const char* iface);
2020-11-13 21:06:34 -05:00
void _setSocketTimeout(int fd, int seconds = 1);
2020-11-13 21:06:34 -05:00
Thread _t;
bool _running;
2018-05-25 14:18:06 -07:00
2020-11-13 21:06:34 -05:00
uint32_t _seq;
2025-07-03 11:26:23 -04:00
std::map<InetAddress, std::set<LinuxNetLink::Route> > _routes;
Mutex _routes_m;
2020-11-13 21:06:34 -05:00
struct iface_entry {
iface_entry()
2025-07-03 11:26:23 -04:00
{
memset(this, 0, sizeof(iface_entry));
}
2020-11-13 21:06:34 -05:00
int index;
2025-07-03 11:26:23 -04:00
char ifacename[16]; // IFNAMSIZ on Linux == 16
2020-11-13 21:06:34 -05:00
char mac[18];
char mac_bin[6];
unsigned int mtu;
};
Hashtable<int, iface_entry> _interfaces;
Mutex _if_m;
2020-11-13 21:06:34 -05:00
// socket communication vars;
int _fd;
struct sockaddr_nl _la;
2019-08-23 09:23:39 -07:00
};
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier
2019-08-23 12:09:31 -07:00
#endif
2025-08-06 12:16:44 -04:00
#endif // ZT_LINUX_NETLINK_HPPS