From patchwork Tue Dec 9 13:30:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4668 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:29c3:b0:7b1:439f:bdf with SMTP id g3csp1327453max; Tue, 9 Dec 2025 05:31:00 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVjj0FAosmlh1FmOYN4/+ewm/mmD45PyQ3/LDs0rtfdvzv1e69lYtZF9Hs+GhpX8qKg/HIFeR5bFuA=@openvpn.net X-Google-Smtp-Source: AGHT+IGZw1xFVxGuzXQbNjY5S0/IEAL5yk1yvzh7bHD+xHxfwHDaKlL9t2g3UEi0yYdLcC17cssO X-Received: by 2002:a05:6808:4fdf:b0:44f:76aa:77d5 with SMTP id 5614622812f47-4539dfdc044mr4825189b6e.16.1765287059928; Tue, 09 Dec 2025 05:30:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1765287059; cv=none; d=google.com; s=arc-20240605; b=W8UL+RyJTF8cAaOFXa80st1OWFlkW+09xD9lq4jbnYviGGBlTjvcY9SeEsEbJSPhIR xW0CmLFW8G+fRrLKkzhqWY+jV7rbpH4rrwoDuwlEKAvlqknBo3J7obJpA5toesQtv5k3 3zEurp5zFhYZgyhkMBZa9GeHWeHWNimV5QPqaabplv1anIEtvRHDJfSH/HZWBw2sHmEK KosLZgmooe4wOkHSTf59qJ2BowhMPDx2Hy6+IMNn8HQYoRLa1p1KlUYXkWT0peXhOqOw dTwqGcgBiifcvPmWbX0SyPb+qi9K0IzN3dgmJvZ96Je8sfThSmszwpHcPCCQD33GNcEn 1xzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=htMrpXwjpzbwQ5cfPqzihxSE2egvP2P9MhuU5Hp2DF0=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=BcEBKVSrXVbuwKpLc/GAB54w+9uHlRDHHlD50yG4d5DxHB4imbLAchseQ7IV5eXE39 7BBipYsX8/OrwVbUNdLM00y4AL/zcyn9RS5Y90OBUf3p9kK+boyTWD8gzedzGlowInl5 ng8z4Ndbq77LEcVsPFF0T53NZ3cc+X6MWy+lzjIH3zpsffDhIy3rJwAy7sDHFol1rVRt CTQqH2VkorQLJTUcMdkM5fRCrOdHe0QZe020ikBAtkMKl8y7F3DZJ33u1H2AM9fqsTR/ loWK5Bpvg7cB5uc/Bja6yP4mlbpMp5AS7l9RqwgRIreNl5qXfPVdMmmenwPwybp+QwWa jRiw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=Nrf4Zhic; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=K6wb6QPh; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dvXrzen3; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 5614622812f47-453801dcfe1si10078988b6e.55.2025.12.09.05.30.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Dec 2025 05:30:59 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=Nrf4Zhic; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=K6wb6QPh; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dvXrzen3; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=htMrpXwjpzbwQ5cfPqzihxSE2egvP2P9MhuU5Hp2DF0=; b=Nrf4Zhicfx302h5HukqnXTH+ff Z+0798mK4OK9bbqIMfRk64VQW/y8uBziL8fhNXPOzfdW4bmr53ZO/pUr5H3GggeOYeXuAL6lFTbr/ dj7lzxH1b+x5cTSYszcobDU5agk4l14cYV7xcrhY6LRnoIZEQbsVhAoE2NfW03TIvZj0=; 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 1vSxnp-0001rS-Hi; Tue, 09 Dec 2025 13:30:54 +0000 Received: from [172.30.29.66] (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 1vSxnn-0001rD-Ov for openvpn-devel@lists.sourceforge.net; Tue, 09 Dec 2025 13:30:52 +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:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: 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=pHe2gurlsrFNxf3oR/Euc/QEZRK7Lpm032xJnfBrj1Q=; b=K6wb6QPhv89xE2tyE4tkODal+R l4D3DJ4kPBaU+9Dym+OrFGHNaSXJteDDFPI50o9/DP4bCrp/qdT1W9vJJS94vrzfoTHk3z8PmVyh8 hgx4cno4jEDCpcZPCHvYBLK17dZB+0DpqpUIxj+9rB1NDMibxEW5OxZcbU/4D5DSvGrA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: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=pHe2gurlsrFNxf3oR/Euc/QEZRK7Lpm032xJnfBrj1Q=; b=dvXrzen3J5ivl9xnl/j0DAK7XW zK7ZhHDz9mc34z3YUu6tCsZxIe0AVp4kavHRh17LzRrFKt4VVTrS0L7vmEXM+QmBHW6aAZalTuG3x Pk3bnalPwdw9UCG7oAYawt6mI3RhbHS047orSFatPFr8GQQ9xzrI/0Dc/b1uClNJyQGE=; Received: from [193.149.48.134] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1vSxnn-0001MD-3H for openvpn-devel@lists.sourceforge.net; Tue, 09 Dec 2025 13:30:52 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 5B9DUdTf005121 for ; Tue, 9 Dec 2025 14:30:39 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 5B9DUdTr005120 for openvpn-devel@lists.sourceforge.net; Tue, 9 Dec 2025 14:30:39 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Tue, 9 Dec 2025 14:30:33 +0100 Message-ID: <20251209133038.5088-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.51.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", 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: Frank Lichtenheld There was a lot of duplicated code, merge it together. Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/open [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1vSxnn-0001MD-3H Subject: [Openvpn-devel] [PATCH v7] tun: Refactor BSD write_tun/read_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: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1851037643731783726?= X-GMAIL-MSGID: =?utf-8?q?1851037643731783726?= From: Frank Lichtenheld There was a lot of duplicated code, merge it together. Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1378 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1378 This mail reflects revision 7 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 7c61dcf..b4e3cb4 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -1685,10 +1685,10 @@ #endif } -#if defined(TARGET_OPENBSD) || defined(TARGET_DARWIN) +#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) || defined(TARGET_DARWIN) /* - * OpenBSD and Mac OS X when using utun + * BSDs and Mac OS X when using utun * have a slightly incompatible TUN device from * the rest of the world, in that it prepends a * uint32 to the beginning of the IP header @@ -1699,10 +1699,8 @@ * We strip off this field on reads and * put it back on writes. * - * I have not tested TAP devices on OpenBSD, - * but I have conditionalized the special - * TUN handling code described above to - * go away for TAP devices. + * For TAP devices, this is not needed and must + * not be done. */ #include @@ -1733,11 +1731,9 @@ { u_int32_t type; struct iovec iv[2]; - struct openvpn_iphdr *iph; + struct ip *iph = (struct ip *)buf; - iph = (struct openvpn_iphdr *)buf; - - if (OPENVPN_IPH_GET_VER(iph->version_len) == 6) + if (iph->ip_v == 6) { type = htonl(AF_INET6); } @@ -1784,7 +1780,26 @@ #pragma GCC diagnostic pop #endif -#endif /* if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */ +/* For MacOS this extra handling is conditional on the UTUN driver. + * So it needs its own read_tun()/write_tun() with the necessary + * checks. They are located in the macOS-specific section below. + */ +#if !defined(TARGET_DARWIN) +int +write_tun(struct tuntap *tt, uint8_t *buf, int len) +{ + return write_tun_header(tt, buf, len); +} + +int +read_tun(struct tuntap *tt, uint8_t *buf, int len) +{ + return read_tun_header(tt, buf, len); +} +#endif + + +#endif /* defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */ bool tun_name_is_fixed(const char *dev) @@ -2679,18 +2694,6 @@ argv_free(&argv); } -int -write_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - return write_tun_header(tt, buf, len); -} - -int -read_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - return read_tun_header(tt, buf, len); -} - #elif defined(TARGET_NETBSD) /* @@ -2792,88 +2795,8 @@ argv_free(&argv); } -static inline int -netbsd_modify_read_write_return(int len) -{ - if (len > 0) - { - return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0; - } - else - { - return len; - } -} - -int -write_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct openvpn_iphdr *iph; - - iph = (struct openvpn_iphdr *)buf; - - if (OPENVPN_IPH_GET_VER(iph->version_len) == 6) - { - type = htonl(AF_INET6); - } - else - { - type = htonl(AF_INET); - } - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return netbsd_modify_read_write_return(writev(tt->fd, iv, 2)); - } - else - { - return write(tt->fd, buf, len); - } -} - -int -read_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return netbsd_modify_read_write_return(readv(tt->fd, iv, 2)); - } - else - { - return read(tt->fd, buf, len); - } -} - #elif defined(TARGET_FREEBSD) -static inline int -freebsd_modify_read_write_return(int len) -{ - if (len > 0) - { - return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0; - } - else - { - return len; - } -} - void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx) @@ -2946,84 +2869,8 @@ argv_free(&argv); } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" -#endif - -int -write_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct ip *iph; - - iph = (struct ip *)buf; - - if (iph->ip_v == 6) - { - type = htonl(AF_INET6); - } - else - { - type = htonl(AF_INET); - } - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return freebsd_modify_read_write_return(writev(tt->fd, iv, 2)); - } - else - { - return write(tt->fd, buf, len); - } -} - -int -read_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return freebsd_modify_read_write_return(readv(tt->fd, iv, 2)); - } - else - { - return read(tt->fd, buf, len); - } -} - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - #elif defined(TARGET_DRAGONFLY) -static inline int -dragonfly_modify_read_write_return(int len) -{ - if (len > 0) - { - return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0; - } - else - { - return len; - } -} - void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx) @@ -3050,60 +2897,6 @@ free(tt); } -int -write_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct ip *iph; - - iph = (struct ip *)buf; - - if (iph->ip_v == 6) - { - type = htonl(AF_INET6); - } - else - { - type = htonl(AF_INET); - } - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2)); - } - else - { - return write(tt->fd, buf, len); - } -} - -int -read_tun(struct tuntap *tt, uint8_t *buf, int len) -{ - if (tt->type == DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base = (char *)&type; - iv[0].iov_len = sizeof(type); - iv[1].iov_base = buf; - iv[1].iov_len = len; - - return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2)); - } - else - { - return read(tt->fd, buf, len); - } -} - #elif defined(TARGET_DARWIN) /* Darwin (MacOS X) is mostly "just use the generic stuff", but there