EthernetTap.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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: 2026-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. #include "EthernetTap.hpp"
  14. #include "OSUtils.hpp"
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #ifdef ZT_SDK
  18. #include "../controller/EmbeddedNetworkController.hpp"
  19. #include "../node/Node.hpp"
  20. #include "../include/VirtualTap.hpp"
  21. #else
  22. #ifdef __APPLE__
  23. #include <sys/sysctl.h>
  24. #include "MacEthernetTap.hpp"
  25. #include "MacKextEthernetTap.hpp"
  26. #endif // __APPLE__
  27. #ifdef __LINUX__
  28. #include "LinuxEthernetTap.hpp"
  29. #endif // __LINUX__
  30. #ifdef __WINDOWS__
  31. #include "WindowsEthernetTap.hpp"
  32. #endif // __WINDOWS__
  33. #ifdef __FreeBSD__
  34. #include "BSDEthernetTap.hpp"
  35. #endif // __FreeBSD__
  36. #ifdef __NetBSD__
  37. #include "NetBSDEthernetTap.hpp"
  38. #endif // __NetBSD__
  39. #ifdef __OpenBSD__
  40. #include "BSDEthernetTap.hpp"
  41. #endif // __OpenBSD__
  42. #endif
  43. namespace ZeroTier {
  44. std::shared_ptr<EthernetTap> EthernetTap::newInstance(
  45. const char *tapDeviceType, // OS-specific, NULL for default
  46. unsigned int concurrency,
  47. bool pinning,
  48. const char *homePath,
  49. const MAC &mac,
  50. unsigned int mtu,
  51. unsigned int metric,
  52. uint64_t nwid,
  53. const char *friendlyName,
  54. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  55. void *arg)
  56. {
  57. #ifdef ZT_SDK
  58. return std::shared_ptr<EthernetTap>(new VirtualTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  59. #else // not ZT_SDK
  60. #ifdef __APPLE__
  61. char osrelease[256];
  62. size_t size = sizeof(osrelease);
  63. if (sysctlbyname("kern.osrelease",osrelease,&size,nullptr,0) == 0) {
  64. char *dotAt = strchr(osrelease,'.');
  65. if (dotAt) {
  66. *dotAt = (char)0;
  67. // The "feth" virtual Ethernet device type appeared in Darwin 17.x.x. Older versions
  68. // (Sierra and earlier) must use the a kernel extension.
  69. if (strtol(osrelease,(char **)0,10) < 17) {
  70. return std::shared_ptr<EthernetTap>(new MacKextEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  71. } else {
  72. return std::shared_ptr<EthernetTap>(new MacEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  73. }
  74. }
  75. }
  76. #endif // __APPLE__
  77. #ifdef __LINUX__
  78. return std::shared_ptr<EthernetTap>(new LinuxEthernetTap(homePath,concurrency,pinning,mac,mtu,metric,nwid,friendlyName,handler,arg));
  79. #endif // __LINUX__
  80. #ifdef __WINDOWS__
  81. HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  82. if (FAILED(hres)) {
  83. throw std::runtime_error("WinEthernetTap: COM initialization failed");
  84. }
  85. static bool _comInit = false;
  86. static Mutex _comInit_m;
  87. {
  88. Mutex::Lock l(_comInit_m);
  89. if (!_comInit) {
  90. hres = CoInitializeSecurity(
  91. NULL,
  92. -1,
  93. NULL,
  94. NULL,
  95. RPC_C_AUTHN_LEVEL_PKT,
  96. RPC_C_IMP_LEVEL_IMPERSONATE,
  97. NULL,
  98. EOAC_NONE,
  99. NULL
  100. );
  101. if (FAILED(hres)) {
  102. CoUninitialize();
  103. fprintf(stderr, "WinEthernetTap: Failed to initialize security");
  104. throw std::runtime_error("WinEthernetTap: Failed to initialize security");
  105. }
  106. _comInit = true;
  107. }
  108. }
  109. return std::shared_ptr<EthernetTap>(new WindowsEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  110. #endif // __WINDOWS__
  111. #ifdef __FreeBSD__
  112. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,concurrency,pinning,mac,mtu,metric,nwid,friendlyName,handler,arg));
  113. #endif // __FreeBSD__
  114. #ifdef __NetBSD__
  115. return std::shared_ptr<EthernetTap>(new NetBSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  116. #endif // __NetBSD__
  117. #ifdef __OpenBSD__
  118. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  119. #endif // __OpenBSD__
  120. #endif // ZT_SDK?
  121. return std::shared_ptr<EthernetTap>();
  122. }
  123. EthernetTap::EthernetTap() {}
  124. EthernetTap::~EthernetTap() {}
  125. bool EthernetTap::addIps(std::vector<InetAddress> ips)
  126. {
  127. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  128. if (!addIp(*i))
  129. return false;
  130. }
  131. return true;
  132. }
  133. std::string EthernetTap::friendlyName() const
  134. {
  135. // Most platforms do not have this.
  136. return std::string();
  137. }
  138. } // namespace ZeroTier