OpenVPN
forward.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/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 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24
31#ifndef FORWARD_H
32#define FORWARD_H
33
34/* the following macros must be defined before including any other header
35 * file
36 */
37
38#define TUN_OUT(c) (BLEN(&(c)->c2.to_tun) > 0)
39#define LINK_OUT(c) (BLEN(&(c)->c2.to_link) > 0)
40#define ANY_OUT(c) (TUN_OUT(c) || LINK_OUT(c))
41
42#ifdef ENABLE_FRAGMENT
43#define TO_LINK_FRAG(c) ((c)->c2.fragment && fragment_outgoing_defined((c)->c2.fragment))
44#else
45#define TO_LINK_FRAG(c) (false)
46#endif
47
48#define TO_LINK_DEF(c) (LINK_OUT(c) || TO_LINK_FRAG(c))
49
50#include "openvpn.h"
51#include "occ.h"
52#include "ping.h"
53#include "multi_io.h"
54
55#define IOW_TO_TUN (1<<0)
56#define IOW_TO_LINK (1<<1)
57#define IOW_READ_TUN (1<<2)
58#define IOW_READ_LINK (1<<3)
59#define IOW_SHAPER (1<<4)
60#define IOW_CHECK_RESIDUAL (1<<5)
61#define IOW_FRAG (1<<6)
62#define IOW_MBUF (1<<7)
63#define IOW_READ_TUN_FORCE (1<<8)
64#define IOW_WAIT_SIGNAL (1<<9)
65
66#define IOW_READ (IOW_READ_TUN|IOW_READ_LINK)
67
69
71
72void get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags);
73
74void get_io_flags_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags);
75
76void io_wait_dowork(struct context *c, const unsigned int flags);
77
78void pre_select(struct context *c);
79
80void process_io(struct context *c, struct link_socket *sock);
81
82
83/**********************************************************************/
116void encrypt_sign(struct context *c, bool comp_frag);
117
118int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout);
119
120/**********************************************************************/
141void read_incoming_link(struct context *c, struct link_socket *sock);
142
169bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, bool floated);
170
196void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, const uint8_t *orig_buf);
197
211void process_outgoing_link(struct context *c, struct link_socket *sock);
212
213
214/**************************************************************************/
227void read_incoming_tun(struct context *c);
228
229
244void process_incoming_tun(struct context *c, struct link_socket *out_sock);
245
246
260void process_outgoing_tun(struct context *c, struct link_socket *in_sock);
261
262
263/**************************************************************************/
264
265/*
266 * Send a string to remote over the TLS control channel.
267 * Used for push/pull messages, passing username/password,
268 * etc.
269 * @param c - The context structure of the VPN tunnel associated with
270 * the packet.
271 * @param str - The message to be sent
272 * @param msglevel - Message level to use for logging
273 */
274bool
275send_control_channel_string(struct context *c, const char *str, int msglevel);
276
277/*
278 * Send a string to remote over the TLS control channel.
279 * Used for push/pull messages, auth pending and other clear text
280 * control messages.
281 *
282 * This variant does not schedule the actual sending of the message
283 * The caller needs to ensure that it is scheduled or call
284 * send_control_channel_string
285 *
286 * @param session - The session structure of the VPN tunnel associated
287 * with the packet. The method will always use the
288 * primary key (KS_PRIMARY) for sending the message
289 * @param str - The message to be sent
290 * @param msglevel - Message level to use for logging
291 */
292
293bool
295 const char *str, int msglevel);
296
297
304void reschedule_multi_process(struct context *c);
305
306#define PIPV4_PASSTOS (1<<0)
307#define PIP_MSSFIX (1<<1) /* v4 and v6 */
308#define PIP_OUTGOING (1<<2)
309#define PIPV4_EXTRACT_DHCP_ROUTER (1<<3)
310#define PIPV4_CLIENT_NAT (1<<4)
311#define PIPV6_ICMP_NOHOST_CLIENT (1<<5)
312#define PIPV6_ICMP_NOHOST_SERVER (1<<6)
313
314
315void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf,
316 struct link_socket *sock);
317
318bool schedule_exit(struct context *c);
319
320static inline struct link_socket_info *
322{
323 if (c->c2.link_socket_infos)
324 {
325 return c->c2.link_socket_infos[0];
326 }
327 else
328 {
329 return &c->c2.link_sockets[0]->info;
330 }
331}
332
333static inline void
334register_activity(struct context *c, const int size)
335{
337 {
338 c->c2.inactivity_bytes += size;
340 {
341 c->c2.inactivity_bytes = 0;
343 }
344 }
345}
346
347/*
348 * Return the io_wait() flags appropriate for
349 * a point-to-point tunnel.
350 */
351static inline unsigned int
352p2p_iow_flags(const struct context *c)
353{
355 if (c->c2.to_link.len > 0)
356 {
357 flags |= IOW_TO_LINK;
358 }
359 if (c->c2.to_tun.len > 0)
360 {
361 flags |= IOW_TO_TUN;
362 }
363#ifdef _WIN32
365 {
366 flags &= ~IOW_READ_TUN;
367 }
368#endif
369 return flags;
370}
371
372/*
373 * This is the core I/O wait function, used for all I/O waits except
374 * for the top-level server sockets.
375 */
376static inline void
377io_wait(struct context *c, const unsigned int flags)
378{
380 && c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
381 {
382 /* fast path -- only for TUN/TAP/UDP writes */
383 unsigned int ret = 0;
384 if (flags & IOW_TO_TUN)
385 {
386 ret |= TUN_WRITE;
387 }
388 if (flags & (IOW_TO_LINK|IOW_MBUF))
389 {
390 ret |= SOCKET_WRITE;
391 }
392 c->c2.event_set_status = ret;
393 }
394 else
395 {
396#ifdef _WIN32
397 bool skip_iowait = flags & IOW_TO_TUN;
398 if (flags & IOW_READ_TUN)
399 {
400 /*
401 * don't read from tun if we have pending write to link,
402 * since every tun read overwrites to_link buffer filled
403 * by previous tun read
404 */
405 skip_iowait = !(flags & IOW_TO_LINK);
406 }
407 if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
408 {
409 unsigned int ret = 0;
410 if (flags & IOW_TO_TUN)
411 {
412 ret |= TUN_WRITE;
413 }
414 if (flags & IOW_READ_TUN)
415 {
416 ret |= TUN_READ;
417 }
418 c->c2.event_set_status = ret;
419 }
420 else
421#endif /* ifdef _WIN32 */
422 {
423 /* slow path */
424 io_wait_dowork(c, flags);
425 }
426 }
427}
428
429static inline bool
431{
432 if (c->c2.tls_multi)
433 {
435 }
436 else
437 {
439 }
440}
441
442#endif /* FORWARD_H */
uint64_t counter_type
Definition common.h:30
#define TUN_WRITE
Definition event.h:66
#define SOCKET_WRITE
Definition event.h:63
#define TUN_READ
Definition event.h:65
static unsigned int p2p_iow_flags(const struct context *c)
Definition forward.h:352
counter_type link_write_bytes_global
Definition forward.c:51
void reschedule_multi_process(struct context *c)
Reschedule tls_multi_process.
Definition forward.c:403
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:509
#define IOW_WAIT_SIGNAL
Definition forward.h:64
#define IOW_READ_TUN
Definition forward.h:57
bool send_control_channel_string_dowork(struct tls_session *session, const char *str, int msglevel)
Definition forward.c:381
void process_io(struct context *c, struct link_socket *sock)
Definition forward.c:2387
#define IOW_SHAPER
Definition forward.h:59
#define IOW_FRAG
Definition forward.h:61
bool schedule_exit(struct context *c)
Definition forward.c:533
#define IOW_MBUF
Definition forward.h:62
bool send_control_channel_string(struct context *c, const char *str, int msglevel)
Definition forward.c:410
static void io_wait(struct context *c, const unsigned int flags)
Definition forward.h:377
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition forward.h:321
void get_io_flags_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
Definition forward.c:2201
void pre_select(struct context *c)
Definition forward.c:1997
#define IOW_CHECK_RESIDUAL
Definition forward.h:60
static bool connection_established(struct context *c)
Definition forward.h:430
static void register_activity(struct context *c, const int size)
Definition forward.h:334
void get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
Definition forward.c:2192
#define IOW_TO_TUN
Definition forward.h:55
counter_type link_read_bytes_global
Definition forward.c:50
void io_wait_dowork(struct context *c, const unsigned int flags)
Definition forward.c:2254
void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf, struct link_socket *sock)
Definition forward.c:1658
#define IOW_READ
Definition forward.h:66
#define IOW_TO_LINK
Definition forward.h:56
void encrypt_sign(struct context *c, bool comp_frag)
Process a data channel packet that will be sent through a VPN tunnel.
Definition forward.c:625
bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, bool floated)
Starts processing a packet read from the external network interface.
Definition forward.c:1000
void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, const uint8_t *orig_buf)
Continues processing a packet read from the external network interface.
Definition forward.c:1150
void process_outgoing_link(struct context *c, struct link_socket *sock)
Write a packet to the external network interface.
Definition forward.c:1744
void read_incoming_link(struct context *c, struct link_socket *sock)
Read a packet from the external network interface.
Definition forward.c:934
void read_incoming_tun(struct context *c)
Read a packet from the virtual tun/tap network interface.
Definition forward.c:1299
void process_incoming_tun(struct context *c, struct link_socket *out_sock)
Process a packet read from the virtual tun/tap network interface.
Definition forward.c:1464
void process_outgoing_tun(struct context *c, struct link_socket *in_sock)
Write a packet to the virtual tun/tap network interface.
Definition forward.c:1901
static void event_timeout_reset(struct event_timeout *et)
Resets a timer.
Definition interval.h:189
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
Definition socket.h:597
@ CAS_WAITING_OPTIONS_IMPORT
client with pull or p2p waiting for first time options import
Definition ssl_common.h:574
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:171
unsigned int event_set_status
Definition openvpn.h:235
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
struct buffer to_link
Definition openvpn.h:377
int64_t inactivity_bytes
Definition openvpn.h:288
struct buffer to_tun
Definition openvpn.h:376
struct link_socket ** link_sockets
Definition openvpn.h:237
struct link_socket_info ** link_socket_infos
Definition openvpn.h:238
bool fast_io
Definition openvpn.h:424
struct event_timeout inactivity_interval
Definition openvpn.h:287
Contains all state information for one tunnel.
Definition openvpn.h:474
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
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:513
int64_t inactivity_minimum_bytes
Definition options.h:344
int inactivity_timeout
Definition options.h:343
enum multi_status multi_state
Definition ssl_common.h:618
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:480
static bool tuntap_is_wintun(struct tuntap *tt)
Definition tun.h:265
static bool tuntap_ring_empty(struct tuntap *tt)
Definition tun.h:271