From: "cron2 (Code Review)" Date: Fri, 25 Jul 2025 18:06:25 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., 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="kEfPAx8siSo="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dco_linux: fix async message reception List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: plaisthos , flichtenheld , openvpn-devel --kEfPAx8siSo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/11= 00?usp=3Demail ) Change subject: dco_linux: fix async message reception ...................................................................... dco_linux: fix async message reception Currently whenever we send a PEER_GET request to ovpn, we also set the CB that is supposed to parse the reply. However, due to the async nature of netlink messages, we could get an unrelated notification, sent by ovpn (kernel) upon some event, after userland has set the CB, but before parsing the awaited reply. When this happens, the notification is then parsed with the configured CB instead of the notification parser, thus effectively rejecting the notification and losing the event. To fix this inconsistency, make ovpn_handle_msg() the default and only netlink parser CB. It is configured upon DCO initialization and is never removed. ovpn_handle_msg() will check the message type and will call the corresponding handler. This way, no matter what message we get at what time, we'll always parse it correctly. As a bonus we can also simplify the nl_sendmsg() API as we don't need to pass the cb and its argument anymore. The ID of the NLCTRL family is now also stored in the DCO context as we need it to check when we receive a mcast ID lookup message. Change-Id: I23ad79e14844aefde9ece34dadef0b75ff267201 Github: closes OpenVPN/openvpn#793 Signed-off-by: Antonio Quartulli Acked-by: Gert Doering Message-Id: <20250725172708.19456-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 339.html Signed-off-by: Gert Doering --- M src/openvpn/dco_linux.c M src/openvpn/dco_linux.h 2 files changed, 110 insertions(+), 67 deletions(-) diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index 7c639d9..728fb7e 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -167,23 +167,19 @@ } /** - * Send a prepared netlink message and registers cb as callback if non-nul= l. + * Send a prepared netlink message. * * The method will also free nl_msg * @param dco The dco context to use * @param nl_msg the message to use - * @param cb An optional callback if the caller expects an answer - * @param cb_arg An optional param to pass to the callback * @param prefix A prefix to report in the error message to give the us= er context * @return status of sending the message */ static int -ovpn_nl_msg_send(dco_context_t *dco, struct nl_msg *nl_msg, ovpn_nl_cb cb, - void *cb_arg, const char *prefix) +ovpn_nl_msg_send(dco_context_t *dco, struct nl_msg *nl_msg, const char *pr= efix) { dco->status =3D 1; - nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, cb, cb_arg); nl_send_auto(dco->nl_sock, nl_msg); while (dco->status =3D=3D 1) @@ -285,7 +281,7 @@ } nla_nest_end(nl_msg, attr); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -385,6 +381,29 @@ } static void +ovpn_dco_register(dco_context_t *dco) +{ + msg(D_DCO_DEBUG, __func__); + ovpn_get_mcast_id(dco); + + if (dco->ovpn_dco_mcast_id < 0) + { + msg(M_FATAL, "cannot get mcast group: %s", nl_geterror(dco->ovpn_= dco_mcast_id)); + } + + /* Register for ovpn-dco specific multicast messages that the kernel m= ay + * send + */ + int ret =3D nl_socket_add_membership(dco->nl_sock, dco->ovpn_dco_mcast= _id); + if (ret) + { + msg(M_FATAL, "%s: failed to join groups: %d", __func__, ret); + } +} + +static int ovpn_handle_msg(struct nl_msg *msg, void *arg); + +static void ovpn_dco_init_netlink(dco_context_t *dco) { dco->ovpn_dco_id =3D resolve_ovpn_netlink_id(M_FATAL); @@ -420,11 +439,15 @@ nl_socket_set_cb(dco->nl_sock, dco->nl_cb); + dco->dco_message_peer_id =3D -1; nl_cb_err(dco->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &dco->status); nl_cb_set(dco->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish, &dco->status); nl_cb_set(dco->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_nl_cb_finish, &dco->status); + nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco); + + ovpn_dco_register(dco); /* The async PACKET messages confuse libnl and it will drop them with * wrong sequence numbers (NLE_SEQ_MISMATCH), so disable libnl's seque= nce @@ -476,27 +499,6 @@ CLEAR(dco); } -static void -ovpn_dco_register(dco_context_t *dco) -{ - msg(D_DCO_DEBUG, __func__); - ovpn_get_mcast_id(dco); - - if (dco->ovpn_dco_mcast_id < 0) - { - msg(M_FATAL, "cannot get mcast group: %s", nl_geterror(dco->ovpn_= dco_mcast_id)); - } - - /* Register for ovpn-dco specific multicast messages that the kernel m= ay - * send - */ - int ret =3D nl_socket_add_membership(dco->nl_sock, dco->ovpn_dco_mcast= _id); - if (ret) - { - msg(M_FATAL, "%s: failed to join groups: %d", __func__, ret); - } -} - int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev) { @@ -516,10 +518,6 @@ msg(M_FATAL, "DCO: cannot retrieve ifindex for interface %s", dev); } - tt->dco.dco_message_peer_id =3D -1; - - ovpn_dco_register(&tt->dco); - return 0; } @@ -548,7 +546,7 @@ NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_PEER_ID, peerid); nla_nest_end(nl_msg, attr); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -572,7 +570,7 @@ NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid); nla_nest_end(nl_msg, attr); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -598,7 +596,7 @@ NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_SLOT, slot); nla_nest_end(nl_msg, keyconf); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -657,7 +655,7 @@ nla_nest_end(nl_msg, key_conf); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -686,7 +684,7 @@ keepalive_timeout); nla_nest_end(nl_msg, attr); - ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -754,7 +752,7 @@ /* Even though 'nlctrl' is a constant, there seem to be no library * provided define for it */ - int ctrlid =3D genl_ctrl_resolve(dco->nl_sock, "nlctrl"); + dco->ctrlid =3D genl_ctrl_resolve(dco->nl_sock, "nlctrl"); struct nl_msg *nl_msg =3D nlmsg_alloc(); if (!nl_msg) @@ -762,12 +760,12 @@ return -ENOMEM; } - genlmsg_put(nl_msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); + genlmsg_put(nl_msg, 0, 0, dco->ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); int ret =3D -EMSGSIZE; NLA_PUT_STRING(nl_msg, CTRL_ATTR_FAMILY_NAME, OVPN_FAMILY_NAME); - ret =3D ovpn_nl_msg_send(dco, nl_msg, mcast_family_handler, dco, __fun= c__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); @@ -879,31 +877,34 @@ } static int -dco_parse_peer_multi(struct nl_msg *msg, void *arg) +ovpn_handle_peer_multi(dco_context_t *dco, struct nlattr *attrs[]) { - struct nlattr *tb[OVPN_A_MAX + 1]; - struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); - msg(D_DCO_DEBUG, "%s: parsing message...", __func__); - nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0), - genlmsg_attrlen(gnlh, 0), NULL); + /* this function assumes openvpn is running in multipeer mode as + * it accesses c->multi + */ + if (dco->ifmode !=3D OVPN_MODE_MP) + { + msg(M_WARN, "%s: can't parse 'multi-peer' message on P2P instance"= , __func__); + return NL_SKIP; + } - if (!tb[OVPN_A_PEER]) + if (!attrs[OVPN_A_PEER]) { return NL_SKIP; } struct nlattr *tb_peer[OVPN_A_PEER_MAX + 1]; - nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL); + nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL); if (!tb_peer[OVPN_A_PEER_ID]) { - msg(M_WARN, "%s: no peer-id provided in reply", __func__); + msg(M_WARN, "ovpn-dco: no peer-id provided in (MULTI) PEER_GET rep= ly"); return NL_SKIP; } - struct multi_context *m =3D arg; + struct multi_context *m =3D dco->c->multi; uint32_t peer_id =3D nla_get_u32(tb_peer[OVPN_A_PEER_ID]); if (peer_id >=3D m->max_clients || !m->instances[peer_id]) @@ -919,39 +920,53 @@ } static int -dco_parse_peer(struct nl_msg *msg, void *arg) +ovpn_handle_peer(dco_context_t *dco, struct nlattr *attrs[]) { - struct context *c =3D arg; - struct nlattr *tb[OVPN_A_MAX + 1]; - struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); - msg(D_DCO_DEBUG, "%s: parsing message...", __func__); - nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0), - genlmsg_attrlen(gnlh, 0), NULL); - - if (!tb[OVPN_A_PEER]) + if (!attrs[OVPN_A_PEER]) { msg(D_DCO_DEBUG, "%s: malformed reply", __func__); return NL_SKIP; } struct nlattr *tb_peer[OVPN_A_PEER_MAX + 1]; - nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL); + nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL); if (!tb_peer[OVPN_A_PEER_ID]) { - msg(M_WARN, "%s: no peer-id provided in reply", __func__); + msg(M_WARN, "ovpn-dco: no peer-id provided in PEER_GET reply"); return NL_SKIP; } uint32_t peer_id =3D nla_get_u32(tb_peer[OVPN_A_PEER_ID]); - if (c->c2.tls_multi->dco_peer_id !=3D peer_id) + struct context_2 *c2; + + if (dco->ifmode =3D=3D OVPN_MODE_P2P) + { + c2 =3D &dco->c->c2; + } + else + { + struct multi_instance *mi =3D dco->c->multi->instances[peer_id]; + if (!mi) + { + msg(M_WARN, "%s: received data for a non-existing peer %u", __= func__, peer_id); + return NL_SKIP; + } + + c2 =3D &mi->context.c2; + } + + /* at this point this check should never fail for MP mode, + * but it's still fully valid for P2P mode + */ + if (c2->tls_multi->dco_peer_id !=3D peer_id) { return NL_SKIP; } - dco_update_peer_stat(&c->c2, tb_peer, peer_id); + dco_update_peer_stat(c2, tb_peer, peer_id); return NL_OK; } @@ -1120,9 +1135,22 @@ { dco_context_t *dco =3D arg; - struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); struct nlattr *attrs[OVPN_A_MAX + 1]; struct nlmsghdr *nlh =3D nlmsg_hdr(msg); + struct genlmsghdr *gnlh =3D genlmsg_hdr(nlh); + + msg(D_DCO_DEBUG, "ovpn-dco: received netlink message type=3D%u cmd=3D%= u flags=3D%#.4x", + nlh->nlmsg_type, gnlh->cmd, nlh->nlmsg_flags); + + /* if we get a message from the NLCTRL family, it means + * this is the reply to the mcast ID resolution request + * and we parse it accordingly. + */ + if (nlh->nlmsg_type =3D=3D dco->ctrlid) + { + msg(D_DCO_DEBUG, "ovpn-dco: received CTRLID message"); + return mcast_family_handler(msg, dco); + } if (!genlmsg_valid_hdr(nlh, 0)) { @@ -1146,6 +1174,21 @@ */ switch (gnlh->cmd) { + case OVPN_CMD_PEER_GET: + { + /* this message is part of a peer list dump, hence triggered + * by a MP/server instance + */ + if (nlh->nlmsg_flags & NLM_F_MULTI) + { + return ovpn_handle_peer_multi(dco, attrs); + } + else + { + return ovpn_handle_peer(dco, attrs); + } + } + case OVPN_CMD_PEER_DEL_NTF: { return ovpn_handle_peer_del_ntf(dco, attrs); @@ -1174,7 +1217,6 @@ dco_do_read(dco_context_t *dco) { msg(D_DCO_DEBUG, __func__); - nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco); return ovpn_nl_recvmsgs(dco, __func__); } @@ -1189,7 +1231,7 @@ nlmsg_hdr(nl_msg)->nlmsg_flags |=3D NLM_F_DUMP; - int ret =3D ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer_multi, m, __f= unc__); + int ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nlmsg_free(nl_msg); @@ -1227,7 +1269,7 @@ NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peer_id); nla_nest_end(nl_msg, attr); - ret =3D ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer, c, __func__); + ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__); nla_put_failure: nlmsg_free(nl_msg); diff --git a/src/openvpn/dco_linux.h b/src/openvpn/dco_linux.h index 5e61cf1..cc14f45 100644 --- a/src/openvpn/dco_linux.h +++ b/src/openvpn/dco_linux.h @@ -66,6 +66,7 @@ int status; struct context *c; + int ctrlid; enum ovpn_mode ifmode; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1100?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I23ad79e14844aefde9ece34dadef0b75ff267201 Gerrit-Change-Number: 1100 Gerrit-PatchSet: 6 Gerrit-Owner: ordex Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --kEfPAx8siSo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

dco_linux: fix async message reception
Currently whenever we send a PEER_GET request to ovpn, we also
set the = CB that is supposed to parse the reply.

However, due to the async na= ture of netlink messages, we could
get an unrelated notification, sent b= y ovpn (kernel) upon some event,
after userland has set the CB, but befo= re parsing the awaited reply.

When this happens, the notification is= then parsed with the
configured CB instead of the notification parser, = thus effectively
rejecting the notification and losing the event.
To fix this inconsistency, make ovpn_handle_msg() the default and
only = netlink parser CB. It is configured upon DCO initialization
and is never= removed.

ovpn_handle_msg() will check the message type and will cal= l the
corresponding handler. This way, no matter what message we get at<= br>what time, we'll always parse it correctly.

As a bonus we can= also simplify the nl_sendmsg() API as we
don't need to pass the cb = and its argument anymore.

The ID of the NLCTRL family is now also st= ored in the DCO
context as we need it to check when we receive a mcast I= D
lookup message.

Change-Id: I23ad79e14844aefde9ece34dadef0b75ff2= 67201
Github: closes OpenVPN/openvpn#793
Signed-off-by: Antonio Quart= ulli <antonio@...2726...>
Acked-by: Gert Doering <gert@...1725...= ..>
Message-Id: <20250725172708.19456-1-gert@...1296...>
URL= : https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32339= .html
Signed-off-by: Gert Doering <gert@...1296...>
---
M sr= c/openvpn/dco_linux.c
M src/openvpn/dco_linux.h
2 files changed, 110 = insertions(+), 67 deletions(-)

diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.cindex 7c639d9..728fb7e 100644
--- a/src/openvpn/dco_= linux.c
+++ b/src/openvpn/dco_linux.c
@@ -1= 67,23 +167,19 @@
}

/**<= /span>
- * Send a prepared net= link message and registers cb as callback if non-null.
+ * Send a prepared netlink message.
*
* The method will also free nl_msg=
* @param dco The dco context to use
* @= param nl_msg the message to use
- * @param cb An optional callback if the caller expects= an answer
- * @param c= b_arg An optional param to pass to the callback
* @par= am prefix A prefix to report in the error message to give the user conte= xt
* @return status of sending the message
*/
static int
-ovpn_nl_msg_send(dco_context_t *dco, struct nl_msg *n= l_msg, ovpn_nl_cb cb,
-= void *cb_arg, const char *prefix)
+ovpn_nl_msg_send(dco_context_t *dco, struct = nl_msg *nl_msg, const char *prefix)
{
= dco->status =3D 1;

- nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, = cb, cb_arg);
nl_send_auto(dco->nl_sock, nl_msg);

while (dco->status =3D=3D 1)=
@@ -285,7 +281,7 @@
}
n= la_nest_end(nl_msg, attr);

- ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL= , __func__);
+ ret= =3D ovpn_nl_msg_send(dco, nl_msg, __func__);

<= span> nla_put_failure:

nlmsg_free(nl_msg);
<= span>@@ -385,6 +381,29 @@

}

static void

+ovp= n_dco_register(dco_context_t *dco)
+{
+ = msg(D_DCO_DEBUG, __func__);
+ ovpn_get_mcast_id(dco);
+
+ = if (dco->ovpn_dco_mcast_id < 0)
+ {
+ msg(M_FATAL, "cannot get mcast group: %s", nl_gete= rror(dco->ovpn_dco_mcast_id));
+ }
+=
+ /* Register for= ovpn-dco specific multicast messages that the kernel may
+ * send
+ */
+ int ret =3D nl_socket_add_membership(dco->nl_sock,= dco->ovpn_dco_mcast_id);
+ if (ret)
+ {
+ msg(= M_FATAL, "%s: failed to join groups: %d", __func__, ret);<= br>+ }
+}
+
+sta= tic int ovpn_handle_msg(struct nl_msg *msg, void *arg);
+
+static void
ovpn_dco_init_netlink(dco_con= text_t *dco)
{
dco->ovpn_dco_id = =3D resolve_ovpn_netlink_id(M_FATAL);
@@ -420,11 +439,15 @@=

nl_socket_set_cb(dco->nl_sock, d= co->nl_cb);

+ dco->dco_message_peer_id =3D -1;
n= l_cb_err(dco->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &dco->status= );
nl_cb_set(dco->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM= , ovpn_nl_cb_finish,
&dco->status);
nl_cb_set(dco->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_= nl_cb_finish,
&dco->status);+ nl_cb_set(dco->nl_cb,= NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco);
+
+ ovpn_dco_register(dco);

= /* The async PACKET messages confuse libnl and it will drop them with
* wrong sequence numbers (NLE_SEQ_MISMATCH), so disabl= e libnl's sequence
@@ -476,27 +499,6 @@
CLEAR(dco);

}

-static void
-ovpn_dco_register(dco_context_t *dco)
-{

- msg(D_DCO_DEBUG, __func__);
- ovpn_get_mcast_id(dco);
-
- if (dco->ovpn_dco_mcast_id < 0)
- {
- msg(M_FATAL, "cannot get mcast group: %s&quo= t;, nl_geterror(dco->ovpn_dco_mcast_id));
- }
-
- /* Regist= er for ovpn-dco specific multicast messages that the kernel may
<= span style=3D"color: hsl(0, 100%, 40%);">- * send

- */
- int ret =3D nl_socket_add_membership(dco->nl_sock,= dco->ovpn_dco_mcast_id);
- if (ret)
- = {
- msg(M_FATA= L, "%s: failed to join groups: %d", __func__, ret);
- }
-}
-
int
open_tun_dco(struct tuntap *tt, = openvpn_net_ctx_t *ctx, const char *dev)
{
@@ -516,10 +518,6 @@
msg(M_FATAL, "DCO: cann= ot retrieve ifindex for interface %s", dev);
}

- tt= ->dco.dco_message_peer_id =3D -1;
-
- ov= pn_dco_register(&tt->dco);
-
return 0;
}

@@ -548,7 +546,7 @@
NLA_PUT_U32(= nl_msg, OVPN_A_KEYCONF_PEER_ID, peerid);
nla_nest_end(= nl_msg, attr);

- ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);=
+ ret =3D ovpn_nl= _msg_send(dco, nl_msg, __func__);

nla_pu= t_failure:
nlmsg_free(nl_msg);
@@ -572= ,7 +570,7 @@
NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peeri= d);
nla_nest_end(nl_msg, attr);

- ret =3D ovpn_nl_msg_se= nd(dco, nl_msg, NULL, NULL, __func__);
+ ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__);


nla_put_failure:
nlmsg= _free(nl_msg);
@@ -598,7 +596,7 @@
NLA= _PUT_U32(nl_msg, OVPN_A_KEYCONF_SLOT, slot);
nla_nest_= end(nl_msg, keyconf);

- ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __f= unc__);
+ ret =3D = ovpn_nl_msg_send(dco, nl_msg, __func__);

= nla_put_failure:
nlmsg_free(nl_msg);
= @@ -657,7 +655,7 @@
nla_nest_end(nl_msg, key_conf);


- ret =3D ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__)= ;
+ ret =3D ovpn_n= l_msg_send(dco, nl_msg, __func__);

nla_p= ut_failure:
nlmsg_free(nl_msg);
@@ -68= 6,7 +684,7 @@
keepalive_timeout); nla_nest_end(nl_msg, attr);

- ret =3D ovpn_nl_msg_send(dco, nl_m= sg, NULL, NULL, __func__);
+ ret =3D ovpn_nl_msg_send(dco, nl_msg, __func__);

nla_put_failure:
nlmsg_free(nl_msg= );
@@ -754,7 +752,7 @@

= /* Even though 'nlctrl' is a constant, there seem to be no libra= ry
* provided define for it */
- int ctrlid =3D genl_ctrl_resolve(dco-&g= t;nl_sock, "nlctrl");
+ dco->ctrlid =3D genl_ctrl_resolve(dco->nl_sock, "= ;nlctrl");

struct nl_msg *nl_ms= g =3D nlmsg_alloc();
if (!nl_msg)
@@ -= 762,12 +760,12 @@
return -ENOMEM;
= }

+ genlmsg_put(nl_msg, = 0, 0, dco->ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);

int ret =3D -EMSGSIZE;
NLA_PUT_STRING(= nl_msg, CTRL_ATTR_FAMILY_NAME, OVPN_FAMILY_NAME);
<= br>- ret =3D ovpn_nl_msg_send(= dco, nl_msg, mcast_family_handler, dco, __func__);
+ ret =3D ovpn_nl_msg_send(dco, nl_msg, __= func__);

nla_put_failure:
nlmsg_free(nl_msg);

@@ -879,31 +877,34 @@
= }

static int
-dco_parse_peer_multi(struct nl_msg *msg, v= oid *arg)
+ovpn_handl= e_peer_multi(dco_context_t *dco, struct nlattr *attrs[])
{=
- struct nlattr *tb= [OVPN_A_MAX + 1];
- = struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg));
-
msg(D_DCO_DEBUG, &q= uot;%s: parsing message...", __func__);

- nla_parse(tb, OVPN_A_MAX, genl= msg_attrdata(gnlh, 0),
= - genlmsg_attrlen(gnlh, 0), NULL);
+ /* this function assumes openvpn is running= in multipeer mode as
+ * it accesses c->multi
+ */
= + if (dco->ifmode !=3D OVPN_MODE_MP)
+ {
+ msg(M_WARN, "%s: can't parse 'multi-peer'= message on P2P instance", __func__);
+ return NL_SKIP;
+ }

- if (!tb[OVPN_A_PEER])
+ if (!attrs[OVPN_A_PEER])
<= span> {
return NL_SKIP;
}=

struct nlattr *tb_peer[OVPN_A_PEER_= MAX + 1];
- nla_pars= e_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL);
+ nla_parse_nested(tb_peer, OVPN_A= _PEER_MAX, attrs[OVPN_A_PEER], NULL);

= if (!tb_peer[OVPN_A_PEER_ID])
{
- msg(M_WARN, "%s: no peer-id p= rovided in reply", __func__);
+ msg(M_WARN, "ovpn-dco: no peer-id provided in (= MULTI) PEER_GET reply");
return NL_SKIP;
}

- struct multi_context *m =3D arg;
+ struct multi_context *m =3D dco->c= ->multi;
uint32_t peer_id =3D nla_get_u32(tb_peer[O= VPN_A_PEER_ID]);

if (peer_id >=3D= m->max_clients || !m->instances[peer_id])
@@ -919,39= +920,53 @@
}

static in= t
-dco_parse_peer(struc= t nl_msg *msg, void *arg)
+ovpn_handle_peer(dco_context_t *dco, struct nlattr *attrs[]) {
- struct = context *c =3D arg;
- = struct nlattr *tb[OVPN_A_MAX + 1];
- struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg));<= /span>
-
= msg(D_DCO_DEBUG, "%s: parsing message...", __func__);
<= span>

- nla_parse(t= b, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0),
- genlmsg_attrlen(gnlh, 0), NULL);-
- if (!tb[OVPN_A_PEER])
+ if (!attrs[OVPN_A_PEER])
= {
msg(D_DCO_DEBUG, "%s: malformed reply&= quot;, __func__);
return NL_SKIP;
= }

struct nlattr *tb_peer[OVPN_A= _PEER_MAX + 1];
- nl= a_parse_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL);
= + nla_parse_nested(tb_peer, = OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL);

if (!tb_peer[OVPN_A_PEER_ID])

{
- msg(M_WARN, "%s: no pee= r-id provided in reply", __func__);
+ msg(M_WARN, "ovpn-dco: no peer-id provide= d in PEER_GET reply");
return NL_SKIP;=
}

uint32_t peer_id = =3D nla_get_u32(tb_peer[OVPN_A_PEER_ID]);
- if (c->c2.tls_multi->dco_peer_id !=3D peer_id= )
+ struct context= _2 *c2;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (dco->ifmode =3D=3D O= VPN_MODE_P2P)
+ {<= /span>
+ c2 =3D &= dco->c->c2;
+ = }
+ else<= br>+ {
+ struct multi_instance *mi =3D dco= ->c->multi->instances[peer_id];
+ if (!mi)
+ {
+ msg(M_WARN, "%s: received data for a non-existing = peer %u", __func__, peer_id);
+ return NL_SKIP;
+ }
+
+ = c2 =3D &mi->context.c2;
+ }
+<= /span>
+ /* at this point= this check should never fail for MP mode,
+ * but it's still fully valid for P2P mode
+ */
+ if (c2->tls_multi->dco_= peer_id !=3D peer_id)

{
retur= n NL_SKIP;
}

- dco_update_peer_stat(&c->c2, tb_= peer, peer_id);
+ = dco_update_peer_stat(c2, tb_peer, peer_id);

return NL_OK;

}
@@ -1120,9 +1135,2= 2 @@
{
dco_context_t *dco =3D arg;


- st= ruct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg));
= struct nlattr *attrs[OVPN_A_MAX + 1];
struct nlmsghdr = *nlh =3D nlmsg_hdr(msg);
+ struct genlmsghdr *gnlh =3D genlmsg_hdr(nlh);
+
+ msg(D_DCO_DEBUG, "ovpn-dco: received netlink messa= ge type=3D%u cmd=3D%u flags=3D%#.4x",
+ nlh->nlmsg_type, gnlh->cmd, nlh->nl= msg_flags);
+<= br>+ /* if we get a message = from the NLCTRL family, it means
+ * this is the reply to the mcast ID resolution request
+ * and we parse it= accordingly.
+ *= /
+ if (nlh->nl= msg_type =3D=3D dco->ctrlid)
+ {
+ = msg(D_DCO_DEBUG, "ovpn-dco: received CTRLID message");
+ return mcast_fam= ily_handler(msg, dco);
+ case= OVPN_CMD_PEER_GET:
+= {
+ = /* this message is part of a peer list dump, hence triggered
+ * by a MP/server in= stance

+ = */
+ if (n= lh->nlmsg_flags & NLM_F_MULTI)
+ {
+ return ovpn_handle_peer_multi(dco, attrs);
+ }
= + else
+ {

+ return ovpn_handle_peer= (dco, attrs);
+ = }
+ }
+
= case OVPN_CMD_PEER_DEL_NTF:
{
= return ovpn_handle_peer_del_ntf(dco, attrs);
@= @ -1174,7 +1217,6 @@
dco_do_read(dco_context_t *dco)
{
msg(D_DCO_DEBUG, __func__);
- nl_cb_set(dco->nl_cb, NL_CB= _VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco);


return ovpn_nl_recvmsgs(dco, __func__);

}<= br>@@ -1189,7 +1231,7 @@

nlmsg= _hdr(nl_msg)->nlmsg_flags |=3D NLM_F_DUMP;

<= span style=3D"color: hsl(0, 100%, 40%);">- int ret =3D ovpn_nl_msg_send(= dco, nl_msg, dco_parse_peer_multi, m, __func__);

+ int ret =3D ovpn_nl_msg_send(dco, nl_msg, = __func__);

nlmsg_free(nl_msg);

@@ -1227,7 +1269,7 @@
NLA= _PUT_U32(nl_msg, OVPN_A_PEER_ID, peer_id);
nla_nest_en= d(nl_msg, attr);

- ret =3D ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer, c, _= _func__);
+ ret = =3D ovpn_nl_msg_send(dco, nl_msg, __func__);

nla_put_failure:

nlmsg_free(nl_msg);
diff --git a/src/openvpn/dco_linux.h b/src/openvpn/dco_linux.hindex 5e61cf1..cc14f45 100644
--- a/src/openvpn/dco= _linux.h
+++ b/src/openvpn/dco_linux.h
@@ -= 66,6 +66,7 @@
int status;

= struct context *c;
+ int ctrlid;

enum ovpn_= mode ifmode;


To view,= visit c= hange 1100. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I23ad79e14844aefde9ece34dade= f0b75ff267201
Gerrit-Change-Number: 1100
Gerrit-PatchSet: 6
Gerrit-Owner: ordex <antonio@...2726...>=
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-MessageType: merged
--kEfPAx8siSo=--