From: "cron2 (Code Review)" Date: Thu, 6 Mar 2025 11:01:28 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., gianmarco@...2726..., openvpn-devel@lists.sourceforge.net, frank@...2641..., a@...2181... Message-ID: <0cdf0337dc2eed3ea96f29010dfb4b4683788549-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="XKhoRdsg/0w="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Add support for simultaneous use of UDP and TCP sockets List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: its_Giaan Cc: flichtenheld , plaisthos , openvpn-devel --XKhoRdsg/0w= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/76= 4?usp=3Demail ) Change subject: Add support for simultaneous use of UDP and TCP sockets ...................................................................... Add support for simultaneous use of UDP and TCP sockets Add all the bound sockets to the event loop. The main server loop has been updated to handle both TCP and UDP connections. The hash function has also been modified to include the protocol during the creation of new client instances. There are also a couple of refinements to make the whole code flow management capable of handling different kind of clients: MULTI: properly remove TCP instances by checking the multi_instance protocol instead of the global one. TLS: set the tls_option xmit_hold bool value to true only in case of TCP child instance to avoid checking the global protocol value. INIT: initialize the c->c2.event_set in the inherit_context_top() by default and not only in case of UDP since we could have multiple different sockets. Change-Id: I31bbf87e4e568021445c7512ecefadfd4a69b363 Signed-off-by: Gianmarco De Gregori Acked-by: Gert Doering Message-Id: <20250306095928.10229-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg31= 028.html Signed-off-by: Gert Doering --- M doc/man-sections/link-options.rst M src/openvpn/forward.c M src/openvpn/forward.h M src/openvpn/init.c M src/openvpn/mtcp.c M src/openvpn/mtcp.h M src/openvpn/mudp.c M src/openvpn/mudp.h M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/multi_io.c M src/openvpn/multi_io.h M src/openvpn/openvpn.h M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/socket.c M src/openvpn/ssl_common.h 17 files changed, 426 insertions(+), 325 deletions(-) diff --git a/doc/man-sections/link-options.rst b/doc/man-sections/link-opti= ons.rst index 4496da2..d48021e 100644 --- a/doc/man-sections/link-options.rst +++ b/doc/man-sections/link-options.rst @@ -106,7 +106,7 @@ is not reliable. It is recommended to set tun-mtu with enough headroom instead. ---local host|* [port] +--local host|* [port] [protocol] Local host name or IP address and port for bind. If specified, OpenVPN w= ill bind to this address. If unspecified, OpenVPN will bind to all interfaces. '*' can be used as hostname and means 'any host' (OpenVPN will listen on= what diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 8b94469..b025344 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -1770,7 +1770,7 @@ if (c->options.shaper) { int overhead =3D datagram_overhead(c->c2.to_link_addr->des= t.addr.sa.sa_family, - c->options.ce.proto); + sock->info.proto); shaper_wrote_bytes(&c->c2.shaper, BLEN(&c->c2.to_link) + overhead); } @@ -2060,52 +2060,24 @@ check_timeout_random_component(c); } -/* - * Wait for I/O events. Used for both TCP & UDP sockets - * in point-to-point mode and for UDP sockets in - * point-to-multipoint mode. - */ - -void -io_wait_dowork(struct context *c, const unsigned int flags) +static void +multi_io_process_flags(struct context *c, struct event_set *es, + const unsigned int flags, unsigned int *out_socket, + unsigned int *out_tuntap) { unsigned int socket =3D 0; unsigned int tuntap =3D 0; - struct event_set_return esr[4]; - - /* These shifts all depend on EVENT_READ and EVENT_WRITE */ - static uintptr_t socket_shift =3D SOCKET_SHIFT; /* depends on SOCKET= _READ and SOCKET_WRITE */ - static uintptr_t tun_shift =3D TUN_SHIFT; /* depends on TUN_READ = and TUN_WRITE */ - static uintptr_t err_shift =3D ERR_SHIFT; /* depends on ES_ERROR = */ -#ifdef ENABLE_MANAGEMENT - static uintptr_t management_shift =3D MANAGEMENT_SHIFT; /* depends on = MANAGEMENT_READ and MANAGEMENT_WRITE */ -#endif -#ifdef ENABLE_ASYNC_PUSH - static uintptr_t file_shift =3D FILE_SHIFT; -#endif -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN= 32) - static uintptr_t dco_shift =3D DCO_SHIFT; /* Event from DCO kernel = module */ -#endif + static uintptr_t tun_shift =3D TUN_SHIFT; + static uintptr_t err_shift =3D ERR_SHIFT; /* - * Decide what kind of events we want to wait for. - */ - event_reset(c->c2.event_set); - - /* - * On win32 we use the keyboard or an event object as a source - * of asynchronous signals. + * Calculate the flags based on the provided 'flags' argument. */ if (flags & IOW_WAIT_SIGNAL) { - wait_signal(c->c2.event_set, (void *)err_shift); + wait_signal(es, (void *)err_shift); } =20 - /* - * If outgoing data (for TCP/UDP port) pending, wait for ready-to-send - * status from TCP/UDP port. Otherwise, wait for incoming data on - * TUN/TAP device. - */ if (flags & IOW_TO_LINK) { if (flags & IOW_SHAPER) @@ -2193,12 +2165,120 @@ */ for (int i =3D 0; i < c->c1.link_sockets_num; i++) { - socket_set(c->c2.link_sockets[i], c->c2.event_set, socket, + socket_set(c->c2.link_sockets[i], es, socket, &c->c2.link_sockets[i]->ev_arg, NULL); } - tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)tun_shift, NULL= ); -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN= 32) - if (socket & EVENT_READ && c->c2.did_open_tun) + + tun_set(c->c1.tuntap, es, tuntap, (void *)tun_shift, NULL); + + if (out_socket) + { + *out_socket =3D socket; + } + + if (out_tuntap) + { + *out_tuntap =3D tuntap; + } + +} + +/* + * Wait for I/O events. Used for both TCP & UDP sockets + * in point-to-point mode and for UDP sockets in + * point-to-multipoint mode. + */ + +void +get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io, cons= t unsigned int flags) +{ + unsigned int out_socket, out_tuntap; + + multi_io_process_flags(c, multi_io->es, flags, &out_socket, &out_tunta= p); + multi_io->udp_flags =3D out_socket | out_tuntap; +} + +void +get_io_flags_udp(struct context *c, struct multi_io *multi_io, const unsig= ned int flags) +{ + multi_io->udp_flags =3D ES_ERROR; + 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 =3D 0; + if (flags & IOW_TO_TUN) + { + ret |=3D TUN_WRITE; + } + if (flags & (IOW_TO_LINK | IOW_MBUF)) + { + ret |=3D SOCKET_WRITE; + } + multi_io->udp_flags =3D ret; + } + else + { +#ifdef _WIN32 + bool skip_iowait =3D flags & IOW_TO_TUN; + if (flags & IOW_READ_TUN) + { + /* + * don't read from tun if we have pending write to link, + * since every tun read overwrites to_link buffer filled + * by previous tun read + */ + skip_iowait =3D !(flags & IOW_TO_LINK); + } + if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait) + { + unsigned int ret =3D 0; + if (flags & IOW_TO_TUN) + { + ret |=3D TUN_WRITE; + } + if (flags & IOW_READ_TUN) + { + ret |=3D TUN_READ; + } + multi_io->udp_flags =3D ret; + } + else +#endif /* ifdef _WIN32 */ + { + /* slow path - delegate to io_wait_dowork_udp to calculate fla= gs */ + get_io_flags_dowork_udp(c, multi_io, flags); + } + } +} + +void +io_wait_dowork(struct context *c, const unsigned int flags) +{ + unsigned int out_socket; + unsigned int out_tuntap; + struct event_set_return esr[4]; + + /* These shifts all depend on EVENT_READ and EVENT_WRITE */ + static uintptr_t socket_shift =3D SOCKET_SHIFT; /* depends on SOCKET= _READ and SOCKET_WRITE */ +#ifdef ENABLE_MANAGEMENT + static uintptr_t management_shift =3D MANAGEMENT_SHIFT; /* depends on = MANAGEMENT_READ and MANAGEMENT_WRITE */ +#endif +#ifdef ENABLE_ASYNC_PUSH + static uintptr_t file_shift =3D FILE_SHIFT; +#endif +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) + static uintptr_t dco_shift =3D DCO_SHIFT; /* Event from DCO linux k= ernel module */ +#endif + + /* + * Decide what kind of events we want to wait for. + */ + event_reset(c->c2.event_set); + + multi_io_process_flags(c, c->c2.event_set, flags, &out_socket, &out_tu= ntap); + +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) + if (out_socket & EVENT_READ && c->c2.did_open_tun) { dco_event_set(&c->c1.tuntap->dco, c->c2.event_set, (void *)dco_shi= ft); } diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index 214a322..77892d6 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -69,6 +69,10 @@ extern counter_type link_write_bytes_global; +void get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io,= const unsigned int flags); + +void get_io_flags_udp(struct context *c, struct multi_io *multi_io, const = unsigned int flags); + void io_wait_dowork(struct context *c, const unsigned int flags); void pre_select(struct context *c); @@ -367,12 +371,13 @@ /* * This is the core I/O wait function, used for all I/O waits except - * for TCP in server mode. + * for the top-level server sockets. */ static inline void io_wait(struct context *c, const unsigned int flags) { - if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF))) + if (proto_is_dgram(c->c2.link_sockets[0]->info.proto) + && c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF))) { /* fast path -- only for TUN/TAP/UDP writes */ unsigned int ret =3D 0; diff --git a/src/openvpn/init.c b/src/openvpn/init.c index b57e5f8..c21474a 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -612,6 +612,7 @@ } c->options.ce =3D *ce; + #ifdef ENABLE_MANAGEMENT if (ce_defined && management && management_query_remote_enabled(ma= nagement)) { @@ -2681,7 +2682,9 @@ if (found & OPT_P_EXPLICIT_NOTIFY) { - if (!proto_is_udp(c->options.ce.proto) && c->options.ce.explicit_e= xit_notification) + /* Client side, so just check the first link_socket */ + if (!proto_is_udp(c->c2.link_sockets[0]->info.proto) + && c->options.ce.explicit_exit_notification) { msg(D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only b= e used with --proto udp"); c->options.ce.explicit_exit_notification =3D 0; @@ -2850,14 +2853,14 @@ int sec =3D 2; int backoff =3D 0; - switch (c->options.ce.proto) + switch (c->mode) { - case PROTO_TCP_SERVER: + case CM_TOP: sec =3D 1; break; - case PROTO_UDP: - case PROTO_TCP_CLIENT: + case CM_CHILD_UDP: + case CM_CHILD_TCP: sec =3D c->options.ce.connect_retry_seconds; break; } @@ -2875,7 +2878,7 @@ } /* Slow down reconnection after 5 retries per remote -- for TCP client= or UDP tls-client only */ - if (c->options.ce.proto =3D=3D PROTO_TCP_CLIENT + if (c->mode =3D=3D CM_CHILD_TCP || (c->options.ce.proto =3D=3D PROTO_UDP && c->options.tls_client)) { backoff =3D (c->options.unsuccessful_attempts / c->options.connect= ion_list->len) - 4; @@ -3355,7 +3358,6 @@ to.server =3D options->tls_server; to.replay_window =3D options->replay_window; to.replay_time =3D options->replay_time; - to.tcp_mode =3D link_socket_proto_connection_oriented(options->ce.prot= o); to.config_ciphername =3D c->options.ciphername; to.config_ncp_ciphers =3D c->options.ncp_ciphers; to.transition_window =3D options->transition_window; @@ -3409,7 +3411,7 @@ /* should we not xmit any packets until we get an initial * response from client? */ - if (to.server && options->ce.proto =3D=3D PROTO_TCP_SERVER) + if (to.server && c->mode =3D=3D CM_CHILD_TCP) { to.xmit_hold =3D true; } @@ -4260,20 +4262,13 @@ #ifdef _WIN32 msg(M_INFO, "NOTE: --fast-io is disabled since we are running on W= indows"); #else - if (!proto_is_udp(c->options.ce.proto)) + if (c->options.shaper) { - msg(M_INFO, "NOTE: --fast-io is disabled since we are not usin= g UDP"); + msg(M_INFO, "NOTE: --fast-io is disabled since we are using --= shaper"); } else { - if (c->options.shaper) - { - msg(M_INFO, "NOTE: --fast-io is disabled since we are usin= g --shaper"); - } - else - { - c->c2.fast_io =3D true; - } + c->c2.fast_io =3D true; } #endif } @@ -4689,7 +4684,7 @@ } /* our wait-for-i/o objects, different for posix vs. win32 */ - if (c->mode =3D=3D CM_P2P) + if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP) { do_event_set_init(c, SHAPER_DEFINED(&c->options)); } @@ -4956,7 +4951,7 @@ CLEAR(*dest); /* proto_is_dgram will ASSERT(0) if proto is invalid */ - dest->mode =3D proto_is_dgram(src->options.ce.proto) ? CM_CHILD_UDP : = CM_CHILD_TCP; + dest->mode =3D proto_is_dgram(ls->info.proto) ? CM_CHILD_UDP : CM_CHIL= D_TCP; dest->gc =3D gc_new(); @@ -4982,8 +4977,11 @@ /* options */ dest->options =3D src->options; + dest->options.ce.proto =3D ls->info.proto; options_detach(&dest->options); + dest->c2.event_set =3D src->c2.event_set; + if (dest->mode =3D=3D CM_CHILD_TCP) { /* @@ -5075,10 +5073,7 @@ dest->c2.es_owned =3D false; dest->c2.event_set =3D NULL; - if (proto_is_dgram(src->options.ce.proto)) - { - do_event_set_init(dest, false); - } + do_event_set_init(dest, false); #ifdef USE_COMP dest->c2.comp_context =3D NULL; diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index 9a8b1cb..0d409b6 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -149,20 +149,6 @@ multi_io->n_esr =3D 0; } -void -multi_tcp_set_global_rw_flags(struct multi_context *m, struct multi_instan= ce *mi) -{ - if (mi) - { - mi->socket_set_called =3D true; - socket_set(mi->context.c2.link_sockets[0], - m->multi_io->es, - mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, - &mi->ev_arg, - &mi->tcp_rwflags); - } -} - bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct mult= i_instance *mi, const unsigned int mpp_flags) { @@ -230,87 +216,3 @@ } return ret; } - -/* - * Top level event loop for single-threaded operation. - * TCP mode. - */ -void -tunnel_server_tcp(struct context *top) -{ - struct multi_context multi; - int status; - - top->mode =3D CM_TOP; - context_clear_2(top); - - /* initialize top-tunnel instance */ - init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); - if (IS_SIG(top)) - { - return; - } - - /* initialize global multi_context object */ - multi_init(&multi, top, true); - - /* initialize our cloned top object */ - multi_top_init(&multi, top); - - /* initialize management interface */ - init_management_callback_multi(&multi); - - /* finished with initialization */ - initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto tcp-server */ - -#ifdef ENABLE_ASYNC_PUSH - multi.top.c2.inotify_fd =3D inotify_init(); - if (multi.top.c2.inotify_fd < 0) - { - msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); - } -#endif - - /* per-packet event loop */ - while (true) - { - perf_push(PERF_EVENT_LOOP); - - /* wait on tun/socket list */ - multi_get_timeout(&multi, &multi.top.c2.timeval); - status =3D multi_io_wait(&multi); - MULTI_CHECK_SIG(&multi); - - /* check on status of coarse timers */ - multi_process_per_second_timers(&multi); - - /* timeout? */ - if (status > 0) - { - /* process the I/O which triggered select */ - multi_io_process_io(&multi); - MULTI_CHECK_SIG(&multi); - } - else if (status =3D=3D 0) - { - multi_io_action(&multi, NULL, TA_TIMEOUT, false); - } - - perf_pop(); - } - -#ifdef ENABLE_ASYNC_PUSH - close(top->c2.inotify_fd); -#endif - - /* shut down management interface */ - uninit_management_callback(); - - /* save ifconfig-pool */ - multi_ifconfig_pool_persist(&multi, true); - - /* tear down tunnel instance (unless --persist-tun) */ - multi_uninit(&multi); - multi_top_free(&multi); - close_instance(top); -} diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.h index 0da0a7d..9de5203 100644 --- a/src/openvpn/mtcp.h +++ b/src/openvpn/mtcp.h @@ -40,8 +40,6 @@ void multi_tcp_instance_specific_free(struct multi_instance *mi); -void multi_tcp_set_global_rw_flags(struct multi_context *m, struct multi_i= nstance *mi); - bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, = const unsigned int mpp_flags); bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct= multi_instance *mi, const unsigned int mpp_flags); @@ -50,17 +48,6 @@ void multi_tcp_link_out_deferred(struct multi_context *m, struct multi_ins= tance *mi); - -/*************************************************************************= */ -/** - * Main event loop for OpenVPN in TCP server mode. - * @ingroup eventloop - * - * @param top - Top-level context structure. - */ -void tunnel_server_tcp(struct context *top); - - void multi_tcp_delete_event(struct multi_io *multi_io, event_t event); =20 #endif /* ifndef MTCP_H */ diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index e0b27af..c0344b1 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -195,6 +195,7 @@ struct multi_instance *mi =3D NULL; struct hash *hash =3D m->hash; real.proto =3D ls->info.proto; + m->hmac_reply_ls =3D ls; if (mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true) && m->top.c2.buf.len > 0) @@ -321,7 +322,8 @@ msg_set_prefix("Connection Attempt"); m->top.c2.to_link =3D m->hmac_reply; m->top.c2.to_link_addr =3D m->hmac_reply_dest; - process_outgoing_link(&m->top, m->top.c2.link_sockets[0]); + process_outgoing_link(&m->top, m->hmac_reply_ls); + m->hmac_reply_ls =3D NULL; m->hmac_reply_dest =3D NULL; } } @@ -329,10 +331,10 @@ /* * Process an I/O event. */ -static void +void multi_process_io_udp(struct multi_context *m, struct link_socket *sock) { - const unsigned int status =3D m->top.c2.event_set_status; + const unsigned int status =3D m->multi_io->udp_flags; const unsigned int mpp_flags =3D m->top.c2.fast_io ? (MPP_CONDITIONAL_PRE_SELECT | MPP_CLO= SE_ON_SIGNAL) : (MPP_PRE_SELECT | MPP_CLOSE_ON_SIGNAL= ); @@ -420,13 +422,15 @@ } } #endif + + m->multi_io->udp_flags =3D ES_ERROR; } /* * Return the io_wait() flags appropriate for * a point-to-multipoint tunnel. */ -static inline unsigned int +unsigned int p2mp_iow_flags(const struct multi_context *m) { unsigned int flags =3D IOW_WAIT_SIGNAL; @@ -461,87 +465,3 @@ #endif return flags; } - - -void -tunnel_server_udp(struct context *top) -{ - struct multi_context multi; - - top->mode =3D CM_TOP; - context_clear_2(top); - - /* initialize top-tunnel instance */ - init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); - if (IS_SIG(top)) - { - return; - } - - /* initialize global multi_context object */ - multi_init(&multi, top, false); - - /* initialize our cloned top object */ - multi_top_init(&multi, top); - - /* initialize management interface */ - init_management_callback_multi(&multi); - - /* finished with initialization */ - initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto udp */ - -#ifdef ENABLE_ASYNC_PUSH - multi.top.c2.inotify_fd =3D inotify_init(); - if (multi.top.c2.inotify_fd < 0) - { - msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); - } -#endif - - /* per-packet event loop */ - while (true) - { - perf_push(PERF_EVENT_LOOP); - - /* set up and do the io_wait() */ - multi_get_timeout(&multi, &multi.top.c2.timeval); - io_wait(&multi.top, p2mp_iow_flags(&multi)); - MULTI_CHECK_SIG(&multi); - - /* check on status of coarse timers */ - multi_process_per_second_timers(&multi); - - /* timeout? */ - if (multi.top.c2.event_set_status =3D=3D ES_TIMEOUT) - { - multi_process_timeout(&multi, MPP_PRE_SELECT|MPP_CLOSE_ON_SIGN= AL); - } - else - { - /* process I/O */ - - /* Since there's only one link_socket just use the first, in a= n upcoming - * patch this will be changed by using the link_socket returne= d by the - * event set */ - multi_process_io_udp(&multi, top->c2.link_sockets[0]); - MULTI_CHECK_SIG(&multi); - } - - perf_pop(); - } - -#ifdef ENABLE_ASYNC_PUSH - close(top->c2.inotify_fd); -#endif - - /* shut down management interface */ - uninit_management_callback(); - - /* save ifconfig-pool */ - multi_ifconfig_pool_persist(&multi, true); - - /* tear down tunnel instance (unless --persist-tun) */ - multi_uninit(&multi); - multi_top_free(&multi); - close_instance(top); -} diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h index b378754..357b684 100644 --- a/src/openvpn/mudp.h +++ b/src/openvpn/mudp.h @@ -31,18 +31,9 @@ struct context; struct multi_context; +unsigned int p2mp_iow_flags(const struct multi_context *m); -/** - * Main event loop for OpenVPN in UDP server mode. - * @ingroup eventloop - * - * This function implements OpenVPN's main event loop for UDP server mode. - * - * @param top - Top-level context structure. - */ -void tunnel_server_udp(struct context *top); - - +void multi_process_io_udp(struct multi_context *m, struct link_socket *ls); /*************************************************************************= */ /** * Get, and if necessary create, the multi_instance associated with a diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 7ab9289..07258be 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -289,7 +289,7 @@ * Main initialization function, init multi_context object. */ void -multi_init(struct multi_context *m, struct context *t, bool tcp_mode) +multi_init(struct multi_context *m, struct context *t) { int dev =3D DEV_TYPE_UNDEF; @@ -435,13 +435,12 @@ m->instances =3D calloc(m->max_clients, sizeof(struct multi_instance *= )); + m->top.c2.event_set =3D t->c2.event_set; + /* - * Initialize multi-socket TCP I/O wait object + * Initialize multi-socket I/O wait object */ - if (tcp_mode) - { - m->multi_io =3D multi_io_init(t->options.max_clients, &m->max_clie= nts); - } + m->multi_io =3D multi_io_init(t->options.max_clients, &m->max_clients); m->tcp_queue_limit =3D t->options.tcp_queue_limit; /* @@ -607,6 +606,7 @@ ASSERT(!mi->halt); mi->halt =3D true; + bool is_dgram =3D proto_is_dgram(mi->context.c2.link_sockets[0]->info.= proto); dmsg(D_MULTI_DEBUG, "MULTI: multi_close_instance called"); @@ -665,7 +665,7 @@ mi->did_iroutes =3D false; } - if (m->multi_io) + if (!is_dgram) { multi_tcp_dereference_instance(m->multi_io, mi); } @@ -3401,7 +3401,7 @@ /* decrypt in instance context */ perf_push(PERF_PROC_IN_LINK); - lsi =3D get_link_socket_info(c); + lsi =3D &ls->info; orig_buf =3D c->c2.buf.data; if (process_incoming_link_part1(c, lsi, floated)) { @@ -3850,7 +3850,7 @@ while ((he =3D hash_iterator_next(&hi))) { struct multi_instance *mi =3D (struct multi_instance *) he->value; - if (!mi->halt) + if (!mi->halt && proto_is_dgram(mi->context.c2.link_sockets[0]->in= fo.proto)) { send_control_channel_string(&mi->context, next_server ? "RESTA= RT,[N]" : "RESTART", D_PUSH); multi_schedule_context_wakeup(m, mi); @@ -3888,7 +3888,7 @@ status_close(so); return false; } - else if (proto_is_dgram(m->top.options.ce.proto) + else if (has_udp_in_local_list(&m->top.options) && is_exit_restart(m->top.sig->signal_received) && (m->deferred_shutdown_signal.signal_received =3D=3D 0) && m->top.options.ce.explicit_exit_notification !=3D 0) @@ -4169,6 +4169,45 @@ ASSERT(mi->context.c2.tls_multi->peer_id < m->max_clients); } +/*************************************************************************= */ +/** + * Main event loop for OpenVPN in point-to-multipoint server mode. + * @ingroup eventloop + * + * @param top - Top-level context structure. + */ +static void +tunnel_server_loop(struct multi_context *multi) +{ + int status; + + while (true) + { + perf_push(PERF_EVENT_LOOP); + + /* wait on tun/socket list */ + multi_get_timeout(multi, &multi->top.c2.timeval); + status =3D multi_io_wait(multi); + MULTI_CHECK_SIG(multi); + + /* check on status of coarse timers */ + multi_process_per_second_timers(multi); + + /* timeout? */ + if (status > 0) + { + /* process the I/O which triggered select */ + multi_io_process_io(multi); + MULTI_CHECK_SIG(multi); + } + else if (status =3D=3D 0) + { + multi_io_action(multi, NULL, TA_TIMEOUT, false); + } + + perf_pop(); + } +} /* * Top level event loop. @@ -4178,12 +4217,53 @@ { ASSERT(top->options.mode =3D=3D MODE_SERVER); - if (proto_is_dgram(top->options.ce.proto)) + struct multi_context multi; + + top->mode =3D CM_TOP; + context_clear_2(top); + + /* initialize top-tunnel instance */ + init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); + if (IS_SIG(top)) { - tunnel_server_udp(top); + return; } - else + + /* initialize global multi_context object */ + multi_init(&multi, top); + + /* initialize our cloned top object */ + multi_top_init(&multi, top); + + /* initialize management interface */ + init_management_callback_multi(&multi); + + /* finished with initialization */ + initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto tcp-server */ + +#ifdef ENABLE_ASYNC_PUSH + multi.top.c2.inotify_fd =3D inotify_init(); + if (multi.top.c2.inotify_fd < 0) { - tunnel_server_tcp(top); + msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); } +#endif + + tunnel_server_loop(&multi); + + #ifdef ENABLE_ASYNC_PUSH + close(top->c2.inotify_fd); +#endif + + /* shut down management interface */ + uninit_management_callback(); + + /* save ifconfig-pool */ + multi_ifconfig_pool_persist(&multi, true); + + /* tear down tunnel instance (unless --persist-tun) */ + multi_uninit(&multi); + multi_top_free(&multi); + close_instance(top); + } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index eacfb52..4104706 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -204,6 +204,7 @@ struct buffer hmac_reply; struct link_socket_actual *hmac_reply_dest; + struct link_socket *hmac_reply_ls; /* * Timer object for stale route check @@ -251,11 +252,6 @@ * Main event loop for OpenVPN in server mode. * @ingroup eventloop * - * This function calls the appropriate main event loop function depending - * on the transport protocol used: - * - \c tunnel_server_udp() - * - \c tunnel_server_tcp() - * * @param top - Top-level context structure. */ void tunnel_server(struct context *top); @@ -267,7 +263,7 @@ * Called by mtcp.c, mudp.c, or other (to be written) protocol drivers */ -void multi_init(struct multi_context *m, struct context *t, bool tcp_mode); +void multi_init(struct multi_context *m, struct context *t); void multi_uninit(struct multi_context *m); diff --git a/src/openvpn/multi_io.c b/src/openvpn/multi_io.c index e4174dd..f1751ff 100644 --- a/src/openvpn/multi_io.c +++ b/src/openvpn/multi_io.c @@ -133,6 +133,33 @@ } void +multi_io_set_global_rw_flags(struct multi_context *m, struct multi_instanc= e *mi) +{ + if (!mi) + { + return; + } + + mi->socket_set_called =3D true; + if (proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto)) + { + socket_set(mi->context.c2.link_sockets[0], + m->multi_io->es, + EVENT_READ, + &mi->context.c2.link_sockets[0]->ev_arg, + NULL); + } + else + { + socket_set(mi->context.c2.link_sockets[0], + m->multi_io->es, + mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, + &mi->ev_arg, + &mi->tcp_rwflags); + } +} + +void multi_io_free(struct multi_io *multi_io) { if (multi_io) @@ -155,6 +182,11 @@ &m->top.c2.link_sockets[i]->ev_arg); } + if (has_udp_in_local_list(&m->top.options)) + { + get_io_flags_udp(&m->top, m->multi_io, p2mp_iow_flags(m)); + } + #ifdef _WIN32 if (tuntap_is_wintun(m->top.c1.tuntap)) { @@ -333,7 +365,7 @@ case TA_INITIAL: ASSERT(mi); - multi_tcp_set_global_rw_flags(m, mi); + multi_io_set_global_rw_flags(m, mi); multi_process_post(m, mi, mpp_flags); break; @@ -383,7 +415,7 @@ } else { - multi_tcp_set_global_rw_flags(m, mi); + multi_io_set_global_rw_flags(m, mi); } break; @@ -442,24 +474,31 @@ } break; - /* new incoming TCP client attempting to connect? */ case EVENT_ARG_LINK_SOCKET: if (!ev_arg->u.sock) { msg(D_MULTI_ERRORS, "MULTI IO: multi_io_proc_io: n= ull socket"); break; } - + /* new incoming TCP client attempting to connect? */ if (!proto_is_dgram(ev_arg->u.sock->info.proto)) { socket_reset_listen_persistent(ev_arg->u.sock); mi =3D multi_create_instance_tcp(m, ev_arg->u.sock= ); - if (mi) - { - multi_io_action(m, mi, TA_INITIAL, false); - } - break; } + else + { + multi_process_io_udp(m, ev_arg->u.sock); + mi =3D m->pending; + } + /* monitor and/or handle events that are + * triggered in succession by the first one + * before returning to the main loop. */ + if (mi) + { + multi_io_action(m, mi, TA_INITIAL, false); + } + break; } } else diff --git a/src/openvpn/multi_io.h b/src/openvpn/multi_io.h index 03d708c..0887a5e 100644 --- a/src/openvpn/multi_io.h +++ b/src/openvpn/multi_io.h @@ -70,6 +70,8 @@ void multi_io_process_io(struct multi_context *m); +void multi_io_set_global_rw_flags(struct multi_context *m, struct multi_in= stance *mi); + void multi_io_action(struct multi_context *m, struct multi_instance *mi, i= nt action, bool poll); void multi_io_delete_event(struct multi_io *multi_io, event_t event); diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index df43bba..0bbd1a4 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -216,8 +216,8 @@ * \c SIGUSR1 restarts. * * This structure is initialized at the top of the \c - * tunnel_point_to_point(), \c tunnel_server_udp(), and \c - * tunnel_server_tcp() functions. In other words, it is reset for every + * tunnel_point_to_point() and \c tunnel_server() \c + * functions. In other words, it is reset for every * iteration of the \c main() function's inner \c SIGUSR1 loop. */ struct context_2 diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 6b2dfa5..511ce05 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -999,10 +999,6 @@ const struct connection_entry *e, const int i) { - setenv_str_i(es, "proto", proto2ascii(e->proto, e->af, false), i); - /* expected to be for single socket contexts only */ - setenv_str_i(es, "local", e->local_list->array[0]->local, i); - setenv_str_i(es, "local_port", e->local_list->array[0]->port, i); setenv_str_i(es, "remote", e->remote, i); setenv_str_i(es, "remote_port", e->remote_port, i); @@ -1018,6 +1014,16 @@ } } +static void +setenv_local_entry(struct env_set *es, + const struct local_entry *e, + const int i) +{ + setenv_str_i(es, "proto", proto2ascii(e->proto, AF_UNSPEC, false), i); + setenv_str_i(es, "local", e->local, i); + setenv_str_i(es, "local_port", e->port, i); +} + void setenv_settings(struct env_set *es, const struct options *o) { @@ -1041,6 +1047,14 @@ setenv_connection_entry(es, &o->ce, 1); } + if (o->ce.local_list) + { + for (int i =3D 0; i < o->ce.local_list->len; i++) + { + setenv_local_entry(es, o->ce.local_list->array[i], i+1); + } + } + if (!o->pull) { setenv_dns_options(&o->dns_options, es); @@ -1725,12 +1739,17 @@ static void show_connection_entry(const struct connection_entry *o) { - msg(D_SHOW_PARMS, " proto =3D %s", proto2ascii(o->proto, o->af, false= )); + /* Display the global proto only in client mode or with no '--local'*/ + if (o->local_list->len =3D=3D 1) + { + msg(D_SHOW_PARMS, " proto =3D %s", proto2ascii(o->proto, o->af, f= alse)); + } + msg(D_SHOW_PARMS, " Local Sockets:"); for (int i =3D 0; i < o->local_list->len; i++) { - msg(D_SHOW_PARMS, " [%s]:%s", o->local_list->array[i]->local, - o->local_list->array[i]->port); + msg(D_SHOW_PARMS, " [%s]:%s-%s", o->local_list->array[i]->local, + o->local_list->array[i]->port, proto2ascii(o->local_list->arra= y[i]->proto, o->af, false)); } SHOW_STR(remote); SHOW_STR(remote_port); @@ -2205,6 +2224,7 @@ } ALLOC_OBJ_CLEAR_GC(e, struct local_entry, gc); + e->proto =3D PROTO_NONE; l->array[l->len++] =3D e; return e; @@ -2471,7 +2491,7 @@ { struct local_entry *le =3D ce->local_list->array[i]; - if (proto_is_net(ce->proto) + if (proto_is_net(le->proto) && string_defined_equal(le->local, ce->remote) && string_defined_equal(le->port, ce->remote_port)) { @@ -3176,14 +3196,16 @@ if (ce->proto =3D=3D PROTO_TCP) { ce->proto =3D PROTO_TCP_SERVER; + o->ce.proto =3D ce->proto; } } - if (o->client) + if (o->mode !=3D MODE_SERVER) { if (ce->proto =3D=3D PROTO_TCP) { ce->proto =3D PROTO_TCP_CLIENT; + o->ce.proto =3D ce->proto; } } @@ -3325,13 +3347,19 @@ } static void -options_postprocess_mutate_le(struct connection_entry *ce, struct local_en= try *le) +options_postprocess_mutate_le(struct connection_entry *ce, struct local_en= try *le, int mode) { /* use the global port if none is specified */ if (!le->port) { le->port =3D ce->local_port; } + /* use the global proto if none is specified and + * allow proto bindings on server mode only */ + if (!le->proto || mode =3D=3D MODE_POINT_TO_POINT) + { + le->proto =3D ce->proto; + } } #ifdef _WIN32 @@ -3779,7 +3807,19 @@ { for (i =3D 0; i < o->ce.local_list->len; i++) { - options_postprocess_mutate_le(&o->ce, o->ce.local_list->array[= i]); + options_postprocess_mutate_le(&o->ce, o->ce.local_list->array[= i], o->mode); + } + + for (int i =3D 0; i < o->ce.local_list->len; i++) + { + if (o->ce.local_list->array[i]->proto =3D=3D PROTO_TCP) + { + o->ce.local_list->array[i]->proto =3D PROTO_TCP_SERVER; + } + else if (o->ce.local_list->array[i]->proto =3D=3D PROTO_NONE) + { + o->ce.local_list->array[i]->proto =3D o->ce.proto; + } } } else @@ -3789,6 +3829,7 @@ struct local_entry *e =3D alloc_local_entry(&o->ce, M_USAGE, &o->g= c); ASSERT(e); e->port =3D o->ce.local_port; + e->proto =3D o->ce.proto; } /* use the same listen list for every outgoing connection */ @@ -6191,7 +6232,7 @@ VERIFY_PERMISSION(OPT_P_UP); options->ifconfig_nowarn =3D true; } - else if (streq(p[0], "local") && p[1] && !p[3]) + else if (streq(p[0], "local") && p[1] && !p[4]) { struct local_entry *e; @@ -6212,6 +6253,11 @@ { e->port =3D p[2]; } + + if (p[3]) + { + e->proto =3D ascii2proto(p[3]); + } } else if (streq(p[0], "remote-random") && !p[1]) { @@ -9621,3 +9667,37 @@ err: gc_free(&gc); } + +bool +has_udp_in_local_list(const struct options *options) +{ + if (options->ce.local_list) + { + for (int i =3D 0; i < options->ce.local_list->len; i++) + { + if (proto_is_dgram(options->ce.local_list->array[i]->proto)) + { + return true; + } + } + } + + return false; +} + +bool +has_tcp_in_local_list(const struct options *options) +{ + if (options->ce.local_list) + { + for (int i =3D 0; i < options->ce.local_list->len; i++) + { + if (!proto_is_dgram(options->ce.local_list->array[i]->proto)) + { + return true; + } + } + } + + return false; +} diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 3f4cb90..161899a 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -98,6 +98,7 @@ { const char *local; const char *port; + int proto; }; struct connection_entry @@ -917,6 +918,10 @@ bool key_is_external(const struct options *options); +bool has_udp_in_local_list(const struct options *options); + +bool has_tcp_in_local_list(const struct options *options); + /** * Returns whether the current configuration has dco enabled. */ diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 5b32885..d8cb9f5 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1880,11 +1880,31 @@ struct options *o =3D &c->options; ASSERT(sock); + const char *host =3D o->ce.local_list->array[sock_index]->local; + const char *port =3D o->ce.local_list->array[sock_index]->port; + int proto =3D o->ce.local_list->array[sock_index]->proto; const char *remote_host =3D o->ce.remote; const char *remote_port =3D o->ce.remote_port; - sock->local_host =3D o->ce.local_list->array[sock_index]->local; - sock->local_port =3D o->ce.local_list->array[sock_index]->port; + if (c->mode =3D=3D CM_CHILD_TCP || c->mode =3D=3D CM_CHILD_UDP) + { + struct link_socket *tmp_sock =3D NULL; + if (c->mode =3D=3D CM_CHILD_TCP) + { + tmp_sock =3D (struct link_socket *)c->c2.accept_from; + } + else if (c->mode =3D=3D CM_CHILD_UDP) + { + tmp_sock =3D c->c2.link_sockets[0]; + } + + host =3D tmp_sock->local_host; + port =3D tmp_sock->local_port; + proto =3D tmp_sock->info.proto; + } + + sock->local_host =3D host; + sock->local_port =3D port; sock->remote_host =3D remote_host; sock->remote_port =3D remote_port; sock->dns_cache =3D c->c1.dns_cache; @@ -1912,7 +1932,7 @@ sock->mark =3D o->mark; sock->bind_dev =3D o->bind_dev; - sock->info.proto =3D o->ce.proto; + sock->info.proto =3D proto; sock->info.af =3D o->ce.af; sock->info.remote_float =3D o->ce.remote_float; sock->info.lsa =3D &c->c1.link_socket_addrs[sock_index]; diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 9625a99..6916ad4 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -362,7 +362,6 @@ int replay_window; /* --replay-window parm */ int replay_time; /* --replay-window parm */ - bool tcp_mode; const char *config_ciphername; const char *config_ncp_ciphers; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/764?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I31bbf87e4e568021445c7512ecefadfd4a69b363 Gerrit-Change-Number: 764 Gerrit-PatchSet: 22 Gerrit-Owner: its_Giaan Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-CC: ordex Gerrit-MessageType: merged --XKhoRdsg/0w= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

Add support for simultaneous use of UDP and TC=
P sockets

Add all the bound sockets to the event loop.
The main s= erver loop has been updated to handle both
TCP and UDP connections.
T= he hash function has also been modified to include the
protocol during t= he creation of new client instances.
There are also a couple of refineme= nts to make the
whole code flow management capable of handling
differ= ent kind of clients:

MULTI: properly remove TCP instances by checkin= g the multi_instance
protocol instead of the global one.

T= LS: set the tls_option xmit_hold bool value to true only in case of
= TCP child instance to avoid checking the global protocol
value.
INIT: initialize the c->c2.event_set in the inherit_context_top() by default and not only in case of UDP since we could have
= multiple different sockets.

Change-Id: I31bbf87e4e568021445c7512ece= fadfd4a69b363
Signed-off-by: Gianmarco De Gregori <gianmarco@...2786...= ..>
Acked-by: Gert Doering <gert@...1296...>
Message-Id: <= ;20250306095928.10229-1-gert@...1296...>
URL: https://www.mail-archiv= e.com/openvpn-devel@lists.sourceforge.net/msg31028.html
Signed-off-by: G= ert Doering <gert@...1296...>
---
M doc/man-sections/link-optio= ns.rst
M src/openvpn/forward.c
M src/openvpn/forward.h
M src/openv= pn/init.c
M src/openvpn/mtcp.c
M src/openvpn/mtcp.h
M src/openvpn/= mudp.c
M src/openvpn/mudp.h
M src/openvpn/multi.c
M src/openvpn/mu= lti.h
M src/openvpn/multi_io.c
M src/openvpn/multi_io.h
M src/open= vpn/openvpn.h
M src/openvpn/options.c
M src/openvpn/options.h
M sr= c/openvpn/socket.c
M src/openvpn/ssl_common.h
17 files changed, 426 i= nsertions(+), 325 deletions(-)

diff --git a/doc/man-sections/link-options.rst b/doc/man-sections/link-o=
ptions.rst
index 4496da2..d48021e 100644
--= - a/doc/man-sections/link-options.rst
+++ b/doc/man-section= s/link-options.rst
@@ -106,7 +106,7 @@
i= s not reliable. It is recommended to set tun-mtu with enough headroom
instead.

---local host|* [p= ort]
+--local host|* = [port] [protocol]
Local host name or IP address and port= for bind. If specified, OpenVPN will bind
to this addre= ss. If unspecified, OpenVPN will bind to all interfaces.
= '*' can be used as hostname and means 'any host' (OpenVPN = will listen on what
diff --git a/src/openvpn/forward.c b/sr= c/openvpn/forward.c
index 8b94469..b025344 100644--- a/src/openvpn/forward.c
+++ b/src/openvpn/forwar= d.c
@@ -1770,7 +1770,7 @@
if (= c->options.shaper)
{
= int overhead =3D datagram_overhead(c->c2.to_link_addr->des= t.addr.sa.sa_family,
- = c->options.ce.proto);
+ = sock->info.proto);
= shaper_wrote_bytes(&c->c2.shaper,
= BLEN(&c->c2.to_link) + overhead); }
@@ -2060,52 +2060,24 @@
check_timeout_random_component(c);

}

-/*
- * Wait for I/O events. Used for bo= th TCP & UDP sockets
- * point-to-multipoint mode.
- */
-
-= void
-io_wait_dowork(st= ruct context *c, const unsigned int flags)
+static void
+multi_io_process_flags(struct context *c, struct event_set *= es,
+ = const unsigned int flags, unsigned int *out_socket,
+ unsigned int = *out_tuntap)
{
unsigned int socket = =3D 0;
unsigned int tuntap =3D 0;
- struct event_set_return esr[4];=
-
- /* These shifts all depend on EVENT_READ and = EVENT_WRITE */
- sta= tic uintptr_t socket_shift =3D SOCKET_SHIFT; /* depends on SOCKET_READ an= d SOCKET_WRITE */
- = static uintptr_t tun_shift =3D TUN_SHIFT; /* depends on TUN_READ and T= UN_WRITE */
- static= uintptr_t err_shift =3D ERR_SHIFT; /* depends on ES_ERROR */-#ifdef ENABLE_MANAGEMENT
- static uintptr_t manage= ment_shift =3D MANAGEMENT_SHIFT; /* depends on MANAGEMENT_READ and MANAGEME= NT_WRITE */
-#endif

-#ifdef ENABLE_ASYNC_PUSH<= /span>
- static uintptr_t f= ile_shift =3D FILE_SHIFT;
-#endif
-#if defined= (TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN32)- static uintptr_t dco_shift= =3D DCO_SHIFT; /* Event from DCO kernel module */
-#endif
+ static uintptr_t tun_shift =3D TUN_SHIFT;
= + static uintptr_t err_shift= =3D ERR_SHIFT;

/*
- * Decide what kind of events we wa= nt to wait for.
- *= /
- event_reset(c-&g= t;c2.event_set);
-
- /*
- * On win32 we use the keyboard or an= event object as a source
- * of asynchronous signals.
+ * Calculate the flags based on the provided 'flags= ' argument.
*/
if (flags &am= p; IOW_WAIT_SIGNAL)
{
- wait_signal(c->c2.event_set, (void *)err_sh= ift);
+ wait_s= ignal(es, (void *)err_shift);
}

- /*
- * If outgoing data (for TCP/UDP port= ) pending, wait for ready-to-send
- * status from TCP/UDP port. Otherwise, wait for incoming d= ata on
- * TUN/TAP = device.
- */=
if (flags & IOW_TO_LINK)
{ if (flags & IOW_SHAPER)
@@ -2193,12 +2= 165,120 @@
*/
for (int i =3D 0; = i < c->c1.link_sockets_num; i++)
{
- socket_set(c->c2.link_so= ckets[i], c->c2.event_set, socket,

+ socket_set(c->c2.link_sockets[i], es, socket,<= /span>
&c->c2.link_sockets[i]->ev_ar= g, NULL);
}
- tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *= )tun_shift, NULL);
-#if= defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN32)<= /span>
- if (socket & E= VENT_READ && c->c2.did_open_tun)
+
+ tun_set(c->c1.tuntap, es, tuntap, (void *)tun_shift, NULL);
+
+ if (out_socket)
+ {
+ *out_socket =3D socket;
+ }
+
= + if (out_tuntap)
= + {
+ *out_= tuntap =3D tuntap;
+ = }
+
+}

+
+/*
+ * Wait for I= /O events. Used for both TCP & UDP sockets
+ * in point-to-point mode and for UDP sockets i= n
+ * point-to-multip= oint mode.
+ */
+
+void
+get_io_flags_dowork_udp(struct context *c, struct multi_io = *multi_io, const unsigned int flags)
+{
+ = unsigned int out_socket, out_tuntap;
+
= + multi_io_process_flags(c, multi_io->es, flags, &out_socket, &am= p;out_tuntap);
+ m= ulti_io->udp_flags =3D out_socket | out_tuntap;
+}
+
+void
+get_io_flags_udp(stru= ct context *c, struct multi_io *multi_io, const unsigned int flags)<= br>+{
+ multi_io->udp_flags =3D ES_ERROR;
+ if (c->c2.fast_io= && (flags & (IOW_TO_TUN | IOW_TO_LINK | IOW_MBUF)))
= + {
+ /* fast path -- only for TUN/TAP/UDP= writes */
+ u= nsigned int ret =3D 0;
+ {
+ ret |=3D TUN_WRITE;
+ }
+ if (flags & (IOW_TO_LINK | IOW_MBUF))
+ {

+ ret |=3D SOCKET_WRITE;+ }
+ multi_io->udp_flags =3D ret;=
+ }
+ else

+ {
+#ifdef _WIN32
+ bool skip_iowait =3D flags & IOW_TO_TUN;
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (flags & IOW_REA= D_TUN)

+ {
+ /*+ * don't rea= d from tun if we have pending write to link,
+ * since every tun read overwrites to_= link buffer filled
+ = * by previous tun read
+ */
+ skip_iowait =3D !(flags & IOW_TO_LINK);+ }
+ if (tuntap_is_wintun(c->c1.= tuntap) && skip_iowait)
+ {
+ unsigned int ret =3D 0;
+ if (flags & IOW_TO_TUN)
+ {
+ ret |=3D TUN_WRITE;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (flags & IOW= _READ_TUN)

+ = {
+ = ret |=3D TUN_READ;
+= }
+ = multi_io->udp_flags =3D ret;
+ }
+ else
= +#endif /* ifdef _WIN32 */
+ {
+ = /* slow path - delegate to io_wait_dowork_udp to calculate flags *= /
+ get_io= _flags_dowork_udp(c, multi_io, flags);
+ }
+ }
+}
+
+void
+io_wait_dowork(struct context *c, const unsigned int fla= gs)
+{
+ unsigned int out_socket;
+ unsigned int out_tunt= ap;
+ struct event= _set_return esr[4];
+=
+ /* These shifts= all depend on EVENT_READ and EVENT_WRITE */
+ static uintptr_t socket_shift =3D SOCKET_SHIFT= ; /* depends on SOCKET_READ and SOCKET_WRITE */
+#ifdef ENABLE_MANAGEMENT
+ static uintptr_t management_shift = =3D MANAGEMENT_SHIFT; /* depends on MANAGEMENT_READ and MANAGEMENT_WRITE */=
+#endif
+#ifdef ENABLE_ASYNC_PUSH
<= br>+ static uintptr_t file_s= hift =3D FILE_SHIFT;
= +#endif
+#if defined(= TARGET_LINUX) || defined(TARGET_FREEBSD)
+ static uintptr_t dco_shift =3D DCO_SHIFT; /* Ev= ent from DCO linux kernel module */
+#endif
+ /*+ * Decide what kind of ev= ents we want to wait for.
+ */
+ ev= ent_reset(c->c2.event_set);
+
+ mult= i_io_process_flags(c, c->c2.event_set, flags, &out_socket, &out_= tuntap);
+
= +#if defined(TARGET_LINUX) || d= efined(TARGET_FREEBSD)
+void get_io_flags_dowork_udp(struct context *c, st= ruct multi_io *multi_io, const unsigned int flags);
+
+void get_io_flags_udp(struct context *c, struct multi_io *mu= lti_io, const unsigned int flags);
+
void io_wait_dowork(struct context *c, con= st unsigned int flags);

void pre_select(= struct context *c);
@@ -367,12 +371,13 @@
=
/*
* This is the core I/O wait function= , used for all I/O waits except
- * for TCP in server mode.
+ * for the top-level server sockets.
*/<= /span>
static inline void
io_wait(struct context= *c, const unsigned int flags)
{
- if (c->c2.fast_io && (flags &= ; (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
+ if (proto_is_dgram(c->c2.link_sockets[0]->info.= proto)
+ &= & c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_= MBUF)))
{
/* fast path -- onl= y for TUN/TAP/UDP writes */
unsigned int ret =3D 0= ;
diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index b57e5f8..c21474a 100644
--- a/src/openvpn= /init.c
+++ b/src/openvpn/init.c
@@ -612,6 = +612,7 @@
}

= c->options.ce =3D *ce;
+
#ifdef ENABLE_MANAGEMENT
= if (ce_defined && management && management_query_remote= _enabled(management))
{
@@ -2681,7= +2682,9 @@

if (found & OPT_P_EX= PLICIT_NOTIFY)
{
- if (!proto_is_udp(c->options.ce.proto) &&= ; c->options.ce.explicit_exit_notification)
+ /* Client side, so just check the first = link_socket */
+ = if (!proto_is_udp(c->c2.link_sockets[0]->info.proto)
+ && c->opti= ons.ce.explicit_exit_notification)

{
msg(D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify ca= n only be used with --proto udp");

c->= options.ce.explicit_exit_notification =3D 0;
@@ -2850,14 +2= 853,14 @@
int sec =3D 2;
int back= off =3D 0;

- switch (c->options.ce.proto)
+ switch (c->mode)
{
- case PROTO_TCP_SERV= ER:
+ case CM_= TOP:
sec =3D 1;
b= reak;

- case PROTO_UDP:
- case PROTO_TCP_CLIENT:
+ case CM_CHILD_UDP:
+ case CM_CHILD_TCP:
= sec =3D c->options.ce.connect_retry_seconds;
= break;
}
@@ -2875,7 +2878,7 @@
}

/* Slow down re= connection after 5 retries per remote -- for TCP client or UDP tls-client o= nly */
- if (c->o= ptions.ce.proto =3D=3D PROTO_TCP_CLIENT
+ if (c->mode =3D=3D CM_CHILD_TCP
= || (c->options.ce.proto =3D=3D PROTO_UDP && c->optio= ns.tls_client))
{
backoff =3D= (c->options.unsuccessful_attempts / c->options.connection_list->l= en) - 4;
@@ -3355,7 +3358,6 @@
to.serv= er =3D options->tls_server;
to.replay_window =3D op= tions->replay_window;
to.replay_time =3D options-&g= t;replay_time;
- to.= tcp_mode =3D link_socket_proto_connection_oriented(options->ce.proto);
to.config_ciphername =3D c->options.ciphername;
to.config_ncp_ciphers =3D c->options.ncp_ciphers;
to.transition_window =3D options->transition_window;
@@ -3409,7 +3411,7 @@

/= * should we not xmit any packets until we get an initial
= * response from client? */
- if (to.server && options->ce.proto =3D=3D PROTO_TCP= _SERVER)
+ if (to.= server && c->mode =3D=3D CM_CHILD_TCP)
{
to.xmit_hold =3D true;
}=
@@ -4260,20 +4262,13 @@
#ifdef _WIN32
= msg(M_INFO, "NOTE: --fast-io is disabled since we are r= unning on Windows");
#else
- if (!proto_is_udp(c->options.ce.proto= ))
+ if (c->= ;options.shaper)
{
- msg(M_INFO, "NOTE: --fast-io is disa= bled since we are not using UDP");
+ msg(M_INFO, "NOTE: --fast-io is disabl= ed since we are using --shaper");
} else
{
- if (c->options.shaper)
<= span style=3D"color: hsl(0, 100%, 40%);">- {

- msg(M_INFO, "NOTE: -= -fast-io is disabled since we are using --shaper");
- }
- else
- {
- c->c2.fast_io =3D true;
- }
+ c->c2.fast_io =3D true;<= br> }
#endif
}@@ -4689,7 +4684,7 @@
}

/* our wait-for-i/o objects, different for posix vs. win32= */
- if (c->mode= =3D=3D CM_P2P)
+ = if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP)
= {
do_event_set_init(c, SHAPER_DEFINED(&c-&= gt;options));
}
@@ -4956,7 +4951,7 @@<= /span>
CLEAR(*dest);

/= * proto_is_dgram will ASSERT(0) if proto is invalid */
- dest->mode =3D proto_is_dgram(src-&= gt;options.ce.proto) ? CM_CHILD_UDP : CM_CHILD_TCP;
+ dest->mode =3D proto_is_dgram(ls-&= gt;info.proto) ? CM_CHILD_UDP : CM_CHILD_TCP;

<= span> dest->gc =3D gc_new();


@@ -4= 982,8 +4977,11 @@

/* options */
dest->options =3D src->options;
+ dest->options.ce.proto =3D ls->= ;info.proto;
options_detach(&dest->options);

+ = dest->c2.event_set =3D src->c2.event_set;
+
if (dest->mode =3D=3D = CM_CHILD_TCP)
{
/*
= @@ -5075,10 +5073,7 @@
dest->c2.es_owned =3D = false;

dest->c2.event_set =3D NUL= L;
- if (proto_is_dg= ram(src->options.ce.proto))
- {
- = do_event_set_init(dest, false);
- }
+ = do_event_set_init(dest, false);

#ifdef = USE_COMP
dest->c2.comp_context =3D NULL;
= diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c
= index 9a8b1cb..0d409b6 100644
--- a/src/openvpn/mtcp.c
+++ b/src/openvpn/mtcp.c
@@ -149,20 +149,6 @@
multi_io->n_esr =3D 0;
}
=
-void-multi_tcp_set_global_rw_flags(s= truct multi_context *m, struct multi_instance *mi)
-{
- if (mi)
- = {
- mi->so= cket_set_called =3D true;
- socket_set(mi->context.c2.link_sockets[0],
- m->multi_io->e= s,
- = mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE : EVENT_READ,
- &mi= ->ev_arg,
- = &mi->tcp_rwflags);
- }
-}<= /span>
-
bool=
multi_tcp_process_outgoing_link_ready(struct multi_contex= t *m, struct multi_instance *mi, const unsigned int mpp_flags)
{

@@ -230,87 +216,3 @@
} return ret;
}
-
= -/*
- * Top level event= loop for single-threaded operation.
- * TCP mode.
- */
-void<= br>-tunnel_server_tcp(struct cont= ext *top)
-{
= - struct multi_context multi;<= /span>
- int status;=
-
- top->mode =3D CM_TOP;
- context_clear_2(top);
-
- /* initialize top-tunnel instance */
- init_instance_handle_signals(top, to= p->es, CC_HARD_USR1_TO_HUP);
- if (IS_SIG(top))
- {
- = return;
- }<= br>-
- /* initialize global multi_context object */
- multi_init(&mul= ti, top, true);
-
- /* initialize our clone= d top object */
- mu= lti_top_init(&multi, top);
-
- /* initi= alize management interface */
- init_management_callback_multi(&multi);
-
- /* finished with initialization */
- initialization_sequence_completed(top, = ISC_SERVER); /* --mode server --proto tcp-server */
-
-#ifdef ENABLE_ASYNC_PUSH
- multi.top.c2.inotify_fd =3D inotify_init();
- if (multi.top.c2.inotify_fd <= ; 0)

- {
<= span style=3D"color: hsl(0, 100%, 40%);">- msg(D_MULTI_ERRORS | M_ER= RNO, "MULTI: inotify_init error");

- }
-#endif
-<= br>- /* per-packet event loop = */
- while (true)
- {
- perf_push(PERF_EVENT_LOOP);
-
- /* wait on tun/socket list */- multi_get_timeout(&= multi, &multi.top.c2.timeval);
- status =3D multi_io_wait(&multi);
- MULTI_CHECK_SIG(&multi);
-
- /* check on status of coarse timers= */
- multi_proc= ess_per_second_timers(&multi);
-
- = /* timeout? */
- = if (status > 0)
- = {
- /*= process the I/O which triggered select */
- multi_io_process_io(&multi);- MULTI_CHECK_SIG(&am= p;multi);
- }
- else if (status = =3D=3D 0)
- {
- multi_io_act= ion(&multi, NULL, TA_TIMEOUT, false);
- }
-
- perf_= pop();
- }-
-#ifdef ENABLE_ASYNC_PUSH
- close(top->c2.inotify_fd);
-#endif

-
- /* shut down management interface */
- uninit_management_callback();
-
- /* save ifconfig-pool */
- multi_ifconfig_pool_persist(&multi, true);
-
- /* tear down tunnel instance (unless --persi= st-tun) */
- multi_u= ninit(&multi);
- = multi_top_free(&multi);
- close_instance(top);
-}
diff --git a/src/openvpn/mtcp.h b/src/openvpn/m= tcp.h
index 0da0a7d..9de5203 100644
--- a/s= rc/openvpn/mtcp.h
+++ b/src/openvpn/mtcp.h
= @@ -40,8 +40,6 @@

void multi_tcp_instanc= e_specific_free(struct multi_instance *mi);

-void multi_tcp_set_global_rw_flags(= struct multi_context *m, struct multi_instance *mi);

-
bool multi_tcp_process_ou= tgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_fla= gs);

bool multi_tcp_process_outgoing_lin= k_ready(struct multi_context *m, struct multi_instance *mi, const unsigned = int mpp_flags);
@@ -50,17 +48,6 @@
=
void multi_tcp_link_out_deferred(struct multi_context *m, struct= multi_instance *mi);

-
-/**= ************************************************************************/
-/**
- * Main event loop for OpenVPN in TCP ser= ver mode.
- * @ingroup = eventloop
- *- * @param top - Top-level conte= xt structure.
- */
-void tunnel_server_tcp(str= uct context *top);
-
-
void m= ulti_tcp_delete_event(struct multi_io *multi_io, event_t event);
=
#endif /* ifndef MTCP_H */
diff --= git a/src/openvpn/mudp.c b/src/openvpn/mudp.c
index e0b27af= ..c0344b1 100644
--- a/src/openvpn/mudp.c
+= ++ b/src/openvpn/mudp.c
@@ -195,6 +195,7 @@
struct multi_instance *mi =3D NULL;

struct hash = *hash =3D m->hash;
real.proto =3D ls->info.proto= ;
+ m->hmac_rep= ly_ls =3D ls;

if (mroute_extract_ope= nvpn_sockaddr(&real, &m->top.c2.from.dest, true)
&& m->top.c2.buf.len > 0)

@@ -321,7= +322,8 @@
msg_set_prefix("Connection Attempt= ");
m->top.c2.to_link =3D m->hmac_reply= ;
m->top.c2.to_link_addr =3D m->hmac_reply_d= est;
- process_o= utgoing_link(&m->top, m->top.c2.link_sockets[0]);
+ process_outgoing_link(&= m->top, m->hmac_reply_ls);

+ m->hmac_reply_ls =3D NULL;
= m->hmac_reply_dest =3D NULL;
}
}<= /span>
@@ -329,10 +331,10 @@
/*
= * Process an I/O event.
*/
-static void
+void
multi_process_io_udp(struct multi_con= text *m, struct link_socket *sock)
{
- const unsigned int status =3D m->top= .c2.event_set_status;
+ const unsigned int status =3D m->multi_io->udp_flags; const unsigned int mpp_flags =3D m->top.c2.fast_io ? (MPP_CONDITIONAL_PRE_SELECT | = MPP_CLOSE_ON_SIGNAL)
: = (MPP_PRE_SELECT | MPP_CLOSE_ON_SIGNAL);
@@ -420,13 +422,15 = @@
}
}
#endi= f
+
+ m->multi_io->udp_flags =3D = ES_ERROR;
}

/* * Return the io_wait() flags appropriate for
* = a point-to-multipoint tunnel.
*/
-static inline unsigned int
+unsigned int
p2mp_iow_= flags(const struct multi_context *m)
{
= unsigned int flags =3D IOW_WAIT_SIGNAL;
@@ -461,87 +465,3= @@
#endif
return flags;
}

-
-

-void
= -tunnel_server_udp(struct context *top)
-{
- = struct multi_context multi;
-
- top->m= ode =3D CM_TOP;
- co= ntext_clear_2(top);
-
- /* initialize top-t= unnel instance */
- = init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP);<= br>- if (IS_SIG(top))- {
- return;
- }
-
- /* initial= ize global multi_context object */
- multi_init(&multi, top, false);
-
- /* initialize our cloned top object */
- multi_top_init(&multi, top);=
-
- /* initialize management interface */<= br>- init_management_callback_= multi(&multi);
-
- /* finished with ini= tialization */
- ini= tialization_sequence_completed(top, ISC_SERVER); /* --mode server --proto u= dp */
-
-#ifdef ENABLE_ASYNC_PUSH

- multi.top.c2.inotify_fd =3D in= otify_init();

- if (= multi.top.c2.inotify_fd < 0)
- {
- = msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");
- }
-#endif
-
- = /* per-packet event loop */
- while (true)
- perf_= push(PERF_EVENT_LOOP);
= -
- /* set up an= d do the io_wait() */
-= multi_get_timeout(&multi, &multi.top.c2.timeval);- io_wait(&multi.top,= p2mp_iow_flags(&multi));
- MULTI_CHECK_SIG(&multi);
-
- /* check on status of coarse timers */
- multi_process_per_second_timers(&mul= ti);
-
- /* timeout? */
- if (multi.top.c2.event_set_st= atus =3D=3D ES_TIMEOUT)

- {
- = multi_process_timeout(&multi, MPP_PRE_SELECT|MPP_CLOSE_ON_SIGNAL);
- }
- else

- {
- /* process I/O */
-
- /* Since there's only one link_socket just use the f= irst, in an upcoming
- = * patch this will be changed by using the link_socket returned = by the
- * = event set */
- = multi_process_io_udp(&multi, top->c2.link_sockets[0]);
= - MULTI_CHECK_SIG(&= ;multi);
- }
-
- perf_pop();
- }
-
-#ifdef ENA= BLE_ASYNC_PUSH
- clo= se(top->c2.inotify_fd);
-#endif
-- /* shut down management int= erface */
- uninit_m= anagement_callback();
-=
- /* save ifconfig-= pool */
- multi_ifco= nfig_pool_persist(&multi, true);
-
- /*= tear down tunnel instance (unless --persist-tun) */
- multi_uninit(&multi);
- multi_top_free(&multi);
- close_instance(top);<= /span>
-}
diff= --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h
index b378= 754..357b684 100644
--- a/src/openvpn/mudp.h
+++ b/src/openvpn/mudp.h

@@ -31,18 +31,9 @@
struct context;

struct multi_context;
=
+unsigned int p2mp_= iow_flags(const struct multi_context *m);

-/**

- * Main event loop for OpenVPN in UDP server mode.
- * @ingroup eventloop
- *
- * This function implements OpenVPN's m= ain event loop for UDP server mode.
- *
- * @p= aram top - Top-level context structure.
- */
-= void tunnel_server_udp(struct context *top);
-
-
+void multi_proces= s_io_udp(struct multi_context *m, struct link_socket *ls);
= /*************************************************************************= */
/**
* Get, and if necessary create, t= he multi_instance associated with a
diff --git a/src/openvp= n/multi.c b/src/openvpn/multi.c
index 7ab9289..07258be 1006= 44
--- a/src/openvpn/multi.c
+++ b/src/open= vpn/multi.c
@@ -289,7 +289,7 @@
* Main in= itialization function, init multi_context object.
*/
void
-mult= i_init(struct multi_context *m, struct context *t, bool tcp_mode)+multi_init(struct multi_conte= xt *m, struct context *t)
{
int dev = =3D DEV_TYPE_UNDEF;

@@ -435,13 +435,12 @@=

m->instances =3D calloc(m->ma= x_clients, sizeof(struct multi_instance *));

+ m->top.c2.event_set =3D t= ->c2.event_set;

+<= /span>
/*
- * Initialize multi-socket TCP I/O wait object
+ * Initialize multi-socket I/O wait o= bject
*/
- if (tcp_mode)
- {
- m-&= gt;multi_io =3D multi_io_init(t->options.max_clients, &m->max_cli= ents);
- }+ m->multi_io =3D multi_= io_init(t->options.max_clients, &m->max_clients);
m->tcp_queue_limit =3D t->options.tcp_queue_limit;


/*
@@ -607,6 +606,7 @@
ASSERT(!mi->halt);
mi= ->halt =3D true;
+= bool is_dgram =3D proto_is_dgram(mi->context.c2.link_sockets[0]->= info.proto);

dmsg(D_MULTI_DEBUG, &qu= ot;MULTI: multi_close_instance called");

<= span>@@ -665,7 +665,7 @@

mi->did_iroutes = =3D false;
}

- if (m->multi_io)
+ if (!is_dgram)

= {
multi_tcp_dereference_instanc= e(m->multi_io, mi);
}
@@ -3401,= 7 +3401,7 @@
/* decrypt in instance context */=

perf_push(PERF_PROC_IN_LINK= );
- lsi =3D= get_link_socket_info(c);
+ lsi =3D &ls->info;
or= ig_buf =3D c->c2.buf.data;
if (process_inco= ming_link_part1(c, lsi, floated))
{
= @@ -3850,7 +3850,7 @@
while ((he =3D hash_iterat= or_next(&hi)))
{
struct m= ulti_instance *mi =3D (struct multi_instance *) he->value;
- if (!mi->halt)
+ if (!mi->halt &= ;& proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto))
{
send_control_channel_= string(&mi->context, next_server ? "RESTART,[N]" : "R= ESTART", D_PUSH);
multi_schedule_context_= wakeup(m, mi);
@@ -3888,7 +3888,7 @@
= status_close(so);
return false;
}

- else if (p= roto_is_dgram(m->top.options.ce.proto)
+ else if (has_udp_in_local_list(&m->top.opt= ions)
&& is_exit_restart(m->top.si= g->signal_received)
&& (m->defe= rred_shutdown_signal.signal_received =3D=3D 0)
= && m->top.options.ce.explicit_exit_notification !=3D 0)=
@@ -4169,6 +4169,45 @@
ASSERT(mi->context= .c2.tls_multi->peer_id < m->max_clients);
}

+/******= ********************************************************************/
+/**
+ * Main event loop for OpenVPN in point-t= o-multipoint server mode.
+ * @ingroup eventloop
+ *
+ * @param= top - Top-level context structure.
+ */
+= static void
+tunnel_s= erver_loop(struct multi_context *multi)
+{
+ int status;
+
+ while (true)
+ {
+ perf_push(PERF_EVENT_LOOP);
+
+ /* wait on tun/socket list */
+ multi_get_timeo= ut(multi, &multi->top.c2.timeval);
+ status =3D multi_io_wait(multi);
+ MULTI_CHECK_SIG(multi);<= /span>
+
+ /* check on status of coarse ti= mers */
+ mult= i_process_per_second_timers(multi);
+
+ = /* timeout? */
+= if (status > 0)
+ {
+ = /* process the I/O which triggered select */
+ multi_io_process_io(multi);
+ MULTI_CHE= CK_SIG(multi);
+ = }
+ else if= (status =3D=3D 0)
+ = {
+ = multi_io_action(multi, NULL, TA_TIMEOUT, false);
+ }
+
= + perf_pop();
= + }
+}
<= span>

/*
* Top level event loop.=
@@ -4178,12 +4217,53 @@
{
A= SSERT(top->options.mode =3D=3D MODE_SERVER);
- if (proto_is_dgram(top->= options.ce.proto))
+ = struct multi_context multi;
+
+ top-= >mode =3D CM_TOP;
= + context_clear_2(top);
+
+ /* initi= alize top-tunnel instance */
+ init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_= HUP);
+ if (IS_SIG= (top))
{
- tunnel_server_udp(top);
+ return;
}
- else
+
+ /* initialize global multi_context object */
+ multi_init(&multi, top);=
+
+ /* initialize our cloned top object */

+ multi_top_init(&am= p;multi, top);
+
+ /* initialize manage= ment interface */
+ = init_management_callback_multi(&multi);
+
+ /* finished with initialization */
+ initialization_sequence_completed(top, ISC_SE= RVER); /* --mode server --proto tcp-server */
+
+#ifdef ENABLE_ASYNC_PUSH
+ multi.top.c2.inotify_fd =3D inotify_init();
+ if (multi.top.c2.inotify_fd <= ; 0)

{
- tunnel_server_tcp(top);
+ msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotif= y_init error");
}
+#endif
+
+ tunn= el_server_loop(&multi);
+
+ #ifdef = ENABLE_ASYNC_PUSH
+ = close(top->c2.inotify_fd);
+#endif
+<= /span>
+ /* shut down man= agement interface */
= + uninit_management_callback();
+
+ = /* save ifconfig-pool */
+ multi_ifconfig_pool_persist(&multi, true);
+
+ /* tear down tunnel instance (unless --persist-tun) */=
+ multi_uninit(&a= mp;multi);
+ multi= _top_free(&multi);
+
}
diff --git a/src/openvpn/multi.= h b/src/openvpn/multi.h
index eacfb52..4104706 100644
--- a/src/openvpn/multi.h
+++ b/src/openvpn/mult= i.h
@@ -204,6 +204,7 @@

= struct buffer hmac_reply;
struct link_socket_actua= l *hmac_reply_dest;
+= struct link_socket *hmac_reply_ls;

= /*
* Timer object for stale route check
= @@ -251,11 +252,6 @@
* Main event loop for OpenVPN = in server mode.
* @ingroup eventloop
*<= /span>
- * This function calls= the appropriate main event loop function depending
- * on the transport protocol used:- * - \c tunnel_server_udp()
- * - \c tunnel_server_= tcp()
- *
* @param top - Top-level context structure.

= */
void tunnel_server(struct context *top);
@@ -267,7 +263,7 @@

* Called by mtcp.c, mudp.c, or oth= er (to be written) protocol drivers
*/
<= /span>
-void multi_init(struct= multi_context *m, struct context *t, bool tcp_mode);
+void multi_init(struct multi_context *m, = struct context *t);

void multi_uninit(st= ruct multi_context *m);

diff --git a/src/= openvpn/multi_io.c b/src/openvpn/multi_io.c
index e4174dd..= f1751ff 100644
--- a/src/openvpn/multi_io.c
+++ b/src/openvpn/multi_io.c

@@ -133,6 +133,33 @@ }

void
+multi_io_set_global_rw_flags(struct multi_co= ntext *m, struct multi_instance *mi)
+{
+ = if (!mi)
+ {
+ return;=
+ }
+
+ mi->socket_set_called =3D true;
+ if (proto_is_dgram(mi->context.c2= .link_sockets[0]->info.proto))
+ {
+= socket_set(mi->context.c2.link_sockets[0],
+ m->multi_io->es,=
+ = EVENT_READ,
+ = &mi->context.c2.link_sockets[0]->ev_arg,
+ NULL);
<= br>+ }
+ else
+ {
+ socket_set(mi->context.c2.link_sockets[0],
+ m->multi_i= o->es,

+ = mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE : EVENT_R= EAD,
+ = &mi->ev_arg,
+ &mi->tcp_rwflags);
+ }
+}
+
+void
= multi_io_free(struct multi_io *multi_io)
{
if (multi_io)

@@ -155,6 +182,11 @@
= &m->top.c2.link_sockets[i]->e= v_arg);
}

+ if (has_udp_in_local_list(&m->top.o= ptions))
+ {
+ get_io_flags_udp(= &m->top, m->multi_io, p2mp_iow_flags(m));
+ }
+
#ifdef _WIN32
if= (tuntap_is_wintun(m->top.c1.tuntap))
{
<= span>@@ -333,7 +365,7 @@


case TA= _INITIAL:
ASSERT(mi);
- multi_tcp_set_global_rw_flags(m, m= i);
+ mult= i_io_set_global_rw_flags(m, mi);
multi_process= _post(m, mi, mpp_flags);
break;

@@ -383,7 +415,7 @@
}
else
{
- multi_tcp_set_global= _rw_flags(m, mi);

+ = multi_io_set_global_rw_flags(m, mi);
= }
break;

@@ -442,24 +474,31 @@

}
break;


- /* new incoming TCP client atte= mpting to connect? */
case EVENT_ARG_LINK_= SOCKET:
if (!ev_arg->u.sock)=
{
m= sg(D_MULTI_ERRORS, "MULTI IO: multi_io_proc_io: null socket");
break;
= }
-
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* new inco= ming TCP client attempting to connect? */

= if (!proto_is_dgram(ev_arg->u.sock->info.proto))
= {
socket_rese= t_listen_persistent(ev_arg->u.sock);
= mi =3D multi_create_instance_tcp(m, ev_arg->u.sock);
- if (mi)
- {=
- = multi_io_action(m, mi, TA_INITIAL, false);
- }
- break;
}

+ else
+ {
+ multi_process_io_udp(m, ev_arg->u.= sock);
+ = mi =3D m->pending;
+ }
+ /* monitor and/or handle events that are=
+ = * triggered in succession by the first one
+ * before returning to the mai= n loop. */
+ = if (mi)
+ = {
= + multi_io_action(m, mi, TA_INITIAL, false);<= br>+ }
+ break= ;
}
}
else

diff --git a/src/openvpn/multi_io.h b/src/op= envpn/multi_io.h
index 03d708c..0887a5e 100644
--- a/src/openvpn/multi_io.h

+++ b/src/openvpn/multi_io= .h
@@ -70,6 +70,8 @@

voi= d multi_io_process_io(struct multi_context *m);
+void multi_io_set_global_rw_f= lags(struct multi_context *m, struct multi_instance *mi);
+
void multi_io_actio= n(struct multi_context *m, struct multi_instance *mi, int action, bool poll= );

void multi_io_delete_event(struct mul= ti_io *multi_io, event_t event);
diff --git a/src/openvpn/o= penvpn.h b/src/openvpn/openvpn.h
index df43bba..0bbd1a4 100= 644
--- a/src/openvpn/openvpn.h
+++ b/src/o= penvpn/openvpn.h
@@ -216,8 +216,8 @@
* \c= SIGUSR1 restarts.
*
* This structure i= s initialized at the top of the \c
- * tunnel_point_to_point(), \c tunnel_server_udp(), and \c
- * tunnel_server_tcp() fu= nctions. In other words, it is reset for every
+ * tunnel_point_to_point() and \c tunnel_server= () \c
+ * functions. = In other words, it is reset for every
* iteration of the= \c main() function's inner \c SIGUSR1 loop.
*/
struct context_2
diff --git a/src/openvpn/optio= ns.c b/src/openvpn/options.c
index 6b2dfa5..511ce05 100644<= /span>
--- a/src/openvpn/options.c
+++ b/src/openv= pn/options.c
@@ -999,10 +999,6 @@
= const struct connection_entry *e,
= const int i)
{
- setenv_str_i(es, "proto", proto2asc= ii(e->proto, e->af, false), i);
- /* expected to be for single socket contexts only */
- setenv_str_i(es, &qu= ot;local", e->local_list->array[0]->local, i);
- setenv_str_i(es, "local_por= t", e->local_list->array[0]->port, i);

s= etenv_str_i(es, "remote", e->remote, i);
= setenv_str_i(es, "remote_port", e->remote_port, i);
=
@@ -1018,6 +1014,16 @@
}
}

+static void
+setenv_local_entry(struct env_set *es,
+ const struct local_entry *e,
+ const= int i)
+{
= + setenv_str_i(es, "pro= to", proto2ascii(e->proto, AF_UNSPEC, false), i);
+ setenv_str_i(es, "local"= ;, e->local, i);
+= setenv_str_i(es, "local_port", e->port, i);
+}

+
void
setenv_setti= ngs(struct env_set *es, const struct options *o)
{<= br>@@ -1041,6 +1047,14 @@
setenv_connection_= entry(es, &o->ce, 1);
}

+ if (o->ce.local_li= st)
+ {
= + for (int i =3D 0; i &l= t; o->ce.local_list->len; i++)
+ {
+ setenv_local_entry(es, o->ce.local_list->array[i],= i+1);
+ }
+ }
+
if (!o->pull= )
{
setenv_dns_options(&o= ->dns_options, es);
@@ -1725,12 +1739,17 @@
static void

show_connection_entry(const struct connec= tion_entry *o)
{
- msg(D_SHOW_PARMS, " proto =3D %s", proto2ascii(o= ->proto, o->af, false));
+ /* Display the global proto only in client mode or with no &= #39;--local'*/
+ = if (o->local_list->len =3D=3D 1)
+ {
+ msg(D_SHOW_PARMS, " proto =3D %s", proto2ascii(o= ->proto, o->af, false));
+ }
+
msg(D_SHOW_PARMS, " Local Sockets:");<= br> for (int i =3D 0; i < o->local_list->len; i++)
{
- = msg(D_SHOW_PARMS, " [%s]:%s", o->local_list->array[i= ]->local,
- = o->local_list->array[i]->port);
+ msg(D_SHOW_PARMS, " [%s]:%s-%s"= ;, o->local_list->array[i]->local,
+ o->local_list->array[i]->port,= proto2ascii(o->local_list->array[i]->proto, o->af, false));
}
SHOW_STR(remote);
SHOW_STR(remote_port);

@@ -2205,6 +2224,7 @@ }

ALLOC_OBJ_CLEAR_GC(e,= struct local_entry, gc);
+ e->proto =3D PROTO_NONE;
l->array[l-&g= t;len++] =3D e;

return e;
= @@ -2471,7 +2491,7 @@
{
= struct local_entry *le =3D ce->local_list->array[i];

- if (proto_i= s_net(ce->proto)
+= if (proto_is_net(le->proto)
&&a= mp; string_defined_equal(le->local, ce->remote)
= && string_defined_equal(le->port, ce->remote_port))
{
@@ -3176,14 +3196,16 @@ if (ce->proto =3D=3D PROTO_TCP)
= {
ce->proto =3D PROTO_TCP_SERVER;+ o->ce.proto = =3D ce->proto;
}
}<= br>
- if (o-&= gt;client)
+ if (o= ->mode !=3D MODE_SERVER)
{
= if (ce->proto =3D=3D PROTO_TCP)
{
ce->proto =3D PROTO_TCP_CLIENT;

+ o->ce.proto =3D ce->pro= to;
}
}

@@ -3325,13 +3347,19 @@
}

static void
-options_postprocess_mutate_le(struct connection_entry *ce, struct loc= al_entry *le)
+option= s_postprocess_mutate_le(struct connection_entry *ce, struct local_entry *le= , int mode)
{
/* use the global port = if none is specified */
if (!le->port)
{

le->port =3D ce->local_port;
}
+ = /* use the global proto if none is specified and
+ * allow proto bindings on server mod= e only */
+ if (!l= e->proto || mode =3D=3D MODE_POINT_TO_POINT)
+ {
+ le->proto =3D ce->proto;
+ }
}
=
#ifdef _WIN32
@@ -3779,7 +3807,19 @@
{
for (i =3D 0; i < o->ce.= local_list->len; i++)
{
- options_postprocess_mutate_le(&= amp;o->ce, o->ce.local_list->array[i]);
+ options_postprocess_mutate_le(&= ;o->ce, o->ce.local_list->array[i], o->mode);
+ }
+
+ for (int i =3D 0; i < o->ce.local_list->len; i++)=
+ {+ if (o->ce.loca= l_list->array[i]->proto =3D=3D PROTO_TCP)
+ {
+ o->ce.local_list->array[i]->= proto =3D PROTO_TCP_SERVER;
+ }
+ else if (o->ce.local_list->array[i]->proto =3D=3D PR= OTO_NONE)
+ = {
+ = o->ce.local_list->array[i]->proto =3D o->ce.proto;
+ }

= }
}
else
@= @ -3789,6 +3829,7 @@
struct local_entry *e =3D all= oc_local_entry(&o->ce, M_USAGE, &o->gc);
= ASSERT(e);
e->port =3D o->ce.local_port= ;
+ e->prot= o =3D o->ce.proto;
}

/* use the same listen list for every outgoing connection */
@@ -6191,7 +6232,7 @@
VERIFY_PERMISSION= (OPT_P_UP);
options->ifconfig_nowarn =3D true;<= /span>
}
= - else if (streq(p[0], "local") && p[1] && !p[= 3])
+ else if (str= eq(p[0], "local") && p[1] && !p[4])
{

struct local_entry *e;
=
@@ -6212,6 +6253,11 @@
{ e->port =3D p[2];
}=
+
+ if (p[3])
+ {
+ e->proto =3D ascii2proto(p[3]);
= + }
= }
else if (streq(p[0], "remote-random") &am= p;& !p[1])
{
@@ -9621,3 +9667,37 @= @
err:
gc_free(&gc);
}

+
+bool

+has_udp_in_local_list(const struct options *opt= ions)
+{
+ if (options->ce.local_lis= t)

+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ for (int i =3D 0; i <= ; options->ce.local_list->len; i++)

+ {
+ if (proto_is_dgram(options->ce.local_list->ar= ray[i]->proto))
+ = {
+ = return true;
+ }
+ = }
+ }+
+ return false;
+}
+
+bool=
+has_tcp_in_local_list(cons= t struct options *options)
+{
+ if (opt= ions->ce.local_list)
+ {
+ fo= r (int i =3D 0; i < options->ce.local_list->len; i++)
+ {

+ if (!proto_is_dgram(options-&= gt;ce.local_list->array[i]->proto))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;=
+}
diff --g= it a/src/openvpn/options.h b/src/openvpn/options.h
index 3f= 4cb90..161899a 100644
--- a/src/openvpn/options.h+++ b/src/openvpn/options.h
@@ -98,6 +98,7 @@=
{
const char *local;
c= onst char *port;
+ = int proto;
};

struct c= onnection_entry
@@ -917,6 +918,10 @@

bool key_is_external(const struct options *options);
+bool has_ud= p_in_local_list(const struct options *options);
+
+bool has_tcp_in_local_list(const struct options *options);<= br>+
/**
* Returns whether the current configuration has dco enabled.
*/
diff --git a/src/openvpn/socket.c b/src= /openvpn/socket.c
index 5b32885..d8cb9f5 100644
<= span>--- a/src/openvpn/socket.c

+++ b/src/openvpn/socket.c<= /span>
@@ -1880,11 +1880,31 @@
struct options= *o =3D &c->options;
ASSERT(sock);

+ const char= *host =3D o->ce.local_list->array[sock_index]->local;
<= span style=3D"color: hsl(120, 100%, 40%);">+ const char *port =3D o->= ce.local_list->array[sock_index]->port;

+ int proto =3D o->ce.local_list->array[s= ock_index]->proto;
const char *remote_host =3D o-&g= t;ce.remote;
const char *remote_port =3D o->ce.remo= te_port;

- sock->local_host =3D o->ce.local_list->array[sock_index]-= >local;
- sock-&g= t;local_port =3D o->ce.local_list->array[sock_index]->port;=
+ if (c->mode =3D=3D = CM_CHILD_TCP || c->mode =3D=3D CM_CHILD_UDP)
+ {
+ struct link_socket *tmp_sock =3D NULL;
+ if (c->mode =3D=3D CM_C= HILD_TCP)

+ {<= /span>
+ tmp_sock= =3D (struct link_socket *)c->c2.accept_from;
+ }
+ else if (c->mode =3D=3D CM_CHILD_UDP)<= br>+ {
+ tmp_sock =3D c->c2.lin= k_sockets[0];
+ = }
+
+ host =3D tmp_sock->local= _host;

+ port = =3D tmp_sock->local_port;
+ proto =3D tmp_sock->info.proto;
+ }
+
= + sock->local_host =3D host;
+ sock->local_port =3D port;
sock-= >remote_host =3D remote_host;
sock->remote_port = =3D remote_port;
sock->dns_cache =3D c->c1.dns_c= ache;
@@ -1912,7 +1932,7 @@

sock->mark =3D o->mark;

sock->bind_de= v =3D o->bind_dev;
-= sock->info.proto =3D o->ce.proto;
+ sock->info.proto =3D proto;
= sock->info.af =3D o->ce.af;
sock->info.r= emote_float =3D o->ce.remote_float;
sock->info.l= sa =3D &c->c1.link_socket_addrs[sock_index];
diff --= git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h
i= ndex 9625a99..6916ad4 100644
--- a/src/openvpn/ssl_common.h=
+++ b/src/openvpn/ssl_common.h
@@ -362,7 += 362,6 @@

int replay_window; = /* --replay-window parm */
int replay_time; = /* --replay-window parm */
- bool tcp_mode;

= const char *config_ciphername;
const char *config_= ncp_ciphers;

To view, visit change 764. To u= nsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I31bbf87e4e568021445c7512ece= fadfd4a69b363
Gerrit-Change-Number: 764
Gerrit-PatchSet: 22
Gerrit-Owner: its_Giaan <gianmarco@...2786...= ..>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-CC: ordex <a@...2181...>
Gerrit-MessageType: merged
--XKhoRdsg/0w=--