LinuxNetLink.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_LINUX_NETLINK_HPP
  14. #define ZT_LINUX_NETLINK_HPP
  15. #include "../core/Constants.hpp"
  16. #ifdef __LINUX__
  17. #include <vector>
  18. #include <sys/socket.h>
  19. #include <asm/types.h>
  20. #include <linux/rtnetlink.h>
  21. #include <sys/socket.h>
  22. #include <linux/if.h>
  23. #include "../core/InetAddress.hpp"
  24. #include "../core/MAC.hpp"
  25. #include "Thread.hpp"
  26. #include "../core/Mutex.hpp"
  27. namespace ZeroTier {
  28. struct route_entry {
  29. InetAddress target;
  30. InetAddress via;
  31. int if_index;
  32. char iface[IFNAMSIZ];
  33. };
  34. typedef std::vector<route_entry> RouteList;
  35. /**
  36. * Interface with Linux's RTNETLINK
  37. */
  38. class LinuxNetLink
  39. {
  40. private:
  41. LinuxNetLink();
  42. ~LinuxNetLink();
  43. public:
  44. static LinuxNetLink& getInstance()
  45. {
  46. static LinuxNetLink instance;
  47. return instance;
  48. }
  49. LinuxNetLink(LinuxNetLink const&) = delete;
  50. void operator=(LinuxNetLink const&) = delete;
  51. void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
  52. void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
  53. RouteList getIPV4Routes() const;
  54. RouteList getIPV6Routes() const;
  55. void addAddress(const InetAddress &addr, const char *iface);
  56. void removeAddress(const InetAddress &addr, const char *iface);
  57. void threadMain() throw();
  58. private:
  59. int _doRecv(int fd);
  60. void _processMessage(struct nlmsghdr *nlp, int nll);
  61. void _routeAdded(struct nlmsghdr *nlp);
  62. void _routeDeleted(struct nlmsghdr *nlp);
  63. void _linkAdded(struct nlmsghdr *nlp);
  64. void _linkDeleted(struct nlmsghdr *nlp);
  65. void _ipAddressAdded(struct nlmsghdr *nlp);
  66. void _ipAddressDeleted(struct nlmsghdr *nlp);
  67. void _requestInterfaceList();
  68. void _requestIPv4Routes();
  69. void _requestIPv6Routes();
  70. int _indexForInterface(const char *iface);
  71. void _setSocketTimeout(int fd, int seconds = 1);
  72. Thread _t;
  73. bool _running;
  74. RouteList _routes_ipv4;
  75. Mutex _rv4_m;
  76. RouteList _routes_ipv6;
  77. Mutex _rv6_m;
  78. uint32_t _seq;
  79. struct iface_entry {
  80. int index;
  81. char ifacename[IFNAMSIZ];
  82. char mac[18];
  83. char mac_bin[6];
  84. unsigned int mtu;
  85. };
  86. std::map<int, iface_entry> _interfaces;
  87. Mutex _if_m;
  88. // socket communication vars;
  89. int _fd;
  90. struct sockaddr_nl _la;
  91. };
  92. }
  93. #endif
  94. #endif // ZT_LINUX_NETLINK_HPPS