LinuxNetLink.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c)2019 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: 2023-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 "../node/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 "../node/InetAddress.hpp"
  24. #include "../node/MAC.hpp"
  25. #include "Thread.hpp"
  26. #include "../node/Hashtable.hpp"
  27. #include "../node/Mutex.hpp"
  28. namespace ZeroTier {
  29. struct route_entry {
  30. InetAddress target;
  31. InetAddress via;
  32. int if_index;
  33. char iface[IFNAMSIZ];
  34. };
  35. typedef std::vector<route_entry> RouteList;
  36. /**
  37. * Interface with Linux's RTNETLINK
  38. */
  39. class LinuxNetLink
  40. {
  41. private:
  42. LinuxNetLink();
  43. ~LinuxNetLink();
  44. public:
  45. static LinuxNetLink& getInstance()
  46. {
  47. static LinuxNetLink instance;
  48. return instance;
  49. }
  50. LinuxNetLink(LinuxNetLink const&) = delete;
  51. void operator=(LinuxNetLink const&) = delete;
  52. void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
  53. void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
  54. RouteList getIPV4Routes() const;
  55. RouteList getIPV6Routes() const;
  56. void addAddress(const InetAddress &addr, const char *iface);
  57. void removeAddress(const InetAddress &addr, const char *iface);
  58. void threadMain() throw();
  59. private:
  60. int _doRecv(int fd);
  61. void _processMessage(struct nlmsghdr *nlp, int nll);
  62. void _routeAdded(struct nlmsghdr *nlp);
  63. void _routeDeleted(struct nlmsghdr *nlp);
  64. void _linkAdded(struct nlmsghdr *nlp);
  65. void _linkDeleted(struct nlmsghdr *nlp);
  66. void _ipAddressAdded(struct nlmsghdr *nlp);
  67. void _ipAddressDeleted(struct nlmsghdr *nlp);
  68. void _requestInterfaceList();
  69. void _requestIPv4Routes();
  70. void _requestIPv6Routes();
  71. int _indexForInterface(const char *iface);
  72. void _setSocketTimeout(int fd, int seconds = 1);
  73. Thread _t;
  74. bool _running;
  75. RouteList _routes_ipv4;
  76. Mutex _rv4_m;
  77. RouteList _routes_ipv6;
  78. Mutex _rv6_m;
  79. uint32_t _seq;
  80. struct iface_entry {
  81. int index;
  82. char ifacename[IFNAMSIZ];
  83. char mac[18];
  84. char mac_bin[6];
  85. unsigned int mtu;
  86. };
  87. Hashtable<int, iface_entry> _interfaces;
  88. Mutex _if_m;
  89. // socket communication vars;
  90. int _fd;
  91. struct sockaddr_nl _la;
  92. };
  93. }
  94. #endif
  95. #endif // ZT_LINUX_NETLINK_HPPS