Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 2 | /* |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 3 | * net/tipc/crypto.c: TIPC crypto for key handling & packet en/decryption |
| 4 | * |
| 5 | * Copyright (c) 2019, Ericsson AB |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include <crypto/aead.h> |
| 38 | #include <crypto/aes.h> |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 39 | #include <crypto/rng.h> |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 40 | #include "crypto.h" |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 41 | #include "msg.h" |
| 42 | #include "bcast.h" |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 43 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 44 | #define TIPC_TX_GRACE_PERIOD msecs_to_jiffies(5000) /* 5s */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 45 | #define TIPC_TX_LASTING_TIME msecs_to_jiffies(10000) /* 10s */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 46 | #define TIPC_RX_ACTIVE_LIM msecs_to_jiffies(3000) /* 3s */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 47 | #define TIPC_RX_PASSIVE_LIM msecs_to_jiffies(15000) /* 15s */ |
| 48 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 49 | #define TIPC_MAX_TFMS_DEF 10 |
| 50 | #define TIPC_MAX_TFMS_LIM 1000 |
| 51 | |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 52 | #define TIPC_REKEYING_INTV_DEF (60 * 24) /* default: 1 day */ |
| 53 | |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 54 | /* |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 55 | * TIPC Key ids |
| 56 | */ |
| 57 | enum { |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 58 | KEY_MASTER = 0, |
| 59 | KEY_MIN = KEY_MASTER, |
| 60 | KEY_1 = 1, |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 61 | KEY_2, |
| 62 | KEY_3, |
| 63 | KEY_MAX = KEY_3, |
| 64 | }; |
| 65 | |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 66 | /* |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 67 | * TIPC Crypto statistics |
| 68 | */ |
| 69 | enum { |
| 70 | STAT_OK, |
| 71 | STAT_NOK, |
| 72 | STAT_ASYNC, |
| 73 | STAT_ASYNC_OK, |
| 74 | STAT_ASYNC_NOK, |
| 75 | STAT_BADKEYS, /* tx only */ |
| 76 | STAT_BADMSGS = STAT_BADKEYS, /* rx only */ |
| 77 | STAT_NOKEYS, |
| 78 | STAT_SWITCHES, |
| 79 | |
| 80 | MAX_STATS, |
| 81 | }; |
| 82 | |
| 83 | /* TIPC crypto statistics' header */ |
| 84 | static const char *hstats[MAX_STATS] = {"ok", "nok", "async", "async_ok", |
| 85 | "async_nok", "badmsgs", "nokeys", |
| 86 | "switches"}; |
| 87 | |
| 88 | /* Max TFMs number per key */ |
| 89 | int sysctl_tipc_max_tfms __read_mostly = TIPC_MAX_TFMS_DEF; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 90 | /* Key exchange switch, default: on */ |
| 91 | int sysctl_tipc_key_exchange_enabled __read_mostly = 1; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 92 | |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 93 | /* |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 94 | * struct tipc_key - TIPC keys' status indicator |
| 95 | * |
| 96 | * 7 6 5 4 3 2 1 0 |
| 97 | * +-----+-----+-----+-----+-----+-----+-----+-----+ |
| 98 | * key: | (reserved)|passive idx| active idx|pending idx| |
| 99 | * +-----+-----+-----+-----+-----+-----+-----+-----+ |
| 100 | */ |
| 101 | struct tipc_key { |
| 102 | #define KEY_BITS (2) |
| 103 | #define KEY_MASK ((1 << KEY_BITS) - 1) |
| 104 | union { |
| 105 | struct { |
| 106 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 107 | u8 pending:2, |
| 108 | active:2, |
| 109 | passive:2, /* rx only */ |
| 110 | reserved:2; |
| 111 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 112 | u8 reserved:2, |
| 113 | passive:2, /* rx only */ |
| 114 | active:2, |
| 115 | pending:2; |
| 116 | #else |
| 117 | #error "Please fix <asm/byteorder.h>" |
| 118 | #endif |
| 119 | } __packed; |
| 120 | u8 keys; |
| 121 | }; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * struct tipc_tfm - TIPC TFM structure to form a list of TFMs |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 126 | * @tfm: cipher handle/key |
| 127 | * @list: linked list of TFMs |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 128 | */ |
| 129 | struct tipc_tfm { |
| 130 | struct crypto_aead *tfm; |
| 131 | struct list_head list; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * struct tipc_aead - TIPC AEAD key structure |
| 136 | * @tfm_entry: per-cpu pointer to one entry in TFM list |
| 137 | * @crypto: TIPC crypto owns this key |
| 138 | * @cloned: reference to the source key in case cloning |
| 139 | * @users: the number of the key users (TX/RX) |
| 140 | * @salt: the key's SALT value |
| 141 | * @authsize: authentication tag size (max = 16) |
| 142 | * @mode: crypto mode is applied to the key |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 143 | * @hint: a hint for user key |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 144 | * @rcu: struct rcu_head |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 145 | * @key: the aead key |
| 146 | * @gen: the key's generation |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 147 | * @seqno: the key seqno (cluster scope) |
| 148 | * @refcnt: the key reference counter |
| 149 | */ |
| 150 | struct tipc_aead { |
| 151 | #define TIPC_AEAD_HINT_LEN (5) |
| 152 | struct tipc_tfm * __percpu *tfm_entry; |
| 153 | struct tipc_crypto *crypto; |
| 154 | struct tipc_aead *cloned; |
| 155 | atomic_t users; |
| 156 | u32 salt; |
| 157 | u8 authsize; |
| 158 | u8 mode; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 159 | char hint[2 * TIPC_AEAD_HINT_LEN + 1]; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 160 | struct rcu_head rcu; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 161 | struct tipc_aead_key *key; |
| 162 | u16 gen; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 163 | |
| 164 | atomic64_t seqno ____cacheline_aligned; |
| 165 | refcount_t refcnt ____cacheline_aligned; |
| 166 | |
| 167 | } ____cacheline_aligned; |
| 168 | |
| 169 | /** |
| 170 | * struct tipc_crypto_stats - TIPC Crypto statistics |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 171 | * @stat: array of crypto statistics |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 172 | */ |
| 173 | struct tipc_crypto_stats { |
| 174 | unsigned int stat[MAX_STATS]; |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * struct tipc_crypto - TIPC TX/RX crypto structure |
| 179 | * @net: struct net |
| 180 | * @node: TIPC node (RX) |
| 181 | * @aead: array of pointers to AEAD keys for encryption/decryption |
| 182 | * @peer_rx_active: replicated peer RX active key index |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 183 | * @key_gen: TX/RX key generation |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 184 | * @key: the key states |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 185 | * @skey_mode: session key's mode |
| 186 | * @skey: received session key |
| 187 | * @wq: common workqueue on TX crypto |
| 188 | * @work: delayed work sched for TX/RX |
| 189 | * @key_distr: key distributing state |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 190 | * @rekeying_intv: rekeying interval (in minutes) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 191 | * @stats: the crypto statistics |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 192 | * @name: the crypto name |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 193 | * @sndnxt: the per-peer sndnxt (TX) |
| 194 | * @timer1: general timer 1 (jiffies) |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 195 | * @timer2: general timer 2 (jiffies) |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 196 | * @working: the crypto is working or not |
| 197 | * @key_master: flag indicates if master key exists |
| 198 | * @legacy_user: flag indicates if a peer joins w/o master key (for bwd comp.) |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 199 | * @nokey: no key indication |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 200 | * @flags: combined flags field |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 201 | * @lock: tipc_key lock |
| 202 | */ |
| 203 | struct tipc_crypto { |
| 204 | struct net *net; |
| 205 | struct tipc_node *node; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 206 | struct tipc_aead __rcu *aead[KEY_MAX + 1]; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 207 | atomic_t peer_rx_active; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 208 | u16 key_gen; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 209 | struct tipc_key key; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 210 | u8 skey_mode; |
| 211 | struct tipc_aead_key *skey; |
| 212 | struct workqueue_struct *wq; |
| 213 | struct delayed_work work; |
| 214 | #define KEY_DISTR_SCHED 1 |
| 215 | #define KEY_DISTR_COMPL 2 |
| 216 | atomic_t key_distr; |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 217 | u32 rekeying_intv; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 218 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 219 | struct tipc_crypto_stats __percpu *stats; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 220 | char name[48]; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 221 | |
| 222 | atomic64_t sndnxt ____cacheline_aligned; |
| 223 | unsigned long timer1; |
| 224 | unsigned long timer2; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 225 | union { |
| 226 | struct { |
| 227 | u8 working:1; |
| 228 | u8 key_master:1; |
| 229 | u8 legacy_user:1; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 230 | u8 nokey: 1; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 231 | }; |
| 232 | u8 flags; |
| 233 | }; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 234 | spinlock_t lock; /* crypto lock */ |
| 235 | |
| 236 | } ____cacheline_aligned; |
| 237 | |
| 238 | /* struct tipc_crypto_tx_ctx - TX context for callbacks */ |
| 239 | struct tipc_crypto_tx_ctx { |
| 240 | struct tipc_aead *aead; |
| 241 | struct tipc_bearer *bearer; |
| 242 | struct tipc_media_addr dst; |
| 243 | }; |
| 244 | |
| 245 | /* struct tipc_crypto_rx_ctx - RX context for callbacks */ |
| 246 | struct tipc_crypto_rx_ctx { |
| 247 | struct tipc_aead *aead; |
| 248 | struct tipc_bearer *bearer; |
| 249 | }; |
| 250 | |
| 251 | static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead); |
| 252 | static inline void tipc_aead_put(struct tipc_aead *aead); |
| 253 | static void tipc_aead_free(struct rcu_head *rp); |
| 254 | static int tipc_aead_users(struct tipc_aead __rcu *aead); |
| 255 | static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim); |
| 256 | static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim); |
| 257 | static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val); |
| 258 | static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead); |
| 259 | static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey, |
| 260 | u8 mode); |
| 261 | static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src); |
| 262 | static void *tipc_aead_mem_alloc(struct crypto_aead *tfm, |
| 263 | unsigned int crypto_ctx_size, |
| 264 | u8 **iv, struct aead_request **req, |
| 265 | struct scatterlist **sg, int nsg); |
| 266 | static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb, |
| 267 | struct tipc_bearer *b, |
| 268 | struct tipc_media_addr *dst, |
| 269 | struct tipc_node *__dnode); |
| 270 | static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err); |
| 271 | static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead, |
| 272 | struct sk_buff *skb, struct tipc_bearer *b); |
| 273 | static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err); |
| 274 | static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr); |
| 275 | static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead, |
| 276 | u8 tx_key, struct sk_buff *skb, |
| 277 | struct tipc_crypto *__rx); |
| 278 | static inline void tipc_crypto_key_set_state(struct tipc_crypto *c, |
| 279 | u8 new_passive, |
| 280 | u8 new_active, |
| 281 | u8 new_pending); |
| 282 | static int tipc_crypto_key_attach(struct tipc_crypto *c, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 283 | struct tipc_aead *aead, u8 pos, |
| 284 | bool master_key); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 285 | static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending); |
| 286 | static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx, |
| 287 | struct tipc_crypto *rx, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 288 | struct sk_buff *skb, |
| 289 | u8 tx_key); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 290 | static void tipc_crypto_key_synch(struct tipc_crypto *rx, struct sk_buff *skb); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 291 | static int tipc_crypto_key_revoke(struct net *net, u8 tx_key); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 292 | static inline void tipc_crypto_clone_msg(struct net *net, struct sk_buff *_skb, |
| 293 | struct tipc_bearer *b, |
| 294 | struct tipc_media_addr *dst, |
| 295 | struct tipc_node *__dnode, u8 type); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 296 | static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead, |
| 297 | struct tipc_bearer *b, |
| 298 | struct sk_buff **skb, int err); |
| 299 | static void tipc_crypto_do_cmd(struct net *net, int cmd); |
| 300 | static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 301 | static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new, |
| 302 | char *buf); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 303 | static int tipc_crypto_key_xmit(struct net *net, struct tipc_aead_key *skey, |
| 304 | u16 gen, u8 mode, u32 dnode); |
| 305 | static bool tipc_crypto_key_rcv(struct tipc_crypto *rx, struct tipc_msg *hdr); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 306 | static void tipc_crypto_work_tx(struct work_struct *work); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 307 | static void tipc_crypto_work_rx(struct work_struct *work); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 308 | static int tipc_aead_key_generate(struct tipc_aead_key *skey); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 309 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 310 | #define is_tx(crypto) (!(crypto)->node) |
| 311 | #define is_rx(crypto) (!is_tx(crypto)) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 312 | |
| 313 | #define key_next(cur) ((cur) % KEY_MAX + 1) |
| 314 | |
| 315 | #define tipc_aead_rcu_ptr(rcu_ptr, lock) \ |
| 316 | rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock)) |
| 317 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 318 | #define tipc_aead_rcu_replace(rcu_ptr, ptr, lock) \ |
| 319 | do { \ |
Hoang Huu Le | 97bc84b | 2021-03-11 10:33:23 +0700 | [diff] [blame] | 320 | struct tipc_aead *__tmp = rcu_dereference_protected((rcu_ptr), \ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 321 | lockdep_is_held(lock)); \ |
| 322 | rcu_assign_pointer((rcu_ptr), (ptr)); \ |
| 323 | tipc_aead_put(__tmp); \ |
| 324 | } while (0) |
| 325 | |
| 326 | #define tipc_crypto_key_detach(rcu_ptr, lock) \ |
| 327 | tipc_aead_rcu_replace((rcu_ptr), NULL, lock) |
| 328 | |
| 329 | /** |
| 330 | * tipc_aead_key_validate - Validate a AEAD user key |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 331 | * @ukey: pointer to user key data |
| 332 | * @info: netlink info pointer |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 333 | */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 334 | int tipc_aead_key_validate(struct tipc_aead_key *ukey, struct genl_info *info) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 335 | { |
| 336 | int keylen; |
| 337 | |
| 338 | /* Check if algorithm exists */ |
| 339 | if (unlikely(!crypto_has_alg(ukey->alg_name, 0, 0))) { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 340 | GENL_SET_ERR_MSG(info, "unable to load the algorithm (module existed?)"); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 341 | return -ENODEV; |
| 342 | } |
| 343 | |
| 344 | /* Currently, we only support the "gcm(aes)" cipher algorithm */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 345 | if (strcmp(ukey->alg_name, "gcm(aes)")) { |
| 346 | GENL_SET_ERR_MSG(info, "not supported yet the algorithm"); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 347 | return -ENOTSUPP; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 348 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 349 | |
| 350 | /* Check if key size is correct */ |
| 351 | keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE; |
| 352 | if (unlikely(keylen != TIPC_AES_GCM_KEY_SIZE_128 && |
| 353 | keylen != TIPC_AES_GCM_KEY_SIZE_192 && |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 354 | keylen != TIPC_AES_GCM_KEY_SIZE_256)) { |
| 355 | GENL_SET_ERR_MSG(info, "incorrect key length (20, 28 or 36 octets?)"); |
| 356 | return -EKEYREJECTED; |
| 357 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 362 | /** |
| 363 | * tipc_aead_key_generate - Generate new session key |
| 364 | * @skey: input/output key with new content |
| 365 | * |
| 366 | * Return: 0 in case of success, otherwise < 0 |
| 367 | */ |
| 368 | static int tipc_aead_key_generate(struct tipc_aead_key *skey) |
| 369 | { |
| 370 | int rc = 0; |
| 371 | |
| 372 | /* Fill the key's content with a random value via RNG cipher */ |
| 373 | rc = crypto_get_default_rng(); |
| 374 | if (likely(!rc)) { |
| 375 | rc = crypto_rng_get_bytes(crypto_default_rng, skey->key, |
| 376 | skey->keylen); |
| 377 | crypto_put_default_rng(); |
| 378 | } |
| 379 | |
| 380 | return rc; |
| 381 | } |
| 382 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 383 | static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead) |
| 384 | { |
| 385 | struct tipc_aead *tmp; |
| 386 | |
| 387 | rcu_read_lock(); |
| 388 | tmp = rcu_dereference(aead); |
| 389 | if (unlikely(!tmp || !refcount_inc_not_zero(&tmp->refcnt))) |
| 390 | tmp = NULL; |
| 391 | rcu_read_unlock(); |
| 392 | |
| 393 | return tmp; |
| 394 | } |
| 395 | |
| 396 | static inline void tipc_aead_put(struct tipc_aead *aead) |
| 397 | { |
| 398 | if (aead && refcount_dec_and_test(&aead->refcnt)) |
| 399 | call_rcu(&aead->rcu, tipc_aead_free); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * tipc_aead_free - Release AEAD key incl. all the TFMs in the list |
| 404 | * @rp: rcu head pointer |
| 405 | */ |
| 406 | static void tipc_aead_free(struct rcu_head *rp) |
| 407 | { |
| 408 | struct tipc_aead *aead = container_of(rp, struct tipc_aead, rcu); |
| 409 | struct tipc_tfm *tfm_entry, *head, *tmp; |
| 410 | |
| 411 | if (aead->cloned) { |
| 412 | tipc_aead_put(aead->cloned); |
| 413 | } else { |
Tuong Lien | bb8872a | 2020-08-30 02:37:55 +0700 | [diff] [blame] | 414 | head = *get_cpu_ptr(aead->tfm_entry); |
| 415 | put_cpu_ptr(aead->tfm_entry); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 416 | list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) { |
| 417 | crypto_free_aead(tfm_entry->tfm); |
| 418 | list_del(&tfm_entry->list); |
| 419 | kfree(tfm_entry); |
| 420 | } |
| 421 | /* Free the head */ |
| 422 | crypto_free_aead(head->tfm); |
| 423 | list_del(&head->list); |
| 424 | kfree(head); |
| 425 | } |
| 426 | free_percpu(aead->tfm_entry); |
Eric Biggers | 23224e4 | 2020-10-23 16:27:16 -0700 | [diff] [blame] | 427 | kfree_sensitive(aead->key); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 428 | kfree(aead); |
| 429 | } |
| 430 | |
| 431 | static int tipc_aead_users(struct tipc_aead __rcu *aead) |
| 432 | { |
| 433 | struct tipc_aead *tmp; |
| 434 | int users = 0; |
| 435 | |
| 436 | rcu_read_lock(); |
| 437 | tmp = rcu_dereference(aead); |
| 438 | if (tmp) |
| 439 | users = atomic_read(&tmp->users); |
| 440 | rcu_read_unlock(); |
| 441 | |
| 442 | return users; |
| 443 | } |
| 444 | |
| 445 | static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim) |
| 446 | { |
| 447 | struct tipc_aead *tmp; |
| 448 | |
| 449 | rcu_read_lock(); |
| 450 | tmp = rcu_dereference(aead); |
| 451 | if (tmp) |
| 452 | atomic_add_unless(&tmp->users, 1, lim); |
| 453 | rcu_read_unlock(); |
| 454 | } |
| 455 | |
| 456 | static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim) |
| 457 | { |
| 458 | struct tipc_aead *tmp; |
| 459 | |
| 460 | rcu_read_lock(); |
| 461 | tmp = rcu_dereference(aead); |
| 462 | if (tmp) |
| 463 | atomic_add_unless(&rcu_dereference(aead)->users, -1, lim); |
| 464 | rcu_read_unlock(); |
| 465 | } |
| 466 | |
| 467 | static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val) |
| 468 | { |
| 469 | struct tipc_aead *tmp; |
| 470 | int cur; |
| 471 | |
| 472 | rcu_read_lock(); |
| 473 | tmp = rcu_dereference(aead); |
| 474 | if (tmp) { |
| 475 | do { |
| 476 | cur = atomic_read(&tmp->users); |
| 477 | if (cur == val) |
| 478 | break; |
| 479 | } while (atomic_cmpxchg(&tmp->users, cur, val) != cur); |
| 480 | } |
| 481 | rcu_read_unlock(); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * tipc_aead_tfm_next - Move TFM entry to the next one in list and return it |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 486 | * @aead: the AEAD key pointer |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 487 | */ |
| 488 | static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead) |
| 489 | { |
Tuong Lien | bb8872a | 2020-08-30 02:37:55 +0700 | [diff] [blame] | 490 | struct tipc_tfm **tfm_entry; |
| 491 | struct crypto_aead *tfm; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 492 | |
Tuong Lien | bb8872a | 2020-08-30 02:37:55 +0700 | [diff] [blame] | 493 | tfm_entry = get_cpu_ptr(aead->tfm_entry); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 494 | *tfm_entry = list_next_entry(*tfm_entry, list); |
Tuong Lien | bb8872a | 2020-08-30 02:37:55 +0700 | [diff] [blame] | 495 | tfm = (*tfm_entry)->tfm; |
| 496 | put_cpu_ptr(tfm_entry); |
| 497 | |
| 498 | return tfm; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * tipc_aead_init - Initiate TIPC AEAD |
| 503 | * @aead: returned new TIPC AEAD key handle pointer |
| 504 | * @ukey: pointer to user key data |
| 505 | * @mode: the key mode |
| 506 | * |
| 507 | * Allocate a (list of) new cipher transformation (TFM) with the specific user |
| 508 | * key data if valid. The number of the allocated TFMs can be set via the sysfs |
| 509 | * "net/tipc/max_tfms" first. |
| 510 | * Also, all the other AEAD data are also initialized. |
| 511 | * |
| 512 | * Return: 0 if the initiation is successful, otherwise: < 0 |
| 513 | */ |
| 514 | static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey, |
| 515 | u8 mode) |
| 516 | { |
| 517 | struct tipc_tfm *tfm_entry, *head; |
| 518 | struct crypto_aead *tfm; |
| 519 | struct tipc_aead *tmp; |
| 520 | int keylen, err, cpu; |
| 521 | int tfm_cnt = 0; |
| 522 | |
| 523 | if (unlikely(*aead)) |
| 524 | return -EEXIST; |
| 525 | |
| 526 | /* Allocate a new AEAD */ |
Hoang Le | f845fe5 | 2021-12-17 10:00:59 +0700 | [diff] [blame] | 527 | tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 528 | if (unlikely(!tmp)) |
| 529 | return -ENOMEM; |
| 530 | |
| 531 | /* The key consists of two parts: [AES-KEY][SALT] */ |
| 532 | keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE; |
| 533 | |
| 534 | /* Allocate per-cpu TFM entry pointer */ |
| 535 | tmp->tfm_entry = alloc_percpu(struct tipc_tfm *); |
| 536 | if (!tmp->tfm_entry) { |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 537 | kfree_sensitive(tmp); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 538 | return -ENOMEM; |
| 539 | } |
| 540 | |
| 541 | /* Make a list of TFMs with the user key data */ |
| 542 | do { |
| 543 | tfm = crypto_alloc_aead(ukey->alg_name, 0, 0); |
| 544 | if (IS_ERR(tfm)) { |
| 545 | err = PTR_ERR(tfm); |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | if (unlikely(!tfm_cnt && |
| 550 | crypto_aead_ivsize(tfm) != TIPC_AES_GCM_IV_SIZE)) { |
| 551 | crypto_free_aead(tfm); |
| 552 | err = -ENOTSUPP; |
| 553 | break; |
| 554 | } |
| 555 | |
Colin Ian King | c33fdc3 | 2019-11-11 12:33:34 +0000 | [diff] [blame] | 556 | err = crypto_aead_setauthsize(tfm, TIPC_AES_GCM_TAG_SIZE); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 557 | err |= crypto_aead_setkey(tfm, ukey->key, keylen); |
| 558 | if (unlikely(err)) { |
| 559 | crypto_free_aead(tfm); |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | tfm_entry = kmalloc(sizeof(*tfm_entry), GFP_KERNEL); |
| 564 | if (unlikely(!tfm_entry)) { |
| 565 | crypto_free_aead(tfm); |
| 566 | err = -ENOMEM; |
| 567 | break; |
| 568 | } |
| 569 | INIT_LIST_HEAD(&tfm_entry->list); |
| 570 | tfm_entry->tfm = tfm; |
| 571 | |
| 572 | /* First entry? */ |
| 573 | if (!tfm_cnt) { |
| 574 | head = tfm_entry; |
| 575 | for_each_possible_cpu(cpu) { |
| 576 | *per_cpu_ptr(tmp->tfm_entry, cpu) = head; |
| 577 | } |
| 578 | } else { |
| 579 | list_add_tail(&tfm_entry->list, &head->list); |
| 580 | } |
| 581 | |
| 582 | } while (++tfm_cnt < sysctl_tipc_max_tfms); |
| 583 | |
| 584 | /* Not any TFM is allocated? */ |
| 585 | if (!tfm_cnt) { |
| 586 | free_percpu(tmp->tfm_entry); |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 587 | kfree_sensitive(tmp); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 588 | return err; |
| 589 | } |
| 590 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 591 | /* Form a hex string of some last bytes as the key's hint */ |
| 592 | bin2hex(tmp->hint, ukey->key + keylen - TIPC_AEAD_HINT_LEN, |
| 593 | TIPC_AEAD_HINT_LEN); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 594 | |
| 595 | /* Initialize the other data */ |
| 596 | tmp->mode = mode; |
| 597 | tmp->cloned = NULL; |
| 598 | tmp->authsize = TIPC_AES_GCM_TAG_SIZE; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 599 | tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL); |
Tadeusz Struk | 3e6db07 | 2021-11-15 08:01:43 -0800 | [diff] [blame] | 600 | if (!tmp->key) { |
| 601 | tipc_aead_free(&tmp->rcu); |
| 602 | return -ENOMEM; |
| 603 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 604 | memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE); |
| 605 | atomic_set(&tmp->users, 0); |
| 606 | atomic64_set(&tmp->seqno, 0); |
| 607 | refcount_set(&tmp->refcnt, 1); |
| 608 | |
| 609 | *aead = tmp; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * tipc_aead_clone - Clone a TIPC AEAD key |
| 615 | * @dst: dest key for the cloning |
| 616 | * @src: source key to clone from |
| 617 | * |
| 618 | * Make a "copy" of the source AEAD key data to the dest, the TFMs list is |
| 619 | * common for the keys. |
| 620 | * A reference to the source is hold in the "cloned" pointer for the later |
| 621 | * freeing purposes. |
| 622 | * |
| 623 | * Note: this must be done in cluster-key mode only! |
| 624 | * Return: 0 in case of success, otherwise < 0 |
| 625 | */ |
| 626 | static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src) |
| 627 | { |
| 628 | struct tipc_aead *aead; |
| 629 | int cpu; |
| 630 | |
| 631 | if (!src) |
| 632 | return -ENOKEY; |
| 633 | |
| 634 | if (src->mode != CLUSTER_KEY) |
| 635 | return -EINVAL; |
| 636 | |
| 637 | if (unlikely(*dst)) |
| 638 | return -EEXIST; |
| 639 | |
| 640 | aead = kzalloc(sizeof(*aead), GFP_ATOMIC); |
| 641 | if (unlikely(!aead)) |
| 642 | return -ENOMEM; |
| 643 | |
| 644 | aead->tfm_entry = alloc_percpu_gfp(struct tipc_tfm *, GFP_ATOMIC); |
| 645 | if (unlikely(!aead->tfm_entry)) { |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 646 | kfree_sensitive(aead); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 647 | return -ENOMEM; |
| 648 | } |
| 649 | |
| 650 | for_each_possible_cpu(cpu) { |
| 651 | *per_cpu_ptr(aead->tfm_entry, cpu) = |
| 652 | *per_cpu_ptr(src->tfm_entry, cpu); |
| 653 | } |
| 654 | |
| 655 | memcpy(aead->hint, src->hint, sizeof(src->hint)); |
| 656 | aead->mode = src->mode; |
| 657 | aead->salt = src->salt; |
| 658 | aead->authsize = src->authsize; |
| 659 | atomic_set(&aead->users, 0); |
| 660 | atomic64_set(&aead->seqno, 0); |
| 661 | refcount_set(&aead->refcnt, 1); |
| 662 | |
| 663 | WARN_ON(!refcount_inc_not_zero(&src->refcnt)); |
| 664 | aead->cloned = src; |
| 665 | |
| 666 | *dst = aead; |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * tipc_aead_mem_alloc - Allocate memory for AEAD request operations |
| 672 | * @tfm: cipher handle to be registered with the request |
| 673 | * @crypto_ctx_size: size of crypto context for callback |
| 674 | * @iv: returned pointer to IV data |
| 675 | * @req: returned pointer to AEAD request data |
| 676 | * @sg: returned pointer to SG lists |
| 677 | * @nsg: number of SG lists to be allocated |
| 678 | * |
| 679 | * Allocate memory to store the crypto context data, AEAD request, IV and SG |
| 680 | * lists, the memory layout is as follows: |
| 681 | * crypto_ctx || iv || aead_req || sg[] |
| 682 | * |
| 683 | * Return: the pointer to the memory areas in case of success, otherwise NULL |
| 684 | */ |
| 685 | static void *tipc_aead_mem_alloc(struct crypto_aead *tfm, |
| 686 | unsigned int crypto_ctx_size, |
| 687 | u8 **iv, struct aead_request **req, |
| 688 | struct scatterlist **sg, int nsg) |
| 689 | { |
| 690 | unsigned int iv_size, req_size; |
| 691 | unsigned int len; |
| 692 | u8 *mem; |
| 693 | |
| 694 | iv_size = crypto_aead_ivsize(tfm); |
| 695 | req_size = sizeof(**req) + crypto_aead_reqsize(tfm); |
| 696 | |
| 697 | len = crypto_ctx_size; |
| 698 | len += iv_size; |
| 699 | len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1); |
| 700 | len = ALIGN(len, crypto_tfm_ctx_alignment()); |
| 701 | len += req_size; |
| 702 | len = ALIGN(len, __alignof__(struct scatterlist)); |
| 703 | len += nsg * sizeof(**sg); |
| 704 | |
| 705 | mem = kmalloc(len, GFP_ATOMIC); |
| 706 | if (!mem) |
| 707 | return NULL; |
| 708 | |
| 709 | *iv = (u8 *)PTR_ALIGN(mem + crypto_ctx_size, |
| 710 | crypto_aead_alignmask(tfm) + 1); |
| 711 | *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size, |
| 712 | crypto_tfm_ctx_alignment()); |
| 713 | *sg = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size, |
| 714 | __alignof__(struct scatterlist)); |
| 715 | |
| 716 | return (void *)mem; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * tipc_aead_encrypt - Encrypt a message |
| 721 | * @aead: TIPC AEAD key for the message encryption |
| 722 | * @skb: the input/output skb |
| 723 | * @b: TIPC bearer where the message will be delivered after the encryption |
| 724 | * @dst: the destination media address |
| 725 | * @__dnode: TIPC dest node if "known" |
| 726 | * |
| 727 | * Return: |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 728 | * * 0 : if the encryption has completed |
| 729 | * * -EINPROGRESS/-EBUSY : if a callback will be performed |
| 730 | * * < 0 : the encryption has failed |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 731 | */ |
| 732 | static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb, |
| 733 | struct tipc_bearer *b, |
| 734 | struct tipc_media_addr *dst, |
| 735 | struct tipc_node *__dnode) |
| 736 | { |
| 737 | struct crypto_aead *tfm = tipc_aead_tfm_next(aead); |
| 738 | struct tipc_crypto_tx_ctx *tx_ctx; |
| 739 | struct aead_request *req; |
| 740 | struct sk_buff *trailer; |
| 741 | struct scatterlist *sg; |
| 742 | struct tipc_ehdr *ehdr; |
| 743 | int ehsz, len, tailen, nsg, rc; |
| 744 | void *ctx; |
| 745 | u32 salt; |
| 746 | u8 *iv; |
| 747 | |
| 748 | /* Make sure message len at least 4-byte aligned */ |
| 749 | len = ALIGN(skb->len, 4); |
| 750 | tailen = len - skb->len + aead->authsize; |
| 751 | |
| 752 | /* Expand skb tail for authentication tag: |
| 753 | * As for simplicity, we'd have made sure skb having enough tailroom |
| 754 | * for authentication tag @skb allocation. Even when skb is nonlinear |
| 755 | * but there is no frag_list, it should be still fine! |
| 756 | * Otherwise, we must cow it to be a writable buffer with the tailroom. |
| 757 | */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 758 | SKB_LINEAR_ASSERT(skb); |
| 759 | if (tailen > skb_tailroom(skb)) { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 760 | pr_debug("TX(): skb tailroom is not enough: %d, requires: %d\n", |
| 761 | skb_tailroom(skb), tailen); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 762 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 763 | |
| 764 | if (unlikely(!skb_cloned(skb) && tailen <= skb_tailroom(skb))) { |
| 765 | nsg = 1; |
| 766 | trailer = skb; |
| 767 | } else { |
| 768 | /* TODO: We could avoid skb_cow_data() if skb has no frag_list |
| 769 | * e.g. by skb_fill_page_desc() to add another page to the skb |
| 770 | * with the wanted tailen... However, page skbs look not often, |
| 771 | * so take it easy now! |
| 772 | * Cloned skbs e.g. from link_xmit() seems no choice though :( |
| 773 | */ |
| 774 | nsg = skb_cow_data(skb, tailen, &trailer); |
| 775 | if (unlikely(nsg < 0)) { |
| 776 | pr_err("TX: skb_cow_data() returned %d\n", nsg); |
| 777 | return nsg; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | pskb_put(skb, trailer, tailen); |
| 782 | |
| 783 | /* Allocate memory for the AEAD operation */ |
| 784 | ctx = tipc_aead_mem_alloc(tfm, sizeof(*tx_ctx), &iv, &req, &sg, nsg); |
| 785 | if (unlikely(!ctx)) |
| 786 | return -ENOMEM; |
| 787 | TIPC_SKB_CB(skb)->crypto_ctx = ctx; |
| 788 | |
| 789 | /* Map skb to the sg lists */ |
| 790 | sg_init_table(sg, nsg); |
| 791 | rc = skb_to_sgvec(skb, sg, 0, skb->len); |
| 792 | if (unlikely(rc < 0)) { |
| 793 | pr_err("TX: skb_to_sgvec() returned %d, nsg %d!\n", rc, nsg); |
| 794 | goto exit; |
| 795 | } |
| 796 | |
| 797 | /* Prepare IV: [SALT (4 octets)][SEQNO (8 octets)] |
| 798 | * In case we're in cluster-key mode, SALT is varied by xor-ing with |
| 799 | * the source address (or w0 of id), otherwise with the dest address |
| 800 | * if dest is known. |
| 801 | */ |
| 802 | ehdr = (struct tipc_ehdr *)skb->data; |
| 803 | salt = aead->salt; |
| 804 | if (aead->mode == CLUSTER_KEY) |
Hoang Huu Le | 97bc84b | 2021-03-11 10:33:23 +0700 | [diff] [blame] | 805 | salt ^= __be32_to_cpu(ehdr->addr); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 806 | else if (__dnode) |
| 807 | salt ^= tipc_node_get_addr(__dnode); |
| 808 | memcpy(iv, &salt, 4); |
| 809 | memcpy(iv + 4, (u8 *)&ehdr->seqno, 8); |
| 810 | |
| 811 | /* Prepare request */ |
| 812 | ehsz = tipc_ehdr_size(ehdr); |
| 813 | aead_request_set_tfm(req, tfm); |
| 814 | aead_request_set_ad(req, ehsz); |
| 815 | aead_request_set_crypt(req, sg, sg, len - ehsz, iv); |
| 816 | |
| 817 | /* Set callback function & data */ |
| 818 | aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 819 | tipc_aead_encrypt_done, skb); |
| 820 | tx_ctx = (struct tipc_crypto_tx_ctx *)ctx; |
| 821 | tx_ctx->aead = aead; |
| 822 | tx_ctx->bearer = b; |
| 823 | memcpy(&tx_ctx->dst, dst, sizeof(*dst)); |
| 824 | |
| 825 | /* Hold bearer */ |
| 826 | if (unlikely(!tipc_bearer_hold(b))) { |
| 827 | rc = -ENODEV; |
| 828 | goto exit; |
| 829 | } |
| 830 | |
| 831 | /* Now, do encrypt */ |
| 832 | rc = crypto_aead_encrypt(req); |
| 833 | if (rc == -EINPROGRESS || rc == -EBUSY) |
| 834 | return rc; |
| 835 | |
| 836 | tipc_bearer_put(b); |
| 837 | |
| 838 | exit: |
| 839 | kfree(ctx); |
| 840 | TIPC_SKB_CB(skb)->crypto_ctx = NULL; |
| 841 | return rc; |
| 842 | } |
| 843 | |
| 844 | static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err) |
| 845 | { |
| 846 | struct sk_buff *skb = base->data; |
| 847 | struct tipc_crypto_tx_ctx *tx_ctx = TIPC_SKB_CB(skb)->crypto_ctx; |
| 848 | struct tipc_bearer *b = tx_ctx->bearer; |
| 849 | struct tipc_aead *aead = tx_ctx->aead; |
| 850 | struct tipc_crypto *tx = aead->crypto; |
| 851 | struct net *net = tx->net; |
| 852 | |
| 853 | switch (err) { |
| 854 | case 0: |
| 855 | this_cpu_inc(tx->stats->stat[STAT_ASYNC_OK]); |
Xin Long | f6db909 | 2020-08-20 15:34:47 +0800 | [diff] [blame] | 856 | rcu_read_lock(); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 857 | if (likely(test_bit(0, &b->up))) |
| 858 | b->media->send_msg(net, skb, b, &tx_ctx->dst); |
| 859 | else |
| 860 | kfree_skb(skb); |
Xin Long | f6db909 | 2020-08-20 15:34:47 +0800 | [diff] [blame] | 861 | rcu_read_unlock(); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 862 | break; |
| 863 | case -EINPROGRESS: |
| 864 | return; |
| 865 | default: |
| 866 | this_cpu_inc(tx->stats->stat[STAT_ASYNC_NOK]); |
| 867 | kfree_skb(skb); |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | kfree(tx_ctx); |
| 872 | tipc_bearer_put(b); |
| 873 | tipc_aead_put(aead); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * tipc_aead_decrypt - Decrypt an encrypted message |
| 878 | * @net: struct net |
| 879 | * @aead: TIPC AEAD for the message decryption |
| 880 | * @skb: the input/output skb |
| 881 | * @b: TIPC bearer where the message has been received |
| 882 | * |
| 883 | * Return: |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 884 | * * 0 : if the decryption has completed |
| 885 | * * -EINPROGRESS/-EBUSY : if a callback will be performed |
| 886 | * * < 0 : the decryption has failed |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 887 | */ |
| 888 | static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead, |
| 889 | struct sk_buff *skb, struct tipc_bearer *b) |
| 890 | { |
| 891 | struct tipc_crypto_rx_ctx *rx_ctx; |
| 892 | struct aead_request *req; |
| 893 | struct crypto_aead *tfm; |
| 894 | struct sk_buff *unused; |
| 895 | struct scatterlist *sg; |
| 896 | struct tipc_ehdr *ehdr; |
| 897 | int ehsz, nsg, rc; |
| 898 | void *ctx; |
| 899 | u32 salt; |
| 900 | u8 *iv; |
| 901 | |
| 902 | if (unlikely(!aead)) |
| 903 | return -ENOKEY; |
| 904 | |
Xin Long | 3cf4375 | 2021-07-23 18:46:01 -0400 | [diff] [blame] | 905 | nsg = skb_cow_data(skb, 0, &unused); |
| 906 | if (unlikely(nsg < 0)) { |
| 907 | pr_err("RX: skb_cow_data() returned %d\n", nsg); |
| 908 | return nsg; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* Allocate memory for the AEAD operation */ |
| 912 | tfm = tipc_aead_tfm_next(aead); |
| 913 | ctx = tipc_aead_mem_alloc(tfm, sizeof(*rx_ctx), &iv, &req, &sg, nsg); |
| 914 | if (unlikely(!ctx)) |
| 915 | return -ENOMEM; |
| 916 | TIPC_SKB_CB(skb)->crypto_ctx = ctx; |
| 917 | |
| 918 | /* Map skb to the sg lists */ |
| 919 | sg_init_table(sg, nsg); |
| 920 | rc = skb_to_sgvec(skb, sg, 0, skb->len); |
| 921 | if (unlikely(rc < 0)) { |
| 922 | pr_err("RX: skb_to_sgvec() returned %d, nsg %d\n", rc, nsg); |
| 923 | goto exit; |
| 924 | } |
| 925 | |
| 926 | /* Reconstruct IV: */ |
| 927 | ehdr = (struct tipc_ehdr *)skb->data; |
| 928 | salt = aead->salt; |
| 929 | if (aead->mode == CLUSTER_KEY) |
Hoang Huu Le | 97bc84b | 2021-03-11 10:33:23 +0700 | [diff] [blame] | 930 | salt ^= __be32_to_cpu(ehdr->addr); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 931 | else if (ehdr->destined) |
| 932 | salt ^= tipc_own_addr(net); |
| 933 | memcpy(iv, &salt, 4); |
| 934 | memcpy(iv + 4, (u8 *)&ehdr->seqno, 8); |
| 935 | |
| 936 | /* Prepare request */ |
| 937 | ehsz = tipc_ehdr_size(ehdr); |
| 938 | aead_request_set_tfm(req, tfm); |
| 939 | aead_request_set_ad(req, ehsz); |
| 940 | aead_request_set_crypt(req, sg, sg, skb->len - ehsz, iv); |
| 941 | |
| 942 | /* Set callback function & data */ |
| 943 | aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 944 | tipc_aead_decrypt_done, skb); |
| 945 | rx_ctx = (struct tipc_crypto_rx_ctx *)ctx; |
| 946 | rx_ctx->aead = aead; |
| 947 | rx_ctx->bearer = b; |
| 948 | |
| 949 | /* Hold bearer */ |
| 950 | if (unlikely(!tipc_bearer_hold(b))) { |
| 951 | rc = -ENODEV; |
| 952 | goto exit; |
| 953 | } |
| 954 | |
| 955 | /* Now, do decrypt */ |
| 956 | rc = crypto_aead_decrypt(req); |
| 957 | if (rc == -EINPROGRESS || rc == -EBUSY) |
| 958 | return rc; |
| 959 | |
| 960 | tipc_bearer_put(b); |
| 961 | |
| 962 | exit: |
| 963 | kfree(ctx); |
| 964 | TIPC_SKB_CB(skb)->crypto_ctx = NULL; |
| 965 | return rc; |
| 966 | } |
| 967 | |
| 968 | static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err) |
| 969 | { |
| 970 | struct sk_buff *skb = base->data; |
| 971 | struct tipc_crypto_rx_ctx *rx_ctx = TIPC_SKB_CB(skb)->crypto_ctx; |
| 972 | struct tipc_bearer *b = rx_ctx->bearer; |
| 973 | struct tipc_aead *aead = rx_ctx->aead; |
| 974 | struct tipc_crypto_stats __percpu *stats = aead->crypto->stats; |
| 975 | struct net *net = aead->crypto->net; |
| 976 | |
| 977 | switch (err) { |
| 978 | case 0: |
| 979 | this_cpu_inc(stats->stat[STAT_ASYNC_OK]); |
| 980 | break; |
| 981 | case -EINPROGRESS: |
| 982 | return; |
| 983 | default: |
| 984 | this_cpu_inc(stats->stat[STAT_ASYNC_NOK]); |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | kfree(rx_ctx); |
| 989 | tipc_crypto_rcv_complete(net, aead, b, &skb, err); |
| 990 | if (likely(skb)) { |
| 991 | if (likely(test_bit(0, &b->up))) |
| 992 | tipc_rcv(net, skb, b); |
| 993 | else |
| 994 | kfree_skb(skb); |
| 995 | } |
| 996 | |
| 997 | tipc_bearer_put(b); |
| 998 | } |
| 999 | |
| 1000 | static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr) |
| 1001 | { |
| 1002 | return (ehdr->user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE; |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * tipc_ehdr_validate - Validate an encryption message |
| 1007 | * @skb: the message buffer |
| 1008 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1009 | * Return: "true" if this is a valid encryption message, otherwise "false" |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1010 | */ |
| 1011 | bool tipc_ehdr_validate(struct sk_buff *skb) |
| 1012 | { |
| 1013 | struct tipc_ehdr *ehdr; |
| 1014 | int ehsz; |
| 1015 | |
| 1016 | if (unlikely(!pskb_may_pull(skb, EHDR_MIN_SIZE))) |
| 1017 | return false; |
| 1018 | |
| 1019 | ehdr = (struct tipc_ehdr *)skb->data; |
| 1020 | if (unlikely(ehdr->version != TIPC_EVERSION)) |
| 1021 | return false; |
| 1022 | ehsz = tipc_ehdr_size(ehdr); |
| 1023 | if (unlikely(!pskb_may_pull(skb, ehsz))) |
| 1024 | return false; |
| 1025 | if (unlikely(skb->len <= ehsz + TIPC_AES_GCM_TAG_SIZE)) |
| 1026 | return false; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1027 | |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * tipc_ehdr_build - Build TIPC encryption message header |
| 1033 | * @net: struct net |
| 1034 | * @aead: TX AEAD key to be used for the message encryption |
| 1035 | * @tx_key: key id used for the message encryption |
| 1036 | * @skb: input/output message skb |
| 1037 | * @__rx: RX crypto handle if dest is "known" |
| 1038 | * |
| 1039 | * Return: the header size if the building is successful, otherwise < 0 |
| 1040 | */ |
| 1041 | static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead, |
| 1042 | u8 tx_key, struct sk_buff *skb, |
| 1043 | struct tipc_crypto *__rx) |
| 1044 | { |
| 1045 | struct tipc_msg *hdr = buf_msg(skb); |
| 1046 | struct tipc_ehdr *ehdr; |
| 1047 | u32 user = msg_user(hdr); |
| 1048 | u64 seqno; |
| 1049 | int ehsz; |
| 1050 | |
| 1051 | /* Make room for encryption header */ |
| 1052 | ehsz = (user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE; |
| 1053 | WARN_ON(skb_headroom(skb) < ehsz); |
| 1054 | ehdr = (struct tipc_ehdr *)skb_push(skb, ehsz); |
| 1055 | |
| 1056 | /* Obtain a seqno first: |
| 1057 | * Use the key seqno (= cluster wise) if dest is unknown or we're in |
| 1058 | * cluster key mode, otherwise it's better for a per-peer seqno! |
| 1059 | */ |
| 1060 | if (!__rx || aead->mode == CLUSTER_KEY) |
| 1061 | seqno = atomic64_inc_return(&aead->seqno); |
| 1062 | else |
| 1063 | seqno = atomic64_inc_return(&__rx->sndnxt); |
| 1064 | |
| 1065 | /* Revoke the key if seqno is wrapped around */ |
| 1066 | if (unlikely(!seqno)) |
| 1067 | return tipc_crypto_key_revoke(net, tx_key); |
| 1068 | |
| 1069 | /* Word 1-2 */ |
| 1070 | ehdr->seqno = cpu_to_be64(seqno); |
| 1071 | |
| 1072 | /* Words 0, 3- */ |
| 1073 | ehdr->version = TIPC_EVERSION; |
| 1074 | ehdr->user = 0; |
| 1075 | ehdr->keepalive = 0; |
| 1076 | ehdr->tx_key = tx_key; |
| 1077 | ehdr->destined = (__rx) ? 1 : 0; |
| 1078 | ehdr->rx_key_active = (__rx) ? __rx->key.active : 0; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1079 | ehdr->rx_nokey = (__rx) ? __rx->nokey : 0; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1080 | ehdr->master_key = aead->crypto->key_master; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1081 | ehdr->reserved_1 = 0; |
| 1082 | ehdr->reserved_2 = 0; |
| 1083 | |
| 1084 | switch (user) { |
| 1085 | case LINK_CONFIG: |
| 1086 | ehdr->user = LINK_CONFIG; |
| 1087 | memcpy(ehdr->id, tipc_own_id(net), NODE_ID_LEN); |
| 1088 | break; |
| 1089 | default: |
| 1090 | if (user == LINK_PROTOCOL && msg_type(hdr) == STATE_MSG) { |
| 1091 | ehdr->user = LINK_PROTOCOL; |
| 1092 | ehdr->keepalive = msg_is_keepalive(hdr); |
| 1093 | } |
| 1094 | ehdr->addr = hdr->hdr[3]; |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | return ehsz; |
| 1099 | } |
| 1100 | |
| 1101 | static inline void tipc_crypto_key_set_state(struct tipc_crypto *c, |
| 1102 | u8 new_passive, |
| 1103 | u8 new_active, |
| 1104 | u8 new_pending) |
| 1105 | { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1106 | struct tipc_key old = c->key; |
| 1107 | char buf[32]; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1108 | |
| 1109 | c->key.keys = ((new_passive & KEY_MASK) << (KEY_BITS * 2)) | |
| 1110 | ((new_active & KEY_MASK) << (KEY_BITS)) | |
| 1111 | ((new_pending & KEY_MASK)); |
| 1112 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1113 | pr_debug("%s: key changing %s ::%pS\n", c->name, |
| 1114 | tipc_key_change_dump(old, c->key, buf), |
| 1115 | __builtin_return_address(0)); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * tipc_crypto_key_init - Initiate a new user / AEAD key |
| 1120 | * @c: TIPC crypto to which new key is attached |
| 1121 | * @ukey: the user key |
| 1122 | * @mode: the key mode (CLUSTER_KEY or PER_NODE_KEY) |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1123 | * @master_key: specify this is a cluster master key |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1124 | * |
| 1125 | * A new TIPC AEAD key will be allocated and initiated with the specified user |
| 1126 | * key, then attached to the TIPC crypto. |
| 1127 | * |
| 1128 | * Return: new key id in case of success, otherwise: < 0 |
| 1129 | */ |
| 1130 | int tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1131 | u8 mode, bool master_key) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1132 | { |
| 1133 | struct tipc_aead *aead = NULL; |
| 1134 | int rc = 0; |
| 1135 | |
| 1136 | /* Initiate with the new user key */ |
| 1137 | rc = tipc_aead_init(&aead, ukey, mode); |
| 1138 | |
| 1139 | /* Attach it to the crypto */ |
| 1140 | if (likely(!rc)) { |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1141 | rc = tipc_crypto_key_attach(c, aead, 0, master_key); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1142 | if (rc < 0) |
| 1143 | tipc_aead_free(&aead->rcu); |
| 1144 | } |
| 1145 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1146 | return rc; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * tipc_crypto_key_attach - Attach a new AEAD key to TIPC crypto |
| 1151 | * @c: TIPC crypto to which the new AEAD key is attached |
| 1152 | * @aead: the new AEAD key pointer |
| 1153 | * @pos: desired slot in the crypto key array, = 0 if any! |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1154 | * @master_key: specify this is a cluster master key |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1155 | * |
| 1156 | * Return: new key id in case of success, otherwise: -EBUSY |
| 1157 | */ |
| 1158 | static int tipc_crypto_key_attach(struct tipc_crypto *c, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1159 | struct tipc_aead *aead, u8 pos, |
| 1160 | bool master_key) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1161 | { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1162 | struct tipc_key key; |
| 1163 | int rc = -EBUSY; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1164 | u8 new_key; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1165 | |
| 1166 | spin_lock_bh(&c->lock); |
| 1167 | key = c->key; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1168 | if (master_key) { |
| 1169 | new_key = KEY_MASTER; |
| 1170 | goto attach; |
| 1171 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1172 | if (key.active && key.passive) |
| 1173 | goto exit; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1174 | if (key.pending) { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1175 | if (tipc_aead_users(c->aead[key.pending]) > 0) |
| 1176 | goto exit; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1177 | /* if (pos): ok with replacing, will be aligned when needed */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1178 | /* Replace it */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1179 | new_key = key.pending; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1180 | } else { |
| 1181 | if (pos) { |
| 1182 | if (key.active && pos != key_next(key.active)) { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1183 | key.passive = pos; |
| 1184 | new_key = pos; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1185 | goto attach; |
| 1186 | } else if (!key.active && !key.passive) { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1187 | key.pending = pos; |
| 1188 | new_key = pos; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1189 | goto attach; |
| 1190 | } |
| 1191 | } |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1192 | key.pending = key_next(key.active ?: key.passive); |
| 1193 | new_key = key.pending; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | attach: |
| 1197 | aead->crypto = c; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1198 | aead->gen = (is_tx(c)) ? ++c->key_gen : c->key_gen; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1199 | tipc_aead_rcu_replace(c->aead[new_key], aead, &c->lock); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1200 | if (likely(c->key.keys != key.keys)) |
| 1201 | tipc_crypto_key_set_state(c, key.passive, key.active, |
| 1202 | key.pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1203 | c->working = 1; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1204 | c->nokey = 0; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1205 | c->key_master |= master_key; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1206 | rc = new_key; |
| 1207 | |
| 1208 | exit: |
| 1209 | spin_unlock_bh(&c->lock); |
| 1210 | return rc; |
| 1211 | } |
| 1212 | |
| 1213 | void tipc_crypto_key_flush(struct tipc_crypto *c) |
| 1214 | { |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1215 | struct tipc_crypto *tx, *rx; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1216 | int k; |
| 1217 | |
| 1218 | spin_lock_bh(&c->lock); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1219 | if (is_rx(c)) { |
| 1220 | /* Try to cancel pending work */ |
| 1221 | rx = c; |
| 1222 | tx = tipc_net(rx->net)->crypto_tx; |
| 1223 | if (cancel_delayed_work(&rx->work)) { |
| 1224 | kfree(rx->skey); |
| 1225 | rx->skey = NULL; |
| 1226 | atomic_xchg(&rx->key_distr, 0); |
| 1227 | tipc_node_put(rx->node); |
| 1228 | } |
| 1229 | /* RX stopping => decrease TX key users if any */ |
| 1230 | k = atomic_xchg(&rx->peer_rx_active, 0); |
| 1231 | if (k) { |
| 1232 | tipc_aead_users_dec(tx->aead[k], 0); |
| 1233 | /* Mark the point TX key users changed */ |
| 1234 | tx->timer1 = jiffies; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1238 | c->flags = 0; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1239 | tipc_crypto_key_set_state(c, 0, 0, 0); |
| 1240 | for (k = KEY_MIN; k <= KEY_MAX; k++) |
| 1241 | tipc_crypto_key_detach(c->aead[k], &c->lock); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1242 | atomic64_set(&c->sndnxt, 0); |
| 1243 | spin_unlock_bh(&c->lock); |
| 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * tipc_crypto_key_try_align - Align RX keys if possible |
| 1248 | * @rx: RX crypto handle |
| 1249 | * @new_pending: new pending slot if aligned (= TX key from peer) |
| 1250 | * |
| 1251 | * Peer has used an unknown key slot, this only happens when peer has left and |
| 1252 | * rejoned, or we are newcomer. |
| 1253 | * That means, there must be no active key but a pending key at unaligned slot. |
| 1254 | * If so, we try to move the pending key to the new slot. |
| 1255 | * Note: A potential passive key can exist, it will be shifted correspondingly! |
| 1256 | * |
| 1257 | * Return: "true" if key is successfully aligned, otherwise "false" |
| 1258 | */ |
| 1259 | static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending) |
| 1260 | { |
| 1261 | struct tipc_aead *tmp1, *tmp2 = NULL; |
| 1262 | struct tipc_key key; |
| 1263 | bool aligned = false; |
| 1264 | u8 new_passive = 0; |
| 1265 | int x; |
| 1266 | |
| 1267 | spin_lock(&rx->lock); |
| 1268 | key = rx->key; |
| 1269 | if (key.pending == new_pending) { |
| 1270 | aligned = true; |
| 1271 | goto exit; |
| 1272 | } |
| 1273 | if (key.active) |
| 1274 | goto exit; |
| 1275 | if (!key.pending) |
| 1276 | goto exit; |
| 1277 | if (tipc_aead_users(rx->aead[key.pending]) > 0) |
| 1278 | goto exit; |
| 1279 | |
| 1280 | /* Try to "isolate" this pending key first */ |
| 1281 | tmp1 = tipc_aead_rcu_ptr(rx->aead[key.pending], &rx->lock); |
| 1282 | if (!refcount_dec_if_one(&tmp1->refcnt)) |
| 1283 | goto exit; |
| 1284 | rcu_assign_pointer(rx->aead[key.pending], NULL); |
| 1285 | |
| 1286 | /* Move passive key if any */ |
| 1287 | if (key.passive) { |
Paul E. McKenney | 1a271eb | 2019-12-09 19:13:45 -0800 | [diff] [blame] | 1288 | tmp2 = rcu_replace_pointer(rx->aead[key.passive], tmp2, lockdep_is_held(&rx->lock)); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1289 | x = (key.passive - key.pending + new_pending) % KEY_MAX; |
| 1290 | new_passive = (x <= 0) ? x + KEY_MAX : x; |
| 1291 | } |
| 1292 | |
| 1293 | /* Re-allocate the key(s) */ |
| 1294 | tipc_crypto_key_set_state(rx, new_passive, 0, new_pending); |
| 1295 | rcu_assign_pointer(rx->aead[new_pending], tmp1); |
| 1296 | if (new_passive) |
| 1297 | rcu_assign_pointer(rx->aead[new_passive], tmp2); |
| 1298 | refcount_set(&tmp1->refcnt, 1); |
| 1299 | aligned = true; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1300 | pr_info_ratelimited("%s: key[%d] -> key[%d]\n", rx->name, key.pending, |
| 1301 | new_pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1302 | |
| 1303 | exit: |
| 1304 | spin_unlock(&rx->lock); |
| 1305 | return aligned; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * tipc_crypto_key_pick_tx - Pick one TX key for message decryption |
| 1310 | * @tx: TX crypto handle |
| 1311 | * @rx: RX crypto handle (can be NULL) |
| 1312 | * @skb: the message skb which will be decrypted later |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1313 | * @tx_key: peer TX key id |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1314 | * |
| 1315 | * This function looks up the existing TX keys and pick one which is suitable |
| 1316 | * for the message decryption, that must be a cluster key and not used before |
| 1317 | * on the same message (i.e. recursive). |
| 1318 | * |
| 1319 | * Return: the TX AEAD key handle in case of success, otherwise NULL |
| 1320 | */ |
| 1321 | static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx, |
| 1322 | struct tipc_crypto *rx, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1323 | struct sk_buff *skb, |
| 1324 | u8 tx_key) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1325 | { |
| 1326 | struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(skb); |
| 1327 | struct tipc_aead *aead = NULL; |
| 1328 | struct tipc_key key = tx->key; |
| 1329 | u8 k, i = 0; |
| 1330 | |
| 1331 | /* Initialize data if not yet */ |
| 1332 | if (!skb_cb->tx_clone_deferred) { |
| 1333 | skb_cb->tx_clone_deferred = 1; |
| 1334 | memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx)); |
| 1335 | } |
| 1336 | |
| 1337 | skb_cb->tx_clone_ctx.rx = rx; |
| 1338 | if (++skb_cb->tx_clone_ctx.recurs > 2) |
| 1339 | return NULL; |
| 1340 | |
| 1341 | /* Pick one TX key */ |
| 1342 | spin_lock(&tx->lock); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1343 | if (tx_key == KEY_MASTER) { |
| 1344 | aead = tipc_aead_rcu_ptr(tx->aead[KEY_MASTER], &tx->lock); |
| 1345 | goto done; |
| 1346 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1347 | do { |
| 1348 | k = (i == 0) ? key.pending : |
| 1349 | ((i == 1) ? key.active : key.passive); |
| 1350 | if (!k) |
| 1351 | continue; |
| 1352 | aead = tipc_aead_rcu_ptr(tx->aead[k], &tx->lock); |
| 1353 | if (!aead) |
| 1354 | continue; |
| 1355 | if (aead->mode != CLUSTER_KEY || |
| 1356 | aead == skb_cb->tx_clone_ctx.last) { |
| 1357 | aead = NULL; |
| 1358 | continue; |
| 1359 | } |
| 1360 | /* Ok, found one cluster key */ |
| 1361 | skb_cb->tx_clone_ctx.last = aead; |
| 1362 | WARN_ON(skb->next); |
| 1363 | skb->next = skb_clone(skb, GFP_ATOMIC); |
| 1364 | if (unlikely(!skb->next)) |
| 1365 | pr_warn("Failed to clone skb for next round if any\n"); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1366 | break; |
| 1367 | } while (++i < 3); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1368 | |
| 1369 | done: |
| 1370 | if (likely(aead)) |
| 1371 | WARN_ON(!refcount_inc_not_zero(&aead->refcnt)); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1372 | spin_unlock(&tx->lock); |
| 1373 | |
| 1374 | return aead; |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * tipc_crypto_key_synch: Synch own key data according to peer key status |
| 1379 | * @rx: RX crypto handle |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1380 | * @skb: TIPCv2 message buffer (incl. the ehdr from peer) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1381 | * |
| 1382 | * This function updates the peer node related data as the peer RX active key |
| 1383 | * has changed, so the number of TX keys' users on this node are increased and |
| 1384 | * decreased correspondingly. |
| 1385 | * |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1386 | * It also considers if peer has no key, then we need to make own master key |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1387 | * (if any) taking over i.e. starting grace period and also trigger key |
| 1388 | * distributing process. |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1389 | * |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1390 | * The "per-peer" sndnxt is also reset when the peer key has switched. |
| 1391 | */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1392 | static void tipc_crypto_key_synch(struct tipc_crypto *rx, struct sk_buff *skb) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1393 | { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1394 | struct tipc_ehdr *ehdr = (struct tipc_ehdr *)skb_network_header(skb); |
| 1395 | struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx; |
| 1396 | struct tipc_msg *hdr = buf_msg(skb); |
| 1397 | u32 self = tipc_own_addr(rx->net); |
| 1398 | u8 cur, new; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1399 | unsigned long delay; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1400 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1401 | /* Update RX 'key_master' flag according to peer, also mark "legacy" if |
| 1402 | * a peer has no master key. |
| 1403 | */ |
| 1404 | rx->key_master = ehdr->master_key; |
| 1405 | if (!rx->key_master) |
| 1406 | tx->legacy_user = 1; |
| 1407 | |
| 1408 | /* For later cases, apply only if message is destined to this node */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1409 | if (!ehdr->destined || msg_short(hdr) || msg_destnode(hdr) != self) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1410 | return; |
| 1411 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1412 | /* Case 1: Peer has no keys, let's make master key take over */ |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1413 | if (ehdr->rx_nokey) { |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1414 | /* Set or extend grace period */ |
| 1415 | tx->timer2 = jiffies; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1416 | /* Schedule key distributing for the peer if not yet */ |
| 1417 | if (tx->key.keys && |
| 1418 | !atomic_cmpxchg(&rx->key_distr, 0, KEY_DISTR_SCHED)) { |
| 1419 | get_random_bytes(&delay, 2); |
| 1420 | delay %= 5; |
| 1421 | delay = msecs_to_jiffies(500 * ++delay); |
| 1422 | if (queue_delayed_work(tx->wq, &rx->work, delay)) |
| 1423 | tipc_node_get(rx->node); |
| 1424 | } |
| 1425 | } else { |
| 1426 | /* Cancel a pending key distributing if any */ |
| 1427 | atomic_xchg(&rx->key_distr, 0); |
| 1428 | } |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1429 | |
| 1430 | /* Case 2: Peer RX active key has changed, let's update own TX users */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1431 | cur = atomic_read(&rx->peer_rx_active); |
| 1432 | new = ehdr->rx_key_active; |
| 1433 | if (tx->key.keys && |
| 1434 | cur != new && |
| 1435 | atomic_cmpxchg(&rx->peer_rx_active, cur, new) == cur) { |
| 1436 | if (new) |
| 1437 | tipc_aead_users_inc(tx->aead[new], INT_MAX); |
| 1438 | if (cur) |
| 1439 | tipc_aead_users_dec(tx->aead[cur], 0); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1440 | |
| 1441 | atomic64_set(&rx->sndnxt, 0); |
| 1442 | /* Mark the point TX key users changed */ |
| 1443 | tx->timer1 = jiffies; |
| 1444 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1445 | pr_debug("%s: key users changed %d-- %d++, peer %s\n", |
| 1446 | tx->name, cur, new, rx->name); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | static int tipc_crypto_key_revoke(struct net *net, u8 tx_key) |
| 1451 | { |
| 1452 | struct tipc_crypto *tx = tipc_net(net)->crypto_tx; |
| 1453 | struct tipc_key key; |
| 1454 | |
| 1455 | spin_lock(&tx->lock); |
| 1456 | key = tx->key; |
| 1457 | WARN_ON(!key.active || tx_key != key.active); |
| 1458 | |
| 1459 | /* Free the active key */ |
| 1460 | tipc_crypto_key_set_state(tx, key.passive, 0, key.pending); |
| 1461 | tipc_crypto_key_detach(tx->aead[key.active], &tx->lock); |
| 1462 | spin_unlock(&tx->lock); |
| 1463 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1464 | pr_warn("%s: key is revoked\n", tx->name); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1465 | return -EKEYREVOKED; |
| 1466 | } |
| 1467 | |
| 1468 | int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net, |
| 1469 | struct tipc_node *node) |
| 1470 | { |
| 1471 | struct tipc_crypto *c; |
| 1472 | |
| 1473 | if (*crypto) |
| 1474 | return -EEXIST; |
| 1475 | |
| 1476 | /* Allocate crypto */ |
Hoang Le | f845fe5 | 2021-12-17 10:00:59 +0700 | [diff] [blame] | 1477 | c = kzalloc(sizeof(*c), GFP_ATOMIC); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1478 | if (!c) |
| 1479 | return -ENOMEM; |
| 1480 | |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1481 | /* Allocate workqueue on TX */ |
| 1482 | if (!node) { |
| 1483 | c->wq = alloc_ordered_workqueue("tipc_crypto", 0); |
| 1484 | if (!c->wq) { |
| 1485 | kfree(c); |
| 1486 | return -ENOMEM; |
| 1487 | } |
| 1488 | } |
| 1489 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1490 | /* Allocate statistic structure */ |
Hoang Le | f845fe5 | 2021-12-17 10:00:59 +0700 | [diff] [blame] | 1491 | c->stats = alloc_percpu_gfp(struct tipc_crypto_stats, GFP_ATOMIC); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1492 | if (!c->stats) { |
Yang Yingliang | ac1db7a | 2021-03-31 16:36:02 +0800 | [diff] [blame] | 1493 | if (c->wq) |
| 1494 | destroy_workqueue(c->wq); |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1495 | kfree_sensitive(c); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1496 | return -ENOMEM; |
| 1497 | } |
| 1498 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1499 | c->flags = 0; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1500 | c->net = net; |
| 1501 | c->node = node; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1502 | get_random_bytes(&c->key_gen, 2); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1503 | tipc_crypto_key_set_state(c, 0, 0, 0); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1504 | atomic_set(&c->key_distr, 0); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1505 | atomic_set(&c->peer_rx_active, 0); |
| 1506 | atomic64_set(&c->sndnxt, 0); |
| 1507 | c->timer1 = jiffies; |
| 1508 | c->timer2 = jiffies; |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 1509 | c->rekeying_intv = TIPC_REKEYING_INTV_DEF; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1510 | spin_lock_init(&c->lock); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1511 | scnprintf(c->name, 48, "%s(%s)", (is_rx(c)) ? "RX" : "TX", |
| 1512 | (is_rx(c)) ? tipc_node_get_id_str(c->node) : |
| 1513 | tipc_own_id_string(c->net)); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1514 | |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1515 | if (is_rx(c)) |
| 1516 | INIT_DELAYED_WORK(&c->work, tipc_crypto_work_rx); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 1517 | else |
| 1518 | INIT_DELAYED_WORK(&c->work, tipc_crypto_work_tx); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1519 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1520 | *crypto = c; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | void tipc_crypto_stop(struct tipc_crypto **crypto) |
| 1525 | { |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1526 | struct tipc_crypto *c = *crypto; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1527 | u8 k; |
| 1528 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1529 | if (!c) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1530 | return; |
| 1531 | |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1532 | /* Flush any queued works & destroy wq */ |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 1533 | if (is_tx(c)) { |
| 1534 | c->rekeying_intv = 0; |
| 1535 | cancel_delayed_work_sync(&c->work); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1536 | destroy_workqueue(c->wq); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 1537 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1538 | |
| 1539 | /* Release AEAD keys */ |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1540 | rcu_read_lock(); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1541 | for (k = KEY_MIN; k <= KEY_MAX; k++) |
| 1542 | tipc_aead_put(rcu_dereference(c->aead[k])); |
| 1543 | rcu_read_unlock(); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1544 | pr_debug("%s: has been stopped\n", c->name); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1545 | |
| 1546 | /* Free this crypto statistics */ |
| 1547 | free_percpu(c->stats); |
| 1548 | |
| 1549 | *crypto = NULL; |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1550 | kfree_sensitive(c); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | void tipc_crypto_timeout(struct tipc_crypto *rx) |
| 1554 | { |
| 1555 | struct tipc_net *tn = tipc_net(rx->net); |
| 1556 | struct tipc_crypto *tx = tn->crypto_tx; |
| 1557 | struct tipc_key key; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1558 | int cmd; |
| 1559 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1560 | /* TX pending: taking all users & stable -> active */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1561 | spin_lock(&tx->lock); |
| 1562 | key = tx->key; |
| 1563 | if (key.active && tipc_aead_users(tx->aead[key.active]) > 0) |
| 1564 | goto s1; |
| 1565 | if (!key.pending || tipc_aead_users(tx->aead[key.pending]) <= 0) |
| 1566 | goto s1; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1567 | if (time_before(jiffies, tx->timer1 + TIPC_TX_LASTING_TIME)) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1568 | goto s1; |
| 1569 | |
| 1570 | tipc_crypto_key_set_state(tx, key.passive, key.pending, 0); |
| 1571 | if (key.active) |
| 1572 | tipc_crypto_key_detach(tx->aead[key.active], &tx->lock); |
| 1573 | this_cpu_inc(tx->stats->stat[STAT_SWITCHES]); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1574 | pr_info("%s: key[%d] is activated\n", tx->name, key.pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1575 | |
| 1576 | s1: |
| 1577 | spin_unlock(&tx->lock); |
| 1578 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1579 | /* RX pending: having user -> active */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1580 | spin_lock(&rx->lock); |
| 1581 | key = rx->key; |
| 1582 | if (!key.pending || tipc_aead_users(rx->aead[key.pending]) <= 0) |
| 1583 | goto s2; |
| 1584 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1585 | if (key.active) |
| 1586 | key.passive = key.active; |
| 1587 | key.active = key.pending; |
| 1588 | rx->timer2 = jiffies; |
| 1589 | tipc_crypto_key_set_state(rx, key.passive, key.active, 0); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1590 | this_cpu_inc(rx->stats->stat[STAT_SWITCHES]); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1591 | pr_info("%s: key[%d] is activated\n", rx->name, key.pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1592 | goto s5; |
| 1593 | |
| 1594 | s2: |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1595 | /* RX pending: not working -> remove */ |
| 1596 | if (!key.pending || tipc_aead_users(rx->aead[key.pending]) > -10) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1597 | goto s3; |
| 1598 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1599 | tipc_crypto_key_set_state(rx, key.passive, key.active, 0); |
| 1600 | tipc_crypto_key_detach(rx->aead[key.pending], &rx->lock); |
| 1601 | pr_debug("%s: key[%d] is removed\n", rx->name, key.pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1602 | goto s5; |
| 1603 | |
| 1604 | s3: |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1605 | /* RX active: timed out or no user -> pending */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1606 | if (!key.active) |
| 1607 | goto s4; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1608 | if (time_before(jiffies, rx->timer1 + TIPC_RX_ACTIVE_LIM) && |
| 1609 | tipc_aead_users(rx->aead[key.active]) > 0) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1610 | goto s4; |
| 1611 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1612 | if (key.pending) |
| 1613 | key.passive = key.active; |
| 1614 | else |
| 1615 | key.pending = key.active; |
| 1616 | rx->timer2 = jiffies; |
| 1617 | tipc_crypto_key_set_state(rx, key.passive, 0, key.pending); |
| 1618 | tipc_aead_users_set(rx->aead[key.pending], 0); |
| 1619 | pr_debug("%s: key[%d] is deactivated\n", rx->name, key.active); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1620 | goto s5; |
| 1621 | |
| 1622 | s4: |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1623 | /* RX passive: outdated or not working -> free */ |
| 1624 | if (!key.passive) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1625 | goto s5; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1626 | if (time_before(jiffies, rx->timer2 + TIPC_RX_PASSIVE_LIM) && |
| 1627 | tipc_aead_users(rx->aead[key.passive]) > -10) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1628 | goto s5; |
| 1629 | |
| 1630 | tipc_crypto_key_set_state(rx, 0, key.active, key.pending); |
| 1631 | tipc_crypto_key_detach(rx->aead[key.passive], &rx->lock); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1632 | pr_debug("%s: key[%d] is freed\n", rx->name, key.passive); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1633 | |
| 1634 | s5: |
| 1635 | spin_unlock(&rx->lock); |
| 1636 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1637 | /* Relax it here, the flag will be set again if it really is, but only |
| 1638 | * when we are not in grace period for safety! |
| 1639 | */ |
| 1640 | if (time_after(jiffies, tx->timer2 + TIPC_TX_GRACE_PERIOD)) |
| 1641 | tx->legacy_user = 0; |
| 1642 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1643 | /* Limit max_tfms & do debug commands if needed */ |
| 1644 | if (likely(sysctl_tipc_max_tfms <= TIPC_MAX_TFMS_LIM)) |
| 1645 | return; |
| 1646 | |
| 1647 | cmd = sysctl_tipc_max_tfms; |
| 1648 | sysctl_tipc_max_tfms = TIPC_MAX_TFMS_DEF; |
| 1649 | tipc_crypto_do_cmd(rx->net, cmd); |
| 1650 | } |
| 1651 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1652 | static inline void tipc_crypto_clone_msg(struct net *net, struct sk_buff *_skb, |
| 1653 | struct tipc_bearer *b, |
| 1654 | struct tipc_media_addr *dst, |
| 1655 | struct tipc_node *__dnode, u8 type) |
| 1656 | { |
| 1657 | struct sk_buff *skb; |
| 1658 | |
| 1659 | skb = skb_clone(_skb, GFP_ATOMIC); |
| 1660 | if (skb) { |
| 1661 | TIPC_SKB_CB(skb)->xmit_type = type; |
| 1662 | tipc_crypto_xmit(net, &skb, b, dst, __dnode); |
| 1663 | if (skb) |
| 1664 | b->media->send_msg(net, skb, b, dst); |
| 1665 | } |
| 1666 | } |
| 1667 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1668 | /** |
| 1669 | * tipc_crypto_xmit - Build & encrypt TIPC message for xmit |
| 1670 | * @net: struct net |
| 1671 | * @skb: input/output message skb pointer |
| 1672 | * @b: bearer used for xmit later |
| 1673 | * @dst: destination media address |
| 1674 | * @__dnode: destination node for reference if any |
| 1675 | * |
| 1676 | * First, build an encryption message header on the top of the message, then |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1677 | * encrypt the original TIPC message by using the pending, master or active |
| 1678 | * key with this preference order. |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1679 | * If the encryption is successful, the encrypted skb is returned directly or |
| 1680 | * via the callback. |
| 1681 | * Otherwise, the skb is freed! |
| 1682 | * |
| 1683 | * Return: |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1684 | * * 0 : the encryption has succeeded (or no encryption) |
| 1685 | * * -EINPROGRESS/-EBUSY : the encryption is ongoing, a callback will be made |
| 1686 | * * -ENOKEK : the encryption has failed due to no key |
| 1687 | * * -EKEYREVOKED : the encryption has failed due to key revoked |
| 1688 | * * -ENOMEM : the encryption has failed due to no memory |
| 1689 | * * < 0 : the encryption has failed due to other reasons |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1690 | */ |
| 1691 | int tipc_crypto_xmit(struct net *net, struct sk_buff **skb, |
| 1692 | struct tipc_bearer *b, struct tipc_media_addr *dst, |
| 1693 | struct tipc_node *__dnode) |
| 1694 | { |
| 1695 | struct tipc_crypto *__rx = tipc_node_crypto_rx(__dnode); |
| 1696 | struct tipc_crypto *tx = tipc_net(net)->crypto_tx; |
| 1697 | struct tipc_crypto_stats __percpu *stats = tx->stats; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1698 | struct tipc_msg *hdr = buf_msg(*skb); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1699 | struct tipc_key key = tx->key; |
| 1700 | struct tipc_aead *aead = NULL; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1701 | u32 user = msg_user(hdr); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1702 | u32 type = msg_type(hdr); |
| 1703 | int rc = -ENOKEY; |
| 1704 | u8 tx_key = 0; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1705 | |
| 1706 | /* No encryption? */ |
| 1707 | if (!tx->working) |
| 1708 | return 0; |
| 1709 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1710 | /* Pending key if peer has active on it or probing time */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1711 | if (unlikely(key.pending)) { |
| 1712 | tx_key = key.pending; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1713 | if (!tx->key_master && !key.active) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1714 | goto encrypt; |
| 1715 | if (__rx && atomic_read(&__rx->peer_rx_active) == tx_key) |
| 1716 | goto encrypt; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1717 | if (TIPC_SKB_CB(*skb)->xmit_type == SKB_PROBING) { |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1718 | pr_debug("%s: probing for key[%d]\n", tx->name, |
| 1719 | key.pending); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1720 | goto encrypt; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1721 | } |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1722 | if (user == LINK_CONFIG || user == LINK_PROTOCOL) |
| 1723 | tipc_crypto_clone_msg(net, *skb, b, dst, __dnode, |
| 1724 | SKB_PROBING); |
| 1725 | } |
| 1726 | |
| 1727 | /* Master key if this is a *vital* message or in grace period */ |
| 1728 | if (tx->key_master) { |
| 1729 | tx_key = KEY_MASTER; |
| 1730 | if (!key.active) |
| 1731 | goto encrypt; |
| 1732 | if (TIPC_SKB_CB(*skb)->xmit_type == SKB_GRACING) { |
| 1733 | pr_debug("%s: gracing for msg (%d %d)\n", tx->name, |
| 1734 | user, type); |
| 1735 | goto encrypt; |
| 1736 | } |
| 1737 | if (user == LINK_CONFIG || |
| 1738 | (user == LINK_PROTOCOL && type == RESET_MSG) || |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1739 | (user == MSG_CRYPTO && type == KEY_DISTR_MSG) || |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1740 | time_before(jiffies, tx->timer2 + TIPC_TX_GRACE_PERIOD)) { |
| 1741 | if (__rx && __rx->key_master && |
| 1742 | !atomic_read(&__rx->peer_rx_active)) |
| 1743 | goto encrypt; |
| 1744 | if (!__rx) { |
| 1745 | if (likely(!tx->legacy_user)) |
| 1746 | goto encrypt; |
| 1747 | tipc_crypto_clone_msg(net, *skb, b, dst, |
| 1748 | __dnode, SKB_GRACING); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | } |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1752 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1753 | /* Else, use the active key if any */ |
| 1754 | if (likely(key.active)) { |
| 1755 | tx_key = key.active; |
| 1756 | goto encrypt; |
| 1757 | } |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1758 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1759 | goto exit; |
| 1760 | |
| 1761 | encrypt: |
| 1762 | aead = tipc_aead_get(tx->aead[tx_key]); |
| 1763 | if (unlikely(!aead)) |
| 1764 | goto exit; |
| 1765 | rc = tipc_ehdr_build(net, aead, tx_key, *skb, __rx); |
| 1766 | if (likely(rc > 0)) |
| 1767 | rc = tipc_aead_encrypt(aead, *skb, b, dst, __dnode); |
| 1768 | |
| 1769 | exit: |
| 1770 | switch (rc) { |
| 1771 | case 0: |
| 1772 | this_cpu_inc(stats->stat[STAT_OK]); |
| 1773 | break; |
| 1774 | case -EINPROGRESS: |
| 1775 | case -EBUSY: |
| 1776 | this_cpu_inc(stats->stat[STAT_ASYNC]); |
| 1777 | *skb = NULL; |
| 1778 | return rc; |
| 1779 | default: |
| 1780 | this_cpu_inc(stats->stat[STAT_NOK]); |
| 1781 | if (rc == -ENOKEY) |
| 1782 | this_cpu_inc(stats->stat[STAT_NOKEYS]); |
| 1783 | else if (rc == -EKEYREVOKED) |
| 1784 | this_cpu_inc(stats->stat[STAT_BADKEYS]); |
| 1785 | kfree_skb(*skb); |
| 1786 | *skb = NULL; |
| 1787 | break; |
| 1788 | } |
| 1789 | |
| 1790 | tipc_aead_put(aead); |
| 1791 | return rc; |
| 1792 | } |
| 1793 | |
| 1794 | /** |
| 1795 | * tipc_crypto_rcv - Decrypt an encrypted TIPC message from peer |
| 1796 | * @net: struct net |
| 1797 | * @rx: RX crypto handle |
| 1798 | * @skb: input/output message skb pointer |
| 1799 | * @b: bearer where the message has been received |
| 1800 | * |
| 1801 | * If the decryption is successful, the decrypted skb is returned directly or |
| 1802 | * as the callback, the encryption header and auth tag will be trimed out |
| 1803 | * before forwarding to tipc_rcv() via the tipc_crypto_rcv_complete(). |
| 1804 | * Otherwise, the skb will be freed! |
| 1805 | * Note: RX key(s) can be re-aligned, or in case of no key suitable, TX |
| 1806 | * cluster key(s) can be taken for decryption (- recursive). |
| 1807 | * |
| 1808 | * Return: |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1809 | * * 0 : the decryption has successfully completed |
| 1810 | * * -EINPROGRESS/-EBUSY : the decryption is ongoing, a callback will be made |
| 1811 | * * -ENOKEY : the decryption has failed due to no key |
| 1812 | * * -EBADMSG : the decryption has failed due to bad message |
| 1813 | * * -ENOMEM : the decryption has failed due to no memory |
| 1814 | * * < 0 : the decryption has failed due to other reasons |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1815 | */ |
| 1816 | int tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx, |
| 1817 | struct sk_buff **skb, struct tipc_bearer *b) |
| 1818 | { |
| 1819 | struct tipc_crypto *tx = tipc_net(net)->crypto_tx; |
| 1820 | struct tipc_crypto_stats __percpu *stats; |
| 1821 | struct tipc_aead *aead = NULL; |
| 1822 | struct tipc_key key; |
| 1823 | int rc = -ENOKEY; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1824 | u8 tx_key, n; |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1825 | |
| 1826 | tx_key = ((struct tipc_ehdr *)(*skb)->data)->tx_key; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1827 | |
| 1828 | /* New peer? |
| 1829 | * Let's try with TX key (i.e. cluster mode) & verify the skb first! |
| 1830 | */ |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1831 | if (unlikely(!rx || tx_key == KEY_MASTER)) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1832 | goto pick_tx; |
| 1833 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1834 | /* Pick RX key according to TX key if any */ |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1835 | key = rx->key; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1836 | if (tx_key == key.active || tx_key == key.pending || |
| 1837 | tx_key == key.passive) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1838 | goto decrypt; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1839 | |
| 1840 | /* Unknown key, let's try to align RX key(s) */ |
| 1841 | if (tipc_crypto_key_try_align(rx, tx_key)) |
| 1842 | goto decrypt; |
| 1843 | |
| 1844 | pick_tx: |
| 1845 | /* No key suitable? Try to pick one from TX... */ |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1846 | aead = tipc_crypto_key_pick_tx(tx, rx, *skb, tx_key); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1847 | if (aead) |
| 1848 | goto decrypt; |
| 1849 | goto exit; |
| 1850 | |
| 1851 | decrypt: |
| 1852 | rcu_read_lock(); |
| 1853 | if (!aead) |
| 1854 | aead = tipc_aead_get(rx->aead[tx_key]); |
| 1855 | rc = tipc_aead_decrypt(net, aead, *skb, b); |
| 1856 | rcu_read_unlock(); |
| 1857 | |
| 1858 | exit: |
| 1859 | stats = ((rx) ?: tx)->stats; |
| 1860 | switch (rc) { |
| 1861 | case 0: |
| 1862 | this_cpu_inc(stats->stat[STAT_OK]); |
| 1863 | break; |
| 1864 | case -EINPROGRESS: |
| 1865 | case -EBUSY: |
| 1866 | this_cpu_inc(stats->stat[STAT_ASYNC]); |
| 1867 | *skb = NULL; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1868 | return rc; |
| 1869 | default: |
| 1870 | this_cpu_inc(stats->stat[STAT_NOK]); |
| 1871 | if (rc == -ENOKEY) { |
| 1872 | kfree_skb(*skb); |
| 1873 | *skb = NULL; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1874 | if (rx) { |
| 1875 | /* Mark rx->nokey only if we dont have a |
| 1876 | * pending received session key, nor a newer |
| 1877 | * one i.e. in the next slot. |
| 1878 | */ |
| 1879 | n = key_next(tx_key); |
| 1880 | rx->nokey = !(rx->skey || |
| 1881 | rcu_access_pointer(rx->aead[n])); |
| 1882 | pr_debug_ratelimited("%s: nokey %d, key %d/%x\n", |
| 1883 | rx->name, rx->nokey, |
| 1884 | tx_key, rx->key.keys); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1885 | tipc_node_put(rx->node); |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 1886 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1887 | this_cpu_inc(stats->stat[STAT_NOKEYS]); |
| 1888 | return rc; |
| 1889 | } else if (rc == -EBADMSG) { |
| 1890 | this_cpu_inc(stats->stat[STAT_BADMSGS]); |
| 1891 | } |
| 1892 | break; |
| 1893 | } |
| 1894 | |
| 1895 | tipc_crypto_rcv_complete(net, aead, b, skb, rc); |
| 1896 | return rc; |
| 1897 | } |
| 1898 | |
| 1899 | static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead, |
| 1900 | struct tipc_bearer *b, |
| 1901 | struct sk_buff **skb, int err) |
| 1902 | { |
| 1903 | struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(*skb); |
| 1904 | struct tipc_crypto *rx = aead->crypto; |
| 1905 | struct tipc_aead *tmp = NULL; |
| 1906 | struct tipc_ehdr *ehdr; |
| 1907 | struct tipc_node *n; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1908 | |
| 1909 | /* Is this completed by TX? */ |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1910 | if (unlikely(is_tx(aead->crypto))) { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1911 | rx = skb_cb->tx_clone_ctx.rx; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1912 | pr_debug("TX->RX(%s): err %d, aead %p, skb->next %p, flags %x\n", |
| 1913 | (rx) ? tipc_node_get_id_str(rx->node) : "-", err, aead, |
| 1914 | (*skb)->next, skb_cb->flags); |
| 1915 | pr_debug("skb_cb [recurs %d, last %p], tx->aead [%p %p %p]\n", |
| 1916 | skb_cb->tx_clone_ctx.recurs, skb_cb->tx_clone_ctx.last, |
| 1917 | aead->crypto->aead[1], aead->crypto->aead[2], |
| 1918 | aead->crypto->aead[3]); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1919 | if (unlikely(err)) { |
| 1920 | if (err == -EBADMSG && (*skb)->next) |
| 1921 | tipc_rcv(net, (*skb)->next, b); |
| 1922 | goto free_skb; |
| 1923 | } |
| 1924 | |
| 1925 | if (likely((*skb)->next)) { |
| 1926 | kfree_skb((*skb)->next); |
| 1927 | (*skb)->next = NULL; |
| 1928 | } |
| 1929 | ehdr = (struct tipc_ehdr *)(*skb)->data; |
| 1930 | if (!rx) { |
| 1931 | WARN_ON(ehdr->user != LINK_CONFIG); |
| 1932 | n = tipc_node_create(net, 0, ehdr->id, 0xffffu, 0, |
| 1933 | true); |
| 1934 | rx = tipc_node_crypto_rx(n); |
| 1935 | if (unlikely(!rx)) |
| 1936 | goto free_skb; |
| 1937 | } |
| 1938 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1939 | /* Ignore cloning if it was TX master key */ |
| 1940 | if (ehdr->tx_key == KEY_MASTER) |
| 1941 | goto rcv; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1942 | if (tipc_aead_clone(&tmp, aead) < 0) |
| 1943 | goto rcv; |
Xin Long | 2a2403c | 2021-04-06 10:45:23 +0800 | [diff] [blame] | 1944 | WARN_ON(!refcount_inc_not_zero(&tmp->refcnt)); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1945 | if (tipc_crypto_key_attach(rx, tmp, ehdr->tx_key, false) < 0) { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1946 | tipc_aead_free(&tmp->rcu); |
| 1947 | goto rcv; |
| 1948 | } |
| 1949 | tipc_aead_put(aead); |
Xin Long | 2a2403c | 2021-04-06 10:45:23 +0800 | [diff] [blame] | 1950 | aead = tmp; |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | if (unlikely(err)) { |
Hoang Huu Le | 97bc84b | 2021-03-11 10:33:23 +0700 | [diff] [blame] | 1954 | tipc_aead_users_dec((struct tipc_aead __force __rcu *)aead, INT_MIN); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1955 | goto free_skb; |
| 1956 | } |
| 1957 | |
| 1958 | /* Set the RX key's user */ |
Hoang Huu Le | 97bc84b | 2021-03-11 10:33:23 +0700 | [diff] [blame] | 1959 | tipc_aead_users_set((struct tipc_aead __force __rcu *)aead, 1); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1960 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1961 | /* Mark this point, RX works */ |
| 1962 | rx->timer1 = jiffies; |
| 1963 | |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 1964 | rcv: |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1965 | /* Remove ehdr & auth. tag prior to tipc_rcv() */ |
| 1966 | ehdr = (struct tipc_ehdr *)(*skb)->data; |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1967 | |
| 1968 | /* Mark this point, RX passive still works */ |
| 1969 | if (rx->key.passive && ehdr->tx_key == rx->key.passive) |
| 1970 | rx->timer2 = jiffies; |
| 1971 | |
| 1972 | skb_reset_network_header(*skb); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1973 | skb_pull(*skb, tipc_ehdr_size(ehdr)); |
| 1974 | pskb_trim(*skb, (*skb)->len - aead->authsize); |
| 1975 | |
| 1976 | /* Validate TIPCv2 message */ |
| 1977 | if (unlikely(!tipc_msg_validate(skb))) { |
| 1978 | pr_err_ratelimited("Packet dropped after decryption!\n"); |
| 1979 | goto free_skb; |
| 1980 | } |
| 1981 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 1982 | /* Ok, everything's fine, try to synch own keys according to peers' */ |
| 1983 | tipc_crypto_key_synch(rx, *skb); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 1984 | |
| 1985 | /* Mark skb decrypted */ |
| 1986 | skb_cb->decrypted = 1; |
| 1987 | |
| 1988 | /* Clear clone cxt if any */ |
| 1989 | if (likely(!skb_cb->tx_clone_deferred)) |
| 1990 | goto exit; |
| 1991 | skb_cb->tx_clone_deferred = 0; |
| 1992 | memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx)); |
| 1993 | goto exit; |
| 1994 | |
| 1995 | free_skb: |
| 1996 | kfree_skb(*skb); |
| 1997 | *skb = NULL; |
| 1998 | |
| 1999 | exit: |
| 2000 | tipc_aead_put(aead); |
| 2001 | if (rx) |
| 2002 | tipc_node_put(rx->node); |
| 2003 | } |
| 2004 | |
| 2005 | static void tipc_crypto_do_cmd(struct net *net, int cmd) |
| 2006 | { |
| 2007 | struct tipc_net *tn = tipc_net(net); |
| 2008 | struct tipc_crypto *tx = tn->crypto_tx, *rx; |
| 2009 | struct list_head *p; |
| 2010 | unsigned int stat; |
| 2011 | int i, j, cpu; |
| 2012 | char buf[200]; |
| 2013 | |
| 2014 | /* Currently only one command is supported */ |
| 2015 | switch (cmd) { |
| 2016 | case 0xfff1: |
| 2017 | goto print_stats; |
| 2018 | default: |
| 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | print_stats: |
| 2023 | /* Print a header */ |
| 2024 | pr_info("\n=============== TIPC Crypto Statistics ===============\n\n"); |
| 2025 | |
| 2026 | /* Print key status */ |
| 2027 | pr_info("Key status:\n"); |
| 2028 | pr_info("TX(%7.7s)\n%s", tipc_own_id_string(net), |
| 2029 | tipc_crypto_key_dump(tx, buf)); |
| 2030 | |
| 2031 | rcu_read_lock(); |
| 2032 | for (p = tn->node_list.next; p != &tn->node_list; p = p->next) { |
| 2033 | rx = tipc_node_crypto_rx_by_list(p); |
| 2034 | pr_info("RX(%7.7s)\n%s", tipc_node_get_id_str(rx->node), |
| 2035 | tipc_crypto_key_dump(rx, buf)); |
| 2036 | } |
| 2037 | rcu_read_unlock(); |
| 2038 | |
| 2039 | /* Print crypto statistics */ |
| 2040 | for (i = 0, j = 0; i < MAX_STATS; i++) |
| 2041 | j += scnprintf(buf + j, 200 - j, "|%11s ", hstats[i]); |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 2042 | pr_info("Counter %s", buf); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2043 | |
| 2044 | memset(buf, '-', 115); |
| 2045 | buf[115] = '\0'; |
| 2046 | pr_info("%s\n", buf); |
| 2047 | |
| 2048 | j = scnprintf(buf, 200, "TX(%7.7s) ", tipc_own_id_string(net)); |
| 2049 | for_each_possible_cpu(cpu) { |
| 2050 | for (i = 0; i < MAX_STATS; i++) { |
| 2051 | stat = per_cpu_ptr(tx->stats, cpu)->stat[i]; |
| 2052 | j += scnprintf(buf + j, 200 - j, "|%11d ", stat); |
| 2053 | } |
| 2054 | pr_info("%s", buf); |
| 2055 | j = scnprintf(buf, 200, "%12s", " "); |
| 2056 | } |
| 2057 | |
| 2058 | rcu_read_lock(); |
| 2059 | for (p = tn->node_list.next; p != &tn->node_list; p = p->next) { |
| 2060 | rx = tipc_node_crypto_rx_by_list(p); |
| 2061 | j = scnprintf(buf, 200, "RX(%7.7s) ", |
| 2062 | tipc_node_get_id_str(rx->node)); |
| 2063 | for_each_possible_cpu(cpu) { |
| 2064 | for (i = 0; i < MAX_STATS; i++) { |
| 2065 | stat = per_cpu_ptr(rx->stats, cpu)->stat[i]; |
| 2066 | j += scnprintf(buf + j, 200 - j, "|%11d ", |
| 2067 | stat); |
| 2068 | } |
| 2069 | pr_info("%s", buf); |
| 2070 | j = scnprintf(buf, 200, "%12s", " "); |
| 2071 | } |
| 2072 | } |
| 2073 | rcu_read_unlock(); |
| 2074 | |
| 2075 | pr_info("\n======================== Done ========================\n"); |
| 2076 | } |
| 2077 | |
| 2078 | static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf) |
| 2079 | { |
| 2080 | struct tipc_key key = c->key; |
| 2081 | struct tipc_aead *aead; |
| 2082 | int k, i = 0; |
| 2083 | char *s; |
| 2084 | |
| 2085 | for (k = KEY_MIN; k <= KEY_MAX; k++) { |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 2086 | if (k == KEY_MASTER) { |
| 2087 | if (is_rx(c)) |
| 2088 | continue; |
| 2089 | if (time_before(jiffies, |
| 2090 | c->timer2 + TIPC_TX_GRACE_PERIOD)) |
| 2091 | s = "ACT"; |
| 2092 | else |
| 2093 | s = "PAS"; |
| 2094 | } else { |
| 2095 | if (k == key.passive) |
| 2096 | s = "PAS"; |
| 2097 | else if (k == key.active) |
| 2098 | s = "ACT"; |
| 2099 | else if (k == key.pending) |
| 2100 | s = "PEN"; |
| 2101 | else |
| 2102 | s = "-"; |
| 2103 | } |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2104 | i += scnprintf(buf + i, 200 - i, "\tKey%d: %s", k, s); |
| 2105 | |
| 2106 | rcu_read_lock(); |
| 2107 | aead = rcu_dereference(c->aead[k]); |
| 2108 | if (aead) |
| 2109 | i += scnprintf(buf + i, 200 - i, |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 2110 | "{\"0x...%s\", \"%s\"}/%d:%d", |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2111 | aead->hint, |
| 2112 | (aead->mode == CLUSTER_KEY) ? "c" : "p", |
| 2113 | atomic_read(&aead->users), |
| 2114 | refcount_read(&aead->refcnt)); |
| 2115 | rcu_read_unlock(); |
| 2116 | i += scnprintf(buf + i, 200 - i, "\n"); |
| 2117 | } |
| 2118 | |
Tuong Lien | f779bf7 | 2020-09-18 08:17:26 +0700 | [diff] [blame] | 2119 | if (is_rx(c)) |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2120 | i += scnprintf(buf + i, 200 - i, "\tPeer RX active: %d\n", |
| 2121 | atomic_read(&c->peer_rx_active)); |
| 2122 | |
| 2123 | return buf; |
| 2124 | } |
| 2125 | |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2126 | static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new, |
| 2127 | char *buf) |
| 2128 | { |
| 2129 | struct tipc_key *key = &old; |
| 2130 | int k, i = 0; |
| 2131 | char *s; |
| 2132 | |
| 2133 | /* Output format: "[%s %s %s] -> [%s %s %s]", max len = 32 */ |
| 2134 | again: |
| 2135 | i += scnprintf(buf + i, 32 - i, "["); |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 2136 | for (k = KEY_1; k <= KEY_3; k++) { |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2137 | if (k == key->passive) |
| 2138 | s = "pas"; |
| 2139 | else if (k == key->active) |
| 2140 | s = "act"; |
| 2141 | else if (k == key->pending) |
| 2142 | s = "pen"; |
| 2143 | else |
| 2144 | s = "-"; |
| 2145 | i += scnprintf(buf + i, 32 - i, |
Tuong Lien | daef1ee | 2020-09-18 08:17:27 +0700 | [diff] [blame] | 2146 | (k != KEY_3) ? "%s " : "%s", s); |
Tuong Lien | fc1b6d6 | 2019-11-08 12:05:11 +0700 | [diff] [blame] | 2147 | } |
| 2148 | if (key != &new) { |
| 2149 | i += scnprintf(buf + i, 32 - i, "] -> "); |
| 2150 | key = &new; |
| 2151 | goto again; |
| 2152 | } |
| 2153 | i += scnprintf(buf + i, 32 - i, "]"); |
| 2154 | return buf; |
| 2155 | } |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2156 | |
| 2157 | /** |
| 2158 | * tipc_crypto_msg_rcv - Common 'MSG_CRYPTO' processing point |
| 2159 | * @net: the struct net |
| 2160 | * @skb: the receiving message buffer |
| 2161 | */ |
| 2162 | void tipc_crypto_msg_rcv(struct net *net, struct sk_buff *skb) |
| 2163 | { |
| 2164 | struct tipc_crypto *rx; |
| 2165 | struct tipc_msg *hdr; |
| 2166 | |
| 2167 | if (unlikely(skb_linearize(skb))) |
| 2168 | goto exit; |
| 2169 | |
| 2170 | hdr = buf_msg(skb); |
| 2171 | rx = tipc_node_crypto_rx_by_addr(net, msg_prevnode(hdr)); |
| 2172 | if (unlikely(!rx)) |
| 2173 | goto exit; |
| 2174 | |
| 2175 | switch (msg_type(hdr)) { |
| 2176 | case KEY_DISTR_MSG: |
| 2177 | if (tipc_crypto_key_rcv(rx, hdr)) |
| 2178 | goto exit; |
| 2179 | break; |
| 2180 | default: |
| 2181 | break; |
| 2182 | } |
| 2183 | |
| 2184 | tipc_node_put(rx->node); |
| 2185 | |
| 2186 | exit: |
| 2187 | kfree_skb(skb); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
| 2191 | * tipc_crypto_key_distr - Distribute a TX key |
| 2192 | * @tx: the TX crypto |
| 2193 | * @key: the key's index |
| 2194 | * @dest: the destination tipc node, = NULL if distributing to all nodes |
| 2195 | * |
| 2196 | * Return: 0 in case of success, otherwise < 0 |
| 2197 | */ |
| 2198 | int tipc_crypto_key_distr(struct tipc_crypto *tx, u8 key, |
| 2199 | struct tipc_node *dest) |
| 2200 | { |
| 2201 | struct tipc_aead *aead; |
| 2202 | u32 dnode = tipc_node_get_addr(dest); |
| 2203 | int rc = -ENOKEY; |
| 2204 | |
| 2205 | if (!sysctl_tipc_key_exchange_enabled) |
| 2206 | return 0; |
| 2207 | |
| 2208 | if (key) { |
| 2209 | rcu_read_lock(); |
| 2210 | aead = tipc_aead_get(tx->aead[key]); |
| 2211 | if (likely(aead)) { |
| 2212 | rc = tipc_crypto_key_xmit(tx->net, aead->key, |
| 2213 | aead->gen, aead->mode, |
| 2214 | dnode); |
| 2215 | tipc_aead_put(aead); |
| 2216 | } |
| 2217 | rcu_read_unlock(); |
| 2218 | } |
| 2219 | |
| 2220 | return rc; |
| 2221 | } |
| 2222 | |
| 2223 | /** |
| 2224 | * tipc_crypto_key_xmit - Send a session key |
| 2225 | * @net: the struct net |
| 2226 | * @skey: the session key to be sent |
| 2227 | * @gen: the key's generation |
| 2228 | * @mode: the key's mode |
| 2229 | * @dnode: the destination node address, = 0 if broadcasting to all nodes |
| 2230 | * |
| 2231 | * The session key 'skey' is packed in a TIPC v2 'MSG_CRYPTO/KEY_DISTR_MSG' |
| 2232 | * as its data section, then xmit-ed through the uc/bc link. |
| 2233 | * |
| 2234 | * Return: 0 in case of success, otherwise < 0 |
| 2235 | */ |
| 2236 | static int tipc_crypto_key_xmit(struct net *net, struct tipc_aead_key *skey, |
| 2237 | u16 gen, u8 mode, u32 dnode) |
| 2238 | { |
| 2239 | struct sk_buff_head pkts; |
| 2240 | struct tipc_msg *hdr; |
| 2241 | struct sk_buff *skb; |
| 2242 | u16 size, cong_link_cnt; |
| 2243 | u8 *data; |
| 2244 | int rc; |
| 2245 | |
| 2246 | size = tipc_aead_key_size(skey); |
| 2247 | skb = tipc_buf_acquire(INT_H_SIZE + size, GFP_ATOMIC); |
| 2248 | if (!skb) |
| 2249 | return -ENOMEM; |
| 2250 | |
| 2251 | hdr = buf_msg(skb); |
| 2252 | tipc_msg_init(tipc_own_addr(net), hdr, MSG_CRYPTO, KEY_DISTR_MSG, |
| 2253 | INT_H_SIZE, dnode); |
| 2254 | msg_set_size(hdr, INT_H_SIZE + size); |
| 2255 | msg_set_key_gen(hdr, gen); |
| 2256 | msg_set_key_mode(hdr, mode); |
| 2257 | |
| 2258 | data = msg_data(hdr); |
| 2259 | *((__be32 *)(data + TIPC_AEAD_ALG_NAME)) = htonl(skey->keylen); |
| 2260 | memcpy(data, skey->alg_name, TIPC_AEAD_ALG_NAME); |
| 2261 | memcpy(data + TIPC_AEAD_ALG_NAME + sizeof(__be32), skey->key, |
| 2262 | skey->keylen); |
| 2263 | |
| 2264 | __skb_queue_head_init(&pkts); |
| 2265 | __skb_queue_tail(&pkts, skb); |
| 2266 | if (dnode) |
| 2267 | rc = tipc_node_xmit(net, &pkts, dnode, 0); |
| 2268 | else |
| 2269 | rc = tipc_bcast_xmit(net, &pkts, &cong_link_cnt); |
| 2270 | |
| 2271 | return rc; |
| 2272 | } |
| 2273 | |
| 2274 | /** |
| 2275 | * tipc_crypto_key_rcv - Receive a session key |
| 2276 | * @rx: the RX crypto |
| 2277 | * @hdr: the TIPC v2 message incl. the receiving session key in its data |
| 2278 | * |
| 2279 | * This function retrieves the session key in the message from peer, then |
| 2280 | * schedules a RX work to attach the key to the corresponding RX crypto. |
| 2281 | * |
| 2282 | * Return: "true" if the key has been scheduled for attaching, otherwise |
| 2283 | * "false". |
| 2284 | */ |
| 2285 | static bool tipc_crypto_key_rcv(struct tipc_crypto *rx, struct tipc_msg *hdr) |
| 2286 | { |
| 2287 | struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx; |
| 2288 | struct tipc_aead_key *skey = NULL; |
| 2289 | u16 key_gen = msg_key_gen(hdr); |
| 2290 | u16 size = msg_data_sz(hdr); |
| 2291 | u8 *data = msg_data(hdr); |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2292 | unsigned int keylen; |
| 2293 | |
| 2294 | /* Verify whether the size can exist in the packet */ |
| 2295 | if (unlikely(size < sizeof(struct tipc_aead_key) + TIPC_AEAD_KEYLEN_MIN)) { |
| 2296 | pr_debug("%s: message data size is too small\n", rx->name); |
| 2297 | goto exit; |
| 2298 | } |
| 2299 | |
| 2300 | keylen = ntohl(*((__be32 *)(data + TIPC_AEAD_ALG_NAME))); |
| 2301 | |
| 2302 | /* Verify the supplied size values */ |
| 2303 | if (unlikely(size != keylen + sizeof(struct tipc_aead_key) || |
| 2304 | keylen > TIPC_AEAD_KEY_SIZE_MAX)) { |
| 2305 | pr_debug("%s: invalid MSG_CRYPTO key size\n", rx->name); |
| 2306 | goto exit; |
| 2307 | } |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2308 | |
| 2309 | spin_lock(&rx->lock); |
| 2310 | if (unlikely(rx->skey || (key_gen == rx->key_gen && rx->key.keys))) { |
| 2311 | pr_err("%s: key existed <%p>, gen %d vs %d\n", rx->name, |
| 2312 | rx->skey, key_gen, rx->key_gen); |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2313 | goto exit_unlock; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /* Allocate memory for the key */ |
| 2317 | skey = kmalloc(size, GFP_ATOMIC); |
| 2318 | if (unlikely(!skey)) { |
| 2319 | pr_err("%s: unable to allocate memory for skey\n", rx->name); |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2320 | goto exit_unlock; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | /* Copy key from msg data */ |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2324 | skey->keylen = keylen; |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2325 | memcpy(skey->alg_name, data, TIPC_AEAD_ALG_NAME); |
| 2326 | memcpy(skey->key, data + TIPC_AEAD_ALG_NAME + sizeof(__be32), |
| 2327 | skey->keylen); |
| 2328 | |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2329 | rx->key_gen = key_gen; |
| 2330 | rx->skey_mode = msg_key_mode(hdr); |
| 2331 | rx->skey = skey; |
| 2332 | rx->nokey = 0; |
| 2333 | mb(); /* for nokey flag */ |
| 2334 | |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2335 | exit_unlock: |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2336 | spin_unlock(&rx->lock); |
| 2337 | |
Max VA | fa40d97 | 2021-10-25 17:31:53 +0200 | [diff] [blame] | 2338 | exit: |
Tuong Lien | 1ef6f7c | 2020-09-18 08:17:28 +0700 | [diff] [blame] | 2339 | /* Schedule the key attaching on this crypto */ |
| 2340 | if (likely(skey && queue_delayed_work(tx->wq, &rx->work, 0))) |
| 2341 | return true; |
| 2342 | |
| 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * tipc_crypto_work_rx - Scheduled RX works handler |
| 2348 | * @work: the struct RX work |
| 2349 | * |
| 2350 | * The function processes the previous scheduled works i.e. distributing TX key |
| 2351 | * or attaching a received session key on RX crypto. |
| 2352 | */ |
| 2353 | static void tipc_crypto_work_rx(struct work_struct *work) |
| 2354 | { |
| 2355 | struct delayed_work *dwork = to_delayed_work(work); |
| 2356 | struct tipc_crypto *rx = container_of(dwork, struct tipc_crypto, work); |
| 2357 | struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx; |
| 2358 | unsigned long delay = msecs_to_jiffies(5000); |
| 2359 | bool resched = false; |
| 2360 | u8 key; |
| 2361 | int rc; |
| 2362 | |
| 2363 | /* Case 1: Distribute TX key to peer if scheduled */ |
| 2364 | if (atomic_cmpxchg(&rx->key_distr, |
| 2365 | KEY_DISTR_SCHED, |
| 2366 | KEY_DISTR_COMPL) == KEY_DISTR_SCHED) { |
| 2367 | /* Always pick the newest one for distributing */ |
| 2368 | key = tx->key.pending ?: tx->key.active; |
| 2369 | rc = tipc_crypto_key_distr(tx, key, rx->node); |
| 2370 | if (unlikely(rc)) |
| 2371 | pr_warn("%s: unable to distr key[%d] to %s, err %d\n", |
| 2372 | tx->name, key, tipc_node_get_id_str(rx->node), |
| 2373 | rc); |
| 2374 | |
| 2375 | /* Sched for key_distr releasing */ |
| 2376 | resched = true; |
| 2377 | } else { |
| 2378 | atomic_cmpxchg(&rx->key_distr, KEY_DISTR_COMPL, 0); |
| 2379 | } |
| 2380 | |
| 2381 | /* Case 2: Attach a pending received session key from peer if any */ |
| 2382 | if (rx->skey) { |
| 2383 | rc = tipc_crypto_key_init(rx, rx->skey, rx->skey_mode, false); |
| 2384 | if (unlikely(rc < 0)) |
| 2385 | pr_warn("%s: unable to attach received skey, err %d\n", |
| 2386 | rx->name, rc); |
| 2387 | switch (rc) { |
| 2388 | case -EBUSY: |
| 2389 | case -ENOMEM: |
| 2390 | /* Resched the key attaching */ |
| 2391 | resched = true; |
| 2392 | break; |
| 2393 | default: |
| 2394 | synchronize_rcu(); |
| 2395 | kfree(rx->skey); |
| 2396 | rx->skey = NULL; |
| 2397 | break; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | if (resched && queue_delayed_work(tx->wq, &rx->work, delay)) |
| 2402 | return; |
| 2403 | |
| 2404 | tipc_node_put(rx->node); |
| 2405 | } |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 2406 | |
| 2407 | /** |
| 2408 | * tipc_crypto_rekeying_sched - (Re)schedule rekeying w/o new interval |
| 2409 | * @tx: TX crypto |
| 2410 | * @changed: if the rekeying needs to be rescheduled with new interval |
| 2411 | * @new_intv: new rekeying interval (when "changed" = true) |
| 2412 | */ |
| 2413 | void tipc_crypto_rekeying_sched(struct tipc_crypto *tx, bool changed, |
| 2414 | u32 new_intv) |
| 2415 | { |
| 2416 | unsigned long delay; |
| 2417 | bool now = false; |
| 2418 | |
| 2419 | if (changed) { |
| 2420 | if (new_intv == TIPC_REKEYING_NOW) |
| 2421 | now = true; |
| 2422 | else |
| 2423 | tx->rekeying_intv = new_intv; |
| 2424 | cancel_delayed_work_sync(&tx->work); |
| 2425 | } |
| 2426 | |
| 2427 | if (tx->rekeying_intv || now) { |
| 2428 | delay = (now) ? 0 : tx->rekeying_intv * 60 * 1000; |
| 2429 | queue_delayed_work(tx->wq, &tx->work, msecs_to_jiffies(delay)); |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * tipc_crypto_work_tx - Scheduled TX works handler |
| 2435 | * @work: the struct TX work |
| 2436 | * |
| 2437 | * The function processes the previous scheduled work, i.e. key rekeying, by |
| 2438 | * generating a new session key based on current one, then attaching it to the |
| 2439 | * TX crypto and finally distributing it to peers. It also re-schedules the |
| 2440 | * rekeying if needed. |
| 2441 | */ |
| 2442 | static void tipc_crypto_work_tx(struct work_struct *work) |
| 2443 | { |
| 2444 | struct delayed_work *dwork = to_delayed_work(work); |
| 2445 | struct tipc_crypto *tx = container_of(dwork, struct tipc_crypto, work); |
| 2446 | struct tipc_aead_key *skey = NULL; |
| 2447 | struct tipc_key key = tx->key; |
| 2448 | struct tipc_aead *aead; |
| 2449 | int rc = -ENOMEM; |
| 2450 | |
| 2451 | if (unlikely(key.pending)) |
| 2452 | goto resched; |
| 2453 | |
| 2454 | /* Take current key as a template */ |
| 2455 | rcu_read_lock(); |
| 2456 | aead = rcu_dereference(tx->aead[key.active ?: KEY_MASTER]); |
| 2457 | if (unlikely(!aead)) { |
| 2458 | rcu_read_unlock(); |
| 2459 | /* At least one key should exist for securing */ |
| 2460 | return; |
| 2461 | } |
| 2462 | |
| 2463 | /* Lets duplicate it first */ |
Hoang Le | f845fe5 | 2021-12-17 10:00:59 +0700 | [diff] [blame] | 2464 | skey = kmemdup(aead->key, tipc_aead_key_size(aead->key), GFP_ATOMIC); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 2465 | rcu_read_unlock(); |
| 2466 | |
| 2467 | /* Now, generate new key, initiate & distribute it */ |
| 2468 | if (likely(skey)) { |
| 2469 | rc = tipc_aead_key_generate(skey) ?: |
| 2470 | tipc_crypto_key_init(tx, skey, PER_NODE_KEY, false); |
| 2471 | if (likely(rc > 0)) |
| 2472 | rc = tipc_crypto_key_distr(tx, rc, NULL); |
Eric Biggers | 23224e4 | 2020-10-23 16:27:16 -0700 | [diff] [blame] | 2473 | kfree_sensitive(skey); |
Tuong Lien | 23700da | 2020-09-18 08:17:29 +0700 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | if (unlikely(rc)) |
| 2477 | pr_warn_ratelimited("%s: rekeying returns %d\n", tx->name, rc); |
| 2478 | |
| 2479 | resched: |
| 2480 | /* Re-schedule rekeying if any */ |
| 2481 | tipc_crypto_rekeying_sched(tx, false, 0); |
| 2482 | } |