EthernetTap.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "../include/VirtualTap.hpp"
  20. #include "../node/Node.hpp"
  21. #else
  22. #ifdef __APPLE__
  23. #include "MacEthernetTap.hpp"
  24. #include "MacKextEthernetTap.hpp"
  25. #include <sys/sysctl.h>
  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. }
  72. else {
  73. return std::shared_ptr<EthernetTap>(new MacEthernetTap(homePath, mac, mtu, metric, nwid, friendlyName, handler, arg));
  74. }
  75. }
  76. }
  77. #endif // __APPLE__
  78. #ifdef __LINUX__
  79. return std::shared_ptr<EthernetTap>(new LinuxEthernetTap(homePath, concurrency, pinning, mac, mtu, metric, nwid, friendlyName, handler, arg));
  80. #endif // __LINUX__
  81. #ifdef __WINDOWS__
  82. HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  83. if (FAILED(hres)) {
  84. throw std::runtime_error("WinEthernetTap: COM initialization failed");
  85. }
  86. static bool _comInit = false;
  87. static Mutex _comInit_m;
  88. {
  89. Mutex::Lock l(_comInit_m);
  90. if (! _comInit) {
  91. hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
  92. if (FAILED(hres)) {
  93. CoUninitialize();
  94. fprintf(stderr, "WinEthernetTap: Failed to initialize security");
  95. throw std::runtime_error("WinEthernetTap: Failed to initialize security");
  96. }
  97. _comInit = true;
  98. }
  99. }
  100. return std::shared_ptr<EthernetTap>(new WindowsEthernetTap(homePath, mac, mtu, metric, nwid, friendlyName, handler, arg));
  101. #endif // __WINDOWS__
  102. #ifdef __FreeBSD__
  103. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath, concurrency, pinning, mac, mtu, metric, nwid, friendlyName, handler, arg));
  104. #endif // __FreeBSD__
  105. #ifdef __NetBSD__
  106. return std::shared_ptr<EthernetTap>(new NetBSDEthernetTap(homePath, mac, mtu, metric, nwid, friendlyName, handler, arg));
  107. #endif // __NetBSD__
  108. #ifdef __OpenBSD__
  109. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath, concurrency, pinning, mac, mtu, metric, nwid, friendlyName, handler, arg));
  110. #endif // __OpenBSD__
  111. #endif // ZT_SDK?
  112. return std::shared_ptr<EthernetTap>();
  113. }
  114. EthernetTap::EthernetTap()
  115. {
  116. }
  117. EthernetTap::~EthernetTap()
  118. {
  119. }
  120. bool EthernetTap::addIps(std::vector<InetAddress> ips)
  121. {
  122. for (std::vector<InetAddress>::const_iterator i(ips.begin()); i != ips.end(); ++i) {
  123. if (! addIp(*i))
  124. return false;
  125. }
  126. return true;
  127. }
  128. std::string EthernetTap::friendlyName() const
  129. {
  130. // Most platforms do not have this.
  131. return std::string();
  132. }
  133. } // namespace ZeroTier