2 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4 * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved.
5 * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved.
6 * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved.
7 * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
9 * This software is available to you under a choice of one of two
10 * licenses. You may choose to be licensed under the terms of the GNU
11 * General Public License (GPL) Version 2, available from the file
12 * COPYING in the main directory of this source tree, or the
13 * OpenIB.org BSD license below:
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials
26 * provided with the distribution.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #include <linux/bug.h>
39 #include <linux/sched/signal.h>
40 #include <linux/module.h>
41 #include <linux/splice.h>
42 #include <crypto/aead.h>
44 #include <net/strparser.h>
49 struct tls_decrypt_arg {
55 struct tls_decrypt_ctx {
57 u8 aad[TLS_MAX_AAD_SIZE];
59 struct scatterlist sg[];
62 noinline void tls_err_abort(struct sock *sk, int err)
64 WARN_ON_ONCE(err >= 0);
65 /* sk->sk_err should contain a positive error code. */
70 static int __skb_nsg(struct sk_buff *skb, int offset, int len,
71 unsigned int recursion_level)
73 int start = skb_headlen(skb);
74 int i, chunk = start - offset;
75 struct sk_buff *frag_iter;
78 if (unlikely(recursion_level >= 24))
91 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
94 WARN_ON(start > offset + len);
96 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
110 if (unlikely(skb_has_frag_list(skb))) {
111 skb_walk_frags(skb, frag_iter) {
114 WARN_ON(start > offset + len);
116 end = start + frag_iter->len;
117 chunk = end - offset;
121 ret = __skb_nsg(frag_iter, offset - start, chunk,
122 recursion_level + 1);
123 if (unlikely(ret < 0))
138 /* Return the number of scatterlist elements required to completely map the
139 * skb, or -EMSGSIZE if the recursion depth is exceeded.
141 static int skb_nsg(struct sk_buff *skb, int offset, int len)
143 return __skb_nsg(skb, offset, len, 0);
146 static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
147 struct tls_decrypt_arg *darg)
149 struct strp_msg *rxm = strp_msg(skb);
150 struct tls_msg *tlm = tls_msg(skb);
153 /* Determine zero-padding length */
154 if (prot->version == TLS_1_3_VERSION) {
155 int offset = rxm->full_len - TLS_TAG_SIZE - 1;
156 char content_type = darg->zc ? darg->tail : 0;
159 while (content_type == 0) {
160 if (offset < prot->prepend_size)
162 err = skb_copy_bits(skb, rxm->offset + offset,
171 tlm->control = content_type;
176 static void tls_decrypt_done(struct crypto_async_request *req, int err)
178 struct aead_request *aead_req = (struct aead_request *)req;
179 struct scatterlist *sgout = aead_req->dst;
180 struct scatterlist *sgin = aead_req->src;
181 struct tls_sw_context_rx *ctx;
182 struct tls_context *tls_ctx;
183 struct tls_prot_info *prot;
184 struct scatterlist *sg;
188 skb = (struct sk_buff *)req->data;
189 tls_ctx = tls_get_ctx(skb->sk);
190 ctx = tls_sw_ctx_rx(tls_ctx);
191 prot = &tls_ctx->prot_info;
193 /* Propagate if there was an err */
196 TLS_INC_STATS(sock_net(skb->sk),
197 LINUX_MIB_TLSDECRYPTERROR);
198 ctx->async_wait.err = err;
199 tls_err_abort(skb->sk, err);
201 struct strp_msg *rxm = strp_msg(skb);
203 /* No TLS 1.3 support with async crypto */
204 WARN_ON(prot->tail_size);
206 rxm->offset += prot->prepend_size;
207 rxm->full_len -= prot->overhead_size;
210 /* After using skb->sk to propagate sk through crypto async callback
211 * we need to NULL it again.
216 /* Free the destination pages if skb was not decrypted inplace */
218 /* Skip the first S/G entry as it points to AAD */
219 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
222 put_page(sg_page(sg));
228 spin_lock_bh(&ctx->decrypt_compl_lock);
229 if (!atomic_dec_return(&ctx->decrypt_pending))
230 complete(&ctx->async_wait.completion);
231 spin_unlock_bh(&ctx->decrypt_compl_lock);
234 static int tls_do_decryption(struct sock *sk,
236 struct scatterlist *sgin,
237 struct scatterlist *sgout,
240 struct aead_request *aead_req,
241 struct tls_decrypt_arg *darg)
243 struct tls_context *tls_ctx = tls_get_ctx(sk);
244 struct tls_prot_info *prot = &tls_ctx->prot_info;
245 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
248 aead_request_set_tfm(aead_req, ctx->aead_recv);
249 aead_request_set_ad(aead_req, prot->aad_size);
250 aead_request_set_crypt(aead_req, sgin, sgout,
251 data_len + prot->tag_size,
255 /* Using skb->sk to push sk through to crypto async callback
256 * handler. This allows propagating errors up to the socket
257 * if needed. It _must_ be cleared in the async handler
258 * before consume_skb is called. We _know_ skb->sk is NULL
259 * because it is a clone from strparser.
262 aead_request_set_callback(aead_req,
263 CRYPTO_TFM_REQ_MAY_BACKLOG,
264 tls_decrypt_done, skb);
265 atomic_inc(&ctx->decrypt_pending);
267 aead_request_set_callback(aead_req,
268 CRYPTO_TFM_REQ_MAY_BACKLOG,
269 crypto_req_done, &ctx->async_wait);
272 ret = crypto_aead_decrypt(aead_req);
273 if (ret == -EINPROGRESS) {
277 ret = crypto_wait_req(ret, &ctx->async_wait);
284 static void tls_trim_both_msgs(struct sock *sk, int target_size)
286 struct tls_context *tls_ctx = tls_get_ctx(sk);
287 struct tls_prot_info *prot = &tls_ctx->prot_info;
288 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
289 struct tls_rec *rec = ctx->open_rec;
291 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
293 target_size += prot->overhead_size;
294 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
297 static int tls_alloc_encrypted_msg(struct sock *sk, int len)
299 struct tls_context *tls_ctx = tls_get_ctx(sk);
300 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
301 struct tls_rec *rec = ctx->open_rec;
302 struct sk_msg *msg_en = &rec->msg_encrypted;
304 return sk_msg_alloc(sk, msg_en, len, 0);
307 static int tls_clone_plaintext_msg(struct sock *sk, int required)
309 struct tls_context *tls_ctx = tls_get_ctx(sk);
310 struct tls_prot_info *prot = &tls_ctx->prot_info;
311 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
312 struct tls_rec *rec = ctx->open_rec;
313 struct sk_msg *msg_pl = &rec->msg_plaintext;
314 struct sk_msg *msg_en = &rec->msg_encrypted;
317 /* We add page references worth len bytes from encrypted sg
318 * at the end of plaintext sg. It is guaranteed that msg_en
319 * has enough required room (ensured by caller).
321 len = required - msg_pl->sg.size;
323 /* Skip initial bytes in msg_en's data to be able to use
324 * same offset of both plain and encrypted data.
326 skip = prot->prepend_size + msg_pl->sg.size;
328 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
331 static struct tls_rec *tls_get_rec(struct sock *sk)
333 struct tls_context *tls_ctx = tls_get_ctx(sk);
334 struct tls_prot_info *prot = &tls_ctx->prot_info;
335 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
336 struct sk_msg *msg_pl, *msg_en;
340 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
342 rec = kzalloc(mem_size, sk->sk_allocation);
346 msg_pl = &rec->msg_plaintext;
347 msg_en = &rec->msg_encrypted;
352 sg_init_table(rec->sg_aead_in, 2);
353 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
354 sg_unmark_end(&rec->sg_aead_in[1]);
356 sg_init_table(rec->sg_aead_out, 2);
357 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
358 sg_unmark_end(&rec->sg_aead_out[1]);
363 static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
365 sk_msg_free(sk, &rec->msg_encrypted);
366 sk_msg_free(sk, &rec->msg_plaintext);
370 static void tls_free_open_rec(struct sock *sk)
372 struct tls_context *tls_ctx = tls_get_ctx(sk);
373 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
374 struct tls_rec *rec = ctx->open_rec;
377 tls_free_rec(sk, rec);
378 ctx->open_rec = NULL;
382 int tls_tx_records(struct sock *sk, int flags)
384 struct tls_context *tls_ctx = tls_get_ctx(sk);
385 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
386 struct tls_rec *rec, *tmp;
387 struct sk_msg *msg_en;
388 int tx_flags, rc = 0;
390 if (tls_is_partially_sent_record(tls_ctx)) {
391 rec = list_first_entry(&ctx->tx_list,
392 struct tls_rec, list);
395 tx_flags = rec->tx_flags;
399 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
403 /* Full record has been transmitted.
404 * Remove the head of tx_list
406 list_del(&rec->list);
407 sk_msg_free(sk, &rec->msg_plaintext);
411 /* Tx all ready records */
412 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
413 if (READ_ONCE(rec->tx_ready)) {
415 tx_flags = rec->tx_flags;
419 msg_en = &rec->msg_encrypted;
420 rc = tls_push_sg(sk, tls_ctx,
421 &msg_en->sg.data[msg_en->sg.curr],
426 list_del(&rec->list);
427 sk_msg_free(sk, &rec->msg_plaintext);
435 if (rc < 0 && rc != -EAGAIN)
436 tls_err_abort(sk, -EBADMSG);
441 static void tls_encrypt_done(struct crypto_async_request *req, int err)
443 struct aead_request *aead_req = (struct aead_request *)req;
444 struct sock *sk = req->data;
445 struct tls_context *tls_ctx = tls_get_ctx(sk);
446 struct tls_prot_info *prot = &tls_ctx->prot_info;
447 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
448 struct scatterlist *sge;
449 struct sk_msg *msg_en;
454 rec = container_of(aead_req, struct tls_rec, aead_req);
455 msg_en = &rec->msg_encrypted;
457 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
458 sge->offset -= prot->prepend_size;
459 sge->length += prot->prepend_size;
461 /* Check if error is previously set on socket */
462 if (err || sk->sk_err) {
465 /* If err is already set on socket, return the same code */
467 ctx->async_wait.err = -sk->sk_err;
469 ctx->async_wait.err = err;
470 tls_err_abort(sk, err);
475 struct tls_rec *first_rec;
477 /* Mark the record as ready for transmission */
478 smp_store_mb(rec->tx_ready, true);
480 /* If received record is at head of tx_list, schedule tx */
481 first_rec = list_first_entry(&ctx->tx_list,
482 struct tls_rec, list);
483 if (rec == first_rec)
487 spin_lock_bh(&ctx->encrypt_compl_lock);
488 pending = atomic_dec_return(&ctx->encrypt_pending);
490 if (!pending && ctx->async_notify)
491 complete(&ctx->async_wait.completion);
492 spin_unlock_bh(&ctx->encrypt_compl_lock);
497 /* Schedule the transmission */
498 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
499 schedule_delayed_work(&ctx->tx_work.work, 1);
502 static int tls_do_encryption(struct sock *sk,
503 struct tls_context *tls_ctx,
504 struct tls_sw_context_tx *ctx,
505 struct aead_request *aead_req,
506 size_t data_len, u32 start)
508 struct tls_prot_info *prot = &tls_ctx->prot_info;
509 struct tls_rec *rec = ctx->open_rec;
510 struct sk_msg *msg_en = &rec->msg_encrypted;
511 struct scatterlist *sge = sk_msg_elem(msg_en, start);
512 int rc, iv_offset = 0;
514 /* For CCM based ciphers, first byte of IV is a constant */
515 switch (prot->cipher_type) {
516 case TLS_CIPHER_AES_CCM_128:
517 rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
520 case TLS_CIPHER_SM4_CCM:
521 rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
526 memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
527 prot->iv_size + prot->salt_size);
529 tls_xor_iv_with_seq(prot, rec->iv_data + iv_offset,
530 tls_ctx->tx.rec_seq);
532 sge->offset += prot->prepend_size;
533 sge->length -= prot->prepend_size;
535 msg_en->sg.curr = start;
537 aead_request_set_tfm(aead_req, ctx->aead_send);
538 aead_request_set_ad(aead_req, prot->aad_size);
539 aead_request_set_crypt(aead_req, rec->sg_aead_in,
541 data_len, rec->iv_data);
543 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
544 tls_encrypt_done, sk);
546 /* Add the record in tx_list */
547 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
548 atomic_inc(&ctx->encrypt_pending);
550 rc = crypto_aead_encrypt(aead_req);
551 if (!rc || rc != -EINPROGRESS) {
552 atomic_dec(&ctx->encrypt_pending);
553 sge->offset -= prot->prepend_size;
554 sge->length += prot->prepend_size;
558 WRITE_ONCE(rec->tx_ready, true);
559 } else if (rc != -EINPROGRESS) {
560 list_del(&rec->list);
564 /* Unhook the record from context if encryption is not failure */
565 ctx->open_rec = NULL;
566 tls_advance_record_sn(sk, prot, &tls_ctx->tx);
570 static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
571 struct tls_rec **to, struct sk_msg *msg_opl,
572 struct sk_msg *msg_oen, u32 split_point,
573 u32 tx_overhead_size, u32 *orig_end)
575 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
576 struct scatterlist *sge, *osge, *nsge;
577 u32 orig_size = msg_opl->sg.size;
578 struct scatterlist tmp = { };
579 struct sk_msg *msg_npl;
583 new = tls_get_rec(sk);
586 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
587 tx_overhead_size, 0);
589 tls_free_rec(sk, new);
593 *orig_end = msg_opl->sg.end;
594 i = msg_opl->sg.start;
595 sge = sk_msg_elem(msg_opl, i);
596 while (apply && sge->length) {
597 if (sge->length > apply) {
598 u32 len = sge->length - apply;
600 get_page(sg_page(sge));
601 sg_set_page(&tmp, sg_page(sge), len,
602 sge->offset + apply);
607 apply -= sge->length;
608 bytes += sge->length;
611 sk_msg_iter_var_next(i);
612 if (i == msg_opl->sg.end)
614 sge = sk_msg_elem(msg_opl, i);
618 msg_opl->sg.curr = i;
619 msg_opl->sg.copybreak = 0;
620 msg_opl->apply_bytes = 0;
621 msg_opl->sg.size = bytes;
623 msg_npl = &new->msg_plaintext;
624 msg_npl->apply_bytes = apply;
625 msg_npl->sg.size = orig_size - bytes;
627 j = msg_npl->sg.start;
628 nsge = sk_msg_elem(msg_npl, j);
630 memcpy(nsge, &tmp, sizeof(*nsge));
631 sk_msg_iter_var_next(j);
632 nsge = sk_msg_elem(msg_npl, j);
635 osge = sk_msg_elem(msg_opl, i);
636 while (osge->length) {
637 memcpy(nsge, osge, sizeof(*nsge));
639 sk_msg_iter_var_next(i);
640 sk_msg_iter_var_next(j);
643 osge = sk_msg_elem(msg_opl, i);
644 nsge = sk_msg_elem(msg_npl, j);
648 msg_npl->sg.curr = j;
649 msg_npl->sg.copybreak = 0;
655 static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
656 struct tls_rec *from, u32 orig_end)
658 struct sk_msg *msg_npl = &from->msg_plaintext;
659 struct sk_msg *msg_opl = &to->msg_plaintext;
660 struct scatterlist *osge, *nsge;
664 sk_msg_iter_var_prev(i);
665 j = msg_npl->sg.start;
667 osge = sk_msg_elem(msg_opl, i);
668 nsge = sk_msg_elem(msg_npl, j);
670 if (sg_page(osge) == sg_page(nsge) &&
671 osge->offset + osge->length == nsge->offset) {
672 osge->length += nsge->length;
673 put_page(sg_page(nsge));
676 msg_opl->sg.end = orig_end;
677 msg_opl->sg.curr = orig_end;
678 msg_opl->sg.copybreak = 0;
679 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
680 msg_opl->sg.size += msg_npl->sg.size;
682 sk_msg_free(sk, &to->msg_encrypted);
683 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
688 static int tls_push_record(struct sock *sk, int flags,
689 unsigned char record_type)
691 struct tls_context *tls_ctx = tls_get_ctx(sk);
692 struct tls_prot_info *prot = &tls_ctx->prot_info;
693 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
694 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
695 u32 i, split_point, orig_end;
696 struct sk_msg *msg_pl, *msg_en;
697 struct aead_request *req;
704 msg_pl = &rec->msg_plaintext;
705 msg_en = &rec->msg_encrypted;
707 split_point = msg_pl->apply_bytes;
708 split = split_point && split_point < msg_pl->sg.size;
709 if (unlikely((!split &&
711 prot->overhead_size > msg_en->sg.size) ||
714 prot->overhead_size > msg_en->sg.size))) {
716 split_point = msg_en->sg.size;
719 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
720 split_point, prot->overhead_size,
724 /* This can happen if above tls_split_open_record allocates
725 * a single large encryption buffer instead of two smaller
726 * ones. In this case adjust pointers and continue without
729 if (!msg_pl->sg.size) {
730 tls_merge_open_record(sk, rec, tmp, orig_end);
731 msg_pl = &rec->msg_plaintext;
732 msg_en = &rec->msg_encrypted;
735 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
736 prot->overhead_size);
739 rec->tx_flags = flags;
740 req = &rec->aead_req;
743 sk_msg_iter_var_prev(i);
745 rec->content_type = record_type;
746 if (prot->version == TLS_1_3_VERSION) {
747 /* Add content type to end of message. No padding added */
748 sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
749 sg_mark_end(&rec->sg_content_type);
750 sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
751 &rec->sg_content_type);
753 sg_mark_end(sk_msg_elem(msg_pl, i));
756 if (msg_pl->sg.end < msg_pl->sg.start) {
757 sg_chain(&msg_pl->sg.data[msg_pl->sg.start],
758 MAX_SKB_FRAGS - msg_pl->sg.start + 1,
762 i = msg_pl->sg.start;
763 sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]);
766 sk_msg_iter_var_prev(i);
767 sg_mark_end(sk_msg_elem(msg_en, i));
769 i = msg_en->sg.start;
770 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
772 tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
773 tls_ctx->tx.rec_seq, record_type, prot);
775 tls_fill_prepend(tls_ctx,
776 page_address(sg_page(&msg_en->sg.data[i])) +
777 msg_en->sg.data[i].offset,
778 msg_pl->sg.size + prot->tail_size,
781 tls_ctx->pending_open_record_frags = false;
783 rc = tls_do_encryption(sk, tls_ctx, ctx, req,
784 msg_pl->sg.size + prot->tail_size, i);
786 if (rc != -EINPROGRESS) {
787 tls_err_abort(sk, -EBADMSG);
789 tls_ctx->pending_open_record_frags = true;
790 tls_merge_open_record(sk, rec, tmp, orig_end);
793 ctx->async_capable = 1;
796 msg_pl = &tmp->msg_plaintext;
797 msg_en = &tmp->msg_encrypted;
798 sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
799 tls_ctx->pending_open_record_frags = true;
803 return tls_tx_records(sk, flags);
806 static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
807 bool full_record, u8 record_type,
808 ssize_t *copied, int flags)
810 struct tls_context *tls_ctx = tls_get_ctx(sk);
811 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
812 struct sk_msg msg_redir = { };
813 struct sk_psock *psock;
814 struct sock *sk_redir;
820 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
821 psock = sk_psock_get(sk);
822 if (!psock || !policy) {
823 err = tls_push_record(sk, flags, record_type);
824 if (err && sk->sk_err == EBADMSG) {
825 *copied -= sk_msg_free(sk, msg);
826 tls_free_open_rec(sk);
830 sk_psock_put(sk, psock);
834 enospc = sk_msg_full(msg);
835 if (psock->eval == __SK_NONE) {
836 delta = msg->sg.size;
837 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
838 delta -= msg->sg.size;
840 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
841 !enospc && !full_record) {
847 if (msg->apply_bytes && msg->apply_bytes < send)
848 send = msg->apply_bytes;
850 switch (psock->eval) {
852 err = tls_push_record(sk, flags, record_type);
853 if (err && sk->sk_err == EBADMSG) {
854 *copied -= sk_msg_free(sk, msg);
855 tls_free_open_rec(sk);
861 sk_redir = psock->sk_redir;
862 memcpy(&msg_redir, msg, sizeof(*msg));
863 if (msg->apply_bytes < send)
864 msg->apply_bytes = 0;
866 msg->apply_bytes -= send;
867 sk_msg_return_zero(sk, msg, send);
868 msg->sg.size -= send;
870 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
873 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
876 if (msg->sg.size == 0)
877 tls_free_open_rec(sk);
881 sk_msg_free_partial(sk, msg, send);
882 if (msg->apply_bytes < send)
883 msg->apply_bytes = 0;
885 msg->apply_bytes -= send;
886 if (msg->sg.size == 0)
887 tls_free_open_rec(sk);
888 *copied -= (send + delta);
893 bool reset_eval = !ctx->open_rec;
897 msg = &rec->msg_plaintext;
898 if (!msg->apply_bytes)
902 psock->eval = __SK_NONE;
903 if (psock->sk_redir) {
904 sock_put(psock->sk_redir);
905 psock->sk_redir = NULL;
912 sk_psock_put(sk, psock);
916 static int tls_sw_push_pending_record(struct sock *sk, int flags)
918 struct tls_context *tls_ctx = tls_get_ctx(sk);
919 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
920 struct tls_rec *rec = ctx->open_rec;
921 struct sk_msg *msg_pl;
927 msg_pl = &rec->msg_plaintext;
928 copied = msg_pl->sg.size;
932 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
936 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
938 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
939 struct tls_context *tls_ctx = tls_get_ctx(sk);
940 struct tls_prot_info *prot = &tls_ctx->prot_info;
941 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
942 bool async_capable = ctx->async_capable;
943 unsigned char record_type = TLS_RECORD_TYPE_DATA;
944 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
945 bool eor = !(msg->msg_flags & MSG_MORE);
948 struct sk_msg *msg_pl, *msg_en;
959 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
963 mutex_lock(&tls_ctx->tx_lock);
966 if (unlikely(msg->msg_controllen)) {
967 ret = tls_process_cmsg(sk, msg, &record_type);
969 if (ret == -EINPROGRESS)
971 else if (ret != -EAGAIN)
976 while (msg_data_left(msg)) {
985 rec = ctx->open_rec = tls_get_rec(sk);
991 msg_pl = &rec->msg_plaintext;
992 msg_en = &rec->msg_encrypted;
994 orig_size = msg_pl->sg.size;
996 try_to_copy = msg_data_left(msg);
997 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
998 if (try_to_copy >= record_room) {
999 try_to_copy = record_room;
1003 required_size = msg_pl->sg.size + try_to_copy +
1004 prot->overhead_size;
1006 if (!sk_stream_memory_free(sk))
1007 goto wait_for_sndbuf;
1010 ret = tls_alloc_encrypted_msg(sk, required_size);
1013 goto wait_for_memory;
1015 /* Adjust try_to_copy according to the amount that was
1016 * actually allocated. The difference is due
1017 * to max sg elements limit
1019 try_to_copy -= required_size - msg_en->sg.size;
1023 if (!is_kvec && (full_record || eor) && !async_capable) {
1024 u32 first = msg_pl->sg.end;
1026 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
1027 msg_pl, try_to_copy);
1029 goto fallback_to_reg_send;
1032 copied += try_to_copy;
1034 sk_msg_sg_copy_set(msg_pl, first);
1035 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1036 record_type, &copied,
1039 if (ret == -EINPROGRESS)
1041 else if (ret == -ENOMEM)
1042 goto wait_for_memory;
1043 else if (ctx->open_rec && ret == -ENOSPC)
1045 else if (ret != -EAGAIN)
1050 copied -= try_to_copy;
1051 sk_msg_sg_copy_clear(msg_pl, first);
1052 iov_iter_revert(&msg->msg_iter,
1053 msg_pl->sg.size - orig_size);
1054 fallback_to_reg_send:
1055 sk_msg_trim(sk, msg_pl, orig_size);
1058 required_size = msg_pl->sg.size + try_to_copy;
1060 ret = tls_clone_plaintext_msg(sk, required_size);
1065 /* Adjust try_to_copy according to the amount that was
1066 * actually allocated. The difference is due
1067 * to max sg elements limit
1069 try_to_copy -= required_size - msg_pl->sg.size;
1071 sk_msg_trim(sk, msg_en,
1072 msg_pl->sg.size + prot->overhead_size);
1076 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
1077 msg_pl, try_to_copy);
1082 /* Open records defined only if successfully copied, otherwise
1083 * we would trim the sg but not reset the open record frags.
1085 tls_ctx->pending_open_record_frags = true;
1086 copied += try_to_copy;
1087 if (full_record || eor) {
1088 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1089 record_type, &copied,
1092 if (ret == -EINPROGRESS)
1094 else if (ret == -ENOMEM)
1095 goto wait_for_memory;
1096 else if (ret != -EAGAIN) {
1107 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1109 ret = sk_stream_wait_memory(sk, &timeo);
1113 tls_trim_both_msgs(sk, orig_size);
1117 if (ctx->open_rec && msg_en->sg.size < required_size)
1118 goto alloc_encrypted;
1123 } else if (num_zc) {
1124 /* Wait for pending encryptions to get completed */
1125 spin_lock_bh(&ctx->encrypt_compl_lock);
1126 ctx->async_notify = true;
1128 pending = atomic_read(&ctx->encrypt_pending);
1129 spin_unlock_bh(&ctx->encrypt_compl_lock);
1131 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1133 reinit_completion(&ctx->async_wait.completion);
1135 /* There can be no concurrent accesses, since we have no
1136 * pending encrypt operations
1138 WRITE_ONCE(ctx->async_notify, false);
1140 if (ctx->async_wait.err) {
1141 ret = ctx->async_wait.err;
1146 /* Transmit if any encryptions have completed */
1147 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1148 cancel_delayed_work(&ctx->tx_work.work);
1149 tls_tx_records(sk, msg->msg_flags);
1153 ret = sk_stream_error(sk, msg->msg_flags, ret);
1156 mutex_unlock(&tls_ctx->tx_lock);
1157 return copied > 0 ? copied : ret;
1160 static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1161 int offset, size_t size, int flags)
1163 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1164 struct tls_context *tls_ctx = tls_get_ctx(sk);
1165 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1166 struct tls_prot_info *prot = &tls_ctx->prot_info;
1167 unsigned char record_type = TLS_RECORD_TYPE_DATA;
1168 struct sk_msg *msg_pl;
1169 struct tls_rec *rec;
1177 eor = !(flags & MSG_SENDPAGE_NOTLAST);
1178 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1180 /* Call the sk_stream functions to manage the sndbuf mem. */
1182 size_t copy, required_size;
1190 rec = ctx->open_rec;
1192 rec = ctx->open_rec = tls_get_rec(sk);
1198 msg_pl = &rec->msg_plaintext;
1200 full_record = false;
1201 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
1203 if (copy >= record_room) {
1208 required_size = msg_pl->sg.size + copy + prot->overhead_size;
1210 if (!sk_stream_memory_free(sk))
1211 goto wait_for_sndbuf;
1213 ret = tls_alloc_encrypted_msg(sk, required_size);
1216 goto wait_for_memory;
1218 /* Adjust copy according to the amount that was
1219 * actually allocated. The difference is due
1220 * to max sg elements limit
1222 copy -= required_size - msg_pl->sg.size;
1226 sk_msg_page_add(msg_pl, page, copy, offset);
1227 sk_mem_charge(sk, copy);
1233 tls_ctx->pending_open_record_frags = true;
1234 if (full_record || eor || sk_msg_full(msg_pl)) {
1235 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1236 record_type, &copied, flags);
1238 if (ret == -EINPROGRESS)
1240 else if (ret == -ENOMEM)
1241 goto wait_for_memory;
1242 else if (ret != -EAGAIN) {
1251 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1253 ret = sk_stream_wait_memory(sk, &timeo);
1256 tls_trim_both_msgs(sk, msg_pl->sg.size);
1265 /* Transmit if any encryptions have completed */
1266 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1267 cancel_delayed_work(&ctx->tx_work.work);
1268 tls_tx_records(sk, flags);
1272 ret = sk_stream_error(sk, flags, ret);
1273 return copied > 0 ? copied : ret;
1276 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1277 int offset, size_t size, int flags)
1279 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1280 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
1281 MSG_NO_SHARED_FRAGS))
1284 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1287 int tls_sw_sendpage(struct sock *sk, struct page *page,
1288 int offset, size_t size, int flags)
1290 struct tls_context *tls_ctx = tls_get_ctx(sk);
1293 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1294 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1297 mutex_lock(&tls_ctx->tx_lock);
1299 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1301 mutex_unlock(&tls_ctx->tx_lock);
1306 tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
1309 struct tls_context *tls_ctx = tls_get_ctx(sk);
1310 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1311 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1313 while (!ctx->recv_pkt) {
1314 if (!sk_psock_queue_empty(psock))
1318 return sock_error(sk);
1320 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1321 __strp_unpause(&ctx->strp);
1326 if (sk->sk_shutdown & RCV_SHUTDOWN)
1329 if (sock_flag(sk, SOCK_DONE))
1332 if (nonblock || !timeo)
1335 add_wait_queue(sk_sleep(sk), &wait);
1336 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1337 sk_wait_event(sk, &timeo,
1338 ctx->recv_pkt || !sk_psock_queue_empty(psock),
1340 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1341 remove_wait_queue(sk_sleep(sk), &wait);
1343 /* Handle signals */
1344 if (signal_pending(current))
1345 return sock_intr_errno(timeo);
1351 static int tls_setup_from_iter(struct iov_iter *from,
1352 int length, int *pages_used,
1353 struct scatterlist *to,
1356 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1357 struct page *pages[MAX_SKB_FRAGS];
1358 unsigned int size = 0;
1359 ssize_t copied, use;
1362 while (length > 0) {
1364 maxpages = to_max_pages - num_elem;
1365 if (maxpages == 0) {
1369 copied = iov_iter_get_pages(from, pages,
1377 iov_iter_advance(from, copied);
1382 use = min_t(int, copied, PAGE_SIZE - offset);
1384 sg_set_page(&to[num_elem],
1385 pages[i], use, offset);
1386 sg_unmark_end(&to[num_elem]);
1387 /* We do not uncharge memory from this API */
1396 /* Mark the end in the last sg entry if newly added */
1397 if (num_elem > *pages_used)
1398 sg_mark_end(&to[num_elem - 1]);
1401 iov_iter_revert(from, size);
1402 *pages_used = num_elem;
1407 /* This function decrypts the input skb into either out_iov or in out_sg
1408 * or in skb buffers itself. The input parameter 'zc' indicates if
1409 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1410 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1411 * NULL, then the decryption happens inside skb buffers itself, i.e.
1412 * zero-copy gets disabled and 'zc' is updated.
1415 static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1416 struct iov_iter *out_iov,
1417 struct scatterlist *out_sg,
1418 struct tls_decrypt_arg *darg)
1420 struct tls_context *tls_ctx = tls_get_ctx(sk);
1421 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1422 struct tls_prot_info *prot = &tls_ctx->prot_info;
1423 int n_sgin, n_sgout, aead_size, err, pages = 0;
1424 struct strp_msg *rxm = strp_msg(skb);
1425 struct tls_msg *tlm = tls_msg(skb);
1426 struct aead_request *aead_req;
1427 struct sk_buff *unused;
1428 struct scatterlist *sgin = NULL;
1429 struct scatterlist *sgout = NULL;
1430 const int data_len = rxm->full_len - prot->overhead_size;
1431 int tail_pages = !!prot->tail_size;
1432 struct tls_decrypt_ctx *dctx;
1436 if (darg->zc && (out_iov || out_sg)) {
1438 n_sgout = 1 + tail_pages +
1439 iov_iter_npages_cap(out_iov, INT_MAX, data_len);
1441 n_sgout = sg_nents(out_sg);
1442 n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
1443 rxm->full_len - prot->prepend_size);
1447 n_sgin = skb_cow_data(skb, 0, &unused);
1453 /* Increment to accommodate AAD */
1454 n_sgin = n_sgin + 1;
1456 /* Allocate a single block of memory which contains
1457 * aead_req || tls_decrypt_ctx.
1458 * Both structs are variable length.
1460 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1461 mem = kmalloc(aead_size + struct_size(dctx, sg, n_sgin + n_sgout),
1466 /* Segment the allocated memory */
1467 aead_req = (struct aead_request *)mem;
1468 dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
1469 sgin = &dctx->sg[0];
1470 sgout = &dctx->sg[n_sgin];
1472 /* For CCM based ciphers, first byte of nonce+iv is a constant */
1473 switch (prot->cipher_type) {
1474 case TLS_CIPHER_AES_CCM_128:
1475 dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE;
1478 case TLS_CIPHER_SM4_CCM:
1479 dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
1485 if (prot->version == TLS_1_3_VERSION ||
1486 prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
1487 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv,
1488 prot->iv_size + prot->salt_size);
1490 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1491 &dctx->iv[iv_offset] + prot->salt_size,
1495 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size);
1497 tls_xor_iv_with_seq(prot, &dctx->iv[iv_offset], tls_ctx->rx.rec_seq);
1500 tls_make_aad(dctx->aad, rxm->full_len - prot->overhead_size +
1502 tls_ctx->rx.rec_seq, tlm->control, prot);
1505 sg_init_table(sgin, n_sgin);
1506 sg_set_buf(&sgin[0], dctx->aad, prot->aad_size);
1507 err = skb_to_sgvec(skb, &sgin[1],
1508 rxm->offset + prot->prepend_size,
1509 rxm->full_len - prot->prepend_size);
1515 sg_init_table(sgout, n_sgout);
1516 sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
1518 err = tls_setup_from_iter(out_iov, data_len,
1520 (n_sgout - 1 - tail_pages));
1522 goto fallback_to_reg_recv;
1524 if (prot->tail_size) {
1525 sg_unmark_end(&sgout[pages]);
1526 sg_set_buf(&sgout[pages + 1], &dctx->tail,
1528 sg_mark_end(&sgout[pages + 1]);
1530 } else if (out_sg) {
1531 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1533 goto fallback_to_reg_recv;
1536 fallback_to_reg_recv:
1542 /* Prepare and submit AEAD request */
1543 err = tls_do_decryption(sk, skb, sgin, sgout, dctx->iv,
1544 data_len + prot->tail_size, aead_req, darg);
1548 if (prot->tail_size)
1549 darg->tail = dctx->tail;
1551 /* Release the pages in case iov was mapped to pages */
1552 for (; pages > 0; pages--)
1553 put_page(sg_page(&sgout[pages]));
1559 static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
1560 struct iov_iter *dest,
1561 struct tls_decrypt_arg *darg)
1563 struct tls_context *tls_ctx = tls_get_ctx(sk);
1564 struct tls_prot_info *prot = &tls_ctx->prot_info;
1565 struct strp_msg *rxm = strp_msg(skb);
1566 struct tls_msg *tlm = tls_msg(skb);
1569 if (tlm->decrypted) {
1571 darg->async = false;
1575 if (tls_ctx->rx_conf == TLS_HW) {
1576 err = tls_device_decrypted(sk, tls_ctx, skb, rxm);
1582 darg->async = false;
1587 err = decrypt_internal(sk, skb, dest, NULL, darg);
1589 if (err == -EBADMSG)
1590 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
1595 /* If opportunistic TLS 1.3 ZC failed retry without ZC */
1596 if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
1597 darg->tail != TLS_RECORD_TYPE_DATA)) {
1599 TLS_INC_STATS(sock_net(sk), LINUX_MIN_TLSDECRYPTRETRY);
1600 return decrypt_skb_update(sk, skb, dest, darg);
1604 pad = tls_padding_length(prot, skb, darg);
1608 rxm->full_len -= pad;
1609 rxm->offset += prot->prepend_size;
1610 rxm->full_len -= prot->overhead_size;
1613 tls_advance_record_sn(sk, prot, &tls_ctx->rx);
1618 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1619 struct scatterlist *sgout)
1621 struct tls_decrypt_arg darg = { .zc = true, };
1623 return decrypt_internal(sk, skb, NULL, sgout, &darg);
1626 static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
1632 *control = tlm->control;
1636 err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1637 sizeof(*control), control);
1638 if (*control != TLS_RECORD_TYPE_DATA) {
1639 if (err || msg->msg_flags & MSG_CTRUNC)
1642 } else if (*control != tlm->control) {
1649 /* This function traverses the rx_list in tls receive context to copies the
1650 * decrypted records into the buffer provided by caller zero copy is not
1651 * true. Further, the records are removed from the rx_list if it is not a peek
1652 * case and the record has been consumed completely.
1654 static int process_rx_list(struct tls_sw_context_rx *ctx,
1662 struct sk_buff *skb = skb_peek(&ctx->rx_list);
1663 struct tls_msg *tlm;
1667 while (skip && skb) {
1668 struct strp_msg *rxm = strp_msg(skb);
1671 err = tls_record_content_type(msg, tlm, control);
1675 if (skip < rxm->full_len)
1678 skip = skip - rxm->full_len;
1679 skb = skb_peek_next(skb, &ctx->rx_list);
1682 while (len && skb) {
1683 struct sk_buff *next_skb;
1684 struct strp_msg *rxm = strp_msg(skb);
1685 int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1689 err = tls_record_content_type(msg, tlm, control);
1693 if (!zc || (rxm->full_len - skip) > len) {
1694 err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1701 copied = copied + chunk;
1703 /* Consume the data from record if it is non-peek case*/
1705 rxm->offset = rxm->offset + chunk;
1706 rxm->full_len = rxm->full_len - chunk;
1708 /* Return if there is unconsumed data in the record */
1709 if (rxm->full_len - skip)
1713 /* The remaining skip-bytes must lie in 1st record in rx_list.
1714 * So from the 2nd record, 'skip' should be 0.
1719 msg->msg_flags |= MSG_EOR;
1721 next_skb = skb_peek_next(skb, &ctx->rx_list);
1724 __skb_unlink(skb, &ctx->rx_list);
1733 return copied ? : err;
1737 tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
1738 size_t len_left, size_t decrypted, ssize_t done,
1743 if (len_left <= decrypted)
1746 max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
1747 if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
1751 sk_flush_backlog(sk);
1754 int tls_sw_recvmsg(struct sock *sk,
1760 struct tls_context *tls_ctx = tls_get_ctx(sk);
1761 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1762 struct tls_prot_info *prot = &tls_ctx->prot_info;
1763 struct sk_psock *psock;
1764 unsigned char control = 0;
1765 ssize_t decrypted = 0;
1766 size_t flushed_at = 0;
1767 struct strp_msg *rxm;
1768 struct tls_msg *tlm;
1769 struct sk_buff *skb;
1772 int target, err = 0;
1774 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
1775 bool is_peek = flags & MSG_PEEK;
1776 bool bpf_strp_enabled;
1779 if (unlikely(flags & MSG_ERRQUEUE))
1780 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1782 psock = sk_psock_get(sk);
1784 bpf_strp_enabled = sk_psock_strp_enabled(psock);
1786 /* If crypto failed the connection is broken */
1787 err = ctx->async_wait.err;
1791 /* Process pending decrypted records. It must be non-zero-copy */
1792 err = process_rx_list(ctx, msg, &control, 0, len, false, is_peek);
1800 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1802 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1804 zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
1807 while (len && (decrypted + copied < target || ctx->recv_pkt)) {
1808 struct tls_decrypt_arg darg = {};
1809 int to_decrypt, chunk;
1811 err = tls_rx_rec_wait(sk, psock, flags & MSG_DONTWAIT, timeo);
1814 chunk = sk_msg_recvmsg(sk, psock, msg, len,
1822 skb = ctx->recv_pkt;
1823 rxm = strp_msg(skb);
1826 to_decrypt = rxm->full_len - prot->overhead_size;
1828 if (zc_capable && to_decrypt <= len &&
1829 tlm->control == TLS_RECORD_TYPE_DATA)
1832 /* Do not use async mode if record is non-data */
1833 if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
1834 darg.async = ctx->async_capable;
1838 err = decrypt_skb_update(sk, skb, &msg->msg_iter, &darg);
1840 tls_err_abort(sk, -EBADMSG);
1844 async |= darg.async;
1846 /* If the type of records being processed is not known yet,
1847 * set it to record type just dequeued. If it is already known,
1848 * but does not match the record type just dequeued, go to end.
1849 * We always get record type here since for tls1.2, record type
1850 * is known just after record is dequeued from stream parser.
1851 * For tls1.3, we disable async.
1853 err = tls_record_content_type(msg, tlm, &control);
1857 /* periodically flush backlog, and feed strparser */
1858 tls_read_flush_backlog(sk, prot, len, to_decrypt,
1859 decrypted + copied, &flushed_at);
1861 ctx->recv_pkt = NULL;
1862 __strp_unpause(&ctx->strp);
1863 __skb_queue_tail(&ctx->rx_list, skb);
1866 /* TLS 1.2-only, to_decrypt must be text length */
1867 chunk = min_t(int, to_decrypt, len);
1873 /* TLS 1.3 may have updated the length by more than overhead */
1874 chunk = rxm->full_len;
1877 bool partially_consumed = chunk > len;
1879 if (bpf_strp_enabled) {
1880 /* BPF may try to queue the skb */
1881 __skb_unlink(skb, &ctx->rx_list);
1882 err = sk_psock_tls_strp_read(psock, skb);
1883 if (err != __SK_PASS) {
1884 rxm->offset = rxm->offset + rxm->full_len;
1886 if (err == __SK_DROP)
1890 __skb_queue_tail(&ctx->rx_list, skb);
1893 if (partially_consumed)
1896 err = skb_copy_datagram_msg(skb, rxm->offset,
1904 if (partially_consumed) {
1905 rxm->offset += chunk;
1906 rxm->full_len -= chunk;
1914 __skb_unlink(skb, &ctx->rx_list);
1917 /* Return full control message to userspace before trying
1918 * to parse another message type
1920 msg->msg_flags |= MSG_EOR;
1921 if (control != TLS_RECORD_TYPE_DATA)
1929 /* Wait for all previously submitted records to be decrypted */
1930 spin_lock_bh(&ctx->decrypt_compl_lock);
1931 reinit_completion(&ctx->async_wait.completion);
1932 pending = atomic_read(&ctx->decrypt_pending);
1933 spin_unlock_bh(&ctx->decrypt_compl_lock);
1935 ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1937 if (err >= 0 || err == -EINPROGRESS)
1944 /* Drain records from the rx_list & copy if required */
1945 if (is_peek || is_kvec)
1946 err = process_rx_list(ctx, msg, &control, copied,
1947 decrypted, false, is_peek);
1949 err = process_rx_list(ctx, msg, &control, 0,
1950 decrypted, true, is_peek);
1951 decrypted = max(err, 0);
1954 copied += decrypted;
1959 sk_psock_put(sk, psock);
1960 return copied ? : err;
1963 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1964 struct pipe_inode_info *pipe,
1965 size_t len, unsigned int flags)
1967 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
1968 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1969 struct strp_msg *rxm = NULL;
1970 struct sock *sk = sock->sk;
1971 struct tls_msg *tlm;
1972 struct sk_buff *skb;
1981 timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK);
1983 from_queue = !skb_queue_empty(&ctx->rx_list);
1985 skb = __skb_dequeue(&ctx->rx_list);
1987 struct tls_decrypt_arg darg = {};
1989 err = tls_rx_rec_wait(sk, NULL, flags & SPLICE_F_NONBLOCK,
1992 goto splice_read_end;
1994 skb = ctx->recv_pkt;
1996 err = decrypt_skb_update(sk, skb, NULL, &darg);
1998 tls_err_abort(sk, -EBADMSG);
1999 goto splice_read_end;
2003 rxm = strp_msg(skb);
2006 /* splice does not support reading control messages */
2007 if (tlm->control != TLS_RECORD_TYPE_DATA) {
2009 goto splice_read_end;
2012 chunk = min_t(unsigned int, rxm->full_len, len);
2013 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
2015 goto splice_read_end;
2018 ctx->recv_pkt = NULL;
2019 __strp_unpause(&ctx->strp);
2021 if (chunk < rxm->full_len) {
2022 __skb_queue_head(&ctx->rx_list, skb);
2024 rxm->full_len -= len;
2031 return copied ? : err;
2034 bool tls_sw_sock_is_readable(struct sock *sk)
2036 struct tls_context *tls_ctx = tls_get_ctx(sk);
2037 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2038 bool ingress_empty = true;
2039 struct sk_psock *psock;
2042 psock = sk_psock(sk);
2044 ingress_empty = list_empty(&psock->ingress_msg);
2047 return !ingress_empty || ctx->recv_pkt ||
2048 !skb_queue_empty(&ctx->rx_list);
2051 static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
2053 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2054 struct tls_prot_info *prot = &tls_ctx->prot_info;
2055 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
2056 struct strp_msg *rxm = strp_msg(skb);
2057 struct tls_msg *tlm = tls_msg(skb);
2058 size_t cipher_overhead;
2059 size_t data_len = 0;
2062 /* Verify that we have a full TLS header, or wait for more data */
2063 if (rxm->offset + prot->prepend_size > skb->len)
2066 /* Sanity-check size of on-stack buffer. */
2067 if (WARN_ON(prot->prepend_size > sizeof(header))) {
2072 /* Linearize header to local buffer */
2073 ret = skb_copy_bits(skb, rxm->offset, header, prot->prepend_size);
2078 tlm->control = header[0];
2080 data_len = ((header[4] & 0xFF) | (header[3] << 8));
2082 cipher_overhead = prot->tag_size;
2083 if (prot->version != TLS_1_3_VERSION &&
2084 prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
2085 cipher_overhead += prot->iv_size;
2087 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
2092 if (data_len < cipher_overhead) {
2097 /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
2098 if (header[1] != TLS_1_2_VERSION_MINOR ||
2099 header[2] != TLS_1_2_VERSION_MAJOR) {
2104 tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
2105 TCP_SKB_CB(skb)->seq + rxm->offset);
2106 return data_len + TLS_HEADER_SIZE;
2109 tls_err_abort(strp->sk, ret);
2114 static void tls_queue(struct strparser *strp, struct sk_buff *skb)
2116 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2117 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2119 ctx->recv_pkt = skb;
2122 ctx->saved_data_ready(strp->sk);
2125 static void tls_data_ready(struct sock *sk)
2127 struct tls_context *tls_ctx = tls_get_ctx(sk);
2128 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2129 struct sk_psock *psock;
2131 strp_data_ready(&ctx->strp);
2133 psock = sk_psock_get(sk);
2135 if (!list_empty(&psock->ingress_msg))
2136 ctx->saved_data_ready(sk);
2137 sk_psock_put(sk, psock);
2141 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
2143 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2145 set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
2146 set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
2147 cancel_delayed_work_sync(&ctx->tx_work.work);
2150 void tls_sw_release_resources_tx(struct sock *sk)
2152 struct tls_context *tls_ctx = tls_get_ctx(sk);
2153 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2154 struct tls_rec *rec, *tmp;
2157 /* Wait for any pending async encryptions to complete */
2158 spin_lock_bh(&ctx->encrypt_compl_lock);
2159 ctx->async_notify = true;
2160 pending = atomic_read(&ctx->encrypt_pending);
2161 spin_unlock_bh(&ctx->encrypt_compl_lock);
2164 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2166 tls_tx_records(sk, -1);
2168 /* Free up un-sent records in tx_list. First, free
2169 * the partially sent record if any at head of tx_list.
2171 if (tls_ctx->partially_sent_record) {
2172 tls_free_partial_record(sk, tls_ctx);
2173 rec = list_first_entry(&ctx->tx_list,
2174 struct tls_rec, list);
2175 list_del(&rec->list);
2176 sk_msg_free(sk, &rec->msg_plaintext);
2180 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
2181 list_del(&rec->list);
2182 sk_msg_free(sk, &rec->msg_encrypted);
2183 sk_msg_free(sk, &rec->msg_plaintext);
2187 crypto_free_aead(ctx->aead_send);
2188 tls_free_open_rec(sk);
2191 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
2193 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2198 void tls_sw_release_resources_rx(struct sock *sk)
2200 struct tls_context *tls_ctx = tls_get_ctx(sk);
2201 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2203 kfree(tls_ctx->rx.rec_seq);
2204 kfree(tls_ctx->rx.iv);
2206 if (ctx->aead_recv) {
2207 kfree_skb(ctx->recv_pkt);
2208 ctx->recv_pkt = NULL;
2209 __skb_queue_purge(&ctx->rx_list);
2210 crypto_free_aead(ctx->aead_recv);
2211 strp_stop(&ctx->strp);
2212 /* If tls_sw_strparser_arm() was not called (cleanup paths)
2213 * we still want to strp_stop(), but sk->sk_data_ready was
2216 if (ctx->saved_data_ready) {
2217 write_lock_bh(&sk->sk_callback_lock);
2218 sk->sk_data_ready = ctx->saved_data_ready;
2219 write_unlock_bh(&sk->sk_callback_lock);
2224 void tls_sw_strparser_done(struct tls_context *tls_ctx)
2226 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2228 strp_done(&ctx->strp);
2231 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
2233 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2238 void tls_sw_free_resources_rx(struct sock *sk)
2240 struct tls_context *tls_ctx = tls_get_ctx(sk);
2242 tls_sw_release_resources_rx(sk);
2243 tls_sw_free_ctx_rx(tls_ctx);
2246 /* The work handler to transmitt the encrypted records in tx_list */
2247 static void tx_work_handler(struct work_struct *work)
2249 struct delayed_work *delayed_work = to_delayed_work(work);
2250 struct tx_work *tx_work = container_of(delayed_work,
2251 struct tx_work, work);
2252 struct sock *sk = tx_work->sk;
2253 struct tls_context *tls_ctx = tls_get_ctx(sk);
2254 struct tls_sw_context_tx *ctx;
2256 if (unlikely(!tls_ctx))
2259 ctx = tls_sw_ctx_tx(tls_ctx);
2260 if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
2263 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
2265 mutex_lock(&tls_ctx->tx_lock);
2267 tls_tx_records(sk, -1);
2269 mutex_unlock(&tls_ctx->tx_lock);
2272 static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)
2274 struct tls_rec *rec;
2276 rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
2280 return READ_ONCE(rec->tx_ready);
2283 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
2285 struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
2287 /* Schedule the transmission if tx list is ready */
2288 if (tls_is_tx_ready(tx_ctx) &&
2289 !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
2290 schedule_delayed_work(&tx_ctx->tx_work.work, 0);
2293 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
2295 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2297 write_lock_bh(&sk->sk_callback_lock);
2298 rx_ctx->saved_data_ready = sk->sk_data_ready;
2299 sk->sk_data_ready = tls_data_ready;
2300 write_unlock_bh(&sk->sk_callback_lock);
2302 strp_check_rcv(&rx_ctx->strp);
2305 void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
2307 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2309 rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
2310 tls_ctx->prot_info.version != TLS_1_3_VERSION;
2313 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2315 struct tls_context *tls_ctx = tls_get_ctx(sk);
2316 struct tls_prot_info *prot = &tls_ctx->prot_info;
2317 struct tls_crypto_info *crypto_info;
2318 struct tls_sw_context_tx *sw_ctx_tx = NULL;
2319 struct tls_sw_context_rx *sw_ctx_rx = NULL;
2320 struct cipher_context *cctx;
2321 struct crypto_aead **aead;
2322 struct strp_callbacks cb;
2323 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
2324 struct crypto_tfm *tfm;
2325 char *iv, *rec_seq, *key, *salt, *cipher_name;
2335 if (!ctx->priv_ctx_tx) {
2336 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2341 ctx->priv_ctx_tx = sw_ctx_tx;
2344 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
2347 if (!ctx->priv_ctx_rx) {
2348 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2353 ctx->priv_ctx_rx = sw_ctx_rx;
2356 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
2361 crypto_init_wait(&sw_ctx_tx->async_wait);
2362 spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
2363 crypto_info = &ctx->crypto_send.info;
2365 aead = &sw_ctx_tx->aead_send;
2366 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
2367 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2368 sw_ctx_tx->tx_work.sk = sk;
2370 crypto_init_wait(&sw_ctx_rx->async_wait);
2371 spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
2372 crypto_info = &ctx->crypto_recv.info;
2374 skb_queue_head_init(&sw_ctx_rx->rx_list);
2375 aead = &sw_ctx_rx->aead_recv;
2378 switch (crypto_info->cipher_type) {
2379 case TLS_CIPHER_AES_GCM_128: {
2380 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
2382 gcm_128_info = (void *)crypto_info;
2383 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2384 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
2385 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2386 iv = gcm_128_info->iv;
2387 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
2388 rec_seq = gcm_128_info->rec_seq;
2389 keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
2390 key = gcm_128_info->key;
2391 salt = gcm_128_info->salt;
2392 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
2393 cipher_name = "gcm(aes)";
2396 case TLS_CIPHER_AES_GCM_256: {
2397 struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
2399 gcm_256_info = (void *)crypto_info;
2400 nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2401 tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
2402 iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2403 iv = gcm_256_info->iv;
2404 rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
2405 rec_seq = gcm_256_info->rec_seq;
2406 keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
2407 key = gcm_256_info->key;
2408 salt = gcm_256_info->salt;
2409 salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
2410 cipher_name = "gcm(aes)";
2413 case TLS_CIPHER_AES_CCM_128: {
2414 struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
2416 ccm_128_info = (void *)crypto_info;
2417 nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2418 tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
2419 iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2420 iv = ccm_128_info->iv;
2421 rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
2422 rec_seq = ccm_128_info->rec_seq;
2423 keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
2424 key = ccm_128_info->key;
2425 salt = ccm_128_info->salt;
2426 salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
2427 cipher_name = "ccm(aes)";
2430 case TLS_CIPHER_CHACHA20_POLY1305: {
2431 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info;
2433 chacha20_poly1305_info = (void *)crypto_info;
2435 tag_size = TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE;
2436 iv_size = TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE;
2437 iv = chacha20_poly1305_info->iv;
2438 rec_seq_size = TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE;
2439 rec_seq = chacha20_poly1305_info->rec_seq;
2440 keysize = TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE;
2441 key = chacha20_poly1305_info->key;
2442 salt = chacha20_poly1305_info->salt;
2443 salt_size = TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE;
2444 cipher_name = "rfc7539(chacha20,poly1305)";
2447 case TLS_CIPHER_SM4_GCM: {
2448 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info;
2450 sm4_gcm_info = (void *)crypto_info;
2451 nonce_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2452 tag_size = TLS_CIPHER_SM4_GCM_TAG_SIZE;
2453 iv_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2454 iv = sm4_gcm_info->iv;
2455 rec_seq_size = TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE;
2456 rec_seq = sm4_gcm_info->rec_seq;
2457 keysize = TLS_CIPHER_SM4_GCM_KEY_SIZE;
2458 key = sm4_gcm_info->key;
2459 salt = sm4_gcm_info->salt;
2460 salt_size = TLS_CIPHER_SM4_GCM_SALT_SIZE;
2461 cipher_name = "gcm(sm4)";
2464 case TLS_CIPHER_SM4_CCM: {
2465 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info;
2467 sm4_ccm_info = (void *)crypto_info;
2468 nonce_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2469 tag_size = TLS_CIPHER_SM4_CCM_TAG_SIZE;
2470 iv_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2471 iv = sm4_ccm_info->iv;
2472 rec_seq_size = TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE;
2473 rec_seq = sm4_ccm_info->rec_seq;
2474 keysize = TLS_CIPHER_SM4_CCM_KEY_SIZE;
2475 key = sm4_ccm_info->key;
2476 salt = sm4_ccm_info->salt;
2477 salt_size = TLS_CIPHER_SM4_CCM_SALT_SIZE;
2478 cipher_name = "ccm(sm4)";
2486 if (crypto_info->version == TLS_1_3_VERSION) {
2488 prot->aad_size = TLS_HEADER_SIZE;
2489 prot->tail_size = 1;
2491 prot->aad_size = TLS_AAD_SPACE_SIZE;
2492 prot->tail_size = 0;
2495 /* Sanity-check the sizes for stack allocations. */
2496 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
2497 rec_seq_size > TLS_MAX_REC_SEQ_SIZE || tag_size != TLS_TAG_SIZE ||
2498 prot->aad_size > TLS_MAX_AAD_SIZE) {
2503 prot->version = crypto_info->version;
2504 prot->cipher_type = crypto_info->cipher_type;
2505 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
2506 prot->tag_size = tag_size;
2507 prot->overhead_size = prot->prepend_size +
2508 prot->tag_size + prot->tail_size;
2509 prot->iv_size = iv_size;
2510 prot->salt_size = salt_size;
2511 cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
2516 /* Note: 128 & 256 bit salt are the same size */
2517 prot->rec_seq_size = rec_seq_size;
2518 memcpy(cctx->iv, salt, salt_size);
2519 memcpy(cctx->iv + salt_size, iv, iv_size);
2520 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
2521 if (!cctx->rec_seq) {
2527 *aead = crypto_alloc_aead(cipher_name, 0, 0);
2528 if (IS_ERR(*aead)) {
2529 rc = PTR_ERR(*aead);
2535 ctx->push_pending_record = tls_sw_push_pending_record;
2537 rc = crypto_aead_setkey(*aead, key, keysize);
2542 rc = crypto_aead_setauthsize(*aead, prot->tag_size);
2547 tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
2549 tls_update_rx_zc_capable(ctx);
2550 sw_ctx_rx->async_capable =
2551 crypto_info->version != TLS_1_3_VERSION &&
2552 !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
2554 /* Set up strparser */
2555 memset(&cb, 0, sizeof(cb));
2556 cb.rcv_msg = tls_queue;
2557 cb.parse_msg = tls_read_size;
2559 strp_init(&sw_ctx_rx->strp, sk, &cb);
2565 crypto_free_aead(*aead);
2568 kfree(cctx->rec_seq);
2569 cctx->rec_seq = NULL;
2575 kfree(ctx->priv_ctx_tx);
2576 ctx->priv_ctx_tx = NULL;
2578 kfree(ctx->priv_ctx_rx);
2579 ctx->priv_ctx_rx = NULL;