From patchwork Mon Oct 14 12:16:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Lichtenheld X-Patchwork-Id: 3889 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:1757:b0:5b9:581e:f939 with SMTP id c23csp1872086maw; Mon, 14 Oct 2024 05:16:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWyN8ylCsvtlySC76O3xDaZDfDXwrngykWu3u1LhGNKkb2/Ume2B4JvpSv41j0Rgjqz+hn5zgo+5Kw=@openvpn.net X-Google-Smtp-Source: AGHT+IEvrbLI1RY6HzzxUQL8xNO7a4RpDqsJwPCvhwvkN8iTAF8ThX77MyNr1KOnl/teokxykvIu X-Received: by 2002:a05:6808:30a6:b0:3df:a2f:4ad3 with SMTP id 5614622812f47-3e5c8f1bcacmr5456513b6e.11.1728908193213; Mon, 14 Oct 2024 05:16:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728908193; cv=none; d=google.com; s=arc-20240605; b=VQ4ijgiPG2eAXi1pyhMYR1FbdUXHd+hC+Fkk+p1wDKKBCiUNuZGpaw6Vfo36lEbj8M sLzdKA6o1rxHN8hQ3PPUMwICjKD/KQ9X7AdQjflJz3efDbwfsiOfd6ns7EbWQPxL1XEu V4wkVXman0epLSW1E6ZJl6HpK75EgM8Q6ptVig1FSp+J4zt6UzOOSqQFnTovQN68Gruw uo+FbmxoVUCLycCz2A/I1imHKR6Jk6vs1Trm1rWE9Ra1hQ8nG22ljtIbmJMChG7/uMYc ko/+5Tev4MuGhx4AgiowSC5SOJIGEyfsyaFeVf8RiAwr+ZIIlv7P3fND2sWKKWmvhpdf qbhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:cc: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=v5XLweJcvB0Ne8eyPiFuEnsCpTl4UckcCrnnNO6mqU8=; fh=yXAbqwSLX4AZy2IfjuXV6xw9sEGAlDO35mmlrsbkSnM=; b=UoMWLNNhN5OFKOZA2PojXH9TLXg3rstHLRoQwchQwEy5mEyP+MlZXeUd4M/m8FD7Tq ArhTF8U4rZNNRI2PYJ93a4RiOz/N7pQghYghwnCTM+uocEls7QMxkYEnqhJvpbp+KfwW MtgYdDIoSNe+v7RFBU/8q+ti9FXnSKac9HV9TABkW9uVF+hR1uFBc1HppxvDffDhfAI5 H00ukkN65c99V5M2VbuB56IltoXZSdyB7LeEUFIdVXtLngmg2rl/H+tWGkgBc+lBFYWH zJBNT+zypTcwaen7Uj/StdqrWjFvfJNMxkSYaBREmDqX3gxJQ0FfoT/5mn+qIFNgtLkX Xhpw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=N3f5waM0; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=D9y98tzM; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b="qrVYZT/y"; 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 Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 5614622812f47-3e51503f70csi4663184b6e.301.2024.10.14.05.16.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Oct 2024 05:16:33 -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=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=N3f5waM0; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=D9y98tzM; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b="qrVYZT/y"; 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 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1t0Jzm-0006nU-UY; Mon, 14 Oct 2024 12:16:19 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1t0Jzk-0006nK-U8 for openvpn-devel@lists.sourceforge.net; Mon, 14 Oct 2024 12:16:18 +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:Cc:To:From:Sender:Reply-To: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=UJLSKYIBvqQlKY2qFU3aSLTuSoja/EpMoQn66kebnpY=; b=N3f5waM0WJDZLUQruel7qDVDQu 9XZagQkddfUtdAtZrY6fqxs/sZm/EvvqA+kZwRXRZibSg2V3TLAR/STZ2Ge7MaP2mQcOImY2DSC8O CrkyN0cUdZLglJhTi1twOJvveY6nGjQ78Tg35xE/crTHxoXlV+8z6guum+NT+bsIww7Q=; 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:Cc:To:From:Sender:Reply-To: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=UJLSKYIBvqQlKY2qFU3aSLTuSoja/EpMoQn66kebnpY=; b=D9y98tzM0LcWJycykYEyPXSxLI W+FGz7Dscfj4eEg97/Fd4fuvpCnl05sVGaeIxiE0HPpBtA+ju+bADTVJh0Z+l1sqjoybD0Ke5Z//y dRleQv5TAi7HeoedvnxGUd2kXfuMyaDGULATX6G+zDZqxKNTUgJwgIl8iDEImBQH9rAM=; Received: from mout-p-201.mailbox.org ([80.241.56.171]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1t0Jzj-0004tL-Mu for openvpn-devel@lists.sourceforge.net; Mon, 14 Oct 2024 12:16:17 +0000 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4XRx5t1F4mz9sb8; Mon, 14 Oct 2024 14:16:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lichtenheld.com; s=MBO0001; t=1728908162; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UJLSKYIBvqQlKY2qFU3aSLTuSoja/EpMoQn66kebnpY=; b=qrVYZT/yac/34J2Ppla7TzUPBNUrX3rhQsQTlGKrWQB9Ntdczg03cFW94Vms448bMiNFpT iWkLc1zlFCil8rXq0Rlba+sYCt173k6ceTiOzD+x6HaCA2DRsBSYusXQiNLRKnycInbiPq //Xqgw+8yL4WMBAKAbsFUMr2evYirInTBLUchbHFxiXvmhtwZ8d+BJexST8dAgzO+ZQgKa ikvuEqWkECl52aXLavAO/+bqOpBIeChOjmglzmGjPvTruvIYyL69JiiaOwlXglrkSdOgP3 xXwqam7hJo8NholOr1PHoEdHqZ0BOoF2ec/yPRE6IuGJhxJ8nCEE1jTi4iRkbw== From: Frank Lichtenheld To: openvpn-devel@lists.sourceforge.net Date: Mon, 14 Oct 2024 14:16:00 +0200 Message-Id: <20241014121600.36187-1-frank@lichtenheld.com> In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 4XRx5t1F4mz9sb8 X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", 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 In order to prepare the event handling code to deal with multiple listening sockets, we have to make sure that it is possible to distinguish which of these sockets have been poked by an incoming conne [...] Content analysis details: (-0.9 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [80.241.56.171 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-Headers-End: 1t0Jzj-0004tL-Mu Subject: [Openvpn-devel] [PATCH v4] event/multi: add event_arg object to make event handling more generic 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: , Cc: Antonio Quartulli Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1812891637187632618?= X-GMAIL-MSGID: =?utf-8?q?1812891637187632618?= From: Antonio Quartulli In order to prepare the event handling code to deal with multiple listening sockets, we have to make sure that it is possible to distinguish which of these sockets have been poked by an incoming connection request. To achieve that, this patch changes the object being passed as event handler argument, from a "partly integer-evaluated variable" to a full struct with a proper type attribute. This struct will allow the code to carry around the particular listening socket where the connection is being established. This change affects the TCP server code path only as UDP servers use only one socket to handle all clients. Change-Id: Icd7f6a2ad350cdc2312b3e80fa0dbdd7e4311d2e Signed-off-by: Antonio Quartulli Acked-by: Frank Lichtenheld --- 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/+/757 This mail reflects revision 4 of this Change. Acked-by according to Gerrit (reflected above): Frank Lichtenheld diff --git a/src/openvpn/event.h b/src/openvpn/event.h index 856551a..844ea7b 100644 --- a/src/openvpn/event.h +++ b/src/openvpn/event.h @@ -126,6 +126,20 @@ struct event_set_functions func; }; +typedef enum { + EVENT_ARG_MULTI_INSTANCE = 0, + EVENT_ARG_LINK_SOCKET, +} event_arg_t; + +/* generic event argument object to pass to event_ctl() */ +struct event_arg +{ + event_arg_t type; + union { + struct multi_instance *mi; /* if type = EVENT_ARG_MULTI_INSTANCE */ + } u; +}; + /* * maxevents on input: desired max number of event_t descriptors * simultaneously set with event_ctl diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index 96408d1..c002a38 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -54,7 +54,6 @@ /* * Special tags passed to event.[ch] functions */ -#define MTCP_SOCKET ((void *)1) #define MTCP_TUN ((void *)2) #define MTCP_SIG ((void *)3) /* Only on Windows */ #define MTCP_MANAGEMENT ((void *)4) @@ -253,7 +252,7 @@ socket_set(mi->context.c2.link_socket, m->mtcp->es, mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE : EVENT_READ, - mi, + &mi->ev_arg, &mi->tcp_rwflags); } } @@ -263,8 +262,8 @@ struct multi_tcp *mtcp) { int status; - unsigned int *persistent = &mtcp->tun_rwflags; - socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET); + socket_set_listen_persistent(c->c2.link_socket, mtcp->es, + &c->c2.link_socket->ev_arg); #ifdef _WIN32 if (tuntap_is_wintun(c->c1.tuntap)) @@ -280,7 +279,7 @@ persistent = NULL; } #endif - tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent); + tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags); #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO); #endif @@ -693,20 +692,43 @@ { struct event_set_return *e = &mtcp->esr[i]; - /* incoming data for instance? */ + /* incoming data for instance or listening socket? */ if (e->arg >= MTCP_N) { - struct multi_instance *mi = (struct multi_instance *) e->arg; - if (mi) + struct event_arg *ev_arg = (struct event_arg *)e->arg; + switch (ev_arg->type) { - if (e->rwflags & EVENT_WRITE) - { - multi_tcp_action(m, mi, TA_SOCKET_WRITE_READY, false); - } - else if (e->rwflags & EVENT_READ) - { - multi_tcp_action(m, mi, TA_SOCKET_READ, false); - } + struct multi_instance *mi; + + /* react to event on child instance */ + case EVENT_ARG_MULTI_INSTANCE: + if (!ev_arg->u.mi) + { + msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null minstance"); + break; + } + + mi = ev_arg->u.mi; + if (e->rwflags & EVENT_WRITE) + { + multi_tcp_action(m, mi, TA_SOCKET_WRITE_READY, false); + } + else if (e->rwflags & EVENT_READ) + { + multi_tcp_action(m, mi, TA_SOCKET_READ, false); + } + break; + + /* new incoming TCP client attempting to connect? */ + case EVENT_ARG_LINK_SOCKET: + ASSERT(m->top.c2.link_socket); + socket_reset_listen_persistent(m->top.c2.link_socket); + mi = multi_create_instance_tcp(m); + if (mi) + { + multi_tcp_action(m, mi, TA_INITIAL, false); + } + break; } } else @@ -731,18 +753,6 @@ multi_tcp_action(m, NULL, TA_TUN_READ, false); } } - /* new incoming TCP client attempting to connect? */ - else if (e->arg == MTCP_SOCKET) - { - struct multi_instance *mi; - ASSERT(m->top.c2.link_socket); - socket_reset_listen_persistent(m->top.c2.link_socket); - mi = multi_create_instance_tcp(m); - if (mi) - { - multi_tcp_action(m, mi, TA_INITIAL, false); - } - } #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD)) /* incoming data on DCO? */ else if (e->arg == MTCP_DCO) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 0509911..45b3cfa 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -823,6 +823,9 @@ goto err; } + mi->ev_arg.type = EVENT_ARG_MULTI_INSTANCE; + mi->ev_arg.u.mi = mi; + perf_pop(); gc_free(&gc); return mi; diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 7167639..1b99ef7 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -102,6 +102,12 @@ struct schedule_entry se; /* this must be the first element of the structure, * We cast between this and schedule_entry so the * beginning of the struct must be identical */ + + struct event_arg ev_arg; /**< this struct will store a pointer to either mi or + * link_socket, depending on the event type, to keep + * it accessible it's placed within the same struct + * it points to. */ + struct gc_arena gc; bool halt; int refcount; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 7b1e603..cf04090 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1830,6 +1830,8 @@ ALLOC_OBJ_CLEAR(sock, struct link_socket); sock->sd = SOCKET_UNDEFINED; sock->ctrl_sd = SOCKET_UNDEFINED; + sock->ev_arg.type = EVENT_ARG_LINK_SOCKET; + return sock; } diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 2e583af..c152ab0 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -178,6 +178,11 @@ { struct link_socket_info info; + struct event_arg ev_arg; /**< this struct will store a pointer to either mi or + * link_socket, depending on the event type, to keep + * it accessible it's placed within the same struct + * it points to. */ + socket_descriptor_t sd; socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */