ExtOsdep.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_EXTOSDEP_HPP
  9. #define ZT_EXTOSDEP_HPP
  10. #ifdef ZT_EXTOSDEP
  11. #include <stdint.h>
  12. #define ZT_EOD_MAXMSGSIZE (64 * 1024)
  13. #define ZT_EOD_MSG_STARTED 1 // no data
  14. #define ZT_EOD_MSG_ADDTAP 2
  15. #define ZT_EOD_MSG_ADDTAPRESP 3
  16. #define ZT_EOD_MSG_DELTAP 4
  17. #define ZT_EOD_MSG_DELTAPRESP 5 // no data
  18. #define ZT_EOD_MSG_SETMTU 6
  19. #define ZT_EOD_MSG_SETMTURESP 7
  20. #define ZT_EOD_MSG_ADDIP 8
  21. #define ZT_EOD_MSG_ADDIPRESP 9
  22. #define ZT_EOD_MSG_DELIP 10
  23. #define ZT_EOD_MSG_DELIPRESP 11
  24. #define ZT_EOD_MSG_GETIPS 12
  25. #define ZT_EOD_MSG_GETIPSRESP 13
  26. #define ZT_EOD_MSG_GETBINDADDRS 14
  27. #define ZT_EOD_MSG_GETBINDADDRSRESP 15
  28. #define ZT_EOD_MSG_ADDROUTE 16
  29. #define ZT_EOD_MSG_ADDROUTERESP 17
  30. #define ZT_EOD_MSG_DELROUTE 18
  31. #define ZT_EOD_MSG_DELROUTERESP 19
  32. struct zt_eod_msg_addtap {
  33. unsigned char cmd;
  34. uint64_t nwid;
  35. uint32_t mtu;
  36. uint32_t metric;
  37. char fname[128];
  38. unsigned char mac[6];
  39. } __attribute__((packed));
  40. struct zt_eod_msg_addtapresp {
  41. unsigned char cmd;
  42. char name[16];
  43. } __attribute__((packed));
  44. struct zt_eod_msg_deltap {
  45. unsigned char cmd;
  46. char name[16];
  47. } __attribute__((packed));
  48. struct zt_eod_msg_setmtu {
  49. unsigned char cmd;
  50. char name[16];
  51. unsigned mtu;
  52. } __attribute__((packed));
  53. struct zt_eod_msg_ip {
  54. unsigned char cmd;
  55. char name[16];
  56. unsigned char afi; // 1 ip, 2 ip6
  57. unsigned char len; // bits in mask
  58. unsigned char data[16];
  59. } __attribute__((packed));
  60. struct zt_eod_msg_getips {
  61. unsigned char cmd;
  62. char name[16];
  63. } __attribute__((packed));
  64. struct zt_eod_msg_getipsresp {
  65. unsigned char cmd;
  66. struct addr {
  67. unsigned char afi;
  68. unsigned char len;
  69. unsigned char data[16];
  70. } __attribute__((packed)) addrs[0];
  71. } __attribute__((packed));
  72. #define ZT_EOD_GETIPSRESP_MAXADDRS ((ZT_EOD_MAXMSGSIZE - sizeof(zt_eod_msg_getipsresp)) / sizeof(zt_eod_msg_getipsresp::addr))
  73. struct zt_eod_msg_getbindaddrsresp {
  74. unsigned char cmd;
  75. unsigned char result;
  76. struct addr {
  77. unsigned char afi;
  78. unsigned char len;
  79. unsigned char data[16];
  80. char ifname[16];
  81. } __attribute__((packed)) addrs[0];
  82. } __attribute__((packed));
  83. #define ZT_EOD_GETBINDADDRSRESP_MAXADDRS ((ZT_EOD_MAXMSGSIZE - sizeof(zt_eod_msg_getbindaddrsresp)) / sizeof(zt_eod_msg_getbindaddrsresp::addr))
  84. struct zt_eod_msg_route {
  85. unsigned char cmd;
  86. unsigned char afi; // 1 ip, 2 ip6
  87. unsigned char dstlen;
  88. unsigned char dst[16];
  89. unsigned char gw[16];
  90. char dev[16];
  91. unsigned char src[16];
  92. } __attribute__((packed));
  93. struct zt_eod_mgmt_req {
  94. uint32_t method;
  95. uint32_t pathlen;
  96. uint32_t datalen;
  97. unsigned char data[0];
  98. } __attribute__((packed));
  99. struct zt_eod_mgmt_reply {
  100. uint32_t scode;
  101. uint32_t datalen;
  102. unsigned char data[0];
  103. } __attribute__((packed));
  104. #ifndef ZT_EXTOSDEP_IFACEONLY
  105. #include "../node/AtomicCounter.hpp"
  106. #include "../node/Hashtable.hpp"
  107. #include "../node/InetAddress.hpp"
  108. #include "../node/MAC.hpp"
  109. #include "../node/Mutex.hpp"
  110. #include "BlockingQueue.hpp"
  111. #include "EthernetTap.hpp"
  112. #include "Thread.hpp"
  113. #include <atomic>
  114. #include <functional>
  115. #include <mutex>
  116. #include <string>
  117. #include <thread>
  118. namespace ZeroTier {
  119. class ExtOsdep {
  120. public:
  121. static void init(int, int);
  122. static void started(int*, void**);
  123. static void routeAddDel(bool, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
  124. static bool getBindAddrs(std::map<InetAddress, std::string>&);
  125. static bool mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)>);
  126. static bool mgmtWritable(void*);
  127. };
  128. class ExtOsdepTap : public EthernetTap {
  129. public:
  130. ExtOsdepTap(
  131. const char* homePath,
  132. const MAC& mac,
  133. unsigned int mtu,
  134. unsigned int metric,
  135. uint64_t nwid,
  136. const char* friendlyName,
  137. void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
  138. void* arg);
  139. virtual ~ExtOsdepTap();
  140. virtual void setEnabled(bool en);
  141. virtual bool enabled() const;
  142. virtual bool addIp(const InetAddress& ip);
  143. virtual bool addIps(std::vector<InetAddress> ips);
  144. virtual bool removeIp(const InetAddress& ip);
  145. virtual std::vector<InetAddress> ips() const;
  146. virtual void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len);
  147. virtual std::string deviceName() const;
  148. virtual void setFriendlyName(const char* friendlyName);
  149. virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
  150. virtual void setMtu(unsigned int mtu);
  151. virtual void setDns(const char* domain, const std::vector<InetAddress>& servers)
  152. {
  153. }
  154. private:
  155. void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
  156. void* _arg;
  157. uint64_t _nwid;
  158. MAC _mac;
  159. std::string _homePath;
  160. std::string _dev;
  161. std::vector<MulticastGroup> _multicastGroups;
  162. unsigned int _mtu;
  163. int _fd;
  164. int _shutdownSignalPipe[2];
  165. std::atomic_bool _enabled;
  166. std::atomic_bool _run;
  167. std::thread _tapReaderThread[2];
  168. std::thread _tapProcessorThread;
  169. std::mutex _buffers_l;
  170. std::vector<void*> _buffers;
  171. BlockingQueue<std::pair<void*, int> > _tapq;
  172. AtomicCounter _tapqsize;
  173. std::vector<InetAddress> allIps;
  174. void doRemoveIp(const InetAddress&);
  175. };
  176. } // namespace ZeroTier
  177. #endif // ZT_EXTOSDEP_IFACEONLY
  178. #endif // ZT_EXTOSDEP
  179. #endif