Introducing OpenVPN Data Channel Offload
Subsystems (6)
Antonio Quartulli
maintainerv3 cover Introducing OpenVPN Data Channel Offload
Hi all!
I am finally back with version 3 of the ovpn patchset.
It took a while to address all comments I have received on v2, but I am happy to say that I addressed 99% of the feedback I collected.
The 1% I did not make yet is using BQL for handling the packets queue.
Although such change looks pretty simple in terms of code, I need to spend some more time understanding the concept behind and therefore I decided to postpone this change to the (near) future in order to not slow down the whole review/merge process.
Major changes from v2 are:
* added YAML documentation for the netlink uAPI
** uapi/linnu/ovpn.h, driners/net/ovpn/netlink-gen.{c,h} are now self
generated by the tools/net/ynl/ynl-regen.sh script
* the first patch now also modifies the ynl script to account for the new MAX_LEN() policy macro
* added more doxygen documentation
* added kselftest unit for ovpn in tools/testing/selftest/ovpn with some basic tests
* fixed various typ0s in documentation
* moved includes of local headers last
* wrapped code at 80 chars
* rearranged includes a bit to reduce double inclusions
* set default ifname to ovpn%d and allowed users to not specify any
* now sending reply to NEW_IFACE NL command containing actual new ifname
* used GENL_REQ_ATTR_CHECK() when possible
* turned carrier off in iface create function
* turned carrier on in open function and clearly explain why we keep it always on (new patch)
* left ethtool info ->version empty
* removed internal driver version
* checked return value of alloc_netdev
* renamed _lookup() functions to _get()
* removed memset-zero from init function as netdev is already zero'd
* added missing TCP component initialization in ovpn_init
* .. included various small fixes as requested by reviewers
The latest code can also be found at:
https://github.com/OpenVPN/linux-kernel-ovpn
Thanks to the new kunitest component, it is now pssible to run basic ovpn tests. Peers are emulated by using multiple network namespaces which are interconnected by means of veth pairs.
Please note that patches have been split for easier review, but if required, I can send a long 1/1 with all courses and dishes in one go :)
Thanks so far!
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 (24):
netlink: add NLA_POLICY_MAX_LEN macro
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: implement interface creation/destruction via netlink
ovpn: 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 | 331 ++++ MAINTAINERS | 8 + drivers/net/Kconfig | 13 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 61 + drivers/net/ovpn/bind.h | 130 ++ drivers/net/ovpn/crypto.c | 162 ++ drivers/net/ovpn/crypto.h | 138 ++ drivers/net/ovpn/crypto_aead.c | 378 +++++ drivers/net/ovpn/crypto_aead.h | 30 + drivers/net/ovpn/io.c | 566 +++++++ drivers/net/ovpn/io.h | 35 + drivers/net/ovpn/main.c | 320 ++++ drivers/net/ovpn/main.h | 56 + drivers/net/ovpn/netlink-gen.c | 206 +++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 993 ++++++++++++ drivers/net/ovpn/netlink.h | 46 + drivers/net/ovpn/ovpnstruct.h | 48 + drivers/net/ovpn/packet.h | 40 + drivers/net/ovpn/peer.c | 1077 +++++++++++++ drivers/net/ovpn/peer.h | 303 ++++ drivers/net/ovpn/pktid.c | 132 ++ drivers/net/ovpn/pktid.h | 85 + drivers/net/ovpn/proto.h | 115 ++ drivers/net/ovpn/skb.h | 51 + drivers/net/ovpn/socket.c | 150 ++ drivers/net/ovpn/socket.h | 81 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 52 + drivers/net/ovpn/tcp.c | 511 ++++++ drivers/net/ovpn/tcp.h | 42 + drivers/net/ovpn/udp.c | 393 +++++ drivers/net/ovpn/udp.h | 47 + include/net/netlink.h | 1 + include/uapi/linux/ovpn.h | 109 ++ include/uapi/linux/udp.h | 1 + tools/net/ynl/ynl-gen-c.py | 2 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/ovpn/Makefile | 15 + tools/testing/selftests/ovpn/config | 8 + tools/testing/selftests/ovpn/data64.key | 5 + tools/testing/selftests/ovpn/float-test.sh | 113 ++ tools/testing/selftests/ovpn/netns-test.sh | 118 ++ tools/testing/selftests/ovpn/ovpn-cli.c | 1640 ++++++++++++++++++++ tools/testing/selftests/ovpn/run.sh | 12 + tools/testing/selftests/ovpn/tcp_peers.txt | 1 + tools/testing/selftests/ovpn/udp_peers.txt | 5 + 49 files changed, 8716 insertions(+) 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/ovpn/Makefile create mode 100644 tools/testing/selftests/ovpn/config create mode 100644 tools/testing/selftests/ovpn/data64.key create mode 100644 tools/testing/selftests/ovpn/float-test.sh create mode 100644 tools/testing/selftests/ovpn/netns-test.sh create mode 100644 tools/testing/selftests/ovpn/ovpn-cli.c create mode 100644 tools/testing/selftests/ovpn/run.sh create mode 100644 tools/testing/selftests/ovpn/tcp_peers.txt create mode 100644 tools/testing/selftests/ovpn/udp_peers.txt
Signature · 1 line
2.43.2
Antonio Quartulli
maintainerPATCH v3 1/24 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.checks['exact-len']) + ')' + elif 'max-len' in self.checks: + mem = 'NLA_POLICY_MAX_LEN(' + str(self.checks['max-len']) + ')' else: mem = '{ ' if len(self.checks) == 1 and 'min-len' in self.checks:
Signature · 1 line
2.43.2
Antonio Quartulli
maintainerPATCH v3 2/24 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 | 8 ++++ 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 | 83 +++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/main.h | 21 ++++++++++ include/uapi/linux/udp.h | 1 + 9 files changed, 175 insertions(+) create mode 100644 drivers/net/ovpn/Makefile create mode 100644 drivers/net/ovpn/io.c create mode 100644 drivers/net/ovpn/io.h create mode 100644 drivers/net/ovpn/main.c create mode 100644 drivers/net/ovpn/main.h
MAINTAINERS +8 -0
@@ -16660,6 +16660,14 @@ 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/ +F: include/uapi/linux/ovpn.h + 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 +83 -0
83 lines not on this page · raw message
drivers/net/ovpn/main.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_MAIN_H_ +#define _NET_OVPN_MAIN_H_ + +/** +* ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' +* @dev: the interface to check +* +* Return: whether the netdevice is of type 'ovpn' +*/ +bool ovpn_dev_is_valid (const struct net_device *dev); + +#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.43.2
Antonio Quartulli
maintainerPATCH v3 3/24 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 | 331 ++++++++++++++++++++++++++ drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/main.c | 15 ++ drivers/net/ovpn/netlink-gen.c | 206 ++++++++++++++++ drivers/net/ovpn/netlink-gen.h | 41 ++++ drivers/net/ovpn/netlink.c | 154 ++++++++++++ drivers/net/ovpn/netlink.h | 30 +++ drivers/net/ovpn/ovpnstruct.h | 21 ++ include/uapi/linux/ovpn.h | 109 +++++++++ 9 files changed, 909 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 +331 -0
@@ -0,0 +1,331 @@ +# 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: u32 + doc: Number of packets received over the tunnel + - + name: vpn_tx_packets + type: u32 + 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: OVPN_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 + + - + name: pad + type: pad + +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
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 +15 -0
@@ -7,11 +7,15 @@* James Yonan <[email protected]> */ +#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> #include <linux/version.h> +#include <uapi/linux/ovpn.h> +#include "ovpnstruct.h" #include "main.h" +#include "netlink.h" #include "io.h" /* Driver info */
@@ -67,11 +71,22 @@ static int __init ovpn_init(void)return err; } + err =ovpn_nl_register (); +if (err) { +pr_err ("ovpn: can't register netlink family: %d\n" , err); + goto unreg_netdev; + } +return 0 ; + +unreg_netdev: +unregister_netdevice_notifier (&ovpn_netdev_notifier); +return err; } static __exit void ovpn_cleanup(void) { +ovpn_nl_unregister ();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 +154 -0
154 lines not on this page · raw message
drivers/net/ovpn/netlink.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: Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_NETLINK_H_ +#define _NET_OVPN_NETLINK_H_ + +/** +* ovpn_nl_init - initialize netlink specific members +* @ovpn: the openvpn instance to initialize +* +* Return 0 on succe or a negative error code otherwise +*/ +int ovpn_nl_init (struct ovpn_struct *ovpn); + +/** +* ovpn_nl_register - perform any needed registration in the NL subsustem +*/ +int ovpn_nl_register (void ); + +/** +* ovpn_nl_unregister - undo any module wide netlink registration +*/ +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 +109 -0
109 lines not on this page · raw message
Signature · 1 line
2.43.2
Jakub Kicinski
maintainerRe: [PATCH net-next v3 03/24] ovpn: add basic netlink support
Quoted 1 line · On Mon, 6 May 2024 03:16:16 +0200 Antonio Quartulli wrote:
+ name:
nonce_tail_size
nit: typically we hyphenate the names in YAML and C codegen replaces the hyphens with underscores (and converts to uppercase)
Quoted 1 line
+ exact-len:
OVPN_NONCE_TAIL_SIZE
speaking of which - is the codegen buggy or this can be nonce_tail_size? (or rather nonce-tail-size)
Documentation/netlink/specs/ovpn.yaml line 217
Quoted 3 lines
Documentation/netlink/specs/ovpn.yaml new file +3
-0+ - + name: pad + type: pad
You shouldn't need this, now that we have uint.
replace nla_put_u64_64bit() with nla_put_uint(). Unfortunately libnl hasn't caught up so you may need to open code the getter a little in user space CLI.
BTW I'd also bump the packet counters to uint. Doesn't cost much if they don't grow > 32b and you never know..
Documentation/netlink/specs/ovpn.yaml line 233
Quoted 7 lines
Documentation/netlink/specs/ovpn.yaml new file +7
-0+ request: + attributes: + - ifname + - mode + reply: + attributes: + - ifname
The attribute lists
drivers/net/ovpn/netlink.c line 36
Quoted 4 lines
drivers/net/ovpn/netlink.c new file +4
-0+struct net_device *dev; +int ifindex; + +if (!attrs[OVPN_A_IFINDEX ])
GENL_REQ_ATTR_CHECK()
Quoted 5 lines
drivers/net/ovpn/netlink.c new file +5
-0+return ERR_PTR (-EINVAL ); + + ifindex =nla_get_u32 (attrs[OVPN_A_IFINDEX ]); + + dev =dev_get_by_index (net, ifindex);
drivers/net/ovpn/netlink.c line 51
Quoted 10 lines
drivers/net/ovpn/netlink.c new file +10
-0+if (!dev) +return ERR_PTR (-ENODEV ); + +if (!ovpn_dev_is_valid (dev)) + goto err_put_dev; + +return dev; + +err_put_dev: +dev_put (dev);
NL_SET_BAD_ATTR(info->extack, ...[OVPN_A_IFINDEX])
Quoted 1 line
(quoted diff) +1
-0+ return ERR_PTR(-EINVAL);
Antonio Quartulli
maintainerRe: [PATCH net-next v3 03/24] ovpn: add basic netlink support
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Mon, 6 May 2024 03:16:16 +0200 Antonio Quartulli wrote:
+ name:
nonce_tail_sizenit: typically we hyphenate the names in YAML and C codegen replaces the hyphens with underscores (and converts to uppercase)
ACK will go with hyphens.
Quoted 5 lines
Quoted 2 lines
+ exact-len:
OVPN_NONCE_TAIL_SIZEspeaking of which - is the codegen buggy or this can be
nonce_tail_size? (or rather nonce-tail-size)
yeah, something was wrong.
I used 'nonce_tail_size' at first, but it did not get converted, therefore I hardcoded the final define name.
From what you say this seems to be unexpected.
Will check what the script does.
Quoted 7 lines
Documentation/netlink/specs/ovpn.yaml line 217
Quoted 4 lines
Documentation/netlink/specs/ovpn.yaml new file +3
-0+ - + name: pad + type: padYou shouldn't need this, now that we have uint.
replacenla_put_u64_64bit()withnla_put_uint().
ACK
Quoted 2 lines
Unfortunately libnl hasn't caught up so you may need to open code the getter a little in user space CLI.
ok, no big deal.
Quoted 3 lines
BTW I'd also bump the packet counters to uint. Doesn't cost much if they don't grow > 32b and you never know..
Ok, will do
Quoted 9 lines
Documentation/netlink/specs/ovpn.yaml line 233
Quoted 8 lines
Documentation/netlink/specs/ovpn.yaml new file +7
-0+ request: + attributes: + - ifname + - mode + reply: + attributes: + - ifname
Quoted 8 lines
The attribute lists
drivers/net/ovpn/netlink.c line 36
Quoted 4 lines
drivers/net/ovpn/netlink.c new file +4
-0+struct net_device *dev; +int ifindex; + +if (!attrs[OVPN_A_IFINDEX ])
GENL_REQ_ATTR_CHECK()
ACK, I must have missed this one.
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/netlink.c new file +9
-0+return ERR_PTR (-EINVAL ); + + ifindex =nla_get_u32 (attrs[OVPN_A_IFINDEX ]); + + dev =dev_get_by_index (net, ifindex); +if (!dev) +return ERR_PTR (-ENODEV ); + +if (!ovpn_dev_is_valid (dev))
Quoted 8 lines
drivers/net/ovpn/netlink.c line 51
Quoted 6 lines
drivers/net/ovpn/netlink.c new file +6
-0+ goto err_put_dev; + +return dev; + +err_put_dev: +dev_put (dev);
NL_SET_BAD_ATTR(info->extack, ...[OVPN_A_IFINDEX])
Oh, thanks for pointing this out.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +1
-0+ return ERR_PTR(-EINVAL);
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 03/24] ovpn: add basic netlink support
In reply to Antonio Quartullidrivers/net/ovpn/netlink.c line 81
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c new file +3
-0+int ovpn_nl_new_iface_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -ENOTSUPP ;
nit: All thhese should probably be EOPNOTSUPP if those return values can be passed back to userspace, but since you're removing all of them as you implement the functions, it doesn't really matter.
[...]
drivers/net/ovpn/netlink.c line 131
Quoted 8 lines
drivers/net/ovpn/netlink.c new file +8
-0+/** +* ovpn_nl_init - perform any ovpn specific netlink initialization +* @ovpn: the openvpn instance object +*/ +int ovpn_nl_init (struct ovpn_struct *ovpn) +{ +return 0 ; +}
Is this also part of the auto-generated code? Or maybe a leftover from previous iterations? This function doesn't do anything even after all other patches are applied.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 03/24] ovpn: add basic netlink support
Quoted 13 lines · Sabrina Dubroca
drivers/net/ovpn/netlink.c line 81
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c new file +3
-0+int ovpn_nl_new_iface_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -ENOTSUPP ;nit: All thhese should probably be EOPNOTSUPP if those return values can be passed back to userspace, but since you're removing all of them as you implement the functions, it doesn't really matter.
Yeah, I just saw the warnings about these errors.
I'll still change them in v4.
Quoted 14 lines
[...]
drivers/net/ovpn/netlink.c line 131
Quoted 8 lines
drivers/net/ovpn/netlink.c new file +8
-0+/** +* ovpn_nl_init - perform any ovpn specific netlink initialization +* @ovpn: the openvpn instance object +*/ +int ovpn_nl_init (struct ovpn_struct *ovpn) +{ +return 0 ; +}Is this also part of the auto-generated code? Or maybe a leftover from previous iterations? This function doesn't do anything even after all other patches are applied.
Ouch, I missed this. Definitely a left over.
Will remove it.
Thanks a lot
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 4/24 ovpn: add basic interface creation/destruction/management routines
In reply to Antonio QuartulliAdd basic infrastructure for handling ovpn interfaces.
--- drivers/net/ovpn/io.c | 20 ++++ drivers/net/ovpn/io.h | 7 ++ drivers/net/ovpn/main.c | 183 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/main.h | 31 ++++++ drivers/net/ovpn/ovpnstruct.h | 8 ++ drivers/net/ovpn/packet.h | 40 ++++++++ 6 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 drivers/net/ovpn/packet.h
drivers/net/ovpn/io.c +20 -0
@@ -11,6 +11,26 @@#include <linux/skbuff.h> #include "io.h" +#include "ovpnstruct.h" +#include "netlink.h" + +int ovpn_struct_init (struct net_device *dev) +{ +struct ovpn_struct *ovpn =netdev_priv (dev); +int err; + + ovpn->dev = dev; + + err =ovpn_nl_init (ovpn); +if (err <0 ) +return err; + + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); +if (!dev->tstats) +return -ENOMEM ; + +return 0 ; +}/* Send user data to the network */
drivers/net/ovpn/io.h +7 -0
@@ -10,6 +10,13 @@#ifndef _NET_OVPN_OVPN_H_#define _NET_OVPN_OVPN_H_ +/** +* ovpn_struct_init - Initialize the netdevice private area +* @dev: the device to initialize +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_struct_init (struct net_device *dev);netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev);#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +179 -4
252 lines not on this page · raw message
drivers/net/ovpn/main.h +31 -0
@@ -10,6 +10,30 @@#ifndef _NET_OVPN_MAIN_H_#define _NET_OVPN_MAIN_H_ +/** +* 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); + +/** +* 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); +/** * ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' * @dev: the interface to check
@@ -18,4 +42,11 @@*/ 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.43.2
Jakub Kicinski
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 13 lines · On Mon, 6 May 2024 03:16:17 +0200 Antonio Quartulli wrote:
drivers/net/ovpn/io.c +5
-0@@ -11,6 +11,26 @@#include <linux/skbuff.h> #include "io.h" +#include "ovpnstruct.h" +#include "netlink.h" + +int ovpn_struct_init (struct net_device *dev) +{
drivers/net/ovpn/io.c line 28
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+struct ovpn_struct *ovpn =netdev_priv (dev); +int err; + + ovpn->dev = dev; + + err =ovpn_nl_init (ovpn); +if (err <0 ) +return err; + + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Set pcpu_stat_type, core will allocate for you
drivers/net/ovpn/io.c
Quoted 13 lines
drivers/net/ovpn/io.c +12
-0+if (!dev->tstats) +return -ENOMEM ; + +return 0 ; +} +/** +* ovpn_struct_init - Initialize the netdevice private area +* @dev: the device to initialize +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_struct_init (struct net_device *dev);
Weak preference for kdoc to go with the implementation, not declaration.
drivers/net/ovpn/io.c
Quoted 5 lines
drivers/net/ovpn/io.c +5
-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, + .ndo_get_stats64 = dev_get_tstats64,
Core should count pcpu stats automatically
Quoted 4 lines
drivers/net/ovpn/io.c +2
-0+}; +bool ovpn_dev_is_valid (const struct net_device *dev) {
drivers/net/ovpn/io.c
Quoted 10 lines
drivers/net/ovpn/io.c +7
-0return dev->netdev_ops->ndo_start_xmit == ovpn_net_xmit; } +list_add (&ovpn->dev_list, &dev_list); +rtnl_unlock (); + +/* turn carrier explicitly off after registration, this way state is +* clearly defined +*/ +netif_carrier_off (dev);
carrier off inside the locked section, user can call open immediately after unlock
Antonio Quartulli
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 19 lines · Jakub Kicinski
Quoted 17 lines · On Mon, 6 May 2024 03:16:17 +0200 Antonio Quartulli wrote:
drivers/net/ovpn/io.c +9
-0@@ -11,6 +11,26 @@#include <linux/skbuff.h> #include "io.h" +#include "ovpnstruct.h" +#include "netlink.h" + +int ovpn_struct_init (struct net_device *dev) +{ +struct ovpn_struct *ovpn =netdev_priv (dev); +int err; + + ovpn->dev = dev;
Quoted 8 lines
drivers/net/ovpn/io.c line 28
Quoted 6 lines
drivers/net/ovpn/io.c +6
-0+ + err =ovpn_nl_init (ovpn); +if (err <0 ) +return err; + + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);Set
pcpu_stat_type, core will allocate for you
ok
Quoted 7 lines
Quoted 6 lines
drivers/net/ovpn/io.c +5
-0+if (!dev->tstats) +return -ENOMEM ; + +return 0 ; +}
Quoted 9 lines
drivers/net/ovpn/io.c
Quoted 7 lines
drivers/net/ovpn/io.c +7
-0+/** +* ovpn_struct_init - Initialize the netdevice private area +* @dev: the device to initialize +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_struct_init (struct net_device *dev);Weak preference for kdoc to go with the implementation, not declaration.
oh ok - this wasn't clear.
Will move the kdoc next to the implementation.
Quoted 8 lines
drivers/net/ovpn/io.c
Quoted 6 lines
drivers/net/ovpn/io.c +5
-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, + .ndo_get_stats64 = dev_get_tstats64,Core should count pcpu stats automatically
Thanks for pointing this out.
I see dev_get_stats() takes care of all this for us.
Quoted 8 lines
Quoted 7 lines
drivers/net/ovpn/io.c +2
-0+}; +bool ovpn_dev_is_valid (const struct net_device *dev) {return dev->netdev_ops->ndo_start_xmit == ovpn_net_xmit; }
Quoted 10 lines
drivers/net/ovpn/io.c
Quoted 7 lines
drivers/net/ovpn/io.c +7
-0+list_add (&ovpn->dev_list, &dev_list); +rtnl_unlock (); + +/* turn carrier explicitly off after registration, this way state is +* clearly defined +*/ +netif_carrier_off (dev);carrier off inside the locked section, user can call open immediately after unlock
ok, will move it up.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
In reply to Antonio Quartullidrivers/net/ovpn/io.c line 17
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/io.c +4
-0@@ -11,6 +11,26 @@#include <linux/skbuff.h> #include "io.h" +#include "ovpnstruct.h" +#include "netlink.h" + +int ovpn_struct_init (struct net_device *dev)
nit: Should this be in main.c? It's only used there, and I think it would make more sense to drop it next to ovpn_struct_free.
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+{ +struct ovpn_struct *ovpn =netdev_priv (dev); +int err; +
[...]
Quoted 4 lines
drivers/net/ovpn/main.c +0
-0
[...]
drivers/net/ovpn/main.c line 35
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static void ovpn_struct_free (struct net_device *net) +{ +struct ovpn_struct *ovpn =netdev_priv (net); + +rtnl_lock ();
->priv_destructor can run from register_netdevice (already under
RTNL), this doesn't look right.
drivers/net/ovpn/main.c line 36
Quoted 1 line
drivers/net/ovpn/main.c +1
-0+list_del (&ovpn->dev_list);
And if this gets called from register_netdevice, the list_add from ovpn_iface_create hasn't run yet, so this will probably do strange things?
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+rtnl_unlock (); + +free_percpu (net->tstats); +} +
drivers/net/ovpn/main.c line 51
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+static int ovpn_net_open (struct net_device *dev) +{ +struct in_device *dev_v4 =__in_dev_get_rtnl (dev); + +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn handling +* same-LAN routing +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false;
Jakub, are you ok with that? This feels a bit weird to have in the middle of a driver.
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+ } + +netif_tx_start_all_queues (dev); +return 0 ; +}
[...]
drivers/net/ovpn/main.c line 165
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; + +unregister_netdevice (ovpn->dev); +synchronize_net ();
If this gets called from the loop in ovpn_netns_pre_exit, one synchronize_net per ovpn device would seem quite expensive.
Quoted 22 lines
drivers/net/ovpn/main.c +7
-3+} +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 */
drivers/net/ovpn/main.c line 194
Quoted 10 lines
drivers/net/ovpn/main.c +7
-0+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);
Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
Quoted 14 lines
drivers/net/ovpn/main.c +1
-0break ;case NETDEV_POST_INIT :case NETDEV_GOING_DOWN :case NETDEV_DOWN :case NETDEV_UP :case NETDEV_PRE_UP : +break ;default :return NOTIFY_DONE ; }@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, };
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 222
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+static void ovpn_netns_pre_exit (struct net *net) +{ +struct ovpn_struct *ovpn; + +rtnl_lock (); +list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);
Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Quoted 3 lines
drivers/net/ovpn/main.c +3
-0+ } +rtnl_unlock (); +}
Signature · 1 line
Sabrina
Jakub Kicinski
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 5 lines · On Wed, 8 May 2024 16:52:27 +0200 Sabrina Dubroca wrote:
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static int ovpn_net_open (struct net_device *dev) +{ +struct in_device *dev_v4 =__in_dev_get_rtnl (dev); + +if (dev_v4) {
Quoted 8 lines
drivers/net/ovpn/main.c line 51
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+/* disable redirects as Linux gets confused by ovpn handling +* same-LAN routing +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false;Jakub, are you ok with that? This feels a bit weird to have in the middle of a driver.
Herm, I only looked at the netlink bits so far.
Would be good to get more details on the problem and see if we can fix it more directly.
Antonio Quartulli
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
In reply to Sabrina DubrocaQuoted 8 lines · Sabrina Dubroca
Quoted 7 lines · Antonio Quartulli
drivers/net/ovpn/io.c +0
-0@@ -11,6 +11,26 @@#include <linux/skbuff.h>
Quoted 8 lines
drivers/net/ovpn/io.c line 17
Quoted 5 lines
drivers/net/ovpn/io.c +4
-0#include "io.h" +#include "ovpnstruct.h" +#include "netlink.h" + +int ovpn_struct_init (struct net_device *dev)nit: Should this be in
main.c? It's only used there, and I think it would make more sense to drop it next toovpn_struct_free.
yeah, it makes sense. will move it.
Quoted 21 lines
Quoted 5 lines
drivers/net/ovpn/io.c +4
-0+{ +struct ovpn_struct *ovpn =netdev_priv (dev); +int err; +[...]
Quoted 4 lines
drivers/net/ovpn/main.c +0
-0[...]
drivers/net/ovpn/main.c line 35
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static void ovpn_struct_free (struct net_device *net) +{ +struct ovpn_struct *ovpn =netdev_priv (net); + +rtnl_lock ();->priv_destructor can run from
register_netdevice(already under
RTNL), this doesn't look right.
Quoted 5 lines
drivers/net/ovpn/main.c line 36
Quoted 1 line
drivers/net/ovpn/main.c +1
-0+list_del (&ovpn->dev_list);And if this gets called from
register_netdevice, thelist_addfromovpn_iface_createhasn't run yet, so this will probably do strange things?
Argh, again I haven't considered a failure in register_netdevice and you are indeed right.
Maybe it is better to call list_del() in the netdev notifier, upon NETDEV_UNREGISTER event?
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/main.c +10
-0+rtnl_unlock (); + +free_percpu (net->tstats); +} + +static int ovpn_net_open (struct net_device *dev) +{ +struct in_device *dev_v4 =__in_dev_get_rtnl (dev); + +if (dev_v4) {
Quoted 8 lines
drivers/net/ovpn/main.c line 51
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+/* disable redirects as Linux gets confused by ovpn handling +* same-LAN routing +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false;Jakub, are you ok with that? This feels a bit weird to have in the middle of a driver.
Let me share what the problem is (copied from the email I sent to Andrew Lunn as he was also curious about this):
The reason for requiring this setting lies in the OpenVPN server acting as relay point (star topology) for hosts in the same subnet.
Example: given the a.b.c.0/24 IP network, you have .2 that in order to talk to .3 must have its traffic relayed by .1 (the server).
When the kernel (at .1) sees this traffic it will send the ICMP redirects, because it believes that .2 should directly talk to .3 without passing through .1.
Of course it makes sense in a normal network with a classic broadcast domain, but this is not the case in a VPN implemented as a star topology.
Does it make sense?
The only way I see to fix this globally is to have an extra flag in the netdevice signaling this peculiarity and thus disabling ICMP redirects automatically.
Note: wireguard has those lines too, as it probably needs to address the same scenario.
Quoted 8 lines
Quoted 6 lines
drivers/net/ovpn/main.c +5
-0+ } + +netif_tx_start_all_queues (dev); +return 0 ; +}[...]
Quoted 13 lines
drivers/net/ovpn/main.c line 165
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; + +unregister_netdevice (ovpn->dev); +synchronize_net ();If this gets called from the loop in
ovpn_netns_pre_exit, onesynchronize_netper ovpn device would seem quite expensive.
As per your other comment, maybe I should just remove the synchronize_net() entirely since it'll be the core to take care of inflight packets?
Quoted 28 lines
Quoted 28 lines
drivers/net/ovpn/main.c +10
-3+} +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
Quoted 8 lines
drivers/net/ovpn/main.c line 194
Quoted 5 lines
*/
drivers/net/ovpn/main.c +4
-0+if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
We need to perform some internal cleanup (i.e. release all peers). I don't see how this can happen automatically, no?
Quoted 15 lines
Quoted 15 lines
break;
caseNETDEV_POST_INIT:
caseNETDEV_GOING_DOWN:
caseNETDEV_DOWN:
caseNETDEV_UP:
caseNETDEV_PRE_UP:drivers/net/ovpn/main.c +1
-0+break ;default :return NOTIFY_DONE ; }@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, };
Quoted 13 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 222
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+static void ovpn_netns_pre_exit (struct net *net) +{ +struct ovpn_struct *ovpn; + +rtnl_lock (); +list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Before implementing ovpn_netns_pre_exit() this way, upon namespace deletion the ovpn interface was being moved to the global namespace.
Hence I decided to manually take care of its destruction.
Isn't this expected?
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 22 lines · Antonio Quartulli
Quoted 15 lines · Sabrina Dubroca
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/main.c +0
-0[...]
drivers/net/ovpn/main.c line 35
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static void ovpn_struct_free (struct net_device *net) +{ +struct ovpn_struct *ovpn =netdev_priv (net); + +rtnl_lock ();->priv_destructor can run from
register_netdevice(already under
RTNL), this doesn't look right.Quoted 5 lines
drivers/net/ovpn/main.c line 36
Quoted 1 line
drivers/net/ovpn/main.c +1
-0+list_del (&ovpn->dev_list);And if this gets called from
register_netdevice, thelist_addfromovpn_iface_createhasn't run yet, so this will probably do strange things?
Quoted 5 lines
Argh, again I haven't considered a failure in
register_netdeviceand you are indeed right.Maybe it is better to call
list_del()in the netdev notifier, uponNETDEV_UNREGISTERevent?
I'd like to avoid splitting the clean up code over so maybe different functions and called through different means. Keep it simple.
AFAICT the only reason you need this list is to delete your devices on netns exit, so if we can get rid of that the list can go away.
Quoted 20 lines
Quoted 5 lines
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static int ovpn_net_open (struct net_device *dev) +{ +struct in_device *dev_v4 =__in_dev_get_rtnl (dev); + +if (dev_v4) {Quoted 8 lines
drivers/net/ovpn/main.c line 51
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+/* disable redirects as Linux gets confused by ovpn handling +* same-LAN routing +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false;Jakub, are you ok with that? This feels a bit weird to have in the middle of a driver.
Let me share what the problem is (copied from the email I sent to Andrew Lunn as he was also curious about this):
The reason for requiring this setting lies in the OpenVPN server acting as relay point (star topology) for hosts in the same subnet.
Quoted 6 lines
Example: given the a.b.c.0/24 IP network, you have .2 that in order to talk to .3 must have its traffic relayed by .1 (the server).
When the kernel (at .1) sees this traffic it will send the ICMP redirects, because it believes that .2 should directly talk to .3 without passing through .1.
So only the server would need to stop sending them, not the client? (or the client would need to ignore them)
But the kernel has no way of knowing if an ovpn device is on a client or a server?
Quoted 4 lines
Of course it makes sense in a normal network with a classic broadcast domain, but this is not the case in a VPN implemented as a star topology.
Does it make sense?
It looks like the problem is that ovpn links are point-to-point (instead of a broadcast LAN kind of link where redirects would make sense), and the kernel doesn't handle it that way.
Quoted 6 lines
The only way I see to fix this globally is to have an extra flag in the netdevice signaling this peculiarity and thus disabling ICMP redirects automatically.
Note: wireguard has those lines too, as it probably needs to address the same scenario.
I've noticed a lot of similarities in some bits I've looked at, and I hate that this is turning into another pile of duplicate code like vxlan/geneve, bond/team, etc :(
Quoted 15 lines
Quoted 14 lines
[...]
drivers/net/ovpn/main.c line 165
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; + +unregister_netdevice (ovpn->dev); +synchronize_net ();If this gets called from the loop in
ovpn_netns_pre_exit, onesynchronize_netper ovpn device would seem quite expensive.
Quoted 2 lines
As per your other comment, maybe I should just remove the
synchronize_net()entirely since it'll be the core to take care of inflight packets?
There's a synchronize_net in unregister_netdevice_many_notify, so I'd say you can get rid of it here.
Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
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);drivers/net/ovpn/main.c +8
-3+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 Quoted 8 lines
drivers/net/ovpn/main.c line 194
Quoted 5 lines
*/
drivers/net/ovpn/main.c +4
-0+if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
Quoted 2 lines
We need to perform some internal cleanup (i.e. release all peers). I don't see how this can happen automatically, no?
That's what ->priv_destructor does, and it will be called ultimately by the unregister_netdevice call you have in ovpn_iface_destruct (in netdev_run_todo). Anyway, this UNREGISTER event is probably generated by unregister_netdevice_many_notify (basically a previous unregister_netdevice() call), so I don't know why you want to call unregister_netdevice again on the same device.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/net/ovpn/main.c +5
-0@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, }; +static void ovpn_netns_pre_exit (struct net *net) +{ +struct ovpn_struct *ovpn; + +rtnl_lock ();Quoted 8 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 222
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Quoted 2 lines
Before implementing
ovpn_netns_pre_exit()this way, upon namespace deletion the ovpn interface was being moved to the global namespace.
Crap it's only the devices with ->rtnl_link_ops that get killed by the core. Because you create your devices via genl (which I'm not a fan of, even if it's a bit nicer for userspace having a single netlink api to deal with), default_device_exit_batch/default_device_exit_net think ovpn devices are real NICs and move them back to init_net instead of destroying them.
Maybe we can extend the condition in default_device_exit_net with a new flag so that ovpn devices get destroyed by the core, even without rtnl_link_ops?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 26 lines · Sabrina Dubroca
Quoted 25 lines · Antonio Quartulli
Quoted 15 lines · Sabrina Dubroca
Quoted 4 lines · Antonio Quartulli
drivers/net/ovpn/main.c +0
-0[...]
drivers/net/ovpn/main.c line 35
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+static void ovpn_struct_free (struct net_device *net) +{ +struct ovpn_struct *ovpn =netdev_priv (net); + +rtnl_lock ();->priv_destructor can run from
register_netdevice(already under
RTNL), this doesn't look right.Quoted 5 lines
drivers/net/ovpn/main.c line 36
Quoted 1 line
drivers/net/ovpn/main.c +1
-0+list_del (&ovpn->dev_list);And if this gets called from
register_netdevice, thelist_addfromovpn_iface_createhasn't run yet, so this will probably do strange things?Argh, again I haven't considered a failure in
register_netdeviceand you are indeed right.
Quoted 8 lines
Quoted 2 lines
Maybe it is better to call
list_del()in the netdev notifier, uponNETDEV_UNREGISTERevent?I'd like to avoid splitting the clean up code over so maybe different functions and called through different means. Keep it simple.
AFAICT the only reason you need this list is to delete your devices on netns exit, so if we can get rid of that the list can go away.
right.
Quoted 22 lines
Quoted 22 lines
Quoted 15 lines
drivers/net/ovpn/main.c line 51
Quoted 12 lines
drivers/net/ovpn/main.c +10
-0+static int ovpn_net_open (struct net_device *dev) +{ +struct in_device *dev_v4 =__in_dev_get_rtnl (dev); + +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn handling +* same-LAN routing +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false;Jakub, are you ok with that? This feels a bit weird to have in the middle of a driver.
Let me share what the problem is (copied from the email I sent to Andrew Lunn as he was also curious about this):
The reason for requiring this setting lies in the OpenVPN server acting as relay point (star topology) for hosts in the same subnet.
Quoted 8 lines
Quoted 6 lines
Example: given the a.b.c.0/24 IP network, you have .2 that in order to talk to .3 must have its traffic relayed by .1 (the server).
When the kernel (at .1) sees this traffic it will send the ICMP redirects, because it believes that .2 should directly talk to .3 without passing through .1.
So only the server would need to stop sending them, not the client?
correct
Quoted 3 lines
(or the client would need to ignore them)
But the kernel has no way of knowing if an ovpn device is on a client or a server?
the server knows if the interface is configured in P2P or MP (MultiPeer) mode. The latter is what requires redirects to be off, so we could at least add a check and switch them off only for MP ifaces.
Quoted 9 lines
Quoted 5 lines
Of course it makes sense in a normal network with a classic broadcast domain, but this is not the case in a VPN implemented as a star topology.
Does it make sense?
It looks like the problem is that ovpn links are point-to-point (instead of a broadcast LAN kind of link where redirects would make sense), and the kernel doesn't handle it that way.
exactly
Quoted 11 lines
Quoted 7 lines
The only way I see to fix this globally is to have an extra flag in the netdevice signaling this peculiarity and thus disabling ICMP redirects automatically.
Note: wireguard has those lines too, as it probably needs to address the same scenario.
I've noticed a lot of similarities in some bits I've looked at, and I hate that this is turning into another pile of duplicate code like vxlan/geneve, bond/team, etc :(
For starters, we could at least moves these few lines in some helper function and call it from both modules.
On the other hand, we could, like I suggested above, convert this into a netdev flag and let core handle the behaviour when the flag is set.
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
[...]
drivers/net/ovpn/main.c line 165
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; + +unregister_netdevice (ovpn->dev); +synchronize_net ();If this gets called from the loop in
ovpn_netns_pre_exit, onesynchronize_netper ovpn device would seem quite expensive.
Quoted 5 lines
Quoted 2 lines
As per your other comment, maybe I should just remove the
synchronize_net()entirely since it'll be the core to take care of inflight packets?There's a
synchronize_netinunregister_netdevice_many_notify, so I'd say you can get rid of it here.
ok! Jakub was indeed suggesting that core should already take care of this.
Will remove it for good.
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 20 lines
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);drivers/net/ovpn/main.c +5
-3+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 */ drivers/net/ovpn/main.c line 194
Quoted 10 lines
drivers/net/ovpn/main.c +7
-0+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);Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
Quoted 4 lines
Quoted 2 lines
We need to perform some internal cleanup (i.e. release all peers). I don't see how this can happen automatically, no?
That's what ->priv_destructor does,
Not really.
Every peer object increases the netdev refcounter to the netdev, therefore we must first delete all peers in order to have netdevice->refcnt reach 0 (and then invoke priv_destructor).
So the idea is: upon UNREGISTER event we drop all resources and eventually (via RCU) all references to the netdev are also released, which in turn triggers the destructor.
makes sense?
Quoted 6 lines
and it will be called ultimately
by theunregister_netdevicecall you have inovpn_iface_destruct(innetdev_run_todo). Anyway, this UNREGISTER event is probably generated byunregister_netdevice_many_notify(basically a previousunregister_netdevice()call), so I don't know why you want to callunregister_netdeviceagain on the same device.
I believe I have seen this notification being triggered upon netns exit, but in that case the netdevice was not being removed from core.
Hence I decided to fully trigger the unregistration.
Expected?
I can repeat the test to be sure.
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 5 lines
drivers/net/ovpn/main.c +0
-0@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, };drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 221
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+static void ovpn_netns_pre_exit (struct net *net) +{ +struct ovpn_struct *ovpn; + +rtnl_lock (); +list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Quoted 5 lines
Quoted 2 lines
Before implementing
ovpn_netns_pre_exit()this way, upon namespace deletion the ovpn interface was being moved to the global namespace.Crap it's only the devices with ->rtnl_link_ops that get killed by the core.
exactly! this goes hand to hand with my comment above: event delivered but interface not destroyed.
Quoted 3 lines
Because you create your devices via genl (which I'm not a fan of, even if it's a bit nicer for userspace having a single netlink api to deal with),
Originally I had implemented the rtnl_link_ops, but the (meaningful) objection was that a user is never supposed to create an ovpn iface by himself, but there should always be an openvpn process running in userspace. Hence the restriction to genl only.
Quoted 7 lines
default_device_exit_batch/default_device_exit_net think ovpn devices are real NICs and move them back to
init_netinstead of destroying them.Maybe we can extend the condition in
default_device_exit_netwith a new flag so that ovpn devices get destroyed by the core, even withoutrtnl_link_ops?
Thanks for pointing out the function responsible for this decision. How would you extend the check though?
Alternatively, what if ovpn simply registers an empty rtnl_link_ops with netns_fund set to false? That should make the condition happy, while keeping ovpn genl-only
Thanks a lot
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 39 lines · Antonio Quartulli
Quoted 38 lines · Sabrina Dubroca
Quoted 36 lines · Antonio Quartulli
Quoted 24 lines · Sabrina Dubroca
Quoted 23 lines · Antonio Quartulli
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);drivers/net/ovpn/main.c +8
-3+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 Quoted 8 lines
drivers/net/ovpn/main.c line 194
Quoted 5 lines
*/drivers/net/ovpn/main.c +4
-0+if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
We need to perform some internal cleanup (i.e. release all peers). I don't see how this can happen automatically, no?
Quoted 7 lines
Quoted 1 line
That's what ->priv_destructor does,
Not really.
Every peer object increases the netdev refcounter to the netdev, therefore we must first delete all peers in order to have
netdevice->refcntreach 0 (and then invokepriv_destructor).
Oh, I see. I'm still trying to wrap my head around all the objects and components of your driver.
Quoted 5 lines
So the idea is: upon UNREGISTER event we drop all resources and eventually (via RCU) all references to the netdev are also released, which in turn triggers the destructor.
makes sense?
That part, yes, thanks for explaining. Do you really need the peers to hold a reference on the netdevice? With my limited understanding, it seems the peers are sub-objects of the netdevice.
Quoted 9 lines
Quoted 6 lines
and it will be called ultimately
by theunregister_netdevicecall you have inovpn_iface_destruct(innetdev_run_todo). Anyway, this UNREGISTER event is probably generated byunregister_netdevice_many_notify(basically a previousunregister_netdevice()call), so I don't know why you want to callunregister_netdeviceagain on the same device.I believe I have seen this notification being triggered upon netns exit, but in that case the netdevice was not being removed from core.
Sure, but you have a comment about that and you're filtering that event, so I'm ignoring this case.
Quoted 1 line
Hence I decided to fully trigger the unregistration.
That's the bit that doesn't make sense to me: the device is going away, so you trigger a manual unregister. Cleaning up some additional resources (peers etc), that makes sense. But calling unregister_netdevice (when you're most likely getting called from unregister_netdevice already, because I don't see other spots setting dev->reg_state = NETREG_UNREGISTERING) is what I don't get. And I wonder why you're not hitting the BUG_ON in unregister_netdevice_many_notify:
BUG_ON(dev->reg_state != NETREG_REGISTERED);
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 212
Quoted 4 lines
drivers/net/ovpn/main.c +1
-0@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, }; +static void ovpn_netns_pre_exit (struct net *net)
BTW, in case you end up keeping this function, it should have __net_exit annotation (see for example ipv4_frags_exit_net).
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 222
Quoted 9 lines
drivers/net/ovpn/main.c +9
-0+{ +struct ovpn_struct *ovpn; + +rtnl_lock (); +list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Before implementing
ovpn_netns_pre_exit()this way, upon namespace deletion the ovpn interface was being moved to the global namespace.Crap it's only the devices with ->rtnl_link_ops that get killed by the core.
exactly! this goes hand to hand with my comment above: event delivered but interface not destroyed.
There's no event sent to ovpn_netdev_notifier_call in that case (well, only the fake "unregister" out of the current netns that you're ignoring). Otherwise, you wouldn't need ovpn_netns_pre_exit.
Quoted 8 lines
Quoted 3 lines
Because you create your devices via genl (which I'm not a fan of, even if it's a bit nicer for userspace having a single netlink api to deal with),
Originally I had implemented the
rtnl_link_ops, but the (meaningful) objection was that a user is never supposed to create an ovpn iface by himself, but there should always be an openvpn process running in userspace. Hence the restriction to genl only.
Sorry, but how does genl prevent a user from creating the ovpn interface manually? Whatever API you define, anyone who manages to come up with the right netlink message will be able to create an interface. You can't stop people from using your API without your official client.
Quoted 8 lines
Quoted 7 lines
default_device_exit_batch/default_device_exit_net think ovpn devices are real NICs and move them back to
init_netinstead of destroying them.Maybe we can extend the condition in
default_device_exit_netwith a new flag so that ovpn devices get destroyed by the core, even withoutrtnl_link_ops?
Quoted 6 lines
Thanks for pointing out the function responsible for this decision. How would you extend the check though?
Alternatively, what if ovpn simply registers an empty
rtnl_link_opswithnetns_fundset to false? That should make the condition happy, while keeping ovpn genl-only
Yes. I was thinking about adding a flag to the device, because I wasn't sure an almost empty rtnl_link_ops could be handled safely, but it seems ok. ovs does it, see commit 5b9e7e160795 ("openvswitch:
introduce rtnl ops stub"). And, as that commit message says, "ip -d link show" would also show that the device is of type openvpn (or ovpn, whatever you put in ops->kind), which would be nice.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
By the way, thank you very much for taking the time to have this constructive discussion. I really appreciate it!
Quoted 68 lines · Sabrina Dubroca
Quoted 39 lines · Antonio Quartulli
Quoted 38 lines · Sabrina Dubroca
Quoted 36 lines · Antonio Quartulli
Quoted 24 lines · Sabrina Dubroca
Quoted 23 lines · Antonio Quartulli
static int ovpn_netdev_notifier_call(struct
notifier_block*nb,
unsigned long state, void *ptr)
{
structnet_device*dev =netdev_notifier_info_to_dev(ptr);drivers/net/ovpn/main.c +8
-3+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 Quoted 8 lines
drivers/net/ovpn/main.c line 194
Quoted 5 lines
*/drivers/net/ovpn/main.c +4
-0+if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);Maybe I'm misunderstanding this code. Why do you want to manually destroy a device that is already going away?
We need to perform some internal cleanup (i.e. release all peers). I don't see how this can happen automatically, no?
Quoted 7 lines
Quoted 1 line
That's what ->priv_destructor does,
Not really.
Every peer object increases the netdev refcounter to the netdev, therefore we must first delete all peers in order to have
netdevice->refcntreach 0 (and then invokepriv_destructor).Oh, I see. I'm still trying to wrap my head around all the objects and components of your driver.
Quoted 5 lines
So the idea is: upon UNREGISTER event we drop all resources and eventually (via RCU) all references to the netdev are also released, which in turn triggers the destructor.
makes sense?
That part, yes, thanks for explaining. Do you really need the peers to hold a reference on the netdevice? With my limited understanding, it seems the peers are sub-objects of the netdevice.
Quoted 7 lines
Quoted 6 lines
and it will be called ultimately
by theunregister_netdevicecall you have inovpn_iface_destruct(innetdev_run_todo). Anyway, this UNREGISTER event is probably generated byunregister_netdevice_many_notify(basically a previousunregister_netdevice()call), so I don't know why you want to callunregister_netdeviceagain on the same device.
Quoted 5 lines
Quoted 2 lines
I believe I have seen this notification being triggered upon netns exit, but in that case the netdevice was not being removed from core.
Sure, but you have a comment about that and you're filtering that event, so I'm ignoring this case.
You're right..now I wonder if my observation was made before I introduced that check...
Quoted 12 lines
Quoted 2 lines
Hence I decided to fully trigger the unregistration.
That's the bit that doesn't make sense to me: the device is going away, so you trigger a manual unregister. Cleaning up some additional resources (peers etc), that makes sense. But calling
unregister_netdevice(when you're most likely getting called fromunregister_netdevicealready, because I don't see other spots settingdev->reg_state=NETREG_UNREGISTERING) is what I don't get. And I wonder why you're not hitting theBUG_ONinunregister_netdevice_many_notify:
Quoted 1 line
BUG_ON(dev->reg_state != NETREG_REGISTERED);
I think because we have our ovpn->registered check.
It ensures that we don't call ovpn_iface_destruct more than once.
But now, that I implemented the rtnl_link_ops I can confirm I am hitting the BUG_ON. And now it makes sense.
I presume that now I can I simply remove the call to unregister_netdevice() from ovpn_iface_destruct() and move it to ovpn_nl_del_iface_doit().
This way, upon netns exit, the real UNREGISTER handler (triggered thanks to rtnl_link_ops) will still perform the destruct, but won't try to schedule an UNREGISTER event again.
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 212
Quoted 6 lines
drivers/net/ovpn/main.c +1
-0@@ -62,6 +210,24 @@ static struct notifier_block ovpn_netdev_notifier = { .notifier_call = ovpn_netdev_notifier_call, }; +static void ovpn_netns_pre_exit (struct net *net)BTW, in case you end up keeping this function, it should have
__net_exitannotation (see for exampleipv4_frags_exit_net).
ACK, but thanks to the rtnl_link_ops trick we are definitely ditching it.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines
Quoted 13 lines
drivers/net/ovpn/main.c static struct notifier_block ovpn_netdev_notifier = { · line 222
Quoted 10 lines
drivers/net/ovpn/main.c +9
-0+{ +struct ovpn_struct *ovpn; + +rtnl_lock (); +list_for_each_entry (ovpn, &dev_list, dev_list) { +if (dev_net (ovpn->dev) != net) +continue ; + +ovpn_iface_destruct (ovpn);Is this needed? On netns destruction all devices within the ns will be destroyed by the networking core.
Quoted 2 lines
Before implementing
ovpn_netns_pre_exit()this way, upon namespace deletion the ovpn interface was being moved to the global namespace.Crap it's only the devices with ->rtnl_link_ops that get killed by the core.
Quoted 6 lines
Quoted 2 lines
exactly! this goes hand to hand with my comment above: event delivered but interface not destroyed.
There's no event sent to
ovpn_netdev_notifier_callin that case (well, only the fake "unregister" out of the current netns that you're ignoring). Otherwise, you wouldn't needovpn_netns_pre_exit.
Yeah you're right. I think I wanted to conclude the same thing but my brain was unable to produce a meaningful sentence.
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Because you create your devices via genl (which I'm not a fan of, even if it's a bit nicer for userspace having a single netlink api to deal with),
Originally I had implemented the
rtnl_link_ops, but the (meaningful) objection was that a user is never supposed to create an ovpn iface by himself, but there should always be an openvpn process running in userspace. Hence the restriction to genl only.
Quoted 5 lines
Sorry, but how does genl prevent a user from creating the ovpn interface manually? Whatever API you define, anyone who manages to come up with the right netlink message will be able to create an interface. You can't stop people from using your API without your official client.
I don't want to prevent people from creating ovpn ifaces the way they like.
I just don't see how the rtnl_link API can be useful, other than allowing users to execute 'ip link add/del..'.
And by design that is not a usecase we want to support, because once the iface is created, nothing will happen if there is no userspace software driving it (no matter if it is openvpn or anything else).
When explaining this decision, I like to make a comparison to virtual 802.11/wifi ifaces.
They also lack rtnl_link (AFAIR) as they also require some userspace software to handle them in order to be useful.
All this said, having everything in one place looks cleaner too :)
Quoted 16 lines
Quoted 9 lines
Quoted 8 lines
default_device_exit_batch/default_device_exit_net think ovpn devices are real NICs and move them back to
init_netinstead of destroying them.Maybe we can extend the condition in
default_device_exit_netwith a new flag so that ovpn devices get destroyed by the core, even withoutrtnl_link_ops?Quoted 6 lines
Thanks for pointing out the function responsible for this decision. How would you extend the check though?
Alternatively, what if ovpn simply registers an empty
rtnl_link_opswithnetns_fundset to false? That should make the condition happy, while keeping ovpn genl-only
Quoted 6 lines
Yes. I was thinking about adding a flag to the device, because I wasn't sure an almost empty
rtnl_link_opscould be handled safely, but it seems ok. ovs does it, see commit5b9e7e160795("openvswitch:
introduce rtnl ops stub"). And, as that commit message says, "ip -d link show" would also show that the device is of type openvpn (or ovpn, whatever you put inops->kind), which would be nice.
I just coded something along those lines.
It seems pretty clean and we don't need to touch core (+ the bonus of having the name in "ip -d link")....and the iface does get destroyed upon netns exit! :-)
I am grasping much better how all these APIs work together now.
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 04/24] ovpn: add basic interface creation/destruction/management routines
Quoted 2 lines · Antonio Quartulli
By the way, thank you very much for taking the time to have this constructive discussion. I really appreciate it!
Cheers :)
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines
Quoted 2 lines · Antonio Quartulli
On 09/05/2024 12:09, Sabrina Dubroca wrote:
Hence I decided to fully trigger the unregistration.That's the bit that doesn't make sense to me: the device is going away, so you trigger a manual unregister. Cleaning up some additional resources (peers etc), that makes sense. But calling
unregister_netdevice(when you're most likely getting called fromunregister_netdevicealready, because I don't see other spots settingdev->reg_state=NETREG_UNREGISTERING) is what I don't get. And I wonder why you're not hitting theBUG_ONinunregister_netdevice_many_notify:
Quoted 5 lines
Quoted 1 line
BUG_ON(dev->reg_state != NETREG_REGISTERED);I think because we have our
ovpn->registeredcheck.It ensures that we don't call
ovpn_iface_destructmore than once.
Ah, probably, yes.
Quoted 5 lines
But now, that I implemented the
rtnl_link_opsI can confirm I am hitting theBUG_ON. And now it makes sense.I presume that now I can I simply remove the call to
unregister_netdevice()fromovpn_iface_destruct()and move it toovpn_nl_del_iface_doit().
Sounds good.
Quoted 23 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Because you create your devices via genl (which I'm not a fan of, even if it's a bit nicer for userspace having a single netlink api to deal with),
Originally I had implemented the
rtnl_link_ops, but the (meaningful) objection was that a user is never supposed to create an ovpn iface by himself, but there should always be an openvpn process running in userspace. Hence the restriction to genl only.Quoted 5 lines
Sorry, but how does genl prevent a user from creating the ovpn interface manually? Whatever API you define, anyone who manages to come up with the right netlink message will be able to create an interface. You can't stop people from using your API without your official client.
I don't want to prevent people from creating ovpn ifaces the way they like. I just don't see how the
rtnl_linkAPI can be useful, other than allowing users to execute 'ip link add/del..'.And by design that is not a usecase we want to support, because once the iface is created, nothing will happen if there is no userspace software driving it (no matter if it is openvpn or anything else).
Quoted 6 lines
When explaining this decision, I like to make a comparison to virtual 802.11/wifi ifaces.
They also lackrtnl_link(AFAIR) as they also require some userspace software to handle them in order to be useful.All this said, having everything in one place looks cleaner too :)
From an API point of view, maybe. But for the kernel implementation, using rtnl_link_ops->newlink is easier.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 7 lines
default_device_exit_batch/default_device_exit_net think ovpn devices are real NICs and move them back to
init_netinstead of destroying them.Maybe we can extend the condition in
default_device_exit_netwith a new flag so that ovpn devices get destroyed by the core, even withoutrtnl_link_ops?Thanks for pointing out the function responsible for this decision. How would you extend the check though?
Alternatively, what if ovpn simply registers an empty
rtnl_link_opswithnetns_fundset to false? That should make the condition happy, while keeping ovpn genl-only
Quoted 8 lines
Quoted 6 lines
Yes. I was thinking about adding a flag to the device, because I wasn't sure an almost empty
rtnl_link_opscould be handled safely, but it seems ok. ovs does it, see commit5b9e7e160795("openvswitch:
introduce rtnl ops stub"). And, as that commit message says, "ip -d link show" would also show that the device is of type openvpn (or ovpn, whatever you put inops->kind), which would be nice.I just coded something along those lines.
Great, thanks.
Quoted 5 lines
It seems pretty clean and we don't need to touch core (+ the bonus of having the name in "ip -d link")....and the iface does get destroyed upon netns exit! :-)
I am grasping much better how all these APIs work together now.
Nice :)
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerPATCH v3 5/24 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 | 55 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 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" +/** * ovpn_iface_create - create and initialize a new 'ovpn' netdevice * @name: the name of the new device
drivers/net/ovpn/netlink.c +53 -2
71 lines not on this page · raw message
Signature · 1 line
2.43.2
Jakub Kicinski
maintainerRe: [PATCH net-next v3 05/24] ovpn: implement interface creation/destruction via netlink
Quoted 11 lines · On Mon, 6 May 2024 03:16:18 +0200 Antonio Quartulli wrote:
int
ovpn_nl_new_iface_doit(struct sk_buff *skb, struct genl_info *info){drivers/net/ovpn/netlink.c +8
-1-return -ENOTSUPP ; +const char *ifname =OVPN_DEFAULT_IFNAME ; +enum ovpn_mode mode =OVPN_MODE_P2P ; +struct net_device *dev; +struct sk_buff *msg; +void *hdr; + +if (info->attrs[OVPN_A_IFNAME ]) + ifname =nla_data (info->attrs[OVPN_A_IFNAME ]);
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 99
Quoted 10 lines
drivers/net/ovpn/netlink.c +10
-0+ +if (info->attrs[OVPN_A_MODE ]) { + mode =nla_get_u32 (info->attrs[OVPN_A_MODE ]); +pr_debug ("ovpn: setting device (%s) mode: %u\n" , ifname, mode); + } + + dev =ovpn_iface_create (ifname, mode,genl_info_net (info)); +if (IS_ERR (dev)) { +pr_err ("ovpn: error while creating interface %s: %ld\n" , ifname, +PTR_ERR (dev));
Better to send the error to the caller with NL_SET_ERR_MSG_MOD()
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 108
Quoted 9 lines
drivers/net/ovpn/netlink.c +9
-0+return PTR_ERR (dev); + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put (msg, info->snd_portid, info->snd_seq, &ovpn_nl_family, +0 ,OVPN_CMD_NEW_IFACE );
genlmsg_iput() will save you a lot of typing
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 114
Quoted 6 lines
drivers/net/ovpn/netlink.c +6
-0+if (!hdr) { +netdev_err (dev,"%s: cannot create message header\n" , __func__); +return -EMSGSIZE ; + } + +if (nla_put (msg,OVPN_A_IFNAME ,strlen (dev->name) +1 , dev->name)) {
nla_put_string() ?
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 115
Quoted 1 line
drivers/net/ovpn/netlink.c +1
-0+netdev_err (dev,"%s: cannot add ifname to reply\n" , __func__);
Probably not worth it, can't happen given the message size
Quoted 11 lines
drivers/net/ovpn/netlink.c +8
-0+genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }int ovpn_nl_del_iface_doit (struct sk_buff *skb,struct genl_info *info)
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 135
Quoted 10 lines
drivers/net/ovpn/netlink.c +8
-1{ -return -ENOTSUPP ; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; + +rtnl_lock (); +ovpn_iface_destruct (ovpn); +dev_put (ovpn->dev); +rtnl_unlock (); + +synchronize_net ();
Why? 🤔️
Antonio Quartulli
maintainerRe: [PATCH net-next v3 05/24] ovpn: implement interface creation/destruction via netlink
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · On Mon, 6 May 2024 03:16:18 +0200 Antonio Quartulli wrote:
int ovpn_nl_new_iface_doit(struct sk_buff *skb, struct genl_info *info) {drivers/net/ovpn/netlink.c +12
-1-return -ENOTSUPP ; +const char *ifname =OVPN_DEFAULT_IFNAME ; +enum ovpn_mode mode =OVPN_MODE_P2P ; +struct net_device *dev; +struct sk_buff *msg; +void *hdr; + +if (info->attrs[OVPN_A_IFNAME ]) + ifname =nla_data (info->attrs[OVPN_A_IFNAME ]); + +if (info->attrs[OVPN_A_MODE ]) { + mode =nla_get_u32 (info->attrs[OVPN_A_MODE ]); +pr_debug ("ovpn: setting device (%s) mode: %u\n" , ifname, mode);
Quoted 8 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 99
Quoted 6 lines
drivers/net/ovpn/netlink.c +6
-0+ } + + dev =ovpn_iface_create (ifname, mode,genl_info_net (info)); +if (IS_ERR (dev)) { +pr_err ("ovpn: error while creating interface %s: %ld\n" , ifname, +PTR_ERR (dev));Better to send the error to the caller with
NL_SET_ERR_MSG_MOD()
yeah, makes sense. I guess I can do the same for every other error generated in any netlink handler.
Quoted 12 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 108
Quoted 10 lines
drivers/net/ovpn/netlink.c +9
-0+return PTR_ERR (dev); + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put (msg, info->snd_portid, info->snd_seq, &ovpn_nl_family, +0 ,OVPN_CMD_NEW_IFACE );
genlmsg_iput()will save you a lot of typing
oh, wow, nice one :) will switch to iput()
Quoted 10 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 114
Quoted 7 lines
drivers/net/ovpn/netlink.c +6
-0+if (!hdr) { +netdev_err (dev,"%s: cannot create message header\n" , __func__); +return -EMSGSIZE ; + } + +if (nla_put (msg,OVPN_A_IFNAME ,strlen (dev->name) +1 , dev->name)) {
nla_put_string()?
right.
Quoted 3 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 115
Quoted 1 line
drivers/net/ovpn/netlink.c +1
-0+netdev_err (dev,"%s: cannot add ifname to reply\n" , __func__);Probably not worth it, can't happen given the message size
Personally I still prefer to check the return value of functions that may fail, because somebody may break the assumption (i.e. message large enough by design) without realizing that this call was relying on that.
If you want, I could still add a comment saying that we don't expect this to happen.
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/netlink.c +8
-0+genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }
Quoted 13 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 135
Quoted 11 lines
int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) {drivers/net/ovpn/netlink.c +8
-1-return -ENOTSUPP ; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; + +rtnl_lock (); +ovpn_iface_destruct (ovpn); +dev_put (ovpn->dev); +rtnl_unlock (); + +synchronize_net ();Why? 🤔️
hmm I was under the impression that we should always call this function when destroying an interface to make sure that packets that already entered the network stack can be properly processed before the interface is gone for good.
Maybe this is not the right place? Any hint?
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Jakub Kicinski
maintainerRe: [PATCH net-next v3 05/24] ovpn: implement interface creation/destruction via netlink
Quoted 10 lines · On Wed, 8 May 2024 11:49:07 +0200 Antonio Quartulli wrote:
Quoted 3 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 115
Quoted 1 line
drivers/net/ovpn/netlink.c +1
-0+netdev_err (dev,"%s: cannot add ifname to reply\n" , __func__);Probably not worth it, can't happen given the message size
Personally I still prefer to check the return value of functions that may fail, because somebody may break the assumption (i.e. message large enough by design) without realizing that this call was relying on that.
If you want, I could still add a comment saying that we don't expect this to happen.
In a few other places we put a WARN_ON_ONCE() on messages size errors. That way syzbot usually catches the miscalculation rather quickly. But no strong objections if you prefer the print.
Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/netlink.c +8
-0+genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }Quoted 13 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 135
Quoted 11 lines
int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) {drivers/net/ovpn/netlink.c +8
-1-return -ENOTSUPP ; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; + +rtnl_lock (); +ovpn_iface_destruct (ovpn); +dev_put (ovpn->dev); +rtnl_unlock (); + +synchronize_net ();Why? 🤔️
Quoted 6 lines
hmm I was under the impression that we should always call this function when destroying an interface to make sure that packets that already entered the network stack can be properly processed before the interface is gone for good.
Maybe this is not the right place? Any hint?
The unregistration of the netdevice should take care of syncing packets in flight, AFAIU.
Antonio Quartulli
maintainerRe: [PATCH net-next v3 05/24] ovpn: implement interface creation/destruction via netlink
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Wed, 8 May 2024 11:49:07 +0200 Antonio Quartulli wrote:
Quoted 3 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 115
Quoted 1 line
drivers/net/ovpn/netlink.c +1
-0+netdev_err (dev,"%s: cannot add ifname to reply\n" , __func__);Probably not worth it, can't happen given the message size
Personally I still prefer to check the return value of functions that may fail, because somebody may break the assumption (i.e. message large enough by design) without realizing that this call was relying on that.
Quoted 6 lines
Quoted 2 lines
If you want, I could still add a comment saying that we don't expect this to happen.
In a few other places we put a
WARN_ON_ONCE()on messages size errors. That way syzbot usually catches the miscalculation rather quickly. But no strong objections if you prefer the print.
I am fine as long as we have some check.
If WARN_ON_ONCE() helps syzbot, then I'll go with it.
Quoted 31 lines
Quoted 31 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/netlink.c +8
-0+genlmsg_cancel (msg, hdr); +nlmsg_free (msg); +return -EMSGSIZE ; + } + +genlmsg_end (msg, hdr); + +return genlmsg_reply (msg, info); }Quoted 13 lines
drivers/net/ovpn/netlink.c void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb, · line 135
Quoted 11 lines
int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info) {drivers/net/ovpn/netlink.c +8
-1-return -ENOTSUPP ; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; + +rtnl_lock (); +ovpn_iface_destruct (ovpn); +dev_put (ovpn->dev); +rtnl_unlock (); + +synchronize_net ();Why? 🤔️
hmm I was under the impression that we should always call this function when destroying an interface to make sure that packets that already entered the network stack can be properly processed before the interface is gone for good.
Quoted 4 lines
Quoted 1 line
Maybe this is not the right place? Any hint?
The unregistration of the netdevice should take care of syncing packets in flight, AFAIU.
I have another call to synchronize_net() in ovpn_iface_destruct() after calling unregister_netdevice().
Sabrina was actually questioning that call too.
First of all I now realize that we are calling it twice, but from what I am understanding, I think we can just ditch any invocation and let core do the right thing.
I'll remove it and do some tests.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 6/24 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
@@ -51,6 +51,13 @@ static int ovpn_net_open(struct net_device *dev)IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; } +/* 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.43.2
Antonio Quartulli
maintainerPATCH v3 7/24 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 | 60 ++++++ drivers/net/ovpn/bind.h | 130 ++++++++++++ drivers/net/ovpn/io.c | 8 + drivers/net/ovpn/main.c | 10 + drivers/net/ovpn/main.h | 2 + drivers/net/ovpn/ovpnstruct.h | 7 + drivers/net/ovpn/peer.c | 379 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 152 ++++++++++++++ 9 files changed, 750 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 +60 -0
60 lines not on this page · raw message
drivers/net/ovpn/bind.h new file +130 -0
130 lines not on this page · raw message
drivers/net/ovpn/io.c +8 -0
@@ -13,6 +13,7 @@#include "io.h" #include "ovpnstruct.h" #include "netlink.h" +#include "peer.h" int ovpn_struct_init (struct net_device *dev) {
@@ -25,6 +26,13 @@ int ovpn_struct_init(struct net_device *dev)if (err <0 )return err; +spin_lock_init (&ovpn->lock); + + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM , +0 , dev->name); +if (!ovpn->events_wq) +return -ENOMEM ; + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);if (!dev->tstats)return -ENOMEM ;
drivers/net/ovpn/main.c +10 -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 <uapi/linux/if_arp.h>
@@ -21,6 +22,7 @@#include "netlink.h" #include "io.h" #include "packet.h" +#include "peer.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -37,6 +39,9 @@ static void ovpn_struct_free(struct net_device *net)rtnl_unlock ();free_percpu (net->tstats); +flush_workqueue (ovpn->events_wq); +destroy_workqueue (ovpn->events_wq); +rcu_barrier (); }static int ovpn_net_open (struct net_device *dev)
@@ -168,6 +173,9 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn) ovpn->registered = false; +if (ovpn->mode ==OVPN_MODE_P2P ) +ovpn_peer_release_p2p (ovpn); +unregister_netdevice (ovpn->dev);synchronize_net (); }
@@ -270,6 +278,8 @@ static __exit void ovpn_cleanup(void)ovpn_nl_unregister ();unregister_pernet_device (&ovpn_pernet_ops);unregister_netdevice_notifier (&ovpn_netdev_notifier); + +rcu_barrier (); }module_init (ovpn_init);
drivers/net/ovpn/main.h +2 -0
@@ -51,4 +51,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 +7 -0
@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @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 workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };
drivers/net/ovpn/peer.c new file +379 -0
379 lines not on this page · raw message
drivers/net/ovpn/peer.h new file +152 -0
152 lines not on this page · raw message
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 9 lines · Antonio Quartulli
An
ovpn_peerobject 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 theovpn_bindobject is introcued
^
typo: "introduced"
Quoted 16 lines
as the two are strictly related.
Anovpn_bindobject wraps a sockaddr representing the local coordinates being used to talk to a specific peer.drivers/net/ovpn/bind.c new file +7
-0+static void ovpn_bind_release_rcu (struct rcu_head *head) +{ + struct ovpn_bind *bind = container_of(head, struct ovpn_bind, rcu); + +kfree (bind); +} +
drivers/net/ovpn/bind.c line 59
Quoted 10 lines
drivers/net/ovpn/bind.c new file +10
-0+void ovpn_bind_reset (struct ovpn_peer *peer,struct ovpn_bind *new) +{ +struct ovpn_bind *old; + +spin_lock_bh (&peer->lock); + old =rcu_replace_pointer (peer->bind, new, true); +spin_unlock_bh (&peer->lock); + +if (old) +call_rcu (&old->rcu, ovpn_bind_release_rcu);
Isn't that just kfree_rcu? (note kfree_rcu doesn't need the NULL check)
Quoted 8 lines
drivers/net/ovpn/bind.c new file +1
-0+}drivers/net/ovpn/bind.h new file +0
-0
[...]
drivers/net/ovpn/bind.h line 79
Quoted 2 lines
drivers/net/ovpn/bind.h new file +2
-0+static inline bool ovpn_bind_skb_src_match (const struct ovpn_bind *bind, +struct sk_buff *skb)
nit: I think skb can also be const here
Quoted 13 lines
drivers/net/ovpn/io.c +1
-0@@ -13,6 +13,7 @@#include "io.h" #include "ovpnstruct.h" #include "netlink.h" +#include "peer.h" int ovpn_struct_init (struct net_device *dev) {
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 32
Quoted 7 lines
drivers/net/ovpn/io.c +4
-0@@ -25,6 +26,13 @@ int ovpn_struct_init(struct net_device *dev)if (err <0 )return err; +spin_lock_init (&ovpn->lock); + + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM , +0 , dev->name);
I'm not convinced this will get freed consistently if register_netdevice fails early (before ndo_init). After talking to Paolo, it seems this should be moved into a new ->ndo_init instead.
Quoted 11 lines
drivers/net/ovpn/io.c +3
-0+if (!ovpn->events_wq) +return -ENOMEM ; + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);if (!dev->tstats)return -ENOMEM ;drivers/net/ovpn/main.c +0
-0
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 43
Quoted 5 lines
drivers/net/ovpn/main.c +2
-0@@ -37,6 +39,9 @@ static void ovpn_struct_free(struct net_device *net)rtnl_unlock ();free_percpu (net->tstats); +flush_workqueue (ovpn->events_wq); +destroy_workqueue (ovpn->events_wq);
Is the flush needed? I'm not an expert on workqueues, but from a quick look at destroy_workqueue it calls drain_workqueue, which would take care of flushing the queue?
Quoted 3 lines
drivers/net/ovpn/main.c +1
-0+rcu_barrier (); }
[...]
Quoted 9 lines
drivers/net/ovpn/ovpnstruct.h +1
-0@@ -17,12 +17,19 @@* @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
drivers/net/ovpn/ovpnstruct.h line 30
Quoted 10 lines
drivers/net/ovpn/ovpnstruct.h +4
-0+* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @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 */
nit: the comment isn't really needed since you have kdoc saying the same thing
Quoted 10 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+struct workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c new file +0
-0
[...]
drivers/net/ovpn/peer.c line 95
Quoted 9 lines
drivers/net/ovpn/peer.c new file +9
-0+/** +* ovpn_peer_free - release private members and free peer object +* @peer: the peer to free +*/ +static void ovpn_peer_free (struct ovpn_peer *peer) +{ +ovpn_bind_reset (peer,NULL ); + +WARN_ON (!__ptr_ring_empty (&peer->tx_ring));
Could you pass a destructor to ptr_ring_cleanup instead of all these WARNs?
Quoted 12 lines
drivers/net/ovpn/peer.c new file +12
-0+ptr_ring_cleanup (&peer->tx_ring,NULL ); +WARN_ON (!__ptr_ring_empty (&peer->rx_ring)); +ptr_ring_cleanup (&peer->rx_ring,NULL ); +WARN_ON (!__ptr_ring_empty (&peer->netif_rx_ring)); +ptr_ring_cleanup (&peer->netif_rx_ring,NULL ); + +dst_cache_destroy (&peer->dst_cache); + +dev_put (peer->ovpn->dev); + +kfree (peer); +}
[...]
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +}
drivers/net/ovpn/peer.c line 133
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+ +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object +*/ +static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);
Does call_rcu really need to run in process context?
Quoted 1 line
+}
[...]
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 (struct ovpn_peer *peer, +struct sockaddr_storage *ss) +{
[...]
drivers/net/ovpn/peer.c line 209
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+case AF_INET6 : + sa6 = (struct sockaddr_in6 *)ss; +if (memcmp (&sa6->sin6_addr, &bind->sa.in6.sin6_addr, +sizeof (struct in6_addr )))
ipv6_addr_equal?
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+return false; +if (sa6->sin6_port != bind->sa.in6.sin6_port) +return false; +break ;
[...]
drivers/net/ovpn/peer.c line 292
Quoted 12 lines
drivers/net/ovpn/peer.c new file +12
-0+struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id) +{ +struct ovpn_peer *peer =NULL ; + +if (ovpn->mode ==OVPN_MODE_P2P ) + peer =ovpn_peer_get_by_id_p2p (ovpn, peer_id); + +return peer; +} + +/** +* ovpn_peer_add_p2p - add per to related tables in a P2P instance
^
typo: peer?
[...]
Quoted 7 lines
drivers/net/ovpn/peer.c new file +7
-0+/** +* ovpn_peer_del_p2p - delete peer from related tables in a P2P instance +* @peer: the peer to delete +* @reason: reason why the peer was deleted (sent to userspace) +* +* Return: 0 on success or a negative error code otherwise +*/
drivers/net/ovpn/peer.c line 344
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+static int ovpn_peer_del_p2p (struct ovpn_peer *peer, +enum ovpn_del_peer_reason reason) +{ +struct ovpn_peer *tmp; +int ret = -ENOENT ; + +spin_lock_bh (&peer->ovpn->lock); + tmp =rcu_dereference (peer->ovpn->peer); +if (tmp != peer) + goto unlock;
How do we recover if all those objects got out of sync? Are we stuck with a broken peer?
And if this happens during interface deletion, aren't we leaking the peer memory here?
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+ovpn_peer_put (tmp); + tmp->delete_reason = reason; +RCU_INIT_POINTER (peer->ovpn->peer,NULL ); + ret =0 ; + +unlock: +spin_unlock_bh (&peer->ovpn->lock); + +return ret; +}
[...]
Quoted 5 lines
drivers/net/ovpn/peer.h new file +0
-0
[...]
Quoted 23 lines
drivers/net/ovpn/peer.h new file +23
-0+/** +* struct ovpn_peer - the main remote peer object +* @ovpn: main openvpn instance this peer belongs to +* @id: unique identifier +* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel +* @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @tx_ring: queue of outgoing poackets to this peer +* @rx_ring: queue of incoming packets from this peer +* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI +* @dst_cache: cache for dst_entry used to send to peer +* @bind: remote peer binding +* @halt: true if ovpn_peer_mark_delete was called +* @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) +* @lock: protects binding to peer (bind) +* @refcount: reference counter +* @rcu: used to free peer in an RCU safe way +* @delete_work: deferred cleanup work, used to notify userspace +*/ +struct ovpn_peer { +struct ovpn_struct *ovpn; +u32 id; +struct { +struct in_addr ipv4;
drivers/net/ovpn/peer.h line 51
Quoted 10 lines
drivers/net/ovpn/peer.h new file +10
-0+struct in6_addr ipv6; + } vpn_addrs; +struct ptr_ring tx_ring; +struct ptr_ring rx_ring; +struct ptr_ring netif_rx_ring; +struct dst_cache dst_cache; +struct ovpn_bind __rcu *bind; +bool halt; +enum ovpn_del_peer_reason delete_reason; +spinlock_t lock;/* protects bind */
nit: the comment isn't really needed, it's redundant with kdoc.
Quoted 4 lines
drivers/net/ovpn/peer.h new file +4
-0+struct kref refcount; +struct rcu_head rcu; +struct work_struct delete_work; +};
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 4 lines · Sabrina Dubroca
Quoted 3 lines · Antonio Quartulli
An
ovpn_peerobject holds the whole status of a remote peer (regardless whether it is a server or a client).
Quoted 8 lines
Quoted 6 lines
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 theovpn_bindobject is introcued^typo: "introduced"
thanks
Quoted 21 lines
Quoted 21 lines
as the two are strictly related.
Anovpn_bindobject wraps a sockaddr representing the local coordinates being used to talk to a specific peer.drivers/net/ovpn/bind.c new file +11
-0+static void ovpn_bind_release_rcu (struct rcu_head *head) +{ + struct ovpn_bind *bind = container_of(head, struct ovpn_bind, rcu); + +kfree (bind); +} + +void ovpn_bind_reset (struct ovpn_peer *peer,struct ovpn_bind *new) +{ +struct ovpn_bind *old; +
Quoted 8 lines
drivers/net/ovpn/bind.c line 59
Quoted 6 lines
drivers/net/ovpn/bind.c new file +6
-0+spin_lock_bh (&peer->lock); + old =rcu_replace_pointer (peer->bind, new, true); +spin_unlock_bh (&peer->lock); + +if (old) +call_rcu (&old->rcu, ovpn_bind_release_rcu);Isn't that just
kfree_rcu? (notekfree_rcudoesn't need the NULL check)
yeah, you're right. I think ovpn_bind_release_rcu() was more complex in the past, but got reduced step by step...will directly use kfree_rcu().
Quoted 10 lines
Quoted 9 lines
drivers/net/ovpn/bind.c new file +1
-0+}drivers/net/ovpn/bind.h new file +0
-0[...]
Quoted 4 lines
drivers/net/ovpn/bind.h line 79
Quoted 2 lines
drivers/net/ovpn/bind.h new file +2
-0+static inline bool ovpn_bind_skb_src_match (const struct ovpn_bind *bind, +struct sk_buff *skb)nit: I think skb can also be const here
right
Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/io.c +1
-0@@ -13,6 +13,7 @@#include "io.h" #include "ovpnstruct.h" #include "netlink.h" +#include "peer.h" int ovpn_struct_init (struct net_device *dev) {@@ -25,6 +26,13 @@ int ovpn_struct_init(struct net_device *dev)if (err <0 )return err;
Quoted 8 lines
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 32
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+spin_lock_init (&ovpn->lock); + + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM , +0 , dev->name);I'm not convinced this will get freed consistently if
register_netdevicefails early (beforendo_init). After talking to Paolo, it seems this should be moved into a new ->ndo_init instead.
oh good point. I didn't consider that register_netdevice could fail that early.
Quoted 14 lines
Quoted 14 lines
drivers/net/ovpn/io.c +3
-0+if (!ovpn->events_wq) +return -ENOMEM ; + dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);if (!dev->tstats)return -ENOMEM ;drivers/net/ovpn/main.c +0
-0@@ -37,6 +39,9 @@ static void ovpn_struct_free(struct net_device *net)rtnl_unlock ();
Quoted 7 lines
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 43
Quoted 3 lines
drivers/net/ovpn/main.c +2
-0free_percpu (net->tstats); +flush_workqueue (ovpn->events_wq); +destroy_workqueue (ovpn->events_wq);Is the flush needed? I'm not an expert on workqueues, but from a quick look at
destroy_workqueueit callsdrain_workqueue, which would take care of flushing the queue?
you're right. drain_workqueue calls __flush_workqueue as often as needed to empty the queue. Therefore I can get rid of my flush_worqueue invocation.
Quoted 19 lines
Quoted 4 lines
drivers/net/ovpn/main.c +1
-0+rcu_barrier (); }[...]
Quoted 13 lines
drivers/net/ovpn/ovpnstruct.h +4
-0@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list
Quoted 8 lines
drivers/net/ovpn/ovpnstruct.h line 30
Quoted 6 lines
drivers/net/ovpn/ovpnstruct.h +1
-0*/ struct ovpn_struct {struct net_device *dev;bool registered;enum ovpn_mode mode; +spinlock_t lock;/* protect writing to the ovpn_struct object */ nit: the comment isn't really needed since you have kdoc saying the same thing
True, but checkpatch.pl (or some other script?) was still throwing a warning, therefore I added this comment to silence it.
Quoted 12 lines
Quoted 11 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+struct workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c new file +0
-0[...]
Quoted 11 lines
drivers/net/ovpn/peer.c line 95
Quoted 9 lines
drivers/net/ovpn/peer.c new file +9
-0+/** +* ovpn_peer_free - release private members and free peer object +* @peer: the peer to free +*/ +static void ovpn_peer_free (struct ovpn_peer *peer) +{ +ovpn_bind_reset (peer,NULL ); + +WARN_ON (!__ptr_ring_empty (&peer->tx_ring));Could you pass a destructor to
ptr_ring_cleanupinstead of all these WARNs?
hmm but if we remove the WARNs then we lose the possibility to catch potential bugs, no? rings should definitely be empty at this point.
Or you think I should just not care and free any potentially remaining item?
Quoted 15 lines
Quoted 13 lines
drivers/net/ovpn/peer.c new file +12
-0+ptr_ring_cleanup (&peer->tx_ring,NULL ); +WARN_ON (!__ptr_ring_empty (&peer->rx_ring)); +ptr_ring_cleanup (&peer->rx_ring,NULL ); +WARN_ON (!__ptr_ring_empty (&peer->netif_rx_ring)); +ptr_ring_cleanup (&peer->netif_rx_ring,NULL ); + +dst_cache_destroy (&peer->dst_cache); + +dev_put (peer->ovpn->dev); + +kfree (peer); +}[...]
Quoted 16 lines
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +}drivers/net/ovpn/peer.c line 133
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+ +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object +*/ +static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);Does
call_rcureally need to run in process context?
Reason for switching to process context is that we have to invoke ovpn_nl_notify_del_peer (that sends a netlink event to userspace) and the latter requires a reference to the peer.
For this reason I thought it would be safe to have ovpn_nl_notify_del_peer and call_rcu invoked by the same context.
If I invoke call_rcu in ovpn_peer_release_kref, how can I be sure that the peer hasn't been free'd already when ovpn_nl_notify_del_peer is executed?
Quoted 15 lines
Quoted 2 lines
+}
[...]
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 (struct ovpn_peer *peer, +struct sockaddr_storage *ss) +{[...]
Quoted 7 lines
drivers/net/ovpn/peer.c line 209
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+case AF_INET6 : + sa6 = (struct sockaddr_in6 *)ss; +if (memcmp (&sa6->sin6_addr, &bind->sa.in6.sin6_addr, +sizeof (struct in6_addr )))
ipv6_addr_equal?
definitely. thanks
Quoted 19 lines
Quoted 4 lines
drivers/net/ovpn/peer.c new file +4
-0+return false; +if (sa6->sin6_port != bind->sa.in6.sin6_port) +return false; +break ;[...]
drivers/net/ovpn/peer.c line 292
Quoted 12 lines
drivers/net/ovpn/peer.c new file +12
-0+struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id) +{ +struct ovpn_peer *peer =NULL ; + +if (ovpn->mode ==OVPN_MODE_P2P ) + peer =ovpn_peer_get_by_id_p2p (ovpn, peer_id); + +return peer; +} + +/** +* ovpn_peer_add_p2p - add per to related tables in a P2P instance ^
Quoted 1 line
typo: peer?
yeah
Quoted 15 lines
[...]
Quoted 12 lines
drivers/net/ovpn/peer.c new file +12
-0+/** +* ovpn_peer_del_p2p - delete peer from related tables in a P2P instance +* @peer: the peer to delete +* @reason: reason why the peer was deleted (sent to userspace) +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_peer_del_p2p (struct ovpn_peer *peer, +enum ovpn_del_peer_reason reason) +{ +struct ovpn_peer *tmp; +int ret = -ENOENT ;
Quoted 8 lines
drivers/net/ovpn/peer.c line 344
Quoted 5 lines
drivers/net/ovpn/peer.c new file +5
-0+ +spin_lock_bh (&peer->ovpn->lock); + tmp =rcu_dereference (peer->ovpn->peer); +if (tmp != peer) + goto unlock;How do we recover if all those objects got out of sync? Are we stuck with a broken peer?
mhhh I don't fully get the scenario you are depicting.
In P2P mode there is only peer stored (reference is saved in ovpn->peer)
When we want to get rid of it, we invoke ovpn_peer_del_p2p().
The check we are performing here is just about being sure that we are removing the exact peer we requested to remove (and not some other peer that was still floating around for some reason).
Quoted 3 lines
And if this happens during interface deletion, aren't we leaking the peer memory here?
at interface deletion we call
ovpn_iface_destruct -> ovpn_peer_release_p2p -> ovpn_peer_del_p2p(ovpn->peer)
so at the last step we just ask to remove the very same peer that is curently stored, which should just never fail.
makes sense?
Quoted 46 lines
Quoted 11 lines
drivers/net/ovpn/peer.c new file +10
-0+ovpn_peer_put (tmp); + tmp->delete_reason = reason; +RCU_INIT_POINTER (peer->ovpn->peer,NULL ); + ret =0 ; + +unlock: +spin_unlock_bh (&peer->ovpn->lock); + +return ret; +}[...]
Quoted 5 lines
drivers/net/ovpn/peer.h new file +0
-0[...]
Quoted 27 lines
drivers/net/ovpn/peer.h new file +27
-0+/** +* struct ovpn_peer - the main remote peer object +* @ovpn: main openvpn instance this peer belongs to +* @id: unique identifier +* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel +* @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @tx_ring: queue of outgoing poackets to this peer +* @rx_ring: queue of incoming packets from this peer +* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI +* @dst_cache: cache for dst_entry used to send to peer +* @bind: remote peer binding +* @halt: true if ovpn_peer_mark_delete was called +* @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) +* @lock: protects binding to peer (bind) +* @refcount: reference counter +* @rcu: used to free peer in an RCU safe way +* @delete_work: deferred cleanup work, used to notify userspace +*/ +struct ovpn_peer { +struct ovpn_struct *ovpn; +u32 id; +struct { +struct in_addr ipv4; +struct in6_addr ipv6; + } vpn_addrs; +struct ptr_ring tx_ring; +struct ptr_ring rx_ring;
Quoted 8 lines
drivers/net/ovpn/peer.h line 51
Quoted 6 lines
drivers/net/ovpn/peer.h new file +6
-0+struct ptr_ring netif_rx_ring; +struct dst_cache dst_cache; +struct ovpn_bind __rcu *bind; +bool halt; +enum ovpn_del_peer_reason delete_reason; +spinlock_t lock;/* protects bind */ nit: the comment isn't really needed, it's redundant with kdoc.
like before, also here I had a warning which I wanted to silence.
Quoted 6 lines
drivers/net/ovpn/peer.h line 55
Quoted 5 lines
drivers/net/ovpn/peer.h new file +4
-0+struct kref refcount; +struct rcu_head rcu; +struct work_struct delete_work; +};
Thanks a lot!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 24 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/ovpnstruct.h +4
-0@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list Quoted 8 lines
drivers/net/ovpn/ovpnstruct.h line 30
Quoted 6 lines
drivers/net/ovpn/ovpnstruct.h +1
-0*/ struct ovpn_struct {struct net_device *dev;bool registered;enum ovpn_mode mode; +spinlock_t lock;/* protect writing to the ovpn_struct object */ nit: the comment isn't really needed since you have kdoc saying the same thing
Quoted 2 lines
True, but checkpatch.pl (or some other script?) was still throwing a warning, therefore I added this comment to silence it.
Ok, then I guess the comment (and the other one below) can stay. That sounds like a checkpatch.pl bug.
Quoted 22 lines
Quoted 10 lines
Quoted 9 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+struct workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c new file +0
-0[...]
Quoted 11 lines
drivers/net/ovpn/peer.c line 95
Quoted 9 lines
drivers/net/ovpn/peer.c new file +9
-0+/** +* ovpn_peer_free - release private members and free peer object +* @peer: the peer to free +*/ +static void ovpn_peer_free (struct ovpn_peer *peer) +{ +ovpn_bind_reset (peer,NULL ); + +WARN_ON (!__ptr_ring_empty (&peer->tx_ring));Could you pass a destructor to
ptr_ring_cleanupinstead of all these WARNs?
Quoted 2 lines
hmm but if we remove the WARNs then we lose the possibility to catch potential bugs, no? rings should definitely be empty at this point.
Ok, I haven't looked deep enough into how all the parts interact to understand that. The refcount bump around the tx_ring loop in ovpn_encrypt_work() takes care of that? Maybe worth a comment "$RING should be empty at this point because of XYZ" (for each of the rings).
Quoted 1 line
Or you think I should just not care and free any potentially remaining item?
Whether you WARN or not, any remaining item is going to be leaked. I'd go with WARN (or maybe DEBUG_NET_WARN_ON_ONCE) and free remaining items. It should never happen but seems easy to deal with, so why not handle it?
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/net/ovpn/peer.c new file +8
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +} + +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object Quoted 8 lines
drivers/net/ovpn/peer.c line 133
Quoted 6 lines
+ */
drivers/net/ovpn/peer.c new file +5
-0+static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);Does
call_rcureally need to run in process context?
Quoted 3 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the latter requires a reference to the peer.
I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires ovpn_nl_notify_del_peer to to run from process context.
Quoted 5 lines
For this reason I thought it would be safe to have
ovpn_nl_notify_del_peerandcall_rcuinvoked by the same context.If I invoke
call_rcuinovpn_peer_release_kref, how can I be sure that the peer hasn't been free'd already whenovpn_nl_notify_del_peeris executed?
Put the ovpn_nl_notify_del_peer call before the call_rcu, it will access the peer and then once that's done call_rcu will do its job?
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/ovpn/peer.c new file +12
-0+/** +* ovpn_peer_del_p2p - delete peer from related tables in a P2P instance +* @peer: the peer to delete +* @reason: reason why the peer was deleted (sent to userspace) +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_peer_del_p2p (struct ovpn_peer *peer, +enum ovpn_del_peer_reason reason) +{ +struct ovpn_peer *tmp; +int ret = -ENOENT ;Quoted 8 lines
drivers/net/ovpn/peer.c line 344
Quoted 5 lines
drivers/net/ovpn/peer.c new file +5
-0+ +spin_lock_bh (&peer->ovpn->lock); + tmp =rcu_dereference (peer->ovpn->peer); +if (tmp != peer) + goto unlock;How do we recover if all those objects got out of sync? Are we stuck with a broken peer?
Quoted 8 lines
mhhh I don't fully get the scenario you are depicting.
In P2P mode there is only peer stored (reference is saved in
ovpn->peer)When we want to get rid of it, we invoke
ovpn_peer_del_p2p(). The check we are performing here is just about being sure that we are removing the exact peer we requested to remove (and not some other peer that was still floating around for some reason).
But it's the right peer because it's the one the caller decided to get rid of. How about DEBUG_NET_WARN_ON_ONCE(tmp != peer) and always releasing the peer?
Quoted 10 lines
Quoted 2 lines
And if this happens during interface deletion, aren't we leaking the peer memory here?
at interface deletion we call
ovpn_iface_destruct->ovpn_peer_release_p2p->ovpn_peer_del_p2p(ovpn->peer)so at the last step we just ask to remove the very same peer that is curently stored, which should just never fail.
But that's not what the test checks for. If ovpn->peer->ovpn != ovpn, the test in ovpn_peer_del_p2p will fail. That's "objects getting out of sync" in my previous email. The peer has a bogus back reference to its ovpn parent, but it's ovpn->peer nevertheless.
Signature · 1 line
Sabrina
Re: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 25 lines · Sabrina Dubroca
Quoted 24 lines · Antonio Quartulli
Quoted 22 lines · Sabrina Dubroca
Quoted 9 lines · Antonio Quartulli
drivers/net/ovpn/ovpnstruct.h +1
-0@@ -17,12 +17,19 @@* @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 drivers/net/ovpn/ovpnstruct.h line 30
Quoted 10 lines
drivers/net/ovpn/ovpnstruct.h +4
-0+* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @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 */ nit: the comment isn't really needed since you have kdoc saying the same thing
Quoted 5 lines
Quoted 2 lines
True, but checkpatch.pl (or some other script?) was still throwing a warning, therefore I added this comment to silence it.
Ok, then I guess the comment (and the other one below) can stay. That sounds like a checkpatch.pl bug.
I suspect it is more complex than that. checkpatch does not understand kdoc. It just knows the rule that there should be a comment next to a lock, hopefully indicating what the lock protects. In order to fix this, checkpatch would need to somehow invoke the kdoc parser, and ask it if the lock has kdoc documentation.
I suspect we are just going to have to live with this.
Andrew
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 32 lines · Andrew Lunn
Quoted 25 lines · Sabrina Dubroca
Quoted 24 lines · Antonio Quartulli
Quoted 22 lines · Sabrina Dubroca
Quoted 9 lines · Antonio Quartulli
drivers/net/ovpn/ovpnstruct.h +1
-0@@ -17,12 +17,19 @@* @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 drivers/net/ovpn/ovpnstruct.h line 30
Quoted 10 lines
drivers/net/ovpn/ovpnstruct.h +4
-0+* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context +* @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 */ nit: the comment isn't really needed since you have kdoc saying the same thing
Quoted 5 lines
Quoted 2 lines
True, but checkpatch.pl (or some other script?) was still throwing a warning, therefore I added this comment to silence it.
Ok, then I guess the comment (and the other one below) can stay. That sounds like a checkpatch.pl bug.
Quoted 7 lines
I suspect it is more complex than that. checkpatch does not understand kdoc. It just knows the rule that there should be a comment next to a lock, hopefully indicating what the lock protects. In order to fix this, checkpatch would need to somehow invoke the kdoc parser, and ask it if the lock has kdoc documentation.
I suspect we are just going to have to live with this.
since we are now requiring new code to always have kdoc, can't we just drop the checkpatch warning?
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Jakub Kicinski
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 10 lines · On Fri, 10 May 2024 20:57:33 +0200 Antonio Quartulli wrote:
Quoted 7 lines
I suspect it is more complex than that. checkpatch does not understand kdoc. It just knows the rule that there should be a comment next to a lock, hopefully indicating what the lock protects. In order to fix this, checkpatch would need to somehow invoke the kdoc parser, and ask it if the lock has kdoc documentation.
I suspect we are just going to have to live with this.
since we are now requiring new code to always have kdoc, can't we just drop the checkpatch warning?
I don't think we require kdoc, but I agree that the warning is rather ineffective.
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
In reply to Sabrina DubrocaOn 09/05/2024 15:04, Sabrina Dubroca wrote:
[..]
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 9 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+struct workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c new file +0
-0[...]
drivers/net/ovpn/peer.c line 95
Quoted 9 lines
drivers/net/ovpn/peer.c new file +9
-0+/** +* ovpn_peer_free - release private members and free peer object +* @peer: the peer to free +*/ +static void ovpn_peer_free (struct ovpn_peer *peer) +{ +ovpn_bind_reset (peer,NULL ); + +WARN_ON (!__ptr_ring_empty (&peer->tx_ring));Could you pass a destructor to
ptr_ring_cleanupinstead of all these WARNs?
Quoted 7 lines
Quoted 2 lines
hmm but if we remove the WARNs then we lose the possibility to catch potential bugs, no? rings should definitely be empty at this point.
Ok, I haven't looked deep enough into how all the parts interact to understand that. The refcount bump around the
tx_ringloop inovpn_encrypt_work()takes care of that? Maybe worth a comment "$RING should be empty at this point because of XYZ" (for each of the rings).
Yeah, all piped skbs will be processed before exiting. Ok, will add a comment.
Quoted 7 lines
Quoted 2 lines
Or you think I should just not care and free any potentially remaining item?
Whether you WARN or not, any remaining item is going to be leaked. I'd go with WARN (or maybe
DEBUG_NET_WARN_ON_ONCE) and free remaining items. It should never happen but seems easy to deal with, so why not handle it?
Sure, passing consume_skb as destructor to ptr_ring_cleanup should be enough.
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 5 lines
drivers/net/ovpn/peer.c new file +4
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +}drivers/net/ovpn/peer.c line 133
Quoted 10 lines
drivers/net/ovpn/peer.c new file +10
-0+ +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object +*/ +static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);Does
call_rcureally need to run in process context?
Quoted 7 lines
Quoted 3 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the latter requires a reference to the peer.I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
ovpn_nl_notify_del_peer sends a netlink message to userspace and I was under the impression that it may block/sleep, no? For this reason I assumed it must be executed in process context.
Quoted 9 lines
Quoted 6 lines
For this reason I thought it would be safe to have
ovpn_nl_notify_del_peerandcall_rcuinvoked by the same context.If I invoke
call_rcuinovpn_peer_release_kref, how can I be sure that the peer hasn't been free'd already whenovpn_nl_notify_del_peeris executed?Put the
ovpn_nl_notify_del_peercall before thecall_rcu, it will access the peer and then once that's donecall_rcuwill do its job?
If ovpn_nl_notify_del_peer is allowed to run out of process context, then I totally agree.
Will test again.
Quoted 27 lines
Quoted 27 lines
Quoted 14 lines
Quoted 14 lines
drivers/net/ovpn/peer.c new file +12
-0+/** +* ovpn_peer_del_p2p - delete peer from related tables in a P2P instance +* @peer: the peer to delete +* @reason: reason why the peer was deleted (sent to userspace) +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_peer_del_p2p (struct ovpn_peer *peer, +enum ovpn_del_peer_reason reason) +{ +struct ovpn_peer *tmp; +int ret = -ENOENT ;Quoted 8 lines
drivers/net/ovpn/peer.c line 344
Quoted 5 lines
drivers/net/ovpn/peer.c new file +5
-0+ +spin_lock_bh (&peer->ovpn->lock); + tmp =rcu_dereference (peer->ovpn->peer); +if (tmp != peer) + goto unlock;How do we recover if all those objects got out of sync? Are we stuck with a broken peer?
mhhh I don't fully get the scenario you are depicting.
In P2P mode there is only peer stored (reference is saved in
ovpn->peer)
Quoted 8 lines
Quoted 4 lines
When we want to get rid of it, we invoke
ovpn_peer_del_p2p(). The check we are performing here is just about being sure that we are removing the exact peer we requested to remove (and not some other peer that was still floating around for some reason).But it's the right peer because it's the one the caller decided to get rid of. How about
DEBUG_NET_WARN_ON_ONCE(tmp != peer)and always releasing the peer?
sounds good. I should force myself to use more WARN_ON for conditions that are truly unexpected.
This said, I have a question regarding DEBUG_NET_WARN_ON_ONCE: it prints something only if CONFIG_DEBUG_NET is enabled. Is this the case on standard desktop/server distribution? Otherwise how are we going to get reports from users?
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
And if this happens during interface deletion, aren't we leaking the peer memory here?
at interface deletion we call
ovpn_iface_destruct->ovpn_peer_release_p2p->ovpn_peer_del_p2p(ovpn->peer)
Quoted 8 lines
Quoted 2 lines
so at the last step we just ask to remove the very same peer that is curently stored, which should just never fail.
But that's not what the test checks for. If
ovpn->peer->ovpn!= ovpn, the test inovpn_peer_del_p2pwill fail. That's "objects getting out of sync" in my previous email. The peer has a bogus back reference to its ovpn parent, but it'sovpn->peernevertheless.
Oh thanks for explaining that.
Ok, my assumption is that "ovpn->peer->ovpn != ovpn" can never be true.
Peers are created within the context of one ovpn object and are never exposed to other ovpns.
I hope it makes sense.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 9 lines
Quoted 4 lines
Whether you WARN or not, any remaining item is going to be leaked. I'd go with WARN (or maybe
DEBUG_NET_WARN_ON_ONCE) and free remaining items. It should never happen but seems easy to deal with, so why not handle it?This said, I have a question regarding
DEBUG_NET_WARN_ON_ONCE: it prints something only ifCONFIG_DEBUG_NETis enabled.
Is this the case on standard desktop/server distribution? Otherwise how are we going to get reports from users?
A bit tangential, but:
https://lwn.net/Articles/969923/
Andrew
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
In reply to Antonio QuartulliQuoted 22 lines · Antonio Quartulli
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
drivers/net/ovpn/peer.c new file +8
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +} + +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object Quoted 8 lines
drivers/net/ovpn/peer.c line 133
Quoted 6 lines
+ */
drivers/net/ovpn/peer.c new file +5
-0+static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);Does
call_rcureally need to run in process context?Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the latter requires a reference to the peer.
Quoted 7 lines
Quoted 3 lines
I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.
With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Quoted 4 lines
This said, I have a question regarding
DEBUG_NET_WARN_ON_ONCE: it prints something only ifCONFIG_DEBUG_NETis enabled.
Is this the case on standard desktop/server distribution? Otherwise how are we going to get reports from users?
That's pretty much why I'm suggesting to use it. For those things that should really never happen, I think letting developers find them during testing (or syzbot when it gets to your driver) is enough. I'm not convinced getting a stack trace from a user without any ability to reproduce is that useful.
But if you or someone else really want some WARN_ONs, I can live with that.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
And if this happens during interface deletion, aren't we leaking the peer memory here?
at interface deletion we call
ovpn_iface_destruct->ovpn_peer_release_p2p->ovpn_peer_del_p2p(ovpn->peer)Quoted 8 lines
Quoted 2 lines
so at the last step we just ask to remove the very same peer that is curently stored, which should just never fail.
But that's not what the test checks for. If
ovpn->peer->ovpn!= ovpn, the test inovpn_peer_del_p2pwill fail. That's "objects getting out of sync" in my previous email. The peer has a bogus back reference to its ovpn parent, but it'sovpn->peernevertheless.
Quoted 8 lines
Oh thanks for explaining that.
Ok, my assumption is that "
ovpn->peer->ovpn!= ovpn" can never be true.Peers are created within the context of one ovpn object and are never exposed to other ovpns.
I hope it makes sense.
Ok, so this would indicate that something has gone badly wrong. Is it really worth checking for that (or maybe just during development)?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 27 lines · Sabrina Dubroca
Quoted 26 lines · Antonio Quartulli
Quoted 24 lines · Sabrina Dubroca
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/net/ovpn/peer.c new file +8
-0+void ovpn_peer_release (struct ovpn_peer *peer) +{ +call_rcu (&peer->rcu, ovpn_peer_release_rcu); +} + +/** +* ovpn_peer_delete_work - work scheduled to release peer in process context +* @work: the work object Quoted 8 lines
drivers/net/ovpn/peer.c line 133
Quoted 6 lines
+ */
drivers/net/ovpn/peer.c new file +5
-0+static void ovpn_peer_delete_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); +ovpn_peer_release (peer);Does
call_rcureally need to run in process context?Quoted 3 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the latter requires a reference to the peer.I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
Quoted 7 lines
Quoted 3 lines
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Oh ok, I thought the send could be a problem too.
Will test with GFP_ATOMIC then. Thanks for the hint.
Quoted 8 lines
Quoted 7 lines
This said, I have a question regarding
DEBUG_NET_WARN_ON_ONCE: it prints something only ifCONFIG_DEBUG_NETis enabled.
Is this the case on standard desktop/server distribution? Otherwise how are we going to get reports from users?
Quoted 8 lines
That's pretty much why I'm suggesting to use it. For those things that should really never happen, I think letting developers find them during testing (or syzbot when it gets to your driver) is enough. I'm not convinced getting a stack trace from a user without any ability to reproduce is that useful.
But if you or someone else really want some
WARN_ONs, I can live with that.
I would personally prefer to keep the WARN_ON.
Since these bogus conditions may have consequences, users will open report in any case. Having some extra text that they can post for us to contextualize the issue may be useful.
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
And if this happens during interface deletion, aren't we leaking the peer memory here?
at interface deletion we call
ovpn_iface_destruct->ovpn_peer_release_p2p->ovpn_peer_del_p2p(ovpn->peer)Quoted 8 lines
Quoted 2 lines
so at the last step we just ask to remove the very same peer that is curently stored, which should just never fail.
But that's not what the test checks for. If
ovpn->peer->ovpn!= ovpn, the test inovpn_peer_del_p2pwill fail. That's "objects getting out of sync" in my previous email. The peer has a bogus back reference to its ovpn parent, but it'sovpn->peernevertheless.Oh thanks for explaining that.
Ok, my assumption is that "
ovpn->peer->ovpn!= ovpn" can never be true.
Quoted 8 lines
Quoted 4 lines
Peers are created within the context of one ovpn object and are never exposed to other ovpns.
I hope it makes sense.
Ok, so this would indicate that something has gone badly wrong. Is it really worth checking for that (or maybe just during development)?
A peer is created in ovpn_nl_set_peer_doit(), where the ovpn object is used to first assign peer->ovpn and then to store the peer in its own members. This all happens in one call and the value of ovpn can't be switched.
Anyway, bugs hide where we are most confident that things cannot go wrong :) So I'll still add a WARN_ON, just in case.
Thanks
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 33 lines · Antonio Quartulli
Quoted 32 lines · Sabrina Dubroca
Quoted 30 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 18 lines
Quoted 17 lines
Quoted 10 lines
(quoted diff) +7
-0+void ovpn_peer_release(struct ovpn_peer *peer) +{ + call_rcu(&peer->rcu, ovpn_peer_release_rcu); +} + +/** + * ovpn_peer_delete_work - work scheduled to release peer inprocess context
+ * @work: the work object
+ */Review
Quoted 5 lines
(quoted diff) +5
-0+static void ovpn_peer_delete_work(struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); + ovpn_peer_release(peer);Does
call_rcureally need to run in process context?Quoted 7 lines
Quoted 3 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the latter requires a reference to the peer.I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.
Quoted 7 lines
Quoted 3 lines
With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Oh ok, I thought the send could be a problem too.
Will test with
GFP_ATOMICthen. Thanks for the hint.
I am back and unfortunately we also have (added by a later patch):
294 napi_disable(&peer->napi);
295 netif_napi_del(&peer->napi);
that need to be executed in process context.
So it seems I must fire up the worker anyway..
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 42 lines · Antonio Quartulli
Quoted 39 lines · Antonio Quartulli
Quoted 37 lines · Sabrina Dubroca
Quoted 24 lines · Antonio Quartulli
Quoted 23 lines · Sabrina Dubroca
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +7
-0+void ovpn_peer_release(struct ovpn_peer *peer) +{ + call_rcu(&peer->rcu, ovpn_peer_release_rcu); +} + +/** + * ovpn_peer_delete_work - work scheduled torelease peer in process context
+ * @work: the work objectQuoted 8 lines
Review
Quoted 6 lines
+ */
(quoted diff) +5
-0+static void ovpn_peer_delete_work(struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); + ovpn_peer_release(peer);Does
call_rcureally need to run in process context?Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the
latter requires a reference to the peer.Quoted 8 lines
Quoted 3 lines
I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under
the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Quoted 8 lines
Quoted 3 lines
Oh ok, I thought the send could be a problem too.
Will test with
GFP_ATOMICthen. Thanks for the hint.I am back and unfortunately we also have (added by a later patch):
294
napi_disable(&peer->napi);
295netif_napi_del(&peer->napi);
Do you need the napi instance to be per peer, or can it be per netdevice? If it's per netdevice you can clean it up in ->priv_destructor.
Quoted 2 lines
that need to be executed in process context.
So it seems I must fire up the worker anyway..
I hope with can simplify all that logic. There's some complexity that's unavoidable in this kind of driver, but maybe not as much as you've got here.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 47 lines · Sabrina Dubroca
Quoted 46 lines · Antonio Quartulli
Quoted 42 lines · Antonio Quartulli
Quoted 29 lines · Sabrina Dubroca
Quoted 28 lines · Antonio Quartulli
Quoted 26 lines · Sabrina Dubroca
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +7
-0+void ovpn_peer_release(struct ovpn_peer *peer) +{ + call_rcu(&peer->rcu, ovpn_peer_release_rcu); +} + +/** + * ovpn_peer_delete_work - work scheduled torelease peer in process context
+ * @work: the work objectQuoted 8 lines
Review
Quoted 6 lines
+ */
(quoted diff) +5
-0+static void ovpn_peer_delete_work(struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); + ovpn_peer_release(peer);Does
call_rcureally need to run in process context?Quoted 4 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the
latter requires a reference to the peer.I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.Quoted 8 lines
Quoted 4 lines
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under
the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Oh ok, I thought the send could be a problem too.
Will test with
GFP_ATOMICthen. Thanks for the hint.
Quoted 8 lines
Quoted 4 lines
I am back and unfortunately we also have (added by a later patch):
294 napi_disable(&peer->napi); 295 netif_napi_del(&peer->napi);Do you need the napi instance to be per peer, or can it be per netdevice? If it's per netdevice you can clean it up in ->priv_destructor.
In an ideal world, at some point I could leverage on multiple CPUs handling traffic from multiple peers, therefore every queue in the driver is per peer, NAPI included.
Does it make sense?
Now, whether this is truly feasible from the core perspective is something I don't know yet.
For sure, for the time being I could shrink everything to one queue.
There is one workqueue only encrypting/decrypting packets right now, therefore multiple NAPI queues are not truly useful at this time.
Quoted 8 lines
Quoted 3 lines
that need to be executed in process context.
So it seems I must fire up the worker anyway..I hope with can simplify all that logic. There's some complexity that's unavoidable in this kind of driver, but maybe not as much as you've got here.
I am all for simplification, and rest assured that the current version is already much simpler than what it originally was :-)
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 53 lines · Antonio Quartulli
Quoted 52 lines · Sabrina Dubroca
Quoted 50 lines · Antonio Quartulli
Quoted 35 lines · Antonio Quartulli
Quoted 34 lines · Sabrina Dubroca
Quoted 32 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 18 lines
Quoted 17 lines
Quoted 10 lines
(quoted diff) +7
-0+void ovpn_peer_release(struct ovpn_peer *peer) +{ + call_rcu(&peer->rcu, ovpn_peer_release_rcu); +} + +/** + * ovpn_peer_delete_work - work scheduled torelease peer in process context
+ * @work: the work object
+ */Review
Quoted 5 lines
(quoted diff) +5
-0+static void ovpn_peer_delete_work(struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + delete_work); + ovpn_peer_release(peer);Does
call_rcureally need to run in process context?Quoted 8 lines
Quoted 4 lines
Reason for switching to process context is that we have to invoke
ovpn_nl_notify_del_peer(that sends a netlink event to userspace) and the
latter requires a reference to the peer.I'm confused. When you say "requires a reference to the peer", do you mean accessing fields of the peer object? I don't see why this requires
ovpn_nl_notify_del_peerto to run from process context.
ovpn_nl_notify_del_peersends a netlink message to userspace and I was under
the impression that it may block/sleep, no?
For this reason I assumed it must be executed in process context.Quoted 7 lines
Quoted 3 lines
With s/GFP_KERNEL/GFP_ATOMIC/, it should be ok to run from whatever context. Firing up a workqueue just to send a 100B netlink message seems a bit overkill.
Oh ok, I thought the send could be a problem too.
Will test with
GFP_ATOMICthen. Thanks for the hint.I am back and unfortunately we also have (added by a later patch):
294 napi_disable(&peer->napi); 295 netif_napi_del(&peer->napi);
Quoted 7 lines
Quoted 3 lines
Do you need the napi instance to be per peer, or can it be per netdevice? If it's per netdevice you can clean it up in ->priv_destructor.
In an ideal world, at some point I could leverage on multiple CPUs handling traffic from multiple peers, therefore every queue in the driver is per peer, NAPI included.
But for that they could also be per-CPU at the device level, instead of being created at the peer level.
Quoted 4 lines
Does it make sense?
Now, whether this is truly feasible from the core perspective is something I don't know yet.
If those packets arrive on different CPUs/queues, probably.
Quoted 8 lines
For sure, for the time being I could shrink everything to one queue. There is one workqueue only encrypting/decrypting packets right now, therefore multiple NAPI queues are not truly useful at this time.
Quoted 4 lines
Quoted 3 lines
that need to be executed in process context.
So it seems I must fire up the worker anyway..
Quoted 7 lines
Quoted 4 lines
I hope with can simplify all that logic. There's some complexity that's unavoidable in this kind of driver, but maybe not as much as you've got here.
I am all for simplification, and rest assured that the current version is already much simpler than what it originally was :-)
Ouch :)
Signature · 1 line
Sabrina
Simon Horman
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
In reply to Antonio QuartulliQuoted 14 lines · Antonio Quartulli
An
ovpn_peerobject 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 theovpn_bindobject is introcued as the two are strictly related.
Anovpn_bindobject wraps a sockaddr representing the local coordinates being used to talk to a specific peer.Signed-off-by Antonio Quartulli <[email protected]>
...
drivers/net/ovpn/ovpnstruct.h line 22
Quoted 11 lines
drivers/net/ovpn/ovpnstruct.h +3
-0@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context
nit: events_wq
Quoted 13 lines
drivers/net/ovpn/ovpnstruct.h +4
-0+* @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 workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };
...
Antonio Quartulli
maintainerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 18 lines · Simon Horman
Quoted 14 lines · Antonio Quartulli
An
ovpn_peerobject 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 theovpn_bindobject is introcued as the two are strictly related.
Anovpn_bindobject wraps a sockaddr representing the local coordinates being used to talk to a specific peer.Signed-off-by Antonio Quartulli <[email protected]>...
Quoted 13 lines
drivers/net/ovpn/ovpnstruct.h line 22
Quoted 11 lines
drivers/net/ovpn/ovpnstruct.h +3
-0@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context nit:
events_wq
Thanks for the report. I fixed this locally already.
You don't know how long I had to stare at the kdoc warning and the code in order to realize that I missed a 's' :-S
Regards,
Quoted 16 lines
Quoted 14 lines
drivers/net/ovpn/ovpnstruct.h +4
-0+* @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 workqueue_struct *events_wq; +struct ovpn_peer __rcu *peer;struct list_head dev_list; };...
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Simon Horman
reviewerRe: [PATCH net-next v3 07/24] ovpn: introduce the ovpn_peer object
Quoted 33 lines · Antonio Quartulli
Quoted 18 lines · Simon Horman
Quoted 14 lines · Antonio Quartulli
An
ovpn_peerobject 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 theovpn_bindobject is introcued as the two are strictly related.
Anovpn_bindobject wraps a sockaddr representing the local coordinates being used to talk to a specific peer.Signed-off-by Antonio Quartulli <[email protected]>...
Quoted 13 lines
drivers/net/ovpn/ovpnstruct.h line 22
Quoted 11 lines
drivers/net/ovpn/ovpnstruct.h +3
-0@@ -17,12 +17,19 @@* @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 +* @event_wq: used to schedule generic events that may sleep and that need to be +* performed outside of softirq context nit:
events_wq
Quoted 4 lines
Thanks for the report. I fixed this locally already.
You don't know how long I had to stare at the kdoc warning and the code in order to realize that I missed a 's' :-S
It took me more than one reading too :)
Antonio Quartulli
maintainerPATCH v3 8/24 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 attathced to them for status tracking purposes.
Initially only UDP support is introduced. TCP will come in a later patch.
--- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/socket.c | 105 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.h | 68 ++++++++++++++++++++++++ drivers/net/ovpn/udp.c | 45 ++++++++++++++++ drivers/net/ovpn/udp.h | 27 ++++++++++ 5 files changed, 247 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 +105 -0
105 lines not on this page · raw message
drivers/net/ovpn/socket.h new file +68 -0
68 lines not on this page · raw message
drivers/net/ovpn/udp.c new file +45 -0
45 lines not on this page · raw message
drivers/net/ovpn/udp.h new file +27 -0
@@ -0,0 +1,27 @@ +/* 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 ; + +/** +* ovpn_udp_socket_attach - set udp-tunnel CBs on socket and link it to ovpn +* @sock: socket to configure +* @ovpn: the openvp instance to link +* +* After invoking this function, the sock will be controlled by ovpn so that +* any incoming packet may be processed by ovpn first. +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); + +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 08/24] ovpn: introduce the ovpn_socket object
Quoted 5 lines · Antonio Quartulli
This specific structure is used in the ovpn kernel module to wrap and carry around a standard kernel socket.
ovpn takes ownership of passed sockets and therefore an ovpn specific objects is attathced to them for status tracking
typos: object attached
Quoted 5 lines
drivers/net/ovpn/socket.c new file +0
-0
[...]
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */
kref_put seems to call ovpn_socket_release_kref without waiting, and then that calls ovpn_socket_detach immediately as well. Am I missing something?
Quoted 7 lines
drivers/net/ovpn/socket.c new file +7
-0+static void ovpn_socket_detach (struct socket *sock) +{ +if (!sock) +return ; + +sockfd_put (sock); +}
[...]
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */
Did that comment get misplaced? It doesn't match the code.
Quoted 18 lines
drivers/net/ovpn/socket.c new file +12
-0+static int ovpn_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +int ret = -EOPNOTSUPP ; + +if (!sock || !peer) +return -EINVAL ; + +if (sock->sk->sk_protocol ==IPPROTO_UDP ) + ret =ovpn_udp_socket_attach (sock, peer->ovpn); + +return ret; +}drivers/net/ovpn/udp.c new file +0
-0
[...]
drivers/net/ovpn/udp.c line 23
Quoted 8 lines
drivers/net/ovpn/udp.c new file +8
-0+ +int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn) +{ +struct ovpn_socket *old_data; + +/* sanity check */ +if (sock->sk->sk_protocol !=IPPROTO_UDP ) { +netdev_err (ovpn->dev,"%s: expected UDP socket\n" , __func__);
Maybe use DEBUG_NET_WARN_ON_ONCE here since it's never expected to actually happen? That would help track down (in test/debug setups) how we ended up here.
drivers/net/ovpn/udp.c line 32
Quoted 9 lines
drivers/net/ovpn/udp.c new file +9
-0+return -EINVAL ; + } + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock (); +if (old_data) { +if (old_data->ovpn == ovpn) {
You should stay under rcu_read_unlock if you access old_data's fields.
Quoted 13 lines
drivers/net/ovpn/udp.c new file +13
-0+netdev_dbg (ovpn->dev, +"%s: provided socket already owned by this interface\n" , + __func__); +return -EALREADY ; + } + +netdev_err (ovpn->dev,"%s: provided socket already taken by other user\n" , + __func__); +return -EBUSY ; + } + +return 0 ; +}
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 08/24] ovpn: introduce the ovpn_socket object
Quoted 8 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
This specific structure is used in the ovpn kernel module to wrap and carry around a standard kernel socket.
ovpn takes ownership of passed sockets and therefore an ovpn specific objects is attathced to them for status tracking
typos: object attached
thanks
Quoted 8 lines
Quoted 7 lines
drivers/net/ovpn/socket.c new file +0
-0[...]
Quoted 6 lines
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */
kref_putseems to callovpn_socket_release_krefwithout waiting, and then that callsovpn_socket_detachimmediately as well. Am I missing something?
hmm what do we need to wait for exactly? (Maybe I am missing something) The ovpn_socket will survive a bit longer thanks to kfree_rcu.
Quoted 10 lines
Quoted 8 lines
drivers/net/ovpn/socket.c new file +7
-0+static void ovpn_socket_detach (struct socket *sock) +{ +if (!sock) +return ; + +sockfd_put (sock); +}[...]
Quoted 4 lines
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */ Did that comment get misplaced? It doesn't match the code.
yeah it did. wiping it.
Quoted 20 lines
Quoted 19 lines
drivers/net/ovpn/socket.c new file +12
-0+static int ovpn_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +int ret = -EOPNOTSUPP ; + +if (!sock || !peer) +return -EINVAL ; + +if (sock->sk->sk_protocol ==IPPROTO_UDP ) + ret =ovpn_udp_socket_attach (sock, peer->ovpn); + +return ret; +}drivers/net/ovpn/udp.c new file +0
-0[...]
Quoted 12 lines
drivers/net/ovpn/udp.c line 23
Quoted 8 lines
drivers/net/ovpn/udp.c new file +8
-0+ +int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn) +{ +struct ovpn_socket *old_data; + +/* sanity check */ +if (sock->sk->sk_protocol !=IPPROTO_UDP ) { +netdev_err (ovpn->dev,"%s: expected UDP socket\n" , __func__);Maybe use
DEBUG_NET_WARN_ON_ONCEhere since it's never expected to actually happen? That would help track down (in test/debug setups) how we ended up here.
will do, thanks for the suggestion
Quoted 12 lines
drivers/net/ovpn/udp.c line 32
Quoted 10 lines
drivers/net/ovpn/udp.c new file +9
-0+return -EINVAL ; + } + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock (); +if (old_data) { +if (old_data->ovpn == ovpn) {You should stay under
rcu_read_unlockif you access old_data's fields.
My assumption was: if we have an ovpn object in the user data, it means that its reference counter was increased to account for this usage.
But I presume we have no guarantee that it won't be decreased while outside of the rcu read lock area.
Will move the check inside.
Quoted 15 lines
Quoted 4 lines
drivers/net/ovpn/udp.c new file +3
-0+netdev_dbg (ovpn->dev, +"%s: provided socket already owned by this interface\n" , + __func__);drivers/net/ovpn/udp.c line 45
Quoted 10 lines
drivers/net/ovpn/udp.c new file +10
-0+return -EALREADY ; + } + +netdev_err (ovpn->dev,"%s: provided socket already taken by other user\n" , + __func__); +return -EBUSY ; + } + +return 0 ; +}
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 08/24] ovpn: introduce the ovpn_socket object
Quoted 15 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
drivers/net/ovpn/socket.c new file +0
-0[...]
Quoted 6 lines
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */
kref_putseems to callovpn_socket_release_krefwithout waiting, and then that callsovpn_socket_detachimmediately as well. Am I missing something?
Quoted 2 lines
hmm what do we need to wait for exactly? (Maybe I am missing something) The
ovpn_socketwill survive a bit longer thanks tokfree_rcu.
The way I read this comment, it says that ovpn_socket_detach will be called after one RCU grace period, but I don't see where that grace period would come from.
ovpn_socket_put -> kref_put(release=ovpn_socket_release_kref) ->
ovpn_socket_release_kref -> ovpn_socket_detachNo grace period here.
Or am I misinterpreting the comment? There will be a grace period caused by kfree_rcu before the ovpn_socket is actually freed, is that what the comment means?
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/net/ovpn/socket.c new file +7
-0+static void ovpn_socket_detach (struct socket *sock) +{ +if (!sock) +return ; + +sockfd_put (sock); +}
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 08/24] ovpn: introduce the ovpn_socket object
Quoted 23 lines · Sabrina Dubroca
Quoted 15 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
drivers/net/ovpn/socket.c new file +0
-0[...]
Quoted 6 lines
drivers/net/ovpn/socket.c
Quoted 2 lines
drivers/net/ovpn/socket.c new file +2
-0+ +/* Finalize release of socket, called after RCU grace period */
kref_putseems to callovpn_socket_release_krefwithout waiting, and then that callsovpn_socket_detachimmediately as well. Am I missing something?Quoted 2 lines
hmm what do we need to wait for exactly? (Maybe I am missing something) The
ovpn_socketwill survive a bit longer thanks tokfree_rcu.The way I read this comment, it says that
ovpn_socket_detachwill be called after one RCU grace period, but I don't see where that grace period would come from.
Quoted 8 lines
ovpn_socket_put -> kref_put(release=ovpn_socket_release_kref) -> ovpn_socket_release_kref -> ovpn_socket_detachNo grace period here.
Or am I misinterpreting the comment? There will be a grace period caused by
kfree_rcubefore theovpn_socketis actually freed, is that what the comment means?
Forgive me - only now I realized that you were referring to what the comment says.
That comment is just totally busted. I think it was there since the code was doing something totally different and was carried over and over by mistake.
Sorry
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 9/24 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 | 174 ++++++++++++++++++++++++++- drivers/net/ovpn/io.h | 2 + drivers/net/ovpn/main.c | 2 + drivers/net/ovpn/ovpnstruct.h | 2 + drivers/net/ovpn/peer.c | 37 ++++++ drivers/net/ovpn/peer.h | 9 ++ drivers/net/ovpn/udp.c | 219 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/udp.h | 14 +++ 8 files changed, 458 insertions(+), 1 deletion(-)
drivers/net/ovpn/io.c +173 -1
@@ -9,11 +9,13 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gso.h> #include "io.h" #include "ovpnstruct.h" #include "netlink.h" #include "peer.h" +#include "udp.h" int ovpn_struct_init (struct net_device *dev) {
@@ -28,6 +30,12 @@ int ovpn_struct_init(struct net_device *dev)spin_lock_init (&ovpn->lock); + ovpn->crypto_wq =alloc_workqueue ("ovpn-crypto-wq-%s" , +WQ_CPU_INTENSIVE |WQ_MEM_RECLAIM ,0 , + dev->name); +if (!ovpn->crypto_wq) +return -ENOMEM ; + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM ,0 , dev->name);if (!ovpn->events_wq)
@@ -40,11 +48,175 @@ int ovpn_struct_init(struct net_device *dev)return 0 ; } +static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +return true; +} + +/* Process packets in TX queue in a transport-specific way. +* +* UDP transport - encrypt and send across the tunnel. +*/ +void ovpn_encrypt_work (struct work_struct *work) +{ +struct sk_buff *skb, *curr, *next; +struct ovpn_peer *peer; + + peer = container_of(work, struct ovpn_peer, encrypt_work); +while ((skb =ptr_ring_consume_bh (&peer->tx_ring))) { +/* this might be a GSO-segmented skb list: process each skb +* independently +*/ + skb_list_walk_safe(skb, curr, next) { +/* if one segment fails encryption, we drop the entire +* packet, because it does not really make sense to send +* only part of it at this point +*/ +if (unlikely (!ovpn_encrypt_one (peer, curr))) { +kfree_skb_list (skb); + skb =NULL ; +break ; + } + } + +/* successful encryption */ +if (likely (skb)) { + skb_list_walk_safe(skb, curr, next) { +skb_mark_not_on_list (curr); + +switch (peer->sock->sock->sk->sk_protocol) { +case IPPROTO_UDP : +ovpn_udp_send_skb (peer->ovpn, peer, + curr); +break ; +default : +/* no transport configured yet */ +consume_skb (skb); +break ; + } + } + } + +/* give a chance to be rescheduled if needed */ +cond_resched (); + } +ovpn_peer_put (peer); +} + +/* send skb to connected peer, if any */ +static void ovpn_queue_skb (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +int ret; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); + goto drop; + } + + ret =ptr_ring_produce_bh (&peer->tx_ring, skb); +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: cannot queue packet to TX ring\n" , + peer->ovpn->dev->name); + goto drop; + } + +if (!queue_work (ovpn->crypto_wq, &peer->encrypt_work)) +ovpn_peer_put (peer); + +return ; +drop: +if (peer) +ovpn_peer_put (peer); +kfree_skb_list (skb); +} + +/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static __be16 ovpn_ip_check_protocol (struct sk_buff *skb) +{ +__be16 proto =0 ; + +/* skb could be non-linear, make sure IP header is in non-fragmented +* part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} +/* Send user data to the network */ netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) { +struct ovpn_struct *ovpn =netdev_priv (dev); +struct sk_buff *segments, *tmp, *curr, *next; +struct sk_buff_head skb_list; +__be16 proto; +int ret; + +/* reset netfilter state */ +nf_reset_ct (skb); + +/* verify IP header size in network packet */ + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto || skb->protocol != proto)) { +net_err_ratelimited ("%s: dropping malformed payload packet\n" , + dev->name); + goto drop; + } + +if (skb_is_gso (skb)) { + segments =skb_gso_segment (skb,0 ); +if (IS_ERR (segments)) { + ret =PTR_ERR (segments); +net_err_ratelimited ("%s: cannot segment packet: %d\n" , + dev->name, ret); + goto drop; + } + +consume_skb (skb); + skb = segments; + } + +/* from this moment on, "skb" might be a list */ + +__skb_queue_head_init (&skb_list); + skb_list_walk_safe(skb, curr, next) { +skb_mark_not_on_list (curr); + + tmp =skb_share_check (curr,GFP_ATOMIC ); +if (unlikely (!tmp)) { +kfree_skb_list (next); +net_err_ratelimited ("%s: skb_share_check failed\n" , + dev->name); + goto drop_list; + } + +__skb_queue_tail (&skb_list, tmp); + } + skb_list.prev->next =NULL ; + +ovpn_queue_skb (ovpn, skb_list.next,NULL ); + +return NETDEV_TX_OK ; + +drop_list: + skb_queue_walk_safe(&skb_list, curr, next) +kfree_skb (curr); +drop:skb_tx_error (skb); -kfree_skb (skb); +kfree_skb_list (skb);return NET_XMIT_DROP ; }
drivers/net/ovpn/io.h +2 -0
@@ -19,4 +19,6 @@int ovpn_struct_init (struct net_device *dev);netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); +void ovpn_encrypt_work (struct work_struct *work); +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +2 -0
@@ -39,7 +39,9 @@ static void ovpn_struct_free(struct net_device *net)rtnl_unlock ();free_percpu (net->tstats); +flush_workqueue (ovpn->crypto_wq);flush_workqueue (ovpn->events_wq); +destroy_workqueue (ovpn->crypto_wq);destroy_workqueue (ovpn->events_wq);rcu_barrier (); }
drivers/net/ovpn/ovpnstruct.h +2 -0
@@ -18,6 +18,7 @@* @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) * @lock: protect this object +* @crypto_wq: used to schedule crypto work that may sleep during TX/RX * @event_wq: used to schedule generic events that may sleep and that need to be * performed outside of softirq context * @peer: in P2P mode, this is the only remote peer
@@ -28,6 +29,7 @@ struct ovpn_struct {bool registered;enum ovpn_mode mode;spinlock_t lock;/* protect writing to the ovpn_struct object */ +struct workqueue_struct *crypto_wq;struct workqueue_struct *events_wq;struct ovpn_peer __rcu *peer;struct list_head dev_list;
drivers/net/ovpn/peer.c +37 -0
@@ -39,6 +39,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)spin_lock_init (&peer->lock);kref_init (&peer->refcount); +INIT_WORK (&peer->encrypt_work, ovpn_encrypt_work); + ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );if (ret <0 ) { netdev_err(ovpn->dev, "%s: cannot initialize dst cache\n",
@@ -119,6 +121,9 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)void ovpn_peer_release (struct ovpn_peer *peer) { +if (peer->sock) +ovpn_socket_put (peer->sock); +call_rcu (&peer->rcu, ovpn_peer_release_rcu); }
@@ -288,6 +293,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 *tmp, *peer =NULL ; + +/* in P2P mode, no matter the destination, packets are always sent to +* the single peer listening on the other side +*/ +if (ovpn->mode ==OVPN_MODE_P2P ) { +rcu_read_lock (); + tmp =rcu_dereference (ovpn->peer); +if (likely (tmp &&ovpn_peer_hold (tmp))) + peer = tmp; +rcu_read_unlock (); + } + +return peer; +} +/** * ovpn_peer_add_p2p - add per to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +9 -0
@@ -11,6 +11,7 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "socket.h" #include <linux/ptr_ring.h> #include <net/dst_cache.h>
@@ -22,9 +23,12 @@* @id: unique identifier * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @encrypt_work: work used to process outgoing packets +* @decrypt_work: work used to process incoming packets * @tx_ring: queue of outgoing poackets to this peer * @rx_ring: queue of incoming packets from this peer * @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI +* @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
@@ -41,9 +45,12 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct work_struct encrypt_work; +struct work_struct decrypt_work;struct ptr_ring tx_ring;struct ptr_ring rx_ring;struct ptr_ring netif_rx_ring; +struct ovpn_socket *sock;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -148,5 +155,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn,* Return: a pointer to the peer if found or NULL otherwise */ struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_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/udp.c +219 -0
232 lines not on this page · raw message
drivers/net/ovpn/udp.h +14 -0
@@ -9,7 +9,12 @@#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 ;/**
@@ -24,4 +29,13 @@ struct socket;*/ int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); +/** +* ovpn_udp_send_skb - prepare skb and send it over via UDP +* @ovpn: the openvpn instance +* @peer: the destination peer +* @skb: the packet to send +*/ +void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb); +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
Quoted 17 lines · Antonio Quartulli
drivers/net/ovpn/io.c +6
-0@@ -28,6 +30,12 @@ int ovpn_struct_init(struct net_device *dev)spin_lock_init (&ovpn->lock); + ovpn->crypto_wq =alloc_workqueue ("ovpn-crypto-wq-%s" , +WQ_CPU_INTENSIVE |WQ_MEM_RECLAIM ,0 , + dev->name); +if (!ovpn->crypto_wq) +return -ENOMEM ; + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM ,0 , dev->name);if (!ovpn->events_wq)
Quoted 1 line
return
-ENOMEM;
This will leak crypto_wq on failure. You need to roll back all previous changes when something fails (also if you move all this stuff into ndo_init).
drivers/net/ovpn/peer.h line 27
Quoted 10 lines
drivers/net/ovpn/peer.h +2
-0@@ -22,9 +23,12 @@* @id: unique identifier * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @encrypt_work: work used to process outgoing packets +* @decrypt_work: work used to process incoming packets
nit: Only encrypt_work is used in this patch, decrypt_work is for RX
Quoted 29 lines
drivers/net/ovpn/udp.c +25
-0+/** +* ovpn_udp4_output - send IPv4 packet over udp socket +* @ovpn: the openvpn instance +* @bind: the binding related to the destination peer +* @cache: dst cache +* @sk: the socket to send the packet over +* @skb: the packet to send +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_udp4_output (struct ovpn_struct *ovpn,struct ovpn_bind *bind, +struct dst_cache *cache,struct sock *sk, +struct sk_buff *skb) +{ +struct rtable *rt; +struct flowi4 fl = { + .saddr = bind->local.ipv4.s_addr, + .daddr = bind->sa.in4.sin_addr.s_addr, + .fl4_sport =inet_sk (sk)->inet_sport, + .fl4_dport = bind->sa.in4.sin_port, + .flowi4_proto = sk->sk_protocol, + .flowi4_mark = sk->sk_mark, + }; +int ret; +
drivers/net/ovpn/udp.c line 60
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+local_bh_disable (); + rt =dst_cache_get_ip4 (cache, &fl.saddr); +if (rt) + goto transmit; + +if (unlikely (!inet_confirm_addr (sock_net (sk),NULL ,0 , fl.saddr, +RT_SCOPE_HOST ))) { +/* we may end up here when the cached address is not usable +* anymore. In this case we reset address/cache and perform a +* new look up
What exactly are you trying to guard against here? The ipv4 address used for the last packet being removed from the device/host? I don't see other tunnels using dst_cache doing this kind of thing (except wireguard).
drivers/net/ovpn/udp.c line 73
Quoted 13 lines
drivers/net/ovpn/udp.c +13
-0+*/ + fl.saddr =0 ; + bind->local.ipv4.s_addr =0 ; +dst_cache_reset (cache); + } + + rt =ip_route_output_flow (sock_net (sk), &fl, sk); +if (IS_ERR (rt) &&PTR_ERR (rt) == -EINVAL ) { + fl.saddr =0 ; + bind->local.ipv4.s_addr =0 ; +dst_cache_reset (cache); + + rt =ip_route_output_flow (sock_net (sk), &fl, sk);
Why do you need to repeat the lookup? And why only for ipv4, but not for ipv6?
drivers/net/ovpn/udp.c line 82
Quoted 9 lines
drivers/net/ovpn/udp.c +9
-0+ } + +if (IS_ERR (rt)) { + ret =PTR_ERR (rt); +net_dbg_ratelimited ("%s: no route to host %pISpc: %d\n" , + ovpn->dev->name, &bind->sa.in4, ret); + goto err; + } +dst_cache_set_ip4 (cache, &rt->dst, fl.saddr);
Overall this looks a whole lot like udp_tunnel_dst_lookup, except for:
- 2nd lookup
- inet_confirm_addr/dst_cache_reset
(and there's udp_tunnel6_dst_lookup for ipv6)
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
Quoted 9 lines · Sabrina Dubroca
Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/io.c +0
-0@@ -28,6 +30,12 @@ int ovpn_struct_init(struct net_device *dev)spin_lock_init (&ovpn->lock);
Quoted 14 lines
Quoted 10 lines
drivers/net/ovpn/io.c +6
-0+ ovpn->crypto_wq =alloc_workqueue ("ovpn-crypto-wq-%s" , +WQ_CPU_INTENSIVE |WQ_MEM_RECLAIM ,0 , + dev->name); +if (!ovpn->crypto_wq) +return -ENOMEM ; + ovpn->events_wq =alloc_workqueue ("ovpn-events-wq-%s" ,WQ_MEM_RECLAIM ,0 , dev->name);if (!ovpn->events_wq)return
-ENOMEM;This will leak
crypto_wqon failure. You need to roll back all previous changes when something fails (also if you move all this stuff intondo_init).
ouch, good catch! thanks.
Quoted 13 lines
drivers/net/ovpn/peer.h line 27
Quoted 11 lines
drivers/net/ovpn/peer.h +2
-0@@ -22,9 +23,12 @@* @id: unique identifier * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @encrypt_work: work used to process outgoing packets +* @decrypt_work: work used to process incoming packets nit: Only
encrypt_workis used in this patch,decrypt_workis for RX
Right, same for tx_ring actually. will move both to the next patch
Quoted 38 lines
Quoted 38 lines
drivers/net/ovpn/udp.c +32
-0+/** +* ovpn_udp4_output - send IPv4 packet over udp socket +* @ovpn: the openvpn instance +* @bind: the binding related to the destination peer +* @cache: dst cache +* @sk: the socket to send the packet over +* @skb: the packet to send +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_udp4_output (struct ovpn_struct *ovpn,struct ovpn_bind *bind, +struct dst_cache *cache,struct sock *sk, +struct sk_buff *skb) +{ +struct rtable *rt; +struct flowi4 fl = { + .saddr = bind->local.ipv4.s_addr, + .daddr = bind->sa.in4.sin_addr.s_addr, + .fl4_sport =inet_sk (sk)->inet_sport, + .fl4_dport = bind->sa.in4.sin_port, + .flowi4_proto = sk->sk_protocol, + .flowi4_mark = sk->sk_mark, + }; +int ret; + +local_bh_disable (); + rt =dst_cache_get_ip4 (cache, &fl.saddr); +if (rt) + goto transmit; + +if (unlikely (!inet_confirm_addr (sock_net (sk),NULL ,0 , fl.saddr, +RT_SCOPE_HOST ))) {
Quoted 8 lines
drivers/net/ovpn/udp.c line 60
Quoted 3 lines
drivers/net/ovpn/udp.c +3
-0+/* we may end up here when the cached address is not usable +* anymore. In this case we reset address/cache and perform a +* new look up What exactly are you trying to guard against here? The ipv4 address used for the last packet being removed from the device/host? I don't see other tunnels using
dst_cachedoing this kind of thing (except wireguard).
yes, that's the scenario being checked (which hopefully is what the comment conveys).
Quoted 17 lines
Quoted 4 lines
drivers/net/ovpn/udp.c +3
-0+*/ + fl.saddr =0 ; + bind->local.ipv4.s_addr =0 ;drivers/net/ovpn/udp.c line 73
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+dst_cache_reset (cache); + } + + rt =ip_route_output_flow (sock_net (sk), &fl, sk); +if (IS_ERR (rt) &&PTR_ERR (rt) == -EINVAL ) { + fl.saddr =0 ; + bind->local.ipv4.s_addr =0 ; +dst_cache_reset (cache); + + rt =ip_route_output_flow (sock_net (sk), &fl, sk);Why do you need to repeat the lookup? And why only for ipv4, but not for ipv6?
We are repeating the lookup without the saddr.
The first lookup may have failed because the destination is not reachable anymore from that specific source address, but it may be reachable from another one (i.e. routing table change in a multi-homed setup).
Why not for v6..that's a good question..I wonder if I should just do the same and repeat the lookup with ip6addr_any as source..I think it would make sense as we could end up in the same scenario as described for IPv4.
What do you think?
Quoted 14 lines
drivers/net/ovpn/udp.c line 82
Quoted 10 lines
drivers/net/ovpn/udp.c +9
-0+ } + +if (IS_ERR (rt)) { + ret =PTR_ERR (rt); +net_dbg_ratelimited ("%s: no route to host %pISpc: %d\n" , + ovpn->dev->name, &bind->sa.in4, ret); + goto err; + } +dst_cache_set_ip4 (cache, &rt->dst, fl.saddr);Overall this looks a whole lot like
udp_tunnel_dst_lookup, except for:
- 2nd lookup
- inet_confirm_addr/dst_cache_reset
but why doesn't udp_tunnel_dst_lookup account for cases where the source address is not usable anymore? I think they are reasonable, no?
Maybe I could still use udp_tunnel_dst_lookup, but call it a second time without saddr in case of failure?
Quoted 3 lines
(and there's
udp_tunnel6_dst_lookupfor ipv6)
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
In reply to Antonio QuartulliQuoted 14 lines · Antonio Quartulli
drivers/net/ovpn/io.c +14
-0+/* send skb to connected peer, if any */ +static void ovpn_queue_skb (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +int ret; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); + goto drop; + }
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 129
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ + ret =ptr_ring_produce_bh (&peer->tx_ring, skb); +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: cannot queue packet to TX ring\n" , + peer->ovpn->dev->name); + goto drop; + } + +if (!queue_work (ovpn->crypto_wq, &peer->encrypt_work)) +ovpn_peer_put (peer);
I wanted to come back to this after going through the crypto patch, because this felt like a strange construct when I first looked at this patch.
Why are you using a workqueue here? Based on the kdoc for crypto_wq ("used to schedule crypto work that may sleep during TX/RX"), it's to deal with async crypto.
If so, why not use the more standard way of dealing with async crypto in contexts that cannot sleep, ie letting the crypto core call the "done" callback asynchronously? You need to do all the proper refcount handling, but IMO it's cleaner and simpler than this workqueue and ptr_ring. You can see an example of that in macsec (macsec_encrypt_* in drivers/net/macsec.c).
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
Quoted 34 lines · Sabrina Dubroca
Quoted 14 lines · Antonio Quartulli
drivers/net/ovpn/io.c +14
-0+/* send skb to connected peer, if any */ +static void ovpn_queue_skb (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +int ret; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); + goto drop; + }drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 129
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ + ret =ptr_ring_produce_bh (&peer->tx_ring, skb); +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: cannot queue packet to TX ring\n" , + peer->ovpn->dev->name); + goto drop; + } + +if (!queue_work (ovpn->crypto_wq, &peer->encrypt_work)) +ovpn_peer_put (peer);I wanted to come back to this after going through the crypto patch, because this felt like a strange construct when I first looked at this patch.
Why are you using a workqueue here? Based on the kdoc for
crypto_wq("used to schedule crypto work that may sleep during TX/RX"), it's to deal with async crypto.
Quoted 6 lines
If so, why not use the more standard way of dealing with async crypto in contexts that cannot sleep, ie letting the crypto core call the "done" callback asynchronously? You need to do all the proper refcount handling, but IMO it's cleaner and simpler than this workqueue and
ptr_ring. You can see an example of that in macsec (macsec_encrypt_* indrivers/net/macsec.c).
Aha! You don't know how happy I was when I found the doc describing how to convert the async code into sync-looking :-) With the detail that I had to move to a different context, as the code may want to sleep (hence the introduction of the workqueue).
It looks like I am little fan of WQs, while you are telling me to avoid them if possible.
I presume that using WQs comes with a non-negligible cost, therefore if we can just get things done without having to use them, then I should just don't.
I think I could go back to no-workqueue encrypt/decrypt.
Do you think this may have any impact on any future multi-core optimization? Back then I also thought that going through workers may make improvements in this area easier. But I could just be wrong.
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
Quoted 42 lines · Antonio Quartulli
Quoted 34 lines · Sabrina Dubroca
Quoted 14 lines · Antonio Quartulli
drivers/net/ovpn/io.c +14
-0+/* send skb to connected peer, if any */ +static void ovpn_queue_skb (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +int ret; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); + goto drop; + }drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 129
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ + ret =ptr_ring_produce_bh (&peer->tx_ring, skb); +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: cannot queue packet to TX ring\n" , + peer->ovpn->dev->name); + goto drop; + } + +if (!queue_work (ovpn->crypto_wq, &peer->encrypt_work)) +ovpn_peer_put (peer);I wanted to come back to this after going through the crypto patch, because this felt like a strange construct when I first looked at this patch.
Why are you using a workqueue here? Based on the kdoc for
crypto_wq("used to schedule crypto work that may sleep during TX/RX"), it's to deal with async crypto.Quoted 6 lines
If so, why not use the more standard way of dealing with async crypto in contexts that cannot sleep, ie letting the crypto core call the "done" callback asynchronously? You need to do all the proper refcount handling, but IMO it's cleaner and simpler than this workqueue and
ptr_ring. You can see an example of that in macsec (macsec_encrypt_* indrivers/net/macsec.c).
Quoted 7 lines
Aha! You don't know how happy I was when I found the doc describing how to convert the async code into sync-looking :-) With the detail that I had to move to a different context, as the code may want to sleep (hence the introduction of the workqueue).
It looks like I am little fan of WQs, while you are telling me to avoid them if possible.
I'm mainly trying to simplify the code (get rid of some ptr_rings, get rid of some ping-pong between functions and changes of context, etc). And here, I'm also trying to make it look more like other similar pieces of code, because I'm already familiar with a few kernel implementations of protocols doing crypto (macsec, ipsec, tls).
Quoted 3 lines
I presume that using WQs comes with a non-negligible cost, therefore if we can just get things done without having to use them, then I should just don't.
If you're using AESNI for your GCM implementation, the crypto API will also be using a workqueue (see crypto/cryptd.c), but only when the crypto can't be done immediately (ie, when the FPU is already busing).
In the case of crypto accelerators, there might be benefits from queueing multiple requests and then letting them live their life, instead of waiting for each request separately. I don't have access to that HW so I cannot test this.
Quoted 4 lines
I think I could go back to no-workqueue encrypt/decrypt.
Do you think this may have any impact on any future multi-core optimization? Back then I also thought that going through workers may make improvements in this area easier. But I could just be wrong.
Without thinking about it too deeply, the workqueue looks more like a bottleneck that a no-workqueue approach just wouldn't have. You would probably need per-CPU WQs (probably separated for encrypt and decrypt). cryptd_enqueue_request (crypto/cryptd.c) has an example of that.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 09/24] ovpn: implement basic TX path (UDP)
Quoted 48 lines · Sabrina Dubroca
Quoted 47 lines · Antonio Quartulli
Quoted 34 lines · Sabrina Dubroca
Quoted 14 lines · Antonio Quartulli
drivers/net/ovpn/io.c +14
-0+/* send skb to connected peer, if any */ +static void ovpn_queue_skb (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +int ret; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); + goto drop; + }drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 129
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ + ret =ptr_ring_produce_bh (&peer->tx_ring, skb); +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: cannot queue packet to TX ring\n" , + peer->ovpn->dev->name); + goto drop; + } + +if (!queue_work (ovpn->crypto_wq, &peer->encrypt_work)) +ovpn_peer_put (peer);I wanted to come back to this after going through the crypto patch, because this felt like a strange construct when I first looked at this patch.
Why are you using a workqueue here? Based on the kdoc for
crypto_wq("used to schedule crypto work that may sleep during TX/RX"), it's to deal with async crypto.Quoted 6 lines
If so, why not use the more standard way of dealing with async crypto in contexts that cannot sleep, ie letting the crypto core call the "done" callback asynchronously? You need to do all the proper refcount handling, but IMO it's cleaner and simpler than this workqueue and
ptr_ring. You can see an example of that in macsec (macsec_encrypt_* indrivers/net/macsec.c).Aha! You don't know how happy I was when I found the doc describing how to convert the async code into sync-looking :-) With the detail that I had to move to a different context, as the code may want to sleep (hence the introduction of the workqueue).
Quoted 8 lines
Quoted 2 lines
It looks like I am little fan of WQs, while you are telling me to avoid them if possible.
I'm mainly trying to simplify the code (get rid of some
ptr_rings, get rid of some ping-pong between functions and changes of context, etc). And here, I'm also trying to make it look more like other similar pieces of code, because I'm already familiar with a few kernel implementations of protocols doing crypto (macsec, ipsec, tls).
Thanks for that, you already helped me getting rid of several constructs that weren't really needed.
Quoted 8 lines
Quoted 4 lines
I presume that using WQs comes with a non-negligible cost, therefore if we can just get things done without having to use them, then I should just don't.
If you're using AESNI for your GCM implementation, the crypto API will also be using a workqueue (see
crypto/cryptd.c), but only when the crypto can't be done immediately (ie, when the FPU is already busing).
Right.
Quoted 11 lines
In the case of crypto accelerators, there might be benefits from queueing multiple requests and then letting them live their life, instead of waiting for each request separately. I don't have access to that HW so I cannot test this.
Quoted 4 lines
I think I could go back to no-workqueue encrypt/decrypt.
Do you think this may have any impact on any future multi-core optimization? Back then I also thought that going through workers may make improvements in this area easier. But I could just be wrong.
Quoted 5 lines
Without thinking about it too deeply, the workqueue looks more like a bottleneck that a no-workqueue approach just wouldn't have. You would probably need per-CPU WQs (probably separated for encrypt and decrypt).
cryptd_enqueue_request(crypto/cryptd.c) has an example of that.
Ok. Sounds like something we can re-consider later on.
Let's start by killing the wq in the current code.
Thanks for the pointers.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 10/24 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 | 114 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/io.h | 5 ++ drivers/net/ovpn/peer.c | 9 +++ drivers/net/ovpn/peer.h | 2 + drivers/net/ovpn/proto.h | 115 +++++++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.c | 24 ++++++++ drivers/net/ovpn/udp.c | 125 +++++++++++++++++++++++++++++++++++++- drivers/net/ovpn/udp.h | 6 ++ 8 files changed, 397 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ovpn/proto.h
drivers/net/ovpn/io.c +112 -2
@@ -11,10 +11,10 @@#include <linux/skbuff.h> #include <net/gso.h> -#include "io.h" #include "ovpnstruct.h" -#include "netlink.h" #include "peer.h" +#include "io.h" +#include "netlink.h" #include "udp.h" int ovpn_struct_init (struct net_device *dev)
@@ -48,6 +48,116 @@ int ovpn_struct_init(struct net_device *dev)return 0 ; } +/* 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); + +/* update per-cpu RX stats with the stored size of encrypted packet */ + +/* we are in softirq context - hence no locking nor disable preemption +* needed +*/ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); + +/* cause packet to be "received" by the interface */ +napi_gro_receive (&peer->napi, skb); +} + +int ovpn_napi_poll (struct napi_struct *napi,int budget) +{ + struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi); +struct sk_buff *skb; +int work_done =0 ; + +if (unlikely (budget <=0 )) +return 0 ; +/* this function should schedule at most 'budget' number of +* packets for delivery to the interface. +* If in the queue we have more packets than what allowed by the +* budget, the next polling will take care of those +*/ +while ((work_done < budget) && + (skb =ptr_ring_consume_bh (&peer->netif_rx_ring))) { +ovpn_netdev_write (peer, skb); + work_done++; + } + +if (work_done < budget) +napi_complete_done (napi, work_done); + +return work_done; +} + +/* Entry point for processing an incoming packet (in skb form) +* +* Enqueue the packet and schedule RX consumer. +* Reference to peer is dropped only in case of success. +* +* Return 0 if the packet was handled (and consumed) +* Return <0 in case of error (return value is error code) +*/ +int ovpn_recv (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb) +{ +if (unlikely (ptr_ring_produce_bh (&peer->rx_ring, skb) <0 )) +return -ENOSPC ; + +if (!queue_work (ovpn->crypto_wq, &peer->decrypt_work)) +ovpn_peer_put (peer); + +return 0 ; +} + +static int ovpn_decrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +return true; +} + +/* pick next packet from RX queue, decrypt and forward it to the device */ +void ovpn_decrypt_work (struct work_struct *work) +{ +struct ovpn_peer *peer; +struct sk_buff *skb; + + peer = container_of(work, struct ovpn_peer, decrypt_work); +while ((skb =ptr_ring_consume_bh (&peer->rx_ring))) { +if (likely (ovpn_decrypt_one (peer, skb) ==0 )) { +/* if a packet has been enqueued for NAPI, signal +* availability to the networking stack +*/ +local_bh_disable (); +napi_schedule (&peer->napi); +local_bh_enable (); + } + +/* give a chance to be rescheduled if needed */ +cond_resched (); + } +ovpn_peer_put (peer); +} +static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) {return true;
drivers/net/ovpn/io.h +5 -0
@@ -18,7 +18,12 @@*/ int ovpn_struct_init (struct net_device *dev);netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); +int ovpn_napi_poll (struct napi_struct *napi,int budget); + +int ovpn_recv (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb);void ovpn_encrypt_work (struct work_struct *work); +void ovpn_decrypt_work (struct work_struct *work);#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/peer.c +9 -0
@@ -40,6 +40,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)kref_init (&peer->refcount);INIT_WORK (&peer->encrypt_work, ovpn_encrypt_work); +INIT_WORK (&peer->decrypt_work, ovpn_decrypt_work); ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );if (ret <0 ) {
@@ -69,6 +70,11 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) goto err_rx_ring; } +/* configure and start NAPI */ +netif_napi_add_tx_weight (ovpn->dev, &peer->napi, ovpn_napi_poll, +NAPI_POLL_WEIGHT ); +napi_enable (&peer->napi); +dev_hold (ovpn->dev);return peer;
@@ -121,6 +127,9 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)void ovpn_peer_release (struct ovpn_peer *peer) { +napi_disable (&peer->napi); +netif_napi_del (&peer->napi); +if (peer->sock)ovpn_socket_put (peer->sock);
drivers/net/ovpn/peer.h +2 -0
@@ -28,6 +28,7 @@* @tx_ring: queue of outgoing poackets to this peer * @rx_ring: queue of incoming packets from this peer * @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI +* @napi: NAPI object * @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
@@ -50,6 +51,7 @@ struct ovpn_peer {struct ptr_ring tx_ring;struct ptr_ring rx_ring;struct ptr_ring netif_rx_ring; +struct napi_struct napi;struct ovpn_socket *sock;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;
drivers/net/ovpn/proto.h new file +115 -0
115 lines not on this page · raw message
drivers/net/ovpn/socket.c +24 -0
@@ -23,6 +23,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); }
@@ -69,6 +72,27 @@ static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)return ret; } +/* Retrieve the corresponding ovpn object from a UDP socket +* rcu_read_lock must be held on entry +*/ +struct ovpn_struct *ovpn_from_udp_sock (struct sock *sk) +{ +struct ovpn_socket *ovpn_sock; + +if (unlikely (READ_ONCE (udp_sk (sk)->encap_type) !=UDP_ENCAP_OVPNINUDP )) +return NULL ; + + ovpn_sock =rcu_dereference_sk_user_data (sk); +if (unlikely (!ovpn_sock)) +return NULL ; + +/* make sure that sk matches our stored transport socket */ +if (unlikely (!ovpn_sock->sock || sk != ovpn_sock->sock->sk)) +return NULL ; + +return ovpn_sock->ovpn; +} +struct ovpn_socket *ovpn_socket_new (struct socket *sock,struct ovpn_peer *peer) {struct ovpn_socket *ovpn_sock;
drivers/net/ovpn/udp.c +124 -1
149 lines not on this page · raw message
drivers/net/ovpn/udp.h +6 -0
@@ -29,6 +29,12 @@ struct socket;*/ int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); +/** +* ovpn_udp_socket_detach - clean udp-tunnel status for this socket +* @sock: the socket to clean +*/ +void ovpn_udp_socket_detach (struct socket *sock); +/** * ovpn_udp_send_skb - prepare skb and send it over via UDP * @ovpn: the openvpn instance
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +7
-0+/* 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
But it could have been corrupted before it got into the VPN?
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+*/ + skb->ip_summed =CHECKSUM_UNNECESSARY ; + skb->csum_level = ~0 ; +
[...]
Quoted 14 lines
drivers/net/ovpn/io.c +14
-0+int ovpn_napi_poll (struct napi_struct *napi,int budget) +{ + struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi); +struct sk_buff *skb; +int work_done =0 ; + +if (unlikely (budget <=0 )) +return 0 ; +/* this function should schedule at most 'budget' number of +* packets for delivery to the interface. +* If in the queue we have more packets than what allowed by the +* budget, the next polling will take care of those +*/ + while ((work_done < budget) &&
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 111
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ (skb = ptr_ring_consume_bh(&peer->netif_rx_ring))) { +ovpn_netdev_write (peer, skb); + work_done++; + } + +if (work_done < budget) +napi_complete_done (napi, work_done); + +return work_done; +}
Why not use gro_cells? It would avoid all that napi polling and netif_rx_ring code (and it's per-cpu, going back to our other discussion around napi).
Quoted 5 lines
drivers/net/ovpn/proto.h new file +0
-0
[...]
drivers/net/ovpn/proto.h line 107
Quoted 12 lines
drivers/net/ovpn/proto.h new file +11
-0+/** +* 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) +static inline u32 ovpn_opcode_compose(u8 opcode, u8 key_id, u32 peer_id)
(tiny nit: those aren't used yet in this patch. probably not worth moving them into the right patch.)
Quoted 35 lines
drivers/net/ovpn/udp.c +24
-0@@ -20,9 +20,117 @@#include "bind.h" #include "io.h" #include "peer.h" +#include "proto.h" #include "socket.h" #include "udp.h" +/** +* ovpn_udp_encap_recv - Start processing a received UDP packet. +* @sk: socket over which the packet was received +* @skb: the received packet +* +* If the first byte of the payload is DATA_V2, the packet is further processed, +* otherwise it is forwarded to the UDP stack for delivery to user space. +* +* Return: +* 0 if skb was consumed or dropped +* >0 if skb should be passed up to userspace as UDP (packet not consumed) +* <0 if skb should be resubmitted as proto -N (packet not consumed) +*/ +static int ovpn_udp_encap_recv (struct sock *sk,struct sk_buff *skb) +{ +struct ovpn_peer *peer =NULL ; +struct ovpn_struct *ovpn; +u32 peer_id; +u8 opcode; +int ret; + + ovpn =ovpn_from_udp_sock (sk); +if (unlikely (!ovpn)) {
drivers/net/ovpn/udp.c line 59
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+net_err_ratelimited ("%s: cannot obtain ovpn object from UDP socket\n" , + __func__); + goto drop; + } + +/* Make sure the first 4 bytes of the skb data buffer after the UDP +* header are accessible. +* They are required to fetch the OP code, the key ID and the peer ID. +*/ +if (unlikely (!pskb_may_pull (skb,sizeof (struct udphdr ) +4 ))) {
Is this OVPN_OP_SIZE_V2?
Quoted 25 lines
drivers/net/ovpn/udp.c +25
-0+net_dbg_ratelimited ("%s: packet too small\n" , __func__); + goto drop; + } + + opcode =ovpn_opcode_from_skb (skb,sizeof (struct udphdr )); +if (unlikely (opcode !=OVPN_DATA_V2 )) { +/* DATA_V1 is not supported */ +if (opcode ==OVPN_DATA_V1 ) + goto drop; + +/* unknown or control packet: let it bubble up to userspace */ +return 1 ; + } + + peer_id =ovpn_peer_id_from_skb (skb,sizeof (struct udphdr )); +/* some OpenVPN server implementations send data packets with the +* peer-id set to undef. In this case we skip the peer lookup by peer-id +* and we try with the transport address +*/ +if (peer_id !=OVPN_PEER_ID_UNDEF ) { + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +net_err_ratelimited ("%s: received data from unknown peer (id: %d)\n" , + __func__, peer_id); + goto drop;
drivers/net/ovpn/udp.c line 94
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+ } + } + +if (!peer) { +/* data packet with undef peer-id */ + peer =ovpn_peer_get_by_transp_addr (ovpn, skb); +if (unlikely (!peer)) { +netdev_dbg (ovpn->dev, +"%s: received data with undef peer-id from unknown source\n" , + __func__);
_ratelimited?
drivers/net/ovpn/udp.c line 106
Quoted 12 lines
drivers/net/ovpn/udp.c +12
-0+ goto drop; + } + } + +/* At this point we know the packet is from a configured peer. +* DATA_V2 packets are handled in kernel space, the rest goes to user +* space. +* +* Return 1 to instruct the stack to let the packet bubble up to +* userspace +*/ +if (unlikely (opcode !=OVPN_DATA_V2 )) {
You already handled those earlier, before getting the peer.
[...]
drivers/net/ovpn/udp.c int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 372
Quoted 7 lines
drivers/net/ovpn/udp.c +2
-1@@ -255,10 +368,20 @@ int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn)return -EALREADY ; } - netdev_err(ovpn->dev, "%s: provided socket already taken by other user\n", + netdev_err(ovpn->dev, + "%s: provided socket already taken by other user\n",
I guess you meant to break that line in the patch that introduced it, rather than here? :)
drivers/net/ovpn/udp.c int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 386
Quoted 5 lines
drivers/net/ovpn/udp.c +5
-0+void ovpn_udp_socket_detach (struct socket *sock) +{ +struct udp_tunnel_sock_cfg cfg = { }; + +setup_udp_tunnel_sock (sock_net (sock->sk), sock, &cfg);
I can't find anything in the kernel currently using setup_udp_tunnel_sock the way you're using it here.
Does this provide any benefit compared to just letting the kernel disable encap when the socket goes away? Are you planning to detach and then re-attach the same socket?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +7
-0+/* 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 But it could have been corrupted before it got into the VPN?
It could, but I believe a VPN should only take care of integrity along its tunnel (and this is guaranteed by the OpenVPN protocol).
If something corrupted enters the tunnel, we will just deliver it as is to the other end. Upper layers (where the corruption actually happened) have to deal with that.
Quoted 25 lines
Quoted 5 lines
drivers/net/ovpn/io.c +4
-0+*/ + skb->ip_summed =CHECKSUM_UNNECESSARY ; + skb->csum_level = ~0 ; +[...]
Quoted 18 lines
drivers/net/ovpn/io.c +18
-0+int ovpn_napi_poll (struct napi_struct *napi,int budget) +{ + struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi); +struct sk_buff *skb; +int work_done =0 ; + +if (unlikely (budget <=0 )) +return 0 ; +/* this function should schedule at most 'budget' number of +* packets for delivery to the interface. +* If in the queue we have more packets than what allowed by the +* budget, the next polling will take care of those +*/ +while ((work_done < budget) && + (skb =ptr_ring_consume_bh (&peer->netif_rx_ring))) { +ovpn_netdev_write (peer, skb); + work_done++; + }
Quoted 8 lines
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 111
Quoted 6 lines
drivers/net/ovpn/io.c +6
-0+ +if (work_done < budget) +napi_complete_done (napi, work_done); + +return work_done; +}Why not use
gro_cells?
First because I did not know they existed :-)
Quoted 3 lines
It would avoid all that napi polling and
netif_rx_ringcode (and it's per-cpu, going back to our other discussion around napi).
This sounds truly appealing. And if we can make this per-cpu by design, I believe we can definitely drop the per-peer NAPI logic.
Quoted 19 lines
Quoted 7 lines
drivers/net/ovpn/proto.h new file +0
-0[...]
Quoted 10 lines
drivers/net/ovpn/proto.h new file +10
-0+/** +* 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)
Quoted 4 lines
Quoted 1 line
+static inline u32
ovpn_opcode_compose(u8 opcode, u8 key_id, u32 peer_id)(tiny nit: those aren't used yet in this patch. probably not worth moving them into the right patch.)
ouch. I am already going at a speed of 20-25rph (Rebases Per Hour). It shouldn't be a problem to clean this up too.
Quoted 41 lines
Quoted 41 lines
drivers/net/ovpn/udp.c +28
-0@@ -20,9 +20,117 @@#include "bind.h" #include "io.h" #include "peer.h" +#include "proto.h" #include "socket.h" #include "udp.h" +/** +* ovpn_udp_encap_recv - Start processing a received UDP packet. +* @sk: socket over which the packet was received +* @skb: the received packet +* +* If the first byte of the payload is DATA_V2, the packet is further processed, +* otherwise it is forwarded to the UDP stack for delivery to user space. +* +* Return: +* 0 if skb was consumed or dropped +* >0 if skb should be passed up to userspace as UDP (packet not consumed) +* <0 if skb should be resubmitted as proto -N (packet not consumed) +*/ +static int ovpn_udp_encap_recv (struct sock *sk,struct sk_buff *skb) +{ +struct ovpn_peer *peer =NULL ; +struct ovpn_struct *ovpn; +u32 peer_id; +u8 opcode; +int ret; + + ovpn =ovpn_from_udp_sock (sk); +if (unlikely (!ovpn)) { +net_err_ratelimited ("%s: cannot obtain ovpn object from UDP socket\n" , + __func__); + goto drop; + }
Quoted 8 lines
drivers/net/ovpn/udp.c line 59
Quoted 6 lines
drivers/net/ovpn/udp.c +6
-0+ +/* Make sure the first 4 bytes of the skb data buffer after the UDP +* header are accessible. +* They are required to fetch the OP code, the key ID and the peer ID. +*/ +if (unlikely (!pskb_may_pull (skb,sizeof (struct udphdr ) +4 ))) {Is this
OVPN_OP_SIZE_V2?
It is! I will use that define. thanks
Quoted 30 lines
Quoted 30 lines
drivers/net/ovpn/udp.c +29
-0+net_dbg_ratelimited ("%s: packet too small\n" , __func__); + goto drop; + } + + opcode =ovpn_opcode_from_skb (skb,sizeof (struct udphdr )); +if (unlikely (opcode !=OVPN_DATA_V2 )) { +/* DATA_V1 is not supported */ +if (opcode ==OVPN_DATA_V1 ) + goto drop; + +/* unknown or control packet: let it bubble up to userspace */ +return 1 ; + } + + peer_id =ovpn_peer_id_from_skb (skb,sizeof (struct udphdr )); +/* some OpenVPN server implementations send data packets with the +* peer-id set to undef. In this case we skip the peer lookup by peer-id +* and we try with the transport address +*/ +if (peer_id !=OVPN_PEER_ID_UNDEF ) { + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +net_err_ratelimited ("%s: received data from unknown peer (id: %d)\n" , + __func__, peer_id); + goto drop; + } + } + +if (!peer) {
Quoted 8 lines
drivers/net/ovpn/udp.c line 94
Quoted 6 lines
drivers/net/ovpn/udp.c +6
-0+/* data packet with undef peer-id */ + peer =ovpn_peer_get_by_transp_addr (ovpn, skb); +if (unlikely (!peer)) { +netdev_dbg (ovpn->dev, +"%s: received data with undef peer-id from unknown source\n" , + __func__);
_ratelimited?
makes sense. will use net_dbg_ratelimited
Quoted 15 lines
drivers/net/ovpn/udp.c line 106
Quoted 13 lines
drivers/net/ovpn/udp.c +12
-0+ goto drop; + } + } + +/* At this point we know the packet is from a configured peer. +* DATA_V2 packets are handled in kernel space, the rest goes to user +* space. +* +* Return 1 to instruct the stack to let the packet bubble up to +* userspace +*/ +if (unlikely (opcode !=OVPN_DATA_V2 )) {You already handled those earlier, before getting the peer.
ouch..you're right. This can just go.
Quoted 7 lines
[...]
Quoted 4 lines
drivers/net/ovpn/udp.c +0
-0@@ -255,10 +368,20 @@ int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn)return -EALREADY ; }
Quoted 6 lines
drivers/net/ovpn/udp.c int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 372
Quoted 3 lines
drivers/net/ovpn/udp.c +2
-1- netdev_err(ovpn->dev, "%s: provided socket already taken by other user\n", + netdev_err(ovpn->dev, + "%s: provided socket already taken by other user\n",I guess you meant to break that line in the patch that introduced it, rather than here? :)
indeed.
Quoted 11 lines
drivers/net/ovpn/udp.c int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 386
Quoted 7 lines
drivers/net/ovpn/udp.c +5
-0+void ovpn_udp_socket_detach (struct socket *sock) +{ +struct udp_tunnel_sock_cfg cfg = { }; + +setup_udp_tunnel_sock (sock_net (sock->sk), sock, &cfg);I can't find anything in the kernel currently using
setup_udp_tunnel_sockthe way you're using it here.
Quoted 3 lines
Does this provide any benefit compared to just letting the kernel disable encap when the socket goes away? Are you planning to detach and then re-attach the same socket?
Technically, we don't know what happens to this socket after we detach. We have no guarantee that it will be closed.
Right now we detach when the instance is closed, so it's likely that the socket will go, but I don't want to make hard assumptions about what userspace may decide to do with this socket in the future.
If it doesn't hurt, why not doing this easy cleanup?
Thanks!
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Sorry Antonio, I'm only coming back to this now.
Quoted 16 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +7
-0+/* 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 But it could have been corrupted before it got into the VPN?
Quoted 5 lines
It could, but I believe a VPN should only take care of integrity along its tunnel (and this is guaranteed by the OpenVPN protocol).
If something corrupted enters the tunnel, we will just deliver it as is to the other end. Upper layers (where the corruption actually happened) have to deal with that.
I agree with that, but I don't think that's what CHECKSUM_UNNECESSARY (especially with csum_level = MAX) would do. CHECKSUM_UNNECESSARY tells the networking stack that the checksum has been verified (up to csum_level+1, so 0 means the first level of TCP/UDP type headers has been validated):
// include/linux/skbuff.h
* - %CHECKSUM_UNNECESSARY
*
* The hardware you're dealing with doesn't calculate the full checksum * (as in %CHECKSUM_COMPLETE), but it does parse headers and verify checksums * for specific protocols. For such packets it will set %CHECKSUM_UNNECESSARY * if their checksums are okay.
* &sk_buff.csum_level indicates the number of consecutive checksums found in * the packet minus one that have been verified as %CHECKSUM_UNNECESSARY. * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet * and a device is able to verify the checksums for UDP (possibly zero), * GRE (checksum flag is set) and TCP, &sk_buff.csum_level would be set to * two. If the device were only able to verify the UDP checksum and not * GRE, either because it doesn't support GRE checksum or because GRE * checksum is bad, skb->csum_level would be set to zero (TCP checksum is * not considered in this case).
I think you want CHECKSUM_NONE:
* Device did not checksum this packet e.g. due to lack of capabilities.
Then the stack will check if the packet was corrupted.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/net/ovpn/io.c +4
-0+*/ + skb->ip_summed =CHECKSUM_UNNECESSARY ; + skb->csum_level = ~0 ; +
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 1 line · Sabrina Dubroca
Sorry Antonio, I'm only coming back to this now.
No worries and thanks for fishing this email.
Quoted 49 lines
Quoted 16 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +7
-0+/* 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 But it could have been corrupted before it got into the VPN?
Quoted 5 lines
It could, but I believe a VPN should only take care of integrity along its tunnel (and this is guaranteed by the OpenVPN protocol).
If something corrupted enters the tunnel, we will just deliver it as is to the other end. Upper layers (where the corruption actually happened) have to deal with that.I agree with that, but I don't think that's what
CHECKSUM_UNNECESSARY(especially withcsum_level= MAX) would do.CHECKSUM_UNNECESSARYtells the networking stack that the checksum has been verified (up to csum_level+1, so 0 means the first level of TCP/UDP type headers has been validated)://
include/linux/skbuff.h* - %CHECKSUM_UNNECESSARY
** The hardware you're dealing with doesn't calculate the full checksum * (as in %CHECKSUM_COMPLETE), but it does parse headers and verify checksums * for specific protocols. For such packets it will set %CHECKSUM_UNNECESSARY * if their checksums are okay.* &sk_buff.csum_level indicates the number of consecutive checksums found in * the packet minus one that have been verified as %CHECKSUM_UNNECESSARY. * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet * and a device is able to verify the checksums for UDP (possibly zero), * GRE (checksum flag is set) and TCP, &sk_buff.csum_level would be set to * two. If the device were only able to verify the UDP checksum and not * GRE, either because it doesn't support GRE checksum or because GRE * checksum is bad, skb->csum_level would be set to zero (TCP checksum is * not considered in this case).
Quoted 5 lines
I think you want
CHECKSUM_NONE:* Device did not checksum this packet e.g. due to lack of capabilities.
Then the stack will check if the packet was corrupted.
I went back to the wireguard code, which I used for inspiration for this specific part (we are dealing with the same problem here):
https://elixir.bootlin.com/linux/v6.10/source/drivers/net/wireguard/receive.c#L376
basically the idea is: with our encapsulation we can guarantee that what entered the tunnel is also exiting the tunnel, without corruption. Therefore we claim that checksums are all correct.
Doesn't it make sense?
Cheers,
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/net/ovpn/io.c +4
-0+*/ + skb->ip_summed =CHECKSUM_UNNECESSARY ; + skb->csum_level = ~0 ; +
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 60 lines · Antonio Quartulli
Quoted 1 line · Sabrina Dubroca
Sorry Antonio, I'm only coming back to this now.
No worries and thanks for fishing this email.
Quoted 49 lines
Quoted 16 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/io.c int ovpn_struct_init(struct net_device *dev) · line 57
Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/io.c +7
-0+/* 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 But it could have been corrupted before it got into the VPN?
Quoted 5 lines
It could, but I believe a VPN should only take care of integrity along its tunnel (and this is guaranteed by the OpenVPN protocol).
If something corrupted enters the tunnel, we will just deliver it as is to the other end. Upper layers (where the corruption actually happened) have to deal with that.I agree with that, but I don't think that's what
CHECKSUM_UNNECESSARY(especially withcsum_level= MAX) would do.CHECKSUM_UNNECESSARYtells the networking stack that the checksum has been verified (up to csum_level+1, so 0 means the first level of TCP/UDP type headers has been validated)://
include/linux/skbuff.h* - %CHECKSUM_UNNECESSARY
** The hardware you're dealing with doesn't calculate the full checksum * (as in %CHECKSUM_COMPLETE), but it does parse headers and verify checksums * for specific protocols. For such packets it will set %CHECKSUM_UNNECESSARY * if their checksums are okay.* &sk_buff.csum_level indicates the number of consecutive checksums found in * the packet minus one that have been verified as %CHECKSUM_UNNECESSARY. * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet * and a device is able to verify the checksums for UDP (possibly zero), * GRE (checksum flag is set) and TCP, &sk_buff.csum_level would be set to * two. If the device were only able to verify the UDP checksum and not * GRE, either because it doesn't support GRE checksum or because GRE * checksum is bad, skb->csum_level would be set to zero (TCP checksum is * not considered in this case).Quoted 5 lines
I think you want
CHECKSUM_NONE:* Device did not checksum this packet e.g. due to lack of capabilities.
Then the stack will check if the packet was corrupted.
Quoted 8 lines
I went back to the wireguard code, which I used for inspiration for this specific part (we are dealing with the same problem here):
https://elixir.bootlin.com/linux/v6.10/source/drivers/net/wireguard/receive.c#L376
basically the idea is: with our encapsulation we can guarantee that what entered the tunnel is also exiting the tunnel, without corruption. Therefore we claim that checksums are all correct.
Can you be sure that they were correct when they went into the tunnel? If not, I think you have to set CHECKSUM_NONE.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 6 lines · Sabrina Dubroca
Quoted 3 lines
basically the idea is: with our encapsulation we can guarantee that what entered the tunnel is also exiting the tunnel, without corruption. Therefore we claim that checksums are all correct.
Can you be sure that they were correct when they went into the tunnel? If not, I think you have to set
CHECKSUM_NONE.
I can't be sure, because on the sender side we don't validate checksums before encapsulation.
If we assume that outgoing packets are always well formed and they can only be damaged while traveling on the link, then the current code should be ok.
If we cannot make this assumption, then we need the receiver to verify all checksums before moving forward (which is what you are suggesting).
Is it truly possible for the kernel to hand ovpn a packet with invalid checksums on the TX path?
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 11 lines · Antonio Quartulli
Quoted 6 lines · Sabrina Dubroca
Quoted 3 lines
basically the idea is: with our encapsulation we can guarantee that what entered the tunnel is also exiting the tunnel, without corruption. Therefore we claim that checksums are all correct.
Can you be sure that they were correct when they went into the tunnel? If not, I think you have to set
CHECKSUM_NONE.I can't be sure, because on the sender side we don't validate checksums before encapsulation.
Quoted 8 lines
If we assume that outgoing packets are always well formed and they can only be damaged while traveling on the link, then the current code should be ok.
If we cannot make this assumption, then we need the receiver to verify all checksums before moving forward (which is what you are suggesting).
Is it truly possible for the kernel to hand ovpn a packet with invalid checksums on the TX path?
The networking stack shouldn't generate packets with broken checksums, but it could happen. On a VPN server that's giving access to an internal network, I think the packet could get corrupted on the internal network and may be pushed without verification into the tunnel.
It's also possible to inject them with packet sockets for testing. Using scapy to send packets over your ovpn device should allow you to do that.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 10/24] ovpn: implement basic RX path (UDP)
Quoted 18 lines · Sabrina Dubroca
Quoted 17 lines · Antonio Quartulli
Quoted 6 lines · Sabrina Dubroca
Quoted 3 lines
basically the idea is: with our encapsulation we can guarantee that what entered the tunnel is also exiting the tunnel, without corruption. Therefore we claim that checksums are all correct.
Can you be sure that they were correct when they went into the tunnel? If not, I think you have to set
CHECKSUM_NONE.I can't be sure, because on the sender side we don't validate checksums before encapsulation.
If we assume that outgoing packets are always well formed and they can only be damaged while traveling on the link, then the current code should be ok.
If we cannot make this assumption, then we need the receiver to verify all checksums before moving forward (which is what you are suggesting).
Quoted 8 lines
Quoted 2 lines
Is it truly possible for the kernel to hand ovpn a packet with invalid checksums on the TX path?
The networking stack shouldn't generate packets with broken checksums, but it could happen. On a VPN server that's giving access to an internal network, I think the packet could get corrupted on the internal network and may be pushed without verification into the tunnel.
Right.
In these cases the receiver would have a chance to detect and discard this packet.
With the current ovpn code, instead, we are saying "everything is good, don't check" and the packet would be delivered to the upper layer.
Ok, I think it makes sense to switch to CHECKSUM_NONE.
(I wonder what the wireguard guys think about it :-))
Quoted 4 lines
It's also possible to inject them with packet sockets for testing. Using scapy to send packets over your ovpn device should allow you to do that.
Thanks for the hint!
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 11/24 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/bind.c | 1 + drivers/net/ovpn/crypto.c | 162 ++++++++++++++ drivers/net/ovpn/crypto.h | 138 ++++++++++++ drivers/net/ovpn/crypto_aead.c | 378 +++++++++++++++++++++++++++++++++ drivers/net/ovpn/crypto_aead.h | 30 +++ drivers/net/ovpn/io.c | 158 +++++++++++--- drivers/net/ovpn/packet.h | 2 +- drivers/net/ovpn/peer.c | 24 +++ drivers/net/ovpn/peer.h | 14 ++ drivers/net/ovpn/pktid.c | 132 ++++++++++++ drivers/net/ovpn/pktid.h | 85 ++++++++ drivers/net/ovpn/socket.c | 1 + drivers/net/ovpn/udp.c | 1 + 14 files changed, 1104 insertions(+), 25 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/bind.c +1 -0
@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h" struct ovpn_bind *ovpn_bind_from_sockaddr (const struct sockaddr_storage *ss)
drivers/net/ovpn/crypto.c new file +162 -0
162 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 +378 -0
378 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 +134 -24
190 lines not on this page · raw message
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 +24 -0
@@ -13,6 +13,9 @@#include "ovpnstruct.h" #include "bind.h" +#include "packet.h" +#include "pktid.h" +#include "crypto.h" #include "io.h" #include "main.h" #include "netlink.h"
@@ -36,6 +39,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);
@@ -122,6 +126,7 @@ static void ovpn_peer_release_rcu(struct rcu_head *head) { struct ovpn_peer *peer = container_of(head, struct ovpn_peer, rcu); +ovpn_crypto_state_release (&peer->crypto);ovpn_peer_free (peer); }
@@ -334,6 +339,25 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,return peer; } +struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn, +struct sk_buff *skb) +{ +struct ovpn_peer *tmp, *peer =NULL ; + +/* in P2P mode, no matter the destination, packets are always sent to +* the single peer listening on the other side +*/ +if (ovpn->mode ==OVPN_MODE_P2P ) { +rcu_read_lock (); + tmp =rcu_dereference (ovpn->peer); +if (likely (tmp &&ovpn_peer_hold (tmp))) + peer = tmp; +rcu_read_unlock (); + } + +return peer; +} +/** * ovpn_peer_add_p2p - add per to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +14 -0
@@ -11,6 +11,8 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "socket.h" #include <linux/ptr_ring.h>
@@ -30,6 +32,7 @@* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI * @napi: NAPI object * @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
@@ -53,6 +56,7 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;struct ovpn_socket *sock; +struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -160,4 +164,14 @@ 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); +/** +* ovpn_peer_get_by_src - retrieve peer by matching skb source address +* @ovpn: the openvpn instance to search +* @skb: the packet to use for matching +* +* Return: the peer if found or NULL otherwise +*/ +struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn, +struct sk_buff *skb); +#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/pktid.c new file +132 -0
132 lines not on this page · raw message
drivers/net/ovpn/pktid.h new file +85 -0
85 lines not on this page · raw message
drivers/net/ovpn/socket.c +1 -0
@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "main.h" #include "io.h" +#include "packet.h" #include "peer.h" #include "socket.h" #include "udp.h"
drivers/net/ovpn/udp.c +1 -0
@@ -19,6 +19,7 @@#include "main.h" #include "bind.h" #include "io.h" +#include "packet.h" #include "peer.h" #include "proto.h" #include "socket.h"
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 11/24] ovpn: implement packet processing
drivers/net/ovpn/bind.c line 17
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/bind.c +1
-0@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h"
You have a few hunks like that in this patch, adding an include to a file that is otherwise not being modified. That's odd.
Quoted 9 lines
drivers/net/ovpn/crypto.c new file +3
-0@@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +*
drivers/net/ovpn/crypto.c line 13
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +10
-0+* Copyright (C) 2020-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#include <linux/types.h> +#include <linux/net.h> +#include <linux/netdevice.h> +//#include <linux/skbuff.h>
That's also odd :)
[...]
drivers/net/ovpn/crypto.c line 85
Quoted 13 lines
drivers/net/ovpn/crypto.c new file +13
-0+/* Reset the ovpn_crypto_state object in a way that is atomic +* to RCU readers. +*/ +int ovpn_crypto_state_reset (struct ovpn_crypto_state *cs, +const struct ovpn_peer_key_reset *pkr) + __must_hold(cs->mutex) +{ +struct ovpn_crypto_key_slot *old =NULL ; +struct ovpn_crypto_key_slot *new; + +lockdep_assert_held (&cs->mutex); + + new =ovpn_aead_crypto_key_slot_new (&pkr->key);
This doesn't need the lock to be held, you could move the lock to a smaller section (only around the pointer swap).
Quoted 4 lines
drivers/net/ovpn/crypto.c new file +4
-0+if (IS_ERR (new)) +return PTR_ERR (new); + +switch (pkr->slot) {
drivers/net/ovpn/crypto.c line 99
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +10
-0+case OVPN_KEY_SLOT_PRIMARY : + old =rcu_replace_pointer (cs->primary, new, +lockdep_is_held (&cs->mutex)); +break ; +case OVPN_KEY_SLOT_SECONDARY : + old =rcu_replace_pointer (cs->secondary, new, +lockdep_is_held (&cs->mutex)); +break ; +default : + goto free_key;
And validating pkr->slot before alloc could avoid a pointless alloc/free (and simplify the code: once _new() has succeeded, no failure can occur anymore).
Quoted 24 lines
drivers/net/ovpn/crypto.c new file +24
-0+ } + +if (old) +ovpn_crypto_key_slot_put (old); + +return 0 ; +free_key: +ovpn_crypto_key_slot_put (new); +return -EINVAL ; +} + +void ovpn_crypto_key_slot_delete (struct ovpn_crypto_state *cs, +enum ovpn_key_slot slot) +{ +struct ovpn_crypto_key_slot *ks =NULL ; + +mutex_lock (&cs->mutex); +switch (slot) { +case OVPN_KEY_SLOT_PRIMARY : + ks =rcu_replace_pointer (cs->primary,NULL , +lockdep_is_held (&cs->mutex)); +break ; +case OVPN_KEY_SLOT_SECONDARY : + ks = rcu_replace_pointer(cs->secondary, NULL,
drivers/net/ovpn/crypto.c line 133
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +10
-0+ lockdep_is_held(&cs->mutex)); +break ; +default : +pr_warn ("Invalid slot to release: %u\n" , slot); +break ; + } +mutex_unlock (&cs->mutex); + +if (!ks) { +pr_debug ("Key slot already released: %u\n" , slot);
This will also be printed in case of an invalid argument, which would be mildly confusing.
Quoted 20 lines
drivers/net/ovpn/crypto.c new file +18
-0+return ; + } +pr_debug ("deleting key slot %u, key_id=%u\n" , slot, ks->key_id); + +ovpn_crypto_key_slot_put (ks); +} +static struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_init (enum ovpn_cipher_alg alg, +const unsigned char *encrypt_key, +unsigned int encrypt_keylen, +const unsigned char *decrypt_key, +unsigned int decrypt_keylen, +const unsigned char *encrypt_nonce_tail, +unsigned int encrypt_nonce_tail_len, +const unsigned char *decrypt_nonce_tail, +unsigned int decrypt_nonce_tail_len, +u16 key_id) +{
[...]
drivers/net/ovpn/crypto.c
Quoted 6 lines
drivers/net/ovpn/crypto.c new file +6
-0+ +if (sizeof (struct ovpn_nonce_tail ) != encrypt_nonce_tail_len || +sizeof (struct ovpn_nonce_tail ) != decrypt_nonce_tail_len) { + ret = -EINVAL ; + goto destroy_ks; + }
Those checks could be done earlier, before bothering with any allocations.
Quoted 21 lines
drivers/net/ovpn/crypto.c new file +21
-0+ +memcpy (ks->nonce_tail_xmit.u8, encrypt_nonce_tail, +sizeof (struct ovpn_nonce_tail )); +memcpy (ks->nonce_tail_recv.u8, decrypt_nonce_tail, +sizeof (struct ovpn_nonce_tail )); + +/* init packet ID generation/validation */ +ovpn_pktid_xmit_init (&ks->pid_xmit); +ovpn_pktid_recv_init (&ks->pid_recv); + +return ks; + +destroy_ks: +ovpn_aead_crypto_key_slot_destroy (ks); +return ERR_PTR (ret); +} + +struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc) +{ + return ovpn_aead_crypto_key_slot_init(kc->cipher_alg,
drivers/net/ovpn/crypto.c
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +10
-0+ kc->encrypt.cipher_key, + kc->encrypt.cipher_key_size, + kc->decrypt.cipher_key, + kc->decrypt.cipher_key_size, + kc->encrypt.nonce_tail, + kc->encrypt.nonce_tail_size, + kc->decrypt.nonce_tail, + kc->decrypt.nonce_tail_size, + kc->key_id); +}
Why the wrapper? You could just call ovpn_aead_crypto_key_slot_init directly.
drivers/net/ovpn/io.c int ovpn_napi_poll(struct napi_struct *napi, int budget) · line 120
Quoted 12 lines
drivers/net/ovpn/io.c +4
-0@@ -110,6 +114,27 @@ int ovpn_napi_poll(struct napi_struct *napi, int budget)return work_done; } +/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static __be16 ovpn_ip_check_protocol (struct sk_buff *skb)
nit: if you put this function higher up in the patch that introduced it, you wouldn't have to move it now
Quoted 21 lines
drivers/net/ovpn/io.c +17
-0+{ +__be16 proto =0 ; + +/* skb could be non-linear, make sure IP header is in non-fragmented +* part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} +/* Entry point for processing an incoming packet (in skb form) * * Enqueue the packet and schedule RX consumer.
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 135
Quoted 4 lines
drivers/net/ovpn/io.c +0
-1@@ -132,7 +157,81 @@ int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer,static int ovpn_decrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) { -return true;
I missed that in the RX patch, true isn't an int :)
Were you intending this function to be bool like ovpn_encrypt_one? Since you're not actually using the returned value in the caller, it would be reasonable, but you'd have to convert all the <0 error values to bool.
Quoted 19 lines
drivers/net/ovpn/io.c +19
-0+struct ovpn_peer *allowed_peer =NULL ; +struct ovpn_crypto_key_slot *ks; +__be16 proto; +int ret =-1 ; +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); +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); + goto drop; + } + +/* decrypt */ + ret =ovpn_aead_decrypt (ks, skb); + +ovpn_crypto_key_slot_put (ks);
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 188
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ +if (unlikely (ret <0 )) { +net_err_ratelimited ("%s: error during decryption for peer %u, key-id %u: %d\n" , + peer->ovpn->dev->name, peer->id, key_id, + ret); + goto drop; + } + +/* check if this is a valid datapacket that has to be delivered to the +* tun interface
s/tun/ovpn/ ?
Quoted 26 lines
drivers/net/ovpn/io.c +26
-0+*/ +skb_reset_network_header (skb); + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto)) { +/* check if null packet */ +if (unlikely (!pskb_may_pull (skb,1 ))) { +netdev_dbg (peer->ovpn->dev, +"NULL packet received from peer %u\n" , + peer->id); + ret = -EINVAL ; + goto drop; + } + +netdev_dbg (peer->ovpn->dev, +"unsupported protocol received from peer %u\n" , + peer->id); + + ret = -EPROTONOSUPPORT ; + goto drop; + } + skb->protocol = proto; + +/* perform Reverse Path Filtering (RPF) */ + allowed_peer =ovpn_peer_get_by_src (peer->ovpn, skb); +if (unlikely (allowed_peer != peer)) { +if (skb_protocol_to_family (skb) ==AF_INET6 )
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 224
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+net_warn_ratelimited ("%s: RPF dropped packet from peer %u, src: %pI6c\n" , + peer->ovpn->dev->name, peer->id, + &ipv6_hdr (skb)->saddr); + else + net_warn_ratelimited("%s: RPF dropped packet from peer %u, src: %pI4\n", + peer->ovpn->dev->name, peer->id, + &ip_hdr(skb)->saddr); + ret = -EPERM ; + goto drop; + }
Have you considered holding rcu_read_lock around this whole RPF check? It would avoid taking a reference on the peer just to release it 3 lines later. And the same could likely be done for some of the other ovpn_peer_get_* lookups too.
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 234
Quoted 9 lines
drivers/net/ovpn/io.c +9
-0+ ret =ptr_ring_produce_bh (&peer->netif_rx_ring, skb); +drop: +if (likely (allowed_peer)) +ovpn_peer_put (allowed_peer); + +if (unlikely (ret <0 )) +kfree_skb (skb); + +return ret;
Mixing the drop/success returns looks kind of strange. This would be a bit simpler:
ovpn_peer_put(allowed_peer);
return ptr_ring_produce_bh(&peer->netif_rx_ring, skb);
drop:
if (allowed_peer)
ovpn_peer_put(allowed_peer);
kfree_skb(skb);
return ret;
drivers/net/ovpn/packet.h line 13
Quoted 10 lines
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
nit: that typo came in earlier
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 11/24] ovpn: implement packet processing
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/bind.c line 17
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/bind.c +1
-0@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h" You have a few hunks like that in this patch, adding an include to a file that is otherwise not being modified. That's odd.
Argh. The whole ovpn was originall a single patch, which I the went and divided in smaller changes for easier review.
As you may imagine this process is prone to mistakes like this, expecially when the number of patches is quite high...
I will go through all the patches and clean them up from issues like this and like the one below..
Sorry about that.
Quoted 25 lines
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +3
-0@@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* drivers/net/ovpn/crypto.c line 13
Quoted 10 lines
drivers/net/ovpn/crypto.c new file +10
-0+* Copyright (C) 2020-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#include <linux/types.h> +#include <linux/net.h> +#include <linux/netdevice.h> +//#include <linux/skbuff.h> That's also odd :)
[...]
Quoted 16 lines
drivers/net/ovpn/crypto.c line 85
Quoted 13 lines
drivers/net/ovpn/crypto.c new file +13
-0+/* Reset the ovpn_crypto_state object in a way that is atomic +* to RCU readers. +*/ +int ovpn_crypto_state_reset (struct ovpn_crypto_state *cs, +const struct ovpn_peer_key_reset *pkr) + __must_hold(cs->mutex) +{ +struct ovpn_crypto_key_slot *old =NULL ; +struct ovpn_crypto_key_slot *new; + +lockdep_assert_held (&cs->mutex); + + new =ovpn_aead_crypto_key_slot_new (&pkr->key);This doesn't need the lock to be held, you could move the lock to a smaller section (only around the pointer swap).
I think you're right. I also like the idea of shrinking the lock active area.. Will fix this!
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/crypto.c new file +10
-0+if (IS_ERR (new)) +return PTR_ERR (new); + +switch (pkr->slot) { +case OVPN_KEY_SLOT_PRIMARY : + old =rcu_replace_pointer (cs->primary, new, +lockdep_is_held (&cs->mutex)); +break ; +case OVPN_KEY_SLOT_SECONDARY : + old = rcu_replace_pointer(cs->secondary, new,
Quoted 8 lines
drivers/net/ovpn/crypto.c line 99
Quoted 4 lines
drivers/net/ovpn/crypto.c new file +4
-0+ lockdep_is_held(&cs->mutex)); +break ; +default : + goto free_key;And validating
pkr->slotbefore alloc could avoid a pointless alloc/free (and simplify the code: once_new()has succeeded, no failure can occur anymore).
right! will fix
Quoted 30 lines
Quoted 30 lines
drivers/net/ovpn/crypto.c new file +29
-0+ } + +if (old) +ovpn_crypto_key_slot_put (old); + +return 0 ; +free_key: +ovpn_crypto_key_slot_put (new); +return -EINVAL ; +} + +void ovpn_crypto_key_slot_delete (struct ovpn_crypto_state *cs, +enum ovpn_key_slot slot) +{ +struct ovpn_crypto_key_slot *ks =NULL ; + +mutex_lock (&cs->mutex); +switch (slot) { +case OVPN_KEY_SLOT_PRIMARY : + ks =rcu_replace_pointer (cs->primary,NULL , +lockdep_is_held (&cs->mutex)); +break ; +case OVPN_KEY_SLOT_SECONDARY : + ks =rcu_replace_pointer (cs->secondary,NULL , +lockdep_is_held (&cs->mutex)); +break ; +default : +pr_warn ("Invalid slot to release: %u\n" , slot); +break ;
Quoted 8 lines
drivers/net/ovpn/crypto.c line 133
Quoted 5 lines
drivers/net/ovpn/crypto.c new file +5
-0+ } +mutex_unlock (&cs->mutex); + +if (!ks) { +pr_debug ("Key slot already released: %u\n" , slot);This will also be printed in case of an invalid argument, which would be mildly confusing.
although we will have the pr_warn printed in as well that case. But I agree this is not nice. will fix
Quoted 22 lines
Quoted 21 lines
drivers/net/ovpn/crypto.c new file +18
-0+return ; + } +pr_debug ("deleting key slot %u, key_id=%u\n" , slot, ks->key_id); + +ovpn_crypto_key_slot_put (ks); +} +static struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_init (enum ovpn_cipher_alg alg, +const unsigned char *encrypt_key, +unsigned int encrypt_keylen, +const unsigned char *decrypt_key, +unsigned int decrypt_keylen, +const unsigned char *encrypt_nonce_tail, +unsigned int encrypt_nonce_tail_len, +const unsigned char *decrypt_nonce_tail, +unsigned int decrypt_nonce_tail_len, +u16 key_id) +{[...]
Quoted 9 lines
drivers/net/ovpn/crypto.c
Quoted 6 lines
drivers/net/ovpn/crypto.c new file +6
-0+ +if (sizeof (struct ovpn_nonce_tail ) != encrypt_nonce_tail_len || +sizeof (struct ovpn_nonce_tail ) != decrypt_nonce_tail_len) { + ret = -EINVAL ; + goto destroy_ks; + }Those checks could be done earlier, before bothering with any allocations.
ACK
Quoted 27 lines
Quoted 27 lines
drivers/net/ovpn/crypto.c new file +26
-0+ +memcpy (ks->nonce_tail_xmit.u8, encrypt_nonce_tail, +sizeof (struct ovpn_nonce_tail )); +memcpy (ks->nonce_tail_recv.u8, decrypt_nonce_tail, +sizeof (struct ovpn_nonce_tail )); + +/* init packet ID generation/validation */ +ovpn_pktid_xmit_init (&ks->pid_xmit); +ovpn_pktid_recv_init (&ks->pid_recv); + +return ks; + +destroy_ks: +ovpn_aead_crypto_key_slot_destroy (ks); +return ERR_PTR (ret); +} + +struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc) +{ + return ovpn_aead_crypto_key_slot_init(kc->cipher_alg, + kc->encrypt.cipher_key, + kc->encrypt.cipher_key_size, + kc->decrypt.cipher_key, + kc->decrypt.cipher_key_size, + kc->encrypt.nonce_tail,
Quoted 8 lines
drivers/net/ovpn/crypto.c
Quoted 5 lines
drivers/net/ovpn/crypto.c new file +5
-0+ kc->encrypt.nonce_tail_size, + kc->decrypt.nonce_tail, + kc->decrypt.nonce_tail_size, + kc->key_id); +}Why the wrapper? You could just call
ovpn_aead_crypto_key_slot_initdirectly.
Mostly for ahestetic reasons, being the call very large.
On top of that this is a little leftover from a previous version where this call happened more than once as part of an internal abstraction, hence the decision to create a wrapper.
But I think it's ok to remove it now.
Quoted 9 lines
Quoted 9 lines
drivers/net/ovpn/io.c +0
-0@@ -110,6 +114,27 @@ int ovpn_napi_poll(struct napi_struct *napi, int budget)return work_done; }
Quoted 7 lines
drivers/net/ovpn/io.c int ovpn_napi_poll(struct napi_struct *napi, int budget) · line 120
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static __be16 ovpn_ip_check_protocol (struct sk_buff *skb)nit: if you put this function higher up in the patch that introduced it, you wouldn't have to move it now
ACK
Quoted 23 lines
Quoted 23 lines
drivers/net/ovpn/io.c +17
-0+{ +__be16 proto =0 ; + +/* skb could be non-linear, make sure IP header is in non-fragmented +* part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} +/* Entry point for processing an incoming packet (in skb form) * * Enqueue the packet and schedule RX consumer. @@ -132,7 +157,81 @@ int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer,
Quoted 9 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 135
Quoted 3 lines
drivers/net/ovpn/io.c +0
-1static int ovpn_decrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) { -return true;I missed that in the RX patch, true isn't an int :)
Were you intending this function to be bool likeovpn_encrypt_one? Since you're not actually using the returned value in the caller, it would be reasonable, but you'd have to convert all the <0 error values to bool.
Mhh let me think what's best and I wil make this uniform.
Quoted 24 lines
Quoted 24 lines
drivers/net/ovpn/io.c +23
-0+struct ovpn_peer *allowed_peer =NULL ; +struct ovpn_crypto_key_slot *ks; +__be16 proto; +int ret =-1 ; +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); +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); + goto drop; + } + +/* decrypt */ + ret =ovpn_aead_decrypt (ks, skb); + +ovpn_crypto_key_slot_put (ks); + +if (unlikely (ret <0 )) { + net_err_ratelimited("%s: error during decryption for peer %u, key-id %u: %d\n", + peer->ovpn->dev->name, peer->id, key_id,
Quoted 8 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 188
Quoted 6 lines
drivers/net/ovpn/io.c +6
-0+ ret); + goto drop; + } + +/* check if this is a valid datapacket that has to be delivered to the +* tun interface s/tun/ovpn/ ?
yap. we used to call "tun" any interface used by openvpn...just legacy that fills our brains :-) will fix
Quoted 34 lines
Quoted 34 lines
drivers/net/ovpn/io.c +33
-0+*/ +skb_reset_network_header (skb); + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto)) { +/* check if null packet */ +if (unlikely (!pskb_may_pull (skb,1 ))) { +netdev_dbg (peer->ovpn->dev, +"NULL packet received from peer %u\n" , + peer->id); + ret = -EINVAL ; + goto drop; + } + +netdev_dbg (peer->ovpn->dev, +"unsupported protocol received from peer %u\n" , + peer->id); + + ret = -EPROTONOSUPPORT ; + goto drop; + } + skb->protocol = proto; + +/* perform Reverse Path Filtering (RPF) */ + allowed_peer =ovpn_peer_get_by_src (peer->ovpn, skb); +if (unlikely (allowed_peer != peer)) { +if (skb_protocol_to_family (skb) ==AF_INET6 ) +net_warn_ratelimited ("%s: RPF dropped packet from peer %u, src: %pI6c\n" , + peer->ovpn->dev->name, peer->id, + &ipv6_hdr (skb)->saddr); +else +net_warn_ratelimited ("%s: RPF dropped packet from peer %u, src: %pI4\n" , + peer->ovpn->dev->name, peer->id, + &ip_hdr (skb)->saddr);
Quoted 8 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 224
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+ ret = -EPERM ; + goto drop; + }Have you considered holding
rcu_read_lockaround this whole RPF check? It would avoid taking a reference on the peer just to release it 3 lines later. And the same could likely be done for some of the other ovpn_peer_get_* lookups too.
thinking about this..you're right, because the peer object never leavs this context and therefore it is not stricly needed to hold the reference and do the full dance..
Sometimes I fear that I may envelope too many instructions within the rcu_read_lock and thus I go with the smallest area needed (a bit like a classic lock). But I agree that for these lookups this is not truly the case.
Will review the other lookups and change them accordingly.
Thanks!
Quoted 15 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 234
Quoted 11 lines
drivers/net/ovpn/io.c +9
-0+ ret =ptr_ring_produce_bh (&peer->netif_rx_ring, skb); +drop: +if (likely (allowed_peer)) +ovpn_peer_put (allowed_peer); + +if (unlikely (ret <0 )) +kfree_skb (skb); + +return ret;Mixing the drop/success returns looks kind of strange. This would be a bit simpler:
Quoted 8 lines
ovpn_peer_put(allowed_peer);
returnptr_ring_produce_bh(&peer->netif_rx_ring, skb);drop:
if (allowed_peer)ovpn_peer_put(allowed_peer);
kfree_skb(skb);
return ret;
Honestly I have seen this pattern fairly often (and implemented it this way fairly often).
I presume it is mostly a matter of taste.
The idea is: when exiting a function 90% of the code is shared between success and failure, therefore let's just write it once and simply add a few branches based on ret.
This way we have less code and if we need to chang somethig in the exit path, we can change it once only.
A few examples:
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/translation-table.c#L813
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/routing.c#L269
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/mac80211/scan.c#L1344
ovpn code can be further simplified by setting skb to NULL in case of success (this way we avoid checking ret) and let ovpn_peer_put handle the case of peer == NULL (we avoid the NULL check before calling it).
What do you think?
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/packet.h +0
-0@@ -10,7 +10,7 @@#ifndef _NET_OVPN_PACKET_H_#define _NET_OVPN_PACKET_H_
Quoted 4 lines
drivers/net/ovpn/packet.h line 13
Quoted 2 lines
drivers/net/ovpn/packet.h +1
-1-/* When the OpenVPN protocol is ran in AEAD mode, use +/* When the OpenVPN protocol is run in AEAD mode, use nit: that typo came in earlier
ops
Thanks!
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 11/24] ovpn: implement packet processing
Quoted 19 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/bind.c line 17
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/bind.c +1
-0@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h" You have a few hunks like that in this patch, adding an include to a file that is otherwise not being modified. That's odd.
Argh. The whole ovpn was originall a single patch, which I the went and divided in smaller changes for easier review.
Quoted 7 lines
As you may imagine this process is prone to mistakes like this, expecially when the number of patches is quite high...
I will go through all the patches and clean them up from issues like this and like the one below..
Sorry about that.
Yep, I understand.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/net/ovpn/bind.c +9
-0+struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc) +{ + return ovpn_aead_crypto_key_slot_init(kc->cipher_alg, + kc->encrypt.cipher_key, + kc->encrypt.cipher_key_size, + kc->decrypt.cipher_key, + kc->decrypt.cipher_key_size, + kc->encrypt.nonce_tail,Quoted 8 lines
drivers/net/ovpn/bind.c
Quoted 5 lines
drivers/net/ovpn/bind.c +5
-0+ kc->encrypt.nonce_tail_size, + kc->decrypt.nonce_tail, + kc->decrypt.nonce_tail_size, + kc->key_id); +}Why the wrapper? You could just call
ovpn_aead_crypto_key_slot_initdirectly.
Quoted 1 line
Mostly for ahestetic reasons, being the call very large.
But that wrapper doesn't really do anything.
In case my previous comment wasn't clear: I would keep the single argument at the callsite (whether it's called _new or _init), and kill the 10-args variant (it's too verbose and _very_ easy to mess up).
Quoted 11 lines
Quoted 10 lines
drivers/net/ovpn/bind.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 134
Quoted 4 lines
drivers/net/ovpn/bind.c +0
-1@@ -132,7 +157,81 @@ int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer,static int ovpn_decrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) { -return true;I missed that in the RX patch, true isn't an int :)
Were you intending this function to be bool likeovpn_encrypt_one? Since you're not actually using the returned value in the caller, it would be reasonable, but you'd have to convert all the <0 error values to bool.
Quoted 1 line
Mhh let me think what's best and I wil make this uniform.
Yes please. If you can make the returns consistent (on success, one returns true and the other returns 0), it would be nice.
Quoted 13 lines
Quoted 13 lines
drivers/net/ovpn/bind.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 167
Quoted 9 lines
drivers/net/ovpn/bind.c +9
-0+ ret =ptr_ring_produce_bh (&peer->netif_rx_ring, skb); +drop: +if (likely (allowed_peer)) +ovpn_peer_put (allowed_peer); + +if (unlikely (ret <0 )) +kfree_skb (skb); + +return ret;Mixing the drop/success returns looks kind of strange. This would be a bit simpler:
Quoted 8 lines
Quoted 8 lines
ovpn_peer_put(allowed_peer);
returnptr_ring_produce_bh(&peer->netif_rx_ring, skb);drop:
if (allowed_peer)ovpn_peer_put(allowed_peer);
kfree_skb(skb);
return ret;
Scratch that, it's broken (we'd leak the skb if ptr_ring_produce_bh fails). Let's keep your version.
Quoted 4 lines
Honestly I have seen this pattern fairly often (and implemented it this way fairly often).
I presume it is mostly a matter of taste.
Maybe. As a reader I find it confusing to land into the "drop" label on success and conditionally free the skb.
Quoted 3 lines
The idea is: when exiting a function 90% of the code is shared between success and failure, therefore let's just write it once and simply add a few branches based on ret.
If it's 90%, yes. Here, it looked like very little common code.
Quoted 9 lines
This way we have less code and if we need to chang somethig in the exit path, we can change it once only.
A few examples:
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/translation-table.c#L813
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/routing.c#L269
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/mac80211/scan.c#L1344
Quoted 3 lines
ovpn code can be further simplified by setting skb to NULL in case of success (this way we avoid checking ret) and let
ovpn_peer_puthandle the case of peer == NULL (we avoid the NULL check before calling it).
That won't be needed if you don't take a reference. Anyway, netif_rx_ring will be gone if you switch to gro_cells, so that code is likely to change.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 11/24] ovpn: implement packet processing
Quoted 18 lines · Sabrina Dubroca
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
drivers/net/ovpn/crypto_aead.c new file +4
-0+struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc) +{ + return ovpn_aead_crypto_key_slot_init(kc->cipher_alg,drivers/net/ovpn/crypto_aead.c line 378
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+ kc->encrypt.cipher_key, + kc->encrypt.cipher_key_size, + kc->decrypt.cipher_key, + kc->decrypt.cipher_key_size, + kc->encrypt.nonce_tail, + kc->encrypt.nonce_tail_size, + kc->decrypt.nonce_tail, + kc->decrypt.nonce_tail_size, + kc->key_id); +}Why the wrapper? You could just call
ovpn_aead_crypto_key_slot_initdirectly.
Quoted 7 lines
Quoted 1 line
Mostly for ahestetic reasons, being the call very large.
But that wrapper doesn't really do anything.
In case my previous comment wasn't clear: I would keep the single argument at the callsite (whether it's called
_newor_init), and kill the 10-args variant (it's too verbose and _very_ easy to mess up).
Oh ok, then I misunderstood your earlier comment.
Now it's clear and I totally agree. Originally there was a crypto abstraction in ovpn, to allow more crypto families later on.
But I deemed it being too complex and overkill. This wrapper is a useless leftover of that approach.
Will get rid of this 10-args variant.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 134
Quoted 6 lines
drivers/net/ovpn/io.c +0
-1@@ -132,7 +157,81 @@ int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer,static int ovpn_decrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) { -return true;I missed that in the RX patch, true isn't an int :)
Were you intending this function to be bool likeovpn_encrypt_one? Since you're not actually using the returned value in the caller, it would be reasonable, but you'd have to convert all the <0 error values to bool.
Quoted 4 lines
Quoted 1 line
Mhh let me think what's best and I wil make this uniform.
Yes please. If you can make the returns consistent (on success, one returns true and the other returns 0), it would be nice.
I am normally all for int, as I don't like failing with no exact code. Will most likely go with that.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/net/ovpn/io.c int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, · line 234
Quoted 11 lines
drivers/net/ovpn/io.c +9
-0+ ret =ptr_ring_produce_bh (&peer->netif_rx_ring, skb); +drop: +if (likely (allowed_peer)) +ovpn_peer_put (allowed_peer); + +if (unlikely (ret <0 )) +kfree_skb (skb); + +return ret;Mixing the drop/success returns looks kind of strange. This would be a bit simpler:
ovpn_peer_put(allowed_peer);
returnptr_ring_produce_bh(&peer->netif_rx_ring, skb);
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
drop:
if (allowed_peer)ovpn_peer_put(allowed_peer);
kfree_skb(skb);
return ret;Scratch that, it's broken (we'd leak the skb if
ptr_ring_produce_bhfails). Let's keep your version.
Right.
Quoted 24 lines
Quoted 5 lines
Honestly I have seen this pattern fairly often (and implemented it this way fairly often).
I presume it is mostly a matter of taste.
Maybe. As a reader I find it confusing to land into the "drop" label on success and conditionally free the skb.
Quoted 3 lines
The idea is: when exiting a function 90% of the code is shared between success and failure, therefore let's just write it once and simply add a few branches based on ret.
If it's 90%, yes. Here, it looked like very little common code.
Quoted 9 lines
This way we have less code and if we need to chang somethig in the exit path, we can change it once only.
A few examples:
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/translation-table.c#L813
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/batman-adv/routing.c#L269
* https://elixir.bootlin.com/linux/v6.9-rc7/source/net/mac80211/scan.c#L1344
Quoted 7 lines
Quoted 3 lines
ovpn code can be further simplified by setting skb to NULL in case of success (this way we avoid checking ret) and let
ovpn_peer_puthandle the case of peer == NULL (we avoid the NULL check before calling it).That won't be needed if you don't take a reference. Anyway,
netif_rx_ringwill be gone if you switch togro_cells, so that code is likely to change.
Yap, working on gro_cells right now!
Thanks
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerRe: [PATCH net-next v3 11/24] ovpn: implement packet processing
In reply to Sabrina DubrocaQuoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/bind.c line 17
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/bind.c +1
-0@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h" You have a few hunks like that in this patch, adding an include to a file that is otherwise not being modified. That's odd.
I just went through this and there is a reason for these extra includes.
Basically this patch is modifying peer.h so that it now requires packet.h as dependency.
To reduce the includes complexity I am adding as many includes as possible to .c files only, therefore the dependency needs to appear in every .c file including peer.h, rather than adding the include to peer.h itself.
This was my interpretation of Andrew Lunn's suggestion, but I may have got it too extreme.
Opinions?
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v3 11/24] ovpn: implement packet processing
Quoted 21 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
drivers/net/ovpn/bind.c line 17
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/bind.c +1
-0@@ -13,6 +13,7 @@#include "ovpnstruct.h" #include "io.h" #include "bind.h" +#include "packet.h" #include "peer.h" You have a few hunks like that in this patch, adding an include to a file that is otherwise not being modified. That's odd.
I just went through this and there is a reason for these extra includes.
Basically this patch is modifying
peer.hso that it now requirespacket.has dependency.
Quoted 6 lines
To reduce the includes complexity I am adding as many includes as possible to .c files only, therefore the dependency needs to appear in every .c file including
peer.h, rather than adding the include topeer.hitself.This was my interpretation of Andrew Lunn's suggestion, but I may have got it too extreme.
It becomes an issue when adding one include pulls in 10s to 100s of other includes, 99% of which are not needed and just slows down the compile. With our own local headers, this is probably not going to happen.
Try using
make foo/bar/foobar.i
which will run cpp on foobar.c and produce foobar.i. You can then see the effects of the additional include. If it is minimum, you don't need to care much, and could always include it.
Andrew
Antonio Quartulli
maintainerPATCH v3 12/24 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 | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 13 +++++++--- drivers/net/ovpn/stats.c | 21 ++++++++++++++++ drivers/net/ovpn/stats.h | 52 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) 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 +10 -0
@@ -10,6 +10,7 @@#include <linux/netdevice.h> #include <linux/skbuff.h> #include <net/gso.h> +#include <net/ip.h> #include "ovpnstruct.h" #include "io.h"
@@ -19,6 +20,7 @@#include "crypto_aead.h" #include "netlink.h" #include "proto.h" +#include "socket.h" #include "udp.h" int ovpn_struct_init (struct net_device *dev)
@@ -163,6 +165,8 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)int ret =-1 ;u8 key_id; +ovpn_peer_stats_increment_rx (&peer->link_stats, skb->len); +/* get the key slot matching the key Id in the received packet */ key_id =ovpn_key_id_from_skb (skb); ks =ovpn_crypto_key_id_to_slot (&peer->crypto, key_id);
@@ -184,6 +188,9 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) goto drop; } +/* increment RX stats */ +ovpn_peer_stats_increment_rx (&peer->vpn_stats, skb->len); +/* check if this is a valid datapacket that has to be delivered to the * tun interface */
@@ -278,6 +285,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) goto err; } +ovpn_peer_stats_increment_tx (&peer->vpn_stats, skb->len); +/* encrypt */ ret =ovpn_aead_encrypt (ks, skb, peer->id);if (unlikely (ret <0 )) {
@@ -289,6 +298,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) success = true; +ovpn_peer_stats_increment_tx (&peer->link_stats, skb->len); err:ovpn_crypto_key_slot_put (ks);return success;
drivers/net/ovpn/peer.c +3 -0
@@ -20,6 +20,7 @@#include "main.h" #include "netlink.h" #include "peer.h" +#include "socket.h" struct ovpn_peer *ovpn_peer_new (struct ovpn_struct *ovpn,u32 id) {
@@ -42,6 +43,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock);kref_init (&peer->refcount); +ovpn_peer_stats_init (&peer->vpn_stats); +ovpn_peer_stats_init (&peer->link_stats);INIT_WORK (&peer->encrypt_work, ovpn_encrypt_work);INIT_WORK (&peer->decrypt_work, ovpn_decrypt_work);
drivers/net/ovpn/peer.h +9 -4
@@ -10,14 +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 <linux/ptr_ring.h> -#include <net/dst_cache.h> -#include <uapi/linux/ovpn.h> +#include "stats.h" /** * struct ovpn_peer - the main remote peer object
@@ -36,6 +37,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
@@ -60,6 +63,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/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 ); +atomic_set (&ps->rx.packets,0 ); + +atomic64_set (&ps->tx.bytes,0 ); +atomic_set (&ps->tx.packets,0 ); +}
drivers/net/ovpn/stats.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 12/24] ovpn: store tunnel and transport statistics
Quoted 5 lines · Antonio Quartulli
Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
Quoted 8 lines
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 13 +++++++--- drivers/net/ovpn/stats.c | 21 ++++++++++++++++ drivers/net/ovpn/stats.h | 52 +++++++++++++++++++++++++++++++++++++++
What I'm seeing in this patch are "success" counters. I don't see any stats for dropped packets that would help the user figure out why their VPN isn't working, or why their CPU is burning up decrypting packets that don't show up on the host, etc. You can guess there are issues by subtracting the link and vpn stats, but that's very limited.
For example:
- counter for packets dropped during the udp encap/decap
- counter for failed encrypt/decrypt (especially failed decrypt)
- counter for replay protection failures
- counter for malformed packets
Maybe not a separate counter for each of the prints you added in the rx/tx code, but at least enough of them to start figuring out what's going on without enabling all the prints and parsing dmesg.
Quoted 11 lines
drivers/net/ovpn/peer.h +3
-0@@ -10,14 +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>
drivers/net/ovpn/peer.h line 21
Quoted 10 lines
drivers/net/ovpn/peer.h +2
-4+#include "bind.h" #include "pktid.h" #include "crypto.h" #include "socket.h" - -#include <linux/ptr_ring.h> -#include <net/dst_cache.h> -#include <uapi/linux/ovpn.h> +#include "stats.h"
Header reshuffling got squashed into the wrong patch?
Quoted 11 lines
drivers/net/ovpn/stats.h new file +5
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2024 OpenVPN, Inc. +*
drivers/net/ovpn/stats.h line 15
Quoted 10 lines
drivers/net/ovpn/stats.h new file +10
-0+* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +* Lev Stipakov <[email protected]> +*/ + +#ifndef _NET_OVPN_OVPNSTATS_H_ +#define _NET_OVPN_OVPNSTATS_H_ + +//#include <linux/atomic.h> +//#include <linux/jiffies.h>
Forgot a clean up before posting? :)
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 12/24] ovpn: store tunnel and transport statistics
Quoted 15 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
Quoted 8 lines
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 13 +++++++--- drivers/net/ovpn/stats.c | 21 ++++++++++++++++ drivers/net/ovpn/stats.h | 52 +++++++++++++++++++++++++++++++++++++++
Quoted 5 lines
What I'm seeing in this patch are "success" counters. I don't see any stats for dropped packets that would help the user figure out why their VPN isn't working, or why their CPU is burning up decrypting packets that don't show up on the host, etc. You can guess there are issues by subtracting the link and vpn stats, but that's very limited.
This stats are just the bare minimum to make our current userspace happy :-)
But we can always extend the stats reporting later on, no?
Quoted 10 lines
For example:
- counter for packets dropped during the udp encap/decap
- counter for failed encrypt/decrypt (especially failed decrypt)
- counter for replay protection failures
- counter for malformed packetsMaybe not a separate counter for each of the prints you added in the rx/tx code, but at least enough of them to start figuring out what's going on without enabling all the prints and parsing dmesg.
Definitely a good suggestion! I'd just postpone it for later, unless you think it's a blocker.
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/peer.h +0
-0@@ -10,14 +10,15 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_
Quoted 15 lines
drivers/net/ovpn/peer.h line 21
Quoted 13 lines
drivers/net/ovpn/peer.h +5
-4+#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 <linux/ptr_ring.h> -#include <net/dst_cache.h> -#include <uapi/linux/ovpn.h> +#include "stats.h" Header reshuffling got squashed into the wrong patch?
indeed, darn. Juggling this many patches has been quite tedious
Quoted 17 lines
Quoted 17 lines
drivers/net/ovpn/stats.h new file +9
-0@@ -0,0 +1,52 @@ +/* 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]> +* Lev Stipakov <[email protected]> +*/
Quoted 8 lines
drivers/net/ovpn/stats.h line 15
Quoted 6 lines
drivers/net/ovpn/stats.h new file +6
-0+ +#ifndef _NET_OVPN_OVPNSTATS_H_ +#define _NET_OVPN_OVPNSTATS_H_ + +//#include <linux/atomic.h> +//#include <linux/jiffies.h> Forgot a clean up before posting? :)
Yeah..I guess I'll write a small script to catch all these things..it's easy to lose them across the whole patchset.
Thanks for spotting them! I will make sure they all go away
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 12/24] ovpn: store tunnel and transport statistics
Quoted 33 lines · Antonio Quartulli
Quoted 15 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
Quoted 8 lines
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 13 +++++++--- drivers/net/ovpn/stats.c | 21 ++++++++++++++++ drivers/net/ovpn/stats.h | 52 +++++++++++++++++++++++++++++++++++++++Quoted 5 lines
What I'm seeing in this patch are "success" counters. I don't see any stats for dropped packets that would help the user figure out why their VPN isn't working, or why their CPU is burning up decrypting packets that don't show up on the host, etc. You can guess there are issues by subtracting the link and vpn stats, but that's very limited.
This stats are just the bare minimum to make our current userspace happy :-)
But we can always extend the stats reporting later on, no?
Quoted 7 lines
For example:
- counter for packets dropped during the udp encap/decap
- counter for failed encrypt/decrypt (especially failed decrypt)
- counter for replay protection failures
- counter for malformed packets
Quoted 6 lines
Quoted 3 lines
Maybe not a separate counter for each of the prints you added in the rx/tx code, but at least enough of them to start figuring out what's going on without enabling all the prints and parsing dmesg.
Definitely a good suggestion! I'd just postpone it for later, unless you think it's a blocker.
I'm not sure. It's not strictly necessary to make the driver work, but from a user/admin's point of view, I think counters would be really useful.
Maybe at least increment the rx_dropped/rx_errors/etc counters from rtnl_link_stats on the netdevice?
Quoted 8 lines
indeed, darn. Juggling this many patches has been quite tedious
Yeah..I guess I'll write a small script to catch all these things..it's easy to lose them across the whole patchset.
Thanks for spotting them! I will make sure they all go away
Thanks. I know it's painful :(
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 12/24] ovpn: store tunnel and transport statistics
Quoted 41 lines · Sabrina Dubroca
Quoted 33 lines · Antonio Quartulli
Quoted 15 lines · Sabrina Dubroca
Quoted 5 lines · Antonio Quartulli
Byte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
Quoted 8 lines
Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 13 +++++++--- drivers/net/ovpn/stats.c | 21 ++++++++++++++++ drivers/net/ovpn/stats.h | 52 +++++++++++++++++++++++++++++++++++++++Quoted 5 lines
What I'm seeing in this patch are "success" counters. I don't see any stats for dropped packets that would help the user figure out why their VPN isn't working, or why their CPU is burning up decrypting packets that don't show up on the host, etc. You can guess there are issues by subtracting the link and vpn stats, but that's very limited.
This stats are just the bare minimum to make our current userspace happy :-)
But we can always extend the stats reporting later on, no?
Quoted 7 lines
For example:
- counter for packets dropped during the udp encap/decap
- counter for failed encrypt/decrypt (especially failed decrypt)
- counter for replay protection failures
- counter for malformed packetsQuoted 6 lines
Quoted 3 lines
Maybe not a separate counter for each of the prints you added in the rx/tx code, but at least enough of them to start figuring out what's going on without enabling all the prints and parsing dmesg.
Definitely a good suggestion! I'd just postpone it for later, unless you think it's a blocker.
Quoted 6 lines
I'm not sure. It's not strictly necessary to make the driver work, but from a user/admin's point of view, I think counters would be really useful.
Maybe at least increment the rx_dropped/rx_errors/etc counters from
rtnl_link_statson the netdevice?
Ok, will start with this and see how much work is to add the err counters right away.
Thanks for the hint!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 13/24 ovpn: implement TCP transport
In reply to Antonio QuartulliWith this changem ovpn is allowed to communicate to peers also via TCP.
--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 5 + drivers/net/ovpn/main.c | 9 +- drivers/net/ovpn/peer.h | 29 +++ drivers/net/ovpn/skb.h | 51 ++++ drivers/net/ovpn/socket.c | 20 ++ drivers/net/ovpn/socket.h | 15 +- drivers/net/ovpn/tcp.c | 511 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/tcp.h | 42 ++++ 9 files changed, 681 insertions(+), 2 deletions(-) create mode 100644 drivers/net/ovpn/skb.h create mode 100644 drivers/net/ovpn/tcp.c create mode 100644 drivers/net/ovpn/tcp.h
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 +5 -0
@@ -21,6 +21,7 @@#include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" int ovpn_struct_init (struct net_device *dev)
@@ -307,6 +308,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)/* Process packets in TX queue in a transport-specific way. * * UDP transport - encrypt and send across the tunnel. +* TCP transport - encrypt and put into TCP TX queue. */ void ovpn_encrypt_work (struct work_struct *work) {
@@ -340,6 +342,9 @@ void ovpn_encrypt_work(struct work_struct *work)ovpn_udp_send_skb (peer->ovpn, peer, curr);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, curr); +break ;default :/* no transport configured yet */ consume_skb (skb);
drivers/net/ovpn/main.c +8 -1
@@ -23,6 +23,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -247,8 +248,14 @@ static struct pernet_operations ovpn_pernet_ops = {static int __init ovpn_init (void ) { -int err =register_netdevice_notifier (&ovpn_netdev_notifier); +int err =ovpn_tcp_init (); +if (err) { +pr_err ("ovpn: cannot initialize TCP component: %d\n" , err); +return err; + } + + err =register_netdevice_notifier (&ovpn_netdev_notifier);if (err) {pr_err ("ovpn: can't register netdevice notifier: %d\n" , err);return err;
drivers/net/ovpn/peer.h +29 -0
@@ -33,6 +33,16 @@* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI * @napi: NAPI object * @sock: the socket being used to talk to this peer +* @tcp.tx_ring: queue for packets to be forwarded to userspace (TCP only) +* @tcp.tx_work: work for processing outgoing socket data (TCP only) +* @tcp.rx_work: wok for processing incoming socket data (TCP only) +* @tcp.raw_len: next packet length as read from the stream (TCP only) +* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only) +* @tcp.sk_cb.sk_data_ready: pointer to original cb +* @tcp.sk_cb.sk_write_space: pointer to original cb +* @tcp.sk_cb.prot: pointer to original prot object * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding
@@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;struct ovpn_socket *sock; +/* state of the TCP reading. Needed to keep track of how much of a +* single packet has already been read from the stream and how much is +* missing +*/ +struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)]; +struct sk_buff *skb; +u16 offset; +u16 data_len; +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;
drivers/net/ovpn/skb.h new file +51 -0
51 lines not on this page · raw message
drivers/net/ovpn/socket.c +20 -0
@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */
@@ -26,6 +27,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); }
@@ -69,6 +72,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; }
@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;kref_init (&ovpn_sock->refcount); +/* TCP sockets are per-peer, therefore they are linked to their unique +* peer +*/ +if (sock->sk->sk_protocol ==IPPROTO_TCP ) { + ovpn_sock->peer = peer; + ret =ptr_ring_init (&ovpn_sock->recv_ring,OVPN_QUEUE_LEN , +GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"%s: cannot allocate TCP recv ring\n" , + __func__); +kfree (ovpn_sock); +return ERR_PTR (ret); + } + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock;
drivers/net/ovpn/socket.h +14 -1
@@ -21,12 +21,25 @@ 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) +* @recv_ring: queue where non-data packets directed to userspace are stored * @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 { +/* the VPN session object owning this socket (UDP only) */ +struct ovpn_struct *ovpn; + +/* TCP only */ +struct { +/** @peer: unique peer transmitting over this socket */ +struct ovpn_peer *peer; +struct ptr_ring recv_ring; + }; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;
drivers/net/ovpn/tcp.c new file +511 -0
511 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.43.2
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Hi Simon,
On 06/05/2024 03:16, Antonio Quartulli wrote:
[...]
drivers/net/ovpn/peer.h line 39
Quoted 12 lines
drivers/net/ovpn/peer.h +4
-0@@ -33,6 +33,16 @@* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI * @napi: NAPI object * @sock: the socket being used to talk to this peer +* @tcp.tx_ring: queue for packets to be forwarded to userspace (TCP only) +* @tcp.tx_work: work for processing outgoing socket data (TCP only) +* @tcp.rx_work: wok for processing incoming socket data (TCP only) +* @tcp.raw_len: next packet length as read from the stream (TCP only)
can you please help me with the following warning from kerneldoc? As you can see below, raw_len is an array.
May that be the reason why the script isn't picking it up correctly?
drivers/net/ovpn/peer.h:101: warning: Function parameter or struct member 'raw_len' not described in 'ovpn_peer' drivers/net/ovpn/peer.h:101: warning: Excess struct member 'tcp.raw_len' description in 'ovpn_peer'
(line number may differ as I am in the middle of a rebase)
Regards,
Quoted 740 lines
drivers/net/ovpn/peer.h +25
-0+* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only) +* @tcp.sk_cb.sk_data_ready: pointer to original cb +* @tcp.sk_cb.sk_write_space: pointer to original cb +* @tcp.sk_cb.prot: pointer to original prot object * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding @@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;struct ovpn_socket *sock; +/* state of the TCP reading. Needed to keep track of how much of a +* single packet has already been read from the stream and how much is +* missing +*/ +struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)]; +struct sk_buff *skb; +u16 offset; +u16 data_len; +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;drivers/net/ovpn/skb.h new file +51
-0@@ -0,0 +1,51 @@ +/* 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_SKB_H_ +#define _NET_OVPN_SKB_H_ + +#include <linux/in.h> +#include <linux/in6.h> +#include <linux/ip.h> +#include <linux/skbuff.h> +#include <linux/socket.h> +#include <linux/types.h> + +#define OVPN_SKB_CB (skb) ((struct ovpn_skb_cb *)&((skb)->cb)) + +struct ovpn_skb_cb { +union { +struct in_addr ipv4; +struct in6_addr ipv6; + } local; +sa_family_t sa_fam; +}; + +/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static inline __be16 ovpn_ip_check_protocol (struct sk_buff *skb) +{ +__be16 proto =0 ; + +/* skb could be non-linear, +* make sure IP header is in non-fragmented part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} + +#endif /* _NET_OVPN_SKB_H_ */ drivers/net/ovpn/socket.c +20
-0@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */ @@ -26,6 +27,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); }@@ -69,6 +72,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; }@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;kref_init (&ovpn_sock->refcount); +/* TCP sockets are per-peer, therefore they are linked to their unique +* peer +*/ +if (sock->sk->sk_protocol ==IPPROTO_TCP ) { + ovpn_sock->peer = peer; + ret =ptr_ring_init (&ovpn_sock->recv_ring,OVPN_QUEUE_LEN , +GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"%s: cannot allocate TCP recv ring\n" , + __func__); +kfree (ovpn_sock); +return ERR_PTR (ret); + } + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock;drivers/net/ovpn/socket.h +14
-1@@ -21,12 +21,25 @@ 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) +* @recv_ring: queue where non-data packets directed to userspace are stored * @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 { +/* the VPN session object owning this socket (UDP only) */ +struct ovpn_struct *ovpn; + +/* TCP only */ +struct { +/** @peer: unique peer transmitting over this socket */ +struct ovpn_peer *peer; +struct ptr_ring recv_ring; + }; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;drivers/net/ovpn/tcp.c new file +511
-0@@ -0,0 +1,511 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#include <linux/ptr_ring.h> +#include <linux/skbuff.h> +#include <net/tcp.h> +#include <net/route.h> + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "packet.h" +#include "peer.h" +#include "proto.h" +#include "skb.h" +#include "socket.h" +#include "tcp.h" + +static struct proto ovpn_tcp_prot; + +static int ovpn_tcp_read_sock (read_descriptor_t *desc,struct sk_buff *in_skb, +unsigned int in_offset,size_t in_len) +{ +struct sock *sk = desc->arg.data; +struct ovpn_socket *sock; +struct ovpn_skb_cb *cb; +struct ovpn_peer *peer; +size_t chunk, copied =0 ; +void *data; +u16 len; +int st; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock (); + +if (unlikely (!sock || !sock->peer)) { +pr_err ("ovpn: read_sock triggered for socket with no metadata\n" ); + desc->error = -EINVAL ; +return 0 ; + } + + peer = sock->peer; + +while (in_len >0 ) { +/* no skb allocated means that we have to read (or finish +* reading) the 2 bytes prefix containing the actual packet +* size. +*/ +if (!peer->tcp.skb) { + chunk =min_t (size_t, in_len, +sizeof (u16) - peer->tcp.offset); +WARN_ON (skb_copy_bits (in_skb, in_offset, + peer->tcp.raw_len + + peer->tcp.offset, chunk) <0 ); + peer->tcp.offset += chunk; + +/* keep on reading until we got the whole packet size */ +if (peer->tcp.offset !=sizeof (u16)) + goto next_read; + + len =ntohs (*(__be16 *)peer->tcp.raw_len); +/* invalid packet length: this is a fatal TCP error */ +if (!len) { +netdev_err (peer->ovpn->dev, +"%s: received invalid packet length: %d\n" , + __func__, len); + desc->error = -EINVAL ; + goto err; + } + +/* add 2 bytes to allocated space (and immediately +* reserve them) for packet length prepending, in case +* the skb has to be forwarded to userspace +*/ + peer->tcp.skb = +netdev_alloc_skb_ip_align (peer->ovpn->dev, + len +sizeof (u16)); +if (!peer->tcp.skb) { + desc->error = -ENOMEM ; + goto err; + } +skb_reserve (peer->tcp.skb,sizeof (u16)); + + peer->tcp.offset =0 ; + peer->tcp.data_len = len; + }else { + chunk =min_t (size_t, in_len, + peer->tcp.data_len - peer->tcp.offset); + +/* extend skb to accommodate the new chunk and copy it +* from the input skb +*/ + data =skb_put (peer->tcp.skb, chunk); +WARN_ON (skb_copy_bits (in_skb, in_offset, data, + chunk) <0 ); + peer->tcp.offset += chunk; + +/* keep on reading until we get the full packet */ +if (peer->tcp.offset != peer->tcp.data_len) + goto next_read; + +/* do not perform IP caching for TCP connections */ + cb =OVPN_SKB_CB (peer->tcp.skb); + cb->sa_fam =AF_UNSPEC ; + +/* At this point we know the packet is from a configured +* peer. +* DATA_V2 packets are handled in kernel space, the rest +* goes to user space. +* +* Queue skb for sending to userspace via recvmsg on the +* socket +*/ +if (likely (ovpn_opcode_from_skb (peer->tcp.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)); + st =ovpn_recv (peer->ovpn, peer, peer->tcp.skb); +if (unlikely (st <0 )) +ovpn_peer_put (peer); + + }else { +/* prepend skb with packet len. this way +* userspace can parse the packet as if it just +* arrived from the remote endpoint +*/ +void *raw_len =__skb_push (peer->tcp.skb, +sizeof (u16)); + +memcpy (raw_len, peer->tcp.raw_len,sizeof (u16)); + + st =ptr_ring_produce_bh (&peer->sock->recv_ring, + peer->tcp.skb); +if (likely (!st)) + peer->tcp.sk_cb.sk_data_ready (sk); + } + +/* skb not consumed - free it now */ +if (unlikely (st <0 )) +kfree_skb (peer->tcp.skb); + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + } +next_read: + in_len -= chunk; + in_offset += chunk; + copied += chunk; + } + +return copied; +err: +netdev_err (peer->ovpn->dev,"cannot process incoming TCP data: %d\n" , + desc->error); +ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); +return 0 ; +} + +static void ovpn_tcp_data_ready (struct sock *sk) +{ +struct socket *sock = sk->sk_socket; +read_descriptor_t desc; + +if (unlikely (!sock || !sock->ops || !sock->ops->read_sock)) +return ; + + desc.arg.data = sk; + desc.error =0 ; + desc.count =1 ; + + sock->ops->read_sock (sk, &desc, ovpn_tcp_read_sock); +} + +static void ovpn_tcp_write_space (struct sock *sk) +{ +struct ovpn_socket *sock; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock (); + +if (!sock || !sock->peer) +return ; + +queue_work (sock->peer->ovpn->events_wq, &sock->peer->tcp.tx_work); +} + +static bool ovpn_tcp_sock_is_readable (struct sock *sk) + +{ +struct ovpn_socket *sock; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock (); + +if (!sock || !sock->peer) +return false; + +return !ptr_ring_empty_bh (&sock->recv_ring); +} + +static int ovpn_tcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len, +int flags,int *addr_len) +{ +bool tmp = flags &MSG_DONTWAIT ; +DEFINE_WAIT_FUNC (wait, woken_wake_function); +int ret, chunk, copied =0 ; +struct ovpn_socket *sock; +struct sk_buff *skb; +long timeo; + +if (unlikely (flags &MSG_ERRQUEUE )) +return sock_recv_errqueue (sk, msg, len,SOL_IP ,IP_RECVERR ); + + timeo =sock_rcvtimeo (sk, tmp); + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock (); + +if (!sock || !sock->peer) { + ret = -EBADF ; + goto unlock; + } + +while (ptr_ring_empty_bh (&sock->recv_ring)) { +if (sk->sk_shutdown &RCV_SHUTDOWN ) +return 0 ; + +if (sock_flag (sk,SOCK_DONE )) +return 0 ; + +if (!timeo) { + ret = -EAGAIN ; + goto unlock; + } + +add_wait_queue (sk_sleep (sk), &wait); +sk_set_bit (SOCKWQ_ASYNC_WAITDATA , sk); +sk_wait_event (sk, &timeo, !ptr_ring_empty_bh (&sock->recv_ring), + &wait); +sk_clear_bit (SOCKWQ_ASYNC_WAITDATA , sk); +remove_wait_queue (sk_sleep (sk), &wait); + +/* take care of signals */ +if (signal_pending (current)) { + ret =sock_intr_errno (timeo); + goto unlock; + } + } + +while (len && (skb =__ptr_ring_peek (&sock->recv_ring))) { + chunk =min_t (size_t, len, skb->len); + ret =skb_copy_datagram_msg (skb,0 , msg, chunk); +if (ret <0 ) { +pr_err ("ovpn: cannot copy TCP data to userspace: %d\n" , + ret); +kfree_skb (skb); + goto unlock; + } + +__skb_pull (skb, chunk); + +if (!skb->len) { +/* skb was entirely consumed and can now be removed from +* the ring +*/ +__ptr_ring_discard_one (&sock->recv_ring); +consume_skb (skb); + } + + len -= chunk; + copied += chunk; + } + ret = copied; + +unlock: +return ret ? : -EAGAIN ; +} + +static void ovpn_destroy_skb (void *skb) +{ +consume_skb (skb); +} + +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); +rcu_read_unlock (); + +if (!ovpn_sock->peer) +return ; + + peer = ovpn_sock->peer; + +/* restore CBs that were saved in ovpn_sock_set_tcp_cb() */ +write_lock_bh (&sock->sk->sk_callback_lock); + sock->sk->sk_data_ready = peer->tcp.sk_cb.sk_data_ready; + sock->sk->sk_write_space = peer->tcp.sk_cb.sk_write_space; + sock->sk->sk_prot = peer->tcp.sk_cb.prot; +rcu_assign_sk_user_data (sock->sk,NULL ); +write_unlock_bh (&sock->sk->sk_callback_lock); + +/* cancel any ongoing work. Done after removing the CBs so that these +* workers cannot be re-armed +*/ +cancel_work_sync (&peer->tcp.tx_work); + +ptr_ring_cleanup (&ovpn_sock->recv_ring, ovpn_destroy_skb); +ptr_ring_cleanup (&peer->tcp.tx_ring, ovpn_destroy_skb); +} + +/* Try to send one skb (or part of it) over the TCP stream. +* +* Return 0 on success or a negative error code otherwise. +* +* Note that the skb is modified by putting away the data being sent, therefore +* the caller should check if skb->len is zero to understand if the full skb was +* sent or not. +*/ +static int ovpn_tcp_send_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +struct msghdr msg = { .msg_flags =MSG_DONTWAIT |MSG_NOSIGNAL }; +struct kvec iv = {0 }; +int ret; + +if (skb_linearize (skb) <0 ) { +net_err_ratelimited ("%s: can't linearize packet\n" , __func__); +return -ENOMEM ; + } + +/* initialize iv structure now as skb_linearize() may have changed +* skb->data +*/ + iv.iov_base = skb->data; + iv.iov_len = skb->len; + + ret =kernel_sendmsg (peer->sock->sock, &msg, &iv,1 , iv.iov_len); +if (ret >0 ) { +__skb_pull (skb, ret); + +/* since we update per-cpu stats in process context, +* we need to disable softirqs +*/ +local_bh_disable (); +dev_sw_netstats_tx_add (peer->ovpn->dev,1 , ret); +local_bh_enable (); + +return 0 ; + } + +return ret; +} + +/* Process packets in TCP TX queue */ +static void ovpn_tcp_tx_work (struct work_struct *work) +{ +struct ovpn_peer *peer; +struct sk_buff *skb; +int ret; + + peer = container_of(work, struct ovpn_peer, tcp.tx_work); +while ((skb =__ptr_ring_peek (&peer->tcp.tx_ring))) { + ret =ovpn_tcp_send_one (peer, skb); +if (ret <0 && ret != -EAGAIN ) { +net_warn_ratelimited ("%s: cannot send TCP packet to peer %u: %d\n" , + __func__, peer->id, ret); +/* in case of TCP error stop sending loop and delete +* peer +*/ +ovpn_peer_del (peer, +OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); +break ; + }else if (!skb->len) { +/* skb was entirely consumed and can now be removed from +* the ring +*/ +__ptr_ring_discard_one (&peer->tcp.tx_ring); +consume_skb (skb); + } + +/* give a chance to be rescheduled if needed */ +cond_resched (); + } +} + +/* Put packet into TCP TX queue and schedule a consumer */ +void ovpn_queue_tcp_skb (struct ovpn_peer *peer,struct sk_buff *skb) +{ +int ret; + + ret =ptr_ring_produce_bh (&peer->tcp.tx_ring, skb); +if (ret <0 ) { +kfree_skb_list (skb); +return ; + } + +queue_work (peer->ovpn->events_wq, &peer->tcp.tx_work); +} + +/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +void *old_data; +int ret; + +INIT_WORK (&peer->tcp.tx_work, ovpn_tcp_tx_work); + + ret =ptr_ring_init (&peer->tcp.tx_ring,OVPN_QUEUE_LEN ,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"cannot allocate TCP TX ring\n" ); +return ret; + } + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + +write_lock_bh (&sock->sk->sk_callback_lock); + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock (); +if (old_data) { +netdev_err (peer->ovpn->dev, +"provided socket already taken by other user\n" ); + ret = -EBUSY ; + goto err; + } + +/* sanity check */ +if (sock->sk->sk_protocol !=IPPROTO_TCP ) { +netdev_err (peer->ovpn->dev, +"provided socket is UDP but expected TCP\n" ); + ret = -EINVAL ; + goto err; + } + +/* only a fully connected socket are expected. Connection should be +* handled in userspace +*/ +if (sock->sk->sk_state !=TCP_ESTABLISHED ) { +netdev_err (peer->ovpn->dev, +"provided TCP socket is not in ESTABLISHED state: %d\n" , + sock->sk->sk_state); + ret = -EINVAL ; + goto err; + } + +/* save current CBs so that they can be restored upon socket release */ + peer->tcp.sk_cb.sk_data_ready = sock->sk->sk_data_ready; + peer->tcp.sk_cb.sk_write_space = sock->sk->sk_write_space; + peer->tcp.sk_cb.prot = sock->sk->sk_prot; + +/* assign our static CBs */ + sock->sk->sk_data_ready = ovpn_tcp_data_ready; + sock->sk->sk_write_space = ovpn_tcp_write_space; + sock->sk->sk_prot = &ovpn_tcp_prot; + +write_unlock_bh (&sock->sk->sk_callback_lock); + +return 0 ; +err: +write_unlock_bh (&sock->sk->sk_callback_lock); +ptr_ring_cleanup (&peer->tcp.tx_ring,NULL ); + +return ret; +} + +int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. +* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot; + ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg; + ovpn_tcp_prot.sock_is_readable = ovpn_tcp_sock_is_readable; + +return 0 ; +}drivers/net/ovpn/tcp.h new file +42
-0@@ -0,0 +1,42 @@ +/* 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_TCP_H_ +#define _NET_OVPN_TCP_H_ + +#include <linux/net.h> +#include <linux/skbuff.h> +#include <linux/types.h> +#include <linux/workqueue.h> + +#include "peer.h" + +/* Initialize TCP static objects */ +int __init ovpn_tcp_init (void ); + +void ovpn_queue_tcp_skb (struct ovpn_peer *peer,struct sk_buff *skb); + +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer); +void ovpn_tcp_socket_detach (struct socket *sock); + +/* Prepare skb and enqueue it for sending to peer. +* +* Preparation consist in prepending the skb payload with its size. +* Required by the OpenVPN protocol in order to extract packets from +* the TCP stream on the receiver side. +*/ +static inline void ovpn_tcp_send_skb (struct ovpn_peer *peer, +struct sk_buff *skb) +{ +u16 len = skb->len; + + *(__be16 *)__skb_push (skb,sizeof (u16)) =htons (len); +ovpn_queue_tcp_skb (peer, skb); +} + +#endif /* _NET_OVPN_TCP_H_ */
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Jakub Kicinski
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 11 lines · On Mon, 13 May 2024 15:37:54 +0200 Antonio Quartulli wrote:
Quoted 7 lines
* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI
* @napi: NAPI object
* @sock: the socket being used to talk to this peer
+ * @tcp.tx_ring: queue for packets to be forwarded to userspace (TCP only)
+ * @tcp.tx_work: work for processing outgoing socket data (TCP only)
+ * @tcp.rx_work: wok for processing incoming socket data (TCP only)
+ * @tcp.raw_len: next packet length as read from the stream (TCP only)can you please help me with the following warning from kerneldoc? As you can see below,
raw_lenis an array.
Quoted 8 lines
May that be the reason why the script isn't picking it up correctly?
drivers/net/ovpn/peer.h:101: warning: Function parameter or struct member 'raw_len' not described in 'ovpn_peer'drivers/net/ovpn/peer.h:101: warning: Excess struct member 'tcp.raw_len' description in 'ovpn_peer'(line number may differ as I am in the middle of a rebase)
Hm, the script itself is a fairly simple file of perl regexps You can try to tweak it and send a fix to the list.
I presume using sizeof() to declare an array is fairly uncommon. Or forgo the sizeof() and use literal 2? :)
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
In reply to Antonio QuartulliQuoted 31 lines · Antonio Quartulli
drivers/net/ovpn/io.c +4
-0@@ -307,6 +308,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)/* Process packets in TX queue in a transport-specific way. * * UDP transport - encrypt and send across the tunnel. +* TCP transport - encrypt and put into TCP TX queue. */ void ovpn_encrypt_work (struct work_struct *work) {@@ -340,6 +342,9 @@ void ovpn_encrypt_work(struct work_struct *work)ovpn_udp_send_skb (peer->ovpn, peer, curr);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, curr); +break ;default :/* no transport configured yet */ consume_skb (skb);drivers/net/ovpn/main.c +1
-0@@ -23,6 +23,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
drivers/net/ovpn/main.c static struct pernet_operations ovpn_pernet_ops = { · line 253
Quoted 7 lines
drivers/net/ovpn/main.c +2
-1@@ -247,8 +248,14 @@ static struct pernet_operations ovpn_pernet_ops = {static int __init ovpn_init (void ) { -int err =register_netdevice_notifier (&ovpn_netdev_notifier); +int err =ovpn_tcp_init (); +if (err) {
ovpn_tcp_init cannot fail (and if it could, you'd need to clean up when register_netdevice_notifier fails). I'd make ovpn_tcp_init void and kill this check.
Quoted 13 lines
drivers/net/ovpn/main.c +5
-0+pr_err ("ovpn: cannot initialize TCP component: %d\n" , err); +return err; + } + + err =register_netdevice_notifier (&ovpn_netdev_notifier);if (err) {pr_err ("ovpn: can't register netdevice notifier: %d\n" , err);return err;drivers/net/ovpn/peer.h +0
-0
drivers/net/ovpn/peer.h line 38
Quoted 6 lines
drivers/net/ovpn/peer.h +3
-0@@ -33,6 +33,16 @@* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI * @napi: NAPI object * @sock: the socket being used to talk to this peer +* @tcp.tx_ring: queue for packets to be forwarded to userspace (TCP only) +* @tcp.tx_work: work for processing outgoing socket data (TCP only) +* @tcp.rx_work: wok for processing incoming socket data (TCP only)
Never actually used.
If you keep it: s/wok/work/
drivers/net/ovpn/peer.h line 42
Quoted 4 lines
drivers/net/ovpn/peer.h +4
-0+* @tcp.raw_len: next packet length as read from the stream (TCP only) +* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only)
It would be nice to add information about whether they're used for TX or RX.
Quoted 10 lines
drivers/net/ovpn/peer.h +3
-0+* @tcp.sk_cb.sk_data_ready: pointer to original cb +* @tcp.sk_cb.sk_write_space: pointer to original cb +* @tcp.sk_cb.prot: pointer to original prot object * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding @@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;struct ovpn_socket *sock;
drivers/net/ovpn/peer.h struct ovpn_peer { · line 81
Quoted 10 lines
drivers/net/ovpn/peer.h +10
-0+/* state of the TCP reading. Needed to keep track of how much of a +* single packet has already been read from the stream and how much is +* missing +*/ +struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)];
Why not u16 or __be16 for this one?
Quoted 41 lines
drivers/net/ovpn/peer.h +9
-0+struct sk_buff *skb; +u16 offset; +u16 data_len; +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;drivers/net/ovpn/skb.h new file +23
-0@@ -0,0 +1,51 @@ +/* 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_SKB_H_ +#define _NET_OVPN_SKB_H_ + +#include <linux/in.h> +#include <linux/in6.h> +#include <linux/ip.h> +#include <linux/skbuff.h> +#include <linux/socket.h> +#include <linux/types.h> + +#define OVPN_SKB_CB(skb) ((struct ovpn_skb_cb *)&((skb)->cb)) + +struct ovpn_skb_cb { + union {
drivers/net/ovpn/skb.h line 33
Quoted 10 lines
drivers/net/ovpn/skb.h new file +10
-0+struct in_addr ipv4; +struct in6_addr ipv6; + } local; +sa_family_t sa_fam; +}; + +/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static inline __be16 ovpn_ip_check_protocol (struct sk_buff *skb)
A dupe of this function exists in drivers/net/ovpn/io.c. I guess you can just introduce skb.h from the start (with only ovpn_ip_check_protocol at first).
Quoted 49 lines
drivers/net/ovpn/skb.h new file +18
-0+{ +__be16 proto =0 ; + +/* skb could be non-linear, +* make sure IP header is in non-fragmented part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} + +#endif /* _NET_OVPN_SKB_H_ */ drivers/net/ovpn/socket.c +5
-0@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */ @@ -26,6 +27,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); }@@ -69,6 +72,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; }
drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 129
Quoted 1 line
drivers/net/ovpn/socket.c +0
-0@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;
The line above this is:
ovpn_sock->ovpn = peer->ovpn;
It's technically fine since you then overwrite this with peer in case we're on TCP, but ovpn_sock->ovpn only exists on UDP since you moved it into a union in this patch.
drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 141
Quoted 12 lines
drivers/net/ovpn/socket.c +10
-0kref_init (&ovpn_sock->refcount); +/* TCP sockets are per-peer, therefore they are linked to their unique +* peer +*/ +if (sock->sk->sk_protocol ==IPPROTO_TCP ) { + ovpn_sock->peer = peer; + ret =ptr_ring_init (&ovpn_sock->recv_ring,OVPN_QUEUE_LEN , +GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"%s: cannot allocate TCP recv ring\n" , + __func__);
Should you also call ovpn_socket_detach here? (as well when the kzalloc for ovpn_sock fails a bit earlier)
Quoted 16 lines
drivers/net/ovpn/socket.c +5
-0+kfree (ovpn_sock); +return ERR_PTR (ret); + } + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock;drivers/net/ovpn/socket.h +0
-0@@ -21,12 +21,25 @@ struct ovpn_peer;/** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only)
drivers/net/ovpn/socket.h struct ovpn_peer; · line 32
Quoted 10 lines
drivers/net/ovpn/socket.h +4
-1+* @peer: unique peer transmitting over this socket (TCP only) +* @recv_ring: queue where non-data packets directed to userspace are stored * @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 { +/* the VPN session object owning this socket (UDP only) */
nit: Probably not needed
drivers/net/ovpn/socket.h struct ovpn_peer; · line 37
Quoted 5 lines
drivers/net/ovpn/socket.h +5
-0+struct ovpn_struct *ovpn; + +/* TCP only */ +struct { +/** @peer: unique peer transmitting over this socket */
Is kdoc upset about peer but not recv_ring?
Quoted 44 lines
drivers/net/ovpn/socket.h +5
-0+struct ovpn_peer *peer; +struct ptr_ring recv_ring; + }; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;drivers/net/ovpn/tcp.c new file +30
-0@@ -0,0 +1,511 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#include <linux/ptr_ring.h> +#include <linux/skbuff.h> +#include <net/tcp.h> +#include <net/route.h> + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "packet.h" +#include "peer.h" +#include "proto.h" +#include "skb.h" +#include "socket.h" +#include "tcp.h" + +static struct proto ovpn_tcp_prot; + +static int ovpn_tcp_read_sock (read_descriptor_t *desc,struct sk_buff *in_skb, +unsigned int in_offset,size_t in_len) +{ +struct sock *sk = desc->arg.data; +struct ovpn_socket *sock;
drivers/net/ovpn/tcp.c line 40
Quoted 10 lines
drivers/net/ovpn/tcp.c new file +10
-0+struct ovpn_skb_cb *cb; +struct ovpn_peer *peer; +size_t chunk, copied =0 ; +void *data; +u16 len; +int st; + +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock ();
You can't just release rcu_read_lock and keep using sock (here and in the rest of this file). Either you keep rcu_read_lock, or you can take a reference on the ovpn_socket.
Anyway, this looks like you're reinventing strparser. Overall this is very similar to net/xfrm/espintcp.c, but the receive side of espintcp uses strp and is much shorter (recv_ring looks equivalent to ike_queue, both sending a few messages to userspace -- look for strp_init, espintcp_rcv, espintcp_parse in that file).
Quoted 19 lines
drivers/net/ovpn/tcp.c new file +19
-0+/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +void *old_data; +int ret; + +INIT_WORK (&peer->tcp.tx_work, ovpn_tcp_tx_work); + + ret =ptr_ring_init (&peer->tcp.tx_ring,OVPN_QUEUE_LEN ,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"cannot allocate TCP TX ring\n" ); +return ret; + } + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + +write_lock_bh (&sock->sk->sk_callback_lock);
drivers/net/ovpn/tcp.c line 450
Quoted 10 lines
drivers/net/ovpn/tcp.c new file +10
-0+ +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock (); +if (old_data) { +netdev_err (peer->ovpn->dev, +"provided socket already taken by other user\n" ); + ret = -EBUSY ; + goto err;
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
Quoted 5 lines
drivers/net/ovpn/tcp.c new file +5
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets.
drivers/net/ovpn/tcp.c line 506
Quoted 10 lines
drivers/net/ovpn/tcp.c new file +10
-0+* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. +* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;
Don't you need a separate variant for IPv6, like TLS does?
drivers/net/ovpn/tcp.c line 507
Quoted 1 line
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;
You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 38 lines · Sabrina Dubroca
Quoted 37 lines · Antonio Quartulli
drivers/net/ovpn/io.c +4
-0@@ -307,6 +308,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)/* Process packets in TX queue in a transport-specific way. * * UDP transport - encrypt and send across the tunnel. +* TCP transport - encrypt and put into TCP TX queue. */ void ovpn_encrypt_work (struct work_struct *work) {@@ -340,6 +342,9 @@ void ovpn_encrypt_work(struct work_struct *work)ovpn_udp_send_skb (peer->ovpn, peer, curr);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, curr); +break ;default :/* no transport configured yet */ consume_skb (skb);drivers/net/ovpn/main.c +2
-1@@ -23,6 +23,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"@@ -247,8 +248,14 @@ static struct pernet_operations ovpn_pernet_ops = {static int __init ovpn_init (void ) { -int err =register_netdevice_notifier (&ovpn_netdev_notifier); +int err =ovpn_tcp_init ();
Quoted 5 lines
drivers/net/ovpn/main.c static struct pernet_operations ovpn_pernet_ops = { · line 253
Quoted 1 line
drivers/net/ovpn/main.c +1
-0+if (err) {
ovpn_tcp_initcannot fail (and if it could, you'd need to clean up whenregister_netdevice_notifierfails). I'd makeovpn_tcp_initvoid and kill this check.
I like to have all init functions returning int by design, even though they may not fail.
But I can undersand this is not necessarily good practice (somebody will always ask "when does it fail?" and there will will be no answer, which is confusing)
Quoted 15 lines
Quoted 15 lines
drivers/net/ovpn/main.c +5
-0+pr_err ("ovpn: cannot initialize TCP component: %d\n" , err); +return err; + } + + err =register_netdevice_notifier (&ovpn_netdev_notifier);if (err) {pr_err ("ovpn: can't register netdevice notifier: %d\n" , err);return err;drivers/net/ovpn/peer.h +0
-0@@ -33,6 +33,16 @@* @netif_rx_ring: queue of packets to be sent to the netdevice via NAPI
Quoted 8 lines
drivers/net/ovpn/peer.h line 38
Quoted 5 lines
drivers/net/ovpn/peer.h +3
-0* @napi: NAPI object * @sock: the socket being used to talk to this peer +* @tcp.tx_ring: queue for packets to be forwarded to userspace (TCP only) +* @tcp.tx_work: work for processing outgoing socket data (TCP only) +* @tcp.rx_work: wok for processing incoming socket data (TCP only) Never actually used.
If you keep it: s/wok/work/
Indeed, I think this is another leftover.
Quoted 7 lines
drivers/net/ovpn/peer.h line 42
Quoted 5 lines
drivers/net/ovpn/peer.h +4
-0+* @tcp.raw_len: next packet length as read from the stream (TCP only) +* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only) It would be nice to add information about whether they're used for TX or RX.
they are all about "from the stream" and "to the skb", meaning that we are doing RX. Will make it more explicit.
Quoted 15 lines
Quoted 15 lines
drivers/net/ovpn/peer.h +7
-0+* @tcp.sk_cb.sk_data_ready: pointer to original cb +* @tcp.sk_cb.sk_write_space: pointer to original cb +* @tcp.sk_cb.prot: pointer to original prot object * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding @@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;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 +*/
Quoted 8 lines
drivers/net/ovpn/peer.h struct ovpn_peer { · line 81
Quoted 6 lines
drivers/net/ovpn/peer.h +6
-0+struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)];Why not u16 or
__be16for this one?
because in this array we are putting the bytes as we get them from the stream.
We may be at the point where one out of two bytes is available on the stream. For this reason I use an array to store this u16 byte by byte.
Once thw two bytes are ready, we convert the content in an actual int and store it in "data_len" (a few lines below).
Quoted 48 lines
Quoted 48 lines
drivers/net/ovpn/peer.h +9
-0+struct sk_buff *skb; +u16 offset; +u16 data_len; +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;drivers/net/ovpn/skb.h new file +29
-0@@ -0,0 +1,51 @@ +/* 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_SKB_H_ +#define _NET_OVPN_SKB_H_ + +#include <linux/in.h> +#include <linux/in6.h> +#include <linux/ip.h> +#include <linux/skbuff.h> +#include <linux/socket.h> +#include <linux/types.h> + +#define OVPN_SKB_CB (skb) ((struct ovpn_skb_cb *)&((skb)->cb)) + +struct ovpn_skb_cb { +union { +struct in_addr ipv4; +struct in6_addr ipv6; + } local; +sa_family_t sa_fam; +}; +
Quoted 8 lines
drivers/net/ovpn/skb.h line 33
Quoted 4 lines
drivers/net/ovpn/skb.h new file +4
-0+/* Return IP protocol version from skb header. +* Return 0 if protocol is not IPv4/IPv6 or cannot be read. +*/ +static inline __be16 ovpn_ip_check_protocol (struct sk_buff *skb)A dupe of this function exists in
drivers/net/ovpn/io.c. I guess you can just introduceskb.hfrom the start (with onlyovpn_ip_check_protocolat first).
thanks. I think that was the idea, but something went horribly wrong.
Quoted 54 lines
Quoted 50 lines
drivers/net/ovpn/skb.h new file +18
-0+{ +__be16 proto =0 ; + +/* skb could be non-linear, +* make sure IP header is in non-fragmented part +*/ +if (!pskb_network_may_pull (skb,sizeof (struct iphdr ))) +return 0 ; + +if (ip_hdr (skb)->version ==4 ) + proto =htons (ETH_P_IP ); +else if (ip_hdr (skb)->version ==6 ) + proto =htons (ETH_P_IPV6 ); + +return proto; +} + +#endif /* _NET_OVPN_SKB_H_ */ drivers/net/ovpn/socket.c +5
-0@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */ @@ -26,6 +27,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); }@@ -69,6 +72,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; }drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 129
Quoted 1 line
drivers/net/ovpn/socket.c +0
-0@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;The line above this is:
Quoted 5 lines
ovpn_sock->ovpn = peer->ovpn;It's technically fine since you then overwrite this with peer in case we're on TCP, but
ovpn_sock->ovpnonly exists on UDP since you moved it into a union in this patch.
Yeah, I did not want to make another branch, but having a UDP specific case will make code easier to read.
Quoted 16 lines
drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 141
Quoted 13 lines
drivers/net/ovpn/socket.c +10
-0kref_init (&ovpn_sock->refcount); +/* TCP sockets are per-peer, therefore they are linked to their unique +* peer +*/ +if (sock->sk->sk_protocol ==IPPROTO_TCP ) { + ovpn_sock->peer = peer; + ret =ptr_ring_init (&ovpn_sock->recv_ring,OVPN_QUEUE_LEN , +GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"%s: cannot allocate TCP recv ring\n" , + __func__);Should you also call
ovpn_socket_detachhere? (as well when the kzalloc forovpn_sockfails a bit earlier)
mh, the attach is performed as first thing when we enter this function therefore you are right. we must undo the attach in case of failure.
Quoted 30 lines
Quoted 17 lines
drivers/net/ovpn/socket.c +5
-0+kfree (ovpn_sock); +return ERR_PTR (ret); + } + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock;drivers/net/ovpn/socket.h +0
-0@@ -21,12 +21,25 @@ struct ovpn_peer;/** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) drivers/net/ovpn/socket.h struct ovpn_peer; · line 32
Quoted 10 lines
drivers/net/ovpn/socket.h +4
-1+* @peer: unique peer transmitting over this socket (TCP only) +* @recv_ring: queue where non-data packets directed to userspace are stored * @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 { +/* the VPN session object owning this socket (UDP only) */ nit: Probably not needed
Quoted 7 lines
drivers/net/ovpn/socket.h struct ovpn_peer; · line 37
Quoted 5 lines
drivers/net/ovpn/socket.h +5
-0+struct ovpn_struct *ovpn; + +/* TCP only */ +struct { +/** @peer: unique peer transmitting over this socket */ Is kdoc upset about peer but not
recv_ring?
leftovers from before having the kdoc. I am removing them.
Quoted 51 lines
Quoted 51 lines
drivers/net/ovpn/socket.h +5
-0+struct ovpn_peer *peer; +struct ptr_ring recv_ring; + }; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;drivers/net/ovpn/tcp.c new file +36
-0@@ -0,0 +1,511 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#include <linux/ptr_ring.h> +#include <linux/skbuff.h> +#include <net/tcp.h> +#include <net/route.h> + +#include "ovpnstruct.h" +#include "main.h" +#include "io.h" +#include "packet.h" +#include "peer.h" +#include "proto.h" +#include "skb.h" +#include "socket.h" +#include "tcp.h" + +static struct proto ovpn_tcp_prot; + +static int ovpn_tcp_read_sock (read_descriptor_t *desc,struct sk_buff *in_skb, +unsigned int in_offset,size_t in_len) +{ +struct sock *sk = desc->arg.data; +struct ovpn_socket *sock; +struct ovpn_skb_cb *cb; +struct ovpn_peer *peer; +size_t chunk, copied =0 ; +void *data; +u16 len; +int st;
Quoted 8 lines
drivers/net/ovpn/tcp.c line 40
Quoted 4 lines
drivers/net/ovpn/tcp.c new file +4
-0+ +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock ();You can't just release
rcu_read_lockand keep using sock (here and in the rest of this file). Either you keeprcu_read_lock, or you can take a reference on theovpn_socket.
I was just staring at this today, after having worked on the rcu_read_lock/unlock for the peer get()s..
I thinkt the assumption was: if we are in this read_sock callback, it's impossible that the ovpn_socket was invalidated, because it gets invalidated upon detach, which also prevents any further calling of this callback. But this sounds racy, and I guess we should somewhat hold a reference..
Quoted 7 lines
Anyway, this looks like you're reinventing strparser. Overall this is very similar to
net/xfrm/espintcp.c, but the receive side of espintcp uses strp and is much shorter (recv_ringlooks equivalent toike_queue, both sending a few messages to userspace -- look forstrp_init,espintcp_rcv,espintcp_parsein that file).
I think I did have a look at strparser once, but I wasn't sure to be grasping all details.
Will have another look and see what I can re-use.
Quoted 25 lines
Quoted 25 lines
drivers/net/ovpn/tcp.c new file +24
-0+/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +void *old_data; +int ret; + +INIT_WORK (&peer->tcp.tx_work, ovpn_tcp_tx_work); + + ret =ptr_ring_init (&peer->tcp.tx_ring,OVPN_QUEUE_LEN ,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"cannot allocate TCP TX ring\n" ); +return ret; + } + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + +write_lock_bh (&sock->sk->sk_callback_lock); + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock ();
Quoted 8 lines
drivers/net/ovpn/tcp.c line 450
Quoted 5 lines
drivers/net/ovpn/tcp.c new file +5
-0+if (old_data) { +netdev_err (peer->ovpn->dev, +"provided socket already taken by other user\n" ); + ret = -EBUSY ; + goto err;The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type ovpn_socket?
When we find the user data already assigned, we don't know what was really stored in there, right? Technically this socket could have gone through another module which assigned its own state.
Therefore I think that what UDP does [ dereferencing ((struct ovpn_socket *)user_data)->ovpn ] is probably not safe. Would you agree?
Quoted 12 lines
Quoted 12 lines
drivers/net/ovpn/tcp.c new file +9
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected.
Quoted 8 lines
drivers/net/ovpn/tcp.c line 506
Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6
-0+* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;Don't you need a separate variant for IPv6, like TLS does?
Never did so far.
My wild wild wild guess: for the time this socket is owned by ovpn, we only use callbacks that are IPvX agnostic, hence v4 vs v6 doesn't make any difference. When this socket is released, we reassigned the original prot.
Quoted 5 lines
drivers/net/ovpn/tcp.c line 507
Quoted 2 lines
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
It is, but my assumption is that those packets will just go through the socket as usual. No need to be handled by ovpn (those packets are not encrypted/decrypted, like data traffic is). And this is how it has worked so far.
Makes sense?
Thanks a lot!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 24 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/main.c +1
-0@@ -23,6 +23,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"@@ -247,8 +248,14 @@ static struct pernet_operations ovpn_pernet_ops = {static int __init ovpn_init (void )Quoted 8 lines
drivers/net/ovpn/main.c static struct pernet_operations ovpn_pernet_ops = { · line 253
Quoted 4 lines
drivers/net/ovpn/main.c +2
-1{ -int err =register_netdevice_notifier (&ovpn_netdev_notifier); +int err =ovpn_tcp_init (); +if (err) {
ovpn_tcp_initcannot fail (and if it could, you'd need to clean up whenregister_netdevice_notifierfails). I'd makeovpn_tcp_initvoid and kill this check.
Quoted 6 lines
I like to have all init functions returning int by design, even though they may not fail.
But I can undersand this is not necessarily good practice (somebody will always ask "when does it fail?" and there will will be no answer, which is confusing)
Yes, pretty much.
Quoted 11 lines
Quoted 10 lines
drivers/net/ovpn/peer.h line 42
Quoted 8 lines
drivers/net/ovpn/peer.h +4
-0+* @tcp.raw_len: next packet length as read from the stream (TCP only) +* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only) It would be nice to add information about whether they're used for TX or RX.
Quoted 3 lines
they are all about "from the stream" and "to the skb", meaning that we are doing RX.
Will make it more explicit.
Maybe group them in a struct rx?
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
+ * @tcp.sk_cb.sk_data_ready: pointer to original cb
+ * @tcp.sk_cb.sk_write_space: pointer to original cb
+ * @tcp.sk_cb.prot: pointer to original prot object
* @crypto: the crypto configuration (ciphers, keys, etc..)
* @dst_cache: cache fordst_entryused to send to peer
* @bind: remote peer bindingdrivers/net/ovpn/peer.h +4
-0@@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;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 +*/ Quoted 8 lines
drivers/net/ovpn/peer.h struct ovpn_peer { · line 81
Quoted 6 lines
drivers/net/ovpn/peer.h +6
-0+struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)];Why not u16 or
__be16for this one?
Quoted 7 lines
because in this array we are putting the bytes as we get them from the stream.
We may be at the point where one out of two bytes is available on the stream. For this reason I use an array to store this u16 byte by byte.Once thw two bytes are ready, we convert the content in an actual int and store it in "
data_len" (a few lines below).
Ok, I see. Hopefully you can switch to strparser and make this one go away.
Quoted 30 lines
Quoted 30 lines
Quoted 26 lines
drivers/net/ovpn/socket.c +5
-0@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */ @@ -26,6 +27,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); }@@ -69,6 +72,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; }drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 129
Quoted 1 line
drivers/net/ovpn/socket.c +0
-0@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;The line above this is:
Quoted 8 lines
Quoted 5 lines
ovpn_sock->ovpn = peer->ovpn;It's technically fine since you then overwrite this with peer in case we're on TCP, but
ovpn_sock->ovpnonly exists on UDP since you moved it into a union in this patch.Yeah, I did not want to make another branch, but having a UDP specific case will make code easier to read.
Either that, or drop the union.
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
drivers/net/ovpn/tcp.c new file +11
-0@@ -0,0 +1,511 @@ +static int ovpn_tcp_read_sock (read_descriptor_t *desc,struct sk_buff *in_skb, +unsigned int in_offset,size_t in_len) +{ +struct sock *sk = desc->arg.data; +struct ovpn_socket *sock; +struct ovpn_skb_cb *cb; +struct ovpn_peer *peer; +size_t chunk, copied =0 ; +void *data; +u16 len; +int st;Quoted 8 lines
drivers/net/ovpn/tcp.c line 40
Quoted 4 lines
drivers/net/ovpn/tcp.c new file +4
-0+ +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock ();You can't just release
rcu_read_lockand keep using sock (here and in the rest of this file). Either you keeprcu_read_lock, or you can take a reference on theovpn_socket.
Quoted 7 lines
I was just staring at this today, after having worked on the rcu_read_lock/unlock for the peer get()s..
I thinkt the assumption was: if we are in this
read_sockcallback, it's impossible that theovpn_socketwas invalidated, because it gets invalidated upon detach, which also prevents any further calling of this callback. But this sounds racy, and I guess we should somewhat hold a reference..
ovpn_tcp_read_sock starts
detachkfree_rcu(ovpn_socket)
...ovpn_socket actually freed
...ovpn_tcp_read_sock continues with freed ovpn_socket
I don't think anything in the current code prevents this.
Quoted 36 lines
Quoted 24 lines
Quoted 24 lines
drivers/net/ovpn/tcp.c new file +24
-0+/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +void *old_data; +int ret; + +INIT_WORK (&peer->tcp.tx_work, ovpn_tcp_tx_work); + + ret =ptr_ring_init (&peer->tcp.tx_ring,OVPN_QUEUE_LEN ,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"cannot allocate TCP TX ring\n" ); +return ret; + } + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + +write_lock_bh (&sock->sk->sk_callback_lock); + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock ();Quoted 8 lines
drivers/net/ovpn/tcp.c line 450
Quoted 5 lines
drivers/net/ovpn/tcp.c new file +5
-0+if (old_data) { +netdev_err (peer->ovpn->dev, +"provided socket already taken by other user\n" ); + ret = -EBUSY ; + goto err;The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?
Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?
Hmmm, yeah, I think you're right. If you checked encap_type == UDP_ENCAP_OVPNINUDP before (sk_prot for TCP), then you'd know it's really your data. Basically call ovpn_from_udp_sock during attach if you want to check something beyond EBUSY.
Once you're in your own callbacks, it should be safe. If some other code sends packet with a non-ovpn socket to ovpn's ->encap_rcv, something is really broken.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/net/ovpn/tcp.c new file +9
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. Quoted 8 lines
drivers/net/ovpn/tcp.c line 506
Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6
-0+* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;Don't you need a separate variant for IPv6, like TLS does?
Quoted 6 lines
Never did so far.
My wild wild wild guess: for the time this socket is owned by ovpn, we only use callbacks that are IPvX agnostic, hence v4 vs v6 doesn't make any difference.
When this socket is released, we reassigned the original prot.
That seems a bit suspicious to me. For example, tcpv6_prot has a different backlog_rcv. And you don't control if the socket is detached before being closed, or which callbacks are needed. Your userspace client doesn't use them, but someone else's might.
Quoted 11 lines
Quoted 4 lines
drivers/net/ovpn/tcp.c line 507
Quoted 1 line
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
It is, but my assumption is that those packets will just go through the socket as usual. No need to be handled by ovpn (those packets are not encrypted/decrypted, like data traffic is).
And this is how it has worked so far.Makes sense?
Two things come to mind:
- userspace is expected to prefix the messages it inserts on the stream with the 2-byte length field? otherwise, the peer won't be able to parse them out of the stream
- I'm not convinced this would be safe wrt kernel writing partial messages. if ovpn_tcp_send_one doesn't send the full message, you could interleave two messages:
+------+-------------------+------+--------+----------------+ | len1 | (bytes from msg1) | len2 | (msg2) | (rest of msg1) | +------+-------------------+------+--------+----------------+
and the RX side would parse that as:
+------+-----------------------------------+------+--------- | len1 | (bytes from msg1) | len2 | (msg2) | ???? | ... +------+-------------------+---------------+------+---------
and try to interpret some random bytes out of either msg1 or msg2 as a length prefix, resulting in a broken stream.
The stream format looks identical to ESP in TCP [1] (2B length prefix followed by the actual message), so I think the espintcp code (both tx and rx, except for actual protocol parsing) should look very similar. The problems that need to be solved for both protocols are pretty much the same.
[1] https://www.rfc-editor.org/rfc/rfc8229#section-3
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 11 lines · Sabrina Dubroca
Quoted 11 lines
Quoted 10 lines
drivers/net/ovpn/peer.h line 42
Quoted 8 lines
drivers/net/ovpn/peer.h +4
-0+* @tcp.raw_len: next packet length as read from the stream (TCP only) +* @tcp.skb: next packet being filled with data from the stream (TCP only) +* @tcp.offset: position of the next byte to write in the skb (TCP only) +* @tcp.data_len: next packet length converted to host order (TCP only) It would be nice to add information about whether they're used for TX or RX.
Quoted 5 lines
Quoted 3 lines
they are all about "from the stream" and "to the skb", meaning that we are doing RX.
Will make it more explicit.Maybe group them in a struct rx?
yap, makes sense.
Quoted 68 lines
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
+ * @tcp.sk_cb.sk_data_ready: pointer to original cb
+ * @tcp.sk_cb.sk_write_space: pointer to original cb
+ * @tcp.sk_cb.prot: pointer to original prot object
* @crypto: the crypto configuration (ciphers, keys, etc..)
* @dst_cache: cache fordst_entryused to send to peer
* @bind: remote peer bindingdrivers/net/ovpn/peer.h +4
-0@@ -59,6 +69,25 @@ struct ovpn_peer {struct ptr_ring netif_rx_ring;struct napi_struct napi;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 +*/ Quoted 8 lines
drivers/net/ovpn/peer.h struct ovpn_peer { · line 81
Quoted 6 lines
drivers/net/ovpn/peer.h +6
-0+struct { +struct ptr_ring tx_ring; +struct work_struct tx_work; +struct work_struct rx_work; + +u8 raw_len[sizeof (u16)];Why not u16 or
__be16for this one?Quoted 7 lines
because in this array we are putting the bytes as we get them from the stream.
We may be at the point where one out of two bytes is available on the stream. For this reason I use an array to store this u16 byte by byte.Once thw two bytes are ready, we convert the content in an actual int and store it in "
data_len" (a few lines below).Ok, I see. Hopefully you can switch to strparser and make this one go away.
Quoted 32 lines
Quoted 32 lines
Quoted 26 lines
drivers/net/ovpn/socket.c +5
-0@@ -16,6 +16,7 @@#include "packet.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" /* Finalize release of socket, called after RCU grace period */ @@ -26,6 +27,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); }@@ -69,6 +72,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; }drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 129
Quoted 1 line
drivers/net/ovpn/socket.c +0
-0@@ -124,6 +129,21 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) ovpn_sock->sock = sock;The line above this is:
ovpn_sock->ovpn = peer->ovpn;
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
It's technically fine since you then overwrite this with peer in case we're on TCP, but
ovpn_sock->ovpnonly exists on UDP since you moved it into a union in this patch.Yeah, I did not want to make another branch, but having a UDP specific case will make code easier to read.
Either that, or drop the union.
ACK
Quoted 46 lines
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
drivers/net/ovpn/tcp.c new file +11
-0@@ -0,0 +1,511 @@ +static int ovpn_tcp_read_sock (read_descriptor_t *desc,struct sk_buff *in_skb, +unsigned int in_offset,size_t in_len) +{ +struct sock *sk = desc->arg.data; +struct ovpn_socket *sock; +struct ovpn_skb_cb *cb; +struct ovpn_peer *peer; +size_t chunk, copied =0 ; +void *data; +u16 len; +int st;Quoted 8 lines
drivers/net/ovpn/tcp.c line 40
Quoted 4 lines
drivers/net/ovpn/tcp.c new file +4
-0+ +rcu_read_lock (); + sock =rcu_dereference_sk_user_data (sk); +rcu_read_unlock ();You can't just release
rcu_read_lockand keep using sock (here and in the rest of this file). Either you keeprcu_read_lock, or you can take a reference on theovpn_socket.Quoted 7 lines
I was just staring at this today, after having worked on the rcu_read_lock/unlock for the peer get()s..
I thinkt the assumption was: if we are in this
read_sockcallback, it's impossible that theovpn_socketwas invalidated, because it gets invalidated upon detach, which also prevents any further calling of this callback. But this sounds racy, and I guess we should somewhat hold a reference..
ovpn_tcp_read_sockstartsdetach
kfree_rcu(ovpn_socket)
...ovpn_socketactually freed
...ovpn_tcp_read_sockcontinues with freedovpn_socket
Quoted 1 line
I don't think anything in the current code prevents this.
mh yeah, if something like this happens right after having started the read_sock we are doomed. Will fix this.
Quoted 43 lines
Quoted 43 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/ovpn/tcp.c new file +24
-0+/* Set TCP encapsulation callbacks */ +int ovpn_tcp_socket_attach (struct socket *sock,struct ovpn_peer *peer) +{ +void *old_data; +int ret; + +INIT_WORK (&peer->tcp.tx_work, ovpn_tcp_tx_work); + + ret =ptr_ring_init (&peer->tcp.tx_ring,OVPN_QUEUE_LEN ,GFP_KERNEL ); +if (ret <0 ) { +netdev_err (peer->ovpn->dev,"cannot allocate TCP TX ring\n" ); +return ret; + } + + peer->tcp.skb =NULL ; + peer->tcp.offset =0 ; + peer->tcp.data_len =0 ; + +write_lock_bh (&sock->sk->sk_callback_lock); + +/* make sure no pre-existing encapsulation handler exists */ +rcu_read_lock (); + old_data =rcu_dereference_sk_user_data (sock->sk); +rcu_read_unlock ();Quoted 8 lines
drivers/net/ovpn/tcp.c line 450
Quoted 5 lines
drivers/net/ovpn/tcp.c new file +5
-0+if (old_data) { +netdev_err (peer->ovpn->dev, +"provided socket already taken by other user\n" ); + ret = -EBUSY ; + goto err;The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.
Quoted 7 lines
Quoted 2 lines
Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
Quoted 4 lines
Once you're in your own callbacks, it should be safe. If some other code sends packet with a non-ovpn socket to ovpn's ->encap_rcv, something is really broken.
yup
Quoted 41 lines
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/tcp.c new file +9
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. Quoted 8 lines
drivers/net/ovpn/tcp.c line 506
Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6
-0+* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;Don't you need a separate variant for IPv6, like TLS does?
Quoted 6 lines
Never did so far.
My wild wild wild guess: for the time this socket is owned by ovpn, we only use callbacks that are IPvX agnostic, hence v4 vs v6 doesn't make any difference.
When this socket is released, we reassigned the original prot.That seems a bit suspicious to me. For example,
tcpv6_prothas a differentbacklog_rcv. And you don't control if the socket is detached before being closed, or which callbacks are needed. Your userspace client doesn't use them, but someone else's might.Quoted 10 lines
Quoted 4 lines
drivers/net/ovpn/tcp.c line 507
Quoted 1 line
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
It is, but my assumption is that those packets will just go through the socket as usual. No need to be handled by ovpn (those packets are not encrypted/decrypted, like data traffic is).
And this is how it has worked so far.
Quoted 7 lines
Quoted 1 line
Makes sense?
Two things come to mind:
- userspace is expected to prefix the messages it inserts on the stream with the 2-byte length field? otherwise, the peer won't be able to parse them out of the stream
correct. userspace sends those packets as if ovpn is not running, therefore this happens naturally.
Quoted 9 lines
- I'm not convinced this would be safe wrt kernel writing partial messages. if
ovpn_tcp_send_onedoesn't send the full message, you could interleave two messages:+------+-------------------+------+--------+----------------+ | len1 | (bytes from msg1) | len2 | (msg2) | (rest of msg1) | +------+-------------------+------+--------+----------------+
Quoted 8 lines
and the RX side would parse that as:
+------+-----------------------------------+------+--------- | len1 | (bytes from msg1) | len2 | (msg2) | ???? | ... +------+-------------------+---------------+------+---------and try to interpret some random bytes out of either msg1 or msg2 as a length prefix, resulting in a broken stream.
hm you are correct. if multiple sendmsg can overlap, then we might be in troubles, but are we sure this can truly happen?
Quoted 7 lines
The stream format looks identical to ESP in TCP [1] (2B length prefix followed by the actual message), so I think the espintcp code (both tx and rx, except for actual protocol parsing) should look very similar. The problems that need to be solved for both protocols are pretty much the same.
ok, will have a look. maybe this will simplify the code even more and we will get rid of some of the issues we were discussing above.
Thanks!
Quoted 3 lines
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 15 lines · Antonio Quartulli
Quoted 14 lines · Sabrina Dubroca
Quoted 13 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?
Quoted 7 lines
Quoted 4 lines
Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in udp.c and the corresponding refcount increase in ovpn_socket_new?
Quoted 42 lines
Quoted 42 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/net/ovpn/tcp.c new file +9
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. Quoted 8 lines
drivers/net/ovpn/tcp.c line 506
Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6
-0+* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;Don't you need a separate variant for IPv6, like TLS does?
Quoted 6 lines
Never did so far.
My wild wild wild guess: for the time this socket is owned by ovpn, we only use callbacks that are IPvX agnostic, hence v4 vs v6 doesn't make any difference.
When this socket is released, we reassigned the original prot.That seems a bit suspicious to me. For example,
tcpv6_prothas a differentbacklog_rcv. And you don't control if the socket is detached before being closed, or which callbacks are needed. Your userspace client doesn't use them, but someone else's might.Quoted 11 lines
Quoted 4 lines
drivers/net/ovpn/tcp.c line 507
Quoted 1 line
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
It is, but my assumption is that those packets will just go through the socket as usual. No need to be handled by ovpn (those packets are not encrypted/decrypted, like data traffic is).
And this is how it has worked so far.Makes sense?
Quoted 8 lines
Quoted 5 lines
Two things come to mind:
- userspace is expected to prefix the messages it inserts on the stream with the 2-byte length field? otherwise, the peer won't be able to parse them out of the stream
correct. userspace sends those packets as if ovpn is not running, therefore this happens naturally.
ok.
Quoted 14 lines
Quoted 14 lines
- I'm not convinced this would be safe wrt kernel writing partial messages. if
ovpn_tcp_send_onedoesn't send the full message, you could interleave two messages:+------+-------------------+------+--------+----------------+ | len1 | (bytes from msg1) | len2 | (msg2) | (rest of msg1) | +------+-------------------+------+--------+----------------+and the RX side would parse that as: +------+-----------------------------------+------+--------- | len1 | (bytes from msg1) | len2 | (msg2) | ???? | ... +------+-------------------+---------------+------+---------
Quoted 5 lines
Quoted 2 lines
and try to interpret some random bytes out of either msg1 or msg2 as a length prefix, resulting in a broken stream.
hm you are correct. if multiple sendmsg can overlap, then we might be in troubles, but are we sure this can truly happen?
What would prevent this? The kernel_sendmsg call in ovpn_tcp_send_one could send a partial message, and then what would stop userspace from sending its own message during the cond_resched from ovpn_tcp_tx_work?
Quoted 8 lines
Quoted 5 lines
The stream format looks identical to ESP in TCP [1] (2B length prefix followed by the actual message), so I think the espintcp code (both tx and rx, except for actual protocol parsing) should look very similar. The problems that need to be solved for both protocols are pretty much the same.
ok, will have a look. maybe this will simplify the code even more and we will get rid of some of the issues we were discussing above.
I doubt dealing with possible interleaving will make the code simpler, but I think it has to be done.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 6 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.
Quoted 5 lines
Quoted 2 lines
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in
udp.cand the corresponding refcount increase inovpn_socket_new?
it's just me that likes to be verbose when doing error reporting.
But eventually the exact error is ignored and we release the reference. From netlink.c:
342 peer->sock = ovpn_socket_new(sock, peer);
343 if (IS_ERR(peer->sock)) {
344 sockfd_put(sock); 345 peer->sock = NULL; 346 ret = -ENOTSOCK;
so no added value in distinguishing the two cases.
Quoted 73 lines
Quoted 44 lines
Quoted 44 lines
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/ovpn/tcp.c new file +9
-0+int __init ovpn_tcp_init (void ) +{ +/* We need to substitute the recvmsg and the sock_is_readable +* callbacks in the sk_prot member of the sock object for TCP +* sockets. +* +* However sock->sk_prot is a pointer to a static variable and +* therefore we can't directly modify it, otherwise every socket +* pointing to it will be affected. Quoted 8 lines
drivers/net/ovpn/tcp.c line 506
Quoted 6 lines
drivers/net/ovpn/tcp.c new file +6
-0+* +* For this reason we create our own static copy and modify what +* we need. Then we make sk_prot point to this copy +* (in ovpn_tcp_socket_attach()) +*/ + ovpn_tcp_prot = tcp_prot;Don't you need a separate variant for IPv6, like TLS does?
Quoted 6 lines
Never did so far.
My wild wild wild guess: for the time this socket is owned by ovpn, we only use callbacks that are IPvX agnostic, hence v4 vs v6 doesn't make any difference.
When this socket is released, we reassigned the original prot.That seems a bit suspicious to me. For example,
tcpv6_prothas a differentbacklog_rcv. And you don't control if the socket is detached before being closed, or which callbacks are needed. Your userspace client doesn't use them, but someone else's might.Quoted 11 lines
Quoted 4 lines
drivers/net/ovpn/tcp.c line 507
Quoted 1 line
drivers/net/ovpn/tcp.c new file +1
-0+ ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;You don't need to replace ->sendmsg as well? The userspace client is not expected to send messages?
It is, but my assumption is that those packets will just go through the socket as usual. No need to be handled by ovpn (those packets are not encrypted/decrypted, like data traffic is).
And this is how it has worked so far.Makes sense?
Quoted 8 lines
Quoted 5 lines
Two things come to mind:
- userspace is expected to prefix the messages it inserts on the stream with the 2-byte length field? otherwise, the peer won't be able to parse them out of the stream
correct. userspace sends those packets as if ovpn is not running, therefore this happens naturally.
ok.
Quoted 17 lines
Quoted 16 lines
- I'm not convinced this would be safe wrt kernel writing partial
messages. if ovpn_tcp_send_one doesn't send the full message, you could interleave two messages: +------+-------------------+------+--------+----------------+ | len1 | (bytes from msg1) | len2 | (msg2) | (rest of msg1) | +------+-------------------+------+--------+----------------+ and the RX side would parse that as: +------+-----------------------------------+------+--------- | len1 | (bytes from msg1) | len2 | (msg2) | ???? | ... +------+-------------------+---------------+------+--------- and try to interpret some random bytes out of either msg1 or msg2 as a length prefix, resulting in a broken stream.
Quoted 6 lines
Quoted 2 lines
hm you are correct. if multiple sendmsg can overlap, then we might be in troubles, but are we sure this can truly happen?
What would prevent this? The
kernel_sendmsgcall inovpn_tcp_send_onecould send a partial message, and then what would stop userspace from sending its own message during thecond_reschedfromovpn_tcp_tx_work?
I was under the impression that ovpn_tcp_send_one() would always send an entire packet, but this may not be the case. So you're definitely right.
We may end up having interleaving sendmsg from kernelspace and userspace.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The stream format looks identical to ESP in TCP [1] (2B length prefix followed by the actual message), so I think the espintcp code (both tx and rx, except for actual protocol parsing) should look very similar. The problems that need to be solved for both protocols are pretty much the same.
Quoted 5 lines
Quoted 2 lines
ok, will have a look. maybe this will simplify the code even more and we will get rid of some of the issues we were discussing above.
I doubt dealing with possible interleaving will make the code simpler, but I think it has to be done.
Yap.
Thanks a lot for pointing this out and for the pointers you gave me.
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 22 lines · Antonio Quartulli
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 6 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in
udp.cand the corresponding refcount increase inovpn_socket_new?it's just me that likes to be verbose when doing error reporting.
With the "already owned by this interface" message? Sure, I get that.
Quoted 10 lines
But eventually the exact error is ignored and we release the reference. From
netlink.c:342
peer->sock=ovpn_socket_new(sock, peer);
343 if (IS_ERR(peer->sock)) {344 sockfd_put(sock); 345 peer->sock = NULL; 346 ret = -ENOTSOCK;so no added value in distinguishing the two cases.
But ovpn_socket_new currently turns EALREADY into a valid result, so we won't go through the error hanadling here. That's the part I'm unclear about.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 42 lines · Sabrina Dubroca
Quoted 22 lines · Antonio Quartulli
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 6 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in
udp.cand the corresponding refcount increase inovpn_socket_new?it's just me that likes to be verbose when doing error reporting.
With the "already owned by this interface" message? Sure, I get that.
Quoted 9 lines
But eventually the exact error is ignored and we release the reference. From
netlink.c:342
peer->sock=ovpn_socket_new(sock, peer);
343 if (IS_ERR(peer->sock)) {344 sockfd_put(sock); 345 peer->sock = NULL; 346 ret = -ENOTSOCK;
Quoted 5 lines
Quoted 1 line
so no added value in distinguishing the two cases.
But
ovpn_socket_newcurrently turns EALREADY into a valid result, so we won't go through the error hanadling here. That's the part I'm unclear about.
you're right. I had forgotten a little but important detail.
With UDP OpenVPN creates one socket and uses it for all peers. With TCP we forcefully need one socket per client.
Consequently, when a UDP socket is found to be used by our own instance,
we can happily increase the refcounter and use it as if it was free
(we are just attaching it to yet another peer).
In TCP this is not possible, so the socket must be unused, otherwise we can't attach it.
I hope it makes sense.
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 54 lines · Antonio Quartulli
Quoted 42 lines · Sabrina Dubroca
Quoted 22 lines · Antonio Quartulli
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 6 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in
udp.cand the corresponding refcount increase inovpn_socket_new?it's just me that likes to be verbose when doing error reporting.
With the "already owned by this interface" message? Sure, I get that.
Quoted 9 lines
But eventually the exact error is ignored and we release the reference. From
netlink.c:342
peer->sock=ovpn_socket_new(sock, peer);
343 if (IS_ERR(peer->sock)) {344 sockfd_put(sock); 345 peer->sock = NULL; 346 ret = -ENOTSOCK;Quoted 5 lines
Quoted 1 line
so no added value in distinguishing the two cases.
But
ovpn_socket_newcurrently turns EALREADY into a valid result, so we won't go through the error hanadling here. That's the part I'm unclear about.you're right. I had forgotten a little but important detail.
With UDP OpenVPN creates one socket and uses it for all peers. With TCP we forcefully need one socket per client.
Quoted 8 lines
Consequently, when a UDP socket is found to be used by our own instance, we can happily increase the refcounter and use it as if it was free (we are just attaching it to yet another peer).
In TCP this is not possible, so the socket must be unused, otherwise we can't attach it.
I hope it makes sense.
Yes, thanks. This behavior should be documented (for example, by putting exactly what you just wrote in a comment above ovpn_socket_new).
So for TCP you just need the existing check and EBUSY return. For UDP, you need the EALREADY check, but with an extra encap_type test before looking at the contents of the sk_user_data.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 13/24] ovpn: implement TCP transport
Quoted 59 lines · Sabrina Dubroca
Quoted 58 lines · Antonio Quartulli
Quoted 42 lines · Sabrina Dubroca
Quoted 22 lines · Antonio Quartulli
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 6 lines
Quoted 2 lines
The UDP code differentiates "socket already owned by this interface" from "already taken by other user". That doesn't apply to TCP?
This makes me wonder: how safe it is to interpret the user data as an object of type
ovpn_socket?Quoted 7 lines
When we find the user data already assigned, we don't know what was really stored in there, right?
Technically this socket could have gone through another module which assigned its own state.Therefore I think that what UDP does [ dereferencing ((struct
ovpn_socket*)user_data)->ovpn ] is probably not safe. Would you agree?Hmmm, yeah, I think you're right. If you checked
encap_type==UDP_ENCAP_OVPNINUDPbefore (sk_protfor TCP), then you'd know it's really your data. Basically callovpn_from_udp_sockduring attach if you want to check something beyond EBUSY.Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
right. Maybe we can leave with simply reporting EBUSY and be done with it, without adding extra checks and what not.
I don't know. What was the reason for the EALREADY handling in
udp.cand the corresponding refcount increase inovpn_socket_new?it's just me that likes to be verbose when doing error reporting.
With the "already owned by this interface" message? Sure, I get that.
Quoted 9 lines
But eventually the exact error is ignored and we release the reference. From
netlink.c:342
peer->sock=ovpn_socket_new(sock, peer);
343 if (IS_ERR(peer->sock)) {344 sockfd_put(sock); 345 peer->sock = NULL; 346 ret = -ENOTSOCK;Quoted 5 lines
Quoted 1 line
so no added value in distinguishing the two cases.
But
ovpn_socket_newcurrently turns EALREADY into a valid result, so we won't go through the error hanadling here. That's the part I'm unclear about.you're right. I had forgotten a little but important detail.
With UDP OpenVPN creates one socket and uses it for all peers. With TCP we forcefully need one socket per client.
Consequently, when a UDP socket is found to be used by our own instance, we can happily increase the refcounter and use it as if it was free (we are just attaching it to yet another peer).
Quoted 8 lines
Quoted 4 lines
In TCP this is not possible, so the socket must be unused, otherwise we can't attach it.
I hope it makes sense.
Yes, thanks. This behavior should be documented (for example, by putting exactly what you just wrote in a comment above
ovpn_socket_new).
absolutely, will do.
Quoted 4 lines
So for TCP you just need the existing check and EBUSY return. For UDP, you need the EALREADY check, but with an extra
encap_typetest before looking at the contents of thesk_user_data.
ACK
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 14/24 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/io.c | 1 + drivers/net/ovpn/main.c | 8 +- drivers/net/ovpn/ovpnstruct.h | 10 +++ drivers/net/ovpn/peer.c | 149 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 14 ++++ 5 files changed, 181 insertions(+), 1 deletion(-)
drivers/net/ovpn/io.c +1 -0
@@ -36,6 +36,7 @@ int ovpn_struct_init(struct net_device *dev)return err;spin_lock_init (&ovpn->lock); +spin_lock_init (&ovpn->peers.lock); ovpn->crypto_wq = alloc_workqueue("ovpn-crypto-wq-%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0,
drivers/net/ovpn/main.c +7 -1
@@ -176,8 +176,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 ; + }unregister_netdevice (ovpn->dev);synchronize_net ();
drivers/net/ovpn/ovpnstruct.h +10 -0
@@ -21,6 +21,10 @@* @crypto_wq: used to schedule crypto work that may sleep during TX/RX * @event_wq: used to schedule generic events that may sleep and that need to be * performed outside of softirq context +* @peers.by_id: table of peers index by ID +* @peers.by_transp_addr: table of peers indexed by transport address +* @peers.by_vpn_addr: table of peers indexed by VPN IP address +* @peers.lock: protects writes to peers tables * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list */
@@ -31,6 +35,12 @@ struct ovpn_struct {spinlock_t lock;/* protect writing to the ovpn_struct object */ struct workqueue_struct *crypto_wq;struct workqueue_struct *events_wq; +struct { + 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 */ + } peers;struct ovpn_peer __rcu *peer;struct list_head dev_list; };
drivers/net/ovpn/peer.c +149 -0
182 lines not on this page · raw message
drivers/net/ovpn/peer.h +14 -0
@@ -26,6 +26,10 @@* @id: unique identifier * @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 * @encrypt_work: work used to process outgoing packets * @decrypt_work: work used to process incoming packets * @tx_ring: queue of outgoing poackets to this peer
@@ -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 work_struct encrypt_work;struct work_struct decrypt_work;struct ptr_ring tx_ring;
@@ -208,4 +216,10 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb); +/** +* ovpn_peers_free - free all peers in the instance +* @ovpn: the instance whose peers should be released +*/ +void ovpn_peers_free (struct ovpn_struct *ovpn); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 14/24] ovpn: implement multi-peer support
Hi Antonio, I took a little break but I'm looking at your patches again now.
drivers/net/ovpn/ovpnstruct.h struct ovpn_struct { · line 41
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/ovpnstruct.h +4
-0@@ -31,6 +35,12 @@ struct ovpn_struct {spinlock_t lock;/* protect writing to the ovpn_struct object */ struct workqueue_struct *crypto_wq;struct workqueue_struct *events_wq; +struct { + DECLARE_HASHTABLE(by_id, 12); + DECLARE_HASHTABLE(by_transp_addr, 12); + DECLARE_HASHTABLE(by_vpn_addr, 12);
Those are really big. I guess for large servers they make sense, but you're making clients hold 98kB in memory that they're not going to use.
Maybe they could be dynamically sized, but I think struct peers should be allocated on demand (only for mode == MP) if you want this size.
Quoted 10 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+spinlock_t lock;/* protects writes to peers tables */ + } peers;struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c +0
-0
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 366
Quoted 5 lines
drivers/net/ovpn/peer.c +2
-0@@ -361,6 +362,91 @@ struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn,return peer; } +/** +* ovpn_peer_add_mp - add per to related tables in a MP instance
^
s/per/peer/Quoted 7 lines
drivers/net/ovpn/peer.c +7
-0+* @ovpn: the instance to add the peer to +* @peer: the peer to add +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_peer_add_mp (struct ovpn_struct *ovpn,struct ovpn_peer *peer) +{
[...]
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 434
Quoted 12 lines
drivers/net/ovpn/peer.c +12
-0+ 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]); + +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]); + } + +hlist_del_init_rcu (&peer->hash_entry_addr6);
Why are hash_entry_transp_addr and hash_entry_addr6 getting a hlist_del_init_rcu() call, but not hash_entry_id and hash_entry_addr4?
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 436
Quoted 2 lines
drivers/net/ovpn/peer.c +2
-0+if (memcmp (&peer->vpn_addrs.ipv6, &in6addr_any, +sizeof (peer->vpn_addrs.ipv6))) {
!ipv6_addr_any(&peer->vpn_addrs.ipv6)
Quoted 7 lines
drivers/net/ovpn/peer.c +7
-0+ 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]); + } +
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 14/24] ovpn: implement multi-peer support
Quoted 2 lines · Sabrina Dubroca
Hi Antonio, I took a little break but I'm looking at your patches again now.
Thanks Sabrina! Meanwhile I have been working on all your suggested changes. Right now I am familiarizing with the strparser.
Quoted 17 lines
drivers/net/ovpn/ovpnstruct.h struct ovpn_struct { · line 41
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/ovpnstruct.h +4
-0@@ -31,6 +35,12 @@ struct ovpn_struct {spinlock_t lock;/* protect writing to the ovpn_struct object */ struct workqueue_struct *crypto_wq;struct workqueue_struct *events_wq; +struct { + DECLARE_HASHTABLE(by_id, 12); + DECLARE_HASHTABLE(by_transp_addr, 12); + DECLARE_HASHTABLE(by_vpn_addr, 12);Those are really big. I guess for large servers they make sense, but you're making clients hold 98kB in memory that they're not going to use.
Right - for clients it doesn't make sense.
Quoted 3 lines
Maybe they could be dynamically sized, but I think struct peers should be allocated on demand (only for mode == MP) if you want this size.
Yeah, makes sense. I'll allocate it dynamically then.
Quoted 14 lines
Quoted 14 lines
drivers/net/ovpn/ovpnstruct.h +2
-0+spinlock_t lock;/* protects writes to peers tables */ + } peers;struct ovpn_peer __rcu *peer;struct list_head dev_list; };drivers/net/ovpn/peer.c +0
-0@@ -361,6 +362,91 @@ struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn,return peer; }
Quoted 4 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 366
Quoted 2 lines
drivers/net/ovpn/peer.c +2
-0+/** +* ovpn_peer_add_mp - add per to related tables in a MP instance ^ s/per/peer/
ACK
Quoted 9 lines
Quoted 8 lines
drivers/net/ovpn/peer.c +7
-0+* @ovpn: the instance to add the peer to +* @peer: the peer to add +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_peer_add_mp (struct ovpn_struct *ovpn,struct ovpn_peer *peer) +{[...]
Quoted 15 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 434
Quoted 12 lines
drivers/net/ovpn/peer.c +12
-0+ 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]); + +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]); + } + +hlist_del_init_rcu (&peer->hash_entry_addr6);Why are
hash_entry_transp_addrandhash_entry_addr6getting ahlist_del_init_rcu()call, but nothash_entry_idandhash_entry_addr4?
I think not calling del_init_rcu on hash_entry_addr4 was a mistake.
Calling del_init_rcu on addr4, addr6 and transp_addr is needed to put them in a known state in case they are not hashed.
While hash_entry_id always goes through hlist_add_head_rcu, therefore del_init_rcu is useless (to my understanding).
Quoted 5 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 436
Quoted 3 lines
drivers/net/ovpn/peer.c +2
-0+if (memcmp (&peer->vpn_addrs.ipv6, &in6addr_any, +sizeof (peer->vpn_addrs.ipv6))) {!ipv6_addr_any(&peer->vpn_addrs.ipv6)
ACK
Quoted 9 lines
Quoted 8 lines
drivers/net/ovpn/peer.c +7
-0+ 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]); + } +
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 14/24] ovpn: implement multi-peer support
Quoted 6 lines · Antonio Quartulli
Quoted 2 lines · Sabrina Dubroca
Hi Antonio, I took a little break but I'm looking at your patches again now.
Thanks Sabrina! Meanwhile I have been working on all your suggested changes. Right now I am familiarizing with the strparser.
Cool :)
Quoted 17 lines
Quoted 16 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 434
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +12
-0+ 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]); + +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]); + } + +hlist_del_init_rcu (&peer->hash_entry_addr6);Why are
hash_entry_transp_addrandhash_entry_addr6getting ahlist_del_init_rcu()call, but nothash_entry_idandhash_entry_addr4?
Quoted 4 lines
I think not calling
del_init_rcuonhash_entry_addr4was a mistake.Calling
del_init_rcuon addr4, addr6 andtransp_addris needed to put them in a known state in case they are not hashed.
hlist_del_init_rcu does nothing if node is not already on a list.
Quoted 2 lines
While
hash_entry_idalways goes throughhlist_add_head_rcu, thereforedel_init_rcuis useless (to my understanding).
I'm probably missing something about how this all fits together. In patch 19, I see ovpn_nl_set_peer_doit can re-add a peer that is already added (but I'm not sure why, since you don't allow changing the addresses, so it won't actually be re-hashed).
I don't think doing a 2nd add of the same element to peers.by_id (or any of the other hashtables) is correct, so I'd say you need hlist_del_init_rcu for all of them.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 14/24] ovpn: implement multi-peer support
Quoted 27 lines · Sabrina Dubroca
Quoted 6 lines · Antonio Quartulli
Quoted 2 lines · Sabrina Dubroca
Hi Antonio, I took a little break but I'm looking at your patches again now.
Thanks Sabrina! Meanwhile I have been working on all your suggested changes. Right now I am familiarizing with the strparser.
Cool :)
Quoted 17 lines
Quoted 16 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 434
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +12
-0+ 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]); + +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]); + } + +hlist_del_init_rcu (&peer->hash_entry_addr6);Why are
hash_entry_transp_addrandhash_entry_addr6getting ahlist_del_init_rcu()call, but nothash_entry_idandhash_entry_addr4?
Quoted 6 lines
Quoted 4 lines
I think not calling
del_init_rcuonhash_entry_addr4was a mistake.Calling
del_init_rcuon addr4, addr6 andtransp_addris needed to put them in a known state in case they are not hashed.
hlist_del_init_rcudoes nothing if node is not already on a list.
Mh you're right. I must have got confused for some reason. Those del_init_rcu can go then.
Quoted 8 lines
Quoted 3 lines
While
hash_entry_idalways goes throughhlist_add_head_rcu, thereforedel_init_rcuis useless (to my understanding).I'm probably missing something about how this all fits together. In patch 19, I see
ovpn_nl_set_peer_doitcan re-add a peer that is already added (but I'm not sure why, since you don't allow changing the addresses, so it won't actually be re-hashed).
Actually it's not a "re-add", but the intent is to "update" a peer that already exists. However, some fields are forbidden from being updated, like the address.
[NOTE: I found some issue with the "peer update" logic in ovpn_nl_set_peer_doit and it's being changed a bit]
Quoted 4 lines
I don't think doing a 2nd add of the same element to peers.by_id (or any of the other hashtables) is correct, so I'd say you need
hlist_del_init_rcufor all of them.
This is exactly the bug I mentioned above: we should not go through the add again. Ideally we should just update the fields and be done with it, without re-hashing the object.
I hope it makes sense.
Cheers,
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 14/24] ovpn: implement multi-peer support
Quoted 55 lines · Antonio Quartulli
Quoted 27 lines · Sabrina Dubroca
Quoted 6 lines · Antonio Quartulli
Quoted 2 lines · Sabrina Dubroca
Hi Antonio, I took a little break but I'm looking at your patches again now.
Thanks Sabrina! Meanwhile I have been working on all your suggested changes. Right now I am familiarizing with the strparser.
Cool :)
Quoted 17 lines
Quoted 16 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 434
Quoted 12 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +12
-0+ 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]); + +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]); + } + +hlist_del_init_rcu (&peer->hash_entry_addr6);Why are
hash_entry_transp_addrandhash_entry_addr6getting ahlist_del_init_rcu()call, but nothash_entry_idandhash_entry_addr4?Quoted 6 lines
Quoted 4 lines
I think not calling
del_init_rcuonhash_entry_addr4was a mistake.Calling
del_init_rcuon addr4, addr6 andtransp_addris needed to put them in a known state in case they are not hashed.
hlist_del_init_rcudoes nothing if node is not already on a list.Mh you're right. I must have got confused for some reason. Those
del_init_rcucan go then.Quoted 8 lines
Quoted 3 lines
While
hash_entry_idalways goes throughhlist_add_head_rcu, thereforedel_init_rcuis useless (to my understanding).I'm probably missing something about how this all fits together. In patch 19, I see
ovpn_nl_set_peer_doitcan re-add a peer that is already added (but I'm not sure why, since you don't allow changing the addresses, so it won't actually be re-hashed).Actually it's not a "re-add", but the intent is to "update" a peer that already exists. However, some fields are forbidden from being updated, like the address.
[NOTE: I found some issue with the "peer update" logic in
ovpn_nl_set_peer_doitand it's being changed a bit]Quoted 1 line
Quoted 7 lines
Quoted 3 lines
I don't think doing a 2nd add of the same element to peers.by_id (or any of the other hashtables) is correct, so I'd say you need
hlist_del_init_rcufor all of them.This is exactly the bug I mentioned above: we should not go through the add again. Ideally we should just update the fields and be done with it, without re-hashing the object.
Ok, if you only call ovpn_peer_add for new peers, this looks fine and the hlist_del_init_rcu can all be removed as you said.
Thanks.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerPATCH v3 15/24 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 | 285 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 281 insertions(+), 4 deletions(-)
drivers/net/ovpn/peer.c +281 -4
@@ -198,6 +198,98 @@ 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) +{ +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) +{ +struct rt6_info *rt = (struct rt6_info *)skb_rtable (skb); + +if (!rt || !(rt->rt6i_flags &RTF_GATEWAY )) +return ipv6_hdr (skb)->daddr; + +return rt->rt6i_gateway; +} + +/** +* ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address +* @head: list head to search +* @addr: VPN IPv4 to use as search key +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4 (struct hlist_head *head, +__be32 *addr) +{ +struct ovpn_peer *tmp, *peer =NULL ; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr4) { +if (*addr != tmp->vpn_addrs.ipv4.s_addr) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock (); + +return peer; +} + +/** +* ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address +* @head: list head to search +* @addr: VPN IPv6 to use as search key +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6 (struct hlist_head *head, +struct in6_addr *addr) +{ +struct ovpn_peer *tmp, *peer =NULL ; +int i; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr6) { +for (i =0 ; i <4 ; i++) { +if (addr->s6_addr32[i] != + tmp->vpn_addrs.ipv6.s6_addr32[i]) +continue ; + } + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock (); + +return peer; +} +/** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from
@@ -268,14 +360,46 @@ 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; +bool found; +u32 index;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 ; + } + + index =ovpn_peer_index (ovpn->peers.by_transp_addr, &ss, sa_len); + head = &ovpn->peers.by_transp_addr[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_transp_addr) { + found =ovpn_peer_transp_match (tmp, &ss); +if (!found) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -303,10 +427,28 @@ 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; +u32 index;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); + + index =ovpn_peer_index (ovpn->peers.by_id, &peer_id,sizeof (peer_id)); + head = &ovpn->peers.by_id[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -328,6 +470,11 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side
@@ -338,15 +485,123 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb); + +switch (sa_fam) { +case AF_INET : + addr4 =ovpn_nexthop_from_skb4 (skb); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_skb6 (skb); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6); +break ; }return peer; } +/** +* ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dst: 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 the dst itself otherwise +*/ +static __be32 ovpn_nexthop_from_rt4 (struct ovpn_struct *ovpn,__be32 dst) +{ +struct rtable *rt; +struct flowi4 fl = { + .daddr = dst + }; + + 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__, + &dst); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dst; + } + +if (!rt->rt_uses_gateway) + goto out; + + dst = rt->rt_gw4; +out: +ip_rt_put (rt); +return dst; +} + +/** +* ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dst: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IO 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 the dst itself otherwise +*/ +static struct in6_addr ovpn_nexthop_from_rt6 (struct ovpn_struct *ovpn, +struct in6_addr dst) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +struct dst_entry *entry; +struct rt6_info *rt; +struct flowi6 fl = { + .daddr = dst, + }; + + 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__, + &dst); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dst; + } + + rt = container_of(entry, struct rt6_info, dst); + +if (!(rt->rt6i_flags &RTF_GATEWAY )) + goto out; + + dst = rt->rt6i_gateway; +out: +dst_release ((struct dst_entry *)rt); +#endif +return dst; +} +struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side
@@ -357,6 +612,28 @@ struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb); + +switch (sa_fam) { +case AF_INET : + addr4 =ovpn_nexthop_from_rt4 (ovpn,ip_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_rt6 (ovpn,ipv6_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6); +break ; }return peer;
Signature · 1 line
2.43.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 15/24] ovpn: implement peer lookup logic
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 225
Quoted 3 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +3
-0+static struct in6_addr ovpn_nexthop_from_skb6 (struct sk_buff *skb) +{ +struct rt6_info *rt = (struct rt6_info *)skb_rtable (skb);
skb_rt6_info?
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 238
Quoted 13 lines
drivers/net/ovpn/peer.c +13
-0+ +if (!rt || !(rt->rt6i_flags &RTF_GATEWAY )) +return ipv6_hdr (skb)->daddr; + +return rt->rt6i_gateway; +} + +/** +* ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address +* @head: list head to search +* @addr: VPN IPv4 to use as search key +* +* Return: the peer if found or NULL otherwise
The doc for all those ovpn_peer_get_* functions could indicate that on success, a reference on the peer is held.
[...]
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 280
Quoted 13 lines
drivers/net/ovpn/peer.c +13
-0+static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6 (struct hlist_head *head, +struct in6_addr *addr) +{ +struct ovpn_peer *tmp, *peer =NULL ; +int i; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr6) { +for (i =0 ; i <4 ; i++) { +if (addr->s6_addr32[i] != + tmp->vpn_addrs.ipv6.s6_addr32[i]) +continue ; + }
ipv6_addr_equal
[...]
drivers/net/ovpn/peer.c ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn, · line 388
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+default : +return NULL ; + } + + index =ovpn_peer_index (ovpn->peers.by_transp_addr, &ss, sa_len); + head = &ovpn->peers.by_transp_addr[index];
Maybe worth adding a get_bucket helper (with a better name :)) instead of ovpn_peer_index, since all uses of ovpn_peer_index are followed by a "head = TBL[index]" (or direct use in some hlist iterator), but the index itself is not used later on, only the bucket.
drivers/net/ovpn/peer.c ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn, · line 393
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_transp_addr) { + found =ovpn_peer_transp_match (tmp, &ss); +if (!found)
nit: call ovpn_peer_transp_match directly and drop the found variable
Quoted 25 lines
drivers/net/ovpn/peer.c +14
-2+continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }@@ -303,10 +427,28 @@ 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; +u32 index;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); +
drivers/net/ovpn/peer.c static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn, · line 446
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+ index =ovpn_peer_index (ovpn->peers.by_id, &peer_id,sizeof (peer_id)); + head = &ovpn->peers.by_id[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ;
Can there ever be multiple peers with the same id? (ie, is it worth continuing the loop if this fails? the same question probably applies to ovpn_peer_get_by_transp_addr as well)
Quoted 34 lines
drivers/net/ovpn/peer.c +20
-0+ peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }@@ -328,6 +470,11 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side @@ -338,15 +485,123 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb); + +switch (sa_fam) { +case AF_INET : + addr4 =ovpn_nexthop_from_skb4 (skb); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index];
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 508
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+ + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_skb6 (skb); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6);
The index -> head -> peer code is identical in get_by_dst and get_by_src, it could be stuffed into ovpn_peer_get_by_vpn_addr{4,6}.
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 513
Quoted 5 lines
drivers/net/ovpn/peer.c +1
-0+break ; }return peer; }
[snip the _rt4 variant, comments apply to both]
Review struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 515
Quoted 2 lines
(quoted diff) +2
-0+/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destination
I'm a bit confused by this talk about "destination" when those two functions are then used with the source address from the packet, from a function called "get_by_src".
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 557
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+* @ovpn: the private data representing the current VPN session +* @dst: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IO of the nexthop to be used
"the IO"?
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 561
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+* 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 the dst itself otherwise
"the dst" tends to refer to a dst_entry, maybe "or @dst otherwise"? (though I'm not sure that's valid kdoc)
(also for ovpn_nexthop_from_rt4)
Quoted 13 lines
drivers/net/ovpn/peer.c +13
-0+*/ +static struct in6_addr ovpn_nexthop_from_rt6 (struct ovpn_struct *ovpn, +struct in6_addr dst) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +struct dst_entry *entry; +struct rt6_info *rt; +struct flowi6 fl = { + .daddr = dst, + }; + + entry = ipv6_stub->ipv6_dst_lookup_flow (dev_net (ovpn->dev),NULL , &fl, +NULL );
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 584
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+if (IS_ERR (entry)) { +net_dbg_ratelimited ("%s: no route to host %pI6c\n" , __func__, + &dst); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dst; + } + + rt = container_of(entry, struct rt6_info, dst);
dst_rt6_info(entry)
Quoted 24 lines
drivers/net/ovpn/peer.c +16
-0+ +if (!(rt->rt6i_flags &RTF_GATEWAY )) + goto out; + + dst = rt->rt6i_gateway; +out: +dst_release ((struct dst_entry *)rt); +#endif +return dst; +} +struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 620
Quoted 9 lines
drivers/net/ovpn/peer.c +6
-0@@ -357,6 +612,28 @@ struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb); + +switch (sa_fam) {
nit:
switch (skb_protocol_to_family(skb))
seems a bit more readable to me (also in ovpn_peer_get_by_dst) - and saves you from reverse xmas tree complaints (sa_fam should have been after addr6)
Quoted 23 lines
drivers/net/ovpn/peer.c +16
-0+case AF_INET : + addr4 =ovpn_nexthop_from_rt4 (ovpn,ip_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_rt6 (ovpn,ipv6_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6); +break ; }return peer;Signature · 1 line
2.43.2
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 15/24] ovpn: implement peer lookup logic
Quoted 6 lines · Sabrina Dubroca
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 225
Quoted 3 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +3
-0+static struct in6_addr ovpn_nexthop_from_skb6 (struct sk_buff *skb) +{ +struct rt6_info *rt = (struct rt6_info *)skb_rtable (skb);
skb_rt6_info?
Yes! I have been looking for this guy all over the place in sk_buff.h....it was just in another header :) thanks!
Quoted 17 lines
Quoted 4 lines
drivers/net/ovpn/peer.c +3
-0+ +if (!rt || !(rt->rt6i_flags &RTF_GATEWAY )) +return ipv6_hdr (skb)->daddr;drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 238
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+ +return rt->rt6i_gateway; +} + +/** +* ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address +* @head: list head to search +* @addr: VPN IPv4 to use as search key +* +* Return: the peer if found or NULL otherwise The doc for all those ovpn_peer_get_* functions could indicate that on success, a reference on the peer is held.
ACK
Quoted 18 lines
[...]
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 280
Quoted 13 lines
drivers/net/ovpn/peer.c +13
-0+static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6 (struct hlist_head *head, +struct in6_addr *addr) +{ +struct ovpn_peer *tmp, *peer =NULL ; +int i; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr6) { +for (i =0 ; i <4 ; i++) { +if (addr->s6_addr32[i] != + tmp->vpn_addrs.ipv6.s6_addr32[i]) +continue ; + }
ipv6_addr_equal
Thanks
Quoted 13 lines
[...]
drivers/net/ovpn/peer.c ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn, · line 388
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+default : +return NULL ; + } + + index =ovpn_peer_index (ovpn->peers.by_transp_addr, &ss, sa_len); + head = &ovpn->peers.by_transp_addr[index];Maybe worth adding a
get_buckethelper (with a better name :)) instead ofovpn_peer_index, since all uses ofovpn_peer_indexare followed by a "head = TBL[index]" (or direct use in some hlist iterator), but the index itself is not used later on, only the bucket.
yup, good idea
Quoted 8 lines
drivers/net/ovpn/peer.c ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn, · line 393
Quoted 6 lines
drivers/net/ovpn/peer.c +5
-0+ +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_transp_addr) { + found =ovpn_peer_transp_match (tmp, &ss); +if (!found)nit: call
ovpn_peer_transp_matchdirectly and drop the found variable
ACK.
I presume it's a leftover from the past, otherwise it wouldn't make much sense.
Quoted 32 lines
Quoted 32 lines
drivers/net/ovpn/peer.c +20
-2+continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }@@ -303,10 +427,28 @@ 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; +u32 index;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); + + index =ovpn_peer_index (ovpn->peers.by_id, &peer_id,sizeof (peer_id)); + head = &ovpn->peers.by_id[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id)
Quoted 8 lines
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 251
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+continue ; + +if (!ovpn_peer_hold (tmp)) +continue ;Can there ever be multiple peers with the same id? (ie, is it worth continuing the loop if this fails? the same question probably applies to
ovpn_peer_get_by_transp_addras well)
Well, not at the same time, but theoretically we could re-use the ID of a peer that is being released (i.e. still in the list but refcnt at 0) because it won't be returned by this lookup.
This said, I truly believe it's impossible for a peer to have refcnt 0 and still being in the list:
Either
* delete on the peer was not yet called, thus peer is in the list and the last reference wasn't yet dropped
* delete on the peer was called, thus peer cannot be in the list anymore and refcnt may or may not be 0...
Quoted 41 lines
Quoted 41 lines
drivers/net/ovpn/peer.c +25
-0+ peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }@@ -328,6 +470,11 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side @@ -338,15 +485,123 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb); + +switch (sa_fam) { +case AF_INET : + addr4 =ovpn_nexthop_from_skb4 (skb); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_skb6 (skb);
Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 508
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6);The index -> head -> peer code is identical in
get_by_dstandget_by_src, it could be stuffed into ovpn_peer_get_by_vpn_addr{4,6}.
hm yeah, you're right. I'll do it!
Quoted 9 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 513
Quoted 6 lines
drivers/net/ovpn/peer.c +1
-0+break ; }return peer; }[snip the
_rt4variant, comments apply to both]
Quoted 6 lines
Review struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 515
Quoted 2 lines
(quoted diff) +2
-0+/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destinationI'm a bit confused by this talk about "destination" when those two functions are then used with the source address from the packet, from a function called "
get_by_src".
well, in my brain a next hop can exists only when I want to reach a certain destination. Therefore, at a low level, the terms nextop and destination always need to go hand in hand.
This said, when implementing RPF (Reverse Path Filtering) I need to imagine that I want to route to the source IP of the incoming packet. If the nexthop I looked up matches the peer the packet came from, then everything is fine.
makes sense?
[FTR I have already renamed/changed get_by_src into check_by_src, because I don't need to truly extract a peer and get a reference, but I only need to perform the aforementioned comparison.]
Quoted 7 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 557
Quoted 5 lines
drivers/net/ovpn/peer.c +4
-0+* @ovpn: the private data representing the current VPN session +* @dst: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IO of the nexthop to be used "the IO"?
typ0: "the IP"
Quoted 7 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 561
Quoted 5 lines
drivers/net/ovpn/peer.c +4
-0+* 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 the dst itself otherwise "the dst" tends to refer to a
dst_entry, maybe "or @dst otherwise"?
it refers to @dst (the function argument). That's basically the case where the destination is "onlink" and thus it is the nexthop (basically the destination is the connected peer).
Quoted 21 lines
(though I'm not sure that's valid kdoc)
(also for
ovpn_nexthop_from_rt4)Quoted 17 lines
drivers/net/ovpn/peer.c +17
-0+*/ +static struct in6_addr ovpn_nexthop_from_rt6 (struct ovpn_struct *ovpn, +struct in6_addr dst) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +struct dst_entry *entry; +struct rt6_info *rt; +struct flowi6 fl = { + .daddr = dst, + }; + + 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__, + &dst); +/* if we end up here this packet is probably going to be
Quoted 8 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 584
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+* thrown away later +*/ +return dst; + } + + rt = container_of(entry, struct rt6_info, dst);
dst_rt6_info(entry)
Oh, I see this just came to life in 6.10-rc1. Thanks!
Quoted 32 lines
Quoted 32 lines
drivers/net/ovpn/peer.c +20
-0+ +if (!(rt->rt6i_flags &RTF_GATEWAY )) + goto out; + + dst = rt->rt6i_gateway; +out: +dst_release ((struct dst_entry *)rt); +#endif +return dst; +} +struct ovpn_peer *ovpn_peer_get_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb) {struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head; +sa_family_t sa_fam; +struct in6_addr addr6; +__be32 addr4; +u32 index;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side @@ -357,6 +612,28 @@ struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn,if (likely (tmp &&ovpn_peer_hold (tmp))) peer = tmp;rcu_read_unlock (); +return peer; + } + + sa_fam =skb_protocol_to_family (skb);
Quoted 8 lines
Review struct ovpn_peer *ovpn_peer_get_by_src(struct ovpn_struct *ovpn, · line 620
Quoted 2 lines
(quoted diff) +2
-0+ + switch (sa_fam) {nit:
switch (skb_protocol_to_family(skb))
seems a bit more readable to me (also inovpn_peer_get_by_dst) - and saves you from reverse xmas tree complaints (sa_famshould have been after addr6)
ACK, thanks!
Quoted 25 lines
Quoted 24 lines
drivers/net/ovpn/peer.c +16
-0+case AF_INET : + addr4 =ovpn_nexthop_from_rt4 (ovpn,ip_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr4, +sizeof (addr4)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr4 (head, &addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_rt6 (ovpn,ipv6_hdr (skb)->saddr); + index =ovpn_peer_index (ovpn->peers.by_vpn_addr, &addr6, +sizeof (addr6)); + head = &ovpn->peers.by_vpn_addr[index]; + + peer =ovpn_peer_get_by_vpn_addr6 (head, &addr6); +break ; }return peer;Signature · 1 line
2.43.2
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v3 15/24] ovpn: implement peer lookup logic
Quoted 32 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 17 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +11
-2@@ -303,10 +427,28 @@ 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; +u32 index;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); + + index =ovpn_peer_index (ovpn->peers.by_id, &peer_id,sizeof (peer_id)); + head = &ovpn->peers.by_id[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id)Quoted 8 lines
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 251
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+continue ; + +if (!ovpn_peer_hold (tmp)) +continue ;Can there ever be multiple peers with the same id? (ie, is it worth continuing the loop if this fails? the same question probably applies to
ovpn_peer_get_by_transp_addras well)Well, not at the same time, but theoretically we could re-use the ID of a peer that is being released (i.e. still in the list but refcnt at 0) because it won't be returned by this lookup.
Quoted 7 lines
This said, I truly believe it's impossible for a peer to have refcnt 0 and still being in the list:
Either
* delete on the peer was not yet called, thus peer is in the list and the last reference wasn't yet dropped
* delete on the peer was called, thus peer cannot be in the list anymore and refcnt may or may not be 0...
Ok, thanks. Let's just keep this code.
Quoted 11 lines
Quoted 6 lines
Review static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn, · line 446
Quoted 2 lines
(quoted diff) +2
-0+/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destinationI'm a bit confused by this talk about "destination" when those two functions are then used with the source address from the packet, from a function called "
get_by_src".well, in my brain a next hop can exists only when I want to reach a certain destination. Therefore, at a low level, the terms nextop and destination always need to go hand in hand.
Quoted 5 lines
This said, when implementing RPF (Reverse Path Filtering) I need to imagine that I want to route to the source IP of the incoming packet. If the nexthop I looked up matches the peer the packet came from, then everything is fine.
makes sense?
Yeah, that's fair.
Quoted 4 lines
[FTR I have already renamed/changed
get_by_srcintocheck_by_src, because I don't need to truly extract a peer and get a reference, but I only need to perform the aforementioned comparison.]
Ok.
Quoted 15 lines
Quoted 6 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 557
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+* @ovpn: the private data representing the current VPN session +* @dst: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IO of the nexthop to be used "the IO"?
typ0: "the IP"
Quoted 6 lines
Quoted 5 lines
drivers/net/ovpn/peer.c +4
-0+* 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 the dst itself otherwise
Quoted 5 lines
Quoted 1 line
"the dst" tends to refer to a
dst_entry, maybe "or @dst otherwise"?it refers to @dst (the function argument). That's basically the case where the destination is "onlink" and thus it is the nexthop (basically the destination is the connected peer).
I understand that, it's just the wording "the dst" that I'm nitpicking. s/dst/addr/ would help easily-confused people like me (for both "the dst" and my confusion with source vs destination in caller/callee), but I can live with this.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v3 15/24] ovpn: implement peer lookup logic
Quoted 41 lines · Sabrina Dubroca
Quoted 32 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 17 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +11
-2@@ -303,10 +427,28 @@ 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; +u32 index;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); + + index =ovpn_peer_index (ovpn->peers.by_id, &peer_id,sizeof (peer_id)); + head = &ovpn->peers.by_id[index]; + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id)Quoted 8 lines
drivers/net/ovpn/peer.c static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, · line 251
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+continue ; + +if (!ovpn_peer_hold (tmp)) +continue ;Can there ever be multiple peers with the same id? (ie, is it worth continuing the loop if this fails? the same question probably applies to
ovpn_peer_get_by_transp_addras well)Well, not at the same time, but theoretically we could re-use the ID of a peer that is being released (i.e. still in the list but refcnt at 0) because it won't be returned by this lookup.
Quoted 7 lines
This said, I truly believe it's impossible for a peer to have refcnt 0 and still being in the list:
Either
* delete on the peer was not yet called, thus peer is in the list and the last reference wasn't yet dropped
* delete on the peer was called, thus peer cannot be in the list anymore and refcnt may or may not be 0...
Quoted 1 line
Ok, thanks. Let's just keep this code.
ok
Quoted 45 lines
Quoted 13 lines
Quoted 8 lines
Review static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn, · line 446
Quoted 4 lines
(quoted diff) +2
-0+/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destinationI'm a bit confused by this talk about "destination" when those two functions are then used with the source address from the packet, from a function called "
get_by_src".well, in my brain a next hop can exists only when I want to reach a certain destination. Therefore, at a low level, the terms nextop and destination always need to go hand in hand.
Quoted 5 lines
This said, when implementing RPF (Reverse Path Filtering) I need to imagine that I want to route to the source IP of the incoming packet. If the nexthop I looked up matches the peer the packet came from, then everything is fine.
makes sense?
Yeah, that's fair.
Quoted 4 lines
[FTR I have already renamed/changed
get_by_srcintocheck_by_src, because I don't need to truly extract a peer and get a reference, but I only need to perform the aforementioned comparison.]Ok.
Quoted 17 lines
Quoted 6 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 557
Quoted 4 lines
drivers/net/ovpn/peer.c +4
-0+* @ovpn: the private data representing the current VPN session +* @dst: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IO of the nexthop to be used "the IO"?
typ0: "the IP"
Quoted 7 lines
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, · line 561
Quoted 5 lines
drivers/net/ovpn/peer.c +4
-0+* 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 the dst itself otherwise "the dst" tends to refer to a
dst_entry, maybe "or @dst otherwise"?
Quoted 8 lines
Quoted 3 lines
it refers to @dst (the function argument). That's basically the case where the destination is "onlink" and thus it is the nexthop (basically the destination is the connected peer).
I understand that, it's just the wording "the dst" that I'm nitpicking. s/dst/addr/ would help easily-confused people like me (for both "the dst" and my confusion with source vs destination in caller/callee), but I can live with this.
Oh ok, now I understand your concern.
I will reword this part a bit and add a comment in the caller to clarify why we invoke nexthop_from_rt4/6 passing the source address as param.
Cheers,
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v3 16/24 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 | 88 +++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/io.h | 6 +++ drivers/net/ovpn/peer.c | 65 ++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 51 ++++++++++++++++++++++++ 4 files changed, 210 insertions(+)
drivers/net/ovpn/io.c +88 -0
@@ -24,6 +24,31 @@#include "tcp.h" #include "udp.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)); +} +int ovpn_struct_init (struct net_device *dev) {struct ovpn_struct *ovpn =netdev_priv (dev);
@@ -190,6 +215,9 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) goto drop; } +/* note event of authenticated packet received for keepalive */ +ovpn_peer_keepalive_recv_reset (peer); +/* increment RX stats */ ovpn_peer_stats_increment_rx (&peer->vpn_stats, skb->len);
@@ -208,6 +236,18 @@ static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) goto drop; } +/* check if special OpenVPN message */ +if (ovpn_is_keepalive (skb)) { +netdev_dbg (peer->ovpn->dev, +"ping received from peer %u\n" , peer->id); +/* not an error */ +consume_skb (skb); +/* inform the caller that NAPI should not be scheduled +* for this packet +*/ +return -1 ; + } +netdev_dbg (peer->ovpn->dev,"unsupported protocol received from peer %u\n" , peer->id);
@@ -352,6 +392,11 @@ void ovpn_encrypt_work(struct work_struct *work)break ; } } + +/* note event of authenticated packet xmit for +* keepalive +*/ +ovpn_peer_keepalive_xmit_reset (peer); }/* give a chance to be rescheduled if needed */
@@ -456,3 +501,46 @@ 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_queue_skb (ovpn, skb, peer); +} + +void ovpn_keepalive_xmit (struct ovpn_peer *peer) +{ +ovpn_xmit_special (peer, ovpn_keepalive_message, +sizeof (ovpn_keepalive_message)); +}
drivers/net/ovpn/io.h +6 -0
@@ -20,6 +20,12 @@ int ovpn_struct_init(struct net_device *dev);netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev);int ovpn_napi_poll (struct napi_struct *napi,int budget); +/** +* ovpn_keepalive_xmit - send keepalive message to peer +* @peer: the peer to send the message to +*/ +void ovpn_keepalive_xmit (struct ovpn_peer *peer); +int ovpn_recv (struct ovpn_struct *ovpn,struct ovpn_peer *peer,struct sk_buff *skb);
drivers/net/ovpn/peer.c +65 -0
89 lines not on this page · raw message
drivers/net/ovpn/peer.h +51 -0
67 lines not on this page · raw message
Signature · 1 line
2.43.2