From nobody Wed Dec 17 19:43:43 2025 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB91B2165EF for ; Mon, 9 Dec 2024 08:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733734390; cv=none; b=q0BK1kxuOJ6FG5/9GeT7hPQBasdxVIRCP3oP1vg+WQ8o7fk7omhe5MkG8uYi87evvhNEzFMSN/uJ5HPw4IBsbjfgsAOF6SUaQpiYvzMnrb5riVHAxzS2aO3tYu35QKkbJbSWoxpmBIJpQx8T0D2BAS+Zd90522fxnmwl91vyi4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733734390; c=relaxed/simple; bh=M8gNqQbi0rQ9/EHbOPxLbfPu705KbN+HrD3T/RxuNJQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pyUn6FfXQxCZd0rWRdrPeWcy8uyfmaTptma23K1XD7w2v+fFahfMyi7l351SF8Ze7AQUeGjhYkIlPzHsqb46n+fhRsItsHkESuwD00sF4+dLsvH2+5W/1NYn+Wu8k3g54WtY2gPa7pIiylCDliPHWOTMft52q3nDaktw/Au+M+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=openvpn.net; spf=pass smtp.mailfrom=openvpn.com; dkim=pass (2048-bit key) header.d=openvpn.net header.i=@openvpn.net header.b=N2B47Rfk; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=openvpn.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=openvpn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=openvpn.net header.i=@openvpn.net header.b="N2B47Rfk" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-434e406a547so12214845e9.3 for ; Mon, 09 Dec 2024 00:53:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1733734386; x=1734339186; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=J/iB0pPD3YtcNPkyKNqoQoWPIVEtSQcv/dxMDug0/uA=; b=N2B47RfkkkNG7ooFAUDtbjkVJsJUA75pgkcmiVqBz8GQ15A+m3Bsu8xbsMBgRePRPc KwNV2FzX7JhHxZK6HEQcQlOEdh7u8ElomcGaSDONMch8sjZ3p3dX8n/Un2VaDyUA9Pcf SIWPhvplyjt8Yv1D6MzF0HCHtESN/4oyKHw4zw42SVMCUD+xI4Ed63P4rGRYiX24up/B cUcGefOm01TRJQHgaI6FyJn/mGHNpHpIE42C4pEs2+TZsVcOBfultLklJDEElkhaAC4V szATl0+yINQ/V5L+1sUND/6g5LiAn5FmcqNFzimk5DTaAj4hWioA7BI5fqWhSh2li8mz 20HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733734386; x=1734339186; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J/iB0pPD3YtcNPkyKNqoQoWPIVEtSQcv/dxMDug0/uA=; b=itaqJiZuoWgJNQ0403V2otNBB+HX27XoL3myvWsn8h6DjpLWMBHhZ8+jfETHdcidII VqbFdQo70ESCFXy5AB/UMXsx/wJfkL4ICgZLRHI+Ieg3mcMM6yNA2cxDvAqqp9fiRCMY 4jSlu4XDxdwS2o97KLfic6mKGu8IdzrTxOKmB+kxKuSR+sIhDJSQ5kAlktSZQUgWa/98 abLqSUx8YaVpaR2cLITbLPcNxlUf0v415FGWj1YBESQCPAkAGLuLYhw08ne/KHnLPKE/ 5VaUqxt8FWQpspbOWtS7pAQ/xij+iW/z/IsB2OcxcCrURshjTEx0TVNezJ8v/CA+y2Vx GNwA== X-Forwarded-Encrypted: i=1; AJvYcCWc2FBIHu05yBguf9PRm3cCX3D5vCMnAoPDJuF7PhwbZ3o7yzsmP0ZHDQ9KlNU9dxaFazbFbgVzERQBszQ=@vger.kernel.org X-Gm-Message-State: AOJu0YykzL0KGMcs3RXlUe0j/kqRZh/kflQ2nuFsTKsqBIyRaVRPtI7+ +viLkEORcHSMywg/DViusrQP8Tcb8FvgPMPOKdrmIx8TtdTJxur/kj/f2bsSc5Y= X-Gm-Gg: ASbGncs4JvDSZgu8npvDy2zoju1BsRJDdBUtxjHyS2J19vGAQaSIenr+V3YG/9sSAuf Bji+3TgwCEouAGxYP9sNKAt256T+qMTBjJbvYSr4rW7o9pez7iockfQL6kR9jG0tLxoVbeLDDQ5 ybQcFg9DZUp9pctt8qJIf5oU249IY2dOYgII+OTGOdGwdpl/hapm2x0WomAxYHuFzGv/lHbb9wg mW7sM3wJEDMZWMwwzkRq21xwOk7YKaP82k1EMKIlc2vaKqk4mvGiJ0ZFCMw X-Google-Smtp-Source: AGHT+IF9ezypYFrB9qJ5ihkcUmMKiShoTwOz9Y+OA4ZKyHiAeA8xlUjhsShCbzOR9hz0pVckiGozTA== X-Received: by 2002:a5d:6d89:0:b0:385:d7f9:f169 with SMTP id ffacd0b85a97d-3862b33ab64mr6566552f8f.12.1733734386156; Mon, 09 Dec 2024 00:53:06 -0800 (PST) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:c60f:6f50:7258:1f7]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38621fbbea8sm12439844f8f.97.2024.12.09.00.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 00:53:05 -0800 (PST) From: Antonio Quartulli Date: Mon, 09 Dec 2024 09:53:15 +0100 Subject: [PATCH net-next v14 06/22] ovpn: introduce the ovpn_socket object Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241209-b4-ovpn-v14-6-ea243cf16417@openvpn.net> References: <20241209-b4-ovpn-v14-0-ea243cf16417@openvpn.net> In-Reply-To: <20241209-b4-ovpn-v14-0-ea243cf16417@openvpn.net> To: netdev@vger.kernel.org, Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: Simon Horman , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9404; i=antonio@openvpn.net; h=from:subject:message-id; bh=M8gNqQbi0rQ9/EHbOPxLbfPu705KbN+HrD3T/RxuNJQ=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnVrASppgiLIXh56HvLrw0NiLEekU3Ky0wiSbSm yDJdlm/Wm2JATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZ1awEgAKCRALcOU6oDjV hwxPB/96QuQ85IemgNUR8dlIw8KbMyD/pS86muGuPKOZI8SbGrDqIf73vDjbK5rnA8eVSrnd3DG DvlEUHNjQnAzhgm0U5EZAoz1nVCGPePR/hOugmT7R8AYxPzwWGk91sd1mTyko2B6nZvRDvxM4dL N37EIIn9b00WlIr7vLRSG1DuVtiwLsycaY3UbrMthNYLP4hW0w3DDPaFpNgafeHh8q/Oyts8qvh mZgtb1cLehG6ETN7ZjvPribNIL/2EuKsa/sXpZZYwWQ6VAx+f4FNP6N+jsQcb7Ej3HckjDdSRWO 8zkDoEQSkIStY1jwuZhjOtwiwKx5eeFpnF9VyH5kQeGUBAH0 X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C This specific structure is used in the ovpn kernel module to wrap and carry around a standard kernel socket. ovpn takes ownership of passed sockets and therefore an ovpn specific objects is attached to them for status tracking purposes. Initially only UDP support is introduced. TCP will come in a later patch. Cc: willemdebruijn.kernel@gmail.com Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/socket.c | 119 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/socket.h | 48 +++++++++++++++++++ drivers/net/ovpn/udp.c | 65 +++++++++++++++++++++++++ drivers/net/ovpn/udp.h | 17 +++++++ include/uapi/linux/udp.h | 1 + 6 files changed, 252 insertions(+) diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index ce13499b3e1775a7f2a9ce16c6cb0aa088f93685..56bddc9bef83e0befde6af3c356= 5bb91731d7b22 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -13,3 +13,5 @@ ovpn-y +=3D io.o ovpn-y +=3D netlink.o ovpn-y +=3D netlink-gen.o ovpn-y +=3D peer.o +ovpn-y +=3D socket.o +ovpn-y +=3D udp.o diff --git a/drivers/net/ovpn/socket.c b/drivers/net/ovpn/socket.c new file mode 100644 index 0000000000000000000000000000000000000000..0abac02e13fb4ef1e212dacae07= 5d5b58e872d34 --- /dev/null +++ b/drivers/net/ovpn/socket.c @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#include +#include +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "peer.h" +#include "socket.h" +#include "udp.h" + +static void ovpn_socket_detach(struct socket *sock) +{ + if (!sock) + return; + + sockfd_put(sock); +} + +/** + * ovpn_socket_release_kref - kref_put callback + * @kref: the kref object + */ +void ovpn_socket_release_kref(struct kref *kref) +{ + struct ovpn_socket *sock =3D container_of(kref, struct ovpn_socket, + refcount); + + ovpn_socket_detach(sock->sock); + kfree_rcu(sock, rcu); +} + +static bool ovpn_socket_hold(struct ovpn_socket *sock) +{ + return kref_get_unless_zero(&sock->refcount); +} + +static struct ovpn_socket *ovpn_socket_get(struct socket *sock) +{ + struct ovpn_socket *ovpn_sock; + + rcu_read_lock(); + ovpn_sock =3D rcu_dereference_sk_user_data(sock->sk); + if (WARN_ON(!ovpn_socket_hold(ovpn_sock))) + ovpn_sock =3D NULL; + rcu_read_unlock(); + + return ovpn_sock; +} + +static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer) +{ + int ret =3D -EOPNOTSUPP; + + if (!sock || !peer) + return -EINVAL; + + if (sock->sk->sk_protocol =3D=3D IPPROTO_UDP) + ret =3D ovpn_udp_socket_attach(sock, peer->ovpn); + + return ret; +} + +/** + * ovpn_socket_new - create a new socket and initialize it + * @sock: the kernel socket to embed + * @peer: the peer reachable via this socket + * + * Return: an openvpn socket on success or a negative error code otherwise + */ +struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer = *peer) +{ + struct ovpn_socket *ovpn_sock; + int ret; + + ret =3D ovpn_socket_attach(sock, peer); + if (ret < 0 && ret !=3D -EALREADY) + return ERR_PTR(ret); + + /* if this socket is already owned by this interface, just increase the + * refcounter and use it as expected. + * + * Since UDP sockets can be used to talk to multiple remote endpoints, + * openvpn normally instantiates only one socket and shares it among all + * its peers. For this reason, when we find out that a socket is already + * used for some other peer in *this* instance, we can happily increase + * its refcounter and use it normally. + */ + if (ret =3D=3D -EALREADY) { + /* caller is expected to increase the sock refcounter before + * passing it to this function. For this reason we drop it if + * not needed, like when this socket is already owned. + */ + ovpn_sock =3D ovpn_socket_get(sock); + sockfd_put(sock); + return ovpn_sock; + } + + ovpn_sock =3D kzalloc(sizeof(*ovpn_sock), GFP_KERNEL); + if (!ovpn_sock) + return ERR_PTR(-ENOMEM); + + ovpn_sock->ovpn =3D peer->ovpn; + ovpn_sock->sock =3D sock; + kref_init(&ovpn_sock->refcount); + + rcu_assign_sk_user_data(sock->sk, ovpn_sock); + + return ovpn_sock; +} diff --git a/drivers/net/ovpn/socket.h b/drivers/net/ovpn/socket.h new file mode 100644 index 0000000000000000000000000000000000000000..904814d2b9e9f2b0773bf942372= bcbe904ef5474 --- /dev/null +++ b/drivers/net/ovpn/socket.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#ifndef _NET_OVPN_SOCK_H_ +#define _NET_OVPN_SOCK_H_ + +#include +#include +#include + +struct ovpn_priv; +struct ovpn_peer; + +/** + * struct ovpn_socket - a kernel socket referenced in the ovpn code + * @ovpn: ovpn instance owning this socket (UDP only) + * @sock: the low level sock object + * @refcount: amount of contexts currently referencing this object + * @rcu: member used to schedule RCU destructor callback + */ +struct ovpn_socket { + struct ovpn_priv *ovpn; + struct socket *sock; + struct kref refcount; + struct rcu_head rcu; +}; + +void ovpn_socket_release_kref(struct kref *kref); + +/** + * ovpn_socket_put - decrease reference counter + * @sock: the socket whose reference counter should be decreased + */ +static inline void ovpn_socket_put(struct ovpn_socket *sock) +{ + kref_put(&sock->refcount, ovpn_socket_release_kref); +} + +struct ovpn_socket *ovpn_socket_new(struct socket *sock, + struct ovpn_peer *peer); + +#endif /* _NET_OVPN_SOCK_H_ */ diff --git a/drivers/net/ovpn/udp.c b/drivers/net/ovpn/udp.c new file mode 100644 index 0000000000000000000000000000000000000000..c00e07f148d72ff737e732028fd= 73f82a507fb57 --- /dev/null +++ b/drivers/net/ovpn/udp.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#include +#include +#include +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "socket.h" +#include "udp.h" + +/** + * ovpn_udp_socket_attach - set udp-tunnel CBs on socket and link it to ov= pn + * @sock: socket to configure + * @ovpn: the openvp instance to link + * + * After invoking this function, the sock will be controlled by ovpn so th= at + * any incoming packet may be processed by ovpn first. + * + * Return: 0 on success or a negative error code otherwise + */ +int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_priv *ovpn) +{ + struct ovpn_socket *old_data; + int ret =3D 0; + + /* make sure no pre-existing encapsulation handler exists */ + rcu_read_lock(); + old_data =3D rcu_dereference_sk_user_data(sock->sk); + if (!old_data) { + /* socket is currently unused - we can take it */ + rcu_read_unlock(); + return 0; + } + + /* socket is in use. We need to understand if it's owned by this ovpn + * instance or by something else. + * In the former case, we can increase the refcounter and happily + * use it, because the same UDP socket is expected to be shared among + * different peers. + * + * Unlikely TCP, a single UDP socket can be used to talk to many remote + * hosts and therefore openvpn instantiates one only for all its peers + */ + if ((READ_ONCE(udp_sk(sock->sk)->encap_type) =3D=3D UDP_ENCAP_OVPNINUDP) = && + old_data->ovpn =3D=3D ovpn) { + netdev_dbg(ovpn->dev, + "provided socket already owned by this interface\n"); + ret =3D -EALREADY; + } else { + netdev_dbg(ovpn->dev, + "provided socket already taken by other user\n"); + ret =3D -EBUSY; + } + rcu_read_unlock(); + + return ret; +} diff --git a/drivers/net/ovpn/udp.h b/drivers/net/ovpn/udp.h new file mode 100644 index 0000000000000000000000000000000000000000..3c48a06f15eed624aec0a2a7b87= 1f0e7f3004137 --- /dev/null +++ b/drivers/net/ovpn/udp.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#ifndef _NET_OVPN_UDP_H_ +#define _NET_OVPN_UDP_H_ + +struct ovpn_priv; +struct socket; + +int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_priv *ovpn); + +#endif /* _NET_OVPN_UDP_H_ */ diff --git a/include/uapi/linux/udp.h b/include/uapi/linux/udp.h index d85d671deed3c78f6969189281b9083dcac000c6..edca3e430305a6bffc34e617421= f1f3071582e69 100644 --- a/include/uapi/linux/udp.h +++ b/include/uapi/linux/udp.h @@ -43,5 +43,6 @@ struct udphdr { #define UDP_ENCAP_GTP1U 5 /* 3GPP TS 29.060 */ #define UDP_ENCAP_RXRPC 6 #define TCP_ENCAP_ESPINTCP 7 /* Yikes, this is really xfrm encap types. */ +#define UDP_ENCAP_OVPNINUDP 8 /* OpenVPN traffic */ =20 #endif /* _UAPI_LINUX_UDP_H */ --=20 2.45.2