Debian Codegraph @

drivers/net/ovpn-dco/sock.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
2 /* OpenVPN data channel accelerator
3 *
4 * Copyright (C) 2020- OpenVPN, Inc.
5 *
6 * Author: James Yonan <[email protected]>
7 * Antonio Quartulli <[email protected]>
8 */
9
10 #include "main.h"
11 #include "ovpn.h"
12 #include "peer.h"
13 #include "sock.h"
14 #include "rcu.h"
15 #include "tcp.h"
16 #include "udp.h"
17
18 #include <net/udp.h>
19 #include <net/udp_tunnel.h>
20
21 /* Finalize release of socket, called after RCU grace period */
22 static void ovpn_socket_detach(struct socket *sock)
23 {
24 if (!sock)
25 return;
26
27 if (sock->sk->sk_protocol == IPPROTO_UDP)
28 ovpn_udp_socket_detach(sock);
29 else if (sock->sk->sk_protocol == IPPROTO_TCP)
30 ovpn_tcp_socket_detach(sock);
31
32 sockfd_put(sock);
33 }
34
35 void ovpn_socket_release_kref(struct kref *kref)
36 {
37 struct ovpn_socket *sock = container_of(kref, struct ovpn_socket, refcount);
38
39 ovpn_socket_detach(sock->sock);
40 kfree_rcu(sock, rcu);
41 }
42
43 static bool ovpn_socket_hold(struct ovpn_socket *sock)
44 {
45 return kref_get_unless_zero(&sock->refcount);
46 }
47
48 static struct ovpn_socket *ovpn_socket_get(struct socket *sock)
49 {
50 struct ovpn_socket *ovpn_sock;
51
52 rcu_read_lock();
53 ovpn_sock = rcu_dereference_sk_user_data(sock->sk);
54 if (!ovpn_socket_hold(ovpn_sock)) {
55 pr_warn("%s: found ovpn_socket with ref = 0\n", __func__);
56 ovpn_sock = NULL;
57 }
58 rcu_read_unlock();
59
60 return ovpn_sock;
61 }
62
63 /* Finalize release of socket, called after RCU grace period */
64 static int ovpn_socket_attach(struct socket *sock, struct ovpn_peer *peer)
65 {
66 int ret = -EOPNOTSUPP;
67
68 if (!sock || !peer)
69 return -EINVAL;
70
71 if (sock->sk->sk_protocol == IPPROTO_UDP)
72 ret = ovpn_udp_socket_attach(sock, peer->ovpn);
73 else if (sock->sk->sk_protocol == IPPROTO_TCP)
74 ret = ovpn_tcp_socket_attach(sock, peer);
75
76 return ret;
77 }
78
79 struct ovpn_struct *ovpn_from_udp_sock(struct sock *sk)
80 {
81 struct ovpn_socket *ovpn_sock;
82
83 ovpn_rcu_lockdep_assert_held();
84
85 if (unlikely(READ_ONCE(udp_sk(sk)->encap_type) != UDP_ENCAP_OVPNINUDP))
86 return NULL;
87
88 ovpn_sock = rcu_dereference_sk_user_data(sk);
89 if (unlikely(!ovpn_sock))
90 return NULL;
91
92 /* make sure that sk matches our stored transport socket */
93 if (unlikely(!ovpn_sock->sock || sk != ovpn_sock->sock->sk))
94 return NULL;
95
96 return ovpn_sock->ovpn;
97 }
98
99 struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
100 {
101 struct ovpn_socket *ovpn_sock;
102 int ret;
103
104 ret = ovpn_socket_attach(sock, peer);
105 if (ret < 0 && ret != -EALREADY)
106 return ERR_PTR(ret);
107
108 /* if this socket is already owned by this interface, just increase the refcounter */
109 if (ret == -EALREADY) {
110 /* caller is expected to increase the sock refcounter before passing it to this
111 * function. For this reason we drop it if not needed, like when this socket is
112 * already owned.
113 */
114 ovpn_sock = ovpn_socket_get(sock);
115 sockfd_put(sock);
116 return ovpn_sock;
117 }
118
119 ovpn_sock = kzalloc(sizeof(*ovpn_sock), GFP_KERNEL);
120 if (!ovpn_sock)
121 return ERR_PTR(-ENOMEM);
122
123 ovpn_sock->ovpn = peer->ovpn;
124 ovpn_sock->sock = sock;
125 kref_init(&ovpn_sock->refcount);
126
127 /* TCP sockets are per-peer, therefore they are linked to their unique peer */
128 if (sock->sk->sk_protocol == IPPROTO_TCP) {
129 ovpn_sock->peer = peer;
130 ret = ptr_ring_init(&ovpn_sock->recv_ring, OVPN_QUEUE_LEN, GFP_KERNEL);
131 if (ret < 0) {
132 netdev_err(peer->ovpn->dev, "%s: cannot allocate TCP recv ring\n",
133 __func__);
134 goto err;
135 }
136 }
137
138 rcu_assign_sk_user_data(sock->sk, ovpn_sock);
139
140 return ovpn_sock;
141 err:
142 kfree(ovpn_sock);
143 return ERR_PTR(ret);
144 }