1 // SPDX-License-Identifier: GPL-2.0
3 * net/tipc/crypto.c: TIPC crypto for key handling & packet en/decryption
5 * Copyright (c) 2019, Ericsson AB
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
37 #include <crypto/aead.h>
38 #include <crypto/aes.h>
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
61 * TIPC Crypto statistics
69 STAT_BADKEYS
, /* tx only */
70 STAT_BADMSGS
= STAT_BADKEYS
, /* rx only */
77 /* TIPC crypto statistics' header */
78 static const char *hstats
[MAX_STATS
] = {"ok", "nok", "async", "async_ok",
79 "async_nok", "badmsgs", "nokeys",
82 /* Max TFMs number per key */
83 int sysctl_tipc_max_tfms __read_mostly
= TIPC_MAX_TFMS_DEF
;
86 * struct tipc_key - TIPC keys' status indicator
89 * +-----+-----+-----+-----+-----+-----+-----+-----+
90 * key: | (reserved)|passive idx| active idx|pending idx|
91 * +-----+-----+-----+-----+-----+-----+-----+-----+
95 #define KEY_MASK ((1 << KEY_BITS) - 1)
98 #if defined(__LITTLE_ENDIAN_BITFIELD)
101 passive
:2, /* rx only */
103 #elif defined(__BIG_ENDIAN_BITFIELD)
105 passive
:2, /* rx only */
109 #error "Please fix <asm/byteorder.h>"
117 * struct tipc_tfm - TIPC TFM structure to form a list of TFMs
120 struct crypto_aead
*tfm
;
121 struct list_head list
;
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
139 #define TIPC_AEAD_HINT_LEN (5)
140 struct tipc_tfm
* __percpu
*tfm_entry
;
141 struct tipc_crypto
*crypto
;
142 struct tipc_aead
*cloned
;
147 char hint
[TIPC_AEAD_HINT_LEN
+ 1];
150 atomic64_t seqno ____cacheline_aligned
;
151 refcount_t refcnt ____cacheline_aligned
;
153 } ____cacheline_aligned
;
156 * struct tipc_crypto_stats - TIPC Crypto statistics
158 struct tipc_crypto_stats
{
159 unsigned int stat
[MAX_STATS
];
163 * struct tipc_crypto - TIPC TX/RX crypto structure
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
178 struct tipc_node
*node
;
179 struct tipc_aead __rcu
*aead
[KEY_MAX
+ 1]; /* key[0] is UNUSED */
180 atomic_t peer_rx_active
;
183 struct tipc_crypto_stats __percpu
*stats
;
185 atomic64_t sndnxt ____cacheline_aligned
;
186 unsigned long timer1
;
187 unsigned long timer2
;
188 spinlock_t lock
; /* crypto lock */
190 } ____cacheline_aligned
;
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
;
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
;
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
,
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
,
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,
255 #define key_next(cur) ((cur) % KEY_MAX + 1)
257 #define tipc_aead_rcu_ptr(rcu_ptr, lock) \
258 rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock))
260 #define tipc_aead_rcu_swap(rcu_ptr, ptr, lock) \
261 rcu_swap_protected((rcu_ptr), (ptr), lockdep_is_held(lock))
263 #define tipc_aead_rcu_replace(rcu_ptr, ptr, lock) \
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); \
271 #define tipc_crypto_key_detach(rcu_ptr, lock) \
272 tipc_aead_rcu_replace((rcu_ptr), NULL, lock)
275 * tipc_aead_key_validate - Validate a AEAD user key
277 int tipc_aead_key_validate(struct tipc_aead_key
*ukey
)
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
);
287 /* Currently, we only support the "gcm(aes)" cipher algorithm */
288 if (strcmp(ukey
->alg_name
, "gcm(aes)"))
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
))
301 static struct tipc_aead
*tipc_aead_get(struct tipc_aead __rcu
*aead
)
303 struct tipc_aead
*tmp
;
306 tmp
= rcu_dereference(aead
);
307 if (unlikely(!tmp
|| !refcount_inc_not_zero(&tmp
->refcnt
)))
314 static inline void tipc_aead_put(struct tipc_aead
*aead
)
316 if (aead
&& refcount_dec_and_test(&aead
->refcnt
))
317 call_rcu(&aead
->rcu
, tipc_aead_free
);
321 * tipc_aead_free - Release AEAD key incl. all the TFMs in the list
322 * @rp: rcu head pointer
324 static void tipc_aead_free(struct rcu_head
*rp
)
326 struct tipc_aead
*aead
= container_of(rp
, struct tipc_aead
, rcu
);
327 struct tipc_tfm
*tfm_entry
, *head
, *tmp
;
330 tipc_aead_put(aead
->cloned
);
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
);
339 crypto_free_aead(head
->tfm
);
340 list_del(&head
->list
);
343 free_percpu(aead
->tfm_entry
);
347 static int tipc_aead_users(struct tipc_aead __rcu
*aead
)
349 struct tipc_aead
*tmp
;
353 tmp
= rcu_dereference(aead
);
355 users
= atomic_read(&tmp
->users
);
361 static void tipc_aead_users_inc(struct tipc_aead __rcu
*aead
, int lim
)
363 struct tipc_aead
*tmp
;
366 tmp
= rcu_dereference(aead
);
368 atomic_add_unless(&tmp
->users
, 1, lim
);
372 static void tipc_aead_users_dec(struct tipc_aead __rcu
*aead
, int lim
)
374 struct tipc_aead
*tmp
;
377 tmp
= rcu_dereference(aead
);
379 atomic_add_unless(&rcu_dereference(aead
)->users
, -1, lim
);
383 static void tipc_aead_users_set(struct tipc_aead __rcu
*aead
, int val
)
385 struct tipc_aead
*tmp
;
389 tmp
= rcu_dereference(aead
);
392 cur
= atomic_read(&tmp
->users
);
395 } while (atomic_cmpxchg(&tmp
->users
, cur
, val
) != cur
);
401 * tipc_aead_tfm_next - Move TFM entry to the next one in list and return it
403 static struct crypto_aead
*tipc_aead_tfm_next(struct tipc_aead
*aead
)
405 struct tipc_tfm
**tfm_entry
= this_cpu_ptr(aead
->tfm_entry
);
407 *tfm_entry
= list_next_entry(*tfm_entry
, list
);
408 return (*tfm_entry
)->tfm
;
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
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.
422 * Return: 0 if the initiation is successful, otherwise: < 0
424 static int tipc_aead_init(struct tipc_aead
**aead
, struct tipc_aead_key
*ukey
,
427 struct tipc_tfm
*tfm_entry
, *head
;
428 struct crypto_aead
*tfm
;
429 struct tipc_aead
*tmp
;
430 int keylen
, err
, cpu
;
436 /* Allocate a new AEAD */
437 tmp
= kzalloc(sizeof(*tmp
), GFP_ATOMIC
);
441 /* The key consists of two parts: [AES-KEY][SALT] */
442 keylen
= ukey
->keylen
- TIPC_AES_GCM_SALT_SIZE
;
444 /* Allocate per-cpu TFM entry pointer */
445 tmp
->tfm_entry
= alloc_percpu(struct tipc_tfm
*);
446 if (!tmp
->tfm_entry
) {
451 /* Make a list of TFMs with the user key data */
453 tfm
= crypto_alloc_aead(ukey
->alg_name
, 0, 0);
459 if (unlikely(!tfm_cnt
&&
460 crypto_aead_ivsize(tfm
) != TIPC_AES_GCM_IV_SIZE
)) {
461 crypto_free_aead(tfm
);
466 err
= crypto_aead_setauthsize(tfm
, TIPC_AES_GCM_TAG_SIZE
);
467 err
|= crypto_aead_setkey(tfm
, ukey
->key
, keylen
);
469 crypto_free_aead(tfm
);
473 tfm_entry
= kmalloc(sizeof(*tfm_entry
), GFP_KERNEL
);
474 if (unlikely(!tfm_entry
)) {
475 crypto_free_aead(tfm
);
479 INIT_LIST_HEAD(&tfm_entry
->list
);
480 tfm_entry
->tfm
= tfm
;
485 for_each_possible_cpu(cpu
) {
486 *per_cpu_ptr(tmp
->tfm_entry
, cpu
) = head
;
489 list_add_tail(&tfm_entry
->list
, &head
->list
);
492 } while (++tfm_cnt
< sysctl_tipc_max_tfms
);
494 /* Not any TFM is allocated? */
496 free_percpu(tmp
->tfm_entry
);
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';
505 /* Initialize the other data */
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);
519 * tipc_aead_clone - Clone a TIPC AEAD key
520 * @dst: dest key for the cloning
521 * @src: source key to clone from
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
528 * Note: this must be done in cluster-key mode only!
529 * Return: 0 in case of success, otherwise < 0
531 static int tipc_aead_clone(struct tipc_aead
**dst
, struct tipc_aead
*src
)
533 struct tipc_aead
*aead
;
539 if (src
->mode
!= CLUSTER_KEY
)
545 aead
= kzalloc(sizeof(*aead
), GFP_ATOMIC
);
549 aead
->tfm_entry
= alloc_percpu_gfp(struct tipc_tfm
*, GFP_ATOMIC
);
550 if (unlikely(!aead
->tfm_entry
)) {
555 for_each_possible_cpu(cpu
) {
556 *per_cpu_ptr(aead
->tfm_entry
, cpu
) =
557 *per_cpu_ptr(src
->tfm_entry
, cpu
);
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);
568 WARN_ON(!refcount_inc_not_zero(&src
->refcnt
));
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
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[]
588 * Return: the pointer to the memory areas in case of success, otherwise NULL
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
)
595 unsigned int iv_size
, req_size
;
599 iv_size
= crypto_aead_ivsize(tfm
);
600 req_size
= sizeof(**req
) + crypto_aead_reqsize(tfm
);
602 len
= crypto_ctx_size
;
604 len
+= crypto_aead_alignmask(tfm
) & ~(crypto_tfm_ctx_alignment() - 1);
605 len
= ALIGN(len
, crypto_tfm_ctx_alignment());
607 len
= ALIGN(len
, __alignof__(struct scatterlist
));
608 len
+= nsg
* sizeof(**sg
);
610 mem
= kmalloc(len
, GFP_ATOMIC
);
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
));
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"
633 * 0 : if the encryption has completed
634 * -EINPROGRESS/-EBUSY : if a callback will be performed
635 * < 0 : the encryption has failed
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
)
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
;
653 /* Make sure message len at least 4-byte aligned */
654 len
= ALIGN(skb
->len
, 4);
655 tailen
= len
- skb
->len
+ aead
->authsize
;
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.
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
);
671 if (unlikely(!skb_cloned(skb
) && tailen
<= skb_tailroom(skb
))) {
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 :(
681 nsg
= skb_cow_data(skb
, tailen
, &trailer
);
682 if (unlikely(nsg
< 0)) {
683 pr_err("TX: skb_cow_data() returned %d\n", nsg
);
688 pskb_put(skb
, trailer
, tailen
);
690 /* Allocate memory for the AEAD operation */
691 ctx
= tipc_aead_mem_alloc(tfm
, sizeof(*tx_ctx
), &iv
, &req
, &sg
, nsg
);
694 TIPC_SKB_CB(skb
)->crypto_ctx
= ctx
;
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
);
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
709 ehdr
= (struct tipc_ehdr
*)skb
->data
;
711 if (aead
->mode
== CLUSTER_KEY
)
712 salt
^= ehdr
->addr
; /* __be32 */
714 salt
^= tipc_node_get_addr(__dnode
);
715 memcpy(iv
, &salt
, 4);
716 memcpy(iv
+ 4, (u8
*)&ehdr
->seqno
, 8);
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
);
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
;
730 memcpy(&tx_ctx
->dst
, dst
, sizeof(*dst
));
733 if (unlikely(!tipc_bearer_hold(b
))) {
738 /* Now, do encrypt */
739 rc
= crypto_aead_encrypt(req
);
740 if (rc
== -EINPROGRESS
|| rc
== -EBUSY
)
747 TIPC_SKB_CB(skb
)->crypto_ctx
= NULL
;
751 static void tipc_aead_encrypt_done(struct crypto_async_request
*base
, int err
)
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
;
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
);
771 this_cpu_inc(tx
->stats
->stat
[STAT_ASYNC_NOK
]);
782 * tipc_aead_decrypt - Decrypt an encrypted message
784 * @aead: TIPC AEAD for the message decryption
785 * @skb: the input/output skb
786 * @b: TIPC bearer where the message has been received
789 * 0 : if the decryption has completed
790 * -EINPROGRESS/-EBUSY : if a callback will be performed
791 * < 0 : the decryption has failed
793 static int tipc_aead_decrypt(struct net
*net
, struct tipc_aead
*aead
,
794 struct sk_buff
*skb
, struct tipc_bearer
*b
)
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
;
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
;
815 nsg
= skb_cow_data(skb
, 0, &unused
);
816 if (unlikely(nsg
< 0)) {
817 pr_err("RX: skb_cow_data() returned %d\n", nsg
);
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
);
827 TIPC_SKB_CB(skb
)->crypto_ctx
= ctx
;
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
);
837 /* Reconstruct IV: */
838 ehdr
= (struct tipc_ehdr
*)skb
->data
;
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);
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
);
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
;
861 if (unlikely(!tipc_bearer_hold(b
))) {
866 /* Now, do decrypt */
867 rc
= crypto_aead_decrypt(req
);
868 if (rc
== -EINPROGRESS
|| rc
== -EBUSY
)
875 TIPC_SKB_CB(skb
)->crypto_ctx
= NULL
;
879 static void tipc_aead_decrypt_done(struct crypto_async_request
*base
, int err
)
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
;
890 this_cpu_inc(stats
->stat
[STAT_ASYNC_OK
]);
895 this_cpu_inc(stats
->stat
[STAT_ASYNC_NOK
]);
900 tipc_crypto_rcv_complete(net
, aead
, b
, &skb
, err
);
902 if (likely(test_bit(0, &b
->up
)))
903 tipc_rcv(net
, skb
, b
);
911 static inline int tipc_ehdr_size(struct tipc_ehdr
*ehdr
)
913 return (ehdr
->user
!= LINK_CONFIG
) ? EHDR_SIZE
: EHDR_CFG_SIZE
;
917 * tipc_ehdr_validate - Validate an encryption message
918 * @skb: the message buffer
920 * Returns "true" if this is a valid encryption message, otherwise "false"
922 bool tipc_ehdr_validate(struct sk_buff
*skb
)
924 struct tipc_ehdr
*ehdr
;
927 if (unlikely(!pskb_may_pull(skb
, EHDR_MIN_SIZE
)))
930 ehdr
= (struct tipc_ehdr
*)skb
->data
;
931 if (unlikely(ehdr
->version
!= TIPC_EVERSION
))
933 ehsz
= tipc_ehdr_size(ehdr
);
934 if (unlikely(!pskb_may_pull(skb
, ehsz
)))
936 if (unlikely(skb
->len
<= ehsz
+ TIPC_AES_GCM_TAG_SIZE
))
938 if (unlikely(!ehdr
->tx_key
))
945 * tipc_ehdr_build - Build TIPC encryption message header
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"
952 * Return: the header size if the building is successful, otherwise < 0
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
)
958 struct tipc_msg
*hdr
= buf_msg(skb
);
959 struct tipc_ehdr
*ehdr
;
960 u32 user
= msg_user(hdr
);
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
);
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!
973 if (!__rx
|| aead
->mode
== CLUSTER_KEY
)
974 seqno
= atomic64_inc_return(&aead
->seqno
);
976 seqno
= atomic64_inc_return(&__rx
->sndnxt
);
978 /* Revoke the key if seqno is wrapped around */
979 if (unlikely(!seqno
))
980 return tipc_crypto_key_revoke(net
, tx_key
);
983 ehdr
->seqno
= cpu_to_be64(seqno
);
986 ehdr
->version
= TIPC_EVERSION
;
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;
997 ehdr
->user
= LINK_CONFIG
;
998 memcpy(ehdr
->id
, tipc_own_id(net
), NODE_ID_LEN
);
1001 if (user
== LINK_PROTOCOL
&& msg_type(hdr
) == STATE_MSG
) {
1002 ehdr
->user
= LINK_PROTOCOL
;
1003 ehdr
->keepalive
= msg_is_keepalive(hdr
);
1005 ehdr
->addr
= hdr
->hdr
[3];
1012 static inline void tipc_crypto_key_set_state(struct tipc_crypto
*c
,
1017 #ifdef TIPC_CRYPTO_DEBUG
1018 struct tipc_key old
= c
->key
;
1022 c
->key
.keys
= ((new_passive
& KEY_MASK
) << (KEY_BITS
* 2)) |
1023 ((new_active
& KEY_MASK
) << (KEY_BITS
)) |
1024 ((new_pending
& KEY_MASK
));
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));
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)
1042 * A new TIPC AEAD key will be allocated and initiated with the specified user
1043 * key, then attached to the TIPC crypto.
1045 * Return: new key id in case of success, otherwise: < 0
1047 int tipc_crypto_key_init(struct tipc_crypto
*c
, struct tipc_aead_key
*ukey
,
1050 struct tipc_aead
*aead
= NULL
;
1053 /* Initiate with the new user key */
1054 rc
= tipc_aead_init(&aead
, ukey
, mode
);
1056 /* Attach it to the crypto */
1058 rc
= tipc_crypto_key_attach(c
, aead
, 0);
1060 tipc_aead_free(&aead
->rcu
);
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
),
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!
1078 * Return: new key id in case of success, otherwise: -EBUSY
1080 static int tipc_crypto_key_attach(struct tipc_crypto
*c
,
1081 struct tipc_aead
*aead
, u8 pos
)
1083 u8 new_pending
, new_passive
, new_key
;
1084 struct tipc_key key
;
1087 spin_lock_bh(&c
->lock
);
1089 if (key
.active
&& key
.passive
)
1091 if (key
.passive
&& !tipc_aead_users(c
->aead
[key
.passive
]))
1096 if (tipc_aead_users(c
->aead
[key
.pending
]) > 0)
1099 new_pending
= key
.pending
;
1100 new_passive
= key
.passive
;
1101 new_key
= new_pending
;
1104 if (key
.active
&& pos
!= key_next(key
.active
)) {
1105 new_pending
= key
.pending
;
1107 new_key
= new_passive
;
1109 } else if (!key
.active
&& !key
.passive
) {
1111 new_passive
= key
.passive
;
1112 new_key
= new_pending
;
1116 new_pending
= key_next(key
.active
?: key
.passive
);
1117 new_passive
= key
.passive
;
1118 new_key
= new_pending
;
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
);
1127 c
->timer1
= jiffies
;
1128 c
->timer2
= jiffies
;
1132 spin_unlock_bh(&c
->lock
);
1136 void tipc_crypto_key_flush(struct tipc_crypto
*c
)
1140 spin_lock_bh(&c
->lock
);
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
);
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)
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!
1161 * Return: "true" if key is successfully aligned, otherwise "false"
1163 static bool tipc_crypto_key_try_align(struct tipc_crypto
*rx
, u8 new_pending
)
1165 struct tipc_aead
*tmp1
, *tmp2
= NULL
;
1166 struct tipc_key key
;
1167 bool aligned
= false;
1171 spin_lock(&rx
->lock
);
1173 if (key
.pending
== new_pending
) {
1181 if (tipc_aead_users(rx
->aead
[key
.pending
]) > 0)
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
))
1188 rcu_assign_pointer(rx
->aead
[key
.pending
], NULL
);
1190 /* Move passive key if any */
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
;
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
);
1201 rcu_assign_pointer(rx
->aead
[new_passive
], tmp2
);
1202 refcount_set(&tmp1
->refcnt
, 1);
1204 pr_info("RX(%s): key is aligned!\n", tipc_node_get_id_str(rx
->node
));
1207 spin_unlock(&rx
->lock
);
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
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).
1221 * Return: the TX AEAD key handle in case of success, otherwise NULL
1223 static struct tipc_aead
*tipc_crypto_key_pick_tx(struct tipc_crypto
*tx
,
1224 struct tipc_crypto
*rx
,
1225 struct sk_buff
*skb
)
1227 struct tipc_skb_cb
*skb_cb
= TIPC_SKB_CB(skb
);
1228 struct tipc_aead
*aead
= NULL
;
1229 struct tipc_key key
= tx
->key
;
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
));
1238 skb_cb
->tx_clone_ctx
.rx
= rx
;
1239 if (++skb_cb
->tx_clone_ctx
.recurs
> 2)
1242 /* Pick one TX key */
1243 spin_lock(&tx
->lock
);
1245 k
= (i
== 0) ? key
.pending
:
1246 ((i
== 1) ? key
.active
: key
.passive
);
1249 aead
= tipc_aead_rcu_ptr(tx
->aead
[k
], &tx
->lock
);
1252 if (aead
->mode
!= CLUSTER_KEY
||
1253 aead
== skb_cb
->tx_clone_ctx
.last
) {
1257 /* Ok, found one cluster key */
1258 skb_cb
->tx_clone_ctx
.last
= aead
;
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
));
1266 spin_unlock(&tx
->lock
);
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
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.
1281 * The "per-peer" sndnxt is also reset when the peer key has switched.
1283 static void tipc_crypto_key_synch(struct tipc_crypto
*rx
, u8 new_rx_active
,
1284 struct tipc_msg
*hdr
)
1286 struct net
*net
= rx
->net
;
1287 struct tipc_crypto
*tx
= tipc_net(net
)->crypto_tx
;
1290 /* TX might be even not ready yet */
1291 if (unlikely(!tx
->key
.active
&& !tx
->key
.pending
))
1294 cur_rx_active
= atomic_read(&rx
->peer_rx_active
);
1295 if (likely(cur_rx_active
== new_rx_active
))
1298 /* Make sure this message destined for this node */
1299 if (unlikely(msg_short(hdr
) ||
1300 msg_destnode(hdr
) != tipc_own_addr(net
)))
1303 /* Peer RX active key has changed, try to update owns' & TX users */
1304 if (atomic_cmpxchg(&rx
->peer_rx_active
,
1306 new_rx_active
) == cur_rx_active
) {
1308 tipc_aead_users_inc(tx
->aead
[new_rx_active
], INT_MAX
);
1310 tipc_aead_users_dec(tx
->aead
[cur_rx_active
], 0);
1312 atomic64_set(&rx
->sndnxt
, 0);
1313 /* Mark the point TX key users changed */
1314 tx
->timer1
= jiffies
;
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
));
1324 static int tipc_crypto_key_revoke(struct net
*net
, u8 tx_key
)
1326 struct tipc_crypto
*tx
= tipc_net(net
)->crypto_tx
;
1327 struct tipc_key key
;
1329 spin_lock(&tx
->lock
);
1331 WARN_ON(!key
.active
|| tx_key
!= key
.active
);
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
);
1338 pr_warn("TX(%s): key is revoked!\n", tipc_own_id_string(net
));
1339 return -EKEYREVOKED
;
1342 int tipc_crypto_start(struct tipc_crypto
**crypto
, struct net
*net
,
1343 struct tipc_node
*node
)
1345 struct tipc_crypto
*c
;
1350 /* Allocate crypto */
1351 c
= kzalloc(sizeof(*c
), GFP_ATOMIC
);
1355 /* Allocate statistic structure */
1356 c
->stats
= alloc_percpu_gfp(struct tipc_crypto_stats
, GFP_ATOMIC
);
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
);
1376 void tipc_crypto_stop(struct tipc_crypto
**crypto
)
1378 struct tipc_crypto
*c
, *tx
, *rx
;
1386 /* RX stopping? => decrease TX key users if any */
1387 is_rx
= !!((*crypto
)->node
);
1390 tx
= tipc_net(rx
->net
)->crypto_tx
;
1391 k
= atomic_read(&rx
->peer_rx_active
);
1393 tipc_aead_users_dec(tx
->aead
[k
], 0);
1394 /* Mark the point TX key users changed */
1395 tx
->timer1
= jiffies
;
1399 /* Release AEAD keys */
1401 for (k
= KEY_MIN
; k
<= KEY_MAX
; k
++)
1402 tipc_aead_put(rcu_dereference(c
->aead
[k
]));
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
));
1410 /* Free this crypto statistics */
1411 free_percpu(c
->stats
);
1417 void tipc_crypto_timeout(struct tipc_crypto
*rx
)
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
;
1425 /* TX key activating:
1426 * The pending key (users > 0) -> active
1427 * The active key if any (users == 0) -> free
1429 spin_lock(&tx
->lock
);
1431 if (key
.active
&& tipc_aead_users(tx
->aead
[key
.active
]) > 0)
1433 if (!key
.pending
|| tipc_aead_users(tx
->aead
[key
.pending
]) <= 0)
1435 if (time_before(jiffies
, tx
->timer1
+ TIPC_TX_LASTING_LIM
))
1438 tipc_crypto_key_set_state(tx
, key
.passive
, key
.pending
, 0);
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
),
1446 spin_unlock(&tx
->lock
);
1448 /* RX key activating:
1449 * The pending key (users > 0) -> active
1450 * The active key if any -> passive, freed later
1452 spin_lock(&rx
->lock
);
1454 if (!key
.pending
|| tipc_aead_users(rx
->aead
[key
.pending
]) <= 0)
1457 new_pending
= (key
.passive
&&
1458 !tipc_aead_users(rx
->aead
[key
.passive
])) ?
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
);
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!
1474 if (!key
.pending
|| tipc_aead_users(rx
->aead
[key
.pending
]) > -30)
1476 if (!key
.passive
|| tipc_aead_users(rx
->aead
[key
.passive
]) != 0)
1479 new_pending
= key
.passive
;
1480 new_passive
= key
.pending
;
1481 tipc_crypto_key_set_state(rx
, new_passive
, key
.active
, new_pending
);
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)
1493 if (time_before(jiffies
, rx
->timer1
+ TIPC_RX_ACTIVE_LIM
))
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);
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
);
1507 /* RX key passive -> freed: */
1509 if (!key
.passive
|| !tipc_aead_users(rx
->aead
[key
.passive
]))
1511 if (time_before(jiffies
, rx
->timer2
+ TIPC_RX_PASSIVE_LIM
))
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
),
1520 spin_unlock(&rx
->lock
);
1522 /* Limit max_tfms & do debug commands if needed */
1523 if (likely(sysctl_tipc_max_tfms
<= TIPC_MAX_TFMS_LIM
))
1526 cmd
= sysctl_tipc_max_tfms
;
1527 sysctl_tipc_max_tfms
= TIPC_MAX_TFMS_DEF
;
1528 tipc_crypto_do_cmd(rx
->net
, cmd
);
1532 * tipc_crypto_xmit - Build & encrypt TIPC message for xmit
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
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
1543 * Otherwise, the skb is freed!
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
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
)
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
;
1566 /* No encryption? */
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;
1575 if (unlikely(key
.pending
)) {
1576 tx_key
= key
.pending
;
1579 if (__rx
&& atomic_read(&__rx
->peer_rx_active
) == tx_key
)
1581 if (TIPC_SKB_CB(*skb
)->probe
)
1584 time_after(jiffies
, tx
->timer2
+ TIPC_TX_PROBE_LIM
)) {
1585 tx
->timer2
= jiffies
;
1586 probe
= skb_clone(*skb
, GFP_ATOMIC
);
1588 TIPC_SKB_CB(probe
)->probe
= 1;
1589 tipc_crypto_xmit(net
, &probe
, b
, dst
, __dnode
);
1591 b
->media
->send_msg(net
, probe
, b
, dst
);
1595 /* Else, use the active key if any */
1596 if (likely(key
.active
)) {
1597 tx_key
= key
.active
;
1603 aead
= tipc_aead_get(tx
->aead
[tx_key
]);
1604 if (unlikely(!aead
))
1606 rc
= tipc_ehdr_build(net
, aead
, tx_key
, *skb
, __rx
);
1608 rc
= tipc_aead_encrypt(aead
, *skb
, b
, dst
, __dnode
);
1613 this_cpu_inc(stats
->stat
[STAT_OK
]);
1617 this_cpu_inc(stats
->stat
[STAT_ASYNC
]);
1621 this_cpu_inc(stats
->stat
[STAT_NOK
]);
1623 this_cpu_inc(stats
->stat
[STAT_NOKEYS
]);
1624 else if (rc
== -EKEYREVOKED
)
1625 this_cpu_inc(stats
->stat
[STAT_BADKEYS
]);
1631 tipc_aead_put(aead
);
1636 * tipc_crypto_rcv - Decrypt an encrypted TIPC message from peer
1638 * @rx: RX crypto handle
1639 * @skb: input/output message skb pointer
1640 * @b: bearer where the message has been received
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).
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
1657 int tipc_crypto_rcv(struct net
*net
, struct tipc_crypto
*rx
,
1658 struct sk_buff
**skb
, struct tipc_bearer
*b
)
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
;
1668 * Let's try with TX key (i.e. cluster mode) & verify the skb first!
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);
1678 tx_key
= ((struct tipc_ehdr
*)(*skb
)->data
)->tx_key
;
1680 if (likely(tx_key
== key
.active
))
1682 if (tx_key
== key
.pending
)
1684 if (tx_key
== key
.passive
) {
1685 rx
->timer2
= jiffies
;
1686 if (tipc_aead_users(rx
->aead
[key
.passive
]) > 0)
1690 /* Unknown key, let's try to align RX key(s) */
1691 if (tipc_crypto_key_try_align(rx
, tx_key
))
1695 /* No key suitable? Try to pick one from TX... */
1696 aead
= tipc_crypto_key_pick_tx(tx
, rx
, *skb
);
1704 aead
= tipc_aead_get(rx
->aead
[tx_key
]);
1705 rc
= tipc_aead_decrypt(net
, aead
, *skb
, b
);
1709 stats
= ((rx
) ?: tx
)->stats
;
1712 this_cpu_inc(stats
->stat
[STAT_OK
]);
1716 this_cpu_inc(stats
->stat
[STAT_ASYNC
]);
1720 this_cpu_inc(stats
->stat
[STAT_NOK
]);
1721 if (rc
== -ENOKEY
) {
1725 tipc_node_put(rx
->node
);
1726 this_cpu_inc(stats
->stat
[STAT_NOKEYS
]);
1728 } else if (rc
== -EBADMSG
) {
1729 this_cpu_inc(stats
->stat
[STAT_BADMSGS
]);
1734 tipc_crypto_rcv_complete(net
, aead
, b
, skb
, rc
);
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
)
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
;
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]);
1762 if (unlikely(err
)) {
1763 if (err
== -EBADMSG
&& (*skb
)->next
)
1764 tipc_rcv(net
, (*skb
)->next
, b
);
1768 if (likely((*skb
)->next
)) {
1769 kfree_skb((*skb
)->next
);
1770 (*skb
)->next
= NULL
;
1772 ehdr
= (struct tipc_ehdr
*)(*skb
)->data
;
1774 WARN_ON(ehdr
->user
!= LINK_CONFIG
);
1775 n
= tipc_node_create(net
, 0, ehdr
->id
, 0xffffu
, 0,
1777 rx
= tipc_node_crypto_rx(n
);
1782 /* Skip cloning this time as we had a RX pending key */
1783 if (rx
->key
.pending
)
1785 if (tipc_aead_clone(&tmp
, aead
) < 0)
1787 if (tipc_crypto_key_attach(rx
, tmp
, ehdr
->tx_key
) < 0) {
1788 tipc_aead_free(&tmp
->rcu
);
1791 tipc_aead_put(aead
);
1792 aead
= tipc_aead_get(tmp
);
1795 if (unlikely(err
)) {
1796 tipc_aead_users_dec(aead
, INT_MIN
);
1800 /* Set the RX key's user */
1801 tipc_aead_users_set(aead
, 1);
1804 /* Mark this point, RX works */
1805 rx
->timer1
= jiffies
;
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
);
1814 /* Validate TIPCv2 message */
1815 if (unlikely(!tipc_msg_validate(skb
))) {
1816 pr_err_ratelimited("Packet dropped after decryption!\n");
1820 /* Update peer RX active key & TX users */
1822 tipc_crypto_key_synch(rx
, rx_key_active
, buf_msg(*skb
));
1824 /* Mark skb decrypted */
1825 skb_cb
->decrypted
= 1;
1827 /* Clear clone cxt if any */
1828 if (likely(!skb_cb
->tx_clone_deferred
))
1830 skb_cb
->tx_clone_deferred
= 0;
1831 memset(&skb_cb
->tx_clone_ctx
, 0, sizeof(skb_cb
->tx_clone_ctx
));
1839 tipc_aead_put(aead
);
1841 tipc_node_put(rx
->node
);
1844 static void tipc_crypto_do_cmd(struct net
*net
, int cmd
)
1846 struct tipc_net
*tn
= tipc_net(net
);
1847 struct tipc_crypto
*tx
= tn
->crypto_tx
, *rx
;
1848 struct list_head
*p
;
1853 /* Currently only one command is supported */
1862 /* Print a header */
1863 pr_info("\n=============== TIPC Crypto Statistics ===============\n\n");
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
));
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
));
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
);
1883 memset(buf
, '-', 115);
1885 pr_info("%s\n", buf
);
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
);
1894 j
= scnprintf(buf
, 200, "%12s", " ");
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 ",
1909 j
= scnprintf(buf
, 200, "%12s", " ");
1914 pr_info("\n======================== Done ========================\n");
1917 static char *tipc_crypto_key_dump(struct tipc_crypto
*c
, char *buf
)
1919 struct tipc_key key
= c
->key
;
1920 struct tipc_aead
*aead
;
1924 for (k
= KEY_MIN
; k
<= KEY_MAX
; k
++) {
1925 if (k
== key
.passive
)
1927 else if (k
== key
.active
)
1929 else if (k
== key
.pending
)
1933 i
+= scnprintf(buf
+ i
, 200 - i
, "\tKey%d: %s", k
, s
);
1936 aead
= rcu_dereference(c
->aead
[k
]);
1938 i
+= scnprintf(buf
+ i
, 200 - i
,
1939 "{\"%s...\", \"%s\"}/%d:%d",
1941 (aead
->mode
== CLUSTER_KEY
) ? "c" : "p",
1942 atomic_read(&aead
->users
),
1943 refcount_read(&aead
->refcnt
));
1945 i
+= scnprintf(buf
+ i
, 200 - i
, "\n");
1949 i
+= scnprintf(buf
+ i
, 200 - i
, "\tPeer RX active: %d\n",
1950 atomic_read(&c
->peer_rx_active
));
1955 #ifdef TIPC_CRYPTO_DEBUG
1956 static char *tipc_key_change_dump(struct tipc_key old
, struct tipc_key
new,
1959 struct tipc_key
*key
= &old
;
1963 /* Output format: "[%s %s %s] -> [%s %s %s]", max len = 32 */
1965 i
+= scnprintf(buf
+ i
, 32 - i
, "[");
1966 for (k
= KEY_MIN
; k
<= KEY_MAX
; k
++) {
1967 if (k
== key
->passive
)
1969 else if (k
== key
->active
)
1971 else if (k
== key
->pending
)
1975 i
+= scnprintf(buf
+ i
, 32 - i
,
1976 (k
!= KEY_MAX
) ? "%s " : "%s", s
);
1979 i
+= scnprintf(buf
+ i
, 32 - i
, "] -> ");
1983 i
+= scnprintf(buf
+ i
, 32 - i
, "]");