|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* OpenVPN data channel offload |
| 3 | + * |
| 4 | + * Copyright (C) 2020-2025 OpenVPN, Inc. |
| 5 | + * |
| 6 | + * Author: James Yonan <[email protected]> |
| 7 | + * Antonio Quartulli <[email protected]> |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/net.h> |
| 11 | +#include <linux/netdevice.h> |
| 12 | +#include <linux/udp.h> |
| 13 | + |
| 14 | +#include "ovpnpriv.h" |
| 15 | +#include "main.h" |
| 16 | +#include "io.h" |
| 17 | +#include "peer.h" |
| 18 | +#include "socket.h" |
| 19 | +#include "udp.h" |
| 20 | + |
| 21 | +static void ovpn_socket_release_kref(struct kref *kref) |
| 22 | +{ |
| 23 | + struct ovpn_socket *sock = container_of(kref, struct ovpn_socket, |
| 24 | + refcount); |
| 25 | + |
| 26 | + if (sock->sock->sk->sk_protocol == IPPROTO_UDP) |
| 27 | + ovpn_udp_socket_detach(sock); |
| 28 | + |
| 29 | + kfree_rcu(sock, rcu); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * ovpn_socket_put - decrease reference counter |
| 34 | + * @peer: peer whose socket reference counter should be decreased |
| 35 | + * @sock: the RCU protected peer socket |
| 36 | + * |
| 37 | + * This function is only used internally. Users willing to release |
| 38 | + * references to the ovpn_socket should use ovpn_socket_release() |
| 39 | + */ |
| 40 | +static void ovpn_socket_put(struct ovpn_peer *peer, struct ovpn_socket *sock) |
| 41 | +{ |
| 42 | + kref_put(&sock->refcount, ovpn_socket_release_kref); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * ovpn_socket_release - release resources owned by socket user |
| 47 | + * @peer: peer whose socket should be released |
| 48 | + * |
| 49 | + * This function should be invoked when the user is shutting |
| 50 | + * down and wants to drop its link to the socket. |
| 51 | + * |
| 52 | + * In case of UDP, the detach routine will drop a reference to the |
| 53 | + * ovpn netdev, pointed by the ovpn_socket. |
| 54 | + * |
| 55 | + * In case of TCP, releasing the socket will cause dropping |
| 56 | + * the refcounter for the peer it is linked to, thus allowing the peer |
| 57 | + * disappear as well. |
| 58 | + * |
| 59 | + * This function is expected to be invoked exactly once per peer |
| 60 | + * |
| 61 | + * NOTE: this function may sleep |
| 62 | + */ |
| 63 | +void ovpn_socket_release(struct ovpn_peer *peer) |
| 64 | +{ |
| 65 | + struct ovpn_socket *sock; |
| 66 | + |
| 67 | + might_sleep(); |
| 68 | + |
| 69 | + sock = rcu_replace_pointer(peer->sock, NULL, true); |
| 70 | + /* release may be invoked after socket was detached */ |
| 71 | + if (!sock) |
| 72 | + return; |
| 73 | + |
| 74 | + /* sanity check: we should not end up here if the socket |
| 75 | + * was already closed |
| 76 | + */ |
| 77 | + if (!sock->sock->sk) { |
| 78 | + DEBUG_NET_WARN_ON_ONCE(1); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + /* Drop the reference while holding the sock lock to avoid |
| 83 | + * concurrent ovpn_socket_new call to mess up with a partially |
| 84 | + * detached socket. |
| 85 | + * |
| 86 | + * Holding the lock ensures that a socket with refcnt 0 is fully |
| 87 | + * detached before it can be picked by a concurrent reader. |
| 88 | + */ |
| 89 | + lock_sock(sock->sock->sk); |
| 90 | + ovpn_socket_put(peer, sock); |
| 91 | + release_sock(sock->sock->sk); |
| 92 | + |
| 93 | + /* align all readers with sk_user_data being NULL */ |
| 94 | + synchronize_rcu(); |
| 95 | +} |
| 96 | + |
| 97 | +static bool ovpn_socket_hold(struct ovpn_socket *sock) |
| 98 | +{ |
| 99 | + return kref_get_unless_zero(&sock->refcount); |
| 100 | +} |
| 101 | + |
| 102 | +static int ovpn_socket_attach(struct ovpn_socket *sock, struct ovpn_peer *peer) |
| 103 | +{ |
| 104 | + if (sock->sock->sk->sk_protocol == IPPROTO_UDP) |
| 105 | + return ovpn_udp_socket_attach(sock, peer->ovpn); |
| 106 | + |
| 107 | + return -EOPNOTSUPP; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * ovpn_socket_new - create a new socket and initialize it |
| 112 | + * @sock: the kernel socket to embed |
| 113 | + * @peer: the peer reachable via this socket |
| 114 | + * |
| 115 | + * Return: an openvpn socket on success or a negative error code otherwise |
| 116 | + */ |
| 117 | +struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) |
| 118 | +{ |
| 119 | + struct ovpn_socket *ovpn_sock; |
| 120 | + int ret; |
| 121 | + |
| 122 | + lock_sock(sock->sk); |
| 123 | + |
| 124 | + /* a TCP socket can only be owned by a single peer, therefore there |
| 125 | + * can't be any other user |
| 126 | + */ |
| 127 | + if (sock->sk->sk_protocol == IPPROTO_TCP && sock->sk->sk_user_data) { |
| 128 | + ovpn_sock = ERR_PTR(-EBUSY); |
| 129 | + goto sock_release; |
| 130 | + } |
| 131 | + |
| 132 | + /* a UDP socket can be shared across multiple peers, but we must make |
| 133 | + * sure it is not owned by something else |
| 134 | + */ |
| 135 | + if (sock->sk->sk_protocol == IPPROTO_UDP) { |
| 136 | + u8 type = READ_ONCE(udp_sk(sock->sk)->encap_type); |
| 137 | + |
| 138 | + /* socket owned by other encapsulation module */ |
| 139 | + if (type && type != UDP_ENCAP_OVPNINUDP) { |
| 140 | + ovpn_sock = ERR_PTR(-EBUSY); |
| 141 | + goto sock_release; |
| 142 | + } |
| 143 | + |
| 144 | + rcu_read_lock(); |
| 145 | + ovpn_sock = rcu_dereference_sk_user_data(sock->sk); |
| 146 | + if (ovpn_sock) { |
| 147 | + /* socket owned by another ovpn instance, we can't use it */ |
| 148 | + if (ovpn_sock->ovpn != peer->ovpn) { |
| 149 | + ovpn_sock = ERR_PTR(-EBUSY); |
| 150 | + rcu_read_unlock(); |
| 151 | + goto sock_release; |
| 152 | + } |
| 153 | + |
| 154 | + /* this socket is already owned by this instance, |
| 155 | + * therefore we can increase the refcounter and |
| 156 | + * use it as expected |
| 157 | + */ |
| 158 | + if (WARN_ON(!ovpn_socket_hold(ovpn_sock))) { |
| 159 | + /* this should never happen because setting |
| 160 | + * the refcnt to 0 and detaching the socket |
| 161 | + * is expected to be atomic |
| 162 | + */ |
| 163 | + ovpn_sock = ERR_PTR(-EAGAIN); |
| 164 | + rcu_read_unlock(); |
| 165 | + goto sock_release; |
| 166 | + } |
| 167 | + |
| 168 | + rcu_read_unlock(); |
| 169 | + goto sock_release; |
| 170 | + } |
| 171 | + rcu_read_unlock(); |
| 172 | + } |
| 173 | + |
| 174 | + /* socket is not owned: attach to this ovpn instance */ |
| 175 | + |
| 176 | + ovpn_sock = kzalloc(sizeof(*ovpn_sock), GFP_KERNEL); |
| 177 | + if (!ovpn_sock) { |
| 178 | + ovpn_sock = ERR_PTR(-ENOMEM); |
| 179 | + goto sock_release; |
| 180 | + } |
| 181 | + |
| 182 | + ovpn_sock->ovpn = peer->ovpn; |
| 183 | + ovpn_sock->sock = sock; |
| 184 | + kref_init(&ovpn_sock->refcount); |
| 185 | + |
| 186 | + ret = ovpn_socket_attach(ovpn_sock, peer); |
| 187 | + if (ret < 0) { |
| 188 | + kfree(ovpn_sock); |
| 189 | + ovpn_sock = ERR_PTR(ret); |
| 190 | + goto sock_release; |
| 191 | + } |
| 192 | + |
| 193 | + rcu_assign_sk_user_data(sock->sk, ovpn_sock); |
| 194 | +sock_release: |
| 195 | + release_sock(sock->sk); |
| 196 | + return ovpn_sock; |
| 197 | +} |
0 commit comments