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