OpenVPN
dco_win.c
Go to the documentation of this file.
1/*
2 * Interface to ovpn-win-dco networking code
3 *
4 * Copyright (C) 2020-2025 Arne Schwabe <arne@rfc2549.org>
5 * Copyright (C) 2020-2025 OpenVPN Inc <sales@openvpn.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING included with this
18 * distribution); if not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#if defined(_WIN32)
26
27#include "syshead.h"
28
29#include "dco.h"
30#include "forward.h"
31#include "tun.h"
32#include "crypto.h"
33#include "multi.h"
34#include "ssl_common.h"
35#include "openvpn.h"
36
37#include <bcrypt.h>
38#include <winsock2.h>
39#include <ws2tcpip.h>
40
41#if defined(__MINGW32__)
42const IN_ADDR in4addr_any = { 0 };
43#endif
44
45/* Sometimes IP Helper API, which we use for setting IP address etc,
46 * complains that interface is not found. Give it some time to settle
47 */
48static void
50{
51 for (int i = 0; i < 20; ++i)
52 {
53 MIB_IPINTERFACE_ROW row = { .InterfaceIndex = idx, .Family = AF_INET };
54 if (GetIpInterfaceEntry(&row) != ERROR_NOT_FOUND)
55 {
56 break;
57 }
58 msg(D_DCO_DEBUG, "interface %ld not yet ready, retrying", idx);
59 Sleep(50);
60 }
61}
62
72static bool
74{
75 CLEAR(*version);
76
77 bool res = false;
78
79 HANDLE h = CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
80
81 if (h == INVALID_HANDLE_VALUE)
82 {
83 /* fallback to a "normal" device, this will fail if device is already in use */
84 h = CreateFile("\\\\.\\ovpn-dco", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
85 }
86
87 if (h == INVALID_HANDLE_VALUE)
88 {
89 goto done;
90 }
91
92 DWORD bytes_returned = 0;
93 if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0, version, sizeof(*version),
94 &bytes_returned, NULL))
95 {
96 goto done;
97 }
98
99 res = true;
100
101done:
102 if (h != INVALID_HANDLE_VALUE)
103 {
104 CloseHandle(h);
105 }
106
107 msg(D_DCO_DEBUG, "dco version: %ld.%ld.%ld", version->Major, version->Minor, version->Patch);
108
109 return res;
110}
111
121void
122ovpn_dco_init_mp(dco_context_t *dco, const char *dev_node)
123{
124 ASSERT(dco->ifmode == DCO_MODE_UNINIT);
125 dco->ifmode = DCO_MODE_MP;
126
127 /* Use manual reset event so it remains signalled until
128 * explicitly reset. This way we won't lose notifications
129 */
130 dco->ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
131 if (dco->ov.hEvent == NULL)
132 {
133 msg(M_ERR, "Error: ovpn_dco_init: CreateEvent failed");
134 }
135
136 dco->rwhandle.read = dco->ov.hEvent;
137
138 /* open DCO device */
139 struct gc_arena gc = gc_new();
140 const char *device_guid;
141 tun_open_device(dco->tt, dev_node, &device_guid, &gc);
142 gc_free(&gc);
143
144 /* set mp mode */
146 DWORD bytes_returned = 0;
147 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_SET_MODE, &m, sizeof(m), NULL, 0,
148 &bytes_returned, NULL))
149 {
150 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_SET_MODE) failed");
151 }
152
153 dco_wait_ready(dco->tt->adapter_index);
154}
155
164void
166{
167 DWORD bytes_returned = 0;
168 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_START_VPN, NULL, 0, NULL, 0, &bytes_returned, NULL))
169 {
170 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_START_VPN) failed");
171 }
172
173 /* Sometimes IP Helper API, which we use for setting IP address etc,
174 * complains that interface is not found. Give it some time to settle
175 */
177}
178
179
189bool
191{
192 dco_context_t *dco = &c->c1.tuntap->dco;
193
194 dco->c = c;
195
196 switch (c->mode)
197 {
199 dco->ifmode = DCO_MODE_P2P;
200 dco_p2p_start_vpn(dco->tt);
201 break;
202
203 case MODE_SERVER:
205 break;
206
207 default:
208 ASSERT(false);
209 }
210
211 return true;
212}
213
214int
215open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
216{
217 ASSERT(0);
218 return 0;
219}
220
221static void
222dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, struct signal_info *sig_info)
223{
224 volatile int *signal_received = &sig_info->signal_received;
225 /* GetOverlappedResultEx is available starting from Windows 8 */
226 typedef BOOL(WINAPI * get_overlapped_result_ex_t)(HANDLE, LPOVERLAPPED, LPDWORD, DWORD, BOOL);
227 get_overlapped_result_ex_t get_overlapped_result_ex =
228 (get_overlapped_result_ex_t)GetProcAddress(GetModuleHandle("Kernel32.dll"),
229 "GetOverlappedResultEx");
230
231 if (get_overlapped_result_ex == NULL)
232 {
233 msg(M_ERR, "Failed to load GetOverlappedResult()");
234 }
235
236 DWORD timeout_msec = timeout * 1000;
237 const int poll_interval_ms = 50;
238
239 while (timeout_msec > 0)
240 {
241 timeout_msec -= poll_interval_ms;
242
243 DWORD transferred;
244 if (get_overlapped_result_ex(handle, ov, &transferred, poll_interval_ms, FALSE) != 0)
245 {
246 /* TCP connection established by dco */
247 return;
248 }
249
250 DWORD err = GetLastError();
251 if ((err != WAIT_TIMEOUT) && (err != ERROR_IO_INCOMPLETE))
252 {
253 /* dco reported connection error */
254 msg(M_NONFATAL | M_ERRNO, "dco connect error");
255 register_signal(sig_info, SIGUSR1, "dco-connect-error");
256 return;
257 }
258
259 get_signal(signal_received);
260 if (*signal_received)
261 {
262 return;
263 }
264
266 }
267
268 /* we end up here when timeout occurs in userspace */
269 msg(M_NONFATAL, "dco connect timeout");
270 register_signal(sig_info, SIGUSR1, "dco-connect-timeout");
271}
272
282void
283dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
284{
285 msg(D_DCO_DEBUG, "%s", __func__);
286
287 int ai_family = sock->info.lsa->bind_local->ai_family;
288 struct addrinfo *local = sock->info.lsa->bind_local;
289 struct addrinfo *cur = NULL;
290
291 for (cur = local; cur; cur = cur->ai_next)
292 {
293 if (cur->ai_family == ai_family)
294 {
295 break;
296 }
297 }
298 if (!cur)
299 {
300 msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", __func__,
301 addr_family_name(ai_family));
302 }
303
304 OVPN_MP_START_VPN in, out;
305 in.IPv6Only = sock->info.bind_ipv6_only ? 1 : 0;
306 if (ai_family == AF_INET)
307 {
308 memcpy(&in.ListenAddress.Addr4, cur->ai_addr, sizeof(struct sockaddr_in));
309 }
310 else
311 {
312 memcpy(&in.ListenAddress.Addr6, cur->ai_addr, sizeof(struct sockaddr_in6));
313 }
314
315 /* in multipeer mode control channel packets are prepended with remote peer's sockaddr */
316 sock->sockflags |= SF_PREPEND_SA;
317
318 DWORD bytes_returned = 0;
319 if (!DeviceIoControl(handle, OVPN_IOCTL_MP_START_VPN, &in, sizeof(in), &out, sizeof(out),
320 &bytes_returned, NULL))
321 {
322 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_MP_START_VPN) failed");
323 }
324}
325
326void
327dco_p2p_new_peer(HANDLE handle, OVERLAPPED *ov, struct link_socket *sock,
328 struct signal_info *sig_info)
329{
330 msg(D_DCO_DEBUG, "%s", __func__);
331
332 OVPN_NEW_PEER peer = { 0 };
333
334 struct addrinfo *remoteaddr = sock->info.lsa->current_remote;
335
336 struct sockaddr *local = NULL;
337 struct sockaddr *remote = remoteaddr->ai_addr;
338
339 if (remoteaddr->ai_protocol == IPPROTO_TCP || remoteaddr->ai_socktype == SOCK_STREAM)
340 {
341 peer.Proto = OVPN_PROTO_TCP;
342 }
343 else
344 {
345 peer.Proto = OVPN_PROTO_UDP;
346 }
347
348 if (sock->bind_local)
349 {
350 /* Use first local address with correct address family */
351 struct addrinfo *bind = sock->info.lsa->bind_local;
352 while (bind && !local)
353 {
354 if (bind->ai_family == remote->sa_family)
355 {
356 local = bind->ai_addr;
357 }
358 bind = bind->ai_next;
359 }
360 }
361
362 if (sock->bind_local && !local)
363 {
364 msg(M_FATAL, "DCO: Socket bind failed: Address to bind lacks %s record",
365 addr_family_name(remote->sa_family));
366 }
367
368 if (remote->sa_family == AF_INET6)
369 {
370 peer.Remote.Addr6 = *((SOCKADDR_IN6 *)(remoteaddr->ai_addr));
371 if (local)
372 {
373 peer.Local.Addr6 = *((SOCKADDR_IN6 *)local);
374 }
375 else
376 {
377 peer.Local.Addr6.sin6_addr = in6addr_any;
378 peer.Local.Addr6.sin6_port = 0;
379 peer.Local.Addr6.sin6_family = AF_INET6;
380 }
381 }
382 else if (remote->sa_family == AF_INET)
383 {
384 peer.Remote.Addr4 = *((SOCKADDR_IN *)(remoteaddr->ai_addr));
385 if (local)
386 {
387 peer.Local.Addr4 = *((SOCKADDR_IN *)local);
388 }
389 else
390 {
391 peer.Local.Addr4.sin_addr = in4addr_any;
392 peer.Local.Addr4.sin_port = 0;
393 peer.Local.Addr4.sin_family = AF_INET;
394 }
395 }
396 else
397 {
398 ASSERT(0);
399 }
400
401 CLEAR(*ov);
402 if (!DeviceIoControl(handle, OVPN_IOCTL_NEW_PEER, &peer, sizeof(peer), NULL, 0, NULL, ov))
403 {
404 DWORD err = GetLastError();
405 if (err != ERROR_IO_PENDING)
406 {
407 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_NEW_PEER) failed");
408 }
409 else
410 {
412 sig_info);
413 }
414 }
415}
416
417int
418dco_new_peer(dco_context_t *dco, unsigned int peerid, socket_descriptor_t sd, struct sockaddr *localaddr,
419 struct sockaddr *remoteaddr, struct in_addr *vpn_ipv4, struct in6_addr *vpn_ipv6)
420{
421 msg(D_DCO_DEBUG, "%s: peer-id %d, fd " SOCKET_PRINTF, __func__, peerid, sd);
422
423 if (dco->ifmode == DCO_MODE_P2P)
424 {
425 /* no-op for p2p */
426 return 0;
427 }
428
429 OVPN_MP_NEW_PEER newPeer = { 0 };
430
431 if (remoteaddr)
432 {
433 /* while the driver doesn't use the local address yet it requires its AF to be valid */
434 newPeer.Local.Addr4.sin_family = remoteaddr->sa_family;
435
436 if (remoteaddr->sa_family == AF_INET)
437 {
438 memcpy(&newPeer.Remote.Addr4, remoteaddr, sizeof(struct sockaddr_in));
439 }
440 else
441 {
442 memcpy(&newPeer.Remote.Addr6, remoteaddr, sizeof(struct sockaddr_in6));
443 }
444 }
445
446 if (vpn_ipv4)
447 {
448 newPeer.VpnAddr4 = *vpn_ipv4;
449 }
450
451 if (vpn_ipv6)
452 {
453 newPeer.VpnAddr6 = *vpn_ipv6;
454 }
455
456 newPeer.PeerId = peerid;
457
458 DWORD bytesReturned;
459 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_NEW_PEER, &newPeer, sizeof(newPeer), NULL, 0,
460 &bytesReturned, NULL))
461 {
462 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_MP_NEW_PEER) failed");
463 }
464
465 return 0;
466}
467
468int
469dco_del_peer(dco_context_t *dco, unsigned int peerid)
470{
471 msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peerid);
472
473 OVPN_MP_DEL_PEER del_peer = { peerid };
474 VOID *buf = NULL;
475 DWORD len = 0;
476 DWORD ioctl = OVPN_IOCTL_DEL_PEER;
477
478 if (dco->ifmode == DCO_MODE_MP)
479 {
481 buf = &del_peer;
482 len = sizeof(del_peer);
483 }
484
485 DWORD bytes_returned = 0;
486 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
487 {
488 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_DEL_PEER) failed");
489 return -1;
490 }
491 return 0;
492}
493
494int
495dco_set_peer(dco_context_t *dco, unsigned int peerid, int keepalive_interval, int keepalive_timeout,
496 int mss)
497{
498 msg(D_DCO_DEBUG, "%s: peer-id %d, keepalive %d/%d, mss %d", __func__, peerid,
499 keepalive_interval, keepalive_timeout, mss);
500
501 OVPN_MP_SET_PEER mp_peer = { peerid, keepalive_interval, keepalive_timeout, mss };
502 OVPN_SET_PEER peer = { keepalive_interval, keepalive_timeout, mss };
503 VOID *buf = NULL;
504 DWORD len = 0;
505 DWORD ioctl = (dco->ifmode == DCO_MODE_MP) ? OVPN_IOCTL_MP_SET_PEER : OVPN_IOCTL_SET_PEER;
506
507 if (dco->ifmode == DCO_MODE_MP)
508 {
509 buf = &mp_peer;
510 len = sizeof(OVPN_MP_SET_PEER);
511 }
512 else
513 {
514 buf = &peer;
515 len = sizeof(OVPN_SET_PEER);
516 }
517
518 DWORD bytes_returned = 0;
519 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
520 {
521 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_SET_PEER) failed");
522 return -1;
523 }
524
525 return 0;
526}
527
528int
529dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_slot_t slot,
530 const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const uint8_t *decrypt_key,
531 const uint8_t *decrypt_iv, const char *ciphername, bool epoch)
532{
533 msg(D_DCO_DEBUG, "%s: slot %d, key-id %d, peer-id %d, cipher %s", __func__, slot, keyid, peerid,
534 ciphername);
535
536 const int nonce_len = 8;
537 size_t key_len = cipher_kt_key_size(ciphername);
538 ASSERT(key_len <= 32);
539
540 OVPN_CRYPTO_DATA_V2 crypto_data;
541 ZeroMemory(&crypto_data, sizeof(crypto_data));
542
543 OVPN_CRYPTO_DATA *v1 = &crypto_data.V1;
544
545 v1->CipherAlg = dco_get_cipher(ciphername);
546 ASSERT(keyid >= 0 && keyid <= UCHAR_MAX);
547 v1->KeyId = (unsigned char)keyid;
548 v1->PeerId = peerid;
549 v1->KeySlot = slot;
550
551 /* for epoch we use key material as a seed, no as actual key */
552 CopyMemory(v1->Encrypt.Key, encrypt_key, epoch ? 32 : key_len);
553 v1->Encrypt.KeyLen = (unsigned char)key_len;
554 CopyMemory(v1->Encrypt.NonceTail, encrypt_iv, nonce_len);
555
556 CopyMemory(v1->Decrypt.Key, decrypt_key, epoch ? 32 : key_len);
557 v1->Decrypt.KeyLen = (unsigned char)key_len;
558 CopyMemory(v1->Decrypt.NonceTail, decrypt_iv, nonce_len);
559
560 ASSERT(v1->CipherAlg > 0);
561
562 DWORD ioctl = OVPN_IOCTL_NEW_KEY;
563 VOID *buf = &crypto_data.V1;
564 DWORD bufSize = sizeof(crypto_data.V1);
565 if (epoch)
566 {
567 ioctl = OVPN_IOCTL_NEW_KEY_V2;
568 crypto_data.CryptoOptions |= CRYPTO_OPTIONS_EPOCH;
569 buf = &crypto_data;
570 bufSize = sizeof(crypto_data);
571 }
572
573 DWORD bytes_returned = 0;
574
575 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, bufSize, NULL, 0, &bytes_returned, NULL))
576 {
577 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_NEW_KEY) failed");
578 return -1;
579 }
580 return 0;
581}
582
583int
584dco_del_key(dco_context_t *dco, unsigned int peerid, dco_key_slot_t slot)
585{
586 msg(D_DCO, "%s: peer-id %d, slot %d called but ignored", __func__, peerid, slot);
587 /* FIXME: Implement in driver first */
588 return 0;
589}
590
591int
592dco_swap_keys(dco_context_t *dco, unsigned int peer_id)
593{
594 msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peer_id);
595
596 OVPN_MP_SWAP_KEYS swap = { peer_id };
597 DWORD ioctl = OVPN_IOCTL_SWAP_KEYS;
598 VOID *buf = NULL;
599 DWORD len = 0;
600
601 if (dco->ifmode == DCO_MODE_MP)
602 {
604 buf = &swap;
605 len = sizeof(swap);
606 }
607
608 DWORD bytes_returned = 0;
609 if (!DeviceIoControl(dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))
610 {
611 msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_SWAP_KEYS) failed");
612 return -1;
613 }
614 return 0;
615}
616
617bool
619{
620 /* try to open device by symbolic name */
621 HANDLE h = CreateFile("\\\\.\\ovpn-dco", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
622 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, NULL);
623
624 if (h != INVALID_HANDLE_VALUE)
625 {
626 CloseHandle(h);
627 return true;
628 }
629
630 DWORD err = GetLastError();
631 if (err == ERROR_ACCESS_DENIED)
632 {
633 /* this likely means that device exists but is already in use,
634 * don't bail out since later we try to open all existing dco
635 * devices and then bail out if all devices are in use
636 */
637 return true;
638 }
639
640 msg(msglevel, "Note: ovpn-dco-win driver is missing, disabling data channel offload.");
641 return false;
642}
643
644const char *
646{
647 OVPN_VERSION version = { 0 };
648 if (dco_get_version(&version))
649 {
650 struct buffer out = alloc_buf_gc(256, gc);
651 buf_printf(&out, "%ld.%ld.%ld", version.Major, version.Minor, version.Patch);
652 return BSTR(&out);
653 }
654 else
655 {
656 return "N/A";
657 }
658}
659
671static void
673{
674 DWORD bytes_read = 0;
675 BOOL res = GetOverlappedResult(dco->tt->hand, &dco->ov, &bytes_read, FALSE);
676 if (res)
677 {
678 msg(D_DCO_DEBUG, "%s: completion%s success [%ld]", __func__, queued ? "" : " non-queued",
679 bytes_read);
680
681 dco->dco_message_peer_id = dco->notif_buf.PeerId;
682 dco->dco_message_type = dco->notif_buf.Cmd;
683 dco->dco_del_peer_reason = dco->notif_buf.DelPeerReason;
684 dco->dco_float_peer_ss = dco->notif_buf.FloatAddress;
685 }
686 else
687 {
688 msg(D_DCO_DEBUG | M_ERRNO, "%s: completion%s error", __func__, queued ? "" : " non-queued");
689 }
690}
691
692int
694{
695 if (dco->ifmode != DCO_MODE_MP)
696 {
697 ASSERT(false);
698 }
699
700 dco->dco_message_peer_id = -1;
701 dco->dco_message_type = 0;
702
703 switch (dco->iostate)
704 {
705 case IOSTATE_QUEUED:
707
708 ASSERT(ResetEvent(dco->ov.hEvent));
709 dco->iostate = IOSTATE_INITIAL;
710
711 break;
712
714 dco->iostate = IOSTATE_INITIAL;
715 ASSERT(ResetEvent(dco->ov.hEvent));
716
717 if (dco->ov_ret == ERROR_SUCCESS)
718 {
720 }
721 else
722 {
723 SetLastError(dco->ov_ret);
724 msg(D_DCO_DEBUG | M_ERRNO, "%s: completion non-queued error", __func__);
725 }
726
727 break;
728 }
729
730 return 0;
731}
732
733int
735{
736 struct gc_arena gc = gc_new();
737
738 int ret = 0;
739 struct tuntap *tt = dco->tt;
740
741 if (!tuntap_defined(tt))
742 {
743 ret = -1;
744 goto done;
745 }
746
748 .PeerId = -1
749 };
750
751 DWORD required_size = 0, bytes_returned = 0;
752 /* first, figure out buffer size */
753 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &required_size, sizeof(DWORD), &bytes_returned, NULL))
754 {
755 if (GetLastError() == ERROR_MORE_DATA)
756 {
757 if (bytes_returned != sizeof(DWORD))
758 {
759 msg(M_WARN, "%s: invalid bytes returned for size query (%lu, expected %zu)", __func__, bytes_returned, sizeof(DWORD));
760 ret = -1;
761 goto done;
762 }
763 /* required_size now contains the size written by the driver */
764 if (required_size == 0)
765 {
766 ret = 0; /* no peers to process */
767 goto done;
768 }
769 if (required_size < sizeof(OVPN_PEER_STATS))
770 {
771 msg(M_WARN, "%s: invalid required size %lu (minimum %zu)", __func__, required_size, sizeof(OVPN_PEER_STATS));
772 ret = -1;
773 goto done;
774 }
775 }
776 else
777 {
778 msg(M_WARN | M_ERRNO, "%s: failed to fetch required buffer size", __func__);
779 ret = -1;
780 goto done;
781 }
782 }
783 else
784 {
785 /* unexpected success? */
786 if (bytes_returned == 0)
787 {
788 ret = 0; /* no peers to process */
789 goto done;
790 }
791
792 msg(M_WARN, "%s: first DeviceIoControl call succeeded unexpectedly (%lu bytes returned)", __func__, bytes_returned);
793 ret = -1;
794 goto done;
795 }
796
797
798 /* allocate the buffer and fetch stats */
799 OVPN_PEER_STATS *peer_stats = gc_malloc(required_size, true, &gc);
800 if (!peer_stats)
801 {
802 msg(M_WARN, "%s: failed to allocate buffer of size %lu", __func__, required_size);
803 ret = -1;
804 goto done;
805 }
806
807 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), peer_stats, required_size, &bytes_returned, NULL))
808 {
809 /* unlikely case when a peer has been added since fetching buffer size, not an error! */
810 if (GetLastError() == ERROR_MORE_DATA)
811 {
812 msg(M_WARN, "%s: peer has been added, skip fetching stats", __func__);
813 ret = 0;
814 goto done;
815 }
816
817 msg(M_WARN | M_ERRNO, "%s: failed to fetch multipeer stats", __func__);
818 ret = -1;
819 goto done;
820 }
821
822 /* iterate over stats and update peers */
823 for (int i = 0; i < bytes_returned / sizeof(OVPN_PEER_STATS); ++i)
824 {
825 OVPN_PEER_STATS *stat = &peer_stats[i];
826
827 if (stat->PeerId >= dco->c->multi->max_clients)
828 {
829 msg(M_WARN, "%s: received out of bound peer_id %u (max=%u)", __func__, stat->PeerId,
830 dco->c->multi->max_clients);
831 continue;
832 }
833
834 struct multi_instance *mi = dco->c->multi->instances[stat->PeerId];
835 if (!mi)
836 {
837 msg(M_WARN, "%s: received data for a non-existing peer %u", __func__, stat->PeerId);
838 continue;
839 }
840
841 /* update peer stats */
842 struct context_2 *c2 = &mi->context.c2;
843 c2->dco_read_bytes = stat->LinkRxBytes;
844 c2->dco_write_bytes = stat->LinkTxBytes;
845 c2->tun_read_bytes = stat->VpnRxBytes;
846 c2->tun_write_bytes = stat->VpnTxBytes;
847 }
848
849done:
850 gc_free(&gc);
851
852 if (raise_sigusr1_on_err && ret < 0)
853 {
854 register_signal(dco->c->sig, SIGUSR1, "dco peer stats error");
855 }
856
857 return ret;
858}
859
860int
861dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on_err)
862{
863 struct tuntap *tt = c->c1.tuntap;
864
865 if (!tuntap_defined(tt))
866 {
867 return -1;
868 }
869
870 OVPN_STATS stats;
871 ZeroMemory(&stats, sizeof(OVPN_STATS));
872
873 DWORD bytes_returned = 0;
874 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_STATS, NULL, 0, &stats, sizeof(stats),
875 &bytes_returned, NULL))
876 {
877 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_GET_STATS) failed");
878 return -1;
879 }
880
885
886 return 0;
887}
888
889int
890dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
891{
892 struct tuntap *tt = c->c1.tuntap;
893
894 if (!tuntap_defined(tt))
895 {
896 return -1;
897 }
898
899 /* first, try a new ioctl */
901
902 OVPN_PEER_STATS peer_stats = { 0 };
903 DWORD bytes_returned = 0;
904 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &peer_stats, sizeof(peer_stats),
905 &bytes_returned, NULL))
906 {
907 if (GetLastError() == ERROR_INVALID_FUNCTION)
908 {
909 /* are we using the old driver? */
910 return dco_get_peer_stats_fallback(c, raise_sigusr1_on_err);
911 }
912
913 msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) failed", __func__);
914 return -1;
915 }
916
917 if (bytes_returned != sizeof(OVPN_PEER_STATS))
918 {
919 msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) returned invalid size", __func__);
920 return -1;
921 }
922
923 c->c2.dco_read_bytes = peer_stats.LinkRxBytes;
924 c->c2.dco_write_bytes = peer_stats.LinkTxBytes;
925 c->c2.tun_read_bytes = peer_stats.VpnRxBytes;
926 c->c2.tun_write_bytes = peer_stats.VpnTxBytes;
927
928 return 0;
929}
930
931void
933{
934 if (dco->ifmode != DCO_MODE_MP)
935 {
936 /* mp only */
937 return;
938 }
939
940 event_ctl(es, &dco->rwhandle, EVENT_READ, arg);
941
942 if (dco->iostate == IOSTATE_INITIAL)
943 {
944 /* the overlapped IOCTL will signal this event on I/O completion */
945 ASSERT(ResetEvent(dco->ov.hEvent));
946
947 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_NOTIFY_EVENT, NULL, 0, &dco->notif_buf,
948 sizeof(dco->notif_buf), NULL, &dco->ov))
949 {
950 DWORD err = GetLastError();
951 if (err == ERROR_IO_PENDING) /* operation queued? */
952 {
953 dco->iostate = IOSTATE_QUEUED;
954 dco->ov_ret = ERROR_SUCCESS;
955
956 msg(D_DCO_DEBUG, "%s: notify ioctl queued", __func__);
957 }
958 else
959 {
960 /* error occured */
961 ASSERT(SetEvent(dco->ov.hEvent));
962 dco->iostate = IOSTATE_IMMEDIATE_RETURN;
963 dco->ov_ret = err;
964
965 msg(D_DCO_DEBUG | M_ERRNO, "%s: notify ioctl error", __func__);
966 }
967 }
968 else
969 {
970 ASSERT(SetEvent(dco->ov.hEvent));
971 dco->iostate = IOSTATE_IMMEDIATE_RETURN;
972 dco->ov_ret = ERROR_SUCCESS;
973
974 msg(D_DCO_DEBUG, "%s: notify ioctl immediate return", __func__);
975 }
976 }
977}
978
979const char *
981{
982 /*
983 * this API can be called either from user mode or kernel mode,
984 * which enables us to probe driver's chachapoly support
985 * (available starting from Windows 11)
986 */
987
988 BCRYPT_ALG_HANDLE h;
989 NTSTATUS status = BCryptOpenAlgorithmProvider(&h, L"CHACHA20_POLY1305", NULL, 0);
990 if (BCRYPT_SUCCESS(status))
991 {
992 BCryptCloseAlgorithmProvider(h, 0);
993 return "AES-128-GCM:AES-256-GCM:AES-192-GCM:CHACHA20-POLY1305";
994 }
995 else
996 {
997 return "AES-128-GCM:AES-256-GCM:AES-192-GCM";
998 }
999}
1000
1001bool
1003{
1004 OVPN_VERSION ver = { 0 };
1005 return dco_get_version(&ver) && ver.Major >= 2;
1006}
1007
1008void
1009dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits,
1010 unsigned int peer_id)
1011{
1012 struct gc_arena gc = gc_new();
1013
1015 .Addr.Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = peer_id, .IPv6 = 0
1016 };
1017
1018 msg(D_DCO_DEBUG, "%s: %s/%d -> peer %d", __func__, print_in_addr_t(dst, IA_NET_ORDER, &gc),
1019 netbits, peer_id);
1020
1021 DWORD bytes_returned = 0;
1022 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route, sizeof(route), NULL, 0,
1023 &bytes_returned, NULL))
1024 {
1025 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE) failed");
1026 }
1027
1028 gc_free(&gc);
1029}
1030
1031void
1032dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits,
1033 unsigned int peer_id)
1034{
1035 struct gc_arena gc = gc_new();
1036
1037 OVPN_MP_IROUTE route = { .Addr.Addr6 = dst, .Netbits = netbits, .PeerId = peer_id, .IPv6 = 1 };
1038
1039 msg(D_DCO_DEBUG, "%s: %s/%d -> peer %d", __func__, print_in6_addr(dst, IA_NET_ORDER, &gc),
1040 netbits, peer_id);
1041
1042 DWORD bytes_returned = 0;
1043 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route, sizeof(route), NULL, 0,
1044 &bytes_returned, NULL))
1045 {
1046 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE) failed");
1047 }
1048
1049 gc_free(&gc);
1050}
1051
1052void
1053dco_win_del_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits)
1054{
1055 struct gc_arena gc = gc_new();
1056
1058 .Addr.Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = -1, .IPv6 = 0
1059 };
1060
1061 msg(D_DCO_DEBUG, "%s: %s/%d", __func__, print_in_addr_t(dst, IA_NET_ORDER, &gc), netbits);
1062
1063 DWORD bytes_returned = 0;
1064 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_DEL_IROUTE, &route, sizeof(route), NULL, 0,
1065 &bytes_returned, NULL))
1066 {
1067 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_DEL_IROUTE) failed");
1068 }
1069
1070 gc_free(&gc);
1071}
1072
1073void
1074dco_win_del_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits)
1075{
1076 struct gc_arena gc = gc_new();
1077
1078 OVPN_MP_IROUTE route = { .Addr.Addr6 = dst, .Netbits = netbits, .PeerId = -1, .IPv6 = 1 };
1079
1080 msg(D_DCO_DEBUG, "%s: %s/%d", __func__, print_in6_addr(dst, IA_NET_ORDER, &gc), netbits);
1081
1082 DWORD bytes_returned = 0;
1083 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_DEL_IROUTE, &route, sizeof(route), NULL, 0,
1084 &bytes_returned, NULL))
1085 {
1086 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_DEL_IROUTE) failed");
1087 }
1088
1089 gc_free(&gc);
1090}
1091
1092bool
1094{
1095 OVPN_VERSION ver = { 0 };
1096 return dco_get_version(&ver) && ((ver.Major == 2 && ver.Minor >= 8) || (ver.Major > 2));
1097}
1098
1099#endif /* defined(_WIN32) */
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:336
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
#define BSTR(buf)
Definition buffer.h:128
static void gc_free(struct gc_arena *a)
Definition buffer.h:1025
static struct gc_arena gc_new(void)
Definition buffer.h:1017
Data Channel Cryptography Module.
int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
void * dco_context_t
Definition dco.h:258
int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
Definition dco_win.c:215
int dco_del_key(dco_context_t *dco, unsigned int peerid, dco_key_slot_t slot)
Definition dco_win.c:584
bool dco_supports_epoch_data(struct context *c)
Definition dco_win.c:1093
bool dco_available(msglvl_t msglevel)
Definition dco_win.c:618
void dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
Initializes and binds the kernel UDP transport socket for multipeer mode.
Definition dco_win.c:283
int dco_del_peer(dco_context_t *dco, unsigned int peerid)
Definition dco_win.c:469
int dco_new_peer(dco_context_t *dco, unsigned int peerid, socket_descriptor_t sd, struct sockaddr *localaddr, struct sockaddr *remoteaddr, struct in_addr *vpn_ipv4, struct in6_addr *vpn_ipv6)
Definition dco_win.c:418
void dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits, unsigned int peer_id)
Definition dco_win.c:1032
const char * dco_version_string(struct gc_arena *gc)
Definition dco_win.c:645
void dco_p2p_start_vpn(struct tuntap *tt)
Transitions the DCO adapter to the connected state in P2P mode.
Definition dco_win.c:165
const char * dco_get_supported_ciphers(void)
Definition dco_win.c:980
bool ovpn_dco_init(struct context *c)
Initializes DCO depends on mode
Definition dco_win.c:190
static void dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, struct signal_info *sig_info)
Definition dco_win.c:222
void dco_win_del_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits)
Definition dco_win.c:1053
bool dco_win_supports_multipeer(void)
Definition dco_win.c:1002
int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
Definition dco_win.c:734
void dco_win_del_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits)
Definition dco_win.c:1074
int dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on_err)
Definition dco_win.c:861
static void dco_wait_ready(DWORD idx)
Definition dco_win.c:49
int dco_set_peer(dco_context_t *dco, unsigned int peerid, int keepalive_interval, int keepalive_timeout, int mss)
Definition dco_win.c:495
int dco_swap_keys(dco_context_t *dco, unsigned int peer_id)
Definition dco_win.c:592
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.
Definition dco_win.c:122
int dco_do_read(dco_context_t *dco)
Definition dco_win.c:693
int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
Definition dco_win.c:890
static void dco_handle_overlapped_success(dco_context_t *dco, bool queued)
Handles successful completion of overlapped operation.
Definition dco_win.c:672
static bool dco_get_version(OVPN_VERSION *version)
Gets version of dco-win driver.
Definition dco_win.c:73
void dco_p2p_new_peer(HANDLE handle, OVERLAPPED *ov, struct link_socket *sock, struct signal_info *sig_info)
Definition dco_win.c:327
void dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits, unsigned int peer_id)
Definition dco_win.c:1009
void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition dco_win.c:932
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)
Definition dco_win.c:529
#define D_DCO
Definition errlevel.h:93
#define D_DCO_DEBUG
Definition errlevel.h:117
#define EVENT_READ
Definition event.h:37
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:182
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:501
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS status
Definition interactive.c:51
@ route
Definition interactive.c:85
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4140
Header file for server-mode related structures and functions.
void * openvpn_net_ctx_t
Definition networking.h:38
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define M_NONFATAL
Definition error.h:91
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
#define MODE_POINT_TO_POINT
Definition options.h:263
#define MODE_SERVER
Definition options.h:264
#define OVPN_SET_PEER
#define OVPN_IOCTL_GET_STATS
#define OVPN_IOCTL_NEW_KEY_V2
#define OVPN_IOCTL_GET_VERSION
@ OVPN_PROTO_UDP
@ OVPN_PROTO_TCP
#define OVPN_IOCTL_SWAP_KEYS
#define OVPN_IOCTL_NEW_KEY
#define OVPN_IOCTL_NOTIFY_EVENT
#define OVPN_IOCTL_MP_DEL_IROUTE
OVPN_MODE
@ OVPN_MODE_MP
#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.
Definition sig.c:228
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:109
#define SF_PREPEND_SA
Definition socket.h:197
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)
#define IA_NET_ORDER
Definition socket_util.h:90
Control Channel Common Data Structures.
OVPN_CRYPTO_DATA V1
OVPN_KEY_SLOT KeySlot
unsigned char KeyId
OVPN_KEY_DIRECTION Decrypt
OVPN_KEY_DIRECTION Encrypt
OVPN_CIPHER_ALG CipherAlg
unsigned char Key[32]
unsigned char NonceTail[8]
unsigned char KeyLen
union _OVPN_MP_IROUTE::@20 Addr
union _OVPN_MP_NEW_PEER::@17 Local
SOCKADDR_IN6 Addr6
SOCKADDR_IN Addr4
union _OVPN_MP_NEW_PEER::@18 Remote
SOCKADDR_IN6 Addr6
union _OVPN_MP_START_VPN::@19 ListenAddress
SOCKADDR_IN6 Addr6
union _OVPN_NEW_PEER::@15 Local
SOCKADDR_IN Addr4
union _OVPN_NEW_PEER::@16 Remote
OVPN_PROTO Proto
LONG64 TunBytesSent
LONG64 TransportBytesSent
LONG64 TunBytesReceived
LONG64 TransportBytesReceived
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:172
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:224
counter_type dco_read_bytes
Definition openvpn.h:267
counter_type tun_read_bytes
Definition openvpn.h:264
counter_type dco_write_bytes
Definition openvpn.h:270
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
counter_type tun_write_bytes
Definition openvpn.h:265
Contains all state information for one tunnel.
Definition openvpn.h:474
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:487
struct context_2 c2
Level 2 context.
Definition openvpn.h:517
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:475
struct context_1 c1
Level 1 context.
Definition openvpn.h:516
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:143
const char * dev_node
Definition options.h:323
volatile int signal_received
Definition sig.h:42
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition ssl_common.h:724
Definition tun.h:183
DWORD adapter_index
Definition tun.h:234
HANDLE hand
Definition tun.h:218
dco_context_t dco
Definition tun.h:249
#define SOCKET_PRINTF
Definition syshead.h:439
SOCKET socket_descriptor_t
Definition syshead.h:440
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:131
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
Definition tun.c:6044
static bool tuntap_defined(const struct tuntap *tt)
Definition tun.h:254
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:207
#define IOSTATE_INITIAL
Definition win32.h:205
#define IOSTATE_QUEUED
Definition win32.h:206