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 if (dco->c->mode == CM_TOP)
731 {
733 }
734 else
735 {
737 }
738
739 return 0;
740}
741
742int
744{
745 struct gc_arena gc = gc_new();
746
747 int ret = 0;
748 struct tuntap *tt = dco->tt;
749
750 if (!tuntap_defined(tt))
751 {
752 ret = -1;
753 goto done;
754 }
755
757 .PeerId = -1
758 };
759
760 DWORD required_size = 0, bytes_returned = 0;
761 /* first, figure out buffer size */
762 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &required_size, sizeof(DWORD), &bytes_returned, NULL))
763 {
764 if (GetLastError() == ERROR_MORE_DATA)
765 {
766 if (bytes_returned != sizeof(DWORD))
767 {
768 msg(M_WARN, "%s: invalid bytes returned for size query (%lu, expected %zu)", __func__, bytes_returned, sizeof(DWORD));
769 ret = -1;
770 goto done;
771 }
772 /* required_size now contains the size written by the driver */
773 if (required_size == 0)
774 {
775 ret = 0; /* no peers to process */
776 goto done;
777 }
778 if (required_size < sizeof(OVPN_PEER_STATS))
779 {
780 msg(M_WARN, "%s: invalid required size %lu (minimum %zu)", __func__, required_size, sizeof(OVPN_PEER_STATS));
781 ret = -1;
782 goto done;
783 }
784 }
785 else
786 {
787 msg(M_WARN | M_ERRNO, "%s: failed to fetch required buffer size", __func__);
788 ret = -1;
789 goto done;
790 }
791 }
792 else
793 {
794 /* unexpected success? */
795 if (bytes_returned == 0)
796 {
797 ret = 0; /* no peers to process */
798 goto done;
799 }
800
801 msg(M_WARN, "%s: first DeviceIoControl call succeeded unexpectedly (%lu bytes returned)", __func__, bytes_returned);
802 ret = -1;
803 goto done;
804 }
805
806
807 /* allocate the buffer and fetch stats */
808 OVPN_PEER_STATS *peer_stats = gc_malloc(required_size, true, &gc);
809 if (!peer_stats)
810 {
811 msg(M_WARN, "%s: failed to allocate buffer of size %lu", __func__, required_size);
812 ret = -1;
813 goto done;
814 }
815
816 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), peer_stats, required_size, &bytes_returned, NULL))
817 {
818 /* unlikely case when a peer has been added since fetching buffer size, not an error! */
819 if (GetLastError() == ERROR_MORE_DATA)
820 {
821 msg(M_WARN, "%s: peer has been added, skip fetching stats", __func__);
822 ret = 0;
823 goto done;
824 }
825
826 msg(M_WARN | M_ERRNO, "%s: failed to fetch multipeer stats", __func__);
827 ret = -1;
828 goto done;
829 }
830
831 /* iterate over stats and update peers */
832 for (int i = 0; i < bytes_returned / sizeof(OVPN_PEER_STATS); ++i)
833 {
834 OVPN_PEER_STATS *stat = &peer_stats[i];
835
836 if (stat->PeerId >= dco->c->multi->max_clients)
837 {
838 msg(M_WARN, "%s: received out of bound peer_id %u (max=%u)", __func__, stat->PeerId,
839 dco->c->multi->max_clients);
840 continue;
841 }
842
843 struct multi_instance *mi = dco->c->multi->instances[stat->PeerId];
844 if (!mi)
845 {
846 msg(M_WARN, "%s: received data for a non-existing peer %u", __func__, stat->PeerId);
847 continue;
848 }
849
850 /* update peer stats */
851 struct context_2 *c2 = &mi->context.c2;
852 c2->dco_read_bytes = stat->LinkRxBytes;
853 c2->dco_write_bytes = stat->LinkTxBytes;
854 c2->tun_read_bytes = stat->VpnRxBytes;
855 c2->tun_write_bytes = stat->VpnTxBytes;
856 }
857
858done:
859 gc_free(&gc);
860
861 if (raise_sigusr1_on_err && ret < 0)
862 {
863 register_signal(dco->c->sig, SIGUSR1, "dco peer stats error");
864 }
865
866 return ret;
867}
868
869int
870dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on_err)
871{
872 struct tuntap *tt = c->c1.tuntap;
873
874 if (!tuntap_defined(tt))
875 {
876 return -1;
877 }
878
879 OVPN_STATS stats;
880 ZeroMemory(&stats, sizeof(OVPN_STATS));
881
882 DWORD bytes_returned = 0;
883 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_STATS, NULL, 0, &stats, sizeof(stats),
884 &bytes_returned, NULL))
885 {
886 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_GET_STATS) failed");
887 return -1;
888 }
889
894
895 return 0;
896}
897
898int
899dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
900{
901 struct tuntap *tt = c->c1.tuntap;
902
903 if (!tuntap_defined(tt))
904 {
905 return -1;
906 }
907
908 /* first, try a new ioctl */
910
911 OVPN_PEER_STATS peer_stats = { 0 };
912 DWORD bytes_returned = 0;
913 if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &peer_stats, sizeof(peer_stats),
914 &bytes_returned, NULL))
915 {
916 if (GetLastError() == ERROR_INVALID_FUNCTION)
917 {
918 /* are we using the old driver? */
919 return dco_get_peer_stats_fallback(c, raise_sigusr1_on_err);
920 }
921
922 msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) failed", __func__);
923 return -1;
924 }
925
926 if (bytes_returned != sizeof(OVPN_PEER_STATS))
927 {
928 msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) returned invalid size", __func__);
929 return -1;
930 }
931
932 c->c2.dco_read_bytes = peer_stats.LinkRxBytes;
933 c->c2.dco_write_bytes = peer_stats.LinkTxBytes;
934 c->c2.tun_read_bytes = peer_stats.VpnRxBytes;
935 c->c2.tun_write_bytes = peer_stats.VpnTxBytes;
936
937 return 0;
938}
939
940void
942{
943 if (dco->ifmode != DCO_MODE_MP)
944 {
945 /* mp only */
946 return;
947 }
948
949 event_ctl(es, &dco->rwhandle, EVENT_READ, arg);
950
951 if (dco->iostate == IOSTATE_INITIAL)
952 {
953 /* the overlapped IOCTL will signal this event on I/O completion */
954 ASSERT(ResetEvent(dco->ov.hEvent));
955
956 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_NOTIFY_EVENT, NULL, 0, &dco->notif_buf,
957 sizeof(dco->notif_buf), NULL, &dco->ov))
958 {
959 DWORD err = GetLastError();
960 if (err == ERROR_IO_PENDING) /* operation queued? */
961 {
962 dco->iostate = IOSTATE_QUEUED;
963 dco->ov_ret = ERROR_SUCCESS;
964
965 msg(D_DCO_DEBUG, "%s: notify ioctl queued", __func__);
966 }
967 else
968 {
969 /* error occured */
970 ASSERT(SetEvent(dco->ov.hEvent));
971 dco->iostate = IOSTATE_IMMEDIATE_RETURN;
972 dco->ov_ret = err;
973
974 msg(D_DCO_DEBUG | M_ERRNO, "%s: notify ioctl error", __func__);
975 }
976 }
977 else
978 {
979 ASSERT(SetEvent(dco->ov.hEvent));
980 dco->iostate = IOSTATE_IMMEDIATE_RETURN;
981 dco->ov_ret = ERROR_SUCCESS;
982
983 msg(D_DCO_DEBUG, "%s: notify ioctl immediate return", __func__);
984 }
985 }
986}
987
988const char *
990{
991 /*
992 * this API can be called either from user mode or kernel mode,
993 * which enables us to probe driver's chachapoly support
994 * (available starting from Windows 11)
995 */
996
997 BCRYPT_ALG_HANDLE h;
998 NTSTATUS status = BCryptOpenAlgorithmProvider(&h, L"CHACHA20_POLY1305", NULL, 0);
999 if (BCRYPT_SUCCESS(status))
1000 {
1001 BCryptCloseAlgorithmProvider(h, 0);
1002 return "AES-128-GCM:AES-256-GCM:AES-192-GCM:CHACHA20-POLY1305";
1003 }
1004 else
1005 {
1006 return "AES-128-GCM:AES-256-GCM:AES-192-GCM";
1007 }
1008}
1009
1010bool
1012{
1013 OVPN_VERSION ver = { 0 };
1014 return dco_get_version(&ver) && ver.Major >= 2;
1015}
1016
1017void
1019 unsigned int peer_id)
1020{
1021 struct gc_arena gc = gc_new();
1022
1024 .Addr.Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = peer_id, .IPv6 = 0
1025 };
1026
1027 msg(D_DCO_DEBUG, "%s: %s/%d -> peer %d", __func__, print_in_addr_t(dst, IA_NET_ORDER, &gc),
1028 netbits, peer_id);
1029
1030 DWORD bytes_returned = 0;
1031 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route, sizeof(route), NULL, 0,
1032 &bytes_returned, NULL))
1033 {
1034 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE) failed");
1035 }
1036
1037 gc_free(&gc);
1038}
1039
1040void
1041dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits,
1042 unsigned int peer_id)
1043{
1044 struct gc_arena gc = gc_new();
1045
1046 OVPN_MP_IROUTE route = { .Addr.Addr6 = dst, .Netbits = netbits, .PeerId = peer_id, .IPv6 = 1 };
1047
1048 msg(D_DCO_DEBUG, "%s: %s/%d -> peer %d", __func__, print_in6_addr(dst, IA_NET_ORDER, &gc),
1049 netbits, peer_id);
1050
1051 DWORD bytes_returned = 0;
1052 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route, sizeof(route), NULL, 0,
1053 &bytes_returned, NULL))
1054 {
1055 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE) failed");
1056 }
1057
1058 gc_free(&gc);
1059}
1060
1061void
1062dco_win_del_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int netbits)
1063{
1064 struct gc_arena gc = gc_new();
1065
1067 .Addr.Addr4.S_un.S_addr = dst, .Netbits = netbits, .PeerId = -1, .IPv6 = 0
1068 };
1069
1070 msg(D_DCO_DEBUG, "%s: %s/%d", __func__, print_in_addr_t(dst, IA_NET_ORDER, &gc), netbits);
1071
1072 DWORD bytes_returned = 0;
1073 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_DEL_IROUTE, &route, sizeof(route), NULL, 0,
1074 &bytes_returned, NULL))
1075 {
1076 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_DEL_IROUTE) failed");
1077 }
1078
1079 gc_free(&gc);
1080}
1081
1082void
1083dco_win_del_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits)
1084{
1085 struct gc_arena gc = gc_new();
1086
1087 OVPN_MP_IROUTE route = { .Addr.Addr6 = dst, .Netbits = netbits, .PeerId = -1, .IPv6 = 1 };
1088
1089 msg(D_DCO_DEBUG, "%s: %s/%d", __func__, print_in6_addr(dst, IA_NET_ORDER, &gc), netbits);
1090
1091 DWORD bytes_returned = 0;
1092 if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_DEL_IROUTE, &route, sizeof(route), NULL, 0,
1093 &bytes_returned, NULL))
1094 {
1095 msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_DEL_IROUTE) failed");
1096 }
1097
1098 gc_free(&gc);
1099}
1100
1101bool
1103{
1104 OVPN_VERSION ver = { 0 };
1105 return dco_get_version(&ver) && ((ver.Major == 2 && ver.Minor >= 8) || (ver.Major > 2));
1106}
1107
1108#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:259
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:1102
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:1041
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
int dco_read_and_process(dco_context_t *dco)
Definition dco_win.c:693
const char * dco_get_supported_ciphers(void)
Definition dco_win.c:989
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:1062
bool dco_win_supports_multipeer(void)
Definition dco_win.c:1011
int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
Definition dco_win.c:743
void dco_win_del_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int netbits)
Definition dco_win.c:1083
int dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on_err)
Definition dco_win.c:870
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_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
Definition dco_win.c:899
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:1018
void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition dco_win.c:941
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:180
void process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
Definition forward.c:1247
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:504
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 multi_process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
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 CM_TOP
Definition openvpn.h:483
#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:725
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:444
SOCKET socket_descriptor_t
Definition syshead.h:445
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
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:6035
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