2020-11-27 14:50:34 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 OR MIT
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-10-17 15:11:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
#include <winsock2.h>
|
2021-03-16 10:11:00 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <ipexport.h>
|
2020-11-02 18:34:49 +01:00
|
|
|
#include <ifdef.h>
|
2021-10-12 07:21:31 +02:00
|
|
|
#include <ws2ipdef.h>
|
2020-10-17 15:11:34 +02:00
|
|
|
|
2020-10-24 23:25:26 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
#ifndef ALIGNED
|
|
|
|
# if defined(_MSC_VER)
|
|
|
|
# define ALIGNED(n) __declspec(align(n))
|
|
|
|
# elif defined(__GNUC__)
|
|
|
|
# define ALIGNED(n) __attribute__((aligned(n)))
|
|
|
|
# else
|
|
|
|
# error "Unable to define ALIGNED"
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* MinGW is missing this one, unfortunately. */
|
|
|
|
#ifndef _Post_maybenull_
|
|
|
|
# define _Post_maybenull_
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4324) /* structure was padded due to alignment specifier */
|
|
|
|
|
2020-10-17 15:11:34 +02:00
|
|
|
/**
|
|
|
|
* A handle representing Wintun adapter
|
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef struct _WINTUN_ADAPTER *WINTUN_ADAPTER_HANDLE;
|
2020-10-17 15:11:34 +02:00
|
|
|
|
|
|
|
/**
|
2020-11-05 13:47:18 +01:00
|
|
|
* Creates a new Wintun adapter.
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2020-10-31 18:13:36 +01:00
|
|
|
* @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
|
|
|
* characters.
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @param TunelType Name of the adapter tunnel type. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
|
|
|
* characters.
|
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically.
|
|
|
|
* If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is
|
|
|
|
* created for each new adapter. It is called "requested" GUID because the API it uses is
|
|
|
|
* completely undocumented, and so there could be minor interesting complications with its usage.
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @return If the function succeeds, the return value is the adapter handle. Must be released with
|
|
|
|
* WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
|
|
|
|
* GetLastError.
|
2020-10-17 15:11:34 +02:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Must_inspect_result_
|
|
|
|
_Return_type_success_(return != NULL)
|
|
|
|
_Post_maybenull_
|
2021-10-12 07:21:31 +02:00
|
|
|
WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_CREATE_ADAPTER_FUNC)
|
|
|
|
(_In_z_ LPCWSTR Name, _In_z_ LPCWSTR TunnelType, _In_opt_ const GUID *RequestedGUID);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
2020-11-05 13:47:18 +01:00
|
|
|
/**
|
|
|
|
* Opens an existing Wintun adapter.
|
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
|
|
|
* characters.
|
2020-11-05 13:47:18 +01:00
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @return If the function succeeds, the return value is the adapter handle. Must be released with
|
|
|
|
* WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
|
|
|
|
* GetLastError.
|
2020-11-05 13:47:18 +01:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Must_inspect_result_
|
|
|
|
_Return_type_success_(return != NULL)
|
|
|
|
_Post_maybenull_
|
2021-10-12 07:21:31 +02:00
|
|
|
WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_OPEN_ADAPTER_FUNC)(_In_z_ LPCWSTR Name);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-12 07:21:31 +02:00
|
|
|
* Releases Wintun adapter resources and, if adapter was created with WintunCreateAdapter, removes adapter.
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @param Adapter Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter.
|
2020-10-17 15:11:34 +02:00
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(WINAPI WINTUN_CLOSE_ADAPTER_FUNC)(_In_opt_ WINTUN_ADAPTER_HANDLE Adapter);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-12 07:21:31 +02:00
|
|
|
* Deletes the Wintun driver if there are no more adapters in use.
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
2020-11-05 13:47:18 +01:00
|
|
|
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
|
|
|
* get extended error information, call GetLastError.
|
2020-10-17 15:11:34 +02:00
|
|
|
*/
|
2020-11-05 13:47:18 +01:00
|
|
|
typedef _Return_type_success_(return != FALSE)
|
2021-10-12 07:21:31 +02:00
|
|
|
BOOL(WINAPI WINTUN_DELETE_DRIVER_FUNC)(VOID);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the LUID of the adapter.
|
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @param Adapter Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter
|
2020-10-17 15:11:34 +02:00
|
|
|
*
|
|
|
|
* @param Luid Pointer to LUID to receive adapter LUID.
|
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(WINAPI WINTUN_GET_ADAPTER_LUID_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _Out_ NET_LUID *Luid);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
2020-11-02 17:42:52 +01:00
|
|
|
/**
|
|
|
|
* Determines the version of the Wintun driver currently loaded.
|
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @return If the function succeeds, the return value is the version number. If the function fails, the return value is
|
|
|
|
* zero. To get extended error information, call GetLastError. Possible errors include the following:
|
2020-11-04 01:08:41 +01:00
|
|
|
* ERROR_FILE_NOT_FOUND Wintun not loaded
|
2020-11-02 17:42:52 +01:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Return_type_success_(return != 0)
|
2021-10-12 07:21:31 +02:00
|
|
|
DWORD(WINAPI WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC)(VOID);
|
2020-11-02 17:42:52 +01:00
|
|
|
|
2020-11-04 18:23:47 +01:00
|
|
|
/**
|
|
|
|
* Determines the level of logging, passed to WINTUN_LOGGER_CALLBACK.
|
|
|
|
*/
|
2020-11-04 16:53:52 +01:00
|
|
|
typedef enum
|
2020-10-17 15:11:34 +02:00
|
|
|
{
|
2020-11-04 18:23:47 +01:00
|
|
|
WINTUN_LOG_INFO, /**< Informational */
|
|
|
|
WINTUN_LOG_WARN, /**< Warning */
|
|
|
|
WINTUN_LOG_ERR /**< Error */
|
2020-10-17 15:11:34 +02:00
|
|
|
} WINTUN_LOGGER_LEVEL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by internal logger to report diagnostic messages
|
|
|
|
*
|
|
|
|
* @param Level Message level.
|
|
|
|
*
|
2021-10-12 07:21:31 +02:00
|
|
|
* @param Timestamp Message timestamp in in 100ns intervals since 1601-01-01 UTC.
|
|
|
|
*
|
2020-10-17 15:11:34 +02:00
|
|
|
* @param Message Message text.
|
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(CALLBACK *WINTUN_LOGGER_CALLBACK)(
|
|
|
|
_In_ WINTUN_LOGGER_LEVEL Level,
|
|
|
|
_In_ DWORD64 Timestamp,
|
|
|
|
_In_z_ LPCWSTR Message);
|
2020-10-17 15:11:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets logger callback function.
|
|
|
|
*
|
2020-10-19 22:23:09 +02:00
|
|
|
* @param NewLogger Pointer to callback function to use as a new global logger. NewLogger may be called from various
|
|
|
|
* threads concurrently. Should the logging require serialization, you must handle serialization in
|
2020-11-02 17:42:52 +01:00
|
|
|
* NewLogger. Set to NULL to disable.
|
2020-10-17 15:11:34 +02:00
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(WINAPI WINTUN_SET_LOGGER_FUNC)(_In_ WINTUN_LOGGER_CALLBACK NewLogger);
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum ring capacity.
|
|
|
|
*/
|
|
|
|
#define WINTUN_MIN_RING_CAPACITY 0x20000 /* 128kiB */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum ring capacity.
|
|
|
|
*/
|
|
|
|
#define WINTUN_MAX_RING_CAPACITY 0x4000000 /* 64MiB */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A handle representing Wintun session
|
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef struct _TUN_SESSION *WINTUN_SESSION_HANDLE;
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts Wintun session.
|
|
|
|
*
|
2020-11-05 13:39:13 +01:00
|
|
|
* @param Adapter Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
|
2020-10-24 08:28:17 +02:00
|
|
|
*
|
|
|
|
* @param Capacity Rings capacity. Must be between WINTUN_MIN_RING_CAPACITY and WINTUN_MAX_RING_CAPACITY (incl.)
|
|
|
|
* Must be a power of two.
|
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @return Wintun session handle. Must be released with WintunEndSession. If the function fails, the return value is
|
|
|
|
* NULL. To get extended error information, call GetLastError.
|
2020-10-24 08:28:17 +02:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Must_inspect_result_
|
|
|
|
_Return_type_success_(return != NULL)
|
|
|
|
_Post_maybenull_
|
2021-10-12 07:21:31 +02:00
|
|
|
WINTUN_SESSION_HANDLE(WINAPI WINTUN_START_SESSION_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ DWORD Capacity);
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends Wintun session.
|
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(WINAPI WINTUN_END_SESSION_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
|
2020-10-24 08:28:17 +02:00
|
|
|
|
2020-11-02 16:04:33 +01:00
|
|
|
/**
|
|
|
|
* Gets Wintun session's read-wait event handle.
|
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
|
|
|
*
|
|
|
|
* @return Pointer to receive event handle to wait for available data when reading. Should
|
|
|
|
* WintunReceivePackets return ERROR_NO_MORE_ITEMS (after spinning on it for a while under heavy
|
|
|
|
* load), wait for this event to become signaled before retrying WintunReceivePackets. Do not call
|
|
|
|
* CloseHandle on this event - it is managed by the session.
|
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef HANDLE(WINAPI WINTUN_GET_READ_WAIT_EVENT_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
|
2020-11-02 16:04:33 +01:00
|
|
|
|
2020-10-24 08:28:17 +02:00
|
|
|
/**
|
|
|
|
* Maximum IP packet size
|
|
|
|
*/
|
|
|
|
#define WINTUN_MAX_IP_PACKET_SIZE 0xFFFF
|
|
|
|
|
|
|
|
/**
|
2020-11-05 13:01:41 +01:00
|
|
|
* Retrieves one or packet. After the packet content is consumed, call WintunReleaseReceivePacket with Packet returned
|
2020-10-25 10:01:35 +01:00
|
|
|
* from this function to release internal buffer. This function is thread-safe.
|
2020-10-24 08:28:17 +02:00
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @param PacketSize Pointer to receive packet size.
|
2020-10-25 10:01:35 +01:00
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @return Pointer to layer 3 IPv4 or IPv6 packet. Client may modify its content at will. If the function fails, the
|
|
|
|
* return value is NULL. To get extended error information, call GetLastError. Possible errors include the
|
|
|
|
* following:
|
|
|
|
* ERROR_HANDLE_EOF Wintun adapter is terminating;
|
|
|
|
* ERROR_NO_MORE_ITEMS Wintun buffer is exhausted;
|
|
|
|
* ERROR_INVALID_DATA Wintun buffer is corrupt
|
2020-10-24 08:28:17 +02:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Must_inspect_result_
|
|
|
|
_Return_type_success_(return != NULL)
|
|
|
|
_Post_maybenull_
|
|
|
|
_Post_writable_byte_size_(*PacketSize)
|
2021-10-12 07:21:31 +02:00
|
|
|
BYTE *(WINAPI WINTUN_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _Out_ DWORD *PacketSize);
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-25 10:01:35 +01:00
|
|
|
* Releases internal buffer after the received packet has been processed by the client. This function is thread-safe.
|
2020-10-24 08:28:17 +02:00
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
2020-10-25 10:01:35 +01:00
|
|
|
*
|
|
|
|
* @param Packet Packet obtained with WintunReceivePacket
|
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef VOID(
|
2021-10-12 07:21:31 +02:00
|
|
|
WINAPI WINTUN_RELEASE_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
|
2020-10-25 10:01:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates memory for a packet to send. After the memory is filled with packet data, call WintunSendPacket to send
|
|
|
|
* and release internal buffer. WintunAllocateSendPacket is thread-safe and the WintunAllocateSendPacket order of
|
|
|
|
* calls define the packet sending order.
|
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
|
|
|
*
|
|
|
|
* @param PacketSize Exact packet size. Must be less or equal to WINTUN_MAX_IP_PACKET_SIZE.
|
|
|
|
*
|
2020-11-03 12:29:34 +01:00
|
|
|
* @return Returns pointer to memory where to prepare layer 3 IPv4 or IPv6 packet for sending. If the function fails,
|
|
|
|
* the return value is NULL. To get extended error information, call GetLastError. Possible errors include the
|
|
|
|
* following:
|
|
|
|
* ERROR_HANDLE_EOF Wintun adapter is terminating;
|
|
|
|
* ERROR_BUFFER_OVERFLOW Wintun buffer is full;
|
2020-10-25 10:01:35 +01:00
|
|
|
*/
|
2021-07-28 20:20:09 +02:00
|
|
|
typedef _Must_inspect_result_
|
|
|
|
_Return_type_success_(return != NULL)
|
|
|
|
_Post_maybenull_
|
|
|
|
_Post_writable_byte_size_(PacketSize)
|
2021-10-12 07:21:31 +02:00
|
|
|
BYTE *(WINAPI WINTUN_ALLOCATE_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ DWORD PacketSize);
|
2020-10-25 10:01:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the packet and releases internal buffer. WintunSendPacket is thread-safe, but the WintunAllocateSendPacket
|
|
|
|
* order of calls define the packet sending order. This means the packet is not guaranteed to be sent in the
|
|
|
|
* WintunSendPacket yet.
|
|
|
|
*
|
|
|
|
* @param Session Wintun session handle obtained with WintunStartSession
|
|
|
|
*
|
|
|
|
* @param Packet Packet obtained with WintunAllocateSendPacket
|
2020-10-24 08:28:17 +02:00
|
|
|
*/
|
2021-10-12 07:21:31 +02:00
|
|
|
typedef VOID(WINAPI WINTUN_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
|
|
|
|
|
|
|
|
#pragma warning(pop)
|
2020-10-24 23:25:26 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|