From patchwork Wed Apr 25 09:57:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 324 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.27.255.51]) by backend30.mail.ord1d.rsapps.net (Dovecot) with LMTP id vBFEDjje4FrqOQAAIUCqbw for ; Wed, 25 Apr 2018 15:59:52 -0400 Received: from proxy3.mail.iad3a.rsapps.net ([172.27.255.51]) by director7.mail.ord1d.rsapps.net (Dovecot) with LMTP id of+aDjje4FrRaAAAovjBpQ ; Wed, 25 Apr 2018 15:59:52 -0400 Received: from smtp13.gate.iad3a ([172.27.255.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy3.mail.iad3a.rsapps.net with LMTP id OAppEzje4FoqJgAAYaqY3Q ; Wed, 25 Apr 2018 15:59:52 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp13.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=unstable.cc X-Suspicious-Flag: YES X-Classification-ID: 36b456b4-48c3-11e8-a2a0-5254004b83b1-1-1 Received: from [216.105.38.7] ([216.105.38.7:48536] helo=lists.sourceforge.net) by smtp13.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id E5/94-28091-73ED0EA5; Wed, 25 Apr 2018 15:59:51 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1fBQZ9-0000pI-Ik; Wed, 25 Apr 2018 19:58:59 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1fBQZ7-0000ou-GM for openvpn-devel@lists.sourceforge.net; Wed, 25 Apr 2018 19:58:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=9Mem1bkbl+o7S88Cf8zA3Sm8++ufPZ6HCMODaYriVI0=; b=C/TmNtEHPuRdCK73VfwWs4mmVz OeEU0h3sPe3IdCEPFNSdXz98734X74BUbvGyoT72pKJg+XXmc8+SVgcgOq26275gM7Od3TToamZZr piSAWeG3okWx05ExR/OwEmji9N4rARLZKBC7PGBu4GJTC5H89i4Cy23/MIsKGw3HKb5w=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=9Mem1bkbl+o7S88Cf8zA3Sm8++ufPZ6HCMODaYriVI0=; b=MSUmwbXjxbCWDTyGQ/6zxzOIOO diY054WnlsiYe0vifKVCWVsqg5Xpq0FHHZcbZze6NSPJGyvKDOXND9G/iYBRrhGTnqrbQLPitASjk NHWHan5lieH0HNZILANgjQZOCwWxboFvNDPCFkqQY7pix95kA6xpGf4siOgESKzu4dxc=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1fBQZ5-00Bt3J-Cg for openvpn-devel@lists.sourceforge.net; Wed, 25 Apr 2018 19:58:57 +0000 From: Antonio Quartulli To: openvpn-devel@lists.sourceforge.net Date: Thu, 26 Apr 2018 03:57:16 +0800 Message-Id: <20180425195722.20744-3-a@unstable.cc> In-Reply-To: <20180425195722.20744-1-a@unstable.cc> References: <20180425195722.20744-1-a@unstable.cc> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [5.148.176.60 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1fBQZ5-00Bt3J-Cg Subject: [Openvpn-devel] [RFC 2/8] pass link_socket object to i/o functions X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Antonio Quartulli MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox In order to prepare the code to work with distinct sockets, it is essential that i/o functions do not operate on any hard-coded socket object (i.e. c->c2.link_socket). This patch changes all the low-level i/o functionis to work with a socket specified as argument rather than a fixed one. Signed-off-by: Antonio Quartulli --- src/openvpn/event.h | 1 + src/openvpn/forward.c | 49 ++++++++++++++++++++++--------------------- src/openvpn/forward.h | 17 ++++++++------- src/openvpn/mtcp.c | 2 +- src/openvpn/mudp.c | 2 +- src/openvpn/multi.h | 2 +- src/openvpn/openvpn.c | 2 +- src/openvpn/socket.c | 1 + 8 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/openvpn/event.h b/src/openvpn/event.h index 33ff8890..be199ad9 100644 --- a/src/openvpn/event.h +++ b/src/openvpn/event.h @@ -96,6 +96,7 @@ struct event_arg event_arg_t type; union { struct multi_instance *mi; /* if type = EVENT_ARG_MULTI_INSTANCE */ + struct link_socket *ls; /* if type = EVENT_ARG_LINK_SOCKET */ } u; }; diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 7d9a338d..c5ddf385 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -663,9 +663,9 @@ check_timeout_random_component(struct context *c) */ static inline void -socks_postprocess_incoming_link(struct context *c) +socks_postprocess_incoming_link(struct context *c, struct link_socket *ls) { - if (c->c2.link_socket->socks_proxy && c->c2.link_socket->info.proto == PROTO_UDP) + if (ls->socks_proxy && ls->info.proto == PROTO_UDP) { socks_process_incoming_udp(&c->c2.buf, &c->c2.from); } @@ -673,13 +673,14 @@ socks_postprocess_incoming_link(struct context *c) static inline void socks_preprocess_outgoing_link(struct context *c, + struct link_socket *ls, struct link_socket_actual **to_addr, int *size_delta) { - if (c->c2.link_socket->socks_proxy && c->c2.link_socket->info.proto == PROTO_UDP) + if (ls->socks_proxy && ls->info.proto == PROTO_UDP) { *size_delta += socks_process_outgoing_udp(&c->c2.to_link, c->c2.to_link_addr); - *to_addr = &c->c2.link_socket->socks_relay; + *to_addr = &ls->socks_relay; } } @@ -704,7 +705,7 @@ link_socket_write_post_size_adjust(int *size, */ void -read_incoming_link(struct context *c) +read_incoming_link(struct context *c, struct link_socket *ls) { /* * Set up for recvfrom call to read datagram @@ -719,17 +720,17 @@ read_incoming_link(struct context *c) c->c2.buf = c->c2.buffers->read_link_buf; ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM_ADJ(&c->c2.frame, FRAME_HEADROOM_MARKER_READ_LINK))); - status = link_socket_read(c->c2.link_socket, + status = link_socket_read(ls, &c->c2.buf, &c->c2.from); - if (socket_connection_reset(c->c2.link_socket, status)) + if (socket_connection_reset(ls, status)) { #if PORT_SHARE - if (port_share && socket_foreign_protocol_detected(c->c2.link_socket)) + if (port_share && socket_foreign_protocol_detected(ls)) { - const struct buffer *fbuf = socket_foreign_protocol_head(c->c2.link_socket); - const int sd = socket_foreign_protocol_sd(c->c2.link_socket); + const struct buffer *fbuf = socket_foreign_protocol_head(ls); + const int sd = socket_foreign_protocol_sd(ls); port_share_redirect(port_share, fbuf, sd); register_signal(c, SIGTERM, "port-share-redirect"); } @@ -763,10 +764,10 @@ read_incoming_link(struct context *c) } /* check recvfrom status */ - check_status(status, "read", c->c2.link_socket, NULL); + check_status(status, "read", ls, NULL); /* Remove socks header if applicable */ - socks_postprocess_incoming_link(c); + socks_postprocess_incoming_link(c, ls); perf_pop(); } @@ -996,11 +997,11 @@ process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, con } static void -process_incoming_link(struct context *c) +process_incoming_link(struct context *c, struct link_socket *ls) { perf_push(PERF_PROC_IN_LINK); - struct link_socket_info *lsi = get_link_socket_info(c); + struct link_socket_info *lsi = &ls->info; const uint8_t *orig_buf = c->c2.buf.data; process_incoming_link_part1(c, lsi, false); @@ -1293,7 +1294,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf) */ void -process_outgoing_link(struct context *c) +process_outgoing_link(struct context *c, struct link_socket *ls) { struct gc_arena gc = gc_new(); int error_code = 0; @@ -1335,7 +1336,7 @@ process_outgoing_link(struct context *c) #if PASSTOS_CAPABILITY /* Set TOS */ - link_socket_set_tos(c->c2.link_socket); + link_socket_set_tos(ls); #endif /* Log packet send */ @@ -1346,7 +1347,7 @@ process_outgoing_link(struct context *c) } #endif msg(D_LINK_RW, "%s WRITE [%d] to %s: %s", - proto2ascii(c->c2.link_socket->info.proto, c->c2.link_socket->info.af, true), + proto2ascii(ls->info.proto, ls->info.af, true), BLEN(&c->c2.to_link), print_link_socket_actual(c->c2.to_link_addr, &gc), PROTO_DUMP(&c->c2.to_link, &gc)); @@ -1357,10 +1358,10 @@ process_outgoing_link(struct context *c) int size_delta = 0; /* If Socks5 over UDP, prepend header */ - socks_preprocess_outgoing_link(c, &to_addr, &size_delta); + socks_preprocess_outgoing_link(c, ls, &to_addr, &size_delta); /* Send packet */ - size = link_socket_write(c->c2.link_socket, + size = link_socket_write(ls, &c->c2.to_link, to_addr); @@ -1393,7 +1394,7 @@ process_outgoing_link(struct context *c) /* Check return status */ error_code = openvpn_errno(); - check_status(size, "write", c->c2.link_socket, NULL); + check_status(size, "write", ls, NULL); if (size > 0) { @@ -1797,7 +1798,7 @@ io_wait_dowork(struct context *c, const unsigned int flags) } void -process_io(struct context *c) +process_io(struct context *c, struct link_socket *ls) { const unsigned int status = c->c2.event_set_status; @@ -1812,7 +1813,7 @@ process_io(struct context *c) /* TCP/UDP port ready to accept write */ if (status & SOCKET_WRITE) { - process_outgoing_link(c); + process_outgoing_link(c, ls); } /* TUN device ready to accept write */ else if (status & TUN_WRITE) @@ -1822,10 +1823,10 @@ process_io(struct context *c) /* Incoming data on TCP/UDP port */ else if (status & SOCKET_READ) { - read_incoming_link(c); + read_incoming_link(c, ls); if (!IS_SIG(c)) { - process_incoming_link(c); + process_incoming_link(c, ls); } } /* Incoming data on TUN device */ diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index 924cc5e2..16639f75 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -63,7 +63,8 @@ void pre_select(struct context *c); -void process_io(struct context *c); +void process_io(struct context *ci, struct link_socket *ls); + const char *wait_status_string(struct context *c, struct gc_arena *gc); @@ -124,10 +125,11 @@ int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout); * context associated with the appropriate VPN tunnel for which data is * available to be read. * - * @param c - The context structure which contains the external - * network socket from which to read incoming packets. + * @param c The context structure which contains the external + * network socket from which to read incoming packets. + * @param ls The socket where the packet can be read from. */ -void read_incoming_link(struct context *c); +void read_incoming_link(struct context *c, struct link_socket *ls); /** * Starts processing a packet read from the external network interface. @@ -193,10 +195,11 @@ void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi * * If an error occurs, it is logged and the packet is dropped. * - * @param c - The context structure of the VPN tunnel associated with the - * packet. + * @param c The context structure of the VPN tunnel associated with the + * packet. + * @param ls The socket to be used to send the packet. */ -void process_outgoing_link(struct context *c); +void process_outgoing_link(struct context *c, struct link_socket *ls); /**************************************************************************/ diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index 234c077c..51f339d0 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -470,7 +470,7 @@ multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int ASSERT(mi); ASSERT(mi->context.c2.link_socket); set_prefix(mi); - read_incoming_link(&mi->context); + read_incoming_link(&mi->context, mi->context.c2.link_socket); clear_prefix(); if (!IS_SIG(&mi->context)) { diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index b3690ab7..80efefb3 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -227,7 +227,7 @@ multi_process_io_udp(struct multi_context *m) /* Incoming data on UDP port */ else if (status & SOCKET_READ) { - read_incoming_link(&m->top); + read_incoming_link(&m->top, m->top.c2.link_socket); if (!IS_SIG(&m->top)) { multi_process_incoming_link(m, NULL, mpp_flags); diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index c52445ed..5fb8402f 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -637,7 +637,7 @@ multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instanc { bool ret = true; set_prefix(mi); - process_outgoing_link(&mi->context); + process_outgoing_link(&mi->context, mi->context.c2.link_socket); ret = multi_process_post(m, mi, mpp_flags); clear_prefix(); return ret; diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c index b9e914a8..b52063a2 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -115,7 +115,7 @@ tunnel_point_to_point(struct context *c) } /* process the I/O which triggered select */ - process_io(c); + process_io(c, c->c2.link_socket); P2P_CHECK_SIG(); perf_pop(); diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index e0620f09..564948a2 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1713,6 +1713,7 @@ link_socket_new(void) sock->sd = SOCKET_UNDEFINED; sock->ctrl_sd = SOCKET_UNDEFINED; sock->ev_arg.type = EVENT_ARG_LINK_SOCKET; + sock->ev_arg.u.ls = sock; return sock; }