From: "cron2 (Code Review)" Date: Tue, 9 Dec 2025 15:25:38 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="/bS6mq56AtI="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: tun: Refactor BSD write_tun/read_tun List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --/bS6mq56AtI= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#8) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1378?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: tun: Refactor BSD write_tun/read_tun ...................................................................... tun: Refactor BSD write_tun/read_tun 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 Message-Id: <20251209133038.5088-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34= 946.html Signed-off-by: Gert Doering --- M src/openvpn/tun.c 1 file changed, 26 insertions(+), 233 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1378/8 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 =3D (struct ip *)buf; - iph =3D (struct openvpn_iphdr *)buf; - - if (OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6) + if (iph->ip_v =3D=3D 6) { type =3D 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct openvpn_iphdr *iph; - - iph =3D (struct openvpn_iphdr *)buf; - - if (OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6) - { - type =3D htonl(AF_INET6); - } - else - { - type =3D htonl(AF_INET); - } - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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, stru= ct 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct ip *iph; - - iph =3D (struct ip *)buf; - - if (iph->ip_v =3D=3D 6) - { - type =3D htonl(AF_INET6); - } - else - { - type =3D htonl(AF_INET); - } - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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, stru= ct 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - struct ip *iph; - - iph =3D (struct ip *)buf; - - if (iph->ip_v =3D=3D 6) - { - type =3D htonl(AF_INET6); - } - else - { - type =3D htonl(AF_INET); - } - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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 =3D=3D DEV_TYPE_TUN) - { - u_int32_t type; - struct iovec iv[2]; - - iv[0].iov_base =3D (char *)&type; - iv[0].iov_len =3D sizeof(type); - iv[1].iov_base =3D buf; - iv[1].iov_len =3D 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 -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Gerrit-Change-Number: 1378 Gerrit-PatchSet: 8 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --/bS6mq56AtI= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #8 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

tun: Refactor BSD write_tun/read_tu=
n

There was a lot of duplicated code, merge it together.

Chan= ge-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c
Signed-off-by: Frank Li= chtenheld <frank@...2641...>
Acked-by: Gert Doering <gert@...1711...= 96...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1378
Mes= sage-Id: <20251209133038.5088-1-gert@...1296...>
URL: https://www.= mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34946.html
Signe= d-off-by: Gert Doering <gert@...1296...>
---
M src/openvpn/tun.= c
1 file changed, 26 insertions(+), 233 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/137=
8/8
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) || d= efined(TARGET_DARWIN)
+#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGE= T_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 spec= ial
- * TUN handling co= de described above to
-= * go away for TAP devices.
+ * For TAP devices, this is not needed and must
+ * not be done.
*/


#include <netinet/ip.h>
@@ -1733,11 +1731,9 @@
{
= u_int32_t type;
struct iovec iv[2];
- struct openvpn_iphdr *iph;
+ struct ip *ip= h =3D (struct ip *)buf;

- iph =3D (struct openvpn_iphdr *)buf;
= -
- if (OPENVPN_IPH_GET_VER(iph->version_len) = =3D=3D 6)
+ if= (iph->ip_v =3D=3D 6)
{
= type =3D htonl(AF_INET6);
}
= @@ -1784,7 +1780,26 @@
#pragma GCC diagnostic pop #endif

-#endif /* if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN)= */
+/* For MacOS thi= s extra handling is conditional on the UTUN driver.
+ * So it needs its own read_tun()/write_t= un() 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 le= n)
+{
+ 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 defi= ned (TARGET_OPENBSD) || defined(TARGET_DARWIN) */
<= br> bool
tun_name_is_fixed(const char *dev)@@ -2679,18 +2694,6 @@
argv_free(&argv);
}

-int
-writ= e_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return write_tun_header(tt, buf, len);
-}
-
-int
= -read_tun(struct tuntap *tt, uint= 8_t *buf, int len)
-{
- return read_tun_hea= der(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 =3D=3D DEV_TYPE_TUN)
- {
- struct iovec iv[2];
- struct openvpn_iphdr *iph;
-
-
- if (OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6)<= br>- {
- type =3D htonl(AF_INET6);
- }
- else

- {
- type =3D htonl(AF_INET);
- }
-
- = iv[0].iov_base =3D (char *)&type;
- iv[0].iov_len =3D sizeof(type);
- iv[1].iov_base =3D buf;
- iv[1].iov_len = =3D len;
-
- return netbsd_modify_read_= write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->= ;fd, buf, len);
- }<= /span>
-}
-
-int
-read_t= un(struct tuntap *tt, uint8_t *buf, int len)
-{
- {
- u_int32_t type;
- struct iovec iv[2];
-
- = iv[0].iov_base =3D (char *)&type;
- iv[0].iov_len =3D sizeof(type);
- iv[1].iov_base =3D buf;- iv[1].iov_len =3D len;=
-
- return netbsd_modify_read_write_re= turn(readv(tt->fd, iv, 2));
- }
- els= e
- {
- return read(tt->fd, buf, = len);

- }
= -}
-
#elif defined(TARGET_FREEBSD)

-static i= nline int
-freebsd_modi= fy_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 *d= ev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx)
@@ -2946,84 +2869= ,8 @@
argv_free(&argv);
}<= br>
-#if defined= (__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"

-#endif
-
-int
-wri= te_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type =3D=3D DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- = iph =3D (struct ip *)buf;
-
- if= (iph->ip_v =3D=3D 6)
- = type =3D htonl(AF_INET6);
- }
- = else
- {
- type =3D htonl= (AF_INET);
- }
-
- iv[0].iov_base =3D (char *)&typ= e;
- iv[0].iov_l= en =3D sizeof(type);
- = iv[1].iov_base =3D buf;
- iv[1].iov_len =3D 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 =3D=3D DEV_TYPE_TUN)=
- {
- u_int32_t type;

- struct iovec iv[2];
<= br>-
- iv[0].iov_base =3D (char *)&type;
- iv[0].iov_len =3D = sizeof(type);
- = iv[1].iov_base =3D buf;
- iv[1].iov_len =3D len;
-
- re= turn freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
<= span style=3D"color: hsl(0, 100%, 40%);">- }

- else
- {
- = return read(tt->fd, buf, len);
- }
= -}
-
-#if defined(__GNUC__) || defined(__clang= __)
-#pragma GCC diagno= stic pop
-#endif=
-
#elif defi= ned(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
<= span> 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(str= uct tuntap *tt, uint8_t *buf, int len)
-{
- = if (tt->type =3D=3D DEV_TYPE_TUN)
- {
-= u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- iph= =3D (struct ip *)buf;
= -
- if (iph->= ip_v =3D=3D 6)
- = {
- type = =3D htonl(AF_INET6);
- = }
- else<= /span>
- {
- type =3D htonl(AF_INET= );

- }-
- iv[0].iov_base =3D (char *)&type;=
- iv[0].iov_len =3D si= zeof(type);
- iv= [1].iov_base =3D buf;
-= iv[1].iov_len =3D len;
-
- retu= rn 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 =3D=3D DEV_TYPE_TUN)=
- {
- u_int32_t type;
- struct iovec iv[2];
-

- iv[0].iov_base =3D (char *)&type;
= - iv[0].iov_len =3D sizeof= (type);
- iv[1].= iov_base =3D buf;
- = iv[1].iov_len =3D len;
-
- return d= ragonfly_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
<= span>

To view, visit change 1378. To unsubscribe, or for hel= p writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a= 09733208fa56c
Gerrit-Change-Number: 1378
Gerrit-PatchSet: 8
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --/bS6mq56AtI=--