From patchwork Sun Jul 27 10:22:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4330 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:3644:b0:671:5a2c:6455 with SMTP id a4csp2459465mai; Sun, 27 Jul 2025 03:22:58 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXbxDCR2iSmsPAqejC4kRVu/XRD7MewRWEAjLdTEJ2zmdsqoR646wJ8RRiZis6Suqcq97c1U9XzNSA=@openvpn.net X-Google-Smtp-Source: AGHT+IG6we2+ZSit7alZbl+jL6a+Ii3ZARly4QKqv6dFnBGNiWfT74BC8HEL8yxmAOHlzMECwodx X-Received: by 2002:a05:6870:912b:b0:2e9:d485:5ad4 with SMTP id 586e51a60fabf-30701ffd2a2mr4585695fac.33.1753611778254; Sun, 27 Jul 2025 03:22:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1753611778; cv=none; d=google.com; s=arc-20240605; b=WjAwNGL5AmW0ztbjxVS2pVxoflUROPpufxddEFQaT3APlPk4tDG6xu1xje6T66Pp+4 XRkTJBr4KZ8q+TzVlEZTPOC8TMisclXuMxWe51tV3iAtKTV77T4Bo5FfZzGl4agCv/nd 2wuLzNDSqwH0+CX6Qa/Xi55u/UN0lGODM1gsCCCoiiMvf4eFfBU9Jti8TmMGZ01X9Eij Gc2MHrtlcSm1HZvUGRSTIUbJcJTlhEb5h0eDy7GjfZGTrBXJQTVS8SogTQPTwtAQlJGY Nn+3kySxsJV7WO1+AUsqA2jRhdOgctUE86DlOjzxADwHX6pA8LvmIBgRn+MkIdcGoPxk 9lJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=Mebrkq5/a2j00GsIwpu+1HZXlGD5InIb87emxZR1D0g=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=WcV1DVGoSNjJaCanfUsX0Gtei7aLODiROUnl3ajgSQ+oiUOMSJBzKUwpjLZGEn/T+D DTemhKFOhg7CFbts1YOAVs65BoE4GF8NQFSq2LUS1nevVRP/hf3Kt5d4Z3kBfPuHxFeZ GEwUtz6akY37qUhq4TVgtNwPbeckgSO5yiSuKwFrorAkgEzWveRDo1GMkF0J9ftAx5AL srFZkh57RwyDwoEU7Hqoz87UScY4oZjkSDcVmIe5B9cBQ4PnpSvb85KsTTMEHegZ+6D8 1vGovbaFe45CNBjtEGBauXTAQ5xSNNLNYhUHL2+Yj16kvk49Xe3R1QWqt8yOX/9k/NPZ Y4mQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=Xc+RXZAz; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=W3MCwxaI; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YYOWjVIO; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 46e09a7af769-741483afa69si1389940a34.369.2025.07.27.03.22.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 Jul 2025 03:22:57 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=Xc+RXZAz; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=W3MCwxaI; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YYOWjVIO; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Mebrkq5/a2j00GsIwpu+1HZXlGD5InIb87emxZR1D0g=; b=Xc+RXZAz4JBxIcLJhG+6Mv5Ba/ DMAspYxVVyxJBNHH5wj2xkOkq011CfRbaoaZdfXWabj75HCAeaKz3Z09QUUg31SMTFfk7i9kNY/nn 7dWNoldFP6qDuIkKjDdmKpmlM9H2rfz9vAUeda0pyRH7m+FFesbf010oNOkc+inlVNG8=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1ufyWs-000803-Hn; Sun, 27 Jul 2025 10:22:55 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1ufyWr-0007zq-Ha for openvpn-devel@lists.sourceforge.net; Sun, 27 Jul 2025 10:22:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=cM8aPCoNn0O6glN1u2sHnVSeDwJQ5jbNj/nMRCzBTFA=; b=W3MCwxaImcybc4jFwDLJgxFOjG DwFIflaFfdLrXI0Tp8aTSuDpFHYMZMlIrdNn1+/alOc9i0Lg/pZF0syFdcnJhO9jkwjYiTgkonaia +LPpsqgY5CIDyceed1MHiAdK+qf9l1xysTphUGQQuulmbvrbjVBFGbIVBlsGOCHcyYA0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=cM8aPCoNn0O6glN1u2sHnVSeDwJQ5jbNj/nMRCzBTFA=; b=YYOWjVIO8VK0us6doo0aNfp8ZM 7ohEVlFc9Kn/mj2lqLqp6iyJMszR8ByahF5EWzMdGLI89h1BPtT6DQhid2v/j1F0+qNuYdDy4469g TZIgRFDR4P8nvcPRfC8erL/rKw9mzBnc6nz8BTz+owl1N66LCO/iV5bquZPyBZYInwG8=; Received: from [193.149.48.143] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1ufyWq-0003BJ-Ru for openvpn-devel@lists.sourceforge.net; Sun, 27 Jul 2025 10:22:54 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.17.1.9/8.17.1.9) with ESMTP id 56RAMkPW024957 for ; Sun, 27 Jul 2025 12:22:46 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.17.1.9/8.17.1.9/Submit) id 56RAMkMX024955 for openvpn-devel@lists.sourceforge.net; Sun, 27 Jul 2025 12:22:46 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Sun, 27 Jul 2025 12:22:40 +0200 Message-ID: <20250727102245.24931-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-2.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Antonio Quartulli This patch is intended to reduce code duplication and cleanup the DCO code around the PEER_GET command. Specifically it: * unified PEER_GET reply parser for `multi` and `non-multi` case * unified PEER_GET request trigger for `multi` and `non-multi` case * dropped struct multi_context from the argument l [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1ufyWq-0003BJ-Ru Subject: [Openvpn-devel] [PATCH v3] dco_linux: clean up PEER_GET trigger and parser X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1838795223919766815?= X-GMAIL-MSGID: =?utf-8?q?1838795223919766815?= From: Antonio Quartulli This patch is intended to reduce code duplication and cleanup the DCO code around the PEER_GET command. Specifically it: * unified PEER_GET reply parser for `multi` and `non-multi` case * unified PEER_GET request trigger for `multi` and `non-multi` case * dropped struct multi_context from the argument list of dco_get_peer_stats_multi() Github: closes OpenVPN/openvpn#800 Change-Id: Icbc70225d53ca678b8c22ed437b424c16e199d66 Signed-off-by: Antonio Quartulli Acked-by: Gert Doering --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1114 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h index 9078417..2ce0eb1 100644 --- a/src/openvpn/dco.h +++ b/src/openvpn/dco.h @@ -230,11 +230,9 @@ * Update traffic statistics for all peers * * @param dco DCO device context - * @param m the server context * @param raise_sigusr1_on_err whether to raise SIGUSR1 on error **/ -int dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, - const bool raise_sigusr1_on_err); +int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err); /** * Update traffic statistics for single peer @@ -374,8 +372,7 @@ } static inline int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, - const bool raise_sigusr1_on_err) +dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err) { return 0; } diff --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c index 98d8fb5..78ee9a1 100644 --- a/src/openvpn/dco_freebsd.c +++ b/src/openvpn/dco_freebsd.c @@ -167,6 +167,8 @@ bool ovpn_dco_init(struct context *c) { + c->c1.tuntap->dco.c = c; + if (open_fd(&c->c1.tuntap->dco) < 0) { msg(M_ERR, "Failed to open socket"); @@ -713,8 +715,7 @@ } int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, - const bool raise_sigusr1_on_err) +dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err) { struct ifdrv drv; @@ -774,7 +775,7 @@ const nvlist_t *peer = nvpeers[i]; uint32_t peerid = nvlist_get_number(peer, "peerid"); - dco_update_peer_stat(m, peerid, nvlist_get_nvlist(peer, "bytes")); + dco_update_peer_stat(dco->c->multi, peerid, nvlist_get_nvlist(peer, "bytes")); } nvlist_destroy(nvl); diff --git a/src/openvpn/dco_freebsd.h b/src/openvpn/dco_freebsd.h index e1a054e..e926af5 100644 --- a/src/openvpn/dco_freebsd.h +++ b/src/openvpn/dco_freebsd.h @@ -57,6 +57,8 @@ int dco_del_peer_reason; uint64_t dco_read_bytes; uint64_t dco_write_bytes; + + struct context *c; } dco_context_t; #endif /* defined(ENABLE_DCO) && defined(TARGET_FREEBSD) */ diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index 728fb7e..9ad3d51 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -877,53 +877,8 @@ } static int -ovpn_handle_peer_multi(dco_context_t *dco, struct nlattr *attrs[]) -{ - msg(D_DCO_DEBUG, "%s: parsing message...", __func__); - - /* this function assumes openvpn is running in multipeer mode as - * it accesses c->multi - */ - if (dco->ifmode != OVPN_MODE_MP) - { - msg(M_WARN, "%s: can't parse 'multi-peer' message on P2P instance", __func__); - return NL_SKIP; - } - - 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, attrs[OVPN_A_PEER], NULL); - - if (!tb_peer[OVPN_A_PEER_ID]) - { - msg(M_WARN, "ovpn-dco: no peer-id provided in (MULTI) PEER_GET reply"); - return NL_SKIP; - } - - struct multi_context *m = dco->c->multi; - uint32_t peer_id = nla_get_u32(tb_peer[OVPN_A_PEER_ID]); - - if (peer_id >= m->max_clients || !m->instances[peer_id]) - { - msg(M_WARN, "%s: cannot store DCO stats for peer %u", __func__, - peer_id); - return NL_SKIP; - } - - dco_update_peer_stat(&m->instances[peer_id]->context.c2, tb_peer, peer_id); - - return NL_OK; -} - -static int ovpn_handle_peer(dco_context_t *dco, struct nlattr *attrs[]) { - msg(D_DCO_DEBUG, "%s: parsing message...", __func__); - if (!attrs[OVPN_A_PEER]) { msg(D_DCO_DEBUG, "%s: malformed reply", __func__); @@ -942,12 +897,25 @@ uint32_t peer_id = nla_get_u32(tb_peer[OVPN_A_PEER_ID]); struct context_2 *c2; + msg(D_DCO_DEBUG, "%s: parsing message for peer %u...", __func__, peer_id); + if (dco->ifmode == OVPN_MODE_P2P) { c2 = &dco->c->c2; + if (c2->tls_multi->dco_peer_id != peer_id) + { + return NL_SKIP; + } } else { + if (peer_id >= dco->c->multi->max_clients) + { + msg(M_WARN, "%s: received out of bound peer_id %u (max=%u)", __func__, peer_id, + dco->c->multi->max_clients); + return NL_SKIP; + } + struct multi_instance *mi = dco->c->multi->instances[peer_id]; if (!mi) { @@ -958,14 +926,6 @@ c2 = &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 != peer_id) - { - return NL_SKIP; - } - dco_update_peer_stat(c2, tb_peer, peer_id); return NL_OK; @@ -1176,17 +1136,7 @@ { 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); - } + return ovpn_handle_peer(dco, attrs); } case OVPN_CMD_PEER_DEL_NTF: @@ -1221,52 +1171,32 @@ return ovpn_nl_recvmsgs(dco, __func__); } -int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, - const bool raise_sigusr1_on_err) +static int +dco_get_peer(dco_context_t *dco, int peer_id, const bool raise_sigusr1_on_err) { - msg(D_DCO_DEBUG, "%s", __func__); - - struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_GET); - - nlmsg_hdr(nl_msg)->nlmsg_flags |= NLM_F_DUMP; - - int ret = ovpn_nl_msg_send(dco, nl_msg, __func__); - - nlmsg_free(nl_msg); - - if (raise_sigusr1_on_err && ret < 0) - { - msg(M_WARN, "Error retrieving DCO peer stats: the underlying DCO peer" - "may have been deleted from the kernel without notifying " - "userspace. Restarting the session"); - register_signal(m->top.sig, SIGUSR1, "dco peer stats error"); - } - return ret; -} - -int -dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) -{ - int peer_id = c->c2.tls_multi->dco_peer_id; - if (peer_id == -1) + /* peer_id == -1 means "dump all peers", but this is allowed in MP mode only. + * If it happens in P2P mode it means that the DCO peer was deleted and we + * can simply bail out + */ + if (peer_id == -1 && dco->ifmode == OVPN_MODE_P2P) { return 0; } msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peer_id); - if (!c->c1.tuntap) - { - return 0; - } - - dco_context_t *dco = &c->c1.tuntap->dco; struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_GET); struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER); int ret = -EMSGSIZE; - NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peer_id); + if (peer_id != -1) + { + NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peer_id); + } + else + { + nlmsg_hdr(nl_msg)->nlmsg_flags |= NLM_F_DUMP; + } nla_nest_end(nl_msg, attr); ret = ovpn_nl_msg_send(dco, nl_msg, __func__); @@ -1279,11 +1209,23 @@ msg(M_WARN, "Error retrieving DCO peer stats: the underlying DCO peer" "may have been deleted from the kernel without notifying " "userspace. Restarting the session"); - register_signal(c->sig, SIGUSR1, "dco peer stats error"); + register_signal(dco->c->sig, SIGUSR1, "dco peer stats error"); } return ret; } +int +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) +{ + return dco_get_peer(&c->c1.tuntap->dco, c->c2.tls_multi->dco_peer_id, raise_sigusr1_on_err); +} + +int +dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err) +{ + return dco_get_peer(dco, -1, raise_sigusr1_on_err); +} + bool dco_available(int msglevel) { diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index e5a33a0..995b121 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -715,8 +715,7 @@ } int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, - const bool raise_sigusr1_on_err) +dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err) { /* Not implemented. */ return 0; diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index a62c57a..c5691ff 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -551,7 +551,7 @@ { if (dco_enabled(&m->top.options)) { - if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m, false) < 0) + if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, false) < 0) { return; } @@ -862,7 +862,7 @@ if (dco_enabled(&m->top.options)) { - if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m, true) < 0) + if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, true) < 0) { return; }