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-2010 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 20 * along with this program (see the file COPYING included with this 21 * distribution); if not, write to the Free Software Foundation, Inc., 22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25#ifndef FORWARD_INLINE_H 26#define FORWARD_INLINE_H 27 28/* 29 * Inline functions 30 */ 31 32/* 33 * Does TLS session need service? 34 */ 35static inline void 36check_tls (struct context *c) 37{ 38#if defined(ENABLE_CRYPTO) && defined(ENABLE_SSL) 39 void check_tls_dowork (struct context *c); 40 if (c->c2.tls_multi) 41 check_tls_dowork (c); 42#endif 43} 44 45/* 46 * TLS errors are fatal in TCP mode. 47 * Also check for --tls-exit trigger. 48 */ 49static inline void 50check_tls_errors (struct context *c) 51{ 52#if defined(ENABLE_CRYPTO) && defined(ENABLE_SSL) 53 void check_tls_errors_co (struct context *c); 54 void check_tls_errors_nco (struct context *c); 55 if (c->c2.tls_multi && c->c2.tls_exit_signal) 56 { 57 if (link_socket_connection_oriented (c->c2.link_socket)) 58 { 59 if (c->c2.tls_multi->n_soft_errors) 60 check_tls_errors_co (c); 61 } 62 else 63 { 64 if (c->c2.tls_multi->n_hard_errors) 65 check_tls_errors_nco (c); 66 } 67 } 68#endif 69} 70 71/* 72 * Check for possible incoming configuration 73 * messages on the control channel. 74 */ 75static inline void 76check_incoming_control_channel (struct context *c) 77{ 78#if P2MP 79 void check_incoming_control_channel_dowork (struct context *c); 80 if (tls_test_payload_len (c->c2.tls_multi) > 0) 81 check_incoming_control_channel_dowork (c); 82#endif 83} 84 85/* 86 * Options like --up-delay need to be triggered by this function which 87 * checks for connection establishment. 88 */ 89static inline void 90check_connection_established (struct context *c) 91{ 92 void check_connection_established_dowork (struct context *c); 93 if (event_timeout_defined (&c->c2.wait_for_connect)) 94 check_connection_established_dowork (c); 95} 96 97/* 98 * Should we add routes? 99 */ 100static inline void 101check_add_routes (struct context *c) 102{ 103 void check_add_routes_dowork (struct context *c); 104 if (event_timeout_trigger (&c->c2.route_wakeup, &c->c2.timeval, ETT_DEFAULT)) 105 check_add_routes_dowork (c); 106} 107 108/* 109 * Should we exit due to inactivity timeout? 110 */ 111static inline void 112check_inactivity_timeout (struct context *c) 113{ 114 void check_inactivity_timeout_dowork (struct context *c); 115 116 if (c->options.inactivity_timeout 117 && event_timeout_trigger (&c->c2.inactivity_interval, &c->c2.timeval, ETT_DEFAULT)) 118 check_inactivity_timeout_dowork (c); 119} 120 121#if P2MP 122 123static inline void 124check_server_poll_timeout (struct context *c) 125{ 126 void check_server_poll_timeout_dowork (struct context *c); 127 128 if (c->options.server_poll_timeout 129 && event_timeout_trigger (&c->c2.server_poll_interval, &c->c2.timeval, ETT_DEFAULT)) 130 check_server_poll_timeout_dowork (c); 131} 132 133/* 134 * Scheduled exit? 135 */ 136static inline void 137check_scheduled_exit (struct context *c) 138{ 139 void check_scheduled_exit_dowork (struct context *c); 140 141 if (event_timeout_defined (&c->c2.scheduled_exit)) 142 { 143 if (event_timeout_trigger (&c->c2.scheduled_exit, &c->c2.timeval, ETT_DEFAULT)) 144 check_scheduled_exit_dowork (c); 145 } 146} 147#endif 148 149/* 150 * Should we write timer-triggered status file. 151 */ 152static inline void 153check_status_file (struct context *c) 154{ 155 void check_status_file_dowork (struct context *c); 156 157 if (c->c1.status_output) 158 { 159 if (status_trigger_tv (c->c1.status_output, &c->c2.timeval)) 160 check_status_file_dowork (c); 161 } 162} 163 164#ifdef ENABLE_FRAGMENT 165/* 166 * Should we deliver a datagram fragment to remote? 167 */ 168static inline void 169check_fragment (struct context *c) 170{ 171 void check_fragment_dowork (struct context *c); 172 if (c->c2.fragment) 173 check_fragment_dowork (c); 174} 175#endif 176 177#if P2MP 178 179/* 180 * see if we should send a push_request in response to --pull 181 */ 182static inline void 183check_push_request (struct context *c) 184{ 185 void check_push_request_dowork (struct context *c); 186 if (event_timeout_trigger (&c->c2.push_request_interval, &c->c2.timeval, ETT_DEFAULT)) 187 check_push_request_dowork (c); 188} 189 190#endif 191 192#ifdef ENABLE_CRYPTO 193/* 194 * Should we persist our anti-replay packet ID state to disk? 195 */ 196static inline void 197check_packet_id_persist_flush (struct context *c) 198{ 199 if (packet_id_persist_enabled (&c->c1.pid_persist) 200 && event_timeout_trigger (&c->c2.packet_id_persist_interval, &c->c2.timeval, ETT_DEFAULT)) 201 packet_id_persist_save (&c->c1.pid_persist); 202} 203#endif 204 205/* 206 * Set our wakeup to 0 seconds, so we will be rescheduled 207 * immediately. 208 */ 209static inline void 210context_immediate_reschedule (struct context *c) 211{ 212 c->c2.timeval.tv_sec = 0; /* ZERO-TIMEOUT */ 213 c->c2.timeval.tv_usec = 0; 214} 215 216static inline void 217context_reschedule_sec (struct context *c, int sec) 218{ 219 if (sec < 0) 220 sec = 0; 221 if (sec < c->c2.timeval.tv_sec) 222 { 223 c->c2.timeval.tv_sec = sec; 224 c->c2.timeval.tv_usec = 0; 225 } 226} 227 228static inline struct link_socket_info * 229get_link_socket_info (struct context *c) 230{ 231 if (c->c2.link_socket_info) 232 return c->c2.link_socket_info; 233 else 234 return &c->c2.link_socket->info; 235} 236 237static inline void 238register_activity (struct context *c, const int size) 239{ 240 if (c->options.inactivity_timeout) 241 { 242 c->c2.inactivity_bytes += size; 243 if (c->c2.inactivity_bytes >= c->options.inactivity_minimum_bytes) 244 { 245 c->c2.inactivity_bytes = 0; 246 event_timeout_reset (&c->c2.inactivity_interval); 247 } 248 } 249} 250 251/* 252 * Return the io_wait() flags appropriate for 253 * a point-to-point tunnel. 254 */ 255static inline unsigned int 256p2p_iow_flags (const struct context *c) 257{ 258 unsigned int flags = (IOW_SHAPER|IOW_CHECK_RESIDUAL|IOW_FRAG|IOW_READ|IOW_WAIT_SIGNAL); 259 if (c->c2.to_link.len > 0) 260 flags |= IOW_TO_LINK; 261 if (c->c2.to_tun.len > 0) 262 flags |= IOW_TO_TUN; 263 return flags; 264} 265 266/* 267 * This is the core I/O wait function, used for all I/O waits except 268 * for TCP in server mode. 269 */ 270static inline void 271io_wait (struct context *c, const unsigned int flags) 272{ 273 void io_wait_dowork (struct context *c, const unsigned int flags); 274 275 if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF))) 276 { 277 /* fast path -- only for TUN/TAP/UDP writes */ 278 unsigned int ret = 0; 279 if (flags & IOW_TO_TUN) 280 ret |= TUN_WRITE; 281 if (flags & (IOW_TO_LINK|IOW_MBUF)) 282 ret |= SOCKET_WRITE; 283 c->c2.event_set_status = ret; 284 } 285 else 286 { 287 /* slow path */ 288 io_wait_dowork (c, flags); 289 } 290} 291 292#define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established) 293 294#endif /* EVENT_INLINE_H */ 295