Skip to contentSkip to conversation

Fix error message about extended errors for IPv4-only sockets.

PATCH Superseded Gert Doering 3 participants 7 messages Jun 7, 2022 – Jul 25, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix error message about extended errors for IPv4-only sockets.

The new code to enable IPv6 extended error reporting will cause an error ("Protocol not available (errno=92)") if trying to enable that setsockopt() option on an IPv4-only socket.

Fix: pass sock->info.af to set_sock_extended_error_passing(), only apply to AF_INET6 sockets.

Add comments to make explicit that the asymmetry here (IPv4 extended socket error reporting is enabled on all sockets) is intentional.

---
 src/openvpn/mtu.c    | 13 +++++++++----
 src/openvpn/mtu.h    |  2 +-
 src/openvpn/socket.c |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
src/openvpn/mtu.c +9 -4
index 59b91798..f60f4853 100644
@@ -413,17 +413,22 @@ exit:
 }
 
 void
-set_sock_extended_error_passing(int sd)
+set_sock_extended_error_passing(int sd, sa_family_t proto_af)
 {
     int on = 1;
-    /* see "man 7 ip" (on Linux) */
+    /* see "man 7 ip" (on Linux)
+     * this works on IPv4 and IPv6(-dual-stack) sockets (v4-mapped)
+     */
     if (setsockopt(sd, SOL_IP, IP_RECVERR, (void *) &on, sizeof(on)) != 0)
     {
         msg(M_WARN | M_ERRNO,
             "Note: enable extended error passing on TCP/UDP socket failed (IP_RECVERR)");
     }
-    /* see "man 7 ipv6" (on Linux) */
-    if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVERR, (void *) &on, sizeof(on)) != 0)
+    /* see "man 7 ipv6" (on Linux)
+     * this only works on IPv6 sockets
+     */
+    if (proto_af == AF_INET6
+        && setsockopt(sd, IPPROTO_IPV6, IPV6_RECVERR, (void *) &on, sizeof(on)) != 0)
     {
         msg(M_WARN | M_ERRNO,
             "Note: enable extended error passing on TCP/UDP socket failed (IPV6_RECVERR)");
src/openvpn/mtu.h +1 -1
index 7f967e06..ac3268f7 100644
@@ -278,7 +278,7 @@ void alloc_buf_sock_tun(struct buffer *buf,
 
 #if EXTENDED_SOCKET_ERROR_CAPABILITY
 
-void set_sock_extended_error_passing(int sd);
+void set_sock_extended_error_passing(int sd, sa_family_t proto_af);
 
 const char *format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc);
 
src/openvpn/socket.c +1 -1
index 4e4a3a2f..47f1ba27 100644
@@ -1949,7 +1949,7 @@ phase2_set_socket_flags(struct link_socket *sock)
 
 #if EXTENDED_SOCKET_ERROR_CAPABILITY
     /* if the OS supports it, enable extended error passing on the socket */
-    set_sock_extended_error_passing(sock->sd);
+    set_sock_extended_error_passing(sock->sd, sock->info.af);
 #endif
 }
 
Signature · 1 line
2.35.1
#1 lore raw

src/openvpn/socket.c phase2_set_socket_flags(struct link_socket *sock) · line 1954

Quoted 8 lines
  #if EXTENDED_SOCKET_ERROR_CAPABILITY
      /* if the OS supports it, enable extended error passing on the socket */
src/openvpn/socket.c +1 -1
-    set_sock_extended_error_passing(sock->sd);
+    set_sock_extended_error_passing(sock->sd, sock->info.af);
  #endif
  }

I have to NACK this patch. sock->info.af is not the actual address familiy but the family we want to use and it might be 0 if there is no udp4/udp6 in the config file.

For a client you can look at sock->info.lsa->actual.dest if that is defined. For a server we seem to actually override/set that info.af value according to my debugger but with a client I get 0 in that value. It looks that set_mtu_discover_type in the same function has the same problem, so it is easy to this mistake.

Arne

#2 lore raw

Hi,

Quoted 12 lines · Arne Schwabe

src/openvpn/socket.c phase2_set_socket_flags(struct link_socket *sock) · line 1954

Quoted 7 lines
  #if EXTENDED_SOCKET_ERROR_CAPABILITY
      /* if the OS supports it, enable extended error passing on the socket */
src/openvpn/socket.c +1 -1
-    set_sock_extended_error_passing(sock->sd);
+    set_sock_extended_error_passing(sock->sd, sock->info.af);
  #endif
  }

I have to NACK this patch. sock->info.af is not the actual address familiy but the family we want to use and it might be 0 if there is no udp4/udp6 in the config file.

Quoted 5 lines

For a client you can look at sock->info.lsa->actual.dest if that is defined. For a server we seem to actually override/set that info.af value according to my debugger but with a client I get 0 in that value. It looks that set_mtu_discover_type in the same function has the same problem, so it is easy to this mistake.

So how do we know if this is a v4 or v6 socket, in a client?

Phrased differently - I could do a v2 which will just ignore this particular errno value. Maybe not "as clean" as not calling the "wrong" setsockopt() in the first place, but will keep the log free of non-useful error messages just fine...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#3 lore raw

Am 19.06.22 um 19:03 schrieb Gert Doering:

Quoted 21 lines

Hi,

Quoted 12 lines · Arne Schwabe

src/openvpn/socket.c phase2_set_socket_flags(struct link_socket *sock) · line 1954

Quoted 7 lines
   #if EXTENDED_SOCKET_ERROR_CAPABILITY
       /* if the OS supports it, enable extended error passing on the socket */
src/openvpn/socket.c +1 -1
-    set_sock_extended_error_passing(sock->sd);
+    set_sock_extended_error_passing(sock->sd, sock->info.af);
   #endif
   }

I have to NACK this patch. sock->info.af is not the actual address familiy but the family we want to use and it might be 0 if there is no udp4/udp6 in the config file.

Quoted 5 lines

For a client you can look at sock->info.lsa->actual.dest if that is defined. For a server we seem to actually override/set that info.af value according to my debugger but with a client I get 0 in that value. It looks that set_mtu_discover_type in the same function has the same problem, so it is easy to this mistake.

Quoted 6 lines

So how do we know if this is a v4 or v6 socket, in a client?

Phrased differently - I could do a v2 which will just ignore this particular errno value. Maybe not "as clean" as not calling the "wrong" setsockopt() in the first place, but will keep the log free of non-useful error messages just fine...

Two ways. Either fix that info.af is 0 for client sockets. You already fixed that for MTU discovery and server sockets %) (commit ed5d0fe5097). For client socket, we should probably set the info.af in the openvpn_connect/socket_connect path.

The other more hacky is to check info.af first and then check lsa->actual.dest and see if one of them is non-zero.

Arne

#4 lore raw

PATCH v2 1/1 Fix error message about extended errors for IPv4-only sockets.

In reply to Gert Doering

The new code to enable IPv6 extended error reporting will cause an error ("Protocol not available (errno=92)") if trying to enable that setsockopt() option on an IPv4-only socket.

Fix: pass sock->info.af to set_sock_extended_error_passing(), only apply to AF_INET6 sockets.

To make that work, ensure that sock->info.af is set to not only the value coming from config (which might be AF_UNSPEC) but to the actual value used in socket creation (credits: Arne Schwabe).

Add comments to make explicit that the asymmetry here (IPv4 extended socket error reporting is enabled on all sockets) is intentional.

--
v2: set sock->info.af in create_socket() (Arne Schwabe)

---
 src/openvpn/mtu.c    | 13 +++++++++----
 src/openvpn/mtu.h    |  2 +-
 src/openvpn/socket.c |  6 +++++-
 3 files changed, 15 insertions(+), 6 deletions(-)
src/openvpn/mtu.c +9 -4
index 59b91798..f60f4853 100644
@@ -413,17 +413,22 @@ exit:
 }
 
 void
-set_sock_extended_error_passing(int sd)
+set_sock_extended_error_passing(int sd, sa_family_t proto_af)
 {
     int on = 1;
-    /* see "man 7 ip" (on Linux) */
+    /* see "man 7 ip" (on Linux)
+     * this works on IPv4 and IPv6(-dual-stack) sockets (v4-mapped)
+     */
     if (setsockopt(sd, SOL_IP, IP_RECVERR, (void *) &on, sizeof(on)) != 0)
     {
         msg(M_WARN | M_ERRNO,
             "Note: enable extended error passing on TCP/UDP socket failed (IP_RECVERR)");
     }
-    /* see "man 7 ipv6" (on Linux) */
-    if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVERR, (void *) &on, sizeof(on)) != 0)
+    /* see "man 7 ipv6" (on Linux)
+     * this only works on IPv6 sockets
+     */
+    if (proto_af == AF_INET6
+        && setsockopt(sd, IPPROTO_IPV6, IPV6_RECVERR, (void *) &on, sizeof(on)) != 0)
     {
         msg(M_WARN | M_ERRNO,
             "Note: enable extended error passing on TCP/UDP socket failed (IPV6_RECVERR)");
src/openvpn/mtu.h +1 -1
index 9db6cf26..d4856f16 100644
@@ -268,7 +268,7 @@ void alloc_buf_sock_tun(struct buffer *buf,
 
 #if EXTENDED_SOCKET_ERROR_CAPABILITY
 
-void set_sock_extended_error_passing(int sd);
+void set_sock_extended_error_passing(int sd, sa_family_t proto_af);
 
 const char *format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc);
 
src/openvpn/socket.c +5 -1
index 4e4a3a2f..b4c20f69 100644
@@ -1131,6 +1131,10 @@ create_socket(struct link_socket *sock, struct addrinfo *addr)
     {
         ASSERT(0);
     }
+    /* Set af field of sock->info, so it always reflects the address family
+     * of the created socket */
+    sock->info.af = addr->ai_family;
+
     /* set socket buffers based on --sndbuf and --rcvbuf options */
     socket_set_buffers(sock->sd, &sock->socket_buffer_sizes);
 
@@ -1949,7 +1953,7 @@ phase2_set_socket_flags(struct link_socket *sock)
 
 #if EXTENDED_SOCKET_ERROR_CAPABILITY
     /* if the OS supports it, enable extended error passing on the socket */
-    set_sock_extended_error_passing(sock->sd);
+    set_sock_extended_error_passing(sock->sd, sock->info.af);
 #endif
 }
 
Signature · 1 line
2.35.1
Acked-by on v2 1/1
#5 lore raw
Quoted 7 lines · Gert Doering

The new code to enable IPv6 extended error reporting will cause an error ("Protocol not available (errno=92)") if trying to enable that setsockopt() option on an IPv4-only socket.

Fix: pass sock->info.af to set_sock_extended_error_passing(), only apply to AF_INET6 sockets.

Quoted 8 lines

To make that work, ensure that sock->info.af is set to not only the value coming from config (which might be AF_UNSPEC) but to the actual value used in socket creation (credits: Arne Schwabe).

Add comments to make explicit that the asymmetry here (IPv4 extended socket error reporting is enabled on all sockets) is intentional.

Tested that it indeed removes the spurious error.
Code changes look good to me.

Regards,

Signature · 1 line
  Frank Lichtenheld
#6 lore raw
In reply to Gert Doering

Patch has been applied to the master branch.

For good measure, passed "make check" on FreeBSD and Linux.

commit a2e63fb978b7497ebc7e8ec56504fd2300f97621

Author: Gert Doering
Date:   Sat Jul 23 14:19:09 2022 +0200
     Fix error message about extended errors for IPv4-only sockets.

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

--
kind regards,

Gert Doering