| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "main.h" |
| 11 |
#include "crypto_aead.h" |
| 12 |
#include "crypto.h" |
| 13 |
|
| 14 |
#include <uapi/linux/ovpn_dco.h> |
| 15 |
|
| 16 |
static void ovpn_ks_destroy_rcu(struct rcu_head *head) |
| 17 |
{ |
| 18 |
struct ovpn_crypto_key_slot *ks; |
| 19 |
|
| 20 |
ks = container_of(head, struct ovpn_crypto_key_slot, rcu); |
| 21 |
ovpn_aead_crypto_key_slot_destroy(ks); |
| 22 |
} |
| 23 |
|
| 24 |
void ovpn_crypto_key_slot_release(struct kref *kref) |
| 25 |
{ |
| 26 |
struct ovpn_crypto_key_slot *ks; |
| 27 |
|
| 28 |
ks = container_of(kref, struct ovpn_crypto_key_slot, refcount); |
| 29 |
call_rcu(&ks->rcu, ovpn_ks_destroy_rcu); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) |
| 36 |
{ |
| 37 |
struct ovpn_crypto_key_slot *ks; |
| 38 |
|
| 39 |
ks = rcu_access_pointer(cs->primary); |
| 40 |
if (ks) { |
| 41 |
RCU_INIT_POINTER(cs->primary, NULL); |
| 42 |
ovpn_crypto_key_slot_put(ks); |
| 43 |
} |
| 44 |
|
| 45 |
ks = rcu_access_pointer(cs->secondary); |
| 46 |
if (ks) { |
| 47 |
RCU_INIT_POINTER(cs->secondary, NULL); |
| 48 |
ovpn_crypto_key_slot_put(ks); |
| 49 |
} |
| 50 |
|
| 51 |
mutex_destroy(&cs->mutex); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
void ovpn_crypto_kill_primary(struct ovpn_crypto_state *cs) |
| 56 |
{ |
| 57 |
struct ovpn_crypto_key_slot *ks; |
| 58 |
|
| 59 |
mutex_lock(&cs->mutex); |
| 60 |
ks = rcu_replace_pointer(cs->primary, NULL, lockdep_is_held(&cs->mutex)); |
| 61 |
ovpn_crypto_key_slot_put(ks); |
| 62 |
mutex_unlock(&cs->mutex); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs, |
| 69 |
const struct ovpn_peer_key_reset *pkr) |
| 70 |
__must_hold(cs->mutex) |
| 71 |
{ |
| 72 |
struct ovpn_crypto_key_slot *old = NULL; |
| 73 |
struct ovpn_crypto_key_slot *new; |
| 74 |
|
| 75 |
lockdep_assert_held(&cs->mutex); |
| 76 |
|
| 77 |
new = ovpn_aead_crypto_key_slot_new(&pkr->key); |
| 78 |
if (IS_ERR(new)) |
| 79 |
return PTR_ERR(new); |
| 80 |
|
| 81 |
switch (pkr->slot) { |
| 82 |
case OVPN_KEY_SLOT_PRIMARY: |
| 83 |
old = rcu_replace_pointer(cs->primary, new, |
| 84 |
lockdep_is_held(&cs->mutex)); |
| 85 |
break; |
| 86 |
case OVPN_KEY_SLOT_SECONDARY: |
| 87 |
old = rcu_replace_pointer(cs->secondary, new, |
| 88 |
lockdep_is_held(&cs->mutex)); |
| 89 |
break; |
| 90 |
default: |
| 91 |
goto free_key; |
| 92 |
} |
| 93 |
|
| 94 |
if (old) |
| 95 |
ovpn_crypto_key_slot_put(old); |
| 96 |
|
| 97 |
return 0; |
| 98 |
free_key: |
| 99 |
ovpn_crypto_key_slot_put(new); |
| 100 |
return -EINVAL; |
| 101 |
} |
| 102 |
|
| 103 |
void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs, |
| 104 |
enum ovpn_key_slot slot) |
| 105 |
{ |
| 106 |
struct ovpn_crypto_key_slot *ks = NULL; |
| 107 |
|
| 108 |
mutex_lock(&cs->mutex); |
| 109 |
switch (slot) { |
| 110 |
case OVPN_KEY_SLOT_PRIMARY: |
| 111 |
ks = rcu_replace_pointer(cs->primary, NULL, |
| 112 |
lockdep_is_held(&cs->mutex)); |
| 113 |
break; |
| 114 |
case OVPN_KEY_SLOT_SECONDARY: |
| 115 |
ks = rcu_replace_pointer(cs->secondary, NULL, |
| 116 |
lockdep_is_held(&cs->mutex)); |
| 117 |
break; |
| 118 |
default: |
| 119 |
pr_warn("Invalid slot to release: %u\n", slot); |
| 120 |
break; |
| 121 |
} |
| 122 |
mutex_unlock(&cs->mutex); |
| 123 |
|
| 124 |
if (!ks) { |
| 125 |
pr_debug("Key slot already released: %u\n", slot); |
| 126 |
return; |
| 127 |
} |
| 128 |
pr_debug("deleting key slot %u, key_id=%u\n", slot, ks->key_id); |
| 129 |
|
| 130 |
ovpn_crypto_key_slot_put(ks); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs) |
| 138 |
{ |
| 139 |
const struct ovpn_crypto_key_slot *old_primary, *old_secondary; |
| 140 |
|
| 141 |
mutex_lock(&cs->mutex); |
| 142 |
|
| 143 |
old_secondary = rcu_dereference_protected(cs->secondary, |
| 144 |
lockdep_is_held(&cs->mutex)); |
| 145 |
old_primary = rcu_replace_pointer(cs->primary, old_secondary, |
| 146 |
lockdep_is_held(&cs->mutex)); |
| 147 |
rcu_assign_pointer(cs->secondary, old_primary); |
| 148 |
|
| 149 |
pr_debug("key swapped: %u <-> %u\n", |
| 150 |
old_primary ? old_primary->key_id : 0, |
| 151 |
old_secondary ? old_secondary->key_id : 0); |
| 152 |
|
| 153 |
mutex_unlock(&cs->mutex); |
| 154 |
} |