From patchwork Mon Aug 29 01:51:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 2734 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director14.mail.ord1d.rsapps.net ([172.27.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id aNzCDJaoDGMZfAAAIUCqbw (envelope-from ) for ; Mon, 29 Aug 2022 07:52:54 -0400 Received: from proxy4.mail.iad3a.rsapps.net ([172.27.255.1]) by director14.mail.ord1d.rsapps.net with LMTP id UBl7DJaoDGNZJwAAeJ7fFg (envelope-from ) for ; Mon, 29 Aug 2022 07:52:54 -0400 Received: from smtp31.gate.iad3a ([172.27.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy4.mail.iad3a.rsapps.net with LMTPS id gDQ9BJaoDGPHWQAA8Zvu4w (envelope-from ) for ; Mon, 29 Aug 2022 07:52:54 -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: smtp31.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; 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: 1cdf908a-2791-11ed-8c0c-5254003d9392-1-1 Received: from [216.105.38.7] ([216.105.38.7:54682] helo=lists.sourceforge.net) by smtp31.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 5F/2E-28631-598AC036; Mon, 29 Aug 2022 07:52:53 -0400 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.95) (envelope-from ) id 1oSdIu-0001Ey-2r; Mon, 29 Aug 2022 11:51:44 +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.95) (envelope-from ) id 1oSdIt-0001Es-7W for openvpn-devel@lists.sourceforge.net; Mon, 29 Aug 2022 11:51:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=eKcnuZPZssVutLDKDXxrScWK6s375T1gHvxnFPn1nT4=; b=ItuZWSrxJLb0XdpQ4o+yzuYBJu 3fBsAypBgIDSLEnQnWULWpRGOHObPnLxPTD85l1Nyv3EWLijr5NJEDuN4hF3cq5V9ihhKKC+238od ++mU0w2BORfuC+fjg6xLdNGhqy7Xz6nw1u0NwuO+v7lygynAjI9bkCFQhLwFjvAiaecQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=eKcnuZPZssVutLDKDXxrScWK6s375T1gHvxnFPn1nT4=; b=E JYWWQxK9S5lomj2bvodOnZ0xB7VJ43POmUrXwmwnIx1yiAq/Mc/RIYZJgNc1bwcqF57r1/54KP3+T T4BsoCZUcbqkJ/82rqw3EU5VAjRrQ4iEUe4y6OVlVPPYJDRHMJbIdIMYNR0zknvs1wsJXGNAFL5ob B1dXOcgvEtU08x5o=; Received: from mail-ej1-f43.google.com ([209.85.218.43]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1oSdIs-006IkJ-EA for openvpn-devel@lists.sourceforge.net; Mon, 29 Aug 2022 11:51:43 +0000 Received: by mail-ej1-f43.google.com with SMTP id og21so15169087ejc.2 for ; Mon, 29 Aug 2022 04:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=eKcnuZPZssVutLDKDXxrScWK6s375T1gHvxnFPn1nT4=; b=Nng8fYKF09CUbsTIndjlMjuNUzVmLqe3aO9bTfS3Xf4kxfZkIw09mwgjCc8GJDr7y1 dVv4Zt26E8XNGyomv5MT9VuKlzcInHBmIyqMJ/1kirC7j9l+SJ3QmkQAZTtK/GLHsYq+ 1M7R9uMgRILtRU6Stm5U5jCIQ6RzYXsB4CDfgGw/+/vtNPrXGAknPIgi2UkHEtDwcIyq QIxT5+FmWFzG4XtRRqrtRlb958YUObRh8cQPRhSd08i20SJ4SFqZVp8iZ9tnAHvSKZIO Fr73GKMVkwZRcEwMAX+SN5FPQwG0i+UwelBXA1QvBZN5rBagIgsrkAFfjxTQhCo6MI9T BolA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=eKcnuZPZssVutLDKDXxrScWK6s375T1gHvxnFPn1nT4=; b=soWj/XyQyHYw37NbzinJGWroeW7DVhmwZLezXW89iGmx2Fe2WjtIWgLQh7qrO3MosH JAHWIZ2L19fPxN9Q4yOX3NMwynw1onnH3F3vhYPKyHSWIq+FdJr+fjwGVIU1AuF9aQA/ 06eJVcUG7ZpPjDFeHu/sPdg5+fRCPHvTJo/CUrOoPJvt8DgJIKsTVvusViE7rWGFjZFT Ozft8BTfK53SuC2NB8DDmQ9Ip9a5kmfuBUWDBMt0K6AVe3iLVNzKQht/HLmd9f2r6ZqN /q0z+Fvy6wXaWj0QPtXO4KYOeQ/qPF68JONQD+vJNPAJNbPa2uWsZ+3xiIGyi2FIJ+Me PyhQ== X-Gm-Message-State: ACgBeo1RXDfqfxW0Stry3IYZk3wQHBBDukfht3Caopt5+ekiis5Q0tBU kfn2InVOhemP8oSStTDC+/HQlx4VRXcFXg== X-Google-Smtp-Source: AA6agR7zBbOeQsHNAVq9ZJwI3Zg/eiOPibxM+Rg/j4QfgS6hR4DTGMgWuhEkvxCtuiBU5RlVyNgrYQ== X-Received: by 2002:a17:907:d91:b0:741:771f:1d11 with SMTP id go17-20020a1709070d9100b00741771f1d11mr4273464ejc.588.1661773895565; Mon, 29 Aug 2022 04:51:35 -0700 (PDT) Received: from LAPTOP-4L3N7KFS.localdomain (stipakov.fi. [128.199.52.117]) by smtp.gmail.com with ESMTPSA id a6-20020a1709063a4600b0073c5192cba6sm4371256ejf.114.2022.08.29.04.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 04:51:35 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Mon, 29 Aug 2022 14:51:17 +0300 Message-Id: <20220829115117.291-1-lstipakov@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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 Since version 0.8.0, dco-win driver added support for DEL_PEER command, which enabled --persist-tun implementation on client side. Add real implementation for dco_del_peer on Windows, which calls DEL_PEER, which clears peer state on the driver without tearing tunnel down. Content analysis details: (-0.2 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.218.43 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [lstipakov[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.218.43 listed in wl.mailspike.net] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -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_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1oSdIs-006IkJ-EA Subject: [Openvpn-devel] [PATCH] dco-win: support for --persist-tun 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 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Lev Stipakov Since version 0.8.0, dco-win driver added support for DEL_PEER command, which enabled --persist-tun implementation on client side. Add real implementation for dco_del_peer on Windows, which calls DEL_PEER, which clears peer state on the driver without tearing tunnel down. When pulled options are changed on restart, we need to close and reopen tun device. This is not yes supported for dco-win, so we close tun and trigger reconnect. Signed-off-by: Lev Stipakov --- src/openvpn/dco.c | 5 ----- src/openvpn/dco_win.c | 29 ++++++++++++++---------- src/openvpn/dco_win.h | 8 ++++--- src/openvpn/init.c | 19 +++++++++++++--- src/openvpn/ovpn_dco_win.h | 1 + src/openvpn/socket.c | 45 ++++++++++++++++++++------------------ 6 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/openvpn/dco.c b/src/openvpn/dco.c index 78023eea..075820c3 100644 --- a/src/openvpn/dco.c +++ b/src/openvpn/dco.c @@ -233,11 +233,6 @@ dco_check_startup_option_conflict(int msglevel, const struct options *o) return false; } - if (o->persist_tun) - { - msg(msglevel, "--persist-tun is not supported with ovpn-dco."); - return false; - } #elif defined(TARGET_LINUX) /* if the device name is fixed, we need to check if an interface with this * name already exists. IF it does, it must be a DCO interface, otherwise diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index a2030866..a7d6b55d 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -42,7 +42,7 @@ const IN_ADDR in4addr_any = { 0 }; #endif -static struct tuntap +struct tuntap create_dco_handle(const char *devname, struct gc_arena *gc) { struct tuntap tt = { .windows_driver = WINDOWS_DRIVER_DCO }; @@ -157,10 +157,9 @@ dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, volatile int *signa return -1; } -struct tuntap -dco_create_socket(struct addrinfo *remoteaddr, bool bind_local, - struct addrinfo *bind, const char *devname, - struct gc_arena *gc, int timeout, +bool +dco_create_socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local, + struct addrinfo *bind, int timeout, volatile int *signal_received) { msg(D_DCO_DEBUG, "%s", __func__); @@ -232,10 +231,8 @@ dco_create_socket(struct addrinfo *remoteaddr, bool bind_local, ASSERT(0); } - struct tuntap tt = create_dco_handle(devname, gc); - OVERLAPPED ov = { 0 }; - if (!DeviceIoControl(tt.hand, OVPN_IOCTL_NEW_PEER, &peer, sizeof(peer), NULL, 0, NULL, &ov)) + if (!DeviceIoControl(handle, OVPN_IOCTL_NEW_PEER, &peer, sizeof(peer), NULL, 0, NULL, &ov)) { DWORD err = GetLastError(); if (err != ERROR_IO_PENDING) @@ -244,13 +241,13 @@ dco_create_socket(struct addrinfo *remoteaddr, bool bind_local, } else { - if (dco_connect_wait(tt.hand, &ov, timeout, signal_received) < 0) + if (dco_connect_wait(handle, &ov, timeout, signal_received) < 0) { - close_tun_handle(&tt); + return false; } } } - return tt; + return true; } int @@ -265,7 +262,15 @@ dco_new_peer(dco_context_t *dco, unsigned int peerid, int sd, int dco_del_peer(dco_context_t *dco, unsigned int peerid) { - msg(D_DCO_DEBUG, "%s: peer-id %d - not implemented", __func__, peerid); + msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peerid); + + DWORD bytes_returned = 0; + if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_DEL_PEER, NULL, + 0, NULL, 0, &bytes_returned, NULL)) + { + msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_DEL_PEER) failed"); + return -1; + } return 0; } diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h index 348fc568..3a04e4f3 100644 --- a/src/openvpn/dco_win.h +++ b/src/openvpn/dco_win.h @@ -37,9 +37,11 @@ struct dco_context { typedef struct dco_context dco_context_t; struct tuntap -dco_create_socket(struct addrinfo *remoteaddr, bool bind_local, - struct addrinfo *bind, const char *devname, - struct gc_arena *gc, int timeout, +create_dco_handle(const char *devname, struct gc_arena *gc); + +bool +dco_create_socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local, + struct addrinfo *bind, int timeout, volatile int *signal_received); void diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 9917cefe..84d95c21 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2183,10 +2183,23 @@ do_up(struct context *c, bool pulled_options, unsigned int option_types_found) { /* if so, close tun, delete routes, then reinitialize tun and add routes */ msg(M_INFO, "NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device."); + + bool tt_dco_win = tuntap_is_dco_win(c->c1.tuntap); do_close_tun(c, true); - management_sleep(1); - c->c2.did_open_tun = do_open_tun(c); - update_time(); + + if (tt_dco_win) + { + msg(M_NONFATAL, "dco-win doesn't yet support reopening TUN device"); + /* prevent link_socket_close() from closing handle with WinSock API */ + c->c2.link_socket->sd = SOCKET_UNDEFINED; + return false; + } + else + { + management_sleep(1); + c->c2.did_open_tun = do_open_tun(c); + update_time(); + } } } diff --git a/src/openvpn/ovpn_dco_win.h b/src/openvpn/ovpn_dco_win.h index 1ebd51a7..cbbdf92e 100644 --- a/src/openvpn/ovpn_dco_win.h +++ b/src/openvpn/ovpn_dco_win.h @@ -106,3 +106,4 @@ typedef struct _OVPN_SET_PEER { #define OVPN_IOCTL_SWAP_KEYS CTL_CODE(FILE_DEVICE_UNKNOWN, 4, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_SET_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 5, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_START_VPN CTL_CODE(FILE_DEVICE_UNKNOWN, 6, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define OVPN_IOCTL_DEL_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 7, METHOD_BUFFERED, FILE_ANY_ACCESS) diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 4e29327b..f2bb3d69 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -2128,23 +2128,29 @@ static void create_socket_dco_win(struct context *c, struct link_socket *sock, volatile int *signal_received) { - struct tuntap *tt; - /* In this case persist-tun is enabled, which we don't support yet */ - ASSERT(!c->c1.tuntap); - - ALLOC_OBJ(tt, struct tuntap); - - *tt = dco_create_socket(sock->info.lsa->current_remote, - sock->bind_local, - sock->info.lsa->bind_local, - c->options.dev_node, - &c->gc, - get_server_poll_remaining_time(sock->server_poll_timeout), - signal_received); - - /* This state is used by signal handler which does teardown, - * so it has to be set before return */ - c->c1.tuntap = tt; + if (!c->c1.tuntap) + { + struct tuntap *tt; + ALLOC_OBJ(tt, struct tuntap); + + *tt = create_dco_handle(c->options.dev_node, &c->gc); + + /* Ensure we can "safely" cast the handle to a socket */ + static_assert(sizeof(sock->sd) == sizeof(tt->hand), "HANDLE and SOCKET size differs"); + + c->c1.tuntap = tt; + } + + bool res = dco_create_socket(c->c1.tuntap->hand, + sock->info.lsa->current_remote, + sock->bind_local, sock->info.lsa->bind_local, + get_server_poll_remaining_time(sock->server_poll_timeout), + signal_received); + if (!res) + { + close_tun_handle(c->c1.tuntap); + } + sock->info.dco_installed = true; if (*signal_received) @@ -2152,10 +2158,7 @@ create_socket_dco_win(struct context *c, struct link_socket *sock, return; } - /* Ensure we can "safely" cast the handle to a socket */ - static_assert(sizeof(sock->sd) == sizeof(tt->hand), "HANDLE and SOCKET size differs"); - sock->sd = (SOCKET)tt->hand; - + sock->sd = (SOCKET)c->c1.tuntap->hand; linksock_print_addr(sock); } #endif /* if defined(_WIN32) */