Debian Codegraph @

drivers/net/ovpn-dco/peer.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
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 #ifndef _NET_OVPN_DCO_OVPNPEER_H_
11 #define _NET_OVPN_DCO_OVPNPEER_H_
12
13 #include "addr.h"
14 #include "bind.h"
15 #include "sock.h"
16 #include "stats.h"
17
18 #include <linux/timer.h>
19 #include <linux/ptr_ring.h>
20 #include <net/dst_cache.h>
21
22 struct ovpn_peer {
23 struct ovpn_struct *ovpn;
24
25 u32 id;
26
27 struct {
28 struct in_addr ipv4;
29 struct in6_addr ipv6;
30 } vpn_addrs;
31
32 struct hlist_node hash_entry_id;
33 struct hlist_node hash_entry_addr4;
34 struct hlist_node hash_entry_addr6;
35 struct hlist_node hash_entry_transp_addr;
36
37 /* work objects to handle encryption/decryption of packets.
38 * these works are queued on the ovpn->crypt_wq workqueue.
39 */
40 struct work_struct encrypt_work;
41 struct work_struct decrypt_work;
42
43 struct ptr_ring tx_ring;
44 struct ptr_ring rx_ring;
45 struct ptr_ring netif_rx_ring;
46
47 struct napi_struct napi;
48
49 struct ovpn_socket *sock;
50
51 /* state of the TCP reading. Needed to keep track of how much of a single packet has already
52 * been read from the stream and how much is missing
53 */
54 struct {
55 struct ptr_ring tx_ring;
56 struct work_struct tx_work;
57 struct work_struct rx_work;
58
59 u8 raw_len[sizeof(u16)];
60 struct sk_buff *skb;
61 u16 offset;
62 u16 data_len;
63 struct {
64 void (*sk_state_change)(struct sock *sk);
65 void (*sk_data_ready)(struct sock *sk);
66 void (*sk_write_space)(struct sock *sk);
67 struct proto *prot;
68 } sk_cb;
69 } tcp;
70
71 struct dst_cache dst_cache;
72
73 /* our crypto state */
74 struct ovpn_crypto_state crypto;
75
76 /* our binding to peer, protected by spinlock */
77 struct ovpn_bind __rcu *bind;
78
79 /* timer used to send periodic ping messages to the other peer, if no
80 * other data was sent within the past keepalive_interval seconds
81 */
82 struct timer_list keepalive_xmit;
83 /* keepalive interval in seconds */
84 unsigned long keepalive_interval;
85
86 /* timer used to mark a peer as expired when no data is received for
87 * keepalive_timeout seconds
88 */
89 struct timer_list keepalive_recv;
90 /* keepalive timeout in seconds */
91 unsigned long keepalive_timeout;
92
93 /* true if ovpn_peer_mark_delete was called */
94 bool halt;
95
96 /* per-peer in-VPN rx/tx stats */
97 struct ovpn_peer_stats vpn_stats;
98
99 /* per-peer link/transport rx/tx stats */
100 struct ovpn_peer_stats link_stats;
101
102 /* why peer was deleted - keepalive timeout, module removed etc */
103 enum ovpn_del_peer_reason delete_reason;
104
105 /* protects binding to peer (bind) and timers
106 * (keepalive_xmit, keepalive_expire)
107 */
108 spinlock_t lock;
109
110 /* needed because crypto methods can go async */
111 struct kref refcount;
112
113 /* needed to free a peer in an RCU safe way */
114 struct rcu_head rcu;
115
116 /* needed to notify userspace about deletion */
117 struct work_struct delete_work;
118 };
119
120 void ovpn_peer_release_kref(struct kref *kref);
121 void ovpn_peer_release(struct ovpn_peer *peer);
122
123 static inline bool ovpn_peer_hold(struct ovpn_peer *peer)
124 {
125 return kref_get_unless_zero(&peer->refcount);
126 }
127
128 static inline void ovpn_peer_put(struct ovpn_peer *peer)
129 {
130 kref_put(&peer->refcount, ovpn_peer_release_kref);
131 }
132
133 static inline void ovpn_peer_keepalive_recv_reset(struct ovpn_peer *peer)
134 {
135 u32 delta = msecs_to_jiffies(peer->keepalive_timeout * MSEC_PER_SEC);
136
137 if (unlikely(!delta))
138 return;
139
140 mod_timer(&peer->keepalive_recv, jiffies + delta);
141 }
142
143 static inline void ovpn_peer_keepalive_xmit_reset(struct ovpn_peer *peer)
144 {
145 u32 delta = msecs_to_jiffies(peer->keepalive_interval * MSEC_PER_SEC);
146
147 if (unlikely(!delta))
148 return;
149
150 mod_timer(&peer->keepalive_xmit, jiffies + delta);
151 }
152
153 struct ovpn_peer *ovpn_peer_new(struct ovpn_struct *ovpn, const struct sockaddr_storage *sa,
154 struct socket *sock, u32 id, uint8_t *local_ip);
155
156 void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout);
157
158 int ovpn_peer_add(struct ovpn_struct *ovpn, struct ovpn_peer *peer);
159 int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason);
160 struct ovpn_peer *ovpn_peer_find(struct ovpn_struct *ovpn, u32 peer_id);
161 void ovpn_peer_release_p2p(struct ovpn_struct *ovpn);
162 void ovpn_peers_free(struct ovpn_struct *ovpn);
163
164 struct ovpn_peer *ovpn_peer_lookup_transp_addr(struct ovpn_struct *ovpn, struct sk_buff *skb);
165 struct ovpn_peer *ovpn_peer_lookup_vpn_addr(struct ovpn_struct *ovpn, struct sk_buff *skb,
166 bool use_src);
167 struct ovpn_peer *ovpn_peer_lookup_id(struct ovpn_struct *ovpn, u32 peer_id);
168
169 void ovpn_peer_update_local_endpoint(struct ovpn_peer *peer, struct sk_buff *skb);
170 void ovpn_peer_float(struct ovpn_peer *peer, struct sk_buff *skb);
171
172 #endif /* _NET_OVPN_DCO_OVPNPEER_H_ */