summaryrefslogtreecommitdiff
path: root/app/openvpn/src/openvpn/forward-inline.h
blob: 7eb480ddd14001b52c840b6b281f91166682d04a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *  OpenVPN -- An application to securely tunnel IP networks
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 *             session authentication and key exchange,
 *             packet encryption, packet authentication, and
 *             packet compression.
 *
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (see the file COPYING included with this
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef FORWARD_INLINE_H
#define FORWARD_INLINE_H

/*
 * Inline functions
 */

/*
 * Does TLS session need service?
 */
static inline void
check_tls (struct context *c)
{
#if defined(ENABLE_CRYPTO) && defined(ENABLE_SSL)
  void check_tls_dowork (struct context *c);
  if (c->c2.tls_multi)
    check_tls_dowork (c);
#endif
}

/*
 * TLS errors are fatal in TCP mode.
 * Also check for --tls-exit trigger.
 */
static inline void
check_tls_errors (struct context *c)
{
#if defined(ENABLE_CRYPTO) && defined(ENABLE_SSL)
  void check_tls_errors_co (struct context *c);
  void check_tls_errors_nco (struct context *c);
  if (c->c2.tls_multi && c->c2.tls_exit_signal)
    {
      if (link_socket_connection_oriented (c->c2.link_socket))
	{
	  if (c->c2.tls_multi->n_soft_errors)
	    check_tls_errors_co (c);
	}
      else
	{
	  if (c->c2.tls_multi->n_hard_errors)
	    check_tls_errors_nco (c);
	}
    }
#endif
}

/*
 * Check for possible incoming configuration
 * messages on the control channel.
 */
static inline void
check_incoming_control_channel (struct context *c)
{
#if P2MP
  void check_incoming_control_channel_dowork (struct context *c);
  if (tls_test_payload_len (c->c2.tls_multi) > 0)
    check_incoming_control_channel_dowork (c);
#endif
}

/*
 * Options like --up-delay need to be triggered by this function which
 * checks for connection establishment.
 */
static inline void
check_connection_established (struct context *c)
{
  void check_connection_established_dowork (struct context *c);
  if (event_timeout_defined (&c->c2.wait_for_connect))
    check_connection_established_dowork (c);
}

/*
 * Should we add routes?
 */
static inline void
check_add_routes (struct context *c)
{
  void check_add_routes_dowork (struct context *c);
  if (event_timeout_trigger (&c->c2.route_wakeup, &c->c2.timeval, ETT_DEFAULT))
    check_add_routes_dowork (c);
}

/*
 * Should we exit due to inactivity timeout?
 */
static inline void
check_inactivity_timeout (struct context *c)
{
  void check_inactivity_timeout_dowork (struct context *c);

  if (c->options.inactivity_timeout
      && event_timeout_trigger (&c->c2.inactivity_interval, &c->c2.timeval, ETT_DEFAULT))
    check_inactivity_timeout_dowork (c);
}

#if P2MP

static inline void
check_server_poll_timeout (struct context *c)
{
  void check_server_poll_timeout_dowork (struct context *c);

  if (c->options.server_poll_timeout
      && event_timeout_trigger (&c->c2.server_poll_interval, &c->c2.timeval, ETT_DEFAULT))
    check_server_poll_timeout_dowork (c);
}

/*
 * Scheduled exit?
 */
static inline void
check_scheduled_exit (struct context *c)
{
  void check_scheduled_exit_dowork (struct context *c);

  if (event_timeout_defined (&c->c2.scheduled_exit))
    {
      if (event_timeout_trigger (&c->c2.scheduled_exit, &c->c2.timeval, ETT_DEFAULT))
	check_scheduled_exit_dowork (c);
    }
}
#endif

/*
 * Should we write timer-triggered status file.
 */
static inline void
check_status_file (struct context *c)
{
  void check_status_file_dowork (struct context *c);

  if (c->c1.status_output)
    {
      if (status_trigger_tv (c->c1.status_output, &c->c2.timeval))
	check_status_file_dowork (c);
    }
}

#ifdef ENABLE_FRAGMENT
/*
 * Should we deliver a datagram fragment to remote?
 */
static inline void
check_fragment (struct context *c)
{
  void check_fragment_dowork (struct context *c);
  if (c->c2.fragment)
    check_fragment_dowork (c);
}
#endif

#if P2MP

/*
 * see if we should send a push_request in response to --pull
 */
static inline void
check_push_request (struct context *c)
{
  void check_push_request_dowork (struct context *c);
  if (event_timeout_trigger (&c->c2.push_request_interval, &c->c2.timeval, ETT_DEFAULT))
    check_push_request_dowork (c);
}

#endif

#ifdef ENABLE_CRYPTO
/*
 * Should we persist our anti-replay packet ID state to disk?
 */
static inline void
check_packet_id_persist_flush (struct context *c)
{
  if (packet_id_persist_enabled (&c->c1.pid_persist)
      && event_timeout_trigger (&c->c2.packet_id_persist_interval, &c->c2.timeval, ETT_DEFAULT))
    packet_id_persist_save (&c->c1.pid_persist);
}
#endif

/*
 * Set our wakeup to 0 seconds, so we will be rescheduled
 * immediately.
 */
static inline void
context_immediate_reschedule (struct context *c)
{
  c->c2.timeval.tv_sec = 0;    /* ZERO-TIMEOUT */
  c->c2.timeval.tv_usec = 0;
}

static inline void
context_reschedule_sec (struct context *c, int sec)
{
  if (sec < 0)
    sec = 0;
  if (sec < c->c2.timeval.tv_sec)
    {
      c->c2.timeval.tv_sec = sec;
      c->c2.timeval.tv_usec = 0;
    }
}

static inline struct link_socket_info *
get_link_socket_info (struct context *c)
{

  if (c->c2.link_socket_info)
    return c->c2.link_socket_info;
  else
    return &c->c2.link_socket->info;
}

static inline void
register_activity (struct context *c, const int size)
{
  if (c->options.inactivity_timeout)
    {
      c->c2.inactivity_bytes += size;
      if (c->c2.inactivity_bytes >= c->options.inactivity_minimum_bytes)
	{
	  c->c2.inactivity_bytes = 0;
	  event_timeout_reset (&c->c2.inactivity_interval);
	}
    }
}

/*
 * Return the io_wait() flags appropriate for
 * a point-to-point tunnel.
 */
static inline unsigned int
p2p_iow_flags (const struct context *c)
{
  unsigned int flags = (IOW_SHAPER|IOW_CHECK_RESIDUAL|IOW_FRAG|IOW_READ|IOW_WAIT_SIGNAL);
  if (c->c2.to_link.len > 0)
    flags |= IOW_TO_LINK;
  if (c->c2.to_tun.len > 0)
    flags |= IOW_TO_TUN;
  return flags;
}

/*
 * This is the core I/O wait function, used for all I/O waits except
 * for TCP in server mode.
 */
static inline void
io_wait (struct context *c, const unsigned int flags)
{
  void io_wait_dowork (struct context *c, const unsigned int flags);

  if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
    {
      /* fast path -- only for TUN/TAP/UDP writes */
      unsigned int ret = 0;
      if (flags & IOW_TO_TUN)
	ret |= TUN_WRITE;
      if (flags & (IOW_TO_LINK|IOW_MBUF))
	ret |= SOCKET_WRITE;
      c->c2.event_set_status = ret;
    }
  else
    {
      /* slow path */
      io_wait_dowork (c, flags);
    }
}

#define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)

#endif /* EVENT_INLINE_H */