OpenVPN
ring_buffer.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9 * 2019 Lev Stipakov <lev@openvpn.net>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#ifdef _WIN32
26#ifndef OPENVPN_RING_BUFFER_H
27#define OPENVPN_RING_BUFFER_H
28
29#include <windows.h>
30#include <winioctl.h>
31
32#include <stdint.h>
33#include <stdbool.h>
34
35/*
36 * Values below are taken from Wireguard Windows client
37 * https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/ring_windows.go#L14
38 */
39#define WINTUN_RING_CAPACITY 0x800000
40#define WINTUN_RING_TRAILING_BYTES 0x10000
41#define WINTUN_MAX_PACKET_SIZE 0xffff
42#define WINTUN_PACKET_ALIGN 4
43
44#define TUN_IOCTL_REGISTER_RINGS \
45 CTL_CODE(51820U, 0x970U, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
46
52{
53 volatile ULONG head;
54 volatile ULONG tail;
55 volatile LONG alertable;
57};
58
64{
65 struct
66 {
67 ULONG ring_size;
68 struct tun_ring *ring;
69 HANDLE tail_moved;
71};
72
74{
75 uint32_t size;
76};
77
79{
80 uint32_t size;
82};
83
99static inline bool
100register_ring_buffers(HANDLE device, struct tun_ring *send_ring, struct tun_ring *receive_ring,
101 HANDLE send_tail_moved, HANDLE receive_tail_moved)
102{
103 struct tun_register_rings rr;
104 BOOL res;
105 DWORD bytes_returned;
106
107 ZeroMemory(&rr, sizeof(rr));
108
109 rr.send.ring = send_ring;
110 rr.send.ring_size = sizeof(struct tun_ring);
111 rr.send.tail_moved = send_tail_moved;
112
113 rr.receive.ring = receive_ring;
114 rr.receive.ring_size = sizeof(struct tun_ring);
115 rr.receive.tail_moved = receive_tail_moved;
116
117 res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr), NULL, 0,
118 &bytes_returned, NULL);
119
120 return res != FALSE;
121}
122
123#endif /* ifndef OPENVPN_RING_BUFFER_H */
124#endif /* ifdef _WIN32 */
#define WINTUN_MAX_PACKET_SIZE
Definition ring_buffer.h:41
#define WINTUN_RING_TRAILING_BYTES
Definition ring_buffer.h:40
#define WINTUN_RING_CAPACITY
Definition ring_buffer.h:39
#define TUN_IOCTL_REGISTER_RINGS
Definition ring_buffer.h:44
static bool register_ring_buffers(HANDLE device, struct tun_ring *send_ring, struct tun_ring *receive_ring, HANDLE send_tail_moved, HANDLE receive_tail_moved)
Registers ring buffers used to exchange data between userspace openvpn process and wintun kernel driv...
UCHAR data[WINTUN_MAX_PACKET_SIZE]
Definition ring_buffer.h:81
uint32_t size
Definition ring_buffer.h:80
Struct for ring buffers registration See https://github.com/WireGuard/wintun#registering-rings.
Definition ring_buffer.h:64
struct tun_register_rings::@24 receive
struct tun_register_rings::@24 send
struct tun_ring * ring
Definition ring_buffer.h:68
Wintun ring buffer See https://github.com/WireGuard/wintun#ring-layout.
Definition ring_buffer.h:52
UCHAR data[WINTUN_RING_CAPACITY+WINTUN_RING_TRAILING_BYTES]
Definition ring_buffer.h:56
volatile LONG alertable
Definition ring_buffer.h:55
volatile ULONG head
Definition ring_buffer.h:53
volatile ULONG tail
Definition ring_buffer.h:54
char ** res