wintun.h 13 KB

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