Introducing OpenVPN Data Channel Offload
Subsystems (6)
Antonio Quartulli
maintainerv5 cover Introducing OpenVPN Data Channel Offload
Hi all,
here I am with v5 of the ovpn patchset.
The only differences from v4 are:
* moved the ovpn kselftest to the net/ subfolder
* renamed kselftest scripts to something more reasonable
* removed kselftest wrapper script run.sh, now each test runs individually
* made checkpatch happy regarding the ovpn-cli.c kselftest tool
* CCed more maintainers to a few individual patches, as requested by checkpatch
* fixed one remaining kdoc warning in patch 4
* properly updated MAINTAINERS file in patch 4
* fixed clang build warning in patch 22
(The only remaining failure on patchwork is about series being larger than 15 patches)
Nothing else has been touched.
For covenience, here is the summary I sent for v4:
Thanks to Sabrina's feedback, several parts of the code are now simpler than before.
There is an extra kernel patch, compared to v3, that is 02/25. This patch is required to properly handle virtual devices not implementing a dellink upon invocation of rtnl_unregister_ops().
Here is an overview of the changesfrom v3, sorted more or less by importance (very minor changes haven't been mentioned):
* got rid of the TX/RX queues entirely
* got rid of the workqueues entirely
** the only sporadic scheduled event, TCP TX retry, is scheduled on the
global kernel queue
* use strparser for receiving TCP data
* use gro_cells and get rid of any napi and netif_rx_ring related code
* crypto code now follows the classic async paradigma
* removed synchronize_net()
* avoided unregister_netdevice() double call
* added empty rtnl_link_ops to ensure ifaces are destroyed upon ns exit
* convert pkt counters to 64 bit
* counted dropped packets (in core stats)
* simplified peer lookup routines (no need to hold ref every time)
* simplified TCP recvmsg implementation
* peer collection for MultiPeer mode is now allocated dynamically
* use GFP_ATOMIC for sending nl notifications out of process context
* documented how EALREADY and EBUSY are used in UDP socket attach
* used GENL_REQ_ATTR_CHECK in ovpn_get_dev_from_attrs/pre_doit
* used NL_SET_BAD_ATTR/GENL_SET_ERR_MSG in ovpn_get_dev_from_attrs/pre_doit
* used NL_SET_ERR_MSG_MOD for reporting back error strings
* dropped netlink 'pad' attribute
* used genlmsg_iput
* set pcpu_stat_type to let core handle stats internally
* used nla_put_string/in_addr/in6_addr when possibly
* used ipv6 helpers when possible (ipv6_addr_equal/any)
* added various calls to DEBUG_NET_WARN_ON_ONCE/WARN_ON
* removed unworthy error message in case of netlink message size errors
* used -EOPNOTSUPP instead of -ENOTSUPP
* userspace testing tool improved
* various code rearrangments based on provded feedback ..
The latest code can also be found at:
https://github.com/OpenVPN/linux-kernel-ovpn
Below is the original description posted with the first patchest:
===================================================================
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.
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 (25):
netlink: add NLA_POLICY_MAX_LEN macro
rtnetlink: don't crash on unregister if no dellink exists
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: keep carrier always on
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
testing/selftest: add test tool and scripts for ovpn module
Documentation/netlink/specs/ovpn.yaml | 327 +++ MAINTAINERS | 8 + drivers/net/Kconfig | 14 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 58 + drivers/net/ovpn/bind.h | 119 ++ drivers/net/ovpn/crypto.c | 161 ++ drivers/net/ovpn/crypto.h | 138 ++ drivers/net/ovpn/crypto_aead.c | 347 ++++ drivers/net/ovpn/crypto_aead.h | 30 + drivers/net/ovpn/io.c | 438 ++++ drivers/net/ovpn/io.h | 21 + drivers/net/ovpn/main.c | 360 ++++ drivers/net/ovpn/main.h | 29 + drivers/net/ovpn/netlink-gen.c | 206 ++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 960 +++++++++ drivers/net/ovpn/netlink.h | 31 + drivers/net/ovpn/ovpnstruct.h | 52 + drivers/net/ovpn/packet.h | 40 + drivers/net/ovpn/peer.c | 1047 ++++++++++ drivers/net/ovpn/peer.h | 202 ++ drivers/net/ovpn/pktid.c | 130 ++ drivers/net/ovpn/pktid.h | 87 + drivers/net/ovpn/proto.h | 106 + drivers/net/ovpn/skb.h | 56 + drivers/net/ovpn/socket.c | 165 ++ drivers/net/ovpn/socket.h | 54 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 47 + drivers/net/ovpn/tcp.c | 502 +++++ drivers/net/ovpn/tcp.h | 42 + drivers/net/ovpn/udp.c | 404 ++++ drivers/net/ovpn/udp.h | 26 + include/net/netlink.h | 1 + include/uapi/linux/ovpn.h | 108 + include/uapi/linux/udp.h | 1 + net/core/rtnetlink.c | 8 +- tools/net/ynl/ynl-gen-c.py | 2 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 17 + tools/testing/selftests/net/ovpn/config | 8 + .../selftests/net/ovpn/data-test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/data-test.sh | 130 ++ tools/testing/selftests/net/ovpn/data64.key | 5 + .../testing/selftests/net/ovpn/float-test.sh | 115 ++ tools/testing/selftests/net/ovpn/ovpn-cli.c | 1787 +++++++++++++++++ .../testing/selftests/net/ovpn/tcp_peers.txt | 1 + .../testing/selftests/net/ovpn/udp_peers.txt | 5 + 51 files changed, 8490 insertions(+), 2 deletions(-) create mode 100644 Documentation/netlink/specs/ovpn.yaml 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-gen.c create mode 100644 drivers/net/ovpn/netlink-gen.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 create mode 100644 tools/testing/selftests/net/ovpn/.gitignore create mode 100644 tools/testing/selftests/net/ovpn/Makefile create mode 100644 tools/testing/selftests/net/ovpn/config create mode 100755 tools/testing/selftests/net/ovpn/data-test-tcp.sh create mode 100755 tools/testing/selftests/net/ovpn/data-test.sh create mode 100644 tools/testing/selftests/net/ovpn/data64.key create mode 100755 tools/testing/selftests/net/ovpn/float-test.sh create mode 100644 tools/testing/selftests/net/ovpn/ovpn-cli.c create mode 100644 tools/testing/selftests/net/ovpn/tcp_peers.txt create mode 100644 tools/testing/selftests/net/ovpn/udp_peers.txt
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 1/25 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.
The netlink generator for YAML specs has been extended accordingly.
--- include/net/netlink.h | 1 + tools/net/ynl/ynl-gen-c.py | 2 ++ 2 files changed, 3 insertions(+)
include/net/netlink.h +1 -0
@@ -469,6 +469,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
tools/net/ynl/ynl-gen-c.py +2 -0
@@ -466,6 +466,8 @@ class TypeBinary(Type): def _attr_policy(self, policy): if 'exact-len' in self.checks: mem = 'NLA_POLICY_EXACT_LEN(' + str(self.get_limit('exact-len')) + ')' + elif 'max-len' in self.checks: + mem = 'NLA_POLICY_MAX_LEN(' + str(self.get_limit('max-len')) + ')' else: mem = '{ ' if len(self.checks) == 1 and 'min-len' in self.checks:
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 2/25 rtnetlink: don't crash on unregister if no dellink exists
In reply to Antonio Quartullirtnl_unregister_ops calls __rtnl_links_kill which calls unconditionally ops->dellink on any related interface.
However, if a module hasn't implemented dellink, this invocation will crash.
Add a check to avoid calling dellink when NULL and rather invoke netdevice_unregister_queue to schedule unregistering the device as usual.
--- net/core/rtnetlink.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/rtnetlink.c +6 -2
@@ -453,8 +453,12 @@ static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)LIST_HEAD (list_kill);for_each_netdev (net, dev) { -if (dev->rtnl_link_ops == ops) - ops->dellink (dev, &list_kill); +if (dev->rtnl_link_ops == ops) { +if (ops->dellink) + ops->dellink (dev, &list_kill); +else +unregister_netdevice_queue (dev, &list_kill); + } }unregister_netdevice_many (&list_kill); }
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 3/25 net: introduce OpenVPN Data Channel Offload (ovpn)
In reply to Antonio QuartulliOpenVPN 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 | 7 +++ drivers/net/Kconfig | 13 +++++ drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 11 ++++ drivers/net/ovpn/io.c | 22 ++++++++ drivers/net/ovpn/io.h | 15 ++++++ drivers/net/ovpn/main.c | 109 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/main.h | 15 ++++++ include/uapi/linux/udp.h | 1 + 9 files changed, 194 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 +7 -0
@@ -16908,6 +16908,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/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/ + P54 WIRELESS DRIVER M: Christian Lamparter <[email protected]> L: [email protected]
drivers/net/Kconfig +13 -0
@@ -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
@@ -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
@@ -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 +22 -0
@@ -0,0 +1,22 @@ +// 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 <linux/netdevice.h> +#include <linux/skbuff.h> + +#include "io.h" + +/* Send user data to the network +*/ +netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) +{ +skb_tx_error (skb); +kfree_skb (skb); +return NET_XMIT_DROP ; +}
drivers/net/ovpn/io.h new file +15 -0
@@ -0,0 +1,15 @@ +/* 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_ + +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 +109 -0
109 lines not on this page · raw message
drivers/net/ovpn/main.h new file +15 -0
@@ -0,0 +1,15 @@ +/* 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_ + +bool ovpn_dev_is_valid (const struct net_device *dev); + +#endif /* _NET_OVPN_MAIN_H_ */
include/uapi/linux/udp.h +1 -0
@@ -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.44.2
Antonio Quartulli
maintainerPATCH v5 4/25 ovpn: add basic netlink support
In reply to Antonio QuartulliThis commit introduces basic netlink support with family registration/unregistration functionalities and stub pre/post-doit.
More importantly it introduces the YAML uAPI description along with its auto-generated files:
- include/uapi/linux/ovpn.h
- drivers/net/ovpn/netlink-gen.c
- drivers/net/ovpn/netlink-gen.h
--- Documentation/netlink/specs/ovpn.yaml | 327 ++++++++++++++++++++++++++ MAINTAINERS | 1 + drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/main.c | 14 ++ drivers/net/ovpn/netlink-gen.c | 206 ++++++++++++++++ drivers/net/ovpn/netlink-gen.h | 41 ++++ drivers/net/ovpn/netlink.c | 153 ++++++++++++ drivers/net/ovpn/netlink.h | 15 ++ drivers/net/ovpn/ovpnstruct.h | 21 ++ include/uapi/linux/ovpn.h | 108 +++++++++ 10 files changed, 888 insertions(+) create mode 100644 Documentation/netlink/specs/ovpn.yaml create mode 100644 drivers/net/ovpn/netlink-gen.c create mode 100644 drivers/net/ovpn/netlink-gen.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 include/uapi/linux/ovpn.h
Documentation/netlink/specs/ovpn.yaml new file +327 -0
@@ -0,0 +1,327 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) +# +# Author: Antonio Quartulli <[email protected]> +# +# Copyright (c) 2024, OpenVPN Inc. +# + +name: ovpn + +protocol: genetlink + +doc: Netlink protocol to control OpenVPN network devices + +definitions: + - + type: const + name: nonce-tail-size + value: 8 + - + type: enum + name: cipher-alg + value-start: 0 + entries: [ none, aes-gcm, chacha20_poly1305 ] + - + type: enum + name: del-peer_reason + value-start: 0 + entries: [ teardown, userspace, expired, transport-error, transport_disconnect ] + - + type: enum + name: key-slot + value-start: 0 + entries: [ primary, secondary ] + - + type: enum + name: mode + value-start: 0 + entries: [ p2p, mp ] + +attribute-sets: + - + name: peer + attributes: + - + name: id + type: u32 + doc: | + The unique Id of the peer. To be used to identify peers during + operations + checks: + max: 0xFFFFFF + - + name: sockaddr-remote + type: binary + doc: | + The sockaddr_in/in6 object identifying the remote address/port of the + peer + - + name: socket + type: u32 + doc: The socket to be used to communicate with the peer + - + name: vpn-ipv4 + type: u32 + doc: The IPv4 assigned to the peer by the server + display-hint: ipv4 + - + name: vpn-ipv6 + type: binary + doc: The IPv6 assigned to the peer by the server + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-ip + type: binary + doc: The local IP to be used to send packets to the peer (UDP only) + checks: + max-len: 16 + - + name: local-port + type: u32 + doc: The local port to be used to send packets to the peer (UDP only) + checks: + min: 1 + max: u16-max + - + name: keepalive-interval + type: u32 + doc: | + The number of seconds after which a keep alive message is sent to the + peer + - + name: keepalive-timeout + type: u32 + doc: | + The number of seconds from the last activity after which the peer is + assumed dead + - + name: del-reason + type: u32 + doc: The reason why a peer was deleted + enum: del-peer_reason + - + name: keyconf + type: nest + doc: Peer specific cipher configuration + nested-attributes: keyconf + - + name: vpn-rx_bytes + type: uint + doc: Number of bytes received over the tunnel + - + name: vpn-tx_bytes + type: uint + doc: Number of bytes transmitted over the tunnel + - + name: vpn-rx_packets + type: uint + doc: Number of packets received over the tunnel + - + name: vpn-tx_packets + type: uint + doc: Number of packets transmitted over the tunnel + - + name: link-rx_bytes + type: uint + doc: Number of bytes received at the transport level + - + name: link-tx_bytes + type: uint + doc: Number of bytes transmitted at the transport level + - + name: link-rx_packets + type: u32 + doc: Number of packets received at the transport level + - + name: link-tx_packets + type: u32 + doc: Number of packets transmitted at the transport level + - + name: keyconf + attributes: + - + name: slot + type: u32 + doc: The slot where the key should be stored + enum: key-slot + - + name: key-id + doc: | + The unique ID for the key. Used to fetch the correct key upon + decryption + type: u32 + checks: + max: 2 + - + name: cipher-alg + type: u32 + doc: The cipher to be used when communicating with the peer + enum: cipher-alg + - + name: encrypt-dir + type: nest + doc: Key material for encrypt direction + nested-attributes: keydir + - + name: decrypt-dir + type: nest + doc: Key material for decrypt direction + nested-attributes: keydir + - + name: keydir + attributes: + - + name: cipher-key + type: binary + doc: The actual key to be used by the cipher + checks: + max-len: 256 + - + name: nonce-tail + type: binary + doc: | + Random nonce to be concatenated to the packet ID, in order to + obtain the actua cipher IV + checks: + exact-len: nonce-tail-size + - + name: ovpn + attributes: + - + name: ifindex + type: u32 + doc: Index of the ovpn interface to operate on + - + name: ifname + type: string + doc: Name of the ovpn interface that is being created + - + name: mode + type: u32 + enum: mode + doc: | + Oper mode instructing an interface to act as Point2Point or + MultiPoint + - + name: peer + type: nest + doc: | + The peer object containing the attributed of interest for the specific + operation + nested-attributes: peer + +operations: + list: + - + name: new-iface + attribute-set: ovpn + flags: [ admin-perm ] + doc: Create a new interface + do: + request: + attributes: + - ifname + - mode + reply: + attributes: + - ifname + - + name: del-iface + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing interface + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - + name: set-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add or modify a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: get-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve data about existing remote peers (or a specific one) + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + reply: + attributes: + - peer + dump: + request: + attributes: + - ifindex + reply: + attributes: + - peer + - + name: del-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: set-key + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add or modify a cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: swap-keys + attribute-set: ovpn + flags: [ admin-perm ] + doc: Swap primary and secondary session keys for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: del-key + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + +mcast-groups: + list: + - + name: peers
MAINTAINERS +1 -0
@@ -16914,6 +16914,7 @@ 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]>
drivers/net/ovpn/Makefile +2 -0
@@ -9,3 +9,5 @@ obj-$(CONFIG_OVPN) := ovpn.o ovpn-y += main.o ovpn-y += io.o +ovpn-y += netlink.o +ovpn-y += netlink-gen.o
drivers/net/ovpn/main.c +14 -0
@@ -7,12 +7,16 @@* James Yonan <[email protected]> */ +#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> #include <linux/version.h> #include <net/rtnetlink.h> +#include <uapi/linux/ovpn.h> +#include "ovpnstruct.h" #include "main.h" +#include "netlink.h" #include "io.h" /* Driver info */
@@ -34,6 +38,7 @@ bool ovpn_dev_is_valid(const struct net_device *dev)* therefore ifaces should be destroyed when exiting a netns */ static struct rtnl_link_ops ovpn_link_ops = { + .kind =OVPN_FAMILY_NAME , }; static int ovpn_netdev_notifier_call(struct notifier_block *nb,
@@ -86,8 +91,16 @@ static int __init ovpn_init(void) goto unreg_netdev; } + err =ovpn_nl_register (); +if (err) { +pr_err ("ovpn: can't register netlink family: %d\n" , err); + goto unreg_rtnl; + } +return 0 ; +unreg_rtnl: +rtnl_link_unregister (&ovpn_link_ops); unreg_netdev:unregister_netdevice_notifier (&ovpn_netdev_notifier);return err;
@@ -95,6 +108,7 @@ static int __init ovpn_init(void) static __exit void ovpn_cleanup(void) { +ovpn_nl_unregister ();rtnl_link_unregister (&ovpn_link_ops);unregister_netdevice_notifier (&ovpn_netdev_notifier);
drivers/net/ovpn/netlink-gen.c new file +206 -0
206 lines not on this page · raw message
drivers/net/ovpn/netlink-gen.h new file +41 -0
41 lines not on this page · raw message
drivers/net/ovpn/netlink.c new file +153 -0
153 lines not on this page · raw message
drivers/net/ovpn/netlink.h new file +15 -0
@@ -0,0 +1,15 @@ +/* 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_ + +int ovpn_nl_register (void ); +void ovpn_nl_unregister (void ); + +#endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/ovpnstruct.h new file +21 -0
@@ -0,0 +1,21 @@ +/* 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_ + +/** +* struct ovpn_struct - per ovpn interface state +* @dev: the actual netdev representing the tunnel +*/ +struct ovpn_struct { +struct net_device *dev; +}; + +#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
include/uapi/linux/ovpn.h new file +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 5/25 ovpn: add basic interface creation/destruction/management routines
In reply to Antonio QuartulliAdd basic infrastructure for handling ovpn interfaces.
--- drivers/net/ovpn/main.c | 162 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/main.h | 10 +++ drivers/net/ovpn/ovpnstruct.h | 8 ++ drivers/net/ovpn/packet.h | 40 +++++++++ 4 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ovpn/packet.h
drivers/net/ovpn/main.c +159 -3
@@ -10,19 +10,62 @@#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> +#include <linux/inetdevice.h> #include <linux/version.h> +#include <net/ip.h> #include <net/rtnetlink.h> +#include <uapi/linux/if_arp.h> #include <uapi/linux/ovpn.h> #include "ovpnstruct.h" #include "main.h" #include "netlink.h" #include "io.h" +#include "packet.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"#define DRV_COPYRIGHT "(C) 2020-2024 OpenVPN, Inc." +/** +* ovpn_struct_init - Initialize the netdevice private area +* @dev: the device to initialize +* @mode: device operation mode (i.e. p2p, mp, ..) +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_struct_init (struct net_device *dev,enum ovpn_mode mode) +{ +struct ovpn_struct *ovpn =netdev_priv (dev); + + ovpn->dev = dev; + ovpn->mode = mode; + +return 0 ; +} + +static void ovpn_struct_free (struct net_device *net) +{ +} + +static int ovpn_net_open (struct net_device *dev) +{ +netif_tx_start_all_queues (dev); +return 0 ; +} + +static int ovpn_net_stop (struct net_device *dev) +{ +netif_tx_stop_all_queues (dev); +return 0 ; +} + +static const struct net_device_ops ovpn_netdev_ops = { + .ndo_open = ovpn_net_open, + .ndo_stop = ovpn_net_stop, + .ndo_start_xmit = ovpn_net_xmit, +}; +/** * ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' * @dev: the interface to check
@@ -41,30 +84,143 @@ static struct rtnl_link_ops ovpn_link_ops = { .kind = OVPN_FAMILY_NAME, }; +static void ovpn_setup (struct net_device *dev) +{ +/* compute the overhead considering AEAD encryption */ +const int overhead =sizeof (u32) +NONCE_WIRE_SIZE +16 + +sizeof (struct udphdr ) + +max (sizeof (struct ipv6hdr ),sizeof (struct iphdr )); + +netdev_features_t feat =NETIF_F_SG |NETIF_F_LLTX | +NETIF_F_HW_CSUM |NETIF_F_RXCSUM | +NETIF_F_GSO |NETIF_F_GSO_SOFTWARE | +NETIF_F_HIGHDMA ; + + dev->needs_free_netdev = true; + + dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; + + dev->netdev_ops = &ovpn_netdev_ops; + dev->rtnl_link_ops = &ovpn_link_ops; + + dev->priv_destructor = ovpn_struct_free; + + dev->hard_header_len =0 ; + dev->addr_len =0 ; + dev->mtu =ETH_DATA_LEN - overhead; + dev->min_mtu =IPV4_MIN_MTU ; + dev->max_mtu =IP_MAX_MTU - overhead; + + dev->type =ARPHRD_NONE ; + dev->flags =IFF_POINTOPOINT |IFF_NOARP ; + + dev->features |= feat; + dev->hw_features |= feat; + dev->hw_enc_features |= feat; + + dev->needed_headroom =OVPN_HEAD_ROOM ; + dev->needed_tailroom =OVPN_MAX_PADDING ; +} + +/** +* ovpn_iface_create - create and initialize a new 'ovpn' netdevice +* @name: the name of the new device +* @mode: the OpenVPN mode to set this device to +* @net: the netns this device should be created in +* +* A new netdevice is created and registered. +* Its private area is initialized with an empty ovpn_struct object. +* +* Return: a pointer to the new device on success or a negative error code +* otherwise +*/ +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode, +struct net *net) +{ +struct net_device *dev; +int ret; + + dev =alloc_netdev (sizeof (struct ovpn_struct ), name,NET_NAME_USER , + ovpn_setup); +if (!dev) +return ERR_PTR (-ENOMEM ); + +dev_net_set (dev, net); + + ret =ovpn_struct_init (dev, mode); +if (ret <0 ) + goto err; + +rtnl_lock (); + ret =register_netdevice (dev); +if (ret <0 ) { +netdev_err (dev,"cannot register interface: %d\n" , ret); +rtnl_unlock (); + goto err; + } +/* turn carrier explicitly off after registration, this way state is +* clearly defined +*/ +netif_carrier_off (dev); +rtnl_unlock (); + +return dev; + +err: +free_netdev (dev); +return ERR_PTR (ret); +} + +/** +* ovpn_iface_destruct - tear down netdevice +* @ovpn: the ovpn instance objected related to the interface to tear down +* +* This function takes care of tearing down an ovpn device and can be invoked +* internally or upon UNREGISTER netdev event +*/ +void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; +} +static int ovpn_netdev_notifier_call (struct notifier_block *nb,unsigned long state,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct ovpn_struct *ovpn;if (!ovpn_dev_is_valid (dev))return NOTIFY_DONE ; + ovpn =netdev_priv (dev); +switch (state) {case NETDEV_REGISTER : -/* add device to internal list for later destruction upon -* unregistration -*/ + ovpn->registered = true;break ;case NETDEV_UNREGISTER : +/* twiddle thumbs on netns device moves */ +if (dev->reg_state !=NETREG_UNREGISTERING ) +break ; +/* can be delivered multiple times, so check registered flag, * then destroy the interface */ +if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);break ;case NETDEV_POST_INIT :case NETDEV_GOING_DOWN :case NETDEV_DOWN :case NETDEV_UP :case NETDEV_PRE_UP : +break ;default :return NOTIFY_DONE ; }
drivers/net/ovpn/main.h +10 -0
@@ -10,6 +10,16 @@#ifndef _NET_OVPN_MAIN_H_#define _NET_OVPN_MAIN_H_ +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode, +struct net *net); +void ovpn_iface_destruct (struct ovpn_struct *ovpn);bool ovpn_dev_is_valid (const struct net_device *dev); +#define SKB_HEADER_LEN \ + (max(sizeof(struct iphdr), sizeof(struct ipv6hdr)) + \ + sizeof(struct udphdr) + NET_SKB_PAD) + +#define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4) +#define OVPN_MAX_PADDING 16 +#endif /* _NET_OVPN_MAIN_H_ */
drivers/net/ovpn/ovpnstruct.h +8 -0
@@ -10,12 +10,20 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <uapi/linux/ovpn.h> +/** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel +* @registered: whether dev is still registered with netdev or not +* @mode: device operation mode (i.e. p2p, mp, ..) +* @dev_list: entry for the module wide device list */ struct ovpn_struct {struct net_device *dev; +bool registered; +enum ovpn_mode mode; +struct list_head dev_list; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/packet.h new file +40 -0
@@ -0,0 +1,40 @@ +/* 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_PACKET_H_ +#define _NET_OVPN_PACKET_H_ + +/* When the OpenVPN protocol is ran in AEAD mode, use +* the OpenVPN packet ID as the AEAD nonce: +* +* 00000005 521c3b01 4308c041 +* [seq # ] [ nonce_tail ] +* [ 12-byte full IV ] -> NONCE_SIZE +* [4-bytes -> NONCE_WIRE_SIZE +* on wire] +*/ + +/* OpenVPN nonce size */ +#define NONCE_SIZE 12 + +/* OpenVPN nonce size reduced by 8-byte nonce tail -- this is the +* size of the AEAD Associated Data (AD) sent over the wire +* and is normally the head of the IV +*/ +#define NONCE_WIRE_SIZE (NONCE_SIZE - sizeof(struct ovpn_nonce_tail)) + +/* Last 8 bytes of AEAD nonce +* Provided by userspace and usually derived from +* key material generated during TLS handshake +*/ +struct ovpn_nonce_tail { +u8 u8[OVPN_NONCE_TAIL_SIZE ]; +}; + +#endif /* _NET_OVPN_PACKET_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 05/25] ovpn: add basic interface creation/destruction/management routines
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 90
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/main.c +4
-0+static void ovpn_setup (struct net_device *dev) +{ +/* compute the overhead considering AEAD encryption */ + const int overhead = sizeof(u32) + NONCE_WIRE_SIZE + 16 +
Is that 16 equal to OVPN_MAX_PADDING? Or some other constant that would help figure out the details of that overhead?
Quoted 4 lines
drivers/net/ovpn/main.c +4
-0+sizeof (struct udphdr ) + +max (sizeof (struct ipv6hdr ),sizeof (struct iphdr )); + + netdev_features_t feat = NETIF_F_SG | NETIF_F_LLTX |
[...]
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 118
Quoted 2 lines
drivers/net/ovpn/main.c +2
-0+ dev->features |= feat; + dev->hw_features |= feat;
I'm not sure we want NETIF_F_LLTX to be part of hw_features, I think it's more of a property of the device than something we want to let users toggle. AFAICT no other virtual driver does that.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 05/25] ovpn: add basic interface creation/destruction/management routines
Hi,
Quoted 8 lines · Sabrina Dubroca
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 90
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/main.c +4
-0+static void ovpn_setup (struct net_device *dev) +{ +/* compute the overhead considering AEAD encryption */ + const int overhead = sizeof(u32) + NONCE_WIRE_SIZE + 16 +Is that 16 equal to
OVPN_MAX_PADDING? Or some other constant that would help figure out the details of that overhead?
That's the size of the auth tag.
Sadly the explanation of the OpenVPN header (with AEAD encryption) is only described in a comment inside ovpn_aead_encrypt().
Here the comment is much "slimmer"..
In crypto_aead.c it is defined as AUTH_TAG_SIZE. Maybe I could export that define and reuse it here.
Same goes for the "4" in ovpn_aead_encap_overhead(), which is actually NONCE_WIRE_SIZE.
Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/main.c +4
-0+sizeof (struct udphdr ) + +max (sizeof (struct ipv6hdr ),sizeof (struct iphdr )); + + netdev_features_t feat = NETIF_F_SG | NETIF_F_LLTX |[...]
Quoted 6 lines
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 118
Quoted 2 lines
drivers/net/ovpn/main.c +2
-0+ dev->features |= feat; + dev->hw_features |= feat;I'm not sure we want
NETIF_F_LLTXto be part ofhw_features, I think it's more of a property of the device than something we want to let users toggle. AFAICT no other virtual driver does that.
I agree. It should *not* be part of hw_features. I'll remove it from 'feat' and assign it only to '->features'.
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 6/25 ovpn: implement interface creation/destruction via netlink
In reply to Antonio QuartulliAllow userspace to create and destroy an interface using netlink commands.
--- drivers/net/ovpn/main.h | 2 ++ drivers/net/ovpn/netlink.c | 53 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-)
drivers/net/ovpn/main.h +2 -0
@@ -10,6 +10,8 @@#ifndef _NET_OVPN_MAIN_H_#define _NET_OVPN_MAIN_H_ +#define OVPN_DEFAULT_IFNAME "ovpn%d" +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode,struct net *net);void ovpn_iface_destruct (struct ovpn_struct *ovpn);
drivers/net/ovpn/netlink.c +51 -2
69 lines not on this page · raw message
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 06/25] ovpn: implement interface creation/destruction via netlink
Quoted 2 lines · Antonio Quartulli
int
ovpn_nl_new_iface_doit(struct sk_buff *skb, struct genl_info *info){
[...]
Quoted 5 lines
drivers/net/ovpn/netlink.c +5
-0+ msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_iput (msg, info);
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 124
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+if (!hdr) { +nlmsg_free (msg); +return -ENOBUFS ; + } + +if (nla_put_string (msg,OVPN_A_IFNAME , dev->name)) { +genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + }
Maybe the ifindex as well? The notifications in later patches use that rather than the name, but I don't know how the client handles this reply.
Quoted 4 lines
drivers/net/ovpn/netlink.c +3
-0+genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 06/25] ovpn: implement interface creation/destruction via netlink
Quoted 14 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
int ovpn_nl_new_iface_doit(struct sk_buff *skb, struct genl_info *info) {[...]
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_iput (msg, info); +if (!hdr) { +nlmsg_free (msg); +return -ENOBUFS ; + } +
Quoted 8 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 124
Quoted 5 lines
drivers/net/ovpn/netlink.c +5
-0+if (nla_put_string (msg,OVPN_A_IFNAME , dev->name)) { +genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + }Maybe the ifindex as well? The notifications in later patches use that rather than the name, but I don't know how the client handles this reply.
Userspace will just throw the name in if_nametoindex().
However passing both doesn't hurt anybody, and actually spares the conversion in userspace.
will add ifindex too.
Thanks!
Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c +3
-0+genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 7/25 ovpn: keep carrier always on
In reply to Antonio QuartulliAn ovpn interface will keep carrier always on and let the user decide when an interface should be considered disconnected.
This way, even if an ovpn interface is not connected to any peer, it can still retain all IPs and routes and thus prevent any data leak.
--- drivers/net/ovpn/main.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/net/ovpn/main.c +7 -0
@@ -50,6 +50,13 @@ static void ovpn_struct_free(struct net_device *net)static int ovpn_net_open (struct net_device *dev) { +/* ovpn keeps the carrier always on to avoid losing IP or route +* configuration upon disconnection. This way it can prevent leaks +* of traffic outside of the VPN tunnel. +* The user may override this behaviour by tearing down the interface +* manually. +*/ +netif_carrier_on (dev);netif_tx_start_all_queues (dev);return 0 ; }
Signature · 1 line
2.44.2
Re: [PATCH net-next v5 07/25] ovpn: keep carrier always on
Quoted 8 lines · Antonio Quartulli
An ovpn interface will keep carrier always on and let the user decide when an interface should be considered disconnected.
This way, even if an ovpn interface is not connected to any peer, it can still retain all IPs and routes and thus prevent any data leak.
Signed-off-by Antonio Quartulli <[email protected]>
Andrew
Antonio Quartulli
maintainerPATCH v5 8/25 ovpn: introduce the ovpn_peer object
In reply to Antonio QuartulliAn 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 | 58 ++++++ drivers/net/ovpn/bind.h | 119 +++++++++++ drivers/net/ovpn/main.c | 12 ++ drivers/net/ovpn/main.h | 2 + drivers/net/ovpn/ovpnstruct.h | 4 + drivers/net/ovpn/peer.c | 358 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 82 ++++++++ 8 files changed, 637 insertions(+) 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
@@ -7,7 +7,9 @@ # 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 += netlink-gen.o +ovpn-y += peer.o
drivers/net/ovpn/bind.c new file +58 -0
58 lines not on this page · raw message
drivers/net/ovpn/bind.h new file +119 -0
119 lines not on this page · raw message
drivers/net/ovpn/main.c +12 -0
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> +//#include <linux/rcupdate.h> #include <linux/version.h> #include <net/ip.h> #include <net/rtnetlink.h>
@@ -22,6 +23,7 @@#include "netlink.h" #include "io.h" #include "packet.h" +#include "peer.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -40,6 +42,7 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->dev = dev; ovpn->mode = mode; +spin_lock_init (&ovpn->lock);return 0 ; }
@@ -48,6 +51,11 @@ static void ovpn_struct_free(struct net_device *net) { } +static int ovpn_net_init (struct net_device *dev) +{ +return 0 ; +} +static int ovpn_net_open (struct net_device *dev) {/* ovpn keeps the carrier always on to avoid losing IP or route
@@ -68,6 +76,7 @@ static int ovpn_net_stop(struct net_device *dev) }static const struct net_device_ops ovpn_netdev_ops = { + .ndo_init = ovpn_net_init, .ndo_open = ovpn_net_open, .ndo_stop = ovpn_net_stop, .ndo_start_xmit = ovpn_net_xmit,
@@ -192,6 +201,9 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn)netif_carrier_off (ovpn->dev); ovpn->registered = false; + +if (ovpn->mode ==OVPN_MODE_P2P ) +ovpn_peer_release_p2p (ovpn); } static int ovpn_netdev_notifier_call(struct notifier_block *nb,
drivers/net/ovpn/main.h +2 -0
@@ -24,4 +24,6 @@ bool ovpn_dev_is_valid(const struct net_device *dev);#define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4)#define OVPN_MAX_PADDING 16 +#define OVPN_QUEUE_LEN 1024 +#endif /* _NET_OVPN_MAIN_H_ */
drivers/net/ovpn/ovpnstruct.h +4 -0
@@ -17,12 +17,16 @@* @dev: the actual netdev representing the tunnel * @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) +* @lock: protect this object +* @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list */ struct ovpn_struct {struct net_device *dev;bool registered;enum ovpn_mode mode; +spinlock_t lock;/* protect writing to the ovpn_struct object */ +struct ovpn_peer __rcu *peer;struct list_head dev_list; };
drivers/net/ovpn/peer.c new file +358 -0
358 lines not on this page · raw message
drivers/net/ovpn/peer.h new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 08/25] ovpn: introduce the ovpn_peer object
drivers/net/ovpn/bind.h line 32
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/bind.h new file +11
-0+/** +* struct ovpn_sockaddr - basic transport layer address +* @in4: IPv4 address +* @in6: IPv6 address +*/ +struct ovpn_sockaddr { +union { +struct sockaddr_in in4; +struct sockaddr_in6 in6; + }; +};
nit: wrapping the anonymous union in a struct that contains nothing else is not that useful.
drivers/net/ovpn/bind.h line 43
Quoted 10 lines
drivers/net/ovpn/bind.h new file +10
-0+/** +* struct ovpn_bind - remote peer binding +* @sa: the remote peer sockaddress +* @local: local endpoint used to talk to the peer +* @local.ipv4: local IPv4 used to talk to the peer +* @local.ipv6: local IPv6 used to talk to the peer +* @rcu: used to schedule RCU cleanup job +*/ +struct ovpn_bind { +struct ovpn_sockaddr sa;/* remote sockaddr */
nit: then maybe call it "peer" or "remote" instead of sa?
Quoted 8 lines
drivers/net/ovpn/bind.h new file +8
-0+union { +struct in_addr ipv4; +struct in6_addr ipv6; + } local; + +struct rcu_head rcu; +}; +
[...]
Quoted 20 lines
drivers/net/ovpn/peer.c new file +20
-0+struct ovpn_peer *ovpn_peer_new (struct ovpn_struct *ovpn,u32 id) +{ +struct ovpn_peer *peer; +int ret; + +/* alloc and init peer object */ + peer =kzalloc (sizeof (*peer),GFP_KERNEL ); +if (!peer) +return ERR_PTR (-ENOMEM ); + + peer->id = id; + peer->halt = false; + peer->ovpn = ovpn; + + peer->vpn_addrs.ipv4.s_addr =htonl (INADDR_ANY ); + peer->vpn_addrs.ipv6 = in6addr_any; + +RCU_INIT_POINTER (peer->bind,NULL ); +spin_lock_init (&peer->lock); +kref_init (&peer->refcount);
drivers/net/ovpn/peer.c line 57
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+ + ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (ovpn->dev,"%s: cannot initialize dst cache\n" , + __func__); +kfree (peer); +return ERR_PTR (ret); + } + +netdev_hold (ovpn->dev,NULL ,GFP_KERNEL );
It would be good to add a tracker to help debug refcount issues.
drivers/net/ovpn/peer.c line 63
Quoted 6 lines
drivers/net/ovpn/peer.c new file +6
-0+ +return peer; +} + +#define ovpn_peer_index (_tbl, _key, _key_len) \ + (jhash(_key, _key_len, 0) % HASH_SIZE(_tbl)) \
nit: not used in this patch, and even removed by patch 16 as you convert from index to buckets (that conversion should be squashed into patch 15)
drivers/net/ovpn/peer.c line 132
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+/** +* ovpn_peer_transp_match - check if sockaddr and peer binding match +* @peer: the peer to get the binding from +* @ss: the sockaddr to match +* +* Return: true if sockaddr and binding match or false otherwise +*/ +static bool ovpn_peer_transp_match (const struct ovpn_peer *peer, +const struct sockaddr_storage *ss) +{
AFAICT ovpn_peer_transp_match is only called with ss from ovpn_peer_skb_to_sockaddr, so it's pretty much ovpn_bind_skb_src_match but using peer->bind. You can probably avoid the code duplication (ovpn_peer_transp_match and ovpn_bind_skb_src_match are very similar).
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 08/25] ovpn: introduce the ovpn_peer object
Quoted 15 lines · Sabrina Dubroca
drivers/net/ovpn/bind.h line 32
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/bind.h new file +11
-0+/** +* struct ovpn_sockaddr - basic transport layer address +* @in4: IPv4 address +* @in6: IPv6 address +*/ +struct ovpn_sockaddr { +union { +struct sockaddr_in in4; +struct sockaddr_in6 in6; + }; +};nit: wrapping the anonymous union in a struct that contains nothing else is not that useful.
yeah, I guess I can just turn ovpn_sockaddr in a union.
Quoted 14 lines
drivers/net/ovpn/bind.h line 43
Quoted 12 lines
drivers/net/ovpn/bind.h new file +10
-0+/** +* struct ovpn_bind - remote peer binding +* @sa: the remote peer sockaddress +* @local: local endpoint used to talk to the peer +* @local.ipv4: local IPv4 used to talk to the peer +* @local.ipv6: local IPv6 used to talk to the peer +* @rcu: used to schedule RCU cleanup job +*/ +struct ovpn_bind { +struct ovpn_sockaddr sa;/* remote sockaddr */ nit: then maybe call it "peer" or "remote" instead of sa?
yap, makes sense. I will call it "remote".
Quoted 35 lines
Quoted 9 lines
drivers/net/ovpn/bind.h new file +8
-0+union { +struct in_addr ipv4; +struct in6_addr ipv6; + } local; + +struct rcu_head rcu; +}; +[...]
Quoted 24 lines
drivers/net/ovpn/peer.c new file +24
-0+struct ovpn_peer *ovpn_peer_new (struct ovpn_struct *ovpn,u32 id) +{ +struct ovpn_peer *peer; +int ret; + +/* alloc and init peer object */ + peer =kzalloc (sizeof (*peer),GFP_KERNEL ); +if (!peer) +return ERR_PTR (-ENOMEM ); + + peer->id = id; + peer->halt = false; + peer->ovpn = ovpn; + + peer->vpn_addrs.ipv4.s_addr =htonl (INADDR_ANY ); + peer->vpn_addrs.ipv6 = in6addr_any; + +RCU_INIT_POINTER (peer->bind,NULL ); +spin_lock_init (&peer->lock); +kref_init (&peer->refcount); + + ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL ); +if (ret <0 ) { + netdev_err(ovpn->dev, "%s: cannot initialize dst cache\n",
Quoted 8 lines
drivers/net/ovpn/peer.c line 57
Quoted 6 lines
drivers/net/ovpn/peer.c new file +6
-0+ __func__); +kfree (peer); +return ERR_PTR (ret); + } + +netdev_hold (ovpn->dev,NULL ,GFP_KERNEL );It would be good to add a tracker to help debug refcount issues.
Ok, will do!
Quoted 12 lines
drivers/net/ovpn/peer.c line 63
Quoted 8 lines
drivers/net/ovpn/peer.c new file +6
-0+ +return peer; +} + +#define ovpn_peer_index (_tbl, _key, _key_len) \ + (jhash(_key, _key_len, 0) % HASH_SIZE(_tbl)) \nit: not used in this patch, and even removed by patch 16 as you convert from index to buckets (that conversion should be squashed into patch 15)
You're correct. Will merge all these pieces in patch 15.
Quoted 17 lines
drivers/net/ovpn/peer.c line 132
Quoted 11 lines
drivers/net/ovpn/peer.c new file +10
-0+/** +* ovpn_peer_transp_match - check if sockaddr and peer binding match +* @peer: the peer to get the binding from +* @ss: the sockaddr to match +* +* Return: true if sockaddr and binding match or false otherwise +*/ +static bool ovpn_peer_transp_match (const struct ovpn_peer *peer, +const struct sockaddr_storage *ss) +{AFAICT
ovpn_peer_transp_matchis only called with ss fromovpn_peer_skb_to_sockaddr, so it's pretty muchovpn_bind_skb_src_matchbut usingpeer->bind. You can probably avoid the code duplication (ovpn_peer_transp_matchandovpn_bind_skb_src_matchare very similar).
mhh it is not called in ovpn_peer_skb_to_sockaddr, but I guess your comment still applies: ovpn_peer_transp_match and ovpn_bind_skb_src_match are very similar.
However in one we have a sockaddr_storage while in the other we have an skb. How do we combine the two? The only way I see is to create an ss out of the skb and then always use ovpn_peer_transp_match. Is this what you were alluding to?
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 9/25 ovpn: introduce the ovpn_socket object
In reply to Antonio QuartulliThis specific structure is used in the ovpn kernel module to wrap and carry around a standard kernel socket.
ovpn takes ownership of passed sockets and therefore an ovpn specific objects is attached to them for status tracking purposes.
Initially only UDP support is introduced. TCP will come in a later patch.
--- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/socket.c | 120 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.h | 49 ++++++++++++++++ drivers/net/ovpn/udp.c | 72 +++++++++++++++++++++++ drivers/net/ovpn/udp.h | 17 ++++++ 5 files changed, 260 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
@@ -13,3 +13,5 @@ ovpn-y += io.o ovpn-y += netlink.o ovpn-y += netlink-gen.o ovpn-y += peer.o +ovpn-y += socket.o +ovpn-y += udp.o
drivers/net/ovpn/socket.c new file +120 -0
120 lines not on this page · raw message
drivers/net/ovpn/socket.h new file +49 -0
49 lines not on this page · raw message
drivers/net/ovpn/udp.c new file +72 -0
72 lines not on this page · raw message
drivers/net/ovpn/udp.h new file +17 -0
@@ -0,0 +1,17 @@ +/* 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_ + +struct ovpn_struct ; +struct socket ; + +int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); + +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 10/25 ovpn: implement basic TX path (UDP)
In reply to Antonio QuartulliPackets sent over the ovpn interface are processed and transmitted to the connected peer, if any.
Implementation is UDP only. TCP will be added by a later patch.
Note: no crypto/encapsulation exists yet. packets are just captured and sent.
--- drivers/net/ovpn/io.c | 139 +++++++++++++++++++++++- drivers/net/ovpn/io.h | 2 + drivers/net/ovpn/peer.c | 37 ++++++- drivers/net/ovpn/peer.h | 5 + drivers/net/ovpn/skb.h | 54 ++++++++++ drivers/net/ovpn/udp.c | 230 ++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/udp.h | 8 ++ 7 files changed, 472 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ovpn/skb.h
drivers/net/ovpn/io.c +138 -1
@@ -9,14 +9,151 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gso.h> #include "io.h" +#include "ovpnstruct.h" +#include "peer.h" +#include "udp.h" +#include "skb.h" + +static void ovpn_encrypt_post (struct sk_buff *skb,int ret) +{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto err; + +skb_mark_not_on_list (skb); + +switch (peer->sock->sock->sk->sk_protocol) { +case IPPROTO_UDP : +ovpn_udp_send_skb (peer->ovpn, peer, skb); +break ; +default : +/* no transport configured yet */ + goto err; + } +/* skb passed down the stack - don't free it */ + skb =NULL ; +err: +if (unlikely (skb)) { +dev_core_stats_tx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); + } +ovpn_peer_put (peer); +} + +static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; + +/* take a reference to the peer because the crypto code may run async. +* ovpn_encrypt_post() will release it upon completion +*/ +DEBUG_NET_WARN_ON_ONCE (!ovpn_peer_hold (peer)); +ovpn_encrypt_post (skb,0 ); +return true; +} + +/* send skb to connected peer, if any */ +static void ovpn_send (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +struct sk_buff *curr, *next; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +/* this might be a GSO-segmented skb list: process each skb +* independently +*/ + skb_list_walk_safe(skb, curr, next) +if (unlikely (!ovpn_encrypt_one (peer, curr))) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr); + } + +/* skb passed over, no need to free */ + skb =NULL ; +drop: +if (likely (peer)) +ovpn_peer_put (peer); +kfree_skb_list (skb); +}/* 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); +dev_core_stats_tx_dropped_inc (ovpn->dev); + 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); +dev_core_stats_tx_dropped_inc (ovpn->dev); + 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_send (ovpn, skb_list.next,NULL ); + +return NETDEV_TX_OK ; + +drop_list: + skb_queue_walk_safe(&skb_list, curr, next) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +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
@@ -12,4 +12,6 @@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/peer.c +35 -2
@@ -9,7 +9,6 @@#include <linux/skbuff.h> #include <linux/list.h> -#include <linux/workqueue.h> #include "ovpnstruct.h" #include "bind.h"
@@ -68,8 +67,10 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)*/ static void ovpn_peer_release (struct ovpn_peer *peer) { -ovpn_bind_reset (peer,NULL ); +if (peer->sock) +ovpn_socket_put (peer->sock); +ovpn_bind_reset (peer,NULL );dst_cache_destroy (&peer->dst_cache); }
@@ -246,6 +247,38 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_id)return peer; } +/** +* ovpn_peer_get_by_dst - Lookup peer to send skb to +* @ovpn: the private data representing the current VPN session +* @skb: the skb to extract the destination address from +* +* This function takes a tunnel packet and looks up the peer to send it to +* after encapsulation. The skb is expected to be the in-tunnel packet, without +* any OpenVPN related header. +* +* Assume that the IP header is accessible in the skb data. +* +* Return: the peer if found or NULL otherwise. +*/ +struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn, +struct sk_buff *skb) +{ +struct ovpn_peer *peer =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 (); + peer =rcu_dereference (ovpn->peer); +if (unlikely (peer && !ovpn_peer_hold (peer))) + peer =NULL ; +rcu_read_unlock (); + } + +return peer; +} +/** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +5 -0
@@ -11,6 +11,7 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "socket.h" #include <net/dst_cache.h> #include <uapi/linux/ovpn.h>
@@ -22,6 +23,7 @@* @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @sock: the socket being used to talk to this peer * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called
@@ -38,6 +40,7 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct ovpn_socket *sock;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -78,5 +81,7 @@ void ovpn_peer_release_p2p(struct ovpn_struct *ovpn);struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb);struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id); +struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn, +struct sk_buff *skb);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/skb.h new file +54 -0
54 lines not on this page · raw message
drivers/net/ovpn/udp.c +230 -0
244 lines not on this page · raw message
drivers/net/ovpn/udp.h +8 -0
@@ -9,9 +9,17 @@#ifndef _NET_OVPN_UDP_H_#define _NET_OVPN_UDP_H_ +#include <linux/skbuff.h> +#include <net/sock.h> + +struct ovpn_peer ;struct ovpn_struct ; +struct sk_buff ;struct socket ;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.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 10/25] ovpn: implement basic TX path (UDP)
drivers/net/ovpn/io.c line 54
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/io.c +8
-0+static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; + +/* take a reference to the peer because the crypto code may run async. +* ovpn_encrypt_post() will release it upon completion +*/ +DEBUG_NET_WARN_ON_ONCE (!ovpn_peer_hold (peer));
Shouldn't we abort if this fails? This should not really happen, but if it did, we would proceed (possibly with async crypto) without a ref on the peer.
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+ovpn_encrypt_post (skb,0 ); +return true; +} +
[...]
drivers/net/ovpn/io.h line 15
Quoted 9 lines
drivers/net/ovpn/io.h +1
-0@@ -12,4 +12,6 @@netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); +void ovpn_encrypt_work (struct work_struct *work);
leftover from the old implementation I think?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 10/25] ovpn: implement basic TX path (UDP)
Quoted 13 lines · Sabrina Dubroca
drivers/net/ovpn/io.c line 54
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/io.c +8
-0+static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; + +/* take a reference to the peer because the crypto code may run async. +* ovpn_encrypt_post() will release it upon completion +*/ +DEBUG_NET_WARN_ON_ONCE (!ovpn_peer_hold (peer));Shouldn't we abort if this fails? This should not really happen, but if it did, we would proceed (possibly with async crypto) without a ref on the peer.
Yap, better bail out.
Quoted 13 lines
Quoted 5 lines
drivers/net/ovpn/io.c +4
-0+ovpn_encrypt_post (skb,0 ); +return true; +} +[...]
Quoted 6 lines
drivers/net/ovpn/io.h +0
-0@@ -12,4 +12,6 @@
Quoted 5 lines
drivers/net/ovpn/io.h line 15
Quoted 3 lines
drivers/net/ovpn/io.h +1
-0netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); +void ovpn_encrypt_work (struct work_struct *work);leftover from the old implementation I think?
ops, you're right.
Thanks
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 11/25 ovpn: implement basic RX path (UDP)
In reply to Antonio QuartulliPackets received over the socket are forwarded to the user device.
Implementation is UDP only. TCP will be added by a later patch.
Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing.
--- drivers/net/ovpn/io.c | 66 ++++++++++++++++++++- drivers/net/ovpn/io.h | 2 +- drivers/net/ovpn/main.c | 9 ++- drivers/net/ovpn/ovpnstruct.h | 3 + drivers/net/ovpn/proto.h | 75 ++++++++++++++++++++++++ drivers/net/ovpn/skb.h | 3 - drivers/net/ovpn/socket.c | 24 ++++++++ drivers/net/ovpn/udp.c | 104 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/udp.h | 3 +- 9 files changed, 281 insertions(+), 8 deletions(-) create mode 100644 drivers/net/ovpn/proto.h
drivers/net/ovpn/io.c +65 -1
@@ -9,14 +9,78 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gro_cells.h> #include <net/gso.h> -#include "io.h" #include "ovpnstruct.h" #include "peer.h" +#include "io.h" +#include "netlink.h" +#include "proto.h" #include "udp.h" #include "skb.h" +/* Called after decrypt to write the IP packet to the device. +* This method is expected to manage/free the skb. +*/ +static void ovpn_netdev_write (struct ovpn_peer *peer,struct sk_buff *skb) +{ +/* 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 the +* 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); + +memset (skb->cb,0 ,sizeof (skb->cb)); + +/* cause packet to be "received" by the interface */ +if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); +else +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +} + +static void ovpn_decrypt_post (struct sk_buff *skb,int ret) +{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto drop; + +ovpn_netdev_write (peer, skb); +/* skb is passed to upper layer - don't free it */ + skb =NULL ; +drop: +if (unlikely (skb)) +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +ovpn_peer_put (peer); +} + +/* pick next packet from RX queue, decrypt and forward it to the device */ +void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; +ovpn_decrypt_post (skb,0 ); +} +static void ovpn_encrypt_post (struct sk_buff *skb,int ret) {struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer;
drivers/net/ovpn/io.h +1 -1
@@ -12,6 +12,6 @@netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); -void ovpn_encrypt_work (struct work_struct *work); +void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb);#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +8 -1
@@ -13,6 +13,7 @@#include <linux/inetdevice.h> //#include <linux/rcupdate.h> #include <linux/version.h> +#include <net/gro_cells.h> #include <net/ip.h> #include <net/rtnetlink.h> #include <uapi/linux/if_arp.h>
@@ -49,11 +50,17 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode)static void ovpn_struct_free (struct net_device *net) { +struct ovpn_struct *ovpn =netdev_priv (net); + +gro_cells_destroy (&ovpn->gro_cells); +rcu_barrier (); }static int ovpn_net_init (struct net_device *dev) { -return 0 ; +struct ovpn_struct *ovpn =netdev_priv (dev); + +return gro_cells_init (&ovpn->gro_cells, dev); }static int ovpn_net_open (struct net_device *dev)
drivers/net/ovpn/ovpnstruct.h +3 -0
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <net/gro_cells.h> #include <uapi/linux/ovpn.h> /**
@@ -20,6 +21,7 @@* @lock: protect this object * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list +* @gro_cells: pointer to the Generic Receive Offload cell */ struct ovpn_struct {struct net_device *dev;
@@ -28,6 +30,7 @@ struct ovpn_struct {spinlock_t lock;/* protect writing to the ovpn_struct object */ struct ovpn_peer __rcu *peer;struct list_head dev_list; + struct gro_cells gro_cells; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/proto.h new file +75 -0
75 lines not on this page · raw message
drivers/net/ovpn/skb.h +0 -3
@@ -18,10 +18,7 @@#include <linux/types.h> struct ovpn_cb { -struct aead_request *req;struct ovpn_peer *peer; -struct ovpn_crypto_key_slot *ks; -unsigned int payload_offset; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
drivers/net/ovpn/socket.c +24 -0
@@ -22,6 +22,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); }
@@ -71,6 +74,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; +} +/** * ovpn_socket_new - create a new socket and initialize it * @sock: the kernel socket to embed
drivers/net/ovpn/udp.c +103 -1
129 lines not on this page · raw message
drivers/net/ovpn/udp.h +2 -1
@@ -18,8 +18,9 @@ struct sk_buff;struct socket ;int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); - +void ovpn_udp_socket_detach (struct socket *sock);void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer,struct sk_buff *skb); +struct ovpn_struct *ovpn_from_udp_sock (struct sock *sk);#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 11/25] ovpn: implement basic RX path (UDP)
Quoted 2 lines · Antonio Quartulli
(quoted diff) +2
-0+static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) +{
[...]
drivers/net/ovpn/io.c line 57
Quoted 7 lines
drivers/net/ovpn/io.c +7
-0+/* cause packet to be "received" by the interface */ +if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); +else +dev_core_stats_rx_dropped_inc (peer->ovpn->dev);
Not needed AFAICT, gro_cells_receive already does dev_core_stats_rx_dropped_inc(skb->dev) when it drops the packet.
drivers/net/ovpn/io.c line 70
Quoted 13 lines
drivers/net/ovpn/io.c +13
-0+} + +static void ovpn_decrypt_post (struct sk_buff *skb,int ret) +{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto drop; + +ovpn_netdev_write (peer, skb); +/* skb is passed to upper layer - don't free it */ + skb =NULL ; +drop:
I really find this "common" return code confusing. The only thing the two cases have in common is dropping the peer reference.
Quoted 5 lines
drivers/net/ovpn/io.c +5
-0+if (unlikely (skb)) +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +ovpn_peer_put (peer); +}
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 11/25] ovpn: implement basic RX path (UDP)
Hi,
Quoted 4 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
(quoted diff) +2
-0+static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) +{[...]
Quoted 10 lines
drivers/net/ovpn/io.c line 57
Quoted 7 lines
drivers/net/ovpn/io.c +7
-0+/* cause packet to be "received" by the interface */ +if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); +else +dev_core_stats_rx_dropped_inc (peer->ovpn->dev);Not needed AFAICT,
gro_cells_receivealready doesdev_core_stats_rx_dropped_inc(skb->dev)when it drops the packet.
You're right! This should be removed.
Quoted 17 lines
Quoted 4 lines
drivers/net/ovpn/io.c +3
-0+} + +static void ovpn_decrypt_post (struct sk_buff *skb,int ret)drivers/net/ovpn/io.c line 70
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto drop; + +ovpn_netdev_write (peer, skb); +/* skb is passed to upper layer - don't free it */ + skb =NULL ; +drop:I really find this "common" return code confusing. The only thing the two cases have in common is dropping the peer reference.
I believe it's just a matter of perspective: I read it as "exit path" and if something went wrong I just do some extra actions.
However, I am perfectly fine splitting success/failure paths if the common code is little like in this case.
Thanks!
Quoted 7 lines
Quoted 6 lines
drivers/net/ovpn/io.c +5
-0+if (unlikely (skb)) +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +ovpn_peer_put (peer); +}
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 12/25 ovpn: implement packet processing
In reply to Antonio QuartulliThis 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 | 161 +++++++++++++++ drivers/net/ovpn/crypto.h | 138 +++++++++++++ drivers/net/ovpn/crypto_aead.c | 347 +++++++++++++++++++++++++++++++++ drivers/net/ovpn/crypto_aead.h | 30 +++ drivers/net/ovpn/io.c | 113 ++++++++++- drivers/net/ovpn/io.h | 3 + drivers/net/ovpn/packet.h | 2 +- drivers/net/ovpn/peer.c | 29 +++ drivers/net/ovpn/peer.h | 6 + drivers/net/ovpn/pktid.c | 130 ++++++++++++ drivers/net/ovpn/pktid.h | 87 +++++++++ drivers/net/ovpn/proto.h | 31 +++ drivers/net/ovpn/skb.h | 4 + 14 files changed, 1079 insertions(+), 5 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
@@ -8,10 +8,13 @@ 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 += netlink-gen.o ovpn-y += peer.o +ovpn-y += pktid.o ovpn-y += socket.o ovpn-y += udp.o
drivers/net/ovpn/crypto.c new file +161 -0
161 lines not on this page · raw message
drivers/net/ovpn/crypto.h new file +138 -0
138 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.c new file +347 -0
347 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.h new file +30 -0
@@ -0,0 +1,30 @@ +/* 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 +109 -4
160 lines not on this page · raw message
drivers/net/ovpn/io.h +3 -0
@@ -14,4 +14,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb); +void ovpn_encrypt_post (struct sk_buff *skb,int ret); +void ovpn_decrypt_post (struct sk_buff *skb,int ret); +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/packet.h +1 -1
@@ -10,7 +10,7 @@#ifndef _NET_OVPN_PACKET_H_#define _NET_OVPN_PACKET_H_ -/* When the OpenVPN protocol is ran in AEAD mode, use +/* When the OpenVPN protocol is run in AEAD mode, use * the OpenVPN packet ID as the AEAD nonce: * * 00000005 521c3b01 4308c041
drivers/net/ovpn/peer.c +29 -0
@@ -12,6 +12,8 @@#include "ovpnstruct.h" #include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "io.h" #include "main.h" #include "netlink.h"
@@ -42,6 +44,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);
@@ -70,6 +73,7 @@ static void ovpn_peer_release(struct ovpn_peer *peer)if (peer->sock)ovpn_socket_put (peer->sock); +ovpn_crypto_state_release (&peer->crypto);ovpn_bind_reset (peer,NULL );dst_cache_destroy (&peer->dst_cache); }
@@ -279,6 +283,31 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,return peer; } +/** +* ovpn_peer_check_by_src - check that skb source is routed via peer +* @ovpn: the openvpn instance to search +* @skb: the packet to extra source address from +* @peer: the peer to check against the source address +* +* Return: true if the peer is matching or false otherwise +*/ +bool ovpn_peer_check_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +bool match = false; + +if (ovpn->mode ==OVPN_MODE_P2P ) { +/* in P2P mode, no matter the destination, packets are always +* sent to the single peer listening on the other side +*/ +rcu_read_lock (); + match = (peer ==rcu_dereference (ovpn->peer)); +rcu_read_unlock (); + } + +return match; +} +/** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +6 -0
@@ -11,6 +11,8 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "socket.h" #include <net/dst_cache.h>
@@ -24,6 +26,7 @@* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer +* @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called
@@ -41,6 +44,7 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket *sock; +struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -83,5 +87,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn,struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id);struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn,struct sk_buff *skb); +bool ovpn_peer_check_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/pktid.c new file +130 -0
130 lines not on this page · raw message
drivers/net/ovpn/pktid.h new file +87 -0
87 lines not on this page · raw message
drivers/net/ovpn/proto.h +31 -0
@@ -72,4 +72,35 @@ static inline u32 ovpn_peer_id_from_skb(const struct sk_buff *skb, u16 offset) return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK; } +/** +* ovpn_key_id_from_skb - extract key ID from the skb head +* @skb: the packet to extract the key ID code from +* +* Note: this function assumes that the skb head was pulled enough +* to access the first byte. +* +* Return: the key ID +*/ +static inline u8 ovpn_key_id_from_skb (const struct sk_buff *skb) +{ +return *skb->data &OVPN_KEY_ID_MASK ; +} + +/** +* ovpn_opcode_compose - combine OP code, key ID and peer ID to wire format +* @opcode: the OP code +* @key_id: the key ID +* @peer_id: the peer ID +* +* Return: a 4 bytes integer obtained combining all input values following the +* OpenVPN wire format. This integer can then be written to the packet header. +*/ +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_ */
drivers/net/ovpn/skb.h +4 -0
@@ -19,6 +19,10 @@struct ovpn_cb {struct ovpn_peer *peer; +struct sk_buff *skb; +struct ovpn_crypto_key_slot *ks; +struct aead_request *req; +unsigned int payload_offset; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 12/25] ovpn: implement packet processing
drivers/net/ovpn/crypto.c line 67
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/crypto.c new file +8
-0+/* removes the primary key from the crypto context */ +void ovpn_crypto_kill_primary (struct ovpn_crypto_state *cs) +{ +struct ovpn_crypto_key_slot *ks; + +mutex_lock (&cs->mutex); + ks =rcu_replace_pointer (cs->primary,NULL , +lockdep_is_held (&cs->mutex));
Should there be a check that we're killing the key that has expired and not some other key? I'm wondering if this could happen:
ovpn_encrypt_one
ovpn_aead_encrypt
ovpn_pktid_xmit_next
seq_num reaches threshold
returns -ERANGE
returns -ERANGE
ovpn_crypto_key_slots_swap
replaces cs->primary with cs->secondary
ovpn_encrypt_post
ret = -ERANGE ovpn_crypto_kill_primary
kills the freshly installed primary key
Quoted 4 lines
drivers/net/ovpn/crypto.c new file +4
-0+ovpn_crypto_key_slot_put (ks); +mutex_unlock (&cs->mutex); +} +
[...]
Quoted 5 lines
drivers/net/ovpn/crypto_aead.c new file +5
-0+static void ovpn_aead_encrypt_done (void *data,int ret) +{ +struct sk_buff *skb = data; + +aead_request_free (ovpn_skb_cb (skb)->req);
drivers/net/ovpn/crypto_aead.c line 49
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+ovpn_encrypt_post (skb, ret); +} + +int ovpn_aead_encrypt (struct ovpn_crypto_key_slot *ks,struct sk_buff *skb, +u32 peer_id) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->encrypt); +const unsigned int head_size =ovpn_aead_encap_overhead (ks); +struct scatterlist sg[MAX_SKB_FRAGS +2 ]; +DECLARE_CRYPTO_WAIT (wait);
unused? (also in _decrypt)
[...]
drivers/net/ovpn/crypto_aead.c line 124
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c new file +8
-0+ + req =aead_request_alloc (ks->encrypt,GFP_ATOMIC ); +if (unlikely (!req)) +return -ENOMEM ; + +/* setup async crypto operation */ +aead_request_set_tfm (req, ks->encrypt); +aead_request_set_callback (req,0 , ovpn_aead_encrypt_done,NULL );
NULL? That should be skb, ovpn_aead_encrypt_done needs it (same for decrypt).
I suspect you haven't triggered the async path in testing. For that, you can use crconf:
git clone https://git.code.sf.net/p/crconf/code
cd code && make
./src/crconf add driver 'pcrypt(generic-gcm-aesni)' type 3 priority 10000
Then all packets encrypted with gcm(aes) should go through the async code.
Quoted 9 lines
drivers/net/ovpn/crypto_aead.c new file +9
-0+aead_request_set_crypt (req, sg, sg, skb->len - head_size, iv); +aead_request_set_ad (req,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + +ovpn_skb_cb (skb)->req = req; +ovpn_skb_cb (skb)->ks = ks; + +/* encrypt it */ +return crypto_aead_encrypt (req); +}
[...]
drivers/net/ovpn/io.c static void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 141
Quoted 10 lines
drivers/net/ovpn/io.c +6
-0@@ -77,14 +133,45 @@ static void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* pick next packet from RX queue, decrypt and forward it to the device */ void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb) { +struct ovpn_crypto_key_slot *ks; +u8 key_id; + +/* 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);
This takes a reference on the keyslot (ovpn_crypto_key_slot_hold), but I don't see it getting released in ovpn_decrypt_post. In ovpn_encrypt_post you're adding a ovpn_crypto_key_slot_put (to match ovpn_crypto_key_slot_primary), but nothing equivalent in ovpn_decrypt_post?
Quoted 12 lines
drivers/net/ovpn/io.c +9
-1+if (unlikely (!ks)) { +net_info_ratelimited ("%s: no available key for peer %u, key-id: %u\n" , + peer->ovpn->dev->name, peer->id, key_id); +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +return ; + } +ovpn_skb_cb (skb)->peer = peer; -ovpn_decrypt_post (skb,0 ); +ovpn_decrypt_post (skb,ovpn_aead_decrypt (ks, skb)); }
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 12/25] ovpn: implement packet processing
Hi,
Quoted 23 lines · Sabrina Dubroca
drivers/net/ovpn/crypto.c line 67
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/crypto.c new file +8
-0+/* removes the primary key from the crypto context */ +void ovpn_crypto_kill_primary (struct ovpn_crypto_state *cs) +{ +struct ovpn_crypto_key_slot *ks; + +mutex_lock (&cs->mutex); + ks =rcu_replace_pointer (cs->primary,NULL , +lockdep_is_held (&cs->mutex));Should there be a check that we're killing the key that has expired and not some other key? I'm wondering if this could happen:
ovpn_encrypt_one
ovpn_aead_encrypt
ovpn_pktid_xmit_next
seq_numreaches threshold
returns-ERANGE
returns-ERANGE
ovpn_crypto_key_slots_swap
replacescs->primarywithcs->secondary
Quoted 8 lines
ovpn_encrypt_post
ret =-ERANGEovpn_crypto_kill_primary
kills the freshly installed primary keydrivers/net/ovpn/crypto.c line 70
Quoted 3 lines
drivers/net/ovpn/crypto.c new file +3
-0+ovpn_crypto_key_slot_put (ks); +mutex_unlock (&cs->mutex); +}
hm I think you're right.
This is theoretically possible...
Userspace might be reneweing the key exactly when the primary key is expiring..
We need to specify the key_id and kill exactly that key.
Quoted 12 lines
Quoted 1 line
+
[...]
Quoted 9 lines
drivers/net/ovpn/crypto_aead.c new file +9
-0+static void ovpn_aead_encrypt_done (void *data,int ret) +{ +struct sk_buff *skb = data; + +aead_request_free (ovpn_skb_cb (skb)->req); +ovpn_encrypt_post (skb, ret); +} + +int ovpn_aead_encrypt(struct ovpn_crypto_key_slot *ks, struct sk_buff *skb,
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 49
Quoted 6 lines
drivers/net/ovpn/crypto_aead.c new file +6
-0+ u32 peer_id) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->encrypt); +const unsigned int head_size =ovpn_aead_encap_overhead (ks); +struct scatterlist sg[MAX_SKB_FRAGS +2 ]; +DECLARE_CRYPTO_WAIT (wait);unused? (also in
_decrypt)
Right. Leftover from the sync crypto approach. Will drop it.
Quoted 13 lines
[...]
drivers/net/ovpn/crypto_aead.c line 124
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c new file +8
-0+ + req =aead_request_alloc (ks->encrypt,GFP_ATOMIC ); +if (unlikely (!req)) +return -ENOMEM ; + +/* setup async crypto operation */ +aead_request_set_tfm (req, ks->encrypt); +aead_request_set_callback (req,0 , ovpn_aead_encrypt_done,NULL );NULL? That should be skb,
ovpn_aead_encrypt_doneneeds it (same for decrypt).
Ouch
Quoted 3 lines
I suspect you haven't triggered the async path in testing. For that, you can use crconf:
Yeah, I doubt I did, although I tried pumping as much traffic as possible (but it may not have been enough to trigger the needed conditions).
Quoted 7 lines
git clone https://git.code.sf.net/p/crconf/code
cd code && make
./src/crconf add driver 'pcrypt(generic-gcm-aesni)' type 3 priority 10000Then all packets encrypted with gcm(aes) should go through the async code.
Amazing! Thanks! Will give it a go.
Quoted 12 lines
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +9
-0+aead_request_set_crypt (req, sg, sg, skb->len - head_size, iv); +aead_request_set_ad (req,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + +ovpn_skb_cb (skb)->req = req; +ovpn_skb_cb (skb)->ks = ks; + +/* encrypt it */ +return crypto_aead_encrypt (req); +}[...]
Quoted 16 lines
drivers/net/ovpn/io.c static void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 141
Quoted 10 lines
drivers/net/ovpn/io.c +6
-0@@ -77,14 +133,45 @@ static void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* pick next packet from RX queue, decrypt and forward it to the device */ void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb) { +struct ovpn_crypto_key_slot *ks; +u8 key_id; + +/* 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);This takes a reference on the keyslot (
ovpn_crypto_key_slot_hold), but I don't see it getting released inovpn_decrypt_post. Inovpn_encrypt_postyou're adding aovpn_crypto_key_slot_put(to matchovpn_crypto_key_slot_primary), but nothing equivalent inovpn_decrypt_post?
good catch! I don't think it triggered any critical side effect (Except from the leak) hence it went unnoticed.
Will add the ovpn_crypto_key_slot_put in the exit path.
Thanks!
Quoted 14 lines
Quoted 13 lines
drivers/net/ovpn/io.c +9
-1+if (unlikely (!ks)) { +net_info_ratelimited ("%s: no available key for peer %u, key-id: %u\n" , + peer->ovpn->dev->name, peer->id, key_id); +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +return ; + } +ovpn_skb_cb (skb)->peer = peer; -ovpn_decrypt_post (skb,0 ); +ovpn_decrypt_post (skb,ovpn_aead_decrypt (ks, skb)); }
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 13/25 ovpn: store tunnel and transport statistics
In reply to Antonio QuartulliByte/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 | 12 ++++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 9 ++++++++ drivers/net/ovpn/skb.h | 1 + drivers/net/ovpn/stats.c | 21 +++++++++++++++++ drivers/net/ovpn/stats.h | 47 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+) create mode 100644 drivers/net/ovpn/stats.c create mode 100644 drivers/net/ovpn/stats.h
drivers/net/ovpn/Makefile +1 -0
@@ -17,4 +17,5 @@ ovpn-y += netlink-gen.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 +12 -0
@@ -11,6 +11,7 @@#include <linux/skbuff.h> #include <net/gro_cells.h> #include <net/gso.h> +#include <net/ip.h> #include "ovpnstruct.h" #include "peer.h"
@@ -19,6 +20,7 @@#include "crypto_aead.h" #include "netlink.h" #include "proto.h" +#include "socket.h" #include "udp.h" #include "skb.h"
@@ -120,6 +122,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* increment RX stats */ +ovpn_peer_stats_increment_rx (&peer->vpn_stats, skb->len); +ovpn_peer_stats_increment_rx (&peer->link_stats, +ovpn_skb_cb (skb)->orig_len); +ovpn_netdev_write (peer, skb);/* skb is passed to upper layer - don't free it */ skb =NULL ;
@@ -148,6 +155,7 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb) }ovpn_skb_cb (skb)->peer = peer; +ovpn_skb_cb (skb)->orig_len = skb->len;ovpn_decrypt_post (skb,ovpn_aead_decrypt (ks, skb)); }
@@ -176,6 +184,9 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret) goto err;skb_mark_not_on_list (skb); +ovpn_peer_stats_increment_tx (&peer->link_stats, skb->len); +ovpn_peer_stats_increment_tx (&peer->vpn_stats, +ovpn_skb_cb (skb)->orig_len);switch (peer->sock->sock->sk->sk_protocol) {case IPPROTO_UDP :
@@ -216,6 +227,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) }ovpn_skb_cb (skb)->peer = peer; +ovpn_skb_cb (skb)->orig_len = skb->len;/* take a reference to the peer because the crypto code may run async. * ovpn_encrypt_post() will release it upon completion
drivers/net/ovpn/peer.c +3 -0
@@ -18,6 +18,7 @@#include "main.h" #include "netlink.h" #include "peer.h" +#include "socket.h" /** * ovpn_peer_new - allocate and initialize a new peer object
@@ -47,6 +48,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock);kref_init (&peer->refcount); +ovpn_peer_stats_init (&peer->vpn_stats); +ovpn_peer_stats_init (&peer->link_stats); ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );if (ret <0 ) {
drivers/net/ovpn/peer.h +9 -0
@@ -10,10 +10,15 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ +#include <linux/ptr_ring.h> +#include <net/dst_cache.h> +#include <uapi/linux/ovpn.h> +#include "bind.h" #include "pktid.h" #include "crypto.h" #include "socket.h" +#include "stats.h" #include <net/dst_cache.h> #include <uapi/linux/ovpn.h>
@@ -30,6 +35,8 @@* @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called +* @vpn_stats: per-peer in-VPN TX/RX stays +* @link_stats: per-peer link/transport TX/RX stats * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) * @lock: protects binding to peer (bind) * @refcount: reference counter
@@ -48,6 +55,8 @@ struct ovpn_peer {struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt; +struct ovpn_peer_stats vpn_stats; +struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind */ struct kref refcount;
drivers/net/ovpn/skb.h +1 -0
@@ -22,6 +22,7 @@ struct ovpn_cb {struct sk_buff *skb;struct ovpn_crypto_key_slot *ks;struct aead_request *req; +size_t orig_len;unsigned int payload_offset; };
drivers/net/ovpn/stats.c new file +21 -0
@@ -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 <linux/atomic.h> + +#include "stats.h" + +void ovpn_peer_stats_init (struct ovpn_peer_stats *ps) +{ +atomic64_set (&ps->rx.bytes,0 ); +atomic64_set (&ps->rx.packets,0 ); + +atomic64_set (&ps->tx.bytes,0 ); +atomic64_set (&ps->tx.packets,0 ); +}
drivers/net/ovpn/stats.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 14/25 ovpn: implement TCP transport
In reply to Antonio QuartulliWith this change ovpn is allowed to communicate to peers also via TCP. Parsing of incoming messages is implemented through the strparser API.
--- drivers/net/Kconfig | 1 + drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 9 +- drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/peer.h | 38 ++- drivers/net/ovpn/socket.c | 27 +- drivers/net/ovpn/socket.h | 7 +- drivers/net/ovpn/tcp.c | 502 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/tcp.h | 42 ++++ 9 files changed, 624 insertions(+), 6 deletions(-) create mode 100644 drivers/net/ovpn/tcp.c create mode 100644 drivers/net/ovpn/tcp.h
drivers/net/Kconfig +1 -0
@@ -118,6 +118,7 @@ config WIREGUARD_DEBUG config OVPN tristate "OpenVPN data channel offload" depends on NET && INET + select STREAM_PARSER select NET_UDP_TUNNEL select DST_CACHE select CRYPTO
drivers/net/ovpn/Makefile +1 -0
@@ -18,4 +18,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 +8 -1
@@ -21,6 +21,7 @@#include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" #include "skb.h"
@@ -84,8 +85,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* PID sits after the op */ pid = (__force __be32 *)(skb->data +OVPN_OP_SIZE_V2 ); ret =ovpn_pktid_recv (&ks->pid_recv,ntohl (*pid),0 ); -if (unlikely (ret <0 )) +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: PKT ID RX error: %d\n" , + peer->ovpn->dev->name, ret); goto drop; + }/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->payload_offset);
@@ -192,6 +196,9 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret)case IPPROTO_UDP :ovpn_udp_send_skb (peer->ovpn, peer, skb);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, skb); +break ;default :/* no transport configured yet */ goto err;
drivers/net/ovpn/main.c +3 -0
@@ -25,6 +25,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -279,6 +280,8 @@ static int __init ovpn_init(void) goto unreg_rtnl; } +ovpn_tcp_init (); +return 0 ; unreg_rtnl:
drivers/net/ovpn/peer.h +37 -1
@@ -10,8 +10,8 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ -#include <linux/ptr_ring.h> #include <net/dst_cache.h> +#include <net/strparser.h> #include <uapi/linux/ovpn.h> #include "bind.h"
@@ -31,6 +31,18 @@* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer +* @tcp: keeps track of TCP specific state +* @tcp.strp: stream parser context (TCP only) +* @tcp.tx_work: work for deferring outgoing packet processing (TCP only) +* @tcp.user_queue: received packets that have to go to userspace (TCP only) +* @tcp.tx_in_progress: true if TX is already ongoing (TCP only) +* @tcp.out_msg.skb: packet scheduled for sending (TCP only) +* @tcp.out_msg.offset: offset where next send should start (TCP only) +* @tcp.out_msg.len: remaining data to send within packet (TCP only) +* @tcp.sk_cb.sk_data_ready: pointer to original cb (TCP only) +* @tcp.sk_cb.sk_write_space: pointer to original cb (TCP only) +* @tcp.sk_cb.prot: pointer to original prot object (TCP only) +* @tcp.sk_cb.ops: pointer to the original prot_ops object (TCP only) * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding
@@ -51,6 +63,30 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket *sock; + +/* state of the TCP reading. Needed to keep track of how much of a +* single packet has already been read from the stream and how much is +* missing +*/ +struct { +struct strparser strp; +struct work_struct tx_work; +struct sk_buff_head user_queue; +bool tx_in_progress; + +struct { +struct sk_buff *skb; +int offset; +int len; + } out_msg; + +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; +const struct proto_ops *ops; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;
drivers/net/ovpn/socket.c +24 -3
@@ -15,6 +15,7 @@#include "io.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" static void ovpn_socket_detach (struct socket *sock)
@@ -24,6 +25,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); }
@@ -70,6 +73,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; }
@@ -131,14 +136,30 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) } ovpn_sock =kzalloc (sizeof (*ovpn_sock),GFP_KERNEL ); -if (!ovpn_sock) -return ERR_PTR (-ENOMEM ); +if (!ovpn_sock) { + ret = -ENOMEM ; + goto err; + } - ovpn_sock->ovpn = peer->ovpn; 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; + }else { +/* in UDP we only link the ovpn instance since the socket is +* shared among multiple peers +*/ + ovpn_sock->ovpn = peer->ovpn; + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock; +err: +ovpn_socket_detach (sock); +return ERR_PTR (ret); }
drivers/net/ovpn/socket.h +6 -1
@@ -21,12 +21,17 @@ struct ovpn_peer;/** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) +* @peer: unique peer transmitting over this socket (TCP only) * @sock: the low level sock object * @refcount: amount of contexts currently referencing this object * @rcu: member used to schedule RCU destructor callback */ struct ovpn_socket { -struct ovpn_struct *ovpn; +union { +struct ovpn_struct *ovpn; +struct ovpn_peer *peer; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;
drivers/net/ovpn/tcp.c new file +502 -0
502 lines not on this page · raw message
drivers/net/ovpn/tcp.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 14/25] ovpn: implement TCP transport
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/io.c +1
-0@@ -21,6 +21,7 @@#include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" #include "skb.h"
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 90
Quoted 7 lines
drivers/net/ovpn/io.c +3
-1@@ -84,8 +85,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* PID sits after the op */ pid = (__force __be32 *)(skb->data +OVPN_OP_SIZE_V2 ); ret =ovpn_pktid_recv (&ks->pid_recv,ntohl (*pid),0 ); -if (unlikely (ret <0 )) +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: PKT ID RX error: %d\n" , + peer->ovpn->dev->name, ret);
nit: this should be part of the "packet processing" patch?
drivers/net/ovpn/peer.h line 13
Quoted 9 lines
drivers/net/ovpn/peer.h +0
-1@@ -10,8 +10,8 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ -#include <linux/ptr_ring.h>
nit: I think you don't need it at all in this version and forgot to drop it in a previous patch? (I didn't notice when it was introduced)
drivers/net/ovpn/peer.h
Quoted 6 lines
drivers/net/ovpn/peer.h +6
-0+static int ovpn_tcp_to_userspace (struct ovpn_socket *sock,struct sk_buff *skb) +{ +struct sock *sk = sock->sock->sk; + +skb_set_owner_r (skb, sk); +memset (skb->cb,0 ,sizeof (skb->cb));
nit: this was just done in ovpn_tcp_rcv
Quoted 8 lines
drivers/net/ovpn/peer.h +8
-0+skb_queue_tail (&sock->peer->tcp.user_queue, skb); + sock->peer->tcp.sk_cb.sk_data_ready (sk); + +return 0 ; +} + +static void ovpn_tcp_rcv (struct strparser *strp,struct sk_buff *skb) +{
[...]
drivers/net/ovpn/peer.h
Quoted 9 lines
drivers/net/ovpn/peer.h +9
-0+/* DATA_V2 packets are handled in kernel, the rest goes to user space */ +if (likely (ovpn_opcode_from_skb (skb,0 ) ==OVPN_DATA_V2 )) { +/* hold reference to peer as required by ovpn_recv(). +* +* NOTE: in this context we should already be holding a +* reference to this peer, therefore ovpn_peer_hold() is +* not expected to fail +*/ +WARN_ON (!ovpn_peer_hold (peer));
drop the packet if this fails? otherwise I suspect we'll crash later on.
Quoted 13 lines
drivers/net/ovpn/peer.h +13
-0+ovpn_recv (peer, skb); + }else { +/* The packet size header must be there when sending the packet +* to userspace, therefore we put it back +*/ +skb_push (skb,2 ); +memset (skb->cb,0 ,sizeof (skb->cb)); +if (ovpn_tcp_to_userspace (peer->sock, skb) <0 ) { +net_warn_ratelimited ("%s: cannot send skb to userspace\n" , + peer->ovpn->dev->name); + goto err; + } + }
[...]
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+void ovpn_tcp_socket_detach (struct socket *sock) +{ +struct ovpn_socket *ovpn_sock; +struct ovpn_peer *peer; + +if (!sock) +return ; + +rcu_read_lock (); + ovpn_sock =rcu_dereference_sk_user_data (sock->sk); +
[...]
drivers/net/ovpn/peer.h
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+/* cancel any ongoing work. Done after removing the CBs so that these +* workers cannot be re-armed +*/ +cancel_work_sync (&peer->tcp.tx_work);
I don't think that's ok to call under rcu_read_lock, it seems it can sleep.
drivers/net/ovpn/peer.h
Quoted 1 line
drivers/net/ovpn/peer.h +1
-0+strp_done (&peer->tcp.strp);
And same here, since strp_done also calls cancel_work_sync.
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+rcu_read_unlock (); +} + +static void ovpn_tcp_send_sock (struct ovpn_peer *peer)
drivers/net/ovpn/peer.h
Quoted 10 lines
drivers/net/ovpn/peer.h +10
-0+{ +struct sk_buff *skb = peer->tcp.out_msg.skb; + +if (!skb) +return ; + +if (peer->tcp.tx_in_progress) +return ; + + peer->tcp.tx_in_progress = true;
I'm not convinced this is safe. ovpn_tcp_send_sock could run concurrently for the same peer (lock_sock doesn't exclude bh_lock_sock after the short "grab ownership" phase), so I think both sides could see tx_in_progress = false and then proceed.
drivers/net/ovpn/peer.h
Quoted 7 lines
drivers/net/ovpn/peer.h +7
-0+ do { + int ret = skb_send_sock_locked(peer->sock->sock->sk, skb, + peer->tcp.out_msg.offset, + peer->tcp.out_msg.len); + if (unlikely(ret < 0)) { + if (ret == -EAGAIN) + goto out;
This will silently drop the message? And then in case of a userspace message, ovpn_tcp_sendmsg will lie to the user (the openvpn client), claiming that the control message was sent (ret = size just above the unlock)?
Quoted 6 lines
drivers/net/ovpn/peer.h +6
-0+ +net_warn_ratelimited ("%s: TCP error to peer %u: %d\n" , + peer->ovpn->dev->name, peer->id, + ret); + +/* in case of TCP error we can't recover the VPN
drivers/net/ovpn/peer.h
Quoted 10 lines
drivers/net/ovpn/peer.h +10
-0+* stream therefore we abort the connection +*/ +ovpn_peer_del (peer, +OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); +break ; + } + + peer->tcp.out_msg.len -= ret; + peer->tcp.out_msg.offset += ret; + }while (peer->tcp.out_msg.len >0 );
Another thing that worries me: assume the receiver is a bit slow, the underlying TCP socket gets stuck. skb_send_sock_locked manages to push some data down the TCP socket, but not everything. We advance by that amount, and restart this loop. The socket is still stuck, so skb_send_sock_locked returns -EAGAIN. We have only pushed a partial message down to the TCP socket, but we drop the rest? Now the stream is broken, and the next call to ovpn_tcp_send_sock will happily send its message.
ovpn_tcp_send_sock with msg_len = 1000
iteration 1
skb_send_sock_locked returns 100
advance
iteration 2
skb_send_sock_locked returns -EAGAIN
goto out
So you'd have to keep that partially-sent message around until you can finish pushing it out on the socket.
[...]
drivers/net/ovpn/peer.h
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+static int ovpn_tcp_sendmsg (struct sock *sk,struct msghdr *msg,size_t size) +{ +struct ovpn_socket *sock; +int ret, linear =PAGE_SIZE ; +struct ovpn_peer *peer; +struct sk_buff *skb; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); + peer = sock->peer; +rcu_read_unlock ();
What's stopping the peer being freed here?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 14/25] ovpn: implement TCP transport
Hi,
Quoted 13 lines · Sabrina Dubroca
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/io.c +1
-0@@ -21,6 +21,7 @@#include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" #include "skb.h"
Quoted 10 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 90
Quoted 8 lines
drivers/net/ovpn/io.c +3
-1@@ -84,8 +85,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* PID sits after the op */ pid = (__force __be32 *)(skb->data +OVPN_OP_SIZE_V2 ); ret =ovpn_pktid_recv (&ks->pid_recv,ntohl (*pid),0 ); -if (unlikely (ret <0 )) +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: PKT ID RX error: %d\n" , + peer->ovpn->dev->name, ret);nit: this should be part of the "packet processing" patch?
Yap, makes sense.
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/peer.h +0
-0@@ -10,8 +10,8 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_
Quoted 4 lines
drivers/net/ovpn/peer.h line 13
Quoted 1 line
drivers/net/ovpn/peer.h +0
-1-#include <linux/ptr_ring.h> nit: I think you don't need it at all in this version and forgot to drop it in a previous patch? (I didn't notice when it was introduced)
Ouch, you are correct
Quoted 11 lines
drivers/net/ovpn/peer.h
Quoted 9 lines
drivers/net/ovpn/peer.h +6
-0+static int ovpn_tcp_to_userspace (struct ovpn_socket *sock,struct sk_buff *skb) +{ +struct sock *sk = sock->sock->sk; + +skb_set_owner_r (skb, sk); +memset (skb->cb,0 ,sizeof (skb->cb));nit: this was just done in
ovpn_tcp_rcv
right!
Quoted 10 lines
Quoted 9 lines
drivers/net/ovpn/peer.h +8
-0+skb_queue_tail (&sock->peer->tcp.user_queue, skb); + sock->peer->tcp.sk_cb.sk_data_ready (sk); + +return 0 ; +} + +static void ovpn_tcp_rcv (struct strparser *strp,struct sk_buff *skb) +{[...]
Quoted 11 lines
drivers/net/ovpn/peer.h
Quoted 9 lines
drivers/net/ovpn/peer.h +9
-0+/* DATA_V2 packets are handled in kernel, the rest goes to user space */ +if (likely (ovpn_opcode_from_skb (skb,0 ) ==OVPN_DATA_V2 )) { +/* hold reference to peer as required by ovpn_recv(). +* +* NOTE: in this context we should already be holding a +* reference to this peer, therefore ovpn_peer_hold() is +* not expected to fail +*/ +WARN_ON (!ovpn_peer_hold (peer));drop the packet if this fails? otherwise I suspect we'll crash later on.
yeah, jumping to "err" and dropping everything makes sense.
Quoted 37 lines
Quoted 14 lines
drivers/net/ovpn/peer.h +13
-0+ovpn_recv (peer, skb); + }else { +/* The packet size header must be there when sending the packet +* to userspace, therefore we put it back +*/ +skb_push (skb,2 ); +memset (skb->cb,0 ,sizeof (skb->cb)); +if (ovpn_tcp_to_userspace (peer->sock, skb) <0 ) { +net_warn_ratelimited ("%s: cannot send skb to userspace\n" , + peer->ovpn->dev->name); + goto err; + } + }[...]
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+void ovpn_tcp_socket_detach (struct socket *sock) +{ +struct ovpn_socket *ovpn_sock; +struct ovpn_peer *peer; + +if (!sock) +return ; + +rcu_read_lock (); + ovpn_sock =rcu_dereference_sk_user_data (sock->sk); +[...]
drivers/net/ovpn/peer.h
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+/* cancel any ongoing work. Done after removing the CBs so that these +* workers cannot be re-armed +*/ +cancel_work_sync (&peer->tcp.tx_work);I don't think that's ok to call under
rcu_read_lock, it seems it can sleep.
Quoted 3 lines
drivers/net/ovpn/peer.h
Quoted 1 line
drivers/net/ovpn/peer.h +1
-0+strp_done (&peer->tcp.strp);And same here, since
strp_donealso callscancel_work_sync.
hm you're right. I'll see how to re-arrange this part..I expect this to be tricky.
Quoted 12 lines
Quoted 12 lines
drivers/net/ovpn/peer.h +11
-0+rcu_read_unlock (); +} + +static void ovpn_tcp_send_sock (struct ovpn_peer *peer) +{ +struct sk_buff *skb = peer->tcp.out_msg.skb; + +if (!skb) +return ; + +if (peer->tcp.tx_in_progress)
Quoted 8 lines
drivers/net/ovpn/peer.h
Quoted 3 lines
drivers/net/ovpn/peer.h +3
-0+return ; + + peer->tcp.tx_in_progress = true;I'm not convinced this is safe.
ovpn_tcp_send_sockcould run concurrently for the same peer (lock_sockdoesn't excludebh_lock_sockafter the short "grab ownership" phase), so I think both sides could seetx_in_progress= false and then proceed.
I may be missing something here.
I was under the impression that ovpn_tcp_send_sock() is always invoked with lock_sock() held. Shouldn't that be enough to prevent concurrent executions for the same peer/sock?
Quoted 14 lines
drivers/net/ovpn/peer.h
Quoted 9 lines
drivers/net/ovpn/peer.h +7
-0+ do { + int ret = skb_send_sock_locked(peer->sock->sock->sk, skb, + peer->tcp.out_msg.offset, + peer->tcp.out_msg.len); + if (unlikely(ret < 0)) { + if (ret == -EAGAIN) + goto out;This will silently drop the message? And then in case of a userspace message,
ovpn_tcp_sendmsgwill lie to the user (the openvpn client), claiming that the control message was sent (ret = size just above the unlock)?
why do you think the message will be dropped?
By jumping to 'out' we are keeping the skb in peer->tcp.out_msg.skb, with peer->tcp.out_msg.offset and peer->tcp.out_msg.len left untouched and ready for the next attempt triggered by ovpn_tcp_write_space().
Quoted 18 lines
Quoted 7 lines
drivers/net/ovpn/peer.h +6
-0+ +net_warn_ratelimited ("%s: TCP error to peer %u: %d\n" , + peer->ovpn->dev->name, peer->id, + ret); + +/* in case of TCP error we can't recover the VPN drivers/net/ovpn/peer.h
Quoted 10 lines
drivers/net/ovpn/peer.h +10
-0+* stream therefore we abort the connection +*/ +ovpn_peer_del (peer, +OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); +break ; + } + + peer->tcp.out_msg.len -= ret; + peer->tcp.out_msg.offset += ret; + }while (peer->tcp.out_msg.len >0 );
Quoted 8 lines
Another thing that worries me: assume the receiver is a bit slow, the underlying TCP socket gets stuck.
skb_send_sock_lockedmanages to push some data down the TCP socket, but not everything. We advance by that amount, and restart this loop. The socket is still stuck, soskb_send_sock_lockedreturns-EAGAIN. We have only pushed a partial message down to the TCP socket, but we drop the rest? Now the stream is broken, and the next call toovpn_tcp_send_sockwill happily send its message.
I think this is answered above, where I say that we are actually keeping the skb (not dropping it) ready for the next sending attempt.
Quoted 10 lines
ovpn_tcp_send_sockwithmsg_len= 1000
iteration 1
skb_send_sock_lockedreturns 100
advance
iteration 2
skb_send_sock_lockedreturns-EAGAIN
goto out
Quoted 2 lines
So you'd have to keep that partially-sent message around until you can finish pushing it out on the socket.
yap, see above.
Quoted 16 lines
[...]
drivers/net/ovpn/peer.h
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+static int ovpn_tcp_sendmsg (struct sock *sk,struct msghdr *msg,size_t size) +{ +struct ovpn_socket *sock; +int ret, linear =PAGE_SIZE ; +struct ovpn_peer *peer; +struct sk_buff *skb; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); + peer = sock->peer; +rcu_read_unlock ();What's stopping the peer being freed here?
I assumed that while we are in our own sk_cb it should not be possible for a peer to have refcnt reaching 0.
But after double checking I don't think there is any protection about this. I Will add a call to ovpn_peer_hold() and abort if that call fails.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 15/25 ovpn: implement multi-peer support
In reply to Antonio QuartulliWith 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/main.c | 41 ++++++++- drivers/net/ovpn/ovpnstruct.h | 16 ++++ drivers/net/ovpn/peer.c | 152 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 9 ++ 4 files changed, 216 insertions(+), 2 deletions(-)
drivers/net/ovpn/main.c +39 -2
@@ -46,6 +46,17 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->mode = mode;spin_lock_init (&ovpn->lock); +if (mode ==OVPN_MODE_MP ) { +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers) +return -ENOMEM ; + +spin_lock_init (&ovpn->peers->lock); + } +return 0 ; }
@@ -54,14 +65,34 @@ static void ovpn_struct_free(struct net_device *net)struct ovpn_struct *ovpn =netdev_priv (net);gro_cells_destroy (&ovpn->gro_cells); +kfree (ovpn->peers);rcu_barrier (); }static int ovpn_net_init (struct net_device *dev) {struct ovpn_struct *ovpn =netdev_priv (dev); +int err =gro_cells_init (&ovpn->gro_cells, dev); +struct in_device *dev_v4; + +if (err) +return err; -return gro_cells_init (&ovpn->gro_cells, dev); +if (ovpn->mode ==OVPN_MODE_MP ) { + dev_v4 =__in_dev_get_rtnl (dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly. +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; + } + } +return 0 ; }static int ovpn_net_open (struct net_device *dev)
@@ -210,8 +241,14 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn) ovpn->registered = false; -if (ovpn->mode ==OVPN_MODE_P2P ) +switch (ovpn->mode) { +case OVPN_MODE_P2P :ovpn_peer_release_p2p (ovpn); +break ; +default : +ovpn_peers_free (ovpn); +break ; + } } static int ovpn_netdev_notifier_call(struct notifier_block *nb,
drivers/net/ovpn/ovpnstruct.h +16 -0
@@ -13,12 +13,27 @@#include <net/gro_cells.h> #include <uapi/linux/ovpn.h> +/** +* struct ovpn_peer_collection - container of peers for MultiPeer mode +* @by_id: table of peers index by ID +* @by_transp_addr: table of peers indexed by transport address +* @by_vpn_addr: table of peers indexed by VPN IP address +* @lock: protects writes to peers tables +*/ +struct ovpn_peer_collection { + DECLARE_HASHTABLE(by_id, 12); + DECLARE_HASHTABLE(by_transp_addr, 12); + DECLARE_HASHTABLE(by_vpn_addr, 12); +spinlock_t lock;/* protects writes to peers tables */ +}; +/** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel * @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) * @lock: protect this object +* @peers: data structures holding multi-peer references * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list * @gro_cells: pointer to the Generic Receive Offload cell
@@ -28,6 +43,7 @@ struct ovpn_struct {bool registered;enum ovpn_mode mode;spinlock_t lock;/* protect writing to the ovpn_struct object */ +struct ovpn_peer_collection *peers;struct ovpn_peer __rcu *peer;struct list_head dev_list;struct gro_cells gro_cells;
drivers/net/ovpn/peer.c +152 -0
185 lines not on this page · raw message
drivers/net/ovpn/peer.h +9 -0
@@ -30,6 +30,10 @@* @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @hash_entry_id: entry in the peer ID hashtable +* @hash_entry_addr4: entry in the peer IPv4 hashtable +* @hash_entry_addr6: entry in the peer IPv6 hashtable +* @hash_entry_transp_addr: entry in the peer transport address hashtable * @sock: the socket being used to talk to this peer * @tcp: keeps track of TCP specific state * @tcp.strp: stream parser context (TCP only)
@@ -62,6 +66,10 @@ struct ovpn_peer {struct in_addr ipv4;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;struct ovpn_socket *sock;/* state of the TCP reading. Needed to keep track of how much of a
@@ -126,6 +134,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id);int ovpn_peer_add (struct ovpn_struct *ovpn,struct ovpn_peer *peer);int ovpn_peer_del (struct ovpn_peer *peer,enum ovpn_del_peer_reason reason);void ovpn_peer_release_p2p (struct ovpn_struct *ovpn); +void ovpn_peers_free (struct ovpn_struct *ovpn);struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb);
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 15/25] ovpn: implement multi-peer support
drivers/net/ovpn/main.c static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) · line 55
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/main.c +7
-0@@ -46,6 +46,17 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->mode = mode;spin_lock_init (&ovpn->lock); +if (mode ==OVPN_MODE_MP ) { +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers) +return -ENOMEM ;
As we discussed in v3 (it was about the workqueue), I don't think this will get freed correctly in some of the failure cases. This should go in ovpn_net_init.
Quoted 6 lines
drivers/net/ovpn/main.c +4
-0+ +spin_lock_init (&ovpn->peers->lock); + } +return 0 ; }
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 15/25] ovpn: implement multi-peer support
Hi,
Quoted 5 lines · Sabrina Dubroca
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/main.c +0
-0@@ -46,6 +46,17 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->mode = mode;spin_lock_init (&ovpn->lock);
Quoted 11 lines
drivers/net/ovpn/main.c static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) · line 55
Quoted 7 lines
drivers/net/ovpn/main.c +7
-0+if (mode ==OVPN_MODE_MP ) { +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers) +return -ENOMEM ;As we discussed in v3 (it was about the workqueue), I don't think this will get freed correctly in some of the failure cases. This should go in
ovpn_net_init.
Argh. Right. I will move it there.
Quoted 8 lines
drivers/net/ovpn/main.c static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) · line 61
Quoted 7 lines
drivers/net/ovpn/main.c +4
-0+ +spin_lock_init (&ovpn->peers->lock); + } +return 0 ; }
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 16/25 ovpn: implement peer lookup logic
In reply to Antonio QuartulliIn a multi-peer scenario there are a number of situations when a specific peer needs to be looked up.
We may want to lookup a peer by:
1. its ID
2. its VPN destination IP
3. its transport IP/port couple
For each of the above, there is a specific routing table referencing all peers for fast look up.
Case 2. is a bit special in the sense that an outgoing packet may not be sent to the peer VPN IP directly, but rather to a network behind it. For this reason we first perform a nexthop lookup in the system routing table and then we use the retrieved nexthop as peer search key.
--- drivers/net/ovpn/peer.c | 299 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 274 insertions(+), 25 deletions(-)
drivers/net/ovpn/peer.c +274 -25
@@ -10,6 +10,7 @@#include <linux/skbuff.h> #include <linux/list.h> #include <linux/hashtable.h> +#include <net/ip6_route.h> #include "ovpnstruct.h" #include "bind.h"
@@ -65,9 +66,6 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)return peer; } -#define ovpn_peer_index (_tbl, _key, _key_len) \ - (jhash(_key, _key_len, 0) % HASH_SIZE(_tbl)) \ -/** * ovpn_peer_release - release peer private members * @peer: the peer to release
@@ -129,6 +127,91 @@ static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb,return true; } +/** +* ovpn_nexthop_from_skb4 - retrieve IPv4 nexthop for outgoing skb +* @skb: the outgoing packet +* +* Return: the IPv4 of the nexthop +*/ +static __be32 ovpn_nexthop_from_skb4 (struct sk_buff *skb) +{ +const struct rtable *rt =skb_rtable (skb); + +if (rt && rt->rt_uses_gateway) +return rt->rt_gw4; + +return ip_hdr (skb)->daddr; +} + +/** +* ovpn_nexthop_from_skb6 - retrieve IPv6 nexthop for outgoing skb +* @skb: the outgoing packet +* +* Return: the IPv6 of the nexthop +*/ +static struct in6_addr ovpn_nexthop_from_skb6 (struct sk_buff *skb) +{ +const struct rt6_info *rt =skb_rt6_info (skb); + +if (!rt || !(rt->rt6i_flags &RTF_GATEWAY )) +return ipv6_hdr (skb)->daddr; + +return rt->rt6i_gateway; +} + +#define ovpn_get_hash_head (_tbl, _key, _key_len) \ + (&(_tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(_tbl)]) \ + +/** +* ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address +* @ovpn: the openvpn instance to search +* @addr: VPN IPv4 to use as search key +* +* Refcounter is not increased for the returned peer. +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4 (struct ovpn_struct *ovpn, +__be32 addr) +{ +struct hlist_head *head; +struct ovpn_peer *tmp; + + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, &addr, +sizeof (addr)); + +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr4) +if (addr == tmp->vpn_addrs.ipv4.s_addr) +return tmp; + +return NULL ; +} + +/** +* ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address +* @ovpn: the openvpn instance to search +* @addr: VPN IPv6 to use as search key +* +* Refcounter is not increased for the returned peer. +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6 (struct ovpn_struct *ovpn, +struct in6_addr *addr) +{ +struct hlist_head *head; +struct ovpn_peer *tmp; + + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, addr, +sizeof (*addr)); + +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr6) +if (ipv6_addr_equal (addr, &tmp->vpn_addrs.ipv6)) +return tmp; + +return NULL ; +} +/** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from
@@ -205,14 +288,42 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn,struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb) { -struct ovpn_peer *peer =NULL ; +struct ovpn_peer *tmp, *peer =NULL ;struct sockaddr_storage ss = {0 }; +struct hlist_head *head; +size_t sa_len;if (unlikely (!ovpn_peer_skb_to_sockaddr (skb, &ss)))return NULL ;if (ovpn->mode ==OVPN_MODE_P2P ) - peer =ovpn_peer_get_by_transp_addr_p2p (ovpn, &ss); +return ovpn_peer_get_by_transp_addr_p2p (ovpn, &ss); + +switch (ss.ss_family) { +case AF_INET : + sa_len =sizeof (struct sockaddr_in ); +break ; +case AF_INET6 : + sa_len =sizeof (struct sockaddr_in6 ); +break ; +default : +return NULL ; + } + + head =ovpn_get_hash_head (ovpn->peers->by_transp_addr, &ss, sa_len); + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_transp_addr) { +if (!ovpn_peer_transp_match (tmp, &ss)) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -247,10 +358,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn,*/ struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id) { -struct ovpn_peer *peer =NULL ; +struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head;if (ovpn->mode ==OVPN_MODE_P2P ) - peer =ovpn_peer_get_by_id_p2p (ovpn, peer_id); +return ovpn_peer_get_by_id_p2p (ovpn, peer_id); + + head =ovpn_get_hash_head (ovpn->peers->by_id, &peer_id, +sizeof (peer_id)); + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -272,6 +400,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb) {struct ovpn_peer *peer =NULL ; +struct in6_addr addr6; +__be32 addr4;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side
@@ -282,11 +412,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,if (unlikely (peer && !ovpn_peer_hold (peer))) peer =NULL ;rcu_read_unlock (); +return peer; + } + +rcu_read_lock (); +switch (skb_protocol_to_family (skb)) { +case AF_INET : + addr4 =ovpn_nexthop_from_skb4 (skb); + peer =ovpn_peer_get_by_vpn_addr4 (ovpn, addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_skb6 (skb); + peer =ovpn_peer_get_by_vpn_addr6 (ovpn, &addr6); +break ; } +if (unlikely (peer && !ovpn_peer_hold (peer))) + peer =NULL ; +rcu_read_unlock (); +return peer; } +/** +* ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dest: the destination to be looked up +* +* Looks up in the IPv4 system routing table the IP of the nexthop to be 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. +* +* Return: the IP of the next hop if found or dest itself otherwise +*/ +static __be32 ovpn_nexthop_from_rt4 (struct ovpn_struct *ovpn,__be32 dest) +{ +struct rtable *rt; +struct flowi4 fl = { + .daddr = dest + }; + + rt =ip_route_output_flow (dev_net (ovpn->dev), &fl,NULL ); +if (IS_ERR (rt)) { +net_dbg_ratelimited ("%s: no route to host %pI4\n" , __func__, + &dest); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dest; + } + +if (!rt->rt_uses_gateway) + goto out; + + dest = rt->rt_gw4; +out: +ip_rt_put (rt); +return dest; +} + +/** +* ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dest: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IP of the nexthop to be 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. +* +* Return: the IP of the next hop if found or dest itself otherwise +*/ +static struct in6_addr ovpn_nexthop_from_rt6 (struct ovpn_struct *ovpn, +struct in6_addr dest) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +struct dst_entry *entry; +struct rt6_info *rt; +struct flowi6 fl = { + .daddr = dest, + }; + + entry = ipv6_stub->ipv6_dst_lookup_flow (dev_net (ovpn->dev),NULL , &fl, +NULL ); +if (IS_ERR (entry)) { +net_dbg_ratelimited ("%s: no route to host %pI6c\n" , __func__, + &dest); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dest; + } + + rt =dst_rt6_info (entry); + +if (!(rt->rt6i_flags &RTF_GATEWAY )) + goto out; + + dest = rt->rt6i_gateway; +out: +dst_release ((struct dst_entry *)rt); +#endif +return dest; +} +/** * ovpn_peer_check_by_src - check that skb source is routed via peer * @ovpn: the openvpn instance to search
@@ -299,6 +527,8 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer) {bool match = false; +struct in6_addr addr6; +__be32 addr4;if (ovpn->mode ==OVPN_MODE_P2P ) {/* in P2P mode, no matter the destination, packets are always
@@ -307,6 +537,28 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb,rcu_read_lock (); match = (peer ==rcu_dereference (ovpn->peer));rcu_read_unlock (); +return match; + } + +/* This function performs a reverse path check, therefore we now +* lookup the nexthop we would use if we wanted to route a packet +* to the source IP. If the nexthop matches the sender we know the +* latter is valid and we allow the packet to come in +*/ + +switch (skb_protocol_to_family (skb)) { +case AF_INET : + addr4 =ovpn_nexthop_from_rt4 (ovpn,ip_hdr (skb)->saddr); +rcu_read_lock (); + match = (peer ==ovpn_peer_get_by_vpn_addr4 (ovpn, addr4)); +rcu_read_unlock (); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_rt6 (ovpn,ipv6_hdr (skb)->saddr); +rcu_read_lock (); + match = (peer ==ovpn_peer_get_by_vpn_addr6 (ovpn, &addr6)); +rcu_read_unlock (); +break ; }return match;
@@ -324,11 +576,11 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer)struct sockaddr_storage sa = {0 };struct sockaddr_in6 *sa6;struct sockaddr_in *sa4; +struct hlist_head *head;struct ovpn_bind *bind;struct ovpn_peer *tmp;size_t salen;int ret =0 ; -u32 index;spin_lock_bh (&ovpn->peers->lock);/* do not add duplicates */
@@ -364,30 +616,27 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) goto unlock; } - index =ovpn_peer_index (ovpn->peers->by_transp_addr, &sa, - salen); -hlist_add_head_rcu (&peer->hash_entry_transp_addr, - &ovpn->peers->by_transp_addr[index]); + head =ovpn_get_hash_head (ovpn->peers->by_transp_addr, &sa, + salen); +hlist_add_head_rcu (&peer->hash_entry_transp_addr, head); } - index =ovpn_peer_index (ovpn->peers->by_id, &peer->id, -sizeof (peer->id)); -hlist_add_head_rcu (&peer->hash_entry_id, &ovpn->peers->by_id[index]); +hlist_add_head_rcu (&peer->hash_entry_id, +ovpn_get_hash_head (ovpn->peers->by_id, &peer->id, +sizeof (peer->id)));if (peer->vpn_addrs.ipv4.s_addr !=htonl (INADDR_ANY )) { - index =ovpn_peer_index (ovpn->peers->by_vpn_addr, - &peer->vpn_addrs.ipv4, -sizeof (peer->vpn_addrs.ipv4)); -hlist_add_head_rcu (&peer->hash_entry_addr4, - &ovpn->peers->by_vpn_addr[index]); + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv4, +sizeof (peer->vpn_addrs.ipv4)); +hlist_add_head_rcu (&peer->hash_entry_addr4, head); }if (!ipv6_addr_any (&peer->vpn_addrs.ipv6)) { - index =ovpn_peer_index (ovpn->peers->by_vpn_addr, - &peer->vpn_addrs.ipv6, -sizeof (peer->vpn_addrs.ipv6)); -hlist_add_head_rcu (&peer->hash_entry_addr6, - &ovpn->peers->by_vpn_addr[index]); + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, + &peer->vpn_addrs.ipv6, +sizeof (peer->vpn_addrs.ipv6)); +hlist_add_head_rcu (&peer->hash_entry_addr6, head); } unlock:
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 16/25] ovpn: implement peer lookup logic
Quoted 4 lines · Antonio Quartulli
/**
*ovpn_peer_check_by_src- check that skb source is routed via peer
* @ovpn: the openvpn instance to search
* @skb: the packet to extra source address from
nit, just noticed now but should be fixed in patch 12: s/to extra/to extract/
[...]
Quoted 15 lines
drivers/net/ovpn/peer.c +1
-1@@ -324,11 +576,11 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer)struct sockaddr_storage sa = {0 };struct sockaddr_in6 *sa6;struct sockaddr_in *sa4; +struct hlist_head *head;struct ovpn_bind *bind;struct ovpn_peer *tmp;size_t salen;int ret =0 ; -u32 index;spin_lock_bh (&ovpn->peers->lock);/* do not add duplicates */ @@ -364,30 +616,27 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) goto unlock;
drivers/net/ovpn/peer.c static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) · line 622
Quoted 10 lines
drivers/net/ovpn/peer.c +3
-4} - index =ovpn_peer_index (ovpn->peers->by_transp_addr, &sa, - salen); -hlist_add_head_rcu (&peer->hash_entry_transp_addr, - &ovpn->peers->by_transp_addr[index]); + head =ovpn_get_hash_head (ovpn->peers->by_transp_addr, &sa, + salen); +hlist_add_head_rcu (&peer->hash_entry_transp_addr, head); }
These changes to ovpn_peer_add_mp (and the replacement of ovpn_peer_index with ovpn_get_hash_head) could be squashed into the previous patch.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 16/25] ovpn: implement peer lookup logic
Hi,
Quoted 7 lines · Sabrina Dubroca
Quoted 4 lines · Antonio Quartulli
/**
*ovpn_peer_check_by_src- check that skb source is routed via peer
* @ovpn: the openvpn instance to search
* @skb: the packet to extra source address fromnit, just noticed now but should be fixed in patch 12: s/to extra/to extract/
argh. thanks!
Quoted 19 lines
[...]
Quoted 17 lines
drivers/net/ovpn/peer.c +1
-1@@ -324,11 +576,11 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer)struct sockaddr_storage sa = {0 };struct sockaddr_in6 *sa6;struct sockaddr_in *sa4; +struct hlist_head *head;struct ovpn_bind *bind;struct ovpn_peer *tmp;size_t salen;int ret =0 ; -u32 index;spin_lock_bh (&ovpn->peers->lock);/* do not add duplicates */ @@ -364,30 +616,27 @@ static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) goto unlock; }
Quoted 12 lines
drivers/net/ovpn/peer.c static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) · line 622
Quoted 8 lines
drivers/net/ovpn/peer.c +3
-4- index =ovpn_peer_index (ovpn->peers->by_transp_addr, &sa, - salen); -hlist_add_head_rcu (&peer->hash_entry_transp_addr, - &ovpn->peers->by_transp_addr[index]); + head =ovpn_get_hash_head (ovpn->peers->by_transp_addr, &sa, + salen); +hlist_add_head_rcu (&peer->hash_entry_transp_addr, head); }These changes to
ovpn_peer_add_mp(and the replacement ofovpn_peer_indexwithovpn_get_hash_head) could be squashed into the previous patch.
hmm true. will do.
Thanks!
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 17/25 ovpn: implement keepalive mechanism
In reply to Antonio QuartulliOpenVPN 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 | 82 +++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/io.h | 1 + drivers/net/ovpn/peer.c | 72 ++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 45 ++++++++++++++++++++++ 4 files changed, 200 insertions(+)
drivers/net/ovpn/io.c +82 -0
@@ -25,6 +25,31 @@#include "udp.h" #include "skb.h" +static const unsigned char ovpn_keepalive_message[] = { +0x2a ,0x18 ,0x7b ,0xf3 ,0x64 ,0x1e ,0xb4 ,0xcb , +0x07 ,0xed ,0x2d ,0x0a ,0x98 ,0x1f ,0xc7 ,0x48 +}; + +/** +* ovpn_is_keepalive - check if skb contains a keepalive message +* @skb: packet to check +* +* Assumes that the first byte of skb->data is defined. +* +* Return: true if skb contains a keepalive or false otherwise +*/ +static bool ovpn_is_keepalive (struct sk_buff *skb) +{ +if (*skb->data !=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)); +} +/* Called after decrypt to write the IP packet to the device. * This method is expected to manage/free the skb. */
@@ -91,6 +116,9 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* note event of authenticated packet received for keepalive */ +ovpn_peer_keepalive_recv_reset (peer); +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->payload_offset);
@@ -107,6 +135,12 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id); + goto drop; + } +net_info_ratelimited ("%s: unsupported protocol received from peer %u\n" , peer->ovpn->dev->name, peer->id); goto drop;
@@ -203,6 +237,7 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret)/* no transport configured yet */ goto err; } +ovpn_peer_keepalive_xmit_reset (peer);/* skb passed down the stack - don't free it */ skb =NULL ; err:
@@ -345,3 +380,50 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)kfree_skb_list (skb);return NET_XMIT_DROP ; } + +/** +* ovpn_xmit_special - encrypt and transmit an out-of-band message to peer +* @peer: peer to send the message to +* @data: message content +* @len: message length +* +* Assumes that caller holds a reference to peer +*/ +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_send (ovpn, skb, peer); +} + +/** +* ovpn_keepalive_xmit - send keepalive message to peer +* @peer: the peer to send the message to +*/ +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
@@ -13,6 +13,7 @@netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev);void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb); +void ovpn_keepalive_xmit (struct ovpn_peer *peer);void ovpn_encrypt_post (struct sk_buff *skb,int ret);void ovpn_decrypt_post (struct sk_buff *skb,int ret);
drivers/net/ovpn/peer.c +72 -0
93 lines not on this page · raw message
drivers/net/ovpn/peer.h +45 -0
61 lines not on this page · raw message
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Quoted 6 lines · Antonio Quartulli
drivers/net/ovpn/io.c +6
-0+static const unsigned char ovpn_keepalive_message[] = { +0x2a ,0x18 ,0x7b ,0xf3 ,0x64 ,0x1e ,0xb4 ,0xcb , +0x07 ,0xed ,0x2d ,0x0a ,0x98 ,0x1f ,0xc7 ,0x48 +}; + +/**
drivers/net/ovpn/io.c line 43
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+* ovpn_is_keepalive - check if skb contains a keepalive message +* @skb: packet to check +* +* Assumes that the first byte of skb->data is defined. +* +* Return: true if skb contains a keepalive or false otherwise +*/ +static bool ovpn_is_keepalive (struct sk_buff *skb) +{ +if (*skb->data !=OVPN_KEEPALIVE_FIRST_BYTE )
You could use ovpn_keepalive_message[0], and then you wouldn't need this extra constant.
drivers/net/ovpn/io.c line 50
Quoted 7 lines
drivers/net/ovpn/io.c +7
-0+return false; + +if (!pskb_may_pull (skb,sizeof (ovpn_keepalive_message))) +return false; + +return !memcmp (skb->data, ovpn_keepalive_message, +sizeof (ovpn_keepalive_message));
Is a packet that contains some extra bytes after the exact keepalive considered a valid keepalive, or does it need to be the correct length?
Quoted 16 lines
drivers/net/ovpn/io.c +5
-0+} +/* Called after decrypt to write the IP packet to the device. * This method is expected to manage/free the skb. */ @@ -91,6 +116,9 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* note event of authenticated packet received for keepalive */ +ovpn_peer_keepalive_recv_reset (peer); +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->payload_offset);
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 6 lines
drivers/net/ovpn/io.c +3
-0@@ -107,6 +135,12 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);
That should probably be _ratelimited, but it seems we don't have _ratelimited variants for the netdev_* helpers.
Quoted 9 lines
drivers/net/ovpn/io.c +9
-0+/** +* ovpn_xmit_special - encrypt and transmit an out-of-band message to peer +* @peer: peer to send the message to +* @data: message content +* @len: message length +* +* Assumes that caller holds a reference to peer +*/ +static void ovpn_xmit_special(struct ovpn_peer *peer, const void *data,
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 402
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ 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 );
Where is that 256 coming from?
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (unlikely(!skb)) + return;
Failure to send a keepalive should probably have a counter, to help users troubleshoot why their connection dropped.
(can be done later unless someone insists)
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 406
Quoted 1 line
drivers/net/ovpn/io.c +1
-0+skb_reserve (skb,128 );
And that 128?
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 408
Quoted 2 lines
drivers/net/ovpn/io.c +2
-0+ skb->priority =TC_PRIO_BESTEFFORT ; +memcpy (__skb_put (skb, len), data, len);
nit: that's __skb_put_data
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+/* 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_send (ovpn, skb, peer); +}
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 429
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ +/** +* ovpn_keepalive_xmit - send keepalive message to peer +* @peer: the peer to send the message to +*/ +void ovpn_keepalive_xmit (struct ovpn_peer *peer) +{ +ovpn_xmit_special (peer, ovpn_keepalive_message, +sizeof (ovpn_keepalive_message)); +}
I don't see other users of ovpn_xmit_special in this series, if you don't have more planned in the future you could drop the extra function.
Quoted 2 lines
+/**
+ *ovpn_peer_expire- timer task for incoming keepialive timeout
typo: s/keepialive/keepalive/
Quoted 8 lines
drivers/net/ovpn/peer.c +8
-0+/** +* ovpn_peer_keepalive_set - configure keepalive values for peer +* @peer: the peer to configure +* @interval: outgoing keepalive interval +* @timeout: incoming keepalive timeout +*/ +void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout) +{
drivers/net/ovpn/peer.c line 68
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+u32 delta; + +netdev_dbg (peer->ovpn->dev, +"%s: scheduling keepalive for peer %u: interval=%u timeout=%u\n" , + __func__, peer->id, interval, timeout); + + peer->keepalive_interval = interval; +if (interval >0 ) { + delta =msecs_to_jiffies (interval *MSEC_PER_SEC ); +mod_timer (&peer->keepalive_xmit, jiffies + delta);
Maybe something to consider in the future: this could be resetting a timer that was just about to go off to a somewhat distant time in the future. Not sure the peer will be happy about that (and not consider it a timeout).
drivers/net/ovpn/peer.c line 74
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+ }else { +timer_delete (&peer->keepalive_xmit); + } + + peer->keepalive_timeout = timeout; +if (timeout) {
pedantic nit: inconsistent style with the "interval > 0" test just above
Quoted 7 lines
drivers/net/ovpn/peer.c +7
-0+ delta =msecs_to_jiffies (timeout *MSEC_PER_SEC ); +mod_timer (&peer->keepalive_recv, jiffies + delta); + }else { +timer_delete (&peer->keepalive_recv); + } +} +
[...]
Quoted 5 lines
drivers/net/ovpn/peer.h +5
-0+/** +* ovpn_peer_keepalive_recv_reset - reset keepalive timeout +* @peer: peer for which the timeout should be reset +* +* To be invoked upon reception of an authenticated packet from peer in order
drivers/net/ovpn/peer.h struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 169
Quoted 10 lines
drivers/net/ovpn/peer.h +10
-0+* to report valid activity and thus reset the keepalive timeout +*/ +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);
This (and ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Hi,
Quoted 12 lines · Sabrina Dubroca
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +11
-0+static const unsigned char ovpn_keepalive_message[] = { +0x2a ,0x18 ,0x7b ,0xf3 ,0x64 ,0x1e ,0xb4 ,0xcb , +0x07 ,0xed ,0x2d ,0x0a ,0x98 ,0x1f ,0xc7 ,0x48 +}; + +/** +* ovpn_is_keepalive - check if skb contains a keepalive message +* @skb: packet to check +* +* Assumes that the first byte of skb->data is defined. +*
Quoted 8 lines
drivers/net/ovpn/io.c line 43
Quoted 5 lines
+ * Return: true if skb contains a keepalive or false otherwise
+ */drivers/net/ovpn/io.c +3
-0+static bool ovpn_is_keepalive (struct sk_buff *skb) +{ +if (*skb->data !=OVPN_KEEPALIVE_FIRST_BYTE )You could use ovpn_keepalive_message[0], and then you wouldn't need this extra constant.
Indeed, shame on me, will do as suggested
Quoted 12 lines
drivers/net/ovpn/io.c line 50
Quoted 8 lines
drivers/net/ovpn/io.c +7
-0+return false; + +if (!pskb_may_pull (skb,sizeof (ovpn_keepalive_message))) +return false; + +return !memcmp (skb->data, ovpn_keepalive_message, +sizeof (ovpn_keepalive_message));Is a packet that contains some extra bytes after the exact keepalive considered a valid keepalive, or does it need to be the correct length?
I checked the userspace code and it assumes the length of the received keepalive message to be the same as the ovpn_keepalive_message array.
So no extra byte expected, otherwise the message is not considered a keepalive anymore.
This means I must add an extra check before the memcmp to make sure there is no extra data.
Good catch, thanks!
Quoted 20 lines
Quoted 20 lines
drivers/net/ovpn/io.c +5
-0+} +/* Called after decrypt to write the IP packet to the device. * This method is expected to manage/free the skb. */ @@ -91,6 +116,9 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* note event of authenticated packet received for keepalive */ +ovpn_peer_keepalive_recv_reset (peer); +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->payload_offset);@@ -107,6 +135,12 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; }
Quoted 6 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);That should probably be
_ratelimited, but it seems we don't have_ratelimitedvariants for the netdev_* helpers.
Right.
I have used the net_*_ratelimited() variants when needed. Too bad we don't have those.
Quoted 16 lines
Quoted 16 lines
drivers/net/ovpn/io.c +13
-0+/** +* ovpn_xmit_special - encrypt and transmit an out-of-band message to peer +* @peer: peer to send the message to +* @data: message content +* @len: message length +* +* Assumes that caller holds a reference to peer +*/ +static void ovpn_xmit_special (struct ovpn_peer *peer,const void *data, +const unsigned int len) +{ +struct ovpn_struct *ovpn; +struct sk_buff *skb;
Quoted 8 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 402
Quoted 6 lines
drivers/net/ovpn/io.c +6
-0+ + ovpn = peer->ovpn; +if (unlikely (!ovpn)) +return ; + + skb =alloc_skb (256 + len,GFP_ATOMIC );Where is that 256 coming from?
"Reasonable number" which should be enough[tm] to hold the entire packet.
Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ if (unlikely(!skb)) + return;Failure to send a keepalive should probably have a counter, to help users troubleshoot why their connection dropped.
(can be done later unless someone insists)
This will be part of a more sophisticated error counting that I will introduce later on.
Quoted 5 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 406
Quoted 3 lines
drivers/net/ovpn/io.c +1
-0+skb_reserve (skb,128 );And that 128?
same "logic" as 256.
Quoted 5 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 408
Quoted 3 lines
drivers/net/ovpn/io.c +2
-0+ skb->priority =TC_PRIO_BESTEFFORT ; +memcpy (__skb_put (skb, len), data, len);nit: that's
__skb_put_data
oh cool, thanks!
Quoted 16 lines
Quoted 16 lines
drivers/net/ovpn/io.c +15
-0+/* 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_send (ovpn, skb, peer); +} + +/** +* ovpn_keepalive_xmit - send keepalive message to peer +* @peer: the peer to send the message to +*/
Quoted 8 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 429
Quoted 5 lines
drivers/net/ovpn/io.c +5
-0+void ovpn_keepalive_xmit (struct ovpn_peer *peer) +{ +ovpn_xmit_special (peer, ovpn_keepalive_message, +sizeof (ovpn_keepalive_message)); +}I don't see other users of
ovpn_xmit_specialin this series, if you don't have more planned in the future you could drop the extra function.
initially there were plans, but I have always fought back any idea about adding more unnecessary logic to the kernel side. So for now there is nothing planned. I'll remove the extra wrapper.
Quoted 6 lines
Quoted 4 lines
+/**
+ *ovpn_peer_expire- timer task for incoming keepialive timeouttypo: s/keepialive/keepalive/
Thanks
Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/peer.c +15
-0+/** +* ovpn_peer_keepalive_set - configure keepalive values for peer +* @peer: the peer to configure +* @interval: outgoing keepalive interval +* @timeout: incoming keepalive timeout +*/ +void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout) +{ +u32 delta; + +netdev_dbg (peer->ovpn->dev, +"%s: scheduling keepalive for peer %u: interval=%u timeout=%u\n" , + __func__, peer->id, interval, timeout); + + peer->keepalive_interval = interval;
Quoted 8 lines
drivers/net/ovpn/peer.c line 68
Quoted 3 lines
drivers/net/ovpn/peer.c +3
-0+if (interval >0 ) { + delta =msecs_to_jiffies (interval *MSEC_PER_SEC ); +mod_timer (&peer->keepalive_xmit, jiffies + delta);Maybe something to consider in the future: this could be resetting a timer that was just about to go off to a somewhat distant time in the future. Not sure the peer will be happy about that (and not consider it a timeout).
Normally this timer is only set upon connection, or maybe upon some future parameter exchange. In both cases we can assume the connection is alive, so this case should not scare us.
But thanks for pointing it out
Quoted 10 lines
drivers/net/ovpn/peer.c line 74
Quoted 7 lines
drivers/net/ovpn/peer.c +6
-0+ }else { +timer_delete (&peer->keepalive_xmit); + } + + peer->keepalive_timeout = timeout; +if (timeout) {pedantic nit: inconsistent style with the "interval > 0" test just above
ACK, will make them uniform.
Quoted 21 lines
Quoted 8 lines
drivers/net/ovpn/peer.c +7
-0+ delta =msecs_to_jiffies (timeout *MSEC_PER_SEC ); +mod_timer (&peer->keepalive_recv, jiffies + delta); + }else { +timer_delete (&peer->keepalive_recv); + } +} +[...]
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+/** +* ovpn_peer_keepalive_recv_reset - reset keepalive timeout +* @peer: peer for which the timeout should be reset +* +* To be invoked upon reception of an authenticated packet from peer in order +* to report valid activity and thus reset the keepalive timeout +*/ +static inline void ovpn_peer_keepalive_recv_reset (struct ovpn_peer *peer) +{ +u32 delta =msecs_to_jiffies (peer->keepalive_timeout *MSEC_PER_SEC ); +
Quoted 9 lines
drivers/net/ovpn/peer.h struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 169
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+if (unlikely (!delta)) +return ; + +mod_timer (&peer->keepalive_recv, jiffies + delta);This (and
ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.
May it even introduce some performance penalty?
Maybe we should get rid of the timer object and introduce a periodic (1s) worker which checks some last_recv timestamp on every known peer? What do you think?
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Hi,
Quoted 8 lines · Antonio Quartulli
Quoted 4 lines · Sabrina Dubroca
This (and
ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.
Quoted 5 lines
May it even introduce some performance penalty?
Maybe we should get rid of the timer object and introduce a periodic (1s) worker which checks some
last_recvtimestamp on every known peer? What do you think?
FWIW In NATT keepalive for IPsec the first RFC was using timers and the workqueue
approach was suggested [1], and later implemented [2]. The code is currently in net-next.
Eyal.
[1] https://linux-ipsec.org/pipermail/devel/2023/000283.html
[2] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Antonio Quartulli
maintainerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Hi Eyal,
thanks a lot for chiming in.
Quoted 22 lines · Eyal Birger
Hi,
Quoted 8 lines · Antonio Quartulli
Quoted 4 lines · Sabrina Dubroca
This (and
ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.Quoted 5 lines
May it even introduce some performance penalty?
Maybe we should get rid of the timer object and introduce a periodic (1s) worker which checks some
last_recvtimestamp on every known peer? What do you think?FWIW In NATT keepalive for IPsec the first RFC was using timers and the workqueue
approach was suggested [1], and later implemented [2]. The code is currently in net-next.
Quoted 4 lines
Eyal.
[1] https://linux-ipsec.org/pipermail/devel/2023/000283.html
[2] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Thanks for these pointers.
Basically this is pretty much what I had in mind, but rather than running the worker every second, the next run is scheduled based on the closest expiration time.
I like it!
Since this worker has no other housekeeping to do, I will go with this approach as well.
Thanks!
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
In reply to Antonio QuartulliQuoted 26 lines · Antonio Quartulli
Hi,
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/io.c +13
-0+/** +* ovpn_xmit_special - encrypt and transmit an out-of-band message to peer +* @peer: peer to send the message to +* @data: message content +* @len: message length +* +* Assumes that caller holds a reference to peer +*/ +static void ovpn_xmit_special (struct ovpn_peer *peer,const void *data, +const unsigned int len) +{ +struct ovpn_struct *ovpn; +struct sk_buff *skb;Quoted 8 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 402
Quoted 6 lines
drivers/net/ovpn/io.c +6
-0+ + ovpn = peer->ovpn; +if (unlikely (!ovpn)) +return ; + + skb =alloc_skb (256 + len,GFP_ATOMIC );Where is that 256 coming from?
Quoted 1 line
"Reasonable number" which should be enough[tm] to hold the entire packet.
Ok, let's go with that for now, unless someone else wants you to change it.
Quoted 9 lines
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (unlikely(!skb)) + return;Failure to send a keepalive should probably have a counter, to help users troubleshoot why their connection dropped.
(can be done later unless someone insists)This will be part of a more sophisticated error counting that I will introduce later on.
Cool, thanks.
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/ovpn/peer.c +15
-0+/** +* ovpn_peer_keepalive_set - configure keepalive values for peer +* @peer: the peer to configure +* @interval: outgoing keepalive interval +* @timeout: incoming keepalive timeout +*/ +void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout) +{ +u32 delta; + +netdev_dbg (peer->ovpn->dev, +"%s: scheduling keepalive for peer %u: interval=%u timeout=%u\n" , + __func__, peer->id, interval, timeout); + + peer->keepalive_interval = interval;Quoted 8 lines
drivers/net/ovpn/peer.c line 68
Quoted 3 lines
drivers/net/ovpn/peer.c +3
-0+if (interval >0 ) { + delta =msecs_to_jiffies (interval *MSEC_PER_SEC ); +mod_timer (&peer->keepalive_xmit, jiffies + delta);Maybe something to consider in the future: this could be resetting a timer that was just about to go off to a somewhat distant time in the future. Not sure the peer will be happy about that (and not consider it a timeout).
Quoted 5 lines
Normally this timer is only set upon connection, or maybe upon some future parameter exchange. In both cases we can assume the connection is alive, so this case should not scare us.
But thanks for pointing it out
Ok, I was thinking about updates while the connection is fully established. If it's only done during setup, it shouldn't be a problem.
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+/** +* ovpn_peer_keepalive_recv_reset - reset keepalive timeout +* @peer: peer for which the timeout should be reset +* +* To be invoked upon reception of an authenticated packet from peer in order +* to report valid activity and thus reset the keepalive timeout +*/ +static inline void ovpn_peer_keepalive_recv_reset (struct ovpn_peer *peer) +{ +u32 delta =msecs_to_jiffies (peer->keepalive_timeout *MSEC_PER_SEC ); +Quoted 9 lines
drivers/net/ovpn/peer.h struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 169
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+if (unlikely (!delta)) +return ; + +mod_timer (&peer->keepalive_recv, jiffies + delta);This (and
ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.
Quoted 1 line
May it even introduce some performance penalty?
That's what I was worried about, yes.
I asked Paolo, he suggested checking that we're actually doing any change to the timer:
if (new_timeout_time != old_timeout_time)
mod_timer(...)
This would reduce the update frequency to one per jiffy, which should be acceptable.
Quoted 3 lines
Maybe we should get rid of the timer object and introduce a periodic (1s) worker which checks some
last_recvtimestamp on every known peer? What do you think?
That should work, or the workqueue like Eyal is saying.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Quoted 31 lines · Sabrina Dubroca
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/peer.h +11
-0+/** +* ovpn_peer_keepalive_recv_reset - reset keepalive timeout +* @peer: peer for which the timeout should be reset +* +* To be invoked upon reception of an authenticated packet from peer in order +* to report valid activity and thus reset the keepalive timeout +*/ +static inline void ovpn_peer_keepalive_recv_reset (struct ovpn_peer *peer) +{ +u32 delta =msecs_to_jiffies (peer->keepalive_timeout *MSEC_PER_SEC ); +Quoted 9 lines
drivers/net/ovpn/peer.h struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 169
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+if (unlikely (!delta)) +return ; + +mod_timer (&peer->keepalive_recv, jiffies + delta);This (and
ovpn_peer_keepalive_xmit_reset) is going to be called for each packet. I wonder how well the timer subsystem deals with one timer getting updated possibly thousands of time per second.Quoted 1 line
May it even introduce some performance penalty?
That's what I was worried about, yes.
I asked Paolo, he suggested checking that we're actually doing any change to the timer:
if (new_timeout_time != old_timeout_time) mod_timer(...)
Quoted 8 lines
This would reduce the update frequency to one per jiffy, which should be acceptable.
Quoted 3 lines
Maybe we should get rid of the timer object and introduce a periodic (1s) worker which checks some
last_recvtimestamp on every known peer? What do you think?That should work, or the workqueue like Eyal is saying.
I will go with Eyal's approach.
This way we also eliminate any timer related operation from the fast path.
Cheers!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
In reply to Antonio QuartulliQuoted 10 lines
Quoted 6 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);That should probably be
_ratelimited, but it seems we don't have_ratelimitedvariants for the netdev_* helpers.Right.
I have used the net_*_ratelimited() variants when needed. Too bad we don't have those.
If you think netdev_dbg_ratelimited() would be useful, i don't see why you cannot add it.
I just did an search and found something interesting in the history:
https://lore.kernel.org/all/[email protected]/T/#u
Maybe limit it to netdev_dbg_ratelimited() to avoid the potential abuse DaveM was worried about.
Andrew
Antonio Quartulli
maintainerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Quoted 14 lines · Andrew Lunn
Quoted 10 lines
Quoted 6 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);That should probably be
_ratelimited, but it seems we don't have_ratelimitedvariants for the netdev_* helpers.Right.
I have used the net_*_ratelimited() variants when needed. Too bad we don't have those.If you think
netdev_dbg_ratelimited()would be useful, i don't see why you cannot add it.
Quoted 6 lines
I just did an search and found something interesting in the history:
https://lore.kernel.org/all/[email protected]/T/#u
Maybe limit it to
netdev_dbg_ratelimited()to avoid the potential abuse DaveM was worried about.
I see what Dave says however...
...along the packet processing routine there are several messages (some are err or warn or info) which require ratelimiting. Otherwise you end up with a gazilion log entries in case of a long lasting issue.
Right now I am using net_dbg/warn/err/info_ratelimited(), therefore not having a netdev counterpart is not really helping with Dave's argument.
I can try to take on this mission after this patchset is in and see what Dave/Jakub think about it.
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Quoted 24 lines · Antonio Quartulli
Quoted 14 lines · Andrew Lunn
Quoted 10 lines
Quoted 6 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);That should probably be
_ratelimited, but it seems we don't have_ratelimitedvariants for the netdev_* helpers.Right.
I have used the net_*_ratelimited() variants when needed. Too bad we don't have those.If you think
netdev_dbg_ratelimited()would be useful, i don't see why you cannot add it.Quoted 6 lines
I just did an search and found something interesting in the history:
https://lore.kernel.org/all/[email protected]/T/#u
Maybe limit it to
netdev_dbg_ratelimited()to avoid the potential abuse DaveM was worried about.I see what Dave says however...
Quoted 7 lines
...along the packet processing routine there are several messages (some are err or warn or info) which require ratelimiting.
Otherwise you end up with a gazilion log entries in case of a long lasting issue.Right now I am using net_dbg/warn/err/info_ratelimited(), therefore not having a netdev counterpart is not really helping with Dave's argument.
Yes, i think Dave' argument is weak because these alternatives exist. Maybe they did not at the time?
I suspect he was using it as a way to force fixing the real issue. A driver should not be issues lots of err or info messages. Protocol errors are part of normal behaviour, just increment a counter and keep going. Peer devices disappearing is normal behaviour, count it and keep going. _err is generally reserved for something fatal happened, and there is no recovery, other than unload the kernel module and reload it.
Andrew
Antonio Quartulli
maintainerRe: [PATCH net-next v5 17/25] ovpn: implement keepalive mechanism
Quoted 30 lines · Andrew Lunn
Quoted 29 lines · Antonio Quartulli
Quoted 14 lines · Andrew Lunn
Quoted 10 lines
Quoted 6 lines
drivers/net/ovpn/io.c void ovpn_decrypt_post(struct sk_buff *skb, int ret) · line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id);That should probably be
_ratelimited, but it seems we don't have_ratelimitedvariants for the netdev_* helpers.Right.
I have used the net_*_ratelimited() variants when needed. Too bad we don't have those.If you think
netdev_dbg_ratelimited()would be useful, i don't see why you cannot add it.Quoted 6 lines
I just did an search and found something interesting in the history:
https://lore.kernel.org/all/[email protected]/T/#u
Maybe limit it to
netdev_dbg_ratelimited()to avoid the potential abuse DaveM was worried about.I see what Dave says however...
...along the packet processing routine there are several messages (some are err or warn or info) which require ratelimiting.
Otherwise you end up with a gazilion log entries in case of a long lasting issue.
Quoted 5 lines
Quoted 2 lines
Right now I am using net_dbg/warn/err/info_ratelimited(), therefore not having a netdev counterpart is not really helping with Dave's argument.
Yes, i think Dave' argument is weak because these alternatives exist. Maybe they did not at the time?
They did and it's exactly what Hangbin was introducing:
https://lore.kernel.org/all/[email protected]/
before being suggested by Joe to implement the new macros:
https://lore.kernel.org/all/[email protected]/
Quoted 8 lines
I suspect he was using it as a way to force fixing the real issue. A driver should not be issues lots of err or info messages. Protocol errors are part of normal behaviour, just increment a counter and keep going. Peer devices disappearing is normal behaviour, count it and keep going.
_erris generally reserved for something fatal happened, and there is no recovery, other than unload the kernel module and reload it.
Yeah, it looks like it and David pushed them to rethink the error handling rather than making the error less painful.
But I can still netdev_dbg_ratelimited useful for debugging/development. Anyway, I will add this to my todo and see if this specific variant can be accepted.
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 18/25 ovpn: add support for updating local UDP endpoint
In reply to Antonio QuartulliIn 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 | 5 +++++ drivers/net/ovpn/peer.c | 42 +++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 3 +++ 3 files changed, 50 insertions(+)
drivers/net/ovpn/io.c +5 -0
@@ -119,6 +119,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* note event of authenticated packet received for keepalive */ ovpn_peer_keepalive_recv_reset (peer); +if (peer->sock->sock->sk->sk_protocol ==IPPROTO_UDP ) { +/* update source endpoint for this peer */ +ovpn_peer_update_local_endpoint (peer, skb); + } +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->payload_offset);
drivers/net/ovpn/peer.c +42 -0
48 lines not on this page · raw message
drivers/net/ovpn/peer.h +3 -0
@@ -189,4 +189,7 @@ static inline void ovpn_peer_keepalive_xmit_reset(struct ovpn_peer *peer)void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout); +void ovpn_peer_update_local_endpoint (struct ovpn_peer *peer, +struct sk_buff *skb); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v5 19/25 ovpn: add support for peer floating
In reply to Antonio QuartulliA 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/io.c | 4 ++ drivers/net/ovpn/peer.c | 114 ++++++++++++++++++++++++++++++++++++++-- drivers/net/ovpn/peer.h | 2 + 3 files changed, 117 insertions(+), 3 deletions(-)
drivers/net/ovpn/io.c +4 -0
@@ -120,6 +120,10 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)ovpn_peer_keepalive_recv_reset (peer);if (peer->sock->sock->sk->sk_protocol ==IPPROTO_UDP ) { +/* check if this peer changed it's IP address and update +* state +*/ +ovpn_peer_float (peer, skb);/* update source endpoint for this peer */ ovpn_peer_update_local_endpoint (peer, skb); }
drivers/net/ovpn/peer.c +111 -3
126 lines not on this page · raw message
drivers/net/ovpn/peer.h +2 -0
@@ -192,4 +192,6 @@ void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout);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); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 19/25] ovpn: add support for peer floating
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +4
-0+void ovpn_peer_float (struct ovpn_peer *peer,struct sk_buff *skb) +{ +struct sockaddr_storage ss; +const u8 *local_ip =NULL ;
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 191
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+struct sockaddr_in6 *sa6; +struct sockaddr_in *sa; +struct ovpn_bind *bind; +sa_family_t family; +size_t salen; + +rcu_read_lock (); + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) + goto unlock;
Why are you aborting here? ovpn_bind_skb_src_match considers bind==NULL to be "no match" (reasonable), then we would create a new bind for the current address.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + if (likely(ovpn_bind_skb_src_match(bind, skb)))
This could be running in parallel on two CPUs, because ->encap_rcv isn't protected against that. So the bind could be getting updated in parallel. I would move spin_lock_bh above this check to make sure it doesn't happen.
ovpn_peer_update_local_endpoint would also need something like that, I think.
Quoted 30 lines
drivers/net/ovpn/peer.c +30
-0+ 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);
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 233
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+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 */ +hlist_add_head_rcu (&peer->hash_entry_transp_addr, +ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, + &ss, salen));
That could send a concurrent reader onto the wrong hash bucket, if it's going through peer's old bucket, finds peer before the update, then continues reading after peer is moved to the new bucket.
This kind of re-hash can be handled with nulls, and re-trying the lookup if we ended up on the wrong chain. See for example __inet_lookup_established in net/ipv4/inet_hashtables.c (Thanks to Paolo for the pointer).
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+spin_unlock_bh (&peer->ovpn->peers->lock); + +unlock: +rcu_read_unlock (); +}
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 19/25] ovpn: add support for peer floating
Quoted 11 lines · Sabrina Dubroca
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +10
-0+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; +
Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 191
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+rcu_read_lock (); + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) + goto unlock;Why are you aborting here?
ovpn_bind_skb_src_matchconsiders bind==NULL to be "no match" (reasonable), then we would create a new bind for the current address.
(NOTE: float and the following explanation assume connection via UDP)
peer->bind is assigned right after peer creation in ovpn_nl_set_peer_doit().
ovpn_peer_float() is called while the peer is exchanging traffic.
If we got to this point and bind is NULL, then the peer was being released, because there is no way we are going to NULLify bind during the peer life cycle, except upon ovpn_peer_release().
Does it make sense?
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ + if (likely(ovpn_bind_skb_src_match(bind, skb)))This could be running in parallel on two CPUs, because ->encap_rcv isn't protected against that. So the bind could be getting updated in parallel. I would move
spin_lock_bhabove this check to make sure it doesn't happen.
hm, I should actually use peer->lock for this, which is currently only used in ovpn_bind_reset() to avoid multiple concurrent assignments...but you're right we should include the call to skb_src_check() as well.
Quoted 3 lines
ovpn_peer_update_local_endpointwould also need something like that, I think.
at least the test-and-set part should be protected, if we can truly invoke ovpn_peer_update_local_endpoint() multiple times concurrently.
How do I test running encap_rcv in parallel?
This is actually an interesting case that I thought to not be possible (no specific reason for this..).
Quoted 37 lines
Quoted 37 lines
drivers/net/ovpn/peer.c +36
-0+ 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);
Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 233
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+/* re-add with new transport address */ +hlist_add_head_rcu (&peer->hash_entry_transp_addr, +ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, + &ss, salen));That could send a concurrent reader onto the wrong hash bucket, if it's going through peer's old bucket, finds peer before the update, then continues reading after peer is moved to the new bucket.
I haven't fully grasped this scenario.
I am imagining we are running ovpn_peer_get_by_transp_addr() in parallel: reader gets the old bucket and finds peer, because ovpn_peer_transp_match() will still return true (update wasn't performed yet), and will return it.
At this point, what do you mean with "continues reading after peer is moved to the new bucket"?
Quoted 12 lines
This kind of re-hash can be handled with nulls, and re-trying the lookup if we ended up on the wrong chain. See for example
__inet_lookup_establishedinnet/ipv4/inet_hashtables.c(Thanks to Paolo for the pointer).Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+spin_unlock_bh (&peer->ovpn->peers->lock); + +unlock: +rcu_read_unlock (); +}
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 19/25] ovpn: add support for peer floating
Quoted 25 lines · Antonio Quartulli
Quoted 11 lines · Sabrina Dubroca
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +10
-0+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; +Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 191
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+rcu_read_lock (); + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) + goto unlock;Why are you aborting here?
ovpn_bind_skb_src_matchconsiders bind==NULL to be "no match" (reasonable), then we would create a new bind for the current address.(NOTE: float and the following explanation assume connection via UDP)
peer->bindis assigned right after peer creation inovpn_nl_set_peer_doit().
Quoted 7 lines
ovpn_peer_float()is called while the peer is exchanging traffic.If we got to this point and bind is NULL, then the peer was being released, because there is no way we are going to NULLify bind during the peer life cycle, except upon
ovpn_peer_release().Does it make sense?
Alright, thanks, I missed that.
Quoted 10 lines
Quoted 6 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 193
Quoted 1 line
drivers/net/ovpn/peer.c +1
-0+if (likely (ovpn_bind_skb_src_match (bind, skb)))This could be running in parallel on two CPUs, because ->encap_rcv isn't protected against that. So the bind could be getting updated in parallel. I would move
spin_lock_bhabove this check to make sure it doesn't happen.hm, I should actually use
peer->lockfor this, which is currently only used inovpn_bind_reset()to avoid multiple concurrent assignments...but you're right we should include the call toskb_src_check()as well.
Ok, sounds good.
Quoted 5 lines
Quoted 2 lines
ovpn_peer_update_local_endpointwould also need something like that, I think.at least the test-and-set part should be protected, if we can truly invoke
ovpn_peer_update_local_endpoint()multiple times concurrently.
Yes.
Quoted 3 lines
How do I test running
encap_rcvin parallel?
This is actually an interesting case that I thought to not be possible (no specific reason for this..).
It should happen when the packets come from different source IPs and the NIC has multiple queues, then they can be spread over different CPUs. But it's probably not going to be easy to land multiple packets in ovpn_peer_float at the same time to trigger this issue.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/net/ovpn/peer.c +8
-0+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);Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 233
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+/* re-add with new transport address */ +hlist_add_head_rcu (&peer->hash_entry_transp_addr, +ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, + &ss, salen));That could send a concurrent reader onto the wrong hash bucket, if it's going through peer's old bucket, finds peer before the update, then continues reading after peer is moved to the new bucket.
Quoted 4 lines
I haven't fully grasped this scenario.
I am imagining we are runningovpn_peer_get_by_transp_addr()in parallel:
reader gets the old bucket and finds peer, becauseovpn_peer_transp_match()will still return true (update wasn't performed yet), and will return it.
The other reader isn't necessarily looking for peer, but maybe another item that landed in the same bucket (though your hashtables are so large, it would be a bit unlucky).
Quoted 2 lines
At this point, what do you mean with "continues reading after peer is moved to the new bucket"?
Continues iterating, in hlist_for_each_entry_rcu inside ovpn_peer_get_by_transp_addr.
ovpn_peer_float ovpn_peer_get_by_transp_addr
start lookup
head = ovpn_get_hash_head(...)
hlist_for_each_entry_rcu
...
find peer on headpeer moved from head to head2
continue hlist_for_each_entry_rcu with peer->next
but peer->next is now on head2
keep walking ->next on head2 instead of headSignature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 19/25] ovpn: add support for peer floating
Quoted 8 lines · Sabrina Dubroca
Quoted 3 lines
How do I test running
encap_rcvin parallel?
This is actually an interesting case that I thought to not be possible (no specific reason for this..).It should happen when the packets come from different source IPs and the NIC has multiple queues, then they can be spread over different CPUs. But it's probably not going to be easy to land multiple packets in
ovpn_peer_floatat the same time to trigger this issue.
I see. Yeah, this is not easy.
Quoted 42 lines
Quoted 19 lines
Quoted 18 lines
Quoted 4 lines
drivers/net/ovpn/peer.c +2
-0+netdev_dbg (peer->ovpn->dev,"%s: peer %d floated to %pIScp" , __func__, + peer->id, &ss);drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 233
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+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 */ +hlist_add_head_rcu (&peer->hash_entry_transp_addr, +ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, + &ss, salen));That could send a concurrent reader onto the wrong hash bucket, if it's going through peer's old bucket, finds peer before the update, then continues reading after peer is moved to the new bucket.
Quoted 4 lines
I haven't fully grasped this scenario.
I am imagining we are runningovpn_peer_get_by_transp_addr()in parallel:
reader gets the old bucket and finds peer, becauseovpn_peer_transp_match()will still return true (update wasn't performed yet), and will return it.The other reader isn't necessarily looking for peer, but maybe another item that landed in the same bucket (though your hashtables are so large, it would be a bit unlucky).
Quoted 2 lines
At this point, what do you mean with "continues reading after peer is moved to the new bucket"?
Continues iterating, in
hlist_for_each_entry_rcuinsideovpn_peer_get_by_transp_addr.
ovpn_peer_floatovpn_peer_get_by_transp_addrstart lookup head = ovpn_get_hash_head(...) hlist_for_each_entry_rcu ... find peer on head
Quoted 6 lines
peer moved from head to head2
continue hlist_for_each_entry_rcu with peer->next but peer->next is now on head2 keep walking ->next on head2 instead of head
Ok got it.
Basically we might move the reader from a list to another without it noticing.
Will have a look at the pointer provided by Paolo and modify this code accordingly.
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 20/25 ovpn: implement peer add/dump/delete via netlink
In reply to Antonio QuartulliThis 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 | 487 ++++++++++++++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 8 +- drivers/net/ovpn/peer.h | 4 + 3 files changed, 490 insertions(+), 9 deletions(-)
drivers/net/ovpn/netlink.c +482 -5
@@ -8,6 +8,7 @@#include <linux/netdevice.h> #include <linux/rtnetlink.h> +#include <linux/types.h> #include <net/genetlink.h> #include <uapi/linux/ovpn.h>
@@ -17,6 +18,10 @@#include "io.h" #include "netlink.h" #include "netlink-gen.h" +#include "bind.h" +#include "packet.h" +#include "peer.h" +#include "socket.h" MODULE_ALIAS_GENL_FAMILY (OVPN_FAMILY_NAME );
@@ -29,7 +34,7 @@ MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);* Return: the netdevice, if found, or an error otherwise */ static struct net_device * -ovpn_get_dev_from_attrs (struct net *net,struct genl_info *info) +ovpn_get_dev_from_attrs (struct net *net,const struct genl_info *info) {struct net_device *dev;int ifindex;
@@ -141,24 +146,496 @@ int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +static u8 *ovpn_nl_attr_local_ip (struct genl_info *info, +struct ovpn_struct *ovpn, +struct nlattr **attrs,int sock_fam) +{ +size_t ip_len =nla_len (attrs[OVPN_A_PEER_LOCAL_IP ]); +u8 *local_ip =nla_data (attrs[OVPN_A_PEER_LOCAL_IP ]); +bool is_mapped; + +if (ip_len ==sizeof (struct in_addr )) { +if (sock_fam !=AF_INET ) { +NL_SET_ERR_MSG_MOD (info->extack, +"mismatching AF between local IP (v4) and peer" ); +return ERR_PTR (-EINVAL ); + } + }else if (ip_len ==sizeof (struct in6_addr )) { + is_mapped =ipv6_addr_v4mapped ((struct in6_addr *)local_ip); + +if (sock_fam !=AF_INET6 && !is_mapped) { +NL_SET_ERR_MSG_MOD (info->extack, +"mismatching AF between local IP (v6) and peer" ); +return ERR_PTR (-EINVAL ); + } + +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 { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"invalid local IP length: %zu" , ip_len); +return ERR_PTR (-EINVAL ); + } + +return local_ip; +} +int ovpn_nl_set_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +bool keepalive_set = false, new_peer = false; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +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; +u8 *local_ip =NULL ; +size_t sa_len; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); +/* check if the peer exists first, otherwise create a new one */ + peer =ovpn_peer_get_by_id (ovpn, id); +if (!peer) { + peer =ovpn_peer_new (ovpn, id); + new_peer = true; +if (IS_ERR (peer)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot create new peer object for peer %u (sockaddr=%pIScp): %ld" , + id, ss,PTR_ERR (peer)); +return PTR_ERR (peer); + } + } + +if (new_peer &&NL_REQ_ATTR_CHECK (info->extack, + info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_SOCKET )) { + 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 ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"a VPN IP is required when adding a peer in MP mode" ); + 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) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot lookup peer socket (fd=%u): %d" , + sockfd, ret); + ret = -ENOTSOCK ; + goto peer_release; + } + +if (peer->sock) +ovpn_socket_put (peer->sock); + + peer->sock =ovpn_socket_new (sock, peer); +if (IS_ERR (peer->sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (peer->sock)); +sockfd_put (sock); + peer->sock =NULL ; + 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 (peer->sock->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 ; + +NL_SET_ERR_MSG_MOD (info->extack, +"remote sockaddr_in has invalid family" ); + ret = -EINVAL ; + goto peer_release; +case sizeof (struct sockaddr_in6 ): +if (ss->ss_family ==AF_INET6 ) +/* valid sockaddr */ +break ; + +NL_SET_ERR_MSG_MOD (info->extack, +"remote sockaddr_in6 has invalid family" ); + ret = -EINVAL ; + goto peer_release; +default : +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"invalid size for sockaddr: %zd" , + sa_len); + ret = -EINVAL ; + goto peer_release; + } + +/* if this is a v6-mapped-v4, convert the sockaddr +* object from AF_INET6 to AF_INET before continue +* processing +*/ +if (ss->ss_family ==AF_INET6 ) { + in6 = (struct sockaddr_in6 *)ss; + +if (ipv6_addr_v4mapped (&in6->sin6_addr)) { + 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; + } + } + +if (attrs[OVPN_A_PEER_LOCAL_IP ]) { + local_ip =ovpn_nl_attr_local_ip (info, ovpn, + attrs, + ss->ss_family); +if (IS_ERR (local_ip)) { + ret =PTR_ERR (local_ip); +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot retrieve local IP: %d" , + ret); + goto peer_release; + } + } + +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, ss, local_ip); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); + goto peer_release; + } + } + +/* VPN IPs cannot be updated, because they are hashed */ +if (new_peer && attrs[OVPN_A_PEER_VPN_IPV4 ]) + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + +/* VPN IPs cannot be updated, because they are hashed */ +if (new_peer && attrs[OVPN_A_PEER_VPN_IPV6 ]) + peer->vpn_addrs.ipv6 = +nla_get_in6_addr (attrs[OVPN_A_PEER_VPN_IPV6 ]); + +/* when setting the keepalive, both parameters have to be configured */ +if (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ]) { + 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: %s peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + __func__, (new_peer ?"adding" :"modifying" ), ss, + peer->sock->sock->sk->sk_prot_creator->name, peer->id, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +if (new_peer) { + ret =ovpn_peer_add (ovpn, peer); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot add new peer (id=%u) to hashtable: %d\n" , + peer->id, ret); + goto peer_release; + } + }else { +ovpn_peer_put (peer); + } + +return 0 ; + +peer_release: +if (new_peer) { +/* release right away because peer is not really used in any +* context +*/ +ovpn_peer_release (peer); +kfree (peer); + }else { +ovpn_peer_put (peer); + } + +return ret; +} + +static int ovpn_nl_send_peer (struct sk_buff *skb,const struct genl_info *info, +const struct ovpn_peer *peer,u32 portid,u32 seq, +int flags) +{ +const struct ovpn_bind *bind; +struct nlattr *attr; +void *hdr; + + hdr =genlmsg_put (skb, portid, seq, &ovpn_nl_family, flags, +OVPN_CMD_SET_PEER ); +if (!hdr) +return -ENOBUFS ; + + attr =nla_nest_start (skb,OVPN_A_PEER ); +if (!attr) + goto err; + +if (nla_put_u32 (skb,OVPN_A_PEER_ID , peer->id)) + goto err; + +if (peer->vpn_addrs.ipv4.s_addr !=htonl (INADDR_ANY )) +if (nla_put_in_addr (skb,OVPN_A_PEER_VPN_IPV4 , + peer->vpn_addrs.ipv4.s_addr)) + goto err; + +if (!ipv6_addr_equal (&peer->vpn_addrs.ipv6, &in6addr_any)) +if (nla_put_in6_addr (skb,OVPN_A_PEER_VPN_IPV6 , + &peer->vpn_addrs.ipv6)) + goto err; + +if (nla_put_u32 (skb,OVPN_A_PEER_KEEPALIVE_INTERVAL , + peer->keepalive_interval) || +nla_put_u32 (skb,OVPN_A_PEER_KEEPALIVE_TIMEOUT , + peer->keepalive_timeout)) + goto err; + +rcu_read_lock (); + bind =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_uint (skb,OVPN_A_PEER_VPN_RX_BYTES , +atomic64_read (&peer->vpn_stats.rx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_VPN_RX_PACKETS , +atomic64_read (&peer->vpn_stats.rx.packets)) || +/* VPN TX stats */ +nla_put_uint (skb,OVPN_A_PEER_VPN_TX_BYTES , +atomic64_read (&peer->vpn_stats.tx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_VPN_TX_PACKETS , +atomic64_read (&peer->vpn_stats.tx.packets)) || +/* link RX stats */ +nla_put_uint (skb,OVPN_A_PEER_LINK_RX_BYTES , +atomic64_read (&peer->link_stats.rx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_LINK_RX_PACKETS , +atomic64_read (&peer->link_stats.rx.packets)) || +/* link TX stats */ +nla_put_uint (skb,OVPN_A_PEER_LINK_TX_BYTES , +atomic64_read (&peer->link_stats.tx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_LINK_TX_PACKETS , +atomic64_read (&peer->link_stats.tx.packets))) + goto err; + +nla_nest_end (skb, attr); +genlmsg_end (skb, hdr); + +return 0 ; +err_unlock: +rcu_read_unlock (); +err: +genlmsg_cancel (skb, hdr); +return -EMSGSIZE ; }int ovpn_nl_get_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; +struct ovpn_peer *peer; +struct sk_buff *msg; +u32 peer_id; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot find peer with id %u" , peer_id); +return -ENOENT ; + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + ret =ovpn_nl_send_peer (msg, info, 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; }int ovpn_nl_get_peer_dumpit (struct sk_buff *skb,struct netlink_callback *cb) { -return -EOPNOTSUPP ; +const struct genl_info *info =genl_info_dump (cb); +struct ovpn_struct *ovpn; +struct ovpn_peer *peer; +struct net_device *dev; +int bkt, last_idx = cb->args[1 ], dumped =0 ; + + dev =ovpn_get_dev_from_attrs (sock_net (cb->skb->sk), info); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ovpn =netdev_priv (dev); + +if (ovpn->mode ==OVPN_MODE_P2P ) { +/* if we already dumped a peer it means we are done */ +if (last_idx) + goto out; + +rcu_read_lock (); + peer =rcu_dereference (ovpn->peer); +if (peer) { +if (ovpn_nl_send_peer (skb, info, peer, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, +NLM_F_MULTI ) ==0 ) + dumped++; + } +rcu_read_unlock (); + }else { +rcu_read_lock (); +hash_for_each_rcu (ovpn->peers->by_id, bkt, peer, + hash_entry_id) { +/* skip already dumped peers that were dumped by +* previous invocations +*/ +if (last_idx >0 ) { + last_idx--; +continue ; + } + +if (ovpn_nl_send_peer (skb, info, peer, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, +NLM_F_MULTI ) <0 ) +break ; + +/* count peers being dumped during this invocation */ + dumped++; + } +rcu_read_unlock (); + } + +out: +netdev_put (dev,NULL ); + +/* sum up peers dumped in this message, so that at the next invocation +* we can continue from where we left +*/ + cb->args[1 ] += dumped; +return skb->len; }int ovpn_nl_del_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; +struct ovpn_peer *peer; +u32 peer_id; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + + peer =ovpn_peer_get_by_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; }int ovpn_nl_set_key_doit (struct sk_buff *skb,struct genl_info *info)
drivers/net/ovpn/peer.c +4 -4
@@ -134,9 +134,9 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)* * Return: 0 on success or a negative error code otherwise */ -static int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, -const struct sockaddr_storage *ss, -const u8 *local_ip) +int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, +const struct sockaddr_storage *ss, +const u8 *local_ip) {struct ovpn_bind *bind;size_t ip_len;
@@ -251,7 +251,7 @@ static void ovpn_peer_timer_delete_all(struct ovpn_peer *peer)* ovpn_peer_release - release peer private members * @peer: the peer to release */ -static void ovpn_peer_release (struct ovpn_peer *peer) +void ovpn_peer_release (struct ovpn_peer *peer) {if (peer->sock)ovpn_socket_put (peer->sock);
drivers/net/ovpn/peer.h +4 -0
@@ -127,6 +127,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *peer)return kref_get_unless_zero (&peer->refcount); } +void ovpn_peer_release (struct ovpn_peer *peer);void ovpn_peer_release_kref (struct kref *kref);/**
@@ -193,5 +194,8 @@ 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);#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 20/25] ovpn: implement peer add/dump/delete via netlink
drivers/net/ovpn/netlink.c MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME); · line 37
Quoted 6 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c +1
-1@@ -29,7 +34,7 @@ MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);* Return: the netdevice, if found, or an error otherwise */ static struct net_device * -ovpn_get_dev_from_attrs (struct net *net,struct genl_info *info) +ovpn_get_dev_from_attrs (struct net *net,const struct genl_info *info)
nit: this should be squashed into "add basic netlink support"
[...]
Quoted 28 lines
drivers/net/ovpn/netlink.c +25
-1int ovpn_nl_set_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +bool keepalive_set = false, new_peer = false; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +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; +u8 *local_ip =NULL ; +size_t sa_len; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; +
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 224
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); +/* check if the peer exists first, otherwise create a new one */ + peer =ovpn_peer_get_by_id (ovpn, id); +if (!peer) { + peer =ovpn_peer_new (ovpn, id); + new_peer = true; +if (IS_ERR (peer)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot create new peer object for peer %u (sockaddr=%pIScp): %ld" , + id, ss,PTR_ERR (peer));
ss hasn't been set yet at this point, including it in the extack message is not useful.
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 231
Quoted 7 lines
drivers/net/ovpn/netlink.c +7
-0+return PTR_ERR (peer); + } + } + +if (new_peer &&NL_REQ_ATTR_CHECK (info->extack, + info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_SOCKET )) {
This can be checked at the start of the previous block (!peer), we'd avoid a pointless peer allocation.
(and the linebreaks in NL_REQ_ATTR_CHECK end up being slightly better because you don't need the "new_peer &&" test that is wider than the tab used to indent the !peer block :))
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 237
Quoted 6 lines
drivers/net/ovpn/netlink.c +6
-0+ 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 ]) {
Same for this check.
Quoted 21 lines
drivers/net/ovpn/netlink.c +21
-0+NL_SET_ERR_MSG_MOD (info->extack, +"a VPN IP is required when adding a peer in MP mode" ); + 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) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot lookup peer socket (fd=%u): %d" , + sockfd, ret); + ret = -ENOTSOCK ; + goto peer_release; + } +
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 268
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+if (peer->sock) +ovpn_socket_put (peer->sock); + + peer->sock =ovpn_socket_new (sock, peer); +if (IS_ERR (peer->sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (peer->sock)); +sockfd_put (sock); + peer->sock =NULL ;
Is there any value for the client in keeping the old peer->sock assigned if we fail here?
ie something like:
tmp = ovpn_socket_new(sock, peer);
if (IS_ERR(tmp)) {
...
goto peer_release;
}
if (peer->sock)
ovpn_socket_put(peer->sock);
peer->sock = tmp;But if it's just going to get rid of the old socket and the whole association/peer on failure, probably not.
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 280
Quoted 12 lines
drivers/net/ovpn/netlink.c +12
-0+ 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.
(that should also work with UDP "connected" sockets)
Quoted 3 lines
drivers/net/ovpn/netlink.c +3
-0+*/ +if (peer->sock->sock->sk->sk_protocol ==IPPROTO_UDP && + attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]) {
[...]
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 337
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ +if (attrs[OVPN_A_PEER_LOCAL_IP ]) { + local_ip =ovpn_nl_attr_local_ip (info, ovpn, + attrs, + ss->ss_family); +if (IS_ERR (local_ip)) { + ret =PTR_ERR (local_ip); +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot retrieve local IP: %d" , + ret);
ovpn_nl_attr_local_ip already sets a more specific extack message, this is unnecessary.
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 350
Quoted 13 lines
drivers/net/ovpn/netlink.c +13
-0+ goto peer_release; + } + } + +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, ss, local_ip); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); + goto peer_release; + } + }
I would reject OVPN_A_PEER_SOCKADDR_REMOTE for a non-UDP socket.
Review
Quoted 1 line
(quoted diff) +1
-0+ /* VPN IPs cannot be updated, because they are hashed */
Then I think there should be something like
if (!new_peer && (attrs[OVPN_A_PEER_VPN_IPV4] || attrs[OVPN_A_PEER_VPN_IPV6])) {
NL_SET_ERR_MSG_FMT_MOD(... "can't update ip");
ret = -EINVAL;
goto peer_release;
}(just after getting the peer, before any changes have actually been made)
And if they are only used in MP mode, I would maybe also reject requests where mode==P2P and OVPN_A_PEER_VPN_IPV* is provided.
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 362
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+if (new_peer && attrs[OVPN_A_PEER_VPN_IPV4 ]) + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + +/* VPN IPs cannot be updated, because they are hashed */ +if (new_peer && attrs[OVPN_A_PEER_VPN_IPV6 ]) + peer->vpn_addrs.ipv6 = +nla_get_in6_addr (attrs[OVPN_A_PEER_VPN_IPV6 ]); + +/* when setting the keepalive, both parameters have to be configured */
Then I would also reject a config where only one is set (also before any changes have been made).
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 371
Quoted 9 lines
drivers/net/ovpn/netlink.c +9
-0+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);
Why not skip the bool and just do this in the previous block?
Quoted 17 lines
drivers/net/ovpn/netlink.c +17
-0+netdev_dbg (ovpn->dev, +"%s: %s peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + __func__, (new_peer ?"adding" :"modifying" ), ss, + peer->sock->sock->sk->sk_prot_creator->name, peer->id, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +if (new_peer) { + ret =ovpn_peer_add (ovpn, peer); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot add new peer (id=%u) to hashtable: %d\n" , + peer->id, ret); + goto peer_release; + } + }else { +ovpn_peer_put (peer); + }
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 399
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ +return 0 ; + +peer_release: +if (new_peer) { +/* release right away because peer is not really used in any +* context +*/ +ovpn_peer_release (peer); +kfree (peer);
I don't think that's correct, the new peer was created with ovpn_peer_new, so it took a reference on the netdevice (netdev_hold(ovpn->dev, ...)), which isn't released by ovpn_peer_release. Why not just go through ovpn_peer_put?
Quoted 7 lines
drivers/net/ovpn/netlink.c +7
-0+ }else { +ovpn_peer_put (peer); + } + +return ret; +} +
[...]
Quoted 2 lines
int
ovpn_nl_get_peer_doit(struct sk_buff *skb, struct genl_info *info){
[...]
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 529
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot find peer with id %u" , peer_id); +return -ENOENT ; + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg)
Missing ovpn_peer_put?
Quoted 14 lines
drivers/net/ovpn/netlink.c +13
-0+return -ENOMEM ; + + ret =ovpn_nl_send_peer (msg, info, 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; }
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 20/25] ovpn: implement peer add/dump/delete via netlink
Quoted 9 lines · Sabrina Dubroca
drivers/net/ovpn/netlink.c MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME); · line 37
Quoted 6 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c +1
-1@@ -29,7 +34,7 @@ MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);* Return: the netdevice, if found, or an error otherwise */ static struct net_device * -ovpn_get_dev_from_attrs (struct net *net,struct genl_info *info) +ovpn_get_dev_from_attrs (struct net *net,const struct genl_info *info)nit: this should be squashed into "add basic netlink support"
Right, thanks
Quoted 36 lines
[...]
Quoted 33 lines
int ovpn_nl_set_peer_doit(struct sk_buff *skb, struct genl_info *info) {drivers/net/ovpn/netlink.c +30
-1-return -EOPNOTSUPP ; +bool keepalive_set = false, new_peer = false; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +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; +u8 *local_ip =NULL ; +size_t sa_len; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); +/* check if the peer exists first, otherwise create a new one */ + peer =ovpn_peer_get_by_id (ovpn, id); +if (!peer) { + peer =ovpn_peer_new (ovpn, id);
Quoted 8 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 224
Quoted 5 lines
drivers/net/ovpn/netlink.c +5
-0+ new_peer = true; +if (IS_ERR (peer)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot create new peer object for peer %u (sockaddr=%pIScp): %ld" , + id, ss,PTR_ERR (peer));ss hasn't been set yet at this point, including it in the extack message is not useful.
argh. you are correct. I'll remove it.
Quoted 11 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 231
Quoted 8 lines
drivers/net/ovpn/netlink.c +7
-0+return PTR_ERR (peer); + } + } + +if (new_peer &&NL_REQ_ATTR_CHECK (info->extack, + info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_SOCKET )) {This can be checked at the start of the previous block (!peer), we'd avoid a pointless peer allocation.
Right - will move it up.
Quoted 4 lines
(and the linebreaks in
NL_REQ_ATTR_CHECKend up being slightly better because you don't need the "new_peer&&" test that is wider than the tab used to indent the !peer block :))
good point! :-D
Quoted 9 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 237
Quoted 7 lines
drivers/net/ovpn/netlink.c +6
-0+ 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 ]) {Same for this check.
ACK
Quoted 48 lines
Quoted 22 lines
drivers/net/ovpn/netlink.c +21
-0+NL_SET_ERR_MSG_MOD (info->extack, +"a VPN IP is required when adding a peer in MP mode" ); + 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) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot lookup peer socket (fd=%u): %d" , + sockfd, ret); + ret = -ENOTSOCK ; + goto peer_release; + } +drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 268
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+if (peer->sock) +ovpn_socket_put (peer->sock); + + peer->sock =ovpn_socket_new (sock, peer); +if (IS_ERR (peer->sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (peer->sock)); +sockfd_put (sock); + peer->sock =NULL ;Is there any value for the client in keeping the old
peer->sockassigned if we fail here?ie something like:
tmp = ovpn_socket_new(sock, peer); if (IS_ERR(tmp)) { ... goto peer_release; } if (peer->sock) ovpn_socket_put(peer->sock); peer->sock = tmp;
Quoted 2 lines
But if it's just going to get rid of the old socket and the whole association/peer on failure, probably not.
Right. if attaching the new socket fails, we are entering some broken status which is not worth keeping around.
Quoted 15 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 280
Quoted 13 lines
drivers/net/ovpn/netlink.c +12
-0+ 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. (that should also work with UDP "connected" sockets)
True, but those are not used in openvpn. In case of UDP, userspace just creates one socket and uses it for all peers. I will add a note about 'connected UDP socket' in the comment, to clear this out.
Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c +3
-0+*/ +if (peer->sock->sock->sk->sk_protocol ==IPPROTO_UDP && + attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]) {[...]
Quoted 13 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 337
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ +if (attrs[OVPN_A_PEER_LOCAL_IP ]) { + local_ip =ovpn_nl_attr_local_ip (info, ovpn, + attrs, + ss->ss_family); +if (IS_ERR (local_ip)) { + ret =PTR_ERR (local_ip); +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot retrieve local IP: %d" , + ret);
ovpn_nl_attr_local_ipalready sets a more specific extack message, this is unnecessary.
right.
Quoted 16 lines
Quoted 4 lines
drivers/net/ovpn/netlink.c +3
-0+ goto peer_release; + } + }drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 350
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, ss, local_ip); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); + goto peer_release; + } + }I would reject
OVPN_A_PEER_SOCKADDR_REMOTEfor a non-UDP socket.
judging from the comments below, it seems you prefer to reject unneeded attributes. OTOH I took the opposite approach (just ignore those).
However, I was actually looking for some preference/indication regarding this point and I now I got one :-)
I will be strict and return -EINVAL when unneded attributes are present.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +1
-0+ /* VPN IPs cannot be updated, because they are hashed */Then I think there should be something like
Quoted 8 lines
if (!new_peer && (attrs[OVPN_A_PEER_VPN_IPV4] || attrs[OVPN_A_PEER_VPN_IPV6])) { NL_SET_ERR_MSG_FMT_MOD(... "can't update ip"); ret = -EINVAL; goto peer_release; }(just after getting the peer, before any changes have actually been made)
ACK
Quoted 3 lines
And if they are only used in MP mode, I would maybe also reject requests where mode==P2P and OVPN_A_PEER_VPN_IPV* is provided.
yup, like I commented above.
Quoted 15 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 362
Quoted 12 lines
drivers/net/ovpn/netlink.c +10
-0+if (new_peer && attrs[OVPN_A_PEER_VPN_IPV4 ]) + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + +/* VPN IPs cannot be updated, because they are hashed */ +if (new_peer && attrs[OVPN_A_PEER_VPN_IPV6 ]) + peer->vpn_addrs.ipv6 = +nla_get_in6_addr (attrs[OVPN_A_PEER_VPN_IPV6 ]); + +/* when setting the keepalive, both parameters have to be configured */ Then I would also reject a config where only one is set (also before any changes have been made).
ok
Quoted 12 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 371
Quoted 10 lines
drivers/net/ovpn/netlink.c +9
-0+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);Why not skip the bool and just do this in the previous block?
I am pretty sure there was a reason...but it may have faded away after the 95-th rebase hehe. Thanks for spotting this!
Quoted 25 lines
Quoted 25 lines
drivers/net/ovpn/netlink.c +24
-0+netdev_dbg (ovpn->dev, +"%s: %s peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + __func__, (new_peer ?"adding" :"modifying" ), ss, + peer->sock->sock->sk->sk_prot_creator->name, peer->id, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +if (new_peer) { + ret =ovpn_peer_add (ovpn, peer); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot add new peer (id=%u) to hashtable: %d\n" , + peer->id, ret); + goto peer_release; + } + }else { +ovpn_peer_put (peer); + } + +return 0 ; + +peer_release: +if (new_peer) { +/* release right away because peer is not really used in any +* context
Quoted 8 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 399
Quoted 3 lines
drivers/net/ovpn/netlink.c +3
-0+*/ +ovpn_peer_release (peer); +kfree (peer);I don't think that's correct, the new peer was created with
ovpn_peer_new, so it took a reference on the netdevice (netdev_hold(ovpn->dev, ...)), which isn't released byovpn_peer_release. Why not just go throughovpn_peer_put?
Because then we would send the notification to userspace, but it is not correct to do so, because the new() is just about to return an error.
I presume I should just move netdev_put(peer->ovpn->dev, NULL); to ovpn_peer_release(). That will take care of this case too.
Quoted 13 lines
Quoted 8 lines
drivers/net/ovpn/netlink.c +7
-0+ }else { +ovpn_peer_put (peer); + } + +return ret; +} +[...]
Quoted 2 lines
int ovpn_nl_get_peer_doit(struct sk_buff *skb, struct genl_info *info) {[...]
Quoted 12 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 529
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot find peer with id %u" , peer_id); +return -ENOENT ; + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg)Missing
ovpn_peer_put?
indeed. have to set ret and goto err;
Quoted 16 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c +4
-0+return -ENOMEM ; + + ret =ovpn_nl_send_peer (msg, info, peer, info->snd_portid, + info->snd_seq,0 );drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 543
Quoted 10 lines
drivers/net/ovpn/netlink.c +9
-0+if (ret <0 ) { +nlmsg_free (msg); + goto err; + } + + ret =genlmsg_reply (msg, info); +err: +ovpn_peer_put (peer); +return ret; }
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 20/25] ovpn: implement peer add/dump/delete via netlink
Quoted 43 lines · Antonio Quartulli
Quoted 42 lines · Sabrina Dubroca
Quoted 15 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c +15
-0+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) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot lookup peer socket (fd=%u): %d" , + sockfd, ret); + ret = -ENOTSOCK ; + goto peer_release; + } +drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 268
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+if (peer->sock) +ovpn_socket_put (peer->sock); + + peer->sock =ovpn_socket_new (sock, peer); +if (IS_ERR (peer->sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (peer->sock)); +sockfd_put (sock); + peer->sock =NULL ;Is there any value for the client in keeping the old
peer->sockassigned if we fail here?ie something like:
tmp = ovpn_socket_new(sock, peer); if (IS_ERR(tmp)) { ... goto peer_release; } if (peer->sock) ovpn_socket_put(peer->sock); peer->sock = tmp;
Quoted 5 lines
Quoted 2 lines
But if it's just going to get rid of the old socket and the whole association/peer on failure, probably not.
Right. if attaching the new socket fails, we are entering some broken status which is not worth keeping around.
Ok, then the current code is fine, thanks.
Quoted 10 lines
Quoted 9 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 280
Quoted 7 lines
drivers/net/ovpn/netlink.c +7
-0+/* 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. (that should also work with UDP "connected" sockets)
Quoted 4 lines
True, but those are not used in openvpn. In case of UDP, userspace just creates one socket and uses it for all peers.
I will add a note about 'connected UDP socket' in the comment, to clear this out.
If you want. I was being pedantic, I don't think it's really necessary to mention this.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/net/ovpn/netlink.c +7
-0+ goto peer_release; + } + } + +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, ss, local_ip); +if (ret <0 ) {Quoted 8 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 350
Quoted 6 lines
drivers/net/ovpn/netlink.c +6
-0+NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); + goto peer_release; + } + }I would reject
OVPN_A_PEER_SOCKADDR_REMOTEfor a non-UDP socket.
Quoted 2 lines
judging from the comments below, it seems you prefer to reject unneeded attributes. OTOH I took the opposite approach (just ignore those).
Yes.
Quoted 2 lines
However, I was actually looking for some preference/indication regarding this point and I now I got one :-)
I don't think there's an established rule, and a lot of the old code is very tolerant. That's my preference (in part because I think refusing bogus combinations allows to enable them in the future with a new behavior), but maybe the maintainers have a different opinion? OTOH ignoring those attributes can let a modern client run on an old kernel (possibly without some features, depending on what the attribute is).
(leaving a few other examples of stricter validation for context:)
Quoted 42 lines
I will be strict and return
-EINVALwhen unneded attributes are present.Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +1
-0+ /* VPN IPs cannot be updated, because they are hashed */Then I think there should be something like
Quoted 8 lines
if (!new_peer && (attrs[OVPN_A_PEER_VPN_IPV4] || attrs[OVPN_A_PEER_VPN_IPV6])) { NL_SET_ERR_MSG_FMT_MOD(... "can't update ip"); ret = -EINVAL; goto peer_release; }(just after getting the peer, before any changes have actually been made)
ACK
Quoted 3 lines
And if they are only used in MP mode, I would maybe also reject requests where mode==P2P and OVPN_A_PEER_VPN_IPV* is provided.
yup, like I commented above.
Quoted 15 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 362
Quoted 12 lines
drivers/net/ovpn/netlink.c +10
-0+if (new_peer && attrs[OVPN_A_PEER_VPN_IPV4 ]) + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + +/* VPN IPs cannot be updated, because they are hashed */ +if (new_peer && attrs[OVPN_A_PEER_VPN_IPV6 ]) + peer->vpn_addrs.ipv6 = +nla_get_in6_addr (attrs[OVPN_A_PEER_VPN_IPV6 ]); + +/* when setting the keepalive, both parameters have to be configured */ Then I would also reject a config where only one is set (also before any changes have been made).
ok
[...]
Quoted 12 lines
Quoted 11 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 371
Quoted 9 lines
drivers/net/ovpn/netlink.c +9
-0+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);Why not skip the bool and just do this in the previous block?
Quoted 2 lines
I am pretty sure there was a reason...but it may have faded away after the 95-th rebase hehe. Thanks for spotting this!
:)
Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/ovpn/netlink.c +24
-0+netdev_dbg (ovpn->dev, +"%s: %s peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + __func__, (new_peer ?"adding" :"modifying" ), ss, + peer->sock->sock->sk->sk_prot_creator->name, peer->id, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +if (new_peer) { + ret =ovpn_peer_add (ovpn, peer); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot add new peer (id=%u) to hashtable: %d\n" , + peer->id, ret); + goto peer_release; + } + }else { +ovpn_peer_put (peer); + } + +return 0 ; + +peer_release: +if (new_peer) { +/* release right away because peer is not really used in any +* context Quoted 8 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) · line 399
Quoted 3 lines
drivers/net/ovpn/netlink.c +3
-0+*/ +ovpn_peer_release (peer); +kfree (peer);I don't think that's correct, the new peer was created with
ovpn_peer_new, so it took a reference on the netdevice (netdev_hold(ovpn->dev, ...)), which isn't released byovpn_peer_release. Why not just go throughovpn_peer_put?
Quoted 2 lines
Because then we would send the notification to userspace, but it is not correct to do so, because the
new()is just about to return an error.
Oh, right.
Quoted 2 lines
I presume I should just move
netdev_put(peer->ovpn->dev, NULL); toovpn_peer_release(). That will take care of this case too.
Ok.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerPATCH v5 21/25 ovpn: implement key add/del/swap via netlink
In reply to Antonio QuartulliThis change introduces the netlink commands needed to add, delete and swap keys for a specific peer.
Userspace is expected to use these commands to create, destroy and rotate session keys for a specific peer.
--- Documentation/netlink/specs/ovpn.yaml | 2 +- drivers/net/ovpn/netlink-gen.c | 2 +- drivers/net/ovpn/netlink.c | 199 +++++++++++++++++++++++++- 3 files changed, 198 insertions(+), 5 deletions(-)
Documentation/netlink/specs/ovpn.yaml +1 -1
@@ -153,7 +153,7 @@ attribute-sets: decryption type: u32 checks: - max: 2 + max: 7 - name: cipher-alg type: u32
drivers/net/ovpn/netlink-gen.c +1 -1
@@ -23,7 +23,7 @@ static const struct netlink_range_validation ovpn_a_peer_local_port_range = {/* Common nested types */ const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRYPT_DIR +1 ] = { [OVPN_A_KEYCONF_SLOT ] =NLA_POLICY_MAX (NLA_U32 ,1 ), - [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,2 ), + [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,7 ), [OVPN_A_KEYCONF_CIPHER_ALG ] =NLA_POLICY_MAX (NLA_U32 ,2 ), [OVPN_A_KEYCONF_ENCRYPT_DIR ] =NLA_POLICY_NESTED (ovpn_keydir_nl_policy), [OVPN_A_KEYCONF_DECRYPT_DIR ] =NLA_POLICY_NESTED (ovpn_keydir_nl_policy),
drivers/net/ovpn/netlink.c +196 -3
215 lines not on this page · raw message
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 21/25] ovpn: implement key add/del/swap via netlink
Quoted 18 lines · Antonio Quartulli
This change introduces the netlink commands needed to add, delete and swap keys for a specific peer.
Userspace is expected to use these commands to create, destroy and rotate session keys for a specific peer.
Signed-off-by Antonio Quartulli <[email protected]>--- Documentation/netlink/specs/ovpn.yaml | 2 +- drivers/net/ovpn/netlink-gen.c | 2 +- drivers/net/ovpn/netlink.c | 199 +++++++++++++++++++++++++- 3 files changed, 198 insertions(+), 5 deletions(-)Documentation/netlink/specs/ovpn.yaml +0
-0
Documentation/netlink/specs/ovpn.yaml attribute-sets: · line 156
Quoted 5 lines
Documentation/netlink/specs/ovpn.yaml +1
-1@@ -153,7 +153,7 @@ attribute-sets: decryption type: u32 checks: - max: 2 + max: 7
Looks like this got squashed into the wrong patch.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 21/25] ovpn: implement key add/del/swap via netlink
Hi,
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines · Antonio Quartulli
This change introduces the netlink commands needed to add, delete and swap keys for a specific peer.
Userspace is expected to use these commands to create, destroy and rotate session keys for a specific peer.
Signed-off-by Antonio Quartulli <[email protected]>--- Documentation/netlink/specs/ovpn.yaml | 2 +- drivers/net/ovpn/netlink-gen.c | 2 +- drivers/net/ovpn/netlink.c | 199 +++++++++++++++++++++++++- 3 files changed, 198 insertions(+), 5 deletions(-)
Quoted 12 lines
Documentation/netlink/specs/ovpn.yaml attribute-sets: · line 156
Quoted 10 lines
Documentation/netlink/specs/ovpn.yaml +1
-1@@ -153,7 +153,7 @@ attribute-sets: decryption type: u32 checks: - max: 2 + max: 7Looks like this got squashed into the wrong patch.
Indeed. Thanks for spotting it.
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 22/25 ovpn: kill key and notify userspace in case of IV exhaustion
In reply to Antonio QuartulliIV 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/netlink.c | 39 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 8 ++++++++ 2 files changed, 47 insertions(+)
drivers/net/ovpn/netlink.c +39 -0
@@ -846,6 +846,45 @@ int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer) +{ +struct sk_buff *msg; +int ret = -EMSGSIZE ; +void *hdr; + +netdev_info (peer->ovpn->dev,"peer with id %u must rekey - primary key unusable.\n" , + peer->id); + + msg =nlmsg_new (100 ,GFP_ATOMIC ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put (msg,0 ,0 , &ovpn_nl_family,0 ,OVPN_CMD_SWAP_KEYS ); +if (!hdr) { + ret = -ENOBUFS ; + goto err_free_msg; + } + +if (nla_put_u32 (msg,OVPN_A_IFINDEX , peer->ovpn->dev->ifindex)) + goto err_cancel_msg; + +if (nla_put_u32 (msg,OVPN_A_PEER_ID , peer->id)) + goto err_cancel_msg; + +genlmsg_end (msg, hdr); + +genlmsg_multicast_netns (&ovpn_nl_family,dev_net (peer->ovpn->dev), msg, +0 ,OVPN_NLGRP_PEERS ,GFP_ATOMIC ); + +return 0 ; + +err_cancel_msg: +genlmsg_cancel (msg, hdr); +err_free_msg: +nlmsg_free (msg); +return ret; +} +/** * ovpn_nl_register - perform any needed registration in the NL subsustem *
drivers/net/ovpn/netlink.h +8 -0
@@ -12,4 +12,12 @@int ovpn_nl_register (void );void ovpn_nl_unregister (void ); +/** +* ovpn_nl_notify_swap_keys - notify userspace peer's key must be renewed +* @peer: the peer whose key needs to be renewed +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer); +#endif /* _NET_OVPN_NETLINK_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 22/25] ovpn: kill key and notify userspace in case of IV exhaustion
Quoted 15 lines · 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.
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/netlink.c | 39 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 8 ++++++++ 2 files changed, 47 insertions(+)drivers/net/ovpn/netlink.c +0
-0
drivers/net/ovpn/netlink.c int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info) · line 849
Quoted 4 lines
drivers/net/ovpn/netlink.c +1
-0@@ -846,6 +846,45 @@ int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer)
This is not getting called anywhere in this version. v3 had a change to ovpn_encrypt_one to handle the -ERANGE coming from ovpn_pktid_xmit_next.
Assuming this was getting called just as the TX key expires (like it was in v3), I'm a bit unclear on how the client can deal well with this event.
I don't see any way for userspace to know the current IV state (no notification for when the packetid gets past some threshold, and pid_xmit isn't getting dumped via netlink), so no chance for userspace to swap keys early and avoid running out of IVs. And then, since we don't have a usable primary key anymore, we will have to drop packets until userspace tells the kernel to swap the keys (or possibly install a secondary).
Am I missing something in the kernel/userspace interaction?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 22/25] ovpn: kill key and notify userspace in case of IV exhaustion
Hi,
Quoted 19 lines · Sabrina Dubroca
Quoted 18 lines · 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.
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/netlink.c | 39 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 8 ++++++++ 2 files changed, 47 insertions(+)drivers/net/ovpn/netlink.c +0
-0@@ -846,6 +846,45 @@ int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; }
Quoted 5 lines
drivers/net/ovpn/netlink.c int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info) · line 849
Quoted 1 line
drivers/net/ovpn/netlink.c +1
-0+int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer)This is not getting called anywhere in this version. v3 had a change to
ovpn_encrypt_oneto handle the-ERANGEcoming fromovpn_pktid_xmit_next.
Darn! I must have missed this. This must have been removed by accident during the n-th rebase/reshuffle. Thanks for pointing it out.
Quoted 3 lines
Assuming this was getting called just as the TX key expires (like it was in v3), I'm a bit unclear on how the client can deal well with this event.
Correct assumption.
Quoted 10 lines
I don't see any way for userspace to know the current IV state (no notification for when the packetid gets past some threshold, and
pid_xmitisn't getting dumped via netlink), so no chance for userspace to swap keys early and avoid running out of IVs. And then, since we don't have a usable primary key anymore, we will have to drop packets until userspace tells the kernel to swap the keys (or possibly install a secondary).Am I missing something in the kernel/userspace interaction?
There are two events triggering userspace to generate a new key:
1) time based
2) packet count based
1) is easy: after X seconds/minutes generate a new key and send it to the kernel. It's obviously based on guestimate and normally our default works well.
2) after X packets/bytes generate a new key. Here userspace keeps track of the amount of traffic by periodically polling GET_PEER and fetching the VPN/LINK stats.
A future improvement could be to have ovpn proactively notifying userspace after reaching a certain threshold, but for now this mechanism does not exist.
I hope it helps.
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 22/25] ovpn: kill key and notify userspace in case of IV exhaustion
Quoted 15 lines · Antonio Quartulli
Quoted 9 lines · Sabrina Dubroca
I don't see any way for userspace to know the current IV state (no notification for when the packetid gets past some threshold, and
pid_xmitisn't getting dumped via netlink), so no chance for userspace to swap keys early and avoid running out of IVs. And then, since we don't have a usable primary key anymore, we will have to drop packets until userspace tells the kernel to swap the keys (or possibly install a secondary).Am I missing something in the kernel/userspace interaction?
There are two events triggering userspace to generate a new key:
1) time based
2) packet count based
Quoted 7 lines
1) is easy: after X seconds/minutes generate a new key and send it to the kernel. It's obviously based on guestimate and normally our default works well.
2) after X packets/bytes generate a new key. Here userspace keeps track of the amount of traffic by periodically polling
GET_PEERand fetching the VPN/LINK stats.
Oh right, that's what I was missing. TX packet count should be equivalent to packetid. Thanks.
Quoted 3 lines
A future improvement could be to have ovpn proactively notifying userspace after reaching a certain threshold, but for now this mechanism does not exist.
If it's not there from the start, you won't be able to rely on it (because the userspace client may run on a kernel that does not provide the notification), so you would still have to fetch the stats, unless you have a way to poll for the threshold notification feature being present.
Quoted 1 line
I hope it helps.
Yup, thanks. Can you add this explanation to the commit message for this patch in the next version? Documenting a bit the expectations of the kernel/userspace interactions would be helpful, also for the sequencing of key installation/key swap operations. I'm guessing it goes something like this:
1. client sets up a primary key (key#1) and uses it
2. at some point, it sets up a secondary key (key#2)
3. later, keys are swapped (key#2 is now primary)
4. after some more time, the secondary (key#1) is removed and a new secondary (key#3) is installed
[steps 3 and 4 keep repeating]
And from reading patch 21, both the TX and RX key seem to be changed together (swap and delete operate on the whole keyslot, and set requires both the ENCRYPT_DIR and DECRYPT_DIR attributes).
A rough description of the overall life of a client (opening sockets and setting up the ovpn device/peers) could also be useful alongside the code.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 22/25] ovpn: kill key and notify userspace in case of IV exhaustion
Quoted 20 lines · Sabrina Dubroca
Quoted 19 lines · Antonio Quartulli
Quoted 9 lines · Sabrina Dubroca
I don't see any way for userspace to know the current IV state (no notification for when the packetid gets past some threshold, and
pid_xmitisn't getting dumped via netlink), so no chance for userspace to swap keys early and avoid running out of IVs. And then, since we don't have a usable primary key anymore, we will have to drop packets until userspace tells the kernel to swap the keys (or possibly install a secondary).Am I missing something in the kernel/userspace interaction?
There are two events triggering userspace to generate a new key:
1) time based
2) packet count based1) is easy: after X seconds/minutes generate a new key and send it to the kernel. It's obviously based on guestimate and normally our default works well.
Quoted 6 lines
Quoted 3 lines
2) after X packets/bytes generate a new key. Here userspace keeps track of the amount of traffic by periodically polling
GET_PEERand fetching the VPN/LINK stats.Oh right, that's what I was missing. TX packet count should be equivalent to packetid. Thanks.
np!
Quoted 11 lines
Quoted 5 lines
A future improvement could be to have ovpn proactively notifying userspace after reaching a certain threshold, but for now this mechanism does not exist.
If it's not there from the start, you won't be able to rely on it (because the userspace client may run on a kernel that does not provide the notification), so you would still have to fetch the stats, unless you have a way to poll for the threshold notification feature being present.
You're right. Then scratch that because userspace is not ready for this.
Quoted 9 lines
Quoted 3 lines
I hope it helps.
Yup, thanks. Can you add this explanation to the commit message for this patch in the next version? Documenting a bit the expectations of the kernel/userspace interactions would be helpful, also for the sequencing of key installation/key swap operations. I'm guessing it goes something like this:
Sure, will document how this works.
Quoted 11 lines
1. client sets up a primary key (key#1) and uses it
2. at some point, it sets up a secondary key (key#2)
3. later, keys are swapped (key#2 is now primary)
4. after some more time, the secondary (key#1) is removed and a new secondary (key#3) is installed
[steps 3 and 4 keep repeating]And from reading patch 21, both the TX and RX key seem to be changed together (swap and delete operate on the whole keyslot, and set requires both the
ENCRYPT_DIRandDECRYPT_DIRattributes).
You are correct. Encryption and decryption keys are derived from the same key material that is exchanged/generated upon each "renegotiation".
Quoted 5 lines
A rough description of the overall life of a client (opening sockets and setting up the ovpn device/peers) could also be useful alongside the code.
ok, will add it too!
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 23/25 ovpn: notify userspace when a peer is deleted
In reply to Antonio QuartulliWhenever 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 | 49 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 8 +++++++ drivers/net/ovpn/peer.c | 1 + drivers/net/ovpn/peer.h | 1 + 4 files changed, 59 insertions(+)
drivers/net/ovpn/netlink.c +49 -0
@@ -846,6 +846,55 @@ int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +int ovpn_nl_notify_del_peer (struct ovpn_peer *peer) +{ +struct sk_buff *msg; +struct nlattr *attr; +int ret = -EMSGSIZE ; +void *hdr; + +netdev_info (peer->ovpn->dev,"deleting peer with id %u, reason %d\n" , + peer->id, peer->delete_reason); + + msg =nlmsg_new (100 ,GFP_ATOMIC ); +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)) + goto err_cancel_msg; + + attr =nla_nest_start (msg,OVPN_A_PEER ); +if (!attr) + goto err_cancel_msg; + +if (nla_put_u8 (msg,OVPN_A_PEER_DEL_REASON , peer->delete_reason)) + goto err_cancel_msg; + +if (nla_put_u32 (msg,OVPN_A_PEER_ID , peer->id)) + goto err_cancel_msg; + +nla_nest_end (msg, attr); + +genlmsg_end (msg, hdr); + +genlmsg_multicast_netns (&ovpn_nl_family,dev_net (peer->ovpn->dev), msg, +0 ,OVPN_NLGRP_PEERS ,GFP_ATOMIC ); + +return 0 ; + +err_cancel_msg: +genlmsg_cancel (msg, hdr); +err_free_msg: +nlmsg_free (msg); +return ret; +} +int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer) {struct sk_buff *msg;
drivers/net/ovpn/netlink.h +8 -0
@@ -12,6 +12,14 @@int ovpn_nl_register (void );void ovpn_nl_unregister (void ); +/** +* ovpn_nl_notify_del_peer - notify userspace about peer being deleted +* @peer: the peer being deleted +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_nl_notify_del_peer (struct ovpn_peer *peer); +/** * ovpn_nl_notify_swap_keys - notify userspace peer's key must be renewed * @peer: the peer whose key needs to be renewed
drivers/net/ovpn/peer.c +1 -0
@@ -273,6 +273,7 @@ void ovpn_peer_release_kref(struct kref *kref)ovpn_peer_release (peer);netdev_put (peer->ovpn->dev,NULL ); +ovpn_nl_notify_del_peer (peer);kfree_rcu (peer, rcu); }
drivers/net/ovpn/peer.h +1 -0
@@ -129,6 +129,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *peer)void ovpn_peer_release (struct ovpn_peer *peer);void ovpn_peer_release_kref (struct kref *kref); +void ovpn_peer_release (struct ovpn_peer *peer);/** * ovpn_peer_put - decrease reference counter
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v5 23/25] ovpn: notify userspace when a peer is deleted
drivers/net/ovpn/peer.c void ovpn_peer_release_kref(struct kref *kref) · line 275
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +0
-0@@ -273,6 +273,7 @@ void ovpn_peer_release_kref(struct kref *kref)ovpn_peer_release (peer);netdev_put (peer->ovpn->dev,NULL );
I don't think you can access peer->ovpn once you release that reference, if this peer was holding the last reference the netdev (and the ovpn struct) could be gone while we're accessing it in ovpn_nl_notify_del_peer.
drivers/net/ovpn/peer.h static inline bool ovpn_peer_hold(struct ovpn_peer *peer) · line 132
Quoted 13 lines
drivers/net/ovpn/peer.c +1
-0+ovpn_nl_notify_del_peer (peer);kfree_rcu (peer, rcu); }drivers/net/ovpn/peer.h +1
-0@@ -129,6 +129,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *peer)void ovpn_peer_release (struct ovpn_peer *peer);void ovpn_peer_release_kref (struct kref *kref); +void ovpn_peer_release (struct ovpn_peer *peer);
nit: dupe of 2 lines before
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v5 23/25] ovpn: notify userspace when a peer is deleted
Hi,
Quoted 7 lines · Sabrina Dubroca
Quoted 6 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +0
-0@@ -273,6 +273,7 @@ void ovpn_peer_release_kref(struct kref *kref)
Quoted 7 lines
drivers/net/ovpn/peer.c void ovpn_peer_release_kref(struct kref *kref) · line 275
Quoted 2 lines
drivers/net/ovpn/peer.c +0
-0ovpn_peer_release (peer);netdev_put (peer->ovpn->dev,NULL );I don't think you can access
peer->ovpnonce you release that reference, if this peer was holding the last reference the netdev (and the ovpn struct) could be gone while we're accessing it inovpn_nl_notify_del_peer.
I see what you mean.
I will move notify_del_peer up above, before the release
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/peer.c +1
-0+ovpn_nl_notify_del_peer (peer);kfree_rcu (peer, rcu); }drivers/net/ovpn/peer.h +0
-0@@ -129,6 +129,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *peer)
Quoted 5 lines
drivers/net/ovpn/peer.h static inline bool ovpn_peer_hold(struct ovpn_peer *peer) · line 132
Quoted 3 lines
drivers/net/ovpn/peer.h +1
-0void ovpn_peer_release (struct ovpn_peer *peer);void ovpn_peer_release_kref (struct kref *kref); +void ovpn_peer_release (struct ovpn_peer *peer);nit: dupe of 2 lines before
ops
Thanks!
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v5 24/25 ovpn: add basic ethtool support
In reply to Antonio QuartulliImplement support for basic ethtool functionality.
Note that ovpn is a virtual device driver, therefore various ethtool APIs are just not meaningful and thus not implemented.
--- drivers/net/ovpn/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/net/ovpn/main.c +15 -0
@@ -7,6 +7,7 @@* James Yonan <[email protected]> */ +#include <linux/ethtool.h> #include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h>
@@ -132,6 +133,19 @@ bool ovpn_dev_is_valid(const struct net_device *dev)return dev->netdev_ops->ndo_start_xmit == ovpn_net_xmit; } +static void ovpn_get_drvinfo (struct net_device *dev, +struct ethtool_drvinfo *info) +{ +strscpy (info->driver,OVPN_FAMILY_NAME ,sizeof (info->driver)); +strscpy (info->bus_info,"ovpn" ,sizeof (info->bus_info)); +} + +static const struct ethtool_ops ovpn_ethtool_ops = { + .get_drvinfo = ovpn_get_drvinfo, + .get_link = ethtool_op_get_link, + .get_ts_info = ethtool_op_get_ts_info, +}; +/* we register with rtnl to let core know that ovpn is a virtual driver and * therefore ifaces should be destroyed when exiting a netns */
@@ -155,6 +169,7 @@ static void ovpn_setup(struct net_device *dev) dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; + dev->ethtool_ops = &ovpn_ethtool_ops; dev->netdev_ops = &ovpn_netdev_ops; dev->rtnl_link_ops = &ovpn_link_ops;
Signature · 1 line
2.44.2
Re: [PATCH net-next v5 24/25] ovpn: add basic ethtool support
Quoted 7 lines · Antonio Quartulli
Implement support for basic ethtool functionality.
Note that ovpn is a virtual device driver, therefore various ethtool APIs are just not meaningful and thus not implemented.
Signed-off-by Antonio Quartulli <[email protected]>
Andrew
Antonio Quartulli
maintainerPATCH v5 25/25 testing/selftest: add test tool and scripts for ovpn module
In reply to Antonio QuartulliThe ovpn-cli tool can be compiled and used as selftest for the ovpn kernel module.
It implementes the netlink API and can thus be integrated in any script for more automated testing.
Along with the tool, 2 scripts are added that perform basic functionality tests by means of network namespaces.
The scripts can be performed in sequence by running run.sh
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 17 + tools/testing/selftests/net/ovpn/config | 8 + .../selftests/net/ovpn/data-test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/data-test.sh | 130 ++ tools/testing/selftests/net/ovpn/data64.key | 5 + .../testing/selftests/net/ovpn/float-test.sh | 115 ++ tools/testing/selftests/net/ovpn/ovpn-cli.c | 1787 +++++++++++++++++ .../testing/selftests/net/ovpn/tcp_peers.txt | 1 + .../testing/selftests/net/ovpn/udp_peers.txt | 5 + 11 files changed, 2080 insertions(+) create mode 100644 tools/testing/selftests/net/ovpn/.gitignore create mode 100644 tools/testing/selftests/net/ovpn/Makefile create mode 100644 tools/testing/selftests/net/ovpn/config create mode 100755 tools/testing/selftests/net/ovpn/data-test-tcp.sh create mode 100755 tools/testing/selftests/net/ovpn/data-test.sh create mode 100644 tools/testing/selftests/net/ovpn/data64.key create mode 100755 tools/testing/selftests/net/ovpn/float-test.sh create mode 100644 tools/testing/selftests/net/ovpn/ovpn-cli.c create mode 100644 tools/testing/selftests/net/ovpn/tcp_peers.txt create mode 100644 tools/testing/selftests/net/ovpn/udp_peers.txt
tools/testing/selftests/Makefile +1 -0
@@ -64,6 +64,7 @@ TARGETS += net/forwarding TARGETS += net/hsr TARGETS += net/mptcp TARGETS += net/openvswitch +TARGETS += net/ovpn TARGETS += net/tcp_ao TARGETS += net/netfilter TARGETS += nsfs
tools/testing/selftests/net/ovpn/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0+ +ovpn-cli
tools/testing/selftests/net/ovpn/Makefile new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +CFLAGS = -Wall -I../../../../../usr/include +CFLAGS += $(shell pkg-config --cflags libnl-3.0 libnl-genl-3.0) + +LDFLAGS = -lmbedtls -lmbedcrypto +LDFLAGS += $(shell pkg-config --libs libnl-3.0 libnl-genl-3.0) + +ovpn-cli: ovpn-cli.c + +TEST_PROGS = data-test.sh \ + data-test-tcp.sh \ + float-test.sh +TEST_GEN_FILES = ovpn-cli + +include ../../lib.mk
tools/testing/selftests/net/ovpn/config new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_NET=y +CONFIG_INET=y +CONFIG_NET_UDP_TUNNEL=y +CONFIG_DST_CACHE=y +CONFIG_CRYPTO_AES=y +CONFIG_CRYPTO_GCM=y +CONFIG_CRYPTO_CHACHA20POLY1305=y +CONFIG_OVPN=y
tools/testing/selftests/net/ovpn/data-test-tcp.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +PROTO="TCP" + +source data-test.sh
tools/testing/selftests/net/ovpn/data-test.sh new file +130 -0
130 lines not on this page · raw message
tools/testing/selftests/net/ovpn/data64.key new file +5 -0
@@ -0,0 +1,5 @@ +jRqMACN7d7/aFQNT8S7jkrBD8uwrgHbG5OQZP2eu4R1Y7tfpS2bf5RHv06Vi163CGoaIiTX99R3B +ia9ycAH8Wz1+9PWv51dnBLur9jbShlgZ2QHLtUc4a/gfT7zZwULXuuxdLnvR21DDeMBaTbkgbai9 +uvAa7ne1liIgGFzbv+Bas4HDVrygxIxuAnP5Qgc3648IJkZ0QEXPF+O9f0n5+QIvGCxkAUVx+5K6 +KIs+SoeWXnAopELmoGSjUpFtJbagXK82HfdqpuUxT2Tnuef0/14SzVE/vNleBNu2ZbyrSAaah8tE +BofkPJUBFY+YQcfZNM5Dgrw3i+Bpmpq/gpdg5w==
tools/testing/selftests/net/ovpn/float-test.sh new file +115 -0
115 lines not on this page · raw message
tools/testing/selftests/net/ovpn/ovpn-cli.c new file +1787 -0
1,787 lines not on this page · raw message
tools/testing/selftests/net/ovpn/tcp_peers.txt new file +1 -0
@@ -0,0 +1 @@ +1 5.5.5.2
tools/testing/selftests/net/ovpn/udp_peers.txt new file +5 -0
@@ -0,0 +1,5 @@ +1 10.10.1.2 1 5.5.5.2 +2 10.10.2.2 1 5.5.5.3 +3 10.10.3.2 1 5.5.5.4 +4 10.10.4.2 1 5.5.5.5 +5 10.10.5.2 1 5.5.5.6
Signature · 1 line
2.44.2