Skip to contentSkip to conversation

dco: fix source IP selection

PATCH Superseded Qingfang Deng 3 participants 3 messages Feb 1, 2025 – Feb 10, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dco: fix source IP selection

When multihome option is enabled, OpenVPN passes ipi_addr to DCO, which is always 0.0.0.0. It should use ipi_spec_dst instead.
When local option is present, OpenVPN does not pass it to DCO. As a result, Linux may pick a different IP as the source IP, breaking the connection.

---
v2: fix code style reported by CI
Discussions: https://github.com/OpenVPN/openvpn/pull/668

 src/openvpn/dco.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
src/openvpn/dco.c +12 -2
index b5a21369..eef49ade 100644
@@ -493,6 +493,7 @@ dco_p2p_add_new_peer(struct context *c)
     ASSERT(sock->info.connection_established);
 
     struct sockaddr *remoteaddr = &sock->info.lsa->actual.dest.addr.sa;
+    struct sockaddr *localaddr = NULL;
     struct tls_multi *multi = c->c2.tls_multi;
 #ifdef TARGET_FREEBSD
     /* In Linux in P2P mode the kernel automatically removes an existing peer
@@ -503,8 +504,12 @@ dco_p2p_add_new_peer(struct context *c)
         c->c2.tls_multi->dco_peer_id = -1;
     }
 #endif
+    if (sock->bind_local && sock->info.lsa->bind_local) {
+        localaddr = sock->info.lsa->bind_local->ai_addr;
+    }
+
     int ret = dco_new_peer(&c->c1.tuntap->dco, multi->peer_id,
-                           c->c2.link_sockets[0]->sd, NULL, remoteaddr, NULL, NULL);
+                           c->c2.link_sockets[0]->sd, localaddr, remoteaddr, NULL, NULL);
     if (ret < 0)
     {
         return ret;
@@ -550,7 +555,7 @@ dco_multi_get_localaddr(struct multi_context *m, struct multi_instance *mi,
         {
             struct sockaddr_in *sock_in4 = (struct sockaddr_in *)local;
 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
-            sock_in4->sin_addr = actual->pi.in4.ipi_addr;
+            sock_in4->sin_addr = actual->pi.in4.ipi_spec_dst;
 #elif defined(IP_RECVDSTADDR)
             sock_in4->sin_addr = actual->pi.in4;
 #else
@@ -616,10 +621,15 @@ dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
         vpn_addr6 = &c->c2.push_ifconfig_ipv6_local;
     }
 
+    struct link_socket *ls = c->c2.link_sockets[0];
     if (dco_multi_get_localaddr(m, mi, &local))
     {
         localaddr = (struct sockaddr *)&local;
     }
+    else if (ls->bind_local && ls->info.lsa->bind_local)
+    {
+        localaddr = ls->info.lsa->bind_local->ai_addr;
+    }
 
     int ret = dco_new_peer(&c->c1.tuntap->dco, peer_id, sd, localaddr,
                            remoteaddr, vpn_addr4, vpn_addr6);
Signature · 1 line
2.43.0
#1 lore raw

Hi,

src/openvpn/dco.c dco_p2p_add_new_peer(struct context *c) · line 510

Quoted 5 lines · Qingfang Deng

#endif

src/openvpn/dco.c +4 -0
+    if (sock->bind_local && sock->info.lsa->bind_local) {
+        localaddr = sock->info.lsa->bind_local->ai_addr;
+    }
+

This is still not correct style - the opening "{" needs to go on its own line. Also see below on whether this is necessary at all.

Quoted 7 lines
     int ret = dco_new_peer(&c->c1.tuntap->dco, multi->peer_id,
src/openvpn/dco.c +1 -1
-                           c->c2.link_sockets[0]->sd, NULL, remoteaddr, NULL, NULL);
+                           c->c2.link_sockets[0]->sd, localaddr, remoteaddr, NULL, NULL);
     if (ret < 0)
     {
         return ret;

src/openvpn/dco.c dco_multi_get_localaddr(struct multi_context *m, struct multi_instance *mi, · line 560

Quoted 7 lines
src/openvpn/dco.c +1 -1
@@ -550,7 +555,7 @@ dco_multi_get_localaddr(struct multi_context *m, struct multi_instance *mi,
         {
             struct sockaddr_in *sock_in4 = (struct sockaddr_in *)local;
 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
-            sock_in4->sin_addr = actual->pi.in4.ipi_addr;
+            sock_in4->sin_addr = actual->pi.in4.ipi_spec_dst;
 #elif defined(IP_RECVDSTADDR)
             sock_in4->sin_addr = actual->pi.in4;

Well spotted.

Quoted 4 lines
src/openvpn/dco.c +0 -0
 #else
@@ -616,10 +621,15 @@ dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
         vpn_addr6 = &c->c2.push_ifconfig_ipv6_local;
     }

src/openvpn/dco.c dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi) · line 632

Quoted 10 lines
src/openvpn/dco.c +5 -0
 
+    struct link_socket *ls = c->c2.link_sockets[0];
     if (dco_multi_get_localaddr(m, mi, &local))
     {
         localaddr = (struct sockaddr *)&local;
     }
+    else if (ls->bind_local && ls->info.lsa->bind_local)
+    {
+        localaddr = ls->info.lsa->bind_local->ai_addr;
+    }

Not sure about that. If the socket is bound, the kernel knows about the binding (because we pass the socket to the kernel). You're sure this is needed?

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]...
#2 lore raw

Hi,

Thanks a lot for bringing this problem to our attention.

Quoted 4 lines · Gert Doering
Quoted 4 lines
src/openvpn/dco.c +0 -0
@@ -616,10 +621,15 @@ dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
          vpn_addr6 = &c->c2.push_ifconfig_ipv6_local;
      }
 
Quoted 13 lines

src/openvpn/dco.c dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi) · line 632

Quoted 9 lines
src/openvpn/dco.c +5 -0
+    struct link_socket *ls = c->c2.link_sockets[0];
      if (dco_multi_get_localaddr(m, mi, &local))
      {
          localaddr = (struct sockaddr *)&local;
      }
+    else if (ls->bind_local && ls->info.lsa->bind_local)
+    {
+        localaddr = ls->info.lsa->bind_local->ai_addr;
+    }

Not sure about that. If the socket is bound, the kernel knows about the binding (because we pass the socket to the kernel). You're sure this is needed?

As already questioned by Gert, I can confirm that this is not expected, because DCO should automatically pick any source address that was bound to the socket.

This said, DCO seems to not be doing this and therefore the bound source address is not respected.

Therefore this is a bug that needs fixing in DCO and should not be worked-around by userspace.

Hence, I'd suggest to drop the hunk about and rather fix the issue in DCO.
while looking at this patch, I have already identified where DCO needs adjustments, therefore it's likely that the next DCO revision will have this fixed.

Regards,

Quoted 9 lines

gert

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli