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-2023 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 
54 #define IOW_TO_TUN (1<<0)
55 #define IOW_TO_LINK (1<<1)
56 #define IOW_READ_TUN (1<<2)
57 #define IOW_READ_LINK (1<<3)
58 #define IOW_SHAPER (1<<4)
59 #define IOW_CHECK_RESIDUAL (1<<5)
60 #define IOW_FRAG (1<<6)
61 #define IOW_MBUF (1<<7)
62 #define IOW_READ_TUN_FORCE (1<<8)
63 #define IOW_WAIT_SIGNAL (1<<9)
64 
65 #define IOW_READ (IOW_READ_TUN|IOW_READ_LINK)
66 
68 
70 
71 void io_wait_dowork(struct context *c, const unsigned int flags);
72 
73 void pre_select(struct context *c);
74 
75 void process_io(struct context *c);
76 
77 /**********************************************************************/
110 void encrypt_sign(struct context *c, bool comp_frag);
111 
112 int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout);
113 
114 /**********************************************************************/
134 void read_incoming_link(struct context *c);
135 
162 bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, bool floated);
163 
189 void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, const uint8_t *orig_buf);
190 
203 void process_outgoing_link(struct context *c);
204 
205 
206 /**************************************************************************/
219 void read_incoming_tun(struct context *c);
220 
221 
234 void process_incoming_tun(struct context *c);
235 
236 
249 void process_outgoing_tun(struct context *c);
250 
251 
252 /**************************************************************************/
253 
254 /*
255  * Send a string to remote over the TLS control channel.
256  * Used for push/pull messages, passing username/password,
257  * etc.
258  * @param c - The context structure of the VPN tunnel associated with
259  * the packet.
260  * @param str - The message to be sent
261  * @param msglevel - Message level to use for logging
262  */
263 bool
264 send_control_channel_string(struct context *c, const char *str, int msglevel);
265 
266 /*
267  * Send a string to remote over the TLS control channel.
268  * Used for push/pull messages, auth pending and other clear text
269  * control messages.
270  *
271  * This variant does not schedule the actual sending of the message
272  * The caller needs to ensure that it is scheduled or call
273  * send_control_channel_string
274  *
275  * @param session - The session structure of the VPN tunnel associated
276  * with the packet. The method will always use the
277  * primary key (KS_PRIMARY) for sending the message
278  * @param str - The message to be sent
279  * @param msglevel - Message level to use for logging
280  */
281 
282 bool
284  const char *str, int msglevel);
285 
286 
293 void reschedule_multi_process(struct context *c);
294 
295 #define PIPV4_PASSTOS (1<<0)
296 #define PIP_MSSFIX (1<<1) /* v4 and v6 */
297 #define PIP_OUTGOING (1<<2)
298 #define PIPV4_EXTRACT_DHCP_ROUTER (1<<3)
299 #define PIPV4_CLIENT_NAT (1<<4)
300 #define PIPV6_IMCP_NOHOST_CLIENT (1<<5)
301 #define PIPV6_IMCP_NOHOST_SERVER (1<<6)
302 
303 void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf);
304 
305 void schedule_exit(struct context *c, const int n_seconds, const int signal);
306 
307 static inline struct link_socket_info *
309 {
310  if (c->c2.link_socket_info)
311  {
312  return c->c2.link_socket_info;
313  }
314  else
315  {
316  return &c->c2.link_socket->info;
317  }
318 }
319 
320 static inline void
321 register_activity(struct context *c, const int size)
322 {
324  {
325  c->c2.inactivity_bytes += size;
327  {
328  c->c2.inactivity_bytes = 0;
330  }
331  }
332 }
333 
334 /*
335  * Return the io_wait() flags appropriate for
336  * a point-to-point tunnel.
337  */
338 static inline unsigned int
339 p2p_iow_flags(const struct context *c)
340 {
342  if (c->c2.to_link.len > 0)
343  {
344  flags |= IOW_TO_LINK;
345  }
346  if (c->c2.to_tun.len > 0)
347  {
348  flags |= IOW_TO_TUN;
349  }
350 #ifdef _WIN32
351  if (tuntap_ring_empty(c->c1.tuntap))
352  {
353  flags &= ~IOW_READ_TUN;
354  }
355 #endif
356  return flags;
357 }
358 
359 /*
360  * This is the core I/O wait function, used for all I/O waits except
361  * for TCP in server mode.
362  */
363 static inline void
364 io_wait(struct context *c, const unsigned int flags)
365 {
366  if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
367  {
368  /* fast path -- only for TUN/TAP/UDP writes */
369  unsigned int ret = 0;
370  if (flags & IOW_TO_TUN)
371  {
372  ret |= TUN_WRITE;
373  }
374  if (flags & (IOW_TO_LINK|IOW_MBUF))
375  {
376  ret |= SOCKET_WRITE;
377  }
378  c->c2.event_set_status = ret;
379  }
380  else
381  {
382 #ifdef _WIN32
383  bool skip_iowait = flags & IOW_TO_TUN;
384  if (flags & IOW_READ_TUN)
385  {
386  /*
387  * don't read from tun if we have pending write to link,
388  * since every tun read overwrites to_link buffer filled
389  * by previous tun read
390  */
391  skip_iowait = !(flags & IOW_TO_LINK);
392  }
393  if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
394  {
395  unsigned int ret = 0;
396  if (flags & IOW_TO_TUN)
397  {
398  ret |= TUN_WRITE;
399  }
400  if (flags & IOW_READ_TUN)
401  {
402  ret |= TUN_READ;
403  }
404  c->c2.event_set_status = ret;
405  }
406  else
407 #endif /* ifdef _WIN32 */
408  {
409  /* slow path */
410  io_wait_dowork(c, flags);
411  }
412  }
413 }
414 
415 static inline bool
417 {
418  if (c->c2.tls_multi)
419  {
421  }
422  else
423  {
425  }
426 }
427 
428 #endif /* FORWARD_H */
context_2::event_set_status
unsigned int event_set_status
Definition: openvpn.h:238
TUN_READ
#define TUN_READ
Definition: event.h:65
process_incoming_link_part2
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:1126
context_2::to_link
struct buffer to_link
Definition: openvpn.h:380
context_2::tls_multi
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition: openvpn.h:326
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
context_1::tuntap
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition: openvpn.h:170
tuntap_is_wintun
static bool tuntap_is_wintun(struct tuntap *tt)
Definition: tun.h:249
options::inactivity_minimum_bytes
int64_t inactivity_minimum_bytes
Definition: options.h:331
context
Contains all state information for one tunnel.
Definition: openvpn.h:476
link_write_bytes_global
counter_type link_write_bytes_global
Definition: forward.c:50
register_activity
static void register_activity(struct context *c, const int size)
Definition: forward.h:321
openvpn.h
context_2::inactivity_interval
struct event_timeout inactivity_interval
Definition: openvpn.h:290
context_2::link_socket_info
struct link_socket_info * link_socket_info
This variable is used instead link_socket->info for P2MP UDP childs.
Definition: openvpn.h:244
IOW_MBUF
#define IOW_MBUF
Definition: forward.h:61
IOW_SHAPER
#define IOW_SHAPER
Definition: forward.h:58
process_outgoing_tun
void process_outgoing_tun(struct context *c)
Write a packet to the virtual tun/tap network interface.
Definition: forward.c:1878
tls_multi::multi_state
enum multi_status multi_state
Definition: ssl_common.h:609
IOW_TO_LINK
#define IOW_TO_LINK
Definition: forward.h:55
context::c2
struct context_2 c2
Level 2 context.
Definition: openvpn.h:517
pre_select
void pre_select(struct context *c)
Definition: forward.c:1968
counter_type
uint64_t counter_type
Definition: common.h:30
ping.h
encrypt_sign
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:604
process_io
void process_io(struct context *c)
Definition: forward.c:2256
io_wait
static void io_wait(struct context *c, const unsigned int flags)
Definition: forward.h:364
IOW_TO_TUN
#define IOW_TO_TUN
Definition: forward.h:54
process_ip_header
void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
Definition: forward.c:1625
io_wait_dowork
void io_wait_dowork(struct context *c, const unsigned int flags)
Definition: forward.c:2040
context::options
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:478
IOW_FRAG
#define IOW_FRAG
Definition: forward.h:60
read_incoming_tun
void read_incoming_tun(struct context *c)
Read a packet from the virtual tun/tap network interface.
Definition: forward.c:1273
SOCKET_WRITE
#define SOCKET_WRITE
Definition: event.h:63
reschedule_multi_process
void reschedule_multi_process(struct context *c)
Reschedule tls_multi_process.
Definition: forward.c:389
send_control_channel_string_dowork
bool send_control_channel_string_dowork(struct tls_session *session, const char *str, int msglevel)
Definition: forward.c:367
tuntap_ring_empty
static bool tuntap_ring_empty(struct tuntap *tt)
Definition: tun.h:255
TUN_WRITE
#define TUN_WRITE
Definition: event.h:66
get_link_socket_info
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition: forward.h:308
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
IOW_WAIT_SIGNAL
#define IOW_WAIT_SIGNAL
Definition: forward.h:63
tls_session
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:468
context_2::link_socket
struct link_socket * link_socket
Definition: openvpn.h:240
occ.h
get_server_poll_remaining_time
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition: forward.c:496
context_2::inactivity_bytes
int64_t inactivity_bytes
Definition: openvpn.h:291
IOW_CHECK_RESIDUAL
#define IOW_CHECK_RESIDUAL
Definition: forward.h:59
context_2::to_tun
struct buffer to_tun
Definition: openvpn.h:379
CAS_WAITING_OPTIONS_IMPORT
@ CAS_WAITING_OPTIONS_IMPORT
client with pull or p2p waiting for first time options import
Definition: ssl_common.h:565
read_incoming_link
void read_incoming_link(struct context *c)
Read a packet from the external network interface.
Definition: forward.c:912
link_read_bytes_global
counter_type link_read_bytes_global
Definition: forward.c:49
IOW_READ_TUN
#define IOW_READ_TUN
Definition: forward.h:56
p2p_iow_flags
static unsigned int p2p_iow_flags(const struct context *c)
Definition: forward.h:339
event_timeout
Definition: interval.h:136
schedule_exit
void schedule_exit(struct context *c, const int n_seconds, const int signal)
Definition: forward.c:520
send_control_channel_string
bool send_control_channel_string(struct context *c, const char *str, int msglevel)
Definition: forward.c:396
session
Definition: keyingmaterialexporter.c:56
process_incoming_link_part1
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:978
process_incoming_tun
void process_incoming_tun(struct context *c)
Process a packet read from the virtual tun/tap network interface.
Definition: forward.c:1431
options::inactivity_timeout
int inactivity_timeout
Definition: options.h:330
event_timeout_reset
static void event_timeout_reset(struct event_timeout *et)
Resets a timer.
Definition: interval.h:189
IOW_READ
#define IOW_READ
Definition: forward.h:65
process_outgoing_link
void process_outgoing_link(struct context *c)
Write a packet to the external network interface.
Definition: forward.c:1723
context::c1
struct context_1 c1
Level 1 context.
Definition: openvpn.h:516
connection_established
static bool connection_established(struct context *c)
Definition: forward.h:416
context_2::fast_io
bool fast_io
Definition: openvpn.h:427