From: "cron2 (Code Review)" Date: Fri, 17 Oct 2025 20:29:40 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., marco@...2726..., openvpn-devel@lists.sourceforge.net, frank@...2641..., antonio@...2726... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="NyP1hYXhN64="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: PUSH_UPDATE server: remove old IP(s) from vhash after sending a messa... List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: mrbff , plaisthos , flichtenheld Cc: openvpn-devel --NyP1hYXhN64= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#8) to the change originally created by= mrbff. ( http://gerrit.openvpn.net/c/openvpn/+/1253?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: PUSH_UPDATE server: remove old IP(s) from vhash after sendi= ng a message containing ifconfig(-ipv6) ...................................................................... PUSH_UPDATE server: remove old IP(s) from vhash after sending a message con= taining ifconfig(-ipv6) When sending a PUSH_UPDATE containing an ifconfig(-ipv6) option, we must ad= d the new IP to the multi_context vhash (hash table of the clients indexed by virtual IPs). Now= in addition to adding new client IPs, old IPs are also removed from vhash, allowing for a = more complete update. Change-Id: I07a8ddd9026eef64b6f5abde98702a9801616a5f Signed-off-by: Marco Baffo Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1253 Message-Id: <20251017201916.21697-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg33= 412.html Signed-off-by: Gert Doering --- M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/push.c M src/openvpn/push.h M src/openvpn/push_util.c M tests/unit_tests/openvpn/test_push_update_msg.c 6 files changed, 205 insertions(+), 72 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/53/1253/8 diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 2863ff1..558314c 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -4269,46 +4269,144 @@ close_instance(top); } +/* Searches for the address and deletes it if it is owned by the multi_ins= tance */ +static void +multi_unlearn_addr(struct multi_context *m, struct multi_instance *mi, con= st struct mroute_addr *addr) +{ + struct hash_element *he; + const uint32_t hv =3D hash_value(m->vhash, addr); + struct hash_bucket *bucket =3D hash_bucket(m->vhash, hv); + struct multi_route *r =3D NULL; + + /* if route currently exists, get the instance which owns it */ + he =3D hash_lookup_fast(m->vhash, bucket, addr, hv); + if (he) + { + r =3D (struct multi_route *)he->value; + } + + /* if the route does not exist or exists but is not owned by the curre= nt instance, return */ + if (!r || r->instance !=3D mi) + { + return; + } + + struct gc_arena gc =3D gc_new(); + msg(D_MULTI_LOW, "MULTI: Unlearn: %s -> %s", mroute_addr_print(&r->add= r, &gc), multi_instance_string(mi, false, &gc)); + learn_address_script(m, NULL, "delete", &r->addr); + hash_remove_by_value(m->vhash, r); + multi_route_del(r); + + gc_free(&gc); +} + +/** + * @param m The multi_context + * @param mi The multi_instance of the client we are updating + * @param a The new IPv4 address in network byte order + */ +static void +multi_unlearn_in_addr_t(struct multi_context *m, struct multi_instance *mi= , in_addr_t a) +{ + struct mroute_addr addr; + CLEAR(addr); + + addr.type =3D MR_ADDR_IPV4; + addr.len =3D 4; + addr.v4.addr =3D a; + + multi_unlearn_addr(m, mi, &addr); +} + +/** + * @param m The multi_context + * @param mi The multi_instance of the client we are updating + * @param a6 The new IPv6 address + */ +static void +multi_unlearn_in6_addr(struct multi_context *m, struct multi_instance *mi,= struct in6_addr a6) +{ + struct mroute_addr addr; + CLEAR(addr); + + addr.type =3D MR_ADDR_IPV6; + addr.len =3D 16; + addr.v6.addr =3D a6; + + multi_unlearn_addr(m, mi, &addr); +} + +/* Function to unlearn previous ifconfig of a client in the server multi_c= ontext after a PUSH_UPDATE */ +void +unlearn_ifconfig(struct multi_context *m, struct multi_instance *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); + mi->context.c2.push_ifconfig_defined =3D false; + mi->context.c2.push_ifconfig_local =3D 0; +} + +/* Function to unlearn previous ifconfig-ipv6 of a client in the server mu= lti_context after a PUSH_UPDATE */ +void +unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi) +{ + struct in6_addr old_addr6; + CLEAR(old_addr6); + old_addr6 =3D mi->context.c2.push_ifconfig_ipv6_local; + multi_unlearn_in6_addr(m, mi, old_addr6); + mi->context.c2.push_ifconfig_ipv6_defined =3D false; + CLEAR(mi->context.c2.push_ifconfig_ipv6_local); +} + /** * 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. This function should be called after a push-update - * and old_ip/old_ipv6 are the previous addresses of the client in - * ctx->options.ifconfig_local and ctx->options.ifconfig_ipv6_local. + * from the server. + * + * @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 */ void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *old_ip, const char *old_ipv6) +update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *new_ip, const char *new_ipv6) { - struct in_addr addr; - struct in6_addr new_ipv6; - - if ((mi->context.options.ifconfig_local && (!old_ip || strcmp(old_ip, = mi->context.options.ifconfig_local))) - && inet_pton(AF_INET, mi->context.options.ifconfig_local, &addr) = =3D=3D 1) + if (new_ip) { - in_addr_t new_ip =3D ntohl(addr.s_addr); + /* Remove old IP */ + if (mi->context.c2.push_ifconfig_defined) + { + unlearn_ifconfig(m, mi); + } /* Add new IP */ - multi_learn_in_addr_t(m, mi, new_ip, -1, true); + 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)) + { + mi->context.c2.push_ifconfig_defined =3D true; + mi->context.c2.push_ifconfig_local =3D ntohl(new_addr.s_addr); + } } - /* TO DO: - * else if (old_ip) - * { - * // remove old IP - * } - */ - - if ((mi->context.options.ifconfig_ipv6_local && (!old_ipv6 || strcmp(o= ld_ipv6, mi->context.options.ifconfig_ipv6_local))) - && inet_pton(AF_INET6, mi->context.options.ifconfig_ipv6_local, &n= ew_ipv6) =3D=3D 1) + if (new_ipv6) { - /* Add new IPv6 */ - multi_learn_in6_addr(m, mi, new_ipv6, -1, true); - } + /* Remove old IPv6 */ + if (mi->context.c2.push_ifconfig_ipv6_defined) + { + unlearn_ifconfig_ipv6(m, mi); + } - /* TO DO: - * else if (old_ipv6) - * { - * // remove old IPv6 - * } - */ + /* 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)) + { + mi->context.c2.push_ifconfig_ipv6_defined =3D true; + mi->context.c2.push_ifconfig_ipv6_local =3D new_addr6; + } + } } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index b2b892b..97bbc4a 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -692,6 +692,8 @@ #endif void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *old_ip, const char *old_ipv6); +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); #endif /* MULTI_H */ diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 0c8eb84..2c717c7 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -1118,7 +1118,7 @@ " To be able to process PUSH_UPDATE messages, be s= ure to use the --disable-dco option."); return PUSH_MSG_ERROR; } - return process_incoming_push_update(c, permission_mask, option_typ= es_found, &buf, false); + return process_push_update(c, &c->options, permission_mask, option= _types_found, &buf, false); } else { diff --git a/src/openvpn/push.h b/src/openvpn/push.h index 6b3275e..19a029a 100644 --- a/src/openvpn/push.h +++ b/src/openvpn/push.h @@ -61,11 +61,13 @@ * message has not yet been received. * * @param c The context for the operation. + * @param o The options structure to be updated with the received push opt= ions. * @param permission_mask The permission mask specifying which options are= allowed to be pulled. * @param option_types_found A pointer to a variable that will be filled w= ith the types of options * found in the message. * @param buf A buffer containing the received message. - * @param msg_sender A boolean indicating if function is called by the mes= sage sender (server). + * @param msg_sender A boolean indicating if the message is being processe= d on the client (false) + * or on the server (true). * * @return * - `PUSH_MSG_UPDATE`: The message was processed successfully, and the up= dates were applied. @@ -74,9 +76,8 @@ * - `PUSH_MSG_ERROR`: An error occurred during message processing, or the= message is invalid. */ -int process_incoming_push_update(struct context *c, unsigned int permissio= n_mask, - unsigned int *option_types_found, struct = buffer *buf, - bool msg_sender); +int process_push_update(struct context *c, struct options *o, unsigned int= permission_mask, + unsigned int *option_types_found, struct buffer *b= uf, bool msg_sender); int process_incoming_push_msg(struct context *c, const struct buffer *buff= er, bool honor_received_options, unsigned int pe= rmission_mask, diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c index 25c6ebe..0e86ad3 100644 --- a/src/openvpn/push_util.c +++ b/src/openvpn/push_util.c @@ -16,18 +16,17 @@ #endif int -process_incoming_push_update(struct context *c, unsigned int permission_ma= sk, - unsigned int *option_types_found, struct buff= er *buf, - bool msg_sender) +process_push_update(struct context *c, struct options *o, unsigned int per= mission_mask, + unsigned int *option_types_found, struct buffer *buf, = bool msg_sender) { int ret =3D PUSH_MSG_ERROR; const uint8_t ch =3D buf_read_u8(buf); if (ch =3D=3D ',') { - if (apply_push_options(c, &c->options, buf, permission_mask, optio= n_types_found, c->c2.es, + if (apply_push_options(c, o, buf, permission_mask, option_types_fo= und, c->c2.es, true)) { - switch (c->options.push_continuation) + switch (o->push_continuation) { case 0: case 1: @@ -144,13 +143,27 @@ /* send the message(s) prepared to one single client */ static bool -send_single_push_update(struct context *c, struct buffer *msgs, unsigned i= nt *option_types_found) +send_single_push_update(struct multi_context *m, struct multi_instance *mi= , struct buffer *msgs) { if (!msgs[0].data || !*(msgs[0].data)) { return false; } + int i =3D -1; + unsigned int option_types_found =3D 0; + struct context *c =3D &mi->context; + 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. + */ + const char *canary =3D "canary"; + o.ifconfig_local =3D canary; + o.ifconfig_ipv6_local =3D canary; while (msgs[++i].data && *(msgs[i].data)) { @@ -159,14 +172,14 @@ return false; } - /* After sending the control message, we update the options - * server-side in the client's context so pushed options like - * ifconfig/ifconfig-ipv6 can actually work. + /* After sending the control message, we parse it, miming the beha= vior + * of `process_incoming_push_msg()` and we fill an empty `options`= struct + * with the new options. If an `ifconfig_local` or `ifconfig_ipv6_= local` + * options is found we update the vhash accordingly, so that the p= ushed + * ifconfig/ifconfig-ipv6 options can actually work. * If we don't do that, packets arriving from the client with the * new address will be rejected and packets for the new address * will not be routed towards the client. - * For the same reason we later update the vhash too in - * `send_push_update()` function. * Using `buf_string_compare_advance()` we mimic the behavior * inside `process_incoming_push_msg()`. However, we don't need * to check the return value here because we just want to `advance= `, @@ -176,17 +189,39 @@ */ struct buffer tmp_msg =3D msgs[i]; buf_string_compare_advance(&tmp_msg, push_update_cmd); - if (process_incoming_push_update(c, pull_permission_mask(c), optio= n_types_found, &tmp_msg, true) =3D=3D PUSH_MSG_ERROR) + unsigned int permission_mask =3D pull_permission_mask(c); + if (process_push_update(c, &o, permission_mask, &option_types_foun= d, &tmp_msg, true) =3D=3D PUSH_MSG_ERROR) { msg(M_WARN, "Failed to process push update message sent to cli= ent ID: %u", c->c2.tls_multi->peer_id); - continue; - } - c->options.push_option_types_found |=3D *option_types_found; - if (!options_postprocess_pull(&c->options, c->c2.es)) - { - msg(M_WARN, "Failed to post-process push update message sent t= o client ID: %u", c->c2.tls_multi->peer_id); } } + + 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); + } + return true; } @@ -229,8 +264,6 @@ int msgs_num =3D (strlen(msg) / safe_cap) + ((strlen(msg) % safe_cap) = !=3D 0); struct buffer *msgs =3D gc_malloc((msgs_num + 1) * sizeof(struct buffe= r), true, &gc); - unsigned int option_types_found =3D 0; - msgs[msgs_num].data =3D NULL; if (!message_splitter(msg, msgs, &gc, safe_cap)) { @@ -255,15 +288,9 @@ return 0; } - const char *old_ip =3D mi->context.options.ifconfig_local; - const char *old_ipv6 =3D mi->context.options.ifconfig_ipv6_local; if (!mi->halt - && send_single_push_update(&mi->context, msgs, &option_types_f= ound)) + && send_single_push_update(m, mi, msgs)) { - if (option_types_found & OPT_P_UP) - { - update_vhash(m, mi, old_ip, old_ipv6); - } gc_free(&gc); return 1; } @@ -289,18 +316,11 @@ } /* Type is UPT_BROADCAST so we update every client */ - option_types_found =3D 0; - const char *old_ip =3D curr_mi->context.options.ifconfig_local; - const char *old_ipv6 =3D curr_mi->context.options.ifconfig_ipv6_lo= cal; - if (!send_single_push_update(&curr_mi->context, msgs, &option_type= s_found)) + if (!send_single_push_update(m, curr_mi, msgs)) { msg(M_CLIENT, "ERROR: Peer ID: %u has not been updated", curr_= mi->context.c2.tls_multi->peer_id); continue; } - if (option_types_found & OPT_P_UP) - { - update_vhash(m, curr_mi, old_ip, old_ipv6); - } count++; } diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b/tests/unit_t= ests/openvpn/test_push_update_msg.c index 60596ed..7fadb69 100644 --- a/tests/unit_tests/openvpn/test_push_update_msg.c +++ b/tests/unit_tests/openvpn/test_push_update_msg.c @@ -29,7 +29,19 @@ } void -update_vhash(struct multi_context *m, struct multi_instance *mi, const cha= r *old_ip, const char *old_ipv6) +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) { return; } @@ -95,7 +107,7 @@ } else if (honor_received_options && buf_string_compare_advance(&buf, pu= sh_update_cmd)) { - return process_incoming_push_update(c, permission_mask, option_typ= es_found, &buf, false); + return process_push_update(c, &c->options, permission_mask, option= _types_found, &buf, false); } else { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1253?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: I07a8ddd9026eef64b6f5abde98702a9801616a5f Gerrit-Change-Number: 1253 Gerrit-PatchSet: 8 Gerrit-Owner: mrbff Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-CC: ordex --NyP1hYXhN64= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #8 to the change originally created by mrbff.

<= p>View C= hange

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

PUSH_UPDATE server: remove old IP(s=
) from vhash after sending a message containing ifconfig(-ipv6)

When= sending a PUSH_UPDATE containing an ifconfig(-ipv6) option, we must add th= e new IP to the
multi_context vhash (hash table of the clients indexed b= y virtual IPs). Now in addition to
adding new client IPs, old IPs are al= so removed from vhash, allowing for a more complete update.

Change-I= d: I07a8ddd9026eef64b6f5abde98702a9801616a5f
Signed-off-by: Marco Baffo = <marco@...2726...>
Acked-by: Gert Doering <gert@...1296...><= br>Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1253
Message-Id: &= lt;20251017201916.21697-1-gert@...1296...>
URL: https://www.mail-arch= ive.com/openvpn-devel@lists.sourceforge.net/msg33412.html
Signed-off-by:= Gert Doering <gert@...1296...>
---
M src/openvpn/multi.c
M = src/openvpn/multi.h
M src/openvpn/push.c
M src/openvpn/push.h
M sr= c/openvpn/push_util.c
M tests/unit_tests/openvpn/test_push_update_msg.c<= br>6 files changed, 205 insertions(+), 72 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/53/1253=
/8
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.cindex 2863ff1..558314c 100644
--- a/src/openvpn/mult= i.c
+++ b/src/openvpn/multi.c
@@ -4269,46 += 4269,144 @@
close_instance(top);
}

+/* S= earches for the address and deletes it if it is owned by the multi_instance= */
+static void
+multi_unlearn_addr(struc= t multi_context *m, struct multi_instance *mi, const struct mroute_addr *ad= dr)
+{
+ struct hash_element *he;
+ const uint32_t hv =3D= hash_value(m->vhash, addr);
+ struct hash_bucket *bucket =3D hash_bucket(m->vhash, hv)= ;
+ struct multi_r= oute *r =3D NULL;
+
+ /* if route curre= ntly exists, get the instance which owns it */
+ he =3D hash_lookup_fast(m->vhash, bucket,= addr, hv);
+ if (= he)
+ {
= + r =3D (struct multi_ro= ute *)he->value;
+= }
+
+ /* if the route does not exis= t or exists but is not owned by the current instance, return */

<= span style=3D"color: hsl(120, 100%, 40%);">+ if (!r || r->instance != =3D mi)

+ {=
+ return;
= + }
+
+ struct gc_arena gc =3D gc_new();
+ msg(D_MULTI_LOW, "MULTI: Unlearn: %s = -> %s", mroute_addr_print(&r->addr, &gc), multi_instance= _string(mi, false, &gc));
+ learn_address_script(m, NULL, "delete", &r->= addr);
+ hash_remo= ve_by_value(m->vhash, r);
+ multi_route_del(r);
+
+ gc= _free(&gc);
+}
+
+/**
+ * @param m The multi_context
+ * @param mi The multi_instance of the= client we are updating
+ * @param a The new IPv4 address in network byte order
+ */

+static void
+multi_unlearn_in_addr_t(struct multi_context *m, struct = multi_instance *mi, in_addr_t a)
+{
+ s= truct mroute_addr addr;
+ CLEAR(addr);
= +
+ addr.type =3D = MR_ADDR_IPV4;
+ ad= dr.len =3D 4;
+ ad= dr.v4.addr =3D a;
+
+ multi_unlearn_add= r(m, mi, &addr);
= +}
+
+/**
+ * @param m The multi_context
+ * @param mi The multi_instance of = the client we are updating
+ * @param a6 The new IPv6 address
+ */
+static void
+= multi_unlearn_in6_addr(struct multi_context *m, struct multi_instance *mi, = struct in6_addr a6)
+= {
+ struct mroute_= addr addr;
+ CLEAR= (addr);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ addr.type =3D MR_ADDR_IPV6;=

+ addr.len =3D 16= ;
+ addr.v6.addr = =3D a6;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ multi_unlearn_addr(m, mi, &= amp;addr);

+}<= br>+
+/* Function to unlearn previous ifconfig of a = client in the server multi_context after a PUSH_UPDATE */
+void
+unlearn_ifconfig(struct multi_context *m, struct mul= ti_instance *mi)
+{
+ in_addr_t old_add= r =3D 0;
+ old_add= r =3D htonl(mi->context.c2.push_ifconfig_local);
+ multi_unlearn_in_addr_t(m, mi, old_ad= dr);
+ mi->cont= ext.c2.push_ifconfig_defined =3D false;
+ mi->context.c2.push_ifconfig_local =3D 0;=
+}
+
+/* Function to unlearn previous ifconfig-ipv6 of a client in th= e server multi_context after a PUSH_UPDATE */
+void
+unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_inst= ance *mi)
+{+ struct in6_addr old_addr= 6;
+ CLEAR(old_add= r6);
+ old_addr6 = =3D mi->context.c2.push_ifconfig_ipv6_local;
+ multi_unlearn_in6_addr(m, mi, old_addr6);
+ mi->context.c2= .push_ifconfig_ipv6_defined =3D false;
+ CLEAR(mi->context.c2.push_ifconfig_ipv6_local);
+}
+
/**
= * Update the vhash with new IP/IPv6 addresses in the multi_context when a<= /span>
* push-update message containing ifconfig/ifconfig-ipv6 o= ptions is sent
- * from= the server. This function should be called after a push-update
<= span style=3D"color: hsl(0, 100%, 40%);">- * and old_ip/old_ipv6 are the pr= evious addresses of the client in

- * ctx->options.ifconfig_local and ctx->options.ifconfig_= ipv6_local.
+ * from = the server.
+ *
+ * @param m The m= ulti_context
+ * @par= am 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
*/
void
-update_vhash(struct multi_context *m, struct multi_instanc= e *mi, const char *old_ip, const char *old_ipv6)
+update_vhash(struct multi_context *m, struct m= ulti_instance *mi, const char *new_ip, const char *new_ipv6)
{

- struct in_add= r addr;
- struct in6= _addr new_ipv6;
-
- if ((mi->context.opt= ions.ifconfig_local && (!old_ip || strcmp(old_ip, mi->context.op= tions.ifconfig_local)))
- && inet_pton(AF_INET, mi->context.options.ifconfig_loc= al, &addr) =3D=3D 1)
+ if (new_ip)
{
- in_addr_t new_ip =3D ntohl(addr.s_addr);
+ /* Remove old IP= */
+ if (mi-&= gt;context.c2.push_ifconfig_defined)
+ {
+ unlearn_ifconfig(m, mi);
+ }

= /* Add new IP */
- = multi_learn_in_addr_t(m, mi, new_ip, -1, true);
+ struct in_addr new_addr;+ CLEAR(new_addr);
+ if (inet_pton(A= F_INET, new_ip, &new_addr) =3D=3D 1
+ && multi_learn_in_addr_t(m, mi, nto= hl(new_addr.s_addr), -1, true))
+ {
+ mi->context.c2.push_ifconfig_defined =3D true;
<= span style=3D"color: hsl(120, 100%, 40%);">+ mi->context.c2.p= ush_ifconfig_local =3D ntohl(new_addr.s_addr);

+ }
}

- /* TO DO:
- * else if (old_ip)
- * {
- * // remove old IP
<= br>- * }
- */
-
- = if ((mi->context.options.ifconfig_ipv6_local && (!old_ipv6 ||= strcmp(old_ipv6, mi->context.options.ifconfig_ipv6_local)))
<= span style=3D"color: hsl(0, 100%, 40%);">- && inet_pton(AF_I= NET6, mi->context.options.ifconfig_ipv6_local, &new_ipv6) =3D=3D 1)<= /span>
+ if (new_ipv6)
{
- = /* Add new IPv6 */
- multi_learn_in6_addr(m, mi, new_ipv6, -1, true);
- }

+ /* Remove old IPv6 */
+ if (mi->context.c2.push_ifcon= fig_ipv6_defined)
+ = {
+ = unlearn_ifconfig_ipv6(m, mi);
+ }

- /* TO DO:
- * else if (old_ipv6)
- * {
= - * // remove old IPv6
- * }
- = */
+ /* Add= new IPv6 */
+ = struct in6_addr new_addr6;
+ CLEAR(new_addr6);
+ if (inet_pton(AF_INET6, new_ipv6, &new_addr6) =3D= =3D 1
+ &a= mp;& multi_learn_in6_addr(m, mi, new_addr6, -1, true))
+ {
+ mi->context.c2.push_ifconfig_ip= v6_defined =3D true;
= + mi->context.c2.push_ifconfig_ipv6_local =3D new_addr6;
+ }
+ }

}diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
index b2b892b..97bbc4a 100644

--- a/src/openvpn/multi.h<= /span>
+++ b/src/openvpn/multi.h
@@ -692,6 +692,8 = @@
#endif

void-update_vhash(struct multi_cont= ext *m, struct multi_instance *mi, const char *old_ip, const char *old_ipv6= );
+update_vhash(stru= ct multi_context *m, struct multi_instance *mi, const char *new_ip, const c= har *new_ipv6);
+void= unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi);
+void unlearn_ifconfig_ip= v6(struct multi_context *m, struct multi_instance *mi);

#endif /* MULTI_H */
diff --git a/src/openv= pn/push.c b/src/openvpn/push.c
index 0c8eb84..2c717c7 10064= 4
--- a/src/openvpn/push.c
+++ b/src/openvp= n/push.c
@@ -1118,7 +1118,7 @@
= " To be able to process PUSH_UPDATE messages, be sure to = use the --disable-dco option.");
return P= USH_MSG_ERROR;
}
- return process_incoming_push_update(c, permissi= on_mask, option_types_found, &buf, false);
+ return process_push_update(c, &c->= ;options, permission_mask, option_types_found, &buf, false);
= }
else
{
diff --git a/src/openvpn/push.h b/src/openvpn/push.h

ind= ex 6b3275e..19a029a 100644
--- a/src/openvpn/push.h<= br>+++ b/src/openvpn/push.h
@@ -61,11 +61,13 @@
* message has not yet been received.
*
* @param c The context for the operation.
+ * @param o The options structure to be = updated with the received push options.
* @param permissi= on_mask The permission mask specifying which options are allowed to be pull= ed.
* @param option_types_found A pointer to a variable t= hat will be filled with the types of options
* = found in the message.
* @param buf A buff= er containing the received message.
- * @param msg_sender A boolean indicating if function is call= ed by the message sender (server).
+ * @param msg_sender A boolean indicating if the message is = being processed on the client (false)
+ * or on the server (true).
<= span> *

* @return
* - `PUSH_MSG_UPDATE= `: The message was processed successfully, and the updates were applied.
@@ -74,9 +76,8 @@
* - `PUSH_MSG_ERROR`: An = error occurred during message processing, or the message is invalid.=
*/

-int process_incoming_push_update(struct context *c, unsigned int= permission_mask,
- = unsigned int *option_types_found, struct buffe= r *buf,
- = bool msg_sender);
+int process_push_update(struct context *c, struct options = *o, unsigned int permission_mask,
+ unsigned int *option_types_found, str= uct buffer *buf, bool msg_sender);

int p= rocess_incoming_push_msg(struct context *c, const struct buffer *buffer,
bool honor_received_options, u= nsigned int permission_mask,
diff --git a/src/openvpn/push_= util.c b/src/openvpn/push_util.c
index 25c6ebe..0e86ad3 100= 644
--- a/src/openvpn/push_util.c
+++ b/src= /openvpn/push_util.c
@@ -16,18 +16,17 @@
#= endif

int
-process_incoming_push_update(struct context *c, unsi= gned int permission_mask,
- unsigned int *option_types_found, struct b= uffer *buf,
- = bool msg_sender)
+process_push_update(struct context *c, struct options *o, u= nsigned int permission_mask,
+ unsigned int *option_types_found, struct buffe= r *buf, bool msg_sender)
{
int ret = =3D PUSH_MSG_ERROR;
const uint8_t ch =3D buf_read_u8(b= uf);
if (ch =3D=3D ',')
{=
- if (apply_pus= h_options(c, &c->options, buf, permission_mask, option_types_found, = c->c2.es,
+ = if (apply_push_options(c, o, buf, permission_mask, option_types_found, c-&= gt;c2.es,
true))
= {
- = switch (c->options.push_continuation)
+ switch (o->push_continuation)
{
case 0:
case 1:
@@ -144,13 +143,27 @@

/* send the message(s) prepared to one si= ngle client */
static bool
-send_single_push_update(struct context *c, struct buf= fer *msgs, unsigned int *option_types_found)
+send_single_push_update(struct multi_context *m, s= truct multi_instance *mi, struct buffer *msgs)
{ if (!msgs[0].data || !*(msgs[0].data))
{
return false;
}
+

int i =3D -1= ;
+ unsigned int o= ption_types_found =3D 0;
+ struct context *c =3D &mi->context;
+ struct options o;
+ CLEAR(o);
+
+ /* Set canary values to detect ifconfig options in push-upda= te messages.
+ * = These placeholder strings will be overwritten to NULL by the option<= br>+ * parser if -ifconfig = or -ifconfig-ipv6 options are present in the
+ * push-update.
+ */
+ const char *canary =3D "canary";
+ o.ifconfig_local =3D canary;
+ o.ifconfig_ipv6_lo= cal =3D canary;

while (msgs[++i].dat= a && *(msgs[i].data))
{
@@ -15= 9,14 +172,14 @@
return false;
= }

- /* After sending the control message, we update the options<= /span>
- * server-side= in the client's context so pushed options like
- * ifconfig/ifconfig-ipv6 can actua= lly work.
+ /*= After sending the control message, we parse it, miming the behavior=
+ * of `process_inc= oming_push_msg()` and we fill an empty `options` struct
+ * with the new options. If an = `ifconfig_local` or `ifconfig_ipv6_local`
+ * options is found we update the vhash accor= dingly, so that the pushed
+ * ifconfig/ifconfig-ipv6 options can actually work.<= br> * If we don't do that, packets arriving from the cli= ent with the
* new address will be rejected and p= ackets for the new address
* will not be routed t= owards the client.
- = * For the same reason we later update the vhash too in
- * `send_push_update()` func= tion.

* Using `buf_string_compare_advance()` we m= imic the behavior
* inside `process_incoming_push= _msg()`. However, we don't need
* to check th= e return value here because we just want to `advance`,
@@ -= 176,17 +189,39 @@
*/
str= uct buffer tmp_msg =3D msgs[i];
buf_string_compare= _advance(&tmp_msg, push_update_cmd);
- if (process_incoming_push_update(c, pull_permiss= ion_mask(c), option_types_found, &tmp_msg, true) =3D=3D PUSH_MSG_ERROR)=
+ unsigned in= t permission_mask =3D pull_permission_mask(c);
+ if (process_push_update(c, &o, permi= ssion_mask, &option_types_found, &tmp_msg, true) =3D=3D PUSH_MSG_ER= ROR)
{
msg(M_WARN, &q= uot;Failed to process push update message sent to client ID: %u", c-&g= t;c2.tls_multi->peer_id);
- continue;
- }
- = c->options.push_option_types_found |=3D *option_types_found;- if (!options_postproce= ss_pull(&c->options, c->c2.es))
- {
- msg(M_WARN, "Failed to post-process push update me= ssage sent to client ID: %u", c->c2.tls_multi->peer_id);<= br> }
}
+
+ 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.p= ush_ifconfig_ipv6_defined)
+ {
+ = unlearn_ifconfig_ipv6(m, mi);
+ }
+
+ if= (o.ifconfig_local && !strcmp(o.ifconfig_local, canary))
= + {
+ o.ifconfig_local =3D NULL;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (o.ifconfig_ipv6_loc= al && !strcmp(o.ifconfig_ipv6_local, canary))

+ {
+ o.ifconfig_ipv6_local =3D NULL;<= br>+ }
+
+ /* new ifconfig or new ifconfig-ipv6 */+ update_vhash(m, mi, = o.ifconfig_local, o.ifconfig_ipv6_local);
+ }
+
return true;
}

@@ -229,8 +264,6 @@
int msgs_num = =3D (strlen(msg) / safe_cap) + ((strlen(msg) % safe_cap) !=3D 0); struct buffer *msgs =3D gc_malloc((msgs_num + 1) * sizeof(struc= t buffer), true, &gc);

- unsigned int option_types_found =3D 0;-
msgs[msgs= _num].data =3D NULL;
if (!message_splitter(msg, msgs, = &gc, safe_cap))
{
@@ -255,15 +288,= 9 @@
return 0;
}

- = const char *old_ip =3D mi->context.options.ifconfig_local;
- const char *old_ipv6 =3D mi= ->context.options.ifconfig_ipv6_local;

if (!mi-= >halt
- &= amp;& send_single_push_update(&mi->context, msgs, &option_ty= pes_found))
+ = && send_single_push_update(m, mi, msgs))
= {
- if (o= ption_types_found & OPT_P_UP)
- {
- update_vhash(m, mi, old_ip, old_ipv6);
- }
= gc_free(&gc);
return 1;
}

@@ -289,18 +316,11 @@
= }

/* Type is UPT_BROADCAST so = we update every client */
- option_types_found =3D 0;
- const char *old_ip =3D curr_mi->context.options.= ifconfig_local;
- = const char *old_ipv6 =3D curr_mi->context.options.ifconfig_ipv6_local;=
- if (!send_sin= gle_push_update(&curr_mi->context, msgs, &option_types_found))
+ if (!send_sin= gle_push_update(m, curr_mi, msgs))
{
msg(M_CLIENT, "ERROR: Peer ID: %u has not been updated= ", curr_mi->context.c2.tls_multi->peer_id);

= continue;
}
- if (option_types_found & OPT_P_UP)
- {
- update_vhash(m, curr_mi, o= ld_ip, old_ipv6);
- = }
count++;
}

diff --git a/tests/unit_tests/openvpn/test_push_updat= e_msg.c b/tests/unit_tests/openvpn/test_push_update_msg.c
i= ndex 60596ed..7fadb69 100644
--- a/tests/unit_tests/openvpn= /test_push_update_msg.c
+++ b/tests/unit_tests/openvpn/test= _push_update_msg.c
@@ -29,7 +29,19 @@
}

void
-update_vhash(struct multi_context *m, struct multi_instance= *mi, const char *old_ip, const char *old_ipv6)
+unlearn_ifconfig(struct multi_context *m, struc= t multi_instance *mi)
+{
+ return;
+}
+
+void
+= unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
+{
+ return;
+}
+
+void
+update_vhash(struct mult= i_context *m, struct multi_instance *mi, const char *new_ip, const char *ne= w_ipv6)
{
return;
}<= /span>
@@ -95,7 +107,7 @@
}
= else if (honor_received_options && buf_string_compare_advance(&= amp;buf, push_update_cmd))
{
- return process_incoming_push_update(c, = permission_mask, option_types_found, &buf, false);
+ return process_push_update(c, &a= mp;c->options, permission_mask, option_types_found, &buf, false);
}
else
{

To view, visit change 1253. To unsubscribe, or f= or help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I07a8ddd9026eef64b6f5abde987= 02a9801616a5f
Gerrit-Change-Number: 1253
Gerrit-PatchSet: 8
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-CC: ordex <antonio@...2726...> = --NyP1hYXhN64=--