From: "mrbff (Code Review)" Attention is currently required from: flichtenheld, plaisthos. mrbff uploaded patch set #2 to this change.redirect-gateway: add route toward new remote host when using persist-tun=
When both --redirect-gateway and --persist-tun are used together,a route must be added for each new remote host we attempt to connect to.<=
br>
Change-Id: Ic7b486ccb85df7fc1d6a573ac1315d235728822c
Signed-off-b=
y: Marco Baffo <marco@...2726...>
---
M src/openvpn/init.c
M=
src/openvpn/route.c
M src/openvpn/route.h
3 files changed, 187 inser=
tions(+), 22 deletions(-)git pull ssh://gerrit.openvpn.net:29418=
/openvpn refs/changes/01/901/2
diff --git a/src/openvpn/init.c b/sr=
c/openvpn/init.c
index 40ae2c8..e79b777 100644--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -2101,6 +2101,10 @@
=
signal_description(c->sig->signal_received, c->sig->signal_text=
),
"route-pre-down", c-&=
gt;c2.es);
+ if (c->mode =3D=3D CM_P2P && c->options.=
persist_tun)
+ =
{
+ =
del_route_towards_remote(c);
+ }
delete_routes(c->=
c1.route_list, c->c1.route_ipv6_list, c->c1.tuntap,
=
ROUTE_OPTION_FLAGS(&c->options), c->c2.=
es, &c->net_ctx);
}
@@ -214=
5,6 +2149,11 @@
c->c2.es);
}
+ if (c->mode =3D=3D CM_P2P && c->=
options.persist_tun)
=
+ {
+ =
del_route_towards_remote(c);
+ }
+
del_wfp_block(c, adapter_index);
gc_free(&gc);
@@ -4714,6 =
+4723,17 @@
}
}
+ /**<=
br>+ * When using both --re=
direct-gateway and --persist-tun,
+ * if the connection to the server is lost, a /32 (or /12=
8 if IPv6) route must be added
+ * to ensure connectivity to the next remote.
+ if (c->mode =3D=3D CM_P2P
+ {=
span>
+ add_route_tow=
ards_remote(c);
+ =
}
+
=
/*
* Actually do UID/GID downgrade, and chroot, i=
f requested.
* May be delayed by --client, --pull, or=
--up-delay.
diff --git a/src/openvpn/route.c b/src/openvpn=
/route.c
index e504485..1f43d5a 100644
--- =
a/src/openvpn/route.c
+++ b/src/openvpn/route.c
<=
span>@@ -42,6 +42,7 @@
#include "options.h"
#include "networking.h"
#include &q=
uot;integer.h"
+=
#include "openvpn.h"
#include =
"memdbg.h"
@@ -844,30 +845,14 @=
@
}
/* add VPN =
server host route if needed */
- if (need_remote_ipv6_route)
+ if (need_remote_ipv6_route
+ && !(rl6->iflags &=
RL_DID_LOCAL))
{
if ((rl6-&g=
t;rgi6.flags & (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
=
=3D=3D (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
{
- =
struct route_ipv6 *r6;
- ALLOC_OBJ_CLEAR_GC(r6, struct route_ipv6, &rl6->gc);<=
/span>
-
- r6->network =3D *remote_host=
_ipv6;
- r6-=
>netbits =3D 128;
- =
if (!(rl6->rgi6.flags & RGI_ON_LINK))
- {
- r6->gateway =3D rl6->rgi6.g=
ateway.addr_ipv6;
- =
}
- =
r6->metric =3D 1;
-#=
ifdef _WIN32
- =
r6->adapter_index =3D rl6->rgi6.adapter_index;
-#else
- r6->iface =3D rl6->rgi6.iface;-#endif
- r6->flags =3D RT_DEFINED | R=
T_METRIC_DEFINED;
-
- r6->next =
=3D rl6->routes_ipv6;
- rl6->routes_ipv6 =3D r6;
+ rl6->routes_ipv6 =3D create_host_rout=
e_ipv6(*remote_host_ipv6, rl6);
+ rl6->iflags |=3D RL_DID_LOCAL;
=
}
else
{@@ -880,6 +865,30 @@
return ret;
+struct route_ipv6 *
+create_host_route_ipv6(struct in6_addr remote_host_ipv6, struct route_i=
pv6_list *rl6)
+{
+ struct route_ipv6 *=
r6;
+ ALLOC_OBJ_CL=
EAR_GC(r6, struct route_ipv6, &rl6->gc);
+
+ r6->network =3D remote_host_ipv6;
+ r6->netbits =3D 128;
+ if (!(rl6->rgi6.flags & RG=
I_ON_LINK))
+ {
+ r6->gateway=
=3D rl6->rgi6.gateway.addr_ipv6;
+ }
+ r6->metric =3D 1;
+#ifdef _WIN32
+ r6->adapter_index =3D rl6->rgi6.adapter_index;
+#else
+ r6->iface =3D rl6->rgi6.iface;
+#endif
+ r6->flags =3D RT_DEFINED | RT_METR=
IC_DEFINED;
+ r6-&=
gt;next =3D rl6->routes_ipv6;
+
+ re=
turn r6;
+}
+
static bool=
add_route3(in_addr_t network, in_addr_t netmask, in_addr_=
t gateway, const struct tuntap *tt,
unsigned in=
t flags, const struct route_gateway_info *rgi, const struct env_set *es,
@@ -992,7 +1001,8 @@
/* if r=
emote_host is not ipv4 (ie: ipv6), just skip
=
* adding this special /32 route */
if =
((rl->spec.flags & RTSA_REMOTE_HOST)
- && rl->spec.remote_hos=
t !=3D IPV4_INVALID_ADDR)
+ && rl->spec.remote_host !=3D IPV4_INVA=
LID_ADDR
+ =
&& !(rl->iflags & RL_DID_LOCAL))
=
{
ret =3D add_route3(rl=
->spec.remote_host, IPV4_NETMASK_HOST, rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW, &am=
p;rl->rgi, es, ctx);
@@ -1203,7 +1213,7 @@
delete_route_ipv6(r6, tt, es, ct=
x);
}
- rl6->iflags &=3D ~RL_ROUTES_ADDED;
+ rl6->iflags &=3D ~(RL=
_ROUTES_ADDED | RL_DID_LOCAL);
}
if (rl6)
@@ -1212,6 +1222,130 @@ }
}
+void
+add_route_towards_remote(struct context *c)+{
+ ASSERT(c->c1.link_socket_addrs);
=
+
+ int current_remote_family =3D c->c1.link_s=
ocket_addrs[0].actual.dest.addr.sa.sa_family;
+
+ if (current_remote_family =3D=3D AF_INET && c->c1.rou=
te_list
+ &=
;& (c->c1.route_list->flags & RG_REROUTE_GW))
+ {
+ if (c->c1.route_list->iflags & =
RL_DID_LOCAL)
+ =
{
+ del_=
route_towards_remote(c);
+ }
+
+ in_addr_t curren=
t_remote =3D ntohl(c->c1.link_socket_addrs[0].actual.dest.addr.in4.sin_a=
ddr.s_addr);
+=
+ if (add_route3(cur=
rent_remote,
+ =
IPV4_NETMASK_HOST,
+ c->c1.route_list->rgi.gateway.a=
ddr,
+ =
c->c1.tuntap,
+ ROUTE_OPTION_FLAGS(&c->options) | ROUTE_=
REF_GW,
+ =
&c->c1.route_list->rgi,
+ c->c2.es,
+ {<=
/span>
+ c->c1=
.route_list->iflags |=3D RL_DID_LOCAL;
+ c->c1.route_list->spec.remote_host =
=3D current_remote;
+=
}
+ }
+ else if (current_rem=
ote_family =3D=3D AF_INET6 && c->c1.route_ipv6_list+ && (c->c=
1.route_ipv6_list->flags & RG_REROUTE_GW))
+ {
+ if (c->c1.route_ipv6_list->iflags & RL_DI=
D_LOCAL)
+ {=
span>
+ del_route=
_towards_remote(c);
+=
}
+
+ const struct in6_addr=
*remote_host_ipv6 =3D &(c->c1.link_socket_addrs[0].actual.dest.addr=
.in6.sin6_addr);
+ =
struct route_ipv6_list *rl6 =3D c->c1.route_ipv6_list;+
+ if ((rl6->rgi6.flags & (RGI_ADDR_DEF=
INED | RGI_IFACE_DEFINED)) =3D=3D (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))=
span>
+ {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ struct route_ipv6 *=
r6 =3D create_host_route_ipv6(*remote_host_ipv6, rl6);
+
+ if (add_route_ipv6(r6, c->c1.tuntap,<=
br>+ =
ROUTE_OPTION_FLAGS(&c->options), c->c2.es, &c->net_ctx=
))
+ {
+ rl6->=
;routes_ipv6 =3D r6;
=
+ rl6->iflags |=3D RL_DID_LOCAL;
+ }
+ }
+ else
+ {
+ =
msg(M_WARN, "ROUTE6: IPv6 route overlaps with IPv6 remote ad=
dress, but could not determine IPv6 gateway address + interface, expect fai=
lure\n");
+ =
}
+ }
+}
+
+void
+del_ro=
ute_towards_remote(struct context *c)
+{
+=
/* If the function is called from do_close_tun() it means that the sock=
et
+ * has alread=
y been closed and c->c2.link_sockets[0]->info.lsa` used in
=
+ * `add_route_towards_remo=
te()` cleaned up. So we should use
+ * `c->c1.link_socket_addrs[0]` instead.+ */
+ ASSERT(c->c1.link_socket_addrs);
+
+ int current_remote_family =3D 0;
+
+ if (c->c1.link_socket_addrs[0].act=
ual.dest.addr.sa.sa_family)
+ {
+ =
current_remote_family =3D c->c1.link_socket_addrs[0].actual.dest.addr.=
sa.sa_family;
+ }<=
/span>
+ else if (c->c=
1.link_socket_addrs[0].current_remote)
+ {
+ current_remote_family =3D c->c1.link_socket_addrs[0].curren=
t_remote->ai_family;
+ }
+
+ if (current_remote_family=
=3D=3D AF_INET && c->c1.route_list
+ && (c->c1.route_list->flag=
s & RG_REROUTE_GW)
+ && (c->c1.route_list->iflags & RL_DID_LOCAL))<=
/span>
+ {
+ IPV4_NETMASK_HOST,
+ c->c1.route_list->rgi.gat=
eway.addr,
+ =
c->c1.tuntap,
+ ROUTE_OPTION_FLAGS(&c->options) | ROUTE_RE=
F_GW,
+ =
&c->c1.route_list->rgi,
+ c->c2.es,
+ &c->net_ctx);
+ c->c1.route_l=
ist->iflags &=3D ~RL_DID_LOCAL;
+ }
+ else if (current_remote_family =3D=3D AF_INET6 && c->c1=
.route_ipv6_list
+ =
&& (c->c1.route_ipv6_list->flags & RG_REROUTE_G=
W)
+ &=
;& (c->c1.route_ipv6_list->iflags & RL_DID_LOCAL))
=
+ {
+ struct in6_addr *remote_host_ipv6 =
=3D &c->c1.route_ipv6_list->remote_host_ipv6;
+ struct route_ipv6 *host_route =
=3D create_host_route_ipv6(*remote_host_ipv6, c->c1.route_ipv6_list);
+
+ for (struct route_ipv6 *prev =3D =
NULL, *r6 =3D c->c1.route_ipv6_list->routes_ipv6; r6; r6 =3D r6->n=
ext)
+ {
+ if (memcmp(&a=
mp;r6->network, &host_route->network, sizeof(struct in6_addr))
+ || r6-=
>netbits !=3D host_route->netbits
+ || memcmp(&r6->gateway, &hos=
t_route->gateway, sizeof(struct in6_addr)))
+ {
+ continue;
+ }
+
+ delete_route_ipv6(r6, c->c1.tuntap, c->c2.es, &c-&g=
t;net_ctx);
+ =
if (!prev)
+ =
{
+ =
c->c1.route_ipv6_list->routes_ipv6 =3D r6->next;+ }
+ else
+ {
+ prev->next =3D r6->next;<=
/span>
+ }=
+ c->c1.route=
_ipv6_list->iflags &=3D ~RL_DID_LOCAL;
+ }
+ }
+}
+
#ifndef ENABLE_SMALL
static const char=
*
diff --git a/src/openvpn/route.h b/src/openvpn/route.h=
span>
index ea8b767..642ec35 100644
--- a/src/open=
vpn/route.h
+++ b/src/openvpn/route.h
@@ -3=
09,6 +309,14 @@
const struct route_gateway_=
info *rgi, const struct env_set *es,
openvp=
n_net_ctx_t *ctx);
+/* Function to add a route towards the new remote when usi=
ng
+ * `--redirect-ga=
teway` and `--persist-tun` options together. */
+void add_route_towards_remote(struct context *c=
);
+
+/* Function to delete the route towa=
rds the remote when using
+ * `--redirect-gateway` and `--persist-tun` options together. */
+void del_route_towards_r=
emote(struct context *c);
+
void add_route_to_option_list(struct route_option_l=
ist *l, const char *network, const char *netmask,
=
const char *gateway, const char *metric, int table_id=
);
@@ -359,6 +367,9 @@
v=
oid print_default_gateway(const int msglevel, const struct route_gateway_in=
fo *rgi,
const struct route_ipv=
6_gateway_info *rgi6);
+/* Return a `route_ipv6 *` /128 IPv6 route toward the =
remote host */
+struc=
t route_ipv6 *create_host_route_ipv6(struct in6_addr remote_host_ipv6, stru=
ct route_ipv6_list *rl6);
+
/*
* Test if addr is reachable vi=
a a local interface (return ILA_LOCAL),
* or if it needs =
to be routed via the default gateway (return
To view, visit change 901. To unsubscribe, or for help writing mail filter= s, visit settings.