From: "its_Giaan (Code Review)" Date: Wed, 23 Oct 2024 11:04:53 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="kUDBIBwsftI="; charset=UTF-8 Subject: [Openvpn-devel] [XL] Change in openvpn[master]: multiproto: move generic event handling code in dedicated files List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --kUDBIBwsftI= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: its_Giaan, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/763?usp=3Demail to look at the new patch set (#6). Change subject: multiproto: move generic event handling code in dedicated f= iles ...................................................................... multiproto: move generic event handling code in dedicated files Introduced multi_io.h and multi_io.c files to centralize all codes related to multiple protocols. Renamed the struct mtcp to struct multi_protocol since it encompasses the event_set used by the parent context in server mode. Several methods have also been renamed and moved to fit the multiproto structure: - multi_tcp_init() -> multi_protocol_init(); - multi_tcp_free() -> multi_protocol_free(); - multi_tcp_wait() -> multi_protocol_io_wait(); and so forth. Change-Id: I1e5a84969988e4f027a18658d4ab268c13fbf929 Signed-off-by: Gianmarco De Gregori --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/forward.h M src/openvpn/init.c M src/openvpn/init.h 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 A src/openvpn/multi_io.c A src/openvpn/multi_io.h 13 files changed, 765 insertions(+), 632 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/63/763/6 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5db207d..d5202da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -458,6 +458,8 @@ src/openvpn/mudp.h src/openvpn/multi.c src/openvpn/multi.h + src/openvpn/multi_io.h + src/openvpn/multi_io.c src/openvpn/ntlm.c src/openvpn/ntlm.h src/openvpn/occ.c diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index ecb2bcf..4436dbc 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -92,6 +92,7 @@ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ + multi_io.c multi_io.h \ networking_freebsd.c \ networking_iproute2.c networking_iproute2.h \ networking_sitnl.c networking_sitnl.h \ diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index 5c2fdb8..924fb17 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -50,6 +50,7 @@ #include "openvpn.h" #include "occ.h" #include "ping.h" +#include "multi_io.h" #define IOW_TO_TUN (1<<0) #define IOW_TO_LINK (1<<1) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 771f6fe..53fb682 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -4896,7 +4896,7 @@ void inherit_context_child(struct context *dest, const struct context *src, - struct link_socket *ls) + struct link_socket *sock) { CLEAR(*dest); @@ -4935,7 +4935,7 @@ * The CM_TOP context does the socket listen(), * and the CM_CHILD_TCP context does the accept(). */ - dest->c2.accept_from =3D ls; + dest->c2.accept_from =3D sock; } #ifdef ENABLE_PLUGIN @@ -4969,13 +4969,13 @@ &dest->gc); /* inherit parent link_socket and tuntap */ - dest->c2.link_sockets[0] =3D ls; + dest->c2.link_sockets[0] =3D sock; ALLOC_ARRAY_GC(dest->c2.link_socket_infos, struct link_socket_info= *, 1, &dest->gc); ALLOC_OBJ_GC(dest->c2.link_socket_infos[0], struct link_socket_inf= o, &dest->gc); - *dest->c2.link_socket_infos[0] =3D ls->info; + *dest->c2.link_socket_infos[0] =3D sock->info; /* locally override some link_socket_info fields */ dest->c2.link_socket_infos[0]->lsa =3D &dest->c1.link_socket_addrs= [0]; diff --git a/src/openvpn/init.h b/src/openvpn/init.h index 11c32ac..50b38e3 100644 --- a/src/openvpn/init.h +++ b/src/openvpn/init.h @@ -96,7 +96,7 @@ void inherit_context_child(struct context *dest, const struct context *src, - struct link_socket *ls); + struct link_socket *sock); void inherit_context_top(struct context *dest, const struct context *src); diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index b5bbf13..1dae184 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -29,6 +29,8 @@ #include "multi.h" #include "forward.h" +#include "mtcp.h" +#include "multi_io.h" #include "memdbg.h" @@ -36,30 +38,6 @@ #include #endif -/* - * TCP States - */ -#define TA_UNDEF 0 -#define TA_SOCKET_READ 1 -#define TA_SOCKET_READ_RESIDUAL 2 -#define TA_SOCKET_WRITE 3 -#define TA_SOCKET_WRITE_READY 4 -#define TA_SOCKET_WRITE_DEFERRED 5 -#define TA_TUN_READ 6 -#define TA_TUN_WRITE 7 -#define TA_INITIAL 8 -#define TA_TIMEOUT 9 -#define TA_TUN_WRITE_TIMEOUT 10 - -/* - * Special tags passed to event.[ch] functions - */ -#define MTCP_TUN ((void *)2) -#define MTCP_SIG ((void *)3) /* Only on Windows */ -#define MTCP_MANAGEMENT ((void *)4) -#define MTCP_FILE_CLOSE_WRITE ((void *)5) -#define MTCP_DCO ((void *)6) - struct ta_iow_flags { unsigned int flags; @@ -68,59 +46,14 @@ unsigned int sock; }; =20 -#ifdef ENABLE_DEBUG -static const char * -pract(int action) -{ - switch (action) - { - case TA_UNDEF: - return "TA_UNDEF"; - - case TA_SOCKET_READ: - return "TA_SOCKET_READ"; - - case TA_SOCKET_READ_RESIDUAL: - return "TA_SOCKET_READ_RESIDUAL"; - - case TA_SOCKET_WRITE: - return "TA_SOCKET_WRITE"; - - case TA_SOCKET_WRITE_READY: - return "TA_SOCKET_WRITE_READY"; - - case TA_SOCKET_WRITE_DEFERRED: - return "TA_SOCKET_WRITE_DEFERRED"; - - case TA_TUN_READ: - return "TA_TUN_READ"; - - case TA_TUN_WRITE: - return "TA_TUN_WRITE"; - - case TA_INITIAL: - return "TA_INITIAL"; - - case TA_TIMEOUT: - return "TA_TIMEOUT"; - - case TA_TUN_WRITE_TIMEOUT: - return "TA_TUN_WRITE_TIMEOUT"; - - default: - return "?"; - } -} -#endif /* ENABLE_DEBUG */ - -static struct multi_instance * -multi_create_instance_tcp(struct multi_context *m, struct link_socket *ls) +struct multi_instance * +multi_create_instance_tcp(struct multi_context *m, struct link_socket *soc= k) { struct gc_arena gc =3D gc_new(); struct multi_instance *mi =3D NULL; struct hash *hash =3D m->hash; - mi =3D multi_create_instance(m, NULL, ls); + mi =3D multi_create_instance(m, NULL, sock); if (mi) { struct hash_element *he; @@ -193,126 +126,42 @@ mbuf_free(mi->tcp_link_out_deferred); } -struct multi_tcp * -multi_tcp_init(int maxevents, int *maxclients) -{ - struct multi_tcp *mtcp; - const int extra_events =3D BASE_N_EVENTS; - - ASSERT(maxevents >=3D 1); - ASSERT(maxclients); - - ALLOC_OBJ_CLEAR(mtcp, struct multi_tcp); - mtcp->maxevents =3D maxevents + extra_events; - mtcp->es =3D event_set_init(&mtcp->maxevents, 0); - wait_signal(mtcp->es, MTCP_SIG); - ALLOC_ARRAY(mtcp->esr, struct event_set_return, mtcp->maxevents); - *maxclients =3D max_int(min_int(mtcp->maxevents - extra_events, *maxcl= ients), 1); - msg(D_MULTI_LOW, "MULTI: TCP INIT maxclients=3D%d maxevents=3D%d", *ma= xclients, mtcp->maxevents); - return mtcp; -} - void -multi_tcp_delete_event(struct multi_tcp *mtcp, event_t event) +multi_tcp_delete_event(struct multi_protocol *multi_io, event_t event) { - if (mtcp && mtcp->es) + if (multi_io && multi_io->es) { - event_del(mtcp->es, event); + event_del(multi_io->es, event); } } void -multi_tcp_free(struct multi_tcp *mtcp) +multi_tcp_dereference_instance(struct multi_protocol *multi_io, struct mul= ti_instance *mi) { - if (mtcp) + struct link_socket *sock =3D mi->context.c2.link_sockets[0]; + if (sock && mi->socket_set_called) { - event_free(mtcp->es); - free(mtcp->esr); - free(mtcp); - } -} - -void -multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_instan= ce *mi) -{ - struct link_socket *ls =3D mi->context.c2.link_sockets[0]; - if (ls && mi->socket_set_called) - { - event_del(mtcp->es, socket_event_handle(ls)); + event_del(multi_io->es, socket_event_handle(sock)); mi->socket_set_called =3D false; } - mtcp->n_esr =3D 0; + multi_io->n_esr =3D 0; } =20 -static inline void +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->mtcp->es, + m->multi_io->es, mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, &mi->ev_arg, &mi->tcp_rwflags); } } -static inline int -multi_tcp_wait(const struct context *c, - struct multi_tcp *mtcp) -{ - int status; - unsigned int *persistent =3D &mtcp->tun_rwflags; - - for (int i =3D 0; i < c->c1.link_sockets_num; i++) - { - socket_set_listen_persistent(c->c2.link_sockets[i], mtcp->es, - &c->c2.link_sockets[i]->ev_arg); - } - -#ifdef _WIN32 - if (tuntap_is_wintun(c->c1.tuntap)) - { - if (!tuntap_ring_empty(c->c1.tuntap)) - { - /* there is data in wintun ring buffer, read it immediately */ - mtcp->esr[0].arg =3D MTCP_TUN; - mtcp->esr[0].rwflags =3D EVENT_READ; - mtcp->n_esr =3D 1; - return 1; - } - persistent =3D NULL; - } -#endif - tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent); -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) - dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO); -#endif - -#ifdef ENABLE_MANAGEMENT - if (management) - { - management_socket_set(management, mtcp->es, MTCP_MANAGEMENT, &mtcp= ->management_persist_flags); - } -#endif - -#ifdef ENABLE_ASYNC_PUSH - /* arm inotify watcher */ - event_ctl(mtcp->es, c->c2.inotify_fd, EVENT_READ, MTCP_FILE_CLOSE_WRIT= E); -#endif - - status =3D event_wait(mtcp->es, &c->c2.timeval, mtcp->esr, mtcp->maxev= ents); - update_time(); - mtcp->n_esr =3D 0; - if (status > 0) - { - mtcp->n_esr =3D status; - } - return status; -} - -static inline struct context * +struct context * multi_tcp_context(struct multi_context *m, struct multi_instance *mi) { if (mi) @@ -325,7 +174,7 @@ } } -static bool +bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct mult= i_instance *mi, const unsigned int mpp_flags) { struct mbuf_item item; @@ -349,7 +198,7 @@ return ret; } -static bool +bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const= unsigned int mpp_flags) { struct multi_instance *mi =3D multi_process_outgoing_link_pre(m); @@ -393,417 +242,6 @@ return ret; } -static int -multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, co= nst int action, bool *tun_input_pending) -{ - struct context *c =3D multi_tcp_context(m, mi); - unsigned int looking_for =3D 0; - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_wait_lite a=3D%s mi=3D" ptr_= format, - pract(action), - (ptr_type)mi); - - tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */ - - switch (action) - { - case TA_TUN_READ: - looking_for =3D TUN_READ; - tun_input_pending =3D NULL; - io_wait(c, IOW_READ_TUN); - break; - - case TA_SOCKET_READ: - looking_for =3D SOCKET_READ; - tun_input_pending =3D NULL; - io_wait(c, IOW_READ_LINK); - break; - - case TA_TUN_WRITE: - looking_for =3D TUN_WRITE; - tun_input_pending =3D NULL; - c->c2.timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 = TUN/TAP driver hits this timeout */ - perf_push(PERF_PROC_OUT_TUN_MTCP); - io_wait(c, IOW_TO_TUN); - perf_pop(); - break; - - case TA_SOCKET_WRITE: - looking_for =3D SOCKET_WRITE; - io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE); - break; - - default: - msg(M_FATAL, "MULTI TCP: multi_tcp_wait_lite, unhandled action= =3D%d", action); - } - - if (tun_input_pending && (c->c2.event_set_status & TUN_READ)) - { - *tun_input_pending =3D true; - } - - if (c->c2.event_set_status & looking_for) - { - return action; - } - else - { - switch (action) - { - /* TCP socket output buffer is full */ - case TA_SOCKET_WRITE: - return TA_SOCKET_WRITE_DEFERRED; - - /* TUN device timed out on accepting write */ - case TA_TUN_WRITE: - return TA_TUN_WRITE_TIMEOUT; - } - - return TA_UNDEF; - } -} - -static struct multi_instance * -multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, con= st int action) -{ - const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_RECORD_TOUCH; - struct multi_instance *touched =3D mi; - m->mpp_touched =3D &touched; - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_dispatch a=3D%s mi=3D" ptr_f= ormat, - pract(action), - (ptr_type)mi); - - switch (action) - { - case TA_TUN_READ: - read_incoming_tun(&m->top); - if (!IS_SIG(&m->top)) - { - multi_process_incoming_tun(m, mpp_flags); - } - break; - - case TA_SOCKET_READ: - case TA_SOCKET_READ_RESIDUAL: - ASSERT(mi); - ASSERT(mi->context.c2.link_sockets); - ASSERT(mi->context.c2.link_sockets[0]); - set_prefix(mi); - read_incoming_link(&mi->context, mi->context.c2.link_sockets[0= ]); - clear_prefix(); - if (!IS_SIG(&mi->context)) - { - multi_process_incoming_link(m, mi, mpp_flags, - mi->context.c2.link_sockets[0]= ); - if (!IS_SIG(&mi->context)) - { - stream_buf_read_setup(mi->context.c2.link_sockets[0]); - } - } - break; - - case TA_TIMEOUT: - multi_process_timeout(m, mpp_flags); - break; - - case TA_TUN_WRITE: - multi_process_outgoing_tun(m, mpp_flags); - break; - - case TA_TUN_WRITE_TIMEOUT: - multi_process_drop_outgoing_tun(m, mpp_flags); - break; - - case TA_SOCKET_WRITE_READY: - ASSERT(mi); - multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags); - break; - - case TA_SOCKET_WRITE: - multi_tcp_process_outgoing_link(m, false, mpp_flags); - break; - - case TA_SOCKET_WRITE_DEFERRED: - multi_tcp_process_outgoing_link(m, true, mpp_flags); - break; - - case TA_INITIAL: - ASSERT(mi); - multi_tcp_set_global_rw_flags(m, mi); - multi_process_post(m, mi, mpp_flags); - break; - - default: - msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, unhandled action= =3D%d", action); - } - - m->mpp_touched =3D NULL; - return touched; -} - -static int -multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const i= nt action) -{ - struct context *c =3D multi_tcp_context(m, mi); - int newaction =3D TA_UNDEF; - -#define MTP_NONE 0 -#define MTP_TUN_OUT (1<<0) -#define MTP_LINK_OUT (1<<1) - unsigned int flags =3D MTP_NONE; - - if (TUN_OUT(c)) - { - flags |=3D MTP_TUN_OUT; - } - if (LINK_OUT(c)) - { - flags |=3D MTP_LINK_OUT; - } - - switch (flags) - { - case MTP_TUN_OUT|MTP_LINK_OUT: - case MTP_TUN_OUT: - newaction =3D TA_TUN_WRITE; - break; - - case MTP_LINK_OUT: - newaction =3D TA_SOCKET_WRITE; - break; - - case MTP_NONE: - if (mi && sockets_read_residual(c)) - { - newaction =3D TA_SOCKET_READ_RESIDUAL; - } - else - { - multi_tcp_set_global_rw_flags(m, mi); - } - break; - - default: - { - struct gc_arena gc =3D gc_new(); - msg(M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=3D%s fla= gs=3D%d", - multi_instance_string(mi, false, &gc), - flags); - gc_free(&gc); - break; - } - } - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_post %s -> %s", - pract(action), - pract(newaction)); - - return newaction; -} - -static void -multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int a= ction, bool poll) -{ - bool tun_input_pending =3D false; - - do - { - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_action a=3D%s p=3D%d", - pract(action), - poll); - - /* - * If TA_SOCKET_READ_RESIDUAL, it means we still have pending - * input packets which were read by a prior TCP recv. - * - * Otherwise do a "lite" wait, which means we wait with 0 timeout - * on I/O events only related to the current instance, not - * the big list of events. - * - * On our first pass, poll will be false because we already know - * that input is available, and to call io_wait would be redundant. - */ - if (poll && action !=3D TA_SOCKET_READ_RESIDUAL) - { - const int orig_action =3D action; - action =3D multi_tcp_wait_lite(m, mi, action, &tun_input_pendi= ng); - if (action =3D=3D TA_UNDEF) - { - msg(M_FATAL, "MULTI TCP: I/O wait required blocking in mul= ti_tcp_action, action=3D%d", orig_action); - } - } - - /* - * Dispatch the action - */ - struct multi_instance *touched =3D multi_tcp_dispatch(m, mi, actio= n); - - /* - * Signal received or TCP connection - * reset by peer? - */ - if (touched && IS_SIG(&touched->context)) - { - if (mi =3D=3D touched) - { - mi =3D NULL; - } - multi_close_instance_on_signal(m, touched); - } - - - /* - * If dispatch produced any pending output - * for a particular instance, point to - * that instance. - */ - if (m->pending) - { - mi =3D m->pending; - } - - /* - * Based on the effects of the action, - * such as generating pending output, - * possibly transition to a new action state. - */ - action =3D multi_tcp_post(m, mi, action); - - /* - * If we are finished processing the original action, - * check if we have any TUN input. If so, transition - * our action state to processing this input. - */ - if (tun_input_pending && action =3D=3D TA_UNDEF) - { - action =3D TA_TUN_READ; - mi =3D NULL; - tun_input_pending =3D false; - poll =3D false; - } - else - { - poll =3D true; - } - - } while (action !=3D TA_UNDEF); -} - -static void -multi_tcp_process_io(struct multi_context *m) -{ - struct multi_tcp *mtcp =3D m->mtcp; - int i; - - for (i =3D 0; i < mtcp->n_esr; ++i) - { - struct event_set_return *e =3D &mtcp->esr[i]; - - /* incoming data for instance or listening socket? */ - if (e->arg >=3D MULTI_N) - { - struct event_arg *ev_arg =3D (struct event_arg *)e->arg; - switch (ev_arg->type) - { - struct multi_instance *mi; - - /* react to event on child instance */ - case EVENT_ARG_MULTI_INSTANCE: - if (!ev_arg->u.mi) - { - msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null min= stance"); - break; - } - - mi =3D ev_arg->u.mi; - if (e->rwflags & EVENT_WRITE) - { - multi_tcp_action(m, mi, TA_SOCKET_WRITE_READY, fal= se); - } - else if (e->rwflags & EVENT_READ) - { - multi_tcp_action(m, mi, TA_SOCKET_READ, false); - } - break; - - /* new incoming TCP client attempting to connect? */ - case EVENT_ARG_LINK_SOCKET: - if (!ev_arg->u.sock) - { - msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null soc= ket"); - break; - } - - socket_reset_listen_persistent(ev_arg->u.sock); - mi =3D multi_create_instance_tcp(m, ev_arg->u.sock); - if (mi) - { - multi_tcp_action(m, mi, TA_INITIAL, false); - } - break; - } - } - else - { -#ifdef ENABLE_MANAGEMENT - if (e->arg =3D=3D MTCP_MANAGEMENT) - { - ASSERT(management); - management_io(management); - } - else -#endif - /* incoming data on TUN? */ - if (e->arg =3D=3D MTCP_TUN) - { - if (e->rwflags & EVENT_WRITE) - { - multi_tcp_action(m, NULL, TA_TUN_WRITE, false); - } - else if (e->rwflags & EVENT_READ) - { - multi_tcp_action(m, NULL, TA_TUN_READ, false); - } - } -#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) - /* incoming data on DCO? */ - else if (e->arg =3D=3D MTCP_DCO) - { - multi_process_incoming_dco(m); - } -#endif - /* signal received? */ - else if (e->arg =3D=3D MTCP_SIG) - { - get_signal(&m->top.sig->signal_received); - } -#ifdef ENABLE_ASYNC_PUSH - else if (e->arg =3D=3D MTCP_FILE_CLOSE_WRITE) - { - multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_T= OUCH); - } -#endif - } - if (IS_SIG(&m->top)) - { - break; - } - } - mtcp->n_esr =3D 0; - - /* - * Process queued mbuf packets destined for TCP socket - */ - { - struct multi_instance *mi; - while (!IS_SIG(&m->top) && (mi =3D mbuf_peek(m->mbuf)) !=3D NULL) - { - multi_tcp_action(m, mi, TA_SOCKET_WRITE, true); - } - } -} - /* * Top level event loop for single-threaded operation. * TCP mode. @@ -851,7 +289,7 @@ /* wait on tun/socket list */ multi_get_timeout(&multi, &multi.top.c2.timeval); - status =3D multi_tcp_wait(&multi.top, multi.mtcp); + status =3D multi_protocol_io_wait(&multi); MULTI_CHECK_SIG(&multi); /* check on status of coarse timers */ @@ -861,12 +299,12 @@ if (status > 0) { /* process the I/O which triggered select */ - multi_tcp_process_io(&multi); + multi_protocol_process_io(&multi); MULTI_CHECK_SIG(&multi); } else if (status =3D=3D 0) { - multi_tcp_action(&multi, NULL, TA_TIMEOUT, false); + multi_protocol_action(&multi, NULL, TA_TIMEOUT, false); } perf_pop(); diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.h index ab968e9..b9f73d4 100644 --- a/src/openvpn/mtcp.h +++ b/src/openvpn/mtcp.h @@ -30,34 +30,28 @@ #include "event.h" -/* - * Extra state info needed for TCP mode - */ -struct multi_tcp -{ - struct event_set *es; - struct event_set_return *esr; - int n_esr; - int maxevents; - unsigned int tun_rwflags; -#ifdef ENABLE_MANAGEMENT - unsigned int management_persist_flags; -#endif -}; - struct multi_instance; struct context; -struct multi_tcp *multi_tcp_init(int maxevents, int *maxclients); - -void multi_tcp_free(struct multi_tcp *mtcp); - -void multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_i= nstance *mi); +void multi_tcp_dereference_instance(struct multi_protocol *multi_io, struc= t multi_instance *mi); bool multi_tcp_instance_specific_init(struct multi_context *m, struct mult= i_instance *mi); void multi_tcp_instance_specific_free(struct multi_instance *mi); +struct context *multi_tcp_context(struct multi_context *m, struct multi_in= stance *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); + +struct multi_instance *multi_create_instance_tcp(struct multi_context *m, = struct link_socket *sock); + +void multi_tcp_link_out_deferred(struct multi_context *m, struct multi_ins= tance *mi); + + /*************************************************************************= */ /** * Main event loop for OpenVPN in TCP server mode. @@ -68,6 +62,6 @@ void tunnel_server_tcp(struct context *top); -void multi_tcp_delete_event(struct multi_tcp *mtcp, event_t event); +void multi_tcp_delete_event(struct multi_protocol *multi_io, event_t event= ); #endif /* ifndef MTCP_H */ diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index 9e915b2..11fff48 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -186,7 +186,7 @@ struct multi_instance * multi_get_create_instance_udp(struct multi_context *m, bool *floated, - struct link_socket *ls) + struct link_socket *sock) { struct gc_arena gc =3D gc_new(); struct mroute_addr real =3D {0}; @@ -257,7 +257,7 @@ * connect-freq but not against connect-freq-initial */ reflect_filter_rate_limit_decrease(m->initial_rate_lim= iter); - mi =3D multi_create_instance(m, &real, ls); + mi =3D multi_create_instance(m, &real, sock); if (mi) { hash_add_fast(hash, bucket, &mi->real, hv, mi); diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h index b378754..3107cc6 100644 --- a/src/openvpn/mudp.h +++ b/src/openvpn/mudp.h @@ -56,13 +56,13 @@ * successful, returns the newly created instance. * * @param m - The single multi_context structure. - * @param ls - Listening socket where this instance is connecting= to + * @param sock - Listening socket where this instance is connecting= to * * @return A pointer to a multi_instance if one already existed for the * packet's source address or if one was a newly created successfully. * NULL if one did not yet exist and a new one was not created. */ struct multi_instance *multi_get_create_instance_udp(struct multi_context = *m, bool *floated, - struct link_socket *l= s); + struct link_socket *s= ock); #endif /* ifndef MUDP_H */ diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index d9e6b1f..4fddd62 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -440,7 +440,7 @@ */ if (tcp_mode) { - m->mtcp =3D multi_tcp_init(t->options.max_clients, &m->max_clients= ); + m->multi_io =3D multi_protocol_init(t->options.max_clients, &m->ma= x_clients); } m->tcp_queue_limit =3D t->options.tcp_queue_limit; @@ -665,9 +665,9 @@ mi->did_iroutes =3D false; } - if (m->mtcp) + if (m->multi_io) { - multi_tcp_dereference_instance(m->mtcp, mi); + multi_tcp_dereference_instance(m->multi_io, mi); } =20 mbuf_dereference_instance(m->mbuf, mi); @@ -742,7 +742,7 @@ initial_rate_limit_free(m->initial_rate_limiter); multi_reap_free(m->reaper); mroute_helper_free(m->route_helper); - multi_tcp_free(m->mtcp); + multi_protocol_free(m->multi_io); } } =20 @@ -751,7 +751,7 @@ */ struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *r= eal, - struct link_socket *ls) + struct link_socket *sock) { struct gc_arena gc =3D gc_new(); struct multi_instance *mi; @@ -774,7 +774,7 @@ generate_prefix(mi); } - inherit_context_child(&mi->context, &m->top, ls); + inherit_context_child(&mi->context, &m->top, sock); if (IS_SIG(&mi->context)) { goto err; @@ -3126,7 +3126,7 @@ void multi_process_float(struct multi_context *m, struct multi_instance *mi, - struct link_socket *ls) + struct link_socket *sock) { struct mroute_addr real =3D {0}; struct hash *hash =3D m->hash; @@ -3182,7 +3182,7 @@ mi->context.c2.to_link_addr =3D &mi->context.c2.from; /* inherit parent link_socket and link_socket_info */ - mi->context.c2.link_sockets[0] =3D ls; + mi->context.c2.link_sockets[0] =3D sock; mi->context.c2.link_socket_infos[0]->lsa->actual =3D m->top.c2.from; tls_update_remote_addr(mi->context.c2.tls_multi, &mi->context.c2.from); @@ -3332,7 +3332,7 @@ */ bool multi_process_incoming_link(struct multi_context *m, struct multi_instance= *instance, const unsigned int mpp_flags, - struct link_socket *ls) + struct link_socket *sock) { struct gc_arena gc =3D gc_new(); @@ -3353,7 +3353,7 @@ #ifdef MULTI_DEBUG_EVENT_LOOP printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf)); #endif - multi_set_pending(m, multi_get_create_instance_udp(m, &floated, ls= )); + multi_set_pending(m, multi_get_create_instance_udp(m, &floated, so= ck)); } else { @@ -3394,7 +3394,7 @@ /* nonzero length means that we have a valid, decrypted pa= cked */ if (floated && c->c2.buf.len > 0) { - multi_process_float(m, m->pending, ls); + multi_process_float(m, m->pending, sock); } process_incoming_link_part2(c, lsi, orig_buf); @@ -3974,9 +3974,9 @@ management_delete_event(void *arg, event_t event) { struct multi_context *m =3D (struct multi_context *) arg; - if (m->mtcp) + if (m->multi_io) { - multi_tcp_delete_event(m->mtcp, event); + multi_tcp_delete_event(m->multi_io, event); } } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index a4cea79..bef0b73 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -37,6 +37,7 @@ #include "pool.h" #include "mudp.h" #include "mtcp.h" +#include "multi_io.h" #include "perf.h" #include "vlan.h" #include "reflect_filter.h" @@ -173,8 +174,8 @@ struct mbuf_set *mbuf; /**< Set of buffers for passing data * channel packets between VPN tunnel * instances. */ - struct multi_tcp *mtcp; /**< State specific to OpenVPN using TCP - * as external transport. */ + struct multi_protocol *multi_io; /**< State specific to OpenVPN us= ing TCP + * as external transport. */ struct ifconfig_pool *ifconfig_pool; struct frequency_limit *new_connection_limiter; struct initial_packet_rate_limit *initial_rate_limiter; @@ -275,7 +276,7 @@ void multi_top_free(struct multi_context *m); struct multi_instance *multi_create_instance(struct multi_context *m, cons= t struct mroute_addr *real, - struct link_socket *ls); + struct link_socket *sock); void multi_close_instance(struct multi_context *m, struct multi_instance *= mi, bool shutdown); @@ -290,7 +291,7 @@ * updates hashtables in multi_context. */ void multi_process_float(struct multi_context *m, struct multi_instance *m= i, - struct link_socket *ls); + struct link_socket *sock); #define MPP_PRE_SELECT (1<<0) #define MPP_CONDITIONAL_PRE_SELECT (1<<1) @@ -355,10 +356,10 @@ * when using TCP transport. Otherwise NULL, as is * the case when using UDP transport. * @param mpp_flags - Fast I/O optimization flags. - * @param ls - Socket where the packet was received. + * @param sock - Socket where the packet was received. */ bool multi_process_incoming_link(struct multi_context *m, struct multi_ins= tance *instance, const unsigned int mpp_flags, - struct link_socket *ls); + struct link_socket *sock); /** diff --git a/src/openvpn/multi_io.c b/src/openvpn/multi_io.c new file mode 100644 index 0000000..43fe24b --- /dev/null +++ b/src/openvpn/multi_io.c @@ -0,0 +1,619 @@ +/* + * 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-2023 OpenVPN Inc + * + * 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; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include "memdbg.h" + +#include "multi.h" +#include "forward.h" +#include "multi_io.h" + +#ifdef HAVE_SYS_INOTIFY_H +#include +#endif + +/* + * Special tags passed to event.[ch] functions + */ +#define MULTI_IO_SOCKET ((void *)1) +#define MULTI_IO_TUN ((void *)2) +#define MULTI_IO_SIG ((void *)3) /* Only on Windows */ +#define MULTI_IO_MANAGEMENT ((void *)4) +#define MULTI_IO_FILE_CLOSE_WRITE ((void *)5) +#define MULTI_IO_DCO ((void *)6) + +struct ta_iow_flags +{ + unsigned int flags; + unsigned int ret; + unsigned int tun; + unsigned int sock; +}; + +#ifdef ENABLE_DEBUG +static const char * +pract(int action) +{ + switch (action) + { + case TA_UNDEF: + return "TA_UNDEF"; + + case TA_SOCKET_READ: + return "TA_SOCKET_READ"; + + case TA_SOCKET_READ_RESIDUAL: + return "TA_SOCKET_READ_RESIDUAL"; + + case TA_SOCKET_WRITE: + return "TA_SOCKET_WRITE"; + + case TA_SOCKET_WRITE_READY: + return "TA_SOCKET_WRITE_READY"; + + case TA_SOCKET_WRITE_DEFERRED: + return "TA_SOCKET_WRITE_DEFERRED"; + + case TA_TUN_READ: + return "TA_TUN_READ"; + + case TA_TUN_WRITE: + return "TA_TUN_WRITE"; + + case TA_INITIAL: + return "TA_INITIAL"; + + case TA_TIMEOUT: + return "TA_TIMEOUT"; + + case TA_TUN_WRITE_TIMEOUT: + return "TA_TUN_WRITE_TIMEOUT"; + + default: + return "?"; + } +} +#endif /* ENABLE_DEBUG */ + +struct multi_protocol * +multi_protocol_init(int maxevents, int *maxclients) +{ + struct multi_protocol *multi_io; + const int extra_events =3D BASE_N_EVENTS; + + ASSERT(maxevents >=3D 1); + ASSERT(maxclients); + + ALLOC_OBJ_CLEAR(multi_io, struct multi_protocol); + multi_io->maxevents =3D maxevents + extra_events; + multi_io->es =3D event_set_init(&multi_io->maxevents, 0); + wait_signal(multi_io->es, MULTI_IO_SIG); + ALLOC_ARRAY(multi_io->esr, struct event_set_return, multi_io->maxevent= s); + *maxclients =3D max_int(min_int(multi_io->maxevents - extra_events, *m= axclients), 1); + msg(D_MULTI_LOW, "MULTI: TCP INIT maxclients=3D%d maxevents=3D%d", *ma= xclients, multi_io->maxevents); + return multi_io; +} + +void +multi_protocol_free(struct multi_protocol *multi_io) +{ + if (multi_io) + { + event_free(multi_io->es); + free(multi_io->esr); + free(multi_io); + } +} + +int +multi_protocol_io_wait(struct multi_context *m) +{ + int status, i; + unsigned int *persistent =3D &m->multi_io->tun_rwflags; + + for (i =3D 0; i < m->top.c1.link_sockets_num; i++) + { + socket_set_listen_persistent(m->top.c2.link_sockets[i], m->multi_i= o->es, + &m->top.c2.link_sockets[i]->ev_arg); + } + +#ifdef _WIN32 + if (tuntap_is_wintun(m->top.c1.tuntap)) + { + if (!tuntap_ring_empty(m->top.c1.tuntap)) + { + /* there is data in wintun ring buffer, read it immediately */ + m->multi_io->esr[0].arg =3D MULTI_IO_TUN; + m->multi_io->esr[0].rwflags =3D EVENT_READ; + m->multi_io->n_esr =3D 1; + return 1; + } + persistent =3D NULL; + } +#endif + tun_set(m->top.c1.tuntap, m->multi_io->es, EVENT_READ, MULTI_IO_TUN, p= ersistent); +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) + dco_event_set(&m->top.c1.tuntap->dco, m->multi_io->es, MULTI_IO_DCO); +#endif + +#ifdef ENABLE_MANAGEMENT + if (management) + { + management_socket_set(management, m->multi_io->es, MULTI_IO_MANAGE= MENT, &m->multi_io->management_persist_flags); + } +#endif + +#ifdef ENABLE_ASYNC_PUSH + /* arm inotify watcher */ + event_ctl(m->multi_io->es, m->top.c2.inotify_fd, EVENT_READ, MULTI_IO_= FILE_CLOSE_WRITE); +#endif + + status =3D event_wait(m->multi_io->es, &m->top.c2.timeval, m->multi_io= ->esr, m->multi_io->maxevents); + update_time(); + m->multi_io->n_esr =3D 0; + if (status > 0) + { + m->multi_io->n_esr =3D status; + } + return status; +} + +static int +multi_protocol_wait_lite(struct multi_context *m, struct multi_instance *m= i, const int action, bool *tun_input_pending) +{ + struct context *c =3D multi_tcp_context(m, mi); + unsigned int looking_for =3D 0; + + dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_protocol_wait_lite a=3D%s m= i=3D" ptr_format, + pract(action), + (ptr_type)mi); + + tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */ + + switch (action) + { + case TA_TUN_READ: + looking_for =3D TUN_READ; + tun_input_pending =3D NULL; + io_wait(c, IOW_READ_TUN); + break; + + case TA_SOCKET_READ: + looking_for =3D SOCKET_READ; + tun_input_pending =3D NULL; + io_wait(c, IOW_READ_LINK); + break; + + case TA_TUN_WRITE: + looking_for =3D TUN_WRITE; + tun_input_pending =3D NULL; + c->c2.timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 = TUN/TAP driver hits this timeout */ + perf_push(PERF_PROC_OUT_TUN_MTCP); + io_wait(c, IOW_TO_TUN); + perf_pop(); + break; + + case TA_SOCKET_WRITE: + looking_for =3D SOCKET_WRITE; + io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE); + break; + + default: + msg(M_FATAL, "MULTI PROTOCOL: multi_protocol_wait_lite, unhand= led action=3D%d", action); + } + + if (tun_input_pending && (c->c2.event_set_status & TUN_READ)) + { + *tun_input_pending =3D true; + } + + if (c->c2.event_set_status & looking_for) + { + return action; + } + else + { + switch (action) + { + /* MULTI PROTOCOL socket output buffer is full */ + case TA_SOCKET_WRITE: + return TA_SOCKET_WRITE_DEFERRED; + + /* TUN device timed out on accepting write */ + case TA_TUN_WRITE: + return TA_TUN_WRITE_TIMEOUT; + } + + return TA_UNDEF; + } +} + +static struct multi_instance * +multi_protocol_dispatch(struct multi_context *m, struct multi_instance *mi= , const int action) +{ + const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_RECORD_TOUCH; + struct multi_instance *touched =3D mi; + m->mpp_touched =3D &touched; + + dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_protocol_dispatch a=3D%s mi= =3D" ptr_format, + pract(action), + (ptr_type)mi); + + switch (action) + { + case TA_TUN_READ: + read_incoming_tun(&m->top); + if (!IS_SIG(&m->top)) + { + multi_process_incoming_tun(m, mpp_flags); + } + break; + + case TA_SOCKET_READ: + case TA_SOCKET_READ_RESIDUAL: + ASSERT(mi); + ASSERT(mi->context.c2.link_sockets); + ASSERT(mi->context.c2.link_sockets[0]); + set_prefix(mi); + read_incoming_link(&mi->context, mi->context.c2.link_sockets[0= ]); + clear_prefix(); + if (!IS_SIG(&mi->context)) + { + multi_process_incoming_link(m, mi, mpp_flags, + mi->context.c2.link_sockets[0]= ); + if (!IS_SIG(&mi->context)) + { + stream_buf_read_setup(mi->context.c2.link_sockets[0]); + } + } + break; + + case TA_TIMEOUT: + multi_process_timeout(m, mpp_flags); + break; + + case TA_TUN_WRITE: + multi_process_outgoing_tun(m, mpp_flags); + break; + + case TA_TUN_WRITE_TIMEOUT: + multi_process_drop_outgoing_tun(m, mpp_flags); + break; + + case TA_SOCKET_WRITE_READY: + ASSERT(mi); + multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags); + break; + + case TA_SOCKET_WRITE: + multi_tcp_process_outgoing_link(m, false, mpp_flags); + break; + + case TA_SOCKET_WRITE_DEFERRED: + multi_tcp_process_outgoing_link(m, true, mpp_flags); + break; + + case TA_INITIAL: + ASSERT(mi); + multi_tcp_set_global_rw_flags(m, mi); + multi_process_post(m, mi, mpp_flags); + break; + + default: + msg(M_FATAL, "MULTI PROTOCOL: multi_protocol_dispatch, unhandl= ed action=3D%d", action); + } + + m->mpp_touched =3D NULL; + return touched; +} + +static int +multi_protocol_post(struct multi_context *m, struct multi_instance *mi, co= nst int action) +{ + struct context *c =3D multi_tcp_context(m, mi); + int newaction =3D TA_UNDEF; + +#define MTP_NONE 0 +#define MTP_TUN_OUT (1<<0) +#define MTP_LINK_OUT (1<<1) + unsigned int flags =3D MTP_NONE; + + if (TUN_OUT(c)) + { + flags |=3D MTP_TUN_OUT; + } + if (LINK_OUT(c)) + { + flags |=3D MTP_LINK_OUT; + } + + switch (flags) + { + case MTP_TUN_OUT|MTP_LINK_OUT: + case MTP_TUN_OUT: + newaction =3D TA_TUN_WRITE; + break; + + case MTP_LINK_OUT: + newaction =3D TA_SOCKET_WRITE; + break; + + case MTP_NONE: + if (mi && sockets_read_residual(c)) + { + newaction =3D TA_SOCKET_READ_RESIDUAL; + } + else + { + multi_tcp_set_global_rw_flags(m, mi); + } + break; + + default: + { + struct gc_arena gc =3D gc_new(); + msg(M_FATAL, "MULTI PROTOCOL: multi_protocol_post bad state, m= i=3D%s flags=3D%d", + multi_instance_string(mi, false, &gc), + flags); + gc_free(&gc); + break; + } + } + + dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_protocol_post %s -> %s", + pract(action), + pract(newaction)); + + return newaction; +} + +void +multi_protocol_process_io(struct multi_context *m) +{ + struct multi_protocol *multi_io =3D m->multi_io; + int i; + + for (i =3D 0; i < multi_io->n_esr; ++i) + { + struct event_set_return *e =3D &multi_io->esr[i]; + struct event_arg *ev_arg =3D (struct event_arg *)e->arg; + + /* incoming data for instance or listening socket? */ + if (e->arg >=3D MULTI_N) + { + switch (ev_arg->type) + { + struct multi_instance *mi; + + /* react to event on child instance */ + case EVENT_ARG_MULTI_INSTANCE: + if (!ev_arg->u.mi) + { + msg(D_MULTI_ERRORS, "MULTI PROTOCOL: multi_proto_p= roc_io: null minstance"); + break; + } + + mi =3D ev_arg->u.mi; + if (e->rwflags & EVENT_WRITE) + { + multi_protocol_action(m, mi, TA_SOCKET_WRITE_READY= , false); + } + else if (e->rwflags & EVENT_READ) + { + multi_protocol_action(m, mi, TA_SOCKET_READ, false= ); + } + break; + + /* new incoming TCP client attempting to connect? */ + case EVENT_ARG_LINK_SOCKET: + if (!ev_arg->u.sock) + { + msg(D_MULTI_ERRORS, "MULTI PROTOCOL: multi_proto_p= roc_io: null socket"); + break; + } + + 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_protocol_action(m, mi, TA_INITIAL, false= ); + } + break; + } + } + } + else + { +#ifdef ENABLE_MANAGEMENT + if (e->arg =3D=3D MULTI_IO_MANAGEMENT) + { + ASSERT(management); + management_io(management); + } + else +#endif + /* incoming data on TUN? */ + if (e->arg =3D=3D MULTI_IO_TUN) + { + if (e->rwflags & EVENT_WRITE) + { + multi_protocol_action(m, NULL, TA_TUN_WRITE, false); + } + else if (e->rwflags & EVENT_READ) + { + multi_protocol_action(m, NULL, TA_TUN_READ, false); + } + } + /* new incoming TCP client attempting to connect? */ + else if (e->arg =3D=3D MULTI_IO_SOCKET) + { + struct multi_instance *mi; + ASSERT(m->top.c2.link_sockets[0]); + socket_reset_listen_persistent(m->top.c2.link_sockets[0]); + mi =3D multi_create_instance_tcp(m, m->top.c2.link_sockets= [0]); + if (mi) + { + multi_protocol_action(m, mi, TA_INITIAL, false); + } + } +#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) + /* incoming data on DCO? */ + else if (e->arg =3D=3D MULTI_IO_DCO) + { + multi_process_incoming_dco(m); + } +#endif + /* signal received? */ + else if (e->arg =3D=3D MULTI_IO_SIG) + { + get_signal(&m->top.sig->signal_received); + } +#ifdef ENABLE_ASYNC_PUSH + else if (e->arg =3D=3D MULTI_IO_FILE_CLOSE_WRITE) + { + multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_T= OUCH); + } +#endif + } + if (IS_SIG(&m->top)) + { + break; + } + } + multi_io->n_esr =3D 0; + + /* + * Process queued mbuf packets destined for TCP socket + */ + { + struct multi_instance *mi; + while (!IS_SIG(&m->top) && (mi =3D mbuf_peek(m->mbuf)) !=3D NULL) + { + multi_protocol_action(m, mi, TA_SOCKET_WRITE, true); + } + } +} + +void +multi_protocol_action(struct multi_context *m, struct multi_instance *mi, = int action, bool poll) +{ + bool tun_input_pending =3D false; + + do + { + dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_protocol_action a=3D%s = p=3D%d", + pract(action), + poll); + + /* + * If TA_SOCKET_READ_RESIDUAL, it means we still have pending + * input packets which were read by a prior recv. + * + * Otherwise do a "lite" wait, which means we wait with 0 timeout + * on I/O events only related to the current instance, not + * the big list of events. + * + * On our first pass, poll will be false because we already know + * that input is available, and to call io_wait would be redundant. + */ + if (poll && action !=3D TA_SOCKET_READ_RESIDUAL) + { + const int orig_action =3D action; + action =3D multi_protocol_wait_lite(m, mi, action, &tun_input_= pending); + if (action =3D=3D TA_UNDEF) + { + msg(M_FATAL, "MULTI PROTOCOL: I/O wait required blocking i= n multi_protocol_action, action=3D%d", orig_action); + } + } + + /* + * Dispatch the action + */ + struct multi_instance *touched =3D multi_protocol_dispatch(m, mi, = action); + + /* + * Signal received or connection + * reset by peer? + */ + if (touched && IS_SIG(&touched->context)) + { + if (mi =3D=3D touched) + { + mi =3D NULL; + } + multi_close_instance_on_signal(m, touched); + } + + + /* + * If dispatch produced any pending output + * for a particular instance, point to + * that instance. + */ + if (m->pending) + { + mi =3D m->pending; + } + + /* + * Based on the effects of the action, + * such as generating pending output, + * possibly transition to a new action state. + */ + action =3D multi_protocol_post(m, mi, action); + + /* + * If we are finished processing the original action, + * check if we have any TUN input. If so, transition + * our action state to processing this input. + */ + if (tun_input_pending && action =3D=3D TA_UNDEF) + { + action =3D TA_TUN_READ; + mi =3D NULL; + tun_input_pending =3D false; + poll =3D false; + } + else + { + poll =3D true; + } + + } while (action !=3D TA_UNDEF); +} + +void +multi_protocol_delete_event(struct multi_protocol *multi_io, event_t event) +{ + if (multi_io && multi_io->es) + { + event_del(multi_io->es, event); + } +} diff --git a/src/openvpn/multi_io.h b/src/openvpn/multi_io.h new file mode 100644 index 0000000..07fc935 --- /dev/null +++ b/src/openvpn/multi_io.h @@ -0,0 +1,77 @@ +/* + * 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-2023 OpenVPN Inc + * + * 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; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Multi-protocol specific code for --mode server + */ + +#ifndef MULTI_IO_H +#define MULTI_IO_H + +#include "event.h" + +/* + * I/O processing States + */ + +#define TA_UNDEF 0 +#define TA_SOCKET_READ 1 +#define TA_SOCKET_READ_RESIDUAL 2 +#define TA_SOCKET_WRITE 3 +#define TA_SOCKET_WRITE_READY 4 +#define TA_SOCKET_WRITE_DEFERRED 5 +#define TA_TUN_READ 6 +#define TA_TUN_WRITE 7 +#define TA_INITIAL 8 +#define TA_TIMEOUT 9 +#define TA_TUN_WRITE_TIMEOUT 10 + +/* + * Extra state info needed for Multi-protocol + */ +struct multi_protocol +{ + struct event_set *es; + struct event_set_return *esr; + int n_esr; + int maxevents; + unsigned int tun_rwflags; + unsigned int udp_flags; +#ifdef ENABLE_MANAGEMENT + unsigned int management_persist_flags; +#endif +}; + +struct multi_protocol *multi_protocol_init(int maxevents, int *maxclients); + +void multi_protocol_free(struct multi_protocol *multi_io); + +int multi_protocol_io_wait(struct multi_context *m); + +void multi_protocol_process_io(struct multi_context *m); + +void multi_protocol_action(struct multi_context *m, struct multi_instance = *mi, int action, bool poll); + +void multi_protocol_delete_event(struct multi_protocol *multi_io, event_t = event); + +#endif /* ifndef MULTI_IO_H */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/763?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: I1e5a84969988e4f027a18658d4ab268c13fbf929 Gerrit-Change-Number: 763 Gerrit-PatchSet: 6 Gerrit-Owner: its_Giaan Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: its_Giaan Gerrit-MessageType: newpatchset --kUDBIBwsftI= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: its_Giaan, plaisthos.

its_Giaan uploaded patch set #6 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/763?usp=3Demail">View Chang= e

multiproto: move generic event handling code in dedicated files
Introduced multi_io.h and multi_io.c files to centralize
all c= odes related to multiple protocols.

Renamed the struct mtcp to s= truct multi_protocol since it
encompasses the event_set used by the = parent context in server mode.

Several methods have also been re= named and moved to fit the
multiproto structure:

- multi_= tcp_init() -> multi_protocol_init();
- multi_tcp_free() -> mul= ti_protocol_free();
- multi_tcp_wait() -> multi_protocol_io_wait(= );
and so forth.

Change-Id: I1e5a84969988e4f027a18658d4ab268c= 13fbf929
Signed-off-by: Gianmarco De Gregori <gianmarco@...2726...>= ;
---
M CMakeLists.txt
M src/openvpn/Makefile.am
M src/openvpn/= forward.h
M src/openvpn/init.c
M src/openvpn/init.h
M src/openvpn/= mtcp.c
M src/openvpn/mtcp.h
M src/openvpn/mudp.c
M src/openvpn/mud= p.h
M src/openvpn/multi.c
M src/openvpn/multi.h
A src/openvpn/mult= i_io.c
A src/openvpn/multi_io.h
13 files changed, 765 insertions(+), = 632 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn =
refs/changes/63/763/6
diff --git a/CMakeLists.txt b/CMakeLists.txt<=
/span>
index 5db207d..d5202da 100644
--- a/CMakeLi= sts.txt
+++ b/CMakeLists.txt
@@ -458,6 +458= ,8 @@
src/openvpn/mudp.h
src/open= vpn/multi.c
src/openvpn/multi.h
+ src/openvpn/multi_io.h
+ src/openvpn/multi_io.c
<= br> src/openvpn/ntlm.c
src/openvpn/ntlm.h
src/openvpn/occ.c
diff --git a/src/openv= pn/Makefile.am b/src/openvpn/Makefile.am
index ecb2bcf..443= 6dbc 100644
--- a/src/openvpn/Makefile.am
+= ++ b/src/openvpn/Makefile.am
@@ -92,6 +92,7 @@
mtu.c mtu.h \

mudp.c mudp.h \
mul= ti.c multi.h \
+ mult= i_io.c multi_io.h \
networking_freebsd.c \
networking_iproute2.c networking_iproute2.h \
network= ing_sitnl.c networking_sitnl.h \
diff --git a/src/openvpn/f= orward.h b/src/openvpn/forward.h
index 5c2fdb8..924fb17 100= 644
--- a/src/openvpn/forward.h
+++ b/src/o= penvpn/forward.h
@@ -50,6 +50,7 @@
#includ= e "openvpn.h"
#include "occ.h"<= br> #include "ping.h"
+#include "multi_io.h"
<= br> #define IOW_TO_TUN (1<<0)
#define= IOW_TO_LINK (1<<1)
diff --git a/src/openvpn/= init.c b/src/openvpn/init.c
index 771f6fe..53fb682 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/i= nit.c
@@ -4896,7 +4896,7 @@
void inherit_context_child(struct context *dest,
= const struct context *src,
- struct link_socket *ls)+ struct= link_socket *sock)
{
CLEAR(*dest);

@@ -4935,7 +4935,7 @@
= * The CM_TOP context does the socket listen(),
= * and the CM_CHILD_TCP context does the accept().
= */
- dest-&= gt;c2.accept_from =3D ls;
+ dest->c2.accept_from =3D sock;
}


#ifdef ENABLE_PLUGIN
@@ -496= 9,13 +4969,13 @@
&dest->gc);=

/* inherit parent link_socket a= nd tuntap */
- d= est->c2.link_sockets[0] =3D ls;
+ dest->c2.link_sockets[0] =3D sock;

ALLOC_ARRAY_GC(dest->c2.link_socket_infos, = struct link_socket_info *, 1,
&= dest->gc);
ALLOC_OBJ_GC(dest->c2.link_socket= _infos[0], struct link_socket_info,
&= amp;dest->gc);
- = *dest->c2.link_socket_infos[0] =3D ls->info;
+ *dest->c2.link_socket_infos[0= ] =3D sock->info;

/* locally = override some link_socket_info fields */
dest->= c2.link_socket_infos[0]->lsa =3D &dest->c1.link_socket_addrs[0];<= /span>
diff --git a/src/openvpn/init.h b/src/openvpn/init.h=
index 11c32ac..50b38e3 100644
--- a/src/openvpn/i= nit.h
+++ b/src/openvpn/init.h
@@ -96,7 +96= ,7 @@

void inherit_context_child(struct = context *dest,
const struct con= text *src,
- = struct link_socket *ls);
+ struct link_socket *sock);<= /span>

void inherit_context_top(struct context = *dest,
const struct context *src)= ;
diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c

index b5bbf13..1dae184 100644
--- a/src/openvpn= /mtcp.c
+++ b/src/openvpn/mtcp.c
@@ -29,6 += 29,8 @@

#include "multi.h"
#include "forward.h"
+#include "mtcp.h"
+#include "multi_io.h"
#include "memdbg.h"

@@ -36,30 +38,6 @@
#include <sys/inotify= .h>
#endif

-/*
- * TCP States
-= */
-#define TA_UNDEF = 0
-#defi= ne TA_SOCKET_READ 1
-#define TA_SOCKET_READ_RESIDUAL 2
-#define TA_SOCKET_WRITE 3
-#define TA_SOCKET_WRITE_READY 4=
-#define TA_SOCKET_WRITE_DEFE= RRED 5
-#define TA_TUN_= READ 6
-#d= efine TA_TUN_WRITE 7
-#define TA_INITIAL 8
-#define TA_TIMEOUT 9
-#define TA_TUN_WRITE_TIMEOUT 10
-
-/*
- * Special tags passed to event.[ch] functions
- */

-#define MTCP_TUN ((void *)2)
-#define MTCP_SIG ((void *)3) /* = Only on Windows */
-#de= fine MTCP_MANAGEMENT ((void *)4)
-#define MTCP_FILE_CLOSE_WRITE ((void *)5)
-#define MTCP_DCO ((void *)6)<= br>-
struct ta_i= ow_flags
{
unsigned int flags;=
@@ -68,59 +46,14 @@
unsigned int sock;
};

-#ifdef ENABLE_DEBUG
-static const char *
-pract(int action)
- switch (actio= n)
- {
- case TA_UNDEF:

- return "TA_UNDEF&= quot;;

-
- case TA_SOCKET_READ:
<= br>- return "TA_S= OCKET_READ";
-
- case TA_SOCKET_RE= AD_RESIDUAL:
- = return "TA_SOCKET_READ_RESIDUAL";
-
- case TA_SOCKET_WRITE:
- return "TA_SOCKET_WRITE";
-

- case TA_SOCKET_WRITE_READY:
- return "TA_SOCKET_WRITE_RE= ADY";
-
= - case TA_SOCKET_WRITE_DEF= ERRED:
- ret= urn "TA_SOCKET_WRITE_DEFERRED";
-
- = case TA_TUN_READ:
-
- case TA_TUN_WRITE:
- return "TA_TUN_WRITE";
-
- case TA_INITIAL:
- return "TA_INITIAL";
-
- case TA_TIMEOUT:
- return "TA_TIMEOUT";
-
- case TA_TUN_WRITE_TIMEOUT:
- return "TA_TUN_WRITE_TIMEO= UT";
-
<= span style=3D"color: hsl(0, 100%, 40%);">- default:
- return "?";
- }
-}
-#endif /* ENABLE_DEBUG */
-
-stat= ic struct multi_instance *
-multi_create_instance_tcp(struct multi_context *m, struct link_socket = *ls)
+struct multi_in= stance *
+multi_creat= e_instance_tcp(struct multi_context *m, struct link_socket *sock) {
struct gc_arena gc =3D gc_new();
<= span> struct multi_instance *mi =3D NULL;

struct h= ash *hash =3D m->hash;

- mi =3D multi_create_instance(m, NULL, ls);=
+ mi =3D multi_create_in= stance(m, NULL, sock);
if (mi)
{<= /span>
struct hash_element *he;
@@ -193,1= 26 +126,42 @@
mbuf_free(mi->tcp_link_out_deferred);=
}

-struct multi_tcp *
-multi_tcp_init(int maxevents, int *maxclients)
-{

- struct multi_tcp *mtcp;
- const int extra_events =3D BASE_N_EVENTS;

-
- ASSERT(maxevents >=3D 1);
- ASSERT(maxclients);
-
- ALLOC_OBJ_CLEAR(mtcp, struct multi_tcp);
<= span style=3D"color: hsl(0, 100%, 40%);">- mtcp->maxevents =3D maxeve= nts + extra_events;
- = mtcp->es =3D event_set_init(&mtcp->maxevents, 0);
- wait_signal(mtcp->es, MTCP_S= IG);
- ALLOC_ARRAY(m= tcp->esr, struct event_set_return, mtcp->maxevents);
- *maxclients =3D max_int(min_int(mt= cp->maxevents - extra_events, *maxclients), 1);
- msg(D_MULTI_LOW, "MULTI: TCP INIT max= clients=3D%d maxevents=3D%d", *maxclients, mtcp->maxevents);=
- return mtcp;
<= span style=3D"color: hsl(0, 100%, 40%);">-}
-
void
-multi_tcp_delete_event(struct multi_tcp *mtcp, event= _t event)
+multi_tcp_= delete_event(struct multi_protocol *multi_io, event_t event)
{
- if (mtcp &= ;& mtcp->es)
+= if (multi_io && multi_io->es)
{<= br>- event_del(mtcp->es= , event);
+ ev= ent_del(multi_io->es, event);
}
}<= /span>

void
-multi_tcp_free(struct multi_tcp *mtcp)
+multi_tcp_dereference_instance(struct = multi_protocol *multi_io, struct multi_instance *mi)
{

- if (mtcp)
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct link_socket *sock = =3D mi->context.c2.link_sockets[0];
+ if (sock && mi->socket_set_called)=
{
- = event_free(mtcp->es);
- free(mtcp->esr);
- free(mtcp);
- }
-}
-
-void
-multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct mu= lti_instance *mi)
-{
- struct link_socket *= ls =3D mi->context.c2.link_sockets[0];
- if (ls && mi->socket_set_called)<= br>- {
- event_del(mtcp->es, socket_event= _handle(ls));
+ = event_del(multi_io->es, socket_event_handle(sock));
= mi->socket_set_called =3D false;
}- mtcp->n_esr =3D 0;

+ multi_io->n_esr = =3D 0;
}

-static inline void
+void
multi_tcp_set_global_rw_flags(st= ruct multi_context *m, struct multi_instance *mi)
{=
if (mi)
{
mi-&= gt;socket_set_called =3D true;
socket_set(mi->c= ontext.c2.link_sockets[0],
- m->mtcp->es,
+ m->multi_io->es,
= mbuf_defined(mi->tcp_link_out_deferred) ? EVEN= T_WRITE : EVENT_READ,
&mi->ev_ar= g,
&mi->tcp_rwflags);
= }
}

-static inline int
-multi_tcp_wait(const struct context *c,
<= span style=3D"color: hsl(0, 100%, 40%);">- struct multi_tcp *= mtcp)
-{
- int status;
- unsigned int *persistent =3D &mtcp= ->tun_rwflags;
-
- for (int i =3D 0; i &= lt; c->c1.link_sockets_num; i++)
- {
- = socket_set_listen_persistent(c->c2.link_sockets[i], mtcp->es,
- = &c->c2.link_sockets[i]->ev_arg);
- }
-
-#i= fdef _WIN32
- if (tu= ntap_is_wintun(c->c1.tuntap))
- {
- = if (!tuntap_ring_empty(c->c1.tuntap))
- {
- /* there is data in wintun ring buffer, read it immed= iately */
- = mtcp->esr[0].arg =3D MTCP_TUN;
- mtcp->esr[0].rwflags =3D EVENT_READ;
<= span style=3D"color: hsl(0, 100%, 40%);">- mtcp->n_esr =3D 1;=

- return 1;=
- }
<= span style=3D"color: hsl(0, 100%, 40%);">- persistent =3D NULL;

- }
-#endif
- tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTC= P_TUN, persistent);
-#i= f defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
- dco_event_set(&c->c1.tuntap->dco= , mtcp->es, MTCP_DCO);
-#endif
-
= -#ifdef ENABLE_MANAGEMENT<= br>- if (management)- {
- management_socket_set(management, mtcp-= >es, MTCP_MANAGEMENT, &mtcp->management_persist_flags);- }
-#endif
-
-#ifdef E= NABLE_ASYNC_PUSH
- /= * arm inotify watcher */
-#endif
-
- status =3D event_wait(mtcp->es, &c-= >c2.timeval, mtcp->esr, mtcp->maxevents);
- update_time();
- mtcp->n_esr =3D 0;
- if (status > 0)
- {
- mtcp->n_esr =3D status;
- }
- return status;
-}
-=
-static inline struct context= *
+struct context *<= /span>
multi_tcp_context(struct multi_context *m, struct multi_in= stance *mi)
{
if (mi)
@@ -325,7 +174,7 @@

}
}

-static bool
+bool
mu= lti_tcp_process_outgoing_link_ready(struct multi_context *m, struct multi_i= nstance *mi, const unsigned int mpp_flags)
{
struct mbuf_item item;

@@ -349,7 +198,7 @@ return ret;
}

-static bool
+bool
multi_tcp_process_o= utgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_fl= ags)
{
struct multi_instance *mi =3D = multi_process_outgoing_link_pre(m);
@@ -393,417 +242,6 @@
return ret;
}
=
-static int
-multi_tcp_wait_lite(struct multi_contex= t *m, struct multi_instance *mi, const int action, bool *tun_input_pending)=
-{
- struct context *c =3D multi_tcp_conte= xt(m, mi);
- unsigne= d int looking_for =3D 0;
- dmsg(D_MULTI_D= EBUG, "MULTI TCP: multi_tcp_wait_lite a=3D%s mi=3D" ptr_format,
- pract(action),=
- (ptr_type)mi= );
-
- tv_clear(&c->c2.timeval); /* = ZERO-TIMEOUT */
-
- switch (action)<= br>- {
- case TA_TUN_READ:
- looking_for =3D TUN_READ;
- tun_input_p= ending =3D NULL;
- = io_wait(c, IOW_READ_TUN);
- break;
-
- case = TA_SOCKET_READ:
- = looking_for =3D SOCKET_READ;
- tun_input_pending =3D NULL;
- io_wait(c, IOW_READ_LINK);
- break;<= br>-
- case TA_TUN_WRITE:
- looking_for =3D TUN_WRITE;
- tun_input_pend= ing =3D NULL;
- = c->c2.timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 TUN/TA= P driver hits this timeout */
- perf_push(PERF_PROC_OUT_TUN_MTCP);
- io_wait(c, IOW_TO_TUN);=
- perf_pop();
- break;<= br>-
- case TA_SOCKET_WRITE:
- looking_for =3D SOCKET_WRITE;<= /span>
- io_wait(c,= IOW_TO_LINK|IOW_READ_TUN_FORCE);
- break;
-
- defa= ult:
- msg(M= _FATAL, "MULTI TCP: multi_tcp_wait_lite, unhandled action=3D%d", = action);
- }<= br>-
- if (tun_input_pending && (c->c2.eve= nt_set_status & TUN_READ))
- {
- = *tun_input_pending =3D true;
- }
-<= br>- if (c->c2.event_set_st= atus & looking_for)
- {
- return= action;
- }<= br>- else
- {
- switch (action)
- {
- /* TCP socket output buffer is full */
- case TA_SOCKET_WRITE:
- return TA_S= OCKET_WRITE_DEFERRED;
-=
- /* TUN de= vice timed out on accepting write */
- case TA_TUN_WRITE:
- return TA_TUN_WRITE_TIMEOUT;=
- }
-
- return TA_UNDEF;
- }
= -}
-
-static struct multi_instance *-multi_tcp_dispatch(struct multi= _context *m, struct multi_instance *mi, const int action)
-{
- const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_REC= ORD_TOUCH;
- struct = multi_instance *touched =3D mi;
- m->mpp_touched =3D &touched;
-
- dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_dispatch a=3D%s m= i=3D" ptr_format,
= - pract(action),
-
- switch (acti= on)
- {
- case TA_TUN_READ:
- read_incoming_tun(= &m->top);
- = if (!IS_SIG(&m->top))
- {
- multi_process_incoming_tun(m, mpp_flags);
<= span style=3D"color: hsl(0, 100%, 40%);">- }

- break;
-
- case TA_SOCKET_READ:
- case TA_SOCKET_READ_RESIDUAL:
- ASSERT(mi);
- ASSERT(mi->context.c2.li= nk_sockets);
- = ASSERT(mi->context.c2.link_sockets[0]);
- set_prefix(mi);
- read_incoming_link(&mi->= context, mi->context.c2.link_sockets[0]);
- clear_prefix();
- if (!IS_SIG(&mi->context))<= /span>
- {- multi_process_= incoming_link(m, mi, mpp_flags,
- mi->context.c2.lin= k_sockets[0]);
- = if (!IS_SIG(&mi->context))
- {
- stream_buf_read_setup(mi->context.c= 2.link_sockets[0]);
- = }
- = }
- br= eak;
-
- case TA_TIMEOUT:
- multi_process_timeout(m= , mpp_flags);

- = break;
-
= - case TA_TUN_WRITE:
- multi_process_o= utgoing_tun(m, mpp_flags);
- break;
-
- case TA_TUN= _WRITE_TIMEOUT:
- = multi_process_drop_outgoing_tun(m, mpp_flags);
- break;
-
- case TA_SOCKET_WRITE_READY:
- ASSERT(mi);
- multi_tcp_process_outgoing_link_ready(m= , mi, mpp_flags);
- = break;
-=
- case TA_SOCKET_WRITE= :
- multi_tc= p_process_outgoing_link(m, false, mpp_flags);
- break;
-
-= case TA_SOCKET_WRITE_DEFERRED:
- multi_tcp_process_outgoing_link(m, true, mpp_f= lags);
- bre= ak;
-
- case TA_INITIAL:
- ASSERT(mi);

- multi_tcp_set_global_r= w_flags(m, mi);

- = multi_process_post(m, mi, mpp_flags);
- break;
-
- = default:
- = msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, unhandled action=3D%= d", action);
- = }
-
- m->mpp_touched =3D NULL;- return touched;
<= span style=3D"color: hsl(0, 100%, 40%);">-}

-
= -static int
-multi_tcp_= post(struct multi_context *m, struct multi_instance *mi, const int action)<= /span>
-{
- struct context *c =3D multi_tcp_contex= t(m, mi);
- int newa= ction =3D TA_UNDEF;
-
-#define MTP_NONE = 0
-#define MTP_TUN_OU= T (1<<0)
-#d= efine MTP_LINK_OUT (1<<1)
- unsigned int flags =3D MTP_NONE;
-
- if (TUN_OUT(c))
- {
- fl= ags |=3D MTP_TUN_OUT;
-= }
- if (LINK_OUT= (c))
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- flags |=3D MTP_LINK_OUT;<= /span>
- }
-
- switch (flags)
- {
- = case MTP_TUN_OUT|MTP_LINK_OUT:
- case MTP_TUN_OUT:
- newaction =3D TA_TUN_WRITE;
- break;
-
- case MTP_LINK_OUT:
- newaction =3D TA_SOCKET_WRITE;
- break;

-
- case MTP_NONE:
- if (mi && sockets_read_residual(c))
- {
- newaction =3D TA_S= OCKET_READ_RESIDUAL;

- = }
- = else
- {<= /span>
- multi_= tcp_set_global_rw_flags(m, mi);
- }
- break;
-<= /span>
- default:
- {
- struct gc_arena gc =3D gc_n= ew();
- msg(= M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=3D%s flags=3D%d"= ;,
- mul= ti_instance_string(mi, false, &gc),
- flags);
- gc_free(&gc);
- break;
- }
- }
-
- dmsg(D_MULTI_DEBUG, = "MULTI TCP: multi_tcp_post %s -> %s",
- pract(action),
- pract(newaction));
-

- return newaction;
-}
-
-static void
-multi_tcp_action(struct multi_cont= ext *m, struct multi_instance *mi, int action, bool poll)

-{
- bool tun_input_pending =3D false;
-
- do
- = {
- dmsg(D_MULTI= _DEBUG, "MULTI TCP: multi_tcp_action a=3D%s p=3D%d",
- pract(action),
=
- poll);-
- /*
- * If TA_SOCKET_READ_RESIDUAL, it means we still have = pending
- * inp= ut packets which were read by a prior TCP recv.
- *
- * Otherwise do a "lite" wait, which means= we wait with 0 timeout
- * on I/O events only related to the current instance, not=
- * the big list of e= vents.
- *
- * On our first pa= ss, poll will be false because we already know
- * that input is available, and to call io= _wait would be redundant.
- */
- = if (poll && action !=3D TA_SOCKET_READ_RESIDUAL)
- {
- const int orig_action =3D action;
- action =3D mul= ti_tcp_wait_lite(m, mi, action, &tun_input_pending);
- if (action =3D=3D TA_UNDEF)<= /span>
- {- msg(M_FATAL, &= quot;MULTI TCP: I/O wait required blocking in multi_tcp_action, action=3D%d= ", orig_action);
-= }
- = }
-
- /*
- * Dispatch the action
- */
- struct multi_instance *touched =3D multi_tcp_= dispatch(m, mi, action);
- /*<= br>- * Signal received or= TCP connection
- = * reset by peer?
- = */
- if (= touched && IS_SIG(&touched->context))
- {
- if (mi =3D=3D touched)
- {
- mi =3D NULL;
- }
- multi_close_instance_on_signal(m, touch= ed);
- }<= br>-
-
- /*
- = * If dispatch produced any pending output
- * for a particular instance, point to=
- * that instance.
- */
- if (m->pending)
<= br>- {
- mi =3D m->pending;<= br>- }
-
- /*
- * Based on the effects of the action,
- * such as generating pending output,
- * possibly tran= sition to a new action state.
- */
- = action =3D multi_tcp_post(m, mi, action);
-
- /*
- = * If we are finished processing the original action,
- * check if we have any TUN input= . If so, transition
- = * our action state to processing this input.
- */
- if (tun_input_pending && action =3D= =3D TA_UNDEF)
- = {
- action = =3D TA_TUN_READ;
- = mi =3D NULL;
- = tun_input_pending =3D false;
- poll =3D false;
- }
- else
- {
- = poll =3D true;
- = }
-
- } while (action !=3D TA_UNDEF);
-}
-
-static void
= -multi_tcp_process_io(struct multi_context *m)
-{
- struct multi_tcp *mtcp =3D m->mtcp;
- int i;
-
- fo= r (i =3D 0; i < mtcp->n_esr; ++i)
- {
- struct event_set_return *e =3D &mtcp->esr[i];
-

- /* incoming data for instance or listening sock= et? */
- if (e-&= gt;arg >=3D MULTI_N)
- {
- = struct event_arg *ev_arg =3D (struct event_arg *)e->arg;
- switch (ev_arg->type= )

- {=
- struct multi= _instance *mi;
-=
- /* react to = event on child instance */
- case EVENT_ARG_MULTI_INSTANCE:
- if (!ev_arg->u.mi)
- {
- = msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null minstance");
- br= eak;
- = }
-
- mi =3D ev_arg->= u.mi;
- = if (e->rwflags & EVENT_WRITE)
- {
- multi_tcp_action(m, mi, TA_SOCKE= T_WRITE_READY, false);
= - }
= - else if (e->rwflags & EVENT_READ)
- {

- multi_tcp_a= ction(m, mi, TA_SOCKET_READ, false);

- }
- break;
-
- = /* new incoming TCP client attempting to connect? */<= br>- case EVENT_AR= G_LINK_SOCKET:
- = if (!ev_arg->u.sock)
- {
- msg(D_MULTI_ERRORS, "MULTI: mtc= p_proc_io: null socket");
- break;
- }
-
- = socket_reset_listen_persistent(ev_arg->u.sock);<= br>- mi =3D mu= lti_create_instance_tcp(m, ev_arg->u.sock);
- if (mi)
- {
- multi_tcp_action(m,= mi, TA_INITIAL, false);
- }
-= }
- else=
- {
<= span style=3D"color: hsl(0, 100%, 40%);">-#ifdef ENABLE_MANAGEMENT
- if (e->arg =3D= =3D MTCP_MANAGEMENT)
- = {
- = ASSERT(management);
- management_io(management);
- }
- else
-#endif
- = /* incoming data on TUN? */
- if (e->arg =3D=3D MTCP_TUN)
- {
- if (e->rwflags & EVENT_= WRITE)
- = {
- = multi_tcp_action(m, NULL, TA_TUN_WRITE, false);
- }
- else if (e->rwflags & EVENT_R= EAD)
- {=
- m= ulti_tcp_action(m, NULL, TA_TUN_READ, false);
- }
- }
-#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || de= fined(TARGET_FREEBSD))
= - /* incoming data on DCO? */
- else if (e->arg =3D=3D MTCP_DCO)- {
- multi_process_incoming= _dco(m);
- }=
-#endif
- /* signal received? */
- else if (e-= >arg =3D=3D MTCP_SIG)
- = get_signal(&m->top.sig->signal_received);
- }

-#ifdef ENABLE_ASYNC_PUSH
- else if (e->arg =3D=3D M= TCP_FILE_CLOSE_WRITE)
-= {
- = multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_TOUCH);
- }
= -#endif
- }
- if (IS_SIG(&m->top))
- {
- break;
- }
- }
- mtcp-&g= t;n_esr =3D 0;
-=
- /*
- * Process queued mbuf packets destined= for TCP socket
- *= /
- {
- struct multi_instance *mi;
- while (!IS_SIG(= &m->top) && (mi =3D mbuf_peek(m->mbuf)) !=3D NULL)=
- {
- multi_tcp_action(m, mi, TA_S= OCKET_WRITE, true);
- = }
- }-}
-
/*
* Top level= event loop for single-threaded operation.
* TCP mode.
@@ -851,7 +289,7 @@

= /* wait on tun/socket list */
multi_get_timeout(= &multi, &multi.top.c2.timeval);
- status =3D multi_tcp_wait(&multi.top, multi.m= tcp);
+ status= =3D multi_protocol_io_wait(&multi);
MULTI_CHE= CK_SIG(&multi);

/* check on = status of coarse timers */
@@ -861,12 +299,12 @@
= if (status > 0)
{
/* process the I/O which triggered select */

- multi_tcp_process_io(&= ;multi);

+ = multi_protocol_process_io(&multi);
MULTI_= CHECK_SIG(&multi);
}
= else if (status =3D=3D 0)
{
- multi_tcp_action(&multi, NU= LL, TA_TIMEOUT, false);
+ multi_protocol_action(&multi, NULL, TA_TIMEOUT, false);=
}

perf= _pop();
diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.= h
index ab968e9..b9f73d4 100644
--- a/src/o= penvpn/mtcp.h
+++ b/src/openvpn/mtcp.h
@@ -= 30,34 +30,28 @@

#include "event.h&q= uot;

= -/*
- * Extra state inf= o needed for TCP mode
-= */
-struct multi_tcp
-{
- struct event_set *es;
- struct event_set_return *esr;
- int n_esr;
- int maxevents;

- unsigned int tun_rwflags;
<= br>-#ifdef ENABLE_MANAGEMENT
- unsigned int managemen= t_persist_flags;
-#endi= f
-};
-
struct multi_instance= ;
struct context;

-struct multi_tcp *multi_tcp_init(int maxeve= nts, int *maxclients);
= -
-void multi_tcp_free(= struct multi_tcp *mtcp);
-void multi_tcp_der= eference_instance(struct multi_tcp *mtcp, struct multi_instance *mi);
+void multi_tcp_dereferenc= e_instance(struct multi_protocol *multi_io, struct multi_instance *mi);


bool multi_tcp_instance_specific_init(struc= t multi_context *m, struct multi_instance *mi);
void multi_tcp_instance_specific_free(struct multi_instance *mi);

+str= uct context *multi_tcp_context(struct multi_context *m, struct multi_instan= ce *mi);
+
= +void multi_tcp_set_global_rw_f= lags(struct multi_context *m, struct multi_instance *mi);
+
+bool multi_tcp_process_outgoing_link(struct multi_contex= t *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);
+

+struct multi_instance *multi_create_instance_tcp(stru= ct multi_context *m, struct link_socket *sock);
+
+void multi_tcp_link_out_deferred(struct multi_context *m, struct m= ulti_instance *mi);
+=
+
/= **************************************************************************/=
/**
* Main event loop for OpenVPN in TC= P server mode.
@@ -68,6 +62,6 @@
void tunn= el_server_tcp(struct context *top);


-void multi_tcp_delete_eve= nt(struct multi_tcp *mtcp, event_t event);
+void multi_tcp_delete_event(struct multi_protocol *m= ulti_io, event_t event);

#endif /* ifnde= f MTCP_H */
diff --git a/src/openvpn/mudp.c b/src/openvpn/m= udp.c
index 9e915b2..11fff48 100644
--- a/s= rc/openvpn/mudp.c
+++ b/src/openvpn/mudp.c
= @@ -186,7 +186,7 @@

struct multi_instanc= e *
multi_get_create_instance_udp(struct multi_context *m,= bool *floated,
- = struct link_socket *ls)
+ struct link_socke= t *sock)
{
struct gc_arena gc =3D gc_= new();
struct mroute_addr real =3D {0};
@@ -257,7 +257,7 @@

* connect-freq = but not against connect-freq-initial */
= reflect_filter_rate_limit_decrease(m->initial_rate_limiter);
- = mi =3D multi_create_instance(m, &real, ls);
+ mi =3D multi_create_= instance(m, &real, sock);
if (mi)<= /span>
{
= hash_add_fast(hash, bucket, &mi->real, hv, mi);
diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h

index= b378754..3107cc6 100644
--- a/src/openvpn/mudp.h+++ b/src/openvpn/mudp.h
@@ -56,13 +56,13 @@<= br> * successful, returns the newly created instance.
*
* @param m - The single multi_context str= ucture.
- * @param ls = - Listening socket where this instance is connecting to
<= span style=3D"color: hsl(120, 100%, 40%);">+ * @param sock - Listeni= ng socket where this instance is connecting to
* * @return A pointer to a multi_instance if one already existed fo= r the
* packet's source address or if one was a n= ewly created successfully.
* NULL if one did not yet= exist and a new one was not created.
*/
= struct multi_instance *multi_get_create_instance_udp(struct multi_context = *m, bool *floated,
- = struct link_socket *ls);<= /span>
+ = struct link_socket *sock);

#endif /* ifndef MUDP_H */
diff --git a= /src/openvpn/multi.c b/src/openvpn/multi.c
index d9e6b1f..4= fddd62 100644
--- a/src/openvpn/multi.c
+++= b/src/openvpn/multi.c
@@ -440,7 +440,7 @@
= */
if (tcp_mode)
{- m->mtcp =3D multi_t= cp_init(t->options.max_clients, &m->max_clients);
+ m->multi_io =3D multi_pr= otocol_init(t->options.max_clients, &m->max_clients);
<= span> }
m->tcp_queue_limit =3D t->options.tc= p_queue_limit;

@@ -665,9 +665,9 @@=
mi->did_iroutes =3D false;
= }

= - if (m->mtcp)
+ if (m->multi_io)
{
- multi_tcp_dereference_ins= tance(m->mtcp, mi);
- multi_tcp_free(m->mtcp);
+ multi_protocol_free(m->multi_i= o);
}
}

<= span>@@ -751,7 +751,7 @@
*/
struct multi= _instance *
multi_create_instance(struct multi_context *m,= const struct mroute_addr *real,
- struct link_socket *ls)
+ struct link_sock= et *sock)
{
struct gc_arena gc =3D gc= _new();
struct multi_instance *mi;
@@ = -774,7 +774,7 @@
generate_prefix(mi);
}

- inherit_context_child(&mi->context, &m->top, ls);<= /span>
+ inherit_context_= child(&mi->context, &m->top, sock);
if (= IS_SIG(&mi->context))
{
= goto err;
@@ -3126,7 +3126,7 @@
<= br> void
multi_process_float(struct multi_context *m= , struct multi_instance *mi,
- struct link_socket *ls)
+ struct link_socket *sock= )
{
struct mroute_addr real =3D {0};<= /span>
struct hash *hash =3D m->hash;
@@ -= 3182,7 +3182,7 @@
mi->context.c2.to_link_addr =3D &= amp;mi->context.c2.from;

/* inher= it parent link_socket and link_socket_info */
- mi->context.c2.link_sockets[0] =3D ls;

+ mi->context.c2.lin= k_sockets[0] =3D sock;
mi->context.c2.link_socket_i= nfos[0]->lsa->actual =3D m->top.c2.from;
<= br> tls_update_remote_addr(mi->context.c2.tls_multi, &mi-&= gt;context.c2.from);
@@ -3332,7 +3332,7 @@
= */
bool
multi_process_incoming_link(str= uct multi_context *m, struct multi_instance *instance, const unsigned int m= pp_flags,
- = struct link_socket *ls)
+ struct link_socket *sock)
{
struct gc_arena gc =3D gc_new();

@@ -3353,7 +3353,7 @@
#ifde= f MULTI_DEBUG_EVENT_LOOP
printf("TCP/UDP ->= ; TUN [%d]\n", BLEN(&m->top.c2.buf));
#endif
- multi_set_pendi= ng(m, multi_get_create_instance_udp(m, &floated, ls));
+ multi_set_pending(m, multi_g= et_create_instance_udp(m, &floated, sock));
}

else
{
@@ -3394,7 +33= 94,7 @@
/* nonzero length means that we ha= ve a valid, decrypted packed */
if (floate= d && c->c2.buf.len > 0)
{
- mult= i_process_float(m, m->pending, ls);
+ multi_process_float(m, m->pending= , sock);
}

process_incoming_link_part2(c, lsi, orig_buf);
@@ -3974,9 +3974,9 @@
management_delete_event(void= *arg, event_t event)
{
struct multi_= context *m =3D (struct multi_context *) arg;
- if (m->mtcp)
+ if (m->multi_io)
{
- multi_tcp_delete_ev= ent(m->mtcp, event);
+ multi_tcp_delete_event(m->multi_io, event);
= }
}

diff --git a/sr= c/openvpn/multi.h b/src/openvpn/multi.h
index a4cea79..bef0= b73 100644
--- a/src/openvpn/multi.h
+++ b/= src/openvpn/multi.h
@@ -37,6 +37,7 @@
#inc= lude "pool.h"
#include "mudp.h"=
#include "mtcp.h"
+#include "multi_io.h"
#includ= e "perf.h"
#include "vlan.h" #include "reflect_filter.h"
@@ -173,8 +17= 4,8 @@
struct mbuf_set *mbuf; /**< Set of buff= ers for passing data
* = channel packets between VPN tunnel
= * instances. */
- struct multi_tcp *mtcp; /**< State specific to OpenVPN usi= ng TCP
- = * as external transport. */
+ struct multi_protocol *multi_io; /**<= ; State specific to OpenVPN using TCP
+ * as external= transport. */
struct ifconfig_pool *ifconfig_pool;
struct frequency_limit *new_connection_limiter; struct initial_packet_rate_limit *initial_rate_limiter;=
@@ -275,7 +276,7 @@
void multi_top_free(struct m= ulti_context *m);

struct multi_instance = *multi_create_instance(struct multi_context *m, const struct mroute_addr *r= eal,
- = struct link_socket *ls);
+ = struct link_socket *sock);

void mult= i_close_instance(struct multi_context *m, struct multi_instance *mi, bool s= hutdown);

@@ -290,7 +291,7 @@
<= span> * updates hashtables in multi_context.

*/ void multi_process_float(struct multi_context *m, struct multi_ins= tance *mi,
- = struct link_socket *ls);
+ struct link_socket *sock);

#define MPP_PRE_SELECT (1<<= ;0)
#define MPP_CONDITIONAL_PRE_SELECT (1<<1)=
@@ -355,10 +356,10 @@
* w= hen using TCP transport. Otherwise NULL, as is
* = the case when using UDP transport.
* @param= mpp_flags - Fast I/O optimization flags.
- * @param ls - Socket where the packet was= received.
+ * @param= sock - Socket where the packet was received.
*= /
bool multi_process_incoming_link(struct multi_context *m= , struct multi_instance *instance, const unsigned int mpp_flags,
= - = struct link_socket *ls);
+ struct link_socket *sock);

/**
diff --git = a/src/openvpn/multi_io.c b/src/openvpn/multi_io.c
new file = mode 100644
index 0000000..43fe24b
--- /dev= /null
+++ b/src/openvpn/multi_io.c
@@ -0,0 = +1,619 @@
+/*<= br>+ * OpenVPN -- An applicati= on to securely tunnel IP networks
+ * over a single TCP/UDP port, with support for S= SL/TLS-based
+ * = session authentication and key exchange,
+ * packet encryption, packet authe= ntication, and
+ * = packet compression.
+ *
+ * Cop= yright (C) 2002-2023 OpenVPN Inc <sales@...515...>
+ *
+ * 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 S= oftware Foundation.
+= *
+ * This program = is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even t= he implied warranty of
+ * GNU General Public Lic= ense for more details.
+ * You should= have received a copy of the GNU General Public License along
+ * with this program; if not, wr= ite to the Free Software Foundation, Inc.,

+ * 51 Franklin Street, Fifth Floor, Boston, MA 0211= 0-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
= +#endif
+
+#include "syshead.h"
+
+#include "memdbg.h"
+
+#i= nclude "multi.h"
+#include "forward.h"
+#include "multi_io.h"
+
+#ifdef HAVE_SYS_INOTIFY_H
+#include <sys/inotify.h>
+#endif
+
+/*
+ * Special tags passe= d to event.[ch] functions
+ */
+#define MU= LTI_IO_SOCKET ((void *)1)
+#define MULTI_IO_TUN ((void *)2)
+#define MULTI_IO_SIG ((void *)3) = /* Only on Windows */
+#define MULTI_IO_MANAGEMENT ((void *)4)
+#define MULTI_IO_FILE_CLOSE_WRITE ((void *)5)<= br>+#define MULTI_IO_DCO = ((void *)6)
+=
+struct ta_iow_flags=
+{
+ unsigned int flags;
+ unsigned int ret;
+ unsigned int tun;
+ unsigned int sock;
+};

+
+#ifdef ENABLE_DEBUG
+static const char *
+pract(int action)
+{
+ switch (ac= tion)
+ {+ case TA_UNDEF:
+ return "= TA_UNDEF";
+
+ case TA_SOCKET_= READ:
+ re= turn "TA_SOCKET_READ";
+
+ = case TA_SOCKET_READ_RESIDUAL:
+ return "TA_SOCKET_READ_RESIDUAL";<= br>+
+ case TA_SOCKET_WRITE:
+ return "TA_SOCKET_W= RITE";
+<= br>+ case TA_SOCKET_WRIT= E_READY:
+ = return "TA_SOCKET_WRITE_READY";
+
+ case TA_SOCKET_WRITE_DEFERRED:
+ return "TA_SOCKET_WRITE_DEFERRED&qu= ot;;
+
+ case TA_TUN_READ:
+ return "TA_= TUN_READ";
+
+ case TA_TUN_WRI= TE:
+ retu= rn "TA_TUN_WRITE";
+
+ ca= se TA_INITIAL:
+ = return "TA_INITIAL";
+
+ = case TA_TIMEOUT:
+ return "TA_TIMEOUT";
+
+ case TA_TUN_WRITE_TIMEOUT:
+ return "TA_TUN_WRITE_TIMEOUT";=
+
+ default:
+ return "?";<= br>+ }
+}
+#endif /* ENABLE_DEBUG */
+
+multi_protocol_init(int maxevents, int *maxclients)
+{
+ struct multi_protocol *multi_io;
+ const int extra_events =3D BASE_N= _EVENTS;
+
= + ASSERT(maxevents >=3D 1= );
+ ASSERT(maxcli= ents);
+
+ ALLOC_OBJ_CLEAR(multi_io, st= ruct multi_protocol);

+ multi_io->maxevents =3D maxevents + extra_events;
+ multi_io->es =3D event_set_in= it(&multi_io->maxevents, 0);
+ wait_signal(multi_io->es, MULTI_IO_SIG);
<= span style=3D"color: hsl(120, 100%, 40%);">+ ALLOC_ARRAY(multi_io->es= r, struct event_set_return, multi_io->maxevents);

+ *maxclients =3D max_int(min_int(multi= _io->maxevents - extra_events, *maxclients), 1);
+ msg(D_MULTI_LOW, "MULTI: TCP INI= T maxclients=3D%d maxevents=3D%d", *maxclients, multi_io->maxevents= );
+ return multi_= io;
+}
+

+void
+multi_protocol_free(struct multi_protocol *multi_io)
+{

+ if (multi_io)
+ {
+ event_free(multi_io->es);
+ free(multi_io->esr);
+ free(multi_io);
+ }
+}
+
+int+multi_protocol_io_wait(struc= t multi_context *m)
+= {
+ int status, i;=
+ unsigned int *p= ersistent =3D &m->multi_io->tun_rwflags;
+
+ for (i =3D 0; i < m->top.c1.link_sockets_num; i++)
+ {
+ socket_set_listen_persistent= (m->top.c2.link_sockets[i], m->multi_io->es,
+ &= ;m->top.c2.link_sockets[i]->ev_arg);
+ }
+
+#ifdef _WI= N32
+ if (tuntap_i= s_wintun(m->top.c1.tuntap))
+ {
+ = if (!tuntap_ring_empty(m->top.c1.tuntap))
+ {
+ /* there is data in wintun ring buffer, rea= d it immediately */
+= m->multi_io->esr[0].arg =3D MULTI_IO_TUN;
+ m->multi_io->esr[= 0].rwflags =3D EVENT_READ;

+ m->multi_io->n_esr =3D 1;
+ return 1;
+ }
+ persistent =3D NULL;
+ }
+#endif
+ tun_set(m->top.c1.tuntap, m->multi_io->es, EVENT_READ, = MULTI_IO_TUN, persistent);
+#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
+ dco_event_set(&m->top.c1= .tuntap->dco, m->multi_io->es, MULTI_IO_DCO);
+#endif
+
+ if (management)
+ {
+ = management_socket_set(management, m->multi_io->es, MULTI_IO_MANAGEM= ENT, &m->multi_io->management_persist_flags);
+ }
+#endif
+
+#ifdef ENA= BLE_ASYNC_PUSH
+ /= * arm inotify watcher */
+ event_ctl(m->multi_io->es, m->top.c2.inotify_fd, EVENT_RE= AD, MULTI_IO_FILE_CLOSE_WRITE);
+#endif
+<= /span>
+ status =3D event= _wait(m->multi_io->es, &m->top.c2.timeval, m->multi_io->= esr, m->multi_io->maxevents);
+ update_time();
+ m->multi_io->n_esr =3D 0;
+ if (status > 0)
+ {
+ m->multi_io->n_esr =3D status;
= + }
+ return status;
+}
+
+static = int
+multi_protocol_w= ait_lite(struct multi_context *m, struct multi_instance *mi, const int acti= on, bool *tun_input_pending)
+{
+ struc= t context *c =3D multi_tcp_context(m, mi);
+ unsigned int looking_for =3D 0;
+
+ dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_pro= tocol_wait_lite a=3D%s mi=3D" ptr_format,
+ pract(action),
+ (ptr_type)mi);
+
+ tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */
+
+ switch (action)
+ {
+ case TA_TUN_READ:
+ looking_for =3D TUN_READ;
<= span style=3D"color: hsl(120, 100%, 40%);">+ tun_input_pending = =3D NULL;

+ = io_wait(c, IOW_READ_TUN);
+ break;
+
+ case= TA_SOCKET_READ:
+ = looking_for =3D SOCKET_READ;
+ tun_input_pending =3D NULL;
+ io_wait(c, IOW_READ_LINK)= ;
+ break;=
+
+ case TA_TUN_WRITE:
<= span style=3D"color: hsl(120, 100%, 40%);">+ looking_for =3D TUN= _WRITE;

+ = tun_input_pending =3D NULL;
+ c->c2.timeval.tv_sec =3D 1; /* For some reason, the = Linux 2.2 TUN/TAP driver hits this timeout */
+ perf_push(PERF_PROC_OUT_TUN_MTCP);
+ io_wait(c,= IOW_TO_TUN);
+ = perf_pop();
+ = break;
+
+ case TA_SOCKET= _WRITE:
+ = looking_for =3D SOCKET_WRITE;
+ io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE);+ break;
= +
+ default:
+ msg(M_FATAL, "MULTI PROTOCOL: multi= _protocol_wait_lite, unhandled action=3D%d", action);
+ }
+
+ if (tun_input_pending && (c->c2.event_set_status &= TUN_READ))
+ {
+ *tun_input_pen= ding =3D true;
+ }=
+
+ if (c->c2.event_set_status &= ; looking_for)
+ {=
+ return acti= on;
+ }
= + else
+ {
+ switch (action)
+ {
+ /* MULTI PROTOCOL socket output buffer is full */=
+ case TA= _SOCKET_WRITE:
+ = return TA_SOCKET_WRITE_DEFERRED;
+
+ /* TUN device timed out on accepting write */
+ case TA_TUN_WRITE:
+ retur= n TA_TUN_WRITE_TIMEOUT;
+ }
+

+ return TA_UNDEF;<= /span>
+ }
+}

+
+static struct multi_instance *
+multi_protocol_dispatch(struct multi_context *m, struct mult= i_instance *mi, const int action)
+{
+ = const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_RECORD_TOUCH;+ struct multi_instance *to= uched =3D mi;
+ m-= >mpp_touched =3D &touched;
+
+ d= msg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_protocol_dispatch a=3D%s mi= =3D" ptr_format,
+ pract(action),
+ (ptr_type)mi);
+
+ switc= h (action)
+ {
+ case TA_TUN_REA= D:
+ read_= incoming_tun(&m->top);
+ if (!IS_SIG(&m->top))
+ {
+ multi_process_incoming_tun(m, mpp_= flags);
+ = }
+ break;=
+
+ case TA_SOCKET_READ:+ case TA_SOCKET_READ_R= ESIDUAL:
+ = ASSERT(mi);
+ = ASSERT(mi->context.c2.link_sockets);
+ ASSERT(mi->context.c2.link_sockets[= 0]);
+ set= _prefix(mi);
+ = read_incoming_link(&mi->context, mi->context.c2.link_sockets= [0]);
+ cl= ear_prefix();
+ = if (!IS_SIG(&mi->context))
+ {
+ multi_process_incoming_link(m, mi, mpp_flags,=
+ = mi->context.c2.link_sockets[0]);
+ if (!IS_SIG(&m= i->context))

+ = {
+ = stream_buf_read_setup(mi->context.c2.link_sockets[0]);
+ }
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ break;

+

+ case TA_TIMEOUT:
+ multi_process_timeout(m, mpp_flags= );
+ break= ;
+
+ case TA_TUN_WRITE:
= + multi_process_outg= oing_tun(m, mpp_flags);
+ break;
+ case TA_= TUN_WRITE_TIMEOUT:
+ = multi_process_drop_outgoing_tun(m, mpp_flags);
+ break;
+
+ case TA_SOCKET_WRITE_READY:
+ ASSERT(mi);
+ multi_tcp_process_outgoin= g_link_ready(m, mi, mpp_flags);
+ break;
+
+ = case TA_SOCKET_WRITE:
+ multi_tcp_process_outgoing_link(m, false, mpp_flags);<= br>+ break;+
+ case TA_SOCKET_WRITE_DEFERRED:+ multi_tcp_proces= s_outgoing_link(m, true, mpp_flags);
+ break;
+
+ = case TA_INITIAL:
+ ASSERT(mi);
+ multi_tcp_set_global_rw_flags(m, mi);
+ multi_process_post(m, mi, m= pp_flags);
+ = break;
++ default:
+ msg(M_FATAL, "M= ULTI PROTOCOL: multi_protocol_dispatch, unhandled action=3D%d", action= );
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ m->mpp_touched =3D NULL;
+ return touched;
+}
+
+= static int
+multi_pro= tocol_post(struct multi_context *m, struct multi_instance *mi, const int ac= tion)
+{
+ struct context *c =3D multi_= tcp_context(m, mi);
+= int newaction =3D TA_UNDEF;
+
+#define= MTP_NONE 0
+= #define MTP_TUN_OUT (1<<0)
+#define MTP_LINK_OUT (1<<1)
+ unsigned int flags =3D MTP_NONE;

+
+ if (TUN_OUT(c))
+ {
+ flags |=3D MTP_TUN_OUT;
+ }
+ if (LINK_OUT(c))
+ {
+ flags |=3D MTP_LINK_OUT;
+ }
+
+ switch (f= lags)
+ {+ case MTP_TUN_OUT|MTP= _LINK_OUT:
+ c= ase MTP_TUN_OUT:
+ = newaction =3D TA_TUN_WRITE;
+ break;
+
+ = case MTP_LINK_OUT:
+ newaction =3D TA_SOCKET_WRITE;
+ break;
+
+ case MTP_NONE:
+ if (mi && sockets_read_residual(c))+ {
+ newaction =3D TA_SO= CKET_READ_RESIDUAL;
+= }
+ = else
+ = {
+ = multi_tcp_set_global_rw_flags(m, mi);
+ }
+ break;
+
+ = default:
+ {<= /span>
+ struct g= c_arena gc =3D gc_new();
+ msg(M_FATAL, "MULTI PROTOCOL: multi_protocol_post bad= state, mi=3D%s flags=3D%d",
+ multi_instance_string(mi, false, &gc),

+ flags)= ;
+ gc_fre= e(&gc);
+ = break;
+ }=
+ }
+
+ dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_= protocol_post %s -> %s",
+ pract(action),
+ pract(newaction));
+
+ return newaction;
+}
++void
+multi_protocol_process_io(struct multi_conte= xt *m)
+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct multi_protocol *mult= i_io =3D m->multi_io;
+ int i;
+

+ for (i =3D 0; i <= ; multi_io->n_esr; ++i)
+ {
+ = struct event_set_return *e =3D &multi_io->esr[i];
+ struct event_arg *ev_arg =3D = (struct event_arg *)e->arg;
+
+ = /* incoming data for instance or listening socket? */
+ if (e->arg >=3D MULTI_N)
+ {
+ switch (ev_arg->t= ype)

+ {
+ struc= t multi_instance *mi;
+
+ /= * react to event on child instance */
+ case EVENT_ARG_MULTI_INSTANCE:
= + if (!ev_ar= g->u.mi)
+ = {
+ = msg(D_MULTI_ERRORS, "MULTI PROTOCOL: multi_proto_pro= c_io: null minstance");
+ break;
+ }
+
+ if (e->rwflags & EVEN= T_WRITE)
+ = {
+ = multi_protocol_action(m, mi, TA_SOCKET_WRITE_READY, false);<= /span>
+ = }
+ = else if (e->rwflags & EVENT_READ)
+ {
+ multi_protocol_action(m, m= i, TA_SOCKET_READ, false);
+ }
+ break;
+
+ = /* new incoming TCP client attempting to connect? */+ case EVENT_AR= G_LINK_SOCKET:
+ = if (!ev_arg->u.sock)
+ {
+ msg(D_MULTI_ERRORS, "MULT= I PROTOCOL: multi_proto_proc_io: null socket");
+ break;
= + }+
+ 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);
+ i= f (mi)
+ = {
+ = multi_protocol_action(m, mi, TA_INITIAL, false);
+ }=
+ = break;
+ = }
+ = }
+ }
+ else+ {
+#ifdef ENABLE_MANAGEMENT
+ if (e->arg =3D=3D M= ULTI_IO_MANAGEMENT)

+= {
+ = ASSERT(management);
+ management_io(management);
+ }
+ else
+#endif
+ /* incoming data on TUN? */
+ if (e->arg =3D=3D MULTI_IO= _TUN)
+ {<= /span>
+ if (= e->rwflags & EVENT_WRITE)
+ {
+ multi_protocol_action(m, NULL, TA_TUN_WRITE,= false);
+ = }
+ = else if (e->rwflags & EVENT_READ)
+ {
+ multi_protocol_action(m, NULL, TA= _TUN_READ, false);
+ = }
+ = }
+ = /* new incoming TCP client attempting to connect? */
+ else if (e->arg =3D=3D M= ULTI_IO_SOCKET)
+ = {
+ = struct multi_instance *mi;
+ ASSERT(m->top.c2.link_sockets[0]);+ socket_reset= _listen_persistent(m->top.c2.link_sockets[0]);
+ mi =3D multi_create_instance_= tcp(m, m->top.c2.link_sockets[0]);
+ if (mi)
+ {
+ multi_protocol_action(m, mi, TA_INI= TIAL, false);
+ = }
+ = }
+#if defined(EN= ABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
+ /* incoming= data on DCO? */
+ = else if (e->arg =3D=3D MULTI_IO_DCO)
+ {
+ multi_process_incoming_dco(m);
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif

+ /* signal received? */+ else if (e->ar= g =3D=3D MULTI_IO_SIG)
+ = get_signal(&m->top.sig->signal_received);
= + }
+#ifdef ENABLE_ASYNC_PUSH
<= span style=3D"color: hsl(120, 100%, 40%);">+ else if (e->arg = =3D=3D MULTI_IO_FILE_CLOSE_WRITE)

+ {
+ multi_process_file_closed(m, MPP_PRE_SELECT | MPP_R= ECORD_TOUCH);
+ = }
+#endif
+ }
+ if (IS_SIG(&m->top))=

+ {+ break;
= + }
+ }
+ multi_io->n_esr =3D 0;
+
+ /*
+ = * Process queued mbuf packets destined for TCP socket
+ */
+ {
+ struct multi_instance *mi;
+ while (!IS_SIG(&m->top) &&= ; (mi =3D mbuf_peek(m->mbuf)) !=3D NULL)
+ {
+ multi_protocol_action(m, mi, TA_SOCKET_WRITE, tru= e);
+ }=
+ }
+}
+
+vo= id
+multi_protocol_ac= tion(struct multi_context *m, struct multi_instance *mi, int action, bool p= oll)
+{
+ bool tun_input_pending =3D fa= lse;

+
+ do

+ {
+ dmsg(D_MULTI_DEBUG, "MULTI PROTOCOL: multi_proto= col_action a=3D%s p=3D%d",
+ pract(action),
+ poll);
+
= + /*
+ = * If TA_SOCKET_READ_RESIDUAL, it means we still have pending
+ * input packets which we= re read by a prior recv.

+ *
+ = * Otherwise do a "lite" wait, which means we wait with 0 tim= eout
+ * on I= /O events only related to the current instance, not
+ * the big list of events.=
+ *
+ * On our first pass, poll = will be false because we already know

+ * that input is available, and to call io_wait w= ould be redundant.
+ = */
+ i= f (poll && action !=3D TA_SOCKET_READ_RESIDUAL)
+ {
+ const int orig_action =3D action;
+ action =3D = multi_protocol_wait_lite(m, mi, action, &tun_input_pending);
= + if (action =3D=3D = TA_UNDEF)
+ = {
+ = msg(M_FATAL, "MULTI PROTOCOL: I/O wait required blocking in multi_prot= ocol_action, action=3D%d", orig_action);
+ }
+ }
+
+ /= *
+ * Dispatc= h the action
+ = */
+ struct = multi_instance *touched =3D multi_protocol_dispatch(m, mi, action);<= br>+
+ /*
+ * Signal received or connection
+ * reset by peer?
+ */
+ if (touched && IS_SI= G(&touched->context))
+ {
+ = if (mi =3D=3D touched)
+ {
+ mi =3D NULL;
+ }
+ multi_close_instance_on_signal(m, touched);+ }
+
+
+ = /*
+ *= If dispatch produced any pending output
+ * for a particular instance, point to<= br>+ * that instance.
+ */+ if (m->pending)
+ {
+ mi =3D m->pending= ;

+ }+
+ /*
+ * Based on the effects of the action,+ * such as generating= pending output,
+ = * possibly transition to a new action state.
+ */
+ action =3D multi_protocol_post(m, mi, action= );
+
+ /*
+ * If we are finished processing the = original action,
+ = * check if we have any TUN input. If so, transition
+ * our action state to proce= ssing this input.
+ = */
+ if= (tun_input_pending && action =3D=3D TA_UNDEF)
+ {
+ action =3D TA_TUN_READ;
+ mi =3D NULL;
+ tun_input_pending= =3D false;
+ = poll =3D false;
+= }
+ el= se
+ {<= br>+ poll =3D true;<= /span>
+ }
= +
+ } while (action !=3D TA_UNDEF);
+}

+
+void
+multi_prot= ocol_delete_event(struct multi_protocol *multi_io, event_t event)+{
+ if (multi_io && multi_io->es)
+ {
+ event_del(multi_io->es, e= vent);
+ }<= br>+}
diff --gi= t a/src/openvpn/multi_io.h b/src/openvpn/multi_io.h
new fil= e mode 100644
index 0000000..07fc935
--- /d= ev/null
+++ b/src/openvpn/multi_io.h
@@ -0,= 0 +1,77 @@
+/*=
+ * OpenVPN -- An applicat= ion 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 auth= entication, and
+ * = packet compression.
+ *
+ * Co= pyright (C) 2002-2023 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribut= e 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 eve= n the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public = License for more details.
+ *
+ * You sho= uld 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 0= 2110-1301 USA.
+ */
+
+/*
+ * Multi-protocol specific code for --mode server<= br>+ */
+
+#ifndef MULTI_IO_H
+#define MULTI_IO_H
+
+#inclu= de "event.h"
+/*
+ * I/O processing States
+ */
+
+#define TA_UNDEF 0
+#define TA_SOCKET_READ 1
+#define TA_SOCKET_READ_RESIDUAL = 2

+#define TA_SOCKET_= WRITE 3
+#de= fine TA_SOCKET_WRITE_READY 4
+#define TA_SOCKET_WRITE_DEFERRED 5
+#define TA_TUN_READ 6
+#define TA_TUN_WRITE = 7

+#define TA_INITIAL= 8
+#de= fine TA_TIMEOUT 9
+#define TA_TUN_WRITE_TIMEOUT 10
+
+/*
+ * Extra= state info needed for Multi-protocol
+ */
+struct multi_protocol
+{
+ struct eve= nt_set *es;
+ stru= ct event_set_return *esr;
+ int n_esr;
= + int maxevents;
+= unsigned int tun_rwflags;
+ unsigned int udp_flags;
+#ifdef ENABLE_MANAGEMENT
+ unsigned int management_persist_flags;+#endif
+};
+
+st= ruct multi_protocol *multi_protocol_init(int maxevents, int *maxclients);
+
+void multi_protocol_free(struct multi_pr= otocol *multi_io);
+<= /span>
+int multi_protocol_i= o_wait(struct multi_context *m);
+
+void m= ulti_protocol_process_io(struct multi_context *m);
+
+void multi_protocol_action(struct multi_context *m, struct mult= i_instance *mi, int action, bool poll);
+
= +void multi_protocol_delete_event(struct multi_protocol *multi_io, event_t = event);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif /* ifndef MULTI_IO_H */<= /span>

To view, visit change 763. To unsubscribe, o= r for help writing mail filters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1e5a84969988e4f027a18658d4a= b268c13fbf929
Gerrit-Change-Number: 763
Gerrit-PatchSet: 6
Gerrit-Owner: its_Giaan <gianmarco@...2786...= ..>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: its_Giaan <gianmarco@...1665...= 726...>
Gerrit-MessageType: newpatchset
--kUDBIBwsftI=--