Skip to contentSkip to conversation

Fix check if iface name is set

PATCH Superseded Arne Schwabe 5 participants 16 messages Aug 12, 2019 – Aug 15, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Fix check if iface name is set

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +1 -1
index 5f63fd34..a302746e 100644
@@ -3349,7 +3349,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
-        if (rgi6->iface)
+        if (strlen(rgi6->iface))
         {
             rgi6->flags |= RGI_IFACE_DEFINED;
         }
Signature · 1 line
2.22.0
#9 lore raw

PATCH 2/2 Adjust Android code after sitnl patch merge

It turns out that the only part of Android that still shares routing code with Linux is the get_default_ipv6 method.

Instead of fixing a method that makes little sense on Android anyway, have a method that returns a fake ipv6 gateway like for ipv4.

---
 src/openvpn/route.c | 66 ++++++++++++++++++++++++++++++---------------
 src/openvpn/tun.c   |  9 +++++--
 2 files changed, 51 insertions(+), 24 deletions(-)
src/openvpn/route.c +44 -22
index a302746e..9af88f00 100644
@@ -1065,7 +1065,8 @@ redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt,
                             tt,
                             flags,
                             &rl->rgi,
-                            es);
+                            es,
+                            ctx);
 
 #else
                 if (rl->flags & RG_DEF1)
@@ -3169,7 +3170,48 @@ show_routes(int msglev)
     gc_free(&gc);
 }
 
-#elif defined(TARGET_LINUX) || defined(TARGET_ANDROID)
+#elif defined(TARGET_ANDROID)
+
+void
+get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
+{
+    /* Android, set some pseudo GW, addr is in host byte order,
+     * Determining the default GW on Android 5.0+ is non trivial
+     * and serves almost no purpose since OpenVPN only uses the
+     * default GW address to add routes for networks that should
+     * NOT be routed over the VPN. Using a well known address
+     * (127.'d'.'g'.'w') for the default GW make detecting
+     * these routes easier from the controlling app.
+     */
+    CLEAR(*rgi);
+
+    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
+    rgi->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi->iface, "android-gw");
+
+    /* Skip scanning/fetching interface from loopback interface we do
+     * normally on Linux.
+     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
+     */
+
+}
+
+void
+get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
+                         const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
+{
+    /* Same for ipv6 */
+
+    CLEAR(*rgi6);
+
+    /* Use a fake link-local address */
+    ASSERT(inet_pton(AF_INET6, "fe80::ad", &rgi6->addrs->addr_ipv6) == 1);
+    rgi6->addrs->netbits_ipv6 = 64;
+    rgi6->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi6->iface, "android-gw");
+}
+
+#elif defined(TARGET_LINUX)
 
 void
 get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
@@ -3181,7 +3223,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
     CLEAR(*rgi);
     CLEAR(best_name);
 
-#ifndef TARGET_ANDROID
     /* get default gateway IP addr */
     if (net_route_v4_best_gw(ctx, NULL, &rgi->gateway.addr, best_name) == 0)
     {
@@ -3191,25 +3232,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
             rgi->flags |= RGI_ON_LINK;
         }
     }
-#else  /* ifndef TARGET_ANDROID */
-    /* Android, set some pseudo GW, addr is in host byte order,
-     * Determining the default GW on Android 5.0+ is non trivial
-     * and serves almost no purpose since OpenVPN only uses the
-     * default GW address to add routes for networks that should
-     * NOT be routed over the VPN. Using a well known address
-     * (127.'d'.'g'.'w') for the default GW make detecting
-     * these routes easier from the controlling app.
-     */
-    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
-    rgi->flags |= RGI_ADDR_DEFINED;
-    strcpy(best_name, "android-gw");
-
-    /*
-     * Skip scanning/fetching interface from loopback interface
-     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
-     */
-    goto done;
-#endif /* ifndef TARGET_ANDROID */
 
     /* scan adapter list */
     if (rgi->flags & RGI_ADDR_DEFINED)
src/openvpn/tun.c +7 -2
index 8f8f7c6c..1db459f8 100644
@@ -906,9 +906,13 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
 #elif defined(TARGET_ANDROID)
     char out6[64];
 
+    const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, NULL);
     openvpn_snprintf(out6, sizeof(out6), "%s/%d %d",
-                     ifconfig_ipv6_local,tt->netbits_ipv6, tun_mtu);
+                     ifconfig_ipv6_local, tt->netbits_ipv6, tun_mtu);
+
     management_android_control(management, "IFCONFIG6", out6);
+
+    free(ifconfig_ipv6_local);
 #elif defined(TARGET_SOLARIS)
     argv_printf(&argv, "%s %s inet6 unplumb", IFCONFIG_PATH, ifname);
     argv_msg(M_INFO, &argv);
@@ -1045,7 +1049,8 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
     || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
     || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+    || defined(TARGET_SOLARIS) || defined(_WIN32) \
+    || defined(TARGET_ANDROID)
     const char *ifconfig_local = NULL;
     const char *ifconfig_remote_netmask = NULL;
     const char *ifconfig_broadcast = NULL;
Signature · 1 line
2.22.0
#4 lore raw
In reply to Arne Schwabe

Hi Arne,

Quoted 10 lines · Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

Thanks for fixing this!

However you're missing the signed off line.

Quoted 9 lines
---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +0 -0
index 5f63fd34..a302746e 100644

src/openvpn/route.c get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, · line 3352

Quoted 5 lines
src/openvpn/route.c +1 -1
@@ -3349,7 +3349,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
-        if (rgi6->iface)
+        if (strlen(rgi6->iface))

how about adding a "> 0"? I know it's basically the same here, but I think that's the style we use everywhere.

Cheers,

Quoted 4 lines
src/openvpn/route.c +0 -0
         {
             rgi6->flags |= RGI_IFACE_DEFINED;
         }
Signature · 1 line
Antonio Quartulli
#10 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Adjust Android code after sitnl patch merge

In reply to Arne Schwabe

Hi Arne,

Quoted 5 lines · Arne Schwabe

It turns out that the only part of Android that still shares routing code with Linux is the get_default_ipv6 method.

Instead of fixing a method that makes little sense on Android anyway, have a method that returns a fake ipv6 gateway like for ipv4.

you've missed the Signed-off line here too :)

I can't comment much on the Android code as I have never built openvpn for it, however, when playing with platform-ifdef I always recommend to run your branch through the buildbot.

This way we can easily see if we have made big mistakes in regards to other platforms.

Cheers,

Quoted 133 lines
---
 src/openvpn/route.c | 66 ++++++++++++++++++++++++++++++---------------
 src/openvpn/tun.c   |  9 +++++--
 2 files changed, 51 insertions(+), 24 deletions(-)
src/openvpn/route.c +44 -22
index a302746e..9af88f00 100644
@@ -1065,7 +1065,8 @@ redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt,
                             tt,
                             flags,
                             &rl->rgi,
-                            es);
+                            es,
+                            ctx);
 
 #else
                 if (rl->flags & RG_DEF1)
@@ -3169,7 +3170,48 @@ show_routes(int msglev)
     gc_free(&gc);
 }
 
-#elif defined(TARGET_LINUX) || defined(TARGET_ANDROID)
+#elif defined(TARGET_ANDROID)
+
+void
+get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
+{
+    /* Android, set some pseudo GW, addr is in host byte order,
+     * Determining the default GW on Android 5.0+ is non trivial
+     * and serves almost no purpose since OpenVPN only uses the
+     * default GW address to add routes for networks that should
+     * NOT be routed over the VPN. Using a well known address
+     * (127.'d'.'g'.'w') for the default GW make detecting
+     * these routes easier from the controlling app.
+     */
+    CLEAR(*rgi);
+
+    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
+    rgi->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi->iface, "android-gw");
+
+    /* Skip scanning/fetching interface from loopback interface we do
+     * normally on Linux.
+     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
+     */
+
+}
+
+void
+get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
+                         const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
+{
+    /* Same for ipv6 */
+
+    CLEAR(*rgi6);
+
+    /* Use a fake link-local address */
+    ASSERT(inet_pton(AF_INET6, "fe80::ad", &rgi6->addrs->addr_ipv6) == 1);
+    rgi6->addrs->netbits_ipv6 = 64;
+    rgi6->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi6->iface, "android-gw");
+}
+
+#elif defined(TARGET_LINUX)
 
 void
 get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
@@ -3181,7 +3223,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
     CLEAR(*rgi);
     CLEAR(best_name);
 
-#ifndef TARGET_ANDROID
     /* get default gateway IP addr */
     if (net_route_v4_best_gw(ctx, NULL, &rgi->gateway.addr, best_name) == 0)
     {
@@ -3191,25 +3232,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
             rgi->flags |= RGI_ON_LINK;
         }
     }
-#else  /* ifndef TARGET_ANDROID */
-    /* Android, set some pseudo GW, addr is in host byte order,
-     * Determining the default GW on Android 5.0+ is non trivial
-     * and serves almost no purpose since OpenVPN only uses the
-     * default GW address to add routes for networks that should
-     * NOT be routed over the VPN. Using a well known address
-     * (127.'d'.'g'.'w') for the default GW make detecting
-     * these routes easier from the controlling app.
-     */
-    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
-    rgi->flags |= RGI_ADDR_DEFINED;
-    strcpy(best_name, "android-gw");
-
-    /*
-     * Skip scanning/fetching interface from loopback interface
-     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
-     */
-    goto done;
-#endif /* ifndef TARGET_ANDROID */
 
     /* scan adapter list */
     if (rgi->flags & RGI_ADDR_DEFINED)
src/openvpn/tun.c +7 -2
index 8f8f7c6c..1db459f8 100644
@@ -906,9 +906,13 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
 #elif defined(TARGET_ANDROID)
     char out6[64];
 
+    const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, NULL);
     openvpn_snprintf(out6, sizeof(out6), "%s/%d %d",
-                     ifconfig_ipv6_local,tt->netbits_ipv6, tun_mtu);
+                     ifconfig_ipv6_local, tt->netbits_ipv6, tun_mtu);
+
     management_android_control(management, "IFCONFIG6", out6);
+
+    free(ifconfig_ipv6_local);
 #elif defined(TARGET_SOLARIS)
     argv_printf(&argv, "%s %s inet6 unplumb", IFCONFIG_PATH, ifname);
     argv_msg(M_INFO, &argv);
@@ -1045,7 +1049,8 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
     || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
     || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+    || defined(TARGET_SOLARIS) || defined(_WIN32) \
+    || defined(TARGET_ANDROID)
     const char *ifconfig_local = NULL;
     const char *ifconfig_remote_netmask = NULL;
     const char *ifconfig_broadcast = NULL;
Signature · 1 line
Antonio Quartulli
#5 lore raw
In reply to Antonio Quartulli
Quoted 31 lines · Antonio Quartulli

Hi Arne,

Quoted 10 lines · Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

Thanks for fixing this!

However you're missing the signed off line.

Quoted 12 lines
---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +0 -0
index 5f63fd34..a302746e 100644
@@ -3349,7 +3349,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
Quoted 5 lines

src/openvpn/route.c get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, · line 3352

Quoted 2 lines
src/openvpn/route.c +1 -1
-        if (rgi6->iface)
+        if (strlen(rgi6->iface))

how about adding a "> 0"? I know it's basically the same here, but I think that's the style we use everywhere.

Agreed, but just thinking aloud ... since we use CLEAR() and this is a static allocated buffer; constant size always "readable" - wouldn't it be better to do 'if (rgi6->iface[0])' instead? Since the buffer should be NULL terminated and has to be NULL terminted for strlen() to function anyhow. But the compiled code would be a bit more efficient (even though, this isn't necessarily a performance critical code section).

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#6 lore raw

Hi,

Quoted 5 lines · David Sommerseth

wouldn't it be better to
do 'if (rgi6->iface[0])' instead? Since the buffer should be NULL terminated and has to be NULL terminted for strlen() to function anyhow. But the compiled code would be a bit more efficient (even though, this isn't necessarily a performance critical code section).

In my opinion strlen() is more readable for the casual developer checking this code. Behind iface[0] we may hide other ambiguous assumptions (even though this is not the case here, but we won't remember in some months from now).

Cheers,

Signature · 1 line
Antonio Quartulli
#7 lore raw

Hi,

Quoted 11 lines · Antonio Quartulli
Quoted 5 lines · David Sommerseth

wouldn't it be better to
do 'if (rgi6->iface[0])' instead? Since the buffer should be NULL terminated and has to be NULL terminted for strlen() to function anyhow. But the compiled code would be a bit more efficient (even though, this isn't necessarily a performance critical code section).

In my opinion strlen() is more readable for the casual developer checking this code. Behind iface[0] we may hide other ambiguous assumptions (even though this is not the case here, but we won't remember in some months from now).

Antionio's answer is of course the real reason to use strlen(), but you nerd-sniped me by saying "compiled code would be a bit more efficient". As you said, that wouldn't matter here, but still: compilers nowadays do optimize calls to strlen, memset, memcpy, etc.

The following code

#include <string.h>

int test(const char *buf) {
    return (strlen(buf) > 0);
}

for example compiles with -O3 on my GCC 8.3 to

0000000000000000 <test>:

   0:	31 c0                	xor    %eax,%eax
   2:	80 3f 00             	cmpb   $0x0,(%rdi)
   5:	0f 95 c0             	setne  %al
   8:	c3                   	retq

which indeed just compares the first byte of the buffer against zero.

So, unless there are important performance constraints, let's optimize code for readability, rather than performance :)

FOOOOM [0]

-Steffan

[0] https://xkcd.com/356/

#8 lore raw
Quoted 36 lines · Steffan Karger

Hi,

Quoted 11 lines · Antonio Quartulli
Quoted 5 lines · David Sommerseth

wouldn't it be better to
do 'if (rgi6->iface[0])' instead? Since the buffer should be NULL terminated and has to be NULL terminted for strlen() to function anyhow. But the compiled code would be a bit more efficient (even though, this isn't necessarily a performance critical code section).

In my opinion strlen() is more readable for the casual developer checking this code. Behind iface[0] we may hide other ambiguous assumptions (even though this is not the case here, but we won't remember in some months from now).

Antionio's answer is of course the real reason to use strlen(), but you nerd-sniped me by saying "compiled code would be a bit more efficient". As you said, that wouldn't matter here, but still: compilers nowadays do optimize calls to strlen, memset, memcpy, etc.

The following code

#include <string.h>

int test(const char *buf) {
    return (strlen(buf) > 0);
}

for example compiles with -O3 on my GCC 8.3 to

0000000000000000 <test>:

   0:	31 c0                	xor    %eax,%eax
   2:	80 3f 00             	cmpb   $0x0,(%rdi)
   5:	0f 95 c0             	setne  %al
   8:	c3                   	retq
Quoted 4 lines

which indeed just compares the first byte of the buffer against zero.

So, unless there are important performance constraints, let's optimize code for readability, rather than performance :)

Cool! Thanks! I didn't run such a check, as I really didn't expect the compiler to be _that_ smart ... but I agree with you both, and especially when the end result is optimized for both readability *and* performance ;-)

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#1 lore raw

PATCH 1/2 Fix check if iface name is set

In reply to Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +1 -1
index 5f63fd34..a302746e 100644
@@ -3349,7 +3349,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
-        if (rgi6->iface)
+        if (strlen(rgi6->iface))
         {
             rgi6->flags |= RGI_IFACE_DEFINED;
         }
Signature · 1 line
2.22.0
#2 lore raw

PATCH 2/2 Adjust Android code after sitnl patch merge

It turns out that the only part of Android that still shares routing code with Linux is the get_default_ipv6 method.

Instead of fixing a method that makes little sense on Android anyway, have a method that returns a fake ipv6 gateway like for ipv4.

---
 src/openvpn/route.c | 66 ++++++++++++++++++++++++++++++---------------
 src/openvpn/tun.c   |  9 +++++--
 2 files changed, 51 insertions(+), 24 deletions(-)
src/openvpn/route.c +44 -22
index a302746e..9af88f00 100644
@@ -1065,7 +1065,8 @@ redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt,
                             tt,
                             flags,
                             &rl->rgi,
-                            es);
+                            es,
+                            ctx);
 
 #else
                 if (rl->flags & RG_DEF1)
@@ -3169,7 +3170,48 @@ show_routes(int msglev)
     gc_free(&gc);
 }
 
-#elif defined(TARGET_LINUX) || defined(TARGET_ANDROID)
+#elif defined(TARGET_ANDROID)
+
+void
+get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
+{
+    /* Android, set some pseudo GW, addr is in host byte order,
+     * Determining the default GW on Android 5.0+ is non trivial
+     * and serves almost no purpose since OpenVPN only uses the
+     * default GW address to add routes for networks that should
+     * NOT be routed over the VPN. Using a well known address
+     * (127.'d'.'g'.'w') for the default GW make detecting
+     * these routes easier from the controlling app.
+     */
+    CLEAR(*rgi);
+
+    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
+    rgi->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi->iface, "android-gw");
+
+    /* Skip scanning/fetching interface from loopback interface we do
+     * normally on Linux.
+     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
+     */
+
+}
+
+void
+get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
+                         const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
+{
+    /* Same for ipv6 */
+
+    CLEAR(*rgi6);
+
+    /* Use a fake link-local address */
+    ASSERT(inet_pton(AF_INET6, "fe80::ad", &rgi6->addrs->addr_ipv6) == 1);
+    rgi6->addrs->netbits_ipv6 = 64;
+    rgi6->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi6->iface, "android-gw");
+}
+
+#elif defined(TARGET_LINUX)
 
 void
 get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
@@ -3181,7 +3223,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
     CLEAR(*rgi);
     CLEAR(best_name);
 
-#ifndef TARGET_ANDROID
     /* get default gateway IP addr */
     if (net_route_v4_best_gw(ctx, NULL, &rgi->gateway.addr, best_name) == 0)
     {
@@ -3191,25 +3232,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
             rgi->flags |= RGI_ON_LINK;
         }
     }
-#else  /* ifndef TARGET_ANDROID */
-    /* Android, set some pseudo GW, addr is in host byte order,
-     * Determining the default GW on Android 5.0+ is non trivial
-     * and serves almost no purpose since OpenVPN only uses the
-     * default GW address to add routes for networks that should
-     * NOT be routed over the VPN. Using a well known address
-     * (127.'d'.'g'.'w') for the default GW make detecting
-     * these routes easier from the controlling app.
-     */
-    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
-    rgi->flags |= RGI_ADDR_DEFINED;
-    strcpy(best_name, "android-gw");
-
-    /*
-     * Skip scanning/fetching interface from loopback interface
-     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
-     */
-    goto done;
-#endif /* ifndef TARGET_ANDROID */
 
     /* scan adapter list */
     if (rgi->flags & RGI_ADDR_DEFINED)
src/openvpn/tun.c +7 -2
index 8f8f7c6c..1db459f8 100644
@@ -906,9 +906,13 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
 #elif defined(TARGET_ANDROID)
     char out6[64];
 
+    const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, NULL);
     openvpn_snprintf(out6, sizeof(out6), "%s/%d %d",
-                     ifconfig_ipv6_local,tt->netbits_ipv6, tun_mtu);
+                     ifconfig_ipv6_local, tt->netbits_ipv6, tun_mtu);
+
     management_android_control(management, "IFCONFIG6", out6);
+
+    free(ifconfig_ipv6_local);
 #elif defined(TARGET_SOLARIS)
     argv_printf(&argv, "%s %s inet6 unplumb", IFCONFIG_PATH, ifname);
     argv_msg(M_INFO, &argv);
@@ -1045,7 +1049,8 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
     || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
     || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+    || defined(TARGET_SOLARIS) || defined(_WIN32) \
+    || defined(TARGET_ANDROID)
     const char *ifconfig_local = NULL;
     const char *ifconfig_remote_netmask = NULL;
     const char *ifconfig_broadcast = NULL;
Signature · 1 line
2.22.0
#3 lore raw
In reply to Arne Schwabe

Hi Arne,

better mark the patches with v2.
They are also both missing the signed-off-by line.

how about git format-patch -v2 -s -2 ? :)

Cheers,

Quoted 28 lines · Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +1 -1
index 5f63fd34..a302746e 100644
@@ -3349,7 +3349,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
-        if (rgi6->iface)
+        if (strlen(rgi6->iface))
         {
             rgi6->flags |= RGI_IFACE_DEFINED;
         }
Signature · 1 line
Antonio Quartulli
#11 lore raw

PATCH v2 1/2 Fix check if iface name is set

In reply to Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

Patch V2: use if(strlen > 0) instead of if(strlen)

---
 src/openvpn/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/route.c +1 -1
index 3035fa89..1b5eacea 100644
@@ -3350,7 +3350,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
             rgi6->flags |= RGI_ADDR_DEFINED;
         }
 
-        if (rgi6->iface)
+        if (strlen(rgi6->iface) > 0)
         {
             rgi6->flags |= RGI_IFACE_DEFINED;
         }
Signature · 1 line
2.22.0
#12 lore raw

PATCH v2 2/2 Adjust Android code after sitnl patch merge

It turns out that the only part of Android that still shares routing code with Linux is the get_default_ipv6 method.

Instead of fixing a method that makes little sense on Android anyway, have a method that returns a fake ipv6 gateway like for ipv4.

Patch V2: Instead adding ANDROID to a long list of ifdefs, change this
to a non LINUX ifdef

---
 src/openvpn/route.c | 63 ++++++++++++++++++++++++++++++---------------
 src/openvpn/tun.c   | 22 ++++------------
 2 files changed, 47 insertions(+), 38 deletions(-)
src/openvpn/route.c +42 -21
index 1b5eacea..165fce92 100644
@@ -3170,7 +3170,48 @@ show_routes(int msglev)
     gc_free(&gc);
 }
 
-#elif defined(TARGET_LINUX) || defined(TARGET_ANDROID)
+#elif defined(TARGET_ANDROID)
+
+void
+get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
+{
+    /* Android, set some pseudo GW, addr is in host byte order,
+     * Determining the default GW on Android 5.0+ is non trivial
+     * and serves almost no purpose since OpenVPN only uses the
+     * default GW address to add routes for networks that should
+     * NOT be routed over the VPN. Using a well known address
+     * (127.'d'.'g'.'w') for the default GW make detecting
+     * these routes easier from the controlling app.
+     */
+    CLEAR(*rgi);
+
+    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
+    rgi->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi->iface, "android-gw");
+
+    /* Skip scanning/fetching interface from loopback interface we do
+     * normally on Linux.
+     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
+     */
+
+}
+
+void
+get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
+                         const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
+{
+    /* Same for ipv6 */
+
+    CLEAR(*rgi6);
+
+    /* Use a fake link-local address */
+    ASSERT(inet_pton(AF_INET6, "fe80::ad", &rgi6->addrs->addr_ipv6) == 1);
+    rgi6->addrs->netbits_ipv6 = 64;
+    rgi6->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    strcpy(rgi6->iface, "android-gw");
+}
+
+#elif defined(TARGET_LINUX)
 
 void
 get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
@@ -3182,7 +3223,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
     CLEAR(*rgi);
     CLEAR(best_name);
 
-#ifndef TARGET_ANDROID
     /* get default gateway IP addr */
     if (net_route_v4_best_gw(ctx, NULL, &rgi->gateway.addr, best_name) == 0)
     {
@@ -3192,25 +3232,6 @@ get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
             rgi->flags |= RGI_ON_LINK;
         }
     }
-#else  /* ifndef TARGET_ANDROID */
-    /* Android, set some pseudo GW, addr is in host byte order,
-     * Determining the default GW on Android 5.0+ is non trivial
-     * and serves almost no purpose since OpenVPN only uses the
-     * default GW address to add routes for networks that should
-     * NOT be routed over the VPN. Using a well known address
-     * (127.'d'.'g'.'w') for the default GW make detecting
-     * these routes easier from the controlling app.
-     */
-    rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
-    rgi->flags |= RGI_ADDR_DEFINED;
-    strcpy(best_name, "android-gw");
-
-    /*
-     * Skip scanning/fetching interface from loopback interface
-     * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
-     */
-    goto done;
-#endif /* ifndef TARGET_ANDROID */
 
     /* scan adapter list */
     if (rgi->flags & RGI_ADDR_DEFINED)
src/openvpn/tun.c +5 -17
index 8f8f7c6c..d4735640 100644
@@ -878,10 +878,7 @@ static void
 do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
                  const struct env_set *es, openvpn_net_ctx_t *ctx)
 {
-#if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
-    || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
-    || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+#if !defined(TARGET_LINUX)
     struct argv argv = argv_new();
     struct gc_arena gc = gc_new();
     const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
@@ -907,7 +904,7 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
     char out6[64];
 
     openvpn_snprintf(out6, sizeof(out6), "%s/%d %d",
-                     ifconfig_ipv6_local,tt->netbits_ipv6, tun_mtu);
+                     ifconfig_ipv6_local, tt->netbits_ipv6, tun_mtu);
     management_android_control(management, "IFCONFIG6", out6);
 #elif defined(TARGET_SOLARIS)
     argv_printf(&argv, "%s %s inet6 unplumb", IFCONFIG_PATH, ifname);
@@ -1015,10 +1012,7 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
     msg(M_FATAL, "Sorry, but I don't know how to do IPv6 'ifconfig' commands on this operating system.  You should ifconfig your TUN/TAP device manually or use an --up script.");
 #endif /* outer "if defined(TARGET_xxx)" conditional */
 
-#if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
-    || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
-    || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+#if !defined(TARGET_LINUX)
     gc_free(&gc);
     argv_reset(&argv);
 #endif
@@ -1042,10 +1036,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
      */
     bool tun = is_tun_p2p(tt);
 
-#if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
-    || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
-    || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+#if !defined(TARGET_LINUX)
     const char *ifconfig_local = NULL;
     const char *ifconfig_remote_netmask = NULL;
     const char *ifconfig_broadcast = NULL;
@@ -1396,10 +1387,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
     msg(M_FATAL, "Sorry, but I don't know how to do 'ifconfig' commands on this operating system.  You should ifconfig your TUN/TAP device manually or use an --up script.");
 #endif /* if defined(TARGET_LINUX) */
 
-#if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
-    || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
-    || defined(TARGET_DRAGONFLY) || defined(TARGET_AIX) \
-    || defined(TARGET_SOLARIS) || defined(_WIN32)
+#if !defined(TARGET_LINUX)
     gc_free(&gc);
     argv_reset(&argv);
 #endif
Signature · 1 line
2.22.0
Acked-by on v2 1/2
#14 lore raw
In reply to Arne Schwabe

Hi,

Quoted 7 lines · Arne Schwabe

Clang/Android complained

warning: address of array 'rgi6->iface' will always evaluate to 'true' [-Wpointer-bool-conversion]

          if (rgi6->iface)

iface is a char[16]; So its pointer is always true.

Quoted 7 lines

we do a CLEAR(rgi6) always before setting this struct and strcpy the name into iface. So using strlen instead of checking for the pointer should be the right fix.

Patch V2: use if(strlen > 0) instead of if(strlen)

Signature · 1 line
Antonio Quartulli
#15 lore raw
In reply to Arne Schwabe

Your patch has been applied to the master branch.

commit ac4ceada14b9cb8d1a6180a2e1e07d81af8e34f6

Author: Arne Schwabe
Date:   Thu Aug 15 14:10:52 2019 +0200
     Fix check if iface name is set

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v2 2/2
#13 lore raw

[Openvpn-devel] [PATCH applied] Re: Adjust Android code after sitnl patch merge

In reply to Arne Schwabe

I can't really test this, but it only touches (and cleans up) android- specific code - and by convention if you say "android needs this", so it does :-) - and it looks generally sane.

Your patch has been applied to the master branch.

commit 26e5b80096a78ec7d5e7bcd15f81155be5fde2ec

Author: Arne Schwabe
Date:   Thu Aug 15 14:10:53 2019 +0200
     Adjust Android code after sitnl patch merge

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18752.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering