1
0

TAPDevice.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include <stdbool.h>
  16. #include "util/Bits.h"
  17. #include "exception/Except.h"
  18. #include "exception/WinFail.h"
  19. #include "memory/Allocator.h"
  20. #include "interface/tuntap/windows/TAPDevice.h"
  21. #include "util/CString.h"
  22. /*
  23. * Portions of this code are copied from QEMU project which is licensed
  24. * under GPLv2 or greater, further contributions are licensed under GPLv3
  25. * or greater.
  26. */
  27. /*
  28. * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
  29. * on win32. Originally derived from the CIPE-Win32
  30. * project by Damion K. Wilson, with extensive modifications by
  31. * James Yonan.
  32. *
  33. * All source code which derives from the CIPE-Win32 project is
  34. * Copyright (C) Damion K. Wilson, 2003, and is released under the
  35. * GPL version 2 (see below).
  36. *
  37. * All other source code is Copyright (C) James Yonan, 2003-2004,
  38. * and is released under the GPL version 2 (see below).
  39. *
  40. * This program is free software; you can redistribute it and/or modify
  41. * it under the terms of the GNU General Public License as published by
  42. * the Free Software Foundation; either version 2 of the License, or
  43. * (at your option) any later version.
  44. *
  45. * This program is distributed in the hope that it will be useful,
  46. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  48. * GNU General Public License for more details.
  49. *
  50. * You should have received a copy of the GNU General Public License
  51. * along with this program (see the file COPYING included with this
  52. * distribution); if not, write to the Free Software Foundation, Inc.,
  53. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  54. */
  55. #include <stdio.h>
  56. #include <windows.h>
  57. #define NETWORK_ADAPTER_GUID "{4D36E972-E325-11CE-BFC1-08002BE10318}"
  58. #define ADAPTER_KEY \
  59. "SYSTEM\\CurrentControlSet\\Control\\Class\\" NETWORK_ADAPTER_GUID
  60. #define NETWORK_CONNECTIONS_KEY \
  61. "SYSTEM\\CurrentControlSet\\Control\\Network\\" NETWORK_ADAPTER_GUID
  62. #define USERMODEDEVICEDIR "\\\\.\\Global\\"
  63. #define TAPSUFFIX ".tap"
  64. #define BUFF_SZ 0x100
  65. struct Taps
  66. {
  67. char guid[BUFF_SZ];
  68. char name[BUFF_SZ];
  69. struct Taps* next;
  70. };
  71. static int is_tap_win32_dev(const char *guid)
  72. {
  73. HKEY netcard_key;
  74. LONG status;
  75. DWORD len;
  76. int i = 0;
  77. status = RegOpenKeyEx(
  78. HKEY_LOCAL_MACHINE,
  79. ADAPTER_KEY,
  80. 0,
  81. KEY_READ,
  82. &netcard_key);
  83. if (status != ERROR_SUCCESS) {
  84. return FALSE;
  85. }
  86. for (;;) {
  87. char enum_name[256];
  88. char unit_string[256];
  89. HKEY unit_key;
  90. char component_id_string[] = "ComponentId";
  91. char component_id[256];
  92. char net_cfg_instance_id_string[] = "NetCfgInstanceId";
  93. char net_cfg_instance_id[256];
  94. DWORD data_type;
  95. len = sizeof (enum_name);
  96. status = RegEnumKeyEx(
  97. netcard_key,
  98. i,
  99. enum_name,
  100. &len,
  101. NULL,
  102. NULL,
  103. NULL,
  104. NULL);
  105. if (status == ERROR_NO_MORE_ITEMS) {
  106. break;
  107. } else if (status != ERROR_SUCCESS) {
  108. return FALSE;
  109. }
  110. snprintf (unit_string, sizeof(unit_string), "%s\\%s",
  111. ADAPTER_KEY, enum_name);
  112. status = RegOpenKeyEx(
  113. HKEY_LOCAL_MACHINE,
  114. unit_string,
  115. 0,
  116. KEY_READ,
  117. &unit_key);
  118. if (status != ERROR_SUCCESS) {
  119. return FALSE;
  120. } else {
  121. len = sizeof (component_id);
  122. status = RegQueryValueEx(
  123. unit_key,
  124. component_id_string,
  125. NULL,
  126. &data_type,
  127. (uint8_t*)component_id,
  128. &len);
  129. if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
  130. len = sizeof (net_cfg_instance_id);
  131. status = RegQueryValueEx(
  132. unit_key,
  133. net_cfg_instance_id_string,
  134. NULL,
  135. &data_type,
  136. (uint8_t*)net_cfg_instance_id,
  137. &len);
  138. if (status == ERROR_SUCCESS && data_type == REG_SZ) {
  139. if (!Bits_memcmp(component_id, "tap", CString_strlen("tap")) &&
  140. !strcmp (net_cfg_instance_id, guid)) {
  141. RegCloseKey (unit_key);
  142. RegCloseKey (netcard_key);
  143. return TRUE;
  144. }
  145. }
  146. }
  147. RegCloseKey (unit_key);
  148. }
  149. ++i;
  150. }
  151. RegCloseKey (netcard_key);
  152. return FALSE;
  153. }
  154. static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
  155. {
  156. LONG status;
  157. HKEY control_net_key;
  158. DWORD len;
  159. struct Taps* taps = NULL;
  160. struct Taps* tail = NULL;
  161. WinFail_check(eh, (
  162. RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &control_net_key)
  163. ));
  164. int i = 0;
  165. char enum_name[BUFF_SZ];
  166. char connection_string[BUFF_SZ];
  167. char name_data[BUFF_SZ];
  168. const char name_string[] = "Name";
  169. while (true) {
  170. HKEY connKey;
  171. DWORD name_type;
  172. Bits_memset(enum_name, 0, sizeof(enum_name));
  173. Bits_memset(connection_string, 0, sizeof(connection_string));
  174. Bits_memset(name_data, 0, sizeof(name_data));
  175. len = sizeof (enum_name);
  176. status = RegEnumKeyEx(control_net_key, i, enum_name, &len, NULL, NULL, NULL, NULL);
  177. if (status == ERROR_NO_MORE_ITEMS) {
  178. break;
  179. } else if (status != ERROR_SUCCESS) {
  180. WinFail_fail(eh, "RegEnumKeyEx() failed", status);
  181. }
  182. if (len != CString_strlen(NETWORK_ADAPTER_GUID)) {
  183. // extranious directory, eg: "Descriptions"
  184. ++i;
  185. continue;
  186. }
  187. snprintf(connection_string,
  188. sizeof(connection_string),
  189. "%s\\%s\\Connection",
  190. NETWORK_CONNECTIONS_KEY, enum_name);
  191. WinFail_check(eh, (
  192. RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, KEY_READ, &connKey)
  193. ));
  194. // In Windows 10, some interface keys don't have names. We should keep
  195. // going and treat those interfaces as having empty string names.
  196. len = sizeof (name_data);
  197. status = RegQueryValueEx(connKey, name_string, NULL, &name_type,
  198. (uint8_t*)name_data, &len);
  199. if (status == ERROR_FILE_NOT_FOUND) {
  200. // The interface has no name.
  201. strncpy(name_data, "", sizeof (name_data));
  202. } else if (status != ERROR_SUCCESS) {
  203. WinFail_fail(eh, "RegQueryValueEx() for interface name failed", status);
  204. } else {
  205. if (name_type != REG_SZ) {
  206. // Someone named an interface with a non-string
  207. WinFail_fail(eh, "RegQueryValueEx() name_type != REG_SZ", status);
  208. }
  209. }
  210. if (is_tap_win32_dev(enum_name)) {
  211. struct Taps* tap = Allocator_calloc(alloc, sizeof(struct Taps), 1);
  212. Bits_memcpy(tap->guid, enum_name, sizeof(enum_name));
  213. Bits_memcpy(tap->name, name_data, sizeof(name_data));
  214. if (!taps) {
  215. taps = tap;
  216. taps->next = NULL;
  217. }
  218. if (tail) {
  219. tail->next = tap;
  220. }
  221. tail = tap;
  222. }
  223. RegCloseKey(connKey);
  224. ++i;
  225. }
  226. RegCloseKey (control_net_key);
  227. return taps;
  228. }
  229. static int get_device_guid(char *name,
  230. int name_size,
  231. char *actual_name,
  232. int actual_name_size,
  233. struct Allocator* alloc,
  234. struct Except* eh)
  235. {
  236. char buff[BUFF_SZ] = {0};
  237. HANDLE handle;
  238. struct Taps* taps = get_all_taps(alloc, eh);
  239. while (taps) {
  240. if (actual_name && CString_strcmp(actual_name, "") != 0) {
  241. if (CString_strcmp(taps->name, actual_name) != 0) {
  242. taps = taps->next;
  243. continue;
  244. }
  245. }
  246. snprintf(buff, sizeof(buff), USERMODEDEVICEDIR "%s" TAPSUFFIX, taps->guid);
  247. handle = CreateFile(buff,
  248. GENERIC_READ | GENERIC_WRITE,
  249. 0,
  250. 0,
  251. OPEN_EXISTING,
  252. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  253. 0);
  254. if (handle != INVALID_HANDLE_VALUE) {
  255. snprintf(name, name_size, "%s", taps->guid);
  256. if (actual_name && CString_strcmp(actual_name, "") == 0) {
  257. snprintf(actual_name, actual_name_size, "%s", taps->name);
  258. }
  259. CloseHandle(handle);
  260. return 0;
  261. }
  262. taps = taps->next;
  263. }
  264. return -1;
  265. }
  266. struct TAPDevice* TAPDevice_find(const char* preferredName,
  267. struct Except* eh,
  268. struct Allocator* alloc)
  269. {
  270. char guid[BUFF_SZ] = {0};
  271. char buff[BUFF_SZ] = {0};
  272. if (preferredName != NULL) {
  273. snprintf(buff, sizeof(buff), "%s", preferredName);
  274. }
  275. if (get_device_guid(guid, sizeof(guid), buff, sizeof(buff), alloc, eh)) {
  276. return NULL;
  277. }
  278. struct TAPDevice* out = Allocator_malloc(alloc, sizeof(struct TAPDevice));
  279. out->name = Allocator_malloc(alloc, CString_strlen(buff)+1);
  280. Bits_memcpy(out->name, buff, CString_strlen(buff)+1);
  281. snprintf(buff, sizeof(buff), USERMODEDEVICEDIR "%s" TAPSUFFIX, guid);
  282. out->path = Allocator_malloc(alloc, CString_strlen(buff)+1);
  283. Bits_memcpy(out->path, buff, CString_strlen(buff)+1);
  284. return out;
  285. }