From: "cron2 (Code Review)" Date: Fri, 31 Jan 2025 15:57:14 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., lstipakov@...277... Message-ID: <9699580a65dcec31c6ca0684aba5ef4ee6e0c6c3-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ezBtrz6nVnk="; 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: stipa , plaisthos , flichtenheld Cc: openvpn-devel --ezBtrz6nVnk= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#6) to the change originally created by= stipa. ( http://gerrit.openvpn.net/c/openvpn/+/879?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 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 Acked-by: Gert Doering Message-Id: <20250131154135.32169-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 769.html Signed-off-by: Gert Doering --- M src/openvpn/route.c M src/openvpn/route.h 2 files changed, 101 insertions(+), 50 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/79/879/6 diff --git a/src/openvpn/route.c b/src/openvpn/route.c index dd37fb9..d895e1c 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -2732,40 +2732,105 @@ 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) + goto done; + } + + rgi->flags =3D RGI_ADDR_DEFINED | RGI_IFACE_DEFINED; + rgi->gateway.addr =3D ntohl(best_route.NextHop.Ipv4.sin_addr.S_un.S_ad= dr); + rgi->adapter_index =3D best_route.InterfaceIndex; + + if (rgi->gateway.addr =3D=3D INADDR_ANY) + { + rgi->flags |=3D RGI_ON_LINK; + } + + /* get netmask and MAC address */ + const IP_ADAPTER_INFO *adapters =3D get_adapter_info_list(&gc); + 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; + + /* get netmask for non-onlink routes */ + in_addr_t nm =3D inet_addr(ai->IpAddressList.IpMask.String); + if (!(rgi->flags & RGI_ON_LINK) && (nm !=3D INADDR_NONE)) { - 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; - } - } + rgi->gateway.netmask =3D ntohl(nm); + rgi->flags |=3D RGI_NETMASK_DEFINED; } } +done: gc_free(&gc); } @@ -2823,43 +2888,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; } diff --git a/src/openvpn/route.h b/src/openvpn/route.h index 0bbedbb..98ea79e 100644 --- a/src/openvpn/route.h +++ b/src/openvpn/route.h @@ -334,6 +334,13 @@ bool is_special_addr(const char *addr_str); +/** + * @brief Retrieves the best gateway for a given destination based on the = routing table. + * + * @param rgi Pointer to a struct to store the gateway information. + * @param dest Destination IP address in host byte order. + * @param ctx Pointer to a platform-specific network context struct. + */ void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvpn_net_ctx_t *ctx); -- 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: 6 Gerrit-Owner: stipa Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --ezBtrz6nVnk= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #6 to the change originally cr= eated by stipa.

View Change

The following approvals got outdated and= were removed: Code-Review+2 by cron2

route.c: improve get_default_gateway() logic on Wind=
ows

When adding host route for IPv4, we use the default gateway. The= re are
cases, however, when this does not work - for example when remote=
is not accessible via default gateway but via dedicated route.

F= actor out code to look for the best gateway to reach the host from
get_d= efault_gateway_ipv6() and generalize is for IPv4/6.

Change-Id: I6c7e= 1cef637fe9fb3f3bc6ff4fb2c65599cd86fb
Signed-off-by: Lev Stipakov <lev= @...515...>
Acked-by: Gert Doering <gert@...1296...>
Message= -Id: <20250131154135.32169-1-gert@...1296...>
URL: https://www.mai= l-archive.com/openvpn-devel@lists.sourceforge.net/msg30769.html
Signed-o= ff-by: Gert Doering <gert@...1296...>
---
M src/openvpn/route.c=
M src/openvpn/route.h
2 files changed, 101 insertions(+), 50 deletio= ns(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/change=
s/79/879/6
diff --git a/src/openvpn/route.c b/src/openvpn/route.c
index dd37fb9..d895e1c 100644
--- a/src/open= vpn/route.c
+++ b/src/openvpn/route.c
@@ -2= 732,40 +2732,105 @@
return ret;
}

+/**
+ * @brief Determines = the best route to a destination for both IPv4 and IPv6.
+ *
+ * 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 inter= nal string allocation.
+ * @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_ROW2 *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;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ msg(D_ROUTE_DEBUG, "GetBestInterfaceEx() returned if=3D= %d", (int)best_if_index);
+
+ /* g= et the routing information (such as NextHop) for the destination and interf= ace */
+ NET_LUID = luid;
+ CLEAR(luid= );
+ SOCKADDR_INET= best_src;
+ CLEAR= (best_src);
+ stat= us =3D GetBestRoute2(&luid, best_if_index, NULL,
+ dest, 0, best_= route, &best_src);
+ {
+ = msg(D_ROUTE, "NOTE: GetIpForwardEntry2 returned error: %s (code= =3D%u)",
+ = strerror_win32(status, gc),
+ (unsigned int)status);
+ goto done;
+ }
+
+done:=
+ return status;<= /span>
+}
+
void
get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openv= pn_net_ctx_t *ctx)

{
- struct gc_arena gc =3D gc_new();
-
- const IP_ADAPTER_INFO *adapters =3D get_adapter_info_list(&a= mp;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 SO= CKADDR_INET */

+ S= OCKADDR_INET sa;
+ = CLEAR(sa);
+ sa.s= i_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-&= gt;gateway.addr =3D ntohl(row->dwForwardNextHop);
- if (rgi->gateway.addr)+ goto done;
= + }
+
+ rgi->flags =3D RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+ rgi->gateway.add= r =3D ntohl(best_route.NextHop.Ipv4.sin_addr.S_un.S_addr);
+ rgi->adapter_index =3D best_r= oute.InterfaceIndex;
= +
+ if (rgi->ga= teway.addr =3D=3D INADDR_ANY)
+ {
+ = rgi->flags |=3D RGI_ON_LINK;
+ }
+
+ /* get netmas= k and MAC address */
= + const IP_ADAPTER_INFO *adapters =3D get_adapter_info_list(&gc);
+ const IP_ADAPTER_I= NFO *ai =3D get_adapter(adapters, rgi->adapter_index);
+ if (ai)
+ {
+ memcpy(rgi->hwaddr, ai->Address, 6);+ rgi->flags |=3D R= GI_HWADDR_DEFINED;
+<= /span>
+ /* get netma= sk for non-onlink routes */
+ in_addr_t nm =3D inet_addr(ai->IpAddressList.IpMask.Stri= ng);
+ if (!(r= gi->flags & RGI_ON_LINK) && (nm !=3D INADDR_NONE)) {
- = rgi->flags |=3D RGI_ADDR_DEFINED;
- a_index =3D adapter_index_of_ip(adapters= , rgi->gateway.addr, NULL, &rgi->gateway.netmask);
- if (a_index !=3D TUN_ADA= PTER_INDEX_INVALID)

- = {
- = rgi->adapter_index =3D a_index;
- rgi->flags |=3D (RGI_IFACE_DEFINED|RG= I_NETMASK_DEFINED);
- = ai =3D get_adapter(adapters, a_index);
- if (ai)
- {
- memcpy(rgi->hwaddr, = ai->Address, 6);
- = rgi->flags |=3D RGI_HWADDR_DEFINED;
- }
- }
+ rgi->gateway.netmask =3D ntohl(nm)= ;
+ rgi-&g= t;flags |=3D RGI_NETMASK_DEFINED;
}
}


+done:
gc_free(&gc);
}

@@ -2823,43 +2888,22 @@
= const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
{
struct gc_arena gc =3D gc_new();
- MIB_IPFORWARD_ROW2 Be= stRoute;
- 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 Destinat= ionAddress;
+ CLEA= R(DestinationAddress);
DestinationAddress.si_family = =3D AF_INET6;
if (dest)
{<= br> DestinationAddress.Ipv6.sin6_addr =3D *dest;
}


- status =3D GetBestInterfaceEx( (struct sockaddr *)&Destinati= onAddress, &BestIfIndex );
+ MIB_IPFORWARD_ROW2 BestRoute;
+ CLEAR(BestRoute);
+ DWORD status =3D get_best_route(&gc, &am= p;DestinationAddress, &BestRoute);

= if (status !=3D NO_ERROR)
{
- msg(D_ROUTE, "NOTE: GetBestInt= erfaceEx returned error: %s (code=3D%u)",
- strerror_win32(status, &gc),
- (unsigned int)s= tatus);
- goto d= one;
- }
<= span style=3D"color: hsl(0, 100%, 40%);">-

- msg( D_ROUTE, "GetBestInterfaceEx() returned = if=3D%d", (int) BestIfIndex );
-
- sta= tus =3D GetBestRoute2( &InterfaceLuid, BestIfIndex, NULL,
- &De= stinationAddress, 0,

- = &BestRoute, &BestSourceAddress );=
-
- if (status !=3D NO_ERROR)
- {
- msg(D_ROUTE, "NOTE: GetIpForwardEntry2 retur= ned error: %s (code=3D%u)",
- strerror_win32(status, &gc),
- (unsigned int)status);=
goto done;
}

diff --git a/src/openvpn/route.h b/src/openvpn/route.hindex 0bbedbb..98ea79e 100644
--- a/src/openvpn/rou= te.h
+++ b/src/openvpn/route.h
@@ -334,6 +3= 34,13 @@

bool is_special_addr(const char= *addr_str);

+/**
+ * @b= rief Retrieves the best gateway for a given destination based on the routin= g table.
+ *+ * @param rgi Pointer to a = struct to store the gateway information.
+ * @param dest Destination IP address in host byte ord= er.
+ * @param ctx P= ointer to a platform-specific network context struct.
+ */
void get_default_gat= eway(struct route_gateway_info *rgi,
= in_addr_t dest,
openvpn_net_c= tx_t *ctx);

To view, visit change 879. To un= subscribe, 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: 6
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--ezBtrz6nVnk=--