EthernetTap.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ETHERNETTAP_HPP
  28. #define ZT_ETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. #include <list>
  33. #include <vector>
  34. #include <set>
  35. #include <string>
  36. #include <queue>
  37. #include <stdexcept>
  38. #include "Constants.hpp"
  39. #include "InetAddress.hpp"
  40. #include "MAC.hpp"
  41. #include "Mutex.hpp"
  42. #include "MulticastGroup.hpp"
  43. #include "Thread.hpp"
  44. #include "Buffer.hpp"
  45. #include "Array.hpp"
  46. #ifdef __WINDOWS__
  47. #include <WinSock2.h>
  48. #include <Windows.h>
  49. #endif
  50. namespace ZeroTier {
  51. class RuntimeEnvironment;
  52. /**
  53. * System ethernet tap device
  54. */
  55. class EthernetTap
  56. {
  57. public:
  58. /**
  59. * Construct a new TAP device
  60. *
  61. * Handler arguments: arg,from,to,etherType,data
  62. *
  63. * @param renv Runtime environment
  64. * @param tag A tag used to identify persistent taps at the OS layer (e.g. nwid in hex)
  65. * @param mac MAC address of device
  66. * @param mtu MTU of device
  67. * @param desc If non-NULL, a description (not used on all OSes)
  68. * @param handler Handler function to be called when data is received from the tap
  69. * @param arg First argument to handler function
  70. * @throws std::runtime_error Unable to allocate device
  71. */
  72. EthernetTap(
  73. const RuntimeEnvironment *renv,
  74. const char *tag,
  75. const MAC &mac,
  76. unsigned int mtu,
  77. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  78. void *arg)
  79. throw(std::runtime_error);
  80. /**
  81. * Close tap and shut down thread
  82. *
  83. * This may block for a few seconds while thread exits.
  84. */
  85. ~EthernetTap();
  86. /**
  87. * Set the user display name for this connection
  88. *
  89. * This does nothing on platforms that don't have this concept.
  90. *
  91. * @param dn User display name
  92. */
  93. void setDisplayName(const char *dn);
  94. /**
  95. * @return MAC address of this interface
  96. */
  97. inline const MAC &mac() const throw() { return _mac; }
  98. /**
  99. * @return MTU of this interface
  100. */
  101. inline unsigned int mtu() const throw() { return _mtu; }
  102. /**
  103. * Add an IP to this interface
  104. *
  105. * @param ip IP and netmask (netmask stored in port field)
  106. * @return True if IP added successfully
  107. */
  108. bool addIP(const InetAddress &ip);
  109. /**
  110. * Remove an IP from this interface
  111. *
  112. * Link-local IP addresses may not be able to be removed, depending on platform and type.
  113. *
  114. * @param ip IP and netmask (netmask stored in port field)
  115. * @return True if IP removed successfully
  116. */
  117. bool removeIP(const InetAddress &ip);
  118. /**
  119. * @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
  120. */
  121. std::set<InetAddress> ips() const;
  122. /**
  123. * Set this tap's IP addresses to exactly this set of IPs
  124. *
  125. * New IPs are created, ones not in this list are removed.
  126. *
  127. * @param ips IP addresses with netmask in port field
  128. */
  129. inline void setIps(const std::set<InetAddress> &allIps)
  130. {
  131. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  132. addIP(*i);
  133. std::set<InetAddress> myIps(ips());
  134. #ifdef __APPLE__
  135. bool haveV6LinkLocal = false;
  136. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  137. if (i->isLinkLocal()) {
  138. if (i->isV6())
  139. haveV6LinkLocal = true;
  140. } else if (!allIps.count(*i))
  141. removeIP(*i);
  142. }
  143. if (!haveV6LinkLocal)
  144. addIP(InetAddress::makeIpv6LinkLocal(_mac));
  145. #else
  146. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  147. if ((!i->isLinkLocal())&&(!allIps.count(*i)))
  148. removeIP(*i);
  149. }
  150. #endif
  151. }
  152. /**
  153. * Put a frame, making it available to the OS for processing
  154. *
  155. * @param from MAC address from which frame originated
  156. * @param to MAC address of destination (typically MAC of tap itself)
  157. * @param etherType Ethernet protocol ID
  158. * @param data Frame payload
  159. * @param len Length of frame
  160. */
  161. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  162. /**
  163. * @return OS-specific device or connection name
  164. */
  165. std::string deviceName() const;
  166. /**
  167. * @return OS-internal persistent device ID or empty string if not applicable to this platform or not persistent
  168. */
  169. std::string persistentId() const;
  170. /**
  171. * Fill or modify a set to contain multicast groups for this device
  172. *
  173. * This populates a set or, if already populated, modifies it to contain
  174. * only multicast groups in which this device is interested.
  175. *
  176. * This should always include the blind wildcard MulticastGroup (MAC of
  177. * ff:ff:ff:ff:ff:ff and 0 ADI field).
  178. *
  179. * @param groups Set to modify in place
  180. * @return True if set was changed since last call
  181. */
  182. bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  183. /**
  184. * Thread main method; do not call elsewhere
  185. */
  186. void threadMain()
  187. throw();
  188. /**
  189. * Remove persistent tap device by device name
  190. *
  191. * This has no effect on platforms that do not have persistent taps.
  192. * On platforms like Windows with persistent devices the device is
  193. * uninstalled.
  194. *
  195. * @param _r Runtime environment
  196. * @param pdev Device name as returned by persistentId() while tap is running
  197. * @return True if a device was deleted
  198. */
  199. static bool deletePersistentTapDevice(const RuntimeEnvironment *_r,const char *pid);
  200. /**
  201. * Clean persistent tap devices that are not in the supplied set
  202. *
  203. * This has no effect on platforms that do not have persistent taps.
  204. * On platforms like Windows with persistent devices the device is
  205. * uninstalled.
  206. *
  207. * @param _r Runtime environment
  208. * @param exceptThese Devices to leave in place
  209. * @param alsoRemoveUnassociatedDevices If true, remove devices not associated with any network as well
  210. * @return Number of devices deleted or -1 if an error prevented the operation from being performed
  211. */
  212. static int cleanPersistentTapDevices(const RuntimeEnvironment *_r,const std::set<std::string> &exceptThese,bool alsoRemoveUnassociatedDevices);
  213. private:
  214. const MAC _mac;
  215. const unsigned int _mtu;
  216. const RuntimeEnvironment *_r;
  217. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  218. void *_arg;
  219. Thread _thread;
  220. #ifdef __UNIX_LIKE__
  221. char _dev[16];
  222. int _fd;
  223. int _shutdownSignalPipe[2];
  224. #endif
  225. #ifdef __WINDOWS__
  226. void _syncIpsWithRegistry(const std::set<InetAddress> &haveIps);
  227. HANDLE _tap;
  228. OVERLAPPED _tapOvlRead,_tapOvlWrite;
  229. char _tapReadBuf[ZT_IF_MTU + 32];
  230. HANDLE _injectSemaphore;
  231. GUID _deviceGuid;
  232. std::string _myDeviceInstanceId; // NetCfgInstanceId, a GUID
  233. std::string _myDeviceInstanceIdPath; // DeviceInstanceID, another kind of "instance ID"
  234. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  235. Mutex _injectPending_m;
  236. volatile bool _run;
  237. volatile bool _initialized;
  238. #endif
  239. };
  240. } // namespace ZeroTier
  241. #endif