From: "flichtenheld (Code Review)" Date: Fri, 14 Nov 2025 12:37:29 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="+7AnNEhtoDo="; 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: plaisthos Cc: openvpn-devel --+7AnNEhtoDo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=3Demail to review the following change. 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 --- M src/openvpn/tun.c 1 file changed, 27 insertions(+), 228 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1378/1 diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 06b7ae5..22f0c17 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 @@ -1733,11 +1733,17 @@ { u_int32_t type; struct iovec iv[2]; - struct openvpn_iphdr *iph; +#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) + struct ip *iph =3D (struct ip *)buf; - iph =3D (struct openvpn_iphdr *)buf; + bool is_ipv6 =3D iph->ip_v =3D=3D 6; +#else + struct openvpn_iphdr *iph =3D (struct openvpn_iphdr *)buf; - if (OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6) + bool is_ipv6 =3D OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6; +#endif + + if (is_ipv6) { type =3D htonl(AF_INET6); } @@ -1784,7 +1790,22 @@ #pragma GCC diagnostic pop #endif -#endif /* if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */ +#if !defined(TARGET_DARWIN) +int +write_tun(struct tuntap *tt, uint8_t *buf, int len) +{ + write_tun_header(tt, buf, len); +} + +int +read_tun(struct tuntap *tt, uint8_t *buf, int len) +{ + 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) @@ -2688,18 +2709,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) /* @@ -2801,88 +2810,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) @@ -2955,84 +2884,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) @@ -3059,60 +2912,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: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Gerrit-Change-Number: 1378 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --+7AnNEhtoDo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

tun: Refactor BSD write_tun/read_tun
There was a lot of duplicated code, merge it together.

Change-Id: I= fd9384287648d1f37a625d9ed6a09733208fa56c
Signed-off-by: Frank Lichtenhel= d <frank@...2641...>
---
M src/openvpn/tun.c
1 file changed,= 27 insertions(+), 228 deletions(-)

git pull ssh=
://gerrit.openvpn.net:29418/openvpn refs/changes/78/1378/1
diff --=
git a/src/openvpn/tun.c b/src/openvpn/tun.c
index 06b7ae5..= 22f0c17 100644
--- a/src/openvpn/tun.c
+++ = b/src/openvpn/tun.c
@@ -1685,10 +1685,10 @@
#endif
}

-#if defined(TARGET_OPENBSD) || defined(TARGET_DARWI= N)
+#if defined(TARGE= T_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || define= d(TARGET_OPENBSD) || defined(TARGET_DARWIN)

/*
- * OpenBSD and = Mac OS X when using utun
+ * BSDs and Mac OS X when using utun
* have a slight= ly incompatible TUN device from
* the rest of the world, = in that it prepends a
* uint32 to the beginning of the IP= header
@@ -1733,11 +1733,17 @@
{
u_int32_t type;
struct iovec = iv[2];
- struct = openvpn_iphdr *iph;
+= #if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
+ struct ip *iph =3D (struct ip = *)buf;

+ bool is_ipv6 =3D iph->ip_v =3D=3D 6;=
+#else
+ struct openvpn_iphdr *iph= =3D (struct openvpn_iphdr *)buf;


- if (OPENVPN_IPH_GET_VER(iph->ver= sion_len) =3D=3D 6)
+= bool is_ipv6 =3D OPENVPN_IPH_GET_VER(iph->version_len) =3D=3D 6;=
+#endif
+
+ if (is_ipv6)
{

type =3D htonl(AF_INET6);
= }
@@ -1784,7 +1790,22 @@
#pragma GCC dia= gnostic pop
#endif

-#endif /* if defined (TARGET_OPENBSD) || de= fined(TARGET_DARWIN) */
+#if !defined(TARGET_DARWIN)
+int
+writ= e_tun(struct tuntap *tt, uint8_t *buf, int len)
+{
+ write_tun_header(tt, buf, len);
+}
+
+int<= br>+read_tun(struct tuntap *tt,= uint8_t *buf, int len)
+{
+ read_tun_h= eader(tt, buf, len);
= +}
+#endif
= +
+
+#endif /* defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || de= fined(TARGET_NETBSD) || if defined (TARGET_OPENBSD) || defined(TARGET_DARWI= N) */

bool
tun_name_is_= fixed(const char *dev)
@@ -2688,18 +2709,6 @@
argv_free(&argv);
}
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int l= en)
-{
- return write_tun_header(tt, buf, l= en);
-}
-
-int
-r= ead_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return read_tun_header(tt, buf, len);
-}
-
#elif defined(TARGET_NETBSD)

/*
@@ -2801,88 +2810,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 *ip= h;
-
- iph =3D (struct openvpn_iphdr *)= buf;
-
- if (OPENVPN_IPH_GET_VER(iph-&g= t;version_len) =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 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)<= /span>
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base =3D (char *)&type;

- iv[0].iov_len =3D s= izeof(type);
- i= v[1].iov_base =3D buf;
= - iv[1].iov_len =3D len;
-
- ret= urn netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- = return read(tt->fd, buf, len);
- }
-}=
-
#el= if 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_no= de, struct tuntap *tt,
openvpn_net_ctx_t *ctx)
@@ -2955,84 +2884,8 @@
argv_free(&ar= gv);
}

-#if defined(__GNUC__) || defined(__clang__)
<= span style=3D"color: hsl(0, 100%, 40%);">-#pragma GCC diagnostic push

-#pragma GCC diagnostic igno= red "-Wconversion"
-#endif
-<= br>-int
-write_tun(struct tuntap *tt, uint8_t *buf, int = len)
-{
- if (tt->type =3D=3D DEV_TYPE_T= UN)
- {
- 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;
<= span style=3D"color: hsl(0, 100%, 40%);">- iv[1].iov_len =3D len;
-
- return freebsd_modify_read_write_re= turn(writev(tt->fd, iv, 2));
- }
- el= se
- {
- return write(tt->fd, buf= , len);
- }-}
-
-int
-read_tun(struc= t tuntap *tt, uint8_t *buf, int len)
-{
- i= f (tt->type =3D=3D DEV_TYPE_TUN)
- {
- = u_int32_t type;
-= struct iovec iv[2];
-
- iv[0].i= ov_base =3D (char *)&type;
- iv[0].iov_len =3D sizeof(type);
- iv[1].iov_base =3D buf;
- iv[1].iov_len =3D len;<= br>-
- return freebsd_modify_read_write_return(re= adv(tt->fd, iv, 2));
- }
- else

- {
- return read(tt->fd, buf, len);
- }
-}
-
-#if d= efined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#elif defined(TARGET_DRAGONFLY)
<= /span>
-static inline int
-dragonfly_modify_read_writ= e_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) = ? len - sizeof(u_int32_t) : 0;
- }
- els= e
- {
- return len;

- }
-}
= -
void
open_tun(const char *dev, const ch= ar *dev_type, const char *dev_node, struct tuntap *tt,
= openvpn_net_ctx_t *ctx)
@@ -3059,60 +2912,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_b= ase =3D (char *)&type;
- iv[0].iov_len =3D sizeof(type);
- iv[1].iov_base =3D buf;
- iv[1].iov_len =3D len;
<= span style=3D"color: hsl(0, 100%, 40%);">-

- return dragonfly_modify_read_write_return(writ= ev(tt->fd, iv, 2));
= - }
- else=
- {
- return write(tt->fd, buf, len);<= /span>
- }
-}
-
-int<= /span>
-read_tun(struct tuntap= *tt, uint8_t *buf, int len)
-{
- if (tt-&g= t;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
<= span style=3D"color: hsl(0, 100%, 40%);">- {

- return read(tt->fd, buf, len);<= br>- }
-}
-
#elif defined(TARGET_DARWIN)
=
/* Darwin (MacOS X) is mostly "just use the generic= stuff", but there

To view, visit = change 1= 378. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a= 09733208fa56c
Gerrit-Change-Number: 1378
Gerrit-PatchSet: 1
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>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --+7AnNEhtoDo=--