1fc1b6d6dSTuong Lien // SPDX-License-Identifier: GPL-2.0
25fcb7d47SRandy Dunlap /*
3fc1b6d6dSTuong Lien * net/tipc/crypto.c: TIPC crypto for key handling & packet en/decryption
4fc1b6d6dSTuong Lien *
5fc1b6d6dSTuong Lien * Copyright (c) 2019, Ericsson AB
6fc1b6d6dSTuong Lien * All rights reserved.
7fc1b6d6dSTuong Lien *
8fc1b6d6dSTuong Lien * Redistribution and use in source and binary forms, with or without
9fc1b6d6dSTuong Lien * modification, are permitted provided that the following conditions are met:
10fc1b6d6dSTuong Lien *
11fc1b6d6dSTuong Lien * 1. Redistributions of source code must retain the above copyright
12fc1b6d6dSTuong Lien * notice, this list of conditions and the following disclaimer.
13fc1b6d6dSTuong Lien * 2. Redistributions in binary form must reproduce the above copyright
14fc1b6d6dSTuong Lien * notice, this list of conditions and the following disclaimer in the
15fc1b6d6dSTuong Lien * documentation and/or other materials provided with the distribution.
16fc1b6d6dSTuong Lien * 3. Neither the names of the copyright holders nor the names of its
17fc1b6d6dSTuong Lien * contributors may be used to endorse or promote products derived from
18fc1b6d6dSTuong Lien * this software without specific prior written permission.
19fc1b6d6dSTuong Lien *
20fc1b6d6dSTuong Lien * Alternatively, this software may be distributed under the terms of the
21fc1b6d6dSTuong Lien * GNU General Public License ("GPL") version 2 as published by the Free
22fc1b6d6dSTuong Lien * Software Foundation.
23fc1b6d6dSTuong Lien *
24fc1b6d6dSTuong Lien * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25fc1b6d6dSTuong Lien * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26fc1b6d6dSTuong Lien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27fc1b6d6dSTuong Lien * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28fc1b6d6dSTuong Lien * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29fc1b6d6dSTuong Lien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30fc1b6d6dSTuong Lien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31fc1b6d6dSTuong Lien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32fc1b6d6dSTuong Lien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33fc1b6d6dSTuong Lien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34fc1b6d6dSTuong Lien * POSSIBILITY OF SUCH DAMAGE.
35fc1b6d6dSTuong Lien */
36fc1b6d6dSTuong Lien
37fc1b6d6dSTuong Lien #include <crypto/aead.h>
38fc1b6d6dSTuong Lien #include <crypto/aes.h>
3923700da2STuong Lien #include <crypto/rng.h>
40fc1b6d6dSTuong Lien #include "crypto.h"
411ef6f7c9STuong Lien #include "msg.h"
421ef6f7c9STuong Lien #include "bcast.h"
43fc1b6d6dSTuong Lien
44daef1ee3STuong Lien #define TIPC_TX_GRACE_PERIOD msecs_to_jiffies(5000) /* 5s */
45f779bf79STuong Lien #define TIPC_TX_LASTING_TIME msecs_to_jiffies(10000) /* 10s */
46fc1b6d6dSTuong Lien #define TIPC_RX_ACTIVE_LIM msecs_to_jiffies(3000) /* 3s */
47f779bf79STuong Lien #define TIPC_RX_PASSIVE_LIM msecs_to_jiffies(15000) /* 15s */
48f779bf79STuong Lien
49fc1b6d6dSTuong Lien #define TIPC_MAX_TFMS_DEF 10
50fc1b6d6dSTuong Lien #define TIPC_MAX_TFMS_LIM 1000
51fc1b6d6dSTuong Lien
5223700da2STuong Lien #define TIPC_REKEYING_INTV_DEF (60 * 24) /* default: 1 day */
5323700da2STuong Lien
545fcb7d47SRandy Dunlap /*
55fc1b6d6dSTuong Lien * TIPC Key ids
56fc1b6d6dSTuong Lien */
57fc1b6d6dSTuong Lien enum {
58daef1ee3STuong Lien KEY_MASTER = 0,
59daef1ee3STuong Lien KEY_MIN = KEY_MASTER,
60daef1ee3STuong Lien KEY_1 = 1,
61fc1b6d6dSTuong Lien KEY_2,
62fc1b6d6dSTuong Lien KEY_3,
63fc1b6d6dSTuong Lien KEY_MAX = KEY_3,
64fc1b6d6dSTuong Lien };
65fc1b6d6dSTuong Lien
665fcb7d47SRandy Dunlap /*
67fc1b6d6dSTuong Lien * TIPC Crypto statistics
68fc1b6d6dSTuong Lien */
69fc1b6d6dSTuong Lien enum {
70fc1b6d6dSTuong Lien STAT_OK,
71fc1b6d6dSTuong Lien STAT_NOK,
72fc1b6d6dSTuong Lien STAT_ASYNC,
73fc1b6d6dSTuong Lien STAT_ASYNC_OK,
74fc1b6d6dSTuong Lien STAT_ASYNC_NOK,
75fc1b6d6dSTuong Lien STAT_BADKEYS, /* tx only */
76fc1b6d6dSTuong Lien STAT_BADMSGS = STAT_BADKEYS, /* rx only */
77fc1b6d6dSTuong Lien STAT_NOKEYS,
78fc1b6d6dSTuong Lien STAT_SWITCHES,
79fc1b6d6dSTuong Lien
80fc1b6d6dSTuong Lien MAX_STATS,
81fc1b6d6dSTuong Lien };
82fc1b6d6dSTuong Lien
83fc1b6d6dSTuong Lien /* TIPC crypto statistics' header */
84fc1b6d6dSTuong Lien static const char *hstats[MAX_STATS] = {"ok", "nok", "async", "async_ok",
85fc1b6d6dSTuong Lien "async_nok", "badmsgs", "nokeys",
86fc1b6d6dSTuong Lien "switches"};
87fc1b6d6dSTuong Lien
88fc1b6d6dSTuong Lien /* Max TFMs number per key */
89fc1b6d6dSTuong Lien int sysctl_tipc_max_tfms __read_mostly = TIPC_MAX_TFMS_DEF;
901ef6f7c9STuong Lien /* Key exchange switch, default: on */
911ef6f7c9STuong Lien int sysctl_tipc_key_exchange_enabled __read_mostly = 1;
92fc1b6d6dSTuong Lien
935fcb7d47SRandy Dunlap /*
94fc1b6d6dSTuong Lien * struct tipc_key - TIPC keys' status indicator
95fc1b6d6dSTuong Lien *
96fc1b6d6dSTuong Lien * 7 6 5 4 3 2 1 0
97fc1b6d6dSTuong Lien * +-----+-----+-----+-----+-----+-----+-----+-----+
98fc1b6d6dSTuong Lien * key: | (reserved)|passive idx| active idx|pending idx|
99fc1b6d6dSTuong Lien * +-----+-----+-----+-----+-----+-----+-----+-----+
100fc1b6d6dSTuong Lien */
101fc1b6d6dSTuong Lien struct tipc_key {
102fc1b6d6dSTuong Lien #define KEY_BITS (2)
103fc1b6d6dSTuong Lien #define KEY_MASK ((1 << KEY_BITS) - 1)
104fc1b6d6dSTuong Lien union {
105fc1b6d6dSTuong Lien struct {
106fc1b6d6dSTuong Lien #if defined(__LITTLE_ENDIAN_BITFIELD)
107fc1b6d6dSTuong Lien u8 pending:2,
108fc1b6d6dSTuong Lien active:2,
109fc1b6d6dSTuong Lien passive:2, /* rx only */
110fc1b6d6dSTuong Lien reserved:2;
111fc1b6d6dSTuong Lien #elif defined(__BIG_ENDIAN_BITFIELD)
112fc1b6d6dSTuong Lien u8 reserved:2,
113fc1b6d6dSTuong Lien passive:2, /* rx only */
114fc1b6d6dSTuong Lien active:2,
115fc1b6d6dSTuong Lien pending:2;
116fc1b6d6dSTuong Lien #else
117fc1b6d6dSTuong Lien #error "Please fix <asm/byteorder.h>"
118fc1b6d6dSTuong Lien #endif
119fc1b6d6dSTuong Lien } __packed;
120fc1b6d6dSTuong Lien u8 keys;
121fc1b6d6dSTuong Lien };
122fc1b6d6dSTuong Lien };
123fc1b6d6dSTuong Lien
124fc1b6d6dSTuong Lien /**
125fc1b6d6dSTuong Lien * struct tipc_tfm - TIPC TFM structure to form a list of TFMs
1265fcb7d47SRandy Dunlap * @tfm: cipher handle/key
1275fcb7d47SRandy Dunlap * @list: linked list of TFMs
128fc1b6d6dSTuong Lien */
129fc1b6d6dSTuong Lien struct tipc_tfm {
130fc1b6d6dSTuong Lien struct crypto_aead *tfm;
131fc1b6d6dSTuong Lien struct list_head list;
132fc1b6d6dSTuong Lien };
133fc1b6d6dSTuong Lien
134fc1b6d6dSTuong Lien /**
135fc1b6d6dSTuong Lien * struct tipc_aead - TIPC AEAD key structure
136fc1b6d6dSTuong Lien * @tfm_entry: per-cpu pointer to one entry in TFM list
137fc1b6d6dSTuong Lien * @crypto: TIPC crypto owns this key
138fc1b6d6dSTuong Lien * @cloned: reference to the source key in case cloning
139fc1b6d6dSTuong Lien * @users: the number of the key users (TX/RX)
140fc1b6d6dSTuong Lien * @salt: the key's SALT value
141fc1b6d6dSTuong Lien * @authsize: authentication tag size (max = 16)
142fc1b6d6dSTuong Lien * @mode: crypto mode is applied to the key
1435fcb7d47SRandy Dunlap * @hint: a hint for user key
144fc1b6d6dSTuong Lien * @rcu: struct rcu_head
1451ef6f7c9STuong Lien * @key: the aead key
1461ef6f7c9STuong Lien * @gen: the key's generation
147fc1b6d6dSTuong Lien * @seqno: the key seqno (cluster scope)
148fc1b6d6dSTuong Lien * @refcnt: the key reference counter
149fc1b6d6dSTuong Lien */
150fc1b6d6dSTuong Lien struct tipc_aead {
151fc1b6d6dSTuong Lien #define TIPC_AEAD_HINT_LEN (5)
152fc1b6d6dSTuong Lien struct tipc_tfm * __percpu *tfm_entry;
153fc1b6d6dSTuong Lien struct tipc_crypto *crypto;
154fc1b6d6dSTuong Lien struct tipc_aead *cloned;
155fc1b6d6dSTuong Lien atomic_t users;
156fc1b6d6dSTuong Lien u32 salt;
157fc1b6d6dSTuong Lien u8 authsize;
158fc1b6d6dSTuong Lien u8 mode;
159f779bf79STuong Lien char hint[2 * TIPC_AEAD_HINT_LEN + 1];
160fc1b6d6dSTuong Lien struct rcu_head rcu;
1611ef6f7c9STuong Lien struct tipc_aead_key *key;
1621ef6f7c9STuong Lien u16 gen;
163fc1b6d6dSTuong Lien
164fc1b6d6dSTuong Lien atomic64_t seqno ____cacheline_aligned;
165fc1b6d6dSTuong Lien refcount_t refcnt ____cacheline_aligned;
166fc1b6d6dSTuong Lien
167fc1b6d6dSTuong Lien } ____cacheline_aligned;
168fc1b6d6dSTuong Lien
169fc1b6d6dSTuong Lien /**
170fc1b6d6dSTuong Lien * struct tipc_crypto_stats - TIPC Crypto statistics
1715fcb7d47SRandy Dunlap * @stat: array of crypto statistics
172fc1b6d6dSTuong Lien */
173fc1b6d6dSTuong Lien struct tipc_crypto_stats {
174fc1b6d6dSTuong Lien unsigned int stat[MAX_STATS];
175fc1b6d6dSTuong Lien };
176fc1b6d6dSTuong Lien
177fc1b6d6dSTuong Lien /**
178fc1b6d6dSTuong Lien * struct tipc_crypto - TIPC TX/RX crypto structure
179fc1b6d6dSTuong Lien * @net: struct net
180fc1b6d6dSTuong Lien * @node: TIPC node (RX)
181fc1b6d6dSTuong Lien * @aead: array of pointers to AEAD keys for encryption/decryption
182fc1b6d6dSTuong Lien * @peer_rx_active: replicated peer RX active key index
1831ef6f7c9STuong Lien * @key_gen: TX/RX key generation
184fc1b6d6dSTuong Lien * @key: the key states
1851ef6f7c9STuong Lien * @skey_mode: session key's mode
1861ef6f7c9STuong Lien * @skey: received session key
1871ef6f7c9STuong Lien * @wq: common workqueue on TX crypto
1881ef6f7c9STuong Lien * @work: delayed work sched for TX/RX
1891ef6f7c9STuong Lien * @key_distr: key distributing state
19023700da2STuong Lien * @rekeying_intv: rekeying interval (in minutes)
191fc1b6d6dSTuong Lien * @stats: the crypto statistics
192f779bf79STuong Lien * @name: the crypto name
193fc1b6d6dSTuong Lien * @sndnxt: the per-peer sndnxt (TX)
194fc1b6d6dSTuong Lien * @timer1: general timer 1 (jiffies)
195f779bf79STuong Lien * @timer2: general timer 2 (jiffies)
196daef1ee3STuong Lien * @working: the crypto is working or not
197daef1ee3STuong Lien * @key_master: flag indicates if master key exists
198daef1ee3STuong Lien * @legacy_user: flag indicates if a peer joins w/o master key (for bwd comp.)
1991ef6f7c9STuong Lien * @nokey: no key indication
2005fcb7d47SRandy Dunlap * @flags: combined flags field
201fc1b6d6dSTuong Lien * @lock: tipc_key lock
202fc1b6d6dSTuong Lien */
203fc1b6d6dSTuong Lien struct tipc_crypto {
204fc1b6d6dSTuong Lien struct net *net;
205fc1b6d6dSTuong Lien struct tipc_node *node;
206daef1ee3STuong Lien struct tipc_aead __rcu *aead[KEY_MAX + 1];
207fc1b6d6dSTuong Lien atomic_t peer_rx_active;
2081ef6f7c9STuong Lien u16 key_gen;
209fc1b6d6dSTuong Lien struct tipc_key key;
2101ef6f7c9STuong Lien u8 skey_mode;
2111ef6f7c9STuong Lien struct tipc_aead_key *skey;
2121ef6f7c9STuong Lien struct workqueue_struct *wq;
2131ef6f7c9STuong Lien struct delayed_work work;
2141ef6f7c9STuong Lien #define KEY_DISTR_SCHED 1
2151ef6f7c9STuong Lien #define KEY_DISTR_COMPL 2
2161ef6f7c9STuong Lien atomic_t key_distr;
21723700da2STuong Lien u32 rekeying_intv;
2181ef6f7c9STuong Lien
219fc1b6d6dSTuong Lien struct tipc_crypto_stats __percpu *stats;
220f779bf79STuong Lien char name[48];
221fc1b6d6dSTuong Lien
222fc1b6d6dSTuong Lien atomic64_t sndnxt ____cacheline_aligned;
223fc1b6d6dSTuong Lien unsigned long timer1;
224fc1b6d6dSTuong Lien unsigned long timer2;
225daef1ee3STuong Lien union {
226daef1ee3STuong Lien struct {
227daef1ee3STuong Lien u8 working:1;
228daef1ee3STuong Lien u8 key_master:1;
229daef1ee3STuong Lien u8 legacy_user:1;
2301ef6f7c9STuong Lien u8 nokey: 1;
231daef1ee3STuong Lien };
232daef1ee3STuong Lien u8 flags;
233daef1ee3STuong Lien };
234fc1b6d6dSTuong Lien spinlock_t lock; /* crypto lock */
235fc1b6d6dSTuong Lien
236fc1b6d6dSTuong Lien } ____cacheline_aligned;
237fc1b6d6dSTuong Lien
238fc1b6d6dSTuong Lien /* struct tipc_crypto_tx_ctx - TX context for callbacks */
239fc1b6d6dSTuong Lien struct tipc_crypto_tx_ctx {
240fc1b6d6dSTuong Lien struct tipc_aead *aead;
241fc1b6d6dSTuong Lien struct tipc_bearer *bearer;
242fc1b6d6dSTuong Lien struct tipc_media_addr dst;
243fc1b6d6dSTuong Lien };
244fc1b6d6dSTuong Lien
245fc1b6d6dSTuong Lien /* struct tipc_crypto_rx_ctx - RX context for callbacks */
246fc1b6d6dSTuong Lien struct tipc_crypto_rx_ctx {
247fc1b6d6dSTuong Lien struct tipc_aead *aead;
248fc1b6d6dSTuong Lien struct tipc_bearer *bearer;
249fc1b6d6dSTuong Lien };
250fc1b6d6dSTuong Lien
251fc1b6d6dSTuong Lien static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead);
252fc1b6d6dSTuong Lien static inline void tipc_aead_put(struct tipc_aead *aead);
253fc1b6d6dSTuong Lien static void tipc_aead_free(struct rcu_head *rp);
254fc1b6d6dSTuong Lien static int tipc_aead_users(struct tipc_aead __rcu *aead);
255fc1b6d6dSTuong Lien static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim);
256fc1b6d6dSTuong Lien static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim);
257fc1b6d6dSTuong Lien static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val);
258fc1b6d6dSTuong Lien static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead);
259fc1b6d6dSTuong Lien static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
260fc1b6d6dSTuong Lien u8 mode);
261fc1b6d6dSTuong Lien static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src);
262fc1b6d6dSTuong Lien static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
263fc1b6d6dSTuong Lien unsigned int crypto_ctx_size,
264fc1b6d6dSTuong Lien u8 **iv, struct aead_request **req,
265fc1b6d6dSTuong Lien struct scatterlist **sg, int nsg);
266fc1b6d6dSTuong Lien static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
267fc1b6d6dSTuong Lien struct tipc_bearer *b,
268fc1b6d6dSTuong Lien struct tipc_media_addr *dst,
269fc1b6d6dSTuong Lien struct tipc_node *__dnode);
27065cb4657SHerbert Xu static void tipc_aead_encrypt_done(void *data, int err);
271fc1b6d6dSTuong Lien static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
272fc1b6d6dSTuong Lien struct sk_buff *skb, struct tipc_bearer *b);
27365cb4657SHerbert Xu static void tipc_aead_decrypt_done(void *data, int err);
274fc1b6d6dSTuong Lien static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr);
275fc1b6d6dSTuong Lien static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
276fc1b6d6dSTuong Lien u8 tx_key, struct sk_buff *skb,
277fc1b6d6dSTuong Lien struct tipc_crypto *__rx);
278fc1b6d6dSTuong Lien static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
279fc1b6d6dSTuong Lien u8 new_passive,
280fc1b6d6dSTuong Lien u8 new_active,
281fc1b6d6dSTuong Lien u8 new_pending);
282fc1b6d6dSTuong Lien static int tipc_crypto_key_attach(struct tipc_crypto *c,
283daef1ee3STuong Lien struct tipc_aead *aead, u8 pos,
284daef1ee3STuong Lien bool master_key);
285fc1b6d6dSTuong Lien static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending);
286fc1b6d6dSTuong Lien static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
287fc1b6d6dSTuong Lien struct tipc_crypto *rx,
288daef1ee3STuong Lien struct sk_buff *skb,
289daef1ee3STuong Lien u8 tx_key);
290f779bf79STuong Lien static void tipc_crypto_key_synch(struct tipc_crypto *rx, struct sk_buff *skb);
291fc1b6d6dSTuong Lien static int tipc_crypto_key_revoke(struct net *net, u8 tx_key);
292daef1ee3STuong Lien static inline void tipc_crypto_clone_msg(struct net *net, struct sk_buff *_skb,
293daef1ee3STuong Lien struct tipc_bearer *b,
294daef1ee3STuong Lien struct tipc_media_addr *dst,
295daef1ee3STuong Lien struct tipc_node *__dnode, u8 type);
296fc1b6d6dSTuong Lien static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
297fc1b6d6dSTuong Lien struct tipc_bearer *b,
298fc1b6d6dSTuong Lien struct sk_buff **skb, int err);
299fc1b6d6dSTuong Lien static void tipc_crypto_do_cmd(struct net *net, int cmd);
300fc1b6d6dSTuong Lien static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf);
301fc1b6d6dSTuong Lien static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
302fc1b6d6dSTuong Lien char *buf);
3031ef6f7c9STuong Lien static int tipc_crypto_key_xmit(struct net *net, struct tipc_aead_key *skey,
3041ef6f7c9STuong Lien u16 gen, u8 mode, u32 dnode);
3051ef6f7c9STuong Lien static bool tipc_crypto_key_rcv(struct tipc_crypto *rx, struct tipc_msg *hdr);
30623700da2STuong Lien static void tipc_crypto_work_tx(struct work_struct *work);
3071ef6f7c9STuong Lien static void tipc_crypto_work_rx(struct work_struct *work);
30823700da2STuong Lien static int tipc_aead_key_generate(struct tipc_aead_key *skey);
3091ef6f7c9STuong Lien
310f779bf79STuong Lien #define is_tx(crypto) (!(crypto)->node)
311f779bf79STuong Lien #define is_rx(crypto) (!is_tx(crypto))
312fc1b6d6dSTuong Lien
313fc1b6d6dSTuong Lien #define key_next(cur) ((cur) % KEY_MAX + 1)
314fc1b6d6dSTuong Lien
315fc1b6d6dSTuong Lien #define tipc_aead_rcu_ptr(rcu_ptr, lock) \
316fc1b6d6dSTuong Lien rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock))
317fc1b6d6dSTuong Lien
318fc1b6d6dSTuong Lien #define tipc_aead_rcu_replace(rcu_ptr, ptr, lock) \
319fc1b6d6dSTuong Lien do { \
32097bc84bbSHoang Huu Le struct tipc_aead *__tmp = rcu_dereference_protected((rcu_ptr), \
321fc1b6d6dSTuong Lien lockdep_is_held(lock)); \
322fc1b6d6dSTuong Lien rcu_assign_pointer((rcu_ptr), (ptr)); \
323fc1b6d6dSTuong Lien tipc_aead_put(__tmp); \
324fc1b6d6dSTuong Lien } while (0)
325fc1b6d6dSTuong Lien
326fc1b6d6dSTuong Lien #define tipc_crypto_key_detach(rcu_ptr, lock) \
327fc1b6d6dSTuong Lien tipc_aead_rcu_replace((rcu_ptr), NULL, lock)
328fc1b6d6dSTuong Lien
329fc1b6d6dSTuong Lien /**
330fc1b6d6dSTuong Lien * tipc_aead_key_validate - Validate a AEAD user key
3315fcb7d47SRandy Dunlap * @ukey: pointer to user key data
3325fcb7d47SRandy Dunlap * @info: netlink info pointer
333fc1b6d6dSTuong Lien */
tipc_aead_key_validate(struct tipc_aead_key * ukey,struct genl_info * info)334f779bf79STuong Lien int tipc_aead_key_validate(struct tipc_aead_key *ukey, struct genl_info *info)
335fc1b6d6dSTuong Lien {
336fc1b6d6dSTuong Lien int keylen;
337fc1b6d6dSTuong Lien
338fc1b6d6dSTuong Lien /* Check if algorithm exists */
339fc1b6d6dSTuong Lien if (unlikely(!crypto_has_alg(ukey->alg_name, 0, 0))) {
340f779bf79STuong Lien GENL_SET_ERR_MSG(info, "unable to load the algorithm (module existed?)");
341fc1b6d6dSTuong Lien return -ENODEV;
342fc1b6d6dSTuong Lien }
343fc1b6d6dSTuong Lien
344fc1b6d6dSTuong Lien /* Currently, we only support the "gcm(aes)" cipher algorithm */
345f779bf79STuong Lien if (strcmp(ukey->alg_name, "gcm(aes)")) {
346f779bf79STuong Lien GENL_SET_ERR_MSG(info, "not supported yet the algorithm");
347fc1b6d6dSTuong Lien return -ENOTSUPP;
348f779bf79STuong Lien }
349fc1b6d6dSTuong Lien
350fc1b6d6dSTuong Lien /* Check if key size is correct */
351fc1b6d6dSTuong Lien keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
352fc1b6d6dSTuong Lien if (unlikely(keylen != TIPC_AES_GCM_KEY_SIZE_128 &&
353fc1b6d6dSTuong Lien keylen != TIPC_AES_GCM_KEY_SIZE_192 &&
354f779bf79STuong Lien keylen != TIPC_AES_GCM_KEY_SIZE_256)) {
355f779bf79STuong Lien GENL_SET_ERR_MSG(info, "incorrect key length (20, 28 or 36 octets?)");
356f779bf79STuong Lien return -EKEYREJECTED;
357f779bf79STuong Lien }
358fc1b6d6dSTuong Lien
359fc1b6d6dSTuong Lien return 0;
360fc1b6d6dSTuong Lien }
361fc1b6d6dSTuong Lien
36223700da2STuong Lien /**
36323700da2STuong Lien * tipc_aead_key_generate - Generate new session key
36423700da2STuong Lien * @skey: input/output key with new content
36523700da2STuong Lien *
36623700da2STuong Lien * Return: 0 in case of success, otherwise < 0
36723700da2STuong Lien */
tipc_aead_key_generate(struct tipc_aead_key * skey)36823700da2STuong Lien static int tipc_aead_key_generate(struct tipc_aead_key *skey)
36923700da2STuong Lien {
37023700da2STuong Lien int rc = 0;
37123700da2STuong Lien
37223700da2STuong Lien /* Fill the key's content with a random value via RNG cipher */
37323700da2STuong Lien rc = crypto_get_default_rng();
37423700da2STuong Lien if (likely(!rc)) {
37523700da2STuong Lien rc = crypto_rng_get_bytes(crypto_default_rng, skey->key,
37623700da2STuong Lien skey->keylen);
37723700da2STuong Lien crypto_put_default_rng();
37823700da2STuong Lien }
37923700da2STuong Lien
38023700da2STuong Lien return rc;
38123700da2STuong Lien }
38223700da2STuong Lien
tipc_aead_get(struct tipc_aead __rcu * aead)383fc1b6d6dSTuong Lien static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead)
384fc1b6d6dSTuong Lien {
385fc1b6d6dSTuong Lien struct tipc_aead *tmp;
386fc1b6d6dSTuong Lien
387fc1b6d6dSTuong Lien rcu_read_lock();
388fc1b6d6dSTuong Lien tmp = rcu_dereference(aead);
389fc1b6d6dSTuong Lien if (unlikely(!tmp || !refcount_inc_not_zero(&tmp->refcnt)))
390fc1b6d6dSTuong Lien tmp = NULL;
391fc1b6d6dSTuong Lien rcu_read_unlock();
392fc1b6d6dSTuong Lien
393fc1b6d6dSTuong Lien return tmp;
394fc1b6d6dSTuong Lien }
395fc1b6d6dSTuong Lien
tipc_aead_put(struct tipc_aead * aead)396fc1b6d6dSTuong Lien static inline void tipc_aead_put(struct tipc_aead *aead)
397fc1b6d6dSTuong Lien {
398fc1b6d6dSTuong Lien if (aead && refcount_dec_and_test(&aead->refcnt))
399fc1b6d6dSTuong Lien call_rcu(&aead->rcu, tipc_aead_free);
400fc1b6d6dSTuong Lien }
401fc1b6d6dSTuong Lien
402fc1b6d6dSTuong Lien /**
403fc1b6d6dSTuong Lien * tipc_aead_free - Release AEAD key incl. all the TFMs in the list
404fc1b6d6dSTuong Lien * @rp: rcu head pointer
405fc1b6d6dSTuong Lien */
tipc_aead_free(struct rcu_head * rp)406fc1b6d6dSTuong Lien static void tipc_aead_free(struct rcu_head *rp)
407fc1b6d6dSTuong Lien {
408fc1b6d6dSTuong Lien struct tipc_aead *aead = container_of(rp, struct tipc_aead, rcu);
409fc1b6d6dSTuong Lien struct tipc_tfm *tfm_entry, *head, *tmp;
410fc1b6d6dSTuong Lien
411fc1b6d6dSTuong Lien if (aead->cloned) {
412fc1b6d6dSTuong Lien tipc_aead_put(aead->cloned);
413fc1b6d6dSTuong Lien } else {
414bb8872a1STuong Lien head = *get_cpu_ptr(aead->tfm_entry);
415bb8872a1STuong Lien put_cpu_ptr(aead->tfm_entry);
416fc1b6d6dSTuong Lien list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) {
417fc1b6d6dSTuong Lien crypto_free_aead(tfm_entry->tfm);
418fc1b6d6dSTuong Lien list_del(&tfm_entry->list);
419fc1b6d6dSTuong Lien kfree(tfm_entry);
420fc1b6d6dSTuong Lien }
421fc1b6d6dSTuong Lien /* Free the head */
422fc1b6d6dSTuong Lien crypto_free_aead(head->tfm);
423fc1b6d6dSTuong Lien list_del(&head->list);
424fc1b6d6dSTuong Lien kfree(head);
425fc1b6d6dSTuong Lien }
426fc1b6d6dSTuong Lien free_percpu(aead->tfm_entry);
42723224e45SEric Biggers kfree_sensitive(aead->key);
428fc1b6d6dSTuong Lien kfree(aead);
429fc1b6d6dSTuong Lien }
430fc1b6d6dSTuong Lien
tipc_aead_users(struct tipc_aead __rcu * aead)431fc1b6d6dSTuong Lien static int tipc_aead_users(struct tipc_aead __rcu *aead)
432fc1b6d6dSTuong Lien {
433fc1b6d6dSTuong Lien struct tipc_aead *tmp;
434fc1b6d6dSTuong Lien int users = 0;
435fc1b6d6dSTuong Lien
436fc1b6d6dSTuong Lien rcu_read_lock();
437fc1b6d6dSTuong Lien tmp = rcu_dereference(aead);
438fc1b6d6dSTuong Lien if (tmp)
439fc1b6d6dSTuong Lien users = atomic_read(&tmp->users);
440fc1b6d6dSTuong Lien rcu_read_unlock();
441fc1b6d6dSTuong Lien
442fc1b6d6dSTuong Lien return users;
443fc1b6d6dSTuong Lien }
444fc1b6d6dSTuong Lien
tipc_aead_users_inc(struct tipc_aead __rcu * aead,int lim)445fc1b6d6dSTuong Lien static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim)
446fc1b6d6dSTuong Lien {
447fc1b6d6dSTuong Lien struct tipc_aead *tmp;
448fc1b6d6dSTuong Lien
449fc1b6d6dSTuong Lien rcu_read_lock();
450fc1b6d6dSTuong Lien tmp = rcu_dereference(aead);
451fc1b6d6dSTuong Lien if (tmp)
452fc1b6d6dSTuong Lien atomic_add_unless(&tmp->users, 1, lim);
453fc1b6d6dSTuong Lien rcu_read_unlock();
454fc1b6d6dSTuong Lien }
455fc1b6d6dSTuong Lien
tipc_aead_users_dec(struct tipc_aead __rcu * aead,int lim)456fc1b6d6dSTuong Lien static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim)
457fc1b6d6dSTuong Lien {
458fc1b6d6dSTuong Lien struct tipc_aead *tmp;
459fc1b6d6dSTuong Lien
460fc1b6d6dSTuong Lien rcu_read_lock();
461fc1b6d6dSTuong Lien tmp = rcu_dereference(aead);
462fc1b6d6dSTuong Lien if (tmp)
463fc1b6d6dSTuong Lien atomic_add_unless(&rcu_dereference(aead)->users, -1, lim);
464fc1b6d6dSTuong Lien rcu_read_unlock();
465fc1b6d6dSTuong Lien }
466fc1b6d6dSTuong Lien
tipc_aead_users_set(struct tipc_aead __rcu * aead,int val)467fc1b6d6dSTuong Lien static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
468fc1b6d6dSTuong Lien {
469fc1b6d6dSTuong Lien struct tipc_aead *tmp;
470fc1b6d6dSTuong Lien int cur;
471fc1b6d6dSTuong Lien
472fc1b6d6dSTuong Lien rcu_read_lock();
473fc1b6d6dSTuong Lien tmp = rcu_dereference(aead);
474fc1b6d6dSTuong Lien if (tmp) {
475fc1b6d6dSTuong Lien do {
476fc1b6d6dSTuong Lien cur = atomic_read(&tmp->users);
477fc1b6d6dSTuong Lien if (cur == val)
478fc1b6d6dSTuong Lien break;
479fc1b6d6dSTuong Lien } while (atomic_cmpxchg(&tmp->users, cur, val) != cur);
480fc1b6d6dSTuong Lien }
481fc1b6d6dSTuong Lien rcu_read_unlock();
482fc1b6d6dSTuong Lien }
483fc1b6d6dSTuong Lien
484fc1b6d6dSTuong Lien /**
485fc1b6d6dSTuong Lien * tipc_aead_tfm_next - Move TFM entry to the next one in list and return it
4865fcb7d47SRandy Dunlap * @aead: the AEAD key pointer
487fc1b6d6dSTuong Lien */
tipc_aead_tfm_next(struct tipc_aead * aead)488fc1b6d6dSTuong Lien static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
489fc1b6d6dSTuong Lien {
490bb8872a1STuong Lien struct tipc_tfm **tfm_entry;
491bb8872a1STuong Lien struct crypto_aead *tfm;
492fc1b6d6dSTuong Lien
493bb8872a1STuong Lien tfm_entry = get_cpu_ptr(aead->tfm_entry);
494fc1b6d6dSTuong Lien *tfm_entry = list_next_entry(*tfm_entry, list);
495bb8872a1STuong Lien tfm = (*tfm_entry)->tfm;
496bb8872a1STuong Lien put_cpu_ptr(tfm_entry);
497bb8872a1STuong Lien
498bb8872a1STuong Lien return tfm;
499fc1b6d6dSTuong Lien }
500fc1b6d6dSTuong Lien
501fc1b6d6dSTuong Lien /**
502fc1b6d6dSTuong Lien * tipc_aead_init - Initiate TIPC AEAD
503fc1b6d6dSTuong Lien * @aead: returned new TIPC AEAD key handle pointer
504fc1b6d6dSTuong Lien * @ukey: pointer to user key data
505fc1b6d6dSTuong Lien * @mode: the key mode
506fc1b6d6dSTuong Lien *
507fc1b6d6dSTuong Lien * Allocate a (list of) new cipher transformation (TFM) with the specific user
508fc1b6d6dSTuong Lien * key data if valid. The number of the allocated TFMs can be set via the sysfs
509fc1b6d6dSTuong Lien * "net/tipc/max_tfms" first.
510fc1b6d6dSTuong Lien * Also, all the other AEAD data are also initialized.
511fc1b6d6dSTuong Lien *
512fc1b6d6dSTuong Lien * Return: 0 if the initiation is successful, otherwise: < 0
513fc1b6d6dSTuong Lien */
tipc_aead_init(struct tipc_aead ** aead,struct tipc_aead_key * ukey,u8 mode)514fc1b6d6dSTuong Lien static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
515fc1b6d6dSTuong Lien u8 mode)
516fc1b6d6dSTuong Lien {
517fc1b6d6dSTuong Lien struct tipc_tfm *tfm_entry, *head;
518fc1b6d6dSTuong Lien struct crypto_aead *tfm;
519fc1b6d6dSTuong Lien struct tipc_aead *tmp;
520fc1b6d6dSTuong Lien int keylen, err, cpu;
521fc1b6d6dSTuong Lien int tfm_cnt = 0;
522fc1b6d6dSTuong Lien
523fc1b6d6dSTuong Lien if (unlikely(*aead))
524fc1b6d6dSTuong Lien return -EEXIST;
525fc1b6d6dSTuong Lien
526fc1b6d6dSTuong Lien /* Allocate a new AEAD */
527f845fe58SHoang Le tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
528fc1b6d6dSTuong Lien if (unlikely(!tmp))
529fc1b6d6dSTuong Lien return -ENOMEM;
530fc1b6d6dSTuong Lien
531fc1b6d6dSTuong Lien /* The key consists of two parts: [AES-KEY][SALT] */
532fc1b6d6dSTuong Lien keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
533fc1b6d6dSTuong Lien
534fc1b6d6dSTuong Lien /* Allocate per-cpu TFM entry pointer */
535fc1b6d6dSTuong Lien tmp->tfm_entry = alloc_percpu(struct tipc_tfm *);
536fc1b6d6dSTuong Lien if (!tmp->tfm_entry) {
537453431a5SWaiman Long kfree_sensitive(tmp);
538fc1b6d6dSTuong Lien return -ENOMEM;
539fc1b6d6dSTuong Lien }
540fc1b6d6dSTuong Lien
541fc1b6d6dSTuong Lien /* Make a list of TFMs with the user key data */
542fc1b6d6dSTuong Lien do {
543fc1b6d6dSTuong Lien tfm = crypto_alloc_aead(ukey->alg_name, 0, 0);
544fc1b6d6dSTuong Lien if (IS_ERR(tfm)) {
545fc1b6d6dSTuong Lien err = PTR_ERR(tfm);
546fc1b6d6dSTuong Lien break;
547fc1b6d6dSTuong Lien }
548fc1b6d6dSTuong Lien
549fc1b6d6dSTuong Lien if (unlikely(!tfm_cnt &&
550fc1b6d6dSTuong Lien crypto_aead_ivsize(tfm) != TIPC_AES_GCM_IV_SIZE)) {
551fc1b6d6dSTuong Lien crypto_free_aead(tfm);
552fc1b6d6dSTuong Lien err = -ENOTSUPP;
553fc1b6d6dSTuong Lien break;
554fc1b6d6dSTuong Lien }
555fc1b6d6dSTuong Lien
556c33fdc34SColin Ian King err = crypto_aead_setauthsize(tfm, TIPC_AES_GCM_TAG_SIZE);
557fc1b6d6dSTuong Lien err |= crypto_aead_setkey(tfm, ukey->key, keylen);
558fc1b6d6dSTuong Lien if (unlikely(err)) {
559fc1b6d6dSTuong Lien crypto_free_aead(tfm);
560fc1b6d6dSTuong Lien break;
561fc1b6d6dSTuong Lien }
562fc1b6d6dSTuong Lien
563fc1b6d6dSTuong Lien tfm_entry = kmalloc(sizeof(*tfm_entry), GFP_KERNEL);
564fc1b6d6dSTuong Lien if (unlikely(!tfm_entry)) {
565fc1b6d6dSTuong Lien crypto_free_aead(tfm);
566fc1b6d6dSTuong Lien err = -ENOMEM;
567fc1b6d6dSTuong Lien break;
568fc1b6d6dSTuong Lien }
569fc1b6d6dSTuong Lien INIT_LIST_HEAD(&tfm_entry->list);
570fc1b6d6dSTuong Lien tfm_entry->tfm = tfm;
571fc1b6d6dSTuong Lien
572fc1b6d6dSTuong Lien /* First entry? */
573fc1b6d6dSTuong Lien if (!tfm_cnt) {
574fc1b6d6dSTuong Lien head = tfm_entry;
575fc1b6d6dSTuong Lien for_each_possible_cpu(cpu) {
576fc1b6d6dSTuong Lien *per_cpu_ptr(tmp->tfm_entry, cpu) = head;
577fc1b6d6dSTuong Lien }
578fc1b6d6dSTuong Lien } else {
579fc1b6d6dSTuong Lien list_add_tail(&tfm_entry->list, &head->list);
580fc1b6d6dSTuong Lien }
581fc1b6d6dSTuong Lien
582fc1b6d6dSTuong Lien } while (++tfm_cnt < sysctl_tipc_max_tfms);
583fc1b6d6dSTuong Lien
584fc1b6d6dSTuong Lien /* Not any TFM is allocated? */
585fc1b6d6dSTuong Lien if (!tfm_cnt) {
586fc1b6d6dSTuong Lien free_percpu(tmp->tfm_entry);
587453431a5SWaiman Long kfree_sensitive(tmp);
588fc1b6d6dSTuong Lien return err;
589fc1b6d6dSTuong Lien }
590fc1b6d6dSTuong Lien
591f779bf79STuong Lien /* Form a hex string of some last bytes as the key's hint */
592f779bf79STuong Lien bin2hex(tmp->hint, ukey->key + keylen - TIPC_AEAD_HINT_LEN,
593f779bf79STuong Lien TIPC_AEAD_HINT_LEN);
594fc1b6d6dSTuong Lien
595fc1b6d6dSTuong Lien /* Initialize the other data */
596fc1b6d6dSTuong Lien tmp->mode = mode;
597fc1b6d6dSTuong Lien tmp->cloned = NULL;
598fc1b6d6dSTuong Lien tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
5991ef6f7c9STuong Lien tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
6003e6db079STadeusz Struk if (!tmp->key) {
6013e6db079STadeusz Struk tipc_aead_free(&tmp->rcu);
6023e6db079STadeusz Struk return -ENOMEM;
6033e6db079STadeusz Struk }
604fc1b6d6dSTuong Lien memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
605fc1b6d6dSTuong Lien atomic_set(&tmp->users, 0);
606fc1b6d6dSTuong Lien atomic64_set(&tmp->seqno, 0);
607fc1b6d6dSTuong Lien refcount_set(&tmp->refcnt, 1);
608fc1b6d6dSTuong Lien
609fc1b6d6dSTuong Lien *aead = tmp;
610fc1b6d6dSTuong Lien return 0;
611fc1b6d6dSTuong Lien }
612fc1b6d6dSTuong Lien
613fc1b6d6dSTuong Lien /**
614fc1b6d6dSTuong Lien * tipc_aead_clone - Clone a TIPC AEAD key
615fc1b6d6dSTuong Lien * @dst: dest key for the cloning
616fc1b6d6dSTuong Lien * @src: source key to clone from
617fc1b6d6dSTuong Lien *
618fc1b6d6dSTuong Lien * Make a "copy" of the source AEAD key data to the dest, the TFMs list is
619fc1b6d6dSTuong Lien * common for the keys.
620fc1b6d6dSTuong Lien * A reference to the source is hold in the "cloned" pointer for the later
621fc1b6d6dSTuong Lien * freeing purposes.
622fc1b6d6dSTuong Lien *
623fc1b6d6dSTuong Lien * Note: this must be done in cluster-key mode only!
624fc1b6d6dSTuong Lien * Return: 0 in case of success, otherwise < 0
625fc1b6d6dSTuong Lien */
tipc_aead_clone(struct tipc_aead ** dst,struct tipc_aead * src)626fc1b6d6dSTuong Lien static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src)
627fc1b6d6dSTuong Lien {
628fc1b6d6dSTuong Lien struct tipc_aead *aead;
629fc1b6d6dSTuong Lien int cpu;
630fc1b6d6dSTuong Lien
631fc1b6d6dSTuong Lien if (!src)
632fc1b6d6dSTuong Lien return -ENOKEY;
633fc1b6d6dSTuong Lien
634fc1b6d6dSTuong Lien if (src->mode != CLUSTER_KEY)
635fc1b6d6dSTuong Lien return -EINVAL;
636fc1b6d6dSTuong Lien
637fc1b6d6dSTuong Lien if (unlikely(*dst))
638fc1b6d6dSTuong Lien return -EEXIST;
639fc1b6d6dSTuong Lien
640fc1b6d6dSTuong Lien aead = kzalloc(sizeof(*aead), GFP_ATOMIC);
641fc1b6d6dSTuong Lien if (unlikely(!aead))
642fc1b6d6dSTuong Lien return -ENOMEM;
643fc1b6d6dSTuong Lien
644fc1b6d6dSTuong Lien aead->tfm_entry = alloc_percpu_gfp(struct tipc_tfm *, GFP_ATOMIC);
645fc1b6d6dSTuong Lien if (unlikely(!aead->tfm_entry)) {
646453431a5SWaiman Long kfree_sensitive(aead);
647fc1b6d6dSTuong Lien return -ENOMEM;
648fc1b6d6dSTuong Lien }
649fc1b6d6dSTuong Lien
650fc1b6d6dSTuong Lien for_each_possible_cpu(cpu) {
651fc1b6d6dSTuong Lien *per_cpu_ptr(aead->tfm_entry, cpu) =
652fc1b6d6dSTuong Lien *per_cpu_ptr(src->tfm_entry, cpu);
653fc1b6d6dSTuong Lien }
654fc1b6d6dSTuong Lien
655fc1b6d6dSTuong Lien memcpy(aead->hint, src->hint, sizeof(src->hint));
656fc1b6d6dSTuong Lien aead->mode = src->mode;
657fc1b6d6dSTuong Lien aead->salt = src->salt;
658fc1b6d6dSTuong Lien aead->authsize = src->authsize;
659fc1b6d6dSTuong Lien atomic_set(&aead->users, 0);
660fc1b6d6dSTuong Lien atomic64_set(&aead->seqno, 0);
661fc1b6d6dSTuong Lien refcount_set(&aead->refcnt, 1);
662fc1b6d6dSTuong Lien
663fc1b6d6dSTuong Lien WARN_ON(!refcount_inc_not_zero(&src->refcnt));
664fc1b6d6dSTuong Lien aead->cloned = src;
665fc1b6d6dSTuong Lien
666fc1b6d6dSTuong Lien *dst = aead;
667fc1b6d6dSTuong Lien return 0;
668fc1b6d6dSTuong Lien }
669fc1b6d6dSTuong Lien
670fc1b6d6dSTuong Lien /**
671fc1b6d6dSTuong Lien * tipc_aead_mem_alloc - Allocate memory for AEAD request operations
672fc1b6d6dSTuong Lien * @tfm: cipher handle to be registered with the request
673fc1b6d6dSTuong Lien * @crypto_ctx_size: size of crypto context for callback
674fc1b6d6dSTuong Lien * @iv: returned pointer to IV data
675fc1b6d6dSTuong Lien * @req: returned pointer to AEAD request data
676fc1b6d6dSTuong Lien * @sg: returned pointer to SG lists
677fc1b6d6dSTuong Lien * @nsg: number of SG lists to be allocated
678fc1b6d6dSTuong Lien *
679fc1b6d6dSTuong Lien * Allocate memory to store the crypto context data, AEAD request, IV and SG
680fc1b6d6dSTuong Lien * lists, the memory layout is as follows:
681fc1b6d6dSTuong Lien * crypto_ctx || iv || aead_req || sg[]
682fc1b6d6dSTuong Lien *
683fc1b6d6dSTuong Lien * Return: the pointer to the memory areas in case of success, otherwise NULL
684fc1b6d6dSTuong Lien */
tipc_aead_mem_alloc(struct crypto_aead * tfm,unsigned int crypto_ctx_size,u8 ** iv,struct aead_request ** req,struct scatterlist ** sg,int nsg)685fc1b6d6dSTuong Lien static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
686fc1b6d6dSTuong Lien unsigned int crypto_ctx_size,
687fc1b6d6dSTuong Lien u8 **iv, struct aead_request **req,
688fc1b6d6dSTuong Lien struct scatterlist **sg, int nsg)
689fc1b6d6dSTuong Lien {
690fc1b6d6dSTuong Lien unsigned int iv_size, req_size;
691fc1b6d6dSTuong Lien unsigned int len;
692fc1b6d6dSTuong Lien u8 *mem;
693fc1b6d6dSTuong Lien
694fc1b6d6dSTuong Lien iv_size = crypto_aead_ivsize(tfm);
695fc1b6d6dSTuong Lien req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
696fc1b6d6dSTuong Lien
697fc1b6d6dSTuong Lien len = crypto_ctx_size;
698fc1b6d6dSTuong Lien len += iv_size;
699fc1b6d6dSTuong Lien len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
700fc1b6d6dSTuong Lien len = ALIGN(len, crypto_tfm_ctx_alignment());
701fc1b6d6dSTuong Lien len += req_size;
702fc1b6d6dSTuong Lien len = ALIGN(len, __alignof__(struct scatterlist));
703fc1b6d6dSTuong Lien len += nsg * sizeof(**sg);
704fc1b6d6dSTuong Lien
705fc1b6d6dSTuong Lien mem = kmalloc(len, GFP_ATOMIC);
706fc1b6d6dSTuong Lien if (!mem)
707fc1b6d6dSTuong Lien return NULL;
708fc1b6d6dSTuong Lien
709fc1b6d6dSTuong Lien *iv = (u8 *)PTR_ALIGN(mem + crypto_ctx_size,
710fc1b6d6dSTuong Lien crypto_aead_alignmask(tfm) + 1);
711fc1b6d6dSTuong Lien *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
712fc1b6d6dSTuong Lien crypto_tfm_ctx_alignment());
713fc1b6d6dSTuong Lien *sg = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
714fc1b6d6dSTuong Lien __alignof__(struct scatterlist));
715fc1b6d6dSTuong Lien
716fc1b6d6dSTuong Lien return (void *)mem;
717fc1b6d6dSTuong Lien }
718fc1b6d6dSTuong Lien
719fc1b6d6dSTuong Lien /**
720fc1b6d6dSTuong Lien * tipc_aead_encrypt - Encrypt a message
721fc1b6d6dSTuong Lien * @aead: TIPC AEAD key for the message encryption
722fc1b6d6dSTuong Lien * @skb: the input/output skb
723fc1b6d6dSTuong Lien * @b: TIPC bearer where the message will be delivered after the encryption
724fc1b6d6dSTuong Lien * @dst: the destination media address
725fc1b6d6dSTuong Lien * @__dnode: TIPC dest node if "known"
726fc1b6d6dSTuong Lien *
727fc1b6d6dSTuong Lien * Return:
728637b77fdSRandy Dunlap * * 0 : if the encryption has completed
729637b77fdSRandy Dunlap * * -EINPROGRESS/-EBUSY : if a callback will be performed
730637b77fdSRandy Dunlap * * < 0 : the encryption has failed
731fc1b6d6dSTuong Lien */
tipc_aead_encrypt(struct tipc_aead * aead,struct sk_buff * skb,struct tipc_bearer * b,struct tipc_media_addr * dst,struct tipc_node * __dnode)732fc1b6d6dSTuong Lien static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
733fc1b6d6dSTuong Lien struct tipc_bearer *b,
734fc1b6d6dSTuong Lien struct tipc_media_addr *dst,
735fc1b6d6dSTuong Lien struct tipc_node *__dnode)
736fc1b6d6dSTuong Lien {
737fc1b6d6dSTuong Lien struct crypto_aead *tfm = tipc_aead_tfm_next(aead);
738fc1b6d6dSTuong Lien struct tipc_crypto_tx_ctx *tx_ctx;
739fc1b6d6dSTuong Lien struct aead_request *req;
740fc1b6d6dSTuong Lien struct sk_buff *trailer;
741fc1b6d6dSTuong Lien struct scatterlist *sg;
742fc1b6d6dSTuong Lien struct tipc_ehdr *ehdr;
743fc1b6d6dSTuong Lien int ehsz, len, tailen, nsg, rc;
744fc1b6d6dSTuong Lien void *ctx;
745fc1b6d6dSTuong Lien u32 salt;
746fc1b6d6dSTuong Lien u8 *iv;
747fc1b6d6dSTuong Lien
748fc1b6d6dSTuong Lien /* Make sure message len at least 4-byte aligned */
749fc1b6d6dSTuong Lien len = ALIGN(skb->len, 4);
750fc1b6d6dSTuong Lien tailen = len - skb->len + aead->authsize;
751fc1b6d6dSTuong Lien
752fc1b6d6dSTuong Lien /* Expand skb tail for authentication tag:
753fc1b6d6dSTuong Lien * As for simplicity, we'd have made sure skb having enough tailroom
754fc1b6d6dSTuong Lien * for authentication tag @skb allocation. Even when skb is nonlinear
755fc1b6d6dSTuong Lien * but there is no frag_list, it should be still fine!
756fc1b6d6dSTuong Lien * Otherwise, we must cow it to be a writable buffer with the tailroom.
757fc1b6d6dSTuong Lien */
758fc1b6d6dSTuong Lien SKB_LINEAR_ASSERT(skb);
759fc1b6d6dSTuong Lien if (tailen > skb_tailroom(skb)) {
760f779bf79STuong Lien pr_debug("TX(): skb tailroom is not enough: %d, requires: %d\n",
761fc1b6d6dSTuong Lien skb_tailroom(skb), tailen);
762fc1b6d6dSTuong Lien }
763fc1b6d6dSTuong Lien
764fc1b6d6dSTuong Lien nsg = skb_cow_data(skb, tailen, &trailer);
765fc1b6d6dSTuong Lien if (unlikely(nsg < 0)) {
766fc1b6d6dSTuong Lien pr_err("TX: skb_cow_data() returned %d\n", nsg);
767fc1b6d6dSTuong Lien return nsg;
768fc1b6d6dSTuong Lien }
769fc1b6d6dSTuong Lien
770fc1b6d6dSTuong Lien pskb_put(skb, trailer, tailen);
771fc1b6d6dSTuong Lien
772fc1b6d6dSTuong Lien /* Allocate memory for the AEAD operation */
773fc1b6d6dSTuong Lien ctx = tipc_aead_mem_alloc(tfm, sizeof(*tx_ctx), &iv, &req, &sg, nsg);
774fc1b6d6dSTuong Lien if (unlikely(!ctx))
775fc1b6d6dSTuong Lien return -ENOMEM;
776fc1b6d6dSTuong Lien TIPC_SKB_CB(skb)->crypto_ctx = ctx;
777fc1b6d6dSTuong Lien
778fc1b6d6dSTuong Lien /* Map skb to the sg lists */
779fc1b6d6dSTuong Lien sg_init_table(sg, nsg);
780fc1b6d6dSTuong Lien rc = skb_to_sgvec(skb, sg, 0, skb->len);
781fc1b6d6dSTuong Lien if (unlikely(rc < 0)) {
782fc1b6d6dSTuong Lien pr_err("TX: skb_to_sgvec() returned %d, nsg %d!\n", rc, nsg);
783fc1b6d6dSTuong Lien goto exit;
784fc1b6d6dSTuong Lien }
785fc1b6d6dSTuong Lien
786fc1b6d6dSTuong Lien /* Prepare IV: [SALT (4 octets)][SEQNO (8 octets)]
787fc1b6d6dSTuong Lien * In case we're in cluster-key mode, SALT is varied by xor-ing with
788fc1b6d6dSTuong Lien * the source address (or w0 of id), otherwise with the dest address
789fc1b6d6dSTuong Lien * if dest is known.
790fc1b6d6dSTuong Lien */
791fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)skb->data;
792fc1b6d6dSTuong Lien salt = aead->salt;
793fc1b6d6dSTuong Lien if (aead->mode == CLUSTER_KEY)
79497bc84bbSHoang Huu Le salt ^= __be32_to_cpu(ehdr->addr);
795fc1b6d6dSTuong Lien else if (__dnode)
796fc1b6d6dSTuong Lien salt ^= tipc_node_get_addr(__dnode);
797fc1b6d6dSTuong Lien memcpy(iv, &salt, 4);
798fc1b6d6dSTuong Lien memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
799fc1b6d6dSTuong Lien
800fc1b6d6dSTuong Lien /* Prepare request */
801fc1b6d6dSTuong Lien ehsz = tipc_ehdr_size(ehdr);
802fc1b6d6dSTuong Lien aead_request_set_tfm(req, tfm);
803fc1b6d6dSTuong Lien aead_request_set_ad(req, ehsz);
804fc1b6d6dSTuong Lien aead_request_set_crypt(req, sg, sg, len - ehsz, iv);
805fc1b6d6dSTuong Lien
806fc1b6d6dSTuong Lien /* Set callback function & data */
807fc1b6d6dSTuong Lien aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
808fc1b6d6dSTuong Lien tipc_aead_encrypt_done, skb);
809fc1b6d6dSTuong Lien tx_ctx = (struct tipc_crypto_tx_ctx *)ctx;
810fc1b6d6dSTuong Lien tx_ctx->aead = aead;
811fc1b6d6dSTuong Lien tx_ctx->bearer = b;
812fc1b6d6dSTuong Lien memcpy(&tx_ctx->dst, dst, sizeof(*dst));
813fc1b6d6dSTuong Lien
814fc1b6d6dSTuong Lien /* Hold bearer */
815fc1b6d6dSTuong Lien if (unlikely(!tipc_bearer_hold(b))) {
816fc1b6d6dSTuong Lien rc = -ENODEV;
817fc1b6d6dSTuong Lien goto exit;
818fc1b6d6dSTuong Lien }
819fc1b6d6dSTuong Lien
820fc1b6d6dSTuong Lien /* Now, do encrypt */
821fc1b6d6dSTuong Lien rc = crypto_aead_encrypt(req);
822fc1b6d6dSTuong Lien if (rc == -EINPROGRESS || rc == -EBUSY)
823fc1b6d6dSTuong Lien return rc;
824fc1b6d6dSTuong Lien
825fc1b6d6dSTuong Lien tipc_bearer_put(b);
826fc1b6d6dSTuong Lien
827fc1b6d6dSTuong Lien exit:
828fc1b6d6dSTuong Lien kfree(ctx);
829fc1b6d6dSTuong Lien TIPC_SKB_CB(skb)->crypto_ctx = NULL;
830fc1b6d6dSTuong Lien return rc;
831fc1b6d6dSTuong Lien }
832fc1b6d6dSTuong Lien
tipc_aead_encrypt_done(void * data,int err)83365cb4657SHerbert Xu static void tipc_aead_encrypt_done(void *data, int err)
834fc1b6d6dSTuong Lien {
83565cb4657SHerbert Xu struct sk_buff *skb = data;
836fc1b6d6dSTuong Lien struct tipc_crypto_tx_ctx *tx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
837fc1b6d6dSTuong Lien struct tipc_bearer *b = tx_ctx->bearer;
838fc1b6d6dSTuong Lien struct tipc_aead *aead = tx_ctx->aead;
839fc1b6d6dSTuong Lien struct tipc_crypto *tx = aead->crypto;
840fc1b6d6dSTuong Lien struct net *net = tx->net;
841fc1b6d6dSTuong Lien
842fc1b6d6dSTuong Lien switch (err) {
843fc1b6d6dSTuong Lien case 0:
844fc1b6d6dSTuong Lien this_cpu_inc(tx->stats->stat[STAT_ASYNC_OK]);
845f6db9096SXin Long rcu_read_lock();
846fc1b6d6dSTuong Lien if (likely(test_bit(0, &b->up)))
847fc1b6d6dSTuong Lien b->media->send_msg(net, skb, b, &tx_ctx->dst);
848fc1b6d6dSTuong Lien else
849fc1b6d6dSTuong Lien kfree_skb(skb);
850f6db9096SXin Long rcu_read_unlock();
851fc1b6d6dSTuong Lien break;
852fc1b6d6dSTuong Lien case -EINPROGRESS:
853fc1b6d6dSTuong Lien return;
854fc1b6d6dSTuong Lien default:
855fc1b6d6dSTuong Lien this_cpu_inc(tx->stats->stat[STAT_ASYNC_NOK]);
856fc1b6d6dSTuong Lien kfree_skb(skb);
857fc1b6d6dSTuong Lien break;
858fc1b6d6dSTuong Lien }
859fc1b6d6dSTuong Lien
860fc1b6d6dSTuong Lien kfree(tx_ctx);
861fc1b6d6dSTuong Lien tipc_bearer_put(b);
862fc1b6d6dSTuong Lien tipc_aead_put(aead);
863fc1b6d6dSTuong Lien }
864fc1b6d6dSTuong Lien
865fc1b6d6dSTuong Lien /**
866fc1b6d6dSTuong Lien * tipc_aead_decrypt - Decrypt an encrypted message
867fc1b6d6dSTuong Lien * @net: struct net
868fc1b6d6dSTuong Lien * @aead: TIPC AEAD for the message decryption
869fc1b6d6dSTuong Lien * @skb: the input/output skb
870fc1b6d6dSTuong Lien * @b: TIPC bearer where the message has been received
871fc1b6d6dSTuong Lien *
872fc1b6d6dSTuong Lien * Return:
873637b77fdSRandy Dunlap * * 0 : if the decryption has completed
874637b77fdSRandy Dunlap * * -EINPROGRESS/-EBUSY : if a callback will be performed
875637b77fdSRandy Dunlap * * < 0 : the decryption has failed
876fc1b6d6dSTuong Lien */
tipc_aead_decrypt(struct net * net,struct tipc_aead * aead,struct sk_buff * skb,struct tipc_bearer * b)877fc1b6d6dSTuong Lien static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
878fc1b6d6dSTuong Lien struct sk_buff *skb, struct tipc_bearer *b)
879fc1b6d6dSTuong Lien {
880fc1b6d6dSTuong Lien struct tipc_crypto_rx_ctx *rx_ctx;
881fc1b6d6dSTuong Lien struct aead_request *req;
882fc1b6d6dSTuong Lien struct crypto_aead *tfm;
883fc1b6d6dSTuong Lien struct sk_buff *unused;
884fc1b6d6dSTuong Lien struct scatterlist *sg;
885fc1b6d6dSTuong Lien struct tipc_ehdr *ehdr;
886fc1b6d6dSTuong Lien int ehsz, nsg, rc;
887fc1b6d6dSTuong Lien void *ctx;
888fc1b6d6dSTuong Lien u32 salt;
889fc1b6d6dSTuong Lien u8 *iv;
890fc1b6d6dSTuong Lien
891fc1b6d6dSTuong Lien if (unlikely(!aead))
892fc1b6d6dSTuong Lien return -ENOKEY;
893fc1b6d6dSTuong Lien
894fc1b6d6dSTuong Lien nsg = skb_cow_data(skb, 0, &unused);
895fc1b6d6dSTuong Lien if (unlikely(nsg < 0)) {
896fc1b6d6dSTuong Lien pr_err("RX: skb_cow_data() returned %d\n", nsg);
897fc1b6d6dSTuong Lien return nsg;
898fc1b6d6dSTuong Lien }
899fc1b6d6dSTuong Lien
900fc1b6d6dSTuong Lien /* Allocate memory for the AEAD operation */
901fc1b6d6dSTuong Lien tfm = tipc_aead_tfm_next(aead);
902fc1b6d6dSTuong Lien ctx = tipc_aead_mem_alloc(tfm, sizeof(*rx_ctx), &iv, &req, &sg, nsg);
903fc1b6d6dSTuong Lien if (unlikely(!ctx))
904fc1b6d6dSTuong Lien return -ENOMEM;
905fc1b6d6dSTuong Lien TIPC_SKB_CB(skb)->crypto_ctx = ctx;
906fc1b6d6dSTuong Lien
907fc1b6d6dSTuong Lien /* Map skb to the sg lists */
908fc1b6d6dSTuong Lien sg_init_table(sg, nsg);
909fc1b6d6dSTuong Lien rc = skb_to_sgvec(skb, sg, 0, skb->len);
910fc1b6d6dSTuong Lien if (unlikely(rc < 0)) {
911fc1b6d6dSTuong Lien pr_err("RX: skb_to_sgvec() returned %d, nsg %d\n", rc, nsg);
912fc1b6d6dSTuong Lien goto exit;
913fc1b6d6dSTuong Lien }
914fc1b6d6dSTuong Lien
915fc1b6d6dSTuong Lien /* Reconstruct IV: */
916fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)skb->data;
917fc1b6d6dSTuong Lien salt = aead->salt;
918fc1b6d6dSTuong Lien if (aead->mode == CLUSTER_KEY)
91997bc84bbSHoang Huu Le salt ^= __be32_to_cpu(ehdr->addr);
920fc1b6d6dSTuong Lien else if (ehdr->destined)
921fc1b6d6dSTuong Lien salt ^= tipc_own_addr(net);
922fc1b6d6dSTuong Lien memcpy(iv, &salt, 4);
923fc1b6d6dSTuong Lien memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
924fc1b6d6dSTuong Lien
925fc1b6d6dSTuong Lien /* Prepare request */
926fc1b6d6dSTuong Lien ehsz = tipc_ehdr_size(ehdr);
927fc1b6d6dSTuong Lien aead_request_set_tfm(req, tfm);
928fc1b6d6dSTuong Lien aead_request_set_ad(req, ehsz);
929fc1b6d6dSTuong Lien aead_request_set_crypt(req, sg, sg, skb->len - ehsz, iv);
930fc1b6d6dSTuong Lien
931fc1b6d6dSTuong Lien /* Set callback function & data */
932fc1b6d6dSTuong Lien aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
933fc1b6d6dSTuong Lien tipc_aead_decrypt_done, skb);
934fc1b6d6dSTuong Lien rx_ctx = (struct tipc_crypto_rx_ctx *)ctx;
935fc1b6d6dSTuong Lien rx_ctx->aead = aead;
936fc1b6d6dSTuong Lien rx_ctx->bearer = b;
937fc1b6d6dSTuong Lien
938fc1b6d6dSTuong Lien /* Hold bearer */
939fc1b6d6dSTuong Lien if (unlikely(!tipc_bearer_hold(b))) {
940fc1b6d6dSTuong Lien rc = -ENODEV;
941fc1b6d6dSTuong Lien goto exit;
942fc1b6d6dSTuong Lien }
943fc1b6d6dSTuong Lien
944fc1b6d6dSTuong Lien /* Now, do decrypt */
945fc1b6d6dSTuong Lien rc = crypto_aead_decrypt(req);
946fc1b6d6dSTuong Lien if (rc == -EINPROGRESS || rc == -EBUSY)
947fc1b6d6dSTuong Lien return rc;
948fc1b6d6dSTuong Lien
949fc1b6d6dSTuong Lien tipc_bearer_put(b);
950fc1b6d6dSTuong Lien
951fc1b6d6dSTuong Lien exit:
952fc1b6d6dSTuong Lien kfree(ctx);
953fc1b6d6dSTuong Lien TIPC_SKB_CB(skb)->crypto_ctx = NULL;
954fc1b6d6dSTuong Lien return rc;
955fc1b6d6dSTuong Lien }
956fc1b6d6dSTuong Lien
tipc_aead_decrypt_done(void * data,int err)95765cb4657SHerbert Xu static void tipc_aead_decrypt_done(void *data, int err)
958fc1b6d6dSTuong Lien {
95965cb4657SHerbert Xu struct sk_buff *skb = data;
960fc1b6d6dSTuong Lien struct tipc_crypto_rx_ctx *rx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
961fc1b6d6dSTuong Lien struct tipc_bearer *b = rx_ctx->bearer;
962fc1b6d6dSTuong Lien struct tipc_aead *aead = rx_ctx->aead;
963fc1b6d6dSTuong Lien struct tipc_crypto_stats __percpu *stats = aead->crypto->stats;
964fc1b6d6dSTuong Lien struct net *net = aead->crypto->net;
965fc1b6d6dSTuong Lien
966fc1b6d6dSTuong Lien switch (err) {
967fc1b6d6dSTuong Lien case 0:
968fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_ASYNC_OK]);
969fc1b6d6dSTuong Lien break;
970fc1b6d6dSTuong Lien case -EINPROGRESS:
971fc1b6d6dSTuong Lien return;
972fc1b6d6dSTuong Lien default:
973fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_ASYNC_NOK]);
974fc1b6d6dSTuong Lien break;
975fc1b6d6dSTuong Lien }
976fc1b6d6dSTuong Lien
977fc1b6d6dSTuong Lien kfree(rx_ctx);
978fc1b6d6dSTuong Lien tipc_crypto_rcv_complete(net, aead, b, &skb, err);
979fc1b6d6dSTuong Lien if (likely(skb)) {
980fc1b6d6dSTuong Lien if (likely(test_bit(0, &b->up)))
981fc1b6d6dSTuong Lien tipc_rcv(net, skb, b);
982fc1b6d6dSTuong Lien else
983fc1b6d6dSTuong Lien kfree_skb(skb);
984fc1b6d6dSTuong Lien }
985fc1b6d6dSTuong Lien
986fc1b6d6dSTuong Lien tipc_bearer_put(b);
987fc1b6d6dSTuong Lien }
988fc1b6d6dSTuong Lien
tipc_ehdr_size(struct tipc_ehdr * ehdr)989fc1b6d6dSTuong Lien static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr)
990fc1b6d6dSTuong Lien {
991fc1b6d6dSTuong Lien return (ehdr->user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
992fc1b6d6dSTuong Lien }
993fc1b6d6dSTuong Lien
994fc1b6d6dSTuong Lien /**
995fc1b6d6dSTuong Lien * tipc_ehdr_validate - Validate an encryption message
996fc1b6d6dSTuong Lien * @skb: the message buffer
997fc1b6d6dSTuong Lien *
998637b77fdSRandy Dunlap * Return: "true" if this is a valid encryption message, otherwise "false"
999fc1b6d6dSTuong Lien */
tipc_ehdr_validate(struct sk_buff * skb)1000fc1b6d6dSTuong Lien bool tipc_ehdr_validate(struct sk_buff *skb)
1001fc1b6d6dSTuong Lien {
1002fc1b6d6dSTuong Lien struct tipc_ehdr *ehdr;
1003fc1b6d6dSTuong Lien int ehsz;
1004fc1b6d6dSTuong Lien
1005fc1b6d6dSTuong Lien if (unlikely(!pskb_may_pull(skb, EHDR_MIN_SIZE)))
1006fc1b6d6dSTuong Lien return false;
1007fc1b6d6dSTuong Lien
1008fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)skb->data;
1009fc1b6d6dSTuong Lien if (unlikely(ehdr->version != TIPC_EVERSION))
1010fc1b6d6dSTuong Lien return false;
1011fc1b6d6dSTuong Lien ehsz = tipc_ehdr_size(ehdr);
1012fc1b6d6dSTuong Lien if (unlikely(!pskb_may_pull(skb, ehsz)))
1013fc1b6d6dSTuong Lien return false;
1014fc1b6d6dSTuong Lien if (unlikely(skb->len <= ehsz + TIPC_AES_GCM_TAG_SIZE))
1015fc1b6d6dSTuong Lien return false;
1016fc1b6d6dSTuong Lien
1017fc1b6d6dSTuong Lien return true;
1018fc1b6d6dSTuong Lien }
1019fc1b6d6dSTuong Lien
1020fc1b6d6dSTuong Lien /**
1021fc1b6d6dSTuong Lien * tipc_ehdr_build - Build TIPC encryption message header
1022fc1b6d6dSTuong Lien * @net: struct net
1023fc1b6d6dSTuong Lien * @aead: TX AEAD key to be used for the message encryption
1024fc1b6d6dSTuong Lien * @tx_key: key id used for the message encryption
1025fc1b6d6dSTuong Lien * @skb: input/output message skb
1026fc1b6d6dSTuong Lien * @__rx: RX crypto handle if dest is "known"
1027fc1b6d6dSTuong Lien *
1028fc1b6d6dSTuong Lien * Return: the header size if the building is successful, otherwise < 0
1029fc1b6d6dSTuong Lien */
tipc_ehdr_build(struct net * net,struct tipc_aead * aead,u8 tx_key,struct sk_buff * skb,struct tipc_crypto * __rx)1030fc1b6d6dSTuong Lien static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
1031fc1b6d6dSTuong Lien u8 tx_key, struct sk_buff *skb,
1032fc1b6d6dSTuong Lien struct tipc_crypto *__rx)
1033fc1b6d6dSTuong Lien {
1034fc1b6d6dSTuong Lien struct tipc_msg *hdr = buf_msg(skb);
1035fc1b6d6dSTuong Lien struct tipc_ehdr *ehdr;
1036fc1b6d6dSTuong Lien u32 user = msg_user(hdr);
1037fc1b6d6dSTuong Lien u64 seqno;
1038fc1b6d6dSTuong Lien int ehsz;
1039fc1b6d6dSTuong Lien
1040fc1b6d6dSTuong Lien /* Make room for encryption header */
1041fc1b6d6dSTuong Lien ehsz = (user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
1042fc1b6d6dSTuong Lien WARN_ON(skb_headroom(skb) < ehsz);
1043fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)skb_push(skb, ehsz);
1044fc1b6d6dSTuong Lien
1045fc1b6d6dSTuong Lien /* Obtain a seqno first:
1046fc1b6d6dSTuong Lien * Use the key seqno (= cluster wise) if dest is unknown or we're in
1047fc1b6d6dSTuong Lien * cluster key mode, otherwise it's better for a per-peer seqno!
1048fc1b6d6dSTuong Lien */
1049fc1b6d6dSTuong Lien if (!__rx || aead->mode == CLUSTER_KEY)
1050fc1b6d6dSTuong Lien seqno = atomic64_inc_return(&aead->seqno);
1051fc1b6d6dSTuong Lien else
1052fc1b6d6dSTuong Lien seqno = atomic64_inc_return(&__rx->sndnxt);
1053fc1b6d6dSTuong Lien
1054fc1b6d6dSTuong Lien /* Revoke the key if seqno is wrapped around */
1055fc1b6d6dSTuong Lien if (unlikely(!seqno))
1056fc1b6d6dSTuong Lien return tipc_crypto_key_revoke(net, tx_key);
1057fc1b6d6dSTuong Lien
1058fc1b6d6dSTuong Lien /* Word 1-2 */
1059fc1b6d6dSTuong Lien ehdr->seqno = cpu_to_be64(seqno);
1060fc1b6d6dSTuong Lien
1061fc1b6d6dSTuong Lien /* Words 0, 3- */
1062fc1b6d6dSTuong Lien ehdr->version = TIPC_EVERSION;
1063fc1b6d6dSTuong Lien ehdr->user = 0;
1064fc1b6d6dSTuong Lien ehdr->keepalive = 0;
1065fc1b6d6dSTuong Lien ehdr->tx_key = tx_key;
1066fc1b6d6dSTuong Lien ehdr->destined = (__rx) ? 1 : 0;
1067fc1b6d6dSTuong Lien ehdr->rx_key_active = (__rx) ? __rx->key.active : 0;
10681ef6f7c9STuong Lien ehdr->rx_nokey = (__rx) ? __rx->nokey : 0;
1069daef1ee3STuong Lien ehdr->master_key = aead->crypto->key_master;
1070fc1b6d6dSTuong Lien ehdr->reserved_1 = 0;
1071fc1b6d6dSTuong Lien ehdr->reserved_2 = 0;
1072fc1b6d6dSTuong Lien
1073fc1b6d6dSTuong Lien switch (user) {
1074fc1b6d6dSTuong Lien case LINK_CONFIG:
1075fc1b6d6dSTuong Lien ehdr->user = LINK_CONFIG;
1076fc1b6d6dSTuong Lien memcpy(ehdr->id, tipc_own_id(net), NODE_ID_LEN);
1077fc1b6d6dSTuong Lien break;
1078fc1b6d6dSTuong Lien default:
1079fc1b6d6dSTuong Lien if (user == LINK_PROTOCOL && msg_type(hdr) == STATE_MSG) {
1080fc1b6d6dSTuong Lien ehdr->user = LINK_PROTOCOL;
1081fc1b6d6dSTuong Lien ehdr->keepalive = msg_is_keepalive(hdr);
1082fc1b6d6dSTuong Lien }
1083fc1b6d6dSTuong Lien ehdr->addr = hdr->hdr[3];
1084fc1b6d6dSTuong Lien break;
1085fc1b6d6dSTuong Lien }
1086fc1b6d6dSTuong Lien
1087fc1b6d6dSTuong Lien return ehsz;
1088fc1b6d6dSTuong Lien }
1089fc1b6d6dSTuong Lien
tipc_crypto_key_set_state(struct tipc_crypto * c,u8 new_passive,u8 new_active,u8 new_pending)1090fc1b6d6dSTuong Lien static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
1091fc1b6d6dSTuong Lien u8 new_passive,
1092fc1b6d6dSTuong Lien u8 new_active,
1093fc1b6d6dSTuong Lien u8 new_pending)
1094fc1b6d6dSTuong Lien {
1095fc1b6d6dSTuong Lien struct tipc_key old = c->key;
1096fc1b6d6dSTuong Lien char buf[32];
1097fc1b6d6dSTuong Lien
1098fc1b6d6dSTuong Lien c->key.keys = ((new_passive & KEY_MASK) << (KEY_BITS * 2)) |
1099fc1b6d6dSTuong Lien ((new_active & KEY_MASK) << (KEY_BITS)) |
1100fc1b6d6dSTuong Lien ((new_pending & KEY_MASK));
1101fc1b6d6dSTuong Lien
1102f779bf79STuong Lien pr_debug("%s: key changing %s ::%pS\n", c->name,
1103fc1b6d6dSTuong Lien tipc_key_change_dump(old, c->key, buf),
1104fc1b6d6dSTuong Lien __builtin_return_address(0));
1105fc1b6d6dSTuong Lien }
1106fc1b6d6dSTuong Lien
1107fc1b6d6dSTuong Lien /**
1108fc1b6d6dSTuong Lien * tipc_crypto_key_init - Initiate a new user / AEAD key
1109fc1b6d6dSTuong Lien * @c: TIPC crypto to which new key is attached
1110fc1b6d6dSTuong Lien * @ukey: the user key
1111fc1b6d6dSTuong Lien * @mode: the key mode (CLUSTER_KEY or PER_NODE_KEY)
1112daef1ee3STuong Lien * @master_key: specify this is a cluster master key
1113fc1b6d6dSTuong Lien *
1114fc1b6d6dSTuong Lien * A new TIPC AEAD key will be allocated and initiated with the specified user
1115fc1b6d6dSTuong Lien * key, then attached to the TIPC crypto.
1116fc1b6d6dSTuong Lien *
1117fc1b6d6dSTuong Lien * Return: new key id in case of success, otherwise: < 0
1118fc1b6d6dSTuong Lien */
tipc_crypto_key_init(struct tipc_crypto * c,struct tipc_aead_key * ukey,u8 mode,bool master_key)1119fc1b6d6dSTuong Lien int tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey,
1120daef1ee3STuong Lien u8 mode, bool master_key)
1121fc1b6d6dSTuong Lien {
1122fc1b6d6dSTuong Lien struct tipc_aead *aead = NULL;
1123fc1b6d6dSTuong Lien int rc = 0;
1124fc1b6d6dSTuong Lien
1125fc1b6d6dSTuong Lien /* Initiate with the new user key */
1126fc1b6d6dSTuong Lien rc = tipc_aead_init(&aead, ukey, mode);
1127fc1b6d6dSTuong Lien
1128fc1b6d6dSTuong Lien /* Attach it to the crypto */
1129fc1b6d6dSTuong Lien if (likely(!rc)) {
1130daef1ee3STuong Lien rc = tipc_crypto_key_attach(c, aead, 0, master_key);
1131fc1b6d6dSTuong Lien if (rc < 0)
1132fc1b6d6dSTuong Lien tipc_aead_free(&aead->rcu);
1133fc1b6d6dSTuong Lien }
1134fc1b6d6dSTuong Lien
1135fc1b6d6dSTuong Lien return rc;
1136fc1b6d6dSTuong Lien }
1137fc1b6d6dSTuong Lien
1138fc1b6d6dSTuong Lien /**
1139fc1b6d6dSTuong Lien * tipc_crypto_key_attach - Attach a new AEAD key to TIPC crypto
1140fc1b6d6dSTuong Lien * @c: TIPC crypto to which the new AEAD key is attached
1141fc1b6d6dSTuong Lien * @aead: the new AEAD key pointer
1142fc1b6d6dSTuong Lien * @pos: desired slot in the crypto key array, = 0 if any!
1143daef1ee3STuong Lien * @master_key: specify this is a cluster master key
1144fc1b6d6dSTuong Lien *
1145fc1b6d6dSTuong Lien * Return: new key id in case of success, otherwise: -EBUSY
1146fc1b6d6dSTuong Lien */
tipc_crypto_key_attach(struct tipc_crypto * c,struct tipc_aead * aead,u8 pos,bool master_key)1147fc1b6d6dSTuong Lien static int tipc_crypto_key_attach(struct tipc_crypto *c,
1148daef1ee3STuong Lien struct tipc_aead *aead, u8 pos,
1149daef1ee3STuong Lien bool master_key)
1150fc1b6d6dSTuong Lien {
1151fc1b6d6dSTuong Lien struct tipc_key key;
1152fc1b6d6dSTuong Lien int rc = -EBUSY;
1153f779bf79STuong Lien u8 new_key;
1154fc1b6d6dSTuong Lien
1155fc1b6d6dSTuong Lien spin_lock_bh(&c->lock);
1156fc1b6d6dSTuong Lien key = c->key;
1157daef1ee3STuong Lien if (master_key) {
1158daef1ee3STuong Lien new_key = KEY_MASTER;
1159daef1ee3STuong Lien goto attach;
1160daef1ee3STuong Lien }
1161fc1b6d6dSTuong Lien if (key.active && key.passive)
1162fc1b6d6dSTuong Lien goto exit;
1163fc1b6d6dSTuong Lien if (key.pending) {
1164fc1b6d6dSTuong Lien if (tipc_aead_users(c->aead[key.pending]) > 0)
1165fc1b6d6dSTuong Lien goto exit;
1166f779bf79STuong Lien /* if (pos): ok with replacing, will be aligned when needed */
1167fc1b6d6dSTuong Lien /* Replace it */
1168f779bf79STuong Lien new_key = key.pending;
1169fc1b6d6dSTuong Lien } else {
1170fc1b6d6dSTuong Lien if (pos) {
1171fc1b6d6dSTuong Lien if (key.active && pos != key_next(key.active)) {
1172f779bf79STuong Lien key.passive = pos;
1173f779bf79STuong Lien new_key = pos;
1174fc1b6d6dSTuong Lien goto attach;
1175fc1b6d6dSTuong Lien } else if (!key.active && !key.passive) {
1176f779bf79STuong Lien key.pending = pos;
1177f779bf79STuong Lien new_key = pos;
1178fc1b6d6dSTuong Lien goto attach;
1179fc1b6d6dSTuong Lien }
1180fc1b6d6dSTuong Lien }
1181f779bf79STuong Lien key.pending = key_next(key.active ?: key.passive);
1182f779bf79STuong Lien new_key = key.pending;
1183fc1b6d6dSTuong Lien }
1184fc1b6d6dSTuong Lien
1185fc1b6d6dSTuong Lien attach:
1186fc1b6d6dSTuong Lien aead->crypto = c;
11871ef6f7c9STuong Lien aead->gen = (is_tx(c)) ? ++c->key_gen : c->key_gen;
1188fc1b6d6dSTuong Lien tipc_aead_rcu_replace(c->aead[new_key], aead, &c->lock);
1189f779bf79STuong Lien if (likely(c->key.keys != key.keys))
1190f779bf79STuong Lien tipc_crypto_key_set_state(c, key.passive, key.active,
1191f779bf79STuong Lien key.pending);
1192fc1b6d6dSTuong Lien c->working = 1;
11931ef6f7c9STuong Lien c->nokey = 0;
1194daef1ee3STuong Lien c->key_master |= master_key;
1195fc1b6d6dSTuong Lien rc = new_key;
1196fc1b6d6dSTuong Lien
1197fc1b6d6dSTuong Lien exit:
1198fc1b6d6dSTuong Lien spin_unlock_bh(&c->lock);
1199fc1b6d6dSTuong Lien return rc;
1200fc1b6d6dSTuong Lien }
1201fc1b6d6dSTuong Lien
tipc_crypto_key_flush(struct tipc_crypto * c)1202fc1b6d6dSTuong Lien void tipc_crypto_key_flush(struct tipc_crypto *c)
1203fc1b6d6dSTuong Lien {
12041ef6f7c9STuong Lien struct tipc_crypto *tx, *rx;
1205fc1b6d6dSTuong Lien int k;
1206fc1b6d6dSTuong Lien
1207fc1b6d6dSTuong Lien spin_lock_bh(&c->lock);
12081ef6f7c9STuong Lien if (is_rx(c)) {
12091ef6f7c9STuong Lien /* Try to cancel pending work */
12101ef6f7c9STuong Lien rx = c;
12111ef6f7c9STuong Lien tx = tipc_net(rx->net)->crypto_tx;
12121ef6f7c9STuong Lien if (cancel_delayed_work(&rx->work)) {
12131ef6f7c9STuong Lien kfree(rx->skey);
12141ef6f7c9STuong Lien rx->skey = NULL;
12151ef6f7c9STuong Lien atomic_xchg(&rx->key_distr, 0);
12161ef6f7c9STuong Lien tipc_node_put(rx->node);
12171ef6f7c9STuong Lien }
12181ef6f7c9STuong Lien /* RX stopping => decrease TX key users if any */
12191ef6f7c9STuong Lien k = atomic_xchg(&rx->peer_rx_active, 0);
12201ef6f7c9STuong Lien if (k) {
12211ef6f7c9STuong Lien tipc_aead_users_dec(tx->aead[k], 0);
12221ef6f7c9STuong Lien /* Mark the point TX key users changed */
12231ef6f7c9STuong Lien tx->timer1 = jiffies;
12241ef6f7c9STuong Lien }
12251ef6f7c9STuong Lien }
12261ef6f7c9STuong Lien
1227daef1ee3STuong Lien c->flags = 0;
1228fc1b6d6dSTuong Lien tipc_crypto_key_set_state(c, 0, 0, 0);
1229fc1b6d6dSTuong Lien for (k = KEY_MIN; k <= KEY_MAX; k++)
1230fc1b6d6dSTuong Lien tipc_crypto_key_detach(c->aead[k], &c->lock);
1231fc1b6d6dSTuong Lien atomic64_set(&c->sndnxt, 0);
1232fc1b6d6dSTuong Lien spin_unlock_bh(&c->lock);
1233fc1b6d6dSTuong Lien }
1234fc1b6d6dSTuong Lien
1235fc1b6d6dSTuong Lien /**
1236fc1b6d6dSTuong Lien * tipc_crypto_key_try_align - Align RX keys if possible
1237fc1b6d6dSTuong Lien * @rx: RX crypto handle
1238fc1b6d6dSTuong Lien * @new_pending: new pending slot if aligned (= TX key from peer)
1239fc1b6d6dSTuong Lien *
1240fc1b6d6dSTuong Lien * Peer has used an unknown key slot, this only happens when peer has left and
1241fc1b6d6dSTuong Lien * rejoned, or we are newcomer.
1242fc1b6d6dSTuong Lien * That means, there must be no active key but a pending key at unaligned slot.
1243fc1b6d6dSTuong Lien * If so, we try to move the pending key to the new slot.
1244fc1b6d6dSTuong Lien * Note: A potential passive key can exist, it will be shifted correspondingly!
1245fc1b6d6dSTuong Lien *
1246fc1b6d6dSTuong Lien * Return: "true" if key is successfully aligned, otherwise "false"
1247fc1b6d6dSTuong Lien */
tipc_crypto_key_try_align(struct tipc_crypto * rx,u8 new_pending)1248fc1b6d6dSTuong Lien static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending)
1249fc1b6d6dSTuong Lien {
1250fc1b6d6dSTuong Lien struct tipc_aead *tmp1, *tmp2 = NULL;
1251fc1b6d6dSTuong Lien struct tipc_key key;
1252fc1b6d6dSTuong Lien bool aligned = false;
1253fc1b6d6dSTuong Lien u8 new_passive = 0;
1254fc1b6d6dSTuong Lien int x;
1255fc1b6d6dSTuong Lien
1256fc1b6d6dSTuong Lien spin_lock(&rx->lock);
1257fc1b6d6dSTuong Lien key = rx->key;
1258fc1b6d6dSTuong Lien if (key.pending == new_pending) {
1259fc1b6d6dSTuong Lien aligned = true;
1260fc1b6d6dSTuong Lien goto exit;
1261fc1b6d6dSTuong Lien }
1262fc1b6d6dSTuong Lien if (key.active)
1263fc1b6d6dSTuong Lien goto exit;
1264fc1b6d6dSTuong Lien if (!key.pending)
1265fc1b6d6dSTuong Lien goto exit;
1266fc1b6d6dSTuong Lien if (tipc_aead_users(rx->aead[key.pending]) > 0)
1267fc1b6d6dSTuong Lien goto exit;
1268fc1b6d6dSTuong Lien
1269fc1b6d6dSTuong Lien /* Try to "isolate" this pending key first */
1270fc1b6d6dSTuong Lien tmp1 = tipc_aead_rcu_ptr(rx->aead[key.pending], &rx->lock);
1271fc1b6d6dSTuong Lien if (!refcount_dec_if_one(&tmp1->refcnt))
1272fc1b6d6dSTuong Lien goto exit;
1273fc1b6d6dSTuong Lien rcu_assign_pointer(rx->aead[key.pending], NULL);
1274fc1b6d6dSTuong Lien
1275fc1b6d6dSTuong Lien /* Move passive key if any */
1276fc1b6d6dSTuong Lien if (key.passive) {
12771a271ebbSPaul E. McKenney tmp2 = rcu_replace_pointer(rx->aead[key.passive], tmp2, lockdep_is_held(&rx->lock));
1278fc1b6d6dSTuong Lien x = (key.passive - key.pending + new_pending) % KEY_MAX;
1279fc1b6d6dSTuong Lien new_passive = (x <= 0) ? x + KEY_MAX : x;
1280fc1b6d6dSTuong Lien }
1281fc1b6d6dSTuong Lien
1282fc1b6d6dSTuong Lien /* Re-allocate the key(s) */
1283fc1b6d6dSTuong Lien tipc_crypto_key_set_state(rx, new_passive, 0, new_pending);
1284fc1b6d6dSTuong Lien rcu_assign_pointer(rx->aead[new_pending], tmp1);
1285fc1b6d6dSTuong Lien if (new_passive)
1286fc1b6d6dSTuong Lien rcu_assign_pointer(rx->aead[new_passive], tmp2);
1287fc1b6d6dSTuong Lien refcount_set(&tmp1->refcnt, 1);
1288fc1b6d6dSTuong Lien aligned = true;
1289f779bf79STuong Lien pr_info_ratelimited("%s: key[%d] -> key[%d]\n", rx->name, key.pending,
1290f779bf79STuong Lien new_pending);
1291fc1b6d6dSTuong Lien
1292fc1b6d6dSTuong Lien exit:
1293fc1b6d6dSTuong Lien spin_unlock(&rx->lock);
1294fc1b6d6dSTuong Lien return aligned;
1295fc1b6d6dSTuong Lien }
1296fc1b6d6dSTuong Lien
1297fc1b6d6dSTuong Lien /**
1298fc1b6d6dSTuong Lien * tipc_crypto_key_pick_tx - Pick one TX key for message decryption
1299fc1b6d6dSTuong Lien * @tx: TX crypto handle
1300fc1b6d6dSTuong Lien * @rx: RX crypto handle (can be NULL)
1301fc1b6d6dSTuong Lien * @skb: the message skb which will be decrypted later
1302daef1ee3STuong Lien * @tx_key: peer TX key id
1303fc1b6d6dSTuong Lien *
1304fc1b6d6dSTuong Lien * This function looks up the existing TX keys and pick one which is suitable
1305fc1b6d6dSTuong Lien * for the message decryption, that must be a cluster key and not used before
1306fc1b6d6dSTuong Lien * on the same message (i.e. recursive).
1307fc1b6d6dSTuong Lien *
1308fc1b6d6dSTuong Lien * Return: the TX AEAD key handle in case of success, otherwise NULL
1309fc1b6d6dSTuong Lien */
tipc_crypto_key_pick_tx(struct tipc_crypto * tx,struct tipc_crypto * rx,struct sk_buff * skb,u8 tx_key)1310fc1b6d6dSTuong Lien static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
1311fc1b6d6dSTuong Lien struct tipc_crypto *rx,
1312daef1ee3STuong Lien struct sk_buff *skb,
1313daef1ee3STuong Lien u8 tx_key)
1314fc1b6d6dSTuong Lien {
1315fc1b6d6dSTuong Lien struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(skb);
1316fc1b6d6dSTuong Lien struct tipc_aead *aead = NULL;
1317fc1b6d6dSTuong Lien struct tipc_key key = tx->key;
1318fc1b6d6dSTuong Lien u8 k, i = 0;
1319fc1b6d6dSTuong Lien
1320fc1b6d6dSTuong Lien /* Initialize data if not yet */
1321fc1b6d6dSTuong Lien if (!skb_cb->tx_clone_deferred) {
1322fc1b6d6dSTuong Lien skb_cb->tx_clone_deferred = 1;
1323fc1b6d6dSTuong Lien memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1324fc1b6d6dSTuong Lien }
1325fc1b6d6dSTuong Lien
1326fc1b6d6dSTuong Lien skb_cb->tx_clone_ctx.rx = rx;
1327fc1b6d6dSTuong Lien if (++skb_cb->tx_clone_ctx.recurs > 2)
1328fc1b6d6dSTuong Lien return NULL;
1329fc1b6d6dSTuong Lien
1330fc1b6d6dSTuong Lien /* Pick one TX key */
1331fc1b6d6dSTuong Lien spin_lock(&tx->lock);
1332daef1ee3STuong Lien if (tx_key == KEY_MASTER) {
1333daef1ee3STuong Lien aead = tipc_aead_rcu_ptr(tx->aead[KEY_MASTER], &tx->lock);
1334daef1ee3STuong Lien goto done;
1335daef1ee3STuong Lien }
1336fc1b6d6dSTuong Lien do {
1337fc1b6d6dSTuong Lien k = (i == 0) ? key.pending :
1338fc1b6d6dSTuong Lien ((i == 1) ? key.active : key.passive);
1339fc1b6d6dSTuong Lien if (!k)
1340fc1b6d6dSTuong Lien continue;
1341fc1b6d6dSTuong Lien aead = tipc_aead_rcu_ptr(tx->aead[k], &tx->lock);
1342fc1b6d6dSTuong Lien if (!aead)
1343fc1b6d6dSTuong Lien continue;
1344fc1b6d6dSTuong Lien if (aead->mode != CLUSTER_KEY ||
1345fc1b6d6dSTuong Lien aead == skb_cb->tx_clone_ctx.last) {
1346fc1b6d6dSTuong Lien aead = NULL;
1347fc1b6d6dSTuong Lien continue;
1348fc1b6d6dSTuong Lien }
1349fc1b6d6dSTuong Lien /* Ok, found one cluster key */
1350fc1b6d6dSTuong Lien skb_cb->tx_clone_ctx.last = aead;
1351fc1b6d6dSTuong Lien WARN_ON(skb->next);
1352fc1b6d6dSTuong Lien skb->next = skb_clone(skb, GFP_ATOMIC);
1353fc1b6d6dSTuong Lien if (unlikely(!skb->next))
1354fc1b6d6dSTuong Lien pr_warn("Failed to clone skb for next round if any\n");
1355fc1b6d6dSTuong Lien break;
1356fc1b6d6dSTuong Lien } while (++i < 3);
1357daef1ee3STuong Lien
1358daef1ee3STuong Lien done:
1359daef1ee3STuong Lien if (likely(aead))
1360daef1ee3STuong Lien WARN_ON(!refcount_inc_not_zero(&aead->refcnt));
1361fc1b6d6dSTuong Lien spin_unlock(&tx->lock);
1362fc1b6d6dSTuong Lien
1363fc1b6d6dSTuong Lien return aead;
1364fc1b6d6dSTuong Lien }
1365fc1b6d6dSTuong Lien
1366fc1b6d6dSTuong Lien /**
1367fc1b6d6dSTuong Lien * tipc_crypto_key_synch: Synch own key data according to peer key status
1368fc1b6d6dSTuong Lien * @rx: RX crypto handle
1369f779bf79STuong Lien * @skb: TIPCv2 message buffer (incl. the ehdr from peer)
1370fc1b6d6dSTuong Lien *
1371fc1b6d6dSTuong Lien * This function updates the peer node related data as the peer RX active key
1372fc1b6d6dSTuong Lien * has changed, so the number of TX keys' users on this node are increased and
1373fc1b6d6dSTuong Lien * decreased correspondingly.
1374fc1b6d6dSTuong Lien *
1375daef1ee3STuong Lien * It also considers if peer has no key, then we need to make own master key
13761ef6f7c9STuong Lien * (if any) taking over i.e. starting grace period and also trigger key
13771ef6f7c9STuong Lien * distributing process.
1378daef1ee3STuong Lien *
1379fc1b6d6dSTuong Lien * The "per-peer" sndnxt is also reset when the peer key has switched.
1380fc1b6d6dSTuong Lien */
tipc_crypto_key_synch(struct tipc_crypto * rx,struct sk_buff * skb)1381f779bf79STuong Lien static void tipc_crypto_key_synch(struct tipc_crypto *rx, struct sk_buff *skb)
1382fc1b6d6dSTuong Lien {
1383f779bf79STuong Lien struct tipc_ehdr *ehdr = (struct tipc_ehdr *)skb_network_header(skb);
1384f779bf79STuong Lien struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx;
1385f779bf79STuong Lien struct tipc_msg *hdr = buf_msg(skb);
1386f779bf79STuong Lien u32 self = tipc_own_addr(rx->net);
1387f779bf79STuong Lien u8 cur, new;
13881ef6f7c9STuong Lien unsigned long delay;
1389fc1b6d6dSTuong Lien
1390daef1ee3STuong Lien /* Update RX 'key_master' flag according to peer, also mark "legacy" if
1391daef1ee3STuong Lien * a peer has no master key.
1392daef1ee3STuong Lien */
1393daef1ee3STuong Lien rx->key_master = ehdr->master_key;
1394daef1ee3STuong Lien if (!rx->key_master)
1395daef1ee3STuong Lien tx->legacy_user = 1;
1396daef1ee3STuong Lien
1397daef1ee3STuong Lien /* For later cases, apply only if message is destined to this node */
1398f779bf79STuong Lien if (!ehdr->destined || msg_short(hdr) || msg_destnode(hdr) != self)
1399fc1b6d6dSTuong Lien return;
1400fc1b6d6dSTuong Lien
1401daef1ee3STuong Lien /* Case 1: Peer has no keys, let's make master key take over */
14021ef6f7c9STuong Lien if (ehdr->rx_nokey) {
1403daef1ee3STuong Lien /* Set or extend grace period */
1404daef1ee3STuong Lien tx->timer2 = jiffies;
14051ef6f7c9STuong Lien /* Schedule key distributing for the peer if not yet */
14061ef6f7c9STuong Lien if (tx->key.keys &&
14071ef6f7c9STuong Lien !atomic_cmpxchg(&rx->key_distr, 0, KEY_DISTR_SCHED)) {
14081ef6f7c9STuong Lien get_random_bytes(&delay, 2);
14091ef6f7c9STuong Lien delay %= 5;
14101ef6f7c9STuong Lien delay = msecs_to_jiffies(500 * ++delay);
14111ef6f7c9STuong Lien if (queue_delayed_work(tx->wq, &rx->work, delay))
14121ef6f7c9STuong Lien tipc_node_get(rx->node);
14131ef6f7c9STuong Lien }
14141ef6f7c9STuong Lien } else {
14151ef6f7c9STuong Lien /* Cancel a pending key distributing if any */
14161ef6f7c9STuong Lien atomic_xchg(&rx->key_distr, 0);
14171ef6f7c9STuong Lien }
1418daef1ee3STuong Lien
1419daef1ee3STuong Lien /* Case 2: Peer RX active key has changed, let's update own TX users */
1420f779bf79STuong Lien cur = atomic_read(&rx->peer_rx_active);
1421f779bf79STuong Lien new = ehdr->rx_key_active;
1422f779bf79STuong Lien if (tx->key.keys &&
1423f779bf79STuong Lien cur != new &&
1424f779bf79STuong Lien atomic_cmpxchg(&rx->peer_rx_active, cur, new) == cur) {
1425f779bf79STuong Lien if (new)
1426f779bf79STuong Lien tipc_aead_users_inc(tx->aead[new], INT_MAX);
1427f779bf79STuong Lien if (cur)
1428f779bf79STuong Lien tipc_aead_users_dec(tx->aead[cur], 0);
1429fc1b6d6dSTuong Lien
1430fc1b6d6dSTuong Lien atomic64_set(&rx->sndnxt, 0);
1431fc1b6d6dSTuong Lien /* Mark the point TX key users changed */
1432fc1b6d6dSTuong Lien tx->timer1 = jiffies;
1433fc1b6d6dSTuong Lien
1434f779bf79STuong Lien pr_debug("%s: key users changed %d-- %d++, peer %s\n",
1435f779bf79STuong Lien tx->name, cur, new, rx->name);
1436fc1b6d6dSTuong Lien }
1437fc1b6d6dSTuong Lien }
1438fc1b6d6dSTuong Lien
tipc_crypto_key_revoke(struct net * net,u8 tx_key)1439fc1b6d6dSTuong Lien static int tipc_crypto_key_revoke(struct net *net, u8 tx_key)
1440fc1b6d6dSTuong Lien {
1441fc1b6d6dSTuong Lien struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1442fc1b6d6dSTuong Lien struct tipc_key key;
1443fc1b6d6dSTuong Lien
144408e50cf0SChengfeng Ye spin_lock_bh(&tx->lock);
1445fc1b6d6dSTuong Lien key = tx->key;
1446fc1b6d6dSTuong Lien WARN_ON(!key.active || tx_key != key.active);
1447fc1b6d6dSTuong Lien
1448fc1b6d6dSTuong Lien /* Free the active key */
1449fc1b6d6dSTuong Lien tipc_crypto_key_set_state(tx, key.passive, 0, key.pending);
1450fc1b6d6dSTuong Lien tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
145108e50cf0SChengfeng Ye spin_unlock_bh(&tx->lock);
1452fc1b6d6dSTuong Lien
1453f779bf79STuong Lien pr_warn("%s: key is revoked\n", tx->name);
1454fc1b6d6dSTuong Lien return -EKEYREVOKED;
1455fc1b6d6dSTuong Lien }
1456fc1b6d6dSTuong Lien
tipc_crypto_start(struct tipc_crypto ** crypto,struct net * net,struct tipc_node * node)1457fc1b6d6dSTuong Lien int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
1458fc1b6d6dSTuong Lien struct tipc_node *node)
1459fc1b6d6dSTuong Lien {
1460fc1b6d6dSTuong Lien struct tipc_crypto *c;
1461fc1b6d6dSTuong Lien
1462fc1b6d6dSTuong Lien if (*crypto)
1463fc1b6d6dSTuong Lien return -EEXIST;
1464fc1b6d6dSTuong Lien
1465fc1b6d6dSTuong Lien /* Allocate crypto */
1466f845fe58SHoang Le c = kzalloc(sizeof(*c), GFP_ATOMIC);
1467fc1b6d6dSTuong Lien if (!c)
1468fc1b6d6dSTuong Lien return -ENOMEM;
1469fc1b6d6dSTuong Lien
14701ef6f7c9STuong Lien /* Allocate workqueue on TX */
14711ef6f7c9STuong Lien if (!node) {
14721ef6f7c9STuong Lien c->wq = alloc_ordered_workqueue("tipc_crypto", 0);
14731ef6f7c9STuong Lien if (!c->wq) {
14741ef6f7c9STuong Lien kfree(c);
14751ef6f7c9STuong Lien return -ENOMEM;
14761ef6f7c9STuong Lien }
14771ef6f7c9STuong Lien }
14781ef6f7c9STuong Lien
1479fc1b6d6dSTuong Lien /* Allocate statistic structure */
1480f845fe58SHoang Le c->stats = alloc_percpu_gfp(struct tipc_crypto_stats, GFP_ATOMIC);
1481fc1b6d6dSTuong Lien if (!c->stats) {
1482ac1db7acSYang Yingliang if (c->wq)
1483ac1db7acSYang Yingliang destroy_workqueue(c->wq);
1484453431a5SWaiman Long kfree_sensitive(c);
1485fc1b6d6dSTuong Lien return -ENOMEM;
1486fc1b6d6dSTuong Lien }
1487fc1b6d6dSTuong Lien
1488daef1ee3STuong Lien c->flags = 0;
1489fc1b6d6dSTuong Lien c->net = net;
1490fc1b6d6dSTuong Lien c->node = node;
14911ef6f7c9STuong Lien get_random_bytes(&c->key_gen, 2);
1492fc1b6d6dSTuong Lien tipc_crypto_key_set_state(c, 0, 0, 0);
14931ef6f7c9STuong Lien atomic_set(&c->key_distr, 0);
1494fc1b6d6dSTuong Lien atomic_set(&c->peer_rx_active, 0);
1495fc1b6d6dSTuong Lien atomic64_set(&c->sndnxt, 0);
1496fc1b6d6dSTuong Lien c->timer1 = jiffies;
1497fc1b6d6dSTuong Lien c->timer2 = jiffies;
149823700da2STuong Lien c->rekeying_intv = TIPC_REKEYING_INTV_DEF;
1499fc1b6d6dSTuong Lien spin_lock_init(&c->lock);
1500f779bf79STuong Lien scnprintf(c->name, 48, "%s(%s)", (is_rx(c)) ? "RX" : "TX",
1501f779bf79STuong Lien (is_rx(c)) ? tipc_node_get_id_str(c->node) :
1502f779bf79STuong Lien tipc_own_id_string(c->net));
1503fc1b6d6dSTuong Lien
15041ef6f7c9STuong Lien if (is_rx(c))
15051ef6f7c9STuong Lien INIT_DELAYED_WORK(&c->work, tipc_crypto_work_rx);
150623700da2STuong Lien else
150723700da2STuong Lien INIT_DELAYED_WORK(&c->work, tipc_crypto_work_tx);
15081ef6f7c9STuong Lien
1509f779bf79STuong Lien *crypto = c;
1510fc1b6d6dSTuong Lien return 0;
1511fc1b6d6dSTuong Lien }
1512fc1b6d6dSTuong Lien
tipc_crypto_stop(struct tipc_crypto ** crypto)1513fc1b6d6dSTuong Lien void tipc_crypto_stop(struct tipc_crypto **crypto)
1514fc1b6d6dSTuong Lien {
15151ef6f7c9STuong Lien struct tipc_crypto *c = *crypto;
1516fc1b6d6dSTuong Lien u8 k;
1517fc1b6d6dSTuong Lien
1518f779bf79STuong Lien if (!c)
1519fc1b6d6dSTuong Lien return;
1520fc1b6d6dSTuong Lien
15211ef6f7c9STuong Lien /* Flush any queued works & destroy wq */
152223700da2STuong Lien if (is_tx(c)) {
152323700da2STuong Lien c->rekeying_intv = 0;
152423700da2STuong Lien cancel_delayed_work_sync(&c->work);
15251ef6f7c9STuong Lien destroy_workqueue(c->wq);
152623700da2STuong Lien }
1527fc1b6d6dSTuong Lien
1528fc1b6d6dSTuong Lien /* Release AEAD keys */
15291ef6f7c9STuong Lien rcu_read_lock();
1530fc1b6d6dSTuong Lien for (k = KEY_MIN; k <= KEY_MAX; k++)
1531fc1b6d6dSTuong Lien tipc_aead_put(rcu_dereference(c->aead[k]));
1532fc1b6d6dSTuong Lien rcu_read_unlock();
1533f779bf79STuong Lien pr_debug("%s: has been stopped\n", c->name);
1534fc1b6d6dSTuong Lien
1535fc1b6d6dSTuong Lien /* Free this crypto statistics */
1536fc1b6d6dSTuong Lien free_percpu(c->stats);
1537fc1b6d6dSTuong Lien
1538fc1b6d6dSTuong Lien *crypto = NULL;
1539453431a5SWaiman Long kfree_sensitive(c);
1540fc1b6d6dSTuong Lien }
1541fc1b6d6dSTuong Lien
tipc_crypto_timeout(struct tipc_crypto * rx)1542fc1b6d6dSTuong Lien void tipc_crypto_timeout(struct tipc_crypto *rx)
1543fc1b6d6dSTuong Lien {
1544fc1b6d6dSTuong Lien struct tipc_net *tn = tipc_net(rx->net);
1545fc1b6d6dSTuong Lien struct tipc_crypto *tx = tn->crypto_tx;
1546fc1b6d6dSTuong Lien struct tipc_key key;
1547fc1b6d6dSTuong Lien int cmd;
1548fc1b6d6dSTuong Lien
1549f779bf79STuong Lien /* TX pending: taking all users & stable -> active */
1550fc1b6d6dSTuong Lien spin_lock(&tx->lock);
1551fc1b6d6dSTuong Lien key = tx->key;
1552fc1b6d6dSTuong Lien if (key.active && tipc_aead_users(tx->aead[key.active]) > 0)
1553fc1b6d6dSTuong Lien goto s1;
1554fc1b6d6dSTuong Lien if (!key.pending || tipc_aead_users(tx->aead[key.pending]) <= 0)
1555fc1b6d6dSTuong Lien goto s1;
1556f779bf79STuong Lien if (time_before(jiffies, tx->timer1 + TIPC_TX_LASTING_TIME))
1557fc1b6d6dSTuong Lien goto s1;
1558fc1b6d6dSTuong Lien
1559fc1b6d6dSTuong Lien tipc_crypto_key_set_state(tx, key.passive, key.pending, 0);
1560fc1b6d6dSTuong Lien if (key.active)
1561fc1b6d6dSTuong Lien tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
1562fc1b6d6dSTuong Lien this_cpu_inc(tx->stats->stat[STAT_SWITCHES]);
1563f779bf79STuong Lien pr_info("%s: key[%d] is activated\n", tx->name, key.pending);
1564fc1b6d6dSTuong Lien
1565fc1b6d6dSTuong Lien s1:
1566fc1b6d6dSTuong Lien spin_unlock(&tx->lock);
1567fc1b6d6dSTuong Lien
1568f779bf79STuong Lien /* RX pending: having user -> active */
1569fc1b6d6dSTuong Lien spin_lock(&rx->lock);
1570fc1b6d6dSTuong Lien key = rx->key;
1571fc1b6d6dSTuong Lien if (!key.pending || tipc_aead_users(rx->aead[key.pending]) <= 0)
1572fc1b6d6dSTuong Lien goto s2;
1573fc1b6d6dSTuong Lien
1574f779bf79STuong Lien if (key.active)
1575f779bf79STuong Lien key.passive = key.active;
1576f779bf79STuong Lien key.active = key.pending;
1577f779bf79STuong Lien rx->timer2 = jiffies;
1578f779bf79STuong Lien tipc_crypto_key_set_state(rx, key.passive, key.active, 0);
1579fc1b6d6dSTuong Lien this_cpu_inc(rx->stats->stat[STAT_SWITCHES]);
1580f779bf79STuong Lien pr_info("%s: key[%d] is activated\n", rx->name, key.pending);
1581fc1b6d6dSTuong Lien goto s5;
1582fc1b6d6dSTuong Lien
1583fc1b6d6dSTuong Lien s2:
1584f779bf79STuong Lien /* RX pending: not working -> remove */
1585f779bf79STuong Lien if (!key.pending || tipc_aead_users(rx->aead[key.pending]) > -10)
1586fc1b6d6dSTuong Lien goto s3;
1587fc1b6d6dSTuong Lien
1588f779bf79STuong Lien tipc_crypto_key_set_state(rx, key.passive, key.active, 0);
1589f779bf79STuong Lien tipc_crypto_key_detach(rx->aead[key.pending], &rx->lock);
1590f779bf79STuong Lien pr_debug("%s: key[%d] is removed\n", rx->name, key.pending);
1591fc1b6d6dSTuong Lien goto s5;
1592fc1b6d6dSTuong Lien
1593fc1b6d6dSTuong Lien s3:
1594f779bf79STuong Lien /* RX active: timed out or no user -> pending */
1595fc1b6d6dSTuong Lien if (!key.active)
1596fc1b6d6dSTuong Lien goto s4;
1597f779bf79STuong Lien if (time_before(jiffies, rx->timer1 + TIPC_RX_ACTIVE_LIM) &&
1598f779bf79STuong Lien tipc_aead_users(rx->aead[key.active]) > 0)
1599fc1b6d6dSTuong Lien goto s4;
1600fc1b6d6dSTuong Lien
1601f779bf79STuong Lien if (key.pending)
1602f779bf79STuong Lien key.passive = key.active;
1603f779bf79STuong Lien else
1604f779bf79STuong Lien key.pending = key.active;
1605f779bf79STuong Lien rx->timer2 = jiffies;
1606f779bf79STuong Lien tipc_crypto_key_set_state(rx, key.passive, 0, key.pending);
1607f779bf79STuong Lien tipc_aead_users_set(rx->aead[key.pending], 0);
1608f779bf79STuong Lien pr_debug("%s: key[%d] is deactivated\n", rx->name, key.active);
1609fc1b6d6dSTuong Lien goto s5;
1610fc1b6d6dSTuong Lien
1611fc1b6d6dSTuong Lien s4:
1612f779bf79STuong Lien /* RX passive: outdated or not working -> free */
1613f779bf79STuong Lien if (!key.passive)
1614fc1b6d6dSTuong Lien goto s5;
1615f779bf79STuong Lien if (time_before(jiffies, rx->timer2 + TIPC_RX_PASSIVE_LIM) &&
1616f779bf79STuong Lien tipc_aead_users(rx->aead[key.passive]) > -10)
1617fc1b6d6dSTuong Lien goto s5;
1618fc1b6d6dSTuong Lien
1619fc1b6d6dSTuong Lien tipc_crypto_key_set_state(rx, 0, key.active, key.pending);
1620fc1b6d6dSTuong Lien tipc_crypto_key_detach(rx->aead[key.passive], &rx->lock);
1621f779bf79STuong Lien pr_debug("%s: key[%d] is freed\n", rx->name, key.passive);
1622fc1b6d6dSTuong Lien
1623fc1b6d6dSTuong Lien s5:
1624fc1b6d6dSTuong Lien spin_unlock(&rx->lock);
1625fc1b6d6dSTuong Lien
1626daef1ee3STuong Lien /* Relax it here, the flag will be set again if it really is, but only
1627daef1ee3STuong Lien * when we are not in grace period for safety!
1628daef1ee3STuong Lien */
1629daef1ee3STuong Lien if (time_after(jiffies, tx->timer2 + TIPC_TX_GRACE_PERIOD))
1630daef1ee3STuong Lien tx->legacy_user = 0;
1631daef1ee3STuong Lien
1632fc1b6d6dSTuong Lien /* Limit max_tfms & do debug commands if needed */
1633fc1b6d6dSTuong Lien if (likely(sysctl_tipc_max_tfms <= TIPC_MAX_TFMS_LIM))
1634fc1b6d6dSTuong Lien return;
1635fc1b6d6dSTuong Lien
1636fc1b6d6dSTuong Lien cmd = sysctl_tipc_max_tfms;
1637fc1b6d6dSTuong Lien sysctl_tipc_max_tfms = TIPC_MAX_TFMS_DEF;
1638fc1b6d6dSTuong Lien tipc_crypto_do_cmd(rx->net, cmd);
1639fc1b6d6dSTuong Lien }
1640fc1b6d6dSTuong Lien
tipc_crypto_clone_msg(struct net * net,struct sk_buff * _skb,struct tipc_bearer * b,struct tipc_media_addr * dst,struct tipc_node * __dnode,u8 type)1641daef1ee3STuong Lien static inline void tipc_crypto_clone_msg(struct net *net, struct sk_buff *_skb,
1642daef1ee3STuong Lien struct tipc_bearer *b,
1643daef1ee3STuong Lien struct tipc_media_addr *dst,
1644daef1ee3STuong Lien struct tipc_node *__dnode, u8 type)
1645daef1ee3STuong Lien {
1646daef1ee3STuong Lien struct sk_buff *skb;
1647daef1ee3STuong Lien
1648daef1ee3STuong Lien skb = skb_clone(_skb, GFP_ATOMIC);
1649daef1ee3STuong Lien if (skb) {
1650daef1ee3STuong Lien TIPC_SKB_CB(skb)->xmit_type = type;
1651daef1ee3STuong Lien tipc_crypto_xmit(net, &skb, b, dst, __dnode);
1652daef1ee3STuong Lien if (skb)
1653daef1ee3STuong Lien b->media->send_msg(net, skb, b, dst);
1654daef1ee3STuong Lien }
1655daef1ee3STuong Lien }
1656daef1ee3STuong Lien
1657fc1b6d6dSTuong Lien /**
1658fc1b6d6dSTuong Lien * tipc_crypto_xmit - Build & encrypt TIPC message for xmit
1659fc1b6d6dSTuong Lien * @net: struct net
1660fc1b6d6dSTuong Lien * @skb: input/output message skb pointer
1661fc1b6d6dSTuong Lien * @b: bearer used for xmit later
1662fc1b6d6dSTuong Lien * @dst: destination media address
1663fc1b6d6dSTuong Lien * @__dnode: destination node for reference if any
1664fc1b6d6dSTuong Lien *
1665fc1b6d6dSTuong Lien * First, build an encryption message header on the top of the message, then
1666daef1ee3STuong Lien * encrypt the original TIPC message by using the pending, master or active
1667daef1ee3STuong Lien * key with this preference order.
1668fc1b6d6dSTuong Lien * If the encryption is successful, the encrypted skb is returned directly or
1669fc1b6d6dSTuong Lien * via the callback.
1670fc1b6d6dSTuong Lien * Otherwise, the skb is freed!
1671fc1b6d6dSTuong Lien *
1672fc1b6d6dSTuong Lien * Return:
1673637b77fdSRandy Dunlap * * 0 : the encryption has succeeded (or no encryption)
1674637b77fdSRandy Dunlap * * -EINPROGRESS/-EBUSY : the encryption is ongoing, a callback will be made
1675637b77fdSRandy Dunlap * * -ENOKEK : the encryption has failed due to no key
1676637b77fdSRandy Dunlap * * -EKEYREVOKED : the encryption has failed due to key revoked
1677637b77fdSRandy Dunlap * * -ENOMEM : the encryption has failed due to no memory
1678637b77fdSRandy Dunlap * * < 0 : the encryption has failed due to other reasons
1679fc1b6d6dSTuong Lien */
tipc_crypto_xmit(struct net * net,struct sk_buff ** skb,struct tipc_bearer * b,struct tipc_media_addr * dst,struct tipc_node * __dnode)1680fc1b6d6dSTuong Lien int tipc_crypto_xmit(struct net *net, struct sk_buff **skb,
1681fc1b6d6dSTuong Lien struct tipc_bearer *b, struct tipc_media_addr *dst,
1682fc1b6d6dSTuong Lien struct tipc_node *__dnode)
1683fc1b6d6dSTuong Lien {
1684fc1b6d6dSTuong Lien struct tipc_crypto *__rx = tipc_node_crypto_rx(__dnode);
1685fc1b6d6dSTuong Lien struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1686fc1b6d6dSTuong Lien struct tipc_crypto_stats __percpu *stats = tx->stats;
1687f779bf79STuong Lien struct tipc_msg *hdr = buf_msg(*skb);
1688fc1b6d6dSTuong Lien struct tipc_key key = tx->key;
1689fc1b6d6dSTuong Lien struct tipc_aead *aead = NULL;
1690f779bf79STuong Lien u32 user = msg_user(hdr);
1691daef1ee3STuong Lien u32 type = msg_type(hdr);
1692daef1ee3STuong Lien int rc = -ENOKEY;
1693daef1ee3STuong Lien u8 tx_key = 0;
1694fc1b6d6dSTuong Lien
1695fc1b6d6dSTuong Lien /* No encryption? */
1696fc1b6d6dSTuong Lien if (!tx->working)
1697fc1b6d6dSTuong Lien return 0;
1698fc1b6d6dSTuong Lien
1699daef1ee3STuong Lien /* Pending key if peer has active on it or probing time */
1700fc1b6d6dSTuong Lien if (unlikely(key.pending)) {
1701fc1b6d6dSTuong Lien tx_key = key.pending;
1702daef1ee3STuong Lien if (!tx->key_master && !key.active)
1703fc1b6d6dSTuong Lien goto encrypt;
1704fc1b6d6dSTuong Lien if (__rx && atomic_read(&__rx->peer_rx_active) == tx_key)
1705fc1b6d6dSTuong Lien goto encrypt;
1706daef1ee3STuong Lien if (TIPC_SKB_CB(*skb)->xmit_type == SKB_PROBING) {
1707f779bf79STuong Lien pr_debug("%s: probing for key[%d]\n", tx->name,
1708f779bf79STuong Lien key.pending);
1709fc1b6d6dSTuong Lien goto encrypt;
1710f779bf79STuong Lien }
1711daef1ee3STuong Lien if (user == LINK_CONFIG || user == LINK_PROTOCOL)
1712daef1ee3STuong Lien tipc_crypto_clone_msg(net, *skb, b, dst, __dnode,
1713daef1ee3STuong Lien SKB_PROBING);
1714daef1ee3STuong Lien }
1715daef1ee3STuong Lien
1716daef1ee3STuong Lien /* Master key if this is a *vital* message or in grace period */
1717daef1ee3STuong Lien if (tx->key_master) {
1718daef1ee3STuong Lien tx_key = KEY_MASTER;
1719daef1ee3STuong Lien if (!key.active)
1720daef1ee3STuong Lien goto encrypt;
1721daef1ee3STuong Lien if (TIPC_SKB_CB(*skb)->xmit_type == SKB_GRACING) {
1722daef1ee3STuong Lien pr_debug("%s: gracing for msg (%d %d)\n", tx->name,
1723daef1ee3STuong Lien user, type);
1724daef1ee3STuong Lien goto encrypt;
1725daef1ee3STuong Lien }
1726daef1ee3STuong Lien if (user == LINK_CONFIG ||
1727daef1ee3STuong Lien (user == LINK_PROTOCOL && type == RESET_MSG) ||
17281ef6f7c9STuong Lien (user == MSG_CRYPTO && type == KEY_DISTR_MSG) ||
1729daef1ee3STuong Lien time_before(jiffies, tx->timer2 + TIPC_TX_GRACE_PERIOD)) {
1730daef1ee3STuong Lien if (__rx && __rx->key_master &&
1731daef1ee3STuong Lien !atomic_read(&__rx->peer_rx_active))
1732daef1ee3STuong Lien goto encrypt;
1733daef1ee3STuong Lien if (!__rx) {
1734daef1ee3STuong Lien if (likely(!tx->legacy_user))
1735daef1ee3STuong Lien goto encrypt;
1736daef1ee3STuong Lien tipc_crypto_clone_msg(net, *skb, b, dst,
1737daef1ee3STuong Lien __dnode, SKB_GRACING);
1738fc1b6d6dSTuong Lien }
1739fc1b6d6dSTuong Lien }
1740fc1b6d6dSTuong Lien }
1741daef1ee3STuong Lien
1742fc1b6d6dSTuong Lien /* Else, use the active key if any */
1743fc1b6d6dSTuong Lien if (likely(key.active)) {
1744fc1b6d6dSTuong Lien tx_key = key.active;
1745fc1b6d6dSTuong Lien goto encrypt;
1746fc1b6d6dSTuong Lien }
1747daef1ee3STuong Lien
1748fc1b6d6dSTuong Lien goto exit;
1749fc1b6d6dSTuong Lien
1750fc1b6d6dSTuong Lien encrypt:
1751fc1b6d6dSTuong Lien aead = tipc_aead_get(tx->aead[tx_key]);
1752fc1b6d6dSTuong Lien if (unlikely(!aead))
1753fc1b6d6dSTuong Lien goto exit;
1754fc1b6d6dSTuong Lien rc = tipc_ehdr_build(net, aead, tx_key, *skb, __rx);
1755fc1b6d6dSTuong Lien if (likely(rc > 0))
1756fc1b6d6dSTuong Lien rc = tipc_aead_encrypt(aead, *skb, b, dst, __dnode);
1757fc1b6d6dSTuong Lien
1758fc1b6d6dSTuong Lien exit:
1759fc1b6d6dSTuong Lien switch (rc) {
1760fc1b6d6dSTuong Lien case 0:
1761fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_OK]);
1762fc1b6d6dSTuong Lien break;
1763fc1b6d6dSTuong Lien case -EINPROGRESS:
1764fc1b6d6dSTuong Lien case -EBUSY:
1765fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_ASYNC]);
1766fc1b6d6dSTuong Lien *skb = NULL;
1767fc1b6d6dSTuong Lien return rc;
1768fc1b6d6dSTuong Lien default:
1769fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_NOK]);
1770fc1b6d6dSTuong Lien if (rc == -ENOKEY)
1771fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_NOKEYS]);
1772fc1b6d6dSTuong Lien else if (rc == -EKEYREVOKED)
1773fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_BADKEYS]);
1774fc1b6d6dSTuong Lien kfree_skb(*skb);
1775fc1b6d6dSTuong Lien *skb = NULL;
1776fc1b6d6dSTuong Lien break;
1777fc1b6d6dSTuong Lien }
1778fc1b6d6dSTuong Lien
1779fc1b6d6dSTuong Lien tipc_aead_put(aead);
1780fc1b6d6dSTuong Lien return rc;
1781fc1b6d6dSTuong Lien }
1782fc1b6d6dSTuong Lien
1783fc1b6d6dSTuong Lien /**
1784fc1b6d6dSTuong Lien * tipc_crypto_rcv - Decrypt an encrypted TIPC message from peer
1785fc1b6d6dSTuong Lien * @net: struct net
1786fc1b6d6dSTuong Lien * @rx: RX crypto handle
1787fc1b6d6dSTuong Lien * @skb: input/output message skb pointer
1788fc1b6d6dSTuong Lien * @b: bearer where the message has been received
1789fc1b6d6dSTuong Lien *
1790fc1b6d6dSTuong Lien * If the decryption is successful, the decrypted skb is returned directly or
1791fc1b6d6dSTuong Lien * as the callback, the encryption header and auth tag will be trimed out
1792fc1b6d6dSTuong Lien * before forwarding to tipc_rcv() via the tipc_crypto_rcv_complete().
1793fc1b6d6dSTuong Lien * Otherwise, the skb will be freed!
1794fc1b6d6dSTuong Lien * Note: RX key(s) can be re-aligned, or in case of no key suitable, TX
1795fc1b6d6dSTuong Lien * cluster key(s) can be taken for decryption (- recursive).
1796fc1b6d6dSTuong Lien *
1797fc1b6d6dSTuong Lien * Return:
1798637b77fdSRandy Dunlap * * 0 : the decryption has successfully completed
1799637b77fdSRandy Dunlap * * -EINPROGRESS/-EBUSY : the decryption is ongoing, a callback will be made
1800637b77fdSRandy Dunlap * * -ENOKEY : the decryption has failed due to no key
1801637b77fdSRandy Dunlap * * -EBADMSG : the decryption has failed due to bad message
1802637b77fdSRandy Dunlap * * -ENOMEM : the decryption has failed due to no memory
1803637b77fdSRandy Dunlap * * < 0 : the decryption has failed due to other reasons
1804fc1b6d6dSTuong Lien */
tipc_crypto_rcv(struct net * net,struct tipc_crypto * rx,struct sk_buff ** skb,struct tipc_bearer * b)1805fc1b6d6dSTuong Lien int tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx,
1806fc1b6d6dSTuong Lien struct sk_buff **skb, struct tipc_bearer *b)
1807fc1b6d6dSTuong Lien {
1808fc1b6d6dSTuong Lien struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1809fc1b6d6dSTuong Lien struct tipc_crypto_stats __percpu *stats;
1810fc1b6d6dSTuong Lien struct tipc_aead *aead = NULL;
1811fc1b6d6dSTuong Lien struct tipc_key key;
1812fc1b6d6dSTuong Lien int rc = -ENOKEY;
18131ef6f7c9STuong Lien u8 tx_key, n;
1814daef1ee3STuong Lien
1815daef1ee3STuong Lien tx_key = ((struct tipc_ehdr *)(*skb)->data)->tx_key;
1816fc1b6d6dSTuong Lien
1817fc1b6d6dSTuong Lien /* New peer?
1818fc1b6d6dSTuong Lien * Let's try with TX key (i.e. cluster mode) & verify the skb first!
1819fc1b6d6dSTuong Lien */
1820daef1ee3STuong Lien if (unlikely(!rx || tx_key == KEY_MASTER))
1821fc1b6d6dSTuong Lien goto pick_tx;
1822fc1b6d6dSTuong Lien
1823f779bf79STuong Lien /* Pick RX key according to TX key if any */
1824fc1b6d6dSTuong Lien key = rx->key;
1825f779bf79STuong Lien if (tx_key == key.active || tx_key == key.pending ||
1826f779bf79STuong Lien tx_key == key.passive)
1827fc1b6d6dSTuong Lien goto decrypt;
1828fc1b6d6dSTuong Lien
1829fc1b6d6dSTuong Lien /* Unknown key, let's try to align RX key(s) */
1830fc1b6d6dSTuong Lien if (tipc_crypto_key_try_align(rx, tx_key))
1831fc1b6d6dSTuong Lien goto decrypt;
1832fc1b6d6dSTuong Lien
1833fc1b6d6dSTuong Lien pick_tx:
1834fc1b6d6dSTuong Lien /* No key suitable? Try to pick one from TX... */
1835daef1ee3STuong Lien aead = tipc_crypto_key_pick_tx(tx, rx, *skb, tx_key);
1836fc1b6d6dSTuong Lien if (aead)
1837fc1b6d6dSTuong Lien goto decrypt;
1838fc1b6d6dSTuong Lien goto exit;
1839fc1b6d6dSTuong Lien
1840fc1b6d6dSTuong Lien decrypt:
1841fc1b6d6dSTuong Lien rcu_read_lock();
1842fc1b6d6dSTuong Lien if (!aead)
1843fc1b6d6dSTuong Lien aead = tipc_aead_get(rx->aead[tx_key]);
1844fc1b6d6dSTuong Lien rc = tipc_aead_decrypt(net, aead, *skb, b);
1845fc1b6d6dSTuong Lien rcu_read_unlock();
1846fc1b6d6dSTuong Lien
1847fc1b6d6dSTuong Lien exit:
1848fc1b6d6dSTuong Lien stats = ((rx) ?: tx)->stats;
1849fc1b6d6dSTuong Lien switch (rc) {
1850fc1b6d6dSTuong Lien case 0:
1851fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_OK]);
1852fc1b6d6dSTuong Lien break;
1853fc1b6d6dSTuong Lien case -EINPROGRESS:
1854fc1b6d6dSTuong Lien case -EBUSY:
1855fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_ASYNC]);
1856fc1b6d6dSTuong Lien *skb = NULL;
1857fc1b6d6dSTuong Lien return rc;
1858fc1b6d6dSTuong Lien default:
1859fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_NOK]);
1860fc1b6d6dSTuong Lien if (rc == -ENOKEY) {
1861fc1b6d6dSTuong Lien kfree_skb(*skb);
1862fc1b6d6dSTuong Lien *skb = NULL;
18631ef6f7c9STuong Lien if (rx) {
18641ef6f7c9STuong Lien /* Mark rx->nokey only if we dont have a
18651ef6f7c9STuong Lien * pending received session key, nor a newer
18661ef6f7c9STuong Lien * one i.e. in the next slot.
18671ef6f7c9STuong Lien */
18681ef6f7c9STuong Lien n = key_next(tx_key);
18691ef6f7c9STuong Lien rx->nokey = !(rx->skey ||
18701ef6f7c9STuong Lien rcu_access_pointer(rx->aead[n]));
18711ef6f7c9STuong Lien pr_debug_ratelimited("%s: nokey %d, key %d/%x\n",
18721ef6f7c9STuong Lien rx->name, rx->nokey,
18731ef6f7c9STuong Lien tx_key, rx->key.keys);
1874fc1b6d6dSTuong Lien tipc_node_put(rx->node);
18751ef6f7c9STuong Lien }
1876fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_NOKEYS]);
1877fc1b6d6dSTuong Lien return rc;
1878fc1b6d6dSTuong Lien } else if (rc == -EBADMSG) {
1879fc1b6d6dSTuong Lien this_cpu_inc(stats->stat[STAT_BADMSGS]);
1880fc1b6d6dSTuong Lien }
1881fc1b6d6dSTuong Lien break;
1882fc1b6d6dSTuong Lien }
1883fc1b6d6dSTuong Lien
1884fc1b6d6dSTuong Lien tipc_crypto_rcv_complete(net, aead, b, skb, rc);
1885fc1b6d6dSTuong Lien return rc;
1886fc1b6d6dSTuong Lien }
1887fc1b6d6dSTuong Lien
tipc_crypto_rcv_complete(struct net * net,struct tipc_aead * aead,struct tipc_bearer * b,struct sk_buff ** skb,int err)1888fc1b6d6dSTuong Lien static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
1889fc1b6d6dSTuong Lien struct tipc_bearer *b,
1890fc1b6d6dSTuong Lien struct sk_buff **skb, int err)
1891fc1b6d6dSTuong Lien {
1892fc1b6d6dSTuong Lien struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(*skb);
1893fc1b6d6dSTuong Lien struct tipc_crypto *rx = aead->crypto;
1894fc1b6d6dSTuong Lien struct tipc_aead *tmp = NULL;
1895fc1b6d6dSTuong Lien struct tipc_ehdr *ehdr;
1896fc1b6d6dSTuong Lien struct tipc_node *n;
1897fc1b6d6dSTuong Lien
1898fc1b6d6dSTuong Lien /* Is this completed by TX? */
1899f779bf79STuong Lien if (unlikely(is_tx(aead->crypto))) {
1900fc1b6d6dSTuong Lien rx = skb_cb->tx_clone_ctx.rx;
1901f779bf79STuong Lien pr_debug("TX->RX(%s): err %d, aead %p, skb->next %p, flags %x\n",
1902fc1b6d6dSTuong Lien (rx) ? tipc_node_get_id_str(rx->node) : "-", err, aead,
1903fc1b6d6dSTuong Lien (*skb)->next, skb_cb->flags);
1904f779bf79STuong Lien pr_debug("skb_cb [recurs %d, last %p], tx->aead [%p %p %p]\n",
1905fc1b6d6dSTuong Lien skb_cb->tx_clone_ctx.recurs, skb_cb->tx_clone_ctx.last,
1906fc1b6d6dSTuong Lien aead->crypto->aead[1], aead->crypto->aead[2],
1907fc1b6d6dSTuong Lien aead->crypto->aead[3]);
1908fc1b6d6dSTuong Lien if (unlikely(err)) {
1909fc1b6d6dSTuong Lien if (err == -EBADMSG && (*skb)->next)
1910fc1b6d6dSTuong Lien tipc_rcv(net, (*skb)->next, b);
1911fc1b6d6dSTuong Lien goto free_skb;
1912fc1b6d6dSTuong Lien }
1913fc1b6d6dSTuong Lien
1914fc1b6d6dSTuong Lien if (likely((*skb)->next)) {
1915fc1b6d6dSTuong Lien kfree_skb((*skb)->next);
1916fc1b6d6dSTuong Lien (*skb)->next = NULL;
1917fc1b6d6dSTuong Lien }
1918fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)(*skb)->data;
1919fc1b6d6dSTuong Lien if (!rx) {
1920fc1b6d6dSTuong Lien WARN_ON(ehdr->user != LINK_CONFIG);
1921fc1b6d6dSTuong Lien n = tipc_node_create(net, 0, ehdr->id, 0xffffu, 0,
1922fc1b6d6dSTuong Lien true);
1923fc1b6d6dSTuong Lien rx = tipc_node_crypto_rx(n);
1924fc1b6d6dSTuong Lien if (unlikely(!rx))
1925fc1b6d6dSTuong Lien goto free_skb;
1926fc1b6d6dSTuong Lien }
1927fc1b6d6dSTuong Lien
1928daef1ee3STuong Lien /* Ignore cloning if it was TX master key */
1929daef1ee3STuong Lien if (ehdr->tx_key == KEY_MASTER)
1930daef1ee3STuong Lien goto rcv;
1931fc1b6d6dSTuong Lien if (tipc_aead_clone(&tmp, aead) < 0)
1932fc1b6d6dSTuong Lien goto rcv;
19332a2403caSXin Long WARN_ON(!refcount_inc_not_zero(&tmp->refcnt));
1934daef1ee3STuong Lien if (tipc_crypto_key_attach(rx, tmp, ehdr->tx_key, false) < 0) {
1935fc1b6d6dSTuong Lien tipc_aead_free(&tmp->rcu);
1936fc1b6d6dSTuong Lien goto rcv;
1937fc1b6d6dSTuong Lien }
1938fc1b6d6dSTuong Lien tipc_aead_put(aead);
19392a2403caSXin Long aead = tmp;
1940fc1b6d6dSTuong Lien }
1941fc1b6d6dSTuong Lien
1942fc1b6d6dSTuong Lien if (unlikely(err)) {
194397bc84bbSHoang Huu Le tipc_aead_users_dec((struct tipc_aead __force __rcu *)aead, INT_MIN);
1944fc1b6d6dSTuong Lien goto free_skb;
1945fc1b6d6dSTuong Lien }
1946fc1b6d6dSTuong Lien
1947fc1b6d6dSTuong Lien /* Set the RX key's user */
194897bc84bbSHoang Huu Le tipc_aead_users_set((struct tipc_aead __force __rcu *)aead, 1);
1949fc1b6d6dSTuong Lien
1950fc1b6d6dSTuong Lien /* Mark this point, RX works */
1951fc1b6d6dSTuong Lien rx->timer1 = jiffies;
1952fc1b6d6dSTuong Lien
1953daef1ee3STuong Lien rcv:
1954fc1b6d6dSTuong Lien /* Remove ehdr & auth. tag prior to tipc_rcv() */
1955fc1b6d6dSTuong Lien ehdr = (struct tipc_ehdr *)(*skb)->data;
1956f779bf79STuong Lien
1957f779bf79STuong Lien /* Mark this point, RX passive still works */
1958f779bf79STuong Lien if (rx->key.passive && ehdr->tx_key == rx->key.passive)
1959f779bf79STuong Lien rx->timer2 = jiffies;
1960f779bf79STuong Lien
1961f779bf79STuong Lien skb_reset_network_header(*skb);
1962fc1b6d6dSTuong Lien skb_pull(*skb, tipc_ehdr_size(ehdr));
1963e46e06ffSYuanjun Gong if (pskb_trim(*skb, (*skb)->len - aead->authsize))
1964e46e06ffSYuanjun Gong goto free_skb;
1965fc1b6d6dSTuong Lien
1966fc1b6d6dSTuong Lien /* Validate TIPCv2 message */
1967fc1b6d6dSTuong Lien if (unlikely(!tipc_msg_validate(skb))) {
1968fc1b6d6dSTuong Lien pr_err_ratelimited("Packet dropped after decryption!\n");
1969fc1b6d6dSTuong Lien goto free_skb;
1970fc1b6d6dSTuong Lien }
1971fc1b6d6dSTuong Lien
1972f779bf79STuong Lien /* Ok, everything's fine, try to synch own keys according to peers' */
1973f779bf79STuong Lien tipc_crypto_key_synch(rx, *skb);
1974fc1b6d6dSTuong Lien
19753067bc61SXin Long /* Re-fetch skb cb as skb might be changed in tipc_msg_validate */
19763067bc61SXin Long skb_cb = TIPC_SKB_CB(*skb);
19773067bc61SXin Long
1978fc1b6d6dSTuong Lien /* Mark skb decrypted */
1979fc1b6d6dSTuong Lien skb_cb->decrypted = 1;
1980fc1b6d6dSTuong Lien
1981fc1b6d6dSTuong Lien /* Clear clone cxt if any */
1982fc1b6d6dSTuong Lien if (likely(!skb_cb->tx_clone_deferred))
1983fc1b6d6dSTuong Lien goto exit;
1984fc1b6d6dSTuong Lien skb_cb->tx_clone_deferred = 0;
1985fc1b6d6dSTuong Lien memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1986fc1b6d6dSTuong Lien goto exit;
1987fc1b6d6dSTuong Lien
1988fc1b6d6dSTuong Lien free_skb:
1989fc1b6d6dSTuong Lien kfree_skb(*skb);
1990fc1b6d6dSTuong Lien *skb = NULL;
1991fc1b6d6dSTuong Lien
1992fc1b6d6dSTuong Lien exit:
1993fc1b6d6dSTuong Lien tipc_aead_put(aead);
1994fc1b6d6dSTuong Lien if (rx)
1995fc1b6d6dSTuong Lien tipc_node_put(rx->node);
1996fc1b6d6dSTuong Lien }
1997fc1b6d6dSTuong Lien
tipc_crypto_do_cmd(struct net * net,int cmd)1998fc1b6d6dSTuong Lien static void tipc_crypto_do_cmd(struct net *net, int cmd)
1999fc1b6d6dSTuong Lien {
2000fc1b6d6dSTuong Lien struct tipc_net *tn = tipc_net(net);
2001fc1b6d6dSTuong Lien struct tipc_crypto *tx = tn->crypto_tx, *rx;
2002fc1b6d6dSTuong Lien struct list_head *p;
2003fc1b6d6dSTuong Lien unsigned int stat;
2004fc1b6d6dSTuong Lien int i, j, cpu;
2005fc1b6d6dSTuong Lien char buf[200];
2006fc1b6d6dSTuong Lien
2007fc1b6d6dSTuong Lien /* Currently only one command is supported */
2008fc1b6d6dSTuong Lien switch (cmd) {
2009fc1b6d6dSTuong Lien case 0xfff1:
2010fc1b6d6dSTuong Lien goto print_stats;
2011fc1b6d6dSTuong Lien default:
2012fc1b6d6dSTuong Lien return;
2013fc1b6d6dSTuong Lien }
2014fc1b6d6dSTuong Lien
2015fc1b6d6dSTuong Lien print_stats:
2016fc1b6d6dSTuong Lien /* Print a header */
2017fc1b6d6dSTuong Lien pr_info("\n=============== TIPC Crypto Statistics ===============\n\n");
2018fc1b6d6dSTuong Lien
2019fc1b6d6dSTuong Lien /* Print key status */
2020fc1b6d6dSTuong Lien pr_info("Key status:\n");
2021fc1b6d6dSTuong Lien pr_info("TX(%7.7s)\n%s", tipc_own_id_string(net),
2022fc1b6d6dSTuong Lien tipc_crypto_key_dump(tx, buf));
2023fc1b6d6dSTuong Lien
2024fc1b6d6dSTuong Lien rcu_read_lock();
2025fc1b6d6dSTuong Lien for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
2026fc1b6d6dSTuong Lien rx = tipc_node_crypto_rx_by_list(p);
2027fc1b6d6dSTuong Lien pr_info("RX(%7.7s)\n%s", tipc_node_get_id_str(rx->node),
2028fc1b6d6dSTuong Lien tipc_crypto_key_dump(rx, buf));
2029fc1b6d6dSTuong Lien }
2030fc1b6d6dSTuong Lien rcu_read_unlock();
2031fc1b6d6dSTuong Lien
2032fc1b6d6dSTuong Lien /* Print crypto statistics */
2033fc1b6d6dSTuong Lien for (i = 0, j = 0; i < MAX_STATS; i++)
2034fc1b6d6dSTuong Lien j += scnprintf(buf + j, 200 - j, "|%11s ", hstats[i]);
2035f779bf79STuong Lien pr_info("Counter %s", buf);
2036fc1b6d6dSTuong Lien
2037fc1b6d6dSTuong Lien memset(buf, '-', 115);
2038fc1b6d6dSTuong Lien buf[115] = '\0';
2039fc1b6d6dSTuong Lien pr_info("%s\n", buf);
2040fc1b6d6dSTuong Lien
2041fc1b6d6dSTuong Lien j = scnprintf(buf, 200, "TX(%7.7s) ", tipc_own_id_string(net));
2042fc1b6d6dSTuong Lien for_each_possible_cpu(cpu) {
2043fc1b6d6dSTuong Lien for (i = 0; i < MAX_STATS; i++) {
2044fc1b6d6dSTuong Lien stat = per_cpu_ptr(tx->stats, cpu)->stat[i];
2045fc1b6d6dSTuong Lien j += scnprintf(buf + j, 200 - j, "|%11d ", stat);
2046fc1b6d6dSTuong Lien }
2047fc1b6d6dSTuong Lien pr_info("%s", buf);
2048fc1b6d6dSTuong Lien j = scnprintf(buf, 200, "%12s", " ");
2049fc1b6d6dSTuong Lien }
2050fc1b6d6dSTuong Lien
2051fc1b6d6dSTuong Lien rcu_read_lock();
2052fc1b6d6dSTuong Lien for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
2053fc1b6d6dSTuong Lien rx = tipc_node_crypto_rx_by_list(p);
2054fc1b6d6dSTuong Lien j = scnprintf(buf, 200, "RX(%7.7s) ",
2055fc1b6d6dSTuong Lien tipc_node_get_id_str(rx->node));
2056fc1b6d6dSTuong Lien for_each_possible_cpu(cpu) {
2057fc1b6d6dSTuong Lien for (i = 0; i < MAX_STATS; i++) {
2058fc1b6d6dSTuong Lien stat = per_cpu_ptr(rx->stats, cpu)->stat[i];
2059fc1b6d6dSTuong Lien j += scnprintf(buf + j, 200 - j, "|%11d ",
2060fc1b6d6dSTuong Lien stat);
2061fc1b6d6dSTuong Lien }
2062fc1b6d6dSTuong Lien pr_info("%s", buf);
2063fc1b6d6dSTuong Lien j = scnprintf(buf, 200, "%12s", " ");
2064fc1b6d6dSTuong Lien }
2065fc1b6d6dSTuong Lien }
2066fc1b6d6dSTuong Lien rcu_read_unlock();
2067fc1b6d6dSTuong Lien
2068fc1b6d6dSTuong Lien pr_info("\n======================== Done ========================\n");
2069fc1b6d6dSTuong Lien }
2070fc1b6d6dSTuong Lien
tipc_crypto_key_dump(struct tipc_crypto * c,char * buf)2071fc1b6d6dSTuong Lien static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf)
2072fc1b6d6dSTuong Lien {
2073fc1b6d6dSTuong Lien struct tipc_key key = c->key;
2074fc1b6d6dSTuong Lien struct tipc_aead *aead;
2075fc1b6d6dSTuong Lien int k, i = 0;
2076fc1b6d6dSTuong Lien char *s;
2077fc1b6d6dSTuong Lien
2078fc1b6d6dSTuong Lien for (k = KEY_MIN; k <= KEY_MAX; k++) {
2079daef1ee3STuong Lien if (k == KEY_MASTER) {
2080daef1ee3STuong Lien if (is_rx(c))
2081daef1ee3STuong Lien continue;
2082daef1ee3STuong Lien if (time_before(jiffies,
2083daef1ee3STuong Lien c->timer2 + TIPC_TX_GRACE_PERIOD))
2084daef1ee3STuong Lien s = "ACT";
2085daef1ee3STuong Lien else
2086daef1ee3STuong Lien s = "PAS";
2087daef1ee3STuong Lien } else {
2088fc1b6d6dSTuong Lien if (k == key.passive)
2089fc1b6d6dSTuong Lien s = "PAS";
2090fc1b6d6dSTuong Lien else if (k == key.active)
2091fc1b6d6dSTuong Lien s = "ACT";
2092fc1b6d6dSTuong Lien else if (k == key.pending)
2093fc1b6d6dSTuong Lien s = "PEN";
2094fc1b6d6dSTuong Lien else
2095fc1b6d6dSTuong Lien s = "-";
2096daef1ee3STuong Lien }
2097fc1b6d6dSTuong Lien i += scnprintf(buf + i, 200 - i, "\tKey%d: %s", k, s);
2098fc1b6d6dSTuong Lien
2099fc1b6d6dSTuong Lien rcu_read_lock();
2100fc1b6d6dSTuong Lien aead = rcu_dereference(c->aead[k]);
2101fc1b6d6dSTuong Lien if (aead)
2102fc1b6d6dSTuong Lien i += scnprintf(buf + i, 200 - i,
2103f779bf79STuong Lien "{\"0x...%s\", \"%s\"}/%d:%d",
2104fc1b6d6dSTuong Lien aead->hint,
2105fc1b6d6dSTuong Lien (aead->mode == CLUSTER_KEY) ? "c" : "p",
2106fc1b6d6dSTuong Lien atomic_read(&aead->users),
2107fc1b6d6dSTuong Lien refcount_read(&aead->refcnt));
2108fc1b6d6dSTuong Lien rcu_read_unlock();
2109fc1b6d6dSTuong Lien i += scnprintf(buf + i, 200 - i, "\n");
2110fc1b6d6dSTuong Lien }
2111fc1b6d6dSTuong Lien
2112f779bf79STuong Lien if (is_rx(c))
2113fc1b6d6dSTuong Lien i += scnprintf(buf + i, 200 - i, "\tPeer RX active: %d\n",
2114fc1b6d6dSTuong Lien atomic_read(&c->peer_rx_active));
2115fc1b6d6dSTuong Lien
2116fc1b6d6dSTuong Lien return buf;
2117fc1b6d6dSTuong Lien }
2118fc1b6d6dSTuong Lien
tipc_key_change_dump(struct tipc_key old,struct tipc_key new,char * buf)2119fc1b6d6dSTuong Lien static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
2120fc1b6d6dSTuong Lien char *buf)
2121fc1b6d6dSTuong Lien {
2122fc1b6d6dSTuong Lien struct tipc_key *key = &old;
2123fc1b6d6dSTuong Lien int k, i = 0;
2124fc1b6d6dSTuong Lien char *s;
2125fc1b6d6dSTuong Lien
2126fc1b6d6dSTuong Lien /* Output format: "[%s %s %s] -> [%s %s %s]", max len = 32 */
2127fc1b6d6dSTuong Lien again:
2128fc1b6d6dSTuong Lien i += scnprintf(buf + i, 32 - i, "[");
2129daef1ee3STuong Lien for (k = KEY_1; k <= KEY_3; k++) {
2130fc1b6d6dSTuong Lien if (k == key->passive)
2131fc1b6d6dSTuong Lien s = "pas";
2132fc1b6d6dSTuong Lien else if (k == key->active)
2133fc1b6d6dSTuong Lien s = "act";
2134fc1b6d6dSTuong Lien else if (k == key->pending)
2135fc1b6d6dSTuong Lien s = "pen";
2136fc1b6d6dSTuong Lien else
2137fc1b6d6dSTuong Lien s = "-";
2138fc1b6d6dSTuong Lien i += scnprintf(buf + i, 32 - i,
2139daef1ee3STuong Lien (k != KEY_3) ? "%s " : "%s", s);
2140fc1b6d6dSTuong Lien }
2141fc1b6d6dSTuong Lien if (key != &new) {
2142fc1b6d6dSTuong Lien i += scnprintf(buf + i, 32 - i, "] -> ");
2143fc1b6d6dSTuong Lien key = &new;
2144fc1b6d6dSTuong Lien goto again;
2145fc1b6d6dSTuong Lien }
2146fc1b6d6dSTuong Lien i += scnprintf(buf + i, 32 - i, "]");
2147fc1b6d6dSTuong Lien return buf;
2148fc1b6d6dSTuong Lien }
21491ef6f7c9STuong Lien
21501ef6f7c9STuong Lien /**
21511ef6f7c9STuong Lien * tipc_crypto_msg_rcv - Common 'MSG_CRYPTO' processing point
21521ef6f7c9STuong Lien * @net: the struct net
21531ef6f7c9STuong Lien * @skb: the receiving message buffer
21541ef6f7c9STuong Lien */
tipc_crypto_msg_rcv(struct net * net,struct sk_buff * skb)21551ef6f7c9STuong Lien void tipc_crypto_msg_rcv(struct net *net, struct sk_buff *skb)
21561ef6f7c9STuong Lien {
21571ef6f7c9STuong Lien struct tipc_crypto *rx;
21581ef6f7c9STuong Lien struct tipc_msg *hdr;
21591ef6f7c9STuong Lien
21601ef6f7c9STuong Lien if (unlikely(skb_linearize(skb)))
21611ef6f7c9STuong Lien goto exit;
21621ef6f7c9STuong Lien
21631ef6f7c9STuong Lien hdr = buf_msg(skb);
21641ef6f7c9STuong Lien rx = tipc_node_crypto_rx_by_addr(net, msg_prevnode(hdr));
21651ef6f7c9STuong Lien if (unlikely(!rx))
21661ef6f7c9STuong Lien goto exit;
21671ef6f7c9STuong Lien
21681ef6f7c9STuong Lien switch (msg_type(hdr)) {
21691ef6f7c9STuong Lien case KEY_DISTR_MSG:
21701ef6f7c9STuong Lien if (tipc_crypto_key_rcv(rx, hdr))
21711ef6f7c9STuong Lien goto exit;
21721ef6f7c9STuong Lien break;
21731ef6f7c9STuong Lien default:
21741ef6f7c9STuong Lien break;
21751ef6f7c9STuong Lien }
21761ef6f7c9STuong Lien
21771ef6f7c9STuong Lien tipc_node_put(rx->node);
21781ef6f7c9STuong Lien
21791ef6f7c9STuong Lien exit:
21801ef6f7c9STuong Lien kfree_skb(skb);
21811ef6f7c9STuong Lien }
21821ef6f7c9STuong Lien
21831ef6f7c9STuong Lien /**
21841ef6f7c9STuong Lien * tipc_crypto_key_distr - Distribute a TX key
21851ef6f7c9STuong Lien * @tx: the TX crypto
21861ef6f7c9STuong Lien * @key: the key's index
21871ef6f7c9STuong Lien * @dest: the destination tipc node, = NULL if distributing to all nodes
21881ef6f7c9STuong Lien *
21891ef6f7c9STuong Lien * Return: 0 in case of success, otherwise < 0
21901ef6f7c9STuong Lien */
tipc_crypto_key_distr(struct tipc_crypto * tx,u8 key,struct tipc_node * dest)21911ef6f7c9STuong Lien int tipc_crypto_key_distr(struct tipc_crypto *tx, u8 key,
21921ef6f7c9STuong Lien struct tipc_node *dest)
21931ef6f7c9STuong Lien {
21941ef6f7c9STuong Lien struct tipc_aead *aead;
21951ef6f7c9STuong Lien u32 dnode = tipc_node_get_addr(dest);
21961ef6f7c9STuong Lien int rc = -ENOKEY;
21971ef6f7c9STuong Lien
21981ef6f7c9STuong Lien if (!sysctl_tipc_key_exchange_enabled)
21991ef6f7c9STuong Lien return 0;
22001ef6f7c9STuong Lien
22011ef6f7c9STuong Lien if (key) {
22021ef6f7c9STuong Lien rcu_read_lock();
22031ef6f7c9STuong Lien aead = tipc_aead_get(tx->aead[key]);
22041ef6f7c9STuong Lien if (likely(aead)) {
22051ef6f7c9STuong Lien rc = tipc_crypto_key_xmit(tx->net, aead->key,
22061ef6f7c9STuong Lien aead->gen, aead->mode,
22071ef6f7c9STuong Lien dnode);
22081ef6f7c9STuong Lien tipc_aead_put(aead);
22091ef6f7c9STuong Lien }
22101ef6f7c9STuong Lien rcu_read_unlock();
22111ef6f7c9STuong Lien }
22121ef6f7c9STuong Lien
22131ef6f7c9STuong Lien return rc;
22141ef6f7c9STuong Lien }
22151ef6f7c9STuong Lien
22161ef6f7c9STuong Lien /**
22171ef6f7c9STuong Lien * tipc_crypto_key_xmit - Send a session key
22181ef6f7c9STuong Lien * @net: the struct net
22191ef6f7c9STuong Lien * @skey: the session key to be sent
22201ef6f7c9STuong Lien * @gen: the key's generation
22211ef6f7c9STuong Lien * @mode: the key's mode
22221ef6f7c9STuong Lien * @dnode: the destination node address, = 0 if broadcasting to all nodes
22231ef6f7c9STuong Lien *
22241ef6f7c9STuong Lien * The session key 'skey' is packed in a TIPC v2 'MSG_CRYPTO/KEY_DISTR_MSG'
22251ef6f7c9STuong Lien * as its data section, then xmit-ed through the uc/bc link.
22261ef6f7c9STuong Lien *
22271ef6f7c9STuong Lien * Return: 0 in case of success, otherwise < 0
22281ef6f7c9STuong Lien */
tipc_crypto_key_xmit(struct net * net,struct tipc_aead_key * skey,u16 gen,u8 mode,u32 dnode)22291ef6f7c9STuong Lien static int tipc_crypto_key_xmit(struct net *net, struct tipc_aead_key *skey,
22301ef6f7c9STuong Lien u16 gen, u8 mode, u32 dnode)
22311ef6f7c9STuong Lien {
22321ef6f7c9STuong Lien struct sk_buff_head pkts;
22331ef6f7c9STuong Lien struct tipc_msg *hdr;
22341ef6f7c9STuong Lien struct sk_buff *skb;
22351ef6f7c9STuong Lien u16 size, cong_link_cnt;
22361ef6f7c9STuong Lien u8 *data;
22371ef6f7c9STuong Lien int rc;
22381ef6f7c9STuong Lien
22391ef6f7c9STuong Lien size = tipc_aead_key_size(skey);
22401ef6f7c9STuong Lien skb = tipc_buf_acquire(INT_H_SIZE + size, GFP_ATOMIC);
22411ef6f7c9STuong Lien if (!skb)
22421ef6f7c9STuong Lien return -ENOMEM;
22431ef6f7c9STuong Lien
22441ef6f7c9STuong Lien hdr = buf_msg(skb);
22451ef6f7c9STuong Lien tipc_msg_init(tipc_own_addr(net), hdr, MSG_CRYPTO, KEY_DISTR_MSG,
22461ef6f7c9STuong Lien INT_H_SIZE, dnode);
22471ef6f7c9STuong Lien msg_set_size(hdr, INT_H_SIZE + size);
22481ef6f7c9STuong Lien msg_set_key_gen(hdr, gen);
22491ef6f7c9STuong Lien msg_set_key_mode(hdr, mode);
22501ef6f7c9STuong Lien
22511ef6f7c9STuong Lien data = msg_data(hdr);
22521ef6f7c9STuong Lien *((__be32 *)(data + TIPC_AEAD_ALG_NAME)) = htonl(skey->keylen);
22531ef6f7c9STuong Lien memcpy(data, skey->alg_name, TIPC_AEAD_ALG_NAME);
22541ef6f7c9STuong Lien memcpy(data + TIPC_AEAD_ALG_NAME + sizeof(__be32), skey->key,
22551ef6f7c9STuong Lien skey->keylen);
22561ef6f7c9STuong Lien
22571ef6f7c9STuong Lien __skb_queue_head_init(&pkts);
22581ef6f7c9STuong Lien __skb_queue_tail(&pkts, skb);
22591ef6f7c9STuong Lien if (dnode)
22601ef6f7c9STuong Lien rc = tipc_node_xmit(net, &pkts, dnode, 0);
22611ef6f7c9STuong Lien else
22621ef6f7c9STuong Lien rc = tipc_bcast_xmit(net, &pkts, &cong_link_cnt);
22631ef6f7c9STuong Lien
22641ef6f7c9STuong Lien return rc;
22651ef6f7c9STuong Lien }
22661ef6f7c9STuong Lien
22671ef6f7c9STuong Lien /**
22681ef6f7c9STuong Lien * tipc_crypto_key_rcv - Receive a session key
22691ef6f7c9STuong Lien * @rx: the RX crypto
22701ef6f7c9STuong Lien * @hdr: the TIPC v2 message incl. the receiving session key in its data
22711ef6f7c9STuong Lien *
22721ef6f7c9STuong Lien * This function retrieves the session key in the message from peer, then
22731ef6f7c9STuong Lien * schedules a RX work to attach the key to the corresponding RX crypto.
22741ef6f7c9STuong Lien *
22751ef6f7c9STuong Lien * Return: "true" if the key has been scheduled for attaching, otherwise
22761ef6f7c9STuong Lien * "false".
22771ef6f7c9STuong Lien */
tipc_crypto_key_rcv(struct tipc_crypto * rx,struct tipc_msg * hdr)22781ef6f7c9STuong Lien static bool tipc_crypto_key_rcv(struct tipc_crypto *rx, struct tipc_msg *hdr)
22791ef6f7c9STuong Lien {
22801ef6f7c9STuong Lien struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx;
22811ef6f7c9STuong Lien struct tipc_aead_key *skey = NULL;
22821ef6f7c9STuong Lien u16 key_gen = msg_key_gen(hdr);
2283143de8d9SHangyu Hua u32 size = msg_data_sz(hdr);
22841ef6f7c9STuong Lien u8 *data = msg_data(hdr);
2285fa40d973SMax VA unsigned int keylen;
2286fa40d973SMax VA
2287fa40d973SMax VA /* Verify whether the size can exist in the packet */
2288fa40d973SMax VA if (unlikely(size < sizeof(struct tipc_aead_key) + TIPC_AEAD_KEYLEN_MIN)) {
2289fa40d973SMax VA pr_debug("%s: message data size is too small\n", rx->name);
2290fa40d973SMax VA goto exit;
2291fa40d973SMax VA }
2292fa40d973SMax VA
2293fa40d973SMax VA keylen = ntohl(*((__be32 *)(data + TIPC_AEAD_ALG_NAME)));
2294fa40d973SMax VA
2295fa40d973SMax VA /* Verify the supplied size values */
2296*44b4b974SDan Carpenter if (unlikely(keylen > TIPC_AEAD_KEY_SIZE_MAX ||
2297*44b4b974SDan Carpenter size != keylen + sizeof(struct tipc_aead_key))) {
2298fa40d973SMax VA pr_debug("%s: invalid MSG_CRYPTO key size\n", rx->name);
2299fa40d973SMax VA goto exit;
2300fa40d973SMax VA }
23011ef6f7c9STuong Lien
23021ef6f7c9STuong Lien spin_lock(&rx->lock);
23031ef6f7c9STuong Lien if (unlikely(rx->skey || (key_gen == rx->key_gen && rx->key.keys))) {
23041ef6f7c9STuong Lien pr_err("%s: key existed <%p>, gen %d vs %d\n", rx->name,
23051ef6f7c9STuong Lien rx->skey, key_gen, rx->key_gen);
2306fa40d973SMax VA goto exit_unlock;
23071ef6f7c9STuong Lien }
23081ef6f7c9STuong Lien
23091ef6f7c9STuong Lien /* Allocate memory for the key */
23101ef6f7c9STuong Lien skey = kmalloc(size, GFP_ATOMIC);
23111ef6f7c9STuong Lien if (unlikely(!skey)) {
23121ef6f7c9STuong Lien pr_err("%s: unable to allocate memory for skey\n", rx->name);
2313fa40d973SMax VA goto exit_unlock;
23141ef6f7c9STuong Lien }
23151ef6f7c9STuong Lien
23161ef6f7c9STuong Lien /* Copy key from msg data */
2317fa40d973SMax VA skey->keylen = keylen;
23181ef6f7c9STuong Lien memcpy(skey->alg_name, data, TIPC_AEAD_ALG_NAME);
23191ef6f7c9STuong Lien memcpy(skey->key, data + TIPC_AEAD_ALG_NAME + sizeof(__be32),
23201ef6f7c9STuong Lien skey->keylen);
23211ef6f7c9STuong Lien
23221ef6f7c9STuong Lien rx->key_gen = key_gen;
23231ef6f7c9STuong Lien rx->skey_mode = msg_key_mode(hdr);
23241ef6f7c9STuong Lien rx->skey = skey;
23251ef6f7c9STuong Lien rx->nokey = 0;
23261ef6f7c9STuong Lien mb(); /* for nokey flag */
23271ef6f7c9STuong Lien
2328fa40d973SMax VA exit_unlock:
23291ef6f7c9STuong Lien spin_unlock(&rx->lock);
23301ef6f7c9STuong Lien
2331fa40d973SMax VA exit:
23321ef6f7c9STuong Lien /* Schedule the key attaching on this crypto */
23331ef6f7c9STuong Lien if (likely(skey && queue_delayed_work(tx->wq, &rx->work, 0)))
23341ef6f7c9STuong Lien return true;
23351ef6f7c9STuong Lien
23361ef6f7c9STuong Lien return false;
23371ef6f7c9STuong Lien }
23381ef6f7c9STuong Lien
23391ef6f7c9STuong Lien /**
23401ef6f7c9STuong Lien * tipc_crypto_work_rx - Scheduled RX works handler
23411ef6f7c9STuong Lien * @work: the struct RX work
23421ef6f7c9STuong Lien *
23431ef6f7c9STuong Lien * The function processes the previous scheduled works i.e. distributing TX key
23441ef6f7c9STuong Lien * or attaching a received session key on RX crypto.
23451ef6f7c9STuong Lien */
tipc_crypto_work_rx(struct work_struct * work)23461ef6f7c9STuong Lien static void tipc_crypto_work_rx(struct work_struct *work)
23471ef6f7c9STuong Lien {
23481ef6f7c9STuong Lien struct delayed_work *dwork = to_delayed_work(work);
23491ef6f7c9STuong Lien struct tipc_crypto *rx = container_of(dwork, struct tipc_crypto, work);
23501ef6f7c9STuong Lien struct tipc_crypto *tx = tipc_net(rx->net)->crypto_tx;
23511ef6f7c9STuong Lien unsigned long delay = msecs_to_jiffies(5000);
23521ef6f7c9STuong Lien bool resched = false;
23531ef6f7c9STuong Lien u8 key;
23541ef6f7c9STuong Lien int rc;
23551ef6f7c9STuong Lien
23561ef6f7c9STuong Lien /* Case 1: Distribute TX key to peer if scheduled */
23571ef6f7c9STuong Lien if (atomic_cmpxchg(&rx->key_distr,
23581ef6f7c9STuong Lien KEY_DISTR_SCHED,
23591ef6f7c9STuong Lien KEY_DISTR_COMPL) == KEY_DISTR_SCHED) {
23601ef6f7c9STuong Lien /* Always pick the newest one for distributing */
23611ef6f7c9STuong Lien key = tx->key.pending ?: tx->key.active;
23621ef6f7c9STuong Lien rc = tipc_crypto_key_distr(tx, key, rx->node);
23631ef6f7c9STuong Lien if (unlikely(rc))
23641ef6f7c9STuong Lien pr_warn("%s: unable to distr key[%d] to %s, err %d\n",
23651ef6f7c9STuong Lien tx->name, key, tipc_node_get_id_str(rx->node),
23661ef6f7c9STuong Lien rc);
23671ef6f7c9STuong Lien
23681ef6f7c9STuong Lien /* Sched for key_distr releasing */
23691ef6f7c9STuong Lien resched = true;
23701ef6f7c9STuong Lien } else {
23711ef6f7c9STuong Lien atomic_cmpxchg(&rx->key_distr, KEY_DISTR_COMPL, 0);
23721ef6f7c9STuong Lien }
23731ef6f7c9STuong Lien
23741ef6f7c9STuong Lien /* Case 2: Attach a pending received session key from peer if any */
23751ef6f7c9STuong Lien if (rx->skey) {
23761ef6f7c9STuong Lien rc = tipc_crypto_key_init(rx, rx->skey, rx->skey_mode, false);
23771ef6f7c9STuong Lien if (unlikely(rc < 0))
23781ef6f7c9STuong Lien pr_warn("%s: unable to attach received skey, err %d\n",
23791ef6f7c9STuong Lien rx->name, rc);
23801ef6f7c9STuong Lien switch (rc) {
23811ef6f7c9STuong Lien case -EBUSY:
23821ef6f7c9STuong Lien case -ENOMEM:
23831ef6f7c9STuong Lien /* Resched the key attaching */
23841ef6f7c9STuong Lien resched = true;
23851ef6f7c9STuong Lien break;
23861ef6f7c9STuong Lien default:
23871ef6f7c9STuong Lien synchronize_rcu();
23881ef6f7c9STuong Lien kfree(rx->skey);
23891ef6f7c9STuong Lien rx->skey = NULL;
23901ef6f7c9STuong Lien break;
23911ef6f7c9STuong Lien }
23921ef6f7c9STuong Lien }
23931ef6f7c9STuong Lien
23941ef6f7c9STuong Lien if (resched && queue_delayed_work(tx->wq, &rx->work, delay))
23951ef6f7c9STuong Lien return;
23961ef6f7c9STuong Lien
23971ef6f7c9STuong Lien tipc_node_put(rx->node);
23981ef6f7c9STuong Lien }
239923700da2STuong Lien
240023700da2STuong Lien /**
240123700da2STuong Lien * tipc_crypto_rekeying_sched - (Re)schedule rekeying w/o new interval
240223700da2STuong Lien * @tx: TX crypto
240323700da2STuong Lien * @changed: if the rekeying needs to be rescheduled with new interval
240423700da2STuong Lien * @new_intv: new rekeying interval (when "changed" = true)
240523700da2STuong Lien */
tipc_crypto_rekeying_sched(struct tipc_crypto * tx,bool changed,u32 new_intv)240623700da2STuong Lien void tipc_crypto_rekeying_sched(struct tipc_crypto *tx, bool changed,
240723700da2STuong Lien u32 new_intv)
240823700da2STuong Lien {
240923700da2STuong Lien unsigned long delay;
241023700da2STuong Lien bool now = false;
241123700da2STuong Lien
241223700da2STuong Lien if (changed) {
241323700da2STuong Lien if (new_intv == TIPC_REKEYING_NOW)
241423700da2STuong Lien now = true;
241523700da2STuong Lien else
241623700da2STuong Lien tx->rekeying_intv = new_intv;
241723700da2STuong Lien cancel_delayed_work_sync(&tx->work);
241823700da2STuong Lien }
241923700da2STuong Lien
242023700da2STuong Lien if (tx->rekeying_intv || now) {
242123700da2STuong Lien delay = (now) ? 0 : tx->rekeying_intv * 60 * 1000;
242223700da2STuong Lien queue_delayed_work(tx->wq, &tx->work, msecs_to_jiffies(delay));
242323700da2STuong Lien }
242423700da2STuong Lien }
242523700da2STuong Lien
242623700da2STuong Lien /**
242723700da2STuong Lien * tipc_crypto_work_tx - Scheduled TX works handler
242823700da2STuong Lien * @work: the struct TX work
242923700da2STuong Lien *
243023700da2STuong Lien * The function processes the previous scheduled work, i.e. key rekeying, by
243123700da2STuong Lien * generating a new session key based on current one, then attaching it to the
243223700da2STuong Lien * TX crypto and finally distributing it to peers. It also re-schedules the
243323700da2STuong Lien * rekeying if needed.
243423700da2STuong Lien */
tipc_crypto_work_tx(struct work_struct * work)243523700da2STuong Lien static void tipc_crypto_work_tx(struct work_struct *work)
243623700da2STuong Lien {
243723700da2STuong Lien struct delayed_work *dwork = to_delayed_work(work);
243823700da2STuong Lien struct tipc_crypto *tx = container_of(dwork, struct tipc_crypto, work);
243923700da2STuong Lien struct tipc_aead_key *skey = NULL;
244023700da2STuong Lien struct tipc_key key = tx->key;
244123700da2STuong Lien struct tipc_aead *aead;
244223700da2STuong Lien int rc = -ENOMEM;
244323700da2STuong Lien
244423700da2STuong Lien if (unlikely(key.pending))
244523700da2STuong Lien goto resched;
244623700da2STuong Lien
244723700da2STuong Lien /* Take current key as a template */
244823700da2STuong Lien rcu_read_lock();
244923700da2STuong Lien aead = rcu_dereference(tx->aead[key.active ?: KEY_MASTER]);
245023700da2STuong Lien if (unlikely(!aead)) {
245123700da2STuong Lien rcu_read_unlock();
245223700da2STuong Lien /* At least one key should exist for securing */
245323700da2STuong Lien return;
245423700da2STuong Lien }
245523700da2STuong Lien
245623700da2STuong Lien /* Lets duplicate it first */
2457f845fe58SHoang Le skey = kmemdup(aead->key, tipc_aead_key_size(aead->key), GFP_ATOMIC);
245823700da2STuong Lien rcu_read_unlock();
245923700da2STuong Lien
246023700da2STuong Lien /* Now, generate new key, initiate & distribute it */
246123700da2STuong Lien if (likely(skey)) {
246223700da2STuong Lien rc = tipc_aead_key_generate(skey) ?:
246323700da2STuong Lien tipc_crypto_key_init(tx, skey, PER_NODE_KEY, false);
246423700da2STuong Lien if (likely(rc > 0))
246523700da2STuong Lien rc = tipc_crypto_key_distr(tx, rc, NULL);
246623224e45SEric Biggers kfree_sensitive(skey);
246723700da2STuong Lien }
246823700da2STuong Lien
246923700da2STuong Lien if (unlikely(rc))
247023700da2STuong Lien pr_warn_ratelimited("%s: rekeying returns %d\n", tx->name, rc);
247123700da2STuong Lien
247223700da2STuong Lien resched:
247323700da2STuong Lien /* Re-schedule rekeying if any */
247423700da2STuong Lien tipc_crypto_rekeying_sched(tx, false, 0);
247523700da2STuong Lien }
2476