From: "mrbff (Code Review)" Date: Tue, 10 Mar 2026 08:05:22 +0000 In-Reply-To: References: Reply-To: marco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="x568nD5v8i8="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: PUSH_UPDATE: add DCO and iroutes support on server List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --x568nD5v8i8= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1450?usp=3Demail to look at the new patch set (#6). 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 value of ifconfig (if present) as canary. Added logic in the update_vaddr() to not unlearn and re-learn the s= ame address. Added logic to update the peer's virtual address(es) in the DCO to the curr= ent value(s) after sending a PUSH_UPDATE message on Linux. Added dco_multi_update_peer_addr() and dco_update_peer_addr() to do so. Added logic to update iroutes after changing the peer's virtual address(es). Divided dco_delete_iroutes() in _v4() and _v6() to better updating iroutes. Change-Id: I88f37e47026a7d7ea0f7c6bde1d79d32c14e5297 Signed-off-by: Marco Baffo --- 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/multi.c M src/openvpn/multi.h M src/openvpn/push_util.c M tests/unit_tests/openvpn/test_push_update_msg.c 9 files changed, 379 insertions(+), 142 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/50/1450/6 diff --git a/src/openvpn/dco.c b/src/openvpn/dco.c index 26b8645..70aaf90 100644 --- a/src/openvpn/dco.c +++ b/src/openvpn/dco.c @@ -658,6 +658,41 @@ return 0; } +int +dco_multi_update_peer_addr(struct multi_context *m, struct multi_instance = *mi, int flags) +{ + if (!flags) + { + return 0; + } + + struct context *c =3D &mi->context; + int peer_id =3D 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 =3D { 0 }; + struct in_addr *vpn_addr4 =3D NULL; + if (c->c2.push_ifconfig_defined) + { + vpn_ip4.s_addr =3D htonl(c->c2.push_ifconfig_local); + vpn_addr4 =3D &vpn_ip4; + } + + struct in6_addr *vpn_addr6 =3D NULL; + if (c->c2.push_ifconfig_ipv6_defined) + { + vpn_addr6 =3D &c->c2.push_ifconfig_ipv6_local; + } + + int ret =3D dco_update_peer_addr(&c->c1.tuntap->dco, peer_id, vpn_addr= 4, vpn_addr6, flags); + if (ret < 0) + { + return ret; + } + + return 0; +} + void dco_install_iroute(struct multi_context *m, struct multi_instance *mi, str= uct mroute_addr *addr) { @@ -723,7 +758,7 @@ } void -dco_delete_iroutes(struct multi_context *m, struct multi_instance *mi) +dco_delete_iroutes_v4(struct multi_context *m, struct multi_instance *mi) { #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_WIN32) if (!dco_enabled(&m->top.options)) @@ -734,7 +769,7 @@ struct context *c =3D &mi->context; - if (mi->context.c2.push_ifconfig_defined) + if (c->c2.push_ifconfig_defined) { for (const struct iroute *ir =3D c->options.iroutes; ir; ir =3D ir= ->next) { @@ -742,7 +777,7 @@ dco_win_del_iroute_ipv4(&c->c1.tuntap->dco, htonl(ir->network)= , ir->netbits); #else net_route_v4_del(&m->top.net_ctx, &ir->network, ir->netbits, - &mi->context.c2.push_ifconfig_local, c->c1.tu= ntap->actual_name, 0, + &c->c2.push_ifconfig_local, c->c1.tuntap->act= ual_name, 0, DCO_IROUTE_METRIC); #endif } @@ -750,7 +785,7 @@ #if !defined(_WIN32) /* Check if we added a host route as the assigned client IP addres= s was * not in the on link scope defined by --ifconfig */ - in_addr_t ifconfig_local =3D mi->context.c2.push_ifconfig_local; + in_addr_t ifconfig_local =3D c->c2.push_ifconfig_local; if (multi_check_push_ifconfig_extra_route(mi, htonl(ifconfig_local= ))) { @@ -761,8 +796,22 @@ } #endif } +#endif /* if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_= WIN32) */ +} - if (mi->context.c2.push_ifconfig_ipv6_defined) +void +dco_delete_iroutes_v6(struct multi_context *m, struct multi_instance *mi) +{ +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_WIN32) + if (!dco_enabled(&m->top.options)) + { + return; + } + ASSERT(TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DEV_TYPE_TUN); + + struct context *c =3D &mi->context; + + if (c->c2.push_ifconfig_ipv6_defined) { for (const struct iroute_ipv6 *ir6 =3D c->options.iroutes_ipv6; ir= 6; ir6 =3D ir6->next) { @@ -770,7 +819,7 @@ dco_win_del_iroute_ipv6(&c->c1.tuntap->dco, ir6->network, ir6-= >netbits); #else net_route_v6_del(&m->top.net_ctx, &ir6->network, ir6->netbits, - &mi->context.c2.push_ifconfig_ipv6_local, c->= c1.tuntap->actual_name, 0, + &c->c2.push_ifconfig_ipv6_local, c->c1.tuntap= ->actual_name, 0, DCO_IROUTE_METRIC); #endif } @@ -778,7 +827,7 @@ /* Checked if we added a host route as the assigned client IP addr= ess was * outside the --ifconfig-ipv6 tun interface config */ #if !defined(_WIN32) - struct in6_addr *dest =3D &mi->context.c2.push_ifconfig_ipv6_local; + struct in6_addr *dest =3D &c->c2.push_ifconfig_ipv6_local; if (multi_check_push_ifconfig_ipv6_extra_route(mi, dest)) { /* On windows we do not install these routes, so we also do no= t need to delete them */ diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h index 4e5aad5..d999e2f 100644 --- a/src/openvpn/dco.h +++ b/src/openvpn/dco.h @@ -191,6 +191,30 @@ int keepalive_timeout, int mss); /** + * Update the peer's virtual address(es) in the DCO to the current value(s) + * + * @param dco DCO device contect + * @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 wich 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, struct in_ad= dr *vpn_ipv4, struct in6_addr *vpn_ipv6, int flags); + +/** + * Update the peer's virtual address(es) in the DCO to the current value(s) + * + * @param m server multi context + * @param mi peer multi instnace + * @param flags `(1 << AF_INET)`, `(1 << AF_INET6)` or `(1 << AF_INET)= | (1 << AF_INET6)` depending on wich address has changed + * @return int 0 on success or a negative error code otherwise + */ +int +dco_multi_update_peer_addr(struct multi_context *m, struct multi_instance = *mi, int flags); + +/** * Remove a peer from DCO * * @param c the main instance context of the peer to remove @@ -218,12 +242,20 @@ struct mroute_addr *addr); /** - * Remove all routes added through the specified client + * Remove all IPv4 routes added through the specified client * * @param m the server context * @param mi the client instance for which routes have to be removed */ -void dco_delete_iroutes(struct multi_context *m, struct multi_instance *mi= ); +void dco_delete_iroutes_v4(struct multi_context *m, struct multi_instance = *mi); + +/** + * Remove all IPv6 routes added through the specified client + * + * @param m the server context + * @param mi the client instance for which routes have to be removed + */ +void dco_delete_iroutes_v6(struct multi_context *m, struct multi_instance = *mi); /** * Update traffic statistics for all peers @@ -361,7 +393,12 @@ } static inline void -dco_delete_iroutes(struct multi_context *m, struct multi_instance *mi) +dco_delete_iroutes_v4(struct multi_context *m, struct multi_instance *mi) +{ +} + +static inline void +dco_delete_iroutes_v6(struct multi_context *m, struct multi_instance *mi) { } diff --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c index a1e373d..59fc633 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, struct in_ad= dr *vpn_ipv4, struct in6_addr *vpn_ipv6, int flags) +{ + return 0; +} + #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-compare" diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index b92fa43..512d0c0 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -684,6 +684,50 @@ return ret; } +int +dco_update_peer_addr(dco_context_t *dco, unsigned int peerid, struct in_ad= dr *vpn_ipv4, 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 =3D ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_SET= ); + if (!nl_msg) + { + return -ENOMEM; + } + + struct nlattr *attr =3D nla_nest_start(nl_msg, OVPN_A_PEER); + int ret =3D -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_a= ddr : 0); + } + + if (flags & (1 << AF_INET6)) + { + struct in6_addr addr6; + if (!vpn_ipv6) + { + CLEAR(addr6); + vpn_ipv6 =3D &addr6; + } + NLA_PUT(nl_msg, OVPN_A_PEER_VPN_IPV6, sizeof(struct in6_addr), vpn= _ipv6); + } + + nla_nest_end(nl_msg, attr); + ret =3D 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_G= ETFAMILY * message. We parse the reply and we retrieve the multicast group ID asso= ciated * with the "ovpn-dco" netlink family. diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index f46c24d..20b2930 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -526,6 +526,12 @@ } int +dco_update_peer_addr(dco_context_t *dco, unsigned int peerid, struct in_ad= dr *vpn_ipv4, struct in6_addr *vpn_ipv6, int flags) +{ + return 0; +} + +int dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_sl= ot_t slot, const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const u= int8_t *decrypt_key, const uint8_t *decrypt_iv, const char *ciphername, bool epoch) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 1625fd0..3328cb0 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -476,6 +476,40 @@ set_prefix(mi); } +static void +multi_del_iroutes_v4(struct multi_context *m, struct multi_instance *mi) +{ + if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN) + { + return; + } + + const struct iroute *ir; + + dco_delete_iroutes_v4(m, mi); + for (ir =3D mi->context.options.iroutes; ir !=3D NULL; ir =3D ir->next) + { + mroute_helper_del_iroute46(m->route_helper, ir->netbits); + } +} + +static void +multi_del_iroutes_v6(struct multi_context *m, struct multi_instance *mi) +{ + if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN) + { + return; + } + + const struct iroute_ipv6 *ir6; + + dco_delete_iroutes_v6(m, mi); + for (ir6 =3D mi->context.options.iroutes_ipv6; ir6 !=3D NULL; ir6 =3D = ir6->next) + { + mroute_helper_del_iroute46(m->route_helper, ir6->netbits); + } +} + /* * Tell the route helper about deleted iroutes so * that it can update its mask of currently used @@ -484,23 +518,8 @@ static void multi_del_iroutes(struct multi_context *m, struct multi_instance *mi) { - const struct iroute *ir; - const struct iroute_ipv6 *ir6; - - dco_delete_iroutes(m, mi); - - if (TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DEV_TYPE_TUN) - { - for (ir =3D mi->context.options.iroutes; ir !=3D NULL; ir =3D ir->= next) - { - mroute_helper_del_iroute46(m->route_helper, ir->netbits); - } - - for (ir6 =3D mi->context.options.iroutes_ipv6; ir6 !=3D NULL; ir6 = =3D ir6->next) - { - mroute_helper_del_iroute46(m->route_helper, ir6->netbits); - } - } + multi_del_iroutes_v4(m, mi); + multi_del_iroutes_v6(m, mi); } static void @@ -1291,6 +1310,64 @@ return owner; } +static void +multi_add_iroutes_v4(struct multi_context *m, struct multi_instance *mi) +{ + if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN) + { + return; + } + + struct gc_arena gc =3D gc_new(); + const struct iroute *ir; + + mi->did_iroutes =3D true; + for (ir =3D mi->context.options.iroutes; ir !=3D NULL; ir =3D ir->next) + { + if (ir->netbits >=3D 0) + { + msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s", + print_in_addr_t(ir->network, 0, &gc), ir->netbits, + multi_instance_string(mi, false, &gc)); + } + else + { + msg(D_MULTI_LOW, "MULTI: internal route %s -> %s", + print_in_addr_t(ir->network, 0, &gc), multi_instance_strin= g(mi, false, &gc)); + } + + mroute_helper_add_iroute46(m->route_helper, ir->netbits); + + multi_learn_in_addr_t(m, mi, ir->network, ir->netbits, false); + } + gc_free(&gc); +} + +static void +multi_add_iroutes_v6(struct multi_context *m, struct multi_instance *mi) +{ + if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN) + { + return; + } + + struct gc_arena gc =3D gc_new(); + const struct iroute_ipv6 *ir6; + + mi->did_iroutes =3D true; + for (ir6 =3D mi->context.options.iroutes_ipv6; ir6 !=3D NULL; ir6 =3D = ir6->next) + { + msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s", + print_in6_addr(ir6->network, 0, &gc), ir6->netbits, + multi_instance_string(mi, false, &gc)); + + mroute_helper_add_iroute46(m->route_helper, ir6->netbits); + + multi_learn_in6_addr(m, mi, ir6->network, ir6->netbits, false); + } + gc_free(&gc); +} + /* * A new client has connected, add routes (server -> client) * to internal routing table. @@ -1298,42 +1375,8 @@ static void multi_add_iroutes(struct multi_context *m, struct multi_instance *mi) { - struct gc_arena gc =3D gc_new(); - const struct iroute *ir; - const struct iroute_ipv6 *ir6; - if (TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DEV_TYPE_TUN) - { - mi->did_iroutes =3D true; - for (ir =3D mi->context.options.iroutes; ir !=3D NULL; ir =3D ir->= next) - { - if (ir->netbits >=3D 0) - { - msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s", - print_in_addr_t(ir->network, 0, &gc), ir->netbits, - multi_instance_string(mi, false, &gc)); - } - else - { - msg(D_MULTI_LOW, "MULTI: internal route %s -> %s", - print_in_addr_t(ir->network, 0, &gc), multi_instance_s= tring(mi, false, &gc)); - } - - mroute_helper_add_iroute46(m->route_helper, ir->netbits); - - multi_learn_in_addr_t(m, mi, ir->network, ir->netbits, false); - } - for (ir6 =3D mi->context.options.iroutes_ipv6; ir6 !=3D NULL; ir6 = =3D ir6->next) - { - msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s", - print_in6_addr(ir6->network, 0, &gc), ir6->netbits, - multi_instance_string(mi, false, &gc)); - - mroute_helper_add_iroute46(m->route_helper, ir6->netbits); - - multi_learn_in6_addr(m, mi, ir6->network, ir6->netbits, false); - } - } - gc_free(&gc); + multi_add_iroutes_v4(m, mi); + multi_add_iroutes_v6(m, mi); } /* @@ -4347,9 +4390,10 @@ } /* Function to unlearn previous ifconfig of a client in the server multi_c= ontext after a PUSH_UPDATE */ -void +static void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi) { + multi_del_iroutes_v4(m, mi); in_addr_t old_addr =3D 0; old_addr =3D htonl(mi->context.c2.push_ifconfig_local); multi_unlearn_in_addr_t(m, mi, old_addr); @@ -4359,9 +4403,10 @@ } /* Function to unlearn previous ifconfig-ipv6 of a client in the server mu= lti_context after a PUSH_UPDATE */ -void +static void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi) { + multi_del_iroutes_v6(m, mi); struct in6_addr old_addr6; CLEAR(old_addr6); old_addr6 =3D mi->context.c2.push_ifconfig_ipv6_local; @@ -4372,59 +4417,120 @@ } /** - * 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. + * When a push-update message containing ifconfig/ifconfig-ipv6 options is= sent + * from the server, update the vhash with new IP/IPv6 addresses in the + * multi_context and update the virtual peer addresses in the DCO. + * Also remove the iroutes and readd it 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 curren= t IPv4 if no change + * @param new_ipv6 The new IPv6 address, NULL for -ifconfig-ipv6 or the c= urrent IPv6 if no change */ void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *new_ip, const char *new_ipv6) +update_vaddr(struct multi_context *m, struct multi_instance *mi, const cha= r *new_ip, const char *new_ipv6) { + /* to avoid resetting an IP to its current value, the dco_update_peer_= addr() function used to update the DCO, + * needs to know if actually an IPv4 or an IPv6 has been modified + */ + int update_flags =3D 0; + + /* -ifconfig */ + if (!new_ip && mi->context.c2.push_ifconfig_defined) + { + unlearn_ifconfig(m, mi); + update_flags |=3D (1 << AF_INET); + } + /* -ifconfig-ipv6 */ + if (!new_ipv6 && mi->context.c2.push_ifconfig_ipv6_defined) + { + unlearn_ifconfig_ipv6(m, mi); + update_flags |=3D (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) =3D=3D 1 - && multi_learn_in_addr_t(m, mi, ntohl(new_addr.s_addr), -1, tr= ue)) + if (inet_pton(AF_INET, new_ip, &new_addr) =3D=3D 1) { - mi->context.c2.push_ifconfig_defined =3D true; - mi->context.c2.push_ifconfig_local =3D ntohl(new_addr.s_addr); - /* set our client's VPN endpoint for status reporting purposes= */ - mi->reporting_addr =3D mi->context.c2.push_ifconfig_local; + new_addr.s_addr =3D ntohl(new_addr.s_addr); + /* if no previous IP or the new IP is different */ + if (!mi->context.c2.push_ifconfig_defined + || (mi->context.c2.push_ifconfig_defined + && new_addr.s_addr !=3D mi->context.c2.push_ifconfig_l= ocal)) + { + /* Remove old IP */ + if (mi->context.c2.push_ifconfig_defined) + { + unlearn_ifconfig(m, mi); + } + + /* Add new IP */ + if (multi_learn_in_addr_t(m, mi, new_addr.s_addr, -1, true= )) + { + mi->context.c2.push_ifconfig_defined =3D true; + mi->context.c2.push_ifconfig_local =3D new_addr.s_addr; + /* set our client's VPN endpoint for status reporting = purposes */ + mi->reporting_addr =3D mi->context.c2.push_ifconfig_lo= cal; + update_flags |=3D (1 << AF_INET); + } + } } } 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) =3D=3D 1 - && multi_learn_in6_addr(m, mi, new_addr6, -1, true)) + if (inet_pton(AF_INET6, new_ipv6, &new_addr6) =3D=3D 1) { - mi->context.c2.push_ifconfig_ipv6_defined =3D true; - mi->context.c2.push_ifconfig_ipv6_local =3D new_addr6; - /* set our client's VPN endpoint for status reporting purposes= */ - mi->reporting_addr_ipv6 =3D mi->context.c2.push_ifconfig_ipv6_= local; + /* if no previous IPv6 or the new IPv6 is different */ + if (!mi->context.c2.push_ifconfig_ipv6_defined + || (mi->context.c2.push_ifconfig_ipv6_defined + && memcmp(&new_addr6, &mi->context.c2.push_ifconfig_ip= v6_local, sizeof(struct in6_addr)))) + { + /* Remove old IPv6 */ + if (mi->context.c2.push_ifconfig_ipv6_defined) + { + unlearn_ifconfig_ipv6(m, mi); + } + + /* Add new IPv6 */ + if (multi_learn_in6_addr(m, mi, new_addr6, -1, true)) + { + mi->context.c2.push_ifconfig_ipv6_defined =3D true; + mi->context.c2.push_ifconfig_ipv6_local =3D new_addr6; + /* set our client's VPN endpoint for status reporting = purposes */ + mi->reporting_addr_ipv6 =3D mi->context.c2.push_ifconf= ig_ipv6_local; + update_flags |=3D (1 << AF_INET); + } + } } } + + if (!update_flags) + { + return; + } + +#if defined(TARGET_LINUX) + /* we update the virtual addresses of the peer in the DCO */ + if (dco_enabled(&m->top.options)) + { + dco_multi_update_peer_addr(m, mi, update_flags); + } +#endif + + /* we add the iroutes with the new VPN IP */ + if (update_flags & (1 << AF_INET) && new_ip) + { + multi_add_iroutes_v4(m, mi); + } + if (update_flags & (1 << AF_INET6) && new_ipv6) + { + multi_add_iroutes_v6(m, mi); + } } bool diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index c686e47..739aaef 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -723,8 +723,6 @@ #endif void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *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 cha= r *new_ip, const char *new_ipv6); #endif /* MULTI_H */ diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c index 51c7b5f..bcab1d0 100644 --- a/src/openvpn/push_util.c +++ b/src/openvpn/push_util.c @@ -151,14 +151,35 @@ struct options o; CLEAR(o); - /* Set canary values to detect ifconfig options in push-update message= s. - * 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, by the a new value if present, or + * they will remain equals to the current values otherwise. + * If the current values are not defined they will remain NULL. */ - const char *canary =3D "canary"; - o.ifconfig_local =3D canary; - o.ifconfig_ipv6_local =3D canary; + + char ifconfig_local[INET_ADDRSTRLEN]; + struct in_addr addr4 =3D { .s_addr =3D htonl(mi->context.c2.push_ifcon= fig_local) }; + if (mi->context.c2.push_ifconfig_defined) + { + o.ifconfig_local =3D inet_ntop(AF_INET, &addr4, ifconfig_local, si= zeof(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 =3D inet_ntop(AF_INET6, &mi->context.c2.push= _ifconfig_ipv6_local, + ifconfig_ipv6_local, sizeof(ifco= nfig_ipv6_local)); + if (!o.ifconfig_ipv6_local) + { + return false; + } + } struct buffer_entry *e =3D msgs->head; while (e) @@ -195,28 +216,8 @@ 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_de= fined) - { - unlearn_ifconfig_ipv6(m, mi); - } - - if (o.ifconfig_local && !strcmp(o.ifconfig_local, canary)) - { - o.ifconfig_local =3D NULL; - } - if (o.ifconfig_ipv6_local && !strcmp(o.ifconfig_ipv6_local, canary= )) - { - o.ifconfig_ipv6_local =3D 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 true; @@ -253,12 +254,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 w= hile 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 !=3D UPT_BROADCAST)) { diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b/tests/unit_t= ests/openvpn/test_push_update_msg.c index 9b7978e..2d2fc93 100644 --- a/tests/unit_tests/openvpn/test_push_update_msg.c +++ b/tests/unit_tests/openvpn/test_push_update_msg.c @@ -31,19 +31,7 @@ } void -unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi) -{ - return; -} - -void -unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi) -{ - return; -} - -void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *new_ip, const char *new_ipv6) +update_vaddr(struct multi_context *m, struct multi_instance *mi, const cha= r *new_ip, const char *new_ipv6) { return; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1450?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: I88f37e47026a7d7ea0f7c6bde1d79d32c14e5297 Gerrit-Change-Number: 1450 Gerrit-PatchSet: 6 Gerrit-Owner: mrbff Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --x568nD5v8i8= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

mrbff uploaded patch set #6 to this change.

View Change

PUSH_UPDATE: add DCO and iroutes support on server
<= br>Renamed update_vhash() to update_vaddr().
Moved the -ifconfig logic f= rom send_single_push_update() to update_vaddr().
Changed canary logic to= use the current value of ifconfig (if present) as
canary. Added logic i= n the update_vaddr() to not unlearn and re-learn the same
address.
Added logic to update the peer's virtual address(es) in the DCO to th= e current
value(s) after sending a PUSH_UPDATE message on Linux.
Adde= d dco_multi_update_peer_addr() and dco_update_peer_addr() to do so.

= Added logic to update iroutes after changing the peer's virtual address= (es).
Divided dco_delete_iroutes() in _v4() and _v6() to better updating= iroutes.

Change-Id: I88f37e47026a7d7ea0f7c6bde1d79d32c14e5297
Si= gned-off-by: Marco Baffo <marco@...2726...>
---
M src/openvpn/d= co.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/multi.c
M src/o= penvpn/multi.h
M src/openvpn/push_util.c
M tests/unit_tests/openvpn/t= est_push_update_msg.c
9 files changed, 379 insertions(+), 142 deletions(= -)

git pull ssh://gerrit.openvpn.net:29418/openv=
pn refs/changes/50/1450/6
diff --git a/src/openvpn/dco.c b/src/open=
vpn/dco.c
index 26b8645..70aaf90 100644
---= a/src/openvpn/dco.c
+++ b/src/openvpn/dco.c
@@ -658,6 +658,41 @@
return 0;
}

+int<= /span>
+dco_multi_update_pee= r_addr(struct multi_context *m, struct multi_instance *mi, int flags)
+{
+ if (!flags)
+ {
+ return 0;
+ }
+=
+ struct context = *c =3D &mi->context;
+ int peer_id =3D c->c2.tls_multi->dco_peer_id;
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ /* In server mode we need to fetch the remote = addresses from the push config */
+ struct in_addr vpn_ip4 =3D { 0 };
+ struct in_addr *vpn_addr4 =3D NULL;
+ if (c->c2.push= _ifconfig_defined)
+ = {
+ vpn_ip4= .s_addr =3D htonl(c->c2.push_ifconfig_local);
+ vpn_addr4 =3D &vpn_ip4;
= + }
+
+ struct in6_addr *vpn_addr6 =3D NULL;
+ if (c->c2.push_ifconfig_ipv6_defin= ed)
+ {
= + vpn_addr6 =3D &c-&= gt;c2.push_ifconfig_ipv6_local;
+ }
+
+ int ret =3D dco_u= pdate_peer_addr(&c->c1.tuntap->dco, peer_id, vpn_addr4, vpn_addr6= , flags);
+ if (re= t < 0)
+ {

+ return ret;
+ }
+
+ return 0;
+}
+
void
dco_install_iroute(struct multi_context *= m, struct multi_instance *mi, struct mroute_addr *addr)
{<= /span>
@@ -723,7 +758,7 @@
}

void
-dc= o_delete_iroutes(struct multi_context *m, struct multi_instance *mi)=
+dco_delete_iroutes_v4(stru= ct multi_context *m, struct multi_instance *mi)
{ #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_W= IN32)
if (!dco_enabled(&m->top.options))=
@@ -734,7 +769,7 @@

struct= context *c =3D &mi->context;

- if (mi->context.c2.push_ifconfig_de= fined)
+ if (c->= ;c2.push_ifconfig_defined)
{
= for (const struct iroute *ir =3D c->options.iroutes; ir; ir =3D ir->n= ext)
{
@@ -742,7 +777,7 @@<= br> dco_win_del_iroute_ipv4(&c->c1.tuntap->dco,= htonl(ir->network), ir->netbits);
#else
<= span> net_route_v4_del(&m->top.net_ctx, &ir->netw= ork, ir->netbits,

- = &mi->context.c2.push_ifconfig_local, c-&= gt;c1.tuntap->actual_name, 0,
+ &c->c2.push_ifconfig_local= , c->c1.tuntap->actual_name, 0,
= DCO_IROUTE_METRIC);
#endif
= }
@@ -750,7 +785,7 @@
#if !defined(_WIN= 32)
/* Check if we added a host route as the assig= ned client IP address was
* not in the on link sc= ope defined by --ifconfig */
- in_addr_t ifconfig_local =3D mi->context.c2.push_ifconfig= _local;
+ in_a= ddr_t ifconfig_local =3D c->c2.push_ifconfig_local;

if (multi_check_push_ifconfig_extra_route(mi, htonl(= ifconfig_local)))
{
@@ -761,8 +796= ,22 @@
}
#endif
= }
+#endif /* if d= efined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(_WIN32) */
+}

- if (mi->context.c2.p= ush_ifconfig_ipv6_defined)
+void
+dco_dele= te_iroutes_v6(struct multi_context *m, struct multi_instance *mi)+{
+#if defined(TARGET_LINUX) || defined(TARGET_FRE= EBSD) || defined(_WIN32)
+ if (!dco_enabled(&m->top.options))
+ {
+ return;
+ }
+ = ASSERT(TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DEV_TYPE_TUN);
+
+ struct context *c =3D &mi->contex= t;
+
+ if (c->c2.push_ifconfig_ipv6_= defined)
{
for (const struct = iroute_ipv6 *ir6 =3D c->options.iroutes_ipv6; ir6; ir6 =3D ir6->next)=
{
@@ -770,7 +819,7 @@
<= span> dco_win_del_iroute_ipv6(&c->c1.tuntap->dco, ir6= ->network, ir6->netbits);

#else
= net_route_v6_del(&m->top.net_ctx, &ir6->network, ir6= ->netbits,
- = &mi->context.c2.push_ifconfig_ipv6_local, c->= ;c1.tuntap->actual_name, 0,
+ &c->c2.push_ifconfig_ipv6_lo= cal, c->c1.tuntap->actual_name, 0,
= DCO_IROUTE_METRIC);
#endif
= }
@@ -778,7 +827,7 @@
/* Che= cked if we added a host route as the assigned client IP address was<= br> * outside the --ifconfig-ipv6 tun interface config */
#if !defined(_WIN32)
- struct in6_addr *dest =3D &mi->context.c2.push= _ifconfig_ipv6_local;
+ struct in6_addr *dest =3D &c->c2.push_ifconfig_ipv6_local;=
if (multi_check_push_ifconfig_ipv6_extra_route(mi= , dest))
{
/* On wind= ows we do not install these routes, so we also do not need to delete them *= /
diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h=
index 4e5aad5..d999e2f 100644
--- a/src/openvpn/d= co.h
+++ b/src/openvpn/dco.h
@@ -191,6 +191= ,30 @@
int keepalive_timeout, int mss);

/**
+ * Update the peer's virtual address(es) in the DCO t= o the current value(s)
+ * @param dco = DCO device contect
+ * @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 vp= n_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_IN= ET6)` or `(1 << AF_INET) | (1 << AF_INET6)` depending on wich a= ddress has changed
+ = * @return int the DCO message status on success or a negative error co= de otherwise
+ */
+int
+dco_update_peer_addr(dco_context_t *dco= , unsigned int peerid, struct in_addr *vpn_ipv4, struct in6_addr *vpn_ipv6,= int flags);
+=
+/**
+ * Update the peer's virtual address(= es) in the DCO to the current value(s)
+ *
+ * @param m server multi context
+ * @param mi peer multi instnace
+ * @param flags `(1 <<= AF_INET)`, `(1 << AF_INET6)` or `(1 << AF_INET) | (1 << = AF_INET6)` depending on wich address has changed

+ * @return int 0 on success or a negative= error code otherwise
+ */
+int
= +dco_multi_update_peer_addr(str= uct multi_context *m, struct multi_instance *mi, int flags);
+

+/**
* Remove a peer from DCO=
*
* @param c the main instance contex= t of the peer to remove
@@ -218,12 +242,20 @@
struct mroute_addr *addr);


/**
- * = Remove all routes added through the specified client
+ * Remove all IPv4 routes added through t= he specified client
*
* @param m = the server context
* @param mi the client instan= ce for which routes have to be removed
*/
-void dco_delete_iroutes(struct multi_= context *m, struct multi_instance *mi);

+void dco_delete_iroutes_v4(struct multi_context *m, str= uct multi_instance *mi);
+
+/**
= + * Remove all IPv6 routes adde= d through the specified client
+ *
+ * @pa= ram m the server context
+ * @param mi the client instance for which routes have = to be removed
+ */
+void dco_delete_iroute= s_v6(struct multi_context *m, struct multi_instance *mi);
=
/**
* Update traffic statistics for all= peers
@@ -361,7 +393,12 @@
}

static inline void
-dco_delete_iroutes(struct multi_context *m, struct mult= i_instance *mi)
+dco_= delete_iroutes_v4(struct multi_context *m, struct multi_instance *mi)
+{
+}
+
+sta= tic inline void
+dco_= delete_iroutes_v6(struct multi_context *m, struct multi_instance *mi)
{
}

diff --g= it a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c
= index a1e373d..59fc633 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_con= text_t *dco, unsigned int peerid, struct in_addr *vpn_ipv4, struct in6_addr= *vpn_ipv6, int flags)
+ return 0;
+}
+
#if defined(__GNUC__)= || defined(__clang__)
#pragma GCC diagnostic push<= br> #pragma GCC diagnostic ignored "-Wsign-compare"<= br>diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c
index b92fa43..512d0c0 100644
--- a/src/openvp= n/dco_linux.c
+++ b/src/openvpn/dco_linux.c
@@ -684,6 +684,50 @@

return ret;
}

+int=
+dco_update_peer_add= r(dco_context_t *dco, unsigned int peerid, struct in_addr *vpn_ipv4, struct= in6_addr *vpn_ipv6, int flags)
+{
+ ms= g(D_DCO_DEBUG, "%s: peer-id %d", __func__, peerid);
+

+ if (!flags)
+ {
+ return 0;
+
= + struct nl_msg *nl_msg =3D = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_SET);
+ if (!nl_msg)
+ {
+ return -ENOMEM;
+ }
+=
+ struct nlattr *= attr =3D nla_nest_start(nl_msg, OVPN_A_PEER);
+ int ret =3D -EMSGSIZE;
+ NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, p= eerid);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (flags & (1 <<= AF_INET))

+ {
+ NLA_PUT_U32(nl_= msg, OVPN_A_PEER_VPN_IPV4, vpn_ipv4 ? vpn_ipv4->s_addr : 0);
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ if (flags & (1 << AF_INET6))
+ {
+ struct in6_addr addr6;
+ if (!vpn_ipv6)
+ {
+ CLEAR(addr6);
+ vpn_ipv6 =3D &addr6;+ }
+ NLA_PUT(nl_msg, OVPN_A_PEER_VP= N_IPV6, sizeof(struct in6_addr), vpn_ipv6);
+ }
+
+ nla_n= est_end(nl_msg, attr);
+
+nla_put_failure:
+ nlmsg_free(nl_msg);
+ return ret;
+}
+
/* This function parses the reply provided by the kernel to t= he CTRL_CMD_GETFAMILY
* message. We parse the reply and w= e retrieve the multicast group ID associated
* with the &= quot;ovpn-dco" netlink family.
diff --git a/src/openvp= n/dco_win.c b/src/openvpn/dco_win.c
index f46c24d..20b2930 = 100644
--- a/src/openvpn/dco_win.c
+++ b/sr= c/openvpn/dco_win.c
@@ -526,6 +526,12 @@
}=

int
+dco_update_peer_addr(dco_context_t *dco, unsigned int p= eerid, struct in_addr *vpn_ipv4, 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_k= ey, const uint8_t *encrypt_iv, const uint8_t *decrypt_key,
= const uint8_t *decrypt_iv, const char *ciphername, bool epoch)=
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 1625fd0..3328cb0 100644
--- a/src/openvp= n/multi.c
+++ b/src/openvpn/multi.c
@@ -476= ,6 +476,40 @@
set_prefix(mi);
}

+static = void
+multi_del_irout= es_v4(struct multi_context *m, struct multi_instance *mi)
+{
+ if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_= TYPE_TUN)
+ {
+ return;+ }
+
+ const struct iroute *ir;
+
+ dco_delete_iroutes_v4(m, mi);
+ for (ir =3D mi->context.options.iroutes; ir !=3D = NULL; ir =3D ir->next)
+ {
+ = mroute_helper_del_iroute46(m->route_helper, ir->netbits);
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+}
+
+static = void
+multi_del_irout= es_v6(struct multi_context *m, struct multi_instance *mi)
+{
+ if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_= TYPE_TUN)
+ {
+ return;+ }
+
+ const struct iroute_ipv6 *ir6;
+
+ dco_delete_iroutes_v6(m, mi);
+ for (ir6 =3D mi->context.options.iroutes_ip= v6; ir6 !=3D NULL; ir6 =3D ir6->next)
+ {
+ mroute_helper_del_iroute46(m->route_helper, ir6->netbi= ts);
+ }+}
+
/*
* Tell t= he route helper about deleted iroutes so
* that it can up= date its mask of currently used
@@ -484,23 +518,8 @@=
static void
multi_del_iroutes(struct multi_cont= ext *m, struct multi_instance *mi)
{
- const struct iroute *ir;
- const struct iroute_ipv6 *ir6;
-
- dco_delete_iroutes(m, mi);
-

- if (TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DE= V_TYPE_TUN)
- {
- for (ir =3D mi->= ;context.options.iroutes; ir !=3D NULL; ir =3D ir->next)
- {

- mroute_helper_del_iroute46(m->rout= e_helper, ir->netbits);
- }
-=
- for (ir6 =3D mi->= context.options.iroutes_ipv6; ir6 !=3D NULL; ir6 =3D ir6->next)- {
- mroute_helper_del_iroute46(m-&= gt;route_helper, ir6->netbits);
- }
= - }
+ multi_del= _iroutes_v4(m, mi);
+= multi_del_iroutes_v6(m, mi);
}

static void
@@ -1291,6 +1310,64 @@
return owner;

}

+static void

+multi_add_iroutes_v4(struct multi_context= *m, struct multi_instance *mi)
+{
+ if= (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN)
+ {
+ return;
+ }
+
+ struct g= c_arena gc =3D gc_new();
+ const struct iroute *ir;
+
+ m= i->did_iroutes =3D true;
+ for (ir =3D mi->context.options.iroutes; ir !=3D NULL; ir = =3D ir->next)
+ = {
+ if (ir-&g= t;netbits >=3D 0)
= + {
+ = msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s",
+ print_in_= addr_t(ir->network, 0, &gc), ir->netbits,
+ multi_instance_string(mi,= false, &gc));
+ = }
+ els= e
+ {+ msg(D_MULTI_LOW,= "MULTI: internal route %s -> %s",
+ print_in_addr_t(ir->network,= 0, &gc), multi_instance_string(mi, false, &gc));
+ }
+
+ mroute_helper_add_iroute46(m->route_helper, ir->netb= its);
+
+ multi_learn_in_addr_t(m, = mi, ir->network, ir->netbits, false);

+ }
+ gc_free(&gc);
+}
+
+static void
+multi_add_iroutes_v6(struct multi= _context *m, struct multi_instance *mi)

+{
+ if (TUNNEL_TYPE(mi->context.c1.tuntap) !=3D DEV_TYPE_TUN)+ {
+ return;
+ }
+
+ = struct gc_arena gc =3D gc_new();
+ const struct iroute_ipv6 *ir6;
+
+ mi->did_iroutes =3D true;
+ for (ir6 =3D mi->context.options.iroutes_ipv6= ; ir6 !=3D NULL; ir6 =3D ir6->next)
+ {
+ msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s&qu= ot;,
+ pri= nt_in6_addr(ir6->network, 0, &gc), ir6->netbits,
+ multi_instance_string(mi= , false, &gc));
+=
+ mroute_help= er_add_iroute46(m->route_helper, ir6->netbits);
+
+ multi_learn_in6_addr(m, mi, ir6->network, ir6->= ;netbits, false);
+ = }
+ gc_free(&= ;gc);
+}
+

/*
<= span> * A new client has connected, add routes (server -> client)
* to internal routing table.
@@ -1298,42 +1375= ,8 @@
static void
multi_add_iroutes(struc= t multi_context *m, struct multi_instance *mi)
{- struct gc_arena gc =3D gc_n= ew();
- const struct= iroute *ir;
- const= struct iroute_ipv6 *ir6;
- if (TUNNEL_TYPE(mi->context.c1.tuntap) =3D=3D DEV_TYPE_TUN)
- {
- mi->did_iroutes =3D true;
- for (ir =3D mi->= context.options.iroutes; ir !=3D NULL; ir =3D ir->next)
- {
- if (ir->netbits >=3D 0)- {
- msg(D_MULTI_LOW, "= ;MULTI: internal route %s/%d -> %s",
- print_in_addr_t(ir->network, = 0, &gc), ir->netbits,
- multi_instance_string(mi, false, &gc));
- }
- else

- {
- msg(D_MULTI_LOW, "MULTI: i= nternal route %s -> %s",
- print_in_addr_t(ir->network, 0, &gc),= multi_instance_string(mi, false, &gc));
- }
-
- = mroute_helper_add_iroute46(m->route_helper, ir->netbits);
-
- multi_learn_in_addr_t(m, mi, ir->= network, ir->netbits, false);
- }
- = for (ir6 =3D mi->context.options.iroutes_ipv6; ir6 !=3D NULL; ir6= =3D ir6->next)
- = {
- msg= (D_MULTI_LOW, "MULTI: internal route %s/%d -> %s",
<= span style=3D"color: hsl(0, 100%, 40%);">- print_in6_addr(ir= 6->network, 0, &gc), ir6->netbits,

- multi_instance_string(mi, false, &am= p;gc));
-
- mroute_helper_add_irout= e46(m->route_helper, ir6->netbits);

-
- = multi_learn_in6_addr(m, mi, ir6->network, ir6->netbits, fa= lse);
- }=
- }
- gc_free(&gc);
+ multi_add_iroutes_v4(m, mi);+ multi_add_iroutes_v6(m, = mi);
}

/*
@@ -4347,9 +4390,10 @@

}

/* Function to unlearn previous ifconfig of a client in the server mul= ti_context after a PUSH_UPDATE */

-void
+sta= tic void
unlearn_ifconfig(struct multi_context *m, struct = multi_instance *mi)
{
+ multi_del_iroutes_v4(m, mi);
in= _addr_t old_addr =3D 0;
old_addr =3D htonl(mi->cont= ext.c2.push_ifconfig_local);
multi_unlearn_in_addr_t(m= , mi, old_addr);
@@ -4359,9 +4403,10 @@
}<= /span>

/* Function to unlearn previous ifconfig= -ipv6 of a client in the server multi_context after a PUSH_UPDATE */=
-void
+static void
unlearn_ifco= nfig_ipv6(struct multi_context *m, struct multi_instance *mi)
{

+ multi_del_= iroutes_v6(m, mi);
struct in6_addr old_addr6; CLEAR(old_addr6);
old_addr6 =3D mi->co= ntext.c2.push_ifconfig_ipv6_local;
@@ -4372,59 +4417,120 @@=
}

/**
- * Update the vhash with new IP/IPv6 a= ddresses in the multi_context when a
- * push-update message containing ifconfig/ifconfig-ipv6 opt= ions is sent
- * from t= he server.
+ * When a= push-update message containing ifconfig/ifconfig-ipv6 options is sent
+ * from the server, upda= te the vhash with new IP/IPv6 addresses in the
+ * multi_context and update the virtual peer add= resses in the DCO.
+ = * Also remove the iroutes and readd it with the new IP if needed. *
* @param m The multi_context * @param mi The multi_instance of the client we are updatin= g
- * @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 c= hange
+ * @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, struc= t multi_instance *mi, const char *new_ip, const char *new_ipv6)
<= span style=3D"color: hsl(120, 100%, 40%);">+update_vaddr(struct multi_conte= xt *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6)=

{
+= /* to avoid resetting an IP to its current value, the dco_update_peer_a= ddr() function used to update the DCO,
+ * needs to know if actually an IPv4 or an IPv6 has = been modified
+ *= /
+ int update_fla= gs =3D 0;
++ /* -ifconfig */+ if (!new_ip && mi= ->context.c2.push_ifconfig_defined)
+ {
+ unlearn_ifconfig(m, mi);
+ update_flags |=3D (1 << AF_INET);+ }
+ /* -ifconfig-ipv6 */
+ if (!new_ipv6 && mi->= context.c2.push_ifconfig_ipv6_defined)
+ {
+ unlearn_ifconfig_ipv6(m, mi);
+ update_flags |=3D (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_a= ddr;
CLEAR(new_addr);
- if (inet_pton(AF_INET, new_ip, &new_ad= dr) =3D=3D 1
- = && multi_learn_in_addr_t(m, mi, ntohl(new_addr.s_addr), -1, true= ))
+ if (inet_= pton(AF_INET, new_ip, &new_addr) =3D=3D 1)
{
- mi->cont= ext.c2.push_ifconfig_defined =3D true;
- mi->context.c2.push_ifconfig_local =3D ntoh= l(new_addr.s_addr);
- = /* set our client's VPN endpoint for status reporting purpose= s */
- mi-&g= t;reporting_addr =3D mi->context.c2.push_ifconfig_local;
+ new_addr.s_addr =3D nto= hl(new_addr.s_addr);

= + /* if no previous IP or the new IP is different */
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (!mi->context= .c2.push_ifconfig_defined

+ || (mi->context.c2.push_ifconfig_defined+ &&= ; new_addr.s_addr !=3D mi->context.c2.push_ifconfig_local))
+ {

+ /* Remove old IP */
+ if (mi= ->context.c2.push_ifconfig_defined)
+ {
+ unlearn_ifconfig(m, mi);
+ }

+

+ /* Add new IP */
+ if (multi_learn_in_addr_t= (m, mi, new_addr.s_addr, -1, true))
+ {
+ mi->context.c2.push_ifconfig_defined = =3D true;
+ = mi->context.c2.push_ifconfig_local =3D new_addr.s_addr;=
+ /* set= our client's VPN endpoint for status reporting purposes */
<= span style=3D"color: hsl(120, 100%, 40%);">+ mi->repo= rting_addr =3D mi->context.c2.push_ifconfig_local;

+ update_flags |=3D (1 = << AF_INET);
+ = }
+ = }
}
}

if (new_ipv6)
{
- /* Remove old IPv6 */
- if (mi->context.= c2.push_ifconfig_ipv6_defined)
- {
- = unlearn_ifconfig_ipv6(m, mi);
- }
-
- /* Add n= ew IPv6 */
struct in6_addr new_addr6;
CLEAR(new_addr6);

- if (inet_pton(AF_INET6, new_ipv6, &new_addr6) =3D=3D 1=
- &&= ; multi_learn_in6_addr(m, mi, new_addr6, -1, true))
+ if (inet_pton(AF_INET6, new_ipv6,= &new_addr6) =3D=3D 1)
{
- mi->context.c2.push_ifconfig= _ipv6_defined =3D true;
- mi->context.c2.push_ifconfig_ipv6_local =3D new_addr6;
- /* set our cl= ient's VPN endpoint for status reporting purposes */
- mi->reporting_addr_ipv6 = =3D mi->context.c2.push_ifconfig_ipv6_local;
+ /* if no previous IPv6 or the new I= Pv6 is different */
+= if (!mi->context.c2.push_ifconfig_ipv6_defined
+ || (mi->contex= t.c2.push_ifconfig_ipv6_defined

+ && memcmp(&new_addr6, &mi-&= gt;context.c2.push_ifconfig_ipv6_local, sizeof(struct in6_addr))))+ {
+ /* Remove old IPv6= */

+ = if (mi->context.c2.push_ifconfig_ipv6_defined)
+ {
+ unlearn_ifconfig_ipv6(m, mi= );
+ }=
+
+ /* Add new IPv6 */
+ if (mul= ti_learn_in6_addr(m, mi, new_addr6, -1, true))
+ {
+ mi->context.c2.push_ifconfi= g_ipv6_defined =3D true;
+ mi->context.c2.push_ifconfig_ipv6_local =3D new= _addr6;
+ = /* set our client's VPN endpoint for status reporting purposes = */
+ = mi->reporting_addr_ipv6 =3D mi->context.c2.push_ifconfig_ipv6_loca= l;
+ = update_flags |=3D (1 << AF_INET);
+ }
+ }
}
}

++ if (!update_flags)=
+ {
+ return;
+ }
+
+#i= f defined(TARGET_LINUX)
+ /* we update the virtual addresses of the peer in the DCO */=
+ if (dco_enabled(&m= ->top.options))
+ = {
+ dco_mul= ti_update_peer_addr(m, mi, update_flags);
+ }
+#endif
+
+ /* we add the iroute= s with the new VPN IP */
+ if (update_flags & (1 << AF_INET) && new_ip)
+ {
+ multi_add_iroutes_v4(m, mi);<= /span>
+ }
+ if (update_flags & (1 <= < AF_INET6) && new_ipv6)

+ {
+ multi_add_iroutes_v6(m, mi);
+ }
}

bool

diff --git a/src/openvpn/multi.h b/src/openvpn/mul= ti.h
index c686e47..739aaef 100644
--- a/sr= c/openvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -723,8 +723,6 @@

#endif

void

-update_vhash= (struct multi_context *m, struct multi_instance *mi, const char *new_ip, co= nst char *new_ipv6);
-v= oid unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi);
-void unlearn_ifconfig_i= pv6(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 --g= it a/src/openvpn/push_util.c b/src/openvpn/push_util.c
inde= x 51c7b5f..bcab1d0 100644
--- a/src/openvpn/push_util.c
+++ b/src/openvpn/push_util.c
@@ -151,14 +151,= 35 @@
struct options o;
CLEAR(o);=

- = /* Set canary values to detect ifconfig options in push-update messages.
- * These placeholde= r strings will be overwritten to NULL by the option
- * parser if -ifconfig or -ifconfig-ipv= 6 options are present in the
- * push-update.
+ /* Set canary values to detect -ifconfig and -ifconfig-ipv6 opti= ons in
+ * push-u= pdate messages. These placeholder strings will be overwritten
+ * to NULL by the option pars= er if -ifconfig or -ifconfig-ipv6 options

+ * are present in the push-update, by the a new v= alue if present, or
+= * they will remain equals to the current values otherwise.
<= span style=3D"color: hsl(120, 100%, 40%);">+ * If the current values ar= e not defined they will remain NULL.

*/
- const char *canary =3D "ca= nary";

- o.ifco= nfig_local =3D canary;
= - o.ifconfig_ipv6_local =3D canary;
+
+= char ifconfig_local[INET_ADDRSTRLEN];
+ struct in_addr addr4 =3D { .s_addr =3D htonl(mi-&= gt;context.c2.push_ifconfig_local) };
+ if (mi->context.c2.push_ifconfig_defined)+ {
+ o.ifconfig_local =3D 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_i= pv6_defined)
+ {
+ o.ifconfig_ip= v6_local =3D inet_ntop(AF_INET6, &mi->context.c2.push_ifconfig_ipv6_= local,
+ = ifconfig_ipv6_local, sizeof(ifconfig_ipv6_loca= l));
+ if (!o.= ifconfig_ipv6_local)
= + {
+ = return false;
+ = }
+ }<= br>
struct buffer_entry *e =3D msgs->head;
while (e)
@@ -195,28 +216,8 @@
if (option_types_found & OPT_P_UP)
{
- = /* -ifconfig */
- = if (!o.ifconfig_local && mi->context.c2.push_ifconfig_defi= ned)
- {<= br>- unlearn_ifconfig(= m, mi);
- }
- /* -ifconfig-ipv6 = */
- if (!o.ifco= nfig_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 =3D NULL;
- }
- = if (o.ifconfig_ipv6_local && !strcmp(o.ifconfig_ipv6_local, ca= nary))
- {
- o.ifconfig_ipv6= _local =3D NULL;
- = }
-
- /* new ifconfig or new ifconfi= g-ipv6 */
- upda= te_vhash(m, mi, o.ifconfig_local, o.ifconfig_ipv6_local);
+ /* ifconfig or ifconfig-ipv6 = */
+ update_va= ddr(m, mi, o.ifconfig_local, o.ifconfig_ipv6_local);
}=

return true;
@@ -25= 3,12 +254,14 @@
static int
send_push_upda= te(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 mes= sages cannot currently be sent while DCO is enabled."
= " To send a PUSH_UPDATE message, be sure to use t= he --disable-dco option.");
return 0;<= br> }
+#end= if

if (!msg || !*msg || !m || (!targ= et && type !=3D UPT_BROADCAST))
{
diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b/tests/un= it_tests/openvpn/test_push_update_msg.c

index 9b7978e..2d2f= c93 100644
--- a/tests/unit_tests/openvpn/test_push_update_= msg.c
+++ b/tests/unit_tests/openvpn/test_push_update_msg.c=
@@ -31,19 +31,7 @@
}

void
-un= learn_ifconfig(struct multi_context *m, struct multi_instance *mi)-{
- return;
-}
-
-void
-unlearn_ifconfig_ipv6(struct multi_context = *m, struct multi_instance *mi)
-{
- return;=
-}
-
-void
-upda= te_vhash(struct multi_context *m, struct multi_instance *mi, const char *ne= w_ip, const char *new_ipv6)
+update_vaddr(struct multi_context *m, struct multi_instance *mi, co= nst char *new_ip, const char *new_ipv6)
{
= return;
}

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

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
<= meta itemprop=3D"name" content=3D"View Change"/>
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I88f37e47026a7d7ea0f7c6bde1d= 79d32c14e5297
Gerrit-Change-Number: 1450
Gerrit-PatchSet: 6
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --x568nD5v8i8=--