From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 446EB3D3B3 for ; Wed, 16 Oct 2024 01:03:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040624; cv=none; b=n7yc5+4HsI3P7glW7td8Nk8ymDjE0Od5X+Ku1AIfyD47fIKbkiTPGd6dr5iEunZctEbDcqvVSzudXB7ybFt6gV06e7kPXyGVlVnZM8dcrWpJke3GxM5rjDdLAnfxBpp33NpV53GbyX2yYoKvlmHAf9/CkX99OWwQI8y500/kgrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040624; c=relaxed/simple; bh=YWzfJwwDj9H33yTKqyIsks1NPkpejrGE0PuiyJ5QhmQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=larcBctwtlaxJ5GbPUMn7ymhlb3ook4w8ovw4KjI2Voq8+V1LWraLcA/efA6sX4BtFfE+wZZ8H8/a9ZbiUgVZc4zdtn/eBdklV3kGVzduwVojwn/7u7sjZWiGOEqHytPEx4/rYNc5kwpv2QihnkvdrCV1yVDuvenT8MgLJir8fU= 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=QtitJG8y; arc=none smtp.client-ip=209.85.221.54 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="QtitJG8y" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d3ecad390so217498f8f.1 for ; Tue, 15 Oct 2024 18:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040620; x=1729645420; 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=M9GdUbPvHtzqsFu2h4/Im5jGzt5vX/Qgs50BtWNazkE=; b=QtitJG8ygbhw//ICLnRY5t9+9MgAkTQpl0xfY3FGilgcPCzt4U/a5+bjVbflQ5mMWn aJGaAJNEVLCHJFamNbTmELeBlilanctjFV3gxw4n/ZkUBxUMLKmi8VLYVykTcTgKyFhz h13wd9DalJRNbPL2De6wwTOBvv9xRRDBfN4eoGV8cYUrGcmxXrBcqLxZpexLCwL1quHW 2B+pT19F/W1UEyR5ad2+p1XGzsnKXVPBRrrfUBkY4SMY0wGayjGF7DVkWhUYy9rfkLPc ya+nBbUnpfUpwM5poHJWaWJ6JxbA64OATN60c8dwUns1taxiPlbTW5x6qQN8OmfUmJPP NbWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040620; x=1729645420; 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=M9GdUbPvHtzqsFu2h4/Im5jGzt5vX/Qgs50BtWNazkE=; b=up1VFPnB1OS41+qXnqkRlrUhFtrszTp+AKvVeQ+rK7Mtl8fmZmAYo42jPqrHOCe5R4 /DJSapOddlobbq361tWwOmf4zi6ekNrVfkSAhla0q2cSMZg6IwO7DEUJt6xinv/Mf4Bo OJ+LnwFyE9U6AVsYrYqC6qRz56AedgOxmpHXb6lZgjxAAVwfUcQD8f6KOe5+RoCNKCBk 6oLU7syxMJ1b+FJnkUR5cmjAAwR2Ou9T7EZE3NgVEA9BByT1rEQY0XvgkIwt49qGb71u 4pw4ttgOXoEl1OhtgRnMTMqIIuxSnNRF+7fHVLC1n0CcqsgOXJVCRXA4a8WaSmFyWeGh uIOg== X-Forwarded-Encrypted: i=1; AJvYcCXVXYRjSMwTFBK0PcUPguap3rajIou7YJ7xSumjWR4DPYPm4gpLtYG+kQ6E0d+MkO/BH3UoGVVF+T4hYfc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/f8YVSNcolxCJPB0LSh4O72vlvguglwzt29OdUv6zqglyoZky 12HOg1P9vew12H1xyAy1yPuErAv3lKJDZARa4oKdNdg6ahGhCB6aatKCT1M8LH0= X-Google-Smtp-Source: AGHT+IGnhSAmGLGUDKKbUqvI6y6PJo6IA+NC1DmkR+MMI0dzKNGfU/PKwcleDFpP8R8tmigj6tcp+g== X-Received: by 2002:a5d:591b:0:b0:37c:bafd:5624 with SMTP id ffacd0b85a97d-37d86329aefmr1528351f8f.25.1729040620564; Tue, 15 Oct 2024 18:03:40 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:40 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:01 +0200 Subject: [PATCH net-next v9 01/23] netlink: add NLA_POLICY_MAX_LEN macro 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: <20241016-b4-ovpn-v9-1-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1979; i=antonio@openvpn.net; h=from:subject:message-id; bh=YWzfJwwDj9H33yTKqyIsks1NPkpejrGE0PuiyJ5QhmQ=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDudYwSrNv2JxTj0AACirqozqaUNwxcVn54Q sOfOYs8hbyJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7gAKCRALcOU6oDjV hwgTB/9SsO69NczktOYtzGXdzG9nYDdyCJ8UPQAROWiG8V/HoF5ibInTrwSYNc6Ot4Eoxtxvx6D G3SHjSpbyeN0F0AuCfNztVYN/RyKBT16gz1KkJx98EEm2lMZG8KYjCOmcBbnWJRH6yBo9xBJJG7 xFoNKmxRUJZNAFintQFuETYwyZzPdf0bOc3OCDa/eQy5MAqUd2m9qKRLMhnVTZ7TrqnYPNEqD4M 6eNkbReTbtv/uNXG493MiHeGahWTvXYsSQN2+scPgWuTqbRW4pF0Cyq9BchTF1NJHZovTRtutB/ HOXj17O2jFC8MdOD1cHlcyfLrDMZkvgeh64TQpLILAnkAl37 X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Similarly to NLA_POLICY_MIN_LEN, NLA_POLICY_MAX_LEN defines a policy with a maximum length value. The netlink generator for YAML specs has been extended accordingly. Cc: donald.hunter@gmail.com Signed-off-by: Antonio Quartulli --- include/net/netlink.h | 1 + tools/net/ynl/ynl-gen-c.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/net/netlink.h b/include/net/netlink.h index db6af207287c839408c58cb28b82408e0548eaca..2dc671c977ff3297975269d2362= 64907009703d3 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -469,6 +469,7 @@ struct nla_policy { .max =3D _len \ } #define NLA_POLICY_MIN_LEN(_len) NLA_POLICY_MIN(NLA_BINARY, _len) +#define NLA_POLICY_MAX_LEN(_len) NLA_POLICY_MAX(NLA_BINARY, _len) =20 /** * struct nl_info - netlink source information diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py index d64cb2b49c4437cfc018c669dadafdb43037e727..89ef308e56bbe287aee81e118f4= 5cb951df88046 100755 --- a/tools/net/ynl/ynl-gen-c.py +++ b/tools/net/ynl/ynl-gen-c.py @@ -478,7 +478,7 @@ class TypeBinary(Type): pass elif len(self.checks) =3D=3D 1: check_name =3D list(self.checks)[0] - if check_name not in {'exact-len', 'min-len'}: + if check_name not in {'exact-len', 'min-len', 'max-len'}: raise Exception('Unsupported check for binary type: ' + ch= eck_name) else: raise Exception('More than one check for binary type not imple= mented, yet') @@ -489,6 +489,8 @@ class TypeBinary(Type): mem =3D 'NLA_POLICY_EXACT_LEN(' + self.get_limit_str('exact-le= n') + ')' elif 'min-len' in self.checks: mem =3D '{ .len =3D ' + self.get_limit_str('min-len') + ', }' + elif 'max-len' in self.checks: + mem =3D 'NLA_POLICY_MAX_LEN(' + self.get_limit_str('max-len') = + ')' =20 return mem =20 --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 0C69E148FED for ; Wed, 16 Oct 2024 01:03:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040628; cv=none; b=DggEQOrG5TrgtMuNHxaUrSHRDIPAqLWB/t0Jz6mKSwaUOIXrwDW/ggTr9OT4MxvrYpAzI0ihaZZF2SF/dHnG3DMnx3xkTszLqOCtJ8WekU4JuAiLxQPb2URhcimafLDTFoo357rdj65/sHdoUUuuWQl1rWOLpcPjKarogUU3AFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040628; c=relaxed/simple; bh=Lhd+5hzDSsFhyu8ajaV/xLmuL3kjlTMk1mDp7LyPx1g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kLQbhi8gBaTghLVboIEhLRLjr3NY8fZ9yJviIHn3kI/iHGfrG+mkIklZQjrb4HXEHy4G8rRv9pMHhBQh/BmBt1Yl9zSc4IQcj/zlQ8PCc8GIzhaXOdSSCmR1xw6LbnbanNsAOA4tU/hVXk5HONGcgR47NIEDEqasWnwd/l8/fY4= 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=akVpCq4f; arc=none smtp.client-ip=209.85.221.44 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="akVpCq4f" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-37d4b0943c7so3966244f8f.1 for ; Tue, 15 Oct 2024 18:03:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040622; x=1729645422; 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=9eOyMKxzyYmndmPJkG82ZvusD6Siua+E3j5KGZYYF3g=; b=akVpCq4f6cp02t9Oy0VHhycFElX3bkLO8QHNpr8XJi2YrWmOi5oKoo6RPZw66aoQct QBbMeh5mMYJxp/yd/xMdqJnIESsETwAlJIiNaUN97cY/GKZdOJ5BIiPg6M85tKuOm+VQ 8aI4yV1KXrBjF9JvcmsN7pZnnnRdaYoAU1heukBEKfEBhl5vh5TqAbJ6kcAbcOEL04Kf mGIqPlLI9B5NfjMBHgGsBRZbPsmeLusxTpUaM4FFciozoIH9NdR4wK1bS8tA4a5WdpTp 1pxVIqMWmEo9hHev9aJ8Cz/3LZ4DINr78sKBRTuVNQ/ETfDNUsd8Vkc4XQ77caZmAI+8 emAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040622; x=1729645422; 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=9eOyMKxzyYmndmPJkG82ZvusD6Siua+E3j5KGZYYF3g=; b=hjOcZNEKqxWOu04uo4E6gH7l4dhlgIt1TmfOwj9tgBLdVf6YYLvGa9VO4gj6+0iSXy ztBSH1TJLEEWhlqyTqzL8ebkc1EMZRW+IiPYivLOkHviPjvkH+k7q5uXrImx+m6e3uRy 4AXXGQMRqOXGW64fw+h0OHRUJOUfcyqt5NtWt2fMC3QJ9TV2sxbAvPPecPxWSnWs2v20 z41l4dtApOEs2wwGUo25kbKmTu4Ews5vt9L18f7fDHSwRu11R6GUjF8/X+ztu+6qQvii 2rrmmU3bdmQyri0LoUjoERkVIuJ0Cudpoc4s91HtA5lhGH88m187GDRdatXV3OkLYuAb PkCA== X-Forwarded-Encrypted: i=1; AJvYcCWyRXXqTEGQ6iIQtj1C/RxD/trRP4F3WZuB+fHn1FXpNYOw5GMEBN0XAPhca3UovUgeiSKIAsJygZcI2/k=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8sPQ3B/WAk6PuiWc51IBEgBTmHh2GntF8ZRtxaRKMDvWceIny EI15Vv8fXYbh8UbuLSPuThh5PWIN1yoZ7guyV3JKPVsGddA845nUvg8DiQ5Hwtg= X-Google-Smtp-Source: AGHT+IG46Z7F/udPp7LyyHUF37rHCboHMG5sLCbAfXCNkEIQZI0jO9y8Sk4PL4lujqjR9fgD9MciMg== X-Received: by 2002:adf:dd89:0:b0:37d:45f0:dd0a with SMTP id ffacd0b85a97d-37d86bb66f1mr1364740f8f.1.1729040622256; Tue, 15 Oct 2024 18:03:42 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:41 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:02 +0200 Subject: [PATCH net-next v9 02/23] net: introduce OpenVPN Data Channel Offload (ovpn) 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: <20241016-b4-ovpn-v9-2-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=11928; i=antonio@openvpn.net; h=from:subject:message-id; bh=Lhd+5hzDSsFhyu8ajaV/xLmuL3kjlTMk1mDp7LyPx1g=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDuAhhtj6R73R13C085rLKQ2uSU3Bf5+XFYI ru8xJNt0JeJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7gAKCRALcOU6oDjV h45KCACvBx7x2MFl7tB8FXoW76fUqyyLcJsYWXS6eWPhr1EkGqr/UFktbbAIbGAtJ41EA4sC36L OlA5phvKfdzzMLdkYRVTuJzNTSAFJAIyi9xmQCX2HRGzDXJDTmMQ3TnGX75eikn4LoVbYgyOL7a ejtfQAwnH5VpUYWKgaRkZs1zahBcdRoWO7brT1TJdqZiOiwFB4HFpycgQsW2qDHPskmprA/9BtD Qw0V4Hqa4+nHLOSmGSUkjbKCgCwNCXNRSuGBt2uWT3LyR+J+T+pt8/lLFJhFC0zeyOt9P7oVNRq fx7Uxdif8/eWfEtjmnU1jSKdZYMGKb+DsQ6a+QQUQukLyBC/ X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C OpenVPN is a userspace software existing since around 2005 that allows users to create secure tunnels. So far OpenVPN has implemented all operations in userspace, which implies several back and forth between kernel and user land in order to process packets (encapsulate/decapsulate, encrypt/decrypt, rerouting..). With `ovpn` we intend to move the fast path (data channel) entirely in kernel space and thus improve user measured throughput over the tunnel. `ovpn` is implemented as a simple virtual network device driver, that can be manipulated by means of the standard RTNL APIs. A device of kind `ovpn` allows only IPv4/6 traffic and can be of type: * P2P (peer-to-peer): any packet sent over the interface will be encapsulated and transmitted to the other side (typical OpenVPN client or peer-to-peer behaviour); * P2MP (point-to-multipoint): packets sent over the interface are transmitted to peers based on existing routes (typical OpenVPN server behaviour). After the interface has been created, OpenVPN in userspace can configure it using a new Netlink API. Specifically it is possible to manage peers and their keys. The OpenVPN control channel is multiplexed over the same transport socket by means of OP codes. Anything that is not DATA_V2 (OpenVPN OP code for data traffic) is sent to userspace and handled there. This way the `ovpn` codebase is kept as compact as possible while focusing on handling data traffic only (fast path). Any OpenVPN control feature (like cipher negotiation, TLS handshake, rekeying, etc.) is still fully handled by the userspace process. When userspace establishes a new connection with a peer, it first performs the handshake and then passes the socket to the `ovpn` kernel module, which takes ownership. From this moment on `ovpn` will handle data traffic for the new peer. When control packets are received on the link, they are forwarded to userspace through the same transport socket they were received on, as userspace is still listening to them. Some events (like peer deletion) are sent to a Netlink multicast group. Although it wasn't easy to convince the community, `ovpn` implements only a limited number of the data-channel features supported by the userspace program. Each feature that made it to `ovpn` was attentively vetted to avoid carrying too much legacy along with us (and to give a clear cut to old and probalby-not-so-useful features). Notably, only encryption using AEAD ciphers (specifically ChaCha20Poly1305 and AES-GCM) was implemented. Supporting any other cipher out there was not deemed useful. Both UDP and TCP sockets ae supported. As explained above, in case of P2MP mode, OpenVPN will use the main system routing table to decide which packet goes to which peer. This implies that no routing table was re-implemented in the `ovpn` kernel module. This kernel module can be enabled by selecting the CONFIG_OVPN entry in the networking drivers section. NOTE: this first patch introduces the very basic framework only. Features are then added patch by patch, however, although each patch will compile and possibly not break at runtime, only after having applied the full set it is expected to see the ovpn module fully working. Cc: steffen.klassert@secunet.com Cc: antony.antony@secunet.com Signed-off-by: Antonio Quartulli --- MAINTAINERS | 8 ++++ drivers/net/Kconfig | 14 ++++++ drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 11 +++++ drivers/net/ovpn/io.c | 22 +++++++++ drivers/net/ovpn/io.h | 15 ++++++ drivers/net/ovpn/main.c | 116 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/main.h | 15 ++++++ include/uapi/linux/udp.h | 1 + 9 files changed, 203 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d678a58c0205793a5e70e23e5ea009a55b771304..b6fc8cb11a574caf5589a7d77ab= 8e90c780fccfa 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17467,6 +17467,14 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/g= it/overlayfs/vfs.git F: Documentation/filesystems/overlayfs.rst F: fs/overlayfs/ =20 +OPENVPN DATA CHANNEL OFFLOAD +M: Antonio Quartulli +L: openvpn-devel@lists.sourceforge.net (moderated for non-subscribers) +L: netdev@vger.kernel.org +S: Maintained +T: git https://github.com/OpenVPN/linux-kernel-ovpn.git +F: drivers/net/ovpn/ + P54 WIRELESS DRIVER M: Christian Lamparter L: linux-wireless@vger.kernel.org diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 1fd5acdc73c6af0e1a861867039c3624fc618e25..d54ddf0f0e0979d75120805930d= a82f48b865ea4 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -115,6 +115,20 @@ config WIREGUARD_DEBUG =20 Say N here unless you know what you're doing. =20 +config OVPN + tristate "OpenVPN data channel offload" + depends on NET && INET + select NET_UDP_TUNNEL + select DST_CACHE + select CRYPTO + select CRYPTO_AES + select CRYPTO_GCM + select CRYPTO_CHACHA20POLY1305 + select STREAM_PARSER + help + This module enhances the performance of the OpenVPN userspace software + by offloading the data channel processing to kernelspace. + config EQUALIZER tristate "EQL (serial line load balancing) support" help diff --git a/drivers/net/Makefile b/drivers/net/Makefile index 13743d0e83b5fde479e9b30ad736be402d880dee..5152b3330e28da7eaec821018a2= 6c973bb33ce0c 100644 --- a/drivers/net/Makefile +++ b/drivers/net/Makefile @@ -11,6 +11,7 @@ obj-$(CONFIG_IPVLAN) +=3D ipvlan/ obj-$(CONFIG_IPVTAP) +=3D ipvlan/ obj-$(CONFIG_DUMMY) +=3D dummy.o obj-$(CONFIG_WIREGUARD) +=3D wireguard/ +obj-$(CONFIG_OVPN) +=3D ovpn/ obj-$(CONFIG_EQUALIZER) +=3D eql.o obj-$(CONFIG_IFB) +=3D ifb.o obj-$(CONFIG_MACSEC) +=3D macsec.o diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..53fb197027d787d6683e9056d3d= 341abf6ed38e4 --- /dev/null +++ b/drivers/net/ovpn/Makefile @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# ovpn -- OpenVPN data channel offload in kernel space +# +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli + +obj-$(CONFIG_OVPN) :=3D ovpn.o +ovpn-y +=3D main.o +ovpn-y +=3D io.o diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c new file mode 100644 index 0000000000000000000000000000000000000000..ad3813419c33cbdfe7e8ad6f5c8= b444a3540a69f --- /dev/null +++ b/drivers/net/ovpn/io.c @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#include +#include + +#include "io.h" + +/* Send user data to the network + */ +netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) +{ + skb_tx_error(skb); + kfree_skb(skb); + return NET_XMIT_DROP; +} diff --git a/drivers/net/ovpn/io.h b/drivers/net/ovpn/io.h new file mode 100644 index 0000000000000000000000000000000000000000..aa259be66441f7b0262f39da12d= 6c3dce0a9b24c --- /dev/null +++ b/drivers/net/ovpn/io.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#ifndef _NET_OVPN_OVPN_H_ +#define _NET_OVPN_OVPN_H_ + +netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev); + +#endif /* _NET_OVPN_OVPN_H_ */ diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c new file mode 100644 index 0000000000000000000000000000000000000000..369a5a2b2fc1a497c8444e59f9b= 058eb40e49524 --- /dev/null +++ b/drivers/net/ovpn/main.c @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#include +#include +#include + +#include "main.h" +#include "io.h" + +/* Driver info */ +#define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)" +#define DRV_COPYRIGHT "(C) 2020-2024 OpenVPN, Inc." + +/** + * ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' + * @dev: the interface to check + * + * Return: whether the netdevice is of type 'ovpn' + */ +bool ovpn_dev_is_valid(const struct net_device *dev) +{ + return dev->netdev_ops->ndo_start_xmit =3D=3D ovpn_net_xmit; +} + +static int ovpn_newlink(struct net *src_net, struct net_device *dev, + struct nlattr *tb[], struct nlattr *data[], + struct netlink_ext_ack *extack) +{ + return -EOPNOTSUPP; +} + +static struct rtnl_link_ops ovpn_link_ops =3D { + .kind =3D "ovpn", + .netns_refund =3D false, + .newlink =3D ovpn_newlink, + .dellink =3D unregister_netdevice_queue, +}; + +static int ovpn_netdev_notifier_call(struct notifier_block *nb, + unsigned long state, void *ptr) +{ + struct net_device *dev =3D netdev_notifier_info_to_dev(ptr); + + if (!ovpn_dev_is_valid(dev)) + return NOTIFY_DONE; + + switch (state) { + case NETDEV_REGISTER: + /* add device to internal list for later destruction upon + * unregistration + */ + break; + case NETDEV_UNREGISTER: + /* can be delivered multiple times, so check registered flag, + * then destroy the interface + */ + break; + case NETDEV_POST_INIT: + case NETDEV_GOING_DOWN: + case NETDEV_DOWN: + case NETDEV_UP: + case NETDEV_PRE_UP: + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + +static struct notifier_block ovpn_netdev_notifier =3D { + .notifier_call =3D ovpn_netdev_notifier_call, +}; + +static int __init ovpn_init(void) +{ + int err =3D register_netdevice_notifier(&ovpn_netdev_notifier); + + if (err) { + pr_err("ovpn: can't register netdevice notifier: %d\n", err); + return err; + } + + err =3D rtnl_link_register(&ovpn_link_ops); + if (err) { + pr_err("ovpn: can't register rtnl link ops: %d\n", err); + goto unreg_netdev; + } + + return 0; + +unreg_netdev: + unregister_netdevice_notifier(&ovpn_netdev_notifier); + return err; +} + +static __exit void ovpn_cleanup(void) +{ + rtnl_link_unregister(&ovpn_link_ops); + unregister_netdevice_notifier(&ovpn_netdev_notifier); + + rcu_barrier(); +} + +module_init(ovpn_init); +module_exit(ovpn_cleanup); + +MODULE_DESCRIPTION(DRV_DESCRIPTION); +MODULE_AUTHOR(DRV_COPYRIGHT); +MODULE_LICENSE("GPL"); diff --git a/drivers/net/ovpn/main.h b/drivers/net/ovpn/main.h new file mode 100644 index 0000000000000000000000000000000000000000..a3215316c49bfcdf2496590bac8= 78f145b8b27fd --- /dev/null +++ b/drivers/net/ovpn/main.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#ifndef _NET_OVPN_MAIN_H_ +#define _NET_OVPN_MAIN_H_ + +bool ovpn_dev_is_valid(const struct net_device *dev); + +#endif /* _NET_OVPN_MAIN_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 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 2F9A016D4EF for ; Wed, 16 Oct 2024 01:03:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040629; cv=none; b=Zxd7BftCGrJBT4ATLy153q4jaNFKFjX8GZg9xStTmdlCKGaNYb0v9zpci6118DhGObDVFgVXUjZX/w/ZkMA+VxII+ejmF4zxB9efF8EPK7sAfEP9FHzyjy2Qi/YL6jWeQODj3W8LCgbVazY+HFB3aXPFEGBDSVExcScfrJqFAaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040629; c=relaxed/simple; bh=8TTedho64WVkf+etjwImM6a/MyXEXF8wEct03KN93no=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Nrw5AIxl5eKAqQLL4ArxZl4npF0sMQ4Lc7OxXFDKih/UtxGBxy1owZXav8ptP2H3GBrQSZW4t6V8BPv3ZlFSDVyb5rqiiFDn0ivlXmRMGXn2v9wbV1Ep1rDShR6Q/gWC9iWHotAM6WJ0/zRH7IaF3ObqMWWPUQ07GUTBrYYrLlo= 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=cD7u8pBM; arc=none smtp.client-ip=209.85.221.45 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="cD7u8pBM" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-37d49a7207cso3877298f8f.0 for ; Tue, 15 Oct 2024 18:03:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040624; x=1729645424; 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=QvpwrZueA0V7UBaspnWyx2IRZiwNQrFfUiCVzP6Ifbo=; b=cD7u8pBM8AuW7o/86twSBSvXPcPNhM/rXF0w+xe8svrRS3G9a3sbK8qagGpr6uavnX xqlL/wUZbJzuyqRc12LJ7kzQlMHTj6KfS8LvaAaIxI1PmeW8rfhPlZ0kI1opxL7ubJ7q ETj6Zp0D+wgmqL0o1iwsh8c45ttkWhVS54yqO/vWRk/0/wPJkslhu2iFazJ/HDxMusfh 53PTjEoq2isUjcDHwIrNmHSQOkdUWCmIRZmniTJ8MWgB9PYxTtP7aUckRLFGl4Qvn7qo LdCkMxHATdmkBgQ4zzbTfCdkz5TvWvuBNq+479LYUb8q6ZFGIETSnNALqK76o2Q59I+P N/Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040624; x=1729645424; 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=QvpwrZueA0V7UBaspnWyx2IRZiwNQrFfUiCVzP6Ifbo=; b=ZRGnQoymPu7FqDZtlyk+gzcHplXOQ0km9KYlYhxqo+JpwvEe/eK5eAM9/GOTZYsBtZ Bep4+ImkcGj/MGHfgjusS/hz/y8Gh+wIqXyvGnEXI2EK9PQGHDjXkP+OaFHlb1SpXmu+ iUsbPVA7nVzg/VsU7iCAap0RZaaIVKEjUC7d129xtcuTtW48aFReGr7ynewwKBGCWfWu UZFyDjenfT5Q7T92oXnEpB/uBbRnCQxc7RuPAK+yzFAXbkDhFp9/aN9H1uxUHz0rH55b iBpQjM2MBdhE60OTYf9Z19QQQ5usGfcwm7hAH7kmemLt+Bvtai++Dp2u0rFLpQdaY3CV sgAQ== X-Forwarded-Encrypted: i=1; AJvYcCVh7FHQVVj0sL/RX23ueYrZkh07a3bAfQS1f2yWvYmbuR+a9pysn18VR07Rv3bCN6GIkAt6gEEzFGGkL5A=@vger.kernel.org X-Gm-Message-State: AOJu0YxuqykSY6Z2eTEDz9aTvASe+GVC0ToCNAfTgzyzfV6VX/b8hMqD i3+Jdi8JrVSHeEyLKDho6lXhEJ869IX9JTWEUgqz/Kr3c3a2MwEpCMyMU06873w= X-Google-Smtp-Source: AGHT+IER0T/MWTXb3YWPXZE7l9kF722L2gl6K3tr05C1tIWpbubDE/GA9Jkbo9KHB8MXmmV6NYueiQ== X-Received: by 2002:adf:e504:0:b0:37d:5338:872c with SMTP id ffacd0b85a97d-37d5fec8e0emr7935490f8f.1.1729040624355; Tue, 15 Oct 2024 18:03:44 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:42 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:03 +0200 Subject: [PATCH net-next v9 03/23] ovpn: add basic netlink support 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: <20241016-b4-ovpn-v9-3-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=31145; i=antonio@openvpn.net; h=from:subject:message-id; bh=8TTedho64WVkf+etjwImM6a/MyXEXF8wEct03KN93no=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDuvGQnKVPk+V0Fyy+WcNcdXzSNRyRydQ/Od aVa54XFCAeJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7gAKCRALcOU6oDjV h1kVB/9Uo1VNwzYVXh4pP00ByTQ2JZbXVQn3GiSwXNSeaDgPewzGzszuYJIO3SSl9OUyaSw4PSn OlH4Y5LplJlJdEKG9qQqDmfs8ZdEbhnKl08fknAgO8SRb8hC6vgstPgaQyN9qftJMj3s3/86v2c fA0ZRtDYUOMERUXtM76HHwZnRCtmoy3R2eJKSlF58AuACoRUBO+r23yTM5YrSgs75NgXxoduoJ2 cqHkn4dWOz0PwjGMqE0kYdD1dirHFhOZRjmky+/744YCsIbYm2EiE9ynOckzG2fQFR32OEplRXl DCvRUjygiMdruF2vGlIGiEI5f7XWH86K36aqr0a1Iqk2+zjY X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C This commit introduces basic netlink support with family registration/unregistration functionalities and stub pre/post-doit. More importantly it introduces the YAML uAPI description along with its auto-generated files: - include/uapi/linux/ovpn.h - drivers/net/ovpn/netlink-gen.c - drivers/net/ovpn/netlink-gen.h Cc: donald.hunter@gmail.com Signed-off-by: Antonio Quartulli --- Documentation/netlink/specs/ovpn.yaml | 362 ++++++++++++++++++++++++++++++= ++++ MAINTAINERS | 2 + drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/main.c | 15 +- drivers/net/ovpn/netlink-gen.c | 212 ++++++++++++++++++++ drivers/net/ovpn/netlink-gen.h | 41 ++++ drivers/net/ovpn/netlink.c | 157 +++++++++++++++ drivers/net/ovpn/netlink.h | 15 ++ drivers/net/ovpn/ovpnstruct.h | 25 +++ include/uapi/linux/ovpn.h | 109 ++++++++++ 10 files changed, 939 insertions(+), 1 deletion(-) diff --git a/Documentation/netlink/specs/ovpn.yaml b/Documentation/netlink/= specs/ovpn.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79339c25d607f1b5d15a0a973f6= fc23637e158a2 --- /dev/null +++ b/Documentation/netlink/specs/ovpn.yaml @@ -0,0 +1,362 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cla= use) +# +# Author: Antonio Quartulli +# +# Copyright (c) 2024, OpenVPN Inc. +# + +name: ovpn + +protocol: genetlink + +doc: Netlink protocol to control OpenVPN network devices + +definitions: + - + type: const + name: nonce-tail-size + value: 8 + - + type: enum + name: cipher-alg + entries: [ none, aes-gcm, chacha20-poly1305 ] + - + type: enum + name: del-peer-reason + entries: [ teardown, userspace, expired, transport-error, transport-di= sconnect ] + - + type: enum + name: key-slot + entries: [ primary, secondary ] + +attribute-sets: + - + name: peer + attributes: + - + name: id + type: u32 + doc: | + The unique ID of the peer. To be used to identify peers during + operations + checks: + max: 0xFFFFFF + - + name: remote-ipv4 + type: u32 + doc: The remote IPv4 address of the peer + byte-order: big-endian + display-hint: ipv4 + - + name: remote-ipv6 + type: binary + doc: The remote IPv6 address of the peer + display-hint: ipv6 + checks: + exact-len: 16 + - + name: remote-ipv6-scope-id + type: u32 + doc: The scope id of the remote IPv6 address of the peer (RFC2553) + - + name: remote-port + type: u16 + doc: The remote port of the peer + byte-order: big-endian + checks: + min: 1 + - + name: socket + type: u32 + doc: The socket to be used to communicate with the peer + - + name: vpn-ipv4 + type: u32 + doc: The IPv4 address assigned to the peer by the server + byte-order: big-endian + display-hint: ipv4 + - + name: vpn-ipv6 + type: binary + doc: The IPv6 address assigned to the peer by the server + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-ipv4 + type: u32 + doc: The local IPv4 to be used to send packets to the peer (UDP on= ly) + byte-order: big-endian + display-hint: ipv4 + - + name: local-ipv6 + type: binary + doc: The local IPv6 to be used to send packets to the peer (UDP on= ly) + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-port + type: u16 + doc: The local port to be used to send packets to the peer (UDP on= ly) + byte-order: big-endian + checks: + min: 1 + - + name: keepalive-interval + type: u32 + doc: | + The number of seconds after which a keep alive message is sent t= o the + peer + - + name: keepalive-timeout + type: u32 + doc: | + The number of seconds from the last activity after which the pee= r is + assumed dead + - + name: del-reason + type: u32 + doc: The reason why a peer was deleted + enum: del-peer-reason + - + name: vpn-rx-bytes + type: uint + doc: Number of bytes received over the tunnel + - + name: vpn-tx-bytes + type: uint + doc: Number of bytes transmitted over the tunnel + - + name: vpn-rx-packets + type: uint + doc: Number of packets received over the tunnel + - + name: vpn-tx-packets + type: uint + doc: Number of packets transmitted over the tunnel + - + name: link-rx-bytes + type: uint + doc: Number of bytes received at the transport level + - + name: link-tx-bytes + type: uint + doc: Number of bytes transmitted at the transport level + - + name: link-rx-packets + type: u32 + doc: Number of packets received at the transport level + - + name: link-tx-packets + type: u32 + doc: Number of packets transmitted at the transport level + - + name: keyconf + attributes: + - + name: peer-id + type: u32 + doc: | + The unique ID of the peer. To be used to identify peers during + key operations + checks: + max: 0xFFFFFF + - + name: slot + type: u32 + doc: The slot where the key should be stored + enum: key-slot + - + name: key-id + doc: | + The unique ID of the key. Used to fetch the correct key upon + decryption + type: u32 + checks: + max: 7 + - + name: cipher-alg + type: u32 + doc: The cipher to be used when communicating with the peer + enum: cipher-alg + - + name: encrypt-dir + type: nest + doc: Key material for encrypt direction + nested-attributes: keydir + - + name: decrypt-dir + type: nest + doc: Key material for decrypt direction + nested-attributes: keydir + - + name: keydir + attributes: + - + name: cipher-key + type: binary + doc: The actual key to be used by the cipher + checks: + max-len: 256 + - + name: nonce-tail + type: binary + doc: | + Random nonce to be concatenated to the packet ID, in order to + obtain the actual cipher IV + checks: + exact-len: nonce-tail-size + - + name: ovpn + attributes: + - + name: ifindex + type: u32 + doc: Index of the ovpn interface to operate on + - + name: ifname + type: string + doc: Name of the ovpn interface + - + name: peer + type: nest + doc: | + The peer object containing the attributed of interest for the sp= ecific + operation + nested-attributes: peer + - + name: keyconf + type: nest + doc: Peer specific cipher configuration + nested-attributes: keyconf + +operations: + list: + - + name: peer-new + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-set + attribute-set: ovpn + flags: [ admin-perm ] + doc: modify a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-get + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve data about existing remote peers (or a specific one) + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + reply: + attributes: + - peer + dump: + request: + attributes: + - ifindex + reply: + attributes: + - peer + - + name: peer-del + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-del-ntf + doc: Notification about a peer being deleted + notify: peer-get + mcgrp: peers + + - + name: key-new + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add a cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + - + name: key-get + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve non-sensitive data about peer key and cipher + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + reply: + attributes: + - keyconf + - + name: key-swap + attribute-set: ovpn + flags: [ admin-perm ] + doc: Swap primary and secondary session keys for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + - + name: key-swap-ntf + notify: key-get + doc: | + Notification about key having exhausted its IV space and requiring + renegotiation + mcgrp: peers + - + name: key-del + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + +mcast-groups: + list: + - + name: peers diff --git a/MAINTAINERS b/MAINTAINERS index b6fc8cb11a574caf5589a7d77ab8e90c780fccfa..8edccdabd96ab4a4e8e9ed24d18= ecbcd6d33ecec 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17473,7 +17473,9 @@ L: openvpn-devel@lists.sourceforge.net (moderated f= or non-subscribers) L: netdev@vger.kernel.org S: Maintained T: git https://github.com/OpenVPN/linux-kernel-ovpn.git +F: Documentation/netlink/specs/ovpn.yaml F: drivers/net/ovpn/ +F: include/uapi/linux/ovpn.h =20 P54 WIRELESS DRIVER M: Christian Lamparter diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index 53fb197027d787d6683e9056d3d341abf6ed38e4..201dc001419f1d99ae95c0ee0f9= 6e68f8a4eac16 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -9,3 +9,5 @@ obj-$(CONFIG_OVPN) :=3D ovpn.o ovpn-y +=3D main.o ovpn-y +=3D io.o +ovpn-y +=3D netlink.o +ovpn-y +=3D netlink-gen.o diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index 369a5a2b2fc1a497c8444e59f9b058eb40e49524..d5bdb0055f4dd3a6e32dc6e792b= ed1e7fd59e101 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -7,11 +7,15 @@ * James Yonan */ =20 +#include #include #include #include +#include =20 +#include "ovpnstruct.h" #include "main.h" +#include "netlink.h" #include "io.h" =20 /* Driver info */ @@ -37,7 +41,7 @@ static int ovpn_newlink(struct net *src_net, struct net_d= evice *dev, } =20 static struct rtnl_link_ops ovpn_link_ops =3D { - .kind =3D "ovpn", + .kind =3D OVPN_FAMILY_NAME, .netns_refund =3D false, .newlink =3D ovpn_newlink, .dellink =3D unregister_netdevice_queue, @@ -93,8 +97,16 @@ static int __init ovpn_init(void) goto unreg_netdev; } =20 + err =3D ovpn_nl_register(); + if (err) { + pr_err("ovpn: can't register netlink family: %d\n", err); + goto unreg_rtnl; + } + return 0; =20 +unreg_rtnl: + rtnl_link_unregister(&ovpn_link_ops); unreg_netdev: unregister_netdevice_notifier(&ovpn_netdev_notifier); return err; @@ -102,6 +114,7 @@ static int __init ovpn_init(void) =20 static __exit void ovpn_cleanup(void) { + ovpn_nl_unregister(); rtnl_link_unregister(&ovpn_link_ops); unregister_netdevice_notifier(&ovpn_netdev_notifier); =20 diff --git a/drivers/net/ovpn/netlink-gen.c b/drivers/net/ovpn/netlink-gen.c new file mode 100644 index 0000000000000000000000000000000000000000..6a43eab9a136cf0d739b9674080= d1254a43cf5d0 --- /dev/null +++ b/drivers/net/ovpn/netlink-gen.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/ovpn.yaml */ +/* YNL-GEN kernel source */ + +#include +#include + +#include "netlink-gen.h" + +#include + +/* Integer value ranges */ +static const struct netlink_range_validation ovpn_a_peer_id_range =3D { + .max =3D 16777215ULL, +}; + +static const struct netlink_range_validation ovpn_a_keyconf_peer_id_range = =3D { + .max =3D 16777215ULL, +}; + +/* Common nested types */ +const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRYPT_DIR = + 1] =3D { + [OVPN_A_KEYCONF_PEER_ID] =3D NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_keyco= nf_peer_id_range), + [OVPN_A_KEYCONF_SLOT] =3D NLA_POLICY_MAX(NLA_U32, 1), + [OVPN_A_KEYCONF_KEY_ID] =3D NLA_POLICY_MAX(NLA_U32, 7), + [OVPN_A_KEYCONF_CIPHER_ALG] =3D NLA_POLICY_MAX(NLA_U32, 2), + [OVPN_A_KEYCONF_ENCRYPT_DIR] =3D NLA_POLICY_NESTED(ovpn_keydir_nl_policy), + [OVPN_A_KEYCONF_DECRYPT_DIR] =3D NLA_POLICY_NESTED(ovpn_keydir_nl_policy), +}; + +const struct nla_policy ovpn_keydir_nl_policy[OVPN_A_KEYDIR_NONCE_TAIL + 1= ] =3D { + [OVPN_A_KEYDIR_CIPHER_KEY] =3D NLA_POLICY_MAX_LEN(256), + [OVPN_A_KEYDIR_NONCE_TAIL] =3D NLA_POLICY_EXACT_LEN(OVPN_NONCE_TAIL_SIZE), +}; + +const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_LINK_TX_PACKETS + = 1] =3D { + [OVPN_A_PEER_ID] =3D NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_peer_id_range= ), + [OVPN_A_PEER_REMOTE_IPV4] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_REMOTE_IPV6] =3D NLA_POLICY_EXACT_LEN(16), + [OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_REMOTE_PORT] =3D NLA_POLICY_MIN(NLA_U16, 1), + [OVPN_A_PEER_SOCKET] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_VPN_IPV4] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_VPN_IPV6] =3D NLA_POLICY_EXACT_LEN(16), + [OVPN_A_PEER_LOCAL_IPV4] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_LOCAL_IPV6] =3D NLA_POLICY_EXACT_LEN(16), + [OVPN_A_PEER_LOCAL_PORT] =3D NLA_POLICY_MIN(NLA_U16, 1), + [OVPN_A_PEER_KEEPALIVE_INTERVAL] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_KEEPALIVE_TIMEOUT] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_DEL_REASON] =3D NLA_POLICY_MAX(NLA_U32, 4), + [OVPN_A_PEER_VPN_RX_BYTES] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_VPN_TX_BYTES] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_VPN_RX_PACKETS] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_VPN_TX_PACKETS] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_LINK_RX_BYTES] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_LINK_TX_BYTES] =3D { .type =3D NLA_UINT, }, + [OVPN_A_PEER_LINK_RX_PACKETS] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER_LINK_TX_PACKETS] =3D { .type =3D NLA_U32, }, +}; + +/* OVPN_CMD_PEER_NEW - do */ +static const struct nla_policy ovpn_peer_new_nl_policy[OVPN_A_PEER + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER] =3D NLA_POLICY_NESTED(ovpn_peer_nl_policy), +}; + +/* OVPN_CMD_PEER_SET - do */ +static const struct nla_policy ovpn_peer_set_nl_policy[OVPN_A_PEER + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER] =3D NLA_POLICY_NESTED(ovpn_peer_nl_policy), +}; + +/* OVPN_CMD_PEER_GET - do */ +static const struct nla_policy ovpn_peer_get_do_nl_policy[OVPN_A_PEER + 1]= =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER] =3D NLA_POLICY_NESTED(ovpn_peer_nl_policy), +}; + +/* OVPN_CMD_PEER_GET - dump */ +static const struct nla_policy ovpn_peer_get_dump_nl_policy[OVPN_A_IFINDEX= + 1] =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, +}; + +/* OVPN_CMD_PEER_DEL - do */ +static const struct nla_policy ovpn_peer_del_nl_policy[OVPN_A_PEER + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_PEER] =3D NLA_POLICY_NESTED(ovpn_peer_nl_policy), +}; + +/* OVPN_CMD_KEY_NEW - do */ +static const struct nla_policy ovpn_key_new_nl_policy[OVPN_A_KEYCONF + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_KEYCONF] =3D NLA_POLICY_NESTED(ovpn_keyconf_nl_policy), +}; + +/* OVPN_CMD_KEY_GET - do */ +static const struct nla_policy ovpn_key_get_nl_policy[OVPN_A_KEYCONF + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_KEYCONF] =3D NLA_POLICY_NESTED(ovpn_keyconf_nl_policy), +}; + +/* OVPN_CMD_KEY_SWAP - do */ +static const struct nla_policy ovpn_key_swap_nl_policy[OVPN_A_KEYCONF + 1]= =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_KEYCONF] =3D NLA_POLICY_NESTED(ovpn_keyconf_nl_policy), +}; + +/* OVPN_CMD_KEY_DEL - do */ +static const struct nla_policy ovpn_key_del_nl_policy[OVPN_A_KEYCONF + 1] = =3D { + [OVPN_A_IFINDEX] =3D { .type =3D NLA_U32, }, + [OVPN_A_KEYCONF] =3D NLA_POLICY_NESTED(ovpn_keyconf_nl_policy), +}; + +/* Ops table for ovpn */ +static const struct genl_split_ops ovpn_nl_ops[] =3D { + { + .cmd =3D OVPN_CMD_PEER_NEW, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_peer_new_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_peer_new_nl_policy, + .maxattr =3D OVPN_A_PEER, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_PEER_SET, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_peer_set_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_peer_set_nl_policy, + .maxattr =3D OVPN_A_PEER, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_PEER_GET, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_peer_get_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_peer_get_do_nl_policy, + .maxattr =3D OVPN_A_PEER, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_PEER_GET, + .dumpit =3D ovpn_nl_peer_get_dumpit, + .policy =3D ovpn_peer_get_dump_nl_policy, + .maxattr =3D OVPN_A_IFINDEX, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DUMP, + }, + { + .cmd =3D OVPN_CMD_PEER_DEL, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_peer_del_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_peer_del_nl_policy, + .maxattr =3D OVPN_A_PEER, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_KEY_NEW, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_key_new_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_key_new_nl_policy, + .maxattr =3D OVPN_A_KEYCONF, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_KEY_GET, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_key_get_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_key_get_nl_policy, + .maxattr =3D OVPN_A_KEYCONF, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_KEY_SWAP, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_key_swap_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_key_swap_nl_policy, + .maxattr =3D OVPN_A_KEYCONF, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, + { + .cmd =3D OVPN_CMD_KEY_DEL, + .pre_doit =3D ovpn_nl_pre_doit, + .doit =3D ovpn_nl_key_del_doit, + .post_doit =3D ovpn_nl_post_doit, + .policy =3D ovpn_key_del_nl_policy, + .maxattr =3D OVPN_A_KEYCONF, + .flags =3D GENL_ADMIN_PERM | GENL_CMD_CAP_DO, + }, +}; + +static const struct genl_multicast_group ovpn_nl_mcgrps[] =3D { + [OVPN_NLGRP_PEERS] =3D { "peers", }, +}; + +struct genl_family ovpn_nl_family __ro_after_init =3D { + .name =3D OVPN_FAMILY_NAME, + .version =3D OVPN_FAMILY_VERSION, + .netnsok =3D true, + .parallel_ops =3D true, + .module =3D THIS_MODULE, + .split_ops =3D ovpn_nl_ops, + .n_split_ops =3D ARRAY_SIZE(ovpn_nl_ops), + .mcgrps =3D ovpn_nl_mcgrps, + .n_mcgrps =3D ARRAY_SIZE(ovpn_nl_mcgrps), +}; diff --git a/drivers/net/ovpn/netlink-gen.h b/drivers/net/ovpn/netlink-gen.h new file mode 100644 index 0000000000000000000000000000000000000000..66a4e4a0a055b4477b67801ded8= 25e9ec068b0e6 --- /dev/null +++ b/drivers/net/ovpn/netlink-gen.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/ovpn.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_OVPN_GEN_H +#define _LINUX_OVPN_GEN_H + +#include +#include + +#include + +/* Common nested types */ +extern const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRY= PT_DIR + 1]; +extern const struct nla_policy ovpn_keydir_nl_policy[OVPN_A_KEYDIR_NONCE_T= AIL + 1]; +extern const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_LINK_TX_PAC= KETS + 1]; + +int ovpn_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb, + struct genl_info *info); +void +ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, + struct genl_info *info); + +int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_peer_get_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_peer_get_dumpit(struct sk_buff *skb, struct netlink_callback *= cb); +int ovpn_nl_peer_del_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_key_new_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_key_get_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_key_swap_doit(struct sk_buff *skb, struct genl_info *info); +int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info); + +enum { + OVPN_NLGRP_PEERS, +}; + +extern struct genl_family ovpn_nl_family; + +#endif /* _LINUX_OVPN_GEN_H */ diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c new file mode 100644 index 0000000000000000000000000000000000000000..2cc34eb1d1d870c6705714cb971= c3c5dfb04afda --- /dev/null +++ b/drivers/net/ovpn/netlink.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#include +#include + +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "netlink.h" +#include "netlink-gen.h" + +MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME); + +/** + * ovpn_get_dev_from_attrs - retrieve the ovpn private data from the netde= vice + * a netlink message is targeting + * @net: network namespace where to look for the interface + * @info: generic netlink info from the user request + * + * Return: the ovpn private data, if found, or an error otherwise + */ +static struct ovpn_struct * +ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info) +{ + struct ovpn_struct *ovpn; + struct net_device *dev; + int ifindex; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_IFINDEX)) + return ERR_PTR(-EINVAL); + + ifindex =3D nla_get_u32(info->attrs[OVPN_A_IFINDEX]); + + rcu_read_lock(); + dev =3D dev_get_by_index_rcu(net, ifindex); + if (!dev) { + rcu_read_unlock(); + NL_SET_ERR_MSG_MOD(info->extack, + "ifindex does not match any interface"); + return ERR_PTR(-ENODEV); + } + + if (!ovpn_dev_is_valid(dev)) { + rcu_read_unlock(); + NL_SET_ERR_MSG_MOD(info->extack, + "specified interface is not ovpn"); + NL_SET_BAD_ATTR(info->extack, info->attrs[OVPN_A_IFINDEX]); + return ERR_PTR(-EINVAL); + } + + ovpn =3D netdev_priv(dev); + netdev_hold(dev, &ovpn->dev_tracker, GFP_KERNEL); + rcu_read_unlock(); + + return ovpn; +} + +int ovpn_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb, + struct genl_info *info) +{ + struct ovpn_struct *ovpn =3D ovpn_get_dev_from_attrs(genl_info_net(info), + info); + + if (IS_ERR(ovpn)) + return PTR_ERR(ovpn); + + info->user_ptr[0] =3D ovpn; + + return 0; +} + +void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *s= kb, + struct genl_info *info) +{ + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + + if (ovpn) + netdev_put(ovpn->dev, &ovpn->dev_tracker); +} + +int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_peer_get_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_peer_get_dumpit(struct sk_buff *skb, struct netlink_callback *= cb) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_peer_del_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_key_new_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_key_get_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_key_swap_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info) +{ + return -EOPNOTSUPP; +} + +/** + * ovpn_nl_register - perform any needed registration in the NL subsustem + * + * Return: 0 on success, a negative error code otherwise + */ +int __init ovpn_nl_register(void) +{ + int ret =3D genl_register_family(&ovpn_nl_family); + + if (ret) { + pr_err("ovpn: genl_register_family failed: %d\n", ret); + return ret; + } + + return 0; +} + +/** + * ovpn_nl_unregister - undo any module wide netlink registration + */ +void ovpn_nl_unregister(void) +{ + genl_unregister_family(&ovpn_nl_family); +} diff --git a/drivers/net/ovpn/netlink.h b/drivers/net/ovpn/netlink.h new file mode 100644 index 0000000000000000000000000000000000000000..9e87cf11d1e9813b7a75ddf3705= ab7d5fabe899f --- /dev/null +++ b/drivers/net/ovpn/netlink.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#ifndef _NET_OVPN_NETLINK_H_ +#define _NET_OVPN_NETLINK_H_ + +int ovpn_nl_register(void); +void ovpn_nl_unregister(void); + +#endif /* _NET_OVPN_NETLINK_H_ */ diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h new file mode 100644 index 0000000000000000000000000000000000000000..e3e4df6418b081436378fc51d98= db5bd7b5d1fbe --- /dev/null +++ b/drivers/net/ovpn/ovpnstruct.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#ifndef _NET_OVPN_OVPNSTRUCT_H_ +#define _NET_OVPN_OVPNSTRUCT_H_ + +#include + +/** + * struct ovpn_struct - per ovpn interface state + * @dev: the actual netdev representing the tunnel + * @dev_tracker: reference tracker for associated dev + */ +struct ovpn_struct { + struct net_device *dev; + netdevice_tracker dev_tracker; +}; + +#endif /* _NET_OVPN_OVPNSTRUCT_H_ */ diff --git a/include/uapi/linux/ovpn.h b/include/uapi/linux/ovpn.h new file mode 100644 index 0000000000000000000000000000000000000000..7bac0803cd9fd0dde13f4db74ac= ce8d9df5316d8 --- /dev/null +++ b/include/uapi/linux/ovpn.h @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Cl= ause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/ovpn.yaml */ +/* YNL-GEN uapi header */ + +#ifndef _UAPI_LINUX_OVPN_H +#define _UAPI_LINUX_OVPN_H + +#define OVPN_FAMILY_NAME "ovpn" +#define OVPN_FAMILY_VERSION 1 + +#define OVPN_NONCE_TAIL_SIZE 8 + +enum ovpn_cipher_alg { + OVPN_CIPHER_ALG_NONE, + OVPN_CIPHER_ALG_AES_GCM, + OVPN_CIPHER_ALG_CHACHA20_POLY1305, +}; + +enum ovpn_del_peer_reason { + OVPN_DEL_PEER_REASON_TEARDOWN, + OVPN_DEL_PEER_REASON_USERSPACE, + OVPN_DEL_PEER_REASON_EXPIRED, + OVPN_DEL_PEER_REASON_TRANSPORT_ERROR, + OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT, +}; + +enum ovpn_key_slot { + OVPN_KEY_SLOT_PRIMARY, + OVPN_KEY_SLOT_SECONDARY, +}; + +enum { + OVPN_A_PEER_ID =3D 1, + OVPN_A_PEER_REMOTE_IPV4, + OVPN_A_PEER_REMOTE_IPV6, + OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID, + OVPN_A_PEER_REMOTE_PORT, + OVPN_A_PEER_SOCKET, + OVPN_A_PEER_VPN_IPV4, + OVPN_A_PEER_VPN_IPV6, + OVPN_A_PEER_LOCAL_IPV4, + OVPN_A_PEER_LOCAL_IPV6, + OVPN_A_PEER_LOCAL_PORT, + OVPN_A_PEER_KEEPALIVE_INTERVAL, + OVPN_A_PEER_KEEPALIVE_TIMEOUT, + OVPN_A_PEER_DEL_REASON, + OVPN_A_PEER_VPN_RX_BYTES, + OVPN_A_PEER_VPN_TX_BYTES, + OVPN_A_PEER_VPN_RX_PACKETS, + OVPN_A_PEER_VPN_TX_PACKETS, + OVPN_A_PEER_LINK_RX_BYTES, + OVPN_A_PEER_LINK_TX_BYTES, + OVPN_A_PEER_LINK_RX_PACKETS, + OVPN_A_PEER_LINK_TX_PACKETS, + + __OVPN_A_PEER_MAX, + OVPN_A_PEER_MAX =3D (__OVPN_A_PEER_MAX - 1) +}; + +enum { + OVPN_A_KEYCONF_PEER_ID =3D 1, + OVPN_A_KEYCONF_SLOT, + OVPN_A_KEYCONF_KEY_ID, + OVPN_A_KEYCONF_CIPHER_ALG, + OVPN_A_KEYCONF_ENCRYPT_DIR, + OVPN_A_KEYCONF_DECRYPT_DIR, + + __OVPN_A_KEYCONF_MAX, + OVPN_A_KEYCONF_MAX =3D (__OVPN_A_KEYCONF_MAX - 1) +}; + +enum { + OVPN_A_KEYDIR_CIPHER_KEY =3D 1, + OVPN_A_KEYDIR_NONCE_TAIL, + + __OVPN_A_KEYDIR_MAX, + OVPN_A_KEYDIR_MAX =3D (__OVPN_A_KEYDIR_MAX - 1) +}; + +enum { + OVPN_A_IFINDEX =3D 1, + OVPN_A_IFNAME, + OVPN_A_PEER, + OVPN_A_KEYCONF, + + __OVPN_A_MAX, + OVPN_A_MAX =3D (__OVPN_A_MAX - 1) +}; + +enum { + OVPN_CMD_PEER_NEW =3D 1, + OVPN_CMD_PEER_SET, + OVPN_CMD_PEER_GET, + OVPN_CMD_PEER_DEL, + OVPN_CMD_PEER_DEL_NTF, + OVPN_CMD_KEY_NEW, + OVPN_CMD_KEY_GET, + OVPN_CMD_KEY_SWAP, + OVPN_CMD_KEY_SWAP_NTF, + OVPN_CMD_KEY_DEL, + + __OVPN_CMD_MAX, + OVPN_CMD_MAX =3D (__OVPN_CMD_MAX - 1) +}; + +#define OVPN_MCGRP_PEERS "peers" + +#endif /* _UAPI_LINUX_OVPN_H */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 284EC17333A for ; Wed, 16 Oct 2024 01:03:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040631; cv=none; b=OG2d38ITsVdKb+r4RWyOANXYyTNNzL/5AIYXCjoqmQA9+h630B3dKaN9Ruv9St17Z2zRsgnajqBSElBzRV12hXJescOf/o9FGGFr3EseoXba4EdV4AU2ycexNFO5inZLMenluVYINIsVu/BYk6jgtyyYB/NAS3k+ZkWj57GBM14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040631; c=relaxed/simple; bh=NHB8DY+derJjmpIyfM7tZseTb7zJiJA5OcsJ97CZXUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DJwCchF/TGLMaB+TEaq0RW9jrdjHT3lXCCUe2ZoOmMheFLnwFb+4q8rqh4OdVNjOV5P553PEPI/6N1ZEJIqBDJyMLp+9Nbl8t+1h0IqZwOB4HxxIiby/xXmxkC2cQRTPOJJhPjMmf3i+C+y895XfMpKuKr1SVjWcYYTr4ELzETQ= 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=GFlu8nDv; arc=none smtp.client-ip=209.85.221.44 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="GFlu8nDv" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-37d447de11dso4299398f8f.1 for ; Tue, 15 Oct 2024 18:03:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040627; x=1729645427; 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=TpOrbfvwpuTF+GzXl29fSyJFnaQGQgrW9YLo7vYL7GQ=; b=GFlu8nDvU8ym4plBpYObwfBr4GM3spg2bES1HO/BwJYswrvBowbKFrxJdDxRQjQ9fp km7DpQ/6cpSHJ0YijlO//V3fbfWPcg5zwykhjEcy1jrNb3vC4BXjRqoAd0EuSxnbljlD 2BHPwaayhxNdRoS08ySl871t9SAYa2BsEPuDti1wwzoHH+XT/xI47qrHI1+tVZms3M+5 Cv35I2X18UnTf2MVXSSavI/K1aLJ0XnP2In6syhUrVMott4lUq875g8N7Bb3B1ml3Iy/ 1Eyah+5J10I0ZeZz1dCZtCabGmXJTg7SJ9pniKNKdy7WsRAl+5NIBWjioJKlynX0ZzMH pBfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040627; x=1729645427; 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=TpOrbfvwpuTF+GzXl29fSyJFnaQGQgrW9YLo7vYL7GQ=; b=RzRAwVxQOkRRDLvc23utcP1tB7rOa9Kr+cwh9gVvPQu9DtOASJ1jykPqhihPm40eya cfokyWPQfLLdSYBTepyPYAKj527NpEK+ALU4duzEFUqhQA+0oLz7IG7LQUgIrfaqyrJH T15QsKSnkGearERuYqZ86LU5u9Z/+VEvOcvzP4LAnqvtDa9n4OaT+wcYxCW7O0siW9bb s4c3Mjv8bH4SCvCrcml9qImO7Xqb5kvPIXA1V86K46YJakNA38bO/jgbN+hZz/WhUfCy kzdLvz1HivOZUO+CFXp3oK5CC0FPhKFYKe2LCTKAIUZ07vqKI0S1pZDkhhJdWAi1H/O6 XSsw== X-Forwarded-Encrypted: i=1; AJvYcCUr4lz5MwKodht1igk+MG3KgKwyruPo5i+v0XuJwM0LCDkJpTCGPYYn9J4hGPbsZHHkTZ5rK8gmKzujiQ0=@vger.kernel.org X-Gm-Message-State: AOJu0YyoXQ3ClxuVrGGmQB55i17RQhBU0xTGwVMOXxgB74cyTRotzjm2 FzxhzaVlWLxUMva0q0HBs8IC0yu+9nNHhv4sOHAyfrVbWal9VtxnFMOKQdH1xxo= X-Google-Smtp-Source: AGHT+IEgRR0HXgj6AwjKVKrbrUdJxb6D024PWAiXC5w+HqbSHwJSYdWQwcdtZoxTbXfk5pochHjgrw== X-Received: by 2002:a05:6000:4f0:b0:37d:5103:e41d with SMTP id ffacd0b85a97d-37d5529f022mr12221314f8f.39.1729040627413; Tue, 15 Oct 2024 18:03:47 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:46 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:04 +0200 Subject: [PATCH net-next v9 04/23] ovpn: add basic interface creation/destruction/management routines 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: <20241016-b4-ovpn-v9-4-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=8751; i=antonio@openvpn.net; h=from:subject:message-id; bh=NHB8DY+derJjmpIyfM7tZseTb7zJiJA5OcsJ97CZXUw=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDuFjOrx4IReeKRet9RXdFXSQGkqN4aZo+2j qqOgfFZAHmJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7gAKCRALcOU6oDjV h4odCACUq48RqG6cy5lVgzibosvcPAyXUBieDxMW+qI2uxJAn2m5t5jufQ+vk/Exx2DPo2k7pqD +AH+3ggnNjc70beVOXufRTvWTw11kL+yZjy1iFtcWdoLVsaRVHENxSMXmUUV/82yEjN3w5jA3U7 chKk4Vmd2p9CqcqATjeV6tQWQSQ/vadF0p2XkJuzjaWOXNNmbdNzIuji6NCWSwEmKXVIx/EDstQ yDR/o3O+qSHEeIhIuzpVD6pYDKtk9ksVsj6Uzosb8ImzdVGS5sH8/ABFwVCxh0cZISyxWz0Csjc 5UjWX11VBNSh5521K2BORkUhIt1u0LcmlgX7jDzvlIYheFlW X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Add basic infrastructure for handling ovpn interfaces. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/main.c | 115 ++++++++++++++++++++++++++++++++++++++= ++-- drivers/net/ovpn/main.h | 7 +++ drivers/net/ovpn/ovpnstruct.h | 8 +++ drivers/net/ovpn/packet.h | 40 +++++++++++++++ include/uapi/linux/if_link.h | 15 ++++++ 5 files changed, 180 insertions(+), 5 deletions(-) diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index d5bdb0055f4dd3a6e32dc6e792bed1e7fd59e101..eead7677b8239eb3c48bb26ca95= 492d88512b8d4 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -10,18 +10,52 @@ #include #include #include +#include +#include #include -#include +#include =20 #include "ovpnstruct.h" #include "main.h" #include "netlink.h" #include "io.h" +#include "packet.h" =20 /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)" #define DRV_COPYRIGHT "(C) 2020-2024 OpenVPN, Inc." =20 +static void ovpn_struct_free(struct net_device *net) +{ +} + +static int ovpn_net_open(struct net_device *dev) +{ + netif_tx_start_all_queues(dev); + return 0; +} + +static int ovpn_net_stop(struct net_device *dev) +{ + netif_tx_stop_all_queues(dev); + return 0; +} + +static const struct net_device_ops ovpn_netdev_ops =3D { + .ndo_open =3D ovpn_net_open, + .ndo_stop =3D ovpn_net_stop, + .ndo_start_xmit =3D ovpn_net_xmit, +}; + +static const struct device_type ovpn_type =3D { + .name =3D OVPN_FAMILY_NAME, +}; + +static const struct nla_policy ovpn_policy[IFLA_OVPN_MAX + 1] =3D { + [IFLA_OVPN_MODE] =3D NLA_POLICY_RANGE(NLA_U8, OVPN_MODE_P2P, + OVPN_MODE_MP), +}; + /** * ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' * @dev: the interface to check @@ -33,16 +67,76 @@ bool ovpn_dev_is_valid(const struct net_device *dev) return dev->netdev_ops->ndo_start_xmit =3D=3D ovpn_net_xmit; } =20 +static void ovpn_setup(struct net_device *dev) +{ + /* compute the overhead considering AEAD encryption */ + const int overhead =3D sizeof(u32) + NONCE_WIRE_SIZE + 16 + + sizeof(struct udphdr) + + max(sizeof(struct ipv6hdr), sizeof(struct iphdr)); + + netdev_features_t feat =3D NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM | + NETIF_F_GSO | NETIF_F_GSO_SOFTWARE | + NETIF_F_HIGHDMA; + + dev->needs_free_netdev =3D true; + + dev->pcpu_stat_type =3D NETDEV_PCPU_STAT_TSTATS; + + dev->netdev_ops =3D &ovpn_netdev_ops; + + dev->priv_destructor =3D ovpn_struct_free; + + dev->hard_header_len =3D 0; + dev->addr_len =3D 0; + dev->mtu =3D ETH_DATA_LEN - overhead; + dev->min_mtu =3D IPV4_MIN_MTU; + dev->max_mtu =3D IP_MAX_MTU - overhead; + + dev->type =3D ARPHRD_NONE; + dev->flags =3D IFF_POINTOPOINT | IFF_NOARP; + dev->priv_flags |=3D IFF_NO_QUEUE; + + dev->lltx =3D true; + dev->features |=3D feat; + dev->hw_features |=3D feat; + dev->hw_enc_features |=3D feat; + + dev->needed_headroom =3D OVPN_HEAD_ROOM; + dev->needed_tailroom =3D OVPN_MAX_PADDING; + + SET_NETDEV_DEVTYPE(dev, &ovpn_type); +} + static int ovpn_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { - return -EOPNOTSUPP; + struct ovpn_struct *ovpn =3D netdev_priv(dev); + enum ovpn_mode mode =3D OVPN_MODE_P2P; + + if (data && data[IFLA_OVPN_MODE]) { + mode =3D nla_get_u8(data[IFLA_OVPN_MODE]); + netdev_dbg(dev, "setting device mode: %u\n", mode); + } + + ovpn->dev =3D dev; + ovpn->mode =3D mode; + + /* turn carrier explicitly off after registration, this way state is + * clearly defined + */ + netif_carrier_off(dev); + + return register_netdevice(dev); } =20 static struct rtnl_link_ops ovpn_link_ops =3D { .kind =3D OVPN_FAMILY_NAME, .netns_refund =3D false, + .priv_size =3D sizeof(struct ovpn_struct), + .setup =3D ovpn_setup, + .policy =3D ovpn_policy, + .maxtype =3D IFLA_OVPN_MAX, .newlink =3D ovpn_newlink, .dellink =3D unregister_netdevice_queue, }; @@ -51,26 +145,37 @@ static int ovpn_netdev_notifier_call(struct notifier_b= lock *nb, unsigned long state, void *ptr) { struct net_device *dev =3D netdev_notifier_info_to_dev(ptr); + struct ovpn_struct *ovpn; =20 if (!ovpn_dev_is_valid(dev)) return NOTIFY_DONE; =20 + ovpn =3D netdev_priv(dev); + switch (state) { case NETDEV_REGISTER: - /* add device to internal list for later destruction upon - * unregistration - */ + ovpn->registered =3D true; break; case NETDEV_UNREGISTER: + /* twiddle thumbs on netns device moves */ + if (dev->reg_state !=3D NETREG_UNREGISTERING) + break; + /* can be delivered multiple times, so check registered flag, * then destroy the interface */ + if (!ovpn->registered) + return NOTIFY_DONE; + + netif_carrier_off(dev); + ovpn->registered =3D false; break; case NETDEV_POST_INIT: case NETDEV_GOING_DOWN: case NETDEV_DOWN: case NETDEV_UP: case NETDEV_PRE_UP: + break; default: return NOTIFY_DONE; } diff --git a/drivers/net/ovpn/main.h b/drivers/net/ovpn/main.h index a3215316c49bfcdf2496590bac878f145b8b27fd..0740a05070a817e0daea7b63a1f= 4fcebd274eb37 100644 --- a/drivers/net/ovpn/main.h +++ b/drivers/net/ovpn/main.h @@ -12,4 +12,11 @@ =20 bool ovpn_dev_is_valid(const struct net_device *dev); =20 +#define SKB_HEADER_LEN \ + (max(sizeof(struct iphdr), sizeof(struct ipv6hdr)) + \ + sizeof(struct udphdr) + NET_SKB_PAD) + +#define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4) +#define OVPN_MAX_PADDING 16 + #endif /* _NET_OVPN_MAIN_H_ */ diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h index e3e4df6418b081436378fc51d98db5bd7b5d1fbe..211df871538d34fdff90d182f21= a0b0fb11b28ad 100644 --- a/drivers/net/ovpn/ovpnstruct.h +++ b/drivers/net/ovpn/ovpnstruct.h @@ -11,15 +11,23 @@ #define _NET_OVPN_OVPNSTRUCT_H_ =20 #include +#include +#include =20 /** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel * @dev_tracker: reference tracker for associated dev + * @registered: whether dev is still registered with netdev or not + * @mode: device operation mode (i.e. p2p, mp, ..) + * @dev_list: entry for the module wide device list */ struct ovpn_struct { struct net_device *dev; netdevice_tracker dev_tracker; + bool registered; + enum ovpn_mode mode; + struct list_head dev_list; }; =20 #endif /* _NET_OVPN_OVPNSTRUCT_H_ */ diff --git a/drivers/net/ovpn/packet.h b/drivers/net/ovpn/packet.h new file mode 100644 index 0000000000000000000000000000000000000000..7ed146f5932a25f448af6da5873= 8a7eae81007fe --- /dev/null +++ b/drivers/net/ovpn/packet.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#ifndef _NET_OVPN_PACKET_H_ +#define _NET_OVPN_PACKET_H_ + +/* When the OpenVPN protocol is ran in AEAD mode, use + * the OpenVPN packet ID as the AEAD nonce: + * + * 00000005 521c3b01 4308c041 + * [seq # ] [ nonce_tail ] + * [ 12-byte full IV ] -> NONCE_SIZE + * [4-bytes -> NONCE_WIRE_SIZE + * on wire] + */ + +/* OpenVPN nonce size */ +#define NONCE_SIZE 12 + +/* OpenVPN nonce size reduced by 8-byte nonce tail -- this is the + * size of the AEAD Associated Data (AD) sent over the wire + * and is normally the head of the IV + */ +#define NONCE_WIRE_SIZE (NONCE_SIZE - sizeof(struct ovpn_nonce_tail)) + +/* Last 8 bytes of AEAD nonce + * Provided by userspace and usually derived from + * key material generated during TLS handshake + */ +struct ovpn_nonce_tail { + u8 u8[OVPN_NONCE_TAIL_SIZE]; +}; + +#endif /* _NET_OVPN_PACKET_H_ */ diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index 8516c1ccd57a7c7634a538fe3ac16c858f647420..84d294aab20b79b8e9cb9b736a0= 74105c99338f3 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -1975,4 +1975,19 @@ enum { =20 #define IFLA_DSA_MAX (__IFLA_DSA_MAX - 1) =20 +/* OVPN section */ + +enum ovpn_mode { + OVPN_MODE_P2P, + OVPN_MODE_MP, +}; + +enum { + IFLA_OVPN_UNSPEC, + IFLA_OVPN_MODE, + __IFLA_OVPN_MAX, +}; + +#define IFLA_OVPN_MAX (__IFLA_OVPN_MAX - 1) + #endif /* _UAPI_LINUX_IF_LINK_H */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 5178D17C9FA for ; Wed, 16 Oct 2024 01:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040633; cv=none; b=L1rg3TMltJwPzGX+Y5kLPSx51h468hqtoetOhnYlim8UQ2XXi4NE1PA4xFOtQUJPpTFj6jLwW1AEMEnZsA7TVNu1Xy3DITXwyPtmzOwjdDiIsUpFnSfdQNP1w38/0e/P/gnLLWqUcYO1sAtEETbyEifXkL5NZrJ0Fv/CjZ8x9xo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040633; c=relaxed/simple; bh=fdzF3OOmoZvBMLXtUajHYKsI45JNjgatz8aXU4UpXD0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QsPoyHVLovofXe5mr1au7f0QpaBGzlujeWhxwRxqLcL24U9Qe2r3PVUwTaG7PBx5uqfrM1mBVTQOgOcGBBC6rPmXTFK9nEyFmUzlOG9ZMdmMj9I84G+pFzlZbzJSDGJp7MWVlj4HwoyufKBnruUBoRrQUpANwZ3ZMHd+74JBX2s= 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=QsV42jwq; arc=none smtp.client-ip=209.85.128.51 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="QsV42jwq" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4311fd48032so33835505e9.0 for ; Tue, 15 Oct 2024 18:03:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040629; x=1729645429; 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=XbrFnZJaX61MB7xNUng7QgrtBfqDJUowzpqh7fH2eTs=; b=QsV42jwqCGaFrGox08Yx6k0niJguqMFeqlePDJd+cEUxDD58gnRWfuYmrSe/tvnTda RgF6NPd2K5A47VWHoDvCfqMIBedcvOQClhrh2gFwwwnrDP3pr/slgPJzor84NVCLpMLZ gynwrqAv+LR7iKKbW2D7veqTUWXhPaVRW8kzIFJmK+6UpsH14O+m1UUhlI4aAFuA5aPR ki/wRBJwWZil8v725d6ByPUz0ghZyIoYbyxYwE/qzThfj02qvrfQY78S/cnt2MMTXOA5 FSBwzGsk1VTxvnDisLWwm5KiFCbtRc63Hon/GipYEXJTNc04fsu5aJhz4oDjUlor6QsG MvNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040629; x=1729645429; 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=XbrFnZJaX61MB7xNUng7QgrtBfqDJUowzpqh7fH2eTs=; b=HbhNPJp1axHmiHrKiOH7oNGV81+5fwIURxmyiXDap5Uq4IdAPHcK529OGiWtjc1+By xMlN+i3jDg9sjAjPrpj9udRmKlYO6ryYFHUuU9C72sLEgTSl974gZbeOr4G/W7aWs9vY jdtpEgu/eZqiqdKHTBS6yIuhQ7ySkynv2fN4rWok4d0IKhUXUBvfzsbvUMKDBvtzBL5L dSxYoHeSfwIpdgFwkyeEBsbM/+5qPTK0EgtnC28vxX4vs+pw4M3TymCaOK40RRh+M99u 3xYQ5uZvRCIoNFE8mlPEKmfU8eofvFjtkfDB7F2JLnUrrR0C13XrqA1k7sWFULWmSRDt qZEw== X-Forwarded-Encrypted: i=1; AJvYcCW3LnckLujY3jrPD2sYlR4IJbE3YKUaW+0YZrJWhZDy6BxoU/B2QY1cCdYCjzHJigWc1YK4jbDBMU2u/90=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5YM8G+jCBbl0FagALhnn3uooAxsjoPOV20OyQj0/RdBk94K5Z iXqaBYR8RAoLev2b/Wjmqz95yHnzIBtFV8i5dqhPjkJxX4aJtU2RYTfhItudp/8= X-Google-Smtp-Source: AGHT+IEnmJET+/tKtmD4N3aGzKGV26mdwP3+N26qkziS2iQ4M91Pcy6VsuNDGx9B77czVLSa40BH0g== X-Received: by 2002:a05:600c:1c8b:b0:425:7884:6b29 with SMTP id 5b1f17b1804b1-4314a301656mr17181865e9.19.1729040629512; Tue, 15 Oct 2024 18:03:49 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:48 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:05 +0200 Subject: [PATCH net-next v9 05/23] ovpn: keep carrier always on 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: <20241016-b4-ovpn-v9-5-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1171; i=antonio@openvpn.net; h=from:subject:message-id; bh=fdzF3OOmoZvBMLXtUajHYKsI45JNjgatz8aXU4UpXD0=; b=owEBbAGT/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvTHnFs08ojTWCIYtTGaDcKU9BD/aiQk8ra 20+m/SX2X+JATIEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV hxarB/d7snZW+3I30+pGkneDkzXL+PpZTWCt2UX1AmWm1RzSct/CkRq3JNhptlkHNmwZcdaoACo FyaW93SsXWDoh8qpAPZ72pW/YZSkVwZftK2urpC6cP7fngRpbcJVxUaVWgT3AEv569LY+EuAObF Se0dd04K3S3PX1i4s4hL9cnSv2Jt5nnT9RLIg90G0Cbz7Si9p/4wLDLWbAYUqjZsNIdVIS1kGgx O2XA/ituOjrslh/WqIXix2HHqxhjTyIBokcZ6zJuykX6Snn9vSGhcGlHAZdhtCXFabfM79N1mPf pKAgpbwThr3UNkOorPKPhxcqqEsvquM7p2glA1OPWgDsBkM= X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C An ovpn interface will keep carrier always on and let the user decide when an interface should be considered disconnected. This way, even if an ovpn interface is not connected to any peer, it can still retain all IPs and routes and thus prevent any data leak. Signed-off-by: Antonio Quartulli Reviewed-by: Andrew Lunn --- drivers/net/ovpn/main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index eead7677b8239eb3c48bb26ca95492d88512b8d4..eaa83a8662e4ac2c75820100826= 8f9633643c0b6 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -31,6 +31,13 @@ static void ovpn_struct_free(struct net_device *net) =20 static int ovpn_net_open(struct net_device *dev) { + /* ovpn keeps the carrier always on to avoid losing IP or route + * configuration upon disconnection. This way it can prevent leaks + * of traffic outside of the VPN tunnel. + * The user may override this behaviour by tearing down the interface + * manually. + */ + netif_carrier_on(dev); netif_tx_start_all_queues(dev); return 0; } --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 453E31865E7 for ; Wed, 16 Oct 2024 01:03:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040637; cv=none; b=I8IQ9wIiu4a1mvVvnGFrYn77Nyn3y4kaa16+tg6K3h2im8zc8tphpq5QfJzcIpvrgRyjeoesDBq58JTlbECQ7zJIK5F6TC9MoF3RbFcU9FXyuYdc2V2+ku86MkBpeYYork/Kd5CIQMAWiDzSaCJVt+swmv6uEyFeBxfa0o4YECE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040637; c=relaxed/simple; bh=BY/mL4MgwI3tzOibro+ehaS6Chh0fNwKxaLThxcymXU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IBeEVh5LeeFIKb1tyE51/62j+yOVZgg0/0TWAos/c2Aqz+b3TLiXy1PulxObyqFXIEhc3yoDpJu4688X8hWV4JoHK6wqulFwMs+l6k8IF7JzjzcnUvXvHJLoZgFWlPXBZSfRQmTKYNc3CBTdihmYLtn9iJ77NNYBVixjGkFjtS0= 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=HU2VizAw; arc=none smtp.client-ip=209.85.128.53 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="HU2VizAw" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-430f6bc9ca6so47347835e9.2 for ; Tue, 15 Oct 2024 18:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040632; x=1729645432; 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=dWPh8UK8+CF4Oe7rxvDj0NUUP4Gu8zcF7HLlD9d/Bzk=; b=HU2VizAw8vzvUuwnoTw9FkaG15a7Sm2NbLf150ROvuxx7jNIrzA9sH/BLr1oUr7IdU efkaXI4SMH8VLgCSdtfOdN0RnedxqR1+ogSuw6nmmVarGZj6JRhhTQ8Y3vHf7N8LHdHg H5PYqA/sCTh8zYjdM1KJZP7YMWrkcqsss1AIQx/5ye6jIzFF7i5dMSoxaoP2ApTy44An H0L/+xa/2FI2uPnvIc01oPl0xJfyBNB1ofcVED1dy0tQmOf5Fq0ee0tD2gGyNB0SDuMk wYWSY8KNO19nAtHu2Oy/R5AHTYcZtFyOI3TW99UmrnNOZTs04ha/yRleLN+CcziGM4ad MsBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040632; x=1729645432; 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=dWPh8UK8+CF4Oe7rxvDj0NUUP4Gu8zcF7HLlD9d/Bzk=; b=u8piaRt9FHqfV3cuDMzsEEWfh6EditcyCCc2eBhWlRIYvaphyvW4s3Rj+iEejlg7/w UJjNDXc7nJgpZNKMCzy7Zv4YHHMgeCNrnRog1vlLBmHx+8JgWOfMpUf/q0EpWiux+QzE mhd9CyEcKta+EqIuHXv+Urvx8o4KUgVUtzgKIDdBKaAfG4dufSGN/pTvjRV+fEfQiLFe SCXDA2OoPKfs5T+xE+DYoYfQ9m8A9u5oxUKvScxbFOqCuoZExXyaYFzCkNpFu4t7mLMg W36XABHiv3UK4LheA835hGPnIYzzYIkWJ1u+YwRpSOa/4ql642/X4sqlS4Q6iRvcYrSU Ve3w== X-Forwarded-Encrypted: i=1; AJvYcCWDejO3fss50PCCAQRQ6QMOtyefwVVeqQUU3+R1bCwQ4fFw3jRyPrVKwgK0qAXV7kk9gLrSzwSeRyq0ZsY=@vger.kernel.org X-Gm-Message-State: AOJu0Yxkjeu+i+EVTdBCTI5RXTH36g/9H1zpj6L4OijwP41pPLo1SMxE jxgVBvC9Dnk89JDxdNF9ordiJmgiSxxPAdWvGY2x9359opXmJR9lkIBFboD+ha4= X-Google-Smtp-Source: AGHT+IH2ea1o6oqV/qJbAjEBIyFrRdRfyyCbfKuJsRL+NeN8C+VFDO0P44JxaghPEvyzpunpfYqctQ== X-Received: by 2002:adf:f70c:0:b0:37c:c842:a16e with SMTP id ffacd0b85a97d-37d5fe96419mr10625588f8f.5.1729040631601; Tue, 15 Oct 2024 18:03:51 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:51 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:06 +0200 Subject: [PATCH net-next v9 06/23] ovpn: introduce the ovpn_peer 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: <20241016-b4-ovpn-v9-6-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=21525; i=antonio@openvpn.net; h=from:subject:message-id; bh=BY/mL4MgwI3tzOibro+ehaS6Chh0fNwKxaLThxcymXU=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvMdmjnTo4GgiGW03u2klY1nCWeyqq1oLpD GP3HJcW2bWJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV hzuZB/96Pfyqeo1KL7I6xcPQHA1wD3SjsB5ZdbImfkbwhH9DigiOiNX6zA/7x9b5KklIVJ+SBWb IttGGEAM3fsaneJIjiXLcQpoH2GIqM+GPROVYJrhnqcgv1Ne8M+l1K0/n9XDDzQaKABvC04gOeU 7jF11xdwY+1GbmNlyIOJYaZUqLyG9f7mq4S7ynV780xXBV1PgFycFw8Ip6BydFmoFCvPL3OfUKg MowHIdMFTOOUz20MUMMagP3jM3CHIMXNoRttuz+xvyLF9iI++6Cg+08Cj2LzhDldWmR/HBOPjDI 7/umQevGl7r3S+bHDWKtMjJCMJyvyb32pBCP6Qgb/jNnE2sJ X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C An ovpn_peer object holds the whole status of a remote peer (regardless whether it is a server or a client). This includes status for crypto, tx/rx buffers, napi, etc. Only support for one peer is introduced (P2P mode). Multi peer support is introduced with a later patch. Along with the ovpn_peer, also the ovpn_bind object is introcued as the two are strictly related. An ovpn_bind object wraps a sockaddr representing the local coordinates being used to talk to a specific peer. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/bind.c | 58 +++++++ drivers/net/ovpn/bind.h | 117 ++++++++++++++ drivers/net/ovpn/main.c | 11 ++ drivers/net/ovpn/main.h | 2 + drivers/net/ovpn/ovpnstruct.h | 4 + drivers/net/ovpn/peer.c | 354 ++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/peer.h | 79 ++++++++++ 8 files changed, 627 insertions(+) diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index 201dc001419f1d99ae95c0ee0f96e68f8a4eac16..ce13499b3e1775a7f2a9ce16c6c= b0aa088f93685 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -7,7 +7,9 @@ # Author: Antonio Quartulli =20 obj-$(CONFIG_OVPN) :=3D ovpn.o +ovpn-y +=3D bind.o ovpn-y +=3D main.o ovpn-y +=3D io.o ovpn-y +=3D netlink.o ovpn-y +=3D netlink-gen.o +ovpn-y +=3D peer.o diff --git a/drivers/net/ovpn/bind.c b/drivers/net/ovpn/bind.c new file mode 100644 index 0000000000000000000000000000000000000000..b4d2ccec2ceddf43bc445b489cc= 62a578ef0ad0a --- /dev/null +++ b/drivers/net/ovpn/bind.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2012-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#include +#include + +#include "ovpnstruct.h" +#include "bind.h" +#include "peer.h" + +/** + * ovpn_bind_from_sockaddr - retrieve binding matching sockaddr + * @ss: the sockaddr to match + * + * Return: the bind matching the passed sockaddr if found, NULL otherwise + */ +struct ovpn_bind *ovpn_bind_from_sockaddr(const struct sockaddr_storage *s= s) +{ + struct ovpn_bind *bind; + size_t sa_len; + + if (ss->ss_family =3D=3D AF_INET) + sa_len =3D sizeof(struct sockaddr_in); + else if (ss->ss_family =3D=3D AF_INET6) + sa_len =3D sizeof(struct sockaddr_in6); + else + return ERR_PTR(-EAFNOSUPPORT); + + bind =3D kzalloc(sizeof(*bind), GFP_ATOMIC); + if (unlikely(!bind)) + return ERR_PTR(-ENOMEM); + + memcpy(&bind->remote, ss, sa_len); + + return bind; +} + +/** + * ovpn_bind_reset - assign new binding to peer + * @peer: the peer whose binding has to be replaced + * @new: the new bind to assign + */ +void ovpn_bind_reset(struct ovpn_peer *peer, struct ovpn_bind *new) +{ + struct ovpn_bind *old; + + spin_lock_bh(&peer->lock); + old =3D rcu_replace_pointer(peer->bind, new, true); + spin_unlock_bh(&peer->lock); + + kfree_rcu(old, rcu); +} diff --git a/drivers/net/ovpn/bind.h b/drivers/net/ovpn/bind.h new file mode 100644 index 0000000000000000000000000000000000000000..859213d5040deb36c416eafcf5c= 6ab31c4d52c7a --- /dev/null +++ b/drivers/net/ovpn/bind.h @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2012-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#ifndef _NET_OVPN_OVPNBIND_H_ +#define _NET_OVPN_OVPNBIND_H_ + +#include +#include +#include +#include +#include +#include + +struct ovpn_peer; + +/** + * union ovpn_sockaddr - basic transport layer address + * @in4: IPv4 address + * @in6: IPv6 address + */ +union ovpn_sockaddr { + struct sockaddr_in in4; + struct sockaddr_in6 in6; +}; + +/** + * struct ovpn_bind - remote peer binding + * @remote: the remote peer sockaddress + * @local: local endpoint used to talk to the peer + * @local.ipv4: local IPv4 used to talk to the peer + * @local.ipv6: local IPv6 used to talk to the peer + * @rcu: used to schedule RCU cleanup job + */ +struct ovpn_bind { + union ovpn_sockaddr remote; /* remote sockaddr */ + + union { + struct in_addr ipv4; + struct in6_addr ipv6; + } local; + + struct rcu_head rcu; +}; + +/** + * skb_protocol_to_family - translate skb->protocol to AF_INET or AF_INET6 + * @skb: the packet sk_buff to inspect + * + * Return: AF_INET, AF_INET6 or 0 in case of unknown protocol + */ +static inline unsigned short skb_protocol_to_family(const struct sk_buff *= skb) +{ + switch (skb->protocol) { + case htons(ETH_P_IP): + return AF_INET; + case htons(ETH_P_IPV6): + return AF_INET6; + default: + return 0; + } +} + +/** + * ovpn_bind_skb_src_match - match packet source with binding + * @bind: the binding to match + * @skb: the packet to match + * + * Return: true if the packet source matches the remote peer sockaddr + * in the binding + */ +static inline bool ovpn_bind_skb_src_match(const struct ovpn_bind *bind, + const struct sk_buff *skb) +{ + const unsigned short family =3D skb_protocol_to_family(skb); + const union ovpn_sockaddr *remote; + + if (unlikely(!bind)) + return false; + + remote =3D &bind->remote; + + if (unlikely(remote->in4.sin_family !=3D family)) + return false; + + switch (family) { + case AF_INET: + if (unlikely(remote->in4.sin_addr.s_addr !=3D ip_hdr(skb)->saddr)) + return false; + + if (unlikely(remote->in4.sin_port !=3D udp_hdr(skb)->source)) + return false; + break; + case AF_INET6: + if (unlikely(!ipv6_addr_equal(&remote->in6.sin6_addr, + &ipv6_hdr(skb)->saddr))) + return false; + + if (unlikely(remote->in6.sin6_port !=3D udp_hdr(skb)->source)) + return false; + break; + default: + return false; + } + + return true; +} + +struct ovpn_bind *ovpn_bind_from_sockaddr(const struct sockaddr_storage *s= a); +void ovpn_bind_reset(struct ovpn_peer *peer, struct ovpn_bind *bind); + +#endif /* _NET_OVPN_OVPNBIND_H_ */ diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index eaa83a8662e4ac2c758201008268f9633643c0b6..5492ce07751d135c1484fe1ed82= 27c646df94969 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -20,6 +20,7 @@ #include "netlink.h" #include "io.h" #include "packet.h" +#include "peer.h" =20 /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)" @@ -29,6 +30,11 @@ static void ovpn_struct_free(struct net_device *net) { } =20 +static int ovpn_net_init(struct net_device *dev) +{ + return 0; +} + static int ovpn_net_open(struct net_device *dev) { /* ovpn keeps the carrier always on to avoid losing IP or route @@ -49,6 +55,7 @@ static int ovpn_net_stop(struct net_device *dev) } =20 static const struct net_device_ops ovpn_netdev_ops =3D { + .ndo_init =3D ovpn_net_init, .ndo_open =3D ovpn_net_open, .ndo_stop =3D ovpn_net_stop, .ndo_start_xmit =3D ovpn_net_xmit, @@ -128,6 +135,7 @@ static int ovpn_newlink(struct net *src_net, struct net= _device *dev, =20 ovpn->dev =3D dev; ovpn->mode =3D mode; + spin_lock_init(&ovpn->lock); =20 /* turn carrier explicitly off after registration, this way state is * clearly defined @@ -176,6 +184,9 @@ static int ovpn_netdev_notifier_call(struct notifier_bl= ock *nb, =20 netif_carrier_off(dev); ovpn->registered =3D false; + + if (ovpn->mode =3D=3D OVPN_MODE_P2P) + ovpn_peer_release_p2p(ovpn); break; case NETDEV_POST_INIT: case NETDEV_GOING_DOWN: diff --git a/drivers/net/ovpn/main.h b/drivers/net/ovpn/main.h index 0740a05070a817e0daea7b63a1f4fcebd274eb37..28e5c44816e110974333a7a6a9c= f18bd15ae84e6 100644 --- a/drivers/net/ovpn/main.h +++ b/drivers/net/ovpn/main.h @@ -19,4 +19,6 @@ bool ovpn_dev_is_valid(const struct net_device *dev); #define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4) #define OVPN_MAX_PADDING 16 =20 +#define OVPN_QUEUE_LEN 1024 + #endif /* _NET_OVPN_MAIN_H_ */ diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h index 211df871538d34fdff90d182f21a0b0fb11b28ad..a22c5083381c131db01a28c0f51= e661d690d4998 100644 --- a/drivers/net/ovpn/ovpnstruct.h +++ b/drivers/net/ovpn/ovpnstruct.h @@ -20,6 +20,8 @@ * @dev_tracker: reference tracker for associated dev * @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) + * @lock: protect this object + * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list */ struct ovpn_struct { @@ -27,6 +29,8 @@ struct ovpn_struct { netdevice_tracker dev_tracker; bool registered; enum ovpn_mode mode; + spinlock_t lock; /* protect writing to the ovpn_struct object */ + struct ovpn_peer __rcu *peer; struct list_head dev_list; }; =20 diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c new file mode 100644 index 0000000000000000000000000000000000000000..d9788a0cc99b5839c466c35d1b2= 266cc6b95fb72 --- /dev/null +++ b/drivers/net/ovpn/peer.c @@ -0,0 +1,354 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#include +#include + +#include "ovpnstruct.h" +#include "bind.h" +#include "io.h" +#include "main.h" +#include "netlink.h" +#include "peer.h" + +/** + * ovpn_peer_new - allocate and initialize a new peer object + * @ovpn: the openvpn instance inside which the peer should be created + * @id: the ID assigned to this peer + * + * Return: a pointer to the new peer on success or an error code otherwise + */ +struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) +{ + struct ovpn_peer *peer; + int ret; + + /* alloc and init peer object */ + peer =3D kzalloc(sizeof(*peer), GFP_KERNEL); + if (!peer) + return ERR_PTR(-ENOMEM); + + peer->id =3D id; + peer->halt =3D false; + peer->ovpn =3D ovpn; + + peer->vpn_addrs.ipv4.s_addr =3D htonl(INADDR_ANY); + peer->vpn_addrs.ipv6 =3D in6addr_any; + + RCU_INIT_POINTER(peer->bind, NULL); + spin_lock_init(&peer->lock); + kref_init(&peer->refcount); + + ret =3D dst_cache_init(&peer->dst_cache, GFP_KERNEL); + if (ret < 0) { + netdev_err(ovpn->dev, "%s: cannot initialize dst cache\n", + __func__); + kfree(peer); + return ERR_PTR(ret); + } + + netdev_hold(ovpn->dev, &ovpn->dev_tracker, GFP_KERNEL); + + return peer; +} + +/** + * ovpn_peer_release - release peer private members + * @peer: the peer to release + */ +static void ovpn_peer_release(struct ovpn_peer *peer) +{ + ovpn_bind_reset(peer, NULL); + + dst_cache_destroy(&peer->dst_cache); + netdev_put(peer->ovpn->dev, &peer->ovpn->dev_tracker); + kfree_rcu(peer, rcu); +} + +/** + * ovpn_peer_release_kref - callback for kref_put + * @kref: the kref object belonging to the peer + */ +void ovpn_peer_release_kref(struct kref *kref) +{ + struct ovpn_peer *peer =3D container_of(kref, struct ovpn_peer, refcount); + + ovpn_peer_release(peer); +} + +/** + * ovpn_peer_skb_to_sockaddr - fill sockaddr with skb source address + * @skb: the packet to extract data from + * @ss: the sockaddr to fill + * + * Return: true on success or false otherwise + */ +static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, + struct sockaddr_storage *ss) +{ + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + + ss->ss_family =3D skb_protocol_to_family(skb); + switch (ss->ss_family) { + case AF_INET: + sa4 =3D (struct sockaddr_in *)ss; + sa4->sin_family =3D AF_INET; + sa4->sin_addr.s_addr =3D ip_hdr(skb)->saddr; + sa4->sin_port =3D udp_hdr(skb)->source; + break; + case AF_INET6: + sa6 =3D (struct sockaddr_in6 *)ss; + sa6->sin6_family =3D AF_INET6; + sa6->sin6_addr =3D ipv6_hdr(skb)->saddr; + sa6->sin6_port =3D udp_hdr(skb)->source; + break; + default: + return false; + } + + return true; +} + +/** + * ovpn_peer_transp_match - check if sockaddr and peer binding match + * @peer: the peer to get the binding from + * @ss: the sockaddr to match + * + * Return: true if sockaddr and binding match or false otherwise + */ +static bool ovpn_peer_transp_match(const struct ovpn_peer *peer, + const struct sockaddr_storage *ss) +{ + struct ovpn_bind *bind =3D rcu_dereference(peer->bind); + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + + if (unlikely(!bind)) + return false; + + if (ss->ss_family !=3D bind->remote.in4.sin_family) + return false; + + switch (ss->ss_family) { + case AF_INET: + sa4 =3D (struct sockaddr_in *)ss; + if (sa4->sin_addr.s_addr !=3D bind->remote.in4.sin_addr.s_addr) + return false; + if (sa4->sin_port !=3D bind->remote.in4.sin_port) + return false; + break; + case AF_INET6: + sa6 =3D (struct sockaddr_in6 *)ss; + if (!ipv6_addr_equal(&sa6->sin6_addr, + &bind->remote.in6.sin6_addr)) + return false; + if (sa6->sin6_port !=3D bind->remote.in6.sin6_port) + return false; + break; + default: + return false; + } + + return true; +} + +/** + * ovpn_peer_get_by_transp_addr_p2p - get peer by transport address in a P= 2P + * instance + * @ovpn: the openvpn instance to search + * @ss: the transport socket address + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer * +ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn, + struct sockaddr_storage *ss) +{ + struct ovpn_peer *tmp, *peer =3D NULL; + + rcu_read_lock(); + tmp =3D rcu_dereference(ovpn->peer); + if (likely(tmp && ovpn_peer_transp_match(tmp, ss) && + ovpn_peer_hold(tmp))) + peer =3D tmp; + rcu_read_unlock(); + + return peer; +} + +/** + * ovpn_peer_get_by_transp_addr - retrieve peer by transport address + * @ovpn: the openvpn instance to search + * @skb: the skb to retrieve the source transport address from + * + * Return: a pointer to the peer if found or NULL otherwise + */ +struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn, + struct sk_buff *skb) +{ + struct ovpn_peer *peer =3D NULL; + struct sockaddr_storage ss =3D { 0 }; + + if (unlikely(!ovpn_peer_skb_to_sockaddr(skb, &ss))) + return NULL; + + if (ovpn->mode =3D=3D OVPN_MODE_P2P) + peer =3D ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); + + return peer; +} + +/** + * ovpn_peer_get_by_id_p2p - get peer by ID in a P2P instance + * @ovpn: the openvpn instance to search + * @peer_id: the ID of the peer to find + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn, + u32 peer_id) +{ + struct ovpn_peer *tmp, *peer =3D NULL; + + rcu_read_lock(); + tmp =3D rcu_dereference(ovpn->peer); + if (likely(tmp && tmp->id =3D=3D peer_id && ovpn_peer_hold(tmp))) + peer =3D tmp; + rcu_read_unlock(); + + return peer; +} + +/** + * ovpn_peer_get_by_id - retrieve peer by ID + * @ovpn: the openvpn instance to search + * @peer_id: the unique peer identifier to match + * + * Return: a pointer to the peer if found or NULL otherwise + */ +struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d) +{ + struct ovpn_peer *peer =3D NULL; + + if (ovpn->mode =3D=3D OVPN_MODE_P2P) + peer =3D ovpn_peer_get_by_id_p2p(ovpn, peer_id); + + return peer; +} + +/** + * ovpn_peer_add_p2p - add peer to related tables in a P2P instance + * @ovpn: the instance to add the peer to + * @peer: the peer to add + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_peer_add_p2p(struct ovpn_struct *ovpn, struct ovpn_peer *p= eer) +{ + struct ovpn_peer *tmp; + + spin_lock_bh(&ovpn->lock); + /* in p2p mode it is possible to have a single peer only, therefore the + * old one is released and substituted by the new one + */ + tmp =3D rcu_dereference_protected(ovpn->peer, + lockdep_is_held(&ovpn->lock)); + if (tmp) { + tmp->delete_reason =3D OVPN_DEL_PEER_REASON_TEARDOWN; + ovpn_peer_put(tmp); + } + + rcu_assign_pointer(ovpn->peer, peer); + spin_unlock_bh(&ovpn->lock); + + return 0; +} + +/** + * ovpn_peer_add - add peer to the related tables + * @ovpn: the openvpn instance the peer belongs to + * @peer: the peer object to add + * + * Assume refcounter was increased by caller + * + * Return: 0 on success or a negative error code otherwise + */ +int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovpn_peer *peer) +{ + switch (ovpn->mode) { + case OVPN_MODE_P2P: + return ovpn_peer_add_p2p(ovpn, peer); + default: + return -EOPNOTSUPP; + } +} + +/** + * ovpn_peer_del_p2p - delete peer from related tables in a P2P instance + * @peer: the peer to delete + * @reason: reason why the peer was deleted (sent to userspace) + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_peer_del_p2p(struct ovpn_peer *peer, + enum ovpn_del_peer_reason reason) + __must_hold(&peer->ovpn->lock) +{ + struct ovpn_peer *tmp; + + tmp =3D rcu_dereference_protected(peer->ovpn->peer, + lockdep_is_held(&peer->ovpn->lock)); + if (tmp !=3D peer) { + DEBUG_NET_WARN_ON_ONCE(1); + if (tmp) + ovpn_peer_put(tmp); + + return -ENOENT; + } + + tmp->delete_reason =3D reason; + RCU_INIT_POINTER(peer->ovpn->peer, NULL); + ovpn_peer_put(tmp); + + return 0; +} + +/** + * ovpn_peer_release_p2p - release peer upon P2P device teardown + * @ovpn: the instance being torn down + */ +void ovpn_peer_release_p2p(struct ovpn_struct *ovpn) +{ + struct ovpn_peer *tmp; + + spin_lock_bh(&ovpn->lock); + tmp =3D rcu_dereference_protected(ovpn->peer, + lockdep_is_held(&ovpn->lock)); + if (tmp) + ovpn_peer_del_p2p(tmp, OVPN_DEL_PEER_REASON_TEARDOWN); + spin_unlock_bh(&ovpn->lock); +} + +/** + * ovpn_peer_del - delete peer from related tables + * @peer: the peer object to delete + * @reason: reason for deleting peer (will be sent to userspace) + * + * Return: 0 on success or a negative error code otherwise + */ +int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason) +{ + switch (peer->ovpn->mode) { + case OVPN_MODE_P2P: + return ovpn_peer_del_p2p(peer, reason); + default: + return -EOPNOTSUPP; + } +} diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h new file mode 100644 index 0000000000000000000000000000000000000000..6e0c6b14559de886d0677117f5a= 7ae029214e1f8 --- /dev/null +++ b/drivers/net/ovpn/peer.h @@ -0,0 +1,79 @@ +/* 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_OVPNPEER_H_ +#define _NET_OVPN_OVPNPEER_H_ + +#include + +/** + * struct ovpn_peer - the main remote peer object + * @ovpn: main openvpn instance this peer belongs to + * @id: unique identifier + * @vpn_addrs: IP addresses assigned over the tunnel + * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel + * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel + * @dst_cache: cache for dst_entry used to send to peer + * @bind: remote peer binding + * @halt: true if ovpn_peer_mark_delete was called + * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) + * @lock: protects binding to peer (bind) + * @refcount: reference counter + * @rcu: used to free peer in an RCU safe way + * @delete_work: deferred cleanup work, used to notify userspace + */ +struct ovpn_peer { + struct ovpn_struct *ovpn; + u32 id; + struct { + struct in_addr ipv4; + struct in6_addr ipv6; + } vpn_addrs; + struct dst_cache dst_cache; + struct ovpn_bind __rcu *bind; + bool halt; + enum ovpn_del_peer_reason delete_reason; + spinlock_t lock; /* protects bind */ + struct kref refcount; + struct rcu_head rcu; + struct work_struct delete_work; +}; + +/** + * ovpn_peer_hold - increase reference counter + * @peer: the peer whose counter should be increased + * + * Return: true if the counter was increased or false if it was zero alrea= dy + */ +static inline bool ovpn_peer_hold(struct ovpn_peer *peer) +{ + return kref_get_unless_zero(&peer->refcount); +} + +void ovpn_peer_release_kref(struct kref *kref); + +/** + * ovpn_peer_put - decrease reference counter + * @peer: the peer whose counter should be decreased + */ +static inline void ovpn_peer_put(struct ovpn_peer *peer) +{ + kref_put(&peer->refcount, ovpn_peer_release_kref); +} + +struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id); +int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovpn_peer *peer); +int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason= ); +void ovpn_peer_release_p2p(struct ovpn_struct *ovpn); + +struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn, + struct sk_buff *skb); +struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d); + +#endif /* _NET_OVPN_OVPNPEER_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 63D01189F3F for ; Wed, 16 Oct 2024 01:03:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040639; cv=none; b=bImVzS1xteVagzYb56Yy10lt23sLcwQaeKItre8GuZigfxSiY/SsnmEk+jvoKJortBKVJiSwVJgTplHD6cwh1dFtbSn/WxzwcFRO4/E4dNytKLhQqgfkQm4Vjt1Y97DMXbTNSee6pNQS8q907XfK5UiBKZdrKuQ3oQLWmeOHg+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040639; c=relaxed/simple; bh=ggF5K+58gLgiad5OUSjEn5k2AotXeWRMNMLXDkZhNv0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ka8K3Ray0GqK7pjPKw0TX87ylyuqox7p7LB73a17IQ/KGKLAl4mjv5BX3fsEnYqRIXcwPDVdchqLNSQs7VatDyv2EyuFxam/dqxIY40mV6on6ypgagqzOQJaFS0y975oG1kNPLxHVWh396JRwy+F9kdhffsxxMXm9mzY5HWr8Ko= 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=EjI4Ra7+; arc=none smtp.client-ip=209.85.128.50 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="EjI4Ra7+" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-43124843b04so34747345e9.2 for ; Tue, 15 Oct 2024 18:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040635; x=1729645435; 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=ud6K5GOoq3b4lX4KydaG6wKHFrptq5dDMvYiS7l/dAc=; b=EjI4Ra7+mUtw6mx+ECVTzl22I6MuAJaq+ZpkMdqFRJ0ojMmlcwX1XcWgWsCaoSIb0w HQv3CXvVy8fx4vR1aKBEZgIWbHV/Z1XfcbcAUaEp8TX/5Z32SoIusND0lqsX9bv47+hS xwGsS7Yzp7aZjZwt8oOvNav72HsUofEKG8cnNccav/X2qXfnmVMDoRP7mylROUEaeX6e qERjMz7cmZfvhY7RdgPz3+uJd2aSK1FquDn3h1EOEpgApCYSwWZJk0GqcfMdd1lVEpb0 idfybnmC1bHoZwhORzv2+K5mY22PtQLpl2Aok7TldZ9+Uh7kNWzz+yDV8KFn5gKKNujz iX0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040635; x=1729645435; 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=ud6K5GOoq3b4lX4KydaG6wKHFrptq5dDMvYiS7l/dAc=; b=isUx7kAV+fQr/zt1g1100SFg+Kvg3rZ3mfebem9p5XHSwcJSGgwT3/gP7LarWWwDql tX+YQHBiC4cPpTaP9yqgs0mmtAKpHY14gKOTIh4TPGXUdANJFY1vjk73hajSOcQr3Dfp XZpyUXinSuK7NYNz9QWH869C4ALfqkBjt2xqz/LHft5i7YLeZlxJz+ECfuwjYcDeg2yJ TMwxT3xCnVUZtUW9bdrJTlYuS5wtk4vRXydgl8LX5nTqXV3ioNMxHVzCYH7HSYdud0fr cIDNAwItrqBQhq9hpUnplUjCL9l6E6TZuBCJrhRg8og3Umny1uPQdvaFIj2/+1Wrd8nU YhLQ== X-Forwarded-Encrypted: i=1; AJvYcCU13qz04uMrecXkgMnX5V6Dz1d6MQ1kKPMJwWtJlX0SD6M8Ig/2+SDZpcqQ8BYmsfdrL+Z6iaFv1wUrcPs=@vger.kernel.org X-Gm-Message-State: AOJu0YzH0ZDGsWmGtGacAH6/7l+7j/6lDVpZpEvCsoj3PDm3OuLw5qAx kRxjl+/b9aOiT9vmBdEHnvCnvILx3i2nB257fJcVw3xqgJ5xPEYK/I5H3ZeLWwo= X-Google-Smtp-Source: AGHT+IElhVPemSMkaKzDVK6PyvleajJ2ybaHbRRGgXby+pI4RQz6g2YdJdfaif0XRYc8ChjoZir7gA== X-Received: by 2002:a05:600c:2314:b0:431:5044:e388 with SMTP id 5b1f17b1804b1-4315044e674mr5340115e9.22.1729040634638; Tue, 15 Oct 2024 18:03:54 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:53 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:07 +0200 Subject: [PATCH net-next v9 07/23] 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: <20241016-b4-ovpn-v9-7-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9019; i=antonio@openvpn.net; h=from:subject:message-id; bh=ggF5K+58gLgiad5OUSjEn5k2AotXeWRMNMLXDkZhNv0=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvkB0iccAPf51dXcjIuE5PlChNgocu3n670 bhsJqkOBqmJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV h7IRCACuclE/Hw6nucIkFqbDD4j1FJyNvYHFTA8QEma+CjSf1LvLgRA0+wDQDcvD7CzGs0/kBVL +p7qFfU1Z3hRD5RaUIc58ByahD4fs14gKTzFb+geIoyr8QtLt61RYdG3/rP6FNDAiMD+t6L5XNC CWkqfD8uX6pzQiqnjSek7qBy5fauBVI8QCb0pExKkHnYgk2V41VOxHKeKSSftmOY80rJl0IlK1B 2couLFlVAk+Ml9E0J4NYrmSTLeMy5rU3GNMy890nSkbaAXtTGYLeAqUAB1w3z5Lm4QZoUio2Fx7 sjiyOZ5F94kK3r1HdQYjSLNMI904L1iuXfEf4eU+bmn0o6/l 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. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/socket.c | 120 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/socket.h | 48 +++++++++++++++++++ drivers/net/ovpn/udp.c | 72 ++++++++++++++++++++++++++++ drivers/net/ovpn/udp.h | 17 +++++++ 5 files changed, 259 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..090a3232ab0ec19702110f1a90f= 45c7f10889f6f --- /dev/null +++ b/drivers/net/ovpn/socket.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#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 (!ovpn_socket_hold(ovpn_sock)) { + pr_warn("%s: found ovpn_socket with ref =3D 0\n", __func__); + 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..5ad9c5073b085482da95ee8ebf4= 0acf20bf2e4b3 --- /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_struct; +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_struct *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..c10474d252e19a0626d17a6f5dd= 328a5e5811551 --- /dev/null +++ b/drivers/net/ovpn/udp.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#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_struct *ovpn) +{ + struct ovpn_socket *old_data; + int ret =3D 0; + + /* sanity check */ + if (sock->sk->sk_protocol !=3D IPPROTO_UDP) { + DEBUG_NET_WARN_ON_ONCE(1); + return -EINVAL; + } + + /* 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, + "%s: provided socket already owned by this interface\n", + __func__); + ret =3D -EALREADY; + } else { + netdev_err(ovpn->dev, + "%s: provided socket already taken by other user\n", + __func__); + 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..f2507f8f2c71ea9d5e5ac544680= 1e2d56f86700f --- /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_struct; +struct socket; + +int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn); + +#endif /* _NET_OVPN_UDP_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 7E55518B462 for ; Wed, 16 Oct 2024 01:03:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040641; cv=none; b=V9RdKzcS55Yxx9IO778vWr6a/+jENCCsvtnn1l1Mr8Vdpr5ZRrkqhPVnJDav6pJZwM2sq3dWYXpH2IZC1PomJVHWnkSEK2llONUkItzZ/kETYEzvmXjnBUIgwsBgfJ2+f9RBZ279cg20kexl6k5uiBO1ehtz3oJ6ky3V0midkgk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040641; c=relaxed/simple; bh=YMuwyTMzclpNnNaR/WkRJmXnCptZu+Rq2A1hloxGEmY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GQR9IB74FwvcMpNizFSOw72um6S3xIq6ocLm5E2Nk4IQIgG+g/fjJlNgBqqvOOIlSm9/LPJubc4TR2YWv3enmdIWXikVHZhjgMp395vjOcsVsIalbSOF1kMObeCPicptk0nqrjm+7/WKJ7zodriA2ivWCHj10f2VVluZd6EDf+c= 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=N+3nUttc; arc=none smtp.client-ip=209.85.221.51 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="N+3nUttc" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-37d41894a32so242931f8f.1 for ; Tue, 15 Oct 2024 18:03:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040637; x=1729645437; 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=rEkhq4g07rH6eS4QvsCkWBw8nlVl5m3JneNiJjlmb24=; b=N+3nUttc3MDhAbRXHLAPBmrlcQ3gpDFoVrgVIpclNjKVqJ3CTQ6/q4MmB+q4m2udrv ePNlCCu5z478ntqCIGTOhkB7wl1OU1FPyTm6OIqolHF8CE9xd6ztXViZb/7q+p6zzD2Q fZBeu9y9SgTt2B0ugNq25oFQ1ainbWrQLgxwvZoMRpa2GLWSPZW3tu6xAXNYI7myzFTK BGzHlYn28tEV/JDr5NsAj+MflO7rwrUPoTl1sB95LVXtaoKt8SWHDkQz6IJFBfXyh0R4 5csK/D1hPgksjYXflgVvYeXFYSr7wbK20VkrXyKp/DnWiWLgCDTMldQyG1BVIMD3Yi9x FxLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040637; x=1729645437; 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=rEkhq4g07rH6eS4QvsCkWBw8nlVl5m3JneNiJjlmb24=; b=cWmamaMVGQzAL5YGsF1e0mauOG+6S/su2hw1RghuefeDKSDgOcqOMuhzzRWnOwvpuk lRZInCGKWWMGPJqe2Xvz6MXL84uzwIeuhbXOqgXADXK+xpvti/f3kVvIqCoeLRq6LGhe NEc/2OOS3HJkQk6Kt+yBwpQjDBVE/GXxvTSu/n9TVXc66Q6bbuLfw/9Ame/2NVaB70xr LzhQB0QGxVLMXXua+JrpdMoVqDcD/zIT1AZ2979HQf21cI8k8QlIL4+hyuWljNgEv3rB C578Lz/PoxyIPMkCtd0t6aKAKAZ8Cy4DgPjc3KmnlKMZNdMHSygM0NyMjGFHljAEdhYi qauA== X-Forwarded-Encrypted: i=1; AJvYcCWAH8Ijcv5xRyYs/wEXguGyrnmYYkUaOVvxshdi1vp5h29hjuuVEbt9q0kzxnN+ltuRCn6JuARaJDj3VOk=@vger.kernel.org X-Gm-Message-State: AOJu0YywEAj+lZSfq0OpXtBOhsj5cSnYRYHICDiXXClVjouwmFjL/07K aEa4JimGTpBohI+XtxvTCNr8LtSbBw9qW5371vVvCD79gYYdC2FPAw3vq9Pi9AE= X-Google-Smtp-Source: AGHT+IHxxd3uRD1CMOwjI1wFdLJBPOmy367kPhdMjELOXvHRZmhhQy5Z39M+WfVTnmxeHVWFtY0MQQ== X-Received: by 2002:a5d:4ec8:0:b0:37d:325e:dd59 with SMTP id ffacd0b85a97d-37d5529d987mr11814279f8f.26.1729040636794; Tue, 15 Oct 2024 18:03:56 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:56 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:08 +0200 Subject: [PATCH net-next v9 08/23] ovpn: implement basic TX path (UDP) 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: <20241016-b4-ovpn-v9-8-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=16921; i=antonio@openvpn.net; h=from:subject:message-id; bh=YMuwyTMzclpNnNaR/WkRJmXnCptZu+Rq2A1hloxGEmY=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDve/y0jL3koGQbg4pJ5HRav7bl0/9DdhDuO xKEt9UUfiaJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV h/xECACsMxHihAVKWE2/92IKrn9axr7+vvONH/mJ3OgPzmskAhUYhq3u934EOTDo3ngj1PL+QrS FmWQCJJxQCMxz39NuV5OUcCbB3z3FbnWEeDck5991fcZuspyboOkqdCyT2XYH8gzi+AReh3pRnt P4BhynVSmyukCQQEOYgtgQ21k/8zzqYITWFQmmJCdrh0+0r/MN+RvaJ9Q3l6YdwnhwvD3FqngGB 74KZsvMSjvx1+7kSFwDpkeT8+SW+36nLGLm0E8ND/8XAiIyDIaSuJ5kbKN045cZVZG09B17gG+d QWpgkp+2Ww7ZmHQ2xSwcKrDj3oY+42N9CLjE+8BNTCqfCtYA X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Packets sent over the ovpn interface are processed and transmitted to the connected peer, if any. Implementation is UDP only. TCP will be added by a later patch. Note: no crypto/encapsulation exists yet. packets are just captured and sent. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/io.c | 138 +++++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 37 +++++++- drivers/net/ovpn/peer.h | 4 + drivers/net/ovpn/skb.h | 51 +++++++++++ drivers/net/ovpn/udp.c | 232 ++++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/udp.h | 8 ++ 6 files changed, 468 insertions(+), 2 deletions(-) diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index ad3813419c33cbdfe7e8ad6f5c8b444a3540a69f..77ba4d33ae0bd2f52e8bd1c06a1= 82d24285297b4 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -9,14 +9,150 @@ =20 #include #include +#include =20 #include "io.h" +#include "ovpnstruct.h" +#include "peer.h" +#include "udp.h" +#include "skb.h" +#include "socket.h" + +static void ovpn_encrypt_post(struct sk_buff *skb, int ret) +{ + struct ovpn_peer *peer =3D ovpn_skb_cb(skb)->peer; + + if (unlikely(ret < 0)) + goto err; + + skb_mark_not_on_list(skb); + + switch (peer->sock->sock->sk->sk_protocol) { + case IPPROTO_UDP: + ovpn_udp_send_skb(peer->ovpn, peer, skb); + break; + default: + /* no transport configured yet */ + goto err; + } + /* skb passed down the stack - don't free it */ + skb =3D NULL; +err: + if (unlikely(skb)) + dev_core_stats_tx_dropped_inc(peer->ovpn->dev); + ovpn_peer_put(peer); + kfree_skb(skb); +} + +static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) +{ + ovpn_skb_cb(skb)->peer =3D peer; + + /* take a reference to the peer because the crypto code may run async. + * ovpn_encrypt_post() will release it upon completion + */ + if (unlikely(!ovpn_peer_hold(peer))) { + DEBUG_NET_WARN_ON_ONCE(1); + return false; + } + + ovpn_encrypt_post(skb, 0); + return true; +} + +/* send skb to connected peer, if any */ +static void ovpn_send(struct ovpn_struct *ovpn, struct sk_buff *skb, + struct ovpn_peer *peer) +{ + struct sk_buff *curr, *next; + + if (likely(!peer)) + /* retrieve peer serving the destination IP of this packet */ + peer =3D ovpn_peer_get_by_dst(ovpn, skb); + if (unlikely(!peer)) { + net_dbg_ratelimited("%s: no peer to send data to\n", + ovpn->dev->name); + dev_core_stats_tx_dropped_inc(ovpn->dev); + goto drop; + } + + /* this might be a GSO-segmented skb list: process each skb + * independently + */ + skb_list_walk_safe(skb, curr, next) + if (unlikely(!ovpn_encrypt_one(peer, curr))) { + dev_core_stats_tx_dropped_inc(ovpn->dev); + kfree_skb(curr); + } + + /* skb passed over, no need to free */ + skb =3D NULL; +drop: + if (likely(peer)) + ovpn_peer_put(peer); + kfree_skb_list(skb); +} =20 /* Send user data to the network */ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) { + struct ovpn_struct *ovpn =3D netdev_priv(dev); + struct sk_buff *segments, *curr, *next; + struct sk_buff_head skb_list; + __be16 proto; + int ret; + + /* reset netfilter state */ + nf_reset_ct(skb); + + /* verify IP header size in network packet */ + proto =3D ovpn_ip_check_protocol(skb); + if (unlikely(!proto || skb->protocol !=3D proto)) { + net_err_ratelimited("%s: dropping malformed payload packet\n", + dev->name); + dev_core_stats_tx_dropped_inc(ovpn->dev); + goto drop; + } + + if (skb_is_gso(skb)) { + segments =3D skb_gso_segment(skb, 0); + if (IS_ERR(segments)) { + ret =3D PTR_ERR(segments); + net_err_ratelimited("%s: cannot segment packet: %d\n", + dev->name, ret); + dev_core_stats_tx_dropped_inc(ovpn->dev); + goto drop; + } + + consume_skb(skb); + skb =3D segments; + } + + /* from this moment on, "skb" might be a list */ + + __skb_queue_head_init(&skb_list); + skb_list_walk_safe(skb, curr, next) { + skb_mark_not_on_list(curr); + + curr =3D skb_share_check(curr, GFP_ATOMIC); + if (unlikely(!curr)) { + net_err_ratelimited("%s: skb_share_check failed\n", + dev->name); + dev_core_stats_tx_dropped_inc(ovpn->dev); + continue; + } + + __skb_queue_tail(&skb_list, curr); + } + skb_list.prev->next =3D NULL; + + ovpn_send(ovpn, skb_list.next, NULL); + + return NETDEV_TX_OK; + +drop: skb_tx_error(skb); - kfree_skb(skb); + kfree_skb_list(skb); return NET_XMIT_DROP; } diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index d9788a0cc99b5839c466c35d1b2266cc6b95fb72..aff3e9e99b7d2dd2fa68484d9a3= 96d43f75a6d0b 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -16,6 +16,7 @@ #include "main.h" #include "netlink.h" #include "peer.h" +#include "socket.h" =20 /** * ovpn_peer_new - allocate and initialize a new peer object @@ -64,8 +65,10 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn= , u32 id) */ static void ovpn_peer_release(struct ovpn_peer *peer) { - ovpn_bind_reset(peer, NULL); + if (peer->sock) + ovpn_socket_put(peer->sock); =20 + ovpn_bind_reset(peer, NULL); dst_cache_destroy(&peer->dst_cache); netdev_put(peer->ovpn->dev, &peer->ovpn->dev_tracker); kfree_rcu(peer, rcu); @@ -243,6 +246,38 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_stru= ct *ovpn, u32 peer_id) return peer; } =20 +/** + * ovpn_peer_get_by_dst - Lookup peer to send skb to + * @ovpn: the private data representing the current VPN session + * @skb: the skb to extract the destination address from + * + * This function takes a tunnel packet and looks up the peer to send it to + * after encapsulation. The skb is expected to be the in-tunnel packet, wi= thout + * any OpenVPN related header. + * + * Assume that the IP header is accessible in the skb data. + * + * Return: the peer if found or NULL otherwise. + */ +struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, + struct sk_buff *skb) +{ + struct ovpn_peer *peer =3D NULL; + + /* in P2P mode, no matter the destination, packets are always sent to + * the single peer listening on the other side + */ + if (ovpn->mode =3D=3D OVPN_MODE_P2P) { + rcu_read_lock(); + peer =3D rcu_dereference(ovpn->peer); + if (unlikely(peer && !ovpn_peer_hold(peer))) + peer =3D NULL; + rcu_read_unlock(); + } + + return peer; +} + /** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 6e0c6b14559de886d0677117f5a7ae029214e1f8..51955aa39f1aa85ce541e289c60= e9635cadb9c48 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -19,6 +19,7 @@ * @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel + * @sock: the socket being used to talk to this peer * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called @@ -35,6 +36,7 @@ struct ovpn_peer { struct in_addr ipv4; struct in6_addr ipv6; } vpn_addrs; + struct ovpn_socket *sock; struct dst_cache dst_cache; struct ovpn_bind __rcu *bind; bool halt; @@ -75,5 +77,7 @@ void ovpn_peer_release_p2p(struct ovpn_struct *ovpn); struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb); struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d); +struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, + struct sk_buff *skb); =20 #endif /* _NET_OVPN_OVPNPEER_H_ */ diff --git a/drivers/net/ovpn/skb.h b/drivers/net/ovpn/skb.h new file mode 100644 index 0000000000000000000000000000000000000000..e070fe6f448c0b7a9631394ebef= 4554f6348ef44 --- /dev/null +++ b/drivers/net/ovpn/skb.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#ifndef _NET_OVPN_SKB_H_ +#define _NET_OVPN_SKB_H_ + +#include +#include +#include +#include +#include +#include + +struct ovpn_cb { + struct ovpn_peer *peer; +}; + +static inline struct ovpn_cb *ovpn_skb_cb(struct sk_buff *skb) +{ + BUILD_BUG_ON(sizeof(struct ovpn_cb) > sizeof(skb->cb)); + return (struct ovpn_cb *)skb->cb; +} + +/* Return IP protocol version from skb header. + * Return 0 if protocol is not IPv4/IPv6 or cannot be read. + */ +static inline __be16 ovpn_ip_check_protocol(struct sk_buff *skb) +{ + __be16 proto =3D 0; + + /* skb could be non-linear, + * make sure IP header is in non-fragmented part + */ + if (!pskb_network_may_pull(skb, sizeof(struct iphdr))) + return 0; + + if (ip_hdr(skb)->version =3D=3D 4) + proto =3D htons(ETH_P_IP); + else if (ip_hdr(skb)->version =3D=3D 6) + proto =3D htons(ETH_P_IPV6); + + return proto; +} + +#endif /* _NET_OVPN_SKB_H_ */ diff --git a/drivers/net/ovpn/udp.c b/drivers/net/ovpn/udp.c index c10474d252e19a0626d17a6f5dd328a5e5811551..d26d7566e9c8dfe91fa77f49c34= fb179a9fb2239 100644 --- a/drivers/net/ovpn/udp.c +++ b/drivers/net/ovpn/udp.c @@ -7,14 +7,246 @@ */ =20 #include +#include #include +#include +#include +#include +#include #include +#include =20 #include "ovpnstruct.h" #include "main.h" +#include "bind.h" +#include "io.h" +#include "peer.h" #include "socket.h" #include "udp.h" =20 +/** + * ovpn_udp4_output - send IPv4 packet over udp socket + * @ovpn: the openvpn instance + * @bind: the binding related to the destination peer + * @cache: dst cache + * @sk: the socket to send the packet over + * @skb: the packet to send + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_udp4_output(struct ovpn_struct *ovpn, struct ovpn_bind *bi= nd, + struct dst_cache *cache, struct sock *sk, + struct sk_buff *skb) +{ + struct rtable *rt; + struct flowi4 fl =3D { + .saddr =3D bind->local.ipv4.s_addr, + .daddr =3D bind->remote.in4.sin_addr.s_addr, + .fl4_sport =3D inet_sk(sk)->inet_sport, + .fl4_dport =3D bind->remote.in4.sin_port, + .flowi4_proto =3D sk->sk_protocol, + .flowi4_mark =3D sk->sk_mark, + }; + int ret; + + local_bh_disable(); + rt =3D dst_cache_get_ip4(cache, &fl.saddr); + if (rt) + goto transmit; + + if (unlikely(!inet_confirm_addr(sock_net(sk), NULL, 0, fl.saddr, + RT_SCOPE_HOST))) { + /* we may end up here when the cached address is not usable + * anymore. In this case we reset address/cache and perform a + * new look up + */ + fl.saddr =3D 0; + bind->local.ipv4.s_addr =3D 0; + dst_cache_reset(cache); + } + + rt =3D ip_route_output_flow(sock_net(sk), &fl, sk); + if (IS_ERR(rt) && PTR_ERR(rt) =3D=3D -EINVAL) { + fl.saddr =3D 0; + bind->local.ipv4.s_addr =3D 0; + dst_cache_reset(cache); + + rt =3D ip_route_output_flow(sock_net(sk), &fl, sk); + } + + if (IS_ERR(rt)) { + ret =3D PTR_ERR(rt); + net_dbg_ratelimited("%s: no route to host %pISpc: %d\n", + ovpn->dev->name, &bind->remote.in4, ret); + goto err; + } + dst_cache_set_ip4(cache, &rt->dst, fl.saddr); + +transmit: + udp_tunnel_xmit_skb(rt, sk, skb, fl.saddr, fl.daddr, 0, + ip4_dst_hoplimit(&rt->dst), 0, fl.fl4_sport, + fl.fl4_dport, false, sk->sk_no_check_tx); + ret =3D 0; +err: + local_bh_enable(); + return ret; +} + +#if IS_ENABLED(CONFIG_IPV6) +/** + * ovpn_udp6_output - send IPv6 packet over udp socket + * @ovpn: the openvpn instance + * @bind: the binding related to the destination peer + * @cache: dst cache + * @sk: the socket to send the packet over + * @skb: the packet to send + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_udp6_output(struct ovpn_struct *ovpn, struct ovpn_bind *bi= nd, + struct dst_cache *cache, struct sock *sk, + struct sk_buff *skb) +{ + struct dst_entry *dst; + int ret; + + struct flowi6 fl =3D { + .saddr =3D bind->local.ipv6, + .daddr =3D bind->remote.in6.sin6_addr, + .fl6_sport =3D inet_sk(sk)->inet_sport, + .fl6_dport =3D bind->remote.in6.sin6_port, + .flowi6_proto =3D sk->sk_protocol, + .flowi6_mark =3D sk->sk_mark, + .flowi6_oif =3D bind->remote.in6.sin6_scope_id, + }; + + local_bh_disable(); + dst =3D dst_cache_get_ip6(cache, &fl.saddr); + if (dst) + goto transmit; + + if (unlikely(!ipv6_chk_addr(sock_net(sk), &fl.saddr, NULL, 0))) { + /* we may end up here when the cached address is not usable + * anymore. In this case we reset address/cache and perform a + * new look up + */ + fl.saddr =3D in6addr_any; + bind->local.ipv6 =3D in6addr_any; + dst_cache_reset(cache); + } + + dst =3D ipv6_stub->ipv6_dst_lookup_flow(sock_net(sk), sk, &fl, NULL); + if (IS_ERR(dst)) { + ret =3D PTR_ERR(dst); + net_dbg_ratelimited("%s: no route to host %pISpc: %d\n", + ovpn->dev->name, &bind->remote.in6, ret); + goto err; + } + dst_cache_set_ip6(cache, dst, &fl.saddr); + +transmit: + udp_tunnel6_xmit_skb(dst, sk, skb, skb->dev, &fl.saddr, &fl.daddr, 0, + ip6_dst_hoplimit(dst), 0, fl.fl6_sport, + fl.fl6_dport, udp_get_no_check6_tx(sk)); + ret =3D 0; +err: + local_bh_enable(); + return ret; +} +#endif + +/** + * ovpn_udp_output - transmit skb using udp-tunnel + * @ovpn: the openvpn instance + * @bind: the binding related to the destination peer + * @cache: dst cache + * @sk: the socket to send the packet over + * @skb: the packet to send + * + * rcu_read_lock should be held on entry. + * On return, the skb is consumed. + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_udp_output(struct ovpn_struct *ovpn, struct ovpn_bind *bin= d, + struct dst_cache *cache, struct sock *sk, + struct sk_buff *skb) +{ + int ret; + + /* set sk to null if skb is already orphaned */ + if (!skb->destructor) + skb->sk =3D NULL; + + /* always permit openvpn-created packets to be (outside) fragmented */ + skb->ignore_df =3D 1; + + switch (bind->remote.in4.sin_family) { + case AF_INET: + ret =3D ovpn_udp4_output(ovpn, bind, cache, sk, skb); + break; +#if IS_ENABLED(CONFIG_IPV6) + case AF_INET6: + ret =3D ovpn_udp6_output(ovpn, bind, cache, sk, skb); + break; +#endif + default: + ret =3D -EAFNOSUPPORT; + break; + } + + return ret; +} + +/** + * ovpn_udp_send_skb - prepare skb and send it over via UDP + * @ovpn: the openvpn instance + * @peer: the destination peer + * @skb: the packet to send + */ +void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer, + struct sk_buff *skb) +{ + struct ovpn_bind *bind; + unsigned int pkt_len; + struct socket *sock; + int ret =3D -1; + + skb->dev =3D ovpn->dev; + /* no checksum performed at this layer */ + skb->ip_summed =3D CHECKSUM_NONE; + + /* get socket info */ + sock =3D peer->sock->sock; + if (unlikely(!sock)) { + net_warn_ratelimited("%s: no sock for remote peer\n", __func__); + goto out; + } + + rcu_read_lock(); + /* get binding */ + bind =3D rcu_dereference(peer->bind); + if (unlikely(!bind)) { + net_warn_ratelimited("%s: no bind for remote peer\n", __func__); + goto out_unlock; + } + + /* crypto layer -> transport (UDP) */ + pkt_len =3D skb->len; + ret =3D ovpn_udp_output(ovpn, bind, &peer->dst_cache, sock->sk, skb); + +out_unlock: + rcu_read_unlock(); +out: + if (unlikely(ret < 0)) { + dev_core_stats_tx_dropped_inc(ovpn->dev); + kfree_skb(skb); + return; + } + + dev_sw_netstats_tx_add(ovpn->dev, 1, pkt_len); +} + /** * ovpn_udp_socket_attach - set udp-tunnel CBs on socket and link it to ov= pn * @sock: socket to configure diff --git a/drivers/net/ovpn/udp.h b/drivers/net/ovpn/udp.h index f2507f8f2c71ea9d5e5ac5446801e2d56f86700f..e60f8cd2b4ac8f910aabcf8ed54= 6af59d6ca4be4 100644 --- a/drivers/net/ovpn/udp.h +++ b/drivers/net/ovpn/udp.h @@ -9,9 +9,17 @@ #ifndef _NET_OVPN_UDP_H_ #define _NET_OVPN_UDP_H_ =20 +#include +#include + +struct ovpn_peer; struct ovpn_struct; +struct sk_buff; struct socket; =20 int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn); =20 +void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer, + struct sk_buff *skb); + #endif /* _NET_OVPN_UDP_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 25C3613E40F for ; Wed, 16 Oct 2024 01:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040643; cv=none; b=VmyAy+IOTQ+vf8fMp15ifHLSL8+WVsPJc+YOawWZY07ALgkcLvT92JmCVZLwrlyLl0DVYVRhUqEhNBP1/l0/+Fq1GPEKr6s89m5SRuqYWQUgbmfHTIaQu6IMKtHtJYcLr9s3OFk0/fvMW7SpD3hC7hUj6rkANQMxHnGYay9cqmw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040643; c=relaxed/simple; bh=+ny/7Z1DJ7DqzyniBgVi2YmSRZmW82vQ4lKLHp8YpkU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nrwG4hGwNa2Mqtccf2hFVWZKrGF3pUyCM+IjzPlNYbGPugQUrDuf8cPqvAnEnWlTN/ZeFAXjUv8AzXfA7paEIVMtd+6c23C2UdAEbdttsC33At+BGJxSrmRgH5WGB+egVIobkrlqXsWY+/cjNu+vawbX88SsoX+z0YDullB2o10= 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=caAmBC98; arc=none smtp.client-ip=209.85.128.54 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="caAmBC98" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4314c452180so2366035e9.0 for ; Tue, 15 Oct 2024 18:04:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040639; x=1729645439; 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=7Sqn5jbXzCasokH7RmP7q0PQ4Qz9mfONmQaHn1pOoZY=; b=caAmBC98JiLx4xmq23u6LARZ2aUYHGYph7g4VWMBk+I4FM46ElaIRHkXKXGlNjUcKe GkyF1lGHLfVNiMJXcEkuxhjVHNfPMjTAfNzdllSkBdNXpmxB6aKYf+XO8EqWNuYEJw+c K2aPqPM9SVOzAwt7eDhU1AoNwvT54FFK+OMv2r9PRKJ8/PWB4hCZAaARVHhX8fSD9o6q ozxWDu7KhRitBAjT/sMaVxE/OEpvm9TKacsL/fWMYxzB0+I+QweJLVZbjuZnqxo3ljhS r7Kh2DJP+SNKqHC7s/7Yjcly9J9Nn95plVyE+yyNQsow0EBSM1h7dEvG6J7INpO4n8L5 TF1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040639; x=1729645439; 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=7Sqn5jbXzCasokH7RmP7q0PQ4Qz9mfONmQaHn1pOoZY=; b=p2B0tssGWEGwVxmK84NMDO/ZDdU6ivlw08doIiAXLoJBxlXcWwKGsZ0Pd69HXc5/jX dg7RC2k+qr5vhpNSKUvsHHHZk5cwFTPx+quzHOM9cPtDq3hSVgFl1iFddJrpcyue7aMH 3e4FRc3nOTtaeseeknWp2X38xLToBkBUXdyTCyTJ0DIiVMz4qatzg3jZmyjhnIfMOvfh vr2wjrOaQI2KHc5pfYC4aONgvEXxXXNP0KVLehSzQhzD5m1kLRQmJDQauQKo2Wk5Xgwc iDJwjdRZVEr246DppMCTTebMzHLi1SgCowkAOiUuLm6b1KbEN5WqXOJlxUNy8zTRfiCg mspA== X-Forwarded-Encrypted: i=1; AJvYcCWgqohGus24MtXyRaawL7e+WQ9VQTCL+XyPpuyMxjsBT/ltLVz9vNQaEsMyBkRptTtIjDrG57nLgw1B1V8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywe55Tjf3qrqSS2XHnZKg0qBQ5NsbKN5RYh0fYB3rJ4DZRAxfoF CzXHGSp7KdMy6Sql+hRt3Qkr+w8ok41vDt0LBR/JuZSnwFYOU+nkyzTq+rM9Fb8= X-Google-Smtp-Source: AGHT+IFOAZAtRJgq5QowTQIFhBL5hOx/7zL+sjyxbGBT/odBZHPTdTBOnq74FHtlfyi4TPVYOD2zdQ== X-Received: by 2002:a05:600c:4e44:b0:431:5226:1633 with SMTP id 5b1f17b1804b1-43152261701mr250335e9.6.1729040639476; Tue, 15 Oct 2024 18:03:59 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:03:58 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:09 +0200 Subject: [PATCH net-next v9 09/23] ovpn: implement basic RX path (UDP) 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: <20241016-b4-ovpn-v9-9-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=14461; i=antonio@openvpn.net; h=from:subject:message-id; bh=+ny/7Z1DJ7DqzyniBgVi2YmSRZmW82vQ4lKLHp8YpkU=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvP/HsZ4NgkMVgt3SW1aqdShzNEtH9QMFFI 12DZfjx+TKJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV h7YAB/4qtPKNwSx/VvO2PlisAFgbgaacyrOxkkQbgjIr5HFBoD9d4wfVgCC/fXuviongvWeV5H6 7sqRcQxD5sJs4lLcv5ULmeqTpn/hgyiUMWD8XpWW+oRe4p5/qWHAK2nFDOVCJ+cp1CCDvbEPAwV KN4x2p7Bnr8RFYz49cwBGtSnZFzxCfmMnAkvvlWCf08IWimY1VB7TM9Kkh+opSqhn99/5axQeCs aLmLOCidw6mjgIkeP1oY5eUeuifdGUNUnaDGWiTtsj7g+cd+xpnkYgxm1Qh2P6AwOFzRu4951OD wE4yeQPtWy+15R7yuvC5ecmFobla5pmQ0ThXxqYIYUfpdDzm X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Packets received over the socket are forwarded to the user device. Implementation is UDP only. TCP will be added by a later patch. Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/io.c | 66 ++++++++++++++++++++++++++- drivers/net/ovpn/io.h | 2 + drivers/net/ovpn/main.c | 13 +++++- drivers/net/ovpn/ovpnstruct.h | 3 ++ drivers/net/ovpn/proto.h | 75 ++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.c | 24 ++++++++++ drivers/net/ovpn/udp.c | 104 ++++++++++++++++++++++++++++++++++++++= +++- drivers/net/ovpn/udp.h | 3 +- 8 files changed, 286 insertions(+), 4 deletions(-) diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index 77ba4d33ae0bd2f52e8bd1c06a182d24285297b4..791a1b117125118b179cb13cdfd= 5fbab6523a360 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -9,15 +9,79 @@ =20 #include #include +#include #include =20 -#include "io.h" #include "ovpnstruct.h" #include "peer.h" +#include "io.h" +#include "netlink.h" +#include "proto.h" #include "udp.h" #include "skb.h" #include "socket.h" =20 +/* Called after decrypt to write the IP packet to the device. + * This method is expected to manage/free the skb. + */ +static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) +{ + unsigned int pkt_len; + + /* we can't guarantee the packet wasn't corrupted before entering the + * VPN, therefore we give other layers a chance to check that + */ + skb->ip_summed =3D CHECKSUM_NONE; + + /* skb hash for transport packet no longer valid after decapsulation */ + skb_clear_hash(skb); + + /* post-decrypt scrub -- prepare to inject encapsulated packet onto the + * interface, based on __skb_tunnel_rx() in dst.h + */ + skb->dev =3D peer->ovpn->dev; + skb_set_queue_mapping(skb, 0); + skb_scrub_packet(skb, true); + + skb_reset_network_header(skb); + skb_reset_transport_header(skb); + skb_probe_transport_header(skb); + skb_reset_inner_headers(skb); + + memset(skb->cb, 0, sizeof(skb->cb)); + + /* cause packet to be "received" by the interface */ + pkt_len =3D skb->len; + if (likely(gro_cells_receive(&peer->ovpn->gro_cells, + skb) =3D=3D NET_RX_SUCCESS)) + /* update RX stats with the size of decrypted packet */ + dev_sw_netstats_rx_add(peer->ovpn->dev, pkt_len); +} + +static void ovpn_decrypt_post(struct sk_buff *skb, int ret) +{ + struct ovpn_peer *peer =3D ovpn_skb_cb(skb)->peer; + + if (unlikely(ret < 0)) + goto drop; + + ovpn_netdev_write(peer, skb); + /* skb is passed to upper layer - don't free it */ + skb =3D NULL; +drop: + if (unlikely(skb)) + dev_core_stats_rx_dropped_inc(peer->ovpn->dev); + ovpn_peer_put(peer); + kfree_skb(skb); +} + +/* pick next packet from RX queue, decrypt and forward it to the device */ +void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb) +{ + ovpn_skb_cb(skb)->peer =3D peer; + ovpn_decrypt_post(skb, 0); +} + static void ovpn_encrypt_post(struct sk_buff *skb, int ret) { struct ovpn_peer *peer =3D ovpn_skb_cb(skb)->peer; diff --git a/drivers/net/ovpn/io.h b/drivers/net/ovpn/io.h index aa259be66441f7b0262f39da12d6c3dce0a9b24c..9667a0a470e0b4b427524fffb5b= 9b395007e5a2f 100644 --- a/drivers/net/ovpn/io.h +++ b/drivers/net/ovpn/io.h @@ -12,4 +12,6 @@ =20 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev); =20 +void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb); + #endif /* _NET_OVPN_OVPN_H_ */ diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index 5492ce07751d135c1484fe1ed8227c646df94969..73348765a8cf24321aa6be78e75= f607d6dbffb1d 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +33,16 @@ static void ovpn_struct_free(struct net_device *net) =20 static int ovpn_net_init(struct net_device *dev) { - return 0; + struct ovpn_struct *ovpn =3D netdev_priv(dev); + + return gro_cells_init(&ovpn->gro_cells, dev); +} + +static void ovpn_net_uninit(struct net_device *dev) +{ + struct ovpn_struct *ovpn =3D netdev_priv(dev); + + gro_cells_destroy(&ovpn->gro_cells); } =20 static int ovpn_net_open(struct net_device *dev) @@ -56,6 +66,7 @@ static int ovpn_net_stop(struct net_device *dev) =20 static const struct net_device_ops ovpn_netdev_ops =3D { .ndo_init =3D ovpn_net_init, + .ndo_uninit =3D ovpn_net_uninit, .ndo_open =3D ovpn_net_open, .ndo_stop =3D ovpn_net_stop, .ndo_start_xmit =3D ovpn_net_xmit, diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h index a22c5083381c131db01a28c0f51e661d690d4998..4a48fc048890ab1cda78bc104fe= 3034b4a49d226 100644 --- a/drivers/net/ovpn/ovpnstruct.h +++ b/drivers/net/ovpn/ovpnstruct.h @@ -10,6 +10,7 @@ #ifndef _NET_OVPN_OVPNSTRUCT_H_ #define _NET_OVPN_OVPNSTRUCT_H_ =20 +#include #include #include #include @@ -23,6 +24,7 @@ * @lock: protect this object * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list + * @gro_cells: pointer to the Generic Receive Offload cell */ struct ovpn_struct { struct net_device *dev; @@ -32,6 +34,7 @@ struct ovpn_struct { spinlock_t lock; /* protect writing to the ovpn_struct object */ struct ovpn_peer __rcu *peer; struct list_head dev_list; + struct gro_cells gro_cells; }; =20 #endif /* _NET_OVPN_OVPNSTRUCT_H_ */ diff --git a/drivers/net/ovpn/proto.h b/drivers/net/ovpn/proto.h new file mode 100644 index 0000000000000000000000000000000000000000..69604cf26bbf82539ee5cd5a7ac= 9c23920f555de --- /dev/null +++ b/drivers/net/ovpn/proto.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#ifndef _NET_OVPN_OVPNPROTO_H_ +#define _NET_OVPN_OVPNPROTO_H_ + +#include "main.h" + +#include + +/* Methods for operating on the initial command + * byte of the OpenVPN protocol. + */ + +/* packet opcode (high 5 bits) and key-id (low 3 bits) are combined in + * one byte + */ +#define OVPN_KEY_ID_MASK 0x07 +#define OVPN_OPCODE_SHIFT 3 +#define OVPN_OPCODE_MASK 0x1F +/* upper bounds on opcode and key ID */ +#define OVPN_KEY_ID_MAX (OVPN_KEY_ID_MASK + 1) +#define OVPN_OPCODE_MAX (OVPN_OPCODE_MASK + 1) +/* packet opcodes of interest to us */ +#define OVPN_DATA_V1 6 /* data channel V1 packet */ +#define OVPN_DATA_V2 9 /* data channel V2 packet */ +/* size of initial packet opcode */ +#define OVPN_OP_SIZE_V1 1 +#define OVPN_OP_SIZE_V2 4 +#define OVPN_PEER_ID_MASK 0x00FFFFFF +#define OVPN_PEER_ID_UNDEF 0x00FFFFFF +/* first byte of keepalive message */ +#define OVPN_KEEPALIVE_FIRST_BYTE 0x2a +/* first byte of exit message */ +#define OVPN_EXPLICIT_EXIT_NOTIFY_FIRST_BYTE 0x28 + +/** + * ovpn_opcode_from_skb - extract OP code from skb at specified offset + * @skb: the packet to extract the OP code from + * @offset: the offset in the data buffer where the OP code is located + * + * Note: this function assumes that the skb head was pulled enough + * to access the first byte. + * + * Return: the OP code + */ +static inline u8 ovpn_opcode_from_skb(const struct sk_buff *skb, u16 offse= t) +{ + u8 byte =3D *(skb->data + offset); + + return byte >> OVPN_OPCODE_SHIFT; +} + +/** + * ovpn_peer_id_from_skb - extract peer ID from skb at specified offset + * @skb: the packet to extract the OP code from + * @offset: the offset in the data buffer where the OP code is located + * + * Note: this function assumes that the skb head was pulled enough + * to access the first 4 bytes. + * + * Return: the peer ID. + */ +static inline u32 ovpn_peer_id_from_skb(const struct sk_buff *skb, u16 off= set) +{ + return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK; +} + +#endif /* _NET_OVPN_OVPNPROTO_H_ */ diff --git a/drivers/net/ovpn/socket.c b/drivers/net/ovpn/socket.c index 090a3232ab0ec19702110f1a90f45c7f10889f6f..964b566de69f4132806a969a455= cec7f6059a0bd 100644 --- a/drivers/net/ovpn/socket.c +++ b/drivers/net/ovpn/socket.c @@ -22,6 +22,9 @@ static void ovpn_socket_detach(struct socket *sock) if (!sock) return; =20 + if (sock->sk->sk_protocol =3D=3D IPPROTO_UDP) + ovpn_udp_socket_detach(sock); + sockfd_put(sock); } =20 @@ -71,6 +74,27 @@ static int ovpn_socket_attach(struct socket *sock, struc= t ovpn_peer *peer) return ret; } =20 +/* Retrieve the corresponding ovpn object from a UDP socket + * rcu_read_lock must be held on entry + */ +struct ovpn_struct *ovpn_from_udp_sock(struct sock *sk) +{ + struct ovpn_socket *ovpn_sock; + + if (unlikely(READ_ONCE(udp_sk(sk)->encap_type) !=3D UDP_ENCAP_OVPNINUDP)) + return NULL; + + ovpn_sock =3D rcu_dereference_sk_user_data(sk); + if (unlikely(!ovpn_sock)) + return NULL; + + /* make sure that sk matches our stored transport socket */ + if (unlikely(!ovpn_sock->sock || sk !=3D ovpn_sock->sock->sk)) + return NULL; + + return ovpn_sock->ovpn; +} + /** * ovpn_socket_new - create a new socket and initialize it * @sock: the kernel socket to embed diff --git a/drivers/net/ovpn/udp.c b/drivers/net/ovpn/udp.c index d26d7566e9c8dfe91fa77f49c34fb179a9fb2239..d1e88ae83843f02d591e67a7995= f2d6868720695 100644 --- a/drivers/net/ovpn/udp.c +++ b/drivers/net/ovpn/udp.c @@ -21,9 +21,95 @@ #include "bind.h" #include "io.h" #include "peer.h" +#include "proto.h" #include "socket.h" #include "udp.h" =20 +/** + * ovpn_udp_encap_recv - Start processing a received UDP packet. + * @sk: socket over which the packet was received + * @skb: the received packet + * + * If the first byte of the payload is DATA_V2, the packet is further proc= essed, + * otherwise it is forwarded to the UDP stack for delivery to user space. + * + * Return: + * 0 if skb was consumed or dropped + * >0 if skb should be passed up to userspace as UDP (packet not consumed) + * <0 if skb should be resubmitted as proto -N (packet not consumed) + */ +static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) +{ + struct ovpn_peer *peer =3D NULL; + struct ovpn_struct *ovpn; + u32 peer_id; + u8 opcode; + + ovpn =3D ovpn_from_udp_sock(sk); + if (unlikely(!ovpn)) { + net_err_ratelimited("%s: cannot obtain ovpn object from UDP socket\n", + __func__); + goto drop_noovpn; + } + + /* Make sure the first 4 bytes of the skb data buffer after the UDP + * header are accessible. + * They are required to fetch the OP code, the key ID and the peer ID. + */ + if (unlikely(!pskb_may_pull(skb, sizeof(struct udphdr) + + OVPN_OP_SIZE_V2))) { + net_dbg_ratelimited("%s: packet too small\n", __func__); + goto drop; + } + + opcode =3D ovpn_opcode_from_skb(skb, sizeof(struct udphdr)); + if (unlikely(opcode !=3D OVPN_DATA_V2)) { + /* DATA_V1 is not supported */ + if (opcode =3D=3D OVPN_DATA_V1) + goto drop; + + /* unknown or control packet: let it bubble up to userspace */ + return 1; + } + + peer_id =3D ovpn_peer_id_from_skb(skb, sizeof(struct udphdr)); + /* some OpenVPN server implementations send data packets with the + * peer-id set to undef. In this case we skip the peer lookup by peer-id + * and we try with the transport address + */ + if (peer_id !=3D OVPN_PEER_ID_UNDEF) { + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) { + net_err_ratelimited("%s: received data from unknown peer (id: %d)\n", + __func__, peer_id); + goto drop; + } + } + + if (!peer) { + /* data packet with undef peer-id */ + peer =3D ovpn_peer_get_by_transp_addr(ovpn, skb); + if (unlikely(!peer)) { + net_dbg_ratelimited("%s: received data with undef peer-id from unknown = source\n", + __func__); + goto drop; + } + } + + /* pop off outer UDP header */ + __skb_pull(skb, sizeof(struct udphdr)); + ovpn_recv(peer, skb); + return 0; + +drop: + if (peer) + ovpn_peer_put(peer); + dev_core_stats_rx_dropped_inc(ovpn->dev); +drop_noovpn: + kfree_skb(skb); + return 0; +} + /** * ovpn_udp4_output - send IPv4 packet over udp socket * @ovpn: the openvpn instance @@ -259,8 +345,12 @@ void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struc= t ovpn_peer *peer, */ int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) { + struct udp_tunnel_sock_cfg cfg =3D { + .encap_type =3D UDP_ENCAP_OVPNINUDP, + .encap_rcv =3D ovpn_udp_encap_recv, + }; struct ovpn_socket *old_data; - int ret =3D 0; + int ret; =20 /* sanity check */ if (sock->sk->sk_protocol !=3D IPPROTO_UDP) { @@ -274,6 +364,7 @@ int ovpn_udp_socket_attach(struct socket *sock, struct = ovpn_struct *ovpn) if (!old_data) { /* socket is currently unused - we can take it */ rcu_read_unlock(); + setup_udp_tunnel_sock(sock_net(sock->sk), sock, &cfg); return 0; } =20 @@ -302,3 +393,14 @@ int ovpn_udp_socket_attach(struct socket *sock, struct= ovpn_struct *ovpn) =20 return ret; } + +/** + * ovpn_udp_socket_detach - clean udp-tunnel status for this socket + * @sock: the socket to clean + */ +void ovpn_udp_socket_detach(struct socket *sock) +{ + struct udp_tunnel_sock_cfg cfg =3D { }; + + setup_udp_tunnel_sock(sock_net(sock->sk), sock, &cfg); +} diff --git a/drivers/net/ovpn/udp.h b/drivers/net/ovpn/udp.h index e60f8cd2b4ac8f910aabcf8ed546af59d6ca4be4..fecb68464896bc1228315faf268= 453f9005e693d 100644 --- a/drivers/net/ovpn/udp.h +++ b/drivers/net/ovpn/udp.h @@ -18,8 +18,9 @@ struct sk_buff; struct socket; =20 int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn); - +void ovpn_udp_socket_detach(struct socket *sock); void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer, struct sk_buff *skb); +struct ovpn_struct *ovpn_from_udp_sock(struct sock *sk); =20 #endif /* _NET_OVPN_UDP_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 9476318BC3D for ; Wed, 16 Oct 2024 01:04:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040646; cv=none; b=GdEb9gRCpzOZTTkaEolKbKiGDDPD9mEDFIHJBOCQU1Y22OjDfSp5il6+oBofHAHeHskbcrhxIKQ9HrlLgU2yBTFblUoBaECEBoQQ79+uBeSKBbM6v/+5uFPtGK+8oKJUDEDkdOxldikQbRRD121Cr5kDJtsiejfF/LribN25XxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040646; c=relaxed/simple; bh=mjmESI8LfgVgPao1Gs/8Edx4jwJgI60KKkAesrW7tdQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EkjiuQQmTpyATyDwDafQeFavD3TuEq1s3v6kKwBeiWlllWEIiCUGD4ZEqhm//H1qKPRHQRYjV42adC/w25O4xF1rKfJ7odxpHx3mQmCamKeIZueirwXV6HO/NkpNRPqVW0128QM0GbMJPNj6NZkOEMpPHPT86TzzQiJs7W5XL2I= 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=FrrJn/lC; arc=none smtp.client-ip=209.85.221.46 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="FrrJn/lC" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-37d63a79bb6so2469647f8f.0 for ; Tue, 15 Oct 2024 18:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040641; x=1729645441; 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=r//HaE/rXxF3mLho1t+pKJoz+6lLFOgJZxMrfMQG96w=; b=FrrJn/lCgDp4XYkKMnrKjdR6jsOms9mN4ltwabZFfMzbDq6L4i3w3aVhVn3fkKNcDB u/KJI6MCQ5kayJUznCHewbnkbSm0tiOKpPldPJ9aAebj9BDpI3/xW7IO3Ye+mQD2FMDo IZ/0vtnKt8FDtDnJjsWFMbr3OtjIWDGyDxIusz9cI33FaXj1HuL6vsGKq5ceHGzsLdqc OLwmRfGm/PUJ6ij5XS9FKxdnaZeJceiZ1RGrb6LfxwxpQttytR9wFpiL8I/MxLRPn1RT crwcSeOmDAgUnjd9JpPFcB7bFpJSjd7ezjCT5kJCn07VVmd5Tp1c13FgVM0Xt1OvVgoY fx6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040641; x=1729645441; 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=r//HaE/rXxF3mLho1t+pKJoz+6lLFOgJZxMrfMQG96w=; b=VVkz6Xzc/zSI23HxYEyOLnkuNAIe9iBPiHWcJwdPMEFAMWqtKPFAm8RSHEGBdQZfRv qV2+Rjmq/2kD15sAAAHfSlQEshBE6KcLYS82dnUdKnV+X4pIvg1gUHsEhxVoSnyrYYus iA4tx05xY0zQcUB9K2DgeX1q+guOfxJtbx8zqvTxJQ4h1HXejx4KS/4FVqJC97Zxi228 duN9AehrV8ELxjGPu/7WwpAQ0Bbeav0nVvc+qzV6pw6zfZqn17fMLeFjbwOeFOIs/4Jh 8aXeg8+g0V0bSJVkH0lTbhPzTPmX8nW0inETcNs+0ce7jyxXCpHYdobxSwZg4FS6c9Ht 4vfQ== X-Forwarded-Encrypted: i=1; AJvYcCVr2OFk0BIBDI2r68kn8eNI5WoAf91QpdcnT4hCBPTgXXq2lLSzuFr7jJX76WV1IpVXvkja2uTfLQ2D2Zg=@vger.kernel.org X-Gm-Message-State: AOJu0YwuENjOwJH1DPgYIZBZzzXHJMT5RmBIPJ0f7SGlz56uTJHjdrRN 5U2DmLdf97HaU+P6d7+7p5vWUllvfhfZoY9LEUPQmjuM1mSmCGS9FRZbfBKgzo0= X-Google-Smtp-Source: AGHT+IG/V+esg9x2jwoxhPMhYjAyZtxQPWZVhq+tyIHqRrWDPTG6JbDT00tGEMixuh4IZfraVkUhQw== X-Received: by 2002:adf:f806:0:b0:37c:c5fc:89f4 with SMTP id ffacd0b85a97d-37d552d8c12mr10307836f8f.51.1729040640607; Tue, 15 Oct 2024 18:04:00 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.03.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:00 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:10 +0200 Subject: [PATCH net-next v9 10/23] ovpn: implement packet processing 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: <20241016-b4-ovpn-v9-10-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=40973; i=antonio@openvpn.net; h=from:subject:message-id; bh=mjmESI8LfgVgPao1Gs/8Edx4jwJgI60KKkAesrW7tdQ=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvjk/8BWyh15XsGWtRBRBkxPCBSuYbm7YaW wRVOth79hCJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV h6aeB/9og2n6Rw8VBaciWZMipoksBQeeHUnv3BPkr66+8phK2N69r2Ee2EFm8XPdFMnNUccB+6e s48TTEaj/2ZDuTbqhIRbdaK+J3NXUznJv7GzrfUAHAVW5eaa6jPeBM7qh8QcrDQwt7dkXKAcYfa br2LA4MwgV0jWj85HlSr+8dsUGsyor2bHvwdlt6dIFav5G9jla3YNSD/HP0SxOnHtxkybOqks4H QkN4L5QHZAC4XN6COIPzP5l9aYZK7KVSgBIrMmXgnHA0UQfC9FDX1jUyWutUEayhgT4U2gRAajA CAGrTnLxCMPvbQDwHDWVy5nv415ybp/DhjTczVLn/gqxghPp X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C This change implements encryption/decryption and encapsulation/decapsulation of OpenVPN packets. Support for generic crypto state is added along with a wrapper for the AEAD crypto kernel API. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/Makefile | 3 + drivers/net/ovpn/crypto.c | 153 ++++++++++++++++++ drivers/net/ovpn/crypto.h | 139 ++++++++++++++++ drivers/net/ovpn/crypto_aead.c | 354 +++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/crypto_aead.h | 31 ++++ drivers/net/ovpn/io.c | 141 ++++++++++++++-- drivers/net/ovpn/io.h | 3 + drivers/net/ovpn/packet.h | 2 +- drivers/net/ovpn/peer.c | 29 ++++ drivers/net/ovpn/peer.h | 6 + drivers/net/ovpn/pktid.c | 130 +++++++++++++++ drivers/net/ovpn/pktid.h | 87 ++++++++++ drivers/net/ovpn/proto.h | 31 ++++ drivers/net/ovpn/skb.h | 11 +- 14 files changed, 1108 insertions(+), 12 deletions(-) diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index 56bddc9bef83e0befde6af3c3565bb91731d7b22..ccdaeced1982c851475657860a0= 05ff2b9dfbd13 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -8,10 +8,13 @@ =20 obj-$(CONFIG_OVPN) :=3D ovpn.o ovpn-y +=3D bind.o +ovpn-y +=3D crypto.o +ovpn-y +=3D crypto_aead.o ovpn-y +=3D main.o ovpn-y +=3D io.o ovpn-y +=3D netlink.o ovpn-y +=3D netlink-gen.o ovpn-y +=3D peer.o +ovpn-y +=3D pktid.o ovpn-y +=3D socket.o ovpn-y +=3D udp.o diff --git a/drivers/net/ovpn/crypto.c b/drivers/net/ovpn/crypto.c new file mode 100644 index 0000000000000000000000000000000000000000..f1f7510e2f735e367f96eb4982b= a82c9af3c8bfc --- /dev/null +++ b/drivers/net/ovpn/crypto.c @@ -0,0 +1,153 @@ +// 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 + +#include "ovpnstruct.h" +#include "main.h" +#include "packet.h" +#include "pktid.h" +#include "crypto_aead.h" +#include "crypto.h" + +static void ovpn_ks_destroy_rcu(struct rcu_head *head) +{ + struct ovpn_crypto_key_slot *ks; + + ks =3D container_of(head, struct ovpn_crypto_key_slot, rcu); + ovpn_aead_crypto_key_slot_destroy(ks); +} + +void ovpn_crypto_key_slot_release(struct kref *kref) +{ + struct ovpn_crypto_key_slot *ks; + + ks =3D container_of(kref, struct ovpn_crypto_key_slot, refcount); + call_rcu(&ks->rcu, ovpn_ks_destroy_rcu); +} + +/* can only be invoked when all peer references have been dropped (i.e. RCU + * release routine) + */ +void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) +{ + struct ovpn_crypto_key_slot *ks; + + ks =3D rcu_access_pointer(cs->slots[0]); + if (ks) { + RCU_INIT_POINTER(cs->slots[0], NULL); + ovpn_crypto_key_slot_put(ks); + } + + ks =3D rcu_access_pointer(cs->slots[1]); + if (ks) { + RCU_INIT_POINTER(cs->slots[1], NULL); + ovpn_crypto_key_slot_put(ks); + } +} + +/* Reset the ovpn_crypto_state object in a way that is atomic + * to RCU readers. + */ +int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs, + const struct ovpn_peer_key_reset *pkr) +{ + struct ovpn_crypto_key_slot *old =3D NULL, *new; + u8 idx; + + if (pkr->slot !=3D OVPN_KEY_SLOT_PRIMARY && + pkr->slot !=3D OVPN_KEY_SLOT_SECONDARY) + return -EINVAL; + + new =3D ovpn_aead_crypto_key_slot_new(&pkr->key); + if (IS_ERR(new)) + return PTR_ERR(new); + + spin_lock_bh(&cs->lock); + idx =3D cs->primary_idx; + switch (pkr->slot) { + case OVPN_KEY_SLOT_PRIMARY: + old =3D rcu_replace_pointer(cs->slots[idx], new, + lockdep_is_held(&cs->lock)); + break; + case OVPN_KEY_SLOT_SECONDARY: + old =3D rcu_replace_pointer(cs->slots[!idx], new, + lockdep_is_held(&cs->lock)); + break; + } + spin_unlock_bh(&cs->lock); + + if (old) + ovpn_crypto_key_slot_put(old); + + return 0; +} + +void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs, + enum ovpn_key_slot slot) +{ + struct ovpn_crypto_key_slot *ks =3D NULL; + u8 idx; + + if (slot !=3D OVPN_KEY_SLOT_PRIMARY && + slot !=3D OVPN_KEY_SLOT_SECONDARY) { + pr_warn("Invalid slot to release: %u\n", slot); + return; + } + + spin_lock_bh(&cs->lock); + idx =3D cs->primary_idx; + switch (slot) { + case OVPN_KEY_SLOT_PRIMARY: + ks =3D rcu_replace_pointer(cs->slots[idx], NULL, + lockdep_is_held(&cs->lock)); + break; + case OVPN_KEY_SLOT_SECONDARY: + ks =3D rcu_replace_pointer(cs->slots[!idx], NULL, + lockdep_is_held(&cs->lock)); + break; + } + spin_unlock_bh(&cs->lock); + + if (!ks) { + pr_debug("Key slot already released: %u\n", slot); + return; + } + + pr_debug("deleting key slot %u, key_id=3D%u\n", slot, ks->key_id); + ovpn_crypto_key_slot_put(ks); +} + +/* this swap is not atomic, but there will be a very short time frame wher= e the + * old_secondary key won't be available. This should not be a big deal as = most + * likely both peers are already using the new primary at this point. + */ +void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs) +{ + const struct ovpn_crypto_key_slot *old_primary, *old_secondary; + u8 idx; + + spin_lock_bh(&cs->lock); + idx =3D cs->primary_idx; + old_primary =3D rcu_dereference_protected(cs->slots[idx], + lockdep_is_held(&cs->lock)); + old_secondary =3D rcu_dereference_protected(cs->slots[!idx], + lockdep_is_held(&cs->lock)); + /* perform real swap by switching the index of the primary key */ + cs->primary_idx =3D !cs->primary_idx; + + pr_debug("key swapped: (old primary) %d <-> (new primary) %d\n", + old_primary ? old_primary->key_id : -1, + old_secondary ? old_secondary->key_id : -1); + + spin_unlock_bh(&cs->lock); +} diff --git a/drivers/net/ovpn/crypto.h b/drivers/net/ovpn/crypto.h new file mode 100644 index 0000000000000000000000000000000000000000..3b437d26b531c3034cca5343c75= 5ef9c7ef57276 --- /dev/null +++ b/drivers/net/ovpn/crypto.h @@ -0,0 +1,139 @@ +/* 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_OVPNCRYPTO_H_ +#define _NET_OVPN_OVPNCRYPTO_H_ + +#include "packet.h" +#include "pktid.h" + +/* info needed for both encrypt and decrypt directions */ +struct ovpn_key_direction { + const u8 *cipher_key; + size_t cipher_key_size; + const u8 *nonce_tail; /* only needed for GCM modes */ + size_t nonce_tail_size; /* only needed for GCM modes */ +}; + +/* all info for a particular symmetric key (primary or secondary) */ +struct ovpn_key_config { + enum ovpn_cipher_alg cipher_alg; + u8 key_id; + struct ovpn_key_direction encrypt; + struct ovpn_key_direction decrypt; +}; + +/* used to pass settings from netlink to the crypto engine */ +struct ovpn_peer_key_reset { + enum ovpn_key_slot slot; + struct ovpn_key_config key; +}; + +struct ovpn_crypto_key_slot { + u8 key_id; + + struct crypto_aead *encrypt; + struct crypto_aead *decrypt; + struct ovpn_nonce_tail nonce_tail_xmit; + struct ovpn_nonce_tail nonce_tail_recv; + + struct ovpn_pktid_recv pid_recv ____cacheline_aligned_in_smp; + struct ovpn_pktid_xmit pid_xmit ____cacheline_aligned_in_smp; + struct kref refcount; + struct rcu_head rcu; +}; + +struct ovpn_crypto_state { + struct ovpn_crypto_key_slot __rcu *slots[2]; + u8 primary_idx; + + /* protects primary and secondary slots */ + spinlock_t lock; +}; + +static inline bool ovpn_crypto_key_slot_hold(struct ovpn_crypto_key_slot *= ks) +{ + return kref_get_unless_zero(&ks->refcount); +} + +static inline void ovpn_crypto_state_init(struct ovpn_crypto_state *cs) +{ + RCU_INIT_POINTER(cs->slots[0], NULL); + RCU_INIT_POINTER(cs->slots[1], NULL); + cs->primary_idx =3D 0; + spin_lock_init(&cs->lock); +} + +static inline struct ovpn_crypto_key_slot * +ovpn_crypto_key_id_to_slot(const struct ovpn_crypto_state *cs, u8 key_id) +{ + struct ovpn_crypto_key_slot *ks; + u8 idx; + + if (unlikely(!cs)) + return NULL; + + rcu_read_lock(); + idx =3D cs->primary_idx; + ks =3D rcu_dereference(cs->slots[idx]); + if (ks && ks->key_id =3D=3D key_id) { + if (unlikely(!ovpn_crypto_key_slot_hold(ks))) + ks =3D NULL; + goto out; + } + + ks =3D rcu_dereference(cs->slots[idx ^ 1]); + if (ks && ks->key_id =3D=3D key_id) { + if (unlikely(!ovpn_crypto_key_slot_hold(ks))) + ks =3D NULL; + goto out; + } + + /* when both key slots are occupied but no matching key ID is found, ks + * has to be reset to NULL to avoid carrying a stale pointer + */ + ks =3D NULL; +out: + rcu_read_unlock(); + + return ks; +} + +static inline struct ovpn_crypto_key_slot * +ovpn_crypto_key_slot_primary(const struct ovpn_crypto_state *cs) +{ + struct ovpn_crypto_key_slot *ks; + + rcu_read_lock(); + ks =3D rcu_dereference(cs->slots[cs->primary_idx]); + if (unlikely(ks && !ovpn_crypto_key_slot_hold(ks))) + ks =3D NULL; + rcu_read_unlock(); + + return ks; +} + +void ovpn_crypto_key_slot_release(struct kref *kref); + +static inline void ovpn_crypto_key_slot_put(struct ovpn_crypto_key_slot *k= s) +{ + kref_put(&ks->refcount, ovpn_crypto_key_slot_release); +} + +int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs, + const struct ovpn_peer_key_reset *pkr); + +void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs, + enum ovpn_key_slot slot); + +void ovpn_crypto_state_release(struct ovpn_crypto_state *cs); + +void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs); + +#endif /* _NET_OVPN_OVPNCRYPTO_H_ */ diff --git a/drivers/net/ovpn/crypto_aead.c b/drivers/net/ovpn/crypto_aead.c new file mode 100644 index 0000000000000000000000000000000000000000..97134ac679c67a3d44a0bf49c2d= df058fd7c0e99 --- /dev/null +++ b/drivers/net/ovpn/crypto_aead.c @@ -0,0 +1,354 @@ +// 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 +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "packet.h" +#include "pktid.h" +#include "crypto_aead.h" +#include "crypto.h" +#include "peer.h" +#include "proto.h" +#include "skb.h" + +#define AUTH_TAG_SIZE 16 + +static int ovpn_aead_encap_overhead(const struct ovpn_crypto_key_slot *ks) +{ + return OVPN_OP_SIZE_V2 + /* OP header size */ + 4 + /* Packet ID */ + crypto_aead_authsize(ks->encrypt); /* Auth Tag */ +} + +int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot = *ks, + struct sk_buff *skb) +{ + const unsigned int tag_size =3D crypto_aead_authsize(ks->encrypt); + const unsigned int head_size =3D ovpn_aead_encap_overhead(ks); + struct aead_request *req; + struct sk_buff *trailer; + struct scatterlist *sg; + u8 iv[NONCE_SIZE]; + int nfrags, ret; + u32 pktid, op; + + /* Sample AEAD header format: + * 48000001 00000005 7e7046bd 444a7e28 cc6387b1 64a4d6c1 380275a... + * [ OP32 ] [seq # ] [ auth tag ] [ payload ... ] + * [4-byte + * IV head] + */ + + /* check that there's enough headroom in the skb for packet + * encapsulation, after adding network header and encryption overhead + */ + if (unlikely(skb_cow_head(skb, OVPN_HEAD_ROOM + head_size))) + return -ENOBUFS; + + /* get number of skb frags and ensure that packet data is writable */ + nfrags =3D skb_cow_data(skb, 0, &trailer); + if (unlikely(nfrags < 0)) + return nfrags; + + if (unlikely(nfrags + 2 > (MAX_SKB_FRAGS + 2))) + return -ENOSPC; + + ovpn_skb_cb(skb)->ctx =3D kmalloc(sizeof(*ovpn_skb_cb(skb)->ctx), + GFP_ATOMIC); + if (unlikely(!ovpn_skb_cb(skb)->ctx)) + return -ENOMEM; + + sg =3D ovpn_skb_cb(skb)->ctx->sg; + + /* sg table: + * 0: op, wire nonce (AD, len=3DOVPN_OP_SIZE_V2+NONCE_WIRE_SIZE), + * 1, 2, 3, ..., n: payload, + * n+1: auth_tag (len=3Dtag_size) + */ + sg_init_table(sg, nfrags + 2); + + /* build scatterlist to encrypt packet payload */ + ret =3D skb_to_sgvec_nomark(skb, sg + 1, 0, skb->len); + if (unlikely(nfrags !=3D ret)) + return -EINVAL; + + /* append auth_tag onto scatterlist */ + __skb_push(skb, tag_size); + sg_set_buf(sg + nfrags + 1, skb->data, tag_size); + + /* obtain packet ID, which is used both as a first + * 4 bytes of nonce and last 4 bytes of associated data. + */ + ret =3D ovpn_pktid_xmit_next(&ks->pid_xmit, &pktid); + if (unlikely(ret < 0)) { + kfree(ovpn_skb_cb(skb)->ctx); + return ret; + } + + /* concat 4 bytes packet id and 8 bytes nonce tail into 12 bytes + * nonce + */ + ovpn_pktid_aead_write(pktid, &ks->nonce_tail_xmit, iv); + + /* make space for packet id and push it to the front */ + __skb_push(skb, NONCE_WIRE_SIZE); + memcpy(skb->data, iv, NONCE_WIRE_SIZE); + + /* add packet op as head of additional data */ + op =3D ovpn_opcode_compose(OVPN_DATA_V2, ks->key_id, peer->id); + __skb_push(skb, OVPN_OP_SIZE_V2); + BUILD_BUG_ON(sizeof(op) !=3D OVPN_OP_SIZE_V2); + *((__force __be32 *)skb->data) =3D htonl(op); + + /* AEAD Additional data */ + sg_set_buf(sg, skb->data, OVPN_OP_SIZE_V2 + NONCE_WIRE_SIZE); + + req =3D aead_request_alloc(ks->encrypt, GFP_ATOMIC); + if (unlikely(!req)) { + kfree(ovpn_skb_cb(skb)->ctx); + return -ENOMEM; + } + + /* setup async crypto operation */ + aead_request_set_tfm(req, ks->encrypt); + aead_request_set_callback(req, 0, ovpn_encrypt_post, skb); + aead_request_set_crypt(req, sg, sg, skb->len - head_size, iv); + aead_request_set_ad(req, OVPN_OP_SIZE_V2 + NONCE_WIRE_SIZE); + + ovpn_skb_cb(skb)->ctx->peer =3D peer; + ovpn_skb_cb(skb)->ctx->req =3D req; + ovpn_skb_cb(skb)->ctx->ks =3D ks; + + /* encrypt it */ + return crypto_aead_encrypt(req); +} + +int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot = *ks, + struct sk_buff *skb) +{ + const unsigned int tag_size =3D crypto_aead_authsize(ks->decrypt); + int ret, payload_len, nfrags; + unsigned int payload_offset; + struct aead_request *req; + struct sk_buff *trailer; + struct scatterlist *sg; + unsigned int sg_len; + u8 iv[NONCE_SIZE]; + + payload_offset =3D OVPN_OP_SIZE_V2 + NONCE_WIRE_SIZE + tag_size; + payload_len =3D skb->len - payload_offset; + + /* sanity check on packet size, payload size must be >=3D 0 */ + if (unlikely(payload_len < 0)) + return -EINVAL; + + /* Prepare the skb data buffer to be accessed up until the auth tag. + * This is required because this area is directly mapped into the sg + * list. + */ + if (unlikely(!pskb_may_pull(skb, payload_offset))) + return -ENODATA; + + /* get number of skb frags and ensure that packet data is writable */ + nfrags =3D skb_cow_data(skb, 0, &trailer); + if (unlikely(nfrags < 0)) + return nfrags; + + if (unlikely(nfrags + 2 > (MAX_SKB_FRAGS + 2))) + return -ENOSPC; + + ovpn_skb_cb(skb)->ctx =3D kmalloc(sizeof(*ovpn_skb_cb(skb)->ctx), + GFP_ATOMIC); + if (unlikely(!ovpn_skb_cb(skb)->ctx)) + return -ENOMEM; + + sg =3D ovpn_skb_cb(skb)->ctx->sg; + + /* sg table: + * 0: op, wire nonce (AD, len=3DOVPN_OP_SIZE_V2+NONCE_WIRE_SIZE), + * 1, 2, 3, ..., n: payload, + * n+1: auth_tag (len=3Dtag_size) + */ + sg_init_table(sg, nfrags + 2); + + /* packet op is head of additional data */ + sg_len =3D OVPN_OP_SIZE_V2 + NONCE_WIRE_SIZE; + sg_set_buf(sg, skb->data, sg_len); + + /* build scatterlist to decrypt packet payload */ + ret =3D skb_to_sgvec_nomark(skb, sg + 1, payload_offset, payload_len); + if (unlikely(nfrags !=3D ret)) { + kfree(ovpn_skb_cb(skb)->ctx); + return -EINVAL; + } + + /* append auth_tag onto scatterlist */ + sg_set_buf(sg + nfrags + 1, skb->data + sg_len, tag_size); + + /* copy nonce into IV buffer */ + memcpy(iv, skb->data + OVPN_OP_SIZE_V2, NONCE_WIRE_SIZE); + memcpy(iv + NONCE_WIRE_SIZE, ks->nonce_tail_recv.u8, + sizeof(struct ovpn_nonce_tail)); + + req =3D aead_request_alloc(ks->decrypt, GFP_ATOMIC); + if (unlikely(!req)) { + kfree(ovpn_skb_cb(skb)->ctx); + return -ENOMEM; + } + + /* setup async crypto operation */ + aead_request_set_tfm(req, ks->decrypt); + aead_request_set_callback(req, 0, ovpn_decrypt_post, skb); + aead_request_set_crypt(req, sg, sg, payload_len + tag_size, iv); + + aead_request_set_ad(req, NONCE_WIRE_SIZE + OVPN_OP_SIZE_V2); + + ovpn_skb_cb(skb)->ctx->payload_offset =3D payload_offset; + ovpn_skb_cb(skb)->ctx->peer =3D peer; + ovpn_skb_cb(skb)->ctx->req =3D req; + ovpn_skb_cb(skb)->ctx->ks =3D ks; + + /* decrypt it */ + return crypto_aead_decrypt(req); +} + +/* Initialize a struct crypto_aead object */ +struct crypto_aead *ovpn_aead_init(const char *title, const char *alg_name, + const unsigned char *key, + unsigned int keylen) +{ + struct crypto_aead *aead; + int ret; + + aead =3D crypto_alloc_aead(alg_name, 0, 0); + if (IS_ERR(aead)) { + ret =3D PTR_ERR(aead); + pr_err("%s crypto_alloc_aead failed, err=3D%d\n", title, ret); + aead =3D NULL; + goto error; + } + + ret =3D crypto_aead_setkey(aead, key, keylen); + if (ret) { + pr_err("%s crypto_aead_setkey size=3D%u failed, err=3D%d\n", title, + keylen, ret); + goto error; + } + + ret =3D crypto_aead_setauthsize(aead, AUTH_TAG_SIZE); + if (ret) { + pr_err("%s crypto_aead_setauthsize failed, err=3D%d\n", title, + ret); + goto error; + } + + /* basic AEAD assumption */ + if (crypto_aead_ivsize(aead) !=3D NONCE_SIZE) { + pr_err("%s IV size must be %d\n", title, NONCE_SIZE); + ret =3D -EINVAL; + goto error; + } + + pr_debug("********* Cipher %s (%s)\n", alg_name, title); + pr_debug("*** IV size=3D%u\n", crypto_aead_ivsize(aead)); + pr_debug("*** req size=3D%u\n", crypto_aead_reqsize(aead)); + pr_debug("*** block size=3D%u\n", crypto_aead_blocksize(aead)); + pr_debug("*** auth size=3D%u\n", crypto_aead_authsize(aead)); + pr_debug("*** alignmask=3D0x%x\n", crypto_aead_alignmask(aead)); + + return aead; + +error: + crypto_free_aead(aead); + return ERR_PTR(ret); +} + +void ovpn_aead_crypto_key_slot_destroy(struct ovpn_crypto_key_slot *ks) +{ + if (!ks) + return; + + crypto_free_aead(ks->encrypt); + crypto_free_aead(ks->decrypt); + kfree(ks); +} + +struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc) +{ + struct ovpn_crypto_key_slot *ks =3D NULL; + const char *alg_name; + int ret; + + /* validate crypto alg */ + switch (kc->cipher_alg) { + case OVPN_CIPHER_ALG_AES_GCM: + alg_name =3D "gcm(aes)"; + break; + case OVPN_CIPHER_ALG_CHACHA20_POLY1305: + alg_name =3D "rfc7539(chacha20,poly1305)"; + break; + default: + return ERR_PTR(-EOPNOTSUPP); + } + + if (sizeof(struct ovpn_nonce_tail) !=3D kc->encrypt.nonce_tail_size || + sizeof(struct ovpn_nonce_tail) !=3D kc->decrypt.nonce_tail_size) + return ERR_PTR(-EINVAL); + + /* build the key slot */ + ks =3D kmalloc(sizeof(*ks), GFP_KERNEL); + if (!ks) + return ERR_PTR(-ENOMEM); + + ks->encrypt =3D NULL; + ks->decrypt =3D NULL; + kref_init(&ks->refcount); + ks->key_id =3D kc->key_id; + + ks->encrypt =3D ovpn_aead_init("encrypt", alg_name, + kc->encrypt.cipher_key, + kc->encrypt.cipher_key_size); + if (IS_ERR(ks->encrypt)) { + ret =3D PTR_ERR(ks->encrypt); + ks->encrypt =3D NULL; + goto destroy_ks; + } + + ks->decrypt =3D ovpn_aead_init("decrypt", alg_name, + kc->decrypt.cipher_key, + kc->decrypt.cipher_key_size); + if (IS_ERR(ks->decrypt)) { + ret =3D PTR_ERR(ks->decrypt); + ks->decrypt =3D NULL; + goto destroy_ks; + } + + memcpy(ks->nonce_tail_xmit.u8, kc->encrypt.nonce_tail, + sizeof(struct ovpn_nonce_tail)); + memcpy(ks->nonce_tail_recv.u8, kc->decrypt.nonce_tail, + sizeof(struct ovpn_nonce_tail)); + + /* init packet ID generation/validation */ + ovpn_pktid_xmit_init(&ks->pid_xmit); + ovpn_pktid_recv_init(&ks->pid_recv); + + return ks; + +destroy_ks: + ovpn_aead_crypto_key_slot_destroy(ks); + return ERR_PTR(ret); +} diff --git a/drivers/net/ovpn/crypto_aead.h b/drivers/net/ovpn/crypto_aead.h new file mode 100644 index 0000000000000000000000000000000000000000..77ee8141599bc06b0dc664c5b0a= 4dae660a89238 --- /dev/null +++ b/drivers/net/ovpn/crypto_aead.h @@ -0,0 +1,31 @@ +/* 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_OVPNAEAD_H_ +#define _NET_OVPN_OVPNAEAD_H_ + +#include "crypto.h" + +#include +#include + +struct crypto_aead *ovpn_aead_init(const char *title, const char *alg_name, + const unsigned char *key, + unsigned int keylen); + +int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot = *ks, + struct sk_buff *skb); +int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot = *ks, + struct sk_buff *skb); + +struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc); +void ovpn_aead_crypto_key_slot_destroy(struct ovpn_crypto_key_slot *ks); + +#endif /* _NET_OVPN_OVPNAEAD_H_ */ diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index 791a1b117125118b179cb13cdfd5fbab6523a360..e9d49d10cf4e9b755d7253140b1= baf55c49e8144 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -7,6 +7,7 @@ * Antonio Quartulli */ =20 +#include #include #include #include @@ -15,6 +16,9 @@ #include "ovpnstruct.h" #include "peer.h" #include "io.h" +#include "bind.h" +#include "crypto.h" +#include "crypto_aead.h" #include "netlink.h" #include "proto.h" #include "udp.h" @@ -58,33 +62,133 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, = struct sk_buff *skb) dev_sw_netstats_rx_add(peer->ovpn->dev, pkt_len); } =20 -static void ovpn_decrypt_post(struct sk_buff *skb, int ret) +void ovpn_decrypt_post(void *data, int ret) { - struct ovpn_peer *peer =3D ovpn_skb_cb(skb)->peer; + struct ovpn_crypto_key_slot *ks =3D NULL; + unsigned int payload_offset =3D 0; + struct ovpn_peer *peer =3D NULL; + struct sk_buff *skb =3D data; + __be16 proto; + __be32 *pid; + + /* crypto is happening asynchronously. this function will be called + * again later by the crypto callback with a proper return code + */ + if (unlikely(ret =3D=3D -EINPROGRESS)) + return; + + /* crypto is done, cleanup skb CB and its members */ + if (likely(ovpn_skb_cb(skb)->ctx)) { + payload_offset =3D ovpn_skb_cb(skb)->ctx->payload_offset; + ks =3D ovpn_skb_cb(skb)->ctx->ks; + peer =3D ovpn_skb_cb(skb)->ctx->peer; + + aead_request_free(ovpn_skb_cb(skb)->ctx->req); + kfree(ovpn_skb_cb(skb)->ctx); + ovpn_skb_cb(skb)->ctx =3D NULL; + } =20 if (unlikely(ret < 0)) goto drop; =20 + /* PID sits after the op */ + pid =3D (__force __be32 *)(skb->data + OVPN_OP_SIZE_V2); + ret =3D ovpn_pktid_recv(&ks->pid_recv, ntohl(*pid), 0); + if (unlikely(ret < 0)) { + net_err_ratelimited("%s: PKT ID RX error: %d\n", + peer->ovpn->dev->name, ret); + goto drop; + } + + /* point to encapsulated IP packet */ + __skb_pull(skb, payload_offset); + + /* check if this is a valid datapacket that has to be delivered to the + * ovpn interface + */ + skb_reset_network_header(skb); + proto =3D ovpn_ip_check_protocol(skb); + if (unlikely(!proto)) { + /* check if null packet */ + if (unlikely(!pskb_may_pull(skb, 1))) { + net_info_ratelimited("%s: NULL packet received from peer %u\n", + peer->ovpn->dev->name, peer->id); + goto drop; + } + + net_info_ratelimited("%s: unsupported protocol received from peer %u\n", + peer->ovpn->dev->name, peer->id); + goto drop; + } + skb->protocol =3D proto; + + /* perform Reverse Path Filtering (RPF) */ + if (unlikely(!ovpn_peer_check_by_src(peer->ovpn, skb, peer))) { + if (skb_protocol_to_family(skb) =3D=3D AF_INET6) + net_dbg_ratelimited("%s: RPF dropped packet from peer %u, src: %pI6c\n", + peer->ovpn->dev->name, peer->id, + &ipv6_hdr(skb)->saddr); + else + net_dbg_ratelimited("%s: RPF dropped packet from peer %u, src: %pI4\n", + peer->ovpn->dev->name, peer->id, + &ip_hdr(skb)->saddr); + goto drop; + } + ovpn_netdev_write(peer, skb); /* skb is passed to upper layer - don't free it */ skb =3D NULL; drop: if (unlikely(skb)) dev_core_stats_rx_dropped_inc(peer->ovpn->dev); - ovpn_peer_put(peer); + if (likely(peer)) + ovpn_peer_put(peer); + if (likely(ks)) + ovpn_crypto_key_slot_put(ks); kfree_skb(skb); } =20 /* pick next packet from RX queue, decrypt and forward it to the device */ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb) { - ovpn_skb_cb(skb)->peer =3D peer; - ovpn_decrypt_post(skb, 0); + struct ovpn_crypto_key_slot *ks; + u8 key_id; + + /* get the key slot matching the key ID in the received packet */ + key_id =3D ovpn_key_id_from_skb(skb); + ks =3D ovpn_crypto_key_id_to_slot(&peer->crypto, key_id); + if (unlikely(!ks)) { + net_info_ratelimited("%s: no available key for peer %u, key-id: %u\n", + peer->ovpn->dev->name, peer->id, key_id); + dev_core_stats_rx_dropped_inc(peer->ovpn->dev); + kfree_skb(skb); + return; + } + + ovpn_skb_cb(skb)->ctx =3D NULL; + ovpn_decrypt_post(skb, ovpn_aead_decrypt(peer, ks, skb)); } =20 -static void ovpn_encrypt_post(struct sk_buff *skb, int ret) +void ovpn_encrypt_post(void *data, int ret) { - struct ovpn_peer *peer =3D ovpn_skb_cb(skb)->peer; + struct ovpn_peer *peer =3D NULL; + struct sk_buff *skb =3D data; + + /* encryption is happening asynchronously. This function will be + * called later by the crypto callback with a proper return value + */ + if (unlikely(ret =3D=3D -EINPROGRESS)) + return; + + /* crypto is done, cleanup skb CB and its members */ + if (likely(ovpn_skb_cb(skb)->ctx)) { + peer =3D ovpn_skb_cb(skb)->ctx->peer; + + ovpn_crypto_key_slot_put(ovpn_skb_cb(skb)->ctx->ks); + aead_request_free(ovpn_skb_cb(skb)->ctx->req); + kfree(ovpn_skb_cb(skb)->ctx); + ovpn_skb_cb(skb)->ctx =3D NULL; + } =20 if (unlikely(ret < 0)) goto err; @@ -104,13 +208,29 @@ static void ovpn_encrypt_post(struct sk_buff *skb, in= t ret) err: if (unlikely(skb)) dev_core_stats_tx_dropped_inc(peer->ovpn->dev); - ovpn_peer_put(peer); + if (likely(peer)) + ovpn_peer_put(peer); kfree_skb(skb); } =20 static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) { - ovpn_skb_cb(skb)->peer =3D peer; + struct ovpn_crypto_key_slot *ks; + + if (unlikely(skb->ip_summed =3D=3D CHECKSUM_PARTIAL && + skb_checksum_help(skb))) { + net_warn_ratelimited("%s: cannot compute checksum for outgoing packet\n", + peer->ovpn->dev->name); + return false; + } + + /* get primary key to be used for encrypting data */ + ks =3D ovpn_crypto_key_slot_primary(&peer->crypto); + if (unlikely(!ks)) { + net_warn_ratelimited("%s: error while retrieving primary key slot for pe= er %u\n", + peer->ovpn->dev->name, peer->id); + return false; + } =20 /* take a reference to the peer because the crypto code may run async. * ovpn_encrypt_post() will release it upon completion @@ -120,7 +240,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, st= ruct sk_buff *skb) return false; } =20 - ovpn_encrypt_post(skb, 0); + ovpn_skb_cb(skb)->ctx =3D NULL; + ovpn_encrypt_post(skb, ovpn_aead_encrypt(peer, ks, skb)); return true; } =20 diff --git a/drivers/net/ovpn/io.h b/drivers/net/ovpn/io.h index 9667a0a470e0b4b427524fffb5b9b395007e5a2f..ad81dd86924689309b329957357= 5a1705eddaf99 100644 --- a/drivers/net/ovpn/io.h +++ b/drivers/net/ovpn/io.h @@ -14,4 +14,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net= _device *dev); =20 void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb); =20 +void ovpn_encrypt_post(void *data, int ret); +void ovpn_decrypt_post(void *data, int ret); + #endif /* _NET_OVPN_OVPN_H_ */ diff --git a/drivers/net/ovpn/packet.h b/drivers/net/ovpn/packet.h index 7ed146f5932a25f448af6da58738a7eae81007fe..e14c9bf464f742e6d27fe3133dd= 175996970845e 100644 --- a/drivers/net/ovpn/packet.h +++ b/drivers/net/ovpn/packet.h @@ -10,7 +10,7 @@ #ifndef _NET_OVPN_PACKET_H_ #define _NET_OVPN_PACKET_H_ =20 -/* When the OpenVPN protocol is ran in AEAD mode, use +/* When the OpenVPN protocol is run in AEAD mode, use * the OpenVPN packet ID as the AEAD nonce: * * 00000005 521c3b01 4308c041 diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index aff3e9e99b7d2dd2fa68484d9a396d43f75a6d0b..98ae7662f1e76811e625dc5f4b4= c5c884856fbd6 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -12,6 +12,8 @@ =20 #include "ovpnstruct.h" #include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "io.h" #include "main.h" #include "netlink.h" @@ -43,6 +45,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn,= u32 id) peer->vpn_addrs.ipv6 =3D in6addr_any; =20 RCU_INIT_POINTER(peer->bind, NULL); + ovpn_crypto_state_init(&peer->crypto); spin_lock_init(&peer->lock); kref_init(&peer->refcount); =20 @@ -68,6 +71,7 @@ static void ovpn_peer_release(struct ovpn_peer *peer) if (peer->sock) ovpn_socket_put(peer->sock); =20 + ovpn_crypto_state_release(&peer->crypto); ovpn_bind_reset(peer, NULL); dst_cache_destroy(&peer->dst_cache); netdev_put(peer->ovpn->dev, &peer->ovpn->dev_tracker); @@ -278,6 +282,31 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_str= uct *ovpn, return peer; } =20 +/** + * ovpn_peer_check_by_src - check that skb source is routed via peer + * @ovpn: the openvpn instance to search + * @skb: the packet to extract source address from + * @peer: the peer to check against the source address + * + * Return: true if the peer is matching or false otherwise + */ +bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, + struct ovpn_peer *peer) +{ + bool match =3D false; + + if (ovpn->mode =3D=3D OVPN_MODE_P2P) { + /* in P2P mode, no matter the destination, packets are always + * sent to the single peer listening on the other side + */ + rcu_read_lock(); + match =3D (peer =3D=3D rcu_dereference(ovpn->peer)); + rcu_read_unlock(); + } + + return match; +} + /** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 51955aa39f1aa85ce541e289c60e9635cadb9c48..754fea470d1b4787f64a931d6c6= adc24182fc16f 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -12,6 +12,8 @@ =20 #include =20 +#include "crypto.h" + /** * struct ovpn_peer - the main remote peer object * @ovpn: main openvpn instance this peer belongs to @@ -20,6 +22,7 @@ * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer + * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called @@ -37,6 +40,7 @@ struct ovpn_peer { struct in6_addr ipv6; } vpn_addrs; struct ovpn_socket *sock; + struct ovpn_crypto_state crypto; struct dst_cache dst_cache; struct ovpn_bind __rcu *bind; bool halt; @@ -79,5 +83,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovp= n_struct *ovpn, struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d); struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb); +bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, + struct ovpn_peer *peer); =20 #endif /* _NET_OVPN_OVPNPEER_H_ */ diff --git a/drivers/net/ovpn/pktid.c b/drivers/net/ovpn/pktid.c new file mode 100644 index 0000000000000000000000000000000000000000..96dc876356706eb6e2104cf8291= c1487b4441b1f --- /dev/null +++ b/drivers/net/ovpn/pktid.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#include +#include +#include +#include +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "packet.h" +#include "pktid.h" + +void ovpn_pktid_xmit_init(struct ovpn_pktid_xmit *pid) +{ + atomic64_set(&pid->seq_num, 1); +} + +void ovpn_pktid_recv_init(struct ovpn_pktid_recv *pr) +{ + memset(pr, 0, sizeof(*pr)); + spin_lock_init(&pr->lock); +} + +/* Packet replay detection. + * Allows ID backtrack of up to REPLAY_WINDOW_SIZE - 1. + */ +int ovpn_pktid_recv(struct ovpn_pktid_recv *pr, u32 pkt_id, u32 pkt_time) +{ + const unsigned long now =3D jiffies; + int ret; + + /* ID must not be zero */ + if (unlikely(pkt_id =3D=3D 0)) + return -EINVAL; + + spin_lock_bh(&pr->lock); + + /* expire backtracks at or below pr->id after PKTID_RECV_EXPIRE time */ + if (unlikely(time_after_eq(now, pr->expire))) + pr->id_floor =3D pr->id; + + /* time changed? */ + if (unlikely(pkt_time !=3D pr->time)) { + if (pkt_time > pr->time) { + /* time moved forward, accept */ + pr->base =3D 0; + pr->extent =3D 0; + pr->id =3D 0; + pr->time =3D pkt_time; + pr->id_floor =3D 0; + } else { + /* time moved backward, reject */ + ret =3D -ETIME; + goto out; + } + } + + if (likely(pkt_id =3D=3D pr->id + 1)) { + /* well-formed ID sequence (incremented by 1) */ + pr->base =3D REPLAY_INDEX(pr->base, -1); + pr->history[pr->base / 8] |=3D (1 << (pr->base % 8)); + if (pr->extent < REPLAY_WINDOW_SIZE) + ++pr->extent; + pr->id =3D pkt_id; + } else if (pkt_id > pr->id) { + /* ID jumped forward by more than one */ + const unsigned int delta =3D pkt_id - pr->id; + + if (delta < REPLAY_WINDOW_SIZE) { + unsigned int i; + + pr->base =3D REPLAY_INDEX(pr->base, -delta); + pr->history[pr->base / 8] |=3D (1 << (pr->base % 8)); + pr->extent +=3D delta; + if (pr->extent > REPLAY_WINDOW_SIZE) + pr->extent =3D REPLAY_WINDOW_SIZE; + for (i =3D 1; i < delta; ++i) { + unsigned int newb =3D REPLAY_INDEX(pr->base, i); + + pr->history[newb / 8] &=3D ~BIT(newb % 8); + } + } else { + pr->base =3D 0; + pr->extent =3D REPLAY_WINDOW_SIZE; + memset(pr->history, 0, sizeof(pr->history)); + pr->history[0] =3D 1; + } + pr->id =3D pkt_id; + } else { + /* ID backtrack */ + const unsigned int delta =3D pr->id - pkt_id; + + if (delta > pr->max_backtrack) + pr->max_backtrack =3D delta; + if (delta < pr->extent) { + if (pkt_id > pr->id_floor) { + const unsigned int ri =3D REPLAY_INDEX(pr->base, + delta); + u8 *p =3D &pr->history[ri / 8]; + const u8 mask =3D (1 << (ri % 8)); + + if (*p & mask) { + ret =3D -EINVAL; + goto out; + } + *p |=3D mask; + } else { + ret =3D -EINVAL; + goto out; + } + } else { + ret =3D -EINVAL; + goto out; + } + } + + pr->expire =3D now + PKTID_RECV_EXPIRE; + ret =3D 0; +out: + spin_unlock_bh(&pr->lock); + return ret; +} diff --git a/drivers/net/ovpn/pktid.h b/drivers/net/ovpn/pktid.h new file mode 100644 index 0000000000000000000000000000000000000000..fe02f0667e1a88a8c866fe4da4e= 5cebfba9efbcf --- /dev/null +++ b/drivers/net/ovpn/pktid.h @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + * James Yonan + */ + +#ifndef _NET_OVPN_OVPNPKTID_H_ +#define _NET_OVPN_OVPNPKTID_H_ + +#include "packet.h" + +/* If no packets received for this length of time, set a backtrack floor + * at highest received packet ID thus far. + */ +#define PKTID_RECV_EXPIRE (30 * HZ) + +/* Packet-ID state for transmitter */ +struct ovpn_pktid_xmit { + atomic64_t seq_num; +}; + +/* replay window sizing in bytes =3D 2^REPLAY_WINDOW_ORDER */ +#define REPLAY_WINDOW_ORDER 8 + +#define REPLAY_WINDOW_BYTES BIT(REPLAY_WINDOW_ORDER) +#define REPLAY_WINDOW_SIZE (REPLAY_WINDOW_BYTES * 8) +#define REPLAY_INDEX(base, i) (((base) + (i)) & (REPLAY_WINDOW_SIZE - 1)) + +/* Packet-ID state for receiver. + * Other than lock member, can be zeroed to initialize. + */ +struct ovpn_pktid_recv { + /* "sliding window" bitmask of recent packet IDs received */ + u8 history[REPLAY_WINDOW_BYTES]; + /* bit position of deque base in history */ + unsigned int base; + /* extent (in bits) of deque in history */ + unsigned int extent; + /* expiration of history in jiffies */ + unsigned long expire; + /* highest sequence number received */ + u32 id; + /* highest time stamp received */ + u32 time; + /* we will only accept backtrack IDs > id_floor */ + u32 id_floor; + unsigned int max_backtrack; + /* protects entire pktd ID state */ + spinlock_t lock; +}; + +/* Get the next packet ID for xmit */ +static inline int ovpn_pktid_xmit_next(struct ovpn_pktid_xmit *pid, u32 *p= ktid) +{ + const s64 seq_num =3D atomic64_fetch_add_unless(&pid->seq_num, 1, + 0x100000000LL); + /* when the 32bit space is over, we return an error because the packet + * ID is used to create the cipher IV and we do not want to reuse the + * same value more than once + */ + if (unlikely(seq_num =3D=3D 0x100000000LL)) + return -ERANGE; + + *pktid =3D (u32)seq_num; + + return 0; +} + +/* Write 12-byte AEAD IV to dest */ +static inline void ovpn_pktid_aead_write(const u32 pktid, + const struct ovpn_nonce_tail *nt, + unsigned char *dest) +{ + *(__force __be32 *)(dest) =3D htonl(pktid); + BUILD_BUG_ON(4 + sizeof(struct ovpn_nonce_tail) !=3D NONCE_SIZE); + memcpy(dest + 4, nt->u8, sizeof(struct ovpn_nonce_tail)); +} + +void ovpn_pktid_xmit_init(struct ovpn_pktid_xmit *pid); +void ovpn_pktid_recv_init(struct ovpn_pktid_recv *pr); + +int ovpn_pktid_recv(struct ovpn_pktid_recv *pr, u32 pkt_id, u32 pkt_time); + +#endif /* _NET_OVPN_OVPNPKTID_H_ */ diff --git a/drivers/net/ovpn/proto.h b/drivers/net/ovpn/proto.h index 69604cf26bbf82539ee5cd5a7ac9c23920f555de..32af6b8e574381fb719a1b3b9de= 3ae1071cc4846 100644 --- a/drivers/net/ovpn/proto.h +++ b/drivers/net/ovpn/proto.h @@ -72,4 +72,35 @@ static inline u32 ovpn_peer_id_from_skb(const struct sk_= buff *skb, u16 offset) return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK; } =20 +/** + * ovpn_key_id_from_skb - extract key ID from the skb head + * @skb: the packet to extract the key ID code from + * + * Note: this function assumes that the skb head was pulled enough + * to access the first byte. + * + * Return: the key ID + */ +static inline u8 ovpn_key_id_from_skb(const struct sk_buff *skb) +{ + return *skb->data & OVPN_KEY_ID_MASK; +} + +/** + * ovpn_opcode_compose - combine OP code, key ID and peer ID to wire format + * @opcode: the OP code + * @key_id: the key ID + * @peer_id: the peer ID + * + * Return: a 4 bytes integer obtained combining all input values following= the + * OpenVPN wire format. This integer can then be written to the packet hea= der. + */ +static inline u32 ovpn_opcode_compose(u8 opcode, u8 key_id, u32 peer_id) +{ + const u8 op =3D (opcode << OVPN_OPCODE_SHIFT) | + (key_id & OVPN_KEY_ID_MASK); + + return (op << 24) | (peer_id & OVPN_PEER_ID_MASK); +} + #endif /* _NET_OVPN_OVPNPROTO_H_ */ diff --git a/drivers/net/ovpn/skb.h b/drivers/net/ovpn/skb.h index e070fe6f448c0b7a9631394ebef4554f6348ef44..3f94efae20fb58772c1a4c47df5= bae8bb11ad322 100644 --- a/drivers/net/ovpn/skb.h +++ b/drivers/net/ovpn/skb.h @@ -17,8 +17,17 @@ #include #include =20 -struct ovpn_cb { +struct ovpn_cb_ctx { struct ovpn_peer *peer; + struct sk_buff *skb; + struct ovpn_crypto_key_slot *ks; + struct aead_request *req; + struct scatterlist sg[MAX_SKB_FRAGS + 2]; + unsigned int payload_offset; +}; + +struct ovpn_cb { + struct ovpn_cb_ctx *ctx; }; =20 static inline struct ovpn_cb *ovpn_skb_cb(struct sk_buff *skb) --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 5D9BF18C033 for ; Wed, 16 Oct 2024 01:04:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040647; cv=none; b=UoIZ92zRL+oEASiNkvMCHKVVoaGU3/sjxxUQ3mo85MGpnEHnbeRucyB787P6CA8JzXDNGZv4hctQPSiqvHQWUY857p8+WLhA7E6+nGXl1ZigiQvMz300AL7PfQw2p1O1Dd0USQBi1bmQ9OWmhD0ABhU/8kI7NxGucVgKaQqzi7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040647; c=relaxed/simple; bh=R0lPiBiWMxxSF4Kond6z7kwWzsBdieVwTySxOvMQvAI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eL6NFs+zUzjQt+7ThBxL8vf2c8UqGADRDZEDrwj+6TC/wfX8NESO3CzLWp4M5StgrGv3zr6luHGXbNXz8rHCJ2kfHm4n3A2o0lwwQOAMLQ2f80vsdzBZ6rPPlwceq6cNhpnIBaJVi0jbG0J1dhkTO/NagbWfTd83j3SoKQzfMGU= 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=JqsHnr55; arc=none smtp.client-ip=209.85.221.54 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="JqsHnr55" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d808ae924so892981f8f.0 for ; Tue, 15 Oct 2024 18:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040643; x=1729645443; 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=PDhD2BKid0LL9GZoUYU43ErgOsQonUdw1fUcPF+4gFE=; b=JqsHnr55x7yaWU50Ga9w8ETBxtchXJeOm6C917SVAdnKs6D6iCwHA8wx9ttcwNiToz RHjQLMI2t9Xy3tEOVEdww4jXLIgb0AHX87jQCehfB+drzW2XX4Qy8yhztfpfx67zAg3O 2xTd9FP3wYaDZqmyOZ2rZz2NoRR/1b3XBbuUAT8fKrjZygxtgyDP+P4T98qqbICpOYVZ M9cyl9o0ecXUM2JuvIxbnpBnTTOGBixWJPGYZJWhkS0Fh8ynLuaFW69fTla9QM2BFhHe wrYnqFgBdG82o+m3hjSoBYbZoUo8fc+IgY9tmagbC8VOAi4peGiSS7Vrn1x9SDL8hUYr oHJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040643; x=1729645443; 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=PDhD2BKid0LL9GZoUYU43ErgOsQonUdw1fUcPF+4gFE=; b=XUAd5wUoQM6/EATkw5piZsiU6eqJJrfbbqqRwRPk7dbdin5bc1/A51hEnINNBYXKgu X3GNlfNrAu4Z5Iy1IphUI9hF5NVMOUiJtVpxKyBOAJbUJKCWP+mNs/ANniyMseEUmook 9sGg45fUZFq+DalPfD6dr1QFp5WFJaS7/BoxpcIZ7/g3tYE67dyezy3N+6GV+VfqH0BJ 8Hsxj6QqlFJ2b/Za5I5nN3GFChNTmd4JD20z43C780zr7FA7l3njr0C0lzDbHGAsSX+9 9w7yJpT8hFCv5wyWKsqoY/Xpm4Ljmadvep0lAM3uXvrg+RytQLTQqhUzmFcDCDDxKu5x baEw== X-Forwarded-Encrypted: i=1; AJvYcCUb2UTmDgMWOeYzflLCQ2BeMicv5L1/StjqKkHCgDFJrLMyGvkQeBWvStDNxOZ7c62E1odN9StvcSPJR3A=@vger.kernel.org X-Gm-Message-State: AOJu0YwUAnDO4gl+NwGE8cLlpZE5ZGiN1ybklc3Lg0PGklWkysY38DNU FNjItO1Qt0GZcOquvJpwqD25JrDedRknEKehb3hNlg2deU6mUq1m82OSnEOA0mI= X-Google-Smtp-Source: AGHT+IHRh/IpUwdA9QMEBJdcFuV3rJm0e/WkWfK9cZcPkle08ZblgSQJCVNG3Gc/1Uv7jRalZBqMmQ== X-Received: by 2002:a5d:4e0e:0:b0:374:c56e:1d44 with SMTP id ffacd0b85a97d-37d86d59a06mr1395596f8f.48.1729040642734; Tue, 15 Oct 2024 18:04:02 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:01 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:11 +0200 Subject: [PATCH net-next v9 11/23] ovpn: store tunnel and transport statistics 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: <20241016-b4-ovpn-v9-11-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=8711; i=antonio@openvpn.net; h=from:subject:message-id; bh=R0lPiBiWMxxSF4Kond6z7kwWzsBdieVwTySxOvMQvAI=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvN8uiBfR1h1bryMr+oU+HI5HV1VyvzCGkR FoBYbLuELmJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV hzQhB/9aa4I5e9zQGxRltJus7WNInxVjhIjWvhz1ehYU5hGUpetNYIPFxr3pD2RnrxswK1SQGWP /lUcDM4oN7tvtHL+HBA/HuPBAK61XAw8b2U+hnu7/iYzW08ZM0k3cRvrKXW63iJSvcIy1GD3OwN b13R76ZQ/fUm/2ma4IZV8rUjPmLuxX9OiRTts+EkkozKAXU0pz17lDHXVI/9Dh8ap9HMsS1Ep38 Sc04qnub3d3pYNcLeyaQQkSpbAYjAfZxvidnXFw4yAd3MIq9pJeIacy7vGsvnDiXbbWwIiRJx0u baW6PJviMP9usu7Zay2tjvbbmVpPAq7VGV6ziPKVb53rHctD X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed. To be exported via netlink. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/crypto_aead.c | 2 ++ drivers/net/ovpn/io.c | 12 +++++++++++ drivers/net/ovpn/peer.c | 2 ++ drivers/net/ovpn/peer.h | 5 +++++ drivers/net/ovpn/skb.h | 1 + drivers/net/ovpn/stats.c | 21 +++++++++++++++++++ drivers/net/ovpn/stats.h | 47 ++++++++++++++++++++++++++++++++++++++= ++++ 8 files changed, 91 insertions(+) diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index ccdaeced1982c851475657860a005ff2b9dfbd13..d43fda72646bdc7644d9a878b56= da0a0e5680c98 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -17,4 +17,5 @@ ovpn-y +=3D netlink-gen.o ovpn-y +=3D peer.o ovpn-y +=3D pktid.o ovpn-y +=3D socket.o +ovpn-y +=3D stats.o ovpn-y +=3D udp.o diff --git a/drivers/net/ovpn/crypto_aead.c b/drivers/net/ovpn/crypto_aead.c index 97134ac679c67a3d44a0bf49c2ddf058fd7c0e99..6599c8550390a60d3b0fb9c144b= eb8c7871bc320 100644 --- a/drivers/net/ovpn/crypto_aead.c +++ b/drivers/net/ovpn/crypto_aead.c @@ -128,6 +128,7 @@ int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ov= pn_crypto_key_slot *ks, aead_request_set_crypt(req, sg, sg, skb->len - head_size, iv); aead_request_set_ad(req, OVPN_OP_SIZE_V2 + NONCE_WIRE_SIZE); =20 + ovpn_skb_cb(skb)->ctx->orig_len =3D skb->len; ovpn_skb_cb(skb)->ctx->peer =3D peer; ovpn_skb_cb(skb)->ctx->req =3D req; ovpn_skb_cb(skb)->ctx->ks =3D ks; @@ -216,6 +217,7 @@ int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ov= pn_crypto_key_slot *ks, =20 aead_request_set_ad(req, NONCE_WIRE_SIZE + OVPN_OP_SIZE_V2); =20 + ovpn_skb_cb(skb)->ctx->orig_len =3D skb->len; ovpn_skb_cb(skb)->ctx->payload_offset =3D payload_offset; ovpn_skb_cb(skb)->ctx->peer =3D peer; ovpn_skb_cb(skb)->ctx->req =3D req; diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index e9d49d10cf4e9b755d7253140b1baf55c49e8144..a770ef956792585d9b6bc779db2= d3a353b349389 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include "ovpnstruct.h" #include "peer.h" @@ -21,6 +22,7 @@ #include "crypto_aead.h" #include "netlink.h" #include "proto.h" +#include "socket.h" #include "udp.h" #include "skb.h" #include "socket.h" @@ -68,6 +70,7 @@ void ovpn_decrypt_post(void *data, int ret) unsigned int payload_offset =3D 0; struct ovpn_peer *peer =3D NULL; struct sk_buff *skb =3D data; + unsigned int orig_len =3D 0; __be16 proto; __be32 *pid; =20 @@ -82,6 +85,7 @@ void ovpn_decrypt_post(void *data, int ret) payload_offset =3D ovpn_skb_cb(skb)->ctx->payload_offset; ks =3D ovpn_skb_cb(skb)->ctx->ks; peer =3D ovpn_skb_cb(skb)->ctx->peer; + orig_len =3D ovpn_skb_cb(skb)->ctx->orig_len; =20 aead_request_free(ovpn_skb_cb(skb)->ctx->req); kfree(ovpn_skb_cb(skb)->ctx); @@ -135,6 +139,10 @@ void ovpn_decrypt_post(void *data, int ret) goto drop; } =20 + /* increment RX stats */ + ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len); + ovpn_peer_stats_increment_rx(&peer->link_stats, orig_len); + ovpn_netdev_write(peer, skb); /* skb is passed to upper layer - don't free it */ skb =3D NULL; @@ -173,6 +181,7 @@ void ovpn_encrypt_post(void *data, int ret) { struct ovpn_peer *peer =3D NULL; struct sk_buff *skb =3D data; + unsigned int orig_len =3D 0; =20 /* encryption is happening asynchronously. This function will be * called later by the crypto callback with a proper return value @@ -183,6 +192,7 @@ void ovpn_encrypt_post(void *data, int ret) /* crypto is done, cleanup skb CB and its members */ if (likely(ovpn_skb_cb(skb)->ctx)) { peer =3D ovpn_skb_cb(skb)->ctx->peer; + orig_len =3D ovpn_skb_cb(skb)->ctx->orig_len; =20 ovpn_crypto_key_slot_put(ovpn_skb_cb(skb)->ctx->ks); aead_request_free(ovpn_skb_cb(skb)->ctx->req); @@ -194,6 +204,8 @@ void ovpn_encrypt_post(void *data, int ret) goto err; =20 skb_mark_not_on_list(skb); + ovpn_peer_stats_increment_tx(&peer->link_stats, skb->len); + ovpn_peer_stats_increment_tx(&peer->vpn_stats, orig_len); =20 switch (peer->sock->sock->sk->sk_protocol) { case IPPROTO_UDP: diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 98ae7662f1e76811e625dc5f4b4c5c884856fbd6..5025bfb759d6a5f31e3f2ec094f= e561fbdb9f451 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -48,6 +48,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn,= u32 id) ovpn_crypto_state_init(&peer->crypto); spin_lock_init(&peer->lock); kref_init(&peer->refcount); + ovpn_peer_stats_init(&peer->vpn_stats); + ovpn_peer_stats_init(&peer->link_stats); =20 ret =3D dst_cache_init(&peer->dst_cache, GFP_KERNEL); if (ret < 0) { diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 754fea470d1b4787f64a931d6c6adc24182fc16f..eb1e31e854fbfff25d07fba8026= 789e41a76c113 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -13,6 +13,7 @@ #include =20 #include "crypto.h" +#include "stats.h" =20 /** * struct ovpn_peer - the main remote peer object @@ -26,6 +27,8 @@ * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called + * @vpn_stats: per-peer in-VPN TX/RX stays + * @link_stats: per-peer link/transport TX/RX stats * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) * @lock: protects binding to peer (bind) * @refcount: reference counter @@ -44,6 +47,8 @@ struct ovpn_peer { struct dst_cache dst_cache; struct ovpn_bind __rcu *bind; bool halt; + struct ovpn_peer_stats vpn_stats; + struct ovpn_peer_stats link_stats; enum ovpn_del_peer_reason delete_reason; spinlock_t lock; /* protects bind */ struct kref refcount; diff --git a/drivers/net/ovpn/skb.h b/drivers/net/ovpn/skb.h index 3f94efae20fb58772c1a4c47df5bae8bb11ad322..3d9f38b081a6531456a8fba3b15= ed3e7cae4240c 100644 --- a/drivers/net/ovpn/skb.h +++ b/drivers/net/ovpn/skb.h @@ -23,6 +23,7 @@ struct ovpn_cb_ctx { struct ovpn_crypto_key_slot *ks; struct aead_request *req; struct scatterlist sg[MAX_SKB_FRAGS + 2]; + size_t orig_len; unsigned int payload_offset; }; =20 diff --git a/drivers/net/ovpn/stats.c b/drivers/net/ovpn/stats.c new file mode 100644 index 0000000000000000000000000000000000000000..a383842c3449b73694c318837b0= b92eb9afaec22 --- /dev/null +++ b/drivers/net/ovpn/stats.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + */ + +#include + +#include "stats.h" + +void ovpn_peer_stats_init(struct ovpn_peer_stats *ps) +{ + atomic64_set(&ps->rx.bytes, 0); + atomic64_set(&ps->rx.packets, 0); + + atomic64_set(&ps->tx.bytes, 0); + atomic64_set(&ps->tx.packets, 0); +} diff --git a/drivers/net/ovpn/stats.h b/drivers/net/ovpn/stats.h new file mode 100644 index 0000000000000000000000000000000000000000..868f49d25eaa8fef04a02a61c36= 3d95f9c9ef80a --- /dev/null +++ b/drivers/net/ovpn/stats.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: James Yonan + * Antonio Quartulli + * Lev Stipakov + */ + +#ifndef _NET_OVPN_OVPNSTATS_H_ +#define _NET_OVPN_OVPNSTATS_H_ + +/* one stat */ +struct ovpn_peer_stat { + atomic64_t bytes; + atomic64_t packets; +}; + +/* rx and tx stats combined */ +struct ovpn_peer_stats { + struct ovpn_peer_stat rx; + struct ovpn_peer_stat tx; +}; + +void ovpn_peer_stats_init(struct ovpn_peer_stats *ps); + +static inline void ovpn_peer_stats_increment(struct ovpn_peer_stat *stat, + const unsigned int n) +{ + atomic64_add(n, &stat->bytes); + atomic64_inc(&stat->packets); +} + +static inline void ovpn_peer_stats_increment_rx(struct ovpn_peer_stats *st= ats, + const unsigned int n) +{ + ovpn_peer_stats_increment(&stats->rx, n); +} + +static inline void ovpn_peer_stats_increment_tx(struct ovpn_peer_stats *st= ats, + const unsigned int n) +{ + ovpn_peer_stats_increment(&stats->tx, n); +} + +#endif /* _NET_OVPN_OVPNSTATS_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 94E5418C340 for ; Wed, 16 Oct 2024 01:04:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040649; cv=none; b=c51FOeEjCfK2XJ1m9gjB4yydbsLEvy/3O/EJThSK/UaEFG7sDPSZ3+sAxz5HnpugdR+5n46BAV462mLespymEc9KtFpNMbXCuePvOPUQFiHymhKpLhC70egUBPKPT4MzSHYOYa2Io/rXxO0OjmpMd8XrHF+DpnMNJHPMzsCuPGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040649; c=relaxed/simple; bh=k8A7EZH2DRFqfEQjbbjGFUcYeK3E0Re0l9P1Jj3pxlI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=N8mD2wHl4ejWhaF909u6DqWipggn8t2RXpAdIyUKgfqc8L/3WuzFa46/82nM5UjODZ+0eRQSI7YnfTP7jtZlwocqXD4D9Etc4hDk5cPNgkfY3poHfT4w1v7BuFiaVEkf6f2AfQt/c4uw6zX+I76UDCGb4qmyVFvvX8qS9N44Stg= 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=HLFtqczi; arc=none smtp.client-ip=209.85.128.48 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="HLFtqczi" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-43144f656f0so9988425e9.0 for ; Tue, 15 Oct 2024 18:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040644; x=1729645444; 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=nrOTSDfeot7djdxBl4MPie517OafOgmwDobhh2/KTf0=; b=HLFtqczieCmXSAWr4Tdpa5oPJz3ODhseA5JDJmIevoDMkUAFKKb8p8Uynm08rHHPX3 /PMgPftw6pAvd46cug3/PIqBKaJ9gcsjer6SEj/PtbGv6T/QF/7Zas++Cx6x+h5lh4Q+ snW3D/MZti7L6OZ/CHVo6LvIBK8UUn0b04JLbMLYyMK7vLkS3vGlRUVYV1iinJUoUIVE qag1M2RVyfR69LfiPnbkFO/m4tQ+1JODQbw/UyqmdRErXXrPvnlkj62EF4TYfVZKbL49 BfwWrAHxAWiDsWPCtBRi7v/HEeUWmv8JlcEGvbTgBHBOiJJWFw7C9tJqNfDx4EOL/zNA m2bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040644; x=1729645444; 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=nrOTSDfeot7djdxBl4MPie517OafOgmwDobhh2/KTf0=; b=atSCBq31LvKDuOirBLGTCgaB+szKtC0bGDYSVoFFJWCEfwcEG+f0Pz5AvVu+syuxYb tEYjT4CyD3To0f7B28MMENPphODl9Y0n+IOlvuIzC9z1t6rXpkjivDVbnlOncOW7t2o+ ANDZmmMm+SkEHAOEfbV9yS4Yue9c1NxejVKCe7sl6f667aWMk1md1yZ9kT/Ejk6R65Op xIUwG8fZzUQiOjbD33BvEkw7XDCfExyqtzfr1C7IFT8GrRhAF+JI7GtkBzdBAzo/GREX QjsFfl372+w/C5YSzThjbSEaSwRBsfUVgtlop1YUvGshj54+QWbS3imuqRsqUk6paYjN VjRg== X-Forwarded-Encrypted: i=1; AJvYcCXwRRRoexnRx+MgC0eleBWV8MVtZURiAP1/S1kcqZCHyW7CB1MBF5PDeH7TvvafUlA645jL9cNBHDj4LFI=@vger.kernel.org X-Gm-Message-State: AOJu0YxtSHLLF7ZLNMnhC6AGDCE1LvWagucEPnv1IXy0+lR3tgTLbwrr teXg2e0fAPjeptTlGOLAaE8lRWIwwB6MxN9+KFjs6JOOG4VCGL4wYaBpdoLqOew= X-Google-Smtp-Source: AGHT+IEqlR3kgQpA8sQ9RGQbO/BvxNEjKY0W6VePwIs/qpwhX0pVDC78c2UwpSRLQBTv2IonRAu1gA== X-Received: by 2002:a05:600c:1e18:b0:426:5416:67e0 with SMTP id 5b1f17b1804b1-4311df5c1c8mr140083755e9.31.1729040643814; Tue, 15 Oct 2024 18:04:03 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:03 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:12 +0200 Subject: [PATCH net-next v9 12/23] ovpn: implement TCP transport 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: <20241016-b4-ovpn-v9-12-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=23542; i=antonio@openvpn.net; h=from:subject:message-id; bh=k8A7EZH2DRFqfEQjbbjGFUcYeK3E0Re0l9P1Jj3pxlI=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDvdF2ay9fEjfHGOmog2AJAH/p1374R+ZUfR 4KBNSUvEoqJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q7wAKCRALcOU6oDjV h/TzB/9lelMAV2ud1ENdXkdmj4ibdlHQkdwOuttZXTeqBv7i/z5XmWEU7VNRe4RbISMB6Yl4f0v 9CjCLkJvWWounzqf8W0o5I0CcHlenSQ1rqZhkSUmhNwcVaLsMV2dS5rp5nW/H5zSmW2p4aczlxf tuFbkmo1IyX9nHkZ9P6Xa0nB8o8XM1nHLMyB31Con+W0alXWR9wBMDfwSmnHJm5faW4nCIjUuBX wFp2lhnvF1ev7B8CNWq3YclUolO10S3StEpjvy4GDDIx9ujSsx5lIUH0QWCG9NVF2v7lPQq6nan vcMo4Ocl6qN+f8pTU3jFYtia8ZAJkbpklbkRlhUCJPpjUydf X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C With this change ovpn is allowed to communicate to peers also via TCP. Parsing of incoming messages is implemented through the strparser API. Signed-off-by: Antonio Quartulli --- drivers/net/Kconfig | 1 + drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 4 + drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/peer.h | 37 ++++ drivers/net/ovpn/socket.c | 44 +++- drivers/net/ovpn/socket.h | 9 +- drivers/net/ovpn/tcp.c | 505 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/tcp.h | 44 ++++ 9 files changed, 642 insertions(+), 6 deletions(-) diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index d54ddf0f0e0979d75120805930da82f48b865ea4..76da58b93ffee5ed13767c9f8e9= 5e80256a9a5e5 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -118,6 +118,7 @@ config WIREGUARD_DEBUG config OVPN tristate "OpenVPN data channel offload" depends on NET && INET + select STREAM_PARSER select NET_UDP_TUNNEL select DST_CACHE select CRYPTO diff --git a/drivers/net/ovpn/Makefile b/drivers/net/ovpn/Makefile index d43fda72646bdc7644d9a878b56da0a0e5680c98..f4d4bd87c851c8dd5b81e357315= c4b22de4bd092 100644 --- a/drivers/net/ovpn/Makefile +++ b/drivers/net/ovpn/Makefile @@ -18,4 +18,5 @@ ovpn-y +=3D peer.o ovpn-y +=3D pktid.o ovpn-y +=3D socket.o ovpn-y +=3D stats.o +ovpn-y +=3D tcp.o ovpn-y +=3D udp.o diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index a770ef956792585d9b6bc779db2d3a353b349389..1c0c553b4182762568ce9806a96= 70e3004076137 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -23,6 +23,7 @@ #include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" #include "skb.h" #include "socket.h" @@ -211,6 +212,9 @@ void ovpn_encrypt_post(void *data, int ret) case IPPROTO_UDP: ovpn_udp_send_skb(peer->ovpn, peer, skb); break; + case IPPROTO_TCP: + ovpn_tcp_send_skb(peer, skb); + break; default: /* no transport configured yet */ goto err; diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index 73348765a8cf24321aa6be78e75f607d6dbffb1d..0488e395eb27d3dba1efc8ff39c= 023e0ac4a38dd 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -22,6 +22,7 @@ #include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" =20 /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)" @@ -237,6 +238,8 @@ static int __init ovpn_init(void) goto unreg_rtnl; } =20 + ovpn_tcp_init(); + return 0; =20 unreg_rtnl: diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index eb1e31e854fbfff25d07fba8026789e41a76c113..2b7fa9510e362ef3646157bb0d3= 61bab19ddaa99 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -11,6 +11,7 @@ #define _NET_OVPN_OVPNPEER_H_ =20 #include +#include =20 #include "crypto.h" #include "stats.h" @@ -23,6 +24,18 @@ * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer + * @tcp: keeps track of TCP specific state + * @tcp.strp: stream parser context (TCP only) + * @tcp.tx_work: work for deferring outgoing packet processing (TCP only) + * @tcp.user_queue: received packets that have to go to userspace (TCP onl= y) + * @tcp.tx_in_progress: true if TX is already ongoing (TCP only) + * @tcp.out_msg.skb: packet scheduled for sending (TCP only) + * @tcp.out_msg.offset: offset where next send should start (TCP only) + * @tcp.out_msg.len: remaining data to send within packet (TCP only) + * @tcp.sk_cb.sk_data_ready: pointer to original cb (TCP only) + * @tcp.sk_cb.sk_write_space: pointer to original cb (TCP only) + * @tcp.sk_cb.prot: pointer to original prot object (TCP only) + * @tcp.sk_cb.ops: pointer to the original prot_ops object (TCP only) * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding @@ -43,6 +56,30 @@ struct ovpn_peer { struct in6_addr ipv6; } vpn_addrs; struct ovpn_socket *sock; + + /* state of the TCP reading. Needed to keep track of how much of a + * single packet has already been read from the stream and how much is + * missing + */ + struct { + struct strparser strp; + struct work_struct tx_work; + struct sk_buff_head user_queue; + bool tx_in_progress; + + struct { + struct sk_buff *skb; + int offset; + int len; + } out_msg; + + struct { + void (*sk_data_ready)(struct sock *sk); + void (*sk_write_space)(struct sock *sk); + struct proto *prot; + const struct proto_ops *ops; + } sk_cb; + } tcp; struct ovpn_crypto_state crypto; struct dst_cache dst_cache; struct ovpn_bind __rcu *bind; diff --git a/drivers/net/ovpn/socket.c b/drivers/net/ovpn/socket.c index 964b566de69f4132806a969a455cec7f6059a0bd..a0c2a02ff20541ecef48b6b0ecc= 40d558d0e3e7b 100644 --- a/drivers/net/ovpn/socket.c +++ b/drivers/net/ovpn/socket.c @@ -15,6 +15,7 @@ #include "io.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" =20 static void ovpn_socket_detach(struct socket *sock) @@ -24,10 +25,26 @@ static void ovpn_socket_detach(struct socket *sock) =20 if (sock->sk->sk_protocol =3D=3D IPPROTO_UDP) ovpn_udp_socket_detach(sock); + else if (sock->sk->sk_protocol =3D=3D IPPROTO_TCP) + ovpn_tcp_socket_detach(sock); =20 sockfd_put(sock); } =20 +static void ovpn_socket_release_work(struct work_struct *work) +{ + struct ovpn_socket *sock =3D container_of(work, struct ovpn_socket, work); + + ovpn_socket_detach(sock->sock); + kfree_rcu(sock, rcu); +} + +static void ovpn_socket_schedule_release(struct ovpn_socket *sock) +{ + INIT_WORK(&sock->work, ovpn_socket_release_work); + schedule_work(&sock->work); +} + /** * ovpn_socket_release_kref - kref_put callback * @kref: the kref object @@ -37,8 +54,7 @@ void ovpn_socket_release_kref(struct kref *kref) struct ovpn_socket *sock =3D container_of(kref, struct ovpn_socket, refcount); =20 - ovpn_socket_detach(sock->sock); - kfree_rcu(sock, rcu); + ovpn_socket_schedule_release(sock); } =20 static bool ovpn_socket_hold(struct ovpn_socket *sock) @@ -70,6 +86,8 @@ static int ovpn_socket_attach(struct socket *sock, struct= ovpn_peer *peer) =20 if (sock->sk->sk_protocol =3D=3D IPPROTO_UDP) ret =3D ovpn_udp_socket_attach(sock, peer->ovpn); + else if (sock->sk->sk_protocol =3D=3D IPPROTO_TCP) + ret =3D ovpn_tcp_socket_attach(sock, peer); =20 return ret; } @@ -131,14 +149,30 @@ struct ovpn_socket *ovpn_socket_new(struct socket *so= ck, struct ovpn_peer *peer) } =20 ovpn_sock =3D kzalloc(sizeof(*ovpn_sock), GFP_KERNEL); - if (!ovpn_sock) - return ERR_PTR(-ENOMEM); + if (!ovpn_sock) { + ret =3D -ENOMEM; + goto err; + } =20 - ovpn_sock->ovpn =3D peer->ovpn; ovpn_sock->sock =3D sock; kref_init(&ovpn_sock->refcount); =20 + /* TCP sockets are per-peer, therefore they are linked to their unique + * peer + */ + if (sock->sk->sk_protocol =3D=3D IPPROTO_TCP) { + ovpn_sock->peer =3D peer; + } else { + /* in UDP we only link the ovpn instance since the socket is + * shared among multiple peers + */ + ovpn_sock->ovpn =3D peer->ovpn; + } + rcu_assign_sk_user_data(sock->sk, ovpn_sock); =20 return ovpn_sock; +err: + ovpn_socket_detach(sock); + return ERR_PTR(ret); } diff --git a/drivers/net/ovpn/socket.h b/drivers/net/ovpn/socket.h index 5ad9c5073b085482da95ee8ebf40acf20bf2e4b3..bc22fff453ad8726f647a47f98f= fc2219fba7b82 100644 --- a/drivers/net/ovpn/socket.h +++ b/drivers/net/ovpn/socket.h @@ -20,14 +20,21 @@ struct ovpn_peer; /** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) + * @peer: unique peer transmitting over this socket (TCP only) * @sock: the low level sock object * @refcount: amount of contexts currently referencing this object + * @work: member used to schedule release routine (it may block) * @rcu: member used to schedule RCU destructor callback */ struct ovpn_socket { - struct ovpn_struct *ovpn; + union { + struct ovpn_struct *ovpn; + struct ovpn_peer *peer; + }; + struct socket *sock; struct kref refcount; + struct work_struct work; struct rcu_head rcu; }; =20 diff --git a/drivers/net/ovpn/tcp.c b/drivers/net/ovpn/tcp.c new file mode 100644 index 0000000000000000000000000000000000000000..428142b6154e51aa71054063836= 2e04f7da0b428 --- /dev/null +++ b/drivers/net/ovpn/tcp.c @@ -0,0 +1,505 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#include +#include +#include +#include +#include +#include + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "packet.h" +#include "peer.h" +#include "proto.h" +#include "skb.h" +#include "tcp.h" + +static struct proto ovpn_tcp_prot __ro_after_init; +static struct proto_ops ovpn_tcp_ops __ro_after_init; +static struct proto ovpn_tcp6_prot; +static struct proto_ops ovpn_tcp6_ops; +static DEFINE_MUTEX(tcp6_prot_mutex); + +static int ovpn_tcp_parse(struct strparser *strp, struct sk_buff *skb) +{ + struct strp_msg *rxm =3D strp_msg(skb); + __be16 blen; + u16 len; + int err; + + /* when packets are written to the TCP stream, they are prepended with + * two bytes indicating the actual packet size. + * Here we read those two bytes and move the skb data pointer to the + * beginning of the packet + */ + + if (skb->len < rxm->offset + 2) + return 0; + + err =3D skb_copy_bits(skb, rxm->offset, &blen, sizeof(blen)); + if (err < 0) + return err; + + len =3D be16_to_cpu(blen); + if (len < 2) + return -EINVAL; + + return len + 2; +} + +/* queue skb for sending to userspace via recvmsg on the socket */ +static void ovpn_tcp_to_userspace(struct ovpn_socket *sock, struct sk_buff= *skb) +{ + struct sock *sk =3D sock->sock->sk; + + skb_set_owner_r(skb, sk); + memset(skb->cb, 0, sizeof(skb->cb)); + skb_queue_tail(&sock->peer->tcp.user_queue, skb); + sock->peer->tcp.sk_cb.sk_data_ready(sk); +} + +static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb) +{ + struct ovpn_peer *peer =3D container_of(strp, struct ovpn_peer, tcp.strp); + struct strp_msg *msg =3D strp_msg(skb); + size_t pkt_len =3D msg->full_len - 2; + size_t off =3D msg->offset + 2; + + /* ensure skb->data points to the beginning of the openvpn packet */ + if (!pskb_pull(skb, off)) { + net_warn_ratelimited("%s: packet too small\n", + peer->ovpn->dev->name); + goto err; + } + + /* strparser does not trim the skb for us, therefore we do it now */ + if (pskb_trim(skb, pkt_len) !=3D 0) { + net_warn_ratelimited("%s: trimming skb failed\n", + peer->ovpn->dev->name); + goto err; + } + + /* we need the first byte of data to be accessible + * to extract the opcode and the key ID later on + */ + if (!pskb_may_pull(skb, 1)) { + net_warn_ratelimited("%s: packet too small to fetch opcode\n", + peer->ovpn->dev->name); + goto err; + } + + /* DATA_V2 packets are handled in kernel, the rest goes to user space */ + if (likely(ovpn_opcode_from_skb(skb, 0) =3D=3D OVPN_DATA_V2)) { + /* hold reference to peer as required by ovpn_recv(). + * + * NOTE: in this context we should already be holding a + * reference to this peer, therefore ovpn_peer_hold() is + * not expected to fail + */ + if (WARN_ON(!ovpn_peer_hold(peer))) + goto err; + + ovpn_recv(peer, skb); + } else { + /* The packet size header must be there when sending the packet + * to userspace, therefore we put it back + */ + skb_push(skb, 2); + ovpn_tcp_to_userspace(peer->sock, skb); + } + + return; +err: + netdev_err(peer->ovpn->dev, + "cannot process incoming TCP data for peer %u\n", peer->id); + dev_core_stats_rx_dropped_inc(peer->ovpn->dev); + kfree_skb(skb); + ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_TRANSPORT_ERROR); +} + +static int ovpn_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t le= n, + int flags, int *addr_len) +{ + int err =3D 0, off, copied =3D 0, ret; + struct ovpn_socket *sock; + struct ovpn_peer *peer; + struct sk_buff *skb; + + rcu_read_lock(); + sock =3D rcu_dereference_sk_user_data(sk); + if (!sock || !sock->peer) { + rcu_read_unlock(); + return -EBADF; + } + /* we take a reference to the peer linked to this TCP socket, because + * in turn the peer holds a reference to the socket itself. + * By doing so we also ensure that the peer stays alive along with + * the socket while executing this function + */ + ovpn_peer_hold(sock->peer); + peer =3D sock->peer; + rcu_read_unlock(); + + skb =3D __skb_recv_datagram(sk, &peer->tcp.user_queue, flags, &off, &err); + if (!skb) { + if (err =3D=3D -EAGAIN && sk->sk_shutdown & RCV_SHUTDOWN) { + ret =3D 0; + goto out; + } + ret =3D err; + goto out; + } + + copied =3D len; + if (copied > skb->len) + copied =3D skb->len; + else if (copied < skb->len) + msg->msg_flags |=3D MSG_TRUNC; + + err =3D skb_copy_datagram_msg(skb, 0, msg, copied); + if (unlikely(err)) { + kfree_skb(skb); + ret =3D err; + goto out; + } + + if (flags & MSG_TRUNC) + copied =3D skb->len; + kfree_skb(skb); + ret =3D copied; +out: + ovpn_peer_put(peer); + return ret; +} + +void ovpn_tcp_socket_detach(struct socket *sock) +{ + struct ovpn_socket *ovpn_sock; + struct ovpn_peer *peer; + + if (!sock) + return; + + rcu_read_lock(); + ovpn_sock =3D rcu_dereference_sk_user_data(sock->sk); + + if (!ovpn_sock->peer) { + rcu_read_unlock(); + return; + } + + peer =3D ovpn_sock->peer; + strp_stop(&peer->tcp.strp); + + skb_queue_purge(&peer->tcp.user_queue); + + /* restore CBs that were saved in ovpn_sock_set_tcp_cb() */ + sock->sk->sk_data_ready =3D peer->tcp.sk_cb.sk_data_ready; + sock->sk->sk_write_space =3D peer->tcp.sk_cb.sk_write_space; + sock->sk->sk_prot =3D peer->tcp.sk_cb.prot; + sock->sk->sk_socket->ops =3D peer->tcp.sk_cb.ops; + rcu_assign_sk_user_data(sock->sk, NULL); + + rcu_read_unlock(); + + /* cancel any ongoing work. Done after removing the CBs so that these + * workers cannot be re-armed + */ + cancel_work_sync(&peer->tcp.tx_work); + strp_done(&peer->tcp.strp); +} + +static void ovpn_tcp_send_sock(struct ovpn_peer *peer) +{ + struct sk_buff *skb =3D peer->tcp.out_msg.skb; + + if (!skb) + return; + + if (peer->tcp.tx_in_progress) + return; + + peer->tcp.tx_in_progress =3D true; + + do { + int ret =3D skb_send_sock_locked(peer->sock->sock->sk, skb, + peer->tcp.out_msg.offset, + peer->tcp.out_msg.len); + if (unlikely(ret < 0)) { + if (ret =3D=3D -EAGAIN) + goto out; + + net_warn_ratelimited("%s: TCP error to peer %u: %d\n", + peer->ovpn->dev->name, peer->id, + ret); + + /* in case of TCP error we can't recover the VPN + * stream therefore we abort the connection + */ + ovpn_peer_del(peer, + OVPN_DEL_PEER_REASON_TRANSPORT_ERROR); + break; + } + + peer->tcp.out_msg.len -=3D ret; + peer->tcp.out_msg.offset +=3D ret; + } while (peer->tcp.out_msg.len > 0); + + if (!peer->tcp.out_msg.len) + dev_sw_netstats_tx_add(peer->ovpn->dev, 1, skb->len); + + kfree_skb(peer->tcp.out_msg.skb); + peer->tcp.out_msg.skb =3D NULL; + peer->tcp.out_msg.len =3D 0; + peer->tcp.out_msg.offset =3D 0; + +out: + peer->tcp.tx_in_progress =3D false; +} + +static void ovpn_tcp_tx_work(struct work_struct *work) +{ + struct ovpn_peer *peer; + + peer =3D container_of(work, struct ovpn_peer, tcp.tx_work); + + lock_sock(peer->sock->sock->sk); + ovpn_tcp_send_sock(peer); + release_sock(peer->sock->sock->sk); +} + +void ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sk_buff *skb) +{ + if (peer->tcp.out_msg.skb) + return; + + peer->tcp.out_msg.skb =3D skb; + peer->tcp.out_msg.len =3D skb->len; + peer->tcp.out_msg.offset =3D 0; + + ovpn_tcp_send_sock(peer); +} + +static int ovpn_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t si= ze) +{ + struct ovpn_socket *sock; + int ret, linear =3D PAGE_SIZE; + struct ovpn_peer *peer; + struct sk_buff *skb; + + rcu_read_lock(); + sock =3D rcu_dereference_sk_user_data(sk); + peer =3D sock->peer; + if (unlikely(!ovpn_peer_hold(peer))) { + rcu_read_unlock(); + return -EIO; + } + rcu_read_unlock(); + + if (msg->msg_flags & ~MSG_DONTWAIT) { + ret =3D -EOPNOTSUPP; + goto peer_free; + } + + lock_sock(sk); + + if (peer->tcp.out_msg.skb) { + ret =3D -EAGAIN; + goto unlock; + } + + if (size < linear) + linear =3D size; + + skb =3D sock_alloc_send_pskb(sk, linear, size - linear, + msg->msg_flags & MSG_DONTWAIT, &ret, 0); + if (!skb) { + net_err_ratelimited("%s: skb alloc failed: %d\n", + sock->peer->ovpn->dev->name, ret); + goto unlock; + } + + skb_put(skb, linear); + skb->len =3D size; + skb->data_len =3D size - linear; + + ret =3D skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); + if (ret) { + kfree_skb(skb); + net_err_ratelimited("%s: skb copy from iter failed: %d\n", + sock->peer->ovpn->dev->name, ret); + goto unlock; + } + + ovpn_tcp_send_sock_skb(sock->peer, skb); + ret =3D size; +unlock: + release_sock(sk); +peer_free: + ovpn_peer_put(peer); + return ret; +} + +static void ovpn_tcp_data_ready(struct sock *sk) +{ + struct ovpn_socket *sock; + + trace_sk_data_ready(sk); + + rcu_read_lock(); + sock =3D rcu_dereference_sk_user_data(sk); + strp_data_ready(&sock->peer->tcp.strp); + rcu_read_unlock(); +} + +static void ovpn_tcp_write_space(struct sock *sk) +{ + struct ovpn_socket *sock; + + rcu_read_lock(); + sock =3D rcu_dereference_sk_user_data(sk); + schedule_work(&sock->peer->tcp.tx_work); + sock->peer->tcp.sk_cb.sk_write_space(sk); + rcu_read_unlock(); +} + +static void ovpn_tcp_build_protos(struct proto *new_prot, + struct proto_ops *new_ops, + const struct proto *orig_prot, + const struct proto_ops *orig_ops); + +/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach(struct socket *sock, struct ovpn_peer *peer) +{ + struct strp_callbacks cb =3D { + .rcv_msg =3D ovpn_tcp_rcv, + .parse_msg =3D ovpn_tcp_parse, + }; + int ret; + + /* make sure no pre-existing encapsulation handler exists */ + if (sock->sk->sk_user_data) + return -EBUSY; + + /* sanity check */ + if (sock->sk->sk_protocol !=3D IPPROTO_TCP) { + netdev_err(peer->ovpn->dev, + "provided socket is not TCP as expected\n"); + return -EINVAL; + } + + /* only a fully connected socket are expected. Connection should be + * handled in userspace + */ + if (sock->sk->sk_state !=3D TCP_ESTABLISHED) { + netdev_err(peer->ovpn->dev, + "provided TCP socket is not in ESTABLISHED state: %d\n", + sock->sk->sk_state); + return -EINVAL; + } + + lock_sock(sock->sk); + + ret =3D strp_init(&peer->tcp.strp, sock->sk, &cb); + if (ret < 0) { + DEBUG_NET_WARN_ON_ONCE(1); + release_sock(sock->sk); + return ret; + } + + INIT_WORK(&peer->tcp.tx_work, ovpn_tcp_tx_work); + __sk_dst_reset(sock->sk); + strp_check_rcv(&peer->tcp.strp); + skb_queue_head_init(&peer->tcp.user_queue); + + /* save current CBs so that they can be restored upon socket release */ + peer->tcp.sk_cb.sk_data_ready =3D sock->sk->sk_data_ready; + peer->tcp.sk_cb.sk_write_space =3D sock->sk->sk_write_space; + peer->tcp.sk_cb.prot =3D sock->sk->sk_prot; + peer->tcp.sk_cb.ops =3D sock->sk->sk_socket->ops; + + /* assign our static CBs and prot/ops */ + sock->sk->sk_data_ready =3D ovpn_tcp_data_ready; + sock->sk->sk_write_space =3D ovpn_tcp_write_space; + + if (sock->sk->sk_family =3D=3D AF_INET) { + sock->sk->sk_prot =3D &ovpn_tcp_prot; + sock->sk->sk_socket->ops =3D &ovpn_tcp_ops; + } else { + mutex_lock(&tcp6_prot_mutex); + if (!ovpn_tcp6_prot.recvmsg) + ovpn_tcp_build_protos(&ovpn_tcp6_prot, &ovpn_tcp6_ops, + sock->sk->sk_prot, + sock->sk->sk_socket->ops); + mutex_unlock(&tcp6_prot_mutex); + + sock->sk->sk_prot =3D &ovpn_tcp6_prot; + sock->sk->sk_socket->ops =3D &ovpn_tcp6_ops; + } + + /* avoid using task_frag */ + sock->sk->sk_allocation =3D GFP_ATOMIC; + sock->sk->sk_use_task_frag =3D false; + + release_sock(sock->sk); + return 0; +} + +static void ovpn_tcp_close(struct sock *sk, long timeout) +{ + struct ovpn_socket *sock; + + rcu_read_lock(); + sock =3D rcu_dereference_sk_user_data(sk); + + strp_stop(&sock->peer->tcp.strp); + barrier(); + + tcp_close(sk, timeout); + + ovpn_peer_del(sock->peer, OVPN_DEL_PEER_REASON_TRANSPORT_ERROR); + rcu_read_unlock(); +} + +static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock, + poll_table *wait) +{ + __poll_t mask =3D datagram_poll(file, sock, wait); + struct ovpn_socket *ovpn_sock; + + rcu_read_lock(); + ovpn_sock =3D rcu_dereference_sk_user_data(sock->sk); + if (!skb_queue_empty(&ovpn_sock->peer->tcp.user_queue)) + mask |=3D EPOLLIN | EPOLLRDNORM; + rcu_read_unlock(); + + return mask; +} + +static void ovpn_tcp_build_protos(struct proto *new_prot, + struct proto_ops *new_ops, + const struct proto *orig_prot, + const struct proto_ops *orig_ops) +{ + memcpy(new_prot, orig_prot, sizeof(*new_prot)); + memcpy(new_ops, orig_ops, sizeof(*new_ops)); + new_prot->recvmsg =3D ovpn_tcp_recvmsg; + new_prot->sendmsg =3D ovpn_tcp_sendmsg; + new_prot->close =3D ovpn_tcp_close; + new_ops->poll =3D ovpn_tcp_poll; +} + +/* Initialize TCP static objects */ +void __init ovpn_tcp_init(void) +{ + ovpn_tcp_build_protos(&ovpn_tcp_prot, &ovpn_tcp_ops, &tcp_prot, + &inet_stream_ops); +} diff --git a/drivers/net/ovpn/tcp.h b/drivers/net/ovpn/tcp.h new file mode 100644 index 0000000000000000000000000000000000000000..fb2cd0b606b4d21114b2729c6a3= 4212f9920c3d1 --- /dev/null +++ b/drivers/net/ovpn/tcp.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload + * + * Copyright (C) 2019-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#ifndef _NET_OVPN_TCP_H_ +#define _NET_OVPN_TCP_H_ + +#include +#include +#include + +#include "peer.h" +#include "skb.h" +#include "socket.h" + +void __init ovpn_tcp_init(void); + +int ovpn_tcp_socket_attach(struct socket *sock, struct ovpn_peer *peer); +void ovpn_tcp_socket_detach(struct socket *sock); +void ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sk_buff *skb); + +/* Prepare skb and enqueue it for sending to peer. + * + * Preparation consist in prepending the skb payload with its size. + * Required by the OpenVPN protocol in order to extract packets from + * the TCP stream on the receiver side. + */ +static inline void ovpn_tcp_send_skb(struct ovpn_peer *peer, + struct sk_buff *skb) +{ + u16 len =3D skb->len; + + *(__be16 *)__skb_push(skb, sizeof(u16)) =3D htons(len); + + bh_lock_sock(peer->sock->sock->sk); + ovpn_tcp_send_sock_skb(peer, skb); + bh_unlock_sock(peer->sock->sock->sk); +} + +#endif /* _NET_OVPN_TCP_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 7CDE018C928 for ; Wed, 16 Oct 2024 01:04:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040650; cv=none; b=MG83QAQk+7VNAo5jbMO1BtM1ULiD4OW9Nm0YOcYvhIEroVF4Ug0PV1Xci+xSGyva3qrPQWkRPPOmyLWDh/YzQrICQgiOENRqo1HM10bTQvTQ2qjJZnBm3I4ZwskYqt0rq0XGCciIIfGWXofkO8MFyaAxjQ15mDoJB8973UGVS5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040650; c=relaxed/simple; bh=5tcu2fG7KrBaMuG4quENwE2rUG2aHWyMgw7oow7J32w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UAsbi7dFUvdhvxrEibKrWm0qQtq+CAAx0nRtjhMDKKEv+V2XQ3SO/1HusAYZkL2uUTZNBvCXPt6Z6kpAo1ZRcRvl1FEwNUVK1p5KrAMj+3bB1v9TqiF9WpJcDJFaqcLGlxnJGFFJuhSSkh7GVE1zzcDjGDmUIHMFfzDai1fMWHY= 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=ahDZDlY3; arc=none smtp.client-ip=209.85.221.54 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="ahDZDlY3" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d47b38336so3784977f8f.3 for ; Tue, 15 Oct 2024 18:04:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040646; x=1729645446; 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=mYOEZ7N/JNRw4kkW5nU68RzhrXcuF8cInBO89qjz/Cg=; b=ahDZDlY37LiMSB6wXBtayHR3oKtgl9BZRKFb/7Pgx4SMqaoTYZHhsTIIJRAfMRomiY mGmAlksuqKElb8jHU99B6Qr8Jse+3j2Rbr14QMk5xD1e7sRjKjSqReUslXOdazXV45gb xjTDE5Pz9fpW3euGgcqFSRtHayJtew9iNcpHspnnbW4vRYowyIrKLiGxa6tE1XrjUhPF MuIg+3lqcjAmHzJ7vAW+Blk+7bWIw5wPROCkdgv1U4rofeWQlX7EbXCkLHCqs97Ql/UI jEEVwu8eMZbKzy7LgrdppuOb4UEE0x5KQv6IFj+c+cR6x3sDQ4dPknDyrWBmzJDm0O6u 6jAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040646; x=1729645446; 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=mYOEZ7N/JNRw4kkW5nU68RzhrXcuF8cInBO89qjz/Cg=; b=DlaNlLVUL1znYPTI+e180UDie2vfUH2S3CyxCCoKD9F/AipKqYgYSv1kmMuRyocPNn O29pFmqjT416VNlYkmQjnfumyN7FwjQeA65rW49AjyfxBSpeDN33J4oFP+fCtseiu2O2 S8z1OBVTGDhWpgyvWxR4H7sH+BZCIYQRfM62TOsoNr7/9XfsVMGiEz6HABn5KZb6eopy JF9nUtiXX1MhJS+mbQ7S+sTXn5ydnAL7d57lAmmqkk7vnJySlML1mQ1kFjLxPY9nFgMT dgaDrKpM6kpDOOPsXTdmap9rvKpEvyoeiMy//nRbW65/E6Lp3mX3qUq9ivRWd4TDrH1Q egsQ== X-Forwarded-Encrypted: i=1; AJvYcCWSCrLg2cLDX00qRFU+m4zZ+BTXAfvq9eqv+WiHKqEVJuX67f+R8PTNGaKdIyn0yICp5D9vFfejblVuP+g=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2cWFyRI8XnT9JQ3n+oFuv0tL4V1clvN1PjejfZJSAcMOHFEIZ CdNWo2+a0fnPN8pNNx8o8V0byBnEcxvsgOKyQmtNJjepqOw7ZPSGaQxxPWsiUGA= X-Google-Smtp-Source: AGHT+IG9/Y3B6XJcdEDgllZ6UUTtcuP75JF0sU/wKBTk/NvJBJ3l26zRceA9GmxqwWjoJJXzwh4onw== X-Received: by 2002:a5d:4947:0:b0:37d:4afe:8c9b with SMTP id ffacd0b85a97d-37d86d5fec1mr1226680f8f.54.1729040645898; Tue, 15 Oct 2024 18:04:05 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:05 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:13 +0200 Subject: [PATCH net-next v9 13/23] ovpn: implement multi-peer support 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: <20241016-b4-ovpn-v9-13-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=13278; i=antonio@openvpn.net; h=from:subject:message-id; bh=5tcu2fG7KrBaMuG4quENwE2rUG2aHWyMgw7oow7J32w=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDwx65Prx0DCm/u1Jn5i4ZnSp4TR9/WLp/py wROtG4yNnWJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h/AgB/9gZmNmx/6cE31mfiHrdYRYHNYvQGDqrvz2AHVJ2MQPhQTv1nTGtVoyrohYmwz4D/RneP/ w2uQ5UgshwmTsTzdEHEs2pTLmCUuMmR1vcnBbqyZAxt9DGyNzp3solTcmQL8HZ9uFeh62Y5SeQv stEFQ0fq0zoe70gYYonwz7GgroUYFFweXat43UARAbipJ2SsZrjxQ4OJvWnQ/dxqcavuzosDAQ/ yPgOvI+WNVlyumw2hR6Hv1YVDOYcdvnf3z+Oc6zGg//l0adMrcIB0xAJGEDHFunt7GoPAqot6kY RtHY6/tgW6JADgfSw1lgEJVCwjlwwdgW9nMW/V1ZEJWF9Pdt X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C With this change an ovpn instance will be able to stay connected to multiple remote endpoints. This functionality is strictly required when running ovpn on an OpenVPN server. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/main.c | 55 +++++++++++++- drivers/net/ovpn/ovpnstruct.h | 19 +++++ drivers/net/ovpn/peer.c | 166 ++++++++++++++++++++++++++++++++++++++= ++-- drivers/net/ovpn/peer.h | 9 +++ 4 files changed, 243 insertions(+), 6 deletions(-) diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index 0488e395eb27d3dba1efc8ff39c023e0ac4a38dd..c7453127ab640d7268c1ce919a8= 7cc5419fac9ee 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -30,6 +30,9 @@ =20 static void ovpn_struct_free(struct net_device *net) { + struct ovpn_struct *ovpn =3D netdev_priv(net); + + kfree(ovpn->peers); } =20 static int ovpn_net_init(struct net_device *dev) @@ -133,12 +136,52 @@ static void ovpn_setup(struct net_device *dev) SET_NETDEV_DEVTYPE(dev, &ovpn_type); } =20 +static int ovpn_mp_alloc(struct ovpn_struct *ovpn) +{ + struct in_device *dev_v4; + int i; + + if (ovpn->mode !=3D OVPN_MODE_MP) + return 0; + + dev_v4 =3D __in_dev_get_rtnl(ovpn->dev); + if (dev_v4) { + /* disable redirects as Linux gets confused by ovpn + * handling same-LAN routing. + * This happens because a multipeer interface is used as + * relay point between hosts in the same subnet, while + * in a classic LAN this would not be needed because the + * two hosts would be able to talk directly. + */ + IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false); + IPV4_DEVCONF_ALL(dev_net(ovpn->dev), SEND_REDIRECTS) =3D false; + } + + /* the peer container is fairly large, therefore we allocate it only in + * MP mode + */ + ovpn->peers =3D kzalloc(sizeof(*ovpn->peers), GFP_KERNEL); + if (!ovpn->peers) + return -ENOMEM; + + spin_lock_init(&ovpn->peers->lock); + + for (i =3D 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) { + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_vpn_addr[i], i); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i], i); + } + + return 0; +} + static int ovpn_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { struct ovpn_struct *ovpn =3D netdev_priv(dev); enum ovpn_mode mode =3D OVPN_MODE_P2P; + int err; =20 if (data && data[IFLA_OVPN_MODE]) { mode =3D nla_get_u8(data[IFLA_OVPN_MODE]); @@ -149,6 +192,10 @@ static int ovpn_newlink(struct net *src_net, struct ne= t_device *dev, ovpn->mode =3D mode; spin_lock_init(&ovpn->lock); =20 + err =3D ovpn_mp_alloc(ovpn); + if (err < 0) + return err; + /* turn carrier explicitly off after registration, this way state is * clearly defined */ @@ -197,8 +244,14 @@ static int ovpn_netdev_notifier_call(struct notifier_b= lock *nb, netif_carrier_off(dev); ovpn->registered =3D false; =20 - if (ovpn->mode =3D=3D OVPN_MODE_P2P) + switch (ovpn->mode) { + case OVPN_MODE_P2P: ovpn_peer_release_p2p(ovpn); + break; + case OVPN_MODE_MP: + ovpn_peers_free(ovpn); + break; + } break; case NETDEV_POST_INIT: case NETDEV_GOING_DOWN: diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h index 4a48fc048890ab1cda78bc104fe3034b4a49d226..12ed5e22c2108c9f143d1984048= eb40c887cac63 100644 --- a/drivers/net/ovpn/ovpnstruct.h +++ b/drivers/net/ovpn/ovpnstruct.h @@ -15,6 +15,23 @@ #include #include =20 +/** + * struct ovpn_peer_collection - container of peers for MultiPeer mode + * @by_id: table of peers index by ID + * @by_vpn_addr: table of peers indexed by VPN IP address (items can be + * rehashed on the fly due to peer IP change) + * @by_transp_addr: table of peers indexed by transport address (items can= be + * rehashed on the fly due to peer IP change) + * @lock: protects writes to peer tables + */ +struct ovpn_peer_collection { + DECLARE_HASHTABLE(by_id, 12); + struct hlist_nulls_head by_vpn_addr[1 << 12]; + struct hlist_nulls_head by_transp_addr[1 << 12]; + + spinlock_t lock; /* protects writes to peer tables */ +}; + /** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel @@ -22,6 +39,7 @@ * @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) * @lock: protect this object + * @peers: data structures holding multi-peer references * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list * @gro_cells: pointer to the Generic Receive Offload cell @@ -32,6 +50,7 @@ struct ovpn_struct { bool registered; enum ovpn_mode mode; spinlock_t lock; /* protect writing to the ovpn_struct object */ + struct ovpn_peer_collection *peers; struct ovpn_peer __rcu *peer; struct list_head dev_list; struct gro_cells gro_cells; diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 5025bfb759d6a5f31e3f2ec094fe561fbdb9f451..73ef509faab9701192a45ffe78a= 46dbbbeab01c2 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -9,6 +9,7 @@ =20 #include #include +#include =20 #include "ovpnstruct.h" #include "bind.h" @@ -64,17 +65,16 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovp= n, u32 id) return peer; } =20 -/** - * ovpn_peer_release - release peer private members - * @peer: the peer to release - */ static void ovpn_peer_release(struct ovpn_peer *peer) { if (peer->sock) ovpn_socket_put(peer->sock); =20 ovpn_crypto_state_release(&peer->crypto); + spin_lock_bh(&peer->lock); ovpn_bind_reset(peer, NULL); + spin_unlock_bh(&peer->lock); + dst_cache_destroy(&peer->dst_cache); netdev_put(peer->ovpn->dev, &peer->ovpn->dev_tracker); kfree_rcu(peer, rcu); @@ -309,6 +309,89 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, = struct sk_buff *skb, return match; } =20 +#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl =3D &(_tbl); \ + (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ + +/** + * ovpn_peer_add_mp - add peer to related tables in a MP instance + * @ovpn: the instance to add the peer to + * @peer: the peer to add + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *pe= er) +{ + struct sockaddr_storage sa =3D { 0 }; + struct hlist_nulls_head *nhead; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + struct ovpn_bind *bind; + struct ovpn_peer *tmp; + size_t salen; + int ret =3D 0; + + spin_lock_bh(&ovpn->peers->lock); + /* do not add duplicates */ + tmp =3D ovpn_peer_get_by_id(ovpn, peer->id); + if (tmp) { + ovpn_peer_put(tmp); + ret =3D -EEXIST; + goto out; + } + + bind =3D rcu_dereference_protected(peer->bind, true); + /* peers connected via TCP have bind =3D=3D NULL */ + if (bind) { + switch (bind->remote.in4.sin_family) { + case AF_INET: + sa4 =3D (struct sockaddr_in *)&sa; + + sa4->sin_family =3D AF_INET; + sa4->sin_addr.s_addr =3D bind->remote.in4.sin_addr.s_addr; + sa4->sin_port =3D bind->remote.in4.sin_port; + salen =3D sizeof(*sa4); + break; + case AF_INET6: + sa6 =3D (struct sockaddr_in6 *)&sa; + + sa6->sin6_family =3D AF_INET6; + sa6->sin6_addr =3D bind->remote.in6.sin6_addr; + sa6->sin6_port =3D bind->remote.in6.sin6_port; + salen =3D sizeof(*sa6); + break; + default: + ret =3D -EPROTONOSUPPORT; + goto out; + } + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_transp_addr, &sa, + salen); + hlist_nulls_add_head_rcu(&peer->hash_entry_transp_addr, nhead); + } + + hlist_add_head_rcu(&peer->hash_entry_id, + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); + + if (peer->vpn_addrs.ipv4.s_addr !=3D htonl(INADDR_ANY)) { + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv4, + sizeof(peer->vpn_addrs.ipv4)); + hlist_nulls_add_head_rcu(&peer->hash_entry_addr4, nhead); + } + + if (!ipv6_addr_any(&peer->vpn_addrs.ipv6)) { + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv6, + sizeof(peer->vpn_addrs.ipv6)); + hlist_nulls_add_head_rcu(&peer->hash_entry_addr6, nhead); + } +out: + spin_unlock_bh(&ovpn->peers->lock); + return ret; +} + /** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to @@ -349,6 +432,8 @@ static int ovpn_peer_add_p2p(struct ovpn_struct *ovpn, = struct ovpn_peer *peer) int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovpn_peer *peer) { switch (ovpn->mode) { + case OVPN_MODE_MP: + return ovpn_peer_add_mp(ovpn, peer); case OVPN_MODE_P2P: return ovpn_peer_add_p2p(ovpn, peer); default: @@ -356,6 +441,51 @@ int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovp= n_peer *peer) } } =20 +/** + * ovpn_peer_unhash - remove peer reference from all hashtables + * @peer: the peer to remove + * @reason: the delete reason to attach to the peer + */ +static void ovpn_peer_unhash(struct ovpn_peer *peer, + enum ovpn_del_peer_reason reason) + __must_hold(&ovpn->peers->lock) +{ + hlist_del_init_rcu(&peer->hash_entry_id); + + hlist_nulls_del_init_rcu(&peer->hash_entry_addr4); + hlist_nulls_del_init_rcu(&peer->hash_entry_addr6); + hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); + + ovpn_peer_put(peer); + peer->delete_reason =3D reason; +} + +/** + * ovpn_peer_del_mp - delete peer from related tables in a MP instance + * @peer: the peer to delete + * @reason: reason why the peer was deleted (sent to userspace) + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_peer_del_mp(struct ovpn_peer *peer, + enum ovpn_del_peer_reason reason) + __must_hold(&peer->ovpn->peers->lock) +{ + struct ovpn_peer *tmp; + int ret =3D -ENOENT; + + tmp =3D ovpn_peer_get_by_id(peer->ovpn, peer->id); + if (tmp =3D=3D peer) { + ovpn_peer_unhash(peer, reason); + ret =3D 0; + } + + if (tmp) + ovpn_peer_put(tmp); + + return ret; +} + /** * ovpn_peer_del_p2p - delete peer from related tables in a P2P instance * @peer: the peer to delete @@ -411,10 +541,36 @@ void ovpn_peer_release_p2p(struct ovpn_struct *ovpn) */ int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason) { + int ret; + switch (peer->ovpn->mode) { + case OVPN_MODE_MP: + spin_lock_bh(&peer->ovpn->peers->lock); + ret =3D ovpn_peer_del_mp(peer, reason); + spin_unlock_bh(&peer->ovpn->peers->lock); + return ret; case OVPN_MODE_P2P: - return ovpn_peer_del_p2p(peer, reason); + spin_lock_bh(&peer->ovpn->lock); + ret =3D ovpn_peer_del_p2p(peer, reason); + spin_unlock_bh(&peer->ovpn->lock); + return ret; default: return -EOPNOTSUPP; } } + +/** + * ovpn_peers_free - free all peers in the instance + * @ovpn: the instance whose peers should be released + */ +void ovpn_peers_free(struct ovpn_struct *ovpn) +{ + struct hlist_node *tmp; + struct ovpn_peer *peer; + int bkt; + + spin_lock_bh(&ovpn->peers->lock); + hash_for_each_safe(ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) + ovpn_peer_unhash(peer, OVPN_DEL_PEER_REASON_TEARDOWN); + spin_unlock_bh(&ovpn->peers->lock); +} diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 2b7fa9510e362ef3646157bb0d361bab19ddaa99..942b90c84a0fb9e6fbb96f6df7f= 7842a9f738caf 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -23,6 +23,10 @@ * @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel + * @hash_entry_id: entry in the peer ID hashtable + * @hash_entry_addr4: entry in the peer IPv4 hashtable + * @hash_entry_addr6: entry in the peer IPv6 hashtable + * @hash_entry_transp_addr: entry in the peer transport address hashtable * @sock: the socket being used to talk to this peer * @tcp: keeps track of TCP specific state * @tcp.strp: stream parser context (TCP only) @@ -55,6 +59,10 @@ struct ovpn_peer { struct in_addr ipv4; struct in6_addr ipv6; } vpn_addrs; + struct hlist_node hash_entry_id; + struct hlist_nulls_node hash_entry_addr4; + struct hlist_nulls_node hash_entry_addr6; + struct hlist_nulls_node hash_entry_transp_addr; struct ovpn_socket *sock; =20 /* state of the TCP reading. Needed to keep track of how much of a @@ -119,6 +127,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovp= n, u32 id); int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovpn_peer *peer); int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason= ); void ovpn_peer_release_p2p(struct ovpn_struct *ovpn); +void ovpn_peers_free(struct ovpn_struct *ovpn); =20 struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb); --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 BB0EC2628D for ; Wed, 16 Oct 2024 01:04:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040652; cv=none; b=U0zELNFMSN+9/bWGcoJKPPOZs7sudfQCbRoYuZDSvrebFnuYUpUUqyjKZxJIsLdH6ZLdzD4yicbEsgHo8crlr5p6raPwTF9Y+udJsXrhec5mxhSeD/nnb9ytV4uWXVJkFZwIWXXLEwzY7NdRsWwYvbJ9xQx0UU4kiRZV6/MNDrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040652; c=relaxed/simple; bh=sA/j4ujOLCHidfSHkWTUz3qBTHj05rdoZRacUys7DAY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WPIr/fVW2YurhDNPia77i77JNhUe3jFj+41nK8Q5mgv9v9KHbEjRQlFClFYeQ5LTqoUgQCq04lx8L9GtDW7BnuQ3CiZrFohCtdg4BPEwN1p6KDPtiajP779oNbXYlmlj+gX4Q6PD7nVww8Nf3K3IENtJYgz+3kRLjct91oEZcvA= 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=ctuVbOvP; arc=none smtp.client-ip=209.85.128.53 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="ctuVbOvP" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-43124843b04so34748685e9.2 for ; Tue, 15 Oct 2024 18:04:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040648; x=1729645448; 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=j9WF8fOdVHScVBWZFs6runNvFAcpSNIRWVoB+2aYyJI=; b=ctuVbOvPt7m+tgfpSZ7VS3AzppHUckjnlazeJM8YqqLYq9v/JxoF7ksJJJ6PGBPdTt IYK9hIE5Cxo1usBhuQRFx479E33bG5yhW5IC2a431Rmhk6p7V8COT0J62qKWVf38L/rA ADvt8uuIxlmSMyraS1WYfjUc9Z8AYRga5O+uzdh2muL1zD6GwYuxxHeg44ptniuwwlBD 3g6aWVXRF5rALHV9yjKmqu2E34be4Mdz9zQe6726x12Wmb+4LUdrxTWBSPkxoyDucYfG qi9jo0RQaPxVkNHZ+9Ycy+h7waQwI4tQiFti7EHiVLYc6w6vSQBJg8+C7yU9gAVJ/dQV jLJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040648; x=1729645448; 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=j9WF8fOdVHScVBWZFs6runNvFAcpSNIRWVoB+2aYyJI=; b=TnrJ9nMtB5puUndFebXxCF9YEes/bW4p8p5ceU76zIGRKvujHbn3wPcZSW6cZb0xFd eM4lR/KLMBGZ6ew76fE6pMXdXmb/QHN4+7CpZjHcVesi0s7SwqoSUXqtAthWi3UclRfx O10opagnZiScthTG/KdK9oyNDRNtcMJe9qJn0qJPW15XJHBPC6VWlWHYQpRa1PQ1JIMn BbhZRm/FYA+nCtnGRXhBVzbLdc6/xGOBouPTnDRt/pTMI0V9kiHtHlRfoVHsVsHOhig7 UOskJ0FuX9KEW5Etm3wWIdX4MpXtEVKrHkMTnX4uIefzlv4yu1o+i93mKlHs1AlKSZNa SDHA== X-Forwarded-Encrypted: i=1; AJvYcCVLRDP8rNvWLUcAqX2kgzC09TLf7MbqpZQwT+PSiUd4B9gAjNSXKcqNassCMrk3kEP8zdoKRCoQPZq/IzY=@vger.kernel.org X-Gm-Message-State: AOJu0YzDXEq7qBZKMFiFB18TPlccrckA3IVL50zcPV26rVYktgu7OAnE drBUo3v07k/F2xv8RkIB8Ujgdm6n2GJHe2BA+0lLf3X023ytlW/SkNycvyrOgEw= X-Google-Smtp-Source: AGHT+IEAnATK5a7GwsIHlRKJMGWSVhk70kVUMulJ3OflxgRD+7+KQVLjsnyPL/mpwFapqMxrkG32dA== X-Received: by 2002:a5d:570d:0:b0:374:c8e5:d568 with SMTP id ffacd0b85a97d-37d551d6b43mr12265936f8f.29.1729040647947; Tue, 15 Oct 2024 18:04:07 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:07 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:14 +0200 Subject: [PATCH net-next v9 14/23] ovpn: implement peer lookup logic 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: <20241016-b4-ovpn-v9-14-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=10819; i=antonio@openvpn.net; h=from:subject:message-id; bh=sA/j4ujOLCHidfSHkWTUz3qBTHj05rdoZRacUys7DAY=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDw7R/hdvfcO2fCIRbAaXLRPqKJvx5LU6Q/v 07pp1yVKzOJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h5z/B/wPkYS0Hl4oIfI13O3tsNCuSOCff97pNJ19Xopp1zP2o+VSEtTeX1LSb/8kMuUVNAv/UKI Q3WErL70hS/nPWY6nMQXHzPYe3xRVIxc5S4daqVD7l6MiVASqmUmZPbcC1Xcb02gt90V6aMi8yu S3FekFXs7I+Efr764Oo9/IFsIlZAg2UhtDDWWkHmQh09Vdg7vXDBj3YAC6jJo8tJtzbpZjnnLEu l2LVJRm+VCKT8vFVFoQrmUTTh0mPMKe7GPpw6h//8YPKJhAwgqREktBasGVNrKwABNXn13IASEj 3lQD3vcNLCAt2JspGNfN2eJIq2/6Oe1ecpilKza85WmxLrSo X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C In a multi-peer scenario there are a number of situations when a specific peer needs to be looked up. We may want to lookup a peer by: 1. its ID 2. its VPN destination IP 3. its transport IP/port couple For each of the above, there is a specific routing table referencing all peers for fast look up. Case 2. is a bit special in the sense that an outgoing packet may not be sent to the peer VPN IP directly, but rather to a network behind it. For this reason we first perform a nexthop lookup in the system routing table and then we use the retrieved nexthop as peer search key. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/peer.c | 272 ++++++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 264 insertions(+), 8 deletions(-) diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 73ef509faab9701192a45ffe78a46dbbbeab01c2..c7dc9032c2b55fd42befc1f3e7a= 0eca893a96576 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -10,6 +10,7 @@ #include #include #include +#include =20 #include "ovpnstruct.h" #include "bind.h" @@ -125,6 +126,94 @@ static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *= skb, return true; } =20 +/** + * ovpn_nexthop_from_skb4 - retrieve IPv4 nexthop for outgoing skb + * @skb: the outgoing packet + * + * Return: the IPv4 of the nexthop + */ +static __be32 ovpn_nexthop_from_skb4(struct sk_buff *skb) +{ + const struct rtable *rt =3D skb_rtable(skb); + + if (rt && rt->rt_uses_gateway) + return rt->rt_gw4; + + return ip_hdr(skb)->daddr; +} + +/** + * ovpn_nexthop_from_skb6 - retrieve IPv6 nexthop for outgoing skb + * @skb: the outgoing packet + * + * Return: the IPv6 of the nexthop + */ +static struct in6_addr ovpn_nexthop_from_skb6(struct sk_buff *skb) +{ + const struct rt6_info *rt =3D skb_rt6_info(skb); + + if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) + return ipv6_hdr(skb)->daddr; + + return rt->rt6i_gateway; +} + +#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl =3D &(_tbl); \ + (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ + +/** + * ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address + * @ovpn: the openvpn instance to search + * @addr: VPN IPv4 to use as search key + * + * Refcounter is not increased for the returned peer. + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4(struct ovpn_struct *ov= pn, + __be32 addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, &addr, + sizeof(addr)); + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr4) + if (addr =3D=3D tmp->vpn_addrs.ipv4.s_addr) + return tmp; + + return NULL; +} + +/** + * ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address + * @ovpn: the openvpn instance to search + * @addr: VPN IPv6 to use as search key + * + * Refcounter is not increased for the returned peer. + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6(struct ovpn_struct *ov= pn, + struct in6_addr *addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, addr, + sizeof(*addr)); + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr6) + if (ipv6_addr_equal(addr, &tmp->vpn_addrs.ipv6)) + return tmp; + + return NULL; +} + /** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from @@ -202,14 +291,44 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *= ovpn, struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb) { - struct ovpn_peer *peer =3D NULL; + struct ovpn_peer *tmp, *peer =3D NULL; struct sockaddr_storage ss =3D { 0 }; + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + size_t sa_len; =20 if (unlikely(!ovpn_peer_skb_to_sockaddr(skb, &ss))) return NULL; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) - peer =3D ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); + return ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); + + switch (ss.ss_family) { + case AF_INET: + sa_len =3D sizeof(struct sockaddr_in); + break; + case AF_INET6: + sa_len =3D sizeof(struct sockaddr_in6); + break; + default: + return NULL; + } + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_transp_addr, &ss, sa_len); + + rcu_read_lock(); + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, + hash_entry_transp_addr) { + if (!ovpn_peer_transp_match(tmp, &ss)) + continue; + + if (!ovpn_peer_hold(tmp)) + continue; + + peer =3D tmp; + break; + } + rcu_read_unlock(); =20 return peer; } @@ -244,10 +363,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(stru= ct ovpn_struct *ovpn, */ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d) { - struct ovpn_peer *peer =3D NULL; + struct ovpn_peer *tmp, *peer =3D NULL; + struct hlist_head *head; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) - peer =3D ovpn_peer_get_by_id_p2p(ovpn, peer_id); + return ovpn_peer_get_by_id_p2p(ovpn, peer_id); + + head =3D ovpn_get_hash_head(ovpn->peers->by_id, &peer_id, + sizeof(peer_id)); + + rcu_read_lock(); + hlist_for_each_entry_rcu(tmp, head, hash_entry_id) { + if (tmp->id !=3D peer_id) + continue; + + if (!ovpn_peer_hold(tmp)) + continue; + + peer =3D tmp; + break; + } + rcu_read_unlock(); =20 return peer; } @@ -269,6 +405,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_stru= ct *ovpn, struct sk_buff *skb) { struct ovpn_peer *peer =3D NULL; + struct in6_addr addr6; + __be32 addr4; =20 /* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side @@ -279,11 +417,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_s= truct *ovpn, if (unlikely(peer && !ovpn_peer_hold(peer))) peer =3D NULL; rcu_read_unlock(); + return peer; } =20 + rcu_read_lock(); + switch (skb_protocol_to_family(skb)) { + case AF_INET: + addr4 =3D ovpn_nexthop_from_skb4(skb); + peer =3D ovpn_peer_get_by_vpn_addr4(ovpn, addr4); + break; + case AF_INET6: + addr6 =3D ovpn_nexthop_from_skb6(skb); + peer =3D ovpn_peer_get_by_vpn_addr6(ovpn, &addr6); + break; + } + + if (unlikely(peer && !ovpn_peer_hold(peer))) + peer =3D NULL; + rcu_read_unlock(); + return peer; } =20 +/** + * ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destinat= ion + * @ovpn: the private data representing the current VPN session + * @dest: the destination to be looked up + * + * Looks up in the IPv4 system routing table the IP of the nexthop to be u= sed + * to reach the destination passed as argument. If no nexthop can be found= , the + * destination itself is returned as it probably has to be used as nexthop. + * + * Return: the IP of the next hop if found or dest itself otherwise + */ +static __be32 ovpn_nexthop_from_rt4(struct ovpn_struct *ovpn, __be32 dest) +{ + struct rtable *rt; + struct flowi4 fl =3D { + .daddr =3D dest + }; + + rt =3D ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL); + if (IS_ERR(rt)) { + net_dbg_ratelimited("%s: no route to host %pI4\n", __func__, + &dest); + /* if we end up here this packet is probably going to be + * thrown away later + */ + return dest; + } + + if (!rt->rt_uses_gateway) + goto out; + + dest =3D rt->rt_gw4; +out: + ip_rt_put(rt); + return dest; +} + +/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destinat= ion + * @ovpn: the private data representing the current VPN session + * @dest: the destination to be looked up + * + * Looks up in the IPv6 system routing table the IP of the nexthop to be u= sed + * to reach the destination passed as argument. If no nexthop can be found= , the + * destination itself is returned as it probably has to be used as nexthop. + * + * Return: the IP of the next hop if found or dest itself otherwise + */ +static struct in6_addr ovpn_nexthop_from_rt6(struct ovpn_struct *ovpn, + struct in6_addr dest) +{ +#if IS_ENABLED(CONFIG_IPV6) + struct dst_entry *entry; + struct rt6_info *rt; + struct flowi6 fl =3D { + .daddr =3D dest, + }; + + entry =3D ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl, + NULL); + if (IS_ERR(entry)) { + net_dbg_ratelimited("%s: no route to host %pI6c\n", __func__, + &dest); + /* if we end up here this packet is probably going to be + * thrown away later + */ + return dest; + } + + rt =3D dst_rt6_info(entry); + + if (!(rt->rt6i_flags & RTF_GATEWAY)) + goto out; + + dest =3D rt->rt6i_gateway; +out: + dst_release((struct dst_entry *)rt); +#endif + return dest; +} + /** * ovpn_peer_check_by_src - check that skb source is routed via peer * @ovpn: the openvpn instance to search @@ -296,6 +532,8 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, s= truct sk_buff *skb, struct ovpn_peer *peer) { bool match =3D false; + struct in6_addr addr6; + __be32 addr4; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) { /* in P2P mode, no matter the destination, packets are always @@ -304,15 +542,33 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn,= struct sk_buff *skb, rcu_read_lock(); match =3D (peer =3D=3D rcu_dereference(ovpn->peer)); rcu_read_unlock(); + return match; + } + + /* This function performs a reverse path check, therefore we now + * lookup the nexthop we would use if we wanted to route a packet + * to the source IP. If the nexthop matches the sender we know the + * latter is valid and we allow the packet to come in + */ + + switch (skb_protocol_to_family(skb)) { + case AF_INET: + addr4 =3D ovpn_nexthop_from_rt4(ovpn, ip_hdr(skb)->saddr); + rcu_read_lock(); + match =3D (peer =3D=3D ovpn_peer_get_by_vpn_addr4(ovpn, addr4)); + rcu_read_unlock(); + break; + case AF_INET6: + addr6 =3D ovpn_nexthop_from_rt6(ovpn, ipv6_hdr(skb)->saddr); + rcu_read_lock(); + match =3D (peer =3D=3D ovpn_peer_get_by_vpn_addr6(ovpn, &addr6)); + rcu_read_unlock(); + break; } =20 return match; } =20 -#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ - typeof(_tbl) *__tbl =3D &(_tbl); \ - (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ - /** * ovpn_peer_add_mp - add peer to related tables in a MP instance * @ovpn: the instance to add the peer to --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 5794118DF69 for ; Wed, 16 Oct 2024 01:04:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040654; cv=none; b=APevGI0sb39EBj97b0kNFGiy2xII4QDi4DqYrdd7FEgwYjg7iq7Kv9LC6ZW5WVKaF7QREfUXORnLhhI5gkZSUlzDABf65wnalscw/a/o+Do2C9+LuLfojING21rLuPJuwpQEqD7rHI4mAPlg2iiFdbDDOuMJvfkxxCkgmLajdi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040654; c=relaxed/simple; bh=tHvdkFCC7XEkmaV+Ugo05ZdniHu9y9hAfXfgM2/YWi4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ReMXubqkP4Umv8rWOdY+l62jUBwsy+u1F3k28aW3HYFXbs2iEcQ5fbneY+Lf5kJmIuN1ABr3iMrt2QaIy3M+qfdYkZqHcrQbtAgRhjWgS9oqivinTmQ2N89n/XmFnwlup8AopXaF+L7MNqM7p/bRxP3zKZsMgsaOOjIn7uU/FoY= 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=RjaKvshd; arc=none smtp.client-ip=209.85.128.42 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="RjaKvshd" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-430f6bc9ca6so47349725e9.2 for ; Tue, 15 Oct 2024 18:04:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040651; x=1729645451; 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=9a2dx/+iMr5WZvHuJXmMe2x+p9wHKQoro31J7CcouvM=; b=RjaKvshdHyyFR0IL+IeC4lGBrj3NFHq4WHcByyVUxRhRqj1Z52r8r9o998NyN16mcO zoomhRg986M5QBHK/rPkQ8FeWoC78/mVN8B89bqR3Jd6Kr6SE1ilGXvVgu4OygShxs8l 181CI8q4DHr+r/6b4W3BckdodGfFto1COuGrV5ZtVyyKuQ1Hz8eQTLOo8zIzTnQhfu7V 8l4aGz9UmiNRfNHtE2VlhTNdKSaNhFIRfk6qJAt7XviIPSIEcMj5tzJbu+lSoioKIpoN LCvRt3kM3jMeYUOWAgyRCLMsP43II3/rUQX3rCGfS6fURXv4+a4gZByRvom5QdqV2bS7 X8yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040651; x=1729645451; 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=9a2dx/+iMr5WZvHuJXmMe2x+p9wHKQoro31J7CcouvM=; b=WrrMWPhCEe7CksWf0VLnkE6VRse0dFA7VE3ALA6M1X6TmjRyI8YiBHOhop+54WEX2v u9Ry/1vP+7znwkPXZdAETDhlRCZKAlqUnFb/nGjmDdHBRYp0z8793j1lAzQ3ijBtStzr aHVIpo8gezrzaVzh8rXrcr4hIeiPt8fIgcm25XljwBSU0uFu0q5L5y2W/T4DaNdU36EI k9cDkKsUir/xr0okM79yznw5GYx/agXqgh9ypDUZ7QKBfrHQ5olsHbrkj2r6hpYSQ8Pj hAPZ6S+SeYzYOw3mV7rfcYuUe3gKTz6BoMCbfkuaj0KAPtDQyKo4s3r712Y3veQ36q0m gUAA== X-Forwarded-Encrypted: i=1; AJvYcCVRBm/zlUZAq6VZC2aGRzkrn9jXF+X5y9cucFub+KbA8hNnvC27aLEFxoe12YJAyl+MNBeNMIwnEPO67sY=@vger.kernel.org X-Gm-Message-State: AOJu0YwQpvmczPgIbKZvPGuByEloEZRvUydOfFKwPOmFNeIUMa1QYN93 j+jtUt53Wzmu0AfOTVp6MN5B7URZuwWzIvJ1l8gzI3dWeGhmxXrAWzW75cn3/+A= X-Google-Smtp-Source: AGHT+IHm7pYSN1xzMWRRskIuF8H0/pBUQHzqrTK8Mr7lTPUIHlpebBeCF745QlynzZRy3BYQb99OaA== X-Received: by 2002:a05:600c:8714:b0:430:57f2:baef with SMTP id 5b1f17b1804b1-4312561a03amr126841675e9.32.1729040650641; Tue, 15 Oct 2024 18:04:10 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:09 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:15 +0200 Subject: [PATCH net-next v9 15/23] ovpn: implement keepalive mechanism 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: <20241016-b4-ovpn-v9-15-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=14927; i=antonio@openvpn.net; h=from:subject:message-id; bh=tHvdkFCC7XEkmaV+Ugo05ZdniHu9y9hAfXfgM2/YWi4=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDwKEiahvYqOlWPvRjI0Q6NM8qTodlvY5LhX tyzShAcHWyJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h7ysB/9k5SBsdxLRJOiGOxEEFM4wE628D3khhrHiO6DJMfUe7vbisfVUCZrEGolRg1n3iCtj2bh 265iLn5xCdX8NTbWrCLd/2OflvUkawGPqb0zM+f18u/uxJ2nx9Ju44zBTWDf2QcdoOLo5o9fR3F zF3rRqpeJuFVzsSin9kaEouV4xS8+h9mvrlmkY5v2q3xA8OBDNW7RrGtLpeadd+Pm9wcwB0mU8S 9PipTNQwXVrH3ZYJgLXPo7Cr9l6Hkf8hn+bK5he0obHcAYsn0SU7vrEbFQEWxpjygaUKeZwOJV+ s5azyOrJ5lk2TIlgFUXc4gbK+lX9d8dVWycGMFgcGLci3xNw X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C OpenVPN supports configuring a periodic keepalive packet. message to allow the remote endpoint detect link failures. This change implements the keepalive sending and timer expiring logic. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/io.c | 77 +++++++++++++++++ drivers/net/ovpn/io.h | 5 ++ drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/ovpnstruct.h | 2 + drivers/net/ovpn/peer.c | 187 ++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/peer.h | 15 ++++ drivers/net/ovpn/proto.h | 2 - 7 files changed, 289 insertions(+), 2 deletions(-) diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index 1c0c553b4182762568ce9806a9670e3004076137..1c4cdbb16df4acc5c87707919c6= 71f7a84bf4295 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -28,6 +28,33 @@ #include "skb.h" #include "socket.h" =20 +const unsigned char ovpn_keepalive_message[OVPN_KEEPALIVE_SIZE] =3D { + 0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb, + 0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48 +}; + +/** + * ovpn_is_keepalive - check if skb contains a keepalive message + * @skb: packet to check + * + * Assumes that the first byte of skb->data is defined. + * + * Return: true if skb contains a keepalive or false otherwise + */ +static bool ovpn_is_keepalive(struct sk_buff *skb) +{ + if (*skb->data !=3D ovpn_keepalive_message[0]) + return false; + + if (skb->len !=3D OVPN_KEEPALIVE_SIZE) + return false; + + if (!pskb_may_pull(skb, OVPN_KEEPALIVE_SIZE)) + return false; + + return !memcmp(skb->data, ovpn_keepalive_message, OVPN_KEEPALIVE_SIZE); +} + /* Called after decrypt to write the IP packet to the device. * This method is expected to manage/free the skb. */ @@ -105,6 +132,9 @@ void ovpn_decrypt_post(void *data, int ret) goto drop; } =20 + /* keep track of last received authenticated packet for keepalive */ + peer->last_recv =3D ktime_get_real_seconds(); + /* point to encapsulated IP packet */ __skb_pull(skb, payload_offset); =20 @@ -121,6 +151,12 @@ void ovpn_decrypt_post(void *data, int ret) goto drop; } =20 + if (ovpn_is_keepalive(skb)) { + net_dbg_ratelimited("%s: ping received from peer %u\n", + peer->ovpn->dev->name, peer->id); + goto drop; + } + net_info_ratelimited("%s: unsupported protocol received from peer %u\n", peer->ovpn->dev->name, peer->id); goto drop; @@ -219,6 +255,10 @@ void ovpn_encrypt_post(void *data, int ret) /* no transport configured yet */ goto err; } + + /* keep track of last sent packet for keepalive */ + peer->last_sent =3D ktime_get_real_seconds(); + /* skb passed down the stack - don't free it */ skb =3D NULL; err: @@ -357,3 +397,40 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct = net_device *dev) kfree_skb_list(skb); return NET_XMIT_DROP; } + +/** + * ovpn_xmit_special - encrypt and transmit an out-of-band message to peer + * @peer: peer to send the message to + * @data: message content + * @len: message length + * + * Assumes that caller holds a reference to peer + */ +void ovpn_xmit_special(struct ovpn_peer *peer, const void *data, + const unsigned int len) +{ + struct ovpn_struct *ovpn; + struct sk_buff *skb; + + ovpn =3D peer->ovpn; + if (unlikely(!ovpn)) + return; + + skb =3D alloc_skb(256 + len, GFP_ATOMIC); + if (unlikely(!skb)) + return; + + skb_reserve(skb, 128); + skb->priority =3D TC_PRIO_BESTEFFORT; + __skb_put_data(skb, data, len); + + /* increase reference counter when passing peer to sending queue */ + if (!ovpn_peer_hold(peer)) { + netdev_dbg(ovpn->dev, "%s: cannot hold peer reference for sending specia= l packet\n", + __func__); + kfree_skb(skb); + return; + } + + ovpn_send(ovpn, skb, peer); +} diff --git a/drivers/net/ovpn/io.h b/drivers/net/ovpn/io.h index ad81dd86924689309b3299573575a1705eddaf99..eb224114152c29f42aadf026212= e8d278006b490 100644 --- a/drivers/net/ovpn/io.h +++ b/drivers/net/ovpn/io.h @@ -10,9 +10,14 @@ #ifndef _NET_OVPN_OVPN_H_ #define _NET_OVPN_OVPN_H_ =20 +#define OVPN_KEEPALIVE_SIZE 16 +extern const unsigned char ovpn_keepalive_message[OVPN_KEEPALIVE_SIZE]; + netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev); =20 void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb); +void ovpn_xmit_special(struct ovpn_peer *peer, const void *data, + const unsigned int len); =20 void ovpn_encrypt_post(void *data, int ret); void ovpn_decrypt_post(void *data, int ret); diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index c7453127ab640d7268c1ce919a87cc5419fac9ee..1bd563e3f16f49dd01c897fbe79= cbd90f4b8e9aa 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -191,6 +191,7 @@ static int ovpn_newlink(struct net *src_net, struct net= _device *dev, ovpn->dev =3D dev; ovpn->mode =3D mode; spin_lock_init(&ovpn->lock); + INIT_DELAYED_WORK(&ovpn->keepalive_work, ovpn_peer_keepalive_work); =20 err =3D ovpn_mp_alloc(ovpn); if (err < 0) @@ -244,6 +245,8 @@ static int ovpn_netdev_notifier_call(struct notifier_bl= ock *nb, netif_carrier_off(dev); ovpn->registered =3D false; =20 + cancel_delayed_work_sync(&ovpn->keepalive_work); + switch (ovpn->mode) { case OVPN_MODE_P2P: ovpn_peer_release_p2p(ovpn); diff --git a/drivers/net/ovpn/ovpnstruct.h b/drivers/net/ovpn/ovpnstruct.h index 12ed5e22c2108c9f143d1984048eb40c887cac63..4ac00d550ecb9f84c6c132dd2bd= c0a3fc0ab342c 100644 --- a/drivers/net/ovpn/ovpnstruct.h +++ b/drivers/net/ovpn/ovpnstruct.h @@ -43,6 +43,7 @@ struct ovpn_peer_collection { * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list * @gro_cells: pointer to the Generic Receive Offload cell + * @keepalive_work: struct used to schedule keepalive periodic job */ struct ovpn_struct { struct net_device *dev; @@ -54,6 +55,7 @@ struct ovpn_struct { struct ovpn_peer __rcu *peer; struct list_head dev_list; struct gro_cells gro_cells; + struct delayed_work keepalive_work; }; =20 #endif /* _NET_OVPN_OVPNSTRUCT_H_ */ diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index c7dc9032c2b55fd42befc1f3e7a0eca893a96576..fa1883d12dfd6ce3e2ad1f9835f= 15aa7db5c8290 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -22,6 +22,34 @@ #include "peer.h" #include "socket.h" =20 +/** + * ovpn_peer_keepalive_set - configure keepalive values for peer + * @peer: the peer to configure + * @interval: outgoing keepalive interval + * @timeout: incoming keepalive timeout + */ +void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 tim= eout) +{ + time64_t now =3D ktime_get_real_seconds(); + + netdev_dbg(peer->ovpn->dev, + "%s: scheduling keepalive for peer %u: interval=3D%u timeout=3D%u\n", + __func__, peer->id, interval, timeout); + + peer->keepalive_interval =3D interval; + peer->last_sent =3D now; + peer->keepalive_xmit_exp =3D now + interval; + + peer->keepalive_timeout =3D timeout; + peer->last_recv =3D now; + peer->keepalive_recv_exp =3D now + timeout; + + /* now that interval and timeout have been changed, kick + * off the worker so that the next delay can be recomputed + */ + mod_delayed_work(system_wq, &peer->ovpn->keepalive_work, 0); +} + /** * ovpn_peer_new - allocate and initialize a new peer object * @ovpn: the openvpn instance inside which the peer should be created @@ -815,6 +843,19 @@ int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_de= l_peer_reason reason) } } =20 +static int ovpn_peer_del_nolock(struct ovpn_peer *peer, + enum ovpn_del_peer_reason reason) +{ + switch (peer->ovpn->mode) { + case OVPN_MODE_MP: + return ovpn_peer_del_mp(peer, reason); + case OVPN_MODE_P2P: + return ovpn_peer_del_p2p(peer, reason); + default: + return -EOPNOTSUPP; + } +} + /** * ovpn_peers_free - free all peers in the instance * @ovpn: the instance whose peers should be released @@ -830,3 +871,149 @@ void ovpn_peers_free(struct ovpn_struct *ovpn) ovpn_peer_unhash(peer, OVPN_DEL_PEER_REASON_TEARDOWN); spin_unlock_bh(&ovpn->peers->lock); } + +static time64_t ovpn_peer_keepalive_work_single(struct ovpn_peer *peer, + time64_t now) +{ + time64_t next_run1, next_run2, delta; + unsigned long timeout, interval; + bool expired; + + spin_lock_bh(&peer->lock); + /* we expect both timers to be configured at the same time, + * therefore bail out if either is not set + */ + if (!peer->keepalive_timeout || !peer->keepalive_interval) { + spin_unlock_bh(&peer->lock); + return 0; + } + + /* check for peer timeout */ + expired =3D false; + timeout =3D peer->keepalive_timeout; + delta =3D now - peer->last_recv; + if (delta < timeout) { + peer->keepalive_recv_exp =3D now + timeout - delta; + next_run1 =3D peer->keepalive_recv_exp; + } else if (peer->keepalive_recv_exp > now) { + next_run1 =3D peer->keepalive_recv_exp; + } else { + expired =3D true; + } + + if (expired) { + /* peer is dead -> kill it and move on */ + spin_unlock_bh(&peer->lock); + netdev_dbg(peer->ovpn->dev, "peer %u expired\n", + peer->id); + ovpn_peer_del_nolock(peer, OVPN_DEL_PEER_REASON_EXPIRED); + return 0; + } + + /* check for peer keepalive */ + expired =3D false; + interval =3D peer->keepalive_interval; + delta =3D now - peer->last_sent; + if (delta < interval) { + peer->keepalive_xmit_exp =3D now + interval - delta; + next_run2 =3D peer->keepalive_xmit_exp; + } else if (peer->keepalive_xmit_exp > now) { + next_run2 =3D peer->keepalive_xmit_exp; + } else { + expired =3D true; + next_run2 =3D now + interval; + } + spin_unlock_bh(&peer->lock); + + if (expired) { + /* a keepalive packet is required */ + netdev_dbg(peer->ovpn->dev, + "sending keepalive to peer %u\n", + peer->id); + ovpn_xmit_special(peer, ovpn_keepalive_message, + sizeof(ovpn_keepalive_message)); + } + + if (next_run1 < next_run2) + return next_run1; + + return next_run2; +} + +static time64_t ovpn_peer_keepalive_work_mp(struct ovpn_struct *ovpn, + time64_t now) +{ + time64_t tmp_next_run, next_run =3D 0; + struct hlist_node *tmp; + struct ovpn_peer *peer; + int bkt; + + spin_lock_bh(&ovpn->peers->lock); + hash_for_each_safe(ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) { + tmp_next_run =3D ovpn_peer_keepalive_work_single(peer, now); + if (!tmp_next_run) + continue; + + /* the next worker run will be scheduled based on the shortest + * required interval across all peers + */ + if (!next_run || tmp_next_run < next_run) + next_run =3D tmp_next_run; + } + spin_unlock_bh(&ovpn->peers->lock); + + return next_run; +} + +static time64_t ovpn_peer_keepalive_work_p2p(struct ovpn_struct *ovpn, + time64_t now) +{ + struct ovpn_peer *peer; + time64_t next_run; + + spin_lock_bh(&ovpn->lock); + peer =3D rcu_dereference_protected(ovpn->peer, + lockdep_is_held(&ovpn->lock)); + next_run =3D ovpn_peer_keepalive_work_single(peer, now); + spin_unlock_bh(&ovpn->lock); + + return next_run; +} + +/** + * ovpn_peer_keepalive_work - run keepalive logic on each known peer + * @work: pointer to the work member of the related ovpn object + * + * Each peer has two timers (if configured): + * 1. peer timeout: when no data is received for a certain interval, + * the peer is considered dead and it gets killed. + * 2. peer keepalive: when no data is sent to a certain peer for a + * certain interval, a special 'keepalive' packet is explicitly sent. + * + * This function iterates across the whole peer collection while + * checking the timers described above. + */ +void ovpn_peer_keepalive_work(struct work_struct *work) +{ + struct ovpn_struct *ovpn =3D container_of(work, struct ovpn_struct, + keepalive_work.work); + time64_t next_run =3D 0, now =3D ktime_get_real_seconds(); + + switch (ovpn->mode) { + case OVPN_MODE_MP: + next_run =3D ovpn_peer_keepalive_work_mp(ovpn, now); + break; + case OVPN_MODE_P2P: + next_run =3D ovpn_peer_keepalive_work_p2p(ovpn, now); + break; + } + + /* prevent rearming if the interface is being destroyed */ + if (next_run > 0 && ovpn->registered) { + netdev_dbg(ovpn->dev, + "scheduling keepalive work: now=3D%llu next_run=3D%llu delta=3D%llu\= n", + next_run, now, next_run - now); + schedule_delayed_work(&ovpn->keepalive_work, + (next_run - now) * HZ); + } +} diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 942b90c84a0fb9e6fbb96f6df7f7842a9f738caf..952927ae78a3ab753aaf2c6cc6f= 77121bdac34be 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -43,6 +43,12 @@ * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding + * @keepalive_interval: seconds after which a new keepalive should be sent + * @keepalive_xmit_exp: future timestamp when next keepalive should be sent + * @last_sent: timestamp of the last successfully sent packet + * @keepalive_timeout: seconds after which an inactive peer is considered = dead + * @keepalive_recv_exp: future timestamp when the peer should expire + * @last_recv: timestamp of the last authenticated received packet * @halt: true if ovpn_peer_mark_delete was called * @vpn_stats: per-peer in-VPN TX/RX stays * @link_stats: per-peer link/transport TX/RX stats @@ -91,6 +97,12 @@ struct ovpn_peer { struct ovpn_crypto_state crypto; struct dst_cache dst_cache; struct ovpn_bind __rcu *bind; + unsigned long keepalive_interval; + unsigned long keepalive_xmit_exp; + time64_t last_sent; + unsigned long keepalive_timeout; + unsigned long keepalive_recv_exp; + time64_t last_recv; bool halt; struct ovpn_peer_stats vpn_stats; struct ovpn_peer_stats link_stats; @@ -137,4 +149,7 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_stru= ct *ovpn, bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer); =20 +void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 tim= eout); +void ovpn_peer_keepalive_work(struct work_struct *work); + #endif /* _NET_OVPN_OVPNPEER_H_ */ diff --git a/drivers/net/ovpn/proto.h b/drivers/net/ovpn/proto.h index 32af6b8e574381fb719a1b3b9de3ae1071cc4846..0de8bafadc89ebb85ce40de95ef= 394588738a4ad 100644 --- a/drivers/net/ovpn/proto.h +++ b/drivers/net/ovpn/proto.h @@ -35,8 +35,6 @@ #define OVPN_OP_SIZE_V2 4 #define OVPN_PEER_ID_MASK 0x00FFFFFF #define OVPN_PEER_ID_UNDEF 0x00FFFFFF -/* first byte of keepalive message */ -#define OVPN_KEEPALIVE_FIRST_BYTE 0x2a /* first byte of exit message */ #define OVPN_EXPLICIT_EXIT_NOTIFY_FIRST_BYTE 0x28 =20 --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 6FAC51AA7B4 for ; Wed, 16 Oct 2024 01:04:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040658; cv=none; b=DBqU7xa6wEHaZotkYJVz4lcRgPTI5qrLwpwwdJu0wS0KKxNrxqmg8GJEPK06WCF1RNb/fphhgoMIKo/V4P61WCq7pqAKeCAZxFBRe4HUBCgHhPjWifwagtZaEco2iBB6G/sRHp3e71zW1IwldUP4ksCGy/u5rs+ie/7H5e/Xb9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040658; c=relaxed/simple; bh=wcrv5vDIBCgquenyyeflyML2Rh+TBSULYHxIEBJctjY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KZ0MOWz5dPi4nfaiJ/qxcfisOwGNoXoaE7PMpoD4i/it5yDjtFbd8n6bON8CZRJIxzcX9TdgoaDudjmzciq7bKTxRfa7/8kauMCoi9V4+j085RnGrWvKJqQ2kR1w9xyKRYSvf/XpjwilDqX8DpdA2a4RRC7hlnSj2AG7pRo0RuU= 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=e94d2SOy; arc=none smtp.client-ip=209.85.221.47 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="e94d2SOy" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-37d518f9abcso3307756f8f.2 for ; Tue, 15 Oct 2024 18:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040654; x=1729645454; 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=ChnIuUCSP0kqnENSs3HRLRGEcxuQeK/etR6vsQ0JSww=; b=e94d2SOykI4tZyMN+y7l6tD3n+l+NMgTzN+81qtZQ0MqQq9CgYbtBBlyrX9gDG68D4 7F2CbCe82SaJRStKoQ3pL+3gPaUSfSfGZ2xmCwcyeJxDOrwMV5bqT7fpp0OE77Wlstma pxow/oiXd3Bwhu90s+S9GlYjqjU7nz897HIyiWv/C+Md13oN4Mx8eJFsPobIeOWhdrGY 8zh7vBIBGXhfukzL42p77+6cQqG+G6qnmTaQ2L3xN8kSXt+WK68rjG2xWUiL6SdVml14 yPTF6EXiHwHt+Ns/X/ay0EOW4eug914VlHZh9Q1foWzIDB6oZOdDEEd7d+CNVTCYTY8W 3RxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040654; x=1729645454; 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=ChnIuUCSP0kqnENSs3HRLRGEcxuQeK/etR6vsQ0JSww=; b=NyBQCux7WoSwlthL0R7qOFpwwmDDAdZ3Bv6xQAxSjOOkgqlJRvqNwlfJ0dmQsrm3Rx RsC18drsU+5Wp+ibuTZIwEWVqvlaw48LKbBONpR30FXGh64WDquv8z5lgywC569AdN+R y+ZDCQs0FWsoIDST34wN75ZjLbXhZMyJBdXroVZz1GocQZ2RfdrVExY+QCYQ+1xy1rDt QYLt2bt3EFSbYqCvl/2IrdvYaw2XPZWDihmfxpyqO3b6zn75WemW0sR7voJqUoww88D1 IMMDoiEfEn3/LER2+v38xNEMPxxPZWxlFcEXCX2HCW4UXq5uwmNjHL3Rzx3OOJHeeeN5 nu0A== X-Forwarded-Encrypted: i=1; AJvYcCXpCHuIxKFVkJmgsfY3ZTz6f7MteX6tTFr1l6YPR5dLvXGI8mW4sXeR/gnPn9A8SB3CkodRi2wi34ayl+M=@vger.kernel.org X-Gm-Message-State: AOJu0Yyr+7aV1tgNJRB4XwxMZeIcFpqt4v7KjXrdMi4L9TPaZJuougrk TreC+Op83zy4td3O5THhfJmDJn4h8Huvjnt85P3qsHKtvSQdhi+4R77+HtX7Kj8= X-Google-Smtp-Source: AGHT+IE7wXhjo5SnBaFKlMDPSjAWhWNI4DkLi1mcYGrN3mBifWUDVNxwRaCf0o64CPXroMk4Z5tP6A== X-Received: by 2002:adf:979a:0:b0:37d:4ebe:163e with SMTP id ffacd0b85a97d-37d86d69785mr1327510f8f.53.1729040653767; Tue, 15 Oct 2024 18:04:13 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:12 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:16 +0200 Subject: [PATCH net-next v9 16/23] ovpn: add support for updating local UDP endpoint 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: <20241016-b4-ovpn-v9-16-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2783; i=antonio@openvpn.net; h=from:subject:message-id; bh=wcrv5vDIBCgquenyyeflyML2Rh+TBSULYHxIEBJctjY=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDwdn3sHTKgsc8bpNPW+2syO0oFdCBTe2XHs nQ4ck4gTCmJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h5LdCACZYPdMqsOGmhv1PHvTJ+E30Nh8fsmf1xnVslUHiu4MAp+H6XpmhfCgrcq7YIYw4kSElBL 9LSXybx0wZ8gc8eAXs/cv9sSzraAj5eGOXgET1dPjb/iYJ7HypKk6VUpjPrsMZDNQV3RmfZBtST WG5G3+ppce02F2l/65t1VCOaKyYN95kW7xNsiXxRS76EZAsHj8DWN/9Jolfr7bVi7D64MVkcB0L R2KSm2zuVLjVJcHScbWv8+0PQAa8LUU7of1NKk2IdOrTP6Om0tTBNS1VdEsZqkc+XWGbqOYpBZK w1P0noN8yKK0c/K/6kHgm9aJTgJhlIj8Pi7kzBad4IQOKAaC X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C In case of UDP links, the local endpoint used to communicate with a given peer may change without a connection restart. Add support for learning the new address in case of change. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/peer.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 3 +++ 2 files changed, 48 insertions(+) diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index fa1883d12dfd6ce3e2ad1f9835f15aa7db5c8290..f3b20a561d614da98a8bee80272= 095b15bc6650d 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -416,6 +416,51 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_stru= ct *ovpn, u32 peer_id) return peer; } =20 +/** + * ovpn_peer_update_local_endpoint - update local endpoint for peer + * @peer: peer to update the endpoint for + * @skb: incoming packet to retrieve the destination address (local) from + */ +void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, + struct sk_buff *skb) +{ + struct ovpn_bind *bind; + + rcu_read_lock(); + bind =3D rcu_dereference(peer->bind); + if (unlikely(!bind)) + goto unlock; + + spin_lock_bh(&peer->lock); + switch (skb_protocol_to_family(skb)) { + case AF_INET: + if (unlikely(bind->local.ipv4.s_addr !=3D ip_hdr(skb)->daddr)) { + netdev_dbg(peer->ovpn->dev, + "%s: learning local IPv4 for peer %d (%pI4 -> %pI4)\n", + __func__, peer->id, &bind->local.ipv4.s_addr, + &ip_hdr(skb)->daddr); + bind->local.ipv4.s_addr =3D ip_hdr(skb)->daddr; + } + break; + case AF_INET6: + if (unlikely(!ipv6_addr_equal(&bind->local.ipv6, + &ipv6_hdr(skb)->daddr))) { + netdev_dbg(peer->ovpn->dev, + "%s: learning local IPv6 for peer %d (%pI6c -> %pI6c\n", + __func__, peer->id, &bind->local.ipv6, + &ipv6_hdr(skb)->daddr); + bind->local.ipv6 =3D ipv6_hdr(skb)->daddr; + } + break; + default: + break; + } + spin_unlock_bh(&peer->lock); + +unlock: + rcu_read_unlock(); +} + /** * ovpn_peer_get_by_dst - Lookup peer to send skb to * @ovpn: the private data representing the current VPN session diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 952927ae78a3ab753aaf2c6cc6f77121bdac34be..1a8638d266b11a4a80ee2f08839= 4d47a7798c3af 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -152,4 +152,7 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, s= truct sk_buff *skb, void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 tim= eout); void ovpn_peer_keepalive_work(struct work_struct *work); =20 +void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, + struct sk_buff *skb); + #endif /* _NET_OVPN_OVPNPEER_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.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 5C9B21C07EA for ; Wed, 16 Oct 2024 01:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040661; cv=none; b=YKevvbo9gOWMM+dQYPdwThrofNu1hJ+vmCwvJoDLtnVlv35p/1dKBtoMQhj3i7uHAmt0VNyiN/BwKaze/K6R4gaxn5jT+3Zg/e8ymS6fZaUSoINAyWJAFVlZR/3Zg95mD8MgLCmMP/qiAlmetfYSM4fGMoVrFUuxHymJOhytcwc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040661; c=relaxed/simple; bh=SY3TCdP0NF7kMR4g37jWPl3RyDYOMbdiDBRkOrn0Xkc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lVw8D4Kw8jGQfLHjJCNsKNTYD1+2fsDJkuJ8sZIHT1eXctKETdmM1559fmIVfsRS2/jbVb2tu9wj4sbiI/oyDtquz5nlO+C39yQqmJvtD68mAigOmUgCl8sHf304P3fY2o6EChLwxmuDJ2V0H0OkWi/JRiSO3gs5NcQfnjy/Z9s= 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=hOoewSjp; arc=none smtp.client-ip=209.85.221.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="hOoewSjp" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-37d70df0b1aso2276108f8f.3 for ; Tue, 15 Oct 2024 18:04:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040657; x=1729645457; 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=jZM55HSOTJuT4mvFK4MBi2MjDdJ3YZMBF8e95H5jr4U=; b=hOoewSjp/PYMJVxA7kDHzPEURb8kerj3rp4Jc2t8p58Ntqs3ciNrZ1sdBQxS5/IB3j DlRxk16U4ZLhzLPHJgnB5445DTPVPUKzHg1hVwvDZMKtd8tT84giAK1kpMBKs1QNvK5c RYWH9rgzxLtRXJfBqvv+PMB4KYWHJPaMXK8jw6+rfsfHAJ4IlYWYwG1y+grU4C6ye5yf sn+Z0/lGUHz6yTKGCr6bwI5eImfJUiMOJOTmbDurx+s2aMDLWhafYgo4n5IXvrObSpYD W9FMU76bd32WkTYT/9f/iIZTuIzYy0oSPOxngNKun/EL9j4J+xTtTzZqAIn9q7o0Zo8f LGmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040657; x=1729645457; 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=jZM55HSOTJuT4mvFK4MBi2MjDdJ3YZMBF8e95H5jr4U=; b=wGinHTCOQrUdFSfdHLOjcOIWc4O73cYxWRRH5Z9eWkU1GUeGP9x8/F4JHImIbeXBLd hlkFjsJVEz6Garz8BoxpRG4vAE3IglcvIsl3ToYGKXySd4//51YCztPl8daX9+Pviir3 ojYIoFaaOTZWKPmPcykdggukjrDKiebildeeORwQXZMHC6o076lT7bG34d7n+nfHRZy4 41WM0oLOfPguiQaUMWaDD+edbmcCSWnF8MHmWFWphrisVIunWL1GKaAn0BUWTNX55/ce Cn6PuqyMgQEDqP+7qLfez9QOZpExo5/usl2eeUUGbCTQ0ofBjVks1InzsofwzkKKXXyc wQBw== X-Forwarded-Encrypted: i=1; AJvYcCU/b5p8NG++zWNnUOL2ucRzEbIKIAboz6Rx0Wc+SlsS/pIVrc+0r3agQh6AquJdVO2xFRIsGsbm3CKMlO0=@vger.kernel.org X-Gm-Message-State: AOJu0YzEL0NdUJxVwrDa4DWGoJ9kUQJgMsrTkPyid94nAf+BsdK2KRsh vt7nwxyzc5AO4BPTIDl8gXgWCDxmiYKwA3aedNn9+vfC5LFPuKPVc6RgmkENmUo= X-Google-Smtp-Source: AGHT+IEHNCrmfMjvc+CWDM2qtFpZfqiOHDDg1SVLXZOpeuGbTYaqTlMiRiVN/ZpDloz0+T4oeiDq9Q== X-Received: by 2002:a5d:444f:0:b0:37d:525d:5a60 with SMTP id ffacd0b85a97d-37d86bb6707mr1176798f8f.8.1729040656718; Tue, 15 Oct 2024 18:04:16 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:15 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:17 +0200 Subject: [PATCH net-next v9 17/23] ovpn: add support for peer floating 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: <20241016-b4-ovpn-v9-17-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6779; i=antonio@openvpn.net; h=from:subject:message-id; bh=SY3TCdP0NF7kMR4g37jWPl3RyDYOMbdiDBRkOrn0Xkc=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDwQU0vFcndi20RyanZbYXSZ7N9SNiwIPS8e Bl5uJicTnuJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h8CFB/9/EQTOqELMdAI3jl0b8ycQfqgeuRlTaDAl2h2/z37m7hgq5HlOREzfwjODTh+XNyUR/dR G3hKVM56+4FUMB01XTN8f5mr70u8whx1eKFB5eLgeWasVl678pVcwxAwuiGf57m7CJNKrre5vAA AcCCnZ+SilCEMMP/92Q/2u8R+YTnRz+HNtiq9/CdYozwdj6Al3LtTNqk+6sfmSlybb1zxeVBSbs W/2TZG2hNx9dZyPi8U2rdNx+kM0oBV1c8ibPpuuP5HT7LeS233GhFbDhpWKw7jYKGyzL8toNxYr IoQgxuKmq+561QTCF+kaecIP0yLiF/8Eh2OytIX2F1RbXmlF X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C A peer connected via UDP may change its IP address without reconnecting (float). Add support for detecting and updating the new peer IP/port in case of floating. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/bind.c | 10 ++-- drivers/net/ovpn/io.c | 9 ++++ drivers/net/ovpn/peer.c | 126 ++++++++++++++++++++++++++++++++++++++++++++= ++-- drivers/net/ovpn/peer.h | 2 + 4 files changed, 136 insertions(+), 11 deletions(-) diff --git a/drivers/net/ovpn/bind.c b/drivers/net/ovpn/bind.c index b4d2ccec2ceddf43bc445b489cc62a578ef0ad0a..d17d078c5730bf4336dc87f45cd= ba3f6b8cad770 100644 --- a/drivers/net/ovpn/bind.c +++ b/drivers/net/ovpn/bind.c @@ -47,12 +47,8 @@ struct ovpn_bind *ovpn_bind_from_sockaddr(const struct s= ockaddr_storage *ss) * @new: the new bind to assign */ void ovpn_bind_reset(struct ovpn_peer *peer, struct ovpn_bind *new) + __must_hold(&peer->lock) { - struct ovpn_bind *old; - - spin_lock_bh(&peer->lock); - old =3D rcu_replace_pointer(peer->bind, new, true); - spin_unlock_bh(&peer->lock); - - kfree_rcu(old, rcu); + kfree_rcu(rcu_replace_pointer(peer->bind, new, + lockdep_is_held(&peer->lock)), rcu); } diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index 1c4cdbb16df4acc5c87707919c671f7a84bf4295..c857ac9015b75530cf3ae40f8db= 0d4db4c08e2fa 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -135,6 +135,15 @@ void ovpn_decrypt_post(void *data, int ret) /* keep track of last received authenticated packet for keepalive */ peer->last_recv =3D ktime_get_real_seconds(); =20 + if (peer->sock->sock->sk->sk_protocol =3D=3D IPPROTO_UDP) { + /* check if this peer changed it's IP address and update + * state + */ + ovpn_peer_float(peer, skb); + /* update source endpoint for this peer */ + ovpn_peer_update_local_endpoint(peer, skb); + } + /* point to encapsulated IP packet */ __skb_pull(skb, payload_offset); =20 diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index f3b20a561d614da98a8bee80272095b15bc6650d..0718051bb57d2e874e0a4e873ee= 24ab88eb3a38c 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -94,6 +94,128 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovp= n, u32 id) return peer; } =20 +/** + * ovpn_peer_reset_sockaddr - recreate binding for peer + * @peer: peer to recreate the binding for + * @ss: sockaddr to use as remote endpoint for the binding + * @local_ip: local IP for the binding + * + * Return: 0 on success or a negative error code otherwise + */ +static int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, + const struct sockaddr_storage *ss, + const u8 *local_ip) + __must_hold(&peer->lock) +{ + struct ovpn_bind *bind; + size_t ip_len; + + /* create new ovpn_bind object */ + bind =3D ovpn_bind_from_sockaddr(ss); + if (IS_ERR(bind)) + return PTR_ERR(bind); + + if (local_ip) { + if (ss->ss_family =3D=3D AF_INET) { + ip_len =3D sizeof(struct in_addr); + } else if (ss->ss_family =3D=3D AF_INET6) { + ip_len =3D sizeof(struct in6_addr); + } else { + netdev_dbg(peer->ovpn->dev, "%s: invalid family for remote endpoint\n", + __func__); + kfree(bind); + return -EINVAL; + } + + memcpy(&bind->local, local_ip, ip_len); + } + + /* set binding */ + ovpn_bind_reset(peer, bind); + + return 0; +} + +#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl =3D &(_tbl); \ + (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ + +/** + * ovpn_peer_float - update remote endpoint for peer + * @peer: peer to update the remote endpoint for + * @skb: incoming packet to retrieve the source address (remote) from + */ +void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb) +{ + struct hlist_nulls_head *nhead; + struct sockaddr_storage ss; + const u8 *local_ip =3D NULL; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa; + struct ovpn_bind *bind; + sa_family_t family; + size_t salen; + + rcu_read_lock(); + bind =3D rcu_dereference(peer->bind); + if (unlikely(!bind)) { + rcu_read_unlock(); + return; + } + + spin_lock_bh(&peer->lock); + if (likely(ovpn_bind_skb_src_match(bind, skb))) + goto unlock; + + family =3D skb_protocol_to_family(skb); + + if (bind->remote.in4.sin_family =3D=3D family) + local_ip =3D (u8 *)&bind->local; + + switch (family) { + case AF_INET: + sa =3D (struct sockaddr_in *)&ss; + sa->sin_family =3D AF_INET; + sa->sin_addr.s_addr =3D ip_hdr(skb)->saddr; + sa->sin_port =3D udp_hdr(skb)->source; + salen =3D sizeof(*sa); + break; + case AF_INET6: + sa6 =3D (struct sockaddr_in6 *)&ss; + sa6->sin6_family =3D AF_INET6; + sa6->sin6_addr =3D ipv6_hdr(skb)->saddr; + sa6->sin6_port =3D udp_hdr(skb)->source; + sa6->sin6_scope_id =3D ipv6_iface_scope_id(&ipv6_hdr(skb)->saddr, + skb->skb_iif); + salen =3D sizeof(*sa6); + break; + default: + goto unlock; + } + + netdev_dbg(peer->ovpn->dev, "%s: peer %d floated to %pIScp", __func__, + peer->id, &ss); + ovpn_peer_reset_sockaddr(peer, (struct sockaddr_storage *)&ss, + local_ip); + + /* rehashing is required only in MP mode as P2P has one peer + * only and thus there is no hashtable + */ + if (peer->ovpn->mode =3D=3D OVPN_MODE_MP) { + spin_lock_bh(&peer->ovpn->peers->lock); + /* remove old hashing */ + hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); + /* re-add with new transport address */ + nhead =3D ovpn_get_hash_head(peer->ovpn->peers->by_transp_addr, + &ss, salen); + hlist_nulls_add_head_rcu(&peer->hash_entry_transp_addr, nhead); + spin_unlock_bh(&peer->ovpn->peers->lock); + } +unlock: + spin_unlock_bh(&peer->lock); + rcu_read_unlock(); +} + static void ovpn_peer_release(struct ovpn_peer *peer) { if (peer->sock) @@ -186,10 +308,6 @@ static struct in6_addr ovpn_nexthop_from_skb6(struct s= k_buff *skb) return rt->rt6i_gateway; } =20 -#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ - typeof(_tbl) *__tbl =3D &(_tbl); \ - (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ - /** * ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address * @ovpn: the openvpn instance to search diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 1a8638d266b11a4a80ee2f088394d47a7798c3af..940cea5372ec0375cfe3e673154= a1e0248978409 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -155,4 +155,6 @@ void ovpn_peer_keepalive_work(struct work_struct *work); void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, struct sk_buff *skb); =20 +void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb); + #endif /* _NET_OVPN_OVPNPEER_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 90E7F1C175C for ; Wed, 16 Oct 2024 01:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040663; cv=none; b=oNanvYT+K/mD5kxdqC+h0r3Q1+bkZNRqer9BfHShlmhdJGWO9GPHbaCrG27u5bvasM9zTJjS2Pz91hzahF7pIO7FrGexBra2YicXj41GLlgTON3d6KRPxtTOLBXAhOxqKuch34t4u9R2mi2YLDd7nggjIm5ct5rHa+lPj7NZ47M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040663; c=relaxed/simple; bh=1X1pyGCmjmcp4p3fdPKDpe1pjIAy/MiZFba+9hJM2O4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=irGyxL/88YBIsEe/jgQ11FzatHAB8iVhtD0eO3nL0WtvGfiAdrULO5hn3BJpxCU8GKWo/+o4fR4PGtAKYCPvR4RNvjktehA4gPmqD3+BZAKVvCBvnGTl1haJj7tA36UJfrOPepUSt8fyYt/kvMWNJab+PA59nynl+OBysEnZKBg= 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=LlWDoLmH; arc=none smtp.client-ip=209.85.221.52 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="LlWDoLmH" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-37d4ac91d97so5104873f8f.2 for ; Tue, 15 Oct 2024 18:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040658; x=1729645458; 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=SqSffse27mBph1p38OnxJbfdaXcQLqBR1SsKwybzkVU=; b=LlWDoLmHR1gD+Ve0lSkUQo7WNhUaKxcrIRRNgVDU1gV2OMs3x5gCuWJlzA1RG5Lr80 dlhLd7IztQxfYkCuzpfkQ7VW+USKeG1KsOFLhVoPFyAuYhSW4YEd+rAGhQeTNh2UTA3H 8HTlUk7Sc1msolZjvM8wfaPaHWD2Q6952nWTSMa90Ho68SCB/b4hsj6uWSpIJpb8o/Qa kig6IkftgbeYsOyk9U/deBQM+XgdUnJbvC4yidE1fwdVTjpqrvqpDhehthx4aTriDnh/ FTuR5Ze1HlUIDaJ4txI1pU6lRwUF8y3PsqGJXehFahNtam1b4697g2jvx/yXBmKguB07 KBXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040658; x=1729645458; 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=SqSffse27mBph1p38OnxJbfdaXcQLqBR1SsKwybzkVU=; b=Bxt0Gkwu0GhIa9V3WBkSwQYroPXLWbEVAYgQxFW+hYCr2TK/j8NfZ8dQCzWBQtbZN2 hPabd8rj7ZVqk/Etj3N23yaPgxG25weHPNF2qONn6N8o2XlPlSE9W+lU1Vi9THTU9KVA BDKDGSVB8twvQFsD+3UxHTvrp10/ejlDvUCCVtEjOEEttPYb5BlisNovZ/MBhDJ92E0l EIwLWr8akrzzkUSBC3Gcsk25gxtjQ0dWcm8dyC14aKYFhix3viFsMBL/nQzrH0XaKY5W //ZlYHQfJTxsBINgG6xUwoZwSYDzl+GXaZK8ejXzwNCjf0fKjCduSuEnN6VUNwqBRlvq bm/Q== X-Forwarded-Encrypted: i=1; AJvYcCXF72nhrZaJvcx3xGu2It6FqndwILfgJIfVY3Pctx/C7+H5TW+2WpUXxAQotnWMsGYO3Q1pv1YgDlZOao0=@vger.kernel.org X-Gm-Message-State: AOJu0YzD07AHzVU7IvMlfLcxLvyqK3LJVxZY4l2y3C8isUCnEAuQyBCJ iIoE1uMuGHHbSmShi8smsGmTlaPZ6tjcYRZRXyh4ke8+rR7RvJyR41VmcqBUEp8= X-Google-Smtp-Source: AGHT+IHnBBXuRoNLFOAwdQQsOVR1ZAMoVz06nE5dQs43ZcdClTajpNYvlqnTNozsJnBmIraWg5gAsg== X-Received: by 2002:adf:9bd2:0:b0:37d:51b7:5e08 with SMTP id ffacd0b85a97d-37d5ff5a4cemr11969225f8f.18.1729040657835; Tue, 15 Oct 2024 18:04:17 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:17 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:18 +0200 Subject: [PATCH net-next v9 18/23] ovpn: implement peer add/dump/delete via netlink 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: <20241016-b4-ovpn-v9-18-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=22439; i=antonio@openvpn.net; h=from:subject:message-id; bh=1X1pyGCmjmcp4p3fdPKDpe1pjIAy/MiZFba+9hJM2O4=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDwdX+1vRp33WjeP9aQfmXtPIOI95S4wFIpm 2n1PtESAROJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h8n6B/9tRIXS8YcOUwpd0AKfmIUdx7DD+t0+4Hop5zigL+qF/zzOw5Ot7GAxWU/YnyBEMr9UcTL jZvCpEZvJ3mnIHFVAIwWLHBx9JjAxTiQJnxK+LlsFzrqwCqVkCbg5UG5V4qLd03ImGxFX3c6Dlf a1vDQhXCZJG6+MsYauRh2cvPYpfcXr4oT8a9JF04V7qmDkv0O2Qc8XKualWNkxfq9DcteIKwEcA 5G4mXRW5HZ73R22wQHlDHd+QzoGa9ilLq/Ifnb31KG1JuPJQTAHE8UjZL7USVwXqKFEl1wJxQI8 SM+DWY83rK5yIK2pkKl9FW0od1dKXW70aFz/s3ducbJlKdyU X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C This change introduces the netlink command needed to add, delete and retrieve/dump known peers. Userspace is expected to use these commands to handle known peer lifecycles. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/netlink.c | 578 +++++++++++++++++++++++++++++++++++++++++= +++- drivers/net/ovpn/peer.c | 48 ++-- drivers/net/ovpn/peer.h | 5 + 3 files changed, 609 insertions(+), 22 deletions(-) diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c index 2cc34eb1d1d870c6705714cb971c3c5dfb04afda..d504445325ef82db04f87367c85= 8adaf025f6297 100644 --- a/drivers/net/ovpn/netlink.c +++ b/drivers/net/ovpn/netlink.c @@ -7,6 +7,7 @@ */ =20 #include +#include #include =20 #include @@ -16,6 +17,10 @@ #include "io.h" #include "netlink.h" #include "netlink-gen.h" +#include "bind.h" +#include "packet.h" +#include "peer.h" +#include "socket.h" =20 MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME); =20 @@ -86,29 +91,592 @@ void ovpn_nl_post_doit(const struct genl_split_ops *op= s, struct sk_buff *skb, netdev_put(ovpn->dev, &ovpn->dev_tracker); } =20 +static int ovpn_nl_attr_sockaddr_remote(struct nlattr **attrs, + struct sockaddr_storage *ss) +{ + struct sockaddr_in6 *sin6; + struct sockaddr_in *sin; + struct in6_addr *in6; + __be16 port =3D 0; + __be32 *in; + int af; + + ss->ss_family =3D AF_UNSPEC; + + if (attrs[OVPN_A_PEER_REMOTE_PORT]) + port =3D nla_get_be16(attrs[OVPN_A_PEER_REMOTE_PORT]); + + if (attrs[OVPN_A_PEER_REMOTE_IPV4]) { + af =3D AF_INET; + ss->ss_family =3D AF_INET; + in =3D nla_data(attrs[OVPN_A_PEER_REMOTE_IPV4]); + } else if (attrs[OVPN_A_PEER_REMOTE_IPV6]) { + af =3D AF_INET6; + ss->ss_family =3D AF_INET6; + in6 =3D nla_data(attrs[OVPN_A_PEER_REMOTE_IPV6]); + } else { + return AF_UNSPEC; + } + + switch (ss->ss_family) { + case AF_INET6: + /* If this is a regular IPv6 just break and move on, + * otherwise switch to AF_INET and extract the IPv4 accordingly + */ + if (!ipv6_addr_v4mapped(in6)) { + sin6 =3D (struct sockaddr_in6 *)ss; + sin6->sin6_port =3D port; + memcpy(&sin6->sin6_addr, in6, sizeof(*in6)); + break; + } + + /* v4-mapped-v6 address */ + ss->ss_family =3D AF_INET; + in =3D &in6->s6_addr32[3]; + fallthrough; + case AF_INET: + sin =3D (struct sockaddr_in *)ss; + sin->sin_port =3D port; + sin->sin_addr.s_addr =3D *in; + break; + } + + /* don't return ss->ss_family as it may have changed in case of + * v4-mapped-v6 address + */ + return af; +} + +static u8 *ovpn_nl_attr_local_ip(struct nlattr **attrs) +{ + u8 *addr6; + + if (!attrs[OVPN_A_PEER_LOCAL_IPV4] && !attrs[OVPN_A_PEER_LOCAL_IPV6]) + return NULL; + + if (attrs[OVPN_A_PEER_LOCAL_IPV4]) + return nla_data(attrs[OVPN_A_PEER_LOCAL_IPV4]); + + addr6 =3D nla_data(attrs[OVPN_A_PEER_LOCAL_IPV6]); + /* this is an IPv4-mapped IPv6 address, therefore extract the actual + * v4 address from the last 4 bytes + */ + if (ipv6_addr_v4mapped((struct in6_addr *)addr6)) + return addr6 + 12; + + return addr6; +} + +static int ovpn_nl_peer_precheck(struct ovpn_struct *ovpn, + struct genl_info *info, + struct nlattr **attrs) +{ + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs, + OVPN_A_PEER_ID)) + return -EINVAL; + + if (attrs[OVPN_A_PEER_REMOTE_IPV4] && attrs[OVPN_A_PEER_REMOTE_IPV6]) { + NL_SET_ERR_MSG_MOD(info->extack, + "cannot specify both remote IPv4 or IPv6 address"); + return -EINVAL; + } + + if (!attrs[OVPN_A_PEER_REMOTE_IPV4] && + !attrs[OVPN_A_PEER_REMOTE_IPV6] && attrs[OVPN_A_PEER_REMOTE_PORT]) { + NL_SET_ERR_MSG_MOD(info->extack, + "cannot specify remote port without IP address"); + return -EINVAL; + } + + if (!attrs[OVPN_A_PEER_REMOTE_IPV4] && + attrs[OVPN_A_PEER_LOCAL_IPV4]) { + NL_SET_ERR_MSG_MOD(info->extack, + "cannot specify local IPv4 address without remote"); + return -EINVAL; + } + + if (!attrs[OVPN_A_PEER_REMOTE_IPV6] && + attrs[OVPN_A_PEER_LOCAL_IPV6]) { + NL_SET_ERR_MSG_MOD(info->extack, + "cannot specify local IPV6 address without remote"); + return -EINVAL; + } + + if (!attrs[OVPN_A_PEER_REMOTE_IPV6] && + attrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID]) { + NL_SET_ERR_MSG_MOD(info->extack, + "cannot specify scope id without remote IPv6 address"); + return -EINVAL; + } + + /* VPN IPs are needed only in MP mode for selecting the right peer */ + if (ovpn->mode =3D=3D OVPN_MODE_P2P && (attrs[OVPN_A_PEER_VPN_IPV4] || + attrs[OVPN_A_PEER_VPN_IPV6])) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "VPN IP unexpected in P2P mode"); + return -EINVAL; + } + + if ((attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] && + !attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) || + (!attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT])) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "keepalive interval and timeout are required together"); + return -EINVAL; + } + + return 0; +} + +/** + * ovpn_nl_peer_modify - modify the peer attributes according to the incom= ing msg + * @peer: the peer to modify + * @info: generic netlink info from the user request + * @attrs: the attributes from the user request + * + * Return: a negative error code in case of failure, 0 on success or 1 on + * success and the VPN IPs have been modified (requires rehashing in MP + * mode) + */ +static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *i= nfo, + struct nlattr **attrs) +{ + struct sockaddr_storage ss =3D {}; + u32 sockfd, interv, timeout; + struct socket *sock =3D NULL; + u8 *local_ip =3D NULL; + bool rehash =3D false; + int ret; + + if (attrs[OVPN_A_PEER_SOCKET]) { + /* lookup the fd in the kernel table and extract the socket + * object + */ + sockfd =3D nla_get_u32(attrs[OVPN_A_PEER_SOCKET]); + /* sockfd_lookup() increases sock's refcounter */ + sock =3D sockfd_lookup(sockfd, &ret); + if (!sock) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot lookup peer socket (fd=3D%u): %d", + sockfd, ret); + return -ENOTSOCK; + } + + /* Only when using UDP as transport protocol the remote endpoint + * can be configured so that ovpn knows where to send packets + * to. + * + * In case of TCP, the socket is connected to the peer and ovpn + * will just send bytes over it, without the need to specify a + * destination. + */ + if (sock->sk->sk_protocol !=3D IPPROTO_UDP && + (attrs[OVPN_A_PEER_REMOTE_IPV4] || + attrs[OVPN_A_PEER_REMOTE_IPV6])) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "unexpected remote IP address for non UDP socket"); + sockfd_put(sock); + return -EINVAL; + } + + if (peer->sock) + ovpn_socket_put(peer->sock); + + peer->sock =3D ovpn_socket_new(sock, peer); + if (IS_ERR(peer->sock)) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot encapsulate socket: %ld", + PTR_ERR(peer->sock)); + sockfd_put(sock); + peer->sock =3D NULL; + return -ENOTSOCK; + } + } + + if (ovpn_nl_attr_sockaddr_remote(attrs, &ss) !=3D AF_UNSPEC) { + /* we carry the local IP in a generic container. + * ovpn_peer_reset_sockaddr() will properly interpret it + * based on ss.ss_family + */ + local_ip =3D ovpn_nl_attr_local_ip(attrs); + + spin_lock_bh(&peer->lock); + /* set peer sockaddr */ + ret =3D ovpn_peer_reset_sockaddr(peer, &ss, local_ip); + if (ret < 0) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot set peer sockaddr: %d", + ret); + spin_unlock_bh(&peer->lock); + return ret; + } + spin_unlock_bh(&peer->lock); + } + + if (attrs[OVPN_A_PEER_VPN_IPV4]) { + rehash =3D true; + peer->vpn_addrs.ipv4.s_addr =3D + nla_get_in_addr(attrs[OVPN_A_PEER_VPN_IPV4]); + } + + if (attrs[OVPN_A_PEER_VPN_IPV6]) { + rehash =3D true; + peer->vpn_addrs.ipv6 =3D + nla_get_in6_addr(attrs[OVPN_A_PEER_VPN_IPV6]); + } + + /* when setting the keepalive, both parameters have to be configured */ + if (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) { + interv =3D nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL]); + timeout =3D nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]); + ovpn_peer_keepalive_set(peer, interv, timeout); + } + + netdev_dbg(peer->ovpn->dev, + "%s: peer id=3D%u endpoint=3D%pIScp/%s VPN-IPv4=3D%pI4 VPN-IPv6=3D%pI= 6c\n", + __func__, peer->id, &ss, + peer->sock->sock->sk->sk_prot_creator->name, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + + return rehash ? 1 : 0; +} + int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_PEER_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER], + ovpn_peer_nl_policy, info->extack); + if (ret) + return ret; + + ret =3D ovpn_nl_peer_precheck(ovpn, info, attrs); + if (ret < 0) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs, + OVPN_A_PEER_SOCKET)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_PEER_ID]); + peer =3D ovpn_peer_new(ovpn, peer_id); + if (IS_ERR(peer)) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot create new peer object for peer %u: %ld", + peer_id, PTR_ERR(peer)); + return PTR_ERR(peer); + } + + ret =3D ovpn_nl_peer_modify(peer, info, attrs); + if (ret < 0) + goto peer_release; + + ret =3D ovpn_peer_add(ovpn, peer); + if (ret < 0) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot add new peer (id=3D%u) to hashtable: %d\n", + peer->id, ret); + goto peer_release; + } + + return 0; + +peer_release: + /* release right away because peer is not used in any context */ + ovpn_peer_release(peer); + + return ret; } =20 int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_PEER_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER], + ovpn_peer_nl_policy, info->extack); + if (ret) + return ret; + + ret =3D ovpn_nl_peer_precheck(ovpn, info, attrs); + if (ret < 0) + return ret; + + peer_id =3D nla_get_u32(attrs[OVPN_A_PEER_ID]); + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) + return -ENOENT; + + ret =3D ovpn_nl_peer_modify(peer, info, attrs); + if (ret < 0) { + ovpn_peer_put(peer); + return ret; + } + + /* ret =3D=3D 1 means that VPN IPv4/6 has been modified and rehashing + * is required + */ + if (ret > 0) { + spin_lock_bh(&ovpn->peers->lock); + ovpn_peer_hash_vpn_ip(peer); + spin_unlock_bh(&ovpn->peers->lock); + } + + ovpn_peer_put(peer); + + return 0; +} + +static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *= info, + const struct ovpn_peer *peer, u32 portid, u32 seq, + int flags) +{ + const struct ovpn_bind *bind; + struct nlattr *attr; + void *hdr; + + hdr =3D genlmsg_put(skb, portid, seq, &ovpn_nl_family, flags, + OVPN_CMD_PEER_GET); + if (!hdr) + return -ENOBUFS; + + attr =3D nla_nest_start(skb, OVPN_A_PEER); + if (!attr) + goto err; + + if (nla_put_u32(skb, OVPN_A_PEER_ID, peer->id)) + goto err; + + if (peer->vpn_addrs.ipv4.s_addr !=3D htonl(INADDR_ANY)) + if (nla_put_in_addr(skb, OVPN_A_PEER_VPN_IPV4, + peer->vpn_addrs.ipv4.s_addr)) + goto err; + + if (!ipv6_addr_equal(&peer->vpn_addrs.ipv6, &in6addr_any)) + if (nla_put_in6_addr(skb, OVPN_A_PEER_VPN_IPV6, + &peer->vpn_addrs.ipv6)) + goto err; + + if (nla_put_u32(skb, OVPN_A_PEER_KEEPALIVE_INTERVAL, + peer->keepalive_interval) || + nla_put_u32(skb, OVPN_A_PEER_KEEPALIVE_TIMEOUT, + peer->keepalive_timeout)) + goto err; + + rcu_read_lock(); + bind =3D rcu_dereference(peer->bind); + if (bind) { + if (bind->remote.in4.sin_family =3D=3D AF_INET) { + if (nla_put_in_addr(skb, OVPN_A_PEER_REMOTE_IPV4, + bind->remote.in4.sin_addr.s_addr) || + nla_put_net16(skb, OVPN_A_PEER_REMOTE_PORT, + bind->remote.in4.sin_port) || + nla_put_in_addr(skb, OVPN_A_PEER_LOCAL_IPV4, + bind->local.ipv4.s_addr)) + goto err_unlock; + } else if (bind->remote.in4.sin_family =3D=3D AF_INET6) { + if (nla_put_in6_addr(skb, OVPN_A_PEER_REMOTE_IPV6, + &bind->remote.in6.sin6_addr) || + nla_put_u32(skb, OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID, + bind->remote.in6.sin6_scope_id) || + nla_put_net16(skb, OVPN_A_PEER_REMOTE_PORT, + bind->remote.in6.sin6_port) || + nla_put_in6_addr(skb, OVPN_A_PEER_LOCAL_IPV6, + &bind->local.ipv6)) + goto err_unlock; + } + } + rcu_read_unlock(); + + if (nla_put_net16(skb, OVPN_A_PEER_LOCAL_PORT, + inet_sk(peer->sock->sock->sk)->inet_sport) || + /* VPN RX stats */ + nla_put_uint(skb, OVPN_A_PEER_VPN_RX_BYTES, + atomic64_read(&peer->vpn_stats.rx.bytes)) || + nla_put_uint(skb, OVPN_A_PEER_VPN_RX_PACKETS, + atomic64_read(&peer->vpn_stats.rx.packets)) || + /* VPN TX stats */ + nla_put_uint(skb, OVPN_A_PEER_VPN_TX_BYTES, + atomic64_read(&peer->vpn_stats.tx.bytes)) || + nla_put_uint(skb, OVPN_A_PEER_VPN_TX_PACKETS, + atomic64_read(&peer->vpn_stats.tx.packets)) || + /* link RX stats */ + nla_put_uint(skb, OVPN_A_PEER_LINK_RX_BYTES, + atomic64_read(&peer->link_stats.rx.bytes)) || + nla_put_uint(skb, OVPN_A_PEER_LINK_RX_PACKETS, + atomic64_read(&peer->link_stats.rx.packets)) || + /* link TX stats */ + nla_put_uint(skb, OVPN_A_PEER_LINK_TX_BYTES, + atomic64_read(&peer->link_stats.tx.bytes)) || + nla_put_uint(skb, OVPN_A_PEER_LINK_TX_PACKETS, + atomic64_read(&peer->link_stats.tx.packets))) + goto err; + + nla_nest_end(skb, attr); + genlmsg_end(skb, hdr); + + return 0; +err_unlock: + rcu_read_unlock(); +err: + genlmsg_cancel(skb, hdr); + return -EMSGSIZE; } =20 int ovpn_nl_peer_get_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_PEER_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct ovpn_peer *peer; + struct sk_buff *msg; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER], + ovpn_peer_nl_policy, info->extack); + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs, + OVPN_A_PEER_ID)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_PEER_ID]); + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot find peer with id %u", peer_id); + return -ENOENT; + } + + msg =3D nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!msg) { + ret =3D -ENOMEM; + goto err; + } + + ret =3D ovpn_nl_send_peer(msg, info, peer, info->snd_portid, + info->snd_seq, 0); + if (ret < 0) { + nlmsg_free(msg); + goto err; + } + + ret =3D genlmsg_reply(msg, info); +err: + ovpn_peer_put(peer); + return ret; } =20 int ovpn_nl_peer_get_dumpit(struct sk_buff *skb, struct netlink_callback *= cb) { - return -EOPNOTSUPP; + const struct genl_info *info =3D genl_info_dump(cb); + int bkt, last_idx =3D cb->args[1], dumped =3D 0; + struct ovpn_struct *ovpn; + struct ovpn_peer *peer; + + ovpn =3D ovpn_get_dev_from_attrs(sock_net(cb->skb->sk), info); + if (IS_ERR(ovpn)) + return PTR_ERR(ovpn); + + if (ovpn->mode =3D=3D OVPN_MODE_P2P) { + /* if we already dumped a peer it means we are done */ + if (last_idx) + goto out; + + rcu_read_lock(); + peer =3D rcu_dereference(ovpn->peer); + if (peer) { + if (ovpn_nl_send_peer(skb, info, peer, + NETLINK_CB(cb->skb).portid, + cb->nlh->nlmsg_seq, + NLM_F_MULTI) =3D=3D 0) + dumped++; + } + rcu_read_unlock(); + } else { + rcu_read_lock(); + hash_for_each_rcu(ovpn->peers->by_id, bkt, peer, + hash_entry_id) { + /* skip already dumped peers that were dumped by + * previous invocations + */ + if (last_idx > 0) { + last_idx--; + continue; + } + + if (ovpn_nl_send_peer(skb, info, peer, + NETLINK_CB(cb->skb).portid, + cb->nlh->nlmsg_seq, + NLM_F_MULTI) < 0) + break; + + /* count peers being dumped during this invocation */ + dumped++; + } + rcu_read_unlock(); + } + +out: + netdev_put(ovpn->dev, &ovpn->dev_tracker); + + /* sum up peers dumped in this message, so that at the next invocation + * we can continue from where we left + */ + cb->args[1] +=3D dumped; + return skb->len; } =20 int ovpn_nl_peer_del_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_PEER_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER], + ovpn_peer_nl_policy, info->extack); + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs, + OVPN_A_PEER_ID)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_PEER_ID]); + + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) + return -ENOENT; + + netdev_dbg(ovpn->dev, "%s: peer id=3D%u\n", __func__, peer->id); + ret =3D ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_USERSPACE); + ovpn_peer_put(peer); + + return ret; } =20 int ovpn_nl_key_new_doit(struct sk_buff *skb, struct genl_info *info) diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 0718051bb57d2e874e0a4e873ee24ab88eb3a38c..2e218e8cfacad753d1284808958= 234913f3ef59e 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -102,9 +102,9 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovp= n, u32 id) * * Return: 0 on success or a negative error code otherwise */ -static int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, - const struct sockaddr_storage *ss, - const u8 *local_ip) +int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, + const struct sockaddr_storage *ss, + const u8 *local_ip) __must_hold(&peer->lock) { struct ovpn_bind *bind; @@ -216,7 +216,7 @@ void ovpn_peer_float(struct ovpn_peer *peer, struct sk_= buff *skb) rcu_read_unlock(); } =20 -static void ovpn_peer_release(struct ovpn_peer *peer) +void ovpn_peer_release(struct ovpn_peer *peer) { if (peer->sock) ovpn_socket_put(peer->sock); @@ -760,6 +760,32 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, = struct sk_buff *skb, return match; } =20 +void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer) + __must_hold(&peer->ovpn->peers->lock) +{ + struct hlist_nulls_head *nhead; + + if (peer->vpn_addrs.ipv4.s_addr !=3D htonl(INADDR_ANY)) { + /* remove potential old hashing */ + hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); + + nhead =3D ovpn_get_hash_head(peer->ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv4, + sizeof(peer->vpn_addrs.ipv4)); + hlist_nulls_add_head_rcu(&peer->hash_entry_addr4, nhead); + } + + if (!ipv6_addr_any(&peer->vpn_addrs.ipv6)) { + /* remove potential old hashing */ + hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); + + nhead =3D ovpn_get_hash_head(peer->ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv6, + sizeof(peer->vpn_addrs.ipv6)); + hlist_nulls_add_head_rcu(&peer->hash_entry_addr6, nhead); + } +} + /** * ovpn_peer_add_mp - add peer to related tables in a MP instance * @ovpn: the instance to add the peer to @@ -821,19 +847,7 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, = struct ovpn_peer *peer) ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, sizeof(peer->id))); =20 - if (peer->vpn_addrs.ipv4.s_addr !=3D htonl(INADDR_ANY)) { - nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, - &peer->vpn_addrs.ipv4, - sizeof(peer->vpn_addrs.ipv4)); - hlist_nulls_add_head_rcu(&peer->hash_entry_addr4, nhead); - } - - if (!ipv6_addr_any(&peer->vpn_addrs.ipv6)) { - nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, - &peer->vpn_addrs.ipv6, - sizeof(peer->vpn_addrs.ipv6)); - hlist_nulls_add_head_rcu(&peer->hash_entry_addr6, nhead); - } + ovpn_peer_hash_vpn_ip(peer); out: spin_unlock_bh(&ovpn->peers->lock); return ret; diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h index 940cea5372ec0375cfe3e673154a1e0248978409..1adecd0f79f8f4a202110543223= fc448c09e5175 100644 --- a/drivers/net/ovpn/peer.h +++ b/drivers/net/ovpn/peer.h @@ -124,6 +124,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *pee= r) return kref_get_unless_zero(&peer->refcount); } =20 +void ovpn_peer_release(struct ovpn_peer *peer); void ovpn_peer_release_kref(struct kref *kref); =20 /** @@ -146,6 +147,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct o= vpn_struct *ovpn, struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d); struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb); +void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer); bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer); =20 @@ -156,5 +158,8 @@ void ovpn_peer_update_local_endpoint(struct ovpn_peer *= peer, struct sk_buff *skb); =20 void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb); +int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, + const struct sockaddr_storage *ss, + const u8 *local_ip); =20 #endif /* _NET_OVPN_OVPNPEER_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 213DB1C233A for ; Wed, 16 Oct 2024 01:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040665; cv=none; b=H3h/dXKSOQQiTWByfdgXliAoa5mgUeHYranOvkYvz1aUQY/gTeGYsX9MGVmXauTfrlEYVG9FFP+PJx5/MXpSbSJAQqagL36/8Za7MMlbh1O8r7AT7eoOftOnP762xLdr5PPDnfeEA+gs3CEfBRvv5j9o9Vwt76KXH4+VPsjVVZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040665; c=relaxed/simple; bh=uBijmJW2oOVCVWghW03aDeybZ37xU5VWTwEgFTeWMTM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pJ7cNc1JZsSzLvhl6FTxW4fLY2tsE/DtAs37JROxlAcqEseuQr9dfvnxQgqQGcsiikwQHqAm8lBmgAJYmoHdFvqeyLl5d7wwcjQLpv6RfM+WOvZkZfJhugLUuURj328929a1CSRYLCFjMtMibfU0Nluh/m2hSlRjiPKVh6pQNqE= 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=EGl6U2s6; arc=none smtp.client-ip=209.85.128.47 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="EGl6U2s6" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4311420b675so55014985e9.2 for ; Tue, 15 Oct 2024 18:04:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040660; x=1729645460; 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=vgBicxL465Iq2zDqEf/9aYEGLc+kPrkVMZ+FhEO9LmY=; b=EGl6U2s6EgXcVo8KUknuiIbDqxkylSc2ksQBzgfE5ZDNSjmDboklgX6RE7auYGcG+w KfsnW7zNhmi4DeNuF9cUsotwJ2aIrBDXh8bGdITqcW01prTE2Z+R1GtMZrKKUuOHnMR2 TovljQfV1H5uLYx+WQ43DTu/Cz0RvlOMi7nabWPIFHaTv+36UjThJgXjXbW7GKiVLY3D b6+IH2x7qKKbfh5pi3weEgP6eKKM9CXqZw3ZW66nFBXbvYLoa2hONzuz5sSz9Npzz7Yw T0DUL2MJSX3oYrkX+Rj6d1rN6YPEfc53zSeRAjvTAO8YlS/1c2qR07U9aF1PsEKqVMhW HyAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040660; x=1729645460; 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=vgBicxL465Iq2zDqEf/9aYEGLc+kPrkVMZ+FhEO9LmY=; b=pXCu5z7Ctg8NES+nH5gQDslr0HqDxoY1al34dCEOGqAE8LrPqIDI2qn5Flirv63IzG eLoTrgisWnU948k8ZAPAMhhKtviKDTdZG0bs2FCZ7QWXIn6Mhb2roernPQNTlN92hnlt 0+Sy/N/2h7X766MsravpB6cMaGwmn4kDG9wJDx8XTsKhNmlsYIlPSiKZs2pDBTHVvZt/ eLCjhpu0HU91eL2LGe12TUewGA4R+VsC8566ix9ldB9PdscExfDjf6rln7PaPTxkZo8M ofwNpuQmxYiJqKiK5ORPpTmWw8LovCgXAgLXvqS//ArPszeZ7HURWboxevS2ggzikhl6 OujA== X-Forwarded-Encrypted: i=1; AJvYcCUhsftU0gNcSrUlzcLBDA5QXQybIlPVMPvdtX49lM7mLc/yujIXZzLSGm2TtY0IgGcWJh8ehs5/G27+3Sg=@vger.kernel.org X-Gm-Message-State: AOJu0YxOEOOgeP5PNso5z3dQJz8oEJAPG8czLjayAhaAcGH2dCNROgLZ aRXVjwMiBTExtFM2hWUtRIUvu03cme7hJ20zCbSZARgES2r6CEUt9lQkXaeVyvo= X-Google-Smtp-Source: AGHT+IFmBvL88e+Hp27h91RfCL9PhqgUkAVIFgmJvgjbGRZ2wN+Vw8lZ3uWsMsuQYO78SffqFA2GKA== X-Received: by 2002:a5d:6182:0:b0:37d:4b07:606b with SMTP id ffacd0b85a97d-37d86d554fbmr1451267f8f.36.1729040660457; Tue, 15 Oct 2024 18:04:20 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:19 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:19 +0200 Subject: [PATCH net-next v9 19/23] ovpn: implement key add/del/swap via netlink 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: <20241016-b4-ovpn-v9-19-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=7966; i=antonio@openvpn.net; h=from:subject:message-id; bh=uBijmJW2oOVCVWghW03aDeybZ37xU5VWTwEgFTeWMTM=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDw/CaDBhVBcSDw3zjfvyyeXM+Ti6K3p4oxg bjVrQPq5rKJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8AAKCRALcOU6oDjV h6ICCAC2fKYU+0wxtQcofn51AHb/XtsWkZneMhtxvqgo08DNF5FjPCZzyQrBB7w8F3wNfR9FhfS P7Lhyt44Uw0zQw9/0sxAAvn8t8dP9jXiw5IcJUkqIjLcHJT6G3ajM5Cr85hgSJylmWx3xg20mXY 3UqxYQm9c6osbd4WN4a0YDHWuJVGOtnHy2udH4i4Rg/FO5A5W2qn7biMSlcgRztyVEcnM3UM1mG VTeQHSbry9eGuBbfg2vOSwmHrtWlFtkNlamTmijLHavdQXe0JAe5eHubPp+I5pEN9/vBas99mmB o0HMVUrshT8amJE+CGEwzWxfT0FnvVq10uteT6keyHesgeVB X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C This change introduces the netlink commands needed to add, delete and swap keys for a specific peer. Userspace is expected to use these commands to create, destroy and rotate session keys for a specific peer. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/netlink.c | 210 +++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 207 insertions(+), 3 deletions(-) diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c index d504445325ef82db04f87367c858adaf025f6297..bb96645205b6704f7066b7de9a0= 03e01e63c10f7 100644 --- a/drivers/net/ovpn/netlink.c +++ b/drivers/net/ovpn/netlink.c @@ -679,9 +679,140 @@ int ovpn_nl_peer_del_doit(struct sk_buff *skb, struct= genl_info *info) return ret; } =20 +static int ovpn_nl_get_key_dir(struct genl_info *info, struct nlattr *key, + enum ovpn_cipher_alg cipher, + struct ovpn_key_direction *dir) +{ + struct nlattr *attrs[OVPN_A_KEYDIR_MAX + 1]; + int ret; + + ret =3D nla_parse_nested(attrs, OVPN_A_KEYDIR_MAX, key, + ovpn_keydir_nl_policy, info->extack); + if (ret) + return ret; + + switch (cipher) { + case OVPN_CIPHER_ALG_AES_GCM: + case OVPN_CIPHER_ALG_CHACHA20_POLY1305: + if (NL_REQ_ATTR_CHECK(info->extack, key, attrs, + OVPN_A_KEYDIR_CIPHER_KEY) || + NL_REQ_ATTR_CHECK(info->extack, key, attrs, + OVPN_A_KEYDIR_NONCE_TAIL)) + return -EINVAL; + + dir->cipher_key =3D nla_data(attrs[OVPN_A_KEYDIR_CIPHER_KEY]); + dir->cipher_key_size =3D nla_len(attrs[OVPN_A_KEYDIR_CIPHER_KEY]); + + /* These algorithms require a 96bit nonce, + * Construct it by combining 4-bytes packet id and + * 8-bytes nonce-tail from userspace + */ + dir->nonce_tail =3D nla_data(attrs[OVPN_A_KEYDIR_NONCE_TAIL]); + dir->nonce_tail_size =3D nla_len(attrs[OVPN_A_KEYDIR_NONCE_TAIL]); + break; + default: + NL_SET_ERR_MSG_MOD(info->extack, "unsupported cipher"); + return -EINVAL; + } + + return 0; +} + +/** + * ovpn_nl_key_new_doit - configure a new key for the specified peer + * @skb: incoming netlink message + * @info: genetlink metadata + * + * This function allows the user to install a new key in the peer crypto + * state. + * Each peer has two 'slots', namely 'primary' and 'secondary', where + * keys can be installed. The key in the 'primary' slot is used for + * encryption, while both keys can be used for decryption by matching the + * key ID carried in the incoming packet. + * + * The user is responsible for rotating keys when necessary. The user + * may fetch peer traffic statistics via netlink in order to better + * identify the right time to rotate keys. + * The renegotiation follows these steps: + * 1. a new key is computed by the user and is installed in the 'secondary' + * slot + * 2. at user discretion (usually after a predetermined time) 'primary' and + * 'secondary' contents are swapped and the new key starts being used f= or + * encryption, while the old key is kept around for decryption of late + * packets. + * + * Return: 0 on success or a negative error code otherwise. + */ int ovpn_nl_key_new_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_KEYCONF_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct ovpn_peer_key_reset pkr; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX, + info->attrs[OVPN_A_KEYCONF], + ovpn_keyconf_nl_policy, info->extack); + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_PEER_ID)) + return -EINVAL; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_SLOT) || + NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_KEY_ID) || + NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_CIPHER_ALG) || + NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_ENCRYPT_DIR) || + NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_DECRYPT_DIR)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]); + pkr.slot =3D nla_get_u8(attrs[OVPN_A_KEYCONF_SLOT]); + pkr.key.key_id =3D nla_get_u16(attrs[OVPN_A_KEYCONF_KEY_ID]); + pkr.key.cipher_alg =3D nla_get_u16(attrs[OVPN_A_KEYCONF_CIPHER_ALG]); + + ret =3D ovpn_nl_get_key_dir(info, attrs[OVPN_A_KEYCONF_ENCRYPT_DIR], + pkr.key.cipher_alg, &pkr.key.encrypt); + if (ret < 0) + return ret; + + ret =3D ovpn_nl_get_key_dir(info, attrs[OVPN_A_KEYCONF_DECRYPT_DIR], + pkr.key.cipher_alg, &pkr.key.decrypt); + if (ret < 0) + return ret; + + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "no peer with id %u to set key for", + peer_id); + return -ENOENT; + } + + ret =3D ovpn_crypto_state_reset(&peer->crypto, &pkr); + if (ret < 0) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "cannot install new key for peer %u", + peer_id); + goto out; + } + + netdev_dbg(ovpn->dev, "%s: new key installed (id=3D%u) for peer %u\n", + __func__, pkr.key.key_id, peer_id); +out: + ovpn_peer_put(peer); + return ret; } =20 int ovpn_nl_key_get_doit(struct sk_buff *skb, struct genl_info *info) @@ -691,12 +822,85 @@ int ovpn_nl_key_get_doit(struct sk_buff *skb, struct = genl_info *info) =20 int ovpn_nl_key_swap_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + struct nlattr *attrs[OVPN_A_PEER_MAX + 1]; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX, + info->attrs[OVPN_A_KEYCONF], + ovpn_keyconf_nl_policy, info->extack); + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_PEER_ID)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]); + + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "no peer with id %u to swap keys for", + peer_id); + return -ENOENT; + } + + ovpn_crypto_key_slots_swap(&peer->crypto); + ovpn_peer_put(peer); + + return 0; } =20 int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info) { - return -EOPNOTSUPP; + struct nlattr *attrs[OVPN_A_KEYCONF_MAX + 1]; + struct ovpn_struct *ovpn =3D info->user_ptr[0]; + enum ovpn_key_slot slot; + struct ovpn_peer *peer; + u32 peer_id; + int ret; + + if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF)) + return -EINVAL; + + ret =3D nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX, + info->attrs[OVPN_A_KEYCONF], + ovpn_keyconf_nl_policy, info->extack); + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_PEER_ID)) + return -EINVAL; + + if (ret) + return ret; + + if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs, + OVPN_A_KEYCONF_SLOT)) + return -EINVAL; + + peer_id =3D nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]); + slot =3D nla_get_u8(attrs[OVPN_A_KEYCONF_SLOT]); + + peer =3D ovpn_peer_get_by_id(ovpn, peer_id); + if (!peer) { + NL_SET_ERR_MSG_FMT_MOD(info->extack, + "no peer with id %u to delete key for", + peer_id); + return -ENOENT; + } + + ovpn_crypto_key_slot_delete(&peer->crypto, slot); + ovpn_peer_put(peer); + + return 0; } =20 /** --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 3BC711D1E9C for ; Wed, 16 Oct 2024 01:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040667; cv=none; b=CPXk8NdDkawpUChQz3+8s2Cmync/k36t1WUmpcpL6HAqiA53HImy/yxQPzqb2BA+Z9KPBwK7eWgl9W3wz1IwwR+MFkEVCMnRY/AhLVbX8qgeQKXYcVk9GZmwg9Ly9ZQUehq0QRqY4FQo3PSP1rnDbBI6M6WrYZj3Olvz7gfsHzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040667; c=relaxed/simple; bh=D9XOuq5NLdXjEhB+gziaUN9VEXaSfc+cmeGM+FY4CmY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hTCIqNnGbqJC6WRaP/VDl/5jR21L9DypTMfNJ5OE+1LB1SMCkWLac6Py5mnYjTHAjZSQHcemk4/M+huWy1Vq6sqYOQrvDA0FExkOXYADXWwiLsb8kfAdDPpjctVuhBkMN1sRRTustD55jwmRLwSyqM4O+PiE/3bGPQKeujOTdvs= 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=VOuZ7Mzd; arc=none smtp.client-ip=209.85.128.51 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="VOuZ7Mzd" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-42f6bec84b5so57638235e9.1 for ; Tue, 15 Oct 2024 18:04:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040663; x=1729645463; 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=rMD7l+FSF3xKMSQaJEdYL4+0Pf5IdciRVFCya7TYwek=; b=VOuZ7MzdF8uODgIL+Y1UYc+lrykA72PARhGSHYjihkH62+sMdPLUdIEK6IGB0Q6Suf 9l4wnTLm08Q2X0F2L8r1q4iUXaNscZR5POFauBd3Klba4l1Dg3WfkSimmgKLO864avVN GCj14gDWYyutpVElR1mh5GhJe7HXUCCIcmaLZc0EHz5dMFgF1hThK19V7mZlIEMk7oFA Qn4PQ+Z/CjYz7MDOOmRhW4NZPeoG1eb2hi1fN3h5rk1D6y7PCSEYnvzJtlsFMUB1zmh5 dlyEsyFQW7ELZyEwUwbGtVVMcHPOCHMqiUIIIJRiTcXkWkSEChuDA0xtdsgCtqOkU/yH VmlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040663; x=1729645463; 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=rMD7l+FSF3xKMSQaJEdYL4+0Pf5IdciRVFCya7TYwek=; b=TH58n8iEecUKV0pweQ1RHU6gkMMjIeGHIz54YUSloeteoZhgkaeADyZVGPKdJWpqBl VRZehhd3PITW1Mr/jyInycazywVFBO+Euow9q82nPf0KlAhNvIvQOoQam0U7J5xi9QUE QBe8kCVLTZsyzoafCWASZfMs9AZk9QLYH7b8UPwIFj1LUKBroP8i3HohkY2ga5mmmDmj JZxRV2v+RhvbEqncyX6yxhINYJgN+UtZ+uZe5ToDss7opwyTHjWaVpyGaQp94CZGVltR xXcplgHrnxlTrBdiQEojdkAastFAIubY0TD55VGb101l8/hNGaBieEB/oeDvw/hL3L90 KFlQ== X-Forwarded-Encrypted: i=1; AJvYcCVxexjdfBj9EXMKM3Ef/CSAS72ljnS4y7Z7QQ+68MwL4A1PKRFFO2ifYS6dADTAY3/YmUeq3PeWy30YGnQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3lL8n+iY4NNdLVAZ5nAjHL6fcVT+72/qQHgXyWa1m0JyhqryS aeBel/Tu4HzfxjqrAxF22DPsvAVDKJ0MaeRiyKw2IXsWXv+PZojJSrlC5rMLc+c= X-Google-Smtp-Source: AGHT+IEsXHjvZSLYefO3htqwaxwzORWf8B0OT6ATZfvFpi1yhCfCetRyT7L5kxe4wZAe75IokFMrDg== X-Received: by 2002:a05:600c:1906:b0:42c:b45d:4a7b with SMTP id 5b1f17b1804b1-4314a37bb99mr19466285e9.25.1729040663627; Tue, 15 Oct 2024 18:04:23 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:22 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:20 +0200 Subject: [PATCH net-next v9 20/23] ovpn: kill key and notify userspace in case of IV exhaustion 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: <20241016-b4-ovpn-v9-20-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6543; i=antonio@openvpn.net; h=from:subject:message-id; bh=D9XOuq5NLdXjEhB+gziaUN9VEXaSfc+cmeGM+FY4CmY=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDxD98nCNTe5ud76sPiUO1YuG5F/Zi1VEblA D3GUuO3E42JATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8QAKCRALcOU6oDjV h6TZB/9EiC73lma2rR+/IWSXjb+n3h1zfkbNg8sSZZeolW3efmCuf69tdyBmvrVZioFJ2spZXCT YHg46IGSCJvSqne+Lb4IbfO/oao/EjvpOPrFyr6JoyAtdxVnSWkhlyebNuYq/Jq5j9RaSEjBwTU HpsNa/6/9GyaXntUF3ki4Jo9cQ/t8TWbHyMlosrJsCOwzHgmcbkSNJ2Q82HZWxmpN7YWq0c+uuu BxSjOCb8qTiMkwwegrDWnXx2v2Wr/es9Nj9uDzTd9Jo8n/vxnCQgsLOb/qVoqiZqI0ysGUa+913 OP8urs2N/JO9y6BVRTYavxNq4aapB944jH9ju+pL2s0T38Ts X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C IV wrap-around is cryptographically dangerous for a number of ciphers, therefore kill the key and inform userspace (via netlink) should the IV space go exhausted. Userspace has two ways of deciding when the key has to be renewed before exhausting the IV space: 1) time based approach: after X seconds/minutes userspace generates a new key and sends it to the kernel. This is based on guestimate and normally default timer value works well. 2) packet count based approach: after X packets/bytes userspace generates a new key and sends it to the kernel. Userspace keeps track of the amount of traffic by periodically polling GET_PEER and fetching the VPN/LINK stats. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/crypto.c | 19 ++++++++++++++++ drivers/net/ovpn/crypto.h | 2 ++ drivers/net/ovpn/io.c | 18 ++++++++++++++- drivers/net/ovpn/netlink.c | 55 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/netlink.h | 2 ++ 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/drivers/net/ovpn/crypto.c b/drivers/net/ovpn/crypto.c index f1f7510e2f735e367f96eb4982ba82c9af3c8bfc..56ae2f5e5c33e13933f26acd91c= 7786f7c02011a 100644 --- a/drivers/net/ovpn/crypto.c +++ b/drivers/net/ovpn/crypto.c @@ -55,6 +55,25 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state = *cs) } } =20 +/* removes the key matching the specified id from the crypto context */ +void ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id) +{ + struct ovpn_crypto_key_slot *ks =3D NULL; + + spin_lock_bh(&cs->lock); + if (rcu_access_pointer(cs->slots[0])->key_id =3D=3D key_id) { + ks =3D rcu_replace_pointer(cs->slots[0], NULL, + lockdep_is_held(&cs->lock)); + } else if (rcu_access_pointer(cs->slots[1])->key_id =3D=3D key_id) { + ks =3D rcu_replace_pointer(cs->slots[1], NULL, + lockdep_is_held(&cs->lock)); + } + spin_unlock_bh(&cs->lock); + + if (ks) + ovpn_crypto_key_slot_put(ks); +} + /* Reset the ovpn_crypto_state object in a way that is atomic * to RCU readers. */ diff --git a/drivers/net/ovpn/crypto.h b/drivers/net/ovpn/crypto.h index 3b437d26b531c3034cca5343c755ef9c7ef57276..53a965ec0fad3402317c1ac580a= a6faad579a09b 100644 --- a/drivers/net/ovpn/crypto.h +++ b/drivers/net/ovpn/crypto.h @@ -136,4 +136,6 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state= *cs); =20 void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs); =20 +void ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id); + #endif /* _NET_OVPN_OVPNCRYPTO_H_ */ diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index c857ac9015b75530cf3ae40f8db0d4db4c08e2fa..dc6479d5f3008c4e851bee8d406= 1e4e85bed40f2 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -225,6 +225,7 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *= skb) =20 void ovpn_encrypt_post(void *data, int ret) { + struct ovpn_crypto_key_slot *ks =3D NULL; struct ovpn_peer *peer =3D NULL; struct sk_buff *skb =3D data; unsigned int orig_len =3D 0; @@ -237,15 +238,28 @@ void ovpn_encrypt_post(void *data, int ret) =20 /* crypto is done, cleanup skb CB and its members */ if (likely(ovpn_skb_cb(skb)->ctx)) { + ks =3D ovpn_skb_cb(skb)->ctx->ks; peer =3D ovpn_skb_cb(skb)->ctx->peer; orig_len =3D ovpn_skb_cb(skb)->ctx->orig_len; =20 - ovpn_crypto_key_slot_put(ovpn_skb_cb(skb)->ctx->ks); aead_request_free(ovpn_skb_cb(skb)->ctx->req); kfree(ovpn_skb_cb(skb)->ctx); ovpn_skb_cb(skb)->ctx =3D NULL; } =20 + if (unlikely(ret =3D=3D -ERANGE)) { + /* we ran out of IVs and we must kill the key as it can't be + * use anymore + */ + netdev_warn(peer->ovpn->dev, + "killing key %u for peer %u\n", ks->key_id, + peer->id); + ovpn_crypto_kill_key(&peer->crypto, ks->key_id); + /* let userspace know so that a new key must be negotiated */ + ovpn_nl_key_swap_notify(peer, ks->key_id); + goto err; + } + if (unlikely(ret < 0)) goto err; =20 @@ -275,6 +289,8 @@ void ovpn_encrypt_post(void *data, int ret) dev_core_stats_tx_dropped_inc(peer->ovpn->dev); if (likely(peer)) ovpn_peer_put(peer); + if (likely(ks)) + ovpn_crypto_key_slot_put(ks); kfree_skb(skb); } =20 diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c index bb96645205b6704f7066b7de9a003e01e63c10f7..09c7df2b1c76c812a3bece6e63c= 9fac13c96c327 100644 --- a/drivers/net/ovpn/netlink.c +++ b/drivers/net/ovpn/netlink.c @@ -903,6 +903,61 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct g= enl_info *info) return 0; } =20 +/** + * ovpn_nl_key_swap_notify - notify userspace peer's key must be renewed + * @peer: the peer whose key needs to be renewed + * @key_id: the ID of the key that needs to be renewed + * + * Return: 0 on success or a negative error code otherwise + */ +int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id) +{ + struct nlattr *k_attr; + struct sk_buff *msg; + int ret =3D -EMSGSIZE; + void *hdr; + + netdev_info(peer->ovpn->dev, "peer with id %u must rekey - primary key un= usable.\n", + peer->id); + + msg =3D nlmsg_new(100, GFP_ATOMIC); + if (!msg) + return -ENOMEM; + + hdr =3D genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0, OVPN_CMD_KEY_SWAP_NTF); + if (!hdr) { + ret =3D -ENOBUFS; + goto err_free_msg; + } + + if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex)) + goto err_cancel_msg; + + k_attr =3D nla_nest_start(msg, OVPN_A_KEYCONF); + if (!k_attr) + goto err_cancel_msg; + + if (nla_put_u32(msg, OVPN_A_KEYCONF_PEER_ID, peer->id)) + goto err_cancel_msg; + + if (nla_put_u16(msg, OVPN_A_KEYCONF_KEY_ID, key_id)) + goto err_cancel_msg; + + nla_nest_end(msg, k_attr); + genlmsg_end(msg, hdr); + + genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev), msg, + 0, OVPN_NLGRP_PEERS, GFP_ATOMIC); + + return 0; + +err_cancel_msg: + genlmsg_cancel(msg, hdr); +err_free_msg: + nlmsg_free(msg); + return ret; +} + /** * ovpn_nl_register - perform any needed registration in the NL subsustem * diff --git a/drivers/net/ovpn/netlink.h b/drivers/net/ovpn/netlink.h index 9e87cf11d1e9813b7a75ddf3705ab7d5fabe899f..33390b13c8904d40b629662005a= 9eb92ff617c3b 100644 --- a/drivers/net/ovpn/netlink.h +++ b/drivers/net/ovpn/netlink.h @@ -12,4 +12,6 @@ int ovpn_nl_register(void); void ovpn_nl_unregister(void); =20 +int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id); + #endif /* _NET_OVPN_NETLINK_H_ */ --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 A74531D2794 for ; Wed, 16 Oct 2024 01:04:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040670; cv=none; b=QBJTg62xo8L7Bt7k5fVaT3a1XEVOxZE87zQp/QnH+Qs25SncCGSN53U8z5g1UoanKAuiR6XEtGlwtUZnQXaN4UoBhMSIu3pYUBF1eRXVtMXxsoQaz0fw3RM5y1JZp+QSh5ylXf6NwZx2KWUBUrKFPUrjlJ4uXrnEqYCjbJ8ERz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040670; c=relaxed/simple; bh=nU8TleDp+Ut6e8BOqP5Lv2Lo+9j4c+aWZvfGoKQf/ac=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=C7Q9E0VkqMZIl5+zCZMGYee0xuIyeXJeOSkQd5X4AZ9Y04iegE/13uVY3QzGCmnit/AKYP/JP8pOrTuyXeYgbh326lW4+rQ+rN7DKCZz+iKu7wIbcn9Lzl/lmDQdAOJdybXm/nL0pNF9lbvoIAHWojvqUqcfOdyfOxuDFOU2xDI= 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=crGPpoG5; arc=none smtp.client-ip=209.85.128.51 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="crGPpoG5" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-431160cdbd0so2218595e9.1 for ; Tue, 15 Oct 2024 18:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040666; x=1729645466; 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=GqS1ZpjN7qw7rm8wczpqCw+7mxghDdiAYmdxPr/ayQM=; b=crGPpoG5NAaGA5nTBvpek0O9woMLjkRf2OtEmiQ8WSP4eTQ64cQVsoeQwnpnnYmbHm +9pXGyWPQrZ0Uw+7u8HsTz8L/KZOOG/W3WXAFocei+XK0ZNSTSy2s41MkfEqyj1ZsKym e5slvuSByqKPXoU+WueGWld0EmGtNFoszpbo3VTGVdcdaxnv78JfrZ0wJY50cfkn6Qv9 U4HndNjUsClsjTHY/q4dg1j7iF129jfCumWbW5wLHJe+CxVCvDgYqMv3UphZ5qDmaOaU mcrwCnaOgtO94QQs3wPUX1oUKi5S3ayljHnIk0uF+XzYBo6anFkMHx4bohkedVGTMYAp 5eVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040666; x=1729645466; 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=GqS1ZpjN7qw7rm8wczpqCw+7mxghDdiAYmdxPr/ayQM=; b=kfmWzGSfRX195DibWuQDXTI0DrSV+jKoAwtl1RxkGCULoCEijwf0mWO9M18yviHW9J nROVt4MXfTTC/ttKfHLiy59+wT3hEVlxMwHn8cy7LiIBKdS9ycERprCDmwQ8yA7DLuD1 riYjRqk+8VVeEPgahjrS6C/Ik3R/0vidzxxsEy3pty6eEZuU7FATy4Yy0f2fvFj2ahC+ BMcDlFLyfv00U533X4ptdYPKHbaYuU09dp9kQDVL38SgLvNAp4/ERTRBlyFHFveKvcCt +/YdhNP4yEd2DNWIfOofNpD6VLVhVosaeNk4CsL+CHY3hbQ++TaRtmtWqYsDl0Li7412 6SEA== X-Forwarded-Encrypted: i=1; AJvYcCX384w2eL9lIFo9bj5m4//EfLr5KFbZRBwLPGfTt4V0MvkMRsXqiQamZCBCGLPpVB6DFEFeGCdazbw3NVE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0ptZA6Wk3OvAiM0zul8womdyW4jXtJ/RPsFIBvC6+W/t32lmh V+8aB0ppJ1RNTIPEQrhz4cpO2CBNuyUPXVQo/yN0nKUBKv95QmO4bk/ARtFMyng= X-Google-Smtp-Source: AGHT+IEWqZORm45+krRlB8x4vdFvtbCcyxw2xawp6YS+IdLSRW/gVnMGHqyKRL9EZpdJhzk79c5MNg== X-Received: by 2002:a5d:424c:0:b0:37d:3e42:be32 with SMTP id ffacd0b85a97d-37d54e0c9e9mr11734053f8f.0.1729040666005; Tue, 15 Oct 2024 18:04:26 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:25 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:21 +0200 Subject: [PATCH net-next v9 21/23] ovpn: notify userspace when a peer is deleted 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: <20241016-b4-ovpn-v9-21-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3254; i=antonio@openvpn.net; h=from:subject:message-id; bh=nU8TleDp+Ut6e8BOqP5Lv2Lo+9j4c+aWZvfGoKQf/ac=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDxJZTZd9C7SpMR2eH0McjXLmAr71bXtMFxf ycnsyJFIzCJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8QAKCRALcOU6oDjV hyHZB/9U16LBSG6sW4qDubGJY1UK4tSzYy4lZE+ZYOMMTYsi44kLIE87Gqi7bDpstD4f+9f32g9 yX1+z85XxCLkz7FB9S/3g7zLpmgmY9WVsUhc7mH6uXOWt8c2vz3EwwQb3zkiQ7GFdp1MnhKJAbA d7K47D1qhl8Z3MM4/uE9uOE+VaeNi+vsKhpHlJ9Pu33xc6QxQSOlxwQaKKXnk/878Qx1RSjPzc8 X9tbomhPVkdPrSAoLAjXYqy8H6RjTk9Vk+gzEMDtBhYj3eVsEd+LUKdr/cvSusqJhJWqpdlKWhU ZYzJwx787G2ceVKFd/Bn9gEBU6X1jXNgUF3j5bz1+vwW0DTi X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Whenever a peer is deleted, send a notification to userspace so that it can react accordingly. This is most important when a peer is deleted due to ping timeout, because it all happens in kernelspace and thus userspace has no direct way to learn about it. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/netlink.c | 55 ++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/ovpn/netlink.h | 1 + drivers/net/ovpn/peer.c | 1 + 3 files changed, 57 insertions(+) diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c index 09c7df2b1c76c812a3bece6e63c9fac13c96c327..975d324c0362e34fc209eaa4552= 051ff75c00036 100644 --- a/drivers/net/ovpn/netlink.c +++ b/drivers/net/ovpn/netlink.c @@ -903,6 +903,61 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct g= enl_info *info) return 0; } =20 +/** + * ovpn_nl_peer_del_notify - notify userspace about peer being deleted + * @peer: the peer being deleted + * + * Return: 0 on success or a negative error code otherwise + */ +int ovpn_nl_peer_del_notify(struct ovpn_peer *peer) +{ + struct sk_buff *msg; + struct nlattr *attr; + int ret =3D -EMSGSIZE; + void *hdr; + + netdev_info(peer->ovpn->dev, "deleting peer with id %u, reason %d\n", + peer->id, peer->delete_reason); + + msg =3D nlmsg_new(100, GFP_ATOMIC); + if (!msg) + return -ENOMEM; + + hdr =3D genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0, OVPN_CMD_PEER_DEL_NTF); + if (!hdr) { + ret =3D -ENOBUFS; + goto err_free_msg; + } + + if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex)) + goto err_cancel_msg; + + attr =3D nla_nest_start(msg, OVPN_A_PEER); + if (!attr) + goto err_cancel_msg; + + if (nla_put_u8(msg, OVPN_A_PEER_DEL_REASON, peer->delete_reason)) + goto err_cancel_msg; + + if (nla_put_u32(msg, OVPN_A_PEER_ID, peer->id)) + goto err_cancel_msg; + + nla_nest_end(msg, attr); + + genlmsg_end(msg, hdr); + + genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev), msg, + 0, OVPN_NLGRP_PEERS, GFP_ATOMIC); + + return 0; + +err_cancel_msg: + genlmsg_cancel(msg, hdr); +err_free_msg: + nlmsg_free(msg); + return ret; +} + /** * ovpn_nl_key_swap_notify - notify userspace peer's key must be renewed * @peer: the peer whose key needs to be renewed diff --git a/drivers/net/ovpn/netlink.h b/drivers/net/ovpn/netlink.h index 33390b13c8904d40b629662005a9eb92ff617c3b..4ab3abcf23dba11f6b92e3d69e7= 00693adbc671b 100644 --- a/drivers/net/ovpn/netlink.h +++ b/drivers/net/ovpn/netlink.h @@ -12,6 +12,7 @@ int ovpn_nl_register(void); void ovpn_nl_unregister(void); =20 +int ovpn_nl_peer_del_notify(struct ovpn_peer *peer); int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id); =20 #endif /* _NET_OVPN_NETLINK_H_ */ diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 2e218e8cfacad753d1284808958234913f3ef59e..267ffa17db632d3b959c5b53f80= 075966f95a53c 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -239,6 +239,7 @@ void ovpn_peer_release_kref(struct kref *kref) { struct ovpn_peer *peer =3D container_of(kref, struct ovpn_peer, refcount); =20 + ovpn_nl_peer_del_notify(peer); ovpn_peer_release(peer); } =20 --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 AA0461D279B for ; Wed, 16 Oct 2024 01:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040670; cv=none; b=ahVk83NH7+YrxKADNmDcgj2QdKjfg1RQh6z3TUZcj6HyEVXy4eq0Hnvdp5YyctSEFKh0H6Ogc08yPnuzdwBbA0y/Gn8KB975lZ1EljWCpEo8r2HVzosgnD6s1iOUt3/5IDbYyXFN7CEx526xiEBkA74imIhoiwXratwvkX85vGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040670; c=relaxed/simple; bh=aDlod1jXavu2K8KnusK1u2l7VsBsilN0fz8PvjNKPQ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PlPMzTsfPvatAC9km6jALNiMq/VHsWhvkRp3rAvWFeyG2NUX0OyNbF5ZCyORs6klhTDo9rQrt/VZFDji5q8GJxf0wYIA7dZNMSEMrWj7Hc0Bwn3QYuMkssqbfnvdlikslBeICC1WvnQcZhTENSL32AQ0RmNkCVlWxqiNxB8mnD8= 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=Pdy7d+MB; arc=none smtp.client-ip=209.85.128.42 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="Pdy7d+MB" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4314311959aso9518325e9.1 for ; Tue, 15 Oct 2024 18:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040667; x=1729645467; 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=vFaxyOK2oGzKEK+1blmMTfwvFfOfF/h23u+ELvVQAZo=; b=Pdy7d+MBdHIzPm+Z85u9Nr2SLBnkGqZPEZjBpU8X7hIazMm1HESPBkks/pOb3xpu1R clmBJpW6HKeqHI1PyUoOoJt8XF/PW9PNNnlCOaLZXAgwGKg2xmVI0FDa+lxi4YqfZYAr ewQXk+ExKxZKkCwTpowNmCE7yENRXBYn6t+nuKKJw03Yqt88He8AOOzKqbfeyomLcwQN Rk5DS8XzGbK1d6VHR1g62aEzbj/iH0hVNSHHwach+FzcduAN6ikRyxSiOQtQAWq5uQrU G7m8PTPl+MrKADwU16n9sA0ePQPBLu+PJNFKehK1gNf3thgH3ZCF4mHql0S9ln4Vy2ll iKqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040667; x=1729645467; 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=vFaxyOK2oGzKEK+1blmMTfwvFfOfF/h23u+ELvVQAZo=; b=fM+1KNwsKhxUMyFAsguxTBddxsknqylqMcG7ZEFDC7K/GEl4qt6ioAW+f6DuAracU0 8JtOq5gBVRlFxVawNzUEdyCim/81K2WSSie38HuxgFU7A0n+0Z2RlxLs2lkol4+V3bw8 5v1MM9tBYKKPnE1iWj62sOpCJ808YE8cqFHLsQ/BO5uRwZf7+smhXDksJ4nKxt2ldaIM /xTykQfFlSZaVcpv8LItc3XGgKf2ZgIUqN1RjpWrPSVgha49y0vevmVV2CHTQ1hD9FdK tYmp3KcWCAUYlIrd0+sOnIUNTaCGvtFIoFWoW2ewMCpqMZoF8E7wLYqOd6ogZv+wDrWW 5ykg== X-Forwarded-Encrypted: i=1; AJvYcCXU/6gw8mvG7nTXRpByiNUbH08reZH69fHh98xMXDZpTBKEPN4E4DEYkwJBT4goB4CPNTx4XB1NM9ngJEE=@vger.kernel.org X-Gm-Message-State: AOJu0YwtgrVJbd9qbIUaxGsB/eacAD5y0L9388nweOFTy5b6Kas6An3w Ud/9y088ln/8M1I3o+7nDXxCR4bB6xR1dUPE5r/YatgyipVJYCyp7bVY4WD+DMg= X-Google-Smtp-Source: AGHT+IEeQrMz5VPvQXB8uM1sFWvNYzDnZgeneeGDqyLdXgZIJF9ZIbIUZhPKVCZBmiIUnHvJxWs3ag== X-Received: by 2002:a5d:6584:0:b0:37c:d20d:447c with SMTP id ffacd0b85a97d-37d5fedbd1amr8390727f8f.29.1729040667008; Tue, 15 Oct 2024 18:04:27 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:26 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:22 +0200 Subject: [PATCH net-next v9 22/23] ovpn: add basic ethtool support 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: <20241016-b4-ovpn-v9-22-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1706; i=antonio@openvpn.net; h=from:subject:message-id; bh=aDlod1jXavu2K8KnusK1u2l7VsBsilN0fz8PvjNKPQ0=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDxI4wc6zllOLyTeDufX0RnRgxkRZvYdET+M woNTxZVGZWJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8QAKCRALcOU6oDjV h2xjB/96RUwBLeg5dJNth5c5jJYkjMWBCotASXoVjETUDB8A0ShnK/xmgxHlQhL0viXIly76GfY 6WbP4SXAdRmvPqME6hFk7qSGsbV2EJLrZwN68M7K1h9boR4EWTeFRm9dBAGiO6+Kc8gBnnmQNjH HY4ZzfMDO3+mKqiJSr7YJoLXDa82JnucF/b/KyEljJ7eE+UakfrTSNDFnq7Xw9zhL3B1+4ogL03 f7k4oArC+EmE0cHbhhD6n2dwBiXQhfS55B48xJm7I2WVqHykGick4K+f+aqZ4ZNE0Ew0yt3qoSL tnnTp3JDXZmHPn7xRgLLkgwp3u6Wd9rO2MEvtHkqtOsvmEIy X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C Implement support for basic ethtool functionality. Note that ovpn is a virtual device driver, therefore various ethtool APIs are just not meaningful and thus not implemented. Signed-off-by: Antonio Quartulli Reviewed-by: Andrew Lunn --- drivers/net/ovpn/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/net/ovpn/main.c b/drivers/net/ovpn/main.c index 1bd563e3f16f49dd01c897fbe79cbd90f4b8e9aa..9dcf51ae1497dda17d418b76201= 1b04bfd0521df 100644 --- a/drivers/net/ovpn/main.c +++ b/drivers/net/ovpn/main.c @@ -7,6 +7,7 @@ * James Yonan */ =20 +#include #include #include #include @@ -96,6 +97,19 @@ bool ovpn_dev_is_valid(const struct net_device *dev) return dev->netdev_ops->ndo_start_xmit =3D=3D ovpn_net_xmit; } =20 +static void ovpn_get_drvinfo(struct net_device *dev, + struct ethtool_drvinfo *info) +{ + strscpy(info->driver, OVPN_FAMILY_NAME, sizeof(info->driver)); + strscpy(info->bus_info, "ovpn", sizeof(info->bus_info)); +} + +static const struct ethtool_ops ovpn_ethtool_ops =3D { + .get_drvinfo =3D ovpn_get_drvinfo, + .get_link =3D ethtool_op_get_link, + .get_ts_info =3D ethtool_op_get_ts_info, +}; + static void ovpn_setup(struct net_device *dev) { /* compute the overhead considering AEAD encryption */ @@ -111,6 +125,7 @@ static void ovpn_setup(struct net_device *dev) =20 dev->pcpu_stat_type =3D NETDEV_PCPU_STAT_TSTATS; =20 + dev->ethtool_ops =3D &ovpn_ethtool_ops; dev->netdev_ops =3D &ovpn_netdev_ops; =20 dev->priv_destructor =3D ovpn_struct_free; --=20 2.45.2 From nobody Tue Nov 26 19:54:39 2024 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 5B73B1D2B17 for ; Wed, 16 Oct 2024 01:04:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040675; cv=none; b=Y/m2CeTbca/LRsPfvsPasXvVdtcrdY6cTvCoSWIy6VHK3KVzTArCdFpIDONfHxVwVJWd9EDHsA7H95EK+J+X4Vd+pjtndBur1Ijz1l/+xBpXW0NI7fZ8QnU4FBbzuccEpkvcXuIP4FRkqS7fh404Y60/jVllLl9WbVFyozbltx4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729040675; c=relaxed/simple; bh=I1bOvp3ftR4Ic1gFJYIZXTOmYE9rY63XbVuHcJIODdI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h9DW8zbucB54SEdFkWuhizRcDLALyGjgz8g0rfFLis5sNBHct2DP160DEslBeh+CBGgoYRRhSQbWYnv0xtt7yb20/M+LHju+N3QnPt+OGRsQmYV+qpnUx8Rs7wm5lFTMg8bkBgwuAl1A1YAYZSkPlgd8qIM2z79Ut+8JN28fV6Y= 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=Ksm7M6j1; arc=none smtp.client-ip=209.85.221.50 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="Ksm7M6j1" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-37d51055097so3613846f8f.3 for ; Tue, 15 Oct 2024 18:04:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1729040669; x=1729645469; 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=khppd248nMm91/znaKstyJYO8jnpkWVhnlC+1C4Dx7k=; b=Ksm7M6j1Gbrx4EOjrAz4PxNMYEfyPEU5OultNaqOrMcdjr324NCpVbJsuDyzJjoezc j7atxP+MQavWYux+g+RzZIzltSKM5G0EpubNBNE4Vr6x1PWnTcjG22VFyhrpPo2Xir5P ulcbT47zf47KjXNw4rRza48ZXr+aJXVck9JrrXNifek7wX37OAfXw0QTbnN5D/XfuOBI 68Yyfk+n+Om1rQihz6RTWufIBV6xa6mOcLoUlIutdNwNJCisN0hArCsTLQ9ci1Qxg5vh l2waw8FYU9ea3atOqIZjvkaLtPhpX6v/eK9BYdcdaLepO+f2FZsHVivdBhpYOEtGA3vB a+3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729040669; x=1729645469; 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=khppd248nMm91/znaKstyJYO8jnpkWVhnlC+1C4Dx7k=; b=k8PFqsdnDWMSmXQ/E38b7dB9xpgh8xpN51TeHU81qBCwl3HBGWqf5+SHr4/oQC0j7L wxo+pGI/hiYE2UbDIuwni9wPXyhqH0yxVYZpnZs+QpiZVFF7ILrB+C8UMfmqVZU7HQCI sdD5Tfhqwzcbq0LwVpiw/xZDhkX6lad02wlDYee4nd6GrmNws7ObUi8wtVoUZuYl+JBJ DjRnuts/67gwBP8rpmonl059AYcIZLytiw7LckQYa8dTkUrUeF5K/r5o+wbIhGuc6AVh DFo6X+8+kDWMrup5nbuGex2N6U+ghSewg2YxfsBcovK6reB3hy5p2k5nGJspk6sjKN0H eNug== X-Forwarded-Encrypted: i=1; AJvYcCWGYjUZcLRH3Ows9CRMSDEd/Gwk1V/iNA5xcWEjXyApppQX4GmiDjoNQhH/OxssAYQ1DviDN1H9G8sPAa8=@vger.kernel.org X-Gm-Message-State: AOJu0Yx25QFBswNbRc1msHSpeRa4H+8tiw1wdnpT7mFDm1Cc3Jq5Mxsj td/INv/OU2A1aHeuOikzzDWQ4ChSzGcrQ5miIqM+LN8hWAOF3mcu1gzFXinjHNQ= X-Google-Smtp-Source: AGHT+IF3dWSqyBVK9QncJa9rymToPb+S2KP2JFDdgIO4JjvE+XwZoZrkRUdDDetK2wGPvPIUUgw0zA== X-Received: by 2002:a5d:6d0c:0:b0:37d:498a:a233 with SMTP id ffacd0b85a97d-37d5ffb9815mr12106147f8f.43.1729040668400; Tue, 15 Oct 2024 18:04:28 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:4c1a:a7c8:72f5:4282]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fa8778csm2886765f8f.25.2024.10.15.18.04.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 18:04:27 -0700 (PDT) From: Antonio Quartulli Date: Wed, 16 Oct 2024 03:03:23 +0200 Subject: [PATCH net-next v9 23/23] testing/selftest: add test tool and scripts for ovpn module 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: <20241016-b4-ovpn-v9-23-aabe9d225ad5@openvpn.net> References: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> In-Reply-To: <20241016-b4-ovpn-v9-0-aabe9d225ad5@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , donald.hunter@gmail.com, sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, openvpn-devel@lists.sourceforge.net, linux-kselftest@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=67150; i=antonio@openvpn.net; h=from:subject:message-id; bh=I1bOvp3ftR4Ic1gFJYIZXTOmYE9rY63XbVuHcJIODdI=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBnDxDxgKiYtbX3FRIv2ILjVVPGfDoR8+sTTfV4e /b20JbCGtWJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZw8Q8QAKCRALcOU6oDjV h96gB/4iBKukwKeoI7l9Bq7w7fhtIlfSv++vZVO+C+FajTOkiryTd0UwuFsOnLMkyPGvK5da2pG Cg94LVlztXhCqOf216tX77VeLUevH4+VQ8ry7YZxKvDWH/2LbP7ytdXwwWD8ZqvmCRFJaBuc+x/ NC6QxZSv6Hz7RCTk8MQ59se5A8vbL9refADP05qPfg7CkLCrnIfR5O+lMoz74HCcqJIpY3OVO5o K/Xga0JfhSwivCvEBVctEcUGRf7sCS2PCDEIrUNcdElHFXk0ssAtWlOOuVXly4MP8gTHUxtXuye 7VrUi3NSn325/cH7xWO/vT3qVMfh3P+LScwazuONMb5wz3Ad X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C The ovpn-cli tool can be compiled and used as selftest for the ovpn kernel module. It implements the netlink API and can thus be integrated in any script for more automated testing. Along with the tool, 2 scripts are added that perform basic functionality tests by means of network namespaces. The scripts can be performed in sequence by running run.sh Cc: shuah@kernel.org Cc: linux-kselftest@vger.kernel.org Signed-off-by: Antonio Quartulli --- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 16 + tools/testing/selftests/net/ovpn/config | 10 + tools/testing/selftests/net/ovpn/data-test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/data-test.sh | 157 ++ tools/testing/selftests/net/ovpn/data64.key | 5 + tools/testing/selftests/net/ovpn/float-test.sh | 122 ++ tools/testing/selftests/net/ovpn/ovpn-cli.c | 2136 +++++++++++++++++= ++++ tools/testing/selftests/net/ovpn/tcp_peers.txt | 5 + tools/testing/selftests/net/ovpn/udp_peers.txt | 5 + 12 files changed, 2469 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 8edccdabd96ab4a4e8e9ed24d18ecbcd6d33ecec..ee94f245a18557974ff35b82d9d= 6d883357e6a01 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17476,6 +17476,7 @@ T: git https://github.com/OpenVPN/linux-kernel-ovpn= .git F: Documentation/netlink/specs/ovpn.yaml F: drivers/net/ovpn/ F: include/uapi/linux/ovpn.h +F: tools/testing/selftests/net/ovpn/ =20 P54 WIRELESS DRIVER M: Christian Lamparter diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Mak= efile index b38199965f99014f3e2636fe8d705972f2c0d148..3ae2dd6492ca70d5e317c6e5b4e= 2560b060e3214 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -68,6 +68,7 @@ TARGETS +=3D net/hsr TARGETS +=3D net/mptcp TARGETS +=3D net/netfilter TARGETS +=3D net/openvswitch +TARGETS +=3D net/ovpn TARGETS +=3D net/packetdrill TARGETS +=3D net/rds TARGETS +=3D net/tcp_ao diff --git a/tools/testing/selftests/net/ovpn/.gitignore b/tools/testing/se= lftests/net/ovpn/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ee44c081ca7c089933659689303= c303a9fa9713b --- /dev/null +++ b/tools/testing/selftests/net/ovpn/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0+ +ovpn-cli diff --git a/tools/testing/selftests/net/ovpn/Makefile b/tools/testing/self= tests/net/ovpn/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9510c9b171390809bcce9f8c81a= 3a0abce885ac0 --- /dev/null +++ b/tools/testing/selftests/net/ovpn/Makefile @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +CFLAGS =3D -Wall -Wl,--no-as-needed -g -O2 $(KHDR_INCLUDES) +CFLAGS +=3D $(shell pkg-config --cflags libnl-3.0 libnl-genl-3.0) + +LDFLAGS =3D -lmbedtls -lmbedcrypto +LDFLAGS +=3D $(shell pkg-config --libs libnl-3.0 libnl-genl-3.0) + +TEST_PROGS =3D data-test.sh \ + data-test-tcp.sh \ + float-test.sh + +TEST_GEN_FILES =3D ovpn-cli + +include ../../lib.mk diff --git a/tools/testing/selftests/net/ovpn/config b/tools/testing/selfte= sts/net/ovpn/config new file mode 100644 index 0000000000000000000000000000000000000000..71946ba9fa175c191725e369eb9= b973503d9d9c4 --- /dev/null +++ b/tools/testing/selftests/net/ovpn/config @@ -0,0 +1,10 @@ +CONFIG_NET=3Dy +CONFIG_INET=3Dy +CONFIG_STREAM_PARSER=3Dy +CONFIG_NET_UDP_TUNNEL=3Dy +CONFIG_DST_CACHE=3Dy +CONFIG_CRYPTO=3Dy +CONFIG_CRYPTO_AES=3Dy +CONFIG_CRYPTO_GCM=3Dy +CONFIG_CRYPTO_CHACHA20POLY1305=3Dy +CONFIG_OVPN=3Dm diff --git a/tools/testing/selftests/net/ovpn/data-test-tcp.sh b/tools/test= ing/selftests/net/ovpn/data-test-tcp.sh new file mode 100755 index 0000000000000000000000000000000000000000..65f05659b5fd8f05d216f2c2289= 8e7f89a6ea4de --- /dev/null +++ b/tools/testing/selftests/net/ovpn/data-test-tcp.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli + +PROTO=3D"TCP" + +source data-test.sh diff --git a/tools/testing/selftests/net/ovpn/data-test.sh b/tools/testing/= selftests/net/ovpn/data-test.sh new file mode 100755 index 0000000000000000000000000000000000000000..9c649c680b57bbebaf587611939= 50def7d3dad74 --- /dev/null +++ b/tools/testing/selftests/net/ovpn/data-test.sh @@ -0,0 +1,157 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli + +#set -x +set -e + +UDP_PEERS_FILE=3D${UDP_PEERS_FILE:-udp_peers.txt} +TCP_PEERS_FILE=3D${TCP_PEERS_FILE:-tcp_peers.txt} +OVPN_CLI=3D${OVPN_CLI:-./ovpn-cli} +ALG=3D${ALG:-aes} +PROTO=3D${PROTO:-UDP} + +create_ns() { + ip netns add peer${1} +} + +setup_ns() { + MODE=3D"P2P" + + if [ ${1} -eq 0 ]; then + MODE=3D"MP" + for p in $(seq 1 ${NUM_PEERS}); do + ip link add veth${p} netns peer0 type veth peer name veth${p} netns pee= r${p} + + ip -n peer0 addr add 10.10.${p}.1/24 dev veth${p} + ip -n peer0 link set veth${p} up + + ip -n peer${p} addr add 10.10.${p}.2/24 dev veth${p} + ip -n peer${p} link set veth${p} up + done + fi + + ip netns exec peer${1} ${OVPN_CLI} new_iface tun${1} $MODE + ip -n peer${1} addr add ${2} dev tun${1} + ip -n peer${1} link set tun${1} up +} + +add_peer() { + if [ "${PROTO}" =3D=3D "UDP" ]; then + if [ ${1} -eq 0 ]; then + ip netns exec peer0 ${OVPN_CLI} new_multi_peer tun0 1 ${UDP_PEERS_FILE} + + for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} new_key tun0 ${p} 1 0 ${ALG} 0 \ + data64.key + done + else + ip netns exec peer${1} ${OVPN_CLI} new_peer tun${1} 1 ${1} 10.10.${1}.1= 1 + ip netns exec peer${1} ${OVPN_CLI} new_key tun${1} ${1} 1 0 ${ALG} 1 \ + data64.key + fi + else + if [ ${1} -eq 0 ]; then + (ip netns exec peer0 ${OVPN_CLI} listen tun0 1 ${TCP_PEERS_FILE} && { + for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} new_key tun0 ${p} 1 0 \ + ${ALG} 0 data64.key + done + }) & + sleep 5 + else + ip netns exec peer${1} ${OVPN_CLI} connect tun${1} ${1} 10.10.${1}.1 1 \ + data64.key + fi + fi +} + +cleanup() { + for p in $(seq 1 10); do + ip -n peer0 link del veth${p} 2>/dev/null || true + done + for p in $(seq 0 10); do + ip netns exec peer${p} ${OVPN_CLI} del_iface tun${p} 2>/dev/null || true + ip netns del peer${p} 2>/dev/null || true + done +} + +if [ "${PROTO}" =3D=3D "UDP" ]; then + NUM_PEERS=3D${NUM_PEERS:-$(wc -l ${UDP_PEERS_FILE} | awk '{print $1}')} +else + NUM_PEERS=3D${NUM_PEERS:-$(wc -l ${TCP_PEERS_FILE} | awk '{print $1}')} +fi + +cleanup + +modprobe -q ovpn || true + +for p in $(seq 0 ${NUM_PEERS}); do + create_ns ${p} +done + +for p in $(seq 0 ${NUM_PEERS}); do + setup_ns ${p} 5.5.5.$((${p} + 1))/24 +done + +for p in $(seq 0 ${NUM_PEERS}); do + add_peer ${p} +done + +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} set_peer tun0 ${p} 60 120 + ip netns exec peer${p} ${OVPN_CLI} set_peer tun${p} ${p} 60 120 +done + +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ping -qfc 1000 -w 5 5.5.5.$((${p} + 1)) +done + +ip netns exec peer0 iperf3 -1 -s & +sleep 1 +ip netns exec peer1 iperf3 -Z -t 3 -c 5.5.5.1 + +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} new_key tun0 ${p} 2 1 ${ALG} 0 data64.key + ip netns exec peer${p} ${OVPN_CLI} new_key tun${p} ${p} 2 1 ${ALG} 1 data= 64.key + ip netns exec peer${p} ${OVPN_CLI} swap_keys tun${p} ${p} +done + +sleep 1 +echo "Querying all peers:" +ip netns exec peer0 ${OVPN_CLI} get_peer tun0 +ip netns exec peer1 ${OVPN_CLI} get_peer tun1 + +echo "Querying peer 1:" +ip netns exec peer0 ${OVPN_CLI} get_peer tun0 1 + +echo "Querying non-existent peer 10:" +ip netns exec peer0 ${OVPN_CLI} get_peer tun0 10 || true + +ip netns exec peer0 ${OVPN_CLI} del_peer tun0 1 +ip netns exec peer1 ${OVPN_CLI} del_peer tun1 1 + +echo "Setting timeout to 10s MP:" +# bring ifaces down to prevent traffic being sent +for p in $(seq 0 ${NUM_PEERS}); do + ip -n peer${p} link set tun${p} down +done +# set short timeout +for p in $(seq 2 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} set_peer tun0 ${p} 10 10 || true + ip netns exec peer${p} ${OVPN_CLI} set_peer tun${p} ${p} 0 0 +done +# wait for peers to timeout +sleep 15 + +echo "Setting timeout to 10s P2P:" +for p in $(seq 2 ${NUM_PEERS}); do + ip netns exec peer${p} ${OVPN_CLI} set_peer tun${p} ${p} 10 10 +done +sleep 15 + +cleanup + +modprobe -r ovpn || true diff --git a/tools/testing/selftests/net/ovpn/data64.key b/tools/testing/se= lftests/net/ovpn/data64.key new file mode 100644 index 0000000000000000000000000000000000000000..a99e88c4e290f58b12f399b857b= 873f308d9ba09 --- /dev/null +++ b/tools/testing/selftests/net/ovpn/data64.key @@ -0,0 +1,5 @@ +jRqMACN7d7/aFQNT8S7jkrBD8uwrgHbG5OQZP2eu4R1Y7tfpS2bf5RHv06Vi163CGoaIiTX99R= 3B +ia9ycAH8Wz1+9PWv51dnBLur9jbShlgZ2QHLtUc4a/gfT7zZwULXuuxdLnvR21DDeMBaTbkgba= i9 +uvAa7ne1liIgGFzbv+Bas4HDVrygxIxuAnP5Qgc3648IJkZ0QEXPF+O9f0n5+QIvGCxkAUVx+5= K6 +KIs+SoeWXnAopELmoGSjUpFtJbagXK82HfdqpuUxT2Tnuef0/14SzVE/vNleBNu2ZbyrSAaah8= tE +BofkPJUBFY+YQcfZNM5Dgrw3i+Bpmpq/gpdg5w=3D=3D diff --git a/tools/testing/selftests/net/ovpn/float-test.sh b/tools/testing= /selftests/net/ovpn/float-test.sh new file mode 100755 index 0000000000000000000000000000000000000000..704ecf72ad1fe4b8c49a93d33e6= a88d0a315b42a --- /dev/null +++ b/tools/testing/selftests/net/ovpn/float-test.sh @@ -0,0 +1,122 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli + +#set -x +set -e + +UDP_PEERS_FILE=3D${UDP_PEERS_FILE:-udp_peers.txt} +TCP_PEERS_FILE=3D${TCP_PEERS_FILE:-tcp_peers.txt} +OVPN_CLI=3D${OVPN_CLI:-./ovpn-cli} +ALG=3D${ALG:-aes} +PROTO=3D${PROTO:-UDP} + +create_ns() { + ip netns add peer${1} +} + +setup_ns() { + MODE=3D"P2P" + + if [ ${1} -eq 0 ]; then + MODE=3D"MP" + for p in $(seq 1 ${NUM_PEERS}); do + ip link add veth${p} netns peer0 type veth peer name veth${p} netns pee= r${p} + + ip -n peer0 addr add 10.10.${p}.1/24 dev veth${p} + ip -n peer0 link set veth${p} up + + ip -n peer${p} addr add 10.10.${p}.2/24 dev veth${p} + ip -n peer${p} link set veth${p} up + done + fi + + ip netns exec peer${1} ${OVPN_CLI} new_iface tun${1} $MODE + ip -n peer${1} addr add ${2} dev tun${1} + ip -n peer${1} link set tun${1} up +} + +add_peer() { + if [ "${PROTO}" =3D=3D "UDP" ]; then + if [ ${1} -eq 0 ]; then + ip netns exec peer0 ${OVPN_CLI} new_multi_peer tun0 1 ${UDP_PEERS_FILE} + + for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} new_key tun0 ${p} 1 0 ${ALG} 0 \ + data64.key + done + else + ip netns exec peer${1} ${OVPN_CLI} new_peer tun${1} 1 ${1} 10.10.${1}.1= 1 + ip netns exec peer${1} ${OVPN_CLI} new_key tun${1} ${1} 1 0 ${ALG} 1 \ + data64.key + fi + else + if [ ${1} -eq 0 ]; then + (ip netns exec peer0 ${OVPN_CLI} listen tun0 1 ${TCP_PEERS_FILE} && { + for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} new_key tun0 ${p} 1 0 \ + ${ALG} 0 data64.key + done + }) & + sleep 5 + else + ip netns exec peer${1} ${OVPN_CLI} connect tun${1} ${1} 10.10.${1}.1 1 \ + 5.5.5.1 data64.key + fi + fi +} + +cleanup() { + for p in $(seq 1 10); do + ip -n peer0 link del veth${p} 2>/dev/null || true + done + for p in $(seq 0 10); do + ip netns exec peer${p} ${OVPN_CLI} del_iface tun${p} 2>/dev/null || true + ip netns del peer${p} 2>/dev/null || true + done +} + +if [ "${PROTO}" =3D=3D "UDP" ]; then + NUM_PEERS=3D${NUM_PEERS:-$(wc -l ${UDP_PEERS_FILE} | awk '{print $1}')} +else + NUM_PEERS=3D${NUM_PEERS:-$(wc -l ${TCP_PEERS_FILE} | awk '{print $1}')} +fi + +cleanup + +modprobe -q ovpn || true + +for p in $(seq 0 ${NUM_PEERS}); do + create_ns ${p} +done + +for p in $(seq 0 ${NUM_PEERS}); do + setup_ns ${p} 5.5.5.$((${p} + 1))/24 +done + +for p in $(seq 0 ${NUM_PEERS}); do + add_peer ${p} +done + +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ${OVPN_CLI} set_peer tun0 ${p} 60 120 + ip netns exec peer${p} ${OVPN_CLI} set_peer tun${p} ${p} 60 120 +done + +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer0 ping -qfc 1000 -w 5 5.5.5.$((${p} + 1)) +done +# make clients float.. +for p in $(seq 1 ${NUM_PEERS}); do + ip -n peer${p} addr del 10.10.${p}.2/24 dev veth${p} + ip -n peer${p} addr add 10.10.${p}.3/24 dev veth${p} +done +for p in $(seq 1 ${NUM_PEERS}); do + ip netns exec peer${p} ping -qfc 1000 -w 5 5.5.5.1 +done + +cleanup + +modprobe -r ovpn || true diff --git a/tools/testing/selftests/net/ovpn/ovpn-cli.c b/tools/testing/se= lftests/net/ovpn/ovpn-cli.c new file mode 100644 index 0000000000000000000000000000000000000000..546c7e345ce9fb9921ef4532f07= 56d9f8938232c --- /dev/null +++ b/tools/testing/selftests/net/ovpn/ovpn-cli.c @@ -0,0 +1,2136 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel accelerator + * + * Copyright (C) 2020-2024 OpenVPN, Inc. + * + * Author: Antonio Quartulli + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include + +/* defines to make checkpatch happy */ +#define strscpy strncpy +#define __always_unused __attribute__((__unused__)) + +/* libnl < 3.5.0 does not set the NLA_F_NESTED on its own, therefore we + * have to explicitly do it to prevent the kernel from failing upon + * parsing of the message + */ +#define nla_nest_start(_msg, _type) \ + nla_nest_start(_msg, (_type) | NLA_F_NESTED) + +uint64_t nla_get_uint(struct nlattr *attr) +{ + if (nla_len(attr) =3D=3D sizeof(uint32_t)) + return nla_get_u32(attr); + else + return nla_get_u64(attr); +} + +typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg); + +enum ovpn_key_direction { + KEY_DIR_IN =3D 0, + KEY_DIR_OUT, +}; + +#define KEY_LEN (256 / 8) +#define NONCE_LEN 8 + +#define PEER_ID_UNDEF 0x00FFFFFF + +struct nl_ctx { + struct nl_sock *nl_sock; + struct nl_msg *nl_msg; + struct nl_cb *nl_cb; + + int ovpn_dco_id; +}; + +struct ovpn_ctx { + __u8 key_enc[KEY_LEN]; + __u8 key_dec[KEY_LEN]; + __u8 nonce[NONCE_LEN]; + + enum ovpn_cipher_alg cipher; + + sa_family_t sa_family; + + __u32 peer_id; + __u16 lport; + + union { + struct sockaddr_in in4; + struct sockaddr_in6 in6; + } remote; + + union { + struct sockaddr_in in4; + struct sockaddr_in6 in6; + } peer_ip; + + bool peer_ip_set; + + unsigned int ifindex; + char ifname[IFNAMSIZ]; + enum ovpn_mode mode; + bool mode_set; + + int socket; + int cli_socket; + + __u32 keepalive_interval; + __u32 keepalive_timeout; + + enum ovpn_key_direction key_dir; + enum ovpn_key_slot key_slot; + int key_id; +}; + +static int ovpn_nl_recvmsgs(struct nl_ctx *ctx) +{ + int ret; + + ret =3D nl_recvmsgs(ctx->nl_sock, ctx->nl_cb); + + switch (ret) { + case -NLE_INTR: + fprintf(stderr, + "netlink received interrupt due to signal - ignoring\n"); + break; + case -NLE_NOMEM: + fprintf(stderr, "netlink out of memory error\n"); + break; + case -NLE_AGAIN: + fprintf(stderr, + "netlink reports blocking read - aborting wait\n"); + break; + default: + if (ret) + fprintf(stderr, "netlink reports error (%d): %s\n", + ret, nl_geterror(-ret)); + break; + } + + return ret; +} + +static struct nl_ctx *nl_ctx_alloc_flags(struct ovpn_ctx *ovpn, int cmd, + int flags) +{ + struct nl_ctx *ctx; + int err, ret; + + ctx =3D calloc(1, sizeof(*ctx)); + if (!ctx) + return NULL; + + ctx->nl_sock =3D nl_socket_alloc(); + if (!ctx->nl_sock) { + fprintf(stderr, "cannot allocate netlink socket\n"); + goto err_free; + } + + nl_socket_set_buffer_size(ctx->nl_sock, 8192, 8192); + + ret =3D genl_connect(ctx->nl_sock); + if (ret) { + fprintf(stderr, "cannot connect to generic netlink: %s\n", + nl_geterror(ret)); + goto err_sock; + } + + /* enable Extended ACK for detailed error reporting */ + err =3D 1; + setsockopt(nl_socket_get_fd(ctx->nl_sock), SOL_NETLINK, NETLINK_EXT_ACK, + &err, sizeof(err)); + + ctx->ovpn_dco_id =3D genl_ctrl_resolve(ctx->nl_sock, OVPN_FAMILY_NAME); + if (ctx->ovpn_dco_id < 0) { + fprintf(stderr, "cannot find ovpn_dco netlink component: %d\n", + ctx->ovpn_dco_id); + goto err_free; + } + + ctx->nl_msg =3D nlmsg_alloc(); + if (!ctx->nl_msg) { + fprintf(stderr, "cannot allocate netlink message\n"); + goto err_sock; + } + + ctx->nl_cb =3D nl_cb_alloc(NL_CB_DEFAULT); + if (!ctx->nl_cb) { + fprintf(stderr, "failed to allocate netlink callback\n"); + goto err_msg; + } + + nl_socket_set_cb(ctx->nl_sock, ctx->nl_cb); + + genlmsg_put(ctx->nl_msg, 0, 0, ctx->ovpn_dco_id, 0, flags, cmd, 0); + + if (ovpn->ifindex > 0) + NLA_PUT_U32(ctx->nl_msg, OVPN_A_IFINDEX, ovpn->ifindex); + + return ctx; +nla_put_failure: +err_msg: + nlmsg_free(ctx->nl_msg); +err_sock: + nl_socket_free(ctx->nl_sock); +err_free: + free(ctx); + return NULL; +} + +static struct nl_ctx *nl_ctx_alloc(struct ovpn_ctx *ovpn, int cmd) +{ + return nl_ctx_alloc_flags(ovpn, cmd, 0); +} + +static void nl_ctx_free(struct nl_ctx *ctx) +{ + if (!ctx) + return; + + nl_socket_free(ctx->nl_sock); + nlmsg_free(ctx->nl_msg); + nl_cb_put(ctx->nl_cb); + free(ctx); +} + +static int ovpn_nl_cb_error(struct sockaddr_nl (*nla)__always_unused, + struct nlmsgerr *err, void *arg) +{ + struct nlmsghdr *nlh =3D (struct nlmsghdr *)err - 1; + struct nlattr *tb_msg[NLMSGERR_ATTR_MAX + 1]; + int len =3D nlh->nlmsg_len; + struct nlattr *attrs; + int *ret =3D arg; + int ack_len =3D sizeof(*nlh) + sizeof(int) + sizeof(*nlh); + + *ret =3D err->error; + + if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS)) + return NL_STOP; + + if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) + ack_len +=3D err->msg.nlmsg_len - sizeof(*nlh); + + if (len <=3D ack_len) + return NL_STOP; + + attrs =3D (void *)((unsigned char *)nlh + ack_len); + len -=3D ack_len; + + nla_parse(tb_msg, NLMSGERR_ATTR_MAX, attrs, len, NULL); + if (tb_msg[NLMSGERR_ATTR_MSG]) { + len =3D strnlen((char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]), + nla_len(tb_msg[NLMSGERR_ATTR_MSG])); + fprintf(stderr, "kernel error: %*s\n", len, + (char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG])); + } + + if (tb_msg[NLMSGERR_ATTR_MISS_NEST]) { + fprintf(stderr, "missing required nesting type %u\n", + nla_get_u32(tb_msg[NLMSGERR_ATTR_MISS_NEST])); + } + + if (tb_msg[NLMSGERR_ATTR_MISS_TYPE]) { + fprintf(stderr, "missing required attribute type %u\n", + nla_get_u32(tb_msg[NLMSGERR_ATTR_MISS_TYPE])); + } + + return NL_STOP; +} + +static int ovpn_nl_cb_finish(struct nl_msg (*msg)__always_unused, + void *arg) +{ + int *status =3D arg; + + *status =3D 0; + return NL_SKIP; +} + +static int ovpn_nl_cb_ack(struct nl_msg (*msg)__always_unused, + void *arg) +{ + int *status =3D arg; + + *status =3D 0; + return NL_STOP; +} + +static int ovpn_nl_msg_send(struct nl_ctx *ctx, ovpn_nl_cb cb) +{ + int status =3D 1; + + nl_cb_err(ctx->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &status); + nl_cb_set(ctx->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish, + &status); + nl_cb_set(ctx->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_nl_cb_ack, &status); + + if (cb) + nl_cb_set(ctx->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, cb, ctx); + + nl_send_auto_complete(ctx->nl_sock, ctx->nl_msg); + + while (status =3D=3D 1) + ovpn_nl_recvmsgs(ctx); + + if (status < 0) + fprintf(stderr, "failed to send netlink message: %s (%d)\n", + strerror(-status), status); + + return status; +} + +static int ovpn_read_key(const char *file, struct ovpn_ctx *ctx) +{ + int idx_enc, idx_dec, ret =3D -1; + unsigned char *ckey =3D NULL; + __u8 *bkey =3D NULL; + size_t olen =3D 0; + long ckey_len; + FILE *fp; + + fp =3D fopen(file, "r"); + if (!fp) { + fprintf(stderr, "cannot open: %s\n", file); + return -1; + } + + /* get file size */ + fseek(fp, 0L, SEEK_END); + ckey_len =3D ftell(fp); + rewind(fp); + + /* if the file is longer, let's just read a portion */ + if (ckey_len > 256) + ckey_len =3D 256; + + ckey =3D malloc(ckey_len); + if (!ckey) + goto err; + + ret =3D fread(ckey, 1, ckey_len, fp); + if (ret !=3D ckey_len) { + fprintf(stderr, + "couldn't read enough data from key file: %dbytes read\n", + ret); + goto err; + } + + olen =3D 0; + ret =3D mbedtls_base64_decode(NULL, 0, &olen, ckey, ckey_len); + if (ret !=3D MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) { + char buf[256]; + + mbedtls_strerror(ret, buf, sizeof(buf)); + fprintf(stderr, "unexpected base64 error1: %s (%d)\n", buf, + ret); + + goto err; + } + + bkey =3D malloc(olen); + if (!bkey) { + fprintf(stderr, "cannot allocate binary key buffer\n"); + goto err; + } + + ret =3D mbedtls_base64_decode(bkey, olen, &olen, ckey, ckey_len); + if (ret) { + char buf[256]; + + mbedtls_strerror(ret, buf, sizeof(buf)); + fprintf(stderr, "unexpected base64 error2: %s (%d)\n", buf, + ret); + + goto err; + } + + if (olen < 2 * KEY_LEN + NONCE_LEN) { + fprintf(stderr, + "not enough data in key file, found %zdB but needs %dB\n", + olen, 2 * KEY_LEN + NONCE_LEN); + goto err; + } + + switch (ctx->key_dir) { + case KEY_DIR_IN: + idx_enc =3D 0; + idx_dec =3D 1; + break; + case KEY_DIR_OUT: + idx_enc =3D 1; + idx_dec =3D 0; + break; + default: + goto err; + } + + memcpy(ctx->key_enc, bkey + KEY_LEN * idx_enc, KEY_LEN); + memcpy(ctx->key_dec, bkey + KEY_LEN * idx_dec, KEY_LEN); + memcpy(ctx->nonce, bkey + 2 * KEY_LEN, NONCE_LEN); + + ret =3D 0; + +err: + fclose(fp); + free(bkey); + free(ckey); + + return ret; +} + +static int ovpn_read_cipher(const char *cipher, struct ovpn_ctx *ctx) +{ + if (strcmp(cipher, "aes") =3D=3D 0) + ctx->cipher =3D OVPN_CIPHER_ALG_AES_GCM; + else if (strcmp(cipher, "chachapoly") =3D=3D 0) + ctx->cipher =3D OVPN_CIPHER_ALG_CHACHA20_POLY1305; + else if (strcmp(cipher, "none") =3D=3D 0) + ctx->cipher =3D OVPN_CIPHER_ALG_NONE; + else + return -ENOTSUP; + + return 0; +} + +static int ovpn_read_key_direction(const char *dir, struct ovpn_ctx *ctx) +{ + int in_dir; + + in_dir =3D strtoll(dir, NULL, 10); + switch (in_dir) { + case KEY_DIR_IN: + case KEY_DIR_OUT: + ctx->key_dir =3D in_dir; + break; + default: + fprintf(stderr, + "invalid key direction provided. Can be 0 or 1 only\n"); + return -1; + } + + return 0; +} + +static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto) +{ + struct sockaddr_storage local_sock; + struct sockaddr_in6 *in6; + struct sockaddr_in *in; + int ret, s, sock_type; + size_t sock_len; + + if (proto =3D=3D IPPROTO_UDP) + sock_type =3D SOCK_DGRAM; + else if (proto =3D=3D IPPROTO_TCP) + sock_type =3D SOCK_STREAM; + else + return -EINVAL; + + s =3D socket(family, sock_type, 0); + if (s < 0) { + perror("cannot create socket"); + return -1; + } + + memset((char *)&local_sock, 0, sizeof(local_sock)); + + switch (family) { + case AF_INET: + in =3D (struct sockaddr_in *)&local_sock; + in->sin_family =3D family; + in->sin_port =3D htons(ctx->lport); + in->sin_addr.s_addr =3D htonl(INADDR_ANY); + sock_len =3D sizeof(*in); + break; + case AF_INET6: + in6 =3D (struct sockaddr_in6 *)&local_sock; + in6->sin6_family =3D family; + in6->sin6_port =3D htons(ctx->lport); + in6->sin6_addr =3D in6addr_any; + sock_len =3D sizeof(*in6); + break; + default: + return -1; + } + + int opt =3D 1; + + ret =3D setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + if (ret < 0) { + perror("setsockopt for SO_REUSEADDR"); + return ret; + } + + ret =3D setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); + if (ret < 0) { + perror("setsockopt for SO_REUSEPORT"); + return ret; + } + + if (family =3D=3D AF_INET6) { + opt =3D 0; + if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, + sizeof(opt))) { + perror("failed to set IPV6_V6ONLY"); + return -1; + } + } + + ret =3D bind(s, (struct sockaddr *)&local_sock, sock_len); + if (ret < 0) { + perror("cannot bind socket"); + goto err_socket; + } + + ctx->socket =3D s; + ctx->sa_family =3D family; + return 0; + +err_socket: + close(s); + return -1; +} + +static int ovpn_udp_socket(struct ovpn_ctx *ctx, sa_family_t family) +{ + return ovpn_socket(ctx, family, IPPROTO_UDP); +} + +static int ovpn_listen(struct ovpn_ctx *ctx, sa_family_t family) +{ + int ret; + + ret =3D ovpn_socket(ctx, family, IPPROTO_TCP); + if (ret < 0) + return ret; + + ret =3D listen(ctx->socket, 10); + if (ret < 0) { + perror("listen"); + close(ctx->socket); + return -1; + } + + return 0; +} + +static int ovpn_accept(struct ovpn_ctx *ctx) +{ + socklen_t socklen; + int ret; + + socklen =3D sizeof(ctx->remote); + ret =3D accept(ctx->socket, (struct sockaddr *)&ctx->remote, &socklen); + if (ret < 0) { + perror("accept"); + goto err; + } + + fprintf(stderr, "Connection received!\n"); + + switch (socklen) { + case sizeof(struct sockaddr_in): + case sizeof(struct sockaddr_in6): + break; + default: + fprintf(stderr, "error: expecting IPv4 or IPv6 connection\n"); + close(ret); + ret =3D -EINVAL; + goto err; + } + + return ret; +err: + close(ctx->socket); + return ret; +} + +static int ovpn_connect(struct ovpn_ctx *ovpn) +{ + socklen_t socklen; + int s, ret; + + s =3D socket(ovpn->remote.in4.sin_family, SOCK_STREAM, 0); + if (s < 0) { + perror("cannot create socket"); + return -1; + } + + switch (ovpn->remote.in4.sin_family) { + case AF_INET: + socklen =3D sizeof(struct sockaddr_in); + break; + case AF_INET6: + socklen =3D sizeof(struct sockaddr_in6); + break; + default: + return -EOPNOTSUPP; + } + + ret =3D connect(s, (struct sockaddr *)&ovpn->remote, socklen); + if (ret < 0) { + perror("connect"); + goto err; + } + + fprintf(stderr, "connected\n"); + + ovpn->socket =3D s; + + return 0; +err: + close(s); + return ret; +} + +static int ovpn_new_peer(struct ovpn_ctx *ovpn, bool is_tcp) +{ + struct nlattr *attr; + struct nl_ctx *ctx; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_PEER_NEW); + if (!ctx) + return -ENOMEM; + + attr =3D nla_nest_start(ctx->nl_msg, OVPN_A_PEER); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_ID, ovpn->peer_id); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_SOCKET, ovpn->socket); + + if (!is_tcp) { + switch (ovpn->remote.in4.sin_family) { + case AF_INET: + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_REMOTE_IPV4, + ovpn->remote.in4.sin_addr.s_addr); + NLA_PUT_U16(ctx->nl_msg, OVPN_A_PEER_REMOTE_PORT, + ovpn->remote.in4.sin_port); + break; + case AF_INET6: + NLA_PUT(ctx->nl_msg, OVPN_A_PEER_REMOTE_IPV6, + sizeof(ovpn->remote.in6.sin6_addr), + &ovpn->remote.in6.sin6_addr); + NLA_PUT_U32(ctx->nl_msg, + OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID, + ovpn->remote.in6.sin6_scope_id); + NLA_PUT_U16(ctx->nl_msg, OVPN_A_PEER_REMOTE_PORT, + ovpn->remote.in6.sin6_port); + break; + default: + fprintf(stderr, + "Invalid family for remote socket address\n"); + goto nla_put_failure; + } + } + + if (ovpn->peer_ip_set) { + switch (ovpn->peer_ip.in4.sin_family) { + case AF_INET: + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_VPN_IPV4, + ovpn->peer_ip.in4.sin_addr.s_addr); + break; + case AF_INET6: + NLA_PUT(ctx->nl_msg, OVPN_A_PEER_VPN_IPV6, + sizeof(struct in6_addr), + &ovpn->peer_ip.in6.sin6_addr); + break; + default: + fprintf(stderr, "Invalid family for peer address\n"); + goto nla_put_failure; + } + } + + nla_nest_end(ctx->nl_msg, attr); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_set_peer(struct ovpn_ctx *ovpn) +{ + struct nlattr *attr; + struct nl_ctx *ctx; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_PEER_SET); + if (!ctx) + return -ENOMEM; + + attr =3D nla_nest_start(ctx->nl_msg, OVPN_A_PEER); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_ID, ovpn->peer_id); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_KEEPALIVE_INTERVAL, + ovpn->keepalive_interval); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_KEEPALIVE_TIMEOUT, + ovpn->keepalive_timeout); + nla_nest_end(ctx->nl_msg, attr); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_del_peer(struct ovpn_ctx *ovpn) +{ + struct nlattr *attr; + struct nl_ctx *ctx; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_PEER_DEL); + if (!ctx) + return -ENOMEM; + + attr =3D nla_nest_start(ctx->nl_msg, OVPN_A_PEER); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_ID, ovpn->peer_id); + nla_nest_end(ctx->nl_msg, attr); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_handle_peer(struct nl_msg *msg, void *arg) +{ + struct nlattr *pattrs[OVPN_A_PEER_MAX + 1]; + struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); + struct nlattr *attrs[OVPN_A_MAX + 1]; + __u16 rport =3D 0, lport =3D 0; + + nla_parse(attrs, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0), + genlmsg_attrlen(gnlh, 0), NULL); + + if (!attrs[OVPN_A_PEER]) { + fprintf(stderr, "no packet content in netlink message\n"); + return NL_SKIP; + } + + nla_parse(pattrs, OVPN_A_PEER_MAX, nla_data(attrs[OVPN_A_PEER]), + nla_len(attrs[OVPN_A_PEER]), NULL); + + if (pattrs[OVPN_A_PEER_ID]) + fprintf(stderr, "* Peer %u\n", + nla_get_u32(pattrs[OVPN_A_PEER_ID])); + + if (pattrs[OVPN_A_PEER_VPN_IPV4]) { + char buf[INET_ADDRSTRLEN]; + + inet_ntop(AF_INET, nla_data(pattrs[OVPN_A_PEER_VPN_IPV4]), + buf, sizeof(buf)); + fprintf(stderr, "\tVPN IPv4: %s\n", buf); + } + + if (pattrs[OVPN_A_PEER_VPN_IPV6]) { + char buf[INET6_ADDRSTRLEN]; + + inet_ntop(AF_INET6, nla_data(pattrs[OVPN_A_PEER_VPN_IPV6]), + buf, sizeof(buf)); + fprintf(stderr, "\tVPN IPv6: %s\n", buf); + } + + if (pattrs[OVPN_A_PEER_LOCAL_PORT]) + lport =3D ntohs(nla_get_u16(pattrs[OVPN_A_PEER_LOCAL_PORT])); + + if (pattrs[OVPN_A_PEER_REMOTE_PORT]) + rport =3D ntohs(nla_get_u16(pattrs[OVPN_A_PEER_REMOTE_PORT])); + + if (pattrs[OVPN_A_PEER_REMOTE_IPV6]) { + void *ip =3D pattrs[OVPN_A_PEER_REMOTE_IPV6]; + char buf[INET6_ADDRSTRLEN]; + int scope_id =3D -1; + + if (pattrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID]) { + void *p =3D pattrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID]; + + scope_id =3D nla_get_u32(p); + } + + inet_ntop(AF_INET6, nla_data(ip), buf, sizeof(buf)); + fprintf(stderr, "\tRemote: %s:%hu (scope-id: %u)\n", buf, rport, + scope_id); + + if (pattrs[OVPN_A_PEER_LOCAL_IPV6]) { + void *ip =3D pattrs[OVPN_A_PEER_LOCAL_IPV6]; + + inet_ntop(AF_INET6, nla_data(ip), buf, sizeof(buf)); + fprintf(stderr, "\tLocal: %s:%hu\n", buf, lport); + } + } + + if (pattrs[OVPN_A_PEER_REMOTE_IPV4]) { + void *ip =3D pattrs[OVPN_A_PEER_REMOTE_IPV4]; + char buf[INET_ADDRSTRLEN]; + + inet_ntop(AF_INET, nla_data(ip), buf, sizeof(buf)); + fprintf(stderr, "\tRemote: %s:%hu\n", buf, rport); + + if (pattrs[OVPN_A_PEER_LOCAL_IPV4]) { + void *p =3D pattrs[OVPN_A_PEER_LOCAL_IPV4]; + + inet_ntop(AF_INET, nla_data(p), buf, sizeof(buf)); + fprintf(stderr, "\tLocal: %s:%hu\n", buf, lport); + } + } + + if (pattrs[OVPN_A_PEER_KEEPALIVE_INTERVAL]) { + void *p =3D pattrs[OVPN_A_PEER_KEEPALIVE_INTERVAL]; + + fprintf(stderr, "\tKeepalive interval: %u sec\n", + nla_get_u32(p)); + } + + if (pattrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) + fprintf(stderr, "\tKeepalive timeout: %u sec\n", + nla_get_u32(pattrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT])); + + if (pattrs[OVPN_A_PEER_VPN_RX_BYTES]) + fprintf(stderr, "\tVPN RX bytes: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_VPN_RX_BYTES])); + + if (pattrs[OVPN_A_PEER_VPN_TX_BYTES]) + fprintf(stderr, "\tVPN TX bytes: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_VPN_TX_BYTES])); + + if (pattrs[OVPN_A_PEER_VPN_RX_PACKETS]) + fprintf(stderr, "\tVPN RX packets: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_VPN_RX_PACKETS])); + + if (pattrs[OVPN_A_PEER_VPN_TX_PACKETS]) + fprintf(stderr, "\tVPN TX packets: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_VPN_TX_PACKETS])); + + if (pattrs[OVPN_A_PEER_LINK_RX_BYTES]) + fprintf(stderr, "\tLINK RX bytes: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_LINK_RX_BYTES])); + + if (pattrs[OVPN_A_PEER_LINK_TX_BYTES]) + fprintf(stderr, "\tLINK TX bytes: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_LINK_TX_BYTES])); + + if (pattrs[OVPN_A_PEER_LINK_RX_PACKETS]) + fprintf(stderr, "\tLINK RX packets: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_LINK_RX_PACKETS])); + + if (pattrs[OVPN_A_PEER_LINK_TX_PACKETS]) + fprintf(stderr, "\tLINK TX packets: %" PRIu64 "\n", + nla_get_uint(pattrs[OVPN_A_PEER_LINK_TX_PACKETS])); + + return NL_SKIP; +} + +static int ovpn_get_peer(struct ovpn_ctx *ovpn) +{ + int flags =3D 0, ret =3D -1; + struct nlattr *attr; + struct nl_ctx *ctx; + + if (ovpn->peer_id =3D=3D PEER_ID_UNDEF) + flags =3D NLM_F_DUMP; + + ctx =3D nl_ctx_alloc_flags(ovpn, OVPN_CMD_PEER_GET, flags); + if (!ctx) + return -ENOMEM; + + if (ovpn->peer_id !=3D PEER_ID_UNDEF) { + attr =3D nla_nest_start(ctx->nl_msg, OVPN_A_PEER); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_PEER_ID, ovpn->peer_id); + nla_nest_end(ctx->nl_msg, attr); + } + + ret =3D ovpn_nl_msg_send(ctx, ovpn_handle_peer); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_new_key(struct ovpn_ctx *ovpn) +{ + struct nlattr *keyconf, *key_dir; + struct nl_ctx *ctx; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_KEY_NEW); + if (!ctx) + return -ENOMEM; + + keyconf =3D nla_nest_start(ctx->nl_msg, OVPN_A_KEYCONF); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_PEER_ID, ovpn->peer_id); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_SLOT, ovpn->key_slot); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_KEY_ID, ovpn->key_id); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_CIPHER_ALG, ovpn->cipher); + + key_dir =3D nla_nest_start(ctx->nl_msg, OVPN_A_KEYCONF_ENCRYPT_DIR); + NLA_PUT(ctx->nl_msg, OVPN_A_KEYDIR_CIPHER_KEY, KEY_LEN, ovpn->key_enc); + NLA_PUT(ctx->nl_msg, OVPN_A_KEYDIR_NONCE_TAIL, NONCE_LEN, ovpn->nonce); + nla_nest_end(ctx->nl_msg, key_dir); + + key_dir =3D nla_nest_start(ctx->nl_msg, OVPN_A_KEYCONF_DECRYPT_DIR); + NLA_PUT(ctx->nl_msg, OVPN_A_KEYDIR_CIPHER_KEY, KEY_LEN, ovpn->key_dec); + NLA_PUT(ctx->nl_msg, OVPN_A_KEYDIR_NONCE_TAIL, NONCE_LEN, ovpn->nonce); + nla_nest_end(ctx->nl_msg, key_dir); + + nla_nest_end(ctx->nl_msg, keyconf); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_del_key(struct ovpn_ctx *ovpn) +{ + struct nlattr *keyconf; + struct nl_ctx *ctx; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_KEY_DEL); + if (!ctx) + return -ENOMEM; + + keyconf =3D nla_nest_start(ctx->nl_msg, OVPN_A_KEYCONF); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_PEER_ID, ovpn->peer_id); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_SLOT, OVPN_KEY_SLOT_PRIMARY); + nla_nest_end(ctx->nl_msg, keyconf); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +static int ovpn_swap_keys(struct ovpn_ctx *ovpn) +{ + struct nl_ctx *ctx; + struct nlattr *kc; + int ret =3D -1; + + ctx =3D nl_ctx_alloc(ovpn, OVPN_CMD_KEY_SWAP); + if (!ctx) + return -ENOMEM; + + kc =3D nla_nest_start(ctx->nl_msg, OVPN_A_KEYCONF); + NLA_PUT_U32(ctx->nl_msg, OVPN_A_KEYCONF_PEER_ID, ovpn->peer_id); + nla_nest_end(ctx->nl_msg, kc); + + ret =3D ovpn_nl_msg_send(ctx, NULL); +nla_put_failure: + nl_ctx_free(ctx); + return ret; +} + +#define OVPN_ADDATTR(_msg, _max_size, _attr, _data, _size) { \ + if (ovpn_addattr(_msg, _max_size, _attr, _data, _size) < 0) \ + goto err; \ +} + +#define NLMSG_TAIL(nmsg) \ + ((struct rtattr *)(((uint8_t *)(nmsg)) + \ + NLMSG_ALIGN((nmsg)->nlmsg_len))) + +#define OVPN_NEST(_msg, _max_size, _attr) ({ \ + struct rtattr *_nest =3D NLMSG_TAIL(_msg); \ + OVPN_ADDATTR(_msg, _max_size, _attr, NULL, 0); \ + _nest; \ +}) + +#define OVPN_NEST_END(_msg, _nest) { \ + _nest->rta_len =3D (void *)NLMSG_TAIL(_msg) - (void *)_nest; \ +} + +/** + * Helper function used to easily add attributes to a rtnl message + */ +static int ovpn_addattr(struct nlmsghdr *n, int maxlen, int type, + const void *data, int alen) +{ + int len =3D RTA_LENGTH(alen); + struct rtattr *rta; + + if ((int)(NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len)) > maxlen) { + fprintf(stderr, "%s: rtnl: message exceeded bound of %d", + __func__, maxlen); + return -EMSGSIZE; + } + + rta =3D NLMSG_TAIL(n); + rta->rta_type =3D type; + rta->rta_len =3D len; + + if (!data) + memset(RTA_DATA(rta), 0, alen); + else + memcpy(RTA_DATA(rta), data, alen); + + n->nlmsg_len =3D NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); + + return 0; +} + +#define RT_SNDBUF_SIZE (1024 * 2) +#define RT_RCVBUF_SIZE (1024 * 4) + +/** + * Open RTNL socket + */ +static int ovpn_rt_socket(void) +{ + int sndbuf =3D RT_SNDBUF_SIZE, rcvbuf =3D RT_RCVBUF_SIZE, fd; + + fd =3D socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + if (fd < 0) { + fprintf(stderr, "%s: cannot open netlink socket", __func__); + return fd; + } + + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, + sizeof(sndbuf)) < 0) { + fprintf(stderr, "%s: SO_SNDBUF", __func__); + close(fd); + return -1; + } + + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, + sizeof(rcvbuf)) < 0) { + fprintf(stderr, "%s: SO_RCVBUF", __func__); + close(fd); + return -1; + } + + return fd; +} + +/** + * Bind socket to Netlink subsystem + */ +static int ovpn_rt_bind(int fd, uint32_t groups) +{ + struct sockaddr_nl local =3D { }; + socklen_t addr_len; + + local.nl_family =3D AF_NETLINK; + local.nl_groups =3D groups; + + if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) { + fprintf(stderr, "%s: cannot bind netlink socket: %d", __func__, + errno); + return -errno; + } + + addr_len =3D sizeof(local); + if (getsockname(fd, (struct sockaddr *)&local, &addr_len) < 0) { + fprintf(stderr, "%s: cannot getsockname: %d", __func__, errno); + return -errno; + } + + if (addr_len !=3D sizeof(local)) { + fprintf(stderr, "%s: wrong address length %d", __func__, + addr_len); + return -EINVAL; + } + + if (local.nl_family !=3D AF_NETLINK) { + fprintf(stderr, "%s: wrong address family %d", __func__, + local.nl_family); + return -EINVAL; + } + + return 0; +} + +typedef int (*ovpn_parse_reply_cb)(struct nlmsghdr *msg, void *arg); + +/** + * Send Netlink message and run callback on reply (if specified) + */ +static int ovpn_rt_send(struct nlmsghdr *payload, pid_t peer, + unsigned int groups, ovpn_parse_reply_cb cb, + void *arg_cb) +{ + int len, rem_len, fd, ret, rcv_len; + struct sockaddr_nl nladdr =3D { }; + struct nlmsgerr *err; + struct nlmsghdr *h; + char buf[1024 * 16]; + struct iovec iov =3D { + .iov_base =3D payload, + .iov_len =3D payload->nlmsg_len, + }; + struct msghdr nlmsg =3D { + .msg_name =3D &nladdr, + .msg_namelen =3D sizeof(nladdr), + .msg_iov =3D &iov, + .msg_iovlen =3D 1, + }; + + nladdr.nl_family =3D AF_NETLINK; + nladdr.nl_pid =3D peer; + nladdr.nl_groups =3D groups; + + payload->nlmsg_seq =3D time(NULL); + + /* no need to send reply */ + if (!cb) + payload->nlmsg_flags |=3D NLM_F_ACK; + + fd =3D ovpn_rt_socket(); + if (fd < 0) { + fprintf(stderr, "%s: can't open rtnl socket", __func__); + return -errno; + } + + ret =3D ovpn_rt_bind(fd, 0); + if (ret < 0) { + fprintf(stderr, "%s: can't bind rtnl socket", __func__); + ret =3D -errno; + goto out; + } + + ret =3D sendmsg(fd, &nlmsg, 0); + if (ret < 0) { + fprintf(stderr, "%s: rtnl: error on sendmsg()", __func__); + ret =3D -errno; + goto out; + } + + /* prepare buffer to store RTNL replies */ + memset(buf, 0, sizeof(buf)); + iov.iov_base =3D buf; + + while (1) { + /* + * iov_len is modified by recvmsg(), therefore has to be initialized bef= ore + * using it again + */ + iov.iov_len =3D sizeof(buf); + rcv_len =3D recvmsg(fd, &nlmsg, 0); + if (rcv_len < 0) { + if (errno =3D=3D EINTR || errno =3D=3D EAGAIN) { + fprintf(stderr, "%s: interrupted call", + __func__); + continue; + } + fprintf(stderr, "%s: rtnl: error on recvmsg()", + __func__); + ret =3D -errno; + goto out; + } + + if (rcv_len =3D=3D 0) { + fprintf(stderr, + "%s: rtnl: socket reached unexpected EOF", + __func__); + ret =3D -EIO; + goto out; + } + + if (nlmsg.msg_namelen !=3D sizeof(nladdr)) { + fprintf(stderr, + "%s: sender address length: %u (expected %zu)", + __func__, nlmsg.msg_namelen, sizeof(nladdr)); + ret =3D -EIO; + goto out; + } + + h =3D (struct nlmsghdr *)buf; + while (rcv_len >=3D (int)sizeof(*h)) { + len =3D h->nlmsg_len; + rem_len =3D len - sizeof(*h); + + if (rem_len < 0 || len > rcv_len) { + if (nlmsg.msg_flags & MSG_TRUNC) { + fprintf(stderr, "%s: truncated message", + __func__); + ret =3D -EIO; + goto out; + } + fprintf(stderr, "%s: malformed message: len=3D%d", + __func__, len); + ret =3D -EIO; + goto out; + } + + if (h->nlmsg_type =3D=3D NLMSG_DONE) { + ret =3D 0; + goto out; + } + + if (h->nlmsg_type =3D=3D NLMSG_ERROR) { + err =3D (struct nlmsgerr *)NLMSG_DATA(h); + if (rem_len < (int)sizeof(struct nlmsgerr)) { + fprintf(stderr, "%s: ERROR truncated", + __func__); + ret =3D -EIO; + goto out; + } + if (!err->error) { + ret =3D 0; + if (cb) { + int r =3D cb(h, arg_cb); + if (r <=3D 0) + ret =3D r; + } + } else { + fprintf(stderr, + "%s: rtnl: generic error (%d): %s", + __func__, err->error, + strerror(-err->error)); + ret =3D err->error; + } + goto out; + } + + if (cb) { + int r =3D cb(h, arg_cb); + + if (r <=3D 0) { + ret =3D r; + goto out; + } + } else { + fprintf(stderr, "%s: RTNL: unexpected reply", + __func__); + } + + rcv_len -=3D NLMSG_ALIGN(len); + h =3D (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); + } + + if (nlmsg.msg_flags & MSG_TRUNC) { + fprintf(stderr, "%s: message truncated", __func__); + continue; + } + + if (rcv_len) { + fprintf(stderr, "%s: rtnl: %d not parsed bytes", + __func__, rcv_len); + ret =3D -1; + goto out; + } + } +out: + close(fd); + + return ret; +} + +struct ovpn_link_req { + struct nlmsghdr n; + struct ifinfomsg i; + char buf[256]; +}; + +static int ovpn_new_iface(struct ovpn_ctx *ovpn) +{ + struct rtattr *linkinfo, *data; + struct ovpn_link_req req =3D { }; + int ret =3D -1; + + fprintf(stdout, "Creating interface %s with mode %u\n", ovpn->ifname, + ovpn->mode); + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_flags =3D NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL; + req.n.nlmsg_type =3D RTM_NEWLINK; + + OVPN_ADDATTR(&req.n, sizeof(req), IFLA_IFNAME, ovpn->ifname, + strlen(ovpn->ifname) + 1); + + linkinfo =3D OVPN_NEST(&req.n, sizeof(req), IFLA_LINKINFO); + OVPN_ADDATTR(&req.n, sizeof(req), IFLA_INFO_KIND, OVPN_FAMILY_NAME, + strlen(OVPN_FAMILY_NAME) + 1); + + if (ovpn->mode_set) { + data =3D OVPN_NEST(&req.n, sizeof(req), IFLA_INFO_DATA); + OVPN_ADDATTR(&req.n, sizeof(req), IFLA_OVPN_MODE, &ovpn->mode, + sizeof(uint8_t)); + OVPN_NEST_END(&req.n, data); + } + + OVPN_NEST_END(&req.n, linkinfo); + + req.i.ifi_family =3D AF_PACKET; + + ret =3D ovpn_rt_send(&req.n, 0, 0, NULL, NULL); +err: + return ret; +} + +static int ovpn_del_iface(struct ovpn_ctx *ovpn) +{ + struct ovpn_link_req req =3D { }; + + fprintf(stdout, "Deleting interface %s ifindex %u\n", ovpn->ifname, + ovpn->ifindex); + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_flags =3D NLM_F_REQUEST; + req.n.nlmsg_type =3D RTM_DELLINK; + + req.i.ifi_family =3D AF_PACKET; + req.i.ifi_index =3D ovpn->ifindex; + + return ovpn_rt_send(&req.n, 0, 0, NULL, NULL); +} + +static int nl_seq_check(struct nl_msg *msg, void *arg) +{ + return NL_OK; +} + +struct mcast_handler_args { + const char *group; + int id; +}; + +static int mcast_family_handler(struct nl_msg *msg, void *arg) +{ + struct mcast_handler_args *grp =3D arg; + struct nlattr *tb[CTRL_ATTR_MAX + 1]; + struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); + struct nlattr *mcgrp; + int rem_mcgrp; + + nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), + genlmsg_attrlen(gnlh, 0), NULL); + + if (!tb[CTRL_ATTR_MCAST_GROUPS]) + return NL_SKIP; + + nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) { + struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1]; + + nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX, + nla_data(mcgrp), nla_len(mcgrp), NULL); + + if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] || + !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]) + continue; + if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]), + grp->group, nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]))) + continue; + grp->id =3D nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]); + break; + } + + return NL_SKIP; +} + +static int mcast_error_handler(struct sockaddr_nl *nla, struct nlmsgerr *e= rr, + void *arg) +{ + int *ret =3D arg; + + *ret =3D err->error; + return NL_STOP; +} + +static int mcast_ack_handler(struct nl_msg *msg, void *arg) +{ + int *ret =3D arg; + + *ret =3D 0; + return NL_STOP; +} + +static int ovpn_handle_msg(struct nl_msg *msg, void *arg) +{ + struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg)); + struct nlattr *attrs[OVPN_A_MAX + 1]; + struct nlmsghdr *nlh =3D nlmsg_hdr(msg); + //enum ovpn_del_peer_reason reason; + char ifname[IF_NAMESIZE]; + int *ret =3D arg; + __u32 ifindex; + + fprintf(stderr, "received message from ovpn-dco\n"); + + *ret =3D -1; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fprintf(stderr, "invalid header\n"); + return NL_STOP; + } + + if (nla_parse(attrs, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0), + genlmsg_attrlen(gnlh, 0), NULL)) { + fprintf(stderr, "received bogus data from ovpn-dco\n"); + return NL_STOP; + } + + if (!attrs[OVPN_A_IFINDEX]) { + fprintf(stderr, "no ifindex in this message\n"); + return NL_STOP; + } + + ifindex =3D nla_get_u32(attrs[OVPN_A_IFINDEX]); + if (!if_indextoname(ifindex, ifname)) { + fprintf(stderr, "cannot resolve ifname for ifindex: %u\n", + ifindex); + return NL_STOP; + } + + switch (gnlh->cmd) { + case OVPN_CMD_PEER_DEL_NTF: + /*if (!attrs[OVPN_A_DEL_PEER_REASON]) { + * fprintf(stderr, "no reason in DEL_PEER message\n"); + * return NL_STOP; + *} + * + *reason =3D nla_get_u8(attrs[OVPN_A_DEL_PEER_REASON]); + *fprintf(stderr, + * "received CMD_DEL_PEER, ifname: %s reason: %d\n", + * ifname, reason); + */ + fprintf(stdout, "received CMD_PEER_DEL_NTF\n"); + break; + case OVPN_CMD_KEY_SWAP_NTF: + fprintf(stdout, "received CMD_KEY_SWAP_NTF\n"); + break; + default: + fprintf(stderr, "received unknown command: %d\n", gnlh->cmd); + return NL_STOP; + } + + *ret =3D 0; + return NL_OK; +} + +static int ovpn_get_mcast_id(struct nl_sock *sock, const char *family, + const char *group) +{ + struct nl_msg *msg; + struct nl_cb *cb; + int ret, ctrlid; + struct mcast_handler_args grp =3D { + .group =3D group, + .id =3D -ENOENT, + }; + + msg =3D nlmsg_alloc(); + if (!msg) + return -ENOMEM; + + cb =3D nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) { + ret =3D -ENOMEM; + goto out_fail_cb; + } + + ctrlid =3D genl_ctrl_resolve(sock, "nlctrl"); + + genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); + + ret =3D -ENOBUFS; + NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); + + ret =3D nl_send_auto_complete(sock, msg); + if (ret < 0) + goto nla_put_failure; + + ret =3D 1; + + nl_cb_err(cb, NL_CB_CUSTOM, mcast_error_handler, &ret); + nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, mcast_ack_handler, &ret); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, mcast_family_handler, &grp); + + while (ret > 0) + nl_recvmsgs(sock, cb); + + if (ret =3D=3D 0) + ret =3D grp.id; + nla_put_failure: + nl_cb_put(cb); + out_fail_cb: + nlmsg_free(msg); + return ret; +} + +static int ovpn_listen_mcast(void) +{ + struct nl_sock *sock; + struct nl_cb *cb; + int mcid, ret; + + sock =3D nl_socket_alloc(); + if (!sock) { + fprintf(stderr, "cannot allocate netlink socket\n"); + goto err_free; + } + + nl_socket_set_buffer_size(sock, 8192, 8192); + + ret =3D genl_connect(sock); + if (ret < 0) { + fprintf(stderr, "cannot connect to generic netlink: %s\n", + nl_geterror(ret)); + goto err_free; + } + + mcid =3D ovpn_get_mcast_id(sock, OVPN_FAMILY_NAME, OVPN_MCGRP_PEERS); + if (mcid < 0) { + fprintf(stderr, "cannot get mcast group: %s\n", + nl_geterror(mcid)); + goto err_free; + } + + ret =3D nl_socket_add_membership(sock, mcid); + if (ret) { + fprintf(stderr, "failed to join mcast group: %d\n", ret); + goto err_free; + } + + ret =3D 1; + cb =3D nl_cb_alloc(NL_CB_DEFAULT); + nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl_seq_check, NULL); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, &ret); + nl_cb_err(cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &ret); + + while (ret =3D=3D 1) { + int err =3D nl_recvmsgs(sock, cb); + + if (err < 0) { + fprintf(stderr, + "cannot receive netlink message: (%d) %s\n", + err, nl_geterror(-err)); + ret =3D -1; + break; + } + } + + nl_cb_put(cb); +err_free: + nl_socket_free(sock); + return ret; +} + +static void usage(const char *cmd) +{ + fprintf(stderr, "Error: invalid arguments.\n\n"); + fprintf(stderr, + "Usage %s [arguments..]\n", + cmd); + fprintf(stderr, "\tiface: tun interface name\n\n"); + + fprintf(stderr, + "* connect : start connecting peer o= f TCP-based VPN session\n"); + fprintf(stderr, "\tpeer-id: peer ID of the connecting peer\n"); + fprintf(stderr, "\tremote-addr: peer IP address\n"); + fprintf(stderr, "\tremote-port: peer TCP port\n"); + + fprintf(stderr, + "* listen : listen for incoming peer TCP connections= \n"); + fprintf(stderr, "\tlport: src TCP port\n"); + fprintf(stderr, + "\tpeers_file: file containing one peer per line: Line format:\n"); + fprintf(stderr, "\t\t \n\n"); + + fprintf(stderr, + "* new_peer [vpnaddr]: add new peer\n"= ); + fprintf(stderr, + "\tpeer-id: peer ID to be used in data packets to/from this peer\n"); + fprintf(stderr, "\tlocal-port: local UDP port\n"); + fprintf(stderr, "\tremote-addr: peer IP address\n"); + fprintf(stderr, "\tremote-port: peer UDP port\n"); + fprintf(stderr, "\tvpnaddr: peer VPN IP\n\n"); + + fprintf(stderr, + "* new_multi_peer : add multiple peers as listed in the fi= le\n"); + fprintf(stderr, "\tlport: local UDP port to bind to\n"); + fprintf(stderr, + "\tfile: text file containing one peer per line. Line format:\n"); + fprintf(stderr, "\t\t \n\n"); + + fprintf(stderr, + "* set_peer : set peer= attributes\n"); + fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n"); + fprintf(stderr, + "\tkeepalive_interval: interval for sending ping messages\n"); + fprintf(stderr, + "\tkeepalive_timeout: time after which a peer is timed out\n\n"); + + fprintf(stderr, "* del_peer : delete peer\n"); + fprintf(stderr, "\tpeer-id: peer ID of the peer to delete\n\n"); + + fprintf(stderr, + "* new_key : set = data channel key\n"); + fprintf(stderr, + "\tpeer-id: peer ID of the peer to configure the key for\n"); + fprintf(stderr, + "\tcipher: cipher to use, supported: aes (AES-GCM), chachapoly (CHACHA20= POLY1305), none\n"); + fprintf(stderr, + "\tkey_dir: key direction, must 0 on one host and 1 on the other\n"); + fprintf(stderr, "\tkey_file: file containing the pre-shared key\n\n"); + + fprintf(stderr, + "* del_key : erase existing data channel key\n"); + fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n\n"); + + fprintf(stderr, + "* swap_keys : swap primary and secondary key slots\n"); + fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n\n"); + + fprintf(stderr, + "* listen_mcast: listen to ovpn-dco netlink multicast messages\n"); +} + +static int ovpn_parse_remote(struct ovpn_ctx *ovpn, const char *host, + const char *service, const char *vpnip) +{ + int ret; + struct addrinfo *result; + struct addrinfo hints =3D { + .ai_family =3D ovpn->sa_family, + .ai_socktype =3D SOCK_DGRAM, + .ai_protocol =3D IPPROTO_UDP + }; + + if (host) { + ret =3D getaddrinfo(host, service, &hints, &result); + if (ret =3D=3D EAI_NONAME || ret =3D=3D EAI_FAIL) + return -1; + + if (!(result->ai_family =3D=3D AF_INET && + result->ai_addrlen =3D=3D sizeof(struct sockaddr_in)) && + !(result->ai_family =3D=3D AF_INET6 && + result->ai_addrlen =3D=3D sizeof(struct sockaddr_in6))) { + ret =3D -EINVAL; + goto out; + } + + memcpy(&ovpn->remote, result->ai_addr, result->ai_addrlen); + } + + if (vpnip) { + ret =3D getaddrinfo(vpnip, NULL, &hints, &result); + if (ret =3D=3D EAI_NONAME || ret =3D=3D EAI_FAIL) + return -1; + + if (!(result->ai_family =3D=3D AF_INET && + result->ai_addrlen =3D=3D sizeof(struct sockaddr_in)) && + !(result->ai_family =3D=3D AF_INET6 && + result->ai_addrlen =3D=3D sizeof(struct sockaddr_in6))) { + ret =3D -EINVAL; + goto out; + } + + memcpy(&ovpn->peer_ip, result->ai_addr, result->ai_addrlen); + ovpn->sa_family =3D result->ai_family; + + ovpn->peer_ip_set =3D true; + } + + ret =3D 0; +out: + freeaddrinfo(result); + return ret; +} + +static int ovpn_parse_new_peer(struct ovpn_ctx *ovpn, const char *peer_id, + const char *raddr, const char *rport, + const char *vpnip) +{ + ovpn->peer_id =3D strtoul(peer_id, NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + return ovpn_parse_remote(ovpn, raddr, rport, vpnip); +} + +static void ovpn_send_tcp_data(int socket) +{ + uint16_t len =3D htons(1000); + uint8_t buf[1002]; + int ret; + + memcpy(buf, &len, sizeof(len)); + memset(buf + sizeof(len), 0x86, sizeof(buf) - sizeof(len)); + + ret =3D send(socket, buf, sizeof(buf), 0); + + fprintf(stdout, "Sent %u bytes over TCP socket\n", ret); +} + +static void ovpn_recv_tcp_data(int socket) +{ + uint8_t buf[1002]; + uint16_t len; + int ret; + + ret =3D recv(socket, buf, sizeof(buf), 0); + + if (ret < 2) { + fprintf(stderr, ">>>> Error while reading TCP data: %d\n", ret); + return; + } + + memcpy(&len, buf, sizeof(len)); + len =3D ntohs(len); + + fprintf(stdout, ">>>> Received %u bytes over TCP socket, header: %u\n", + ret, len); + +/* int i; + * for (i =3D 2; i < ret; i++) { + * fprintf(stdout, "0x%.2x ", buf[i]); + * if (i && !((i - 2) % 16)) + * fprintf(stdout, "\n"); + * } + * fprintf(stdout, "\n"); + */ +} + +static int ovpn_parse_set_peer(struct ovpn_ctx *ovpn, int argc, char *argv= []) +{ + if (argc < 5) { + usage(argv[0]); + return -1; + } + + ovpn->keepalive_interval =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "keepalive interval value out of range\n"); + return -1; + } + + ovpn->keepalive_timeout =3D strtoul(argv[4], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "keepalive interval value out of range\n"); + return -1; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct ovpn_ctx ovpn; + int ret; + + if (argc < 2) { + usage(argv[0]); + return -1; + } + + memset(&ovpn, 0, sizeof(ovpn)); + ovpn.sa_family =3D AF_INET; + ovpn.cli_socket =3D -1; + + if (argc > 2) { + strscpy(ovpn.ifname, argv[2], IFNAMSIZ - 1); + ovpn.ifname[IFNAMSIZ - 1] =3D '\0'; + } + + /* all commands except new_iface expect a valid ifindex */ + if (strcmp(argv[1], "new_iface")) { + /* in this case a ifname MUST be defined */ + if (argc < 3) { + usage(argv[0]); + return -1; + } + + ovpn.ifindex =3D if_nametoindex(ovpn.ifname); + if (!ovpn.ifindex) { + fprintf(stderr, "cannot find interface: %s\n", + strerror(errno)); + return -1; + } + } + + if (!strcmp(argv[1], "new_iface")) { + if (argc > 3) { + if (!strcmp(argv[3], "P2P")) { + ovpn.mode =3D OVPN_MODE_P2P; + } else if (!strcmp(argv[3], "MP")) { + ovpn.mode =3D OVPN_MODE_MP; + } else { + fprintf(stderr, "Cannot parse iface mode: %s\n", + argv[3]); + return -1; + } + ovpn.mode_set =3D true; + } + + ret =3D ovpn_new_iface(&ovpn); + if (ret < 0) { + fprintf(stderr, "Cannot create interface %s: %d\n", + ovpn.ifname, ret); + return -1; + } + } else if (!strcmp(argv[1], "del_iface")) { + ret =3D ovpn_del_iface(&ovpn); + if (ret < 0) { + fprintf(stderr, "Cannot delete interface %s: %d\n", + ovpn.ifname, ret); + return -1; + } + } else if (!strcmp(argv[1], "listen")) { + char peer_id[10], vpnip[100]; + int n; + FILE *fp; + + if (argc < 4) { + usage(argv[0]); + return -1; + } + + ovpn.lport =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE || ovpn.lport > 65535) { + fprintf(stderr, "lport value out of range\n"); + return -1; + } + + if (argc > 4 && !strcmp(argv[4], "ipv6")) + ovpn.sa_family =3D AF_INET6; + + ret =3D ovpn_listen(&ovpn, ovpn.sa_family); + if (ret < 0) { + fprintf(stderr, "cannot listen on TCP socket\n"); + return ret; + } + + fp =3D fopen(argv[4], "r"); + if (!fp) { + fprintf(stderr, "cannot open file: %s\n", argv[4]); + return -1; + } + + while ((n =3D fscanf(fp, "%s %s\n", peer_id, vpnip)) =3D=3D 2) { + struct ovpn_ctx peer_ctx =3D { 0 }; + + peer_ctx.ifindex =3D ovpn.ifindex; + peer_ctx.sa_family =3D ovpn.sa_family; + + peer_ctx.socket =3D ovpn_accept(&ovpn); + if (peer_ctx.socket < 0) { + fprintf(stderr, "cannot accept connection!\n"); + return -1; + } + + /* store the socket of the first peer to test TCP I/O */ + if (ovpn.cli_socket < 0) + ovpn.cli_socket =3D peer_ctx.socket; + + ret =3D ovpn_parse_new_peer(&peer_ctx, peer_id, NULL, + NULL, vpnip); + if (ret < 0) { + fprintf(stderr, "error while parsing line\n"); + return -1; + } + + ret =3D ovpn_new_peer(&peer_ctx, true); + if (ret < 0) { + fprintf(stderr, + "cannot add peer to VPN: %s %s\n", + peer_id, vpnip); + return ret; + } + } + + if (ovpn.cli_socket >=3D 0) + ovpn_recv_tcp_data(ovpn.cli_socket); + } else if (!strcmp(argv[1], "connect")) { + if (argc < 5) { + usage(argv[0]); + return -1; + } + + ovpn.sa_family =3D AF_INET; + + ret =3D ovpn_parse_new_peer(&ovpn, argv[3], argv[4], argv[5], + NULL); + if (ret < 0) { + fprintf(stderr, "Cannot parse remote peer data\n"); + return ret; + } + + ret =3D ovpn_connect(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot connect TCP socket\n"); + return ret; + } + + ret =3D ovpn_new_peer(&ovpn, true); + if (ret < 0) { + fprintf(stderr, "cannot add peer to VPN\n"); + close(ovpn.socket); + return ret; + } + + if (argc > 6) { + ovpn.key_slot =3D OVPN_KEY_SLOT_PRIMARY; + ovpn.key_id =3D 0; + ovpn.cipher =3D OVPN_CIPHER_ALG_AES_GCM; + ovpn.key_dir =3D KEY_DIR_OUT; + + ret =3D ovpn_read_key(argv[6], &ovpn); + if (ret) + return ret; + + ret =3D ovpn_new_key(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot set key\n"); + return ret; + } + + ovpn_send_tcp_data(ovpn.socket); + } + } else if (!strcmp(argv[1], "new_peer")) { + if (argc < 7) { + usage(argv[0]); + return -1; + } + + ovpn.lport =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE || ovpn.lport > 65535) { + fprintf(stderr, "lport value out of range\n"); + return -1; + } + + const char *vpnip =3D (argc > 7) ? argv[7] : NULL; + + ret =3D ovpn_parse_new_peer(&ovpn, argv[4], argv[5], argv[6], + vpnip); + if (ret < 0) + return ret; + + ret =3D ovpn_udp_socket(&ovpn, AF_INET6); //ovpn.sa_family ? + if (ret < 0) + return ret; + + ret =3D ovpn_new_peer(&ovpn, false); + if (ret < 0) { + fprintf(stderr, "cannot add peer to VPN\n"); + return ret; + } + } else if (!strcmp(argv[1], "new_multi_peer")) { + char peer_id[10], raddr[128], rport[10], vpnip[100]; + FILE *fp; + int n; + + if (argc < 5) { + usage(argv[0]); + return -1; + } + + ovpn.lport =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE || ovpn.lport > 65535) { + fprintf(stderr, "lport value out of range\n"); + return -1; + } + + fp =3D fopen(argv[4], "r"); + if (!fp) { + fprintf(stderr, "cannot open file: %s\n", argv[4]); + return -1; + } + + ret =3D ovpn_udp_socket(&ovpn, AF_INET6); + if (ret < 0) + return ret; + + while ((n =3D fscanf(fp, "%s %s %s %s\n", peer_id, raddr, rport, + vpnip)) =3D=3D 4) { + struct ovpn_ctx peer_ctx =3D { 0 }; + + peer_ctx.ifindex =3D ovpn.ifindex; + peer_ctx.socket =3D ovpn.socket; + peer_ctx.sa_family =3D AF_UNSPEC; + + ret =3D ovpn_parse_new_peer(&peer_ctx, peer_id, raddr, + rport, vpnip); + if (ret < 0) { + fprintf(stderr, "error while parsing line\n"); + return -1; + } + + ret =3D ovpn_new_peer(&peer_ctx, false); + if (ret < 0) { + fprintf(stderr, + "cannot add peer to VPN: %s %s %s %s\n", + peer_id, raddr, rport, vpnip); + return ret; + } + } + } else if (!strcmp(argv[1], "set_peer")) { + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + argv++; + argc--; + + ret =3D ovpn_parse_set_peer(&ovpn, argc, argv); + if (ret < 0) + return ret; + + ret =3D ovpn_set_peer(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot set peer to VPN\n"); + return ret; + } + } else if (!strcmp(argv[1], "del_peer")) { + if (argc < 4) { + usage(argv[0]); + return -1; + } + + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + ret =3D ovpn_del_peer(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot delete peer to VPN\n"); + return ret; + } + } else if (!strcmp(argv[1], "get_peer")) { + ovpn.peer_id =3D PEER_ID_UNDEF; + if (argc > 3) + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + + fprintf(stderr, "List of peers connected to: %s\n", + ovpn.ifname); + + ret =3D ovpn_get_peer(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot get peer(s): %d\n", ret); + return ret; + } + } else if (!strcmp(argv[1], "new_key")) { + if (argc < 8) { + usage(argv[0]); + return -1; + } + + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + int slot =3D strtoul(argv[4], NULL, 10); + + if (errno =3D=3D ERANGE || slot < 1 || slot > 2) { + fprintf(stderr, "key slot out of range\n"); + return -1; + } + + switch (slot) { + case 1: + ovpn.key_slot =3D OVPN_KEY_SLOT_PRIMARY; + break; + case 2: + ovpn.key_slot =3D OVPN_KEY_SLOT_SECONDARY; + break; + } + + ovpn.key_id =3D strtoul(argv[5], NULL, 10); + if (errno =3D=3D ERANGE || ovpn.key_id > 2) { + fprintf(stderr, "key ID out of range\n"); + return -1; + } + + ret =3D ovpn_read_cipher(argv[6], &ovpn); + if (ret < 0) + return ret; + + ret =3D ovpn_read_key_direction(argv[7], &ovpn); + if (ret < 0) + return ret; + + ret =3D ovpn_read_key(argv[8], &ovpn); + if (ret) + return ret; + + ret =3D ovpn_new_key(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot set key\n"); + return ret; + } + } else if (!strcmp(argv[1], "del_key")) { + if (argc < 3) { + usage(argv[0]); + return -1; + } + + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + argv++; + argc--; + + ret =3D ovpn_del_key(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot delete key\n"); + return ret; + } + } else if (!strcmp(argv[1], "swap_keys")) { + if (argc < 3) { + usage(argv[0]); + return -1; + } + + ovpn.peer_id =3D strtoul(argv[3], NULL, 10); + if (errno =3D=3D ERANGE) { + fprintf(stderr, "peer ID value out of range\n"); + return -1; + } + + argv++; + argc--; + + ret =3D ovpn_swap_keys(&ovpn); + if (ret < 0) { + fprintf(stderr, "cannot swap keys\n"); + return ret; + } + } else if (!strcmp(argv[1], "listen_mcast")) { + ret =3D ovpn_listen_mcast(); + } else { + usage(argv[0]); + return -1; + } + + return ret; +} diff --git a/tools/testing/selftests/net/ovpn/tcp_peers.txt b/tools/testing= /selftests/net/ovpn/tcp_peers.txt new file mode 100644 index 0000000000000000000000000000000000000000..d753eebe8716ed3588334ad7669= 81e883ed2469a --- /dev/null +++ b/tools/testing/selftests/net/ovpn/tcp_peers.txt @@ -0,0 +1,5 @@ +1 5.5.5.2 +2 5.5.5.3 +3 5.5.5.4 +4 5.5.5.5 +5 5.5.5.6 diff --git a/tools/testing/selftests/net/ovpn/udp_peers.txt b/tools/testing= /selftests/net/ovpn/udp_peers.txt new file mode 100644 index 0000000000000000000000000000000000000000..32f14bd9347a63e58438311b6d8= 80b9fef768aa2 --- /dev/null +++ b/tools/testing/selftests/net/ovpn/udp_peers.txt @@ -0,0 +1,5 @@ +1 10.10.1.2 1 5.5.5.2 +2 10.10.2.2 1 5.5.5.3 +3 10.10.3.2 1 5.5.5.4 +4 10.10.4.2 1 5.5.5.5 +5 10.10.5.2 1 5.5.5.6 --=20 2.45.2