WindowsEthernetTap.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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: 2025-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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <winsock2.h>
  18. #include <windows.h>
  19. #include <tchar.h>
  20. #include <malloc.h>
  21. #include <winreg.h>
  22. #include <wchar.h>
  23. #include <ws2ipdef.h>
  24. #include <ws2tcpip.h>
  25. #include <IPHlpApi.h>
  26. #include <nldef.h>
  27. #include <netioapi.h>
  28. #include <atlbase.h>
  29. #include <netlistmgr.h>
  30. #include <nldef.h>
  31. #include <SetupAPI.h>
  32. #include <newdev.h>
  33. #include <cfgmgr32.h>
  34. #include <iostream>
  35. #include <set>
  36. #include "../node/Constants.hpp"
  37. #include "../node/Utils.hpp"
  38. #include "../node/Mutex.hpp"
  39. #include "WindowsEthernetTap.hpp"
  40. #include "OSUtils.hpp"
  41. #include "..\windows\TapDriver6\tap-windows.h"
  42. #include "WinDNSHelper.hpp"
  43. #include <netcon.h>
  44. // Create a fake unused default route to force detection of network type on networks without gateways
  45. #define ZT_WINDOWS_CREATE_FAKE_DEFAULT_ROUTE
  46. // Function signatures of dynamically loaded functions, from newdev.h, setupapi.h, and cfgmgr32.h
  47. typedef BOOL (WINAPI *UpdateDriverForPlugAndPlayDevicesA_t)(_In_opt_ HWND hwndParent,_In_ LPCSTR HardwareId,_In_ LPCSTR FullInfPath,_In_ DWORD InstallFlags,_Out_opt_ PBOOL bRebootRequired);
  48. typedef BOOL (WINAPI *SetupDiGetINFClassA_t)(_In_ PCSTR InfName,_Out_ LPGUID ClassGuid,_Out_writes_(ClassNameSize) PSTR ClassName,_In_ DWORD ClassNameSize,_Out_opt_ PDWORD RequiredSize);
  49. typedef HDEVINFO (WINAPI *SetupDiCreateDeviceInfoList_t)(_In_opt_ CONST GUID *ClassGuid,_In_opt_ HWND hwndParent);
  50. typedef BOOL (WINAPI *SetupDiCreateDeviceInfoA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PCSTR DeviceName,_In_ CONST GUID *ClassGuid,_In_opt_ PCSTR DeviceDescription,_In_opt_ HWND hwndParent,_In_ DWORD CreationFlags,_Out_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  51. typedef BOOL (WINAPI *SetupDiSetDeviceRegistryPropertyA_t)(_In_ HDEVINFO DeviceInfoSet,_Inout_ PSP_DEVINFO_DATA DeviceInfoData,_In_ DWORD Property,_In_reads_bytes_opt_(PropertyBufferSize) CONST BYTE *PropertyBuffer,_In_ DWORD PropertyBufferSize);
  52. typedef BOOL (WINAPI *SetupDiCallClassInstaller_t)(_In_ DI_FUNCTION InstallFunction,_In_ HDEVINFO DeviceInfoSet,_In_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  53. typedef BOOL (WINAPI *SetupDiDestroyDeviceInfoList_t)(_In_ HDEVINFO DeviceInfoSet);
  54. typedef HDEVINFO (WINAPI *SetupDiGetClassDevsExA_t)(_In_opt_ CONST GUID *ClassGuid,_In_opt_ PCSTR Enumerator,_In_opt_ HWND hwndParent,_In_ DWORD Flags,_In_opt_ HDEVINFO DeviceInfoSet,_In_opt_ PCSTR MachineName,_Reserved_ PVOID Reserved);
  55. typedef BOOL (WINAPI *SetupDiOpenDeviceInfoA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PCSTR DeviceInstanceId,_In_opt_ HWND hwndParent,_In_ DWORD OpenFlags,_Out_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  56. typedef BOOL (WINAPI *SetupDiEnumDeviceInfo_t)(_In_ HDEVINFO DeviceInfoSet,_In_ DWORD MemberIndex,_Out_ PSP_DEVINFO_DATA DeviceInfoData);
  57. typedef BOOL (WINAPI *SetupDiSetClassInstallParamsA_t)(_In_ HDEVINFO DeviceInfoSet,_In_opt_ PSP_DEVINFO_DATA DeviceInfoData,_In_reads_bytes_opt_(ClassInstallParamsSize) PSP_CLASSINSTALL_HEADER ClassInstallParams,_In_ DWORD ClassInstallParamsSize);
  58. typedef CONFIGRET (WINAPI *CM_Get_Device_ID_ExA_t)(_In_ DEVINST dnDevInst,_Out_writes_(BufferLen) PSTR Buffer,_In_ ULONG BufferLen,_In_ ULONG ulFlags,_In_opt_ HMACHINE hMachine);
  59. typedef BOOL (WINAPI *SetupDiGetDeviceInstanceIdA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PSP_DEVINFO_DATA DeviceInfoData,_Out_writes_opt_(DeviceInstanceIdSize) PSTR DeviceInstanceId,_In_ DWORD DeviceInstanceIdSize,_Out_opt_ PDWORD RequiredSize);
  60. namespace ZeroTier {
  61. namespace {
  62. // Static/singleton class that when initialized loads a bunch of environment information and a few dynamically loaded DLLs
  63. class WindowsEthernetTapEnv
  64. {
  65. public:
  66. WindowsEthernetTapEnv()
  67. {
  68. #ifdef _WIN64
  69. is64Bit = TRUE;
  70. //tapDriverPath = "\\tap-windows\\x64\\zttap300.inf";
  71. #else
  72. is64Bit = FALSE;
  73. IsWow64Process(GetCurrentProcess(),&is64Bit);
  74. if (is64Bit) {
  75. fprintf(stderr,"FATAL: you must use the 64-bit ZeroTier One service on 64-bit Windows systems\r\n");
  76. _exit(1);
  77. }
  78. //tapDriverPath = "\\tap-windows\\x86\\zttap300.inf";
  79. #endif
  80. tapDriverName = "zttap300";
  81. tapDriverPath = "\\zttap300.inf";
  82. setupApiMod = LoadLibraryA("setupapi.dll");
  83. if (!setupApiMod) {
  84. fprintf(stderr,"FATAL: unable to dynamically load setupapi.dll\r\n");
  85. _exit(1);
  86. }
  87. if (!(this->SetupDiGetINFClassA = (SetupDiGetINFClassA_t)GetProcAddress(setupApiMod,"SetupDiGetINFClassA"))) {
  88. fprintf(stderr,"FATAL: SetupDiGetINFClassA not found in setupapi.dll\r\n");
  89. _exit(1);
  90. }
  91. if (!(this->SetupDiCreateDeviceInfoList = (SetupDiCreateDeviceInfoList_t)GetProcAddress(setupApiMod,"SetupDiCreateDeviceInfoList"))) {
  92. fprintf(stderr,"FATAL: SetupDiCreateDeviceInfoList not found in setupapi.dll\r\n");
  93. _exit(1);
  94. }
  95. if (!(this->SetupDiCreateDeviceInfoA = (SetupDiCreateDeviceInfoA_t)GetProcAddress(setupApiMod,"SetupDiCreateDeviceInfoA"))) {
  96. fprintf(stderr,"FATAL: SetupDiCreateDeviceInfoA not found in setupapi.dll\r\n");
  97. _exit(1);
  98. }
  99. if (!(this->SetupDiSetDeviceRegistryPropertyA = (SetupDiSetDeviceRegistryPropertyA_t)GetProcAddress(setupApiMod,"SetupDiSetDeviceRegistryPropertyA"))) {
  100. fprintf(stderr,"FATAL: SetupDiSetDeviceRegistryPropertyA not found in setupapi.dll\r\n");
  101. _exit(1);
  102. }
  103. if (!(this->SetupDiCallClassInstaller = (SetupDiCallClassInstaller_t)GetProcAddress(setupApiMod,"SetupDiCallClassInstaller"))) {
  104. fprintf(stderr,"FATAL: SetupDiCallClassInstaller not found in setupapi.dll\r\n");
  105. _exit(1);
  106. }
  107. if (!(this->SetupDiDestroyDeviceInfoList = (SetupDiDestroyDeviceInfoList_t)GetProcAddress(setupApiMod,"SetupDiDestroyDeviceInfoList"))) {
  108. fprintf(stderr,"FATAL: SetupDiDestroyDeviceInfoList not found in setupapi.dll\r\n");
  109. _exit(1);
  110. }
  111. if (!(this->SetupDiGetClassDevsExA = (SetupDiGetClassDevsExA_t)GetProcAddress(setupApiMod,"SetupDiGetClassDevsExA"))) {
  112. fprintf(stderr,"FATAL: SetupDiGetClassDevsExA not found in setupapi.dll\r\n");
  113. _exit(1);
  114. }
  115. if (!(this->SetupDiOpenDeviceInfoA = (SetupDiOpenDeviceInfoA_t)GetProcAddress(setupApiMod,"SetupDiOpenDeviceInfoA"))) {
  116. fprintf(stderr,"FATAL: SetupDiOpenDeviceInfoA not found in setupapi.dll\r\n");
  117. _exit(1);
  118. }
  119. if (!(this->SetupDiEnumDeviceInfo = (SetupDiEnumDeviceInfo_t)GetProcAddress(setupApiMod,"SetupDiEnumDeviceInfo"))) {
  120. fprintf(stderr,"FATAL: SetupDiEnumDeviceInfo not found in setupapi.dll\r\n");
  121. _exit(1);
  122. }
  123. if (!(this->SetupDiSetClassInstallParamsA = (SetupDiSetClassInstallParamsA_t)GetProcAddress(setupApiMod,"SetupDiSetClassInstallParamsA"))) {
  124. fprintf(stderr,"FATAL: SetupDiSetClassInstallParamsA not found in setupapi.dll\r\n");
  125. _exit(1);
  126. }
  127. if (!(this->SetupDiGetDeviceInstanceIdA = (SetupDiGetDeviceInstanceIdA_t)GetProcAddress(setupApiMod,"SetupDiGetDeviceInstanceIdA"))) {
  128. fprintf(stderr,"FATAL: SetupDiGetDeviceInstanceIdA not found in setupapi.dll\r\n");
  129. _exit(1);
  130. }
  131. newDevMod = LoadLibraryA("newdev.dll");
  132. if (!newDevMod) {
  133. fprintf(stderr,"FATAL: unable to dynamically load newdev.dll\r\n");
  134. _exit(1);
  135. }
  136. if (!(this->UpdateDriverForPlugAndPlayDevicesA = (UpdateDriverForPlugAndPlayDevicesA_t)GetProcAddress(newDevMod,"UpdateDriverForPlugAndPlayDevicesA"))) {
  137. fprintf(stderr,"FATAL: UpdateDriverForPlugAndPlayDevicesA not found in newdev.dll\r\n");
  138. _exit(1);
  139. }
  140. cfgMgrMod = LoadLibraryA("cfgmgr32.dll");
  141. if (!cfgMgrMod) {
  142. fprintf(stderr,"FATAL: unable to dynamically load cfgmgr32.dll\r\n");
  143. _exit(1);
  144. }
  145. if (!(this->CM_Get_Device_ID_ExA = (CM_Get_Device_ID_ExA_t)GetProcAddress(cfgMgrMod,"CM_Get_Device_ID_ExA"))) {
  146. fprintf(stderr,"FATAL: CM_Get_Device_ID_ExA not found in cfgmgr32.dll\r\n");
  147. _exit(1);
  148. }
  149. }
  150. BOOL is64Bit; // is the system 64-bit, regardless of whether this binary is or not
  151. std::string tapDriverPath;
  152. std::string tapDriverName;
  153. UpdateDriverForPlugAndPlayDevicesA_t UpdateDriverForPlugAndPlayDevicesA;
  154. SetupDiGetINFClassA_t SetupDiGetINFClassA;
  155. SetupDiCreateDeviceInfoList_t SetupDiCreateDeviceInfoList;
  156. SetupDiCreateDeviceInfoA_t SetupDiCreateDeviceInfoA;
  157. SetupDiSetDeviceRegistryPropertyA_t SetupDiSetDeviceRegistryPropertyA;
  158. SetupDiCallClassInstaller_t SetupDiCallClassInstaller;
  159. SetupDiDestroyDeviceInfoList_t SetupDiDestroyDeviceInfoList;
  160. SetupDiGetClassDevsExA_t SetupDiGetClassDevsExA;
  161. SetupDiOpenDeviceInfoA_t SetupDiOpenDeviceInfoA;
  162. SetupDiEnumDeviceInfo_t SetupDiEnumDeviceInfo;
  163. SetupDiSetClassInstallParamsA_t SetupDiSetClassInstallParamsA;
  164. SetupDiGetDeviceInstanceIdA_t SetupDiGetDeviceInstanceIdA;
  165. CM_Get_Device_ID_ExA_t CM_Get_Device_ID_ExA;
  166. private:
  167. HMODULE setupApiMod;
  168. HMODULE newDevMod;
  169. HMODULE cfgMgrMod;
  170. };
  171. static const WindowsEthernetTapEnv WINENV;
  172. // Only create or delete devices one at a time
  173. static Mutex _systemTapInitLock;
  174. // Only perform installation or uninstallation options one at a time
  175. static Mutex _systemDeviceManagementLock;
  176. } // anonymous namespace
  177. std::string WindowsEthernetTap::addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId)
  178. {
  179. Mutex::Lock _l(_systemDeviceManagementLock);
  180. GUID classGuid;
  181. char className[1024];
  182. if (!WINENV.SetupDiGetINFClassA(pathToInf,&classGuid,className,sizeof(className),(PDWORD)0)) {
  183. return std::string("SetupDiGetINFClassA() failed -- unable to read zttap driver INF file");
  184. }
  185. HDEVINFO deviceInfoSet = WINENV.SetupDiCreateDeviceInfoList(&classGuid,(HWND)0);
  186. if (deviceInfoSet == INVALID_HANDLE_VALUE) {
  187. return std::string("SetupDiCreateDeviceInfoList() failed");
  188. }
  189. SP_DEVINFO_DATA deviceInfoData;
  190. memset(&deviceInfoData,0,sizeof(deviceInfoData));
  191. deviceInfoData.cbSize = sizeof(deviceInfoData);
  192. if (!WINENV.SetupDiCreateDeviceInfoA(deviceInfoSet,className,&classGuid,(PCSTR)0,(HWND)0,DICD_GENERATE_ID,&deviceInfoData)) {
  193. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  194. return std::string("SetupDiCreateDeviceInfoA() failed");
  195. }
  196. if (!WINENV.SetupDiSetDeviceRegistryPropertyA(deviceInfoSet,&deviceInfoData,SPDRP_HARDWAREID,(const BYTE *)WINENV.tapDriverName.c_str(),(DWORD)(WINENV.tapDriverName.length() + 1))) {
  197. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  198. return std::string("SetupDiSetDeviceRegistryPropertyA() failed");
  199. }
  200. if (!WINENV.SetupDiCallClassInstaller(DIF_REGISTERDEVICE,deviceInfoSet,&deviceInfoData)) {
  201. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  202. return std::string("SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed");
  203. }
  204. // HACK: During upgrades, this can fail while the installer is still running. So make 60 attempts
  205. // with a 1s delay between each attempt.
  206. bool driverInstalled = false;
  207. for(int retryCounter=0;retryCounter<60;++retryCounter) {
  208. BOOL rebootRequired = FALSE;
  209. if (WINENV.UpdateDriverForPlugAndPlayDevicesA((HWND)0,WINENV.tapDriverName.c_str(),pathToInf,INSTALLFLAG_FORCE|INSTALLFLAG_NONINTERACTIVE,&rebootRequired)) {
  210. driverInstalled = true;
  211. break;
  212. } else Sleep(1000);
  213. }
  214. if (!driverInstalled) {
  215. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  216. return std::string("UpdateDriverForPlugAndPlayDevices() failed (made 60 attempts)");
  217. }
  218. char iidbuf[1024];
  219. DWORD iidReqSize = sizeof(iidbuf);
  220. if (WINENV.SetupDiGetDeviceInstanceIdA(deviceInfoSet,&deviceInfoData,iidbuf,sizeof(iidbuf),&iidReqSize)) {
  221. deviceInstanceId = iidbuf;
  222. } // failure here is not fatal since we only need this on Vista and 2008 -- other versions fill it into the registry automatically
  223. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  224. return std::string();
  225. }
  226. std::string WindowsEthernetTap::destroyAllLegacyPersistentTapDevices()
  227. {
  228. char subkeyName[1024];
  229. char subkeyClass[1024];
  230. char data[1024];
  231. std::set<std::string> instanceIdPathsToRemove;
  232. {
  233. HKEY nwAdapters;
  234. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  235. return std::string("Could not open registry key");
  236. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  237. DWORD type;
  238. DWORD dataLen;
  239. DWORD subkeyNameLen = sizeof(subkeyName);
  240. DWORD subkeyClassLen = sizeof(subkeyClass);
  241. FILETIME lastWriteTime;
  242. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  243. type = 0;
  244. dataLen = sizeof(data);
  245. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  246. data[dataLen] = '\0';
  247. if ((!strnicmp(data,"zttap",5))&&(WINENV.tapDriverName != data)) {
  248. std::string instanceIdPath;
  249. type = 0;
  250. dataLen = sizeof(data);
  251. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  252. instanceIdPath.assign(data,dataLen);
  253. if (instanceIdPath.length() != 0)
  254. instanceIdPathsToRemove.insert(instanceIdPath);
  255. }
  256. }
  257. } else break; // end of list or failure
  258. }
  259. RegCloseKey(nwAdapters);
  260. }
  261. std::string errlist;
  262. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp) {
  263. std::string err = deletePersistentTapDevice(iidp->c_str());
  264. if (err.length() > 0) {
  265. if (errlist.length() > 0)
  266. errlist.push_back(',');
  267. errlist.append(err);
  268. }
  269. }
  270. return errlist;
  271. }
  272. std::string WindowsEthernetTap::destroyAllPersistentTapDevices()
  273. {
  274. char subkeyName[1024];
  275. char subkeyClass[1024];
  276. char data[1024];
  277. std::set<std::string> instanceIdPathsToRemove;
  278. {
  279. HKEY nwAdapters;
  280. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  281. return std::string("Could not open registry key");
  282. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  283. DWORD type;
  284. DWORD dataLen;
  285. DWORD subkeyNameLen = sizeof(subkeyName);
  286. DWORD subkeyClassLen = sizeof(subkeyClass);
  287. FILETIME lastWriteTime;
  288. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  289. type = 0;
  290. dataLen = sizeof(data);
  291. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  292. data[dataLen] = '\0';
  293. if (!strnicmp(data,"zttap",5)) {
  294. std::string instanceIdPath;
  295. type = 0;
  296. dataLen = sizeof(data);
  297. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  298. instanceIdPath.assign(data,dataLen);
  299. if (instanceIdPath.length() != 0)
  300. instanceIdPathsToRemove.insert(instanceIdPath);
  301. }
  302. }
  303. } else break; // end of list or failure
  304. }
  305. RegCloseKey(nwAdapters);
  306. }
  307. std::string errlist;
  308. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp) {
  309. std::string err = deletePersistentTapDevice(iidp->c_str());
  310. if (err.length() > 0) {
  311. if (errlist.length() > 0)
  312. errlist.push_back(',');
  313. errlist.append(err);
  314. }
  315. }
  316. return errlist;
  317. }
  318. std::string WindowsEthernetTap::deletePersistentTapDevice(const char *instanceId)
  319. {
  320. char iid[256];
  321. SP_REMOVEDEVICE_PARAMS rmdParams;
  322. memset(&rmdParams,0,sizeof(rmdParams));
  323. rmdParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  324. rmdParams.ClassInstallHeader.InstallFunction = DIF_REMOVE;
  325. rmdParams.Scope = DI_REMOVEDEVICE_GLOBAL;
  326. rmdParams.HwProfile = 0;
  327. Mutex::Lock _l(_systemDeviceManagementLock);
  328. HDEVINFO devInfo = WINENV.SetupDiGetClassDevsExA((const GUID *)0,(PCSTR)0,(HWND)0,DIGCF_ALLCLASSES,(HDEVINFO)0,(PCSTR)0,(PVOID)0);
  329. if (devInfo == INVALID_HANDLE_VALUE)
  330. return std::string("SetupDiGetClassDevsExA() failed");
  331. WINENV.SetupDiOpenDeviceInfoA(devInfo,instanceId,(HWND)0,0,(PSP_DEVINFO_DATA)0);
  332. SP_DEVINFO_DATA devInfoData;
  333. memset(&devInfoData,0,sizeof(devInfoData));
  334. devInfoData.cbSize = sizeof(devInfoData);
  335. for(DWORD devIndex=0;WINENV.SetupDiEnumDeviceInfo(devInfo,devIndex,&devInfoData);devIndex++) {
  336. if ((WINENV.CM_Get_Device_ID_ExA(devInfoData.DevInst,iid,sizeof(iid),0,(HMACHINE)0) == CR_SUCCESS)&&(!strcmp(iid,instanceId))) {
  337. if (!WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&rmdParams.ClassInstallHeader,sizeof(rmdParams))) {
  338. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  339. return std::string("SetupDiSetClassInstallParams() failed");
  340. }
  341. if (!WINENV.SetupDiCallClassInstaller(DIF_REMOVE,devInfo,&devInfoData)) {
  342. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  343. return std::string("SetupDiCallClassInstaller(DIF_REMOVE) failed");
  344. }
  345. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  346. return std::string();
  347. }
  348. }
  349. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  350. return std::string("instance ID not found");
  351. }
  352. bool WindowsEthernetTap::setPersistentTapDeviceState(const char *instanceId,bool enabled)
  353. {
  354. char iid[256];
  355. SP_PROPCHANGE_PARAMS params;
  356. Mutex::Lock _l(_systemDeviceManagementLock);
  357. HDEVINFO devInfo = WINENV.SetupDiGetClassDevsExA((const GUID *)0,(PCSTR)0,(HWND)0,DIGCF_ALLCLASSES,(HDEVINFO)0,(PCSTR)0,(PVOID)0);
  358. if (devInfo == INVALID_HANDLE_VALUE)
  359. return false;
  360. WINENV.SetupDiOpenDeviceInfoA(devInfo,instanceId,(HWND)0,0,(PSP_DEVINFO_DATA)0);
  361. SP_DEVINFO_DATA devInfoData;
  362. memset(&devInfoData,0,sizeof(devInfoData));
  363. devInfoData.cbSize = sizeof(devInfoData);
  364. for(DWORD devIndex=0;WINENV.SetupDiEnumDeviceInfo(devInfo,devIndex,&devInfoData);devIndex++) {
  365. if ((WINENV.CM_Get_Device_ID_ExA(devInfoData.DevInst,iid,sizeof(iid),0,(HMACHINE)0) == CR_SUCCESS)&&(!strcmp(iid,instanceId))) {
  366. memset(&params,0,sizeof(params));
  367. params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  368. params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  369. params.StateChange = enabled ? DICS_ENABLE : DICS_DISABLE;
  370. params.Scope = DICS_FLAG_GLOBAL;
  371. params.HwProfile = 0;
  372. WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&params.ClassInstallHeader,sizeof(params));
  373. WINENV.SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,devInfo,&devInfoData);
  374. memset(&params,0,sizeof(params));
  375. params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  376. params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  377. params.StateChange = enabled ? DICS_ENABLE : DICS_DISABLE;
  378. params.Scope = DICS_FLAG_CONFIGSPECIFIC;
  379. params.HwProfile = 0;
  380. WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&params.ClassInstallHeader,sizeof(params));
  381. WINENV.SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,devInfo,&devInfoData);
  382. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  383. return true;
  384. }
  385. }
  386. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  387. return false;
  388. }
  389. WindowsEthernetTap::WindowsEthernetTap(
  390. const char *hp,
  391. const MAC &mac,
  392. unsigned int mtu,
  393. unsigned int metric,
  394. uint64_t nwid,
  395. const char *friendlyName,
  396. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  397. void *arg) :
  398. _handler(handler),
  399. _arg(arg),
  400. _mac(mac),
  401. _nwid(nwid),
  402. _mtu(mtu),
  403. _tap(INVALID_HANDLE_VALUE),
  404. _friendlyName(friendlyName),
  405. _injectSemaphore(INVALID_HANDLE_VALUE),
  406. _pathToHelpers(hp),
  407. _run(true),
  408. _initialized(false),
  409. _enabled(true),
  410. _lastIfAddrsUpdate(0)
  411. {
  412. char subkeyName[1024];
  413. char subkeyClass[1024];
  414. char data[1024];
  415. char tag[24];
  416. // We "tag" registry entries with the network ID to identify persistent devices
  417. OSUtils::ztsnprintf(tag,sizeof(tag),"%.16llx",(unsigned long long)nwid);
  418. Mutex::Lock _l(_systemTapInitLock);
  419. HKEY nwAdapters;
  420. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  421. throw std::runtime_error("unable to open registry key for network adapter enumeration");
  422. // Look for the tap instance that corresponds with this network
  423. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  424. DWORD type;
  425. DWORD dataLen;
  426. DWORD subkeyNameLen = sizeof(subkeyName);
  427. DWORD subkeyClassLen = sizeof(subkeyClass);
  428. FILETIME lastWriteTime;
  429. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  430. type = 0;
  431. dataLen = sizeof(data);
  432. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  433. data[dataLen] = (char)0;
  434. if (WINENV.tapDriverName == data) {
  435. std::string instanceId;
  436. type = 0;
  437. dataLen = sizeof(data);
  438. if (RegGetValueA(nwAdapters,subkeyName,"NetCfgInstanceId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  439. instanceId.assign(data,dataLen);
  440. std::string instanceIdPath;
  441. type = 0;
  442. dataLen = sizeof(data);
  443. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  444. instanceIdPath.assign(data,dataLen);
  445. if ((_netCfgInstanceId.length() == 0)&&(instanceId.length() != 0)&&(instanceIdPath.length() != 0)) {
  446. type = 0;
  447. dataLen = sizeof(data);
  448. if (RegGetValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  449. data[dataLen] = '\0';
  450. if (!strcmp(data,tag)) {
  451. _netCfgInstanceId = instanceId;
  452. _deviceInstanceId = instanceIdPath;
  453. _mySubkeyName = subkeyName;
  454. break; // found it!
  455. }
  456. }
  457. }
  458. }
  459. }
  460. } else break; // no more subkeys or error occurred enumerating them
  461. }
  462. // If there is no device, try to create one
  463. bool creatingNewDevice = (_netCfgInstanceId.length() == 0);
  464. std::string newDeviceInstanceId;
  465. if (creatingNewDevice) {
  466. for(int getNewAttemptCounter=0;getNewAttemptCounter<2;++getNewAttemptCounter) {
  467. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  468. DWORD type;
  469. DWORD dataLen;
  470. DWORD subkeyNameLen = sizeof(subkeyName);
  471. DWORD subkeyClassLen = sizeof(subkeyClass);
  472. FILETIME lastWriteTime;
  473. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  474. type = 0;
  475. dataLen = sizeof(data);
  476. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  477. data[dataLen] = '\0';
  478. if (WINENV.tapDriverName == data) {
  479. type = 0;
  480. dataLen = sizeof(data);
  481. if ((RegGetValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",RRF_RT_ANY,&type,(PVOID)data,&dataLen) != ERROR_SUCCESS)||(dataLen <= 0)) {
  482. type = 0;
  483. dataLen = sizeof(data);
  484. if (RegGetValueA(nwAdapters,subkeyName,"NetCfgInstanceId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  485. RegSetKeyValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",REG_SZ,tag,(DWORD)(strlen(tag)+1));
  486. _netCfgInstanceId.assign(data,dataLen);
  487. type = 0;
  488. dataLen = sizeof(data);
  489. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  490. _deviceInstanceId.assign(data,dataLen);
  491. _mySubkeyName = subkeyName;
  492. // Disable DHCP by default on new devices
  493. HKEY tcpIpInterfaces;
  494. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  495. DWORD enable = 0;
  496. RegSetKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),"EnableDHCP",REG_DWORD,&enable,sizeof(enable));
  497. RegCloseKey(tcpIpInterfaces);
  498. }
  499. break; // found an unused zttap device
  500. }
  501. }
  502. }
  503. }
  504. } else break; // no more keys or error occurred
  505. }
  506. if (_netCfgInstanceId.length() > 0) {
  507. break; // found an unused zttap device
  508. } else {
  509. // no unused zttap devices, so create one
  510. std::string errm = addNewPersistentTapDevice((std::string(_pathToHelpers) + WINENV.tapDriverPath).c_str(),newDeviceInstanceId);
  511. if (errm.length() > 0)
  512. throw std::runtime_error(std::string("unable to create new device instance: ")+errm);
  513. }
  514. }
  515. }
  516. if (_netCfgInstanceId.length() > 0) {
  517. char tmps[64];
  518. unsigned int tmpsl = OSUtils::ztsnprintf(tmps,sizeof(tmps),"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",(unsigned int)mac[0],(unsigned int)mac[1],(unsigned int)mac[2],(unsigned int)mac[3],(unsigned int)mac[4],(unsigned int)mac[5]) + 1;
  519. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"NetworkAddress",REG_SZ,tmps,tmpsl);
  520. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MAC",REG_SZ,tmps,tmpsl);
  521. tmpsl = OSUtils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu);
  522. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MTU",REG_SZ,tmps,tmpsl);
  523. DWORD tmp = 0;
  524. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"*NdisDeviceType",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  525. tmp = IF_TYPE_ETHERNET_CSMACD;
  526. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"*IfType",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  527. if (creatingNewDevice) {
  528. // Vista/2008 does not set this
  529. if (newDeviceInstanceId.length() > 0)
  530. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"DeviceInstanceID",REG_SZ,newDeviceInstanceId.c_str(),(DWORD)newDeviceInstanceId.length());
  531. // Set EnableDHCP to 0 by default on new devices
  532. tmp = 0;
  533. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"EnableDHCP",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  534. }
  535. RegCloseKey(nwAdapters);
  536. } else {
  537. RegCloseKey(nwAdapters);
  538. throw std::runtime_error("unable to find or create tap adapter");
  539. }
  540. {
  541. char nobraces[128]; // strip braces from GUID before converting it, because Windows
  542. const char *nbtmp1 = _netCfgInstanceId.c_str();
  543. char *nbtmp2 = nobraces;
  544. while (*nbtmp1) {
  545. if ((*nbtmp1 != '{')&&(*nbtmp1 != '}'))
  546. *nbtmp2++ = *nbtmp1;
  547. ++nbtmp1;
  548. }
  549. *nbtmp2 = (char)0;
  550. if (UuidFromStringA((RPC_CSTR)nobraces,&_deviceGuid) != RPC_S_OK)
  551. throw std::runtime_error("unable to convert instance ID GUID to native GUID (invalid NetCfgInstanceId in registry?)");
  552. }
  553. // Get the LUID, which is one of like four fucking ways to refer to a network device in Windows
  554. if (ConvertInterfaceGuidToLuid(&_deviceGuid,&_deviceLuid) != NO_ERROR)
  555. throw std::runtime_error("unable to convert device interface GUID to LUID");
  556. //_initialized = true;
  557. if (friendlyName)
  558. setFriendlyName(friendlyName);
  559. _injectSemaphore = CreateSemaphore(NULL,0,1,NULL);
  560. _thread = Thread::start(this);
  561. }
  562. WindowsEthernetTap::~WindowsEthernetTap()
  563. {
  564. WinDNSHelper::removeDNS(_nwid);
  565. _run = false;
  566. ReleaseSemaphore(_injectSemaphore,1,NULL);
  567. Thread::join(_thread);
  568. CloseHandle(_injectSemaphore);
  569. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  570. }
  571. void WindowsEthernetTap::setEnabled(bool en)
  572. {
  573. _enabled = en;
  574. }
  575. bool WindowsEthernetTap::enabled() const
  576. {
  577. return _enabled;
  578. }
  579. bool WindowsEthernetTap::addIp(const InetAddress &ip)
  580. {
  581. if (!ip.netmaskBits()) // sanity check... netmask of 0.0.0.0 is WUT?
  582. return false;
  583. Mutex::Lock _l(_assignedIps_m);
  584. if (std::find(_assignedIps.begin(),_assignedIps.end(),ip) != _assignedIps.end())
  585. return true;
  586. _assignedIps.push_back(ip);
  587. _syncIps();
  588. return true;
  589. }
  590. bool WindowsEthernetTap::removeIp(const InetAddress &ip)
  591. {
  592. if (ip.isV6())
  593. return true;
  594. {
  595. Mutex::Lock _l(_assignedIps_m);
  596. std::vector<InetAddress>::iterator aip(std::find(_assignedIps.begin(),_assignedIps.end(),ip));
  597. if (aip != _assignedIps.end())
  598. _assignedIps.erase(aip);
  599. }
  600. if (!_initialized)
  601. return false;
  602. try {
  603. MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
  604. if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
  605. if ((ipt)&&(ipt->NumEntries > 0)) {
  606. for(DWORD i=0;i<(DWORD)ipt->NumEntries;++i) {
  607. if (ipt->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  608. InetAddress addr;
  609. switch(ipt->Table[i].Address.si_family) {
  610. case AF_INET:
  611. addr.set(&(ipt->Table[i].Address.Ipv4.sin_addr.S_un.S_addr),4,ipt->Table[i].OnLinkPrefixLength);
  612. break;
  613. case AF_INET6:
  614. addr.set(ipt->Table[i].Address.Ipv6.sin6_addr.u.Byte,16,ipt->Table[i].OnLinkPrefixLength);
  615. if (addr.ipScope() == InetAddress::IP_SCOPE_LINK_LOCAL)
  616. continue; // can't remove link-local IPv6 addresses
  617. break;
  618. }
  619. if (addr == ip) {
  620. DeleteUnicastIpAddressEntry(&(ipt->Table[i]));
  621. FreeMibTable(ipt);
  622. if (ip.isV4()) {
  623. std::vector<std::string> regIps(_getRegistryIPv4Value("IPAddress"));
  624. std::vector<std::string> regSubnetMasks(_getRegistryIPv4Value("SubnetMask"));
  625. char ipbuf[64];
  626. std::string ipstr(ip.toIpString(ipbuf));
  627. for (std::vector<std::string>::iterator rip(regIps.begin()), rm(regSubnetMasks.begin()); ((rip != regIps.end()) && (rm != regSubnetMasks.end())); ++rip, ++rm) {
  628. if (*rip == ipstr) {
  629. regIps.erase(rip);
  630. regSubnetMasks.erase(rm);
  631. _setRegistryIPv4Value("IPAddress", regIps);
  632. _setRegistryIPv4Value("SubnetMask", regSubnetMasks);
  633. break;
  634. }
  635. }
  636. }
  637. return true;
  638. }
  639. }
  640. }
  641. }
  642. FreeMibTable((PVOID)ipt);
  643. }
  644. } catch ( ... ) {}
  645. return false;
  646. }
  647. std::vector<InetAddress> WindowsEthernetTap::ips() const
  648. {
  649. static const InetAddress linkLocalLoopback("fe80::1/64"); // what is this and why does Windows assign it?
  650. std::vector<InetAddress> addrs;
  651. if (!_initialized)
  652. return addrs;
  653. uint64_t now = OSUtils::now();
  654. if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
  655. return _ifaddrs;
  656. }
  657. _lastIfAddrsUpdate = now;
  658. try {
  659. MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
  660. if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
  661. if ((ipt)&&(ipt->NumEntries > 0)) {
  662. for(DWORD i=0;i<(DWORD)ipt->NumEntries;++i) {
  663. if (ipt->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  664. switch(ipt->Table[i].Address.si_family) {
  665. case AF_INET: {
  666. InetAddress ip(&(ipt->Table[i].Address.Ipv4.sin_addr.S_un.S_addr),4,ipt->Table[i].OnLinkPrefixLength);
  667. if (ip != InetAddress::LO4)
  668. addrs.push_back(ip);
  669. } break;
  670. case AF_INET6: {
  671. InetAddress ip(ipt->Table[i].Address.Ipv6.sin6_addr.u.Byte,16,ipt->Table[i].OnLinkPrefixLength);
  672. if ((ip != linkLocalLoopback)&&(ip != InetAddress::LO6))
  673. addrs.push_back(ip);
  674. } break;
  675. }
  676. }
  677. }
  678. }
  679. FreeMibTable(ipt);
  680. }
  681. } catch ( ... ) {} // sanity check, shouldn't happen unless out of memory
  682. std::sort(addrs.begin(),addrs.end());
  683. addrs.erase(std::unique(addrs.begin(),addrs.end()),addrs.end());
  684. _ifaddrs = addrs;
  685. return addrs;
  686. }
  687. void WindowsEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  688. {
  689. if ((!_initialized)||(!_enabled)||(_tap == INVALID_HANDLE_VALUE)||(len > _mtu))
  690. return;
  691. Mutex::Lock _l(_injectPending_m);
  692. _injectPending.emplace();
  693. _injectPending.back().len = len + 14;
  694. char *const d = _injectPending.back().data;
  695. to.copyTo(d,6);
  696. from.copyTo(d + 6,6);
  697. d[12] = (char)((etherType >> 8) & 0xff);
  698. d[13] = (char)(etherType & 0xff);
  699. memcpy(d + 14,data,len);
  700. ReleaseSemaphore(_injectSemaphore,1,NULL);
  701. }
  702. std::string WindowsEthernetTap::deviceName() const
  703. {
  704. char tmp[1024];
  705. if (ConvertInterfaceLuidToNameA(&_deviceLuid,tmp,sizeof(tmp)) != NO_ERROR)
  706. return std::string("[ConvertInterfaceLuidToName() failed]");
  707. return std::string(tmp);
  708. }
  709. void WindowsEthernetTap::setFriendlyName(const char *dn)
  710. {
  711. if (!_initialized)
  712. return;
  713. HKEY ifp;
  714. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,(std::string("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\") + _netCfgInstanceId).c_str(),0,KEY_READ|KEY_WRITE,&ifp) == ERROR_SUCCESS) {
  715. RegSetKeyValueA(ifp,"Connection","Name",REG_SZ,(LPCVOID)dn,(DWORD)(strlen(dn)+1));
  716. RegCloseKey(ifp);
  717. }
  718. HRESULT hr = S_OK;
  719. INetSharingManager *nsm;
  720. hr = CoCreateInstance(__uuidof(NetSharingManager), NULL, CLSCTX_ALL, __uuidof(INetSharingManager), (void**)&nsm);
  721. if (hr != S_OK) return;
  722. bool found = false;
  723. INetSharingEveryConnectionCollection *nsecc = nullptr;
  724. hr = nsm->get_EnumEveryConnection(&nsecc);
  725. if (!nsecc) {
  726. fprintf(stderr, "Failed to get NSM connections");
  727. return;
  728. }
  729. IEnumVARIANT *ev = nullptr;
  730. IUnknown *unk = nullptr;
  731. hr = nsecc->get__NewEnum(&unk);
  732. if (unk) {
  733. hr = unk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&ev);
  734. unk->Release();
  735. }
  736. if (ev) {
  737. VARIANT v;
  738. VariantInit(&v);
  739. while ((S_OK == ev->Next(1, &v, NULL)) && found == FALSE) {
  740. if (V_VT(&v) == VT_UNKNOWN) {
  741. INetConnection *nc = nullptr;
  742. V_UNKNOWN(&v)->QueryInterface(__uuidof(INetConnection), (void**)&nc);
  743. if (nc) {
  744. NETCON_PROPERTIES *ncp = nullptr;
  745. nc->GetProperties(&ncp);
  746. if (ncp != nullptr) {
  747. GUID curId = ncp->guidId;
  748. if (curId == _deviceGuid) {
  749. wchar_t wtext[255];
  750. mbstowcs(wtext, dn, strlen(dn)+1);
  751. nc->Rename(wtext);
  752. found = true;
  753. }
  754. }
  755. nc->Release();
  756. }
  757. }
  758. VariantClear(&v);
  759. }
  760. ev->Release();
  761. }
  762. nsecc->Release();
  763. _friendlyName_m.lock();
  764. _friendlyName = dn;
  765. _friendlyName_m.unlock();
  766. }
  767. std::string WindowsEthernetTap::friendlyName() const
  768. {
  769. Mutex::Lock l(_friendlyName_m);
  770. return _friendlyName;
  771. }
  772. void WindowsEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  773. {
  774. if (!_initialized)
  775. return;
  776. HANDLE t = _tap;
  777. if (t == INVALID_HANDLE_VALUE)
  778. return;
  779. std::vector<MulticastGroup> newGroups;
  780. // The ZT1 tap driver supports an IOCTL to get multicast memberships at the L2
  781. // level... something Windows does not seem to expose ordinarily. This lets
  782. // pretty much anything work... IPv4, IPv6, IPX, oldskool Netbios, who knows...
  783. unsigned char mcastbuf[TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS_OUTPUT_BUF_SIZE];
  784. DWORD bytesReturned = 0;
  785. if (DeviceIoControl(t,TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS,(LPVOID)mcastbuf,sizeof(mcastbuf),(LPVOID)mcastbuf,sizeof(mcastbuf),&bytesReturned,NULL)) {
  786. if ((bytesReturned > 0)&&(bytesReturned <= TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS_OUTPUT_BUF_SIZE)) { // sanity check
  787. MAC mac;
  788. DWORD i = 0;
  789. while ((i + 6) <= bytesReturned) {
  790. mac.setTo(mcastbuf + i,6);
  791. i += 6;
  792. if ((mac.isMulticast())&&(!mac.isBroadcast())) {
  793. // exclude the nulls that may be returned or any other junk Windows puts in there
  794. newGroups.push_back(MulticastGroup(mac,0));
  795. }
  796. }
  797. }
  798. }
  799. std::vector<InetAddress> allIps(ips());
  800. for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
  801. newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
  802. std::sort(newGroups.begin(),newGroups.end());
  803. newGroups.erase(std::unique(newGroups.begin(),newGroups.end()),newGroups.end());
  804. for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
  805. if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
  806. added.push_back(*m);
  807. }
  808. for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
  809. if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
  810. removed.push_back(*m);
  811. }
  812. _multicastGroups.swap(newGroups);
  813. }
  814. void WindowsEthernetTap::setMtu(unsigned int mtu)
  815. {
  816. if (mtu != _mtu) {
  817. _mtu = mtu;
  818. HKEY nwAdapters;
  819. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ | KEY_WRITE, &nwAdapters) == ERROR_SUCCESS) {
  820. char tmps[64];
  821. unsigned int tmpsl = OSUtils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu);
  822. RegSetKeyValueA(nwAdapters, _mySubkeyName.c_str(), "MTU", REG_SZ, tmps, tmpsl);
  823. RegCloseKey(nwAdapters);
  824. }
  825. }
  826. }
  827. NET_IFINDEX WindowsEthernetTap::interfaceIndex() const
  828. {
  829. NET_IFINDEX idx = -1;
  830. if (ConvertInterfaceLuidToIndex(&_deviceLuid,&idx) == NO_ERROR)
  831. return idx;
  832. return -1;
  833. }
  834. void WindowsEthernetTap::threadMain()
  835. throw()
  836. {
  837. HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  838. if (FAILED(hres)) {
  839. fprintf(stderr, "WinEthernetTap: COM initialization failed");
  840. return;
  841. }
  842. char tapReadBuf[ZT_MAX_MTU + 32];
  843. char tapPath[128];
  844. HANDLE wait4[3];
  845. OVERLAPPED tapOvlRead,tapOvlWrite;
  846. OSUtils::ztsnprintf(tapPath,sizeof(tapPath),"\\\\.\\Global\\%s.tap",_netCfgInstanceId.c_str());
  847. try {
  848. while (_run) {
  849. // Because Windows
  850. Sleep(250);
  851. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  852. Sleep(250);
  853. setPersistentTapDeviceState(_deviceInstanceId.c_str(),true);
  854. Sleep(250);
  855. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  856. Sleep(250);
  857. setPersistentTapDeviceState(_deviceInstanceId.c_str(),true);
  858. Sleep(250);
  859. _tap = CreateFileA(tapPath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM|FILE_FLAG_OVERLAPPED,NULL);
  860. if (_tap == INVALID_HANDLE_VALUE) {
  861. Sleep(250);
  862. continue;
  863. }
  864. {
  865. uint32_t tmpi = 1;
  866. DWORD bytesReturned = 0;
  867. DeviceIoControl(_tap,TAP_WIN_IOCTL_SET_MEDIA_STATUS,&tmpi,sizeof(tmpi),&tmpi,sizeof(tmpi),&bytesReturned,NULL);
  868. }
  869. #ifdef ZT_WINDOWS_CREATE_FAKE_DEFAULT_ROUTE
  870. {
  871. /* This inserts a fake default route and a fake ARP entry, forcing
  872. * Windows to detect this as a "real" network and apply proper
  873. * firewall rules.
  874. *
  875. * This hack is completely stupid, but Windows made me do it
  876. * by being broken and insane.
  877. *
  878. * Background: Windows tries to detect its network location by
  879. * matching it to the ARP address of the default route. Networks
  880. * without default routes are "unidentified networks" and cannot
  881. * have their firewall classification changed by the user (easily).
  882. *
  883. * Yes, you read that right.
  884. *
  885. * The common workaround is to set *NdisDeviceType to 1, which
  886. * totally disables all Windows firewall functionality. This is
  887. * the answer you'll find on most forums for things like OpenVPN.
  888. *
  889. * Yes, you read that right.
  890. *
  891. * The default route workaround is also known, but for this to
  892. * work there must be a known default IP that resolves to a known
  893. * ARP address. This works for an OpenVPN tunnel, but not here
  894. * because this isn't a tunnel. It's a mesh. There is no "other
  895. * end," or any other known always on IP.
  896. *
  897. * So let's make a fake one and shove it in there along with its
  898. * fake static ARP entry. Also makes it instant-on and static.
  899. *
  900. * We'll have to see what DHCP does with this. In the future we
  901. * probably will not want to do this on DHCP-enabled networks, so
  902. * when we enable DHCP we will go in and yank this wacko hacko from
  903. * the routing table before doing so.
  904. *
  905. * Like Jesse Pinkman would say: "YEEEEAAH BITCH!" */
  906. const uint32_t fakeIp = htonl(0x19fffffe); // 25.255.255.254 -- unrouted IPv4 block
  907. for(int i=0;i<8;++i) {
  908. MIB_IPNET_ROW2 ipnr;
  909. memset(&ipnr,0,sizeof(ipnr));
  910. ipnr.Address.si_family = AF_INET;
  911. ipnr.Address.Ipv4.sin_addr.s_addr = fakeIp;
  912. ipnr.InterfaceLuid.Value = _deviceLuid.Value;
  913. ipnr.PhysicalAddress[0] = _mac[0] ^ 0x10; // just make something up that's consistent and not part of this net
  914. ipnr.PhysicalAddress[1] = 0x00;
  915. ipnr.PhysicalAddress[2] = (UCHAR)((_deviceGuid.Data1 >> 24) & 0xff);
  916. ipnr.PhysicalAddress[3] = (UCHAR)((_deviceGuid.Data1 >> 16) & 0xff);
  917. ipnr.PhysicalAddress[4] = (UCHAR)((_deviceGuid.Data1 >> 8) & 0xff);
  918. ipnr.PhysicalAddress[5] = (UCHAR)(_deviceGuid.Data1 & 0xff);
  919. ipnr.PhysicalAddressLength = 6;
  920. ipnr.State = NlnsPermanent;
  921. ipnr.IsRouter = 1;
  922. ipnr.IsUnreachable = 0;
  923. ipnr.ReachabilityTime.LastReachable = 0x0fffffff;
  924. ipnr.ReachabilityTime.LastUnreachable = 1;
  925. DWORD result = CreateIpNetEntry2(&ipnr);
  926. if (result != NO_ERROR)
  927. Sleep(250);
  928. else break;
  929. }
  930. for(int i=0;i<8;++i) {
  931. MIB_IPFORWARD_ROW2 nr;
  932. memset(&nr,0,sizeof(nr));
  933. InitializeIpForwardEntry(&nr);
  934. nr.InterfaceLuid.Value = _deviceLuid.Value;
  935. nr.DestinationPrefix.Prefix.si_family = AF_INET; // rest is left as 0.0.0.0/0
  936. nr.NextHop.si_family = AF_INET;
  937. nr.NextHop.Ipv4.sin_addr.s_addr = fakeIp;
  938. nr.Metric = 9999; // do not use as real default route
  939. nr.Protocol = MIB_IPPROTO_NETMGMT;
  940. DWORD result = CreateIpForwardEntry2(&nr);
  941. if (result != NO_ERROR)
  942. Sleep(250);
  943. else break;
  944. }
  945. }
  946. #endif
  947. // Assign or re-assign any should-be-assigned IPs in case we have restarted
  948. {
  949. Mutex::Lock _l(_assignedIps_m);
  950. _syncIps();
  951. }
  952. memset(&tapOvlRead,0,sizeof(tapOvlRead));
  953. tapOvlRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  954. memset(&tapOvlWrite,0,sizeof(tapOvlWrite));
  955. tapOvlWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  956. wait4[0] = _injectSemaphore;
  957. wait4[1] = tapOvlRead.hEvent;
  958. wait4[2] = tapOvlWrite.hEvent; // only included if writeInProgress is true
  959. ReadFile(_tap,tapReadBuf,sizeof(tapReadBuf),NULL,&tapOvlRead);
  960. bool writeInProgress = false;
  961. ULONGLONG timeOfLastBorkCheck = GetTickCount64();
  962. _initialized = true;
  963. unsigned int oldmtu = _mtu;
  964. setFriendlyName(_friendlyName.c_str());
  965. while (_run) {
  966. DWORD waitResult = WaitForMultipleObjectsEx(writeInProgress ? 3 : 2,wait4,FALSE,2500,TRUE);
  967. if (!_run) break; // will also break outer while(_run) since _run is false
  968. // Check for changes in MTU and break to restart tap device to reconfigure in this case
  969. if (_mtu != oldmtu)
  970. break;
  971. // Check for issues with adapter and close/reopen if any are detected. This
  972. // check fixes a while boatload of Windows adapter 'coma' issues after
  973. // sleep/wake and when adapters are added/removed. Basically if the tap
  974. // device is borked, whack it.
  975. {
  976. ULONGLONG tc = GetTickCount64();
  977. if ((tc - timeOfLastBorkCheck) >= 2500) {
  978. timeOfLastBorkCheck = tc;
  979. char aabuf[16384];
  980. ULONG aalen = sizeof(aabuf);
  981. if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_UNICAST|GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER|GAA_FLAG_SKIP_FRIENDLY_NAME,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
  982. bool isBorked = false;
  983. PIP_ADAPTER_ADDRESSES aa = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
  984. while (aa) {
  985. if (_deviceLuid.Value == aa->Luid.Value) {
  986. isBorked = (aa->OperStatus != IfOperStatusUp);
  987. break;
  988. }
  989. aa = aa->Next;
  990. }
  991. if (isBorked) {
  992. // Close and reopen tap device if there's an issue (outer loop)
  993. break;
  994. }
  995. }
  996. }
  997. }
  998. if ((waitResult == WAIT_TIMEOUT)||(waitResult == WAIT_FAILED)) {
  999. Sleep(250); // guard against spinning under some conditions
  1000. continue;
  1001. }
  1002. if (HasOverlappedIoCompleted(&tapOvlRead)) {
  1003. DWORD bytesRead = 0;
  1004. if (GetOverlappedResult(_tap,&tapOvlRead,&bytesRead,FALSE)) {
  1005. if ((bytesRead > 14)&&(_enabled)) {
  1006. MAC to(tapReadBuf,6);
  1007. MAC from(tapReadBuf + 6,6);
  1008. unsigned int etherType = ((((unsigned int)tapReadBuf[12]) & 0xff) << 8) | (((unsigned int)tapReadBuf[13]) & 0xff);
  1009. try {
  1010. _handler(_arg,(void *)0,_nwid,from,to,etherType,0,tapReadBuf + 14,bytesRead - 14);
  1011. } catch ( ... ) {} // handlers should not throw
  1012. }
  1013. }
  1014. ReadFile(_tap,tapReadBuf,ZT_MAX_MTU + 32,NULL,&tapOvlRead);
  1015. }
  1016. if (writeInProgress) {
  1017. if (HasOverlappedIoCompleted(&tapOvlWrite)) {
  1018. writeInProgress = false;
  1019. _injectPending_m.lock();
  1020. _injectPending.pop();
  1021. } else continue; // still writing, so skip code below and wait
  1022. } else _injectPending_m.lock();
  1023. if (!_injectPending.empty()) {
  1024. WriteFile(_tap,_injectPending.front().data,_injectPending.front().len,NULL,&tapOvlWrite);
  1025. writeInProgress = true;
  1026. }
  1027. _injectPending_m.unlock();
  1028. }
  1029. CancelIo(_tap);
  1030. CloseHandle(tapOvlRead.hEvent);
  1031. CloseHandle(tapOvlWrite.hEvent);
  1032. CloseHandle(_tap);
  1033. _tap = INVALID_HANDLE_VALUE;
  1034. // We will restart and re-open the tap unless _run == false
  1035. }
  1036. } catch ( ... ) {} // catch unexpected exceptions -- this should not happen but would prevent program crash or other weird issues since threads should not throw
  1037. CoUninitialize();
  1038. }
  1039. NET_IFINDEX WindowsEthernetTap::_getDeviceIndex()
  1040. {
  1041. MIB_IF_TABLE2 *ift = (MIB_IF_TABLE2 *)0;
  1042. if (GetIfTable2Ex(MibIfTableRaw,&ift) != NO_ERROR)
  1043. throw std::runtime_error("GetIfTable2Ex() failed");
  1044. if (ift->NumEntries > 0) {
  1045. for(ULONG i=0;i<ift->NumEntries;++i) {
  1046. if (ift->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  1047. NET_IFINDEX idx = ift->Table[i].InterfaceIndex;
  1048. FreeMibTable(ift);
  1049. return idx;
  1050. }
  1051. }
  1052. }
  1053. FreeMibTable(&ift);
  1054. throw std::runtime_error("interface not found");
  1055. }
  1056. std::vector<std::string> WindowsEthernetTap::_getRegistryIPv4Value(const char *regKey)
  1057. {
  1058. std::vector<std::string> value;
  1059. HKEY tcpIpInterfaces;
  1060. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  1061. char buf[16384];
  1062. DWORD len = sizeof(buf);
  1063. DWORD kt = REG_MULTI_SZ;
  1064. if (RegGetValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey,0,&kt,&buf,&len) == ERROR_SUCCESS) {
  1065. switch(kt) {
  1066. case REG_SZ:
  1067. if (len > 0)
  1068. value.push_back(std::string(buf));
  1069. break;
  1070. case REG_MULTI_SZ: {
  1071. for(DWORD k=0,s=0;k<len;++k) {
  1072. if (!buf[k]) {
  1073. if (s < k) {
  1074. value.push_back(std::string(buf + s));
  1075. s = k + 1;
  1076. } else break;
  1077. }
  1078. }
  1079. } break;
  1080. }
  1081. }
  1082. RegCloseKey(tcpIpInterfaces);
  1083. }
  1084. return value;
  1085. }
  1086. void WindowsEthernetTap::_setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value)
  1087. {
  1088. std::string regMulti;
  1089. for(std::vector<std::string>::const_iterator s(value.begin());s!=value.end();++s) {
  1090. regMulti.append(*s);
  1091. regMulti.push_back((char)0);
  1092. }
  1093. HKEY tcpIpInterfaces;
  1094. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  1095. if (regMulti.length() > 0) {
  1096. regMulti.push_back((char)0);
  1097. RegSetKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey,REG_MULTI_SZ,regMulti.data(),(DWORD)regMulti.length());
  1098. } else {
  1099. RegDeleteKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey);
  1100. }
  1101. RegCloseKey(tcpIpInterfaces);
  1102. }
  1103. }
  1104. void WindowsEthernetTap::_syncIps()
  1105. {
  1106. // assumes _assignedIps_m is locked
  1107. if (!_initialized)
  1108. return;
  1109. std::vector<InetAddress> haveIps(ips());
  1110. for(std::vector<InetAddress>::const_iterator aip(_assignedIps.begin());aip!=_assignedIps.end();++aip) {
  1111. if (std::find(haveIps.begin(),haveIps.end(),*aip) == haveIps.end()) {
  1112. MIB_UNICASTIPADDRESS_ROW ipr;
  1113. InitializeUnicastIpAddressEntry(&ipr);
  1114. if (aip->isV4()) {
  1115. ipr.Address.Ipv4.sin_family = AF_INET;
  1116. ipr.Address.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)aip->rawIpData());
  1117. ipr.OnLinkPrefixLength = aip->netmaskBits();
  1118. if (ipr.OnLinkPrefixLength >= 32)
  1119. continue;
  1120. } else if (aip->isV6()) {
  1121. ipr.Address.Ipv6.sin6_family = AF_INET6;
  1122. memcpy(ipr.Address.Ipv6.sin6_addr.u.Byte,aip->rawIpData(),16);
  1123. ipr.OnLinkPrefixLength = aip->netmaskBits();
  1124. if (ipr.OnLinkPrefixLength >= 128)
  1125. continue;
  1126. } else continue;
  1127. ipr.PrefixOrigin = IpPrefixOriginManual;
  1128. ipr.SuffixOrigin = IpSuffixOriginManual;
  1129. ipr.ValidLifetime = 0xffffffff;
  1130. ipr.PreferredLifetime = 0xffffffff;
  1131. ipr.InterfaceLuid = _deviceLuid;
  1132. ipr.InterfaceIndex = _getDeviceIndex();
  1133. CreateUnicastIpAddressEntry(&ipr);
  1134. }
  1135. if (aip->isV4()) {
  1136. char ipbuf[64];
  1137. std::string ipStr(aip->toIpString(ipbuf));
  1138. std::vector<std::string> regIps(_getRegistryIPv4Value("IPAddress"));
  1139. if (std::find(regIps.begin(), regIps.end(), ipStr) == regIps.end()) {
  1140. std::vector<std::string> regSubnetMasks(_getRegistryIPv4Value("SubnetMask"));
  1141. regIps.push_back(ipStr);
  1142. regSubnetMasks.push_back(aip->netmask().toIpString(ipbuf));
  1143. _setRegistryIPv4Value("IPAddress", regIps);
  1144. _setRegistryIPv4Value("SubnetMask", regSubnetMasks);
  1145. }
  1146. }
  1147. }
  1148. }
  1149. void WindowsEthernetTap::setDns(const char* domain, const std::vector<InetAddress>& servers)
  1150. {
  1151. WinDNSHelper::setDNS(_nwid, domain, servers);
  1152. }
  1153. } // namespace ZeroTier