Debian Codegraph @

drivers/net/ovpn-dco/crypto.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_OVPNCRYPTO_H_
11 #define _NET_OVPN_DCO_OVPNCRYPTO_H_
12
13 #include "main.h"
14 #include "pktid.h"
15
16 #include <uapi/linux/ovpn_dco.h>
17 #include <linux/skbuff.h>
18
19 struct ovpn_peer;
20 struct ovpn_crypto_key_slot;
21
22 /* info needed for both encrypt and decrypt directions */
23 struct ovpn_key_direction {
24 const u8 *cipher_key;
25 size_t cipher_key_size;
26 const u8 *nonce_tail; /* only needed for GCM modes */
27 size_t nonce_tail_size; /* only needed for GCM modes */
28 };
29
30 /* all info for a particular symmetric key (primary or secondary) */
31 struct ovpn_key_config {
32 enum ovpn_cipher_alg cipher_alg;
33 u8 key_id;
34 struct ovpn_key_direction encrypt;
35 struct ovpn_key_direction decrypt;
36 };
37
38 /* used to pass settings from netlink to the crypto engine */
39 struct ovpn_peer_key_reset {
40 enum ovpn_key_slot slot;
41 struct ovpn_key_config key;
42 };
43
44 struct ovpn_crypto_key_slot {
45 u8 key_id;
46
47 struct crypto_aead *encrypt;
48 struct crypto_aead *decrypt;
49 struct ovpn_nonce_tail nonce_tail_xmit;
50 struct ovpn_nonce_tail nonce_tail_recv;
51
52 struct ovpn_pktid_recv pid_recv ____cacheline_aligned_in_smp;
53 struct ovpn_pktid_xmit pid_xmit ____cacheline_aligned_in_smp;
54 struct kref refcount;
55 struct rcu_head rcu;
56 };
57
58 struct ovpn_crypto_state {
59 struct ovpn_crypto_key_slot __rcu *primary;
60 struct ovpn_crypto_key_slot __rcu *secondary;
61
62 /* protects primary and secondary slots */
63 struct mutex mutex;
64 };
65
66 static inline bool ovpn_crypto_key_slot_hold(struct ovpn_crypto_key_slot *ks)
67 {
68 return kref_get_unless_zero(&ks->refcount);
69 }
70
71 static inline void ovpn_crypto_state_init(struct ovpn_crypto_state *cs)
72 {
73 RCU_INIT_POINTER(cs->primary, NULL);
74 RCU_INIT_POINTER(cs->secondary, NULL);
75 mutex_init(&cs->mutex);
76 }
77
78 static inline struct ovpn_crypto_key_slot *
79 ovpn_crypto_key_id_to_slot(const struct ovpn_crypto_state *cs, u8 key_id)
80 {
81 struct ovpn_crypto_key_slot *ks;
82
83 if (unlikely(!cs))
84 return NULL;
85
86 rcu_read_lock();
87 ks = rcu_dereference(cs->primary);
88 if (ks && ks->key_id == key_id) {
89 if (unlikely(!ovpn_crypto_key_slot_hold(ks)))
90 ks = NULL;
91 goto out;
92 }
93
94 ks = rcu_dereference(cs->secondary);
95 if (ks && ks->key_id == key_id) {
96 if (unlikely(!ovpn_crypto_key_slot_hold(ks)))
97 ks = NULL;
98 goto out;
99 }
100
101 /* when both key slots are occupied but no matching key ID is found, ks has to be reset to
102 * NULL to avoid carrying a stale pointer
103 */
104 ks = NULL;
105 out:
106 rcu_read_unlock();
107
108 return ks;
109 }
110
111 static inline struct ovpn_crypto_key_slot *
112 ovpn_crypto_key_slot_primary(const struct ovpn_crypto_state *cs)
113 {
114 struct ovpn_crypto_key_slot *ks;
115
116 rcu_read_lock();
117 ks = rcu_dereference(cs->primary);
118 if (unlikely(ks && !ovpn_crypto_key_slot_hold(ks)))
119 ks = NULL;
120 rcu_read_unlock();
121
122 return ks;
123 }
124
125 void ovpn_crypto_key_slot_release(struct kref *kref);
126
127 static inline void ovpn_crypto_key_slot_put(struct ovpn_crypto_key_slot *ks)
128 {
129 kref_put(&ks->refcount, ovpn_crypto_key_slot_release);
130 }
131
132 int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs,
133 const struct ovpn_peer_key_reset *pkr);
134
135 void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs,
136 enum ovpn_key_slot slot);
137
138 void ovpn_crypto_state_release(struct ovpn_crypto_state *cs);
139
140 void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs);
141
142 void ovpn_crypto_kill_primary(struct ovpn_crypto_state *cs);
143
144 #endif /* _NET_OVPN_DCO_OVPNCRYPTO_H_ */