From: "ordex (Code Review)" Date: Wed, 23 Jul 2025 09:59:41 +0000 In-Reply-To: References: Reply-To: antonio@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="qHdP6SgZaGM="; 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: , To: plaisthos , flichtenheld Cc: openvpn-devel --qHdP6SgZaGM= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1100?usp=3Demail to look at the new patch set (#3). 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 upon some event, after having 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 according parser. This way, no matter what message we get at what time, but 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: OpenVPN/openvpn#793 Signed-off-by: Antonio Quartulli --- M src/openvpn/dco_linux.c M src/openvpn/dco_linux.h 2 files changed, 130 insertions(+), 85 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/00/1100/3 diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index 0a22879..62052f3 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -172,18 +172,14 @@ * 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,23 +877,26 @@ } 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]) { @@ -903,7 +904,7 @@ 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,25 +920,18 @@ } 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]) { @@ -946,18 +940,39 @@ } 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; } -static int -ovpn_handle_peer_del_ntf(dco_context_t *dco, struct nlattr *attrs[]) +static bool +ovpn_iface_check(dco_context_t *dco, struct nlattr *attrs[]) { /* we must know which interface this message is referring to in order = to * avoid mixing messages for other instances @@ -965,6 +980,25 @@ if (!attrs[OVPN_A_IFINDEX]) { msg(D_DCO, "ovpn-dco: Received message without ifindex"); + return false; + } + + uint32_t ifindex =3D nla_get_u32(attrs[OVPN_A_IFINDEX]); + if (ifindex !=3D dco->ifindex) + { + msg(D_DCO_DEBUG, + "ovpn-dco: ignoring message for foreign ifindex %d", ifindex); + return false; + } + + return true; +} + +static int +ovpn_handle_peer_del_ntf(dco_context_t *dco, struct nlattr *attrs[]) +{ + if (!ovpn_iface_check(dco, attrs)) + { return NL_STOP; } @@ -1008,12 +1042,8 @@ static int ovpn_handle_peer_float_ntf(dco_context_t *dco, struct nlattr *attrs[]) { - /* we must know which interface this message is referring to in order = to - * avoid mixing messages for other instances - */ - if (!attrs[OVPN_A_IFINDEX]) + if (!ovpn_iface_check(dco, attrs)) { - msg(D_DCO, "ovpn-dco: Received message without ifindex"); return NL_STOP; } @@ -1058,12 +1088,8 @@ static int ovpn_handle_key_swap_ntf(dco_context_t *dco, struct nlattr *attrs[]) { - /* we must know which interface this message is referring to in order = to - * avoid mixing messages for other instances - */ - if (!attrs[OVPN_A_IFINDEX]) + if (!ovpn_iface_check(dco, attrs)) { - msg(D_DCO, "ovpn-dco: Received message without ifindex"); return NL_STOP; } @@ -1109,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)) { @@ -1126,15 +1165,6 @@ return NL_STOP; } - uint32_t ifindex =3D nla_get_u32(attrs[OVPN_A_IFINDEX]); - if (ifindex !=3D dco->ifindex) - { - msg(D_DCO_DEBUG, - "ovpn-dco: ignoring message (type=3D%d) for foreign ifindex %d= ", - gnlh->cmd, ifindex); - return NL_STOP; - } - /* based on the message type, we parse the subobject contained in the * message, that stores the type-specific attributes. * @@ -1144,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); @@ -1172,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__); } @@ -1187,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); @@ -1225,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: 3 Gerrit-Owner: ordex Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --qHdP6SgZaGM= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

ordex uploaded patch set #3 to 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 pars= e the reply.

However, due to the async nature of netlink messages, w= e could
get an unrelated notification, sent by ovpn upon some event,
= after having set the CB, but before parsing the awaited reply.

When = this happens, the notification is then parsed with the
configured CB ins= tead of the notification parser, thus effectively
rejecting the notifica= tion and losing the event.

To fix this inconsistency, make ovpn_hand= le_msg() the default and
only netlink parser CB. It is configured upon D= CO initialization
and is never removed.

ovpn_handle_msg() will ch= eck the message type and will call the
according parser. This way, no ma= tter what message we get at
what time, but we'll always parse it cor= rectly.

As a bonus we can also simplify the nl_sendmsg() API as wedon'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: I23a= d79e14844aefde9ece34dadef0b75ff267201
Github: OpenVPN/openvpn#793
Sig= ned-off-by: Antonio Quartulli <antonio@...2726...>
---
M src/op= envpn/dco_linux.c
M src/openvpn/dco_linux.h
2 files changed, 130 inse= rtions(+), 85 deletions(-)

git pull ssh://gerrit.openvpn.net:2941=
8/openvpn refs/changes/00/1100/3
diff --git a/src/openvpn/dco_linux=
.c b/src/openvpn/dco_linux.c
index 0a22879..62052f3 100644<= /span>
--- a/src/openvpn/dco_linux.c
+++ b/src/ope= nvpn/dco_linux.c
@@ -172,18 +172,14 @@
* = The method will also free nl_msg
* @param dco The d= co 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 t= o the callback
* @param prefix A prefix to report in t= he error message to give the user context
* @return = status of sending the message
*/
sta= tic int
-ovpn_nl_msg_se= nd(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 *prefix)<= /span>
{
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 o= vpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
+ ret =3D ovpn_nl_msg_send(dco, nl_msg,= __func__);

nla_put_failure:
<= span> 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 &l= t; 0)
+ {+ msg(M_FATAL, "c= annot get mcast group: %s", nl_geterror(dco->ovpn_dco_mcast_id));<= /span>
+ }
+

+ /* Register for ovpn-dco specific multicast messa= ges 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);
+ }
+= }
+
+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(dc= o->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish,
&dco->status);

nl_cb_set(dco-&g= t;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_han= dle_msg, dco);
+
+ ovpn_dco_register(dc= o);

/* The async PACKET messages con= fuse libnl and it will drop them with
* wrong sequenc= e numbers (NLE_SEQ_MISMATCH), so disable libnl's sequence
@@ -476,27 +499,6 @@

CLEAR(dco);
}=

-sta= tic void
-ovpn_dco_regi= ster(dco_context_t *dco)
- msg(D_DCO_DEB= UG, __func__);
- ovp= n_get_mcast_id(dco);
-<= /span>
- if (dco->ovpn_d= co_mcast_id < 0)
- = {
- msg(M_FATA= L, "cannot get mcast group: %s", nl_geterror(dco->ovpn_dco_mc= ast_id));
- }=
-
- /* Register for ovpn-dco specific multicast m= essages that the kernel may
- * send
- = */
- int ret =3D nl= _socket_add_membership(dco->nl_sock, dco->ovpn_dco_mcast_id);<= br>- 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);
-
re= turn 0;
}

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

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

nla_put_failure:
nlms= g_free(nl_msg);
@@ -572,7 +570,7 @@
NL= A_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid);
nla_nest_en= d(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);
@@ -5= 98,7 +596,7 @@
NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_SLOT= , slot);
nla_nest_end(nl_msg, keyconf);

- ret =3D ovpn_n= l_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:
nlm= sg_free(nl_msg);
@@ -686,7 +684,7 @@
= keepalive_timeout);
nla_nest_end(nl_msg, at= tr);

= - 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' i= s a constant, there seem to be no library
* provided = define for it */
- i= nt ctrlid =3D genl_ctrl_resolve(dco->nl_sock, "nlctrl");
+ dco->ctrlid =3D ge= nl_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, ctrl= id, 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_F= AMILY_NAME);

- ret =3D ovpn_nl_msg_send(dco, nl_msg, mcast_family_handler, dc= o, __func__);
+ re= t =3D ovpn_nl_msg_send(dco, nl_msg, __func__);

= nla_put_failure:
nlmsg_free(nl_msg);
= @@ -879,23 +877,26 @@
}

= static int
-dco_= parse_peer_multi(struct nl_msg *msg, void *arg)
+ovpn_handle_peer_multi(dco_context_t *dco, stru= ct nlattr *attrs[])
{
- struct nlattr *tb[OVPN_A_MAX + 1];
- struct genlmsghdr *gnlh =3D nlmsg_dat= a(nlmsg_hdr(msg));
-
msg(D_DCO_DEBUG, "%s: parsing message...", __f= unc__);

- nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0),
- genlmsg_attrlen(gnlh, = 0), NULL);

+ /* th= is function assumes openvpn is running in multipeer mode as
+ * it accesses c->multi
+ */
+ if (dco->ifmode !=3D OVPN_MOD= E_MP)
+ {+ msg(M_WARN, "%s= : can't parse 'multi-peer' message on P2P instance", __fun= c__);
+ 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])

{
@@ -903,7 +904,7 @@
= return NL_SKIP;
}

- struct multi_context *m =3D arg;=

+ struct multi_co= ntext *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,25 +920,18 @@
}

static int
-dco_parse_peer(struct nl_msg *msg, void *arg)
+ovpn_handle_peer(dco_context_t *dco, struct nla= ttr *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),
<= span style=3D"color: hsl(0, 100%, 40%);">- genlmsg_attrlen(gnl= h, 0), NULL);

-<= br>- if (!tb[OVPN_A_PEER])
+ if (!attrs[OVPN_A_P= EER])
{
msg(D_DCO_DEBUG, &quo= t;%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_PE= ER], NULL);
+ nla_= parse_nested(tb_peer, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL);
if (!tb_peer[OVPN_A_PEER_ID])
{

@@ -946,18 +940,39 @@
}=

uint32_t peer_id =3D nla_get_u32(tb_peer[O= VPN_A_PEER_ID]);
- i= f (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)
+ = {
+ m= sg(M_WARN, "%s: received data for a non-existing peer %u", __func= __, peer_id);
+ = return NL_SKIP;
+ }
+<= br>+ c2 =3D &mi->= context.c2;
+ }
+
+ /* at this point this check should ne= ver 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(c= 2, tb_peer, peer_id);

return NL_OK;<= /span>
}

-static int
-ovpn_handle_peer_del_ntf(dco_context_t *dco, struct nlattr *attrs[])
+static bool
= +ovpn_iface_check(dco_context_t= *dco, struct nlattr *attrs[])
{
/* w= e must know which interface this message is referring to in order to=
* avoid mixing messages for other instances
@@ -965,6 +980,25 @@

if (!attrs[OVPN_A_IFINDEX])
{
msg(D_DCO, "ovpn-dco: Rece= ived message without ifindex");
+ return false;
+ }
+
+ uint32_t i= findex =3D nla_get_u32(attrs[OVPN_A_IFINDEX]);
+ if (ifindex !=3D dco->ifindex)
= + {
+ msg(D_DCO_DEBUG,
+ "ovpn-dco: ignoring me= ssage for foreign ifindex %d", ifindex);
+ return false;
+ }
+
+ r= eturn true;
+}=
+
+static int
+ovpn_handle_peer_del_ntf(dco_context_t *dco, struct nl= attr *attrs[])
+{
+ if (!ovpn_iface_che= ck(dco, attrs))
+ = {
return NL_STOP;
}
@@ -1008,12 +1042,8 @@
static int=
ovpn_handle_peer_float_ntf(dco_context_t *dco, struct nla= ttr *attrs[])
{
- /* we must know which interface this message is referring to= in order to
- * av= oid mixing messages for other instances
- */
- if (!attrs[OVPN_A_IFINDEX])
+ if (!ovpn_iface_check(dco, attrs))
= {
- msg(D_DCO, = "ovpn-dco: Received message without ifindex");
= return NL_STOP;
}

<= span>@@ -1058,12 +1088,8 @@

static int
ov= pn_handle_key_swap_ntf(dco_context_t *dco, struct nlattr *attrs[]) {
- /* we m= ust know which interface this message is referring to in order to- * avoid mixing messages fo= r other instances
- = */
- if (!attrs[OVP= N_A_IFINDEX])
+ if= (!ovpn_iface_check(dco, attrs))
{
- msg(D_DCO, "ovpn-dco: Receiv= ed message without ifindex");
return NL_STOP;=
}

@@ -1109,9 +1135,= 22 @@
{
dco_context_t *dco =3D arg;

- s= truct 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 mess= age type=3D%u cmd=3D%u flags=3D%#.4x",
+ nlh->nlmsg_type, gnlh->cmd, nlh->n= lmsg_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 i= t accordingly.
+ = */
+ if (nlh->n= lmsg_type =3D=3D dco->ctrlid)
+ {
+ = msg(D_DCO_DEBUG, "ovpn-dco: received CTRLID message");
+ return mcast_fa= mily_handler(msg, dco);
+ }

if (!genlmsg_valid_hdr(nlh= , 0))
{
@@ -1126,15 +1165,6 @@<= br> return NL_STOP;
}
<= /span>
- uint32_t ifindex = =3D nla_get_u32(attrs[OVPN_A_IFINDEX]);
- if (ifindex !=3D dco->ifindex)
- {
- msg(D_DCO_DEBUG,
- "ovpn-dco: ignoring message (type=3D%= d) for foreign ifindex %d",
- gnlh->cmd, ifindex);
- return NL_STOP;
- }
-
/* based on the message type, we parse the= subobject contained in the
* message, that stores th= e type-specific attributes.
*
@@ -114= 4,6 +1174,21 @@
*/
switch (gnlh-= >cmd)
{
+ case OVPN_CMD_PEER_GET:
+ {
+ /* this message is part of a peer list dump, henc= e 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, a= ttrs);
@@ -1172,7 +1217,6 @@
dco_do_read(d= co_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__);
}
@@ -1187,7 +1231,7 @@

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

- int r= et =3D ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer_multi, m, __func__);
+ int ret =3D ovpn_n= l_msg_send(dco, nl_msg, __func__);

n= lmsg_free(nl_msg);

@@ -1225,7 +1269,7 @@<= /span>
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_m= sg, 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/openvp= n/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;
<= br> enum ovpn_mode ifmode;


To view, visit change 1100. To unsubscribe, or for help writing= mail filters, visit setting= s.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I23ad79e14844aefde9ece34dade= f0b75ff267201
Gerrit-Change-Number: 1100
Gerrit-PatchSet: 3
Gerrit-Owner: ordex <antonio@...2726...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--qHdP6SgZaGM=--