Introducing OpenVPN Data Channel Offload
Subsystems (6)
Antonio Quartulli
maintainerv6 cover Introducing OpenVPN Data Channel Offload
Hi all,
This is the 6th version of the ovpn patchset.
Notable changes from v5 are:
* moved NETIF_F_LLTX only to features
* added missing call to crypto_key_slot_put() in encrypt_done()
* return also IFINDEX upon nl_new_iface_doit() success
* convert struct ovpn_sockaddr to union
* rename ovpn_bind->sa to ovpn_bind->remote
* added netdevice_tracker to netdev_hold/put when possible
* moved ovpn_peer_index() change to related patch
* removed dev_core_stats_rx_dropped_inc after gro_cells_receive()
* moved call to kill_primary_key to related patch
* passed key_id to userspace when killing key
* passed skb to aead_request_set_callback()
* added missing call to crypto_key_slot_put() in decrypt_post()
* got rid of ptr_ring.h
* removed extra/unneded memset() on cb in TCP code
* made sure to call notify_swap_key
* called notify_del_peer before releasing netdev ref
* converted checks in nl_set_peer_doit to hard requirements
* removed useless keepalive_set boolean variable
* moved kzalloc for ovpn->peers to ndo_init() cb
* added size check in ovpn_is_keepalive()
* drop ovpn_keepalive_xmit() wrapper
* use new helper __skb_put_data()
* hold peer->lock in ovpn_peer_float()
* use ipv6_addr_equal()
* hold peer->lock in update_local_endpoint()
* use hlist_nulls for by_transp_addr due to float rehashing
* switched to CHECKSUM_NONE in RX path
* bailed out when cannot retain peer ref in encrypt_one()
* hold peer ref in ovpn_tcp_sendmsg()
* bail out in case of missing peer ref in ovpn_tcp_rcv()
* moved cancel_work_sync() and strp_done() out of rcu read lock area
* enable extended ack in userspace testing tool
* introduced one lock per hashtable, to avoid conflicting lock operations
* added some extra doc regarding IV and key life cycle
* some more minor reshuffling, mostly as consequence of the above..
Please note that patches previously reviewed by Andrew Lunn have retained the Reviewed-by tag as they have been simply rebased without any modification.
The latest code can also be found at:
https://github.com/OpenVPN/linux-kernel-ovpn
Thanks a lot!
Best Regards,
Antonio Quartulli
OpenVPN Inc.
======================
Antonio Quartulli (25):
netlink: add NLA_POLICY_MAX_LEN macro
rtnetlink: don't crash on unregister if no dellink exists
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: implement interface creation/destruction via netlink
ovpn: keep carrier always on
ovpn: introduce the ovpn_peer object
ovpn: introduce the ovpn_socket object
ovpn: implement basic TX path (UDP)
ovpn: implement basic RX path (UDP)
ovpn: implement packet processing
ovpn: store tunnel and transport statistics
ovpn: implement TCP transport
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
ovpn: implement keepalive mechanism
ovpn: add support for updating local UDP endpoint
ovpn: add support for peer floating
ovpn: implement peer add/dump/delete via netlink
ovpn: implement key add/del/swap via netlink
ovpn: kill key and notify userspace in case of IV exhaustion
ovpn: notify userspace when a peer is deleted
ovpn: add basic ethtool support
testing/selftest: add test tool and scripts for ovpn module
Documentation/netlink/specs/ovpn.yaml | 328 +++ MAINTAINERS | 8 + drivers/net/Kconfig | 14 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 54 + drivers/net/ovpn/bind.h | 117 ++ drivers/net/ovpn/crypto.c | 168 ++ drivers/net/ovpn/crypto.h | 138 ++ drivers/net/ovpn/crypto_aead.c | 376 ++++ drivers/net/ovpn/crypto_aead.h | 31 + drivers/net/ovpn/io.c | 439 ++++ drivers/net/ovpn/io.h | 25 + drivers/net/ovpn/main.c | 371 ++++ drivers/net/ovpn/main.h | 29 + drivers/net/ovpn/netlink-gen.c | 206 ++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 1052 ++++++++++ drivers/net/ovpn/netlink.h | 18 + drivers/net/ovpn/ovpnstruct.h | 63 + drivers/net/ovpn/packet.h | 40 + drivers/net/ovpn/peer.c | 1187 +++++++++++ drivers/net/ovpn/peer.h | 171 ++ drivers/net/ovpn/pktid.c | 130 ++ drivers/net/ovpn/pktid.h | 87 + drivers/net/ovpn/proto.h | 104 + drivers/net/ovpn/skb.h | 61 + drivers/net/ovpn/socket.c | 165 ++ drivers/net/ovpn/socket.h | 53 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 47 + drivers/net/ovpn/tcp.c | 512 +++++ drivers/net/ovpn/tcp.h | 42 + drivers/net/ovpn/udp.c | 404 ++++ drivers/net/ovpn/udp.h | 26 + include/net/netlink.h | 1 + include/uapi/linux/ovpn.h | 108 + include/uapi/linux/udp.h | 1 + net/core/rtnetlink.c | 8 +- tools/net/ynl/ynl-gen-c.py | 2 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 17 + tools/testing/selftests/net/ovpn/config | 8 + .../selftests/net/ovpn/data-test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/data-test.sh | 150 ++ tools/testing/selftests/net/ovpn/data64.key | 5 + .../testing/selftests/net/ovpn/float-test.sh | 115 ++ tools/testing/selftests/net/ovpn/ovpn-cli.c | 1820 +++++++++++++++++ .../testing/selftests/net/ovpn/tcp_peers.txt | 1 + .../testing/selftests/net/ovpn/udp_peers.txt | 5 + 51 files changed, 8802 insertions(+), 2 deletions(-) create mode 100644 Documentation/netlink/specs/ovpn.yaml create mode 100644 drivers/net/ovpn/Makefile create mode 100644 drivers/net/ovpn/bind.c create mode 100644 drivers/net/ovpn/bind.h create mode 100644 drivers/net/ovpn/crypto.c create mode 100644 drivers/net/ovpn/crypto.h create mode 100644 drivers/net/ovpn/crypto_aead.c create mode 100644 drivers/net/ovpn/crypto_aead.h create mode 100644 drivers/net/ovpn/io.c create mode 100644 drivers/net/ovpn/io.h create mode 100644 drivers/net/ovpn/main.c create mode 100644 drivers/net/ovpn/main.h create mode 100644 drivers/net/ovpn/netlink-gen.c create mode 100644 drivers/net/ovpn/netlink-gen.h create mode 100644 drivers/net/ovpn/netlink.c create mode 100644 drivers/net/ovpn/netlink.h create mode 100644 drivers/net/ovpn/ovpnstruct.h create mode 100644 drivers/net/ovpn/packet.h create mode 100644 drivers/net/ovpn/peer.c create mode 100644 drivers/net/ovpn/peer.h create mode 100644 drivers/net/ovpn/pktid.c create mode 100644 drivers/net/ovpn/pktid.h create mode 100644 drivers/net/ovpn/proto.h create mode 100644 drivers/net/ovpn/skb.h create mode 100644 drivers/net/ovpn/socket.c create mode 100644 drivers/net/ovpn/socket.h create mode 100644 drivers/net/ovpn/stats.c create mode 100644 drivers/net/ovpn/stats.h create mode 100644 drivers/net/ovpn/tcp.c create mode 100644 drivers/net/ovpn/tcp.h create mode 100644 drivers/net/ovpn/udp.c create mode 100644 drivers/net/ovpn/udp.h create mode 100644 include/uapi/linux/ovpn.h create mode 100644 tools/testing/selftests/net/ovpn/.gitignore create mode 100644 tools/testing/selftests/net/ovpn/Makefile create mode 100644 tools/testing/selftests/net/ovpn/config create mode 100755 tools/testing/selftests/net/ovpn/data-test-tcp.sh create mode 100755 tools/testing/selftests/net/ovpn/data-test.sh create mode 100644 tools/testing/selftests/net/ovpn/data64.key create mode 100755 tools/testing/selftests/net/ovpn/float-test.sh create mode 100644 tools/testing/selftests/net/ovpn/ovpn-cli.c create mode 100644 tools/testing/selftests/net/ovpn/tcp_peers.txt create mode 100644 tools/testing/selftests/net/ovpn/udp_peers.txt
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 1/25 netlink: add NLA_POLICY_MAX_LEN macro
Similarly to NLA_POLICY_MIN_LEN, NLA_POLICY_MAX_LEN defines a policy with a maximum length value.
The netlink generator for YAML specs has been extended accordingly.
--- include/net/netlink.h | 1 + tools/net/ynl/ynl-gen-c.py | 2 ++ 2 files changed, 3 insertions(+)
include/net/netlink.h +1 -0
@@ -469,6 +469,7 @@ struct nla_policy { .max = _len \ }#define NLA_POLICY_MIN_LEN (_len) NLA_POLICY_MIN(NLA_BINARY, _len) +#define NLA_POLICY_MAX_LEN (_len) NLA_POLICY_MAX(NLA_BINARY, _len)/** * struct nl_info - netlink source information
tools/net/ynl/ynl-gen-c.py +2 -0
@@ -466,6 +466,8 @@ class TypeBinary(Type): def _attr_policy(self, policy): if 'exact-len' in self.checks: mem = 'NLA_POLICY_EXACT_LEN(' + str(self.get_limit('exact-len')) + ')' + elif 'max-len' in self.checks: + mem = 'NLA_POLICY_MAX_LEN(' + str(self.get_limit('max-len')) + ')' else: mem = '{ ' if len(self.checks) == 1 and 'min-len' in self.checks:
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 2/25 rtnetlink: don't crash on unregister if no dellink exists
In reply to Antonio Quartullirtnl_unregister_ops calls __rtnl_links_kill which calls unconditionally ops->dellink on any related interface.
However, if a module hasn't implemented dellink, this invocation will crash.
Add a check to avoid calling dellink when NULL and rather invoke netdevice_unregister_queue to schedule unregistering the device as usual.
--- net/core/rtnetlink.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/rtnetlink.c +6 -2
@@ -453,8 +453,12 @@ static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)LIST_HEAD (list_kill);for_each_netdev (net, dev) { -if (dev->rtnl_link_ops == ops) - ops->dellink (dev, &list_kill); +if (dev->rtnl_link_ops == ops) { +if (ops->dellink) + ops->dellink (dev, &list_kill); +else +unregister_netdevice_queue (dev, &list_kill); + } }unregister_netdevice_many (&list_kill); }
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 3/25 net: introduce OpenVPN Data Channel Offload (ovpn)
In reply to Antonio QuartulliOpenVPN is a userspace software existing since around 2005 that allows users to create secure tunnels.
So far OpenVPN has implemented all operations in userspace, which implies several back and forth between kernel and user land in order to process packets (encapsulate/decapsulate, encrypt/decrypt, rerouting..).
With ovpn we intend to move the fast path (data channel) entirely in kernel space and thus improve user measured throughput over the tunnel.
ovpn is implemented as a simple virtual network device driver, that can be manipulated by means of the standard RTNL APIs. A device of kind ovpn allows only IPv4/6 traffic and can be of type:
* P2P (peer-to-peer): any packet sent over the interface will be encapsulated and transmitted to the other side (typical OpenVPN client or peer-to-peer behaviour);
* P2MP (point-to-multipoint): packets sent over the interface are transmitted to peers based on existing routes (typical OpenVPN server behaviour).
After the interface has been created, OpenVPN in userspace can configure it using a new Netlink API. Specifically it is possible to manage peers and their keys.
The OpenVPN control channel is multiplexed over the same transport socket by means of OP codes. Anything that is not DATA_V2 (OpenVPN OP code for data traffic) is sent to userspace and handled there. This way the ovpn codebase is kept as compact as possible while focusing on handling data traffic only (fast path).
Any OpenVPN control feature (like cipher negotiation, TLS handshake, rekeying, etc.) is still fully handled by the userspace process.
When userspace establishes a new connection with a peer, it first performs the handshake and then passes the socket to the ovpn kernel module, which takes ownership. From this moment on ovpn will handle data traffic for the new peer.
When control packets are received on the link, they are forwarded to userspace through the same transport socket they were received on, as userspace is still listening to them.
Some events (like peer deletion) are sent to a Netlink multicast group.
Although it wasn't easy to convince the community, ovpn implements only a limited number of the data-channel features supported by the userspace program.
Each feature that made it to ovpn was attentively vetted to avoid carrying too much legacy along with us (and to give a clear cut to old and probalby-not-so-useful features).
Notably, only encryption using AEAD ciphers (specifically ChaCha20Poly1305 and AES-GCM) was implemented. Supporting any other cipher out there was not deemed useful.
Both UDP and TCP sockets ae supported.
As explained above, in case of P2MP mode, OpenVPN will use the main system routing table to decide which packet goes to which peer. This implies that no routing table was re-implemented in the ovpn kernel module.
This kernel module can be enabled by selecting the CONFIG_OVPN entry in the networking drivers section.
NOTE: this first patch introduces the very basic framework only. Features are then added patch by patch, however, although each patch will compile and possibly not break at runtime, only after having applied the full set it is expected to see the ovpn module fully working.
--- MAINTAINERS | 7 +++ drivers/net/Kconfig | 13 +++++ drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 11 ++++ drivers/net/ovpn/io.c | 22 ++++++++ drivers/net/ovpn/io.h | 15 ++++++ drivers/net/ovpn/main.c | 109 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/main.h | 15 ++++++ include/uapi/linux/udp.h | 1 + 9 files changed, 194 insertions(+) create mode 100644 drivers/net/ovpn/Makefile create mode 100644 drivers/net/ovpn/io.c create mode 100644 drivers/net/ovpn/io.h create mode 100644 drivers/net/ovpn/main.c create mode 100644 drivers/net/ovpn/main.h
MAINTAINERS +7 -0
@@ -17215,6 +17215,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/vfs.git F: Documentation/filesystems/overlayfs.rst F: fs/overlayfs/ +OPENVPN DATA CHANNEL OFFLOAD +M: Antonio Quartulli <[email protected]> +L: [email protected] (moderated for non-subscribers) +L: [email protected] +S: Maintained +F: drivers/net/ovpn/ + P54 WIRELESS DRIVER M: Christian Lamparter <[email protected]> L: [email protected]
drivers/net/Kconfig +13 -0
@@ -115,6 +115,19 @@ config WIREGUARD_DEBUG Say N here unless you know what you're doing. +config OVPN + tristate "OpenVPN data channel offload" + depends on NET && INET + select NET_UDP_TUNNEL + select DST_CACHE + select CRYPTO + select CRYPTO_AES + select CRYPTO_GCM + select CRYPTO_CHACHA20POLY1305 + help + This module enhances the performance of the OpenVPN userspace software + by offloading the data channel processing to kernelspace. + config EQUALIZER tristate "EQL (serial line load balancing) support" help
drivers/net/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_IPVLAN) += ipvlan/ obj-$(CONFIG_IPVTAP) += ipvlan/ obj-$(CONFIG_DUMMY) += dummy.o obj-$(CONFIG_WIREGUARD) += wireguard/ +obj-$(CONFIG_OVPN) += ovpn/ obj-$(CONFIG_EQUALIZER) += eql.o obj-$(CONFIG_IFB) += ifb.o obj-$(CONFIG_MACSEC) += macsec.o
drivers/net/ovpn/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# ovpn -- OpenVPN data channel offload in kernel space +# +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +obj-$(CONFIG_OVPN) := ovpn.o +ovpn-y += main.o +ovpn-y += io.o
drivers/net/ovpn/io.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#include <linux/netdevice.h> +#include <linux/skbuff.h> + +#include "io.h" + +/* Send user data to the network +*/ +netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) +{ +skb_tx_error (skb); +kfree_skb (skb); +return NET_XMIT_DROP ; +}
drivers/net/ovpn/io.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_OVPN_H_ +#define _NET_OVPN_OVPN_H_ + +netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); + +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c new file +109 -0
109 lines not on this page · raw message
drivers/net/ovpn/main.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_MAIN_H_ +#define _NET_OVPN_MAIN_H_ + +bool ovpn_dev_is_valid (const struct net_device *dev); + +#endif /* _NET_OVPN_MAIN_H_ */
include/uapi/linux/udp.h +1 -0
@@ -43,5 +43,6 @@ struct udphdr {#define UDP_ENCAP_GTP1U 5/* 3GPP TS 29.060 */ #define UDP_ENCAP_RXRPC 6#define TCP_ENCAP_ESPINTCP 7/* Yikes, this is really xfrm encap types. */ +#define UDP_ENCAP_OVPNINUDP 8/* OpenVPN traffic */ #endif /* _UAPI_LINUX_UDP_H */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 03/25] net: introduce OpenVPN Data Channel Offload (ovpn)
Quoted 7 lines · Antonio Quartulli
drivers/net/Kconfig +0
-0@@ -115,6 +115,19 @@ config WIREGUARD_DEBUG Say N here unless you know what you're doing.
drivers/net/Kconfig config WIREGUARD_DEBUG · line 126
Quoted 10 lines
drivers/net/Kconfig +9
-0+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
and STREAM_PARSER for TCP encap?
Quoted 4 lines
drivers/net/Kconfig +4
-0+ help + This module enhances the performance of the OpenVPN userspace software + by offloading the data channel processing to kernelspace. +
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 03/25] net: introduce OpenVPN Data Channel Offload (ovpn)
Quoted 9 lines · Sabrina Dubroca
Quoted 8 lines · Antonio Quartulli
drivers/net/Kconfig +0
-0@@ -115,6 +115,19 @@ config WIREGUARD_DEBUG Say N here unless you know what you're doing.
Quoted 11 lines
drivers/net/Kconfig config WIREGUARD_DEBUG · line 126
Quoted 9 lines
drivers/net/Kconfig +9
-0+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_CHACHA20POLY1305and
STREAM_PARSERfor TCP encap?
Right, thanks!
Quoted 6 lines
Quoted 5 lines
drivers/net/Kconfig +4
-0+ help + This module enhances the performance of the OpenVPN userspace software + by offloading the data channel processing to kernelspace. +
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 4/25 ovpn: add basic netlink support
In reply to Antonio QuartulliThis commit introduces basic netlink support with family registration/unregistration functionalities and stub pre/post-doit.
More importantly it introduces the YAML uAPI description along with its auto-generated files:
- include/uapi/linux/ovpn.h
- drivers/net/ovpn/netlink-gen.c
- drivers/net/ovpn/netlink-gen.h
--- Documentation/netlink/specs/ovpn.yaml | 328 ++++++++++++++++++++++++++ MAINTAINERS | 1 + drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/main.c | 14 ++ drivers/net/ovpn/netlink-gen.c | 206 ++++++++++++++++ drivers/net/ovpn/netlink-gen.h | 41 ++++ drivers/net/ovpn/netlink.c | 153 ++++++++++++ drivers/net/ovpn/netlink.h | 15 ++ drivers/net/ovpn/ovpnstruct.h | 21 ++ include/uapi/linux/ovpn.h | 108 +++++++++ 10 files changed, 889 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 +328 -0
@@ -0,0 +1,328 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) +# +# Author: Antonio Quartulli <[email protected]> +# +# Copyright (c) 2024, OpenVPN Inc. +# + +name: ovpn + +protocol: genetlink + +doc: Netlink protocol to control OpenVPN network devices + +definitions: + - + type: const + name: nonce-tail-size + value: 8 + - + type: enum + name: cipher-alg + value-start: 0 + entries: [ none, aes-gcm, chacha20_poly1305 ] + - + type: enum + name: del-peer_reason + value-start: 0 + entries: [ teardown, userspace, expired, transport-error, transport_disconnect ] + - + type: enum + name: key-slot + value-start: 0 + entries: [ primary, secondary ] + - + type: enum + name: mode + value-start: 0 + entries: [ p2p, mp ] + +attribute-sets: + - + name: peer + attributes: + - + name: id + type: u32 + doc: | + The unique Id of the peer. To be used to identify peers during + operations + checks: + max: 0xFFFFFF + - + name: sockaddr-remote + type: binary + doc: | + The sockaddr_in/in6 object identifying the remote address/port of the + peer + - + name: socket + type: u32 + doc: The socket to be used to communicate with the peer + - + name: vpn-ipv4 + type: u32 + doc: The IPv4 assigned to the peer by the server + display-hint: ipv4 + - + name: vpn-ipv6 + type: binary + doc: The IPv6 assigned to the peer by the server + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-ip + type: binary + doc: The local IP to be used to send packets to the peer (UDP only) + checks: + max-len: 16 + - + name: local-port + type: u32 + doc: The local port to be used to send packets to the peer (UDP only) + checks: + min: 1 + max: u16-max + - + name: keepalive-interval + type: u32 + doc: | + The number of seconds after which a keep alive message is sent to the + peer + - + name: keepalive-timeout + type: u32 + doc: | + The number of seconds from the last activity after which the peer is + assumed dead + - + name: del-reason + type: u32 + doc: The reason why a peer was deleted + enum: del-peer_reason + - + name: keyconf + type: nest + doc: Peer specific cipher configuration + nested-attributes: keyconf + - + name: vpn-rx_bytes + type: uint + doc: Number of bytes received over the tunnel + - + name: vpn-tx_bytes + type: uint + doc: Number of bytes transmitted over the tunnel + - + name: vpn-rx_packets + type: uint + doc: Number of packets received over the tunnel + - + name: vpn-tx_packets + type: uint + doc: Number of packets transmitted over the tunnel + - + name: link-rx_bytes + type: uint + doc: Number of bytes received at the transport level + - + name: link-tx_bytes + type: uint + doc: Number of bytes transmitted at the transport level + - + name: link-rx_packets + type: u32 + doc: Number of packets received at the transport level + - + name: link-tx_packets + type: u32 + doc: Number of packets transmitted at the transport level + - + name: keyconf + attributes: + - + name: slot + type: u32 + doc: The slot where the key should be stored + enum: key-slot + - + name: key-id + doc: | + The unique ID for the key. Used to fetch the correct key upon + decryption + type: u32 + checks: + max: 7 + - + name: cipher-alg + type: u32 + doc: The cipher to be used when communicating with the peer + enum: cipher-alg + - + name: encrypt-dir + type: nest + doc: Key material for encrypt direction + nested-attributes: keydir + - + name: decrypt-dir + type: nest + doc: Key material for decrypt direction + nested-attributes: keydir + - + name: keydir + attributes: + - + name: cipher-key + type: binary + doc: The actual key to be used by the cipher + checks: + max-len: 256 + - + name: nonce-tail + type: binary + doc: | + Random nonce to be concatenated to the packet ID, in order to + obtain the actua cipher IV + checks: + exact-len: nonce-tail-size + - + name: ovpn + attributes: + - + name: ifindex + type: u32 + doc: Index of the ovpn interface to operate on + - + name: ifname + type: string + doc: Name of the ovpn interface that is being created + - + name: mode + type: u32 + enum: mode + doc: | + Oper mode instructing an interface to act as Point2Point or + MultiPoint + - + name: peer + type: nest + doc: | + The peer object containing the attributed of interest for the specific + operation + nested-attributes: peer + +operations: + list: + - + name: new-iface + attribute-set: ovpn + flags: [ admin-perm ] + doc: Create a new interface + do: + request: + attributes: + - ifname + - mode + reply: + attributes: + - ifname + - ifindex + - + name: del-iface + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing interface + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - + name: set-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add or modify a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: get-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve data about existing remote peers (or a specific one) + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + reply: + attributes: + - peer + dump: + request: + attributes: + - ifindex + reply: + attributes: + - peer + - + name: del-peer + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: set-key + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add or modify a cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: swap-keys + attribute-set: ovpn + flags: [ admin-perm ] + doc: Swap primary and secondary session keys for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: del-key + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + +mcast-groups: + list: + - + name: peers
MAINTAINERS +1 -0
@@ -17221,6 +17221,7 @@ L: [email protected] (moderated for non-subscribers) L: [email protected] S: Maintained F: drivers/net/ovpn/ +F: include/uapi/linux/ovpn.h P54 WIRELESS DRIVER M: Christian Lamparter <[email protected]>
drivers/net/ovpn/Makefile +2 -0
@@ -9,3 +9,5 @@ obj-$(CONFIG_OVPN) := ovpn.o ovpn-y += main.o ovpn-y += io.o +ovpn-y += netlink.o +ovpn-y += netlink-gen.o
drivers/net/ovpn/main.c +14 -0
@@ -7,12 +7,16 @@* James Yonan <[email protected]> */ +#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> #include <linux/version.h> #include <net/rtnetlink.h> +#include <uapi/linux/ovpn.h> +#include "ovpnstruct.h" #include "main.h" +#include "netlink.h" #include "io.h" /* Driver info */
@@ -34,6 +38,7 @@ bool ovpn_dev_is_valid(const struct net_device *dev)* therefore ifaces should be destroyed when exiting a netns */ static struct rtnl_link_ops ovpn_link_ops = { + .kind =OVPN_FAMILY_NAME , }; static int ovpn_netdev_notifier_call(struct notifier_block *nb,
@@ -86,8 +91,16 @@ static int __init ovpn_init(void) goto unreg_netdev; } + err =ovpn_nl_register (); +if (err) { +pr_err ("ovpn: can't register netlink family: %d\n" , err); + goto unreg_rtnl; + } +return 0 ; +unreg_rtnl: +rtnl_link_unregister (&ovpn_link_ops); unreg_netdev:unregister_netdevice_notifier (&ovpn_netdev_notifier);return err;
@@ -95,6 +108,7 @@ static int __init ovpn_init(void) static __exit void ovpn_cleanup(void) { +ovpn_nl_unregister ();rtnl_link_unregister (&ovpn_link_ops);unregister_netdevice_notifier (&ovpn_netdev_notifier);
drivers/net/ovpn/netlink-gen.c new file +206 -0
206 lines not on this page · raw message
drivers/net/ovpn/netlink-gen.h new file +41 -0
41 lines not on this page · raw message
drivers/net/ovpn/netlink.c new file +153 -0
153 lines not on this page · raw message
drivers/net/ovpn/netlink.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_NETLINK_H_ +#define _NET_OVPN_NETLINK_H_ + +int ovpn_nl_register (void ); +void ovpn_nl_unregister (void ); + +#endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/ovpnstruct.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_OVPNSTRUCT_H_ +#define _NET_OVPN_OVPNSTRUCT_H_ + +/** +* struct ovpn_struct - per ovpn interface state +* @dev: the actual netdev representing the tunnel +*/ +struct ovpn_struct { +struct net_device *dev; +}; + +#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
include/uapi/linux/ovpn.h new file +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.44.2
Simon Horman
reviewerRe: [PATCH net-next v6 04/25] ovpn: add basic netlink support
Quoted 11 lines · Antonio Quartulli
This commit introduces basic netlink support with family registration/unregistration functionalities and stub pre/post-doit.
More importantly it introduces the YAML uAPI description along with its auto-generated files:
-include/uapi/linux/ovpn.h
-drivers/net/ovpn/netlink-gen.c
-drivers/net/ovpn/netlink-gen.hSigned-off-by Antonio Quartulli <[email protected]>
...
Quoted 1 line
Documentation/netlink/specs/ovpn.yaml +0
-0
...
Quoted 6 lines
Documentation/netlink/specs/ovpn.yaml +6
-0+ - + name: keyconf + attributes: + - + name: slot + type: u32
Documentation/netlink/specs/ovpn.yaml line 156
Quoted 10 lines
Documentation/netlink/specs/ovpn.yaml +10
-0+ 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: 7
Hi Antonio,
Here max for keyconf key-id is 7.
...
Quoted 1 line
drivers/net/ovpn/netlink-gen.c +0
-0
...
drivers/net/ovpn/netlink-gen.c line 26
Quoted 4 lines
drivers/net/ovpn/netlink-gen.c +4
-0+/* Common nested types */ +const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRYPT_DIR +1 ] = { + [OVPN_A_KEYCONF_SLOT ] =NLA_POLICY_MAX (NLA_U32 ,1 ), + [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,2 ),
But here it is 2.
Probably the patch should be refreshed after running:tools/net/ynl/ynl-regen.sh -f
Quoted 4 lines
drivers/net/ovpn/netlink-gen.c +4
-0+ [OVPN_A_KEYCONF_CIPHER_ALG] = NLA_POLICY_MAX(NLA_U32, 2), + [OVPN_A_KEYCONF_ENCRYPT_DIR] = NLA_POLICY_NESTED(ovpn_keydir_nl_policy), + [OVPN_A_KEYCONF_DECRYPT_DIR] = NLA_POLICY_NESTED(ovpn_keydir_nl_policy), +};
...
Antonio Quartulli
maintainerRe: [PATCH net-next v6 04/25] ovpn: add basic netlink support
Hi,
Quoted 39 lines · Simon Horman
Quoted 2 lines
Documentation/netlink/specs/ovpn.yaml +0
-0...
Quoted 6 lines
Documentation/netlink/specs/ovpn.yaml +6
-0+ - + name: keyconf + attributes: + - + name: slot + type: u32Documentation/netlink/specs/ovpn.yaml line 156
Quoted 10 lines
Documentation/netlink/specs/ovpn.yaml +10
-0+ 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: 7Hi Antonio,
Here max for keyconf key-id is 7.
...
Quoted 1 line
drivers/net/ovpn/netlink-gen.c +0
-0...
drivers/net/ovpn/netlink-gen.c line 26
Quoted 4 lines
drivers/net/ovpn/netlink-gen.c +4
-0+/* Common nested types */ +const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRYPT_DIR +1 ] = { + [OVPN_A_KEYCONF_SLOT ] =NLA_POLICY_MAX (NLA_U32 ,1 ), + [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,2 ),But here it is 2.
Quoted 2 lines
Probably the patch should be refreshed after running:
tools/net/ynl/ynl-regen.sh-f
You are correct.
Thanks for pointing this out.
Will get it fixed.
Cheers,
Quoted 7 lines
Quoted 5 lines
drivers/net/ovpn/netlink-gen.c +4
-0+ [OVPN_A_KEYCONF_CIPHER_ALG] = NLA_POLICY_MAX(NLA_U32, 2), + [OVPN_A_KEYCONF_ENCRYPT_DIR] = NLA_POLICY_NESTED(ovpn_keydir_nl_policy), + [OVPN_A_KEYCONF_DECRYPT_DIR] = NLA_POLICY_NESTED(ovpn_keydir_nl_policy), +};...
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 5/25 ovpn: add basic interface creation/destruction/management routines
In reply to Antonio QuartulliAdd basic infrastructure for handling ovpn interfaces.
--- drivers/net/ovpn/main.c | 162 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/main.h | 10 +++ drivers/net/ovpn/ovpnstruct.h | 8 ++ drivers/net/ovpn/packet.h | 40 +++++++++ 4 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ovpn/packet.h
drivers/net/ovpn/main.c +159 -3
@@ -10,19 +10,62 @@#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> +#include <linux/inetdevice.h> #include <linux/version.h> +#include <net/ip.h> #include <net/rtnetlink.h> +#include <uapi/linux/if_arp.h> #include <uapi/linux/ovpn.h> #include "ovpnstruct.h" #include "main.h" #include "netlink.h" #include "io.h" +#include "packet.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"#define DRV_COPYRIGHT "(C) 2020-2024 OpenVPN, Inc." +/** +* ovpn_struct_init - Initialize the netdevice private area +* @dev: the device to initialize +* @mode: device operation mode (i.e. p2p, mp, ..) +* +* Return: 0 on success or a negative error code otherwise +*/ +static int ovpn_struct_init (struct net_device *dev,enum ovpn_mode mode) +{ +struct ovpn_struct *ovpn =netdev_priv (dev); + + ovpn->dev = dev; + ovpn->mode = mode; + +return 0 ; +} + +static void ovpn_struct_free (struct net_device *net) +{ +} + +static int ovpn_net_open (struct net_device *dev) +{ +netif_tx_start_all_queues (dev); +return 0 ; +} + +static int ovpn_net_stop (struct net_device *dev) +{ +netif_tx_stop_all_queues (dev); +return 0 ; +} + +static const struct net_device_ops ovpn_netdev_ops = { + .ndo_open = ovpn_net_open, + .ndo_stop = ovpn_net_stop, + .ndo_start_xmit = ovpn_net_xmit, +}; +/** * ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' * @dev: the interface to check
@@ -41,30 +84,143 @@ static struct rtnl_link_ops ovpn_link_ops = { .kind = OVPN_FAMILY_NAME, }; +static void ovpn_setup (struct net_device *dev) +{ +/* compute the overhead considering AEAD encryption */ +const int overhead =sizeof (u32) +NONCE_WIRE_SIZE +16 + +sizeof (struct udphdr ) + +max (sizeof (struct ipv6hdr ),sizeof (struct iphdr )); + +netdev_features_t feat =NETIF_F_SG |NETIF_F_HW_CSUM |NETIF_F_RXCSUM | +NETIF_F_GSO |NETIF_F_GSO_SOFTWARE | +NETIF_F_HIGHDMA ; + + dev->needs_free_netdev = true; + + dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; + + dev->netdev_ops = &ovpn_netdev_ops; + dev->rtnl_link_ops = &ovpn_link_ops; + + dev->priv_destructor = ovpn_struct_free; + + dev->hard_header_len =0 ; + dev->addr_len =0 ; + dev->mtu =ETH_DATA_LEN - overhead; + dev->min_mtu =IPV4_MIN_MTU ; + dev->max_mtu =IP_MAX_MTU - overhead; + + dev->type =ARPHRD_NONE ; + dev->flags =IFF_POINTOPOINT |IFF_NOARP ; + + dev->features |=NETIF_F_LLTX ; + dev->features |= feat; + dev->hw_features |= feat; + dev->hw_enc_features |= feat; + + dev->needed_headroom =OVPN_HEAD_ROOM ; + dev->needed_tailroom =OVPN_MAX_PADDING ; +} + +/** +* ovpn_iface_create - create and initialize a new 'ovpn' netdevice +* @name: the name of the new device +* @mode: the OpenVPN mode to set this device to +* @net: the netns this device should be created in +* +* A new netdevice is created and registered. +* Its private area is initialized with an empty ovpn_struct object. +* +* Return: a pointer to the new device on success or a negative error code +* otherwise +*/ +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode, +struct net *net) +{ +struct net_device *dev; +int ret; + + dev =alloc_netdev (sizeof (struct ovpn_struct ), name,NET_NAME_USER , + ovpn_setup); +if (!dev) +return ERR_PTR (-ENOMEM ); + +dev_net_set (dev, net); + + ret =ovpn_struct_init (dev, mode); +if (ret <0 ) + goto err; + +rtnl_lock (); + ret =register_netdevice (dev); +if (ret <0 ) { +netdev_err (dev,"cannot register interface: %d\n" , ret); +rtnl_unlock (); + goto err; + } +/* turn carrier explicitly off after registration, this way state is +* clearly defined +*/ +netif_carrier_off (dev); +rtnl_unlock (); + +return dev; + +err: +free_netdev (dev); +return ERR_PTR (ret); +} + +/** +* ovpn_iface_destruct - tear down netdevice +* @ovpn: the ovpn instance objected related to the interface to tear down +* +* This function takes care of tearing down an ovpn device and can be invoked +* internally or upon UNREGISTER netdev event +*/ +void ovpn_iface_destruct (struct ovpn_struct *ovpn) +{ +ASSERT_RTNL (); + +netif_carrier_off (ovpn->dev); + + ovpn->registered = false; +} +static int ovpn_netdev_notifier_call (struct notifier_block *nb,unsigned long state,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct ovpn_struct *ovpn;if (!ovpn_dev_is_valid (dev))return NOTIFY_DONE ; + ovpn =netdev_priv (dev); +switch (state) {case NETDEV_REGISTER : -/* add device to internal list for later destruction upon -* unregistration -*/ + ovpn->registered = true;break ;case NETDEV_UNREGISTER : +/* twiddle thumbs on netns device moves */ +if (dev->reg_state !=NETREG_UNREGISTERING ) +break ; +/* can be delivered multiple times, so check registered flag, * then destroy the interface */ +if (!ovpn->registered) +return NOTIFY_DONE ; + +ovpn_iface_destruct (ovpn);break ;case NETDEV_POST_INIT :case NETDEV_GOING_DOWN :case NETDEV_DOWN :case NETDEV_UP :case NETDEV_PRE_UP : +break ;default :return NOTIFY_DONE ; }
drivers/net/ovpn/main.h +10 -0
@@ -10,6 +10,16 @@#ifndef _NET_OVPN_MAIN_H_#define _NET_OVPN_MAIN_H_ +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode, +struct net *net); +void ovpn_iface_destruct (struct ovpn_struct *ovpn);bool ovpn_dev_is_valid (const struct net_device *dev); +#define SKB_HEADER_LEN \ + (max(sizeof(struct iphdr), sizeof(struct ipv6hdr)) + \ + sizeof(struct udphdr) + NET_SKB_PAD) + +#define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4) +#define OVPN_MAX_PADDING 16 +#endif /* _NET_OVPN_MAIN_H_ */
drivers/net/ovpn/ovpnstruct.h +8 -0
@@ -10,12 +10,20 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <uapi/linux/ovpn.h> +/** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel +* @registered: whether dev is still registered with netdev or not +* @mode: device operation mode (i.e. p2p, mp, ..) +* @dev_list: entry for the module wide device list */ struct ovpn_struct {struct net_device *dev; +bool registered; +enum ovpn_mode mode; +struct list_head dev_list; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/packet.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +* James Yonan <[email protected]> +*/ + +#ifndef _NET_OVPN_PACKET_H_ +#define _NET_OVPN_PACKET_H_ + +/* When the OpenVPN protocol is ran in AEAD mode, use +* the OpenVPN packet ID as the AEAD nonce: +* +* 00000005 521c3b01 4308c041 +* [seq # ] [ nonce_tail ] +* [ 12-byte full IV ] -> NONCE_SIZE +* [4-bytes -> NONCE_WIRE_SIZE +* on wire] +*/ + +/* OpenVPN nonce size */ +#define NONCE_SIZE 12 + +/* OpenVPN nonce size reduced by 8-byte nonce tail -- this is the +* size of the AEAD Associated Data (AD) sent over the wire +* and is normally the head of the IV +*/ +#define NONCE_WIRE_SIZE (NONCE_SIZE - sizeof(struct ovpn_nonce_tail)) + +/* Last 8 bytes of AEAD nonce +* Provided by userspace and usually derived from +* key material generated during TLS handshake +*/ +struct ovpn_nonce_tail { +u8 u8[OVPN_NONCE_TAIL_SIZE ]; +}; + +#endif /* _NET_OVPN_PACKET_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 6/25 ovpn: implement interface creation/destruction via netlink
In reply to Antonio QuartulliAllow userspace to create and destroy an interface using netlink commands.
--- drivers/net/ovpn/main.h | 2 ++ drivers/net/ovpn/netlink.c | 59 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-)
drivers/net/ovpn/main.h +2 -0
@@ -10,6 +10,8 @@#ifndef _NET_OVPN_MAIN_H_#define _NET_OVPN_MAIN_H_ +#define OVPN_DEFAULT_IFNAME "ovpn%d" +struct net_device *ovpn_iface_create (const char *name,enum ovpn_mode mode,struct net *net);void ovpn_iface_destruct (struct ovpn_struct *ovpn);
drivers/net/ovpn/netlink.c +57 -2
75 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 7/25 ovpn: keep carrier always on
In reply to Antonio QuartulliAn ovpn interface will keep carrier always on and let the user decide when an interface should be considered disconnected.
This way, even if an ovpn interface is not connected to any peer, it can still retain all IPs and routes and thus prevent any data leak.
--- drivers/net/ovpn/main.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/net/ovpn/main.c +7 -0
@@ -50,6 +50,13 @@ static void ovpn_struct_free(struct net_device *net)static int ovpn_net_open (struct net_device *dev) { +/* ovpn keeps the carrier always on to avoid losing IP or route +* configuration upon disconnection. This way it can prevent leaks +* of traffic outside of the VPN tunnel. +* The user may override this behaviour by tearing down the interface +* manually. +*/ +netif_carrier_on (dev);netif_tx_start_all_queues (dev);return 0 ; }
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 8/25 ovpn: introduce the ovpn_peer object
In reply to Antonio QuartulliAn ovpn_peer object holds the whole status of a remote peer (regardless whether it is a server or a client).
This includes status for crypto, tx/rx buffers, napi, etc.
Only support for one peer is introduced (P2P mode). Multi peer support is introduced with a later patch.
Along with the ovpn_peer, also the ovpn_bind object is introcued as the two are strictly related.
An ovpn_bind object wraps a sockaddr representing the local coordinates being used to talk to a specific peer.
--- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/bind.c | 58 ++++++ drivers/net/ovpn/bind.h | 117 +++++++++++ drivers/net/ovpn/main.c | 12 ++ drivers/net/ovpn/main.h | 2 + drivers/net/ovpn/ovpnstruct.h | 7 + drivers/net/ovpn/peer.c | 355 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 82 ++++++++ 8 files changed, 635 insertions(+) create mode 100644 drivers/net/ovpn/bind.c create mode 100644 drivers/net/ovpn/bind.h create mode 100644 drivers/net/ovpn/peer.c create mode 100644 drivers/net/ovpn/peer.h
drivers/net/ovpn/Makefile +2 -0
@@ -7,7 +7,9 @@ # Author: Antonio Quartulli <[email protected]> obj-$(CONFIG_OVPN) := ovpn.o +ovpn-y += bind.o ovpn-y += main.o ovpn-y += io.o ovpn-y += netlink.o ovpn-y += netlink-gen.o +ovpn-y += peer.o
drivers/net/ovpn/bind.c new file +58 -0
58 lines not on this page · raw message
drivers/net/ovpn/bind.h new file +117 -0
117 lines not on this page · raw message
drivers/net/ovpn/main.c +12 -0
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> +//#include <linux/rcupdate.h> #include <linux/version.h> #include <net/ip.h> #include <net/rtnetlink.h>
@@ -22,6 +23,7 @@#include "netlink.h" #include "io.h" #include "packet.h" +#include "peer.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -40,6 +42,7 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->dev = dev; ovpn->mode = mode; +spin_lock_init (&ovpn->lock);return 0 ; }
@@ -48,6 +51,11 @@ static void ovpn_struct_free(struct net_device *net) { } +static int ovpn_net_init (struct net_device *dev) +{ +return 0 ; +} +static int ovpn_net_open (struct net_device *dev) {/* ovpn keeps the carrier always on to avoid losing IP or route
@@ -68,6 +76,7 @@ static int ovpn_net_stop(struct net_device *dev) }static const struct net_device_ops ovpn_netdev_ops = { + .ndo_init = ovpn_net_init, .ndo_open = ovpn_net_open, .ndo_stop = ovpn_net_stop, .ndo_start_xmit = ovpn_net_xmit,
@@ -192,6 +201,9 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn)netif_carrier_off (ovpn->dev); ovpn->registered = false; + +if (ovpn->mode ==OVPN_MODE_P2P ) +ovpn_peer_release_p2p (ovpn); } static int ovpn_netdev_notifier_call(struct notifier_block *nb,
drivers/net/ovpn/main.h +2 -0
@@ -24,4 +24,6 @@ bool ovpn_dev_is_valid(const struct net_device *dev);#define OVPN_HEAD_ROOM ALIGN(16 + SKB_HEADER_LEN, 4)#define OVPN_MAX_PADDING 16 +#define OVPN_QUEUE_LEN 1024 +#endif /* _NET_OVPN_MAIN_H_ */
drivers/net/ovpn/ovpnstruct.h +7 -0
@@ -10,19 +10,26 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <net/net_trackers.h> #include <uapi/linux/ovpn.h> /** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel +* @dev_tracker: reference tracker for associated dev * @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) +* @lock: protect this object +* @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list */ struct ovpn_struct {struct net_device *dev; +netdevice_tracker dev_tracker;bool registered;enum ovpn_mode mode; +spinlock_t lock;/* protect writing to the ovpn_struct object */ +struct ovpn_peer __rcu *peer;struct list_head dev_list; };
drivers/net/ovpn/peer.c new file +355 -0
355 lines not on this page · raw message
drivers/net/ovpn/peer.h new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 9/25 ovpn: introduce the ovpn_socket object
In reply to Antonio QuartulliThis specific structure is used in the ovpn kernel module to wrap and carry around a standard kernel socket.
ovpn takes ownership of passed sockets and therefore an ovpn specific objects is attached to them for status tracking purposes.
Initially only UDP support is introduced. TCP will come in a later patch.
--- drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/socket.c | 120 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.h | 48 +++++++++++++++ drivers/net/ovpn/udp.c | 72 +++++++++++++++++++++++ drivers/net/ovpn/udp.h | 17 ++++++ 5 files changed, 259 insertions(+) create mode 100644 drivers/net/ovpn/socket.c create mode 100644 drivers/net/ovpn/socket.h create mode 100644 drivers/net/ovpn/udp.c create mode 100644 drivers/net/ovpn/udp.h
drivers/net/ovpn/Makefile +2 -0
@@ -13,3 +13,5 @@ ovpn-y += io.o ovpn-y += netlink.o ovpn-y += netlink-gen.o ovpn-y += peer.o +ovpn-y += socket.o +ovpn-y += udp.o
drivers/net/ovpn/socket.c new file +120 -0
120 lines not on this page · raw message
drivers/net/ovpn/socket.h new file +48 -0
48 lines not on this page · raw message
drivers/net/ovpn/udp.c new file +72 -0
72 lines not on this page · raw message
drivers/net/ovpn/udp.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2024 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_UDP_H_ +#define _NET_OVPN_UDP_H_ + +struct ovpn_struct ; +struct socket ; + +int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); + +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 10/25 ovpn: implement basic TX path (UDP)
In reply to Antonio QuartulliPackets sent over the ovpn interface are processed and transmitted to the connected peer, if any.
Implementation is UDP only. TCP will be added by a later patch.
Note: no crypto/encapsulation exists yet. packets are just captured and sent.
--- drivers/net/ovpn/io.c | 143 ++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 37 ++++++- drivers/net/ovpn/peer.h | 5 + drivers/net/ovpn/skb.h | 54 ++++++++++ drivers/net/ovpn/udp.c | 230 ++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/udp.h | 8 ++ 6 files changed, 474 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ovpn/skb.h
drivers/net/ovpn/io.c +142 -1
@@ -9,14 +9,155 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gso.h> #include "io.h" +#include "ovpnstruct.h" +#include "peer.h" +#include "udp.h" +#include "skb.h" + +static void ovpn_encrypt_post (struct sk_buff *skb,int ret) +{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto err; + +skb_mark_not_on_list (skb); + +switch (peer->sock->sock->sk->sk_protocol) { +case IPPROTO_UDP : +ovpn_udp_send_skb (peer->ovpn, peer, skb); +break ; +default : +/* no transport configured yet */ + goto err; + } +/* skb passed down the stack - don't free it */ + skb =NULL ; +err: +if (unlikely (skb)) { +dev_core_stats_tx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); + } +ovpn_peer_put (peer); +} + +static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; + +/* take a reference to the peer because the crypto code may run async. +* ovpn_encrypt_post() will release it upon completion +*/ +if (unlikely (!ovpn_peer_hold (peer))) { +DEBUG_NET_WARN_ON_ONCE (1 ); +return false; + } + +ovpn_encrypt_post (skb,0 ); +return true; +} + +/* send skb to connected peer, if any */ +static void ovpn_send (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +struct sk_buff *curr, *next; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +/* this might be a GSO-segmented skb list: process each skb +* independently +*/ + skb_list_walk_safe(skb, curr, next) +if (unlikely (!ovpn_encrypt_one (peer, curr))) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr); + } + +/* skb passed over, no need to free */ + skb =NULL ; +drop: +if (likely (peer)) +ovpn_peer_put (peer); +kfree_skb_list (skb); +}/* Send user data to the network */ netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) { +struct ovpn_struct *ovpn =netdev_priv (dev); +struct sk_buff *segments, *tmp, *curr, *next; +struct sk_buff_head skb_list; +__be16 proto; +int ret; + +/* reset netfilter state */ +nf_reset_ct (skb); + +/* verify IP header size in network packet */ + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto || skb->protocol != proto)) { +net_err_ratelimited ("%s: dropping malformed payload packet\n" , + dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +if (skb_is_gso (skb)) { + segments =skb_gso_segment (skb,0 ); +if (IS_ERR (segments)) { + ret =PTR_ERR (segments); +net_err_ratelimited ("%s: cannot segment packet: %d\n" , + dev->name, ret); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +consume_skb (skb); + skb = segments; + } + +/* from this moment on, "skb" might be a list */ + +__skb_queue_head_init (&skb_list); + skb_list_walk_safe(skb, curr, next) { +skb_mark_not_on_list (curr); + + tmp =skb_share_check (curr,GFP_ATOMIC ); +if (unlikely (!tmp)) { +kfree_skb_list (next); +net_err_ratelimited ("%s: skb_share_check failed\n" , + dev->name); + goto drop_list; + } + +__skb_queue_tail (&skb_list, tmp); + } + skb_list.prev->next =NULL ; + +ovpn_send (ovpn, skb_list.next,NULL ); + +return NETDEV_TX_OK ; + +drop_list: + skb_queue_walk_safe(&skb_list, curr, next) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr); + } +drop:skb_tx_error (skb); -kfree_skb (skb); +kfree_skb_list (skb);return NET_XMIT_DROP ; }
drivers/net/ovpn/peer.c +35 -2
@@ -9,7 +9,6 @@#include <linux/skbuff.h> #include <linux/list.h> -#include <linux/workqueue.h> #include "ovpnstruct.h" #include "bind.h"
@@ -65,8 +64,10 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)*/ static void ovpn_peer_release (struct ovpn_peer *peer) { -ovpn_bind_reset (peer,NULL ); +if (peer->sock) +ovpn_socket_put (peer->sock); +ovpn_bind_reset (peer,NULL );dst_cache_destroy (&peer->dst_cache); }
@@ -244,6 +245,38 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_id)return peer; } +/** +* ovpn_peer_get_by_dst - Lookup peer to send skb to +* @ovpn: the private data representing the current VPN session +* @skb: the skb to extract the destination address from +* +* This function takes a tunnel packet and looks up the peer to send it to +* after encapsulation. The skb is expected to be the in-tunnel packet, without +* any OpenVPN related header. +* +* Assume that the IP header is accessible in the skb data. +* +* Return: the peer if found or NULL otherwise. +*/ +struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn, +struct sk_buff *skb) +{ +struct ovpn_peer *peer =NULL ; + +/* in P2P mode, no matter the destination, packets are always sent to +* the single peer listening on the other side +*/ +if (ovpn->mode ==OVPN_MODE_P2P ) { +rcu_read_lock (); + peer =rcu_dereference (ovpn->peer); +if (unlikely (peer && !ovpn_peer_hold (peer))) + peer =NULL ; +rcu_read_unlock (); + } + +return peer; +} +/** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +5 -0
@@ -11,6 +11,7 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "socket.h" #include <net/dst_cache.h> #include <uapi/linux/ovpn.h>
@@ -22,6 +23,7 @@* @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @sock: the socket being used to talk to this peer * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called
@@ -38,6 +40,7 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct ovpn_socket *sock;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -78,5 +81,7 @@ void ovpn_peer_release_p2p(struct ovpn_struct *ovpn);struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb);struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id); +struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn, +struct sk_buff *skb);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/skb.h new file +54 -0
54 lines not on this page · raw message
drivers/net/ovpn/udp.c +230 -0
244 lines not on this page · raw message
drivers/net/ovpn/udp.h +8 -0
@@ -9,9 +9,17 @@#ifndef _NET_OVPN_UDP_H_#define _NET_OVPN_UDP_H_ +#include <linux/skbuff.h> +#include <net/sock.h> + +struct ovpn_peer ;struct ovpn_struct ; +struct sk_buff ;struct socket ;int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); +void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb); +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 10/25] ovpn: implement basic TX path (UDP)
Hi Antonio,
Thanks for the updated patchset. I'm going through it again.
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/io.c +13
-0+/* send skb to connected peer, if any */ +static void ovpn_send (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +struct sk_buff *curr, *next; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev);
drivers/net/ovpn/io.c line 85
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ goto drop; + } + +/* this might be a GSO-segmented skb list: process each skb +* independently +*/ + skb_list_walk_safe(skb, curr, next) +if (unlikely (!ovpn_encrypt_one (peer, curr))) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr);
Is this a bit inconsistent with ovpn_net_xmit's behavior? There we drop the full list if we fail one skb_share_check, and here we only drop the single packet that failed and handle the rest? Or am I misreading this?
Quoted 45 lines
drivers/net/ovpn/io.c +40
-0+ } + +/* skb passed over, no need to free */ + skb =NULL ; +drop: +if (likely (peer)) +ovpn_peer_put (peer); +kfree_skb_list (skb); +}/* Send user data to the network */ netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) { +struct ovpn_struct *ovpn =netdev_priv (dev); +struct sk_buff *segments, *tmp, *curr, *next; +struct sk_buff_head skb_list; +__be16 proto; +int ret; + +/* reset netfilter state */ +nf_reset_ct (skb); + +/* verify IP header size in network packet */ + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto || skb->protocol != proto)) { +net_err_ratelimited ("%s: dropping malformed payload packet\n" , + dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +if (skb_is_gso (skb)) { + segments =skb_gso_segment (skb,0 ); +if (IS_ERR (segments)) { + ret =PTR_ERR (segments); +net_err_ratelimited ("%s: cannot segment packet: %d\n" , + dev->name, ret); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +consume_skb (skb); + skb = segments; + }
drivers/net/ovpn/io.c line 140
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+ +/* 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);
Those don't get counted as dropped, but the ones we've already handled (and put on skb_list) will be counted as dev_core_stats_tx_dropped_inc? (it probably doesn't matter that much, since if we'd dropped before/at skb_gso_segment we'd only count one drop)
Quoted 24 lines
drivers/net/ovpn/io.c +20
-1+net_err_ratelimited ("%s: skb_share_check failed\n" , + dev->name); + goto drop_list; + } + +__skb_queue_tail (&skb_list, tmp); + } + skb_list.prev->next =NULL ; + +ovpn_send (ovpn, skb_list.next,NULL ); + +return NETDEV_TX_OK ; + +drop_list: + skb_queue_walk_safe(&skb_list, curr, next) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr); + } +drop:skb_tx_error (skb); -kfree_skb (skb); +kfree_skb_list (skb);return NET_XMIT_DROP ; }
[...]
Quoted 29 lines
drivers/net/ovpn/udp.c +29
-0+void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb) +{ +struct ovpn_bind *bind; +struct socket *sock; +int ret =-1 ; + + skb->dev = ovpn->dev; +/* no checksum performed at this layer */ + skb->ip_summed =CHECKSUM_NONE ; + +/* get socket info */ + sock = peer->sock->sock; +if (unlikely (!sock)) { +net_warn_ratelimited ("%s: no sock for remote peer\n" , __func__); + goto out; + } + +rcu_read_lock (); +/* get binding */ + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) { +net_warn_ratelimited ("%s: no bind for remote peer\n" , __func__); + goto out_unlock; + } + +/* crypto layer -> transport (UDP) */ + ret =ovpn_udp_output (ovpn, bind, &peer->dst_cache, sock->sk, skb); +
drivers/net/ovpn/udp.c line 245
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+out_unlock: +rcu_read_unlock (); +out: +if (unlikely (ret <0 )) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (skb); +return ; + } + +dev_sw_netstats_tx_add (ovpn->dev,1 , skb->len);
I don't think it's safe to access skb->len after calling udp_tunnel(6)_xmit_skb.
For example, vxlan_xmit_one (drivers/net/vxlan/vxlan_core.c) has a similar counter and saves skb->len into pkt_len.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 10/25] ovpn: implement basic TX path (UDP)
Quoted 25 lines · Sabrina Dubroca
Hi Antonio,
Thanks for the updated patchset. I'm going through it again.
Quoted 20 lines · Antonio Quartulli
drivers/net/ovpn/io.c +20
-0+/* send skb to connected peer, if any */ +static void ovpn_send (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +struct sk_buff *curr, *next; + +if (likely (!peer)) +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: no peer to send data to\n" , + ovpn->dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +/* this might be a GSO-segmented skb list: process each skb +* independently +*/ +skb_list_walk_safe (skb, curr, next)
Quoted 8 lines
drivers/net/ovpn/io.c line 85
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+if (unlikely (!ovpn_encrypt_one (peer, curr))) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr);Is this a bit inconsistent with ovpn_net_xmit's behavior? There we drop the full list if we fail one
skb_share_check, and here we only drop the single packet that failed and handle the rest? Or am I misreading this?
You're right, it's inconsistent.
In ovpn_send() each call to ovpn_encrypt_one() will result in the skb being sent, therefore, if we wanted, we could free only skbs we haven't sent yet.
Maybe it makes sense to always try sending the rest and assume that the upper layer will deal with the missing data.
I'll adjust ovpn_net_xmit() to drop only the failing skb and move on.
Quoted 53 lines
Quoted 53 lines
drivers/net/ovpn/io.c +47
-0+ } + +/* skb passed over, no need to free */ + skb =NULL ; +drop: +if (likely (peer)) +ovpn_peer_put (peer); +kfree_skb_list (skb); +}/* Send user data to the network */ netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev) { +struct ovpn_struct *ovpn =netdev_priv (dev); +struct sk_buff *segments, *tmp, *curr, *next; +struct sk_buff_head skb_list; +__be16 proto; +int ret; + +/* reset netfilter state */ +nf_reset_ct (skb); + +/* verify IP header size in network packet */ + proto =ovpn_ip_check_protocol (skb); +if (unlikely (!proto || skb->protocol != proto)) { +net_err_ratelimited ("%s: dropping malformed payload packet\n" , + dev->name); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +if (skb_is_gso (skb)) { + segments =skb_gso_segment (skb,0 ); +if (IS_ERR (segments)) { + ret =PTR_ERR (segments); +net_err_ratelimited ("%s: cannot segment packet: %d\n" , + dev->name, ret); +dev_core_stats_tx_dropped_inc (ovpn->dev); + goto drop; + } + +consume_skb (skb); + skb = segments; + } + +/* from this moment on, "skb" might be a list */ + +__skb_queue_head_init (&skb_list); + skb_list_walk_safe(skb, curr, next) { +skb_mark_not_on_list (curr); +
Quoted 8 lines
drivers/net/ovpn/io.c line 140
Quoted 3 lines
drivers/net/ovpn/io.c +3
-0+ tmp =skb_share_check (curr,GFP_ATOMIC ); +if (unlikely (!tmp)) { +kfree_skb_list (next);Those don't get counted as dropped, but the ones we've already handled (and put on
skb_list) will be counted asdev_core_stats_tx_dropped_inc? (it probably doesn't matter that much, since if we'd dropped before/atskb_gso_segmentwe'd only count one drop)
Once I change this part, I'll ensure we count each single dropped packet.
The downside is that any failure before skb_gso_segment() is counted as 1 drop, while anything later than that is counter per-segment. I don't think we can do anything about it though.
Quoted 62 lines
Quoted 25 lines
drivers/net/ovpn/io.c +20
-1+net_err_ratelimited ("%s: skb_share_check failed\n" , + dev->name); + goto drop_list; + } + +__skb_queue_tail (&skb_list, tmp); + } + skb_list.prev->next =NULL ; + +ovpn_send (ovpn, skb_list.next,NULL ); + +return NETDEV_TX_OK ; + +drop_list: + skb_queue_walk_safe(&skb_list, curr, next) { +dev_core_stats_tx_dropped_inc (ovpn->dev); +kfree_skb (curr); + } +drop:skb_tx_error (skb); -kfree_skb (skb); +kfree_skb_list (skb);return NET_XMIT_DROP ; }[...]
Quoted 34 lines
drivers/net/ovpn/udp.c +34
-0+void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer, +struct sk_buff *skb) +{ +struct ovpn_bind *bind; +struct socket *sock; +int ret =-1 ; + + skb->dev = ovpn->dev; +/* no checksum performed at this layer */ + skb->ip_summed =CHECKSUM_NONE ; + +/* get socket info */ + sock = peer->sock->sock; +if (unlikely (!sock)) { +net_warn_ratelimited ("%s: no sock for remote peer\n" , __func__); + goto out; + } + +rcu_read_lock (); +/* get binding */ + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) { +net_warn_ratelimited ("%s: no bind for remote peer\n" , __func__); + goto out_unlock; + } + +/* crypto layer -> transport (UDP) */ + ret =ovpn_udp_output (ovpn, bind, &peer->dst_cache, sock->sk, skb); + +out_unlock: +rcu_read_unlock (); +out: +if (unlikely (ret <0 )) { +dev_core_stats_tx_dropped_inc (ovpn->dev);
Quoted 8 lines
drivers/net/ovpn/udp.c line 245
Quoted 5 lines
drivers/net/ovpn/udp.c +5
-0+kfree_skb (skb); +return ; + } + +dev_sw_netstats_tx_add (ovpn->dev,1 , skb->len);I don't think it's safe to access
skb->lenafter calling udp_tunnel(6)_xmit_skb.
Absolutely right! Thanks!
Quoted 3 lines
For example,
vxlan_xmit_one(drivers/net/vxlan/vxlan_core.c) has a similar counter and savesskb->lenintopkt_len.
Yap, will do the same.
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 11/25 ovpn: implement basic RX path (UDP)
In reply to Antonio QuartulliPackets received over the socket are forwarded to the user device.
Implementation is UDP only. TCP will be added by a later patch.
Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing.
--- drivers/net/ovpn/io.c | 63 +++++++++++++++++++- drivers/net/ovpn/io.h | 2 + drivers/net/ovpn/main.c | 9 ++- drivers/net/ovpn/ovpnstruct.h | 3 + drivers/net/ovpn/proto.h | 75 ++++++++++++++++++++++++ drivers/net/ovpn/skb.h | 3 - drivers/net/ovpn/socket.c | 24 ++++++++ drivers/net/ovpn/udp.c | 104 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/udp.h | 3 +- 9 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 drivers/net/ovpn/proto.h
drivers/net/ovpn/io.c +62 -1
@@ -9,14 +9,75 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gro_cells.h> #include <net/gso.h> -#include "io.h" #include "ovpnstruct.h" #include "peer.h" +#include "io.h" +#include "netlink.h" +#include "proto.h" #include "udp.h" #include "skb.h" +/* Called after decrypt to write the IP packet to the device. +* This method is expected to manage/free the skb. +*/ +static void ovpn_netdev_write (struct ovpn_peer *peer,struct sk_buff *skb) +{ +/* we can't guarantee the packet wasn't corrupted before entering the +* VPN, therefore we give other layers a chance to check that +*/ + skb->ip_summed =CHECKSUM_NONE ; + +/* skb hash for transport packet no longer valid after decapsulation */ +skb_clear_hash (skb); + +/* post-decrypt scrub -- prepare to inject encapsulated packet onto the +* interface, based on __skb_tunnel_rx() in dst.h +*/ + skb->dev = peer->ovpn->dev; +skb_set_queue_mapping (skb,0 ); +skb_scrub_packet (skb, true); + +skb_reset_network_header (skb); +skb_reset_transport_header (skb); +skb_probe_transport_header (skb); +skb_reset_inner_headers (skb); + +memset (skb->cb,0 ,sizeof (skb->cb)); + +/* cause packet to be "received" by the interface */ +if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); +} + +static void ovpn_decrypt_post (struct sk_buff *skb,int ret) +{ +struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; + +if (unlikely (ret <0 )) + goto drop; + +ovpn_netdev_write (peer, skb); +/* skb is passed to upper layer - don't free it */ + skb =NULL ; +drop: +if (unlikely (skb)) +dev_core_stats_rx_dropped_inc (peer->ovpn->dev); +kfree_skb (skb); +ovpn_peer_put (peer); +} + +/* pick next packet from RX queue, decrypt and forward it to the device */ +void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb) +{ +ovpn_skb_cb (skb)->peer = peer; +ovpn_decrypt_post (skb,0 ); +} +static void ovpn_encrypt_post (struct sk_buff *skb,int ret) {struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer;
drivers/net/ovpn/io.h +2 -0
@@ -12,4 +12,6 @@netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); +void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb); +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +8 -1
@@ -13,6 +13,7 @@#include <linux/inetdevice.h> //#include <linux/rcupdate.h> #include <linux/version.h> +#include <net/gro_cells.h> #include <net/ip.h> #include <net/rtnetlink.h> #include <uapi/linux/if_arp.h>
@@ -49,11 +50,17 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode)static void ovpn_struct_free (struct net_device *net) { +struct ovpn_struct *ovpn =netdev_priv (net); + +gro_cells_destroy (&ovpn->gro_cells); +rcu_barrier (); }static int ovpn_net_init (struct net_device *dev) { -return 0 ; +struct ovpn_struct *ovpn =netdev_priv (dev); + +return gro_cells_init (&ovpn->gro_cells, dev); }static int ovpn_net_open (struct net_device *dev)
drivers/net/ovpn/ovpnstruct.h +3 -0
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <net/gro_cells.h> #include <net/net_trackers.h> #include <uapi/linux/ovpn.h>
@@ -22,6 +23,7 @@* @lock: protect this object * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list +* @gro_cells: pointer to the Generic Receive Offload cell */ struct ovpn_struct {struct net_device *dev;
@@ -31,6 +33,7 @@ struct ovpn_struct {spinlock_t lock;/* protect writing to the ovpn_struct object */ struct ovpn_peer __rcu *peer;struct list_head dev_list; + struct gro_cells gro_cells; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/proto.h new file +75 -0
75 lines not on this page · raw message
drivers/net/ovpn/skb.h +0 -3
@@ -18,10 +18,7 @@#include <linux/types.h> struct ovpn_cb { -struct aead_request *req;struct ovpn_peer *peer; -struct ovpn_crypto_key_slot *ks; -unsigned int payload_offset; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
drivers/net/ovpn/socket.c +24 -0
@@ -22,6 +22,9 @@ static void ovpn_socket_detach(struct socket *sock)if (!sock)return ; +if (sock->sk->sk_protocol ==IPPROTO_UDP ) +ovpn_udp_socket_detach (sock); +sockfd_put (sock); }
@@ -71,6 +74,27 @@ static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)return ret; } +/* Retrieve the corresponding ovpn object from a UDP socket +* rcu_read_lock must be held on entry +*/ +struct ovpn_struct *ovpn_from_udp_sock (struct sock *sk) +{ +struct ovpn_socket *ovpn_sock; + +if (unlikely (READ_ONCE (udp_sk (sk)->encap_type) !=UDP_ENCAP_OVPNINUDP )) +return NULL ; + + ovpn_sock =rcu_dereference_sk_user_data (sk); +if (unlikely (!ovpn_sock)) +return NULL ; + +/* make sure that sk matches our stored transport socket */ +if (unlikely (!ovpn_sock->sock || sk != ovpn_sock->sock->sk)) +return NULL ; + +return ovpn_sock->ovpn; +} +/** * ovpn_socket_new - create a new socket and initialize it * @sock: the kernel socket to embed
drivers/net/ovpn/udp.c +103 -1
129 lines not on this page · raw message
drivers/net/ovpn/udp.h +2 -1
@@ -18,8 +18,9 @@ struct sk_buff;struct socket ;int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn); - +void ovpn_udp_socket_detach (struct socket *sock);void ovpn_udp_send_skb (struct ovpn_struct *ovpn,struct ovpn_peer *peer,struct sk_buff *skb); +struct ovpn_struct *ovpn_from_udp_sock (struct sock *sk);#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 11/25] ovpn: implement basic RX path (UDP)
Quoted 19 lines · Antonio Quartulli
drivers/net/ovpn/io.c +19
-0+static void ovpn_netdev_write (struct ovpn_peer *peer,struct sk_buff *skb) +{ +/* we can't guarantee the packet wasn't corrupted before entering the +* VPN, therefore we give other layers a chance to check that +*/ + skb->ip_summed =CHECKSUM_NONE ; + +/* skb hash for transport packet no longer valid after decapsulation */ +skb_clear_hash (skb); + +/* post-decrypt scrub -- prepare to inject encapsulated packet onto the +* interface, based on __skb_tunnel_rx() in dst.h +*/ + skb->dev = peer->ovpn->dev; +skb_set_queue_mapping (skb,0 ); +skb_scrub_packet (skb, true); + +skb_reset_network_header (skb); +skb_reset_transport_header (skb);
drivers/net/ovpn/io.c line 54
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+skb_probe_transport_header (skb); +skb_reset_inner_headers (skb); + +memset (skb->cb,0 ,sizeof (skb->cb)); + +/* cause packet to be "received" by the interface */ +if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len);
I don't think accessing skb->len after passing the skb to gro_cells_receive is safe, seec7cc9200e9b4 ("macsec: avoid use-after-free in macsec_handle_frame()")
[...]
drivers/net/ovpn/main.c static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) · line 56
Quoted 6 lines
static void
ovpn_struct_free(struct net_device *net){drivers/net/ovpn/main.c +4
-0+struct ovpn_struct *ovpn =netdev_priv (net); + +gro_cells_destroy (&ovpn->gro_cells); +rcu_barrier ();
What's the purpose of this rcu_barrier? I expect it in module_exit, not when removing one netdevice.
drivers/net/ovpn/skb.h line 24
Quoted 12 lines
drivers/net/ovpn/skb.h +0
-3@@ -18,10 +18,7 @@#include <linux/types.h> struct ovpn_cb { -struct aead_request *req;struct ovpn_peer *peer; -struct ovpn_crypto_key_slot *ks; -unsigned int payload_offset;
Squashed into the wrong patch?
[...]
drivers/net/ovpn/skb.h
Quoted 8 lines
drivers/net/ovpn/skb.h +8
-0+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);
[1]
Quoted 24 lines
drivers/net/ovpn/skb.h +22
-0+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; +} +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; + + ovpn =ovpn_from_udp_sock (sk); +if (unlikely (!ovpn)) { +net_err_ratelimited ("%s: cannot obtain ovpn object from UDP socket\n" , + __func__); + goto drop; + }
[...]
drivers/net/ovpn/skb.h
Quoted 9 lines
drivers/net/ovpn/skb.h +9
-0+/* pop off outer UDP header */ +__skb_pull (skb,sizeof (struct udphdr )); +ovpn_recv (peer, skb); +return 0 ; + +drop: +if (peer) +ovpn_peer_put (peer); +dev_core_stats_rx_dropped_inc (ovpn->dev);
If we get here from the first goto, ovpn is NULL. You could add a drop_noovpn label right here to just do the free+return.
Quoted 23 lines
drivers/net/ovpn/skb.h +10
-1+kfree_skb (skb); +return 0 ; +} +/** * ovpn_udp4_output - send IPv4 packet over udp socket * @ovpn: the openvpn instance @@ -257,8 +342,13 @@ void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer,*/ int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn) { +struct udp_tunnel_sock_cfg cfg = { + .sk_user_data = ovpn, + .encap_type =UDP_ENCAP_OVPNINUDP , + .encap_rcv = ovpn_udp_encap_recv, + };struct ovpn_socket *old_data; -int ret =0 ; +int ret;/* sanity check */ if (sock->sk->sk_protocol !=IPPROTO_UDP ) {
drivers/net/ovpn/skb.h int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 365
Quoted 4 lines
drivers/net/ovpn/skb.h +1
-0@@ -272,6 +362,7 @@ int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn)if (!old_data) {/* socket is currently unused - we can take it */ rcu_read_unlock (); +setup_udp_tunnel_sock (sock_net (sock->sk), sock, &cfg);
This will set sk_user_data to the ovpn_struct, but ovpn_from_udp_sock expects the ovpn_socket [1], which is stored into sk_user_data a little bit later by ovpn_socket_new. If a packet reaches ovpn_udp_encap_recv -> ovpn_from_udp_sock before ovpn_socket_new overwrites sk_user_data, bad things probably happen.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 11/25] ovpn: implement basic RX path (UDP)
Quoted 26 lines · Sabrina Dubroca
Quoted 25 lines · Antonio Quartulli
drivers/net/ovpn/io.c +25
-0+static void ovpn_netdev_write (struct ovpn_peer *peer,struct sk_buff *skb) +{ +/* we can't guarantee the packet wasn't corrupted before entering the +* VPN, therefore we give other layers a chance to check that +*/ + skb->ip_summed =CHECKSUM_NONE ; + +/* skb hash for transport packet no longer valid after decapsulation */ +skb_clear_hash (skb); + +/* post-decrypt scrub -- prepare to inject encapsulated packet onto the +* interface, based on __skb_tunnel_rx() in dst.h +*/ + skb->dev = peer->ovpn->dev; +skb_set_queue_mapping (skb,0 ); +skb_scrub_packet (skb, true); + +skb_reset_network_header (skb); +skb_reset_transport_header (skb); +skb_probe_transport_header (skb); +skb_reset_inner_headers (skb); + +memset (skb->cb,0 ,sizeof (skb->cb)); + +/* cause packet to be "received" by the interface */
Quoted 8 lines
drivers/net/ovpn/io.c line 54
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+if (likely (gro_cells_receive (&peer->ovpn->gro_cells, + skb) ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len);I don't think accessing
skb->lenafter passing the skb togro_cells_receiveis safe, seec7cc9200e9b4("macsec: avoid use-after-free inmacsec_handle_frame()")
Thanks for spotting this! It's basically the same issue (but symmetric) as patch 10/25.
Quoted 12 lines
[...]
drivers/net/ovpn/main.c static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) · line 56
Quoted 6 lines
static void ovpn_struct_free(struct net_device *net) {drivers/net/ovpn/main.c +4
-0+struct ovpn_struct *ovpn =netdev_priv (net); + +gro_cells_destroy (&ovpn->gro_cells); +rcu_barrier ();What's the purpose of this
rcu_barrier? I expect it inmodule_exit, not when removing one netdevice.
Good question.
I presume it's a leftover from previous tests.
I think it's harmless, but it should not be needed at all.
I will remove it.
Quoted 9 lines
Quoted 9 lines
drivers/net/ovpn/skb.h +0
-0@@ -18,10 +18,7 @@#include <linux/types.h>
Quoted 7 lines
drivers/net/ovpn/skb.h line 24
Quoted 5 lines
drivers/net/ovpn/skb.h +0
-3struct ovpn_cb { -struct aead_request *req;struct ovpn_peer *peer; -struct ovpn_crypto_key_slot *ks; -unsigned int payload_offset;Squashed into the wrong patch?
Darn yes. Sorry for this.
Quoted 39 lines
[...]
drivers/net/ovpn/skb.h
Quoted 8 lines
drivers/net/ovpn/skb.h +8
-0+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);[1]
Quoted 24 lines
drivers/net/ovpn/skb.h +22
-0+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; +} +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; + + 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 12 lines
drivers/net/ovpn/skb.h
Quoted 9 lines
drivers/net/ovpn/skb.h +9
-0+/* pop off outer UDP header */ +__skb_pull (skb,sizeof (struct udphdr )); +ovpn_recv (peer, skb); +return 0 ; + +drop: +if (peer) +ovpn_peer_put (peer); +dev_core_stats_rx_dropped_inc (ovpn->dev);If we get here from the first goto, ovpn is NULL. You could add a
drop_noovpnlabel right here to just do the free+return.
Right.
Weird though that no static analysis tool complained about ovpn possibly being NULL.
Will add the extra label.
Quoted 21 lines
Quoted 21 lines
drivers/net/ovpn/skb.h +10
-1+kfree_skb (skb); +return 0 ; +} +/** * ovpn_udp4_output - send IPv4 packet over udp socket * @ovpn: the openvpn instance @@ -257,8 +342,13 @@ void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer,*/ int ovpn_udp_socket_attach (struct socket *sock,struct ovpn_struct *ovpn) { +struct udp_tunnel_sock_cfg cfg = { + .sk_user_data = ovpn, + .encap_type =UDP_ENCAP_OVPNINUDP , + .encap_rcv = ovpn_udp_encap_recv, + };struct ovpn_socket *old_data; -int ret =0 ; +int ret;
Quoted 13 lines
drivers/net/ovpn/skb.h int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) · line 365
Quoted 7 lines
drivers/net/ovpn/skb.h +1
-0/* sanity check */ if (sock->sk->sk_protocol !=IPPROTO_UDP ) {@@ -272,6 +362,7 @@ int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn)if (!old_data) {/* socket is currently unused - we can take it */ rcu_read_unlock (); +setup_udp_tunnel_sock (sock_net (sock->sk), sock, &cfg);This will set
sk_user_datato theovpn_struct, butovpn_from_udp_sockexpects theovpn_socket[1], which is stored intosk_user_dataa little bit later byovpn_socket_new. If a packet reachesovpn_udp_encap_recv->ovpn_from_udp_sockbeforeovpn_socket_newoverwritessk_user_data, bad things probably happen.
Wow - this is a very nice catch.
I think this wrong cfg.sk_user_data initialization was there this since the first prototype, but it just passed unnoticed.
I will drop the field initialization, so that the sk_user_data stays NULL until it gets assigned the ovpn_sock.
Thanks a lot!
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Simon Horman
reviewerRe: [PATCH net-next v6 11/25] ovpn: implement basic RX path (UDP)
In reply to Antonio QuartulliQuoted 8 lines · Antonio Quartulli
Packets received over the socket are forwarded to the user device.
Implementation is UDP only. TCP will be added by a later patch.
Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing.
Signed-off-by Antonio Quartulli <[email protected]>
...
Quoted 15 lines
drivers/net/ovpn/udp.c +15
-0+/** +* 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) +{
drivers/net/ovpn/udp.c line 52
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+struct ovpn_peer *peer =NULL ; +struct ovpn_struct *ovpn; +u32 peer_id; +u8 opcode; + + ovpn =ovpn_from_udp_sock (sk); +if (unlikely (!ovpn)) { +net_err_ratelimited ("%s: cannot obtain ovpn object from UDP socket\n" , + __func__); + goto drop;
Hi Antonio,
Here ovpn is NULL. But jumping to drop will result in ovpn being dereferenced.
Flagged by Smatch.
Quoted 59 lines
drivers/net/ovpn/udp.c +59
-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 ) + +OVPN_OP_SIZE_V2 ))) { +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) { +/* data packet with undef peer-id */ + peer =ovpn_peer_get_by_transp_addr (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: received data with undef peer-id from unknown source\n" , + __func__); + goto drop; + } + } + +/* pop off outer UDP header */ +__skb_pull (skb,sizeof (struct udphdr )); +ovpn_recv (peer, skb); +return 0 ; + +drop: +if (peer) +ovpn_peer_put (peer); +dev_core_stats_rx_dropped_inc (ovpn->dev); +kfree_skb (skb); +return 0 ; +} +
...
Antonio Quartulli
maintainerRe: [PATCH net-next v6 11/25] ovpn: implement basic RX path (UDP)
Quoted 40 lines · Simon Horman
Quoted 8 lines · Antonio Quartulli
Packets received over the socket are forwarded to the user device.
Implementation is UDP only. TCP will be added by a later patch.
Note: no decryption/decapsulation exists yet, packets are forwarded as they arrive without much processing.
Signed-off-by Antonio Quartulli <[email protected]>...
Quoted 15 lines
drivers/net/ovpn/udp.c +15
-0+/** +* 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) +{drivers/net/ovpn/udp.c line 52
Quoted 10 lines
drivers/net/ovpn/udp.c +10
-0+struct ovpn_peer *peer =NULL ; +struct ovpn_struct *ovpn; +u32 peer_id; +u8 opcode; + + ovpn =ovpn_from_udp_sock (sk); +if (unlikely (!ovpn)) { +net_err_ratelimited ("%s: cannot obtain ovpn object from UDP socket\n" , + __func__); + goto drop;Hi Antonio,
Quoted 3 lines
Here ovpn is NULL. But jumping to drop will result in ovpn being dereferenced.
Flagged by Smatch.
You are correct, thanks for the report.
Will fix it in the next version.
Cheers,
Quoted 63 lines
Quoted 61 lines
drivers/net/ovpn/udp.c +59
-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 ) + +OVPN_OP_SIZE_V2 ))) { +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) { +/* data packet with undef peer-id */ + peer =ovpn_peer_get_by_transp_addr (ovpn, skb); +if (unlikely (!peer)) { +net_dbg_ratelimited ("%s: received data with undef peer-id from unknown source\n" , + __func__); + goto drop; + } + } + +/* pop off outer UDP header */ +__skb_pull (skb,sizeof (struct udphdr )); +ovpn_recv (peer, skb); +return 0 ; + +drop: +if (peer) +ovpn_peer_put (peer); +dev_core_stats_rx_dropped_inc (ovpn->dev); +kfree_skb (skb); +return 0 ; +} +...
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 12/25 ovpn: implement packet processing
In reply to Antonio QuartulliThis change implements encryption/decryption and
encapsulation/decapsulation of OpenVPN packets.
Support for generic crypto state is added along with a wrapper for the AEAD crypto kernel API.
--- drivers/net/ovpn/Makefile | 3 + drivers/net/ovpn/crypto.c | 149 +++++++++++++ drivers/net/ovpn/crypto.h | 136 ++++++++++++ drivers/net/ovpn/crypto_aead.c | 374 +++++++++++++++++++++++++++++++++ drivers/net/ovpn/crypto_aead.h | 31 +++ drivers/net/ovpn/io.c | 124 ++++++++++- drivers/net/ovpn/io.h | 3 + drivers/net/ovpn/packet.h | 2 +- drivers/net/ovpn/peer.c | 29 +++ drivers/net/ovpn/peer.h | 6 + drivers/net/ovpn/pktid.c | 130 ++++++++++++ drivers/net/ovpn/pktid.h | 87 ++++++++ drivers/net/ovpn/proto.h | 31 +++ drivers/net/ovpn/skb.h | 11 +- 14 files changed, 1103 insertions(+), 13 deletions(-) create mode 100644 drivers/net/ovpn/crypto.c create mode 100644 drivers/net/ovpn/crypto.h create mode 100644 drivers/net/ovpn/crypto_aead.c create mode 100644 drivers/net/ovpn/crypto_aead.h create mode 100644 drivers/net/ovpn/pktid.c create mode 100644 drivers/net/ovpn/pktid.h
drivers/net/ovpn/Makefile +3 -0
@@ -8,10 +8,13 @@ obj-$(CONFIG_OVPN) := ovpn.o ovpn-y += bind.o +ovpn-y += crypto.o +ovpn-y += crypto_aead.o ovpn-y += main.o ovpn-y += io.o ovpn-y += netlink.o ovpn-y += netlink-gen.o ovpn-y += peer.o +ovpn-y += pktid.o ovpn-y += socket.o ovpn-y += udp.o
drivers/net/ovpn/crypto.c new file +149 -0
149 lines not on this page · raw message
drivers/net/ovpn/crypto.h new file +136 -0
136 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.c new file +374 -0
374 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.h new file +31 -0
@@ -0,0 +1,31 @@ +/* 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_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb); +int ovpn_aead_decrypt (struct ovpn_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb); + +struct ovpn_crypto_key_slot * +ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc); +void ovpn_aead_crypto_key_slot_destroy (struct ovpn_crypto_key_slot *ks); + +#endif /* _NET_OVPN_OVPNAEAD_H_ */
drivers/net/ovpn/io.c +113 -11
179 lines not on this page · raw message
drivers/net/ovpn/io.h +3 -0
@@ -14,4 +14,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev);void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb); +void ovpn_encrypt_post (struct sk_buff *skb,int ret); +void ovpn_decrypt_post (struct sk_buff *skb,int ret); +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/packet.h +1 -1
@@ -10,7 +10,7 @@#ifndef _NET_OVPN_PACKET_H_#define _NET_OVPN_PACKET_H_ -/* When the OpenVPN protocol is ran in AEAD mode, use +/* When the OpenVPN protocol is run in AEAD mode, use * the OpenVPN packet ID as the AEAD nonce: * * 00000005 521c3b01 4308c041
drivers/net/ovpn/peer.c +29 -0
@@ -12,6 +12,8 @@#include "ovpnstruct.h" #include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "io.h" #include "main.h" #include "netlink.h"
@@ -42,6 +44,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) peer->vpn_addrs.ipv6 = in6addr_any;RCU_INIT_POINTER (peer->bind,NULL ); +ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock);kref_init (&peer->refcount);
@@ -67,6 +70,7 @@ static void ovpn_peer_release(struct ovpn_peer *peer)if (peer->sock)ovpn_socket_put (peer->sock); +ovpn_crypto_state_release (&peer->crypto);ovpn_bind_reset (peer,NULL );dst_cache_destroy (&peer->dst_cache); }
@@ -277,6 +281,31 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,return peer; } +/** +* ovpn_peer_check_by_src - check that skb source is routed via peer +* @ovpn: the openvpn instance to search +* @skb: the packet to extract source address from +* @peer: the peer to check against the source address +* +* Return: true if the peer is matching or false otherwise +*/ +bool ovpn_peer_check_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +bool match = false; + +if (ovpn->mode ==OVPN_MODE_P2P ) { +/* in P2P mode, no matter the destination, packets are always +* sent to the single peer listening on the other side +*/ +rcu_read_lock (); + match = (peer ==rcu_dereference (ovpn->peer)); +rcu_read_unlock (); + } + +return match; +} +/** * ovpn_peer_add_p2p - add peer to related tables in a P2P instance * @ovpn: the instance to add the peer to
drivers/net/ovpn/peer.h +6 -0
@@ -11,6 +11,8 @@#define _NET_OVPN_OVPNPEER_H_#include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "socket.h" #include <net/dst_cache.h>
@@ -24,6 +26,7 @@* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer +* @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called
@@ -41,6 +44,7 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket *sock; +struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt;
@@ -83,5 +87,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *ovpn,struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id);struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_struct *ovpn,struct sk_buff *skb); +bool ovpn_peer_check_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/pktid.c new file +130 -0
130 lines not on this page · raw message
drivers/net/ovpn/pktid.h new file +87 -0
87 lines not on this page · raw message
drivers/net/ovpn/proto.h +31 -0
@@ -72,4 +72,35 @@ static inline u32 ovpn_peer_id_from_skb(const struct sk_buff *skb, u16 offset) return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK; } +/** +* ovpn_key_id_from_skb - extract key ID from the skb head +* @skb: the packet to extract the key ID code from +* +* Note: this function assumes that the skb head was pulled enough +* to access the first byte. +* +* Return: the key ID +*/ +static inline u8 ovpn_key_id_from_skb (const struct sk_buff *skb) +{ +return *skb->data &OVPN_KEY_ID_MASK ; +} + +/** +* ovpn_opcode_compose - combine OP code, key ID and peer ID to wire format +* @opcode: the OP code +* @key_id: the key ID +* @peer_id: the peer ID +* +* Return: a 4 bytes integer obtained combining all input values following the +* OpenVPN wire format. This integer can then be written to the packet header. +*/ +static inline u32 ovpn_opcode_compose (u8 opcode,u8 key_id,u32 peer_id) +{ +const u8 op = (opcode <<OVPN_OPCODE_SHIFT ) | + (key_id &OVPN_KEY_ID_MASK ); + +return (op <<24 ) | (peer_id &OVPN_PEER_ID_MASK ); +} +#endif /* _NET_OVPN_OVPNPROTO_H_ */
drivers/net/ovpn/skb.h +10 -1
@@ -17,8 +17,17 @@#include <linux/socket.h> #include <linux/types.h> -struct ovpn_cb { +struct ovpn_cb_ctx {struct ovpn_peer *peer; +struct sk_buff *skb; +struct ovpn_crypto_key_slot *ks; +struct aead_request *req; +struct scatterlist sg[MAX_SKB_FRAGS +2 ]; +unsigned int payload_offset; +}; + +struct ovpn_cb { +struct ovpn_cb_ctx *ctx; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 1 line · Antonio Quartulli
+/* this swap is not atomic, but there will be a very short time frame where the
Since we're under a mutex, I think we might get put to sleep for a not-so-short time frame.
Quoted 1 line
+ *
old_secondarykey won't be available. This should not be a big deal as most
I could be misreading the code, but isn't it old_primary that's unavailable during the swap? rcu_replace_pointer overwrites cs->primary, so before the final assign, both slots contain old_secondary?
Quoted 20 lines
+ * likely both peers are already using the new primary at this point.
+ */drivers/net/ovpn/crypto.c new file +18
-0+void ovpn_crypto_key_slots_swap (struct ovpn_crypto_state *cs) +{ +const struct ovpn_crypto_key_slot *old_primary, *old_secondary; + +mutex_lock (&cs->mutex); + + old_secondary =rcu_dereference_protected (cs->secondary, +lockdep_is_held (&cs->mutex)); + old_primary =rcu_replace_pointer (cs->primary, old_secondary, +lockdep_is_held (&cs->mutex)); +rcu_assign_pointer (cs->secondary, old_primary); + +pr_debug ("key swapped: %u <-> %u\n" , + old_primary ? old_primary->key_id :0 , + old_secondary ? old_secondary->key_id :0 ); + +mutex_unlock (&cs->mutex); +}
[...]
drivers/net/ovpn/crypto_aead.c line 49
Quoted 6 lines
drivers/net/ovpn/crypto_aead.c new file +6
-0+int ovpn_aead_encrypt (struct ovpn_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->encrypt); +const unsigned int head_size =ovpn_aead_encap_overhead (ks); +DECLARE_CRYPTO_WAIT (wait);
nit: unused
Quoted 22 lines
drivers/net/ovpn/crypto_aead.c new file +22
-0+struct aead_request *req; +struct sk_buff *trailer; +struct scatterlist *sg; +u8 iv[NONCE_SIZE ]; +int nfrags, ret; +u32 pktid, op; + +/* Sample AEAD header format: +* 48000001 00000005 7e7046bd 444a7e28 cc6387b1 64a4d6c1 380275a... +* [ OP32 ] [seq # ] [ auth tag ] [ payload ... ] +* [4-byte +* IV head] +*/ + +/* check that there's enough headroom in the skb for packet +* encapsulation, after adding network header and encryption overhead +*/ +if (unlikely (skb_cow_head (skb,OVPN_HEAD_ROOM + head_size))) +return -ENOBUFS ; + +/* get number of skb frags and ensure that packet data is writable */ + nfrags =skb_cow_data (skb,0 , &trailer);
drivers/net/ovpn/crypto_aead.c line 81
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+if (unlikely (nfrags <0 )) +return nfrags; + +if (unlikely (nfrags +2 > (MAX_SKB_FRAGS +2 ))) +return -ENOSPC ; + +ovpn_skb_cb (skb)->ctx =kmalloc (sizeof (*ovpn_skb_cb (skb)->ctx), +GFP_ATOMIC ); +if (unlikely (!ovpn_skb_cb (skb)->ctx)) +return -ENOMEM ;
I think you should clear skb->cb (or at least ->ctx) at the start of ovpn_aead_encrypt. I don't think it will be cleaned up by the previous user, and if we fail before this alloc, we will possibly have bogus values in ->ctx when we get to kfree(ovpn_skb_cb(skb)->ctx) at the end of ovpn_encrypt_post.
(Similar comments around cb/ctx freeing and initialization apply to ovpn_aead_decrypt and ovpn_decrypt_post)
drivers/net/ovpn/crypto_aead.c line 95
Quoted 13 lines
drivers/net/ovpn/crypto_aead.c new file +13
-0+ sg =ovpn_skb_cb (skb)->ctx->sg; + +/* sg table: +* 0: op, wire nonce (AD, len=OVPN_OP_SIZE_V2+NONCE_WIRE_SIZE), +* 1, 2, 3, ..., n: payload, +* n+1: auth_tag (len=tag_size) +*/ +sg_init_table (sg, nfrags +2 ); + +/* build scatterlist to encrypt packet payload */ + ret =skb_to_sgvec_nomark (skb, sg +1 ,0 , skb->len); +if (unlikely (nfrags != ret)) { +kfree (sg);
This is the only location in this function (and ovpn_encrypt_post) that frees sg. Is that correct? sg points to an array contained within ->ctx, I don't think you want to free that directly.
drivers/net/ovpn/crypto_aead.c line 108
Quoted 13 lines
drivers/net/ovpn/crypto_aead.c new file +13
-0+return -EINVAL ; + } + +/* append auth_tag onto scatterlist */ +__skb_push (skb, tag_size); +sg_set_buf (sg + nfrags +1 , skb->data, tag_size); + +/* obtain packet ID, which is used both as a first +* 4 bytes of nonce and last 4 bytes of associated data. +*/ + ret =ovpn_pktid_xmit_next (&ks->pid_xmit, &pktid); +if (unlikely (ret <0 )) { +kfree (ovpn_skb_cb (skb)->ctx);
Isn't that going to cause a double-free when we get to the end of ovpn_encrypt_post? Or even UAF when we try to get ks/peer at the start?
Quoted 14 lines
drivers/net/ovpn/crypto_aead.c new file +14
-0+return ret; + } + +/* concat 4 bytes packet id and 8 bytes nonce tail into 12 bytes +* nonce +*/ +ovpn_pktid_aead_write (pktid, &ks->nonce_tail_xmit, iv); + +/* make space for packet id and push it to the front */ +__skb_push (skb,NONCE_WIRE_SIZE ); +memcpy (skb->data, iv,NONCE_WIRE_SIZE ); + +/* add packet op as head of additional data */ + op =ovpn_opcode_compose (OVPN_DATA_V2 , ks->key_id, peer->id);
drivers/net/ovpn/crypto_aead.c line 132
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+__skb_push (skb,OVPN_OP_SIZE_V2 ); +BUILD_BUG_ON (sizeof (op) !=OVPN_OP_SIZE_V2 ); + *((__force __be32 *)skb->data) =htonl (op); + +/* AEAD Additional data */ +sg_set_buf (sg, skb->data,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + + req =aead_request_alloc (ks->encrypt,GFP_ATOMIC ); +if (unlikely (!req)) { +kfree (ovpn_skb_cb (skb)->ctx);
Same here.
Quoted 12 lines
drivers/net/ovpn/crypto_aead.c new file +12
-0+return -ENOMEM ; + } + +/* setup async crypto operation */ +aead_request_set_tfm (req, ks->encrypt); +aead_request_set_callback (req,0 , ovpn_aead_encrypt_done, skb); +aead_request_set_crypt (req, sg, sg, skb->len - head_size, iv); +aead_request_set_ad (req,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + +ovpn_skb_cb (skb)->ctx->peer = peer; +ovpn_skb_cb (skb)->ctx->req = req; +ovpn_skb_cb (skb)->ctx->ks = ks;
drivers/net/ovpn/crypto_aead.c line 154
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+ +/* encrypt it */ +return crypto_aead_encrypt (req); +} + +static void ovpn_aead_decrypt_done (void *data,int ret) +{ +struct sk_buff *skb = data; + +aead_request_free (ovpn_skb_cb (skb)->ctx->req);
This function only gets called in the async case. Where's the corresponding aead_request_free in the sync case? (same for encrypt) This should be moved into ovpn_decrypt_post, I think.
drivers/net/ovpn/crypto_aead.c line 164
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+ovpn_decrypt_post (skb, ret); +} + +int ovpn_aead_decrypt (struct ovpn_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->decrypt); +int ret, payload_len, nfrags; +unsigned int payload_offset; +DECLARE_CRYPTO_WAIT (wait);
nit: unused
[...]
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) · line 160
Quoted 6 lines
drivers/net/ovpn/io.c +3
-2-static void ovpn_encrypt_post (struct sk_buff *skb,int ret) +void ovpn_encrypt_post (struct sk_buff *skb,int ret) { -struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; +struct ovpn_crypto_key_slot *ks =ovpn_skb_cb (skb)->ctx->ks; +struct ovpn_peer *peer =ovpn_skb_cb (skb)->ctx->peer;
ovpn_skb_cb(skb)->ctx may not have been set by ovpn_aead_encrypt.
Quoted 11 lines
drivers/net/ovpn/io.c +6
-0+ +/* encryption is happening asynchronously. This function will be +* called later by the crypto callback with a proper return value +*/ +if (unlikely (ret == -EINPROGRESS )) +return ;if (unlikely (ret <0 )) goto err;skb_mark_not_on_list (skb);
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
Quoted 10 lines
drivers/net/ovpn/io.c +2
-0+kfree (ovpn_skb_cb (skb)->ctx); +switch (peer->sock->sock->sk->sk_protocol) {case IPPROTO_UDP :ovpn_udp_send_skb (peer->ovpn, peer, skb);break ;default :/* no transport configured yet */ goto err;
ovpn_skb_cb(skb)->ctx has just been freed before this switch, and here we jump to err and free it again.
Quoted 13 lines
drivers/net/ovpn/io.c +3
-1}/* skb passed down the stack - don't free it */ skb =NULL ; err:if (unlikely (skb)) {dev_core_stats_tx_dropped_inc (peer->ovpn->dev); -kfree_skb (skb); +kfree (ovpn_skb_cb (skb)->ctx); } +kfree_skb (skb); +ovpn_crypto_key_slot_put (ks);ovpn_peer_put (peer); }
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Hi,
Quoted 6 lines · Sabrina Dubroca
Quoted 1 line · Antonio Quartulli
+/* this swap is not atomic, but there will be a very short time frame where the
Since we're under a mutex, I think we might get put to sleep for a not-so-short time frame.
Quoted 6 lines
Quoted 1 line
+ *
old_secondarykey won't be available. This should not be a big deal as mostI could be misreading the code, but isn't it
old_primarythat's unavailable during the swap?rcu_replace_pointeroverwritescs->primary, so before the final assign, both slots containold_secondary?
Right. The comment is not correct.
cs->secondary (old_secondary, that is the newest key) is what is probably being used by the other peer for sending traffic. Therefore old_secondary is what is likely to be needed.
However, this is pure speculation and may not be accurate.
The fact that we could sleep before having completed the swap sounds like something we want to avoid. Maybe I should convert this mutex to a spinlock. Its usage is fairly contained anyway.
Quoted 23 lines
Quoted 21 lines
+ * likely both peers are already using the new primary at this point.
+ */drivers/net/ovpn/crypto.c new file +18
-0+void ovpn_crypto_key_slots_swap (struct ovpn_crypto_state *cs) +{ +const struct ovpn_crypto_key_slot *old_primary, *old_secondary; + +mutex_lock (&cs->mutex); + + old_secondary =rcu_dereference_protected (cs->secondary, +lockdep_is_held (&cs->mutex)); + old_primary =rcu_replace_pointer (cs->primary, old_secondary, +lockdep_is_held (&cs->mutex)); +rcu_assign_pointer (cs->secondary, old_primary); + +pr_debug ("key swapped: %u <-> %u\n" , + old_primary ? old_primary->key_id :0 , + old_secondary ? old_secondary->key_id :0 ); + +mutex_unlock (&cs->mutex); +}[...]
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 49
Quoted 6 lines
drivers/net/ovpn/crypto_aead.c new file +6
-0+int ovpn_aead_encrypt (struct ovpn_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->encrypt); +const unsigned int head_size =ovpn_aead_encap_overhead (ks); +DECLARE_CRYPTO_WAIT (wait);nit: unused
ACK
Quoted 40 lines
Quoted 23 lines
drivers/net/ovpn/crypto_aead.c new file +22
-0+struct aead_request *req; +struct sk_buff *trailer; +struct scatterlist *sg; +u8 iv[NONCE_SIZE ]; +int nfrags, ret; +u32 pktid, op; + +/* Sample AEAD header format: +* 48000001 00000005 7e7046bd 444a7e28 cc6387b1 64a4d6c1 380275a... +* [ OP32 ] [seq # ] [ auth tag ] [ payload ... ] +* [4-byte +* IV head] +*/ + +/* check that there's enough headroom in the skb for packet +* encapsulation, after adding network header and encryption overhead +*/ +if (unlikely (skb_cow_head (skb,OVPN_HEAD_ROOM + head_size))) +return -ENOBUFS ; + +/* get number of skb frags and ensure that packet data is writable */ + nfrags =skb_cow_data (skb,0 , &trailer);drivers/net/ovpn/crypto_aead.c line 81
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+if (unlikely (nfrags <0 )) +return nfrags; + +if (unlikely (nfrags +2 > (MAX_SKB_FRAGS +2 ))) +return -ENOSPC ; + +ovpn_skb_cb (skb)->ctx =kmalloc (sizeof (*ovpn_skb_cb (skb)->ctx), +GFP_ATOMIC ); +if (unlikely (!ovpn_skb_cb (skb)->ctx)) +return -ENOMEM ;I think you should clear
skb->cb(or at least ->ctx) at the start ofovpn_aead_encrypt. I don't think it will be cleaned up by the previous user, and if we fail before this alloc, we will possibly have bogus values in ->ctx when we get to kfree(ovpn_skb_cb(skb)->ctx) at the end ofovpn_encrypt_post.
Quoted 2 lines
(Similar comments around cb/ctx freeing and initialization apply to
ovpn_aead_decryptandovpn_decrypt_post)
good point - will clear it in both cases.
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +9
-0+ sg =ovpn_skb_cb (skb)->ctx->sg; + +/* sg table: +* 0: op, wire nonce (AD, len=OVPN_OP_SIZE_V2+NONCE_WIRE_SIZE), +* 1, 2, 3, ..., n: payload, +* n+1: auth_tag (len=tag_size) +*/ +sg_init_table (sg, nfrags +2 ); +
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 95
Quoted 4 lines
drivers/net/ovpn/crypto_aead.c new file +4
-0+/* build scatterlist to encrypt packet payload */ + ret =skb_to_sgvec_nomark (skb, sg +1 ,0 , skb->len); +if (unlikely (nfrags != ret)) { +kfree (sg);This is the only location in this function (and
ovpn_encrypt_post) that frees sg. Is that correct? sg points to an array contained within ->ctx, I don't think you want to free that directly.
No, it is not correct. As you pointed out 'sg' is an actual array so should not be free'd.
This is a leftover of the first version where 'sg' was kmalloc'd.
FTR: this restructuring is the result of having tested encryption/decryption with pcrypt: sg, that is passed to the crypto code, was initially allocated on the stack, which was obviously not working for async crypto. The solution was to make it part of the skb CB area, so that it can be carried around until crypto is done.
Quoted 10 lines
Quoted 10 lines
drivers/net/ovpn/crypto_aead.c new file +9
-0+return -EINVAL ; + } + +/* append auth_tag onto scatterlist */ +__skb_push (skb, tag_size); +sg_set_buf (sg + nfrags +1 , skb->data, tag_size); + +/* obtain packet ID, which is used both as a first +* 4 bytes of nonce and last 4 bytes of associated data.
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 108
Quoted 4 lines
drivers/net/ovpn/crypto_aead.c new file +4
-0+*/ + ret =ovpn_pktid_xmit_next (&ks->pid_xmit, &pktid); +if (unlikely (ret <0 )) { +kfree (ovpn_skb_cb (skb)->ctx);Isn't that going to cause a double-free when we get to the end of
ovpn_encrypt_post? Or even UAF when we try to get ks/peer at the start?
ouch. you're right.
I should better leave it alone and let ovpn_encrypt_post take care of free'ing it at the end.
Quoted 19 lines
Quoted 19 lines
drivers/net/ovpn/crypto_aead.c new file +18
-0+return ret; + } + +/* concat 4 bytes packet id and 8 bytes nonce tail into 12 bytes +* nonce +*/ +ovpn_pktid_aead_write (pktid, &ks->nonce_tail_xmit, iv); + +/* make space for packet id and push it to the front */ +__skb_push (skb,NONCE_WIRE_SIZE ); +memcpy (skb->data, iv,NONCE_WIRE_SIZE ); + +/* add packet op as head of additional data */ + op =ovpn_opcode_compose (OVPN_DATA_V2 , ks->key_id, peer->id); +__skb_push (skb,OVPN_OP_SIZE_V2 ); +BUILD_BUG_ON (sizeof (op) !=OVPN_OP_SIZE_V2 ); + *((__force __be32 *)skb->data) =htonl (op); +
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 132
Quoted 6 lines
drivers/net/ovpn/crypto_aead.c new file +6
-0+/* AEAD Additional data */ +sg_set_buf (sg, skb->data,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + + req =aead_request_alloc (ks->encrypt,GFP_ATOMIC ); +if (unlikely (!req)) { +kfree (ovpn_skb_cb (skb)->ctx);Same here.
yap
Quoted 19 lines
Quoted 19 lines
drivers/net/ovpn/crypto_aead.c new file +18
-0+return -ENOMEM ; + } + +/* setup async crypto operation */ +aead_request_set_tfm (req, ks->encrypt); +aead_request_set_callback (req,0 , ovpn_aead_encrypt_done, skb); +aead_request_set_crypt (req, sg, sg, skb->len - head_size, iv); +aead_request_set_ad (req,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); + +ovpn_skb_cb (skb)->ctx->peer = peer; +ovpn_skb_cb (skb)->ctx->req = req; +ovpn_skb_cb (skb)->ctx->ks = ks; + +/* encrypt it */ +return crypto_aead_encrypt (req); +} + +static void ovpn_aead_decrypt_done (void *data,int ret)
Quoted 8 lines
drivers/net/ovpn/crypto_aead.c line 40
Quoted 4 lines
drivers/net/ovpn/crypto_aead.c new file +4
-0+{ +struct sk_buff *skb = data; + +aead_request_free (ovpn_skb_cb (skb)->ctx->req);This function only gets called in the async case. Where's the corresponding
aead_request_freein the sync case? (same for encrypt) This should be moved intoovpn_decrypt_post, I think.
I agree, although I am pretty sure I moved it here in the past for a reason. Maybe code changes eliminated that reason.
anyway, thanks for spotting this. I'll definitely move it to the _post functions.
Quoted 13 lines
drivers/net/ovpn/crypto_aead.c line 164
Quoted 11 lines
drivers/net/ovpn/crypto_aead.c new file +10
-0+ovpn_decrypt_post (skb, ret); +} + +int ovpn_aead_decrypt (struct ovpn_peer *peer,struct ovpn_crypto_key_slot *ks, +struct sk_buff *skb) +{ +const unsigned int tag_size =crypto_aead_authsize (ks->decrypt); +int ret, payload_len, nfrags; +unsigned int payload_offset; +DECLARE_CRYPTO_WAIT (wait);nit: unused
ACK
Quoted 11 lines
[...]
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) · line 160
Quoted 6 lines
drivers/net/ovpn/io.c +3
-2-static void ovpn_encrypt_post (struct sk_buff *skb,int ret) +void ovpn_encrypt_post (struct sk_buff *skb,int ret) { -struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; +struct ovpn_crypto_key_slot *ks =ovpn_skb_cb (skb)->ctx->ks; +struct ovpn_peer *peer =ovpn_skb_cb (skb)->ctx->peer;ovpn_skb_cb(skb)->ctx may not have been set by
ovpn_aead_encrypt.
right. we may have hit a return before kmalloc'ing it.
Quoted 13 lines
Quoted 13 lines
drivers/net/ovpn/io.c +6
-0+ +/* encryption is happening asynchronously. This function will be +* called later by the crypto callback with a proper return value +*/ +if (unlikely (ret == -EINPROGRESS )) +return ;if (unlikely (ret <0 )) goto err;skb_mark_not_on_list (skb);
Quoted 12 lines
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
Quoted 9 lines
drivers/net/ovpn/io.c +2
-0+kfree (ovpn_skb_cb (skb)->ctx); +switch (peer->sock->sock->sk->sk_protocol) {case IPPROTO_UDP :ovpn_udp_send_skb (peer->ovpn, peer, skb);break ;default :/* no transport configured yet */ goto err;ovpn_skb_cb(skb)->ctx has just been freed before this switch, and here we jump to err and free it again.
Thanks. Will reworka bit the error path here.
Quoted 15 lines
Quoted 7 lines
} /* skb passed down the stack - don't free it */ skb = NULL; err: if (unlikely(skb)) { dev_core_stats_tx_dropped_inc(peer->ovpn->dev);drivers/net/ovpn/io.c static void ovpn_encrypt_post(struct sk_buff *skb, int ret) · line 193
Quoted 7 lines
drivers/net/ovpn/io.c +3
-1-kfree_skb (skb); +kfree (ovpn_skb_cb (skb)->ctx); } +kfree_skb (skb); +ovpn_crypto_key_slot_put (ks);ovpn_peer_put (peer); }
This patch was basically re-written after realizing that the async crypto path was not working as expected, therefore sorry if there were some "kinda obvious" mistakes.
Thanks a lot for your review.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 16 lines · Antonio Quartulli
Hi,
Quoted 6 lines · Sabrina Dubroca
Quoted 1 line · Antonio Quartulli
+/* this swap is not atomic, but there will be a very short time frame where the
Since we're under a mutex, I think we might get put to sleep for a not-so-short time frame.
Quoted 6 lines
Quoted 1 line
+ *
old_secondarykey won't be available. This should not be a big deal as mostI could be misreading the code, but isn't it
old_primarythat's unavailable during the swap?rcu_replace_pointeroverwritescs->primary, so before the final assign, both slots containold_secondary?
Quoted 4 lines
Right. The comment is not correct.
cs->secondary(old_secondary, that is the newest key) is what is probably being used by the other peer for sending traffic.
Right, thanks. I was getting confused about the key slots and which key was the newest.
If the peer has already started sending with the newest key, no problem. If we're swapping keys before our peer (or we're on a slow network and the peer's packets get delayed), we'll still be receiving packets encrypted with the old key.
Quoted 3 lines
Therefore
old_secondaryis what is likely to be needed.However, this is pure speculation and may not be accurate.
I can think of a few possibilities if this causes too many unwanted drops:
- use a linked list of keys, set the primary instead of swapping, and let delete remove the unused key(s) by ID instead of slot
- decouple the TX and RX keys, which also means you don't really need to swap keys (swap for the TX key becomes "set primary", swap on RX can become a noop since you check both slots for the correct keyid) -- and here too delete becomes based on key ID
- if cs->mutex becomes a spinlock, take it in the datapath when looking up keys. this will make sure we get a consistent view of the keys state.
- come up with a scheme to let the datapath retry the key lookup if it didn't find the key it wanted (maybe something like a seqcount, or maybe taking the lock and retrying if the lookup failed)
I don't know if options 1 and 2 are possible based on how openvpn (the protocol and the userspace application) models keys, but they seem a bit "cleaner" on the datapath side (no locking, no retry). But they require a different API.
Do you have the same problem in the current userspace implementation?
Quoted 4 lines
The fact that we could sleep before having completed the swap sounds like something we want to avoid.
Maybe I should convert this mutex to a spinlock. Its usage is fairly contained anyway.
I think it would make sense. It's only being held for very short periods, just to set/swap a few pointers.
Quoted 5 lines
FTR: this restructuring is the result of having tested encryption/decryption with pcrypt: sg, that is passed to the crypto code, was initially allocated on the stack, which was obviously not working for async crypto. The solution was to make it part of the skb CB area, so that it can be carried around until crypto is done.
I see. I thought this patch looked less familiar than the others :)
An alternative to using the CB is what IPsec does: allocate a chunk of memory for all its temporary needs (crypto req, sg, iv, anything else it needs during async crypto) and carve the pointers/small chunks out of it. See esp_alloc_tmp in net/ipv4/esp4.c. (I'm just mentioning that for reference/curiosity, not asking that you change ovpn)
Quoted 3 lines
This patch was basically re-written after realizing that the async crypto path was not working as expected, therefore sorry if there were some "kinda obvious" mistakes.
And I completely missed some of those issues in previous reviews.
Quoted 1 line
Thanks a lot for your review.
Cheers, and thanks for your patience.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 22 lines · Sabrina Dubroca
Quoted 16 lines · Antonio Quartulli
Hi,
Quoted 6 lines · Sabrina Dubroca
Quoted 1 line · Antonio Quartulli
+/* this swap is not atomic, but there will be a very short time frame where the
Since we're under a mutex, I think we might get put to sleep for a not-so-short time frame.
Quoted 6 lines
Quoted 1 line
+ *
old_secondarykey won't be available. This should not be a big deal as mostI could be misreading the code, but isn't it
old_primarythat's unavailable during the swap?rcu_replace_pointeroverwritescs->primary, so before the final assign, both slots containold_secondary?Quoted 4 lines
Right. The comment is not correct.
cs->secondary(old_secondary, that is the newest key) is what is probably being used by the other peer for sending traffic.
Quoted 7 lines
Right, thanks. I was getting confused about the key slots and which key was the newest.
If the peer has already started sending with the newest key, no problem. If we're swapping keys before our peer (or we're on a slow network and the peer's packets get delayed), we'll still be receiving packets encrypted with the old key.
Right, it's mostly impossible to make the swap happen at the same time on both ends.
Quoted 21 lines
Quoted 5 lines
Therefore
old_secondaryis what is likely to be needed.However, this is pure speculation and may not be accurate.
I can think of a few possibilities if this causes too many unwanted drops:
- use a linked list of keys, set the primary instead of swapping, and let delete remove the unused key(s) by ID instead of slot
- decouple the TX and RX keys, which also means you don't really need to swap keys (swap for the TX key becomes "set primary", swap on RX can become a noop since you check both slots for the correct keyid) -- and here too delete becomes based on key ID
- if
cs->mutexbecomes a spinlock, take it in the datapath when looking up keys. this will make sure we get a consistent view of the keys state.
Quoted 8 lines
- come up with a scheme to let the datapath retry the key lookup if it didn't find the key it wanted (maybe something like a seqcount, or maybe taking the lock and retrying if the lookup failed)
I don't know if options 1 and 2 are possible based on how openvpn (the protocol and the userspace application) models keys, but they seem a bit "cleaner" on the datapath side (no locking, no retry). But they require a different API.
After chewing over all these ideas I think we can summarize the requirements as follows:
* PRIMARY and SECONDARY in the API is just an abstraction for "KEY FOR TX" and "THE OTHER KEY";
* SWAP means "mark for TX the key that currently was not marked";
* we only need a pointer to the key for TX;
* key for RX is picked up by key ID;
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
* array index is the key ID (fast lookup on RX);
* upon SET_KEY we store the provided keys and erase the rest;
* upon SET_KEY we store in a new field the index/ID of the PRIMARY (fast lookup on TX), namely primary_id;
* upon SWAP we just save in primary_id the "other" index/ID;
* at any given time we will have only two keys in the array.
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Opinions?
Quoted 2 lines
Do you have the same problem in the current userspace implementation?
userspace is single-threaded :-P
nothing happens while we are busy swapping.
Quoted 9 lines
Quoted 6 lines
The fact that we could sleep before having completed the swap sounds like something we want to avoid.
Maybe I should convert this mutex to a spinlock. Its usage is fairly contained anyway.I think it would make sense. It's only being held for very short periods, just to set/swap a few pointers.
ACK
Quoted 21 lines
Quoted 7 lines
FTR: this restructuring is the result of having tested encryption/decryption with pcrypt: sg, that is passed to the crypto code, was initially allocated on the stack, which was obviously not working for async crypto. The solution was to make it part of the skb CB area, so that it can be carried around until crypto is done.
I see. I thought this patch looked less familiar than the others :)
An alternative to using the CB is what IPsec does: allocate a chunk of memory for all its temporary needs (crypto req, sg, iv, anything else it needs during async crypto) and carve the pointers/small chunks out of it. See
esp_alloc_tmpinnet/ipv4/esp4.c. (I'm just mentioning that for reference/curiosity, not asking that you change ovpn)Quoted 3 lines
This patch was basically re-written after realizing that the async crypto path was not working as expected, therefore sorry if there were some "kinda obvious" mistakes.
Quoted 6 lines
And I completely missed some of those issues in previous reviews.
Quoted 1 line
Thanks a lot for your review.
Cheers, and thanks for your patience.
Cheers!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 31 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Quoted 1 line · Sabrina Dubroca
2024-08-27, 14:07:52 +0200, Antonio Quartulli wrote:
I can think of a few possibilities if this causes too many unwanted drops:
- use a linked list of keys, set the primary instead of swapping, and let delete remove the unused key(s) by ID instead of slot
- decouple the TX and RX keys, which also means you don't really need to swap keys (swap for the TX key becomes "set primary", swap on RX can become a noop since you check both slots for the correct keyid) -- and here too delete becomes based on key ID
- if
cs->mutexbecomes a spinlock, take it in the datapath when looking up keys. this will make sure we get a consistent view of the keys state.Quoted 8 lines
- come up with a scheme to let the datapath retry the key lookup if it didn't find the key it wanted (maybe something like a seqcount, or maybe taking the lock and retrying if the lookup failed)
I don't know if options 1 and 2 are possible based on how openvpn (the protocol and the userspace application) models keys, but they seem a bit "cleaner" on the datapath side (no locking, no retry). But they require a different API.
After chewing over all these ideas I think we can summarize the requirements as follows:
Quoted 8 lines
* PRIMARY and SECONDARY in the API is just an abstraction for "KEY FOR TX" and "THE OTHER KEY";
* SWAP means "mark for TX the key that currently was not marked";
* we only need a pointer to the key for TX;
* key for RX is picked up by key ID;Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (include/net/macsec.h: macsec_{tx,rx}_sc, sa array).
Quoted 2 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;
I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;
* upon SWAP we just save inprimary_idthe "other" index/ID;
I'm confused by these two. Both SET_KEY and SWAP modify the primary_id?
Quoted 1 line
* at any given time we will have only two keys in the array.
This needs to be enforced in the SET_KEY implementation, otherwise SWAP will have inconsistent effects.
Quoted 2 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
Quoted 2 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a set_tx operation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in ovpn_crypto_state become slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primary
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 38 lines · Sabrina Dubroca
Quoted 37 lines · Antonio Quartulli
Quoted 18 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Quoted 1 line · Sabrina Dubroca
2024-08-27, 14:07:52 +0200, Antonio Quartulli wrote:
I can think of a few possibilities if this causes too many unwanted drops:
- use a linked list of keys, set the primary instead of swapping, and let delete remove the unused key(s) by ID instead of slot
- decouple the TX and RX keys, which also means you don't really need to swap keys (swap for the TX key becomes "set primary", swap on RX can become a noop since you check both slots for the correct keyid) -- and here too delete becomes based on key ID
- if
cs->mutexbecomes a spinlock, take it in the datapath when looking up keys. this will make sure we get a consistent view of the keys state.Quoted 8 lines
- come up with a scheme to let the datapath retry the key lookup if it didn't find the key it wanted (maybe something like a seqcount, or maybe taking the lock and retrying if the lookup failed)
I don't know if options 1 and 2 are possible based on how openvpn (the protocol and the userspace application) models keys, but they seem a bit "cleaner" on the datapath side (no locking, no retry). But they require a different API.
After chewing over all these ideas I think we can summarize the requirements as follows:
* PRIMARY and SECONDARY in the API is just an abstraction for "KEY FOR TX" and "THE OTHER KEY";
* SWAP means "mark for TX the key that currently was not marked";
* we only need a pointer to the key for TX;
* key for RX is picked up by key ID;
Quoted 6 lines
Quoted 2 lines
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (
include/net/macsec.h: macsec_{tx,rx}_sc, sa array).
thanks for the pointer!
Quoted 8 lines
Quoted 3 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Yeah, my point is wrong.
I fooled myself assuming SET_KEY would install two keys each time, but this is not the case, so we can't "erase everything else".
What we can do is:
* if SET_KEY wants to install a key in PRIMARY, then we erase the key marked as "primary" and we mark the new one as such
* if SET_KEY wants to install a key in SECONDARY, then we erase the key without any mark.
This way we simulate writing into slots.
Quoted 3 lines
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;
FTR: this is what "marking as primary" means
Quoted 3 lines
Quoted 1 line
* upon SWAP we just save in
primary_idthe "other" index/ID;I'm confused by these two. Both
SET_KEYand SWAP modify theprimary_id?
Yes.SET_KEY may want to install a key in the primary slot: in that case the new key has to be marked as primary immediately. This normally happens only upon first SET_KEY after connection, when no other key exists.
SWAP will switch primary_id to the other key ID.
makes sense?
Quoted 5 lines
Quoted 2 lines
* at any given time we will have only two keys in the array.
This needs to be enforced in the
SET_KEYimplementation, otherwise SWAP will have inconsistent effects.
yap. I hope what I wrote above about SET_KEY helps clarifying this part.
Quoted 8 lines
Quoted 4 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
I would not decouple TX and RX keys.
Each array item should be the same as what we are now storing into each slot (struct ovpn_crypto_key_slot *). I could use two arrays, instead of an array of slots, but that means changing way more logic and I don't see the benefit.
Quoted 5 lines
Quoted 4 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Quoted 11 lines
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a
set_txoperation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)
unfortunately changing userspace that way is not currently viable. There are more components (and documentation) that attach to this slot abstraction.
Hence my proposal to keep the API the same, but rework the internal for better implementation.
Quoted 15 lines
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in
ovpn_crypto_statebecome slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primary
Quoted 3 lines
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
This approach sounds very close to what I was aiming for, but simpler because we have slots[2] instead of slots[8] (which means that primary_id can be just 'one-bit').
The only downside is that upon RX we have to check both keys. However I don't think this is an issue as we have just 2 keys at most.
I could even optimize the lookup by starting always from the primary key, as that's the one being used most of the time by the sender.
Ok, I will go with this approach you summarized here at the end.
Thanks a lot!
Cheers,
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 23 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (
include/net/macsec.h: macsec_{tx,rx}_sc, sa array).thanks for the pointer!
Quoted 8 lines
Quoted 3 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Yeah, my point is wrong.
I fooled myself assumingSET_KEYwould install two keys each time, but this is not the case, so we can't "erase everything else".
Quoted 5 lines
What we can do is:
* ifSET_KEYwants to install a key in PRIMARY, then we erase the key marked as "primary" and we mark the new one as such
* ifSET_KEYwants to install a key in SECONDARY, then we erase the key without any mark.
Ok, then the DEL_KEY op would not really be needed.
Quoted 19 lines
This way we simulate writing into slots.
Quoted 3 lines
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;FTR: this is what "marking as primary" means
Quoted 3 lines
Quoted 1 line
* upon SWAP we just save in
primary_idthe "other" index/ID;I'm confused by these two. Both
SET_KEYand SWAP modify theprimary_id?Yes.
SET_KEYmay want to install a key in the primary slot: in that case the new key has to be marked as primary immediately.
This normally happens only upon firstSET_KEYafter connection, when no other key exists.
Quoted 3 lines
SWAP will switch
primary_idto the other key ID.makes sense?
Yes, thanks, that fixes up all my confusion around setting primary_id.
Quoted 16 lines
Quoted 4 lines
Quoted 1 line
* at any given time we will have only two keys in the array.
This needs to be enforced in the
SET_KEYimplementation, otherwise SWAP will have inconsistent effects.yap. I hope what I wrote above about
SET_KEYhelps clarifying this part.Quoted 8 lines
Quoted 4 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
Quoted 5 lines
I would not decouple TX and RX keys.
Each array item should be the same as what we are now storing into each slot (structovpn_crypto_key_slot*).
I could use two arrays, instead of an array of slots, but that means changing way more logic and I don't see the benefit.
Avoid changing both keys every time when a link has very asymmetric traffic. And the concept of a primary/secondary key makes no sense on receive, all you need is keyids, and there's no need to swap slots, which avoids the "key not available during SWAP" issue entirely. Primary key only makes sense on TX.
But I'm guessing the keypair concept is also baked deep into openvpn.
Quoted 15 lines
Quoted 14 lines
Quoted 2 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a
set_txoperation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)
Quoted 3 lines
unfortunately changing userspace that way is not currently viable. There are more components (and documentation) that attach to this slot abstraction.
I thought that might be the case. Too bad, it means we have to keep the slots API and the kernel will be stuck with it forever (even if some day userspace can manage key ids instead of slots).
Quoted 2 lines
Hence my proposal to keep the API the same, but rework the internal for better implementation.
Yeah, that makes sense if you have that constraint imposed by the existing userspace.
Quoted 16 lines
Quoted 16 lines
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in
ovpn_crypto_statebecome slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primary
Quoted 7 lines
Quoted 3 lines
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
This approach sounds very close to what I was aiming for, but simpler because we have slots[2] instead of slots[8] (which means that
primary_idcan be just 'one-bit').
Yes.
Quoted 5 lines
The only downside is that upon RX we have to check both keys. However I don't think this is an issue as we have just 2 keys at most.
I could even optimize the lookup by starting always from the primary key, as that's the one being used most of the time by the sender.
Nice.
Quoted 1 line
Ok, I will go with this approach you summarized here at the end.
Ok. I think it should be a pretty minimal set of changes on top of the existing code, especially if you hide the primary/secondary accesses in inline helpers.
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 24 lines · Sabrina Dubroca
Quoted 23 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (
include/net/macsec.h: macsec_{tx,rx}_sc, sa array).thanks for the pointer!
Quoted 8 lines
Quoted 3 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Yeah, my point is wrong.
I fooled myself assumingSET_KEYwould install two keys each time, but this is not the case, so we can't "erase everything else".
Quoted 7 lines
Quoted 5 lines
What we can do is:
* ifSET_KEYwants to install a key in PRIMARY, then we erase the key marked as "primary" and we mark the new one as such
* ifSET_KEYwants to install a key in SECONDARY, then we erase the key without any mark.Ok, then the
DEL_KEYop would not really be needed.
It would be needed only when we want to kick an old key without installing a new one.
IMHO something that should not truly happen during the normal lifecycle of a peer, but there might be some corner cases where userspace may still want to do that (I am checking userspace to understand when this can truly happen, if at all)
Quoted 48 lines
Quoted 20 lines
This way we simulate writing into slots.
Quoted 3 lines
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;FTR: this is what "marking as primary" means
Quoted 3 lines
Quoted 1 line
* upon SWAP we just save in
primary_idthe "other" index/ID;I'm confused by these two. Both
SET_KEYand SWAP modify theprimary_id?Yes.
SET_KEYmay want to install a key in the primary slot: in that case the new key has to be marked as primary immediately.
This normally happens only upon firstSET_KEYafter connection, when no other key exists.Quoted 3 lines
SWAP will switch
primary_idto the other key ID.makes sense?
Yes, thanks, that fixes up all my confusion around setting
primary_id.Quoted 16 lines
Quoted 4 lines
Quoted 1 line
* at any given time we will have only two keys in the array.
This needs to be enforced in the
SET_KEYimplementation, otherwise SWAP will have inconsistent effects.yap. I hope what I wrote above about
SET_KEYhelps clarifying this part.Quoted 8 lines
Quoted 4 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
Quoted 5 lines
I would not decouple TX and RX keys.
Each array item should be the same as what we are now storing into each slot (structovpn_crypto_key_slot*).
I could use two arrays, instead of an array of slots, but that means changing way more logic and I don't see the benefit.
Quoted 7 lines
Avoid changing both keys every time when a link has very asymmetric traffic. And the concept of a primary/secondary key makes no sense on receive, all you need is keyids, and there's no need to swap slots, which avoids the "key not available during SWAP" issue entirely. Primary key only makes sense on TX.
But I'm guessing the keypair concept is also baked deep into openvpn.
Correct. Both keys are recomputed upon each renegotiation, even if one key was still usable.
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 4 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a
set_txoperation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)
Quoted 7 lines
Quoted 3 lines
unfortunately changing userspace that way is not currently viable. There are more components (and documentation) that attach to this slot abstraction.
I thought that might be the case. Too bad, it means we have to keep the slots API and the kernel will be stuck with it forever (even if some day userspace can manage key ids instead of slots).
Yap, but I think there won't be any change like that in the near future.
Quoted 39 lines
Quoted 3 lines
Hence my proposal to keep the API the same, but rework the internal for better implementation.
Yeah, that makes sense if you have that constraint imposed by the existing userspace.
Quoted 16 lines
Quoted 16 lines
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in
ovpn_crypto_statebecome slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primaryQuoted 7 lines
Quoted 3 lines
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
This approach sounds very close to what I was aiming for, but simpler because we have slots[2] instead of slots[8] (which means that
primary_idcan be just 'one-bit').Yes.
Quoted 5 lines
The only downside is that upon RX we have to check both keys. However I don't think this is an issue as we have just 2 keys at most.
I could even optimize the lookup by starting always from the primary key, as that's the one being used most of the time by the sender.
Quoted 7 lines
Nice.
Quoted 1 line
Ok, I will go with this approach you summarized here at the end.
Ok. I think it should be a pretty minimal set of changes on top of the existing code, especially if you hide the primary/secondary accesses in inline helpers.
I think so to - working on it.
Thanks a lot!
Cheers,
Quoted 1 line
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Re: [PATCH net-next v6 12/25] ovpn: implement packet processing
Hello Antonio, Sabrina,
let me join the key management design discussion and put my 2 cents in. Please find my comment below.
Quoted 115 lines · Antonio Quartulli
Quoted 24 lines · Sabrina Dubroca
Quoted 23 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (
include/net/macsec.h: macsec_{tx,rx}_sc, sa array).thanks for the pointer!
Quoted 8 lines
Quoted 3 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Yeah, my point is wrong.
I fooled myself assumingSET_KEYwould install two keys each time, but this is not the case, so we can't "erase everything else".Quoted 7 lines
Quoted 5 lines
What we can do is:
* ifSET_KEYwants to install a key in PRIMARY, then we erase the key marked as "primary" and we mark the new one as such
* ifSET_KEYwants to install a key in SECONDARY, then we erase the key without any mark.Ok, then the
DEL_KEYop would not really be needed.It would be needed only when we want to kick an old key without installing a new one.
IMHO something that should not truly happen during the normal lifecycle of a peer, but there might be some corner cases where userspace may still want to do that (I am checking userspace to understand when this can truly happen, if at all)Quoted 48 lines
Quoted 20 lines
This way we simulate writing into slots.
Quoted 3 lines
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;FTR: this is what "marking as primary" means
Quoted 3 lines
Quoted 1 line
* upon SWAP we just save in
primary_idthe "other" index/ID;I'm confused by these two. Both
SET_KEYand SWAP modify theprimary_id?Yes.
SET_KEYmay want to install a key in the primary slot: in that case the new key has to be marked as primary immediately.
This normally happens only upon firstSET_KEYafter connection, when no other key exists.Quoted 3 lines
SWAP will switch
primary_idto the other key ID.makes sense?
Yes, thanks, that fixes up all my confusion around setting
primary_id.Quoted 16 lines
Quoted 4 lines
Quoted 1 line
* at any given time we will have only two keys in the array.
This needs to be enforced in the
SET_KEYimplementation, otherwise SWAP will have inconsistent effects.yap. I hope what I wrote above about
SET_KEYhelps clarifying this part.Quoted 8 lines
Quoted 4 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
Quoted 5 lines
I would not decouple TX and RX keys.
Each array item should be the same as what we are now storing into each slot (structovpn_crypto_key_slot*).
I could use two arrays, instead of an array of slots, but that means changing way more logic and I don't see the benefit.Quoted 7 lines
Avoid changing both keys every time when a link has very asymmetric traffic. And the concept of a primary/secondary key makes no sense on receive, all you need is keyids, and there's no need to swap slots, which avoids the "key not available during SWAP" issue entirely. Primary key only makes sense on TX.
But I'm guessing the keypair concept is also baked deep into openvpn.
Correct. Both keys are recomputed upon each renegotiation, even if one key was still usable.
Quoted 21 lines
Quoted 20 lines
Quoted 5 lines
Quoted 4 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Quoted 11 lines
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a
set_txoperation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)unfortunately changing userspace that way is not currently viable. There are more components (and documentation) that attach to this slot abstraction.
Quoted 5 lines
Quoted 3 lines
I thought that might be the case. Too bad, it means we have to keep the slots API and the kernel will be stuck with it forever (even if some day userspace can manage key ids instead of slots).
Yap, but I think there won't be any change like that in the near future.
As far as I understand the conclusion of the discussion, the best practice for the keys management is to keep keys discrete including the Tx/Rx keys separation. On the other hand, from the OpenVPN protocol perspective, keys are grouped into slots and peers perform so called key slots swapping.
As I can see, we have two options to adjust userspace application and kernel to each other:
1) implement "key slots" abstraction in Netlink API to keep kernel interface similar to the current OpenVPN protocol;
2) implement "discrete" keys management via Netlink API and move "slots" abstraction to the userspace application.
Sabrina already mentioned that kernel will stuck with the slots abstraction forever. This is due to the rule that user's API should not be changed.
Antonio, can we consider implementing the slot abstraction in the userspace application? It will move complexity from the kernel to the userspace, where it can be easy debugged and maintained.
And since the keys management is part of the control plane. Keeping abstraction closer to the control plane implementation will allow any protocol modification, while keeping the kernel forward compatible.
Quoted 47 lines
Quoted 38 lines
Quoted 2 lines
Hence my proposal to keep the API the same, but rework the internal for better implementation.
Yeah, that makes sense if you have that constraint imposed by the existing userspace.
Quoted 16 lines
Quoted 16 lines
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in
ovpn_crypto_statebecome slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primaryQuoted 7 lines
Quoted 3 lines
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
This approach sounds very close to what I was aiming for, but simpler because we have slots[2] instead of slots[8] (which means that
primary_idcan be just 'one-bit').Yes.
Quoted 5 lines
The only downside is that upon RX we have to check both keys. However I don't think this is an issue as we have just 2 keys at most.
I could even optimize the lookup by starting always from the primary key, as that's the one being used most of the time by the sender.
Quoted 7 lines
Nice.
Quoted 1 line
Ok, I will go with this approach you summarized here at the end.
Ok. I think it should be a pretty minimal set of changes on top of the existing code, especially if you hide the primary/secondary accesses in inline helpers.
I think so to - working on it.
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Hi,
Quoted 132 lines · Sergey Ryazanov
Hello Antonio, Sabrina,
let me join the key management design discussion and put my 2 cents in. Please find my comment below.
Quoted 115 lines · Antonio Quartulli
Quoted 24 lines · Sabrina Dubroca
Quoted 23 lines · Antonio Quartulli
Quoted 7 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
Therefore, how about having an array large enough to store all key IDs (max ID is 7):
Right, I forgot about that. For 8 keys, sure, array sounds ok. MACsec has only 4 and I also implemented it as an array (
include/net/macsec.h: macsec_{tx,rx}_sc, sa array).thanks for the pointer!
Quoted 8 lines
Quoted 3 lines
* array index is the key ID (fast lookup on RX);
* uponSET_KEYwe store the provided keys and erase the rest;I'm not sure about erasing the rest. If you're installing the secondary ("next primary") key, you can't just erase the current primary? ("erase the rest" also means you'd only ever have one key in the array, which contradicts your last item in this list)
Yeah, my point is wrong.
I fooled myself assumingSET_KEYwould install two keys each time, but this is not the case, so we can't "erase everything else".Quoted 7 lines
Quoted 5 lines
What we can do is:
* ifSET_KEYwants to install a key in PRIMARY, then we erase the key marked as "primary" and we mark the new one as such
* ifSET_KEYwants to install a key in SECONDARY, then we erase the key without any mark.Ok, then the
DEL_KEYop would not really be needed.It would be needed only when we want to kick an old key without installing a new one.
IMHO something that should not truly happen during the normal lifecycle of a peer, but there might be some corner cases where userspace may still want to do that (I am checking userspace to understand when this can truly happen, if at all)Quoted 48 lines
Quoted 20 lines
This way we simulate writing into slots.
Quoted 3 lines
Quoted 3 lines
* upon
SET_KEYwe store in a new field the index/ID of the PRIMARY (fast lookup on TX), namelyprimary_id;FTR: this is what "marking as primary" means
Quoted 3 lines
Quoted 1 line
* upon SWAP we just save in
primary_idthe "other" index/ID;I'm confused by these two. Both
SET_KEYand SWAP modify theprimary_id?Yes.
SET_KEYmay want to install a key in the primary slot: in that case the new key has to be marked as primary immediately.
This normally happens only upon firstSET_KEYafter connection, when no other key exists.Quoted 3 lines
SWAP will switch
primary_idto the other key ID.makes sense?
Yes, thanks, that fixes up all my confusion around setting
primary_id.Quoted 16 lines
Quoted 4 lines
Quoted 1 line
* at any given time we will have only two keys in the array.
This needs to be enforced in the
SET_KEYimplementation, otherwise SWAP will have inconsistent effects.yap. I hope what I wrote above about
SET_KEYhelps clarifying this part.Quoted 8 lines
Quoted 4 lines
It's pretty much like your option 1 and 2, but using an array indexed by key ID.
Are you decoupling TX and RX keys (so that they can be installed independently) in this proposal? I can't really tell, the array could be key pairs or separate.
Quoted 5 lines
I would not decouple TX and RX keys.
Each array item should be the same as what we are now storing into each slot (structovpn_crypto_key_slot*).
I could use two arrays, instead of an array of slots, but that means changing way more logic and I don't see the benefit.Quoted 7 lines
Avoid changing both keys every time when a link has very asymmetric traffic. And the concept of a primary/secondary key makes no sense on receive, all you need is keyids, and there's no need to swap slots, which avoids the "key not available during SWAP" issue entirely. Primary key only makes sense on TX.
But I'm guessing the keypair concept is also baked deep into openvpn.
Correct. Both keys are recomputed upon each renegotiation, even if one key was still usable.
Quoted 21 lines
Quoted 20 lines
Quoted 5 lines
Quoted 4 lines
The concept of slot is a bit lost, but it is not important as long as we can keep the API and its semantics the same.
Quoted 11 lines
Would it be a problem for userspace to keep track of key IDs, and use that (instead of slots) to communicate with the kernel? Make setkey/delkey be based on keyid, and replace swap with a
set_txoperation which updates the current keyid for TX. That would seem like a better API, especially for the per-ID array you're proposing here. The concept of slots would be almost completely lost (only "current tx key" is left, which is similar to the "primary slot"). (it becomes almost identical to the way MACsec does things, except possibly that in MACsec the TX and RX keys are not paired at all, so you can install/delete a TX key without touching any RX key)unfortunately changing userspace that way is not currently viable. There are more components (and documentation) that attach to this slot abstraction.
Quoted 5 lines
Quoted 3 lines
I thought that might be the case. Too bad, it means we have to keep the slots API and the kernel will be stuck with it forever (even if some day userspace can manage key ids instead of slots).
Yap, but I think there won't be any change like that in the near future.
As far as I understand the conclusion of the discussion, the best practice for the keys management is to keep keys discrete including the Tx/Rx keys separation. On the other hand, from the OpenVPN protocol perspective, keys are grouped into slots and peers perform so called key slots swapping.
As I can see, we have two options to adjust userspace application and kernel to each other:
1) implement "key slots" abstraction in Netlink API to keep kernel interface similar to the current OpenVPN protocol;
2) implement "discrete" keys management via Netlink API and move "slots" abstraction to the userspace application.
Quoted 5 lines
Sabrina already mentioned that kernel will stuck with the slots abstraction forever. This is due to the rule that user's API should not be changed.
Antonio, can we consider implementing the slot abstraction in the userspace application? It will move complexity from the kernel to the userspace, where it can be easy debugged and maintained.
And since the keys management is part of the control plane. Keeping abstraction closer to the control plane implementation will allow any protocol modification, while keeping the kernel forward compatible.
Thanks for chiming in!
Right now the implementation is already pretty simple IMHO, there is not much complexity to move to userspace. And that's because ovpn in kernelspace "thinks" in terms of slots like userspace.
If we want to go with a different approach, we have to add the "complexity" of converting abstraction from slots to anything being proposed here.
I presume you're advocating about moving from slots to key IDs (and separate keys for TX and RX)?
How do you see it working in detail?
I.e. how many keys should we store and when should we purge them?
(right now we just keep 2 pair of keys - aka slots - and the lifecycle is simply done) And why do you think it is simpler?
One more advantage I see about using slots is that it's easy to match the user and kernel space states: at any point in time userspace can say "these are the pairs I have in my two slots" and kernelspace will just mirror that, without having to care about old IDs.
Don't you think this approach is already pretty simple and less error prone with respect to going through an abstraction conversion?
Thanks!
Cheers,
Quoted 49 lines
Quoted 48 lines
Quoted 39 lines
Quoted 3 lines
Hence my proposal to keep the API the same, but rework the internal for better implementation.
Yeah, that makes sense if you have that constraint imposed by the existing userspace.
Quoted 16 lines
Quoted 16 lines
Maybe you can also rework the current code to look a bit like this array-based proposal, and not give up the notion of slots, if that's something strongly built into the core of openvpn:
- primary/secondary in
ovpn_crypto_statebecome slots[2]
- add a one-bit "primary" reference, used to look into the slots array
- swap just flips that "primary" bit
- TX uses slots[primary]
- RX keeps inspecting both slots (now slot[0] and slot[1] instead of primary/secondary) looking for the correct keyid
- setkey/delkey still operate on primary/secondary, but need to figure out whether slot[0] or slot[1] is primary/secondary based on ->primaryQuoted 7 lines
Quoted 3 lines
It's almost identical to the current code (and API), except you don't need to reassign any pointers to swap keys, so it should avoid the rekey issue.
This approach sounds very close to what I was aiming for, but simpler because we have slots[2] instead of slots[8] (which means that
primary_idcan be just 'one-bit').Yes.
Quoted 5 lines
The only downside is that upon RX we have to check both keys. However I don't think this is an issue as we have just 2 keys at most.
I could even optimize the lookup by starting always from the primary key, as that's the one being used most of the time by the sender.
Quoted 7 lines
Nice.
Quoted 1 line
Ok, I will go with this approach you summarized here at the end.
Ok. I think it should be a pretty minimal set of changes on top of the existing code, especially if you hide the primary/secondary accesses in inline helpers.
I think so to - working on it.
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Simon Horman
reviewerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
In reply to Antonio QuartulliQuoted 7 lines · Antonio Quartulli
This change implements encryption/decryption and
encapsulation/decapsulation of OpenVPN packets.Support for generic crypto state is added along with a wrapper for the AEAD crypto kernel API.
Signed-off-by Antonio Quartulli <[email protected]>
...
Quoted 1 line
drivers/net/ovpn/io.c +0
-0
...
Quoted 5 lines
drivers/net/ovpn/io.c +0
-1@@ -54,39 +56,122 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); } -static void ovpn_decrypt_post (struct sk_buff *skb,int ret)
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) · line 66
Quoted 10 lines
drivers/net/ovpn/io.c +6
-2+void ovpn_decrypt_post (struct sk_buff *skb,int ret) { -struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; +struct ovpn_crypto_key_slot *ks =ovpn_skb_cb (skb)->ctx->ks; +struct ovpn_peer *peer =ovpn_skb_cb (skb)->ctx->peer; +__be16 proto; +__be32 *pid; -if (unlikely (ret <0 )) +/* crypto is happening asyncronously. this function will be called
nit: asynchronously
Flagged by checkpatch.pl --codespell
Quoted 4 lines
drivers/net/ovpn/io.c +4
-0+* again later by the crypto callback with a proper return code +*/ +if (unlikely (ret == -EINPROGRESS )) +return ;
...
Antonio Quartulli
maintainerRe: [PATCH net-next v6 12/25] ovpn: implement packet processing
Quoted 28 lines · Simon Horman
Quoted 7 lines · Antonio Quartulli
This change implements encryption/decryption and
encapsulation/decapsulation of OpenVPN packets.Support for generic crypto state is added along with a wrapper for the AEAD crypto kernel API.
Signed-off-by Antonio Quartulli <[email protected]>...
Quoted 1 line
drivers/net/ovpn/io.c +0
-0...
Quoted 13 lines
drivers/net/ovpn/io.c +5
-2@@ -54,39 +56,122 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)dev_sw_netstats_rx_add (peer->ovpn->dev, skb->len); } -static void ovpn_decrypt_post (struct sk_buff *skb,int ret) +void ovpn_decrypt_post (struct sk_buff *skb,int ret) { -struct ovpn_peer *peer =ovpn_skb_cb (skb)->peer; +struct ovpn_crypto_key_slot *ks =ovpn_skb_cb (skb)->ctx->ks; +struct ovpn_peer *peer =ovpn_skb_cb (skb)->ctx->peer; +__be16 proto; +__be32 *pid;
Quoted 6 lines
drivers/net/ovpn/io.c static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) · line 66
Quoted 2 lines
drivers/net/ovpn/io.c +1
-1-if (unlikely (ret <0 )) +/* crypto is happening asyncronously. this function will be called nit: asynchronously
Flagged by checkpatch.pl --codespell
Thanks! I forgot to add the codespell flag to my last checkpatch run :-( Will fix it in the next version.
Cheers,
Quoted 7 lines
Quoted 5 lines
drivers/net/ovpn/io.c +4
-0+* again later by the crypto callback with a proper return code +*/ +if (unlikely (ret == -EINPROGRESS )) +return ;...
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 13/25 ovpn: store tunnel and transport statistics
In reply to Antonio QuartulliByte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/crypto_aead.c | 2 ++ drivers/net/ovpn/io.c | 10 ++++++++ drivers/net/ovpn/peer.c | 3 +++ drivers/net/ovpn/peer.h | 8 ++++++ drivers/net/ovpn/skb.h | 1 + drivers/net/ovpn/stats.c | 21 +++++++++++++++ drivers/net/ovpn/stats.h | 47 ++++++++++++++++++++++++++++++++++ 8 files changed, 93 insertions(+) create mode 100644 drivers/net/ovpn/stats.c create mode 100644 drivers/net/ovpn/stats.h
drivers/net/ovpn/Makefile +1 -0
@@ -17,4 +17,5 @@ ovpn-y += netlink-gen.o ovpn-y += peer.o ovpn-y += pktid.o ovpn-y += socket.o +ovpn-y += stats.o ovpn-y += udp.o
drivers/net/ovpn/crypto_aead.c +2 -0
@@ -139,6 +139,7 @@ int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,aead_request_set_crypt (req, sg, sg, skb->len - head_size, iv);aead_request_set_ad (req,OVPN_OP_SIZE_V2 +NONCE_WIRE_SIZE ); +ovpn_skb_cb (skb)->ctx->orig_len = skb->len;ovpn_skb_cb (skb)->ctx->peer = peer;ovpn_skb_cb (skb)->ctx->req = req;ovpn_skb_cb (skb)->ctx->ks = ks;
@@ -236,6 +237,7 @@ int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,aead_request_set_ad (req,NONCE_WIRE_SIZE +OVPN_OP_SIZE_V2 ); +ovpn_skb_cb (skb)->ctx->orig_len = skb->len;ovpn_skb_cb (skb)->ctx->payload_offset = payload_offset;ovpn_skb_cb (skb)->ctx->peer = peer;ovpn_skb_cb (skb)->ctx->req = req;
drivers/net/ovpn/io.c +10 -0
@@ -11,6 +11,7 @@#include <linux/skbuff.h> #include <net/gro_cells.h> #include <net/gso.h> +#include <net/ip.h> #include "ovpnstruct.h" #include "peer.h"
@@ -19,6 +20,7 @@#include "crypto_aead.h" #include "netlink.h" #include "proto.h" +#include "socket.h" #include "udp.h" #include "skb.h"
@@ -120,6 +122,11 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* increment RX stats */ +ovpn_peer_stats_increment_rx (&peer->vpn_stats, skb->len); +ovpn_peer_stats_increment_rx (&peer->link_stats, +ovpn_skb_cb (skb)->ctx->orig_len); +kfree (ovpn_skb_cb (skb)->ctx);ovpn_netdev_write (peer, skb);/* skb is passed to upper layer - don't free it */
@@ -169,6 +176,9 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret) goto err;skb_mark_not_on_list (skb); +ovpn_peer_stats_increment_tx (&peer->link_stats, skb->len); +ovpn_peer_stats_increment_tx (&peer->vpn_stats, +ovpn_skb_cb (skb)->ctx->orig_len);kfree (ovpn_skb_cb (skb)->ctx);
drivers/net/ovpn/peer.c +3 -0
@@ -18,6 +18,7 @@#include "main.h" #include "netlink.h" #include "peer.h" +#include "socket.h" /** * ovpn_peer_new - allocate and initialize a new peer object
@@ -47,6 +48,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock);kref_init (&peer->refcount); +ovpn_peer_stats_init (&peer->vpn_stats); +ovpn_peer_stats_init (&peer->link_stats); ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );if (ret <0 ) {
drivers/net/ovpn/peer.h +8 -0
@@ -10,10 +10,14 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ +#include <net/dst_cache.h> +#include <uapi/linux/ovpn.h> +#include "bind.h" #include "pktid.h" #include "crypto.h" #include "socket.h" +#include "stats.h" #include <net/dst_cache.h> #include <uapi/linux/ovpn.h>
@@ -30,6 +34,8 @@* @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding * @halt: true if ovpn_peer_mark_delete was called +* @vpn_stats: per-peer in-VPN TX/RX stays +* @link_stats: per-peer link/transport TX/RX stats * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) * @lock: protects binding to peer (bind) * @refcount: reference counter
@@ -48,6 +54,8 @@ struct ovpn_peer {struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;bool halt; +struct ovpn_peer_stats vpn_stats; +struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind */ struct kref refcount;
drivers/net/ovpn/skb.h +1 -0
@@ -23,6 +23,7 @@ struct ovpn_cb_ctx {struct ovpn_crypto_key_slot *ks;struct aead_request *req;struct scatterlist sg[MAX_SKB_FRAGS +2 ]; +size_t orig_len;unsigned int payload_offset; };
drivers/net/ovpn/stats.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2024 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#include <linux/atomic.h> + +#include "stats.h" + +void ovpn_peer_stats_init (struct ovpn_peer_stats *ps) +{ +atomic64_set (&ps->rx.bytes,0 ); +atomic64_set (&ps->rx.packets,0 ); + +atomic64_set (&ps->tx.bytes,0 ); +atomic64_set (&ps->tx.packets,0 ); +}
drivers/net/ovpn/stats.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 14/25 ovpn: implement TCP transport
In reply to Antonio QuartulliWith this change ovpn is allowed to communicate to peers also via TCP. Parsing of incoming messages is implemented through the strparser API.
--- drivers/net/Kconfig | 1 + drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 4 + drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/peer.h | 37 +++ drivers/net/ovpn/socket.c | 27 +- drivers/net/ovpn/socket.h | 7 +- drivers/net/ovpn/tcp.c | 512 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/tcp.h | 42 ++++ 9 files changed, 630 insertions(+), 4 deletions(-) create mode 100644 drivers/net/ovpn/tcp.c create mode 100644 drivers/net/ovpn/tcp.h
drivers/net/Kconfig +1 -0
@@ -118,6 +118,7 @@ config WIREGUARD_DEBUG config OVPN tristate "OpenVPN data channel offload" depends on NET && INET + select STREAM_PARSER select NET_UDP_TUNNEL select DST_CACHE select CRYPTO
drivers/net/ovpn/Makefile +1 -0
@@ -18,4 +18,5 @@ ovpn-y += peer.o ovpn-y += pktid.o ovpn-y += socket.o ovpn-y += stats.o +ovpn-y += tcp.o ovpn-y += udp.o
drivers/net/ovpn/io.c +4 -0
@@ -21,6 +21,7 @@#include "netlink.h" #include "proto.h" #include "socket.h" +#include "tcp.h" #include "udp.h" #include "skb.h"
@@ -186,6 +187,9 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret)case IPPROTO_UDP :ovpn_udp_send_skb (peer->ovpn, peer, skb);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, skb); +break ;default :/* no transport configured yet */ goto err;
drivers/net/ovpn/main.c +3 -0
@@ -25,6 +25,7 @@#include "io.h" #include "packet.h" #include "peer.h" +#include "tcp.h" /* Driver info */ #define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn)"
@@ -279,6 +280,8 @@ static int __init ovpn_init(void) goto unreg_rtnl; } +ovpn_tcp_init (); +return 0 ; unreg_rtnl:
drivers/net/ovpn/peer.h +37 -0
@@ -11,6 +11,7 @@#define _NET_OVPN_OVPNPEER_H_#include <net/dst_cache.h> +#include <net/strparser.h> #include <uapi/linux/ovpn.h> #include "bind.h"
@@ -30,6 +31,18 @@* @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel * @sock: the socket being used to talk to this peer +* @tcp: keeps track of TCP specific state +* @tcp.strp: stream parser context (TCP only) +* @tcp.tx_work: work for deferring outgoing packet processing (TCP only) +* @tcp.user_queue: received packets that have to go to userspace (TCP only) +* @tcp.tx_in_progress: true if TX is already ongoing (TCP only) +* @tcp.out_msg.skb: packet scheduled for sending (TCP only) +* @tcp.out_msg.offset: offset where next send should start (TCP only) +* @tcp.out_msg.len: remaining data to send within packet (TCP only) +* @tcp.sk_cb.sk_data_ready: pointer to original cb (TCP only) +* @tcp.sk_cb.sk_write_space: pointer to original cb (TCP only) +* @tcp.sk_cb.prot: pointer to original prot object (TCP only) +* @tcp.sk_cb.ops: pointer to the original prot_ops object (TCP only) * @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding
@@ -50,6 +63,30 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket *sock; + +/* state of the TCP reading. Needed to keep track of how much of a +* single packet has already been read from the stream and how much is +* missing +*/ +struct { +struct strparser strp; +struct work_struct tx_work; +struct sk_buff_head user_queue; +bool tx_in_progress; + +struct { +struct sk_buff *skb; +int offset; +int len; + } out_msg; + +struct { +void (*sk_data_ready)(struct sock *sk); +void (*sk_write_space)(struct sock *sk); +struct proto *prot; +const struct proto_ops *ops; + } sk_cb; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;
drivers/net/ovpn/socket.c +24 -3
@@ -15,6 +15,7 @@#include "io.h" #include "peer.h" #include "socket.h" +#include "tcp.h" #include "udp.h" static void ovpn_socket_detach (struct socket *sock)
@@ -24,6 +25,8 @@ static void ovpn_socket_detach(struct socket *sock)if (sock->sk->sk_protocol ==IPPROTO_UDP )ovpn_udp_socket_detach (sock); +else if (sock->sk->sk_protocol ==IPPROTO_TCP ) +ovpn_tcp_socket_detach (sock);sockfd_put (sock); }
@@ -70,6 +73,8 @@ static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)if (sock->sk->sk_protocol ==IPPROTO_UDP ) ret =ovpn_udp_socket_attach (sock, peer->ovpn); +else if (sock->sk->sk_protocol ==IPPROTO_TCP ) + ret =ovpn_tcp_socket_attach (sock, peer);return ret; }
@@ -131,14 +136,30 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) } ovpn_sock =kzalloc (sizeof (*ovpn_sock),GFP_KERNEL ); -if (!ovpn_sock) -return ERR_PTR (-ENOMEM ); +if (!ovpn_sock) { + ret = -ENOMEM ; + goto err; + } - ovpn_sock->ovpn = peer->ovpn; ovpn_sock->sock = sock;kref_init (&ovpn_sock->refcount); +/* TCP sockets are per-peer, therefore they are linked to their unique +* peer +*/ +if (sock->sk->sk_protocol ==IPPROTO_TCP ) { + ovpn_sock->peer = peer; + }else { +/* in UDP we only link the ovpn instance since the socket is +* shared among multiple peers +*/ + ovpn_sock->ovpn = peer->ovpn; + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock);return ovpn_sock; +err: +ovpn_socket_detach (sock); +return ERR_PTR (ret); }
drivers/net/ovpn/socket.h +6 -1
@@ -20,12 +20,17 @@ struct ovpn_peer;/** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) +* @peer: unique peer transmitting over this socket (TCP only) * @sock: the low level sock object * @refcount: amount of contexts currently referencing this object * @rcu: member used to schedule RCU destructor callback */ struct ovpn_socket { -struct ovpn_struct *ovpn; +union { +struct ovpn_struct *ovpn; +struct ovpn_peer *peer; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;
drivers/net/ovpn/tcp.c new file +512 -0
512 lines not on this page · raw message
drivers/net/ovpn/tcp.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 15/25 ovpn: implement multi-peer support
In reply to Antonio QuartulliWith this change an ovpn instance will be able to stay connected to multiple remote endpoints.
This functionality is strictly required when running ovpn on an OpenVPN server.
--- drivers/net/ovpn/main.c | 49 +++++++++- drivers/net/ovpn/ovpnstruct.h | 22 +++++ drivers/net/ovpn/peer.c | 167 +++++++++++++++++++++++++++++++++- drivers/net/ovpn/peer.h | 9 ++ 4 files changed, 244 insertions(+), 3 deletions(-)
drivers/net/ovpn/main.c +47 -2
@@ -54,14 +54,53 @@ static void ovpn_struct_free(struct net_device *net)struct ovpn_struct *ovpn =netdev_priv (net);gro_cells_destroy (&ovpn->gro_cells); +kfree (ovpn->peers);rcu_barrier (); }static int ovpn_net_init (struct net_device *dev) {struct ovpn_struct *ovpn =netdev_priv (dev); +int i, err =gro_cells_init (&ovpn->gro_cells, dev); +struct in_device *dev_v4; -return gro_cells_init (&ovpn->gro_cells, dev); +if (err) +return err; + +if (ovpn->mode ==OVPN_MODE_MP ) { + dev_v4 =__in_dev_get_rtnl (dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly. +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; + } + +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers) +return -ENOMEM ; + +spin_lock_init (&ovpn->peers->lock_by_id); +spin_lock_init (&ovpn->peers->lock_by_vpn_addr); +spin_lock_init (&ovpn->peers->lock_by_transp_addr); + +for (i =0 ; i <ARRAY_SIZE (ovpn->peers->by_id); i++) { +INIT_HLIST_HEAD (&ovpn->peers->by_id[i]); +INIT_HLIST_HEAD (&ovpn->peers->by_vpn_addr[i]); +INIT_HLIST_NULLS_HEAD (&ovpn->peers->by_transp_addr[i], + i); + } + } + +return 0 ; }static int ovpn_net_open (struct net_device *dev)
@@ -210,8 +249,14 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn) ovpn->registered = false; -if (ovpn->mode ==OVPN_MODE_P2P ) +switch (ovpn->mode) { +case OVPN_MODE_P2P :ovpn_peer_release_p2p (ovpn); +break ; +default : +ovpn_peers_free (ovpn); +break ; + } } static int ovpn_netdev_notifier_call(struct notifier_block *nb,
drivers/net/ovpn/ovpnstruct.h +22 -0
@@ -14,6 +14,26 @@#include <net/net_trackers.h> #include <uapi/linux/ovpn.h> +/** +* struct ovpn_peer_collection - container of peers for MultiPeer mode +* @by_id: table of peers index by ID +* @by_vpn_addr: table of peers indexed by VPN IP address +* @by_transp_addr: table of peers indexed by transport address (items can be +* rehashed on the fly due to peer IP change) +* @lock_by_id: protects writes to peers by_id table +* @lock_by_vpn_addr: protects writes to peers by_vpn_addr table +* @lock_by_transp_addr: protects writes to peers by_transp_addr table +*/ +struct ovpn_peer_collection { + DECLARE_HASHTABLE(by_id, 12); + DECLARE_HASHTABLE(by_vpn_addr, 12); +struct hlist_nulls_head by_transp_addr[1 <<12 ]; + +spinlock_t lock_by_id;/* protects writes to by_id */ +spinlock_t lock_by_vpn_addr;/* protects by_vpn_addr */ +spinlock_t lock_by_transp_addr;/* protects by_transp_addr */ +}; +/** * struct ovpn_struct - per ovpn interface state * @dev: the actual netdev representing the tunnel
@@ -21,6 +41,7 @@* @registered: whether dev is still registered with netdev or not * @mode: device operation mode (i.e. p2p, mp, ..) * @lock: protect this object +* @peers: data structures holding multi-peer references * @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list * @gro_cells: pointer to the Generic Receive Offload cell
@@ -31,6 +52,7 @@ struct ovpn_struct {bool registered;enum ovpn_mode mode;spinlock_t lock;/* protect writing to the ovpn_struct object */ +struct ovpn_peer_collection *peers;struct ovpn_peer __rcu *peer;struct list_head dev_list;struct gro_cells gro_cells;
drivers/net/ovpn/peer.c +166 -1
200 lines not on this page · raw message
drivers/net/ovpn/peer.h +9 -0
@@ -30,6 +30,10 @@* @vpn_addrs: IP addresses assigned over the tunnel * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel +* @hash_entry_id: entry in the peer ID hashtable +* @hash_entry_addr4: entry in the peer IPv4 hashtable +* @hash_entry_addr6: entry in the peer IPv6 hashtable +* @hash_entry_transp_addr: entry in the peer transport address hashtable * @sock: the socket being used to talk to this peer * @tcp: keeps track of TCP specific state * @tcp.strp: stream parser context (TCP only)
@@ -62,6 +66,10 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct hlist_node hash_entry_id; +struct hlist_node hash_entry_addr4; +struct hlist_node hash_entry_addr6; +struct hlist_nulls_node hash_entry_transp_addr;struct ovpn_socket *sock;/* state of the TCP reading. Needed to keep track of how much of a
@@ -126,6 +134,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id);int ovpn_peer_add (struct ovpn_struct *ovpn,struct ovpn_peer *peer);int ovpn_peer_del (struct ovpn_peer *peer,enum ovpn_del_peer_reason reason);void ovpn_peer_release_p2p (struct ovpn_struct *ovpn); +void ovpn_peers_free (struct ovpn_struct *ovpn);struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb);
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 64
Quoted 4 lines · Antonio Quartulli
static int
ovpn_net_init(struct net_device *dev)
{drivers/net/ovpn/main.c +1
-0struct ovpn_struct *ovpn =netdev_priv (dev); +int i, err =gro_cells_init (&ovpn->gro_cells, dev);
I'm not a fan of "hiding" the gro_cells_init call up here. I'd prefer if this was done just before the corresponding "if (err)".
Quoted 15 lines
drivers/net/ovpn/main.c +13
-1+struct in_device *dev_v4; -return gro_cells_init (&ovpn->gro_cells, dev); +if (err) +return err; + +if (ovpn->mode ==OVPN_MODE_MP ) { + dev_v4 =__in_dev_get_rtnl (dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly.
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 88
Quoted 10 lines
drivers/net/ovpn/main.c +10
-0+*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; + } + +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers)
missing gro_cells_destroy
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 93
Quoted 5 lines
drivers/net/ovpn/main.c +5
-0+return -ENOMEM ; + +spin_lock_init (&ovpn->peers->lock_by_id); +spin_lock_init (&ovpn->peers->lock_by_vpn_addr); +spin_lock_init (&ovpn->peers->lock_by_transp_addr);
What's the benefit of having 3 separate locks instead of a single lock protecting all the hashtables?
Quoted 48 lines
(quoted diff) +46
-0+ + for (i = 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) { + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]); + INIT_HLIST_HEAD(&ovpn->peers->by_vpn_addr[i]); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i], + i); + } + } + + return 0; } +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) +{ + struct sockaddr_storage sa = { 0 }; + struct hlist_nulls_head *nhead; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + struct hlist_head *head; + struct ovpn_bind *bind; + struct ovpn_peer *tmp; + size_t salen; + + spin_lock_bh(&ovpn->peers->lock_by_id); + /* do not add duplicates */ + tmp = ovpn_peer_get_by_id(ovpn, peer->id); + if (tmp) { + ovpn_peer_put(tmp); + spin_unlock_bh(&ovpn->peers->lock_by_id); + return -EEXIST; + } + + hlist_add_head_rcu(&peer->hash_entry_id, + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); + spin_unlock_bh(&ovpn->peers->lock_by_id); + + bind = rcu_dereference_protected(peer->bind, true); + /* peers connected via TCP have bind == NULL */ + if (bind) { + switch (bind->remote.in4.sin_family) { + case AF_INET: + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family = AF_INET; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen = sizeof(*sa4);
Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + case AF_INET6: + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family = AF_INET6; + sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen = sizeof(*sa6); + break; + default:
And remove from the by_id hashtable? Or is that handled somewhere that I missed (I don't think ovpn_peer_unhash gets called in that case)?
Quoted 3 lines
drivers/net/ovpn/peer.c +3
-0+return -EPROTONOSUPPORT ; + } +
Signature · 1 line
Sabrina
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
Quoted 19 lines · Sabrina Dubroca
Quoted 18 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +18
-0+static int ovpn_peer_add_mp (struct ovpn_struct *ovpn,struct ovpn_peer *peer) +{ +struct sockaddr_storage sa = {0 }; +struct hlist_nulls_head *nhead; +struct sockaddr_in6 *sa6; +struct sockaddr_in *sa4; +struct hlist_head *head; +struct ovpn_bind *bind; +struct ovpn_peer *tmp; +size_t salen; + +spin_lock_bh (&ovpn->peers->lock_by_id); +/* do not add duplicates */ + tmp =ovpn_peer_get_by_id (ovpn, peer->id); +if (tmp) { +ovpn_peer_put (tmp); +spin_unlock_bh (&ovpn->peers->lock_by_id); +return -EEXIST ;
Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 349
Quoted 8 lines
drivers/net/ovpn/peer.c +8
-0+ } + +hlist_add_head_rcu (&peer->hash_entry_id, +ovpn_get_hash_head (ovpn->peers->by_id, &peer->id, +sizeof (peer->id))); +spin_unlock_bh (&ovpn->peers->lock_by_id); + + bind =rcu_dereference_protected (peer->bind, true);
What protects us here? We just released lock_by_id and we're not holding peer->lock.
Quoted 15 lines
Quoted 15 lines
drivers/net/ovpn/peer.c +15
-0+/* peers connected via TCP have bind == NULL */ +if (bind) { +switch (bind->remote.in4.sin_family) { +case AF_INET : + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family =AF_INET ; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen =sizeof (*sa4); +break ; +case AF_INET6 : + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family =AF_INET6 ;
Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 369
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6); +break ; +default :And remove from the
by_idhashtable? Or is that handled somewhere that I missed (I don't thinkovpn_peer_unhashgets called in that case)?
ovpn_nl_set_peer_doit does:
ret = ovpn_peer_add(ovpn, peer);
if (ret < 0) {
[...]
/* release right away because peer is not really used in any
* context
*/
ovpn_peer_release(peer);
kfree(peer);But if we fail at this stage, the peer was published in the by_id hashtable and could be used.
Although AFAICT, ovpn can never create a bind with family != AF_INET{,6}, so this is not a real issue -- in that case I guess a DEBUG_NET_WARN_ON_ONCE with a comment that this should never happen would be acceptable (but I'd still remove the peer from by_id and go through the proper release path instead of direct kfree in ovpn_nl_set_peer_doit). Otherwise, you'd have to reorder things in this function so that all failures are handled before the peer is added to any hashtable.
Quoted 3 lines
Quoted 3 lines
drivers/net/ovpn/peer.c +3
-0+return -EPROTONOSUPPORT ; + } +
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
Quoted 23 lines · Sabrina Dubroca
Quoted 22 lines · Sabrina Dubroca
Quoted 21 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +21
-0+static int ovpn_peer_add_mp (struct ovpn_struct *ovpn,struct ovpn_peer *peer) +{ +struct sockaddr_storage sa = {0 }; +struct hlist_nulls_head *nhead; +struct sockaddr_in6 *sa6; +struct sockaddr_in *sa4; +struct hlist_head *head; +struct ovpn_bind *bind; +struct ovpn_peer *tmp; +size_t salen; + +spin_lock_bh (&ovpn->peers->lock_by_id); +/* do not add duplicates */ + tmp =ovpn_peer_get_by_id (ovpn, peer->id); +if (tmp) { +ovpn_peer_put (tmp); +spin_unlock_bh (&ovpn->peers->lock_by_id); +return -EEXIST ; + } + + hlist_add_head_rcu(&peer->hash_entry_id,
Quoted 8 lines
Quoted 5 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 349
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ovpn_get_hash_head (ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); +spin_unlock_bh (&ovpn->peers->lock_by_id); + + bind =rcu_dereference_protected (peer->bind, true);What protects us here? We just released
lock_by_idand we're not holdingpeer->lock.
hmm. I think originally it was not possible to hold this peer in any other context since the peer was stil being added. But now we have added it to the by_id table already, so we cannot assume that anymore.
Maybe I should simply move this assignment before the hlist_add_head_rcu() to regain that assumption..
Quoted 40 lines
Quoted 16 lines
Quoted 16 lines
drivers/net/ovpn/peer.c +15
-0+/* peers connected via TCP have bind == NULL */ +if (bind) { +switch (bind->remote.in4.sin_family) { +case AF_INET : + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family =AF_INET ; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen =sizeof (*sa4); +break ; +case AF_INET6 : + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family =AF_INET6 ;Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 369
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6); +break ; +default :And remove from the
by_idhashtable? Or is that handled somewhere that I missed (I don't thinkovpn_peer_unhashgets called in that case)?
ovpn_nl_set_peer_doitdoes:ret = ovpn_peer_add(ovpn, peer); if (ret < 0) { [...] /* release right away because peer is not really used in any * context */ ovpn_peer_release(peer); kfree(peer);But if we fail at this stage, the peer was published in the
by_idhashtable and could be used.
Quoted 8 lines
Although AFAICT, ovpn can never create a bind with family != AF_INET{,6}, so this is not a real issue -- in that case I guess a
DEBUG_NET_WARN_ON_ONCEwith a comment that this should never happen would be acceptable (but I'd still remove the peer fromby_idand go through the proper release path instead of direct kfree inovpn_nl_set_peer_doit). Otherwise, you'd have to reorder things in this function so that all failures are handled before the peer is added to any hashtable.
To be honest I don't mind adding a pre-check and error out immediately. I don't like adding a peer to the table that is actually failing basic sanity checks.
Thanks!
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
drivers/net/ovpn/peer.c +3
-0+return -EPROTONOSUPPORT ; + } +
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
In reply to Sabrina DubrocaQuoted 8 lines · Sabrina Dubroca
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 64
Quoted 4 lines · Antonio Quartulli
static int ovpn_net_init(struct net_device *dev) { struct ovpn_struct *ovpn = netdev_priv(dev);drivers/net/ovpn/main.c +1
-0+int i, err =gro_cells_init (&ovpn->gro_cells, dev);I'm not a fan of "hiding" the
gro_cells_initcall up here. I'd prefer if this was done just before the corresponding "if (err)".
I am all with you, but I remember in the past something complaining about "variable declared and then re-assigned right after".
But maybe this is not the case anymore.
Will move the initialization down.
Quoted 20 lines
Quoted 20 lines
drivers/net/ovpn/main.c +17
-1+struct in_device *dev_v4; -return gro_cells_init (&ovpn->gro_cells, dev); +if (err) +return err; + +if (ovpn->mode ==OVPN_MODE_MP ) { + dev_v4 =__in_dev_get_rtnl (dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly. +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; + }
Quoted 8 lines
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 88
Quoted 6 lines
drivers/net/ovpn/main.c +6
-0+ +/* the peer container is fairly large, therefore we dynamically +* allocate it only when needed +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers)missing
gro_cells_destroy
ACK
Quoted 9 lines
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 93
Quoted 6 lines
drivers/net/ovpn/main.c +5
-0+return -ENOMEM ; + +spin_lock_init (&ovpn->peers->lock_by_id); +spin_lock_init (&ovpn->peers->lock_by_vpn_addr); +spin_lock_init (&ovpn->peers->lock_by_transp_addr);What's the benefit of having 3 separate locks instead of a single lock protecting all the hashtables?
The main reason was to avoid a deadlock - I thought I had added a comment about it...
The problem was a deadlock between acquiring peer->lock and ovpn->peers->lock in float() and in then opposite sequence in peers_free(). (IIRC this happens due to ovpn_peer_reset_sockaddr() acquiring peer->lock)
Splitting the larger peers->lock allowed me to avoid this scenario, because I don't need to jump through any hoop to coordinate access to different hashtables.
Quoted 54 lines
Quoted 54 lines
(quoted diff) +51
-0+ + for (i = 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) { + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]); + INIT_HLIST_HEAD(&ovpn->peers->by_vpn_addr[i]); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i], + i); + } + } + + return 0; } +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) +{ + struct sockaddr_storage sa = { 0 }; + struct hlist_nulls_head *nhead; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + struct hlist_head *head; + struct ovpn_bind *bind; + struct ovpn_peer *tmp; + size_t salen; + + spin_lock_bh(&ovpn->peers->lock_by_id); + /* do not add duplicates */ + tmp = ovpn_peer_get_by_id(ovpn, peer->id); + if (tmp) { + ovpn_peer_put(tmp); + spin_unlock_bh(&ovpn->peers->lock_by_id); + return -EEXIST; + } + + hlist_add_head_rcu(&peer->hash_entry_id, + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); + spin_unlock_bh(&ovpn->peers->lock_by_id); + + bind = rcu_dereference_protected(peer->bind, true); + /* peers connected via TCP have bind == NULL */ + if (bind) { + switch (bind->remote.in4.sin_family) { + case AF_INET: + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family = AF_INET; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen = sizeof(*sa4); + break; + case AF_INET6: + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family = AF_INET6;
Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 369
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6); +break ; +default :And remove from the
by_idhashtable? Or is that handled somewhere that I missed (I don't thinkovpn_peer_unhashgets called in that case)?
No we don't call unhash in this case as we assume the adding just failed entirely.
I will add the removal before returning the error (moving the add below the switch would extend the locked area too much.)
Quoted 5 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 372
Quoted 4 lines
drivers/net/ovpn/peer.c +3
-0+return -EPROTONOSUPPORT ; + } +
Thanks a lot
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
Quoted 10 lines · Antonio Quartulli
Quoted 8 lines · Sabrina Dubroca
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 64
Quoted 4 lines · Antonio Quartulli
static int ovpn_net_init(struct net_device *dev) { struct ovpn_struct *ovpn = netdev_priv(dev);drivers/net/ovpn/main.c +1
-0+int i, err =gro_cells_init (&ovpn->gro_cells, dev);I'm not a fan of "hiding" the
gro_cells_initcall up here. I'd prefer if this was done just before the corresponding "if (err)".
Quoted 4 lines
I am all with you, but I remember in the past something complaining about "variable declared and then re-assigned right after".
But maybe this is not the case anymore.
If you had something like:
int err; err = -EINVAL;
sure, it would make sense to combine them.
Quoted 2 lines
Will move the initialization down.
Thanks.
Quoted 10 lines
Quoted 7 lines
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 93
Quoted 4 lines
drivers/net/ovpn/main.c +4
-0+ +spin_lock_init (&ovpn->peers->lock_by_id); +spin_lock_init (&ovpn->peers->lock_by_vpn_addr); +spin_lock_init (&ovpn->peers->lock_by_transp_addr);What's the benefit of having 3 separate locks instead of a single lock protecting all the hashtables?
The main reason was to avoid a deadlock - I thought I had added a comment about it...
Ok.
I could have missed it, I'm not looking at the comments much now that I'm familiar with the code.
Quoted 3 lines
The problem was a deadlock between acquiring
peer->lockandovpn->peers->lockinfloat()and in then opposite sequence inpeers_free(). (IIRC this happens due toovpn_peer_reset_sockaddr()acquiringpeer->lock)
I don't see a problem with ovpn_peer_reset_sockaddr, but ovpn_peer_put can be called with lock_by_id held and then take peer->lock (in ovpn_peer_release), which would be the opposite order to ovpn_peer_float if the locks were merged (peer->lock then lock_by_transp_addr).
This should be solvable with a single lock by delaying the bind cleanup via call_rcu instead of doing it immediately with ovpn_peer_release (after that delay, nothing should be using peer->bind anymore, since we have no reference and no more rcu_read_lock sections that could have found peer, so we can free immediately and no need to take peer->lock). And it's I think a bit more "correct" wrt RCU rules, since at ovpn_peer_put time, even with refcount=0, we could have a reader still using the peer and deciding to update its bind (not the case with how ovpn_peer_float is called, since we have a reference on the peer).
(This could be completely wrong and/or make no sense at all :))
But I'm not going to insist on this, you can keep the separate locks.
Quoted 67 lines
Splitting the larger
peers->lockallowed me to avoid this scenario, because I don't need to jump through any hoop to coordinate access to different hashtables.Quoted 54 lines
Quoted 54 lines
(quoted diff) +51
-0+ + for (i = 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) { + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]); + INIT_HLIST_HEAD(&ovpn->peers->by_vpn_addr[i]); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i], + i); + } + } + + return 0; } +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) +{ + struct sockaddr_storage sa = { 0 }; + struct hlist_nulls_head *nhead; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + struct hlist_head *head; + struct ovpn_bind *bind; + struct ovpn_peer *tmp; + size_t salen; + + spin_lock_bh(&ovpn->peers->lock_by_id); + /* do not add duplicates */ + tmp = ovpn_peer_get_by_id(ovpn, peer->id); + if (tmp) { + ovpn_peer_put(tmp); + spin_unlock_bh(&ovpn->peers->lock_by_id); + return -EEXIST; + } + + hlist_add_head_rcu(&peer->hash_entry_id, + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); + spin_unlock_bh(&ovpn->peers->lock_by_id); + + bind = rcu_dereference_protected(peer->bind, true); + /* peers connected via TCP have bind == NULL */ + if (bind) { + switch (bind->remote.in4.sin_family) { + case AF_INET: + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family = AF_INET; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen = sizeof(*sa4); + break; + case AF_INET6: + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family = AF_INET6;Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 369
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6); +break ; +default :And remove from the
by_idhashtable? Or is that handled somewhere that I missed (I don't thinkovpn_peer_unhashgets called in that case)?
Quoted 5 lines
No we don't call unhash in this case as we assume the adding just failed entirely.
I will add the removal before returning the error (moving the add below the switch would extend the locked area too much.)
I don't think setting a few variables would be too much to do under the lock (and it would address the issues in my 2nd reply to this patch).
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
Quoted 26 lines · Sabrina Dubroca
Quoted 10 lines
Quoted 7 lines
drivers/net/ovpn/main.c static void ovpn_struct_free(struct net_device *net) · line 93
Quoted 4 lines
drivers/net/ovpn/main.c +4
-0+ +spin_lock_init (&ovpn->peers->lock_by_id); +spin_lock_init (&ovpn->peers->lock_by_vpn_addr); +spin_lock_init (&ovpn->peers->lock_by_transp_addr);What's the benefit of having 3 separate locks instead of a single lock protecting all the hashtables?
The main reason was to avoid a deadlock - I thought I had added a comment about it...
Ok.
I could have missed it, I'm not looking at the comments much now that I'm familiar with the code.
Quoted 3 lines
The problem was a deadlock between acquiring
peer->lockandovpn->peers->lockinfloat()and in then opposite sequence inpeers_free(). (IIRC this happens due toovpn_peer_reset_sockaddr()acquiringpeer->lock)I don't see a problem with
ovpn_peer_reset_sockaddr, butovpn_peer_putcan be called withlock_by_idheld and then takepeer->lock(inovpn_peer_release), which would be the opposite order toovpn_peer_floatif the locks were merged (peer->lockthenlock_by_transp_addr).
Quoted 10 lines
This should be solvable with a single lock by delaying the bind cleanup via
call_rcuinstead of doing it immediately withovpn_peer_release(after that delay, nothing should be usingpeer->bindanymore, since we have no reference and no morercu_read_locksections that could have found peer, so we can free immediately and no need to takepeer->lock). And it's I think a bit more "correct" wrt RCU rules, since atovpn_peer_puttime, even with refcount=0, we could have a reader still using the peer and deciding to update its bind (not the case with howovpn_peer_floatis called, since we have a reference on the peer).
Yap, I totally agree with your analysis.
In a previous version we simplified the code to the point that call_rcu was not needed anymore and we could just rely on kfree_rcu for peer.
Now this "going back to call_rcu" felt a bit wrong, so I tried hard to avoid that. But I agree that actually this is a clear case where a two-steps release is the right thing to do.
Will try to get it fixed as per your suggestion. Thanks!
Quoted 76 lines
(This could be completely wrong and/or make no sense at all :))
But I'm not going to insist on this, you can keep the separate locks.
Quoted 70 lines
Splitting the larger
peers->lockallowed me to avoid this scenario, because I don't need to jump through any hoop to coordinate access to different hashtables.Quoted 54 lines
Quoted 54 lines
(quoted diff) +51
-0+ + for (i = 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) { + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]); + INIT_HLIST_HEAD(&ovpn->peers->by_vpn_addr[i]); + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i], + i); + } + } + + return 0; } +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer) +{ + struct sockaddr_storage sa = { 0 }; + struct hlist_nulls_head *nhead; + struct sockaddr_in6 *sa6; + struct sockaddr_in *sa4; + struct hlist_head *head; + struct ovpn_bind *bind; + struct ovpn_peer *tmp; + size_t salen; + + spin_lock_bh(&ovpn->peers->lock_by_id); + /* do not add duplicates */ + tmp = ovpn_peer_get_by_id(ovpn, peer->id); + if (tmp) { + ovpn_peer_put(tmp); + spin_unlock_bh(&ovpn->peers->lock_by_id); + return -EEXIST; + } + + hlist_add_head_rcu(&peer->hash_entry_id, + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, + sizeof(peer->id))); + spin_unlock_bh(&ovpn->peers->lock_by_id); + + bind = rcu_dereference_protected(peer->bind, true); + /* peers connected via TCP have bind == NULL */ + if (bind) { + switch (bind->remote.in4.sin_family) { + case AF_INET: + sa4 = (struct sockaddr_in *)&sa; + + sa4->sin_family = AF_INET; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen = sizeof(*sa4); + break; + case AF_INET6: + sa6 = (struct sockaddr_in6 *)&sa; + + sa6->sin6_family = AF_INET6;Quoted 8 lines
drivers/net/ovpn/peer.c bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, · line 369
Quoted 5 lines
drivers/net/ovpn/peer.c +5
-0+ sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6); +break ; +default :And remove from the
by_idhashtable? Or is that handled somewhere that I missed (I don't thinkovpn_peer_unhashgets called in that case)?No we don't call unhash in this case as we assume the adding just failed entirely.
Quoted 6 lines
Quoted 2 lines
I will add the removal before returning the error (moving the add below the switch would extend the locked area too much.)
I don't think setting a few variables would be too much to do under the lock (and it would address the issues in my 2nd reply to this patch).
Right - pretty much what I replied to that comment (will move the add after the bind dereference)
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 16/25 ovpn: implement peer lookup logic
In reply to Antonio QuartulliIn a multi-peer scenario there are a number of situations when a specific peer needs to be looked up.
We may want to lookup a peer by:
1. its ID
2. its VPN destination IP
3. its transport IP/port couple
For each of the above, there is a specific routing table referencing all peers for fast look up.
Case 2. is a bit special in the sense that an outgoing packet may not be sent to the peer VPN IP directly, but rather to a network behind it. For this reason we first perform a nexthop lookup in the system routing table and then we use the retrieved nexthop as peer search key.
--- drivers/net/ovpn/peer.c | 270 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 262 insertions(+), 8 deletions(-)
drivers/net/ovpn/peer.c +262 -8
@@ -10,6 +10,7 @@#include <linux/skbuff.h> #include <linux/list.h> #include <linux/hashtable.h> +#include <net/ip6_route.h> #include "ovpnstruct.h" #include "bind.h"
@@ -126,6 +127,92 @@ static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *skb,return true; } +/** +* ovpn_nexthop_from_skb4 - retrieve IPv4 nexthop for outgoing skb +* @skb: the outgoing packet +* +* Return: the IPv4 of the nexthop +*/ +static __be32 ovpn_nexthop_from_skb4 (struct sk_buff *skb) +{ +const struct rtable *rt =skb_rtable (skb); + +if (rt && rt->rt_uses_gateway) +return rt->rt_gw4; + +return ip_hdr (skb)->daddr; +} + +/** +* ovpn_nexthop_from_skb6 - retrieve IPv6 nexthop for outgoing skb +* @skb: the outgoing packet +* +* Return: the IPv6 of the nexthop +*/ +static struct in6_addr ovpn_nexthop_from_skb6 (struct sk_buff *skb) +{ +const struct rt6_info *rt =skb_rt6_info (skb); + +if (!rt || !(rt->rt6i_flags &RTF_GATEWAY )) +return ipv6_hdr (skb)->daddr; + +return rt->rt6i_gateway; +} + +#define ovpn_get_hash_head (_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl = &(_tbl); \ + (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ + +/** +* ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address +* @ovpn: the openvpn instance to search +* @addr: VPN IPv4 to use as search key +* +* Refcounter is not increased for the returned peer. +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4 (struct ovpn_struct *ovpn, +__be32 addr) +{ +struct hlist_head *head; +struct ovpn_peer *tmp; + + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, &addr, +sizeof (addr)); + +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr4) +if (addr == tmp->vpn_addrs.ipv4.s_addr) +return tmp; + +return NULL ; +} + +/** +* ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address +* @ovpn: the openvpn instance to search +* @addr: VPN IPv6 to use as search key +* +* Refcounter is not increased for the returned peer. +* +* Return: the peer if found or NULL otherwise +*/ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6 (struct ovpn_struct *ovpn, +struct in6_addr *addr) +{ +struct hlist_head *head; +struct ovpn_peer *tmp; + + head =ovpn_get_hash_head (ovpn->peers->by_vpn_addr, addr, +sizeof (*addr)); + +hlist_for_each_entry_rcu (tmp, head, hash_entry_addr6) +if (ipv6_addr_equal (addr, &tmp->vpn_addrs.ipv6)) +return tmp; + +return NULL ; +} +/** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from
@@ -203,14 +290,44 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *ovpn,struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_struct *ovpn,struct sk_buff *skb) { -struct ovpn_peer *peer =NULL ; +struct ovpn_peer *tmp, *peer =NULL ;struct sockaddr_storage ss = {0 }; +struct hlist_nulls_head *nhead; +struct hlist_nulls_node *ntmp; +size_t sa_len;if (unlikely (!ovpn_peer_skb_to_sockaddr (skb, &ss)))return NULL ;if (ovpn->mode ==OVPN_MODE_P2P ) - peer =ovpn_peer_get_by_transp_addr_p2p (ovpn, &ss); +return ovpn_peer_get_by_transp_addr_p2p (ovpn, &ss); + +switch (ss.ss_family) { +case AF_INET : + sa_len =sizeof (struct sockaddr_in ); +break ; +case AF_INET6 : + sa_len =sizeof (struct sockaddr_in6 ); +break ; +default : +return NULL ; + } + + nhead =ovpn_get_hash_head (ovpn->peers->by_transp_addr, &ss, sa_len); + +rcu_read_lock (); +hlist_nulls_for_each_entry_rcu (tmp, ntmp, nhead, + hash_entry_transp_addr) { +if (!ovpn_peer_transp_match (tmp, &ss)) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -245,10 +362,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_struct *ovpn,*/ struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_struct *ovpn,u32 peer_id) { -struct ovpn_peer *peer =NULL ; +struct ovpn_peer *tmp, *peer =NULL ; +struct hlist_head *head;if (ovpn->mode ==OVPN_MODE_P2P ) - peer =ovpn_peer_get_by_id_p2p (ovpn, peer_id); +return ovpn_peer_get_by_id_p2p (ovpn, peer_id); + + head =ovpn_get_hash_head (ovpn->peers->by_id, &peer_id, +sizeof (peer_id)); + +rcu_read_lock (); +hlist_for_each_entry_rcu (tmp, head, hash_entry_id) { +if (tmp->id != peer_id) +continue ; + +if (!ovpn_peer_hold (tmp)) +continue ; + + peer = tmp; +break ; + } +rcu_read_unlock ();return peer; }
@@ -270,6 +404,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn, struct sk_buff *skb) {struct ovpn_peer *peer =NULL ; +struct in6_addr addr6; +__be32 addr4;/* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side
@@ -280,11 +416,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,if (unlikely (peer && !ovpn_peer_hold (peer))) peer =NULL ;rcu_read_unlock (); +return peer; } +rcu_read_lock (); +switch (skb_protocol_to_family (skb)) { +case AF_INET : + addr4 =ovpn_nexthop_from_skb4 (skb); + peer =ovpn_peer_get_by_vpn_addr4 (ovpn, addr4); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_skb6 (skb); + peer =ovpn_peer_get_by_vpn_addr6 (ovpn, &addr6); +break ; + } + +if (unlikely (peer && !ovpn_peer_hold (peer))) + peer =NULL ; +rcu_read_unlock (); +return peer; } +/** +* ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dest: the destination to be looked up +* +* Looks up in the IPv4 system routing table the IP of the nexthop to be used +* to reach the destination passed as argument. If no nexthop can be found, the +* destination itself is returned as it probably has to be used as nexthop. +* +* Return: the IP of the next hop if found or dest itself otherwise +*/ +static __be32 ovpn_nexthop_from_rt4 (struct ovpn_struct *ovpn,__be32 dest) +{ +struct rtable *rt; +struct flowi4 fl = { + .daddr = dest + }; + + rt =ip_route_output_flow (dev_net (ovpn->dev), &fl,NULL ); +if (IS_ERR (rt)) { +net_dbg_ratelimited ("%s: no route to host %pI4\n" , __func__, + &dest); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dest; + } + +if (!rt->rt_uses_gateway) + goto out; + + dest = rt->rt_gw4; +out: +ip_rt_put (rt); +return dest; +} + +/** +* ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destination +* @ovpn: the private data representing the current VPN session +* @dest: the destination to be looked up +* +* Looks up in the IPv6 system routing table the IP of the nexthop to be used +* to reach the destination passed as argument. If no nexthop can be found, the +* destination itself is returned as it probably has to be used as nexthop. +* +* Return: the IP of the next hop if found or dest itself otherwise +*/ +static struct in6_addr ovpn_nexthop_from_rt6 (struct ovpn_struct *ovpn, +struct in6_addr dest) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +struct dst_entry *entry; +struct rt6_info *rt; +struct flowi6 fl = { + .daddr = dest, + }; + + entry = ipv6_stub->ipv6_dst_lookup_flow (dev_net (ovpn->dev),NULL , &fl, +NULL ); +if (IS_ERR (entry)) { +net_dbg_ratelimited ("%s: no route to host %pI6c\n" , __func__, + &dest); +/* if we end up here this packet is probably going to be +* thrown away later +*/ +return dest; + } + + rt =dst_rt6_info (entry); + +if (!(rt->rt6i_flags &RTF_GATEWAY )) + goto out; + + dest = rt->rt6i_gateway; +out: +dst_release ((struct dst_entry *)rt); +#endif +return dest; +} +/** * ovpn_peer_check_by_src - check that skb source is routed via peer * @ovpn: the openvpn instance to search
@@ -297,6 +531,8 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer) {bool match = false; +struct in6_addr addr6; +__be32 addr4;if (ovpn->mode ==OVPN_MODE_P2P ) {/* in P2P mode, no matter the destination, packets are always
@@ -305,15 +541,33 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb,rcu_read_lock (); match = (peer ==rcu_dereference (ovpn->peer));rcu_read_unlock (); +return match; + } + +/* This function performs a reverse path check, therefore we now +* lookup the nexthop we would use if we wanted to route a packet +* to the source IP. If the nexthop matches the sender we know the +* latter is valid and we allow the packet to come in +*/ + +switch (skb_protocol_to_family (skb)) { +case AF_INET : + addr4 =ovpn_nexthop_from_rt4 (ovpn,ip_hdr (skb)->saddr); +rcu_read_lock (); + match = (peer ==ovpn_peer_get_by_vpn_addr4 (ovpn, addr4)); +rcu_read_unlock (); +break ; +case AF_INET6 : + addr6 =ovpn_nexthop_from_rt6 (ovpn,ipv6_hdr (skb)->saddr); +rcu_read_lock (); + match = (peer ==ovpn_peer_get_by_vpn_addr6 (ovpn, &addr6)); +rcu_read_unlock (); +break ; }return match; } -#define ovpn_get_hash_head (_tbl, _key, _key_len) ({ \ - typeof(_tbl) *__tbl = &(_tbl); \ - (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ -/** * ovpn_peer_add_mp - add peer to related tables in a MP instance * @ovpn: the instance to add the peer to
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 17/25 ovpn: implement keepalive mechanism
In reply to Antonio QuartulliOpenVPN supports configuring a periodic keepalive packet. message to allow the remote endpoint detect link failures.
This change implements the keepalive sending and timer expiring logic.
--- drivers/net/ovpn/io.c | 77 ++++++++++++++ drivers/net/ovpn/io.h | 5 + drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/ovpnstruct.h | 2 + drivers/net/ovpn/peer.c | 186 ++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 15 +++ drivers/net/ovpn/proto.h | 2 - 7 files changed, 288 insertions(+), 2 deletions(-)
drivers/net/ovpn/io.c +77 -0
@@ -25,6 +25,33 @@#include "udp.h" #include "skb.h" +const unsigned char ovpn_keepalive_message[OVPN_KEEPALIVE_SIZE ] = { +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_message[0 ]) +return false; + +if (skb->len !=OVPN_KEEPALIVE_SIZE ) +return false; + +if (!pskb_may_pull (skb,OVPN_KEEPALIVE_SIZE )) +return false; + +return !memcmp (skb->data, ovpn_keepalive_message,OVPN_KEEPALIVE_SIZE ); +} +/* Called after decrypt to write the IP packet to the device. * This method is expected to manage/free the skb. */
@@ -88,6 +115,9 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +/* keep track of last received authenticated packet for keepalive */ + peer->last_recv =ktime_get_real_seconds (); +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->ctx->payload_offset);
@@ -104,6 +134,12 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret) goto drop; } +if (ovpn_is_keepalive (skb)) { +net_dbg_ratelimited ("%s: ping received from peer %u\n" , + peer->ovpn->dev->name, peer->id); + goto drop; + } +net_info_ratelimited ("%s: unsupported protocol received from peer %u\n" , peer->ovpn->dev->name, peer->id); goto drop;
@@ -194,6 +230,10 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret)/* no transport configured yet */ goto err; } + +/* keep track of last sent packet for keepalive */ + peer->last_sent =ktime_get_real_seconds (); +/* skb passed down the stack - don't free it */ skb =NULL ; err:
@@ -338,3 +378,40 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)kfree_skb_list (skb);return NET_XMIT_DROP ; } + +/** +* ovpn_xmit_special - encrypt and transmit an out-of-band message to peer +* @peer: peer to send the message to +* @data: message content +* @len: message length +* +* Assumes that caller holds a reference to peer +*/ +void ovpn_xmit_special (struct ovpn_peer *peer,const void *data, +const unsigned int len) +{ +struct ovpn_struct *ovpn; +struct sk_buff *skb; + + ovpn = 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 ; +__skb_put_data (skb, data, len); + +/* increase reference counter when passing peer to sending queue */ +if (!ovpn_peer_hold (peer)) { +netdev_dbg (ovpn->dev,"%s: cannot hold peer reference for sending special packet\n" , + __func__); +kfree_skb (skb); +return ; + } + +ovpn_send (ovpn, skb, peer); +}
drivers/net/ovpn/io.h +5 -0
@@ -10,9 +10,14 @@#ifndef _NET_OVPN_OVPN_H_#define _NET_OVPN_OVPN_H_ +#define OVPN_KEEPALIVE_SIZE 16 +extern const unsigned char ovpn_keepalive_message[OVPN_KEEPALIVE_SIZE ]; +netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev);void ovpn_recv (struct ovpn_peer *peer,struct sk_buff *skb); +void ovpn_xmit_special (struct ovpn_peer *peer,const void *data, +const unsigned int len);void ovpn_encrypt_post (struct sk_buff *skb,int ret);void ovpn_decrypt_post (struct sk_buff *skb,int ret);
drivers/net/ovpn/main.c +3 -0
@@ -45,6 +45,7 @@ static int ovpn_struct_init(struct net_device *dev, enum ovpn_mode mode) ovpn->dev = dev; ovpn->mode = mode;spin_lock_init (&ovpn->lock); +INIT_DELAYED_WORK (&ovpn->keepalive_work, ovpn_peer_keepalive_work);return 0 ; }
@@ -249,6 +250,8 @@ void ovpn_iface_destruct(struct ovpn_struct *ovpn) ovpn->registered = false; +cancel_delayed_work_sync (&ovpn->keepalive_work); +switch (ovpn->mode) {case OVPN_MODE_P2P :ovpn_peer_release_p2p (ovpn);
drivers/net/ovpn/ovpnstruct.h +2 -0
@@ -45,6 +45,7 @@ struct ovpn_peer_collection {* @peer: in P2P mode, this is the only remote peer * @dev_list: entry for the module wide device list * @gro_cells: pointer to the Generic Receive Offload cell +* @keepalive_work: struct used to schedule keepalive periodic job */ struct ovpn_struct {struct net_device *dev;
@@ -56,6 +57,7 @@ struct ovpn_struct {struct ovpn_peer __rcu *peer;struct list_head dev_list;struct gro_cells gro_cells; + struct delayed_work keepalive_work; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/peer.c +186 -0
207 lines not on this page · raw message
drivers/net/ovpn/peer.h +15 -0
@@ -50,6 +50,12 @@* @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding +* @keepalive_interval: seconds after which a new keepalive should be sent +* @keepalive_xmit_exp: future timestamp when next keepalive should be sent +* @last_sent: timestamp of the last successfully sent packet +* @keepalive_timeout: seconds after which an inactive peer is considered dead +* @keepalive_recv_exp: future timestamp when the peer should expire +* @last_recv: timestamp of the last authenticated received packet * @halt: true if ovpn_peer_mark_delete was called * @vpn_stats: per-peer in-VPN TX/RX stays * @link_stats: per-peer link/transport TX/RX stats
@@ -98,6 +104,12 @@ struct ovpn_peer {struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind; +unsigned long keepalive_interval; +unsigned long keepalive_xmit_exp; +time64_t last_sent; +unsigned long keepalive_timeout; +unsigned long keepalive_recv_exp; +time64_t last_recv;bool halt;struct ovpn_peer_stats vpn_stats;struct ovpn_peer_stats link_stats;
@@ -144,4 +156,7 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_struct *ovpn,bool ovpn_peer_check_by_src (struct ovpn_struct *ovpn,struct sk_buff *skb,struct ovpn_peer *peer); +void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout); +void ovpn_peer_keepalive_work (struct work_struct *work); +#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/proto.h +0 -2
@@ -35,8 +35,6 @@#define OVPN_OP_SIZE_V2 4#define OVPN_PEER_ID_MASK 0x00FFFFFF#define OVPN_PEER_ID_UNDEF 0x00FFFFFF -/* first byte of keepalive message */ -#define OVPN_KEEPALIVE_FIRST_BYTE 0x2a/* first byte of exit message */ #define OVPN_EXPLICIT_EXIT_NOTIFY_FIRST_BYTE 0x28
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 17/25] ovpn: implement keepalive mechanism
Quoted 6 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +6
-0+static time64_t ovpn_peer_keepalive_work_mp (struct ovpn_struct *ovpn, +time64_t now) +{ +time64_t tmp_next_run, next_run =0 ; +struct hlist_node *tmp; +struct ovpn_peer *peer;
drivers/net/ovpn/peer.c void ovpn_peers_free(struct ovpn_struct *ovpn) · line 966
Quoted 10 lines
drivers/net/ovpn/peer.c +10
-0+int bkt; + +spin_lock_bh (&ovpn->peers->lock_by_id); +hash_for_each_safe (ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) { + tmp_next_run =ovpn_peer_keepalive_work_single (peer, now); + +/* the next worker run will be scheduled based on the shortest +* required interval across all peers +*/ +if (!next_run || tmp_next_run < next_run)
I think this should exclude tmp_next_run == 0.
If we have two peers, with the first getting a non-0 value and the 2nd getting 0, we'll end up with next_run = 0 on return.
If we have three peers and ovpn_peer_keepalive_work_single returns 12,0,42, we'll end up with 42 (after resetting to 0 on the 2nd peer), and we could miss sending the needed keepalive for peer 1.
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+ next_run = tmp_next_run; + } +spin_unlock_bh (&ovpn->peers->lock_by_id); + +return next_run; +}
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 17/25] ovpn: implement keepalive mechanism
Hi,
Quoted 11 lines · Sabrina Dubroca
Quoted 10 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +10
-0+static time64_t ovpn_peer_keepalive_work_mp (struct ovpn_struct *ovpn, +time64_t now) +{ +time64_t tmp_next_run, next_run =0 ; +struct hlist_node *tmp; +struct ovpn_peer *peer; +int bkt; + +spin_lock_bh (&ovpn->peers->lock_by_id); +hash_for_each_safe (ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) {
Quoted 8 lines
drivers/net/ovpn/peer.c void ovpn_peers_free(struct ovpn_struct *ovpn) · line 966
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+ tmp_next_run =ovpn_peer_keepalive_work_single (peer, now); + +/* the next worker run will be scheduled based on the shortest +* required interval across all peers +*/ +if (!next_run || tmp_next_run < next_run)I think this should exclude
tmp_next_run== 0.
or, for better clarity of the flow, I will add:
if (!tmp_next_run)
continue;
since 0 explicitly means "keepalive disabled" or "no keepalive needed for $reasons".
Quoted 8 lines
If we have two peers, with the first getting a non-0 value and the 2nd getting 0, we'll end up with
next_run= 0 on return.If we have three peers and
ovpn_peer_keepalive_work_singlereturns 12,0,42, we'll end up with 42 (after resetting to 0 on the 2nd peer), and we could miss sending the needed keepalive for peer 1.
Absolutely. Thanks for pointing this out!
Cheers,
Quoted 7 lines
Quoted 6 lines
drivers/net/ovpn/peer.c +6
-0+ next_run = tmp_next_run; + } +spin_unlock_bh (&ovpn->peers->lock_by_id); + +return next_run; +}
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 18/25 ovpn: add support for updating local UDP endpoint
In reply to Antonio QuartulliIn case of UDP links, the local endpoint used to communicate with a given peer may change without a connection restart.
Add support for learning the new address in case of change.
--- drivers/net/ovpn/peer.c | 45 +++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 3 +++ 2 files changed, 48 insertions(+)
drivers/net/ovpn/peer.c +45 -0
@@ -416,6 +416,51 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_id)return peer; } +/** +* ovpn_peer_update_local_endpoint - update local endpoint for peer +* @peer: peer to update the endpoint for +* @skb: incoming packet to retrieve the destination address (local) from +*/ +void ovpn_peer_update_local_endpoint (struct ovpn_peer *peer, +struct sk_buff *skb) +{ +struct ovpn_bind *bind; + +rcu_read_lock (); + bind =rcu_dereference (peer->bind); +if (unlikely (!bind)) + goto unlock; + +spin_lock_bh (&peer->lock); +switch (skb_protocol_to_family (skb)) { +case AF_INET : +if (unlikely (bind->local.ipv4.s_addr !=ip_hdr (skb)->daddr)) { +netdev_dbg (peer->ovpn->dev, +"%s: learning local IPv4 for peer %d (%pI4 -> %pI4)\n" , + __func__, peer->id, &bind->local.ipv4.s_addr, + &ip_hdr (skb)->daddr); + bind->local.ipv4.s_addr =ip_hdr (skb)->daddr; + } +break ; +case AF_INET6 : +if (unlikely (!ipv6_addr_equal (&bind->local.ipv6, + &ipv6_hdr (skb)->daddr))) { +netdev_dbg (peer->ovpn->dev, +"%s: learning local IPv6 for peer %d (%pI6c -> %pI6c\n" , + __func__, peer->id, &bind->local.ipv6, + &ipv6_hdr (skb)->daddr); + bind->local.ipv6 =ipv6_hdr (skb)->daddr; + } +break ; +default : +break ; + } +spin_unlock_bh (&peer->lock); + +unlock: +rcu_read_unlock (); +} +/** * ovpn_peer_get_by_dst - Lookup peer to send skb to * @ovpn: the private data representing the current VPN session
drivers/net/ovpn/peer.h +3 -0
@@ -159,4 +159,7 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn, struct sk_buff *skb,void ovpn_peer_keepalive_set (struct ovpn_peer *peer,u32 interval,u32 timeout);void ovpn_peer_keepalive_work (struct work_struct *work); +void ovpn_peer_update_local_endpoint (struct ovpn_peer *peer, +struct sk_buff *skb); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 19/25 ovpn: add support for peer floating
In reply to Antonio QuartulliA peer connected via UDP may change its IP address without reconnecting (float).
Add support for detecting and updating the new peer IP/port in case of floating.
--- drivers/net/ovpn/bind.c | 10 +--- drivers/net/ovpn/io.c | 9 +++ drivers/net/ovpn/peer.c | 123 ++++++++++++++++++++++++++++++++++++++-- drivers/net/ovpn/peer.h | 2 + 4 files changed, 133 insertions(+), 11 deletions(-)
drivers/net/ovpn/bind.c +3 -7
@@ -47,12 +47,8 @@ struct ovpn_bind *ovpn_bind_from_sockaddr(const struct sockaddr_storage *ss)* @new: the new bind to assign */ void ovpn_bind_reset (struct ovpn_peer *peer,struct ovpn_bind *new) + __must_hold(&peer->lock) { -struct ovpn_bind *old; - -spin_lock_bh (&peer->lock); - old =rcu_replace_pointer (peer->bind, new, true); -spin_unlock_bh (&peer->lock); - -kfree_rcu (old, rcu); +kfree_rcu (rcu_replace_pointer (peer->bind, new, +lockdep_is_held (&peer->lock)), rcu); }
drivers/net/ovpn/io.c +9 -0
@@ -118,6 +118,15 @@ void ovpn_decrypt_post(struct sk_buff *skb, int ret)/* keep track of last received authenticated packet for keepalive */ peer->last_recv =ktime_get_real_seconds (); +if (peer->sock->sock->sk->sk_protocol ==IPPROTO_UDP ) { +/* check if this peer changed it's IP address and update +* state +*/ +ovpn_peer_float (peer, skb); +/* update source endpoint for this peer */ +ovpn_peer_update_local_endpoint (peer, skb); + } +/* point to encapsulated IP packet */ __skb_pull (skb,ovpn_skb_cb (skb)->ctx->payload_offset);
drivers/net/ovpn/peer.c +119 -4
142 lines not on this page · raw message
drivers/net/ovpn/peer.h +2 -0
@@ -162,4 +162,6 @@ void ovpn_peer_keepalive_work(struct work_struct *work);void ovpn_peer_update_local_endpoint (struct ovpn_peer *peer,struct sk_buff *skb); +void ovpn_peer_float (struct ovpn_peer *peer,struct sk_buff *skb); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Sabrina Dubroca
reviewerRe: [PATCH net-next v6 19/25] ovpn: add support for peer floating
Quoted 2 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +2
-0+void ovpn_peer_float (struct ovpn_peer *peer,struct sk_buff *skb) +{
[...]
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 201
Quoted 7 lines
drivers/net/ovpn/peer.c +7
-0+ +netdev_dbg (peer->ovpn->dev,"%s: peer %d floated to %pIScp" , __func__, + peer->id, &ss); +ovpn_peer_reset_sockaddr (peer, (struct sockaddr_storage *)&ss, + local_ip); + +spin_lock_bh (&peer->ovpn->peers->lock_by_transp_addr);
ovpn->peers in only set in MP mode, is there something preventing us getting here in P2P mode? I think we need a mode==MP check around the rehash. (I just took a look at other uses of ovpn->peers and there are obvious mode == MP checks before all of them except this one)
I guess this would only happen in P2P mode if the server changes IP, so it doesn't really occur in practice?
Signature · 1 line
Sabrina
Antonio Quartulli
maintainerRe: [PATCH net-next v6 19/25] ovpn: add support for peer floating
Quoted 17 lines · Sabrina Dubroca
Quoted 2 lines · Antonio Quartulli
drivers/net/ovpn/peer.c +2
-0+void ovpn_peer_float (struct ovpn_peer *peer,struct sk_buff *skb) +{[...]
drivers/net/ovpn/peer.c struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id) · line 201
Quoted 7 lines
drivers/net/ovpn/peer.c +7
-0+ +netdev_dbg (peer->ovpn->dev,"%s: peer %d floated to %pIScp" , __func__, + peer->id, &ss); +ovpn_peer_reset_sockaddr (peer, (struct sockaddr_storage *)&ss, + local_ip); + +spin_lock_bh (&peer->ovpn->peers->lock_by_transp_addr);
ovpn->peersin only set in MP mode, is there something preventing us getting here in P2P mode? I think we need a mode==MP check around the rehash. (I just took a look at other uses ofovpn->peersand there are obvious mode == MP checks before all of them except this one)
Quoted 2 lines
I guess this would only happen in P2P mode if the server changes IP, so it doesn't really occur in practice?
Good point. Technically this is still possible (a server may change IP or we may be connected against another P2P host that changes IP), therefore we should not prevent float from working properly in P2P.
I will fix this in the next version.
Thanks!
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Antonio Quartulli
maintainerPATCH v6 20/25 ovpn: implement peer add/dump/delete via netlink
In reply to Antonio QuartulliThis change introduces the netlink command needed to add, delete and retrieve/dump known peers. Userspace is expected to use these commands to handle known peer lifecycles.
--- drivers/net/ovpn/netlink.c | 519 ++++++++++++++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 11 +- drivers/net/ovpn/peer.h | 4 + 3 files changed, 525 insertions(+), 9 deletions(-)
drivers/net/ovpn/netlink.c +515 -4
@@ -8,6 +8,7 @@#include <linux/netdevice.h> #include <linux/rtnetlink.h> +#include <linux/types.h> #include <net/genetlink.h> #include <uapi/linux/ovpn.h>
@@ -17,6 +18,10 @@#include "io.h" #include "netlink.h" #include "netlink-gen.h" +#include "bind.h" +#include "packet.h" +#include "peer.h" +#include "socket.h" MODULE_ALIAS_GENL_FAMILY (OVPN_FAMILY_NAME );
@@ -147,24 +152,530 @@ int ovpn_nl_del_iface_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +static u8 *ovpn_nl_attr_local_ip (struct genl_info *info, +struct ovpn_struct *ovpn, +struct nlattr **attrs,int sock_fam) +{ +size_t ip_len =nla_len (attrs[OVPN_A_PEER_LOCAL_IP ]); +u8 *local_ip =nla_data (attrs[OVPN_A_PEER_LOCAL_IP ]); +bool is_mapped; + +if (ip_len ==sizeof (struct in_addr )) { +if (sock_fam !=AF_INET ) { +NL_SET_ERR_MSG_MOD (info->extack, +"mismatching AF between local IP (v4) and peer" ); +return ERR_PTR (-EINVAL ); + } + }else if (ip_len ==sizeof (struct in6_addr )) { + is_mapped =ipv6_addr_v4mapped ((struct in6_addr *)local_ip); + +if (sock_fam !=AF_INET6 && !is_mapped) { +NL_SET_ERR_MSG_MOD (info->extack, +"mismatching AF between local IP (v6) and peer" ); +return ERR_PTR (-EINVAL ); + } + +if (is_mapped) +/* this is an IPv6-mapped IPv4 +* address, therefore extract +* the actual v4 address from +* the last 4 bytes +*/ + local_ip +=12 ; + }else { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"invalid local IP length: %zu" , ip_len); +return ERR_PTR (-EINVAL ); + } + +return local_ip; +} +int ovpn_nl_set_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; +struct sockaddr_storage *ss =NULL ; +u32 sockfd, id, interv, timeout; +struct socket *sock =NULL ; +struct sockaddr_in mapped; +struct sockaddr_in6 *in6; +struct ovpn_peer *peer; +bool new_peer = false; +u8 *local_ip =NULL ; +size_t sa_len; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); +/* check if the peer exists first, otherwise create a new one */ + peer =ovpn_peer_get_by_id (ovpn, id); +if (!peer) { +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], + attrs,OVPN_A_PEER_SOCKET )) +return -EINVAL ; + +if (ovpn->mode ==OVPN_MODE_MP && + !attrs[OVPN_A_PEER_VPN_IPV4 ] && + !attrs[OVPN_A_PEER_VPN_IPV6 ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"a VPN IP is required when adding a peer in MP mode" ); +return -EINVAL ; + } + + peer =ovpn_peer_new (ovpn, id); + new_peer = true; +if (IS_ERR (peer)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot create new peer object for peer %u: %ld" , + id,PTR_ERR (peer)); +return PTR_ERR (peer); + } + } + +/* VPN IPs cannot be updated, because they are hashed */ +if (!new_peer && (attrs[OVPN_A_PEER_VPN_IPV4 ] || + attrs[OVPN_A_PEER_VPN_IPV6 ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"VPN IPs cannot be updated" ); + ret = -EINVAL ; + goto peer_release; + } + +/* VPN IPs are needed only in MP mode for selecting the right peer */ +if (ovpn->mode ==OVPN_MODE_P2P && (attrs[OVPN_A_PEER_VPN_IPV4 ] || + attrs[OVPN_A_PEER_VPN_IPV6 ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"VPN IP unexpected in P2P mode" ); + ret = -EINVAL ; + goto peer_release; + } + +if ((attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + !attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ]) || + (!attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"keepalive interval and timeout are required together" ); + ret = -EINVAL ; + goto peer_release; + } + +if (attrs[OVPN_A_PEER_SOCKET ]) { +/* lookup the fd in the kernel table and extract the socket +* object +*/ + sockfd =nla_get_u32 (attrs[OVPN_A_PEER_SOCKET ]); +/* sockfd_lookup() increases sock's refcounter */ + sock =sockfd_lookup (sockfd, &ret); +if (!sock) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot lookup peer socket (fd=%u): %d" , + sockfd, ret); + ret = -ENOTSOCK ; + goto peer_release; + } + +/* Only when using UDP as transport protocol the remote endpoint +* can be configured so that ovpn knows where to send packets +* to. +* +* In case of TCP, the socket is connected to the peer and ovpn +* will just send bytes over it, without the need to specify a +* destination. +*/ +if (sock->sk->sk_protocol !=IPPROTO_UDP && + attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"unexpected sockaddr remote for non UDP socket" ); +sockfd_put (sock); + ret = -EINVAL ; + goto peer_release; + } + +if (peer->sock) +ovpn_socket_put (peer->sock); + + peer->sock =ovpn_socket_new (sock, peer); +if (IS_ERR (peer->sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (peer->sock)); +sockfd_put (sock); + peer->sock =NULL ; + ret = -ENOTSOCK ; + goto peer_release; + } + } + +if (attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]) { + ss =nla_data (attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]); + sa_len =nla_len (attrs[OVPN_A_PEER_SOCKADDR_REMOTE ]); +switch (sa_len) { +case sizeof (struct sockaddr_in ): +if (ss->ss_family ==AF_INET ) +/* valid sockaddr */ +break ; + +NL_SET_ERR_MSG_MOD (info->extack, +"remote sockaddr_in has invalid family" ); + ret = -EINVAL ; + goto peer_release; +case sizeof (struct sockaddr_in6 ): +if (ss->ss_family ==AF_INET6 ) +/* valid sockaddr */ +break ; + +NL_SET_ERR_MSG_MOD (info->extack, +"remote sockaddr_in6 has invalid family" ); + ret = -EINVAL ; + goto peer_release; +default : +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"invalid size for sockaddr: %zd" , + sa_len); + ret = -EINVAL ; + goto peer_release; + } + +/* if this is a v6-mapped-v4, convert the sockaddr +* object from AF_INET6 to AF_INET before continue +* processing +*/ +if (ss->ss_family ==AF_INET6 ) { + in6 = (struct sockaddr_in6 *)ss; + +if (ipv6_addr_v4mapped (&in6->sin6_addr)) { + mapped.sin_family =AF_INET ; + mapped.sin_addr.s_addr = + in6->sin6_addr.s6_addr32[3 ]; + mapped.sin_port = in6->sin6_port; + ss = (struct sockaddr_storage *)&mapped; + } + } + +if (attrs[OVPN_A_PEER_LOCAL_IP ]) { + local_ip =ovpn_nl_attr_local_ip (info, ovpn, + attrs, + ss->ss_family); +if (IS_ERR (local_ip)) { + ret =PTR_ERR (local_ip); + goto peer_release; + } + } + +spin_lock_bh (&peer->lock); +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, ss, local_ip); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); +spin_unlock_bh (&peer->lock); + goto peer_release; + } +spin_unlock_bh (&peer->lock); + } + +/* VPN IPs cannot be updated, because they are hashed */ +if (attrs[OVPN_A_PEER_VPN_IPV4 ]) + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + +/* VPN IPs cannot be updated, because they are hashed */ +if (attrs[OVPN_A_PEER_VPN_IPV6 ]) + peer->vpn_addrs.ipv6 = +nla_get_in6_addr (attrs[OVPN_A_PEER_VPN_IPV6 ]); + +/* when setting the keepalive, both parameters have to be configured */ +if (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ]) { + interv =nla_get_u32 (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ]); + timeout =nla_get_u32 (attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ]); +ovpn_peer_keepalive_set (peer, interv, timeout); + } + +netdev_dbg (ovpn->dev, +"%s: %s peer with endpoint=%pIScp/%s id=%u VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + __func__, (new_peer ?"adding" :"modifying" ), ss, + peer->sock->sock->sk->sk_prot_creator->name, peer->id, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +if (new_peer) { + ret =ovpn_peer_add (ovpn, peer); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot add new peer (id=%u) to hashtable: %d\n" , + peer->id, ret); + goto peer_release; + } + }else { +ovpn_peer_put (peer); + } + +return 0 ; + +peer_release: +if (new_peer) { +/* release right away because peer is not really used in any +* context +*/ +ovpn_peer_release (peer); +kfree (peer); + }else { +ovpn_peer_put (peer); + } + +return ret; +} + +static int ovpn_nl_send_peer (struct sk_buff *skb,const struct genl_info *info, +const struct ovpn_peer *peer,u32 portid,u32 seq, +int flags) +{ +const struct ovpn_bind *bind; +struct nlattr *attr; +void *hdr; + + hdr =genlmsg_put (skb, portid, seq, &ovpn_nl_family, flags, +OVPN_CMD_SET_PEER ); +if (!hdr) +return -ENOBUFS ; + + attr =nla_nest_start (skb,OVPN_A_PEER ); +if (!attr) + goto err; + +if (nla_put_u32 (skb,OVPN_A_PEER_ID , peer->id)) + goto err; + +if (peer->vpn_addrs.ipv4.s_addr !=htonl (INADDR_ANY )) +if (nla_put_in_addr (skb,OVPN_A_PEER_VPN_IPV4 , + peer->vpn_addrs.ipv4.s_addr)) + goto err; + +if (!ipv6_addr_equal (&peer->vpn_addrs.ipv6, &in6addr_any)) +if (nla_put_in6_addr (skb,OVPN_A_PEER_VPN_IPV6 , + &peer->vpn_addrs.ipv6)) + goto err; + +if (nla_put_u32 (skb,OVPN_A_PEER_KEEPALIVE_INTERVAL , + peer->keepalive_interval) || +nla_put_u32 (skb,OVPN_A_PEER_KEEPALIVE_TIMEOUT , + peer->keepalive_timeout)) + goto err; + +rcu_read_lock (); + bind =rcu_dereference (peer->bind); +if (bind) { +if (bind->remote.in4.sin_family ==AF_INET ) { +if (nla_put (skb,OVPN_A_PEER_SOCKADDR_REMOTE , +sizeof (bind->remote.in4), + &bind->remote.in4) || +nla_put (skb,OVPN_A_PEER_LOCAL_IP , +sizeof (bind->local.ipv4), + &bind->local.ipv4)) + goto err_unlock; + }else if (bind->remote.in4.sin_family ==AF_INET6 ) { +if (nla_put (skb,OVPN_A_PEER_SOCKADDR_REMOTE , +sizeof (bind->remote.in6), + &bind->remote.in6) || +nla_put (skb,OVPN_A_PEER_LOCAL_IP , +sizeof (bind->local.ipv6), + &bind->local.ipv6)) + goto err_unlock; + } + } +rcu_read_unlock (); + +if (nla_put_net16 (skb,OVPN_A_PEER_LOCAL_PORT , +inet_sk (peer->sock->sock->sk)->inet_sport) || +/* VPN RX stats */ +nla_put_uint (skb,OVPN_A_PEER_VPN_RX_BYTES , +atomic64_read (&peer->vpn_stats.rx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_VPN_RX_PACKETS , +atomic64_read (&peer->vpn_stats.rx.packets)) || +/* VPN TX stats */ +nla_put_uint (skb,OVPN_A_PEER_VPN_TX_BYTES , +atomic64_read (&peer->vpn_stats.tx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_VPN_TX_PACKETS , +atomic64_read (&peer->vpn_stats.tx.packets)) || +/* link RX stats */ +nla_put_uint (skb,OVPN_A_PEER_LINK_RX_BYTES , +atomic64_read (&peer->link_stats.rx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_LINK_RX_PACKETS , +atomic64_read (&peer->link_stats.rx.packets)) || +/* link TX stats */ +nla_put_uint (skb,OVPN_A_PEER_LINK_TX_BYTES , +atomic64_read (&peer->link_stats.tx.bytes)) || +nla_put_uint (skb,OVPN_A_PEER_LINK_TX_PACKETS , +atomic64_read (&peer->link_stats.tx.packets))) + goto err; + +nla_nest_end (skb, attr); +genlmsg_end (skb, hdr); + +return 0 ; +err_unlock: +rcu_read_unlock (); +err: +genlmsg_cancel (skb, hdr); +return -EMSGSIZE ; }int ovpn_nl_get_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; +struct ovpn_peer *peer; +struct sk_buff *msg; +u32 peer_id; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot find peer with id %u" , peer_id); +return -ENOENT ; + } + + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!msg) { + ret = -ENOMEM ; + goto err; + } + + ret =ovpn_nl_send_peer (msg, info, peer, info->snd_portid, + info->snd_seq,0 ); +if (ret <0 ) { +nlmsg_free (msg); + goto err; + } + + ret =genlmsg_reply (msg, info); +err: +ovpn_peer_put (peer); +return ret; }int ovpn_nl_get_peer_dumpit (struct sk_buff *skb,struct netlink_callback *cb) { -return -EOPNOTSUPP ; +const struct genl_info *info =genl_info_dump (cb); +struct ovpn_struct *ovpn; +struct ovpn_peer *peer; +struct net_device *dev; +int bkt, last_idx = cb->args[1 ], dumped =0 ; + + dev =ovpn_get_dev_from_attrs (sock_net (cb->skb->sk), info); +if (IS_ERR (dev)) +return PTR_ERR (dev); + + ovpn =netdev_priv (dev); + +if (ovpn->mode ==OVPN_MODE_P2P ) { +/* if we already dumped a peer it means we are done */ +if (last_idx) + goto out; + +rcu_read_lock (); + peer =rcu_dereference (ovpn->peer); +if (peer) { +if (ovpn_nl_send_peer (skb, info, peer, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, +NLM_F_MULTI ) ==0 ) + dumped++; + } +rcu_read_unlock (); + }else { +rcu_read_lock (); +hash_for_each_rcu (ovpn->peers->by_id, bkt, peer, + hash_entry_id) { +/* skip already dumped peers that were dumped by +* previous invocations +*/ +if (last_idx >0 ) { + last_idx--; +continue ; + } + +if (ovpn_nl_send_peer (skb, info, peer, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, +NLM_F_MULTI ) <0 ) +break ; + +/* count peers being dumped during this invocation */ + dumped++; + } +rcu_read_unlock (); + } + +out: +netdev_put (dev,NULL ); + +/* sum up peers dumped in this message, so that at the next invocation +* we can continue from where we left +*/ + cb->args[1 ] += dumped; +return skb->len; }int ovpn_nl_del_peer_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_struct *ovpn = info->user_ptr[0 ]; +struct ovpn_peer *peer; +u32 peer_id; +int ret; + +if (GENL_REQ_ATTR_CHECK (info,OVPN_A_PEER )) +return -EINVAL ; + + ret =nla_parse_nested (attrs,OVPN_A_PEER_MAX , info->attrs[OVPN_A_PEER ], + ovpn_peer_nl_policy, info->extack); +if (ret) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + + peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + + peer =ovpn_peer_get_by_id (ovpn, peer_id); +if (!peer) +return -ENOENT ; + +netdev_dbg (ovpn->dev,"%s: peer id=%u\n" , __func__, peer->id); + ret =ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_USERSPACE ); +ovpn_peer_put (peer); + +return ret; }int ovpn_nl_set_key_doit (struct sk_buff *skb,struct genl_info *info)
drivers/net/ovpn/peer.c +6 -5
@@ -102,9 +102,9 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, u32 id)* * Return: 0 on success or a negative error code otherwise */ -static int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, -const struct sockaddr_storage *ss, -const u8 *local_ip) +int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, +const struct sockaddr_storage *ss, +const u8 *local_ip) __must_hold(&peer->lock) {struct ovpn_bind *bind;
@@ -215,7 +215,7 @@ void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb)* ovpn_peer_release - release peer private members * @peer: the peer to release */ -static void ovpn_peer_release (struct ovpn_peer *peer) +void ovpn_peer_release (struct ovpn_peer *peer) {if (peer->sock)ovpn_socket_put (peer->sock);
@@ -226,6 +226,8 @@ static void ovpn_peer_release(struct ovpn_peer *peer)spin_unlock_bh (&peer->lock);dst_cache_destroy (&peer->dst_cache); + +netdev_put (peer->ovpn->dev, &peer->ovpn->dev_tracker); }/**
@@ -237,7 +239,6 @@ void ovpn_peer_release_kref(struct kref *kref) struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount);ovpn_peer_release (peer); -netdev_put (peer->ovpn->dev, &peer->ovpn->dev_tracker);kfree_rcu (peer, rcu); }
drivers/net/ovpn/peer.h +4 -0
@@ -131,6 +131,7 @@ static inline bool ovpn_peer_hold(struct ovpn_peer *peer)return kref_get_unless_zero (&peer->refcount); } +void ovpn_peer_release (struct ovpn_peer *peer);void ovpn_peer_release_kref (struct kref *kref);/**
@@ -163,5 +164,8 @@ void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, struct sk_buff *skb);void ovpn_peer_float (struct ovpn_peer *peer,struct sk_buff *skb); +int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, +const struct sockaddr_storage *ss, +const u8 *local_ip);#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 21/25 ovpn: implement key add/del/swap via netlink
In reply to Antonio QuartulliThis change introduces the netlink commands needed to add, delete and swap keys for a specific peer.
Userspace is expected to use these commands to create, destroy and rotate session keys for a specific peer.
--- drivers/net/ovpn/netlink-gen.c | 2 +- drivers/net/ovpn/netlink.c | 224 ++++++++++++++++++++++++++++++++- 2 files changed, 222 insertions(+), 4 deletions(-)
drivers/net/ovpn/netlink-gen.c +1 -1
@@ -23,7 +23,7 @@ static const struct netlink_range_validation ovpn_a_peer_local_port_range = {/* Common nested types */ const struct nla_policy ovpn_keyconf_nl_policy[OVPN_A_KEYCONF_DECRYPT_DIR +1 ] = { [OVPN_A_KEYCONF_SLOT ] =NLA_POLICY_MAX (NLA_U32 ,1 ), - [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,2 ), + [OVPN_A_KEYCONF_KEY_ID ] =NLA_POLICY_MAX (NLA_U32 ,7 ), [OVPN_A_KEYCONF_CIPHER_ALG ] =NLA_POLICY_MAX (NLA_U32 ,2 ), [OVPN_A_KEYCONF_ENCRYPT_DIR ] =NLA_POLICY_NESTED (ovpn_keydir_nl_policy), [OVPN_A_KEYCONF_DECRYPT_DIR ] =NLA_POLICY_NESTED (ovpn_keydir_nl_policy),
drivers/net/ovpn/netlink.c +221 -3
240 lines not on this page · raw message
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 22/25 ovpn: kill key and notify userspace in case of IV exhaustion
In reply to Antonio QuartulliIV wrap-around is cryptographically dangerous for a number of ciphers, therefore kill the key and inform userspace (via netlink) should the IV space go exhausted.
Userspace has two ways of deciding when the key has to be renewed before exhausting the IV space:
1) time based approach:
after X seconds/minutes userspace generates a new key and sends it to the kernel. This is based on guestimate and normally default timer value works well.
2) packet count based approach:
after X packets/bytes userspace generates a new key and sends it to the kernel. Userspace keeps track of the amount of traffic by periodically polling GET_PEER and fetching the VPN/LINK stats.
--- drivers/net/ovpn/crypto.c | 19 ++++++++++++ drivers/net/ovpn/crypto.h | 2 ++ drivers/net/ovpn/io.c | 13 +++++++++ drivers/net/ovpn/netlink.c | 60 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 2 ++ 5 files changed, 96 insertions(+)
drivers/net/ovpn/crypto.c +19 -0
@@ -57,6 +57,25 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)mutex_destroy (&cs->mutex); } +/* removes the key matching the specified id from the crypto context */ +void ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id) +{ +struct ovpn_crypto_key_slot *ks =NULL ; + +mutex_lock (&cs->mutex); +if (rcu_access_pointer (cs->primary)->key_id == key_id) { + ks =rcu_replace_pointer (cs->primary,NULL , +lockdep_is_held (&cs->mutex)); + }else if (rcu_access_pointer (cs->secondary)->key_id == key_id) { + ks =rcu_replace_pointer (cs->secondary,NULL , +lockdep_is_held (&cs->mutex)); + } +mutex_unlock (&cs->mutex); + +if (ks) +ovpn_crypto_key_slot_put (ks); +} +/* Reset the ovpn_crypto_state object in a way that is atomic * to RCU readers. */
drivers/net/ovpn/crypto.h +2 -0
@@ -133,4 +133,6 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs);void ovpn_crypto_key_slots_swap (struct ovpn_crypto_state *cs); +void ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id); +#endif /* _NET_OVPN_OVPNCRYPTO_H_ */
drivers/net/ovpn/io.c +13 -0
@@ -218,6 +218,19 @@ void ovpn_encrypt_post(struct sk_buff *skb, int ret)if (unlikely (ret == -EINPROGRESS ))return ; +if (unlikely (ret == -ERANGE )) { +/* we ran out of IVs and we must kill the key as it can't be +* use anymore +*/ +netdev_warn (peer->ovpn->dev, +"killing key %u for peer %u\n" , ks->key_id, + peer->id); +ovpn_crypto_kill_key (&peer->crypto, ks->key_id); +/* let userspace know so that a new key can be negotiated */ +ovpn_nl_notify_swap_keys (peer, ks->key_id); + goto err; + } +if (unlikely (ret <0 )) goto err;
drivers/net/ovpn/netlink.c +60 -0
66 lines not on this page · raw message
drivers/net/ovpn/netlink.h +2 -0
@@ -12,4 +12,6 @@int ovpn_nl_register (void );void ovpn_nl_unregister (void ); +int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer,u8 key_id); +#endif /* _NET_OVPN_NETLINK_H_ */
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 23/25 ovpn: notify userspace when a peer is deleted
In reply to Antonio QuartulliWhenever a peer is deleted, send a notification to userspace so that it can react accordingly.
This is most important when a peer is deleted due to ping timeout, because it all happens in kernelspace and thus userspace has no direct way to learn about it.
--- drivers/net/ovpn/netlink.c | 55 ++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 1 + drivers/net/ovpn/peer.c | 1 + 3 files changed, 57 insertions(+)
drivers/net/ovpn/netlink.c +55 -0
@@ -911,6 +911,61 @@ int ovpn_nl_del_key_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +/** +* ovpn_nl_notify_del_peer - notify userspace about peer being deleted +* @peer: the peer being deleted +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_nl_notify_del_peer (struct ovpn_peer *peer) +{ +struct sk_buff *msg; +struct nlattr *attr; +int ret = -EMSGSIZE ; +void *hdr; + +netdev_info (peer->ovpn->dev,"deleting peer with id %u, reason %d\n" , + peer->id, peer->delete_reason); + + msg =nlmsg_new (100 ,GFP_ATOMIC ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put (msg,0 ,0 , &ovpn_nl_family,0 ,OVPN_CMD_DEL_PEER ); +if (!hdr) { + ret = -ENOBUFS ; + goto err_free_msg; + } + +if (nla_put_u32 (msg,OVPN_A_IFINDEX , peer->ovpn->dev->ifindex)) + goto err_cancel_msg; + + attr =nla_nest_start (msg,OVPN_A_PEER ); +if (!attr) + goto err_cancel_msg; + +if (nla_put_u8 (msg,OVPN_A_PEER_DEL_REASON , peer->delete_reason)) + goto err_cancel_msg; + +if (nla_put_u32 (msg,OVPN_A_PEER_ID , peer->id)) + goto err_cancel_msg; + +nla_nest_end (msg, attr); + +genlmsg_end (msg, hdr); + +genlmsg_multicast_netns (&ovpn_nl_family,dev_net (peer->ovpn->dev), msg, +0 ,OVPN_NLGRP_PEERS ,GFP_ATOMIC ); + +return 0 ; + +err_cancel_msg: +genlmsg_cancel (msg, hdr); +err_free_msg: +nlmsg_free (msg); +return ret; +} +/** * ovpn_nl_notify_swap_keys - notify userspace peer's key must be renewed * @peer: the peer whose key needs to be renewed
drivers/net/ovpn/netlink.h +1 -0
@@ -12,6 +12,7 @@int ovpn_nl_register (void );void ovpn_nl_unregister (void ); +int ovpn_nl_notify_del_peer (struct ovpn_peer *peer);int ovpn_nl_notify_swap_keys (struct ovpn_peer *peer,u8 key_id);#endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/peer.c +1 -0
@@ -238,6 +238,7 @@ void ovpn_peer_release_kref(struct kref *kref) { struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount); +ovpn_nl_notify_del_peer (peer);ovpn_peer_release (peer);kfree_rcu (peer, rcu); }
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 24/25 ovpn: add basic ethtool support
In reply to Antonio QuartulliImplement support for basic ethtool functionality.
Note that ovpn is a virtual device driver, therefore various ethtool APIs are just not meaningful and thus not implemented.
--- drivers/net/ovpn/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/net/ovpn/main.c +15 -0
@@ -7,6 +7,7 @@* James Yonan <[email protected]> */ +#include <linux/ethtool.h> #include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h>
@@ -141,6 +142,19 @@ bool ovpn_dev_is_valid(const struct net_device *dev)return dev->netdev_ops->ndo_start_xmit == ovpn_net_xmit; } +static void ovpn_get_drvinfo (struct net_device *dev, +struct ethtool_drvinfo *info) +{ +strscpy (info->driver,OVPN_FAMILY_NAME ,sizeof (info->driver)); +strscpy (info->bus_info,"ovpn" ,sizeof (info->bus_info)); +} + +static const struct ethtool_ops ovpn_ethtool_ops = { + .get_drvinfo = ovpn_get_drvinfo, + .get_link = ethtool_op_get_link, + .get_ts_info = ethtool_op_get_ts_info, +}; +/* we register with rtnl to let core know that ovpn is a virtual driver and * therefore ifaces should be destroyed when exiting a netns */
@@ -163,6 +177,7 @@ static void ovpn_setup(struct net_device *dev) dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; + dev->ethtool_ops = &ovpn_ethtool_ops; dev->netdev_ops = &ovpn_netdev_ops; dev->rtnl_link_ops = &ovpn_link_ops;
Signature · 1 line
2.44.2
Antonio Quartulli
maintainerPATCH v6 25/25 testing/selftest: add test tool and scripts for ovpn module
In reply to Antonio QuartulliThe ovpn-cli tool can be compiled and used as selftest for the ovpn kernel module.
It implementes the netlink API and can thus be integrated in any script for more automated testing.
Along with the tool, 2 scripts are added that perform basic functionality tests by means of network namespaces.
The scripts can be performed in sequence by running run.sh
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 17 + tools/testing/selftests/net/ovpn/config | 8 + .../selftests/net/ovpn/data-test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/data-test.sh | 150 ++ tools/testing/selftests/net/ovpn/data64.key | 5 + .../testing/selftests/net/ovpn/float-test.sh | 115 ++ tools/testing/selftests/net/ovpn/ovpn-cli.c | 1820 +++++++++++++++++ .../testing/selftests/net/ovpn/tcp_peers.txt | 1 + .../testing/selftests/net/ovpn/udp_peers.txt | 5 + 11 files changed, 2133 insertions(+) create mode 100644 tools/testing/selftests/net/ovpn/.gitignore create mode 100644 tools/testing/selftests/net/ovpn/Makefile create mode 100644 tools/testing/selftests/net/ovpn/config create mode 100755 tools/testing/selftests/net/ovpn/data-test-tcp.sh create mode 100755 tools/testing/selftests/net/ovpn/data-test.sh create mode 100644 tools/testing/selftests/net/ovpn/data64.key create mode 100755 tools/testing/selftests/net/ovpn/float-test.sh create mode 100644 tools/testing/selftests/net/ovpn/ovpn-cli.c create mode 100644 tools/testing/selftests/net/ovpn/tcp_peers.txt create mode 100644 tools/testing/selftests/net/ovpn/udp_peers.txt
tools/testing/selftests/Makefile +1 -0
@@ -66,6 +66,7 @@ TARGETS += net/forwarding TARGETS += net/hsr TARGETS += net/mptcp TARGETS += net/openvswitch +TARGETS += net/ovpn TARGETS += net/tcp_ao TARGETS += net/netfilter TARGETS += net/rds
tools/testing/selftests/net/ovpn/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0+ +ovpn-cli
tools/testing/selftests/net/ovpn/Makefile new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2024 OpenVPN, Inc. +# +CFLAGS = -Wall -I../../../../../usr/include +CFLAGS += $(shell pkg-config --cflags libnl-3.0 libnl-genl-3.0) + +LDFLAGS = -lmbedtls -lmbedcrypto +LDFLAGS += $(shell pkg-config --libs libnl-3.0 libnl-genl-3.0) + +ovpn-cli: ovpn-cli.c + +TEST_PROGS = data-test.sh \ + data-test-tcp.sh \ + float-test.sh +TEST_GEN_FILES = ovpn-cli + +include ../../lib.mk
tools/testing/selftests/net/ovpn/config new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_NET=y +CONFIG_INET=y +CONFIG_NET_UDP_TUNNEL=y +CONFIG_DST_CACHE=y +CONFIG_CRYPTO_AES=y +CONFIG_CRYPTO_GCM=y +CONFIG_CRYPTO_CHACHA20POLY1305=y +CONFIG_OVPN=y
tools/testing/selftests/net/ovpn/data-test-tcp.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +PROTO="TCP" + +source data-test.sh
tools/testing/selftests/net/ovpn/data-test.sh new file +150 -0
150 lines not on this page · raw message
tools/testing/selftests/net/ovpn/data64.key new file +5 -0
@@ -0,0 +1,5 @@ +jRqMACN7d7/aFQNT8S7jkrBD8uwrgHbG5OQZP2eu4R1Y7tfpS2bf5RHv06Vi163CGoaIiTX99R3B +ia9ycAH8Wz1+9PWv51dnBLur9jbShlgZ2QHLtUc4a/gfT7zZwULXuuxdLnvR21DDeMBaTbkgbai9 +uvAa7ne1liIgGFzbv+Bas4HDVrygxIxuAnP5Qgc3648IJkZ0QEXPF+O9f0n5+QIvGCxkAUVx+5K6 +KIs+SoeWXnAopELmoGSjUpFtJbagXK82HfdqpuUxT2Tnuef0/14SzVE/vNleBNu2ZbyrSAaah8tE +BofkPJUBFY+YQcfZNM5Dgrw3i+Bpmpq/gpdg5w==
tools/testing/selftests/net/ovpn/float-test.sh new file +115 -0
115 lines not on this page · raw message
tools/testing/selftests/net/ovpn/ovpn-cli.c new file +1820 -0
1,820 lines not on this page · raw message
tools/testing/selftests/net/ovpn/tcp_peers.txt new file +1 -0
@@ -0,0 +1 @@ +1 5.5.5.2
tools/testing/selftests/net/ovpn/udp_peers.txt new file +5 -0
@@ -0,0 +1,5 @@ +1 10.10.1.2 1 5.5.5.2 +2 10.10.2.2 1 5.5.5.3 +3 10.10.3.2 1 5.5.5.4 +4 10.10.4.2 1 5.5.5.5 +5 10.10.5.2 1 5.5.5.6
Signature · 1 line
2.44.2