xref: /linux/drivers/net/ovpn/peer.h (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* OpenVPN data channel offload
3  *
4  *  Copyright (C) 2020-2025 OpenVPN, Inc.
5  *
6  *  Author:	James Yonan <james@openvpn.net>
7  *		Antonio Quartulli <antonio@openvpn.net>
8  */
9 
10 #ifndef _NET_OVPN_OVPNPEER_H_
11 #define _NET_OVPN_OVPNPEER_H_
12 
13 #include <net/dst_cache.h>
14 #include <net/strparser.h>
15 
16 #include "crypto.h"
17 #include "socket.h"
18 #include "stats.h"
19 
20 /**
21  * struct ovpn_peer - the main remote peer object
22  * @ovpn: main openvpn instance this peer belongs to
23  * @dev_tracker: reference tracker for associated dev
24  * @id: unique identifier, used to match incoming packets
25  * @tx_id: identifier to be used in TX packets
26  * @vpn_addrs: IP addresses assigned over the tunnel
27  * @vpn_addrs.ipv4: IPv4 assigned to peer on the tunnel
28  * @vpn_addrs.ipv6: IPv6 assigned to peer on the tunnel
29  * @hash_entry_id: entry in the peer ID hashtable
30  * @hash_entry_addr4: entry in the peer IPv4 hashtable
31  * @hash_entry_addr6: entry in the peer IPv6 hashtable
32  * @hash_entry_transp_addr: entry in the peer transport address hashtable
33  * @sock: the socket being used to talk to this peer
34  * @tcp: keeps track of TCP specific state
35  * @tcp.strp: stream parser context (TCP only)
36  * @tcp.user_queue: received packets that have to go to userspace (TCP only)
37  * @tcp.out_queue: packets on hold while socket is taken by user (TCP only)
38  * @tcp.tx_in_progress: true if TX is already ongoing (TCP only)
39  * @tcp.out_msg.skb: packet scheduled for sending (TCP only)
40  * @tcp.out_msg.offset: offset where next send should start (TCP only)
41  * @tcp.out_msg.len: remaining data to send within packet (TCP only)
42  * @tcp.sk_cb.sk_data_ready: pointer to original cb (TCP only)
43  * @tcp.sk_cb.sk_write_space: pointer to original cb (TCP only)
44  * @tcp.sk_cb.prot: pointer to original prot object (TCP only)
45  * @tcp.sk_cb.ops: pointer to the original prot_ops object (TCP only)
46  * @crypto: the crypto configuration (ciphers, keys, etc..)
47  * @dst_cache: cache for dst_entry used to send to peer
48  * @bind: remote peer binding
49  * @keepalive_interval: seconds after which a new keepalive should be sent
50  * @keepalive_xmit_exp: future timestamp when next keepalive should be sent
51  * @last_sent: timestamp of the last successfully sent packet
52  * @keepalive_timeout: seconds after which an inactive peer is considered dead
53  * @keepalive_recv_exp: future timestamp when the peer should expire
54  * @last_recv: timestamp of the last authenticated received packet
55  * @vpn_stats: per-peer in-VPN TX/RX stats
56  * @link_stats: per-peer link/transport TX/RX stats
57  * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..)
58  * @lock: protects binding to peer (bind) and keepalive* fields
59  * @refcount: reference counter
60  * @rcu: used to free peer in an RCU safe way
61  * @release_entry: entry for the socket release list
62  * @keepalive_work: used to schedule keepalive sending
63  */
64 struct ovpn_peer {
65 	struct ovpn_priv *ovpn;
66 	netdevice_tracker dev_tracker;
67 	u32 id;
68 	u32 tx_id;
69 	struct {
70 		struct in_addr ipv4;
71 		struct in6_addr ipv6;
72 	} vpn_addrs;
73 	struct hlist_node hash_entry_id;
74 	struct hlist_nulls_node hash_entry_addr4;
75 	struct hlist_nulls_node hash_entry_addr6;
76 	struct hlist_nulls_node hash_entry_transp_addr;
77 	struct ovpn_socket __rcu *sock;
78 
79 	struct {
80 		struct strparser strp;
81 		struct sk_buff_head user_queue;
82 		struct sk_buff_head out_queue;
83 		bool tx_in_progress;
84 
85 		struct {
86 			struct sk_buff *skb;
87 			int offset;
88 			int len;
89 		} out_msg;
90 
91 		struct {
92 			void (*sk_data_ready)(struct sock *sk);
93 			void (*sk_write_space)(struct sock *sk);
94 			struct proto *prot;
95 			const struct proto_ops *ops;
96 		} sk_cb;
97 
98 		struct work_struct defer_del_work;
99 	} tcp;
100 	struct ovpn_crypto_state crypto;
101 	struct dst_cache dst_cache;
102 	struct ovpn_bind __rcu *bind;
103 	unsigned long keepalive_interval;
104 	unsigned long keepalive_xmit_exp;
105 	time64_t last_sent;
106 	unsigned long keepalive_timeout;
107 	unsigned long keepalive_recv_exp;
108 	time64_t last_recv;
109 	struct ovpn_peer_stats vpn_stats;
110 	struct ovpn_peer_stats link_stats;
111 	enum ovpn_del_peer_reason delete_reason;
112 	spinlock_t lock; /* protects bind  and keepalive* */
113 	struct kref refcount;
114 	struct rcu_head rcu;
115 	struct llist_node release_entry;
116 	struct work_struct keepalive_work;
117 };
118 
119 /**
120  * ovpn_peer_hold - increase reference counter
121  * @peer: the peer whose counter should be increased
122  *
123  * Return: true if the counter was increased or false if it was zero already
124  */
125 static inline bool ovpn_peer_hold(struct ovpn_peer *peer)
126 {
127 	return kref_get_unless_zero(&peer->refcount);
128 }
129 
130 void ovpn_peer_release(struct ovpn_peer *peer);
131 void ovpn_peer_release_kref(struct kref *kref);
132 
133 /**
134  * ovpn_peer_put - decrease reference counter
135  * @peer: the peer whose counter should be decreased
136  */
137 static inline void ovpn_peer_put(struct ovpn_peer *peer)
138 {
139 	kref_put(&peer->refcount, ovpn_peer_release_kref);
140 }
141 
142 struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id);
143 int ovpn_peer_add(struct ovpn_priv *ovpn, struct ovpn_peer *peer);
144 int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason);
145 void ovpn_peers_free(struct ovpn_priv *ovpn, struct sock *sock,
146 		     enum ovpn_del_peer_reason reason);
147 
148 struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn,
149 					       struct sk_buff *skb);
150 struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_priv *ovpn, u32 peer_id);
151 struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn,
152 				       struct sk_buff *skb);
153 void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer);
154 bool ovpn_peer_check_by_src(struct ovpn_priv *ovpn, struct sk_buff *skb,
155 			    struct ovpn_peer *peer);
156 
157 void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout);
158 void ovpn_peer_keepalive_work(struct work_struct *work);
159 
160 void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb);
161 int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer,
162 			     const struct sockaddr_storage *ss,
163 			     const void *local_ip);
164 
165 #endif /* _NET_OVPN_OVPNPEER_H_ */
166