wintun.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <winsock2.h>
  7. #include <windows.h>
  8. #include <ipexport.h>
  9. #include <ifdef.h>
  10. #include <ws2ipdef.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #ifndef ALIGNED
  15. # if defined(_MSC_VER)
  16. # define ALIGNED(n) __declspec(align(n))
  17. # elif defined(__GNUC__)
  18. # define ALIGNED(n) __attribute__((aligned(n)))
  19. # else
  20. # error "Unable to define ALIGNED"
  21. # endif
  22. #endif
  23. /* MinGW is missing this one, unfortunately. */
  24. #ifndef _Post_maybenull_
  25. # define _Post_maybenull_
  26. #endif
  27. #pragma warning(push)
  28. #pragma warning(disable : 4324) /* structure was padded due to alignment specifier */
  29. /**
  30. * A handle representing Wintun adapter
  31. */
  32. typedef struct _WINTUN_ADAPTER *WINTUN_ADAPTER_HANDLE;
  33. /**
  34. * Creates a new Wintun adapter.
  35. *
  36. * @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
  37. * characters.
  38. *
  39. * @param TunnelType Name of the adapter tunnel type. Zero-terminated string of up to MAX_ADAPTER_NAME-1
  40. * characters.
  41. *
  42. * @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically.
  43. * If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is
  44. * created for each new adapter. It is called "requested" GUID because the API it uses is
  45. * completely undocumented, and so there could be minor interesting complications with its usage.
  46. *
  47. * @return If the function succeeds, the return value is the adapter handle. Must be released with
  48. * WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
  49. * GetLastError.
  50. */
  51. typedef _Must_inspect_result_
  52. _Return_type_success_(return != NULL)
  53. _Post_maybenull_
  54. WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_CREATE_ADAPTER_FUNC)
  55. (_In_z_ LPCWSTR Name, _In_z_ LPCWSTR TunnelType, _In_opt_ const GUID *RequestedGUID);
  56. /**
  57. * Opens an existing Wintun adapter.
  58. *
  59. * @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
  60. * characters.
  61. *
  62. * @return If the function succeeds, the return value is the adapter handle. Must be released with
  63. * WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
  64. * GetLastError.
  65. */
  66. typedef _Must_inspect_result_
  67. _Return_type_success_(return != NULL)
  68. _Post_maybenull_
  69. WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_OPEN_ADAPTER_FUNC)(_In_z_ LPCWSTR Name);
  70. /**
  71. * Releases Wintun adapter resources and, if adapter was created with WintunCreateAdapter, removes adapter.
  72. *
  73. * @param Adapter Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter.
  74. */
  75. typedef VOID(WINAPI WINTUN_CLOSE_ADAPTER_FUNC)(_In_opt_ WINTUN_ADAPTER_HANDLE Adapter);
  76. /**
  77. * Deletes the Wintun driver if there are no more adapters in use.
  78. *
  79. * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
  80. * get extended error information, call GetLastError.
  81. */
  82. typedef _Return_type_success_(return != FALSE)
  83. BOOL(WINAPI WINTUN_DELETE_DRIVER_FUNC)(VOID);
  84. /**
  85. * Returns the LUID of the adapter.
  86. *
  87. * @param Adapter Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter
  88. *
  89. * @param Luid Pointer to LUID to receive adapter LUID.
  90. */
  91. typedef VOID(WINAPI WINTUN_GET_ADAPTER_LUID_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _Out_ NET_LUID *Luid);
  92. /**
  93. * Determines the version of the Wintun driver currently loaded.
  94. *
  95. * @return If the function succeeds, the return value is the version number. If the function fails, the return value is
  96. * zero. To get extended error information, call GetLastError. Possible errors include the following:
  97. * ERROR_FILE_NOT_FOUND Wintun not loaded
  98. */
  99. typedef _Return_type_success_(return != 0)
  100. DWORD(WINAPI WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC)(VOID);
  101. /**
  102. * Determines the level of logging, passed to WINTUN_LOGGER_CALLBACK.
  103. */
  104. typedef enum
  105. {
  106. WINTUN_LOG_INFO, /**< Informational */
  107. WINTUN_LOG_WARN, /**< Warning */
  108. WINTUN_LOG_ERR /**< Error */
  109. } WINTUN_LOGGER_LEVEL;
  110. /**
  111. * Called by internal logger to report diagnostic messages
  112. *
  113. * @param Level Message level.
  114. *
  115. * @param Timestamp Message timestamp in in 100ns intervals since 1601-01-01 UTC.
  116. *
  117. * @param Message Message text.
  118. */
  119. typedef VOID(CALLBACK *WINTUN_LOGGER_CALLBACK)(
  120. _In_ WINTUN_LOGGER_LEVEL Level,
  121. _In_ DWORD64 Timestamp,
  122. _In_z_ LPCWSTR Message);
  123. /**
  124. * Sets logger callback function.
  125. *
  126. * @param NewLogger Pointer to callback function to use as a new global logger. NewLogger may be called from various
  127. * threads concurrently. Should the logging require serialization, you must handle serialization in
  128. * NewLogger. Set to NULL to disable.
  129. */
  130. typedef VOID(WINAPI WINTUN_SET_LOGGER_FUNC)(_In_ WINTUN_LOGGER_CALLBACK NewLogger);
  131. /**
  132. * Minimum ring capacity.
  133. */
  134. #define WINTUN_MIN_RING_CAPACITY 0x20000 /* 128kiB */
  135. /**
  136. * Maximum ring capacity.
  137. */
  138. #define WINTUN_MAX_RING_CAPACITY 0x4000000 /* 64MiB */
  139. /**
  140. * A handle representing Wintun session
  141. */
  142. typedef struct _TUN_SESSION *WINTUN_SESSION_HANDLE;
  143. /**
  144. * Starts Wintun session.
  145. *
  146. * @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
  147. *
  148. * @param Capacity Rings capacity. Must be between WINTUN_MIN_RING_CAPACITY and WINTUN_MAX_RING_CAPACITY (incl.)
  149. * Must be a power of two.
  150. *
  151. * @return Wintun session handle. Must be released with WintunEndSession. If the function fails, the return value is
  152. * NULL. To get extended error information, call GetLastError.
  153. */
  154. typedef _Must_inspect_result_
  155. _Return_type_success_(return != NULL)
  156. _Post_maybenull_
  157. WINTUN_SESSION_HANDLE(WINAPI WINTUN_START_SESSION_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ DWORD Capacity);
  158. /**
  159. * Ends Wintun session.
  160. *
  161. * @param Session Wintun session handle obtained with WintunStartSession
  162. */
  163. typedef VOID(WINAPI WINTUN_END_SESSION_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
  164. /**
  165. * Gets Wintun session's read-wait event handle.
  166. *
  167. * @param Session Wintun session handle obtained with WintunStartSession
  168. *
  169. * @return Pointer to receive event handle to wait for available data when reading. Should
  170. * WintunReceivePackets return ERROR_NO_MORE_ITEMS (after spinning on it for a while under heavy
  171. * load), wait for this event to become signaled before retrying WintunReceivePackets. Do not call
  172. * CloseHandle on this event - it is managed by the session.
  173. */
  174. typedef HANDLE(WINAPI WINTUN_GET_READ_WAIT_EVENT_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
  175. /**
  176. * Maximum IP packet size
  177. */
  178. #define WINTUN_MAX_IP_PACKET_SIZE 0xFFFF
  179. /**
  180. * Retrieves one or packet. After the packet content is consumed, call WintunReleaseReceivePacket with Packet returned
  181. * from this function to release internal buffer. This function is thread-safe.
  182. *
  183. * @param Session Wintun session handle obtained with WintunStartSession
  184. *
  185. * @param PacketSize Pointer to receive packet size.
  186. *
  187. * @return Pointer to layer 3 IPv4 or IPv6 packet. Client may modify its content at will. If the function fails, the
  188. * return value is NULL. To get extended error information, call GetLastError. Possible errors include the
  189. * following:
  190. * ERROR_HANDLE_EOF Wintun adapter is terminating;
  191. * ERROR_NO_MORE_ITEMS Wintun buffer is exhausted;
  192. * ERROR_INVALID_DATA Wintun buffer is corrupt
  193. */
  194. typedef _Must_inspect_result_
  195. _Return_type_success_(return != NULL)
  196. _Post_maybenull_
  197. _Post_writable_byte_size_(*PacketSize)
  198. BYTE *(WINAPI WINTUN_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _Out_ DWORD *PacketSize);
  199. /**
  200. * Releases internal buffer after the received packet has been processed by the client. This function is thread-safe.
  201. *
  202. * @param Session Wintun session handle obtained with WintunStartSession
  203. *
  204. * @param Packet Packet obtained with WintunReceivePacket
  205. */
  206. typedef VOID(
  207. WINAPI WINTUN_RELEASE_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
  208. /**
  209. * Allocates memory for a packet to send. After the memory is filled with packet data, call WintunSendPacket to send
  210. * and release internal buffer. WintunAllocateSendPacket is thread-safe and the WintunAllocateSendPacket order of
  211. * calls define the packet sending order.
  212. *
  213. * @param Session Wintun session handle obtained with WintunStartSession
  214. *
  215. * @param PacketSize Exact packet size. Must be less or equal to WINTUN_MAX_IP_PACKET_SIZE.
  216. *
  217. * @return Returns pointer to memory where to prepare layer 3 IPv4 or IPv6 packet for sending. If the function fails,
  218. * the return value is NULL. To get extended error information, call GetLastError. Possible errors include the
  219. * following:
  220. * ERROR_HANDLE_EOF Wintun adapter is terminating;
  221. * ERROR_BUFFER_OVERFLOW Wintun buffer is full;
  222. */
  223. typedef _Must_inspect_result_
  224. _Return_type_success_(return != NULL)
  225. _Post_maybenull_
  226. _Post_writable_byte_size_(PacketSize)
  227. BYTE *(WINAPI WINTUN_ALLOCATE_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ DWORD PacketSize);
  228. /**
  229. * Sends the packet and releases internal buffer. WintunSendPacket is thread-safe, but the WintunAllocateSendPacket
  230. * order of calls define the packet sending order. This means the packet is not guaranteed to be sent in the
  231. * WintunSendPacket yet.
  232. *
  233. * @param Session Wintun session handle obtained with WintunStartSession
  234. *
  235. * @param Packet Packet obtained with WintunAllocateSendPacket
  236. */
  237. typedef VOID(WINAPI WINTUN_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
  238. #pragma warning(pop)
  239. #ifdef __cplusplus
  240. }
  241. #endif