Introducing OpenVPN Data Channel Offload
Subsystems (6)
Antonio Quartulli
maintainerv26 cover Introducing OpenVPN Data Channel Offload
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and moved netif_carrier_off() call to .ndo_init
* fixed author in MODULE_AUTHOR()
* properly indented checks in ovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment in ovpn_socket_new()
* removed unrelated hunk in ovpn_socket_new()
The latest code can also be found at:
https://github.com/OpenVPN/ovpn-net-next
Thanks a lot!
Best Regards,
Antonio Quartulli
OpenVPN Inc.
---
Antonio Quartulli (23):
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: keep carrier always on for MP interfaces
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
skb: implement skb_send_sock_locked_with_flags()
ovpn: add support for MSG_NOSIGNAL in tcp_sendmsg
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
ovpn: implement keepalive mechanism
ovpn: add support for updating local or remote UDP endpoint
ovpn: implement peer add/get/dump/delete via netlink
ovpn: implement key add/get/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/selftests: add test tool and scripts for ovpn module
Documentation/netlink/specs/ovpn.yaml | 367 +++ Documentation/netlink/specs/rt-link.yaml | 16 + MAINTAINERS | 11 + drivers/net/Kconfig | 15 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 55 + drivers/net/ovpn/bind.h | 101 + drivers/net/ovpn/crypto.c | 210 ++ drivers/net/ovpn/crypto.h | 145 ++ drivers/net/ovpn/crypto_aead.c | 383 ++++ drivers/net/ovpn/crypto_aead.h | 29 + drivers/net/ovpn/io.c | 446 ++++ drivers/net/ovpn/io.h | 34 + drivers/net/ovpn/main.c | 274 +++ drivers/net/ovpn/main.h | 14 + drivers/net/ovpn/netlink-gen.c | 213 ++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 1258 +++++++++++ drivers/net/ovpn/netlink.h | 18 + drivers/net/ovpn/ovpnpriv.h | 55 + drivers/net/ovpn/peer.c | 1365 +++++++++++ drivers/net/ovpn/peer.h | 163 ++ drivers/net/ovpn/pktid.c | 129 ++ drivers/net/ovpn/pktid.h | 86 + drivers/net/ovpn/proto.h | 118 + drivers/net/ovpn/skb.h | 61 + drivers/net/ovpn/socket.c | 233 ++ drivers/net/ovpn/socket.h | 49 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 47 + drivers/net/ovpn/tcp.c | 598 +++++ drivers/net/ovpn/tcp.h | 36 + drivers/net/ovpn/udp.c | 439 ++++ drivers/net/ovpn/udp.h | 25 + include/linux/skbuff.h | 2 + include/uapi/linux/if_link.h | 15 + include/uapi/linux/ovpn.h | 109 + include/uapi/linux/udp.h | 1 + net/core/skbuff.c | 18 +- net/ipv6/af_inet6.c | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 31 + tools/testing/selftests/net/ovpn/common.sh | 92 + tools/testing/selftests/net/ovpn/config | 10 + tools/testing/selftests/net/ovpn/data64.key | 5 + tools/testing/selftests/net/ovpn/ovpn-cli.c | 2376 ++++++++++++++++++++ tools/testing/selftests/net/ovpn/tcp_peers.txt | 5 + .../testing/selftests/net/ovpn/test-chachapoly.sh | 9 + .../selftests/net/ovpn/test-close-socket-tcp.sh | 9 + .../selftests/net/ovpn/test-close-socket.sh | 45 + tools/testing/selftests/net/ovpn/test-float.sh | 9 + tools/testing/selftests/net/ovpn/test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/test.sh | 113 + tools/testing/selftests/net/ovpn/udp_peers.txt | 5 + 56 files changed, 9940 insertions(+), 5 deletions(-)
---
base-commit: 23f09f01b495cc510a19b30b6093fb4cb0284aaf
change-id: 20241002-b4-ovpn-eeee35c694a2
Best regards,
Signature · 1 line
Antonio Quartulli <[email protected]>
Antonio Quartulli
maintainerPATCH v26 1/23 net: introduce OpenVPN Data Channel Offload (ovpn)
OpenVPN is a userspace software existing since around 2005 that allows users to create secure tunnels.
So far OpenVPN has implemented all operations in userspace, which implies several back and forth between kernel and user land in order to process packets (encapsulate/decapsulate, encrypt/decrypt, rerouting..).
With ovpn we intend to move the fast path (data channel) entirely in kernel space and thus improve user measured throughput over the tunnel.
ovpn is implemented as a simple virtual network device driver, that can be manipulated by means of the standard RTNL APIs. A device of kind ovpn allows only IPv4/6 traffic and can be of type:
* P2P (peer-to-peer): any packet sent over the interface will be encapsulated and transmitted to the other side (typical OpenVPN client or peer-to-peer behaviour);
* P2MP (point-to-multipoint): packets sent over the interface are transmitted to peers based on existing routes (typical OpenVPN server behaviour).
After the interface has been created, OpenVPN in userspace can configure it using a new Netlink API. Specifically it is possible to manage peers and their keys.
The OpenVPN control channel is multiplexed over the same transport socket by means of OP codes. Anything that is not DATA_V2 (OpenVPN OP code for data traffic) is sent to userspace and handled there. This way the ovpn codebase is kept as compact as possible while focusing on handling data traffic only (fast path).
Any OpenVPN control feature (like cipher negotiation, TLS handshake, rekeying, etc.) is still fully handled by the userspace process.
When userspace establishes a new connection with a peer, it first performs the handshake and then passes the socket to the ovpn kernel module, which takes ownership. From this moment on ovpn will handle data traffic for the new peer.
When control packets are received on the link, they are forwarded to userspace through the same transport socket they were received on, as userspace is still listening to them.
Some events (like peer deletion) are sent to a Netlink multicast group.
Although it wasn't easy to convince the community, ovpn implements only a limited number of the data-channel features supported by the userspace program.
Each feature that made it to ovpn was attentively vetted to avoid carrying too much legacy along with us (and to give a clear cut to old and probalby-not-so-useful features).
Notably, only encryption using AEAD ciphers (specifically ChaCha20Poly1305 and AES-GCM) was implemented. Supporting any other cipher out there was not deemed useful.
Both UDP and TCP sockets are supported.
As explained above, in case of P2MP mode, OpenVPN will use the main system routing table to decide which packet goes to which peer. This implies that no routing table was re-implemented in the ovpn kernel module.
This kernel module can be enabled by selecting the CONFIG_OVPN entry in the networking drivers section.
NOTE: this first patch introduces the very basic framework only. Features are then added patch by patch, however, although each patch will compile and possibly not break at runtime, only after having applied the full set it is expected to see the ovpn module fully working.
--- MAINTAINERS | 8 ++++++++ drivers/net/Kconfig | 8 ++++++++ drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 10 +++++++++ drivers/net/ovpn/main.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+)
MAINTAINERS +8 -0
@@ -18125,6 +18125,14 @@ F: arch/openrisc/ F: drivers/irqchip/irq-ompic.c F: drivers/irqchip/irq-or1k-* +OPENVPN DATA CHANNEL OFFLOAD +M: Antonio Quartulli <[email protected]> +L: [email protected] (subscribers-only) +L: [email protected] +S: Supported +T: git https://github.com/OpenVPN/linux-kernel-ovpn.git +F: drivers/net/ovpn/ + OPENVSWITCH M: Aaron Conole <[email protected]> M: Eelco Chaudron <[email protected]>
drivers/net/Kconfig +8 -0
@@ -115,6 +115,14 @@ config WIREGUARD_DEBUG Say N here unless you know what you're doing. +config OVPN + tristate "OpenVPN data channel offload" + depends on NET && INET + depends on IPV6 || !IPV6 + 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 +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# ovpn -- OpenVPN data channel offload in kernel space +# +# Copyright (C) 2020-2025 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +obj-$(CONFIG_OVPN) := ovpn.o +ovpn-y += main.o
drivers/net/ovpn/main.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 2/23 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 | 367 ++++++++++++++++++++++++++++++++++ MAINTAINERS | 2 + drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/main.c | 31 +++ drivers/net/ovpn/main.h | 14 ++ drivers/net/ovpn/netlink-gen.c | 213 ++++++++++++++++++++ drivers/net/ovpn/netlink-gen.h | 41 ++++ drivers/net/ovpn/netlink.c | 160 +++++++++++++++ drivers/net/ovpn/netlink.h | 15 ++ drivers/net/ovpn/ovpnpriv.h | 21 ++ include/uapi/linux/ovpn.h | 109 ++++++++++ 11 files changed, 975 insertions(+)
Documentation/netlink/specs/ovpn.yaml new file +367 -0
@@ -0,0 +1,367 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) +# +# Author: Antonio Quartulli <[email protected]> +# +# Copyright (c) 2024-2025, OpenVPN Inc. +# + +name: ovpn + +protocol: genetlink + +doc: Netlink protocol to control OpenVPN network devices + +definitions: + - + type: const + name: nonce-tail-size + value: 8 + - + type: enum + name: cipher-alg + entries: [ none, aes-gcm, chacha20-poly1305 ] + - + type: enum + name: del-peer-reason + entries: + - teardown + - userspace + - expired + - transport-error + - transport-disconnect + - + type: enum + name: key-slot + entries: [ primary, secondary ] + +attribute-sets: + - + name: peer + attributes: + - + name: id + type: u32 + doc: >- + The unique ID of the peer in the device context. To be used to identify + peers during operations for a specific device + checks: + max: 0xFFFFFF + - + name: remote-ipv4 + type: u32 + doc: The remote IPv4 address of the peer + byte-order: big-endian + display-hint: ipv4 + - + name: remote-ipv6 + type: binary + doc: The remote IPv6 address of the peer + display-hint: ipv6 + checks: + exact-len: 16 + - + name: remote-ipv6-scope-id + type: u32 + doc: The scope id of the remote IPv6 address of the peer (RFC2553) + - + name: remote-port + type: u16 + doc: The remote port of the peer + byte-order: big-endian + checks: + min: 1 + - + name: socket + type: u32 + doc: The socket to be used to communicate with the peer + - + name: socket-netnsid + type: s32 + doc: The ID of the netns the socket assigned to this peer lives in + - + name: vpn-ipv4 + type: u32 + doc: The IPv4 address assigned to the peer by the server + byte-order: big-endian + display-hint: ipv4 + - + name: vpn-ipv6 + type: binary + doc: The IPv6 address assigned to the peer by the server + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-ipv4 + type: u32 + doc: The local IPv4 to be used to send packets to the peer (UDP only) + byte-order: big-endian + display-hint: ipv4 + - + name: local-ipv6 + type: binary + doc: The local IPv6 to be used to send packets to the peer (UDP only) + display-hint: ipv6 + checks: + exact-len: 16 + - + name: local-port + type: u16 + doc: The local port to be used to send packets to the peer (UDP only) + byte-order: big-endian + checks: + min: 1 + - + name: keepalive-interval + type: u32 + doc: >- + The number of seconds after which a keep alive message is sent 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: 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: uint + doc: Number of packets received at the transport level + - + name: link-tx-packets + type: uint + doc: Number of packets transmitted at the transport level + - + name: keyconf + attributes: + - + name: peer-id + type: u32 + doc: >- + The unique ID of the peer in the device context. To be used to + identify peers during key operations + checks: + max: 0xFFFFFF + - + name: slot + type: u32 + doc: The slot where the key should be stored + enum: key-slot + - + name: key-id + doc: >- + The unique ID of the key in the peer context. Used to fetch the + correct key upon decryption + type: u32 + checks: + max: 7 + - + name: cipher-alg + type: u32 + doc: The cipher to be used when communicating with the peer + enum: cipher-alg + - + name: encrypt-dir + type: nest + doc: Key material for encrypt direction + nested-attributes: keydir + - + name: decrypt-dir + type: nest + doc: Key material for decrypt direction + nested-attributes: keydir + - + name: keydir + attributes: + - + name: cipher-key + type: binary + doc: The actual key to be used by the cipher + checks: + max-len: 256 + - + name: nonce-tail + type: binary + doc: >- + Random nonce to be concatenated to the packet ID, in order to + obtain the actual cipher IV + checks: + exact-len: nonce-tail-size + - + name: ovpn + attributes: + - + name: ifindex + type: u32 + doc: Index of the ovpn interface to operate on + - + name: peer + type: nest + doc: >- + The peer object containing the attributed of interest for the specific + operation + nested-attributes: peer + - + name: keyconf + type: nest + doc: Peer specific cipher configuration + nested-attributes: keyconf + +operations: + list: + - + name: peer-new + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-set + attribute-set: ovpn + flags: [ admin-perm ] + doc: modify a remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-get + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve data about existing remote peers (or a specific one) + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + reply: + attributes: + - peer + dump: + request: + attributes: + - ifindex + reply: + attributes: + - peer + - + name: peer-del + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete existing remote peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - peer + - + name: peer-del-ntf + doc: Notification about a peer being deleted + notify: peer-get + mcgrp: peers + + - + name: key-new + attribute-set: ovpn + flags: [ admin-perm ] + doc: Add a cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + - + name: key-get + attribute-set: ovpn + flags: [ admin-perm ] + doc: Retrieve non-sensitive data about peer key and cipher + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + reply: + attributes: + - keyconf + - + name: key-swap + attribute-set: ovpn + flags: [ admin-perm ] + doc: Swap primary and secondary session keys for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + - + name: key-swap-ntf + notify: key-get + doc: >- + Notification about key having exhausted its IV space and requiring + renegotiation + mcgrp: peers + - + name: key-del + attribute-set: ovpn + flags: [ admin-perm ] + doc: Delete cipher key for a specific peer + do: + pre: ovpn-nl-pre-doit + post: ovpn-nl-post-doit + request: + attributes: + - ifindex + - keyconf + +mcast-groups: + list: + - + name: peers
MAINTAINERS +2 -0
@@ -18131,7 +18131,9 @@ L: [email protected] (subscribers-only) L: [email protected] S: Supported T: git https://github.com/OpenVPN/linux-kernel-ovpn.git +F: Documentation/netlink/specs/ovpn.yaml F: drivers/net/ovpn/ +F: include/uapi/linux/ovpn.h OPENVSWITCH M: Aaron Conole <[email protected]>
drivers/net/ovpn/Makefile +2 -0
@@ -8,3 +8,5 @@ obj-$(CONFIG_OVPN) := ovpn.o ovpn-y += main.o +ovpn-y += netlink.o +ovpn-y += netlink-gen.o
drivers/net/ovpn/main.c +31 -0
@@ -7,9 +7,29 @@* James Yonan <[email protected]> */ +#include <linux/genetlink.h> #include <linux/module.h> #include <linux/netdevice.h> #include <net/rtnetlink.h> +#include <uapi/linux/ovpn.h> + +#include "ovpnpriv.h" +#include "main.h" +#include "netlink.h" + +static const struct net_device_ops ovpn_netdev_ops = { +}; + +/** +* ovpn_dev_is_valid - check if the netdevice is of type 'ovpn' +* @dev: the interface to check +* +* Return: whether the netdevice is of type 'ovpn' +*/ +bool ovpn_dev_is_valid (const struct net_device *dev) +{ +return dev->netdev_ops == &ovpn_netdev_ops; +} static int ovpn_newlink(struct net_device *dev, struct rtnl_newlink_params *params,
@@ -34,11 +54,22 @@ static int __init ovpn_init(void)return err; } + err =ovpn_nl_register (); +if (err) { +pr_err ("ovpn: can't register netlink family: %d\n" , err); + goto unreg_rtnl; + } +return 0 ; + +unreg_rtnl: +rtnl_link_unregister (&ovpn_link_ops); +return err; } static __exit void ovpn_cleanup(void) { +ovpn_nl_unregister ();rtnl_link_unregister (&ovpn_link_ops);rcu_barrier ();
drivers/net/ovpn/main.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2025 OpenVPN, Inc. +* +* Author: 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_ */
drivers/net/ovpn/netlink-gen.c new file +213 -0
213 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 +160 -0
160 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-2025 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/ovpnpriv.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2025 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_OVPNSTRUCT_H_ +#define _NET_OVPN_OVPNSTRUCT_H_ + +/** +* struct ovpn_priv - per ovpn interface state +* @dev: the actual netdev representing the tunnel +*/ +struct ovpn_priv { +struct net_device *dev; +}; + +#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
include/uapi/linux/ovpn.h new file +109 -0
109 lines not on this page · raw message
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 3/23 ovpn: add basic interface creation/destruction/management routines
In reply to Antonio QuartulliAdd basic infrastructure for handling ovpn interfaces.
--- Documentation/netlink/specs/rt-link.yaml | 16 +++++++ drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 22 +++++++++ drivers/net/ovpn/io.h | 24 ++++++++++ drivers/net/ovpn/main.c | 82 +++++++++++++++++++++++++++++++- drivers/net/ovpn/ovpnpriv.h | 5 ++ drivers/net/ovpn/proto.h | 38 +++++++++++++++ include/uapi/linux/if_link.h | 15 ++++++ 8 files changed, 201 insertions(+), 2 deletions(-)
Documentation/netlink/specs/rt-link.yaml +16 -0
@@ -938,6 +938,12 @@ definitions: entries: - name: none - name: default + - + name: ovpn-mode + type: enum + entries: + - p2p + - mp attribute-sets: -
@@ -2272,6 +2278,13 @@ attribute-sets: - name: tailroom type: u16 + - + name: linkinfo-ovpn-attrs + attributes: + - + name: mode + type: u8 + enum: ovpn-mode sub-messages: -
@@ -2322,6 +2335,9 @@ sub-messages: - value: netkit attribute-set: linkinfo-netkit-attrs + - + value: ovpn + attribute-set: linkinfo-ovpn-attrs - name: linkinfo-member-data-msg formats:
drivers/net/ovpn/Makefile +1 -0
@@ -8,5 +8,6 @@ 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/io.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2025 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 +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2025 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_OVPN_H_ +#define _NET_OVPN_OVPN_H_ + +/* DATA_V2 header size with AEAD encryption */ +#define OVPN_HEAD_ROOM (OVPN_OPCODE_SIZE + OVPN_NONCE_WIRE_SIZE + \ + 16 /* AEAD TAG length */ + \ + max(sizeof(struct udphdr), sizeof(struct tcphdr)) +\ + max(sizeof(struct ipv6hdr), sizeof(struct iphdr))) + +/* max padding required by encryption */ +#define OVPN_MAX_PADDING 16 + +netdev_tx_t ovpn_net_xmit (struct sk_buff *skb,struct net_device *dev); + +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/main.c +80 -2
112 lines not on this page · raw message
drivers/net/ovpn/ovpnpriv.h +5 -0
@@ -10,12 +10,17 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <uapi/linux/if_link.h> +#include <uapi/linux/ovpn.h> +/** * struct ovpn_priv - per ovpn interface state * @dev: the actual netdev representing the tunnel +* @mode: device operation mode (i.e. p2p, mp, ..) */ struct ovpn_priv {struct net_device *dev; +enum ovpn_mode mode; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/proto.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2025 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +* James Yonan <[email protected]> +*/ + +#ifndef _NET_OVPN_PROTO_H_ +#define _NET_OVPN_PROTO_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 ] -> OVPN_NONCE_SIZE +* [4-bytes -> OVPN_NONCE_WIRE_SIZE +* on wire] +*/ + +/* nonce size (96bits) as required by AEAD ciphers */ +#define OVPN_NONCE_SIZE 12 +/* last 8 bytes of AEAD nonce: provided by userspace and usually derived +* from key material generated during TLS handshake +*/ +#define OVPN_NONCE_TAIL_SIZE 8 + +/* 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 OVPN_NONCE_WIRE_SIZE (OVPN_NONCE_SIZE - OVPN_NONCE_TAIL_SIZE) + +#define OVPN_OPCODE_SIZE 4/* DATA_V2 opcode size */ + +#endif /* _NET_OVPN_PROTO_H_ */
include/uapi/linux/if_link.h +15 -0
@@ -1986,4 +1986,19 @@ enum {#define IFLA_DSA_MAX (__IFLA_DSA_MAX - 1) +/* OVPN section */ + +enum ovpn_mode { +OVPN_MODE_P2P , +OVPN_MODE_MP , +}; + +enum { +IFLA_OVPN_UNSPEC , +IFLA_OVPN_MODE , + __IFLA_OVPN_MAX, +}; + +#define IFLA_OVPN_MAX (__IFLA_OVPN_MAX - 1) +#endif /* _UAPI_LINUX_IF_LINK_H */
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 4/23 ovpn: keep carrier always on for MP interfaces
In reply to Antonio QuartulliAn ovpn interface configured in MP mode will keep carrier always on and let the user decide when to bring it administratively up and down.
This way a MP node (i.e. a server) will keep its interface always up and running, even when no peer is connected.
--- drivers/net/ovpn/main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/ovpn/main.c +11 -3
@@ -93,10 +93,18 @@ static int ovpn_newlink(struct net_device *dev, ovpn->dev = dev; ovpn->mode = mode; -/* turn carrier explicitly off after registration, this way state is -* clearly defined +/* Set carrier explicitly after registration, this way state is +* clearly defined. +* +* In case of MP interfaces we keep the carrier always on. +* +* Carrier for P2P interfaces is initially off and it is then +* switched on and off when the remote peer is added or deleted. */ -netif_carrier_off (dev); +if (ovpn->mode ==OVPN_MODE_MP ) +netif_carrier_on (dev); +else +netif_carrier_off (dev);return register_netdevice (dev); }
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 5/23 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/Kconfig | 1 + drivers/net/ovpn/Makefile | 2 + drivers/net/ovpn/bind.c | 58 +++++++ drivers/net/ovpn/bind.h | 101 +++++++++++ drivers/net/ovpn/main.c | 14 +- drivers/net/ovpn/ovpnpriv.h | 4 + drivers/net/ovpn/peer.c | 411 ++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 80 +++++++++ 8 files changed, 670 insertions(+), 1 deletion(-)
drivers/net/Kconfig +1 -0
@@ -119,6 +119,7 @@ config OVPN tristate "OpenVPN data channel offload" depends on NET && INET depends on IPV6 || !IPV6 + select DST_CACHE help This module enhances the performance of the OpenVPN userspace software by offloading the data channel processing to kernelspace.
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 +101 -0
101 lines not on this page · raw message
drivers/net/ovpn/main.c +13 -1
@@ -19,6 +19,7 @@#include "main.h" #include "netlink.h" #include "io.h" +#include "peer.h" #include "proto.h" static const struct net_device_ops ovpn_netdev_ops = {
@@ -92,6 +93,7 @@ static int ovpn_newlink(struct net_device *dev, ovpn->dev = dev; ovpn->mode = mode; +spin_lock_init (&ovpn->lock);/* Set carrier explicitly after registration, this way state is * clearly defined.
@@ -109,6 +111,16 @@ static int ovpn_newlink(struct net_device *dev,return register_netdevice (dev); } +static void ovpn_dellink (struct net_device *dev,struct list_head *head) +{ +struct ovpn_priv *ovpn =netdev_priv (dev); + +if (ovpn->mode ==OVPN_MODE_P2P ) +ovpn_peer_release_p2p (ovpn,OVPN_DEL_PEER_REASON_TEARDOWN ); + +unregister_netdevice_queue (dev, head); +} +static int ovpn_fill_info (struct sk_buff *skb,const struct net_device *dev) {struct ovpn_priv *ovpn =netdev_priv (dev);
@@ -127,7 +139,7 @@ static struct rtnl_link_ops ovpn_link_ops = { .policy = ovpn_policy, .maxtype = IFLA_OVPN_MAX, .newlink = ovpn_newlink, - .dellink = unregister_netdevice_queue, + .dellink = ovpn_dellink, .fill_info = ovpn_fill_info, };
drivers/net/ovpn/ovpnpriv.h +4 -0
@@ -17,10 +17,14 @@* struct ovpn_priv - per ovpn interface state * @dev: the actual netdev representing the tunnel * @mode: device operation mode (i.e. p2p, mp, ..) +* @lock: protect this object +* @peer: in P2P mode, this is the only remote peer */ struct ovpn_priv {struct net_device *dev;enum ovpn_mode mode; +spinlock_t lock;/* protect writing to the ovpn_priv object */ +struct ovpn_peer __rcu *peer; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/peer.c new file +411 -0
411 lines not on this page · raw message
drivers/net/ovpn/peer.h new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 6/23 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/main.c | 3 +- drivers/net/ovpn/peer.c | 28 +++++-- drivers/net/ovpn/peer.h | 6 +- drivers/net/ovpn/socket.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/socket.h | 38 +++++++++ drivers/net/ovpn/udp.c | 75 ++++++++++++++++++ drivers/net/ovpn/udp.h | 19 +++++ include/uapi/linux/udp.h | 1 + 9 files changed, 362 insertions(+), 7 deletions(-)
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/main.c +2 -1
@@ -116,7 +116,8 @@ static void ovpn_dellink(struct net_device *dev, struct list_head *head)struct ovpn_priv *ovpn =netdev_priv (dev);if (ovpn->mode ==OVPN_MODE_P2P ) -ovpn_peer_release_p2p (ovpn,OVPN_DEL_PEER_REASON_TEARDOWN ); +ovpn_peer_release_p2p (ovpn,NULL , +OVPN_DEL_PEER_REASON_TEARDOWN );unregister_netdevice_queue (dev, head); }
drivers/net/ovpn/peer.c +23 -5
@@ -16,17 +16,20 @@#include "main.h" #include "netlink.h" #include "peer.h" +#include "socket.h" static void unlock_ovpn (struct ovpn_priv *ovpn, -struct llist_head *release_list) +struct llist_head *release_list) __releases(&ovpn->lock) {struct ovpn_peer *peer;spin_unlock_bh (&ovpn->lock); -llist_for_each_entry (peer, release_list->first, release_entry) +llist_for_each_entry (peer, release_list->first, release_entry) { +ovpn_socket_release (peer);ovpn_peer_put (peer); + } }/**
@@ -394,18 +397,33 @@ int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason)/** * ovpn_peer_release_p2p - release peer upon P2P device teardown * @ovpn: the instance being torn down +* @sk: if not NULL, release peer only if it's using this specific socket * @reason: the reason for releasing the peer */ -void ovpn_peer_release_p2p (struct ovpn_priv *ovpn, +void ovpn_peer_release_p2p (struct ovpn_priv *ovpn,struct sock *sk,enum ovpn_del_peer_reason reason) { +struct ovpn_socket *ovpn_sock;LLIST_HEAD (release_list);struct ovpn_peer *peer;spin_lock_bh (&ovpn->lock); peer =rcu_dereference_protected (ovpn->peer,lockdep_is_held (&ovpn->lock)); -if (peer) -ovpn_peer_remove (peer, reason, &release_list); +if (!peer) { +spin_unlock_bh (&ovpn->lock); +return ; + } + +if (sk) { + ovpn_sock =rcu_access_pointer (peer->sock); +if (!ovpn_sock || ovpn_sock->sock->sk != sk) { +spin_unlock_bh (&ovpn->lock); +ovpn_peer_put (peer); +return ; + } + } + +ovpn_peer_remove (peer, reason, &release_list);unlock_ovpn (ovpn, &release_list); }
drivers/net/ovpn/peer.h +5 -1
@@ -12,6 +12,8 @@#include <net/dst_cache.h> +#include "socket.h" +/** * struct ovpn_peer - the main remote peer object * @ovpn: main openvpn instance this peer belongs to
@@ -20,6 +22,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 * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..)
@@ -36,6 +39,7 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct ovpn_socket __rcu *sock;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;enum ovpn_del_peer_reason delete_reason;
@@ -70,7 +74,7 @@ static inline void ovpn_peer_put(struct ovpn_peer *peer)struct ovpn_peer *ovpn_peer_new (struct ovpn_priv *ovpn,u32 id);int ovpn_peer_add (struct ovpn_priv *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_priv *ovpn, +void ovpn_peer_release_p2p (struct ovpn_priv *ovpn,struct sock *sk,enum ovpn_del_peer_reason reason); struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn,
drivers/net/ovpn/socket.c new file +197 -0
197 lines not on this page · raw message
drivers/net/ovpn/socket.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2025 OpenVPN, Inc. +* +* Author: James Yonan <[email protected]> +* Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_SOCK_H_ +#define _NET_OVPN_SOCK_H_ + +#include <linux/net.h> +#include <linux/kref.h> +#include <net/sock.h> + +struct ovpn_priv ; +struct ovpn_peer ; + +/** +* struct ovpn_socket - a kernel socket referenced in the ovpn code +* @ovpn: ovpn instance owning this socket (UDP only) +* @sock: the low level sock object +* @refcount: amount of contexts currently referencing this object +* @rcu: member used to schedule RCU destructor callback +*/ +struct ovpn_socket { +struct ovpn_priv *ovpn; +struct socket *sock; +struct kref refcount; +struct rcu_head rcu; +}; + +struct ovpn_socket *ovpn_socket_new (struct socket *sock, +struct ovpn_peer *peer); +void ovpn_socket_release (struct ovpn_peer *peer); + +#endif /* _NET_OVPN_SOCK_H_ */
drivers/net/ovpn/udp.c new file +75 -0
75 lines not on this page · raw message
drivers/net/ovpn/udp.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2025 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_UDP_H_ +#define _NET_OVPN_UDP_H_ + +struct ovpn_priv ; +struct socket ; + +int ovpn_udp_socket_attach (struct ovpn_socket *ovpn_sock, +struct ovpn_priv *ovpn); +void ovpn_udp_socket_detach (struct ovpn_socket *ovpn_sock); + +#endif /* _NET_OVPN_UDP_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.49.0
Antonio Quartulli
maintainerPATCH v26 7/23 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/Kconfig | 1 + drivers/net/ovpn/io.c | 137 +++++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 32 +++++++ drivers/net/ovpn/peer.h | 2 + drivers/net/ovpn/skb.h | 55 ++++++++++++ drivers/net/ovpn/udp.c | 233 +++++++++++++++++++++++++++++++++++++++++++++++- drivers/net/ovpn/udp.h | 6 ++ 7 files changed, 464 insertions(+), 2 deletions(-)
drivers/net/Kconfig +1 -0
@@ -120,6 +120,7 @@ config OVPN depends on NET && INET depends on IPV6 || !IPV6 select DST_CACHE + select NET_UDP_TUNNEL help This module enhances the performance of the OpenVPN userspace software by offloading the data channel processing to kernelspace.
drivers/net/ovpn/io.c +136 -1
150 lines not on this page · raw message
drivers/net/ovpn/peer.c +32 -0
@@ -294,6 +294,38 @@ static void ovpn_peer_remove(struct ovpn_peer *peer,llist_add (&peer->release_entry, release_list); } +/** +* 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_priv *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 +2 -0
@@ -80,5 +80,7 @@ void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk,struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_priv *ovpn,struct sk_buff *skb);struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_priv *ovpn,u32 peer_id); +struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_priv *ovpn, +struct sk_buff *skb);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/skb.h new file +55 -0
55 lines not on this page · raw message
drivers/net/ovpn/udp.c +232 -1
254 lines not on this page · raw message
drivers/net/ovpn/udp.h +6 -0
@@ -9,6 +9,9 @@#ifndef _NET_OVPN_UDP_H_#define _NET_OVPN_UDP_H_ +#include <net/sock.h> + +struct ovpn_peer ;struct ovpn_priv ;struct socket ;
@@ -16,4 +19,7 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct ovpn_priv *ovpn);void ovpn_udp_socket_detach (struct ovpn_socket *ovpn_sock); +void ovpn_udp_send_skb (struct ovpn_peer *peer,struct socket *sock, +struct sk_buff *skb); +#endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 8/23 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 | 64 ++++++++++++++++++++- drivers/net/ovpn/io.h | 2 + drivers/net/ovpn/main.c | 18 ++++++ drivers/net/ovpn/ovpnpriv.h | 3 + drivers/net/ovpn/proto.h | 50 +++++++++++++++- drivers/net/ovpn/socket.c | 14 ++++- drivers/net/ovpn/socket.h | 9 ++- drivers/net/ovpn/udp.c | 135 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 290 insertions(+), 5 deletions(-)
drivers/net/ovpn/io.c +63 -1
@@ -9,15 +9,77 @@#include <linux/netdevice.h> #include <linux/skbuff.h> +#include <net/gro_cells.h> #include <net/gso.h> -#include "io.h" #include "ovpnpriv.h" #include "peer.h" +#include "io.h" +#include "netlink.h" +#include "proto.h" #include "udp.h" #include "skb.h" #include "socket.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) +{ +unsigned int pkt_len; +int ret; + +/* 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_reset_inner_headers (skb); + +/* cause packet to be "received" by the interface */ + pkt_len = skb->len; + ret =gro_cells_receive (&peer->ovpn->gro_cells, skb); +if (likely (ret ==NET_RX_SUCCESS )) +/* update RX stats with the size of decrypted packet */ +dev_dstats_rx_add (peer->ovpn->dev, pkt_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_dstats_rx_dropped (peer->ovpn->dev); +ovpn_peer_put (peer); +kfree_skb (skb); +} + +/* RX path entry point: decrypt packet 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
@@ -21,4 +21,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 +18 -0
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> +#include <net/gro_cells.h> #include <net/ip.h> #include <net/rtnetlink.h> #include <uapi/linux/if_arp.h>
@@ -21,8 +22,25 @@#include "io.h" #include "peer.h" #include "proto.h" +#include "udp.h" + +static int ovpn_net_init (struct net_device *dev) +{ +struct ovpn_priv *ovpn =netdev_priv (dev); + +return gro_cells_init (&ovpn->gro_cells, dev); +} + +static void ovpn_net_uninit (struct net_device *dev) +{ +struct ovpn_priv *ovpn =netdev_priv (dev); + +gro_cells_destroy (&ovpn->gro_cells); +}static const struct net_device_ops ovpn_netdev_ops = { + .ndo_init = ovpn_net_init, + .ndo_uninit = ovpn_net_uninit, .ndo_start_xmit = ovpn_net_xmit, };
drivers/net/ovpn/ovpnpriv.h +3 -0
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <net/gro_cells.h> #include <uapi/linux/if_link.h> #include <uapi/linux/ovpn.h>
@@ -19,12 +20,14 @@* @mode: device operation mode (i.e. p2p, mp, ..) * @lock: protect this object * @peer: in P2P mode, this is the only remote peer +* @gro_cells: pointer to the Generic Receive Offload cell */ struct ovpn_priv {struct net_device *dev;enum ovpn_mode mode;spinlock_t lock;/* protect writing to the ovpn_priv object */ struct ovpn_peer __rcu *peer; +struct gro_cells gro_cells; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/proto.h +49 -1
60 lines not on this page · raw message
drivers/net/ovpn/socket.c +12 -2
@@ -23,8 +23,10 @@ static void ovpn_socket_release_kref(struct kref *kref) struct ovpn_socket *sock = container_of(kref, struct ovpn_socket, refcount); -if (sock->sock->sk->sk_protocol ==IPPROTO_UDP ) +if (sock->sock->sk->sk_protocol ==IPPROTO_UDP ) {ovpn_udp_socket_detach (sock); +netdev_put (sock->ovpn->dev, &sock->dev_tracker); + }kfree_rcu (sock, rcu); }
@@ -179,7 +181,6 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) goto sock_release; } - ovpn_sock->ovpn = peer->ovpn; ovpn_sock->sock = sock;kref_init (&ovpn_sock->refcount);
@@ -190,6 +191,15 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) goto sock_release; } +if (sock->sk->sk_protocol ==IPPROTO_UDP ) { +/* in UDP we only link the ovpn instance since the socket is +* shared among multiple peers +*/ + ovpn_sock->ovpn = peer->ovpn; +netdev_hold (peer->ovpn->dev, &ovpn_sock->dev_tracker, +GFP_KERNEL ); + } +rcu_assign_sk_user_data (sock->sk, ovpn_sock); sock_release:release_sock (sock->sk);
drivers/net/ovpn/socket.h +8 -1
@@ -20,12 +20,19 @@ struct ovpn_peer;/** * struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) +* @dev_tracker: reference tracker for associated dev (UDP only) * @sock: the low level sock object * @refcount: amount of contexts currently referencing this object * @rcu: member used to schedule RCU destructor callback */ struct ovpn_socket { -struct ovpn_priv *ovpn; +union { +struct { +struct ovpn_priv *ovpn; +netdevice_tracker dev_tracker; + }; + }; +struct socket *sock;struct kref refcount;struct rcu_head rcu;
drivers/net/ovpn/udp.c +135 -0
172 lines not on this page · raw message
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 9/23 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/Kconfig | 4 + drivers/net/ovpn/Makefile | 3 + drivers/net/ovpn/bind.c | 9 +- drivers/net/ovpn/crypto.c | 148 +++++++++++++++++ drivers/net/ovpn/crypto.h | 139 ++++++++++++++++ drivers/net/ovpn/crypto_aead.c | 366 +++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/crypto_aead.h | 27 +++ drivers/net/ovpn/io.c | 137 +++++++++++++-- drivers/net/ovpn/io.h | 3 + drivers/net/ovpn/peer.c | 29 ++++ drivers/net/ovpn/peer.h | 5 + drivers/net/ovpn/pktid.c | 129 +++++++++++++++ drivers/net/ovpn/pktid.h | 86 ++++++++++ drivers/net/ovpn/proto.h | 32 ++++ drivers/net/ovpn/skb.h | 5 + 15 files changed, 1105 insertions(+), 17 deletions(-)
drivers/net/Kconfig +4 -0
@@ -121,6 +121,10 @@ config OVPN depends on IPV6 || !IPV6 select DST_CACHE select NET_UDP_TUNNEL + 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.
drivers/net/ovpn/Makefile +3 -0
@@ -8,10 +8,13 @@ obj-$(CONFIG_OVPN) := ovpn.o ovpn-y += bind.o +ovpn-y += crypto.o +ovpn-y += crypto_aead.o ovpn-y += main.o ovpn-y += io.o ovpn-y += netlink.o ovpn-y += netlink-gen.o ovpn-y += peer.o +ovpn-y += pktid.o ovpn-y += socket.o ovpn-y += udp.o
drivers/net/ovpn/bind.c +3 -6
@@ -48,11 +48,8 @@ struct ovpn_bind *ovpn_bind_from_sockaddr(const struct sockaddr_storage *ss)*/ void ovpn_bind_reset (struct ovpn_peer *peer,struct ovpn_bind *new) { -struct ovpn_bind *old; +lockdep_assert_held (&peer->lock); -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/crypto.c new file +148 -0
148 lines not on this page · raw message
drivers/net/ovpn/crypto.h new file +139 -0
139 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.c new file +366 -0
366 lines not on this page · raw message
drivers/net/ovpn/crypto_aead.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2025 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> + +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 +126 -11
202 lines not on this page · raw message
drivers/net/ovpn/io.h +3 -0
@@ -23,4 +23,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 (void *data,int ret); +void ovpn_decrypt_post (void *data,int ret); +#endif /* _NET_OVPN_OVPN_H_ */
drivers/net/ovpn/peer.c +29 -0
@@ -12,6 +12,8 @@#include "ovpnpriv.h" #include "bind.h" +#include "pktid.h" +#include "crypto.h" #include "io.h" #include "main.h" #include "netlink.h"
@@ -56,6 +58,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *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);
@@ -94,7 +97,10 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)*/ static void ovpn_peer_release (struct ovpn_peer *peer) { +ovpn_crypto_state_release (&peer->crypto); +spin_lock_bh (&peer->lock);ovpn_bind_reset (peer,NULL ); +spin_unlock_bh (&peer->lock);call_rcu (&peer->rcu, ovpn_peer_release_rcu);netdev_put (peer->ovpn->dev, &peer->dev_tracker); }
@@ -326,6 +332,29 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *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_priv *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 +*/ + match = (peer ==rcu_access_pointer (ovpn->peer)); + } + +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 +5 -0
@@ -12,6 +12,7 @@#include <net/dst_cache.h> +#include "crypto.h" #include "socket.h" /**
@@ -23,6 +24,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 * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..)
@@ -40,6 +42,7 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket __rcu *sock; +struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;enum ovpn_del_peer_reason delete_reason;
@@ -82,5 +85,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn,struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_priv *ovpn,u32 peer_id);struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_priv *ovpn,struct sk_buff *skb); +bool ovpn_peer_check_by_src (struct ovpn_priv *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/pktid.c new file +129 -0
129 lines not on this page · raw message
drivers/net/ovpn/pktid.h new file +86 -0
86 lines not on this page · raw message
drivers/net/ovpn/proto.h +32 -0
@@ -83,4 +83,36 @@ static inline u32 ovpn_peer_id_from_skb(const struct sk_buff *skb, u16 offset) return FIELD_GET(OVPN_OPCODE_PEERID_MASK, opcode); } +/** +* 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 4 bytes. +* +* Return: the key ID +*/ +static inline u8 ovpn_key_id_from_skb (const struct sk_buff *skb) +{ +u32 opcode =be32_to_cpu (*(__be32 *)skb->data); + +return FIELD_GET (OVPN_OPCODE_KEYID_MASK , opcode); +} + +/** +* 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) +{ +return FIELD_PREP (OVPN_OPCODE_PKTTYPE_MASK , opcode) | +FIELD_PREP (OVPN_OPCODE_KEYID_MASK , key_id) | +FIELD_PREP (OVPN_OPCODE_PEERID_MASK , peer_id); +} +#endif /* _NET_OVPN_OVPNPROTO_H_ */
drivers/net/ovpn/skb.h +5 -0
@@ -20,6 +20,11 @@struct ovpn_cb {struct ovpn_peer *peer; +struct ovpn_crypto_key_slot *ks; +struct aead_request *req; +struct scatterlist *sg; +u8 *iv; +unsigned int payload_offset; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 10/23 ovpn: store tunnel and transport statistics
In reply to Antonio QuartulliByte/packet counters for in-tunnel and transport streams are now initialized and updated as needed.
To be exported via netlink.
--- drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 12 +++++++++++- drivers/net/ovpn/peer.c | 2 ++ drivers/net/ovpn/peer.h | 5 +++++ drivers/net/ovpn/stats.c | 21 +++++++++++++++++++++ drivers/net/ovpn/stats.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 1 deletion(-)
drivers/net/ovpn/Makefile +1 -0
@@ -17,4 +17,5 @@ ovpn-y += netlink-gen.o ovpn-y += peer.o ovpn-y += pktid.o ovpn-y += socket.o +ovpn-y += stats.o ovpn-y += udp.o
drivers/net/ovpn/io.c +11 -1
@@ -12,6 +12,7 @@#include <linux/skbuff.h> #include <net/gro_cells.h> #include <net/gso.h> +#include <net/ip.h> #include "ovpnpriv.h" #include "peer.h"
@@ -55,9 +56,11 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)/* cause packet to be "received" by the interface */ pkt_len = skb->len; ret =gro_cells_receive (&peer->ovpn->gro_cells, skb); -if (likely (ret ==NET_RX_SUCCESS )) +if (likely (ret ==NET_RX_SUCCESS )) {/* update RX stats with the size of decrypted packet */ +ovpn_peer_stats_increment_rx (&peer->vpn_stats, pkt_len);dev_dstats_rx_add (peer->ovpn->dev, pkt_len); + } }void ovpn_decrypt_post (void *data,int ret)
@@ -152,6 +155,8 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb)struct ovpn_crypto_key_slot *ks;u8 key_id; +ovpn_peer_stats_increment_rx (&peer->link_stats, skb->len); +/* get the key slot matching the key ID in the received packet */ key_id =ovpn_key_id_from_skb (skb); ks =ovpn_crypto_key_id_to_slot (&peer->crypto, key_id);
@@ -175,6 +180,7 @@ void ovpn_encrypt_post(void *data, int ret)struct sk_buff *skb = data;struct ovpn_socket *sock;struct ovpn_peer *peer; +unsigned int orig_len;/* encryption is happening asynchronously. This function will be * called later by the crypto callback with a proper return value
@@ -194,6 +200,7 @@ void ovpn_encrypt_post(void *data, int ret) goto err;skb_mark_not_on_list (skb); + orig_len = skb->len;rcu_read_lock (); sock =rcu_dereference (peer->sock);
@@ -208,6 +215,8 @@ void ovpn_encrypt_post(void *data, int ret)/* no transport configured yet */ goto err_unlock; } + +ovpn_peer_stats_increment_tx (&peer->link_stats, orig_len);/* skb passed down the stack - don't free it */ skb =NULL ; err_unlock:
@@ -322,6 +331,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) goto drop; } +ovpn_peer_stats_increment_tx (&peer->vpn_stats, skb->len);ovpn_send (ovpn, skb_list.next, peer);return NETDEV_TX_OK ;
drivers/net/ovpn/peer.c +2 -0
@@ -61,6 +61,8 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *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 +5 -0
@@ -14,6 +14,7 @@#include "crypto.h" #include "socket.h" +#include "stats.h" /** * struct ovpn_peer - the main remote peer object
@@ -27,6 +28,8 @@* @crypto: the crypto configuration (ciphers, keys, etc..) * @dst_cache: cache for dst_entry used to send to peer * @bind: remote peer binding +* @vpn_stats: per-peer in-VPN TX/RX stats +* @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
@@ -45,6 +48,8 @@ struct ovpn_peer {struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind; +struct ovpn_peer_stats vpn_stats; +struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind */ struct kref refcount;
drivers/net/ovpn/stats.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +/* OpenVPN data channel offload +* +* Copyright (C) 2020-2025 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.49.0
Antonio Quartulli
maintainerPATCH v26 11/23 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.
Note that ovpn redefines sk_prot and sk_socket->ops for the TCP socket used to communicate with the peer.
For this reason it needs to access inet6_stream_ops, which is declared as extern in the IPv6 module, but it is not fully exported.
Therefore this patch is also adding EXPORT_SYMBOL_GPL(inet6_stream_ops) to net/ipv6/af_inet6.c.
--- drivers/net/Kconfig | 1 + drivers/net/ovpn/Makefile | 1 + drivers/net/ovpn/io.c | 4 + drivers/net/ovpn/main.c | 3 + drivers/net/ovpn/ovpnpriv.h | 1 + drivers/net/ovpn/peer.h | 35 +++ drivers/net/ovpn/socket.c | 44 +++- drivers/net/ovpn/socket.h | 8 +- drivers/net/ovpn/tcp.c | 594 ++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/tcp.h | 36 +++ net/ipv6/af_inet6.c | 1 + 11 files changed, 717 insertions(+), 11 deletions(-)
drivers/net/Kconfig +1 -0
@@ -125,6 +125,7 @@ config OVPN select CRYPTO_AES select CRYPTO_GCM select CRYPTO_CHACHA20POLY1305 + select STREAM_PARSER help This module enhances the performance of the OpenVPN userspace software by offloading the data channel processing to kernelspace.
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
@@ -22,6 +22,7 @@#include "crypto_aead.h" #include "netlink.h" #include "proto.h" +#include "tcp.h" #include "udp.h" #include "skb.h" #include "socket.h"
@@ -211,6 +212,9 @@ void ovpn_encrypt_post(void *data, int ret)case IPPROTO_UDP :ovpn_udp_send_skb (peer, sock->sock, skb);break ; +case IPPROTO_TCP : +ovpn_tcp_send_skb (peer, sock->sock, skb); +break ;default :/* no transport configured yet */ goto err_unlock;
drivers/net/ovpn/main.c +3 -0
@@ -22,6 +22,7 @@#include "io.h" #include "peer.h" #include "proto.h" +#include "tcp.h" #include "udp.h" static int ovpn_net_init (struct net_device *dev)
@@ -177,6 +178,8 @@ static int __init ovpn_init(void) goto unreg_rtnl; } +ovpn_tcp_init (); +return 0 ; unreg_rtnl:
drivers/net/ovpn/ovpnpriv.h +1 -0
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNSTRUCT_H_#define _NET_OVPN_OVPNSTRUCT_H_ +#include <linux/workqueue.h> #include <net/gro_cells.h> #include <uapi/linux/if_link.h> #include <uapi/linux/ovpn.h>
drivers/net/ovpn/peer.h +35 -0
@@ -11,6 +11,7 @@#define _NET_OVPN_OVPNPEER_H_#include <net/dst_cache.h> +#include <net/strparser.h> #include "crypto.h" #include "socket.h"
@@ -25,6 +26,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.user_queue: received packets that have to go to userspace (TCP only) +* @tcp.out_queue: packets on hold while socket is taken by user (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
@@ -45,6 +58,28 @@ struct ovpn_peer {struct in6_addr ipv6; } vpn_addrs;struct ovpn_socket __rcu *sock; + +struct { +struct strparser strp; +struct sk_buff_head user_queue; +struct sk_buff_head out_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; + +struct work_struct defer_del_work; + } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;struct ovpn_bind __rcu *bind;
drivers/net/ovpn/socket.c +35 -9
93 lines not on this page · raw message
drivers/net/ovpn/socket.h +6 -2
@@ -21,9 +21,11 @@ struct ovpn_peer;* struct ovpn_socket - a kernel socket referenced in the ovpn code * @ovpn: ovpn instance owning this socket (UDP only) * @dev_tracker: reference tracker for associated dev (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 +* @work: member used to schedule release routine (it may block) +* @tcp_tx_work: work for deferring outgoing packet processing (TCP only) */ struct ovpn_socket { union {
@@ -31,11 +33,13 @@ struct ovpn_socket {struct ovpn_priv *ovpn;netdevice_tracker dev_tracker; }; +struct ovpn_peer *peer; };struct socket *sock;struct kref refcount; -struct rcu_head rcu; +struct work_struct work; +struct work_struct tcp_tx_work; }; struct ovpn_socket *ovpn_socket_new(struct socket *sock,
drivers/net/ovpn/tcp.c new file +594 -0
594 lines not on this page · raw message
drivers/net/ovpn/tcp.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* OpenVPN data channel offload +* +* Copyright (C) 2019-2025 OpenVPN, Inc. +* +* Author: Antonio Quartulli <[email protected]> +*/ + +#ifndef _NET_OVPN_TCP_H_ +#define _NET_OVPN_TCP_H_ + +#include <linux/net.h> +#include <linux/skbuff.h> +#include <linux/types.h> + +#include "peer.h" +#include "skb.h" +#include "socket.h" + +void __init ovpn_tcp_init (void ); + +int ovpn_tcp_socket_attach (struct ovpn_socket *ovpn_sock, +struct ovpn_peer *peer); +void ovpn_tcp_socket_detach (struct ovpn_socket *ovpn_sock); +void ovpn_tcp_socket_wait_finish (struct ovpn_socket *sock); + +/* Prepare skb and enqueue it for sending to peer. +* +* Preparation consist in prepending the skb payload with its size. +* Required by the OpenVPN protocol in order to extract packets from +* the TCP stream on the receiver side. +*/ +void ovpn_tcp_send_skb (struct ovpn_peer *peer,struct socket *sock,struct sk_buff *skb); +void ovpn_tcp_tx_work (struct work_struct *work); + +#endif /* _NET_OVPN_TCP_H_ */
net/ipv6/af_inet6.c +1 -0
@@ -715,6 +715,7 @@ const struct proto_ops inet6_stream_ops = {#endif .set_rcvlowat = tcp_set_rcvlowat, }; +EXPORT_SYMBOL_GPL (inet6_stream_ops);const struct proto_ops inet6_dgram_ops = { .family =PF_INET6 ,
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 12/23 skb: implement skb_send_sock_locked_with_flags()
In reply to Antonio QuartulliWhen sending an skb over a socket using skb_send_sock_locked(), it is currently not possible to specify any flag to be set in msghdr->msg_flags.
However, we may want to pass flags the user may have specified, like MSG_NOSIGNAL.
Extend __skb_send_sock() with a new argument 'flags' and add a new interface named skb_send_sock_locked_with_flags().
--- include/linux/skbuff.h | 2 ++ net/core/skbuff.c | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-)
include/linux/skbuff.h +2 -0
@@ -4145,6 +4145,8 @@ int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, unsigned int flags);int skb_send_sock_locked (struct sock *sk,struct sk_buff *skb,int offset,int len); +int skb_send_sock_locked_with_flags (struct sock *sk,struct sk_buff *skb, +int offset,int len,int flags);int skb_send_sock (struct sock *sk,struct sk_buff *skb,int offset,int len);void skb_copy_and_csum_dev (const struct sk_buff *skb,u8 *to);unsigned int skb_zerocopy_headlen (const struct sk_buff *from);
net/core/skbuff.c +13 -5
@@ -3227,7 +3227,7 @@ static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg)typedef int (*sendmsg_func )(struct sock *sk,struct msghdr *msg);static int __skb_send_sock (struct sock *sk,struct sk_buff *skb,int offset, -int len,sendmsg_func sendmsg) +int len,sendmsg_func sendmsg,int flags) {unsigned int orig_len = len;struct sk_buff *head = skb;
@@ -3245,7 +3245,7 @@ static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, kv.iov_base = skb->data + offset; kv.iov_len = slen;memset (&msg,0 ,sizeof (msg)); - msg.msg_flags =MSG_DONTWAIT ; + msg.msg_flags =MSG_DONTWAIT | flags;iov_iter_kvec (&msg.msg_iter,ITER_SOURCE , &kv,1 , slen); ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
@@ -3282,7 +3282,8 @@ static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,while (slen) {struct bio_vec bvec;struct msghdr msg = { - .msg_flags =MSG_SPLICE_PAGES |MSG_DONTWAIT , + .msg_flags =MSG_SPLICE_PAGES |MSG_DONTWAIT | + flags, }; bvec_set_page(&bvec, skb_frag_page(frag), slen,
@@ -3328,14 +3329,21 @@ static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,int skb_send_sock_locked (struct sock *sk,struct sk_buff *skb,int offset,int len) { -return __skb_send_sock (sk, skb, offset, len, sendmsg_locked); +return __skb_send_sock (sk, skb, offset, len, sendmsg_locked,0 ); }EXPORT_SYMBOL_GPL (skb_send_sock_locked); +int skb_send_sock_locked_with_flags (struct sock *sk,struct sk_buff *skb, +int offset,int len,int flags) +{ +return __skb_send_sock (sk, skb, offset, len, sendmsg_locked, flags); +} +EXPORT_SYMBOL_GPL (skb_send_sock_locked_with_flags); +/* Send skb data on a socket. Socket must be unlocked. */ int skb_send_sock (struct sock *sk,struct sk_buff *skb,int offset,int len) { -return __skb_send_sock (sk, skb, offset, len, sendmsg_unlocked); +return __skb_send_sock (sk, skb, offset, len, sendmsg_unlocked,0 ); }/**
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 13/23 ovpn: add support for MSG_NOSIGNAL in tcp_sendmsg
In reply to Antonio QuartulliUserspace may want to pass the MSG_NOSIGNAL flag to tcp_sendmsg() in order to avoid generating a SIGPIPE.
To pass this flag down the TCP stack a new skb sending API accepting a flags argument is introduced.
--- drivers/net/ovpn/skb.h | 1 + drivers/net/ovpn/tcp.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/net/ovpn/skb.h +1 -0
@@ -25,6 +25,7 @@ struct ovpn_cb {struct scatterlist *sg;u8 *iv;unsigned int payload_offset; +bool nosignal; };static inline struct ovpn_cb *ovpn_skb_cb (struct sk_buff *skb)
drivers/net/ovpn/tcp.c +8 -4
@@ -220,6 +220,7 @@ void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock)static void ovpn_tcp_send_sock (struct ovpn_peer *peer,struct sock *sk) {struct sk_buff *skb = peer->tcp.out_msg.skb; +int ret, flags;if (!skb)return ;
@@ -230,9 +231,11 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk) peer->tcp.tx_in_progress = true; do { - int ret = skb_send_sock_locked(sk, skb, - peer->tcp.out_msg.offset, - peer->tcp.out_msg.len); + flags = ovpn_skb_cb(skb)->nosignal ? MSG_NOSIGNAL : 0; + ret = skb_send_sock_locked_with_flags(sk, skb, + peer->tcp.out_msg.offset, + peer->tcp.out_msg.len, + flags); if (unlikely(ret < 0)) { if (ret == -EAGAIN) goto out;
@@ -380,7 +383,7 @@ static int ovpn_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)rcu_read_unlock (); peer = sock->peer; -if (msg->msg_flags & ~MSG_DONTWAIT ) { +if (msg->msg_flags & ~(MSG_DONTWAIT |MSG_NOSIGNAL )) { ret = -EOPNOTSUPP ; goto peer_free; }
@@ -413,6 +416,7 @@ static int ovpn_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) goto peer_free; } +ovpn_skb_cb (skb)->nosignal = msg->msg_flags &MSG_NOSIGNAL ;ovpn_tcp_send_sock_skb (peer, sk, skb); ret = size; peer_free:
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 14/23 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 | 64 +++++++++++++-- drivers/net/ovpn/ovpnpriv.h | 19 +++++ drivers/net/ovpn/peer.c | 189 ++++++++++++++++++++++++++++++++++++++++++-- drivers/net/ovpn/peer.h | 12 ++- drivers/net/ovpn/udp.c | 4 +- 5 files changed, 272 insertions(+), 16 deletions(-)
drivers/net/ovpn/main.c +59 -5
@@ -25,11 +25,66 @@#include "tcp.h" #include "udp.h" +static void ovpn_priv_free (struct net_device *net) +{ +struct ovpn_priv *ovpn =netdev_priv (net); + +kfree (ovpn->peers); +} + +static int ovpn_mp_alloc (struct ovpn_priv *ovpn) +{ +struct in_device *dev_v4; +int i; + +if (ovpn->mode !=OVPN_MODE_MP ) +return 0 ; + + dev_v4 =__in_dev_get_rtnl (ovpn->dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly. +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (ovpn->dev),SEND_REDIRECTS ) = false; + } + +/* the peer container is fairly large, therefore we allocate it only in +* MP mode +*/ + ovpn->peers =kzalloc (sizeof (*ovpn->peers),GFP_KERNEL ); +if (!ovpn->peers) +return -ENOMEM ; + +for (i =0 ; i <ARRAY_SIZE (ovpn->peers->by_id); i++) { +INIT_HLIST_HEAD (&ovpn->peers->by_id[i]); +INIT_HLIST_NULLS_HEAD (&ovpn->peers->by_vpn_addr4[i], i); +INIT_HLIST_NULLS_HEAD (&ovpn->peers->by_vpn_addr6[i], i); +INIT_HLIST_NULLS_HEAD (&ovpn->peers->by_transp_addr[i], i); + } + +return 0 ; +} +static int ovpn_net_init (struct net_device *dev) {struct ovpn_priv *ovpn =netdev_priv (dev); +int err =gro_cells_init (&ovpn->gro_cells, dev); + +if (err <0 ) +return err; + + err =ovpn_mp_alloc (ovpn); +if (err <0 ) { +gro_cells_destroy (&ovpn->gro_cells); +return err; + } -return gro_cells_init (&ovpn->gro_cells, dev); +return 0 ; }static void ovpn_net_uninit (struct net_device *dev)
@@ -76,6 +131,8 @@ static void ovpn_setup(struct net_device *dev) dev->netdev_ops = &ovpn_netdev_ops; + dev->priv_destructor = ovpn_priv_free; + dev->hard_header_len =0 ; dev->addr_len =0 ; dev->mtu =ETH_DATA_LEN -OVPN_HEAD_ROOM ;
@@ -134,10 +191,7 @@ static void ovpn_dellink(struct net_device *dev, struct list_head *head) {struct ovpn_priv *ovpn =netdev_priv (dev); -if (ovpn->mode ==OVPN_MODE_P2P ) -ovpn_peer_release_p2p (ovpn,NULL , -OVPN_DEL_PEER_REASON_TEARDOWN ); - +ovpn_peers_free (ovpn,NULL ,OVPN_DEL_PEER_REASON_TEARDOWN );unregister_netdevice_queue (dev, head); }
drivers/net/ovpn/ovpnpriv.h +19 -0
@@ -15,11 +15,29 @@#include <uapi/linux/if_link.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_addr4: table of peers indexed by VPN IPv4 address (items can be +* rehashed on the fly due to peer IP change) +* @by_vpn_addr6: table of peers indexed by VPN IPv6 address (items can be +* rehashed on the fly due to peer IP change) +* @by_transp_addr: table of peers indexed by transport address (items can be +* rehashed on the fly due to peer IP change) +*/ +struct ovpn_peer_collection { + DECLARE_HASHTABLE(by_id, 12); +struct hlist_nulls_head by_vpn_addr4[1 <<12 ]; +struct hlist_nulls_head by_vpn_addr6[1 <<12 ]; +struct hlist_nulls_head by_transp_addr[1 <<12 ]; +}; +/** * struct ovpn_priv - per ovpn interface state * @dev: the actual netdev representing the tunnel * @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 * @gro_cells: pointer to the Generic Receive Offload cell */
@@ -27,6 +45,7 @@ struct ovpn_priv {struct net_device *dev;enum ovpn_mode mode;spinlock_t lock;/* protect writing to the ovpn_priv object */ +struct ovpn_peer_collection *peers;struct ovpn_peer __rcu *peer;struct gro_cells gro_cells; };
drivers/net/ovpn/peer.c +183 -6
238 lines not on this page · raw message
drivers/net/ovpn/peer.h +10 -2
@@ -25,6 +25,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)
@@ -57,6 +61,10 @@ struct ovpn_peer {struct in_addr ipv4;struct in6_addr ipv6; } vpn_addrs; +struct hlist_node hash_entry_id; +struct hlist_nulls_node hash_entry_addr4; +struct hlist_nulls_node hash_entry_addr6; +struct hlist_nulls_node hash_entry_transp_addr;struct ovpn_socket __rcu *sock;struct {
@@ -117,8 +125,8 @@ static inline void ovpn_peer_put(struct ovpn_peer *peer)struct ovpn_peer *ovpn_peer_new (struct ovpn_priv *ovpn,u32 id);int ovpn_peer_add (struct ovpn_priv *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_priv *ovpn,struct sock *sk, -enum ovpn_del_peer_reason reason); +void ovpn_peers_free (struct ovpn_priv *ovpn,struct sock *sock, +enum ovpn_del_peer_reason reason);struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_priv *ovpn,struct sk_buff *skb);
drivers/net/ovpn/udp.c +1 -3
@@ -367,9 +367,7 @@ static void ovpn_udp_encap_destroy(struct sock *sk) ovpn = sock->ovpn;rcu_read_unlock (); -if (ovpn->mode ==OVPN_MODE_P2P ) -ovpn_peer_release_p2p (ovpn, sk, -OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT ); +ovpn_peers_free (ovpn, sk,OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT ); }/**
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 15/23 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 | 301 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 291 insertions(+), 10 deletions(-)
drivers/net/ovpn/peer.c +291 -10
@@ -10,6 +10,7 @@#include <linux/skbuff.h> #include <linux/list.h> #include <linux/hashtable.h> +#include <net/ip6_route.h> #include "ovpnpriv.h" #include "bind.h"
@@ -150,6 +151,121 @@ static int ovpn_peer_skb_to_sockaddr(struct sk_buff *skb,return -1 ; } +/** +* 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; +} + +/* variable name __tbl2 needs to be different from __tbl1 +* in the macro below to avoid confusing clang +*/ +#define ovpn_get_hash_slot (_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl2 = &(_tbl); \ + jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl2); \ +}) + +#define ovpn_get_hash_head (_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl1 = &(_tbl); \ + &(*__tbl1)[ovpn_get_hash_slot(*__tbl1, _key, _key_len)];\ +}) + +/** +* 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_priv *ovpn, +__be32 addr) +{ +struct hlist_nulls_head *nhead; +struct hlist_nulls_node *ntmp; +struct ovpn_peer *tmp; +unsigned int slot; + +begin: + slot =ovpn_get_hash_slot (ovpn->peers->by_vpn_addr4, &addr, +sizeof (addr)); + nhead = &ovpn->peers->by_vpn_addr4[slot]; + +hlist_nulls_for_each_entry_rcu (tmp, ntmp, nhead, hash_entry_addr4) +if (addr == tmp->vpn_addrs.ipv4.s_addr) +return tmp; + +/* item may have moved during lookup - check nulls and restart +* if that's the case +*/ +if (get_nulls_value (ntmp) != slot) + goto begin; + +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_priv *ovpn, +struct in6_addr *addr) +{ +struct hlist_nulls_head *nhead; +struct hlist_nulls_node *ntmp; +struct ovpn_peer *tmp; +unsigned int slot; + +begin: + slot =ovpn_get_hash_slot (ovpn->peers->by_vpn_addr6, addr, +sizeof (*addr)); + nhead = &ovpn->peers->by_vpn_addr6[slot]; + +hlist_nulls_for_each_entry_rcu (tmp, ntmp, nhead, hash_entry_addr6) +if (ipv6_addr_equal (addr, &tmp->vpn_addrs.ipv6)) +return tmp; + +/* item may have moved during lookup - check nulls and restart +* if that's the case +*/ +if (get_nulls_value (ntmp) != slot) + goto begin; + +return NULL ; +} +/** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from
@@ -227,14 +343,43 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_priv *ovpn,struct ovpn_peer *ovpn_peer_get_by_transp_addr (struct ovpn_priv *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; +unsigned int slot; +ssize_t sa_len; -if (unlikely (!ovpn_peer_skb_to_sockaddr (skb, &ss))) + sa_len =ovpn_peer_skb_to_sockaddr (skb, &ss); +if (unlikely (sa_len <0 ))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); + +rcu_read_lock (); +begin: + slot =ovpn_get_hash_slot (ovpn->peers->by_transp_addr, &ss, sa_len); + nhead = &ovpn->peers->by_transp_addr[slot]; + +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 ; + } + +/* item may have moved during lookup - check nulls and restart +* if that's the case +*/ +if (!peer &&get_nulls_value (ntmp) != slot) + goto begin; +rcu_read_unlock ();return peer; }
@@ -269,10 +414,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_priv *ovpn,*/ struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_priv *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; }
@@ -330,6 +492,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *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
@@ -340,11 +504,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn,if (unlikely (peer && !ovpn_peer_hold (peer))) peer =NULL ;rcu_read_unlock (); +return peer; } +rcu_read_lock (); +switch (skb->protocol) { +case htons (ETH_P_IP ): + addr4 =ovpn_nexthop_from_skb4 (skb); + peer =ovpn_peer_get_by_vpn_addr4 (ovpn, addr4); +break ; +case htons (ETH_P_IPV6 ): + 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_priv *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" , +netdev_name (ovpn->dev), &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_priv *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" , +netdev_name (ovpn->dev), &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
@@ -357,21 +619,40 @@ bool ovpn_peer_check_by_src(struct ovpn_priv *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 * sent to the single peer listening on the other side */ - match = (peer ==rcu_access_pointer (ovpn->peer)); +return peer ==rcu_access_pointer (ovpn->peer); + } + +/* 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) { +case htons (ETH_P_IP ): + 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 htons (ETH_P_IPV6 ): + 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.49.0
Antonio Quartulli
maintainerPATCH v26 16/23 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 | 78 ++++++++++++++++- drivers/net/ovpn/io.h | 5 ++ drivers/net/ovpn/main.c | 2 + drivers/net/ovpn/ovpnpriv.h | 2 + drivers/net/ovpn/peer.c | 206 ++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/peer.h | 21 ++++- 6 files changed, 311 insertions(+), 3 deletions(-)
drivers/net/ovpn/io.c +77 -1
@@ -27,6 +27,33 @@#include "skb.h" #include "socket.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. */
@@ -101,6 +128,9 @@ void ovpn_decrypt_post(void *data, int ret) goto drop; } +/* keep track of last received authenticated packet for keepalive */ +WRITE_ONCE (peer->last_recv,ktime_get_real_seconds ()); +/* point to encapsulated IP packet */ __skb_pull (skb, payload_offset);
@@ -118,6 +148,15 @@ void ovpn_decrypt_post(void *data, int ret) goto drop; } +if (ovpn_is_keepalive (skb)) { +net_dbg_ratelimited ("%s: ping received from peer %u\n" , +netdev_name (peer->ovpn->dev), + peer->id); +/* we drop the packet, but this is not a failure */ +consume_skb (skb); + goto drop_nocount; + } +net_info_ratelimited ("%s: unsupported protocol received from peer %u\n" ,netdev_name (peer->ovpn->dev), peer->id); goto drop;
@@ -143,11 +182,12 @@ void ovpn_decrypt_post(void *data, int ret) drop:if (unlikely (skb))dev_dstats_rx_dropped (peer->ovpn->dev); +kfree_skb (skb); +drop_nocount:if (likely (peer))ovpn_peer_put (peer);if (likely (ks))ovpn_crypto_key_slot_put (ks); -kfree_skb (skb); }/* RX path entry point: decrypt packet and forward it to the device */
@@ -221,6 +261,8 @@ void ovpn_encrypt_post(void *data, int ret) }ovpn_peer_stats_increment_tx (&peer->link_stats, orig_len); +/* keep track of last sent packet for keepalive */ +WRITE_ONCE (peer->last_sent,ktime_get_real_seconds ());/* skb passed down the stack - don't free it */ skb =NULL ; err_unlock:
@@ -346,3 +388,37 @@ 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, which will be +* passed to ovpn_send() +*/ +void ovpn_xmit_special (struct ovpn_peer *peer,const void *data, +const unsigned int len) +{ +struct ovpn_priv *ovpn; +struct sk_buff *skb; + + ovpn = peer->ovpn; +if (unlikely (!ovpn)) { +ovpn_peer_put (peer); +return ; + } + + skb =alloc_skb (256 + len,GFP_ATOMIC ); +if (unlikely (!skb)) { +ovpn_peer_put (peer); +return ; + } + +skb_reserve (skb,128 ); + skb->priority =TC_PRIO_BESTEFFORT ; +__skb_put_data (skb, data, len); + +ovpn_send (ovpn, skb, peer); +}
drivers/net/ovpn/io.h +5 -0
@@ -19,9 +19,14 @@/* max padding required by encryption */ #define OVPN_MAX_PADDING 16 +#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 (void *data,int ret);void ovpn_decrypt_post (void *data,int ret);
drivers/net/ovpn/main.c +2 -0
@@ -170,6 +170,7 @@ static int ovpn_newlink(struct net_device *dev, ovpn->dev = dev; ovpn->mode = mode;spin_lock_init (&ovpn->lock); +INIT_DELAYED_WORK (&ovpn->keepalive_work, ovpn_peer_keepalive_work);/* Set carrier explicitly after registration, this way state is * clearly defined.
@@ -191,6 +192,7 @@ static void ovpn_dellink(struct net_device *dev, struct list_head *head) {struct ovpn_priv *ovpn =netdev_priv (dev); +cancel_delayed_work_sync (&ovpn->keepalive_work);ovpn_peers_free (ovpn,NULL ,OVPN_DEL_PEER_REASON_TEARDOWN );unregister_netdevice_queue (dev, head); }
drivers/net/ovpn/ovpnpriv.h +2 -0
@@ -40,6 +40,7 @@ struct ovpn_peer_collection {* @peers: data structures holding multi-peer references * @peer: in P2P mode, this is the only remote peer * @gro_cells: pointer to the Generic Receive Offload cell +* @keepalive_work: struct used to schedule keepalive periodic job */ struct ovpn_priv {struct net_device *dev;
@@ -48,6 +49,7 @@ struct ovpn_priv {struct ovpn_peer_collection *peers;struct ovpn_peer __rcu *peer;struct gro_cells gro_cells; + struct delayed_work keepalive_work; };#endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/peer.c +206 -0
221 lines not on this page · raw message
drivers/net/ovpn/peer.h +19 -2
@@ -45,13 +45,20 @@* @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 * @vpn_stats: per-peer in-VPN TX/RX stats * @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) +* @lock: protects binding to peer (bind) and keepalive* fields * @refcount: reference counter * @rcu: used to free peer in an RCU safe way * @release_entry: entry for the socket release list +* @keepalive_work: used to schedule keepalive sending */ struct ovpn_peer {struct ovpn_priv *ovpn;
@@ -91,13 +98,20 @@ 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;struct ovpn_peer_stats vpn_stats;struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason; -spinlock_t lock;/* protects bind */ +spinlock_t lock;/* protects bind and keepalive* */ struct kref refcount;struct rcu_head rcu;struct llist_node release_entry; +struct work_struct keepalive_work; };/**
@@ -136,4 +150,7 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn,bool ovpn_peer_check_by_src (struct ovpn_priv *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_ */
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 17/23 ovpn: add support for updating local or remote UDP endpoint
In reply to Antonio QuartulliIn case of UDP links, the local or remote endpoint used to communicate with a given peer may change without a connection restart.
Add support for learning the new address in case of change.
--- drivers/net/ovpn/io.c | 8 ++ drivers/net/ovpn/peer.c | 213 +++++++++++++++++++++++++++++++++++++++++++++--- drivers/net/ovpn/peer.h | 2 + 3 files changed, 210 insertions(+), 13 deletions(-)
drivers/net/ovpn/io.c +8 -0
@@ -96,6 +96,7 @@ void ovpn_decrypt_post(void *data, int ret)struct ovpn_crypto_key_slot *ks;unsigned int payload_offset =0 ;struct sk_buff *skb = data; +struct ovpn_socket *sock;struct ovpn_peer *peer;__be16 proto;__be32 *pid;
@@ -131,6 +132,13 @@ void ovpn_decrypt_post(void *data, int ret)/* keep track of last received authenticated packet for keepalive */ WRITE_ONCE (peer->last_recv,ktime_get_real_seconds ()); +rcu_read_lock (); + sock =rcu_dereference (peer->sock); +if (sock && sock->sock->sk->sk_protocol ==IPPROTO_UDP ) +/* check if this peer changed local or remote endpoint */ +ovpn_peer_endpoints_update (peer, skb); +rcu_read_unlock (); +/* point to encapsulated IP packet */ __skb_pull (skb, payload_offset);
drivers/net/ovpn/peer.c +200 -13
225 lines not on this page · raw message
drivers/net/ovpn/peer.h +2 -0
@@ -153,4 +153,6 @@ bool ovpn_peer_check_by_src(struct ovpn_priv *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_endpoints_update (struct ovpn_peer *peer,struct sk_buff *skb); +#endif /* _NET_OVPN_OVPNPEER_H_ */
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 18/23 ovpn: implement peer add/get/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 | 686 ++++++++++++++++++++++++++++++++++++++++++++- drivers/net/ovpn/peer.c | 79 ++++-- drivers/net/ovpn/peer.h | 5 + drivers/net/ovpn/socket.c | 4 +- 4 files changed, 738 insertions(+), 36 deletions(-)
drivers/net/ovpn/netlink.c +681 -5
@@ -7,6 +7,7 @@*/ #include <linux/netdevice.h> +#include <linux/types.h> #include <net/genetlink.h> #include <uapi/linux/ovpn.h>
@@ -15,6 +16,9 @@#include "main.h" #include "netlink.h" #include "netlink-gen.h" +#include "bind.h" +#include "peer.h" +#include "socket.h" MODULE_ALIAS_GENL_FAMILY (OVPN_FAMILY_NAME );
@@ -89,29 +93,701 @@ void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb,netdev_put (ovpn->dev, tracker); } +static bool ovpn_nl_attr_sockaddr_remote (struct nlattr **attrs, +struct sockaddr_storage *ss) +{ +struct sockaddr_in6 *sin6; +struct sockaddr_in *sin; +struct in6_addr *in6; +__be16 port =0 ; +__be32 *in; + + ss->ss_family =AF_UNSPEC ; + +if (attrs[OVPN_A_PEER_REMOTE_PORT ]) + port =nla_get_be16 (attrs[OVPN_A_PEER_REMOTE_PORT ]); + +if (attrs[OVPN_A_PEER_REMOTE_IPV4 ]) { + ss->ss_family =AF_INET ; + in =nla_data (attrs[OVPN_A_PEER_REMOTE_IPV4 ]); + }else if (attrs[OVPN_A_PEER_REMOTE_IPV6 ]) { + ss->ss_family =AF_INET6 ; + in6 =nla_data (attrs[OVPN_A_PEER_REMOTE_IPV6 ]); + }else { +return false; + } + +switch (ss->ss_family) { +case AF_INET6 : +/* If this is a regular IPv6 just break and move on, +* otherwise switch to AF_INET and extract the IPv4 accordingly +*/ +if (!ipv6_addr_v4mapped (in6)) { + sin6 = (struct sockaddr_in6 *)ss; + sin6->sin6_port = port; +memcpy (&sin6->sin6_addr, in6,sizeof (*in6)); +break ; + } + +/* v4-mapped-v6 address */ + ss->ss_family =AF_INET ; + in = &in6->s6_addr32[3 ]; + fallthrough; +case AF_INET : + sin = (struct sockaddr_in *)ss; + sin->sin_port = port; + sin->sin_addr.s_addr = *in; +break ; + } + +return true; +} + +static u8 *ovpn_nl_attr_local_ip (struct nlattr **attrs) +{ +u8 *addr6; + +if (!attrs[OVPN_A_PEER_LOCAL_IPV4 ] && !attrs[OVPN_A_PEER_LOCAL_IPV6 ]) +return NULL ; + +if (attrs[OVPN_A_PEER_LOCAL_IPV4 ]) +return nla_data (attrs[OVPN_A_PEER_LOCAL_IPV4 ]); + + addr6 =nla_data (attrs[OVPN_A_PEER_LOCAL_IPV6 ]); +/* this is an IPv4-mapped IPv6 address, therefore extract the actual +* v4 address from the last 4 bytes +*/ +if (ipv6_addr_v4mapped ((struct in6_addr *)addr6)) +return addr6 +12 ; + +return addr6; +} + +static sa_family_t ovpn_nl_family_get (struct nlattr *addr4, +struct nlattr *addr6) +{ +if (addr4) +return AF_INET ; + +if (addr6) { +if (ipv6_addr_v4mapped ((struct in6_addr *)nla_data (addr6))) +return AF_INET ; +return AF_INET6 ; + } + +return AF_UNSPEC ; +} + +static int ovpn_nl_peer_precheck (struct ovpn_priv *ovpn, +struct genl_info *info, +struct nlattr **attrs) +{ +sa_family_t local_fam, remote_fam; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_ID )) +return -EINVAL ; + +if (attrs[OVPN_A_PEER_REMOTE_IPV4 ] && attrs[OVPN_A_PEER_REMOTE_IPV6 ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify both remote IPv4 or IPv6 address" ); +return -EINVAL ; + } + +if (!attrs[OVPN_A_PEER_REMOTE_IPV4 ] && + !attrs[OVPN_A_PEER_REMOTE_IPV6 ] && attrs[OVPN_A_PEER_REMOTE_PORT ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify remote port without IP address" ); +return -EINVAL ; + } + +if ((attrs[OVPN_A_PEER_REMOTE_IPV4 ] || + attrs[OVPN_A_PEER_REMOTE_IPV6 ]) && + !attrs[OVPN_A_PEER_REMOTE_PORT ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify remote IP address without port" ); +return -EINVAL ; + } + +if (!attrs[OVPN_A_PEER_REMOTE_IPV4 ] && + attrs[OVPN_A_PEER_LOCAL_IPV4 ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify local IPv4 address without remote" ); +return -EINVAL ; + } + +if (!attrs[OVPN_A_PEER_REMOTE_IPV6 ] && + attrs[OVPN_A_PEER_LOCAL_IPV6 ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify local IPV6 address without remote" ); +return -EINVAL ; + } + +/* check that local and remote address families are the same even +* after parsing v4mapped IPv6 addresses. +* (if addresses are not provided, family will be AF_UNSPEC and +* the check is skipped) +*/ + local_fam =ovpn_nl_family_get (attrs[OVPN_A_PEER_LOCAL_IPV4 ], + attrs[OVPN_A_PEER_LOCAL_IPV6 ]); + remote_fam =ovpn_nl_family_get (attrs[OVPN_A_PEER_REMOTE_IPV4 ], + attrs[OVPN_A_PEER_REMOTE_IPV6 ]); +if (local_fam !=AF_UNSPEC && remote_fam !=AF_UNSPEC && + local_fam != remote_fam) { +NL_SET_ERR_MSG_MOD (info->extack, +"mismatching local and remote address families" ); +return -EINVAL ; + } + +if (remote_fam !=AF_INET6 && attrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID ]) { +NL_SET_ERR_MSG_MOD (info->extack, +"cannot specify scope id without remote IPv6 address" ); +return -EINVAL ; + } + +/* VPN IPs are needed only in MP mode for selecting the right peer */ +if (ovpn->mode ==OVPN_MODE_P2P && (attrs[OVPN_A_PEER_VPN_IPV4 ] || + attrs[OVPN_A_PEER_VPN_IPV6 ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"unexpected VPN IP in P2P mode" ); +return -EINVAL ; + } + +if ((attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + !attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ]) || + (!attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL ] && + attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"keepalive interval and timeout are required together" ); +return -EINVAL ; + } + +return 0 ; +} + +/** +* ovpn_nl_peer_modify - modify the peer attributes according to the incoming msg +* @peer: the peer to modify +* @info: generic netlink info from the user request +* @attrs: the attributes from the user request +* +* Return: a negative error code in case of failure, 0 on success or 1 on +* success and the VPN IPs have been modified (requires rehashing in MP +* mode) +*/ +static int ovpn_nl_peer_modify (struct ovpn_peer *peer,struct genl_info *info, +struct nlattr **attrs) +{ +struct sockaddr_storage ss = {}; +void *local_ip =NULL ; +u32 interv, timeout; +bool rehash = false; +int ret; + +spin_lock_bh (&peer->lock); + +if (ovpn_nl_attr_sockaddr_remote (attrs, &ss)) { +/* we carry the local IP in a generic container. +* ovpn_peer_reset_sockaddr() will properly interpret it +* based on ss.ss_family +*/ + local_ip =ovpn_nl_attr_local_ip (attrs); + +/* set peer sockaddr */ + ret =ovpn_peer_reset_sockaddr (peer, &ss, local_ip); +if (ret <0 ) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot set peer sockaddr: %d" , + ret); + goto err_unlock; + } +dst_cache_reset (&peer->dst_cache); + } + +if (attrs[OVPN_A_PEER_VPN_IPV4 ]) { + rehash = true; + peer->vpn_addrs.ipv4.s_addr = +nla_get_in_addr (attrs[OVPN_A_PEER_VPN_IPV4 ]); + } + +if (attrs[OVPN_A_PEER_VPN_IPV6 ]) { + rehash = true; + 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 (peer->ovpn->dev, +"modify peer id=%u endpoint=%pIScp VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , + peer->id, &ss, + &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6); + +spin_unlock_bh (&peer->lock); + +return rehash ?1 :0 ; +err_unlock: +spin_unlock_bh (&peer->lock); +return ret; +} +int ovpn_nl_peer_new_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_priv *ovpn = info->user_ptr[0 ]; +struct ovpn_socket *ovpn_sock; +struct socket *sock =NULL ; +struct ovpn_peer *peer; +u32 sockfd, 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; + + ret =ovpn_nl_peer_precheck (ovpn, info, attrs); +if (ret <0 ) +return ret; + +if (NL_REQ_ATTR_CHECK (info->extack, info->attrs[OVPN_A_PEER ], attrs, +OVPN_A_PEER_SOCKET )) +return -EINVAL ; + +/* in MP mode VPN IPs are required for selecting the right peer */ +if (ovpn->mode ==OVPN_MODE_MP && !attrs[OVPN_A_PEER_VPN_IPV4 ] && + !attrs[OVPN_A_PEER_VPN_IPV6 ]) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"VPN IP must be provided in MP mode" ); +return -EINVAL ; + } + + peer_id =nla_get_u32 (attrs[OVPN_A_PEER_ID ]); + peer =ovpn_peer_new (ovpn, peer_id); +if (IS_ERR (peer)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot create new peer object for peer %u: %ld" , + peer_id,PTR_ERR (peer)); +return PTR_ERR (peer); + } + +/* 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. +*/ +if (sock->sk->sk_protocol ==IPPROTO_UDP && + !attrs[OVPN_A_PEER_REMOTE_IPV4 ] && + !attrs[OVPN_A_PEER_REMOTE_IPV6 ]) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"missing remote IP address for UDP socket" ); +sockfd_put (sock); + ret = -EINVAL ; + goto peer_release; + } + +/* 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_TCP && + (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || + attrs[OVPN_A_PEER_REMOTE_IPV6 ])) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"unexpected remote IP address with TCP socket" ); +sockfd_put (sock); + ret = -EINVAL ; + goto peer_release; + } + + ovpn_sock =ovpn_socket_new (sock, peer); +/* at this point we unconditionally drop the reference to the socket: +* - in case of error, the socket has to be dropped +* - if case of success, the socket is configured and let +* userspace own the reference, so that the latter can +* trigger the final close() +*/ +sockfd_put (sock); +if (IS_ERR (ovpn_sock)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot encapsulate socket: %ld" , +PTR_ERR (ovpn_sock)); + ret = -ENOTSOCK ; + goto peer_release; + } + +rcu_assign_pointer (peer->sock, ovpn_sock); + + ret =ovpn_nl_peer_modify (peer, info, attrs); +if (ret <0 ) + goto sock_release; + + 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" , + peer->id, ret); + goto sock_release; + } + +return 0 ; + +sock_release: +ovpn_socket_release (peer); +peer_release: +/* release right away because peer was not yet hashed, thus it is not +* used in any context +*/ +ovpn_peer_release (peer); + +return ret; }int ovpn_nl_peer_set_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_priv *ovpn = info->user_ptr[0 ]; +struct ovpn_socket *sock; +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; + + ret =ovpn_nl_peer_precheck (ovpn, info, attrs); +if (ret <0 ) +return ret; + +if (attrs[OVPN_A_PEER_SOCKET ]) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"socket cannot be modified" ); +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 ; + } + +/* when using a TCP socket the remote IP is not expected */ +rcu_read_lock (); + sock =rcu_dereference (peer->sock); +if (sock && sock->sock->sk->sk_protocol ==IPPROTO_TCP && + (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || + attrs[OVPN_A_PEER_REMOTE_IPV6 ])) { +rcu_read_unlock (); +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"unexpected remote IP address with TCP socket" ); +ovpn_peer_put (peer); +return -EINVAL ; + } +rcu_read_unlock (); + +spin_lock_bh (&ovpn->lock); + ret =ovpn_nl_peer_modify (peer, info, attrs); +if (ret <0 ) { +spin_unlock_bh (&ovpn->lock); +ovpn_peer_put (peer); +return ret; + } + +/* ret == 1 means that VPN IPv4/6 has been modified and rehashing +* is required +*/ +if (ret >0 ) +ovpn_peer_hash_vpn_ip (peer); +spin_unlock_bh (&ovpn->lock); +ovpn_peer_put (peer); + +return 0 ; +} + +static int ovpn_nl_send_peer (struct sk_buff *skb,const struct genl_info *info, +const struct ovpn_peer *peer,u32 portid,u32 seq, +int flags) +{ +const struct ovpn_bind *bind; +struct ovpn_socket *sock; +int ret = -EMSGSIZE ; +struct nlattr *attr; +__be16 local_port; +void *hdr; +int id; + + hdr =genlmsg_put (skb, portid, seq, &ovpn_nl_family, flags, +OVPN_CMD_PEER_GET ); +if (!hdr) +return -ENOBUFS ; + + attr =nla_nest_start (skb,OVPN_A_PEER ); +if (!attr) + goto err; + +rcu_read_lock (); + sock =rcu_dereference (peer->sock); +if (!sock) { + ret = -EINVAL ; + goto err_unlock; + } + +if (!net_eq (genl_info_net (info),sock_net (sock->sock->sk))) { + id =peernet2id_alloc (genl_info_net (info), +sock_net (sock->sock->sk), +GFP_ATOMIC ); +if (nla_put_s32 (skb,OVPN_A_PEER_SOCKET_NETNSID , id)) + goto err_unlock; + } + local_port =inet_sk (sock->sock->sk)->inet_sport; +rcu_read_unlock (); + +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_in_addr (skb,OVPN_A_PEER_REMOTE_IPV4 , + bind->remote.in4.sin_addr.s_addr) || +nla_put_net16 (skb,OVPN_A_PEER_REMOTE_PORT , + bind->remote.in4.sin_port) || +nla_put_in_addr (skb,OVPN_A_PEER_LOCAL_IPV4 , + bind->local.ipv4.s_addr)) + goto err_unlock; + }else if (bind->remote.in4.sin_family ==AF_INET6 ) { +if (nla_put_in6_addr (skb,OVPN_A_PEER_REMOTE_IPV6 , + &bind->remote.in6.sin6_addr) || +nla_put_u32 (skb,OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID , + bind->remote.in6.sin6_scope_id) || +nla_put_net16 (skb,OVPN_A_PEER_REMOTE_PORT , + bind->remote.in6.sin6_port) || +nla_put_in6_addr (skb,OVPN_A_PEER_LOCAL_IPV6 , + &bind->local.ipv6)) + goto err_unlock; + } + } +rcu_read_unlock (); + +if (nla_put_net16 (skb,OVPN_A_PEER_LOCAL_PORT , local_port) || +/* 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 ret; }int ovpn_nl_peer_get_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_priv *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_peer_get_dumpit (struct sk_buff *skb,struct netlink_callback *cb) { -return -EOPNOTSUPP ; +const struct genl_info *info =genl_info_dump (cb); +int bkt, last_idx = cb->args[1 ], dumped =0 ; +netdevice_tracker tracker; +struct ovpn_priv *ovpn; +struct ovpn_peer *peer; + + ovpn =ovpn_get_dev_from_attrs (sock_net (cb->skb->sk), info, &tracker); +if (IS_ERR (ovpn)) +return PTR_ERR (ovpn); + +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 (ovpn->dev, &tracker); + +/* sum up peers dumped in this message, so that at the next invocation +* we can continue from where we left +*/ + cb->args[1 ] += dumped; +return skb->len; }int ovpn_nl_peer_del_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct ovpn_priv *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) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"cannot find peer with id %u" , peer_id); +return -ENOENT ; + } + +netdev_dbg (ovpn->dev,"del peer %u\n" , peer->id); + ret =ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_USERSPACE ); +ovpn_peer_put (peer); + +return ret; }int ovpn_nl_key_new_doit (struct sk_buff *skb,struct genl_info *info)
drivers/net/ovpn/peer.c +50 -29
110 lines not on this page · raw message
drivers/net/ovpn/peer.h +5 -0
@@ -125,6 +125,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);/**
@@ -147,6 +148,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn,struct ovpn_peer *ovpn_peer_get_by_id (struct ovpn_priv *ovpn,u32 peer_id);struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_priv *ovpn,struct sk_buff *skb); +void ovpn_peer_hash_vpn_ip (struct ovpn_peer *peer);bool ovpn_peer_check_by_src (struct ovpn_priv *ovpn,struct sk_buff *skb,struct ovpn_peer *peer);
@@ -154,5 +156,8 @@ 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_endpoints_update (struct ovpn_peer *peer,struct sk_buff *skb); +int ovpn_peer_reset_sockaddr (struct ovpn_peer *peer, +const struct sockaddr_storage *ss, +const void *local_ip);#endif /* _NET_OVPN_OVPNPEER_H_ */
drivers/net/ovpn/socket.c +2 -2
@@ -49,8 +49,8 @@ static bool ovpn_socket_put(struct ovpn_peer *peer, struct ovpn_socket *sock)* ovpn_socket_release - release resources owned by socket user * @peer: peer whose socket should be released * -* This function should be invoked when the user is shutting -* down and wants to drop its link to the socket. +* This function should be invoked when the peer is being removed +* and wants to drop its link to the socket. * * In case of UDP, the detach routine will drop a reference to the * ovpn netdev, pointed by the ovpn_socket.
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 19/23 ovpn: implement key add/get/del/swap via netlink
In reply to Antonio QuartulliThis change introduces the netlink commands needed to add, get, delete and swap keys for a specific peer.
Userspace is expected to use these commands to create, inspect (non sensitive data only), destroy and rotate session keys for a specific peer.
--- drivers/net/ovpn/crypto.c | 40 ++++++ drivers/net/ovpn/crypto.h | 4 + drivers/net/ovpn/crypto_aead.c | 17 +++ drivers/net/ovpn/crypto_aead.h | 2 + drivers/net/ovpn/netlink.c | 301 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 360 insertions(+), 4 deletions(-)
drivers/net/ovpn/crypto.c +40 -0
@@ -146,3 +146,43 @@ void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs)spin_unlock_bh (&cs->lock); } + +/** +* ovpn_crypto_config_get - populate keyconf object with non-sensible key data +* @cs: the crypto state to extract the key data from +* @slot: the specific slot to inspect +* @keyconf: the output object to populate +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_crypto_config_get (struct ovpn_crypto_state *cs, +enum ovpn_key_slot slot, +struct ovpn_key_config *keyconf) +{ +struct ovpn_crypto_key_slot *ks; +int idx; + +switch (slot) { +case OVPN_KEY_SLOT_PRIMARY : + idx = cs->primary_idx; +break ; +case OVPN_KEY_SLOT_SECONDARY : + idx = !cs->primary_idx; +break ; +default : +return -EINVAL ; + } + +rcu_read_lock (); + ks =rcu_dereference (cs->slots[idx]); +if (!ks) { +rcu_read_unlock (); +return -ENOENT ; + } + + keyconf->cipher_alg =ovpn_aead_crypto_alg (ks); + keyconf->key_id = ks->key_id; +rcu_read_unlock (); + +return 0 ; +}
drivers/net/ovpn/crypto.h +4 -0
@@ -136,4 +136,8 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs);void ovpn_crypto_key_slots_swap (struct ovpn_crypto_state *cs); +int ovpn_crypto_config_get (struct ovpn_crypto_state *cs, +enum ovpn_key_slot slot, +struct ovpn_key_config *keyconf); +#endif /* _NET_OVPN_OVPNCRYPTO_H_ */
drivers/net/ovpn/crypto_aead.c +17 -0
@@ -364,3 +364,20 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc)ovpn_aead_crypto_key_slot_destroy (ks);return ERR_PTR (ret); } + +enum ovpn_cipher_alg ovpn_aead_crypto_alg (struct ovpn_crypto_key_slot *ks) +{ +const char *alg_name; + +if (!ks->encrypt) +return OVPN_CIPHER_ALG_NONE ; + + alg_name =crypto_tfm_alg_name (crypto_aead_tfm (ks->encrypt)); + +if (!strcmp (alg_name,ALG_NAME_AES )) +return OVPN_CIPHER_ALG_AES_GCM ; +else if (!strcmp (alg_name,ALG_NAME_CHACHAPOLY )) +return OVPN_CIPHER_ALG_CHACHA20_POLY1305 ; +else +return OVPN_CIPHER_ALG_NONE ; +}
drivers/net/ovpn/crypto_aead.h +2 -0
@@ -24,4 +24,6 @@ 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); +enum ovpn_cipher_alg ovpn_aead_crypto_alg (struct ovpn_crypto_key_slot *ks); +#endif /* _NET_OVPN_OVPNAEAD_H_ */
drivers/net/ovpn/netlink.c +297 -4
327 lines not on this page · raw message
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 20/23 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 | 22 ++++++++++++++++ drivers/net/ovpn/crypto.h | 2 ++ drivers/net/ovpn/io.c | 14 ++++++++++ drivers/net/ovpn/netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 2 ++ 5 files changed, 104 insertions(+)
drivers/net/ovpn/crypto.c +22 -0
@@ -54,6 +54,28 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) } } +/* removes the key matching the specified id from the crypto context */ +bool ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id) +{ +struct ovpn_crypto_key_slot *ks =NULL ; + +spin_lock_bh (&cs->lock); +if (rcu_access_pointer (cs->slots[0 ])->key_id == key_id) { + ks =rcu_replace_pointer (cs->slots[0 ],NULL , +lockdep_is_held (&cs->lock)); + }else if (rcu_access_pointer (cs->slots[1 ])->key_id == key_id) { + ks =rcu_replace_pointer (cs->slots[1 ],NULL , +lockdep_is_held (&cs->lock)); + } +spin_unlock_bh (&cs->lock); + +if (ks) +ovpn_crypto_key_slot_put (ks); + +/* let the caller know if a key was actually killed */ +return ks; +} +/* Reset the ovpn_crypto_state object in a way that is atomic * to RCU readers. */
drivers/net/ovpn/crypto.h +2 -0
@@ -140,4 +140,6 @@ int ovpn_crypto_config_get(struct ovpn_crypto_state *cs,enum ovpn_key_slot slot, struct ovpn_key_config *keyconf); +bool ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id); +#endif /* _NET_OVPN_OVPNCRYPTO_H_ */
drivers/net/ovpn/io.c +14 -0
@@ -245,6 +245,20 @@ void ovpn_encrypt_post(void *data, int ret)kfree (ovpn_skb_cb (skb)->sg);aead_request_free (ovpn_skb_cb (skb)->req); +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); +if (ovpn_crypto_kill_key (&peer->crypto, ks->key_id)) +/* let userspace know so that a new key must be negotiated */ +ovpn_nl_key_swap_notify (peer, ks->key_id); + + goto err; + } +if (unlikely (ret <0 )) goto err;
drivers/net/ovpn/netlink.c +64 -0
70 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_key_swap_notify (struct ovpn_peer *peer,u8 key_id); +#endif /* _NET_OVPN_NETLINK_H_ */
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 21/23 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 | 65 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/net/ovpn/netlink.h | 1 + drivers/net/ovpn/peer.c | 1 + 3 files changed, 67 insertions(+)
drivers/net/ovpn/netlink.c +65 -0
@@ -1103,6 +1103,71 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)return 0 ; } +/** +* ovpn_nl_peer_del_notify - notify userspace about peer being deleted +* @peer: the peer being deleted +* +* Return: 0 on success or a negative error code otherwise +*/ +int ovpn_nl_peer_del_notify (struct ovpn_peer *peer) +{ +struct ovpn_socket *sock; +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 (NLMSG_DEFAULT_SIZE ,GFP_ATOMIC ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put (msg,0 ,0 , &ovpn_nl_family,0 ,OVPN_CMD_PEER_DEL_NTF ); +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_u32 (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); + +rcu_read_lock (); + sock =rcu_dereference (peer->sock); +if (!sock) { + ret = -EINVAL ; + goto err_unlock; + } +genlmsg_multicast_netns (&ovpn_nl_family,sock_net (sock->sock->sk), + msg,0 ,OVPN_NLGRP_PEERS ,GFP_ATOMIC ); +rcu_read_unlock (); + +return 0 ; + +err_unlock: +rcu_read_unlock (); +err_cancel_msg: +genlmsg_cancel (msg, hdr); +err_free_msg: +nlmsg_free (msg); +return ret; +} +/** * ovpn_nl_key_swap_notify - notify userspace peer's key must be renewed * @peer: the peer whose key needs to be renewed
drivers/net/ovpn/netlink.h +1 -0
@@ -12,6 +12,7 @@int ovpn_nl_register (void );void ovpn_nl_unregister (void ); +int ovpn_nl_peer_del_notify (struct ovpn_peer *peer);int ovpn_nl_key_swap_notify (struct ovpn_peer *peer,u8 key_id);#endif /* _NET_OVPN_NETLINK_H_ */
drivers/net/ovpn/peer.c +1 -0
@@ -706,6 +706,7 @@ static void ovpn_peer_remove(struct ovpn_peer *peer, } peer->delete_reason = reason; +ovpn_nl_peer_del_notify (peer);/* append to provided list for later socket release and ref drop */ llist_add (&peer->release_entry, release_list);
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 22/23 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>
@@ -120,6 +121,19 @@ bool ovpn_dev_is_valid(const struct net_device *dev)return dev->netdev_ops == &ovpn_netdev_ops; } +static void ovpn_get_drvinfo (struct net_device *dev, +struct ethtool_drvinfo *info) +{ +strscpy (info->driver,"ovpn" ,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, +}; +static void ovpn_setup (struct net_device *dev) { netdev_features_t feat = NETIF_F_SG | NETIF_F_GSO |
@@ -129,6 +143,7 @@ static void ovpn_setup(struct net_device *dev) dev->pcpu_stat_type =NETDEV_PCPU_STAT_DSTATS ; + dev->ethtool_ops = &ovpn_ethtool_ops; dev->netdev_ops = &ovpn_netdev_ops; dev->priv_destructor = ovpn_priv_free;
Signature · 1 line
2.49.0
Antonio Quartulli
maintainerPATCH v26 23/23 testing/selftests: 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.
[NOTE: it depends on libmedtls for decoding base64-encoded keys]
ovpn-cli implements the netlink and RTNL APIs and can thus be integrated in any script for more automated testing.
Along with the tool, a bunch of scripts are provided that perform basic functionality tests by means of network namespaces.
These scripts take part to the kselftest automation.
The output of the scripts, which will appear in the kselftest reports, is a list of steps performed by the scripts plus some output coming from the execution of ping, iperf and ovpn-cli itself.
In general it is useful only in case of failure, in order to understand which step has failed and why.
Please note: since peer sockets are tied to the userspace process that created them (i.e. exiting the process will result in closing the socket), every run of ovpn-cli that created one will go to background and enter pause(), waiting for the signal which will allow it to terminate.
Termination is accomplished at the end of each script by issuing a killall command.
--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 31 + tools/testing/selftests/net/ovpn/common.sh | 92 + tools/testing/selftests/net/ovpn/config | 10 + tools/testing/selftests/net/ovpn/data64.key | 5 + tools/testing/selftests/net/ovpn/ovpn-cli.c | 2376 ++++++++++++++++++++ tools/testing/selftests/net/ovpn/tcp_peers.txt | 5 + .../testing/selftests/net/ovpn/test-chachapoly.sh | 9 + .../selftests/net/ovpn/test-close-socket-tcp.sh | 9 + .../selftests/net/ovpn/test-close-socket.sh | 45 + tools/testing/selftests/net/ovpn/test-float.sh | 9 + tools/testing/selftests/net/ovpn/test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/test.sh | 113 + tools/testing/selftests/net/ovpn/udp_peers.txt | 5 + 16 files changed, 2722 insertions(+)
MAINTAINERS +1 -0
@@ -18134,6 +18134,7 @@ T: git https://github.com/OpenVPN/linux-kernel-ovpn.git F: Documentation/netlink/specs/ovpn.yaml F: drivers/net/ovpn/ F: include/uapi/linux/ovpn.h +F: tools/testing/selftests/net/ovpn/ OPENVSWITCH M: Aaron Conole <[email protected]>
tools/testing/selftests/Makefile +1 -0
@@ -71,6 +71,7 @@ TARGETS += net/hsr TARGETS += net/mptcp TARGETS += net/netfilter TARGETS += net/openvswitch +TARGETS += net/ovpn TARGETS += net/packetdrill TARGETS += net/rds TARGETS += net/tcp_ao
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 +31 -0
@@ -0,0 +1,31 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-2025 OpenVPN, Inc. +# +CFLAGS = -pedantic -Wextra -Wall -Wl,--no-as-needed -g -O0 -ggdb $(KHDR_INCLUDES) +VAR_CFLAGS = $(shell pkg-config --cflags libnl-3.0 libnl-genl-3.0 2>/dev/null) +ifeq ($(VAR_CFLAGS),) +VAR_CFLAGS = -I/usr/include/libnl3 +endif +CFLAGS += $(VAR_CFLAGS) + + +LDLIBS = -lmbedtls -lmbedcrypto +VAR_LDLIBS = $(shell pkg-config --libs libnl-3.0 libnl-genl-3.0 2>/dev/null) +ifeq ($(VAR_LDLIBS),) +VAR_LDLIBS = -lnl-genl-3 -lnl-3 +endif +LDLIBS += $(VAR_LDLIBS) + + +TEST_FILES = common.sh + +TEST_PROGS = test.sh \ + test-chachapoly.sh \ + test-tcp.sh \ + test-float.sh \ + test-close-socket.sh \ + test-close-socket-tcp.sh + +TEST_GEN_FILES := ovpn-cli + +include ../../lib.mk
tools/testing/selftests/net/ovpn/common.sh new file +92 -0
92 lines not on this page · raw message
tools/testing/selftests/net/ovpn/config new file +10 -0
@@ -0,0 +1,10 @@ +CONFIG_NET=y +CONFIG_INET=y +CONFIG_STREAM_PARSER=y +CONFIG_NET_UDP_TUNNEL=y +CONFIG_DST_CACHE=y +CONFIG_CRYPTO=y +CONFIG_CRYPTO_AES=y +CONFIG_CRYPTO_GCM=y +CONFIG_CRYPTO_CHACHA20POLY1305=y +CONFIG_OVPN=m
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/ovpn-cli.c new file +2376 -0
2,376 lines not on this page · raw message
tools/testing/selftests/net/ovpn/tcp_peers.txt new file +5 -0
@@ -0,0 +1,5 @@ +1 5.5.5.2 +2 5.5.5.3 +3 5.5.5.4 +4 5.5.5.5 +5 5.5.5.6
tools/testing/selftests/net/ovpn/test-chachapoly.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2025 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +ALG="chachapoly" + +source test.sh
tools/testing/selftests/net/ovpn/test-close-socket-tcp.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2025 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +PROTO="TCP" + +source test-close-socket.sh
tools/testing/selftests/net/ovpn/test-close-socket.sh new file +45 -0
45 lines not on this page · raw message
tools/testing/selftests/net/ovpn/test-float.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2025 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +FLOAT="1" + +source test.sh
tools/testing/selftests/net/ovpn/test-tcp.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2025 OpenVPN, Inc. +# +# Author: Antonio Quartulli <[email protected]> + +PROTO="TCP" + +source test.sh
tools/testing/selftests/net/ovpn/test.sh new file +113 -0
113 lines not on this page · raw message
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.49.0
Quoted 38 lines · Antonio Quartulli
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()The latest code can also be found at:
https://github.com/OpenVPN/ovpn-net-next
Thanks a lot!
Best Regards,Antonio Quartulli
OpenVPN Inc.---
Antonio Quartulli (23):
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: keep carrier always on for MP interfaces
ovpn: introduce theovpn_peerobject
ovpn: introduce theovpn_socketobject
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
skb: implementskb_send_sock_locked_with_flags()
ovpn: add support forMSG_NOSIGNALintcp_sendmsg
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
Quoted 8 lines
ovpn: implement keepalive mechanism
ovpn: add support for updating local or remote UDP endpoint
ovpn: implement peer add/get/dump/delete via netlink
ovpn: implement key add/get/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/selftests: add test tool and scripts for ovpn module
The changes since v25 lgtm:
Signature · 1 line
Sabrina
Hello.
Quoted 11 lines · středoevropský letní čas Antonio Quartulli
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()
Quoted 3 lines
The latest code can also be found at:
Thank you for this. I've backported the submission for my local v6.14-based build (had to adjust for 69c7be1b903fca) and I'm using it now with [1] as you've suggested previously. So far so good. Feel free to add my:
A couple of notes if I may:
1. is it expected for then tun iface to stay after the connection is brought down? If that matters, I'm using NetworkManager for managing my OpenVPN connections
2. a userspace nit probably not relevant to this submission: the daemon still reports "DCO version:" but with "N/A" value because that version file under /sys is not presented any more like it was with an out-of-tree v2 implementation
[1]: https://github.com/mandelbitdev/openvpn/tree/gianmarco/179-ovpn-support
Quoted 96 lines
Thanks a lot!
Best Regards,Antonio Quartulli
OpenVPN Inc.---
Antonio Quartulli (23):
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: keep carrier always on for MP interfaces
ovpn: introduce theovpn_peerobject
ovpn: introduce theovpn_socketobject
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
skb: implementskb_send_sock_locked_with_flags()
ovpn: add support forMSG_NOSIGNALintcp_sendmsg
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
ovpn: implement keepalive mechanism
ovpn: add support for updating local or remote UDP endpoint
ovpn: implement peer add/get/dump/delete via netlink
ovpn: implement key add/get/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/selftests: add test tool and scripts for ovpn moduleDocumentation/netlink/specs/ovpn.yaml | 367 +++ Documentation/netlink/specs/rt-link.yaml | 16 + MAINTAINERS | 11 + drivers/net/Kconfig | 15 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 55 + drivers/net/ovpn/bind.h | 101 + drivers/net/ovpn/crypto.c | 210 ++ drivers/net/ovpn/crypto.h | 145 ++ drivers/net/ovpn/crypto_aead.c | 383 ++++ drivers/net/ovpn/crypto_aead.h | 29 + drivers/net/ovpn/io.c | 446 ++++ drivers/net/ovpn/io.h | 34 + drivers/net/ovpn/main.c | 274 +++ drivers/net/ovpn/main.h | 14 + drivers/net/ovpn/netlink-gen.c | 213 ++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 1258 +++++++++++ drivers/net/ovpn/netlink.h | 18 + drivers/net/ovpn/ovpnpriv.h | 55 + drivers/net/ovpn/peer.c | 1365 +++++++++++ drivers/net/ovpn/peer.h | 163 ++ drivers/net/ovpn/pktid.c | 129 ++ drivers/net/ovpn/pktid.h | 86 + drivers/net/ovpn/proto.h | 118 + drivers/net/ovpn/skb.h | 61 + drivers/net/ovpn/socket.c | 233 ++ drivers/net/ovpn/socket.h | 49 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 47 + drivers/net/ovpn/tcp.c | 598 +++++ drivers/net/ovpn/tcp.h | 36 + drivers/net/ovpn/udp.c | 439 ++++ drivers/net/ovpn/udp.h | 25 + include/linux/skbuff.h | 2 + include/uapi/linux/if_link.h | 15 + include/uapi/linux/ovpn.h | 109 + include/uapi/linux/udp.h | 1 + net/core/skbuff.c | 18 +- net/ipv6/af_inet6.c | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 31 + tools/testing/selftests/net/ovpn/common.sh | 92 + tools/testing/selftests/net/ovpn/config | 10 + tools/testing/selftests/net/ovpn/data64.key | 5 + tools/testing/selftests/net/ovpn/ovpn-cli.c | 2376 ++++++++++++++++++++ tools/testing/selftests/net/ovpn/tcp_peers.txt | 5 + .../testing/selftests/net/ovpn/test-chachapoly.sh | 9 + .../selftests/net/ovpn/test-close-socket-tcp.sh | 9 + .../selftests/net/ovpn/test-close-socket.sh | 45 + tools/testing/selftests/net/ovpn/test-float.sh | 9 + tools/testing/selftests/net/ovpn/test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/test.sh | 113 + tools/testing/selftests/net/ovpn/udp_peers.txt | 5 + 56 files changed, 9940 insertions(+), 5 deletions(-)---
base-commit:23f09f01b495cc510a19b30b6093fb4cb0284aaf
change-id: 20241002-b4-ovpn-eeee35c694a2Best regards,
Signature · 1 line
Oleksandr Natalenko, MSE
Antonio Quartulli
maintainerHi Oleksandr,
Thanks a lot for testing!
Quoted 18 lines · Oleksandr Natalenko
Hello.
Quoted 11 lines · středoevropský letní čas Antonio Quartulli
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()Quoted 3 lines
The latest code can also be found at:
Quoted 7 lines
Thank you for this. I've backported the submission for my local v6.14-based build (had to adjust for
69c7be1b903fca) and I'm using it now with [1] as you've suggested previously. So far so good. Feel free to add my:Tested-by Oleksandr Natalenko <[email protected]>A couple of notes if I may:
1. is it expected for then tun iface to stay after the connection is brought down? If that matters, I'm using NetworkManager for managing my OpenVPN connections
If the interface is created by openvpn in userspace, the latter should also destroy it during cleanup.
Quoted 1 line
2. a userspace nit probably not relevant to this submission: the daemon still reports "DCO version:" but with "N/A" value because that version file under /sys is not presented any more like it was with an out-of-tree v2 implementation
Thanks a lot, I have reported this point to gerrit (where the patch is being reviewed): https://gerrit.openvpn.net/c/openvpn/+/941
Regards,
Quoted 101 lines
[1]: https://github.com/mandelbitdev/openvpn/tree/gianmarco/179-ovpn-support
Quoted 96 lines
Thanks a lot!
Best Regards,Antonio Quartulli
OpenVPN Inc.---
Antonio Quartulli (23):
net: introduce OpenVPN Data Channel Offload (ovpn)
ovpn: add basic netlink support
ovpn: add basic interface creation/destruction/management routines
ovpn: keep carrier always on for MP interfaces
ovpn: introduce theovpn_peerobject
ovpn: introduce theovpn_socketobject
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
skb: implementskb_send_sock_locked_with_flags()
ovpn: add support forMSG_NOSIGNALintcp_sendmsg
ovpn: implement multi-peer support
ovpn: implement peer lookup logic
ovpn: implement keepalive mechanism
ovpn: add support for updating local or remote UDP endpoint
ovpn: implement peer add/get/dump/delete via netlink
ovpn: implement key add/get/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/selftests: add test tool and scripts for ovpn moduleDocumentation/netlink/specs/ovpn.yaml | 367 +++ Documentation/netlink/specs/rt-link.yaml | 16 + MAINTAINERS | 11 + drivers/net/Kconfig | 15 + drivers/net/Makefile | 1 + drivers/net/ovpn/Makefile | 22 + drivers/net/ovpn/bind.c | 55 + drivers/net/ovpn/bind.h | 101 + drivers/net/ovpn/crypto.c | 210 ++ drivers/net/ovpn/crypto.h | 145 ++ drivers/net/ovpn/crypto_aead.c | 383 ++++ drivers/net/ovpn/crypto_aead.h | 29 + drivers/net/ovpn/io.c | 446 ++++ drivers/net/ovpn/io.h | 34 + drivers/net/ovpn/main.c | 274 +++ drivers/net/ovpn/main.h | 14 + drivers/net/ovpn/netlink-gen.c | 213 ++ drivers/net/ovpn/netlink-gen.h | 41 + drivers/net/ovpn/netlink.c | 1258 +++++++++++ drivers/net/ovpn/netlink.h | 18 + drivers/net/ovpn/ovpnpriv.h | 55 + drivers/net/ovpn/peer.c | 1365 +++++++++++ drivers/net/ovpn/peer.h | 163 ++ drivers/net/ovpn/pktid.c | 129 ++ drivers/net/ovpn/pktid.h | 86 + drivers/net/ovpn/proto.h | 118 + drivers/net/ovpn/skb.h | 61 + drivers/net/ovpn/socket.c | 233 ++ drivers/net/ovpn/socket.h | 49 + drivers/net/ovpn/stats.c | 21 + drivers/net/ovpn/stats.h | 47 + drivers/net/ovpn/tcp.c | 598 +++++ drivers/net/ovpn/tcp.h | 36 + drivers/net/ovpn/udp.c | 439 ++++ drivers/net/ovpn/udp.h | 25 + include/linux/skbuff.h | 2 + include/uapi/linux/if_link.h | 15 + include/uapi/linux/ovpn.h | 109 + include/uapi/linux/udp.h | 1 + net/core/skbuff.c | 18 +- net/ipv6/af_inet6.c | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/net/ovpn/.gitignore | 2 + tools/testing/selftests/net/ovpn/Makefile | 31 + tools/testing/selftests/net/ovpn/common.sh | 92 + tools/testing/selftests/net/ovpn/config | 10 + tools/testing/selftests/net/ovpn/data64.key | 5 + tools/testing/selftests/net/ovpn/ovpn-cli.c | 2376 ++++++++++++++++++++ tools/testing/selftests/net/ovpn/tcp_peers.txt | 5 + .../testing/selftests/net/ovpn/test-chachapoly.sh | 9 + .../selftests/net/ovpn/test-close-socket-tcp.sh | 9 + .../selftests/net/ovpn/test-close-socket.sh | 45 + tools/testing/selftests/net/ovpn/test-float.sh | 9 + tools/testing/selftests/net/ovpn/test-tcp.sh | 9 + tools/testing/selftests/net/ovpn/test.sh | 113 + tools/testing/selftests/net/ovpn/udp_peers.txt | 5 + 56 files changed, 9940 insertions(+), 5 deletions(-)---
base-commit:23f09f01b495cc510a19b30b6093fb4cb0284aaf
change-id: 20241002-b4-ovpn-eeee35c694a2Best regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
Paolo Abeni
maintainerQuoted 11 lines · Antonio Quartulli
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()
Quoted 3 lines
The latest code can also be found at:
I think it's finally time to merge this. Thanks Anotonio for your patience and persistence and thank you Sabrina for the huge review effort.
/P
Antonio Quartulli
maintainerQuoted 12 lines · Paolo Abeni
Quoted 11 lines · Antonio Quartulli
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()
Quoted 6 lines
Quoted 3 lines
The latest code can also be found at:
I think it's finally time to merge this. Thanks Anotonio for your patience and persistence and thank you Sabrina for the huge review effort.
Thanks Paolo and thank you all for the reviews (especially Sabrina!), suggestions and hints!
It's been a bumpy ride, but we crossed the finish line! :-)
Cheers,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Tue, 15 Apr 2025 13:17:17 +0200 you wrote:
Notable changes since v25:
* removed netdev notifier (was only used for our own devices)
* added .dellink implementation to address what was previously done in notifier
* removed .ndo_open and movednetif_carrier_off()call to .ndo_init
* fixed author inMODULE_AUTHOR()
* properly indented checks inovpn.yaml
* switched from TSTATS to DSTATS
* removed obsolete comment inovpn_socket_new()
* removed unrelated hunk inovpn_socket_new()
Quoted 1 line
[...]
Here is the summary with links:
- [net-next,v26,01/23] net: introduce OpenVPN Data Channel Offload (ovpn)
https://git.kernel.org/netdev/net-next/c/9f23d943eb6b - [net-next,v26,02/23] ovpn: add basic netlink support https://git.kernel.org/netdev/net-next/c/b7a63391aa98 - [net-next,v26,03/23] ovpn: add basic interface creation/destruction/management routines https://git.kernel.org/netdev/net-next/c/c2d950c4672a - [net-next,v26,04/23] ovpn: keep carrier always on for MP interfaces https://git.kernel.org/netdev/net-next/c/8327a3baa9b0 - [net-next,v26,05/23] ovpn: introduce the ovpn_peer object https://git.kernel.org/netdev/net-next/c/80747caef33d - [net-next,v26,06/23] ovpn: introduce the ovpn_socket object https://git.kernel.org/netdev/net-next/c/f6226ae7a0cd - [net-next,v26,07/23] ovpn: implement basic TX path (UDP) https://git.kernel.org/netdev/net-next/c/08857b5ec5d9 - [net-next,v26,08/23] ovpn: implement basic RX path (UDP) https://git.kernel.org/netdev/net-next/c/ab66abbc769b - [net-next,v26,09/23] ovpn: implement packet processing https://git.kernel.org/netdev/net-next/c/8534731dbf2d - [net-next,v26,10/23] ovpn: store tunnel and transport statistics https://git.kernel.org/netdev/net-next/c/04ca14955f9a - [net-next,v26,11/23] ovpn: implement TCP transport https://git.kernel.org/netdev/net-next/c/11851cbd60ea - [net-next,v26,12/23] skb: implement skb_send_sock_locked_with_flags() https://git.kernel.org/netdev/net-next/c/17240749f26e - [net-next,v26,13/23] ovpn: add support for MSG_NOSIGNAL in tcp_sendmsg https://git.kernel.org/netdev/net-next/c/36bb1d713a15 - [net-next,v26,14/23] ovpn: implement multi-peer support https://git.kernel.org/netdev/net-next/c/05003b408c20 - [net-next,v26,15/23] ovpn: implement peer lookup logic https://git.kernel.org/netdev/net-next/c/a3aaef8cd173 - [net-next,v26,16/23] ovpn: implement keepalive mechanism https://git.kernel.org/netdev/net-next/c/3ecfd9349f40 - [net-next,v26,17/23] ovpn: add support for updating local or remote UDP endpoint https://git.kernel.org/netdev/net-next/c/f0281c1d3732 - [net-next,v26,18/23] ovpn: implement peer add/get/dump/delete via netlink https://git.kernel.org/netdev/net-next/c/1d36a36f6d53 - [net-next,v26,19/23] ovpn: implement key add/get/del/swap via netlink https://git.kernel.org/netdev/net-next/c/203e2bf55990 - [net-next,v26,20/23] ovpn: kill key and notify userspace in case of IV exhaustion https://git.kernel.org/netdev/net-next/c/89d3c0e4612a - [net-next,v26,21/23] ovpn: notify userspace when a peer is deleted https://git.kernel.org/netdev/net-next/c/a215d253c17a - [net-next,v26,22/23] ovpn: add basic ethtool support https://git.kernel.org/netdev/net-next/c/b756861e6e63 - [net-next,v26,23/23] testing/selftests: add test tool and scripts for ovpn module https://git.kernel.org/netdev/net-next/c/959bc330a439
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html