Attention is currently required from: Arne Schwabe.
Hello Arne Schwabe,
I'd like you to reexamine a change. Please visit
https://gerrit.openvpn.net/c/openvpn/+/1450?usp=email
to look at the new patch set (#12).
Change subject: PUSH_UPDATE: add DCO and iroutes support on server
......................................................................
PUSH_UPDATE: add DCO and iroutes support on server
Renamed update_vhash() to update_vaddr().
Moved the -ifconfig logic from send_single_push_update() to update_vaddr().
Changed canary logic to use the current ifconfig value, if present.
Avoid re-learning unchanged addresses.
Added dco_multi_update_peer_addr() and dco_update_peer_addr() to update
the peer's virtual addresses in DCO after PUSH_UPDATE on Linux.
Added logic to reinstall DCO iroutes after changing the peer's virtual
addresses, keeping userspace iroutes.
Split dco_delete_iroutes() into _v4() and _v6() to update iroutes per
address family.
Skip DCO route installation when address learning fails, including on
client connect. Restart the affected client and report failure when
the DCO peer address update fails. Skip PUSH_UPDATE for clients with
a scheduled exit.
Change-Id: I88f37e47026a7d7ea0f7c6bde1d79d32c14e5297
Signed-off-by: Marco Baffo <[email protected]>
---
M src/openvpn/dco.c
M src/openvpn/dco.h
M src/openvpn/dco_freebsd.c
M src/openvpn/dco_linux.c
M src/openvpn/dco_win.c
M src/openvpn/dco_win.h
M src/openvpn/forward.c
M src/openvpn/multi.c
M src/openvpn/multi.h
M src/openvpn/openvpn.h
M src/openvpn/push_util.c
M tests/unit_tests/openvpn/mock_push_dependencies.c
12 files changed, 416 insertions(+), 107 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/50/1450/12
diff --git a/src/openvpn/dco.c b/src/openvpn/dco.c
index f3b5d44..401ffea 100644
--- a/src/openvpn/dco.c
+++ b/src/openvpn/dco.c
@@ -676,6 +676,40 @@
return 0;
}
+int
+dco_multi_update_peer_addr(struct context *c, int flags)
+{
+ if (!flags || c->c2.tls_multi->dco_peer_id == -1)
+ {
+ return 0;
+ }
+
+ int peer_id = c->c2.tls_multi->dco_peer_id;
+
+ /* In server mode we need to fetch the remote addresses from the push
config */
+ struct in_addr vpn_ip4 = { 0 };
+ const struct in_addr *vpn_addr4 = NULL;
+ if (c->c2.push_ifconfig_defined)
+ {
+ vpn_ip4.s_addr = htonl(c->c2.push_ifconfig_local);
+ vpn_addr4 = &vpn_ip4;
+ }
+
+ const struct in6_addr *vpn_addr6 = NULL;
+ if (c->c2.push_ifconfig_ipv6_defined)
+ {
+ vpn_addr6 = &c->c2.push_ifconfig_ipv6_local;
+ }
+
+ int ret = dco_update_peer_addr(&c->c1.tuntap->dco, peer_id, vpn_addr4,
vpn_addr6, flags);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ return 0;
+}
+
void
dco_install_iroute(struct multi_context *m, struct multi_instance *mi, const
struct mroute_addr *addr)
{
@@ -706,8 +740,8 @@
if (addrtype == MR_ADDR_IPV6)
{
#if defined(_WIN32)
- dco_win_add_iroute_ipv6(&c->c1.tuntap->dco, addr->v6.addr,
addr->netbits,
- c->c2.tls_multi->rx_peer_id);
+ bool installed = dco_win_add_iroute_ipv6(&c->c1.tuntap->dco,
addr->v6.addr, addr->netbits,
+ c->c2.tls_multi->rx_peer_id);
#else
const struct in6_addr *gateway =
&mi->context.c2.push_ifconfig_ipv6_local;
if (addr->type & MR_ONLINK_DCO_ADDR)
@@ -715,16 +749,18 @@
gateway = NULL;
}
- net_route_v6_add(&m->top.net_ctx, &addr->v6.addr, addr->netbits,
- gateway, c->c1.tuntap->actual_name, 0,
- DCO_IROUTE_METRIC);
+ bool installed = net_route_v6_add(&m->top.net_ctx, &addr->v6.addr,
addr->netbits,
+ gateway, c->c1.tuntap->actual_name,
0,
+ DCO_IROUTE_METRIC)
+ == 0;
#endif
+ mi->context.did_dco_iroutes_v6 |= installed;
}
else if (addrtype == MR_ADDR_IPV4)
{
#if defined(_WIN32)
- dco_win_add_iroute_ipv4(&c->c1.tuntap->dco, addr->v4.addr,
addr->netbits,
- c->c2.tls_multi->rx_peer_id);
+ bool installed = dco_win_add_iroute_ipv4(&c->c1.tuntap->dco,
addr->v4.addr, addr->netbits,
+ c->c2.tls_multi->rx_peer_id);
#else
in_addr_t dest = htonl(addr->v4.addr);
const in_addr_t *gateway = &mi->context.c2.push_ifconfig_local;
@@ -733,15 +769,17 @@
gateway = NULL;
}
- net_route_v4_add(&m->top.net_ctx, &dest, addr->netbits, gateway,
- c->c1.tuntap->actual_name, 0, DCO_IROUTE_METRIC);
+ bool installed = net_route_v4_add(&m->top.net_ctx, &dest,
addr->netbits, gateway,
+ c->c1.tuntap->actual_name, 0,
DCO_IROUTE_METRIC)
+ == 0;
#endif
+ mi->context.did_dco_iroutes_v4 |= installed;
}
#endif /* if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) ||
defined(_WIN32) */
}
void
-dco_delete_iroutes(openvpn_net_ctx_t *net_ctx, const struct context *c)
+dco_delete_iroutes_v4(openvpn_net_ctx_t *net_ctx, const struct context *c)
{
#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_WIN32)
if (!dco_enabled(&c->options))
@@ -750,7 +788,7 @@
}
ASSERT(TUNNEL_TYPE(c->c1.tuntap) == DEV_TYPE_TUN);
- msg(D_DCO, "DCO: attempt removing iroutes from system table");
+ msg(D_DCO, "DCO: attempt removing IPv4 iroutes from system table");
if (c->c2.push_ifconfig_defined)
{
@@ -778,6 +816,20 @@
}
#endif
}
+#endif /* if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) ||
defined(_WIN32) */
+}
+
+void
+dco_delete_iroutes_v6(openvpn_net_ctx_t *net_ctx, const struct context *c)
+{
+#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_WIN32)
+ if (!dco_enabled(&c->options))
+ {
+ return;
+ }
+ ASSERT(TUNNEL_TYPE(c->c1.tuntap) == DEV_TYPE_TUN);
+
+ msg(D_DCO, "DCO: attempt removing IPv6 iroutes from system table");
if (c->c2.push_ifconfig_ipv6_defined)
{
@@ -807,4 +859,11 @@
#endif /* if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) ||
defined(_WIN32) */
}
+void
+dco_delete_iroutes(openvpn_net_ctx_t *net_ctx, const struct context *c)
+{
+ dco_delete_iroutes_v4(net_ctx, c);
+ dco_delete_iroutes_v6(net_ctx, c);
+}
+
#endif /* defined(ENABLE_DCO) */
diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h
index c435f8c..3093ff5 100644
--- a/src/openvpn/dco.h
+++ b/src/openvpn/dco.h
@@ -191,6 +191,31 @@
int keepalive_timeout, int mss);
/**
+ * Update the peer's virtual address(es) in the DCO to the current value(s)
+ *
+ * @param dco DCO device context
+ * @param peerid the ID of the peer to be modified
+ * @param vpn_ipv4 new IPv4 or NULL to remove the outdated one, only used
when `(1 << AF_INET)` flag is present
+ * @param vpn_ipv6 new IPv6 or NULL to remove the outdated one, only used
when `(1 << AF_INET6)` flag is present
+ * @param flags `(1 << AF_INET)`, `(1 << AF_INET6)` or `(1 << AF_INET) |
(1 << AF_INET6)` depending on which address has changed
+ * @return int the DCO message status on success or a negative error code
otherwise
+ */
+int
+dco_update_peer_addr(dco_context_t *dco, unsigned int peerid,
+ const struct in_addr *vpn_ipv4, const struct in6_addr
*vpn_ipv6,
+ int flags);
+
+/**
+ * Update the peer's virtual address(es) in the DCO to the current value(s)
+ *
+ * @param c peer context
+ * @param flags `(1 << AF_INET)`, `(1 << AF_INET6)` or `(1 << AF_INET) |
(1 << AF_INET6)` depending on which address has changed
+ * @return int 0 on success or a negative error code otherwise
+ */
+int
+dco_multi_update_peer_addr(struct context *c, int flags);
+
+/**
* Remove a peer from DCO
*
* @param c the main instance context of the peer to remove
@@ -218,6 +243,22 @@
const struct mroute_addr *addr);
/**
+ * Remove all IPv4 routes added through the specified client
+ *
+ * @param net_ctx the iface networking context
+ * @param c the client context for which routes have to be removed
+ */
+void dco_delete_iroutes_v4(openvpn_net_ctx_t *net_ctx, const struct context
*c);
+
+/**
+ * Remove all IPv6 routes added through the specified client
+ *
+ * @param net_ctx the iface networking context
+ * @param c the client context for which routes have to be removed
+ */
+void dco_delete_iroutes_v6(openvpn_net_ctx_t *net_ctx, const struct context
*c);
+
+/**
* Remove all routes added through the specified client
*
* @param net_ctx the iface networking context
@@ -344,6 +385,20 @@
return 0;
}
+static inline int
+dco_update_peer_addr(dco_context_t *dco, unsigned int peerid,
+ const struct in_addr *vpn_ipv4, const struct in6_addr
*vpn_ipv6,
+ int flags)
+{
+ return 0;
+}
+
+static inline int
+dco_multi_update_peer_addr(struct context *c, int flags)
+{
+ return 0;
+}
+
static inline void
dco_remove_peer(struct context *c)
{
@@ -356,7 +411,17 @@
}
static inline void
-dco_install_iroute(struct multi_context *m, struct multi_instance *mi, struct
mroute_addr *addr)
+dco_install_iroute(struct multi_context *m, struct multi_instance *mi, const
struct mroute_addr *addr)
+{
+}
+
+static inline void
+dco_delete_iroutes_v4(openvpn_net_ctx_t *net_ctx, const struct context *c)
+{
+}
+
+static inline void
+dco_delete_iroutes_v6(openvpn_net_ctx_t *net_ctx, const struct context *c)
{
}
diff --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c
index 3148f10..d85104a 100644
--- a/src/openvpn/dco_freebsd.c
+++ b/src/openvpn/dco_freebsd.c
@@ -559,6 +559,12 @@
return ret;
}
+int
+dco_update_peer_addr(dco_context_t *dco, unsigned int peerid, const struct
in_addr *vpn_ipv4, const struct in6_addr *vpn_ipv6, int flags)
+{
+ return 0;
+}
+
static void
dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t
*nvl)
{
diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c
index 4412ad0..2c19b83 100644
--- a/src/openvpn/dco_linux.c
+++ b/src/openvpn/dco_linux.c
@@ -724,6 +724,50 @@
return ret;
}
+int
+dco_update_peer_addr(dco_context_t *dco, unsigned int peerid, const struct
in_addr *vpn_ipv4, const struct in6_addr *vpn_ipv6, int flags)
+{
+ msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peerid);
+
+ if (!flags)
+ {
+ return 0;
+ }
+
+ struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_SET);
+ if (!nl_msg)
+ {
+ return -ENOMEM;
+ }
+
+ struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER);
+ int ret = -EMSGSIZE;
+ NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid);
+
+ if (flags & (1 << AF_INET))
+ {
+ NLA_PUT_U32(nl_msg, OVPN_A_PEER_VPN_IPV4, vpn_ipv4 ? vpn_ipv4->s_addr
: 0);
+ }
+
+ if (flags & (1 << AF_INET6))
+ {
+ struct in6_addr addr6;
+ if (!vpn_ipv6)
+ {
+ CLEAR(addr6);
+ vpn_ipv6 = &addr6;
+ }
+ NLA_PUT(nl_msg, OVPN_A_PEER_VPN_IPV6, sizeof(struct in6_addr),
vpn_ipv6);
+ }
+
+ nla_nest_end(nl_msg, attr);
+ ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
+
+nla_put_failure:
+ nlmsg_free(nl_msg);
+ return ret;
+}
+
/* This function parses the reply provided by the kernel to the
CTRL_CMD_GETFAMILY
* message. We parse the reply and we retrieve the multicast group ID
associated
* with the "ovpn-dco" netlink family.
diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c
index 12b0666..8f7c8f62 100644
--- a/src/openvpn/dco_win.c
+++ b/src/openvpn/dco_win.c
@@ -519,6 +519,12 @@
}
int
+dco_update_peer_addr(dco_context_t *dco, unsigned int peerid, const struct
in_addr *vpn_ipv4, const struct in6_addr *vpn_ipv6, int flags)
+{
+ return 0;
+}
+
+int
dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_slot_t
slot,
const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const
uint8_t *decrypt_key,
const uint8_t *decrypt_iv, const char *ciphername, bool epoch)
@@ -1007,7 +1013,7 @@
return dco_get_version(&ver) && ver.Major >= 2;
}
-void
+bool
dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int
netbits,
unsigned int peer_id)
{
@@ -1021,16 +1027,18 @@
netbits, peer_id);
DWORD bytes_returned = 0;
- if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route,
sizeof(route), NULL, 0,
- &bytes_returned, NULL))
+ bool installed = DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE,
&route, sizeof(route), NULL, 0,
+ &bytes_returned, NULL);
+ if (!installed)
{
msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE)
failed");
}
gc_free(&gc);
+ return installed;
}
-void
+bool
dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned int
netbits,
unsigned int peer_id)
{
@@ -1042,13 +1050,15 @@
netbits, peer_id);
DWORD bytes_returned = 0;
- if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE, &route,
sizeof(route), NULL, 0,
- &bytes_returned, NULL))
+ bool installed = DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_ADD_IROUTE,
&route, sizeof(route), NULL, 0,
+ &bytes_returned, NULL);
+ if (!installed)
{
msg(M_WARN | M_ERRNO, "DeviceIoControl(OVPN_IOCTL_MP_ADD_IROUTE)
failed");
}
gc_free(&gc);
+ return installed;
}
void
diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h
index a92e5ad..28795b7 100644
--- a/src/openvpn/dco_win.h
+++ b/src/openvpn/dco_win.h
@@ -67,10 +67,10 @@
bool dco_win_supports_multipeer(void);
-void dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int
netbits,
+bool dco_win_add_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int
netbits,
unsigned int peer_id);
-void dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned
int netbits,
+bool dco_win_add_iroute_ipv6(dco_context_t *dco, struct in6_addr dst, unsigned
int netbits,
unsigned int peer_id);
void dco_win_del_iroute_ipv4(dco_context_t *dco, in_addr_t dst, unsigned int
netbits);
diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
index 681210d..7b6f4f8 100644
--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -550,9 +550,10 @@
* last connecting client claiming the iroutes takes ownership. Therefore
* they are not removed during delayed cleanup.
*/
- if (c->did_dco_iroutes)
+ if (c->did_dco_iroutes_v4 || c->did_dco_iroutes_v6)
{
- c->did_dco_iroutes = false;
+ c->did_dco_iroutes_v4 = false;
+ c->did_dco_iroutes_v6 = false;
dco_delete_iroutes(&c->net_ctx, c);
}
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index e302a81..d1b3d16 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -478,6 +478,28 @@
set_prefix(mi);
}
+static void
+multi_del_dco_iroutes_v4(struct multi_context *m, struct multi_instance *mi)
+{
+ /* Skip DCO IPv4 iroutes that have already been removed. */
+ if (mi->context.did_dco_iroutes_v4)
+ {
+ mi->context.did_dco_iroutes_v4 = false;
+ dco_delete_iroutes_v4(&m->top.net_ctx, &mi->context);
+ }
+}
+
+static void
+multi_del_dco_iroutes_v6(struct multi_context *m, struct multi_instance *mi)
+{
+ /* Skip DCO IPv6 iroutes that have already been removed. */
+ if (mi->context.did_dco_iroutes_v6)
+ {
+ mi->context.did_dco_iroutes_v6 = false;
+ dco_delete_iroutes_v6(&m->top.net_ctx, &mi->context);
+ }
+}
+
/*
* Tell the route helper about deleted iroutes so
* that it can update its mask of currently used
@@ -489,12 +511,8 @@
const struct iroute *ir;
const struct iroute_ipv6 *ir6;
- /* check if DCO iroutes were already removed when scheduling a delayed
exit */
- if (mi->context.did_dco_iroutes)
- {
- mi->context.did_dco_iroutes = false;
- dco_delete_iroutes(&m->top.net_ctx, &mi->context);
- }
+ multi_del_dco_iroutes_v4(m, mi);
+ multi_del_dco_iroutes_v6(m, mi);
if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
{
@@ -1211,8 +1229,12 @@
}
struct multi_instance *owner = multi_learn_addr(m, mi, &addr,
MULTI_ROUTE_PERMANENT);
+ if (owner != mi)
+ {
+ return owner;
+ }
#ifdef ENABLE_MANAGEMENT
- if (management && owner)
+ if (management)
{
management_learn_addr(&mi->context.c2.mda_context, &addr, primary);
}
@@ -1256,8 +1278,12 @@
}
struct multi_instance *owner = multi_learn_addr(m, mi, &addr,
MULTI_ROUTE_PERMANENT);
+ if (owner != mi)
+ {
+ return owner;
+ }
#ifdef ENABLE_MANAGEMENT
- if (management && owner)
+ if (management)
{
management_learn_addr(&mi->context.c2.mda_context, &addr, primary);
}
@@ -1297,8 +1323,6 @@
if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
{
mi->did_iroutes = true;
- /* multi_learn_in{6}_addr_t takes care of installing the DCO iroute */
- mi->context.did_dco_iroutes = true;
for (ir = mi->context.options.iroutes; ir != NULL; ir = ir->next)
{
if (ir->netbits >= 0)
@@ -4409,9 +4433,10 @@
}
/* Function to unlearn previous ifconfig of a client in the server
multi_context after a PUSH_UPDATE */
-void
+static void
unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
{
+ multi_del_dco_iroutes_v4(m, mi);
in_addr_t old_addr = 0;
old_addr = htonl(mi->context.c2.push_ifconfig_local);
multi_unlearn_in_addr_t(m, mi, old_addr);
@@ -4421,9 +4446,10 @@
}
/* Function to unlearn previous ifconfig-ipv6 of a client in the server
multi_context after a PUSH_UPDATE */
-void
+static void
unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
{
+ multi_del_dco_iroutes_v6(m, mi);
struct in6_addr old_addr6;
CLEAR(old_addr6);
old_addr6 = mi->context.c2.push_ifconfig_ipv6_local;
@@ -4433,60 +4459,162 @@
CLEAR(mi->reporting_addr_ipv6);
}
+/* Reinstall only routes that are still owned by this client. */
+static void
+multi_reinstall_owned_dco_iroute(struct multi_context *m, struct
multi_instance *mi,
+ const struct mroute_addr *addr)
+{
+ const struct multi_route *route = hash_lookup(m->vhash, addr);
+ if (route && route->instance == mi && multi_route_defined(m, route))
+ {
+ dco_install_iroute(m, mi, addr);
+ }
+}
+
+/* Userspace iroutes do not depend on the client's VPN address. Only their
+ * DCO routes need to be reinstalled when the gateway changes. */
+static void
+multi_reinstall_dco_iroutes(struct multi_context *m, struct multi_instance
*mi, int flags)
+{
+ if ((flags & (1 << AF_INET)) && mi->context.c2.push_ifconfig_defined)
+ {
+ for (const struct iroute *ir = mi->context.options.iroutes; ir; ir =
ir->next)
+ {
+ struct mroute_addr addr = { 0 };
+ mroute_extract_in_addr_t(&addr, ir->network);
+ addr.type |= MR_WITH_NETBITS;
+ addr.netbits = (uint8_t)ir->netbits;
+ multi_reinstall_owned_dco_iroute(m, mi, &addr);
+ }
+ }
+
+ if ((flags & (1 << AF_INET6)) && mi->context.c2.push_ifconfig_ipv6_defined)
+ {
+ for (const struct iroute_ipv6 *ir6 = mi->context.options.iroutes_ipv6;
ir6; ir6 = ir6->next)
+ {
+ struct mroute_addr addr = { 0 };
+ addr.type = MR_ADDR_IPV6 | MR_WITH_NETBITS;
+ addr.len = 16;
+ addr.netbits = (uint8_t)ir6->netbits;
+ addr.v6.addr = ir6->network;
+ mroute_addr_mask_host_bits(&addr);
+ multi_reinstall_owned_dco_iroute(m, mi, &addr);
+ }
+ }
+}
+
/**
- * Update the vhash with new IP/IPv6 addresses in the multi_context when a
- * push-update message containing ifconfig/ifconfig-ipv6 options is sent
- * from the server.
+ * Update the vhash and DCO peer addresses when a push-update message
containing
+ * ifconfig/ifconfig-ipv6 options is sent from the server. Preserve userspace
+ * iroutes and reinstall their DCO routes with the new IP if needed.
*
* @param m The multi_context
* @param mi The multi_instance of the client we are updating
- * @param new_ip The new IPv4 address or NULL if no change
- * @param new_ipv6 The new IPv6 address or NULL if no change
+ * @param new_ip The new IPv4 address, NULL for -ifconfig or the current
IPv4 if no change
+ * @param new_ipv6 The new IPv6 address, NULL for -ifconfig-ipv6 or the
current IPv6 if no change
*/
void
-update_vhash(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6)
+update_vaddr(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6)
{
+ /* Track changed address families to avoid resetting unchanged DCO
addresses. */
+ int update_flags = 0;
+
+ /* -ifconfig */
+ if (!new_ip && mi->context.c2.push_ifconfig_defined)
+ {
+ unlearn_ifconfig(m, mi);
+ update_flags |= (1 << AF_INET);
+ }
+ /* -ifconfig-ipv6 */
+ if (!new_ipv6 && mi->context.c2.push_ifconfig_ipv6_defined)
+ {
+ unlearn_ifconfig_ipv6(m, mi);
+ update_flags |= (1 << AF_INET6);
+ }
+
if (new_ip)
{
- /* Remove old IP */
- if (mi->context.c2.push_ifconfig_defined)
- {
- unlearn_ifconfig(m, mi);
- }
-
- /* Add new IP */
struct in_addr new_addr;
- CLEAR(new_addr);
if (inet_pton(AF_INET, new_ip, &new_addr) == 1
- && multi_learn_in_addr_t(m, mi, ntohl(new_addr.s_addr), -1, true))
+ && (!mi->context.c2.push_ifconfig_defined
+ || ntohl(new_addr.s_addr) !=
mi->context.c2.push_ifconfig_local))
{
+ /* Remove old IP */
+ if (mi->context.c2.push_ifconfig_defined)
+ {
+ unlearn_ifconfig(m, mi);
+ update_flags |= (1 << AF_INET);
+ }
+
+ /* DCO needs the new address while installing its on-link route. */
mi->context.c2.push_ifconfig_defined = true;
mi->context.c2.push_ifconfig_local = ntohl(new_addr.s_addr);
- /* set our client's VPN endpoint for status reporting purposes */
- mi->reporting_addr = mi->context.c2.push_ifconfig_local;
+ if (multi_learn_in_addr_t(m, mi,
mi->context.c2.push_ifconfig_local, -1, true) == mi)
+ {
+ /* set our client's VPN endpoint for status reporting purposes
*/
+ mi->reporting_addr = mi->context.c2.push_ifconfig_local;
+ update_flags |= (1 << AF_INET);
+ }
+ else
+ {
+ mi->context.c2.push_ifconfig_defined = false;
+ mi->context.c2.push_ifconfig_local = 0;
+ }
}
}
if (new_ipv6)
{
- /* Remove old IPv6 */
- if (mi->context.c2.push_ifconfig_ipv6_defined)
- {
- unlearn_ifconfig_ipv6(m, mi);
- }
-
- /* Add new IPv6 */
struct in6_addr new_addr6;
- CLEAR(new_addr6);
if (inet_pton(AF_INET6, new_ipv6, &new_addr6) == 1
- && multi_learn_in6_addr(m, mi, new_addr6, -1, true))
+ && (!mi->context.c2.push_ifconfig_ipv6_defined
+ || memcmp(&new_addr6,
&mi->context.c2.push_ifconfig_ipv6_local, sizeof(new_addr6))))
{
+ /* Remove old IPv6 */
+ if (mi->context.c2.push_ifconfig_ipv6_defined)
+ {
+ unlearn_ifconfig_ipv6(m, mi);
+ update_flags |= (1 << AF_INET6);
+ }
+
+ /* DCO needs the new address while installing its on-link route. */
mi->context.c2.push_ifconfig_ipv6_defined = true;
mi->context.c2.push_ifconfig_ipv6_local = new_addr6;
- /* set our client's VPN endpoint for status reporting purposes */
- mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
+ if (multi_learn_in6_addr(m, mi, new_addr6, -1, true) == mi)
+ {
+ /* set our client's VPN endpoint for status reporting purposes
*/
+ mi->reporting_addr_ipv6 =
mi->context.c2.push_ifconfig_ipv6_local;
+ update_flags |= (1 << AF_INET6);
+ }
+ else
+ {
+ mi->context.c2.push_ifconfig_ipv6_defined = false;
+ CLEAR(mi->context.c2.push_ifconfig_ipv6_local);
+ }
}
}
+
+ if (!update_flags || !dco_enabled(&m->top.options))
+ {
+ return;
+ }
+
+#if defined(TARGET_LINUX)
+ int ret = dco_multi_update_peer_addr(&mi->context, update_flags);
+ if (ret < 0)
+ {
+ msg(M_WARN, "MULTI: failed to update DCO peer addresses (error %d)",
ret);
+ /* PUSH_UPDATE is already queued, so reconnect to restore consistent
state. */
+ send_restart(&mi->context, NULL);
+ multi_schedule_context_wakeup(m, mi);
+ return;
+ }
+#endif
+
+ if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
+ {
+ multi_reinstall_dco_iroutes(m, mi, update_flags);
+ }
}
bool
diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
index 5db7147..010fb9b 100644
--- a/src/openvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -730,8 +730,6 @@
#endif
void
-update_vhash(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6);
-void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi);
-void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi);
+update_vaddr(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6);
#endif /* MULTI_H */
diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h
index cdd02b6..72436fe 100644
--- a/src/openvpn/openvpn.h
+++ b/src/openvpn/openvpn.h
@@ -507,7 +507,8 @@
bool did_we_daemonize; /**< Whether demonization has already
* taken place. */
- bool did_dco_iroutes; /**< Whether DCO iroutes have been installed
*/
+ bool did_dco_iroutes_v4; /**< Whether DCO IPv4 iroutes have been
installed */
+ bool did_dco_iroutes_v6; /**< Whether DCO IPv6 iroutes have been
installed */
struct context_persist persist;
/**< Persistent %context. */
diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c
index 34cb26c..7491880 100644
--- a/src/openvpn/push_util.c
+++ b/src/openvpn/push_util.c
@@ -149,14 +149,35 @@
struct options o;
CLEAR(o);
- /* Set canary values to detect ifconfig options in push-update messages.
- * These placeholder strings will be overwritten to NULL by the option
- * parser if -ifconfig or -ifconfig-ipv6 options are present in the
- * push-update.
+ /* Set canary values to detect -ifconfig and -ifconfig-ipv6 options in
+ * push-update messages. These placeholder strings will be overwritten
+ * to NULL by the option parser if -ifconfig or -ifconfig-ipv6 options
+ * are present in the push-update, to the new value if present, or
+ * they will remain equal to the current values otherwise.
+ * If the current values are not defined they will remain NULL.
*/
- const char *canary = "canary";
- o.ifconfig_local = canary;
- o.ifconfig_ipv6_local = canary;
+
+ char ifconfig_local[INET_ADDRSTRLEN];
+ struct in_addr addr4 = { .s_addr =
htonl(mi->context.c2.push_ifconfig_local) };
+ if (mi->context.c2.push_ifconfig_defined)
+ {
+ o.ifconfig_local = inet_ntop(AF_INET, &addr4, ifconfig_local,
sizeof(ifconfig_local));
+ if (!o.ifconfig_local)
+ {
+ return false;
+ }
+ }
+
+ char ifconfig_ipv6_local[INET6_ADDRSTRLEN];
+ if (mi->context.c2.push_ifconfig_ipv6_defined)
+ {
+ o.ifconfig_ipv6_local = inet_ntop(AF_INET6,
&mi->context.c2.push_ifconfig_ipv6_local,
+ ifconfig_ipv6_local,
sizeof(ifconfig_ipv6_local));
+ if (!o.ifconfig_ipv6_local)
+ {
+ return false;
+ }
+ }
struct buffer_entry *e = msgs->head;
while (e)
@@ -194,28 +215,9 @@
if (option_types_found & OPT_P_UP)
{
- /* -ifconfig */
- if (!o.ifconfig_local && mi->context.c2.push_ifconfig_defined)
- {
- unlearn_ifconfig(m, mi);
- }
- /* -ifconfig-ipv6 */
- if (!o.ifconfig_ipv6_local &&
mi->context.c2.push_ifconfig_ipv6_defined)
- {
- unlearn_ifconfig_ipv6(m, mi);
- }
-
- if (o.ifconfig_local && !strcmp(o.ifconfig_local, canary))
- {
- o.ifconfig_local = NULL;
- }
- if (o.ifconfig_ipv6_local && !strcmp(o.ifconfig_ipv6_local, canary))
- {
- o.ifconfig_ipv6_local = NULL;
- }
-
- /* new ifconfig or new ifconfig-ipv6 */
- update_vhash(m, mi, o.ifconfig_local, o.ifconfig_ipv6_local);
+ /* ifconfig or ifconfig-ipv6 */
+ update_vaddr(m, mi, o.ifconfig_local, o.ifconfig_ipv6_local);
+ return !event_timeout_defined(&c->c2.scheduled_exit);
}
uninit_options(&o);
@@ -253,12 +255,14 @@
static int
send_push_update(struct multi_context *m, const void *target, const char *msg,
const push_update_type type, const size_t push_bundle_size)
{
+#if !defined(TARGET_LINUX)
if (dco_enabled(&m->top.options))
{
msg(M_WARN, "WARN: PUSH_UPDATE messages cannot currently be sent while
DCO is enabled."
" To send a PUSH_UPDATE message, be sure to use the
--disable-dco option.");
return 0;
}
+#endif
if (!msg || !*msg || !m || (!target && type != UPT_BROADCAST))
{
@@ -301,6 +305,7 @@
}
if (!mi->halt
+ && !event_timeout_defined(&mi->context.c2.scheduled_exit)
&& send_single_push_update(m, mi, msgs))
{
buffer_list_free(msgs);
@@ -321,7 +326,9 @@
{
struct multi_instance *curr_mi = m->instances[i];
- if (!curr_mi || curr_mi->halt || !support_push_update(curr_mi))
+ if (!curr_mi || curr_mi->halt
+ || event_timeout_defined(&curr_mi->context.c2.scheduled_exit)
+ || !support_push_update(curr_mi))
{
continue;
}
diff --git a/tests/unit_tests/openvpn/mock_push_dependencies.c
b/tests/unit_tests/openvpn/mock_push_dependencies.c
index bf2ccf3..c951a8f 100644
--- a/tests/unit_tests/openvpn/mock_push_dependencies.c
+++ b/tests/unit_tests/openvpn/mock_push_dependencies.c
@@ -48,17 +48,7 @@
}
void
-unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
-{
-}
-
-void
-unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
-{
-}
-
-void
-update_vhash(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6)
+update_vaddr(struct multi_context *m, struct multi_instance *mi, const char
*new_ip, const char *new_ipv6)
{
}
--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1450?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I88f37e47026a7d7ea0f7c6bde1d79d32c14e5297
Gerrit-Change-Number: 1450
Gerrit-PatchSet: 12
Gerrit-Owner: Marco Baffo <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel