From patchwork Thu Jan 13 10:52:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 2221 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.30.191.6]) by backend41.mail.ord1d.rsapps.net with LMTP id 0JMQMcBv4WEfFwAAqwncew (envelope-from ) for ; Fri, 14 Jan 2022 07:42:40 -0500 Received: from proxy18.mail.ord1d.rsapps.net ([172.30.191.6]) by director8.mail.ord1d.rsapps.net with LMTP id UBGYOMBv4WH+NQAAfY0hYg (envelope-from ) for ; Fri, 14 Jan 2022 07:42:40 -0500 Received: from smtp7.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy18.mail.ord1d.rsapps.net with LMTPS id uJk5OMBv4WEwbAAATCaURg (envelope-from ) for ; Fri, 14 Jan 2022 07:42:40 -0500 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: smtp7.gate.ord1c.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; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: 75c37d4a-7537-11ec-84db-bc305bf04148-1-1 Received: from [216.105.38.7] ([216.105.38.7:57932] helo=lists.sourceforge.net) by smtp7.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 70/E1-24080-0CF61E16; Fri, 14 Jan 2022 07:42:40 -0500 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1n8Lrp-0002jM-Ae; Fri, 14 Jan 2022 12:39:41 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1n8Lrm-0002jC-Hf for openvpn-devel@lists.sourceforge.net; Fri, 14 Jan 2022 12:39:38 +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=bbR4nu+IBCkx43NaDd97BH/uYGS9RO+d3YJDAS71kJA=; b=i0xeqikPM1IakNpIzPwR0fRQvf McASur+IUck/0G26SgEsCBfx70H2xBhIMfjqwc6Fi+R0v1mJIp2i8pVXD13/8cxV9mbGefoPgS4tj /u+IWqaQE0u0b4yo9hNG7Wqw1KQ8xtsA+ScDcrtPUM1edepy2xsyMqHCw3rG9YAb+gS0=; 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=bbR4nu+IBCkx43NaDd97BH/uYGS9RO+d3YJDAS71kJA=; b=RnAnTyOqffNKkpVSUlE+L3WED3 a+oS031IQrBeC3ktxPp6bv9SOCG0RYDSR4r+OOyFkre5UaUwU7JdiR08t6lwcmewfTP0hTen58zAw r7WSENAz9k1msTd9AlOJbOSFo0pPDHneHFHIyR83k1N+mQ0Zb3yWtRC6/pAm4p6l3ZXM=; Received: from mail-wm1-f41.google.com ([209.85.128.41]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) id 1n8Lrf-00FY2W-97 for openvpn-devel@lists.sourceforge.net; Fri, 14 Jan 2022 12:39:37 +0000 Received: by mail-wm1-f41.google.com with SMTP id q9-20020a7bce89000000b00349e697f2fbso6635396wmj.0 for ; Fri, 14 Jan 2022 04:39:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bbR4nu+IBCkx43NaDd97BH/uYGS9RO+d3YJDAS71kJA=; b=CGMJ6zsXFESu70hXibSQ1AUXR7jcGpUjT7E/8OL+dzA8k/HR2fvhUVYAD4qIfVNkwI SvxIUwnZ2g0eJ/DVAS/f9lCE+5T1UEgZT/yocfJMQkD8N8rQqNC9vkunD8wYDuXRrHrf JsiR37S5BtSkuimb1wNr00aKa1TNJGPzIeNrt+ydxIufPVoY2+3J/swlXOETf3aVfYGF rc4Q6QA2bN/3IcLE4ut8z3AVIDQZVCd/LyQEAbSa4NfPtfxopd3tH+iRUcBuXefzn9cs ze3Std6xtt4OYoz8QEBKoBdVb9s9ws0mxR53UgZ0votf2yC4SsuFPWKDbCYeMCH1toIv Oz/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bbR4nu+IBCkx43NaDd97BH/uYGS9RO+d3YJDAS71kJA=; b=bd3P9qVsS7GxoSGd1zlURmGI4M298xn4e42d+VCqkG5miaYuRk6J5wZjfs6NBuXs/f nXF58AeF97RwFJ0cwi+xjLSl3Dva8OkuIlmoJgqFB3YKsVuXTp9J0PrRFfrTLEQK3KUD ZSLfi3ez1v/V6kcj6/YmcUV/OCoskF0NddMDe77A5qjyLu8Y/UMdnmhBGbgrL7lhUXmR Ov0TvtytD1rFJHSMhl5lTeSimu5dUPZKDcvqdRonAtchZHpKvtHrMEa6aIPY0cPTZOZv ysVXHwxIHOR6kORWl8Xay+19UPMrgId7MEfZD7dQ+HRId5aitaorMK8N0qKmA68ivbrQ TEBQ== X-Gm-Message-State: AOAM532ST3rDmcFMUIPh7Ve+TxcYG4OY++LRi6/yRrgis/Ccwi5NrQq8 4Dkx2ocgki+/GZrT/RC8AUxB7P6IUdU= X-Google-Smtp-Source: ABdhPJzqsuOFOMQigasKa5ADwCLpkIETAhb8//Hg4IYyCnJq3NxdlOoIu2g8jmdeoH/9HdRe2hpZmA== X-Received: by 2002:a7b:cf06:: with SMTP id l6mr15678099wmg.24.1642163946956; Fri, 14 Jan 2022 04:39:06 -0800 (PST) Received: from LAPTOP-4L3N7KFS.localdomain (nat2.panoulu.net. [185.38.2.2]) by smtp.gmail.com with ESMTPSA id bg12sm6642357wmb.5.2022.01.14.04.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jan 2022 04:39:06 -0800 (PST) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Thu, 13 Jan 2022 23:52:26 +0200 Message-Id: <20220113215226.161-1-lstipakov@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <222922b3-377a-3eab-1edc-7dc692471196@unstable.cc> References: <222922b3-377a-3eab-1edc-7dc692471196@unstable.cc> X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Lev Stipakov tun_finalize() is essentially subset of socket_finalize() apart from: - using WSAFoo() functions instead of Foo() Content analysis details: (0.6 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.128.41 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.128.41 listed in wl.mailspike.net] 0.0 T_SPF_TEMPERROR SPF: test of record failed (temperror) 0.8 DATE_IN_PAST_12_24 Date: is 12 to 24 hours before Received: date 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [lstipakov[at]gmail.com] -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1n8Lrf-00FY2W-97 Subject: [Openvpn-devel] [PATCH v2] tun: remove tun_finalize() 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: Lev Stipakov MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Lev Stipakov tun_finalize() is essentially subset of socket_finalize() apart from: - using WSAFoo() functions instead of Foo() - "from" address is not returned There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both. Introduce sockethandle_t abstraction, which represents socket and handle. Add SocketHandle* macros which call proper API depends on underlying type in abstraction. Rename socket_finalize() to sockethandle_finalize(), take sockethandle_t and new macros into use and kick tun_finalize(). Signed-off-by: Lev Stipakov --- v2: explicitly initialize .is_handle to false for readablity src/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 31 ++++++++++++-- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 65 insertions(+), 134 deletions(-) diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index f82386a1..a905f993 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) } else { - read_tun_buffered(c->c1.tuntap, &c->c2.buf); + sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; + sockethandle_finalize(sh, &c->c1.tuntap->reads, &c->c2.buf, NULL); } #else /* ifdef _WIN32 */ ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame))); diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index df736746..5687646d 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock, if (!sock->stream_buf.residual_fully_formed) { #ifdef _WIN32 - len = socket_finalize(sock->sd, &sock->reads, buf, NULL); + sockethandle_t sh = { .is_handle = false, .s = sock->sd }; + len = sockethandle_finalize(sh, &sock->reads, buf, NULL); #else struct buffer frag; stream_buf_get_next(&sock->stream_buf, &frag); @@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin } int -socket_finalize(SOCKET s, - struct overlapped_io *io, - struct buffer *buf, - struct link_socket_actual *from) +sockethandle_finalize(sockethandle_t sh, + struct overlapped_io *io, + struct buffer *buf, + struct link_socket_actual *from) { int ret = -1; BOOL status; @@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s, switch (io->iostate) { case IOSTATE_QUEUED: - status = WSAGetOverlappedResult( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status = SocketHandleGetOverlappedResult(sh, &io); if (status) { /* successful return for a queued operation */ @@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate = IOSTATE_INITIAL; ASSERT(ResetEvent(io->overlapped.hEvent)); - dmsg(D_WIN32_IO, "WIN32 I/O: Socket Completion success [%d]", ret); + dmsg(D_WIN32_IO, "WIN32 I/O: Completion success [%d]", ret); } else { /* error during a queued operation */ ret = -1; - if (WSAGetLastError() != WSA_IO_INCOMPLETE) + if (SocketHandleGetLastError(sh) != ERROR_IO_INCOMPLETE) { /* if no error (i.e. just not finished yet), then DON'T execute this code */ io->iostate = IOSTATE_INITIAL; ASSERT(ResetEvent(io->overlapped.hEvent)); - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Socket Completion error"); + msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion error"); } } break; @@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s, if (io->status) { /* error return for a non-queued operation */ - WSASetLastError(io->status); + SocketHandleSetLastError(sh, io->status); ret = -1; - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Socket Completion non-queued error"); + msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion non-queued error"); } else { @@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; - dmsg(D_WIN32_IO, "WIN32 I/O: Socket Completion non-queued success [%d]", ret); + dmsg(D_WIN32_IO, "WIN32 I/O: Completion non-queued success [%d]", ret); } break; case IOSTATE_INITIAL: /* were we called without proper queueing? */ - WSASetLastError(WSAEINVAL); + SocketHandleSetInvalError(sh); ret = -1; - dmsg(D_WIN32_IO, "WIN32 I/O: Socket Completion BAD STATE"); + dmsg(D_WIN32_IO, "WIN32 I/O: Completion BAD STATE"); break; default: @@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, } /* return from address if requested */ - if (from) + if (!sh.is_handle && from) { if (ret >= 0 && io->addr_defined) { diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index cc1e0c36..9d439fc6 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -262,12 +262,33 @@ int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to); -int socket_finalize( - SOCKET s, +typedef struct { + union { + SOCKET s; + HANDLE h; + }; + bool is_handle; +} sockethandle_t; + +int sockethandle_finalize( + sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from); +#define SocketHandleGetOverlappedResult(sh, io) (sh.is_handle ? \ + GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE) : \ + WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags)) + +#define SocketHandleGetLastError(sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError(sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError(sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) + #else /* ifdef _WIN32 */ #define openvpn_close_socket(s) close(s) @@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .is_handle = false, .s = sock->sd }; + return sockethandle_finalize(sh, &sock->reads, buf, from); } #else /* ifdef _WIN32 */ @@ -1078,9 +1100,10 @@ link_socket_write_win32(struct link_socket *sock, { int err = 0; int status = 0; + sockethandle_t sh = { .is_handle = false, .s = sock->sd }; if (overlapped_io_active(&sock->writes)) { - status = socket_finalize(sock->sd, &sock->writes, NULL, NULL); + status = sockethandle_finalize(sh, &sock->writes, NULL, NULL); if (status < 0) { err = WSAGetLastError(); diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 12bdd200..fe32127b 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -3561,87 +3561,29 @@ tun_write_queue(struct tuntap *tt, struct buffer *buf) } int -tun_finalize( - HANDLE h, - struct overlapped_io *io, - struct buffer *buf) +tun_write_win32(struct tuntap *tt, struct buffer *buf) { - int ret = -1; - BOOL status; - - switch (io->iostate) + int err = 0; + int status = 0; + if (overlapped_io_active(&tt->writes)) { - case IOSTATE_QUEUED: - status = GetOverlappedResult( - h, - &io->overlapped, - &io->size, - FALSE - ); - if (status) - { - /* successful return for a queued operation */ - if (buf) - { - *buf = io->buf; - } - ret = io->size; - io->iostate = IOSTATE_INITIAL; - ASSERT(ResetEvent(io->overlapped.hEvent)); - dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion success [%d]", ret); - } - else - { - /* error during a queued operation */ - ret = -1; - if (GetLastError() != ERROR_IO_INCOMPLETE) - { - /* if no error (i.e. just not finished yet), - * then DON'T execute this code */ - io->iostate = IOSTATE_INITIAL; - ASSERT(ResetEvent(io->overlapped.hEvent)); - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion error"); - } - } - break; - - case IOSTATE_IMMEDIATE_RETURN: - io->iostate = IOSTATE_INITIAL; - ASSERT(ResetEvent(io->overlapped.hEvent)); - if (io->status) - { - /* error return for a non-queued operation */ - SetLastError(io->status); - ret = -1; - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion non-queued error"); - } - else - { - /* successful return for a non-queued operation */ - if (buf) - { - *buf = io->buf; - } - ret = io->size; - dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion non-queued success [%d]", ret); - } - break; - - case IOSTATE_INITIAL: /* were we called without proper queueing? */ - SetLastError(ERROR_INVALID_FUNCTION); - ret = -1; - dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion BAD STATE"); - break; - - default: - ASSERT(0); + sockethandle_t sh = { .is_handle = true, .h = tt->hand }; + status = sockethandle_finalize(sh, &tt->writes, NULL, NULL); + if (status < 0) + { + err = GetLastError(); + } } - - if (buf) + tun_write_queue(tt, buf); + if (status < 0) { - buf->len = ret; + SetLastError(err); + return status; + } + else + { + return BLEN(buf); } - return ret; } static const struct device_instance_id_interface * diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index d4657537..a6661be0 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize); int tun_write_queue(struct tuntap *tt, struct buffer *buf); -int tun_finalize(HANDLE h, struct overlapped_io *io, struct buffer *buf); - static inline bool tuntap_stop(int status) { @@ -466,36 +464,8 @@ tuntap_abort(int status) return false; } -static inline int -tun_write_win32(struct tuntap *tt, struct buffer *buf) -{ - int err = 0; - int status = 0; - if (overlapped_io_active(&tt->writes)) - { - status = tun_finalize(tt->hand, &tt->writes, NULL); - if (status < 0) - { - err = GetLastError(); - } - } - tun_write_queue(tt, buf); - if (status < 0) - { - SetLastError(err); - return status; - } - else - { - return BLEN(buf); - } -} - -static inline int -read_tun_buffered(struct tuntap *tt, struct buffer *buf) -{ - return tun_finalize(tt->hand, &tt->reads, buf); -} +int +tun_write_win32(struct tuntap *tt, struct buffer *buf); static inline ULONG wintun_ring_packet_align(ULONG size)