From: "stipa (Code Review)" Date: Tue, 28 Jan 2025 13:17:56 +0000 References: Reply-To: lstipakov@...277..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <2f5e78a5c6ac0839c6bd09dc2a9236db9ba83057-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="4ZAZSK6jaHU="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: route.c: improve get_default_gateway() logic on Windows List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --4ZAZSK6jaHU= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/879?usp=3Demail to review the following change. Change subject: route.c: improve get_default_gateway() logic on Windows ...................................................................... route.c: improve get_default_gateway() logic on Windows When adding host route for IPv4, we use the default gateway. There are cases, however, when this does not work - for example when remote is not accessible via default gateway but via dedicated route. Factor out code to look for the best gateway to reach the host from get_default_gateway_ipv6() and generalize is for IPv4/6. Change-Id: I6c7e1cef637fe9fb3f3bc6ff4fb2c65599cd86fb Signed-off-by: Lev Stipakov --- M src/openvpn/route.c 1 file changed, 91 insertions(+), 52 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/79/879/1 diff --git a/src/openvpn/route.c b/src/openvpn/route.c index 640b0dc..dc98f78 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -2732,40 +2732,100 @@ return ret; } +/** + * @brief Determines the best route to a destination for both IPv4 and IPv= 6. + * + * Uses `GetBestInterfaceEx` and `GetBestRoute2` to find the optimal route + * and network interface for the specified destination address. + * + * @param gc Pointer to struct gc_arena for internal string allocation. + * @param dest The destination IP address (IPv4 or IPv6). + * @param best_route Pointer to a `MIB_IPFORWARD_ROW2` structure to store = the best route. + * @return DWORD `NO_ERROR` on success, or an error code. + */ +static DWORD +get_best_route(struct gc_arena *gc, SOCKADDR_INET *dest, MIB_IPFORWARD_ROW= 2 *best_route) +{ + DWORD best_if_index; + DWORD status; + + CLEAR(*best_route); + + /* get the best interface index to reach dest */ + status =3D GetBestInterfaceEx((struct sockaddr *)dest, &best_if_index); + if (status !=3D NO_ERROR) + { + msg(D_ROUTE, "NOTE: GetBestInterfaceEx returned error: %s (code=3D= %u)", + strerror_win32(status, gc), + (unsigned int)status); + goto done; + } + + msg(D_ROUTE_DEBUG, "GetBestInterfaceEx() returned if=3D%d", (int)best_= if_index); + + /* get the routing information (such as NextHop) for the destination a= nd interface */ + NET_LUID luid; + CLEAR(luid); + SOCKADDR_INET best_src; + CLEAR(best_src); + status =3D GetBestRoute2(&luid, best_if_index, NULL, + dest, 0, best_route, &best_src); + if (status !=3D NO_ERROR) + { + msg(D_ROUTE, "NOTE: GetIpForwardEntry2 returned error: %s (code=3D= %u)", + strerror_win32(status, gc), + (unsigned int)status); + goto done; + } + +done: + return status; +} + void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvp= n_net_ctx_t *ctx) { - struct gc_arena gc =3D gc_new(); - - const IP_ADAPTER_INFO *adapters =3D get_adapter_info_list(&gc); - const MIB_IPFORWARDTABLE *routes =3D get_windows_routing_table(&gc); - const MIB_IPFORWARDROW *row =3D get_default_gateway_row(routes); - DWORD a_index; - const IP_ADAPTER_INFO *ai; - CLEAR(*rgi); - if (row) + struct gc_arena gc =3D gc_new(); + + /* convert in_addr_t into SOCKADDR_INET */ + SOCKADDR_INET sa; + CLEAR(sa); + sa.si_family =3D AF_INET; + sa.Ipv4.sin_addr.s_addr =3D htonl(dest); + + /* get the best route to the destination */ + MIB_IPFORWARD_ROW2 best_route; + CLEAR(best_route); + DWORD status =3D get_best_route(&gc, &sa, &best_route); + if (status !=3D NO_ERROR) { - rgi->gateway.addr =3D ntohl(row->dwForwardNextHop); - if (rgi->gateway.addr) - { - rgi->flags |=3D RGI_ADDR_DEFINED; - a_index =3D adapter_index_of_ip(adapters, rgi->gateway.addr, N= ULL, &rgi->gateway.netmask); - if (a_index !=3D TUN_ADAPTER_INDEX_INVALID) - { - rgi->adapter_index =3D a_index; - rgi->flags |=3D (RGI_IFACE_DEFINED|RGI_NETMASK_DEFINED); - ai =3D get_adapter(adapters, a_index); - if (ai) - { - memcpy(rgi->hwaddr, ai->Address, 6); - rgi->flags |=3D RGI_HWADDR_DEFINED; - } - } - } + goto done; } + rgi->flags =3D RGI_ADDR_DEFINED; + rgi->gateway.addr =3D ntohl(best_route.NextHop.Ipv4.sin_addr.S_un.S_ad= dr); + + /* get netmask and adapter index */ + const IP_ADAPTER_INFO *adapters =3D get_adapter_info_list(&gc); + DWORD a_index =3D adapter_index_of_ip(adapters, rgi->gateway.addr, NUL= L, &rgi->gateway.netmask); + if (a_index =3D=3D TUN_ADAPTER_INDEX_INVALID) + { + goto done; + } + rgi->adapter_index =3D a_index; + rgi->flags |=3D (RGI_IFACE_DEFINED | RGI_NETMASK_DEFINED); + + /* get MAC address */ + const IP_ADAPTER_INFO *ai =3D get_adapter(adapters, rgi->adapter_index= ); + if (ai) + { + memcpy(rgi->hwaddr, ai->Address, 6); + rgi->flags |=3D RGI_HWADDR_DEFINED; + } + +done: gc_free(&gc); } @@ -2823,43 +2883,22 @@ const struct in6_addr *dest, openvpn_net_ctx_t *c= tx) { struct gc_arena gc =3D gc_new(); - MIB_IPFORWARD_ROW2 BestRoute; - SOCKADDR_INET DestinationAddress, BestSourceAddress; - DWORD BestIfIndex; - DWORD status; - NET_LUID InterfaceLuid; - CLEAR(*rgi6); - CLEAR(InterfaceLuid); /* cleared =3D not used for lookup= */ - CLEAR(DestinationAddress); + SOCKADDR_INET DestinationAddress; + CLEAR(DestinationAddress); DestinationAddress.si_family =3D AF_INET6; if (dest) { DestinationAddress.Ipv6.sin6_addr =3D *dest; } - status =3D GetBestInterfaceEx( (struct sockaddr *)&DestinationAddress,= &BestIfIndex ); + MIB_IPFORWARD_ROW2 BestRoute; + CLEAR(BestRoute); + DWORD status =3D get_best_route(&gc, &DestinationAddress, &BestRoute); if (status !=3D NO_ERROR) { - msg(D_ROUTE, "NOTE: GetBestInterfaceEx returned error: %s (code=3D= %u)", - strerror_win32(status, &gc), - (unsigned int)status); - goto done; - } - - msg( D_ROUTE, "GetBestInterfaceEx() returned if=3D%d", (int) BestIfInd= ex ); - - status =3D GetBestRoute2( &InterfaceLuid, BestIfIndex, NULL, - &DestinationAddress, 0, - &BestRoute, &BestSourceAddress ); - - if (status !=3D NO_ERROR) - { - msg(D_ROUTE, "NOTE: GetIpForwardEntry2 returned error: %s (code=3D= %u)", - strerror_win32(status, &gc), - (unsigned int)status); goto done; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/879?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I6c7e1cef637fe9fb3f3bc6ff4fb2c65599cd86fb Gerrit-Change-Number: 879 Gerrit-PatchSet: 1 Gerrit-Owner: stipa Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --4ZAZSK6jaHU= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

stipa would like plaisthos and flichtenheld to review t= his change.

Vi= ew Change

route.c: improve get_default_gateway() logic on Windows
When adding host route for IPv4, we use the default gateway. There arecases, however, when this does not work - for example when remote
is no= t accessible via default gateway but via dedicated route.

Factor out= code to look for the best gateway to reach the host from
get_default_ga= teway_ipv6() and generalize is for IPv4/6.

Change-Id: I6c7e1cef637fe= 9fb3f3bc6ff4fb2c65599cd86fb
Signed-off-by: Lev Stipakov <lev@...1699...= .>
---
M src/openvpn/route.c
1 file changed, 91 insertions(+), = 52 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn r=
efs/changes/79/879/1
diff --git a/src/openvpn/route.c b/src/openvpn=
/route.c
index 640b0dc..dc98f78 100644
--- = a/src/openvpn/route.c
+++ b/src/openvpn/route.c
<= span>@@ -2732,40 +2732,100 @@
return ret;
}

+/**
+ * @brief D= etermines the best route to a destination for both IPv4 and IPv6.+ *
+ * Uses `GetBestInterfaceEx` and `GetBestRoute= 2` to find the optimal route
+ * and network interface for the specified destination address.

+ *
+ * @param gc Pointer to struct gc_arena= for internal string allocation.
+ * @param dest The destination IP address (IPv4 or IPv6).
+ * @param best_route Poi= nter to a `MIB_IPFORWARD_ROW2` structure to store the best route.+ * @return DWORD `NO_ERROR` o= n success, or an error code.
+ */
+static = DWORD
+get_best_route= (struct gc_arena *gc, SOCKADDR_INET *dest, MIB_IPFORWARD_ROW2 *best_route)<= /span>
+{
+ DWORD best_if_index;
+ DWORD status;

+

+ CLEAR(*best_route);
+
+ /* get the best interface index to reach dest */
+ status =3D GetBestInterfaceEx((s= truct sockaddr *)dest, &best_if_index);
+ if (status !=3D NO_ERROR)
+ {
+ msg(D_ROUTE, "NOTE: GetBestInterfaceEx ret= urned error: %s (code=3D%u)",
+ strerror_win32(status, gc),
+ (unsigned int)status);
+ goto done;=
+ }
+
+ msg(D_ROUTE_DEBUG, "GetBestInterfaceEx() returned = if=3D%d", (int)best_if_index);
+
+ = /* get the routing information (such as NextHop) for the destination and i= nterface */
+ NET_= LUID luid;
+ CLEAR= (luid);
+ SOCKADDR= _INET best_src;
+ = CLEAR(best_src);
+ = status =3D GetBestRoute2(&luid, best_if_index, NULL,
+ dest, 0, b= est_route, &best_src);
+ if (status !=3D NO_ERROR)
+ {
+ = strerror_win32(status, gc),
+ (unsigned int)status);
+ goto done;
+ }
+
= +done:
+ return st= atus;
+}
+

void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest,= openvpn_net_ctx_t *ctx)
{
- struct gc_arena gc =3D gc_new();
-
- const IP_ADAPTER_INFO *adapters =3D get_adapter_info_li= st(&gc);
- const= MIB_IPFORWARDTABLE *routes =3D get_windows_routing_table(&gc);<= br>- const MIB_IPFORWARDROW *r= ow =3D get_default_gateway_row(routes);
- DWORD a_index;
- const IP_ADAPTER_INFO *ai;
-
CLEAR(*rgi);
=
- if (row)<= br>+ struct gc_arena gc =3D = gc_new();
++ /* convert in_addr_t into= SOCKADDR_INET */
+ = SOCKADDR_INET sa;
+= CLEAR(sa);
+ s= a.si_family =3D AF_INET;
+ sa.Ipv4.sin_addr.s_addr =3D htonl(dest);
+
+ /* get the best route to the destination */
+ MIB_IPFORWARD_ROW2 best_route;
+ CLEAR(best_route)= ;
+ DWORD status = =3D get_best_route(&gc, &sa, &best_route);
+ if (status !=3D NO_ERROR)
= {
- r= gi->gateway.addr =3D ntohl(row->dwForwardNextHop);
- if (rgi->gateway.addr)=
- {
- rgi->flags |=3D RGI_ADDR_= DEFINED;
- a= _index =3D adapter_index_of_ip(adapters, rgi->gateway.addr, NULL, &r= gi->gateway.netmask);
- {
- rgi->adapter_index =3D a_in= dex;
- r= gi->flags |=3D (RGI_IFACE_DEFINED|RGI_NETMASK_DEFINED);
- ai =3D get_adapter(ada= pters, a_index);
- = if (ai)
- = {
- = memcpy(rgi->hwaddr, ai->Address, 6);
- rgi->flags |=3D RG= I_HWADDR_DEFINED;
- = }
- = }
- }=
+ goto done;<= br> }

+ rgi->flags =3D RGI_ADDR_DEFINED;
+ rgi->gateway.addr =3D ntohl(best_r= oute.NextHop.Ipv4.sin_addr.S_un.S_addr);
+
+ /* get netmask and adapter index */
+ const IP_ADAPTER_INFO *adapters =3D get_adapter_i= nfo_list(&gc);
+ = DWORD a_index =3D adapter_index_of_ip(adapters, rgi->gateway.addr, NU= LL, &rgi->gateway.netmask);
+ if (a_index =3D=3D TUN_ADAPTER_INDEX_INVALID)
= + {
+ goto done;
+ }
+ rgi->adapter_index =3D a_index;
+ rgi->flags |=3D (RGI_IFACE_DEFINE= D | RGI_NETMASK_DEFINED);
+
+ /* get MA= C address */
+ con= st IP_ADAPTER_INFO *ai =3D get_adapter(adapters, rgi->adapter_index);
+ if (ai)
= + {
+ memcpy(rgi->hwaddr, ai->Addres= s, 6);
+ rgi-&= gt;flags |=3D RGI_HWADDR_DEFINED;
+ }
+=
+done:
gc_free(&gc);

}

@@ -2823,43 +2883,22 @@

const= struct in6_addr *dest, openvpn_net_ctx_t *ctx)
{ struct gc_arena gc =3D gc_new();
- MIB_IPFORWARD_ROW2 BestRoute;
- SOCKADDR_INET DestinationAddress, B= estSourceAddress;
- = DWORD BestIfIndex;
- = DWORD status;
- NET= _LUID InterfaceLuid;
-<= /span>
CLEAR(*rgi6);
- CLEAR(InterfaceLuid); /* cleared =3D not use= d for lookup */
- CL= EAR(DestinationAddress);

+ SOCKADDR_INET DestinationAddress;
+ CLEAR(DestinationAddress);
DestinationAddress.si_family =3D AF_INET6;
if (dest)

{
Destinatio= nAddress.Ipv6.sin6_addr =3D *dest;
}
=
- status =3D GetBes= tInterfaceEx( (struct sockaddr *)&DestinationAddress, &BestIfIndex = );
+ MIB_IPFORWARD= _ROW2 BestRoute;
+ = CLEAR(BestRoute);
+ = DWORD status =3D get_best_route(&gc, &DestinationAddress, &B= estRoute);

if (status !=3D NO_ERROR)=
{
- msg(D_ROUTE, "NOTE: GetBestInterfaceEx returned error: %s (c= ode=3D%u)",
- = strerror_win32(status, &gc),
- (unsigned int)status);
- goto done;
- }
-
- msg(= D_ROUTE, "GetBestInterfaceEx() returned if=3D%d", (int) BestIfIn= dex );
-
- status =3D GetBestRoute2( &I= nterfaceLuid, BestIfIndex, NULL,
- &DestinationAddress, 0,- &a= mp;BestRoute, &BestSourceAddress );
-
- = if (status !=3D NO_ERROR)
- {
- msg= (D_ROUTE, "NOTE: GetIpForwardEntry2 returned error: %s (code=3D%u)&quo= t;,
- strerr= or_win32(status, &gc),
- (unsigned int)status);
goto done;=
}


=

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6c7e1cef637fe9fb3f3bc6ff4fb= 2c65599cd86fb
Gerrit-Change-Number: 879
Gerrit-PatchSet: 1
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--4ZAZSK6jaHU=--