wintun.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT
  2. *
  3. * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
  4. */
  5. #pragma once
  6. #include <windows.h>
  7. #include <ipexport.h>
  8. #include <ifdef.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /**
  13. * A handle representing Wintun adapter
  14. */
  15. typedef struct _WINTUN_ADAPTER *WINTUN_ADAPTER_HANDLE;
  16. /**
  17. * Maximum pool name length including zero terminator
  18. */
  19. #define WINTUN_MAX_POOL 256
  20. /**
  21. * Creates a new Wintun adapter.
  22. *
  23. * @param Pool Name of the adapter pool. Zero-terminated string of up to WINTUN_MAX_POOL-1 characters.
  24. *
  25. * @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
  26. * characters.
  27. *
  28. * @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically.
  29. * If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is
  30. * created for each new adapter. It is called "requested" GUID because the API it uses is
  31. * completely undocumented, and so there could be minor interesting complications with its usage.
  32. *
  33. * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot.
  34. *
  35. * @return If the function succeeds, the return value is the adapter handle. Must be released with WintunFreeAdapter. If
  36. * the function fails, the return value is NULL. To get extended error information, call GetLastError.
  37. */
  38. typedef _Must_inspect_result_
  39. _Return_type_success_(return != NULL)
  40. _Post_maybenull_
  41. WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_CREATE_ADAPTER_FUNC_IMPL)
  42. (_In_z_ LPCWSTR Pool, _In_z_ LPCWSTR Name, _In_opt_ const GUID *RequestedGUID, _Out_opt_ BOOL *RebootRequired);
  43. typedef WINTUN_CREATE_ADAPTER_FUNC_IMPL *WINTUN_CREATE_ADAPTER_FUNC;
  44. /**
  45. * Opens an existing Wintun adapter.
  46. *
  47. * @param Pool Name of the adapter pool. Zero-terminated string of up to WINTUN_MAX_POOL-1 characters.
  48. *
  49. * @param Name Adapter name. Zero-terminated string of up to MAX_ADAPTER_NAME-1 characters.
  50. *
  51. * @return If the function succeeds, the return value is adapter handle. Must be released with WintunFreeAdapter. If the
  52. * function fails, the return value is NULL. To get extended error information, call GetLastError. Possible errors
  53. * include the following: ERROR_FILE_NOT_FOUND if adapter with given name is not found; ERROR_ALREADY_EXISTS if adapter
  54. * is found but not a Wintun-class or not a member of the pool
  55. */
  56. typedef _Must_inspect_result_
  57. _Return_type_success_(return != NULL)
  58. _Post_maybenull_
  59. WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_OPEN_ADAPTER_FUNC_IMPL)(_In_z_ LPCWSTR Pool, _In_z_ LPCWSTR Name);
  60. typedef WINTUN_OPEN_ADAPTER_FUNC_IMPL *WINTUN_OPEN_ADAPTER_FUNC;
  61. /**
  62. * Deletes a Wintun adapter.
  63. *
  64. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter.
  65. *
  66. * @param ForceCloseSessions Force close adapter handles that may be in use by other processes. Only set this to TRUE
  67. * with extreme care, as this is resource intensive and may put processes into an undefined
  68. * or unpredictable state. Most users should set this to FALSE.
  69. *
  70. * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot.
  71. *
  72. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  73. * get extended error information, call GetLastError.
  74. */
  75. typedef _Return_type_success_(return != FALSE)
  76. BOOL(WINAPI WINTUN_DELETE_ADAPTER_FUNC_IMPL)
  77. (_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ BOOL ForceCloseSessions, _Out_opt_ BOOL *RebootRequired);
  78. typedef WINTUN_DELETE_ADAPTER_FUNC_IMPL *WINTUN_DELETE_ADAPTER_FUNC;
  79. /**
  80. * Called by WintunEnumAdapters for each adapter in the pool.
  81. *
  82. * @param Adapter Adapter handle, which will be freed when this function returns.
  83. *
  84. * @param Param An application-defined value passed to the WintunEnumAdapters.
  85. *
  86. * @return Non-zero to continue iterating adapters; zero to stop.
  87. */
  88. typedef BOOL(CALLBACK *WINTUN_ENUM_CALLBACK)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ LPARAM Param);
  89. /**
  90. * Enumerates all Wintun adapters.
  91. *
  92. * @param Pool Name of the adapter pool. Zero-terminated string of up to WINTUN_MAX_POOL-1 characters.
  93. *
  94. * @param Callback Callback function. To continue enumeration, the callback function must return TRUE; to stop
  95. * enumeration, it must return FALSE.
  96. *
  97. * @param Param An application-defined value to be passed to the callback function.
  98. *
  99. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  100. * get extended error information, call GetLastError.
  101. */
  102. typedef _Return_type_success_(return != FALSE)
  103. BOOL(WINAPI WINTUN_ENUM_ADAPTERS_FUNC_IMPL)(_In_z_ LPCWSTR Pool, _In_ WINTUN_ENUM_CALLBACK Callback, _In_ LPARAM Param);
  104. typedef WINTUN_ENUM_ADAPTERS_FUNC_IMPL *WINTUN_ENUM_ADAPTERS_FUNC;
  105. /**
  106. * Releases Wintun adapter resources.
  107. *
  108. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter.
  109. */
  110. typedef VOID(WINAPI WINTUN_FREE_ADAPTER_FUNC_IMPL)(_In_ WINTUN_ADAPTER_HANDLE Adapter);
  111. typedef WINTUN_FREE_ADAPTER_FUNC_IMPL *WINTUN_FREE_ADAPTER_FUNC;
  112. /**
  113. * Deletes all Wintun adapters in a pool and if there are no more adapters in any other pools, also removes Wintun
  114. * from the driver store, usually called by uninstallers.
  115. *
  116. * @param Pool Name of the adapter pool. Zero-terminated string of up to WINTUN_MAX_POOL-1 characters.
  117. *
  118. * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot.
  119. *
  120. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  121. * get extended error information, call GetLastError.
  122. */
  123. typedef _Return_type_success_(return != FALSE)
  124. BOOL(WINAPI WINTUN_DELETE_POOL_DRIVER_FUNC_IMPL)(_In_z_ LPCWSTR Pool, _Out_opt_ BOOL *RebootRequired);
  125. typedef WINTUN_DELETE_POOL_DRIVER_FUNC_IMPL *WINTUN_DELETE_POOL_DRIVER_FUNC;
  126. /**
  127. * Returns the LUID of the adapter.
  128. *
  129. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
  130. *
  131. * @param Luid Pointer to LUID to receive adapter LUID.
  132. */
  133. typedef VOID(WINAPI WINTUN_GET_ADAPTER_LUID_FUNC_IMPL)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _Out_ NET_LUID *Luid);
  134. typedef WINTUN_GET_ADAPTER_LUID_FUNC_IMPL *WINTUN_GET_ADAPTER_LUID_FUNC;
  135. /**
  136. * Returns the name of the Wintun adapter.
  137. *
  138. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
  139. *
  140. * @param Name Pointer to a string to receive adapter name
  141. *
  142. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  143. * get extended error information, call GetLastError.
  144. */
  145. typedef _Must_inspect_result_
  146. _Return_type_success_(return != FALSE)
  147. BOOL(WINAPI WINTUN_GET_ADAPTER_NAME_FUNC_IMPL)
  148. (_In_ WINTUN_ADAPTER_HANDLE Adapter, _Out_writes_z_(MAX_ADAPTER_NAME) LPWSTR Name);
  149. typedef WINTUN_GET_ADAPTER_NAME_FUNC_IMPL *WINTUN_GET_ADAPTER_NAME_FUNC;
  150. /**
  151. * Sets name of the Wintun adapter.
  152. *
  153. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
  154. *
  155. * @param Name Adapter name. Zero-terminated string of up to MAX_ADAPTER_NAME-1 characters.
  156. *
  157. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  158. * get extended error information, call GetLastError.
  159. */
  160. typedef _Return_type_success_(return != FALSE)
  161. BOOL(WINAPI WINTUN_SET_ADAPTER_NAME_FUNC_IMPL)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_z_ LPCWSTR Name);
  162. typedef WINTUN_SET_ADAPTER_NAME_FUNC_IMPL *WINTUN_SET_ADAPTER_NAME_FUNC;
  163. /**
  164. * Determines the version of the Wintun driver currently loaded.
  165. *
  166. * @return If the function succeeds, the return value is the version number. If the function fails, the return value is
  167. * zero. To get extended error information, call GetLastError. Possible errors include the following:
  168. * ERROR_FILE_NOT_FOUND Wintun not loaded
  169. */
  170. typedef _Return_type_success_(return != 0)
  171. DWORD(WINAPI WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC_IMPL)(VOID);
  172. typedef WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC_IMPL *WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC;
  173. /**
  174. * Determines the level of logging, passed to WINTUN_LOGGER_CALLBACK.
  175. */
  176. typedef enum
  177. {
  178. WINTUN_LOG_INFO, /**< Informational */
  179. WINTUN_LOG_WARN, /**< Warning */
  180. WINTUN_LOG_ERR /**< Error */
  181. } WINTUN_LOGGER_LEVEL;
  182. /**
  183. * Called by internal logger to report diagnostic messages
  184. *
  185. * @param Level Message level.
  186. *
  187. * @param Message Message text.
  188. */
  189. typedef VOID(CALLBACK *WINTUN_LOGGER_CALLBACK)(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ LPCWSTR Message);
  190. /**
  191. * Sets logger callback function.
  192. *
  193. * @param NewLogger Pointer to callback function to use as a new global logger. NewLogger may be called from various
  194. * threads concurrently. Should the logging require serialization, you must handle serialization in
  195. * NewLogger. Set to NULL to disable.
  196. */
  197. typedef VOID(WINAPI WINTUN_SET_LOGGER_FUNC_IMPL)(_In_ WINTUN_LOGGER_CALLBACK NewLogger);
  198. typedef WINTUN_SET_LOGGER_FUNC_IMPL *WINTUN_SET_LOGGER_FUNC;
  199. /**
  200. * Minimum ring capacity.
  201. */
  202. #define WINTUN_MIN_RING_CAPACITY 0x20000 /* 128kiB */
  203. /**
  204. * Maximum ring capacity.
  205. */
  206. #define WINTUN_MAX_RING_CAPACITY 0x4000000 /* 64MiB */
  207. /**
  208. * A handle representing Wintun session
  209. */
  210. typedef struct _TUN_SESSION *WINTUN_SESSION_HANDLE;
  211. /**
  212. * Starts Wintun session.
  213. *
  214. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
  215. *
  216. * @param Capacity Rings capacity. Must be between WINTUN_MIN_RING_CAPACITY and WINTUN_MAX_RING_CAPACITY (incl.)
  217. * Must be a power of two.
  218. *
  219. * @return Wintun session handle. Must be released with WintunEndSession. If the function fails, the return value is
  220. * NULL. To get extended error information, call GetLastError.
  221. */
  222. typedef _Must_inspect_result_
  223. _Return_type_success_(return != NULL)
  224. _Post_maybenull_
  225. WINTUN_SESSION_HANDLE(WINAPI WINTUN_START_SESSION_FUNC_IMPL)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ DWORD Capacity);
  226. typedef WINTUN_START_SESSION_FUNC_IMPL *WINTUN_START_SESSION_FUNC;
  227. /**
  228. * Ends Wintun session.
  229. *
  230. * @param Session Wintun session handle obtained with WintunStartSession
  231. */
  232. typedef VOID(WINAPI WINTUN_END_SESSION_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session);
  233. typedef WINTUN_END_SESSION_FUNC_IMPL *WINTUN_END_SESSION_FUNC;
  234. /**
  235. * Gets Wintun session's read-wait event handle.
  236. *
  237. * @param Session Wintun session handle obtained with WintunStartSession
  238. *
  239. * @return Pointer to receive event handle to wait for available data when reading. Should
  240. * WintunReceivePackets return ERROR_NO_MORE_ITEMS (after spinning on it for a while under heavy
  241. * load), wait for this event to become signaled before retrying WintunReceivePackets. Do not call
  242. * CloseHandle on this event - it is managed by the session.
  243. */
  244. typedef HANDLE(WINAPI WINTUN_GET_READ_WAIT_EVENT_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session);
  245. typedef WINTUN_GET_READ_WAIT_EVENT_FUNC_IMPL *WINTUN_GET_READ_WAIT_EVENT_FUNC;
  246. /**
  247. * Maximum IP packet size
  248. */
  249. #define WINTUN_MAX_IP_PACKET_SIZE 0xFFFF
  250. /**
  251. * Retrieves one or packet. After the packet content is consumed, call WintunReleaseReceivePacket with Packet returned
  252. * from this function to release internal buffer. This function is thread-safe.
  253. *
  254. * @param Session Wintun session handle obtained with WintunStartSession
  255. *
  256. * @param PacketSize Pointer to receive packet size.
  257. *
  258. * @return Pointer to layer 3 IPv4 or IPv6 packet. Client may modify its content at will. If the function fails, the
  259. * return value is NULL. To get extended error information, call GetLastError. Possible errors include the
  260. * following:
  261. * ERROR_HANDLE_EOF Wintun adapter is terminating;
  262. * ERROR_NO_MORE_ITEMS Wintun buffer is exhausted;
  263. * ERROR_INVALID_DATA Wintun buffer is corrupt
  264. */
  265. typedef _Must_inspect_result_
  266. _Return_type_success_(return != NULL)
  267. _Post_maybenull_
  268. _Post_writable_byte_size_(*PacketSize)
  269. BYTE *(WINAPI WINTUN_RECEIVE_PACKET_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session, _Out_ DWORD *PacketSize);
  270. typedef WINTUN_RECEIVE_PACKET_FUNC_IMPL *WINTUN_RECEIVE_PACKET_FUNC;
  271. /**
  272. * Releases internal buffer after the received packet has been processed by the client. This function is thread-safe.
  273. *
  274. * @param Session Wintun session handle obtained with WintunStartSession
  275. *
  276. * @param Packet Packet obtained with WintunReceivePacket
  277. */
  278. typedef VOID(
  279. WINAPI WINTUN_RELEASE_RECEIVE_PACKET_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
  280. typedef WINTUN_RELEASE_RECEIVE_PACKET_FUNC_IMPL *WINTUN_RELEASE_RECEIVE_PACKET_FUNC;
  281. /**
  282. * Allocates memory for a packet to send. After the memory is filled with packet data, call WintunSendPacket to send
  283. * and release internal buffer. WintunAllocateSendPacket is thread-safe and the WintunAllocateSendPacket order of
  284. * calls define the packet sending order.
  285. *
  286. * @param Session Wintun session handle obtained with WintunStartSession
  287. *
  288. * @param PacketSize Exact packet size. Must be less or equal to WINTUN_MAX_IP_PACKET_SIZE.
  289. *
  290. * @return Returns pointer to memory where to prepare layer 3 IPv4 or IPv6 packet for sending. If the function fails,
  291. * the return value is NULL. To get extended error information, call GetLastError. Possible errors include the
  292. * following:
  293. * ERROR_HANDLE_EOF Wintun adapter is terminating;
  294. * ERROR_BUFFER_OVERFLOW Wintun buffer is full;
  295. */
  296. typedef _Must_inspect_result_
  297. _Return_type_success_(return != NULL)
  298. _Post_maybenull_
  299. _Post_writable_byte_size_(PacketSize)
  300. BYTE *(WINAPI WINTUN_ALLOCATE_SEND_PACKET_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session, _In_ DWORD PacketSize);
  301. typedef WINTUN_ALLOCATE_SEND_PACKET_FUNC_IMPL *WINTUN_ALLOCATE_SEND_PACKET_FUNC;
  302. /**
  303. * Sends the packet and releases internal buffer. WintunSendPacket is thread-safe, but the WintunAllocateSendPacket
  304. * order of calls define the packet sending order. This means the packet is not guaranteed to be sent in the
  305. * WintunSendPacket yet.
  306. *
  307. * @param Session Wintun session handle obtained with WintunStartSession
  308. *
  309. * @param Packet Packet obtained with WintunAllocateSendPacket
  310. */
  311. typedef VOID(WINAPI WINTUN_SEND_PACKET_FUNC_IMPL)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
  312. typedef WINTUN_SEND_PACKET_FUNC_IMPL *WINTUN_SEND_PACKET_FUNC;
  313. #ifdef __cplusplus
  314. }
  315. #endif