src/openvpn/forward-inline.h
6fbf66fa
 /*
  *  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.
  *
49979459
  *  Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
6fbf66fa
  *
  *  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.
  *
caa54ac3
  *  You should have received a copy of the GNU General Public License along
  *  with this program; if not, write to the Free Software Foundation, Inc.,
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
6fbf66fa
  */
 
 #ifndef FORWARD_INLINE_H
 #define FORWARD_INLINE_H
 
 /*
  * Inline functions
  */
 
 /*
  * Does TLS session need service?
  */
 static inline void
81d882d5
 check_tls(struct context *c)
6fbf66fa
 {
81d882d5
     void check_tls_dowork(struct context *c);
 
     if (c->c2.tls_multi)
     {
         check_tls_dowork(c);
     }
6fbf66fa
 }
 
 /*
  * TLS errors are fatal in TCP mode.
  * Also check for --tls-exit trigger.
  */
 static inline void
81d882d5
 check_tls_errors(struct context *c)
6fbf66fa
 {
81d882d5
     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)
6fbf66fa
     {
81d882d5
         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);
             }
         }
6fbf66fa
     }
 }
 
 /*
  * Check for possible incoming configuration
  * messages on the control channel.
  */
 static inline void
81d882d5
 check_incoming_control_channel(struct context *c)
6fbf66fa
 {
 #if P2MP
81d882d5
     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);
     }
6fbf66fa
 #endif
 }
 
 /*
  * Options like --up-delay need to be triggered by this function which
  * checks for connection establishment.
  */
 static inline void
81d882d5
 check_connection_established(struct context *c)
6fbf66fa
 {
81d882d5
     void check_connection_established_dowork(struct context *c);
 
     if (event_timeout_defined(&c->c2.wait_for_connect))
     {
         check_connection_established_dowork(c);
     }
6fbf66fa
 }
 
 /*
  * Should we add routes?
  */
 static inline void
81d882d5
 check_add_routes(struct context *c)
6fbf66fa
 {
81d882d5
     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);
     }
6fbf66fa
 }
 
 /*
  * Should we exit due to inactivity timeout?
  */
 static inline void
81d882d5
 check_inactivity_timeout(struct context *c)
6fbf66fa
 {
81d882d5
     void check_inactivity_timeout_dowork(struct context *c);
6fbf66fa
 
81d882d5
     if (c->options.inactivity_timeout
         && event_timeout_trigger(&c->c2.inactivity_interval, &c->c2.timeval, ETT_DEFAULT))
     {
         check_inactivity_timeout_dowork(c);
     }
6fbf66fa
 }
 
 #if P2MP
e1e977f3
 
 static inline void
81d882d5
 check_server_poll_timeout(struct context *c)
e1e977f3
 {
81d882d5
     void check_server_poll_timeout_dowork(struct context *c);
e1e977f3
 
81d882d5
     if (c->options.ce.connect_timeout
         && event_timeout_trigger(&c->c2.server_poll_interval, &c->c2.timeval, ETT_DEFAULT))
     {
         check_server_poll_timeout_dowork(c);
     }
e1e977f3
 }
 
6fbf66fa
 /*
  * Scheduled exit?
  */
 static inline void
81d882d5
 check_scheduled_exit(struct context *c)
6fbf66fa
 {
81d882d5
     void check_scheduled_exit_dowork(struct context *c);
6fbf66fa
 
81d882d5
     if (event_timeout_defined(&c->c2.scheduled_exit))
6fbf66fa
     {
81d882d5
         if (event_timeout_trigger(&c->c2.scheduled_exit, &c->c2.timeval, ETT_DEFAULT))
         {
             check_scheduled_exit_dowork(c);
         }
6fbf66fa
     }
 }
81d882d5
 #endif /* if P2MP */
6fbf66fa
 
 /*
  * Should we write timer-triggered status file.
  */
 static inline void
81d882d5
 check_status_file(struct context *c)
6fbf66fa
 {
81d882d5
     void check_status_file_dowork(struct context *c);
6fbf66fa
 
81d882d5
     if (c->c1.status_output)
6fbf66fa
     {
81d882d5
         if (status_trigger_tv(c->c1.status_output, &c->c2.timeval))
         {
             check_status_file_dowork(c);
         }
6fbf66fa
     }
 }
 
 #ifdef ENABLE_FRAGMENT
 /*
  * Should we deliver a datagram fragment to remote?
  */
 static inline void
81d882d5
 check_fragment(struct context *c)
6fbf66fa
 {
81d882d5
     void check_fragment_dowork(struct context *c);
 
     if (c->c2.fragment)
     {
         check_fragment_dowork(c);
     }
6fbf66fa
 }
 #endif
 
 #if P2MP
 
 /*
  * see if we should send a push_request in response to --pull
  */
 static inline void
81d882d5
 check_push_request(struct context *c)
6fbf66fa
 {
81d882d5
     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);
     }
6fbf66fa
 }
 
 #endif
 
 /*
  * Should we persist our anti-replay packet ID state to disk?
  */
 static inline void
81d882d5
 check_packet_id_persist_flush(struct context *c)
6fbf66fa
 {
81d882d5
     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);
     }
6fbf66fa
 }
 
 /*
  * Set our wakeup to 0 seconds, so we will be rescheduled
  * immediately.
  */
 static inline void
81d882d5
 context_immediate_reschedule(struct context *c)
6fbf66fa
 {
81d882d5
     c->c2.timeval.tv_sec = 0;  /* ZERO-TIMEOUT */
     c->c2.timeval.tv_usec = 0;
6fbf66fa
 }
 
 static inline void
81d882d5
 context_reschedule_sec(struct context *c, int sec)
6fbf66fa
 {
81d882d5
     if (sec < 0)
6fbf66fa
     {
81d882d5
         sec = 0;
     }
     if (sec < c->c2.timeval.tv_sec)
     {
         c->c2.timeval.tv_sec = sec;
         c->c2.timeval.tv_usec = 0;
6fbf66fa
     }
 }
 
 static inline struct link_socket_info *
81d882d5
 get_link_socket_info(struct context *c)
6fbf66fa
 {
81d882d5
     if (c->c2.link_socket_info)
     {
         return c->c2.link_socket_info;
     }
     else
     {
         return &c->c2.link_socket->info;
     }
6fbf66fa
 }
 
 static inline void
81d882d5
 register_activity(struct context *c, const int size)
6fbf66fa
 {
81d882d5
     if (c->options.inactivity_timeout)
838911cc
     {
81d882d5
         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);
         }
838911cc
     }
6fbf66fa
 }
 
 /*
  * Return the io_wait() flags appropriate for
  * a point-to-point tunnel.
  */
 static inline unsigned int
81d882d5
 p2p_iow_flags(const struct context *c)
6fbf66fa
 {
81d882d5
     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;
6fbf66fa
 }
 
 /*
  * This is the core I/O wait function, used for all I/O waits except
  * for TCP in server mode.
  */
 static inline void
81d882d5
 io_wait(struct context *c, const unsigned int flags)
6fbf66fa
 {
81d882d5
     void io_wait_dowork(struct context *c, const unsigned int flags);
6fbf66fa
 
81d882d5
     if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
6fbf66fa
     {
81d882d5
         /* 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;
6fbf66fa
     }
81d882d5
     else
6fbf66fa
     {
81d882d5
         /* slow path */
         io_wait_dowork(c, flags);
6fbf66fa
     }
 }
 
 #define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)
 
 #endif /* EVENT_INLINE_H */