From: "mrbff (Code Review)" Date: Tue, 21 Oct 2025 14:05:10 +0000 In-Reply-To: References: Reply-To: marco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <506961227184184e72aebdcc3f729f1fccfe9ba7-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="5Te1HHLAjdc="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: route: copied 'gateway_needed' logic from add_route_ipv6 to add_route List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --5Te1HHLAjdc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, mrbff, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/677?usp=3Demail to look at the new patch set (#12). The following approvals got outdated and were removed: Code-Review-1 by plaisthos Change subject: route: copied 'gateway_needed' logic from add_route_ipv6 to= add_route ...................................................................... route: copied 'gateway_needed' logic from add_route_ipv6 to add_route Under certain circumstances it may not be necessary to pass the gateway when adding a new route via net_route_v4_add() API function. add_route_ipv6() already accounts for some of these cases and therefore this patch copies the same logic to add_route(). Change-Id: Id1ec0c6e4c391604ec5dbb8b7122f2e47ad186d1 Signed-off-by: Marco Baffo --- M Changes.rst M src/openvpn/route.c 2 files changed, 51 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/677/12 diff --git a/Changes.rst b/Changes.rst index 4feacad2..a3e00c2 100644 --- a/Changes.rst +++ b/Changes.rst @@ -170,6 +170,13 @@ COPYING: license details only relevant to our Windows installers have been updated and moved to the openvpn-build repo +The route gateway is now only added for IPv4 when strictly necessary. + The gateway-needed logic previously applied to IPv6 only has been copied + and extended to IPv4 route handling. Route additions now omit the + gateway unless required after checking device type, special routes, + on-link gateways, and whether the gateway lies inside the VPN subnet, + optimizing route configuration and potentially reducing redundant route + entries. Deprecated features ------------------- diff --git a/src/openvpn/route.c b/src/openvpn/route.c index 7d988da..c249b38 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -1479,6 +1479,7 @@ { int status =3D 0; int is_local_route; + bool gateway_needed =3D false; if (!(r->flags & RT_DEFINED)) { @@ -1488,8 +1489,8 @@ struct argv argv =3D argv_new(); struct gc_arena gc =3D gc_new(); -#if !defined(TARGET_LINUX) const char *network =3D print_in_addr_t(r->network, 0, &gc); +#if !defined(TARGET_LINUX) #if !defined(TARGET_AIX) const char *netmask =3D print_in_addr_t(r->netmask, 0, &gc); #endif @@ -1502,8 +1503,47 @@ goto done; } =20 +#ifndef _WIN32 + /* server told us which interface to use; pass gateway if it gave one = */ + if (rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi->iface[0] !=3D 0) + { + if (rgi->flags & RGI_ADDR_DEFINED && r->gateway !=3D 0) + { + gateway_needed =3D true; + } + } +#endif + + /* TAP is an L2 device, so for destinations that are not explicitly + * on-link the kernel needs a gateway so it can ARP/ND for the + * MAC address. We make an exception only when the user explicitly sets + * metric 0, which is taken as a request for an on-link =E2=80=9Croute= to + * interface=E2=80=9D (no gateway). This keeps parity with the IPv6 co= de. + */ + if (tt->type =3D=3D DEV_TYPE_TAP + && !((r->flags & RT_METRIC_DEFINED) && r->metric =3D=3D 0)) + { + gateway_needed =3D true; + } + + if (gateway_needed && r->gateway =3D=3D 0) + { + msg(M_WARN, "ROUTE WARNING: " PACKAGE_NAME " needs a gateway " + "parameter for a --route option and no default was set= via " + "--route-gateway or --ifconfig option. Not installing " + "IPv4 route to %s/%d.", + network, netmask_to_netbits2(r->netmask)); + status =3D 0; + goto done; + } + #if defined(TARGET_LINUX) - const char *iface =3D NULL; + const char *iface =3D tt->actual_name; + if (!gateway_needed && rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi-= >iface[0] !=3D 0) /* vpn server special route */ + { + iface =3D rgi->iface; + } + int metric =3D -1; if (is_on_link(is_local_route, flags, rgi)) @@ -1518,7 +1558,8 @@ status =3D RTA_SUCCESS; - int ret =3D net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->= netmask), &r->gateway, + int ret =3D net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->= netmask), + gateway_needed ? &r->gateway : NULL, iface, r->table_id, metric); if (ret =3D=3D -EEXIST) { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/677?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Id1ec0c6e4c391604ec5dbb8b7122f2e47ad186d1 Gerrit-Change-Number: 677 Gerrit-PatchSet: 12 Gerrit-Owner: mrbff Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: cron2 Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-Attention: mrbff --5Te1HHLAjdc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: cron2, flichtenheld, mrbff, plaisthos.

mrbff uploaded patch set #12 to this change.

View Change

The following approvals got outdated and were removed: Code-Review-1 by plaisthos

route: copied 'gateway_need=
ed' logic from add_route_ipv6 to add_route

Under certain circums= tances it may not be necessary to pass the
gateway when adding a new rou= te via net_route_v4_add() API function.
add_route_ipv6() already account= s for some of these cases and
therefore this patch copies the same logic= to add_route().

Change-Id: Id1ec0c6e4c391604ec5dbb8b7122f2e47ad186d= 1
Signed-off-by: Marco Baffo <marco@...2726...>
---
M Change= s.rst
M src/openvpn/route.c
2 files changed, 51 insertions(+), 3 dele= tions(-)

git pull ssh://gerrit.openvpn.net:29418=
/openvpn refs/changes/77/677/12
diff --git a/Changes.rst b/Changes.=
rst
index 4feacad2..a3e00c2 100644
--- a/Ch= anges.rst
+++ b/Changes.rst
@@ -170,6 +170,= 13 @@
COPYING: license details only relevant to our Window= s installers have
been updated and moved to the openvpn= -build repo

+The route gateway is now only added for IPv4 when strictly necess= ary.
+ The gateway-n= eeded logic previously applied to IPv6 only has been copied
+ and extended to IPv4 route handli= ng. Route additions now omit the
+ gateway unless required after checking device type, special= routes,
+ on-link g= ateways, and whether the gateway lies inside the VPN subnet,
+ optimizing route configuration a= nd potentially reducing redundant route
+ entries.

Deprecate= d features
-------------------
diff --git = a/src/openvpn/route.c b/src/openvpn/route.c
index 7d988da..= c249b38 100644
--- a/src/openvpn/route.c
++= + b/src/openvpn/route.c
@@ -1479,6 +1479,7 @@
{
int status =3D 0;
int is_lo= cal_route;
+ bool = gateway_needed =3D false;

if (!(r-&g= t;flags & RT_DEFINED))
{
@@ -1488,= 8 +1489,8 @@
struct argv argv =3D argv_new(); struct gc_arena gc =3D gc_new();

<= span style=3D"color: hsl(0, 100%, 40%);">-#if !defined(TARGET_LINUX)=
const char *network =3D print_in_addr_t(r->network, 0, &a= mp;gc);
+#if !defined= (TARGET_LINUX)
#if !defined(TARGET_AIX)
= const char *netmask =3D print_in_addr_t(r->netmask, 0, &gc);

#endif
@@ -1502,8 +1503,47 @@
= goto done;
}

+#ifndef _WIN32
+ /* server told us which interface t= o use; pass gateway if it gave one */
+ if (rgi && (rgi->flags & RGI_IFACE_DEF= INED) && rgi->iface[0] !=3D 0)
+ {
+ if (rgi->flags & RGI_ADDR_DEFINED && r->g= ateway !=3D 0)
+ = {
+ gat= eway_needed =3D true;
+ }
+ }
+#endif
+

+ /* TAP is an L2 device, so for destinations that a= re not explicitly
+ = * on-link the kernel needs a gateway so it can ARP/ND for the
= + * MAC address. We make an= exception only when the user explicitly sets
+ * metric 0, which is taken as a request for = an on-link =E2=80=9Croute to
+ * interface=E2=80=9D (no gateway). This keeps parity with the= IPv6 code.
+ */<= /span>
+ if (tt->type = =3D=3D DEV_TYPE_TAP
+= && !((r->flags & RT_METRIC_DEFINED) && r->= ;metric =3D=3D 0))
+ = {
+ gateway= _needed =3D true;
+ = }
+
+ if (gateway_needed && r= ->gateway =3D=3D 0)

+ msg= (M_WARN, "ROUTE WARNING: " PACKAGE_NAME " needs a gateway &q= uot;
+ = "parameter for a --route option and no default was set via "=
+ = "--route-gateway or --ifconfig option. Not installing "+ "IP= v4 route to %s/%d.",
+ network, netmask_to_netbits2(r->netmask));
<= span style=3D"color: hsl(120, 100%, 40%);">+ status =3D 0;+ goto done;
= + }
+
#if defined(TARGET_LINUX)<= /span>
- const char *iface = =3D NULL;
+ const = char *iface =3D tt->actual_name;
+ if (!gateway_needed && rgi && (rgi->= flags & RGI_IFACE_DEFINED) && rgi->iface[0] !=3D 0) /* vpn s= erver special route */
+ ifa= ce =3D rgi->iface;
+ }
+
<= span> int metric =3D -1;


if (is_= on_link(is_local_route, flags, rgi))
@@ -1518,7 +1558,8 @@<= /span>


status =3D RTA_SUC= CESS;
- int ret =3D = net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->netmask= ), &r->gateway,
+ gateway_needed ? &r->gateway : NULL= ,
iface, r->table_id, me= tric);
if (ret =3D=3D -EEXIST)
{<= /span>

To view, visit change 677. To unsubscribe, o= r for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id1ec0c6e4c391604ec5dbb8b712= 2f2e47ad186d1
Gerrit-Change-Number: 677
Gerrit-PatchSet: 12
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: cron2 <gert@...1296...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: mrbff <marco@...2726...&g= t;
= --5Te1HHLAjdc=--