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