OpenVPN
forward-inline.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-2017 OpenVPN Technologies, 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 #ifndef FORWARD_INLINE_H
25 #define FORWARD_INLINE_H
26 
27 /*
28  * Inline functions
29  */
30 
31 /*
32  * Does TLS session need service?
33  */
34 static inline void
35 check_tls(struct context *c)
36 {
37 #if defined(ENABLE_CRYPTO)
38  void check_tls_dowork(struct context *c);
39 
40  if (c->c2.tls_multi)
41  {
43  }
44 #endif
45 }
46 
47 /*
48  * TLS errors are fatal in TCP mode.
49  * Also check for --tls-exit trigger.
50  */
51 static inline void
53 {
54 #if defined(ENABLE_CRYPTO)
55  void check_tls_errors_co(struct context *c);
56 
57  void check_tls_errors_nco(struct context *c);
58 
59  if (c->c2.tls_multi && c->c2.tls_exit_signal)
60  {
62  {
63  if (c->c2.tls_multi->n_soft_errors)
64  {
66  }
67  }
68  else
69  {
70  if (c->c2.tls_multi->n_hard_errors)
71  {
73  }
74  }
75  }
76 #endif /* if defined(ENABLE_CRYPTO) */
77 }
78 
79 /*
80  * Check for possible incoming configuration
81  * messages on the control channel.
82  */
83 static inline void
85 {
86 #if P2MP
88 
89  if (tls_test_payload_len(c->c2.tls_multi) > 0)
90  {
92  }
93 #endif
94 }
95 
96 /*
97  * Options like --up-delay need to be triggered by this function which
98  * checks for connection establishment.
99  */
100 static inline void
102 {
104 
106  {
108  }
109 }
110 
111 /*
112  * Should we add routes?
113  */
114 static inline void
116 {
117  void check_add_routes_dowork(struct context *c);
118 
120  {
122  }
123 }
124 
125 /*
126  * Should we exit due to inactivity timeout?
127  */
128 static inline void
130 {
132 
135  {
137  }
138 }
139 
140 #if P2MP
141 
142 static inline void
144 {
146 
147  if (c->options.ce.connect_timeout
149  {
151  }
152 }
153 
154 /*
155  * Scheduled exit?
156  */
157 static inline void
159 {
160  void check_scheduled_exit_dowork(struct context *c);
161 
163  {
165  {
167  }
168  }
169 }
170 #endif /* if P2MP */
171 
172 /*
173  * Should we write timer-triggered status file.
174  */
175 static inline void
177 {
178  void check_status_file_dowork(struct context *c);
179 
180  if (c->c1.status_output)
181  {
183  {
185  }
186  }
187 }
188 
189 #ifdef ENABLE_FRAGMENT
190 /*
191  * Should we deliver a datagram fragment to remote?
192  */
193 static inline void
195 {
196  void check_fragment_dowork(struct context *c);
197 
198  if (c->c2.fragment)
199  {
201  }
202 }
203 #endif
204 
205 #if P2MP
206 
207 /*
208  * see if we should send a push_request in response to --pull
209  */
210 static inline void
212 {
213  void check_push_request_dowork(struct context *c);
214 
216  {
218  }
219 }
220 
221 #endif
222 
223 #ifdef ENABLE_CRYPTO
224 /*
225  * Should we persist our anti-replay packet ID state to disk?
226  */
227 static inline void
229 {
232  {
234  }
235 }
236 #endif
237 
238 /*
239  * Set our wakeup to 0 seconds, so we will be rescheduled
240  * immediately.
241  */
242 static inline void
244 {
245  c->c2.timeval.tv_sec = 0; /* ZERO-TIMEOUT */
246  c->c2.timeval.tv_usec = 0;
247 }
248 
249 static inline void
250 context_reschedule_sec(struct context *c, int sec)
251 {
252  if (sec < 0)
253  {
254  sec = 0;
255  }
256  if (sec < c->c2.timeval.tv_sec)
257  {
258  c->c2.timeval.tv_sec = sec;
259  c->c2.timeval.tv_usec = 0;
260  }
261 }
262 
263 static inline struct link_socket_info *
265 {
266  if (c->c2.link_socket_info)
267  {
268  return c->c2.link_socket_info;
269  }
270  else
271  {
272  return &c->c2.link_socket->info;
273  }
274 }
275 
276 static inline void
277 register_activity(struct context *c, const int size)
278 {
280  {
281  c->c2.inactivity_bytes += size;
283  {
284  c->c2.inactivity_bytes = 0;
286  }
287  }
288 }
289 
290 /*
291  * Return the io_wait() flags appropriate for
292  * a point-to-point tunnel.
293  */
294 static inline unsigned int
295 p2p_iow_flags(const struct context *c)
296 {
298  if (c->c2.to_link.len > 0)
299  {
300  flags |= IOW_TO_LINK;
301  }
302  if (c->c2.to_tun.len > 0)
303  {
304  flags |= IOW_TO_TUN;
305  }
306  return flags;
307 }
308 
309 /*
310  * This is the core I/O wait function, used for all I/O waits except
311  * for TCP in server mode.
312  */
313 static inline void
314 io_wait(struct context *c, const unsigned int flags)
315 {
316  void io_wait_dowork(struct context *c, const unsigned int flags);
317 
318  if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
319  {
320  /* fast path -- only for TUN/TAP/UDP writes */
321  unsigned int ret = 0;
322  if (flags & IOW_TO_TUN)
323  {
324  ret |= TUN_WRITE;
325  }
326  if (flags & (IOW_TO_LINK|IOW_MBUF))
327  {
328  ret |= SOCKET_WRITE;
329  }
330  c->c2.event_set_status = ret;
331  }
332  else
333  {
334  /* slow path */
335  io_wait_dowork(c, flags);
336  }
337 }
338 
339 #define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)
340 
341 #endif /* EVENT_INLINE_H */
void io_wait_dowork(struct context *c, const unsigned int flags)
Definition: forward.c:1617
void check_server_poll_timeout_dowork(struct context *c)
Definition: forward.c:351
struct buffer to_link
Definition: openvpn.h:392
#define ETT_DEFAULT
Definition: interval.h:213
struct event_timeout route_wakeup
Definition: openvpn.h:398
void check_tls_dowork(struct context *c)
Definition: forward.c:92
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:510
Contains all state information for one tunnel.
Definition: openvpn.h:508
static void context_reschedule_sec(struct context *c, int sec)
bool status_trigger_tv(struct status_output *so, struct timeval *tv)
Definition: status.c:150
static int tls_test_payload_len(const struct tls_multi *multi)
Definition: ssl.h:547
struct event_timeout wait_for_connect
Definition: openvpn.h:301
int inactivity_minimum_bytes
Definition: options.h:252
struct link_socket_info * link_socket_info
Definition: openvpn.h:258
#define IOW_SHAPER
Definition: forward.h:54
static void check_packet_id_persist_flush(struct context *c)
void check_status_file_dowork(struct context *c)
Definition: forward.c:392
static void check_scheduled_exit(struct context *c)
#define IOW_TO_LINK
Definition: forward.h:51
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition: socket.h:606
static void check_incoming_control_channel(struct context *c)
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
struct context_1 c1
Level 1 context.
Definition: openvpn.h:546
#define IOW_TO_TUN
Definition: forward.h:50
struct event_timeout inactivity_interval
Definition: openvpn.h:306
static void event_timeout_reset(struct event_timeout *et)
Definition: interval.h:174
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
Definition: interval.c:45
int inactivity_bytes
Definition: openvpn.h:307
#define IOW_MBUF
Definition: forward.h:57
void check_add_routes_dowork(struct context *c)
Definition: forward.c:297
static bool event_timeout_defined(const struct event_timeout *et)
Definition: interval.h:144
int n_hard_errors
Definition: ssl_common.h:527
struct timeval timeval
Definition: openvpn.h:409
static void check_add_routes(struct context *c)
static void check_inactivity_timeout(struct context *c)
#define IOW_FRAG
Definition: forward.h:56
static void register_activity(struct context *c, const int size)
struct event_timeout scheduled_exit
Definition: openvpn.h:478
int n_soft_errors
Definition: ssl_common.h:528
struct link_socket * link_socket
Definition: openvpn.h:256
void check_fragment_dowork(struct context *c)
Definition: forward.c:405
static void check_status_file(struct context *c)
struct connection_entry ce
Definition: options.h:203
struct context_2 c2
Level 2 context.
Definition: openvpn.h:547
void check_push_request_dowork(struct context *c)
Definition: forward.c:192
#define IOW_WAIT_SIGNAL
Definition: forward.h:59
int inactivity_timeout
Definition: options.h:251
struct event_timeout packet_id_persist_interval
Definition: openvpn.h:368
struct event_timeout push_request_interval
Definition: openvpn.h:469
static void check_tls_errors(struct context *c)
void check_scheduled_exit_dowork(struct context *c)
Definition: forward.c:381
void check_incoming_control_channel_dowork(struct context *c)
Definition: forward.c:143
int connect_timeout
Definition: options.h:103
static void check_push_request(struct context *c)
static void check_fragment(struct context *c)
struct fragment_master * fragment
Definition: openvpn.h:270
#define IOW_CHECK_RESIDUAL
Definition: forward.h:55
void check_tls_errors_co(struct context *c)
Definition: forward.c:123
struct status_output * status_output
Definition: openvpn.h:184
struct packet_id_persist pid_persist
Definition: openvpn.h:169
struct event_timeout server_poll_interval
Definition: openvpn.h:421
static void context_immediate_reschedule(struct context *c)
#define TUN_WRITE
Definition: openvpn.h:243
static unsigned int p2p_iow_flags(const struct context *c)
bool fast_io
Definition: openvpn.h:439
#define SOCKET_WRITE
Definition: openvpn.h:241
void check_connection_established_dowork(struct context *c)
Definition: forward.c:206
static bool packet_id_persist_enabled(const struct packet_id_persist *p)
Definition: packet_id.h:283
static void check_connection_established(struct context *c)
void check_inactivity_timeout_dowork(struct context *c)
Definition: forward.c:335
void packet_id_persist_save(struct packet_id_persist *p)
Definition: packet_id.c:484
struct buffer to_tun
Definition: openvpn.h:391
void check_tls_errors_nco(struct context *c)
Definition: forward.c:130
static void check_tls(struct context *c)
static void io_wait(struct context *c, const unsigned int flags)
unsigned int event_set_status
Definition: openvpn.h:254
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition: openvpn.h:342
static void check_server_poll_timeout(struct context *c)
static struct link_socket_info * get_link_socket_info(struct context *c)
int tls_exit_signal
Definition: openvpn.h:360
#define IOW_READ
Definition: forward.h:61