From: "its_Giaan (Code Review)" Date: Tue, 1 Oct 2024 15:30:04 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <828b6bfea7523f961ec5bf801433034e1d4a1443-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="phXtrrDifWA="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Bind to multiple ipv4/ipv6 addresses List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --phXtrrDifWA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/764?usp=3Demail to look at the new patch set (#2). Change subject: Bind to multiple ipv4/ipv6 addresses ...................................................................... Bind to multiple ipv4/ipv6 addresses Enables the binding of multiple listen sockets based on the specified "--local" directives. The main server loop has been updated to handle both TCP and UDP connections. The hash function has also been modified to include the protocol during the creation of new client instances. Change-Id: I31bbf87e4e568021445c7512ecefadfd4a69b363 Signed-off-by: Gianmarco De Gregori --- M doc/man-sections/link-options.rst M src/openvpn/forward.c M src/openvpn/forward.h M src/openvpn/init.c M src/openvpn/mroute.c M src/openvpn/mroute.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 M src/openvpn/openvpn.h M src/openvpn/options.c M src/openvpn/options.h 15 files changed, 418 insertions(+), 277 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/64/764/2 diff --git a/doc/man-sections/link-options.rst b/doc/man-sections/link-opti= ons.rst index 05909ca..e2cf9b8 100644 --- a/doc/man-sections/link-options.rst +++ b/doc/man-sections/link-options.rst @@ -106,7 +106,7 @@ is not reliable. It is recommended to set tun-mtu with enough headroom instead. ---local host|* [port] +--local host|* [port] [protocol] Local host name or IP address and port for bind. If specified, OpenVPN w= ill bind to this address. If unspecified, OpenVPN will bind to all interfaces. '*' can be used as hostname and means 'any host' (OpenVPN will listen on= what diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index e7518e7..e1edbac 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -1772,7 +1772,7 @@ if (c->options.shaper) { int overhead =3D datagram_overhead(c->c2.to_link_addr->des= t.addr.sa.sa_family, - c->options.ce.proto); + sock->info.proto); shaper_wrote_bytes(&c->c2.shaper, BLEN(&c->c2.to_link) + overhead); } @@ -2069,6 +2069,173 @@ */ void +get_io_flags_dowork_udp(struct context *c, struct multi_protocol *multi_io= , const unsigned int flags) +{ + unsigned int socket =3D 0; + unsigned int tuntap =3D 0; + static uintptr_t tun_shift =3D 2; + static uintptr_t err_shift =3D 4; + + /* + * Calculate the flags based on the provided 'flags' argument. + */ + if (flags & IOW_WAIT_SIGNAL) + { + wait_signal(multi_io->es, (void *)err_shift); + } + + if (flags & IOW_TO_LINK) + { + if (flags & IOW_SHAPER) + { + /* + * If sending this packet would put us over our traffic shaping + * quota, don't send -- instead compute the delay we must wait + * until it will be OK to send the packet. + */ + int delay =3D 0; + + /* set traffic shaping delay in microseconds */ + if (c->options.shaper) + { + delay =3D max_int(delay, shaper_delay(&c->c2.shaper)); + } + + if (delay < 1000) + { + socket |=3D EVENT_WRITE; + } + else + { + shaper_soonest_event(&c->c2.timeval, delay); + } + } + else + { + socket |=3D EVENT_WRITE; + } + } + else if (!((flags & IOW_FRAG) && TO_LINK_FRAG(c))) + { + if (flags & IOW_READ_TUN) + { + tuntap |=3D EVENT_READ; + } + } + + /* + * If outgoing data (for TUN/TAP device) pending, wait for ready-to-se= nd status + * from device. Otherwise, wait for incoming data on TCP/UDP port. + */ + if (flags & IOW_TO_TUN) + { + tuntap |=3D EVENT_WRITE; + } + else + { + if (flags & IOW_READ_LINK) + { + socket |=3D EVENT_READ; + } + } + + /* + * outgoing bcast buffer waiting to be sent? + */ + if (flags & IOW_MBUF) + { + socket |=3D EVENT_WRITE; + } + + /* + * Force wait on TUN input, even if also waiting on TCP/UDP output + */ + if (flags & IOW_READ_TUN_FORCE) + { + tuntap |=3D EVENT_READ; + } + +#ifdef _WIN32 + if (tuntap_is_wintun(c->c1.tuntap)) + { + /* + * With wintun we are only interested in read event. Ring buffer is + * always ready for write, so we don't do wait. + */ + tuntap =3D EVENT_READ; + } +#endif + + /* + * Configure event wait based on socket, tuntap flags. + */ + for (int i =3D 0; i < c->c1.link_sockets_num; i++) + { + if (proto_is_dgram(c->c2.link_sockets[i]->info.proto)) + { + socket_set(c->c2.link_sockets[i], multi_io->es, socket, + &c->c2.link_sockets[i]->ev_arg, NULL); + } + } + tun_set(c->c1.tuntap, multi_io->es, tuntap, (void *)tun_shift, NULL); + + multi_io->udp_flags =3D socket | tuntap; +} + +void +get_io_flags_udp(struct context *c, struct multi_protocol *multi_io, const= unsigned int flags) +{ + multi_io->udp_flags =3D ES_ERROR; + if (c->c2.fast_io && (flags & (IOW_TO_TUN | IOW_TO_LINK | IOW_MBUF))) + { + /* fast path -- only for TUN/TAP/UDP writes */ + unsigned int ret =3D 0; + if (flags & IOW_TO_TUN) + { + ret |=3D TUN_WRITE; + } + if (flags & (IOW_TO_LINK | IOW_MBUF)) + { + ret |=3D SOCKET_WRITE; + } + multi_io->udp_flags =3D ret; + } + else + { +#ifdef _WIN32 + bool skip_iowait =3D flags & IOW_TO_TUN; + if (flags & IOW_READ_TUN) + { + /* + * don't read from tun if we have pending write to link, + * since every tun read overwrites to_link buffer filled + * by previous tun read + */ + skip_iowait =3D !(flags & IOW_TO_LINK); + } + if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait) + { + unsigned int ret =3D 0; + if (flags & IOW_TO_TUN) + { + ret |=3D TUN_WRITE; + } + if (flags & IOW_READ_TUN) + { + ret |=3D TUN_READ; + } + multi_io->udp_flags =3D ret; + } + else +#endif /* ifdef _WIN32 */ + { + /* slow path - delegate to io_wait_dowork_udp to calculate fla= gs */ + get_io_flags_dowork_udp(c, multi_io, flags); + } + } +} + +void io_wait_dowork(struct context *c, const unsigned int flags) { unsigned int socket =3D 0; @@ -2310,6 +2477,7 @@ dmsg(D_EVENT_WAIT, "I/O WAIT status=3D0x%04x", c->c2.event_set_status); } + void process_io(struct context *c, struct link_socket *sock) { diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index c99b779..141176c 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -69,6 +69,10 @@ extern counter_type link_write_bytes_global; +void get_io_flags_dowork_udp(struct context *c, struct multi_protocol *mul= ti_io, const unsigned int flags); + +void get_io_flags_udp(struct context *c, struct multi_protocol *multi_io, = const unsigned int flags); + void io_wait_dowork(struct context *c, const unsigned int flags); void pre_select(struct context *c); diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 4eb286a..5877ebb 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -3829,6 +3829,24 @@ unsigned int sockflags =3D c->options.sockflags; int i; + int mode =3D LS_MODE_DEFAULT; + /* mode allows CM_CHILD_TCP + * instances to inherit acceptable fds + * from a top-level parent */ + if (c->options.mode =3D=3D MODE_SERVER) + { + /* initializing listening socket */ + if (c->mode =3D=3D CM_TOP) + { + mode =3D LS_MODE_TCP_LISTEN; + } + /* initializing socket to client */ + else if (c->mode =3D=3D CM_CHILD_TCP) + { + mode =3D LS_MODE_TCP_ACCEPT_FROM; + } + } + #if PORT_SHARE if (c->options.port_share_host && c->options.port_share_port) { @@ -3838,33 +3856,37 @@ for (i =3D 0; i < c->c1.link_sockets_num; i++) { - int mode =3D LS_MODE_DEFAULT; + const char *host =3D c->options.ce.local_list->array[i]->local; + const char *port =3D c->options.ce.local_list->array[i]->port; + int proto =3D c->options.ce.local_list->array[i]->proto; + bool bind_local =3D c->options.ce.local_list->array[i]->bind_local; - /* mode allows CM_CHILD_TCP - * instances to inherit acceptable fds - * from a top-level parent */ - if (c->options.mode =3D=3D MODE_SERVER) + if (c->mode =3D=3D CM_CHILD_TCP || c->mode =3D=3D CM_CHILD_UDP) { - /* initializing listening socket */ - if (c->mode =3D=3D CM_TOP) + const struct link_socket *ls =3D NULL; + if (c->mode =3D=3D CM_CHILD_TCP) { - mode =3D LS_MODE_TCP_LISTEN; + ls =3D c->c2.accept_from; } - /* initializing socket to client */ - else if (c->mode =3D=3D CM_CHILD_TCP) + else if (c->mode =3D=3D CM_CHILD_UDP) { - mode =3D LS_MODE_TCP_ACCEPT_FROM; + ls =3D c->c2.link_sockets[0]; } + + host =3D ls->local_host; + port =3D ls->local_port; + proto =3D ls->info.proto; + bind_local =3D ls->bind_local; } /* init each socket with its specific port */ link_socket_init_phase1(c->c2.link_sockets[i], - c->options.ce.local_list->array[i]->local, - c->options.ce.local_list->array[i]->port, + host, + port, c->options.ce.remote, c->options.ce.remote_port, c->c1.dns_cache, - c->options.ce.proto, + proto, c->options.ce.af, c->options.ce.bind_ipv6_only, mode, @@ -3874,7 +3896,7 @@ #ifdef ENABLE_DEBUG c->options.gremlin, #endif - c->options.ce.local_list->array[i]->bind_l= ocal, + bind_local, c->options.ce.remote_float, &c->c1.link_socket_addrs[i], c->options.ipchange, @@ -4685,7 +4707,7 @@ } /* our wait-for-i/o objects, different for posix vs. win32 */ - if (c->mode =3D=3D CM_P2P) + if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP) { do_event_set_init(c, SHAPER_DEFINED(&c->options)); } @@ -4949,7 +4971,7 @@ CLEAR(*dest); /* proto_is_dgram will ASSERT(0) if proto is invalid */ - dest->mode =3D proto_is_dgram(src->options.ce.proto) ? CM_CHILD_UDP : = CM_CHILD_TCP; + dest->mode =3D proto_is_dgram(sock->info.proto) ? CM_CHILD_UDP : CM_CH= ILD_TCP; dest->gc =3D gc_new(); @@ -4977,6 +4999,8 @@ dest->options =3D src->options; options_detach(&dest->options); + dest->c2.event_set =3D src->c2.event_set; + if (dest->mode =3D=3D CM_CHILD_TCP) { /* diff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c index f4539dc..6c8e8dd 100644 --- a/src/openvpn/mroute.c +++ b/src/openvpn/mroute.c @@ -421,6 +421,7 @@ { buf_printf(&out, ":%d", ntohs(maddr.v4.port)); } + buf_printf(&out, ":%d", maddr.proto); } break; diff --git a/src/openvpn/mroute.h b/src/openvpn/mroute.h index 8b457d4..fd1dbfe 100644 --- a/src/openvpn/mroute.h +++ b/src/openvpn/mroute.h @@ -76,6 +76,7 @@ uint8_t len; /* length of address */ uint8_t unused; uint8_t type; /* MR_ADDR/MR_WITH flags */ + uint8_t proto; uint8_t netbits; /* number of bits in network part of address, * valid if MR_WITH_NETBITS is set */ union { @@ -201,6 +202,10 @@ { return false; } + if (a1->proto !=3D a2->proto) + { + return false; + } if (a1->netbits !=3D a2->netbits) { return false; @@ -222,7 +227,7 @@ static inline uint32_t mroute_addr_hash_len(const struct mroute_addr *a) { - return (uint32_t) a->len + 2; + return (uint32_t) a->len + 3; } static inline void diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index 1dae184..97a6228 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -54,7 +54,7 @@ struct hash *hash =3D m->hash; mi =3D multi_create_instance(m, NULL, sock); - if (mi) + if (mi && !proto_is_dgram(sock->info.proto)) { struct hash_element *he; const uint32_t hv =3D hash_value(hash, &mi->real); @@ -241,87 +241,3 @@ } return ret; } - -/* - * Top level event loop for single-threaded operation. - * TCP mode. - */ -void -tunnel_server_tcp(struct context *top) -{ - struct multi_context multi; - int status; - - top->mode =3D CM_TOP; - context_clear_2(top); - - /* initialize top-tunnel instance */ - init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); - if (IS_SIG(top)) - { - return; - } - - /* initialize global multi_context object */ - multi_init(&multi, top, true); - - /* initialize our cloned top object */ - multi_top_init(&multi, top); - - /* initialize management interface */ - init_management_callback_multi(&multi); - - /* finished with initialization */ - initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto tcp-server */ - -#ifdef ENABLE_ASYNC_PUSH - multi.top.c2.inotify_fd =3D inotify_init(); - if (multi.top.c2.inotify_fd < 0) - { - msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); - } -#endif - - /* per-packet event loop */ - while (true) - { - perf_push(PERF_EVENT_LOOP); - - /* wait on tun/socket list */ - multi_get_timeout(&multi, &multi.top.c2.timeval); - status =3D multi_protocol_io_wait(&multi); - MULTI_CHECK_SIG(&multi); - - /* check on status of coarse timers */ - multi_process_per_second_timers(&multi); - - /* timeout? */ - if (status > 0) - { - /* process the I/O which triggered select */ - multi_protocol_process_io(&multi); - MULTI_CHECK_SIG(&multi); - } - else if (status =3D=3D 0) - { - multi_protocol_action(&multi, NULL, TA_TIMEOUT, false); - } - - perf_pop(); - } - -#ifdef ENABLE_ASYNC_PUSH - close(top->c2.inotify_fd); -#endif - - /* shut down management interface */ - uninit_management_callback(); - - /* save ifconfig-pool */ - multi_ifconfig_pool_persist(&multi, true); - - /* tear down tunnel instance (unless --persist-tun) */ - multi_uninit(&multi); - multi_top_free(&multi); - close_instance(top); -} diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.h index b9f73d4..51b209b 100644 --- a/src/openvpn/mtcp.h +++ b/src/openvpn/mtcp.h @@ -51,17 +51,6 @@ void multi_tcp_link_out_deferred(struct multi_context *m, struct multi_ins= tance *mi); - -/*************************************************************************= */ -/** - * Main event loop for OpenVPN in TCP server mode. - * @ingroup eventloop - * - * @param top - Top-level context structure. - */ -void tunnel_server_tcp(struct context *top); - - void multi_tcp_delete_event(struct multi_protocol *multi_io, event_t event= ); #endif /* ifndef MTCP_H */ diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index 5092665..660ea2d 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -192,6 +192,7 @@ struct mroute_addr real =3D {0}; struct multi_instance *mi =3D NULL; struct hash *hash =3D m->hash; + real.proto =3D sock->info.proto; if (mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true) && m->top.c2.buf.len > 0) @@ -318,18 +319,66 @@ msg_set_prefix("Connection Attempt"); m->top.c2.to_link =3D m->hmac_reply; m->top.c2.to_link_addr =3D m->hmac_reply_dest; - process_outgoing_link(&m->top, m->top.c2.link_sockets[0]); + for (int i =3D 0; i < m->top.c1.link_sockets_num; i++) + { + if (!proto_is_dgram(m->top.c2.link_sockets[i]->info.proto)) + { + continue; + } + + process_outgoing_link(&m->top, m->top.c2.link_sockets[i]); + } m->hmac_reply_dest =3D NULL; } } /* + * Return the io_wait() flags appropriate for + * a point-to-multipoint tunnel. + */ +unsigned int +p2mp_iow_flags(const struct multi_context *m) +{ + unsigned int flags =3D IOW_WAIT_SIGNAL; + if (m->pending) + { + if (TUN_OUT(&m->pending->context)) + { + flags |=3D IOW_TO_TUN; + } + if (LINK_OUT(&m->pending->context)) + { + flags |=3D IOW_TO_LINK; + } + } + else if (mbuf_defined(m->mbuf)) + { + flags |=3D IOW_MBUF; + } + else if (m->hmac_reply_dest) + { + flags |=3D IOW_TO_LINK; + } + else + { + flags |=3D IOW_READ; + } +#ifdef _WIN32 + if (tuntap_ring_empty(m->top.c1.tuntap)) + { + flags &=3D ~IOW_READ_TUN; + } +#endif + return flags; +} + +/* * Process an I/O event. */ -static void +void multi_process_io_udp(struct multi_context *m) { - const unsigned int status =3D m->top.c2.event_set_status; + const unsigned int status =3D m->multi_io->udp_flags; const unsigned int mpp_flags =3D m->top.c2.fast_io ? (MPP_CONDITIONAL_PRE_SELECT | MPP_CLO= SE_ON_SIGNAL) : (MPP_PRE_SELECT | MPP_CLOSE_ON_SIGNAL= ); @@ -427,123 +476,3 @@ } #endif } - -/* - * Return the io_wait() flags appropriate for - * a point-to-multipoint tunnel. - */ -static inline unsigned int -p2mp_iow_flags(const struct multi_context *m) -{ - unsigned int flags =3D IOW_WAIT_SIGNAL; - if (m->pending) - { - if (TUN_OUT(&m->pending->context)) - { - flags |=3D IOW_TO_TUN; - } - if (LINK_OUT(&m->pending->context)) - { - flags |=3D IOW_TO_LINK; - } - } - else if (mbuf_defined(m->mbuf)) - { - flags |=3D IOW_MBUF; - } - else if (m->hmac_reply_dest) - { - flags |=3D IOW_TO_LINK; - } - else - { - flags |=3D IOW_READ; - } -#ifdef _WIN32 - if (tuntap_ring_empty(m->top.c1.tuntap)) - { - flags &=3D ~IOW_READ_TUN; - } -#endif - return flags; -} - - -void -tunnel_server_udp(struct context *top) -{ - struct multi_context multi; - - top->mode =3D CM_TOP; - context_clear_2(top); - - /* initialize top-tunnel instance */ - init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); - if (IS_SIG(top)) - { - return; - } - - /* initialize global multi_context object */ - multi_init(&multi, top, false); - - /* initialize our cloned top object */ - multi_top_init(&multi, top); - - /* initialize management interface */ - init_management_callback_multi(&multi); - - /* finished with initialization */ - initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto udp */ - -#ifdef ENABLE_ASYNC_PUSH - multi.top.c2.inotify_fd =3D inotify_init(); - if (multi.top.c2.inotify_fd < 0) - { - msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); - } -#endif - - /* per-packet event loop */ - while (true) - { - perf_push(PERF_EVENT_LOOP); - - /* set up and do the io_wait() */ - multi_get_timeout(&multi, &multi.top.c2.timeval); - io_wait(&multi.top, p2mp_iow_flags(&multi)); - MULTI_CHECK_SIG(&multi); - - /* check on status of coarse timers */ - multi_process_per_second_timers(&multi); - - /* timeout? */ - if (multi.top.c2.event_set_status =3D=3D ES_TIMEOUT) - { - multi_process_timeout(&multi, MPP_PRE_SELECT|MPP_CLOSE_ON_SIGN= AL); - } - else - { - /* process I/O */ - multi_process_io_udp(&multi); - MULTI_CHECK_SIG(&multi); - } - - perf_pop(); - } - -#ifdef ENABLE_ASYNC_PUSH - close(top->c2.inotify_fd); -#endif - - /* shut down management interface */ - uninit_management_callback(); - - /* save ifconfig-pool */ - multi_ifconfig_pool_persist(&multi, true); - - /* tear down tunnel instance (unless --persist-tun) */ - multi_uninit(&multi); - multi_top_free(&multi); - close_instance(top); -} diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h index 3107cc6..e329832 100644 --- a/src/openvpn/mudp.h +++ b/src/openvpn/mudp.h @@ -31,18 +31,9 @@ struct context; struct multi_context; +unsigned int p2mp_iow_flags(const struct multi_context *m); -/** - * Main event loop for OpenVPN in UDP server mode. - * @ingroup eventloop - * - * This function implements OpenVPN's main event loop for UDP server mode. - * - * @param top - Top-level context structure. - */ -void tunnel_server_udp(struct context *top); - - +void multi_process_io_udp(struct multi_context *m); /*************************************************************************= */ /** * Get, and if necessary create, the multi_instance associated with a diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index fc700de..bf98c7f 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -289,7 +289,7 @@ * Main initialization function, init multi_context object. */ void -multi_init(struct multi_context *m, struct context *t, bool tcp_mode) +multi_init(struct multi_context *m, struct context *t) { int dev =3D DEV_TYPE_UNDEF; @@ -435,13 +435,12 @@ m->instances =3D calloc(m->max_clients, sizeof(struct multi_instance *= )); + m->top.c2.event_set =3D t->c2.event_set; + /* - * Initialize multi-socket TCP I/O wait object + * Initialize multi-socket I/O wait object */ - if (tcp_mode) - { - m->multi_io =3D multi_protocol_init(t->options.max_clients, &m->ma= x_clients); - } + m->multi_io =3D multi_protocol_init(t->options.max_clients, &m->max_cl= ients); m->tcp_queue_limit =3D t->options.tcp_queue_limit; /* @@ -665,7 +664,7 @@ mi->did_iroutes =3D false; } - if (m->multi_io) + if (m->multi_io && !proto_is_dgram(m->top.options.ce.proto)) { multi_tcp_dereference_instance(m->multi_io, mi); } @@ -776,7 +775,6 @@ mi->real =3D *real; generate_prefix(mi); } - mi->did_open_context =3D true; inherit_context_child(&mi->context, &m->top, sock); if (IS_SIG(&mi->context)) @@ -798,6 +796,7 @@ { goto err; } + mi->real.proto =3D sock->info.proto; generate_prefix(mi); } @@ -4158,6 +4157,98 @@ ASSERT(mi->context.c2.tls_multi->peer_id < m->max_clients); } +/*************************************************************************= */ +/** + * Main event loop for OpenVPN in multi-protocol server mode. + * @ingroup eventloop + * + * @param top - Top-level context structure. + */ +void +tunnel_server_loop(struct multi_context *multi) +{ + int status; + + while (true) + { + perf_push(PERF_EVENT_LOOP); + + /* wait on tun/socket list */ + multi_get_timeout(multi, &multi->top.c2.timeval); + status =3D multi_protocol_io_wait(multi); + MULTI_CHECK_SIG(multi); + + /* check on status of coarse timers */ + multi_process_per_second_timers(multi); + + /* timeout? */ + if (status > 0) + { + /* process the I/O which triggered select */ + multi_protocol_process_io(multi); + MULTI_CHECK_SIG(multi); + } + else if (status =3D=3D 0) + { + multi_protocol_action(multi, NULL, TA_TIMEOUT, false); + } + + perf_pop(); + } +} + +void +tunnel_server_init(struct context *top) +{ + struct multi_context multi; + + top->mode =3D CM_TOP; + context_clear_2(top); + + /* initialize top-tunnel instance */ + init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP); + if (IS_SIG(top)) + { + return; + } + + /* initialize global multi_context object */ + multi_init(&multi, top); + + /* initialize our cloned top object */ + multi_top_init(&multi, top); + + /* initialize management interface */ + init_management_callback_multi(&multi); + + /* finished with initialization */ + initialization_sequence_completed(top, ISC_SERVER); /* --mode server -= -proto tcp-server */ + +#ifdef ENABLE_ASYNC_PUSH + multi.top.c2.inotify_fd =3D inotify_init(); + if (multi.top.c2.inotify_fd < 0) + { + msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error"); + } +#endif + + tunnel_server_loop(&multi); + + #ifdef ENABLE_ASYNC_PUSH + close(top->c2.inotify_fd); +#endif + + /* shut down management interface */ + uninit_management_callback(); + + /* save ifconfig-pool */ + multi_ifconfig_pool_persist(&multi, true); + + /* tear down tunnel instance (unless --persist-tun) */ + multi_uninit(&multi); + multi_top_free(&multi); + close_instance(top); +} /* * Top level event loop. @@ -4167,12 +4258,6 @@ { ASSERT(top->options.mode =3D=3D MODE_SERVER); - if (proto_is_dgram(top->options.ce.proto)) - { - tunnel_server_udp(top); - } - else - { - tunnel_server_tcp(top); - } + tunnel_server_init(top); + } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 930d6b9..0a15fd9 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -252,10 +252,8 @@ * Main event loop for OpenVPN in server mode. * @ingroup eventloop * - * This function calls the appropriate main event loop function depending - * on the transport protocol used: - * - \c tunnel_server_udp() - * - \c tunnel_server_tcp() + * This function calls the main event loop function + * on the transport protocols: * * @param top - Top-level context structure. */ @@ -268,7 +266,7 @@ * Called by mtcp.c, mudp.c, or other (to be written) protocol drivers */ -void multi_init(struct multi_context *m, struct context *t, bool tcp_mode); +void multi_init(struct multi_context *m, struct context *t); void multi_uninit(struct multi_context *m); @@ -664,9 +662,12 @@ multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp= _flags) { struct multi_instance *mi =3D m->pending; + if (!mi) + { + return false; + } bool ret =3D true; - ASSERT(mi); #ifdef MULTI_DEBUG_EVENT_LOOP printf("%s -> TUN len=3D%d\n", id(mi), diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index 77bb41d..821ac2e 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -220,8 +220,8 @@ * \c SIGUSR1 restarts. * * This structure is initialized at the top of the \c - * tunnel_point_to_point(), \c tunnel_server_udp(), and \c - * tunnel_server_tcp() functions. In other words, it is reset for every + * tunnel_point_to_point(), \c tunnel_server() \c + * functions. In other words, it is reset for every * iteration of the \c main() function's inner \c SIGUSR1 loop. */ struct context_2 diff --git a/src/openvpn/options.c b/src/openvpn/options.c index f216456..6f4d5e2 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -2199,6 +2199,7 @@ } ALLOC_OBJ_CLEAR_GC(e, struct local_entry, gc); + e->proto =3D PROTO_NONE; l->array[l->len++] =3D e; return e; @@ -3170,14 +3171,30 @@ if (ce->proto =3D=3D PROTO_TCP) { ce->proto =3D PROTO_TCP_SERVER; + o->ce.proto =3D ce->proto; + } + if (ce->local_list) + { + for (int i =3D 0; i < ce->local_list->len; i++) + { + if (ce->local_list->array[i]->proto =3D=3D PROTO_TCP) + { + ce->local_list->array[i]->proto =3D PROTO_TCP_SERVER; + } + else if (ce->local_list->array[i]->proto =3D=3D PROTO_NONE) + { + ce->local_list->array[i]->proto =3D ce->proto; + } + } } } - if (o->client) + if (o->mode !=3D MODE_SERVER) { if (ce->proto =3D=3D PROTO_TCP) { ce->proto =3D PROTO_TCP_CLIENT; + o->ce.proto =3D ce->proto; } } @@ -3331,6 +3348,10 @@ { le->port =3D o->ce.local_port; } + if (!le->proto) + { + le->proto =3D o->ce.proto; + } } #ifdef _WIN32 @@ -3822,6 +3843,7 @@ ASSERT(e); e->port =3D o->ce.local_port; e->bind_local =3D o->connection_list->array[0]->bind_local; + e->proto =3D o->ce.proto; } /* use the same listen list for every outgoing connection */ @@ -6232,7 +6254,7 @@ VERIFY_PERMISSION(OPT_P_UP); options->ifconfig_nowarn =3D true; } - else if (streq(p[0], "local") && p[1] && !p[3]) + else if (streq(p[0], "local") && p[1] && !p[4]) { struct local_entry *e; @@ -6253,7 +6275,12 @@ if (p[2]) { e->port =3D p[2]; - e->bind_local =3D true; + /*e->bind_local =3D true; */ + } + + if (p[3]) + { + e->proto =3D ascii2proto(p[3]); } } else if (streq(p[0], "remote-random") && !p[1]) diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 6942455..f3cf7e5 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -99,6 +99,7 @@ const char *local; const char *port; bool bind_local; + int proto; }; struct connection_entry -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/764?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I31bbf87e4e568021445c7512ecefadfd4a69b363 Gerrit-Change-Number: 764 Gerrit-PatchSet: 2 Gerrit-Owner: its_Giaan Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --phXtrrDifWA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

its_Giaan uploaded patch set #2 to this change.

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

Bind to multiple ipv4/ipv6 addresses

Enables the binding of mu= ltiple listen
sockets based on the specified "--local"
dire= ctives.

The main server loop has been updated
to handle both TCP = and UDP connections.

The hash function has also been modified
to = include the protocol during the
creation of new client instances.
Change-Id: I31bbf87e4e568021445c7512ecefadfd4a69b363
Signed-off-by: Gia= nmarco De Gregori <gianmarco@...2726...>
---
M doc/man-sections= /link-options.rst
M src/openvpn/forward.c
M src/openvpn/forward.h
= M src/openvpn/init.c
M src/openvpn/mroute.c
M src/openvpn/mroute.hM src/openvpn/mtcp.c
M src/openvpn/mtcp.h
M src/openvpn/mudp.c
M = src/openvpn/mudp.h
M src/openvpn/multi.c
M src/openvpn/multi.h
M s= rc/openvpn/openvpn.h
M src/openvpn/options.c
M src/openvpn/options.h<= br>15 files changed, 418 insertions(+), 277 deletions(-)

git pull=
 ssh://gerrit.openvpn.net:29418/openvpn refs/changes/64/764/2
diff =
--git a/doc/man-sections/link-options.rst b/doc/man-sections/link-options.r=
st
index 05909ca..e2cf9b8 100644
--- a/doc/= man-sections/link-options.rst
+++ b/doc/man-sections/link-o= ptions.rst
@@ -106,7 +106,7 @@
is not re= liable. It is recommended to set tun-mtu with enough headroom
instead.

---local host|* [port]

+--local host|* [port] [= protocol]
Local host name or IP address and port for bin= d. If specified, OpenVPN will bind
to this address. If u= nspecified, OpenVPN will bind to all interfaces.
'*&= #39; can be used as hostname and means 'any host' (OpenVPN will lis= ten on what
diff --git a/src/openvpn/forward.c b/src/openvp= n/forward.c
index e7518e7..e1edbac 100644
-= -- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -1772,7 +1772,7 @@
if (c->op= tions.shaper)
{
= int overhead =3D datagram_overhead(c->c2.to_link_addr->dest.addr.s= a.sa_family,
- = c->options.ce.proto);
= + = sock->info.proto);
s= haper_wrote_bytes(&c->c2.shaper,
= BLEN(&c->c2.to_link) + overhead);
= }
@@ -2069,6 +2069,173 @@
*/<= /span>

void
+get_io_flags_dowork_udp(struct context *c, struct multi= _protocol *multi_io, const unsigned int flags)
+{
+ unsigned int socket =3D 0;
+ unsigned int tuntap =3D 0;
+ static uintptr_t tun_shift =3D 2;=
+ static uintptr_t err_s= hift =3D 4;
+<= br>+ /*
+ * Calculate the flags based on the = provided 'flags' argument.
+ */
+ {
+ wait_signal(multi_io->es, (void *)err_shift);
+ }

+
+ if (flags & IOW_TO_LINK)
+ {
+ if (flags & IOW_SHAPER)
+ {
+ /*
+ * If sending this packet would put us over our traff= ic shaping
+ = * quota, don't send -- instead compute the delay we must wait
+ * until it w= ill be OK to send the packet.
+ */
+ int delay =3D 0;
+
+ = /* set traffic shaping delay in microseconds */
+ if (c->options.shaper)
+ {<= br>+ delay =3D m= ax_int(delay, shaper_delay(&c->c2.shaper));
+ }
+
+ if (delay < 1000)
+ {
+ socket |=3D EVENT_WRITE;
+ }
+ else
+ {
+ shaper_soonest_event(&c->c2.timev= al, delay);
+ = }
+ }
+ else
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ socket |=3D EVENT_WRITE;
+ }
+ }

+ else if (!((flags & IOW_FRAG) &&= TO_LINK_FRAG(c)))
+ = {
+ if (fla= gs & IOW_READ_TUN)
+ = tuntap |=3D EVENT_READ;
+ }
+ = }
+
+ /*

+ * If outgoing data (for TUN/TAP device) pe= nding, wait for ready-to-send status
+ * from device. Otherwise, wait for incoming data on = TCP/UDP port.
+ *= /
+ if (flags &= ; IOW_TO_TUN)
+ {<= /span>
+ tuntap |=3D = EVENT_WRITE;
+ }
+ else
+ {

+ if (flags & IOW_READ_LINK)<= br>+ {
+ socket |=3D EVENT_READ;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ /*
+ = * outgoing bcast buffer waiting to be sent?
+ */
+ if (flags & IOW_MBUF)
+ {
+ socket |=3D EVENT_WRITE;
+ }
+
+ /*
+ * Force wait on T= UN input, even if also waiting on TCP/UDP output
+ */
+ if (flags & IOW_READ_TUN_FORCE)
+ {
+ tuntap |=3D EVENT_READ;
+ }
+
+#ifdef _WIN32
+ = if (tuntap_is_wintun(c->c1.tuntap))
+ {
+ /*
+ = * With wintun we are only interested in read event. Ring buffer is
+ * always ready= for write, so we don't do wait.
+ */
+ tuntap =3D EVENT_READ;
+ }
+#endif
+=
+ /*
+ * Configure event wait based on so= cket, tuntap flags.
+= */
+ for (int= i =3D 0; i < c->c1.link_sockets_num; i++)
+ {
+ if (proto_is_dgram(c->c2.link_sockets[i]->info= .proto))
+ {
+ socket_se= t(c->c2.link_sockets[i], multi_io->es, socket,
+ &c->c2.link= _sockets[i]->ev_arg, NULL);
+ }
= + }
+ tun_set(c= ->c1.tuntap, multi_io->es, tuntap, (void *)tun_shift, NULL);+
+ multi_io->udp_flags =3D socket | tuntap;=
+}
+
+void
+get_io_flags_udp(struct context *c, struct multi_protocol *multi_io, co= nst unsigned int flags)
+{
+ multi_io-&= gt;udp_flags =3D ES_ERROR;
+ if (c->c2.fast_io && (flags & (IOW_TO_TUN | IOW_T= O_LINK | IOW_MBUF)))
= + {
+ /* fa= st path -- only for TUN/TAP/UDP writes */
+ unsigned int ret =3D 0;
+ if (flags & IOW_TO_TUN)
+ {
+ ret |=3D TUN_WRITE;
+ }
+ if (flags & (IOW_TO_L= INK | IOW_MBUF))

+ = {
+ r= et |=3D SOCKET_WRITE;
+ }
+ = multi_io->udp_flags =3D ret;
+ }
+ = else
+ {<= br>+#ifdef _WIN32
+ bool skip_iowait =3D flag= s & IOW_TO_TUN;

+= if (flags & IOW_READ_TUN)
+ {
+ /*
+ * sinc= e every tun read overwrites to_link buffer filled
+ * by previous tun read+ */
+ skip_iowait =3D !(fla= gs & IOW_TO_LINK);

+ = if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+ {

+ unsigned int ret =3D 0;
+ if (flags &am= p; IOW_TO_TUN)
+ = {
+ = ret |=3D TUN_WRITE;
+ }
+ = if (flags & IOW_READ_TUN)
+ {
+ ret |=3D TUN_READ;
+ }
+ multi_io->udp_flags =3D ret;<= br>+ }
+ else
+#endif /* ifdef _WIN32 */
+ {
+ /* slow path - delegate to io_wait_d= owork_udp to calculate flags */
+ get_io_flags_dowork_udp(c, multi_io, flags);=
+ }
+ }
+}
+
+void<= br> io_wait_dowork(struct context *c, const unsigned int flags)
{
unsigned int socket =3D 0;
@@ -2310,6 +2477,7 @@

dmsg(D_EVENT_WAIT, "I/O = WAIT status=3D0x%04x", c->c2.event_set_status);
}<= /span>

+
void
process_io(struct context *c, struct = link_socket *sock)
{
diff --git a/src/open= vpn/forward.h b/src/openvpn/forward.h
index c99b779..141176= c 100644
--- a/src/openvpn/forward.h
+++ b/= src/openvpn/forward.h
@@ -69,6 +69,10 @@
<= /span>
extern counter_type link_write_bytes_global;

+void get_io_fla= gs_dowork_udp(struct context *c, struct multi_protocol *multi_io, const uns= igned int flags);
+
+void get_io_flags_udp= (struct context *c, struct multi_protocol *multi_io, const unsigned int fla= gs);
+
void io_wait_dowork(struct context *c, const unsigned int flags);
=

void pre_select(struct context *c);
= diff --git a/src/openvpn/init.c b/src/openvpn/init.c
= index 4eb286a..5877ebb 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -3829,6 +3829,24 @@=
unsigned int sockflags =3D c->options.sockflags;
int i;

+ int mode =3D LS_MODE_DEFAULT;
+ /* mode allows CM_CHILD_TCP
+ * instances to inhe= rit acceptable fds
+ = * from a top-level parent */
+ if (c->options.mode =3D=3D MODE_SERVER)
+ {

+ /* initializing listening socket */
+ if (c->mode = =3D=3D CM_TOP)
+ = {
+ mod= e =3D LS_MODE_TCP_LISTEN;
+ }
+ = /* initializing socket to client */
+ else if (c->mode =3D=3D CM_CHILD_TCP)=
+ {
+ mode =3D LS_MODE_TCP_ACC= EPT_FROM;
+ }<= /span>
+ }
+

#if PORT_SHARE
if (c->options.port_share_host && c->opti= ons.port_share_port)
{
@@ -3838,33 +38= 56,37 @@

for (i =3D 0; i < c->= c1.link_sockets_num; i++)
{
- int mode =3D LS_MODE_DEFAULT;
= + const char *host =3D c= ->options.ce.local_list->array[i]->local;
+ const char *port =3D c->options.c= e.local_list->array[i]->port;
+ int proto =3D c->options.ce.local_list->array= [i]->proto;
+ = bool bind_local =3D c->options.ce.local_list->array[i]->bind_lo= cal;

= - /* mode allows CM_CHILD_TCP
- * instances to inherit acceptable fds
- * from a top-level parent = */

- if (c->o= ptions.mode =3D=3D MODE_SERVER)
+ if (c->mode =3D=3D CM_CHILD_TCP || c->mode =3D=3D= CM_CHILD_UDP)
{
- /* initializing listening socket */<= br>- if (c->mode = =3D=3D CM_TOP)
+ = const struct link_socket *ls =3D NULL;
+ if (c->mode =3D=3D CM_CHILD_TCP)
{
- mode =3D LS_MODE_TCP_LISTEN;
+ ls =3D c->c2.accept_fr= om;
}
- /* initializing socket to client */
- else if (c->mode =3D= =3D CM_CHILD_TCP)

+ = else if (c->mode =3D=3D CM_CHILD_UDP)
= {
- = mode =3D LS_MODE_TCP_ACCEPT_FROM;
+ ls =3D c->c2.link_sockets[0];
<= span> }

+=
+ host = =3D ls->local_host;
+ proto =3D ls->info.proto;
<= span style=3D"color: hsl(120, 100%, 40%);">+ bind_local =3D ls-&= gt;bind_local;

}

/* init each socket with its specific port */

= link_socket_init_phase1(c->c2.link_sockets[i],
- c->opt= ions.ce.local_list->array[i]->local,
- c->options.ce.local= _list->array[i]->port,
+ host,
+ port,
= c->options.ce.remote,
<= span> c->options.ce.remote_port,
<= br> c->c1.dns_cache,
- c-&= gt;options.ce.proto,

= + proto,
= c->options.ce.af,
= c->options.ce.bind_ipv6_only,
= mode,
@@ -3874,7 +3896,7 @@
#ifdef ENABLE_DEBUG

c-&g= t;options.gremlin,
#endif
- c->options.ce.local= _list->array[i]->bind_local,
+ bind_local,
= c->options.ce.remote_float,
<= span> &c->c1.link_socket_addrs[i],
c->options.ipchange,
@@ -4685,7 +4707,7 @@
}
=
/* our wait-for-i/o objects, different for posix vs. = win32 */
- if (c->= ;mode =3D=3D CM_P2P)
= + if (c->mode =3D=3D CM_P2P || c->mode =3D=3D CM_TOP)
{

do_event_set_init(c, SHAPER_DEFINED(&am= p;c->options));
}
@@ -4949,7 +4971,= 7 @@
CLEAR(*dest);

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

+ dest->mode =3D proto_is_dgram(s= ock->info.proto) ? CM_CHILD_UDP : CM_CHILD_TCP;
=
dest->gc =3D gc_new();

= @@ -4977,6 +4999,8 @@
dest->options =3D src->opt= ions;
options_detach(&dest->options);
+ dest-&g= t;c2.event_set =3D src->c2.event_set;
+
if (dest->mode =3D=3D CM_CHIL= D_TCP)
{
/*
d= iff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c
ind= ex f4539dc..6c8e8dd 100644
--- a/src/openvpn/mroute.c
+++ b/src/openvpn/mroute.c
@@ -421,6 +421,7 @@
{
buf_= printf(&out, ":%d", ntohs(maddr.v4.port));
= }
+ = buf_printf(&out, ":%d", maddr.proto);
= }
break;
<= /span>
diff --git a/src/openvpn/mroute.h b/src/openvpn/mroute.h
index 8b457d4..fd1dbfe 100644
--- a/src/openv= pn/mroute.h
+++ b/src/openvpn/mroute.h
@@ -= 76,6 +76,7 @@
uint8_t len; /* length of address */<= /span>
uint8_t unused;
uint8_t type; /= * MR_ADDR/MR_WITH flags */
+ uint8_t proto;
uint8_t netbits; /* number o= f bits in network part of address,
*= valid if MR_WITH_NETBITS is set */
union {
= @@ -201,6 +202,10 @@
{
= return false;
}
+ if (a1->proto !=3D a2->proto)
+ {
+ return false;
+ }
if (a1->netbits !=3D= a2->netbits)
{
return fal= se;
@@ -222,7 +227,7 @@
static inline uint= 32_t
mroute_addr_hash_len(const struct mroute_addr *a)
{
- re= turn (uint32_t) a->len + 2;
+ return (uint32_t) a->len + 3;
}
static inline void
diff --git a/= src/openvpn/mtcp.c b/src/openvpn/mtcp.c
index 1dae184..97a6= 228 100644
--- a/src/openvpn/mtcp.c
+++ b/s= rc/openvpn/mtcp.c
@@ -54,7 +54,7 @@
st= ruct hash *hash =3D m->hash;

mi = =3D multi_create_instance(m, NULL, sock);
- if (mi)
+ if (mi && !proto_is_dgram(sock->info.proto))
{
struct hash_element *he;
const uint32_t hv =3D hash_value(hash, &mi->real= );
@@ -241,87 +241,3 @@
}
return ret;

}
-
-/*<= /span>
- * Top level event loo= p for single-threaded operation.
- * TCP mode.
- */
-void
<= span style=3D"color: hsl(0, 100%, 40%);">-tunnel_server_tcp(struct context = *top)

-{
- struct multi_context multi;
- int status;
= -
- top->mode =3D CM_TOP;
- context_clear_2(top);
-
- /* initialize top-tunnel instance */
- init_instance_handle_signals(top, top-&= gt;es, CC_HARD_USR1_TO_HUP);
- if (IS_SIG(top))
- {
- re= turn;
- }
= -
- /* initialize global multi_context object */
- multi_init(&multi,= top, true);
-- /* initialize our cloned t= op object */
- multi= _top_init(&multi, top);
-
- /* initiali= ze management interface */
- init_management_callback_multi(&multi);
-
- /* finished with initialization */
- initialization_sequence_completed(top, ISC_= SERVER); /* --mode server --proto tcp-server */
-
-#ifdef ENABLE_ASYNC_PUSH
- multi.top.c2.inotify_fd =3D inotify_init();
- if (multi.top.c2.inotify_fd < 0)
- {
- msg(D_MULTI_ERRORS | M_ERRNO, &= quot;MULTI: inotify_init error");
- }
= -#endif
-
- /* per-packet event loop */
- while (true)- {
- perf_push(PERF_EVENT_LOOP);
= -
- /* wait on tun/socket list */
- multi_get_timeout(&multi,= &multi.top.c2.timeval);

- status =3D multi_protocol_io_wait(&multi);
- MULTI_CHECK_SIG(&multi)= ;

-
- /* check on status of coarse time= rs */
- multi_pr= ocess_per_second_timers(&multi);
-
- = /* timeout? */
- = if (status > 0)
-= {
- = /* process the I/O which triggered select */
- multi_protocol_process_io(&multi);
- MULTI_CHECK= _SIG(&multi);
- = }
- else if = (status =3D=3D 0)
- = {
- mult= i_protocol_action(&multi, NULL, TA_TIMEOUT, false);
- }
-
= - perf_pop();
- = }
-
-#ifdef ENABLE_ASYNC_PUSH
- close(top->c2.inotify_fd);
-#endif
-
- /* shut down management interface */
- uninit_management_callback();=
-
- /* save ifconfig-pool */
- multi_ifconfig_pool_persist(&multi= , true);
-
- /* tear down tunnel instance (= unless --persist-tun) */

- multi_top_free(&multi);
- close_instance(top);
-}
diff --git a/src/openvpn/mtcp.h b= /src/openvpn/mtcp.h
index b9f73d4..51b209b 100644--- a/src/openvpn/mtcp.h
+++ b/src/openvpn/mtcp.h
@@ -51,17 +51,6 @@

void mu= lti_tcp_link_out_deferred(struct multi_context *m, struct multi_instance *m= i);

-=
-/********************= ******************************************************/
-/**
- * Main event loop for OpenVPN in TCP server mode.- * @ingroup eventloop- *
- * @param top - Top-level context structure.
- */
-void tunnel_server_tcp(struct context *top)= ;
-
-
void multi_tcp_delete_e= vent(struct multi_protocol *multi_io, event_t event);

#endif /* ifndef MTCP_H */
diff --git a/src/o= penvpn/mudp.c b/src/openvpn/mudp.c
index 5092665..660ea2d 1= 00644
--- a/src/openvpn/mudp.c
+++ b/src/op= envpn/mudp.c
@@ -192,6 +192,7 @@
struc= t mroute_addr real =3D {0};
struct multi_instance *mi = =3D NULL;
struct hash *hash =3D m->hash;
= + real.proto =3D sock->in= fo.proto;

if (mroute_extract_openvpn= _sockaddr(&real, &m->top.c2.from.dest, true)
= && m->top.c2.buf.len > 0)
@@ -318,18 +3= 19,66 @@
msg_set_prefix("Connection Attempt&q= uot;);
m->top.c2.to_link =3D m->hmac_reply;<= /span>
m->top.c2.to_link_addr =3D m->hmac_reply_des= t;
- process_out= going_link(&m->top, m->top.c2.link_sockets[0]);
+ for (int i =3D 0; i < m-&g= t;top.c1.link_sockets_num; i++)
+ {
+ if (!proto_is_dgram(m->top.c2.link_sockets[i]->info.pro= to))
+ {
+ conti= nue;
+ }
+
+ process_outgoing_link(&m= ->top, m->top.c2.link_sockets[i]);
+ }
m->hmac_reply_de= st =3D NULL;
}
}

/*
+ *= Return the io_wait() flags appropriate for
+ * a point-to-multipoint tunnel.
+ */
+unsigned int
+p2mp_iow_flags(const struct multi_context *m)
+{

+ unsigned int flags =3D IOW_WAIT_SIGNAL;+ if (m->pending)=
+ {
+ if (TUN_OUT(&m->pending-&= gt;context))
+ = {
+ flags= |=3D IOW_TO_TUN;
+ = }
+ if (= LINK_OUT(&m->pending->context))
+ {
+ flags |=3D IOW_TO_LINK;
+ }
+ }
+ else if (mbuf_defined(m->mbuf))
+ {
+ flags |=3D IOW_MBUF;
+ }
+ else if (m->hmac_reply_dest)
+ {
+ flags |=3D IOW_TO_LINK;
+ }
+ else
+ {
+ flags |=3D IO= W_READ;
+ }=
+#ifdef _WIN32
+ if (tuntap_ring_empty(m->= top.c1.tuntap))

+ = {
+ flags &= ;=3D ~IOW_READ_TUN;
+= }
+#endif<= br>+ return flags;+}
+
+/*
* Process an I/O event.
*/<= /span>
-static void
= +void
multi_pr= ocess_io_udp(struct multi_context *m)
{
- const unsigned int status =3D m->= top.c2.event_set_status;
+ const unsigned int status =3D m->multi_io->udp_flags;=
const unsigned int mpp_flags =3D m->top.c2.fast_io=
? (MPP_CONDITIONAL_PRE_SELECT= | MPP_CLOSE_ON_SIGNAL)
= : (MPP_PRE_SELECT | MPP_CLOSE_ON_SIGNAL);
@@ -427,123 +476= ,3 @@
}
#endif
}
-
-/*
- * Return the io_wait() flags appropriate for
- * a point-to-multipoint tunnel.- */
-static inline unsigned int
-p2mp_iow_flags(const struct multi_context *= m)
-{
- unsigned int flags =3D IOW_WAIT_SIG= NAL;
- if (m->pen= ding)
- {
= - if (TUN_OUT(&m->p= ending->context))
- = {
- f= lags |=3D IOW_TO_TUN;
-= }
- if (= LINK_OUT(&m->pending->context))
- {
- flags |=3D IOW_TO_LINK;
- }
- }
- el= se if (mbuf_defined(m->mbuf))
- {
- = flags |=3D IOW_MBUF;
- }
- else if (m= ->hmac_reply_dest)
-= {
- flags |= =3D IOW_TO_LINK;
- }=
- else
- {

- flags |=3D IOW_READ;
- }
-#ifdef _WIN32
- if (tuntap_ring_empty(m->top.c1.tuntap))
- {
- flags &=3D ~IOW_READ_TUN;
- }
-#endif
-}=
-
-
-void
-tunne= l_server_udp(struct context *top)
-{
- stru= ct multi_context multi;
-
- top->mode = =3D CM_TOP;
- contex= t_clear_2(top);
-
- /* initialize top-tunne= l instance */
- init= _instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP);
<= span style=3D"color: hsl(0, 100%, 40%);">- if (IS_SIG(top))

- {

- return;
- }
- /* initialize = global multi_context object */
- multi_init(&multi, top, false);
-
- /* initialize our cloned top object */
- multi_top_init(&multi, top);
<= span style=3D"color: hsl(0, 100%, 40%);">-

- /* initialize management interface */
- init_management_callback_multi= (&multi);

-<= br>- /* finished with initiali= zation */
- initiali= zation_sequence_completed(top, ISC_SERVER); /* --mode server --proto udp */=
-
-#ifdef ENABLE_ASYNC_PUSH
- multi.top.c2.inotify_fd =3D inotify= _init();
- if (multi= .top.c2.inotify_fd < 0)
- {
- msg= (D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");- }
-#endif
-
- /* = per-packet event loop */
- = {
- perf_push(= PERF_EVENT_LOOP);
-
- /* set up and do = the io_wait() */
- = multi_get_timeout(&multi, &multi.top.c2.timeval);
- io_wait(&multi.top, p2mp= _iow_flags(&multi));

-
- = /* check on status of coarse timers */
- multi_process_per_second_timers(&multi);<= /span>
-
- /* timeout? */
- if (multi.top.c2.event_set_status = =3D=3D ES_TIMEOUT)
- = {
- mul= ti_process_timeout(&multi, MPP_PRE_SELECT|MPP_CLOSE_ON_SIGNAL);<= br>- }
- else
- {
- /* process I/O */
- multi_process_io_udp(&multi);- MULTI_CHECK_SIG(&a= mp;multi);
- }
-
- perf_pop();
- }
-
-#ifde= f ENABLE_ASYNC_PUSH
- = close(top->c2.inotify_fd);
-#endif
-
- /* shut down managemen= t interface */
- uni= nit_management_callback();
-
- /* save ifco= nfig-pool */
- multi= _ifconfig_pool_persist(&multi, true);
-
- = /* tear down tunnel instance (unless --persist-tun) */
- multi_uninit(&multi);- multi_top_free(&multi);=
- close_instance(to= p);
-}
= diff --git a/src/openvpn/mudp.h b/src/openvpn/mudp.h
index = 3107cc6..e329832 100644
--- a/src/openvpn/mudp.h
= +++ b/src/openvpn/mudp.h
@@ -31,18 +31,9 @@ struct context;
struct multi_context;

+unsigned int p= 2mp_iow_flags(const struct multi_context *m);

<= span style=3D"color: hsl(0, 100%, 40%);">-/**

- * Main event loop for OpenVPN in UDP server mode.<= /span>
- * @ingroup eventloop<= /span>
- *
- * This function implements OpenVPN's= main event loop for UDP server mode.
- *
- * = @param top - Top-level context structure.
- */
-void tunnel_server_udp(struct context *top);
-
-
+void multi_proc= ess_io_udp(struct multi_context *m);
/********************= ******************************************************/
/*= *
* Get, and if necessary create, the multi_instance asso= ciated with a
diff --git a/src/openvpn/multi.c b/src/openvp= n/multi.c
index fc700de..bf98c7f 100644
---= a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
= @@ -289,7 +289,7 @@
* Main initialization function,= init multi_context object.
*/
void
-multi_init(struct multi_co= ntext *m, struct context *t, bool tcp_mode)
+multi_init(struct multi_context *m, struct context = *t)
{
int dev =3D DEV_TYPE_UNDEF;

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

m->instances =3D calloc(m->max_clients, sizeof(struc= t multi_instance *));

+ m->top.c2.event_set =3D t->c2.event_set;
+
/*=
- * Initialize mul= ti-socket TCP I/O wait object
+ * Initialize multi-socket I/O wait object
= */
- if (tcp_mod= e)
- {
- m->multi_io =3D multi_pr= otocol_init(t->options.max_clients, &m->max_clients);

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

+ m->multi_io =3D multi_protocol_init(t-&g= t;options.max_clients, &m->max_clients);
m->= tcp_queue_limit =3D t->options.tcp_queue_limit;
=
/*
@@ -665,7 +664,7 @@
= mi->did_iroutes =3D false;
}
=
- if (m-= >multi_io)
+ = if (m->multi_io && !proto_is_dgram(m->top.options.ce.proto)= )
{
multi_tcp_derefer= ence_instance(m->multi_io, mi);
}
@@ -776,7 +775,6 @@

mi->real =3D *real;
generate_prefix(mi);
}
= -
mi->did= _open_context =3D true;
inherit_context_child(&mi-= >context, &m->top, sock);
if (IS_SIG(&mi= ->context))
@@ -798,6 +796,7 @@
= {
goto err;
}=
+ mi->real.proto = =3D sock->info.proto;
generate_prefix(mi);
}

@@ -4158,6 +4157,98 @@=
ASSERT(mi->context.c2.tls_multi->peer_id < m= ->max_clients);
}

+/**************************************= ************************************/
+/**
+ * Main event loop for OpenVPN in multi-protocol server mode.
<= span style=3D"color: hsl(120, 100%, 40%);">+ * @ingroup eventloop
+ *
+ * @param top - Top-level context structure.
+ */
+void
+tunnel_server_loop(struct multi_context *multi)
+{
+ int status;
+
+ while (true)
+ {
+ = perf_push(PERF_EVENT_LOOP);
+
+ /= * wait on tun/socket list */
+ multi_get_timeout(multi, &multi->top.c2.timeval);
+ status =3D mu= lti_protocol_io_wait(multi);
+ MULTI_CHECK_SIG(multi);
+
+ /* check on status of coarse timers */
+ multi_process_per_second_timers(multi)= ;
+
+ /* timeout? */
+ if (status > 0)
<= br>+ {
+ /* process the I/O which = triggered select */
+= multi_protocol_process_io(multi);
+ MULTI_CHECK_SIG(multi);
+ }

+ else if (status =3D=3D 0)<= br>+ {
+ multi_protocol_action(mul= ti, NULL, TA_TIMEOUT, false);
+ }
+=
+ perf_pop();=
+ }
+}

+
+void
+tunnel_ser= ver_init(struct context *top)
+{
+ stru= ct multi_context multi;
+
+ top->mod= e =3D CM_TOP;
+ co= ntext_clear_2(top);
+=
+ /* initialize t= op-tunnel instance */
+ init_instance_handle_signals(top, top->es, CC_HARD_USR1_TO_HUP);
+ if (IS_SIG(top))<= /span>
+ {
+ return;

+ }
+
+ /* initialize global multi_context object */
+ multi_init(&multi, top);+
+ /* initialize our cloned top object */
+ multi_top_init(&= multi, top);
+=
+ /* initialize manageme= nt interface */
+ = init_management_callback_multi(&multi);
+
+ /* finished with initialization */
+ initialization_sequence_completed(top, ISC_SERV= ER); /* --mode server --proto tcp-server */
+
+#ifdef ENABLE_ASYNC_PUSH
+ multi.top.c2.inotify_fd =3D inotify_init();
+ if (multi.top.c2.inotify_fd < = 0)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ msg(D_MULTI_ERRORS | M_= ERRNO, "MULTI: inotify_init error");

+ }
+#endif
+=
+ tunnel_server_l= oop(&multi);
+
+ #ifdef ENABLE_ASYN= C_PUSH
+ close(top= ->c2.inotify_fd);
= +#endif
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* shut down management int= erface */

+ uninit= _management_callback();
+
+ /* save ifc= onfig-pool */
+ mu= lti_ifconfig_pool_persist(&multi, true);
+
+ /* tear down tunnel instance (unless --persist-tun) */
= + multi_uninit(&multi);<= /span>
+ multi_top_free(&= amp;multi);
+ clos= e_instance(top);
+}

/*
* Top level event lo= op.
@@ -4167,12 +4258,6 @@
{
ASSERT(top->options.mode =3D=3D MODE_SERVER);

<= /span>
- if (proto_is_dgram= (top->options.ce.proto))
- {
- tu= nnel_server_udp(top);
-= }
- else<= br>- {
- tunnel_server_tcp(top);
<= span style=3D"color: hsl(0, 100%, 40%);">- }

+ tunnel_server_init(top);
+
}
di= ff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
index = 930d6b9..0a15fd9 100644
--- a/src/openvpn/multi.h+++ b/src/openvpn/multi.h
@@ -252,10 +252,8 @@
* Main event loop for OpenVPN in server mode.
= * @ingroup eventloop
*
- * This function calls the appropriate main event loop= function depending
- *= on the transport protocol used:
- * - \c tunnel_server_udp()
- * - \c tunnel_server_tcp()
+ * This function calls the main event loop funct= ion
+ * on the transp= ort protocols:
*
* @param top = - Top-level context structure.
*/
@@ -268= ,7 +266,7 @@
* Called by mtcp.c, mudp.c, or other (to be = written) protocol drivers
*/

= -void multi_init(struct multi_con= text *m, struct context *t, bool tcp_mode);
+void multi_init(struct multi_context *m, struct con= text *t);

void multi_uninit(struct multi= _context *m);

@@ -664,9 +662,12 @@=
multi_process_outgoing_tun(struct multi_context *m, const unsign= ed int mpp_flags)
{
struct multi_inst= ance *mi =3D m->pending;
+ if (!mi)
= + {
+ retur= n false;
+ }
bool ret =3D true;

- ASSERT(mi);
#ifdef MUL= TI_DEBUG_EVENT_LOOP
printf("%s -> TUN len=3D%d= \n",
id(mi),
diff --git a/= src/openvpn/openvpn.h b/src/openvpn/openvpn.h
index 77bb41d= ..821ac2e 100644
--- a/src/openvpn/openvpn.h
+++ b/src/openvpn/openvpn.h

@@ -220,8 +220,8 @@ * \c SIGUSR1 restarts.
*
* Thi= s structure is initialized at the top of the \c
- * tunnel_point_to_point(), \c tunnel_server_udp(= ), and \c
- * tunnel_se= rver_tcp() functions. In other words, it is reset for every
+ * tunnel_point_to_point(), \c tun= nel_server() \c

+ * f= unctions. In other words, it is reset for every
* iterat= ion of the \c main() function's inner \c SIGUSR1 loop.
= */
struct context_2
diff --git a/src/ope= nvpn/options.c b/src/openvpn/options.c
index f216456..6f4d5= e2 100644
--- a/src/openvpn/options.c
+++ b= /src/openvpn/options.c
@@ -2199,6 +2199,7 @@
}


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

return e;
= @@ -3170,14 +3171,30 @@
if (ce->proto =3D=3D PR= OTO_TCP)
{
ce->pro= to =3D PROTO_TCP_SERVER;
+ o->ce.proto =3D ce->proto;
+ }
+ if (ce->local_list)
+ {
+ for (int i =3D 0; i < ce->local_list= ->len; i++)
+ = {
+ = if (ce->local_list->array[i]->proto =3D=3D PROTO_TCP)<= br>+ {+ ce->lo= cal_list->array[i]->proto =3D PROTO_TCP_SERVER;
+ }
+ else if (ce->local_lis= t->array[i]->proto =3D=3D PROTO_NONE)
+ {
+ ce->local_list->array[i]-&g= t;proto =3D ce->proto;
+ }
+ }
}
}<= br>
- if (o-&= gt;client)
+ if (o= ->mode !=3D MODE_SERVER)
{
= if (ce->proto =3D=3D PROTO_TCP)
{
ce->proto =3D PROTO_TCP_CLIENT;

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

@@ -3331,6 +3348,10 @@
{
= le->port =3D o->ce.local_port;
}=
+ if (!le->proto)
+ {
+ le->proto =3D o->ce.pro= to;
+ }
= }

#ifdef _WIN32
@= @ -3822,6 +3843,7 @@
ASSERT(e);
= e->port =3D o->ce.local_port;
e->b= ind_local =3D o->connection_list->array[0]->bind_local;
= + e->proto =3D o->= ce.proto;
}

/* = use the same listen list for every outgoing connection */
@= @ -6232,7 +6254,7 @@
VERIFY_PERMISSION(OPT_P_UP);<= /span>
options->ifconfig_nowarn =3D true;
}

- else if= (streq(p[0], "local") && p[1] && !p[3])+ else if (streq(p[0], &qu= ot;local") && p[1] && !p[4])
{
struct local_entry *e;

<= span>@@ -6253,7 +6275,12 @@

if (p[2])
{

e->port =3D p[2];
= - e->bind_local =3D= true;
+ /= *e->bind_local =3D true; */
+ }
= +
+ if (p[3])<= /span>
+ {
= + e->proto =3D as= cii2proto(p[3]);
}
} else if (streq(p[0], "remote-random") && !p[= 1])
diff --git a/src/openvpn/options.h b/src/openvpn/option= s.h
index 6942455..f3cf7e5 100644
--- a/src= /openvpn/options.h
+++ b/src/openvpn/options.h
@@ -99,6 +99,7 @@

const char *local;
const char *port;

bool bind_local;
<= span style=3D"color: hsl(120, 100%, 40%);">+ int proto;

= };

struct connection_entry

To view, visit change 764. To unsubscribe, or for help w= riting mail filters, visit s= ettings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I31bbf87e4e568021445c7512ece= fadfd4a69b363
Gerrit-Change-Number: 764
Gerrit-PatchSet: 2
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: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--phXtrrDifWA=--