Skip to contentSkip to conversation

Introducing OpenVPN Data Channel Offload

PATCH Superseded Antonio Quartulli 8 participants 90 messages Mar 4, 2024 – Apr 2, 2024

Subsystems (4)

v2 of 26 Superseded Latest is v26 →

lore
Share & tools
lore

Antonio Quartulli

author maintainer
#0 lore raw

v2 cover Introducing OpenVPN Data Channel Offload

Hi all!

After the comments received last month, I reworked the large patch that I have previously sent and I came up with this patchset hoping to make the review process more human and less cumbersome.

Some features are stricly intertwined with each other, therefore I couldn't split everything up to the very last grain of salt, but I did my best to create a reasonable set of features that add up on top of each other.

I don't expect the kernel module to work between intermediate patches, therefore it is important that all patches are applied if you want to see something meaningful happening.

The following is just the introductory text from v1. It's a useful summary of what this new kernel module represents.

As an intereting note, an earlier version of this kernel module is already being used by quite some OpenVPN users out there claiming important improvements in terms of performance. By merging the ovpn kernel module upstream we were hoping to extend cooperation beyond the mere OpenVPN community.

===================================================================

ovpn is essentialy a device driver that allows creating a virtual network interface to handle the OpenVPN data channel. Any traffic entering the interface is encrypted, encapsulated and sent to the appropriate destination.

ovpn requires OpenVPN in userspace
to run along its side in order to be properly configured and maintained during its life cycle.

The ovpn interface can be created/destroyed and then configured via Netlink API.

Specifically OpenVPN in userspace will:
* create the ovpn interface
* establish the connection with one or more peers
* perform TLS handshake and negotiate any protocol parameter
* configure the ovpn interface with peer data (ip/port, keys, etc.)
* handle any subsequent control channel communication

I'd like to point out the control channel is fully handles in userspace. The idea is to keep the ovpn kernel module as simple as possible and let userspace handle all the non-data (non-fast-path) features.

NOTE: some of you may already know ovpn-dco the out-of-tree predecessor of ovpn. However, be aware that the two are not API compatible and therefore OpenVPN 2.6 will not work with this new ovpn module. More adjustments are required.

If you want to test the ovpn kernel module, for the time being you can use the testing tool ovpn-cli available here:
https://github.com/OpenVPN/ovpn-dco/tree/master/tests

The ovpn code can also be built as out-of-tree module and its code is available here https://github.com/OpenVPN/ovpn-dco (currently in the dev branch).

For more technical details please refer to the actual patches.

Any comment, concern or statement will be appreciated! Thanks a lot!!

Best Regards,

Antonio Quartulli
OpenVPN Inc.

======================

Antonio Quartulli (22):
netlink: add NLA_POLICY_MAX_LEN macro
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: implement interface creation/destruction via netlink
ovpn: introduce the ovpn_peer object
ovpn: introduce the ovpn_socket object
ovpn: implement basic TX path (UDP)
ovpn: implement basic RX path (UDP)
ovpn: implement packet processing
ovpn: store tunnel and transport statistics
ovpn: implement TCP transport
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
ovpn: implement keepalive mechanism
ovpn: add support for updating local UDP endpoint
ovpn: add support for peer floating
ovpn: implement peer add/dump/delete via netlink
ovpn: implement key add/del/swap via netlink
ovpn: kill key and notify userspace in case of IV exhaustion
ovpn: notify userspace when a peer is deleted
ovpn: add basic ethtool support

 MAINTAINERS                    |    8 +
 drivers/net/Kconfig            |   13 +
 drivers/net/Makefile           |    1 +
 drivers/net/ovpn/Makefile      |   21 +
 drivers/net/ovpn/bind.c        |   60 ++
 drivers/net/ovpn/bind.h        |   91 +++
 drivers/net/ovpn/crypto.c      |  154 +++++
 drivers/net/ovpn/crypto.h      |  144 +++++
 drivers/net/ovpn/crypto_aead.c |  366 +++++++++++
 drivers/net/ovpn/crypto_aead.h |   27 +
 drivers/net/ovpn/io.c          |  533 ++++++++++++++++
 drivers/net/ovpn/io.h          |   29 +
 drivers/net/ovpn/main.c        |  280 +++++++++
 drivers/net/ovpn/main.h        |   38 ++
 drivers/net/ovpn/netlink.c     | 1045 ++++++++++++++++++++++++++++++++
 drivers/net/ovpn/netlink.h     |   22 +
 drivers/net/ovpn/ovpnstruct.h  |   58 ++
 drivers/net/ovpn/packet.h      |   44 ++
 drivers/net/ovpn/peer.c        |  929 ++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h        |  176 ++++++
 drivers/net/ovpn/pktid.c       |  126 ++++
 drivers/net/ovpn/pktid.h       |   90 +++
 drivers/net/ovpn/proto.h       |  101 +++
 drivers/net/ovpn/skb.h         |   51 ++
 drivers/net/ovpn/socket.c      |  140 +++++
 drivers/net/ovpn/socket.h      |   57 ++
 drivers/net/ovpn/stats.c       |   21 +
 drivers/net/ovpn/stats.h       |   51 ++
 drivers/net/ovpn/tcp.c         |  474 +++++++++++++++
 drivers/net/ovpn/tcp.h         |   41 ++
 drivers/net/ovpn/udp.c         |  355 +++++++++++
 drivers/net/ovpn/udp.h         |   23 +
 include/net/netlink.h          |    1 +
 include/uapi/linux/ovpn.h      |  174 ++++++
 include/uapi/linux/udp.h       |    1 +
 35 files changed, 5745 insertions(+)
 create mode 100644 drivers/net/ovpn/Makefile
 create mode 100644 drivers/net/ovpn/bind.c
 create mode 100644 drivers/net/ovpn/bind.h
 create mode 100644 drivers/net/ovpn/crypto.c
 create mode 100644 drivers/net/ovpn/crypto.h
 create mode 100644 drivers/net/ovpn/crypto_aead.c
 create mode 100644 drivers/net/ovpn/crypto_aead.h
 create mode 100644 drivers/net/ovpn/io.c
 create mode 100644 drivers/net/ovpn/io.h
 create mode 100644 drivers/net/ovpn/main.c
 create mode 100644 drivers/net/ovpn/main.h
 create mode 100644 drivers/net/ovpn/netlink.c
 create mode 100644 drivers/net/ovpn/netlink.h
 create mode 100644 drivers/net/ovpn/ovpnstruct.h
 create mode 100644 drivers/net/ovpn/packet.h
 create mode 100644 drivers/net/ovpn/peer.c
 create mode 100644 drivers/net/ovpn/peer.h
 create mode 100644 drivers/net/ovpn/pktid.c
 create mode 100644 drivers/net/ovpn/pktid.h
 create mode 100644 drivers/net/ovpn/proto.h
 create mode 100644 drivers/net/ovpn/skb.h
 create mode 100644 drivers/net/ovpn/socket.c
 create mode 100644 drivers/net/ovpn/socket.h
 create mode 100644 drivers/net/ovpn/stats.c
 create mode 100644 drivers/net/ovpn/stats.h
 create mode 100644 drivers/net/ovpn/tcp.c
 create mode 100644 drivers/net/ovpn/tcp.h
 create mode 100644 drivers/net/ovpn/udp.c
 create mode 100644 drivers/net/ovpn/udp.h
 create mode 100644 include/uapi/linux/ovpn.h
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 1/22 netlink: add NLA_POLICY_MAX_LEN macro

Similarly to NLA_POLICY_MIN_LEN, NLA_POLICY_MAX_LEN defines a policy with a maximum length value.

---
 include/net/netlink.h | 1 +
 1 file changed, 1 insertion(+)
include/net/netlink.h +1 -0
index c19ff921b661..0b741f62b266 100644
@@ -464,6 +464,7 @@ struct nla_policy {
 	.max = _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)
 
 /**
  * struct nl_info - netlink source information
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v2 2/22 net: introduce OpenVPN Data Channel Offload (ovpn)

In reply to Antonio Quartulli

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.

---
 MAINTAINERS               |   8 +++
 drivers/net/Kconfig       |  13 +++++
 drivers/net/Makefile      |   1 +
 drivers/net/ovpn/Makefile |  11 ++++
 drivers/net/ovpn/io.c     |  23 ++++++++
 drivers/net/ovpn/io.h     |  19 ++++++
 drivers/net/ovpn/main.c   | 118 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/main.h   |  38 ++++++++++++
 include/uapi/linux/udp.h  |   1 +
 9 files changed, 232 insertions(+)
 create mode 100644 drivers/net/ovpn/Makefile
 create mode 100644 drivers/net/ovpn/io.c
 create mode 100644 drivers/net/ovpn/io.h
 create mode 100644 drivers/net/ovpn/main.c
 create mode 100644 drivers/net/ovpn/main.h
MAINTAINERS +8 -0
index 04e5f7c20e30..5ded5e931622 100644
@@ -16529,6 +16529,14 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git
 F:	Documentation/filesystems/overlayfs.rst
 F:	fs/overlayfs/
 
+OPENVPN DATA CHANNEL OFFLOAD
+M:	Antonio Quartulli <[email protected]>
+L:	[email protected] (moderated for non-subscribers)
+L:	[email protected]
+S:	Maintained
+F:	drivers/net/ovpn/
+F:	include/uapi/linux/ovpn.h
+
 P54 WIRELESS DRIVER
 M:	Christian Lamparter <[email protected]>
 L:	[email protected]
drivers/net/Kconfig +13 -0
index 8ca0bc223b30..66ddfd758a40 100644
@@ -115,6 +115,19 @@ config WIREGUARD_DEBUG
 
 	  Say N here unless you know what you're doing.
 
+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
+	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
drivers/net/Makefile +1 -0
index 7cab36f94782..a0b33e7c29ad 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_IPVLAN) += ipvlan/
 obj-$(CONFIG_IPVTAP) += ipvlan/
 obj-$(CONFIG_DUMMY) += dummy.o
 obj-$(CONFIG_WIREGUARD) += wireguard/
+obj-$(CONFIG_OVPN) += ovpn/
 obj-$(CONFIG_EQUALIZER) += eql.o
 obj-$(CONFIG_IFB) += ifb.o
 obj-$(CONFIG_MACSEC) += macsec.o
drivers/net/ovpn/Makefile new file +11 -0
new file mode 100644
index 000000000000..7b8b1d0ff9b4
@@ -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 <[email protected]>
+
+obj-$(CONFIG_OVPN) += ovpn.o
+ovpn-y += main.o
+ovpn-y += io.o
drivers/net/ovpn/io.c new file +23 -0
new file mode 100644
index 000000000000..a1e19402e36d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#include "io.h"
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.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;
+}
drivers/net/ovpn/io.h new file +19 -0
new file mode 100644
index 000000000000..0a076d14f721
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPN_H_
+#define _NET_OVPN_OVPN_H_
+
+#include <linux/netdevice.h>
+
+struct sk_buff;
+
+netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
+
+#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c new file +118 -0

118 lines not on this page · raw message

drivers/net/ovpn/main.h new file +38 -0
new file mode 100644
index 000000000000..cc624116e2f9
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_MAIN_H_
+#define _NET_OVPN_MAIN_H_
+
+#include <linux/ip.h>
+#include <linux/ipv6.h>
+#include <linux/printk.h>
+#include <linux/udp.h>
+
+#ifndef OVPN_VERSION
+#define OVPN_VERSION "3.0.0"
+#endif
+
+struct net_device;
+struct ovpn_struct;
+enum ovpn_mode;
+
+bool ovpn_dev_is_valid(const struct net_device *dev);
+int ovpn_iface_create(const char *name, enum ovpn_mode mode, struct net *net);
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_device);
+
+#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
+#define OVPN_QUEUE_LEN 1024
+
+#endif /* _NET_OVPN_MAIN_H_ */
include/uapi/linux/udp.h +1 -0
index 4828794efcf8..0dd94757127f 100644
@@ -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 */
 
 #endif /* _UAPI_LINUX_UDP_H */
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH net-next v2 02/22] net: introduce OpenVPN Data Channel Offload (ovpn)

Quoted 9 lines
drivers/net/ovpn/io.c new file +3 -0
new file mode 100644
index 000000000000..a1e19402e36d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *

drivers/net/ovpn/io.c line 13

Quoted 10 lines
drivers/net/ovpn/io.c new file +10 -0
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#include "io.h"
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>

It is normal to put local headers last.

Quoted 12 lines
drivers/net/ovpn/io.h new file +6 -0
new file mode 100644
index 000000000000..0a076d14f721
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>

drivers/net/ovpn/io.h line 16

Quoted 10 lines
drivers/net/ovpn/io.h new file +10 -0
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPN_H_
+#define _NET_OVPN_OVPN_H_
+
+#include <linux/netdevice.h>
+
+struct sk_buff;
+

Once you have the headers in the normal order, you probably won't need this.

Quoted 21 lines
drivers/net/ovpn/io.h new file +3 -0
+netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
+
+#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c new file +12 -0
new file mode 100644
index 000000000000..25964eb89aac
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ *		James Yonan <[email protected]>
+ */
+
+#include "main.h"
+#include "io.h"
+

drivers/net/ovpn/main.c line 22

Quoted 10 lines
drivers/net/ovpn/main.c new file +10 -0
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/net.h>
+#include <linux/inetdevice.h>
+#include <linux/netdevice.h>
+#include <linux/version.h>
+
+
+/* Driver info */

Double blank lines are generally not liked. I'm surprised checkpatch did not warn?

drivers/net/ovpn/main.c line 34

Quoted 12 lines
drivers/net/ovpn/main.c new file +12 -0
+#define DRV_NAME	"ovpn"
+#define DRV_VERSION	OVPN_VERSION
+#define DRV_DESCRIPTION	"OpenVPN data channel offload (ovpn)"
+#define DRV_COPYRIGHT	"(C) 2020-2024 OpenVPN, Inc."
+
+/* Net device open */
+static int ovpn_net_open(struct net_device *dev)
+{
+	struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
+
+	if (dev_v4) {
+		/* disable redirects as Linux gets confused by ovpn handling same-LAN routing */

Although Linux in general allows longer lines, netdev has kept with
80. Please wrap.

drivers/net/ovpn/main.c line 36

Quoted 2 lines
drivers/net/ovpn/main.c new file +2 -0
+		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
+		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;

Wireguard has the same. How is Linux getting confused? Maybe we should consider fixing this properly?

drivers/net/ovpn/main.c line 39

Quoted 3 lines
drivers/net/ovpn/main.c new file +3 -0
+#ifndef OVPN_VERSION
+#define OVPN_VERSION "3.0.0"
+#endif

What could sensible define it to some other value?

These version numbers are generally useless. A driver is not standalone. It fits within a kernel. If you get a bug report, what you actually want to know is the kernel version, ideally the git hash.

Andrew

author maintainer
#4 lore raw

Re: [PATCH net-next v2 02/22] net: introduce OpenVPN Data Channel Offload (ovpn)

Hi Andrew,

Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ovpn/io.c new file +7 -0
new file mode 100644
index 000000000000..a1e19402e36d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
Quoted 8 lines

drivers/net/ovpn/io.c line 13

Quoted 6 lines
drivers/net/ovpn/io.c new file +6 -0
+ */
+
+#include "io.h"
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>

It is normal to put local headers last.

Ok, will make this change on all files.

Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/io.h new file +11 -0
new file mode 100644
index 000000000000..0a076d14f721
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPN_H_
+#define _NET_OVPN_OVPN_H_
Quoted 8 lines

drivers/net/ovpn/io.h line 16

Quoted 5 lines
drivers/net/ovpn/io.h new file +5 -0
+
+#include <linux/netdevice.h>
+
+struct sk_buff;
+

Once you have the headers in the normal order, you probably won't need this.

True, but I personally I always try to include headers in any file where they are needed, to avoid implicitly forcing some kind of include ordering or dependency. Isn't it recommended?

Quoted 27 lines
Quoted 27 lines
drivers/net/ovpn/io.h new file +3 -0
+netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
+
+#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c new file +17 -0
new file mode 100644
index 000000000000..25964eb89aac
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ *		James Yonan <[email protected]>
+ */
+
+#include "main.h"
+#include "io.h"
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/net.h>
+#include <linux/inetdevice.h>
Quoted 8 lines

drivers/net/ovpn/main.c line 22

Quoted 5 lines
drivers/net/ovpn/main.c new file +5 -0
+#include <linux/netdevice.h>
+#include <linux/version.h>
+
+
+/* Driver info */

Double blank lines are generally not liked. I'm surprised checkpatch did not warn?

No, it did not complain. I added an extra white line between headers and code, but I can remove it and avoid double blank lines at all.

Quoted 16 lines

drivers/net/ovpn/main.c line 34

Quoted 13 lines
drivers/net/ovpn/main.c new file +12 -0
+#define DRV_NAME	"ovpn"
+#define DRV_VERSION	OVPN_VERSION
+#define DRV_DESCRIPTION	"OpenVPN data channel offload (ovpn)"
+#define DRV_COPYRIGHT	"(C) 2020-2024 OpenVPN, Inc."
+
+/* Net device open */
+static int ovpn_net_open(struct net_device *dev)
+{
+	struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
+
+	if (dev_v4) {
+		/* disable redirects as Linux gets confused by ovpn handling same-LAN routing */

Although Linux in general allows longer lines, netdev has kept with
80. Please wrap.

Oh ok. I thought the line length was relaxed kernel-wide. Will wrap all lines as needed then.

Quoted 13 lines

drivers/net/ovpn/main.c line 36

Quoted 3 lines
drivers/net/ovpn/main.c new file +2 -0
+		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
+		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;

Wireguard has the same. How is Linux getting confused? Maybe we should consider fixing this properly?

drivers/net/ovpn/main.c line 39

Quoted 3 lines
drivers/net/ovpn/main.c new file +3 -0
+#ifndef OVPN_VERSION
+#define OVPN_VERSION "3.0.0"
+#endif

What could sensible define it to some other value?

Quoted 3 lines

These version numbers are generally useless. A driver is not standalone. It fits within a kernel. If you get a bug report, what you actually want to know is the kernel version, ideally the git hash.

True, unless the kernel module was compiled as out-of-tree or manually (back-)ported to a different kernel. In that case I'd need the exact version to know what the reporter was running. Right?

Although, while porting to another kernel ovpn could always reference its original kernel as its own version.

I.e.: ovpn-6.9.0 built for linux-4.4.1

Does it make sense?
How do other drivers deal with this?
For example batman-adv has its own:

#define BATADV_SOURCE_VERSION "2024.1"

It helps when compiling the code out of tree.

Regards,

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#5 lore raw

Re: [PATCH net-next v2 02/22] net: introduce OpenVPN Data Channel Offload (ovpn)

Quoted 54 lines · Antonio Quartulli

Hi Andrew,

Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ovpn/io.c new file +7 -0
new file mode 100644
index 000000000000..a1e19402e36d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
Quoted 8 lines

drivers/net/ovpn/io.c line 13

Quoted 6 lines
drivers/net/ovpn/io.c new file +6 -0
+ */
+
+#include "io.h"
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>

It is normal to put local headers last.

Ok, will make this change on all files.

Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/io.h new file +11 -0
new file mode 100644
index 000000000000..0a076d14f721
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPN_H_
+#define _NET_OVPN_OVPN_H_
Quoted 8 lines

drivers/net/ovpn/io.h line 16

Quoted 5 lines
drivers/net/ovpn/io.h new file +5 -0
+
+#include <linux/netdevice.h>
+
+struct sk_buff;
+

Once you have the headers in the normal order, you probably won't need this.

Quoted 3 lines

True, but I personally I always try to include headers in any file where they are needed, to avoid implicitly forcing some kind of include ordering or dependency. Isn't it recommended?

It is a bit of a balancing act. There is a massive patch series crossing the entire kernel which significantly reduces the kernel build time by optimising includes. It only includes what is needed, and it breaks up some of the big header files. The compiler spends a significant time processing include files. So don't include what you don't need, and try at avoid including the same header multiple times.

Quoted 25 lines
Quoted 4 lines

drivers/net/ovpn/io.h line 17

Quoted 1 line
drivers/net/ovpn/io.h new file +1 -0
+/* Driver info */

Double blank lines are generally not liked. I'm surprised checkpatch did not warn?

No, it did not complain. I added an extra white line between headers and code, but I can remove it and avoid double blank lines at all.

Quoted 16 lines

drivers/net/ovpn/io.h

Quoted 13 lines
drivers/net/ovpn/io.h new file +12 -0
+#define DRV_NAME	"ovpn"
+#define DRV_VERSION	OVPN_VERSION
+#define DRV_DESCRIPTION	"OpenVPN data channel offload (ovpn)"
+#define DRV_COPYRIGHT	"(C) 2020-2024 OpenVPN, Inc."
+
+/* Net device open */
+static int ovpn_net_open(struct net_device *dev)
+{
+	struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
+
+	if (dev_v4) {
+		/* disable redirects as Linux gets confused by ovpn handling same-LAN routing */

Although Linux in general allows longer lines, netdev has kept with
80. Please wrap.

Quoted 2 lines

Oh ok. I thought the line length was relaxed kernel-wide. Will wrap all lines as needed then.

https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Notice the netdev/checkpatch test:

CHECK: Please don't use multiple blank lines WARNING: line length of 82 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns
WARNING: line length of 96 exceeds 80 columns

There are some other test failures you should look at.

Quoted 13 lines
Quoted 13 lines

drivers/net/ovpn/io.h

Quoted 3 lines
drivers/net/ovpn/io.h new file +2 -0
+		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
+		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;

Wireguard has the same. How is Linux getting confused? Maybe we should consider fixing this properly?

drivers/net/ovpn/io.h

Quoted 3 lines
drivers/net/ovpn/io.h new file +3 -0
+#ifndef OVPN_VERSION
+#define OVPN_VERSION "3.0.0"
+#endif

What could sensible define it to some other value?

Quoted 7 lines
Quoted 3 lines

These version numbers are generally useless. A driver is not standalone. It fits within a kernel. If you get a bug report, what you actually want to know is the kernel version, ideally the git hash.

True, unless the kernel module was compiled as out-of-tree or manually (back-)ported to a different kernel. In that case I'd need the exact version to know what the reporter was running. Right?

With my mainline hat on: You don't compile an in tree module out of tree.

Quoted 7 lines

Although, while porting to another kernel ovpn could always reference its original kernel as its own version.

I.e.: ovpn-6.9.0 built for linux-4.4.1

Does it make sense?
How do other drivers deal with this?

$ ethtool -i enp2s0
[sudo] password for andrew: 
driver: r8169
version: 6.6.9-amd64

It reports uname -r. This is what my Debian kernel calls itself. And a hand built kernel should have a git hash. A Redhat kernel probably has something which identifies it as Redhat. So if somebody backports it to a distribution Frankenkernel, you should be able to identify what the kernel is.

We tell driver writes to implement ethtool .get_drvinfo, and leave ethtool_drvinfo.version empty. The ethtool core will then fill it with uname -r. That should identify the kernel the driver is running in.

There is no reason a virtual device should not implement ethtool.

BATMAN is a bit schizophrenic, both in tree and out of tree. I can understand that for something like BATMAN which is quite niche. But my guess would be, OpenVPN is big enough that vendors will do the backport, to their Frankenkernel, you don't need to keep an out of tree version as well as the in tree version.

Andrew

author maintainer
#6 lore raw

Re: [PATCH net-next v2 02/22] net: introduce OpenVPN Data Channel Offload (ovpn)

Quoted 59 lines · Andrew Lunn
Quoted 54 lines · Antonio Quartulli

Hi Andrew,

Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ovpn/io.c new file +7 -0
new file mode 100644
index 000000000000..a1e19402e36d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
Quoted 8 lines

drivers/net/ovpn/io.c line 13

Quoted 6 lines
drivers/net/ovpn/io.c new file +6 -0
+ */
+
+#include "io.h"
+
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>

It is normal to put local headers last.

Ok, will make this change on all files.

Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/io.h new file +11 -0
new file mode 100644
index 000000000000..0a076d14f721
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPN_H_
+#define _NET_OVPN_OVPN_H_
Quoted 8 lines

drivers/net/ovpn/io.h line 16

Quoted 5 lines
drivers/net/ovpn/io.h new file +5 -0
+
+#include <linux/netdevice.h>
+
+struct sk_buff;
+

Once you have the headers in the normal order, you probably won't need this.

Quoted 3 lines

True, but I personally I always try to include headers in any file where they are needed, to avoid implicitly forcing some kind of include ordering or dependency. Isn't it recommended?

Quoted 6 lines

It is a bit of a balancing act. There is a massive patch series crossing the entire kernel which significantly reduces the kernel build time by optimising includes. It only includes what is needed, and it breaks up some of the big header files. The compiler spends a significant time processing include files. So don't include what you don't need, and try at avoid including the same header multiple times.

ACK

Quoted 21 lines
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/net/ovpn/io.h new file +7 -0
+#define DRV_NAME	"ovpn"
+#define DRV_VERSION	OVPN_VERSION
+#define DRV_DESCRIPTION	"OpenVPN data channel offload (ovpn)"
+#define DRV_COPYRIGHT	"(C) 2020-2024 OpenVPN, Inc."
+
+/* Net device open */
+static int ovpn_net_open(struct net_device *dev)
Quoted 8 lines

drivers/net/ovpn/io.h

Quoted 5 lines
drivers/net/ovpn/io.h new file +5 -0
+{
+	struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
+
+	if (dev_v4) {
+		/* disable redirects as Linux gets confused by ovpn handling same-LAN routing */

Although Linux in general allows longer lines, netdev has kept with
80. Please wrap.

Quoted 2 lines

Oh ok. I thought the line length was relaxed kernel-wide. Will wrap all lines as needed then.

https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Quoted 7 lines

Notice the netdev/checkpatch test:

CHECK: Please don't use multiple blank lines WARNING: line length of 82 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns
WARNING: line length of 96 exceeds 80 columns

There are some other test failures you should look at.

Now that I think about it, I did not run checkpatch with --strict, so I must have missed some warnings/messages.

Will double check. thanks.

Quoted 48 lines
Quoted 14 lines
Quoted 14 lines

drivers/net/ovpn/io.h

Quoted 4 lines
drivers/net/ovpn/io.h new file +2 -0
+		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
+		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;

Wireguard has the same. How is Linux getting confused? Maybe we should consider fixing this properly?

drivers/net/ovpn/io.h

Quoted 3 lines
drivers/net/ovpn/io.h new file +3 -0
+#ifndef OVPN_VERSION
+#define OVPN_VERSION "3.0.0"
+#endif

What could sensible define it to some other value?

Quoted 7 lines
Quoted 3 lines

These version numbers are generally useless. A driver is not standalone. It fits within a kernel. If you get a bug report, what you actually want to know is the kernel version, ideally the git hash.

True, unless the kernel module was compiled as out-of-tree or manually (back-)ported to a different kernel. In that case I'd need the exact version to know what the reporter was running. Right?

With my mainline hat on: You don't compile an in tree module out of tree.

Quoted 7 lines

Although, while porting to another kernel ovpn could always reference its original kernel as its own version.

I.e.: ovpn-6.9.0 built for linux-4.4.1

Does it make sense?
How do other drivers deal with this?

$ ethtool -i enp2s0
[sudo] password for andrew:
driver: r8169
version: 6.6.9-amd64

It reports uname -r. This is what my Debian kernel calls itself. And a hand built kernel should have a git hash. A Redhat kernel probably has something which identifies it as Redhat. So if somebody backports it to a distribution Frankenkernel, you should be able to identify what the kernel is.

We tell driver writes to implement ethtool .get_drvinfo, and leave ethtool_drvinfo.version empty. The ethtool core will then fill it with uname -r. That should identify the kernel the driver is running in.

Quoted 7 lines

There is no reason a virtual device should not implement ethtool.

BATMAN is a bit schizophrenic, both in tree and out of tree. I can understand that for something like BATMAN which is quite niche. But my guess would be, OpenVPN is big enough that vendors will do the backport, to their Frankenkernel, you don't need to keep an out of tree version as well as the in tree version.

I think the common usecase with batman-adv is OpenWrt: like batman-adv, also OpenVPN is widely used on small routers/gateways. It is convenient for distros like OpenWRT to be able to compile out-of-tree modules that are more recent than the kernel being shipped with the stable release.

Wifi drivers are also part of this roller-coaster, but they go through the "backports" project[1].

Maybe I should look into hooking in "backports" as well - it may give us what we need without requiring an out-of-tree package.

I guess I'll drop the internal version for now.

Regards,

[1] https://backports.wiki.kernel.org/index.php/Main_Page

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#7 lore raw

Re: [PATCH net-next v2 02/22] net: introduce OpenVPN Data Channel Offload (ovpn)

In reply to Andrew Lunn
Quoted 6 lines · Andrew Lunn

drivers/net/ovpn/main.c line 36

Quoted 2 lines
drivers/net/ovpn/main.c new file +2 -0
+		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
+		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;

Wireguard has the same. How is Linux getting confused? Maybe we should consider fixing this properly?

I wanted to reply to this point individually.

The reason for requiring this setting lies in the OpenVPN server acting as relay point for hosts in the same subnet.

Example: given the a.b.c.0/24 IP network, you will have .2 that in order to talk to .3 must have its traffic relayed by .1 (the server).

When the kernel sees this traffic it will send the ICMP redirects, because it believes that .2 should directly talk to .3 without passing through .1.

It of course makes sense in a normal network with a classic broadcast domain, but this is not the case in a VPN implemented as a start topology.

Does it make sense?

The only way I see to fix this globally is to have an extra flag in the netdevice signaling this peculiarity and thus disabling ICMP redirects automatically.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#8 lore raw

PATCH v2 3/22 ovpn: add basic netlink support

In reply to Antonio Quartulli

This commit introduces basic netlink support with registration/unregistration functionalities and stub pre/post-doit.

More importantly it introduces the UAPI header file that contains the attributes that are inteded to be used by the netlink API implementation.

For convience, packet.h is also added containing some macros about the OpenVPN packet format.

---
 drivers/net/ovpn/Makefile     |   1 +
 drivers/net/ovpn/main.c       |  15 +++
 drivers/net/ovpn/netlink.c    | 229 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/netlink.h    |  18 +++
 drivers/net/ovpn/ovpnstruct.h |  20 +++
 drivers/net/ovpn/packet.h     |  42 +++++++
 include/uapi/linux/ovpn.h     | 174 ++++++++++++++++++++++++++
 7 files changed, 499 insertions(+)
 create mode 100644 drivers/net/ovpn/netlink.c
 create mode 100644 drivers/net/ovpn/netlink.h
 create mode 100644 drivers/net/ovpn/ovpnstruct.h
 create mode 100644 drivers/net/ovpn/packet.h
 create mode 100644 include/uapi/linux/ovpn.h
drivers/net/ovpn/Makefile +1 -0
index 7b8b1d0ff9b4..7e406a69892a 100644
@@ -9,3 +9,4 @@
 obj-$(CONFIG_OVPN) += ovpn.o
 ovpn-y += main.o
 ovpn-y += io.o
+ovpn-y += netlink.o
drivers/net/ovpn/main.c +15 -0
index 25964eb89aac..3769f99cfe6f 100644
@@ -8,8 +8,10 @@
  */
 
 #include "main.h"
+#include "netlink.h"
 #include "io.h"
 
+#include <linux/genetlink.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/types.h>
@@ -17,6 +19,7 @@
 #include <linux/inetdevice.h>
 #include <linux/netdevice.h>
 #include <linux/version.h>
+#include <uapi/linux/ovpn.h>
 
 
 /* Driver info */
@@ -101,12 +104,23 @@ static int __init ovpn_init(void)
 		return err;
 	}
 
+	err = ovpn_nl_register();
+	if (err) {
+		pr_err("ovpn: can't register netlink family: %d\n", err);
+		goto unreg_netdev;
+	}
+
 	return 0;
+
+unreg_netdev:
+	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	return err;
 }
 
 static __exit void ovpn_cleanup(void)
 {
 	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	ovpn_nl_unregister();
 }
 
 module_init(ovpn_init);
@@ -116,3 +130,4 @@ MODULE_DESCRIPTION(DRV_DESCRIPTION);
 MODULE_AUTHOR(DRV_COPYRIGHT);
 MODULE_LICENSE("GPL");
 MODULE_VERSION(DRV_VERSION);
+MODULE_ALIAS_GENL_FAMILY(OVPN_NL_NAME);
drivers/net/ovpn/netlink.c new file +229 -0

229 lines not on this page · raw message

drivers/net/ovpn/netlink.h new file +18 -0
new file mode 100644
index 000000000000..eb7f234842ef
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_NETLINK_H_
+#define _NET_OVPN_NETLINK_H_
+
+struct ovpn_struct;
+
+int ovpn_nl_init(struct ovpn_struct *ovpn);
+int ovpn_nl_register(void);
+void ovpn_nl_unregister(void);
+
+#endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/ovpnstruct.h new file +20 -0
new file mode 100644
index 000000000000..932eb90953e0
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPNSTRUCT_H_
+#define _NET_OVPN_OVPNSTRUCT_H_
+
+#include <linux/netdevice.h>
+
+/* Our state per ovpn interface */
+struct ovpn_struct {
+	struct net_device *dev;
+};
+
+#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/packet.h new file +42 -0

42 lines not on this page · raw message

include/uapi/linux/ovpn.h new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 5 lines · Antonio Quartulli

This commit introduces basic netlink support with registration/unregistration functionalities and stub pre/post-doit.

More importantly it introduces the UAPI header file that contains the attributes that are inteded to be used by the netlink API

intended.

drivers/net/ovpn/netlink.c line 38

Quoted 8 lines

implementation.

For convience, packet.h is also added containing some macros about the OpenVPN packet format.

drivers/net/ovpn/netlink.c new file +3 -0
+/** KEYDIR policy. Can be used for configuring an encryption and a decryption key */
+static const struct nla_policy ovpn_nl_policy_keydir[NUM_OVPN_A_KEYDIR] = {
+	[OVPN_A_KEYDIR_CIPHER_KEY] = NLA_POLICY_MAX_LEN(U8_MAX),

I don't know netlink that well. Is this saying keys are limited to 256 bytes? How future proof is that? I'm not a crypto person, but symmetric algorithms, e.g. AES, seem to have reasonably short keys, 32 bytes, so this seems O.K, to me.

drivers/net/ovpn/netlink.c line 46

Quoted 8 lines
drivers/net/ovpn/netlink.c new file +8 -0
+	[OVPN_A_KEYDIR_NONCE_TAIL] = NLA_POLICY_EXACT_LEN(NONCE_TAIL_SIZE),
+};
+
+/** KEYCONF policy */
+static const struct nla_policy ovpn_nl_policy_keyconf[NUM_OVPN_A_KEYCONF] = {
+	[OVPN_A_KEYCONF_SLOT] = NLA_POLICY_RANGE(NLA_U8, __OVPN_KEY_SLOT_FIRST,
+						 NUM_OVPN_KEY_SLOT - 1),
+	[OVPN_A_KEYCONF_KEY_ID] = { .type = NLA_U8 },

Is that 256 keys globally, or just associated to one session?

drivers/net/ovpn/netlink.c line 71

Quoted 10 lines
drivers/net/ovpn/netlink.c new file +9 -0
+	[OVPN_A_KEYCONF_CIPHER_ALG] = { .type = NLA_U16 },
+	[OVPN_A_KEYCONF_ENCRYPT_DIR] = NLA_POLICY_NESTED(ovpn_nl_policy_keydir),
+	[OVPN_A_KEYCONF_DECRYPT_DIR] = NLA_POLICY_NESTED(ovpn_nl_policy_keydir),
+};
+
 
+/** Generic message container policy */
+static const struct nla_policy ovpn_nl_policy[NUM_OVPN_A] = {
+	[OVPN_A_IFINDEX] = { .type = NLA_U32 },
+	[OVPN_A_IFNAME] = NLA_POLICY_MAX_LEN(IFNAMSIZ),

Generally, ifnames are not passed around, only ifindex. An interface can have multiple names:

12: enlr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq master br0 state DOWN group default qlen 1000

    link/ether 3c:ec:ef:7e:0a:90 brd ff:ff:ff:ff:ff:ff
    altname enp183s0f2
    altname eno7

It is better to let userspace figure out the name from the index, since the name is mostly a user space concept.

Quoted 4 lines
drivers/net/ovpn/netlink.c new file +4 -0
+	[OVPN_A_MODE] = NLA_POLICY_RANGE(NLA_U8, __OVPN_MODE_FIRST,
+					 NUM_OVPN_MODE - 1),
+	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_nl_policy_peer),
+};

drivers/net/ovpn/netlink.c line 122

Quoted 10 lines
drivers/net/ovpn/netlink.c new file +9 -0
 
+static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
+			 struct genl_info *info)
+{
+	struct net *net = genl_info_net(info);
+	struct net_device *dev;
+
+	/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
+	 * just expects an IFNAME, while all the others expect an IFINDEX
+	 */

Could you explain that some more. In general, the name should not matter to the kernel, udev/systemd might rename it soon after creation etc. If it gets moved into a network namespace it might need renaming etc.

include/uapi/linux/ovpn.h line 129

Quoted 5 lines
include/uapi/linux/ovpn.h new file +5 -0
+enum ovpn_nl_peer_attrs {
+	OVPN_A_PEER_UNSPEC = 0,
+	OVPN_A_PEER_ID,
+	OVPN_A_PEER_RX_STATS,
+	OVPN_A_PEER_TX_STATS,

Probably answered later in the patch series: What sort of statistics do you expect here. Don't overlap any of the normal network statistics with this here, please use the existing kAPIs for those. Anything you return here need to be very specific to ovpn.

include/uapi/linux/ovpn.h line 147

Quoted 8 lines
include/uapi/linux/ovpn.h new file +8 -0
+	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,

How do these differ to standard network statistics? e.g. what is in /sys/class/net/*/statistics/ ?

Andrew

author maintainer
#10 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 14 lines · Andrew Lunn
Quoted 5 lines · Antonio Quartulli

This commit introduces basic netlink support with registration/unregistration functionalities and stub pre/post-doit.

More importantly it introduces the UAPI header file that contains the attributes that are inteded to be used by the netlink API

intended.

Quoted 5 lines

implementation.

For convience, packet.h is also added containing some macros about the OpenVPN packet format.

Quoted 8 lines

drivers/net/ovpn/netlink.c line 38

Quoted 3 lines
drivers/net/ovpn/netlink.c new file +3 -0
+/** KEYDIR policy. Can be used for configuring an encryption and a decryption key */
+static const struct nla_policy ovpn_nl_policy_keydir[NUM_OVPN_A_KEYDIR] = {
+	[OVPN_A_KEYDIR_CIPHER_KEY] = NLA_POLICY_MAX_LEN(U8_MAX),

I don't know netlink that well. Is this saying keys are limited to 256 bytes? How future proof is that? I'm not a crypto person, but symmetric algorithms, e.g. AES, seem to have reasonably short keys, 32 bytes, so this seems O.K, to me.

256 bytes should be reasonably large. I don't see anything beyond this size appearing anytime soon or at all.

Quoted 11 lines

drivers/net/ovpn/netlink.c line 46

Quoted 9 lines
drivers/net/ovpn/netlink.c new file +8 -0
+	[OVPN_A_KEYDIR_NONCE_TAIL] = NLA_POLICY_EXACT_LEN(NONCE_TAIL_SIZE),
+};
+
+/** KEYCONF policy */
+static const struct nla_policy ovpn_nl_policy_keyconf[NUM_OVPN_A_KEYCONF] = {
+	[OVPN_A_KEYCONF_SLOT] = NLA_POLICY_RANGE(NLA_U8, __OVPN_KEY_SLOT_FIRST,
+						 NUM_OVPN_KEY_SLOT - 1),
+	[OVPN_A_KEYCONF_KEY_ID] = { .type = NLA_U8 },

Is that 256 keys globally, or just associated to one session?

This is specific to one peer, however, the OpenVPN protocol uses IDs up 7, therefore U8 is just the smallest unit I could use to fit those few values.

Quoted 15 lines

drivers/net/ovpn/netlink.c line 71

Quoted 11 lines
drivers/net/ovpn/netlink.c new file +9 -0
+	[OVPN_A_KEYCONF_CIPHER_ALG] = { .type = NLA_U16 },
+	[OVPN_A_KEYCONF_ENCRYPT_DIR] = NLA_POLICY_NESTED(ovpn_nl_policy_keydir),
+	[OVPN_A_KEYCONF_DECRYPT_DIR] = NLA_POLICY_NESTED(ovpn_nl_policy_keydir),
+};
+
 
+/** Generic message container policy */
+static const struct nla_policy ovpn_nl_policy[NUM_OVPN_A] = {
+	[OVPN_A_IFINDEX] = { .type = NLA_U32 },
+	[OVPN_A_IFNAME] = NLA_POLICY_MAX_LEN(IFNAMSIZ),

Generally, ifnames are not passed around, only ifindex. An interface can have multiple names:

Quoted 7 lines

12: enlr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq master br0 state DOWN group default qlen 1000

     link/ether 3c:ec:ef:7e:0a:90 brd ff:ff:ff:ff:ff:ff
     altname enp183s0f2
     altname eno7

It is better to let userspace figure out the name from the index, since the name is mostly a user space concept.

This is strictly related to your next question. Please see my answer below.

Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c new file +4 -0
+	[OVPN_A_MODE] = NLA_POLICY_RANGE(NLA_U8, __OVPN_MODE_FIRST,
+					 NUM_OVPN_MODE - 1),
+	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_nl_policy_peer),
+};
Quoted 14 lines

drivers/net/ovpn/netlink.c line 122

Quoted 9 lines
drivers/net/ovpn/netlink.c new file +9 -0
+static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
+			 struct genl_info *info)
+{
+	struct net *net = genl_info_net(info);
+	struct net_device *dev;
+
+	/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
+	 * just expects an IFNAME, while all the others expect an IFINDEX
+	 */

Could you explain that some more. In general, the name should not matter to the kernel, udev/systemd might rename it soon after creation etc. If it gets moved into a network namespace it might need renaming etc.

In a previous discussion it was agreed that we should create ovpn interfaces via GENL and not via RTNL.

For this reason ovpn needs userspace to send the name to give the interface upon creation. This name is just passed to the networking stack upon creation/registration, but it is not stored anywhere else.

Subsequent netlink calls are then all performed by passing an ifindex.

Hence, OVPN_CMD_NEW_IFACE is the only GENL command that required the IFNAME to be specified.

Does it make sense?

Quoted 11 lines

include/uapi/linux/ovpn.h line 129

Quoted 6 lines
include/uapi/linux/ovpn.h new file +5 -0
+enum ovpn_nl_peer_attrs {
+	OVPN_A_PEER_UNSPEC = 0,
+	OVPN_A_PEER_ID,
+	OVPN_A_PEER_RX_STATS,
+	OVPN_A_PEER_TX_STATS,

Probably answered later in the patch series: What sort of statistics do you expect here. Don't overlap any of the normal network statistics with this here, please use the existing kAPIs for those. Anything you return here need to be very specific to ovpn.

Actually you found a leftover from an old approach. OVPN_A_PEER_RX_STATS and OVPN_A_PEER_TX_STATS shall be removed.

The actual stats we store are those below:

Quoted 12 lines

include/uapi/linux/ovpn.h line 147

Quoted 9 lines
include/uapi/linux/ovpn.h new file +8 -0
+	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,

How do these differ to standard network statistics? e.g. what is in /sys/class/net/*/statistics/ ?

The first difference is that these stats are per-peer and not per-device. Behind each device there might be multiple peers connected.

This way ovpn is able to tell how much data was sent/received by every single connected peer.

LINK and VPN store different values.
LINK stats are recorded at the transport layer (before decapsulation or after encapsulation), while VPN stats are recorded at the tunnel layer (after decapsulation or before encapsulation).

I didn't see how to convey the same information using the standard statistics.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#11 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 35 lines
Quoted 7 lines

12: enlr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq master br0 state DOWN group default qlen 1000

     link/ether 3c:ec:ef:7e:0a:90 brd ff:ff:ff:ff:ff:ff
     altname enp183s0f2
     altname eno7

It is better to let userspace figure out the name from the index, since the name is mostly a user space concept.

This is strictly related to your next question.
Please see my answer below.

Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c new file +4 -0
+	[OVPN_A_MODE] = NLA_POLICY_RANGE(NLA_U8, __OVPN_MODE_FIRST,
+					 NUM_OVPN_MODE - 1),
+	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_nl_policy_peer),
+};
Quoted 14 lines

drivers/net/ovpn/netlink.c line 122

Quoted 9 lines
drivers/net/ovpn/netlink.c new file +9 -0
+static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
+			 struct genl_info *info)
+{
+	struct net *net = genl_info_net(info);
+	struct net_device *dev;
+
+	/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
+	 * just expects an IFNAME, while all the others expect an IFINDEX
+	 */

Could you explain that some more. In general, the name should not matter to the kernel, udev/systemd might rename it soon after creation etc. If it gets moved into a network namespace it might need renaming etc.

In a previous discussion it was agreed that we should create ovpn interfaces via GENL and not via RTNL.

Quoted 8 lines

For this reason ovpn needs userspace to send the name to give the interface upon creation. This name is just passed to the networking stack upon creation/registration, but it is not stored anywhere else.

Subsequent netlink calls are then all performed by passing an ifindex.

Hence, OVPN_CMD_NEW_IFACE is the only GENL command that required the IFNAME to be specified.

I don't really see why GENL vs RTNL makes a difference. The reply to the request can contain the ifindex of the newly created interface. If you set the name to "ovpn%d" before calling register_netdevice() the kernel will find the next free unique ovpn interface name, race free. So you could have multiple openvpn daemon running, and not have to worry about races when creating interfaces.

Jakub has been raising questions about this recently for another patchset. He might comment on this.

Quoted 18 lines
Quoted 11 lines

include/uapi/linux/ovpn.h line 147

Quoted 8 lines
include/uapi/linux/ovpn.h new file +8 -0
+	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,

How do these differ to standard network statistics? e.g. what is in /sys/class/net/*/statistics/ ?

The first difference is that these stats are per-peer and not per-device. Behind each device there might be multiple peers connected.

This way ovpn is able to tell how much data was sent/received by every single connected peer.

Quoted 7 lines

LINK and VPN store different values.
LINK stats are recorded at the transport layer (before decapsulation or after encapsulation), while VPN stats are recorded at the tunnel layer (after decapsulation or before encapsulation).

I didn't see how to convey the same information using the standard statistics.

Right, so this in general makes sense. The only question i have now is, should you be using rtnl_link_stats64. That is the standard structure for interface statistics.

Again, Jakub likes to comment about statistics...

And in general, maybe add more comments. This is the UAPI, it needs to be clear and unambiguous. Documentation/networking/ethtool-netlink.rst.

Andrew

maintainer
#12 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 34 lines · On Tue, 5 Mar 2024 17:23:25 +0100 Andrew Lunn wrote:
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/ovpn/netlink.c new file +6 -0
+static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
+			 struct genl_info *info)
+{
+	struct net *net = genl_info_net(info);
+	struct net_device *dev;
+
Quoted 8 lines

drivers/net/ovpn/netlink.c line 122

Quoted 3 lines
drivers/net/ovpn/netlink.c new file +3 -0
+	/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
+	 * just expects an IFNAME, while all the others expect an IFINDEX
+	 */  

Could you explain that some more. In general, the name should not matter to the kernel, udev/systemd might rename it soon after creation etc. If it gets moved into a network namespace it might need renaming etc.

In a previous discussion it was agreed that we should create ovpn interfaces via GENL and not via RTNL.

Quoted 8 lines

For this reason ovpn needs userspace to send the name to give the interface upon creation. This name is just passed to the networking stack upon creation/registration, but it is not stored anywhere else.

Subsequent netlink calls are then all performed by passing an ifindex.

Hence, OVPN_CMD_NEW_IFACE is the only GENL command that required the IFNAME to be specified.

I don't really see why GENL vs RTNL makes a difference. The reply to the request can contain the ifindex of the newly created interface. If you set the name to "ovpn%d" before calling register_netdevice() the kernel will find the next free unique ovpn interface name, race free. So you could have multiple openvpn daemon running, and not have to worry about races when creating interfaces.

Quoted 2 lines

Jakub has been raising questions about this recently for another patchset. He might comment on this.

FWIW using ifindex for most ops sounds like the right way to go. Passing the name to create sounds fine, but as Andrew said, we should default to "ovpn%d" instead of forcing the user to specify the name (and you can echo back the allocated name in the reply to OVPN_CMD_NEW_IFACE).

Somewhat related - if you require an attr - GENL_REQ_ATTR_CHECK(), it does the extact setting for you.

Quoted 26 lines
Quoted 18 lines
Quoted 11 lines

include/uapi/linux/ovpn.h line 147

Quoted 8 lines
include/uapi/linux/ovpn.h new file +8 -0
+	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,  

How do these differ to standard network statistics? e.g. what is in /sys/class/net/*/statistics/ ?

The first difference is that these stats are per-peer and not per-device. Behind each device there might be multiple peers connected.

This way ovpn is able to tell how much data was sent/received by every single connected peer.

Quoted 7 lines

LINK and VPN store different values.
LINK stats are recorded at the transport layer (before decapsulation or after encapsulation), while VPN stats are recorded at the tunnel layer (after decapsulation or before encapsulation).

I didn't see how to convey the same information using the standard statistics.

Quoted 8 lines

Right, so this in general makes sense. The only question i have now is, should you be using rtnl_link_stats64. That is the standard structure for interface statistics.

Again, Jakub likes to comment about statistics...

And in general, maybe add more comments. This is the UAPI, it needs to be clear and unambiguous. Documentation/networking/ethtool-netlink.rst.

Or put enough docs in the YAML spec as those comments get rendered in the uAPI header and in HTML docs :)

author maintainer
#13 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 35 lines · Jakub Kicinski
Quoted 34 lines · On Tue, 5 Mar 2024 17:23:25 +0100 Andrew Lunn wrote:
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/ovpn/netlink.c new file +6 -0
+static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
+			 struct genl_info *info)
+{
+	struct net *net = genl_info_net(info);
+	struct net_device *dev;
+
Quoted 8 lines

drivers/net/ovpn/netlink.c line 122

Quoted 3 lines
drivers/net/ovpn/netlink.c new file +3 -0
+	/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
+	 * just expects an IFNAME, while all the others expect an IFINDEX
+	 */

Could you explain that some more. In general, the name should not matter to the kernel, udev/systemd might rename it soon after creation etc. If it gets moved into a network namespace it might need renaming etc.

In a previous discussion it was agreed that we should create ovpn interfaces via GENL and not via RTNL.

Quoted 8 lines

For this reason ovpn needs userspace to send the name to give the interface upon creation. This name is just passed to the networking stack upon creation/registration, but it is not stored anywhere else.

Subsequent netlink calls are then all performed by passing an ifindex.

Hence, OVPN_CMD_NEW_IFACE is the only GENL command that required the IFNAME to be specified.

I don't really see why GENL vs RTNL makes a difference. The reply to the request can contain the ifindex of the newly created interface. If you set the name to "ovpn%d" before calling register_netdevice() the kernel will find the next free unique ovpn interface name, race free. So you could have multiple openvpn daemon running, and not have to worry about races when creating interfaces.

Quoted 9 lines
Quoted 2 lines

Jakub has been raising questions about this recently for another patchset. He might comment on this.

FWIW using ifindex for most ops sounds like the right way to go. Passing the name to create sounds fine, but as Andrew said, we should default to "ovpn%d" instead of forcing the user to specify the name (and you can echo back the allocated name in the reply to OVPN_CMD_NEW_IFACE).

Ok, it definitely makes sense and actually makes userspace code simpler.

Quoted 2 lines

Somewhat related - if you require an attr - GENL_REQ_ATTR_CHECK(), it does the extact setting for you.

ok, will check it out!

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 12 lines

include/uapi/linux/ovpn.h line 147

Quoted 9 lines
include/uapi/linux/ovpn.h new file +8 -0
+	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,

How do these differ to standard network statistics? e.g. what is in /sys/class/net/*/statistics/ ?

The first difference is that these stats are per-peer and not per-device. Behind each device there might be multiple peers connected.

This way ovpn is able to tell how much data was sent/received by every single connected peer.

LINK and VPN store different values.
LINK stats are recorded at the transport layer (before decapsulation or after encapsulation), while VPN stats are recorded at the tunnel layer (after decapsulation or before encapsulation).

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

I didn't see how to convey the same information using the standard statistics.

Right, so this in general makes sense. The only question i have now is, should you be using rtnl_link_stats64. That is the standard structure for interface statistics.

@Andrew: do you see how I could return/send this object per-peer to userspace?
I think the whole interface stats logic is based on the one-stats-per-device concept. Hence, I thought it was meaningful to just send my own stats via netlink.

Quoted 8 lines
Quoted 5 lines

Again, Jakub likes to comment about statistics...

And in general, maybe add more comments. This is the UAPI, it needs to be clear and unambiguous. Documentation/networking/ethtool-netlink.rst.

Or put enough docs in the YAML spec as those comments get rendered in the uAPI header and in HTML docs :)

Or I'll do both. In any case, I will add more documentation!

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#14 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Right, so this in general makes sense. The only question i have now is, should you be using rtnl_link_stats64. That is the standard structure for interface statistics.

@Andrew: do you see how I could return/send this object per-peer to userspace?
I think the whole interface stats logic is based on the one-stats-per-device concept. Hence, I thought it was meaningful to just send my own stats via netlink.

Ah, interesting. I never looked at the details for rtnl_link_stats64. It is buried in the middle of ifinfo. So not very reusable :-(

Idea #2:

A peer is not that different to an interface queue. Jakub recently posted some code for that:

https://lwn.net/ml/netdev/[email protected]/

Maybe there are ideas you can borrow from there.

But lets look at this from another direction. What are the use cases for these statistics? Does the userspace daemon need them, e.g. to detect a peer which is idle and should be disconnected? Are they exported via an SNMP MIB?

Andrew

author maintainer
#15 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 19 lines · Andrew Lunn
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Right, so this in general makes sense. The only question i have now is, should you be using rtnl_link_stats64. That is the standard structure for interface statistics.

@Andrew: do you see how I could return/send this object per-peer to userspace?
I think the whole interface stats logic is based on the one-stats-per-device concept. Hence, I thought it was meaningful to just send my own stats via netlink.

Ah, interesting. I never looked at the details for rtnl_link_stats64. It is buried in the middle of ifinfo. So not very reusable :-(

Idea #2:

A peer is not that different to an interface queue. Jakub recently posted some code for that:

Quoted 8 lines

https://lwn.net/ml/netdev/[email protected]/

Maybe there are ideas you can borrow from there.

But lets look at this from another direction. What are the use cases for these statistics? Does the userspace daemon need them, e.g. to detect a peer which is idle and should be disconnected? Are they exported via an SNMP MIB?

Yes, they are used by userspace. Main usecases are:
* inactivity timeout (if configured, when less than N bytes are transferred in a given time the client will be disconnected)
* stats reporting during connection (during the life of a peer, stats can be periodically pulled by the user for reporting purposes)
* stats reporting at disconnection (stats are pulled upon disconnection in order to report the total traffic related to a peer)

These stats can be reported via a status file or via the "management interface" (an interactive interface that can be used to issue live commands to a running openvpn daemon).

How this data is actually used is up to the user.

Regards,

Quoted 3 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#16 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

In reply to Antonio Quartulli
kernel test robot noticed the following build warnings:

Hi Antonio,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Antonio-Quartulli/netlink-add-NLA_POLICY_MAX_LEN-macro/20240304-232835
base:   net-next/main
patch link:    https://lore.kernel.org/r/20240304150914.11444-4-antonio%40openvpn.net
patch subject: [PATCH net-next v2 03/22] ovpn: add basic netlink support
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20240305/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240305/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/net/ovpn/netlink.c:82: warning: Function parameter or struct member 'net' not described in 'ovpn_get_dev_from_attrs'
>> drivers/net/ovpn/netlink.c:82: warning: Function parameter or struct member 'attrs' not described in 'ovpn_get_dev_from_attrs'
>> drivers/net/ovpn/netlink.c:181: warning: Function parameter or struct member 'nb' not described in 'ovpn_nl_notify'
>> drivers/net/ovpn/netlink.c:181: warning: Function parameter or struct member 'state' not described in 'ovpn_nl_notify'
>> drivers/net/ovpn/netlink.c:181: warning: Function parameter or struct member '_notify' not described in 'ovpn_nl_notify'
>> drivers/net/ovpn/netlink.c:193: warning: Function parameter or struct member 'ovpn' not described in 'ovpn_nl_init'

vim +82 drivers/net/ovpn/netlink.c

76
77 /**
78 * ovpn_get_dev_from_attrs() - retrieve the netdevice a netlink message is targeting
79 */
80 static struct net_device *
81 ovpn_get_dev_from_attrs(struct net *net, struct nlattr **attrs)

  > 82	{
    83		struct net_device *dev;
    84		int ifindex;
    85	
    86		if (!attrs[OVPN_A_IFINDEX])
    87			return ERR_PTR(-EINVAL);
    88	
    89		ifindex = nla_get_u32(attrs[OVPN_A_IFINDEX]);
    90	
    91		dev = dev_get_by_index(net, ifindex);
    92		if (!dev)
    93			return ERR_PTR(-ENODEV);
    94	
    95		if (!ovpn_dev_is_valid(dev))
    96			goto err_put_dev;
    97	
    98		return dev;
    99	
   100	err_put_dev:
   101		dev_put(dev);
   102	
   103		return ERR_PTR(-EINVAL);
   104	}
   105	
   106	/**
   107	 * ovpn_pre_doit() - Prepare ovpn genl doit request
   108	 * @ops: requested netlink operation
   109	 * @skb: Netlink message with request data
   110	 * @info: receiver information
   111	 *
   112	 * Return: 0 on success or negative error number in case of failure
   113	 */
   114	static int ovpn_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
   115				 struct genl_info *info)
   116	{
   117		struct net *net = genl_info_net(info);
   118		struct net_device *dev;
   119	
   120		/* the OVPN_CMD_NEW_IFACE command is different from the rest as it
   121		 * just expects an IFNAME, while all the others expect an IFINDEX
   122		 */
   123		if (info->genlhdr->cmd == OVPN_CMD_NEW_IFACE) {
   124			if (!info->attrs[OVPN_A_IFNAME]) {
   125				GENL_SET_ERR_MSG(info, "no interface name specified");
   126				return -EINVAL;
   127			}
   128			return 0;
   129		}
   130	
   131		dev = ovpn_get_dev_from_attrs(net, info->attrs);
   132		if (IS_ERR(dev))
   133			return PTR_ERR(dev);
   134	
   135		info->user_ptr[0] = netdev_priv(dev);
   136	
   137		return 0;
   138	}
   139	
   140	/**
   141	 * ovpn_post_doit() - complete ovpn genl doit request
   142	 * @ops: requested netlink operation
   143	 * @skb: Netlink message with request data
   144	 * @info: receiver information
   145	 */
   146	static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
   147				   struct genl_info *info)
   148	{
   149		struct ovpn_struct *ovpn;
   150	
   151		ovpn = info->user_ptr[0];
   152		/* in case of OVPN_CMD_NEW_IFACE, there is no pre-stored device */
   153		if (ovpn)
   154			dev_put(ovpn->dev);
   155	}
   156	
   157	static const struct genl_small_ops ovpn_nl_ops[] = {
   158	};
   159	
   160	static struct genl_family ovpn_nl_family __ro_after_init = {
   161		.hdrsize = 0,
   162		.name = OVPN_NL_NAME,
   163		.version = 1,
   164		.maxattr = NUM_OVPN_A + 1,
   165		.policy = ovpn_nl_policy,
   166		.netnsok = true,
   167		.pre_doit = ovpn_pre_doit,
   168		.post_doit = ovpn_post_doit,
   169		.module = THIS_MODULE,
   170		.small_ops = ovpn_nl_ops,
   171		.n_small_ops = ARRAY_SIZE(ovpn_nl_ops),
   172		.mcgrps = ovpn_nl_mcgrps,
   173		.n_mcgrps = ARRAY_SIZE(ovpn_nl_mcgrps),
   174	};
   175	
   176	/**
   177	 * ovpn_nl_notify() - react to openvpn userspace process exit
   178	 */
   179	static int ovpn_nl_notify(struct notifier_block *nb, unsigned long state,
   180				  void *_notify)
 > 181	{
   182		return NOTIFY_DONE;
   183	}
   184	
   185	static struct notifier_block ovpn_nl_notifier = {
   186		.notifier_call = ovpn_nl_notify,
   187	};
   188	
   189	/**
   190	 * ovpn_nl_init() - perform any ovpn specific netlink initialization
   191	 */
   192	int ovpn_nl_init(struct ovpn_struct *ovpn)
 > 193	{

194 return 0;
195 }
196

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#17 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

In reply to Antonio Quartulli
Quoted 15 lines · Antonio Quartulli
drivers/net/ovpn/main.c +6 -0
index 25964eb89aac..3769f99cfe6f 100644
@@ -101,12 +104,23 @@ static int __init ovpn_init(void)
 		return err;
 	}
 
+	err = ovpn_nl_register();
+	if (err) {
+		pr_err("ovpn: can't register netlink family: %d\n", err);
+		goto unreg_netdev;
+	}
+
 	return 0;

drivers/net/ovpn/main.c static int __init ovpn_init(void) · line 123

Quoted 10 lines
drivers/net/ovpn/main.c +5 -0
+
+unreg_netdev:
+	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	return err;
 }
 
 static __exit void ovpn_cleanup(void)
 {
 	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	ovpn_nl_unregister();

Any good reason for not using reverse order from ovpn_init() here?

drivers/net/ovpn/main.c static int __init ovpn_init(void) · line 124

Quoted 1 line
drivers/net/ovpn/main.c +0 -0
 }

/Esben

author maintainer
#18 lore raw

Re: [PATCH net-next v2 03/22] ovpn: add basic netlink support

Quoted 23 lines · Esben Haabendal
Quoted 21 lines · Antonio Quartulli
drivers/net/ovpn/main.c +10 -0
index 25964eb89aac..3769f99cfe6f 100644
@@ -101,12 +104,23 @@ static int __init ovpn_init(void)
  		return err;
  	}
 
+	err = ovpn_nl_register();
+	if (err) {
+		pr_err("ovpn: can't register netlink family: %d\n", err);
+		goto unreg_netdev;
+	}
+
  	return 0;
+
+unreg_netdev:
+	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	return err;
  }
 
Quoted 6 lines

drivers/net/ovpn/main.c static int __init ovpn_init(void) · line 123

Quoted 4 lines
drivers/net/ovpn/main.c +1 -0
  static __exit void ovpn_cleanup(void)
  {
  	unregister_netdevice_notifier(&ovpn_netdev_notifier);
+	ovpn_nl_unregister();

Any good reason for not using reverse order from ovpn_init() here?

not really.
Actually I also prefer uninit in reverse order compared to init.

Will fix this, thanks!

Quoted 4 lines

drivers/net/ovpn/main.c static int __init ovpn_init(void) · line 124

Quoted 2 lines
drivers/net/ovpn/main.c +0 -0
  }

/Esben

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#19 lore raw

PATCH v2 4/22 ovpn: add basic interface creation/destruction/management routines

In reply to Antonio Quartulli
---
 drivers/net/ovpn/io.c         |  26 +++++++++
 drivers/net/ovpn/io.h         |   1 +
 drivers/net/ovpn/main.c       | 107 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/ovpnstruct.h |  16 +++++
 4 files changed, 150 insertions(+)
drivers/net/ovpn/io.c +26 -0
index a1e19402e36d..b283449ba479 100644
@@ -8,11 +8,37 @@
  */
 
 #include "io.h"
+#include "ovpnstruct.h"
+#include "netlink.h"
 
 #include <linux/netdevice.h>
 #include <linux/skbuff.h>
 
 
+int ovpn_struct_init(struct net_device *dev)
+{
+	struct ovpn_struct *ovpn = netdev_priv(dev);
+	int err;
+
+	memset(ovpn, 0, sizeof(*ovpn));
+
+	ovpn->dev = dev;
+
+	err = ovpn_nl_init(ovpn);
+	if (err < 0)
+		return err;
+
+	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
+	if (!dev->tstats)
+		return -ENOMEM;
+
+	err = security_tun_dev_alloc_security(&ovpn->security);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
 /* Send user data to the network
  */
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
drivers/net/ovpn/io.h +1 -0
index 0a076d14f721..e7728718c8a9 100644
@@ -14,6 +14,7 @@
 
 struct sk_buff;
 
+int ovpn_struct_init(struct net_device *dev);
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
 
 #endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +107 -0

156 lines not on this page · raw message

drivers/net/ovpn/ovpnstruct.h +16 -0
index 932eb90953e0..c2cdfd6f84b3 100644
@@ -10,11 +10,27 @@
 #ifndef _NET_OVPN_OVPNSTRUCT_H_
 #define _NET_OVPN_OVPNSTRUCT_H_
 
+#include <uapi/linux/ovpn.h>
 #include <linux/netdevice.h>
+#include <linux/types.h>
 
 /* Our state per ovpn interface */
 struct ovpn_struct {
 	struct net_device *dev;
+
+	/* whether this device is still registered with netdev or not */
+	bool registered;
+
+	/* device operation mode (i.e. P2P, MP) */
+	enum ovpn_mode mode;
+
+	unsigned int max_tun_queue_len;
+
+	netdev_features_t set_features;
+
+	void *security;
+
+	struct list_head dev_list;
 };
 
 #endif /* _NET_OVPN_OVPNSTRUCT_H_ */
Signature · 1 line
2.43.0
#20 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

drivers/net/ovpn/io.c line 23

Quoted 6 lines
drivers/net/ovpn/io.c +6 -0
+int ovpn_struct_init(struct net_device *dev)
+{
+	struct ovpn_struct *ovpn = netdev_priv(dev);
+	int err;
+
+	memset(ovpn, 0, sizeof(*ovpn));

Probably not required. When a netdev is created, it should of zeroed the priv.

Quoted 29 lines
drivers/net/ovpn/main.c +29 -0
+int ovpn_iface_create(const char *name, enum ovpn_mode mode, struct net *net)
+{
+	struct net_device *dev;
+	struct ovpn_struct *ovpn;
+	int ret;
+
+	dev = alloc_netdev(sizeof(struct ovpn_struct), name, NET_NAME_USER, ovpn_setup);
+
+	dev_net_set(dev, net);
+
+	ret = ovpn_struct_init(dev);
+	if (ret < 0)
+		goto err;
+
+	ovpn = netdev_priv(dev);
+	ovpn->mode = mode;
+
+	rtnl_lock();
+
+	ret = register_netdevice(dev);
+	if (ret < 0) {
+		netdev_dbg(dev, "cannot register interface %s: %d\n", dev->name, ret);
+		rtnl_unlock();
+		goto err;
+	}
+	rtnl_unlock();
+
+	return ret;
+

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 10 lines
drivers/net/ovpn/main.c +10 -0
+err:
+	free_netdev(dev);
+	return ret;
+}
+
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Andrew

author maintainer
#21 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 9 lines · Andrew Lunn

drivers/net/ovpn/io.c line 23

Quoted 6 lines
drivers/net/ovpn/io.c +6 -0
+int ovpn_struct_init(struct net_device *dev)
+{
+	struct ovpn_struct *ovpn = netdev_priv(dev);
+	int err;
+
+	memset(ovpn, 0, sizeof(*ovpn));

Probably not required. When a netdev is created, it should of zeroed the priv.

ACK. There is a kvzalloc() involved.

Quoted 36 lines
Quoted 36 lines
drivers/net/ovpn/main.c +35 -0
+int ovpn_iface_create(const char *name, enum ovpn_mode mode, struct net *net)
+{
+	struct net_device *dev;
+	struct ovpn_struct *ovpn;
+	int ret;
+
+	dev = alloc_netdev(sizeof(struct ovpn_struct), name, NET_NAME_USER, ovpn_setup);
+
+	dev_net_set(dev, net);
+
+	ret = ovpn_struct_init(dev);
+	if (ret < 0)
+		goto err;
+
+	ovpn = netdev_priv(dev);
+	ovpn->mode = mode;
+
+	rtnl_lock();
+
+	ret = register_netdevice(dev);
+	if (ret < 0) {
+		netdev_dbg(dev, "cannot register interface %s: %d\n", dev->name, ret);
+		rtnl_unlock();
+		goto err;
+	}
+	rtnl_unlock();
+
+	return ret;
+
+err:
+	free_netdev(dev);
+	return ret;
+}
+
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
Quoted 8 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 4 lines
drivers/net/ovpn/main.c +4 -0
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Are you suggesting to turn it off both here and in the create function? Or should I remove the invocation above?

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#22 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 10 lines
Quoted 9 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Quoted 2 lines

Are you suggesting to turn it off both here and in the create function? Or should I remove the invocation above?

I noticed it in the _destruct function and went back to look at create. You probably want it in both, unless as part of destruct, you first disconnect all peers, which should set the carrier to off when the last peer disconnects?

Andrew

author maintainer
#23 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 10 lines · Andrew Lunn
Quoted 10 lines
Quoted 9 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Quoted 7 lines
Quoted 2 lines

Are you suggesting to turn it off both here and in the create function? Or should I remove the invocation above?

I noticed it in the _destruct function and went back to look at create. You probably want it in both, unless as part of destruct, you first disconnect all peers, which should set the carrier to off when the last peer disconnects?

I think keeping the carrier on while no peer is connected is better for OpenVPN.

I will then turn the carrier off in the create function as well. Thanks!

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#24 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 14 lines · Antonio Quartulli
Quoted 13 lines · Andrew Lunn
Quoted 12 lines
Quoted 9 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Are you suggesting to turn it off both here and in the create function? Or should I remove the invocation above?

Quoted 7 lines
Quoted 4 lines

I noticed it in the _destruct function and went back to look at create. You probably want it in both, unless as part of destruct, you first disconnect all peers, which should set the carrier to off when the last peer disconnects?

I think keeping the carrier on while no peer is connected is better for OpenVPN.

I then have to wounder what carrier actually means?

Some routing protocols will kick off determining routes when the carrier goes down. Can you put team/bonding on top of openvpn? If the peer has gone, you want team to fall over to the active backup?

Andrew

author maintainer
#25 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 20 lines · Andrew Lunn
Quoted 19 lines · Antonio Quartulli
Quoted 17 lines · Andrew Lunn
Quoted 10 lines
Quoted 9 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 151

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+void ovpn_iface_destruct(struct ovpn_struct *ovpn, bool unregister_netdev)
+{
+	ASSERT_RTNL();
+
+	netif_carrier_off(ovpn->dev);

You often see virtual devices turn their carrier off in there probe/create function, because it is unclear what state it is in after register_netdevice().

Quoted 2 lines

Are you suggesting to turn it off both here and in the create function? Or should I remove the invocation above?

I noticed it in the _destruct function and went back to look at create. You probably want it in both, unless as part of destruct, you first disconnect all peers, which should set the carrier to off when the last peer disconnects?

Quoted 8 lines
Quoted 2 lines

I think keeping the carrier on while no peer is connected is better for OpenVPN.

I then have to wounder what carrier actually means?

Some routing protocols will kick off determining routes when the carrier goes down. Can you put team/bonding on top of openvpn? If the peer has gone, you want team to fall over to the active backup?

IIRC team/bonding requires a L2 interface, but ovpn creates a L3 device. So I don't think this case applies here.

To be honest we don't have any real concept for the carrier off. Especially on a server, I hardly believe it would be any useful.

However, on a client or on a p2p link, where there is exactly one remote host on the other side, it may make sense to turn the carrier off when that remote peer is lost.

There is an extra detail to consider: if the user wants to, the ovpn device should remain configured (with all IPs and routes) even if openvpn has fully disconnected and it is attempting a reconnection to another server. Reason being avoiding data leaks by accidentally removing routes to the tunnel (traffic that should go through the tunnel would rather go to the local network).

With all this being said, it may make sense to just keep the carrier on all time long.

Should we come up with something smarter in the future, we can still improve this behaviour.

Regards,

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#26 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 14 lines

To be honest we don't have any real concept for the carrier off. Especially on a server, I hardly believe it would be any useful.

However, on a client or on a p2p link, where there is exactly one remote host on the other side, it may make sense to turn the carrier off when that remote peer is lost.

There is an extra detail to consider: if the user wants to, the ovpn device should remain configured (with all IPs and routes) even if openvpn has fully disconnected and it is attempting a reconnection to another server. Reason being avoiding data leaks by accidentally removing routes to the tunnel (traffic that should go through the tunnel would rather go to the local network).

Quoted 5 lines

With all this being said, it may make sense to just keep the carrier on all time long.

Should we come up with something smarter in the future, we can still improve this behaviour.

O.K, so..

You already have more patches than recommended for a series, but... I suggest you make the carrier_on in probe() a patch of its own with a good commit message based on the above. We then have a clear explanation in git why the carrier is always on.

Andrew

author maintainer
#27 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 4 lines · Andrew Lunn

You already have more patches than recommended for a series, but... I suggest you make the carrier_on in probe() a patch of its own with a good commit message based on the above. We then have a clear explanation in git why the carrier is always on.

ACK. will do.

Thanks for the recommendation.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
maintainer
#28 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

In reply to Antonio Quartulli

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 121

Quoted 3 lines · On Mon, 4 Mar 2024 16:08:55 +0100 Antonio Quartulli wrote:
drivers/net/ovpn/main.c +3 -0
+	dev = alloc_netdev(sizeof(struct ovpn_struct), name, NET_NAME_USER, ovpn_setup);
+
+	dev_net_set(dev, net);

the dev allocation never fails? Or the error is handles somewhere else?

author maintainer
#29 lore raw

Re: [PATCH net-next v2 04/22] ovpn: add basic interface creation/destruction/management routines

Quoted 6 lines · Jakub Kicinski

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 121

Quoted 3 lines · On Mon, 4 Mar 2024 16:08:55 +0100 Antonio Quartulli wrote:
drivers/net/ovpn/main.c +3 -0
+	dev = alloc_netdev(sizeof(struct ovpn_struct), name, NET_NAME_USER, ovpn_setup);
+
+	dev_net_set(dev, net);

the dev allocation never fails? Or the error is handles somewhere else?

It definitely can fail and dev_net_set will crash. My bad. Thanks for reporting this.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#30 lore raw

PATCH v2 5/22 ovpn: implement interface creation/destruction via netlink

In reply to Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
 drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +50 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
 		dev_put(ovpn->dev);
 }
 
+static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);
+	}
+
+	ret = ovpn_iface_create(ifname, mode, genl_info_net(info));
+	if (ret < 0)
+		netdev_dbg(dev, "error while creating interface %s: %d\n", ifname, ret);
+
+	return ret;
+}
+
+static int ovpn_nl_del_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+
+	rtnl_lock();
+	ovpn_iface_destruct(ovpn, true);
+	dev_put(ovpn->dev);
+	rtnl_unlock();
+
+	/* we set the user_ptr to NULL to prevent post_doit from releasing it again */
+	info->user_ptr[0] = NULL;
+
+	return 0;
+}
+
 static const struct genl_small_ops ovpn_nl_ops[] = {
+	{
+		.cmd = OVPN_CMD_NEW_IFACE,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_new_iface,
+	},
+	{
+		.cmd = OVPN_CMD_DEL_IFACE,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_del_iface,
+	},
 };
 
 static struct genl_family ovpn_nl_family __ro_after_init = {
Signature · 1 line
2.43.0
reviewer
#31 lore raw

Re: [PATCH net-next v2 05/22] ovpn: implement interface creation/destruction via netlink

Quoted 22 lines · Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
 drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +6 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
 		dev_put(ovpn->dev);
 }
 
+static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 172

Quoted 10 lines
drivers/net/ovpn/netlink.c +10 -0
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);

nit: dev is uninitialised here.

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 177

Quoted 5 lines
drivers/net/ovpn/netlink.c +5 -0
+	}
+
+	ret = ovpn_iface_create(ifname, mode, genl_info_net(info));
+	if (ret < 0)
+		netdev_dbg(dev, "error while creating interface %s: %d\n", ifname, ret);

And here.

Flagged by Smatch.

Quoted 3 lines
drivers/net/ovpn/netlink.c +3 -0
+
+	return ret;
+}

...

author maintainer
#32 lore raw

Re: [PATCH net-next v2 05/22] ovpn: implement interface creation/destruction via netlink

Quoted 44 lines · Simon Horman
Quoted 22 lines · Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
  drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +6 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
  		dev_put(ovpn->dev);
  }
 
+static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 172

Quoted 10 lines
drivers/net/ovpn/netlink.c +10 -0
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);

nit: dev is uninitialised here.

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 177

Quoted 5 lines
drivers/net/ovpn/netlink.c +5 -0
+	}
+
+	ret = ovpn_iface_create(ifname, mode, genl_info_net(info));
+	if (ret < 0)
+		netdev_dbg(dev, "error while creating interface %s: %d\n", ifname, ret);

And here.

Quoted 1 line

Flagged by Smatch.

Thanks for reporting these.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#33 lore raw

Re: [PATCH net-next v2 05/22] ovpn: implement interface creation/destruction via netlink

In reply to Antonio Quartulli
Quoted 22 lines · Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
 drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +6 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
 		dev_put(ovpn->dev);
 }
 
+static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 172

Quoted 10 lines
drivers/net/ovpn/netlink.c +10 -0
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);

Maybe print out the message even if the default mode is used, as the mode is applied in ovpn_iface_create anyways.

/Esben

author maintainer
#34 lore raw

Re: [PATCH net-next v2 05/22] ovpn: implement interface creation/destruction via netlink

Quoted 29 lines · Esben Haabendal
Quoted 27 lines · Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
  drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +11 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
  		dev_put(ovpn->dev);
  }
 
+static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
Quoted 8 lines

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 172

Quoted 5 lines
drivers/net/ovpn/netlink.c +5 -0
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);

Maybe print out the message even if the default mode is used, as the mode is applied in ovpn_iface_create anyways.

Being this a debug message, my reasoning was "let's print what we got via netlink" (if nothing is printed, we know we are applying the default).

Otherwise, when printing "P2P" we wouldn't be able to understand if it was set by default or received via netlink.

Does it make sense?

Cheers,

Quoted 2 lines

/Esben

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#35 lore raw

Re: [PATCH net-next v2 05/22] ovpn: implement interface creation/destruction via netlink

Quoted 37 lines · Antonio Quartulli
Quoted 27 lines · Esben Haabendal
Quoted 25 lines · Antonio Quartulli

Allow userspace to create and destroy an interface using netlink commands.

---
  drivers/net/ovpn/netlink.c | 50 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
drivers/net/ovpn/netlink.c +9 -0
index 2e855ce145e7..02b41034f615 100644
@@ -154,7 +154,57 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
  		dev_put(ovpn->dev);
  }
  +static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
+{
+	enum ovpn_mode mode = OVPN_MODE_P2P;
+	struct net_device *dev;
+	char *ifname;
+	int ret;
+
+	if (!info->attrs[OVPN_A_IFNAME])
+		return -EINVAL;
+
Quoted 8 lines

drivers/net/ovpn/netlink.c static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb · line 172

Quoted 6 lines
drivers/net/ovpn/netlink.c +6 -0
+	ifname = nla_data(info->attrs[OVPN_A_IFNAME]);
+
+	if (info->attrs[OVPN_A_MODE]) {
+		mode = nla_get_u8(info->attrs[OVPN_A_MODE]);
+		netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, ifname,
+			   mode);

Maybe print out the message even if the default mode is used, as the mode is applied in ovpn_iface_create anyways.

Quoted 7 lines

Being this a debug message, my reasoning was "let's print what we got via netlink" (if nothing is printed, we know we are applying the default).

Otherwise, when printing "P2P" we wouldn't be able to understand if it was set by default or received via netlink.

Does it make sense?

Yes, reasoning is sane. And the prefixing with __func__ should help making it clear.

/Esben

author maintainer
#36 lore raw

PATCH v2 6/22 ovpn: introduce the ovpn_peer object

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/Makefile     |   2 +
 drivers/net/ovpn/bind.c       |  60 ++++++
 drivers/net/ovpn/bind.h       |  91 +++++++++
 drivers/net/ovpn/io.c         |   7 +
 drivers/net/ovpn/main.c       |   6 +
 drivers/net/ovpn/ovpnstruct.h |  12 +-
 drivers/net/ovpn/peer.c       | 354 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h       |  88 +++++++++
 8 files changed, 619 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/ovpn/bind.c
 create mode 100644 drivers/net/ovpn/bind.h
 create mode 100644 drivers/net/ovpn/peer.c
 create mode 100644 drivers/net/ovpn/peer.h
drivers/net/ovpn/Makefile +2 -0
index 7e406a69892a..1399b9d9c076 100644
@@ -7,6 +7,8 @@
 # Author:	Antonio Quartulli <[email protected]>
 
 obj-$(CONFIG_OVPN) += ovpn.o
+ovpn-y += bind.o
 ovpn-y += main.o
 ovpn-y += io.o
 ovpn-y += netlink.o
+ovpn-y += peer.o
drivers/net/ovpn/bind.c new file +60 -0

60 lines not on this page · raw message

drivers/net/ovpn/bind.h new file +91 -0

91 lines not on this page · raw message

drivers/net/ovpn/io.c +7 -0
index b283449ba479..ede15c84ea69 100644
@@ -10,6 +10,7 @@
 #include "io.h"
 #include "ovpnstruct.h"
 #include "netlink.h"
+#include "peer.h"
 
 #include <linux/netdevice.h>
 #include <linux/skbuff.h>
@@ -28,6 +29,12 @@ int ovpn_struct_init(struct net_device *dev)
 	if (err < 0)
 		return err;
 
+	spin_lock_init(&ovpn->lock);
+
+	ovpn->events_wq = alloc_workqueue("ovpn-events-wq-%s", WQ_MEM_RECLAIM, 0, dev->name);
+	if (!ovpn->events_wq)
+		return -ENOMEM;
+
 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
 	if (!dev->tstats)
 		return -ENOMEM;
drivers/net/ovpn/main.c +6 -0
index 1be0fd50c356..3e054811b8c6 100644
@@ -21,6 +21,7 @@
 #include <linux/net.h>
 #include <linux/inetdevice.h>
 #include <linux/netdevice.h>
+#include <linux/rcupdate.h>
 #include <linux/version.h>
 #include <net/ip.h>
 #include <uapi/linux/if_arp.h>
@@ -41,6 +42,9 @@ static void ovpn_struct_free(struct net_device *net)
 
 	security_tun_dev_free_security(ovpn->security);
 	free_percpu(net->tstats);
+	flush_workqueue(ovpn->events_wq);
+	destroy_workqueue(ovpn->events_wq);
+	rcu_barrier();
 }
 
 /* Net device open */
@@ -228,6 +232,8 @@ static __exit void ovpn_cleanup(void)
 {
 	unregister_netdevice_notifier(&ovpn_netdev_notifier);
 	ovpn_nl_unregister();
+
+	rcu_barrier();
 }
 
 module_init(ovpn_init);
drivers/net/ovpn/ovpnstruct.h +11 -1
index c2cdfd6f84b3..a595d44f2276 100644
@@ -12,6 +12,7 @@
 
 #include <uapi/linux/ovpn.h>
 #include <linux/netdevice.h>
+#include <linux/spinlock.h>
 #include <linux/types.h>
 
 /* Our state per ovpn interface */
@@ -24,7 +25,16 @@ struct ovpn_struct {
 	/* device operation mode (i.e. P2P, MP) */
 	enum ovpn_mode mode;
 
-	unsigned int max_tun_queue_len;
+	/* protect writing to the ovpn_struct object */
+	spinlock_t lock;
+
+	/* workqueue used to schedule generic event that may sleep or that need
+	 * to be performed out of softirq context
+	 */
+	struct workqueue_struct *events_wq;
+
+	/* for p2p mode */
+	struct ovpn_peer __rcu *peer;
 
 	netdev_features_t set_features;
 
drivers/net/ovpn/peer.c new file +354 -0

354 lines not on this page · raw message

drivers/net/ovpn/peer.h new file +88 -0

88 lines not on this page · raw message

Signature · 1 line
2.43.0
#37 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 9 lines · Antonio Quartulli

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

introduced

drivers/net/ovpn/bind.h line 51

Quoted 10 lines
drivers/net/ovpn/bind.h new file +10 -0
+/* Translate skb->protocol value to AF_INET or AF_INET6 */
+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;

That feels like the sort of thing which should already exist somewhere. But a quick search did not find it...

Andrew

author maintainer
#38 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 13 lines · Andrew Lunn
Quoted 9 lines · Antonio Quartulli

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

introduced

Quoted 13 lines

drivers/net/ovpn/bind.h line 51

Quoted 10 lines
drivers/net/ovpn/bind.h new file +10 -0
+/* Translate skb->protocol value to AF_INET or AF_INET6 */
+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;

That feels like the sort of thing which should already exist somewhere. But a quick search did not find it...

Do you think we have a similar pattern in other drivers?
If so, it may be worth implementing this functionality in a kernel header.

Regards,

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#39 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

In reply to Antonio Quartulli
Quoted 7 lines
drivers/net/ovpn/peer.c new file +7 -0
+	ret = ptr_ring_init(&peer->tx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate TX ring\n", __func__);
+		goto err_dst_cache;
+	}
+
+	ret = ptr_ring_init(&peer->rx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);

drivers/net/ovpn/peer.c line 69

Quoted 10 lines
drivers/net/ovpn/peer.c new file +10 -0
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate RX ring\n", __func__);
+		goto err_tx_ring;
+	}
+
+	ret = ptr_ring_init(&peer->netif_rx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate NETIF RX ring\n", __func__);
+		goto err_rx_ring;
+	}

These rings are 1024 entries? The real netif below also likely has another 1024 entry ring. Rings like this are latency. Is there a BQL like mechanism to actually keep the rings empty, throw packets away rather than queue them, because queueing them just accumulates latency?

So, i guess my question is, how do you avoid bufferbloat? Why do you actually need these rings?

Andrew

author maintainer
#40 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 11 lines · Andrew Lunn
Quoted 11 lines
drivers/net/ovpn/peer.c new file +11 -0
+	ret = ptr_ring_init(&peer->tx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate TX ring\n", __func__);
+		goto err_dst_cache;
+	}
+
+	ret = ptr_ring_init(&peer->rx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate RX ring\n", __func__);
+		goto err_tx_ring;
+	}
Quoted 8 lines

drivers/net/ovpn/peer.c line 69

Quoted 6 lines
drivers/net/ovpn/peer.c new file +6 -0
+
+	ret = ptr_ring_init(&peer->netif_rx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot allocate NETIF RX ring\n", __func__);
+		goto err_rx_ring;
+	}

These rings are 1024 entries?

Yes

Quoted 5 lines

The real netif below also likely has
another 1024 entry ring. Rings like this are latency. Is there a BQL like mechanism to actually keep the rings empty, throw packets away rather than queue them, because queueing them just accumulates latency?

No BQL is implemented yet.

Quoted 3 lines

So, i guess my question is, how do you avoid bufferbloat? Why do you actually need these rings?

This is a very good point where I might require some input/feedback.
I have not ignored the problem, but I was hoping to solve it in a future iteration. (all the better if we can get it out the way right now)

The reason for having these rings is to pass packets between contexts.

When packets are received from the network in softirq context, they are queued in the rx_ring and later processed by a dedicated worker. The latter also takes care of decryption, which may sleep.

The same, but symmetric, process happens for packets sent by the user to the device: queued in tx_ring and then encrypted by the dedicated worker.

netif_rx_ring is just a queue for NAPI.

I can definitely have a look at BQL, but feel free to drop me any pointer/keyword as to what I should look at.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#41 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 10 lines

This is a very good point where I might require some input/feedback. I have not ignored the problem, but I was hoping to solve it in a future iteration. (all the better if we can get it out the way right now)

The reason for having these rings is to pass packets between contexts.

When packets are received from the network in softirq context, they are queued in the rx_ring and later processed by a dedicated worker. The latter also takes care of decryption, which may sleep.

Quoted 8 lines

The same, but symmetric, process happens for packets sent by the user to the
device: queued in tx_ring and then encrypted by the dedicated worker.

netif_rx_ring is just a queue for NAPI.

I can definitely have a look at BQL, but feel free to drop me any pointer/keyword as to what I should look at.

Do you have any measurements about the average and maximum fill size of these queues? If you could make the rings smaller, the whole question of latency and bufferbloat disappears. NAPI tends to deal with up to 64 packets. So could you make these rings 128 in size?

Andrew

author maintainer
#42 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 16 lines · Andrew Lunn
Quoted 16 lines

This is a very good point where I might require some input/feedback. I have not ignored the problem, but I was hoping to solve it in a future iteration. (all the better if we can get it out the way right now)

The reason for having these rings is to pass packets between contexts.

When packets are received from the network in softirq context, they are queued in the rx_ring and later processed by a dedicated worker. The latter also takes care of decryption, which may sleep.

The same, but symmetric, process happens for packets sent by the user to the
device: queued in tx_ring and then encrypted by the dedicated worker.

netif_rx_ring is just a queue for NAPI.

Quoted 7 lines
Quoted 2 lines

I can definitely have a look at BQL, but feel free to drop me any pointer/keyword as to what I should look at.

Do you have any measurements about the average and maximum fill size of these queues? If you could make the rings smaller, the whole question of latency and bufferbloat disappears. NAPI tends to deal with up to 64 packets. So could you make these rings 128 in size?

I don't have actual numbers at hand.
Fill size depends on encryption/decryption speed vs network speed.

I can tell you that, while performing stress tests on AWS, we could get to the point where all rings are full and packets started being dropped.

However, this may be a ill-formed case, where network is much faster than the crypto part.

I will try to measure how many packets are kept in the queue under standard utilization. That should tell us if 128 is a decent size or not.

Thanks for the hint!

Regards,

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#43 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

In reply to Antonio Quartulli

drivers/net/ovpn/peer.h line 71

Quoted 9 lines
drivers/net/ovpn/peer.h new file +9 -0
+static inline bool ovpn_peer_hold(struct ovpn_peer *peer)
+{
+	return kref_get_unless_zero(&peer->refcount);
+}
+
+static inline void ovpn_peer_put(struct ovpn_peer *peer)
+{
+	kref_put(&peer->refcount, ovpn_peer_release_kref);
+}

It is reasonably normal in the kernel to use _get() which takes a reference on something and _put() to release it.

drivers/net/ovpn/peer.h line 83

Quoted 4 lines
drivers/net/ovpn/peer.h new file +4 -0
+struct ovpn_peer *ovpn_peer_lookup_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);

All these look to take a reference on the peer. So maybe replace lookup by get? It should then be easier to check there is a matching put to every get.

Andrew

author maintainer
#44 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 4 lines · Andrew Lunn
Quoted 4 lines
drivers/net/ovpn/peer.h new file +4 -0
+static inline bool ovpn_peer_hold(struct ovpn_peer *peer)
+{
+	return kref_get_unless_zero(&peer->refcount);
+}
Quoted 8 lines

drivers/net/ovpn/peer.h line 71

Quoted 5 lines
drivers/net/ovpn/peer.h new file +5 -0
+
+static inline void ovpn_peer_put(struct ovpn_peer *peer)
+{
+	kref_put(&peer->refcount, ovpn_peer_release_kref);
+}

It is reasonably normal in the kernel to use _get() which takes a reference on something and _put() to release it.

I think I got inspired by dev_hold(), but I agree that _get() is more appropriate.

Will change that.

Quoted 9 lines

drivers/net/ovpn/peer.h line 83

Quoted 5 lines
drivers/net/ovpn/peer.h new file +4 -0
+struct ovpn_peer *ovpn_peer_lookup_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb);
+struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);

All these look to take a reference on the peer. So maybe replace lookup by get? It should then be easier to check there is a matching put to every get.

Alright, will do!

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#45 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

In reply to Antonio Quartulli

drivers/net/ovpn/bind.h line 58

Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/bind.h new file +4 -0
+static inline bool ovpn_bind_skb_src_match(const struct ovpn_bind *bind, struct sk_buff *skb)
+{
+	const unsigned short family = skb_protocol_to_family(skb);
+	const struct ovpn_sockaddr *sa = &bind->sa;

You should move this dereferencing of bind to after the following check to avoid segmentation fault.

Quoted 8 lines
(quoted diff) +2 -0
+	if (unlikely(!bind))
+		return false;
drivers/net/ovpn/peer.c new file +0 -0
new file mode 100644
index 000000000000..4319271927a4

drivers/net/ovpn/peer.c line 133

Quoted 10 lines
drivers/net/ovpn/peer.c new file +10 -0
+
+/* Use with kref_put calls, when releasing refcount
+ * on ovpn_peer objects.  This method should only
+ * be called from process context with config_mutex held.
+ */
+void ovpn_peer_release_kref(struct kref *kref)
+{
+	struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount);
+
+	INIT_WORK(&peer->delete_work, ovpn_peer_delete_work);

Is this safe, or could we end up re-initializing delete_work while it is queued or running?

drivers/net/ovpn/peer.c line 134

Quoted 1 line
drivers/net/ovpn/peer.c new file +1 -0
+	queue_work(peer->ovpn->events_wq, &peer->delete_work);

/Esben

author maintainer
#46 lore raw

Re: [PATCH net-next v2 06/22] ovpn: introduce the ovpn_peer object

Quoted 9 lines · Esben Haabendal

drivers/net/ovpn/bind.h line 58

Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/bind.h new file +4 -0
+static inline bool ovpn_bind_skb_src_match(const struct ovpn_bind *bind, struct sk_buff *skb)
+{
+	const unsigned short family = skb_protocol_to_family(skb);
+	const struct ovpn_sockaddr *sa = &bind->sa;

You should move this dereferencing of bind to after the following check to avoid segmentation fault.

ouch. good catch, thanks!

Quoted 14 lines
Quoted 14 lines
(quoted diff) +2 -0
+	if (unlikely(!bind))
+		return false;
drivers/net/ovpn/peer.c new file +5 -0
new file mode 100644
index 000000000000..4319271927a4
+
+/* Use with kref_put calls, when releasing refcount
+ * on ovpn_peer objects.  This method should only
+ * be called from process context with config_mutex held.
+ */
Quoted 8 lines

drivers/net/ovpn/peer.c line 133

Quoted 5 lines
drivers/net/ovpn/peer.c new file +5 -0
+void ovpn_peer_release_kref(struct kref *kref)
+{
+	struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount);
+
+	INIT_WORK(&peer->delete_work, ovpn_peer_delete_work);

Is this safe, or could we end up re-initializing delete_work while it is queued or running?

ovpn_peer_release_kref() is only invoked once upon peer refcounter reaching 0 (then the peer object is erased). Therefore it should be safe to init the work object here.

Cheers,

Quoted 4 lines

drivers/net/ovpn/peer.c line 134

Quoted 2 lines
drivers/net/ovpn/peer.c new file +1 -0
+	queue_work(peer->ovpn->events_wq, &peer->delete_work);

/Esben

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#47 lore raw

PATCH v2 7/22 ovpn: introduce the ovpn_socket object

In reply to Antonio Quartulli

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 attathced to them for status tracking purposes.

Initially only UDP support is introduced. TCP will come in a later patch.

---
 drivers/net/ovpn/Makefile |  2 +
 drivers/net/ovpn/socket.c | 98 +++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/socket.h | 47 +++++++++++++++++++
 drivers/net/ovpn/udp.c    | 46 ++++++++++++++++++
 drivers/net/ovpn/udp.h    | 18 +++++++
 5 files changed, 211 insertions(+)
 create mode 100644 drivers/net/ovpn/socket.c
 create mode 100644 drivers/net/ovpn/socket.h
 create mode 100644 drivers/net/ovpn/udp.c
 create mode 100644 drivers/net/ovpn/udp.h
drivers/net/ovpn/Makefile +2 -0
index 1399b9d9c076..f768cd95b777 100644
@@ -12,3 +12,5 @@ ovpn-y += main.o
 ovpn-y += io.o
 ovpn-y += netlink.o
 ovpn-y += peer.o
+ovpn-y += socket.o
+ovpn-y += udp.o
drivers/net/ovpn/socket.c new file +98 -0

98 lines not on this page · raw message

drivers/net/ovpn/socket.h new file +47 -0

47 lines not on this page · raw message

drivers/net/ovpn/udp.c new file +46 -0

46 lines not on this page · raw message

drivers/net/ovpn/udp.h new file +18 -0
new file mode 100644
index 000000000000..9ba41bd539aa
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_UDP_H_
+#define _NET_OVPN_UDP_H_
+
+#include <net/sock.h>
+
+struct ovpn_struct;
+
+int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn);
+
+#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.43.0
reviewer
#48 lore raw

Re: [PATCH net-next v2 07/22] ovpn: introduce the ovpn_socket object

On Mon, Mar 04, 2024 at 04:08:58PM +0100, Antonio Quartulli wrote:

...

Quoted 20 lines
drivers/net/ovpn/socket.h new file +14 -0
new file mode 100644
index 000000000000..92c50f795f7c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_SOCK_H_
+#define _NET_OVPN_SOCK_H_
+
+#include <linux/net.h>
+#include <linux/kref.h>

drivers/net/ovpn/socket.h line 24

Quoted 10 lines
drivers/net/ovpn/socket.h new file +10 -0
+#include <linux/ptr_ring.h>
+#include <net/sock.h>
+
+
+struct ovpn_struct;
+struct ovpn_peer;
+
+/**
+ * struct ovpn_socket - a kernel socket referenced in the ovpn code
+ */

nit: ./scripts/kernel-doc -none
complains that the fields of this structure are not covered by it's Kernel doc.

Quoted 10 lines
drivers/net/ovpn/socket.h new file +10 -0
+struct ovpn_socket {
+	/* the VPN session object owning this socket (UDP only) */
+	struct ovpn_struct *ovpn;
+	/* the kernel socket */
+	struct socket *sock;
+	/* amount of contexts currently referencing this object */
+	struct kref refcount;
+	/* member used to schedule RCU destructor callback */
+	struct rcu_head rcu;
+};

...

author maintainer
#49 lore raw

Re: [PATCH net-next v2 07/22] ovpn: introduce the ovpn_socket object

Quoted 4 lines · Simon Horman

nit: ./scripts/kernel-doc -none
complains that the fields of this structure are not covered by it's Kernel doc.

Thanks a lot for telling me about this other script. It's been a while since my last kernel submission..

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#50 lore raw

PATCH v2 8/22 ovpn: implement basic TX path (UDP)

In reply to Antonio Quartulli

Packets sent over the ovpn interface are processed and transmitted to the connected peer, if any.

Impementation is UDP only. TCP will be added by a later patch.

Note: no crypto/encapsulation exists yet. packets are just captured and sent.

---
 drivers/net/ovpn/io.c         | 165 ++++++++++++++++++++++++++++-
 drivers/net/ovpn/io.h         |   2 +
 drivers/net/ovpn/main.c       |   2 +
 drivers/net/ovpn/ovpnstruct.h |   3 +
 drivers/net/ovpn/peer.c       |   5 +
 drivers/net/ovpn/peer.h       |   9 ++
 drivers/net/ovpn/udp.c        | 188 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/udp.h        |   4 +
 8 files changed, 377 insertions(+), 1 deletion(-)
drivers/net/ovpn/io.c +164 -1
index ede15c84ea69..d2fdb7485023 100644
@@ -11,9 +11,11 @@
 #include "ovpnstruct.h"
 #include "netlink.h"
 #include "peer.h"
+#include "udp.h"
 
 #include <linux/netdevice.h>
 #include <linux/skbuff.h>
+#include <net/gso.h>
 
 
 int ovpn_struct_init(struct net_device *dev)
@@ -31,6 +33,11 @@ int ovpn_struct_init(struct net_device *dev)
 
 	spin_lock_init(&ovpn->lock);
 
+	ovpn->crypto_wq = alloc_workqueue("ovpn-crypto-wq-%s",
+					  WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
+	if (!ovpn->crypto_wq)
+		return -ENOMEM;
+
 	ovpn->events_wq = alloc_workqueue("ovpn-events-wq-%s", WQ_MEM_RECLAIM, 0, dev->name);
 	if (!ovpn->events_wq)
 		return -ENOMEM;
@@ -46,11 +53,167 @@ int ovpn_struct_init(struct net_device *dev)
 	return 0;
 }
 
+static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	return true;
+}
+
+/* Process packets in TX queue in a transport-specific way.
+ *
+ * UDP transport - encrypt and send across the tunnel.
+ */
+void ovpn_encrypt_work(struct work_struct *work)
+{
+	struct sk_buff *skb, *curr, *next;
+	struct ovpn_peer *peer;
+
+	peer = container_of(work, struct ovpn_peer, encrypt_work);
+	while ((skb = ptr_ring_consume_bh(&peer->tx_ring))) {
+		/* this might be a GSO-segmented skb list: process each skb
+		 * independently
+		 */
+		skb_list_walk_safe(skb, curr, next) {
+			/* if one segment fails encryption, we drop the entire
+			 * packet, because it does not really make sense to send
+			 * only part of it at this point
+			 */
+			if (unlikely(!ovpn_encrypt_one(peer, curr))) {
+				kfree_skb_list(skb);
+				skb = NULL;
+				break;
+			}
+		}
+
+		/* successful encryption */
+		if (likely(skb)) {
+			skb_list_walk_safe(skb, curr, next) {
+				skb_mark_not_on_list(curr);
+
+				switch (peer->sock->sock->sk->sk_protocol) {
+				case IPPROTO_UDP:
+					ovpn_udp_send_skb(peer->ovpn, peer, curr);
+					break;
+				default:
+					/* no transport configured yet */
+					consume_skb(skb);
+					break;
+				}
+			}
+		}
+
+		/* give a chance to be rescheduled if needed */
+		cond_resched();
+	}
+	ovpn_peer_put(peer);
+}
+
+/* send skb to connected peer, if any */
+static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
+{
+	int ret;
+
+	if (likely(!peer))
+		/* retrieve peer serving the destination IP of this packet */
+		peer = ovpn_peer_lookup_by_dst(ovpn, skb);
+	if (unlikely(!peer)) {
+		net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
+		goto drop;
+	}
+
+	ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
+	if (unlikely(ret < 0)) {
+		net_err_ratelimited("%s: cannot queue packet to TX ring\n", peer->ovpn->dev->name);
+		goto drop;
+	}
+
+	if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
+		ovpn_peer_put(peer);
+
+	return;
+drop:
+	if (peer)
+		ovpn_peer_put(peer);
+	kfree_skb_list(skb);
+}
+
+/* Return IP protocol version from skb header.
+ * Return 0 if protocol is not IPv4/IPv6 or cannot be read.
+ */
+static __be16 ovpn_ip_check_protocol(struct sk_buff *skb)
+{
+	__be16 proto = 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 == 4)
+		proto = htons(ETH_P_IP);
+	else if (ip_hdr(skb)->version == 6)
+		proto = htons(ETH_P_IPV6);
+
+	return proto;
+}
+
 /* Send user data to the network
  */
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 {
+	struct ovpn_struct *ovpn = netdev_priv(dev);
+	struct sk_buff *segments, *tmp, *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 = ovpn_ip_check_protocol(skb);
+	if (unlikely(!proto || skb->protocol != proto)) {
+		net_err_ratelimited("%s: dropping malformed payload packet\n",
+				    dev->name);
+		goto drop;
+	}
+
+	if (skb_is_gso(skb)) {
+		segments = skb_gso_segment(skb, 0);
+		if (IS_ERR(segments)) {
+			ret = PTR_ERR(segments);
+			net_err_ratelimited("%s: cannot segment packet: %d\n", dev->name, ret);
+			goto drop;
+		}
+
+		consume_skb(skb);
+		skb = 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);
+
+		tmp = skb_share_check(curr, GFP_ATOMIC);
+		if (unlikely(!tmp)) {
+			kfree_skb_list(next);
+			net_err_ratelimited("%s: skb_share_check failed\n", dev->name);
+			goto drop_list;
+		}
+
+		__skb_queue_tail(&skb_list, tmp);
+	}
+	skb_list.prev->next = NULL;
+
+	ovpn_queue_skb(ovpn, skb_list.next, NULL);
+
+	return NETDEV_TX_OK;
+
+drop_list:
+	skb_queue_walk_safe(&skb_list, curr, next)
+		kfree_skb(curr);
+drop:
 	skb_tx_error(skb);
-	kfree_skb(skb);
+	kfree_skb_list(skb);
 	return NET_XMIT_DROP;
 }
drivers/net/ovpn/io.h +2 -0
index e7728718c8a9..633b9fb3276c 100644
@@ -17,4 +17,6 @@ struct sk_buff;
 int ovpn_struct_init(struct net_device *dev);
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
 
+void ovpn_encrypt_work(struct work_struct *work);
+
 #endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +2 -0
index 3e054811b8c6..95a94ccc99c1 100644
@@ -42,7 +42,9 @@ static void ovpn_struct_free(struct net_device *net)
 
 	security_tun_dev_free_security(ovpn->security);
 	free_percpu(net->tstats);
+	flush_workqueue(ovpn->crypto_wq);
 	flush_workqueue(ovpn->events_wq);
+	destroy_workqueue(ovpn->crypto_wq);
 	destroy_workqueue(ovpn->events_wq);
 	rcu_barrier();
 }
drivers/net/ovpn/ovpnstruct.h +3 -0
index a595d44f2276..300906bc694f 100644
@@ -28,6 +28,9 @@ struct ovpn_struct {
 	/* protect writing to the ovpn_struct object */
 	spinlock_t lock;
 
+	/* workqueue used to schedule crypto work that may sleep during TX/RX */
+	struct workqueue_struct *crypto_wq;
+
 	/* workqueue used to schedule generic event that may sleep or that need
 	 * to be performed out of softirq context
 	 */
drivers/net/ovpn/peer.c +5 -0
index 4319271927a4..4dbbd25b25c9 100644
@@ -44,6 +44,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)
 	spin_lock_init(&peer->lock);
 	kref_init(&peer->refcount);
 
+	INIT_WORK(&peer->encrypt_work, ovpn_encrypt_work);
+
 	ret = dst_cache_init(&peer->dst_cache, GFP_KERNEL);
 	if (ret < 0) {
 		netdev_err(ovpn->dev, "%s: cannot initialize dst cache\n", __func__);
@@ -112,6 +114,9 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)
 
 void ovpn_peer_release(struct ovpn_peer *peer)
 {
+	if (peer->sock)
+		ovpn_socket_put(peer->sock);
+
 	call_rcu(&peer->rcu, ovpn_peer_release_rcu);
 }
 
drivers/net/ovpn/peer.h +9 -0
index c3ccbb6bdf41..ef4174be7dea 100644
@@ -11,6 +11,7 @@
 #define _NET_OVPN_OVPNPEER_H_
 
 #include "bind.h"
+#include "socket.h"
 
 #include <linux/ptr_ring.h>
 #include <net/dst_cache.h>
@@ -27,10 +28,18 @@ struct ovpn_peer {
 		struct in6_addr ipv6;
 	} vpn_addrs;
 
+	/* work objects to handle encryption/decryption of packets.
+	 * these works are queued on the ovpn->crypt_wq workqueue.
+	 */
+	struct work_struct encrypt_work;
+	struct work_struct decrypt_work;
+
 	struct ptr_ring tx_ring;
 	struct ptr_ring rx_ring;
 	struct ptr_ring netif_rx_ring;
 
+	struct ovpn_socket *sock;
+
 	struct dst_cache dst_cache;
 
 	/* our binding to peer, protected by spinlock */
drivers/net/ovpn/udp.c +188 -0

200 lines not on this page · raw message

drivers/net/ovpn/udp.h +4 -0
index 9ba41bd539aa..4d6ba9ecabd1 100644
@@ -9,10 +9,14 @@
 #ifndef _NET_OVPN_UDP_H_
 #define _NET_OVPN_UDP_H_
 
+#include <linux/skbuff.h>
 #include <net/sock.h>
 
+struct ovpn_peer;
 struct ovpn_struct;
 
 int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn);
+void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer,
+		       struct sk_buff *skb);
 
 #endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.43.0
maintainer
#51 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

Quoted 9 lines · On Mon, 4 Mar 2024 16:08:59 +0100 Antonio Quartulli wrote:
drivers/net/ovpn/io.c +9 -0
+	if (skb_is_gso(skb)) {
+		segments = skb_gso_segment(skb, 0);
+		if (IS_ERR(segments)) {
+			ret = PTR_ERR(segments);
+			net_err_ratelimited("%s: cannot segment packet: %d\n", dev->name, ret);
+			goto drop;
+		}
+
+		consume_skb(skb);

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 197

Quoted 10 lines
drivers/net/ovpn/io.c +10 -0
+		skb = 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);
+
+		tmp = skb_share_check(curr, GFP_ATOMIC);

The share check needs to be before the segmentation, I think.

author maintainer
#52 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

Quoted 14 lines · Jakub Kicinski
Quoted 13 lines · On Mon, 4 Mar 2024 16:08:59 +0100 Antonio Quartulli wrote:
drivers/net/ovpn/io.c +13 -0
+	if (skb_is_gso(skb)) {
+		segments = skb_gso_segment(skb, 0);
+		if (IS_ERR(segments)) {
+			ret = PTR_ERR(segments);
+			net_err_ratelimited("%s: cannot segment packet: %d\n", dev->name, ret);
+			goto drop;
+		}
+
+		consume_skb(skb);
+		skb = segments;
+	}
+
+	/* from this moment on, "skb" might be a list */
Quoted 8 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 197

Quoted 6 lines
drivers/net/ovpn/io.c +6 -0
+
+	__skb_queue_head_init(&skb_list);
+	skb_list_walk_safe(skb, curr, next) {
+		skb_mark_not_on_list(curr);
+
+		tmp = skb_share_check(curr, GFP_ATOMIC);

The share check needs to be before the segmentation, I think.

To be honest, I am not 100% sure.

I checked other occurrences of skb_gso_segment() and I don't see skb_share_check() being invoked beforehand.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#53 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

In reply to Antonio Quartulli
Quoted 18 lines · Antonio Quartulli
drivers/net/ovpn/io.c +18 -0
+/* send skb to connected peer, if any */
+static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
+{
+	int ret;
+
+	if (likely(!peer))
+		/* retrieve peer serving the destination IP of this packet */
+		peer = ovpn_peer_lookup_by_dst(ovpn, skb);
+	if (unlikely(!peer)) {
+		net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
+		goto drop;
+	}
+
+	ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
+	if (unlikely(ret < 0)) {
+		net_err_ratelimited("%s: cannot queue packet to TX ring\n", peer->ovpn->dev->name);
+		goto drop;
+	}

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 137

Quoted 10 lines
drivers/net/ovpn/io.c +10 -0
+
+	if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
+		ovpn_peer_put(peer);
+
+	return;
+drop:
+	if (peer)
+		ovpn_peer_put(peer);
+	kfree_skb_list(skb);
+}

So this puts packets on a per-peer 1024-packet FIFO queue with no backpressure? That sounds like a pretty terrible bufferbloat situation. Did you do any kind of latency-under-load testing of this, such as running the RRUL test[0] through it?

-Toke

[0] https://flent.org/tests.html#the-realtime-response-under-load-rrul-test

author maintainer
#54 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

Hi Toke,

Quoted 27 lines · Toke Høiland-Jørgensen
Quoted 25 lines · Antonio Quartulli
drivers/net/ovpn/io.c +25 -0
+/* send skb to connected peer, if any */
+static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
+{
+	int ret;
+
+	if (likely(!peer))
+		/* retrieve peer serving the destination IP of this packet */
+		peer = ovpn_peer_lookup_by_dst(ovpn, skb);
+	if (unlikely(!peer)) {
+		net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
+		goto drop;
+	}
+
+	ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
+	if (unlikely(ret < 0)) {
+		net_err_ratelimited("%s: cannot queue packet to TX ring\n", peer->ovpn->dev->name);
+		goto drop;
+	}
+
+	if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
+		ovpn_peer_put(peer);
+
+	return;
+drop:
+	if (peer)
Quoted 8 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 137

Quoted 3 lines
drivers/net/ovpn/io.c +3 -0
+		ovpn_peer_put(peer);
+	kfree_skb_list(skb);
+}

So this puts packets on a per-peer 1024-packet FIFO queue with no backpressure? That sounds like a pretty terrible bufferbloat situation. Did you do any kind of latency-under-load testing of this, such as running the RRUL test[0] through it?

Thanks for pointing this out.

Andrew Lunn just raised a similar point about these rings being potential bufferbloat pitfalls.

And I totally agree.

I haven't performed any specific test, but I have already seen latency bumping here and there under heavy load.

Andrew suggested at least reducing rings size to something like 128 and then looking at BQL.

Do you have any hint as to what may make sense for a first implementation, balancing complexity and good results?

Thanks a lot.

Regards,

Quoted 5 lines

-Toke

[0] https://flent.org/tests.html#the-realtime-response-under-load-rrul-test

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#55 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

Quoted 46 lines · Antonio Quartulli

Hi Toke,

Quoted 27 lines · Toke Høiland-Jørgensen
Quoted 25 lines · Antonio Quartulli
drivers/net/ovpn/io.c +25 -0
+/* send skb to connected peer, if any */
+static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
+{
+	int ret;
+
+	if (likely(!peer))
+		/* retrieve peer serving the destination IP of this packet */
+		peer = ovpn_peer_lookup_by_dst(ovpn, skb);
+	if (unlikely(!peer)) {
+		net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
+		goto drop;
+	}
+
+	ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
+	if (unlikely(ret < 0)) {
+		net_err_ratelimited("%s: cannot queue packet to TX ring\n", peer->ovpn->dev->name);
+		goto drop;
+	}
+
+	if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
+		ovpn_peer_put(peer);
+
+	return;
+drop:
+	if (peer)
Quoted 8 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 137

Quoted 3 lines
drivers/net/ovpn/io.c +3 -0
+		ovpn_peer_put(peer);
+	kfree_skb_list(skb);
+}

So this puts packets on a per-peer 1024-packet FIFO queue with no backpressure? That sounds like a pretty terrible bufferbloat situation. Did you do any kind of latency-under-load testing of this, such as running the RRUL test[0] through it?

Thanks for pointing this out.

Andrew Lunn just raised a similar point about these rings being potential bufferbloat pitfalls.

And I totally agree.

Quoted 8 lines

I haven't performed any specific test, but I have already seen latency bumping here and there under heavy load.

Andrew suggested at least reducing rings size to something like 128 and then looking at BQL.

Do you have any hint as to what may make sense for a first implementation, balancing complexity and good results?

Hmm, I think BQL may actually be fairly straight forward to implement for this; if you just call netdev_tx_sent_queue() when the packet has been encrypted and sent on to the lower layer, the BQL algorithm should keep the ring buffer occupancy just at the level it needs to be to keep the encryption worker busy. I am not sure if there is some weird reason this won't work for something like this, but I can't think of any off the top of my head. And implementing this should be fairly simple (it's just a couple of function calls in the right places). As an example, see this commit adding it to the mvneta driver:

a29b6235560a ("net: mvneta: add BQL support")

Not sure if some additional mechanism is needed to keep a bunch of encrypted packets from piling up in the physical device qdisc (after encryption), but that will be in addition, in that case.

-Toke

author maintainer
#56 lore raw

Re: [PATCH net-next v2 08/22] ovpn: implement basic TX path (UDP)

Quoted 67 lines · Toke Høiland-Jørgensen
Quoted 46 lines · Antonio Quartulli

Hi Toke,

Quoted 27 lines · Toke Høiland-Jørgensen
Quoted 25 lines · Antonio Quartulli
drivers/net/ovpn/io.c +25 -0
+/* send skb to connected peer, if any */
+static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
+{
+	int ret;
+
+	if (likely(!peer))
+		/* retrieve peer serving the destination IP of this packet */
+		peer = ovpn_peer_lookup_by_dst(ovpn, skb);
+	if (unlikely(!peer)) {
+		net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
+		goto drop;
+	}
+
+	ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
+	if (unlikely(ret < 0)) {
+		net_err_ratelimited("%s: cannot queue packet to TX ring\n", peer->ovpn->dev->name);
+		goto drop;
+	}
+
+	if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
+		ovpn_peer_put(peer);
+
+	return;
+drop:
+	if (peer)
Quoted 8 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 137

Quoted 3 lines
drivers/net/ovpn/io.c +3 -0
+		ovpn_peer_put(peer);
+	kfree_skb_list(skb);
+}

So this puts packets on a per-peer 1024-packet FIFO queue with no backpressure? That sounds like a pretty terrible bufferbloat situation. Did you do any kind of latency-under-load testing of this, such as running the RRUL test[0] through it?

Thanks for pointing this out.

Andrew Lunn just raised a similar point about these rings being potential bufferbloat pitfalls.

And I totally agree.

Quoted 8 lines

I haven't performed any specific test, but I have already seen latency bumping here and there under heavy load.

Andrew suggested at least reducing rings size to something like 128 and then looking at BQL.

Do you have any hint as to what may make sense for a first implementation, balancing complexity and good results?

Hmm, I think BQL may actually be fairly straight forward to implement for this; if you just call netdev_tx_sent_queue() when the packet has been encrypted and sent on to the lower layer, the BQL algorithm should keep the ring buffer occupancy just at the level it needs to be to keep the encryption worker busy. I am not sure if there is some weird reason this won't work for something like this, but I can't think of any off the top of my head. And implementing this should be fairly simple (it's just a couple of function calls in the right places). As an example, see this commit adding it to the mvneta driver:

Quoted 5 lines

a29b6235560a ("net: mvneta: add BQL support")

Not sure if some additional mechanism is needed to keep a bunch of encrypted packets from piling up in the physical device qdisc (after encryption), but that will be in addition, in that case.

Thank you very much - really appreciated.

I will look into the mentioned commit and will try to implement this logic in the next patchset iteration.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#57 lore raw

PATCH v2 9/22 ovpn: implement basic RX path (UDP)

In reply to Antonio Quartulli

Packets received over the socket are forwarded to the user device.

Impementation 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.

---
 drivers/net/ovpn/io.c     | 107 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/io.h     |   6 ++
 drivers/net/ovpn/peer.c   |   9 +++
 drivers/net/ovpn/peer.h   |   2 +
 drivers/net/ovpn/proto.h  | 101 ++++++++++++++++++++++++++++++++
 drivers/net/ovpn/socket.c |  24 ++++++++
 drivers/net/ovpn/udp.c    | 118 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/udp.h    |   1 +
 8 files changed, 368 insertions(+)
 create mode 100644 drivers/net/ovpn/proto.h
drivers/net/ovpn/io.c +107 -0
index d2fdb7485023..59a6e532bb8b 100644
@@ -53,6 +53,113 @@ int ovpn_struct_init(struct net_device *dev)
 	return 0;
 }
 
+/* Called after decrypt to write IP packet to tun netdev.
+ * This method is expected to manage/free skb.
+ */
+static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	/* packet integrity was verified on the VPN layer - no need to perform
+	 * any additional check along the stack
+	 */
+	skb->ip_summed = CHECKSUM_UNNECESSARY;
+	skb->csum_level = ~0;
+
+	/* skb hash for transport packet no longer valid after decapsulation */
+	skb_clear_hash(skb);
+
+	/* post-decrypt scrub -- prepare to inject encapsulated packet onto tun
+	 * interface, based on __skb_tunnel_rx() in dst.h
+	 */
+	skb->dev = 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);
+
+	/* update per-cpu RX stats with the stored size of encrypted packet */
+
+	/* we are in softirq context - hence no locking nor disable preemption needed */
+	dev_sw_netstats_rx_add(peer->ovpn->dev, skb->len);
+
+	/* cause packet to be "received" by tun interface */
+	napi_gro_receive(&peer->napi, skb);
+}
+
+int ovpn_napi_poll(struct napi_struct *napi, int budget)
+{
+	struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi);
+	struct sk_buff *skb;
+	int work_done = 0;
+
+	if (unlikely(budget <= 0))
+		return 0;
+	/* this function should schedule at most 'budget' number of
+	 * packets for delivery to the tun interface.
+	 * If in the queue we have more packets than what allowed by the
+	 * budget, the next polling will take care of those
+	 */
+	while ((work_done < budget) &&
+	       (skb = ptr_ring_consume_bh(&peer->netif_rx_ring))) {
+		ovpn_netdev_write(peer, skb);
+		work_done++;
+	}
+
+	if (work_done < budget)
+		napi_complete_done(napi, work_done);
+
+	return work_done;
+}
+
+/* Entry point for processing an incoming packet (in skb form)
+ *
+ * Enqueue the packet and schedule RX consumer.
+ * Reference to peer is dropped only in case of success.
+ *
+ * Return 0  if the packet was handled (and consumed)
+ * Return <0 in case of error (return value is error code)
+ */
+int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	if (unlikely(ptr_ring_produce_bh(&peer->rx_ring, skb) < 0))
+		return -ENOSPC;
+
+	if (!queue_work(ovpn->crypto_wq, &peer->decrypt_work))
+		ovpn_peer_put(peer);
+
+	return 0;
+}
+
+static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	return true;
+}
+
+/* pick packet from RX queue, decrypt and forward it to the tun device */
+void ovpn_decrypt_work(struct work_struct *work)
+{
+	struct ovpn_peer *peer;
+	struct sk_buff *skb;
+
+	peer = container_of(work, struct ovpn_peer, decrypt_work);
+	while ((skb = ptr_ring_consume_bh(&peer->rx_ring))) {
+		if (likely(ovpn_decrypt_one(peer, skb) == 0)) {
+			/* if a packet has been enqueued for NAPI, signal
+			 * availability to the networking stack
+			 */
+			local_bh_disable();
+			napi_schedule(&peer->napi);
+			local_bh_enable();
+		}
+
+		/* give a chance to be rescheduled if needed */
+		cond_resched();
+	}
+	ovpn_peer_put(peer);
+}
+
 static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 {
 	return true;
drivers/net/ovpn/io.h +6 -0
index 633b9fb3276c..dee121a36dd7 100644
@@ -13,10 +13,16 @@
 #include <linux/netdevice.h>
 
 struct sk_buff;
+struct ovpn_peer;
+struct ovpn_struct;
 
 int ovpn_struct_init(struct net_device *dev);
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
+int ovpn_napi_poll(struct napi_struct *napi, int budget);
+
+int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, struct sk_buff *skb);
 
 void ovpn_encrypt_work(struct work_struct *work);
+void ovpn_decrypt_work(struct work_struct *work);
 
 #endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/peer.c +9 -0
index 4dbbd25b25c9..1ca59746ec69 100644
@@ -45,6 +45,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)
 	kref_init(&peer->refcount);
 
 	INIT_WORK(&peer->encrypt_work, ovpn_encrypt_work);
+	INIT_WORK(&peer->decrypt_work, ovpn_decrypt_work);
 
 	ret = dst_cache_init(&peer->dst_cache, GFP_KERNEL);
 	if (ret < 0) {
@@ -70,6 +71,11 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)
 		goto err_rx_ring;
 	}
 
+	/* configure and start NAPI */
+	netif_napi_add_tx_weight(ovpn->dev, &peer->napi, ovpn_napi_poll,
+				 NAPI_POLL_WEIGHT);
+	napi_enable(&peer->napi);
+
 	dev_hold(ovpn->dev);
 
 	return peer;
@@ -114,6 +120,9 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)
 
 void ovpn_peer_release(struct ovpn_peer *peer)
 {
+	napi_disable(&peer->napi);
+	netif_napi_del(&peer->napi);
+
 	if (peer->sock)
 		ovpn_socket_put(peer->sock);
 
drivers/net/ovpn/peer.h +2 -0
index ef4174be7dea..8279af12dd09 100644
@@ -38,6 +38,8 @@ struct ovpn_peer {
 	struct ptr_ring rx_ring;
 	struct ptr_ring netif_rx_ring;
 
+	struct napi_struct napi;
+
 	struct ovpn_socket *sock;
 
 	struct dst_cache dst_cache;
drivers/net/ovpn/proto.h new file +101 -0

101 lines not on this page · raw message

drivers/net/ovpn/socket.c +24 -0
index b7ccc4882d1e..203d04825d88 100644
@@ -19,6 +19,9 @@ static void ovpn_socket_detach(struct socket *sock)
 	if (!sock)
 		return;
 
+	if (sock->sk->sk_protocol == IPPROTO_UDP)
+		ovpn_udp_socket_detach(sock);
+
 	sockfd_put(sock);
 }
 
@@ -64,6 +67,27 @@ static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)
 	return ret;
 }
 
+/* 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) != UDP_ENCAP_OVPNINUDP))
+		return NULL;
+
+	ovpn_sock = 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 != ovpn_sock->sock->sk))
+		return NULL;
+
+	return ovpn_sock->ovpn;
+}
+
 struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
 {
 	struct ovpn_socket *ovpn_sock;
drivers/net/ovpn/udp.c +118 -0

141 lines not on this page · raw message

drivers/net/ovpn/udp.h +1 -0
index 4d6ba9ecabd1..8e3598daed17 100644
@@ -16,6 +16,7 @@ struct ovpn_peer;
 struct ovpn_struct;
 
 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);
 
Signature · 1 line
2.43.0
reviewer
#58 lore raw

Re: [PATCH net-next v2 09/22] ovpn: implement basic RX path (UDP)

Quoted 3 lines · Antonio Quartulli

Packets received over the socket are forwarded to the user device.

Impementation is UDP only. TCP will be added by a later patch.

nit: Implementation

Quoted 5 lines

Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing.

...

Quoted 43 lines
drivers/net/ovpn/proto.h new file +37 -0
new file mode 100644
index 000000000000..c016422fe6f3
@@ -0,0 +1,101 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ *		James Yonan <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPNPROTO_H_
+#define _NET_OVPN_OVPNPROTO_H_
+
+#include "main.h"
+
+#include <linux/skbuff.h>
+
+/* 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

drivers/net/ovpn/proto.h line 47

Quoted 10 lines
drivers/net/ovpn/proto.h new file +10 -0
+/* first byte of keepalive message */
+#define OVPN_KEEPALIVE_FIRST_BYTE 0x2a
+/* first byte of exit message */
+#define OVPN_EXPLICIT_EXIT_NOTIFY_FIRST_BYTE 0x28
+
+/**
+ * Extract the OP code from the specified byte
+ *
+ * Return the OP code
+ */

nit: '/**' denotes the start of a Kernel doc, however,
other than that syntax this is not a Kernel doc.

Likewise below.

Flagged by /scripts/kernel-doc -none

Quoted 54 lines
drivers/net/ovpn/proto.h new file +54 -0
+static inline u8 ovpn_opcode_from_byte(u8 byte)
+{
+	return byte >> OVPN_OPCODE_SHIFT;
+}
+
+/**
+ * Extract the OP code from the skb head.
+ *
+ * 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 offset)
+{
+	return ovpn_opcode_from_byte(*(skb->data + offset));
+}
+
+/**
+ * Extract the key ID from the skb head.
+ *
+ * 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;
+}
+
+/**
+ * Extract the peer ID from the skb head.
+ *
+ * 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 offset)
+{
+	return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK;
+}
+
+static inline u32 ovpn_opcode_compose(u8 opcode, u8 key_id, u32 peer_id)
+{
+	const u8 op = (opcode << OVPN_OPCODE_SHIFT) | (key_id & OVPN_KEY_ID_MASK);
+
+	return (op << 24) | (peer_id & OVPN_PEER_ID_MASK);
+}
+
+#endif /* _NET_OVPN_OVPNPROTO_H_ */

...

author maintainer
#59 lore raw

Re: [PATCH net-next v2 09/22] ovpn: implement basic RX path (UDP)

Quoted 2 lines · Simon Horman

Flagged by /scripts/kernel-doc -none

Thanks again.
Will have a look at all issues reported by /scripts/kernel-doc.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#60 lore raw

Re: [PATCH net-next v2 09/22] ovpn: implement basic RX path (UDP)

In reply to Antonio Quartulli

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57

Quoted 7 lines

+++ b/drivers/net/ovpn/io.c

drivers/net/ovpn/io.c +2 -0
@@ -53,6 +53,113 @@ int ovpn_struct_init(struct net_device *dev)
 	return 0;
 }
 
+/* Called after decrypt to write IP packet to tun netdev.
+ * This method is expected to manage/free skb.

tun?

The userspace implementation uses tun, but being in the kernel, don't you just pass it to the stack?

Quoted 33 lines
drivers/net/ovpn/io.c +33 -0
+ */
+static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	/* packet integrity was verified on the VPN layer - no need to perform
+	 * any additional check along the stack
+	 */
+	skb->ip_summed = CHECKSUM_UNNECESSARY;
+	skb->csum_level = ~0;
+
+	/* skb hash for transport packet no longer valid after decapsulation */
+	skb_clear_hash(skb);
+
+	/* post-decrypt scrub -- prepare to inject encapsulated packet onto tun
+	 * interface, based on __skb_tunnel_rx() in dst.h
+	 */
+	skb->dev = 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);
+
+	/* update per-cpu RX stats with the stored size of encrypted packet */
+
+	/* we are in softirq context - hence no locking nor disable preemption needed */
+	dev_sw_netstats_rx_add(peer->ovpn->dev, skb->len);
+
+	/* cause packet to be "received" by tun interface */
+	napi_gro_receive(&peer->napi, skb);
+}
+

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 100

Quoted 10 lines
drivers/net/ovpn/io.c +10 -0
+int ovpn_napi_poll(struct napi_struct *napi, int budget)
+{
+	struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi);
+	struct sk_buff *skb;
+	int work_done = 0;
+
+	if (unlikely(budget <= 0))
+		return 0;
+	/* this function should schedule at most 'budget' number of
+	 * packets for delivery to the tun interface.

More tun. Did you copy code from the tun driver?

Andrew

author maintainer
#61 lore raw

Re: [PATCH net-next v2 09/22] ovpn: implement basic RX path (UDP)

Quoted 5 lines · Andrew Lunn
Quoted 5 lines

+++ b/drivers/net/ovpn/io.c

drivers/net/ovpn/io.c +0 -0
@@ -53,6 +53,113 @@ int ovpn_struct_init(struct net_device *dev)
  	return 0;
  }
 
Quoted 7 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57

Quoted 2 lines
drivers/net/ovpn/io.c +2 -0
+/* Called after decrypt to write IP packet to tun netdev.
+ * This method is expected to manage/free skb.

tun?

The userspace implementation uses tun, but being in the kernel, don't you just pass it to the stack?

Sorry - this is just me being used to refer to the tunnel interface as "tun".

Whenever you read "tun", I actually refer to the interface created by ovpn. This is totally unrelated to the tun module.

But I agree that this doesn't make sense for anybody other than me.

I will get rid of any word "tun" in the comments/doc.

Quoted 38 lines
Quoted 38 lines
drivers/net/ovpn/io.c +37 -0
+ */
+static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	/* packet integrity was verified on the VPN layer - no need to perform
+	 * any additional check along the stack
+	 */
+	skb->ip_summed = CHECKSUM_UNNECESSARY;
+	skb->csum_level = ~0;
+
+	/* skb hash for transport packet no longer valid after decapsulation */
+	skb_clear_hash(skb);
+
+	/* post-decrypt scrub -- prepare to inject encapsulated packet onto tun
+	 * interface, based on __skb_tunnel_rx() in dst.h
+	 */
+	skb->dev = 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);
+
+	/* update per-cpu RX stats with the stored size of encrypted packet */
+
+	/* we are in softirq context - hence no locking nor disable preemption needed */
+	dev_sw_netstats_rx_add(peer->ovpn->dev, skb->len);
+
+	/* cause packet to be "received" by tun interface */
+	napi_gro_receive(&peer->napi, skb);
+}
+
+int ovpn_napi_poll(struct napi_struct *napi, int budget)
+{
+	struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi);
+	struct sk_buff *skb;
Quoted 8 lines

drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 100

Quoted 6 lines
drivers/net/ovpn/io.c +6 -0
+	int work_done = 0;
+
+	if (unlikely(budget <= 0))
+		return 0;
+	/* this function should schedule at most 'budget' number of
+	 * packets for delivery to the tun interface.

More tun. Did you copy code from the tun driver?

Same as above.

Thanks for nothing those.

Regards,

Quoted 2 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#62 lore raw

PATCH v2 10/22 ovpn: implement packet processing

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/Makefile      |   3 +
 drivers/net/ovpn/crypto.c      | 154 ++++++++++++++
 drivers/net/ovpn/crypto.h      | 144 +++++++++++++
 drivers/net/ovpn/crypto_aead.c | 366 +++++++++++++++++++++++++++++++++
 drivers/net/ovpn/crypto_aead.h |  27 +++
 drivers/net/ovpn/io.c          | 141 +++++++++++--
 drivers/net/ovpn/packet.h      |   2 +
 drivers/net/ovpn/peer.c        |  21 ++
 drivers/net/ovpn/peer.h        |   3 +
 drivers/net/ovpn/pktid.c       | 126 ++++++++++++
 drivers/net/ovpn/pktid.h       |  90 ++++++++
 11 files changed, 1056 insertions(+), 21 deletions(-)
 create mode 100644 drivers/net/ovpn/crypto.c
 create mode 100644 drivers/net/ovpn/crypto.h
 create mode 100644 drivers/net/ovpn/crypto_aead.c
 create mode 100644 drivers/net/ovpn/crypto_aead.h
 create mode 100644 drivers/net/ovpn/pktid.c
 create mode 100644 drivers/net/ovpn/pktid.h
drivers/net/ovpn/Makefile +3 -0
index f768cd95b777..eaa16c24573c 100644
@@ -8,9 +8,12 @@
 
 obj-$(CONFIG_OVPN) += ovpn.o
 ovpn-y += bind.o
+ovpn-y += crypto.o
+ovpn-y += crypto_aead.o
 ovpn-y += main.o
 ovpn-y += io.o
 ovpn-y += netlink.o
 ovpn-y += peer.o
+ovpn-y += pktid.o
 ovpn-y += socket.o
 ovpn-y += udp.o
drivers/net/ovpn/crypto.c new file +154 -0

154 lines not on this page · raw message

drivers/net/ovpn/crypto.h new file +144 -0

144 lines not on this page · raw message

drivers/net/ovpn/crypto_aead.c new file +366 -0

366 lines not on this page · raw message

drivers/net/ovpn/crypto_aead.h new file +27 -0
new file mode 100644
index 000000000000..ba5e3f86df83
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_OVPNAEAD_H_
+#define _NET_OVPN_OVPNAEAD_H_
+
+#include "crypto.h"
+
+#include <asm/types.h>
+#include <linux/skbuff.h>
+
+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_crypto_key_slot *ks, struct sk_buff *skb, u32 peer_id);
+int ovpn_aead_decrypt(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_ */
drivers/net/ovpn/io.c +120 -21

175 lines not on this page · raw message

drivers/net/ovpn/packet.h +2 -0
index 0c6c6165c63c..344a4f24c493 100644
@@ -10,6 +10,8 @@
 #ifndef _NET_OVPN_PACKET_H_
 #define _NET_OVPN_PACKET_H_
 
+#include <linux/types.h>
+
 /* When the OpenVPN protocol is run in AEAD mode, use
  * the OpenVPN packet ID as the AEAD nonce:
  *
drivers/net/ovpn/peer.c +21 -0
index 1ca59746ec69..01b994801393 100644
@@ -8,6 +8,7 @@
  */
 
 #include "bind.h"
+#include "crypto.h"
 #include "io.h"
 #include "main.h"
 #include "netlink.h"
@@ -41,6 +42,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)
 	peer->vpn_addrs.ipv6 = in6addr_any;
 
 	RCU_INIT_POINTER(peer->bind, NULL);
+	ovpn_crypto_state_init(&peer->crypto);
 	spin_lock_init(&peer->lock);
 	kref_init(&peer->refcount);
 
@@ -115,6 +117,7 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)
 {
 	struct ovpn_peer *peer = container_of(head, struct ovpn_peer, rcu);
 
+	ovpn_crypto_state_release(&peer->crypto);
 	ovpn_peer_free(peer);
 }
 
@@ -180,6 +183,24 @@ struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_bu
 	return peer;
 }
 
+struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb)
+{
+	struct ovpn_peer *tmp, *peer = NULL;
+
+	/* in P2P mode, no matter the destination, packets are always sent to the single peer
+	 * listening on the other side
+	 */
+	if (ovpn->mode == OVPN_MODE_P2P) {
+		rcu_read_lock();
+		tmp = rcu_dereference(ovpn->peer);
+		if (likely(tmp && ovpn_peer_hold(tmp)))
+			peer = tmp;
+		rcu_read_unlock();
+	}
+
+	return peer;
+}
+
 static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, struct sockaddr_storage *ss)
 {
 	struct sockaddr_in6 *sa6;
drivers/net/ovpn/peer.h +3 -0
index 8279af12dd09..03793f68a62b 100644
@@ -11,6 +11,7 @@
 #define _NET_OVPN_OVPNPEER_H_
 
 #include "bind.h"
+#include "crypto.h"
 #include "socket.h"
 
 #include <linux/ptr_ring.h>
@@ -42,6 +43,8 @@ struct ovpn_peer {
 
 	struct ovpn_socket *sock;
 
+	struct ovpn_crypto_state crypto;
+
 	struct dst_cache dst_cache;
 
 	/* our binding to peer, protected by spinlock */
drivers/net/ovpn/pktid.c new file +126 -0

126 lines not on this page · raw message

drivers/net/ovpn/pktid.h new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#63 lore raw

PATCH v2 11/22 ovpn: store tunnel and transport statistics

In reply to Antonio Quartulli

Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.

To be exported via netlink.

---
 drivers/net/ovpn/Makefile |  1 +
 drivers/net/ovpn/io.c     |  8 ++++++
 drivers/net/ovpn/peer.c   |  2 ++
 drivers/net/ovpn/peer.h   |  7 ++++++
 drivers/net/ovpn/stats.c  | 21 ++++++++++++++++
 drivers/net/ovpn/stats.h  | 51 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 90 insertions(+)
 create mode 100644 drivers/net/ovpn/stats.c
 create mode 100644 drivers/net/ovpn/stats.h
drivers/net/ovpn/Makefile +1 -0
index eaa16c24573c..7eaee71bbe9f 100644
@@ -16,4 +16,5 @@ ovpn-y += netlink.o
 ovpn-y += peer.o
 ovpn-y += pktid.o
 ovpn-y += socket.o
+ovpn-y += stats.o
 ovpn-y += udp.o
drivers/net/ovpn/io.c +8 -0
index 6c469fedb89a..e6c99e08cf41 100644
@@ -162,6 +162,8 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 	int ret = -1;
 	u8 key_id;
 
+	ovpn_peer_stats_increment_rx(&peer->link_stats, skb->len);
+
 	/* get the key slot matching the key Id in the received packet */
 	key_id = ovpn_key_id_from_skb(skb);
 	ks = ovpn_crypto_key_id_to_slot(&peer->crypto, key_id);
@@ -182,6 +184,9 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 		goto drop;
 	}
 
+	/* increment RX stats */
+	ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len);
+
 	/* check if this is a valid datapacket that has to be delivered to the
 	 * tun interface
 	 */
@@ -268,6 +273,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 		goto err;
 	}
 
+	ovpn_peer_stats_increment_tx(&peer->vpn_stats, skb->len);
+
 	/* encrypt */
 	ret = ovpn_aead_encrypt(ks, skb, peer->id);
 	if (unlikely(ret < 0)) {
@@ -278,6 +285,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 
 	success = true;
 
+	ovpn_peer_stats_increment_tx(&peer->link_stats, skb->len);
 err:
 	ovpn_crypto_key_slot_put(ks);
 	return success;
drivers/net/ovpn/peer.c +2 -0
index 01b994801393..658d922d4ae8 100644
@@ -45,6 +45,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);
 
 	INIT_WORK(&peer->encrypt_work, ovpn_encrypt_work);
 	INIT_WORK(&peer->decrypt_work, ovpn_decrypt_work);
drivers/net/ovpn/peer.h +7 -0
index 03793f68a62b..788ec933fc00 100644
@@ -13,6 +13,7 @@
 #include "bind.h"
 #include "crypto.h"
 #include "socket.h"
+#include "stats.h"
 
 #include <linux/ptr_ring.h>
 #include <net/dst_cache.h>
@@ -53,6 +54,12 @@ struct ovpn_peer {
 	/* true if ovpn_peer_mark_delete was called */
 	bool halt;
 
+	/* per-peer in-VPN rx/tx stats */
+	struct ovpn_peer_stats vpn_stats;
+
+	/* per-peer link/transport rx/tx stats */
+	struct ovpn_peer_stats link_stats;
+
 	/* why peer was deleted - keepalive timeout, module removed etc */
 	enum ovpn_del_peer_reason delete_reason;
 
drivers/net/ovpn/stats.c new file +21 -0
new file mode 100644
index 000000000000..10ae70ed9cc9
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2020-2024 OpenVPN, Inc.
+ *
+ *  Author:	James Yonan <[email protected]>
+ *		Antonio Quartulli <[email protected]>
+ */
+
+#include "stats.h"
+
+#include <linux/atomic.h>
+
+void ovpn_peer_stats_init(struct ovpn_peer_stats *ps)
+{
+	atomic64_set(&ps->rx.bytes, 0);
+	atomic_set(&ps->rx.packets, 0);
+
+	atomic64_set(&ps->tx.bytes, 0);
+	atomic_set(&ps->tx.packets, 0);
+}
drivers/net/ovpn/stats.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#64 lore raw

PATCH v2 12/22 ovpn: implement TCP transport

In reply to Antonio Quartulli

With this changem ovpn is allowed to communicate to peers also via TCP.

---
 drivers/net/ovpn/Makefile |   1 +
 drivers/net/ovpn/io.c     |   5 +
 drivers/net/ovpn/peer.h   |  21 ++
 drivers/net/ovpn/socket.c |  18 ++
 drivers/net/ovpn/socket.h |  14 +-
 drivers/net/ovpn/tcp.c    | 474 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/tcp.h    |  41 ++++
 7 files changed, 572 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/ovpn/tcp.c
 create mode 100644 drivers/net/ovpn/tcp.h
drivers/net/ovpn/Makefile +1 -0
index 7eaee71bbe9f..336d35a31147 100644
@@ -17,4 +17,5 @@ ovpn-y += peer.o
 ovpn-y += pktid.o
 ovpn-y += socket.o
 ovpn-y += stats.o
+ovpn-y += tcp.o
 ovpn-y += udp.o
drivers/net/ovpn/io.c +5 -0
index e6c99e08cf41..28d05e42cc98 100644
@@ -14,6 +14,7 @@
 #include "netlink.h"
 #include "peer.h"
 #include "proto.h"
+#include "tcp.h"
 #include "udp.h"
 
 #include <linux/netdevice.h>
@@ -294,6 +295,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 /* Process packets in TX queue in a transport-specific way.
  *
  * UDP transport - encrypt and send across the tunnel.
+ * TCP transport - encrypt and put into TCP TX queue.
  */
 void ovpn_encrypt_work(struct work_struct *work)
 {
@@ -326,6 +328,9 @@ void ovpn_encrypt_work(struct work_struct *work)
 				case IPPROTO_UDP:
 					ovpn_udp_send_skb(peer->ovpn, peer, curr);
 					break;
+				case IPPROTO_TCP:
+					ovpn_tcp_send_skb(peer, curr);
+					break;
 				default:
 					/* no transport configured yet */
 					consume_skb(skb);
drivers/net/ovpn/peer.h +21 -0
index 788ec933fc00..1cae726bbbf5 100644
@@ -44,6 +44,27 @@ struct ovpn_peer {
 
 	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 ptr_ring tx_ring;
+		struct work_struct tx_work;
+		struct work_struct rx_work;
+
+		u8 raw_len[sizeof(u16)];
+		struct sk_buff *skb;
+		u16 offset;
+		u16 data_len;
+		struct {
+			void (*sk_state_change)(struct sock *sk);
+			void (*sk_data_ready)(struct sock *sk);
+			void (*sk_write_space)(struct sock *sk);
+			struct proto *prot;
+		} sk_cb;
+	} tcp;
+
+
 	struct ovpn_crypto_state crypto;
 
 	struct dst_cache dst_cache;
drivers/net/ovpn/socket.c +18 -0
index 203d04825d88..d0966992bd9d 100644
@@ -9,8 +9,10 @@
 
 #include "main.h"
 #include "io.h"
+#include "ovpnstruct.h"
 #include "peer.h"
 #include "socket.h"
+#include "tcp.h"
 #include "udp.h"
 
 /* Finalize release of socket, called after RCU grace period */
@@ -21,6 +23,8 @@ static void ovpn_socket_detach(struct socket *sock)
 
 	if (sock->sk->sk_protocol == IPPROTO_UDP)
 		ovpn_udp_socket_detach(sock);
+	else if (sock->sk->sk_protocol == IPPROTO_TCP)
+		ovpn_tcp_socket_detach(sock);
 
 	sockfd_put(sock);
 }
@@ -63,6 +67,8 @@ static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)
 
 	if (sock->sk->sk_protocol == IPPROTO_UDP)
 		ret = ovpn_udp_socket_attach(sock, peer->ovpn);
+	else if (sock->sk->sk_protocol == IPPROTO_TCP)
+		ret = ovpn_tcp_socket_attach(sock, peer);
 
 	return ret;
 }
@@ -116,6 +122,18 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
 	ovpn_sock->sock = sock;
 	kref_init(&ovpn_sock->refcount);
 
+	/* TCP sockets are per-peer, therefore they are linked to their unique peer */
+	if (sock->sk->sk_protocol == IPPROTO_TCP) {
+		ovpn_sock->peer = peer;
+		ret = ptr_ring_init(&ovpn_sock->recv_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
+		if (ret < 0) {
+			netdev_err(peer->ovpn->dev, "%s: cannot allocate TCP recv ring\n",
+				   __func__);
+			kfree(ovpn_sock);
+			return ERR_PTR(ret);
+		}
+	}
+
 	rcu_assign_sk_user_data(sock->sk, ovpn_sock);
 
 	return ovpn_sock;
drivers/net/ovpn/socket.h +12 -2
index 92c50f795f7c..3d6e5414d980 100644
@@ -23,8 +23,18 @@ struct ovpn_peer;
  * struct ovpn_socket - a kernel socket referenced in the ovpn code
  */
 struct ovpn_socket {
-	/* the VPN session object owning this socket (UDP only) */
-	struct ovpn_struct *ovpn;
+	union {
+		/* the VPN session object owning this socket (UDP only) */
+		struct ovpn_struct *ovpn;
+
+		/* TCP only */
+		struct {
+			/** @peer: the unique peer transmitting over this socket (TCP only) */
+			struct ovpn_peer *peer;
+			struct ptr_ring recv_ring;
+		};
+	};
+
 	/* the kernel socket */
 	struct socket *sock;
 	/* amount of contexts currently referencing this object */
drivers/net/ovpn/tcp.c new file +474 -0

474 lines not on this page · raw message

drivers/net/ovpn/tcp.h new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#65 lore raw

Re: [PATCH net-next v2 12/22] ovpn: implement TCP transport

Quoted 3 lines · Antonio Quartulli

With this changem ovpn is allowed to communicate to peers also via TCP.

...

Quoted 10 lines
drivers/net/ovpn/tcp.c new file +4 -0
new file mode 100644
index 000000000000..d810929bc470
@@ -0,0 +1,474 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.

drivers/net/ovpn/tcp.c line 14

Quoted 10 lines
drivers/net/ovpn/tcp.c new file +10 -0
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ */
+
+#include "main.h"
+#include "ovpnstruct.h"
+#include "io.h"
+#include "peer.h"
+#include "proto.h"
+#include "skb.h"

Hi Antonio,

this breaks bisection because skb.h doesn't exist until the following patch in this series.

Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6 -0
+#include "tcp.h"
+
+#include <linux/ptr_ring.h>
+#include <linux/skbuff.h>
+#include <net/tcp.h>
+#include <net/route.h>

...

author maintainer
#66 lore raw

Re: [PATCH net-next v2 12/22] ovpn: implement TCP transport

Quoted 30 lines · Simon Horman
Quoted 3 lines · Antonio Quartulli

With this changem ovpn is allowed to communicate to peers also via TCP.

...

Quoted 10 lines
drivers/net/ovpn/tcp.c new file +4 -0
new file mode 100644
index 000000000000..d810929bc470
@@ -0,0 +1,474 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2019-2024 OpenVPN, Inc.

drivers/net/ovpn/tcp.c line 14

Quoted 10 lines
drivers/net/ovpn/tcp.c new file +10 -0
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ */
+
+#include "main.h"
+#include "ovpnstruct.h"
+#include "io.h"
+#include "peer.h"
+#include "proto.h"
+#include "skb.h"

Hi Antonio,

Quoted 2 lines

this breaks bisection because skb.h doesn't exist until the following patch in this series.

I must have overlooked this - I normally check that every single patch does not break.

Will fix it, although the whole ovpn code may just be merged as a single patch at the end.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#67 lore raw

PATCH v2 13/22 ovpn: implement multi-peer support

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/io.c         |   1 +
 drivers/net/ovpn/ovpnstruct.h |   9 +++
 drivers/net/ovpn/peer.c       | 124 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h       |   7 ++
 drivers/net/ovpn/skb.h        |  51 ++++++++++++++
 5 files changed, 192 insertions(+)
 create mode 100644 drivers/net/ovpn/skb.h
drivers/net/ovpn/io.c +1 -0
index 28d05e42cc98..91706c55784d 100644
@@ -36,6 +36,7 @@ int ovpn_struct_init(struct net_device *dev)
 		return err;
 
 	spin_lock_init(&ovpn->lock);
+	spin_lock_init(&ovpn->peers.lock);
 
 	ovpn->crypto_wq = alloc_workqueue("ovpn-crypto-wq-%s",
 					  WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
drivers/net/ovpn/ovpnstruct.h +9 -0
index 300906bc694f..63ac1f0e7afe 100644
@@ -36,6 +36,15 @@ struct ovpn_struct {
 	 */
 	struct workqueue_struct *events_wq;
 
+	/* list of known peers */
+	struct {
+		DECLARE_HASHTABLE(by_id, 12);
+		DECLARE_HASHTABLE(by_transp_addr, 12);
+		DECLARE_HASHTABLE(by_vpn_addr, 12);
+		/* protects write access to any of the hashtables above */
+		spinlock_t lock;
+	} peers;
+
 	/* for p2p mode */
 	struct ovpn_peer __rcu *peer;
 
drivers/net/ovpn/peer.c +124 -0

151 lines not on this page · raw message

drivers/net/ovpn/peer.h +7 -0
index 1cae726bbbf5..72bbe8ffd252 100644
@@ -30,6 +30,11 @@ struct ovpn_peer {
 		struct in6_addr ipv6;
 	} vpn_addrs;
 
+	struct hlist_node hash_entry_id;
+	struct hlist_node hash_entry_addr4;
+	struct hlist_node hash_entry_addr6;
+	struct hlist_node hash_entry_transp_addr;
+
 	/* work objects to handle encryption/decryption of packets.
 	 * these works are queued on the ovpn->crypt_wq workqueue.
 	 */
@@ -127,4 +132,6 @@ struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);
 int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, const struct sockaddr_storage *ss,
 			     const u8 *local_ip);
 
+void ovpn_peers_free(struct ovpn_struct *ovpn);
+
 #endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/skb.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#68 lore raw

PATCH v2 14/22 ovpn: implement peer lookup logic

In reply to Antonio Quartulli

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 tranport 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.

---
 drivers/net/ovpn/peer.c | 283 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 259 insertions(+), 24 deletions(-)
drivers/net/ovpn/peer.c +259 -24
index 6c2c6877d539..acef27b1ca5d 100644
@@ -153,6 +153,69 @@ void ovpn_peer_release_kref(struct kref *kref)
 	queue_work(peer->ovpn->events_wq, &peer->delete_work);
 }
 
+static __be32 ovpn_nexthop_from_rt4(struct sk_buff *skb)
+{
+	struct rtable *rt = skb_rtable(skb);
+
+	if (rt && rt->rt_uses_gateway)
+		return rt->rt_gw4;
+
+	return ip_hdr(skb)->daddr;
+}
+
+static struct in6_addr ovpn_nexthop_from_rt6(struct sk_buff *skb)
+{
+	struct rt6_info *rt = (struct rt6_info *)skb_rtable(skb);
+
+	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
+		return ipv6_hdr(skb)->daddr;
+
+	return rt->rt6i_gateway;
+}
+
+static struct ovpn_peer *ovpn_peer_lookup_vpn_addr4(struct hlist_head *head, __be32 *addr)
+{
+	struct ovpn_peer *tmp, *peer = NULL;
+
+	rcu_read_lock();
+	hlist_for_each_entry_rcu(tmp, head, hash_entry_addr4) {
+		if (*addr != tmp->vpn_addrs.ipv4.s_addr)
+			continue;
+
+		if (!ovpn_peer_hold(tmp))
+			continue;
+
+		peer = tmp;
+		break;
+	}
+	rcu_read_unlock();
+
+	return peer;
+}
+
+static struct ovpn_peer *ovpn_peer_lookup_vpn_addr6(struct hlist_head *head, struct in6_addr *addr)
+{
+	struct ovpn_peer *tmp, *peer = NULL;
+	int i;
+
+	rcu_read_lock();
+	hlist_for_each_entry_rcu(tmp, head, hash_entry_addr6) {
+		for (i = 0; i < 4; i++) {
+			if (addr->s6_addr32[i] != tmp->vpn_addrs.ipv6.s6_addr32[i])
+				continue;
+		}
+
+		if (!ovpn_peer_hold(tmp))
+			continue;
+
+		peer = tmp;
+		break;
+	}
+	rcu_read_unlock();
+
+	return peer;
+}
+
 /**
  * ovpn_peer_lookup_by_dst() - Lookup peer to send skb to
  *
@@ -170,6 +233,11 @@ void ovpn_peer_release_kref(struct kref *kref)
 struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb)
 {
 	struct ovpn_peer *tmp, *peer = NULL;
+	struct hlist_head *head;
+	sa_family_t sa_fam;
+	struct in6_addr addr6;
+	__be32 addr4;
+	u32 index;
 
 	/* in P2P mode, no matter the destination, packets are always sent to the single peer
 	 * listening on the other side
@@ -180,14 +248,111 @@ struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_bu
 		if (likely(tmp && ovpn_peer_hold(tmp)))
 			peer = tmp;
 		rcu_read_unlock();
+		return peer;
+	}
+
+	sa_fam = skb_protocol_to_family(skb);
+
+	switch (sa_fam) {
+	case AF_INET:
+		addr4 = ovpn_nexthop_from_rt4(skb);
+		index = ovpn_peer_index(ovpn->peers.by_vpn_addr, &addr4, sizeof(addr4));
+		head = &ovpn->peers.by_vpn_addr[index];
+
+		peer = ovpn_peer_lookup_vpn_addr4(head, &addr4);
+		break;
+	case AF_INET6:
+		addr6 = ovpn_nexthop_from_rt6(skb);
+		index = ovpn_peer_index(ovpn->peers.by_vpn_addr, &addr6, sizeof(addr6));
+		head = &ovpn->peers.by_vpn_addr[index];
+
+		peer = ovpn_peer_lookup_vpn_addr6(head, &addr6);
+		break;
 	}
 
 	return peer;
 }
 
+/**
+ * ovpn_nexthop_lookup4() - looks up the IP of the nexthop for the given destination
+ *
+ * Looks up in the IPv4 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up
+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */
+static __be32 ovpn_nexthop_lookup4(struct ovpn_struct *ovpn, __be32 src)
+{
+	struct rtable *rt;
+	struct flowi4 fl = {
+		.daddr = src
+	};
+
+	rt = ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI4\n", ovpn->dev->name, &src);
+		/* if we end up here this packet is probably going to be
+		 * thrown away later
+		 */
+		return src;
+	}
+
+	if (!rt->rt_uses_gateway)
+		goto out;
+
+	src = rt->rt_gw4;
+out:
+	return src;
+}
+
+/**
+ * ovpn_nexthop_lookup6() - looks up the IPv6 of the nexthop for the given destination
+ *
+ * Looks up in the IPv6 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up
+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */
+static struct in6_addr ovpn_nexthop_lookup6(struct ovpn_struct *ovpn, struct in6_addr addr)
+{
+#if IS_ENABLED(CONFIG_IPV6)
+	struct rt6_info *rt;
+	struct flowi6 fl = {
+		.daddr = addr,
+	};
+
+	rt = (struct rt6_info *)ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl,
+								NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI6\n", ovpn->dev->name, &addr);
+		/* if we end up here this packet is probably going to be thrown away later */
+		return addr;
+	}
+
+	if (rt->rt6i_flags & RTF_GATEWAY)
+		addr = rt->rt6i_gateway;
+
+	dst_release((struct dst_entry *)rt);
+#endif
+	return addr;
+}
+
 struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb)
 {
 	struct ovpn_peer *tmp, *peer = NULL;
+	struct hlist_head *head;
+	sa_family_t sa_fam;
+	struct in6_addr addr6;
+	__be32 addr4;
+	u32 index;
 
 	/* in P2P mode, no matter the destination, packets are always sent to the single peer
 	 * listening on the other side
@@ -198,35 +363,29 @@ struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_bu
 		if (likely(tmp && ovpn_peer_hold(tmp)))
 			peer = tmp;
 		rcu_read_unlock();
+		return peer;
 	}
 
-	return peer;
-}
+	sa_fam = skb_protocol_to_family(skb);
 
-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 = skb_protocol_to_family(skb);
-	switch (ss->ss_family) {
+	switch (sa_fam) {
 	case AF_INET:
-		sa4 = (struct sockaddr_in *)ss;
-		sa4->sin_family = AF_INET;
-		sa4->sin_addr.s_addr = ip_hdr(skb)->saddr;
-		sa4->sin_port = udp_hdr(skb)->source;
+		addr4 = ovpn_nexthop_lookup4(ovpn, ip_hdr(skb)->saddr);
+		index = ovpn_peer_index(ovpn->peers.by_vpn_addr, &addr4, sizeof(addr4));
+		head = &ovpn->peers.by_vpn_addr[index];
+
+		peer = ovpn_peer_lookup_vpn_addr4(head, &addr4);
 		break;
 	case AF_INET6:
-		sa6 = (struct sockaddr_in6 *)ss;
-		sa6->sin6_family = AF_INET6;
-		sa6->sin6_addr = ipv6_hdr(skb)->saddr;
-		sa6->sin6_port = udp_hdr(skb)->source;
+		addr6 = ovpn_nexthop_lookup6(ovpn, ipv6_hdr(skb)->saddr);
+		index = ovpn_peer_index(ovpn->peers.by_vpn_addr, &addr6, sizeof(addr6));
+		head = &ovpn->peers.by_vpn_addr[index];
+
+		peer = ovpn_peer_lookup_vpn_addr6(head, &addr6);
 		break;
-	default:
-		return false;
 	}
 
-	return true;
+	return peer;
 }
 
 static bool ovpn_peer_transp_match(struct ovpn_peer *peer, struct sockaddr_storage *ss)
@@ -277,16 +436,74 @@ static struct ovpn_peer *ovpn_peer_lookup_transp_addr_p2p(struct ovpn_struct *ov
 	return peer;
 }
 
+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 = skb_protocol_to_family(skb);
+	switch (ss->ss_family) {
+	case AF_INET:
+		sa4 = (struct sockaddr_in *)ss;
+		sa4->sin_family = AF_INET;
+		sa4->sin_addr.s_addr = ip_hdr(skb)->saddr;
+		sa4->sin_port = udp_hdr(skb)->source;
+		break;
+	case AF_INET6:
+		sa6 = (struct sockaddr_in6 *)ss;
+		sa6->sin6_family = AF_INET6;
+		sa6->sin6_addr = ipv6_hdr(skb)->saddr;
+		sa6->sin6_port = udp_hdr(skb)->source;
+		break;
+	default:
+		return false;
+	}
+
+	return true;
+}
+
 struct ovpn_peer *ovpn_peer_lookup_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb)
 {
-	struct ovpn_peer *peer = NULL;
+	struct ovpn_peer *tmp, *peer = NULL;
 	struct sockaddr_storage ss = { 0 };
+	struct hlist_head *head;
+	size_t sa_len;
+	bool found;
+	u32 index;
 
 	if (unlikely(!ovpn_peer_skb_to_sockaddr(skb, &ss)))
 		return NULL;
 
 	if (ovpn->mode == OVPN_MODE_P2P)
-		peer = ovpn_peer_lookup_transp_addr_p2p(ovpn, &ss);
+		return ovpn_peer_lookup_transp_addr_p2p(ovpn, &ss);
+
+	switch (ss.ss_family) {
+	case AF_INET:
+		sa_len = sizeof(struct sockaddr_in);
+		break;
+	case AF_INET6:
+		sa_len = sizeof(struct sockaddr_in6);
+		break;
+	default:
+		return NULL;
+	}
+
+	index = ovpn_peer_index(ovpn->peers.by_transp_addr, &ss, sa_len);
+	head = &ovpn->peers.by_transp_addr[index];
+
+	rcu_read_lock();
+	hlist_for_each_entry_rcu(tmp, head, hash_entry_transp_addr) {
+		found = ovpn_peer_transp_match(tmp, &ss);
+		if (!found)
+			continue;
+
+		if (!ovpn_peer_hold(tmp))
+			continue;
+
+		peer = tmp;
+		break;
+	}
+	rcu_read_unlock();
 
 	return peer;
 }
@@ -306,10 +523,28 @@ static struct ovpn_peer *ovpn_peer_lookup_id_p2p(struct ovpn_struct *ovpn, u32 p
 
 struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id)
 {
-	struct ovpn_peer *peer = NULL;
+	struct ovpn_peer *tmp, *peer = NULL;
+	struct hlist_head *head;
+	u32 index;
 
 	if (ovpn->mode == OVPN_MODE_P2P)
-		peer = ovpn_peer_lookup_id_p2p(ovpn, peer_id);
+		return ovpn_peer_lookup_id_p2p(ovpn, peer_id);
+
+	index = ovpn_peer_index(ovpn->peers.by_id, &peer_id, sizeof(peer_id));
+	head = &ovpn->peers.by_id[index];
+
+	rcu_read_lock();
+	hlist_for_each_entry_rcu(tmp, head, hash_entry_id) {
+		if (tmp->id != peer_id)
+			continue;
+
+		if (!ovpn_peer_hold(tmp))
+			continue;
+
+		peer = tmp;
+		break;
+	}
+	rcu_read_unlock();
 
 	return peer;
 }
Signature · 1 line
2.43.0
reviewer
#69 lore raw

Re: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic

Quoted 7 lines · Antonio Quartulli

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 tranport IP/port couple

nit: transport

checkpatch.pl --codespell is your friend here.

Quoted 9 lines

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.

...

Quoted 9 lines

+/**
+ * ovpn_nexthop_lookup4() - looks up the IP of the nexthop for the given destination
+ *
+ * Looks up in the IPv4 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up

I think you need to document @src instead of @dst here.

Quoted 26 lines

+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */

drivers/net/ovpn/peer.c +23 -0
+static __be32 ovpn_nexthop_lookup4(struct ovpn_struct *ovpn, __be32 src)
+{
+	struct rtable *rt;
+	struct flowi4 fl = {
+		.daddr = src
+	};
+
+	rt = ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI4\n", ovpn->dev->name, &src);
+		/* if we end up here this packet is probably going to be
+		 * thrown away later
+		 */
+		return src;
+	}
+
+	if (!rt->rt_uses_gateway)
+		goto out;
+
+	src = rt->rt_gw4;
+out:
+	return src;
+}

drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_bu · line 320

Quoted 10 lines
drivers/net/ovpn/peer.c +10 -0
+
+/**
+ * ovpn_nexthop_lookup6() - looks up the IPv6 of the nexthop for the given destination
+ *
+ * Looks up in the IPv6 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up

And here.

Quoted 26 lines

+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */

drivers/net/ovpn/peer.c +23 -0
+static struct in6_addr ovpn_nexthop_lookup6(struct ovpn_struct *ovpn, struct in6_addr addr)
+{
+#if IS_ENABLED(CONFIG_IPV6)
+	struct rt6_info *rt;
+	struct flowi6 fl = {
+		.daddr = addr,
+	};
+
+	rt = (struct rt6_info *)ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl,
+								NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI6\n", ovpn->dev->name, &addr);
+		/* if we end up here this packet is probably going to be thrown away later */
+		return addr;
+	}
+
+	if (rt->rt6i_flags & RTF_GATEWAY)
+		addr = rt->rt6i_gateway;
+
+	dst_release((struct dst_entry *)rt);
+#endif
+	return addr;
+}

...

author maintainer
#70 lore raw

Re: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic

Quoted 4 lines · Simon Horman
Quoted 3 lines · Antonio Quartulli

In a multi-peer scenario there are a number of situations when a specific peer needs to be looked up.

Quoted 8 lines
Quoted 4 lines

We may want to lookup a peer by:
1. its ID
2. its VPN destination IP
3. its tranport IP/port couple

nit: transport

checkpatch.pl --codespell is your friend here.

Thanks for this hint too!

Quoted 23 lines
Quoted 10 lines

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.

...

Quoted 9 lines

+/**
+ * ovpn_nexthop_lookup4() - looks up the IP of the nexthop for the given destination
+ *
+ * Looks up in the IPv4 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up

Quoted 1 line

I think you need to document @src instead of @dst here.

Right

Quoted 31 lines
Quoted 31 lines

+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */

drivers/net/ovpn/peer.c +27 -0
+static __be32 ovpn_nexthop_lookup4(struct ovpn_struct *ovpn, __be32 src)
+{
+	struct rtable *rt;
+	struct flowi4 fl = {
+		.daddr = src
+	};
+
+	rt = ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI4\n", ovpn->dev->name, &src);
+		/* if we end up here this packet is probably going to be
+		 * thrown away later
+		 */
+		return src;
+	}
+
+	if (!rt->rt_uses_gateway)
+		goto out;
+
+	src = rt->rt_gw4;
+out:
+	return src;
+}
+
+/**
+ * ovpn_nexthop_lookup6() - looks up the IPv6 of the nexthop for the given destination
+ *
Quoted 8 lines
Quoted 6 lines

+ * Looks up in the IPv6 system routing table the IP of the nexthop to be used
+ * 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.
+ *
+ * @ovpn: the private data representing the current VPN session
+ * @dst: the destination to be looked up

And here.

Right.

Quoted 21 lines
Quoted 21 lines

+ *
+ * Return the IP of the next hop if found or the dst itself otherwise
+ */

drivers/net/ovpn/peer.c +17 -0
+static struct in6_addr ovpn_nexthop_lookup6(struct ovpn_struct *ovpn, struct in6_addr addr)
+{
+#if IS_ENABLED(CONFIG_IPV6)
+	struct rt6_info *rt;
+	struct flowi6 fl = {
+		.daddr = addr,
+	};
+
+	rt = (struct rt6_info *)ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl,
+								NULL);
+	if (IS_ERR(rt)) {
+		net_dbg_ratelimited("%s: no nexthop found for %pI6\n", ovpn->dev->name, &addr);
+		/* if we end up here this packet is probably going to be thrown away later */
+		return addr;
+	}
+
+	if (rt->rt6i_flags & RTF_GATEWAY)
Quoted 8 lines
Quoted 6 lines
drivers/net/ovpn/peer.c +6 -0
+		addr = rt->rt6i_gateway;
+
+	dst_release((struct dst_entry *)rt);
+#endif
+	return addr;
+}

...

Thanks a lot.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#71 lore raw

Re: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic

In reply to Antonio Quartulli
kernel test robot noticed the following build warnings:

Hi Antonio,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Antonio-Quartulli/netlink-add-NLA_POLICY_MAX_LEN-macro/20240304-232835
base:   net-next/main
patch link:    https://lore.kernel.org/r/20240304150914.11444-15-antonio%40openvpn.net
patch subject: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20240306/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240306/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/net/ovpn/peer.c:289: warning: Function parameter or struct member 'src' not described in 'ovpn_nexthop_lookup4'
>> drivers/net/ovpn/peer.c:289: warning: Excess function parameter 'dst' description in 'ovpn_nexthop_lookup4'
>> drivers/net/ovpn/peer.c:325: warning: Function parameter or struct member 'addr' not described in 'ovpn_nexthop_lookup6'
>> drivers/net/ovpn/peer.c:325: warning: Excess function parameter 'dst' description in 'ovpn_nexthop_lookup6'

vim +289 drivers/net/ovpn/peer.c

275
276 /**
277 * ovpn_nexthop_lookup4() - looks up the IP of the nexthop for the given destination
278 *
279 * Looks up in the IPv4 system routing table the IP of the nexthop to be used
280 * to reach the destination passed as argument. If no nexthop can be found, the
281 * destination itself is returned as it probably has to be used as nexthop.
282 *
283 * @ovpn: the private data representing the current VPN session
284 * @dst: the destination to be looked up
285 *
286 * Return the IP of the next hop if found or the dst itself otherwise
287 */
288 static __be32 ovpn_nexthop_lookup4(struct ovpn_struct *ovpn, __be32 src)

 > 289	{
   290		struct rtable *rt;
   291		struct flowi4 fl = {
   292			.daddr = src
   293		};
   294	
   295		rt = ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL);
   296		if (IS_ERR(rt)) {
   297			net_dbg_ratelimited("%s: no nexthop found for %pI4\n", ovpn->dev->name, &src);
   298			/* if we end up here this packet is probably going to be
   299			 * thrown away later
   300			 */
   301			return src;
   302		}
   303	
   304		if (!rt->rt_uses_gateway)
   305			goto out;
   306	
   307		src = rt->rt_gw4;
   308	out:
   309		return src;
   310	}
   311	
   312	/**
   313	 * ovpn_nexthop_lookup6() - looks up the IPv6 of the nexthop for the given destination
   314	 *
   315	 * Looks up in the IPv6 system routing table the IP of the nexthop to be used
   316	 * to reach the destination passed as argument. If no nexthop can be found, the
   317	 * destination itself is returned as it probably has to be used as nexthop.
   318	 *
   319	 * @ovpn: the private data representing the current VPN session
   320	 * @dst: the destination to be looked up
   321	 *
   322	 * Return the IP of the next hop if found or the dst itself otherwise
   323	 */
   324	static struct in6_addr ovpn_nexthop_lookup6(struct ovpn_struct *ovpn, struct in6_addr addr)
 > 325	{

326 #if IS_ENABLED(CONFIG_IPV6)
327 struct rt6_info *rt;
328 struct flowi6 fl = {
329 .daddr = addr,
330 };
331
332 rt = (struct rt6_info *)ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl,
333 NULL);
334 if (IS_ERR(rt)) {
335 net_dbg_ratelimited("%s: no nexthop found for %pI6\n", ovpn->dev->name, &addr);
336 /* if we end up here this packet is probably going to be thrown away later */
337 return addr;
338 }
339
340 if (rt->rt6i_flags & RTF_GATEWAY)
341 addr = rt->rt6i_gateway;
342
343 dst_release((struct dst_entry *)rt);
344 #endif
345 return addr;
346 }
347

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#72 lore raw

Re: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic

In reply to Antonio Quartulli
kernel test robot noticed the following build warnings:

Hi Antonio,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Antonio-Quartulli/netlink-add-NLA_POLICY_MAX_LEN-macro/20240304-232835
base:   net-next/main
patch link:    https://lore.kernel.org/r/20240304150914.11444-15-antonio%40openvpn.net
patch subject: [PATCH net-next v2 14/22] ovpn: implement peer lookup logic
config: sparc-randconfig-r051-20240309 (https://download.01.org/0day-ci/archive/20240309/[email protected]/config)
compiler: sparc-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240309/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/net/ovpn/peer.c: In function 'ovpn_nexthop_from_rt6':
drivers/net/ovpn/peer.c:170:24: error: invalid use of undefined type 'struct rt6_info'

     170 |         if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
         |                        ^~
   drivers/net/ovpn/peer.c:173:18: error: invalid use of undefined type 'struct rt6_info'
     173 |         return rt->rt6i_gateway;
         |                  ^~
>> drivers/net/ovpn/peer.c:174:1: warning: control reaches end of non-void function [-Wreturn-type]
     174 | }
         | ^

vim +174 drivers/net/ovpn/peer.c

165
166 static struct in6_addr ovpn_nexthop_from_rt6(struct sk_buff *skb)
167 {
168 struct rt6_info *rt = (struct rt6_info *)skb_rtable(skb);
169

 > 170		if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
   171			return ipv6_hdr(skb)->daddr;
   172	
   173		return rt->rt6i_gateway;
 > 174	}

175

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#73 lore raw

PATCH v2 15/22 ovpn: implement keepalive mechanism

In reply to Antonio Quartulli

OpenVPN supports configuring a periodic keepalive "ping" message to allow the remote endpoint detect link failures.

This change implements the ping sending and timer expiring logic.

---
 drivers/net/ovpn/io.c   | 76 +++++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/io.h   |  1 +
 drivers/net/ovpn/peer.c | 52 ++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h | 36 +++++++++++++++++++
 4 files changed, 165 insertions(+)
drivers/net/ovpn/io.c +76 -0
index 91706c55784d..d070e2fd642f 100644
@@ -22,6 +22,26 @@
 #include <net/gso.h>
 
 
+static const unsigned char ovpn_keepalive_message[] = {
+	0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
+	0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
+};
+
+/* Is keepalive message?
+ * Assumes that the first byte of skb->data is defined.
+ */
+static bool ovpn_is_keepalive(struct sk_buff *skb)
+{
+	if (*skb->data != OVPN_KEEPALIVE_FIRST_BYTE)
+		return false;
+
+	if (!pskb_may_pull(skb, sizeof(ovpn_keepalive_message)))
+		return false;
+
+	return !memcmp(skb->data, ovpn_keepalive_message,
+		       sizeof(ovpn_keepalive_message));
+}
+
 int ovpn_struct_init(struct net_device *dev)
 {
 	struct ovpn_struct *ovpn = netdev_priv(dev);
@@ -186,6 +206,9 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 		goto drop;
 	}
 
+	/* note event of authenticated packet received for keepalive */
+	ovpn_peer_keepalive_recv_reset(peer);
+
 	/* increment RX stats */
 	ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len);
 
@@ -203,6 +226,17 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 			goto drop;
 		}
 
+		/* check if special OpenVPN message */
+		if (ovpn_is_keepalive(skb)) {
+			netdev_dbg(peer->ovpn->dev, "ping received from peero %u\n", peer->id);
+			/* not an error */
+			consume_skb(skb);
+			/* inform the caller that NAPI should not be scheduled
+			 * for this packet
+			 */
+			return -1;
+		}
+
 		netdev_dbg(peer->ovpn->dev, "unsupported protocol received from peer %u\n",
 			   peer->id);
 
@@ -338,6 +372,9 @@ void ovpn_encrypt_work(struct work_struct *work)
 					break;
 				}
 			}
+
+			/* note event of authenticated packet xmit for keepalive */
+			ovpn_peer_keepalive_xmit_reset(peer);
 		}
 
 		/* give a chance to be rescheduled if needed */
@@ -437,3 +474,42 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 	kfree_skb_list(skb);
 	return NET_XMIT_DROP;
 }
+
+/* Encrypt and transmit a special message to peer, such as keepalive
+ * or explicit-exit-notify.  Called from softirq context.
+ * Assumes that caller holds a reference to peer.
+ */
+static void ovpn_xmit_special(struct ovpn_peer *peer, const void *data,
+			      const unsigned int len)
+{
+	struct ovpn_struct *ovpn;
+	struct sk_buff *skb;
+
+	ovpn = peer->ovpn;
+	if (unlikely(!ovpn))
+		return;
+
+	skb = alloc_skb(256 + len, GFP_ATOMIC);
+	if (unlikely(!skb))
+		return;
+
+	skb_reserve(skb, 128);
+	skb->priority = TC_PRIO_BESTEFFORT;
+	memcpy(__skb_put(skb, len), 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 special packet\n",
+			   __func__);
+		kfree_skb(skb);
+		return;
+	}
+
+	ovpn_queue_skb(ovpn, skb, peer);
+}
+
+void ovpn_keepalive_xmit(struct ovpn_peer *peer)
+{
+	ovpn_xmit_special(peer, ovpn_keepalive_message,
+			  sizeof(ovpn_keepalive_message));
+}
drivers/net/ovpn/io.h +1 -0
index dee121a36dd7..8e708857ec74 100644
@@ -19,6 +19,7 @@ struct ovpn_struct;
 int ovpn_struct_init(struct net_device *dev);
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);
 int ovpn_napi_poll(struct napi_struct *napi, int budget);
+void ovpn_keepalive_xmit(struct ovpn_peer *peer);
 
 int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, struct sk_buff *skb);
 
drivers/net/ovpn/peer.c +52 -0

73 lines not on this page · raw message

drivers/net/ovpn/peer.h +36 -0
index 72bbe8ffd252..9e5ff9ee1c92 100644
@@ -77,6 +77,20 @@ struct ovpn_peer {
 	/* our binding to peer, protected by spinlock */
 	struct ovpn_bind __rcu *bind;
 
+	/* timer used to send periodic ping messages to the other peer, if no
+	 * other data was sent within the past keepalive_interval seconds
+	 */
+	struct timer_list keepalive_xmit;
+	/* keepalive interval in seconds */
+	unsigned long keepalive_interval;
+
+	/* timer used to mark a peer as expired when no data is received for
+	 * keepalive_timeout seconds
+	 */
+	struct timer_list keepalive_recv;
+	/* keepalive timeout in seconds */
+	unsigned long keepalive_timeout;
+
 	/* true if ovpn_peer_mark_delete was called */
 	bool halt;
 
@@ -117,8 +131,30 @@ static inline void ovpn_peer_put(struct ovpn_peer *peer)
 	kref_put(&peer->refcount, ovpn_peer_release_kref);
 }
 
+static inline void ovpn_peer_keepalive_recv_reset(struct ovpn_peer *peer)
+{
+	u32 delta = msecs_to_jiffies(peer->keepalive_timeout * MSEC_PER_SEC);
+
+	if (unlikely(!delta))
+		return;
+
+	mod_timer(&peer->keepalive_recv, jiffies + delta);
+}
+
+static inline void ovpn_peer_keepalive_xmit_reset(struct ovpn_peer *peer)
+{
+	u32 delta = msecs_to_jiffies(peer->keepalive_interval * MSEC_PER_SEC);
+
+	if (unlikely(!delta))
+		return;
+
+	mod_timer(&peer->keepalive_xmit, jiffies + delta);
+}
+
 struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id);
 
+void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout);
+
 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);
 struct ovpn_peer *ovpn_peer_find(struct ovpn_struct *ovpn, u32 peer_id);
Signature · 1 line
2.43.0
author maintainer
#74 lore raw

PATCH v2 16/22 ovpn: add support for updating local UDP endpoint

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/io.c   |  4 ++++
 drivers/net/ovpn/peer.c | 34 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h |  2 ++
 3 files changed, 40 insertions(+)
drivers/net/ovpn/io.c +4 -0
index d070e2fd642f..cb2a355f8766 100644
@@ -209,6 +209,10 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 	/* note event of authenticated packet received for keepalive */
 	ovpn_peer_keepalive_recv_reset(peer);
 
+	/* update source endpoint for this peer */
+	if (peer->sock->sock->sk->sk_protocol == IPPROTO_UDP)
+		ovpn_peer_update_local_endpoint(peer, skb);
+
 	/* increment RX stats */
 	ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len);
 
drivers/net/ovpn/peer.c +34 -0
index 58ea72557d89..1fcced805399 100644
@@ -601,6 +601,40 @@ struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id)
 	return peer;
 }
 
+void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	struct ovpn_bind *bind;
+
+	rcu_read_lock();
+	bind = rcu_dereference(peer->bind);
+	if (unlikely(!bind))
+		goto unlock;
+
+	switch (skb_protocol_to_family(skb)) {
+	case AF_INET:
+		if (unlikely(bind->local.ipv4.s_addr != 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 = ip_hdr(skb)->daddr;
+		}
+		break;
+	case AF_INET6:
+		if (unlikely(memcmp(&bind->local.ipv6, &ipv6_hdr(skb)->daddr,
+				    sizeof(bind->local.ipv6)))) {
+			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 = ipv6_hdr(skb)->daddr;
+		}
+		break;
+	default:
+		break;
+	}
+unlock:
+	rcu_read_unlock();
+}
+
 static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer)
 {
 	struct sockaddr_storage sa = { 0 };
drivers/net/ovpn/peer.h +2 -0
index 9e5ff9ee1c92..739560fc992a 100644
@@ -165,6 +165,8 @@ struct ovpn_peer *ovpn_peer_lookup_by_dst(struct ovpn_struct *ovpn, struct sk_bu
 struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb);
 struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);
 
+void ovpn_peer_update_local_endpoint(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);
 
Signature · 1 line
2.43.0
author maintainer
#75 lore raw

PATCH v2 17/22 ovpn: add support for peer floating

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/peer.c | 92 +++++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h |  1 +
 drivers/net/ovpn/udp.c  |  3 ++
 3 files changed, 96 insertions(+)
drivers/net/ovpn/peer.c +92 -0
index 1fcced805399..ca5cce0a0cda 100644
@@ -139,9 +139,101 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)
 	return ERR_PTR(ret);
 }
 
+int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, const struct sockaddr_storage *ss,
+			     const u8 *local_ip)
+{
+	struct ovpn_bind *bind;
+	size_t ip_len;
+
+	/* create new ovpn_bind object */
+	bind = ovpn_bind_from_sockaddr(ss);
+	if (IS_ERR(bind))
+		return PTR_ERR(bind);
+
+	if (local_ip) {
+		if (ss->ss_family == AF_INET) {
+			ip_len = sizeof(struct in_addr);
+		} else if (ss->ss_family == AF_INET6) {
+			ip_len = 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_peer_index(_tbl, _key, _key_len)		\
 	(jhash(_key, _key_len, 0) % HASH_SIZE(_tbl))	\
 
+void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb)
+{
+	struct sockaddr_storage ss;
+	const u8 *local_ip = NULL;
+	struct sockaddr_in6 *sa6;
+	struct sockaddr_in *sa;
+	struct ovpn_bind *bind;
+	sa_family_t family;
+	size_t salen;
+	u32 index;
+
+	rcu_read_lock();
+	bind = rcu_dereference(peer->bind);
+	if (unlikely(!bind))
+		goto unlock;
+
+	if (likely(ovpn_bind_skb_src_match(bind, skb)))
+		goto unlock;
+
+	family = skb_protocol_to_family(skb);
+
+	if (bind->sa.in4.sin_family == family)
+		local_ip = (u8 *)&bind->local;
+
+	switch (family) {
+	case AF_INET:
+		sa = (struct sockaddr_in *)&ss;
+		sa->sin_family = AF_INET;
+		sa->sin_addr.s_addr = ip_hdr(skb)->saddr;
+		sa->sin_port = udp_hdr(skb)->source;
+		salen = sizeof(*sa);
+		break;
+	case AF_INET6:
+		sa6 = (struct sockaddr_in6 *)&ss;
+		sa6->sin6_family = AF_INET6;
+		sa6->sin6_addr = ipv6_hdr(skb)->saddr;
+		sa6->sin6_port = udp_hdr(skb)->source;
+		sa6->sin6_scope_id = ipv6_iface_scope_id(&ipv6_hdr(skb)->saddr, skb->skb_iif);
+		salen = 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);
+
+	spin_lock_bh(&peer->ovpn->peers.lock);
+	/* remove old hashing */
+	hlist_del_init_rcu(&peer->hash_entry_transp_addr);
+	/* re-add with new transport address */
+	index = ovpn_peer_index(peer->ovpn->peers.by_transp_addr, &ss, salen);
+	hlist_add_head_rcu(&peer->hash_entry_transp_addr,
+			   &peer->ovpn->peers.by_transp_addr[index]);
+	spin_unlock_bh(&peer->ovpn->peers.lock);
+
+unlock:
+	rcu_read_unlock();
+}
+
 static void ovpn_peer_timer_delete_all(struct ovpn_peer *peer)
 {
 	del_timer_sync(&peer->keepalive_xmit);
drivers/net/ovpn/peer.h +1 -0
index 739560fc992a..c0bcb3a86ecf 100644
@@ -166,6 +166,7 @@ struct ovpn_peer *ovpn_peer_lookup_by_src(struct ovpn_struct *ovpn, struct sk_bu
 struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);
 
 void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, struct sk_buff *skb);
+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);
drivers/net/ovpn/udp.c +3 -0
index addcf55abf26..693f1f6a2c0c 100644
@@ -81,6 +81,9 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 					   __func__, peer_id);
 			goto drop;
 		}
+
+		/* check if this peer changed it's IP address and update state */
+		ovpn_peer_float(peer, skb);
 	}
 
 	if (!peer) {
Signature · 1 line
2.43.0
author maintainer
#76 lore raw

PATCH v2 18/22 ovpn: implement peer add/dump/delete via netlink

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/netlink.c | 474 +++++++++++++++++++++++++++++++++++++
 1 file changed, 474 insertions(+)
drivers/net/ovpn/netlink.c +474 -0
index 02b41034f615..99ee9889241d 100644
@@ -10,6 +10,7 @@
 #include "io.h"
 #include "netlink.h"
 #include "ovpnstruct.h"
+#include "peer.h"
 #include "packet.h"
 
 #include <uapi/linux/ovpn.h>
@@ -154,6 +155,463 @@ static void ovpn_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb
 		dev_put(ovpn->dev);
 }
 
+static int ovpn_nl_set_peer(struct sk_buff *skb, struct genl_info *info)
+{
+	bool keepalive_set = false, new_peer = false;
+	struct nlattr *attrs[NUM_OVPN_A_PEER];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	struct sockaddr_storage *ss = NULL;
+	u32 sockfd, id, interv, timeout;
+	struct socket *sock = NULL;
+	struct sockaddr_in mapped;
+	struct sockaddr_in6 *in6;
+	struct ovpn_peer *peer;
+	size_t sa_len, ip_len;
+	u8 *local_ip = NULL;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER], NULL,
+			       info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_PEER_ID]) {
+		netdev_err(ovpn->dev, "%s: peer ID missing\n", __func__);
+		return -EINVAL;
+	}
+
+	id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
+	/* check if the peer exists first, otherwise create a new one */
+	peer = ovpn_peer_lookup_id(ovpn, id);
+	if (!peer) {
+		peer = ovpn_peer_new(ovpn, id);
+		new_peer = true;
+		if (IS_ERR(peer)) {
+			netdev_err(ovpn->dev, "%s: cannot create new peer object for peer %u (sockaddr=%pIScp): %ld\n",
+				   __func__, id, ss, PTR_ERR(peer));
+			return PTR_ERR(peer);
+		}
+	}
+
+	if (new_peer && !attrs[OVPN_A_PEER_SOCKET]) {
+		netdev_err(ovpn->dev, "%s: peer socket missing\n", __func__);
+		ret = -EINVAL;
+		goto peer_release;
+	}
+
+	if (new_peer && ovpn->mode == OVPN_MODE_MP && !attrs[OVPN_A_PEER_VPN_IPV4] &&
+	    !attrs[OVPN_A_PEER_VPN_IPV6]) {
+		netdev_err(ovpn->dev, "%s: a VPN IP is required when adding a peer in MP mode\n",
+			   __func__);
+		ret = -EINVAL;
+		goto peer_release;
+	}
+
+	if (attrs[OVPN_A_PEER_SOCKET]) {
+		/* lookup the fd in the kernel table and extract the socket object */
+		sockfd = nla_get_u32(attrs[OVPN_A_PEER_SOCKET]);
+		/* sockfd_lookup() increases sock's refcounter */
+		sock = sockfd_lookup(sockfd, &ret);
+		if (!sock) {
+			netdev_dbg(ovpn->dev, "%s: cannot lookup peer socket (fd=%u): %d\n",
+				   __func__, sockfd, ret);
+			ret = -ENOTSOCK;
+			goto peer_release;
+		}
+
+		/* 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 == IPPROTO_UDP && attrs[OVPN_A_PEER_SOCKADDR_REMOTE]) {
+			ss = nla_data(attrs[OVPN_A_PEER_SOCKADDR_REMOTE]);
+			sa_len = nla_len(attrs[OVPN_A_PEER_SOCKADDR_REMOTE]);
+			switch (sa_len) {
+			case sizeof(struct sockaddr_in):
+				if (ss->ss_family == AF_INET)
+					/* valid sockaddr */
+					break;
+
+				netdev_err(ovpn->dev, "%s: remote sockaddr_in has invalid family\n",
+					   __func__);
+				ret = -EINVAL;
+				goto peer_release;
+			case sizeof(struct sockaddr_in6):
+				if (ss->ss_family == AF_INET6)
+					/* valid sockaddr */
+					break;
+
+				netdev_err(ovpn->dev, "%s: remote sockaddr_in6 has invalid family\n",
+					   __func__);
+				ret = -EINVAL;
+				goto peer_release;
+			default:
+				netdev_err(ovpn->dev, "%s: invalid size for sockaddr\n", __func__);
+				ret = -EINVAL;
+				goto peer_release;
+			}
+
+			if (ss->ss_family == AF_INET6) {
+				in6 = (struct sockaddr_in6 *)ss;
+
+				if (ipv6_addr_type(&in6->sin6_addr) & IPV6_ADDR_MAPPED) {
+					mapped.sin_family = AF_INET;
+					mapped.sin_addr.s_addr = in6->sin6_addr.s6_addr32[3];
+					mapped.sin_port = in6->sin6_port;
+					ss = (struct sockaddr_storage *)&mapped;
+				}
+			}
+
+			/* When using UDP we may be talking over socket bound to 0.0.0.0/::.
+			 * In this case, if the host has multiple IPs, we need to make sure
+			 * that outgoing traffic has as source IP the same address that the
+			 * peer is using to reach us.
+			 *
+			 * Since early control packets were all forwarded to userspace, we
+			 * need the latter to tell us what IP has to be used.
+			 */
+			if (attrs[OVPN_A_PEER_LOCAL_IP]) {
+				ip_len = nla_len(attrs[OVPN_A_PEER_LOCAL_IP]);
+				local_ip = nla_data(attrs[OVPN_A_PEER_LOCAL_IP]);
+
+				if (ip_len == sizeof(struct in_addr)) {
+					if (ss->ss_family != AF_INET) {
+						netdev_dbg(ovpn->dev,
+							   "%s: the specified local IP is IPv4, but the peer endpoint is not\n",
+							   __func__);
+						ret = -EINVAL;
+						goto peer_release;
+					}
+				} else if (ip_len == sizeof(struct in6_addr)) {
+					bool is_mapped = ipv6_addr_type((struct in6_addr *)local_ip) &
+						IPV6_ADDR_MAPPED;
+
+					if (ss->ss_family != AF_INET6 && !is_mapped) {
+						netdev_dbg(ovpn->dev,
+							   "%s: the specified local IP is IPv6, but the peer endpoint is not\n",
+							   __func__);
+						ret = -EINVAL;
+						goto peer_release;
+					}
+
+					if (is_mapped)
+						/* this is an IPv6-mapped IPv4 address, therefore extract
+						 * the actual v4 address from the last 4 bytes
+						 */
+						local_ip += 12;
+				} else {
+					netdev_dbg(ovpn->dev,
+						   "%s: invalid length %zu for local IP\n", __func__,
+						   ip_len);
+					ret = -EINVAL;
+					goto peer_release;
+				}
+			}
+
+			/* set peer sockaddr */
+			ret = ovpn_peer_reset_sockaddr(peer, ss, local_ip);
+			if (ret < 0)
+				goto peer_release;
+		}
+
+		if (peer->sock)
+			ovpn_socket_put(peer->sock);
+
+		peer->sock = ovpn_socket_new(sock, peer);
+		if (IS_ERR(peer->sock)) {
+			sockfd_put(sock);
+			peer->sock = NULL;
+			ret = -ENOTSOCK;
+			goto peer_release;
+		}
+	}
+
+	/* VPN IPs cannot be updated, because they are hashed */
+	if (new_peer && attrs[OVPN_A_PEER_VPN_IPV4]) {
+		if (nla_len(attrs[OVPN_A_PEER_VPN_IPV4]) != sizeof(struct in_addr)) {
+			ret = -EINVAL;
+			goto peer_release;
+		}
+
+		peer->vpn_addrs.ipv4.s_addr = nla_get_be32(attrs[OVPN_A_PEER_VPN_IPV4]);
+	}
+
+	/* VPN IPs cannot be updated, because they are hashed */
+	if (new_peer && attrs[OVPN_A_PEER_VPN_IPV6]) {
+		if (nla_len(attrs[OVPN_A_PEER_VPN_IPV6]) != sizeof(struct in6_addr)) {
+			ret = -EINVAL;
+			goto peer_release;
+		}
+
+		memcpy(&peer->vpn_addrs.ipv6, nla_data(attrs[OVPN_A_PEER_VPN_IPV6]),
+		       sizeof(struct in6_addr));
+	}
+
+	/* when setting the keepalive, both parameters have to be configured */
+	if (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] &&
+	    attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) {
+		keepalive_set = true;
+		interv = nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL]);
+		timeout = nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]);
+	}
+
+	if (keepalive_set)
+		ovpn_peer_keepalive_set(peer, interv, timeout);
+
+	netdev_dbg(ovpn->dev,
+		   "%s: adding peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n",
+		   __func__, ss, sock->sk->sk_prot_creator->name, peer->id,
+		   &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6);
+
+	ret = ovpn_peer_add(ovpn, peer);
+	if (ret < 0) {
+		netdev_err(ovpn->dev, "%s: cannot add new peer (id=%u) to hashtable: %d\n",
+			   __func__, peer->id, ret);
+		goto peer_release;
+	}
+
+	return 0;
+
+peer_release:
+	/* release right away because peer is not really used in any context */
+	ovpn_peer_release(peer);
+	return ret;
+
+}
+
+static int ovpn_nl_send_peer(struct sk_buff *skb, const struct ovpn_peer *peer, u32 portid,
+			     u32 seq, int flags)
+{
+	const struct ovpn_bind *bind;
+	struct nlattr *attr;
+	void *hdr;
+
+	hdr = genlmsg_put(skb, portid, seq, &ovpn_nl_family, flags, OVPN_CMD_SET_PEER);
+	if (!hdr) {
+		netdev_dbg(peer->ovpn->dev, "%s: cannot create message header\n", __func__);
+		return -EMSGSIZE;
+	}
+
+	attr = nla_nest_start(skb, OVPN_A_PEER);
+	if (!attr) {
+		netdev_dbg(peer->ovpn->dev, "%s: cannot create submessage\n", __func__);
+		goto err;
+	}
+
+	if (nla_put_u32(skb, OVPN_A_PEER_ID, peer->id))
+		goto err;
+
+	if (peer->vpn_addrs.ipv4.s_addr != htonl(INADDR_ANY))
+		if (nla_put(skb, OVPN_A_PEER_VPN_IPV4, sizeof(peer->vpn_addrs.ipv4),
+			    &peer->vpn_addrs.ipv4))
+			goto err;
+
+	if (memcmp(&peer->vpn_addrs.ipv6, &in6addr_any, sizeof(peer->vpn_addrs.ipv6)))
+		if (nla_put(skb, OVPN_A_PEER_VPN_IPV6, sizeof(peer->vpn_addrs.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 = rcu_dereference(peer->bind);
+	if (bind) {
+		if (bind->sa.in4.sin_family == AF_INET) {
+			if (nla_put(skb, OVPN_A_PEER_SOCKADDR_REMOTE,
+				    sizeof(bind->sa.in4), &bind->sa.in4) ||
+			    nla_put(skb, OVPN_A_PEER_LOCAL_IP,
+				    sizeof(bind->local.ipv4), &bind->local.ipv4))
+				goto err_unlock;
+		} else if (bind->sa.in4.sin_family == AF_INET6) {
+			if (nla_put(skb, OVPN_A_PEER_SOCKADDR_REMOTE,
+				    sizeof(bind->sa.in6), &bind->sa.in6) ||
+			    nla_put(skb, OVPN_A_PEER_LOCAL_IP,
+				    sizeof(bind->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_u64_64bit(skb, OVPN_A_PEER_VPN_RX_BYTES,
+			      atomic64_read(&peer->vpn_stats.rx.bytes),
+			      OVPN_A_PEER_UNSPEC) ||
+	    nla_put_u32(skb, OVPN_A_PEER_VPN_RX_PACKETS,
+			atomic_read(&peer->vpn_stats.rx.packets)) ||
+	    /* VPN TX stats */
+	    nla_put_u64_64bit(skb, OVPN_A_PEER_VPN_TX_BYTES,
+			      atomic64_read(&peer->vpn_stats.tx.bytes),
+			      OVPN_A_PEER_UNSPEC) ||
+	    nla_put_u32(skb, OVPN_A_PEER_VPN_TX_PACKETS,
+			atomic_read(&peer->vpn_stats.tx.packets)) ||
+	    /* link RX stats */
+	    nla_put_u64_64bit(skb, OVPN_A_PEER_LINK_RX_BYTES,
+			      atomic64_read(&peer->link_stats.rx.bytes),
+			      OVPN_A_PEER_UNSPEC) ||
+	    nla_put_u32(skb, OVPN_A_PEER_LINK_RX_PACKETS,
+			atomic_read(&peer->link_stats.rx.packets)) ||
+	    /* link TX stats */
+	    nla_put_u64_64bit(skb, OVPN_A_PEER_LINK_TX_BYTES,
+			      atomic64_read(&peer->link_stats.tx.bytes),
+			      OVPN_A_PEER_UNSPEC) ||
+	    nla_put_u32(skb, OVPN_A_PEER_LINK_TX_PACKETS,
+			atomic_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;
+}
+
+static int ovpn_nl_get_peer(struct sk_buff *skb, struct genl_info *info)
+{
+	struct nlattr *attrs[NUM_OVPN_A_PEER];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	struct ovpn_peer *peer;
+	struct sk_buff *msg;
+	u32 peer_id;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER], NULL,
+			       info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_PEER_ID])
+		return -EINVAL;
+
+	peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
+	peer = ovpn_peer_lookup_id(ovpn, peer_id);
+	if (!peer)
+		return -ENOENT;
+
+	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	ret = ovpn_nl_send_peer(msg, peer, info->snd_portid, info->snd_seq, 0);
+	if (ret < 0) {
+		nlmsg_free(msg);
+		goto err;
+	}
+
+	ret = genlmsg_reply(msg, info);
+err:
+	ovpn_peer_put(peer);
+	return ret;
+}
+
+static int ovpn_nl_dump_peers(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	struct net *netns = sock_net(cb->skb->sk);
+	struct nlattr **attrbuf;
+	struct ovpn_struct *ovpn;
+	struct net_device *dev;
+	int ret, bkt, last_idx = cb->args[1], dumped = 0;
+	struct ovpn_peer *peer;
+
+	attrbuf = kcalloc(NUM_OVPN_A, sizeof(*attrbuf), GFP_KERNEL);
+	if (!attrbuf)
+		return -ENOMEM;
+
+	ret = nlmsg_parse_deprecated(cb->nlh, GENL_HDRLEN, attrbuf, NUM_OVPN_A,
+				     ovpn_nl_policy, NULL);
+	if (ret < 0) {
+		pr_err("ovpn: cannot parse incoming request in %s: %d\n", __func__, ret);
+		goto err;
+	}
+
+	dev = ovpn_get_dev_from_attrs(netns, attrbuf);
+	if (IS_ERR(dev)) {
+		ret = PTR_ERR(dev);
+		pr_err("ovpn: cannot retrieve device in %s: %d\n", __func__, ret);
+		goto err;
+	}
+
+	ovpn = netdev_priv(dev);
+
+	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, 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();
+
+	dev_put(dev);
+
+	/* sum up peers dumped in this message, so that at the next invocation
+	 * we can continue from where we left
+	 */
+	cb->args[1] += dumped;
+	ret = skb->len;
+err:
+	kfree(attrbuf);
+	return ret;
+}
+
+static int ovpn_nl_del_peer(struct sk_buff *skb, struct genl_info *info)
+{
+	struct nlattr *attrs[NUM_OVPN_A_PEER];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	struct ovpn_peer *peer;
+	u32 peer_id;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER], NULL,
+			       info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_PEER_ID])
+		return -EINVAL;
+
+	peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
+
+	peer = ovpn_peer_lookup_id(ovpn, peer_id);
+	if (!peer)
+		return -ENOENT;
+
+	netdev_dbg(ovpn->dev, "%s: peer id=%u\n", __func__, peer->id);
+	ret = ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_USERSPACE);
+	ovpn_peer_put(peer);
+
+	return ret;
+}
+
+
 static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
 {
 	enum ovpn_mode mode = OVPN_MODE_P2P;
@@ -205,6 +663,22 @@ static const struct genl_small_ops ovpn_nl_ops[] = {
 		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
 		.doit = ovpn_nl_del_iface,
 	},
+	{
+		.cmd = OVPN_CMD_SET_PEER,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_set_peer,
+	},
+	{
+		.cmd = OVPN_CMD_DEL_PEER,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_del_peer,
+	},
+	{
+		.cmd = OVPN_CMD_GET_PEER,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP,
+		.doit = ovpn_nl_get_peer,
+		.dumpit = ovpn_nl_dump_peers,
+	},
 };
 
 static struct genl_family ovpn_nl_family __ro_after_init = {
Signature · 1 line
2.43.0
author maintainer
#77 lore raw

PATCH v2 19/22 ovpn: implement key add/del/swap via netlink

In reply to Antonio Quartulli

This change introduces the nelink 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.

---
 drivers/net/ovpn/netlink.c | 194 +++++++++++++++++++++++++++++++++++++
 1 file changed, 194 insertions(+)
drivers/net/ovpn/netlink.c +194 -0
index 99ee9889241d..dbe1dfddba4c 100644
@@ -611,6 +611,185 @@ static int ovpn_nl_del_peer(struct sk_buff *skb, struct genl_info *info)
 	return ret;
 }
 
+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 *attr, *attrs[NUM_OVPN_A_KEYDIR];
+	int ret;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_KEYDIR - 1, key, NULL, info->extack);
+	if (ret)
+		return ret;
+
+	switch (cipher) {
+	case OVPN_CIPHER_ALG_AES_GCM:
+	case OVPN_CIPHER_ALG_CHACHA20_POLY1305:
+		attr = attrs[OVPN_A_KEYDIR_CIPHER_KEY];
+		if (!attr)
+			return -EINVAL;
+
+		dir->cipher_key = nla_data(attr);
+		dir->cipher_key_size = nla_len(attr);
+
+		attr = attrs[OVPN_A_KEYDIR_NONCE_TAIL];
+		/* These algorithms require a 96bit nonce,
+		 * Construct it by combining 4-bytes packet id and
+		 * 8-bytes nonce-tail from userspace
+		 */
+		if (!attr)
+			return -EINVAL;
+
+		dir->nonce_tail = nla_data(attr);
+		dir->nonce_tail_size = nla_len(attr);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int ovpn_nl_set_key(struct sk_buff *skb, struct genl_info *info)
+{
+	struct nlattr *p_attrs[NUM_OVPN_A_PEER];
+	struct nlattr *attrs[NUM_OVPN_A_KEYCONF];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	struct ovpn_peer_key_reset pkr;
+	struct ovpn_peer *peer;
+	u32 peer_id;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(p_attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER],
+			       NULL, info->extack);
+	if (ret)
+		return ret;
+
+	if (!p_attrs[OVPN_A_PEER_ID] || !p_attrs[OVPN_A_PEER_KEYCONF])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_KEYCONF - 1, p_attrs[OVPN_A_PEER_KEYCONF],
+			       NULL, info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_KEYCONF_SLOT] ||
+	    !attrs[OVPN_A_KEYCONF_KEY_ID] ||
+	    !attrs[OVPN_A_KEYCONF_CIPHER_ALG] ||
+	    !attrs[OVPN_A_KEYCONF_ENCRYPT_DIR] ||
+	    !attrs[OVPN_A_KEYCONF_DECRYPT_DIR])
+		return -EINVAL;
+
+	peer_id = nla_get_u32(p_attrs[OVPN_A_PEER_ID]);
+	pkr.slot = nla_get_u8(attrs[OVPN_A_KEYCONF_SLOT]);
+	pkr.key.key_id = nla_get_u16(attrs[OVPN_A_KEYCONF_KEY_ID]);
+	pkr.key.cipher_alg = nla_get_u16(attrs[OVPN_A_KEYCONF_CIPHER_ALG]);
+
+	ret = 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 = 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 = ovpn_peer_lookup_id(ovpn, peer_id);
+	if (!peer) {
+		netdev_dbg(ovpn->dev, "%s: no peer with id %u to set key for\n", __func__, peer_id);
+		return -ENOENT;
+	}
+
+	mutex_lock(&peer->crypto.mutex);
+	ret = ovpn_crypto_state_reset(&peer->crypto, &pkr);
+	if (ret < 0) {
+		netdev_dbg(ovpn->dev, "%s: cannot install new key for peer %u\n", __func__,
+			   peer_id);
+		goto unlock;
+	}
+
+	netdev_dbg(ovpn->dev, "%s: new key installed (id=%u) for peer %u\n", __func__,
+		   pkr.key.key_id, peer_id);
+unlock:
+	mutex_unlock(&peer->crypto.mutex);
+	ovpn_peer_put(peer);
+	return ret;
+}
+
+static int ovpn_nl_del_key(struct sk_buff *skb, struct genl_info *info)
+{
+	struct nlattr *p_attrs[NUM_OVPN_A_PEER];
+	struct nlattr *attrs[NUM_OVPN_A_KEYCONF];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	enum ovpn_key_slot slot;
+	struct ovpn_peer *peer;
+	u32 peer_id;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(p_attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER],
+			       NULL, info->extack);
+
+	if (!p_attrs[OVPN_A_PEER_ID] || !p_attrs[OVPN_A_PEER_KEYCONF])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_KEYCONF - 1, p_attrs[OVPN_A_PEER_KEYCONF],
+			       NULL, info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_KEYCONF_SLOT])
+		return -EINVAL;
+
+	peer_id = nla_get_u32(p_attrs[OVPN_A_PEER_ID]);
+	slot = nla_get_u8(attrs[OVPN_A_KEYCONF_SLOT]);
+
+	peer = ovpn_peer_lookup_id(ovpn, peer_id);
+	if (!peer)
+		return -ENOENT;
+
+	ovpn_crypto_key_slot_delete(&peer->crypto, slot);
+	ovpn_peer_put(peer);
+
+	return 0;
+}
+
+static int ovpn_nl_swap_keys(struct sk_buff *skb, struct genl_info *info)
+{
+	struct nlattr *attrs[NUM_OVPN_A_PEER];
+	struct ovpn_struct *ovpn = info->user_ptr[0];
+	struct ovpn_peer *peer;
+	u32 peer_id;
+	int ret;
+
+	if (!info->attrs[OVPN_A_PEER])
+		return -EINVAL;
+
+	ret = nla_parse_nested(attrs, NUM_OVPN_A_PEER - 1, info->attrs[OVPN_A_PEER],
+			       NULL, info->extack);
+	if (ret)
+		return ret;
+
+	if (!attrs[OVPN_A_PEER_ID])
+		return -EINVAL;
+
+	peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
+
+	peer = ovpn_peer_lookup_id(ovpn, peer_id);
+	if (!peer)
+		return -ENOENT;
+
+	ovpn_crypto_key_slots_swap(&peer->crypto);
+	ovpn_peer_put(peer);
+
+	return 0;
+}
 
 static int ovpn_nl_new_iface(struct sk_buff *skb, struct genl_info *info)
 {
@@ -679,6 +858,21 @@ static const struct genl_small_ops ovpn_nl_ops[] = {
 		.doit = ovpn_nl_get_peer,
 		.dumpit = ovpn_nl_dump_peers,
 	},
+	{
+		.cmd = OVPN_CMD_SET_KEY,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_set_key,
+	},
+	{
+		.cmd = OVPN_CMD_DEL_KEY,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_del_key,
+	},
+	{
+		.cmd = OVPN_CMD_SWAP_KEYS,
+		.flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+		.doit = ovpn_nl_swap_keys,
+	},
 };
 
 static struct genl_family ovpn_nl_family __ro_after_init = {
Signature · 1 line
2.43.0
author maintainer
#78 lore raw

PATCH v2 20/22 ovpn: kill key and notify userspace in case of IV exhaustion

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/io.c      | 14 +++++++++++++
 drivers/net/ovpn/netlink.c | 42 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/netlink.h |  3 +++
 3 files changed, 59 insertions(+)
drivers/net/ovpn/io.c +14 -0
index cb2a355f8766..ea8367c3da08 100644
@@ -318,6 +318,20 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 	/* encrypt */
 	ret = ovpn_aead_encrypt(ks, skb, peer->id);
 	if (unlikely(ret < 0)) {
+		/* if we ran out of IVs we must kill the key as it can't be used anymore */
+		if (ret == -ERANGE) {
+			netdev_warn(peer->ovpn->dev,
+				    "killing primary key as we ran out of IVs for peer %u\n",
+				    peer->id);
+			ovpn_crypto_kill_primary(&peer->crypto);
+			ret = ovpn_nl_notify_swap_keys(peer);
+			if (ret < 0)
+				netdev_warn(peer->ovpn->dev,
+					    "couldn't send key killing notification to userspace for peer %u\n",
+					    peer->id);
+			goto err;
+		}
+
 		net_err_ratelimited("%s: error during encryption for peer %u, key-id %u: %d\n",
 				    peer->ovpn->dev->name, peer->id, ks->key_id, ret);
 		goto err;
drivers/net/ovpn/netlink.c +42 -0

48 lines not on this page · raw message

drivers/net/ovpn/netlink.h +3 -0
index eb7f234842ef..17ca69761440 100644
@@ -9,10 +9,13 @@
 #ifndef _NET_OVPN_NETLINK_H_
 #define _NET_OVPN_NETLINK_H_
 
+struct ovpn_peer;
 struct ovpn_struct;
 
 int ovpn_nl_init(struct ovpn_struct *ovpn);
 int ovpn_nl_register(void);
 void ovpn_nl_unregister(void);
 
+int ovpn_nl_notify_swap_keys(struct ovpn_peer *peer);
+
 #endif /* _NET_OVPN_NETLINK_H_ */
Signature · 1 line
2.43.0
author maintainer
#79 lore raw

PATCH v2 21/22 ovpn: notify userspace when a peer is deleted

In reply to Antonio Quartulli

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.

---
 drivers/net/ovpn/netlink.c | 56 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/netlink.h |  1 +
 drivers/net/ovpn/peer.c    |  1 +
 3 files changed, 58 insertions(+)
drivers/net/ovpn/netlink.c +56 -0
index e8b55c0a15e9..78e09707e5b5 100644
@@ -891,6 +891,62 @@ static struct genl_family ovpn_nl_family __ro_after_init = {
 	.n_mcgrps = ARRAY_SIZE(ovpn_nl_mcgrps),
 };
 
+int ovpn_nl_notify_del_peer(struct ovpn_peer *peer)
+{
+	struct sk_buff *msg;
+	struct nlattr *attr;
+	void *hdr;
+	int ret;
+
+	netdev_info(peer->ovpn->dev, "deleting peer with id %u, reason %d\n",
+		    peer->id, peer->delete_reason);
+
+	msg = nlmsg_new(100, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	hdr = genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0,
+			  OVPN_CMD_DEL_PEER);
+	if (!hdr) {
+		ret = -ENOBUFS;
+		goto err_free_msg;
+	}
+
+	if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex)) {
+		ret = -EMSGSIZE;
+		goto err_free_msg;
+	}
+
+	attr = nla_nest_start(msg, OVPN_A_PEER);
+	if (!attr) {
+		ret = -EMSGSIZE;
+		goto err_free_msg;
+	}
+
+	if (nla_put_u8(msg, OVPN_A_PEER_DEL_REASON, peer->delete_reason)) {
+		ret = -EMSGSIZE;
+		goto err_free_msg;
+	}
+
+	if (nla_put_u32(msg, OVPN_A_PEER_ID, peer->id)) {
+		ret = -EMSGSIZE;
+		goto err_free_msg;
+	}
+
+	nla_nest_end(msg, attr);
+
+	genlmsg_end(msg, hdr);
+
+	genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev),
+				msg, 0, OVPN_MCGRP_PEERS, GFP_KERNEL);
+
+	return 0;
+
+err_free_msg:
+	nlmsg_free(msg);
+	return ret;
+}
+
 int ovpn_nl_notify_swap_keys(struct ovpn_peer *peer)
 {
 	struct sk_buff *msg;
drivers/net/ovpn/netlink.h +1 -0
index 17ca69761440..ac8ea60cefc2 100644
@@ -16,6 +16,7 @@ int ovpn_nl_init(struct ovpn_struct *ovpn);
 int ovpn_nl_register(void);
 void ovpn_nl_unregister(void);
 
+int ovpn_nl_notify_del_peer(struct ovpn_peer *peer);
 int ovpn_nl_notify_swap_keys(struct ovpn_peer *peer);
 
 #endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/peer.c +1 -0
index ca5cce0a0cda..50eb5839d8ef 100644
@@ -283,6 +283,7 @@ static void ovpn_peer_delete_work(struct work_struct *work)
 	struct ovpn_peer *peer = container_of(work, struct ovpn_peer,
 					      delete_work);
 	ovpn_peer_release(peer);
+	ovpn_nl_notify_del_peer(peer);
 }
 
 /* Use with kref_put calls, when releasing refcount
Signature · 1 line
2.43.0
author maintainer
#80 lore raw

PATCH v2 22/22 ovpn: add basic ethtool support

In reply to Antonio Quartulli
---
 drivers/net/ovpn/main.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/net/ovpn/main.c +32 -0
index 95a94ccc99c1..9dfcf2580659 100644
@@ -13,6 +13,7 @@
 #include "ovpnstruct.h"
 #include "packet.h"
 
+#include <linux/ethtool.h>
 #include <linux/genetlink.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
@@ -83,6 +84,36 @@ static const struct net_device_ops ovpn_netdev_ops = {
 	.ndo_get_stats64        = dev_get_tstats64,
 };
 
+static int ovpn_get_link_ksettings(struct net_device *dev,
+				   struct ethtool_link_ksettings *cmd)
+{
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0);
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0);
+	cmd->base.speed	= SPEED_1000;
+	cmd->base.duplex = DUPLEX_FULL;
+	cmd->base.port = PORT_TP;
+	cmd->base.phy_address = 0;
+	cmd->base.transceiver = XCVR_INTERNAL;
+	cmd->base.autoneg = AUTONEG_DISABLE;
+
+	return 0;
+}
+
+static void ovpn_get_drvinfo(struct net_device *dev,
+			     struct ethtool_drvinfo *info)
+{
+	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
+	strscpy(info->version, DRV_VERSION, sizeof(info->version));
+	strscpy(info->bus_info, "ovpn", sizeof(info->bus_info));
+}
+
+static const struct ethtool_ops ovpn_ethtool_ops = {
+	.get_link_ksettings	= ovpn_get_link_ksettings,
+	.get_drvinfo		= ovpn_get_drvinfo,
+	.get_link		= ethtool_op_get_link,
+	.get_ts_info		= ethtool_op_get_ts_info,
+};
+
 static void ovpn_setup(struct net_device *dev)
 {
 	/* compute the overhead considering AEAD encryption */
@@ -95,6 +126,7 @@ static void ovpn_setup(struct net_device *dev)
 
 	dev->needs_free_netdev = true;
 
+	dev->ethtool_ops = &ovpn_ethtool_ops;
 	dev->netdev_ops = &ovpn_netdev_ops;
 
 	dev->priv_destructor = ovpn_struct_free;
Signature · 1 line
2.43.0
#81 lore raw

Re: [PATCH net-next v2 22/22] ovpn: add basic ethtool support

Quoted 22 lines · Antonio Quartulli
---
 drivers/net/ovpn/main.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/net/ovpn/main.c +2 -0
index 95a94ccc99c1..9dfcf2580659 100644
@@ -13,6 +13,7 @@
 #include "ovpnstruct.h"
 #include "packet.h"
 
+#include <linux/ethtool.h>
 #include <linux/genetlink.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
@@ -83,6 +84,36 @@ static const struct net_device_ops ovpn_netdev_ops = {
 	.ndo_get_stats64        = dev_get_tstats64,
 };
 
+static int ovpn_get_link_ksettings(struct net_device *dev,

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 97

Quoted 10 lines
drivers/net/ovpn/main.c +10 -0
+				   struct ethtool_link_ksettings *cmd)
+{
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0);
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0);
+	cmd->base.speed	= SPEED_1000;
+	cmd->base.duplex = DUPLEX_FULL;
+	cmd->base.port = PORT_TP;
+	cmd->base.phy_address = 0;
+	cmd->base.transceiver = XCVR_INTERNAL;
+	cmd->base.autoneg = AUTONEG_DISABLE;

Why? It is a virtual device. Speed and duplex is meaningless. You could run this over FDDI, HIPPI, or RFC 1149? So why PORT_TP?

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 106

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+static void ovpn_get_drvinfo(struct net_device *dev,
+			     struct ethtool_drvinfo *info)
+{
+	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
+	strscpy(info->version, DRV_VERSION, sizeof(info->version));

Please leave version untouched. The ethtool core will then fill it in with something useful.

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 107

Quoted 1 line
drivers/net/ovpn/main.c +1 -0
+	strscpy(info->bus_info, "ovpn", sizeof(info->bus_info));

This is also not accurate. There is no bus involved.

Andrew

author maintainer
#82 lore raw

Re: [PATCH net-next v2 22/22] ovpn: add basic ethtool support

Quoted 22 lines · Andrew Lunn
Quoted 21 lines · Antonio Quartulli
---
  drivers/net/ovpn/main.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)
drivers/net/ovpn/main.c +1 -0
index 95a94ccc99c1..9dfcf2580659 100644
@@ -13,6 +13,7 @@
  #include "ovpnstruct.h"
  #include "packet.h"
 
+#include <linux/ethtool.h>
  #include <linux/genetlink.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
@@ -83,6 +84,36 @@ static const struct net_device_ops ovpn_netdev_ops = {
  	.ndo_get_stats64        = dev_get_tstats64,
  };
 
Quoted 14 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 97

Quoted 11 lines
drivers/net/ovpn/main.c +11 -0
+static int ovpn_get_link_ksettings(struct net_device *dev,
+				   struct ethtool_link_ksettings *cmd)
+{
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0);
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0);
+	cmd->base.speed	= SPEED_1000;
+	cmd->base.duplex = DUPLEX_FULL;
+	cmd->base.port = PORT_TP;
+	cmd->base.phy_address = 0;
+	cmd->base.transceiver = XCVR_INTERNAL;
+	cmd->base.autoneg = AUTONEG_DISABLE;

Why? It is a virtual device. Speed and duplex is meaningless. You could run this over FDDI, HIPPI, or RFC 1149? So why PORT_TP?

To be honest, I couldn't find any description to help me with deciding what to set there and I just used a value I saw in other Ethernet drivers.

Do you have any recommendation?
For the other fields: do you think they make sense? The speed value is always debatable...The actual speed depends on the transport interface and there might be multiple involved. Maybe SPEED_UNKNOWN is more appropriate?

Quoted 9 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 106

Quoted 6 lines
drivers/net/ovpn/main.c +5 -0
+static void ovpn_get_drvinfo(struct net_device *dev,
+			     struct ethtool_drvinfo *info)
+{
+	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
+	strscpy(info->version, DRV_VERSION, sizeof(info->version));

Please leave version untouched. The ethtool core will then fill it in with something useful.

will do!

Quoted 4 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 107

Quoted 2 lines
drivers/net/ovpn/main.c +1 -0
+	strscpy(info->bus_info, "ovpn", sizeof(info->bus_info));

This is also not accurate. There is no bus involved.

Should I just leave it empty then?

My concern is that a user expects $something and it will crash on my empty string. But if empty is allowed, I will just go with it.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#83 lore raw

Re: [PATCH net-next v2 22/22] ovpn: add basic ethtool support

Quoted 36 lines · Antonio Quartulli
Quoted 26 lines · Andrew Lunn
Quoted 25 lines · Antonio Quartulli
---
  drivers/net/ovpn/main.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)
drivers/net/ovpn/main.c +7 -0
index 95a94ccc99c1..9dfcf2580659 100644
@@ -13,6 +13,7 @@
  #include "ovpnstruct.h"
  #include "packet.h"
+#include <linux/ethtool.h>
  #include <linux/genetlink.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
@@ -83,6 +84,36 @@ static const struct net_device_ops ovpn_netdev_ops = {
  	.ndo_get_stats64        = dev_get_tstats64,
  };
+static int ovpn_get_link_ksettings(struct net_device *dev,
+				   struct ethtool_link_ksettings *cmd)
+{
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0);
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0);
+	cmd->base.speed	= SPEED_1000;
Quoted 8 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 97

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+	cmd->base.duplex = DUPLEX_FULL;
+	cmd->base.port = PORT_TP;
+	cmd->base.phy_address = 0;
+	cmd->base.transceiver = XCVR_INTERNAL;
+	cmd->base.autoneg = AUTONEG_DISABLE;

Why? It is a virtual device. Speed and duplex is meaningless. You could run this over FDDI, HIPPI, or RFC 1149? So why PORT_TP?

Quoted 7 lines

To be honest, I couldn't find any description to help me with deciding what to set there and I just used a value I saw in other Ethernet drivers.

Do you have any recommendation?
For the other fields: do you think they make sense? The speed value is always debatable...The actual speed depends on the transport interface and there might be multiple involved. Maybe SPEED_UNKNOWN is more appropriate?

Turn it around. What is your use case? What is using this information? I would just not implement this function. But maybe you know of something which actually requires it?

Quoted 8 lines
Quoted 3 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 107

Quoted 1 line
drivers/net/ovpn/main.c +1 -0
+	strscpy(info->bus_info, "ovpn", sizeof(info->bus_info));

This is also not accurate. There is no bus involved.

Should I just leave it empty then?

My concern is that a user expects $something and it will crash on my empty string. But if empty is allowed, I will just go with it.

Empty is allowed. The bridge uses "N/A", which i would say is also correct. tun/tap does however use "tun", so "ovpn" is not that different i supposes.

Andrew

author maintainer
#84 lore raw

Re: [PATCH net-next v2 22/22] ovpn: add basic ethtool support

Quoted 40 lines · Andrew Lunn
Quoted 39 lines · Antonio Quartulli
Quoted 26 lines · Andrew Lunn
Quoted 25 lines · Antonio Quartulli
---
   drivers/net/ovpn/main.c | 32 ++++++++++++++++++++++++++++++++
   1 file changed, 32 insertions(+)
drivers/net/ovpn/main.c +7 -0
index 95a94ccc99c1..9dfcf2580659 100644
@@ -13,6 +13,7 @@
   #include "ovpnstruct.h"
   #include "packet.h"
+#include <linux/ethtool.h>
   #include <linux/genetlink.h>
   #include <linux/module.h>
   #include <linux/moduleparam.h>
@@ -83,6 +84,36 @@ static const struct net_device_ops ovpn_netdev_ops = {
   	.ndo_get_stats64        = dev_get_tstats64,
   };
+static int ovpn_get_link_ksettings(struct net_device *dev,
+				   struct ethtool_link_ksettings *cmd)
+{
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0);
+	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0);
+	cmd->base.speed	= SPEED_1000;
Quoted 8 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 97

Quoted 5 lines
drivers/net/ovpn/main.c +5 -0
+	cmd->base.duplex = DUPLEX_FULL;
+	cmd->base.port = PORT_TP;
+	cmd->base.phy_address = 0;
+	cmd->base.transceiver = XCVR_INTERNAL;
+	cmd->base.autoneg = AUTONEG_DISABLE;

Why? It is a virtual device. Speed and duplex is meaningless. You could run this over FDDI, HIPPI, or RFC 1149? So why PORT_TP?

To be honest, I couldn't find any description to help me with deciding what to set there and I just used a value I saw in other Ethernet drivers.

Quoted 8 lines
Quoted 4 lines

Do you have any recommendation?
For the other fields: do you think they make sense? The speed value is always debatable...The actual speed depends on the transport interface and there might be multiple involved. Maybe SPEED_UNKNOWN is more appropriate?

Turn it around. What is your use case? What is using this information? I would just not implement this function. But maybe you know of something which actually requires it?

At the moment there is nothing on my side requiring this function.

I thought it was recommended to provide an implementation so that any potential user would be happy (some monitoring tool? some other module in the kernel?)

But if you think there is no meaningful use case for it, then I think it makes sense to just drop it, as we are filling it with random/virtual values.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

drivers/net/ovpn/main.c static const struct net_device_ops ovpn_netdev_ops = { · line 107

Quoted 2 lines
drivers/net/ovpn/main.c +1 -0
+	strscpy(info->bus_info, "ovpn", sizeof(info->bus_info));

This is also not accurate. There is no bus involved.

Quoted 8 lines
Quoted 4 lines

Should I just leave it empty then?

My concern is that a user expects $something and it will crash on my empty string. But if empty is allowed, I will just go with it.

Empty is allowed. The bridge uses "N/A", which i would say is also correct. tun/tap does however use "tun", so "ovpn" is not that different i supposes.

I just grepped through the kernel and I can see various patterns. I also found:

899 strscpy(info->bus_info, "batman", sizeof(info->bus_info));

:-)

All in all, I agree we can sick to "ovpn".

Regards,

Quoted 3 lines

Andrew

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#85 lore raw
In reply to Antonio Quartulli

Hello Antonio,

Quoted 59 lines · Antonio Quartulli

Hi all!

After the comments received last month, I reworked the large patch that I have previously sent and I came up with this patchset hoping to make the review process more human and less cumbersome.

Some features are stricly intertwined with each other, therefore I couldn't split everything up to the very last grain of salt, but I did my best to create a reasonable set of features that add up on top of each other.

I don't expect the kernel module to work between intermediate patches, therefore it is important that all patches are applied if you want to see something meaningful happening.

The following is just the introductory text from v1. It's a useful summary of what this new kernel module represents.

As an intereting note, an earlier version of this kernel module is already being used by quite some OpenVPN users out there claiming important improvements in terms of performance. By merging the ovpn kernel module upstream we were hoping to extend cooperation beyond the mere OpenVPN community.

===================================================================

ovpn is essentialy a device driver that allows creating a virtual network interface to handle the OpenVPN data channel. Any traffic entering the interface is encrypted, encapsulated and sent to the appropriate destination.

ovpn requires OpenVPN in userspace
to run along its side in order to be properly configured and maintained during its life cycle.

The ovpn interface can be created/destroyed and then configured via Netlink API.

Specifically OpenVPN in userspace will:
* create the ovpn interface
* establish the connection with one or more peers
* perform TLS handshake and negotiate any protocol parameter
* configure the ovpn interface with peer data (ip/port, keys, etc.)
* handle any subsequent control channel communication

I'd like to point out the control channel is fully handles in userspace. The idea is to keep the ovpn kernel module as simple as possible and let userspace handle all the non-data (non-fast-path) features.

NOTE: some of you may already know ovpn-dco the out-of-tree predecessor of ovpn. However, be aware that the two are not API compatible and therefore OpenVPN 2.6 will not work with this new ovpn module. More adjustments are required.

If you want to test the ovpn kernel module, for the time being you can use the testing tool ovpn-cli available here:
https://github.com/OpenVPN/ovpn-dco/tree/master/tests

Quoted 8 lines

The ovpn code can also be built as out-of-tree module and its code is available here https://github.com/OpenVPN/ovpn-dco (currently in the dev branch).

For more technical details please refer to the actual patches.

Any comment, concern or statement will be appreciated! Thanks a lot!!

Thank you for preparing this series. I briefly check it and now it looks much more promising!

I will do my best to do a careful review in a reasonable time, but please expected a delay in a few weeks :( It still a considerable amount of code for checking, despite it's well arrangement.

--
Sergey

maintainer
#86 lore raw
In reply to Antonio Quartulli
Quoted 22 lines · On Mon, 4 Mar 2024 16:08:51 +0100 Antonio Quartulli wrote:
 create mode 100644 drivers/net/ovpn/Makefile
 create mode 100644 drivers/net/ovpn/bind.c
 create mode 100644 drivers/net/ovpn/bind.h
 create mode 100644 drivers/net/ovpn/crypto.c
 create mode 100644 drivers/net/ovpn/crypto.h
 create mode 100644 drivers/net/ovpn/crypto_aead.c
 create mode 100644 drivers/net/ovpn/crypto_aead.h
 create mode 100644 drivers/net/ovpn/io.c
 create mode 100644 drivers/net/ovpn/io.h
 create mode 100644 drivers/net/ovpn/main.c
 create mode 100644 drivers/net/ovpn/main.h
 create mode 100644 drivers/net/ovpn/netlink.c
 create mode 100644 drivers/net/ovpn/netlink.h
 create mode 100644 drivers/net/ovpn/ovpnstruct.h
 create mode 100644 drivers/net/ovpn/packet.h
 create mode 100644 drivers/net/ovpn/peer.c
 create mode 100644 drivers/net/ovpn/peer.h
 create mode 100644 drivers/net/ovpn/pktid.c
 create mode 100644 drivers/net/ovpn/pktid.h
 create mode 100644 drivers/net/ovpn/proto.h
 create mode 100644 drivers/net/ovpn/skb.h
 create mode 100644 drivers/net/ovpn/socket.c
Quoted 8 lines
 create mode 100644 drivers/net/ovpn/socket.h
 create mode 100644 drivers/net/ovpn/stats.c
 create mode 100644 drivers/net/ovpn/stats.h
 create mode 100644 drivers/net/ovpn/tcp.c
 create mode 100644 drivers/net/ovpn/tcp.h
 create mode 100644 drivers/net/ovpn/udp.c
 create mode 100644 drivers/net/ovpn/udp.h
 create mode 100644 include/uapi/linux/ovpn.h

At a glance you seem to be missing:
- documentation
- YAML spec for the netlink protocol -
https://docs.kernel.org/next/userspace-api/netlink/specs.html
- some basic set of tests (or mention that you'll run your own CI and report results to us: https://netdev.bots.linux.dev/status.html)
:)

author maintainer
#87 lore raw
Quoted 32 lines · Jakub Kicinski
Quoted 22 lines · On Mon, 4 Mar 2024 16:08:51 +0100 Antonio Quartulli wrote:

create mode 100644 drivers/net/ovpn/Makefile create mode 100644 drivers/net/ovpn/bind.c create mode 100644 drivers/net/ovpn/bind.h create mode 100644 drivers/net/ovpn/crypto.c create mode 100644 drivers/net/ovpn/crypto.h create mode 100644 drivers/net/ovpn/crypto_aead.c create mode 100644 drivers/net/ovpn/crypto_aead.h create mode 100644 drivers/net/ovpn/io.c create mode 100644 drivers/net/ovpn/io.h create mode 100644 drivers/net/ovpn/main.c create mode 100644 drivers/net/ovpn/main.h create mode 100644 drivers/net/ovpn/netlink.c create mode 100644 drivers/net/ovpn/netlink.h create mode 100644 drivers/net/ovpn/ovpnstruct.h create mode 100644 drivers/net/ovpn/packet.h create mode 100644 drivers/net/ovpn/peer.c create mode 100644 drivers/net/ovpn/peer.h create mode 100644 drivers/net/ovpn/pktid.c create mode 100644 drivers/net/ovpn/pktid.h create mode 100644 drivers/net/ovpn/proto.h create mode 100644 drivers/net/ovpn/skb.h create mode 100644 drivers/net/ovpn/socket.c

Quoted 8 lines

create mode 100644 drivers/net/ovpn/socket.h
create mode 100644 drivers/net/ovpn/stats.c
create mode 100644 drivers/net/ovpn/stats.h
create mode 100644 drivers/net/ovpn/tcp.c
create mode 100644 drivers/net/ovpn/tcp.h
create mode 100644 drivers/net/ovpn/udp.c
create mode 100644 drivers/net/ovpn/udp.h
create mode 100644 include/uapi/linux/ovpn.h

Quoted 4 lines

At a glance you seem to be missing:
- documentation
- YAML spec for the netlink protocol -
https://docs.kernel.org/next/userspace-api/netlink/specs.html

triple ACK on the doc :) will add more.

Quoted 2 lines

- some basic set of tests (or mention that you'll run your own CI and report results to us: https://netdev.bots.linux.dev/status.html)

I currently have a small userspace tool (that implements the netlink APIs) and a script that by means of netns and this tool runs a bunch of tests.

Is there any requirement about how the test should work so that I can make it compatible with your harness? I will then push it to /tools/testing/selftests/ovpn so that you can pick it up.

Thanks a lot!

Regards,

Quoted 1 line

:)

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
maintainer
#88 lore raw
Quoted 10 lines · On Wed, 6 Mar 2024 16:44:41 +0100 Antonio Quartulli wrote:
Quoted 2 lines

- some basic set of tests (or mention that you'll run your own CI and report results to us: https://netdev.bots.linux.dev/status.html)

I currently have a small userspace tool (that implements the netlink APIs) and a script that by means of netns and this tool runs a bunch of tests.

Is there any requirement about how the test should work so that I can make it compatible with your harness? I will then push it to /tools/testing/selftests/ovpn so that you can pick it up.

Standard kernel selftest rules apply, we try to keep it pretty vanilla. Test should exit with 0 on success, 1 on fail, and 4 on skip. Skip if there are any missing dependencies in the test environment.

This has more deets on how we execute the tests:
https://github.com/linux-netdev/nipa/wiki/How-to-run-netdev-selftests-CI-style

author maintainer
#89 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 6 Mar 2024 16:44:41 +0100 Antonio Quartulli wrote:
Quoted 2 lines

- some basic set of tests (or mention that you'll run your own CI and report results to us: https://netdev.bots.linux.dev/status.html)

I currently have a small userspace tool (that implements the netlink APIs) and a script that by means of netns and this tool runs a bunch of tests.

Quoted 7 lines
Quoted 3 lines

Is there any requirement about how the test should work so that I can make it compatible with your harness? I will then push it to /tools/testing/selftests/ovpn so that you can pick it up.

Standard kernel selftest rules apply, we try to keep it pretty vanilla. Test should exit with 0 on success, 1 on fail, and 4 on skip. Skip if there are any missing dependencies in the test environment.

Perfect

Quoted 3 lines

This has more deets on how we execute the tests:
https://github.com/linux-netdev/nipa/wiki/How-to-run-netdev-selftests-CI-style

Thanks a lot for this!

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.