Debian Codegraph @

drivers/net/ovpn-dco/ovpn.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: James Yonan <[email protected]>
7 * Antonio Quartulli <[email protected]>
8 */
9
10 #include "main.h"
11 #include "bind.h"
12 #include "netlink.h"
13 #include "ovpn.h"
14 #include "sock.h"
15 #include "peer.h"
16 #include "stats.h"
17 #include "proto.h"
18 #include "crypto.h"
19 #include "crypto_aead.h"
20 #include "skb.h"
21 #include "tcp.h"
22 #include "udp.h"
23
24 #include <linux/workqueue.h>
25 #include <net/gso.h>
26 #include <uapi/linux/if_ether.h>
27
28 static const unsigned char ovpn_keepalive_message[] = {
29 0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
30 0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
31 };
32
33 static const unsigned char ovpn_explicit_exit_notify_message[] = {
34 0x28, 0x7f, 0x34, 0x6b, 0xd4, 0xef, 0x7a, 0x81,
35 0x2d, 0x56, 0xb8, 0xd3, 0xaf, 0xc5, 0x45, 0x9c,
36 6 // OCC_EXIT
37 };
38
39 /* Is keepalive message?
40 * Assumes that single byte at skb->data is defined.
41 */
42 static bool ovpn_is_keepalive(struct sk_buff *skb)
43 {
44 if (*skb->data != OVPN_KEEPALIVE_FIRST_BYTE)
45 return false;
46
47 if (!pskb_may_pull(skb, sizeof(ovpn_keepalive_message)))
48 return false;
49
50 return !memcmp(skb->data, ovpn_keepalive_message,
51 sizeof(ovpn_keepalive_message));
52 }
53
54 int ovpn_struct_init(struct net_device *dev)
55 {
56 struct ovpn_struct *ovpn = netdev_priv(dev);
57 int err;
58
59 memset(ovpn, 0, sizeof(*ovpn));
60
61 ovpn->dev = dev;
62
63 err = ovpn_netlink_init(ovpn);
64 if (err < 0)
65 return err;
66
67 spin_lock_init(&ovpn->lock);
68 spin_lock_init(&ovpn->peers.lock);
69 mutex_init(&ovpn->napi_lock);
70
71 ovpn->crypto_wq = alloc_workqueue("ovpn-crypto-wq-%s",
72 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0,
73 dev->name);
74 if (!ovpn->crypto_wq)
75 return -ENOMEM;
76
77 ovpn->events_wq = alloc_workqueue("ovpn-event-wq-%s", WQ_MEM_RECLAIM, 0, dev->name);
78 if (!ovpn->events_wq)
79 return -ENOMEM;
80
81 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
82 if (!dev->tstats)
83 return -ENOMEM;
84
85 err = security_tun_dev_alloc_security(&ovpn->security);
86 if (err < 0)
87 return err;
88
89 /* kernel -> userspace tun queue length */
90 ovpn->max_tun_queue_len = OVPN_MAX_TUN_QUEUE_LEN;
91
92 return 0;
93 }
94
95 /* Called after decrypt to write IP packet to tun netdev.
96 * This method is expected to manage/free skb.
97 */
98 static void tun_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
99 {
100 /* packet integrity was verified on the VPN layer - no need to perform
101 * any additional check along the stack
102 */
103 skb->ip_summed = CHECKSUM_UNNECESSARY;
104 skb->csum_level = ~0;
105
106 /* skb hash for transport packet no longer valid after decapsulation */
107 skb_clear_hash(skb);
108
109 /* post-decrypt scrub -- prepare to inject encapsulated packet onto tun
110 * interface, based on __skb_tunnel_rx() in dst.h
111 */
112 skb->dev = peer->ovpn->dev;
113 skb_set_queue_mapping(skb, 0);
114 skb_scrub_packet(skb, true);
115
116 skb_reset_network_header(skb);
117 skb_reset_transport_header(skb);
118 skb_probe_transport_header(skb);
119 skb_reset_inner_headers(skb);
120
121 /* update per-cpu RX stats with the stored size of encrypted packet */
122
123 /* we are in softirq context - hence no locking nor disable preemption needed */
124 dev_sw_netstats_rx_add(peer->ovpn->dev, skb->len);
125
126 /* cause packet to be "received" by tun interface */
127 napi_gro_receive(&peer->napi, skb);
128 }
129
130 int ovpn_napi_poll(struct napi_struct *napi, int budget)
131 {
132 struct ovpn_peer *peer = container_of(napi, struct ovpn_peer, napi);
133 struct sk_buff *skb;
134 int work_done = 0;
135
136 if (unlikely(budget <= 0))
137 return 0;
138 /* this function should schedule at most 'budget' number of
139 * packets for delivery to the tun interface.
140 * If in the queue we have more packets than what allowed by the
141 * budget, the next polling will take care of those
142 */
143 while ((work_done < budget) &&
144 (skb = ptr_ring_consume_bh(&peer->netif_rx_ring))) {
145 tun_netdev_write(peer, skb);
146 work_done++;
147 }
148
149 if (work_done < budget)
150 napi_complete_done(napi, work_done);
151
152 return work_done;
153 }
154
155 /* Entry point for processing an incoming packet (in skb form)
156 *
157 * Enqueue the packet and schedule RX consumer.
158 * Reference to peer is dropped only in case of success.
159 *
160 * Return 0 if the packet was handled (and consumed)
161 * Return <0 in case of error (return value is error code)
162 */
163 int ovpn_recv(struct ovpn_struct *ovpn, struct ovpn_peer *peer, struct sk_buff *skb)
164 {
165 if (unlikely(ptr_ring_produce_bh(&peer->rx_ring, skb) < 0))
166 return -ENOSPC;
167
168 if (!queue_work(ovpn->crypto_wq, &peer->decrypt_work))
169 ovpn_peer_put(peer);
170
171 return 0;
172 }
173
174 static int ovpn_decrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
175 {
176 struct ovpn_peer *allowed_peer = NULL;
177 struct ovpn_crypto_key_slot *ks;
178 __be16 proto;
179 int ret = -1;
180 u8 key_id;
181
182 ovpn_peer_stats_increment_rx(&peer->link_stats, skb->len);
183
184 /* get the key slot matching the key Id in the received packet */
185 key_id = ovpn_key_id_from_skb(skb);
186 ks = ovpn_crypto_key_id_to_slot(&peer->crypto, key_id);
187 if (unlikely(!ks)) {
188 net_info_ratelimited("%s: no available key for peer %u, key-id: %u\n", __func__,
189 peer->id, key_id);
190 goto drop;
191 }
192
193 /* decrypt */
194 ret = ovpn_aead_decrypt(ks, skb);
195
196 ovpn_crypto_key_slot_put(ks);
197
198 if (unlikely(ret < 0)) {
199 net_err_ratelimited("%s: error during decryption for peer %u, key-id %u: %d\n",
200 __func__, peer->id, key_id, ret);
201 goto drop;
202 }
203
204 /* note event of authenticated packet received for keepalive */
205 ovpn_peer_keepalive_recv_reset(peer);
206
207 /* update source and destination endpoint for this peer */
208 if (peer->sock->sock->sk->sk_protocol == IPPROTO_UDP)
209 ovpn_peer_update_local_endpoint(peer, skb);
210
211 /* increment RX stats */
212 ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len);
213
214 /* check if this is a valid datapacket that has to be delivered to the
215 * tun interface
216 */
217 skb_reset_network_header(skb);
218 proto = ovpn_ip_check_protocol(skb);
219 if (unlikely(!proto)) {
220 /* check if null packet */
221 if (unlikely(!pskb_may_pull(skb, 1))) {
222 ret = -EINVAL;
223 goto drop;
224 }
225
226 /* check if special OpenVPN message */
227 if (ovpn_is_keepalive(skb)) {
228 netdev_dbg(peer->ovpn->dev, "%s: ping received from peer with id %u\n",
229 __func__, peer->id);
230 /* not an error */
231 consume_skb(skb);
232 /* inform the caller that NAPI should not be scheduled
233 * for this packet
234 */
235 return -1;
236 }
237
238 ret = -EPROTONOSUPPORT;
239 goto drop;
240 }
241 skb->protocol = proto;
242
243 /* perform Reverse Path Filtering (RPF) */
244 allowed_peer = ovpn_peer_lookup_vpn_addr(peer->ovpn, skb, true);
245 if (unlikely(allowed_peer != peer)) {
246 ret = -EPERM;
247 goto drop;
248 }
249
250 ret = ptr_ring_produce_bh(&peer->netif_rx_ring, skb);
251 drop:
252 if (likely(allowed_peer))
253 ovpn_peer_put(allowed_peer);
254
255 if (unlikely(ret < 0))
256 kfree_skb(skb);
257
258 return ret;
259 }
260
261 /* pick packet from RX queue, decrypt and forward it to the tun device */
262 void ovpn_decrypt_work(struct work_struct *work)
263 {
264 struct ovpn_peer *peer;
265 struct sk_buff *skb;
266
267 peer = container_of(work, struct ovpn_peer, decrypt_work);
268 while ((skb = ptr_ring_consume_bh(&peer->rx_ring))) {
269 if (likely(ovpn_decrypt_one(peer, skb) == 0)) {
270 /* if a packet has been enqueued for NAPI, signal
271 * availability to the networking stack
272 */
273 local_bh_disable();
274 napi_schedule(&peer->napi);
275 local_bh_enable();
276 }
277
278 /* give a chance to be rescheduled if needed */
279 cond_resched();
280 }
281 ovpn_peer_put(peer);
282 }
283
284 static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
285 {
286 struct ovpn_crypto_key_slot *ks;
287 bool success = false;
288 int ret;
289
290 /* get primary key to be used for encrypting data */
291 ks = ovpn_crypto_key_slot_primary(&peer->crypto);
292 if (unlikely(!ks)) {
293 net_warn_ratelimited("%s: error while retrieving primary key slot\n", __func__);
294 return false;
295 }
296
297 if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL &&
298 skb_checksum_help(skb))) {
299 net_err_ratelimited("%s: cannot compute checksum for outgoing packet\n", __func__);
300 goto err;
301 }
302
303 ovpn_peer_stats_increment_tx(&peer->vpn_stats, skb->len);
304
305 /* encrypt */
306 ret = ovpn_aead_encrypt(ks, skb, peer->id);
307 if (unlikely(ret < 0)) {
308 /* if we ran out of IVs we must kill the key as it can't be used anymore */
309 if (ret == -ERANGE) {
310 netdev_warn(peer->ovpn->dev,
311 "%s: killing primary key as we ran out of IVs\n", __func__);
312 ovpn_crypto_kill_primary(&peer->crypto);
313 goto err;
314 }
315 net_err_ratelimited("%s: error during encryption for peer %u, key-id %u: %d\n",
316 __func__, peer->id, ks->key_id, ret);
317 goto err;
318 }
319
320 success = true;
321
322 ovpn_peer_stats_increment_tx(&peer->link_stats, skb->len);
323 err:
324 ovpn_crypto_key_slot_put(ks);
325 return success;
326 }
327
328 /* Process packets in TX queue in a transport-specific way.
329 *
330 * UDP transport - encrypt and send across the tunnel.
331 * TCP transport - encrypt and put into TCP TX queue.
332 */
333 void ovpn_encrypt_work(struct work_struct *work)
334 {
335 struct sk_buff *skb, *curr, *next;
336 struct ovpn_peer *peer;
337
338 peer = container_of(work, struct ovpn_peer, encrypt_work);
339 while ((skb = ptr_ring_consume_bh(&peer->tx_ring))) {
340 /* this might be a GSO-segmented skb list: process each skb
341 * independently
342 */
343 skb_list_walk_safe(skb, curr, next) {
344 /* if one segment fails encryption, we drop the entire
345 * packet, because it does not really make sense to send
346 * only part of it at this point
347 */
348 if (unlikely(!ovpn_encrypt_one(peer, curr))) {
349 kfree_skb_list(skb);
350 skb = NULL;
351 break;
352 }
353 }
354
355 /* successful encryption */
356 if (skb) {
357 skb_list_walk_safe(skb, curr, next) {
358 skb_mark_not_on_list(curr);
359
360 switch (peer->sock->sock->sk->sk_protocol) {
361 case IPPROTO_UDP:
362 ovpn_udp_send_skb(peer->ovpn, peer, curr);
363 break;
364 case IPPROTO_TCP:
365 ovpn_tcp_send_skb(peer, curr);
366 break;
367 default:
368 /* no transport configured yet */
369 consume_skb(skb);
370 break;
371 }
372 }
373
374 /* note event of authenticated packet xmit for keepalive */
375 ovpn_peer_keepalive_xmit_reset(peer);
376 }
377
378 /* give a chance to be rescheduled if needed */
379 cond_resched();
380 }
381 ovpn_peer_put(peer);
382 }
383
384 /* Put skb into TX queue and schedule a consumer */
385 static void ovpn_queue_skb(struct ovpn_struct *ovpn, struct sk_buff *skb, struct ovpn_peer *peer)
386 {
387 int ret;
388
389 if (likely(!peer))
390 peer = ovpn_peer_lookup_vpn_addr(ovpn, skb, false);
391 if (unlikely(!peer)) {
392 net_dbg_ratelimited("%s: no peer to send data to\n", ovpn->dev->name);
393 goto drop;
394 }
395
396 ret = ptr_ring_produce_bh(&peer->tx_ring, skb);
397 if (unlikely(ret < 0)) {
398 net_err_ratelimited("%s: cannot queue packet to TX ring\n", __func__);
399 goto drop;
400 }
401
402 if (!queue_work(ovpn->crypto_wq, &peer->encrypt_work))
403 ovpn_peer_put(peer);
404
405 return;
406 drop:
407 if (peer)
408 ovpn_peer_put(peer);
409 kfree_skb_list(skb);
410 }
411
412 /* Net device start xmit
413 */
414 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
415 {
416 struct ovpn_struct *ovpn = netdev_priv(dev);
417 struct sk_buff *segments, *tmp, *curr, *next;
418 struct sk_buff_head skb_list;
419 __be16 proto;
420 int ret;
421
422 /* reset netfilter state */
423 nf_reset_ct(skb);
424
425 /* verify IP header size in network packet */
426 proto = ovpn_ip_check_protocol(skb);
427 if (unlikely(!proto || skb->protocol != proto)) {
428 net_err_ratelimited("%s: dropping malformed payload packet\n",
429 dev->name);
430 goto drop;
431 }
432
433 if (skb_is_gso(skb)) {
434 segments = skb_gso_segment(skb, 0);
435 if (IS_ERR(segments)) {
436 ret = PTR_ERR(segments);
437 net_err_ratelimited("%s: cannot segment packet: %d\n", dev->name, ret);
438 goto drop;
439 }
440
441 consume_skb(skb);
442 skb = segments;
443 }
444
445 /* from this moment on, "skb" might be a list */
446
447 __skb_queue_head_init(&skb_list);
448 skb_list_walk_safe(skb, curr, next) {
449 skb_mark_not_on_list(curr);
450
451 tmp = skb_share_check(curr, GFP_ATOMIC);
452 if (unlikely(!tmp)) {
453 kfree_skb_list(next);
454 net_err_ratelimited("%s: skb_share_check failed\n", dev->name);
455 goto drop_list;
456 }
457
458 __skb_queue_tail(&skb_list, tmp);
459 }
460 skb_list.prev->next = NULL;
461
462 ovpn_queue_skb(ovpn, skb_list.next, NULL);
463
464 return NETDEV_TX_OK;
465
466 drop_list:
467 skb_queue_walk_safe(&skb_list, curr, next)
468 kfree_skb(curr);
469 drop:
470 skb_tx_error(skb);
471 kfree_skb_list(skb);
472 return NET_XMIT_DROP;
473 }
474
475 /* Encrypt and transmit a special message to peer, such as keepalive
476 * or explicit-exit-notify. Called from softirq context.
477 * Assumes that caller holds a reference to peer.
478 */
479 static void ovpn_xmit_special(struct ovpn_peer *peer, const void *data,
480 const unsigned int len)
481 {
482 struct ovpn_struct *ovpn;
483 struct sk_buff *skb;
484
485 ovpn = peer->ovpn;
486 if (unlikely(!ovpn))
487 return;
488
489 skb = alloc_skb(256 + len, GFP_ATOMIC);
490 if (unlikely(!skb))
491 return;
492
493 skb_reserve(skb, 128);
494 skb->priority = TC_PRIO_BESTEFFORT;
495 memcpy(__skb_put(skb, len), data, len);
496
497 /* increase reference counter when passing peer to sending queue */
498 if (!ovpn_peer_hold(peer)) {
499 netdev_dbg(ovpn->dev, "%s: cannot hold peer reference for sending special packet\n",
500 __func__);
501 kfree_skb(skb);
502 return;
503 }
504
505 ovpn_queue_skb(ovpn, skb, peer);
506 }
507
508 void ovpn_keepalive_xmit(struct ovpn_peer *peer)
509 {
510 ovpn_xmit_special(peer, ovpn_keepalive_message,
511 sizeof(ovpn_keepalive_message));
512 }
513
514 /* Transmit explicit exit notification.
515 * Called from process context.
516 */
517 void ovpn_explicit_exit_notify_xmit(struct ovpn_peer *peer)
518 {
519 ovpn_xmit_special(peer, ovpn_explicit_exit_notify_message,
520 sizeof(ovpn_explicit_exit_notify_message));
521 }
522
523 /* Copy buffer into skb and send it across the tunnel.
524 *
525 * For UDP transport: just sent the skb to peer
526 * For TCP transport: put skb into TX queue
527 */
528 int ovpn_send_data(struct ovpn_struct *ovpn, u32 peer_id, const u8 *data, size_t len)
529 {
530 u16 skb_len = SKB_HEADER_LEN + len;
531 struct ovpn_peer *peer;
532 struct sk_buff *skb;
533 bool tcp = false;
534 int ret = 0;
535
536 peer = ovpn_peer_lookup_id(ovpn, peer_id);
537 if (unlikely(!peer)) {
538 netdev_dbg(ovpn->dev, "no peer to send data to\n");
539 return -EHOSTUNREACH;
540 }
541
542 if (peer->sock->sock->sk->sk_protocol == IPPROTO_TCP) {
543 skb_len += sizeof(u16);
544 tcp = true;
545 }
546
547 skb = alloc_skb(skb_len, GFP_ATOMIC);
548 if (unlikely(!skb)) {
549 ret = -ENOMEM;
550 goto out;
551 }
552
553 skb_reserve(skb, SKB_HEADER_LEN);
554 skb_put_data(skb, data, len);
555
556 /* prepend TCP packet with size, as required by OpenVPN protocol */
557 if (tcp) {
558 *(__be16 *)__skb_push(skb, sizeof(u16)) = htons(len);
559 ovpn_queue_tcp_skb(peer, skb);
560 } else {
561 ovpn_udp_send_skb(ovpn, peer, skb);
562 }
563 out:
564 ovpn_peer_put(peer);
565 return ret;
566 }