Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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