xref: /openbmc/linux/net/tipc/crypto.c (revision be122522)
1 // SPDX-License-Identifier: GPL-2.0
2 /**
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>
39 #include "crypto.h"
40 
41 #define TIPC_TX_PROBE_LIM	msecs_to_jiffies(1000) /* > 1s */
42 #define TIPC_TX_LASTING_LIM	msecs_to_jiffies(120000) /* 2 mins */
43 #define TIPC_RX_ACTIVE_LIM	msecs_to_jiffies(3000) /* 3s */
44 #define TIPC_RX_PASSIVE_LIM	msecs_to_jiffies(180000) /* 3 mins */
45 #define TIPC_MAX_TFMS_DEF	10
46 #define TIPC_MAX_TFMS_LIM	1000
47 
48 /**
49  * TIPC Key ids
50  */
51 enum {
52 	KEY_UNUSED = 0,
53 	KEY_MIN,
54 	KEY_1 = KEY_MIN,
55 	KEY_2,
56 	KEY_3,
57 	KEY_MAX = KEY_3,
58 };
59 
60 /**
61  * TIPC Crypto statistics
62  */
63 enum {
64 	STAT_OK,
65 	STAT_NOK,
66 	STAT_ASYNC,
67 	STAT_ASYNC_OK,
68 	STAT_ASYNC_NOK,
69 	STAT_BADKEYS, /* tx only */
70 	STAT_BADMSGS = STAT_BADKEYS, /* rx only */
71 	STAT_NOKEYS,
72 	STAT_SWITCHES,
73 
74 	MAX_STATS,
75 };
76 
77 /* TIPC crypto statistics' header */
78 static const char *hstats[MAX_STATS] = {"ok", "nok", "async", "async_ok",
79 					"async_nok", "badmsgs", "nokeys",
80 					"switches"};
81 
82 /* Max TFMs number per key */
83 int sysctl_tipc_max_tfms __read_mostly = TIPC_MAX_TFMS_DEF;
84 
85 /**
86  * struct tipc_key - TIPC keys' status indicator
87  *
88  *         7     6     5     4     3     2     1     0
89  *      +-----+-----+-----+-----+-----+-----+-----+-----+
90  * key: | (reserved)|passive idx| active idx|pending idx|
91  *      +-----+-----+-----+-----+-----+-----+-----+-----+
92  */
93 struct tipc_key {
94 #define KEY_BITS (2)
95 #define KEY_MASK ((1 << KEY_BITS) - 1)
96 	union {
97 		struct {
98 #if defined(__LITTLE_ENDIAN_BITFIELD)
99 			u8 pending:2,
100 			   active:2,
101 			   passive:2, /* rx only */
102 			   reserved:2;
103 #elif defined(__BIG_ENDIAN_BITFIELD)
104 			u8 reserved:2,
105 			   passive:2, /* rx only */
106 			   active:2,
107 			   pending:2;
108 #else
109 #error  "Please fix <asm/byteorder.h>"
110 #endif
111 		} __packed;
112 		u8 keys;
113 	};
114 };
115 
116 /**
117  * struct tipc_tfm - TIPC TFM structure to form a list of TFMs
118  */
119 struct tipc_tfm {
120 	struct crypto_aead *tfm;
121 	struct list_head list;
122 };
123 
124 /**
125  * struct tipc_aead - TIPC AEAD key structure
126  * @tfm_entry: per-cpu pointer to one entry in TFM list
127  * @crypto: TIPC crypto owns this key
128  * @cloned: reference to the source key in case cloning
129  * @users: the number of the key users (TX/RX)
130  * @salt: the key's SALT value
131  * @authsize: authentication tag size (max = 16)
132  * @mode: crypto mode is applied to the key
133  * @hint[]: a hint for user key
134  * @rcu: struct rcu_head
135  * @seqno: the key seqno (cluster scope)
136  * @refcnt: the key reference counter
137  */
138 struct tipc_aead {
139 #define TIPC_AEAD_HINT_LEN (5)
140 	struct tipc_tfm * __percpu *tfm_entry;
141 	struct tipc_crypto *crypto;
142 	struct tipc_aead *cloned;
143 	atomic_t users;
144 	u32 salt;
145 	u8 authsize;
146 	u8 mode;
147 	char hint[TIPC_AEAD_HINT_LEN + 1];
148 	struct rcu_head rcu;
149 
150 	atomic64_t seqno ____cacheline_aligned;
151 	refcount_t refcnt ____cacheline_aligned;
152 
153 } ____cacheline_aligned;
154 
155 /**
156  * struct tipc_crypto_stats - TIPC Crypto statistics
157  */
158 struct tipc_crypto_stats {
159 	unsigned int stat[MAX_STATS];
160 };
161 
162 /**
163  * struct tipc_crypto - TIPC TX/RX crypto structure
164  * @net: struct net
165  * @node: TIPC node (RX)
166  * @aead: array of pointers to AEAD keys for encryption/decryption
167  * @peer_rx_active: replicated peer RX active key index
168  * @key: the key states
169  * @working: the crypto is working or not
170  * @stats: the crypto statistics
171  * @sndnxt: the per-peer sndnxt (TX)
172  * @timer1: general timer 1 (jiffies)
173  * @timer2: general timer 1 (jiffies)
174  * @lock: tipc_key lock
175  */
176 struct tipc_crypto {
177 	struct net *net;
178 	struct tipc_node *node;
179 	struct tipc_aead __rcu *aead[KEY_MAX + 1]; /* key[0] is UNUSED */
180 	atomic_t peer_rx_active;
181 	struct tipc_key key;
182 	u8 working:1;
183 	struct tipc_crypto_stats __percpu *stats;
184 
185 	atomic64_t sndnxt ____cacheline_aligned;
186 	unsigned long timer1;
187 	unsigned long timer2;
188 	spinlock_t lock; /* crypto lock */
189 
190 } ____cacheline_aligned;
191 
192 /* struct tipc_crypto_tx_ctx - TX context for callbacks */
193 struct tipc_crypto_tx_ctx {
194 	struct tipc_aead *aead;
195 	struct tipc_bearer *bearer;
196 	struct tipc_media_addr dst;
197 };
198 
199 /* struct tipc_crypto_rx_ctx - RX context for callbacks */
200 struct tipc_crypto_rx_ctx {
201 	struct tipc_aead *aead;
202 	struct tipc_bearer *bearer;
203 };
204 
205 static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead);
206 static inline void tipc_aead_put(struct tipc_aead *aead);
207 static void tipc_aead_free(struct rcu_head *rp);
208 static int tipc_aead_users(struct tipc_aead __rcu *aead);
209 static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim);
210 static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim);
211 static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val);
212 static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead);
213 static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
214 			  u8 mode);
215 static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src);
216 static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
217 				 unsigned int crypto_ctx_size,
218 				 u8 **iv, struct aead_request **req,
219 				 struct scatterlist **sg, int nsg);
220 static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
221 			     struct tipc_bearer *b,
222 			     struct tipc_media_addr *dst,
223 			     struct tipc_node *__dnode);
224 static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err);
225 static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
226 			     struct sk_buff *skb, struct tipc_bearer *b);
227 static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err);
228 static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr);
229 static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
230 			   u8 tx_key, struct sk_buff *skb,
231 			   struct tipc_crypto *__rx);
232 static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
233 					     u8 new_passive,
234 					     u8 new_active,
235 					     u8 new_pending);
236 static int tipc_crypto_key_attach(struct tipc_crypto *c,
237 				  struct tipc_aead *aead, u8 pos);
238 static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending);
239 static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
240 						 struct tipc_crypto *rx,
241 						 struct sk_buff *skb);
242 static void tipc_crypto_key_synch(struct tipc_crypto *rx, u8 new_rx_active,
243 				  struct tipc_msg *hdr);
244 static int tipc_crypto_key_revoke(struct net *net, u8 tx_key);
245 static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
246 				     struct tipc_bearer *b,
247 				     struct sk_buff **skb, int err);
248 static void tipc_crypto_do_cmd(struct net *net, int cmd);
249 static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf);
250 #ifdef TIPC_CRYPTO_DEBUG
251 static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
252 				  char *buf);
253 #endif
254 
255 #define key_next(cur) ((cur) % KEY_MAX + 1)
256 
257 #define tipc_aead_rcu_ptr(rcu_ptr, lock)				\
258 	rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock))
259 
260 #define tipc_aead_rcu_swap(rcu_ptr, ptr, lock)				\
261 	rcu_swap_protected((rcu_ptr), (ptr), lockdep_is_held(lock))
262 
263 #define tipc_aead_rcu_replace(rcu_ptr, ptr, lock)			\
264 do {									\
265 	typeof(rcu_ptr) __tmp = rcu_dereference_protected((rcu_ptr),	\
266 						lockdep_is_held(lock));	\
267 	rcu_assign_pointer((rcu_ptr), (ptr));				\
268 	tipc_aead_put(__tmp);						\
269 } while (0)
270 
271 #define tipc_crypto_key_detach(rcu_ptr, lock)				\
272 	tipc_aead_rcu_replace((rcu_ptr), NULL, lock)
273 
274 /**
275  * tipc_aead_key_validate - Validate a AEAD user key
276  */
277 int tipc_aead_key_validate(struct tipc_aead_key *ukey)
278 {
279 	int keylen;
280 
281 	/* Check if algorithm exists */
282 	if (unlikely(!crypto_has_alg(ukey->alg_name, 0, 0))) {
283 		pr_info("Not found cipher: \"%s\"!\n", ukey->alg_name);
284 		return -ENODEV;
285 	}
286 
287 	/* Currently, we only support the "gcm(aes)" cipher algorithm */
288 	if (strcmp(ukey->alg_name, "gcm(aes)"))
289 		return -ENOTSUPP;
290 
291 	/* Check if key size is correct */
292 	keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
293 	if (unlikely(keylen != TIPC_AES_GCM_KEY_SIZE_128 &&
294 		     keylen != TIPC_AES_GCM_KEY_SIZE_192 &&
295 		     keylen != TIPC_AES_GCM_KEY_SIZE_256))
296 		return -EINVAL;
297 
298 	return 0;
299 }
300 
301 static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead)
302 {
303 	struct tipc_aead *tmp;
304 
305 	rcu_read_lock();
306 	tmp = rcu_dereference(aead);
307 	if (unlikely(!tmp || !refcount_inc_not_zero(&tmp->refcnt)))
308 		tmp = NULL;
309 	rcu_read_unlock();
310 
311 	return tmp;
312 }
313 
314 static inline void tipc_aead_put(struct tipc_aead *aead)
315 {
316 	if (aead && refcount_dec_and_test(&aead->refcnt))
317 		call_rcu(&aead->rcu, tipc_aead_free);
318 }
319 
320 /**
321  * tipc_aead_free - Release AEAD key incl. all the TFMs in the list
322  * @rp: rcu head pointer
323  */
324 static void tipc_aead_free(struct rcu_head *rp)
325 {
326 	struct tipc_aead *aead = container_of(rp, struct tipc_aead, rcu);
327 	struct tipc_tfm *tfm_entry, *head, *tmp;
328 
329 	if (aead->cloned) {
330 		tipc_aead_put(aead->cloned);
331 	} else {
332 		head = *this_cpu_ptr(aead->tfm_entry);
333 		list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) {
334 			crypto_free_aead(tfm_entry->tfm);
335 			list_del(&tfm_entry->list);
336 			kfree(tfm_entry);
337 		}
338 		/* Free the head */
339 		crypto_free_aead(head->tfm);
340 		list_del(&head->list);
341 		kfree(head);
342 	}
343 	free_percpu(aead->tfm_entry);
344 	kfree(aead);
345 }
346 
347 static int tipc_aead_users(struct tipc_aead __rcu *aead)
348 {
349 	struct tipc_aead *tmp;
350 	int users = 0;
351 
352 	rcu_read_lock();
353 	tmp = rcu_dereference(aead);
354 	if (tmp)
355 		users = atomic_read(&tmp->users);
356 	rcu_read_unlock();
357 
358 	return users;
359 }
360 
361 static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim)
362 {
363 	struct tipc_aead *tmp;
364 
365 	rcu_read_lock();
366 	tmp = rcu_dereference(aead);
367 	if (tmp)
368 		atomic_add_unless(&tmp->users, 1, lim);
369 	rcu_read_unlock();
370 }
371 
372 static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim)
373 {
374 	struct tipc_aead *tmp;
375 
376 	rcu_read_lock();
377 	tmp = rcu_dereference(aead);
378 	if (tmp)
379 		atomic_add_unless(&rcu_dereference(aead)->users, -1, lim);
380 	rcu_read_unlock();
381 }
382 
383 static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
384 {
385 	struct tipc_aead *tmp;
386 	int cur;
387 
388 	rcu_read_lock();
389 	tmp = rcu_dereference(aead);
390 	if (tmp) {
391 		do {
392 			cur = atomic_read(&tmp->users);
393 			if (cur == val)
394 				break;
395 		} while (atomic_cmpxchg(&tmp->users, cur, val) != cur);
396 	}
397 	rcu_read_unlock();
398 }
399 
400 /**
401  * tipc_aead_tfm_next - Move TFM entry to the next one in list and return it
402  */
403 static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
404 {
405 	struct tipc_tfm **tfm_entry = this_cpu_ptr(aead->tfm_entry);
406 
407 	*tfm_entry = list_next_entry(*tfm_entry, list);
408 	return (*tfm_entry)->tfm;
409 }
410 
411 /**
412  * tipc_aead_init - Initiate TIPC AEAD
413  * @aead: returned new TIPC AEAD key handle pointer
414  * @ukey: pointer to user key data
415  * @mode: the key mode
416  *
417  * Allocate a (list of) new cipher transformation (TFM) with the specific user
418  * key data if valid. The number of the allocated TFMs can be set via the sysfs
419  * "net/tipc/max_tfms" first.
420  * Also, all the other AEAD data are also initialized.
421  *
422  * Return: 0 if the initiation is successful, otherwise: < 0
423  */
424 static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
425 			  u8 mode)
426 {
427 	struct tipc_tfm *tfm_entry, *head;
428 	struct crypto_aead *tfm;
429 	struct tipc_aead *tmp;
430 	int keylen, err, cpu;
431 	int tfm_cnt = 0;
432 
433 	if (unlikely(*aead))
434 		return -EEXIST;
435 
436 	/* Allocate a new AEAD */
437 	tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
438 	if (unlikely(!tmp))
439 		return -ENOMEM;
440 
441 	/* The key consists of two parts: [AES-KEY][SALT] */
442 	keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
443 
444 	/* Allocate per-cpu TFM entry pointer */
445 	tmp->tfm_entry = alloc_percpu(struct tipc_tfm *);
446 	if (!tmp->tfm_entry) {
447 		kzfree(tmp);
448 		return -ENOMEM;
449 	}
450 
451 	/* Make a list of TFMs with the user key data */
452 	do {
453 		tfm = crypto_alloc_aead(ukey->alg_name, 0, 0);
454 		if (IS_ERR(tfm)) {
455 			err = PTR_ERR(tfm);
456 			break;
457 		}
458 
459 		if (unlikely(!tfm_cnt &&
460 			     crypto_aead_ivsize(tfm) != TIPC_AES_GCM_IV_SIZE)) {
461 			crypto_free_aead(tfm);
462 			err = -ENOTSUPP;
463 			break;
464 		}
465 
466 		err = crypto_aead_setauthsize(tfm, TIPC_AES_GCM_TAG_SIZE);
467 		err |= crypto_aead_setkey(tfm, ukey->key, keylen);
468 		if (unlikely(err)) {
469 			crypto_free_aead(tfm);
470 			break;
471 		}
472 
473 		tfm_entry = kmalloc(sizeof(*tfm_entry), GFP_KERNEL);
474 		if (unlikely(!tfm_entry)) {
475 			crypto_free_aead(tfm);
476 			err = -ENOMEM;
477 			break;
478 		}
479 		INIT_LIST_HEAD(&tfm_entry->list);
480 		tfm_entry->tfm = tfm;
481 
482 		/* First entry? */
483 		if (!tfm_cnt) {
484 			head = tfm_entry;
485 			for_each_possible_cpu(cpu) {
486 				*per_cpu_ptr(tmp->tfm_entry, cpu) = head;
487 			}
488 		} else {
489 			list_add_tail(&tfm_entry->list, &head->list);
490 		}
491 
492 	} while (++tfm_cnt < sysctl_tipc_max_tfms);
493 
494 	/* Not any TFM is allocated? */
495 	if (!tfm_cnt) {
496 		free_percpu(tmp->tfm_entry);
497 		kzfree(tmp);
498 		return err;
499 	}
500 
501 	/* Copy some chars from the user key as a hint */
502 	memcpy(tmp->hint, ukey->key, TIPC_AEAD_HINT_LEN);
503 	tmp->hint[TIPC_AEAD_HINT_LEN] = '\0';
504 
505 	/* Initialize the other data */
506 	tmp->mode = mode;
507 	tmp->cloned = NULL;
508 	tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
509 	memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
510 	atomic_set(&tmp->users, 0);
511 	atomic64_set(&tmp->seqno, 0);
512 	refcount_set(&tmp->refcnt, 1);
513 
514 	*aead = tmp;
515 	return 0;
516 }
517 
518 /**
519  * tipc_aead_clone - Clone a TIPC AEAD key
520  * @dst: dest key for the cloning
521  * @src: source key to clone from
522  *
523  * Make a "copy" of the source AEAD key data to the dest, the TFMs list is
524  * common for the keys.
525  * A reference to the source is hold in the "cloned" pointer for the later
526  * freeing purposes.
527  *
528  * Note: this must be done in cluster-key mode only!
529  * Return: 0 in case of success, otherwise < 0
530  */
531 static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src)
532 {
533 	struct tipc_aead *aead;
534 	int cpu;
535 
536 	if (!src)
537 		return -ENOKEY;
538 
539 	if (src->mode != CLUSTER_KEY)
540 		return -EINVAL;
541 
542 	if (unlikely(*dst))
543 		return -EEXIST;
544 
545 	aead = kzalloc(sizeof(*aead), GFP_ATOMIC);
546 	if (unlikely(!aead))
547 		return -ENOMEM;
548 
549 	aead->tfm_entry = alloc_percpu_gfp(struct tipc_tfm *, GFP_ATOMIC);
550 	if (unlikely(!aead->tfm_entry)) {
551 		kzfree(aead);
552 		return -ENOMEM;
553 	}
554 
555 	for_each_possible_cpu(cpu) {
556 		*per_cpu_ptr(aead->tfm_entry, cpu) =
557 				*per_cpu_ptr(src->tfm_entry, cpu);
558 	}
559 
560 	memcpy(aead->hint, src->hint, sizeof(src->hint));
561 	aead->mode = src->mode;
562 	aead->salt = src->salt;
563 	aead->authsize = src->authsize;
564 	atomic_set(&aead->users, 0);
565 	atomic64_set(&aead->seqno, 0);
566 	refcount_set(&aead->refcnt, 1);
567 
568 	WARN_ON(!refcount_inc_not_zero(&src->refcnt));
569 	aead->cloned = src;
570 
571 	*dst = aead;
572 	return 0;
573 }
574 
575 /**
576  * tipc_aead_mem_alloc - Allocate memory for AEAD request operations
577  * @tfm: cipher handle to be registered with the request
578  * @crypto_ctx_size: size of crypto context for callback
579  * @iv: returned pointer to IV data
580  * @req: returned pointer to AEAD request data
581  * @sg: returned pointer to SG lists
582  * @nsg: number of SG lists to be allocated
583  *
584  * Allocate memory to store the crypto context data, AEAD request, IV and SG
585  * lists, the memory layout is as follows:
586  * crypto_ctx || iv || aead_req || sg[]
587  *
588  * Return: the pointer to the memory areas in case of success, otherwise NULL
589  */
590 static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
591 				 unsigned int crypto_ctx_size,
592 				 u8 **iv, struct aead_request **req,
593 				 struct scatterlist **sg, int nsg)
594 {
595 	unsigned int iv_size, req_size;
596 	unsigned int len;
597 	u8 *mem;
598 
599 	iv_size = crypto_aead_ivsize(tfm);
600 	req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
601 
602 	len = crypto_ctx_size;
603 	len += iv_size;
604 	len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
605 	len = ALIGN(len, crypto_tfm_ctx_alignment());
606 	len += req_size;
607 	len = ALIGN(len, __alignof__(struct scatterlist));
608 	len += nsg * sizeof(**sg);
609 
610 	mem = kmalloc(len, GFP_ATOMIC);
611 	if (!mem)
612 		return NULL;
613 
614 	*iv = (u8 *)PTR_ALIGN(mem + crypto_ctx_size,
615 			      crypto_aead_alignmask(tfm) + 1);
616 	*req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
617 						crypto_tfm_ctx_alignment());
618 	*sg = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
619 					      __alignof__(struct scatterlist));
620 
621 	return (void *)mem;
622 }
623 
624 /**
625  * tipc_aead_encrypt - Encrypt a message
626  * @aead: TIPC AEAD key for the message encryption
627  * @skb: the input/output skb
628  * @b: TIPC bearer where the message will be delivered after the encryption
629  * @dst: the destination media address
630  * @__dnode: TIPC dest node if "known"
631  *
632  * Return:
633  * 0                   : if the encryption has completed
634  * -EINPROGRESS/-EBUSY : if a callback will be performed
635  * < 0                 : the encryption has failed
636  */
637 static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
638 			     struct tipc_bearer *b,
639 			     struct tipc_media_addr *dst,
640 			     struct tipc_node *__dnode)
641 {
642 	struct crypto_aead *tfm = tipc_aead_tfm_next(aead);
643 	struct tipc_crypto_tx_ctx *tx_ctx;
644 	struct aead_request *req;
645 	struct sk_buff *trailer;
646 	struct scatterlist *sg;
647 	struct tipc_ehdr *ehdr;
648 	int ehsz, len, tailen, nsg, rc;
649 	void *ctx;
650 	u32 salt;
651 	u8 *iv;
652 
653 	/* Make sure message len at least 4-byte aligned */
654 	len = ALIGN(skb->len, 4);
655 	tailen = len - skb->len + aead->authsize;
656 
657 	/* Expand skb tail for authentication tag:
658 	 * As for simplicity, we'd have made sure skb having enough tailroom
659 	 * for authentication tag @skb allocation. Even when skb is nonlinear
660 	 * but there is no frag_list, it should be still fine!
661 	 * Otherwise, we must cow it to be a writable buffer with the tailroom.
662 	 */
663 #ifdef TIPC_CRYPTO_DEBUG
664 	SKB_LINEAR_ASSERT(skb);
665 	if (tailen > skb_tailroom(skb)) {
666 		pr_warn("TX: skb tailroom is not enough: %d, requires: %d\n",
667 			skb_tailroom(skb), tailen);
668 	}
669 #endif
670 
671 	if (unlikely(!skb_cloned(skb) && tailen <= skb_tailroom(skb))) {
672 		nsg = 1;
673 		trailer = skb;
674 	} else {
675 		/* TODO: We could avoid skb_cow_data() if skb has no frag_list
676 		 * e.g. by skb_fill_page_desc() to add another page to the skb
677 		 * with the wanted tailen... However, page skbs look not often,
678 		 * so take it easy now!
679 		 * Cloned skbs e.g. from link_xmit() seems no choice though :(
680 		 */
681 		nsg = skb_cow_data(skb, tailen, &trailer);
682 		if (unlikely(nsg < 0)) {
683 			pr_err("TX: skb_cow_data() returned %d\n", nsg);
684 			return nsg;
685 		}
686 	}
687 
688 	pskb_put(skb, trailer, tailen);
689 
690 	/* Allocate memory for the AEAD operation */
691 	ctx = tipc_aead_mem_alloc(tfm, sizeof(*tx_ctx), &iv, &req, &sg, nsg);
692 	if (unlikely(!ctx))
693 		return -ENOMEM;
694 	TIPC_SKB_CB(skb)->crypto_ctx = ctx;
695 
696 	/* Map skb to the sg lists */
697 	sg_init_table(sg, nsg);
698 	rc = skb_to_sgvec(skb, sg, 0, skb->len);
699 	if (unlikely(rc < 0)) {
700 		pr_err("TX: skb_to_sgvec() returned %d, nsg %d!\n", rc, nsg);
701 		goto exit;
702 	}
703 
704 	/* Prepare IV: [SALT (4 octets)][SEQNO (8 octets)]
705 	 * In case we're in cluster-key mode, SALT is varied by xor-ing with
706 	 * the source address (or w0 of id), otherwise with the dest address
707 	 * if dest is known.
708 	 */
709 	ehdr = (struct tipc_ehdr *)skb->data;
710 	salt = aead->salt;
711 	if (aead->mode == CLUSTER_KEY)
712 		salt ^= ehdr->addr; /* __be32 */
713 	else if (__dnode)
714 		salt ^= tipc_node_get_addr(__dnode);
715 	memcpy(iv, &salt, 4);
716 	memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
717 
718 	/* Prepare request */
719 	ehsz = tipc_ehdr_size(ehdr);
720 	aead_request_set_tfm(req, tfm);
721 	aead_request_set_ad(req, ehsz);
722 	aead_request_set_crypt(req, sg, sg, len - ehsz, iv);
723 
724 	/* Set callback function & data */
725 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
726 				  tipc_aead_encrypt_done, skb);
727 	tx_ctx = (struct tipc_crypto_tx_ctx *)ctx;
728 	tx_ctx->aead = aead;
729 	tx_ctx->bearer = b;
730 	memcpy(&tx_ctx->dst, dst, sizeof(*dst));
731 
732 	/* Hold bearer */
733 	if (unlikely(!tipc_bearer_hold(b))) {
734 		rc = -ENODEV;
735 		goto exit;
736 	}
737 
738 	/* Now, do encrypt */
739 	rc = crypto_aead_encrypt(req);
740 	if (rc == -EINPROGRESS || rc == -EBUSY)
741 		return rc;
742 
743 	tipc_bearer_put(b);
744 
745 exit:
746 	kfree(ctx);
747 	TIPC_SKB_CB(skb)->crypto_ctx = NULL;
748 	return rc;
749 }
750 
751 static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err)
752 {
753 	struct sk_buff *skb = base->data;
754 	struct tipc_crypto_tx_ctx *tx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
755 	struct tipc_bearer *b = tx_ctx->bearer;
756 	struct tipc_aead *aead = tx_ctx->aead;
757 	struct tipc_crypto *tx = aead->crypto;
758 	struct net *net = tx->net;
759 
760 	switch (err) {
761 	case 0:
762 		this_cpu_inc(tx->stats->stat[STAT_ASYNC_OK]);
763 		if (likely(test_bit(0, &b->up)))
764 			b->media->send_msg(net, skb, b, &tx_ctx->dst);
765 		else
766 			kfree_skb(skb);
767 		break;
768 	case -EINPROGRESS:
769 		return;
770 	default:
771 		this_cpu_inc(tx->stats->stat[STAT_ASYNC_NOK]);
772 		kfree_skb(skb);
773 		break;
774 	}
775 
776 	kfree(tx_ctx);
777 	tipc_bearer_put(b);
778 	tipc_aead_put(aead);
779 }
780 
781 /**
782  * tipc_aead_decrypt - Decrypt an encrypted message
783  * @net: struct net
784  * @aead: TIPC AEAD for the message decryption
785  * @skb: the input/output skb
786  * @b: TIPC bearer where the message has been received
787  *
788  * Return:
789  * 0                   : if the decryption has completed
790  * -EINPROGRESS/-EBUSY : if a callback will be performed
791  * < 0                 : the decryption has failed
792  */
793 static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
794 			     struct sk_buff *skb, struct tipc_bearer *b)
795 {
796 	struct tipc_crypto_rx_ctx *rx_ctx;
797 	struct aead_request *req;
798 	struct crypto_aead *tfm;
799 	struct sk_buff *unused;
800 	struct scatterlist *sg;
801 	struct tipc_ehdr *ehdr;
802 	int ehsz, nsg, rc;
803 	void *ctx;
804 	u32 salt;
805 	u8 *iv;
806 
807 	if (unlikely(!aead))
808 		return -ENOKEY;
809 
810 	/* Cow skb data if needed */
811 	if (likely(!skb_cloned(skb) &&
812 		   (!skb_is_nonlinear(skb) || !skb_has_frag_list(skb)))) {
813 		nsg = 1 + skb_shinfo(skb)->nr_frags;
814 	} else {
815 		nsg = skb_cow_data(skb, 0, &unused);
816 		if (unlikely(nsg < 0)) {
817 			pr_err("RX: skb_cow_data() returned %d\n", nsg);
818 			return nsg;
819 		}
820 	}
821 
822 	/* Allocate memory for the AEAD operation */
823 	tfm = tipc_aead_tfm_next(aead);
824 	ctx = tipc_aead_mem_alloc(tfm, sizeof(*rx_ctx), &iv, &req, &sg, nsg);
825 	if (unlikely(!ctx))
826 		return -ENOMEM;
827 	TIPC_SKB_CB(skb)->crypto_ctx = ctx;
828 
829 	/* Map skb to the sg lists */
830 	sg_init_table(sg, nsg);
831 	rc = skb_to_sgvec(skb, sg, 0, skb->len);
832 	if (unlikely(rc < 0)) {
833 		pr_err("RX: skb_to_sgvec() returned %d, nsg %d\n", rc, nsg);
834 		goto exit;
835 	}
836 
837 	/* Reconstruct IV: */
838 	ehdr = (struct tipc_ehdr *)skb->data;
839 	salt = aead->salt;
840 	if (aead->mode == CLUSTER_KEY)
841 		salt ^= ehdr->addr; /* __be32 */
842 	else if (ehdr->destined)
843 		salt ^= tipc_own_addr(net);
844 	memcpy(iv, &salt, 4);
845 	memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
846 
847 	/* Prepare request */
848 	ehsz = tipc_ehdr_size(ehdr);
849 	aead_request_set_tfm(req, tfm);
850 	aead_request_set_ad(req, ehsz);
851 	aead_request_set_crypt(req, sg, sg, skb->len - ehsz, iv);
852 
853 	/* Set callback function & data */
854 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
855 				  tipc_aead_decrypt_done, skb);
856 	rx_ctx = (struct tipc_crypto_rx_ctx *)ctx;
857 	rx_ctx->aead = aead;
858 	rx_ctx->bearer = b;
859 
860 	/* Hold bearer */
861 	if (unlikely(!tipc_bearer_hold(b))) {
862 		rc = -ENODEV;
863 		goto exit;
864 	}
865 
866 	/* Now, do decrypt */
867 	rc = crypto_aead_decrypt(req);
868 	if (rc == -EINPROGRESS || rc == -EBUSY)
869 		return rc;
870 
871 	tipc_bearer_put(b);
872 
873 exit:
874 	kfree(ctx);
875 	TIPC_SKB_CB(skb)->crypto_ctx = NULL;
876 	return rc;
877 }
878 
879 static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err)
880 {
881 	struct sk_buff *skb = base->data;
882 	struct tipc_crypto_rx_ctx *rx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
883 	struct tipc_bearer *b = rx_ctx->bearer;
884 	struct tipc_aead *aead = rx_ctx->aead;
885 	struct tipc_crypto_stats __percpu *stats = aead->crypto->stats;
886 	struct net *net = aead->crypto->net;
887 
888 	switch (err) {
889 	case 0:
890 		this_cpu_inc(stats->stat[STAT_ASYNC_OK]);
891 		break;
892 	case -EINPROGRESS:
893 		return;
894 	default:
895 		this_cpu_inc(stats->stat[STAT_ASYNC_NOK]);
896 		break;
897 	}
898 
899 	kfree(rx_ctx);
900 	tipc_crypto_rcv_complete(net, aead, b, &skb, err);
901 	if (likely(skb)) {
902 		if (likely(test_bit(0, &b->up)))
903 			tipc_rcv(net, skb, b);
904 		else
905 			kfree_skb(skb);
906 	}
907 
908 	tipc_bearer_put(b);
909 }
910 
911 static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr)
912 {
913 	return (ehdr->user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
914 }
915 
916 /**
917  * tipc_ehdr_validate - Validate an encryption message
918  * @skb: the message buffer
919  *
920  * Returns "true" if this is a valid encryption message, otherwise "false"
921  */
922 bool tipc_ehdr_validate(struct sk_buff *skb)
923 {
924 	struct tipc_ehdr *ehdr;
925 	int ehsz;
926 
927 	if (unlikely(!pskb_may_pull(skb, EHDR_MIN_SIZE)))
928 		return false;
929 
930 	ehdr = (struct tipc_ehdr *)skb->data;
931 	if (unlikely(ehdr->version != TIPC_EVERSION))
932 		return false;
933 	ehsz = tipc_ehdr_size(ehdr);
934 	if (unlikely(!pskb_may_pull(skb, ehsz)))
935 		return false;
936 	if (unlikely(skb->len <= ehsz + TIPC_AES_GCM_TAG_SIZE))
937 		return false;
938 	if (unlikely(!ehdr->tx_key))
939 		return false;
940 
941 	return true;
942 }
943 
944 /**
945  * tipc_ehdr_build - Build TIPC encryption message header
946  * @net: struct net
947  * @aead: TX AEAD key to be used for the message encryption
948  * @tx_key: key id used for the message encryption
949  * @skb: input/output message skb
950  * @__rx: RX crypto handle if dest is "known"
951  *
952  * Return: the header size if the building is successful, otherwise < 0
953  */
954 static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
955 			   u8 tx_key, struct sk_buff *skb,
956 			   struct tipc_crypto *__rx)
957 {
958 	struct tipc_msg *hdr = buf_msg(skb);
959 	struct tipc_ehdr *ehdr;
960 	u32 user = msg_user(hdr);
961 	u64 seqno;
962 	int ehsz;
963 
964 	/* Make room for encryption header */
965 	ehsz = (user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
966 	WARN_ON(skb_headroom(skb) < ehsz);
967 	ehdr = (struct tipc_ehdr *)skb_push(skb, ehsz);
968 
969 	/* Obtain a seqno first:
970 	 * Use the key seqno (= cluster wise) if dest is unknown or we're in
971 	 * cluster key mode, otherwise it's better for a per-peer seqno!
972 	 */
973 	if (!__rx || aead->mode == CLUSTER_KEY)
974 		seqno = atomic64_inc_return(&aead->seqno);
975 	else
976 		seqno = atomic64_inc_return(&__rx->sndnxt);
977 
978 	/* Revoke the key if seqno is wrapped around */
979 	if (unlikely(!seqno))
980 		return tipc_crypto_key_revoke(net, tx_key);
981 
982 	/* Word 1-2 */
983 	ehdr->seqno = cpu_to_be64(seqno);
984 
985 	/* Words 0, 3- */
986 	ehdr->version = TIPC_EVERSION;
987 	ehdr->user = 0;
988 	ehdr->keepalive = 0;
989 	ehdr->tx_key = tx_key;
990 	ehdr->destined = (__rx) ? 1 : 0;
991 	ehdr->rx_key_active = (__rx) ? __rx->key.active : 0;
992 	ehdr->reserved_1 = 0;
993 	ehdr->reserved_2 = 0;
994 
995 	switch (user) {
996 	case LINK_CONFIG:
997 		ehdr->user = LINK_CONFIG;
998 		memcpy(ehdr->id, tipc_own_id(net), NODE_ID_LEN);
999 		break;
1000 	default:
1001 		if (user == LINK_PROTOCOL && msg_type(hdr) == STATE_MSG) {
1002 			ehdr->user = LINK_PROTOCOL;
1003 			ehdr->keepalive = msg_is_keepalive(hdr);
1004 		}
1005 		ehdr->addr = hdr->hdr[3];
1006 		break;
1007 	}
1008 
1009 	return ehsz;
1010 }
1011 
1012 static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
1013 					     u8 new_passive,
1014 					     u8 new_active,
1015 					     u8 new_pending)
1016 {
1017 #ifdef TIPC_CRYPTO_DEBUG
1018 	struct tipc_key old = c->key;
1019 	char buf[32];
1020 #endif
1021 
1022 	c->key.keys = ((new_passive & KEY_MASK) << (KEY_BITS * 2)) |
1023 		      ((new_active  & KEY_MASK) << (KEY_BITS)) |
1024 		      ((new_pending & KEY_MASK));
1025 
1026 #ifdef TIPC_CRYPTO_DEBUG
1027 	pr_info("%s(%s): key changing %s ::%pS\n",
1028 		(c->node) ? "RX" : "TX",
1029 		(c->node) ? tipc_node_get_id_str(c->node) :
1030 			    tipc_own_id_string(c->net),
1031 		tipc_key_change_dump(old, c->key, buf),
1032 		__builtin_return_address(0));
1033 #endif
1034 }
1035 
1036 /**
1037  * tipc_crypto_key_init - Initiate a new user / AEAD key
1038  * @c: TIPC crypto to which new key is attached
1039  * @ukey: the user key
1040  * @mode: the key mode (CLUSTER_KEY or PER_NODE_KEY)
1041  *
1042  * A new TIPC AEAD key will be allocated and initiated with the specified user
1043  * key, then attached to the TIPC crypto.
1044  *
1045  * Return: new key id in case of success, otherwise: < 0
1046  */
1047 int tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey,
1048 			 u8 mode)
1049 {
1050 	struct tipc_aead *aead = NULL;
1051 	int rc = 0;
1052 
1053 	/* Initiate with the new user key */
1054 	rc = tipc_aead_init(&aead, ukey, mode);
1055 
1056 	/* Attach it to the crypto */
1057 	if (likely(!rc)) {
1058 		rc = tipc_crypto_key_attach(c, aead, 0);
1059 		if (rc < 0)
1060 			tipc_aead_free(&aead->rcu);
1061 	}
1062 
1063 	pr_info("%s(%s): key initiating, rc %d!\n",
1064 		(c->node) ? "RX" : "TX",
1065 		(c->node) ? tipc_node_get_id_str(c->node) :
1066 			    tipc_own_id_string(c->net),
1067 		rc);
1068 
1069 	return rc;
1070 }
1071 
1072 /**
1073  * tipc_crypto_key_attach - Attach a new AEAD key to TIPC crypto
1074  * @c: TIPC crypto to which the new AEAD key is attached
1075  * @aead: the new AEAD key pointer
1076  * @pos: desired slot in the crypto key array, = 0 if any!
1077  *
1078  * Return: new key id in case of success, otherwise: -EBUSY
1079  */
1080 static int tipc_crypto_key_attach(struct tipc_crypto *c,
1081 				  struct tipc_aead *aead, u8 pos)
1082 {
1083 	u8 new_pending, new_passive, new_key;
1084 	struct tipc_key key;
1085 	int rc = -EBUSY;
1086 
1087 	spin_lock_bh(&c->lock);
1088 	key = c->key;
1089 	if (key.active && key.passive)
1090 		goto exit;
1091 	if (key.passive && !tipc_aead_users(c->aead[key.passive]))
1092 		goto exit;
1093 	if (key.pending) {
1094 		if (pos)
1095 			goto exit;
1096 		if (tipc_aead_users(c->aead[key.pending]) > 0)
1097 			goto exit;
1098 		/* Replace it */
1099 		new_pending = key.pending;
1100 		new_passive = key.passive;
1101 		new_key = new_pending;
1102 	} else {
1103 		if (pos) {
1104 			if (key.active && pos != key_next(key.active)) {
1105 				new_pending = key.pending;
1106 				new_passive = pos;
1107 				new_key = new_passive;
1108 				goto attach;
1109 			} else if (!key.active && !key.passive) {
1110 				new_pending = pos;
1111 				new_passive = key.passive;
1112 				new_key = new_pending;
1113 				goto attach;
1114 			}
1115 		}
1116 		new_pending = key_next(key.active ?: key.passive);
1117 		new_passive = key.passive;
1118 		new_key = new_pending;
1119 	}
1120 
1121 attach:
1122 	aead->crypto = c;
1123 	tipc_crypto_key_set_state(c, new_passive, key.active, new_pending);
1124 	tipc_aead_rcu_replace(c->aead[new_key], aead, &c->lock);
1125 
1126 	c->working = 1;
1127 	c->timer1 = jiffies;
1128 	c->timer2 = jiffies;
1129 	rc = new_key;
1130 
1131 exit:
1132 	spin_unlock_bh(&c->lock);
1133 	return rc;
1134 }
1135 
1136 void tipc_crypto_key_flush(struct tipc_crypto *c)
1137 {
1138 	int k;
1139 
1140 	spin_lock_bh(&c->lock);
1141 	c->working = 0;
1142 	tipc_crypto_key_set_state(c, 0, 0, 0);
1143 	for (k = KEY_MIN; k <= KEY_MAX; k++)
1144 		tipc_crypto_key_detach(c->aead[k], &c->lock);
1145 	atomic_set(&c->peer_rx_active, 0);
1146 	atomic64_set(&c->sndnxt, 0);
1147 	spin_unlock_bh(&c->lock);
1148 }
1149 
1150 /**
1151  * tipc_crypto_key_try_align - Align RX keys if possible
1152  * @rx: RX crypto handle
1153  * @new_pending: new pending slot if aligned (= TX key from peer)
1154  *
1155  * Peer has used an unknown key slot, this only happens when peer has left and
1156  * rejoned, or we are newcomer.
1157  * That means, there must be no active key but a pending key at unaligned slot.
1158  * If so, we try to move the pending key to the new slot.
1159  * Note: A potential passive key can exist, it will be shifted correspondingly!
1160  *
1161  * Return: "true" if key is successfully aligned, otherwise "false"
1162  */
1163 static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending)
1164 {
1165 	struct tipc_aead *tmp1, *tmp2 = NULL;
1166 	struct tipc_key key;
1167 	bool aligned = false;
1168 	u8 new_passive = 0;
1169 	int x;
1170 
1171 	spin_lock(&rx->lock);
1172 	key = rx->key;
1173 	if (key.pending == new_pending) {
1174 		aligned = true;
1175 		goto exit;
1176 	}
1177 	if (key.active)
1178 		goto exit;
1179 	if (!key.pending)
1180 		goto exit;
1181 	if (tipc_aead_users(rx->aead[key.pending]) > 0)
1182 		goto exit;
1183 
1184 	/* Try to "isolate" this pending key first */
1185 	tmp1 = tipc_aead_rcu_ptr(rx->aead[key.pending], &rx->lock);
1186 	if (!refcount_dec_if_one(&tmp1->refcnt))
1187 		goto exit;
1188 	rcu_assign_pointer(rx->aead[key.pending], NULL);
1189 
1190 	/* Move passive key if any */
1191 	if (key.passive) {
1192 		tipc_aead_rcu_swap(rx->aead[key.passive], tmp2, &rx->lock);
1193 		x = (key.passive - key.pending + new_pending) % KEY_MAX;
1194 		new_passive = (x <= 0) ? x + KEY_MAX : x;
1195 	}
1196 
1197 	/* Re-allocate the key(s) */
1198 	tipc_crypto_key_set_state(rx, new_passive, 0, new_pending);
1199 	rcu_assign_pointer(rx->aead[new_pending], tmp1);
1200 	if (new_passive)
1201 		rcu_assign_pointer(rx->aead[new_passive], tmp2);
1202 	refcount_set(&tmp1->refcnt, 1);
1203 	aligned = true;
1204 	pr_info("RX(%s): key is aligned!\n", tipc_node_get_id_str(rx->node));
1205 
1206 exit:
1207 	spin_unlock(&rx->lock);
1208 	return aligned;
1209 }
1210 
1211 /**
1212  * tipc_crypto_key_pick_tx - Pick one TX key for message decryption
1213  * @tx: TX crypto handle
1214  * @rx: RX crypto handle (can be NULL)
1215  * @skb: the message skb which will be decrypted later
1216  *
1217  * This function looks up the existing TX keys and pick one which is suitable
1218  * for the message decryption, that must be a cluster key and not used before
1219  * on the same message (i.e. recursive).
1220  *
1221  * Return: the TX AEAD key handle in case of success, otherwise NULL
1222  */
1223 static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
1224 						 struct tipc_crypto *rx,
1225 						 struct sk_buff *skb)
1226 {
1227 	struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(skb);
1228 	struct tipc_aead *aead = NULL;
1229 	struct tipc_key key = tx->key;
1230 	u8 k, i = 0;
1231 
1232 	/* Initialize data if not yet */
1233 	if (!skb_cb->tx_clone_deferred) {
1234 		skb_cb->tx_clone_deferred = 1;
1235 		memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1236 	}
1237 
1238 	skb_cb->tx_clone_ctx.rx = rx;
1239 	if (++skb_cb->tx_clone_ctx.recurs > 2)
1240 		return NULL;
1241 
1242 	/* Pick one TX key */
1243 	spin_lock(&tx->lock);
1244 	do {
1245 		k = (i == 0) ? key.pending :
1246 			((i == 1) ? key.active : key.passive);
1247 		if (!k)
1248 			continue;
1249 		aead = tipc_aead_rcu_ptr(tx->aead[k], &tx->lock);
1250 		if (!aead)
1251 			continue;
1252 		if (aead->mode != CLUSTER_KEY ||
1253 		    aead == skb_cb->tx_clone_ctx.last) {
1254 			aead = NULL;
1255 			continue;
1256 		}
1257 		/* Ok, found one cluster key */
1258 		skb_cb->tx_clone_ctx.last = aead;
1259 		WARN_ON(skb->next);
1260 		skb->next = skb_clone(skb, GFP_ATOMIC);
1261 		if (unlikely(!skb->next))
1262 			pr_warn("Failed to clone skb for next round if any\n");
1263 		WARN_ON(!refcount_inc_not_zero(&aead->refcnt));
1264 		break;
1265 	} while (++i < 3);
1266 	spin_unlock(&tx->lock);
1267 
1268 	return aead;
1269 }
1270 
1271 /**
1272  * tipc_crypto_key_synch: Synch own key data according to peer key status
1273  * @rx: RX crypto handle
1274  * @new_rx_active: latest RX active key from peer
1275  * @hdr: TIPCv2 message
1276  *
1277  * This function updates the peer node related data as the peer RX active key
1278  * has changed, so the number of TX keys' users on this node are increased and
1279  * decreased correspondingly.
1280  *
1281  * The "per-peer" sndnxt is also reset when the peer key has switched.
1282  */
1283 static void tipc_crypto_key_synch(struct tipc_crypto *rx, u8 new_rx_active,
1284 				  struct tipc_msg *hdr)
1285 {
1286 	struct net *net = rx->net;
1287 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1288 	u8 cur_rx_active;
1289 
1290 	/* TX might be even not ready yet */
1291 	if (unlikely(!tx->key.active && !tx->key.pending))
1292 		return;
1293 
1294 	cur_rx_active = atomic_read(&rx->peer_rx_active);
1295 	if (likely(cur_rx_active == new_rx_active))
1296 		return;
1297 
1298 	/* Make sure this message destined for this node */
1299 	if (unlikely(msg_short(hdr) ||
1300 		     msg_destnode(hdr) != tipc_own_addr(net)))
1301 		return;
1302 
1303 	/* Peer RX active key has changed, try to update owns' & TX users */
1304 	if (atomic_cmpxchg(&rx->peer_rx_active,
1305 			   cur_rx_active,
1306 			   new_rx_active) == cur_rx_active) {
1307 		if (new_rx_active)
1308 			tipc_aead_users_inc(tx->aead[new_rx_active], INT_MAX);
1309 		if (cur_rx_active)
1310 			tipc_aead_users_dec(tx->aead[cur_rx_active], 0);
1311 
1312 		atomic64_set(&rx->sndnxt, 0);
1313 		/* Mark the point TX key users changed */
1314 		tx->timer1 = jiffies;
1315 
1316 #ifdef TIPC_CRYPTO_DEBUG
1317 		pr_info("TX(%s): key users changed %d-- %d++, peer RX(%s)\n",
1318 			tipc_own_id_string(net), cur_rx_active,
1319 			new_rx_active, tipc_node_get_id_str(rx->node));
1320 #endif
1321 	}
1322 }
1323 
1324 static int tipc_crypto_key_revoke(struct net *net, u8 tx_key)
1325 {
1326 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1327 	struct tipc_key key;
1328 
1329 	spin_lock(&tx->lock);
1330 	key = tx->key;
1331 	WARN_ON(!key.active || tx_key != key.active);
1332 
1333 	/* Free the active key */
1334 	tipc_crypto_key_set_state(tx, key.passive, 0, key.pending);
1335 	tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
1336 	spin_unlock(&tx->lock);
1337 
1338 	pr_warn("TX(%s): key is revoked!\n", tipc_own_id_string(net));
1339 	return -EKEYREVOKED;
1340 }
1341 
1342 int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
1343 		      struct tipc_node *node)
1344 {
1345 	struct tipc_crypto *c;
1346 
1347 	if (*crypto)
1348 		return -EEXIST;
1349 
1350 	/* Allocate crypto */
1351 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
1352 	if (!c)
1353 		return -ENOMEM;
1354 
1355 	/* Allocate statistic structure */
1356 	c->stats = alloc_percpu_gfp(struct tipc_crypto_stats, GFP_ATOMIC);
1357 	if (!c->stats) {
1358 		kzfree(c);
1359 		return -ENOMEM;
1360 	}
1361 
1362 	c->working = 0;
1363 	c->net = net;
1364 	c->node = node;
1365 	tipc_crypto_key_set_state(c, 0, 0, 0);
1366 	atomic_set(&c->peer_rx_active, 0);
1367 	atomic64_set(&c->sndnxt, 0);
1368 	c->timer1 = jiffies;
1369 	c->timer2 = jiffies;
1370 	spin_lock_init(&c->lock);
1371 	*crypto = c;
1372 
1373 	return 0;
1374 }
1375 
1376 void tipc_crypto_stop(struct tipc_crypto **crypto)
1377 {
1378 	struct tipc_crypto *c, *tx, *rx;
1379 	bool is_rx;
1380 	u8 k;
1381 
1382 	if (!*crypto)
1383 		return;
1384 
1385 	rcu_read_lock();
1386 	/* RX stopping? => decrease TX key users if any */
1387 	is_rx = !!((*crypto)->node);
1388 	if (is_rx) {
1389 		rx = *crypto;
1390 		tx = tipc_net(rx->net)->crypto_tx;
1391 		k = atomic_read(&rx->peer_rx_active);
1392 		if (k) {
1393 			tipc_aead_users_dec(tx->aead[k], 0);
1394 			/* Mark the point TX key users changed */
1395 			tx->timer1 = jiffies;
1396 		}
1397 	}
1398 
1399 	/* Release AEAD keys */
1400 	c = *crypto;
1401 	for (k = KEY_MIN; k <= KEY_MAX; k++)
1402 		tipc_aead_put(rcu_dereference(c->aead[k]));
1403 	rcu_read_unlock();
1404 
1405 	pr_warn("%s(%s) has been purged, node left!\n",
1406 		(is_rx) ? "RX" : "TX",
1407 		(is_rx) ? tipc_node_get_id_str((*crypto)->node) :
1408 			  tipc_own_id_string((*crypto)->net));
1409 
1410 	/* Free this crypto statistics */
1411 	free_percpu(c->stats);
1412 
1413 	*crypto = NULL;
1414 	kzfree(c);
1415 }
1416 
1417 void tipc_crypto_timeout(struct tipc_crypto *rx)
1418 {
1419 	struct tipc_net *tn = tipc_net(rx->net);
1420 	struct tipc_crypto *tx = tn->crypto_tx;
1421 	struct tipc_key key;
1422 	u8 new_pending, new_passive;
1423 	int cmd;
1424 
1425 	/* TX key activating:
1426 	 * The pending key (users > 0) -> active
1427 	 * The active key if any (users == 0) -> free
1428 	 */
1429 	spin_lock(&tx->lock);
1430 	key = tx->key;
1431 	if (key.active && tipc_aead_users(tx->aead[key.active]) > 0)
1432 		goto s1;
1433 	if (!key.pending || tipc_aead_users(tx->aead[key.pending]) <= 0)
1434 		goto s1;
1435 	if (time_before(jiffies, tx->timer1 + TIPC_TX_LASTING_LIM))
1436 		goto s1;
1437 
1438 	tipc_crypto_key_set_state(tx, key.passive, key.pending, 0);
1439 	if (key.active)
1440 		tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
1441 	this_cpu_inc(tx->stats->stat[STAT_SWITCHES]);
1442 	pr_info("TX(%s): key %d is activated!\n", tipc_own_id_string(tx->net),
1443 		key.pending);
1444 
1445 s1:
1446 	spin_unlock(&tx->lock);
1447 
1448 	/* RX key activating:
1449 	 * The pending key (users > 0) -> active
1450 	 * The active key if any -> passive, freed later
1451 	 */
1452 	spin_lock(&rx->lock);
1453 	key = rx->key;
1454 	if (!key.pending || tipc_aead_users(rx->aead[key.pending]) <= 0)
1455 		goto s2;
1456 
1457 	new_pending = (key.passive &&
1458 		       !tipc_aead_users(rx->aead[key.passive])) ?
1459 				       key.passive : 0;
1460 	new_passive = (key.active) ?: ((new_pending) ? 0 : key.passive);
1461 	tipc_crypto_key_set_state(rx, new_passive, key.pending, new_pending);
1462 	this_cpu_inc(rx->stats->stat[STAT_SWITCHES]);
1463 	pr_info("RX(%s): key %d is activated!\n",
1464 		tipc_node_get_id_str(rx->node),	key.pending);
1465 	goto s5;
1466 
1467 s2:
1468 	/* RX key "faulty" switching:
1469 	 * The faulty pending key (users < -30) -> passive
1470 	 * The passive key (users = 0) -> pending
1471 	 * Note: This only happens after RX deactivated - s3!
1472 	 */
1473 	key = rx->key;
1474 	if (!key.pending || tipc_aead_users(rx->aead[key.pending]) > -30)
1475 		goto s3;
1476 	if (!key.passive || tipc_aead_users(rx->aead[key.passive]) != 0)
1477 		goto s3;
1478 
1479 	new_pending = key.passive;
1480 	new_passive = key.pending;
1481 	tipc_crypto_key_set_state(rx, new_passive, key.active, new_pending);
1482 	goto s5;
1483 
1484 s3:
1485 	/* RX key deactivating:
1486 	 * The passive key if any -> pending
1487 	 * The active key -> passive (users = 0) / pending
1488 	 * The pending key if any -> passive (users = 0)
1489 	 */
1490 	key = rx->key;
1491 	if (!key.active)
1492 		goto s4;
1493 	if (time_before(jiffies, rx->timer1 + TIPC_RX_ACTIVE_LIM))
1494 		goto s4;
1495 
1496 	new_pending = (key.passive) ?: key.active;
1497 	new_passive = (key.passive) ? key.active : key.pending;
1498 	tipc_aead_users_set(rx->aead[new_pending], 0);
1499 	if (new_passive)
1500 		tipc_aead_users_set(rx->aead[new_passive], 0);
1501 	tipc_crypto_key_set_state(rx, new_passive, 0, new_pending);
1502 	pr_info("RX(%s): key %d is deactivated!\n",
1503 		tipc_node_get_id_str(rx->node), key.active);
1504 	goto s5;
1505 
1506 s4:
1507 	/* RX key passive -> freed: */
1508 	key = rx->key;
1509 	if (!key.passive || !tipc_aead_users(rx->aead[key.passive]))
1510 		goto s5;
1511 	if (time_before(jiffies, rx->timer2 + TIPC_RX_PASSIVE_LIM))
1512 		goto s5;
1513 
1514 	tipc_crypto_key_set_state(rx, 0, key.active, key.pending);
1515 	tipc_crypto_key_detach(rx->aead[key.passive], &rx->lock);
1516 	pr_info("RX(%s): key %d is freed!\n", tipc_node_get_id_str(rx->node),
1517 		key.passive);
1518 
1519 s5:
1520 	spin_unlock(&rx->lock);
1521 
1522 	/* Limit max_tfms & do debug commands if needed */
1523 	if (likely(sysctl_tipc_max_tfms <= TIPC_MAX_TFMS_LIM))
1524 		return;
1525 
1526 	cmd = sysctl_tipc_max_tfms;
1527 	sysctl_tipc_max_tfms = TIPC_MAX_TFMS_DEF;
1528 	tipc_crypto_do_cmd(rx->net, cmd);
1529 }
1530 
1531 /**
1532  * tipc_crypto_xmit - Build & encrypt TIPC message for xmit
1533  * @net: struct net
1534  * @skb: input/output message skb pointer
1535  * @b: bearer used for xmit later
1536  * @dst: destination media address
1537  * @__dnode: destination node for reference if any
1538  *
1539  * First, build an encryption message header on the top of the message, then
1540  * encrypt the original TIPC message by using the active or pending TX key.
1541  * If the encryption is successful, the encrypted skb is returned directly or
1542  * via the callback.
1543  * Otherwise, the skb is freed!
1544  *
1545  * Return:
1546  * 0                   : the encryption has succeeded (or no encryption)
1547  * -EINPROGRESS/-EBUSY : the encryption is ongoing, a callback will be made
1548  * -ENOKEK             : the encryption has failed due to no key
1549  * -EKEYREVOKED        : the encryption has failed due to key revoked
1550  * -ENOMEM             : the encryption has failed due to no memory
1551  * < 0                 : the encryption has failed due to other reasons
1552  */
1553 int tipc_crypto_xmit(struct net *net, struct sk_buff **skb,
1554 		     struct tipc_bearer *b, struct tipc_media_addr *dst,
1555 		     struct tipc_node *__dnode)
1556 {
1557 	struct tipc_crypto *__rx = tipc_node_crypto_rx(__dnode);
1558 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1559 	struct tipc_crypto_stats __percpu *stats = tx->stats;
1560 	struct tipc_key key = tx->key;
1561 	struct tipc_aead *aead = NULL;
1562 	struct sk_buff *probe;
1563 	int rc = -ENOKEY;
1564 	u8 tx_key;
1565 
1566 	/* No encryption? */
1567 	if (!tx->working)
1568 		return 0;
1569 
1570 	/* Try with the pending key if available and:
1571 	 * 1) This is the only choice (i.e. no active key) or;
1572 	 * 2) Peer has switched to this key (unicast only) or;
1573 	 * 3) It is time to do a pending key probe;
1574 	 */
1575 	if (unlikely(key.pending)) {
1576 		tx_key = key.pending;
1577 		if (!key.active)
1578 			goto encrypt;
1579 		if (__rx && atomic_read(&__rx->peer_rx_active) == tx_key)
1580 			goto encrypt;
1581 		if (TIPC_SKB_CB(*skb)->probe)
1582 			goto encrypt;
1583 		if (!__rx &&
1584 		    time_after(jiffies, tx->timer2 + TIPC_TX_PROBE_LIM)) {
1585 			tx->timer2 = jiffies;
1586 			probe = skb_clone(*skb, GFP_ATOMIC);
1587 			if (probe) {
1588 				TIPC_SKB_CB(probe)->probe = 1;
1589 				tipc_crypto_xmit(net, &probe, b, dst, __dnode);
1590 				if (probe)
1591 					b->media->send_msg(net, probe, b, dst);
1592 			}
1593 		}
1594 	}
1595 	/* Else, use the active key if any */
1596 	if (likely(key.active)) {
1597 		tx_key = key.active;
1598 		goto encrypt;
1599 	}
1600 	goto exit;
1601 
1602 encrypt:
1603 	aead = tipc_aead_get(tx->aead[tx_key]);
1604 	if (unlikely(!aead))
1605 		goto exit;
1606 	rc = tipc_ehdr_build(net, aead, tx_key, *skb, __rx);
1607 	if (likely(rc > 0))
1608 		rc = tipc_aead_encrypt(aead, *skb, b, dst, __dnode);
1609 
1610 exit:
1611 	switch (rc) {
1612 	case 0:
1613 		this_cpu_inc(stats->stat[STAT_OK]);
1614 		break;
1615 	case -EINPROGRESS:
1616 	case -EBUSY:
1617 		this_cpu_inc(stats->stat[STAT_ASYNC]);
1618 		*skb = NULL;
1619 		return rc;
1620 	default:
1621 		this_cpu_inc(stats->stat[STAT_NOK]);
1622 		if (rc == -ENOKEY)
1623 			this_cpu_inc(stats->stat[STAT_NOKEYS]);
1624 		else if (rc == -EKEYREVOKED)
1625 			this_cpu_inc(stats->stat[STAT_BADKEYS]);
1626 		kfree_skb(*skb);
1627 		*skb = NULL;
1628 		break;
1629 	}
1630 
1631 	tipc_aead_put(aead);
1632 	return rc;
1633 }
1634 
1635 /**
1636  * tipc_crypto_rcv - Decrypt an encrypted TIPC message from peer
1637  * @net: struct net
1638  * @rx: RX crypto handle
1639  * @skb: input/output message skb pointer
1640  * @b: bearer where the message has been received
1641  *
1642  * If the decryption is successful, the decrypted skb is returned directly or
1643  * as the callback, the encryption header and auth tag will be trimed out
1644  * before forwarding to tipc_rcv() via the tipc_crypto_rcv_complete().
1645  * Otherwise, the skb will be freed!
1646  * Note: RX key(s) can be re-aligned, or in case of no key suitable, TX
1647  * cluster key(s) can be taken for decryption (- recursive).
1648  *
1649  * Return:
1650  * 0                   : the decryption has successfully completed
1651  * -EINPROGRESS/-EBUSY : the decryption is ongoing, a callback will be made
1652  * -ENOKEY             : the decryption has failed due to no key
1653  * -EBADMSG            : the decryption has failed due to bad message
1654  * -ENOMEM             : the decryption has failed due to no memory
1655  * < 0                 : the decryption has failed due to other reasons
1656  */
1657 int tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx,
1658 		    struct sk_buff **skb, struct tipc_bearer *b)
1659 {
1660 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1661 	struct tipc_crypto_stats __percpu *stats;
1662 	struct tipc_aead *aead = NULL;
1663 	struct tipc_key key;
1664 	int rc = -ENOKEY;
1665 	u8 tx_key = 0;
1666 
1667 	/* New peer?
1668 	 * Let's try with TX key (i.e. cluster mode) & verify the skb first!
1669 	 */
1670 	if (unlikely(!rx))
1671 		goto pick_tx;
1672 
1673 	/* Pick RX key according to TX key, three cases are possible:
1674 	 * 1) The current active key (likely) or;
1675 	 * 2) The pending (new or deactivated) key (if any) or;
1676 	 * 3) The passive or old active key (i.e. users > 0);
1677 	 */
1678 	tx_key = ((struct tipc_ehdr *)(*skb)->data)->tx_key;
1679 	key = rx->key;
1680 	if (likely(tx_key == key.active))
1681 		goto decrypt;
1682 	if (tx_key == key.pending)
1683 		goto decrypt;
1684 	if (tx_key == key.passive) {
1685 		rx->timer2 = jiffies;
1686 		if (tipc_aead_users(rx->aead[key.passive]) > 0)
1687 			goto decrypt;
1688 	}
1689 
1690 	/* Unknown key, let's try to align RX key(s) */
1691 	if (tipc_crypto_key_try_align(rx, tx_key))
1692 		goto decrypt;
1693 
1694 pick_tx:
1695 	/* No key suitable? Try to pick one from TX... */
1696 	aead = tipc_crypto_key_pick_tx(tx, rx, *skb);
1697 	if (aead)
1698 		goto decrypt;
1699 	goto exit;
1700 
1701 decrypt:
1702 	rcu_read_lock();
1703 	if (!aead)
1704 		aead = tipc_aead_get(rx->aead[tx_key]);
1705 	rc = tipc_aead_decrypt(net, aead, *skb, b);
1706 	rcu_read_unlock();
1707 
1708 exit:
1709 	stats = ((rx) ?: tx)->stats;
1710 	switch (rc) {
1711 	case 0:
1712 		this_cpu_inc(stats->stat[STAT_OK]);
1713 		break;
1714 	case -EINPROGRESS:
1715 	case -EBUSY:
1716 		this_cpu_inc(stats->stat[STAT_ASYNC]);
1717 		*skb = NULL;
1718 		return rc;
1719 	default:
1720 		this_cpu_inc(stats->stat[STAT_NOK]);
1721 		if (rc == -ENOKEY) {
1722 			kfree_skb(*skb);
1723 			*skb = NULL;
1724 			if (rx)
1725 				tipc_node_put(rx->node);
1726 			this_cpu_inc(stats->stat[STAT_NOKEYS]);
1727 			return rc;
1728 		} else if (rc == -EBADMSG) {
1729 			this_cpu_inc(stats->stat[STAT_BADMSGS]);
1730 		}
1731 		break;
1732 	}
1733 
1734 	tipc_crypto_rcv_complete(net, aead, b, skb, rc);
1735 	return rc;
1736 }
1737 
1738 static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
1739 				     struct tipc_bearer *b,
1740 				     struct sk_buff **skb, int err)
1741 {
1742 	struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(*skb);
1743 	struct tipc_crypto *rx = aead->crypto;
1744 	struct tipc_aead *tmp = NULL;
1745 	struct tipc_ehdr *ehdr;
1746 	struct tipc_node *n;
1747 	u8 rx_key_active;
1748 	bool destined;
1749 
1750 	/* Is this completed by TX? */
1751 	if (unlikely(!rx->node)) {
1752 		rx = skb_cb->tx_clone_ctx.rx;
1753 #ifdef TIPC_CRYPTO_DEBUG
1754 		pr_info("TX->RX(%s): err %d, aead %p, skb->next %p, flags %x\n",
1755 			(rx) ? tipc_node_get_id_str(rx->node) : "-", err, aead,
1756 			(*skb)->next, skb_cb->flags);
1757 		pr_info("skb_cb [recurs %d, last %p], tx->aead [%p %p %p]\n",
1758 			skb_cb->tx_clone_ctx.recurs, skb_cb->tx_clone_ctx.last,
1759 			aead->crypto->aead[1], aead->crypto->aead[2],
1760 			aead->crypto->aead[3]);
1761 #endif
1762 		if (unlikely(err)) {
1763 			if (err == -EBADMSG && (*skb)->next)
1764 				tipc_rcv(net, (*skb)->next, b);
1765 			goto free_skb;
1766 		}
1767 
1768 		if (likely((*skb)->next)) {
1769 			kfree_skb((*skb)->next);
1770 			(*skb)->next = NULL;
1771 		}
1772 		ehdr = (struct tipc_ehdr *)(*skb)->data;
1773 		if (!rx) {
1774 			WARN_ON(ehdr->user != LINK_CONFIG);
1775 			n = tipc_node_create(net, 0, ehdr->id, 0xffffu, 0,
1776 					     true);
1777 			rx = tipc_node_crypto_rx(n);
1778 			if (unlikely(!rx))
1779 				goto free_skb;
1780 		}
1781 
1782 		/* Skip cloning this time as we had a RX pending key */
1783 		if (rx->key.pending)
1784 			goto rcv;
1785 		if (tipc_aead_clone(&tmp, aead) < 0)
1786 			goto rcv;
1787 		if (tipc_crypto_key_attach(rx, tmp, ehdr->tx_key) < 0) {
1788 			tipc_aead_free(&tmp->rcu);
1789 			goto rcv;
1790 		}
1791 		tipc_aead_put(aead);
1792 		aead = tipc_aead_get(tmp);
1793 	}
1794 
1795 	if (unlikely(err)) {
1796 		tipc_aead_users_dec(aead, INT_MIN);
1797 		goto free_skb;
1798 	}
1799 
1800 	/* Set the RX key's user */
1801 	tipc_aead_users_set(aead, 1);
1802 
1803 rcv:
1804 	/* Mark this point, RX works */
1805 	rx->timer1 = jiffies;
1806 
1807 	/* Remove ehdr & auth. tag prior to tipc_rcv() */
1808 	ehdr = (struct tipc_ehdr *)(*skb)->data;
1809 	destined = ehdr->destined;
1810 	rx_key_active = ehdr->rx_key_active;
1811 	skb_pull(*skb, tipc_ehdr_size(ehdr));
1812 	pskb_trim(*skb, (*skb)->len - aead->authsize);
1813 
1814 	/* Validate TIPCv2 message */
1815 	if (unlikely(!tipc_msg_validate(skb))) {
1816 		pr_err_ratelimited("Packet dropped after decryption!\n");
1817 		goto free_skb;
1818 	}
1819 
1820 	/* Update peer RX active key & TX users */
1821 	if (destined)
1822 		tipc_crypto_key_synch(rx, rx_key_active, buf_msg(*skb));
1823 
1824 	/* Mark skb decrypted */
1825 	skb_cb->decrypted = 1;
1826 
1827 	/* Clear clone cxt if any */
1828 	if (likely(!skb_cb->tx_clone_deferred))
1829 		goto exit;
1830 	skb_cb->tx_clone_deferred = 0;
1831 	memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1832 	goto exit;
1833 
1834 free_skb:
1835 	kfree_skb(*skb);
1836 	*skb = NULL;
1837 
1838 exit:
1839 	tipc_aead_put(aead);
1840 	if (rx)
1841 		tipc_node_put(rx->node);
1842 }
1843 
1844 static void tipc_crypto_do_cmd(struct net *net, int cmd)
1845 {
1846 	struct tipc_net *tn = tipc_net(net);
1847 	struct tipc_crypto *tx = tn->crypto_tx, *rx;
1848 	struct list_head *p;
1849 	unsigned int stat;
1850 	int i, j, cpu;
1851 	char buf[200];
1852 
1853 	/* Currently only one command is supported */
1854 	switch (cmd) {
1855 	case 0xfff1:
1856 		goto print_stats;
1857 	default:
1858 		return;
1859 	}
1860 
1861 print_stats:
1862 	/* Print a header */
1863 	pr_info("\n=============== TIPC Crypto Statistics ===============\n\n");
1864 
1865 	/* Print key status */
1866 	pr_info("Key status:\n");
1867 	pr_info("TX(%7.7s)\n%s", tipc_own_id_string(net),
1868 		tipc_crypto_key_dump(tx, buf));
1869 
1870 	rcu_read_lock();
1871 	for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
1872 		rx = tipc_node_crypto_rx_by_list(p);
1873 		pr_info("RX(%7.7s)\n%s", tipc_node_get_id_str(rx->node),
1874 			tipc_crypto_key_dump(rx, buf));
1875 	}
1876 	rcu_read_unlock();
1877 
1878 	/* Print crypto statistics */
1879 	for (i = 0, j = 0; i < MAX_STATS; i++)
1880 		j += scnprintf(buf + j, 200 - j, "|%11s ", hstats[i]);
1881 	pr_info("\nCounter     %s", buf);
1882 
1883 	memset(buf, '-', 115);
1884 	buf[115] = '\0';
1885 	pr_info("%s\n", buf);
1886 
1887 	j = scnprintf(buf, 200, "TX(%7.7s) ", tipc_own_id_string(net));
1888 	for_each_possible_cpu(cpu) {
1889 		for (i = 0; i < MAX_STATS; i++) {
1890 			stat = per_cpu_ptr(tx->stats, cpu)->stat[i];
1891 			j += scnprintf(buf + j, 200 - j, "|%11d ", stat);
1892 		}
1893 		pr_info("%s", buf);
1894 		j = scnprintf(buf, 200, "%12s", " ");
1895 	}
1896 
1897 	rcu_read_lock();
1898 	for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
1899 		rx = tipc_node_crypto_rx_by_list(p);
1900 		j = scnprintf(buf, 200, "RX(%7.7s) ",
1901 			      tipc_node_get_id_str(rx->node));
1902 		for_each_possible_cpu(cpu) {
1903 			for (i = 0; i < MAX_STATS; i++) {
1904 				stat = per_cpu_ptr(rx->stats, cpu)->stat[i];
1905 				j += scnprintf(buf + j, 200 - j, "|%11d ",
1906 					       stat);
1907 			}
1908 			pr_info("%s", buf);
1909 			j = scnprintf(buf, 200, "%12s", " ");
1910 		}
1911 	}
1912 	rcu_read_unlock();
1913 
1914 	pr_info("\n======================== Done ========================\n");
1915 }
1916 
1917 static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf)
1918 {
1919 	struct tipc_key key = c->key;
1920 	struct tipc_aead *aead;
1921 	int k, i = 0;
1922 	char *s;
1923 
1924 	for (k = KEY_MIN; k <= KEY_MAX; k++) {
1925 		if (k == key.passive)
1926 			s = "PAS";
1927 		else if (k == key.active)
1928 			s = "ACT";
1929 		else if (k == key.pending)
1930 			s = "PEN";
1931 		else
1932 			s = "-";
1933 		i += scnprintf(buf + i, 200 - i, "\tKey%d: %s", k, s);
1934 
1935 		rcu_read_lock();
1936 		aead = rcu_dereference(c->aead[k]);
1937 		if (aead)
1938 			i += scnprintf(buf + i, 200 - i,
1939 				       "{\"%s...\", \"%s\"}/%d:%d",
1940 				       aead->hint,
1941 				       (aead->mode == CLUSTER_KEY) ? "c" : "p",
1942 				       atomic_read(&aead->users),
1943 				       refcount_read(&aead->refcnt));
1944 		rcu_read_unlock();
1945 		i += scnprintf(buf + i, 200 - i, "\n");
1946 	}
1947 
1948 	if (c->node)
1949 		i += scnprintf(buf + i, 200 - i, "\tPeer RX active: %d\n",
1950 			       atomic_read(&c->peer_rx_active));
1951 
1952 	return buf;
1953 }
1954 
1955 #ifdef TIPC_CRYPTO_DEBUG
1956 static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
1957 				  char *buf)
1958 {
1959 	struct tipc_key *key = &old;
1960 	int k, i = 0;
1961 	char *s;
1962 
1963 	/* Output format: "[%s %s %s] -> [%s %s %s]", max len = 32 */
1964 again:
1965 	i += scnprintf(buf + i, 32 - i, "[");
1966 	for (k = KEY_MIN; k <= KEY_MAX; k++) {
1967 		if (k == key->passive)
1968 			s = "pas";
1969 		else if (k == key->active)
1970 			s = "act";
1971 		else if (k == key->pending)
1972 			s = "pen";
1973 		else
1974 			s = "-";
1975 		i += scnprintf(buf + i, 32 - i,
1976 			       (k != KEY_MAX) ? "%s " : "%s", s);
1977 	}
1978 	if (key != &new) {
1979 		i += scnprintf(buf + i, 32 - i, "] -> ");
1980 		key = &new;
1981 		goto again;
1982 	}
1983 	i += scnprintf(buf + i, 32 - i, "]");
1984 	return buf;
1985 }
1986 #endif
1987