42#if defined(__MINGW32__)
43const IN_ADDR in4addr_any = { 0 };
52 for (
int i = 0; i < 20; ++i)
54 MIB_IPINTERFACE_ROW row = { .InterfaceIndex = idx, .Family = AF_INET };
55 if (GetIpInterfaceEntry(&row) != ERROR_NOT_FOUND)
80 HANDLE h = CreateFile(
"\\\\.\\ovpn-dco-ver", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
82 if (h == INVALID_HANDLE_VALUE)
85 h = CreateFile(
"\\\\.\\ovpn-dco", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
88 if (h == INVALID_HANDLE_VALUE)
93 DWORD bytes_returned = 0;
95 &bytes_returned, NULL))
103 if (h != INVALID_HANDLE_VALUE)
125 ASSERT(dco->ifmode == DCO_MODE_UNINIT);
126 dco->ifmode = DCO_MODE_MP;
131 dco->ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
132 if (dco->ov.hEvent == NULL)
134 msg(
M_ERR,
"Error: ovpn_dco_init: CreateEvent failed");
137 dco->rwhandle.read = dco->ov.hEvent;
141 const char *device_guid;
147 DWORD bytes_returned = 0;
149 &bytes_returned, NULL))
151 msg(
M_ERR,
"DeviceIoControl(OVPN_IOCTL_SET_MODE) failed");
168 DWORD bytes_returned = 0;
171 msg(
M_ERR,
"DeviceIoControl(OVPN_IOCTL_START_VPN) failed");
200 dco->ifmode = DCO_MODE_P2P;
226 DWORD timeout_msec = timeout * 1000;
227 const int poll_interval_ms = 50;
229 while (timeout_msec > 0)
231 timeout_msec -= poll_interval_ms;
234 if (GetOverlappedResultEx(handle, ov, &transferred, poll_interval_ms, FALSE) != 0)
240 DWORD err = GetLastError();
241 if ((err != WAIT_TIMEOUT) && (err != ERROR_IO_INCOMPLETE))
250 if (*signal_received)
279 const struct addrinfo *cur = NULL;
281 for (cur = local; cur; cur = cur->ai_next)
283 if (cur->ai_family == ai_family)
290 msg(
M_FATAL,
"%s: Socket bind failed: Addr to bind has no %s record", __func__,
296 if (ai_family == AF_INET)
308 DWORD bytes_returned = 0;
310 &bytes_returned, NULL))
312 msg(
M_ERR,
"DeviceIoControl(OVPN_IOCTL_MP_START_VPN) failed");
326 struct sockaddr *local = NULL;
327 const struct sockaddr *remote = remoteaddr->ai_addr;
329 if (remoteaddr->ai_protocol == IPPROTO_TCP || remoteaddr->ai_socktype == SOCK_STREAM)
342 while (bind && !local)
344 if (bind->ai_family == remote->sa_family)
346 local = bind->ai_addr;
348 bind = bind->ai_next;
354 msg(
M_FATAL,
"DCO: Socket bind failed: Address to bind lacks %s record",
358 if (remote->sa_family == AF_INET6)
360 peer.
Remote.
Addr6 = *((SOCKADDR_IN6 *)(remoteaddr->ai_addr));
372 else if (remote->sa_family == AF_INET)
374 peer.
Remote.
Addr4 = *((SOCKADDR_IN *)(remoteaddr->ai_addr));
394 DWORD err = GetLastError();
395 if (err != ERROR_IO_PENDING)
397 msg(
M_ERR,
"DeviceIoControl(OVPN_IOCTL_NEW_PEER) failed");
409 struct sockaddr *localaddr,
struct sockaddr *remoteaddr,
410 const struct in_addr *vpn_ipv4,
const struct in6_addr *vpn_ipv6)
414 if (dco->ifmode == DCO_MODE_P2P)
425 newPeer.
Local.
Addr4.sin_family = remoteaddr->sa_family;
427 if (remoteaddr->sa_family == AF_INET)
429 memcpy(&newPeer.
Remote.
Addr4, remoteaddr,
sizeof(
struct sockaddr_in));
433 memcpy(&newPeer.
Remote.
Addr6, remoteaddr,
sizeof(
struct sockaddr_in6));
451 &bytesReturned, NULL))
470 if (dco->ifmode == DCO_MODE_MP)
474 len =
sizeof(del_peer);
477 DWORD bytes_returned = 0;
478 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
490 msg(
D_DCO_DEBUG,
"%s: peer-id %u, keepalive %d/%d, mss %d", __func__, peerid,
491 keepalive_interval, keepalive_timeout, mss);
493 OVPN_MP_SET_PEER mp_peer = { peerid, keepalive_interval, keepalive_timeout, mss };
494 OVPN_SET_PEER peer = { keepalive_interval, keepalive_timeout, mss };
499 if (dco->ifmode == DCO_MODE_MP)
510 DWORD bytes_returned = 0;
511 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
522 const uint8_t *encrypt_key,
const uint8_t *encrypt_iv,
const uint8_t *decrypt_key,
523 const uint8_t *decrypt_iv,
const char *ciphername,
bool epoch)
525 msg(
D_DCO_DEBUG,
"%s: slot %d, key-id %d, peer-id %u, cipher %s", __func__, slot, keyid, peerid,
528 const int nonce_len = 8;
533 ZeroMemory(&crypto_data,
sizeof(crypto_data));
537 v1->
CipherAlg = dco_get_cipher(ciphername);
538 ASSERT(keyid >= 0 && keyid <= UCHAR_MAX);
539 v1->
KeyId = (
unsigned char)keyid;
544 CopyMemory(v1->
Encrypt.
Key, encrypt_key, epoch ? 32 : key_len);
548 CopyMemory(v1->
Decrypt.
Key, decrypt_key, epoch ? 32 : key_len);
555 VOID *buf = &crypto_data.
V1;
556 DWORD bufSize =
sizeof(crypto_data.
V1);
562 bufSize =
sizeof(crypto_data);
565 DWORD bytes_returned = 0;
567 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, bufSize, NULL, 0, &bytes_returned, NULL))
578 msg(
D_DCO,
"%s: peer-id %u, slot %d called but ignored", __func__, peerid, slot);
593 if (dco->ifmode == DCO_MODE_MP)
600 DWORD bytes_returned = 0;
601 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
613 HANDLE h = CreateFile(
"\\\\.\\ovpn-dco", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
614 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, NULL);
616 if (h != INVALID_HANDLE_VALUE)
622 DWORD err = GetLastError();
623 if (err == ERROR_ACCESS_DENIED)
632 msg(msglevel,
"Note: ovpn-dco-win driver is missing, disabling data channel offload.");
673 dco->dco_message_peer_id = dco->notif_buf.PeerId;
674 dco->dco_message_type = dco->notif_buf.Cmd;
675 dco->dco_del_peer_reason = dco->notif_buf.DelPeerReason;
676 dco->dco_float_peer_ss = dco->notif_buf.FloatAddress;
692 dco->dco_message_peer_id = -1;
693 dco->dco_message_type = 0;
695 switch (dco->iostate)
722 if (dco->c->mode ==
CM_TOP)
752 DWORD required_size = 0, bytes_returned = 0;
756 if (GetLastError() == ERROR_MORE_DATA)
758 if (bytes_returned !=
sizeof(DWORD))
760 msg(
M_WARN,
"%s: invalid bytes returned for size query (%lu, expected %zu)", __func__, bytes_returned,
sizeof(DWORD));
765 if (required_size == 0)
779 msg(
M_WARN |
M_ERRNO,
"%s: failed to fetch required buffer size", __func__);
787 if (bytes_returned == 0)
793 msg(
M_WARN,
"%s: first DeviceIoControl call succeeded unexpectedly (%lu bytes returned)", __func__, bytes_returned);
803 msg(
M_WARN,
"%s: failed to allocate buffer of size %lu", __func__, required_size);
811 if (GetLastError() == ERROR_MORE_DATA)
813 msg(
M_WARN,
"%s: peer has been added, skip fetching stats", __func__);
828 if (stat->
PeerId >= (
int)
dco->c->multi->max_clients)
830 msg(
M_WARN,
"%s: received out of bound peer_id %d (max=%u)", __func__, stat->
PeerId,
831 dco->c->multi->max_clients);
838 msg(
M_WARN,
"%s: received data for a non-existing peer %d", __func__, stat->
PeerId);
853 if (raise_sigusr1_on_err && ret < 0)
874 DWORD bytes_returned = 0;
876 &bytes_returned, NULL))
904 DWORD bytes_returned = 0;
906 &bytes_returned, NULL))
908 if (GetLastError() == ERROR_INVALID_FUNCTION)
914 msg(
M_WARN |
M_ERRNO,
"%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) failed", __func__);
920 msg(
M_WARN |
M_ERRNO,
"%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) returned invalid size", __func__);
935 if (
dco->ifmode != DCO_MODE_MP)
949 sizeof(
dco->notif_buf), NULL, &
dco->ov))
951 DWORD err = GetLastError();
952 if (err == ERROR_IO_PENDING)
955 dco->ov_ret = ERROR_SUCCESS;
973 dco->ov_ret = ERROR_SUCCESS;
990 NTSTATUS
status = BCryptOpenAlgorithmProvider(&h, L
"CHACHA20_POLY1305", NULL, 0);
991 if (BCRYPT_SUCCESS(
status))
993 BCryptCloseAlgorithmProvider(h, 0);
994 return "AES-128-GCM:AES-256-GCM:AES-192-GCM:CHACHA20-POLY1305";
998 return "AES-128-GCM:AES-256-GCM:AES-192-GCM";
1011 unsigned int peer_id)
1016 .
Addr.
Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = peer_id, .IPv6 = 0
1022 DWORD bytes_returned = 0;
1024 &bytes_returned, NULL))
1034 unsigned int peer_id)
1043 DWORD bytes_returned = 0;
1045 &bytes_returned, NULL))
1059 .
Addr.
Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = -1, .IPv6 = 0
1064 DWORD bytes_returned = 0;
1066 &bytes_returned, NULL))
1083 DWORD bytes_returned = 0;
1085 &bytes_returned, NULL))
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Data Channel Cryptography Module.
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
int dco_del_key(dco_context_t *dco, unsigned int peerid, dco_key_slot_t slot)
bool dco_supports_epoch_data(struct context *c)
bool dco_available(msglvl_t msglevel)
void dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
Initializes and binds the kernel UDP transport socket for multipeer mode.
int dco_del_peer(dco_context_t *dco, unsigned int peerid)
void dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits, unsigned int peer_id)
const char * dco_version_string(struct gc_arena *gc)
void dco_p2p_start_vpn(struct tuntap *tt)
Transitions the DCO adapter to the connected state in P2P mode.
int dco_read_and_process(dco_context_t *dco)
const char * dco_get_supported_ciphers(void)
bool ovpn_dco_init(struct context *c)
Initializes DCO depends on mode
static void dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, struct signal_info *sig_info)
void dco_win_del_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits)
bool dco_win_supports_multipeer(void)
int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
void dco_win_del_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits)
int dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on_err)
static void dco_wait_ready(DWORD idx)
int dco_set_peer(dco_context_t *dco, unsigned int peerid, int keepalive_interval, int keepalive_timeout, int mss)
int dco_swap_keys(dco_context_t *dco, unsigned int peer_id)
void ovpn_dco_init_mp(dco_context_t *dco, const char *dev_node)
Initializes the DCO adapter in multipeer mode and sets it to "connected" state.
int dco_new_peer(dco_context_t *dco, unsigned int peerid, socket_descriptor_t sd, struct sockaddr *localaddr, struct sockaddr *remoteaddr, const struct in_addr *vpn_ipv4, const struct in6_addr *vpn_ipv6)
int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
static void dco_handle_overlapped_success(dco_context_t *dco, bool queued)
Handles successful completion of overlapped operation.
static bool dco_get_version(OVPN_VERSION *version)
Gets version of dco-win driver.
void dco_p2p_new_peer(HANDLE handle, OVERLAPPED *ov, struct link_socket *sock, struct signal_info *sig_info)
void dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits, unsigned int peer_id)
void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
int dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_slot_t slot, const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const uint8_t *decrypt_key, const uint8_t *decrypt_iv, const char *ciphername, bool epoch)
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
void process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS status
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Header file for server-mode related structures and functions.
void multi_process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
#define MODE_POINT_TO_POINT
#define OVPN_IOCTL_GET_STATS
#define OVPN_IOCTL_NEW_KEY_V2
#define OVPN_IOCTL_GET_VERSION
#define OVPN_IOCTL_SWAP_KEYS
#define OVPN_IOCTL_NEW_KEY
#define OVPN_IOCTL_NOTIFY_EVENT
#define OVPN_IOCTL_MP_DEL_IROUTE
#define OVPN_IOCTL_MP_SET_PEER
#define OVPN_IOCTL_MP_SWAP_KEYS
#define OVPN_IOCTL_GET_PEER_STATS
#define OVPN_IOCTL_SET_PEER
#define OVPN_IOCTL_NEW_PEER
#define OVPN_IOCTL_MP_ADD_IROUTE
#define CRYPTO_OPTIONS_EPOCH
#define OVPN_IOCTL_START_VPN
#define OVPN_IOCTL_MP_NEW_PEER
#define OVPN_IOCTL_SET_MODE
#define OVPN_IOCTL_DEL_PEER
#define OVPN_IOCTL_MP_START_VPN
struct _OVPN_MP_SET_PEER OVPN_MP_SET_PEER
#define OVPN_IOCTL_MP_DEL_PEER
struct _OVPN_PEER_STATS OVPN_PEER_STATS
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
const char * addr_family_name(int af)
Control Channel Common Data Structures.
OVPN_KEY_DIRECTION Decrypt
OVPN_KEY_DIRECTION Encrypt
OVPN_CIPHER_ALG CipherAlg
unsigned char NonceTail[8]
union _OVPN_MP_IROUTE::@20 Addr
union _OVPN_MP_NEW_PEER::@17 Local
union _OVPN_MP_NEW_PEER::@18 Remote
union _OVPN_MP_START_VPN::@19 ListenAddress
union _OVPN_NEW_PEER::@15 Local
union _OVPN_NEW_PEER::@16 Remote
LONG64 TransportBytesSent
LONG64 TransportBytesReceived
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
struct tuntap * tuntap
Tun/tap virtual network interface.
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
counter_type dco_read_bytes
counter_type tun_read_bytes
counter_type dco_write_bytes
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
counter_type tun_write_bytes
Contains all state information for one tunnel.
int mode
Role of this context within the OpenVPN process.
struct context_2 c2
Level 2 context.
struct options options
Options loaded from command line or configuration file.
struct context_1 c1
Level 1 context.
Garbage collection arena used to keep track of dynamically allocated memory.
struct addrinfo * bind_local
struct addrinfo * current_remote
struct link_socket_addr * lsa
struct event_timeout * server_poll_timeout
struct link_socket_info info
Server-mode state structure for one single VPN tunnel.
struct context context
The context structure storing state for this VPN tunnel.
volatile int signal_received
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
SOCKET socket_descriptor_t
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
static bool tuntap_defined(const struct tuntap *tt)
#define IOSTATE_IMMEDIATE_RETURN