xref: /openbmc/linux/net/tls/tls_sw.c (revision 4de30a8d)
13c4d7559SDave Watson /*
23c4d7559SDave Watson  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
33c4d7559SDave Watson  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
43c4d7559SDave Watson  * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved.
53c4d7559SDave Watson  * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved.
63c4d7559SDave Watson  * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved.
7d3b18ad3SJohn Fastabend  * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
83c4d7559SDave Watson  *
93c4d7559SDave Watson  * This software is available to you under a choice of one of two
103c4d7559SDave Watson  * licenses.  You may choose to be licensed under the terms of the GNU
113c4d7559SDave Watson  * General Public License (GPL) Version 2, available from the file
123c4d7559SDave Watson  * COPYING in the main directory of this source tree, or the
133c4d7559SDave Watson  * OpenIB.org BSD license below:
143c4d7559SDave Watson  *
153c4d7559SDave Watson  *     Redistribution and use in source and binary forms, with or
163c4d7559SDave Watson  *     without modification, are permitted provided that the following
173c4d7559SDave Watson  *     conditions are met:
183c4d7559SDave Watson  *
193c4d7559SDave Watson  *      - Redistributions of source code must retain the above
203c4d7559SDave Watson  *        copyright notice, this list of conditions and the following
213c4d7559SDave Watson  *        disclaimer.
223c4d7559SDave Watson  *
233c4d7559SDave Watson  *      - Redistributions in binary form must reproduce the above
243c4d7559SDave Watson  *        copyright notice, this list of conditions and the following
253c4d7559SDave Watson  *        disclaimer in the documentation and/or other materials
263c4d7559SDave Watson  *        provided with the distribution.
273c4d7559SDave Watson  *
283c4d7559SDave Watson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
293c4d7559SDave Watson  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
303c4d7559SDave Watson  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
313c4d7559SDave Watson  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
323c4d7559SDave Watson  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
333c4d7559SDave Watson  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
343c4d7559SDave Watson  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
353c4d7559SDave Watson  * SOFTWARE.
363c4d7559SDave Watson  */
373c4d7559SDave Watson 
38c46234ebSDave Watson #include <linux/sched/signal.h>
393c4d7559SDave Watson #include <linux/module.h>
403c4d7559SDave Watson #include <crypto/aead.h>
413c4d7559SDave Watson 
42c46234ebSDave Watson #include <net/strparser.h>
433c4d7559SDave Watson #include <net/tls.h>
443c4d7559SDave Watson 
450927f71dSDoron Roberts-Kedes static int __skb_nsg(struct sk_buff *skb, int offset, int len,
460927f71dSDoron Roberts-Kedes                      unsigned int recursion_level)
470927f71dSDoron Roberts-Kedes {
480927f71dSDoron Roberts-Kedes         int start = skb_headlen(skb);
490927f71dSDoron Roberts-Kedes         int i, chunk = start - offset;
500927f71dSDoron Roberts-Kedes         struct sk_buff *frag_iter;
510927f71dSDoron Roberts-Kedes         int elt = 0;
520927f71dSDoron Roberts-Kedes 
530927f71dSDoron Roberts-Kedes         if (unlikely(recursion_level >= 24))
540927f71dSDoron Roberts-Kedes                 return -EMSGSIZE;
550927f71dSDoron Roberts-Kedes 
560927f71dSDoron Roberts-Kedes         if (chunk > 0) {
570927f71dSDoron Roberts-Kedes                 if (chunk > len)
580927f71dSDoron Roberts-Kedes                         chunk = len;
590927f71dSDoron Roberts-Kedes                 elt++;
600927f71dSDoron Roberts-Kedes                 len -= chunk;
610927f71dSDoron Roberts-Kedes                 if (len == 0)
620927f71dSDoron Roberts-Kedes                         return elt;
630927f71dSDoron Roberts-Kedes                 offset += chunk;
640927f71dSDoron Roberts-Kedes         }
650927f71dSDoron Roberts-Kedes 
660927f71dSDoron Roberts-Kedes         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
670927f71dSDoron Roberts-Kedes                 int end;
680927f71dSDoron Roberts-Kedes 
690927f71dSDoron Roberts-Kedes                 WARN_ON(start > offset + len);
700927f71dSDoron Roberts-Kedes 
710927f71dSDoron Roberts-Kedes                 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
720927f71dSDoron Roberts-Kedes                 chunk = end - offset;
730927f71dSDoron Roberts-Kedes                 if (chunk > 0) {
740927f71dSDoron Roberts-Kedes                         if (chunk > len)
750927f71dSDoron Roberts-Kedes                                 chunk = len;
760927f71dSDoron Roberts-Kedes                         elt++;
770927f71dSDoron Roberts-Kedes                         len -= chunk;
780927f71dSDoron Roberts-Kedes                         if (len == 0)
790927f71dSDoron Roberts-Kedes                                 return elt;
800927f71dSDoron Roberts-Kedes                         offset += chunk;
810927f71dSDoron Roberts-Kedes                 }
820927f71dSDoron Roberts-Kedes                 start = end;
830927f71dSDoron Roberts-Kedes         }
840927f71dSDoron Roberts-Kedes 
850927f71dSDoron Roberts-Kedes         if (unlikely(skb_has_frag_list(skb))) {
860927f71dSDoron Roberts-Kedes                 skb_walk_frags(skb, frag_iter) {
870927f71dSDoron Roberts-Kedes                         int end, ret;
880927f71dSDoron Roberts-Kedes 
890927f71dSDoron Roberts-Kedes                         WARN_ON(start > offset + len);
900927f71dSDoron Roberts-Kedes 
910927f71dSDoron Roberts-Kedes                         end = start + frag_iter->len;
920927f71dSDoron Roberts-Kedes                         chunk = end - offset;
930927f71dSDoron Roberts-Kedes                         if (chunk > 0) {
940927f71dSDoron Roberts-Kedes                                 if (chunk > len)
950927f71dSDoron Roberts-Kedes                                         chunk = len;
960927f71dSDoron Roberts-Kedes                                 ret = __skb_nsg(frag_iter, offset - start, chunk,
970927f71dSDoron Roberts-Kedes                                                 recursion_level + 1);
980927f71dSDoron Roberts-Kedes                                 if (unlikely(ret < 0))
990927f71dSDoron Roberts-Kedes                                         return ret;
1000927f71dSDoron Roberts-Kedes                                 elt += ret;
1010927f71dSDoron Roberts-Kedes                                 len -= chunk;
1020927f71dSDoron Roberts-Kedes                                 if (len == 0)
1030927f71dSDoron Roberts-Kedes                                         return elt;
1040927f71dSDoron Roberts-Kedes                                 offset += chunk;
1050927f71dSDoron Roberts-Kedes                         }
1060927f71dSDoron Roberts-Kedes                         start = end;
1070927f71dSDoron Roberts-Kedes                 }
1080927f71dSDoron Roberts-Kedes         }
1090927f71dSDoron Roberts-Kedes         BUG_ON(len);
1100927f71dSDoron Roberts-Kedes         return elt;
1110927f71dSDoron Roberts-Kedes }
1120927f71dSDoron Roberts-Kedes 
1130927f71dSDoron Roberts-Kedes /* Return the number of scatterlist elements required to completely map the
1140927f71dSDoron Roberts-Kedes  * skb, or -EMSGSIZE if the recursion depth is exceeded.
1150927f71dSDoron Roberts-Kedes  */
1160927f71dSDoron Roberts-Kedes static int skb_nsg(struct sk_buff *skb, int offset, int len)
1170927f71dSDoron Roberts-Kedes {
1180927f71dSDoron Roberts-Kedes         return __skb_nsg(skb, offset, len, 0);
1190927f71dSDoron Roberts-Kedes }
1200927f71dSDoron Roberts-Kedes 
121130b392cSDave Watson static int padding_length(struct tls_sw_context_rx *ctx,
122b53f4976SJakub Kicinski 			  struct tls_prot_info *prot, struct sk_buff *skb)
123130b392cSDave Watson {
124130b392cSDave Watson 	struct strp_msg *rxm = strp_msg(skb);
125130b392cSDave Watson 	int sub = 0;
126130b392cSDave Watson 
127130b392cSDave Watson 	/* Determine zero-padding length */
128b53f4976SJakub Kicinski 	if (prot->version == TLS_1_3_VERSION) {
129130b392cSDave Watson 		char content_type = 0;
130130b392cSDave Watson 		int err;
131130b392cSDave Watson 		int back = 17;
132130b392cSDave Watson 
133130b392cSDave Watson 		while (content_type == 0) {
134b53f4976SJakub Kicinski 			if (back > rxm->full_len - prot->prepend_size)
135130b392cSDave Watson 				return -EBADMSG;
136130b392cSDave Watson 			err = skb_copy_bits(skb,
137130b392cSDave Watson 					    rxm->offset + rxm->full_len - back,
138130b392cSDave Watson 					    &content_type, 1);
139b53f4976SJakub Kicinski 			if (err)
140b53f4976SJakub Kicinski 				return err;
141130b392cSDave Watson 			if (content_type)
142130b392cSDave Watson 				break;
143130b392cSDave Watson 			sub++;
144130b392cSDave Watson 			back++;
145130b392cSDave Watson 		}
146130b392cSDave Watson 		ctx->control = content_type;
147130b392cSDave Watson 	}
148130b392cSDave Watson 	return sub;
149130b392cSDave Watson }
150130b392cSDave Watson 
15194524d8fSVakul Garg static void tls_decrypt_done(struct crypto_async_request *req, int err)
15294524d8fSVakul Garg {
15394524d8fSVakul Garg 	struct aead_request *aead_req = (struct aead_request *)req;
15494524d8fSVakul Garg 	struct scatterlist *sgout = aead_req->dst;
155692d7b5dSVakul Garg 	struct scatterlist *sgin = aead_req->src;
1567a3dd8c8SJohn Fastabend 	struct tls_sw_context_rx *ctx;
1577a3dd8c8SJohn Fastabend 	struct tls_context *tls_ctx;
1584509de14SVakul Garg 	struct tls_prot_info *prot;
15994524d8fSVakul Garg 	struct scatterlist *sg;
1607a3dd8c8SJohn Fastabend 	struct sk_buff *skb;
16194524d8fSVakul Garg 	unsigned int pages;
1627a3dd8c8SJohn Fastabend 	int pending;
1637a3dd8c8SJohn Fastabend 
1647a3dd8c8SJohn Fastabend 	skb = (struct sk_buff *)req->data;
1657a3dd8c8SJohn Fastabend 	tls_ctx = tls_get_ctx(skb->sk);
1667a3dd8c8SJohn Fastabend 	ctx = tls_sw_ctx_rx(tls_ctx);
1674509de14SVakul Garg 	prot = &tls_ctx->prot_info;
16894524d8fSVakul Garg 
16994524d8fSVakul Garg 	/* Propagate if there was an err */
17094524d8fSVakul Garg 	if (err) {
1715c5ec668SJakub Kicinski 		if (err == -EBADMSG)
1725c5ec668SJakub Kicinski 			TLS_INC_STATS(sock_net(skb->sk),
1735c5ec668SJakub Kicinski 				      LINUX_MIB_TLSDECRYPTERROR);
17494524d8fSVakul Garg 		ctx->async_wait.err = err;
1757a3dd8c8SJohn Fastabend 		tls_err_abort(skb->sk, err);
176692d7b5dSVakul Garg 	} else {
177692d7b5dSVakul Garg 		struct strp_msg *rxm = strp_msg(skb);
178b53f4976SJakub Kicinski 		int pad;
179b53f4976SJakub Kicinski 
180b53f4976SJakub Kicinski 		pad = padding_length(ctx, prot, skb);
181b53f4976SJakub Kicinski 		if (pad < 0) {
182b53f4976SJakub Kicinski 			ctx->async_wait.err = pad;
183b53f4976SJakub Kicinski 			tls_err_abort(skb->sk, pad);
184b53f4976SJakub Kicinski 		} else {
185b53f4976SJakub Kicinski 			rxm->full_len -= pad;
1864509de14SVakul Garg 			rxm->offset += prot->prepend_size;
1874509de14SVakul Garg 			rxm->full_len -= prot->overhead_size;
18894524d8fSVakul Garg 		}
189b53f4976SJakub Kicinski 	}
19094524d8fSVakul Garg 
1917a3dd8c8SJohn Fastabend 	/* After using skb->sk to propagate sk through crypto async callback
1927a3dd8c8SJohn Fastabend 	 * we need to NULL it again.
1937a3dd8c8SJohn Fastabend 	 */
1947a3dd8c8SJohn Fastabend 	skb->sk = NULL;
1957a3dd8c8SJohn Fastabend 
19694524d8fSVakul Garg 
197692d7b5dSVakul Garg 	/* Free the destination pages if skb was not decrypted inplace */
198692d7b5dSVakul Garg 	if (sgout != sgin) {
19994524d8fSVakul Garg 		/* Skip the first S/G entry as it points to AAD */
20094524d8fSVakul Garg 		for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
20194524d8fSVakul Garg 			if (!sg)
20294524d8fSVakul Garg 				break;
20394524d8fSVakul Garg 			put_page(sg_page(sg));
20494524d8fSVakul Garg 		}
205692d7b5dSVakul Garg 	}
20694524d8fSVakul Garg 
20794524d8fSVakul Garg 	kfree(aead_req);
20894524d8fSVakul Garg 
209692d7b5dSVakul Garg 	pending = atomic_dec_return(&ctx->decrypt_pending);
210692d7b5dSVakul Garg 
21194524d8fSVakul Garg 	if (!pending && READ_ONCE(ctx->async_notify))
21294524d8fSVakul Garg 		complete(&ctx->async_wait.completion);
21394524d8fSVakul Garg }
21494524d8fSVakul Garg 
215c46234ebSDave Watson static int tls_do_decryption(struct sock *sk,
21694524d8fSVakul Garg 			     struct sk_buff *skb,
217c46234ebSDave Watson 			     struct scatterlist *sgin,
218c46234ebSDave Watson 			     struct scatterlist *sgout,
219c46234ebSDave Watson 			     char *iv_recv,
220c46234ebSDave Watson 			     size_t data_len,
22194524d8fSVakul Garg 			     struct aead_request *aead_req,
22294524d8fSVakul Garg 			     bool async)
223c46234ebSDave Watson {
224c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2254509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
226f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
227c46234ebSDave Watson 	int ret;
228c46234ebSDave Watson 
2290b243d00SVakul Garg 	aead_request_set_tfm(aead_req, ctx->aead_recv);
2304509de14SVakul Garg 	aead_request_set_ad(aead_req, prot->aad_size);
231c46234ebSDave Watson 	aead_request_set_crypt(aead_req, sgin, sgout,
2324509de14SVakul Garg 			       data_len + prot->tag_size,
233c46234ebSDave Watson 			       (u8 *)iv_recv);
234c46234ebSDave Watson 
23594524d8fSVakul Garg 	if (async) {
2367a3dd8c8SJohn Fastabend 		/* Using skb->sk to push sk through to crypto async callback
2377a3dd8c8SJohn Fastabend 		 * handler. This allows propagating errors up to the socket
2387a3dd8c8SJohn Fastabend 		 * if needed. It _must_ be cleared in the async handler
239a88c26f6SVakul Garg 		 * before consume_skb is called. We _know_ skb->sk is NULL
2407a3dd8c8SJohn Fastabend 		 * because it is a clone from strparser.
2417a3dd8c8SJohn Fastabend 		 */
2427a3dd8c8SJohn Fastabend 		skb->sk = sk;
24394524d8fSVakul Garg 		aead_request_set_callback(aead_req,
24494524d8fSVakul Garg 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
24594524d8fSVakul Garg 					  tls_decrypt_done, skb);
24694524d8fSVakul Garg 		atomic_inc(&ctx->decrypt_pending);
24794524d8fSVakul Garg 	} else {
24894524d8fSVakul Garg 		aead_request_set_callback(aead_req,
24994524d8fSVakul Garg 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
25094524d8fSVakul Garg 					  crypto_req_done, &ctx->async_wait);
25194524d8fSVakul Garg 	}
25294524d8fSVakul Garg 
25394524d8fSVakul Garg 	ret = crypto_aead_decrypt(aead_req);
25494524d8fSVakul Garg 	if (ret == -EINPROGRESS) {
25594524d8fSVakul Garg 		if (async)
25694524d8fSVakul Garg 			return ret;
25794524d8fSVakul Garg 
25894524d8fSVakul Garg 		ret = crypto_wait_req(ret, &ctx->async_wait);
2595c5ec668SJakub Kicinski 	} else if (ret == -EBADMSG) {
2605c5ec668SJakub Kicinski 		TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
26194524d8fSVakul Garg 	}
26294524d8fSVakul Garg 
26394524d8fSVakul Garg 	if (async)
26494524d8fSVakul Garg 		atomic_dec(&ctx->decrypt_pending);
26594524d8fSVakul Garg 
266c46234ebSDave Watson 	return ret;
267c46234ebSDave Watson }
268c46234ebSDave Watson 
269d829e9c4SDaniel Borkmann static void tls_trim_both_msgs(struct sock *sk, int target_size)
2703c4d7559SDave Watson {
2713c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2724509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
273f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
274a42055e8SVakul Garg 	struct tls_rec *rec = ctx->open_rec;
2753c4d7559SDave Watson 
276d829e9c4SDaniel Borkmann 	sk_msg_trim(sk, &rec->msg_plaintext, target_size);
2773c4d7559SDave Watson 	if (target_size > 0)
2784509de14SVakul Garg 		target_size += prot->overhead_size;
279d829e9c4SDaniel Borkmann 	sk_msg_trim(sk, &rec->msg_encrypted, target_size);
2803c4d7559SDave Watson }
2813c4d7559SDave Watson 
282d829e9c4SDaniel Borkmann static int tls_alloc_encrypted_msg(struct sock *sk, int len)
2833c4d7559SDave Watson {
2843c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
285f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
286a42055e8SVakul Garg 	struct tls_rec *rec = ctx->open_rec;
287d829e9c4SDaniel Borkmann 	struct sk_msg *msg_en = &rec->msg_encrypted;
2883c4d7559SDave Watson 
289d829e9c4SDaniel Borkmann 	return sk_msg_alloc(sk, msg_en, len, 0);
2903c4d7559SDave Watson }
2913c4d7559SDave Watson 
292d829e9c4SDaniel Borkmann static int tls_clone_plaintext_msg(struct sock *sk, int required)
2933c4d7559SDave Watson {
2943c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2954509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
296f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
297a42055e8SVakul Garg 	struct tls_rec *rec = ctx->open_rec;
298d829e9c4SDaniel Borkmann 	struct sk_msg *msg_pl = &rec->msg_plaintext;
299d829e9c4SDaniel Borkmann 	struct sk_msg *msg_en = &rec->msg_encrypted;
3004e6d4720SVakul Garg 	int skip, len;
3013c4d7559SDave Watson 
302d829e9c4SDaniel Borkmann 	/* We add page references worth len bytes from encrypted sg
303d829e9c4SDaniel Borkmann 	 * at the end of plaintext sg. It is guaranteed that msg_en
3044e6d4720SVakul Garg 	 * has enough required room (ensured by caller).
3054e6d4720SVakul Garg 	 */
306d829e9c4SDaniel Borkmann 	len = required - msg_pl->sg.size;
30752ea992cSVakul Garg 
308d829e9c4SDaniel Borkmann 	/* Skip initial bytes in msg_en's data to be able to use
309d829e9c4SDaniel Borkmann 	 * same offset of both plain and encrypted data.
3104e6d4720SVakul Garg 	 */
3114509de14SVakul Garg 	skip = prot->prepend_size + msg_pl->sg.size;
3124e6d4720SVakul Garg 
313d829e9c4SDaniel Borkmann 	return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
3143c4d7559SDave Watson }
3153c4d7559SDave Watson 
316d3b18ad3SJohn Fastabend static struct tls_rec *tls_get_rec(struct sock *sk)
317d3b18ad3SJohn Fastabend {
318d3b18ad3SJohn Fastabend 	struct tls_context *tls_ctx = tls_get_ctx(sk);
3194509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
320d3b18ad3SJohn Fastabend 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
321d3b18ad3SJohn Fastabend 	struct sk_msg *msg_pl, *msg_en;
322d3b18ad3SJohn Fastabend 	struct tls_rec *rec;
323d3b18ad3SJohn Fastabend 	int mem_size;
324d3b18ad3SJohn Fastabend 
325d3b18ad3SJohn Fastabend 	mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
326d3b18ad3SJohn Fastabend 
327d3b18ad3SJohn Fastabend 	rec = kzalloc(mem_size, sk->sk_allocation);
328d3b18ad3SJohn Fastabend 	if (!rec)
329d3b18ad3SJohn Fastabend 		return NULL;
330d3b18ad3SJohn Fastabend 
331d3b18ad3SJohn Fastabend 	msg_pl = &rec->msg_plaintext;
332d3b18ad3SJohn Fastabend 	msg_en = &rec->msg_encrypted;
333d3b18ad3SJohn Fastabend 
334d3b18ad3SJohn Fastabend 	sk_msg_init(msg_pl);
335d3b18ad3SJohn Fastabend 	sk_msg_init(msg_en);
336d3b18ad3SJohn Fastabend 
337d3b18ad3SJohn Fastabend 	sg_init_table(rec->sg_aead_in, 2);
3384509de14SVakul Garg 	sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
339d3b18ad3SJohn Fastabend 	sg_unmark_end(&rec->sg_aead_in[1]);
340d3b18ad3SJohn Fastabend 
341d3b18ad3SJohn Fastabend 	sg_init_table(rec->sg_aead_out, 2);
3424509de14SVakul Garg 	sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
343d3b18ad3SJohn Fastabend 	sg_unmark_end(&rec->sg_aead_out[1]);
344d3b18ad3SJohn Fastabend 
345d3b18ad3SJohn Fastabend 	return rec;
346d3b18ad3SJohn Fastabend }
347d3b18ad3SJohn Fastabend 
348d3b18ad3SJohn Fastabend static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
349d3b18ad3SJohn Fastabend {
350d3b18ad3SJohn Fastabend 	sk_msg_free(sk, &rec->msg_encrypted);
351d3b18ad3SJohn Fastabend 	sk_msg_free(sk, &rec->msg_plaintext);
352d3b18ad3SJohn Fastabend 	kfree(rec);
353d3b18ad3SJohn Fastabend }
354d3b18ad3SJohn Fastabend 
355c774973eSVakul Garg static void tls_free_open_rec(struct sock *sk)
3563c4d7559SDave Watson {
3573c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
358f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
359a42055e8SVakul Garg 	struct tls_rec *rec = ctx->open_rec;
3603c4d7559SDave Watson 
361d3b18ad3SJohn Fastabend 	if (rec) {
362d3b18ad3SJohn Fastabend 		tls_free_rec(sk, rec);
363d3b18ad3SJohn Fastabend 		ctx->open_rec = NULL;
364d3b18ad3SJohn Fastabend 	}
3653c4d7559SDave Watson }
3663c4d7559SDave Watson 
367a42055e8SVakul Garg int tls_tx_records(struct sock *sk, int flags)
368a42055e8SVakul Garg {
369a42055e8SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
370a42055e8SVakul Garg 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
371a42055e8SVakul Garg 	struct tls_rec *rec, *tmp;
372d829e9c4SDaniel Borkmann 	struct sk_msg *msg_en;
373a42055e8SVakul Garg 	int tx_flags, rc = 0;
374a42055e8SVakul Garg 
375a42055e8SVakul Garg 	if (tls_is_partially_sent_record(tls_ctx)) {
3769932a29aSVakul Garg 		rec = list_first_entry(&ctx->tx_list,
377a42055e8SVakul Garg 				       struct tls_rec, list);
378a42055e8SVakul Garg 
379a42055e8SVakul Garg 		if (flags == -1)
380a42055e8SVakul Garg 			tx_flags = rec->tx_flags;
381a42055e8SVakul Garg 		else
382a42055e8SVakul Garg 			tx_flags = flags;
383a42055e8SVakul Garg 
384a42055e8SVakul Garg 		rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
385a42055e8SVakul Garg 		if (rc)
386a42055e8SVakul Garg 			goto tx_err;
387a42055e8SVakul Garg 
388a42055e8SVakul Garg 		/* Full record has been transmitted.
3899932a29aSVakul Garg 		 * Remove the head of tx_list
390a42055e8SVakul Garg 		 */
391a42055e8SVakul Garg 		list_del(&rec->list);
392d829e9c4SDaniel Borkmann 		sk_msg_free(sk, &rec->msg_plaintext);
393a42055e8SVakul Garg 		kfree(rec);
394a42055e8SVakul Garg 	}
395a42055e8SVakul Garg 
3969932a29aSVakul Garg 	/* Tx all ready records */
3979932a29aSVakul Garg 	list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
3989932a29aSVakul Garg 		if (READ_ONCE(rec->tx_ready)) {
399a42055e8SVakul Garg 			if (flags == -1)
400a42055e8SVakul Garg 				tx_flags = rec->tx_flags;
401a42055e8SVakul Garg 			else
402a42055e8SVakul Garg 				tx_flags = flags;
403a42055e8SVakul Garg 
404d829e9c4SDaniel Borkmann 			msg_en = &rec->msg_encrypted;
405a42055e8SVakul Garg 			rc = tls_push_sg(sk, tls_ctx,
406d829e9c4SDaniel Borkmann 					 &msg_en->sg.data[msg_en->sg.curr],
407a42055e8SVakul Garg 					 0, tx_flags);
408a42055e8SVakul Garg 			if (rc)
409a42055e8SVakul Garg 				goto tx_err;
410a42055e8SVakul Garg 
411a42055e8SVakul Garg 			list_del(&rec->list);
412d829e9c4SDaniel Borkmann 			sk_msg_free(sk, &rec->msg_plaintext);
413a42055e8SVakul Garg 			kfree(rec);
414a42055e8SVakul Garg 		} else {
415a42055e8SVakul Garg 			break;
416a42055e8SVakul Garg 		}
417a42055e8SVakul Garg 	}
418a42055e8SVakul Garg 
419a42055e8SVakul Garg tx_err:
420a42055e8SVakul Garg 	if (rc < 0 && rc != -EAGAIN)
421a42055e8SVakul Garg 		tls_err_abort(sk, EBADMSG);
422a42055e8SVakul Garg 
423a42055e8SVakul Garg 	return rc;
424a42055e8SVakul Garg }
425a42055e8SVakul Garg 
426a42055e8SVakul Garg static void tls_encrypt_done(struct crypto_async_request *req, int err)
427a42055e8SVakul Garg {
428a42055e8SVakul Garg 	struct aead_request *aead_req = (struct aead_request *)req;
429a42055e8SVakul Garg 	struct sock *sk = req->data;
430a42055e8SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
4314509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
432a42055e8SVakul Garg 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
433d829e9c4SDaniel Borkmann 	struct scatterlist *sge;
434d829e9c4SDaniel Borkmann 	struct sk_msg *msg_en;
435a42055e8SVakul Garg 	struct tls_rec *rec;
436a42055e8SVakul Garg 	bool ready = false;
437a42055e8SVakul Garg 	int pending;
438a42055e8SVakul Garg 
439a42055e8SVakul Garg 	rec = container_of(aead_req, struct tls_rec, aead_req);
440d829e9c4SDaniel Borkmann 	msg_en = &rec->msg_encrypted;
441a42055e8SVakul Garg 
442d829e9c4SDaniel Borkmann 	sge = sk_msg_elem(msg_en, msg_en->sg.curr);
4434509de14SVakul Garg 	sge->offset -= prot->prepend_size;
4444509de14SVakul Garg 	sge->length += prot->prepend_size;
445a42055e8SVakul Garg 
44680ece6a0SVakul Garg 	/* Check if error is previously set on socket */
447a42055e8SVakul Garg 	if (err || sk->sk_err) {
448a42055e8SVakul Garg 		rec = NULL;
449a42055e8SVakul Garg 
450a42055e8SVakul Garg 		/* If err is already set on socket, return the same code */
451a42055e8SVakul Garg 		if (sk->sk_err) {
452a42055e8SVakul Garg 			ctx->async_wait.err = sk->sk_err;
453a42055e8SVakul Garg 		} else {
454a42055e8SVakul Garg 			ctx->async_wait.err = err;
455a42055e8SVakul Garg 			tls_err_abort(sk, err);
456a42055e8SVakul Garg 		}
457a42055e8SVakul Garg 	}
458a42055e8SVakul Garg 
4599932a29aSVakul Garg 	if (rec) {
4609932a29aSVakul Garg 		struct tls_rec *first_rec;
4619932a29aSVakul Garg 
4629932a29aSVakul Garg 		/* Mark the record as ready for transmission */
4639932a29aSVakul Garg 		smp_store_mb(rec->tx_ready, true);
4649932a29aSVakul Garg 
4659932a29aSVakul Garg 		/* If received record is at head of tx_list, schedule tx */
4669932a29aSVakul Garg 		first_rec = list_first_entry(&ctx->tx_list,
4679932a29aSVakul Garg 					     struct tls_rec, list);
4689932a29aSVakul Garg 		if (rec == first_rec)
4699932a29aSVakul Garg 			ready = true;
4709932a29aSVakul Garg 	}
471a42055e8SVakul Garg 
472a42055e8SVakul Garg 	pending = atomic_dec_return(&ctx->encrypt_pending);
473a42055e8SVakul Garg 
474a42055e8SVakul Garg 	if (!pending && READ_ONCE(ctx->async_notify))
475a42055e8SVakul Garg 		complete(&ctx->async_wait.completion);
476a42055e8SVakul Garg 
477a42055e8SVakul Garg 	if (!ready)
478a42055e8SVakul Garg 		return;
479a42055e8SVakul Garg 
480a42055e8SVakul Garg 	/* Schedule the transmission */
481a42055e8SVakul Garg 	if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
482d829e9c4SDaniel Borkmann 		schedule_delayed_work(&ctx->tx_work.work, 1);
483a42055e8SVakul Garg }
484a42055e8SVakul Garg 
485a42055e8SVakul Garg static int tls_do_encryption(struct sock *sk,
486a42055e8SVakul Garg 			     struct tls_context *tls_ctx,
487a447da7dSDaniel Borkmann 			     struct tls_sw_context_tx *ctx,
488a447da7dSDaniel Borkmann 			     struct aead_request *aead_req,
489d829e9c4SDaniel Borkmann 			     size_t data_len, u32 start)
4903c4d7559SDave Watson {
4914509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
492a42055e8SVakul Garg 	struct tls_rec *rec = ctx->open_rec;
493d829e9c4SDaniel Borkmann 	struct sk_msg *msg_en = &rec->msg_encrypted;
494d829e9c4SDaniel Borkmann 	struct scatterlist *sge = sk_msg_elem(msg_en, start);
495f295b3aeSVakul Garg 	int rc, iv_offset = 0;
4963c4d7559SDave Watson 
497f295b3aeSVakul Garg 	/* For CCM based ciphers, first byte of IV is a constant */
498f295b3aeSVakul Garg 	if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) {
499f295b3aeSVakul Garg 		rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
500f295b3aeSVakul Garg 		iv_offset = 1;
501f295b3aeSVakul Garg 	}
502f295b3aeSVakul Garg 
503f295b3aeSVakul Garg 	memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
504f295b3aeSVakul Garg 	       prot->iv_size + prot->salt_size);
505f295b3aeSVakul Garg 
506f295b3aeSVakul Garg 	xor_iv_with_seq(prot->version, rec->iv_data, tls_ctx->tx.rec_seq);
50732eb67b9SDave Watson 
5084509de14SVakul Garg 	sge->offset += prot->prepend_size;
5094509de14SVakul Garg 	sge->length -= prot->prepend_size;
5103c4d7559SDave Watson 
511d829e9c4SDaniel Borkmann 	msg_en->sg.curr = start;
5124e6d4720SVakul Garg 
5133c4d7559SDave Watson 	aead_request_set_tfm(aead_req, ctx->aead_send);
5144509de14SVakul Garg 	aead_request_set_ad(aead_req, prot->aad_size);
515d829e9c4SDaniel Borkmann 	aead_request_set_crypt(aead_req, rec->sg_aead_in,
516d829e9c4SDaniel Borkmann 			       rec->sg_aead_out,
51732eb67b9SDave Watson 			       data_len, rec->iv_data);
518a54667f6SVakul Garg 
519a54667f6SVakul Garg 	aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
520a42055e8SVakul Garg 				  tls_encrypt_done, sk);
521a54667f6SVakul Garg 
5229932a29aSVakul Garg 	/* Add the record in tx_list */
5239932a29aSVakul Garg 	list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
524a42055e8SVakul Garg 	atomic_inc(&ctx->encrypt_pending);
5253c4d7559SDave Watson 
526a42055e8SVakul Garg 	rc = crypto_aead_encrypt(aead_req);
527a42055e8SVakul Garg 	if (!rc || rc != -EINPROGRESS) {
528a42055e8SVakul Garg 		atomic_dec(&ctx->encrypt_pending);
5294509de14SVakul Garg 		sge->offset -= prot->prepend_size;
5304509de14SVakul Garg 		sge->length += prot->prepend_size;
531a42055e8SVakul Garg 	}
5323c4d7559SDave Watson 
5339932a29aSVakul Garg 	if (!rc) {
5349932a29aSVakul Garg 		WRITE_ONCE(rec->tx_ready, true);
5359932a29aSVakul Garg 	} else if (rc != -EINPROGRESS) {
5369932a29aSVakul Garg 		list_del(&rec->list);
537a42055e8SVakul Garg 		return rc;
5389932a29aSVakul Garg 	}
539a42055e8SVakul Garg 
540a42055e8SVakul Garg 	/* Unhook the record from context if encryption is not failure */
541a42055e8SVakul Garg 	ctx->open_rec = NULL;
542fb0f886fSJakub Kicinski 	tls_advance_record_sn(sk, prot, &tls_ctx->tx);
5433c4d7559SDave Watson 	return rc;
5443c4d7559SDave Watson }
5453c4d7559SDave Watson 
546d3b18ad3SJohn Fastabend static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
547d3b18ad3SJohn Fastabend 				 struct tls_rec **to, struct sk_msg *msg_opl,
548d3b18ad3SJohn Fastabend 				 struct sk_msg *msg_oen, u32 split_point,
549d3b18ad3SJohn Fastabend 				 u32 tx_overhead_size, u32 *orig_end)
550d3b18ad3SJohn Fastabend {
551d3b18ad3SJohn Fastabend 	u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
552d3b18ad3SJohn Fastabend 	struct scatterlist *sge, *osge, *nsge;
553d3b18ad3SJohn Fastabend 	u32 orig_size = msg_opl->sg.size;
554d3b18ad3SJohn Fastabend 	struct scatterlist tmp = { };
555d3b18ad3SJohn Fastabend 	struct sk_msg *msg_npl;
556d3b18ad3SJohn Fastabend 	struct tls_rec *new;
557d3b18ad3SJohn Fastabend 	int ret;
558d3b18ad3SJohn Fastabend 
559d3b18ad3SJohn Fastabend 	new = tls_get_rec(sk);
560d3b18ad3SJohn Fastabend 	if (!new)
561d3b18ad3SJohn Fastabend 		return -ENOMEM;
562d3b18ad3SJohn Fastabend 	ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
563d3b18ad3SJohn Fastabend 			   tx_overhead_size, 0);
564d3b18ad3SJohn Fastabend 	if (ret < 0) {
565d3b18ad3SJohn Fastabend 		tls_free_rec(sk, new);
566d3b18ad3SJohn Fastabend 		return ret;
567d3b18ad3SJohn Fastabend 	}
568d3b18ad3SJohn Fastabend 
569d3b18ad3SJohn Fastabend 	*orig_end = msg_opl->sg.end;
570d3b18ad3SJohn Fastabend 	i = msg_opl->sg.start;
571d3b18ad3SJohn Fastabend 	sge = sk_msg_elem(msg_opl, i);
572d3b18ad3SJohn Fastabend 	while (apply && sge->length) {
573d3b18ad3SJohn Fastabend 		if (sge->length > apply) {
574d3b18ad3SJohn Fastabend 			u32 len = sge->length - apply;
575d3b18ad3SJohn Fastabend 
576d3b18ad3SJohn Fastabend 			get_page(sg_page(sge));
577d3b18ad3SJohn Fastabend 			sg_set_page(&tmp, sg_page(sge), len,
578d3b18ad3SJohn Fastabend 				    sge->offset + apply);
579d3b18ad3SJohn Fastabend 			sge->length = apply;
580d3b18ad3SJohn Fastabend 			bytes += apply;
581d3b18ad3SJohn Fastabend 			apply = 0;
582d3b18ad3SJohn Fastabend 		} else {
583d3b18ad3SJohn Fastabend 			apply -= sge->length;
584d3b18ad3SJohn Fastabend 			bytes += sge->length;
585d3b18ad3SJohn Fastabend 		}
586d3b18ad3SJohn Fastabend 
587d3b18ad3SJohn Fastabend 		sk_msg_iter_var_next(i);
588d3b18ad3SJohn Fastabend 		if (i == msg_opl->sg.end)
589d3b18ad3SJohn Fastabend 			break;
590d3b18ad3SJohn Fastabend 		sge = sk_msg_elem(msg_opl, i);
591d3b18ad3SJohn Fastabend 	}
592d3b18ad3SJohn Fastabend 
593d3b18ad3SJohn Fastabend 	msg_opl->sg.end = i;
594d3b18ad3SJohn Fastabend 	msg_opl->sg.curr = i;
595d3b18ad3SJohn Fastabend 	msg_opl->sg.copybreak = 0;
596d3b18ad3SJohn Fastabend 	msg_opl->apply_bytes = 0;
597d3b18ad3SJohn Fastabend 	msg_opl->sg.size = bytes;
598d3b18ad3SJohn Fastabend 
599d3b18ad3SJohn Fastabend 	msg_npl = &new->msg_plaintext;
600d3b18ad3SJohn Fastabend 	msg_npl->apply_bytes = apply;
601d3b18ad3SJohn Fastabend 	msg_npl->sg.size = orig_size - bytes;
602d3b18ad3SJohn Fastabend 
603d3b18ad3SJohn Fastabend 	j = msg_npl->sg.start;
604d3b18ad3SJohn Fastabend 	nsge = sk_msg_elem(msg_npl, j);
605d3b18ad3SJohn Fastabend 	if (tmp.length) {
606d3b18ad3SJohn Fastabend 		memcpy(nsge, &tmp, sizeof(*nsge));
607d3b18ad3SJohn Fastabend 		sk_msg_iter_var_next(j);
608d3b18ad3SJohn Fastabend 		nsge = sk_msg_elem(msg_npl, j);
609d3b18ad3SJohn Fastabend 	}
610d3b18ad3SJohn Fastabend 
611d3b18ad3SJohn Fastabend 	osge = sk_msg_elem(msg_opl, i);
612d3b18ad3SJohn Fastabend 	while (osge->length) {
613d3b18ad3SJohn Fastabend 		memcpy(nsge, osge, sizeof(*nsge));
614d3b18ad3SJohn Fastabend 		sg_unmark_end(nsge);
615d3b18ad3SJohn Fastabend 		sk_msg_iter_var_next(i);
616d3b18ad3SJohn Fastabend 		sk_msg_iter_var_next(j);
617d3b18ad3SJohn Fastabend 		if (i == *orig_end)
618d3b18ad3SJohn Fastabend 			break;
619d3b18ad3SJohn Fastabend 		osge = sk_msg_elem(msg_opl, i);
620d3b18ad3SJohn Fastabend 		nsge = sk_msg_elem(msg_npl, j);
621d3b18ad3SJohn Fastabend 	}
622d3b18ad3SJohn Fastabend 
623d3b18ad3SJohn Fastabend 	msg_npl->sg.end = j;
624d3b18ad3SJohn Fastabend 	msg_npl->sg.curr = j;
625d3b18ad3SJohn Fastabend 	msg_npl->sg.copybreak = 0;
626d3b18ad3SJohn Fastabend 
627d3b18ad3SJohn Fastabend 	*to = new;
628d3b18ad3SJohn Fastabend 	return 0;
629d3b18ad3SJohn Fastabend }
630d3b18ad3SJohn Fastabend 
631d3b18ad3SJohn Fastabend static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
632d3b18ad3SJohn Fastabend 				  struct tls_rec *from, u32 orig_end)
633d3b18ad3SJohn Fastabend {
634d3b18ad3SJohn Fastabend 	struct sk_msg *msg_npl = &from->msg_plaintext;
635d3b18ad3SJohn Fastabend 	struct sk_msg *msg_opl = &to->msg_plaintext;
636d3b18ad3SJohn Fastabend 	struct scatterlist *osge, *nsge;
637d3b18ad3SJohn Fastabend 	u32 i, j;
638d3b18ad3SJohn Fastabend 
639d3b18ad3SJohn Fastabend 	i = msg_opl->sg.end;
640d3b18ad3SJohn Fastabend 	sk_msg_iter_var_prev(i);
641d3b18ad3SJohn Fastabend 	j = msg_npl->sg.start;
642d3b18ad3SJohn Fastabend 
643d3b18ad3SJohn Fastabend 	osge = sk_msg_elem(msg_opl, i);
644d3b18ad3SJohn Fastabend 	nsge = sk_msg_elem(msg_npl, j);
645d3b18ad3SJohn Fastabend 
646d3b18ad3SJohn Fastabend 	if (sg_page(osge) == sg_page(nsge) &&
647d3b18ad3SJohn Fastabend 	    osge->offset + osge->length == nsge->offset) {
648d3b18ad3SJohn Fastabend 		osge->length += nsge->length;
649d3b18ad3SJohn Fastabend 		put_page(sg_page(nsge));
650d3b18ad3SJohn Fastabend 	}
651d3b18ad3SJohn Fastabend 
652d3b18ad3SJohn Fastabend 	msg_opl->sg.end = orig_end;
653d3b18ad3SJohn Fastabend 	msg_opl->sg.curr = orig_end;
654d3b18ad3SJohn Fastabend 	msg_opl->sg.copybreak = 0;
655d3b18ad3SJohn Fastabend 	msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
656d3b18ad3SJohn Fastabend 	msg_opl->sg.size += msg_npl->sg.size;
657d3b18ad3SJohn Fastabend 
658d3b18ad3SJohn Fastabend 	sk_msg_free(sk, &to->msg_encrypted);
659d3b18ad3SJohn Fastabend 	sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
660d3b18ad3SJohn Fastabend 
661d3b18ad3SJohn Fastabend 	kfree(from);
662d3b18ad3SJohn Fastabend }
663d3b18ad3SJohn Fastabend 
6643c4d7559SDave Watson static int tls_push_record(struct sock *sk, int flags,
6653c4d7559SDave Watson 			   unsigned char record_type)
6663c4d7559SDave Watson {
6673c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
6684509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
669f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
670d3b18ad3SJohn Fastabend 	struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
671d3b18ad3SJohn Fastabend 	u32 i, split_point, uninitialized_var(orig_end);
672d829e9c4SDaniel Borkmann 	struct sk_msg *msg_pl, *msg_en;
673a447da7dSDaniel Borkmann 	struct aead_request *req;
674d3b18ad3SJohn Fastabend 	bool split;
6753c4d7559SDave Watson 	int rc;
6763c4d7559SDave Watson 
677a42055e8SVakul Garg 	if (!rec)
678a42055e8SVakul Garg 		return 0;
679a447da7dSDaniel Borkmann 
680d829e9c4SDaniel Borkmann 	msg_pl = &rec->msg_plaintext;
681d829e9c4SDaniel Borkmann 	msg_en = &rec->msg_encrypted;
682d829e9c4SDaniel Borkmann 
683d3b18ad3SJohn Fastabend 	split_point = msg_pl->apply_bytes;
684d3b18ad3SJohn Fastabend 	split = split_point && split_point < msg_pl->sg.size;
685d3b18ad3SJohn Fastabend 	if (split) {
686d3b18ad3SJohn Fastabend 		rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
6874509de14SVakul Garg 					   split_point, prot->overhead_size,
688d3b18ad3SJohn Fastabend 					   &orig_end);
689d3b18ad3SJohn Fastabend 		if (rc < 0)
690d3b18ad3SJohn Fastabend 			return rc;
691d3b18ad3SJohn Fastabend 		sk_msg_trim(sk, msg_en, msg_pl->sg.size +
6924509de14SVakul Garg 			    prot->overhead_size);
693d3b18ad3SJohn Fastabend 	}
694d3b18ad3SJohn Fastabend 
695a42055e8SVakul Garg 	rec->tx_flags = flags;
696a42055e8SVakul Garg 	req = &rec->aead_req;
6973c4d7559SDave Watson 
698d829e9c4SDaniel Borkmann 	i = msg_pl->sg.end;
699d829e9c4SDaniel Borkmann 	sk_msg_iter_var_prev(i);
700130b392cSDave Watson 
701130b392cSDave Watson 	rec->content_type = record_type;
7024509de14SVakul Garg 	if (prot->version == TLS_1_3_VERSION) {
703130b392cSDave Watson 		/* Add content type to end of message.  No padding added */
704130b392cSDave Watson 		sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
705130b392cSDave Watson 		sg_mark_end(&rec->sg_content_type);
706130b392cSDave Watson 		sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
707130b392cSDave Watson 			 &rec->sg_content_type);
708130b392cSDave Watson 	} else {
709d829e9c4SDaniel Borkmann 		sg_mark_end(sk_msg_elem(msg_pl, i));
710130b392cSDave Watson 	}
711a42055e8SVakul Garg 
712d829e9c4SDaniel Borkmann 	i = msg_pl->sg.start;
713d829e9c4SDaniel Borkmann 	sg_chain(rec->sg_aead_in, 2, rec->inplace_crypto ?
714d829e9c4SDaniel Borkmann 		 &msg_en->sg.data[i] : &msg_pl->sg.data[i]);
715d829e9c4SDaniel Borkmann 
716d829e9c4SDaniel Borkmann 	i = msg_en->sg.end;
717d829e9c4SDaniel Borkmann 	sk_msg_iter_var_prev(i);
718d829e9c4SDaniel Borkmann 	sg_mark_end(sk_msg_elem(msg_en, i));
719d829e9c4SDaniel Borkmann 
720d829e9c4SDaniel Borkmann 	i = msg_en->sg.start;
721d829e9c4SDaniel Borkmann 	sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
722d829e9c4SDaniel Borkmann 
7234509de14SVakul Garg 	tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
7244509de14SVakul Garg 		     tls_ctx->tx.rec_seq, prot->rec_seq_size,
7254509de14SVakul Garg 		     record_type, prot->version);
7263c4d7559SDave Watson 
7273c4d7559SDave Watson 	tls_fill_prepend(tls_ctx,
728d829e9c4SDaniel Borkmann 			 page_address(sg_page(&msg_en->sg.data[i])) +
729130b392cSDave Watson 			 msg_en->sg.data[i].offset,
7304509de14SVakul Garg 			 msg_pl->sg.size + prot->tail_size,
7314509de14SVakul Garg 			 record_type, prot->version);
7323c4d7559SDave Watson 
733d829e9c4SDaniel Borkmann 	tls_ctx->pending_open_record_frags = false;
7343c4d7559SDave Watson 
735130b392cSDave Watson 	rc = tls_do_encryption(sk, tls_ctx, ctx, req,
7364509de14SVakul Garg 			       msg_pl->sg.size + prot->tail_size, i);
7373c4d7559SDave Watson 	if (rc < 0) {
738d3b18ad3SJohn Fastabend 		if (rc != -EINPROGRESS) {
739a42055e8SVakul Garg 			tls_err_abort(sk, EBADMSG);
740d3b18ad3SJohn Fastabend 			if (split) {
741d3b18ad3SJohn Fastabend 				tls_ctx->pending_open_record_frags = true;
742d3b18ad3SJohn Fastabend 				tls_merge_open_record(sk, rec, tmp, orig_end);
743d3b18ad3SJohn Fastabend 			}
744d3b18ad3SJohn Fastabend 		}
7455b053e12SDave Watson 		ctx->async_capable = 1;
746a42055e8SVakul Garg 		return rc;
747d3b18ad3SJohn Fastabend 	} else if (split) {
748d3b18ad3SJohn Fastabend 		msg_pl = &tmp->msg_plaintext;
749d3b18ad3SJohn Fastabend 		msg_en = &tmp->msg_encrypted;
7504509de14SVakul Garg 		sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
751d3b18ad3SJohn Fastabend 		tls_ctx->pending_open_record_frags = true;
752d3b18ad3SJohn Fastabend 		ctx->open_rec = tmp;
7533c4d7559SDave Watson 	}
7543c4d7559SDave Watson 
755a42055e8SVakul Garg 	return tls_tx_records(sk, flags);
7563c4d7559SDave Watson }
7573c4d7559SDave Watson 
758d3b18ad3SJohn Fastabend static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
759d3b18ad3SJohn Fastabend 			       bool full_record, u8 record_type,
760d3b18ad3SJohn Fastabend 			       size_t *copied, int flags)
7613c4d7559SDave Watson {
7623c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
763f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
764d3b18ad3SJohn Fastabend 	struct sk_msg msg_redir = { };
765d3b18ad3SJohn Fastabend 	struct sk_psock *psock;
766d3b18ad3SJohn Fastabend 	struct sock *sk_redir;
767a42055e8SVakul Garg 	struct tls_rec *rec;
7680608c69cSJohn Fastabend 	bool enospc, policy;
769d3b18ad3SJohn Fastabend 	int err = 0, send;
7707246d8edSJohn Fastabend 	u32 delta = 0;
771a42055e8SVakul Garg 
7720608c69cSJohn Fastabend 	policy = !(flags & MSG_SENDPAGE_NOPOLICY);
773d3b18ad3SJohn Fastabend 	psock = sk_psock_get(sk);
7740608c69cSJohn Fastabend 	if (!psock || !policy)
775d3b18ad3SJohn Fastabend 		return tls_push_record(sk, flags, record_type);
776d3b18ad3SJohn Fastabend more_data:
777d3b18ad3SJohn Fastabend 	enospc = sk_msg_full(msg);
7787246d8edSJohn Fastabend 	if (psock->eval == __SK_NONE) {
7797246d8edSJohn Fastabend 		delta = msg->sg.size;
780d3b18ad3SJohn Fastabend 		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
7817246d8edSJohn Fastabend 		if (delta < msg->sg.size)
7827246d8edSJohn Fastabend 			delta -= msg->sg.size;
7837246d8edSJohn Fastabend 		else
7847246d8edSJohn Fastabend 			delta = 0;
7857246d8edSJohn Fastabend 	}
786d3b18ad3SJohn Fastabend 	if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
787d3b18ad3SJohn Fastabend 	    !enospc && !full_record) {
788d3b18ad3SJohn Fastabend 		err = -ENOSPC;
789d3b18ad3SJohn Fastabend 		goto out_err;
790d3b18ad3SJohn Fastabend 	}
791d3b18ad3SJohn Fastabend 	msg->cork_bytes = 0;
792d3b18ad3SJohn Fastabend 	send = msg->sg.size;
793d3b18ad3SJohn Fastabend 	if (msg->apply_bytes && msg->apply_bytes < send)
794d3b18ad3SJohn Fastabend 		send = msg->apply_bytes;
795a42055e8SVakul Garg 
796d3b18ad3SJohn Fastabend 	switch (psock->eval) {
797d3b18ad3SJohn Fastabend 	case __SK_PASS:
798d3b18ad3SJohn Fastabend 		err = tls_push_record(sk, flags, record_type);
799d3b18ad3SJohn Fastabend 		if (err < 0) {
800d3b18ad3SJohn Fastabend 			*copied -= sk_msg_free(sk, msg);
801d3b18ad3SJohn Fastabend 			tls_free_open_rec(sk);
802d3b18ad3SJohn Fastabend 			goto out_err;
803d3b18ad3SJohn Fastabend 		}
804d3b18ad3SJohn Fastabend 		break;
805d3b18ad3SJohn Fastabend 	case __SK_REDIRECT:
806d3b18ad3SJohn Fastabend 		sk_redir = psock->sk_redir;
807d3b18ad3SJohn Fastabend 		memcpy(&msg_redir, msg, sizeof(*msg));
808d3b18ad3SJohn Fastabend 		if (msg->apply_bytes < send)
809d3b18ad3SJohn Fastabend 			msg->apply_bytes = 0;
810d3b18ad3SJohn Fastabend 		else
811d3b18ad3SJohn Fastabend 			msg->apply_bytes -= send;
812d3b18ad3SJohn Fastabend 		sk_msg_return_zero(sk, msg, send);
813d3b18ad3SJohn Fastabend 		msg->sg.size -= send;
814d3b18ad3SJohn Fastabend 		release_sock(sk);
815d3b18ad3SJohn Fastabend 		err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
816d3b18ad3SJohn Fastabend 		lock_sock(sk);
817d3b18ad3SJohn Fastabend 		if (err < 0) {
818d3b18ad3SJohn Fastabend 			*copied -= sk_msg_free_nocharge(sk, &msg_redir);
819d3b18ad3SJohn Fastabend 			msg->sg.size = 0;
820d3b18ad3SJohn Fastabend 		}
821d3b18ad3SJohn Fastabend 		if (msg->sg.size == 0)
822d3b18ad3SJohn Fastabend 			tls_free_open_rec(sk);
823d3b18ad3SJohn Fastabend 		break;
824d3b18ad3SJohn Fastabend 	case __SK_DROP:
825d3b18ad3SJohn Fastabend 	default:
826d3b18ad3SJohn Fastabend 		sk_msg_free_partial(sk, msg, send);
827d3b18ad3SJohn Fastabend 		if (msg->apply_bytes < send)
828d3b18ad3SJohn Fastabend 			msg->apply_bytes = 0;
829d3b18ad3SJohn Fastabend 		else
830d3b18ad3SJohn Fastabend 			msg->apply_bytes -= send;
831d3b18ad3SJohn Fastabend 		if (msg->sg.size == 0)
832d3b18ad3SJohn Fastabend 			tls_free_open_rec(sk);
8337246d8edSJohn Fastabend 		*copied -= (send + delta);
834d3b18ad3SJohn Fastabend 		err = -EACCES;
835d3b18ad3SJohn Fastabend 	}
836a42055e8SVakul Garg 
837d3b18ad3SJohn Fastabend 	if (likely(!err)) {
838d3b18ad3SJohn Fastabend 		bool reset_eval = !ctx->open_rec;
839d3b18ad3SJohn Fastabend 
840d3b18ad3SJohn Fastabend 		rec = ctx->open_rec;
841d3b18ad3SJohn Fastabend 		if (rec) {
842d3b18ad3SJohn Fastabend 			msg = &rec->msg_plaintext;
843d3b18ad3SJohn Fastabend 			if (!msg->apply_bytes)
844d3b18ad3SJohn Fastabend 				reset_eval = true;
845d3b18ad3SJohn Fastabend 		}
846d3b18ad3SJohn Fastabend 		if (reset_eval) {
847d3b18ad3SJohn Fastabend 			psock->eval = __SK_NONE;
848d3b18ad3SJohn Fastabend 			if (psock->sk_redir) {
849d3b18ad3SJohn Fastabend 				sock_put(psock->sk_redir);
850d3b18ad3SJohn Fastabend 				psock->sk_redir = NULL;
851d3b18ad3SJohn Fastabend 			}
852d3b18ad3SJohn Fastabend 		}
853d3b18ad3SJohn Fastabend 		if (rec)
854d3b18ad3SJohn Fastabend 			goto more_data;
855d3b18ad3SJohn Fastabend 	}
856d3b18ad3SJohn Fastabend  out_err:
857d3b18ad3SJohn Fastabend 	sk_psock_put(sk, psock);
858d3b18ad3SJohn Fastabend 	return err;
859d3b18ad3SJohn Fastabend }
860d3b18ad3SJohn Fastabend 
861d3b18ad3SJohn Fastabend static int tls_sw_push_pending_record(struct sock *sk, int flags)
862d3b18ad3SJohn Fastabend {
863d3b18ad3SJohn Fastabend 	struct tls_context *tls_ctx = tls_get_ctx(sk);
864d3b18ad3SJohn Fastabend 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
865d3b18ad3SJohn Fastabend 	struct tls_rec *rec = ctx->open_rec;
866d3b18ad3SJohn Fastabend 	struct sk_msg *msg_pl;
867d3b18ad3SJohn Fastabend 	size_t copied;
868d3b18ad3SJohn Fastabend 
869a42055e8SVakul Garg 	if (!rec)
870d3b18ad3SJohn Fastabend 		return 0;
871a42055e8SVakul Garg 
872d829e9c4SDaniel Borkmann 	msg_pl = &rec->msg_plaintext;
873d3b18ad3SJohn Fastabend 	copied = msg_pl->sg.size;
874d3b18ad3SJohn Fastabend 	if (!copied)
875d3b18ad3SJohn Fastabend 		return 0;
876a42055e8SVakul Garg 
877d3b18ad3SJohn Fastabend 	return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
878d3b18ad3SJohn Fastabend 				   &copied, flags);
879a42055e8SVakul Garg }
880a42055e8SVakul Garg 
881a42055e8SVakul Garg int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
882a42055e8SVakul Garg {
8833c4d7559SDave Watson 	long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
884a42055e8SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
8854509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
886a42055e8SVakul Garg 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
8875b053e12SDave Watson 	bool async_capable = ctx->async_capable;
888a42055e8SVakul Garg 	unsigned char record_type = TLS_RECORD_TYPE_DATA;
88900e23707SDavid Howells 	bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
8903c4d7559SDave Watson 	bool eor = !(msg->msg_flags & MSG_MORE);
8913c4d7559SDave Watson 	size_t try_to_copy, copied = 0;
892d829e9c4SDaniel Borkmann 	struct sk_msg *msg_pl, *msg_en;
893a42055e8SVakul Garg 	struct tls_rec *rec;
894a42055e8SVakul Garg 	int required_size;
895a42055e8SVakul Garg 	int num_async = 0;
8963c4d7559SDave Watson 	bool full_record;
897a42055e8SVakul Garg 	int record_room;
898a42055e8SVakul Garg 	int num_zc = 0;
8993c4d7559SDave Watson 	int orig_size;
9004128c0cfSVakul Garg 	int ret = 0;
9013c4d7559SDave Watson 
9023c4d7559SDave Watson 	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
9033c4d7559SDave Watson 		return -ENOTSUPP;
9043c4d7559SDave Watson 
9053c4d7559SDave Watson 	lock_sock(sk);
9063c4d7559SDave Watson 
907a42055e8SVakul Garg 	/* Wait till there is any pending write on socket */
908a42055e8SVakul Garg 	if (unlikely(sk->sk_write_pending)) {
909a42055e8SVakul Garg 		ret = wait_on_pending_writer(sk, &timeo);
910a42055e8SVakul Garg 		if (unlikely(ret))
9113c4d7559SDave Watson 			goto send_end;
912a42055e8SVakul Garg 	}
9133c4d7559SDave Watson 
9143c4d7559SDave Watson 	if (unlikely(msg->msg_controllen)) {
9153c4d7559SDave Watson 		ret = tls_proccess_cmsg(sk, msg, &record_type);
916a42055e8SVakul Garg 		if (ret) {
917a42055e8SVakul Garg 			if (ret == -EINPROGRESS)
918a42055e8SVakul Garg 				num_async++;
919a42055e8SVakul Garg 			else if (ret != -EAGAIN)
9203c4d7559SDave Watson 				goto send_end;
9213c4d7559SDave Watson 		}
922a42055e8SVakul Garg 	}
9233c4d7559SDave Watson 
9243c4d7559SDave Watson 	while (msg_data_left(msg)) {
9253c4d7559SDave Watson 		if (sk->sk_err) {
92630be8f8dSr.hering@avm.de 			ret = -sk->sk_err;
9273c4d7559SDave Watson 			goto send_end;
9283c4d7559SDave Watson 		}
9293c4d7559SDave Watson 
930d3b18ad3SJohn Fastabend 		if (ctx->open_rec)
931d3b18ad3SJohn Fastabend 			rec = ctx->open_rec;
932d3b18ad3SJohn Fastabend 		else
933d3b18ad3SJohn Fastabend 			rec = ctx->open_rec = tls_get_rec(sk);
934a42055e8SVakul Garg 		if (!rec) {
935a42055e8SVakul Garg 			ret = -ENOMEM;
936a42055e8SVakul Garg 			goto send_end;
937a42055e8SVakul Garg 		}
938a42055e8SVakul Garg 
939d829e9c4SDaniel Borkmann 		msg_pl = &rec->msg_plaintext;
940d829e9c4SDaniel Borkmann 		msg_en = &rec->msg_encrypted;
941d829e9c4SDaniel Borkmann 
942d829e9c4SDaniel Borkmann 		orig_size = msg_pl->sg.size;
9433c4d7559SDave Watson 		full_record = false;
9443c4d7559SDave Watson 		try_to_copy = msg_data_left(msg);
945d829e9c4SDaniel Borkmann 		record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
9463c4d7559SDave Watson 		if (try_to_copy >= record_room) {
9473c4d7559SDave Watson 			try_to_copy = record_room;
9483c4d7559SDave Watson 			full_record = true;
9493c4d7559SDave Watson 		}
9503c4d7559SDave Watson 
951d829e9c4SDaniel Borkmann 		required_size = msg_pl->sg.size + try_to_copy +
9524509de14SVakul Garg 				prot->overhead_size;
9533c4d7559SDave Watson 
9543c4d7559SDave Watson 		if (!sk_stream_memory_free(sk))
9553c4d7559SDave Watson 			goto wait_for_sndbuf;
956a42055e8SVakul Garg 
9573c4d7559SDave Watson alloc_encrypted:
958d829e9c4SDaniel Borkmann 		ret = tls_alloc_encrypted_msg(sk, required_size);
9593c4d7559SDave Watson 		if (ret) {
9603c4d7559SDave Watson 			if (ret != -ENOSPC)
9613c4d7559SDave Watson 				goto wait_for_memory;
9623c4d7559SDave Watson 
9633c4d7559SDave Watson 			/* Adjust try_to_copy according to the amount that was
9643c4d7559SDave Watson 			 * actually allocated. The difference is due
9653c4d7559SDave Watson 			 * to max sg elements limit
9663c4d7559SDave Watson 			 */
967d829e9c4SDaniel Borkmann 			try_to_copy -= required_size - msg_en->sg.size;
9683c4d7559SDave Watson 			full_record = true;
9693c4d7559SDave Watson 		}
970a42055e8SVakul Garg 
971a42055e8SVakul Garg 		if (!is_kvec && (full_record || eor) && !async_capable) {
972d3b18ad3SJohn Fastabend 			u32 first = msg_pl->sg.end;
973d3b18ad3SJohn Fastabend 
974d829e9c4SDaniel Borkmann 			ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
975d829e9c4SDaniel Borkmann 							msg_pl, try_to_copy);
9763c4d7559SDave Watson 			if (ret)
9773c4d7559SDave Watson 				goto fallback_to_reg_send;
9783c4d7559SDave Watson 
9794e6d4720SVakul Garg 			rec->inplace_crypto = 0;
9804e6d4720SVakul Garg 
981a42055e8SVakul Garg 			num_zc++;
9823c4d7559SDave Watson 			copied += try_to_copy;
983d3b18ad3SJohn Fastabend 
984d3b18ad3SJohn Fastabend 			sk_msg_sg_copy_set(msg_pl, first);
985d3b18ad3SJohn Fastabend 			ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
986d3b18ad3SJohn Fastabend 						  record_type, &copied,
987d3b18ad3SJohn Fastabend 						  msg->msg_flags);
988a42055e8SVakul Garg 			if (ret) {
989a42055e8SVakul Garg 				if (ret == -EINPROGRESS)
990a42055e8SVakul Garg 					num_async++;
991d3b18ad3SJohn Fastabend 				else if (ret == -ENOMEM)
992d3b18ad3SJohn Fastabend 					goto wait_for_memory;
993d3b18ad3SJohn Fastabend 				else if (ret == -ENOSPC)
994d3b18ad3SJohn Fastabend 					goto rollback_iter;
995a42055e8SVakul Garg 				else if (ret != -EAGAIN)
9963c4d7559SDave Watson 					goto send_end;
997a42055e8SVakul Garg 			}
9985a3611efSDoron Roberts-Kedes 			continue;
999d3b18ad3SJohn Fastabend rollback_iter:
1000d3b18ad3SJohn Fastabend 			copied -= try_to_copy;
1001d3b18ad3SJohn Fastabend 			sk_msg_sg_copy_clear(msg_pl, first);
1002d3b18ad3SJohn Fastabend 			iov_iter_revert(&msg->msg_iter,
1003d3b18ad3SJohn Fastabend 					msg_pl->sg.size - orig_size);
10043c4d7559SDave Watson fallback_to_reg_send:
1005d829e9c4SDaniel Borkmann 			sk_msg_trim(sk, msg_pl, orig_size);
10063c4d7559SDave Watson 		}
10073c4d7559SDave Watson 
1008d829e9c4SDaniel Borkmann 		required_size = msg_pl->sg.size + try_to_copy;
10094e6d4720SVakul Garg 
1010d829e9c4SDaniel Borkmann 		ret = tls_clone_plaintext_msg(sk, required_size);
10113c4d7559SDave Watson 		if (ret) {
10123c4d7559SDave Watson 			if (ret != -ENOSPC)
10134e6d4720SVakul Garg 				goto send_end;
10143c4d7559SDave Watson 
10153c4d7559SDave Watson 			/* Adjust try_to_copy according to the amount that was
10163c4d7559SDave Watson 			 * actually allocated. The difference is due
10173c4d7559SDave Watson 			 * to max sg elements limit
10183c4d7559SDave Watson 			 */
1019d829e9c4SDaniel Borkmann 			try_to_copy -= required_size - msg_pl->sg.size;
10203c4d7559SDave Watson 			full_record = true;
10214509de14SVakul Garg 			sk_msg_trim(sk, msg_en,
10224509de14SVakul Garg 				    msg_pl->sg.size + prot->overhead_size);
10233c4d7559SDave Watson 		}
10243c4d7559SDave Watson 
102565a10e28SVakul Garg 		if (try_to_copy) {
102665a10e28SVakul Garg 			ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
102765a10e28SVakul Garg 						       msg_pl, try_to_copy);
1028d829e9c4SDaniel Borkmann 			if (ret < 0)
10293c4d7559SDave Watson 				goto trim_sgl;
103065a10e28SVakul Garg 		}
10313c4d7559SDave Watson 
1032d829e9c4SDaniel Borkmann 		/* Open records defined only if successfully copied, otherwise
1033d829e9c4SDaniel Borkmann 		 * we would trim the sg but not reset the open record frags.
1034d829e9c4SDaniel Borkmann 		 */
1035d829e9c4SDaniel Borkmann 		tls_ctx->pending_open_record_frags = true;
10363c4d7559SDave Watson 		copied += try_to_copy;
10373c4d7559SDave Watson 		if (full_record || eor) {
1038d3b18ad3SJohn Fastabend 			ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1039d3b18ad3SJohn Fastabend 						  record_type, &copied,
1040d3b18ad3SJohn Fastabend 						  msg->msg_flags);
10413c4d7559SDave Watson 			if (ret) {
1042a42055e8SVakul Garg 				if (ret == -EINPROGRESS)
1043a42055e8SVakul Garg 					num_async++;
1044d3b18ad3SJohn Fastabend 				else if (ret == -ENOMEM)
1045d3b18ad3SJohn Fastabend 					goto wait_for_memory;
1046d3b18ad3SJohn Fastabend 				else if (ret != -EAGAIN) {
1047d3b18ad3SJohn Fastabend 					if (ret == -ENOSPC)
1048d3b18ad3SJohn Fastabend 						ret = 0;
10493c4d7559SDave Watson 					goto send_end;
10503c4d7559SDave Watson 				}
10513c4d7559SDave Watson 			}
1052d3b18ad3SJohn Fastabend 		}
10533c4d7559SDave Watson 
10543c4d7559SDave Watson 		continue;
10553c4d7559SDave Watson 
10563c4d7559SDave Watson wait_for_sndbuf:
10573c4d7559SDave Watson 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
10583c4d7559SDave Watson wait_for_memory:
10593c4d7559SDave Watson 		ret = sk_stream_wait_memory(sk, &timeo);
10603c4d7559SDave Watson 		if (ret) {
10613c4d7559SDave Watson trim_sgl:
1062d829e9c4SDaniel Borkmann 			tls_trim_both_msgs(sk, orig_size);
10633c4d7559SDave Watson 			goto send_end;
10643c4d7559SDave Watson 		}
10653c4d7559SDave Watson 
1066d829e9c4SDaniel Borkmann 		if (msg_en->sg.size < required_size)
10673c4d7559SDave Watson 			goto alloc_encrypted;
10683c4d7559SDave Watson 	}
10693c4d7559SDave Watson 
1070a42055e8SVakul Garg 	if (!num_async) {
1071a42055e8SVakul Garg 		goto send_end;
1072a42055e8SVakul Garg 	} else if (num_zc) {
1073a42055e8SVakul Garg 		/* Wait for pending encryptions to get completed */
1074a42055e8SVakul Garg 		smp_store_mb(ctx->async_notify, true);
1075a42055e8SVakul Garg 
1076a42055e8SVakul Garg 		if (atomic_read(&ctx->encrypt_pending))
1077a42055e8SVakul Garg 			crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1078a42055e8SVakul Garg 		else
1079a42055e8SVakul Garg 			reinit_completion(&ctx->async_wait.completion);
1080a42055e8SVakul Garg 
1081a42055e8SVakul Garg 		WRITE_ONCE(ctx->async_notify, false);
1082a42055e8SVakul Garg 
1083a42055e8SVakul Garg 		if (ctx->async_wait.err) {
1084a42055e8SVakul Garg 			ret = ctx->async_wait.err;
1085a42055e8SVakul Garg 			copied = 0;
1086a42055e8SVakul Garg 		}
1087a42055e8SVakul Garg 	}
1088a42055e8SVakul Garg 
1089a42055e8SVakul Garg 	/* Transmit if any encryptions have completed */
1090a42055e8SVakul Garg 	if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1091a42055e8SVakul Garg 		cancel_delayed_work(&ctx->tx_work.work);
1092a42055e8SVakul Garg 		tls_tx_records(sk, msg->msg_flags);
1093a42055e8SVakul Garg 	}
1094a42055e8SVakul Garg 
10953c4d7559SDave Watson send_end:
10963c4d7559SDave Watson 	ret = sk_stream_error(sk, msg->msg_flags, ret);
10973c4d7559SDave Watson 
10983c4d7559SDave Watson 	release_sock(sk);
10993c4d7559SDave Watson 	return copied ? copied : ret;
11003c4d7559SDave Watson }
11013c4d7559SDave Watson 
110201cb8a1aSYueHaibing static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
11033c4d7559SDave Watson 			      int offset, size_t size, int flags)
11043c4d7559SDave Watson {
1105a42055e8SVakul Garg 	long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
11063c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1107f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
11084509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
11093c4d7559SDave Watson 	unsigned char record_type = TLS_RECORD_TYPE_DATA;
1110d829e9c4SDaniel Borkmann 	struct sk_msg *msg_pl;
1111a42055e8SVakul Garg 	struct tls_rec *rec;
1112a42055e8SVakul Garg 	int num_async = 0;
1113d3b18ad3SJohn Fastabend 	size_t copied = 0;
11143c4d7559SDave Watson 	bool full_record;
11153c4d7559SDave Watson 	int record_room;
11164128c0cfSVakul Garg 	int ret = 0;
1117a42055e8SVakul Garg 	bool eor;
11183c4d7559SDave Watson 
11193c4d7559SDave Watson 	eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
11203c4d7559SDave Watson 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
11213c4d7559SDave Watson 
1122a42055e8SVakul Garg 	/* Wait till there is any pending write on socket */
1123a42055e8SVakul Garg 	if (unlikely(sk->sk_write_pending)) {
1124a42055e8SVakul Garg 		ret = wait_on_pending_writer(sk, &timeo);
1125a42055e8SVakul Garg 		if (unlikely(ret))
11263c4d7559SDave Watson 			goto sendpage_end;
1127a42055e8SVakul Garg 	}
11283c4d7559SDave Watson 
11293c4d7559SDave Watson 	/* Call the sk_stream functions to manage the sndbuf mem. */
11303c4d7559SDave Watson 	while (size > 0) {
11313c4d7559SDave Watson 		size_t copy, required_size;
11323c4d7559SDave Watson 
11333c4d7559SDave Watson 		if (sk->sk_err) {
113430be8f8dSr.hering@avm.de 			ret = -sk->sk_err;
11353c4d7559SDave Watson 			goto sendpage_end;
11363c4d7559SDave Watson 		}
11373c4d7559SDave Watson 
1138d3b18ad3SJohn Fastabend 		if (ctx->open_rec)
1139d3b18ad3SJohn Fastabend 			rec = ctx->open_rec;
1140d3b18ad3SJohn Fastabend 		else
1141d3b18ad3SJohn Fastabend 			rec = ctx->open_rec = tls_get_rec(sk);
1142a42055e8SVakul Garg 		if (!rec) {
1143a42055e8SVakul Garg 			ret = -ENOMEM;
1144a42055e8SVakul Garg 			goto sendpage_end;
1145a42055e8SVakul Garg 		}
1146a42055e8SVakul Garg 
1147d829e9c4SDaniel Borkmann 		msg_pl = &rec->msg_plaintext;
1148d829e9c4SDaniel Borkmann 
11493c4d7559SDave Watson 		full_record = false;
1150d829e9c4SDaniel Borkmann 		record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
11513c4d7559SDave Watson 		copy = size;
11523c4d7559SDave Watson 		if (copy >= record_room) {
11533c4d7559SDave Watson 			copy = record_room;
11543c4d7559SDave Watson 			full_record = true;
11553c4d7559SDave Watson 		}
1156d829e9c4SDaniel Borkmann 
11574509de14SVakul Garg 		required_size = msg_pl->sg.size + copy + prot->overhead_size;
11583c4d7559SDave Watson 
11593c4d7559SDave Watson 		if (!sk_stream_memory_free(sk))
11603c4d7559SDave Watson 			goto wait_for_sndbuf;
11613c4d7559SDave Watson alloc_payload:
1162d829e9c4SDaniel Borkmann 		ret = tls_alloc_encrypted_msg(sk, required_size);
11633c4d7559SDave Watson 		if (ret) {
11643c4d7559SDave Watson 			if (ret != -ENOSPC)
11653c4d7559SDave Watson 				goto wait_for_memory;
11663c4d7559SDave Watson 
11673c4d7559SDave Watson 			/* Adjust copy according to the amount that was
11683c4d7559SDave Watson 			 * actually allocated. The difference is due
11693c4d7559SDave Watson 			 * to max sg elements limit
11703c4d7559SDave Watson 			 */
1171d829e9c4SDaniel Borkmann 			copy -= required_size - msg_pl->sg.size;
11723c4d7559SDave Watson 			full_record = true;
11733c4d7559SDave Watson 		}
11743c4d7559SDave Watson 
1175d829e9c4SDaniel Borkmann 		sk_msg_page_add(msg_pl, page, copy, offset);
11763c4d7559SDave Watson 		sk_mem_charge(sk, copy);
1177d829e9c4SDaniel Borkmann 
11783c4d7559SDave Watson 		offset += copy;
11793c4d7559SDave Watson 		size -= copy;
1180d3b18ad3SJohn Fastabend 		copied += copy;
11813c4d7559SDave Watson 
1182d829e9c4SDaniel Borkmann 		tls_ctx->pending_open_record_frags = true;
1183d829e9c4SDaniel Borkmann 		if (full_record || eor || sk_msg_full(msg_pl)) {
11844e6d4720SVakul Garg 			rec->inplace_crypto = 0;
1185d3b18ad3SJohn Fastabend 			ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1186d3b18ad3SJohn Fastabend 						  record_type, &copied, flags);
11873c4d7559SDave Watson 			if (ret) {
1188a42055e8SVakul Garg 				if (ret == -EINPROGRESS)
1189a42055e8SVakul Garg 					num_async++;
1190d3b18ad3SJohn Fastabend 				else if (ret == -ENOMEM)
1191d3b18ad3SJohn Fastabend 					goto wait_for_memory;
1192d3b18ad3SJohn Fastabend 				else if (ret != -EAGAIN) {
1193d3b18ad3SJohn Fastabend 					if (ret == -ENOSPC)
1194d3b18ad3SJohn Fastabend 						ret = 0;
11953c4d7559SDave Watson 					goto sendpage_end;
11963c4d7559SDave Watson 				}
11973c4d7559SDave Watson 			}
1198d3b18ad3SJohn Fastabend 		}
11993c4d7559SDave Watson 		continue;
12003c4d7559SDave Watson wait_for_sndbuf:
12013c4d7559SDave Watson 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
12023c4d7559SDave Watson wait_for_memory:
12033c4d7559SDave Watson 		ret = sk_stream_wait_memory(sk, &timeo);
12043c4d7559SDave Watson 		if (ret) {
1205d829e9c4SDaniel Borkmann 			tls_trim_both_msgs(sk, msg_pl->sg.size);
12063c4d7559SDave Watson 			goto sendpage_end;
12073c4d7559SDave Watson 		}
12083c4d7559SDave Watson 
12093c4d7559SDave Watson 		goto alloc_payload;
12103c4d7559SDave Watson 	}
12113c4d7559SDave Watson 
1212a42055e8SVakul Garg 	if (num_async) {
1213a42055e8SVakul Garg 		/* Transmit if any encryptions have completed */
1214a42055e8SVakul Garg 		if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1215a42055e8SVakul Garg 			cancel_delayed_work(&ctx->tx_work.work);
1216a42055e8SVakul Garg 			tls_tx_records(sk, flags);
1217a42055e8SVakul Garg 		}
1218a42055e8SVakul Garg 	}
12193c4d7559SDave Watson sendpage_end:
12203c4d7559SDave Watson 	ret = sk_stream_error(sk, flags, ret);
1221d3b18ad3SJohn Fastabend 	return copied ? copied : ret;
12223c4d7559SDave Watson }
12233c4d7559SDave Watson 
12240608c69cSJohn Fastabend int tls_sw_sendpage(struct sock *sk, struct page *page,
12250608c69cSJohn Fastabend 		    int offset, size_t size, int flags)
12260608c69cSJohn Fastabend {
12270608c69cSJohn Fastabend 	int ret;
12280608c69cSJohn Fastabend 
12290608c69cSJohn Fastabend 	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
12300608c69cSJohn Fastabend 		      MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
12310608c69cSJohn Fastabend 		return -ENOTSUPP;
12320608c69cSJohn Fastabend 
12330608c69cSJohn Fastabend 	lock_sock(sk);
12340608c69cSJohn Fastabend 	ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
12350608c69cSJohn Fastabend 	release_sock(sk);
12360608c69cSJohn Fastabend 	return ret;
12370608c69cSJohn Fastabend }
12380608c69cSJohn Fastabend 
1239d3b18ad3SJohn Fastabend static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock,
1240d3b18ad3SJohn Fastabend 				     int flags, long timeo, int *err)
1241c46234ebSDave Watson {
1242c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1243f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1244c46234ebSDave Watson 	struct sk_buff *skb;
1245c46234ebSDave Watson 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
1246c46234ebSDave Watson 
1247d3b18ad3SJohn Fastabend 	while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) {
1248c46234ebSDave Watson 		if (sk->sk_err) {
1249c46234ebSDave Watson 			*err = sock_error(sk);
1250c46234ebSDave Watson 			return NULL;
1251c46234ebSDave Watson 		}
1252c46234ebSDave Watson 
1253fcf4793eSDoron Roberts-Kedes 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1254fcf4793eSDoron Roberts-Kedes 			return NULL;
1255fcf4793eSDoron Roberts-Kedes 
1256c46234ebSDave Watson 		if (sock_flag(sk, SOCK_DONE))
1257c46234ebSDave Watson 			return NULL;
1258c46234ebSDave Watson 
1259c46234ebSDave Watson 		if ((flags & MSG_DONTWAIT) || !timeo) {
1260c46234ebSDave Watson 			*err = -EAGAIN;
1261c46234ebSDave Watson 			return NULL;
1262c46234ebSDave Watson 		}
1263c46234ebSDave Watson 
1264c46234ebSDave Watson 		add_wait_queue(sk_sleep(sk), &wait);
1265c46234ebSDave Watson 		sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1266d3b18ad3SJohn Fastabend 		sk_wait_event(sk, &timeo,
1267d3b18ad3SJohn Fastabend 			      ctx->recv_pkt != skb ||
1268d3b18ad3SJohn Fastabend 			      !sk_psock_queue_empty(psock),
1269d3b18ad3SJohn Fastabend 			      &wait);
1270c46234ebSDave Watson 		sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1271c46234ebSDave Watson 		remove_wait_queue(sk_sleep(sk), &wait);
1272c46234ebSDave Watson 
1273c46234ebSDave Watson 		/* Handle signals */
1274c46234ebSDave Watson 		if (signal_pending(current)) {
1275c46234ebSDave Watson 			*err = sock_intr_errno(timeo);
1276c46234ebSDave Watson 			return NULL;
1277c46234ebSDave Watson 		}
1278c46234ebSDave Watson 	}
1279c46234ebSDave Watson 
1280c46234ebSDave Watson 	return skb;
1281c46234ebSDave Watson }
1282c46234ebSDave Watson 
1283d829e9c4SDaniel Borkmann static int tls_setup_from_iter(struct sock *sk, struct iov_iter *from,
1284d829e9c4SDaniel Borkmann 			       int length, int *pages_used,
1285d829e9c4SDaniel Borkmann 			       unsigned int *size_used,
1286d829e9c4SDaniel Borkmann 			       struct scatterlist *to,
1287d829e9c4SDaniel Borkmann 			       int to_max_pages)
1288d829e9c4SDaniel Borkmann {
1289d829e9c4SDaniel Borkmann 	int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1290d829e9c4SDaniel Borkmann 	struct page *pages[MAX_SKB_FRAGS];
1291d829e9c4SDaniel Borkmann 	unsigned int size = *size_used;
1292d829e9c4SDaniel Borkmann 	ssize_t copied, use;
1293d829e9c4SDaniel Borkmann 	size_t offset;
1294d829e9c4SDaniel Borkmann 
1295d829e9c4SDaniel Borkmann 	while (length > 0) {
1296d829e9c4SDaniel Borkmann 		i = 0;
1297d829e9c4SDaniel Borkmann 		maxpages = to_max_pages - num_elem;
1298d829e9c4SDaniel Borkmann 		if (maxpages == 0) {
1299d829e9c4SDaniel Borkmann 			rc = -EFAULT;
1300d829e9c4SDaniel Borkmann 			goto out;
1301d829e9c4SDaniel Borkmann 		}
1302d829e9c4SDaniel Borkmann 		copied = iov_iter_get_pages(from, pages,
1303d829e9c4SDaniel Borkmann 					    length,
1304d829e9c4SDaniel Borkmann 					    maxpages, &offset);
1305d829e9c4SDaniel Borkmann 		if (copied <= 0) {
1306d829e9c4SDaniel Borkmann 			rc = -EFAULT;
1307d829e9c4SDaniel Borkmann 			goto out;
1308d829e9c4SDaniel Borkmann 		}
1309d829e9c4SDaniel Borkmann 
1310d829e9c4SDaniel Borkmann 		iov_iter_advance(from, copied);
1311d829e9c4SDaniel Borkmann 
1312d829e9c4SDaniel Borkmann 		length -= copied;
1313d829e9c4SDaniel Borkmann 		size += copied;
1314d829e9c4SDaniel Borkmann 		while (copied) {
1315d829e9c4SDaniel Borkmann 			use = min_t(int, copied, PAGE_SIZE - offset);
1316d829e9c4SDaniel Borkmann 
1317d829e9c4SDaniel Borkmann 			sg_set_page(&to[num_elem],
1318d829e9c4SDaniel Borkmann 				    pages[i], use, offset);
1319d829e9c4SDaniel Borkmann 			sg_unmark_end(&to[num_elem]);
1320d829e9c4SDaniel Borkmann 			/* We do not uncharge memory from this API */
1321d829e9c4SDaniel Borkmann 
1322d829e9c4SDaniel Borkmann 			offset = 0;
1323d829e9c4SDaniel Borkmann 			copied -= use;
1324d829e9c4SDaniel Borkmann 
1325d829e9c4SDaniel Borkmann 			i++;
1326d829e9c4SDaniel Borkmann 			num_elem++;
1327d829e9c4SDaniel Borkmann 		}
1328d829e9c4SDaniel Borkmann 	}
1329d829e9c4SDaniel Borkmann 	/* Mark the end in the last sg entry if newly added */
1330d829e9c4SDaniel Borkmann 	if (num_elem > *pages_used)
1331d829e9c4SDaniel Borkmann 		sg_mark_end(&to[num_elem - 1]);
1332d829e9c4SDaniel Borkmann out:
1333d829e9c4SDaniel Borkmann 	if (rc)
1334d829e9c4SDaniel Borkmann 		iov_iter_revert(from, size - *size_used);
1335d829e9c4SDaniel Borkmann 	*size_used = size;
1336d829e9c4SDaniel Borkmann 	*pages_used = num_elem;
1337d829e9c4SDaniel Borkmann 
1338d829e9c4SDaniel Borkmann 	return rc;
1339d829e9c4SDaniel Borkmann }
1340d829e9c4SDaniel Borkmann 
13410b243d00SVakul Garg /* This function decrypts the input skb into either out_iov or in out_sg
13420b243d00SVakul Garg  * or in skb buffers itself. The input parameter 'zc' indicates if
13430b243d00SVakul Garg  * zero-copy mode needs to be tried or not. With zero-copy mode, either
13440b243d00SVakul Garg  * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
13450b243d00SVakul Garg  * NULL, then the decryption happens inside skb buffers itself, i.e.
13460b243d00SVakul Garg  * zero-copy gets disabled and 'zc' is updated.
13470b243d00SVakul Garg  */
13480b243d00SVakul Garg 
13490b243d00SVakul Garg static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13500b243d00SVakul Garg 			    struct iov_iter *out_iov,
13510b243d00SVakul Garg 			    struct scatterlist *out_sg,
1352692d7b5dSVakul Garg 			    int *chunk, bool *zc, bool async)
13530b243d00SVakul Garg {
13540b243d00SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
13550b243d00SVakul Garg 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
13564509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
13570b243d00SVakul Garg 	struct strp_msg *rxm = strp_msg(skb);
13580b243d00SVakul Garg 	int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
13590b243d00SVakul Garg 	struct aead_request *aead_req;
13600b243d00SVakul Garg 	struct sk_buff *unused;
13610b243d00SVakul Garg 	u8 *aad, *iv, *mem = NULL;
13620b243d00SVakul Garg 	struct scatterlist *sgin = NULL;
13630b243d00SVakul Garg 	struct scatterlist *sgout = NULL;
13644509de14SVakul Garg 	const int data_len = rxm->full_len - prot->overhead_size +
13654509de14SVakul Garg 			     prot->tail_size;
1366f295b3aeSVakul Garg 	int iv_offset = 0;
13670b243d00SVakul Garg 
13680b243d00SVakul Garg 	if (*zc && (out_iov || out_sg)) {
13690b243d00SVakul Garg 		if (out_iov)
13700b243d00SVakul Garg 			n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1;
13710b243d00SVakul Garg 		else
13720b243d00SVakul Garg 			n_sgout = sg_nents(out_sg);
13734509de14SVakul Garg 		n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
13744509de14SVakul Garg 				 rxm->full_len - prot->prepend_size);
13750b243d00SVakul Garg 	} else {
13760b243d00SVakul Garg 		n_sgout = 0;
13770b243d00SVakul Garg 		*zc = false;
13780927f71dSDoron Roberts-Kedes 		n_sgin = skb_cow_data(skb, 0, &unused);
13790b243d00SVakul Garg 	}
13800b243d00SVakul Garg 
13810b243d00SVakul Garg 	if (n_sgin < 1)
13820b243d00SVakul Garg 		return -EBADMSG;
13830b243d00SVakul Garg 
13840b243d00SVakul Garg 	/* Increment to accommodate AAD */
13850b243d00SVakul Garg 	n_sgin = n_sgin + 1;
13860b243d00SVakul Garg 
13870b243d00SVakul Garg 	nsg = n_sgin + n_sgout;
13880b243d00SVakul Garg 
13890b243d00SVakul Garg 	aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
13900b243d00SVakul Garg 	mem_size = aead_size + (nsg * sizeof(struct scatterlist));
13914509de14SVakul Garg 	mem_size = mem_size + prot->aad_size;
13920b243d00SVakul Garg 	mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
13930b243d00SVakul Garg 
13940b243d00SVakul Garg 	/* Allocate a single block of memory which contains
13950b243d00SVakul Garg 	 * aead_req || sgin[] || sgout[] || aad || iv.
13960b243d00SVakul Garg 	 * This order achieves correct alignment for aead_req, sgin, sgout.
13970b243d00SVakul Garg 	 */
13980b243d00SVakul Garg 	mem = kmalloc(mem_size, sk->sk_allocation);
13990b243d00SVakul Garg 	if (!mem)
14000b243d00SVakul Garg 		return -ENOMEM;
14010b243d00SVakul Garg 
14020b243d00SVakul Garg 	/* Segment the allocated memory */
14030b243d00SVakul Garg 	aead_req = (struct aead_request *)mem;
14040b243d00SVakul Garg 	sgin = (struct scatterlist *)(mem + aead_size);
14050b243d00SVakul Garg 	sgout = sgin + n_sgin;
14060b243d00SVakul Garg 	aad = (u8 *)(sgout + n_sgout);
14074509de14SVakul Garg 	iv = aad + prot->aad_size;
14080b243d00SVakul Garg 
1409f295b3aeSVakul Garg 	/* For CCM based ciphers, first byte of nonce+iv is always '2' */
1410f295b3aeSVakul Garg 	if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) {
1411f295b3aeSVakul Garg 		iv[0] = 2;
1412f295b3aeSVakul Garg 		iv_offset = 1;
1413f295b3aeSVakul Garg 	}
1414f295b3aeSVakul Garg 
14150b243d00SVakul Garg 	/* Prepare IV */
14160b243d00SVakul Garg 	err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1417f295b3aeSVakul Garg 			    iv + iv_offset + prot->salt_size,
14184509de14SVakul Garg 			    prot->iv_size);
14190b243d00SVakul Garg 	if (err < 0) {
14200b243d00SVakul Garg 		kfree(mem);
14210b243d00SVakul Garg 		return err;
14220b243d00SVakul Garg 	}
14234509de14SVakul Garg 	if (prot->version == TLS_1_3_VERSION)
1424f295b3aeSVakul Garg 		memcpy(iv + iv_offset, tls_ctx->rx.iv,
1425f295b3aeSVakul Garg 		       crypto_aead_ivsize(ctx->aead_recv));
1426130b392cSDave Watson 	else
1427f295b3aeSVakul Garg 		memcpy(iv + iv_offset, tls_ctx->rx.iv, prot->salt_size);
14280b243d00SVakul Garg 
14294509de14SVakul Garg 	xor_iv_with_seq(prot->version, iv, tls_ctx->rx.rec_seq);
1430130b392cSDave Watson 
14310b243d00SVakul Garg 	/* Prepare AAD */
14324509de14SVakul Garg 	tls_make_aad(aad, rxm->full_len - prot->overhead_size +
14334509de14SVakul Garg 		     prot->tail_size,
14344509de14SVakul Garg 		     tls_ctx->rx.rec_seq, prot->rec_seq_size,
14354509de14SVakul Garg 		     ctx->control, prot->version);
14360b243d00SVakul Garg 
14370b243d00SVakul Garg 	/* Prepare sgin */
14380b243d00SVakul Garg 	sg_init_table(sgin, n_sgin);
14394509de14SVakul Garg 	sg_set_buf(&sgin[0], aad, prot->aad_size);
14400b243d00SVakul Garg 	err = skb_to_sgvec(skb, &sgin[1],
14414509de14SVakul Garg 			   rxm->offset + prot->prepend_size,
14424509de14SVakul Garg 			   rxm->full_len - prot->prepend_size);
14430b243d00SVakul Garg 	if (err < 0) {
14440b243d00SVakul Garg 		kfree(mem);
14450b243d00SVakul Garg 		return err;
14460b243d00SVakul Garg 	}
14470b243d00SVakul Garg 
14480b243d00SVakul Garg 	if (n_sgout) {
14490b243d00SVakul Garg 		if (out_iov) {
14500b243d00SVakul Garg 			sg_init_table(sgout, n_sgout);
14514509de14SVakul Garg 			sg_set_buf(&sgout[0], aad, prot->aad_size);
14520b243d00SVakul Garg 
14530b243d00SVakul Garg 			*chunk = 0;
1454d829e9c4SDaniel Borkmann 			err = tls_setup_from_iter(sk, out_iov, data_len,
1455d829e9c4SDaniel Borkmann 						  &pages, chunk, &sgout[1],
1456d829e9c4SDaniel Borkmann 						  (n_sgout - 1));
14570b243d00SVakul Garg 			if (err < 0)
14580b243d00SVakul Garg 				goto fallback_to_reg_recv;
14590b243d00SVakul Garg 		} else if (out_sg) {
14600b243d00SVakul Garg 			memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
14610b243d00SVakul Garg 		} else {
14620b243d00SVakul Garg 			goto fallback_to_reg_recv;
14630b243d00SVakul Garg 		}
14640b243d00SVakul Garg 	} else {
14650b243d00SVakul Garg fallback_to_reg_recv:
14660b243d00SVakul Garg 		sgout = sgin;
14670b243d00SVakul Garg 		pages = 0;
1468692d7b5dSVakul Garg 		*chunk = data_len;
14690b243d00SVakul Garg 		*zc = false;
14700b243d00SVakul Garg 	}
14710b243d00SVakul Garg 
14720b243d00SVakul Garg 	/* Prepare and submit AEAD request */
147394524d8fSVakul Garg 	err = tls_do_decryption(sk, skb, sgin, sgout, iv,
1474692d7b5dSVakul Garg 				data_len, aead_req, async);
147594524d8fSVakul Garg 	if (err == -EINPROGRESS)
147694524d8fSVakul Garg 		return err;
14770b243d00SVakul Garg 
14780b243d00SVakul Garg 	/* Release the pages in case iov was mapped to pages */
14790b243d00SVakul Garg 	for (; pages > 0; pages--)
14800b243d00SVakul Garg 		put_page(sg_page(&sgout[pages]));
14810b243d00SVakul Garg 
14820b243d00SVakul Garg 	kfree(mem);
14830b243d00SVakul Garg 	return err;
14840b243d00SVakul Garg }
14850b243d00SVakul Garg 
1486dafb67f3SBoris Pismenny static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
1487692d7b5dSVakul Garg 			      struct iov_iter *dest, int *chunk, bool *zc,
1488692d7b5dSVakul Garg 			      bool async)
1489dafb67f3SBoris Pismenny {
1490dafb67f3SBoris Pismenny 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1491dafb67f3SBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
14924509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
1493dafb67f3SBoris Pismenny 	struct strp_msg *rxm = strp_msg(skb);
1494b53f4976SJakub Kicinski 	int pad, err = 0;
1495dafb67f3SBoris Pismenny 
1496d069b780SBoris Pismenny 	if (!ctx->decrypted) {
1497b9d8fec9SJakub Kicinski 		if (tls_ctx->rx_conf == TLS_HW) {
14984de30a8dSJakub Kicinski 			err = tls_device_decrypted(sk, tls_ctx, skb, rxm);
14994799ac81SBoris Pismenny 			if (err < 0)
15004799ac81SBoris Pismenny 				return err;
1501b9d8fec9SJakub Kicinski 		}
1502be2fbc15SJakub Kicinski 
1503d069b780SBoris Pismenny 		/* Still not decrypted after tls_device */
15044799ac81SBoris Pismenny 		if (!ctx->decrypted) {
1505d069b780SBoris Pismenny 			err = decrypt_internal(sk, skb, dest, NULL, chunk, zc,
1506d069b780SBoris Pismenny 					       async);
150794524d8fSVakul Garg 			if (err < 0) {
150894524d8fSVakul Garg 				if (err == -EINPROGRESS)
1509fb0f886fSJakub Kicinski 					tls_advance_record_sn(sk, prot,
1510fb0f886fSJakub Kicinski 							      &tls_ctx->rx);
151194524d8fSVakul Garg 
1512dafb67f3SBoris Pismenny 				return err;
151394524d8fSVakul Garg 			}
1514c43ac97bSJakub Kicinski 		} else {
1515c43ac97bSJakub Kicinski 			*zc = false;
1516d069b780SBoris Pismenny 		}
1517130b392cSDave Watson 
1518b53f4976SJakub Kicinski 		pad = padding_length(ctx, prot, skb);
1519b53f4976SJakub Kicinski 		if (pad < 0)
1520b53f4976SJakub Kicinski 			return pad;
1521b53f4976SJakub Kicinski 
1522b53f4976SJakub Kicinski 		rxm->full_len -= pad;
15234509de14SVakul Garg 		rxm->offset += prot->prepend_size;
15244509de14SVakul Garg 		rxm->full_len -= prot->overhead_size;
1525fb0f886fSJakub Kicinski 		tls_advance_record_sn(sk, prot, &tls_ctx->rx);
1526dafb67f3SBoris Pismenny 		ctx->decrypted = true;
1527dafb67f3SBoris Pismenny 		ctx->saved_data_ready(sk);
1528fedf201eSDave Watson 	} else {
1529fedf201eSDave Watson 		*zc = false;
1530fedf201eSDave Watson 	}
1531dafb67f3SBoris Pismenny 
1532dafb67f3SBoris Pismenny 	return err;
1533dafb67f3SBoris Pismenny }
1534dafb67f3SBoris Pismenny 
1535dafb67f3SBoris Pismenny int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1536c46234ebSDave Watson 		struct scatterlist *sgout)
1537c46234ebSDave Watson {
15380b243d00SVakul Garg 	bool zc = true;
15390b243d00SVakul Garg 	int chunk;
1540c46234ebSDave Watson 
1541692d7b5dSVakul Garg 	return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc, false);
1542c46234ebSDave Watson }
1543c46234ebSDave Watson 
1544c46234ebSDave Watson static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
1545c46234ebSDave Watson 			       unsigned int len)
1546c46234ebSDave Watson {
1547c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1548f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
154994524d8fSVakul Garg 
155094524d8fSVakul Garg 	if (skb) {
1551c46234ebSDave Watson 		struct strp_msg *rxm = strp_msg(skb);
1552c46234ebSDave Watson 
1553c46234ebSDave Watson 		if (len < rxm->full_len) {
1554c46234ebSDave Watson 			rxm->offset += len;
1555c46234ebSDave Watson 			rxm->full_len -= len;
1556c46234ebSDave Watson 			return false;
1557c46234ebSDave Watson 		}
1558a88c26f6SVakul Garg 		consume_skb(skb);
155994524d8fSVakul Garg 	}
1560c46234ebSDave Watson 
1561c46234ebSDave Watson 	/* Finished with message */
1562c46234ebSDave Watson 	ctx->recv_pkt = NULL;
15637170e604SDoron Roberts-Kedes 	__strp_unpause(&ctx->strp);
1564c46234ebSDave Watson 
1565c46234ebSDave Watson 	return true;
1566c46234ebSDave Watson }
1567c46234ebSDave Watson 
1568692d7b5dSVakul Garg /* This function traverses the rx_list in tls receive context to copies the
15692b794c40SVakul Garg  * decrypted records into the buffer provided by caller zero copy is not
1570692d7b5dSVakul Garg  * true. Further, the records are removed from the rx_list if it is not a peek
1571692d7b5dSVakul Garg  * case and the record has been consumed completely.
1572692d7b5dSVakul Garg  */
1573692d7b5dSVakul Garg static int process_rx_list(struct tls_sw_context_rx *ctx,
1574692d7b5dSVakul Garg 			   struct msghdr *msg,
15752b794c40SVakul Garg 			   u8 *control,
15762b794c40SVakul Garg 			   bool *cmsg,
1577692d7b5dSVakul Garg 			   size_t skip,
1578692d7b5dSVakul Garg 			   size_t len,
1579692d7b5dSVakul Garg 			   bool zc,
1580692d7b5dSVakul Garg 			   bool is_peek)
1581692d7b5dSVakul Garg {
1582692d7b5dSVakul Garg 	struct sk_buff *skb = skb_peek(&ctx->rx_list);
15832b794c40SVakul Garg 	u8 ctrl = *control;
15842b794c40SVakul Garg 	u8 msgc = *cmsg;
15852b794c40SVakul Garg 	struct tls_msg *tlm;
1586692d7b5dSVakul Garg 	ssize_t copied = 0;
1587692d7b5dSVakul Garg 
15882b794c40SVakul Garg 	/* Set the record type in 'control' if caller didn't pass it */
15892b794c40SVakul Garg 	if (!ctrl && skb) {
15902b794c40SVakul Garg 		tlm = tls_msg(skb);
15912b794c40SVakul Garg 		ctrl = tlm->control;
15922b794c40SVakul Garg 	}
15932b794c40SVakul Garg 
1594692d7b5dSVakul Garg 	while (skip && skb) {
1595692d7b5dSVakul Garg 		struct strp_msg *rxm = strp_msg(skb);
15962b794c40SVakul Garg 		tlm = tls_msg(skb);
15972b794c40SVakul Garg 
15982b794c40SVakul Garg 		/* Cannot process a record of different type */
15992b794c40SVakul Garg 		if (ctrl != tlm->control)
16002b794c40SVakul Garg 			return 0;
1601692d7b5dSVakul Garg 
1602692d7b5dSVakul Garg 		if (skip < rxm->full_len)
1603692d7b5dSVakul Garg 			break;
1604692d7b5dSVakul Garg 
1605692d7b5dSVakul Garg 		skip = skip - rxm->full_len;
1606692d7b5dSVakul Garg 		skb = skb_peek_next(skb, &ctx->rx_list);
1607692d7b5dSVakul Garg 	}
1608692d7b5dSVakul Garg 
1609692d7b5dSVakul Garg 	while (len && skb) {
1610692d7b5dSVakul Garg 		struct sk_buff *next_skb;
1611692d7b5dSVakul Garg 		struct strp_msg *rxm = strp_msg(skb);
1612692d7b5dSVakul Garg 		int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1613692d7b5dSVakul Garg 
16142b794c40SVakul Garg 		tlm = tls_msg(skb);
16152b794c40SVakul Garg 
16162b794c40SVakul Garg 		/* Cannot process a record of different type */
16172b794c40SVakul Garg 		if (ctrl != tlm->control)
16182b794c40SVakul Garg 			return 0;
16192b794c40SVakul Garg 
16202b794c40SVakul Garg 		/* Set record type if not already done. For a non-data record,
16212b794c40SVakul Garg 		 * do not proceed if record type could not be copied.
16222b794c40SVakul Garg 		 */
16232b794c40SVakul Garg 		if (!msgc) {
16242b794c40SVakul Garg 			int cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
16252b794c40SVakul Garg 					    sizeof(ctrl), &ctrl);
16262b794c40SVakul Garg 			msgc = true;
16272b794c40SVakul Garg 			if (ctrl != TLS_RECORD_TYPE_DATA) {
16282b794c40SVakul Garg 				if (cerr || msg->msg_flags & MSG_CTRUNC)
16292b794c40SVakul Garg 					return -EIO;
16302b794c40SVakul Garg 
16312b794c40SVakul Garg 				*cmsg = msgc;
16322b794c40SVakul Garg 			}
16332b794c40SVakul Garg 		}
16342b794c40SVakul Garg 
1635692d7b5dSVakul Garg 		if (!zc || (rxm->full_len - skip) > len) {
1636692d7b5dSVakul Garg 			int err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1637692d7b5dSVakul Garg 						    msg, chunk);
1638692d7b5dSVakul Garg 			if (err < 0)
1639692d7b5dSVakul Garg 				return err;
1640692d7b5dSVakul Garg 		}
1641692d7b5dSVakul Garg 
1642692d7b5dSVakul Garg 		len = len - chunk;
1643692d7b5dSVakul Garg 		copied = copied + chunk;
1644692d7b5dSVakul Garg 
1645692d7b5dSVakul Garg 		/* Consume the data from record if it is non-peek case*/
1646692d7b5dSVakul Garg 		if (!is_peek) {
1647692d7b5dSVakul Garg 			rxm->offset = rxm->offset + chunk;
1648692d7b5dSVakul Garg 			rxm->full_len = rxm->full_len - chunk;
1649692d7b5dSVakul Garg 
1650692d7b5dSVakul Garg 			/* Return if there is unconsumed data in the record */
1651692d7b5dSVakul Garg 			if (rxm->full_len - skip)
1652692d7b5dSVakul Garg 				break;
1653692d7b5dSVakul Garg 		}
1654692d7b5dSVakul Garg 
1655692d7b5dSVakul Garg 		/* The remaining skip-bytes must lie in 1st record in rx_list.
1656692d7b5dSVakul Garg 		 * So from the 2nd record, 'skip' should be 0.
1657692d7b5dSVakul Garg 		 */
1658692d7b5dSVakul Garg 		skip = 0;
1659692d7b5dSVakul Garg 
1660692d7b5dSVakul Garg 		if (msg)
1661692d7b5dSVakul Garg 			msg->msg_flags |= MSG_EOR;
1662692d7b5dSVakul Garg 
1663692d7b5dSVakul Garg 		next_skb = skb_peek_next(skb, &ctx->rx_list);
1664692d7b5dSVakul Garg 
1665692d7b5dSVakul Garg 		if (!is_peek) {
1666692d7b5dSVakul Garg 			skb_unlink(skb, &ctx->rx_list);
1667a88c26f6SVakul Garg 			consume_skb(skb);
1668692d7b5dSVakul Garg 		}
1669692d7b5dSVakul Garg 
1670692d7b5dSVakul Garg 		skb = next_skb;
1671692d7b5dSVakul Garg 	}
1672692d7b5dSVakul Garg 
16732b794c40SVakul Garg 	*control = ctrl;
1674692d7b5dSVakul Garg 	return copied;
1675692d7b5dSVakul Garg }
1676692d7b5dSVakul Garg 
1677c46234ebSDave Watson int tls_sw_recvmsg(struct sock *sk,
1678c46234ebSDave Watson 		   struct msghdr *msg,
1679c46234ebSDave Watson 		   size_t len,
1680c46234ebSDave Watson 		   int nonblock,
1681c46234ebSDave Watson 		   int flags,
1682c46234ebSDave Watson 		   int *addr_len)
1683c46234ebSDave Watson {
1684c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1685f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
16864509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
1687d3b18ad3SJohn Fastabend 	struct sk_psock *psock;
1688692d7b5dSVakul Garg 	unsigned char control = 0;
1689692d7b5dSVakul Garg 	ssize_t decrypted = 0;
1690c46234ebSDave Watson 	struct strp_msg *rxm;
16912b794c40SVakul Garg 	struct tls_msg *tlm;
1692c46234ebSDave Watson 	struct sk_buff *skb;
1693c46234ebSDave Watson 	ssize_t copied = 0;
1694c46234ebSDave Watson 	bool cmsg = false;
169506030dbaSDaniel Borkmann 	int target, err = 0;
1696c46234ebSDave Watson 	long timeo;
169700e23707SDavid Howells 	bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
1698692d7b5dSVakul Garg 	bool is_peek = flags & MSG_PEEK;
169994524d8fSVakul Garg 	int num_async = 0;
1700c46234ebSDave Watson 
1701c46234ebSDave Watson 	flags |= nonblock;
1702c46234ebSDave Watson 
1703c46234ebSDave Watson 	if (unlikely(flags & MSG_ERRQUEUE))
1704c46234ebSDave Watson 		return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1705c46234ebSDave Watson 
1706d3b18ad3SJohn Fastabend 	psock = sk_psock_get(sk);
1707c46234ebSDave Watson 	lock_sock(sk);
1708c46234ebSDave Watson 
1709692d7b5dSVakul Garg 	/* Process pending decrypted records. It must be non-zero-copy */
17102b794c40SVakul Garg 	err = process_rx_list(ctx, msg, &control, &cmsg, 0, len, false,
17112b794c40SVakul Garg 			      is_peek);
1712692d7b5dSVakul Garg 	if (err < 0) {
1713692d7b5dSVakul Garg 		tls_err_abort(sk, err);
1714692d7b5dSVakul Garg 		goto end;
1715692d7b5dSVakul Garg 	} else {
1716692d7b5dSVakul Garg 		copied = err;
1717692d7b5dSVakul Garg 	}
1718692d7b5dSVakul Garg 
171946a16959SJakub Kicinski 	if (len <= copied)
1720692d7b5dSVakul Garg 		goto recv_end;
172146a16959SJakub Kicinski 
172246a16959SJakub Kicinski 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
172346a16959SJakub Kicinski 	len = len - copied;
172446a16959SJakub Kicinski 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1725692d7b5dSVakul Garg 
172604b25a54SJakub Kicinski 	while (len && (decrypted + copied < target || ctx->recv_pkt)) {
1727692d7b5dSVakul Garg 		bool retain_skb = false;
1728692d7b5dSVakul Garg 		bool zc = false;
1729692d7b5dSVakul Garg 		int to_decrypt;
1730c46234ebSDave Watson 		int chunk = 0;
17317754bd63SEran Ben Elisha 		bool async_capable;
17327754bd63SEran Ben Elisha 		bool async = false;
1733c46234ebSDave Watson 
1734d3b18ad3SJohn Fastabend 		skb = tls_wait_data(sk, psock, flags, timeo, &err);
1735d3b18ad3SJohn Fastabend 		if (!skb) {
1736d3b18ad3SJohn Fastabend 			if (psock) {
173702c558b2SJohn Fastabend 				int ret = __tcp_bpf_recvmsg(sk, psock,
173802c558b2SJohn Fastabend 							    msg, len, flags);
1739d3b18ad3SJohn Fastabend 
1740d3b18ad3SJohn Fastabend 				if (ret > 0) {
1741692d7b5dSVakul Garg 					decrypted += ret;
1742d3b18ad3SJohn Fastabend 					len -= ret;
1743d3b18ad3SJohn Fastabend 					continue;
1744d3b18ad3SJohn Fastabend 				}
1745d3b18ad3SJohn Fastabend 			}
1746c46234ebSDave Watson 			goto recv_end;
17472b794c40SVakul Garg 		} else {
17482b794c40SVakul Garg 			tlm = tls_msg(skb);
17492b794c40SVakul Garg 			if (prot->version == TLS_1_3_VERSION)
17502b794c40SVakul Garg 				tlm->control = 0;
17512b794c40SVakul Garg 			else
17522b794c40SVakul Garg 				tlm->control = ctx->control;
1753d3b18ad3SJohn Fastabend 		}
1754c46234ebSDave Watson 
1755c46234ebSDave Watson 		rxm = strp_msg(skb);
175694524d8fSVakul Garg 
17574509de14SVakul Garg 		to_decrypt = rxm->full_len - prot->overhead_size;
1758fedf201eSDave Watson 
1759fedf201eSDave Watson 		if (to_decrypt <= len && !is_kvec && !is_peek &&
1760130b392cSDave Watson 		    ctx->control == TLS_RECORD_TYPE_DATA &&
17614509de14SVakul Garg 		    prot->version != TLS_1_3_VERSION)
1762fedf201eSDave Watson 			zc = true;
1763fedf201eSDave Watson 
1764c0ab4732SVakul Garg 		/* Do not use async mode if record is non-data */
1765c0ab4732SVakul Garg 		if (ctx->control == TLS_RECORD_TYPE_DATA)
17667754bd63SEran Ben Elisha 			async_capable = ctx->async_capable;
1767c0ab4732SVakul Garg 		else
17687754bd63SEran Ben Elisha 			async_capable = false;
1769c0ab4732SVakul Garg 
1770fedf201eSDave Watson 		err = decrypt_skb_update(sk, skb, &msg->msg_iter,
17717754bd63SEran Ben Elisha 					 &chunk, &zc, async_capable);
1772fedf201eSDave Watson 		if (err < 0 && err != -EINPROGRESS) {
1773fedf201eSDave Watson 			tls_err_abort(sk, EBADMSG);
1774fedf201eSDave Watson 			goto recv_end;
1775fedf201eSDave Watson 		}
1776fedf201eSDave Watson 
17777754bd63SEran Ben Elisha 		if (err == -EINPROGRESS) {
17787754bd63SEran Ben Elisha 			async = true;
1779fedf201eSDave Watson 			num_async++;
17807754bd63SEran Ben Elisha 		} else if (prot->version == TLS_1_3_VERSION) {
17812b794c40SVakul Garg 			tlm->control = ctx->control;
17827754bd63SEran Ben Elisha 		}
17832b794c40SVakul Garg 
17842b794c40SVakul Garg 		/* If the type of records being processed is not known yet,
17852b794c40SVakul Garg 		 * set it to record type just dequeued. If it is already known,
17862b794c40SVakul Garg 		 * but does not match the record type just dequeued, go to end.
17872b794c40SVakul Garg 		 * We always get record type here since for tls1.2, record type
17882b794c40SVakul Garg 		 * is known just after record is dequeued from stream parser.
17892b794c40SVakul Garg 		 * For tls1.3, we disable async.
17902b794c40SVakul Garg 		 */
17912b794c40SVakul Garg 
17922b794c40SVakul Garg 		if (!control)
17932b794c40SVakul Garg 			control = tlm->control;
17942b794c40SVakul Garg 		else if (control != tlm->control)
17952b794c40SVakul Garg 			goto recv_end;
1796fedf201eSDave Watson 
1797c46234ebSDave Watson 		if (!cmsg) {
1798c46234ebSDave Watson 			int cerr;
1799c46234ebSDave Watson 
1800c46234ebSDave Watson 			cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
18012b794c40SVakul Garg 					sizeof(control), &control);
1802c46234ebSDave Watson 			cmsg = true;
18032b794c40SVakul Garg 			if (control != TLS_RECORD_TYPE_DATA) {
1804c46234ebSDave Watson 				if (cerr || msg->msg_flags & MSG_CTRUNC) {
1805c46234ebSDave Watson 					err = -EIO;
1806c46234ebSDave Watson 					goto recv_end;
1807c46234ebSDave Watson 				}
1808c46234ebSDave Watson 			}
1809c46234ebSDave Watson 		}
1810c46234ebSDave Watson 
1811c0ab4732SVakul Garg 		if (async)
1812c0ab4732SVakul Garg 			goto pick_next_record;
1813c0ab4732SVakul Garg 
1814c46234ebSDave Watson 		if (!zc) {
1815692d7b5dSVakul Garg 			if (rxm->full_len > len) {
1816692d7b5dSVakul Garg 				retain_skb = true;
1817692d7b5dSVakul Garg 				chunk = len;
1818692d7b5dSVakul Garg 			} else {
1819692d7b5dSVakul Garg 				chunk = rxm->full_len;
1820692d7b5dSVakul Garg 			}
182194524d8fSVakul Garg 
1822692d7b5dSVakul Garg 			err = skb_copy_datagram_msg(skb, rxm->offset,
1823692d7b5dSVakul Garg 						    msg, chunk);
1824c46234ebSDave Watson 			if (err < 0)
1825c46234ebSDave Watson 				goto recv_end;
1826692d7b5dSVakul Garg 
1827692d7b5dSVakul Garg 			if (!is_peek) {
1828692d7b5dSVakul Garg 				rxm->offset = rxm->offset + chunk;
1829692d7b5dSVakul Garg 				rxm->full_len = rxm->full_len - chunk;
1830692d7b5dSVakul Garg 			}
1831692d7b5dSVakul Garg 		}
1832c46234ebSDave Watson 
183394524d8fSVakul Garg pick_next_record:
1834692d7b5dSVakul Garg 		if (chunk > len)
1835692d7b5dSVakul Garg 			chunk = len;
1836c46234ebSDave Watson 
1837692d7b5dSVakul Garg 		decrypted += chunk;
1838692d7b5dSVakul Garg 		len -= chunk;
1839692d7b5dSVakul Garg 
1840692d7b5dSVakul Garg 		/* For async or peek case, queue the current skb */
1841692d7b5dSVakul Garg 		if (async || is_peek || retain_skb) {
1842692d7b5dSVakul Garg 			skb_queue_tail(&ctx->rx_list, skb);
184394524d8fSVakul Garg 			skb = NULL;
1844692d7b5dSVakul Garg 		}
184594524d8fSVakul Garg 
1846c46234ebSDave Watson 		if (tls_sw_advance_skb(sk, skb, chunk)) {
1847c46234ebSDave Watson 			/* Return full control message to
1848c46234ebSDave Watson 			 * userspace before trying to parse
1849c46234ebSDave Watson 			 * another message type
1850c46234ebSDave Watson 			 */
1851c46234ebSDave Watson 			msg->msg_flags |= MSG_EOR;
1852692d7b5dSVakul Garg 			if (ctx->control != TLS_RECORD_TYPE_DATA)
1853c46234ebSDave Watson 				goto recv_end;
185494524d8fSVakul Garg 		} else {
185594524d8fSVakul Garg 			break;
1856c46234ebSDave Watson 		}
185704b25a54SJakub Kicinski 	}
1858c46234ebSDave Watson 
1859c46234ebSDave Watson recv_end:
186094524d8fSVakul Garg 	if (num_async) {
186194524d8fSVakul Garg 		/* Wait for all previously submitted records to be decrypted */
186294524d8fSVakul Garg 		smp_store_mb(ctx->async_notify, true);
186394524d8fSVakul Garg 		if (atomic_read(&ctx->decrypt_pending)) {
186494524d8fSVakul Garg 			err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
186594524d8fSVakul Garg 			if (err) {
186694524d8fSVakul Garg 				/* one of async decrypt failed */
186794524d8fSVakul Garg 				tls_err_abort(sk, err);
186894524d8fSVakul Garg 				copied = 0;
1869692d7b5dSVakul Garg 				decrypted = 0;
1870692d7b5dSVakul Garg 				goto end;
187194524d8fSVakul Garg 			}
187294524d8fSVakul Garg 		} else {
187394524d8fSVakul Garg 			reinit_completion(&ctx->async_wait.completion);
187494524d8fSVakul Garg 		}
187594524d8fSVakul Garg 		WRITE_ONCE(ctx->async_notify, false);
1876692d7b5dSVakul Garg 
1877692d7b5dSVakul Garg 		/* Drain records from the rx_list & copy if required */
1878692d7b5dSVakul Garg 		if (is_peek || is_kvec)
18792b794c40SVakul Garg 			err = process_rx_list(ctx, msg, &control, &cmsg, copied,
1880692d7b5dSVakul Garg 					      decrypted, false, is_peek);
1881692d7b5dSVakul Garg 		else
18822b794c40SVakul Garg 			err = process_rx_list(ctx, msg, &control, &cmsg, 0,
1883692d7b5dSVakul Garg 					      decrypted, true, is_peek);
1884692d7b5dSVakul Garg 		if (err < 0) {
1885692d7b5dSVakul Garg 			tls_err_abort(sk, err);
1886692d7b5dSVakul Garg 			copied = 0;
1887692d7b5dSVakul Garg 			goto end;
188894524d8fSVakul Garg 		}
1889692d7b5dSVakul Garg 	}
1890692d7b5dSVakul Garg 
1891692d7b5dSVakul Garg 	copied += decrypted;
1892692d7b5dSVakul Garg 
1893692d7b5dSVakul Garg end:
1894c46234ebSDave Watson 	release_sock(sk);
1895d3b18ad3SJohn Fastabend 	if (psock)
1896d3b18ad3SJohn Fastabend 		sk_psock_put(sk, psock);
1897c46234ebSDave Watson 	return copied ? : err;
1898c46234ebSDave Watson }
1899c46234ebSDave Watson 
1900c46234ebSDave Watson ssize_t tls_sw_splice_read(struct socket *sock,  loff_t *ppos,
1901c46234ebSDave Watson 			   struct pipe_inode_info *pipe,
1902c46234ebSDave Watson 			   size_t len, unsigned int flags)
1903c46234ebSDave Watson {
1904c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
1905f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1906c46234ebSDave Watson 	struct strp_msg *rxm = NULL;
1907c46234ebSDave Watson 	struct sock *sk = sock->sk;
1908c46234ebSDave Watson 	struct sk_buff *skb;
1909c46234ebSDave Watson 	ssize_t copied = 0;
1910c46234ebSDave Watson 	int err = 0;
1911c46234ebSDave Watson 	long timeo;
1912c46234ebSDave Watson 	int chunk;
19130b243d00SVakul Garg 	bool zc = false;
1914c46234ebSDave Watson 
1915c46234ebSDave Watson 	lock_sock(sk);
1916c46234ebSDave Watson 
1917c46234ebSDave Watson 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1918c46234ebSDave Watson 
1919d3b18ad3SJohn Fastabend 	skb = tls_wait_data(sk, NULL, flags, timeo, &err);
1920c46234ebSDave Watson 	if (!skb)
1921c46234ebSDave Watson 		goto splice_read_end;
1922c46234ebSDave Watson 
1923fedf201eSDave Watson 	if (!ctx->decrypted) {
1924fedf201eSDave Watson 		err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc, false);
1925fedf201eSDave Watson 
1926c46234ebSDave Watson 		/* splice does not support reading control messages */
1927c46234ebSDave Watson 		if (ctx->control != TLS_RECORD_TYPE_DATA) {
1928c46234ebSDave Watson 			err = -ENOTSUPP;
1929c46234ebSDave Watson 			goto splice_read_end;
1930c46234ebSDave Watson 		}
1931c46234ebSDave Watson 
1932c46234ebSDave Watson 		if (err < 0) {
1933c46234ebSDave Watson 			tls_err_abort(sk, EBADMSG);
1934c46234ebSDave Watson 			goto splice_read_end;
1935c46234ebSDave Watson 		}
1936c46234ebSDave Watson 		ctx->decrypted = true;
1937c46234ebSDave Watson 	}
1938c46234ebSDave Watson 	rxm = strp_msg(skb);
1939c46234ebSDave Watson 
1940c46234ebSDave Watson 	chunk = min_t(unsigned int, rxm->full_len, len);
1941c46234ebSDave Watson 	copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1942c46234ebSDave Watson 	if (copied < 0)
1943c46234ebSDave Watson 		goto splice_read_end;
1944c46234ebSDave Watson 
1945c46234ebSDave Watson 	if (likely(!(flags & MSG_PEEK)))
1946c46234ebSDave Watson 		tls_sw_advance_skb(sk, skb, copied);
1947c46234ebSDave Watson 
1948c46234ebSDave Watson splice_read_end:
1949c46234ebSDave Watson 	release_sock(sk);
1950c46234ebSDave Watson 	return copied ? : err;
1951c46234ebSDave Watson }
1952c46234ebSDave Watson 
1953924ad65eSJohn Fastabend bool tls_sw_stream_read(const struct sock *sk)
1954c46234ebSDave Watson {
1955c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
1956f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1957d3b18ad3SJohn Fastabend 	bool ingress_empty = true;
1958d3b18ad3SJohn Fastabend 	struct sk_psock *psock;
1959c46234ebSDave Watson 
1960d3b18ad3SJohn Fastabend 	rcu_read_lock();
1961d3b18ad3SJohn Fastabend 	psock = sk_psock(sk);
1962d3b18ad3SJohn Fastabend 	if (psock)
1963d3b18ad3SJohn Fastabend 		ingress_empty = list_empty(&psock->ingress_msg);
1964d3b18ad3SJohn Fastabend 	rcu_read_unlock();
1965c46234ebSDave Watson 
196613aecb17SJakub Kicinski 	return !ingress_empty || ctx->recv_pkt ||
196713aecb17SJakub Kicinski 		!skb_queue_empty(&ctx->rx_list);
1968c46234ebSDave Watson }
1969c46234ebSDave Watson 
1970c46234ebSDave Watson static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1971c46234ebSDave Watson {
1972c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
1973f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
19744509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
19753463e51dSKees Cook 	char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
1976c46234ebSDave Watson 	struct strp_msg *rxm = strp_msg(skb);
1977c46234ebSDave Watson 	size_t cipher_overhead;
1978c46234ebSDave Watson 	size_t data_len = 0;
1979c46234ebSDave Watson 	int ret;
1980c46234ebSDave Watson 
1981c46234ebSDave Watson 	/* Verify that we have a full TLS header, or wait for more data */
19824509de14SVakul Garg 	if (rxm->offset + prot->prepend_size > skb->len)
1983c46234ebSDave Watson 		return 0;
1984c46234ebSDave Watson 
19853463e51dSKees Cook 	/* Sanity-check size of on-stack buffer. */
19864509de14SVakul Garg 	if (WARN_ON(prot->prepend_size > sizeof(header))) {
19873463e51dSKees Cook 		ret = -EINVAL;
19883463e51dSKees Cook 		goto read_failure;
19893463e51dSKees Cook 	}
19903463e51dSKees Cook 
1991c46234ebSDave Watson 	/* Linearize header to local buffer */
19924509de14SVakul Garg 	ret = skb_copy_bits(skb, rxm->offset, header, prot->prepend_size);
1993c46234ebSDave Watson 
1994c46234ebSDave Watson 	if (ret < 0)
1995c46234ebSDave Watson 		goto read_failure;
1996c46234ebSDave Watson 
1997c46234ebSDave Watson 	ctx->control = header[0];
1998c46234ebSDave Watson 
1999c46234ebSDave Watson 	data_len = ((header[4] & 0xFF) | (header[3] << 8));
2000c46234ebSDave Watson 
20014509de14SVakul Garg 	cipher_overhead = prot->tag_size;
20024509de14SVakul Garg 	if (prot->version != TLS_1_3_VERSION)
20034509de14SVakul Garg 		cipher_overhead += prot->iv_size;
2004c46234ebSDave Watson 
2005130b392cSDave Watson 	if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
20064509de14SVakul Garg 	    prot->tail_size) {
2007c46234ebSDave Watson 		ret = -EMSGSIZE;
2008c46234ebSDave Watson 		goto read_failure;
2009c46234ebSDave Watson 	}
2010c46234ebSDave Watson 	if (data_len < cipher_overhead) {
2011c46234ebSDave Watson 		ret = -EBADMSG;
2012c46234ebSDave Watson 		goto read_failure;
2013c46234ebSDave Watson 	}
2014c46234ebSDave Watson 
2015130b392cSDave Watson 	/* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
2016130b392cSDave Watson 	if (header[1] != TLS_1_2_VERSION_MINOR ||
2017130b392cSDave Watson 	    header[2] != TLS_1_2_VERSION_MAJOR) {
2018c46234ebSDave Watson 		ret = -EINVAL;
2019c46234ebSDave Watson 		goto read_failure;
2020c46234ebSDave Watson 	}
2021be2fbc15SJakub Kicinski 
2022f953d33bSJakub Kicinski 	tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
2023fe58a5a0SJakub Kicinski 				     TCP_SKB_CB(skb)->seq + rxm->offset);
2024c46234ebSDave Watson 	return data_len + TLS_HEADER_SIZE;
2025c46234ebSDave Watson 
2026c46234ebSDave Watson read_failure:
2027c46234ebSDave Watson 	tls_err_abort(strp->sk, ret);
2028c46234ebSDave Watson 
2029c46234ebSDave Watson 	return ret;
2030c46234ebSDave Watson }
2031c46234ebSDave Watson 
2032c46234ebSDave Watson static void tls_queue(struct strparser *strp, struct sk_buff *skb)
2033c46234ebSDave Watson {
2034c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2035f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2036c46234ebSDave Watson 
2037c46234ebSDave Watson 	ctx->decrypted = false;
2038c46234ebSDave Watson 
2039c46234ebSDave Watson 	ctx->recv_pkt = skb;
2040c46234ebSDave Watson 	strp_pause(strp);
2041c46234ebSDave Watson 
2042ad13acceSVakul Garg 	ctx->saved_data_ready(strp->sk);
2043c46234ebSDave Watson }
2044c46234ebSDave Watson 
2045c46234ebSDave Watson static void tls_data_ready(struct sock *sk)
2046c46234ebSDave Watson {
2047c46234ebSDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2048f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2049d3b18ad3SJohn Fastabend 	struct sk_psock *psock;
2050c46234ebSDave Watson 
2051c46234ebSDave Watson 	strp_data_ready(&ctx->strp);
2052d3b18ad3SJohn Fastabend 
2053d3b18ad3SJohn Fastabend 	psock = sk_psock_get(sk);
2054d3b18ad3SJohn Fastabend 	if (psock && !list_empty(&psock->ingress_msg)) {
2055d3b18ad3SJohn Fastabend 		ctx->saved_data_ready(sk);
2056d3b18ad3SJohn Fastabend 		sk_psock_put(sk, psock);
2057d3b18ad3SJohn Fastabend 	}
2058c46234ebSDave Watson }
2059c46234ebSDave Watson 
2060f87e62d4SJohn Fastabend void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
2061f87e62d4SJohn Fastabend {
2062f87e62d4SJohn Fastabend 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2063f87e62d4SJohn Fastabend 
2064f87e62d4SJohn Fastabend 	set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
2065f87e62d4SJohn Fastabend 	set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
2066f87e62d4SJohn Fastabend 	cancel_delayed_work_sync(&ctx->tx_work.work);
2067f87e62d4SJohn Fastabend }
2068f87e62d4SJohn Fastabend 
2069313ab004SJohn Fastabend void tls_sw_release_resources_tx(struct sock *sk)
20703c4d7559SDave Watson {
20713c4d7559SDave Watson 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2072f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2073a42055e8SVakul Garg 	struct tls_rec *rec, *tmp;
2074a42055e8SVakul Garg 
2075a42055e8SVakul Garg 	/* Wait for any pending async encryptions to complete */
2076a42055e8SVakul Garg 	smp_store_mb(ctx->async_notify, true);
2077a42055e8SVakul Garg 	if (atomic_read(&ctx->encrypt_pending))
2078a42055e8SVakul Garg 		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2079a42055e8SVakul Garg 
2080a42055e8SVakul Garg 	tls_tx_records(sk, -1);
2081a42055e8SVakul Garg 
20829932a29aSVakul Garg 	/* Free up un-sent records in tx_list. First, free
2083a42055e8SVakul Garg 	 * the partially sent record if any at head of tx_list.
2084a42055e8SVakul Garg 	 */
208535b71a34SJakub Kicinski 	if (tls_free_partial_record(sk, tls_ctx)) {
20869932a29aSVakul Garg 		rec = list_first_entry(&ctx->tx_list,
2087a42055e8SVakul Garg 				       struct tls_rec, list);
2088a42055e8SVakul Garg 		list_del(&rec->list);
2089d829e9c4SDaniel Borkmann 		sk_msg_free(sk, &rec->msg_plaintext);
2090a42055e8SVakul Garg 		kfree(rec);
2091a42055e8SVakul Garg 	}
2092a42055e8SVakul Garg 
20939932a29aSVakul Garg 	list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
2094a42055e8SVakul Garg 		list_del(&rec->list);
2095d829e9c4SDaniel Borkmann 		sk_msg_free(sk, &rec->msg_encrypted);
2096d829e9c4SDaniel Borkmann 		sk_msg_free(sk, &rec->msg_plaintext);
2097a42055e8SVakul Garg 		kfree(rec);
2098a42055e8SVakul Garg 	}
20993c4d7559SDave Watson 
21003c4d7559SDave Watson 	crypto_free_aead(ctx->aead_send);
2101c774973eSVakul Garg 	tls_free_open_rec(sk);
2102313ab004SJohn Fastabend }
2103313ab004SJohn Fastabend 
2104313ab004SJohn Fastabend void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
2105313ab004SJohn Fastabend {
2106313ab004SJohn Fastabend 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2107f66de3eeSBoris Pismenny 
2108f66de3eeSBoris Pismenny 	kfree(ctx);
2109f66de3eeSBoris Pismenny }
2110f66de3eeSBoris Pismenny 
211139f56e1aSBoris Pismenny void tls_sw_release_resources_rx(struct sock *sk)
2112f66de3eeSBoris Pismenny {
2113f66de3eeSBoris Pismenny 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2114f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2115f66de3eeSBoris Pismenny 
211612c76861SJakub Kicinski 	kfree(tls_ctx->rx.rec_seq);
211712c76861SJakub Kicinski 	kfree(tls_ctx->rx.iv);
211812c76861SJakub Kicinski 
2119c46234ebSDave Watson 	if (ctx->aead_recv) {
2120c46234ebSDave Watson 		kfree_skb(ctx->recv_pkt);
2121c46234ebSDave Watson 		ctx->recv_pkt = NULL;
2122692d7b5dSVakul Garg 		skb_queue_purge(&ctx->rx_list);
2123c46234ebSDave Watson 		crypto_free_aead(ctx->aead_recv);
2124c46234ebSDave Watson 		strp_stop(&ctx->strp);
2125313ab004SJohn Fastabend 		/* If tls_sw_strparser_arm() was not called (cleanup paths)
2126313ab004SJohn Fastabend 		 * we still want to strp_stop(), but sk->sk_data_ready was
2127313ab004SJohn Fastabend 		 * never swapped.
2128313ab004SJohn Fastabend 		 */
2129313ab004SJohn Fastabend 		if (ctx->saved_data_ready) {
2130c46234ebSDave Watson 			write_lock_bh(&sk->sk_callback_lock);
2131c46234ebSDave Watson 			sk->sk_data_ready = ctx->saved_data_ready;
2132c46234ebSDave Watson 			write_unlock_bh(&sk->sk_callback_lock);
2133c46234ebSDave Watson 		}
213439f56e1aSBoris Pismenny 	}
2135313ab004SJohn Fastabend }
2136313ab004SJohn Fastabend 
2137313ab004SJohn Fastabend void tls_sw_strparser_done(struct tls_context *tls_ctx)
2138313ab004SJohn Fastabend {
2139313ab004SJohn Fastabend 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2140313ab004SJohn Fastabend 
2141313ab004SJohn Fastabend 	strp_done(&ctx->strp);
2142313ab004SJohn Fastabend }
2143313ab004SJohn Fastabend 
2144313ab004SJohn Fastabend void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
2145313ab004SJohn Fastabend {
2146313ab004SJohn Fastabend 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2147313ab004SJohn Fastabend 
2148313ab004SJohn Fastabend 	kfree(ctx);
2149313ab004SJohn Fastabend }
215039f56e1aSBoris Pismenny 
215139f56e1aSBoris Pismenny void tls_sw_free_resources_rx(struct sock *sk)
215239f56e1aSBoris Pismenny {
215339f56e1aSBoris Pismenny 	struct tls_context *tls_ctx = tls_get_ctx(sk);
215439f56e1aSBoris Pismenny 
215539f56e1aSBoris Pismenny 	tls_sw_release_resources_rx(sk);
2156313ab004SJohn Fastabend 	tls_sw_free_ctx_rx(tls_ctx);
21573c4d7559SDave Watson }
21583c4d7559SDave Watson 
21599932a29aSVakul Garg /* The work handler to transmitt the encrypted records in tx_list */
2160a42055e8SVakul Garg static void tx_work_handler(struct work_struct *work)
2161a42055e8SVakul Garg {
2162a42055e8SVakul Garg 	struct delayed_work *delayed_work = to_delayed_work(work);
2163a42055e8SVakul Garg 	struct tx_work *tx_work = container_of(delayed_work,
2164a42055e8SVakul Garg 					       struct tx_work, work);
2165a42055e8SVakul Garg 	struct sock *sk = tx_work->sk;
2166a42055e8SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2167f87e62d4SJohn Fastabend 	struct tls_sw_context_tx *ctx;
2168f87e62d4SJohn Fastabend 
2169f87e62d4SJohn Fastabend 	if (unlikely(!tls_ctx))
2170f87e62d4SJohn Fastabend 		return;
2171f87e62d4SJohn Fastabend 
2172f87e62d4SJohn Fastabend 	ctx = tls_sw_ctx_tx(tls_ctx);
2173f87e62d4SJohn Fastabend 	if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
2174f87e62d4SJohn Fastabend 		return;
2175a42055e8SVakul Garg 
2176a42055e8SVakul Garg 	if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
2177a42055e8SVakul Garg 		return;
2178a42055e8SVakul Garg 	lock_sock(sk);
2179a42055e8SVakul Garg 	tls_tx_records(sk, -1);
2180a42055e8SVakul Garg 	release_sock(sk);
2181a42055e8SVakul Garg }
2182a42055e8SVakul Garg 
21837463d3a2SBoris Pismenny void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
21847463d3a2SBoris Pismenny {
21857463d3a2SBoris Pismenny 	struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
21867463d3a2SBoris Pismenny 
21877463d3a2SBoris Pismenny 	/* Schedule the transmission if tx list is ready */
21887463d3a2SBoris Pismenny 	if (is_tx_ready(tx_ctx) && !sk->sk_write_pending) {
21897463d3a2SBoris Pismenny 		/* Schedule the transmission */
21907463d3a2SBoris Pismenny 		if (!test_and_set_bit(BIT_TX_SCHEDULED,
21917463d3a2SBoris Pismenny 				      &tx_ctx->tx_bitmask))
21927463d3a2SBoris Pismenny 			schedule_delayed_work(&tx_ctx->tx_work.work, 0);
21937463d3a2SBoris Pismenny 	}
21947463d3a2SBoris Pismenny }
21957463d3a2SBoris Pismenny 
2196318892acSJakub Kicinski void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
2197318892acSJakub Kicinski {
2198318892acSJakub Kicinski 	struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2199318892acSJakub Kicinski 
2200318892acSJakub Kicinski 	write_lock_bh(&sk->sk_callback_lock);
2201318892acSJakub Kicinski 	rx_ctx->saved_data_ready = sk->sk_data_ready;
2202318892acSJakub Kicinski 	sk->sk_data_ready = tls_data_ready;
2203318892acSJakub Kicinski 	write_unlock_bh(&sk->sk_callback_lock);
2204318892acSJakub Kicinski 
2205318892acSJakub Kicinski 	strp_check_rcv(&rx_ctx->strp);
2206318892acSJakub Kicinski }
2207318892acSJakub Kicinski 
2208c46234ebSDave Watson int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
22093c4d7559SDave Watson {
22104509de14SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
22114509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
22123c4d7559SDave Watson 	struct tls_crypto_info *crypto_info;
22133c4d7559SDave Watson 	struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
2214fb99bce7SDave Watson 	struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
2215f295b3aeSVakul Garg 	struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
2216f66de3eeSBoris Pismenny 	struct tls_sw_context_tx *sw_ctx_tx = NULL;
2217f66de3eeSBoris Pismenny 	struct tls_sw_context_rx *sw_ctx_rx = NULL;
2218c46234ebSDave Watson 	struct cipher_context *cctx;
2219c46234ebSDave Watson 	struct crypto_aead **aead;
2220c46234ebSDave Watson 	struct strp_callbacks cb;
2221f295b3aeSVakul Garg 	u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
2222692d7b5dSVakul Garg 	struct crypto_tfm *tfm;
2223f295b3aeSVakul Garg 	char *iv, *rec_seq, *key, *salt, *cipher_name;
2224fb99bce7SDave Watson 	size_t keysize;
22253c4d7559SDave Watson 	int rc = 0;
22263c4d7559SDave Watson 
22273c4d7559SDave Watson 	if (!ctx) {
22283c4d7559SDave Watson 		rc = -EINVAL;
22293c4d7559SDave Watson 		goto out;
22303c4d7559SDave Watson 	}
22313c4d7559SDave Watson 
2232f66de3eeSBoris Pismenny 	if (tx) {
2233b190a587SBoris Pismenny 		if (!ctx->priv_ctx_tx) {
2234f66de3eeSBoris Pismenny 			sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2235f66de3eeSBoris Pismenny 			if (!sw_ctx_tx) {
22363c4d7559SDave Watson 				rc = -ENOMEM;
22373c4d7559SDave Watson 				goto out;
22383c4d7559SDave Watson 			}
2239f66de3eeSBoris Pismenny 			ctx->priv_ctx_tx = sw_ctx_tx;
2240c46234ebSDave Watson 		} else {
2241b190a587SBoris Pismenny 			sw_ctx_tx =
2242b190a587SBoris Pismenny 				(struct tls_sw_context_tx *)ctx->priv_ctx_tx;
2243b190a587SBoris Pismenny 		}
2244b190a587SBoris Pismenny 	} else {
2245b190a587SBoris Pismenny 		if (!ctx->priv_ctx_rx) {
2246f66de3eeSBoris Pismenny 			sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2247f66de3eeSBoris Pismenny 			if (!sw_ctx_rx) {
2248f66de3eeSBoris Pismenny 				rc = -ENOMEM;
2249f66de3eeSBoris Pismenny 				goto out;
2250c46234ebSDave Watson 			}
2251f66de3eeSBoris Pismenny 			ctx->priv_ctx_rx = sw_ctx_rx;
2252b190a587SBoris Pismenny 		} else {
2253b190a587SBoris Pismenny 			sw_ctx_rx =
2254b190a587SBoris Pismenny 				(struct tls_sw_context_rx *)ctx->priv_ctx_rx;
2255b190a587SBoris Pismenny 		}
2256f66de3eeSBoris Pismenny 	}
22573c4d7559SDave Watson 
2258c46234ebSDave Watson 	if (tx) {
2259b190a587SBoris Pismenny 		crypto_init_wait(&sw_ctx_tx->async_wait);
226086029d10SSabrina Dubroca 		crypto_info = &ctx->crypto_send.info;
2261c46234ebSDave Watson 		cctx = &ctx->tx;
2262f66de3eeSBoris Pismenny 		aead = &sw_ctx_tx->aead_send;
22639932a29aSVakul Garg 		INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
2264a42055e8SVakul Garg 		INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2265a42055e8SVakul Garg 		sw_ctx_tx->tx_work.sk = sk;
2266c46234ebSDave Watson 	} else {
2267b190a587SBoris Pismenny 		crypto_init_wait(&sw_ctx_rx->async_wait);
226886029d10SSabrina Dubroca 		crypto_info = &ctx->crypto_recv.info;
2269c46234ebSDave Watson 		cctx = &ctx->rx;
2270692d7b5dSVakul Garg 		skb_queue_head_init(&sw_ctx_rx->rx_list);
2271f66de3eeSBoris Pismenny 		aead = &sw_ctx_rx->aead_recv;
2272c46234ebSDave Watson 	}
2273c46234ebSDave Watson 
22743c4d7559SDave Watson 	switch (crypto_info->cipher_type) {
22753c4d7559SDave Watson 	case TLS_CIPHER_AES_GCM_128: {
22763c4d7559SDave Watson 		nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
22773c4d7559SDave Watson 		tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
22783c4d7559SDave Watson 		iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
22793c4d7559SDave Watson 		iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
22803c4d7559SDave Watson 		rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
22813c4d7559SDave Watson 		rec_seq =
22823c4d7559SDave Watson 		 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
22833c4d7559SDave Watson 		gcm_128_info =
22843c4d7559SDave Watson 			(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
2285fb99bce7SDave Watson 		keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
2286fb99bce7SDave Watson 		key = gcm_128_info->key;
2287fb99bce7SDave Watson 		salt = gcm_128_info->salt;
2288f295b3aeSVakul Garg 		salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
2289f295b3aeSVakul Garg 		cipher_name = "gcm(aes)";
2290fb99bce7SDave Watson 		break;
2291fb99bce7SDave Watson 	}
2292fb99bce7SDave Watson 	case TLS_CIPHER_AES_GCM_256: {
2293fb99bce7SDave Watson 		nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2294fb99bce7SDave Watson 		tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
2295fb99bce7SDave Watson 		iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2296fb99bce7SDave Watson 		iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv;
2297fb99bce7SDave Watson 		rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
2298fb99bce7SDave Watson 		rec_seq =
2299fb99bce7SDave Watson 		 ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq;
2300fb99bce7SDave Watson 		gcm_256_info =
2301fb99bce7SDave Watson 			(struct tls12_crypto_info_aes_gcm_256 *)crypto_info;
2302fb99bce7SDave Watson 		keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
2303fb99bce7SDave Watson 		key = gcm_256_info->key;
2304fb99bce7SDave Watson 		salt = gcm_256_info->salt;
2305f295b3aeSVakul Garg 		salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
2306f295b3aeSVakul Garg 		cipher_name = "gcm(aes)";
2307f295b3aeSVakul Garg 		break;
2308f295b3aeSVakul Garg 	}
2309f295b3aeSVakul Garg 	case TLS_CIPHER_AES_CCM_128: {
2310f295b3aeSVakul Garg 		nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2311f295b3aeSVakul Garg 		tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
2312f295b3aeSVakul Garg 		iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2313f295b3aeSVakul Garg 		iv = ((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->iv;
2314f295b3aeSVakul Garg 		rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
2315f295b3aeSVakul Garg 		rec_seq =
2316f295b3aeSVakul Garg 		((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->rec_seq;
2317f295b3aeSVakul Garg 		ccm_128_info =
2318f295b3aeSVakul Garg 		(struct tls12_crypto_info_aes_ccm_128 *)crypto_info;
2319f295b3aeSVakul Garg 		keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
2320f295b3aeSVakul Garg 		key = ccm_128_info->key;
2321f295b3aeSVakul Garg 		salt = ccm_128_info->salt;
2322f295b3aeSVakul Garg 		salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
2323f295b3aeSVakul Garg 		cipher_name = "ccm(aes)";
23243c4d7559SDave Watson 		break;
23253c4d7559SDave Watson 	}
23263c4d7559SDave Watson 	default:
23273c4d7559SDave Watson 		rc = -EINVAL;
2328cf6d43efSSabrina Dubroca 		goto free_priv;
23293c4d7559SDave Watson 	}
23303c4d7559SDave Watson 
233189fec474SJakub Kicinski 	/* Sanity-check the sizes for stack allocations. */
233289fec474SJakub Kicinski 	if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
233389fec474SJakub Kicinski 	    rec_seq_size > TLS_MAX_REC_SEQ_SIZE) {
2334b16520f7SKees Cook 		rc = -EINVAL;
2335b16520f7SKees Cook 		goto free_priv;
2336b16520f7SKees Cook 	}
2337b16520f7SKees Cook 
2338130b392cSDave Watson 	if (crypto_info->version == TLS_1_3_VERSION) {
2339130b392cSDave Watson 		nonce_size = 0;
23404509de14SVakul Garg 		prot->aad_size = TLS_HEADER_SIZE;
23414509de14SVakul Garg 		prot->tail_size = 1;
2342130b392cSDave Watson 	} else {
23434509de14SVakul Garg 		prot->aad_size = TLS_AAD_SPACE_SIZE;
23444509de14SVakul Garg 		prot->tail_size = 0;
2345130b392cSDave Watson 	}
2346130b392cSDave Watson 
23474509de14SVakul Garg 	prot->version = crypto_info->version;
23484509de14SVakul Garg 	prot->cipher_type = crypto_info->cipher_type;
23494509de14SVakul Garg 	prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
23504509de14SVakul Garg 	prot->tag_size = tag_size;
23514509de14SVakul Garg 	prot->overhead_size = prot->prepend_size +
23524509de14SVakul Garg 			      prot->tag_size + prot->tail_size;
23534509de14SVakul Garg 	prot->iv_size = iv_size;
2354f295b3aeSVakul Garg 	prot->salt_size = salt_size;
2355f295b3aeSVakul Garg 	cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
2356c46234ebSDave Watson 	if (!cctx->iv) {
23573c4d7559SDave Watson 		rc = -ENOMEM;
2358cf6d43efSSabrina Dubroca 		goto free_priv;
23593c4d7559SDave Watson 	}
2360fb99bce7SDave Watson 	/* Note: 128 & 256 bit salt are the same size */
23614509de14SVakul Garg 	prot->rec_seq_size = rec_seq_size;
2362f295b3aeSVakul Garg 	memcpy(cctx->iv, salt, salt_size);
2363f295b3aeSVakul Garg 	memcpy(cctx->iv + salt_size, iv, iv_size);
2364969d5090Szhong jiang 	cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
2365c46234ebSDave Watson 	if (!cctx->rec_seq) {
23663c4d7559SDave Watson 		rc = -ENOMEM;
23673c4d7559SDave Watson 		goto free_iv;
23683c4d7559SDave Watson 	}
23693c4d7559SDave Watson 
2370c46234ebSDave Watson 	if (!*aead) {
2371f295b3aeSVakul Garg 		*aead = crypto_alloc_aead(cipher_name, 0, 0);
2372c46234ebSDave Watson 		if (IS_ERR(*aead)) {
2373c46234ebSDave Watson 			rc = PTR_ERR(*aead);
2374c46234ebSDave Watson 			*aead = NULL;
23753c4d7559SDave Watson 			goto free_rec_seq;
23763c4d7559SDave Watson 		}
23773c4d7559SDave Watson 	}
23783c4d7559SDave Watson 
23793c4d7559SDave Watson 	ctx->push_pending_record = tls_sw_push_pending_record;
23803c4d7559SDave Watson 
2381fb99bce7SDave Watson 	rc = crypto_aead_setkey(*aead, key, keysize);
2382fb99bce7SDave Watson 
23833c4d7559SDave Watson 	if (rc)
23843c4d7559SDave Watson 		goto free_aead;
23853c4d7559SDave Watson 
23864509de14SVakul Garg 	rc = crypto_aead_setauthsize(*aead, prot->tag_size);
2387c46234ebSDave Watson 	if (rc)
2388c46234ebSDave Watson 		goto free_aead;
2389c46234ebSDave Watson 
2390f66de3eeSBoris Pismenny 	if (sw_ctx_rx) {
2391692d7b5dSVakul Garg 		tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
23928497ded2SVakul Garg 
23938497ded2SVakul Garg 		if (crypto_info->version == TLS_1_3_VERSION)
23948497ded2SVakul Garg 			sw_ctx_rx->async_capable = false;
23958497ded2SVakul Garg 		else
2396692d7b5dSVakul Garg 			sw_ctx_rx->async_capable =
2397692d7b5dSVakul Garg 				tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
2398692d7b5dSVakul Garg 
2399c46234ebSDave Watson 		/* Set up strparser */
2400c46234ebSDave Watson 		memset(&cb, 0, sizeof(cb));
2401c46234ebSDave Watson 		cb.rcv_msg = tls_queue;
2402c46234ebSDave Watson 		cb.parse_msg = tls_read_size;
2403c46234ebSDave Watson 
2404f66de3eeSBoris Pismenny 		strp_init(&sw_ctx_rx->strp, sk, &cb);
2405c46234ebSDave Watson 	}
2406c46234ebSDave Watson 
2407c46234ebSDave Watson 	goto out;
24083c4d7559SDave Watson 
24093c4d7559SDave Watson free_aead:
2410c46234ebSDave Watson 	crypto_free_aead(*aead);
2411c46234ebSDave Watson 	*aead = NULL;
24123c4d7559SDave Watson free_rec_seq:
2413c46234ebSDave Watson 	kfree(cctx->rec_seq);
2414c46234ebSDave Watson 	cctx->rec_seq = NULL;
24153c4d7559SDave Watson free_iv:
2416f66de3eeSBoris Pismenny 	kfree(cctx->iv);
2417f66de3eeSBoris Pismenny 	cctx->iv = NULL;
2418cf6d43efSSabrina Dubroca free_priv:
2419f66de3eeSBoris Pismenny 	if (tx) {
2420f66de3eeSBoris Pismenny 		kfree(ctx->priv_ctx_tx);
2421f66de3eeSBoris Pismenny 		ctx->priv_ctx_tx = NULL;
2422f66de3eeSBoris Pismenny 	} else {
2423f66de3eeSBoris Pismenny 		kfree(ctx->priv_ctx_rx);
2424f66de3eeSBoris Pismenny 		ctx->priv_ctx_rx = NULL;
2425f66de3eeSBoris Pismenny 	}
24263c4d7559SDave Watson out:
24273c4d7559SDave Watson 	return rc;
24283c4d7559SDave Watson }
2429