Debian Codegraph @

drivers/net/ovpn-dco/tcp.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
2 /* OpenVPN data channel accelerator
3 *
4 * Copyright (C) 2019-2023 OpenVPN, Inc.
5 *
6 * Author: Antonio Quartulli <[email protected]>
7 */
8
9 #include "main.h"
10 #include "ovpnstruct.h"
11 #include "ovpn.h"
12 #include "peer.h"
13 #include "proto.h"
14 #include "skb.h"
15 #include "tcp.h"
16
17 #include <linux/ptr_ring.h>
18 #include <linux/skbuff.h>
19 #include <net/tcp.h>
20 #include <net/route.h>
21
22 static struct proto ovpn_tcp_prot;
23
24 static int ovpn_tcp_read_sock(read_descriptor_t *desc, struct sk_buff *in_skb,
25 unsigned int in_offset, size_t in_len)
26 {
27 struct sock *sk = desc->arg.data;
28 struct ovpn_socket *sock;
29 struct ovpn_skb_cb *cb;
30 struct ovpn_peer *peer;
31 size_t chunk, copied = 0;
32 int status;
33 void *data;
34 u16 len;
35
36 rcu_read_lock();
37 sock = rcu_dereference_sk_user_data(sk);
38 rcu_read_unlock();
39
40 if (unlikely(!sock || !sock->peer)) {
41 pr_err("ovpn: read_sock triggered for socket with no metadata\n");
42 desc->error = -EINVAL;
43 return 0;
44 }
45
46 peer = sock->peer;
47
48 while (in_len > 0) {
49 /* no skb allocated means that we have to read (or finish reading) the 2 bytes
50 * prefix containing the actual packet size.
51 */
52 if (!peer->tcp.skb) {
53 chunk = min_t(size_t, in_len, sizeof(u16) - peer->tcp.offset);
54 WARN_ON(skb_copy_bits(in_skb, in_offset,
55 peer->tcp.raw_len + peer->tcp.offset, chunk) < 0);
56 peer->tcp.offset += chunk;
57
58 /* keep on reading until we got the whole packet size */
59 if (peer->tcp.offset != sizeof(u16))
60 goto next_read;
61
62 len = ntohs(*(__be16 *)peer->tcp.raw_len);
63 /* invalid packet length: this is a fatal TCP error */
64 if (!len) {
65 netdev_err(peer->ovpn->dev, "%s: received invalid packet length: %d\n",
66 __func__, len);
67 desc->error = -EINVAL;
68 goto err;
69 }
70
71 /* add 2 bytes to allocated space (and immediately reserve them) for packet
72 * length prepending, in case the skb has to be forwarded to userspace
73 */
74 peer->tcp.skb = netdev_alloc_skb_ip_align(peer->ovpn->dev,
75 len + sizeof(u16));
76 if (!peer->tcp.skb) {
77 desc->error = -ENOMEM;
78 goto err;
79 }
80 skb_reserve(peer->tcp.skb, sizeof(u16));
81
82 peer->tcp.offset = 0;
83 peer->tcp.data_len = len;
84 } else {
85 chunk = min_t(size_t, in_len, peer->tcp.data_len - peer->tcp.offset);
86
87 /* extend skb to accommodate the new chunk and copy it from the input skb */
88 data = skb_put(peer->tcp.skb, chunk);
89 WARN_ON(skb_copy_bits(in_skb, in_offset, data, chunk) < 0);
90 peer->tcp.offset += chunk;
91
92 /* keep on reading until we get the full packet */
93 if (peer->tcp.offset != peer->tcp.data_len)
94 goto next_read;
95
96 /* do not perform IP caching for TCP connections */
97 cb = OVPN_SKB_CB(peer->tcp.skb);
98 cb->sa_fam = AF_UNSPEC;
99
100 /* At this point we know the packet is from a configured peer.
101 * DATA_V2 packets are handled in kernel space, the rest goes to user space.
102 *
103 * Queue skb for sending to userspace via recvmsg on the socket
104 */
105 if (likely(ovpn_opcode_from_skb(peer->tcp.skb, 0) == OVPN_DATA_V2)) {
106 /* hold reference to peer as required by ovpn_recv().
107 *
108 * NOTE: in this context we should already be holding a
109 * reference to this peer, therefore ovpn_peer_hold() is
110 * not expected to fail
111 */
112 WARN_ON(!ovpn_peer_hold(peer));
113 status = ovpn_recv(peer->ovpn, peer, peer->tcp.skb);
114 if (unlikely(status < 0))
115 ovpn_peer_put(peer);
116
117 } else {
118 /* prepend skb with packet len. this way userspace can parse
119 * the packet as if it just arrived from the remote endpoint
120 */
121 void *raw_len = __skb_push(peer->tcp.skb, sizeof(u16));
122 memcpy(raw_len, peer->tcp.raw_len, sizeof(u16));
123
124 status = ptr_ring_produce_bh(&peer->sock->recv_ring, peer->tcp.skb);
125 if (likely(!status))
126 peer->tcp.sk_cb.sk_data_ready(sk);
127 }
128
129 /* skb not consumed - free it now */
130 if (unlikely(status < 0))
131 kfree_skb(peer->tcp.skb);
132
133 peer->tcp.skb = NULL;
134 peer->tcp.offset = 0;
135 peer->tcp.data_len = 0;
136 }
137 next_read:
138 in_len -= chunk;
139 in_offset += chunk;
140 copied += chunk;
141 }
142
143 return copied;
144 err:
145 netdev_err(peer->ovpn->dev, "cannot process incoming TCP data: %d\n", desc->error);
146 ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_TRANSPORT_ERROR);
147 return 0;
148 }
149
150 static void ovpn_tcp_data_ready(struct sock *sk)
151 {
152 struct socket *sock = sk->sk_socket;
153 read_descriptor_t desc;
154
155 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
156 return;
157
158 desc.arg.data = sk;
159 desc.error = 0;
160 desc.count = 1;
161
162 sock->ops->read_sock(sk, &desc, ovpn_tcp_read_sock);
163 }
164
165 static void ovpn_tcp_write_space(struct sock *sk)
166 {
167 struct ovpn_socket *sock;
168
169 rcu_read_lock();
170 sock = rcu_dereference_sk_user_data(sk);
171 rcu_read_unlock();
172
173 if (!sock || !sock->peer)
174 return;
175
176 queue_work(sock->peer->ovpn->events_wq, &sock->peer->tcp.tx_work);
177 }
178
179 static bool ovpn_tcp_sock_is_readable(
180 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0) && RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(9, 0) && SUSE_PRODUCT_CODE < SUSE_PRODUCT(1, 15, 5, 0)
181 const struct sock *sk
182 #else
183 struct sock *sk
184 #endif
185 )
186
187 {
188 struct ovpn_socket *sock;
189
190 rcu_read_lock();
191 sock = rcu_dereference_sk_user_data(sk);
192 rcu_read_unlock();
193
194 if (!sock || !sock->peer)
195 return false;
196
197 return !ptr_ring_empty_bh(&sock->recv_ring);
198 }
199
200 static int ovpn_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
202 int noblock,
203 #endif
204 int flags
205 #if LINUX_VERSION_CODE < KERNEL_VERSION(7, 1, 0)
206 , int *addr_len
207 #endif
208 )
209 {
210 bool tmp = flags & MSG_DONTWAIT;
211 DEFINE_WAIT_FUNC(wait, woken_wake_function);
212 int ret, chunk, copied = 0;
213 struct ovpn_socket *sock;
214 struct sk_buff *skb;
215 long timeo;
216
217 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
218 tmp = noblock;
219 #endif
220
221 if (unlikely(flags & MSG_ERRQUEUE))
222 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
223
224 timeo = sock_rcvtimeo(sk, tmp);
225
226 rcu_read_lock();
227 sock = rcu_dereference_sk_user_data(sk);
228 rcu_read_unlock();
229
230 if (!sock || !sock->peer) {
231 ret = -EBADF;
232 goto unlock;
233 }
234
235 while (ptr_ring_empty_bh(&sock->recv_ring)) {
236 if (sk->sk_shutdown & RCV_SHUTDOWN)
237 return 0;
238
239 if (sock_flag(sk, SOCK_DONE))
240 return 0;
241
242 if (!timeo) {
243 ret = -EAGAIN;
244 goto unlock;
245 }
246
247 add_wait_queue(sk_sleep(sk), &wait);
248 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
249 sk_wait_event(sk, &timeo, !ptr_ring_empty_bh(&sock->recv_ring), &wait);
250 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
251 remove_wait_queue(sk_sleep(sk), &wait);
252
253 /* take care of signals */
254 if (signal_pending(current)) {
255 ret = sock_intr_errno(timeo);
256 goto unlock;
257 }
258 }
259
260 while (len && (skb = __ptr_ring_peek(&sock->recv_ring))) {
261 chunk = min_t(size_t, len, skb->len);
262 ret = skb_copy_datagram_msg(skb, 0, msg, chunk);
263 if (ret < 0) {
264 pr_err("ovpn: cannot copy TCP data to userspace: %d\n", ret);
265 kfree_skb(skb);
266 goto unlock;
267 }
268
269 __skb_pull(skb, chunk);
270
271 if (!skb->len) {
272 /* skb was entirely consumed and can now be removed from the ring */
273 __ptr_ring_discard_one(&sock->recv_ring);
274 consume_skb(skb);
275 }
276
277 len -= chunk;
278 copied += chunk;
279 }
280 ret = copied;
281
282 unlock:
283 return ret ? : -EAGAIN;
284 }
285
286 static void ovpn_destroy_skb(void *skb)
287 {
288 consume_skb(skb);
289 }
290
291 void ovpn_tcp_socket_detach(struct socket *sock)
292 {
293 struct ovpn_socket *ovpn_sock;
294 struct ovpn_peer *peer;
295
296 if (!sock)
297 return;
298
299 rcu_read_lock();
300 ovpn_sock = rcu_dereference_sk_user_data(sock->sk);
301 rcu_read_unlock();
302
303 if (!ovpn_sock->peer)
304 return;
305
306 peer = ovpn_sock->peer;
307
308 /* restore CBs that were saved in ovpn_sock_set_tcp_cb() */
309 write_lock_bh(&sock->sk->sk_callback_lock);
310 sock->sk->sk_data_ready = peer->tcp.sk_cb.sk_data_ready;
311 sock->sk->sk_write_space = peer->tcp.sk_cb.sk_write_space;
312 sock->sk->sk_prot = peer->tcp.sk_cb.prot;
313 rcu_assign_sk_user_data(sock->sk, NULL);
314 write_unlock_bh(&sock->sk->sk_callback_lock);
315
316 /* cancel any ongoing work. Done after removing the CBs so that these workers cannot be
317 * re-armed
318 */
319 cancel_work_sync(&peer->tcp.tx_work);
320
321 ptr_ring_cleanup(&ovpn_sock->recv_ring, ovpn_destroy_skb);
322 ptr_ring_cleanup(&peer->tcp.tx_ring, ovpn_destroy_skb);
323 }
324
325 /* Try to send one skb (or part of it) over the TCP stream.
326 *
327 * Return 0 on success or a negative error code otherwise.
328 *
329 * Note that the skb is modified by putting away the data being sent, therefore
330 * the caller should check if skb->len is zero to understand if the full skb was
331 * sent or not.
332 */
333 static int ovpn_tcp_send_one(struct ovpn_peer *peer, struct sk_buff *skb)
334 {
335 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
336 struct kvec iv = { 0 };
337 int ret;
338
339 if (skb_linearize(skb) < 0) {
340 net_err_ratelimited("%s: can't linearize packet\n", __func__);
341 return -ENOMEM;
342 }
343
344 /* initialize iv structure now as skb_linearize() may have changed skb->data */
345 iv.iov_base = skb->data;
346 iv.iov_len = skb->len;
347
348 ret = kernel_sendmsg(peer->sock->sock, &msg, &iv, 1, iv.iov_len);
349 if (ret > 0) {
350 __skb_pull(skb, ret);
351
352 /* since we update per-cpu stats in process context,
353 * we need to disable softirqs
354 */
355 local_bh_disable();
356 dev_sw_netstats_tx_add(peer->ovpn->dev, 1, ret);
357 local_bh_enable();
358
359 return 0;
360 }
361
362 return ret;
363 }
364
365 /* Process packets in TCP TX queue */
366 static void ovpn_tcp_tx_work(struct work_struct *work)
367 {
368 struct ovpn_peer *peer;
369 struct sk_buff *skb;
370 int ret;
371
372 peer = container_of(work, struct ovpn_peer, tcp.tx_work);
373 while ((skb = __ptr_ring_peek(&peer->tcp.tx_ring))) {
374 ret = ovpn_tcp_send_one(peer, skb);
375 if (ret < 0 && ret != -EAGAIN) {
376 net_warn_ratelimited("%s: cannot send TCP packet to peer %u: %d\n", __func__,
377 peer->id, ret);
378 /* in case of TCP error stop sending loop and delete peer */
379 ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_TRANSPORT_ERROR);
380 break;
381 } else if (!skb->len) {
382 /* skb was entirely consumed and can now be removed from the ring */
383 __ptr_ring_discard_one(&peer->tcp.tx_ring);
384 consume_skb(skb);
385 }
386
387 /* give a chance to be rescheduled if needed */
388 cond_resched();
389 }
390 }
391
392 /* Put packet into TCP TX queue and schedule a consumer */
393 void ovpn_queue_tcp_skb(struct ovpn_peer *peer, struct sk_buff *skb)
394 {
395 int ret;
396
397 ret = ptr_ring_produce_bh(&peer->tcp.tx_ring, skb);
398 if (ret < 0) {
399 kfree_skb_list(skb);
400 return;
401 }
402
403 queue_work(peer->ovpn->events_wq, &peer->tcp.tx_work);
404 }
405
406 /* Set TCP encapsulation callbacks */
407 int ovpn_tcp_socket_attach(struct socket *sock, struct ovpn_peer *peer)
408 {
409 void *old_data;
410 int ret;
411
412 INIT_WORK(&peer->tcp.tx_work, ovpn_tcp_tx_work);
413
414 ret = ptr_ring_init(&peer->tcp.tx_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
415 if (ret < 0) {
416 netdev_err(peer->ovpn->dev, "cannot allocate TCP TX ring\n");
417 return ret;
418 }
419
420 peer->tcp.skb = NULL;
421 peer->tcp.offset = 0;
422 peer->tcp.data_len = 0;
423
424 write_lock_bh(&sock->sk->sk_callback_lock);
425
426 /* make sure no pre-existing encapsulation handler exists */
427 rcu_read_lock();
428 old_data = rcu_dereference_sk_user_data(sock->sk);
429 rcu_read_unlock();
430 if (old_data) {
431 netdev_err(peer->ovpn->dev, "provided socket already taken by other user\n");
432 ret = -EBUSY;
433 goto err;
434 }
435
436 /* sanity check */
437 if (sock->sk->sk_protocol != IPPROTO_TCP) {
438 netdev_err(peer->ovpn->dev, "provided socket is UDP but expected TCP\n");
439 ret = -EINVAL;
440 goto err;
441 }
442
443 /* only a fully connected socket are expected. Connection should be handled in userspace */
444 if (sock->sk->sk_state != TCP_ESTABLISHED) {
445 netdev_err(peer->ovpn->dev, "provided TCP socket is not in ESTABLISHED state: %d\n",
446 sock->sk->sk_state);
447 ret = -EINVAL;
448 goto err;
449 }
450
451 /* save current CBs so that they can be restored upon socket release */
452 peer->tcp.sk_cb.sk_data_ready = sock->sk->sk_data_ready;
453 peer->tcp.sk_cb.sk_write_space = sock->sk->sk_write_space;
454 peer->tcp.sk_cb.prot = sock->sk->sk_prot;
455
456 /* assign our static CBs */
457 sock->sk->sk_data_ready = ovpn_tcp_data_ready;
458 sock->sk->sk_write_space = ovpn_tcp_write_space;
459 sock->sk->sk_prot = &ovpn_tcp_prot;
460
461 write_unlock_bh(&sock->sk->sk_callback_lock);
462
463 return 0;
464 err:
465 write_unlock_bh(&sock->sk->sk_callback_lock);
466 ptr_ring_cleanup(&peer->tcp.tx_ring, NULL);
467
468 return ret;
469 }
470
471 int __init ovpn_tcp_init(void)
472 {
473 /* We need to substitute the recvmsg and the sock_is_readable
474 * callbacks in the sk_prot member of the sock object for TCP
475 * sockets.
476 *
477 * However sock->sk_prot is a pointer to a static variable and
478 * therefore we can't directly modify it, otherwise every socket
479 * pointing to it will be affected.
480 *
481 * For this reason we create our own static copy and modify what
482 * we need. Then we make sk_prot point to this copy
483 * (in ovpn_tcp_socket_attach())
484 */
485 ovpn_tcp_prot = tcp_prot;
486 ovpn_tcp_prot.recvmsg = ovpn_tcp_recvmsg;
487 ovpn_tcp_prot.sock_is_readable = ovpn_tcp_sock_is_readable;
488
489 return 0;
490 }