|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 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 | +#include <linux/types.h> |
| 11 | +#include <linux/net.h> |
| 12 | +#include <linux/netdevice.h> |
| 13 | +#include <uapi/linux/ovpn.h> |
| 14 | + |
| 15 | +#include "ovpnpriv.h" |
| 16 | +#include "main.h" |
| 17 | +#include "pktid.h" |
| 18 | +#include "crypto_aead.h" |
| 19 | +#include "crypto.h" |
| 20 | + |
| 21 | +static void ovpn_ks_destroy_rcu(struct rcu_head *head) |
| 22 | +{ |
| 23 | + struct ovpn_crypto_key_slot *ks; |
| 24 | + |
| 25 | + ks = container_of(head, struct ovpn_crypto_key_slot, rcu); |
| 26 | + ovpn_aead_crypto_key_slot_destroy(ks); |
| 27 | +} |
| 28 | + |
| 29 | +void ovpn_crypto_key_slot_release(struct kref *kref) |
| 30 | +{ |
| 31 | + struct ovpn_crypto_key_slot *ks; |
| 32 | + |
| 33 | + ks = container_of(kref, struct ovpn_crypto_key_slot, refcount); |
| 34 | + call_rcu(&ks->rcu, ovpn_ks_destroy_rcu); |
| 35 | +} |
| 36 | + |
| 37 | +/* can only be invoked when all peer references have been dropped (i.e. RCU |
| 38 | + * release routine) |
| 39 | + */ |
| 40 | +void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) |
| 41 | +{ |
| 42 | + struct ovpn_crypto_key_slot *ks; |
| 43 | + |
| 44 | + ks = rcu_access_pointer(cs->slots[0]); |
| 45 | + if (ks) { |
| 46 | + RCU_INIT_POINTER(cs->slots[0], NULL); |
| 47 | + ovpn_crypto_key_slot_put(ks); |
| 48 | + } |
| 49 | + |
| 50 | + ks = rcu_access_pointer(cs->slots[1]); |
| 51 | + if (ks) { |
| 52 | + RCU_INIT_POINTER(cs->slots[1], NULL); |
| 53 | + ovpn_crypto_key_slot_put(ks); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/* Reset the ovpn_crypto_state object in a way that is atomic |
| 58 | + * to RCU readers. |
| 59 | + */ |
| 60 | +int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs, |
| 61 | + const struct ovpn_peer_key_reset *pkr) |
| 62 | +{ |
| 63 | + struct ovpn_crypto_key_slot *old = NULL, *new; |
| 64 | + u8 idx; |
| 65 | + |
| 66 | + if (pkr->slot != OVPN_KEY_SLOT_PRIMARY && |
| 67 | + pkr->slot != OVPN_KEY_SLOT_SECONDARY) |
| 68 | + return -EINVAL; |
| 69 | + |
| 70 | + new = ovpn_aead_crypto_key_slot_new(&pkr->key); |
| 71 | + if (IS_ERR(new)) |
| 72 | + return PTR_ERR(new); |
| 73 | + |
| 74 | + spin_lock_bh(&cs->lock); |
| 75 | + idx = cs->primary_idx; |
| 76 | + switch (pkr->slot) { |
| 77 | + case OVPN_KEY_SLOT_PRIMARY: |
| 78 | + old = rcu_replace_pointer(cs->slots[idx], new, |
| 79 | + lockdep_is_held(&cs->lock)); |
| 80 | + break; |
| 81 | + case OVPN_KEY_SLOT_SECONDARY: |
| 82 | + old = rcu_replace_pointer(cs->slots[!idx], new, |
| 83 | + lockdep_is_held(&cs->lock)); |
| 84 | + break; |
| 85 | + } |
| 86 | + spin_unlock_bh(&cs->lock); |
| 87 | + |
| 88 | + if (old) |
| 89 | + ovpn_crypto_key_slot_put(old); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs, |
| 95 | + enum ovpn_key_slot slot) |
| 96 | +{ |
| 97 | + struct ovpn_crypto_key_slot *ks = NULL; |
| 98 | + u8 idx; |
| 99 | + |
| 100 | + if (slot != OVPN_KEY_SLOT_PRIMARY && |
| 101 | + slot != OVPN_KEY_SLOT_SECONDARY) { |
| 102 | + pr_warn("Invalid slot to release: %u\n", slot); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + spin_lock_bh(&cs->lock); |
| 107 | + idx = cs->primary_idx; |
| 108 | + switch (slot) { |
| 109 | + case OVPN_KEY_SLOT_PRIMARY: |
| 110 | + ks = rcu_replace_pointer(cs->slots[idx], NULL, |
| 111 | + lockdep_is_held(&cs->lock)); |
| 112 | + break; |
| 113 | + case OVPN_KEY_SLOT_SECONDARY: |
| 114 | + ks = rcu_replace_pointer(cs->slots[!idx], NULL, |
| 115 | + lockdep_is_held(&cs->lock)); |
| 116 | + break; |
| 117 | + } |
| 118 | + spin_unlock_bh(&cs->lock); |
| 119 | + |
| 120 | + if (!ks) { |
| 121 | + pr_debug("Key slot already released: %u\n", slot); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + pr_debug("deleting key slot %u, key_id=%u\n", slot, ks->key_id); |
| 126 | + ovpn_crypto_key_slot_put(ks); |
| 127 | +} |
| 128 | + |
| 129 | +void ovpn_crypto_key_slots_swap(struct ovpn_crypto_state *cs) |
| 130 | +{ |
| 131 | + const struct ovpn_crypto_key_slot *old_primary, *old_secondary; |
| 132 | + u8 idx; |
| 133 | + |
| 134 | + spin_lock_bh(&cs->lock); |
| 135 | + idx = cs->primary_idx; |
| 136 | + old_primary = rcu_dereference_protected(cs->slots[idx], |
| 137 | + lockdep_is_held(&cs->lock)); |
| 138 | + old_secondary = rcu_dereference_protected(cs->slots[!idx], |
| 139 | + lockdep_is_held(&cs->lock)); |
| 140 | + /* perform real swap by switching the index of the primary key */ |
| 141 | + WRITE_ONCE(cs->primary_idx, !cs->primary_idx); |
| 142 | + |
| 143 | + pr_debug("key swapped: (old primary) %d <-> (new primary) %d\n", |
| 144 | + old_primary ? old_primary->key_id : -1, |
| 145 | + old_secondary ? old_secondary->key_id : -1); |
| 146 | + |
| 147 | + spin_unlock_bh(&cs->lock); |
| 148 | +} |
0 commit comments